141edb306SCy Schubert 241edb306SCy Schubert /* 341edb306SCy Schubert * Copyright (C) 2012 by Darren Reed. 441edb306SCy Schubert * 541edb306SCy Schubert * See the IPFILTER.LICENCE file for details on licencing. 641edb306SCy Schubert * $Id$ 741edb306SCy Schubert */ 841edb306SCy Schubert 941edb306SCy Schubert #ifndef __IPF_H__ 1041edb306SCy Schubert #define __IPF_H__ 1141edb306SCy Schubert 1241edb306SCy Schubert 1341edb306SCy Schubert #include <sys/param.h> 1441edb306SCy Schubert #include <sys/types.h> 1541edb306SCy Schubert #include <sys/file.h> 1641edb306SCy Schubert /* 1741edb306SCy Schubert * This is a workaround for <sys/uio.h> troubles on FreeBSD, HPUX, OpenBSD. 1841edb306SCy Schubert * Needed here because on some systems <sys/uio.h> gets included by things 1941edb306SCy Schubert * like <sys/socket.h> 2041edb306SCy Schubert */ 2141edb306SCy Schubert #ifndef _KERNEL 2241edb306SCy Schubert # define ADD_KERNEL 2341edb306SCy Schubert # define _KERNEL 2441edb306SCy Schubert # define KERNEL 2541edb306SCy Schubert #endif 2641edb306SCy Schubert #include <sys/uio.h> 2741edb306SCy Schubert #ifdef ADD_KERNEL 2841edb306SCy Schubert # undef _KERNEL 2941edb306SCy Schubert # undef KERNEL 3041edb306SCy Schubert #endif 3141edb306SCy Schubert #include <sys/time.h> 3241edb306SCy Schubert #include <sys/socket.h> 3341edb306SCy Schubert #include <net/if.h> 3441edb306SCy Schubert 3541edb306SCy Schubert #include <netinet/in.h> 3641edb306SCy Schubert #include <netinet/in_systm.h> 3741edb306SCy Schubert #include <netinet/ip.h> 3841edb306SCy Schubert #include <netinet/ip_icmp.h> 3941edb306SCy Schubert # include <netinet/tcp.h> 4041edb306SCy Schubert #include <netinet/udp.h> 4141edb306SCy Schubert 4241edb306SCy Schubert #include <arpa/inet.h> 4341edb306SCy Schubert 4441edb306SCy Schubert #include <errno.h> 4541edb306SCy Schubert #include <limits.h> 4641edb306SCy Schubert #include <netdb.h> 471fcc5000SCy Schubert #include <stdarg.h> 4841edb306SCy Schubert #include <stdlib.h> 4941edb306SCy Schubert #include <stddef.h> 5041edb306SCy Schubert #include <stdio.h> 5141edb306SCy Schubert #if !defined(__SVR4) && !defined(__svr4__) && defined(sun) 5241edb306SCy Schubert # include <strings.h> 5341edb306SCy Schubert #endif 5441edb306SCy Schubert #include <string.h> 5541edb306SCy Schubert #include <unistd.h> 5641edb306SCy Schubert 5741edb306SCy Schubert #include "netinet/ip_compat.h" 5841edb306SCy Schubert #include "netinet/ip_fil.h" 5941edb306SCy Schubert #include "netinet/ip_nat.h" 6041edb306SCy Schubert #include "netinet/ip_frag.h" 6141edb306SCy Schubert #include "netinet/ip_state.h" 6241edb306SCy Schubert #include "netinet/ip_proxy.h" 6341edb306SCy Schubert #include "netinet/ip_auth.h" 6441edb306SCy Schubert #include "netinet/ip_lookup.h" 6541edb306SCy Schubert #include "netinet/ip_pool.h" 6641edb306SCy Schubert #include "netinet/ip_scan.h" 6741edb306SCy Schubert #include "netinet/ip_htable.h" 6841edb306SCy Schubert #include "netinet/ip_sync.h" 6941edb306SCy Schubert #include "netinet/ip_dstlist.h" 7041edb306SCy Schubert 7141edb306SCy Schubert #include "opts.h" 7241edb306SCy Schubert 7341edb306SCy Schubert #ifndef __P 7441edb306SCy Schubert # define __P(x) x 7541edb306SCy Schubert #endif 7641edb306SCy Schubert 7741edb306SCy Schubert #ifndef U_32_T 7841edb306SCy Schubert # define U_32_T 1 7941edb306SCy Schubert # if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || \ 8041edb306SCy Schubert defined(__sgi) 8141edb306SCy Schubert typedef u_int32_t u_32_t; 8241edb306SCy Schubert # else 8341edb306SCy Schubert # if defined(__alpha__) || defined(__alpha) || defined(_LP64) 8441edb306SCy Schubert typedef unsigned int u_32_t; 8541edb306SCy Schubert # else 8641edb306SCy Schubert # if SOLARIS2 >= 6 8741edb306SCy Schubert typedef uint32_t u_32_t; 8841edb306SCy Schubert # else 8941edb306SCy Schubert typedef unsigned int u_32_t; 9041edb306SCy Schubert # endif 9141edb306SCy Schubert # endif 9241edb306SCy Schubert # endif /* __NetBSD__ || __OpenBSD__ || __FreeBSD__ || __sgi */ 9341edb306SCy Schubert #endif /* U_32_T */ 9441edb306SCy Schubert 9541edb306SCy Schubert #ifndef MAXHOSTNAMELEN 9641edb306SCy Schubert # define MAXHOSTNAMELEN 256 9741edb306SCy Schubert #endif 9841edb306SCy Schubert 9941edb306SCy Schubert #define MAX_ICMPCODE 16 10041edb306SCy Schubert #define MAX_ICMPTYPE 19 10141edb306SCy Schubert 10241edb306SCy Schubert #define PRINTF (void)printf 10341edb306SCy Schubert #define FPRINTF (void)fprintf 104915395a2SCy Schubert #define FORMAT_IF(_a) (_a != NULL ? _a : "(null)") 10541edb306SCy Schubert 10641edb306SCy Schubert 10741edb306SCy Schubert struct ipopt_names { 10841edb306SCy Schubert int on_value; 10941edb306SCy Schubert int on_bit; 11041edb306SCy Schubert int on_siz; 11141edb306SCy Schubert char *on_name; 11241edb306SCy Schubert }; 11341edb306SCy Schubert 11441edb306SCy Schubert 11541edb306SCy Schubert typedef struct alist_s { 11641edb306SCy Schubert struct alist_s *al_next; 11741edb306SCy Schubert int al_not; 11841edb306SCy Schubert int al_family; 11941edb306SCy Schubert i6addr_t al_i6addr; 12041edb306SCy Schubert i6addr_t al_i6mask; 12141edb306SCy Schubert } alist_t; 12241edb306SCy Schubert 12341edb306SCy Schubert #define al_addr al_i6addr.in4_addr 12441edb306SCy Schubert #define al_mask al_i6mask.in4_addr 12541edb306SCy Schubert #define al_1 al_addr 12641edb306SCy Schubert #define al_2 al_mask 12741edb306SCy Schubert 12841edb306SCy Schubert 12941edb306SCy Schubert typedef struct plist_s { 13041edb306SCy Schubert struct plist_s *pl_next; 13141edb306SCy Schubert int pl_compare; 13241edb306SCy Schubert u_short pl_port1; 13341edb306SCy Schubert u_short pl_port2; 13441edb306SCy Schubert } plist_t; 13541edb306SCy Schubert 13641edb306SCy Schubert 13741edb306SCy Schubert typedef struct { 13841edb306SCy Schubert u_short fb_c; 13941edb306SCy Schubert u_char fb_t; 14041edb306SCy Schubert u_char fb_f; 14141edb306SCy Schubert u_32_t fb_k; 14241edb306SCy Schubert } fakebpf_t; 14341edb306SCy Schubert 14441edb306SCy Schubert 14541edb306SCy Schubert typedef struct { 14641edb306SCy Schubert char *it_name; 14741edb306SCy Schubert int it_v4; 14841edb306SCy Schubert int it_v6; 14941edb306SCy Schubert } icmptype_t; 15041edb306SCy Schubert 15141edb306SCy Schubert 15241edb306SCy Schubert typedef struct wordtab { 15341edb306SCy Schubert char *w_word; 15441edb306SCy Schubert int w_value; 15541edb306SCy Schubert } wordtab_t; 15641edb306SCy Schubert 15741edb306SCy Schubert 15841edb306SCy Schubert typedef struct namelist { 15941edb306SCy Schubert struct namelist *na_next; 16041edb306SCy Schubert char *na_name; 16141edb306SCy Schubert int na_value; 16241edb306SCy Schubert } namelist_t; 16341edb306SCy Schubert 16441edb306SCy Schubert 16541edb306SCy Schubert typedef struct proxyrule { 16641edb306SCy Schubert struct proxyrule *pr_next; 16741edb306SCy Schubert char *pr_proxy; 16841edb306SCy Schubert char *pr_conf; 16941edb306SCy Schubert namelist_t *pr_names; 17041edb306SCy Schubert int pr_proto; 17141edb306SCy Schubert } proxyrule_t; 17241edb306SCy Schubert 17341edb306SCy Schubert 17441edb306SCy Schubert typedef int (* ioctlfunc_t)(int, ioctlcmd_t, ...); 17541edb306SCy Schubert typedef int (* addfunc_t)(int, ioctlfunc_t, void *); 17641edb306SCy Schubert typedef int (* copyfunc_t)(void *, void *, size_t); 17741edb306SCy Schubert 17841edb306SCy Schubert 17941edb306SCy Schubert extern char thishost[MAXHOSTNAMELEN]; 18041edb306SCy Schubert extern char flagset[]; 181*347dd053SRichard Scheffenegger extern uint16_t flags[]; 18241edb306SCy Schubert extern struct ipopt_names ionames[]; 18341edb306SCy Schubert extern struct ipopt_names secclass[]; 18441edb306SCy Schubert extern char *icmpcodes[MAX_ICMPCODE + 1]; 18541edb306SCy Schubert extern char *icmptypes[MAX_ICMPTYPE + 1]; 18641edb306SCy Schubert extern int use_inet6; 18741edb306SCy Schubert extern int lineNum; 18841edb306SCy Schubert extern int debuglevel; 18941edb306SCy Schubert extern struct ipopt_names v6ionames[]; 19041edb306SCy Schubert extern icmptype_t icmptypelist[]; 19141edb306SCy Schubert extern wordtab_t statefields[]; 19241edb306SCy Schubert extern wordtab_t natfields[]; 19341edb306SCy Schubert extern wordtab_t poolfields[]; 19441edb306SCy Schubert 19541edb306SCy Schubert 19641edb306SCy Schubert extern int addicmp(char ***, struct frentry *, int); 19741edb306SCy Schubert extern int addipopt(char *, struct ipopt_names *, int, char *); 19841edb306SCy Schubert extern int addkeep(char ***, struct frentry *, int); 19941edb306SCy Schubert extern alist_t *alist_new(int, char *); 20041edb306SCy Schubert extern void alist_free(alist_t *); 20141edb306SCy Schubert extern void assigndefined(char *); 20241edb306SCy Schubert extern void binprint(void *, size_t); 20341edb306SCy Schubert extern u_32_t buildopts(char *, char *, int); 20441edb306SCy Schubert extern int checkrev(char *); 20541edb306SCy Schubert extern int connecttcp(char *, int); 20641edb306SCy Schubert extern int count6bits(u_32_t *); 20741edb306SCy Schubert extern int count4bits(u_32_t); 20841edb306SCy Schubert extern char *fac_toname(int); 20941edb306SCy Schubert extern int fac_findname(char *); 21041edb306SCy Schubert extern const char *familyname(const int); 21141edb306SCy Schubert extern void fill6bits(int, u_int *); 21241edb306SCy Schubert extern wordtab_t *findword(wordtab_t *, char *); 21341edb306SCy Schubert extern int ftov(int); 21441edb306SCy Schubert extern char *ipf_geterror(int, ioctlfunc_t *); 21541edb306SCy Schubert extern int genmask(int, char *, i6addr_t *); 21641edb306SCy Schubert extern int gethost(int, char *, i6addr_t *); 21741edb306SCy Schubert extern int geticmptype(int, char *); 21841edb306SCy Schubert extern int getport(struct frentry *, char *, u_short *, char *); 21941edb306SCy Schubert extern int getportproto(char *, int); 22041edb306SCy Schubert extern int getproto(char *); 22141edb306SCy Schubert extern char *getnattype(struct nat *); 22241edb306SCy Schubert extern char *getsumd(u_32_t); 22341edb306SCy Schubert extern u_32_t getoptbyname(char *); 22441edb306SCy Schubert extern u_32_t getoptbyvalue(int); 22541edb306SCy Schubert extern u_32_t getv6optbyname(char *); 22641edb306SCy Schubert extern u_32_t getv6optbyvalue(int); 22741edb306SCy Schubert extern char *icmptypename(int, int); 22841edb306SCy Schubert extern void initparse(void); 22941edb306SCy Schubert extern void ipf_dotuning(int, char *, ioctlfunc_t); 23041edb306SCy Schubert extern int ipf_addrule(int, ioctlfunc_t, void *); 23141edb306SCy Schubert extern void ipf_mutex_clean(void); 23241edb306SCy Schubert extern int ipf_parsefile(int, addfunc_t, ioctlfunc_t *, char *); 23341edb306SCy Schubert extern int ipf_parsesome(int, addfunc_t, ioctlfunc_t *, FILE *); 23441edb306SCy Schubert extern void ipf_perror(int, char *); 23541edb306SCy Schubert extern int ipf_perror_fd( int, ioctlfunc_t, char *); 23641edb306SCy Schubert extern void ipf_rwlock_clean(void); 23741edb306SCy Schubert extern char *ipf_strerror(int); 23841edb306SCy Schubert extern void ipferror(int, char *); 23941edb306SCy Schubert extern int ipmon_parsefile(char *); 24041edb306SCy Schubert extern int ipmon_parsesome(FILE *); 24141edb306SCy Schubert extern int ipnat_addrule(int, ioctlfunc_t, void *); 24241edb306SCy Schubert extern int ipnat_parsefile(int, addfunc_t, ioctlfunc_t, char *); 24341edb306SCy Schubert extern int ipnat_parsesome(int, addfunc_t, ioctlfunc_t, FILE *); 24441edb306SCy Schubert extern int ippool_parsefile(int, char *, ioctlfunc_t); 24541edb306SCy Schubert extern int ippool_parsesome(int, FILE *, ioctlfunc_t); 24641edb306SCy Schubert extern int kmemcpywrap(void *, void *, size_t); 24741edb306SCy Schubert extern char *kvatoname(ipfunc_t, ioctlfunc_t); 24841edb306SCy Schubert extern int load_dstlist(struct ippool_dst *, ioctlfunc_t, 24941edb306SCy Schubert ipf_dstnode_t *); 25041edb306SCy Schubert extern int load_dstlistnode(int, char *, struct ipf_dstnode *, 25141edb306SCy Schubert ioctlfunc_t); 25241edb306SCy Schubert extern alist_t *load_file(char *); 25341edb306SCy Schubert extern int load_hash(struct iphtable_s *, struct iphtent_s *, 25441edb306SCy Schubert ioctlfunc_t); 25541edb306SCy Schubert extern int load_hashnode(int, char *, struct iphtent_s *, int, 25641edb306SCy Schubert ioctlfunc_t); 25741edb306SCy Schubert extern alist_t *load_http(char *); 25841edb306SCy Schubert extern int load_pool(struct ip_pool_s *list, ioctlfunc_t); 25941edb306SCy Schubert extern int load_poolnode(int, char *, ip_pool_node_t *, int, ioctlfunc_t); 26041edb306SCy Schubert extern alist_t *load_url(char *); 26141edb306SCy Schubert extern alist_t *make_range(int, struct in_addr, struct in_addr); 26241edb306SCy Schubert extern void mb_hexdump(mb_t *, FILE *); 26341edb306SCy Schubert extern ipfunc_t nametokva(char *, ioctlfunc_t); 26441edb306SCy Schubert extern void nat_setgroupmap(struct ipnat *); 26541edb306SCy Schubert extern int ntomask(int, int, u_32_t *); 26641edb306SCy Schubert extern u_32_t optname(char ***, u_short *, int); 26741edb306SCy Schubert extern wordtab_t *parsefields(wordtab_t *, char *); 26841edb306SCy Schubert extern int *parseipfexpr(char *, char **); 26941edb306SCy Schubert extern int parsewhoisline(char *, addrfamily_t *, addrfamily_t *); 27041edb306SCy Schubert extern void pool_close(void); 27141edb306SCy Schubert extern int pool_fd(void); 27241edb306SCy Schubert extern int pool_ioctl(ioctlfunc_t, ioctlcmd_t, void *); 27341edb306SCy Schubert extern int pool_open(void); 27441edb306SCy Schubert extern char *portname(int, int); 27541edb306SCy Schubert extern int pri_findname(char *); 27641edb306SCy Schubert extern char *pri_toname(int); 27741edb306SCy Schubert extern void print_toif(int, char *, char *, struct frdest *); 27841edb306SCy Schubert extern void printaps(ap_session_t *, int, int); 27941edb306SCy Schubert extern void printaddr(int, int, char *, int, u_32_t *, u_32_t *); 28041edb306SCy Schubert extern void printbuf(char *, int, int); 28141edb306SCy Schubert extern void printfieldhdr(wordtab_t *, wordtab_t *); 28241edb306SCy Schubert extern void printfr(struct frentry *, ioctlfunc_t); 28341edb306SCy Schubert extern struct iphtable_s *printhash(struct iphtable_s *, copyfunc_t, 28441edb306SCy Schubert char *, int, wordtab_t *); 28541edb306SCy Schubert extern struct iphtable_s *printhash_live(iphtable_t *, int, char *, 28641edb306SCy Schubert int, wordtab_t *); 28741edb306SCy Schubert extern ippool_dst_t *printdstl_live(ippool_dst_t *, int, char *, 28841edb306SCy Schubert int, wordtab_t *); 28941edb306SCy Schubert extern void printhashdata(iphtable_t *, int); 29041edb306SCy Schubert extern struct iphtent_s *printhashnode(struct iphtable_s *, 29141edb306SCy Schubert struct iphtent_s *, 29241edb306SCy Schubert copyfunc_t, int, wordtab_t *); 29341edb306SCy Schubert extern void printhost(int, u_32_t *); 29441edb306SCy Schubert extern void printhostmask(int, u_32_t *, u_32_t *); 29541edb306SCy Schubert extern void printip(int, u_32_t *); 29641edb306SCy Schubert extern void printlog(struct frentry *); 29741edb306SCy Schubert extern void printlookup(char *, i6addr_t *addr, i6addr_t *mask); 29841edb306SCy Schubert extern void printmask(int, u_32_t *); 29941edb306SCy Schubert extern void printnataddr(int, char *, nat_addr_t *, int); 30041edb306SCy Schubert extern void printnatfield(nat_t *, int); 30141edb306SCy Schubert extern void printnatside(char *, nat_stat_side_t *); 30241edb306SCy Schubert extern void printpacket(int, mb_t *); 30341edb306SCy Schubert extern void printpacket6(int, mb_t *); 30441edb306SCy Schubert extern struct ippool_dst *printdstlist(struct ippool_dst *, copyfunc_t, 30541edb306SCy Schubert char *, int, ipf_dstnode_t *, 30641edb306SCy Schubert wordtab_t *); 30741edb306SCy Schubert extern void printdstlistdata(ippool_dst_t *, int); 30841edb306SCy Schubert extern ipf_dstnode_t *printdstlistnode(ipf_dstnode_t *, copyfunc_t, 30941edb306SCy Schubert int, wordtab_t *); 31041edb306SCy Schubert extern void printdstlistpolicy(ippool_policy_t); 31141edb306SCy Schubert extern struct ip_pool_s *printpool(struct ip_pool_s *, copyfunc_t, 31241edb306SCy Schubert char *, int, wordtab_t *); 31300d8a28fSCy Schubert extern void printpool_live(struct ip_pool_s *, int, 31441edb306SCy Schubert char *, int, wordtab_t *); 31541edb306SCy Schubert extern void printpooldata(ip_pool_t *, int); 31641edb306SCy Schubert extern void printpoolfield(void *, int, int); 31741edb306SCy Schubert extern struct ip_pool_node *printpoolnode(struct ip_pool_node *, 31841edb306SCy Schubert int, wordtab_t *); 31941edb306SCy Schubert extern void printproto(struct protoent *, int, struct ipnat *); 32041edb306SCy Schubert extern void printportcmp(int, struct frpcmp *); 32141edb306SCy Schubert extern void printstatefield(ipstate_t *, int); 32241edb306SCy Schubert extern void printtqtable(ipftq_t *); 32341edb306SCy Schubert extern void printtunable(ipftune_t *); 32441edb306SCy Schubert extern void printunit(int); 32541edb306SCy Schubert extern void optprint(u_short *, u_long, u_long); 32641edb306SCy Schubert #ifdef USE_INET6 32741edb306SCy Schubert extern void optprintv6(u_short *, u_long, u_long); 32841edb306SCy Schubert #endif 32941edb306SCy Schubert extern int remove_hash(struct iphtable_s *, ioctlfunc_t); 33041edb306SCy Schubert extern int remove_hashnode(int, char *, struct iphtent_s *, ioctlfunc_t); 33141edb306SCy Schubert extern int remove_pool(ip_pool_t *, ioctlfunc_t); 33241edb306SCy Schubert extern int remove_poolnode(int, char *, ip_pool_node_t *, ioctlfunc_t); 333*347dd053SRichard Scheffenegger extern uint16_t tcpflags(char *); 33441edb306SCy Schubert extern void printc(struct frentry *); 33541edb306SCy Schubert extern void printC(int); 33641edb306SCy Schubert extern void emit(int, int, void *, struct frentry *); 33741edb306SCy Schubert extern u_char secbit(int); 33841edb306SCy Schubert extern u_char seclevel(char *); 33941edb306SCy Schubert extern void printfraginfo(char *, struct ipfr *); 34041edb306SCy Schubert extern void printifname(char *, char *, void *); 34141edb306SCy Schubert extern char *hostname(int, void *); 34241edb306SCy Schubert extern struct ipstate *printstate(struct ipstate *, int, u_long); 34341edb306SCy Schubert extern void printsbuf(char *); 34441edb306SCy Schubert extern void printnat(struct ipnat *, int); 34541edb306SCy Schubert extern void printactiveaddress(int, char *, i6addr_t *, char *); 34641edb306SCy Schubert extern void printactivenat(struct nat *, int, u_long); 34741edb306SCy Schubert extern void printhostmap(struct hostmap *, u_int); 34841edb306SCy Schubert extern void printtcpflags(u_32_t, u_32_t); 34941edb306SCy Schubert extern void printipfexpr(int *); 35041edb306SCy Schubert extern void printstatefield(ipstate_t *, int); 35141edb306SCy Schubert extern void printstatefieldhdr(int); 35241edb306SCy Schubert extern int sendtrap_v1_0(int, char *, char *, int, time_t); 35341edb306SCy Schubert extern int sendtrap_v2_0(int, char *, char *, int); 35441edb306SCy Schubert extern int vtof(int); 35541edb306SCy Schubert 35641edb306SCy Schubert extern void set_variable(char *, char *); 35741edb306SCy Schubert extern char *get_variable(char *, char **, int); 35841edb306SCy Schubert extern void resetlexer(void); 35941edb306SCy Schubert 36041edb306SCy Schubert extern void debug(int, char *, ...); 36141edb306SCy Schubert extern void verbose(int, char *, ...); 36241edb306SCy Schubert extern void ipfkdebug(char *, ...); 36341edb306SCy Schubert extern void ipfkverbose(char *, ...); 36441edb306SCy Schubert 36541edb306SCy Schubert #if SOLARIS 36641edb306SCy Schubert extern int gethostname(char *, int ); 36741edb306SCy Schubert extern void sync(void); 36841edb306SCy Schubert #endif 36941edb306SCy Schubert 37041edb306SCy Schubert #endif /* __IPF_H__ */ 371