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