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