xref: /illumos-gate/usr/src/uts/common/inet/ipf/netinet/ip_compat.h (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1 /*
2  * Copyright (C) 1999-2001, 2003 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
7  * Use is subject to license terms.
8  */
9 
10 #pragma ident	"%Z%%M%	%I%	%E% SMI"
11 
12 #ifndef	__IP_COMPAT_H__
13 #define	__IP_COMPAT_H__
14 
15 #ifndef	__P
16 # ifdef	__STDC__
17 #  define	__P(x)  x
18 # else
19 #  define	__P(x)  ()
20 # endif
21 #endif
22 #ifndef	__STDC__
23 # undef		const
24 # define	const
25 #endif
26 
27 #if defined(_KERNEL) || defined(KERNEL) || defined(__KERNEL__)
28 # undef	KERNEL
29 # undef	_KERNEL
30 # undef 	__KERNEL__
31 # define	KERNEL
32 # define	_KERNEL
33 # define 	__KERNEL__
34 #endif
35 
36 #ifndef	SOLARIS
37 #define	SOLARIS	(defined(sun) && (defined(__svr4__) || defined(__SVR4)))
38 #endif
39 #if (SOLARIS2 >= 8) && (SOLARIS2 < 10)
40 # ifndef	USE_INET6
41 #  define	USE_INET6
42 # endif
43 #endif
44 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 400000) && \
45     !defined(_KERNEL)
46 # define	USE_INET6
47 #endif
48 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105000000) && \
49     !defined(_KERNEL)
50 # define	USE_INET6
51 #endif
52 #if defined(OpenBSD) && (OpenBSD >= 200206) && !defined(_KERNEL)
53 # define	USE_INET6
54 #endif
55 #if defined(__osf__)
56 # define	USE_INET6
57 #endif
58 
59 #if defined(BSD) && (BSD < 199103) && defined(__osf__)
60 # undef BSD
61 # define BSD 199103
62 #endif
63 
64 #if defined(__SVR4) || defined(__svr4__) || defined(__sgi)
65 # define index   strchr
66 # if !defined(_KERNEL)
67 #  define	bzero(a,b)	memset(a,0,b)
68 #  define	bcmp		memcmp
69 #  define	bcopy(a,b,c)	memmove(b,a,c)
70 # endif
71 #endif
72 
73 #ifndef LIFNAMSIZ
74 # ifdef IF_NAMESIZE
75 #  define	LIFNAMSIZ	IF_NAMESIZE
76 # else
77 #  ifdef	IFNAMSIZ
78 #   define	LIFNAMSIZ	IFNAMSIZ
79 #  else
80 #   define	LIFNAMSIZ	16
81 #  endif
82 # endif
83 #endif
84 
85 #if defined(__sgi) || defined(bsdi) || defined(__hpux) || defined(hpux)
86 struct  ether_addr {
87         u_char  ether_addr_octet[6];
88 };
89 #endif
90 
91 #if defined(__sgi) && !defined(IPFILTER_LKM)
92 # ifdef __STDC__
93 #  define IPL_EXTERN(ep) ipfilter##ep
94 # else
95 #  define IPL_EXTERN(ep) ipfilter/**/ep
96 # endif
97 #else
98 # ifdef __STDC__
99 #  define IPL_EXTERN(ep) ipl##ep
100 # else
101 #  define IPL_EXTERN(ep) ipl/**/ep
102 # endif
103 #endif
104 
105 /*
106  * This is a workaround for <sys/uio.h> troubles on FreeBSD and OpenBSD.
107  */
108 #ifndef _KERNEL
109 # define ADD_KERNEL
110 # define _KERNEL
111 # define KERNEL
112 #endif
113 #ifdef __OpenBSD__
114 struct file;
115 #endif
116 #include <sys/uio.h>
117 #ifdef ADD_KERNEL
118 # undef _KERNEL
119 # undef KERNEL
120 #endif
121 
122 
123 /* ----------------------------------------------------------------------- */
124 /*                                  S O L A R I S                          */
125 /* ----------------------------------------------------------------------- */
126 #if SOLARIS
127 # define	MENTAT	1
128 # include	<sys/cmn_err.h>
129 # include	<sys/isa_defs.h>
130 # include	<sys/stream.h>
131 # include	<sys/ioccom.h>
132 # include	<sys/sysmacros.h>
133 # include	<sys/kmem.h>
134 # if SOLARIS2 >= 10
135 #  include	<sys/procset.h>
136 #  include	<sys/proc.h>
137 #  include	<sys/devops.h>
138 #  include	<sys/ddi_impldefs.h>
139 # endif
140 /*
141  * because Solaris 2 defines these in two places :-/
142  */
143 # ifndef	KERNEL
144 #  define	_KERNEL
145 #  undef	RES_INIT
146 # endif /* _KERNEL */
147 
148 # if SOLARIS2 >= 8
149 #  include <netinet/ip6.h>
150 #  include <netinet/icmp6.h>
151 # endif
152 
153 # include <inet/common.h>
154 /* These 5 are defined in <inet/ip.h> and <netinet/ip.h> */
155 # undef	IPOPT_EOL
156 # undef	IPOPT_NOP
157 # undef	IPOPT_LSRR
158 # undef	IPOPT_RR
159 # undef	IPOPT_SSRR
160 # include <inet/ip.h>
161 # undef COPYOUT
162 # include <inet/ip_ire.h>
163 # ifndef	KERNEL
164 #  undef	_KERNEL
165 # endif
166 # if SOLARIS2 >= 8
167 #  include <inet/ip_if.h>
168 #  define	ipif_local_addr	ipif_lcl_addr
169 /* Only defined in private include file */
170 #  ifndef	V4_PART_OF_V6
171 #   define	V4_PART_OF_V6(v6)	v6.s6_addr32[3]
172 #  endif
173 struct ip6_ext {
174 	u_char	ip6e_nxt;
175 	u_char	ip6e_len;
176 };
177 # endif /* SOLARIS2 >= 8 */
178 
179 # if SOLARIS2 >= 6
180 #  include <sys/atomic.h>
181 typedef	uint32_t	u_32_t;
182 # else
183 typedef unsigned int	u_32_t;
184 # endif
185 # define	U_32_T	1
186 
187 # ifdef _KERNEL
188 #  define	KRWLOCK_T		krwlock_t
189 #  define	KMUTEX_T		kmutex_t
190 #  include "pfil.h"
191 #  include "qif.h"
192 #  if SOLARIS2 >= 6
193 #   if SOLARIS2 == 6
194 #    define	ATOMIC_INCL(x)		atomic_add_long((uint32_t*)&(x), 1)
195 #    define	ATOMIC_DECL(x)		atomic_add_long((uint32_t*)&(x), -1)
196 #   else
197 #    define	ATOMIC_INCL(x)		atomic_add_long(&(x), 1)
198 #    define	ATOMIC_DECL(x)		atomic_add_long(&(x), -1)
199 #   endif /* SOLARIS2 == 6 */
200 #   define	ATOMIC_INC64(x)		atomic_add_64((uint64_t*)&(x), 1)
201 #   define	ATOMIC_INC32(x)		atomic_add_32((uint32_t*)&(x), 1)
202 #   define	ATOMIC_INC16(x)		atomic_add_16((uint16_t*)&(x), 1)
203 #   define	ATOMIC_DEC64(x)		atomic_add_64((uint64_t*)&(x), -1)
204 #   define	ATOMIC_DEC32(x)		atomic_add_32((uint32_t*)&(x), -1)
205 #   define	ATOMIC_DEC16(x)		atomic_add_16((uint16_t*)&(x), -1)
206 #  else
207 #   define	ATOMIC_INC(x)		{ mutex_enter(&ipf_rw); (x)++; \
208 					  mutex_exit(&ipf_rw); }
209 #   define	ATOMIC_DEC(x)		{ mutex_enter(&ipf_rw); (x)--; \
210 					  mutex_exit(&ipf_rw); }
211 #  endif /* SOLARIS2 >= 6 */
212 #  define	USE_MUTEXES
213 #  define	MUTEX_ENTER(x)		mutex_enter(&(x)->ipf_lk)
214 #  define	READ_ENTER(x)		rw_enter(&(x)->ipf_lk, RW_READER)
215 #  define	WRITE_ENTER(x)		rw_enter(&(x)->ipf_lk, RW_WRITER)
216 #  define	MUTEX_DOWNGRADE(x)	rw_downgrade(&(x)->ipf_lk)
217 # if SOLARIS2 >= 6
218 #  define	RWLOCK_INIT(x, y)	rw_init(&(x)->ipf_lk, NULL,  \
219 						RW_DRIVER, NULL)
220 #  else
221 #  define	RWLOCK_INIT(x, y)	rw_init(&(x)->ipf_lk, (y),  \
222 						RW_DRIVER, NULL)
223 #  endif /* SOLARIS2 >= 6 */
224 
225 #  define	RWLOCK_EXIT(x)		rw_exit(&(x)->ipf_lk)
226 #  define	RW_DESTROY(x)		rw_destroy(&(x)->ipf_lk)
227 
228 # if SOLARIS2 >= 6
229 #  define	MUTEX_INIT(x, y)	mutex_init(&(x)->ipf_lk, NULL, \
230 						   MUTEX_DRIVER, NULL)
231 #  else
232 #  define	MUTEX_INIT(x, y)	mutex_init(&(x)->ipf_lk, (y), \
233 						   MUTEX_DRIVER, NULL)
234 #  endif /* SOLARIS2 >= 6 */
235 #  define	MUTEX_DESTROY(x)	mutex_destroy(&(x)->ipf_lk)
236 #  define	MUTEX_NUKE(x)		bzero((x), sizeof(*(x)))
237 #  define	MUTEX_EXIT(x)		mutex_exit(&(x)->ipf_lk)
238 #  define	COPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
239 #  define	COPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
240 #  define	BCOPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
241 #  define	BCOPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
242 #  define	UIOMOVE(a,b,c,d)	uiomove((caddr_t)a,b,c,d)
243 #  define	KFREE(x)	kmem_free((char *)(x), sizeof(*(x)))
244 #  define	KFREES(x,s)	kmem_free((char *)(x), (s))
245 #  define	SPL_NET(x)	;
246 #  define	SPL_IMP(x)	;
247 #  undef	SPL_X
248 #  define	SPL_X(x)	;
249 #  ifdef sparc
250 #   define	ntohs(x)	(x)
251 #   define	ntohl(x)	(x)
252 #   define	htons(x)	(x)
253 #   define	htonl(x)	(x)
254 #  endif /* sparc */
255 #  define	KMALLOC(a,b)	(a) = (b)kmem_alloc(sizeof(*(a)), KM_NOSLEEP)
256 #  define	KMALLOCS(a,b,c)	(a) = (b)kmem_alloc((c), KM_NOSLEEP)
257 #  define	GET_MINOR(x)	getminor(x)
258 #ifndef IRE_ILL_CN
259 extern	ill_t	*get_unit __P((char *, int));
260 #  define	GETIFP(n, v)	get_unit(n, v)
261 #  define	IFNAME(x)	((ill_t *)x)->ill_name
262 #  define	COPYIFNAME(x, b) \
263 				strncpy(b, ((ill_t *)x)->ill_name, LIFNAMSIZ)
264 #else
265 extern kmutex_t s_ill_g_head_lock;
266 extern struct s_ill_s *s_ill_g_head;	/* ILL List Head */
267 extern	s_ill_t	*get_unit __P((char *, int));
268 #  define	GETIFP(n, v)	get_unit(n, v)
269 #  define	IFNAME(x)	((s_ill_t *)x)->ill_name
270 #  define	COPYIFNAME(x, b) \
271 				strncpy(b, ((s_ill_t *)x)->ill_name, LIFNAMSIZ)
272 #endif /* IRE_ILL_CN */
273 #  define	GETKTIME(x)	uniqtime((struct timeval *)x)
274 #  define	MSGDSIZE(x)	msgdsize(x)
275 #  define	M_LEN(x)	((x)->b_wptr - (x)->b_rptr)
276 #  define	M_DUPLICATE(x)	dupmsg((x))
277 #  define	MTOD(m,t)	(t)((m)->b_rptr)
278 #  define	MTYPE(m)	((m)->b_datap->db_type)
279 #  define	FREE_MB_T(m)	freemsg(m)
280 #  define	CACHE_HASH(x)	(((qif_t *)(x)->fin_qif)->qf_num & 7)
281 #  define	IPF_PANIC(x,y)	if (x) { printf y; cmn_err(CE_PANIC, "ipf_panic"); }
282 typedef mblk_t mb_t;
283 # endif /* _KERNEL */
284 
285 # if (SOLARIS2 >= 7)
286 #  ifdef lint
287 #   define ALIGN32(ptr)    (ptr ? 0L : 0L)
288 #   define ALIGN16(ptr)    (ptr ? 0L : 0L)
289 #  else
290 #   define ALIGN32(ptr)    (ptr)
291 #   define ALIGN16(ptr)    (ptr)
292 #  endif
293 # endif
294 
295 
296 # define OS_RECOGNISED 1
297 
298 #endif /* SOLARIS */
299 
300 /* ----------------------------------------------------------------------- */
301 /*                                  H P U X                                */
302 /* ----------------------------------------------------------------------- */
303 #ifdef __hpux
304 # define	MENTAT	1
305 # include	<sys/sysmacros.h>
306 # include	<sys/spinlock.h>
307 # include	<sys/lock.h>
308 # include	<sys/stream.h>
309 
310 # ifdef _KERNEL
311 
312 #  define	GETKTIME(x)	uniqtime((struct timeval *)x)
313 
314 #  if HPUXREV == 1111
315 #   include	"kern_svcs.h"
316 #  else
317 #   include	<sys/kern_svcs.h>
318 #  endif
319 #  undef	ti_flags
320 #  undef	TCP_NODELAY
321 #  undef	TCP_MAXSEG
322 #  include <sys/reg.h>
323 #  include "../netinet/ip_info.h"
324 /*
325  * According to /usr/include/sys/spinlock.h on HP-UX 11.00, these functions
326  * are available.  Attempting to use them actually results in unresolved
327  * symbols when it comes time to load the module.
328  */
329 #  if 0
330 #   ifdef __LP64__
331 #    define	ATOMIC_INCL(x)		lock_and_incr_int64(&ipf_rw, x, 1)
332 #    define	ATOMIC_DECL(x)		lock_and_incr_int64(&ipf_rw, x, -1)
333 #   else
334 #    define	ATOMIC_INCL(x)		lock_and_incr_int32(&ipf_rw, x, 1)
335 #    define	ATOMIC_DECL(x)		lock_and_incr_int32(&ipf_rw, x, -1)
336 #   endif
337 #   define	ATOMIC_INC64(x)		lock_and_incr_int64(&ipf_rw, x, 1)
338 #   define	ATOMIC_INC32(x)		lock_and_incr_int32(&ipf_rw, x, 1)
339 #   define	ATOMIC_INC16(x)		lock_and_incr_int16(&ipf_rw, x, 1)
340 #   define	ATOMIC_DEC64(x)		lock_and_incr_int64(&ipf_rw, x, -1)
341 #   define	ATOMIC_DEC32(x)		lock_and_incr_int32(&ipf_rw, x, -1)
342 #   define	ATOMIC_DEC16(x)		lock_and_incr_int16(&ipf_rw, x, -1)
343 #  else /* 0 */
344 #   define	ATOMIC_INC(x)		{ spinlock(&ipf_rw); (x)++; \
345 					  spinunlock(&ipf_rw); }
346 #   define	ATOMIC_DEC(x)		{ spinlock(&ipf_rw); (x)--; \
347 					  spinunlock(&ipf_rw); }
348 #  endif
349 #  define	ip_cksum		ip_csuma
350 #  define	memcpy(a,b,c)		bcopy((caddr_t)b, (caddr_t)a, c)
351 #  define	USE_MUTEXES
352 #  define	MUTEX_INIT(x, y)	initlock(&(x)->ipf_lk, 0, 0, (y))
353 #  define	MUTEX_ENTER(x)		spinlock(&(x)->ipf_lk)
354 #  define	MUTEX_EXIT(x)		spinunlock(&(x)->ipf_lk)
355 #  define	MUTEX_DESTROY(x)
356 #  define	MUTEX_NUKE(x)		bzero((x), sizeof(*(x)))
357 /*
358  * The read-write lock implementation in HP-UX 11.0 is crippled - it can
359  * only be used by threads working in a user context!
360  */
361 #  if 0
362 #   define	MUTEX_DOWNGRADE(x)	lock_write_to_read(x)
363 #   define	KRWLOCK_T		struct rwlock
364 #   define	READ_ENTER(x)		lock_read(&(x)->ipf_lk)
365 #   define	WRITE_ENTER(x)		lock_write(&(x)->ipf_lk)
366 #   define	RWLOCK_INIT(x, y)	lock_init3(&(x)->ipf_lk, 0, 1, 0, y)
367 #   define	RWLOCK_EXIT(x)		lock_done(&(x)->ipf_lk)
368 #  else
369 #   define	KRWLOCK_T		lock_t
370 #   define	KMUTEX_T		lock_t
371 #   define	kmutex_t		lock_t		/* for pfil.h */
372 #   define	krwlock_t		lock_t		/* for pfil.h */
373 #   define	READ_ENTER(x)		MUTEX_ENTER(&(x)->ipf_lk)
374 #   define	WRITE_ENTER(x)		MUTEX_ENTER(&(x)->ipf_lk)
375 #   define	MUTEX_DOWNGRADE(x)
376 #   define	RWLOCK_INIT(x, y)	initlock(&(x)->ipf_lk, 0, 0, y)
377 #   define	RWLOCK_EXIT(x)		spinunlock(&(x)->ipf_lk)
378 #  endif
379 #  define	RW_DESTROY(x)
380 #  define	COPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
381 #  define	COPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
382 #  if HPUXREV > 1100
383 #   define	BCOPYIN(a,b,c)	memcpy((caddr_t)(a), (caddr_t)(b), (c))
384 #   define	BCOPYOUT(a,b,c)	memcpy((caddr_t)(a), (caddr_t)(b), (c))
385 #  else
386 #   define	BCOPYIN(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
387 #   define	BCOPYOUT(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
388 #  endif
389 #  define	SPL_NET(x)	;
390 #  define	SPL_IMP(x)	;
391 #  undef	SPL_X
392 #  define	SPL_X(x)	;
393 extern	ifinfo_t	*get_unit __P((char *, int));
394 #  define	GETIFP(n, v)	get_unit(n, v)
395 #  define	IFNAME(x, b)	((ifinfo_t *)x)->ifi_name
396 #  define	COPYIFNAME(x, b) \
397 				strncpy(b, ((ifinfo_t *)x)->ifi_name, \
398 					LIFNAMSIZ)
399 #  define	UIOMOVE(a,b,c,d)	uiomove((caddr_t)a,b,c,d)
400 #  define	SLEEP(id, n)	{ lock_t *_l = get_sleep_lock((caddr_t)id); \
401 				  sleep(id, PZERO+1); \
402 				  spinunlock(_l); \
403 				}
404 #  define	WAKEUP(id)	{ lock_t *_l = get_sleep_lock((caddr_t)id); \
405 				  wakeup(id); \
406 				  spinunlock(_l); \
407 				}
408 #  define	KMALLOC(a, b)	MALLOC((a), b, sizeof(*(a)), M_IOSYS, M_NOWAIT)
409 #  define	KMALLOCS(a, b, c)	MALLOC((a), b, (c), M_IOSYS, M_NOWAIT)
410 #  define	KFREE(x)	kmem_free((char *)(x), sizeof(*(x)))
411 #  define	KFREES(x,s)	kmem_free((char *)(x), (s))
412 #  define	MSGDSIZE(x)	msgdsize(x)
413 #  define	M_LEN(x)	((x)->b_wptr - (x)->b_rptr)
414 #  define	M_DUPLICATE(x)	dupmsg((x))
415 #  define	MTOD(m,t)	(t)((m)->b_rptr)
416 #  define	MTYPE(m)	((m)->b_datap->db_type)
417 #  define	FREE_MB_T(m)	freemsg(m)
418 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
419 typedef mblk_t mb_t;
420 
421 #  define	CACHE_HASH(x)	(((qif_t *)(x)->fin_qif)->qf_num & 7)
422 
423 #  include "pfil.h"
424 #  include "qif.h"
425 
426 # else /* _KERNEL */
427 
428 typedef	unsigned char uchar_t;
429 
430 #  ifndef	_SYS_STREAM_INCLUDED
431 typedef char * mblk_t;
432 typedef void * queue_t;
433 typedef	u_long ulong;
434 #  endif
435 #  include <netinet/ip_info.h>
436 
437 # endif /* _KERNEL */
438 
439 # ifdef lint
440 #  define ALIGN32(ptr)    (ptr ? 0L : 0L)
441 #  define ALIGN16(ptr)    (ptr ? 0L : 0L)
442 # else
443 #  define ALIGN32(ptr)    (ptr)
444 #  define ALIGN16(ptr)    (ptr)
445 # endif
446 
447 typedef	int		minor_t;
448 typedef unsigned int	u_32_t;
449 # define	U_32_T	1
450 
451 # define OS_RECOGNISED 1
452 
453 #endif /* __hpux */
454 
455 /* ----------------------------------------------------------------------- */
456 /*                                  I R I X                                */
457 /* ----------------------------------------------------------------------- */
458 #ifdef __sgi
459 # undef		MENTAT
460 typedef u_int32_t       u_32_t;
461 # define	U_32_T	1
462 
463 # ifdef INET6
464 #  define USE_INET6
465 # endif
466 
467 # define  hz HZ
468 # include <sys/ksynch.h>
469 # define	IPF_LOCK_PL	plhi
470 # include <sys/sema.h>
471 # undef kmutex_t
472 typedef struct {
473 	lock_t *l;
474 	int pl;
475 } kmutex_t;
476 
477 # ifdef MUTEX_INIT
478 #  define	KMUTEX_T		mutex_t
479 # else
480 #  define	KMUTEX_T		kmutex_t
481 #  define	KRWLOCK_T		kmutex_t
482 # endif
483 
484 # ifdef _KERNEL
485 #  define	ATOMIC_INC(x)		{ MUTEX_ENTER(&ipf_rw); \
486 					  (x)++; MUTEX_EXIT(&ipf_rw); }
487 #  define	ATOMIC_DEC(x)		{ MUTEX_ENTER(&ipf_rw); \
488 					  (x)--; MUTEX_EXIT(&ipf_rw); }
489 #  define	USE_MUTEXES
490 #  ifdef MUTEX_INIT
491 #   include <sys/atomic_ops.h>
492 #   define	ATOMIC_INCL(x)		atomicAddUlong(&(x), 1)
493 #   define	ATOMIC_INC64(x)		atomicAddUint64(&(x), 1)
494 #   define	ATOMIC_INC32(x)		atomicAddUint(&(x), 1)
495 #   define	ATOMIC_INC16		ATOMIC_INC
496 #   define	ATOMIC_DECL(x)		atomicAddUlong(&(x), -1)
497 #   define	ATOMIC_DEC64(x)		atomicAddUint64(&(x), -1)
498 #   define	ATOMIC_DEC32(x)		atomicAddUint(&(x), -1)
499 #   define	ATOMIC_DEC16		ATOMIC_DEC
500 #   undef	MUTEX_INIT
501 #   define	MUTEX_INIT(x, y)	mutex_init(&(x)->ipf_lk,
502 						   MUTEX_DEFAULT, y)
503 #   undef	MUTEX_ENTER
504 #   define	MUTEX_ENTER(x)		mutex_lock(&(x)->ipf_lk, 0)
505 #   undef	MUTEX_EXIT
506 #   define	MUTEX_EXIT(x)		mutex_unlock(&(x)->ipf_lk)
507 #   undef	MUTEX_DESTROY
508 #   define	MUTEX_DESTROY(x)	mutex_destroy(&(x)->ipf_lk)
509 #   define	MUTEX_DOWNGRADE(x)	mrdemote(&(x)->ipf_lk)
510 #   define	KRWLOCK_T		mrlock_t
511 #   define	RWLOCK_INIT(x, y)	mrinit(&(x)->ipf_lk, y)
512 #   undef	RW_DESTROY
513 #   define	RW_DESTROY(x)		mrfree(&(x)->ipf_lk)
514 #   define	READ_ENTER(x)		RW_RDLOCK(&(x)->ipf_lk)
515 #   define	WRITE_ENTER(x)		RW_WRLOCK(&(x)->ipf_lk)
516 #   define	RWLOCK_EXIT(x)		RW_UNLOCK(&(x)->ipf_lk)
517 #  else
518 #   define	READ_ENTER(x)		MUTEX_ENTER(&(x)->ipf_lk)
519 #   define	WRITE_ENTER(x)		MUTEX_ENTER(&(x)->ipf_lk)
520 #   define	MUTEX_DOWNGRADE(x)	;
521 #   define	RWLOCK_EXIT(x)		MUTEX_EXIT(&(x)->ipf_lk)
522 #   define	MUTEX_EXIT(x)		UNLOCK((x)->ipf_lk.l, (x)->ipf_lk.pl);
523 #   define	MUTEX_INIT(x,y)		(x)->ipf_lk.l = LOCK_ALLOC((uchar_t)-1, IPF_LOCK_PL, (lkinfo_t *)-1, KM_NOSLEEP)
524 #   define	MUTEX_DESTROY(x)	LOCK_DEALLOC((x)->ipf_lk.l)
525 #   define	MUTEX_ENTER(x)		(x)->ipf_lk.pl = LOCK((x)->ipf_lk.l, \
526 							      IPF_LOCK_PL);
527 #  endif
528 #  define	MUTEX_NUKE(x)		bzero((x), sizeof(*(x)))
529 #  define	FREE_MB_T(m)	m_freem(m)
530 #  define	MTOD(m,t)	mtod(m,t)
531 #  define	COPYIN(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
532 #  define	COPYOUT(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
533 #  define	BCOPYIN(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
534 #  define	BCOPYOUT(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
535 #  define	UIOMOVE(a,b,c,d)	uiomove((caddr_t)a,b,c,d)
536 #  define	SLEEP(id, n)	sleep((id), PZERO+1)
537 #  define	WAKEUP(id)	wakeup(id)
538 #  define	KFREE(x)	kmem_free((char *)(x), sizeof(*(x)))
539 #  define	KFREES(x,s)	kmem_free((char *)(x), (s))
540 #  define	GETIFP(n,v)	ifunit(n)
541 #  include <sys/kmem.h>
542 #  include <sys/ddi.h>
543 #  define	KMALLOC(a,b)	(a) = (b)kmem_alloc(sizeof(*(a)), KM_NOSLEEP)
544 #  define	KMALLOCS(a,b,c)	(a) = (b)kmem_alloc((c), KM_NOSLEEP)
545 #  define	GET_MINOR(x)	getminor(x)
546 #  define	SPL_IMP(x)	(x) = splimp()
547 #  define	SPL_NET(x)	(x) = splnet()
548 #  define	SPL_X(x)	(void) splx(x)
549 extern	void	m_copydata __P((struct mbuf *, int, int, caddr_t));
550 extern	void	m_copyback __P((struct mbuf *, int, int, caddr_t));
551 #  define	MSGDSIZE(x)	mbufchainlen(x)
552 #  define	M_LEN(x)	(x)->m_len
553 #  define	M_DUPLICATE(x)	m_copy((x), 0, M_COPYALL)
554 #  define	GETKTIME(x)	microtime((struct timeval *)x)
555 #  define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
556 				  ((struct ifnet *)fin->fin_ifp)->if_unit) & 7)
557 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
558 typedef struct mbuf mb_t;
559 # endif /* _KERNEL */
560 
561 # define OS_RECOGNISED 1
562 
563 #endif /* __sgi */
564 
565 /* ----------------------------------------------------------------------- */
566 /*                                  T R U 6 4                              */
567 /* ----------------------------------------------------------------------- */
568 #ifdef __osf__
569 # undef		MENTAT
570 /*
571  * Really, any arch where sizeof(long) != sizeof(int).
572  */
573 typedef unsigned int    u_32_t;
574 # define	U_32_T	1
575 
576 # include <kern/lock.h>
577 # include <sys/sysmacros.h>
578 
579 # ifdef _KERNEL
580 #  define	KMUTEX_T		simple_lock_data_t
581 #  define	KRWLOCK_T		lock_data_t
582 #  include <net/net_globals.h>
583 #  define	USE_MUTEXES
584 #  define	READ_ENTER(x)		lock_read(&(x)->ipf_lk)
585 #  define	WRITE_ENTER(x)		lock_write(&(x)->ipf_lk)
586 #  define	MUTEX_DOWNGRADE(x)	lock_write_to_read(&(x)->ipf_lk)
587 #  define	RWLOCK_INIT(x, y)	lock_init(&(x)->ipf_lk, TRUE)
588 #  define	RWLOCK_EXIT(x)		lock_done(&(x)->ipf_lk)
589 #  define	RW_DESTROY(x)		lock_terminate(&(x)->ipf_lk)
590 #  define	MUTEX_ENTER(x)		simple_lock(&(x)->ipf_lk)
591 #  define	MUTEX_INIT(x, y)	simple_lock_init(&(x)->ipf_lk)
592 #  define	MUTEX_DESTROY(x)	simple_lock_terminate(&(x)->ipf_lk)
593 #  define	MUTEX_EXIT(x)		simple_unlock(&(x)->ipf_lk)
594 #  define	MUTEX_NUKE(x)		bzero(x, sizeof(*(x)))
595 #  define	ATOMIC_INC64(x)		atomic_incq((uint64_t*)&(x))
596 #  define	ATOMIC_DEC64(x)		atomic_decq((uint64_t*)&(x))
597 #  define	ATOMIC_INC32(x)		atomic_incl((uint32_t*)&(x))
598 #  define	ATOMIC_DEC32(x)		atomic_decl((uint32_t*)&(x))
599 #  define	ATOMIC_INC16(x)		{ simple_lock(&ipf_rw); (x)++; \
600 					  simple_unlock(&ipf_rw); }
601 #  define	ATOMIC_DEC16(x)		{ simple_lock(&ipf_rw); (x)--; \
602 					  simple_unlock(&ipf_rw); }
603 #  define	ATOMIC_INCL(x)		atomic_incl((uint32_t*)&(x))
604 #  define	ATOMIC_DECL(x)		atomic_decl((uint32_t*)&(x))
605 #  define	ATOMIC_INC(x)		{ simple_lock(&ipf_rw); (x)++; \
606 					  simple_unlock(&ipf_rw); }
607 #  define	ATOMIC_DEC(x)		{ simple_lock(&ipf_rw); (x)--; \
608 					  simple_unlock(&ipf_rw); }
609 #  define	SPL_NET(x)		;
610 #  define	SPL_IMP(x)		;
611 #  undef	SPL_X
612 #  define	SPL_X(x)		;
613 #  define	UIOMOVE(a,b,c,d)	uiomove((caddr_t)a, b, d)
614 #  define	FREE_MB_T(m)		m_freem(m)
615 #  define	MTOD(m,t)		mtod(m,t)
616 #  define	GETIFP(n, v)		ifunit(n)
617 #  define	GET_MINOR		getminor
618 #  define	WAKEUP			wakeup
619 #  define	COPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
620 #  define	COPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
621 #  define	BCOPYIN(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
622 #  define	BCOPYOUT(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
623 #  define	KMALLOC(a, b)	MALLOC((a), b, sizeof(*(a)), M_PFILT, M_NOWAIT)
624 #  define	KMALLOCS(a, b, c)	MALLOC((a), b, (c), M_PFILT, M_NOWAIT)
625 #  define	KFREE(x)	FREE((x), M_PFILT)
626 #  define	KFREES(x,s)	FREE((x), M_PFILT)
627 #  define	MSGDSIZE(x)	(x)->m_pkthdr.len
628 #  define	M_LEN(x)	(x)->m_len
629 #  define	M_DUPLICATE(x)	m_copy((x), 0, M_COPYALL)
630 #  define	GETKTIME(x)	microtime((struct timeval *)x)
631 #  define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
632 				  ((struct ifnet *)fin->fin_ifp)->if_unit) & 7)
633 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
634 typedef struct mbuf mb_t;
635 # endif /* _KERNEL */
636 
637 # if (defined(_KERNEL) || defined(_NO_BITFIELDS) || (__STDC__ == 1))
638 #  define	IP_V(x)		((x)->ip_vhl >> 4)
639 #  define	IP_HL(x)	((x)->ip_vhl & 0xf)
640 #  define	IP_V_A(x,y)	(x)->ip_vhl |= (((y) << 4) & 0xf0)
641 #  define	IP_HL_A(x,y)	(x)->ip_vhl |= ((y) & 0xf)
642 #  define	TCP_OFF(x)	((x)->th_xoff >> 4)
643 #  define	TCP_OFF_A(x,y)	(x)->th_xoff = ((y) << 4)
644 # endif
645 
646 /*
647  * These are from's Solaris' #defines for little endian.
648  */
649 #define	IP6F_MORE_FRAG		0x0100
650 #define	IP6F_RESERVED_MASK	0x0600
651 #define	IP6F_OFF_MASK		0xf8ff
652 
653 struct ip6_ext {
654 	u_char	ip6e_nxt;
655 	u_char	ip6e_len;
656 };
657 
658 # define OS_RECOGNISED 1
659 #endif /* __osf__ */
660 
661 /* ----------------------------------------------------------------------- */
662 /*                                  N E T B S D                            */
663 /* ----------------------------------------------------------------------- */
664 #ifdef __NetBSD__
665 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
666 #  if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 104110000)
667 #   include "opt_inet.h"
668 #  endif
669 #  ifdef INET6
670 #   define USE_INET6
671 #  endif
672 # endif
673 
674 # ifdef _KERNEL
675 #  define	MSGDSIZE(x)	(x)->m_pkthdr.len
676 #  define	M_LEN(x)	(x)->m_len
677 #  define	M_DUPLICATE(x)	m_copy((x), 0, M_COPYALL)
678 #  define	GETKTIME(x)	microtime((struct timeval *)x)
679 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
680 typedef struct mbuf mb_t;
681 # endif /* _KERNEL */
682 # if (NetBSD <= 1991011) && (NetBSD >= 199606)
683 #  define	IFNAME(x)	((struct ifnet *)x)->if_xname
684 #  define	COPYIFNAME(x, b) \
685 				strncpy(b, ((struct ifnet *)x)->if_xname, \
686 					LIFNAMSIZ)
687 #  define	CACHE_HASH(x)	((((struct ifnet *)fin->fin_ifp)->if_index)&7)
688 # else
689 #  define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
690 				  ((struct ifnet *)fin->fin_ifp)->if_unit) & 7)
691 # endif
692 
693 typedef	int		minor_t;
694 typedef	u_int32_t	u_32_t;
695 # define	U_32_T	1
696 
697 
698 # define OS_RECOGNISED 1
699 #endif /* __NetBSD__ */
700 
701 
702 /* ----------------------------------------------------------------------- */
703 /*                                F R E E B S D                            */
704 /* ----------------------------------------------------------------------- */
705 #ifdef __FreeBSD__
706 # if defined(_KERNEL) && !defined(IPFILTER_LKM) && !defined(KLD_MODULE)
707 #  if defined(__FreeBSD_version) && (__FreeBSD_version >= 400000)
708 #   include "opt_inet6.h"
709 #  endif
710 #  ifdef INET6
711 #error foo
712 #   define USE_INET6
713 #  endif
714 # endif
715 
716 # if defined(_KERNEL) /* XXX When *isn't* this defined? */
717 #  if (__FreeBSD_version >= 500043)
718 #   include "opt_pfil.h"
719 #  endif /* __FreeBSD_version */
720 #  ifdef PFIL_HOOKS
721 #   define NETBSD_PF
722 #  endif /* PFIL_HOOKS */
723 # endif /* _KERNEL */
724 
725 # if (__FreeBSD_version >= 500043)
726 #  include <sys/mutex.h>
727 #  define	KMUTEX_T		struct mtx
728 #  define	KRWLOCK_T		struct mtx
729 # endif
730 
731 # ifdef _KERNEL
732 #  define	GETKTIME(x)	microtime((struct timeval *)x)
733 
734 #  if (__FreeBSD_version >= 500002)
735 #   include <netinet/in_systm.h>
736 #   include <netinet/ip.h>
737 #   include <machine/in_cksum.h>
738 #  endif
739 
740 #  if (__FreeBSD_version >= 500043)
741 #   define	USE_MUTEXES
742 #   define	MUTEX_ENTER(x)		mtx_lock(&(x)->ipf_lk)
743 #   define	READ_ENTER(x)		mtx_lock(&(x)->ipf_lk)
744 #   define	WRITE_ENTER(x)		mtx_lock(&(x)->ipf_lk)
745 #   define	MUTEX_DOWNGRADE(x)	;
746 #   define	RWLOCK_INIT(x, y)	mtx_init(&(x)->ipf_lk, (y), NULL, \
747 						 MTX_DEF)
748 #   define	RW_DESTROY(x)		mtx_destroy(&(x)->ipf_lk)
749 #   define	RWLOCK_EXIT(x)		mtx_unlock(&(x)->ipf_lk)
750 #   define	MUTEX_EXIT(x)		mtx_unlock(&(x)->ipf_lk)
751 #   define	MUTEX_INIT(x,y)		mtx_init(&(x)->ipf_lk, (y), NULL,\
752 						 MTX_DEF)
753 #   define	MUTEX_DESTROY(x)	mtx_destroy(&(x)->ipf_lk)
754 #  define	MUTEX_NUKE(x)		bzero((x), sizeof(*(x)))
755 #   include <machine/atomic.h>
756 #   define	ATOMIC_INC(x)		{ mtx_lock(&ipf_rw.ipf_lk); (x)++; \
757 					  mtx_unlock(&ipf_rw.ipf_lk); }
758 #   define	ATOMIC_DEC(x)		{ mtx_lock(&ipf_rw.ipf_lk); (x)--; \
759 					  mtx_unlock(&ipf_rw.ipf_lk); }
760 #   if 0
761 #    define	ATOMIC_INCL(x)		atomic_add_long(x, 1)
762 #    define	ATOMIC_INC64(x)		atomic_add_(x, 1)
763 #    define	ATOMIC_INC32(x)		atomic_add_(x, 1)
764 #    define	ATOMIC_INC16(x)		atomic_add_short(x, 1)
765 #    define	ATOMIC_DECL(x)		atomic_subtrct_long(x, -1)
766 #    define	ATOMIC_DEC64(x)		atomic_subtrct_(x, -1)
767 #    define	ATOMIC_DEC32(x)		atomic_subtrct_(x, -1)
768 #    define	ATOMIC_DEC16(x)		atomic_subtrct_short(x, -1)
769 #   endif
770 #   define	SPL_X(x)	;
771 #   define	SPL_NET(x)	;
772 #   define	SPL_IMP(x)	;
773 extern	int	in_cksum __P((struct mbuf *, int));
774 #  endif /* __FreeBSD_version >= 500043 */
775 #  define	MSGDSIZE(x)	(x)->m_pkthdr.len
776 #  define	M_LEN(x)	(x)->m_len
777 #  define	M_DUPLICATE(x)	m_copy((x), 0, M_COPYALL)
778 #  if (__FreeBSD_version >= 500043)
779 #   define	CACHE_HASH(x)	((((struct ifnet *)fin->fin_ifp)->if_index) & 7)
780 #  else
781 #   define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
782 				  ((struct ifnet *)fin->fin_ifp)->if_unit) & 7)
783 #  endif
784 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
785 typedef struct mbuf mb_t;
786 # endif /* _KERNEL */
787 
788 # if __FreeBSD__ < 3
789 #  include <machine/spl.h>
790 # else
791 #  if __FreeBSD__ == 3
792 #   if defined(IPFILTER_LKM) && !defined(ACTUALLY_LKM_NOT_KERNEL)
793 #    define	ACTUALLY_LKM_NOT_KERNEL
794 #   endif
795 #  endif
796 # endif
797 
798 typedef	int		minor_t;
799 typedef	u_int32_t	u_32_t;
800 # define	U_32_T	1
801 
802 # define OS_RECOGNISED 1
803 #endif /* __FreeBSD__ */
804 
805 
806 /* ----------------------------------------------------------------------- */
807 /*                                O P E N B S D                            */
808 /* ----------------------------------------------------------------------- */
809 #ifdef __OpenBSD__
810 # ifdef INET6
811 #  define USE_INET6
812 # endif
813 
814 # ifdef _KERNEL
815 #  define	GETKTIME(x)	microtime((struct timeval *)x)
816 #  define	MSGDSIZE(x)	(x)->m_pkthdr.len
817 #  define	M_LEN(x)	(x)->m_len
818 #  define	M_DUPLICATE(x)	m_copy((x), 0, M_COPYALL)
819 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
820 typedef struct mbuf mb_t;
821 # endif /* _KERNEL */
822 # if (OpenBSD >= 199603)
823 #  define	IFNAME(x, b)	((struct ifnet *)x)->if_xname
824 #  define	COPYIFNAME(x, b) \
825 				strncpy(b, ((struct ifnet *)x)->if_xname, \
826 					LIFNAMSIZ)
827 #  define	CACHE_HASH(x)	((((struct ifnet *)fin->fin_ifp)->if_index)&7)
828 # else
829 #  define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
830 				  ((struct ifnet *)fin->fin_ifp)->if_unit) & 7)
831 # endif
832 
833 typedef	int		minor_t;
834 typedef	u_int32_t	u_32_t;
835 # define	U_32_T	1
836 
837 # define OS_RECOGNISED 1
838 #endif /* __OpenBSD__ */
839 
840 
841 /* ----------------------------------------------------------------------- */
842 /*                                B S D O S                                */
843 /* ----------------------------------------------------------------------- */
844 #ifdef _BSDI_VERSION
845 # ifdef INET6
846 #  define USE_INET6
847 # endif
848 
849 # ifdef _KERNEL
850 #  define	GETKTIME(x)	microtime((struct timeval *)x)
851 #  define	MSGDSIZE(x)	(x)->m_pkthdr.len
852 #  define	M_LEN(x)	(x)->m_len
853 #  define	M_DUPLICATE(x)	m_copy((x), 0, M_COPYALL)
854 #  define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
855 				  ((struct ifnet *)fin->fin_ifp)->if_unit) & 7)
856 typedef struct mbuf mb_t;
857 # endif /* _KERNEL */
858 
859 typedef	u_int32_t	u_32_t;
860 # define	U_32_T	1
861 
862 #endif /* _BSDI_VERSION */
863 
864 
865 /* ----------------------------------------------------------------------- */
866 /*                                  S U N O S 4                            */
867 /* ----------------------------------------------------------------------- */
868 #if defined(sun) && !defined(OS_RECOGNISED) /* SunOS4 */
869 # ifdef _KERNEL
870 #  include	<sys/kmem_alloc.h>
871 #  define	GETKTIME(x)	uniqtime((struct timeval *)x)
872 #  define	MSGDSIZE(x)	mbufchainlen(x)
873 #  define	M_LEN(x)	(x)->m_len
874 #  define	M_DUPLICATE(x)	m_copy((x), 0, M_COPYALL)
875 #  define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
876 				  ((struct ifnet *)fin->fin_ifp)->if_unit) & 7)
877 #  define	GETIFP(n, v)	ifunit(n, IFNAMSIZ)
878 #  define	KFREE(x)	kmem_free((char *)(x), sizeof(*(x)))
879 #  define	KFREES(x,s)	kmem_free((char *)(x), (s))
880 #  define	SLEEP(id, n)	sleep((id), PZERO+1)
881 #  define	WAKEUP(id)	wakeup(id)
882 #  define	UIOMOVE(a,b,c,d)	uiomove((caddr_t)a,b,c,d)
883 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
884 
885 extern	void	m_copydata __P((struct mbuf *, int, int, caddr_t));
886 extern	void	m_copyback __P((struct mbuf *, int, int, caddr_t));
887 
888 typedef struct mbuf mb_t;
889 # endif
890 
891 typedef	int		minor_t;
892 typedef	unsigned int	u_32_t;
893 # define	U_32_T	1
894 
895 # define OS_RECOGNISED 1
896 
897 #endif /* SunOS 4 */
898 
899 
900 #ifndef	OS_RECOGNISED
901 #error	ip_compat.h does not recognise this platform/OS.
902 #endif
903 
904 /* ----------------------------------------------------------------------- */
905 /*                           G E N E R I C                                 */
906 /* ----------------------------------------------------------------------- */
907 #ifndef OS_RECOGNISED
908 #endif
909 
910 /*
911  * Userland locking primitives
912  */
913 typedef	struct	{
914 	u_int	eMm_magic;
915 	int	eMm_held;
916 	char	*eMm_owner;
917 	int	eMm_heldat;
918 	char	*eMm_heldin;
919 } eMmutex_t;
920 
921 typedef	struct	{
922 	u_int	eMrw_magic;
923 	int	eMrw_read;
924 	int	eMrw_write;
925 	char	*eMrw_owner;
926 	int	eMrw_heldat;
927 	char	*eMrw_heldin;
928 } eMrwlock_t;
929 
930 typedef union {
931 #ifdef KMUTEX_T
932 	KMUTEX_T	ipf_lk;
933 #endif
934 	eMmutex_t	ipf_emu;
935 } ipfmutex_t;
936 
937 typedef union {
938 #ifdef KRWLOCK_T
939 	KRWLOCK_T	ipf_lk;
940 #endif
941 	eMrwlock_t	ipf_emu;
942 } ipfrwlock_t;
943 
944 #ifndef _KERNEL
945 typedef	struct	mb_s	{
946 	struct	mb_s	*mb_next;
947 	int		mb_len;
948 	u_long		mb_buf[2048];
949 } mb_t;
950 # define	MSGDSIZE(x)	(x)->mb_len	/* XXX - from ipt.c */
951 # define	M_LEN(x)	(x)->mb_len
952 # define	M_DUPLICATE(x)	(x)
953 # define	GETKTIME(x)	gettimeofday((struct timeval *)(x), NULL)
954 # define	MTOD(m, t)	(t)((m)->mb_buf)
955 # define	FREE_MB_T(x)
956 # ifndef CACHE_HASH
957 #  define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
958 				  ((struct ifnet *)fin->fin_ifp)->if_unit) & 7)
959 # endif
960 
961 # if 0
962 #  define	KMUTEX_T		eMmutex_t
963 # endif
964 # define	MUTEX_DESTROY(x)	eMmutex_destroy(&(x)->ipf_emu)
965 # define	MUTEX_ENTER(x)		eMmutex_enter(&(x)->ipf_emu, \
966 						      __FILE__, __LINE__)
967 # define	MUTEX_EXIT(x)		eMmutex_exit(&(x)->ipf_emu)
968 # define	MUTEX_INIT(x,y)		eMmutex_init(&(x)->ipf_emu, y)
969 # define	MUTEX_NUKE(x)		bzero((x), sizeof(*(x)))
970 
971 # if 0
972 #  define	KRWLOCK_T		eMrwlock_t
973 # endif
974 # define	MUTEX_DOWNGRADE(x)	eMrwlock_downgrade(&(x)->ipf_emu, \
975 							   __FILE__, __LINE__)
976 # define	READ_ENTER(x)		eMrwlock_read_enter(&(x)->ipf_emu, \
977 							    __FILE__, __LINE__)
978 # define	RWLOCK_INIT(x, y)	eMrwlock_init(&(x)->ipf_emu, y)
979 # define	RWLOCK_EXIT(x)		eMrwlock_exit(&(x)->ipf_emu)
980 # define	RW_DESTROY(x)		eMrwlock_destroy(&(x)->ipf_emu)
981 # define	WRITE_ENTER(x)		eMrwlock_write_enter(&(x)->ipf_emu, \
982 							     __FILE__, \
983 							     __LINE__)
984 
985 # define	USE_MUTEXES		1
986 
987 extern void eMmutex_destroy __P((eMmutex_t *));
988 extern void eMmutex_enter __P((eMmutex_t *, char *, int));
989 extern void eMmutex_exit __P((eMmutex_t *));
990 extern void eMmutex_init __P((eMmutex_t *, char *));
991 extern void eMrwlock_destroy __P((eMrwlock_t *));
992 extern void eMrwlock_exit __P((eMrwlock_t *));
993 extern void eMrwlock_init __P((eMrwlock_t *, char *));
994 extern void eMrwlock_read_enter __P((eMrwlock_t *, char *, int));
995 extern void eMrwlock_write_enter __P((eMrwlock_t *, char *, int));
996 extern void eMrwlock_downgrade __P((eMrwlock_t *, char *, int));
997 
998 #endif
999 
1000 #define	MAX_IPV4HDR	((0xf << 2) + sizeof(struct icmp) + sizeof(ip_t) + 8)
1001 
1002 # ifndef	IP_OFFMASK
1003 #  define	IP_OFFMASK	0x1fff
1004 # endif
1005 
1006 #if	BSD > 199306
1007 # define	USE_QUAD_T
1008 # define	U_QUAD_T	u_quad_t
1009 # define	QUAD_T		quad_t
1010 #else /* BSD > 199306 */
1011 # define	U_QUAD_T	u_long
1012 # define	QUAD_T		long
1013 #endif /* BSD > 199306 */
1014 
1015 
1016 #ifdef	USE_INET6
1017 # if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || \
1018      defined(__osf__)
1019 #  include <netinet/ip6.h>
1020 #  include <netinet/icmp6.h>
1021 #  if defined(_KERNEL) && !defined(__osf__)
1022 #   include <netinet6/ip6_var.h>
1023 #  endif
1024 typedef	struct ip6_hdr	ip6_t;
1025 # endif
1026 #endif
1027 
1028 #ifndef	MAX
1029 # define	MAX(a,b)	(((a) > (b)) ? (a) : (b))
1030 #endif
1031 
1032 #if defined(_KERNEL)
1033 # ifdef MENTAT
1034 #  define	COPYDATA	mb_copydata
1035 #  define	COPYBACK	mb_copyback
1036 # else
1037 #  define	COPYDATA	m_copydata
1038 #  define	COPYBACK	m_copyback
1039 # endif
1040 #else
1041 # define	COPYDATA(m, o, l, b)	bcopy(MTOD((mb_t *)m, char *) + (o), \
1042 					      (b), (l))
1043 # define	COPYBACK(m, o, l, b)	bcopy((b), \
1044 					      MTOD((mb_t *)m, char *) + (o), \
1045 					      (l))
1046 #endif
1047 
1048 #ifdef _KERNEL
1049 # if (BSD >= 199306) || defined(__FreeBSD__)
1050 #  if (defined(__NetBSD_Version__) && (__NetBSD_Version__ < 105180000)) || \
1051        defined(__FreeBSD__) || (defined(OpenBSD) && (OpenBSD < 200206)) || \
1052        defined(_BSDI_VERSION)
1053 #   include <vm/vm.h>
1054 #  endif
1055 #  if !defined(__FreeBSD__) || (defined (__FreeBSD_version) && \
1056       (__FreeBSD_version >= 300000))
1057 #   if (defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105180000)) || \
1058        (defined(OpenBSD) && (OpenBSD >= 200111))
1059 #    include <uvm/uvm_extern.h>
1060 #   else
1061 #    include <vm/vm_extern.h>
1062 extern  vm_map_t        kmem_map;
1063 #   endif
1064 #   include <sys/proc.h>
1065 #  else /* !__FreeBSD__ || (__FreeBSD__ && __FreeBSD_version >= 300000) */
1066 #   include <vm/vm_kern.h>
1067 #  endif /* !__FreeBSD__ || (__FreeBSD__ && __FreeBSD_version >= 300000) */
1068 
1069 #  ifdef IPFILTER_M_IPFILTER
1070 #    include <sys/malloc.h>
1071 MALLOC_DECLARE(M_IPFILTER);
1072 #    define	KMALLOC(a, b)	MALLOC((a), b, sizeof(*(a)), M_IPFILTER, \
1073 				       M_NOWAIT)
1074 #    define	KMALLOCS(a, b, c)	MALLOC((a), b, (c), M_IPFILTER, \
1075 					       M_NOWAIT)
1076 #    define	KFREE(x)	FREE((x), M_IPFILTER)
1077 #    define	KFREES(x,s)	FREE((x), M_IPFILTER)
1078 #  else /* IPFILTER_M_IPFILTER */
1079 #   ifdef M_PFIL
1080 #    define	KMALLOC(a, b)	MALLOC((a), b, sizeof(*(a)), M_PFIL, M_NOWAIT)
1081 #    define	KMALLOCS(a, b, c)	MALLOC((a), b, (c), M_PFIL, M_NOWAIT)
1082 #    define	KFREE(x)	FREE((x), M_PFIL)
1083 #    define	KFREES(x,s)	FREE((x), M_PFIL)
1084 #   else /* M_PFIL */
1085 #    define	KMALLOC(a, b)	MALLOC((a), b, sizeof(*(a)), M_TEMP, M_NOWAIT)
1086 #    define	KMALLOCS(a, b, c)	MALLOC((a), b, (c), M_TEMP, M_NOWAIT)
1087 #    define	KFREE(x)	FREE((x), M_TEMP)
1088 #    define	KFREES(x,s)	FREE((x), M_TEMP)
1089 #   endif /* M_PFIL */
1090 #  endif /* IPFILTER_M_IPFILTER */
1091 #  define	UIOMOVE(a,b,c,d)	uiomove(a,b,d)
1092 #  define	SLEEP(id, n)	tsleep((id), PPAUSE|PCATCH, n, 0)
1093 #  define	WAKEUP(id)	wakeup(id)
1094 #  define	GETIFP(n, v)	ifunit(n)
1095 # endif /* (Free)BSD */
1096 
1097 # if !defined(USE_MUTEXES)
1098 #  if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199407)) || \
1099       (defined(OpenBSD) && (OpenBSD >= 200006))
1100 #   define	SPL_NET(x)	x = splsoftnet()
1101 #  else
1102 #   define	SPL_IMP(x)	x = splimp()
1103 #   define	SPL_NET(x)	x = splnet()
1104 #  endif /* NetBSD && (NetBSD <= 1991011) && (NetBSD >= 199407) */
1105 #  define	SPL_X(x)	(void) splx(x)
1106 # endif /* !USE_MUTEXES */
1107 
1108 # ifndef FREE_MB_T
1109 #  define	FREE_MB_T(m)	m_freem(m)
1110 # endif
1111 
1112 # ifndef MTOD
1113 #  define	MTOD(m,t)	mtod(m,t)
1114 # endif
1115 
1116 # ifndef COPYIN
1117 #  define	COPYIN(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
1118 #  define	COPYOUT(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
1119 #  define	BCOPYIN(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
1120 #  define	BCOPYOUT(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
1121 # endif
1122 
1123 # ifndef KMALLOC
1124 #  define	KMALLOC(a,b)	(a) = (b)new_kmem_alloc(sizeof(*(a)), \
1125 							KMEM_NOSLEEP)
1126 #  define	KMALLOCS(a,b,c)	(a) = (b)new_kmem_alloc((c), KMEM_NOSLEEP)
1127 # endif
1128 
1129 # ifndef	GET_MINOR
1130 #  define	GET_MINOR(x)	minor(x)
1131 # endif
1132 # define	PANIC(x,y)	/* if (x) panic y */
1133 #else /* _KERNEL */
1134 # define	SLEEP(x,y)	1;
1135 # define	WAKEUP(x)	;
1136 # define	IPF_PANIC(x,y)	;
1137 # define	PANIC(x,y)	;
1138 # define	SPL_NET(x)	;
1139 # define	SPL_IMP(x)	;
1140 # define	SPL_X(x)	;
1141 # define	KMALLOC(a,b)	(a) = (b)malloc(sizeof(*a))
1142 # define	KMALLOCS(a,b,c)	(a) = (b)malloc(c)
1143 # define	KFREE(x)	free(x)
1144 # define	KFREES(x,s)	free(x)
1145 # define	GETIFP(x, v)	get_unit(x,v)
1146 # define	COPYIN(a,b,c)	(bcopy((a), (b), (c)), 0)
1147 # define	COPYOUT(a,b,c)	(bcopy((a), (b), (c)), 0)
1148 # define	BCOPYIN(a,b,c)	(bcopy((a), (b), (c)), 0)
1149 # define	BCOPYOUT(a,b,c)	(bcopy((a), (b), (c)), 0)
1150 # define	UIOMOVE(a,b,c,d)	ipfuiomove(a,b,c,d)
1151 extern	void	m_copydata __P((mb_t *, int, int, caddr_t));
1152 extern	int	ipfuiomove __P((caddr_t, int, int, struct uio *));
1153 #endif /* _KERNEL */
1154 
1155 #ifndef	IFNAME
1156 # define	IFNAME(x)	((struct ifnet *)x)->if_name
1157 #endif
1158 #ifndef	COPYIFNAME
1159 # define	NEED_FRGETIFNAME
1160 extern	char	*fr_getifname __P((struct ifnet *, char *));
1161 # define	COPYIFNAME(x, b) \
1162 				fr_getifname((struct ifnet *)x, b)
1163 #endif
1164 
1165 
1166 #if !defined(USE_MUTEXES)
1167 # define	USE_SPL			1
1168 # undef		RW_DESTROY
1169 # undef		MUTEX_INIT
1170 # undef		MUTEX_NUKE
1171 # undef		MUTEX_DESTROY
1172 # define	MUTEX_ENTER(x)		;
1173 # define	READ_ENTER(x)		;
1174 # define	WRITE_ENTER(x)		;
1175 # define	MUTEX_DOWNGRADE(x)	;
1176 # define	RWLOCK_INIT(x, y)	;
1177 # define	RWLOCK_EXIT(x)		;
1178 # define	RW_DESTROY(x)		;
1179 # define	MUTEX_EXIT(x)		;
1180 # define	MUTEX_INIT(x,y)		;
1181 # define	MUTEX_DESTROY(x)	;
1182 # define	MUTEX_NUKE(x)		;
1183 #endif /* !USE_MUTEXES */
1184 #ifndef	ATOMIC_INC
1185 # define	ATOMIC_INC(x)		(x)++
1186 # define	ATOMIC_DEC(x)		(x)--
1187 #endif
1188 
1189 /*
1190  * Build some macros and #defines to enable the same code to compile anywhere
1191  * Well, that's the idea, anyway :-)
1192  */
1193 #ifndef	ATOMIC_INCL
1194 # define	ATOMIC_INCL		ATOMIC_INC
1195 # define	ATOMIC_INC64		ATOMIC_INC
1196 # define	ATOMIC_INC32		ATOMIC_INC
1197 # define	ATOMIC_INC16		ATOMIC_INC
1198 # define	ATOMIC_DECL		ATOMIC_DEC
1199 # define	ATOMIC_DEC64		ATOMIC_DEC
1200 # define	ATOMIC_DEC32		ATOMIC_DEC
1201 # define	ATOMIC_DEC16		ATOMIC_DEC
1202 #endif
1203 
1204 typedef	struct	tcphdr	tcphdr_t;
1205 typedef	struct	udphdr	udphdr_t;
1206 typedef	struct	icmp	icmphdr_t;
1207 typedef	struct	ip	ip_t;
1208 typedef	struct	ether_header	ether_header_t;
1209 typedef	struct	tcpiphdr	tcpiphdr_t;
1210 
1211 #ifndef	FR_GROUPLEN
1212 # define	FR_GROUPLEN	16
1213 #endif
1214 
1215 #ifdef offsetof
1216 # undef	offsetof
1217 #endif
1218 #ifndef offsetof
1219 # define offsetof(t,m) (int)((&((t *)0L)->m))
1220 #endif
1221 
1222 #ifndef IP_V
1223 # define	IP_V(x)		(x)->ip_v
1224 #endif
1225 #ifndef	IP_V_A
1226 # define	IP_V_A(x,y)	(x)->ip_v = (y)
1227 #endif
1228 #ifndef	IP_HL
1229 # define	IP_HL(x)	(x)->ip_hl
1230 #endif
1231 #ifndef	IP_HL_A
1232 # define	IP_HL_A(x,y)	(x)->ip_hl = (y)
1233 #endif
1234 #ifndef	TCP_OFF
1235 # define	TCP_OFF(x)	(x)->th_off
1236 #endif
1237 #ifndef	TCP_OFF_A
1238 # define	TCP_OFF_A(x,y)	(x)->th_off = (y)
1239 #endif
1240 #define	IPMINLEN(i, h)	((i)->ip_len >= (IP_HL(i) * 4 + sizeof(struct h)))
1241 
1242 
1243 /*
1244  * XXX - This is one of those *awful* hacks which nobody likes
1245  */
1246 #ifdef	ultrix
1247 #define	A_A
1248 #else
1249 #define	A_A	&
1250 #endif
1251 
1252 #define	TCPF_ALL	(TH_FIN|TH_SYN|TH_RST|TH_PUSH|TH_ACK|TH_URG|\
1253 			 TH_ECN|TH_CWR)
1254 
1255 #if (BSD >= 199306) && !defined(m_act)
1256 # define	m_act	m_nextpkt
1257 #endif
1258 
1259 /*
1260  * Security Options for Intenet Protocol (IPSO) as defined in RFC 1108.
1261  *
1262  * Basic Option
1263  *
1264  * 00000001   -   (Reserved 4)
1265  * 00111101   -   Top Secret
1266  * 01011010   -   Secret
1267  * 10010110   -   Confidential
1268  * 01100110   -   (Reserved 3)
1269  * 11001100   -   (Reserved 2)
1270  * 10101011   -   Unclassified
1271  * 11110001   -   (Reserved 1)
1272  */
1273 #define	IPSO_CLASS_RES4		0x01
1274 #define	IPSO_CLASS_TOPS		0x3d
1275 #define	IPSO_CLASS_SECR		0x5a
1276 #define	IPSO_CLASS_CONF		0x96
1277 #define	IPSO_CLASS_RES3		0x66
1278 #define	IPSO_CLASS_RES2		0xcc
1279 #define	IPSO_CLASS_UNCL		0xab
1280 #define	IPSO_CLASS_RES1		0xf1
1281 
1282 #define	IPSO_AUTH_GENSER	0x80
1283 #define	IPSO_AUTH_ESI		0x40
1284 #define	IPSO_AUTH_SCI		0x20
1285 #define	IPSO_AUTH_NSA		0x10
1286 #define	IPSO_AUTH_DOE		0x08
1287 #define	IPSO_AUTH_UN		0x06
1288 #define	IPSO_AUTH_FTE		0x01
1289 
1290 /*
1291  * IP option #defines
1292  */
1293 #undef	IPOPT_RR
1294 #define	IPOPT_RR	7
1295 #define	IPOPT_ZSU	10	/* ZSU */
1296 #define	IPOPT_MTUP	11	/* MTUP */
1297 #define	IPOPT_MTUR	12	/* MTUR */
1298 #define	IPOPT_ENCODE	15	/* ENCODE */
1299 #undef	IPOPT_TS
1300 #define	IPOPT_TS	68
1301 #define	IPOPT_TR	82	/* TR */
1302 #undef	IPOPT_SECURITY
1303 #define	IPOPT_SECURITY	130
1304 #undef	IPOPT_LSRR
1305 #define	IPOPT_LSRR	131
1306 #define	IPOPT_E_SEC	133	/* E-SEC */
1307 #define	IPOPT_CIPSO	134	/* CIPSO */
1308 #undef	IPOPT_SATID
1309 #define	IPOPT_SATID	136
1310 #ifndef	IPOPT_SID
1311 # define	IPOPT_SID	IPOPT_SATID
1312 #endif
1313 #undef	IPOPT_SSRR
1314 #define	IPOPT_SSRR	137
1315 #define	IPOPT_ADDEXT	147	/* ADDEXT */
1316 #define	IPOPT_VISA	142	/* VISA */
1317 #define	IPOPT_IMITD	144	/* IMITD */
1318 #define	IPOPT_EIP	145	/* EIP */
1319 #define	IPOPT_RTRALRT	148	/* RTRALRT */
1320 #define	IPOPT_SDB	149
1321 #define	IPOPT_NSAPA	150
1322 #define	IPOPT_DPS	151
1323 #define	IPOPT_UMP	152
1324 #define	IPOPT_FINN	205	/* FINN */
1325 
1326 #ifndef TCPOPT_EOL
1327 # define TCPOPT_EOL		0
1328 #endif
1329 #ifndef TCPOPT_NOP
1330 # define TCPOPT_NOP		1
1331 #endif
1332 #ifndef TCPOPT_MAXSEG
1333 # define TCPOPT_MAXSEG		2
1334 #endif
1335 #ifndef TCPOLEN_MAXSEG
1336 # define TCPOLEN_MAXSEG		4
1337 #endif
1338 #ifndef TCPOPT_WINDOW
1339 # define TCPOPT_WINDOW		3
1340 #endif
1341 #ifndef TCPOLEN_WINDOW
1342 # define TCPOLEN_WINDOW		3
1343 #endif
1344 #ifndef TCPOPT_SACK_PERMITTED
1345 # define TCPOPT_SACK_PERMITTED	4
1346 #endif
1347 #ifndef TCPOLEN_SACK_PERMITTED
1348 # define TCPOLEN_SACK_PERMITTED	2
1349 #endif
1350 #ifndef TCPOPT_SACK
1351 # define TCPOPT_SACK		5
1352 #endif
1353 #ifndef TCPOPT_TIMESTAMP
1354 # define TCPOPT_TIMESTAMP	8
1355 #endif
1356 
1357 #ifndef	ICMP_MINLEN
1358 # define	ICMP_MINLEN	8
1359 #endif
1360 #ifndef	ICMP_ECHOREPLY
1361 # define	ICMP_ECHOREPLY	0
1362 #endif
1363 #ifndef	ICMP_UNREACH
1364 # define	ICMP_UNREACH	3
1365 #endif
1366 #ifndef	ICMP_UNREACH_NET
1367 # define	ICMP_UNREACH_NET	0
1368 #endif
1369 #ifndef	ICMP_UNREACH_HOST
1370 # define	ICMP_UNREACH_HOST	1
1371 #endif
1372 #ifndef	ICMP_UNREACH_PROTOCOL
1373 # define	ICMP_UNREACH_PROTOCOL	2
1374 #endif
1375 #ifndef	ICMP_UNREACH_PORT
1376 # define	ICMP_UNREACH_PORT	3
1377 #endif
1378 #ifndef	ICMP_UNREACH_NEEDFRAG
1379 # define	ICMP_UNREACH_NEEDFRAG	4
1380 #endif
1381 #ifndef	ICMP_UNREACH_SRCFAIL
1382 # define	ICMP_UNREACH_SRCFAIL	5
1383 #endif
1384 #ifndef	ICMP_UNREACH_NET_UNKNOWN
1385 # define	ICMP_UNREACH_NET_UNKNOWN	6
1386 #endif
1387 #ifndef	ICMP_UNREACH_HOST_UNKNOWN
1388 # define	ICMP_UNREACH_HOST_UNKNOWN	7
1389 #endif
1390 #ifndef	ICMP_UNREACH_ISOLATED
1391 # define	ICMP_UNREACH_ISOLATED	8
1392 #endif
1393 #ifndef	ICMP_UNREACH_NET_PROHIB
1394 # define	ICMP_UNREACH_NET_PROHIB	9
1395 #endif
1396 #ifndef	ICMP_UNREACH_HOST_PROHIB
1397 # define	ICMP_UNREACH_HOST_PROHIB	10
1398 #endif
1399 #ifndef	ICMP_UNREACH_TOSNET
1400 # define	ICMP_UNREACH_TOSNET	11
1401 #endif
1402 #ifndef	ICMP_UNREACH_TOSHOST
1403 # define	ICMP_UNREACH_TOSHOST	12
1404 #endif
1405 #ifndef	ICMP_UNREACH_ADMIN_PROHIBIT
1406 # define	ICMP_UNREACH_ADMIN_PROHIBIT	13
1407 #endif
1408 #ifndef	ICMP_UNREACH_FILTER
1409 # define	ICMP_UNREACH_FILTER	13
1410 #endif
1411 #ifndef	ICMP_UNREACH_HOST_PRECEDENCE
1412 # define	ICMP_UNREACH_HOST_PRECEDENCE	14
1413 #endif
1414 #ifndef	ICMP_UNREACH_PRECEDENCE_CUTOFF
1415 # define	ICMP_UNREACH_PRECEDENCE_CUTOFF	15
1416 #endif
1417 #ifndef	ICMP_SOURCEQUENCH
1418 # define	ICMP_SOURCEQUENCH	4
1419 #endif
1420 #ifndef	ICMP_REDIRECT_NET
1421 # define	ICMP_REDIRECT_NET	0
1422 #endif
1423 #ifndef	ICMP_REDIRECT_HOST
1424 # define	ICMP_REDIRECT_HOST	1
1425 #endif
1426 #ifndef	ICMP_REDIRECT_TOSNET
1427 # define	ICMP_REDIRECT_TOSNET	2
1428 #endif
1429 #ifndef	ICMP_REDIRECT_TOSHOST
1430 # define	ICMP_REDIRECT_TOSHOST	3
1431 #endif
1432 #ifndef	ICMP_ALTHOSTADDR
1433 # define	ICMP_ALTHOSTADDR	6
1434 #endif
1435 #ifndef	ICMP_TIMXCEED
1436 # define	ICMP_TIMXCEED	11
1437 #endif
1438 #ifndef	ICMP_TIMXCEED_INTRANS
1439 # define	ICMP_TIMXCEED_INTRANS	0
1440 #endif
1441 #ifndef	ICMP_TIMXCEED_REASS
1442 # define		ICMP_TIMXCEED_REASS	1
1443 #endif
1444 #ifndef	ICMP_PARAMPROB
1445 # define	ICMP_PARAMPROB	12
1446 #endif
1447 #ifndef	ICMP_PARAMPROB_ERRATPTR
1448 # define	ICMP_PARAMPROB_ERRATPTR	0
1449 #endif
1450 #ifndef	ICMP_PARAMPROB_OPTABSENT
1451 # define	ICMP_PARAMPROB_OPTABSENT	1
1452 #endif
1453 #ifndef	ICMP_PARAMPROB_LENGTH
1454 # define	ICMP_PARAMPROB_LENGTH	2
1455 #endif
1456 #ifndef ICMP_TSTAMP
1457 # define	ICMP_TSTAMP	13
1458 #endif
1459 #ifndef ICMP_TSTAMPREPLY
1460 # define	ICMP_TSTAMPREPLY	14
1461 #endif
1462 #ifndef ICMP_IREQ
1463 # define	ICMP_IREQ	15
1464 #endif
1465 #ifndef ICMP_IREQREPLY
1466 # define	ICMP_IREQREPLY	16
1467 #endif
1468 #ifndef	ICMP_MASKREQ
1469 # define	ICMP_MASKREQ	17
1470 #endif
1471 #ifndef ICMP_MASKREPLY
1472 # define	ICMP_MASKREPLY	18
1473 #endif
1474 #ifndef	ICMP_TRACEROUTE
1475 # define	ICMP_TRACEROUTE	30
1476 #endif
1477 #ifndef	ICMP_DATACONVERR
1478 # define	ICMP_DATACONVERR	31
1479 #endif
1480 #ifndef	ICMP_MOBILE_REDIRECT
1481 # define	ICMP_MOBILE_REDIRECT	32
1482 #endif
1483 #ifndef	ICMP_IPV6_WHEREAREYOU
1484 # define	ICMP_IPV6_WHEREAREYOU	33
1485 #endif
1486 #ifndef	ICMP_IPV6_IAMHERE
1487 # define	ICMP_IPV6_IAMHERE	34
1488 #endif
1489 #ifndef	ICMP_MOBILE_REGREQUEST
1490 # define	ICMP_MOBILE_REGREQUEST	35
1491 #endif
1492 #ifndef	ICMP_MOBILE_REGREPLY
1493 # define	ICMP_MOBILE_REGREPLY	36
1494 #endif
1495 #ifndef	ICMP_SKIP
1496 # define	ICMP_SKIP	39
1497 #endif
1498 #ifndef	ICMP_PHOTURIS
1499 # define	ICMP_PHOTURIS	40
1500 #endif
1501 #ifndef	ICMP_PHOTURIS_UNKNOWN_INDEX
1502 # define	ICMP_PHOTURIS_UNKNOWN_INDEX	1
1503 #endif
1504 #ifndef	ICMP_PHOTURIS_AUTH_FAILED
1505 # define	ICMP_PHOTURIS_AUTH_FAILED	2
1506 #endif
1507 #ifndef	ICMP_PHOTURIS_DECRYPT_FAILED
1508 # define	ICMP_PHOTURIS_DECRYPT_FAILED	3
1509 #endif
1510 #ifndef	IPVERSION
1511 # define	IPVERSION	4
1512 #endif
1513 #ifndef	IPOPT_MINOFF
1514 # define	IPOPT_MINOFF	4
1515 #endif
1516 #ifndef	IPOPT_COPIED
1517 # define	IPOPT_COPIED(x)	((x)&0x80)
1518 #endif
1519 #ifndef	IPOPT_EOL
1520 # define	IPOPT_EOL	0
1521 #endif
1522 #ifndef	IPOPT_NOP
1523 # define	IPOPT_NOP	1
1524 #endif
1525 #ifndef	IP_MF
1526 # define	IP_MF	((u_short)0x2000)
1527 #endif
1528 #ifndef	ETHERTYPE_IP
1529 # define	ETHERTYPE_IP	((u_short)0x0800)
1530 #endif
1531 #ifndef	TH_FIN
1532 # define	TH_FIN	0x01
1533 #endif
1534 #ifndef	TH_SYN
1535 # define	TH_SYN	0x02
1536 #endif
1537 #ifndef	TH_RST
1538 # define	TH_RST	0x04
1539 #endif
1540 #ifndef	TH_PUSH
1541 # define	TH_PUSH	0x08
1542 #endif
1543 #ifndef	TH_ACK
1544 # define	TH_ACK	0x10
1545 #endif
1546 #ifndef	TH_URG
1547 # define	TH_URG	0x20
1548 #endif
1549 #undef	TH_ACKMASK
1550 #define	TH_ACKMASK	(TH_FIN|TH_SYN|TH_RST|TH_ACK)
1551 
1552 #ifndef	IPOPT_EOL
1553 # define	IPOPT_EOL	0
1554 #endif
1555 #ifndef	IPOPT_NOP
1556 # define	IPOPT_NOP	1
1557 #endif
1558 #ifndef	IPOPT_RR
1559 # define	IPOPT_RR	7
1560 #endif
1561 #ifndef	IPOPT_TS
1562 # define	IPOPT_TS	68
1563 #endif
1564 #ifndef	IPOPT_SECURITY
1565 # define	IPOPT_SECURITY	130
1566 #endif
1567 #ifndef	IPOPT_LSRR
1568 # define	IPOPT_LSRR	131
1569 #endif
1570 #ifndef	IPOPT_SATID
1571 # define	IPOPT_SATID	136
1572 #endif
1573 #ifndef	IPOPT_SSRR
1574 # define	IPOPT_SSRR	137
1575 #endif
1576 #ifndef	IPOPT_SECUR_UNCLASS
1577 # define	IPOPT_SECUR_UNCLASS	((u_short)0x0000)
1578 #endif
1579 #ifndef	IPOPT_SECUR_CONFID
1580 # define	IPOPT_SECUR_CONFID	((u_short)0xf135)
1581 #endif
1582 #ifndef	IPOPT_SECUR_EFTO
1583 # define	IPOPT_SECUR_EFTO	((u_short)0x789a)
1584 #endif
1585 #ifndef	IPOPT_SECUR_MMMM
1586 # define	IPOPT_SECUR_MMMM	((u_short)0xbc4d)
1587 #endif
1588 #ifndef	IPOPT_SECUR_RESTR
1589 # define	IPOPT_SECUR_RESTR	((u_short)0xaf13)
1590 #endif
1591 #ifndef	IPOPT_SECUR_SECRET
1592 # define	IPOPT_SECUR_SECRET	((u_short)0xd788)
1593 #endif
1594 #ifndef IPOPT_SECUR_TOPSECRET
1595 # define	IPOPT_SECUR_TOPSECRET	((u_short)0x6bc5)
1596 #endif
1597 #ifndef IPOPT_OLEN
1598 # define	IPOPT_OLEN	1
1599 #endif
1600 #ifndef	IPPROTO_HOPOPTS
1601 # define	IPPROTO_HOPOPTS	0
1602 #endif
1603 #ifndef	IPPROTO_ENCAP
1604 # define	IPPROTO_ENCAP	4
1605 #endif
1606 #ifndef	IPPROTO_IPV6
1607 # define	IPPROTO_IPV6	41
1608 #endif
1609 #ifndef	IPPROTO_ROUTING
1610 # define	IPPROTO_ROUTING	43
1611 #endif
1612 #ifndef	IPPROTO_FRAGMENT
1613 # define	IPPROTO_FRAGMENT	44
1614 #endif
1615 #ifndef	IPPROTO_GRE
1616 # define	IPPROTO_GRE	47	/* GRE encaps RFC 1701 */
1617 #endif
1618 #ifndef	IPPROTO_ESP
1619 # define	IPPROTO_ESP	50
1620 #endif
1621 #ifndef	IPPROTO_AH
1622 # define	IPPROTO_AH	51
1623 #endif
1624 #ifndef	IPPROTO_ICMPV6
1625 # define	IPPROTO_ICMPV6	58
1626 #endif
1627 #ifndef	IPPROTO_NONE
1628 # define	IPPROTO_NONE	59
1629 #endif
1630 #ifndef	IPPROTO_DSTOPTS
1631 # define	IPPROTO_DSTOPTS	60
1632 #endif
1633 #ifndef	IPPROTO_FRAGMENT
1634 # define	IPPROTO_FRAGMENT	44
1635 #endif
1636 #ifndef	ICMP_ROUTERADVERT
1637 # define	ICMP_ROUTERADVERT	9
1638 #endif
1639 #ifndef	ICMP_ROUTERSOLICIT
1640 # define	ICMP_ROUTERSOLICIT	10
1641 #endif
1642 #ifndef	ICMP6_DST_UNREACH
1643 # define	ICMP6_DST_UNREACH	1
1644 #endif
1645 #ifndef	ICMP6_PACKET_TOO_BIG
1646 # define	ICMP6_PACKET_TOO_BIG	2
1647 #endif
1648 #ifndef	ICMP6_TIME_EXCEEDED
1649 # define	ICMP6_TIME_EXCEEDED	3
1650 #endif
1651 #ifndef	ICMP6_PARAM_PROB
1652 # define	ICMP6_PARAM_PROB	4
1653 #endif
1654 
1655 #ifndef	ICMP6_ECHO_REQUEST
1656 # define	ICMP6_ECHO_REQUEST	128
1657 #endif
1658 #ifndef	ICMP6_ECHO_REPLY
1659 # define	ICMP6_ECHO_REPLY	129
1660 #endif
1661 #ifndef	ICMP6_MEMBERSHIP_QUERY
1662 # define	ICMP6_MEMBERSHIP_QUERY	130
1663 #endif
1664 #ifndef	MLD6_LISTENER_QUERY
1665 # define	MLD6_LISTENER_QUERY	130
1666 #endif
1667 #ifndef	ICMP6_MEMBERSHIP_REPORT
1668 # define	ICMP6_MEMBERSHIP_REPORT	131
1669 #endif
1670 #ifndef	MLD6_LISTENER_REPORT
1671 # define	MLD6_LISTENER_REPORT	131
1672 #endif
1673 #ifndef	ICMP6_MEMBERSHIP_REDUCTION
1674 # define	ICMP6_MEMBERSHIP_REDUCTION	132
1675 #endif
1676 #ifndef	MLD6_LISTENER_DONE
1677 # define	MLD6_LISTENER_DONE	132
1678 #endif
1679 #ifndef	ND_ROUTER_SOLICIT
1680 # define	ND_ROUTER_SOLICIT	133
1681 #endif
1682 #ifndef	ND_ROUTER_ADVERT
1683 # define	ND_ROUTER_ADVERT	134
1684 #endif
1685 #ifndef	ND_NEIGHBOR_SOLICIT
1686 # define	ND_NEIGHBOR_SOLICIT	135
1687 #endif
1688 #ifndef	ND_NEIGHBOR_ADVERT
1689 # define	ND_NEIGHBOR_ADVERT	136
1690 #endif
1691 #ifndef	ND_REDIRECT
1692 # define	ND_REDIRECT	137
1693 #endif
1694 #ifndef	ICMP6_ROUTER_RENUMBERING
1695 # define	ICMP6_ROUTER_RENUMBERING	138
1696 #endif
1697 #ifndef	ICMP6_WRUREQUEST
1698 # define	ICMP6_WRUREQUEST	139
1699 #endif
1700 #ifndef	ICMP6_WRUREPLY
1701 # define	ICMP6_WRUREPLY		140
1702 #endif
1703 #ifndef	ICMP6_FQDN_QUERY
1704 # define	ICMP6_FQDN_QUERY	139
1705 #endif
1706 #ifndef	ICMP6_FQDN_REPLY
1707 # define	ICMP6_FQDN_REPLY	140
1708 #endif
1709 #ifndef	ICMP6_NI_QUERY
1710 # define	ICMP6_NI_QUERY		139
1711 #endif
1712 #ifndef	ICMP6_NI_REPLY
1713 # define	ICMP6_NI_REPLY		140
1714 #endif
1715 #ifndef	MLD6_MTRACE_RESP
1716 # define	MLD6_MTRACE_RESP	200
1717 #endif
1718 #ifndef	MLD6_MTRACE
1719 # define	MLD6_MTRACE		201
1720 #endif
1721 #ifndef	ICMP6_HADISCOV_REQUEST
1722 # define	ICMP6_HADISCOV_REQUEST	202
1723 #endif
1724 #ifndef	ICMP6_HADISCOV_REPLY
1725 # define	ICMP6_HADISCOV_REPLY	203
1726 #endif
1727 #ifndef	ICMP6_MOBILEPREFIX_SOLICIT
1728 # define	ICMP6_MOBILEPREFIX_SOLICIT	204
1729 #endif
1730 #ifndef	ICMP6_MOBILEPREFIX_ADVERT
1731 # define	ICMP6_MOBILEPREFIX_ADVERT	205
1732 #endif
1733 #ifndef	ICMP6_MAXTYPE
1734 # define	ICMP6_MAXTYPE		205
1735 #endif
1736 
1737 #ifndef	ICMP6_DST_UNREACH_NOROUTE
1738 # define	ICMP6_DST_UNREACH_NOROUTE	0
1739 #endif
1740 #ifndef	ICMP6_DST_UNREACH_ADMIN
1741 # define	ICMP6_DST_UNREACH_ADMIN		1
1742 #endif
1743 #ifndef	ICMP6_DST_UNREACH_NOTNEIGHBOR
1744 # define	ICMP6_DST_UNREACH_NOTNEIGHBOR	2
1745 #endif
1746 #ifndef	ICMP6_DST_UNREACH_BEYONDSCOPE
1747 # define	ICMP6_DST_UNREACH_BEYONDSCOPE	2
1748 #endif
1749 #ifndef	ICMP6_DST_UNREACH_ADDR
1750 # define	ICMP6_DST_UNREACH_ADDR		3
1751 #endif
1752 #ifndef	ICMP6_DST_UNREACH_NOPORT
1753 # define	ICMP6_DST_UNREACH_NOPORT	4
1754 #endif
1755 #ifndef	ICMP6_TIME_EXCEED_TRANSIT
1756 # define	ICMP6_TIME_EXCEED_TRANSIT	0
1757 #endif
1758 #ifndef	ICMP6_TIME_EXCEED_REASSEMBLY
1759 # define	ICMP6_TIME_EXCEED_REASSEMBLY	1
1760 #endif
1761 
1762 #ifndef	ICMP6_NI_SUCCESS
1763 # define	ICMP6_NI_SUCCESS	0
1764 #endif
1765 #ifndef	ICMP6_NI_REFUSED
1766 # define	ICMP6_NI_REFUSED	1
1767 #endif
1768 #ifndef	ICMP6_NI_UNKNOWN
1769 # define	ICMP6_NI_UNKNOWN	2
1770 #endif
1771 
1772 #ifndef	ICMP6_ROUTER_RENUMBERING_COMMAND
1773 # define	ICMP6_ROUTER_RENUMBERING_COMMAND	0
1774 #endif
1775 #ifndef	ICMP6_ROUTER_RENUMBERING_RESULT
1776 # define	ICMP6_ROUTER_RENUMBERING_RESULT	1
1777 #endif
1778 #ifndef	ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET
1779 # define	ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET	255
1780 #endif
1781 
1782 #ifndef	ICMP6_PARAMPROB_HEADER
1783 # define	ICMP6_PARAMPROB_HEADER	0
1784 #endif
1785 #ifndef	ICMP6_PARAMPROB_NEXTHEADER
1786 # define	ICMP6_PARAMPROB_NEXTHEADER	1
1787 #endif
1788 #ifndef	ICMP6_PARAMPROB_OPTION
1789 # define	ICMP6_PARAMPROB_OPTION	2
1790 #endif
1791 
1792 #ifndef	ICMP6_NI_SUBJ_IPV6
1793 # define	ICMP6_NI_SUBJ_IPV6	0
1794 #endif
1795 #ifndef	ICMP6_NI_SUBJ_FQDN
1796 # define	ICMP6_NI_SUBJ_FQDN	1
1797 #endif
1798 #ifndef	ICMP6_NI_SUBJ_IPV4
1799 # define	ICMP6_NI_SUBJ_IPV4	2
1800 #endif
1801 
1802 /*
1803  * ECN is a new addition to TCP - RFC 2481
1804  */
1805 #ifndef TH_ECN
1806 # define	TH_ECN	0x40
1807 #endif
1808 #ifndef TH_CWR
1809 # define	TH_CWR	0x80
1810 #endif
1811 #define	TH_ECNALL	(TH_ECN|TH_CWR)
1812 
1813 /*
1814  * TCP States
1815  */
1816 #define IPF_TCPS_CLOSED		0	/* closed */
1817 #define IPF_TCPS_LISTEN		1	/* listening for connection */
1818 #define IPF_TCPS_SYN_SENT	2	/* active, have sent syn */
1819 #define IPF_TCPS_SYN_RECEIVED	3	/* have send and received syn */
1820 #define IPF_TCPS_HALF_ESTAB	4	/* for connections not fully "up" */
1821 /* states < IPF_TCPS_ESTABLISHED are those where connections not established */
1822 #define IPF_TCPS_ESTABLISHED	5	/* established */
1823 #define IPF_TCPS_CLOSE_WAIT	6	/* rcvd fin, waiting for close */
1824 /* states > IPF_TCPS_CLOSE_WAIT are those where user has closed */
1825 #define IPF_TCPS_FIN_WAIT_1	7	/* have closed, sent fin */
1826 #define IPF_TCPS_CLOSING	8	/* closed xchd FIN; await FIN ACK */
1827 #define IPF_TCPS_LAST_ACK	9	/* had fin and close; await FIN ACK */
1828 /* states > IPF_TCPS_CLOSE_WAIT && < IPF_TCPS_FIN_WAIT_2 await ACK of FIN */
1829 #define IPF_TCPS_FIN_WAIT_2	10	/* have closed, fin is acked */
1830 #define IPF_TCPS_TIME_WAIT	11	/* in 2*msl quiet wait after close */
1831 #define IPF_TCP_NSTATES		12
1832 
1833 #define	TCP_MSL			120
1834 
1835 #undef	ICMP_MAX_UNREACH
1836 #define	ICMP_MAX_UNREACH	14
1837 #undef	ICMP_MAXTYPE
1838 #define	ICMP_MAXTYPE		18
1839 
1840 #ifndef	IFNAMSIZ
1841 #define	IFNAMSIZ		16
1842 #endif
1843 
1844 #ifndef	LOG_FTP
1845 # define	LOG_FTP		(11<<3)
1846 #endif
1847 #ifndef	LOG_AUTHPRIV
1848 # define	LOG_AUTHPRIV	(10<<3)
1849 #endif
1850 #ifndef	LOG_AUDIT
1851 # define	LOG_AUDIT	(13<<3)
1852 #endif
1853 #ifndef	LOG_NTP
1854 # define	LOG_NTP		(12<<3)
1855 #endif
1856 #ifndef	LOG_SECURITY
1857 # define	LOG_SECURITY	(13<<3)
1858 #endif
1859 #ifndef	LOG_LFMT
1860 # define	LOG_LFMT	(14<<3)
1861 #endif
1862 #ifndef	LOG_CONSOLE
1863 # define	LOG_CONSOLE	(14<<3)
1864 #endif
1865 
1866 /*
1867  * ICMP error replies have an IP header (20 bytes), 8 bytes of ICMP data,
1868  * another IP header and then 64 bits of data, totalling 56.  Of course,
1869  * the last 64 bits is dependant on that being available.
1870  */
1871 #define	ICMPERR_ICMPHLEN	8
1872 #define	ICMPERR_IPICMPHLEN	(20 + 8)
1873 #define	ICMPERR_MINPKTLEN	(20 + 8 + 20)
1874 #define	ICMPERR_MAXPKTLEN	(20 + 8 + 20 + 8)
1875 #define ICMP6ERR_MINPKTLEN	(40 + 8)
1876 #define ICMP6ERR_IPICMPHLEN	(40 + 8 + 40)
1877 
1878 #ifdef  __GNUC__
1879 # define	INLINE	__inline__
1880 #else
1881 # ifndef	INLINE
1882 #  define	INLINE
1883 # endif
1884 #endif
1885 
1886 #ifndef MIN
1887 # define	MIN(a,b)	(((a)<(b))?(a):(b))
1888 #endif
1889 
1890 #endif	/* __IP_COMPAT_H__ */
1891