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