2 * Copyright (c) 2015 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Bill Yuan <bycn82@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
40 #include <net/netisr2.h>
42 int ip_fw3_sockopt(struct sockopt *);
43 extern int ip_fw3_loaded;
47 #define IPFW3_LOADED (ip_fw3_loaded)
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
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 */
63 * Template for instructions.
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.
68 * For other instructions which require different/larger arguments
69 * we have derived structures, ipfw_insn_*.
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)
76 * F_NOT negates the match result of the instruction.
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.
84 * NOTA BENE: in a couple of places we assume that
85 * sizeof(ipfw_insn) == sizeof(uint32_t)
86 * this needs to be fixed.
92 #define F_LEN_MASK 0x3f
93 #define F_LEN(cmd) ((cmd)->len & F_LEN_MASK)
95 typedef struct _ipfw_insn { /* template for instructions */
97 uint8_t len; /* numer of 32-byte words */
106 * The F_INSN_SIZE(type) computes the size, in 4-byte words, of
109 #define F_INSN_SIZE(t) ((sizeof (t))/sizeof(uint32_t))
111 #define MTAG_IPFW 1148380143 /* IPFW-tagged cookie */
114 * This is used to store an array of 16-bit entries (ports etc.)
116 typedef struct _ipfw_insn_u16 {
118 uint16_t ports[2]; /* there may be more */
122 * This is used to store an array of 32-bit entries
123 * (uid, single IPv4 addresses etc.)
125 typedef struct _ipfw_insn_u32 {
127 uint32_t d[1]; /* one or more */
131 * This is used to store IP addr-mask pairs.
133 typedef struct _ipfw_insn_ip {
140 * This is used to forward to a given address (ip)
142 typedef struct _ipfw_insn_sa {
144 struct sockaddr_in sa;
148 * This is used for MAC addr-mask pairs.
150 typedef struct _ipfw_insn_mac {
152 u_char addr[12]; /* dst[6] + src[6] */
153 u_char mask[12]; /* dst[6] + src[6] */
157 * This is used for interface match rules (recv xx, xmit xx)
159 typedef struct _ipfw_insn_if {
169 * This is used for pipe and queue actions, which need to store
170 * a single pointer (which can have different size on different
173 typedef struct _ipfw_insn_pipe {
179 * This is used for limit rules.
181 typedef struct _ipfw_insn_limit {
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
195 * Here we have the structure representing an ipfw rule.
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.
201 * Given a rule pointer r:
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).
207 * When assembling instruction, remember the following:
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
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.
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_ */
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 */
233 struct ip_fw *sibling; /* pointer to the rule in next CPU */
235 ipfw_insn cmd[1]; /* storage for commands */
239 #define IPFW_RULE_F_INVALID 0x1
240 #define IPFW_RULE_F_STATE 0x2
242 #define RULESIZE(rule) (sizeof(struct ip_fw) + (rule)->cmd_len * 4 - SIZE_OF_IPFWINSN)
245 * This structure is used as a flow mask and a flow id for various
248 struct ipfw_flow_id {
254 uint8_t flags; /* protocol-specific flags */
258 struct ip_fw_state *next;
259 struct ipfw_flow_id flow_id;
262 uint64_t pcnt; /* packet match counter */
263 uint64_t bcnt; /* byte match counter */
271 /* ipfw_chk/ip_fw_chk_ptr return values */
274 #define IP_FW_DIVERT 2
276 #define IP_FW_DUMMYNET 4
278 #define IP_FW_ROUTE 6
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
289 #define IP_FW_NOT_MATCH 0
290 #define IP_FW_MATCH 1
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.
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 */
303 struct ipfw_flow_id f_id; /* grabbed from IP header */
306 * Depend on the return value of ipfw_chk/ip_fw_chk_ptr
307 * 'cookie' field may save following information:
309 * IP_FW_TEE or IP_FW_DIVERT
310 * The divert port number
313 * The pipe or queue number
320 * Function definitions.
322 int ip_fw_sockopt(struct sockopt *);
323 int ipfw_ctl_x(struct sockopt *sopt);
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 *);
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;
339 extern int fw3_one_pass;
340 extern int fw3_enable;
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"))
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;
357 struct ipfw_state_context {
358 struct ip_fw_state *state;
359 struct ip_fw_state *last;
363 struct ipfw_nat_context {
364 LIST_HEAD(, cfg_nat) nat; /* list of nat entries */
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);
376 #define ACTION_PTR(rule) \
377 (ipfw_insn *)((uint32_t *)((rule)->cmd) + ((rule)->act_ofs))
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_ */
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 */
394 uint64_t pcnt; /* Packet counter */
395 uint64_t bcnt; /* Byte counter */
396 uint32_t timestamp; /* tv_sec of last match */
398 uint8_t reserved[RESERVED_SIZE];
400 ipfw_insn cmd[1]; /* storage for commands */
403 #define IPFW_USR_F_NORULE 0x01
405 #define IPFW_RULE_SIZE_MAX 255 /* unit: uint32_t */
407 #define IOC_RULESIZE(rule) \
408 (sizeof(struct ipfw_ioc_rule) + (rule)->cmd_len * 4 - SIZE_OF_IPFWINSN)
410 struct ipfw_ioc_flowid {
411 uint16_t type; /* ETHERTYPE_ */
425 struct ipfw_ioc_state {
426 uint64_t pcnt; /* packet match counter */
427 uint64_t bcnt; /* byte match counter */
429 uint32_t timestamp; /* alive time */
430 uint32_t expiry; /* expire time */
434 struct ipfw_flow_id flow_id; /* proto +src/dst ip/port */
435 uint8_t reserved[16];
439 * Definitions for IP option names.
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
447 * Definitions for TCP option names.
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
455 #define ICMP_REJECT_RST 0x100 /* fake ICMP code (send a TCP RST) */
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
468 #define IPFW_MAPPING_TYPE_NONE 0
469 #define IPFW_MAPPING_TYPE_IN_USE 1
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);}
475 #define NEXT_ARG ac--; if(ac > 0){av++;}
476 #define NEXT_ARG1 (*ac)--; if(*ac > 0){(*av)++;}
478 #define MATCH_REVERSE 0
479 #define MATCH_FORWARD 1
481 #define MATCH_UNKNOWN 3
483 #define BOTH_SYN (TH_SYN | (TH_SYN << 8))
484 #define BOTH_FIN (TH_FIN | (TH_FIN << 8))
486 #define TIME_LEQ(a, b) ((int)((a) - (b)) <= 0)
487 #define L3HDR(T, ip) ((T *)((uint32_t *)(ip) + (ip)->ip_hl))
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 */
495 typedef void ipfw_basic_delete_state_t(struct ip_fw *);
496 typedef void ipfw_basic_append_state_t(struct ipfw_ioc_state *);
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 */
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 */
512 #define IP_FW_MODULE 67 /* get modules names */
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 */
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 */
525 #endif /* _IPFW3_H_ */