1 /*	$NetBSD: ip_compat.h,v 1.10 2016/06/10 13:31:44 ozaki-r Exp $	*/
2 
3 /*
4  * Copyright (C) 2012 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  * @(#)ip_compat.h	1.8 1/14/96
9  * Id: ip_compat.h,v 1.1.1.2 2012/07/22 13:45:09 darrenr Exp
10  */
11 
12 #ifndef _NETINET_IP_COMPAT_H_
13 #define _NETINET_IP_COMPAT_H_
14 
15 #ifndef	__STDC__
16 # undef		const
17 # define	const
18 #endif
19 
20 #if defined(_KERNEL) || defined(KERNEL) || defined(__KERNEL__)
21 # undef	KERNEL
22 # undef	_KERNEL
23 # undef 	__KERNEL__
24 # define	KERNEL
25 # define	_KERNEL
26 # define 	__KERNEL__
27 #endif
28 
29 #ifndef	SOLARIS
30 # if (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
31 #  define SOLARIS	1
32 # else
33 #  define SOLARIS	0
34 # endif
35 #endif
36 #if (defined(SOLARIS2) && (SOLARIS2 >= 8))
37 # ifndef	USE_INET6
38 #  define	USE_INET6
39 # endif
40 #endif
41 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 400000) && \
42     !defined(_KERNEL) && !defined(USE_INET6) && !defined(NOINET6)
43 # define	USE_INET6
44 #endif
45 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105000000) && \
46     !defined(_KERNEL) && !defined(USE_INET6) && !defined(NOINET6)
47 # define	USE_INET6
48 #endif
49 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 106140000) && \
50     defined(_KERNEL) && \
51     (!defined(IPFILTER_LKM) || (__NetBSD_Version__ >= 399000100))
52 # define	IPFILTER_M_IPFILTER
53 #endif
54 #if !defined(USE_INET6)
55 # if defined(OpenBSD) && (OpenBSD >= 200206) && \
56     !defined(_KERNEL) && !defined(USE_INET6)
57 #  define	USE_INET6
58 # endif
59 # if defined(__osf__)
60 #  define	USE_INET6	1
61 # endif
62 # if defined(linux) && (!defined(_KERNEL) || defined(CONFIG_IPV6))
63 #  define	USE_INET6
64 # endif
65 # if defined(HPUXREV) && (HPUXREV >= 1111)
66 #  define	USE_INET6
67 # endif
68 #endif
69 
70 #if defined(BSD) && (BSD < 199103) && defined(__osf__)
71 # undef BSD
72 # define BSD 199103
73 #endif
74 
75 #if defined(__SVR4) || defined(__svr4__) || defined(__sgi)
76 # define index   strchr
77 # if !defined(_KERNEL)
78 #  define	bzero(a,b)	memset(a,0,b)
79 #  define	bcmp		memcmp
80 #  define	bcopy(a,b,c)	memmove(b,a,c)
81 # endif
82 #endif
83 
84 #ifndef LIFNAMSIZ
85 # ifdef IF_NAMESIZE
86 #  define	LIFNAMSIZ	IF_NAMESIZE
87 # else
88 #  ifdef	IFNAMSIZ
89 #   define	LIFNAMSIZ	IFNAMSIZ
90 #  else
91 #   define	LIFNAMSIZ	16
92 #  endif
93 # endif
94 #endif
95 
96 #if defined(__sgi) || defined(bsdi) || defined(__hpux) || defined(hpux)
97 struct  ether_addr {
98         u_char  ether_addr_octet[6];
99 };
100 #endif
101 
102 #if defined(__sgi) && !defined(IPFILTER_LKM)
103 # ifdef __STDC__
104 #  define IPL_EXTERN(ep) ipfilter##ep
105 # else
106 #  define IPL_EXTERN(ep) ipfilter/**/ep
107 # endif
108 #else
109 # ifdef __STDC__
110 #  define IPL_EXTERN(ep) ipl##ep
111 # else
112 #  define IPL_EXTERN(ep) ipl/**/ep
113 # endif
114 #endif
115 
116 /*
117  * This is a workaround for <sys/uio.h> troubles on FreeBSD and OpenBSD.
118  */
119 #ifndef linux
120 # ifndef _KERNEL
121 #  define ADD_KERNEL
122 #  define _KERNEL
123 #  define KERNEL
124 # endif
125 # ifdef __OpenBSD__
126 struct file;
127 # endif
128 # include <sys/uio.h>
129 # ifdef ADD_KERNEL
130 #  undef _KERNEL
131 #  undef KERNEL
132 # endif
133 #endif
134 
135 #if defined(__NetBSD_Version__)
136 # define NETBSD_GE_REV(x)	(__NetBSD_Version__ >= (x))
137 # define NETBSD_GT_REV(x)	(__NetBSD_Version__ > (x))
138 # define NETBSD_LT_REV(x)	(__NetBSD_Version__ < (x))
139 #else
140 # define NETBSD_GE_REV(x)	0
141 # define NETBSD_GT_REV(x)	0
142 # define NETBSD_LT_REV(x)	0
143 #endif
144 
145 #if defined(__FreeBSD_version)
146 # define FREEBSD_GE_REV(x)	(__FreeBSD_version >= (x))
147 # define FREEBSD_GT_REV(x)	(__FreeBSD_version > (x))
148 # define FREEBSD_LT_REV(x)	(__FreeBSD_version < (x))
149 #else
150 # define FREEBSD_GE_REV(x)	0
151 # define FREEBSD_GT_REV(x)	0
152 # define FREEBSD_LT_REV(x)	0
153 #endif
154 
155 #if defined(_BSDI_VERSION)
156 # define BSDOS_GE_REV(x)	(_BSDI_VERSION >= (x))
157 # define BSDOS_GT_REV(x)	(_BSDI_VERSION > (x))
158 # define BSDOS_LT_REV(x)	(_BSDI_VERSION < (x))
159 #else
160 # define BSDOS_GE_REV(x)	0
161 # define BSDOS_GT_REV(x)	0
162 # define BSDOS_LT_REV(x)	0
163 #endif
164 
165 #if defined(OpenBSD)
166 # define OPENBSD_GE_REV(x)	(OpenBSD >= (x))
167 # define OPENBSD_GT_REV(x)	(OpenBSD > (x))
168 # define OPENBSD_LT_REV(x)	(OpenBSD < (x))
169 #else
170 # define OPENBSD_GE_REV(x)	0
171 # define OPENBSD_GT_REV(x)	0
172 # define OPENBSD_LT_REV(x)	0
173 #endif
174 
175 #if defined(BSD)
176 # define BSD_GE_YEAR(x)		(BSD >= (x))
177 # define BSD_GT_YEAR(x)		(BSD > (x))
178 # define BSD_LT_YEAR(x)		(BSD < (x))
179 #else
180 # define BSD_GE_YEAR(x)		0
181 # define BSD_GT_YEAR(x)		0
182 # define BSD_LT_YEAR(x)		0
183 #endif
184 
185 
186 /* ----------------------------------------------------------------------- */
187 /*                                  S O L A R I S                          */
188 /* ----------------------------------------------------------------------- */
189 #if SOLARIS
190 # define	MENTAT	1
191 # include	<sys/cmn_err.h>
192 # include	<sys/isa_defs.h>
193 # include	<sys/stream.h>
194 # include	<sys/ioccom.h>
195 # include	<sys/sysmacros.h>
196 # include	<sys/kmem.h>
197 # if SOLARIS2 >= 10
198 #  include	<sys/procset.h>
199 #  include	<sys/proc.h>
200 #  include	<sys/devops.h>
201 #  include	<sys/ddi_impldefs.h>
202 #  include	<sys/sdt.h>
203 # endif
204 /*
205  * because Solaris 2 defines these in two places :-/
206  */
207 # ifndef	_KERNEL
208 #  define	ADD_KERNEL
209 #  define	_KERNEL
210 #  undef	RES_INIT
211 # endif /* _KERNEL */
212 
213 # if SOLARIS2 >= 8
214 #  include <netinet/ip6.h>
215 #  include <netinet/icmp6.h>
216 # endif
217 
218 # include <inet/common.h>
219 /* These 5 are defined in <inet/ip.h> and <netinet/ip.h> */
220 # undef	IPOPT_EOL
221 # undef	IPOPT_NOP
222 # undef	IPOPT_LSRR
223 # undef	IPOPT_RR
224 # undef	IPOPT_SSRR
225 # ifdef i386
226 #  define _SYS_PROMIF_H
227 # endif
228 # ifdef	ADD_KERNEL
229 #  undef	_KERNEL
230 # endif
231 # include <inet/mib2.h>
232 # include <inet/ip.h>
233 # undef COPYOUT
234 # if !defined(_SYS_NETI_H)
235 #  include <inet/ip_ire.h>
236 # endif
237 # if SOLARIS2 >= 8
238 
239 #  include <inet/ip_if.h>
240 #  define	ipif_local_addr	ipif_lcl_addr
241 /* Only defined in private include file */
242 #  ifndef	V4_PART_OF_V6
243 #   define	V4_PART_OF_V6(v6)	v6.s6_addr32[3]
244 #  endif
245 struct ip6_ext {
246 	u_char	ip6e_nxt;
247 	u_char	ip6e_len;
248 };
249 # endif /* SOLARIS2 >= 8 */
250 
251 # if SOLARIS2 >= 6
252 #  include <sys/atomic.h>
253 typedef	uint8_t		u_int8_t;
254 typedef	uint32_t	u_32_t;
255 # else
256 typedef	unsigned char	u_int8_t;
257 typedef unsigned int	u_32_t;
258 # endif
259 # define	U_32_T	1
260 # if SOLARIS2 >= 7
261 #  define	USE_QUAD_T	1
262 #  define	U_QUAD_T	uint64_t
263 #  define	QUAD_T		int64_t
264 # endif
265 
266 # ifdef _KERNEL
267 #  define	NEED_LOCAL_RAND		1
268 #  define	KRWLOCK_T		krwlock_t
269 #  define	KMUTEX_T		kmutex_t
270 #  if !defined(FW_HOOKS)
271 #   include "qif.h"
272 #   include "pfil.h"
273 #  endif
274 #  if SOLARIS2 >= 6
275 #   if SOLARIS2 == 6
276 #    define	ATOMIC_INCL(x)		atomic_add_long((uint32_t*)&(x), 1)
277 #    define	ATOMIC_DECL(x)		atomic_add_long((uint32_t*)&(x), -1)
278 #   else
279 #    define	ATOMIC_INCL(x)		atomic_add_long(&(x), 1)
280 #    define	ATOMIC_DECL(x)		atomic_add_long(&(x), -1)
281 #   endif /* SOLARIS2 == 6 */
282 #   define	ATOMIC_INC64(x)		atomic_add_64((uint64_t*)&(x), 1)
283 #   define	ATOMIC_INC32(x)		atomic_add_32((uint32_t*)&(x), 1)
284 #   define	ATOMIC_DEC64(x)		atomic_add_64((uint64_t*)&(x), -1)
285 #   define	ATOMIC_DEC32(x)		atomic_add_32((uint32_t*)&(x), -1)
286 #  else
287 #   define	ATOMIC_INC(x)		{ mutex_enter(&softc->ipf_rw); (x)++; \
288 					  mutex_exit(&softc->ipf_rw); }
289 #   define	ATOMIC_DEC(x)		{ mutex_enter(&softc->ipf_rw); (x)--; \
290 					  mutex_exit(&softc->ipf_rw); }
291 #  endif /* SOLARIS2 >= 6 */
292 #  define	USE_MUTEXES
293 #  define	MUTEX_ENTER(x)		mutex_enter(&(x)->ipf_lk)
294 #  define	READ_ENTER(x)		rw_enter(&(x)->ipf_lk, RW_READER)
295 #  define	WRITE_ENTER(x)		rw_enter(&(x)->ipf_lk, RW_WRITER)
296 #  define	MUTEX_DOWNGRADE(x)	rw_downgrade(&(x)->ipf_lk)
297 #  define	RWLOCK_INIT(x, y)	rw_init(&(x)->ipf_lk, (y),  \
298 						RW_DRIVER, NULL)
299 #  define	RWLOCK_EXIT(x)		rw_exit(&(x)->ipf_lk)
300 #  define	RW_DESTROY(x)		rw_destroy(&(x)->ipf_lk)
301 #  define	MUTEX_INIT(x, y)	mutex_init(&(x)->ipf_lk, (y), \
302 						   MUTEX_DRIVER, NULL)
303 #  define	MUTEX_DESTROY(x)	mutex_destroy(&(x)->ipf_lk)
304 #  define	MUTEX_NUKE(x)		bzero((x), sizeof(*(x)))
305 #  define	MUTEX_EXIT(x)		mutex_exit(&(x)->ipf_lk)
306 #  define	COPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
307 #  define	COPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
308 #  define	BCOPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
309 #  define	BCOPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
310 #  define	UIOMOVE(a,b,c,d)	uiomove((caddr_t)a,b,c,d)
311 #  define	KFREE(x)	kmem_free((char *)(x), sizeof(*(x)))
312 #  define	KFREES(x,s)	kmem_free((char *)(x), (s))
313 #  define	SPL_SCHED(x)	;
314 #  define	SPL_NET(x)	;
315 #  define	SPL_IMP(x)	;
316 #  undef	SPL_X
317 #  define	SPL_X(x)	;
318 #  ifdef sparc
319 #   define	ntohs(x)	(x)
320 #   define	ntohl(x)	(x)
321 #   define	htons(x)	(x)
322 #   define	htonl(x)	(x)
323 #  endif /* sparc */
324 #  define	KMALLOC(a,b)	(a) = (b)kmem_alloc(sizeof(*(a)), KM_NOSLEEP)
325 #  define	KMALLOCS(a,b,c)	(a) = (b)kmem_alloc((c), KM_NOSLEEP)
326 #  define	GET_MINOR(x)	getminor(x)
327 extern	void	*get_unit(void *, char *, int);
328 #  define	GETIFP(n, v)	get_unit(softc, n, v)
329 #  if defined(INSTANCES)
330 #   include	<sys/hook.h>
331 #   include	<sys/neti.h>
332 typedef struct	qpktinfo	{
333 	void		*qpi_real;	/* the real one on the STREAM */
334 	void		*qpi_ill;	/* COPIED */
335 	mblk_t		*qpi_m;
336 	queue_t		*qpi_q;
337 	void		*qpi_data;	/* where layer 3 header starts */
338 	size_t		qpi_off;
339 	int		qpi_flags;	/* COPIED */
340 } qpktinfo_t;
341 
342 #define	QF_MULTICAST	0x0001
343 #define	QF_BROADCAST	0x0002
344 
345 typedef struct qifpkt {
346 	struct qifpkt	*qp_next;
347 	char		qp_ifname[LIFNAMSIZ];
348 	int		qp_sap;
349 	mblk_t		*qp_mb;
350 	int		qp_inout;
351 } qifpkt_t;
352 
353 #   define	COPYIFNAME(v, x,b)					\
354 			do {						\
355 				if ((v) == 4) {				\
356 					net_getifname(softc->ipf_nd_v4,	\
357 						      (phy_if_t)x, b,	\
358 						      sizeof(b));	\
359 				} else {				\
360 					net_getifname(softc->ipf_nd_v6,	\
361 						      (phy_if_t)x, b,	\
362 						      sizeof(b));	\
363 				}					\
364 			} while (0)
365 #   define	GETIFMTU_4(x)	net_getmtu(softc->ipf_nd_v4, (phy_if_t)x, 0)
366 #   define	GETIFMTU_6(x)	net_getmtu(softc->ipf_nd_v6, (phy_if_t)x, 0)
367 #   define	GET_SOFTC(x)	ipf_find_softc(x)
368 #  else
369 #   define	FASTROUTE_RECURSION	1
370 #   define	GET_SOFTC(x)	&ipfmain
371 #   define	GETIFMTU_4(x)	((qif_t *)x)->qf_max_frag
372 #   define	GETIFMTU_6(x)	((qif_t *)x)->qf_max_frag
373 #   define	IFNAME(x)	((qif_t *)x)->qf_name
374 #   define	COPYIFNAME(v, x, b) \
375 				(void) strncpy(b, ((qif_t *)x)->qf_name, \
376 					       LIFNAMSIZ)
377 #  endif
378 #  define	GETKTIME(x)	uniqtime((struct timeval *)x)
379 #  define	MSGDSIZE(m)	msgdsize(m)
380 #  define	M_LEN(m)	((m)->b_wptr - (m)->b_rptr)
381 #  define	M_ADJ(m,x)	adjmsg(m, x)
382 #  define	M_COPY(x)	dupmsg((x))
383 #  define	MTOD(m,t)	((t)((m)->b_rptr))
384 #  define	MTYPE(m)	((m)->b_datap->db_type)
385 #  define	FREE_MB_T(m)	freemsg(m)
386 #  define	ALLOC_MB_T(m,l)	(m) = allocmbt(l)
387 #  define	PREP_MB_T(f,m)	ipf_prependmbt(f, m)
388 #  define	M_DUP(m)	copymsg(m)
389 #  define	m_next		b_cont
390 #  define	IPF_PANIC(x,y)	if (x) { printf y; cmn_err(CE_PANIC, "ipf_panic"); }
391 typedef mblk_t mb_t;
392 extern	void	mb_copydata __P((mblk_t *, size_t, size_t, char *));
393 extern	void	mb_copyback __P((mblk_t *, size_t, size_t, char *));
394 # endif /* _KERNEL */
395 
396 # if (SOLARIS2 >= 7)
397 #  ifdef lint
398 #   define ALIGN32(ptr)    (ptr ? 0L : 0L)
399 #   define ALIGN16(ptr)    (ptr ? 0L : 0L)
400 #  else
401 #   define ALIGN32(ptr)    (ptr)
402 #   define ALIGN16(ptr)    (ptr)
403 #  endif
404 # endif
405 
406 # if SOLARIS2 < 6
407 typedef	struct uio	uio_t;
408 # endif
409 typedef	int		ioctlcmd_t;
410 
411 # define OS_RECOGNISED 1
412 
413 #endif /* SOLARIS */
414 
415 /* ----------------------------------------------------------------------- */
416 /*                                  H P U X                                */
417 /* ----------------------------------------------------------------------- */
418 #ifdef __hpux
419 # define	MENTAT	1
420 # include	<sys/sysmacros.h>
421 # include	<sys/spinlock.h>
422 # include	<sys/lock.h>
423 # include	<sys/stream.h>
424 # ifdef USE_INET6
425 #  include	<netinet/if_ether.h>
426 #  include	<netinet/ip6.h>
427 #  include	<netinet/icmp6.h>
428 typedef	struct	ip6_hdr	ip6_t;
429 # endif
430 
431 # ifdef _KERNEL
432 #  define	FASTROUTE_RECURSION	1
433 #  if (HPUXREV >= 1111)
434 #   define	IPL_SELECT
435 #   ifdef	IPL_SELECT
436 #    include	<machine/sys/user.h>
437 #    include	<sys/kthread_iface.h>
438 #    define	READ_COLLISION	0x01
439 
440 typedef	struct	iplog_select_s {
441 	kthread_t	*read_waiter;
442 	int		state;
443 } iplog_select_t;
444 #   endif
445 #  endif
446 
447 #  define	GETKTIME(x)	uniqtime((struct timeval *)x)
448 
449 #  if HPUXREV == 1111
450 #   include	"kern_svcs.h"
451 #  else
452 #   include	<sys/kern_svcs.h>
453 #  endif
454 #  undef	ti_flags
455 #  undef	TCP_NODELAY
456 #  undef	TCP_MAXSEG
457 #  include <sys/reg.h>
458 #  include "../netinet/ip_info.h"
459 /*
460  * According to /usr/include/sys/spinlock.h on HP-UX 11.00, these functions
461  * are available.  Attempting to use them actually results in unresolved
462  * symbols when it comes time to load the module.
463  * This has been fixed!  Yipee!
464  */
465 #  if 1
466 #   ifdef __LP64__
467 #    define	ATOMIC_INCL(x)		lock_and_incr_int64(&softc->ipf_rw.ipf_lk, &(x), 1)
468 #    define	ATOMIC_DECL(x)		lock_and_incr_int64(&softc->ipf_rw.ipf_lk, &(x), -1)
469 #   else
470 #    define	ATOMIC_INCL(x)		lock_and_incr_int32(&softc->ipf_rw.ipf_lk, &(x), 1)
471 #    define	ATOMIC_DECL(x)		lock_and_incr_int32(&softc->ipf_rw.ipf_lk, &(x), -1)
472 #   endif
473 #   define	ATOMIC_INC64(x)		lock_and_incr_int64(&softc->ipf_rw.ipf_lk, &(x), 1)
474 #   define	ATOMIC_INC32(x)		lock_and_incr_int32(&softc->ipf_rw.ipf_lk, &(x), 1)
475 #   define	ATOMIC_DEC64(x)		lock_and_incr_int64(&softc->ipf_rw.ipf_lk, &(x), -1)
476 #   define	ATOMIC_DEC32(x)		lock_and_incr_int32(&softc->ipf_rw.ipf_lk, &(x), -1)
477 #  else /* 0 */
478 #   define	ATOMIC_INC64(x)		{ MUTEX_ENTER(&softc->ipf_rw); (x)++; \
479 					  MUTEX_EXIT(&softc->ipf_rw); }
480 #   define	ATOMIC_DEC64(x)		{ MUTEX_ENTER(&softc->ipf_rw); (x)--; \
481 					  MUTEX_EXIT(&softc->ipf_rw); }
482 #   define	ATOMIC_INC32(x)		{ MUTEX_ENTER(&softc->ipf_rw); (x)++; \
483 					  MUTEX_EXIT(&softc->ipf_rw); }
484 #   define	ATOMIC_DEC32(x)		{ MUTEX_ENTER(&softc->ipf_rw); (x)--; \
485 					  MUTEX_EXIT(&softc->ipf_rw); }
486 #   define	ATOMIC_INCL(x)		{ MUTEX_ENTER(&softc->ipf_rw); (x)++; \
487 					  MUTEX_EXIT(&softc->ipf_rw); }
488 #   define	ATOMIC_DECL(x)		{ MUTEX_ENTER(&softc->ipf_rw); (x)--; \
489 					  MUTEX_EXIT(&softc->ipf_rw); }
490 #   define	ATOMIC_INC(x)		{ MUTEX_ENTER(&softc->ipf_rw); (x)++; \
491 					  MUTEX_EXIT(&softc->ipf_rw); }
492 #   define	ATOMIC_DEC(x)		{ MUTEX_ENTER(&softc->ipf_rw); (x)--; \
493 					  MUTEX_EXIT(&softc->ipf_rw); }
494 #  endif
495 #  define	ip_cksum		ip_csuma
496 #  define	memcpy(a,b,c)		bcopy((void *)b, (void *)a, c)
497 #  define	USE_MUTEXES
498 #  define	MUTEX_INIT(x, y)	initlock(&(x)->ipf_lk, 0, 0, (y))
499 #  define	MUTEX_ENTER(x)		spinlock(&(x)->ipf_lk)
500 #  define	MUTEX_EXIT(x)		spinunlock(&(x)->ipf_lk);
501 #  define	MUTEX_DESTROY(x)
502 #  define	MUTEX_NUKE(x)		bzero((char *)(x), sizeof(*(x)))
503 #  define	KMUTEX_T		lock_t
504 #  define	kmutex_t		lock_t		/* for pfil.h */
505 #  define	krwlock_t		lock_t		/* for pfil.h */
506 /*
507  * The read-write lock implementation in HP-UX 11.0 is crippled - it can
508  * only be used by threads working in a user context!
509  * This has been fixed!  Yipee! (Or at least it does in 11.00, not 11.11..)
510  */
511 #  if HPUXREV < 1111
512 #   define	MUTEX_DOWNGRADE(x)	lock_write_to_read(x)
513 #   define	KRWLOCK_T		struct rw_lock
514 #   define	READ_ENTER(x)		lock_read(&(x)->ipf_lk)
515 #   define	WRITE_ENTER(x)		lock_write(&(x)->ipf_lk)
516 #   if HPUXREV >= 1111
517 #    define	RWLOCK_INIT(x, y)	rwlock_init4(&(x)->ipf_lk, 0, RWLCK_CANSLEEP, 0, y)
518 #   else
519 #    define	RWLOCK_INIT(x, y)	lock_init3(&(x)->ipf_lk, 0, 1, 0, 0, y)
520 #   endif
521 #   define	RWLOCK_EXIT(x)		lock_done(&(x)->ipf_lk)
522 #  else
523 #   define	KRWLOCK_T		lock_t
524 #   define	KMUTEX_T		lock_t
525 #   define	READ_ENTER(x)		MUTEX_ENTER(x)
526 #   define	WRITE_ENTER(x)		MUTEX_ENTER(x)
527 #   define	MUTEX_DOWNGRADE(x)
528 #   define	RWLOCK_INIT(x, y)	initlock(&(x)->ipf_lk, 0, 0, y)
529 #   define	RWLOCK_EXIT(x)		MUTEX_EXIT(x)
530 #  endif
531 #  define	RW_DESTROY(x)
532 #  define	COPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
533 #  define	COPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
534 #  define	SPL_SCHED(x)	;
535 #  define	SPL_NET(x)	;
536 #  define	SPL_IMP(x)	;
537 #  undef	SPL_X
538 #  define	SPL_X(x)	;
539 extern	void	*get_unit(char *, int);
540 #  define	GETIFP(n, v)	get_unit(n, v)
541 #  define	GETIFMTU_4(x)	((ill_t *)x)->ill_mtu
542 #  define	GETIFMTU_6(x)	((ill_t *)x)->ill_mtu
543 #  define	IFNAME(x, b)	((ill_t *)x)->ill_name
544 #  define	COPYIFNAME(v, x, b) \
545 				(void) strncpy(b, ((qif_t *)x)->qf_name, \
546 					       LIFNAMSIZ)
547 #  define	UIOMOVE(a,b,c,d)	uiomove((void *)a,b,c,d)
548 #  define	SLEEP(id, n)	{ lock_t *_l = get_sleep_lock((void *)id); \
549 				  sleep(id, PZERO+1); \
550 				  spinunlock(_l); \
551 				}
552 #  define	WAKEUP(id,x)	{ lock_t *_l = get_sleep_lock((void *)id); \
553 				  wakeup(id + x); \
554 				  spinunlock(_l); \
555 				}
556 #  define	POLLWAKEUP(x)	;
557 #  define	KMALLOC(a, b)	MALLOC((a), b, sizeof(*(a)), M_IOSYS, M_NOWAIT)
558 #  define	KMALLOCS(a, b, c) (a) = (b)malloc((c), M_IOSYS, M_NOWAIT)
559 #  define	KFREE(x)	kmem_free((char *)(x), sizeof(*(x)))
560 #  define	KFREES(x,s)	kmem_free((char *)(x), (s))
561 #  define	MSGDSIZE(m)	msgdsize(m)
562 #  define	M_ADJ(m,x)	adjmsg(m, x)
563 #  define	M_LEN(m)	((m)->b_wptr - (m)->b_rptr)
564 #  define	M_COPY(m)	copymsg((m))
565 #  define	M_DUP(m)	dupmsg(m)
566 #  define	MTOD(m,t)	((t)((m)->b_rptr))
567 #  define	MTYPE(m)	((m)->b_datap->db_type)
568 #  define	FREE_MB_T(m)	freemsg(m)
569 #  define	m_next		b_cont
570 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
571 typedef mblk_t mb_t;
572 
573 #  include "qif.h"
574 #  include "pfil.h"
575 
576 # else /* _KERNEL */
577 
578 typedef	unsigned char uchar_t;
579 
580 #  ifndef	_SYS_STREAM_INCLUDED
581 typedef char * mblk_t;
582 typedef void *queue_t;
583 typedef	u_long ulong;
584 #  endif
585 #  include <netinet/ip_info.h>
586 
587 # endif /* _KERNEL */
588 
589 # ifdef lint
590 #  define ALIGN32(ptr)    (ptr ? 0L : 0L)
591 #  define ALIGN16(ptr)    (ptr ? 0L : 0L)
592 # else
593 #  define ALIGN32(ptr)    (ptr)
594 #  define ALIGN16(ptr)    (ptr)
595 # endif
596 
597 typedef	struct uio	uio_t;
598 typedef	int		ioctlcmd_t;
599 typedef	int		minor_t;
600 typedef unsigned int	u_32_t;
601 # define	U_32_T	1
602 
603 # define OS_RECOGNISED 1
604 
605 #endif /* __hpux */
606 
607 /* ----------------------------------------------------------------------- */
608 /*                                  I R I X                                */
609 /* ----------------------------------------------------------------------- */
610 #ifdef __sgi
611 # undef		MENTAT
612 # if IRIX < 60500
613 typedef	struct uio	uio_t;
614 # endif
615 typedef	int		ioctlcmd_t;
616 typedef u_int32_t       u_32_t;
617 # define	U_32_T	1
618 
619 # ifdef INET6
620 #  define USE_INET6
621 # endif
622 
623 # define  hz HZ
624 # include <sys/ksynch.h>
625 # define	IPF_LOCK_PL	plhi
626 # include <sys/sema.h>
627 # undef kmutex_t
628 typedef struct {
629 	lock_t *l;
630 	int pl;
631 } kmutex_t;
632 
633 # ifdef MUTEX_INIT
634 #  define	KMUTEX_T		mutex_t
635 # else
636 #  define	KMUTEX_T		kmutex_t
637 #  define	KRWLOCK_T		kmutex_t
638 # endif
639 
640 # ifdef _KERNEL
641 #  define	NEED_LOCAL_RAND		1
642 #  define	ATOMIC_INC(x)		{ MUTEX_ENTER(&softc->ipf_rw); \
643 					  (x)++; MUTEX_EXIT(&softc->ipf_rw); }
644 #  define	ATOMIC_DEC(x)		{ MUTEX_ENTER(&softc->ipf_rw); \
645 					  (x)--; MUTEX_EXIT(&softc->ipf_rw); }
646 #  define	USE_MUTEXES
647 #  ifdef MUTEX_INIT
648 #   include <sys/atomic_ops.h>
649 #   define	ATOMIC_INCL(x)		atomicAddUlong(&(x), 1)
650 #   define	ATOMIC_INC64(x)		atomicAddUint64(&(x), 1)
651 #   define	ATOMIC_INC32(x)		atomicAddUint(&(x), 1)
652 #   define	ATOMIC_DECL(x)		atomicAddUlong(&(x), -1)
653 #   define	ATOMIC_DEC64(x)		atomicAddUint64(&(x), -1)
654 #   define	ATOMIC_DEC32(x)		atomicAddUint(&(x), -1)
655 #   undef	MUTEX_INIT
656 #   define	MUTEX_INIT(x, y)	mutex_init(&(x)->ipf_lk,  \
657 						   MUTEX_DEFAULT, y)
658 #   undef	MUTEX_ENTER
659 #   define	MUTEX_ENTER(x)		mutex_lock(&(x)->ipf_lk, 0)
660 #   undef	MUTEX_EXIT
661 #   define	MUTEX_EXIT(x)		mutex_unlock(&(x)->ipf_lk)
662 #   undef	MUTEX_DESTROY
663 #   define	MUTEX_DESTROY(x)	mutex_destroy(&(x)->ipf_lk)
664 #   define	MUTEX_DOWNGRADE(x)	mrdemote(&(x)->ipf_lk)
665 #   define	KRWLOCK_T		mrlock_t
666 #   define	RWLOCK_INIT(x, y)	mrinit(&(x)->ipf_lk, y)
667 #   undef	RW_DESTROY
668 #   define	RW_DESTROY(x)		mrfree(&(x)->ipf_lk)
669 #   define	READ_ENTER(x)		RW_RDLOCK(&(x)->ipf_lk)
670 #   define	WRITE_ENTER(x)		RW_WRLOCK(&(x)->ipf_lk)
671 #   define	RWLOCK_EXIT(x)		RW_UNLOCK(&(x)->ipf_lk)
672 #  else
673 #   define	READ_ENTER(x)		MUTEX_ENTER(&(x)->ipf_lk)
674 #   define	WRITE_ENTER(x)		MUTEX_ENTER(&(x)->ipf_lk)
675 #   define	MUTEX_DOWNGRADE(x)	;
676 #   define	RWLOCK_EXIT(x)		MUTEX_EXIT(&(x)->ipf_lk)
677 #   define	MUTEX_EXIT(x)		UNLOCK((x)->ipf_lk.l, (x)->ipf_lk.pl);
678 #   define	MUTEX_INIT(x,y)		(x)->ipf_lk.l = LOCK_ALLOC((uchar_t)-1, IPF_LOCK_PL, (lkinfo_t *)-1, KM_NOSLEEP)
679 #   define	MUTEX_DESTROY(x)	LOCK_DEALLOC((x)->ipf_lk.l)
680 #   define	MUTEX_ENTER(x)		(x)->ipf_lk.pl = LOCK((x)->ipf_lk.l, \
681 							      IPF_LOCK_PL);
682 #  endif
683 #  define	MUTEX_NUKE(x)		bzero((x), sizeof(*(x)))
684 #  define	FREE_MB_T(m)	m_freem(m)
685 #  define	MTOD(m,t)	mtod(m,t)
686 #  define	COPYIN(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
687 #  define	COPYOUT(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
688 #  define	UIOMOVE(a,b,c,d)	uiomove((caddr_t)a,b,c,d)
689 #  define	SLEEP(id, n)	sleep((id), PZERO+1)
690 #  define	WAKEUP(id,x)	wakeup(id+x)
691 #  define	POLLWAKEUP(x)	;
692 #  define	KFREE(x)	kmem_free((char *)(x), sizeof(*(x)))
693 #  define	KFREES(x,s)	kmem_free((char *)(x), (s))
694 #  define	GETIFP(n,v)	ifunit(n)
695 #  define	GETIFMTU_4(x)	((struct ifnet *)x)->if_mtu
696 #  define	GETIFMTU_6(x)	((struct ifnet *)x)->if_mtu
697 #  include <sys/kmem.h>
698 #  include <sys/ddi.h>
699 #  define	KMALLOC(a,b)	(a) = (b)kmem_alloc(sizeof(*(a)), KM_NOSLEEP)
700 #  define	KMALLOCS(a,b,c)	(a) = (b)kmem_alloc((c), KM_NOSLEEP)
701 #  define	GET_MINOR(x)	getminor(x)
702 #  define	USE_SPL		1
703 #  define	SPL_IMP(x)	(x) = splimp()
704 #  define	SPL_NET(x)	(x) = splnet()
705 #  define	SPL_SCHED(x)	(x) = splsched()
706 #  define	SPL_X(x)	(void) splx(x)
707 extern	void	m_copydata(struct mbuf *, int, int, void *);
708 extern	void	m_copyback(struct mbuf *, int, int, void *);
709 #  define	M_ADJ(m,x)	m_adj(m, x)
710 #  define	M_LEN(x)	(x)->m_len
711 #  define	M_COPY(m)	m_copy((m), 0, M_COPYALL)
712 #  define	GETKTIME(x)	microtime((struct timeval *)x)
713 #  define	IFNAME(x)	((struct ifnet *)x)->if_name
714 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
715 typedef struct mbuf mb_t;
716 # else
717 #  undef RW_DESTROY
718 #  undef MUTEX_INIT
719 #  undef MUTEX_DESTROY
720 # endif /* _KERNEL */
721 
722 # define OS_RECOGNISED 1
723 
724 #endif /* __sgi */
725 
726 /* ----------------------------------------------------------------------- */
727 /*                                  T R U 6 4                              */
728 /* ----------------------------------------------------------------------- */
729 #ifdef __osf__
730 # undef		MENTAT
731 
732 # include <kern/lock.h>
733 # include <sys/sysmacros.h>
734 
735 # ifdef _KERNEL
736 #  define	NEED_LOCAL_RAND		1
737 #  define	KMUTEX_T		simple_lock_data_t
738 #  define	KRWLOCK_T		lock_data_t
739 #  include <net/net_globals.h>
740 #  define	USE_MUTEXES
741 #  define	READ_ENTER(x)		lock_read(&(x)->ipf_lk)
742 #  define	WRITE_ENTER(x)		lock_write(&(x)->ipf_lk)
743 #  define	MUTEX_DOWNGRADE(x)	lock_write_to_read(&(x)->ipf_lk)
744 #  define	RWLOCK_INIT(x, y)	lock_init(&(x)->ipf_lk, TRUE)
745 #  define	RWLOCK_EXIT(x)		lock_done(&(x)->ipf_lk)
746 #  define	RW_DESTROY(x)		lock_terminate(&(x)->ipf_lk)
747 #  define	MUTEX_ENTER(x)		simple_lock(&(x)->ipf_lk)
748 #  define	MUTEX_INIT(x, y)	simple_lock_init(&(x)->ipf_lk)
749 #  define	MUTEX_DESTROY(x)	simple_lock_terminate(&(x)->ipf_lk)
750 #  define	MUTEX_EXIT(x)		simple_unlock(&(x)->ipf_lk)
751 #  define	MUTEX_NUKE(x)		bzero(x, sizeof(*(x)))
752 #  define	ATOMIC_INC64(x)		atomic_incq((uint64_t*)&(x))
753 #  define	ATOMIC_DEC64(x)		atomic_decq((uint64_t*)&(x))
754 #  define	ATOMIC_INC32(x)		atomic_incl((uint32_t*)&(x))
755 #  define	ATOMIC_DEC32(x)		atomic_decl((uint32_t*)&(x))
756 #  define	ATOMIC_INCL(x)		atomic_incl((uint32_t*)&(x))
757 #  define	ATOMIC_DECL(x)		atomic_decl((uint32_t*)&(x))
758 #  define	ATOMIC_INC(x)		{ simple_lock(&softc->ipf_rw); (x)++; \
759 					  simple_unlock(&softc->ipf_rw); }
760 #  define	ATOMIC_DEC(x)		{ simple_lock(&softc->ipf_rw); (x)--; \
761 					  simple_unlock(&softc->ipf_rw); }
762 #  define	SPL_SCHED(x)		;
763 #  define	SPL_NET(x)		;
764 #  define	SPL_IMP(x)		;
765 #  undef	SPL_X
766 #  define	SPL_X(x)		;
767 #  define	UIOMOVE(a,b,c,d)	uiomove((void *)a, b, d)
768 #  define	FREE_MB_T(m)		m_freem(m)
769 #  define	MTOD(m,t)		mtod(m,t)
770 #  define	GETIFP(n, v)		ifunit(n)
771 #  define	GETIFMTU_4(x)		((struct ifnet *)x)->if_mtu
772 #  define	GETIFMTU_6(x)		((struct ifnet *)x)->if_mtu
773 #  define	GET_MINOR		getminor
774 #  define	WAKEUP(id,x)		wakeup(id + x)
775 #  define	POLLWAKEUP(x)		;
776 #  define	COPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
777 #  define	COPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
778 #  define	KMALLOC(a, b)	MALLOC((a), b, sizeof(*(a)), M_PFILT, M_NOWAIT)
779 #  define	KMALLOCS(a, b, c) (a) = (b)malloc((c), M_PFILT, \
780 					    ((c) > 4096) ? M_WAITOK : M_NOWAIT)
781 #  define	KFREE(x)	FREE((x), M_PFILT)
782 #  define	KFREES(x,s)	FREE((x), M_PFILT)
783 #  define	MSGDSIZE(m)	mbufchainlen(m)
784 #  define	M_LEN(m)	(m)->m_len
785 #  define	M_ADJ(m,x)	m_adj(m, x)
786 #  define	M_COPY(m)	m_copy((m), 0, M_COPYALL)
787 #  define	M_DUP(m)	m_copy((m), 0, M_COPYALL)
788 #  define	GETKTIME(x)	microtime((struct timeval *)x)
789 #  define	IFNAME(x)	((struct ifnet *)x)->if_name
790 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
791 #  define	selinfo		sel_queue
792 typedef struct mbuf mb_t;
793 # endif /* _KERNEL */
794 
795 # if (defined(_KERNEL) || defined(_NO_BITFIELDS) || (__STDC__ == 1))
796 #  define	IP_V(x)		((x)->ip_vhl >> 4)
797 #  define	IP_HL(x)	((x)->ip_vhl & 0xf)
798 #  define	IP_V_A(x,y)	(x)->ip_vhl |= (((y) << 4) & 0xf0)
799 #  define	IP_HL_A(x,y)	(x)->ip_vhl |= ((y) & 0xf)
800 #  define	TCP_X2(x)	((x)->th_xoff & 0xf)
801 #  define	TCP_X2_A(x,y)	(x)->th_xoff |= ((y) & 0xf)
802 #  define	TCP_OFF(x)	((x)->th_xoff >> 4)
803 #  define	TCP_OFF_A(x,y)	(x)->th_xoff |= (((y) << 4) & 0xf0)
804 # endif
805 # if TRU64 <= 1885
806 #  define	ip6_vfc		ip6_vcf
807 # endif
808 
809 /*
810  * These are from's Solaris' #defines for little endian.
811  */
812 #define	IP6F_MORE_FRAG		0x0100
813 #define	IP6F_RESERVED_MASK	0x0600
814 #define	IP6F_OFF_MASK		0xf8ff
815 
816 struct ip6_ext {
817 	u_char	ip6e_nxt;
818 	u_char	ip6e_len;
819 };
820 
821 typedef	int		ioctlcmd_t;
822 /*
823  * Really, any arch where sizeof(long) != sizeof(int).
824  */
825 typedef unsigned int    u_32_t;
826 # define	U_32_T	1
827 
828 # define OS_RECOGNISED 1
829 #endif /* __osf__ */
830 
831 /* ----------------------------------------------------------------------- */
832 /*                                  N E T B S D                            */
833 /* ----------------------------------------------------------------------- */
834 #ifdef __NetBSD__
835 # define HAS_SYS_MD5_H	1
836 # if (NetBSD >= 199905) && !defined(IPFILTER_LKM) && defined(_KERNEL)
837 #  if (__NetBSD_Version__ < 399001400)
838 #   include "opt_ipfilter_log.h"
839 #  else
840 #   if (__NetBSD_Version__ >= 799003000)
841 #    if defined(_KERNEL_OPT)
842 #     include "opt_ipfilter.h"
843 #    endif
844 #   else
845 #    include "opt_ipfilter.h"
846 #   endif
847 #  endif
848 # endif
849 # if defined(_KERNEL)
850 #  include <sys/systm.h>
851 #  include <sys/malloc.h>
852 #  if (__NetBSD_Version__ > 500000000)
853 #   include <sys/kauth.h>
854 #  endif
855 # else
856 #  include <stddef.h>
857 #  include <stdbool.h>
858 # endif
859 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
860 #  if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 599002300)
861 #    define NBPFILTER 1
862 #  else
863 #    include "bpfilter.h"
864 #  endif
865 #  if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 104110000)
866 #   if (__NetBSD_Version__ >= 799003000)
867 #    if defined(_KERNEL_OPT)
868 #     include "opt_inet.h"
869 #    endif
870 #   else
871 #    include "opt_inet.h"
872 #   endif
873 #  endif
874 #  ifdef INET6
875 #   define USE_INET6
876 #  endif
877 #  if (__NetBSD_Version__ >= 105000000)
878 #   define HAVE_M_PULLDOWN 1
879 #  endif
880 # endif
881 
882 #ifndef _KERNEL
883 # define	ipf_random	arc4random
884 #endif
885 
886 # if (__NetBSD_Version__ >= 499000000)
887 #  ifdef _KERNEL
888 #   include <sys/rwlock.h>
889 #   define	USE_MUTEXES		1
890 #   define	KMUTEX_T		kmutex_t
891 #   define	KRWLOCK_T		krwlock_t
892 #   define	MUTEX_DESTROY(x)	mutex_destroy(&(x)->ipf_lk)
893 #   define	MUTEX_DOWNGRADE(x)	rw_downgrade(&(x)->ipf_lk)
894 #   define	MUTEX_ENTER(x)		mutex_enter(&(x)->ipf_lk)
895 #   define	MUTEX_EXIT(x)		mutex_exit(&(x)->ipf_lk)
896 #   define	MUTEX_INIT(x,y)		mutex_init(&(x)->ipf_lk, MUTEX_DRIVER,\
897 						  IPL_SOFTNET)
898 #   define	MUTEX_NUKE(x)		bzero((x), sizeof(*(x)))
899 #   define	READ_ENTER(x)		rw_enter(&(x)->ipf_lk, RW_READER)
900 #   define	RWLOCK_INIT(x, y)	rw_init(&(x)->ipf_lk)
901 #   define	RWLOCK_EXIT(x)		rw_exit(&(x)->ipf_lk)
902 #   define	RW_DESTROY(x)		rw_destroy(&(x)->ipf_lk)
903 #   define	WRITE_ENTER(x)		rw_enter(&(x)->ipf_lk, RW_WRITER)
904 #   define	SPL_SCHED(x)		;
905 #   define	SPL_NET(x)		;
906 #   define	SPL_IMP(x)		;
907 #   define	SPL_X(x)		;
908 #  endif
909 # endif
910 
911 #if (__NetBSD_Version__ >= 699000000)
912 #  define HAVE_RBTREE	1
913 #endif
914 
915 # ifdef _KERNEL
916 #  include <sys/cprng.h>
917 #  if (__NetBSD_Version__ >= 399001400)
918 #   include <sys/selinfo.h>		/* Not in NetBSD 3.1 */
919 #   define	PROC_T  struct lwp
920 #   define	KFREE(a)		free((a), _M_IPF)
921 #   define	KFREES(a, b)		free((a), _M_IPF)
922 #   define	KMALLOC(a, b)		(a) = (b)malloc(sizeof (*(a)), \
923 							_M_IPF, M_NOWAIT)
924 #   define	KMALLOCS(a, b, c)	(a) = (b)malloc((c), _M_IPF, M_NOWAIT)
925 #  else
926 #   define	PROC_T  struct proc
927 #  endif
928 #  define	MSGDSIZE(m)	mbufchainlen(m)
929 #  define	M_LEN(m)	(m)->m_len
930 #  define	M_ADJ(m,x)	m_adj(m, x)
931 #  define	M_COPY(x)	m_copy((x), 0, M_COPYALL)
932 #  define	GETKTIME(x)	microtime((struct timeval *)x)
933 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
934 #  define	COPYIN(a,b,c)	copyin((void *)(a), (void *)(b), (c))
935 #  define	COPYOUT(a,b,c)	copyout((void *)(a), (void *)(b), (c))
936 #  define	BCOPYIN(a,b,c)	(bcopy((void *)(a), (void *)(b), (c)), 0)
937 #  define	BCOPYOUT(a,b,c)	(bcopy((void *)(a), (void *)(b), (c)), 0)
938 #  if (defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 499004900))
939 #   define	POLLWAKEUP(x)	selnotify(softc->ipf_selwait+x, 0, 0)
940 #  endif
941 #  define	ASSERT(x)	KASSERT(x)
942 typedef struct mbuf mb_t;
943 # endif /* _KERNEL */
944 # if (NetBSD <= 1991011) && (NetBSD >= 199606)
945 #  define	IFNAME(x)	((struct ifnet *)x)->if_xname
946 #  define	COPYIFNAME(v, x, b) \
947 				(void) strncpy(b, \
948 					       ((struct ifnet *)x)->if_xname, \
949 					       LIFNAMSIZ)
950 # else
951 #  define	IFNAME(x)	((struct ifnet *)x)->if_name
952 # endif
953 typedef	struct uio	uio_t;
954 typedef	u_long		ioctlcmd_t;
955 typedef	int		minor_t;
956 typedef	u_int32_t	u_32_t;
957 # define	U_32_T	1
958 
959 # define OS_RECOGNISED 1
960 #endif /* __NetBSD__ */
961 
962 
963 /* ----------------------------------------------------------------------- */
964 /*                                F R E E B S D                            */
965 /* ----------------------------------------------------------------------- */
966 #ifdef __FreeBSD__
967 # if  (__FreeBSD_version < 400000)
968 #  define	NEED_LOCAL_RAND	1
969 # endif
970 # if defined(_KERNEL)
971 #  if (__FreeBSD_version >= 500000)
972 #   include "opt_bpf.h"
973 #  endif
974 #  if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000)
975 #   include "opt_inet6.h"
976 #  endif
977 #  if defined(INET6) && !defined(USE_INET6)
978 #   define USE_INET6
979 #  endif
980 # endif
981 
982 # if defined(_KERNEL)
983 #  include <netinet/ip_var.h>
984 #  if (__FreeBSD_version >= 500024)
985 #   if (__FreeBSD_version >= 500043)
986 #    define	p_cred	td_ucred
987 #    define	p_uid	td_ucred->cr_ruid
988 #   else
989 #    define	p_cred	t_proc->p_cred
990 #    define	p_uid	t_proc->p_cred->p_ruid
991 #   endif
992 #  else
993 #   define	p_uid	p_cred->p_ruid
994 #  endif /* __FreeBSD_version >= 500024 */
995 
996 #  if (__FreeBSD_version >= 400000)
997 /*
998  * When #define'd, the 5.2.1 kernel panics when used with the ftp proxy.
999  * There may be other, safe, kernels but this is not extensively tested yet.
1000  */
1001 #   define HAVE_M_PULLDOWN
1002 #  endif
1003 #  if !defined(IPFILTER_LKM) && (__FreeBSD_version >= 300000)
1004 #   include "opt_ipfilter.h"
1005 #  endif
1006 #  define	COPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
1007 #  define	COPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
1008 
1009 #  if (__FreeBSD_version >= 500043)
1010 #   define NETBSD_PF
1011 #  endif
1012 # else
1013 #  include <inttypes.h>
1014 # endif /* _KERNEL */
1015 
1016 # if (__FreeBSD_version >= 700000)
1017 #  include <sys/selinfo.h>
1018 # endif
1019 # if (__FreeBSD_version >= 500043)
1020 #  include <sys/mutex.h>
1021 #  if (__FreeBSD_version >= 700014)
1022 #   define	KRWLOCK_FILL_SZ		36
1023 #   define	KMUTEX_FILL_SZ		24
1024 #   include <sys/rwlock.h>
1025 #   ifdef _KERNEL
1026 #    define	KMUTEX_T		struct mtx
1027 #    define	KRWLOCK_T		struct rwlock
1028 #    define	READ_ENTER(x)		rw_rlock(&(x)->ipf_lk)
1029 #    define	WRITE_ENTER(x)		rw_wlock(&(x)->ipf_lk)
1030 #    define	MUTEX_DOWNGRADE(x)	rw_downgrade(&(x)->ipf_lk)
1031 #    define	RWLOCK_INIT(x,y)	rw_init(&(x)->ipf_lk, (y))
1032 #    define	RW_DESTROY(x)		rw_destroy(&(x)->ipf_lk)
1033 #    define	RWLOCK_EXIT(x)		do { \
1034 					    if (rw_wowned(&(x)->ipf_lk)) \
1035 					    	rw_wunlock(&(x)->ipf_lk); \
1036 					    else \
1037 						rw_runlock(&(x)->ipf_lk); \
1038 					} while (0)
1039 #   endif
1040 #  else
1041 #   include <sys/sx.h>
1042 /*
1043  * Whilst the sx(9) locks on FreeBSD have the right semantics and interface
1044  * for what we want to use them for, despite testing showing they work -
1045  * with a WITNESS kernel, it generates LOR messages.
1046  */
1047 #   if (__FreeBSD_version < 700000)
1048 #    ifdef _KERNEL
1049 #     define	KMUTEX_T		struct mtx
1050 #     define	KRWLOCK_T		struct mtx
1051 #     define	READ_ENTER(x)		mtx_lock(&(x)->ipf_lk)
1052 #     define	WRITE_ENTER(x)		mtx_lock(&(x)->ipf_lk)
1053 #     define	RWLOCK_EXIT(x)		mtx_unlock(&(x)->ipf_lk)
1054 #     define	MUTEX_DOWNGRADE(x)	;
1055 #     define	RWLOCK_INIT(x,y)	mtx_init(&(x)->ipf_lk, (y), NULL,\
1056 						 MTX_DEF)
1057 #     define	RW_DESTROY(x)		mtx_destroy(&(x)->ipf_lk)
1058 #    endif
1059 #   else
1060 #    ifdef _KERNEL
1061 #     define	KRWLOCK_T		struct sx
1062 #     define	KMUTEX_T		struct mtx
1063 #     define	READ_ENTER(x)		sx_slock(&(x)->ipf_lk)
1064 #     define	WRITE_ENTER(x)		sx_xlock(&(x)->ipf_lk)
1065 #     define	MUTEX_DOWNGRADE(x)	sx_downgrade(&(x)->ipf_lk)
1066 #     define	RWLOCK_INIT(x, y)	sx_init(&(x)->ipf_lk, (y))
1067 #     define	RW_DESTROY(x)		sx_destroy(&(x)->ipf_lk)
1068 #     ifdef sx_unlock
1069 #      define	RWLOCK_EXIT(x)		sx_unlock(&(x)->ipf_lk)
1070 #     else
1071 #      define	RWLOCK_EXIT(x)		do { \
1072 					    if ((x)->ipf_lk.sx_cnt < 0) \
1073 						sx_xunlock(&(x)->ipf_lk); \
1074 					    else \
1075 						sx_sunlock(&(x)->ipf_lk); \
1076 					} while (0)
1077 #     endif
1078 #    endif
1079 #   endif
1080 #  endif
1081 # endif
1082 
1083 # if (__FreeBSD_version >= 501113)
1084 #  include <net/if_var.h>
1085 #  define	IFNAME(x)	((struct ifnet *)x)->if_xname
1086 #  define	COPYIFNAME(v, x, b) \
1087 				(void) strncpy(b, \
1088 					       ((struct ifnet *)x)->if_xname, \
1089 					       LIFNAMSIZ)
1090 # endif
1091 # if (__FreeBSD_version >= 500043)
1092 # else
1093 #  define	IFNAME(x)	((struct ifnet *)x)->if_name
1094 # endif
1095 
1096 # ifdef _KERNEL
1097 #  define	GETKTIME(x)	microtime((struct timeval *)x)
1098 
1099 #  if (__FreeBSD_version >= 500002)
1100 #   include <netinet/in_systm.h>
1101 #   include <netinet/ip.h>
1102 #   include <machine/in_cksum.h>
1103 #  endif
1104 
1105 #  if (__FreeBSD_version >= 500043)
1106 #   define	USE_MUTEXES
1107 #   define	MUTEX_ENTER(x)		mtx_lock(&(x)->ipf_lk)
1108 #   define	MUTEX_EXIT(x)		mtx_unlock(&(x)->ipf_lk)
1109 #   define	MUTEX_INIT(x,y)		mtx_init(&(x)->ipf_lk, (y), NULL,\
1110 						 MTX_DEF)
1111 #   define	MUTEX_DESTROY(x)	mtx_destroy(&(x)->ipf_lk)
1112 #   define	MUTEX_NUKE(x)		bzero((x), sizeof(*(x)))
1113 /*
1114  * Whilst the sx(9) locks on FreeBSD have the right semantics and interface
1115  * for what we want to use them for, despite testing showing they work -
1116  * with a WITNESS kernel, it generates LOR messages.
1117  */
1118 #   include <machine/atomic.h>
1119 #   define	ATOMIC_INC(x)		{ mtx_lock(&softc->ipf_rw.ipf_lk); (x)++; \
1120 					  mtx_unlock(&softc->ipf_rw.ipf_lk); }
1121 #   define	ATOMIC_DEC(x)		{ mtx_lock(&softc->ipf_rw.ipf_lk); (x)--; \
1122 					  mtx_unlock(&softc->ipf_rw.ipf_lk); }
1123 #   define	ATOMIC_INCL(x)		atomic_add_long(&(x), 1)
1124 #   define	ATOMIC_INC64(x)		ATOMIC_INC(x)
1125 #   define	ATOMIC_INC32(x)		atomic_add_32((u_int *)&(x), 1)
1126 #   define	ATOMIC_DECL(x)		atomic_add_long(&(x), -1)
1127 #   define	ATOMIC_DEC64(x)		ATOMIC_DEC(x)
1128 #   define	ATOMIC_DEC32(x)		atomic_add_32((u_int *)&(x), -1)
1129 #   define	SPL_X(x)	;
1130 #   define	SPL_NET(x)	;
1131 #   define	SPL_IMP(x)	;
1132 #   define	SPL_SCHED(x)	;
1133 extern	int	in_cksum(struct mbuf *, int);
1134 #  else
1135 #   define	SPL_SCHED(x)	x = splhigh()
1136 #  endif /* __FreeBSD_version >= 500043 */
1137 #  if (__FreeBSD_version >= 500024)
1138 #   define	GET_MINOR		dev2unit
1139 #  endif
1140 #  define	MSGDSIZE(m)	mbufchainlen(m)
1141 #  define	M_LEN(m)	(m)->m_len
1142 #  define	M_ADJ(m,x)	m_adj(m, x)
1143 #  define	M_COPY(x)	m_copy((x), 0, M_COPYALL)
1144 #  define	M_DUP(m)	m_dup(m, M_NOWAIT)
1145 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
1146 typedef struct mbuf mb_t;
1147 # endif /* _KERNEL */
1148 
1149 # if __FreeBSD__ < 3
1150 #  include <machine/spl.h>
1151 # else
1152 #  if __FreeBSD__ == 3
1153 #   if defined(IPFILTER_LKM) && !defined(ACTUALLY_LKM_NOT_KERNEL)
1154 #    define	ACTUALLY_LKM_NOT_KERNEL
1155 #   endif
1156 #  endif
1157 # endif
1158 
1159 # if (__FreeBSD_version >= 300000)
1160 typedef	u_long		ioctlcmd_t;
1161 # else
1162 typedef	int		ioctlcmd_t;
1163 # endif
1164 typedef	struct uio	uio_t;
1165 typedef	int		minor_t;
1166 typedef	u_int32_t	u_32_t;
1167 # define	U_32_T	1
1168 
1169 # define OS_RECOGNISED 1
1170 #endif /* __FreeBSD__ */
1171 
1172 
1173 /* ----------------------------------------------------------------------- */
1174 /*                                O P E N B S D                            */
1175 /* ----------------------------------------------------------------------- */
1176 #ifdef __OpenBSD__
1177 # ifdef INET6
1178 #  define USE_INET6
1179 # endif
1180 
1181 # ifdef _KERNEL
1182 #  if !defined(IPFILTER_LKM)
1183 #   include "bpfilter.h"
1184 #  endif
1185 #  if (OpenBSD >= 200311)
1186 #   if defined(USE_INET6)
1187 #    include "netinet6/in6_var.h"
1188 #    include "netinet6/nd6.h"
1189 #   endif
1190 #  endif
1191 #  if (OpenBSD >= 200012)
1192 #   define HAVE_M_PULLDOWN 1
1193 #  endif
1194 #  define	COPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
1195 #  define	COPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
1196 #  define	GETKTIME(x)	microtime((struct timeval *)x)
1197 #  define	MSGDSIZE(m)	mbufchainlen(m)
1198 #  define	M_LEN(m)	(m)->m_len
1199 #  define	M_ADJ(m,x)	m_adj(m, x)
1200 #  define	M_COPY(m)	m_copy((m), 0, M_COPYALL)
1201 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
1202 typedef struct mbuf mb_t;
1203 # endif /* _KERNEL */
1204 # if (OpenBSD >= 199603)
1205 #  define	IFNAME(x, b)	((struct ifnet *)x)->if_xname
1206 #  define	COPYIFNAME(v, x, b) \
1207 				(void) strncpy(b, \
1208 					       ((struct ifnet *)x)->if_xname, \
1209 					       LIFNAMSIZ)
1210 # else
1211 #  define	IFNAME(x)	((struct ifnet *)x)->if_name
1212 # endif
1213 typedef	struct uio	uio_t;
1214 typedef	u_long		ioctlcmd_t;
1215 typedef	int		minor_t;
1216 typedef	u_int32_t	u_32_t;
1217 # define	U_32_T	1
1218 
1219 # define OS_RECOGNISED 1
1220 #endif /* __OpenBSD__ */
1221 
1222 
1223 /* ----------------------------------------------------------------------- */
1224 /*                                B S D O S                                */
1225 /* ----------------------------------------------------------------------- */
1226 #ifdef _BSDI_VERSION
1227 # ifdef INET6
1228 #  define USE_INET6
1229 # endif
1230 
1231 # ifdef _KERNEL
1232 #  define	GETKTIME(x)	microtime((struct timeval *)x)
1233 #  define	MSGDSIZE(m)	mbufchainlen(m)
1234 #  define	M_LEN(m)	(m)->m_len
1235 #  define	M_ADJ(m,x)	m_adj(m, x)
1236 #  define	M_COPY(m)	m_copy((m), 0, M_COPYALL)
1237 #  define	IFNAME(x)	((struct ifnet *)x)->if_name
1238 typedef struct mbuf mb_t;
1239 # endif /* _KERNEL */
1240 
1241 # if (_BSDI_VERSION >= 199701)
1242 typedef	u_long		ioctlcmd_t;
1243 # else
1244 typedef	int		ioctlcmd_t;
1245 # endif
1246 typedef	u_int32_t	u_32_t;
1247 # define	U_32_T	1
1248 
1249 #endif /* _BSDI_VERSION */
1250 
1251 
1252 /* ----------------------------------------------------------------------- */
1253 /*                                  S U N O S 4                            */
1254 /* ----------------------------------------------------------------------- */
1255 #if defined(sun) && !defined(OS_RECOGNISED) /* SunOS4 */
1256 # ifdef _KERNEL
1257 #  include	<sys/kmem_alloc.h>
1258 #  define	GETKTIME(x)	uniqtime((struct timeval *)x)
1259 #  define	MSGDSIZE(m)	mbufchainlen(m)
1260 #  define	M_LEN(m)	(m)->m_len
1261 #  define	M_ADJ(m,x)	m_adj(m, x)
1262 #  define	M_COPY(m)	m_copy((m), 0, M_COPYALL)
1263 #  define	IFNAME(x)	((struct ifnet *)x)->if_name
1264 #  define	GETIFP(n, v)	ifunit(n, IFNAMSIZ)
1265 #  define	GETIFMTU_4(x)	((struct ifnet *)x)->if_mtu
1266 #  define	KFREE(x)	kmem_free((char *)(x), sizeof(*(x)))
1267 #  define	KFREES(x,s)	kmem_free((char *)(x), (s))
1268 #  define	SLEEP(id, n)	sleep((id), PZERO+1)
1269 #  define	WAKEUP(id,x)	wakeup(id + x)
1270 #  define	POLLWAKEUP(x)	;
1271 #  define	UIOMOVE(a,b,c,d)	uiomove((void *)a,b,c,d)
1272 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
1273 
1274 extern	void	m_copydata(struct mbuf *, int, int, void *);
1275 extern	void	m_copyback(struct mbuf *, int, int, void *);
1276 
1277 typedef struct mbuf mb_t;
1278 # endif
1279 
1280 typedef	struct uio	uio_t;
1281 typedef	int		ioctlcmd_t;
1282 typedef	int		minor_t;
1283 typedef	unsigned int	u_32_t;
1284 # define	U_32_T	1
1285 
1286 # define OS_RECOGNISED 1
1287 
1288 #endif /* SunOS 4 */
1289 
1290 /* ----------------------------------------------------------------------- */
1291 /*                            L I N U X                                    */
1292 /* ----------------------------------------------------------------------- */
1293 #if defined(linux) && !defined(OS_RECOGNISED)
1294 # include <linux/version.h>
1295 # if (LINUX >= 20600) && defined(_KERNEL)
1296 #  define	 HDR_T_PRIVATE	1
1297 # endif
1298 # undef USE_INET6
1299 # ifdef USE_INET6
1300 struct ip6_ext {
1301 	u_char	ip6e_nxt;
1302 	u_char	ip6e_len;
1303 };
1304 # endif
1305 
1306 # ifdef _KERNEL
1307 #  include <asm/byteorder.h>
1308 #  ifdef __LITTLE_ENDIAN
1309 #   define BIG_ENDIAN		0
1310 #   define LITTLE_ENDIAN	1
1311 #   define BYTE_ORDER		LITTLE_ENDIAN
1312 #  else
1313 #   define BIG_ENDIAN		1
1314 #   define LITTLE_ENDIAN	0
1315 #   define BYTE_ORDER		BIG_ENDIAN
1316 #  endif
1317 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
1318 #  define	COPYIN(a,b,c)	copy_from_user((caddr_t)(b), (caddr_t)(a), (c))
1319 #  define	COPYOUT(a,b,c)	copy_to_user((caddr_t)(b), (caddr_t)(a), (c))
1320 #  define	FREE_MB_T(m)	kfree_skb(m)
1321 #  define	GETKTIME(x)	do_gettimeofday((struct timeval *)x)
1322 #  define	POLLWAKEUP(x)	;
1323 #  ifdef wait_event_interruptible
1324 #   define	SLEEP(x,s)	wait_event_interruptible((*(x##_linux)), 0)
1325 #  else
1326 #   define	SLEEP(x,s)	0, interruptible_sleep_on(x##_linux)
1327 #  endif
1328 #   define	WAKEUP(x,y)	wake_up(x##_linux + y)
1329 #  define	UIOMOVE(a,b,c,d)	uiomove((caddr_t)a,b,c,d)
1330 #  define	USE_MUTEXES
1331 #  define	KRWLOCK_T		rwlock_t
1332 #  define	KMUTEX_T		spinlock_t
1333 #  define	MUTEX_INIT(x,y)		spin_lock_init(&(x)->ipf_lk)
1334 #  define	MUTEX_ENTER(x)		spin_lock_bh(&(x)->ipf_lk)
1335 #  define	MUTEX_EXIT(x)		spin_unlock_bh(&(x)->ipf_lk)
1336 #  define	MUTEX_DESTROY(x)	do { } while (0)
1337 #  define	MUTEX_NUKE(x)		bzero(&(x)->ipf_lk, sizeof((x)->ipf_lk))
1338 #  define	READ_ENTER(x)		ipf_read_enter(x)
1339 #  define	WRITE_ENTER(x)		ipf_write_enter(x)
1340 #  define	RWLOCK_INIT(x,y)	ipf_rw_init(x, y)
1341 #  define	RW_DESTROY(x)		do { } while (0)
1342 #  define	RWLOCK_EXIT(x)		ipf_rw_exit(x)
1343 #  define	MUTEX_DOWNGRADE(x)	ipf_rw_downgrade(x)
1344 #  define	ATOMIC_INCL(x)		atomic_long_inc((atomic_long_t *)&(x))
1345 #  define	ATOMIC_DECL(x)		atomic_long_dec((atomic_long_t *)&(x))
1346 #  define	ATOMIC_INC32(x)		atomic_inc((atomic_t *)&(x))
1347 #  define	ATOMIC_DEC32(x)		atomic_dec((atomic_t *)&(x))
1348 #  ifdef CONFIG_X86_32
1349 #   define	ATOMIC_INC64(x)		do { MUTEX_ENTER(&softc->ipf_rw); \
1350 					     (x)++; \
1351 					     MUTEX_EXIT(&softc->ipf_rw); \
1352 					} while (0)
1353 #   define	ATOMIC_DEC64(x)		do { MUTEX_ENTER(&softc->ipf_rw); \
1354 					     (x)--; \
1355 					     MUTEX_EXIT(&softc->ipf_rw); \
1356 					} while (0)
1357 #  else
1358 #   define	ATOMIC_INC64(x)		atomic64_inc((atomic64_t *)&(x))
1359 #   define	ATOMIC_DEC64(x)		atomic64_dec((atomic64_t *)&(x))
1360 #  endif
1361 #  define	U_QUAD_T		u_int64_t
1362 #  define	QUAD_T			int64_t
1363 #  define	SPL_SCHED(x)		do { } while (0)
1364 #  define	SPL_IMP(x)		do { } while (0)
1365 #  define	SPL_NET(x)		do { } while (0)
1366 #  define	SPL_X(x)		do { } while (0)
1367 #  define	IFNAME(x)		((struct net_device*)x)->name
1368 typedef	struct	sk_buff	mb_t;
1369 extern	void	m_copydata(mb_t *, int, int, void *);
1370 extern	void	m_copyback(mb_t *, int, int, void *);
1371 extern	void	m_adj(mb_t *, int);
1372 extern	mb_t	*m_pullup(mb_t *, int);
1373 #  define	mbuf	sk_buff
1374 
1375 #  define	mtod(m, t)	((t)(m)->data)
1376 #  define	m_adj(m, x)	skb_trim((m), (m)->len + (x))
1377 #  define	m_data		data
1378 #  define	m_len		len
1379 #  define	m_next		next
1380 #  define	M_COPY(m)	skb_clone((m), in_interrupt() ? GFP_ATOMIC : \
1381 								GFP_KERNEL)
1382 #  define	MSGDSIZE(m)	(m)->len
1383 #  define	M_LEN(m)	(m)->len
1384 #  define	M_ADJ(m,x)	m_adj(m, x)
1385 #  define	M_DUP(m)	skb_copy((m), in_interrupt() ? GFP_ATOMIC : \
1386 								GFP_KERNEL)
1387 #  define	PREP_MB_T(f, m)	do { \
1388 					(m)->next = *(f)->fin_mp; \
1389 					*(fin)->fin_mp = (m); \
1390 					(f)->fin_m = (m); \
1391 				} while (0)
1392 #  define	ALLOC_MB_T(m,l)	(m) = alloc_skb((l), \
1393 						in_interrupt() ? GFP_ATOMIC : \
1394 								 GFP_KERNEL)
1395 
1396 #  define	splnet(x)	;
1397 #  define	printf		printk
1398 #  define	bcopy(s,d,z)	memmove(d, s, z)
1399 #  define	bzero(s,z)	memset(s, 0, z)
1400 #  define	bcmp(a,b,z)	memcmp(a, b, z)
1401 #  if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
1402 #   define	ipf_random	random32
1403 #   define	arc4random	random32
1404 #  else
1405 #   include <linux/random.h>
1406 #   define	ipf_random	get_random_int
1407 #   define	arc4random	get_random_int
1408 #  endif
1409 
1410 #  define	ifnet		net_device
1411 #  define	if_xname	name
1412 #  define	if_unit		ifindex
1413 
1414 #  define	KMALLOC(x,t)	(x) = (t)kmalloc(sizeof(*(x)), \
1415 				    in_interrupt() ? GFP_ATOMIC : GFP_KERNEL)
1416 #  define	KFREE(x)	kfree(x)
1417 #  define	KMALLOCS(x,t,s)	(x) = (t)kmalloc((s), \
1418 				    in_interrupt() ? GFP_ATOMIC : GFP_KERNEL)
1419 #  define	KFREES(x,s)	kfree(x)
1420 
1421 #  if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
1422 #   define	f_uid		f_owner.uid
1423 #   define	GETIFP(n,v)	dev_get_by_name(&init_net, n)
1424 #  else
1425 #   define	GETIFP(n,v)	dev_get_by_name(n)
1426 #  endif
1427 #  define	GETIFMTU_4(x)	((struct net_device *)x)->mtu
1428 #  define	GETIFMTU_6(x)	((struct net_device *)x)->mtu
1429 
1430 # else
1431 #  include <net/ethernet.h>
1432 
1433 struct mbuf {
1434 };
1435 
1436 #  ifndef _NET_ROUTE_H
1437 struct rtentry {
1438 };
1439 #  endif
1440 
1441 struct ifnet {
1442 	char	if_xname[IFNAMSIZ];
1443 	int	if_unit;
1444 	int	(* if_output)(struct ifnet *, struct mbuf *, struct sockaddr *, struct rtentry *);
1445 	struct	ifaddr	*if_addrlist;
1446 };
1447 # define	IFNAME(x)	((struct ifnet *)x)->if_xname
1448 
1449 # endif	/* _KERNEL */
1450 
1451 # define	COPYIFNAME(v, x, b) \
1452 				(void) strncpy(b, \
1453 					       ((struct ifnet *)x)->if_xname, \
1454 					       LIFNAMSIZ)
1455 
1456 # include <linux/fs.h>
1457 # define	FWRITE	FMODE_WRITE
1458 # define	FREAD	FMODE_READ
1459 
1460 # define	__USE_MISC	1
1461 # define	__FAVOR_BSD	1
1462 
1463 typedef	struct uio {
1464 	struct iovec	*uio_iov;
1465 	void	*uio_file;
1466 	char	*uio_buf;
1467 	int	uio_iovcnt;
1468 	int	uio_offset;
1469 	size_t	uio_resid;
1470 	int	uio_rw;
1471 } uio_t;
1472 
1473 extern	int	uiomove(void *, size_t, int, struct uio *);
1474 
1475 # define	UIO_READ	1
1476 # define	UIO_WRITE	2
1477 
1478 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)) && !defined(_KERNEL)
1479 typedef int		fmode_t;
1480 # endif
1481 
1482 typedef	u_long		ioctlcmd_t;
1483 typedef	int		minor_t;
1484 typedef u_int32_t 	u_32_t;
1485 # define	U_32_T	1
1486 
1487 # define OS_RECOGNISED 1
1488 
1489 #endif
1490 
1491 
1492 /* ----------------------------------------------------------------------- */
1493 /*                                    A I X                                */
1494 /* ----------------------------------------------------------------------- */
1495 #if defined(_AIX51)
1496 # undef		MENTAT
1497 
1498 # include <sys/lock.h>
1499 # include <sys/sysmacros.h>
1500 
1501 # ifdef _KERNEL
1502 #  define rw_read_locked(x)		0
1503 #  include <net/net_globals.h>
1504 #  include <net/net_malloc.h>
1505 #  define	KMUTEX_T		simple_lock_t
1506 #  define	KRWLOCK_T		complex_lock_t
1507 #  define	USE_MUTEXES		1
1508 #  define	USE_SPL			1
1509 #  define	READ_ENTER(x)		lock_read((x)->ipf_lk)
1510 #  define	WRITE_ENTER(x)		lock_write((x)->ipf_lk)
1511 #  define	MUTEX_DOWNGRADE(x)	lock_write_to_read((x)->ipf_lk)
1512 #  define	RWLOCK_INIT(x, y)	lock_alloc(&(x)->ipf_lk, \
1513 						   LOCK_ALLOC_PIN, \
1514 						   (u_short)y, 0); \
1515 					lock_init((x)->ipf_lk, TRUE)
1516 #  define	RWLOCK_EXIT(x)		lock_done((x)->ipf_lk)
1517 #  define	RW_DESTROY(x)		lock_free(&(x)->ipf_lk)
1518 #  define	MUTEX_ENTER(x)		simple_lock((x)->ipf_lk)
1519 #  define	MUTEX_INIT(x, y)	lock_alloc(&(x)->ipf_lk, \
1520 						   LOCK_ALLOC_PIN, \
1521 						   (u_short)y, 0); \
1522 					simple_lock_init((x)->ipf_lk)
1523 #  define	MUTEX_DESTROY(x)	lock_free(&(x)->ipf_lk)
1524 #  define	MUTEX_EXIT(x)		simple_unlock((x)->ipf_lk)
1525 #  define	MUTEX_NUKE(x)		bzero(&(x)->ipf_lk, sizeof((x)->ipf_lk))
1526 #   define	ATOMIC_INC64(x)		{ MUTEX_ENTER(&softc->ipf_rw); (x)++; \
1527 					  MUTEX_EXIT(&softc->ipf_rw); }
1528 #   define	ATOMIC_DEC64(x)		{ MUTEX_ENTER(&softc->ipf_rw); (x)--; \
1529 					  MUTEX_EXIT(&softc->ipf_rw); }
1530 #   define	ATOMIC_INC32(x)		{ MUTEX_ENTER(&softc->ipf_rw); (x)++; \
1531 					  MUTEX_EXIT(&softc->ipf_rw); }
1532 #   define	ATOMIC_DEC32(x)		{ MUTEX_ENTER(&softc->ipf_rw); (x)--; \
1533 					  MUTEX_EXIT(&softc->ipf_rw); }
1534 #   define	ATOMIC_INCL(x)		{ MUTEX_ENTER(&softc->ipf_rw); (x)++; \
1535 					  MUTEX_EXIT(&softc->ipf_rw); }
1536 #   define	ATOMIC_DECL(x)		{ MUTEX_ENTER(&softc->ipf_rw); (x)--; \
1537 					  MUTEX_EXIT(&softc->ipf_rw); }
1538 #   define	ATOMIC_INC(x)		{ MUTEX_ENTER(&softc->ipf_rw); (x)++; \
1539 					  MUTEX_EXIT(&softc->ipf_rw); }
1540 #   define	ATOMIC_DEC(x)		{ MUTEX_ENTER(&softc->ipf_rw); (x)--; \
1541 					  MUTEX_EXIT(&softc->ipf_rw); }
1542 #  define	SPL_SCHED(x)		x = splsched()
1543 #  define	SPL_NET(x)		x = splnet()
1544 #  define	SPL_IMP(x)		x = splimp()
1545 #  undef	SPL_X
1546 #  define	SPL_X(x)		splx(x)
1547 #  define	UIOMOVE(a,b,c,d)	uiomove((void *)a,b,c,d)
1548 extern void* getifp(char *, int);
1549 #  define	GETIFP(n, v)		getifp(n, v)
1550 #  define	GETIFMTU_4(x)		((struct ifnet *)x)->if_mtu
1551 #  define	GETIFMTU_6(x)		((struct ifnet *)x)->if_mtu
1552 #  define	GET_MINOR		minor
1553 #  define	SLEEP(id, n)	sleepx((id), PZERO+1, 0)
1554 #  define	WAKEUP(id,x)	wakeup(id)
1555 #  define	POLLWAKEUP(x)	;
1556 #  define	COPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
1557 #  define	COPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
1558 #  define	KMALLOC(a, b)	MALLOC((a), b, sizeof(*(a)), M_TEMP, M_NOWAIT)
1559 #  define	KMALLOCS(a, b, c)	MALLOC((a), b, (c), M_TEMP, \
1560 					    ((c) > 4096) ? M_WAITOK : M_NOWAIT)
1561 #  define	KFREE(x)	FREE((x), M_TEMP)
1562 #  define	KFREES(x,s)	FREE((x), M_TEMP)
1563 #  define	MSGDSIZE(m)	mbufchainlen(m)
1564 #  define	M_LEN(m)	(m)->m_len
1565 #  define	M_ADJ(m,x)	m_adj(m, x)
1566 #  define	M_COPY(m)	m_copy((m), 0, M_COPYALL)
1567 #  define	GETKTIME(x)
1568 #  define	IPF_PANIC(x,y)
1569 typedef struct mbuf mb_t;
1570 # endif /* _KERNEL */
1571 
1572 /*
1573  * These are from's Solaris' #defines for little endian.
1574  */
1575 #if !defined(IP6F_MORE_FRAG)
1576 # define	IP6F_MORE_FRAG		0x0100
1577 #endif
1578 #if !defined(IP6F_RESERVED_MASK)
1579 # define	IP6F_RESERVED_MASK	0x0600
1580 #endif
1581 #if !defined(IP6F_OFF_MASK)
1582 # define	IP6F_OFF_MASK		0xf8ff
1583 #endif
1584 
1585 struct ip6_ext {
1586 	u_char	ip6e_nxt;
1587 	u_char	ip6e_len;
1588 };
1589 
1590 typedef	int		ioctlcmd_t;
1591 typedef	int		minor_t;
1592 /*
1593  * Really, any arch where sizeof(long) != sizeof(int).
1594  */
1595 typedef unsigned int    u_32_t;
1596 # define	U_32_T	1
1597 
1598 # define OS_RECOGNISED 1
1599 #endif	/* _AIX51 */
1600 
1601 
1602 #ifndef	OS_RECOGNISED
1603 #error	ip_compat.h does not recognise this platform/OS.
1604 #endif
1605 
1606 
1607 /* ----------------------------------------------------------------------- */
1608 /*                           G E N E R I C                                 */
1609 /* ----------------------------------------------------------------------- */
1610 #ifndef OS_RECOGNISED
1611 #endif
1612 
1613 /*
1614  * For BSD kernels, if bpf is in the kernel, enable ipfilter to use bpf in
1615  * filter rules.
1616  */
1617 #if !defined(IPFILTER_BPF)
1618 # if (defined(NBPF) && (NBPF > 0)) || (defined(DEV_BPF) && (DEV_BPF > 0)) || \
1619      (defined(NBPFILTER) && (NBPFILTER > 0))
1620 #  define	IPFILTER_BPF
1621 # endif
1622 #endif
1623 
1624 /*
1625  * Userland locking primitives
1626  */
1627 #if !defined(KMUTEX_FILL_SZ)
1628 # define	KMUTEX_FILL_SZ	1
1629 #endif
1630 #if !defined(KRWLOCK_FILL_SZ)
1631 # define	KRWLOCK_FILL_SZ	1
1632 #endif
1633 
1634 typedef	struct	{
1635 	char	*eMm_owner;
1636 	char	*eMm_heldin;
1637 	u_int	eMm_magic;
1638 	int	eMm_held;
1639 	int	eMm_heldat;
1640 } eMmutex_t;
1641 
1642 typedef	struct	{
1643 	char	*eMrw_owner;
1644 	char	*eMrw_heldin;
1645 	u_int	eMrw_magic;
1646 	short	eMrw_read;
1647 	short	eMrw_write;
1648 	int	eMrw_heldat;
1649 } eMrwlock_t;
1650 
1651 typedef union {
1652 	char	_fill[KMUTEX_FILL_SZ];
1653 #ifdef KMUTEX_T
1654 	struct	{
1655 		KMUTEX_T	ipf_slk;
1656 		const char	*ipf_lname;
1657 	} ipf_lkun_s;
1658 #endif
1659 	eMmutex_t	ipf_emu;
1660 } ipfmutex_t;
1661 
1662 typedef union {
1663 	char	_fill[KRWLOCK_FILL_SZ];
1664 #ifdef KRWLOCK_T
1665 	struct	{
1666 		KRWLOCK_T	ipf_slk;
1667 		const char	*ipf_lname;
1668 		int		ipf_sr;
1669 		int		ipf_sw;
1670 		u_int		ipf_magic;
1671 	} ipf_lkun_s;
1672 #endif
1673 	eMrwlock_t	ipf_emu;
1674 } ipfrwlock_t;
1675 
1676 #define	ipf_lk		ipf_lkun_s.ipf_slk
1677 #define	ipf_lname	ipf_lkun_s.ipf_lname
1678 #define	ipf_isr		ipf_lkun_s.ipf_sr
1679 #define	ipf_isw		ipf_lkun_s.ipf_sw
1680 #define	ipf_magic	ipf_lkun_s.ipf_magic
1681 
1682 #if !defined(__GNUC__) || \
1683     (defined(__FreeBSD_version) && (__FreeBSD_version >= 503000))
1684 # ifndef	INLINE
1685 #  define	INLINE
1686 # endif
1687 #else
1688 # define	INLINE	__inline__
1689 #endif
1690 
1691 #ifndef EXTERN_INLINE
1692 # if defined(__GNUC__) && !defined(__GNUC_STDC_INLINE__)
1693 #  define	EXTERN_INLINE	inline
1694 # else
1695 #  define	EXTERN_INLINE	extern inline
1696 # endif
1697 #endif
1698 
1699 #if defined(linux) && defined(_KERNEL)
1700 extern	void	ipf_read_enter(ipfrwlock_t *);
1701 extern	void	ipf_write_enter(ipfrwlock_t *);
1702 extern	void	ipf_rw_exit(ipfrwlock_t *);
1703 extern	void	ipf_rw_init(ipfrwlock_t *, char *);
1704 extern	void	ipf_rw_downgrade(ipfrwlock_t *);
1705 #endif
1706 
1707 /*
1708  * In a non-kernel environment, there are a lot of macros that need to be
1709  * filled in to be null-ops or to point to some compatibility function,
1710  * somewhere in userland.
1711  */
1712 #ifndef _KERNEL
1713 typedef	struct	mb_s	{
1714 	struct	mb_s	*mb_next;
1715 	char		*mb_data;
1716 	void		*mb_ifp;
1717 	int		mb_len;
1718 	int		mb_flags;
1719 	u_long		mb_buf[2048];
1720 } mb_t;
1721 # undef		m_next
1722 # define	m_next		mb_next
1723 # undef		m_len
1724 # define	m_len		mb_len
1725 # undef		m_flags
1726 # define	m_flags		mb_flags
1727 # undef		m_data
1728 # define	m_data		mb_data
1729 # undef		M_MCAST
1730 # define	M_MCAST		0x01
1731 # undef		M_BCAST
1732 # define	M_BCAST		0x02
1733 # undef		M_MBCAST
1734 # define	M_MBCAST	0x04
1735 # define	MSGDSIZE(m)	msgdsize(m)
1736 # define	M_LEN(m)	(m)->mb_len
1737 # define	M_ADJ(m,x)	(m)->mb_len += x
1738 # define	M_COPY(m)	dupmbt(m)
1739 # define	M_DUP(m)	dupmbt(m)
1740 # define	GETKTIME(x)	gettimeofday((struct timeval *)(x), NULL)
1741 # define	MTOD(m, t)	((t)(m)->mb_data)
1742 # define	FREE_MB_T(m)	freembt(m)
1743 # define	ALLOC_MB_T(m,l)	(m) = allocmbt(l)
1744 # define	PREP_MB_T(f, m)	do { \
1745 						(m)->mb_next = *(f)->fin_mp; \
1746 						*(fin)->fin_mp = (m); \
1747 						(f)->fin_m = (m); \
1748 					} while (0)
1749 # define	SLEEP(x,y)	1;
1750 # define	WAKEUP(x,y)	;
1751 # define	POLLWAKEUP(y)	;
1752 # define	IPF_PANIC(x,y)	;
1753 # define	PANIC(x,y)	;
1754 # define	SPL_SCHED(x)	;
1755 # define	SPL_NET(x)	;
1756 # define	SPL_IMP(x)	;
1757 # define	SPL_X(x)	;
1758 # define	KMALLOC(a,b)	(a) = (b)malloc(sizeof(*a))
1759 # define	KMALLOCS(a,b,c)	(a) = (b)malloc(c)
1760 # define	KFREE(x)	free(x)
1761 # define	KFREES(x,s)	free(x)
1762 # define	GETIFP(x, v)	get_unit(x,v)
1763 # define	GETIFMTU_4(x)	2048
1764 # define	GETIFMTU_6(x)	2048
1765 # define	COPYIN(a,b,c)	bcopywrap((a), (b), (c))
1766 # define	COPYOUT(a,b,c)	bcopywrap((a), (b), (c))
1767 # define	COPYDATA(m, o, l, b)	bcopy(MTOD((mb_t *)m, char *) + (o), \
1768 					      (b), (l))
1769 # define	COPYBACK(m, o, l, b)	bcopy((b), \
1770 					      MTOD((mb_t *)m, char *) + (o), \
1771 					      (l))
1772 # define	UIOMOVE(a,b,c,d)	ipfuiomove(a,b,c,d)
1773 extern	void	m_copydata(mb_t *, int, int, void *);
1774 extern	int	ipfuiomove(void *, int, int, struct uio *);
1775 extern	int	bcopywrap(void *, void *, size_t);
1776 extern	mb_t	*allocmbt(size_t);
1777 extern	mb_t	*dupmbt(mb_t *);
1778 extern	void	freembt(mb_t *);
1779 
1780 # define	MUTEX_DESTROY(x)	eMmutex_destroy(&(x)->ipf_emu, \
1781 							__FILE__, __LINE__)
1782 # define	MUTEX_ENTER(x)		eMmutex_enter(&(x)->ipf_emu, \
1783 						      __FILE__, __LINE__)
1784 # define	MUTEX_EXIT(x)		eMmutex_exit(&(x)->ipf_emu, \
1785 						     __FILE__, __LINE__)
1786 # define	MUTEX_INIT(x,y)		eMmutex_init(&(x)->ipf_emu, y, \
1787 						     __FILE__, __LINE__)
1788 # define	MUTEX_NUKE(x)		bzero((x), sizeof(*(x)))
1789 
1790 # define	MUTEX_DOWNGRADE(x)	eMrwlock_downgrade(&(x)->ipf_emu, \
1791 							   __FILE__, __LINE__)
1792 # define	READ_ENTER(x)		eMrwlock_read_enter(&(x)->ipf_emu, \
1793 							    __FILE__, __LINE__)
1794 # define	RWLOCK_INIT(x, y)	eMrwlock_init(&(x)->ipf_emu, y)
1795 # define	RWLOCK_EXIT(x)		eMrwlock_exit(&(x)->ipf_emu)
1796 # define	RW_DESTROY(x)		eMrwlock_destroy(&(x)->ipf_emu)
1797 # define	WRITE_ENTER(x)		eMrwlock_write_enter(&(x)->ipf_emu, \
1798 							     __FILE__, \
1799 							     __LINE__)
1800 
1801 # define	USE_MUTEXES		1
1802 
1803 extern void eMmutex_destroy(eMmutex_t *, char *, int);
1804 extern void eMmutex_enter(eMmutex_t *, char *, int);
1805 extern void eMmutex_exit(eMmutex_t *, char *, int);
1806 extern void eMmutex_init(eMmutex_t *, char *, char *, int);
1807 extern void eMrwlock_destroy(eMrwlock_t *);
1808 extern void eMrwlock_exit(eMrwlock_t *);
1809 extern void eMrwlock_init(eMrwlock_t *, char *);
1810 extern void eMrwlock_read_enter(eMrwlock_t *, char *, int);
1811 extern void eMrwlock_write_enter(eMrwlock_t *, char *, int);
1812 extern void eMrwlock_downgrade(eMrwlock_t *, char *, int);
1813 
1814 #endif
1815 
1816 extern	mb_t	*allocmbt(size_t);
1817 
1818 #define	MAX_IPV4HDR	((0xf << 2) + sizeof(struct icmp) + sizeof(ip_t) + 8)
1819 
1820 #ifndef	IP_OFFMASK
1821 # define	IP_OFFMASK	0x1fff
1822 #endif
1823 
1824 
1825 /*
1826  * On BSD's use quad_t as a guarantee for getting at least a 64bit sized
1827  * object.
1828  */
1829 #if !defined(__amd64__) && BSD_GT_YEAR(199306)
1830 # define	USE_QUAD_T
1831 # define	U_QUAD_T	u_quad_t
1832 # define	QUAD_T		quad_t
1833 #else /* BSD > 199306 */
1834 # if !defined(U_QUAD_T)
1835 #  define	U_QUAD_T	u_long
1836 #  define	QUAD_T		long
1837 # endif
1838 #endif /* BSD > 199306 */
1839 
1840 
1841 #ifdef	USE_INET6
1842 # if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || \
1843      defined(__osf__) || defined(linux)
1844 #  include <netinet/ip6.h>
1845 #  include <netinet/icmp6.h>
1846 #  if !defined(linux)
1847 #   if defined(_KERNEL) && !defined(__osf__)
1848 #    include <netinet6/ip6_var.h>
1849 #   endif
1850 #  endif
1851 typedef	struct ip6_hdr	ip6_t;
1852 # endif
1853 #endif
1854 
1855 #ifndef	MAX
1856 # define	MAX(a,b)	(((a) > (b)) ? (a) : (b))
1857 #endif
1858 
1859 #if defined(_KERNEL)
1860 # if defined(MENTAT) && !defined(INSTANCES)
1861 #  define	COPYDATA	mb_copydata
1862 #  define	COPYBACK	mb_copyback
1863 # else
1864 #  define	COPYDATA	m_copydata
1865 #  define	COPYBACK	m_copyback
1866 # endif
1867 # if BSD_GE_YEAR(199306) || defined(__FreeBSD__)
1868 #  if (defined(__NetBSD_Version__) && (__NetBSD_Version__ < 105180000)) || \
1869        defined(__FreeBSD__) || (defined(OpenBSD) && (OpenBSD < 200206)) || \
1870        defined(_BSDI_VERSION)
1871 #   include <vm/vm.h>
1872 #  endif
1873 #  if !defined(__FreeBSD__) || FREEBSD_GE_REV(300000)
1874 #   if NETBSD_GE_REV(105180000) || OPENBSD_GE_REV(200111)
1875 /* #    include <uvm/uvm_extern.h> */
1876 #   else
1877 #    include <vm/vm_extern.h>
1878 extern  vm_map_t        kmem_map;
1879 #   endif
1880 #   include <sys/proc.h>
1881 #  else /* !__FreeBSD__ || (__FreeBSD__ && __FreeBSD_version >= 300000) */
1882 #   include <vm/vm_kern.h>
1883 #  endif /* !__FreeBSD__ || (__FreeBSD__ && __FreeBSD_version >= 300000) */
1884 
1885 #  ifdef IPFILTER_M_IPFILTER
1886 #    include <sys/malloc.h>
1887 MALLOC_DECLARE(M_IPFILTER);
1888 #    define	_M_IPF		M_IPFILTER
1889 #  else /* IPFILTER_M_IPFILTER */
1890 #   ifdef M_PFIL
1891 #    define	_M_IPF		M_PFIL
1892 #   else
1893 #    ifdef M_IPFILTER
1894 #     define	_M_IPF		M_IPFILTER
1895 #    else
1896 #     define	_M_IPF		M_TEMP
1897 #    endif /* M_IPFILTER */
1898 #   endif /* M_PFIL */
1899 #  endif /* IPFILTER_M_IPFILTER */
1900 #  if !defined(KMALLOC)
1901 #   define	KMALLOC(a, b)	(a) = (b)malloc(sizeof(*(a)), _M_IPF, M_NOWAIT)
1902 #  endif
1903 #  if !defined(KMALLOCS)
1904 #   define	KMALLOCS(a, b, c)	(a) = (b)malloc((c), _M_IPF, M_NOWAIT)
1905 #  endif
1906 #  if !defined(KFREE)
1907 #   define	KFREE(x)	free((x), _M_IPF)
1908 #  endif
1909 #  if !defined(KFREES)
1910 #   define	KFREES(x,s)	free((x), _M_IPF)
1911 #  endif
1912 #  define	UIOMOVE(a,b,c,d)	uiomove((void *)a,b,d)
1913 #  define	SLEEP(id, n)	tsleep((id), PPAUSE|PCATCH, n, 0)
1914 #  define	WAKEUP(id,x)	wakeup(id+x)
1915 #  if !defined(POLLWAKEUP)
1916 #   define	POLLWAKEUP(x)	selwakeup(softc->ipf_selwait+x)
1917 #  endif
1918 #  define	GETIFP(n, v)	ifunit(n)
1919 #  define	GETIFMTU_4(x)	((struct ifnet *)x)->if_mtu
1920 #  define	GETIFMTU_6(x)	((struct ifnet *)x)->if_mtu
1921 # endif /* (Free)BSD */
1922 
1923 # if !defined(USE_MUTEXES) && !defined(SPL_NET)
1924 #  if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199407)) || \
1925       OPENBSD_GE_REV(200006)
1926 #   define	SPL_NET(x)	x = splsoftnet()
1927 #  else
1928 #   define	SPL_IMP(x)	x = splimp()
1929 #   define	SPL_NET(x)	x = splnet()
1930 #  endif /* NetBSD && (NetBSD <= 1991011) && (NetBSD >= 199407) */
1931 #  if !defined(SPL_SCHED)
1932 #   define	SPL_SCHED(x)	x = splsched()
1933 #  endif
1934 #  define	SPL_X(x)	(void) splx(x)
1935 # endif /* !USE_MUTEXES */
1936 
1937 # ifndef FREE_MB_T
1938 #  define	FREE_MB_T(m)	m_freem(m)
1939 # endif
1940 # ifndef ALLOC_MB_T
1941 #  ifdef MGETHDR
1942 #   define	ALLOC_MB_T(m,l)	do { \
1943 					MGETHDR((m), M_DONTWAIT, MT_HEADER); \
1944 					if ((m) != NULL) { \
1945 						(m)->m_len = (l); \
1946 						(m)->m_pkthdr.len = (l); \
1947 					} \
1948 				} while (0)
1949 #  else
1950 #   define	ALLOC_MB_T(m,l)	do { \
1951 					MGET((m), M_DONTWAIT, MT_HEADER); \
1952 					if ((m) != NULL) { \
1953 						(m)->m_len = (l); \
1954 						(m)->m_pkthdr.len = (l); \
1955 					} \
1956 				} while (0)
1957 #  endif
1958 # endif
1959 # ifndef PREP_MB_T
1960 #  define	PREP_MB_T(f, m)	do { \
1961 						mb_t *_o = *(f)->fin_mp; \
1962 						(m)->m_next = _o; \
1963 						*(fin)->fin_mp = (m); \
1964 						if (_o->m_flags & M_PKTHDR) { \
1965 							(m)->m_pkthdr.len += \
1966 							    _o->m_pkthdr.len; \
1967 							m_copy_rcvif((m), _o); \
1968 						} \
1969 					} while (0)
1970 # endif
1971 # ifndef M_DUP
1972 #  ifdef M_COPYALL
1973 #   define	M_DUP(m)	m_dup(m, 0, M_COPYALL, 0)
1974 #  else
1975 #   define	M_DUP(m)	m_dup(m)
1976 #  endif
1977 # endif
1978 
1979 # ifndef MTOD
1980 #  define	MTOD(m,t)	mtod(m,t)
1981 # endif
1982 
1983 # ifndef COPYIN
1984 #  define	COPYIN(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
1985 #  define	COPYOUT(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
1986 # endif
1987 
1988 # ifndef KMALLOC
1989 #  define	KMALLOC(a,b)	(a) = (b)new_kmem_alloc(sizeof(*(a)), \
1990 							KMEM_NOSLEEP)
1991 #  define	KMALLOCS(a,b,c)	(a) = (b)new_kmem_alloc((c), KMEM_NOSLEEP)
1992 # endif
1993 
1994 # ifndef	GET_MINOR
1995 #  define	GET_MINOR(x)	minor(x)
1996 # endif
1997 # define	PANIC(x,y)	if (x) panic y
1998 #endif /* _KERNEL */
1999 
2000 #if !defined(IFNAME) && !defined(_KERNEL)
2001 # define	IFNAME(x)	get_ifname((struct ifnet *)x)
2002 #endif
2003 #ifndef	COPYIFNAME
2004 # define	NEED_FRGETIFNAME
2005 extern	char	*ipf_getifname(struct ifnet *, char *);
2006 # define	COPYIFNAME(v, x, b) \
2007 				ipf_getifname((struct ifnet *)x, b)
2008 #endif
2009 
2010 #ifndef ASSERT
2011 # ifdef _KERNEL
2012 #  define	ASSERT(x)
2013 # else
2014 #  define	ASSERT(x)	do { if (!(x)) abort(); } while (0)
2015 # endif
2016 #endif
2017 
2018 #ifndef BCOPYIN
2019 #  define	BCOPYIN(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
2020 #  define	BCOPYOUT(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
2021 #endif
2022 
2023 /*
2024  * Because the ctype(3) posix definition, if used "safely" in code everywhere,
2025  * would mean all normal code that walks through strings needed casts.  Yuck.
2026  */
2027 #define	ISALNUM(x)	isalnum((u_char)(x))
2028 #define	ISALPHA(x)	isalpha((u_char)(x))
2029 #define	ISDIGIT(x)	isdigit((u_char)(x))
2030 #define	ISSPACE(x)	isspace((u_char)(x))
2031 #define	ISUPPER(x)	isupper((u_char)(x))
2032 #define	ISXDIGIT(x)	isxdigit((u_char)(x))
2033 #define	ISLOWER(x)	islower((u_char)(x))
2034 #define	TOUPPER(x)	toupper((u_char)(x))
2035 #define	TOLOWER(x)	tolower((u_char)(x))
2036 
2037 /*
2038  * If mutexes aren't being used, turn all the mutex functions into null-ops.
2039  */
2040 #if !defined(USE_MUTEXES)
2041 # define	USE_SPL			1
2042 # undef		RW_DESTROY
2043 # undef		MUTEX_INIT
2044 # undef		MUTEX_NUKE
2045 # undef		MUTEX_DESTROY
2046 # define	MUTEX_ENTER(x)		;
2047 # define	READ_ENTER(x)		;
2048 # define	WRITE_ENTER(x)		;
2049 # define	MUTEX_DOWNGRADE(x)	;
2050 # define	RWLOCK_INIT(x, y)	;
2051 # define	RWLOCK_EXIT(x)		;
2052 # define	RW_DESTROY(x)		;
2053 # define	MUTEX_EXIT(x)		;
2054 # define	MUTEX_INIT(x,y)		;
2055 # define	MUTEX_DESTROY(x)	;
2056 # define	MUTEX_NUKE(x)		;
2057 #endif /* !USE_MUTEXES */
2058 #ifndef	ATOMIC_INC
2059 # define	ATOMIC_INC(x)		(x)++
2060 # define	ATOMIC_DEC(x)		(x)--
2061 #endif
2062 
2063 #if defined(USE_SPL) && defined(_KERNEL)
2064 # define	SPL_INT(x)	int x
2065 #else
2066 # define	SPL_INT(x)
2067 #endif
2068 
2069 /*
2070  * If there are no atomic operations for bit sizes defined, define them to all
2071  * use a generic one that works for all sizes.
2072  */
2073 #ifndef	ATOMIC_INCL
2074 # define	ATOMIC_INCL		ATOMIC_INC
2075 # define	ATOMIC_INC64		ATOMIC_INC
2076 # define	ATOMIC_INC32		ATOMIC_INC
2077 # define	ATOMIC_DECL		ATOMIC_DEC
2078 # define	ATOMIC_DEC64		ATOMIC_DEC
2079 # define	ATOMIC_DEC32		ATOMIC_DEC
2080 #endif
2081 
2082 #ifndef HDR_T_PRIVATE
2083 typedef	struct	tcphdr	tcphdr_t;
2084 typedef	struct	udphdr	udphdr_t;
2085 #endif
2086 typedef	struct	icmp	icmphdr_t;
2087 typedef	struct	ip	ip_t;
2088 typedef	struct	ether_header	ether_header_t;
2089 typedef	struct	tcpiphdr	tcpiphdr_t;
2090 
2091 #ifndef	FR_GROUPLEN
2092 # define	FR_GROUPLEN	16
2093 #endif
2094 
2095 #ifndef offsetof
2096 # define offsetof(t,m) (size_t)((&((t *)0L)->m))
2097 #endif
2098 #ifndef stsizeof
2099 # define stsizeof(t,m)	sizeof(((t *)0L)->m)
2100 #endif
2101 
2102 /*
2103  * This set of macros has been brought about because on Tru64 it is not
2104  * possible to easily assign or examine values in a structure that are
2105  * bit fields.
2106  */
2107 #ifndef IP_V
2108 # define	IP_V(x)		(x)->ip_v
2109 #endif
2110 #ifndef	IP_V_A
2111 # define	IP_V_A(x,y)	(x)->ip_v = (y)
2112 #endif
2113 #ifndef	IP_HL
2114 # define	IP_HL(x)	(x)->ip_hl
2115 #endif
2116 #ifndef	IP_HL_A
2117 # define	IP_HL_A(x,y)	(x)->ip_hl = ((y) & 0xf)
2118 #endif
2119 #ifndef	TCP_X2
2120 # define	TCP_X2(x)	(x)->th_x2
2121 #endif
2122 #ifndef	TCP_X2_A
2123 # define	TCP_X2_A(x,y)	(x)->th_x2 = (y)
2124 #endif
2125 #ifndef	TCP_OFF
2126 # define	TCP_OFF(x)	(x)->th_off
2127 #endif
2128 #ifndef	TCP_OFF_A
2129 # define	TCP_OFF_A(x,y)	(x)->th_off = (y)
2130 #endif
2131 #define	IPMINLEN(i, h)	((i)->ip_len >= (IP_HL(i) * 4 + sizeof(struct h)))
2132 
2133 
2134 /*
2135  * XXX - This is one of those *awful* hacks which nobody likes
2136  */
2137 #ifdef	ultrix
2138 #define	A_A
2139 #else
2140 #define	A_A	&
2141 #endif
2142 
2143 #define	TCPF_ALL	(TH_FIN|TH_SYN|TH_RST|TH_PUSH|TH_ACK|TH_URG|\
2144 			 TH_ECN|TH_CWR)
2145 
2146 #if BSD_GE_YEAR(199306) && !defined(m_act)
2147 # define	m_act	m_nextpkt
2148 #endif
2149 
2150 /*
2151  * Security Options for Intenet Protocol (IPSO) as defined in RFC 1108.
2152  *
2153  * Basic Option
2154  *
2155  * 00000001   -   (Reserved 4)
2156  * 00111101   -   Top Secret
2157  * 01011010   -   Secret
2158  * 10010110   -   Confidential
2159  * 01100110   -   (Reserved 3)
2160  * 11001100   -   (Reserved 2)
2161  * 10101011   -   Unclassified
2162  * 11110001   -   (Reserved 1)
2163  */
2164 #define	IPSO_CLASS_RES4		0x01
2165 #define	IPSO_CLASS_TOPS		0x3d
2166 #define	IPSO_CLASS_SECR		0x5a
2167 #define	IPSO_CLASS_CONF		0x96
2168 #define	IPSO_CLASS_RES3		0x66
2169 #define	IPSO_CLASS_RES2		0xcc
2170 #define	IPSO_CLASS_UNCL		0xab
2171 #define	IPSO_CLASS_RES1		0xf1
2172 
2173 #define	IPSO_AUTH_GENSER	0x80
2174 #define	IPSO_AUTH_ESI		0x40
2175 #define	IPSO_AUTH_SCI		0x20
2176 #define	IPSO_AUTH_NSA		0x10
2177 #define	IPSO_AUTH_DOE		0x08
2178 #define	IPSO_AUTH_UN		0x06
2179 #define	IPSO_AUTH_FTE		0x01
2180 
2181 /*
2182  * IP option #defines
2183  */
2184 #undef	IPOPT_RR
2185 #define	IPOPT_RR	7
2186 #undef	IPOPT_ZSU
2187 #define	IPOPT_ZSU	10	/* ZSU */
2188 #undef	IPOPT_MTUP
2189 #define	IPOPT_MTUP	11	/* MTUP */
2190 #undef	IPOPT_MTUR
2191 #define	IPOPT_MTUR	12	/* MTUR */
2192 #undef	IPOPT_ENCODE
2193 #define	IPOPT_ENCODE	15	/* ENCODE */
2194 #undef	IPOPT_TS
2195 #define	IPOPT_TS	68
2196 #undef	IPOPT_TR
2197 #define	IPOPT_TR	82	/* TR */
2198 #undef	IPOPT_SECURITY
2199 #define	IPOPT_SECURITY	130
2200 #undef	IPOPT_LSRR
2201 #define	IPOPT_LSRR	131
2202 #undef	IPOPT_E_SEC
2203 #define	IPOPT_E_SEC	133	/* E-SEC */
2204 #undef	IPOPT_CIPSO
2205 #define	IPOPT_CIPSO	134	/* CIPSO */
2206 #undef	IPOPT_SATID
2207 #define	IPOPT_SATID	136
2208 #ifndef	IPOPT_SID
2209 # define	IPOPT_SID	IPOPT_SATID
2210 #endif
2211 #undef	IPOPT_SSRR
2212 #define	IPOPT_SSRR	137
2213 #undef	IPOPT_ADDEXT
2214 #define	IPOPT_ADDEXT	147	/* ADDEXT */
2215 #undef	IPOPT_VISA
2216 #define	IPOPT_VISA	142	/* VISA */
2217 #undef	IPOPT_IMITD
2218 #define	IPOPT_IMITD	144	/* IMITD */
2219 #undef	IPOPT_EIP
2220 #define	IPOPT_EIP	145	/* EIP */
2221 #undef	IPOPT_RTRALRT
2222 #define	IPOPT_RTRALRT	148	/* RTRALRT */
2223 #undef	IPOPT_SDB
2224 #define	IPOPT_SDB	149
2225 #undef	IPOPT_NSAPA
2226 #define	IPOPT_NSAPA	150
2227 #undef	IPOPT_DPS
2228 #define	IPOPT_DPS	151
2229 #undef	IPOPT_UMP
2230 #define	IPOPT_UMP	152
2231 #undef	IPOPT_FINN
2232 #define	IPOPT_FINN	205	/* FINN */
2233 #undef	IPOPT_AH
2234 #define	IPOPT_AH	256+IPPROTO_AH
2235 
2236 #ifndef TCPOPT_EOL
2237 # define TCPOPT_EOL		0
2238 #endif
2239 #ifndef TCPOPT_NOP
2240 # define TCPOPT_NOP		1
2241 #endif
2242 #ifndef TCPOPT_MAXSEG
2243 # define TCPOPT_MAXSEG		2
2244 #endif
2245 #ifndef TCPOLEN_MAXSEG
2246 # define TCPOLEN_MAXSEG		4
2247 #endif
2248 #ifndef TCPOPT_WINDOW
2249 # define TCPOPT_WINDOW		3
2250 #endif
2251 #ifndef TCPOLEN_WINDOW
2252 # define TCPOLEN_WINDOW		3
2253 #endif
2254 #ifndef TCPOPT_SACK_PERMITTED
2255 # define TCPOPT_SACK_PERMITTED	4
2256 #endif
2257 #ifndef TCPOLEN_SACK_PERMITTED
2258 # define TCPOLEN_SACK_PERMITTED	2
2259 #endif
2260 #ifndef TCPOPT_SACK
2261 # define TCPOPT_SACK		5
2262 #endif
2263 #ifndef TCPOPT_TIMESTAMP
2264 # define TCPOPT_TIMESTAMP	8
2265 #endif
2266 
2267 #ifndef	ICMP_MINLEN
2268 # define	ICMP_MINLEN	8
2269 #endif
2270 #ifndef	ICMP_ECHOREPLY
2271 # define	ICMP_ECHOREPLY	0
2272 #endif
2273 #ifndef	ICMP_UNREACH
2274 # define	ICMP_UNREACH	3
2275 #endif
2276 #ifndef	ICMP_UNREACH_NET
2277 # define	ICMP_UNREACH_NET	0
2278 #endif
2279 #ifndef	ICMP_UNREACH_HOST
2280 # define	ICMP_UNREACH_HOST	1
2281 #endif
2282 #ifndef	ICMP_UNREACH_PROTOCOL
2283 # define	ICMP_UNREACH_PROTOCOL	2
2284 #endif
2285 #ifndef	ICMP_UNREACH_PORT
2286 # define	ICMP_UNREACH_PORT	3
2287 #endif
2288 #ifndef	ICMP_UNREACH_NEEDFRAG
2289 # define	ICMP_UNREACH_NEEDFRAG	4
2290 #endif
2291 #ifndef	ICMP_UNREACH_SRCFAIL
2292 # define	ICMP_UNREACH_SRCFAIL	5
2293 #endif
2294 #ifndef	ICMP_UNREACH_NET_UNKNOWN
2295 # define	ICMP_UNREACH_NET_UNKNOWN	6
2296 #endif
2297 #ifndef	ICMP_UNREACH_HOST_UNKNOWN
2298 # define	ICMP_UNREACH_HOST_UNKNOWN	7
2299 #endif
2300 #ifndef	ICMP_UNREACH_ISOLATED
2301 # define	ICMP_UNREACH_ISOLATED	8
2302 #endif
2303 #ifndef	ICMP_UNREACH_NET_PROHIB
2304 # define	ICMP_UNREACH_NET_PROHIB	9
2305 #endif
2306 #ifndef	ICMP_UNREACH_HOST_PROHIB
2307 # define	ICMP_UNREACH_HOST_PROHIB	10
2308 #endif
2309 #ifndef	ICMP_UNREACH_TOSNET
2310 # define	ICMP_UNREACH_TOSNET	11
2311 #endif
2312 #ifndef	ICMP_UNREACH_TOSHOST
2313 # define	ICMP_UNREACH_TOSHOST	12
2314 #endif
2315 #ifndef	ICMP_UNREACH_ADMIN_PROHIBIT
2316 # define	ICMP_UNREACH_ADMIN_PROHIBIT	13
2317 #endif
2318 #ifndef	ICMP_UNREACH_FILTER
2319 # define	ICMP_UNREACH_FILTER	13
2320 #endif
2321 #ifndef	ICMP_UNREACH_HOST_PRECEDENCE
2322 # define	ICMP_UNREACH_HOST_PRECEDENCE	14
2323 #endif
2324 #ifndef	ICMP_UNREACH_PRECEDENCE_CUTOFF
2325 # define	ICMP_UNREACH_PRECEDENCE_CUTOFF	15
2326 #endif
2327 #ifndef	ICMP_SOURCEQUENCH
2328 # define	ICMP_SOURCEQUENCH	4
2329 #endif
2330 #ifndef	ICMP_REDIRECT_NET
2331 # define	ICMP_REDIRECT_NET	0
2332 #endif
2333 #ifndef	ICMP_REDIRECT_HOST
2334 # define	ICMP_REDIRECT_HOST	1
2335 #endif
2336 #ifndef	ICMP_REDIRECT_TOSNET
2337 # define	ICMP_REDIRECT_TOSNET	2
2338 #endif
2339 #ifndef	ICMP_REDIRECT_TOSHOST
2340 # define	ICMP_REDIRECT_TOSHOST	3
2341 #endif
2342 #ifndef	ICMP_ALTHOSTADDR
2343 # define	ICMP_ALTHOSTADDR	6
2344 #endif
2345 #ifndef	ICMP_TIMXCEED
2346 # define	ICMP_TIMXCEED	11
2347 #endif
2348 #ifndef	ICMP_TIMXCEED_INTRANS
2349 # define	ICMP_TIMXCEED_INTRANS	0
2350 #endif
2351 #ifndef	ICMP_TIMXCEED_REASS
2352 # define		ICMP_TIMXCEED_REASS	1
2353 #endif
2354 #ifndef	ICMP_PARAMPROB
2355 # define	ICMP_PARAMPROB	12
2356 #endif
2357 #ifndef	ICMP_PARAMPROB_ERRATPTR
2358 # define	ICMP_PARAMPROB_ERRATPTR	0
2359 #endif
2360 #ifndef	ICMP_PARAMPROB_OPTABSENT
2361 # define	ICMP_PARAMPROB_OPTABSENT	1
2362 #endif
2363 #ifndef	ICMP_PARAMPROB_LENGTH
2364 # define	ICMP_PARAMPROB_LENGTH	2
2365 #endif
2366 #ifndef ICMP_TSTAMP
2367 # define	ICMP_TSTAMP	13
2368 #endif
2369 #ifndef ICMP_TSTAMPREPLY
2370 # define	ICMP_TSTAMPREPLY	14
2371 #endif
2372 #ifndef ICMP_IREQ
2373 # define	ICMP_IREQ	15
2374 #endif
2375 #ifndef ICMP_IREQREPLY
2376 # define	ICMP_IREQREPLY	16
2377 #endif
2378 #ifndef	ICMP_MASKREQ
2379 # define	ICMP_MASKREQ	17
2380 #endif
2381 #ifndef ICMP_MASKREPLY
2382 # define	ICMP_MASKREPLY	18
2383 #endif
2384 #ifndef	ICMP_TRACEROUTE
2385 # define	ICMP_TRACEROUTE	30
2386 #endif
2387 #ifndef	ICMP_DATACONVERR
2388 # define	ICMP_DATACONVERR	31
2389 #endif
2390 #ifndef	ICMP_MOBILE_REDIRECT
2391 # define	ICMP_MOBILE_REDIRECT	32
2392 #endif
2393 #ifndef	ICMP_IPV6_WHEREAREYOU
2394 # define	ICMP_IPV6_WHEREAREYOU	33
2395 #endif
2396 #ifndef	ICMP_IPV6_IAMHERE
2397 # define	ICMP_IPV6_IAMHERE	34
2398 #endif
2399 #ifndef	ICMP_MOBILE_REGREQUEST
2400 # define	ICMP_MOBILE_REGREQUEST	35
2401 #endif
2402 #ifndef	ICMP_MOBILE_REGREPLY
2403 # define	ICMP_MOBILE_REGREPLY	36
2404 #endif
2405 #ifndef	ICMP_SKIP
2406 # define	ICMP_SKIP	39
2407 #endif
2408 #ifndef	ICMP_PHOTURIS
2409 # define	ICMP_PHOTURIS	40
2410 #endif
2411 #ifndef	ICMP_PHOTURIS_UNKNOWN_INDEX
2412 # define	ICMP_PHOTURIS_UNKNOWN_INDEX	1
2413 #endif
2414 #ifndef	ICMP_PHOTURIS_AUTH_FAILED
2415 # define	ICMP_PHOTURIS_AUTH_FAILED	2
2416 #endif
2417 #ifndef	ICMP_PHOTURIS_DECRYPT_FAILED
2418 # define	ICMP_PHOTURIS_DECRYPT_FAILED	3
2419 #endif
2420 #ifndef	IPVERSION
2421 # define	IPVERSION	4
2422 #endif
2423 #ifndef	IPOPT_MINOFF
2424 # define	IPOPT_MINOFF	4
2425 #endif
2426 #ifndef	IPOPT_COPIED
2427 # define	IPOPT_COPIED(x)	((x)&0x80)
2428 #endif
2429 #ifndef	IPOPT_EOL
2430 # define	IPOPT_EOL	0
2431 #endif
2432 #ifndef	IPOPT_NOP
2433 # define	IPOPT_NOP	1
2434 #endif
2435 #ifndef	IP_MF
2436 # define	IP_MF	((u_short)0x2000)
2437 #endif
2438 #ifndef	ETHERTYPE_IP
2439 # define	ETHERTYPE_IP	((u_short)0x0800)
2440 #endif
2441 #ifndef	TH_FIN
2442 # define	TH_FIN	0x01
2443 #endif
2444 #ifndef	TH_SYN
2445 # define	TH_SYN	0x02
2446 #endif
2447 #ifndef	TH_RST
2448 # define	TH_RST	0x04
2449 #endif
2450 #ifndef	TH_PUSH
2451 # define	TH_PUSH	0x08
2452 #endif
2453 #ifndef	TH_ACK
2454 # define	TH_ACK	0x10
2455 #endif
2456 #ifndef	TH_URG
2457 # define	TH_URG	0x20
2458 #endif
2459 #undef	TH_ACKMASK
2460 #define	TH_ACKMASK	(TH_FIN|TH_SYN|TH_RST|TH_ACK)
2461 
2462 #ifndef	IPOPT_EOL
2463 # define	IPOPT_EOL	0
2464 #endif
2465 #ifndef	IPOPT_NOP
2466 # define	IPOPT_NOP	1
2467 #endif
2468 #ifndef	IPOPT_RR
2469 # define	IPOPT_RR	7
2470 #endif
2471 #ifndef	IPOPT_TS
2472 # define	IPOPT_TS	68
2473 #endif
2474 #ifndef	IPOPT_SECURITY
2475 # define	IPOPT_SECURITY	130
2476 #endif
2477 #ifndef	IPOPT_LSRR
2478 # define	IPOPT_LSRR	131
2479 #endif
2480 #ifndef	IPOPT_SATID
2481 # define	IPOPT_SATID	136
2482 #endif
2483 #ifndef	IPOPT_SSRR
2484 # define	IPOPT_SSRR	137
2485 #endif
2486 #ifndef	IPOPT_SECUR_UNCLASS
2487 # define	IPOPT_SECUR_UNCLASS	((u_short)0x0000)
2488 #endif
2489 #ifndef	IPOPT_SECUR_CONFID
2490 # define	IPOPT_SECUR_CONFID	((u_short)0xf135)
2491 #endif
2492 #ifndef	IPOPT_SECUR_EFTO
2493 # define	IPOPT_SECUR_EFTO	((u_short)0x789a)
2494 #endif
2495 #ifndef	IPOPT_SECUR_MMMM
2496 # define	IPOPT_SECUR_MMMM	((u_short)0xbc4d)
2497 #endif
2498 #ifndef	IPOPT_SECUR_RESTR
2499 # define	IPOPT_SECUR_RESTR	((u_short)0xaf13)
2500 #endif
2501 #ifndef	IPOPT_SECUR_SECRET
2502 # define	IPOPT_SECUR_SECRET	((u_short)0xd788)
2503 #endif
2504 #ifndef IPOPT_SECUR_TOPSECRET
2505 # define	IPOPT_SECUR_TOPSECRET	((u_short)0x6bc5)
2506 #endif
2507 #ifndef IPOPT_OLEN
2508 # define	IPOPT_OLEN	1
2509 #endif
2510 #ifndef	IPPROTO_HOPOPTS
2511 # define	IPPROTO_HOPOPTS	0
2512 #endif
2513 #ifndef	IPPROTO_IPIP
2514 # define	IPPROTO_IPIP	4
2515 #endif
2516 #ifndef	IPPROTO_ENCAP
2517 # define	IPPROTO_ENCAP	98
2518 #endif
2519 #ifndef	IPPROTO_IPV6
2520 # define	IPPROTO_IPV6	41
2521 #endif
2522 #ifndef	IPPROTO_ROUTING
2523 # define	IPPROTO_ROUTING	43
2524 #endif
2525 #ifndef	IPPROTO_FRAGMENT
2526 # define	IPPROTO_FRAGMENT	44
2527 #endif
2528 #ifndef	IPPROTO_GRE
2529 # define	IPPROTO_GRE	47	/* GRE encaps RFC 1701 */
2530 #endif
2531 #ifndef	IPPROTO_ESP
2532 # define	IPPROTO_ESP	50
2533 #endif
2534 #ifndef	IPPROTO_AH
2535 # define	IPPROTO_AH	51
2536 #endif
2537 #ifndef	IPPROTO_ICMPV6
2538 # define	IPPROTO_ICMPV6	58
2539 #endif
2540 #ifndef	IPPROTO_NONE
2541 # define	IPPROTO_NONE	59
2542 #endif
2543 #ifndef	IPPROTO_DSTOPTS
2544 # define	IPPROTO_DSTOPTS	60
2545 #endif
2546 #ifndef	IPPROTO_MOBILITY
2547 # define	IPPROTO_MOBILITY	135
2548 #endif
2549 
2550 #ifndef	ICMP_ROUTERADVERT
2551 # define	ICMP_ROUTERADVERT	9
2552 #endif
2553 #ifndef	ICMP_ROUTERSOLICIT
2554 # define	ICMP_ROUTERSOLICIT	10
2555 #endif
2556 #ifndef	ICMP6_DST_UNREACH
2557 # define	ICMP6_DST_UNREACH	1
2558 #endif
2559 #ifndef	ICMP6_PACKET_TOO_BIG
2560 # define	ICMP6_PACKET_TOO_BIG	2
2561 #endif
2562 #ifndef	ICMP6_TIME_EXCEEDED
2563 # define	ICMP6_TIME_EXCEEDED	3
2564 #endif
2565 #ifndef	ICMP6_PARAM_PROB
2566 # define	ICMP6_PARAM_PROB	4
2567 #endif
2568 
2569 #ifndef	ICMP6_ECHO_REQUEST
2570 # define	ICMP6_ECHO_REQUEST	128
2571 #endif
2572 #ifndef	ICMP6_ECHO_REPLY
2573 # define	ICMP6_ECHO_REPLY	129
2574 #endif
2575 #ifndef	ICMP6_MEMBERSHIP_QUERY
2576 # define	ICMP6_MEMBERSHIP_QUERY	130
2577 #endif
2578 #ifndef	MLD6_LISTENER_QUERY
2579 # define	MLD6_LISTENER_QUERY	130
2580 #endif
2581 #ifndef	ICMP6_MEMBERSHIP_REPORT
2582 # define	ICMP6_MEMBERSHIP_REPORT	131
2583 #endif
2584 #ifndef	MLD6_LISTENER_REPORT
2585 # define	MLD6_LISTENER_REPORT	131
2586 #endif
2587 #ifndef	ICMP6_MEMBERSHIP_REDUCTION
2588 # define	ICMP6_MEMBERSHIP_REDUCTION	132
2589 #endif
2590 #ifndef	MLD6_LISTENER_DONE
2591 # define	MLD6_LISTENER_DONE	132
2592 #endif
2593 #ifndef	ND_ROUTER_SOLICIT
2594 # define	ND_ROUTER_SOLICIT	133
2595 #endif
2596 #ifndef	ND_ROUTER_ADVERT
2597 # define	ND_ROUTER_ADVERT	134
2598 #endif
2599 #ifndef	ND_NEIGHBOR_SOLICIT
2600 # define	ND_NEIGHBOR_SOLICIT	135
2601 #endif
2602 #ifndef	ND_NEIGHBOR_ADVERT
2603 # define	ND_NEIGHBOR_ADVERT	136
2604 #endif
2605 #ifndef	ND_REDIRECT
2606 # define	ND_REDIRECT	137
2607 #endif
2608 #ifndef	ICMP6_ROUTER_RENUMBERING
2609 # define	ICMP6_ROUTER_RENUMBERING	138
2610 #endif
2611 #ifndef	ICMP6_WRUREQUEST
2612 # define	ICMP6_WRUREQUEST	139
2613 #endif
2614 #ifndef	ICMP6_WRUREPLY
2615 # define	ICMP6_WRUREPLY		140
2616 #endif
2617 #ifndef	ICMP6_FQDN_QUERY
2618 # define	ICMP6_FQDN_QUERY	139
2619 #endif
2620 #ifndef	ICMP6_FQDN_REPLY
2621 # define	ICMP6_FQDN_REPLY	140
2622 #endif
2623 #ifndef	ICMP6_NI_QUERY
2624 # define	ICMP6_NI_QUERY		139
2625 #endif
2626 #ifndef	ICMP6_NI_REPLY
2627 # define	ICMP6_NI_REPLY		140
2628 #endif
2629 #ifndef	MLD6_MTRACE_RESP
2630 # define	MLD6_MTRACE_RESP	200
2631 #endif
2632 #ifndef	MLD6_MTRACE
2633 # define	MLD6_MTRACE		201
2634 #endif
2635 #ifndef	ICMP6_HADISCOV_REQUEST
2636 # define	ICMP6_HADISCOV_REQUEST	202
2637 #endif
2638 #ifndef	ICMP6_HADISCOV_REPLY
2639 # define	ICMP6_HADISCOV_REPLY	203
2640 #endif
2641 #ifndef	ICMP6_MOBILEPREFIX_SOLICIT
2642 # define	ICMP6_MOBILEPREFIX_SOLICIT	204
2643 #endif
2644 #ifndef	ICMP6_MOBILEPREFIX_ADVERT
2645 # define	ICMP6_MOBILEPREFIX_ADVERT	205
2646 #endif
2647 #ifndef	ICMP6_MAXTYPE
2648 # define	ICMP6_MAXTYPE		205
2649 #endif
2650 
2651 #ifndef	ICMP6_DST_UNREACH_NOROUTE
2652 # define	ICMP6_DST_UNREACH_NOROUTE	0
2653 #endif
2654 #ifndef	ICMP6_DST_UNREACH_ADMIN
2655 # define	ICMP6_DST_UNREACH_ADMIN		1
2656 #endif
2657 #ifndef	ICMP6_DST_UNREACH_NOTNEIGHBOR
2658 # define	ICMP6_DST_UNREACH_NOTNEIGHBOR	2
2659 #endif
2660 #ifndef	ICMP6_DST_UNREACH_BEYONDSCOPE
2661 # define	ICMP6_DST_UNREACH_BEYONDSCOPE	2
2662 #endif
2663 #ifndef	ICMP6_DST_UNREACH_ADDR
2664 # define	ICMP6_DST_UNREACH_ADDR		3
2665 #endif
2666 #ifndef	ICMP6_DST_UNREACH_NOPORT
2667 # define	ICMP6_DST_UNREACH_NOPORT	4
2668 #endif
2669 #ifndef	ICMP6_TIME_EXCEED_TRANSIT
2670 # define	ICMP6_TIME_EXCEED_TRANSIT	0
2671 #endif
2672 #ifndef	ICMP6_TIME_EXCEED_REASSEMBLY
2673 # define	ICMP6_TIME_EXCEED_REASSEMBLY	1
2674 #endif
2675 
2676 #ifndef	ICMP6_NI_SUCCESS
2677 # define	ICMP6_NI_SUCCESS	0
2678 #endif
2679 #ifndef	ICMP6_NI_REFUSED
2680 # define	ICMP6_NI_REFUSED	1
2681 #endif
2682 #ifndef	ICMP6_NI_UNKNOWN
2683 # define	ICMP6_NI_UNKNOWN	2
2684 #endif
2685 
2686 #ifndef	ICMP6_ROUTER_RENUMBERING_COMMAND
2687 # define	ICMP6_ROUTER_RENUMBERING_COMMAND	0
2688 #endif
2689 #ifndef	ICMP6_ROUTER_RENUMBERING_RESULT
2690 # define	ICMP6_ROUTER_RENUMBERING_RESULT	1
2691 #endif
2692 #ifndef	ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET
2693 # define	ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET	255
2694 #endif
2695 
2696 #ifndef	ICMP6_PARAMPROB_HEADER
2697 # define	ICMP6_PARAMPROB_HEADER	0
2698 #endif
2699 #ifndef	ICMP6_PARAMPROB_NEXTHEADER
2700 # define	ICMP6_PARAMPROB_NEXTHEADER	1
2701 #endif
2702 #ifndef	ICMP6_PARAMPROB_OPTION
2703 # define	ICMP6_PARAMPROB_OPTION	2
2704 #endif
2705 
2706 #ifndef	ICMP6_NI_SUBJ_IPV6
2707 # define	ICMP6_NI_SUBJ_IPV6	0
2708 #endif
2709 #ifndef	ICMP6_NI_SUBJ_FQDN
2710 # define	ICMP6_NI_SUBJ_FQDN	1
2711 #endif
2712 #ifndef	ICMP6_NI_SUBJ_IPV4
2713 # define	ICMP6_NI_SUBJ_IPV4	2
2714 #endif
2715 
2716 #ifndef	MLD_MTRACE_RESP
2717 # define	MLD_MTRACE_RESP		200
2718 #endif
2719 #ifndef	MLD_MTRACE
2720 # define	MLD_MTRACE		201
2721 #endif
2722 #ifndef	MLD6_MTRACE_RESP
2723 # define	MLD6_MTRACE_RESP	MLD_MTRACE_RESP
2724 #endif
2725 #ifndef	MLD6_MTRACE
2726 # define	MLD6_MTRACE		MLD_MTRACE
2727 #endif
2728 
2729 #if !defined(IPV6_FLOWINFO_MASK)
2730 # if (BYTE_ORDER == BIG_ENDIAN) || defined(_BIG_ENDIAN)
2731 #  define IPV6_FLOWINFO_MASK	0x0fffffff	/* flow info (28 bits) */
2732 # else
2733 #  if(BYTE_ORDER == LITTLE_ENDIAN) || !defined(_BIG_ENDIAN)
2734 #   define IPV6_FLOWINFO_MASK	0xffffff0f	/* flow info (28 bits) */
2735 #  endif /* LITTLE_ENDIAN */
2736 # endif
2737 #endif
2738 #if !defined(IPV6_FLOWLABEL_MASK)
2739 # if (BYTE_ORDER == BIG_ENDIAN) || defined(_BIG_ENDIAN)
2740 #  define IPV6_FLOWLABEL_MASK	0x000fffff	/* flow label (20 bits) */
2741 # else
2742 #  if (BYTE_ORDER == LITTLE_ENDIAN) || !defined(_BIG_ENDIAN)
2743 #   define IPV6_FLOWLABEL_MASK	0xffff0f00	/* flow label (20 bits) */
2744 #  endif /* LITTLE_ENDIAN */
2745 # endif
2746 #endif
2747 
2748 /*
2749  * ECN is a new addition to TCP - RFC 2481
2750  */
2751 #ifndef TH_ECN
2752 # define	TH_ECN	0x40
2753 #endif
2754 #ifndef TH_CWR
2755 # define	TH_CWR	0x80
2756 #endif
2757 #define	TH_ECNALL	(TH_ECN|TH_CWR)
2758 
2759 /*
2760  * TCP States
2761  */
2762 #define IPF_TCPS_LISTEN		0	/* listening for connection */
2763 #define IPF_TCPS_SYN_SENT	1	/* active, have sent syn */
2764 #define IPF_TCPS_SYN_RECEIVED	2	/* have send and received syn */
2765 #define IPF_TCPS_HALF_ESTAB	3	/* for connections not fully "up" */
2766 /* states < IPF_TCPS_ESTABLISHED are those where connections not established */
2767 #define IPF_TCPS_ESTABLISHED	4	/* established */
2768 #define IPF_TCPS_CLOSE_WAIT	5	/* rcvd fin, waiting for close */
2769 /* states > IPF_TCPS_CLOSE_WAIT are those where user has closed */
2770 #define IPF_TCPS_FIN_WAIT_1	6	/* have closed, sent fin */
2771 #define IPF_TCPS_CLOSING	7	/* closed xchd FIN; await FIN ACK */
2772 #define IPF_TCPS_LAST_ACK	8	/* had fin and close; await FIN ACK */
2773 /* states > IPF_TCPS_CLOSE_WAIT && < IPF_TCPS_FIN_WAIT_2 await ACK of FIN */
2774 #define IPF_TCPS_FIN_WAIT_2	9	/* have closed, fin is acked */
2775 #define IPF_TCPS_TIME_WAIT	10	/* in 2*msl quiet wait after close */
2776 #define IPF_TCPS_CLOSED		11	/* closed */
2777 #define IPF_TCP_NSTATES		12
2778 
2779 #define	TCP_MSL			120
2780 
2781 #undef	ICMP_MAX_UNREACH
2782 #define	ICMP_MAX_UNREACH	14
2783 #undef	ICMP_MAXTYPE
2784 #define	ICMP_MAXTYPE		18
2785 
2786 #ifndef	IFNAMSIZ
2787 #define	IFNAMSIZ		16
2788 #endif
2789 
2790 #ifndef	LOG_FTP
2791 # define	LOG_FTP		(11<<3)
2792 #endif
2793 #ifndef	LOG_AUTHPRIV
2794 # define	LOG_AUTHPRIV	(10<<3)
2795 #endif
2796 #ifndef	LOG_AUDIT
2797 # define	LOG_AUDIT	(13<<3)
2798 #endif
2799 #ifndef	LOG_NTP
2800 # define	LOG_NTP		(12<<3)
2801 #endif
2802 #ifndef	LOG_SECURITY
2803 # define	LOG_SECURITY	(13<<3)
2804 #endif
2805 #ifndef	LOG_LFMT
2806 # define	LOG_LFMT	(14<<3)
2807 #endif
2808 #ifndef	LOG_CONSOLE
2809 # define	LOG_CONSOLE	(14<<3)
2810 #endif
2811 
2812 /*
2813  * ICMP error replies have an IP header (20 bytes), 8 bytes of ICMP data,
2814  * another IP header and then 64 bits of data, totalling 56.  Of course,
2815  * the last 64 bits is dependent on that being available.
2816  */
2817 #define	ICMPERR_ICMPHLEN	8
2818 #define	ICMPERR_IPICMPHLEN	(20 + 8)
2819 #define	ICMPERR_MINPKTLEN	(20 + 8 + 20)
2820 #define	ICMPERR_MAXPKTLEN	(20 + 8 + 20 + 8)
2821 #define ICMP6ERR_MINPKTLEN	(40 + 8)
2822 #define ICMP6ERR_IPICMPHLEN	(40 + 8 + 40)
2823 
2824 #ifndef MIN
2825 # define	MIN(a,b)	(((a)<(b))?(a):(b))
2826 #endif
2827 
2828 #ifdef RESCUE
2829 # undef IPFILTER_BPF
2830 #endif
2831 
2832 #ifdef IPF_DEBUG
2833 # define	DPRINT(x)	printf x
2834 #else
2835 # define	DPRINT(x)
2836 #endif
2837 
2838 #ifndef	AF_INET6
2839 # define	AF_INET6	26
2840 #endif
2841 
2842 #ifdef DTRACE_PROBE
2843 # ifdef _KERNEL
2844 #  define	DT(_n)			DTRACE_PROBE(_n)
2845 #  define	DT1(_n,_a,_b)		DTRACE_PROBE1(_n,_a,_b)
2846 #  define	DT2(_n,_a,_b,_c,_d)	DTRACE_PROBE2(_n,_a,_b,_c,_d)
2847 #  define	DT3(_n,_a,_b,_c,_d,_e,_f)	\
2848 					DTRACE_PROBE3(_n,_a,_b,_c,_d,_e,_f)
2849 #  define	DT4(_n,_a,_b,_c,_d,_e,_f,_g,_h) \
2850 				DTRACE_PROBE4(_n,_a,_b,_c,_d,_e,_f,_g,_h)
2851 # else
2852 #  define	DT(_n)
2853 #  define	DT1(_n,_a,_b)
2854 #  define	DT2(_n,_a,_b,_c,_d)
2855 #  define	DT3(_n,_a,_b,_c,_d,_e,_f)
2856 #  define	DT4(_n,_a,_b,_c,_d,_e,_f,_g,_h)
2857 # endif
2858 #else
2859 # define	DT(_n)
2860 # define	DT1(_n,_a,_b)
2861 # define	DT2(_n,_a,_b,_c,_d)
2862 # define	DT3(_n,_a,_b,_c,_d,_e,_f)
2863 # define	DT4(_n,_a,_b,_c,_d,_e,_f,_g,_h)
2864 #endif
2865 
2866 struct ip6_routing {
2867 	u_char	ip6r_nxt;	/* next header */
2868 	u_char	ip6r_len;	/* length in units of 8 octets */
2869 	u_char	ip6r_type;	/* always zero */
2870 	u_char	ip6r_segleft;	/* segments left */
2871 	u_32_t	ip6r_reserved;	/* reserved field */
2872 };
2873 
2874 #endif	/* __IP_COMPAT_H__ */
2875