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