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