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