1/*
2 * Copyright (C) 2012 by Darren Reed.
3 *
4 * See the IPFILTER.LICENCE file for details on licencing.
5 *
6 * @(#)ip_compat.h	1.8 1/14/96
7 * $FreeBSD: stable/10/sys/contrib/ipfilter/netinet/ip_compat.h 342607 2018-12-30 04:37:49Z cy $
8 * Id: ip_compat.h,v 2.142.2.57 2007/10/10 09:51:42 darrenr Exp $
9 */
10
11#ifndef	__IP_COMPAT_H__
12#define	__IP_COMPAT_H__
13
14#ifndef	__P
15# ifdef	__STDC__
16#  define	__P(x)  x
17# else
18#  define	__P(x)  ()
19# endif
20#endif
21#ifndef	__STDC__
22# undef		const
23# define	const
24#endif
25
26#if defined(_KERNEL) || defined(KERNEL) || defined(__KERNEL__)
27# undef	KERNEL
28# undef	_KERNEL
29# undef 	__KERNEL__
30# define	KERNEL
31# define	_KERNEL
32# define 	__KERNEL__
33#endif
34
35#ifndef	SOLARIS
36# if defined(sun) && (defined(__svr4__) || defined(__SVR4))
37#  define	SOLARIS		1
38# else
39#  define	SOLARIS		0
40# endif
41#endif
42
43
44#if defined(__SVR4) || defined(__svr4__) || defined(__sgi)
45# define index   strchr
46# if !defined(_KERNEL)
47#  define	bzero(a,b)	memset(a,0,b)
48#  define	bcmp		memcmp
49#  define	bcopy(a,b,c)	memmove(b,a,c)
50# endif
51#endif
52
53#ifndef LIFNAMSIZ
54# ifdef IF_NAMESIZE
55#  define	LIFNAMSIZ	IF_NAMESIZE
56# else
57#  ifdef	IFNAMSIZ
58#   define	LIFNAMSIZ	IFNAMSIZ
59#  else
60#   define	LIFNAMSIZ	16
61#  endif
62# endif
63#endif
64
65#if defined(__sgi) || defined(bsdi) || defined(__hpux) || defined(hpux)
66struct  ether_addr {
67        u_char  ether_addr_octet[6];
68};
69#endif
70
71# ifdef __STDC__
72#  define IPL_EXTERN(ep) ipl##ep
73# else
74#  define IPL_EXTERN(ep) ipl/**/ep
75# endif
76
77/*
78 * This is a workaround for <sys/uio.h> troubles on FreeBSD and OpenBSD.
79 */
80# ifndef _KERNEL
81#  define ADD_KERNEL
82#  define _KERNEL
83#  define KERNEL
84# endif
85# include <sys/uio.h>
86# ifdef ADD_KERNEL
87#  undef _KERNEL
88#  undef KERNEL
89# endif
90
91#define	NETBSD_GE_REV(x)	(defined(__NetBSD_Version__) && \
92				 (__NetBSD_Version__ >= (x)))
93#define	NETBSD_GT_REV(x)	(defined(__NetBSD_Version__) && \
94				 (__NetBSD_Version__ > (x)))
95#define	NETBSD_LT_REV(x)	(defined(__NetBSD_Version__) && \
96				 (__NetBSD_Version__ < (x)))
97#define	FREEBSD_GE_REV(x)	(defined(__FreeBSD_version) && \
98				 (__FreeBSD_version >= (x)))
99#define	FREEBSD_GT_REV(x)	(defined(__FreeBSD_version) && \
100				 (__FreeBSD_version > (x)))
101#define	FREEBSD_LT_REV(x)	(defined(__FreeBSD_version) && \
102				 (__FreeBSD_version < (x)))
103#define	BSDOS_GE_REV(x)		(defined(_BSDI_VERSION) && \
104				 (_BSDI_VERSION >= (x)))
105#define	BSDOS_GT_REV(x)		(defined(_BSDI_VERSION) && \
106				 (_BSDI_VERSION > (x)))
107#define	BSDOS_LT_REV(x)		(defined(_BSDI_VERSION) && \
108				 (_BSDI_VERSION < (x)))
109#define	OPENBSD_GE_REV(x)	(defined(OpenBSD) && (OpenBSD >= (x)))
110#define	OPENBSD_GT_REV(x)	(defined(OpenBSD) && (OpenBSD > (x)))
111#define	OPENBSD_LT_REV(x)	(defined(OpenBSD) && (OpenBSD < (x)))
112#define	BSD_GE_YEAR(x)		(defined(BSD) && (BSD >= (x)))
113#define	BSD_GT_YEAR(x)		(defined(BSD) && (BSD > (x)))
114#define	BSD_LT_YEAR(x)		(defined(BSD) && (BSD < (x)))
115
116
117/* ----------------------------------------------------------------------- */
118/*                                F R E E B S D                            */
119/* ----------------------------------------------------------------------- */
120# define HAS_SYS_MD5_H	1
121# if defined(_KERNEL)
122#   include "opt_bpf.h"
123#   include "opt_inet6.h"
124#  if defined(INET6) && !defined(USE_INET6)
125#   define USE_INET6
126#  endif
127# else
128#  if !defined(USE_INET6) && !defined(NOINET6)
129#   define	USE_INET6
130#  endif
131# endif
132
133# if defined(_KERNEL)
134#  include <netinet/ip_var.h>
135#    define	p_cred	td_ucred
136#    define	p_uid	td_ucred->cr_ruid
137
138/*
139 * When #define'd, the 5.2.1 kernel panics when used with the ftp proxy.
140 * There may be other, safe, kernels but this is not extensively tested yet.
141 */
142#   define HAVE_M_PULLDOWN
143#  if !defined(IPFILTER_LKM) && (__FreeBSD_version >= 300000)
144#   include "opt_ipfilter.h"
145#  endif
146#  define	COPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
147#  define	COPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
148
149# else
150#  include <inttypes.h>
151# endif /* _KERNEL */
152
153#  include <sys/selinfo.h>
154#  include <sys/mutex.h>
155#    define	KRWLOCK_FILL_SZ		56
156#    define	KMUTEX_FILL_SZ		56
157#   include <sys/rwlock.h>
158#   define	KMUTEX_T		struct mtx
159#   define	KRWLOCK_T		struct rwlock
160#   ifdef _KERNEL
161#    define	READ_ENTER(x)		rw_rlock(&(x)->ipf_lk)
162#    define	WRITE_ENTER(x)		rw_wlock(&(x)->ipf_lk)
163#    define	MUTEX_DOWNGRADE(x)	rw_downgrade(&(x)->ipf_lk)
164#    define	MUTEX_TRY_UPGRADE(x)	rw_try_upgrade(&(x)->ipf_lk)
165#    define	RWLOCK_INIT(x,y)	rw_init(&(x)->ipf_lk, (y))
166#    define	RW_DESTROY(x)		rw_destroy(&(x)->ipf_lk)
167#    define	RWLOCK_EXIT(x)		do { \
168					    if (rw_wowned(&(x)->ipf_lk)) \
169					    	rw_wunlock(&(x)->ipf_lk); \
170					    else \
171						rw_runlock(&(x)->ipf_lk); \
172					} while (0)
173#   endif
174
175#  include <net/if_var.h>
176#  define	IFNAME(x)	((struct ifnet *)x)->if_xname
177#  define	COPYIFNAME(v, x, b) \
178				(void) strncpy(b, \
179					       ((struct ifnet *)x)->if_xname, \
180					       LIFNAMSIZ)
181
182# ifdef _KERNEL
183#  define	GETKTIME(x)	microtime((struct timeval *)x)
184
185#   include <netinet/in_systm.h>
186#   include <netinet/ip.h>
187#   include <machine/in_cksum.h>
188
189#   define	USE_MUTEXES
190#   define	MUTEX_ENTER(x)		mtx_lock(&(x)->ipf_lk)
191#   define	MUTEX_EXIT(x)		mtx_unlock(&(x)->ipf_lk)
192#   define	MUTEX_INIT(x,y)		mtx_init(&(x)->ipf_lk, (y), NULL,\
193						 MTX_DEF)
194#   define	MUTEX_DESTROY(x)	mtx_destroy(&(x)->ipf_lk)
195#   define	MUTEX_NUKE(x)		bzero((x), sizeof(*(x)))
196/*
197 * Whilst the sx(9) locks on FreeBSD have the right semantics and interface
198 * for what we want to use them for, despite testing showing they work -
199 * with a WITNESS kernel, it generates LOR messages.
200 */
201#   include <machine/atomic.h>
202#   define	ATOMIC_INC(x)		{ mtx_lock(&softc->ipf_rw.ipf_lk); (x)++; \
203					  mtx_unlock(&softc->ipf_rw.ipf_lk); }
204#   define	ATOMIC_DEC(x)		{ mtx_lock(&softc->ipf_rw.ipf_lk); (x)--; \
205					  mtx_unlock(&softc->ipf_rw.ipf_lk); }
206#   define	ATOMIC_INCL(x)		atomic_add_long(&(x), 1)
207#   define	ATOMIC_INC64(x)		ATOMIC_INC(x)
208#   define	ATOMIC_INC32(x)		atomic_add_32((u_int *)&(x), 1)
209#   define	ATOMIC_DECL(x)		atomic_add_long(&(x), -1)
210#   define	ATOMIC_DEC64(x)		ATOMIC_DEC(x)
211#   define	ATOMIC_DEC32(x)		atomic_add_32((u_int *)&(x), -1)
212#   define	SPL_X(x)	;
213#   define	SPL_NET(x)	;
214#   define	SPL_IMP(x)	;
215#   define	SPL_SCHED(x)	;
216#   define	GET_MINOR		dev2unit
217#  define	MSGDSIZE(m)	mbufchainlen(m)
218#  define	M_LEN(m)	(m)->m_len
219#  define	M_ADJ(m,x)	m_adj(m, x)
220#  define	M_COPY(x)	m_copy((x), 0, M_COPYALL)
221#  define	M_DUP(m)	m_dup(m, M_NOWAIT)
222#  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
223typedef struct mbuf mb_t;
224# endif /* _KERNEL */
225
226
227typedef	u_long		ioctlcmd_t;
228typedef	struct uio	uio_t;
229typedef	int		minor_t;
230typedef	u_int32_t	u_32_t;
231# define	U_32_T	1
232
233
234/* ----------------------------------------------------------------------- */
235/*                           G E N E R I C                                 */
236/* ----------------------------------------------------------------------- */
237
238/*
239 * For BSD kernels, if bpf is in the kernel, enable ipfilter to use bpf in
240 * filter rules.
241 */
242#if !defined(IPFILTER_BPF)
243# if (defined(NBPF) && (NBPF > 0)) || (defined(DEV_BPF) && (DEV_BPF > 0)) || \
244     (defined(NBPFILTER) && (NBPFILTER > 0))
245#  define	IPFILTER_BPF
246# endif
247#endif
248
249/*
250 * Userland locking primitives
251 */
252#ifndef _KERNEL
253#if !defined(KMUTEX_FILL_SZ)
254# define	KMUTEX_FILL_SZ	1
255#endif
256#if !defined(KRWLOCK_FILL_SZ)
257# define	KRWLOCK_FILL_SZ	1
258#endif
259#endif
260
261typedef	struct	{
262	char	*eMm_owner;
263	char	*eMm_heldin;
264	u_int	eMm_magic;
265	int	eMm_held;
266	int	eMm_heldat;
267} eMmutex_t;
268
269typedef	struct	{
270	char	*eMrw_owner;
271	char	*eMrw_heldin;
272	u_int	eMrw_magic;
273	short	eMrw_read;
274	short	eMrw_write;
275	int	eMrw_heldat;
276} eMrwlock_t;
277
278typedef union {
279	char	_fill[KMUTEX_FILL_SZ];
280#ifdef KMUTEX_T
281	struct	{
282		KMUTEX_T	ipf_slk;
283		const char	*ipf_lname;
284	} ipf_lkun_s;
285#endif
286	eMmutex_t	ipf_emu;
287} ipfmutex_t;
288
289typedef union {
290	char	_fill[KRWLOCK_FILL_SZ];
291#ifdef KRWLOCK_T
292	struct	{
293		KRWLOCK_T	ipf_slk;
294		const char	*ipf_lname;
295		int		ipf_sr;
296		int		ipf_sw;
297		u_int		ipf_magic;
298	} ipf_lkun_s;
299#endif
300	eMrwlock_t	ipf_emu;
301} ipfrwlock_t;
302
303#define	ipf_lk		ipf_lkun_s.ipf_slk
304#define	ipf_lname	ipf_lkun_s.ipf_lname
305#define	ipf_isr		ipf_lkun_s.ipf_sr
306#define	ipf_isw		ipf_lkun_s.ipf_sw
307#define	ipf_magic	ipf_lkun_s.ipf_magic
308
309#if !defined(__GNUC__) || \
310    (defined(__FreeBSD_version) && (__FreeBSD_version >= 503000))
311# ifndef	INLINE
312#  define	INLINE
313# endif
314#else
315# define	INLINE	__inline__
316#endif
317
318#if defined(__FreeBSD_version) && defined(_KERNEL)
319     CTASSERT(sizeof(ipfrwlock_t) == KRWLOCK_FILL_SZ);
320     CTASSERT(sizeof(ipfmutex_t) == KMUTEX_FILL_SZ);
321#endif
322
323
324/*
325 * In a non-kernel environment, there are a lot of macros that need to be
326 * filled in to be null-ops or to point to some compatibility function,
327 * somewhere in userland.
328 */
329#ifndef _KERNEL
330typedef	struct	mb_s	{
331	struct	mb_s	*mb_next;
332	char		*mb_data;
333	void		*mb_ifp;
334	int		mb_len;
335	int		mb_flags;
336	u_long		mb_buf[2048];
337} mb_t;
338# undef		m_next
339# define	m_next		mb_next
340# undef		m_len
341# define	m_len		mb_len
342# undef		m_flags
343# define	m_flags		mb_flags
344# undef		m_data
345# define	m_data		mb_data
346# undef		M_MCAST
347# define	M_MCAST		0x01
348# undef		M_BCAST
349# define	M_BCAST		0x02
350# undef		M_MBCAST
351# define	M_MBCAST	0x04
352# define	MSGDSIZE(m)	msgdsize(m)
353# define	M_LEN(m)	(m)->mb_len
354# define	M_ADJ(m,x)	(m)->mb_len += x
355# define	M_COPY(m)	dupmbt(m)
356# define	M_DUP(m)	dupmbt(m)
357# define	GETKTIME(x)	gettimeofday((struct timeval *)(x), NULL)
358# define	MTOD(m, t)	((t)(m)->mb_data)
359# define	FREE_MB_T(m)	freembt(m)
360# define	ALLOC_MB_T(m,l)	(m) = allocmbt(l)
361# define	PREP_MB_T(f, m)	do { \
362						(m)->mb_next = *(f)->fin_mp; \
363						*(fin)->fin_mp = (m); \
364						(f)->fin_m = (m); \
365					} while (0)
366# define	SLEEP(x,y)	1;
367# define	WAKEUP(x,y)	;
368# define	POLLWAKEUP(y)	;
369# define	IPF_PANIC(x,y)	;
370# define	PANIC(x,y)	;
371# define	SPL_SCHED(x)	;
372# define	SPL_NET(x)	;
373# define	SPL_IMP(x)	;
374# define	SPL_X(x)	;
375# define	KMALLOC(a,b)	(a) = (b)malloc(sizeof(*a))
376# define	KMALLOCS(a,b,c)	(a) = (b)malloc(c)
377# define	KFREE(x)	free(x)
378# define	KFREES(x,s)	free(x)
379# define	GETIFP(x, v)	get_unit(x,v)
380# define	GETIFMTU_4(x)	2048
381# define	GETIFMTU_6(x)	2048
382# define	COPYIN(a,b,c)	bcopywrap((a), (b), (c))
383# define	COPYOUT(a,b,c)	bcopywrap((a), (b), (c))
384# define	COPYDATA(m, o, l, b)	bcopy(MTOD((mb_t *)m, char *) + (o), \
385					      (b), (l))
386# define	COPYBACK(m, o, l, b)	bcopy((b), \
387					      MTOD((mb_t *)m, char *) + (o), \
388					      (l))
389# define	UIOMOVE(a,b,c,d)	ipfuiomove((caddr_t)a,b,c,d)
390extern	void	m_copydata __P((mb_t *, int, int, caddr_t));
391extern	int	ipfuiomove __P((caddr_t, int, int, struct uio *));
392extern	int	bcopywrap __P((void *, void *, size_t));
393extern	mb_t	*allocmbt __P((size_t));
394extern	mb_t	*dupmbt __P((mb_t *));
395extern	void	freembt __P((mb_t *));
396
397# define	MUTEX_DESTROY(x)	eMmutex_destroy(&(x)->ipf_emu, \
398							__FILE__, __LINE__)
399# define	MUTEX_ENTER(x)		eMmutex_enter(&(x)->ipf_emu, \
400						      __FILE__, __LINE__)
401# define	MUTEX_EXIT(x)		eMmutex_exit(&(x)->ipf_emu, \
402						     __FILE__, __LINE__)
403# define	MUTEX_INIT(x,y)		eMmutex_init(&(x)->ipf_emu, y, \
404						     __FILE__, __LINE__)
405# define	MUTEX_NUKE(x)		bzero((x), sizeof(*(x)))
406
407# define	MUTEX_DOWNGRADE(x)	eMrwlock_downgrade(&(x)->ipf_emu, \
408							   __FILE__, __LINE__)
409# define	MUTEX_TRY_UPGRADE(x)	eMrwlock_try_upgrade(&(x)->ipf_emu, \
410							   __FILE__, __LINE__)
411# define	READ_ENTER(x)		eMrwlock_read_enter(&(x)->ipf_emu, \
412							    __FILE__, __LINE__)
413# define	RWLOCK_INIT(x, y)	eMrwlock_init(&(x)->ipf_emu, y)
414# define	RWLOCK_EXIT(x)		eMrwlock_exit(&(x)->ipf_emu)
415# define	RW_DESTROY(x)		eMrwlock_destroy(&(x)->ipf_emu)
416# define	WRITE_ENTER(x)		eMrwlock_write_enter(&(x)->ipf_emu, \
417							     __FILE__, \
418							     __LINE__)
419
420# define	USE_MUTEXES		1
421
422extern void eMmutex_destroy __P((eMmutex_t *, char *, int));
423extern void eMmutex_enter __P((eMmutex_t *, char *, int));
424extern void eMmutex_exit __P((eMmutex_t *, char *, int));
425extern void eMmutex_init __P((eMmutex_t *, char *, char *, int));
426extern void eMrwlock_destroy __P((eMrwlock_t *));
427extern void eMrwlock_exit __P((eMrwlock_t *));
428extern void eMrwlock_init __P((eMrwlock_t *, char *));
429extern void eMrwlock_read_enter __P((eMrwlock_t *, char *, int));
430extern void eMrwlock_write_enter __P((eMrwlock_t *, char *, int));
431extern void eMrwlock_downgrade __P((eMrwlock_t *, char *, int));
432
433#endif
434
435extern	mb_t	*allocmbt(size_t);
436
437#define	MAX_IPV4HDR	((0xf << 2) + sizeof(struct icmp) + sizeof(ip_t) + 8)
438
439#ifndef	IP_OFFMASK
440# define	IP_OFFMASK	0x1fff
441#endif
442
443
444/*
445 * On BSD's use quad_t as a guarantee for getting at least a 64bit sized
446 * object.
447 */
448#if !defined(__amd64__) && BSD_GT_YEAR(199306)
449# define	USE_QUAD_T
450# define	U_QUAD_T	unsigned long long
451# define	QUAD_T		long long
452#else /* BSD > 199306 */
453# if !defined(U_QUAD_T)
454#  define	U_QUAD_T	u_long
455#  define	QUAD_T		long
456# endif
457#endif /* BSD > 199306 */
458
459
460#ifdef	USE_INET6
461# if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || \
462     defined(__osf__) || defined(linux)
463#  include <netinet/ip6.h>
464#  include <netinet/icmp6.h>
465#   if defined(_KERNEL) && !defined(__osf__)
466#    include <netinet6/ip6_var.h>
467#   endif
468typedef	struct ip6_hdr	ip6_t;
469# endif
470#endif
471
472#ifndef	MAX
473# define	MAX(a,b)	(((a) > (b)) ? (a) : (b))
474#endif
475
476#if defined(_KERNEL)
477# if defined(MENTAT) && !defined(INSTANCES)
478#  define	COPYDATA	mb_copydata
479#  define	COPYBACK	mb_copyback
480# else
481#  define	COPYDATA	m_copydata
482#  define	COPYBACK	m_copyback
483# endif
484#  if (defined(__NetBSD_Version__) && (__NetBSD_Version__ < 105180000)) || \
485       defined(__FreeBSD__) || (defined(OpenBSD) && (OpenBSD < 200206)) || \
486       defined(_BSDI_VERSION)
487#   include <vm/vm.h>
488#  endif
489#  if !defined(__FreeBSD__) || FREEBSD_GE_REV(300000)
490#   if NETBSD_GE_REV(105180000) || OPENBSD_GE_REV(200111)
491#    include <uvm/uvm_extern.h>
492#   else
493#    include <vm/vm_extern.h>
494extern  vm_map_t        kmem_map;
495#   endif
496#   include <sys/proc.h>
497#  else /* !__FreeBSD__ || (__FreeBSD__ && __FreeBSD_version >= 300000) */
498#   include <vm/vm_kern.h>
499#  endif /* !__FreeBSD__ || (__FreeBSD__ && __FreeBSD_version >= 300000) */
500
501#  ifdef IPFILTER_M_IPFILTER
502#    include <sys/malloc.h>
503MALLOC_DECLARE(M_IPFILTER);
504#    define	_M_IPF		M_IPFILTER
505#  else /* IPFILTER_M_IPFILTER */
506#   ifdef M_PFIL
507#    define	_M_IPF		M_PFIL
508#   else
509#    ifdef M_IPFILTER
510#     define	_M_IPF		M_IPFILTER
511#    else
512#     define	_M_IPF		M_TEMP
513#    endif /* M_IPFILTER */
514#   endif /* M_PFIL */
515#  endif /* IPFILTER_M_IPFILTER */
516#  if !defined(KMALLOC)
517#   define	KMALLOC(a, b)	MALLOC((a), b, sizeof(*(a)), _M_IPF, M_NOWAIT)
518#  endif
519#  if !defined(KMALLOCS)
520#   define	KMALLOCS(a, b, c)	MALLOC((a), b, (c), _M_IPF, M_NOWAIT)
521#  endif
522#  if !defined(KFREE)
523#   define	KFREE(x)	FREE((x), _M_IPF)
524#  endif
525#   if !defined(KFREES)
526#  define	KFREES(x,s)	FREE((x), _M_IPF)
527#  endif
528#  define	UIOMOVE(a,b,c,d)	uiomove((caddr_t)a,b,d)
529#  define	SLEEP(id, n)	tsleep((id), PPAUSE|PCATCH, n, 0)
530#  define	WAKEUP(id,x)	wakeup(id+x)
531#  if !defined(POLLWAKEUP)
532#   define	POLLWAKEUP(x)	selwakeup(softc->ipf_selwait+x)
533#  endif
534#  define	GETIFP(n, v)	ifunit(n)
535#  define	GETIFMTU_4(x)	((struct ifnet *)x)->if_mtu
536#  define	GETIFMTU_6(x)	((struct ifnet *)x)->if_mtu
537
538# if !defined(USE_MUTEXES) && !defined(SPL_NET)
539#  define	SPL_IMP(x)	x = splimp()
540#  define	SPL_NET(x)	x = splnet()
541#  if !defined(SPL_SCHED)
542#   define	SPL_SCHED(x)	x = splsched()
543#  endif
544#  define	SPL_X(x)	(void) splx(x)
545# endif /* !USE_MUTEXES */
546
547# ifndef FREE_MB_T
548#  define	FREE_MB_T(m)	m_freem(m)
549# endif
550# ifndef ALLOC_MB_T
551#  ifdef MGETHDR
552#   define	ALLOC_MB_T(m,l)	do { \
553					MGETHDR((m), M_DONTWAIT, MT_HEADER); \
554					if ((m) != NULL) { \
555						(m)->m_len = (l); \
556						(m)->m_pkthdr.len = (l); \
557					} \
558				} while (0)
559#  else
560#   define	ALLOC_MB_T(m,l)	do { \
561					MGET((m), M_DONTWAIT, MT_HEADER); \
562					if ((m) != NULL) { \
563						(m)->m_len = (l); \
564						(m)->m_pkthdr.len = (l); \
565					} \
566				} while (0)
567#  endif
568# endif
569# ifndef PREP_MB_T
570#  define	PREP_MB_T(f, m)	do { \
571						mb_t *_o = *(f)->fin_mp; \
572						(m)->m_next = _o; \
573						*(fin)->fin_mp = (m); \
574						if (_o->m_flags & M_PKTHDR) { \
575							(m)->m_pkthdr.len += \
576							    _o->m_pkthdr.len; \
577							(m)->m_pkthdr.rcvif = \
578							  _o->m_pkthdr.rcvif; \
579						} \
580					} while (0)
581# endif
582# ifndef M_DUP
583#  ifdef M_COPYALL
584#   define	M_DUP(m)	m_dup(m, 0, M_COPYALL, 0)
585#  else
586#   define	M_DUP(m)	m_dup(m)
587#  endif
588# endif
589
590# ifndef MTOD
591#  define	MTOD(m,t)	mtod(m,t)
592# endif
593
594# ifndef COPYIN
595#  define	COPYIN(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
596#  define	COPYOUT(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
597# endif
598
599# ifndef KMALLOC
600#  define	KMALLOC(a,b)	(a) = (b)new_kmem_alloc(sizeof(*(a)), \
601							KMEM_NOSLEEP)
602#  define	KMALLOCS(a,b,c)	(a) = (b)new_kmem_alloc((c), KMEM_NOSLEEP)
603# endif
604
605# ifndef	GET_MINOR
606#  define	GET_MINOR(x)	dev2unit(x)
607# endif
608# define	PANIC(x,y)	if (x) panic y
609#endif /* _KERNEL */
610
611#if !defined(IFNAME) && !defined(_KERNEL)
612# define	IFNAME(x)	get_ifname((struct ifnet *)x)
613#endif
614#ifndef	COPYIFNAME
615# define	NEED_FRGETIFNAME
616extern	char	*ipf_getifname __P((struct ifnet *, char *));
617# define	COPYIFNAME(v, x, b) \
618				ipf_getifname((struct ifnet *)x, b)
619#endif
620
621#ifndef ASSERT
622# ifdef _KERNEL
623#  define	ASSERT(x)
624# else
625#  define	ASSERT(x)	do { if (!(x)) abort(); } while (0)
626# endif
627#endif
628
629#ifndef BCOPYIN
630#  define	BCOPYIN(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
631#  define	BCOPYOUT(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
632#endif
633
634/*
635 * Because the ctype(3) posix definition, if used "safely" in code everywhere,
636 * would mean all normal code that walks through strings needed casts.  Yuck.
637 */
638#define	ISALNUM(x)	isalnum((u_char)(x))
639#define	ISALPHA(x)	isalpha((u_char)(x))
640#define	ISDIGIT(x)	isdigit((u_char)(x))
641#define	ISSPACE(x)	isspace((u_char)(x))
642#define	ISUPPER(x)	isupper((u_char)(x))
643#define	ISXDIGIT(x)	isxdigit((u_char)(x))
644#define	ISLOWER(x)	islower((u_char)(x))
645#define	TOUPPER(x)	toupper((u_char)(x))
646#define	TOLOWER(x)	tolower((u_char)(x))
647
648/*
649 * If mutexes aren't being used, turn all the mutex functions into null-ops.
650 */
651#if !defined(USE_MUTEXES)
652# define	USE_SPL			1
653# undef		RW_DESTROY
654# undef		MUTEX_INIT
655# undef		MUTEX_NUKE
656# undef		MUTEX_DESTROY
657# define	MUTEX_ENTER(x)		;
658# define	READ_ENTER(x)		;
659# define	WRITE_ENTER(x)		;
660# define	MUTEX_DOWNGRADE(x)	;
661# define	MUTEX_TRY_UPGRADE(x)	;
662# define	RWLOCK_INIT(x, y)	;
663# define	RWLOCK_EXIT(x)		;
664# define	RW_DESTROY(x)		;
665# define	MUTEX_EXIT(x)		;
666# define	MUTEX_INIT(x,y)		;
667# define	MUTEX_DESTROY(x)	;
668# define	MUTEX_NUKE(x)		;
669#endif /* !USE_MUTEXES */
670#ifndef	ATOMIC_INC
671# define	ATOMIC_INC(x)		(x)++
672# define	ATOMIC_DEC(x)		(x)--
673#endif
674
675#if defined(USE_SPL) && defined(_KERNEL)
676# define	SPL_INT(x)	int x
677#else
678# define	SPL_INT(x)
679#endif
680
681/*
682 * If there are no atomic operations for bit sizes defined, define them to all
683 * use a generic one that works for all sizes.
684 */
685#ifndef	ATOMIC_INCL
686# define	ATOMIC_INCL		ATOMIC_INC
687# define	ATOMIC_INC64		ATOMIC_INC
688# define	ATOMIC_INC32		ATOMIC_INC
689# define	ATOMIC_DECL		ATOMIC_DEC
690# define	ATOMIC_DEC64		ATOMIC_DEC
691# define	ATOMIC_DEC32		ATOMIC_DEC
692#endif
693
694#ifndef HDR_T_PRIVATE
695typedef	struct	tcphdr	tcphdr_t;
696typedef	struct	udphdr	udphdr_t;
697#endif
698typedef	struct	icmp	icmphdr_t;
699typedef	struct	ip	ip_t;
700typedef	struct	ether_header	ether_header_t;
701typedef	struct	tcpiphdr	tcpiphdr_t;
702
703#ifndef	FR_GROUPLEN
704# define	FR_GROUPLEN	16
705#endif
706
707#ifndef offsetof
708# define offsetof(t,m) (size_t)((&((t *)0L)->m))
709#endif
710#ifndef stsizeof
711# define stsizeof(t,m)	sizeof(((t *)0L)->m)
712#endif
713
714/*
715 * This set of macros has been brought about because on Tru64 it is not
716 * possible to easily assign or examine values in a structure that are
717 * bit fields.
718 */
719#ifndef IP_V
720# define	IP_V(x)		(x)->ip_v
721#endif
722#ifndef	IP_V_A
723# define	IP_V_A(x,y)	(x)->ip_v = (y)
724#endif
725#ifndef	IP_HL
726# define	IP_HL(x)	(x)->ip_hl
727#endif
728#ifndef	IP_HL_A
729# define	IP_HL_A(x,y)	(x)->ip_hl = ((y) & 0xf)
730#endif
731#ifndef	TCP_X2
732# define	TCP_X2(x)	(x)->th_x2
733#endif
734#ifndef	TCP_X2_A
735# define	TCP_X2_A(x,y)	(x)->th_x2 = (y)
736#endif
737#ifndef	TCP_OFF
738# define	TCP_OFF(x)	(x)->th_off
739#endif
740#ifndef	TCP_OFF_A
741# define	TCP_OFF_A(x,y)	(x)->th_off = (y)
742#endif
743#define	IPMINLEN(i, h)	((i)->ip_len >= (IP_HL(i) * 4 + sizeof(struct h)))
744
745#define	TCPF_ALL	(TH_FIN|TH_SYN|TH_RST|TH_PUSH|TH_ACK|TH_URG|\
746			 TH_ECN|TH_CWR)
747
748#if BSD_GE_YEAR(199306) && !defined(m_act)
749# define	m_act	m_nextpkt
750#endif
751
752/*
753 * Security Options for Intenet Protocol (IPSO) as defined in RFC 1108.
754 *
755 * Basic Option
756 *
757 * 00000001   -   (Reserved 4)
758 * 00111101   -   Top Secret
759 * 01011010   -   Secret
760 * 10010110   -   Confidential
761 * 01100110   -   (Reserved 3)
762 * 11001100   -   (Reserved 2)
763 * 10101011   -   Unclassified
764 * 11110001   -   (Reserved 1)
765 */
766#define	IPSO_CLASS_RES4		0x01
767#define	IPSO_CLASS_TOPS		0x3d
768#define	IPSO_CLASS_SECR		0x5a
769#define	IPSO_CLASS_CONF		0x96
770#define	IPSO_CLASS_RES3		0x66
771#define	IPSO_CLASS_RES2		0xcc
772#define	IPSO_CLASS_UNCL		0xab
773#define	IPSO_CLASS_RES1		0xf1
774
775#define	IPSO_AUTH_GENSER	0x80
776#define	IPSO_AUTH_ESI		0x40
777#define	IPSO_AUTH_SCI		0x20
778#define	IPSO_AUTH_NSA		0x10
779#define	IPSO_AUTH_DOE		0x08
780#define	IPSO_AUTH_UN		0x06
781#define	IPSO_AUTH_FTE		0x01
782
783/*
784 * IP option #defines
785 */
786#undef	IPOPT_RR
787#define	IPOPT_RR	7
788#undef	IPOPT_ZSU
789#define	IPOPT_ZSU	10	/* ZSU */
790#undef	IPOPT_MTUP
791#define	IPOPT_MTUP	11	/* MTUP */
792#undef	IPOPT_MTUR
793#define	IPOPT_MTUR	12	/* MTUR */
794#undef	IPOPT_ENCODE
795#define	IPOPT_ENCODE	15	/* ENCODE */
796#undef	IPOPT_TS
797#define	IPOPT_TS	68
798#undef	IPOPT_TR
799#define	IPOPT_TR	82	/* TR */
800#undef	IPOPT_SECURITY
801#define	IPOPT_SECURITY	130
802#undef	IPOPT_LSRR
803#define	IPOPT_LSRR	131
804#undef	IPOPT_E_SEC
805#define	IPOPT_E_SEC	133	/* E-SEC */
806#undef	IPOPT_CIPSO
807#define	IPOPT_CIPSO	134	/* CIPSO */
808#undef	IPOPT_SATID
809#define	IPOPT_SATID	136
810#ifndef	IPOPT_SID
811# define	IPOPT_SID	IPOPT_SATID
812#endif
813#undef	IPOPT_SSRR
814#define	IPOPT_SSRR	137
815#undef	IPOPT_ADDEXT
816#define	IPOPT_ADDEXT	147	/* ADDEXT */
817#undef	IPOPT_VISA
818#define	IPOPT_VISA	142	/* VISA */
819#undef	IPOPT_IMITD
820#define	IPOPT_IMITD	144	/* IMITD */
821#undef	IPOPT_EIP
822#define	IPOPT_EIP	145	/* EIP */
823#undef	IPOPT_RTRALRT
824#define	IPOPT_RTRALRT	148	/* RTRALRT */
825#undef	IPOPT_SDB
826#define	IPOPT_SDB	149
827#undef	IPOPT_NSAPA
828#define	IPOPT_NSAPA	150
829#undef	IPOPT_DPS
830#define	IPOPT_DPS	151
831#undef	IPOPT_UMP
832#define	IPOPT_UMP	152
833#undef	IPOPT_FINN
834#define	IPOPT_FINN	205	/* FINN */
835#undef	IPOPT_AH
836#define	IPOPT_AH	256+IPPROTO_AH
837
838#ifndef TCPOPT_EOL
839# define TCPOPT_EOL		0
840#endif
841#ifndef TCPOPT_NOP
842# define TCPOPT_NOP		1
843#endif
844#ifndef TCPOPT_MAXSEG
845# define TCPOPT_MAXSEG		2
846#endif
847#ifndef TCPOLEN_MAXSEG
848# define TCPOLEN_MAXSEG		4
849#endif
850#ifndef TCPOPT_WINDOW
851# define TCPOPT_WINDOW		3
852#endif
853#ifndef TCPOLEN_WINDOW
854# define TCPOLEN_WINDOW		3
855#endif
856#ifndef TCPOPT_SACK_PERMITTED
857# define TCPOPT_SACK_PERMITTED	4
858#endif
859#ifndef TCPOLEN_SACK_PERMITTED
860# define TCPOLEN_SACK_PERMITTED	2
861#endif
862#ifndef TCPOPT_SACK
863# define TCPOPT_SACK		5
864#endif
865#ifndef TCPOPT_TIMESTAMP
866# define TCPOPT_TIMESTAMP	8
867#endif
868
869#ifndef	ICMP_MINLEN
870# define	ICMP_MINLEN	8
871#endif
872#ifndef	ICMP_ECHOREPLY
873# define	ICMP_ECHOREPLY	0
874#endif
875#ifndef	ICMP_UNREACH
876# define	ICMP_UNREACH	3
877#endif
878#ifndef	ICMP_UNREACH_NET
879# define	ICMP_UNREACH_NET	0
880#endif
881#ifndef	ICMP_UNREACH_HOST
882# define	ICMP_UNREACH_HOST	1
883#endif
884#ifndef	ICMP_UNREACH_PROTOCOL
885# define	ICMP_UNREACH_PROTOCOL	2
886#endif
887#ifndef	ICMP_UNREACH_PORT
888# define	ICMP_UNREACH_PORT	3
889#endif
890#ifndef	ICMP_UNREACH_NEEDFRAG
891# define	ICMP_UNREACH_NEEDFRAG	4
892#endif
893#ifndef	ICMP_UNREACH_SRCFAIL
894# define	ICMP_UNREACH_SRCFAIL	5
895#endif
896#ifndef	ICMP_UNREACH_NET_UNKNOWN
897# define	ICMP_UNREACH_NET_UNKNOWN	6
898#endif
899#ifndef	ICMP_UNREACH_HOST_UNKNOWN
900# define	ICMP_UNREACH_HOST_UNKNOWN	7
901#endif
902#ifndef	ICMP_UNREACH_ISOLATED
903# define	ICMP_UNREACH_ISOLATED	8
904#endif
905#ifndef	ICMP_UNREACH_NET_PROHIB
906# define	ICMP_UNREACH_NET_PROHIB	9
907#endif
908#ifndef	ICMP_UNREACH_HOST_PROHIB
909# define	ICMP_UNREACH_HOST_PROHIB	10
910#endif
911#ifndef	ICMP_UNREACH_TOSNET
912# define	ICMP_UNREACH_TOSNET	11
913#endif
914#ifndef	ICMP_UNREACH_TOSHOST
915# define	ICMP_UNREACH_TOSHOST	12
916#endif
917#ifndef	ICMP_UNREACH_ADMIN_PROHIBIT
918# define	ICMP_UNREACH_ADMIN_PROHIBIT	13
919#endif
920#ifndef	ICMP_UNREACH_FILTER
921# define	ICMP_UNREACH_FILTER	13
922#endif
923#ifndef	ICMP_UNREACH_HOST_PRECEDENCE
924# define	ICMP_UNREACH_HOST_PRECEDENCE	14
925#endif
926#ifndef	ICMP_UNREACH_PRECEDENCE_CUTOFF
927# define	ICMP_UNREACH_PRECEDENCE_CUTOFF	15
928#endif
929#ifndef	ICMP_SOURCEQUENCH
930# define	ICMP_SOURCEQUENCH	4
931#endif
932#ifndef	ICMP_REDIRECT_NET
933# define	ICMP_REDIRECT_NET	0
934#endif
935#ifndef	ICMP_REDIRECT_HOST
936# define	ICMP_REDIRECT_HOST	1
937#endif
938#ifndef	ICMP_REDIRECT_TOSNET
939# define	ICMP_REDIRECT_TOSNET	2
940#endif
941#ifndef	ICMP_REDIRECT_TOSHOST
942# define	ICMP_REDIRECT_TOSHOST	3
943#endif
944#ifndef	ICMP_ALTHOSTADDR
945# define	ICMP_ALTHOSTADDR	6
946#endif
947#ifndef	ICMP_TIMXCEED
948# define	ICMP_TIMXCEED	11
949#endif
950#ifndef	ICMP_TIMXCEED_INTRANS
951# define	ICMP_TIMXCEED_INTRANS	0
952#endif
953#ifndef	ICMP_TIMXCEED_REASS
954# define		ICMP_TIMXCEED_REASS	1
955#endif
956#ifndef	ICMP_PARAMPROB
957# define	ICMP_PARAMPROB	12
958#endif
959#ifndef	ICMP_PARAMPROB_ERRATPTR
960# define	ICMP_PARAMPROB_ERRATPTR	0
961#endif
962#ifndef	ICMP_PARAMPROB_OPTABSENT
963# define	ICMP_PARAMPROB_OPTABSENT	1
964#endif
965#ifndef	ICMP_PARAMPROB_LENGTH
966# define	ICMP_PARAMPROB_LENGTH	2
967#endif
968#ifndef ICMP_TSTAMP
969# define	ICMP_TSTAMP	13
970#endif
971#ifndef ICMP_TSTAMPREPLY
972# define	ICMP_TSTAMPREPLY	14
973#endif
974#ifndef ICMP_IREQ
975# define	ICMP_IREQ	15
976#endif
977#ifndef ICMP_IREQREPLY
978# define	ICMP_IREQREPLY	16
979#endif
980#ifndef	ICMP_MASKREQ
981# define	ICMP_MASKREQ	17
982#endif
983#ifndef ICMP_MASKREPLY
984# define	ICMP_MASKREPLY	18
985#endif
986#ifndef	ICMP_TRACEROUTE
987# define	ICMP_TRACEROUTE	30
988#endif
989#ifndef	ICMP_DATACONVERR
990# define	ICMP_DATACONVERR	31
991#endif
992#ifndef	ICMP_MOBILE_REDIRECT
993# define	ICMP_MOBILE_REDIRECT	32
994#endif
995#ifndef	ICMP_IPV6_WHEREAREYOU
996# define	ICMP_IPV6_WHEREAREYOU	33
997#endif
998#ifndef	ICMP_IPV6_IAMHERE
999# define	ICMP_IPV6_IAMHERE	34
1000#endif
1001#ifndef	ICMP_MOBILE_REGREQUEST
1002# define	ICMP_MOBILE_REGREQUEST	35
1003#endif
1004#ifndef	ICMP_MOBILE_REGREPLY
1005# define	ICMP_MOBILE_REGREPLY	36
1006#endif
1007#ifndef	ICMP_SKIP
1008# define	ICMP_SKIP	39
1009#endif
1010#ifndef	ICMP_PHOTURIS
1011# define	ICMP_PHOTURIS	40
1012#endif
1013#ifndef	ICMP_PHOTURIS_UNKNOWN_INDEX
1014# define	ICMP_PHOTURIS_UNKNOWN_INDEX	1
1015#endif
1016#ifndef	ICMP_PHOTURIS_AUTH_FAILED
1017# define	ICMP_PHOTURIS_AUTH_FAILED	2
1018#endif
1019#ifndef	ICMP_PHOTURIS_DECRYPT_FAILED
1020# define	ICMP_PHOTURIS_DECRYPT_FAILED	3
1021#endif
1022#ifndef	IPVERSION
1023# define	IPVERSION	4
1024#endif
1025#ifndef	IPOPT_MINOFF
1026# define	IPOPT_MINOFF	4
1027#endif
1028#ifndef	IPOPT_COPIED
1029# define	IPOPT_COPIED(x)	((x)&0x80)
1030#endif
1031#ifndef	IPOPT_EOL
1032# define	IPOPT_EOL	0
1033#endif
1034#ifndef	IPOPT_NOP
1035# define	IPOPT_NOP	1
1036#endif
1037#ifndef	IP_MF
1038# define	IP_MF	((u_short)0x2000)
1039#endif
1040#ifndef	ETHERTYPE_IP
1041# define	ETHERTYPE_IP	((u_short)0x0800)
1042#endif
1043#ifndef	TH_FIN
1044# define	TH_FIN	0x01
1045#endif
1046#ifndef	TH_SYN
1047# define	TH_SYN	0x02
1048#endif
1049#ifndef	TH_RST
1050# define	TH_RST	0x04
1051#endif
1052#ifndef	TH_PUSH
1053# define	TH_PUSH	0x08
1054#endif
1055#ifndef	TH_ACK
1056# define	TH_ACK	0x10
1057#endif
1058#ifndef	TH_URG
1059# define	TH_URG	0x20
1060#endif
1061#undef	TH_ACKMASK
1062#define	TH_ACKMASK	(TH_FIN|TH_SYN|TH_RST|TH_ACK)
1063
1064#ifndef	IPOPT_EOL
1065# define	IPOPT_EOL	0
1066#endif
1067#ifndef	IPOPT_NOP
1068# define	IPOPT_NOP	1
1069#endif
1070#ifndef	IPOPT_RR
1071# define	IPOPT_RR	7
1072#endif
1073#ifndef	IPOPT_TS
1074# define	IPOPT_TS	68
1075#endif
1076#ifndef	IPOPT_SECURITY
1077# define	IPOPT_SECURITY	130
1078#endif
1079#ifndef	IPOPT_LSRR
1080# define	IPOPT_LSRR	131
1081#endif
1082#ifndef	IPOPT_SATID
1083# define	IPOPT_SATID	136
1084#endif
1085#ifndef	IPOPT_SSRR
1086# define	IPOPT_SSRR	137
1087#endif
1088#ifndef	IPOPT_SECUR_UNCLASS
1089# define	IPOPT_SECUR_UNCLASS	((u_short)0x0000)
1090#endif
1091#ifndef	IPOPT_SECUR_CONFID
1092# define	IPOPT_SECUR_CONFID	((u_short)0xf135)
1093#endif
1094#ifndef	IPOPT_SECUR_EFTO
1095# define	IPOPT_SECUR_EFTO	((u_short)0x789a)
1096#endif
1097#ifndef	IPOPT_SECUR_MMMM
1098# define	IPOPT_SECUR_MMMM	((u_short)0xbc4d)
1099#endif
1100#ifndef	IPOPT_SECUR_RESTR
1101# define	IPOPT_SECUR_RESTR	((u_short)0xaf13)
1102#endif
1103#ifndef	IPOPT_SECUR_SECRET
1104# define	IPOPT_SECUR_SECRET	((u_short)0xd788)
1105#endif
1106#ifndef IPOPT_SECUR_TOPSECRET
1107# define	IPOPT_SECUR_TOPSECRET	((u_short)0x6bc5)
1108#endif
1109#ifndef IPOPT_OLEN
1110# define	IPOPT_OLEN	1
1111#endif
1112#ifndef	IPPROTO_HOPOPTS
1113# define	IPPROTO_HOPOPTS	0
1114#endif
1115#ifndef	IPPROTO_IPIP
1116# define	IPPROTO_IPIP	4
1117#endif
1118#ifndef	IPPROTO_ENCAP
1119# define	IPPROTO_ENCAP	98
1120#endif
1121#ifndef	IPPROTO_IPV6
1122# define	IPPROTO_IPV6	41
1123#endif
1124#ifndef	IPPROTO_ROUTING
1125# define	IPPROTO_ROUTING	43
1126#endif
1127#ifndef	IPPROTO_FRAGMENT
1128# define	IPPROTO_FRAGMENT	44
1129#endif
1130#ifndef	IPPROTO_GRE
1131# define	IPPROTO_GRE	47	/* GRE encaps RFC 1701 */
1132#endif
1133#ifndef	IPPROTO_ESP
1134# define	IPPROTO_ESP	50
1135#endif
1136#ifndef	IPPROTO_AH
1137# define	IPPROTO_AH	51
1138#endif
1139#ifndef	IPPROTO_ICMPV6
1140# define	IPPROTO_ICMPV6	58
1141#endif
1142#ifndef	IPPROTO_NONE
1143# define	IPPROTO_NONE	59
1144#endif
1145#ifndef	IPPROTO_DSTOPTS
1146# define	IPPROTO_DSTOPTS	60
1147#endif
1148#ifndef	IPPROTO_MOBILITY
1149# define	IPPROTO_MOBILITY	135
1150#endif
1151
1152#ifndef	ICMP_ROUTERADVERT
1153# define	ICMP_ROUTERADVERT	9
1154#endif
1155#ifndef	ICMP_ROUTERSOLICIT
1156# define	ICMP_ROUTERSOLICIT	10
1157#endif
1158#ifndef	ICMP6_DST_UNREACH
1159# define	ICMP6_DST_UNREACH	1
1160#endif
1161#ifndef	ICMP6_PACKET_TOO_BIG
1162# define	ICMP6_PACKET_TOO_BIG	2
1163#endif
1164#ifndef	ICMP6_TIME_EXCEEDED
1165# define	ICMP6_TIME_EXCEEDED	3
1166#endif
1167#ifndef	ICMP6_PARAM_PROB
1168# define	ICMP6_PARAM_PROB	4
1169#endif
1170
1171#ifndef	ICMP6_ECHO_REQUEST
1172# define	ICMP6_ECHO_REQUEST	128
1173#endif
1174#ifndef	ICMP6_ECHO_REPLY
1175# define	ICMP6_ECHO_REPLY	129
1176#endif
1177#ifndef	ICMP6_MEMBERSHIP_QUERY
1178# define	ICMP6_MEMBERSHIP_QUERY	130
1179#endif
1180#ifndef	MLD6_LISTENER_QUERY
1181# define	MLD6_LISTENER_QUERY	130
1182#endif
1183#ifndef	ICMP6_MEMBERSHIP_REPORT
1184# define	ICMP6_MEMBERSHIP_REPORT	131
1185#endif
1186#ifndef	MLD6_LISTENER_REPORT
1187# define	MLD6_LISTENER_REPORT	131
1188#endif
1189#ifndef	ICMP6_MEMBERSHIP_REDUCTION
1190# define	ICMP6_MEMBERSHIP_REDUCTION	132
1191#endif
1192#ifndef	MLD6_LISTENER_DONE
1193# define	MLD6_LISTENER_DONE	132
1194#endif
1195#ifndef	ND_ROUTER_SOLICIT
1196# define	ND_ROUTER_SOLICIT	133
1197#endif
1198#ifndef	ND_ROUTER_ADVERT
1199# define	ND_ROUTER_ADVERT	134
1200#endif
1201#ifndef	ND_NEIGHBOR_SOLICIT
1202# define	ND_NEIGHBOR_SOLICIT	135
1203#endif
1204#ifndef	ND_NEIGHBOR_ADVERT
1205# define	ND_NEIGHBOR_ADVERT	136
1206#endif
1207#ifndef	ND_REDIRECT
1208# define	ND_REDIRECT	137
1209#endif
1210#ifndef	ICMP6_ROUTER_RENUMBERING
1211# define	ICMP6_ROUTER_RENUMBERING	138
1212#endif
1213#ifndef	ICMP6_WRUREQUEST
1214# define	ICMP6_WRUREQUEST	139
1215#endif
1216#ifndef	ICMP6_WRUREPLY
1217# define	ICMP6_WRUREPLY		140
1218#endif
1219#ifndef	ICMP6_FQDN_QUERY
1220# define	ICMP6_FQDN_QUERY	139
1221#endif
1222#ifndef	ICMP6_FQDN_REPLY
1223# define	ICMP6_FQDN_REPLY	140
1224#endif
1225#ifndef	ICMP6_NI_QUERY
1226# define	ICMP6_NI_QUERY		139
1227#endif
1228#ifndef	ICMP6_NI_REPLY
1229# define	ICMP6_NI_REPLY		140
1230#endif
1231#ifndef	MLD6_MTRACE_RESP
1232# define	MLD6_MTRACE_RESP	200
1233#endif
1234#ifndef	MLD6_MTRACE
1235# define	MLD6_MTRACE		201
1236#endif
1237#ifndef	ICMP6_HADISCOV_REQUEST
1238# define	ICMP6_HADISCOV_REQUEST	202
1239#endif
1240#ifndef	ICMP6_HADISCOV_REPLY
1241# define	ICMP6_HADISCOV_REPLY	203
1242#endif
1243#ifndef	ICMP6_MOBILEPREFIX_SOLICIT
1244# define	ICMP6_MOBILEPREFIX_SOLICIT	204
1245#endif
1246#ifndef	ICMP6_MOBILEPREFIX_ADVERT
1247# define	ICMP6_MOBILEPREFIX_ADVERT	205
1248#endif
1249#ifndef	ICMP6_MAXTYPE
1250# define	ICMP6_MAXTYPE		205
1251#endif
1252
1253#ifndef	ICMP6_DST_UNREACH_NOROUTE
1254# define	ICMP6_DST_UNREACH_NOROUTE	0
1255#endif
1256#ifndef	ICMP6_DST_UNREACH_ADMIN
1257# define	ICMP6_DST_UNREACH_ADMIN		1
1258#endif
1259#ifndef	ICMP6_DST_UNREACH_NOTNEIGHBOR
1260# define	ICMP6_DST_UNREACH_NOTNEIGHBOR	2
1261#endif
1262#ifndef	ICMP6_DST_UNREACH_BEYONDSCOPE
1263# define	ICMP6_DST_UNREACH_BEYONDSCOPE	2
1264#endif
1265#ifndef	ICMP6_DST_UNREACH_ADDR
1266# define	ICMP6_DST_UNREACH_ADDR		3
1267#endif
1268#ifndef	ICMP6_DST_UNREACH_NOPORT
1269# define	ICMP6_DST_UNREACH_NOPORT	4
1270#endif
1271#ifndef	ICMP6_TIME_EXCEED_TRANSIT
1272# define	ICMP6_TIME_EXCEED_TRANSIT	0
1273#endif
1274#ifndef	ICMP6_TIME_EXCEED_REASSEMBLY
1275# define	ICMP6_TIME_EXCEED_REASSEMBLY	1
1276#endif
1277
1278#ifndef	ICMP6_NI_SUCCESS
1279# define	ICMP6_NI_SUCCESS	0
1280#endif
1281#ifndef	ICMP6_NI_REFUSED
1282# define	ICMP6_NI_REFUSED	1
1283#endif
1284#ifndef	ICMP6_NI_UNKNOWN
1285# define	ICMP6_NI_UNKNOWN	2
1286#endif
1287
1288#ifndef	ICMP6_ROUTER_RENUMBERING_COMMAND
1289# define	ICMP6_ROUTER_RENUMBERING_COMMAND	0
1290#endif
1291#ifndef	ICMP6_ROUTER_RENUMBERING_RESULT
1292# define	ICMP6_ROUTER_RENUMBERING_RESULT	1
1293#endif
1294#ifndef	ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET
1295# define	ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET	255
1296#endif
1297
1298#ifndef	ICMP6_PARAMPROB_HEADER
1299# define	ICMP6_PARAMPROB_HEADER	0
1300#endif
1301#ifndef	ICMP6_PARAMPROB_NEXTHEADER
1302# define	ICMP6_PARAMPROB_NEXTHEADER	1
1303#endif
1304#ifndef	ICMP6_PARAMPROB_OPTION
1305# define	ICMP6_PARAMPROB_OPTION	2
1306#endif
1307
1308#ifndef	ICMP6_NI_SUBJ_IPV6
1309# define	ICMP6_NI_SUBJ_IPV6	0
1310#endif
1311#ifndef	ICMP6_NI_SUBJ_FQDN
1312# define	ICMP6_NI_SUBJ_FQDN	1
1313#endif
1314#ifndef	ICMP6_NI_SUBJ_IPV4
1315# define	ICMP6_NI_SUBJ_IPV4	2
1316#endif
1317
1318#ifndef	MLD_MTRACE_RESP
1319# define	MLD_MTRACE_RESP		200
1320#endif
1321#ifndef	MLD_MTRACE
1322# define	MLD_MTRACE		201
1323#endif
1324#ifndef	MLD6_MTRACE_RESP
1325# define	MLD6_MTRACE_RESP	MLD_MTRACE_RESP
1326#endif
1327#ifndef	MLD6_MTRACE
1328# define	MLD6_MTRACE		MLD_MTRACE
1329#endif
1330
1331#if !defined(IPV6_FLOWINFO_MASK)
1332# if (BYTE_ORDER == BIG_ENDIAN) || defined(_BIG_ENDIAN)
1333#  define IPV6_FLOWINFO_MASK	0x0fffffff	/* flow info (28 bits) */
1334# else
1335#  if(BYTE_ORDER == LITTLE_ENDIAN) || !defined(_BIG_ENDIAN)
1336#   define IPV6_FLOWINFO_MASK	0xffffff0f	/* flow info (28 bits) */
1337#  endif /* LITTLE_ENDIAN */
1338# endif
1339#endif
1340#if !defined(IPV6_FLOWLABEL_MASK)
1341# if (BYTE_ORDER == BIG_ENDIAN) || defined(_BIG_ENDIAN)
1342#  define IPV6_FLOWLABEL_MASK	0x000fffff	/* flow label (20 bits) */
1343# else
1344#  if (BYTE_ORDER == LITTLE_ENDIAN) || !defined(_BIG_ENDIAN)
1345#   define IPV6_FLOWLABEL_MASK	0xffff0f00	/* flow label (20 bits) */
1346#  endif /* LITTLE_ENDIAN */
1347# endif
1348#endif
1349
1350/*
1351 * ECN is a new addition to TCP - RFC 2481
1352 */
1353#ifndef TH_ECN
1354# define	TH_ECN	0x40
1355#endif
1356#ifndef TH_CWR
1357# define	TH_CWR	0x80
1358#endif
1359#define	TH_ECNALL	(TH_ECN|TH_CWR)
1360
1361/*
1362 * TCP States
1363 */
1364#define IPF_TCPS_LISTEN		0	/* listening for connection */
1365#define IPF_TCPS_SYN_SENT	1	/* active, have sent syn */
1366#define IPF_TCPS_SYN_RECEIVED	2	/* have send and received syn */
1367#define IPF_TCPS_HALF_ESTAB	3	/* for connections not fully "up" */
1368/* states < IPF_TCPS_ESTABLISHED are those where connections not established */
1369#define IPF_TCPS_ESTABLISHED	4	/* established */
1370#define IPF_TCPS_CLOSE_WAIT	5	/* rcvd fin, waiting for close */
1371/* states > IPF_TCPS_CLOSE_WAIT are those where user has closed */
1372#define IPF_TCPS_FIN_WAIT_1	6	/* have closed, sent fin */
1373#define IPF_TCPS_CLOSING	7	/* closed xchd FIN; await FIN ACK */
1374#define IPF_TCPS_LAST_ACK	8	/* had fin and close; await FIN ACK */
1375/* states > IPF_TCPS_CLOSE_WAIT && < IPF_TCPS_FIN_WAIT_2 await ACK of FIN */
1376#define IPF_TCPS_FIN_WAIT_2	9	/* have closed, fin is acked */
1377#define IPF_TCPS_TIME_WAIT	10	/* in 2*msl quiet wait after close */
1378#define IPF_TCPS_CLOSED		11	/* closed */
1379#define IPF_TCP_NSTATES		12
1380
1381#define	TCP_MSL			120
1382
1383#undef	ICMP_MAX_UNREACH
1384#define	ICMP_MAX_UNREACH	14
1385#undef	ICMP_MAXTYPE
1386#define	ICMP_MAXTYPE		18
1387
1388#ifndef	LOG_FTP
1389# define	LOG_FTP		(11<<3)
1390#endif
1391#ifndef	LOG_AUTHPRIV
1392# define	LOG_AUTHPRIV	(10<<3)
1393#endif
1394#ifndef	LOG_AUDIT
1395# define	LOG_AUDIT	(13<<3)
1396#endif
1397#ifndef	LOG_NTP
1398# define	LOG_NTP		(12<<3)
1399#endif
1400#ifndef	LOG_SECURITY
1401# define	LOG_SECURITY	(13<<3)
1402#endif
1403#ifndef	LOG_LFMT
1404# define	LOG_LFMT	(14<<3)
1405#endif
1406#ifndef	LOG_CONSOLE
1407# define	LOG_CONSOLE	(14<<3)
1408#endif
1409
1410/*
1411 * ICMP error replies have an IP header (20 bytes), 8 bytes of ICMP data,
1412 * another IP header and then 64 bits of data, totalling 56.  Of course,
1413 * the last 64 bits is dependent on that being available.
1414 */
1415#define	ICMPERR_ICMPHLEN	8
1416#define	ICMPERR_IPICMPHLEN	(20 + 8)
1417#define	ICMPERR_MINPKTLEN	(20 + 8 + 20)
1418#define	ICMPERR_MAXPKTLEN	(20 + 8 + 20 + 8)
1419#define ICMP6ERR_MINPKTLEN	(40 + 8)
1420#define ICMP6ERR_IPICMPHLEN	(40 + 8 + 40)
1421
1422#ifndef MIN
1423# define	MIN(a,b)	(((a)<(b))?(a):(b))
1424#endif
1425
1426#ifdef RESCUE
1427# undef IPFILTER_BPF
1428#endif
1429
1430#ifdef IPF_DEBUG
1431# define	DPRINT(x)	printf x
1432#else
1433# define	DPRINT(x)
1434#endif
1435
1436#ifdef DTRACE_PROBE
1437# ifdef _KERNEL
1438#  define	DT(_n)			DTRACE_PROBE(_n)
1439#  define	DT1(_n,_a,_b)		DTRACE_PROBE1(_n,_a,_b)
1440#  define	DT2(_n,_a,_b,_c,_d)	DTRACE_PROBE2(_n,_a,_b,_c,_d)
1441#  define	DT3(_n,_a,_b,_c,_d,_e,_f)	\
1442					DTRACE_PROBE3(_n,_a,_b,_c,_d,_e,_f)
1443#  define	DT4(_n,_a,_b,_c,_d,_e,_f,_g,_h) \
1444				DTRACE_PROBE4(_n,_a,_b,_c,_d,_e,_f,_g,_h)
1445# else
1446#  define	DT(_n)
1447#  define	DT1(_n,_a,_b)
1448#  define	DT2(_n,_a,_b,_c,_d)
1449#  define	DT3(_n,_a,_b,_c,_d,_e,_f)
1450#  define	DT4(_n,_a,_b,_c,_d,_e,_f,_g,_h)
1451# endif
1452#else
1453# define	DT(_n)
1454# define	DT1(_n,_a,_b)
1455# define	DT2(_n,_a,_b,_c,_d)
1456# define	DT3(_n,_a,_b,_c,_d,_e,_f)
1457# define	DT4(_n,_a,_b,_c,_d,_e,_f,_g,_h)
1458#endif
1459
1460struct ip6_routing {
1461	u_char	ip6r_nxt;	/* next header */
1462	u_char	ip6r_len;	/* length in units of 8 octets */
1463	u_char	ip6r_type;	/* always zero */
1464	u_char	ip6r_segleft;	/* segments left */
1465	u_32_t	ip6r_reserved;	/* reserved field */
1466};
1467
1468#endif	/* __IP_COMPAT_H__ */
1469