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