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