2 * Copyright (c) 2002 Luigi Rizzo, Universita` di Pisa
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
25 * $FreeBSD: src/sys/netinet/ip_fw.h,v 1.110.2.6 2008/10/14 08:03:58 rwatson Exp $
32 * The default rule number. By the design of ip_fw, the default rule
33 * is the last one, so its number can also serve as the highest number
34 * allowed for a rule. The ip_fw code relies on both meanings of this
37 #define IPFW_DEFAULT_RULE 65535
40 * The kernel representation of ipfw rules is made of a list of
41 * 'instructions' (for all practical purposes equivalent to BPF
42 * instructions), which specify which fields of the packet
43 * (or its metadata) should be analysed.
45 * Each instruction is stored in a structure which begins with
46 * "ipfw_insn", and can contain extra fields depending on the
47 * instruction type (listed below).
48 * Note that the code is written so that individual instructions
49 * have a size which is a multiple of 32 bits. This means that, if
50 * such structures contain pointers or other 64-bit entities,
51 * (there is just one instance now) they may end up unaligned on
52 * 64-bit architectures, so the must be handled with care.
54 * "enum ipfw_opcodes" are the opcodes supported. We can have up
55 * to 256 different opcodes. When adding new opcodes, they should
56 * be appended to the end of the opcode list before O_LAST_OPCODE,
57 * this will prevent the ABI from being broken, otherwise users
58 * will have to recompile ipfw(8) when they update the kernel.
61 enum ipfw_opcodes { /* arguments (4 byte each) */
64 O_IP_SRC, /* u32 = IP */
65 O_IP_SRC_MASK, /* ip = IP/mask */
66 O_IP_SRC_ME, /* none */
67 O_IP_SRC_SET, /* u32=base, arg1=len, bitmap */
69 O_IP_DST, /* u32 = IP */
70 O_IP_DST_MASK, /* ip = IP/mask */
71 O_IP_DST_ME, /* none */
72 O_IP_DST_SET, /* u32=base, arg1=len, bitmap */
74 O_IP_SRCPORT, /* (n)port list:mask 4 byte ea */
75 O_IP_DSTPORT, /* (n)port list:mask 4 byte ea */
76 O_PROTO, /* arg1=protocol */
78 O_MACADDR2, /* 2 mac addr:mask */
79 O_MAC_TYPE, /* same as srcport */
89 O_IPOPT, /* arg1 = 2*u8 bitmap */
90 O_IPLEN, /* arg1 = len */
91 O_IPID, /* arg1 = id */
93 O_IPTOS, /* arg1 = id */
94 O_IPPRECEDENCE, /* arg1 = precedence << 5 */
95 O_IPTTL, /* arg1 = TTL */
97 O_IPVER, /* arg1 = version */
100 O_ESTAB, /* none (tcp established) */
101 O_TCPFLAGS, /* arg1 = 2*u8 bitmap */
102 O_TCPWIN, /* arg1 = desired win */
103 O_TCPSEQ, /* u32 = desired seq. */
104 O_TCPACK, /* u32 = desired seq. */
105 O_ICMPTYPE, /* u32 = icmp bitmap */
106 O_TCPOPTS, /* arg1 = 2*u8 bitmap */
108 O_VERREVPATH, /* none */
109 O_VERSRCREACH, /* none */
111 O_PROBE_STATE, /* none */
112 O_KEEP_STATE, /* none */
113 O_LIMIT, /* ipfw_insn_limit */
114 O_LIMIT_PARENT, /* dyn_type, not an opcode. */
117 * These are really 'actions'.
120 O_LOG, /* ipfw_insn_log */
121 O_PROB, /* u32 = match probability */
123 O_CHECK_STATE, /* none */
126 O_REJECT, /* arg1=icmp arg (same as deny) */
128 O_SKIPTO, /* arg1=next rule number */
129 O_PIPE, /* arg1=pipe number */
130 O_QUEUE, /* arg1=queue number */
131 O_DIVERT, /* arg1=port number */
132 O_TEE, /* arg1=port number */
133 O_FORWARD_IP, /* fwd sockaddr */
134 O_FORWARD_MAC, /* fwd mac */
141 O_IPSEC, /* has ipsec history */
142 O_IP_SRC_LOOKUP, /* arg1=table number, u32=value */
143 O_IP_DST_LOOKUP, /* arg1=table number, u32=value */
144 O_ANTISPOOF, /* none */
145 O_JAIL, /* u32 = id */
146 O_ALTQ, /* u32 = altq classif. qid */
147 O_DIVERTED, /* arg1=bitmap (1:loop, 2:out) */
148 O_TCPDATALEN, /* arg1 = tcp data len */
149 O_IP6_SRC, /* address without mask */
150 O_IP6_SRC_ME, /* my addresses */
151 O_IP6_SRC_MASK, /* address with the mask */
155 O_FLOW6ID, /* for flow id tag in the ipv6 pkt */
156 O_ICMP6TYPE, /* icmp6 packet type filtering */
157 O_EXT_HDR, /* filtering for ipv6 extension header */
161 * actions for ng_ipfw
163 O_NETGRAPH, /* send to ng_ipfw */
164 O_NGTEE, /* copy to ng_ipfw */
168 O_UNREACH6, /* arg1=icmpv6 code arg (deny) */
170 O_TAG, /* arg1=tag number */
171 O_TAGGED, /* arg1=tag number */
173 O_SETFIB, /* arg1=FIB number */
174 O_FIB, /* arg1=FIB desired fib number */
176 O_LAST_OPCODE /* not an opcode! */
180 * The extension header are filtered only for presence using a bit
181 * vector with a flag for each header.
183 #define EXT_FRAGMENT 0x1
184 #define EXT_HOPOPTS 0x2
185 #define EXT_ROUTING 0x4
188 #define EXT_DSTOPTS 0x20
189 #define EXT_RTHDR0 0x40
190 #define EXT_RTHDR2 0x80
193 * Template for instructions.
195 * ipfw_insn is used for all instructions which require no operands,
196 * a single 16-bit value (arg1), or a couple of 8-bit values.
198 * For other instructions which require different/larger arguments
199 * we have derived structures, ipfw_insn_*.
201 * The size of the instruction (in 32-bit words) is in the low
202 * 6 bits of "len". The 2 remaining bits are used to implement
203 * NOT and OR on individual instructions. Given a type, you can
204 * compute the length to be put in "len" using F_INSN_SIZE(t)
206 * F_NOT negates the match result of the instruction.
208 * F_OR is used to build or blocks. By default, instructions
209 * are evaluated as part of a logical AND. An "or" block
210 * { X or Y or Z } contains F_OR set in all but the last
211 * instruction of the block. A match will cause the code
212 * to skip past the last instruction of the block.
214 * NOTA BENE: in a couple of places we assume that
215 * sizeof(ipfw_insn) == sizeof(u_int32_t)
216 * this needs to be fixed.
219 typedef struct _ipfw_insn { /* template for instructions */
220 enum ipfw_opcodes opcode:8;
221 u_int8_t len; /* number of 32-bit words */
224 #define F_LEN_MASK 0x3f
225 #define F_LEN(cmd) ((cmd)->len & F_LEN_MASK)
231 * The F_INSN_SIZE(type) computes the size, in 4-byte words, of
234 #define F_INSN_SIZE(t) ((sizeof (t))/sizeof(u_int32_t))
236 #define MTAG_IPFW 1148380143 /* IPFW-tagged cookie */
239 * This is used to store an array of 16-bit entries (ports etc.)
241 typedef struct _ipfw_insn_u16 {
243 u_int16_t ports[2]; /* there may be more */
247 * This is used to store an array of 32-bit entries
248 * (uid, single IPv4 addresses etc.)
250 typedef struct _ipfw_insn_u32 {
252 u_int32_t d[1]; /* one or more */
256 * This is used to store IP addr-mask pairs.
258 typedef struct _ipfw_insn_ip {
265 * This is used to forward to a given address (ip).
267 typedef struct _ipfw_insn_sa {
269 struct sockaddr_in sa;
273 * This is used for MAC addr-mask pairs.
275 typedef struct _ipfw_insn_mac {
277 u_char addr[12]; /* dst[6] + src[6] */
278 u_char mask[12]; /* dst[6] + src[6] */
282 * This is used for interface match rules (recv xx, xmit xx).
284 typedef struct _ipfw_insn_if {
294 * This is used for storing an altq queue id number.
296 typedef struct _ipfw_insn_altq {
302 * This is used for limit rules.
304 typedef struct _ipfw_insn_limit {
307 u_int8_t limit_mask; /* combination of DYN_* below */
308 #define DYN_SRC_ADDR 0x1
309 #define DYN_SRC_PORT 0x2
310 #define DYN_DST_ADDR 0x4
311 #define DYN_DST_PORT 0x8
313 u_int16_t conn_limit;
317 * This is used for log instructions.
319 typedef struct _ipfw_insn_log {
321 u_int32_t max_log; /* how many do we log -- 0 = all */
322 u_int32_t log_left; /* how many left to log */
326 * Data structures required by both ipfw(8) and ipfw(4) but not part of the
327 * management API are protected by IPFW_INTERNAL.
330 /* Server pool support (LSNAT). */
332 LIST_ENTRY(cfg_spool) _next; /* chain of spool instances */
338 /* Redirect modes id. */
339 #define REDIR_ADDR 0x01
340 #define REDIR_PORT 0x02
341 #define REDIR_PROTO 0x04
344 /* Nat redirect configuration. */
346 LIST_ENTRY(cfg_redir) _next; /* chain of redir instances */
347 u_int16_t mode; /* type of redirect mode */
348 struct in_addr laddr; /* local ip address */
349 struct in_addr paddr; /* public ip address */
350 struct in_addr raddr; /* remote ip address */
351 u_short lport; /* local port */
352 u_short pport; /* public port */
353 u_short rport; /* remote port */
354 u_short pport_cnt; /* number of public ports */
355 u_short rport_cnt; /* number of remote ports */
356 int proto; /* protocol: tcp/udp */
357 struct alias_link **alink;
358 /* num of entry in spool chain */
360 /* chain of spool instances */
361 LIST_HEAD(spool_chain, cfg_spool) spool_chain;
365 #define NAT_BUF_LEN 1024
368 /* Nat configuration data struct. */
370 /* chain of nat instances */
371 LIST_ENTRY(cfg_nat) _next;
373 struct in_addr ip; /* nat ip address */
374 char if_name[IF_NAMESIZE]; /* interface name */
375 int mode; /* aliasing mode */
376 struct libalias *lib; /* libalias instance */
377 /* number of entry in spool chain */
379 /* chain of redir instances */
380 LIST_HEAD(redir_chain, cfg_redir) redir_chain;
384 #define SOF_NAT sizeof(struct cfg_nat)
385 #define SOF_REDIR sizeof(struct cfg_redir)
386 #define SOF_SPOOL sizeof(struct cfg_spool)
389 typedef struct _ipfw_insn_nat {
394 /* Apply ipv6 mask on ipv6 addr */
395 #define APPLY_MASK(addr,mask) \
396 (addr)->__u6_addr.__u6_addr32[0] &= (mask)->__u6_addr.__u6_addr32[0]; \
397 (addr)->__u6_addr.__u6_addr32[1] &= (mask)->__u6_addr.__u6_addr32[1]; \
398 (addr)->__u6_addr.__u6_addr32[2] &= (mask)->__u6_addr.__u6_addr32[2]; \
399 (addr)->__u6_addr.__u6_addr32[3] &= (mask)->__u6_addr.__u6_addr32[3];
401 /* Structure for ipv6 */
402 typedef struct _ipfw_insn_ip6 {
404 struct in6_addr addr6;
405 struct in6_addr mask6;
408 /* Used to support icmp6 types */
409 typedef struct _ipfw_insn_icmp6 {
411 uint32_t d[7]; /* XXX This number si related to the netinet/icmp6.h
412 * define ICMP6_MAXTYPE
413 * as follows: n = ICMP6_MAXTYPE/32 + 1
419 * Here we have the structure representing an ipfw rule.
421 * It starts with a general area (with link fields and counters)
422 * followed by an array of one or more instructions, which the code
423 * accesses as an array of 32-bit values.
425 * Given a rule pointer r:
427 * r->cmd is the start of the first instruction.
428 * ACTION_PTR(r) is the start of the first action (things to do
429 * once a rule matched).
431 * When assembling instruction, remember the following:
433 * + if a rule has a "keep-state" (or "limit") option, then the
434 * first instruction (at r->cmd) MUST BE an O_PROBE_STATE
435 * + if a rule has a "log" option, then the first action
436 * (at ACTION_PTR(r)) MUST be O_LOG
437 * + if a rule has an "altq" option, it comes after "log"
438 * + if a rule has an O_TAG option, it comes after "log" and "altq"
440 * NOTE: we use a simple linked list of rules because we never need
441 * to delete a rule without scanning the list. We do not use
442 * queue(3) macros for portability and readability.
446 struct ip_fw *next; /* linked list of rules */
447 struct ip_fw *next_rule; /* ptr to next [skipto] rule */
448 /* 'next_rule' is used to pass up 'set_disable' status */
450 u_int16_t act_ofs; /* offset of action in 32-bit units */
451 u_int16_t cmd_len; /* # of 32-bit words in cmd */
452 u_int16_t rulenum; /* rule number */
453 u_int8_t set; /* rule set (0..31) */
454 #define RESVD_SET 31 /* set for default and persistent rules */
455 u_int8_t _pad; /* padding */
457 /* These fields are present in all rules. */
458 u_int64_t pcnt; /* Packet counter */
459 u_int64_t bcnt; /* Byte counter */
460 u_int32_t timestamp; /* tv_sec of last match */
462 ipfw_insn cmd[1]; /* storage for commands */
465 #define ACTION_PTR(rule) \
466 (ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) )
468 #define RULESIZE(rule) (sizeof(struct ip_fw) + \
469 ((struct ip_fw *)(rule))->cmd_len * 4 - 4)
472 * This structure is used as a flow mask and a flow id for various
475 struct ipfw_flow_id {
482 u_int8_t flags; /* protocol-specific flags */
483 uint8_t addr_type; /* 4 = ipv4, 6 = ipv6, 1=ether ? */
484 struct in6_addr dst_ip6; /* could also store MAC addr! */
485 struct in6_addr src_ip6;
490 #define IS_IP6_FLOW_ID(id) ((id)->addr_type == 6)
495 typedef struct _ipfw_dyn_rule ipfw_dyn_rule;
497 struct _ipfw_dyn_rule {
498 ipfw_dyn_rule *next; /* linked list of rules. */
499 struct ip_fw *rule; /* pointer to rule */
500 /* 'rule' is used to pass up the rule number (from the parent) */
502 ipfw_dyn_rule *parent; /* pointer to parent rule */
503 u_int64_t pcnt; /* packet match counter */
504 u_int64_t bcnt; /* byte match counter */
505 struct ipfw_flow_id id; /* (masked) flow id */
506 u_int32_t expire; /* expire time */
507 u_int32_t bucket; /* which bucket in hash table */
508 u_int32_t state; /* state of this rule (typically a
509 * combination of TCP flags)
511 u_int32_t ack_fwd; /* most recent ACKs in forward */
512 u_int32_t ack_rev; /* and reverse directions (used */
513 /* to generate keepalives) */
514 u_int16_t dyn_type; /* rule type */
515 u_int16_t count; /* refcount */
519 * Definitions for IP option names.
521 #define IP_FW_IPOPT_LSRR 0x01
522 #define IP_FW_IPOPT_SSRR 0x02
523 #define IP_FW_IPOPT_RR 0x04
524 #define IP_FW_IPOPT_TS 0x08
527 * Definitions for TCP option names.
529 #define IP_FW_TCPOPT_MSS 0x01
530 #define IP_FW_TCPOPT_WINDOW 0x02
531 #define IP_FW_TCPOPT_SACK 0x04
532 #define IP_FW_TCPOPT_TS 0x08
533 #define IP_FW_TCPOPT_CC 0x10
535 #define ICMP_REJECT_RST 0x100 /* fake ICMP code (send a TCP RST) */
536 #define ICMP6_UNREACH_RST 0x100 /* fake ICMPv6 code (send a TCP RST) */
539 * These are used for lookup tables.
541 typedef struct _ipfw_table_entry {
542 in_addr_t addr; /* network address */
543 u_int32_t value; /* value */
544 u_int16_t tbl; /* table number */
545 u_int8_t masklen; /* mask length */
548 typedef struct _ipfw_table {
549 u_int32_t size; /* size of entries in bytes */
550 u_int32_t cnt; /* # of entries */
551 u_int16_t tbl; /* table number */
552 ipfw_table_entry ent[0]; /* entries */
555 #define IP_FW_TABLEARG 65535
558 * Main firewall chains definitions and global var's definitions.
562 /* Return values from ipfw_chk() */
575 /* flags for divert mtag */
576 #define IP_FW_DIVERT_LOOPBACK_FLAG 0x00080000
577 #define IP_FW_DIVERT_OUTPUT_FLAG 0x00100000
580 * Structure for collecting parameters to dummynet for ip6_output forwarding
583 struct ip6_pktopts *opt_or;
584 struct route_in6 ro_or;
586 struct ip6_moptions *im6o_or;
587 struct ifnet *origifp_or;
588 struct ifnet *ifp_or;
589 struct sockaddr_in6 dst_or;
591 struct route_in6 ro_pmtu_or;
595 * Arguments for calling ipfw_chk() and dummynet_io(). We put them
596 * all into a structure because this way it is easier and more
597 * efficient to pass variables around and extend the interface.
600 struct mbuf *m; /* the mbuf chain */
601 struct ifnet *oif; /* output interface */
602 struct sockaddr_in *next_hop; /* forward address */
603 struct ip_fw *rule; /* matching rule */
604 struct ether_header *eh; /* for bridged packets */
606 struct ipfw_flow_id f_id; /* grabbed from IP header */
607 u_int32_t cookie; /* a cookie depending on rule action */
610 struct _ip6dn_args dummypar; /* dummynet->ip6_output */
611 struct sockaddr_in hopstore; /* store here if cannot use a pointer */
615 * Function definitions.
622 int ipfw_check_in(void *, struct mbuf **, struct ifnet *, int, struct inpcb *inp);
623 int ipfw_check_out(void *, struct mbuf **, struct ifnet *, int, struct inpcb *inp);
625 int ipfw_chk(struct ip_fw_args *);
628 void ipfw_destroy(void);
630 typedef int ip_fw_ctl_t(struct sockopt *);
631 extern ip_fw_ctl_t *ip_fw_ctl_ptr;
632 extern int fw_one_pass;
633 extern int fw_enable;
635 extern int fw6_enable;
638 /* For kernel ipfw_ether and ipfw_bridge. */
639 typedef int ip_fw_chk_t(struct ip_fw_args *args);
640 extern ip_fw_chk_t *ip_fw_chk_ptr;
641 #define IPFW_LOADED (ip_fw_chk_ptr != NULL)
645 #define IPFW_TABLES_MAX 128
647 struct ip_fw *rules; /* list of rules */
648 struct ip_fw *reap; /* list of rules to reap */
649 LIST_HEAD(, cfg_nat) nat; /* list of nat entries */
650 struct radix_node_head *tables[IPFW_TABLES_MAX];
651 #if defined( __linux__ ) || defined( _WIN32 )
655 #endif /* !__linux__ */
657 #define IPFW_LOCK_INIT(_chain) \
658 rw_init(&(_chain)->rwmtx, "IPFW static rules")
659 #define IPFW_LOCK_DESTROY(_chain) rw_destroy(&(_chain)->rwmtx)
660 #define IPFW_WLOCK_ASSERT(_chain) rw_assert(&(_chain)->rwmtx, RA_WLOCKED)
662 #define IPFW_RLOCK(p) rw_rlock(&(p)->rwmtx)
663 #define IPFW_RUNLOCK(p) rw_runlock(&(p)->rwmtx)
664 #define IPFW_WLOCK(p) rw_wlock(&(p)->rwmtx)
665 #define IPFW_WUNLOCK(p) rw_wunlock(&(p)->rwmtx)
667 #define LOOKUP_NAT(l, i, p) do { \
668 LIST_FOREACH((p), &(l.nat), _next) { \
669 if ((p)->id == (i)) { \
675 typedef int ipfw_nat_t(struct ip_fw_args *, struct cfg_nat *, struct mbuf *);
676 typedef int ipfw_nat_cfg_t(struct sockopt *);
680 #endif /* _IPFW2_H */