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