2 * Copyright (c) 1993 Daniel Boulet
3 * Copyright (c) 1994 Ugen J.S.Antsilevich
4 * Copyright (c) 2002 Luigi Rizzo, Universita` di Pisa
5 * Copyright (c) 2015 The DragonFly Project. All rights reserved.
7 * This code is derived from software contributed to The DragonFly Project
8 * by Bill Yuan <bycn82@gmail.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
20 * 3. Neither the name of The DragonFly Project nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific, prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 #include <net/netisr2.h>
46 int ip_fw3_sockopt(struct sockopt
*);
47 extern int ip_fw3_loaded
;
51 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
54 #define IPFW3_LOADED (ip_fw3_loaded)
57 * _IPFW2_H is from ipfw/ip_fw2.h, both cannot be included past this
58 * point but we need both the IPFW2_LOADED and IPFW3_LOADED macros
63 #define RESERVED_SIZE 12
64 #define SIZE_OF_IPFWINSN 8
65 #define LEN_OF_IPFWINSN 2
66 #define IPFW_DEFAULT_RULE 65535 /* rulenum for the default rule */
67 #define IPFW_DEFAULT_SET 31 /* set number for the default rule */
70 * Template for instructions.
72 * ipfw_insn is used for all instructions which require no operands,
73 * a single 16-bit value (arg1), or a couple of 8-bit values.
75 * For other instructions which require different/larger arguments
76 * we have derived structures, ipfw_insn_*.
78 * The size of the instruction (in 32-bit words) is in the low
79 * 6 bits of "len". The 2 remaining bits are used to implement
80 * NOT and OR on individual instructions. Given a type, you can
81 * compute the length to be put in "len" using F_INSN_SIZE(t)
83 * F_NOT negates the match result of the instruction.
85 * F_OR is used to build or blocks. By default, instructions
86 * are evaluated as part of a logical AND. An "or" block
87 * { X or Y or Z } contains F_OR set in all but the last
88 * instruction of the block. A match will cause the code
89 * to skip past the last instruction of the block.
91 * NOTA BENE: in a couple of places we assume that
92 * sizeof(ipfw_insn) == sizeof(uint32_t)
93 * this needs to be fixed.
99 #define F_LEN_MASK 0x3f
100 #define F_LEN(cmd) ((cmd)->len & F_LEN_MASK)
102 typedef struct _ipfw_insn
{ /* template for instructions */
104 uint8_t len
; /* numer of 32-byte words */
113 * The F_INSN_SIZE(type) computes the size, in 4-byte words, of
116 #define F_INSN_SIZE(t) ((sizeof (t))/sizeof(uint32_t))
118 #define MTAG_IPFW 1148380143 /* IPFW-tagged cookie */
121 * This is used to store an array of 16-bit entries (ports etc.)
123 typedef struct _ipfw_insn_u16
{
125 uint16_t ports
[2]; /* there may be more */
129 * This is used to store an array of 32-bit entries
130 * (uid, single IPv4 addresses etc.)
132 typedef struct _ipfw_insn_u32
{
134 uint32_t d
[1]; /* one or more */
138 * This is used to store IP addr-mask pairs.
140 typedef struct _ipfw_insn_ip
{
147 * This is used to forward to a given address (ip)
149 typedef struct _ipfw_insn_sa
{
151 struct sockaddr_in sa
;
155 * This is used for MAC addr-mask pairs.
157 typedef struct _ipfw_insn_mac
{
159 u_char addr
[12]; /* dst[6] + src[6] */
160 u_char mask
[12]; /* dst[6] + src[6] */
164 * This is used for interface match rules (recv xx, xmit xx)
166 typedef struct _ipfw_insn_if
{
176 * This is used for pipe and queue actions, which need to store
177 * a single pointer (which can have different size on different
180 typedef struct _ipfw_insn_pipe
{
186 * This is used for limit rules.
188 typedef struct _ipfw_insn_limit
{
191 uint8_t limit_mask
; /* combination of DYN_* below */
192 #define DYN_SRC_ADDR 0x1
193 #define DYN_SRC_PORT 0x2
194 #define DYN_DST_ADDR 0x4
195 #define DYN_DST_PORT 0x8
201 * This is used for bpf filtering.
203 typedef struct _ipfw_insn_bpf
{
207 struct bpf_insn bf_insn
[1];
211 * Here we have the structure representing an ipfw rule.
213 * It starts with a general area (with link fields and counters)
214 * followed by an array of one or more instructions, which the code
215 * accesses as an array of 32-bit values.
217 * Given a rule pointer r:
219 * r->cmd is the start of the first instruction.
220 * ACTION_PTR(r) is the start of the first action (things to do
221 * once a rule matched).
223 * When assembling instruction, remember the following:
225 * + if a rule has a "keep-state" (or "limit") option, then the
226 * first instruction (at r->cmd) MUST BE an O_PROBE_STATE
227 * + if a rule has a "log" option, then the first action
228 * (at ACTION_PTR(r)) MUST be O_LOG
230 * NOTE: we use a simple linked list of rules because we never need
231 * to delete a rule without scanning the list. We do not use
232 * queue(3) macros for portability and readability.
236 struct ip_fw
*next
; /* linked list of rules */
237 struct ip_fw
*next_rule
; /* ptr to next [skipto] rule */
238 uint16_t act_ofs
; /* offset of action in 32-bit units */
239 uint16_t cmd_len
; /* # of 32-bit words in cmd */
240 uint16_t rulenum
; /* rule number */
241 uint8_t set
; /* rule set (0..31) */
242 uint8_t flags
; /* IPFW_USR_F_ */
244 /* These fields are present in all rules. */
245 uint64_t pcnt
; /* Packet counter */
246 uint64_t bcnt
; /* Byte counter */
247 uint32_t timestamp
; /* tv_sec of last match */
249 struct ip_fw
*sibling
; /* pointer to the rule in next CPU */
251 ipfw_insn cmd
[1]; /* storage for commands */
255 #define IPFW_RULE_F_INVALID 0x1
256 #define IPFW_RULE_F_STATE 0x2
258 #define RULESIZE(rule) (sizeof(struct ip_fw) + (rule)->cmd_len * 4 - SIZE_OF_IPFWINSN)
261 * This structure is used as a flow mask and a flow id for various
264 struct ipfw_flow_id
{
270 uint8_t flags
; /* protocol-specific flags */
274 struct ip_fw_state
*next
;
275 struct ipfw_flow_id flow_id
;
278 uint64_t pcnt
; /* packet match counter */
279 uint64_t bcnt
; /* byte match counter */
287 /* ipfw_chk/ip_fw_chk_ptr return values */
290 #define IP_FW_DIVERT 2
292 #define IP_FW_DUMMYNET 4
294 #define IP_FW_ROUTE 6
296 /* ipfw_chk controller values */
297 #define IP_FW_CTL_NO 0
298 #define IP_FW_CTL_DONE 1
299 #define IP_FW_CTL_AGAIN 2
300 #define IP_FW_CTL_NEXT 3
301 #define IP_FW_CTL_NAT 4
302 #define IP_FW_CTL_LOOP 5
303 #define IP_FW_CTL_CHK_STATE 6
305 #define IP_FW_NOT_MATCH 0
306 #define IP_FW_MATCH 1
309 * arguments for calling ipfw_chk() and dummynet_io(). We put them
310 * all into a structure because this way it is easier and more
311 * efficient to pass variables around and extend the interface.
314 struct mbuf
*m
; /* the mbuf chain */
315 struct ifnet
*oif
; /* output interface */
316 struct ip_fw
*rule
; /* matching rule */
317 struct ether_header
*eh
; /* for bridged packets */
319 struct ipfw_flow_id f_id
; /* grabbed from IP header */
322 * Depend on the return value of ipfw_chk/ip_fw_chk_ptr
323 * 'cookie' field may save following information:
325 * IP_FW_TEE or IP_FW_DIVERT
326 * The divert port number
329 * The pipe or queue number
336 * Function definitions.
338 int ip_fw_sockopt(struct sockopt
*);
339 int ipfw_ctl_x(struct sockopt
*sopt
);
345 typedef int ip_fw_chk_t(struct ip_fw_args
*);
346 typedef int ip_fw_ctl_t(struct sockopt
*);
347 typedef int ipfw_nat_cfg_t(struct sockopt
*);
348 typedef void ip_fw_dn_io_t(struct mbuf
*, int, int, struct ip_fw_args
*);
351 extern ip_fw_chk_t
*ip_fw_chk_ptr
;
352 extern ip_fw_ctl_t
*ip_fw_ctl_x_ptr
;
353 extern ip_fw_dn_io_t
*ip_fw_dn_io_ptr
;
355 extern int fw3_one_pass
;
356 extern int fw3_enable
;
359 #define IPFW_CFGCPUID 0
360 #define IPFW_CFGPORT netisr_cpuport(IPFW_CFGCPUID)
361 #define IPFW_ASSERT_CFGPORT(msgport) \
362 KASSERT((msgport) == IPFW_CFGPORT, ("not IPFW CFGPORT"))
364 #define IPFW_TABLES_MAX 32
366 /* root of place holding all information, per-cpu */
367 struct ipfw_context
{
368 struct ip_fw
*ipfw_rule_chain
; /* list of rules*/
369 struct ip_fw
*ipfw_default_rule
; /* default rule */
370 struct ipfw_state_context
*state_ctx
;
371 struct ipfw_table_context
*table_ctx
;
372 uint16_t state_hash_size
;
373 uint32_t ipfw_set_disable
;
376 /* place to hold the states */
377 struct ipfw_state_context
{
378 struct ip_fw_state
*state
;
379 struct ip_fw_state
*last
;
383 /* place to hold the nat conf */
384 struct ipfw_nat_context
{
385 LIST_HEAD(, cfg_nat
) nat
; /* list of nat entries */
388 typedef void (*filter_func
)(int *cmd_ctl
,int *cmd_val
,struct ip_fw_args
**args
,
389 struct ip_fw
**f
,ipfw_insn
*cmd
,uint16_t ip_len
);
390 void register_ipfw_filter_funcs(int module
,int opcode
,filter_func func
);
391 void unregister_ipfw_filter_funcs(int module
,filter_func func
);
392 void register_ipfw_module(int module_id
,char *module_name
);
393 int unregister_ipfw_module(int module_id
);
397 #define ACTION_PTR(rule) \
398 (ipfw_insn *)((uint32_t *)((rule)->cmd) + ((rule)->act_ofs))
402 struct ipfw_ioc_rule
{
403 uint16_t act_ofs
; /* offset of action in 32-bit units */
404 uint16_t cmd_len
; /* # of 32-bit words in cmd */
405 uint16_t rulenum
; /* rule number */
406 uint8_t set
; /* rule set (0..31) */
407 uint8_t usr_flags
; /* IPFW_USR_F_ */
409 /* Rule set information */
410 uint32_t set_disable
; /* disabled rule sets */
411 uint32_t static_count
; /* # of static rules */
412 uint32_t static_len
; /* total length of static rules */
415 uint64_t pcnt
; /* Packet counter */
416 uint64_t bcnt
; /* Byte counter */
417 uint32_t timestamp
; /* tv_sec of last match */
419 uint8_t reserved
[RESERVED_SIZE
];
421 ipfw_insn cmd
[1]; /* storage for commands */
424 #define IPFW_USR_F_NORULE 0x01
426 #define IPFW_RULE_SIZE_MAX 255 /* unit: uint32_t */
428 #define IOC_RULESIZE(rule) \
429 (sizeof(struct ipfw_ioc_rule) + (rule)->cmd_len * 4 - SIZE_OF_IPFWINSN)
431 struct ipfw_ioc_flowid
{
432 uint16_t type
; /* ETHERTYPE_ */
446 struct ipfw_ioc_state
{
447 uint64_t pcnt
; /* packet match counter */
448 uint64_t bcnt
; /* byte match counter */
450 uint32_t timestamp
; /* alive time */
451 uint32_t expiry
; /* expire time */
455 struct ipfw_flow_id flow_id
; /* proto +src/dst ip/port */
456 uint8_t reserved
[16];
460 * Definitions for IP option names.
462 #define IP_FW_IPOPT_LSRR 0x01
463 #define IP_FW_IPOPT_SSRR 0x02
464 #define IP_FW_IPOPT_RR 0x04
465 #define IP_FW_IPOPT_TS 0x08
468 * Definitions for TCP option names.
470 #define IP_FW_TCPOPT_MSS 0x01
471 #define IP_FW_TCPOPT_WINDOW 0x02
472 #define IP_FW_TCPOPT_SACK 0x04
473 #define IP_FW_TCPOPT_TS 0x08
474 #define IP_FW_TCPOPT_CC 0x10
476 #define ICMP_REJECT_RST 0x100 /* fake ICMP code (send a TCP RST) */
485 * type of the keyword, it indecates the position of the keyword in the rule
486 * BEFORE ACTION FROM TO FILTER OTHER
501 #define NEED1(msg) {if (ac < 1) errx(EX_USAGE, msg);}
502 #define NEED2(msg) {if (ac < 2) errx(EX_USAGE, msg);}
503 #define NEED(c, n, msg) {if (c < n) errx(EX_USAGE, msg);}
505 #define NEXT_ARG ac--; if(ac > 0){av++;}
506 #define NEXT_ARG1 (*ac)--; if(*ac > 0){(*av)++;}
508 #define MATCH_REVERSE 0
509 #define MATCH_FORWARD 1
511 #define MATCH_UNKNOWN 3
513 #define BOTH_SYN (TH_SYN | (TH_SYN << 8))
514 #define BOTH_FIN (TH_FIN | (TH_FIN << 8))
516 #define TIME_LEQ(a, b) ((int)((a) - (b)) <= 0)
517 #define L3HDR(T, ip) ((T *)((uint32_t *)(ip) + (ip)->ip_hl))
519 /* IP_FW_X header/opcodes */
520 typedef struct _ip_fw_x_header
{
521 uint16_t opcode
; /* Operation opcode */
522 uint16_t _pad
; /* Opcode version */
525 typedef void ipfw_basic_delete_state_t(struct ip_fw
*);
526 typedef void ipfw_basic_append_state_t(struct ipfw_ioc_state
*);
530 #define IP_FW_ADD 50 /* add a firewall rule to chain */
531 #define IP_FW_DEL 51 /* delete a firewall rule from chain */
532 #define IP_FW_FLUSH 52 /* flush firewall rule chain */
533 #define IP_FW_ZERO 53 /* clear single/all firewall counter(s) */
534 #define IP_FW_GET 54 /* get entire firewall rule chain */
535 #define IP_FW_RESETLOG 55 /* reset logging counters */
537 #define IP_DUMMYNET_CONFIGURE 60 /* add/configure a dummynet pipe */
538 #define IP_DUMMYNET_DEL 61 /* delete a dummynet pipe from chain */
539 #define IP_DUMMYNET_FLUSH 62 /* flush dummynet */
540 #define IP_DUMMYNET_GET 64 /* get entire dummynet pipes */
542 #define IP_FW_MODULE 67 /* get modules names */
544 #define IP_FW_NAT_CFG 68 /* add/config a nat rule */
545 #define IP_FW_NAT_DEL 69 /* delete a nat rule */
546 #define IP_FW_NAT_FLUSH 70 /* get configuration of a nat rule */
547 #define IP_FW_NAT_GET 71 /* get config of a nat rule */
548 #define IP_FW_NAT_LOG 72 /* get nat record of a nat rule */
550 #define IP_FW_STATE_ADD 56 /* add one state */
551 #define IP_FW_STATE_DEL 57 /* delete states of one rulenum */
552 #define IP_FW_STATE_FLUSH 58 /* flush all states */
554 #define IP_FW_TABLE_CREATE 73 /* table_create */
555 #define IP_FW_TABLE_DELETE 74 /* table_delete */
556 #define IP_FW_TABLE_APPEND 75 /* table_append */
557 #define IP_FW_TABLE_REMOVE 76 /* table_remove */
558 #define IP_FW_TABLE_LIST 77 /* table_list */
559 #define IP_FW_TABLE_FLUSH 78 /* table_flush */
560 #define IP_FW_TABLE_SHOW 79 /* table_show */
561 #define IP_FW_TABLE_TEST 80 /* table_test */
562 #define IP_FW_TABLE_RENAME 81 /* rename a table */
566 #endif /* _IP_FW3_H_ */