xref: /freebsd/sbin/ipf/common/ipf.h (revision 347dd0539f3a75fdf2128dd4620ca99e96f311e9)
1 
2 /*
3  * Copyright (C) 2012 by Darren Reed.
4  *
5  * See the IPFILTER.LICENCE file for details on licencing.
6  * $Id$
7  */
8 
9 #ifndef	__IPF_H__
10 #define	__IPF_H__
11 
12 
13 #include <sys/param.h>
14 #include <sys/types.h>
15 #include <sys/file.h>
16 /*
17  * This is a workaround for <sys/uio.h> troubles on FreeBSD, HPUX, OpenBSD.
18  * Needed here because on some systems <sys/uio.h> gets included by things
19  * like <sys/socket.h>
20  */
21 #ifndef _KERNEL
22 # define ADD_KERNEL
23 # define _KERNEL
24 # define KERNEL
25 #endif
26 #include <sys/uio.h>
27 #ifdef ADD_KERNEL
28 # undef _KERNEL
29 # undef KERNEL
30 #endif
31 #include <sys/time.h>
32 #include <sys/socket.h>
33 #include <net/if.h>
34 
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <netinet/ip_icmp.h>
39 # include <netinet/tcp.h>
40 #include <netinet/udp.h>
41 
42 #include <arpa/inet.h>
43 
44 #include <errno.h>
45 #include <limits.h>
46 #include <netdb.h>
47 #include <stdarg.h>
48 #include <stdlib.h>
49 #include <stddef.h>
50 #include <stdio.h>
51 #if !defined(__SVR4) && !defined(__svr4__) && defined(sun)
52 # include <strings.h>
53 #endif
54 #include <string.h>
55 #include <unistd.h>
56 
57 #include "netinet/ip_compat.h"
58 #include "netinet/ip_fil.h"
59 #include "netinet/ip_nat.h"
60 #include "netinet/ip_frag.h"
61 #include "netinet/ip_state.h"
62 #include "netinet/ip_proxy.h"
63 #include "netinet/ip_auth.h"
64 #include "netinet/ip_lookup.h"
65 #include "netinet/ip_pool.h"
66 #include "netinet/ip_scan.h"
67 #include "netinet/ip_htable.h"
68 #include "netinet/ip_sync.h"
69 #include "netinet/ip_dstlist.h"
70 
71 #include "opts.h"
72 
73 #ifndef __P
74 #  define	__P(x)	x
75 #endif
76 
77 #ifndef	U_32_T
78 # define	U_32_T	1
79 # if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || \
80     defined(__sgi)
81 typedef	u_int32_t	u_32_t;
82 # else
83 #  if defined(__alpha__) || defined(__alpha) || defined(_LP64)
84 typedef unsigned int	u_32_t;
85 #  else
86 #   if SOLARIS2 >= 6
87 typedef uint32_t	u_32_t;
88 #   else
89 typedef unsigned int	u_32_t;
90 #   endif
91 #  endif
92 # endif /* __NetBSD__ || __OpenBSD__ || __FreeBSD__ || __sgi */
93 #endif /* U_32_T */
94 
95 #ifndef	MAXHOSTNAMELEN
96 # define	MAXHOSTNAMELEN	256
97 #endif
98 
99 #define	MAX_ICMPCODE	16
100 #define	MAX_ICMPTYPE	19
101 
102 #define	PRINTF	(void)printf
103 #define	FPRINTF	(void)fprintf
104 #define FORMAT_IF(_a)	(_a != NULL ? _a : "(null)")
105 
106 
107 struct	ipopt_names	{
108 	int	on_value;
109 	int	on_bit;
110 	int	on_siz;
111 	char	*on_name;
112 };
113 
114 
115 typedef struct  alist_s {
116 	struct	alist_s	*al_next;
117 	int		al_not;
118 	int		al_family;
119 	i6addr_t	al_i6addr;
120 	i6addr_t	al_i6mask;
121 } alist_t;
122 
123 #define	al_addr	al_i6addr.in4_addr
124 #define	al_mask	al_i6mask.in4_addr
125 #define	al_1	al_addr
126 #define	al_2	al_mask
127 
128 
129 typedef struct  plist_s {
130 	struct	plist_s	*pl_next;
131 	int		pl_compare;
132 	u_short		pl_port1;
133 	u_short		pl_port2;
134 } plist_t;
135 
136 
137 typedef	struct	{
138 	u_short	fb_c;
139 	u_char	fb_t;
140 	u_char	fb_f;
141 	u_32_t	fb_k;
142 } fakebpf_t;
143 
144 
145 typedef struct  {
146 	char	*it_name;
147 	int	it_v4;
148 	int	it_v6;
149 } icmptype_t;
150 
151 
152 typedef	struct	wordtab {
153 	char	*w_word;
154 	int	w_value;
155 } wordtab_t;
156 
157 
158 typedef	struct	namelist {
159 	struct namelist	*na_next;
160 	char		*na_name;
161 	int		na_value;
162 } namelist_t;
163 
164 
165 typedef	struct	proxyrule {
166 	struct	proxyrule	*pr_next;
167 	char			*pr_proxy;
168 	char			*pr_conf;
169 	namelist_t		*pr_names;
170 	int			pr_proto;
171 } proxyrule_t;
172 
173 
174 typedef	int	(* ioctlfunc_t)(int, ioctlcmd_t, ...);
175 typedef	int	(* addfunc_t)(int, ioctlfunc_t, void *);
176 typedef	int	(* copyfunc_t)(void *, void *, size_t);
177 
178 
179 extern	char	thishost[MAXHOSTNAMELEN];
180 extern	char	flagset[];
181 extern	uint16_t flags[];
182 extern	struct ipopt_names ionames[];
183 extern	struct ipopt_names secclass[];
184 extern	char	*icmpcodes[MAX_ICMPCODE + 1];
185 extern	char	*icmptypes[MAX_ICMPTYPE + 1];
186 extern	int	use_inet6;
187 extern	int	lineNum;
188 extern	int	debuglevel;
189 extern	struct ipopt_names v6ionames[];
190 extern	icmptype_t icmptypelist[];
191 extern	wordtab_t statefields[];
192 extern	wordtab_t natfields[];
193 extern	wordtab_t poolfields[];
194 
195 
196 extern int addicmp(char ***, struct frentry *, int);
197 extern int addipopt(char *, struct ipopt_names *, int, char *);
198 extern int addkeep(char ***, struct frentry *, int);
199 extern alist_t *alist_new(int, char *);
200 extern void alist_free(alist_t *);
201 extern void assigndefined(char *);
202 extern void binprint(void *, size_t);
203 extern u_32_t buildopts(char *, char *, int);
204 extern int checkrev(char *);
205 extern int connecttcp(char *, int);
206 extern int count6bits(u_32_t *);
207 extern int count4bits(u_32_t);
208 extern char *fac_toname(int);
209 extern int fac_findname(char *);
210 extern const char *familyname(const int);
211 extern void fill6bits(int, u_int *);
212 extern wordtab_t *findword(wordtab_t *, char *);
213 extern int ftov(int);
214 extern char *ipf_geterror(int, ioctlfunc_t *);
215 extern int genmask(int, char *, i6addr_t *);
216 extern int gethost(int, char *, i6addr_t *);
217 extern int geticmptype(int, char *);
218 extern int getport(struct frentry *, char *, u_short *, char *);
219 extern int getportproto(char *, int);
220 extern int getproto(char *);
221 extern char *getnattype(struct nat *);
222 extern char *getsumd(u_32_t);
223 extern u_32_t getoptbyname(char *);
224 extern u_32_t getoptbyvalue(int);
225 extern u_32_t getv6optbyname(char *);
226 extern u_32_t getv6optbyvalue(int);
227 extern char *icmptypename(int, int);
228 extern void initparse(void);
229 extern void ipf_dotuning(int, char *, ioctlfunc_t);
230 extern int ipf_addrule(int, ioctlfunc_t, void *);
231 extern void ipf_mutex_clean(void);
232 extern int ipf_parsefile(int, addfunc_t, ioctlfunc_t *, char *);
233 extern int ipf_parsesome(int, addfunc_t, ioctlfunc_t *, FILE *);
234 extern void ipf_perror(int, char *);
235 extern int ipf_perror_fd( int, ioctlfunc_t, char *);
236 extern void ipf_rwlock_clean(void);
237 extern char *ipf_strerror(int);
238 extern void ipferror(int, char *);
239 extern int ipmon_parsefile(char *);
240 extern int ipmon_parsesome(FILE *);
241 extern int ipnat_addrule(int, ioctlfunc_t, void *);
242 extern int ipnat_parsefile(int, addfunc_t, ioctlfunc_t, char *);
243 extern int ipnat_parsesome(int, addfunc_t, ioctlfunc_t, FILE *);
244 extern int ippool_parsefile(int, char *, ioctlfunc_t);
245 extern int ippool_parsesome(int, FILE *, ioctlfunc_t);
246 extern int kmemcpywrap(void *, void *, size_t);
247 extern char *kvatoname(ipfunc_t, ioctlfunc_t);
248 extern int load_dstlist(struct ippool_dst *, ioctlfunc_t,
249 			     ipf_dstnode_t *);
250 extern int load_dstlistnode(int, char *, struct ipf_dstnode *,
251 				 ioctlfunc_t);
252 extern alist_t *load_file(char *);
253 extern int load_hash(struct iphtable_s *, struct iphtent_s *,
254 			  ioctlfunc_t);
255 extern int load_hashnode(int, char *, struct iphtent_s *, int,
256 			      ioctlfunc_t);
257 extern alist_t *load_http(char *);
258 extern int load_pool(struct ip_pool_s *list, ioctlfunc_t);
259 extern int load_poolnode(int, char *, ip_pool_node_t *, int, ioctlfunc_t);
260 extern alist_t *load_url(char *);
261 extern alist_t *make_range(int, struct in_addr, struct in_addr);
262 extern void mb_hexdump(mb_t *, FILE *);
263 extern ipfunc_t nametokva(char *, ioctlfunc_t);
264 extern void nat_setgroupmap(struct ipnat *);
265 extern int ntomask(int, int, u_32_t *);
266 extern u_32_t optname(char ***, u_short *, int);
267 extern wordtab_t *parsefields(wordtab_t *, char *);
268 extern int *parseipfexpr(char *, char **);
269 extern int parsewhoisline(char *, addrfamily_t *, addrfamily_t *);
270 extern void pool_close(void);
271 extern int pool_fd(void);
272 extern int pool_ioctl(ioctlfunc_t, ioctlcmd_t, void *);
273 extern int pool_open(void);
274 extern char *portname(int, int);
275 extern int pri_findname(char *);
276 extern char *pri_toname(int);
277 extern void print_toif(int, char *, char *, struct frdest *);
278 extern void printaps(ap_session_t *, int, int);
279 extern void printaddr(int, int, char *, int, u_32_t *, u_32_t *);
280 extern void printbuf(char *, int, int);
281 extern void printfieldhdr(wordtab_t *, wordtab_t *);
282 extern void printfr(struct frentry *, ioctlfunc_t);
283 extern struct iphtable_s *printhash(struct iphtable_s *, copyfunc_t,
284 					 char *, int, wordtab_t *);
285 extern struct iphtable_s *printhash_live(iphtable_t *, int, char *,
286 					      int, wordtab_t *);
287 extern ippool_dst_t *printdstl_live(ippool_dst_t *, int, char *,
288 					 int, wordtab_t *);
289 extern void printhashdata(iphtable_t *, int);
290 extern struct iphtent_s *printhashnode(struct iphtable_s *,
291 					    struct iphtent_s *,
292 					    copyfunc_t, int, wordtab_t *);
293 extern void printhost(int, u_32_t *);
294 extern void printhostmask(int, u_32_t *, u_32_t *);
295 extern void printip(int, u_32_t *);
296 extern void printlog(struct frentry *);
297 extern void printlookup(char *, i6addr_t *addr, i6addr_t *mask);
298 extern void printmask(int, u_32_t *);
299 extern void printnataddr(int, char *, nat_addr_t *, int);
300 extern void printnatfield(nat_t *, int);
301 extern void printnatside(char *, nat_stat_side_t *);
302 extern void printpacket(int, mb_t *);
303 extern void printpacket6(int, mb_t *);
304 extern struct ippool_dst *printdstlist(struct ippool_dst *, copyfunc_t,
305 					    char *, int, ipf_dstnode_t *,
306 					    wordtab_t *);
307 extern void printdstlistdata(ippool_dst_t *, int);
308 extern ipf_dstnode_t *printdstlistnode(ipf_dstnode_t *, copyfunc_t,
309 					    int, wordtab_t *);
310 extern void printdstlistpolicy(ippool_policy_t);
311 extern struct ip_pool_s *printpool(struct ip_pool_s *, copyfunc_t,
312 					char *, int, wordtab_t *);
313 extern void printpool_live(struct ip_pool_s *, int,
314 					     char *, int, wordtab_t *);
315 extern void printpooldata(ip_pool_t *, int);
316 extern void printpoolfield(void *, int, int);
317 extern struct ip_pool_node *printpoolnode(struct ip_pool_node *,
318 					       int, wordtab_t *);
319 extern void printproto(struct protoent *, int, struct ipnat *);
320 extern void printportcmp(int, struct frpcmp *);
321 extern void printstatefield(ipstate_t *, int);
322 extern void printtqtable(ipftq_t *);
323 extern void printtunable(ipftune_t *);
324 extern void printunit(int);
325 extern void optprint(u_short *, u_long, u_long);
326 #ifdef	USE_INET6
327 extern void optprintv6(u_short *, u_long, u_long);
328 #endif
329 extern int remove_hash(struct iphtable_s *, ioctlfunc_t);
330 extern int remove_hashnode(int, char *, struct iphtent_s *, ioctlfunc_t);
331 extern int remove_pool(ip_pool_t *, ioctlfunc_t);
332 extern int remove_poolnode(int, char *, ip_pool_node_t *, ioctlfunc_t);
333 extern uint16_t tcpflags(char *);
334 extern void printc(struct frentry *);
335 extern void printC(int);
336 extern void emit(int, int, void *, struct frentry *);
337 extern u_char secbit(int);
338 extern u_char seclevel(char *);
339 extern void printfraginfo(char *, struct ipfr *);
340 extern void printifname(char *, char *, void *);
341 extern char *hostname(int, void *);
342 extern struct ipstate *printstate(struct ipstate *, int, u_long);
343 extern void printsbuf(char *);
344 extern void printnat(struct ipnat *, int);
345 extern void printactiveaddress(int, char *, i6addr_t *, char *);
346 extern void printactivenat(struct nat *, int, u_long);
347 extern void printhostmap(struct hostmap *, u_int);
348 extern void printtcpflags(u_32_t, u_32_t);
349 extern void printipfexpr(int *);
350 extern void printstatefield(ipstate_t *, int);
351 extern void printstatefieldhdr(int);
352 extern int sendtrap_v1_0(int, char *, char *, int, time_t);
353 extern int sendtrap_v2_0(int, char *, char *, int);
354 extern int vtof(int);
355 
356 extern void set_variable(char *, char *);
357 extern char *get_variable(char *, char **, int);
358 extern void resetlexer(void);
359 
360 extern void debug(int, char *, ...);
361 extern void verbose(int, char *, ...);
362 extern void ipfkdebug(char *, ...);
363 extern void ipfkverbose(char *, ...);
364 
365 #if SOLARIS
366 extern int gethostname(char *, int );
367 extern void sync(void);
368 #endif
369 
370 #endif /* __IPF_H__ */
371