kernel/ipfw3: Fix LINT64 building.
[dragonfly.git] / sys / net / ipfw3 / ip_fw3.h
1 /*
2  * Copyright (c) 2015 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Bill Yuan <bycn82@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35
36 #ifndef _IP_FW3_H_
37 #define _IP_FW3_H_
38
39 #ifdef _KERNEL
40 #include <net/netisr2.h>
41
42 int     ip_fw3_sockopt(struct sockopt *);
43 extern int ip_fw3_loaded;
44
45 #endif
46
47 #define IPFW3_LOADED    (ip_fw3_loaded)
48
49 /*
50  * _IPFW2_H is from ipfw/ip_fw2.h, both cannot be included past this
51  * point but we need both the IPFW2_LOADED and IPFW3_LOADED macros
52  */
53 #ifndef _IPFW2_H
54 #define _IPFW2_H
55
56 #define         RESERVED_SIZE           12
57 #define         SIZE_OF_IPFWINSN        8
58 #define         LEN_OF_IPFWINSN         2
59 #define         IPFW_DEFAULT_RULE       65535   /* rulenum for the default rule */
60 #define         IPFW_DEFAULT_SET        31      /* set number for the default rule  */
61
62 /*
63  * Template for instructions.
64  *
65  * ipfw_insn is used for all instructions which require no operands,
66  * a single 16-bit value (arg1), or a couple of 8-bit values.
67  *
68  * For other instructions which require different/larger arguments
69  * we have derived structures, ipfw_insn_*.
70  *
71  * The size of the instruction (in 32-bit words) is in the low
72  * 6 bits of "len". The 2 remaining bits are used to implement
73  * NOT and OR on individual instructions. Given a type, you can
74  * compute the length to be put in "len" using F_INSN_SIZE(t)
75  *
76  * F_NOT        negates the match result of the instruction.
77  *
78  * F_OR         is used to build or blocks. By default, instructions
79  *              are evaluated as part of a logical AND. An "or" block
80  *              { X or Y or Z } contains F_OR set in all but the last
81  *              instruction of the block. A match will cause the code
82  *              to skip past the last instruction of the block.
83  *
84  * NOTA BENE: in a couple of places we assume that
85  *      sizeof(ipfw_insn) == sizeof(uint32_t)
86  * this needs to be fixed.
87  *
88  */
89
90 #define F_NOT           0x80
91 #define F_OR            0x40
92 #define F_LEN_MASK      0x3f
93 #define F_LEN(cmd)      ((cmd)->len & F_LEN_MASK)
94
95 typedef struct  _ipfw_insn {    /* template for instructions */
96         uint8_t         opcode;
97         uint8_t         len;    /* numer of 32-byte words */
98         uint16_t        arg1;
99
100         uint8_t         module;
101         uint8_t         arg3;
102         uint16_t        arg2;
103 } ipfw_insn;
104
105 /*
106  * The F_INSN_SIZE(type) computes the size, in 4-byte words, of
107  * a given type.
108  */
109 #define F_INSN_SIZE(t)  ((sizeof (t))/sizeof(uint32_t))
110
111 #define MTAG_IPFW       1148380143      /* IPFW-tagged cookie */
112
113 /*
114  * This is used to store an array of 16-bit entries (ports etc.)
115  */
116 typedef struct  _ipfw_insn_u16 {
117         ipfw_insn o;
118         uint16_t ports[2];      /* there may be more */
119 } ipfw_insn_u16;
120
121 /*
122  * This is used to store an array of 32-bit entries
123  * (uid, single IPv4 addresses etc.)
124  */
125 typedef struct  _ipfw_insn_u32 {
126         ipfw_insn o;
127         uint32_t d[1];  /* one or more */
128 } ipfw_insn_u32;
129
130 /*
131  * This is used to store IP addr-mask pairs.
132  */
133 typedef struct  _ipfw_insn_ip {
134         ipfw_insn o;
135         struct in_addr  addr;
136         struct in_addr  mask;
137 } ipfw_insn_ip;
138
139 /*
140  * This is used to forward to a given address (ip)
141  */
142 typedef struct  _ipfw_insn_sa {
143         ipfw_insn o;
144         struct sockaddr_in sa;
145 } ipfw_insn_sa;
146
147 /*
148  * This is used for MAC addr-mask pairs.
149  */
150 typedef struct  _ipfw_insn_mac {
151         ipfw_insn o;
152         u_char addr[12];        /* dst[6] + src[6] */
153         u_char mask[12];        /* dst[6] + src[6] */
154 } ipfw_insn_mac;
155
156 /*
157  * This is used for interface match rules (recv xx, xmit xx)
158  */
159 typedef struct  _ipfw_insn_if {
160         ipfw_insn o;
161         union {
162                 struct in_addr ip;
163                 int glob;
164         } p;
165         char name[IFNAMSIZ];
166 } ipfw_insn_if;
167
168 /*
169  * This is used for pipe and queue actions, which need to store
170  * a single pointer (which can have different size on different
171  * architectures.
172  */
173 typedef struct  _ipfw_insn_pipe {
174         ipfw_insn       o;
175         void            *pipe_ptr;
176 } ipfw_insn_pipe;
177
178 /*
179  * This is used for limit rules.
180  */
181 typedef struct  _ipfw_insn_limit {
182         ipfw_insn o;
183         uint8_t _pad;
184         uint8_t limit_mask;     /* combination of DYN_* below   */
185 #define DYN_SRC_ADDR    0x1
186 #define DYN_SRC_PORT    0x2
187 #define DYN_DST_ADDR    0x4
188 #define DYN_DST_PORT    0x8
189
190         uint16_t conn_limit;
191 } ipfw_insn_limit;
192
193
194 /*
195  * Here we have the structure representing an ipfw rule.
196  *
197  * It starts with a general area (with link fields and counters)
198  * followed by an array of one or more instructions, which the code
199  * accesses as an array of 32-bit values.
200  *
201  * Given a rule pointer  r:
202  *
203  *  r->cmd              is the start of the first instruction.
204  *  ACTION_PTR(r)       is the start of the first action (things to do
205  *                      once a rule matched).
206  *
207  * When assembling instruction, remember the following:
208  *
209  *  + if a rule has a "keep-state" (or "limit") option, then the
210  *      first instruction (at r->cmd) MUST BE an O_PROBE_STATE
211  *  + if a rule has a "log" option, then the first action
212  *      (at ACTION_PTR(r)) MUST be O_LOG
213  *
214  * NOTE: we use a simple linked list of rules because we never need
215  *      to delete a rule without scanning the list. We do not use
216  *      queue(3) macros for portability and readability.
217  */
218
219 struct ip_fw {
220         struct ip_fw    *next;          /* linked list of rules         */
221         struct ip_fw    *next_rule;     /* ptr to next [skipto] rule    */
222         uint16_t        act_ofs;        /* offset of action in 32-bit units */
223         uint16_t        cmd_len;        /* # of 32-bit words in cmd     */
224         uint16_t        rulenum;        /* rule number                  */
225         uint8_t         set;            /* rule set (0..31)             */
226         uint8_t         flags;          /* IPFW_USR_F_                  */
227
228         /* These fields are present in all rules.                       */
229         uint64_t        pcnt;           /* Packet counter               */
230         uint64_t        bcnt;           /* Byte counter                 */
231         uint32_t        timestamp;      /* tv_sec of last match         */
232
233         struct ip_fw    *sibling;       /* pointer to the rule in next CPU */
234
235         ipfw_insn       cmd[1];         /* storage for commands         */
236 };
237
238
239 #define IPFW_RULE_F_INVALID     0x1
240 #define IPFW_RULE_F_STATE       0x2
241
242 #define RULESIZE(rule) (sizeof(struct ip_fw) + (rule)->cmd_len * 4 - SIZE_OF_IPFWINSN)
243
244 /*
245  * This structure is used as a flow mask and a flow id for various
246  * parts of the code.
247  */
248 struct ipfw_flow_id {
249         uint32_t        dst_ip;
250         uint32_t        src_ip;
251         uint16_t        dst_port;
252         uint16_t        src_port;
253         uint8_t         proto;
254         uint8_t         flags;  /* protocol-specific flags */
255 };
256
257 struct ip_fw_state {
258         struct ip_fw_state      *next;
259         struct ipfw_flow_id     flow_id;
260         struct ip_fw    *stub;
261
262         uint64_t        pcnt;      /* packet match counter       */
263         uint64_t        bcnt;      /* byte match counter           */
264
265         uint16_t        lifetime;
266         uint32_t        timestamp;
267         uint32_t        expiry;
268 };
269
270
271 /* ipfw_chk/ip_fw_chk_ptr return values */
272 #define IP_FW_PASS      0
273 #define IP_FW_DENY      1
274 #define IP_FW_DIVERT    2
275 #define IP_FW_TEE       3
276 #define IP_FW_DUMMYNET  4
277 #define IP_FW_NAT       5
278 #define IP_FW_ROUTE     6
279
280 /* ipfw_chk controller values */
281 #define IP_FW_CTL_NO            0
282 #define IP_FW_CTL_DONE          1
283 #define IP_FW_CTL_AGAIN         2
284 #define IP_FW_CTL_NEXT          3
285 #define IP_FW_CTL_NAT           4
286 #define IP_FW_CTL_LOOP          5
287 #define IP_FW_CTL_CHK_STATE     6
288
289 #define IP_FW_NOT_MATCH         0
290 #define IP_FW_MATCH             1
291
292 /*
293  * arguments for calling ipfw_chk() and dummynet_io(). We put them
294  * all into a structure because this way it is easier and more
295  * efficient to pass variables around and extend the interface.
296  */
297 struct ip_fw_args {
298         struct mbuf     *m;             /* the mbuf chain               */
299         struct ifnet    *oif;           /* output interface             */
300         struct ip_fw    *rule;          /* matching rule                */
301         struct ether_header *eh;        /* for bridged packets          */
302
303         struct ipfw_flow_id f_id;       /* grabbed from IP header       */
304
305         /*
306          * Depend on the return value of ipfw_chk/ip_fw_chk_ptr
307          * 'cookie' field may save following information:
308          *
309          * IP_FW_TEE or IP_FW_DIVERT
310          *   The divert port number
311          *
312          * IP_FW_DUMMYNET
313          *   The pipe or queue number
314          */
315         uint32_t        cookie;
316 };
317
318 #ifdef _KERNEL
319 /*
320  * Function definitions.
321  */
322 int     ip_fw_sockopt(struct sockopt *);
323 int     ipfw_ctl_x(struct sockopt *sopt);
324
325 /* Firewall hooks */
326 struct sockopt;
327 struct dn_flow_set;
328
329 typedef int     ip_fw_chk_t(struct ip_fw_args *);
330 typedef int     ip_fw_ctl_t(struct sockopt *);
331 typedef int     ipfw_nat_cfg_t(struct sockopt *);
332 typedef void ip_fw_dn_io_t(struct mbuf *, int, int, struct ip_fw_args *);
333
334
335 extern ip_fw_chk_t      *ip_fw_chk_ptr;
336 extern ip_fw_ctl_t      *ip_fw_ctl_x_ptr;
337 extern ip_fw_dn_io_t    *ip_fw_dn_io_ptr;
338
339 extern int fw3_one_pass;
340 extern int fw3_enable;
341
342
343 #define IPFW_CFGCPUID   0
344 #define IPFW_CFGPORT    netisr_cpuport(IPFW_CFGCPUID)
345 #define IPFW_ASSERT_CFGPORT(msgport)                            \
346         KASSERT((msgport) == IPFW_CFGPORT, ("not IPFW CFGPORT"))
347
348
349 struct ipfw_context {
350         struct ip_fw    *ipfw_rule_chain;               /* list of rules*/
351         struct ip_fw    *ipfw_default_rule;      /* default rule */
352         struct ipfw_state_context *state_ctx;
353         uint16_t                state_hash_size;
354         uint32_t                ipfw_set_disable;
355 };
356
357 struct ipfw_state_context {
358         struct ip_fw_state *state;
359         struct ip_fw_state *last;
360         int     count;
361 };
362
363 struct ipfw_nat_context {
364         LIST_HEAD(, cfg_nat) nat;               /* list of nat entries */
365 };
366
367 typedef void (*filter_func)(int *cmd_ctl,int *cmd_val,struct ip_fw_args **args,
368 struct ip_fw **f,ipfw_insn *cmd,uint16_t ip_len);
369 void register_ipfw_filter_funcs(int module,int opcode,filter_func func);
370 void unregister_ipfw_filter_funcs(int module,filter_func func);
371 void register_ipfw_module(int module_id,char *module_name);
372 int unregister_ipfw_module(int module_id);
373
374 #endif
375
376 #define ACTION_PTR(rule)        \
377         (ipfw_insn *)((uint32_t *)((rule)->cmd) + ((rule)->act_ofs))
378
379
380
381 struct ipfw_ioc_rule {
382         uint16_t        act_ofs;        /* offset of action in 32-bit units */
383         uint16_t        cmd_len;        /* # of 32-bit words in cmd     */
384         uint16_t        rulenum;        /* rule number                  */
385         uint8_t         set;            /* rule set (0..31)             */
386         uint8_t         usr_flags;      /* IPFW_USR_F_                  */
387
388         /* Rule set information */
389         uint32_t        set_disable;    /* disabled rule sets           */
390         uint32_t        static_count;   /* # of static rules            */
391         uint32_t        static_len;     /* total length of static rules */
392
393         /* Statistics */
394         uint64_t        pcnt;           /* Packet counter               */
395         uint64_t        bcnt;           /* Byte counter                 */
396         uint32_t        timestamp;      /* tv_sec of last match         */
397
398         uint8_t         reserved[RESERVED_SIZE];
399
400         ipfw_insn       cmd[1];         /* storage for commands         */
401 };
402
403 #define IPFW_USR_F_NORULE       0x01
404
405 #define IPFW_RULE_SIZE_MAX      255     /* unit: uint32_t */
406
407 #define IOC_RULESIZE(rule)      \
408         (sizeof(struct ipfw_ioc_rule) + (rule)->cmd_len * 4 - SIZE_OF_IPFWINSN)
409
410 struct ipfw_ioc_flowid {
411         uint16_t        type;   /* ETHERTYPE_ */
412         uint16_t        pad;
413         union {
414                 struct {
415                         uint32_t dst_ip;
416                         uint32_t src_ip;
417                         uint16_t dst_port;
418                         uint16_t src_port;
419                         uint8_t proto;
420                 } ip;
421                 uint8_t pad[64];
422         } u;
423 };
424
425 struct ipfw_ioc_state {
426         uint64_t        pcnt;           /* packet match counter         */
427         uint64_t        bcnt;           /* byte match counter           */
428         uint16_t        lifetime;
429         uint32_t        timestamp;      /* alive time                           */
430         uint32_t        expiry;         /* expire time                          */
431
432         uint16_t        rulenum;
433         uint16_t        cpuid;
434         struct ipfw_flow_id     flow_id;        /* proto +src/dst ip/port */
435         uint8_t         reserved[16];
436 };
437
438 /*
439  * Definitions for IP option names.
440  */
441 #define IP_FW_IPOPT_LSRR        0x01
442 #define IP_FW_IPOPT_SSRR        0x02
443 #define IP_FW_IPOPT_RR          0x04
444 #define IP_FW_IPOPT_TS          0x08
445
446 /*
447  * Definitions for TCP option names.
448  */
449 #define IP_FW_TCPOPT_MSS        0x01
450 #define IP_FW_TCPOPT_WINDOW     0x02
451 #define IP_FW_TCPOPT_SACK       0x04
452 #define IP_FW_TCPOPT_TS         0x08
453 #define IP_FW_TCPOPT_CC         0x10
454
455 #define ICMP_REJECT_RST         0x100   /* fake ICMP code (send a TCP RST) */
456
457 struct ipfw_module{
458         int type;
459         int id;
460         char name[20];
461 };
462
463 #define IPFW_KEYWORD_TYPE_NONE          0
464 #define IPFW_KEYWORD_TYPE_ACTION        1
465 #define IPFW_KEYWORD_TYPE_FILTER        2
466 #define IPFW_KEYWORD_TYPE_OTHERS        3
467
468 #define IPFW_MAPPING_TYPE_NONE          0
469 #define IPFW_MAPPING_TYPE_IN_USE        1
470
471 #define NEED1(msg)  {if (ac < 1) errx(EX_USAGE, msg);}
472 #define NEED2(msg)  {if (ac < 2) errx(EX_USAGE, msg);}
473 #define NEED(c, n, msg) {if (c < n) errx(EX_USAGE, msg);}
474
475 #define NEXT_ARG        ac--; if(ac > 0){av++;}
476 #define NEXT_ARG1       (*ac)--; if(*ac > 0){(*av)++;}
477
478 #define MATCH_REVERSE   0
479 #define MATCH_FORWARD   1
480 #define MATCH_NONE      2
481 #define MATCH_UNKNOWN   3
482
483 #define BOTH_SYN        (TH_SYN | (TH_SYN << 8))
484 #define BOTH_FIN        (TH_FIN | (TH_FIN << 8))
485
486 #define TIME_LEQ(a, b)  ((int)((a) - (b)) <= 0)
487 #define L3HDR(T, ip) ((T *)((uint32_t *)(ip) + (ip)->ip_hl))
488
489 /* IP_FW_X header/opcodes */
490 typedef struct _ip_fw_x_header {
491         uint16_t opcode;        /* Operation opcode */
492         uint16_t _pad;          /* Opcode version */
493 } ip_fw_x_header;
494
495 typedef void ipfw_basic_delete_state_t(struct ip_fw *);
496 typedef void ipfw_basic_append_state_t(struct ipfw_ioc_state *);
497
498 /* IP_FW3 opcodes */
499
500 #define IP_FW_ADD               50   /* add a firewall rule to chain */
501 #define IP_FW_DEL               51   /* delete a firewall rule from chain */
502 #define IP_FW_FLUSH             52   /* flush firewall rule chain */
503 #define IP_FW_ZERO              53   /* clear single/all firewall counter(s) */
504 #define IP_FW_GET               54   /* get entire firewall rule chain */
505 #define IP_FW_RESETLOG          55   /* reset logging counters */
506
507 #define IP_DUMMYNET_CONFIGURE   60   /* add/configure a dummynet pipe */
508 #define IP_DUMMYNET_DEL         61   /* delete a dummynet pipe from chain */
509 #define IP_DUMMYNET_FLUSH       62   /* flush dummynet */
510 #define IP_DUMMYNET_GET         64   /* get entire dummynet pipes */
511
512 #define IP_FW_MODULE            67  /* get modules names */
513
514 #define IP_FW_NAT_CFG           68   /* add/config a nat rule */
515 #define IP_FW_NAT_DEL           69   /* delete a nat rule */
516 #define IP_FW_NAT_FLUSH         70   /* get configuration of a nat rule */
517 #define IP_FW_NAT_GET           71   /* get log of a nat rule */
518 #define IP_FW_NAT_LOG           72   /* get log of a nat rule */
519
520 #define IP_FW_STATE_ADD         56   /* add one state */
521 #define IP_FW_STATE_DEL         57   /* delete states of one rulenum */
522 #define IP_FW_STATE_FLUSH       58   /* flush all states */
523 #endif
524
525 #endif /* _IPFW3_H_ */