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