ip_fw.h revision 183240
1238104Sdes/*- 2238104Sdes * Copyright (c) 2002 Luigi Rizzo, Universita` di Pisa 3238104Sdes * 4238104Sdes * Redistribution and use in source and binary forms, with or without 5269257Sdes * modification, are permitted provided that the following conditions 6238104Sdes * are met: 7238104Sdes * 1. Redistributions of source code must retain the above copyright 8238104Sdes * notice, this list of conditions and the following disclaimer. 9238104Sdes * 2. Redistributions in binary form must reproduce the above copyright 10238104Sdes * notice, this list of conditions and the following disclaimer in the 11238104Sdes * documentation and/or other materials provided with the distribution. 12238104Sdes * 13238104Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14238104Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15238104Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16269257Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17269257Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18269257Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19269257Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20238104Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21238104Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22238104Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23238104Sdes * SUCH DAMAGE. 24238104Sdes * 25238104Sdes * $FreeBSD: head/sys/netinet/ip_fw.h 183240 2008-09-21 20:42:42Z rik $ 26238104Sdes */ 27238104Sdes 28246854Sdes#ifndef _IPFW2_H 29238104Sdes#define _IPFW2_H 30238104Sdes 31238104Sdes/* 32238104Sdes * The default rule number. By the design of ip_fw, the default rule 33238104Sdes * is the last one, so its number can also serve as the highest number 34238104Sdes * allowed for a rule. The ip_fw code relies on both meanings of this 35238104Sdes * constant. 36238104Sdes */ 37238104Sdes#define IPFW_DEFAULT_RULE 65535 38238104Sdes 39238104Sdes/* 40238104Sdes * The number of ipfw tables. The maximum allowed table number is the 41238104Sdes * (IPFW_TABLES_MAX - 1). 42238104Sdes */ 43238104Sdes#define IPFW_TABLES_MAX 128 44238104Sdes 45238104Sdes/* 46238104Sdes * The kernel representation of ipfw rules is made of a list of 47246854Sdes * 'instructions' (for all practical purposes equivalent to BPF 48238104Sdes * instructions), which specify which fields of the packet 49238104Sdes * (or its metadata) should be analysed. 50238104Sdes * 51238104Sdes * Each instruction is stored in a structure which begins with 52238104Sdes * "ipfw_insn", and can contain extra fields depending on the 53238104Sdes * instruction type (listed below). 54238104Sdes * Note that the code is written so that individual instructions 55238104Sdes * have a size which is a multiple of 32 bits. This means that, if 56238104Sdes * such structures contain pointers or other 64-bit entities, 57238104Sdes * (there is just one instance now) they may end up unaligned on 58238104Sdes * 64-bit architectures, so the must be handled with care. 59238104Sdes * 60238104Sdes * "enum ipfw_opcodes" are the opcodes supported. We can have up 61238104Sdes * to 256 different opcodes. When adding new opcodes, they should 62269257Sdes * be appended to the end of the opcode list before O_LAST_OPCODE, 63269257Sdes * this will prevent the ABI from being broken, otherwise users 64238104Sdes * will have to recompile ipfw(8) when they update the kernel. 65238104Sdes */ 66238104Sdes 67238104Sdesenum ipfw_opcodes { /* arguments (4 byte each) */ 68238104Sdes O_NOP, 69269257Sdes 70238104Sdes O_IP_SRC, /* u32 = IP */ 71238104Sdes O_IP_SRC_MASK, /* ip = IP/mask */ 72238104Sdes O_IP_SRC_ME, /* none */ 73238104Sdes O_IP_SRC_SET, /* u32=base, arg1=len, bitmap */ 74269257Sdes 75238104Sdes O_IP_DST, /* u32 = IP */ 76238104Sdes O_IP_DST_MASK, /* ip = IP/mask */ 77238104Sdes O_IP_DST_ME, /* none */ 78238104Sdes O_IP_DST_SET, /* u32=base, arg1=len, bitmap */ 79238104Sdes 80238104Sdes O_IP_SRCPORT, /* (n)port list:mask 4 byte ea */ 81238104Sdes O_IP_DSTPORT, /* (n)port list:mask 4 byte ea */ 82246854Sdes O_PROTO, /* arg1=protocol */ 83238104Sdes 84246854Sdes O_MACADDR2, /* 2 mac addr:mask */ 85246854Sdes O_MAC_TYPE, /* same as srcport */ 86238104Sdes 87238104Sdes O_LAYER2, /* none */ 88238104Sdes O_IN, /* none */ 89238104Sdes O_FRAG, /* none */ 90246854Sdes 91246854Sdes O_RECV, /* none */ 92238104Sdes O_XMIT, /* none */ 93238104Sdes O_VIA, /* none */ 94238104Sdes 95238104Sdes O_IPOPT, /* arg1 = 2*u8 bitmap */ 96238104Sdes O_IPLEN, /* arg1 = len */ 97238104Sdes O_IPID, /* arg1 = id */ 98238104Sdes 99269257Sdes O_IPTOS, /* arg1 = id */ 100269257Sdes O_IPPRECEDENCE, /* arg1 = precedence << 5 */ 101238104Sdes O_IPTTL, /* arg1 = TTL */ 102238104Sdes 103238104Sdes O_IPVER, /* arg1 = version */ 104238104Sdes O_UID, /* u32 = id */ 105238104Sdes O_GID, /* u32 = id */ 106269257Sdes O_ESTAB, /* none (tcp established) */ 107238104Sdes O_TCPFLAGS, /* arg1 = 2*u8 bitmap */ 108238104Sdes O_TCPWIN, /* arg1 = desired win */ 109238104Sdes O_TCPSEQ, /* u32 = desired seq. */ 110238104Sdes O_TCPACK, /* u32 = desired seq. */ 111238104Sdes O_ICMPTYPE, /* u32 = icmp bitmap */ 112238104Sdes O_TCPOPTS, /* arg1 = 2*u8 bitmap */ 113238104Sdes 114238104Sdes O_VERREVPATH, /* none */ 115238104Sdes O_VERSRCREACH, /* none */ 116238104Sdes 117238104Sdes O_PROBE_STATE, /* none */ 118238104Sdes O_KEEP_STATE, /* none */ 119238104Sdes O_LIMIT, /* ipfw_insn_limit */ 120238104Sdes O_LIMIT_PARENT, /* dyn_type, not an opcode. */ 121238104Sdes 122238104Sdes /* 123238104Sdes * These are really 'actions'. 124238104Sdes */ 125238104Sdes 126238104Sdes O_LOG, /* ipfw_insn_log */ 127238104Sdes O_PROB, /* u32 = match probability */ 128238104Sdes 129238104Sdes O_CHECK_STATE, /* none */ 130238104Sdes O_ACCEPT, /* none */ 131238104Sdes O_DENY, /* none */ 132238104Sdes O_REJECT, /* arg1=icmp arg (same as deny) */ 133238104Sdes O_COUNT, /* none */ 134238104Sdes O_SKIPTO, /* arg1=next rule number */ 135238104Sdes O_PIPE, /* arg1=pipe number */ 136238104Sdes O_QUEUE, /* arg1=queue number */ 137238104Sdes O_DIVERT, /* arg1=port number */ 138238104Sdes O_TEE, /* arg1=port number */ 139238104Sdes O_FORWARD_IP, /* fwd sockaddr */ 140238104Sdes O_FORWARD_MAC, /* fwd mac */ 141238104Sdes O_NAT, /* nope */ 142238104Sdes 143238104Sdes /* 144238104Sdes * More opcodes. 145238104Sdes */ 146238104Sdes O_IPSEC, /* has ipsec history */ 147238104Sdes O_IP_SRC_LOOKUP, /* arg1=table number, u32=value */ 148238104Sdes O_IP_DST_LOOKUP, /* arg1=table number, u32=value */ 149246854Sdes O_ANTISPOOF, /* none */ 150238104Sdes O_JAIL, /* u32 = id */ 151238104Sdes O_ALTQ, /* u32 = altq classif. qid */ 152238104Sdes O_DIVERTED, /* arg1=bitmap (1:loop, 2:out) */ 153238104Sdes O_TCPDATALEN, /* arg1 = tcp data len */ 154238104Sdes O_IP6_SRC, /* address without mask */ 155238104Sdes O_IP6_SRC_ME, /* my addresses */ 156238104Sdes O_IP6_SRC_MASK, /* address with the mask */ 157238104Sdes O_IP6_DST, 158238104Sdes O_IP6_DST_ME, 159246854Sdes O_IP6_DST_MASK, 160238104Sdes O_FLOW6ID, /* for flow id tag in the ipv6 pkt */ 161238104Sdes O_ICMP6TYPE, /* icmp6 packet type filtering */ 162238104Sdes O_EXT_HDR, /* filtering for ipv6 extension header */ 163238104Sdes O_IP6, 164238104Sdes 165238104Sdes /* 166238104Sdes * actions for ng_ipfw 167238104Sdes */ 168238104Sdes O_NETGRAPH, /* send to ng_ipfw */ 169238104Sdes O_NGTEE, /* copy to ng_ipfw */ 170238104Sdes 171238104Sdes O_IP4, 172238104Sdes 173238104Sdes O_UNREACH6, /* arg1=icmpv6 code arg (deny) */ 174238104Sdes 175238104Sdes O_TAG, /* arg1=tag number */ 176238104Sdes O_TAGGED, /* arg1=tag number */ 177238104Sdes 178246854Sdes O_SETFIB, /* arg1=FIB number */ 179246854Sdes O_FIB, /* arg1=FIB desired fib number */ 180246854Sdes 181246854Sdes O_LAST_OPCODE /* not an opcode! */ 182238104Sdes}; 183238104Sdes 184238104Sdes/* 185246854Sdes * The extension header are filtered only for presence using a bit 186238104Sdes * vector with a flag for each header. 187238104Sdes */ 188238104Sdes#define EXT_FRAGMENT 0x1 189246854Sdes#define EXT_HOPOPTS 0x2 190238104Sdes#define EXT_ROUTING 0x4 191246854Sdes#define EXT_AH 0x8 192246854Sdes#define EXT_ESP 0x10 193246854Sdes#define EXT_DSTOPTS 0x20 194246854Sdes#define EXT_RTHDR0 0x40 195238104Sdes#define EXT_RTHDR2 0x80 196238104Sdes 197238104Sdes/* 198238104Sdes * Template for instructions. 199238104Sdes * 200238104Sdes * ipfw_insn is used for all instructions which require no operands, 201238104Sdes * a single 16-bit value (arg1), or a couple of 8-bit values. 202238104Sdes * 203238104Sdes * For other instructions which require different/larger arguments 204238104Sdes * we have derived structures, ipfw_insn_*. 205238104Sdes * 206246854Sdes * The size of the instruction (in 32-bit words) is in the low 207238104Sdes * 6 bits of "len". The 2 remaining bits are used to implement 208238104Sdes * NOT and OR on individual instructions. Given a type, you can 209238104Sdes * compute the length to be put in "len" using F_INSN_SIZE(t) 210269257Sdes * 211269257Sdes * F_NOT negates the match result of the instruction. 212238104Sdes * 213238104Sdes * F_OR is used to build or blocks. By default, instructions 214238104Sdes * are evaluated as part of a logical AND. An "or" block 215238104Sdes * { X or Y or Z } contains F_OR set in all but the last 216238104Sdes * instruction of the block. A match will cause the code 217238104Sdes * to skip past the last instruction of the block. 218246854Sdes * 219238104Sdes * NOTA BENE: in a couple of places we assume that 220238104Sdes * sizeof(ipfw_insn) == sizeof(u_int32_t) 221238104Sdes * this needs to be fixed. 222238104Sdes * 223238104Sdes */ 224238104Sdestypedef struct _ipfw_insn { /* template for instructions */ 225238104Sdes enum ipfw_opcodes opcode:8; 226238104Sdes u_int8_t len; /* numer of 32-byte words */ 227238104Sdes#define F_NOT 0x80 228238104Sdes#define F_OR 0x40 229238104Sdes#define F_LEN_MASK 0x3f 230238104Sdes#define F_LEN(cmd) ((cmd)->len & F_LEN_MASK) 231238104Sdes 232238104Sdes u_int16_t arg1; 233238104Sdes} ipfw_insn; 234238104Sdes 235238104Sdes/* 236238104Sdes * The F_INSN_SIZE(type) computes the size, in 4-byte words, of 237238104Sdes * a given type. 238238104Sdes */ 239238104Sdes#define F_INSN_SIZE(t) ((sizeof (t))/sizeof(u_int32_t)) 240238104Sdes 241238104Sdes#define MTAG_IPFW 1148380143 /* IPFW-tagged cookie */ 242238104Sdes 243238104Sdes/* 244238104Sdes * This is used to store an array of 16-bit entries (ports etc.) 245238104Sdes */ 246238104Sdestypedef struct _ipfw_insn_u16 { 247238104Sdes ipfw_insn o; 248246854Sdes u_int16_t ports[2]; /* there may be more */ 249238104Sdes} ipfw_insn_u16; 250238104Sdes 251246854Sdes/* 252238104Sdes * This is used to store an array of 32-bit entries 253269257Sdes * (uid, single IPv4 addresses etc.) 254269257Sdes */ 255238104Sdestypedef struct _ipfw_insn_u32 { 256269257Sdes ipfw_insn o; 257269257Sdes u_int32_t d[1]; /* one or more */ 258238104Sdes} ipfw_insn_u32; 259269257Sdes 260269257Sdes/* 261269257Sdes * This is used to store IP addr-mask pairs. 262269257Sdes */ 263269257Sdestypedef struct _ipfw_insn_ip { 264269257Sdes ipfw_insn o; 265269257Sdes struct in_addr addr; 266269257Sdes struct in_addr mask; 267269257Sdes} ipfw_insn_ip; 268269257Sdes 269269257Sdes/* 270269257Sdes * This is used to forward to a given address (ip). 271269257Sdes */ 272269257Sdestypedef struct _ipfw_insn_sa { 273269257Sdes ipfw_insn o; 274269257Sdes struct sockaddr_in sa; 275269257Sdes} ipfw_insn_sa; 276269257Sdes 277238104Sdes/* 278238104Sdes * This is used for MAC addr-mask pairs. 279238104Sdes */ 280238104Sdestypedef struct _ipfw_insn_mac { 281238104Sdes ipfw_insn o; 282238104Sdes u_char addr[12]; /* dst[6] + src[6] */ 283238104Sdes u_char mask[12]; /* dst[6] + src[6] */ 284238104Sdes} ipfw_insn_mac; 285238104Sdes 286238104Sdes/* 287238104Sdes * This is used for interface match rules (recv xx, xmit xx). 288238104Sdes */ 289238104Sdestypedef struct _ipfw_insn_if { 290238104Sdes ipfw_insn o; 291238104Sdes union { 292238104Sdes struct in_addr ip; 293238104Sdes int glob; 294238104Sdes } p; 295238104Sdes char name[IFNAMSIZ]; 296238104Sdes} ipfw_insn_if; 297238104Sdes 298238104Sdes/* 299238104Sdes * This is used for storing an altq queue id number. 300238104Sdes */ 301238104Sdestypedef struct _ipfw_insn_altq { 302238104Sdes ipfw_insn o; 303238104Sdes u_int32_t qid; 304238104Sdes} ipfw_insn_altq; 305238104Sdes 306238104Sdes/* 307238104Sdes * This is used for limit rules. 308238104Sdes */ 309238104Sdestypedef struct _ipfw_insn_limit { 310238104Sdes ipfw_insn o; 311238104Sdes u_int8_t _pad; 312238104Sdes u_int8_t limit_mask; /* combination of DYN_* below */ 313238104Sdes#define DYN_SRC_ADDR 0x1 314238104Sdes#define DYN_SRC_PORT 0x2 315238104Sdes#define DYN_DST_ADDR 0x4 316238104Sdes#define DYN_DST_PORT 0x8 317238104Sdes 318238104Sdes u_int16_t conn_limit; 319238104Sdes} ipfw_insn_limit; 320238104Sdes 321238104Sdes/* 322238104Sdes * This is used for log instructions. 323238104Sdes */ 324238104Sdestypedef struct _ipfw_insn_log { 325238104Sdes ipfw_insn o; 326238104Sdes u_int32_t max_log; /* how many do we log -- 0 = all */ 327238104Sdes u_int32_t log_left; /* how many left to log */ 328238104Sdes} ipfw_insn_log; 329238104Sdes 330238104Sdes/* 331238104Sdes * Data structures required by both ipfw(8) and ipfw(4) but not part of the 332238104Sdes * management API are protected by IPFW_INTERNAL. 333238104Sdes */ 334238104Sdes#ifdef IPFW_INTERNAL 335238104Sdes/* Server pool support (LSNAT). */ 336238104Sdesstruct cfg_spool { 337238104Sdes LIST_ENTRY(cfg_spool) _next; /* chain of spool instances */ 338238104Sdes struct in_addr addr; 339238104Sdes u_short port; 340238104Sdes}; 341238104Sdes#endif 342238104Sdes 343238104Sdes/* Redirect modes id. */ 344238104Sdes#define REDIR_ADDR 0x01 345238104Sdes#define REDIR_PORT 0x02 346238104Sdes#define REDIR_PROTO 0x04 347238104Sdes 348238104Sdes#ifdef IPFW_INTERNAL 349238104Sdes/* Nat redirect configuration. */ 350238104Sdesstruct cfg_redir { 351238104Sdes LIST_ENTRY(cfg_redir) _next; /* chain of redir instances */ 352238104Sdes u_int16_t mode; /* type of redirect mode */ 353238104Sdes struct in_addr laddr; /* local ip address */ 354238104Sdes struct in_addr paddr; /* public ip address */ 355238104Sdes struct in_addr raddr; /* remote ip address */ 356238104Sdes u_short lport; /* local port */ 357269257Sdes u_short pport; /* public port */ 358238104Sdes u_short rport; /* remote port */ 359238104Sdes u_short pport_cnt; /* number of public ports */ 360238104Sdes u_short rport_cnt; /* number of remote ports */ 361238104Sdes int proto; /* protocol: tcp/udp */ 362246854Sdes struct alias_link **alink; 363238104Sdes /* num of entry in spool chain */ 364238104Sdes u_int16_t spool_cnt; 365238104Sdes /* chain of spool instances */ 366238104Sdes LIST_HEAD(spool_chain, cfg_spool) spool_chain; 367238104Sdes}; 368238104Sdes#endif 369238104Sdes 370238104Sdes#define NAT_BUF_LEN 1024 371238104Sdes 372238104Sdes#ifdef IPFW_INTERNAL 373238104Sdes/* Nat configuration data struct. */ 374238104Sdesstruct cfg_nat { 375238104Sdes /* chain of nat instances */ 376238104Sdes LIST_ENTRY(cfg_nat) _next; 377238104Sdes int id; /* nat id */ 378238104Sdes struct in_addr ip; /* nat ip address */ 379238104Sdes char if_name[IF_NAMESIZE]; /* interface name */ 380238104Sdes int mode; /* aliasing mode */ 381238104Sdes struct libalias *lib; /* libalias instance */ 382238104Sdes /* number of entry in spool chain */ 383238104Sdes int redir_cnt; 384238104Sdes /* chain of redir instances */ 385238104Sdes LIST_HEAD(redir_chain, cfg_redir) redir_chain; 386238104Sdes}; 387238104Sdes#endif 388238104Sdes 389238104Sdes#define SOF_NAT sizeof(struct cfg_nat) 390238104Sdes#define SOF_REDIR sizeof(struct cfg_redir) 391238104Sdes#define SOF_SPOOL sizeof(struct cfg_spool) 392238104Sdes 393238104Sdes/* Nat command. */ 394238104Sdestypedef struct _ipfw_insn_nat { 395246854Sdes ipfw_insn o; 396246854Sdes struct cfg_nat *nat; 397238104Sdes} ipfw_insn_nat; 398238104Sdes 399238104Sdes/* Apply ipv6 mask on ipv6 addr */ 400238104Sdes#define APPLY_MASK(addr,mask) \ 401238104Sdes (addr)->__u6_addr.__u6_addr32[0] &= (mask)->__u6_addr.__u6_addr32[0]; \ 402238104Sdes (addr)->__u6_addr.__u6_addr32[1] &= (mask)->__u6_addr.__u6_addr32[1]; \ 403238104Sdes (addr)->__u6_addr.__u6_addr32[2] &= (mask)->__u6_addr.__u6_addr32[2]; \ 404238104Sdes (addr)->__u6_addr.__u6_addr32[3] &= (mask)->__u6_addr.__u6_addr32[3]; 405238104Sdes 406238104Sdes/* Structure for ipv6 */ 407238104Sdestypedef struct _ipfw_insn_ip6 { 408238104Sdes ipfw_insn o; 409238104Sdes struct in6_addr addr6; 410238104Sdes struct in6_addr mask6; 411238104Sdes} ipfw_insn_ip6; 412238104Sdes 413238104Sdes/* Used to support icmp6 types */ 414238104Sdestypedef struct _ipfw_insn_icmp6 { 415238104Sdes ipfw_insn o; 416238104Sdes uint32_t d[7]; /* XXX This number si related to the netinet/icmp6.h 417238104Sdes * define ICMP6_MAXTYPE 418238104Sdes * as follows: n = ICMP6_MAXTYPE/32 + 1 419238104Sdes * Actually is 203 420238104Sdes */ 421238104Sdes} ipfw_insn_icmp6; 422269257Sdes 423238104Sdes/* 424238104Sdes * Here we have the structure representing an ipfw rule. 425238104Sdes * 426238104Sdes * It starts with a general area (with link fields and counters) 427238104Sdes * followed by an array of one or more instructions, which the code 428238104Sdes * accesses as an array of 32-bit values. 429238104Sdes * 430238104Sdes * Given a rule pointer r: 431238104Sdes * 432238104Sdes * r->cmd is the start of the first instruction. 433238104Sdes * ACTION_PTR(r) is the start of the first action (things to do 434238104Sdes * once a rule matched). 435238104Sdes * 436238104Sdes * When assembling instruction, remember the following: 437238104Sdes * 438238104Sdes * + if a rule has a "keep-state" (or "limit") option, then the 439238104Sdes * first instruction (at r->cmd) MUST BE an O_PROBE_STATE 440238104Sdes * + if a rule has a "log" option, then the first action 441238104Sdes * (at ACTION_PTR(r)) MUST be O_LOG 442238104Sdes * + if a rule has an "altq" option, it comes after "log" 443238104Sdes * + if a rule has an O_TAG option, it comes after "log" and "altq" 444238104Sdes * 445238104Sdes * NOTE: we use a simple linked list of rules because we never need 446238104Sdes * to delete a rule without scanning the list. We do not use 447238104Sdes * queue(3) macros for portability and readability. 448238104Sdes */ 449238104Sdes 450238104Sdesstruct ip_fw { 451238104Sdes struct ip_fw *next; /* linked list of rules */ 452238104Sdes struct ip_fw *next_rule; /* ptr to next [skipto] rule */ 453238104Sdes /* 'next_rule' is used to pass up 'set_disable' status */ 454238104Sdes 455238104Sdes u_int16_t act_ofs; /* offset of action in 32-bit units */ 456238104Sdes u_int16_t cmd_len; /* # of 32-bit words in cmd */ 457238104Sdes u_int16_t rulenum; /* rule number */ 458238104Sdes u_int8_t set; /* rule set (0..31) */ 459238104Sdes#define RESVD_SET 31 /* set for default and persistent rules */ 460238104Sdes u_int8_t _pad; /* padding */ 461238104Sdes 462238104Sdes /* These fields are present in all rules. */ 463238104Sdes u_int64_t pcnt; /* Packet counter */ 464238104Sdes u_int64_t bcnt; /* Byte counter */ 465238104Sdes u_int32_t timestamp; /* tv_sec of last match */ 466238104Sdes 467238104Sdes ipfw_insn cmd[1]; /* storage for commands */ 468238104Sdes}; 469238104Sdes 470238104Sdes#define ACTION_PTR(rule) \ 471238104Sdes (ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) ) 472238104Sdes 473238104Sdes#define RULESIZE(rule) (sizeof(struct ip_fw) + \ 474238104Sdes ((struct ip_fw *)(rule))->cmd_len * 4 - 4) 475238104Sdes 476238104Sdes/* 477238104Sdes * This structure is used as a flow mask and a flow id for various 478238104Sdes * parts of the code. 479238104Sdes */ 480238104Sdesstruct ipfw_flow_id { 481238104Sdes u_int32_t dst_ip; 482246854Sdes u_int32_t src_ip; 483238104Sdes u_int16_t dst_port; 484238104Sdes u_int16_t src_port; 485238104Sdes u_int8_t fib; 486238104Sdes u_int8_t proto; 487238104Sdes u_int8_t flags; /* protocol-specific flags */ 488238104Sdes uint8_t addr_type; /* 4 = ipv4, 6 = ipv6, 1=ether ? */ 489238104Sdes struct in6_addr dst_ip6; /* could also store MAC addr! */ 490238104Sdes struct in6_addr src_ip6; 491238104Sdes u_int32_t flow_id6; 492238104Sdes u_int32_t frag_id6; 493238104Sdes}; 494238104Sdes 495238104Sdes#define IS_IP6_FLOW_ID(id) ((id)->addr_type == 6) 496238104Sdes 497238104Sdes/* 498238104Sdes * Dynamic ipfw rule. 499238104Sdes */ 500238104Sdestypedef struct _ipfw_dyn_rule ipfw_dyn_rule; 501246854Sdes 502246854Sdesstruct _ipfw_dyn_rule { 503246854Sdes ipfw_dyn_rule *next; /* linked list of rules. */ 504246854Sdes struct ip_fw *rule; /* pointer to rule */ 505246854Sdes /* 'rule' is used to pass up the rule number (from the parent) */ 506246854Sdes 507269257Sdes ipfw_dyn_rule *parent; /* pointer to parent rule */ 508269257Sdes u_int64_t pcnt; /* packet match counter */ 509246854Sdes u_int64_t bcnt; /* byte match counter */ 510246854Sdes struct ipfw_flow_id id; /* (masked) flow id */ 511246854Sdes u_int32_t expire; /* expire time */ 512246854Sdes u_int32_t bucket; /* which bucket in hash table */ 513246854Sdes u_int32_t state; /* state of this rule (typically a 514246854Sdes * combination of TCP flags) 515269257Sdes */ 516269257Sdes u_int32_t ack_fwd; /* most recent ACKs in forward */ 517246854Sdes u_int32_t ack_rev; /* and reverse directions (used */ 518246854Sdes /* to generate keepalives) */ 519246854Sdes u_int16_t dyn_type; /* rule type */ 520246854Sdes u_int16_t count; /* refcount */ 521246854Sdes}; 522246854Sdes 523269257Sdes/* 524269257Sdes * Definitions for IP option names. 525246854Sdes */ 526246854Sdes#define IP_FW_IPOPT_LSRR 0x01 527246854Sdes#define IP_FW_IPOPT_SSRR 0x02 528246854Sdes#define IP_FW_IPOPT_RR 0x04 529246854Sdes#define IP_FW_IPOPT_TS 0x08 530246854Sdes 531269257Sdes/* 532269257Sdes * Definitions for TCP option names. 533246854Sdes */ 534246854Sdes#define IP_FW_TCPOPT_MSS 0x01 535246854Sdes#define IP_FW_TCPOPT_WINDOW 0x02 536246854Sdes#define IP_FW_TCPOPT_SACK 0x04 537246854Sdes#define IP_FW_TCPOPT_TS 0x08 538246854Sdes#define IP_FW_TCPOPT_CC 0x10 539269257Sdes 540269257Sdes#define ICMP_REJECT_RST 0x100 /* fake ICMP code (send a TCP RST) */ 541246854Sdes#define ICMP6_UNREACH_RST 0x100 /* fake ICMPv6 code (send a TCP RST) */ 542246854Sdes 543246854Sdes/* 544246854Sdes * These are used for lookup tables. 545246854Sdes */ 546246854Sdestypedef struct _ipfw_table_entry { 547269257Sdes in_addr_t addr; /* network address */ 548269257Sdes u_int32_t value; /* value */ 549246854Sdes u_int16_t tbl; /* table number */ 550246854Sdes u_int8_t masklen; /* mask length */ 551246854Sdes} ipfw_table_entry; 552246854Sdes 553246854Sdestypedef struct _ipfw_table { 554246854Sdes u_int32_t size; /* size of entries in bytes */ 555269257Sdes u_int32_t cnt; /* # of entries */ 556269257Sdes u_int16_t tbl; /* table number */ 557246854Sdes ipfw_table_entry ent[0]; /* entries */ 558246854Sdes} ipfw_table; 559246854Sdes 560246854Sdes#define IP_FW_TABLEARG 65535 561246854Sdes 562246854Sdes/* 563246854Sdes * Main firewall chains definitions and global var's definitions. 564269257Sdes */ 565269257Sdes#ifdef _KERNEL 566246854Sdes 567246854Sdes/* Return values from ipfw_chk() */ 568246854Sdesenum { 569246854Sdes IP_FW_PASS = 0, 570246854Sdes IP_FW_DENY, 571246854Sdes IP_FW_DIVERT, 572269257Sdes IP_FW_TEE, 573269257Sdes IP_FW_DUMMYNET, 574246854Sdes IP_FW_NETGRAPH, 575246854Sdes IP_FW_NGTEE, 576246854Sdes IP_FW_NAT, 577246854Sdes}; 578246854Sdes 579246854Sdes/* flags for divert mtag */ 580269257Sdes#define IP_FW_DIVERT_LOOPBACK_FLAG 0x00080000 581269257Sdes#define IP_FW_DIVERT_OUTPUT_FLAG 0x00100000 582246854Sdes 583246854Sdes/* 584246854Sdes * Structure for collecting parameters to dummynet for ip6_output forwarding 585246854Sdes */ 586246854Sdesstruct _ip6dn_args { 587246854Sdes struct ip6_pktopts *opt_or; 588269257Sdes struct route_in6 ro_or; 589269257Sdes int flags_or; 590246854Sdes struct ip6_moptions *im6o_or; 591246854Sdes struct ifnet *origifp_or; 592246854Sdes struct ifnet *ifp_or; 593246854Sdes struct sockaddr_in6 dst_or; 594246854Sdes u_long mtu_or; 595246854Sdes struct route_in6 ro_pmtu_or; 596269257Sdes}; 597269257Sdes 598246854Sdes/* 599246854Sdes * Arguments for calling ipfw_chk() and dummynet_io(). We put them 600246854Sdes * all into a structure because this way it is easier and more 601246854Sdes * efficient to pass variables around and extend the interface. 602246854Sdes */ 603246854Sdesstruct ip_fw_args { 604269257Sdes struct mbuf *m; /* the mbuf chain */ 605269257Sdes struct ifnet *oif; /* output interface */ 606246854Sdes struct sockaddr_in *next_hop; /* forward address */ 607246854Sdes struct ip_fw *rule; /* matching rule */ 608246854Sdes struct ether_header *eh; /* for bridged packets */ 609246854Sdes 610246854Sdes struct ipfw_flow_id f_id; /* grabbed from IP header */ 611246854Sdes u_int32_t cookie; /* a cookie depending on rule action */ 612269257Sdes struct inpcb *inp; 613269257Sdes 614246854Sdes struct _ip6dn_args dummypar; /* dummynet->ip6_output */ 615246854Sdes struct sockaddr_in hopstore; /* store here if cannot use a pointer */ 616246854Sdes}; 617246854Sdes 618246854Sdes/* 619246854Sdes * Function definitions. 620269257Sdes */ 621269257Sdes 622246854Sdes/* Firewall hooks */ 623246854Sdesstruct sockopt; 624246854Sdesstruct dn_flow_set; 625246854Sdes 626246854Sdesint ipfw_check_in(void *, struct mbuf **, struct ifnet *, int, struct inpcb *inp); 627246854Sdesint ipfw_check_out(void *, struct mbuf **, struct ifnet *, int, struct inpcb *inp); 628269257Sdes 629269257Sdesint ipfw_chk(struct ip_fw_args *); 630269257Sdes 631269257Sdesint ipfw_init(void); 632246854Sdesvoid ipfw_destroy(void); 633246854Sdes 634246854Sdestypedef int ip_fw_ctl_t(struct sockopt *); 635246854Sdesextern ip_fw_ctl_t *ip_fw_ctl_ptr; 636246854Sdesextern int fw_one_pass; 637246854Sdesextern int fw_enable; 638246854Sdes#ifdef INET6 639269257Sdesextern int fw6_enable; 640269257Sdes#endif 641246854Sdes 642246854Sdes/* For kernel ipfw_ether and ipfw_bridge. */ 643246854Sdestypedef int ip_fw_chk_t(struct ip_fw_args *args); 644246854Sdesextern ip_fw_chk_t *ip_fw_chk_ptr; 645246854Sdes#define IPFW_LOADED (ip_fw_chk_ptr != NULL) 646246854Sdes 647269257Sdes#ifdef IPFW_INTERNAL 648269257Sdes 649246854Sdesstruct ip_fw_chain { 650246854Sdes struct ip_fw *rules; /* list of rules */ 651246854Sdes struct ip_fw *reap; /* list of rules to reap */ 652246854Sdes LIST_HEAD(, cfg_nat) nat; /* list of nat entries */ 653246854Sdes struct radix_node_head *tables[IPFW_TABLES_MAX]; 654246854Sdes struct rwlock rwmtx; 655269257Sdes}; 656269257Sdes#define IPFW_LOCK_INIT(_chain) \ 657246854Sdes rw_init(&(_chain)->rwmtx, "IPFW static rules") 658246854Sdes#define IPFW_LOCK_DESTROY(_chain) rw_destroy(&(_chain)->rwmtx) 659246854Sdes#define IPFW_WLOCK_ASSERT(_chain) rw_assert(&(_chain)->rwmtx, RA_WLOCKED) 660246854Sdes 661246854Sdes#define IPFW_RLOCK(p) rw_rlock(&(p)->rwmtx) 662246854Sdes#define IPFW_RUNLOCK(p) rw_runlock(&(p)->rwmtx) 663269257Sdes#define IPFW_WLOCK(p) rw_wlock(&(p)->rwmtx) 664269257Sdes#define IPFW_WUNLOCK(p) rw_wunlock(&(p)->rwmtx) 665246854Sdes 666246854Sdes#define LOOKUP_NAT(l, i, p) do { \ 667246854Sdes LIST_FOREACH((p), &(l.nat), _next) { \ 668246854Sdes if ((p)->id == (i)) { \ 669246854Sdes break; \ 670246854Sdes } \ 671269257Sdes } \ 672269257Sdes } while (0) 673246854Sdes 674246854Sdestypedef int ipfw_nat_t(struct ip_fw_args *, struct cfg_nat *, struct mbuf *); 675246854Sdestypedef int ipfw_nat_cfg_t(struct sockopt *); 676246854Sdes#endif 677246854Sdes 678246854Sdes#endif /* _KERNEL */ 679246854Sdes#endif /* _IPFW2_H */ 680269257Sdes