ipfw: Add per-cpu table support.
[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_IP_SRC_TABLE,         /* arg1 = tableid               */
116         O_IP_DST_TABLE,         /* arg1 = tableid               */
117         O_LAST_OPCODE           /* not an opcode!               */
118 };
119
120 /*
121  * Template for instructions.
122  *
123  * ipfw_insn is used for all instructions which require no operands,
124  * a single 16-bit value (arg1), or a couple of 8-bit values.
125  *
126  * For other instructions which require different/larger arguments
127  * we have derived structures, ipfw_insn_*.
128  *
129  * The size of the instruction (in 32-bit words) is in the low
130  * 6 bits of "len". The 2 remaining bits are used to implement
131  * NOT and OR on individual instructions. Given a type, you can
132  * compute the length to be put in "len" using F_INSN_SIZE(t)
133  *
134  * F_NOT        negates the match result of the instruction.
135  *
136  * F_OR         is used to build or blocks. By default, instructions
137  *              are evaluated as part of a logical AND. An "or" block
138  *              { X or Y or Z } contains F_OR set in all but the last
139  *              instruction of the block. A match will cause the code
140  *              to skip past the last instruction of the block.
141  *
142  * NOTA BENE: in a couple of places we assume that
143  *      sizeof(ipfw_insn) == sizeof(uint32_t)
144  * this needs to be fixed.
145  *
146  */
147 typedef struct  _ipfw_insn {    /* template for instructions */
148         enum ipfw_opcodes       opcode:8;
149         uint8_t         len;    /* numer of 32-byte words */
150 #define F_NOT           0x80
151 #define F_OR            0x40
152 #define F_LEN_MASK      0x3f
153 #define F_LEN(cmd)      ((cmd)->len & F_LEN_MASK)
154
155         uint16_t        arg1;
156 } ipfw_insn;
157
158 /*
159  * The F_INSN_SIZE(type) computes the size, in 4-byte words, of
160  * a given type.
161  */
162 #define F_INSN_SIZE(t)  ((sizeof (t))/sizeof(uint32_t))
163
164 /*
165  * This is used to store an array of 16-bit entries (ports etc.)
166  */
167 typedef struct  _ipfw_insn_u16 {
168         ipfw_insn o;
169         uint16_t ports[2];      /* there may be more */
170 } ipfw_insn_u16;
171
172 /*
173  * This is used to store an array of 32-bit entries
174  * (uid, single IPv4 addresses etc.)
175  */
176 typedef struct  _ipfw_insn_u32 {
177         ipfw_insn o;
178         uint32_t d[1];  /* one or more */
179 } ipfw_insn_u32;
180
181 /*
182  * This is used to store IP addr-mask pairs.
183  */
184 typedef struct  _ipfw_insn_ip {
185         ipfw_insn o;
186         struct in_addr  addr;
187         struct in_addr  mask;
188 } ipfw_insn_ip;
189
190 /*
191  * This is used to forward to a given address (ip)
192  */
193 typedef struct  _ipfw_insn_sa {
194         ipfw_insn o;
195         struct sockaddr_in sa;
196 } ipfw_insn_sa;
197
198 /*
199  * This is used for MAC addr-mask pairs.
200  */
201 typedef struct  _ipfw_insn_mac {
202         ipfw_insn o;
203         u_char addr[12];        /* dst[6] + src[6] */
204         u_char mask[12];        /* dst[6] + src[6] */
205 } ipfw_insn_mac;
206
207 /*
208  * This is used for interface match rules (recv xx, xmit xx)
209  */
210 typedef struct  _ipfw_insn_if {
211         ipfw_insn o;
212         union {
213                 struct in_addr ip;
214                 int glob;
215         } p;
216         char name[IFNAMSIZ];
217 } ipfw_insn_if;
218
219 /*
220  * This is used for pipe and queue actions, which need to store
221  * a single pointer (which can have different size on different
222  * architectures.
223  */
224 typedef struct  _ipfw_insn_pipe {
225         ipfw_insn       o;
226         void            *pipe_ptr;
227 } ipfw_insn_pipe;
228
229 /*
230  * This is used for limit rules.
231  */
232 typedef struct  _ipfw_insn_limit {
233         ipfw_insn o;
234         uint8_t _pad;
235         uint8_t limit_mask;     /* combination of DYN_* below   */
236 #define DYN_SRC_ADDR    0x1
237 #define DYN_SRC_PORT    0x2
238 #define DYN_DST_ADDR    0x4
239 #define DYN_DST_PORT    0x8
240
241         uint16_t conn_limit;
242 } ipfw_insn_limit;
243
244 /*
245  * This is used for log instructions
246  */
247 typedef struct  _ipfw_insn_log {
248         ipfw_insn o;
249         uint32_t max_log;       /* how many do we log -- 0 = all */
250         uint32_t log_left;      /* how many left to log         */
251 } ipfw_insn_log;
252
253 #ifdef _KERNEL
254
255 /*
256  * Here we have the structure representing an ipfw rule.
257  *
258  * It starts with a general area (with link fields and counters)
259  * followed by an array of one or more instructions, which the code
260  * accesses as an array of 32-bit values.
261  *
262  * Given a rule pointer  r:
263  *
264  *  r->cmd              is the start of the first instruction.
265  *  ACTION_PTR(r)       is the start of the first action (things to do
266  *                      once a rule matched).
267  *
268  * When assembling instruction, remember the following:
269  *
270  *  + if a rule has a "keep-state" (or "limit") option, then the
271  *      first instruction (at r->cmd) MUST BE an O_PROBE_STATE
272  *  + if a rule has a "log" option, then the first action
273  *      (at ACTION_PTR(r)) MUST be O_LOG
274  *
275  * NOTE: we use a simple linked list of rules because we never need
276  *      to delete a rule without scanning the list. We do not use
277  *      queue(3) macros for portability and readability.
278  */
279
280 struct ip_fw {
281         struct ip_fw    *next;          /* linked list of rules         */
282         struct ip_fw    *next_rule;     /* ptr to next [skipto] rule    */
283         uint16_t        act_ofs;        /* offset of action in 32-bit units */
284         uint16_t        cmd_len;        /* # of 32-bit words in cmd     */
285         uint16_t        rulenum;        /* rule number                  */
286         uint8_t         set;            /* rule set (0..31)             */
287         uint8_t         usr_flags;      /* IPFW_USR_F_                  */
288
289         /* These fields are present in all rules.                       */
290         uint64_t        pcnt;           /* Packet counter               */
291         uint64_t        bcnt;           /* Byte counter                 */
292         uint32_t        timestamp;      /* tv_sec of last match         */
293
294         struct ip_fw    *sibling;       /* clone on next cpu            */
295         int             cpuid;          /* owner cpu                    */
296
297         uint32_t        refcnt;         /* Ref count for transit pkts   */
298         uint32_t        rule_flags;     /* IPFW_RULE_F_                 */
299         uintptr_t       track_ruleid;   /* ruleid for src/dst tracks    */
300
301         ipfw_insn       cmd[1];         /* storage for commands         */
302 };
303
304 #define IPFW_RULE_F_INVALID     0x1
305 /* unused                       0x2 */
306 #define IPFW_RULE_F_GENSTATE    0x4
307 #define IPFW_RULE_F_GENTRACK    0x8
308
309 #define RULESIZE(rule)  (sizeof(struct ip_fw) + (rule)->cmd_len * 4 - 4)
310
311 /*
312  * This structure is used as a flow mask and a flow id for various
313  * parts of the code.
314  */
315 struct ipfw_flow_id {
316         uint32_t        dst_ip;
317         uint32_t        src_ip;
318         uint16_t        dst_port;
319         uint16_t        src_port;
320         uint8_t         proto;
321         uint8_t         flags;  /* protocol-specific flags */
322 };
323
324 /*
325  * Main firewall chains definitions and global var's definitions.
326  */
327
328 /* ipfw_chk/ip_fw_chk_ptr return values */
329 #define IP_FW_PASS      0
330 #define IP_FW_DENY      1
331 #define IP_FW_DIVERT    2
332 #define IP_FW_TEE       3
333 #define IP_FW_DUMMYNET  4
334
335 /*
336  * arguments for calling ipfw_chk() and dummynet_io(). We put them
337  * all into a structure because this way it is easier and more
338  * efficient to pass variables around and extend the interface.
339  */
340 struct ip_fw_args {
341         struct mbuf     *m;             /* the mbuf chain               */
342         struct ifnet    *oif;           /* output interface             */
343         struct ip_fw    *rule;          /* matching rule                */
344         struct ether_header *eh;        /* for bridged packets          */
345
346         struct ipfw_flow_id f_id;       /* grabbed from IP header       */
347
348         /*
349          * Depend on the return value of ipfw_chk/ip_fw_chk_ptr
350          * 'cookie' field may save following information:
351          *
352          * IP_FW_TEE or IP_FW_DIVERT
353          *   The divert port number
354          *
355          * IP_FW_DUMMYNET
356          *   The pipe or queue number
357          */
358         uint32_t        cookie;
359 };
360
361 /*
362  * Function definitions.
363  */
364 int     ip_fw_sockopt(struct sockopt *);
365
366 /* Firewall hooks */
367 struct sockopt;
368 struct dn_flow_set;
369
370 typedef int     ip_fw_chk_t(struct ip_fw_args *);
371 typedef int     ip_fw_ctl_t(struct sockopt *);
372 typedef void    ip_fw_dn_io_t(struct mbuf *, int, int, struct ip_fw_args *);
373
374 extern ip_fw_chk_t      *ip_fw_chk_ptr;
375 extern ip_fw_ctl_t      *ip_fw_ctl_ptr;
376 extern ip_fw_dn_io_t    *ip_fw_dn_io_ptr;
377
378 extern int fw_one_pass;
379 extern int fw_enable;
380
381 extern int ip_fw_loaded;
382 #define IPFW_LOADED     (ip_fw_loaded)
383
384 #endif /* _KERNEL */
385
386 #define ACTION_PTR(rule)        \
387         (ipfw_insn *)((uint32_t *)((rule)->cmd) + ((rule)->act_ofs))
388
389 struct ipfw_ioc_rule {
390         uint16_t        act_ofs;        /* offset of action in 32-bit units */
391         uint16_t        cmd_len;        /* # of 32-bit words in cmd     */
392         uint16_t        rulenum;        /* rule number                  */
393         uint8_t         set;            /* rule set (0..31)             */
394         uint8_t         usr_flags;      /* IPFW_USR_F_                  */
395
396         /* Rule set information */
397         uint32_t        set_disable;    /* disabled rule sets           */
398         uint32_t        static_count;   /* # of static rules            */
399         uint32_t        static_len;     /* total length of static rules */
400
401         /* Statistics */
402         uint64_t        pcnt;           /* Packet counter               */
403         uint64_t        bcnt;           /* Byte counter                 */
404         uint32_t        timestamp;      /* tv_sec of last match         */
405
406         uint8_t         reserved[16];
407
408         ipfw_insn       cmd[1];         /* storage for commands         */
409 };
410
411 #define IPFW_USR_F_NORULE       0x01
412
413 #define IPFW_RULE_SIZE_MAX      255     /* unit: uint32_t */
414
415 #define IOC_RULESIZE(rule)      \
416         (sizeof(struct ipfw_ioc_rule) + (rule)->cmd_len * 4 - 4)
417
418 struct ipfw_ioc_flowid {
419         uint16_t        type;   /* ETHERTYPE_ */
420         uint16_t        pad;
421         union {
422                 struct {
423                         uint32_t dst_ip;
424                         uint32_t src_ip;
425                         uint16_t dst_port;
426                         uint16_t src_port;
427                         uint8_t proto;
428                 } ip;
429                 uint8_t pad[64];
430         } u;
431 };
432
433 struct ipfw_ioc_state {
434         uint32_t        expire;         /* expire time                  */
435         uint64_t        pcnt;           /* packet match counter         */
436         uint64_t        bcnt;           /* byte match counter           */
437
438         uint16_t        dyn_type;       /* rule type                    */
439         uint16_t        count;          /* refcount                     */
440
441         uint16_t        rulenum;
442         uint16_t        pad;
443
444         int             cpu;            /* reserved                     */
445
446         struct ipfw_ioc_flowid id;      /* (masked) flow id             */
447         uint8_t         reserved[16];
448 };
449
450 /*
451  * Definitions for IP option names.
452  */
453 #define IP_FW_IPOPT_LSRR        0x01
454 #define IP_FW_IPOPT_SSRR        0x02
455 #define IP_FW_IPOPT_RR          0x04
456 #define IP_FW_IPOPT_TS          0x08
457
458 /*
459  * Definitions for TCP option names.
460  */
461 #define IP_FW_TCPOPT_MSS        0x01
462 #define IP_FW_TCPOPT_WINDOW     0x02
463 #define IP_FW_TCPOPT_SACK       0x04
464 #define IP_FW_TCPOPT_TS         0x08
465 #define IP_FW_TCPOPT_CC         0x10
466
467 #define ICMP_REJECT_RST         0x100   /* fake ICMP code (send a TCP RST) */
468
469 /*
470  * IP_FW_TBL_CREATE, tableid >= 0.
471  * IP_FW_TBL_FLUSH, tableid >= 0.
472  * IP_FW_TBL_FLUSH, tableid < 0, flush all tables.
473  * IP_FW_TBL_DESTROY, tableid >= 0.
474  * IP_FW_TBL_ZERO, tableid >= 0.
475  * IP_FW_TBL_ZERO, tableid < 0, zero all tables' counters.
476  */
477 struct ipfw_ioc_table {
478         int             tableid;
479 };
480
481 struct ipfw_ioc_tblent {
482         struct sockaddr_in key;
483         struct sockaddr_in netmask;
484         u_long          use;
485         time_t          last_used;
486         long            unused[2];
487 };
488
489 /*
490  * IP_FW_TBL_GET, tableid < 0, list of all tables.
491  */
492 struct ipfw_ioc_tbllist {
493         int             tableid;        /* MUST be the first field */
494         int             tablecnt;
495         uint16_t        tables[];
496 };
497
498 /*
499  * IP_FW_TBL_GET, tableid >= 0, entries in the table.
500  * IP_FW_TBL_ADD, tableid >= 0, entcnt == 1.
501  * IP_FW_TBL_DEL, tableid >= 0, entcnt == 1.
502  */
503 struct ipfw_ioc_tblcont {
504         int             tableid;        /* MUST be the first field */
505         int             entcnt;
506         struct ipfw_ioc_tblent ent[1];
507 };
508
509 /*
510  * IP_FW_TBL_EXPIRE, tableid < 0, expire all tables.
511  * IP_FW_TBL_EXPIRE, tableid >= 0.
512  */
513 struct ipfw_ioc_tblexp {
514         int             tableid;
515         int             expcnt;
516         time_t          expire;
517         u_long          unused1[2];
518 };
519
520 #endif /* _IPFW2_H */