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