ip_fil.h revision 349903
1/*
2 * Copyright (C) 2012 by Darren Reed.
3 *
4 * See the IPFILTER.LICENCE file for details on licencing.
5 *
6 * @(#)ip_fil.h	1.35 6/5/96
7 * $FreeBSD: stable/11/sys/contrib/ipfilter/netinet/ip_fil.h 349903 2019-07-11 00:44:09Z cy $
8 * Id: ip_fil.h,v 2.170.2.51 2007/10/10 09:48:03 darrenr Exp $
9 */
10
11#ifndef	__IP_FIL_H__
12#define	__IP_FIL_H__
13
14# include <netinet/in.h>
15
16#include "netinet/ip_compat.h"
17#include "netinet/ipf_rb.h"
18#if NETBSD_GE_REV(104040000)
19# include <sys/callout.h>
20#endif
21#if defined(BSD) && defined(_KERNEL)
22#  include <sys/selinfo.h>
23#endif
24
25#ifndef	SOLARIS
26# if defined(sun) && defined(__SVR4)
27#  define	SOLARIS		1
28# else
29#  define	SOLARIS		0
30# endif
31#endif
32
33#ifndef	__P
34# ifdef	__STDC__
35#  define	__P(x)	x
36# else
37#  define	__P(x)	()
38# endif
39#endif
40
41#if defined(__STDC__) || defined(__GNUC__)
42# define	SIOCADAFR	_IOW('r', 60, struct ipfobj)
43# define	SIOCRMAFR	_IOW('r', 61, struct ipfobj)
44# define	SIOCSETFF	_IOW('r', 62, u_int)
45# define	SIOCGETFF	_IOR('r', 63, u_int)
46# define	SIOCGETFS	_IOWR('r', 64, struct ipfobj)
47# define	SIOCIPFFL	_IOWR('r', 65, int)
48# define	SIOCIPFFB	_IOR('r', 66, int)
49# define	SIOCADIFR	_IOW('r', 67, struct ipfobj)
50# define	SIOCRMIFR	_IOW('r', 68, struct ipfobj)
51# define	SIOCSWAPA	_IOR('r', 69, u_int)
52# define	SIOCINAFR	_IOW('r', 70, struct ipfobj)
53# define	SIOCINIFR	_IOW('r', 71, struct ipfobj)
54# define	SIOCFRENB	_IOW('r', 72, u_int)
55# define	SIOCFRSYN	_IOW('r', 73, u_int)
56# define	SIOCFRZST	_IOWR('r', 74, struct ipfobj)
57# define	SIOCZRLST	_IOWR('r', 75, struct ipfobj)
58# define	SIOCAUTHW	_IOWR('r', 76, struct ipfobj)
59# define	SIOCAUTHR	_IOWR('r', 77, struct ipfobj)
60# define	SIOCSTAT1	_IOWR('r', 78, struct ipfobj)
61# define	SIOCSTLCK	_IOWR('r', 79, u_int)
62# define	SIOCSTPUT	_IOWR('r', 80, struct ipfobj)
63# define	SIOCSTGET	_IOWR('r', 81, struct ipfobj)
64# define	SIOCSTGSZ	_IOWR('r', 82, struct ipfobj)
65# define	SIOCSTAT2	_IOWR('r', 83, struct ipfobj)
66# define	SIOCSETLG	_IOWR('r', 84, int)
67# define	SIOCGETLG	_IOWR('r', 85, int)
68# define	SIOCFUNCL	_IOWR('r', 86, struct ipfunc_resolve)
69# define	SIOCIPFGETNEXT	_IOWR('r', 87, struct ipfobj)
70# define	SIOCIPFGET	_IOWR('r', 88, struct ipfobj)
71# define	SIOCIPFSET	_IOWR('r', 89, struct ipfobj)
72# define	SIOCIPFL6	_IOWR('r', 90, int)
73# define	SIOCIPFITER	_IOWR('r', 91, struct ipfobj)
74# define	SIOCGENITER	_IOWR('r', 92, struct ipfobj)
75# define	SIOCGTABL	_IOWR('r', 93, struct ipfobj)
76# define	SIOCIPFDELTOK	_IOWR('r', 94, int)
77# define	SIOCLOOKUPITER	_IOWR('r', 95, struct ipfobj)
78# define	SIOCGTQTAB	_IOWR('r', 96, struct ipfobj)
79# define	SIOCMATCHFLUSH	_IOWR('r', 97, struct ipfobj)
80# define	SIOCIPFINTERROR	_IOR('r', 98, int)
81#else
82# define	SIOCADAFR	_IOW(r, 60, struct ipfobj)
83# define	SIOCRMAFR	_IOW(r, 61, struct ipfobj)
84# define	SIOCSETFF	_IOW(r, 62, u_int)
85# define	SIOCGETFF	_IOR(r, 63, u_int)
86# define	SIOCGETFS	_IOWR(r, 64, struct ipfobj)
87# define	SIOCIPFFL	_IOWR(r, 65, int)
88# define	SIOCIPFFB	_IOR(r, 66, int)
89# define	SIOCADIFR	_IOW(r, 67, struct ipfobj)
90# define	SIOCRMIFR	_IOW(r, 68, struct ipfobj)
91# define	SIOCSWAPA	_IOR(r, 69, u_int)
92# define	SIOCINAFR	_IOW(r, 70, struct ipfobj)
93# define	SIOCINIFR	_IOW(r, 71, struct ipfobj)
94# define	SIOCFRENB	_IOW(r, 72, u_int)
95# define	SIOCFRSYN	_IOW(r, 73, u_int)
96# define	SIOCFRZST	_IOWR(r, 74, struct ipfobj)
97# define	SIOCZRLST	_IOWR(r, 75, struct ipfobj)
98# define	SIOCAUTHW	_IOWR(r, 76, struct ipfobj)
99# define	SIOCAUTHR	_IOWR(r, 77, struct ipfobj)
100# define	SIOCSTAT1	_IOWR(r, 78, struct ipfobj)
101# define	SIOCSTLCK	_IOWR(r, 79, u_int)
102# define	SIOCSTPUT	_IOWR(r, 80, struct ipfobj)
103# define	SIOCSTGET	_IOWR(r, 81, struct ipfobj)
104# define	SIOCSTGSZ	_IOWR(r, 82, struct ipfobj)
105# define	SIOCSTAT2	_IOWR(r, 83, struct ipfobj)
106# define	SIOCSETLG	_IOWR(r, 84, int)
107# define	SIOCGETLG	_IOWR(r, 85, int)
108# define	SIOCFUNCL	_IOWR(r, 86, struct ipfunc_resolve)
109# define	SIOCIPFGETNEXT	_IOWR(r, 87, struct ipfobj)
110# define	SIOCIPFGET	_IOWR(r, 88, struct ipfobj)
111# define	SIOCIPFSET	_IOWR(r, 89, struct ipfobj)
112# define	SIOCIPFL6	_IOWR(r, 90, int)
113# define	SIOCIPFITER	_IOWR(r, 91, struct ipfobj)
114# define	SIOCGENITER	_IOWR(r, 92, struct ipfobj)
115# define	SIOCGTABL	_IOWR(r, 93, struct ipfobj)
116# define	SIOCIPFDELTOK	_IOWR(r, 94, int)
117# define	SIOCLOOKUPITER	_IOWR(r, 95, struct ipfobj)
118# define	SIOCGTQTAB	_IOWR(r, 96, struct ipfobj)
119# define	SIOCMATCHFLUSH	_IOWR(r, 97, struct ipfobj)
120# define	SIOCIPFINTERROR	_IOR(r, 98, int)
121#endif
122#define	SIOCADDFR	SIOCADAFR
123#define	SIOCDELFR	SIOCRMAFR
124#define	SIOCINSFR	SIOCINAFR
125#define	SIOCATHST	SIOCSTAT1
126#define	SIOCGFRST	SIOCSTAT2
127
128
129struct ipscan;
130struct ifnet;
131struct ipf_main_softc_s;
132
133typedef	int	(* lookupfunc_t) __P((struct ipf_main_softc_s *, void *,
134				      int, void *, u_int));
135
136/*
137 * i6addr is used as a container for both IPv4 and IPv6 addresses, as well
138 * as other types of objects, depending on its qualifier.
139 */
140typedef	union	i6addr	{
141	u_32_t	i6[4];
142	struct	in_addr	in4;
143#ifdef	USE_INET6
144	struct	in6_addr in6;
145#endif
146	void	*vptr[2];
147	lookupfunc_t	lptr[2];
148	struct {
149		u_short	type;
150		u_short	subtype;
151		int	name;
152	} i6un;
153} i6addr_t;
154
155#define in4_addr	in4.s_addr
156#define	iplookupnum	i6[1]
157#define	iplookupname	i6un.name
158#define	iplookuptype	i6un.type
159#define	iplookupsubtype	i6un.subtype
160/*
161 * NOTE: These DO overlap the above on 64bit systems and this IS recognised.
162 */
163#define	iplookupptr	vptr[0]
164#define	iplookupfunc	lptr[1]
165
166#define	I60(x)	(((u_32_t *)(x))[0])
167#define	I61(x)	(((u_32_t *)(x))[1])
168#define	I62(x)	(((u_32_t *)(x))[2])
169#define	I63(x)	(((u_32_t *)(x))[3])
170#define	HI60(x)	ntohl(((u_32_t *)(x))[0])
171#define	HI61(x)	ntohl(((u_32_t *)(x))[1])
172#define	HI62(x)	ntohl(((u_32_t *)(x))[2])
173#define	HI63(x)	ntohl(((u_32_t *)(x))[3])
174
175#define	IP6_EQ(a,b)	((I63(a) == I63(b)) && (I62(a) == I62(b)) && \
176			 (I61(a) == I61(b)) && (I60(a) == I60(b)))
177#define	IP6_NEQ(a,b)	((I63(a) != I63(b)) || (I62(a) != I62(b)) || \
178			 (I61(a) != I61(b)) || (I60(a) != I60(b)))
179#define IP6_ISZERO(a)   ((I60(a) | I61(a) | I62(a) | I63(a)) == 0)
180#define IP6_NOTZERO(a)  ((I60(a) | I61(a) | I62(a) | I63(a)) != 0)
181#define	IP6_ISONES(a)	((I63(a) == 0xffffffff) && (I62(a) == 0xffffffff) && \
182			 (I61(a) == 0xffffffff) && (I60(a) == 0xffffffff))
183#define	IP6_GT(a,b)	(ntohl(HI60(a)) > ntohl(HI60(b)) || \
184			 (HI60(a) == HI60(b) && \
185			  (ntohl(HI61(a)) > ntohl(HI61(b)) || \
186			   (HI61(a) == HI61(b) && \
187			    (ntohl(HI62(a)) > ntohl(HI62(b)) || \
188			     (HI62(a) == HI62(b) && \
189			      ntohl(HI63(a)) > ntohl(HI63(b))))))))
190#define	IP6_LT(a,b)	(ntohl(HI60(a)) < ntohl(HI60(b)) || \
191			 (HI60(a) == HI60(b) && \
192			  (ntohl(HI61(a)) < ntohl(HI61(b)) || \
193			   (HI61(a) == HI61(b) && \
194			    (ntohl(HI62(a)) < ntohl(HI62(b)) || \
195			     (HI62(a) == HI62(b) && \
196			      ntohl(HI63(a)) < ntohl(HI63(b))))))))
197#define	NLADD(n,x)	htonl(ntohl(n) + (x))
198#define	IP6_INC(a)	\
199		do { u_32_t *_i6 = (u_32_t *)(a); \
200		  _i6[3] = NLADD(_i6[3], 1); \
201		  if (_i6[3] == 0) { \
202			_i6[2] = NLADD(_i6[2], 1); \
203			if (_i6[2] == 0) { \
204				_i6[1] = NLADD(_i6[1], 1); \
205				if (_i6[1] == 0) { \
206					_i6[0] = NLADD(_i6[0], 1); \
207				} \
208			} \
209		  } \
210		} while (0)
211#define	IP6_ADD(a,x,d)	\
212		do { i6addr_t *_s = (i6addr_t *)(a); \
213		  i6addr_t *_d = (i6addr_t *)(d); \
214		  _d->i6[0] = NLADD(_s->i6[0], x); \
215		  if (ntohl(_d->i6[0]) < ntohl(_s->i6[0])) { \
216			_d->i6[1] = NLADD(_d->i6[1], 1); \
217			if (ntohl(_d->i6[1]) < ntohl(_s->i6[1])) { \
218				_d->i6[2] = NLADD(_d->i6[2], 1); \
219				if (ntohl(_d->i6[2]) < ntohl(_s->i6[2])) { \
220					_d->i6[3] = NLADD(_d->i6[3], 1); \
221				} \
222			} \
223		  } \
224		} while (0)
225#define	IP6_AND(a,b,d)	do { i6addr_t *_s1 = (i6addr_t *)(a); \
226			  i6addr_t *_s2 = (i6addr_t *)(b); \
227			  i6addr_t *_d = (i6addr_t *)(d); \
228			  _d->i6[0] = _s1->i6[0] & _s2->i6[0]; \
229			  _d->i6[1] = _s1->i6[1] & _s2->i6[1]; \
230			  _d->i6[2] = _s1->i6[2] & _s2->i6[2]; \
231			  _d->i6[3] = _s1->i6[3] & _s2->i6[3]; \
232			} while (0)
233#define	IP6_ANDASSIGN(a,m) \
234			do { i6addr_t *_d = (i6addr_t *)(a); \
235			  i6addr_t *_m = (i6addr_t *)(m); \
236			  _d->i6[0] &= _m->i6[0]; \
237			  _d->i6[1] &= _m->i6[1]; \
238			  _d->i6[2] &= _m->i6[2]; \
239			  _d->i6[3] &= _m->i6[3]; \
240			} while (0)
241#define	IP6_MASKEQ(a,m,b) \
242			(((I60(a) & I60(m)) == I60(b)) && \
243			 ((I61(a) & I61(m)) == I61(b)) && \
244			 ((I62(a) & I62(m)) == I62(b)) && \
245			 ((I63(a) & I63(m)) == I63(b)))
246#define	IP6_MASKNEQ(a,m,b) \
247			(((I60(a) & I60(m)) != I60(b)) || \
248			 ((I61(a) & I61(m)) != I61(b)) || \
249			 ((I62(a) & I62(m)) != I62(b)) || \
250			 ((I63(a) & I63(m)) != I63(b)))
251#define	IP6_MERGE(a,b,c) \
252			do { i6addr_t *_d, *_s1, *_s2; \
253			  _d = (i6addr_t *)(a); \
254			  _s1 = (i6addr_t *)(b); \
255			  _s2 = (i6addr_t *)(c); \
256			  _d->i6[0] |= _s1->i6[0] & ~_s2->i6[0]; \
257			  _d->i6[1] |= _s1->i6[1] & ~_s2->i6[1]; \
258			  _d->i6[2] |= _s1->i6[2] & ~_s2->i6[2]; \
259			  _d->i6[3] |= _s1->i6[3] & ~_s2->i6[3]; \
260			} while (0)
261#define	IP6_MASK(a,b,c) \
262			do { i6addr_t *_d, *_s1, *_s2; \
263			  _d = (i6addr_t *)(a); \
264			  _s1 = (i6addr_t *)(b); \
265			  _s2 = (i6addr_t *)(c); \
266			  _d->i6[0] = _s1->i6[0] & ~_s2->i6[0]; \
267			  _d->i6[1] = _s1->i6[1] & ~_s2->i6[1]; \
268			  _d->i6[2] = _s1->i6[2] & ~_s2->i6[2]; \
269			  _d->i6[3] = _s1->i6[3] & ~_s2->i6[3]; \
270			} while (0)
271#define	IP6_SETONES(a)	\
272			do { i6addr_t *_d = (i6addr_t *)(a); \
273			  _d->i6[0] = 0xffffffff; \
274			  _d->i6[1] = 0xffffffff; \
275			  _d->i6[2] = 0xffffffff; \
276			  _d->i6[3] = 0xffffffff; \
277			} while (0)
278
279typedef	union ipso_u	{
280	u_short	ipso_ripso[2];
281	u_32_t	ipso_doi;
282} ipso_t;
283
284typedef	struct	fr_ip	{
285	u_32_t	fi_v:4;		/* IP version */
286	u_32_t	fi_xx:4;	/* spare */
287	u_32_t	fi_tos:8;	/* IP packet TOS */
288	u_32_t	fi_ttl:8;	/* IP packet TTL */
289	u_32_t	fi_p:8;		/* IP packet protocol */
290	u_32_t	fi_optmsk;	/* bitmask composed from IP options */
291	i6addr_t fi_src;	/* source address from packet */
292	i6addr_t fi_dst;	/* destination address from packet */
293	ipso_t	fi_ipso;	/* IP security options */
294	u_32_t	fi_flx;		/* packet flags */
295	u_32_t	fi_tcpmsk;	/* TCP options set/reset */
296	u_32_t	fi_ports[2];	/* TCP ports */
297	u_char	fi_tcpf;	/* TCP flags */
298	u_char	fi_sensitivity;
299	u_char	fi_xxx[2];	/* pad */
300} fr_ip_t;
301
302/*
303 * For use in fi_flx
304 */
305#define	FI_TCPUDP	0x0001	/* TCP/UCP implied comparison*/
306#define	FI_OPTIONS	0x0002
307#define	FI_FRAG		0x0004
308#define	FI_SHORT	0x0008
309#define	FI_NATED	0x0010
310#define	FI_MULTICAST	0x0020
311#define	FI_BROADCAST	0x0040
312#define	FI_MBCAST	0x0080
313#define	FI_STATE	0x0100
314#define	FI_BADNAT	0x0200
315#define	FI_BAD		0x0400
316#define	FI_OOW		0x0800	/* Out of state window, else match */
317#define	FI_ICMPERR	0x1000
318#define	FI_FRAGBODY	0x2000
319#define	FI_BADSRC	0x4000
320#define	FI_LOWTTL	0x8000
321#define	FI_CMP		0x5cfe3	/* Not FI_FRAG,FI_NATED,FI_FRAGTAIL */
322#define	FI_ICMPCMP	0x0003	/* Flags we can check for ICMP error packets */
323#define	FI_WITH		0x5effe	/* Not FI_TCPUDP */
324#define	FI_V6EXTHDR	0x10000
325#define	FI_COALESCE	0x20000
326#define	FI_NEWNAT	0x40000
327#define	FI_ICMPQUERY	0x80000
328#define	FI_ENCAP	0x100000	/* encap/decap with NAT */
329#define	FI_AH		0x200000	/* AH header present */
330#define	FI_DOCKSUM	0x10000000	/* Proxy wants L4 recalculation */
331#define	FI_NOCKSUM	0x20000000	/* don't do a L4 checksum validation */
332#define	FI_NOWILD	0x40000000	/* Do not do wildcard searches */
333#define	FI_IGNORE	0x80000000
334
335#define	fi_secmsk	fi_ipso.ipso_ripso[0]
336#define	fi_auth		fi_ipso.ipso_ripso[1]
337#define	fi_doi		fi_ipso.ipso_doi
338#define	fi_saddr	fi_src.in4.s_addr
339#define	fi_daddr	fi_dst.in4.s_addr
340#define	fi_srcnum	fi_src.iplookupnum
341#define	fi_dstnum	fi_dst.iplookupnum
342#define	fi_srcname	fi_src.iplookupname
343#define	fi_dstname	fi_dst.iplookupname
344#define	fi_srctype	fi_src.iplookuptype
345#define	fi_dsttype	fi_dst.iplookuptype
346#define	fi_srcsubtype	fi_src.iplookupsubtype
347#define	fi_dstsubtype	fi_dst.iplookupsubtype
348#define	fi_srcptr	fi_src.iplookupptr
349#define	fi_dstptr	fi_dst.iplookupptr
350#define	fi_srcfunc	fi_src.iplookupfunc
351#define	fi_dstfunc	fi_dst.iplookupfunc
352
353
354/*
355 * These are both used by the state and NAT code to indicate that one port or
356 * the other should be treated as a wildcard.
357 * NOTE: When updating, check bit masks in ip_state.h and update there too.
358 */
359#define	SI_W_SPORT	0x00000100
360#define	SI_W_DPORT	0x00000200
361#define	SI_WILDP	(SI_W_SPORT|SI_W_DPORT)
362#define	SI_W_SADDR	0x00000400
363#define	SI_W_DADDR	0x00000800
364#define	SI_WILDA	(SI_W_SADDR|SI_W_DADDR)
365#define	SI_NEWFR	0x00001000
366#define	SI_CLONE	0x00002000
367#define	SI_CLONED	0x00004000
368#define	SI_NEWCLONE	0x00008000
369
370typedef	struct {
371	u_short	fda_ports[2];
372	u_char	fda_tcpf;		/* TCP header flags (SYN, ACK, etc) */
373} frdat_t;
374
375typedef enum fr_breasons_e {
376	FRB_BLOCKED = 0,
377	FRB_LOGFAIL = 1,
378	FRB_PPSRATE = 2,
379	FRB_JUMBO = 3,
380	FRB_MAKEFRIP = 4,
381	FRB_STATEADD = 5,
382	FRB_UPDATEIPID = 6,
383	FRB_LOGFAIL2 = 7,
384	FRB_DECAPFRIP = 8,
385	FRB_AUTHNEW = 9,
386	FRB_AUTHCAPTURE = 10,
387	FRB_COALESCE = 11,
388	FRB_PULLUP = 12,
389	FRB_AUTHFEEDBACK = 13,
390	FRB_BADFRAG = 14,
391	FRB_NATV4 = 15,
392	FRB_NATV6 = 16,
393} fr_breason_t;
394
395#define	FRB_MAX_VALUE	16
396
397typedef enum ipf_cksum_e {
398	FI_CK_BAD = -1,
399	FI_CK_NEEDED = 0,
400	FI_CK_SUMOK = 1,
401	FI_CK_L4PART = 2,
402	FI_CK_L4FULL = 4
403} ipf_cksum_t;
404
405typedef	struct	fr_info	{
406	void	*fin_main_soft;
407	void	*fin_ifp;		/* interface packet is `on' */
408	struct	frentry *fin_fr;	/* last matching rule */
409	int	fin_out;		/* in or out ? 1 == out, 0 == in */
410	fr_ip_t	fin_fi;			/* IP Packet summary */
411	frdat_t	fin_dat;		/* TCP/UDP ports, ICMP code/type */
412	int	fin_dlen;		/* length of data portion of packet */
413	int	fin_plen;
414	u_32_t	fin_rule;		/* rule # last matched */
415	u_short	fin_hlen;		/* length of IP header in bytes */
416	char	fin_group[FR_GROUPLEN];	/* group number, -1 for none */
417	void	*fin_dp;		/* start of data past IP header */
418	/*
419	 * Fields after fin_dp aren't used for compression of log records.
420	 * fin_fi contains the IP version (fin_family)
421	 * fin_rule isn't included because adding a new rule can change it but
422	 * not change fin_fr. fin_rule is the rule number reported.
423	 * It isn't necessary to include fin_crc because that is checked
424	 * for explicitly, before calling bcmp.
425	 */
426	u_32_t	fin_crc;		/* Simple calculation for logging */
427	int	fin_family;		/* AF_INET, etc. */
428	int	fin_icode;		/* ICMP error to return */
429	int	fin_mtu;		/* MTU input for ICMP need-frag */
430	int	fin_rev;		/* state only: 1 = reverse */
431	int	fin_ipoff;		/* # bytes from buffer start to hdr */
432	u_32_t	fin_id;			/* IP packet id field */
433	u_short	fin_l4hlen;		/* length of L4 header, if known */
434	u_short	fin_off;
435	int	fin_depth;		/* Group nesting depth */
436	int	fin_error;		/* Error code to return */
437	ipf_cksum_t	fin_cksum;	/* -1 = bad, 1 = good, 0 = not done */
438	fr_breason_t	fin_reason;	/* why auto blocked */
439	u_int	fin_pktnum;
440	void	*fin_nattag;
441	struct frdest	*fin_dif;
442	struct frdest	*fin_tif;
443	union {
444		ip_t	*fip_ip;
445#ifdef USE_INET6
446		ip6_t	*fip_ip6;
447#endif
448	} fin_ipu;
449	mb_t	**fin_mp;		/* pointer to pointer to mbuf */
450	mb_t	*fin_m;			/* pointer to mbuf */
451#ifdef	MENTAT
452	mb_t	*fin_qfm;		/* pointer to mblk where pkt starts */
453	void	*fin_qpi;
454	char	fin_ifname[LIFNAMSIZ];
455#endif
456	void	*fin_fraghdr;		/* pointer to start of ipv6 frag hdr */
457} fr_info_t;
458
459#define	fin_ip		fin_ipu.fip_ip
460#define	fin_ip6		fin_ipu.fip_ip6
461#define	fin_v		fin_fi.fi_v
462#define	fin_p		fin_fi.fi_p
463#define	fin_flx		fin_fi.fi_flx
464#define	fin_optmsk	fin_fi.fi_optmsk
465#define	fin_secmsk	fin_fi.fi_secmsk
466#define	fin_doi		fin_fi.fi_doi
467#define	fin_auth	fin_fi.fi_auth
468#define	fin_src		fin_fi.fi_src.in4
469#define	fin_saddr	fin_fi.fi_saddr
470#define	fin_dst		fin_fi.fi_dst.in4
471#define	fin_daddr	fin_fi.fi_daddr
472#define	fin_data	fin_fi.fi_ports
473#define	fin_sport	fin_fi.fi_ports[0]
474#define	fin_dport	fin_fi.fi_ports[1]
475#define	fin_tcpf	fin_fi.fi_tcpf
476#define	fin_src6	fin_fi.fi_src
477#define	fin_dst6	fin_fi.fi_dst
478#define	fin_srcip6	fin_fi.fi_src.in6
479#define	fin_dstip6	fin_fi.fi_dst.in6
480
481#define	IPF_IN		0
482#define	IPF_OUT		1
483
484typedef	struct frentry	*(*ipfunc_t) __P((fr_info_t *, u_32_t *));
485typedef	int		(*ipfuncinit_t) __P((struct ipf_main_softc_s *, struct frentry *));
486
487typedef	struct	ipfunc_resolve	{
488	char		ipfu_name[32];
489	ipfunc_t	ipfu_addr;
490	ipfuncinit_t	ipfu_init;
491	ipfuncinit_t	ipfu_fini;
492} ipfunc_resolve_t;
493
494/*
495 * Size for compares on fr_info structures
496 */
497#define	FI_CSIZE	offsetof(fr_info_t, fin_icode)
498#define	FI_LCSIZE	offsetof(fr_info_t, fin_dp)
499
500/*
501 * Size for copying cache fr_info structure
502 */
503#define	FI_COPYSIZE	offsetof(fr_info_t, fin_dp)
504
505/*
506 * Structure for holding IPFilter's tag information
507 */
508#define	IPFTAG_LEN	16
509typedef	struct	{
510	union	{
511		u_32_t	iptu_num[4];
512		char	iptu_tag[IPFTAG_LEN];
513	} ipt_un;
514	int	ipt_not;
515} ipftag_t;
516
517#define	ipt_tag	ipt_un.iptu_tag
518#define	ipt_num	ipt_un.iptu_num
519
520/*
521 * Structure to define address for pool lookups.
522 */
523typedef	struct	{
524	u_char		adf_len;
525	sa_family_t	adf_family;
526	u_char		adf_xxx[2];
527	i6addr_t	adf_addr;
528} addrfamily_t;
529
530
531RBI_LINK(ipf_rb, host_node_s);
532
533typedef	struct	host_node_s {
534	RBI_FIELD(ipf_rb)	hn_entry;
535	addrfamily_t		hn_addr;
536	int			hn_active;
537} host_node_t;
538
539typedef RBI_HEAD(ipf_rb, host_node_s) ipf_rb_head_t;
540
541typedef	struct	host_track_s {
542	ipf_rb_head_t	ht_root;
543	int		ht_max_nodes;
544	int		ht_max_per_node;
545	int		ht_netmask;
546	int		ht_cur_nodes;
547} host_track_t;
548
549
550typedef enum fr_dtypes_e {
551	FRD_NORMAL = 0,
552	FRD_DSTLIST
553} fr_dtypes_t;
554/*
555 * This structure is used to hold information about the next hop for where
556 * to forward a packet.
557 */
558typedef	struct	frdest	{
559	void		*fd_ptr;
560	addrfamily_t	fd_addr;
561	fr_dtypes_t	fd_type;
562	int		fd_name;
563} frdest_t;
564
565#define	fd_ip6	fd_addr.adf_addr
566#define	fd_ip	fd_ip6.in4
567
568
569typedef enum fr_ctypes_e {
570	FR_NONE = 0,
571	FR_EQUAL,
572	FR_NEQUAL,
573	FR_LESST,
574	FR_GREATERT,
575	FR_LESSTE,
576	FR_GREATERTE,
577	FR_OUTRANGE,
578	FR_INRANGE,
579	FR_INCRANGE
580} fr_ctypes_t;
581
582/*
583 * This structure holds information about a port comparison.
584 */
585typedef	struct	frpcmp	{
586	fr_ctypes_t	frp_cmp;	/* data for port comparisons */
587	u_32_t		frp_port;	/* low port for <> and >< */
588	u_32_t		frp_top;	/* high port for <> and >< */
589} frpcmp_t;
590
591
592/*
593 * Structure containing all the relevant TCP/UDP things that can be checked in
594 * a filter rule.
595 */
596typedef	struct	frtuc	{
597	u_char		ftu_tcpfm;	/* tcp flags mask */
598	u_char		ftu_tcpf;	/* tcp flags */
599	frpcmp_t	ftu_src;	/* source port */
600	frpcmp_t	ftu_dst;	/* destination port */
601} frtuc_t;
602
603#define	ftu_scmp	ftu_src.frp_cmp
604#define	ftu_dcmp	ftu_dst.frp_cmp
605#define	ftu_sport	ftu_src.frp_port
606#define	ftu_dport	ftu_dst.frp_port
607#define	ftu_stop	ftu_src.frp_top
608#define	ftu_dtop	ftu_dst.frp_top
609
610#define	FR_TCPFMAX	0x3f
611
612typedef enum fr_atypes_e {
613	FRI_NONE = -1,	/* For LHS of NAT */
614	FRI_NORMAL = 0,	/* Normal address */
615	FRI_DYNAMIC,	/* dynamic address */
616	FRI_LOOKUP,	/* address is a pool # */
617	FRI_RANGE,	/* address/mask is a range */
618	FRI_NETWORK,	/* network address from if */
619	FRI_BROADCAST,	/* broadcast address from if */
620	FRI_PEERADDR,	/* Peer address for P-to-P */
621	FRI_NETMASKED,	/* network address with netmask from if */
622	FRI_SPLIT,	/* For NAT compatibility */
623	FRI_INTERFACE	/* address is based on interface name */
624} fr_atypes_t;
625
626/*
627 * This structure makes up what is considered to be the IPFilter specific
628 * matching components of a filter rule, as opposed to the data structures
629 * used to define the result which are in frentry_t and not here.
630 */
631typedef	struct	fripf	{
632	fr_ip_t		fri_ip;
633	fr_ip_t		fri_mip;	/* mask structure */
634
635	u_short		fri_icmpm;	/* data for ICMP packets (mask) */
636	u_short		fri_icmp;
637
638	frtuc_t		fri_tuc;
639	fr_atypes_t	fri_satype;	/* addres type */
640	fr_atypes_t	fri_datype;	/* addres type */
641	int		fri_sifpidx;	/* doing dynamic addressing */
642	int		fri_difpidx;	/* index into fr_ifps[] to use when */
643} fripf_t;
644
645#define	fri_dlookup	fri_mip.fi_dst
646#define	fri_slookup	fri_mip.fi_src
647#define	fri_dstnum	fri_mip.fi_dstnum
648#define	fri_srcnum	fri_mip.fi_srcnum
649#define	fri_dstname	fri_mip.fi_dstname
650#define	fri_srcname	fri_mip.fi_srcname
651#define	fri_dstptr	fri_mip.fi_dstptr
652#define	fri_srcptr	fri_mip.fi_srcptr
653
654
655typedef enum fr_rtypes_e {
656	FR_T_NONE = 0,
657	FR_T_IPF,		/* IPF structures */
658	FR_T_BPFOPC,		/* BPF opcode */
659	FR_T_CALLFUNC,		/* callout to function in fr_func only */
660	FR_T_COMPIPF,			/* compiled C code */
661	FR_T_IPFEXPR,			/* IPF expression */
662	FR_T_BUILTIN = 0x40000000,	/* rule is in kernel space */
663	FR_T_IPF_BUILTIN,
664	FR_T_BPFOPC_BUILTIN,
665	FR_T_CALLFUNC_BUILTIN,
666	FR_T_COMPIPF_BUILTIN,
667	FR_T_IPFEXPR_BUILTIN
668} fr_rtypes_t;
669
670typedef	struct	frentry	* (* frentfunc_t) __P((fr_info_t *));
671
672typedef	struct	frentry {
673	ipfmutex_t	fr_lock;
674	struct	frentry	*fr_next;
675	struct	frentry	**fr_pnext;
676	struct	frgroup	*fr_grp;
677	struct	frgroup	*fr_grphead;
678	struct	frgroup	*fr_icmpgrp;
679	struct	ipscan	*fr_isc;
680	struct	frentry	*fr_dnext;	/* 2 fr_die linked list pointers */
681	struct	frentry	**fr_pdnext;
682	void	*fr_ifas[4];
683	void	*fr_ptr;	/* for use with fr_arg */
684	int	fr_comment;	/* text comment for rule */
685	int	fr_size;	/* size of this structure */
686	int	fr_ref;		/* reference count */
687	int	fr_statecnt;	/* state count - for limit rules */
688	u_32_t	fr_die;		/* only used on loading the rule */
689	u_int	fr_cksum;	/* checksum on filter rules for performance */
690	/*
691	 * The line number from a file is here because we need to be able to
692	 * match the rule generated with ``grep rule ipf.conf | ipf -rf -''
693	 * with the rule loaded using ``ipf -f ipf.conf'' - thus it can't be
694	 * on the other side of fr_func.
695	 */
696	int	fr_flineno;	/* line number from conf file */
697	/*
698	 * These are only incremented when a packet  matches this rule and
699	 * it is the last match
700	 */
701	U_QUAD_T	fr_hits;
702	U_QUAD_T	fr_bytes;
703
704	/*
705	 * For PPS rate limiting
706	 * fr_lpu is used to always have the same size for this field,
707	 * allocating 64bits for seconds and 32bits for milliseconds.
708	 */
709	union {
710		struct timeval	frp_lastpkt;
711		char	frp_bytes[12];
712	} fr_lpu;
713	int		fr_curpps;
714
715	union	{
716		void		*fru_data;
717		char		*fru_caddr;
718		fripf_t		*fru_ipf;
719		frentfunc_t	fru_func;
720	} fr_dun;
721
722	/*
723	 * Fields after this may not change whilst in the kernel.
724	 */
725	ipfunc_t fr_func; 	/* call this function */
726	int	fr_dsize;
727	int	fr_pps;
728	fr_rtypes_t	fr_type;
729	u_32_t	fr_flags;	/* per-rule flags && options (see below) */
730	u_32_t	fr_logtag;	/* user defined log tag # */
731	u_32_t	fr_collect;	/* collection number */
732	u_int	fr_arg;		/* misc. numeric arg for rule */
733	u_int	fr_loglevel;	/* syslog log facility + priority */
734	u_char	fr_family;
735	u_char	fr_icode;	/* return ICMP code */
736	int	fr_group;	/* group to which this rule belongs */
737	int	fr_grhead;	/* group # which this rule starts */
738	int	fr_ifnames[4];
739	int	fr_isctag;
740	int	fr_rpc;		/* XID Filtering */
741	ipftag_t fr_nattag;
742	frdest_t fr_tifs[2];	/* "to"/"reply-to" interface */
743	frdest_t fr_dif;	/* duplicate packet interface */
744	/*
745	 * These are all options related to stateful filtering
746	 */
747	host_track_t	fr_srctrack;
748	int	fr_nostatelog;
749	int	fr_statemax;	/* max reference count */
750	int	fr_icmphead;	/* ICMP group  for state options */
751	u_int	fr_age[2];	/* non-TCP state timeouts */
752	/*
753	 * How big is the name buffer at the end?
754	 */
755	int	fr_namelen;
756	char	fr_names[1];
757} frentry_t;
758
759#define	fr_lastpkt	fr_lpu.frp_lastpkt
760#define	fr_caddr	fr_dun.fru_caddr
761#define	fr_data		fr_dun.fru_data
762#define	fr_dfunc	fr_dun.fru_func
763#define	fr_ipf		fr_dun.fru_ipf
764#define	fr_ip		fr_ipf->fri_ip
765#define	fr_mip		fr_ipf->fri_mip
766#define	fr_icmpm	fr_ipf->fri_icmpm
767#define	fr_icmp		fr_ipf->fri_icmp
768#define	fr_tuc		fr_ipf->fri_tuc
769#define	fr_satype	fr_ipf->fri_satype
770#define	fr_datype	fr_ipf->fri_datype
771#define	fr_sifpidx	fr_ipf->fri_sifpidx
772#define	fr_difpidx	fr_ipf->fri_difpidx
773#define	fr_proto	fr_ip.fi_p
774#define	fr_mproto	fr_mip.fi_p
775#define	fr_ttl		fr_ip.fi_ttl
776#define	fr_mttl		fr_mip.fi_ttl
777#define	fr_tos		fr_ip.fi_tos
778#define	fr_mtos		fr_mip.fi_tos
779#define	fr_tcpfm	fr_tuc.ftu_tcpfm
780#define	fr_tcpf		fr_tuc.ftu_tcpf
781#define	fr_scmp		fr_tuc.ftu_scmp
782#define	fr_dcmp		fr_tuc.ftu_dcmp
783#define	fr_dport	fr_tuc.ftu_dport
784#define	fr_sport	fr_tuc.ftu_sport
785#define	fr_stop		fr_tuc.ftu_stop
786#define	fr_dtop		fr_tuc.ftu_dtop
787#define	fr_dst		fr_ip.fi_dst.in4
788#define	fr_dst6		fr_ip.fi_dst
789#define	fr_daddr	fr_ip.fi_dst.in4.s_addr
790#define	fr_src		fr_ip.fi_src.in4
791#define	fr_src6		fr_ip.fi_src
792#define	fr_saddr	fr_ip.fi_src.in4.s_addr
793#define	fr_dmsk		fr_mip.fi_dst.in4
794#define	fr_dmsk6	fr_mip.fi_dst
795#define	fr_dmask	fr_mip.fi_dst.in4.s_addr
796#define	fr_smsk		fr_mip.fi_src.in4
797#define	fr_smsk6	fr_mip.fi_src
798#define	fr_smask	fr_mip.fi_src.in4.s_addr
799#define	fr_dstnum	fr_ip.fi_dstnum
800#define	fr_srcnum	fr_ip.fi_srcnum
801#define	fr_dlookup	fr_ip.fi_dst
802#define	fr_slookup	fr_ip.fi_src
803#define	fr_dstname	fr_ip.fi_dstname
804#define	fr_srcname	fr_ip.fi_srcname
805#define	fr_dsttype	fr_ip.fi_dsttype
806#define	fr_srctype	fr_ip.fi_srctype
807#define	fr_dstsubtype	fr_ip.fi_dstsubtype
808#define	fr_srcsubtype	fr_ip.fi_srcsubtype
809#define	fr_dstptr	fr_mip.fi_dstptr
810#define	fr_srcptr	fr_mip.fi_srcptr
811#define	fr_dstfunc	fr_mip.fi_dstfunc
812#define	fr_srcfunc	fr_mip.fi_srcfunc
813#define	fr_optbits	fr_ip.fi_optmsk
814#define	fr_optmask	fr_mip.fi_optmsk
815#define	fr_secbits	fr_ip.fi_secmsk
816#define	fr_secmask	fr_mip.fi_secmsk
817#define	fr_authbits	fr_ip.fi_auth
818#define	fr_authmask	fr_mip.fi_auth
819#define	fr_doi		fr_ip.fi_doi
820#define	fr_doimask	fr_mip.fi_doi
821#define	fr_flx		fr_ip.fi_flx
822#define	fr_mflx		fr_mip.fi_flx
823#define	fr_ifa		fr_ifas[0]
824#define	fr_oifa		fr_ifas[2]
825#define	fr_tif		fr_tifs[0]
826#define	fr_rif		fr_tifs[1]
827
828#define	FR_NOLOGTAG	0
829
830#define	FR_CMPSIZ	(sizeof(struct frentry) - \
831			 offsetof(struct frentry, fr_func))
832#define	FR_NAME(_f, _n)	(_f)->fr_names + (_f)->_n
833
834
835/*
836 * fr_flags
837 */
838#define	FR_BLOCK	0x00001	/* do not allow packet to pass */
839#define	FR_PASS		0x00002	/* allow packet to pass */
840#define	FR_AUTH		0x00003	/* use authentication */
841#define	FR_PREAUTH	0x00004	/* require preauthentication */
842#define	FR_ACCOUNT	0x00005	/* Accounting rule */
843#define	FR_SKIP		0x00006	/* skip rule */
844#define	FR_DECAPSULATE	0x00008	/* decapsulate rule */
845#define	FR_CALL		0x00009	/* call rule */
846#define	FR_CMDMASK	0x0000f
847#define	FR_LOG		0x00010	/* Log */
848#define	FR_LOGB		0x00011	/* Log-fail */
849#define	FR_LOGP		0x00012	/* Log-pass */
850#define	FR_LOGMASK	(FR_LOG|FR_CMDMASK)
851#define	FR_CALLNOW	0x00020	/* call another function (fr_func) if matches */
852#define	FR_NOTSRCIP	0x00040
853#define	FR_NOTDSTIP	0x00080
854#define	FR_QUICK	0x00100	/* match & stop processing list */
855#define	FR_KEEPFRAG	0x00200	/* keep fragment information */
856#define	FR_KEEPSTATE	0x00400	/* keep `connection' state information */
857#define	FR_FASTROUTE	0x00800	/* bypass normal routing */
858#define	FR_RETRST	0x01000	/* Return TCP RST packet - reset connection */
859#define	FR_RETICMP	0x02000	/* Return ICMP unreachable packet */
860#define	FR_FAKEICMP	0x03000	/* Return ICMP unreachable with fake source */
861#define	FR_OUTQUE	0x04000	/* outgoing packets */
862#define	FR_INQUE	0x08000	/* ingoing packets */
863#define	FR_LOGBODY	0x10000	/* Log the body */
864#define	FR_LOGFIRST	0x20000	/* Log the first byte if state held */
865#define	FR_LOGORBLOCK	0x40000	/* block the packet if it can't be logged */
866#define	FR_STLOOSE	0x80000	/* loose state checking */
867#define	FR_FRSTRICT	0x100000	/* strict frag. cache */
868#define	FR_STSTRICT	0x200000	/* strict keep state */
869#define	FR_NEWISN	0x400000	/* new ISN for outgoing TCP */
870#define	FR_NOICMPERR	0x800000	/* do not match ICMP errors in state */
871#define	FR_STATESYNC	0x1000000	/* synchronize state to slave */
872#define	FR_COPIED	0x2000000	/* copied from user space */
873#define	FR_INACTIVE	0x4000000	/* only used when flush'ing rules */
874#define	FR_NOMATCH	0x8000000	/* no match occured */
875		/*	0x10000000 	FF_LOGPASS */
876		/*	0x20000000 	FF_LOGBLOCK */
877		/*	0x40000000 	FF_LOGNOMATCH */
878		/*	0x80000000 	FF_BLOCKNONIP */
879
880#define	FR_RETMASK	(FR_RETICMP|FR_RETRST|FR_FAKEICMP)
881#define	FR_ISBLOCK(x)	(((x) & FR_CMDMASK) == FR_BLOCK)
882#define	FR_ISPASS(x)	(((x) & FR_CMDMASK) == FR_PASS)
883#define	FR_ISAUTH(x)	(((x) & FR_CMDMASK) == FR_AUTH)
884#define	FR_ISPREAUTH(x)	(((x) & FR_CMDMASK) == FR_PREAUTH)
885#define	FR_ISACCOUNT(x)	(((x) & FR_CMDMASK) == FR_ACCOUNT)
886#define	FR_ISSKIP(x)	(((x) & FR_CMDMASK) == FR_SKIP)
887#define	FR_ISDECAPS(x)	(((x) & FR_CMDMASK) == FR_DECAPSULATE)
888#define	FR_ISNOMATCH(x)	((x) & FR_NOMATCH)
889#define	FR_INOUT	(FR_INQUE|FR_OUTQUE)
890
891/*
892 * recognized flags for SIOCGETFF and SIOCSETFF, and get put in fr_flags
893 */
894#define	FF_LOGPASS	0x10000000
895#define	FF_LOGBLOCK	0x20000000
896#define	FF_LOGNOMATCH	0x40000000
897#define	FF_LOGGING	(FF_LOGPASS|FF_LOGBLOCK|FF_LOGNOMATCH)
898#define	FF_BLOCKNONIP	0x80000000	/* Solaris2 Only */
899
900
901/*
902 * Structure that passes information on what/how to flush to the kernel.
903 */
904typedef	struct	ipfflush	{
905	int		ipflu_how;
906	int		ipflu_arg;
907} ipfflush_t;
908
909
910/*
911 *
912 */
913typedef	struct	ipfgetctl	{
914	u_int		ipfg_min;	/* min value */
915	u_int		ipfg_current;	/* current value */
916	u_int		ipfg_max;	/* max value */
917	u_int		ipfg_default;	/* default value */
918	u_int		ipfg_steps;	/* value increments */
919	char		ipfg_name[40];	/* tag name for this control */
920} ipfgetctl_t;
921
922typedef	struct	ipfsetctl	{
923	int	ipfs_which;	/* 0 = min 1 = current 2 = max 3 = default */
924	u_int	ipfs_value;	/* min value */
925	char	ipfs_name[40];	/* tag name for this control */
926} ipfsetctl_t;
927
928
929/*
930 * Some of the statistics below are in their own counters, but most are kept
931 * in this single structure so that they can all easily be collected and
932 * copied back as required.
933 */
934typedef	struct	ipf_statistics {
935	u_long	fr_icmp_coalesce;
936	u_long	fr_tcp_frag;
937	u_long	fr_tcp_pullup;
938	u_long	fr_tcp_short;
939	u_long	fr_tcp_small;
940	u_long	fr_tcp_bad_flags;
941	u_long	fr_udp_pullup;
942	u_long	fr_ip_freed;
943	u_long	fr_v6_ah_bad;
944	u_long	fr_v6_bad;
945	u_long	fr_v6_badfrag;
946	u_long	fr_v6_dst_bad;
947	u_long	fr_v6_esp_pullup;
948	u_long	fr_v6_ext_short;
949	u_long	fr_v6_ext_pullup;
950	u_long	fr_v6_ext_hlen;
951	u_long	fr_v6_frag_bad;
952	u_long	fr_v6_frag_pullup;
953	u_long	fr_v6_frag_size;
954	u_long	fr_v6_gre_pullup;
955	u_long	fr_v6_icmp6_pullup;
956	u_long	fr_v6_rh_bad;
957	u_long	fr_v6_badttl;	/* TTL in packet doesn't reach minimum */
958	u_long	fr_v4_ah_bad;
959	u_long	fr_v4_ah_pullup;
960	u_long	fr_v4_esp_pullup;
961	u_long	fr_v4_cipso_bad;
962	u_long	fr_v4_cipso_tlen;
963	u_long	fr_v4_gre_frag;
964	u_long	fr_v4_gre_pullup;
965	u_long	fr_v4_icmp_frag;
966	u_long	fr_v4_icmp_pullup;
967	u_long	fr_v4_badttl;	/* TTL in packet doesn't reach minimum */
968	u_long	fr_v4_badsrc;	/* source received doesn't match route */
969	u_long	fr_l4_badcksum;	/* layer 4 header checksum failure */
970	u_long	fr_badcoalesces;
971	u_long	fr_pass;	/* packets allowed */
972	u_long	fr_block;	/* packets denied */
973	u_long	fr_nom;		/* packets which don't match any rule */
974	u_long	fr_short;	/* packets which are short */
975	u_long	fr_ppkl;	/* packets allowed and logged */
976	u_long	fr_bpkl;	/* packets denied and logged */
977	u_long	fr_npkl;	/* packets unmatched and logged */
978	u_long	fr_ret;		/* packets for which a return is sent */
979	u_long	fr_acct;	/* packets for which counting was performed */
980	u_long	fr_bnfr;	/* bad attempts to allocate fragment state */
981	u_long	fr_nfr;		/* new fragment state kept */
982	u_long	fr_cfr;		/* add new fragment state but complete pkt */
983	u_long	fr_bads;	/* bad attempts to allocate packet state */
984	u_long	fr_ads;		/* new packet state kept */
985	u_long	fr_chit;	/* cached hit */
986	u_long	fr_cmiss;	/* cached miss */
987	u_long	fr_tcpbad;	/* TCP checksum check failures */
988	u_long	fr_pull[2];	/* good and bad pullup attempts */
989	u_long	fr_bad;		/* bad IP packets to the filter */
990	u_long	fr_ipv6;	/* IPv6 packets in/out */
991	u_long	fr_ppshit;	/* dropped because of pps ceiling */
992	u_long	fr_ipud;	/* IP id update failures */
993	u_long	fr_blocked[FRB_MAX_VALUE + 1];
994} ipf_statistics_t;
995
996/*
997 * Log structure.  Each packet header logged is prepended by one of these.
998 * Following this in the log records read from the device will be an ipflog
999 * structure which is then followed by any packet data.
1000 */
1001typedef	struct	iplog	{
1002	u_32_t		ipl_magic;
1003	u_int		ipl_count;
1004	u_32_t		ipl_seqnum;
1005	struct	timeval	ipl_time;
1006	size_t		ipl_dsize;
1007	struct	iplog	*ipl_next;
1008} iplog_t;
1009
1010#define	ipl_sec		ipl_time.tv_sec
1011#define	ipl_usec	ipl_time.tv_usec
1012
1013#define IPL_MAGIC	0x49504c4d	/* 'IPLM' */
1014#define IPL_MAGIC_NAT	0x49504c4e	/* 'IPLN' */
1015#define IPL_MAGIC_STATE	0x49504c53	/* 'IPLS' */
1016#define	IPLOG_SIZE	sizeof(iplog_t)
1017
1018typedef	struct	ipflog	{
1019	u_int		fl_unit;
1020	u_32_t		fl_rule;
1021	u_32_t		fl_flags;
1022	u_32_t		fl_lflags;
1023	u_32_t		fl_logtag;
1024	ipftag_t	fl_nattag;
1025	u_short		fl_plen;	/* extra data after hlen */
1026	u_short		fl_loglevel;	/* syslog log level */
1027	char		fl_group[FR_GROUPLEN];
1028	u_char		fl_hlen;	/* length of IP headers saved */
1029	u_char		fl_dir;
1030	u_char		fl_breason;	/* from fin_reason */
1031	u_char		fl_family;	/* address family of packet logged */
1032	char		fl_ifname[LIFNAMSIZ];
1033} ipflog_t;
1034
1035#ifndef	IPF_LOGGING
1036# define	IPF_LOGGING	0
1037#endif
1038#ifndef	IPF_DEFAULT_PASS
1039# define	IPF_DEFAULT_PASS	FR_PASS
1040#endif
1041
1042#define	DEFAULT_IPFLOGSIZE	32768
1043#ifndef	IPFILTER_LOGSIZE
1044# define	IPFILTER_LOGSIZE	DEFAULT_IPFLOGSIZE
1045#else
1046# if IPFILTER_LOGSIZE < 8192
1047#  error IPFILTER_LOGSIZE too small.  Must be >= 8192
1048# endif
1049#endif
1050
1051#define	IPF_OPTCOPY	0x07ff00	/* bit mask of copied options */
1052
1053/*
1054 * Device filenames for reading log information.  Use ipf on Solaris2 because
1055 * ipl is already a name used by something else.
1056 */
1057#ifndef	IPL_NAME
1058# if	SOLARIS
1059#  define	IPL_NAME	"/dev/ipf"
1060# else
1061#  define	IPL_NAME	"/dev/ipl"
1062# endif
1063#endif
1064/*
1065 * Pathnames for various IP Filter control devices.  Used by LKM
1066 * and userland, so defined here.
1067 */
1068#define	IPNAT_NAME	"/dev/ipnat"
1069#define	IPSTATE_NAME	"/dev/ipstate"
1070#define	IPAUTH_NAME	"/dev/ipauth"
1071#define	IPSYNC_NAME	"/dev/ipsync"
1072#define	IPSCAN_NAME	"/dev/ipscan"
1073#define	IPLOOKUP_NAME	"/dev/iplookup"
1074
1075#define	IPL_LOGIPF	0	/* Minor device #'s for accessing logs */
1076#define	IPL_LOGNAT	1
1077#define	IPL_LOGSTATE	2
1078#define	IPL_LOGAUTH	3
1079#define	IPL_LOGSYNC	4
1080#define	IPL_LOGSCAN	5
1081#define	IPL_LOGLOOKUP	6
1082#define	IPL_LOGCOUNT	7
1083#define	IPL_LOGMAX	7
1084#define	IPL_LOGSIZE	IPL_LOGMAX + 1
1085#define	IPL_LOGALL	-1
1086#define	IPL_LOGNONE	-2
1087
1088/*
1089 * For SIOCGETFS
1090 */
1091typedef	struct	friostat	{
1092	ipf_statistics_t f_st[2];
1093	frentry_t	*f_ipf[2][2];
1094	frentry_t	*f_acct[2][2];
1095	frentry_t	*f_auth;
1096	struct frgroup	*f_groups[IPL_LOGSIZE][2];
1097	u_long		f_froute[2];
1098	u_long		f_log_ok;
1099	u_long		f_log_fail;
1100	u_long		f_rb_no_mem;
1101	u_long		f_rb_node_max;
1102	u_32_t		f_ticks;
1103	int		f_locks[IPL_LOGSIZE];
1104	int		f_defpass;	/* default pass - from fr_pass */
1105	int		f_active;	/* 1 or 0 - active rule set */
1106	int		f_running;	/* 1 if running, else 0 */
1107	int		f_logging;	/* 1 if enabled, else 0 */
1108	int		f_features;
1109	char		f_version[32];	/* version string */
1110} friostat_t;
1111
1112#define	f_fin		f_ipf[0]
1113#define	f_fout		f_ipf[1]
1114#define	f_acctin	f_acct[0]
1115#define	f_acctout	f_acct[1]
1116
1117#define	IPF_FEAT_LKM		0x001
1118#define	IPF_FEAT_LOG		0x002
1119#define	IPF_FEAT_LOOKUP		0x004
1120#define	IPF_FEAT_BPF		0x008
1121#define	IPF_FEAT_COMPILED	0x010
1122#define	IPF_FEAT_CKSUM		0x020
1123#define	IPF_FEAT_SYNC		0x040
1124#define	IPF_FEAT_SCAN		0x080
1125#define	IPF_FEAT_IPV6		0x100
1126
1127typedef struct	optlist {
1128	u_short ol_val;
1129	int	ol_bit;
1130} optlist_t;
1131
1132
1133/*
1134 * Group list structure.
1135 */
1136typedef	struct frgroup {
1137	struct frgroup	*fg_next;
1138	struct frentry	*fg_head;
1139	struct frentry	*fg_start;
1140	struct frgroup	**fg_set;
1141	u_32_t		fg_flags;
1142	int		fg_ref;
1143	char		fg_name[FR_GROUPLEN];
1144} frgroup_t;
1145
1146#define	FG_NAME(g)	(*(g)->fg_name == '\0' ? "" : (g)->fg_name)
1147
1148
1149/*
1150 * Used by state and NAT tables
1151 */
1152typedef struct icmpinfo {
1153	u_short		ici_id;
1154	u_short		ici_seq;
1155	u_char		ici_type;
1156} icmpinfo_t;
1157
1158typedef struct udpinfo {
1159	u_short		us_sport;
1160	u_short		us_dport;
1161} udpinfo_t;
1162
1163
1164typedef	struct	tcpdata	{
1165	u_32_t		td_end;
1166	u_32_t		td_maxend;
1167	u_32_t		td_maxwin;
1168	u_32_t		td_winscale;
1169	u_32_t		td_maxseg;
1170	int		td_winflags;
1171} tcpdata_t;
1172
1173#define	TCP_WSCALE_MAX		14
1174
1175#define	TCP_WSCALE_SEEN		0x00000001
1176#define	TCP_WSCALE_FIRST	0x00000002
1177#define	TCP_SACK_PERMIT		0x00000004
1178
1179
1180typedef	struct tcpinfo {
1181	u_32_t		ts_sport;
1182	u_32_t		ts_dport;
1183	tcpdata_t	ts_data[2];
1184} tcpinfo_t;
1185
1186
1187/*
1188 * Structures to define a GRE header as seen in a packet.
1189 */
1190struct	grebits	{
1191#if defined(sparc)
1192	u_32_t		grb_ver:3;
1193	u_32_t		grb_flags:3;
1194	u_32_t		grb_A:1;
1195	u_32_t		grb_recur:1;
1196	u_32_t		grb_s:1;
1197	u_32_t		grb_S:1;
1198	u_32_t		grb_K:1;
1199	u_32_t		grb_R:1;
1200	u_32_t		grb_C:1;
1201#else
1202	u_32_t		grb_C:1;
1203	u_32_t		grb_R:1;
1204	u_32_t		grb_K:1;
1205	u_32_t		grb_S:1;
1206	u_32_t		grb_s:1;
1207	u_32_t		grb_recur:1;
1208	u_32_t		grb_A:1;
1209	u_32_t		grb_flags:3;
1210	u_32_t		grb_ver:3;
1211#endif
1212	u_short		grb_ptype;
1213};
1214
1215typedef	struct	grehdr	{
1216	union	{
1217		struct	grebits	gru_bits;
1218		u_short	gru_flags;
1219	} gr_un;
1220	u_short		gr_len;
1221	u_short		gr_call;
1222} grehdr_t;
1223
1224#define	gr_flags	gr_un.gru_flags
1225#define	gr_bits		gr_un.gru_bits
1226#define	gr_ptype	gr_bits.grb_ptype
1227#define	gr_C		gr_bits.grb_C
1228#define	gr_R		gr_bits.grb_R
1229#define	gr_K		gr_bits.grb_K
1230#define	gr_S		gr_bits.grb_S
1231#define	gr_s		gr_bits.grb_s
1232#define	gr_recur	gr_bits.grb_recur
1233#define	gr_A		gr_bits.grb_A
1234#define	gr_ver		gr_bits.grb_ver
1235
1236/*
1237 * GRE information tracked by "keep state"
1238 */
1239typedef	struct	greinfo	{
1240	u_short		gs_call[2];
1241	u_short		gs_flags;
1242	u_short		gs_ptype;
1243} greinfo_t;
1244
1245#define	GRE_REV(x)	((ntohs(x) >> 13) & 7)
1246
1247
1248/*
1249 * Format of an Authentication header
1250 */
1251typedef	struct	authhdr	{
1252	u_char		ah_next;
1253	u_char		ah_plen;
1254	u_short		ah_reserved;
1255	u_32_t		ah_spi;
1256	u_32_t		ah_seq;
1257	/* Following the sequence number field is 0 or more bytes of */
1258	/* authentication data, as specified by ah_plen - RFC 2402.  */
1259} authhdr_t;
1260
1261
1262/*
1263 * Timeout tail queue list member
1264 */
1265typedef	struct	ipftqent	{
1266	struct ipftqent **tqe_pnext;
1267	struct ipftqent *tqe_next;
1268	struct	ipftq	*tqe_ifq;
1269	void		*tqe_parent;	/* pointer back to NAT/state struct */
1270	u_32_t		tqe_die;	/* when this entriy is to die */
1271	u_32_t		tqe_touched;
1272	int		tqe_flags;
1273	int		tqe_state[2];	/* current state of this entry */
1274} ipftqent_t;
1275
1276#define	TQE_RULEBASED	0x00000001
1277#define	TQE_DELETE	0x00000002
1278
1279
1280/*
1281 * Timeout tail queue head for IPFilter
1282 */
1283typedef struct  ipftq   {
1284	ipfmutex_t	ifq_lock;
1285	u_int		ifq_ttl;
1286	ipftqent_t	*ifq_head;
1287	ipftqent_t	**ifq_tail;
1288	struct ipftq	*ifq_next;
1289	struct ipftq	**ifq_pnext;
1290	int		ifq_ref;
1291	u_int		ifq_flags;
1292} ipftq_t;
1293
1294#define	IFQF_USER	0x01		/* User defined aging */
1295#define	IFQF_DELETE	0x02		/* Marked for deletion */
1296#define	IFQF_PROXY	0x04		/* Timeout queue in use by a proxy */
1297
1298#define	IPFTQ_INIT(x,y,z)	do {			\
1299					(x)->ifq_ttl = (y);	\
1300					(x)->ifq_head = NULL;	\
1301					(x)->ifq_ref = 1;	\
1302					(x)->ifq_tail = &(x)->ifq_head; \
1303					MUTEX_INIT(&(x)->ifq_lock, (z)); \
1304				} while (0)
1305
1306#define	IPF_HZ_MULT	1
1307#define	IPF_HZ_DIVIDE	2		/* How many times a second ipfilter */
1308					/* checks its timeout queues.       */
1309#define	IPF_TTLVAL(x)	(((x) / IPF_HZ_MULT) * IPF_HZ_DIVIDE)
1310
1311typedef	int	(*ipftq_delete_fn_t)(struct ipf_main_softc_s *, void *);
1312
1313
1314/*
1315 * Object structure description.  For passing through in ioctls.
1316 */
1317typedef	struct	ipfobj	{
1318	u_32_t		ipfo_rev;	/* IPFilter version number */
1319	u_32_t		ipfo_size;	/* size of object at ipfo_ptr */
1320	void		*ipfo_ptr;	/* pointer to object */
1321	int		ipfo_type;	/* type of object being pointed to */
1322	int		ipfo_offset;	/* bytes from ipfo_ptr where to start */
1323	int		ipfo_retval;	/* return value */
1324	u_char		ipfo_xxxpad[28];	/* reserved for future use */
1325} ipfobj_t;
1326
1327#define	IPFOBJ_FRENTRY		0	/* struct frentry */
1328#define	IPFOBJ_IPFSTAT		1	/* struct friostat */
1329#define	IPFOBJ_IPFINFO		2	/* struct fr_info */
1330#define	IPFOBJ_AUTHSTAT		3	/* struct fr_authstat */
1331#define	IPFOBJ_FRAGSTAT		4	/* struct ipfrstat */
1332#define	IPFOBJ_IPNAT		5	/* struct ipnat */
1333#define	IPFOBJ_NATSTAT		6	/* struct natstat */
1334#define	IPFOBJ_STATESAVE	7	/* struct ipstate_save */
1335#define	IPFOBJ_NATSAVE		8	/* struct nat_save */
1336#define	IPFOBJ_NATLOOKUP	9	/* struct natlookup */
1337#define	IPFOBJ_IPSTATE		10	/* struct ipstate */
1338#define	IPFOBJ_STATESTAT	11	/* struct ips_stat */
1339#define	IPFOBJ_FRAUTH		12	/* struct frauth */
1340#define	IPFOBJ_TUNEABLE		13	/* struct ipftune */
1341#define	IPFOBJ_NAT		14	/* struct nat */
1342#define	IPFOBJ_IPFITER		15	/* struct ipfruleiter */
1343#define	IPFOBJ_GENITER		16	/* struct ipfgeniter */
1344#define	IPFOBJ_GTABLE		17	/* struct ipftable */
1345#define	IPFOBJ_LOOKUPITER	18	/* struct ipflookupiter */
1346#define	IPFOBJ_STATETQTAB	19	/* struct ipftq * NSTATES */
1347#define	IPFOBJ_IPFEXPR		20
1348#define	IPFOBJ_PROXYCTL		21	/* strct ap_ctl */
1349#define	IPFOBJ_FRIPF		22	/* structfripf */
1350#define	IPFOBJ_COUNT		23	/* How many #defines are above this? */
1351
1352
1353typedef	union	ipftunevalptr	{
1354	void		*ipftp_void;
1355	u_long		*ipftp_long;
1356	u_int		*ipftp_int;
1357	u_short		*ipftp_short;
1358	u_char		*ipftp_char;
1359	u_long		ipftp_offset;
1360} ipftunevalptr_t;
1361
1362typedef	union	ipftuneval	{
1363	u_long		ipftu_long;
1364	u_int		ipftu_int;
1365	u_short		ipftu_short;
1366	u_char		ipftu_char;
1367} ipftuneval_t;
1368
1369struct ipftuneable;
1370typedef	int (* ipftunefunc_t) __P((struct ipf_main_softc_s *, struct ipftuneable *, ipftuneval_t *));
1371
1372typedef	struct	ipftuneable	{
1373	ipftunevalptr_t	ipft_una;
1374	const char	*ipft_name;
1375	u_long		ipft_min;
1376	u_long		ipft_max;
1377	int		ipft_sz;
1378	int		ipft_flags;
1379	struct ipftuneable *ipft_next;
1380	ipftunefunc_t	ipft_func;
1381} ipftuneable_t;
1382
1383#define	ipft_addr	ipft_una.ipftp_void
1384#define	ipft_plong	ipft_una.ipftp_long
1385#define	ipft_pint	ipft_una.ipftp_int
1386#define	ipft_pshort	ipft_una.ipftp_short
1387#define	ipft_pchar	ipft_una.ipftp_char
1388
1389#define	IPFT_RDONLY	1	/* read-only */
1390#define	IPFT_WRDISABLED	2	/* write when disabled only */
1391
1392typedef	struct	ipftune	{
1393	void    	*ipft_cookie;
1394	ipftuneval_t	ipft_un;
1395	u_long  	ipft_min;
1396	u_long  	ipft_max;
1397	int		ipft_sz;
1398	int		ipft_flags;
1399	char		ipft_name[80];
1400} ipftune_t;
1401
1402#define	ipft_vlong	ipft_un.ipftu_long
1403#define	ipft_vint	ipft_un.ipftu_int
1404#define	ipft_vshort	ipft_un.ipftu_short
1405#define	ipft_vchar	ipft_un.ipftu_char
1406
1407/*
1408 * Hash table header
1409 */
1410#define	IPFHASH(x,y)	typedef struct { 			\
1411				ipfrwlock_t	ipfh_lock;	\
1412				struct	x	*ipfh_head;	\
1413				} y
1414
1415/*
1416** HPUX Port
1417*/
1418
1419#if !defined(CDEV_MAJOR) && defined (__FreeBSD_version) && \
1420    (__FreeBSD_version >= 220000)
1421# define	CDEV_MAJOR	79
1422#endif
1423
1424#ifdef _KERNEL
1425# define	FR_VERBOSE(verb_pr)
1426# define	FR_DEBUG(verb_pr)
1427#else
1428extern	void	ipfkdebug __P((char *, ...));
1429extern	void	ipfkverbose __P((char *, ...));
1430# define	FR_VERBOSE(verb_pr)	ipfkverbose verb_pr
1431# define	FR_DEBUG(verb_pr)	ipfkdebug verb_pr
1432#endif
1433
1434/*
1435 *
1436 */
1437typedef	struct	ipfruleiter {
1438	int		iri_inout;
1439	char		iri_group[FR_GROUPLEN];
1440	int		iri_active;
1441	int		iri_nrules;
1442	int		iri_v;		/* No longer used (compatibility) */
1443	frentry_t	*iri_rule;
1444} ipfruleiter_t;
1445
1446/*
1447 * Values for iri_inout
1448 */
1449#define	F_IN	0
1450#define	F_OUT	1
1451#define	F_ACIN	2
1452#define	F_ACOUT	3
1453
1454
1455typedef	struct	ipfgeniter {
1456	int	igi_type;
1457	int	igi_nitems;
1458	void	*igi_data;
1459} ipfgeniter_t;
1460
1461#define	IPFGENITER_IPF		0
1462#define	IPFGENITER_NAT		1
1463#define	IPFGENITER_IPNAT	2
1464#define	IPFGENITER_FRAG		3
1465#define	IPFGENITER_AUTH		4
1466#define	IPFGENITER_STATE	5
1467#define	IPFGENITER_NATFRAG	6
1468#define	IPFGENITER_HOSTMAP	7
1469#define	IPFGENITER_LOOKUP	8
1470
1471typedef	struct	ipftable {
1472	int	ita_type;
1473	void	*ita_table;
1474} ipftable_t;
1475
1476#define	IPFTABLE_BUCKETS	1
1477#define	IPFTABLE_BUCKETS_NATIN	2
1478#define	IPFTABLE_BUCKETS_NATOUT	3
1479
1480
1481typedef struct ipf_v4_masktab_s {
1482	u_32_t	imt4_active[33];
1483	int	imt4_masks[33];
1484	int	imt4_max;
1485} ipf_v4_masktab_t;
1486
1487typedef struct ipf_v6_masktab_s {
1488	i6addr_t	imt6_active[129];
1489	int		imt6_masks[129];
1490	int		imt6_max;
1491} ipf_v6_masktab_t;
1492
1493
1494/*
1495 *
1496 */
1497typedef struct ipftoken {
1498	struct ipftoken	*ipt_next;
1499	struct ipftoken	**ipt_pnext;
1500	void		*ipt_ctx;
1501	void		*ipt_data;
1502	u_long		ipt_die;
1503	int		ipt_type;
1504	int		ipt_uid;
1505	int		ipt_subtype;
1506	int		ipt_ref;
1507	int		ipt_complete;
1508} ipftoken_t;
1509
1510
1511/*
1512 *
1513 */
1514typedef struct ipfexp {
1515	int		ipfe_cmd;
1516	int		ipfe_not;
1517	int		ipfe_narg;
1518	int		ipfe_size;
1519	int		ipfe_arg0[1];
1520} ipfexp_t;
1521
1522/*
1523 * Currently support commands (ipfe_cmd)
1524 * 32bits is split up follows:
1525 * aabbcccc
1526 * aa = 0 = packet matching, 1 = meta data matching
1527 * bb = IP protocol number
1528 * cccc = command
1529 */
1530#define	IPF_EXP_IP_PR		0x00000001
1531#define	IPF_EXP_IP_ADDR		0x00000002
1532#define	IPF_EXP_IP_SRCADDR	0x00000003
1533#define	IPF_EXP_IP_DSTADDR	0x00000004
1534#define	IPF_EXP_IP6_ADDR	0x00000005
1535#define	IPF_EXP_IP6_SRCADDR	0x00000006
1536#define	IPF_EXP_IP6_DSTADDR	0x00000007
1537#define	IPF_EXP_TCP_FLAGS	0x00060001
1538#define	IPF_EXP_TCP_PORT	0x00060002
1539#define	IPF_EXP_TCP_SPORT	0x00060003
1540#define	IPF_EXP_TCP_DPORT	0x00060004
1541#define	IPF_EXP_UDP_PORT	0x00110002
1542#define	IPF_EXP_UDP_SPORT	0x00110003
1543#define	IPF_EXP_UDP_DPORT	0x00110004
1544#define	IPF_EXP_IDLE_GT		0x01000001
1545#define	IPF_EXP_TCP_STATE	0x01060002
1546#define	IPF_EXP_END		0xffffffff
1547
1548#define	ONE_DAY			IPF_TTLVAL(1 * 86400)   /* 1 day */
1549#define	FIVE_DAYS		(5 * ONE_DAY)
1550
1551typedef struct ipf_main_softc_s {
1552	struct ipf_main_softc_s *ipf_next;
1553	ipfmutex_t	ipf_rw;
1554	ipfmutex_t      ipf_timeoutlock;
1555	ipfrwlock_t     ipf_mutex;
1556	ipfrwlock_t	ipf_frag;
1557	ipfrwlock_t	ipf_global;
1558	ipfrwlock_t	ipf_tokens;
1559	ipfrwlock_t	ipf_state;
1560	ipfrwlock_t	ipf_nat;
1561	ipfrwlock_t	ipf_natfrag;
1562	ipfrwlock_t	ipf_poolrw;
1563	int		ipf_dynamic_softc;
1564	int		ipf_refcnt;
1565	int		ipf_running;
1566	int		ipf_flags;
1567	int		ipf_active;
1568	int		ipf_control_forwarding;
1569	int		ipf_update_ipid;
1570	int		ipf_chksrc;	/* causes a system crash if enabled */
1571	int		ipf_pass;
1572	int		ipf_minttl;
1573	int		ipf_icmpminfragmtu;
1574	int		ipf_interror;	/* Should be in a struct that is per  */
1575					/* thread or process. Does not belong */
1576					/* here but there's a lot more work   */
1577					/* in doing that properly. For now,   */
1578					/* it is squatting. */
1579	u_int		ipf_tcpidletimeout;
1580	u_int		ipf_tcpclosewait;
1581	u_int		ipf_tcplastack;
1582	u_int		ipf_tcptimewait;
1583	u_int		ipf_tcptimeout;
1584	u_int		ipf_tcpsynsent;
1585	u_int		ipf_tcpsynrecv;
1586	u_int		ipf_tcpclosed;
1587	u_int		ipf_tcphalfclosed;
1588	u_int		ipf_udptimeout;
1589	u_int		ipf_udpacktimeout;
1590	u_int		ipf_icmptimeout;
1591	u_int		ipf_icmpacktimeout;
1592	u_int		ipf_iptimeout;
1593	u_long		ipf_ticks;
1594	u_long		ipf_userifqs;
1595	u_long		ipf_rb_no_mem;
1596	u_long		ipf_rb_node_max;
1597	u_long		ipf_frouteok[2];
1598	ipftuneable_t	*ipf_tuners;
1599	void		*ipf_frag_soft;
1600	void		*ipf_nat_soft;
1601	void		*ipf_state_soft;
1602	void		*ipf_auth_soft;
1603	void		*ipf_proxy_soft;
1604	void		*ipf_sync_soft;
1605	void		*ipf_lookup_soft;
1606	void		*ipf_log_soft;
1607	struct frgroup	*ipf_groups[IPL_LOGSIZE][2];
1608	frentry_t	*ipf_rules[2][2];
1609	frentry_t	*ipf_acct[2][2];
1610	frentry_t	*ipf_rule_explist[2];
1611	ipftoken_t	*ipf_token_head;
1612	ipftoken_t	**ipf_token_tail;
1613#if defined(__FreeBSD_version) && defined(_KERNEL)
1614	struct callout ipf_slow_ch;
1615#endif
1616#if NETBSD_GE_REV(104040000)
1617	struct callout	ipf_slow_ch;
1618#endif
1619#if SOLARIS
1620	timeout_id_t	ipf_slow_ch;
1621#endif
1622#if defined(_KERNEL)
1623# if SOLARIS
1624	struct pollhead	ipf_poll_head[IPL_LOGSIZE];
1625	void		*ipf_dip;
1626#  if defined(INSTANCES)
1627	int		ipf_get_loopback;
1628	u_long		ipf_idnum;
1629	net_handle_t	ipf_nd_v4;
1630	net_handle_t	ipf_nd_v6;
1631	hook_t		*ipf_hk_v4_in;
1632	hook_t		*ipf_hk_v4_out;
1633	hook_t		*ipf_hk_v4_nic;
1634	hook_t		*ipf_hk_v6_in;
1635	hook_t		*ipf_hk_v6_out;
1636	hook_t		*ipf_hk_v6_nic;
1637	hook_t		*ipf_hk_loop_v4_in;
1638	hook_t		*ipf_hk_loop_v4_out;
1639	hook_t		*ipf_hk_loop_v6_in;
1640	hook_t		*ipf_hk_loop_v6_out;
1641#  endif
1642# else
1643	struct selinfo	ipf_selwait[IPL_LOGSIZE];
1644# endif
1645#endif
1646	void		*ipf_slow;
1647	ipf_statistics_t ipf_stats[2];
1648	u_char		ipf_iss_secret[32];
1649	u_short		ipf_ip_id;
1650} ipf_main_softc_t;
1651
1652#define	IPFERROR(_e)	do { softc->ipf_interror = (_e); \
1653			     DT1(user_error, int, _e); \
1654			} while (0)
1655
1656#ifndef	_KERNEL
1657extern	int	ipf_check __P((void *, struct ip *, int, struct ifnet *, int, mb_t **));
1658extern	struct	ifnet *get_unit __P((char *, int));
1659extern	char	*get_ifname __P((struct ifnet *));
1660extern	int	ipfioctl __P((ipf_main_softc_t *, int, ioctlcmd_t,
1661			      caddr_t, int));
1662extern	void	m_freem __P((mb_t *));
1663extern	size_t	msgdsize __P((mb_t *));
1664extern	int	bcopywrap __P((void *, void *, size_t));
1665extern	void	ip_fillid(struct ip *);
1666#else /* #ifndef _KERNEL */
1667# if defined(__NetBSD__) && defined(PFIL_HOOKS)
1668extern	void	ipfilterattach __P((int));
1669# endif
1670extern	int	ipl_enable __P((void));
1671extern	int	ipl_disable __P((void));
1672# ifdef MENTAT
1673/* XXX MENTAT is always defined for Solaris */
1674extern	int	ipf_check __P((void *, struct ip *, int, struct ifnet *, int, void *,
1675			       mblk_t **));
1676#  if SOLARIS
1677extern	void	ipf_prependmbt(fr_info_t *, mblk_t *);
1678extern	int	ipfioctl __P((dev_t, int, intptr_t, int, cred_t *, int *));
1679#  endif
1680extern	int	ipf_qout __P((queue_t *, mblk_t *));
1681# else /* MENTAT */
1682/* XXX MENTAT is never defined for FreeBSD & NetBSD */
1683extern	int	ipf_check __P((void *, struct ip *, int, struct ifnet *, int, mb_t **));
1684extern	int	(*fr_checkp) __P((ip_t *, int, void *, int, mb_t **));
1685extern	size_t	mbufchainlen __P((mb_t *));
1686#   ifdef	IPFILTER_LKM
1687extern	int	ipf_identify __P((char *));
1688#   endif
1689#     if defined(__FreeBSD_version)
1690extern	int	ipfioctl __P((struct cdev*, u_long, caddr_t, int, struct thread *));
1691#     elif defined(__NetBSD__)
1692extern	int	ipfioctl __P((dev_t, u_long, void *, int, struct lwp *));
1693#     endif
1694# endif /* MENTAT */
1695
1696# if defined(__FreeBSD_version)
1697extern	int	ipf_pfil_hook __P((void));
1698extern	int	ipf_pfil_unhook __P((void));
1699extern	void	ipf_event_reg __P((void));
1700extern	void	ipf_event_dereg __P((void));
1701# endif
1702
1703# if defined(INSTANCES)
1704extern	ipf_main_softc_t	*ipf_find_softc __P((u_long));
1705extern	int	ipf_set_loopback __P((ipf_main_softc_t *, ipftuneable_t *,
1706				      ipftuneval_t *));
1707# endif
1708
1709#endif /* #ifndef _KERNEL */
1710
1711extern	char	*memstr __P((const char *, char *, size_t, size_t));
1712extern	int	count4bits __P((u_32_t));
1713#ifdef USE_INET6
1714extern	int	count6bits __P((u_32_t *));
1715#endif
1716extern	int	frrequest __P((ipf_main_softc_t *, int, ioctlcmd_t, caddr_t,
1717			       int, int));
1718extern	char	*getifname __P((struct ifnet *));
1719extern	int	ipfattach __P((ipf_main_softc_t *));
1720extern	int	ipfdetach __P((ipf_main_softc_t *));
1721extern	u_short	ipf_cksum __P((u_short *, int));
1722extern	int	copyinptr __P((ipf_main_softc_t *, void *, void *, size_t));
1723extern	int	copyoutptr __P((ipf_main_softc_t *, void *, void *, size_t));
1724extern	int	ipf_fastroute __P((mb_t *, mb_t **, fr_info_t *, frdest_t *));
1725extern	int	ipf_inject __P((fr_info_t *, mb_t *));
1726extern	int	ipf_inobj __P((ipf_main_softc_t *, void *, ipfobj_t *,
1727			       void *, int));
1728extern	int	ipf_inobjsz __P((ipf_main_softc_t *, void *, void *,
1729				 int , int));
1730extern	int	ipf_ioctlswitch __P((ipf_main_softc_t *, int, void *,
1731				     ioctlcmd_t, int, int, void *));
1732extern	int	ipf_ipf_ioctl __P((ipf_main_softc_t *, caddr_t, ioctlcmd_t,
1733				   int, int, void *));
1734extern	int	ipf_ipftune __P((ipf_main_softc_t *, ioctlcmd_t, void *));
1735extern	int	ipf_matcharray_load __P((ipf_main_softc_t *, caddr_t,
1736					 ipfobj_t *, int **));
1737extern	int	ipf_matcharray_verify __P((int *, int));
1738extern	int	ipf_outobj __P((ipf_main_softc_t *, void *, void *, int));
1739extern	int	ipf_outobjk __P((ipf_main_softc_t *, ipfobj_t *, void *));
1740extern	int	ipf_outobjsz __P((ipf_main_softc_t *, void *, void *,
1741				  int, int));
1742extern	void	*ipf_pullup __P((mb_t *, fr_info_t *, int));
1743extern	int	ipf_resolvedest __P((ipf_main_softc_t *, char *,
1744				     struct frdest *, int));
1745extern	int	ipf_resolvefunc __P((ipf_main_softc_t *, void *));
1746extern	void	*ipf_resolvenic __P((ipf_main_softc_t *, char *, int));
1747extern	int	ipf_send_icmp_err __P((int, fr_info_t *, int));
1748extern	int	ipf_send_reset __P((fr_info_t *));
1749extern	void	ipf_apply_timeout __P((ipftq_t *, u_int));
1750extern	ipftq_t	*ipf_addtimeoutqueue __P((ipf_main_softc_t *, ipftq_t **,
1751					  u_int));
1752extern	void	ipf_deletequeueentry __P((ipftqent_t *));
1753extern	int	ipf_deletetimeoutqueue __P((ipftq_t *));
1754extern	void	ipf_freetimeoutqueue __P((ipf_main_softc_t *, ipftq_t *));
1755extern	void	ipf_movequeue __P((u_long, ipftqent_t *, ipftq_t *,
1756				   ipftq_t *));
1757extern	void	ipf_queueappend __P((u_long, ipftqent_t *, ipftq_t *, void *));
1758extern	void	ipf_queueback __P((u_long, ipftqent_t *));
1759extern	int	ipf_queueflush __P((ipf_main_softc_t *, ipftq_delete_fn_t,
1760				    ipftq_t *, ipftq_t *, u_int *, int, int));
1761extern	void	ipf_queuefront __P((ipftqent_t *));
1762extern	int	ipf_settimeout_tcp __P((ipftuneable_t *, ipftuneval_t *,
1763					ipftq_t *));
1764extern	int	ipf_checkv4sum __P((fr_info_t *));
1765extern	int	ipf_checkl4sum __P((fr_info_t *));
1766extern	int	ipf_ifpfillv4addr __P((int, struct sockaddr_in *,
1767				      struct sockaddr_in *, struct in_addr *,
1768				      struct in_addr *));
1769extern	int	ipf_coalesce __P((fr_info_t *));
1770#ifdef	USE_INET6
1771extern	int	ipf_checkv6sum __P((fr_info_t *));
1772extern	int	ipf_ifpfillv6addr __P((int, struct sockaddr_in6 *,
1773				      struct sockaddr_in6 *, i6addr_t *,
1774				      i6addr_t *));
1775#endif
1776
1777extern	int	ipf_tune_add __P((ipf_main_softc_t *, ipftuneable_t *));
1778extern	int	ipf_tune_add_array __P((ipf_main_softc_t *, ipftuneable_t *));
1779extern	int	ipf_tune_del __P((ipf_main_softc_t *, ipftuneable_t *));
1780extern	int	ipf_tune_del_array __P((ipf_main_softc_t *, ipftuneable_t *));
1781extern	int	ipf_tune_array_link __P((ipf_main_softc_t *, ipftuneable_t *));
1782extern	int	ipf_tune_array_unlink __P((ipf_main_softc_t *,
1783					   ipftuneable_t *));
1784extern	ipftuneable_t *ipf_tune_array_copy __P((void *, size_t,
1785						ipftuneable_t *));
1786
1787extern int	ipf_pr_pullup __P((fr_info_t *, int));
1788
1789extern	int	ipf_flush __P((ipf_main_softc_t *, minor_t, int));
1790extern	frgroup_t *ipf_group_add __P((ipf_main_softc_t *, char *, void *,
1791				      u_32_t, minor_t, int));
1792extern	void	ipf_group_del __P((ipf_main_softc_t *, frgroup_t *,
1793				   frentry_t *));
1794extern	int	ipf_derefrule __P((ipf_main_softc_t *, frentry_t **));
1795extern	frgroup_t *ipf_findgroup __P((ipf_main_softc_t *, char *, minor_t,
1796				      int, frgroup_t ***));
1797
1798extern	int	ipf_log_init __P((void));
1799extern	int	ipf_log_bytesused __P((ipf_main_softc_t *, int));
1800extern	int	ipf_log_canread __P((ipf_main_softc_t *, int));
1801extern	int	ipf_log_clear __P((ipf_main_softc_t *, minor_t));
1802extern	u_long  ipf_log_failures __P((ipf_main_softc_t *, int));
1803extern	int	ipf_log_read __P((ipf_main_softc_t *, minor_t, uio_t *));
1804extern	int	ipf_log_items __P((ipf_main_softc_t *, int, fr_info_t *,
1805				   void **, size_t *, int *, int));
1806extern	u_long  ipf_log_logok __P((ipf_main_softc_t *, int));
1807extern	void	ipf_log_unload __P((ipf_main_softc_t *));
1808extern	int 	ipf_log_pkt __P((fr_info_t *, u_int));
1809
1810extern	frentry_t	*ipf_acctpkt __P((fr_info_t *, u_32_t *));
1811extern	u_short		fr_cksum __P((fr_info_t *, ip_t *, int, void *));
1812extern	void		ipf_deinitialise __P((ipf_main_softc_t *));
1813extern	int		ipf_deliverlocal __P((ipf_main_softc_t *, int, void *,
1814					      i6addr_t *));
1815extern	frentry_t 	*ipf_dstgrpmap __P((fr_info_t *, u_32_t *));
1816extern	void		ipf_fixskip __P((frentry_t **, frentry_t *, int));
1817extern	void		ipf_forgetifp __P((ipf_main_softc_t *, void *));
1818extern	frentry_t 	*ipf_getrulen __P((ipf_main_softc_t *, int, char *,
1819					   u_32_t));
1820extern	int		ipf_ifpaddr __P((ipf_main_softc_t *, int, int, void *,
1821					i6addr_t *, i6addr_t *));
1822extern	void		ipf_inet_mask_add __P((int, ipf_v4_masktab_t *));
1823extern	void		ipf_inet_mask_del __P((int, ipf_v4_masktab_t *));
1824#ifdef	USE_INET6
1825extern	void		ipf_inet6_mask_add __P((int, i6addr_t *,
1826						ipf_v6_masktab_t *));
1827extern	void		ipf_inet6_mask_del __P((int, i6addr_t *,
1828						ipf_v6_masktab_t *));
1829#endif
1830extern	int		ipf_initialise __P((void));
1831extern	int		ipf_lock __P((caddr_t, int *));
1832extern  int		ipf_makefrip __P((int, ip_t *, fr_info_t *));
1833extern	int		ipf_matchtag __P((ipftag_t *, ipftag_t *));
1834extern	int		ipf_matchicmpqueryreply __P((int, icmpinfo_t *,
1835						     struct icmp *, int));
1836extern	u_32_t		ipf_newisn __P((fr_info_t *));
1837extern	u_int		ipf_pcksum __P((fr_info_t *, int, u_int));
1838extern	void		ipf_rule_expire __P((ipf_main_softc_t *));
1839extern	int		ipf_scanlist __P((fr_info_t *, u_32_t));
1840extern	frentry_t 	*ipf_srcgrpmap __P((fr_info_t *, u_32_t *));
1841extern	int		ipf_tcpudpchk __P((fr_ip_t *, frtuc_t *));
1842extern	int		ipf_verifysrc __P((fr_info_t *fin));
1843extern	int		ipf_zerostats __P((ipf_main_softc_t *, char *));
1844extern	int		ipf_getnextrule __P((ipf_main_softc_t *, ipftoken_t *,
1845					     void *));
1846extern	int		ipf_sync __P((ipf_main_softc_t *, void *));
1847extern	int		ipf_token_deref __P((ipf_main_softc_t *, ipftoken_t *));
1848extern	void		ipf_token_expire __P((ipf_main_softc_t *));
1849extern	ipftoken_t	*ipf_token_find __P((ipf_main_softc_t *, int, int,
1850					    void *));
1851extern	int		ipf_token_del __P((ipf_main_softc_t *, int, int,
1852					  void *));
1853extern	void		ipf_token_mark_complete __P((ipftoken_t *));
1854extern	int		ipf_genericiter __P((ipf_main_softc_t *, void *,
1855					     int, void *));
1856#ifdef	IPFILTER_LOOKUP
1857extern	void		*ipf_resolvelookup __P((int, u_int, u_int,
1858						lookupfunc_t *));
1859#endif
1860extern	u_32_t		ipf_random __P((void));
1861
1862extern	int		ipf_main_load __P((void));
1863extern	void		*ipf_main_soft_create __P((void *));
1864extern	void		ipf_main_soft_destroy __P((ipf_main_softc_t *));
1865extern	int		ipf_main_soft_init __P((ipf_main_softc_t *));
1866extern	int		ipf_main_soft_fini __P((ipf_main_softc_t *));
1867extern	int		ipf_main_unload __P((void));
1868extern	int		ipf_load_all __P((void));
1869extern	int		ipf_unload_all __P((void));
1870extern	void		ipf_destroy_all __P((ipf_main_softc_t *));
1871extern	ipf_main_softc_t *ipf_create_all __P((void *));
1872extern	int		ipf_init_all __P((ipf_main_softc_t *));
1873extern	int		ipf_fini_all __P((ipf_main_softc_t *));
1874extern	void		ipf_log_soft_destroy __P((ipf_main_softc_t *, void *));
1875extern	void		*ipf_log_soft_create __P((ipf_main_softc_t *));
1876extern	int		ipf_log_soft_init __P((ipf_main_softc_t *, void *));
1877extern	int		ipf_log_soft_fini __P((ipf_main_softc_t *, void *));
1878extern	int		ipf_log_main_load __P((void));
1879extern	int		ipf_log_main_unload __P((void));
1880
1881
1882extern	char	ipfilter_version[];
1883#ifdef	USE_INET6
1884extern	int	icmptoicmp6types[ICMP_MAXTYPE+1];
1885extern	int	icmptoicmp6unreach[ICMP_MAX_UNREACH];
1886extern	int	icmpreplytype6[ICMP6_MAXTYPE + 1];
1887#endif
1888#ifdef	IPFILTER_COMPAT
1889extern	int	ipf_in_compat __P((ipf_main_softc_t *, ipfobj_t *, void *,int));
1890extern	int	ipf_out_compat __P((ipf_main_softc_t *, ipfobj_t *, void *));
1891#endif
1892extern	int	icmpreplytype4[ICMP_MAXTYPE + 1];
1893
1894extern	int	ipf_ht_node_add __P((ipf_main_softc_t *, host_track_t *,
1895				     int, i6addr_t *));
1896extern	int	ipf_ht_node_del __P((host_track_t *, int, i6addr_t *));
1897extern	void	ipf_rb_ht_flush __P((host_track_t *));
1898extern	void	ipf_rb_ht_freenode __P((host_node_t *, void *));
1899extern	void	ipf_rb_ht_init __P((host_track_t *));
1900
1901#endif	/* __IP_FIL_H__ */
1902