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