Merge from vendor branch FILE:
[dragonfly.git] / sys / net / ipfw / ip_fw2.h
1 /*
2  * Copyright (c) 2002 Luigi Rizzo, Universita` di Pisa
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD: src/sys/netinet/ip_fw2.h,v 1.1.2.2 2002/08/16 11:03:11 luigi Exp $
26  * $DragonFly: src/sys/net/ipfw/ip_fw2.h,v 1.8 2007/11/16 02:45:45 sephe Exp $
27  */
28
29 #ifndef _IPFW2_H
30 #define _IPFW2_H
31
32 /*
33  * The kernel representation of ipfw rules is made of a list of
34  * 'instructions' (for all practical purposes equivalent to BPF
35  * instructions), which specify which fields of the packet
36  * (or its metatada) should be analysed.
37  *
38  * Each instruction is stored in a structure which begins with
39  * "ipfw_insn", and can contain extra fields depending on the
40  * instruction type (listed below).
41  *
42  * "enum ipfw_opcodes" are the opcodes supported. We can have up
43  * to 256 different opcodes.
44  */
45
46 enum ipfw_opcodes {             /* arguments (4 byte each)      */
47         O_NOP,
48
49         O_IP_SRC,               /* u32 = IP                     */
50         O_IP_SRC_MASK,          /* ip = IP/mask                 */
51         O_IP_SRC_ME,            /* none                         */
52         O_IP_SRC_SET,           /* u32=base, arg1=len, bitmap   */
53
54         O_IP_DST,               /* u32 = IP                     */
55         O_IP_DST_MASK,          /* ip = IP/mask                 */
56         O_IP_DST_ME,            /* none                         */
57         O_IP_DST_SET,           /* u32=base, arg1=len, bitmap   */
58
59         O_IP_SRCPORT,           /* (n)port list:mask 4 byte ea  */
60         O_IP_DSTPORT,           /* (n)port list:mask 4 byte ea  */
61         O_PROTO,                /* arg1=protocol                */
62
63         O_MACADDR2,             /* 2 mac addr:mask              */
64         O_MAC_TYPE,             /* same as srcport              */
65
66         O_LAYER2,               /* none                         */
67         O_IN,                   /* none                         */
68         O_FRAG,                 /* none                         */
69
70         O_RECV,                 /* none                         */
71         O_XMIT,                 /* none                         */
72         O_VIA,                  /* none                         */
73
74         O_IPOPT,                /* arg1 = 2*u8 bitmap           */
75         O_IPLEN,                /* arg1 = len                   */
76         O_IPID,                 /* arg1 = id                    */
77
78         O_IPTOS,                /* arg1 = id                    */
79         O_IPPRECEDENCE,         /* arg1 = precedence << 5       */
80         O_IPTTL,                /* arg1 = TTL                   */
81
82         O_IPVER,                /* arg1 = version               */
83         O_UID,                  /* u32 = id                     */
84         O_GID,                  /* u32 = id                     */
85         O_ESTAB,                /* none (tcp established)       */
86         O_TCPFLAGS,             /* arg1 = 2*u8 bitmap           */
87         O_TCPWIN,               /* arg1 = desired win           */
88         O_TCPSEQ,               /* u32 = desired seq.           */
89         O_TCPACK,               /* u32 = desired seq.           */
90         O_ICMPTYPE,             /* u32 = icmp bitmap            */
91         O_TCPOPTS,              /* arg1 = 2*u8 bitmap           */
92
93         O_PROBE_STATE,          /* none                         */
94         O_KEEP_STATE,           /* none                         */
95         O_LIMIT,                /* ipfw_insn_limit              */
96         O_LIMIT_PARENT,         /* dyn_type, not an opcode.     */
97         /*
98          * these are really 'actions', and must be last in the list.
99          */
100
101         O_LOG,                  /* ipfw_insn_log                */
102         O_PROB,                 /* u32 = match probability      */
103
104         O_CHECK_STATE,          /* none                         */
105         O_ACCEPT,               /* none                         */
106         O_DENY,                 /* none                         */
107         O_REJECT,               /* arg1=icmp arg (same as deny) */
108         O_COUNT,                /* none                         */
109         O_SKIPTO,               /* arg1=next rule number        */
110         O_PIPE,                 /* arg1=pipe number             */
111         O_QUEUE,                /* arg1=queue number            */
112         O_DIVERT,               /* arg1=port number             */
113         O_TEE,                  /* arg1=port number             */
114         O_FORWARD_IP,           /* fwd sockaddr                 */
115         O_FORWARD_MAC,          /* fwd mac                      */
116         O_LAST_OPCODE           /* not an opcode!               */
117 };
118
119 /*
120  * Template for instructions.
121  *
122  * ipfw_insn is used for all instructions which require no operands,
123  * a single 16-bit value (arg1), or a couple of 8-bit values.
124  *
125  * For other instructions which require different/larger arguments
126  * we have derived structures, ipfw_insn_*.
127  *
128  * The size of the instruction (in 32-bit words) is in the low
129  * 6 bits of "len". The 2 remaining bits are used to implement
130  * NOT and OR on individual instructions. Given a type, you can
131  * compute the length to be put in "len" using F_INSN_SIZE(t)
132  *
133  * F_NOT        negates the match result of the instruction.
134  *
135  * F_OR         is used to build or blocks. By default, instructions
136  *              are evaluated as part of a logical AND. An "or" block
137  *              { X or Y or Z } contains F_OR set in all but the last
138  *              instruction of the block. A match will cause the code
139  *              to skip past the last instruction of the block.
140  *
141  * NOTA BENE: in a couple of places we assume that
142  *      sizeof(ipfw_insn) == sizeof(uint32_t)
143  * this needs to be fixed.
144  *
145  */
146 typedef struct  _ipfw_insn {    /* template for instructions */
147         enum ipfw_opcodes       opcode:8;
148         uint8_t         len;    /* numer of 32-byte words */
149 #define F_NOT           0x80
150 #define F_OR            0x40
151 #define F_LEN_MASK      0x3f
152 #define F_LEN(cmd)      ((cmd)->len & F_LEN_MASK)
153
154         uint16_t        arg1;
155 } ipfw_insn;
156
157 /*
158  * The F_INSN_SIZE(type) computes the size, in 4-byte words, of
159  * a given type.
160  */
161 #define F_INSN_SIZE(t)  ((sizeof (t))/sizeof(uint32_t))
162
163 /*
164  * This is used to store an array of 16-bit entries (ports etc.)
165  */
166 typedef struct  _ipfw_insn_u16 {
167         ipfw_insn o;
168         uint16_t ports[2];      /* there may be more */
169 } ipfw_insn_u16;
170
171 /*
172  * This is used to store an array of 32-bit entries
173  * (uid, single IPv4 addresses etc.)
174  */
175 typedef struct  _ipfw_insn_u32 {
176         ipfw_insn o;
177         uint32_t d[1];  /* one or more */
178 } ipfw_insn_u32;
179
180 /*
181  * This is used to store IP addr-mask pairs.
182  */
183 typedef struct  _ipfw_insn_ip {
184         ipfw_insn o;
185         struct in_addr  addr;
186         struct in_addr  mask;
187 } ipfw_insn_ip;
188
189 /*
190  * This is used to forward to a given address (ip)
191  */
192 typedef struct  _ipfw_insn_sa {
193         ipfw_insn o;
194         struct sockaddr_in sa;
195 } ipfw_insn_sa;
196
197 /*
198  * This is used for MAC addr-mask pairs.
199  */
200 typedef struct  _ipfw_insn_mac {
201         ipfw_insn o;
202         u_char addr[12];        /* dst[6] + src[6] */
203         u_char mask[12];        /* dst[6] + src[6] */
204 } ipfw_insn_mac;
205
206 /*
207  * This is used for interface match rules (recv xx, xmit xx)
208  */
209 typedef struct  _ipfw_insn_if {
210         ipfw_insn o;
211         union {
212                 struct in_addr ip;
213                 int glob;
214         } p;
215         char name[IFNAMSIZ];
216 } ipfw_insn_if;
217
218 /*
219  * This is used for pipe and queue actions, which need to store
220  * a single pointer (which can have different size on different
221  * architectures.
222  */
223 typedef struct  _ipfw_insn_pipe {
224         ipfw_insn       o;
225         void            *pipe_ptr;
226 } ipfw_insn_pipe;
227
228 /*
229  * This is used for limit rules.
230  */
231 typedef struct  _ipfw_insn_limit {
232         ipfw_insn o;
233         uint8_t _pad;
234         uint8_t limit_mask;     /* combination of DYN_* below   */
235 #define DYN_SRC_ADDR    0x1
236 #define DYN_SRC_PORT    0x2
237 #define DYN_DST_ADDR    0x4
238 #define DYN_DST_PORT    0x8
239
240         uint16_t conn_limit;
241 } ipfw_insn_limit;
242
243 /*
244  * This is used for log instructions
245  */
246 typedef struct  _ipfw_insn_log {
247         ipfw_insn o;
248         uint32_t max_log;       /* how many do we log -- 0 = all */
249         uint32_t log_left;      /* how many left to log         */
250 } ipfw_insn_log;
251
252 #ifdef _KERNEL
253
254 /*
255  * Here we have the structure representing an ipfw rule.
256  *
257  * It starts with a general area (with link fields and counters)
258  * followed by an array of one or more instructions, which the code
259  * accesses as an array of 32-bit values.
260  *
261  * Given a rule pointer  r:
262  *
263  *  r->cmd              is the start of the first instruction.
264  *  ACTION_PTR(r)       is the start of the first action (things to do
265  *                      once a rule matched).
266  *
267  * When assembling instruction, remember the following:
268  *
269  *  + if a rule has a "keep-state" (or "limit") option, then the
270  *      first instruction (at r->cmd) MUST BE an O_PROBE_STATE
271  *  + if a rule has a "log" option, then the first action
272  *      (at ACTION_PTR(r)) MUST be O_LOG
273  *
274  * NOTE: we use a simple linked list of rules because we never need
275  *      to delete a rule without scanning the list. We do not use
276  *      queue(3) macros for portability and readability.
277  */
278
279 struct ip_fw {
280         struct ip_fw    *next;          /* linked list of rules         */
281         struct ip_fw    *next_rule;     /* ptr to next [skipto] rule    */
282         uint16_t        act_ofs;        /* offset of action in 32-bit units */
283         uint16_t        cmd_len;        /* # of 32-bit words in cmd     */
284         uint16_t        rulenum;        /* rule number                  */
285         uint8_t         set;            /* rule set (0..31)             */
286         uint8_t         usr_flags;      /* IPFW_USR_F_                  */
287
288         /* These fields are present in all rules.                       */
289         uint64_t        pcnt;           /* Packet counter               */
290         uint64_t        bcnt;           /* Byte counter                 */
291         uint32_t        timestamp;      /* tv_sec of last match         */
292
293         uint32_t        refcnt;         /* Ref count for transit pkts   */
294         uint32_t        rule_flags;     /* IPFW_RULE_F_                 */
295
296         ipfw_insn       cmd[1];         /* storage for commands         */
297 };
298
299 #define IPFW_RULE_F_INVALID     0x1
300
301 #define RULESIZE(rule)  (sizeof(struct ip_fw) + (rule)->cmd_len * 4 - 4)
302
303 /*
304  * This structure is used as a flow mask and a flow id for various
305  * parts of the code.
306  */
307 struct ipfw_flow_id {
308         uint32_t        dst_ip;
309         uint32_t        src_ip;
310         uint16_t        dst_port;
311         uint16_t        src_port;
312         uint8_t         proto;
313         uint8_t         flags;  /* protocol-specific flags */
314 };
315
316 /*
317  * dynamic ipfw rule
318  */
319 typedef struct _ipfw_dyn_rule ipfw_dyn_rule;
320
321 struct _ipfw_dyn_rule {
322         ipfw_dyn_rule   *next;          /* linked list of rules.        */
323         struct ipfw_flow_id id;         /* (masked) flow id             */
324         struct ip_fw *rule;             /* pointer to rule              */
325         ipfw_dyn_rule *parent;          /* pointer to parent rule       */
326         uint32_t        expire;         /* expire time                  */
327         uint64_t        pcnt;           /* packet match counter         */
328         uint64_t        bcnt;           /* byte match counter           */
329         uint32_t        bucket;         /* which bucket in hash table   */
330         uint32_t        state;          /* state of this rule (typically a
331                                          * combination of TCP flags)
332                                          */
333         uint32_t        ack_fwd;        /* most recent ACKs in forward  */
334         uint32_t        ack_rev;        /* and reverse directions (used */
335                                         /* to generate keepalives)      */
336         uint16_t        dyn_type;       /* rule type                    */
337         uint16_t        count;          /* refcount                     */
338 };
339
340 /*
341  * Main firewall chains definitions and global var's definitions.
342  */
343
344 #define IP_FW_PORT_DYNT_FLAG    0x10000
345 #define IP_FW_PORT_TEE_FLAG     0x20000
346 #define IP_FW_PORT_DENY_FLAG    0x40000
347
348 /*
349  * arguments for calling ipfw_chk() and dummynet_io(). We put them
350  * all into a structure because this way it is easier and more
351  * efficient to pass variables around and extend the interface.
352  */
353 struct ip_fw_args {
354         struct mbuf     *m;             /* the mbuf chain               */
355         struct ifnet    *oif;           /* output interface             */
356         struct sockaddr_in *next_hop;   /* forward address              */
357         struct ip_fw    *rule;          /* matching rule                */
358         struct ether_header *eh;        /* for bridged packets          */
359
360         struct route    *ro;            /* for dummynet                 */
361         struct sockaddr_in *dst;        /* for dummynet                 */
362         int flags;                      /* for dummynet                 */
363
364         struct ipfw_flow_id f_id;       /* grabbed from IP header       */
365         uint32_t        retval;
366 };
367
368 /*
369  * Function definitions.
370  */
371
372 /* Firewall hooks */
373 struct sockopt;
374 struct dn_flow_set;
375
376 typedef int     ip_fw_chk_t(struct ip_fw_args *);
377 typedef int     ip_fw_ctl_t(struct sockopt *);
378 typedef void    ip_fw_dn_io_t(struct mbuf *, int, int, struct ip_fw_args *);
379
380 extern ip_fw_chk_t      *ip_fw_chk_ptr;
381 extern ip_fw_ctl_t      *ip_fw_ctl_ptr;
382 extern ip_fw_dn_io_t    *ip_fw_dn_io_ptr;
383
384 extern int fw_one_pass;
385 extern int fw_enable;
386 #define IPFW_LOADED     (ip_fw_chk_ptr != NULL)
387
388 #endif /* _KERNEL */
389
390 #define ACTION_PTR(rule)        \
391         (ipfw_insn *)((uint32_t *)((rule)->cmd) + ((rule)->act_ofs))
392
393 struct ipfw_ioc_rule {
394         uint16_t        act_ofs;        /* offset of action in 32-bit units */
395         uint16_t        cmd_len;        /* # of 32-bit words in cmd     */
396         uint16_t        rulenum;        /* rule number                  */
397         uint8_t         set;            /* rule set (0..31)             */
398         uint8_t         usr_flags;      /* IPFW_USR_F_                  */
399
400         /* Rule set information */
401         uint32_t        set_disable;    /* disabled rule sets           */
402         uint32_t        static_count;   /* # of static rules            */
403         uint32_t        static_len;     /* total length of static rules */
404
405         /* Statistics */
406         uint64_t        pcnt;           /* Packet counter               */
407         uint64_t        bcnt;           /* Byte counter                 */
408         uint32_t        timestamp;      /* tv_sec of last match         */
409
410         uint8_t         reserved[16];
411
412         ipfw_insn       cmd[1];         /* storage for commands         */
413 };
414
415 #define IPFW_USR_F_NORULE       0x01
416
417 #define IPFW_RULE_SIZE_MAX      255     /* unit: uint32_t */
418
419 #define IOC_RULESIZE(rule)      \
420         (sizeof(struct ipfw_ioc_rule) + (rule)->cmd_len * 4 - 4)
421
422 struct ipfw_ioc_flowid {
423         uint16_t        type;   /* ETHERTYPE_ */
424         uint16_t        pad;
425         union {
426                 struct {
427                         uint32_t dst_ip;
428                         uint32_t src_ip;
429                         uint16_t dst_port;
430                         uint16_t src_port;
431                         uint8_t proto;
432                 } ip;
433                 uint8_t pad[64];
434         } u;
435 };
436
437 struct ipfw_ioc_state {
438         uint32_t        expire;         /* expire time                  */
439         uint64_t        pcnt;           /* packet match counter         */
440         uint64_t        bcnt;           /* byte match counter           */
441
442         uint16_t        dyn_type;       /* rule type                    */
443         uint16_t        count;          /* refcount                     */
444
445         uint16_t        rulenum;
446         uint16_t        pad;
447
448         int             cpu;            /* reserved                     */
449
450         struct ipfw_ioc_flowid id;      /* (masked) flow id             */
451         uint8_t         reserved[16];
452 };
453
454 /*
455  * Definitions for IP option names.
456  */
457 #define IP_FW_IPOPT_LSRR        0x01
458 #define IP_FW_IPOPT_SSRR        0x02
459 #define IP_FW_IPOPT_RR          0x04
460 #define IP_FW_IPOPT_TS          0x08
461
462 /*
463  * Definitions for TCP option names.
464  */
465 #define IP_FW_TCPOPT_MSS        0x01
466 #define IP_FW_TCPOPT_WINDOW     0x02
467 #define IP_FW_TCPOPT_SACK       0x04
468 #define IP_FW_TCPOPT_TS         0x08
469 #define IP_FW_TCPOPT_CC         0x10
470
471 #define ICMP_REJECT_RST         0x100   /* fake ICMP code (send a TCP RST) */
472
473 #endif /* _IPFW2_H */