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