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