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