1 /*
2  * Copyright (C) 1993-2001, 2003 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * @(#)ip_compat.h	1.8 1/14/96
7  * $Id: ip_compat.h,v 2.142.2.30 2005/08/11 15:13:49 darrenr Exp $
8  *
9  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
10  * Use is subject to license terms.
11  */
12 
13 #pragma ident	"%Z%%M%	%I%	%E% SMI"
14 
15 
16 #ifndef	__IP_COMPAT_H__
17 #define	__IP_COMPAT_H__
18 
19 #ifndef	__P
20 # ifdef	__STDC__
21 #  define	__P(x)  x
22 # else
23 #  define	__P(x)  ()
24 # endif
25 #endif
26 #ifndef	__STDC__
27 # undef		const
28 # define	const
29 #endif
30 
31 #if defined(_KERNEL) || defined(KERNEL) || defined(__KERNEL__)
32 # undef	KERNEL
33 # undef	_KERNEL
34 # undef 	__KERNEL__
35 # define	KERNEL
36 # define	_KERNEL
37 # define 	__KERNEL__
38 #endif
39 
40 #ifndef	SOLARIS
41 #define	SOLARIS	(defined(sun) && (defined(__svr4__) || defined(__SVR4)))
42 #endif
43 #if SOLARIS2 >= 8
44 # ifndef	USE_INET6
45 #  define	USE_INET6
46 # endif
47 #endif
48 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 400000) && \
49     !defined(_KERNEL) && !defined(USE_INET6) && !defined(NOINET6)
50 # define	USE_INET6
51 #endif
52 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105000000) && \
53     !defined(_KERNEL) && !defined(USE_INET6)
54 # define	USE_INET6
55 # define	IPFILTER_M_IPFILTER
56 #endif
57 #if defined(OpenBSD) && (OpenBSD >= 200206) && \
58     !defined(_KERNEL) && !defined(USE_INET6)
59 # define	USE_INET6
60 #endif
61 #if defined(__osf__)
62 # define	USE_INET6
63 #endif
64 #if defined(linux) && (!defined(_KERNEL) || defined(CONFIG_IPV6))
65 # define	USE_INET6
66 #endif
67 #if defined(HPUXREV) && (HPUXREV >= 1111)
68 # define	USE_INET6
69 #endif
70 
71 #if defined(BSD) && (BSD < 199103) && defined(__osf__)
72 # undef BSD
73 # define BSD 199103
74 #endif
75 
76 #if defined(__SVR4) || defined(__svr4__) || defined(__sgi)
77 # define index   strchr
78 # if !defined(_KERNEL)
79 #  define	bzero(a,b)	memset(a,0,b)
80 #  define	bcmp		memcmp
81 #  define	bcopy(a,b,c)	memmove(b,a,c)
82 # endif
83 #endif
84 
85 #ifndef LIFNAMSIZ
86 # ifdef IF_NAMESIZE
87 #  define	LIFNAMSIZ	IF_NAMESIZE
88 # else
89 #  ifdef	IFNAMSIZ
90 #   define	LIFNAMSIZ	IFNAMSIZ
91 #  else
92 #   define	LIFNAMSIZ	16
93 #  endif
94 # endif
95 #endif
96 
97 #if defined(__sgi) || defined(bsdi) || defined(__hpux) || defined(hpux)
98 struct  ether_addr {
99         u_char  ether_addr_octet[6];
100 };
101 #endif
102 
103 #if defined(__sgi) && !defined(IPFILTER_LKM)
104 # ifdef __STDC__
105 #  define IPL_EXTERN(ep) ipfilter##ep
106 # else
107 #  define IPL_EXTERN(ep) ipfilter/**/ep
108 # endif
109 #else
110 # ifdef __STDC__
111 #  define IPL_EXTERN(ep) ipl##ep
112 # else
113 #  define IPL_EXTERN(ep) ipl/**/ep
114 # endif
115 #endif
116 
117 /*
118  * This is a workaround for <sys/uio.h> troubles on FreeBSD and OpenBSD.
119  */
120 #ifndef linux
121 # ifndef _KERNEL
122 #  define ADD_KERNEL
123 #  define _KERNEL
124 #  define KERNEL
125 # endif
126 # ifdef __OpenBSD__
127 struct file;
128 # endif
129 # include <sys/uio.h>
130 # ifdef ADD_KERNEL
131 #  undef _KERNEL
132 #  undef KERNEL
133 # endif
134 #endif
135 
136 
137 /* ----------------------------------------------------------------------- */
138 /*                                  S O L A R I S                          */
139 /* ----------------------------------------------------------------------- */
140 #if SOLARIS
141 # define	MENTAT	1
142 # include	<sys/cmn_err.h>
143 # include	<sys/isa_defs.h>
144 # include	<sys/stream.h>
145 # include	<sys/ioccom.h>
146 # include	<sys/sysmacros.h>
147 # include	<sys/kmem.h>
148 # if SOLARIS2 >= 10
149 #  include	<sys/procset.h>
150 #  include	<sys/proc.h>
151 #  include	<sys/devops.h>
152 #  include	<sys/ddi_impldefs.h>
153 # endif
154 /*
155  * because Solaris 2 defines these in two places :-/
156  */
157 # ifndef	KERNEL
158 #  define	_KERNEL
159 #  undef	RES_INIT
160 # endif /* _KERNEL */
161 
162 # if SOLARIS2 >= 8
163 #  include <netinet/ip6.h>
164 #  include <netinet/icmp6.h>
165 # endif
166 
167 # include <inet/common.h>
168 /* These 5 are defined in <inet/ip.h> and <netinet/ip.h> */
169 # undef	IPOPT_EOL
170 # undef	IPOPT_NOP
171 # undef	IPOPT_LSRR
172 # undef	IPOPT_RR
173 # undef	IPOPT_SSRR
174 # ifdef i386
175 #  define _SYS_PROMIF_H
176 # endif
177 # include <inet/ip.h>
178 # undef COPYOUT
179 # include <inet/ip_ire.h>
180 # ifndef	KERNEL
181 #  undef	_KERNEL
182 # endif
183 # if SOLARIS2 >= 8
184 #  define SNPRINTF	snprintf
185 
186 #  include <inet/ip_if.h>
187 #  define	ipif_local_addr	ipif_lcl_addr
188 /* Only defined in private include file */
189 #  ifndef	V4_PART_OF_V6
190 #   define	V4_PART_OF_V6(v6)	v6.s6_addr32[3]
191 #  endif
192 struct ip6_ext {
193 	u_char	ip6e_nxt;
194 	u_char	ip6e_len;
195 };
196 # endif /* SOLARIS2 >= 8 */
197 
198 # if SOLARIS2 >= 6
199 #  include <sys/atomic.h>
200 typedef	uint32_t	u_32_t;
201 # else
202 typedef unsigned int	u_32_t;
203 # endif
204 # define	U_32_T	1
205 
206 # ifdef _KERNEL
207 #  define	KRWLOCK_T		krwlock_t
208 #  define	KMUTEX_T		kmutex_t
209 #  include "qif.h"
210 #  include "pfil.h"
211 #  if SOLARIS2 >= 6
212 #   if SOLARIS2 == 6
213 #    define	ATOMIC_INCL(x)		atomic_add_long((uint32_t*)&(x), 1)
214 #    define	ATOMIC_DECL(x)		atomic_add_long((uint32_t*)&(x), -1)
215 #   else
216 #    define	ATOMIC_INCL(x)		atomic_add_long(&(x), 1)
217 #    define	ATOMIC_DECL(x)		atomic_add_long(&(x), -1)
218 #   endif /* SOLARIS2 == 6 */
219 #   define	ATOMIC_INC64(x)		atomic_add_64((uint64_t*)&(x), 1)
220 #   define	ATOMIC_INC32(x)		atomic_add_32((uint32_t*)&(x), 1)
221 #   define	ATOMIC_INC16(x)		atomic_add_16((uint16_t*)&(x), 1)
222 #   define	ATOMIC_DEC64(x)		atomic_add_64((uint64_t*)&(x), -1)
223 #   define	ATOMIC_DEC32(x)		atomic_add_32((uint32_t*)&(x), -1)
224 #   define	ATOMIC_DEC16(x)		atomic_add_16((uint16_t*)&(x), -1)
225 #  else
226 #   define	ATOMIC_INC(x)		{ mutex_enter(&ipf_rw); (x)++; \
227 					  mutex_exit(&ipf_rw); }
228 #   define	ATOMIC_DEC(x)		{ mutex_enter(&ipf_rw); (x)--; \
229 					  mutex_exit(&ipf_rw); }
230 #  endif /* SOLARIS2 >= 6 */
231 #  define	USE_MUTEXES
232 #  define	MUTEX_ENTER(x)		mutex_enter(&(x)->ipf_lk)
233 #  define	READ_ENTER(x)		rw_enter(&(x)->ipf_lk, RW_READER)
234 #  define	WRITE_ENTER(x)		rw_enter(&(x)->ipf_lk, RW_WRITER)
235 #  define	MUTEX_DOWNGRADE(x)	rw_downgrade(&(x)->ipf_lk)
236 #  define	RWLOCK_INIT(x, y)	rw_init(&(x)->ipf_lk, (y),  \
237 						RW_DRIVER, NULL)
238 #  define	RWLOCK_EXIT(x)		rw_exit(&(x)->ipf_lk)
239 #  define	RW_DESTROY(x)		rw_destroy(&(x)->ipf_lk)
240 #  define	MUTEX_INIT(x, y)	mutex_init(&(x)->ipf_lk, (y), \
241 						   MUTEX_DRIVER, NULL)
242 #  define	MUTEX_DESTROY(x)	mutex_destroy(&(x)->ipf_lk)
243 #  define	MUTEX_NUKE(x)		bzero((x), sizeof(*(x)))
244 #  define	MUTEX_EXIT(x)		mutex_exit(&(x)->ipf_lk)
245 #  define	COPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
246 #  define	COPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
247 #  define	BCOPYIN(a,b,c)	(void) copyin((caddr_t)(a), (caddr_t)(b), (c))
248 #  define	BCOPYOUT(a,b,c)	(void) copyout((caddr_t)(a), (caddr_t)(b), (c))
249 #  define	UIOMOVE(a,b,c,d)	uiomove((caddr_t)a,b,c,d)
250 #  define	KFREE(x)	kmem_free((char *)(x), sizeof(*(x)))
251 #  define	KFREES(x,s)	kmem_free((char *)(x), (s))
252 #  define	SPL_NET(x)	;
253 #  define	SPL_IMP(x)	;
254 #  undef	SPL_X
255 #  define	SPL_X(x)	;
256 #  ifdef sparc
257 #   define	ntohs(x)	(x)
258 #   define	ntohl(x)	(x)
259 #   define	htons(x)	(x)
260 #   define	htonl(x)	(x)
261 #  endif /* sparc */
262 #  define	KMALLOC(a,b)	(a) = (b)kmem_alloc(sizeof(*(a)), KM_NOSLEEP)
263 #  define	KMALLOCS(a,b,c)	(a) = (b)kmem_alloc((c), KM_NOSLEEP)
264 #  define	GET_MINOR(x)	getminor(x)
265 extern	void	*get_unit __P((char *, int));
266 #  define	GETIFP(n, v)	get_unit(n, v)
267 #  define	IFNAME(x)	((qif_t *)x)->qf_name
268 #  define	COPYIFNAME(x, b) \
269 				(void) strncpy(b, ((qif_t *)x)->qf_name, \
270 					       LIFNAMSIZ)
271 #ifdef	IRE_ILL_CN
272 extern kmutex_t s_ill_g_head_lock;
273 extern struct s_ill_s *s_ill_g_head;	/* ILL List Head */
274 #endif /* IRE_ILL_CN */
275 #  define	GETKTIME(x)	uniqtime((struct timeval *)x)
276 #  define	MSGDSIZE(x)	msgdsize(x)
277 #  define	M_LEN(x)	((x)->b_wptr - (x)->b_rptr)
278 #  define	M_DUPLICATE(x)	dupmsg((x))
279 #  define	MTOD(m,t)	((t)((m)->b_rptr))
280 #  define	MTYPE(m)	((m)->b_datap->db_type)
281 #  define	FREE_MB_T(m)	freemsg(m)
282 #  define	m_next		b_cont
283 #  define	CACHE_HASH(x)	(((qpktinfo_t *)(x)->fin_qpi)->qpi_num & 7)
284 #  define	IPF_PANIC(x,y)	if (x) { printf y; cmn_err(CE_PANIC, "ipf_panic"); }
285 typedef mblk_t mb_t;
286 # endif /* _KERNEL */
287 
288 # if (SOLARIS2 >= 7)
289 #  ifdef lint
290 #   define ALIGN32(ptr)    (ptr ? 0L : 0L)
291 #   define ALIGN16(ptr)    (ptr ? 0L : 0L)
292 #  else
293 #   define ALIGN32(ptr)    (ptr)
294 #   define ALIGN16(ptr)    (ptr)
295 #  endif
296 # endif
297 
298 # if SOLARIS2 < 6
299 typedef	struct uio	uio_t;
300 # endif
301 typedef	int		ioctlcmd_t;
302 
303 # define OS_RECOGNISED 1
304 
305 #endif /* SOLARIS */
306 
307 /* ----------------------------------------------------------------------- */
308 /*                                  H P U X                                */
309 /* ----------------------------------------------------------------------- */
310 #ifdef __hpux
311 # define	MENTAT	1
312 # include	<sys/sysmacros.h>
313 # include	<sys/spinlock.h>
314 # include	<sys/lock.h>
315 # include	<sys/stream.h>
316 # ifdef USE_INET6
317 #  include	<netinet/if_ether.h>
318 #  include	<netinet/ip6.h>
319 #  include	<netinet/icmp6.h>
320 typedef	struct	ip6_hdr	ip6_t;
321 # endif
322 
323 # ifdef _KERNEL
324 #  define SNPRINTF	sprintf
325 #  if (HPUXREV >= 1111)
326 #   define	IPL_SELECT
327 #   ifdef	IPL_SELECT
328 #    include	<machine/sys/user.h>
329 #    include	<sys/kthread_iface.h>
330 #    define	READ_COLLISION	0x01
331 
332 typedef	struct	iplog_select_s {
333 	kthread_t	*read_waiter;
334 	int		state;
335 } iplog_select_t;
336 #   endif
337 #  endif
338 
339 #  define	GETKTIME(x)	uniqtime((struct timeval *)x)
340 
341 #  if HPUXREV == 1111
342 #   include	"kern_svcs.h"
343 #  else
344 #   include	<sys/kern_svcs.h>
345 #  endif
346 #  undef	ti_flags
347 #  undef	TCP_NODELAY
348 #  undef	TCP_MAXSEG
349 #  include <sys/reg.h>
350 #  include "../netinet/ip_info.h"
351 /*
352  * According to /usr/include/sys/spinlock.h on HP-UX 11.00, these functions
353  * are available.  Attempting to use them actually results in unresolved
354  * symbols when it comes time to load the module.
355  * This has been fixed!  Yipee!
356  */
357 #  if 1
358 #   ifdef __LP64__
359 #    define	ATOMIC_INCL(x)		lock_and_incr_int64(&ipf_rw.ipf_lk, &(x), 1)
360 #    define	ATOMIC_DECL(x)		lock_and_incr_int64(&ipf_rw.ipf_lk, &(x), -1)
361 #   else
362 #    define	ATOMIC_INCL(x)		lock_and_incr_int32(&ipf_rw.ipf_lk, &(x), 1)
363 #    define	ATOMIC_DECL(x)		lock_and_incr_int32(&ipf_rw.ipf_lk, &(x), -1)
364 #   endif
365 #   define	ATOMIC_INC64(x)		lock_and_incr_int64(&ipf_rw.ipf_lk, &(x), 1)
366 #   define	ATOMIC_INC32(x)		lock_and_incr_int32(&ipf_rw.ipf_lk, &(x), 1)
367 #   define	ATOMIC_INC16(x)		lock_and_incr_int16(&ipf_rw.ipf_lk, &(x), 1)
368 #   define	ATOMIC_DEC64(x)		lock_and_incr_int64(&ipf_rw.ipf_lk, &(x), -1)
369 #   define	ATOMIC_DEC32(x)		lock_and_incr_int32(&ipf_rw.ipf_lk, &(x), -1)
370 #   define	ATOMIC_DEC16(x)		lock_and_incr_int16(&ipf_rw.ipf_lk, &(x), -1)
371 #  else /* 0 */
372 #   define	ATOMIC_INC64(x)		{ MUTEX_ENTER(&ipf_rw); (x)++; \
373 					  MUTEX_EXIT(&ipf_rw); }
374 #   define	ATOMIC_DEC64(x)		{ MUTEX_ENTER(&ipf_rw); (x)--; \
375 					  MUTEX_EXIT(&ipf_rw); }
376 #   define	ATOMIC_INC32(x)		{ MUTEX_ENTER(&ipf_rw); (x)++; \
377 					  MUTEX_EXIT(&ipf_rw); }
378 #   define	ATOMIC_DEC32(x)		{ MUTEX_ENTER(&ipf_rw); (x)--; \
379 					  MUTEX_EXIT(&ipf_rw); }
380 #   define	ATOMIC_INCL(x)		{ MUTEX_ENTER(&ipf_rw); (x)++; \
381 					  MUTEX_EXIT(&ipf_rw); }
382 #   define	ATOMIC_DECL(x)		{ MUTEX_ENTER(&ipf_rw); (x)--; \
383 					  MUTEX_EXIT(&ipf_rw); }
384 #   define	ATOMIC_INC(x)		{ MUTEX_ENTER(&ipf_rw); (x)++; \
385 					  MUTEX_EXIT(&ipf_rw); }
386 #   define	ATOMIC_DEC(x)		{ MUTEX_ENTER(&ipf_rw); (x)--; \
387 					  MUTEX_EXIT(&ipf_rw); }
388 #  endif
389 #  define	ip_cksum		ip_csuma
390 #  define	memcpy(a,b,c)		bcopy((caddr_t)b, (caddr_t)a, c)
391 #  define	USE_MUTEXES
392 #  define	MUTEX_INIT(x, y)	initlock(&(x)->ipf_lk, 0, 0, (y))
393 #  define	MUTEX_ENTER(x)		spinlock(&(x)->ipf_lk)
394 #  define	MUTEX_EXIT(x)		spinunlock(&(x)->ipf_lk);
395 #  define	MUTEX_DESTROY(x)
396 #  define	MUTEX_NUKE(x)		bzero((char *)(x), sizeof(*(x)))
397 #  define	KMUTEX_T		lock_t
398 #  define	kmutex_t		lock_t		/* for pfil.h */
399 #  define	krwlock_t		lock_t		/* for pfil.h */
400 /*
401  * The read-write lock implementation in HP-UX 11.0 is crippled - it can
402  * only be used by threads working in a user context!
403  * This has been fixed!  Yipee! (Or at least it does in 11.00, not 11.11..)
404  */
405 #  if HPUXREV < 1111
406 #   define	MUTEX_DOWNGRADE(x)	lock_write_to_read(x)
407 #   define	KRWLOCK_T		struct rw_lock
408 #   define	READ_ENTER(x)		lock_read(&(x)->ipf_lk)
409 #   define	WRITE_ENTER(x)		lock_write(&(x)->ipf_lk)
410 #   if HPUXREV >= 1111
411 #    define	RWLOCK_INIT(x, y)	rwlock_init4(&(x)->ipf_lk, 0, RWLCK_CANSLEEP, 0, y)
412 #   else
413 #    define	RWLOCK_INIT(x, y)	lock_init3(&(x)->ipf_lk, 0, 1, 0, 0, y)
414 #   endif
415 #   define	RWLOCK_EXIT(x)		lock_done(&(x)->ipf_lk)
416 #  else
417 #   define	KRWLOCK_T		lock_t
418 #   define	KMUTEX_T		lock_t
419 #   define	READ_ENTER(x)		MUTEX_ENTER(x)
420 #   define	WRITE_ENTER(x)		MUTEX_ENTER(x)
421 #   define	MUTEX_DOWNGRADE(x)
422 #   define	RWLOCK_INIT(x, y)	initlock(&(x)->ipf_lk, 0, 0, y)
423 #   define	RWLOCK_EXIT(x)		MUTEX_EXIT(x)
424 #  endif
425 #  define	RW_DESTROY(x)
426 #  define	COPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
427 #  define	COPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
428 #  if HPUXREV >= 1111
429 #   define	BCOPYIN(a,b,c)	0; bcopy((caddr_t)(a), (caddr_t)(b), (c))
430 #   define	BCOPYOUT(a,b,c)	0; bcopy((caddr_t)(a), (caddr_t)(b), (c))
431 #  else
432 #   define	BCOPYIN(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
433 #   define	BCOPYOUT(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
434 #  endif
435 #  define	SPL_NET(x)	;
436 #  define	SPL_IMP(x)	;
437 #  undef	SPL_X
438 #  define	SPL_X(x)	;
439 extern	void	*get_unit __P((char *, int));
440 #  define	GETIFP(n, v)	get_unit(n, v)
441 #  define	IFNAME(x, b)	((ill_t *)x)->ill_name
442 #  define	COPYIFNAME(x, b) \
443 				(void) strncpy(b, ((qif_t *)x)->qf_name, \
444 					       LIFNAMSIZ)
445 #  define	UIOMOVE(a,b,c,d)	uiomove((caddr_t)a,b,c,d)
446 #  define	SLEEP(id, n)	{ lock_t *_l = get_sleep_lock((caddr_t)id); \
447 				  sleep(id, PZERO+1); \
448 				  spinunlock(_l); \
449 				}
450 #  define	WAKEUP(id,x)	{ lock_t *_l = get_sleep_lock((caddr_t)id); \
451 				  wakeup(id + x); \
452 				  spinunlock(_l); \
453 				}
454 #  define	KMALLOC(a, b)	MALLOC((a), b, sizeof(*(a)), M_IOSYS, M_NOWAIT)
455 #  define	KMALLOCS(a, b, c)	MALLOC((a), b, (c), M_IOSYS, M_NOWAIT)
456 #  define	KFREE(x)	kmem_free((char *)(x), sizeof(*(x)))
457 #  define	KFREES(x,s)	kmem_free((char *)(x), (s))
458 #  define	MSGDSIZE(x)	msgdsize(x)
459 #  define	M_LEN(x)	((x)->b_wptr - (x)->b_rptr)
460 #  define	M_DUPLICATE(x)	dupmsg((x))
461 #  define	MTOD(m,t)	((t)((m)->b_rptr))
462 #  define	MTYPE(m)	((m)->b_datap->db_type)
463 #  define	FREE_MB_T(m)	freemsg(m)
464 #  define	m_next		b_cont
465 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
466 typedef mblk_t mb_t;
467 
468 #  define	CACHE_HASH(x)	(((qpktinfo_t *)(x)->fin_qpi)->qpi_num & 7)
469 
470 #  include "qif.h"
471 #  include "pfil.h"
472 
473 # else /* _KERNEL */
474 
475 typedef	unsigned char uchar_t;
476 
477 #  ifndef	_SYS_STREAM_INCLUDED
478 typedef char * mblk_t;
479 typedef void * queue_t;
480 typedef	u_long ulong;
481 #  endif
482 #  include <netinet/ip_info.h>
483 
484 # endif /* _KERNEL */
485 
486 # ifdef lint
487 #  define ALIGN32(ptr)    (ptr ? 0L : 0L)
488 #  define ALIGN16(ptr)    (ptr ? 0L : 0L)
489 # else
490 #  define ALIGN32(ptr)    (ptr)
491 #  define ALIGN16(ptr)    (ptr)
492 # endif
493 
494 typedef	struct uio	uio_t;
495 typedef	int		ioctlcmd_t;
496 typedef	int		minor_t;
497 typedef unsigned int	u_32_t;
498 # define	U_32_T	1
499 
500 # define OS_RECOGNISED 1
501 
502 #endif /* __hpux */
503 
504 /* ----------------------------------------------------------------------- */
505 /*                                  I R I X                                */
506 /* ----------------------------------------------------------------------- */
507 #ifdef __sgi
508 # undef		MENTAT
509 # if IRIX < 60500
510 typedef	struct uio	uio_t;
511 # endif
512 typedef	int		ioctlcmd_t;
513 typedef u_int32_t       u_32_t;
514 # define	U_32_T	1
515 
516 # ifdef INET6
517 #  define USE_INET6
518 # endif
519 
520 # define  hz HZ
521 # include <sys/ksynch.h>
522 # define	IPF_LOCK_PL	plhi
523 # include <sys/sema.h>
524 # undef kmutex_t
525 typedef struct {
526 	lock_t *l;
527 	int pl;
528 } kmutex_t;
529 
530 # ifdef MUTEX_INIT
531 #  define	KMUTEX_T		mutex_t
532 # else
533 #  define	KMUTEX_T		kmutex_t
534 #  define	KRWLOCK_T		kmutex_t
535 # endif
536 
537 # ifdef _KERNEL
538 #  define	ATOMIC_INC(x)		{ MUTEX_ENTER(&ipf_rw); \
539 					  (x)++; MUTEX_EXIT(&ipf_rw); }
540 #  define	ATOMIC_DEC(x)		{ MUTEX_ENTER(&ipf_rw); \
541 					  (x)--; MUTEX_EXIT(&ipf_rw); }
542 #  define	USE_MUTEXES
543 #  ifdef MUTEX_INIT
544 #   include <sys/atomic_ops.h>
545 #   define	ATOMIC_INCL(x)		atomicAddUlong(&(x), 1)
546 #   define	ATOMIC_INC64(x)		atomicAddUint64(&(x), 1)
547 #   define	ATOMIC_INC32(x)		atomicAddUint(&(x), 1)
548 #   define	ATOMIC_INC16		ATOMIC_INC
549 #   define	ATOMIC_DECL(x)		atomicAddUlong(&(x), -1)
550 #   define	ATOMIC_DEC64(x)		atomicAddUint64(&(x), -1)
551 #   define	ATOMIC_DEC32(x)		atomicAddUint(&(x), -1)
552 #   define	ATOMIC_DEC16		ATOMIC_DEC
553 #   undef	MUTEX_INIT
554 #   define	MUTEX_INIT(x, y)	mutex_init(&(x)->ipf_lk,  \
555 						   MUTEX_DEFAULT, y)
556 #   undef	MUTEX_ENTER
557 #   define	MUTEX_ENTER(x)		mutex_lock(&(x)->ipf_lk, 0)
558 #   undef	MUTEX_EXIT
559 #   define	MUTEX_EXIT(x)		mutex_unlock(&(x)->ipf_lk)
560 #   undef	MUTEX_DESTROY
561 #   define	MUTEX_DESTROY(x)	mutex_destroy(&(x)->ipf_lk)
562 #   define	MUTEX_DOWNGRADE(x)	mrdemote(&(x)->ipf_lk)
563 #   define	KRWLOCK_T		mrlock_t
564 #   define	RWLOCK_INIT(x, y)	mrinit(&(x)->ipf_lk, y)
565 #   undef	RW_DESTROY
566 #   define	RW_DESTROY(x)		mrfree(&(x)->ipf_lk)
567 #   define	READ_ENTER(x)		RW_RDLOCK(&(x)->ipf_lk)
568 #   define	WRITE_ENTER(x)		RW_WRLOCK(&(x)->ipf_lk)
569 #   define	RWLOCK_EXIT(x)		RW_UNLOCK(&(x)->ipf_lk)
570 #  else
571 #   define	READ_ENTER(x)		MUTEX_ENTER(&(x)->ipf_lk)
572 #   define	WRITE_ENTER(x)		MUTEX_ENTER(&(x)->ipf_lk)
573 #   define	MUTEX_DOWNGRADE(x)	;
574 #   define	RWLOCK_EXIT(x)		MUTEX_EXIT(&(x)->ipf_lk)
575 #   define	MUTEX_EXIT(x)		UNLOCK((x)->ipf_lk.l, (x)->ipf_lk.pl);
576 #   define	MUTEX_INIT(x,y)		(x)->ipf_lk.l = LOCK_ALLOC((uchar_t)-1, IPF_LOCK_PL, (lkinfo_t *)-1, KM_NOSLEEP)
577 #   define	MUTEX_DESTROY(x)	LOCK_DEALLOC((x)->ipf_lk.l)
578 #   define	MUTEX_ENTER(x)		(x)->ipf_lk.pl = LOCK((x)->ipf_lk.l, \
579 							      IPF_LOCK_PL);
580 #  endif
581 #  define	MUTEX_NUKE(x)		bzero((x), sizeof(*(x)))
582 #  define	FREE_MB_T(m)	m_freem(m)
583 #  define	MTOD(m,t)	mtod(m,t)
584 #  define	COPYIN(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
585 #  define	COPYOUT(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
586 #  define	BCOPYIN(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
587 #  define	BCOPYOUT(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
588 #  define	UIOMOVE(a,b,c,d)	uiomove((caddr_t)a,b,c,d)
589 #  define	SLEEP(id, n)	sleep((id), PZERO+1)
590 #  define	WAKEUP(id,x)	wakeup(id+x)
591 #  define	KFREE(x)	kmem_free((char *)(x), sizeof(*(x)))
592 #  define	KFREES(x,s)	kmem_free((char *)(x), (s))
593 #  define	GETIFP(n,v)	ifunit(n)
594 #  include <sys/kmem.h>
595 #  include <sys/ddi.h>
596 #  define	KMALLOC(a,b)	(a) = (b)kmem_alloc(sizeof(*(a)), KM_NOSLEEP)
597 #  define	KMALLOCS(a,b,c)	(a) = (b)kmem_alloc((c), KM_NOSLEEP)
598 #  define	GET_MINOR(x)	getminor(x)
599 #  define	USE_SPL		1
600 #  define	SPL_IMP(x)	(x) = splimp()
601 #  define	SPL_NET(x)	(x) = splnet()
602 #  define	SPL_X(x)	(void) splx(x)
603 extern	void	m_copydata __P((struct mbuf *, int, int, caddr_t));
604 extern	void	m_copyback __P((struct mbuf *, int, int, caddr_t));
605 #  define	MSGDSIZE(x)	mbufchainlen(x)
606 #  define	M_LEN(x)	(x)->m_len
607 #  define	M_DUPLICATE(x)	m_copy((x), 0, M_COPYALL)
608 #  define	GETKTIME(x)	microtime((struct timeval *)x)
609 #  define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
610 				  ((struct ifnet *)fin->fin_ifp)->if_unit) & 7)
611 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
612 typedef struct mbuf mb_t;
613 # else
614 #  undef RW_DESTROY
615 #  undef MUTEX_INIT
616 #  undef MUTEX_DESTROY
617 # endif /* _KERNEL */
618 
619 # define OS_RECOGNISED 1
620 
621 #endif /* __sgi */
622 
623 /* ----------------------------------------------------------------------- */
624 /*                                  T R U 6 4                              */
625 /* ----------------------------------------------------------------------- */
626 #ifdef __osf__
627 # undef		MENTAT
628 
629 # include <kern/lock.h>
630 # include <sys/sysmacros.h>
631 
632 # ifdef _KERNEL
633 #  define	KMUTEX_T		simple_lock_data_t
634 #  define	KRWLOCK_T		lock_data_t
635 #  include <net/net_globals.h>
636 #  define	USE_MUTEXES
637 #  define	READ_ENTER(x)		lock_read(&(x)->ipf_lk)
638 #  define	WRITE_ENTER(x)		lock_write(&(x)->ipf_lk)
639 #  define	MUTEX_DOWNGRADE(x)	lock_write_to_read(&(x)->ipf_lk)
640 #  define	RWLOCK_INIT(x, y)	lock_init(&(x)->ipf_lk, TRUE)
641 #  define	RWLOCK_EXIT(x)		lock_done(&(x)->ipf_lk)
642 #  define	RW_DESTROY(x)		lock_terminate(&(x)->ipf_lk)
643 #  define	MUTEX_ENTER(x)		simple_lock(&(x)->ipf_lk)
644 #  define	MUTEX_INIT(x, y)	simple_lock_init(&(x)->ipf_lk)
645 #  define	MUTEX_DESTROY(x)	simple_lock_terminate(&(x)->ipf_lk)
646 #  define	MUTEX_EXIT(x)		simple_unlock(&(x)->ipf_lk)
647 #  define	MUTEX_NUKE(x)		bzero(x, sizeof(*(x)))
648 #  define	ATOMIC_INC64(x)		atomic_incq((uint64_t*)&(x))
649 #  define	ATOMIC_DEC64(x)		atomic_decq((uint64_t*)&(x))
650 #  define	ATOMIC_INC32(x)		atomic_incl((uint32_t*)&(x))
651 #  define	ATOMIC_DEC32(x)		atomic_decl((uint32_t*)&(x))
652 #  define	ATOMIC_INC16(x)		{ simple_lock(&ipf_rw); (x)++; \
653 					  simple_unlock(&ipf_rw); }
654 #  define	ATOMIC_DEC16(x)		{ simple_lock(&ipf_rw); (x)--; \
655 					  simple_unlock(&ipf_rw); }
656 #  define	ATOMIC_INCL(x)		atomic_incl((uint32_t*)&(x))
657 #  define	ATOMIC_DECL(x)		atomic_decl((uint32_t*)&(x))
658 #  define	ATOMIC_INC(x)		{ simple_lock(&ipf_rw); (x)++; \
659 					  simple_unlock(&ipf_rw); }
660 #  define	ATOMIC_DEC(x)		{ simple_lock(&ipf_rw); (x)--; \
661 					  simple_unlock(&ipf_rw); }
662 #  define	SPL_NET(x)		;
663 #  define	SPL_IMP(x)		;
664 #  undef	SPL_X
665 #  define	SPL_X(x)		;
666 #  define	UIOMOVE(a,b,c,d)	uiomove((caddr_t)a, b, d)
667 #  define	FREE_MB_T(m)		m_freem(m)
668 #  define	MTOD(m,t)		mtod(m,t)
669 #  define	GETIFP(n, v)		ifunit(n)
670 #  define	GET_MINOR		getminor
671 #  define	WAKEUP(id,x)		wakeup(id + x)
672 #  define	COPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
673 #  define	COPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
674 #  define	BCOPYIN(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
675 #  define	BCOPYOUT(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
676 #  define	KMALLOC(a, b)	MALLOC((a), b, sizeof(*(a)), M_PFILT, M_NOWAIT)
677 #  define	KMALLOCS(a, b, c)	MALLOC((a), b, (c), M_PFILT, \
678 					    ((c) > 4096) ? M_WAITOK : M_NOWAIT)
679 #  define	KFREE(x)	FREE((x), M_PFILT)
680 #  define	KFREES(x,s)	FREE((x), M_PFILT)
681 #  define	MSGDSIZE(x)	mbufchainlen(x)
682 #  define	M_LEN(x)	(x)->m_len
683 #  define	M_DUPLICATE(x)	m_copy((x), 0, M_COPYALL)
684 #  define	GETKTIME(x)	microtime((struct timeval *)x)
685 #  define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
686 				  ((struct ifnet *)fin->fin_ifp)->if_unit) & 7)
687 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
688 typedef struct mbuf mb_t;
689 # endif /* _KERNEL */
690 
691 # if (defined(_KERNEL) || defined(_NO_BITFIELDS) || (__STDC__ == 1))
692 #  define	IP_V(x)		((x)->ip_vhl >> 4)
693 #  define	IP_HL(x)	((x)->ip_vhl & 0xf)
694 #  define	IP_V_A(x,y)	(x)->ip_vhl |= (((y) << 4) & 0xf0)
695 #  define	IP_HL_A(x,y)	(x)->ip_vhl |= ((y) & 0xf)
696 #  define	TCP_X2(x)	((x)->th_xoff & 0xf)
697 #  define	TCP_X2_A(x,y)	(x)->th_xoff |= ((y) & 0xf)
698 #  define	TCP_OFF(x)	((x)->th_xoff >> 4)
699 #  define	TCP_OFF_A(x,y)	(x)->th_xoff |= (((y) << 4) & 0xf0)
700 # endif
701 
702 /*
703  * These are from's Solaris' #defines for little endian.
704  */
705 #define	IP6F_MORE_FRAG		0x0100
706 #define	IP6F_RESERVED_MASK	0x0600
707 #define	IP6F_OFF_MASK		0xf8ff
708 
709 struct ip6_ext {
710 	u_char	ip6e_nxt;
711 	u_char	ip6e_len;
712 };
713 
714 typedef	int		ioctlcmd_t;
715 /*
716  * Really, any arch where sizeof(long) != sizeof(int).
717  */
718 typedef unsigned int    u_32_t;
719 # define	U_32_T	1
720 
721 # define OS_RECOGNISED 1
722 #endif /* __osf__ */
723 
724 /* ----------------------------------------------------------------------- */
725 /*                                  N E T B S D                            */
726 /* ----------------------------------------------------------------------- */
727 #ifdef __NetBSD__
728 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
729 #  include "bpfilter.h"
730 #  if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 104110000)
731 #   include "opt_inet.h"
732 #  endif
733 #  ifdef INET6
734 #   define USE_INET6
735 #  endif
736 #  if (__NetBSD_Version__ >= 105000000)
737 #   define HAVE_M_PULLDOWN 1
738 #  endif
739 # endif
740 
741 # ifdef _KERNEL
742 #  define	MSGDSIZE(x)	mbufchainlen(x)
743 #  define	M_LEN(x)	(x)->m_len
744 #  define	M_DUPLICATE(x)	m_copy((x), 0, M_COPYALL)
745 #  define	GETKTIME(x)	microtime((struct timeval *)x)
746 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
747 #  define	COPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
748 #  define	COPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
749 #  define	BCOPYIN(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
750 #  define	BCOPYOUT(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
751 typedef struct mbuf mb_t;
752 # endif /* _KERNEL */
753 # if (NetBSD <= 1991011) && (NetBSD >= 199606)
754 #  define	IFNAME(x)	((struct ifnet *)x)->if_xname
755 #  define	COPYIFNAME(x, b) \
756 				(void) strncpy(b, \
757 					       ((struct ifnet *)x)->if_xname, \
758 					       LIFNAMSIZ)
759 #  define	CACHE_HASH(x)	((((struct ifnet *)fin->fin_ifp)->if_index)&7)
760 # else
761 #  define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
762 				  ((struct ifnet *)fin->fin_ifp)->if_unit) & 7)
763 # endif
764 
765 typedef	struct uio	uio_t;
766 typedef	u_long		ioctlcmd_t;
767 typedef	int		minor_t;
768 typedef	u_int32_t	u_32_t;
769 # define	U_32_T	1
770 
771 # define OS_RECOGNISED 1
772 #endif /* __NetBSD__ */
773 
774 
775 /* ----------------------------------------------------------------------- */
776 /*                                F R E E B S D                            */
777 /* ----------------------------------------------------------------------- */
778 #ifdef __FreeBSD__
779 # if defined(_KERNEL)
780 #  if (__FreeBSD_version >= 500000)
781 #   include "opt_bpf.h"
782 #  else
783 #   include "bpf.h"
784 #  endif
785 #  if defined(__FreeBSD_version) && (__FreeBSD_version >= 400000)
786 #   include "opt_inet6.h"
787 #  endif
788 #  if defined(INET6) && !defined(USE_INET6)
789 #   define USE_INET6
790 #  endif
791 # endif
792 
793 # if defined(_KERNEL)
794 #  if (__FreeBSD_version >= 400000)
795 /*
796  * When #define'd, the 5.2.1 kernel panics when used with the ftp proxy.
797  * There may be other, safe, kernels but this is not extensively tested yet.
798  */
799 #   define HAVE_M_PULLDOWN
800 #  endif
801 #  if !defined(IPFILTER_LKM) && (__FreeBSD_version >= 300000)
802 #   include "opt_ipfilter.h"
803 #  endif
804 #  define	COPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
805 #  define	COPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
806 #  define	BCOPYIN(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
807 #  define	BCOPYOUT(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
808 
809 #  if (__FreeBSD_version >= 500043)
810 #   define NETBSD_PF
811 #  endif
812 # endif /* _KERNEL */
813 
814 # if (__FreeBSD_version >= 500043)
815 #  include <sys/mutex.h>
816 #  include <sys/sx.h>
817 /*
818  * Whilst the sx(9) locks on FreeBSD have the right semantics and interface
819  * for what we want to use them for, despite testing showing they work -
820  * with a WITNESS kernel, it generates LOR messages.
821  */
822 #  define	KMUTEX_T		struct mtx
823 #  if 1
824 #   define	KRWLOCK_T		struct mtx
825 #  else
826 #   define	KRWLOCK_T		struct sx
827 #  endif
828 # endif
829 
830 # if (__FreeBSD_version >= 501113)
831 #  include <net/if_var.h>
832 #  define	IFNAME(x)	((struct ifnet *)x)->if_xname
833 #  define	COPYIFNAME(x, b) \
834 				(void) strncpy(b, \
835 					       ((struct ifnet *)x)->if_xname, \
836 					       LIFNAMSIZ)
837 # endif
838 # if (__FreeBSD_version >= 500043)
839 #  define	CACHE_HASH(x)	((((struct ifnet *)fin->fin_ifp)->if_index) & 7)
840 # else
841 #  define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
842 				  ((struct ifnet *)fin->fin_ifp)->if_unit) & 7)
843 # endif
844 
845 # ifdef _KERNEL
846 #  define	GETKTIME(x)	microtime((struct timeval *)x)
847 
848 #  if (__FreeBSD_version >= 500002)
849 #   include <netinet/in_systm.h>
850 #   include <netinet/ip.h>
851 #   include <machine/in_cksum.h>
852 #  endif
853 
854 #  if (__FreeBSD_version >= 500043)
855 #   define	USE_MUTEXES
856 #   define	MUTEX_ENTER(x)		mtx_lock(&(x)->ipf_lk)
857 #   define	MUTEX_EXIT(x)		mtx_unlock(&(x)->ipf_lk)
858 #   define	MUTEX_INIT(x,y)		mtx_init(&(x)->ipf_lk, (y), NULL,\
859 						 MTX_DEF)
860 #   define	MUTEX_DESTROY(x)	mtx_destroy(&(x)->ipf_lk)
861 #   define	MUTEX_NUKE(x)		bzero((x), sizeof(*(x)))
862 /*
863  * Whilst the sx(9) locks on FreeBSD have the right semantics and interface
864  * for what we want to use them for, despite testing showing they work -
865  * with a WITNESS kernel, it generates LOR messages.
866  */
867 #   if 1
868 #    define	READ_ENTER(x)		mtx_lock(&(x)->ipf_lk)
869 #    define	WRITE_ENTER(x)		mtx_lock(&(x)->ipf_lk)
870 #    define	RWLOCK_EXIT(x)		mtx_unlock(&(x)->ipf_lk)
871 #    define	MUTEX_DOWNGRADE(x)	;
872 #    define	RWLOCK_INIT(x,y)	mtx_init(&(x)->ipf_lk, (y), NULL,\
873 						 MTX_DEF)
874 #    define	RW_DESTROY(x)		mtx_destroy(&(x)->ipf_lk)
875 #   else
876 #    define	READ_ENTER(x)		sx_slock(&(x)->ipf_lk)
877 #    define	WRITE_ENTER(x)		sx_xlock(&(x)->ipf_lk)
878 #    define	MUTEX_DOWNGRADE(x)	sx_downgrade(&(x)->ipf_lk)
879 #    define	RWLOCK_INIT(x, y)	sx_init(&(x)->ipf_lk, (y))
880 #    define	RW_DESTROY(x)		sx_destroy(&(x)->ipf_lk)
881 #    ifdef sx_unlock
882 #     define	RWLOCK_EXIT(x)		sx_unlock(x)
883 #    else
884 #     define	RWLOCK_EXIT(x)		do { \
885 					    if ((x)->ipf_lk.sx_cnt < 0) \
886 						sx_xunlock(&(x)->ipf_lk); \
887 					    else \
888 						sx_sunlock(&(x)->ipf_lk); \
889 					} while (0)
890 #    endif
891 #   endif
892 #   include <machine/atomic.h>
893 #   define	ATOMIC_INC(x)		{ mtx_lock(&ipf_rw.ipf_lk); (x)++; \
894 					  mtx_unlock(&ipf_rw.ipf_lk); }
895 #   define	ATOMIC_DEC(x)		{ mtx_lock(&ipf_rw.ipf_lk); (x)--; \
896 					  mtx_unlock(&ipf_rw.ipf_lk); }
897 #   define	ATOMIC_INCL(x)		atomic_add_long(&(x), 1)
898 #   define	ATOMIC_INC64(x)		ATOMIC_INC(x)
899 #   define	ATOMIC_INC32(x)		atomic_add_32(&(x), 1)
900 #   define	ATOMIC_INC16(x)		atomic_add_16(&(x), 1)
901 #   define	ATOMIC_DECL(x)		atomic_add_long(&(x), -1)
902 #   define	ATOMIC_DEC64(x)		ATOMIC_DEC(x)
903 #   define	ATOMIC_DEC32(x)		atomic_add_32(&(x), -1)
904 #   define	ATOMIC_DEC16(x)		atomic_add_16(&(x), -1)
905 #   define	SPL_X(x)	;
906 #   define	SPL_NET(x)	;
907 #   define	SPL_IMP(x)	;
908 extern	int	in_cksum __P((struct mbuf *, int));
909 #  endif /* __FreeBSD_version >= 500043 */
910 #  define	MSGDSIZE(x)	mbufchainlen(x)
911 #  define	M_LEN(x)	(x)->m_len
912 #  define	M_DUPLICATE(x)	m_copy((x), 0, M_COPYALL)
913 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
914 typedef struct mbuf mb_t;
915 # endif /* _KERNEL */
916 
917 # if __FreeBSD__ < 3
918 #  include <machine/spl.h>
919 # else
920 #  if __FreeBSD__ == 3
921 #   if defined(IPFILTER_LKM) && !defined(ACTUALLY_LKM_NOT_KERNEL)
922 #    define	ACTUALLY_LKM_NOT_KERNEL
923 #   endif
924 #  endif
925 # endif
926 
927 # if (__FreeBSD_version >= 300000)
928 typedef	u_long		ioctlcmd_t;
929 # else
930 typedef	int		ioctlcmd_t;
931 # endif
932 typedef	struct uio	uio_t;
933 typedef	int		minor_t;
934 typedef	u_int32_t	u_32_t;
935 # define	U_32_T	1
936 
937 # define OS_RECOGNISED 1
938 #endif /* __FreeBSD__ */
939 
940 
941 /* ----------------------------------------------------------------------- */
942 /*                                O P E N B S D                            */
943 /* ----------------------------------------------------------------------- */
944 #ifdef __OpenBSD__
945 # ifdef INET6
946 #  define USE_INET6
947 # endif
948 
949 # ifdef _KERNEL
950 #  if !defined(IPFILTER_LKM)
951 #   include "bpfilter.h"
952 #  endif
953 #  if (OpenBSD >= 200311)
954 #   define SNPRINTF	snprintf
955 #   if defined(USE_INET6)
956 #    include "netinet6/in6_var.h"
957 #    include "netinet6/nd6.h"
958 #   endif
959 #  endif
960 #  if (OpenBSD >= 200012)
961 #   define HAVE_M_PULLDOWN 1
962 #  endif
963 #  define	COPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
964 #  define	COPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
965 #  define	BCOPYIN(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
966 #  define	BCOPYOUT(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
967 #  define	GETKTIME(x)	microtime((struct timeval *)x)
968 #  define	MSGDSIZE(x)	mbufchainlen(x)
969 #  define	M_LEN(x)	(x)->m_len
970 #  define	M_DUPLICATE(x)	m_copy((x), 0, M_COPYALL)
971 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
972 typedef struct mbuf mb_t;
973 # endif /* _KERNEL */
974 # if (OpenBSD >= 199603)
975 #  define	IFNAME(x, b)	((struct ifnet *)x)->if_xname
976 #  define	COPYIFNAME(x, b) \
977 				(void) strncpy(b, \
978 					       ((struct ifnet *)x)->if_xname, \
979 					       LIFNAMSIZ)
980 #  define	CACHE_HASH(x)	((((struct ifnet *)fin->fin_ifp)->if_index)&7)
981 # else
982 #  define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
983 				  ((struct ifnet *)fin->fin_ifp)->if_unit) & 7)
984 # endif
985 
986 typedef	struct uio	uio_t;
987 typedef	u_long		ioctlcmd_t;
988 typedef	int		minor_t;
989 typedef	u_int32_t	u_32_t;
990 # define	U_32_T	1
991 
992 # define OS_RECOGNISED 1
993 #endif /* __OpenBSD__ */
994 
995 
996 /* ----------------------------------------------------------------------- */
997 /*                                B S D O S                                */
998 /* ----------------------------------------------------------------------- */
999 #ifdef _BSDI_VERSION
1000 # ifdef INET6
1001 #  define USE_INET6
1002 # endif
1003 
1004 # ifdef _KERNEL
1005 #  define	GETKTIME(x)	microtime((struct timeval *)x)
1006 #  define	MSGDSIZE(x)	mbufchainlen(x)
1007 #  define	M_LEN(x)	(x)->m_len
1008 #  define	M_DUPLICATE(x)	m_copy((x), 0, M_COPYALL)
1009 #  define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
1010 				  ((struct ifnet *)fin->fin_ifp)->if_unit) & 7)
1011 typedef struct mbuf mb_t;
1012 # endif /* _KERNEL */
1013 
1014 # if (_BSDI_VERSION >= 199701)
1015 typedef	u_long		ioctlcmd_t;
1016 # else
1017 typedef	int		ioctlcmd_t;
1018 # endif
1019 typedef	u_int32_t	u_32_t;
1020 # define	U_32_T	1
1021 
1022 #endif /* _BSDI_VERSION */
1023 
1024 
1025 /* ----------------------------------------------------------------------- */
1026 /*                                  S U N O S 4                            */
1027 /* ----------------------------------------------------------------------- */
1028 #if defined(sun) && !defined(OS_RECOGNISED) /* SunOS4 */
1029 # ifdef _KERNEL
1030 #  include	<sys/kmem_alloc.h>
1031 #  define	GETKTIME(x)	uniqtime((struct timeval *)x)
1032 #  define	MSGDSIZE(x)	mbufchainlen(x)
1033 #  define	M_LEN(x)	(x)->m_len
1034 #  define	M_DUPLICATE(x)	m_copy((x), 0, M_COPYALL)
1035 #  define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
1036 				  ((struct ifnet *)fin->fin_ifp)->if_unit) & 7)
1037 #  define	GETIFP(n, v)	ifunit(n, IFNAMSIZ)
1038 #  define	KFREE(x)	kmem_free((char *)(x), sizeof(*(x)))
1039 #  define	KFREES(x,s)	kmem_free((char *)(x), (s))
1040 #  define	SLEEP(id, n)	sleep((id), PZERO+1)
1041 #  define	WAKEUP(id,x)	wakeup(id + x)
1042 #  define	UIOMOVE(a,b,c,d)	uiomove((caddr_t)a,b,c,d)
1043 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
1044 
1045 extern	void	m_copydata __P((struct mbuf *, int, int, caddr_t));
1046 extern	void	m_copyback __P((struct mbuf *, int, int, caddr_t));
1047 
1048 typedef struct mbuf mb_t;
1049 # endif
1050 
1051 typedef	struct uio	uio_t;
1052 typedef	int		ioctlcmd_t;
1053 typedef	int		minor_t;
1054 typedef	unsigned int	u_32_t;
1055 # define	U_32_T	1
1056 
1057 # define OS_RECOGNISED 1
1058 
1059 #endif /* SunOS 4 */
1060 
1061 /* ----------------------------------------------------------------------- */
1062 /*                            L I N U X                                    */
1063 /* ----------------------------------------------------------------------- */
1064 #if defined(linux) && !defined(OS_RECOGNISED)
1065 #include <linux/config.h>
1066 #include <linux/version.h>
1067 # if LINUX >= 20600
1068 #  define	 HDR_T_PRIVATE	1
1069 # endif
1070 # undef USE_INET6
1071 # ifdef USE_INET6
1072 struct ip6_ext {
1073 	u_char	ip6e_nxt;
1074 	u_char	ip6e_len;
1075 };
1076 # endif
1077 
1078 # ifdef _KERNEL
1079 #  define	IPF_PANIC(x,y)	if (x) { printf y; panic("ipf_panic"); }
1080 #  define	BCOPYIN(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
1081 #  define	BCOPYOUT(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
1082 #  define	COPYIN(a,b,c)	copy_from_user((caddr_t)(b), (caddr_t)(a), (c))
1083 #  define	COPYOUT(a,b,c)	copy_to_user((caddr_t)(b), (caddr_t)(a), (c))
1084 #  define	FREE_MB_T(m)	kfree_skb(m)
1085 #  define	GETKTIME(x)	do_gettimeofday((struct timeval *)x)
1086 #  define	SLEEP(x,s)	0, interruptible_sleep_on(x##_linux)
1087 #  define	WAKEUP(x,y)	wake_up(x##_linux + y)
1088 #  define	UIOMOVE(a,b,c,d)	uiomove(a,b,c,d)
1089 #  define	USE_MUTEXES
1090 #  define	KRWLOCK_T		rwlock_t
1091 #  define	KMUTEX_T		spinlock_t
1092 #  define	MUTEX_INIT(x,y)		spin_lock_init(&(x)->ipf_lk)
1093 #  define	MUTEX_ENTER(x)		spin_lock(&(x)->ipf_lk)
1094 #  define	MUTEX_EXIT(x)		spin_unlock(&(x)->ipf_lk)
1095 #  define	MUTEX_DESTROY(x)	do { } while (0)
1096 #  define	MUTEX_NUKE(x)		bzero(&(x)->ipf_lk, sizeof((x)->ipf_lk))
1097 #  define	READ_ENTER(x)		ipf_read_enter(x)
1098 #  define	WRITE_ENTER(x)		ipf_write_enter(x)
1099 #  define	RWLOCK_INIT(x,y)	rwlock_init(&(x)->ipf_lk)
1100 #  define	RW_DESTROY(x)		do { } while (0)
1101 #  define	RWLOCK_EXIT(x)		ipf_rw_exit(x)
1102 #  define	MUTEX_DOWNGRADE(x)	ipf_rw_downgrade(x)
1103 #  define	ATOMIC_INCL(x)		MUTEX_ENTER(&ipf_rw); (x)++; \
1104 					MUTEX_EXIT(&ipf_rw)
1105 #  define	ATOMIC_DECL(x)		MUTEX_ENTER(&ipf_rw); (x)--; \
1106 					MUTEX_EXIT(&ipf_rw)
1107 #  define	ATOMIC_INC64(x)		MUTEX_ENTER(&ipf_rw); (x)++; \
1108 					MUTEX_EXIT(&ipf_rw)
1109 #  define	ATOMIC_INC32(x)		MUTEX_ENTER(&ipf_rw); (x)++; \
1110 					MUTEX_EXIT(&ipf_rw)
1111 #  define	ATOMIC_INC16(x)		MUTEX_ENTER(&ipf_rw); (x)++; \
1112 					MUTEX_EXIT(&ipf_rw)
1113 #  define	ATOMIC_DEC64(x)		MUTEX_ENTER(&ipf_rw); (x)--; \
1114 					MUTEX_EXIT(&ipf_rw)
1115 #  define	ATOMIC_DEC32(x)		MUTEX_ENTER(&ipf_rw); (x)--; \
1116 					MUTEX_EXIT(&ipf_rw)
1117 #  define	ATOMIC_DEC16(x)		MUTEX_ENTER(&ipf_rw); (x)--; \
1118 					MUTEX_EXIT(&ipf_rw)
1119 #  define	SPL_IMP(x)		do { } while (0)
1120 #  define	SPL_NET(x)		do { } while (0)
1121 #  define	SPL_X(x)		do { } while (0)
1122 #  define	IFNAME(x)		((struct net_device*)x)->name
1123 #  define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
1124 			  ((struct net_device *)fin->fin_ifp)->ifindex) & 7)
1125 typedef	struct	sk_buff	mb_t;
1126 extern	void	m_copydata __P((mb_t *, int, int, caddr_t));
1127 extern	void	m_copyback __P((mb_t *, int, int, caddr_t));
1128 extern	void	m_adj __P((mb_t *, int));
1129 extern	mb_t	*m_pullup __P((mb_t *, int));
1130 #  define	mbuf	sk_buff
1131 
1132 #  define	mtod(m, t)	((t)(m)->data)
1133 #  define	m_len		len
1134 #  define	m_next		next
1135 #  define	M_DUPLICATE(m)	skb_clone((m), in_interrupt() ? GFP_ATOMIC : \
1136 								GFP_KERNEL)
1137 #  define	MSGDSIZE(m)	(m)->len
1138 #  define	M_LEN(m)	(m)->len
1139 
1140 #  define	splnet(x)	;
1141 #  define	printf		printk
1142 #  define	bcopy(s,d,z)	memmove(d, s, z)
1143 #  define	bzero(s,z)	memset(s, 0, z)
1144 #  define	bcmp(a,b,z)	memcmp(a, b, z)
1145 
1146 #  define	ifnet		net_device
1147 #  define	if_xname	name
1148 #  define	if_unit		ifindex
1149 
1150 #  define	KMALLOC(x,t)	(x) = (t)kmalloc(sizeof(*(x)), \
1151 				    in_interrupt() ? GFP_ATOMIC : GFP_KERNEL)
1152 #  define	KFREE(x)	kfree(x)
1153 #  define	KMALLOCS(x,t,s)	(x) = (t)kmalloc((s), \
1154 				    in_interrupt() ? GFP_ATOMIC : GFP_KERNEL)
1155 #  define	KFREES(x,s)	kfree(x)
1156 
1157 #  define GETIFP(n,v)	dev_get_by_name(n)
1158 
1159 # else
1160 #  include <net/ethernet.h>
1161 
1162 struct mbuf {
1163 };
1164 
1165 #  ifndef _NET_ROUTE_H
1166 struct rtentry {
1167 };
1168 #  endif
1169 
1170 struct ifnet {
1171 	char	if_xname[IFNAMSIZ];
1172 	int	if_unit;
1173 	int	(* if_output) __P((struct ifnet *, struct mbuf *, struct sockaddr *, struct rtentry *));
1174 	struct	ifaddr	*if_addrlist;
1175 };
1176 # define	IFNAME(x)	((struct ifnet *)x)->if_xname
1177 
1178 # endif	/* _KERNEL */
1179 
1180 # define	COPYIFNAME(x, b) \
1181 				(void) strncpy(b, \
1182 					       ((struct ifnet *)x)->if_xname, \
1183 					       LIFNAMSIZ)
1184 
1185 # include <linux/fs.h>
1186 # define	FWRITE	FMODE_WRITE
1187 # define	FREAD	FMODE_READ
1188 
1189 # define	__USE_MISC	1
1190 # define	__FAVOR_BSD	1
1191 
1192 typedef	struct uio {
1193 	struct iovec	*uio_iov;
1194 	void	*uio_file;
1195 	char	*uio_buf;
1196 	int	uio_iovcnt;
1197 	int	uio_offset;
1198 	size_t	uio_resid;
1199 	int	uio_rw;
1200 } uio_t;
1201 
1202 extern	int	uiomove __P((caddr_t, size_t, int, struct uio *));
1203 
1204 # define	UIO_READ	1
1205 # define	UIO_WRITE	2
1206 
1207 typedef	u_long		ioctlcmd_t;
1208 typedef	int		minor_t;
1209 typedef u_int32_t 	u_32_t;
1210 # define	U_32_T	1
1211 
1212 # define OS_RECOGNISED 1
1213 
1214 #endif
1215 
1216 
1217 /* ----------------------------------------------------------------------- */
1218 /*                                    A I X                                */
1219 /* ----------------------------------------------------------------------- */
1220 #if defined(_AIX51)
1221 # undef		MENTAT
1222 
1223 # include <sys/lock.h>
1224 # include <sys/sysmacros.h>
1225 
1226 # ifdef _KERNEL
1227 #  define rw_read_locked(x)		0
1228 #  include <net/net_globals.h>
1229 #  include <net/net_malloc.h>
1230 #  define	KMUTEX_T		simple_lock_t
1231 #  define	KRWLOCK_T		complex_lock_t
1232 #  define	USE_MUTEXES		1
1233 #  define	USE_SPL			1
1234 #  define	READ_ENTER(x)		lock_read((x)->ipf_lk)
1235 #  define	WRITE_ENTER(x)		lock_write((x)->ipf_lk)
1236 #  define	MUTEX_DOWNGRADE(x)	lock_write_to_read((x)->ipf_lk)
1237 #  define	RWLOCK_INIT(x, y)	lock_alloc(&(x)->ipf_lk, \
1238 						   LOCK_ALLOC_PIN, \
1239 						   (u_short)y, 0); \
1240 					lock_init((x)->ipf_lk, TRUE)
1241 #  define	RWLOCK_EXIT(x)		lock_done((x)->ipf_lk)
1242 #  define	RW_DESTROY(x)		lock_free(&(x)->ipf_lk)
1243 #  define	MUTEX_ENTER(x)		simple_lock((x)->ipf_lk)
1244 #  define	MUTEX_INIT(x, y)	lock_alloc(&(x)->ipf_lk, \
1245 						   LOCK_ALLOC_PIN, \
1246 						   (u_short)y, 0); \
1247 					simple_lock_init((x)->ipf_lk)
1248 #  define	MUTEX_DESTROY(x)	lock_free(&(x)->ipf_lk)
1249 #  define	MUTEX_EXIT(x)		simple_unlock((x)->ipf_lk)
1250 #  define	MUTEX_NUKE(x)		bzero(&(x)->ipf_lk, sizeof((x)->ipf_lk))
1251 #   define	ATOMIC_INC64(x)		{ MUTEX_ENTER(&ipf_rw); (x)++; \
1252 					  MUTEX_EXIT(&ipf_rw); }
1253 #   define	ATOMIC_DEC64(x)		{ MUTEX_ENTER(&ipf_rw); (x)--; \
1254 					  MUTEX_EXIT(&ipf_rw); }
1255 #   define	ATOMIC_INC32(x)		{ MUTEX_ENTER(&ipf_rw); (x)++; \
1256 					  MUTEX_EXIT(&ipf_rw); }
1257 #   define	ATOMIC_DEC32(x)		{ MUTEX_ENTER(&ipf_rw); (x)--; \
1258 					  MUTEX_EXIT(&ipf_rw); }
1259 #   define	ATOMIC_INCL(x)		{ MUTEX_ENTER(&ipf_rw); (x)++; \
1260 					  MUTEX_EXIT(&ipf_rw); }
1261 #   define	ATOMIC_DECL(x)		{ MUTEX_ENTER(&ipf_rw); (x)--; \
1262 					  MUTEX_EXIT(&ipf_rw); }
1263 #   define	ATOMIC_INC(x)		{ MUTEX_ENTER(&ipf_rw); (x)++; \
1264 					  MUTEX_EXIT(&ipf_rw); }
1265 #   define	ATOMIC_DEC(x)		{ MUTEX_ENTER(&ipf_rw); (x)--; \
1266 					  MUTEX_EXIT(&ipf_rw); }
1267 #  define	SPL_NET(x)		x = splnet()
1268 #  define	SPL_IMP(x)		x = splimp()
1269 #  undef	SPL_X
1270 #  define	SPL_X(x)		splx(x)
1271 #  define	UIOMOVE(a,b,c,d)	uiomove((caddr_t)a,b,c,d)
1272 extern void* getifp __P((char *, int));
1273 #  define	GETIFP(n, v)		getifp(n, v)
1274 #  define	GET_MINOR		minor
1275 #  define	SLEEP(id, n)	sleepx((id), PZERO+1, 0)
1276 #  define	WAKEUP(id,x)	wakeup(id)
1277 #  define	COPYIN(a,b,c)	copyin((caddr_t)(a), (caddr_t)(b), (c))
1278 #  define	COPYOUT(a,b,c)	copyout((caddr_t)(a), (caddr_t)(b), (c))
1279 #  define	BCOPYIN(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
1280 #  define	BCOPYOUT(a,b,c)	bcopy((caddr_t)(a), (caddr_t)(b), (c))
1281 #  define	KMALLOC(a, b)	MALLOC((a), b, sizeof(*(a)), M_TEMP, M_NOWAIT)
1282 #  define	KMALLOCS(a, b, c)	MALLOC((a), b, (c), M_TEMP, \
1283 					    ((c) > 4096) ? M_WAITOK : M_NOWAIT)
1284 #  define	KFREE(x)	FREE((x), M_TEMP)
1285 #  define	KFREES(x,s)	FREE((x), M_TEMP)
1286 #  define	MSGDSIZE(x)	mbufchainlen(x)
1287 #  define	M_LEN(x)	(x)->m_len
1288 #  define	M_DUPLICATE(x)	m_copy((x), 0, M_COPYALL)
1289 #  define	GETKTIME(x)
1290 #  define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
1291 				  ((struct ifnet *)fin->fin_ifp)->if_unit) & 7)
1292 #  define	IPF_PANIC(x,y)
1293 typedef struct mbuf mb_t;
1294 # endif /* _KERNEL */
1295 
1296 /*
1297  * These are from's Solaris' #defines for little endian.
1298  */
1299 #if !defined(IP6F_MORE_FRAG)
1300 # define	IP6F_MORE_FRAG		0x0100
1301 #endif
1302 #if !defined(IP6F_RESERVED_MASK)
1303 # define	IP6F_RESERVED_MASK	0x0600
1304 #endif
1305 #if !defined(IP6F_OFF_MASK)
1306 # define	IP6F_OFF_MASK		0xf8ff
1307 #endif
1308 
1309 struct ip6_ext {
1310 	u_char	ip6e_nxt;
1311 	u_char	ip6e_len;
1312 };
1313 
1314 typedef	int		ioctlcmd_t;
1315 typedef	int		minor_t;
1316 /*
1317  * Really, any arch where sizeof(long) != sizeof(int).
1318  */
1319 typedef unsigned int    u_32_t;
1320 # define	U_32_T	1
1321 
1322 # define OS_RECOGNISED 1
1323 #endif	/* _AIX51 */
1324 
1325 
1326 #ifndef	OS_RECOGNISED
1327 #error	ip_compat.h does not recognise this platform/OS.
1328 #endif
1329 
1330 
1331 /* ----------------------------------------------------------------------- */
1332 /*                           G E N E R I C                                 */
1333 /* ----------------------------------------------------------------------- */
1334 #ifndef OS_RECOGNISED
1335 #endif
1336 
1337 /*
1338  * For BSD kernels, if bpf is in the kernel, enable ipfilter to use bpf in
1339  * filter rules.
1340  */
1341 #if !defined(IPFILTER_BPF) && ((NBPF > 0) || (NBPFILTER > 0) || (DEV_BPF > 0))
1342 # define IPFILTER_BPF
1343 #endif
1344 
1345 /*
1346  * Userland locking primitives
1347  */
1348 typedef	struct	{
1349 	char	*eMm_owner;
1350 	char	*eMm_heldin;
1351 	u_int	eMm_magic;
1352 	int	eMm_held;
1353 	int	eMm_heldat;
1354 #ifdef __hpux
1355 	char	eMm_fill[8];
1356 #endif
1357 } eMmutex_t;
1358 
1359 typedef	struct	{
1360 	char	*eMrw_owner;
1361 	char	*eMrw_heldin;
1362 	u_int	eMrw_magic;
1363 	short	eMrw_read;
1364 	short	eMrw_write;
1365 	int	eMrw_heldat;
1366 #ifdef __hpux
1367 	char	eMm_fill[24];
1368 #endif
1369 } eMrwlock_t;
1370 
1371 typedef union {
1372 #ifdef KMUTEX_T
1373 	struct	{
1374 		KMUTEX_T	ipf_slk;
1375 		char		*ipf_lname;
1376 	} ipf_lkun_s;
1377 #endif
1378 	eMmutex_t	ipf_emu;
1379 } ipfmutex_t;
1380 
1381 typedef union {
1382 #ifdef KRWLOCK_T
1383 	struct	{
1384 		KRWLOCK_T	ipf_slk;
1385 		char		*ipf_lname;
1386 		int		ipf_sr;
1387 		int		ipf_sw;
1388 		u_int		ipf_magic;
1389 	} ipf_lkun_s;
1390 #endif
1391 	eMrwlock_t	ipf_emu;
1392 } ipfrwlock_t;
1393 
1394 #define	ipf_lk		ipf_lkun_s.ipf_slk
1395 #define	ipf_lname	ipf_lkun_s.ipf_lname
1396 #define	ipf_isr		ipf_lkun_s.ipf_sr
1397 #define	ipf_isw		ipf_lkun_s.ipf_sw
1398 #define	ipf_magic	ipf_lkun_s.ipf_magic
1399 
1400 #if !defined(__GNUC__) || \
1401     (defined(__FreeBSD_version) && (__FreeBSD_version >= 503000))
1402 # ifndef	INLINE
1403 #  define	INLINE
1404 # endif
1405 #else
1406 # define	INLINE	__inline__
1407 #endif
1408 
1409 #if defined(linux) && defined(_KERNEL)
1410 extern	INLINE	void	ipf_read_enter __P((ipfrwlock_t *));
1411 extern	INLINE	void	ipf_write_enter __P((ipfrwlock_t *));
1412 extern	INLINE	void	ipf_rw_exit __P((ipfrwlock_t *));
1413 extern	INLINE	void	ipf_rw_downgrade __P((ipfrwlock_t *));
1414 #endif
1415 
1416 /*
1417  * In a non-kernel environment, there are a lot of macros that need to be
1418  * filled in to be null-ops or to point to some compatibility function,
1419  * somewhere in userland.
1420  */
1421 #ifndef _KERNEL
1422 typedef	struct	mb_s	{
1423 	struct	mb_s	*mb_next;
1424 	int		mb_len;
1425 	u_long		mb_buf[2048];
1426 } mb_t;
1427 # undef		m_next
1428 # define	m_next		mb_next
1429 # define	MSGDSIZE(x)	(x)->mb_len	/* XXX - from ipt.c */
1430 # define	M_LEN(x)	(x)->mb_len
1431 # define	M_DUPLICATE(x)	(x)
1432 # define	GETKTIME(x)	gettimeofday((struct timeval *)(x), NULL)
1433 # undef		MTOD
1434 # define	MTOD(m, t)	((t)(m)->mb_buf)
1435 # define	FREE_MB_T(x)
1436 # define	SLEEP(x,y)	1;
1437 # define	WAKEUP(x,y)	;
1438 # define	IPF_PANIC(x,y)	;
1439 # define	PANIC(x,y)	;
1440 # define	SPL_NET(x)	;
1441 # define	SPL_IMP(x)	;
1442 # define	SPL_X(x)	;
1443 # define	KMALLOC(a,b)	(a) = (b)malloc(sizeof(*a))
1444 # define	KMALLOCS(a,b,c)	(a) = (b)malloc(c)
1445 # define	KFREE(x)	free(x)
1446 # define	KFREES(x,s)	free(x)
1447 # define	GETIFP(x, v)	get_unit(x,v)
1448 # define	COPYIN(a,b,c)	(bcopy((a), (b), (c)), 0)
1449 # define	COPYOUT(a,b,c)	(bcopy((a), (b), (c)), 0)
1450 # define	BCOPYIN(a,b,c)	(bcopy((a), (b), (c)), 0)
1451 # define	BCOPYOUT(a,b,c)	(bcopy((a), (b), (c)), 0)
1452 # define	COPYDATA(m, o, l, b)	bcopy(MTOD((mb_t *)m, char *) + (o), \
1453 					      (b), (l))
1454 # define	COPYBACK(m, o, l, b)	bcopy((b), \
1455 					      MTOD((mb_t *)m, char *) + (o), \
1456 					      (l))
1457 # define	UIOMOVE(a,b,c,d)	ipfuiomove(a,b,c,d)
1458 extern	void	m_copydata __P((mb_t *, int, int, caddr_t));
1459 extern	int	ipfuiomove __P((caddr_t, int, int, struct uio *));
1460 # ifndef CACHE_HASH
1461 #  define	CACHE_HASH(x)	((IFNAME(fin->fin_ifp)[0] + \
1462 				  ((struct ifnet *)fin->fin_ifp)->if_unit) & 7)
1463 # endif
1464 
1465 # define	MUTEX_DESTROY(x)	eMmutex_destroy(&(x)->ipf_emu)
1466 # define	MUTEX_ENTER(x)		eMmutex_enter(&(x)->ipf_emu, \
1467 						      __FILE__, __LINE__)
1468 # define	MUTEX_EXIT(x)		eMmutex_exit(&(x)->ipf_emu)
1469 # define	MUTEX_INIT(x,y)		eMmutex_init(&(x)->ipf_emu, y)
1470 # define	MUTEX_NUKE(x)		bzero((x), sizeof(*(x)))
1471 
1472 # define	MUTEX_DOWNGRADE(x)	eMrwlock_downgrade(&(x)->ipf_emu, \
1473 							   __FILE__, __LINE__)
1474 # define	READ_ENTER(x)		eMrwlock_read_enter(&(x)->ipf_emu, \
1475 							    __FILE__, __LINE__)
1476 # define	RWLOCK_INIT(x, y)	eMrwlock_init(&(x)->ipf_emu, y)
1477 # define	RWLOCK_EXIT(x)		eMrwlock_exit(&(x)->ipf_emu)
1478 # define	RW_DESTROY(x)		eMrwlock_destroy(&(x)->ipf_emu)
1479 # define	WRITE_ENTER(x)		eMrwlock_write_enter(&(x)->ipf_emu, \
1480 							     __FILE__, \
1481 							     __LINE__)
1482 
1483 # define	USE_MUTEXES		1
1484 
1485 extern void eMmutex_destroy __P((eMmutex_t *));
1486 extern void eMmutex_enter __P((eMmutex_t *, char *, int));
1487 extern void eMmutex_exit __P((eMmutex_t *));
1488 extern void eMmutex_init __P((eMmutex_t *, char *));
1489 extern void eMrwlock_destroy __P((eMrwlock_t *));
1490 extern void eMrwlock_exit __P((eMrwlock_t *));
1491 extern void eMrwlock_init __P((eMrwlock_t *, char *));
1492 extern void eMrwlock_read_enter __P((eMrwlock_t *, char *, int));
1493 extern void eMrwlock_write_enter __P((eMrwlock_t *, char *, int));
1494 extern void eMrwlock_downgrade __P((eMrwlock_t *, char *, int));
1495 
1496 #endif
1497 
1498 #define	MAX_IPV4HDR	((0xf << 2) + sizeof(struct icmp) + sizeof(ip_t) + 8)
1499 
1500 #ifndef	IP_OFFMASK
1501 # define	IP_OFFMASK	0x1fff
1502 #endif
1503 
1504 
1505 /*
1506  * On BSD's use quad_t as a guarantee for getting at least a 64bit sized
1507  * object.
1508  */
1509 #if	BSD > 199306
1510 # define	USE_QUAD_T
1511 # define	U_QUAD_T	u_quad_t
1512 # define	QUAD_T		quad_t
1513 #else /* BSD > 199306 */
1514 # define	U_QUAD_T	u_long
1515 # define	QUAD_T		long
1516 #endif /* BSD > 199306 */
1517 
1518 
1519 #ifdef	USE_INET6
1520 # if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || \
1521      defined(__osf__) || defined(linux)
1522 #  include <netinet/ip6.h>
1523 #  include <netinet/icmp6.h>
1524 #  if !defined(linux)
1525 #   if defined(_KERNEL) && !defined(__osf__)
1526 #    include <netinet6/ip6_var.h>
1527 #   endif
1528 #  endif
1529 typedef	struct ip6_hdr	ip6_t;
1530 # endif
1531 #endif
1532 
1533 #ifndef	MAX
1534 # define	MAX(a,b)	(((a) > (b)) ? (a) : (b))
1535 #endif
1536 
1537 #if defined(_KERNEL)
1538 # ifdef MENTAT
1539 #  define	COPYDATA	mb_copydata
1540 #  define	COPYBACK	mb_copyback
1541 # else
1542 #  define	COPYDATA	m_copydata
1543 #  define	COPYBACK	m_copyback
1544 # endif
1545 # if (BSD >= 199306) || defined(__FreeBSD__)
1546 #  if (defined(__NetBSD_Version__) && (__NetBSD_Version__ < 105180000)) || \
1547        defined(__FreeBSD__) || (defined(OpenBSD) && (OpenBSD < 200206)) || \
1548        defined(_BSDI_VERSION)
1549 #   include <vm/vm.h>
1550 #  endif
1551 #  if !defined(__FreeBSD__) || (defined (__FreeBSD_version) && \
1552       (__FreeBSD_version >= 300000))
1553 #   if (defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105180000)) || \
1554        (defined(OpenBSD) && (OpenBSD >= 200111))
1555 #    include <uvm/uvm_extern.h>
1556 #   else
1557 #    include <vm/vm_extern.h>
1558 extern  vm_map_t        kmem_map;
1559 #   endif
1560 #   include <sys/proc.h>
1561 #  else /* !__FreeBSD__ || (__FreeBSD__ && __FreeBSD_version >= 300000) */
1562 #   include <vm/vm_kern.h>
1563 #  endif /* !__FreeBSD__ || (__FreeBSD__ && __FreeBSD_version >= 300000) */
1564 
1565 #  ifdef IPFILTER_M_IPFILTER
1566 #    include <sys/malloc.h>
1567 MALLOC_DECLARE(M_IPFILTER);
1568 #    define	_M_IPF		M_IPFILTER
1569 #  else /* IPFILTER_M_IPFILTER */
1570 #   ifdef M_PFIL
1571 #    define	_M_IPF		M_PFIL
1572 #   else
1573 #    ifdef M_IPFILTER
1574 #     define	_M_IPF		M_IPFILTER
1575 #    else
1576 #     define	_M_IPF		M_TEMP
1577 #    endif /* M_IPFILTER */
1578 #   endif /* M_PFIL */
1579 #  endif /* IPFILTER_M_IPFILTER */
1580 #  define	KMALLOC(a, b)	MALLOC((a), b, sizeof(*(a)), _M_IPF, M_NOWAIT)
1581 #  define	KMALLOCS(a, b, c)	MALLOC((a), b, (c), _M_IPF, M_NOWAIT)
1582 #  define	KFREE(x)	FREE((x), _M_IPF)
1583 #  define	KFREES(x,s)	FREE((x), _M_IPF)
1584 #  define	UIOMOVE(a,b,c,d)	uiomove(a,b,d)
1585 #  define	SLEEP(id, n)	tsleep((id), PPAUSE|PCATCH, n, 0)
1586 #  define	WAKEUP(id,x)	wakeup(id+x)
1587 #  define	GETIFP(n, v)	ifunit(n)
1588 # endif /* (Free)BSD */
1589 
1590 # if !defined(USE_MUTEXES) && !defined(SPL_NET)
1591 #  if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199407)) || \
1592       (defined(OpenBSD) && (OpenBSD >= 200006))
1593 #   define	SPL_NET(x)	x = splsoftnet()
1594 #  else
1595 #   define	SPL_IMP(x)	x = splimp()
1596 #   define	SPL_NET(x)	x = splnet()
1597 #  endif /* NetBSD && (NetBSD <= 1991011) && (NetBSD >= 199407) */
1598 #  define	SPL_X(x)	(void) splx(x)
1599 # endif /* !USE_MUTEXES */
1600 
1601 # ifndef FREE_MB_T
1602 #  define	FREE_MB_T(m)	m_freem(m)
1603 # endif
1604 
1605 # ifndef MTOD
1606 #  define	MTOD(m,t)	mtod(m,t)
1607 # endif
1608 
1609 # ifndef COPYIN
1610 #  define	COPYIN(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
1611 #  define	COPYOUT(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
1612 #  define	BCOPYIN(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
1613 #  define	BCOPYOUT(a,b,c)	(bcopy((caddr_t)(a), (caddr_t)(b), (c)), 0)
1614 # endif
1615 
1616 # ifndef KMALLOC
1617 #  define	KMALLOC(a,b)	(a) = (b)new_kmem_alloc(sizeof(*(a)), \
1618 							KMEM_NOSLEEP)
1619 #  define	KMALLOCS(a,b,c)	(a) = (b)new_kmem_alloc((c), KMEM_NOSLEEP)
1620 # endif
1621 
1622 # ifndef	GET_MINOR
1623 #  define	GET_MINOR(x)	minor(x)
1624 # endif
1625 # define	PANIC(x,y)	if (x) panic y
1626 #endif /* _KERNEL */
1627 
1628 #ifndef	IFNAME
1629 # define	IFNAME(x)	((struct ifnet *)x)->if_name
1630 #endif
1631 #ifndef	COPYIFNAME
1632 # define	NEED_FRGETIFNAME
1633 extern	char	*fr_getifname __P((struct ifnet *, char *));
1634 # define	COPYIFNAME(x, b) \
1635 				fr_getifname((struct ifnet *)x, b)
1636 #endif
1637 
1638 #ifndef ASSERT
1639 # define	ASSERT(x)
1640 #endif
1641 
1642 /*
1643  * Because the ctype(3) posix definition, if used "safely" in code everywhere,
1644  * would mean all normal code that walks through strings needed casts.  Yuck.
1645  */
1646 #define	ISALNUM(x)	isalnum((u_char)(x))
1647 #define	ISALPHA(x)	isalpha((u_char)(x))
1648 #define	ISASCII(x)	isascii((u_char)(x))
1649 #define	ISDIGIT(x)	isdigit((u_char)(x))
1650 #define	ISPRINT(x)	isprint((u_char)(x))
1651 #define	ISSPACE(x)	isspace((u_char)(x))
1652 #define	ISUPPER(x)	isupper((u_char)(x))
1653 #define	ISXDIGIT(x)	isxdigit((u_char)(x))
1654 #define	ISLOWER(x)	islower((u_char)(x))
1655 #define	TOUPPER(x)	toupper((u_char)(x))
1656 #define	TOLOWER(x)	tolower((u_char)(x))
1657 
1658 /*
1659  * If mutexes aren't being used, turn all the mutex functions into null-ops.
1660  */
1661 #if !defined(USE_MUTEXES)
1662 # define	USE_SPL			1
1663 # undef		RW_DESTROY
1664 # undef		MUTEX_INIT
1665 # undef		MUTEX_NUKE
1666 # undef		MUTEX_DESTROY
1667 # define	MUTEX_ENTER(x)		;
1668 # define	READ_ENTER(x)		;
1669 # define	WRITE_ENTER(x)		;
1670 # define	MUTEX_DOWNGRADE(x)	;
1671 # define	RWLOCK_INIT(x, y)	;
1672 # define	RWLOCK_EXIT(x)		;
1673 # define	RW_DESTROY(x)		;
1674 # define	MUTEX_EXIT(x)		;
1675 # define	MUTEX_INIT(x,y)		;
1676 # define	MUTEX_DESTROY(x)	;
1677 # define	MUTEX_NUKE(x)		;
1678 #endif /* !USE_MUTEXES */
1679 #ifndef	ATOMIC_INC
1680 # define	ATOMIC_INC(x)		(x)++
1681 # define	ATOMIC_DEC(x)		(x)--
1682 #endif
1683 
1684 #if defined(USE_SPL) && defined(_KERNEL)
1685 # define	SPL_INT(x)	int x
1686 #else
1687 # define	SPL_INT(x)
1688 #endif
1689 
1690 /*
1691  * If there are no atomic operations for bit sizes defined, define them to all
1692  * use a generic one that works for all sizes.
1693  */
1694 #ifndef	ATOMIC_INCL
1695 # define	ATOMIC_INCL		ATOMIC_INC
1696 # define	ATOMIC_INC64		ATOMIC_INC
1697 # define	ATOMIC_INC32		ATOMIC_INC
1698 # define	ATOMIC_INC16		ATOMIC_INC
1699 # define	ATOMIC_DECL		ATOMIC_DEC
1700 # define	ATOMIC_DEC64		ATOMIC_DEC
1701 # define	ATOMIC_DEC32		ATOMIC_DEC
1702 # define	ATOMIC_DEC16		ATOMIC_DEC
1703 #endif
1704 
1705 #ifndef HDR_T_PRIVATE
1706 typedef	struct	tcphdr	tcphdr_t;
1707 typedef	struct	udphdr	udphdr_t;
1708 #endif
1709 typedef	struct	icmp	icmphdr_t;
1710 typedef	struct	ip	ip_t;
1711 typedef	struct	ether_header	ether_header_t;
1712 typedef	struct	tcpiphdr	tcpiphdr_t;
1713 
1714 #ifndef	FR_GROUPLEN
1715 # define	FR_GROUPLEN	16
1716 #endif
1717 
1718 #ifdef offsetof
1719 # undef	offsetof
1720 #endif
1721 #ifndef offsetof
1722 # define offsetof(t,m) (int)((&((t *)0L)->m))
1723 #endif
1724 
1725 /*
1726  * This set of macros has been brought about because on Tru64 it is not
1727  * possible to easily assign or examine values in a structure that are
1728  * bit fields.
1729  */
1730 #ifndef IP_V
1731 # define	IP_V(x)		(x)->ip_v
1732 #endif
1733 #ifndef	IP_V_A
1734 # define	IP_V_A(x,y)	(x)->ip_v = (y)
1735 #endif
1736 #ifndef	IP_HL
1737 # define	IP_HL(x)	(x)->ip_hl
1738 #endif
1739 #ifndef	IP_HL_A
1740 # define	IP_HL_A(x,y)	(x)->ip_hl = (y)
1741 #endif
1742 #ifndef	TCP_X2
1743 # define	TCP_X2(x)	(x)->th_x2
1744 #endif
1745 #ifndef	TCP_X2_A
1746 # define	TCP_X2_A(x,y)	(x)->th_x2 = (y)
1747 #endif
1748 #ifndef	TCP_OFF
1749 # define	TCP_OFF(x)	(x)->th_off
1750 #endif
1751 #ifndef	TCP_OFF_A
1752 # define	TCP_OFF_A(x,y)	(x)->th_off = (y)
1753 #endif
1754 #define	IPMINLEN(i, h)	((i)->ip_len >= (IP_HL(i) * 4 + sizeof(struct h)))
1755 
1756 
1757 /*
1758  * XXX - This is one of those *awful* hacks which nobody likes
1759  */
1760 #ifdef	ultrix
1761 #define	A_A
1762 #else
1763 #define	A_A	&
1764 #endif
1765 
1766 #define	TCPF_ALL	(TH_FIN|TH_SYN|TH_RST|TH_PUSH|TH_ACK|TH_URG|\
1767 			 TH_ECN|TH_CWR)
1768 
1769 #if (BSD >= 199306) && !defined(m_act)
1770 # define	m_act	m_nextpkt
1771 #endif
1772 
1773 /*
1774  * Security Options for Intenet Protocol (IPSO) as defined in RFC 1108.
1775  *
1776  * Basic Option
1777  *
1778  * 00000001   -   (Reserved 4)
1779  * 00111101   -   Top Secret
1780  * 01011010   -   Secret
1781  * 10010110   -   Confidential
1782  * 01100110   -   (Reserved 3)
1783  * 11001100   -   (Reserved 2)
1784  * 10101011   -   Unclassified
1785  * 11110001   -   (Reserved 1)
1786  */
1787 #define	IPSO_CLASS_RES4		0x01
1788 #define	IPSO_CLASS_TOPS		0x3d
1789 #define	IPSO_CLASS_SECR		0x5a
1790 #define	IPSO_CLASS_CONF		0x96
1791 #define	IPSO_CLASS_RES3		0x66
1792 #define	IPSO_CLASS_RES2		0xcc
1793 #define	IPSO_CLASS_UNCL		0xab
1794 #define	IPSO_CLASS_RES1		0xf1
1795 
1796 #define	IPSO_AUTH_GENSER	0x80
1797 #define	IPSO_AUTH_ESI		0x40
1798 #define	IPSO_AUTH_SCI		0x20
1799 #define	IPSO_AUTH_NSA		0x10
1800 #define	IPSO_AUTH_DOE		0x08
1801 #define	IPSO_AUTH_UN		0x06
1802 #define	IPSO_AUTH_FTE		0x01
1803 
1804 /*
1805  * IP option #defines
1806  */
1807 #undef	IPOPT_RR
1808 #define	IPOPT_RR	7
1809 #undef	IPOPT_ZSU
1810 #define	IPOPT_ZSU	10	/* ZSU */
1811 #undef	IPOPT_MTUP
1812 #define	IPOPT_MTUP	11	/* MTUP */
1813 #undef	IPOPT_MTUR
1814 #define	IPOPT_MTUR	12	/* MTUR */
1815 #undef	IPOPT_ENCODE
1816 #define	IPOPT_ENCODE	15	/* ENCODE */
1817 #undef	IPOPT_TS
1818 #define	IPOPT_TS	68
1819 #undef	IPOPT_TR
1820 #define	IPOPT_TR	82	/* TR */
1821 #undef	IPOPT_SECURITY
1822 #define	IPOPT_SECURITY	130
1823 #undef	IPOPT_LSRR
1824 #define	IPOPT_LSRR	131
1825 #undef	IPOPT_E_SEC
1826 #define	IPOPT_E_SEC	133	/* E-SEC */
1827 #undef	IPOPT_CIPSO
1828 #define	IPOPT_CIPSO	134	/* CIPSO */
1829 #undef	IPOPT_SATID
1830 #define	IPOPT_SATID	136
1831 #ifndef	IPOPT_SID
1832 # define	IPOPT_SID	IPOPT_SATID
1833 #endif
1834 #undef	IPOPT_SSRR
1835 #define	IPOPT_SSRR	137
1836 #undef	IPOPT_ADDEXT
1837 #define	IPOPT_ADDEXT	147	/* ADDEXT */
1838 #undef	IPOPT_VISA
1839 #define	IPOPT_VISA	142	/* VISA */
1840 #undef	IPOPT_IMITD
1841 #define	IPOPT_IMITD	144	/* IMITD */
1842 #undef	IPOPT_EIP
1843 #define	IPOPT_EIP	145	/* EIP */
1844 #undef	IPOPT_RTRALRT
1845 #define	IPOPT_RTRALRT	148	/* RTRALRT */
1846 #undef	IPOPT_SDB
1847 #define	IPOPT_SDB	149
1848 #undef	IPOPT_NSAPA
1849 #define	IPOPT_NSAPA	150
1850 #undef	IPOPT_DPS
1851 #define	IPOPT_DPS	151
1852 #undef	IPOPT_UMP
1853 #define	IPOPT_UMP	152
1854 #undef	IPOPT_FINN
1855 #define	IPOPT_FINN	205	/* FINN */
1856 
1857 #ifndef TCPOPT_EOL
1858 # define TCPOPT_EOL		0
1859 #endif
1860 #ifndef TCPOPT_NOP
1861 # define TCPOPT_NOP		1
1862 #endif
1863 #ifndef TCPOPT_MAXSEG
1864 # define TCPOPT_MAXSEG		2
1865 #endif
1866 #ifndef TCPOLEN_MAXSEG
1867 # define TCPOLEN_MAXSEG		4
1868 #endif
1869 #ifndef TCPOPT_WINDOW
1870 # define TCPOPT_WINDOW		3
1871 #endif
1872 #ifndef TCPOLEN_WINDOW
1873 # define TCPOLEN_WINDOW		3
1874 #endif
1875 #ifndef TCPOPT_SACK_PERMITTED
1876 # define TCPOPT_SACK_PERMITTED	4
1877 #endif
1878 #ifndef TCPOLEN_SACK_PERMITTED
1879 # define TCPOLEN_SACK_PERMITTED	2
1880 #endif
1881 #ifndef TCPOPT_SACK
1882 # define TCPOPT_SACK		5
1883 #endif
1884 #ifndef TCPOPT_TIMESTAMP
1885 # define TCPOPT_TIMESTAMP	8
1886 #endif
1887 
1888 #ifndef	ICMP_MINLEN
1889 # define	ICMP_MINLEN	8
1890 #endif
1891 #ifndef	ICMP_ECHOREPLY
1892 # define	ICMP_ECHOREPLY	0
1893 #endif
1894 #ifndef	ICMP_UNREACH
1895 # define	ICMP_UNREACH	3
1896 #endif
1897 #ifndef	ICMP_UNREACH_NET
1898 # define	ICMP_UNREACH_NET	0
1899 #endif
1900 #ifndef	ICMP_UNREACH_HOST
1901 # define	ICMP_UNREACH_HOST	1
1902 #endif
1903 #ifndef	ICMP_UNREACH_PROTOCOL
1904 # define	ICMP_UNREACH_PROTOCOL	2
1905 #endif
1906 #ifndef	ICMP_UNREACH_PORT
1907 # define	ICMP_UNREACH_PORT	3
1908 #endif
1909 #ifndef	ICMP_UNREACH_NEEDFRAG
1910 # define	ICMP_UNREACH_NEEDFRAG	4
1911 #endif
1912 #ifndef	ICMP_UNREACH_SRCFAIL
1913 # define	ICMP_UNREACH_SRCFAIL	5
1914 #endif
1915 #ifndef	ICMP_UNREACH_NET_UNKNOWN
1916 # define	ICMP_UNREACH_NET_UNKNOWN	6
1917 #endif
1918 #ifndef	ICMP_UNREACH_HOST_UNKNOWN
1919 # define	ICMP_UNREACH_HOST_UNKNOWN	7
1920 #endif
1921 #ifndef	ICMP_UNREACH_ISOLATED
1922 # define	ICMP_UNREACH_ISOLATED	8
1923 #endif
1924 #ifndef	ICMP_UNREACH_NET_PROHIB
1925 # define	ICMP_UNREACH_NET_PROHIB	9
1926 #endif
1927 #ifndef	ICMP_UNREACH_HOST_PROHIB
1928 # define	ICMP_UNREACH_HOST_PROHIB	10
1929 #endif
1930 #ifndef	ICMP_UNREACH_TOSNET
1931 # define	ICMP_UNREACH_TOSNET	11
1932 #endif
1933 #ifndef	ICMP_UNREACH_TOSHOST
1934 # define	ICMP_UNREACH_TOSHOST	12
1935 #endif
1936 #ifndef	ICMP_UNREACH_ADMIN_PROHIBIT
1937 # define	ICMP_UNREACH_ADMIN_PROHIBIT	13
1938 #endif
1939 #ifndef	ICMP_UNREACH_FILTER
1940 # define	ICMP_UNREACH_FILTER	13
1941 #endif
1942 #ifndef	ICMP_UNREACH_HOST_PRECEDENCE
1943 # define	ICMP_UNREACH_HOST_PRECEDENCE	14
1944 #endif
1945 #ifndef	ICMP_UNREACH_PRECEDENCE_CUTOFF
1946 # define	ICMP_UNREACH_PRECEDENCE_CUTOFF	15
1947 #endif
1948 #ifndef	ICMP_SOURCEQUENCH
1949 # define	ICMP_SOURCEQUENCH	4
1950 #endif
1951 #ifndef	ICMP_REDIRECT_NET
1952 # define	ICMP_REDIRECT_NET	0
1953 #endif
1954 #ifndef	ICMP_REDIRECT_HOST
1955 # define	ICMP_REDIRECT_HOST	1
1956 #endif
1957 #ifndef	ICMP_REDIRECT_TOSNET
1958 # define	ICMP_REDIRECT_TOSNET	2
1959 #endif
1960 #ifndef	ICMP_REDIRECT_TOSHOST
1961 # define	ICMP_REDIRECT_TOSHOST	3
1962 #endif
1963 #ifndef	ICMP_ALTHOSTADDR
1964 # define	ICMP_ALTHOSTADDR	6
1965 #endif
1966 #ifndef	ICMP_TIMXCEED
1967 # define	ICMP_TIMXCEED	11
1968 #endif
1969 #ifndef	ICMP_TIMXCEED_INTRANS
1970 # define	ICMP_TIMXCEED_INTRANS	0
1971 #endif
1972 #ifndef	ICMP_TIMXCEED_REASS
1973 # define		ICMP_TIMXCEED_REASS	1
1974 #endif
1975 #ifndef	ICMP_PARAMPROB
1976 # define	ICMP_PARAMPROB	12
1977 #endif
1978 #ifndef	ICMP_PARAMPROB_ERRATPTR
1979 # define	ICMP_PARAMPROB_ERRATPTR	0
1980 #endif
1981 #ifndef	ICMP_PARAMPROB_OPTABSENT
1982 # define	ICMP_PARAMPROB_OPTABSENT	1
1983 #endif
1984 #ifndef	ICMP_PARAMPROB_LENGTH
1985 # define	ICMP_PARAMPROB_LENGTH	2
1986 #endif
1987 #ifndef ICMP_TSTAMP
1988 # define	ICMP_TSTAMP	13
1989 #endif
1990 #ifndef ICMP_TSTAMPREPLY
1991 # define	ICMP_TSTAMPREPLY	14
1992 #endif
1993 #ifndef ICMP_IREQ
1994 # define	ICMP_IREQ	15
1995 #endif
1996 #ifndef ICMP_IREQREPLY
1997 # define	ICMP_IREQREPLY	16
1998 #endif
1999 #ifndef	ICMP_MASKREQ
2000 # define	ICMP_MASKREQ	17
2001 #endif
2002 #ifndef ICMP_MASKREPLY
2003 # define	ICMP_MASKREPLY	18
2004 #endif
2005 #ifndef	ICMP_TRACEROUTE
2006 # define	ICMP_TRACEROUTE	30
2007 #endif
2008 #ifndef	ICMP_DATACONVERR
2009 # define	ICMP_DATACONVERR	31
2010 #endif
2011 #ifndef	ICMP_MOBILE_REDIRECT
2012 # define	ICMP_MOBILE_REDIRECT	32
2013 #endif
2014 #ifndef	ICMP_IPV6_WHEREAREYOU
2015 # define	ICMP_IPV6_WHEREAREYOU	33
2016 #endif
2017 #ifndef	ICMP_IPV6_IAMHERE
2018 # define	ICMP_IPV6_IAMHERE	34
2019 #endif
2020 #ifndef	ICMP_MOBILE_REGREQUEST
2021 # define	ICMP_MOBILE_REGREQUEST	35
2022 #endif
2023 #ifndef	ICMP_MOBILE_REGREPLY
2024 # define	ICMP_MOBILE_REGREPLY	36
2025 #endif
2026 #ifndef	ICMP_SKIP
2027 # define	ICMP_SKIP	39
2028 #endif
2029 #ifndef	ICMP_PHOTURIS
2030 # define	ICMP_PHOTURIS	40
2031 #endif
2032 #ifndef	ICMP_PHOTURIS_UNKNOWN_INDEX
2033 # define	ICMP_PHOTURIS_UNKNOWN_INDEX	1
2034 #endif
2035 #ifndef	ICMP_PHOTURIS_AUTH_FAILED
2036 # define	ICMP_PHOTURIS_AUTH_FAILED	2
2037 #endif
2038 #ifndef	ICMP_PHOTURIS_DECRYPT_FAILED
2039 # define	ICMP_PHOTURIS_DECRYPT_FAILED	3
2040 #endif
2041 #ifndef	IPVERSION
2042 # define	IPVERSION	4
2043 #endif
2044 #ifndef	IPOPT_MINOFF
2045 # define	IPOPT_MINOFF	4
2046 #endif
2047 #ifndef	IPOPT_COPIED
2048 # define	IPOPT_COPIED(x)	((x)&0x80)
2049 #endif
2050 #ifndef	IPOPT_EOL
2051 # define	IPOPT_EOL	0
2052 #endif
2053 #ifndef	IPOPT_NOP
2054 # define	IPOPT_NOP	1
2055 #endif
2056 #ifndef	IP_MF
2057 # define	IP_MF	((u_short)0x2000)
2058 #endif
2059 #ifndef	ETHERTYPE_IP
2060 # define	ETHERTYPE_IP	((u_short)0x0800)
2061 #endif
2062 #ifndef	TH_FIN
2063 # define	TH_FIN	0x01
2064 #endif
2065 #ifndef	TH_SYN
2066 # define	TH_SYN	0x02
2067 #endif
2068 #ifndef	TH_RST
2069 # define	TH_RST	0x04
2070 #endif
2071 #ifndef	TH_PUSH
2072 # define	TH_PUSH	0x08
2073 #endif
2074 #ifndef	TH_ACK
2075 # define	TH_ACK	0x10
2076 #endif
2077 #ifndef	TH_URG
2078 # define	TH_URG	0x20
2079 #endif
2080 #undef	TH_ACKMASK
2081 #define	TH_ACKMASK	(TH_FIN|TH_SYN|TH_RST|TH_ACK)
2082 
2083 #ifndef	IPOPT_EOL
2084 # define	IPOPT_EOL	0
2085 #endif
2086 #ifndef	IPOPT_NOP
2087 # define	IPOPT_NOP	1
2088 #endif
2089 #ifndef	IPOPT_RR
2090 # define	IPOPT_RR	7
2091 #endif
2092 #ifndef	IPOPT_TS
2093 # define	IPOPT_TS	68
2094 #endif
2095 #ifndef	IPOPT_SECURITY
2096 # define	IPOPT_SECURITY	130
2097 #endif
2098 #ifndef	IPOPT_LSRR
2099 # define	IPOPT_LSRR	131
2100 #endif
2101 #ifndef	IPOPT_SATID
2102 # define	IPOPT_SATID	136
2103 #endif
2104 #ifndef	IPOPT_SSRR
2105 # define	IPOPT_SSRR	137
2106 #endif
2107 #ifndef	IPOPT_SECUR_UNCLASS
2108 # define	IPOPT_SECUR_UNCLASS	((u_short)0x0000)
2109 #endif
2110 #ifndef	IPOPT_SECUR_CONFID
2111 # define	IPOPT_SECUR_CONFID	((u_short)0xf135)
2112 #endif
2113 #ifndef	IPOPT_SECUR_EFTO
2114 # define	IPOPT_SECUR_EFTO	((u_short)0x789a)
2115 #endif
2116 #ifndef	IPOPT_SECUR_MMMM
2117 # define	IPOPT_SECUR_MMMM	((u_short)0xbc4d)
2118 #endif
2119 #ifndef	IPOPT_SECUR_RESTR
2120 # define	IPOPT_SECUR_RESTR	((u_short)0xaf13)
2121 #endif
2122 #ifndef	IPOPT_SECUR_SECRET
2123 # define	IPOPT_SECUR_SECRET	((u_short)0xd788)
2124 #endif
2125 #ifndef IPOPT_SECUR_TOPSECRET
2126 # define	IPOPT_SECUR_TOPSECRET	((u_short)0x6bc5)
2127 #endif
2128 #ifndef IPOPT_OLEN
2129 # define	IPOPT_OLEN	1
2130 #endif
2131 #ifndef	IPPROTO_HOPOPTS
2132 # define	IPPROTO_HOPOPTS	0
2133 #endif
2134 #ifndef	IPPROTO_ENCAP
2135 # define	IPPROTO_ENCAP	4
2136 #endif
2137 #ifndef	IPPROTO_IPV6
2138 # define	IPPROTO_IPV6	41
2139 #endif
2140 #ifndef	IPPROTO_ROUTING
2141 # define	IPPROTO_ROUTING	43
2142 #endif
2143 #ifndef	IPPROTO_FRAGMENT
2144 # define	IPPROTO_FRAGMENT	44
2145 #endif
2146 #ifndef	IPPROTO_GRE
2147 # define	IPPROTO_GRE	47	/* GRE encaps RFC 1701 */
2148 #endif
2149 #ifndef	IPPROTO_ESP
2150 # define	IPPROTO_ESP	50
2151 #endif
2152 #ifndef	IPPROTO_AH
2153 # define	IPPROTO_AH	51
2154 #endif
2155 #ifndef	IPPROTO_ICMPV6
2156 # define	IPPROTO_ICMPV6	58
2157 #endif
2158 #ifndef	IPPROTO_NONE
2159 # define	IPPROTO_NONE	59
2160 #endif
2161 #ifndef	IPPROTO_DSTOPTS
2162 # define	IPPROTO_DSTOPTS	60
2163 #endif
2164 #ifndef	IPPROTO_FRAGMENT
2165 # define	IPPROTO_FRAGMENT	44
2166 #endif
2167 #ifndef	ICMP_ROUTERADVERT
2168 # define	ICMP_ROUTERADVERT	9
2169 #endif
2170 #ifndef	ICMP_ROUTERSOLICIT
2171 # define	ICMP_ROUTERSOLICIT	10
2172 #endif
2173 #ifndef	ICMP6_DST_UNREACH
2174 # define	ICMP6_DST_UNREACH	1
2175 #endif
2176 #ifndef	ICMP6_PACKET_TOO_BIG
2177 # define	ICMP6_PACKET_TOO_BIG	2
2178 #endif
2179 #ifndef	ICMP6_TIME_EXCEEDED
2180 # define	ICMP6_TIME_EXCEEDED	3
2181 #endif
2182 #ifndef	ICMP6_PARAM_PROB
2183 # define	ICMP6_PARAM_PROB	4
2184 #endif
2185 
2186 #ifndef	ICMP6_ECHO_REQUEST
2187 # define	ICMP6_ECHO_REQUEST	128
2188 #endif
2189 #ifndef	ICMP6_ECHO_REPLY
2190 # define	ICMP6_ECHO_REPLY	129
2191 #endif
2192 #ifndef	ICMP6_MEMBERSHIP_QUERY
2193 # define	ICMP6_MEMBERSHIP_QUERY	130
2194 #endif
2195 #ifndef	MLD6_LISTENER_QUERY
2196 # define	MLD6_LISTENER_QUERY	130
2197 #endif
2198 #ifndef	ICMP6_MEMBERSHIP_REPORT
2199 # define	ICMP6_MEMBERSHIP_REPORT	131
2200 #endif
2201 #ifndef	MLD6_LISTENER_REPORT
2202 # define	MLD6_LISTENER_REPORT	131
2203 #endif
2204 #ifndef	ICMP6_MEMBERSHIP_REDUCTION
2205 # define	ICMP6_MEMBERSHIP_REDUCTION	132
2206 #endif
2207 #ifndef	MLD6_LISTENER_DONE
2208 # define	MLD6_LISTENER_DONE	132
2209 #endif
2210 #ifndef	ND_ROUTER_SOLICIT
2211 # define	ND_ROUTER_SOLICIT	133
2212 #endif
2213 #ifndef	ND_ROUTER_ADVERT
2214 # define	ND_ROUTER_ADVERT	134
2215 #endif
2216 #ifndef	ND_NEIGHBOR_SOLICIT
2217 # define	ND_NEIGHBOR_SOLICIT	135
2218 #endif
2219 #ifndef	ND_NEIGHBOR_ADVERT
2220 # define	ND_NEIGHBOR_ADVERT	136
2221 #endif
2222 #ifndef	ND_REDIRECT
2223 # define	ND_REDIRECT	137
2224 #endif
2225 #ifndef	ICMP6_ROUTER_RENUMBERING
2226 # define	ICMP6_ROUTER_RENUMBERING	138
2227 #endif
2228 #ifndef	ICMP6_WRUREQUEST
2229 # define	ICMP6_WRUREQUEST	139
2230 #endif
2231 #ifndef	ICMP6_WRUREPLY
2232 # define	ICMP6_WRUREPLY		140
2233 #endif
2234 #ifndef	ICMP6_FQDN_QUERY
2235 # define	ICMP6_FQDN_QUERY	139
2236 #endif
2237 #ifndef	ICMP6_FQDN_REPLY
2238 # define	ICMP6_FQDN_REPLY	140
2239 #endif
2240 #ifndef	ICMP6_NI_QUERY
2241 # define	ICMP6_NI_QUERY		139
2242 #endif
2243 #ifndef	ICMP6_NI_REPLY
2244 # define	ICMP6_NI_REPLY		140
2245 #endif
2246 #ifndef	MLD6_MTRACE_RESP
2247 # define	MLD6_MTRACE_RESP	200
2248 #endif
2249 #ifndef	MLD6_MTRACE
2250 # define	MLD6_MTRACE		201
2251 #endif
2252 #ifndef	ICMP6_HADISCOV_REQUEST
2253 # define	ICMP6_HADISCOV_REQUEST	202
2254 #endif
2255 #ifndef	ICMP6_HADISCOV_REPLY
2256 # define	ICMP6_HADISCOV_REPLY	203
2257 #endif
2258 #ifndef	ICMP6_MOBILEPREFIX_SOLICIT
2259 # define	ICMP6_MOBILEPREFIX_SOLICIT	204
2260 #endif
2261 #ifndef	ICMP6_MOBILEPREFIX_ADVERT
2262 # define	ICMP6_MOBILEPREFIX_ADVERT	205
2263 #endif
2264 #ifndef	ICMP6_MAXTYPE
2265 # define	ICMP6_MAXTYPE		205
2266 #endif
2267 
2268 #ifndef	ICMP6_DST_UNREACH_NOROUTE
2269 # define	ICMP6_DST_UNREACH_NOROUTE	0
2270 #endif
2271 #ifndef	ICMP6_DST_UNREACH_ADMIN
2272 # define	ICMP6_DST_UNREACH_ADMIN		1
2273 #endif
2274 #ifndef	ICMP6_DST_UNREACH_NOTNEIGHBOR
2275 # define	ICMP6_DST_UNREACH_NOTNEIGHBOR	2
2276 #endif
2277 #ifndef	ICMP6_DST_UNREACH_BEYONDSCOPE
2278 # define	ICMP6_DST_UNREACH_BEYONDSCOPE	2
2279 #endif
2280 #ifndef	ICMP6_DST_UNREACH_ADDR
2281 # define	ICMP6_DST_UNREACH_ADDR		3
2282 #endif
2283 #ifndef	ICMP6_DST_UNREACH_NOPORT
2284 # define	ICMP6_DST_UNREACH_NOPORT	4
2285 #endif
2286 #ifndef	ICMP6_TIME_EXCEED_TRANSIT
2287 # define	ICMP6_TIME_EXCEED_TRANSIT	0
2288 #endif
2289 #ifndef	ICMP6_TIME_EXCEED_REASSEMBLY
2290 # define	ICMP6_TIME_EXCEED_REASSEMBLY	1
2291 #endif
2292 
2293 #ifndef	ICMP6_NI_SUCCESS
2294 # define	ICMP6_NI_SUCCESS	0
2295 #endif
2296 #ifndef	ICMP6_NI_REFUSED
2297 # define	ICMP6_NI_REFUSED	1
2298 #endif
2299 #ifndef	ICMP6_NI_UNKNOWN
2300 # define	ICMP6_NI_UNKNOWN	2
2301 #endif
2302 
2303 #ifndef	ICMP6_ROUTER_RENUMBERING_COMMAND
2304 # define	ICMP6_ROUTER_RENUMBERING_COMMAND	0
2305 #endif
2306 #ifndef	ICMP6_ROUTER_RENUMBERING_RESULT
2307 # define	ICMP6_ROUTER_RENUMBERING_RESULT	1
2308 #endif
2309 #ifndef	ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET
2310 # define	ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET	255
2311 #endif
2312 
2313 #ifndef	ICMP6_PARAMPROB_HEADER
2314 # define	ICMP6_PARAMPROB_HEADER	0
2315 #endif
2316 #ifndef	ICMP6_PARAMPROB_NEXTHEADER
2317 # define	ICMP6_PARAMPROB_NEXTHEADER	1
2318 #endif
2319 #ifndef	ICMP6_PARAMPROB_OPTION
2320 # define	ICMP6_PARAMPROB_OPTION	2
2321 #endif
2322 
2323 #ifndef	ICMP6_NI_SUBJ_IPV6
2324 # define	ICMP6_NI_SUBJ_IPV6	0
2325 #endif
2326 #ifndef	ICMP6_NI_SUBJ_FQDN
2327 # define	ICMP6_NI_SUBJ_FQDN	1
2328 #endif
2329 #ifndef	ICMP6_NI_SUBJ_IPV4
2330 # define	ICMP6_NI_SUBJ_IPV4	2
2331 #endif
2332 
2333 /*
2334  * ECN is a new addition to TCP - RFC 2481
2335  */
2336 #ifndef TH_ECN
2337 # define	TH_ECN	0x40
2338 #endif
2339 #ifndef TH_CWR
2340 # define	TH_CWR	0x80
2341 #endif
2342 #define	TH_ECNALL	(TH_ECN|TH_CWR)
2343 
2344 /*
2345  * TCP States
2346  */
2347 #define IPF_TCPS_CLOSED		0	/* closed */
2348 #define IPF_TCPS_LISTEN		1	/* listening for connection */
2349 #define IPF_TCPS_SYN_SENT	2	/* active, have sent syn */
2350 #define IPF_TCPS_SYN_RECEIVED	3	/* have send and received syn */
2351 #define IPF_TCPS_HALF_ESTAB	4	/* for connections not fully "up" */
2352 /* states < IPF_TCPS_ESTABLISHED are those where connections not established */
2353 #define IPF_TCPS_ESTABLISHED	5	/* established */
2354 #define IPF_TCPS_CLOSE_WAIT	6	/* rcvd fin, waiting for close */
2355 /* states > IPF_TCPS_CLOSE_WAIT are those where user has closed */
2356 #define IPF_TCPS_FIN_WAIT_1	7	/* have closed, sent fin */
2357 #define IPF_TCPS_CLOSING	8	/* closed xchd FIN; await FIN ACK */
2358 #define IPF_TCPS_LAST_ACK	9	/* had fin and close; await FIN ACK */
2359 /* states > IPF_TCPS_CLOSE_WAIT && < IPF_TCPS_FIN_WAIT_2 await ACK of FIN */
2360 #define IPF_TCPS_FIN_WAIT_2	10	/* have closed, fin is acked */
2361 #define IPF_TCPS_TIME_WAIT	11	/* in 2*msl quiet wait after close */
2362 #define IPF_TCP_NSTATES		12
2363 
2364 #define	TCP_MSL			120
2365 
2366 #undef	ICMP_MAX_UNREACH
2367 #define	ICMP_MAX_UNREACH	14
2368 #undef	ICMP_MAXTYPE
2369 #define	ICMP_MAXTYPE		18
2370 
2371 #ifndef	IFNAMSIZ
2372 #define	IFNAMSIZ		16
2373 #endif
2374 
2375 #ifndef	LOG_FTP
2376 # define	LOG_FTP		(11<<3)
2377 #endif
2378 #ifndef	LOG_AUTHPRIV
2379 # define	LOG_AUTHPRIV	(10<<3)
2380 #endif
2381 #ifndef	LOG_AUDIT
2382 # define	LOG_AUDIT	(13<<3)
2383 #endif
2384 #ifndef	LOG_NTP
2385 # define	LOG_NTP		(12<<3)
2386 #endif
2387 #ifndef	LOG_SECURITY
2388 # define	LOG_SECURITY	(13<<3)
2389 #endif
2390 #ifndef	LOG_LFMT
2391 # define	LOG_LFMT	(14<<3)
2392 #endif
2393 #ifndef	LOG_CONSOLE
2394 # define	LOG_CONSOLE	(14<<3)
2395 #endif
2396 
2397 /*
2398  * ICMP error replies have an IP header (20 bytes), 8 bytes of ICMP data,
2399  * another IP header and then 64 bits of data, totalling 56.  Of course,
2400  * the last 64 bits is dependent on that being available.
2401  */
2402 #define	ICMPERR_ICMPHLEN	8
2403 #define	ICMPERR_IPICMPHLEN	(20 + 8)
2404 #define	ICMPERR_MINPKTLEN	(20 + 8 + 20)
2405 #define	ICMPERR_MAXPKTLEN	(20 + 8 + 20 + 8)
2406 #define ICMP6ERR_MINPKTLEN	(40 + 8)
2407 #define ICMP6ERR_IPICMPHLEN	(40 + 8 + 40)
2408 
2409 #ifndef MIN
2410 # define	MIN(a,b)	(((a)<(b))?(a):(b))
2411 #endif
2412 
2413 #ifdef IPF_DEBUG
2414 # define	DPRINT(x)	printf x
2415 #else
2416 # define	DPRINT(x)
2417 #endif
2418 
2419 #ifdef RESCUE
2420 # undef IPFILTER_BPF
2421 #endif
2422 
2423 #endif	/* __IP_COMPAT_H__ */
2424