xref: /freebsd/sbin/ipf/common/ipf.h (revision be181ee2)
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 <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 #if defined(__NetBSD__) || defined(__FreeBSD__) || \
177 	SOLARIS
178 # include <stdarg.h>
179 typedef	int	(* ioctlfunc_t)(int, ioctlcmd_t, ...);
180 #else
181 typedef	int	(* ioctlfunc_t)(dev_t, ioctlcmd_t, void *);
182 #endif
183 typedef	int	(* addfunc_t)(int, ioctlfunc_t, void *);
184 typedef	int	(* copyfunc_t)(void *, void *, size_t);
185 
186 
187 extern	char	thishost[MAXHOSTNAMELEN];
188 extern	char	flagset[];
189 extern	u_char	flags[];
190 extern	struct ipopt_names ionames[];
191 extern	struct ipopt_names secclass[];
192 extern	char	*icmpcodes[MAX_ICMPCODE + 1];
193 extern	char	*icmptypes[MAX_ICMPTYPE + 1];
194 extern	int	use_inet6;
195 extern	int	lineNum;
196 extern	int	debuglevel;
197 extern	struct ipopt_names v6ionames[];
198 extern	icmptype_t icmptypelist[];
199 extern	wordtab_t statefields[];
200 extern	wordtab_t natfields[];
201 extern	wordtab_t poolfields[];
202 
203 
204 extern int addicmp(char ***, struct frentry *, int);
205 extern int addipopt(char *, struct ipopt_names *, int, char *);
206 extern int addkeep(char ***, struct frentry *, int);
207 extern alist_t *alist_new(int, char *);
208 extern void alist_free(alist_t *);
209 extern void assigndefined(char *);
210 extern void binprint(void *, size_t);
211 extern u_32_t buildopts(char *, char *, int);
212 extern int checkrev(char *);
213 extern int connecttcp(char *, int);
214 extern int count6bits(u_32_t *);
215 extern int count4bits(u_32_t);
216 extern char *fac_toname(int);
217 extern int fac_findname(char *);
218 extern const char *familyname(const int);
219 extern void fill6bits(int, u_int *);
220 extern wordtab_t *findword(wordtab_t *, char *);
221 extern int ftov(int);
222 extern char *ipf_geterror(int, ioctlfunc_t *);
223 extern int genmask(int, char *, i6addr_t *);
224 extern int gethost(int, char *, i6addr_t *);
225 extern int geticmptype(int, char *);
226 extern int getport(struct frentry *, char *, u_short *, char *);
227 extern int getportproto(char *, int);
228 extern int getproto(char *);
229 extern char *getnattype(struct nat *);
230 extern char *getsumd(u_32_t);
231 extern u_32_t getoptbyname(char *);
232 extern u_32_t getoptbyvalue(int);
233 extern u_32_t getv6optbyname(char *);
234 extern u_32_t getv6optbyvalue(int);
235 extern char *icmptypename(int, int);
236 extern void initparse(void);
237 extern void ipf_dotuning(int, char *, ioctlfunc_t);
238 extern int ipf_addrule(int, ioctlfunc_t, void *);
239 extern void ipf_mutex_clean(void);
240 extern int ipf_parsefile(int, addfunc_t, ioctlfunc_t *, char *);
241 extern int ipf_parsesome(int, addfunc_t, ioctlfunc_t *, FILE *);
242 extern void ipf_perror(int, char *);
243 extern int ipf_perror_fd( int, ioctlfunc_t, char *);
244 extern void ipf_rwlock_clean(void);
245 extern char *ipf_strerror(int);
246 extern void ipferror(int, char *);
247 extern int ipmon_parsefile(char *);
248 extern int ipmon_parsesome(FILE *);
249 extern int ipnat_addrule(int, ioctlfunc_t, void *);
250 extern int ipnat_parsefile(int, addfunc_t, ioctlfunc_t, char *);
251 extern int ipnat_parsesome(int, addfunc_t, ioctlfunc_t, FILE *);
252 extern int ippool_parsefile(int, char *, ioctlfunc_t);
253 extern int ippool_parsesome(int, FILE *, ioctlfunc_t);
254 extern int kmemcpywrap(void *, void *, size_t);
255 extern char *kvatoname(ipfunc_t, ioctlfunc_t);
256 extern int load_dstlist(struct ippool_dst *, ioctlfunc_t,
257 			     ipf_dstnode_t *);
258 extern int load_dstlistnode(int, char *, struct ipf_dstnode *,
259 				 ioctlfunc_t);
260 extern alist_t *load_file(char *);
261 extern int load_hash(struct iphtable_s *, struct iphtent_s *,
262 			  ioctlfunc_t);
263 extern int load_hashnode(int, char *, struct iphtent_s *, int,
264 			      ioctlfunc_t);
265 extern alist_t *load_http(char *);
266 extern int load_pool(struct ip_pool_s *list, ioctlfunc_t);
267 extern int load_poolnode(int, char *, ip_pool_node_t *, int, ioctlfunc_t);
268 extern alist_t *load_url(char *);
269 extern alist_t *make_range(int, struct in_addr, struct in_addr);
270 extern void mb_hexdump(mb_t *, FILE *);
271 extern ipfunc_t nametokva(char *, ioctlfunc_t);
272 extern void nat_setgroupmap(struct ipnat *);
273 extern int ntomask(int, int, u_32_t *);
274 extern u_32_t optname(char ***, u_short *, int);
275 extern wordtab_t *parsefields(wordtab_t *, char *);
276 extern int *parseipfexpr(char *, char **);
277 extern int parsewhoisline(char *, addrfamily_t *, addrfamily_t *);
278 extern void pool_close(void);
279 extern int pool_fd(void);
280 extern int pool_ioctl(ioctlfunc_t, ioctlcmd_t, void *);
281 extern int pool_open(void);
282 extern char *portname(int, int);
283 extern int pri_findname(char *);
284 extern char *pri_toname(int);
285 extern void print_toif(int, char *, char *, struct frdest *);
286 extern void printaps(ap_session_t *, int, int);
287 extern void printaddr(int, int, char *, int, u_32_t *, u_32_t *);
288 extern void printbuf(char *, int, int);
289 extern void printfieldhdr(wordtab_t *, wordtab_t *);
290 extern void printfr(struct frentry *, ioctlfunc_t);
291 extern struct iphtable_s *printhash(struct iphtable_s *, copyfunc_t,
292 					 char *, int, wordtab_t *);
293 extern struct iphtable_s *printhash_live(iphtable_t *, int, char *,
294 					      int, wordtab_t *);
295 extern ippool_dst_t *printdstl_live(ippool_dst_t *, int, char *,
296 					 int, wordtab_t *);
297 extern void printhashdata(iphtable_t *, int);
298 extern struct iphtent_s *printhashnode(struct iphtable_s *,
299 					    struct iphtent_s *,
300 					    copyfunc_t, int, wordtab_t *);
301 extern void printhost(int, u_32_t *);
302 extern void printhostmask(int, u_32_t *, u_32_t *);
303 extern void printip(int, u_32_t *);
304 extern void printlog(struct frentry *);
305 extern void printlookup(char *, i6addr_t *addr, i6addr_t *mask);
306 extern void printmask(int, u_32_t *);
307 extern void printnataddr(int, char *, nat_addr_t *, int);
308 extern void printnatfield(nat_t *, int);
309 extern void printnatside(char *, nat_stat_side_t *);
310 extern void printpacket(int, mb_t *);
311 extern void printpacket6(int, mb_t *);
312 extern struct ippool_dst *printdstlist(struct ippool_dst *, copyfunc_t,
313 					    char *, int, ipf_dstnode_t *,
314 					    wordtab_t *);
315 extern void printdstlistdata(ippool_dst_t *, int);
316 extern ipf_dstnode_t *printdstlistnode(ipf_dstnode_t *, copyfunc_t,
317 					    int, wordtab_t *);
318 extern void printdstlistpolicy(ippool_policy_t);
319 extern struct ip_pool_s *printpool(struct ip_pool_s *, copyfunc_t,
320 					char *, int, wordtab_t *);
321 extern void printpool_live(struct ip_pool_s *, int,
322 					     char *, int, wordtab_t *);
323 extern void printpooldata(ip_pool_t *, int);
324 extern void printpoolfield(void *, int, int);
325 extern struct ip_pool_node *printpoolnode(struct ip_pool_node *,
326 					       int, wordtab_t *);
327 extern void printproto(struct protoent *, int, struct ipnat *);
328 extern void printportcmp(int, struct frpcmp *);
329 extern void printstatefield(ipstate_t *, int);
330 extern void printtqtable(ipftq_t *);
331 extern void printtunable(ipftune_t *);
332 extern void printunit(int);
333 extern void optprint(u_short *, u_long, u_long);
334 #ifdef	USE_INET6
335 extern void optprintv6(u_short *, u_long, u_long);
336 #endif
337 extern int remove_hash(struct iphtable_s *, ioctlfunc_t);
338 extern int remove_hashnode(int, char *, struct iphtent_s *, ioctlfunc_t);
339 extern int remove_pool(ip_pool_t *, ioctlfunc_t);
340 extern int remove_poolnode(int, char *, ip_pool_node_t *, ioctlfunc_t);
341 extern u_char tcpflags(char *);
342 extern void printc(struct frentry *);
343 extern void printC(int);
344 extern void emit(int, int, void *, struct frentry *);
345 extern u_char secbit(int);
346 extern u_char seclevel(char *);
347 extern void printfraginfo(char *, struct ipfr *);
348 extern void printifname(char *, char *, void *);
349 extern char *hostname(int, void *);
350 extern struct ipstate *printstate(struct ipstate *, int, u_long);
351 extern void printsbuf(char *);
352 extern void printnat(struct ipnat *, int);
353 extern void printactiveaddress(int, char *, i6addr_t *, char *);
354 extern void printactivenat(struct nat *, int, u_long);
355 extern void printhostmap(struct hostmap *, u_int);
356 extern void printtcpflags(u_32_t, u_32_t);
357 extern void printipfexpr(int *);
358 extern void printstatefield(ipstate_t *, int);
359 extern void printstatefieldhdr(int);
360 extern int sendtrap_v1_0(int, char *, char *, int, time_t);
361 extern int sendtrap_v2_0(int, char *, char *, int);
362 extern int vtof(int);
363 
364 extern void set_variable(char *, char *);
365 extern char *get_variable(char *, char **, int);
366 extern void resetlexer(void);
367 
368 extern void debug(int, char *, ...);
369 extern void verbose(int, char *, ...);
370 extern void ipfkdebug(char *, ...);
371 extern void ipfkverbose(char *, ...);
372 
373 #if SOLARIS
374 extern int gethostname(char *, int );
375 extern void sync(void);
376 #endif
377 
378 #endif /* __IPF_H__ */
379