]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/ip_compat.h
Import IP Filter v3.1.7 into FreeBSD tree
[FreeBSD/FreeBSD.git] / contrib / ipfilter / ip_compat.h
1 /*
2  * (C)opyright 1993, 1994, 1995 by Darren Reed.
3  *
4  * Redistribution and use in source and binary forms are permitted
5  * provided that this notice is preserved and due credit is given
6  * to the original author and the contributors.
7  *
8  * @(#)ip_compat.h      1.8 1/14/96
9  * $Id: ip_compat.h,v 2.0.1.4 1997/02/04 14:24:25 darrenr Exp $
10  */
11
12 #ifndef __IP_COMPAT_H_
13 #define __IP_COMPAT_H__
14
15 #ifndef SOLARIS
16 #define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
17 #endif
18 #if     SOLARIS
19 #define MTYPE(m)        ((m)->b_datap->db_type)
20 #endif
21 #define IPMINLEN(i, h)  ((i)->ip_len >= ((i)->ip_hl * 4 + sizeof(struct h)))
22
23 #ifndef IP_OFFMASK
24 #define IP_OFFMASK      0x1fff
25 #endif
26
27 #ifndef MAX
28 #define MAX(a,b)        (((a) > (b)) ? (a) : (b))
29 #endif
30
31 /*
32  * Security Options for Intenet Protocol (IPSO) as defined in RFC 1108.
33  *
34  * Basic Option
35  *
36  * 00000001   -   (Reserved 4)
37  * 00111101   -   Top Secret
38  * 01011010   -   Secret
39  * 10010110   -   Confidential
40  * 01100110   -   (Reserved 3)
41  * 11001100   -   (Reserved 2)
42  * 10101011   -   Unclassified
43  * 11110001   -   (Reserved 1)
44  */
45 #define IPSO_CLASS_RES4         0x01
46 #define IPSO_CLASS_TOPS         0x3d
47 #define IPSO_CLASS_SECR         0x5a
48 #define IPSO_CLASS_CONF         0x96
49 #define IPSO_CLASS_RES3         0x66
50 #define IPSO_CLASS_RES2         0xcc
51 #define IPSO_CLASS_UNCL         0xab
52 #define IPSO_CLASS_RES1         0xf1
53
54 #define IPSO_AUTH_GENSER        0x80
55 #define IPSO_AUTH_ESI           0x40
56 #define IPSO_AUTH_SCI           0x20
57 #define IPSO_AUTH_NSA           0x10
58 #define IPSO_AUTH_DOE           0x08
59 #define IPSO_AUTH_UN            0x06
60 #define IPSO_AUTH_FTE           0x01
61
62 /*
63  * IP option #defines
64  */
65 /*#define       IPOPT_RR        7 */
66 #define IPOPT_ZSU       10      /* ZSU */
67 #define IPOPT_MTUP      11      /* MTUP */
68 #define IPOPT_MTUR      12      /* MTUR */
69 #define IPOPT_ENCODE    15      /* ENCODE */
70 /*#define       IPOPT_TS        68 */
71 #define IPOPT_TR        82      /* TR */
72 /*#define       IPOPT_SECURITY  130 */
73 /*#define       IPOPT_LSRR      131 */
74 #define IPOPT_E_SEC     133     /* E-SEC */
75 #define IPOPT_CIPSO     134     /* CIPSO */
76 /*#define       IPOPT_SATID     136 */
77 #ifndef IPOPT_SID
78 # define        IPOPT_SID       IPOPT_SATID
79 #endif
80 /*#define       IPOPT_SSRR      137 */
81 #define IPOPT_ADDEXT    147     /* ADDEXT */
82 #define IPOPT_VISA      142     /* VISA */
83 #define IPOPT_IMITD     144     /* IMITD */
84 #define IPOPT_EIP       145     /* EIP */
85 #define IPOPT_FINN      205     /* FINN */
86
87
88 /*
89  * Build some macros and #defines to enable the same code to compile anywhere
90  * Well, that's the idea, anyway :-)
91  */
92 #ifdef _KERNEL
93 # if SOLARIS
94 #  define       MUTEX_ENTER(x)  mutex_enter(x)
95 #  define       MUTEX_EXIT(x)   mutex_exit(x)
96 #  define       MTOD(m,t)       (t)((m)->b_rptr)
97 #  define       IRCOPY(a,b,c)   copyin((a), (b), (c))
98 #  define       IWCOPY(a,b,c)   copyout((a), (b), (c))
99 # else
100 #  define       MUTEX_ENTER(x)  ;
101 #  define       MUTEX_EXIT(x)   ;
102 #  ifndef linux
103 #   define      MTOD(m,t)       mtod(m,t)
104 #   define      IRCOPY(a,b,c)   bcopy((a), (b), (c))
105 #   define      IWCOPY(a,b,c)   bcopy((a), (b), (c))
106 #  endif
107 # endif /* SOLARIS */
108
109 # ifdef sun
110 #  if defined(__svr4__) || defined(__SVR4)
111 #   define      GETUNIT(n)      get_unit((n))
112 #  else
113 #   include     <sys/kmem_alloc.h>
114 #   define      GETUNIT(n)      ifunit((n), IFNAMSIZ)
115 #  endif
116 # else
117 #  define       GETUNIT(n)      ifunit((n))
118 # endif /* sun */
119
120 # if defined(sun) && !defined(linux)
121 #  define       UIOMOVE(a,b,c,d)        uiomove(a,b,c,d)
122 #  define       SLEEP(id, n)    sleep((id), PZERO+1)
123 #  define       KFREE(x)        kmem_free((char *)(x), sizeof(*(x)))
124 #  if SOLARIS
125 typedef struct  qif     {
126         struct  qif     *qf_next;
127         ill_t   *qf_ill;
128         kmutex_t        qf_lock;
129         void    *qf_iptr;
130         void    *qf_optr;
131         queue_t *qf_in;
132         queue_t *qf_out;
133         void    *qf_wqinfo;
134         void    *qf_rqinfo;
135         int     (*qf_inp)();
136         int     (*qf_outp)();
137         mblk_t  *qf_m;
138         int     qf_len;
139         char    qf_name[8];
140         /*
141          * in case the ILL has disappeared...
142          */
143         int     qf_hl;  /* header length */
144 } qif_t;
145 #   define      SPLNET(x)       ;
146 #   undef       SPLX
147 #   define      SPLX(x)         ;
148 #   ifdef       sparc
149 #    define     ntohs(x)        (x)
150 #    define     ntohl(x)        (x)
151 #    define     htons(x)        (x)
152 #    define     htonl(x)        (x)
153 #   endif
154 #   define      KMALLOC(x)      kmem_alloc((x), KM_NOSLEEP)
155 #   define      GET_MINOR(x)    getminor(x)
156 #  else
157 #   define      KMALLOC(x)      new_kmem_alloc((x), KMEM_NOSLEEP)
158 #  endif /* __svr4__ */
159 # endif /* sun && !linux */
160 # ifndef        GET_MINOR
161 #  define       GET_MINOR(x)    minor(x)
162 # endif
163 # if BSD >= 199306 || defined(__FreeBSD__)
164 #  include <vm/vm.h>
165 #  if !defined(__FreeBSD__)
166 #   include <vm/vm_extern.h>
167 #   include <sys/proc.h>
168 extern  vm_map_t        kmem_map;
169 #  else
170 #   include <vm/vm_kern.h>
171 #  endif /* __FreeBSD__ */
172 /*
173 ** #  define    KMALLOC(x)      kmem_alloc(kmem_map, (x))
174 ** #  define    KFREE(x)        kmem_free(kmem_map, (vm_offset_t)(x), \
175                                           sizeof(*(x)))
176 */
177 #  ifdef        M_PFIL
178 #   define      KMALLOC(x)      malloc((x), M_PFIL, M_NOWAIT)
179 #   define      KFREE(x)        FREE((x), M_PFIL)
180 #  else
181 #   define      KMALLOC(x)      malloc((x), M_TEMP, M_NOWAIT)
182 #   define      KFREE(x)        FREE((x), M_TEMP)
183 #  endif
184 #  define       UIOMOVE(a,b,c,d)        uiomove(a,b,d)
185 #  define       SLEEP(id, n)    tsleep((id), PPAUSE|PCATCH, n, 0)
186 # endif /* BSD */
187 # if defined(NetBSD1_0) && (NetBSD1_0 > 1)
188 #  define       SPLNET(x)       x = splsoftnet()
189 # else
190 #  if !SOLARIS
191 #   define      SPLNET(x)       x = splnet()
192 #   define      SPLX(x)         (void) splx(x)
193 #  endif
194 # endif
195 #else
196 # ifndef        linux
197 #  define       MUTEX_ENTER(x)  ;
198 #  define       MUTEX_EXIT(x)   ;
199 #  define       SPLNET(x)       ;
200 #  define       SPLX(x)         ;
201 #  define       KMALLOC(x)      malloc(x)
202 #  define       KFREE(x)        free(x)
203 #  define       GETUNIT(x)      (x)
204 #  define       IRCOPY(a,b,c)   bcopy((a), (b), (c))
205 #  define       IWCOPY(a,b,c)   bcopy((a), (b), (c))
206 # endif
207 #endif /* KERNEL */
208
209 #ifdef linux
210 # define        ICMP_UNREACH    ICMP_DEST_UNREACH
211 # define        ICMP_SOURCEQUENCH       ICMP_SOURCE_QUENCH
212 # define        ICMP_TIMXCEED   ICMP_TIME_EXCEEDED
213 # define        ICMP_PARAMPROB  ICMP_PARAMETERPROB
214
215 # define        TH_FIN  0x01
216 # define        TH_SYN  0x02
217 # define        TH_RST  0x04
218 # define        TH_PUSH 0x08
219 # define        TH_ACK  0x10
220 # define        TH_URG  0x20
221
222 typedef struct  {
223         __u16   th_sport;
224         __u16   th_dport;
225         __u32   th_seq;
226         __u32   th_ack;
227         __u8    th_x;
228         __u8    th_flags;
229         __u16   th_win;
230         __u16   th_sum;
231         __u16   th_urp;
232 } tcphdr_t;
233
234 typedef struct  {
235         __u16   uh_sport;
236         __u16   uh_dport;
237         __u16   uh_ulen;
238         __u16   uh_sun;
239 } udphdr_t;
240
241 typedef struct  {
242 # if defined(__i386__) || defined(__MIPSEL__) || defined(__alpha__) ||\
243     defined(vax)
244         __u8    ip_hl:4;
245         __u8    ip_v:4;
246 # else
247         __u8    ip_hl:4;
248         __u8    ip_v:4;
249 # endif
250         __u8    ip_tos;
251         __u16   ip_len;
252         __u16   ip_id;
253         __u16   ip_off;
254         __u8    ip_ttl;
255         __u8    ip_p;
256         __u16   ip_sum;
257         struct  in_addr ip_src;
258         struct  in_addr ip_dst;
259 } ip_t;
260
261 /*
262  * Structure of an icmp header.
263  */
264 struct icmp {
265         u_char  icmp_type;              /* type of message, see below */
266         u_char  icmp_code;              /* type sub code */
267         u_short icmp_cksum;             /* ones complement cksum of struct */
268         union {
269                 u_char ih_pptr;                 /* ICMP_PARAMPROB */
270                 struct in_addr ih_gwaddr;       /* ICMP_REDIRECT */
271                 struct ih_idseq {
272                         n_short icd_id;
273                         n_short icd_seq;
274                 } ih_idseq;
275                 int ih_void;
276         } icmp_hun;
277 # define        icmp_pptr       icmp_hun.ih_pptr
278 # define        icmp_gwaddr     icmp_hun.ih_gwaddr
279 # define        icmp_id         icmp_hun.ih_idseq.icd_id
280 # define        icmp_seq        icmp_hun.ih_idseq.icd_seq
281 # define        icmp_void       icmp_hun.ih_void
282         union {
283                 struct id_ts {
284                         n_time its_otime;
285                         n_time its_rtime;
286                         n_time its_ttime;
287                 } id_ts;
288                 struct id_ip  {
289                         ip_t idi_ip;
290                         /* options and then 64 bits of data */
291                 } id_ip;
292                 u_long  id_mask;
293                 char    id_data[1];
294         } icmp_dun;
295 # define        icmp_otime      icmp_dun.id_ts.its_otime
296 # define        icmp_rtime      icmp_dun.id_ts.its_rtime
297 # define        icmp_ttime      icmp_dun.id_ts.its_ttime
298 # define        icmp_ip         icmp_dun.id_ip.idi_ip
299 # define        icmp_mask       icmp_dun.id_mask
300 # define        icmp_data       icmp_dun.id_data
301 };
302
303 struct ipovly {
304         caddr_t ih_next, ih_prev;       /* for protocol sequence q's */
305         u_char  ih_x1;                  /* (unused) */
306         u_char  ih_pr;                  /* protocol */
307         short   ih_len;                 /* protocol length */
308         struct  in_addr ih_src;         /* source internet address */
309         struct  in_addr ih_dst;         /* destination internet address */
310 };
311
312 # define        SPLX(x)         (void)
313 # define        SPLNET(x)       (void)
314
315 # define        bcopy(a,b,c)    memmove(b,a,c)
316 # define        bcmp(a,b,c)     memcmp(a,b,c)
317
318 # define        UNITNAME(n)     dev_get((n))
319 # define        ifnet   device
320
321 # define        KMALLOC(x)      kmalloc((x), GFP_ATOMIC)
322 # define        KFREE(x)        kfree_s((x), sizeof(*(x)))
323 # define        IRCOPY(a,b,c)   { \
324                                  error = verify_area(VERIFY_READ, \
325                                                      (b) ,sizeof((b))); \
326                                  if (!error) \
327                                         memcpy_fromfs((b), (a), (c)); \
328                                 }
329 # define        IWCOPY(a,b,c)   { \
330                                  error = verify_area(VERIFY_WRITE, \
331                                                      (b) ,sizeof((b))); \
332                                  if (!error) \
333                                         memcpy_tofs((b), (a), (c)); \
334                                 }
335 #else
336 typedef struct  tcphdr  tcphdr_t;
337 typedef struct  udphdr  udphdr_t;
338 typedef struct  icmp    icmphdr_t;
339 typedef struct  ip      ip_t;
340 #endif /* linux */
341
342 #endif  /* __IP_COMPAT_H__ */