/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#ifndef _IPFW2_H28#define _IPFW2_H2930/*31* The default rule number. By the design of ip_fw, the default rule32* is the last one, so its number can also serve as the highest number33* allowed for a rule. The ip_fw code relies on both meanings of this34* constant.35*/36#define IPFW_DEFAULT_RULE 655353738#define RESVD_SET 31 /*set for default and persistent rules*/39#define IPFW_MAX_SETS 32 /* Number of sets supported by ipfw*/4041/*42* Compat values for old clients43*/44#ifndef _KERNEL45#define IPFW_TABLES_MAX 6553546#define IPFW_TABLES_DEFAULT 12847#endif4849/*50* Most commands (queue, pipe, tag, untag, limit...) can have a 16-bit51* argument between 1 and 65534. The value 0 (IP_FW_TARG) is used52* to represent 'tablearg' value, e.g. indicate the use of a 'tablearg'53* result of the most recent table() lookup.54* Note that 16bit is only a historical limit, resulting from55* the use of a 16-bit fields for that value. In reality, we can have56* 2^32 pipes, queues, tag values and so on.57*/58#define IPFW_ARG_MIN 159#define IPFW_ARG_MAX 6553460#define IP_FW_TABLEARG 65535 /* Compat value for old clients */61#define IP_FW_TARG 0 /* Current tablearg value */62#define IP_FW_NAT44_GLOBAL 65535 /* arg1 value for "nat global" */6364/*65* Number of entries in the call stack of the call/return commands.66* Call stack currently is an uint16_t array with rule numbers.67*/68#define IPFW_CALLSTACK_SIZE 166970/* IP_FW3 header/opcodes */71typedef struct _ip_fw3_opheader {72uint16_t opcode; /* Operation opcode */73uint16_t version; /* Opcode version */74uint16_t reserved[2]; /* Align to 64-bit boundary */75} ip_fw3_opheader;7677#define IP_FW3_OPVER_0 078#define IP_FW3_OPVER_1 1 /* 32bit rulenum */79#define IP_FW3_OPVER IP_FW3_OPVER_18081/* IP_FW3 opcodes */82#define IP_FW_TABLE_XADD 86 /* add entry */83#define IP_FW_TABLE_XDEL 87 /* delete entry */84#define IP_FW_TABLE_XGETSIZE 88 /* get table size (deprecated) */85#define IP_FW_TABLE_XLIST 89 /* list table contents */86#define IP_FW_TABLE_XDESTROY 90 /* destroy table */87#define IP_FW_TABLES_XLIST 92 /* list all tables */88#define IP_FW_TABLE_XINFO 93 /* request info for one table */89#define IP_FW_TABLE_XFLUSH 94 /* flush table data */90#define IP_FW_TABLE_XCREATE 95 /* create new table */91#define IP_FW_TABLE_XMODIFY 96 /* modify existing table */92#define IP_FW_XGET 97 /* Retrieve configuration */93#define IP_FW_XADD 98 /* add rule */94#define IP_FW_XDEL 99 /* del rule */95#define IP_FW_XMOVE 100 /* move rules to different set */96#define IP_FW_XZERO 101 /* clear accounting */97#define IP_FW_XRESETLOG 102 /* zero rules logs */98#define IP_FW_SET_SWAP 103 /* Swap between 2 sets */99#define IP_FW_SET_MOVE 104 /* Move one set to another one */100#define IP_FW_SET_ENABLE 105 /* Enable/disable sets */101#define IP_FW_TABLE_XFIND 106 /* finds an entry */102#define IP_FW_XIFLIST 107 /* list tracked interfaces */103#define IP_FW_TABLES_ALIST 108 /* list table algorithms */104#define IP_FW_TABLE_XSWAP 109 /* swap two tables */105#define IP_FW_TABLE_VLIST 110 /* dump table value hash */106107#define IP_FW_NAT44_XCONFIG 111 /* Create/modify NAT44 instance */108#define IP_FW_NAT44_DESTROY 112 /* Destroys NAT44 instance */109#define IP_FW_NAT44_XGETCONFIG 113 /* Get NAT44 instance config */110#define IP_FW_NAT44_LIST_NAT 114 /* List all NAT44 instances */111#define IP_FW_NAT44_XGETLOG 115 /* Get log from NAT44 instance */112113#define IP_FW_DUMP_SOPTCODES 116 /* Dump available sopts/versions */114#define IP_FW_DUMP_SRVOBJECTS 117 /* Dump existing named objects */115#define IP_FW_SKIPTO_CACHE 118 /* Manage skipto cache */116117#define IP_FW_NAT64STL_CREATE 130 /* Create stateless NAT64 instance */118#define IP_FW_NAT64STL_DESTROY 131 /* Destroy stateless NAT64 instance */119#define IP_FW_NAT64STL_CONFIG 132 /* Modify stateless NAT64 instance */120#define IP_FW_NAT64STL_LIST 133 /* List stateless NAT64 instances */121#define IP_FW_NAT64STL_STATS 134 /* Get NAT64STL instance statistics */122#define IP_FW_NAT64STL_RESET_STATS 135 /* Reset NAT64STL instance statistics */123124#define IP_FW_NAT64LSN_CREATE 140 /* Create stateful NAT64 instance */125#define IP_FW_NAT64LSN_DESTROY 141 /* Destroy stateful NAT64 instance */126#define IP_FW_NAT64LSN_CONFIG 142 /* Modify stateful NAT64 instance */127#define IP_FW_NAT64LSN_LIST 143 /* List stateful NAT64 instances */128#define IP_FW_NAT64LSN_STATS 144 /* Get NAT64LSN instance statistics */129#define IP_FW_NAT64LSN_LIST_STATES 145 /* Get stateful NAT64 states */130#define IP_FW_NAT64LSN_RESET_STATS 146 /* Reset NAT64LSN instance statistics */131132#define IP_FW_NPTV6_CREATE 150 /* Create NPTv6 instance */133#define IP_FW_NPTV6_DESTROY 151 /* Destroy NPTv6 instance */134#define IP_FW_NPTV6_CONFIG 152 /* Modify NPTv6 instance */135#define IP_FW_NPTV6_LIST 153 /* List NPTv6 instances */136#define IP_FW_NPTV6_STATS 154 /* Get NPTv6 instance statistics */137#define IP_FW_NPTV6_RESET_STATS 155 /* Reset NPTv6 instance statistics */138139#define IP_FW_NAT64CLAT_CREATE 160 /* Create clat NAT64 instance */140#define IP_FW_NAT64CLAT_DESTROY 161 /* Destroy clat NAT64 instance */141#define IP_FW_NAT64CLAT_CONFIG 162 /* Modify clat NAT64 instance */142#define IP_FW_NAT64CLAT_LIST 163 /* List clat NAT64 instances */143#define IP_FW_NAT64CLAT_STATS 164 /* Get NAT64CLAT instance statistics */144#define IP_FW_NAT64CLAT_RESET_STATS 165 /* Reset NAT64CLAT instance statistics */145146/*147* The kernel representation of ipfw rules is made of a list of148* 'instructions' (for all practical purposes equivalent to BPF149* instructions), which specify which fields of the packet150* (or its metadata) should be analysed.151*152* Each instruction is stored in a structure which begins with153* "ipfw_insn", and can contain extra fields depending on the154* instruction type (listed below).155* Note that the code is written so that individual instructions156* have a size which is a multiple of 32 bits. This means that, if157* such structures contain pointers or other 64-bit entities,158* (there is just one instance now) they may end up unaligned on159* 64-bit architectures, so the must be handled with care.160*161* "enum ipfw_opcodes" are the opcodes supported. We can have up162* to 256 different opcodes. When adding new opcodes, they should163* be appended to the end of the opcode list before O_LAST_OPCODE,164* this will prevent the ABI from being broken, otherwise users165* will have to recompile ipfw(8) when they update the kernel.166*/167168enum ipfw_opcodes { /* arguments (4 byte each) */169O_NOP = 0,170171O_IP_SRC = 1, /* u32 = IP */172O_IP_SRC_MASK = 2, /* ip = IP/mask */173O_IP_SRC_ME = 3, /* none */174O_IP_SRC_SET = 4, /* u32=base, arg1=len, bitmap */175176O_IP_DST = 5, /* u32 = IP */177O_IP_DST_MASK = 6, /* ip = IP/mask */178O_IP_DST_ME = 7, /* none */179O_IP_DST_SET = 8, /* u32=base, arg1=len, bitmap */180181O_IP_SRCPORT = 9, /* (n)port list:mask 4 byte ea */182O_IP_DSTPORT = 10, /* (n)port list:mask 4 byte ea */183O_PROTO = 11, /* arg1=protocol */184185O_MACADDR2 = 12, /* 2 mac addr:mask */186O_MAC_TYPE = 13, /* same as srcport */187188O_LAYER2 = 14, /* none */189O_IN = 15, /* none */190O_FRAG = 16, /* none */191192O_RECV = 17, /* none */193O_XMIT = 18, /* none */194O_VIA = 19, /* none */195196O_IPOPT = 20, /* arg1 = 2*u8 bitmap */197O_IPLEN = 21, /* arg1 = len */198O_IPID = 22, /* arg1 = id */199200O_IPTOS = 23, /* arg1 = id */201O_IPPRECEDENCE = 24, /* arg1 = precedence << 5 */202O_IPTTL = 25, /* arg1 = TTL */203204O_IPVER = 26, /* arg1 = version */205O_UID = 27, /* u32 = id */206O_GID = 28, /* u32 = id */207O_ESTAB = 29, /* none (tcp established) */208O_TCPFLAGS = 30, /* arg1 = 2*u8 bitmap */209O_TCPWIN = 31, /* arg1 = desired win */210O_TCPSEQ = 32, /* u32 = desired seq. */211O_TCPACK = 33, /* u32 = desired seq. */212O_ICMPTYPE = 34, /* u32 = icmp bitmap */213O_TCPOPTS = 35, /* arg1 = 2*u8 bitmap */214215O_VERREVPATH = 36, /* none */216O_VERSRCREACH = 37, /* none */217218O_PROBE_STATE = 38, /* v0:arg1=kidx, v1:kidx=kidx */219O_KEEP_STATE = 39, /* v0:arg1=kidx, v1:kidx=kidx */220O_LIMIT = 40, /* ipfw_insn_limit */221O_LIMIT_PARENT = 41, /* dyn_type, not an opcode. */222223/*224* These are really 'actions'.225*/226227O_LOG = 42, /* ipfw_insn_log */228O_PROB = 43, /* u32 = match probability */229230O_CHECK_STATE = 44, /* v0:arg1=kidx, v1:kidx=kidx */231O_ACCEPT = 45, /* none */232O_DENY = 46, /* none */233O_REJECT = 47, /* arg1=icmp arg (same as deny) */234O_COUNT = 48, /* none */235O_SKIPTO = 49, /* v0:arg1=next rule number */236/* v1:kidx= next rule number */237O_PIPE = 50, /* arg1=pipe number */238O_QUEUE = 51, /* arg1=queue number */239O_DIVERT = 52, /* arg1=port number */240O_TEE = 53, /* arg1=port number */241O_FORWARD_IP = 54, /* fwd sockaddr */242O_FORWARD_MAC = 55, /* fwd mac */243O_NAT = 56, /* nope */244O_REASS = 57, /* none */245246/*247* More opcodes.248*/249O_IPSEC = 58, /* has ipsec history */250O_IP_SRC_LOOKUP = 59, /* v0:arg1=table number, u32=value */251/* v1:kidx=name, u32=value, arg1=key */252O_IP_DST_LOOKUP = 60, /* arg1=table number, u32=value */253/* v1:kidx=name, u32=value, arg1=key */254O_ANTISPOOF = 61, /* none */255O_JAIL = 62, /* u32 = id */256O_ALTQ = 63, /* u32 = altq classif. qid */257O_DIVERTED = 64, /* arg1=bitmap (1:loop, 2:out) */258O_TCPDATALEN = 65, /* arg1 = tcp data len */259O_IP6_SRC = 66, /* address without mask */260O_IP6_SRC_ME = 67, /* my addresses */261O_IP6_SRC_MASK = 68, /* address with the mask */262O_IP6_DST = 69,263O_IP6_DST_ME = 70,264O_IP6_DST_MASK = 71,265O_FLOW6ID = 72, /* for flow id tag in the ipv6 pkt */266O_ICMP6TYPE = 73, /* icmp6 packet type filtering */267O_EXT_HDR = 74, /* filtering for ipv6 extension header */268O_IP6 = 75,269270/*271* actions for ng_ipfw272*/273O_NETGRAPH = 76, /* send to ng_ipfw */274O_NGTEE = 77, /* copy to ng_ipfw */275276O_IP4 = 78,277278O_UNREACH6 = 79, /* arg1=icmpv6 code arg (deny) */279280O_TAG = 80, /* arg1=tag number */281O_TAGGED = 81, /* arg1=tag number */282283O_SETFIB = 82, /* arg1=FIB number */284O_FIB = 83, /* arg1=FIB desired fib number */285286O_SOCKARG = 84, /* socket argument */287288O_CALLRETURN = 85, /* v0:arg1=called rule number */289/* v1:kidx=called rule number */290291O_FORWARD_IP6 = 86, /* fwd sockaddr_in6 */292293O_DSCP = 87, /* 2 u32 = DSCP mask */294O_SETDSCP = 88, /* arg1=DSCP value */295O_IP_FLOW_LOOKUP = 89, /* v0:arg1=table number, u32=value */296/* v1:kidx=name, u32=value */297298O_EXTERNAL_ACTION = 90, /* v0:arg1=id of external action handler */299/* v1:kidx=id of external action handler */300O_EXTERNAL_INSTANCE = 91, /* v0:arg1=id of eaction handler instance */301/* v1:kidx=id of eaction handler instance */302O_EXTERNAL_DATA = 92, /* variable length data */303304O_SKIP_ACTION = 93, /* none */305O_TCPMSS = 94, /* arg1=MSS value */306307O_MAC_SRC_LOOKUP = 95, /* kidx=name, u32=value, arg1=key */308O_MAC_DST_LOOKUP = 96, /* kidx=name, u32=value, arg1=key */309310O_SETMARK = 97, /* u32 = value */311O_MARK = 98, /* 2 u32 = value, bitmask */312313O_LAST_OPCODE /* not an opcode! */314};315316/*317* The extension header are filtered only for presence using a bit318* vector with a flag for each header.319*/320#define EXT_FRAGMENT 0x1321#define EXT_HOPOPTS 0x2322#define EXT_ROUTING 0x4323#define EXT_AH 0x8324#define EXT_ESP 0x10325#define EXT_DSTOPTS 0x20326#define EXT_RTHDR0 0x40327#define EXT_RTHDR2 0x80328329/*330* Template for instructions.331*332* ipfw_insn is used for all instructions which require no operands,333* a single 16-bit value (arg1), or a couple of 8-bit values.334*335* For other instructions which require different/larger arguments336* we have derived structures, ipfw_insn_*.337*338* The size of the instruction (in 32-bit words) is in the low339* 6 bits of "len". The 2 remaining bits are used to implement340* NOT and OR on individual instructions. Given a type, you can341* compute the length to be put in "len" using F_INSN_SIZE(t)342*343* F_NOT negates the match result of the instruction.344*345* F_OR is used to build or blocks. By default, instructions346* are evaluated as part of a logical AND. An "or" block347* { X or Y or Z } contains F_OR set in all but the last348* instruction of the block. A match will cause the code349* to skip past the last instruction of the block.350*351* NOTA BENE: in a couple of places we assume that352* sizeof(ipfw_insn) == sizeof(u_int32_t)353* this needs to be fixed.354*355*/356typedef struct _ipfw_insn { /* template for instructions */357_Alignas(_Alignof(u_int32_t)) u_int8_t opcode;358u_int8_t len; /* number of 32-bit words */359#define F_NOT 0x80360#define F_OR 0x40361#define F_LEN_MASK 0x3f362#define F_LEN(cmd) ((cmd)->len & F_LEN_MASK)363364u_int16_t arg1;365} ipfw_insn;366367/*368* The F_INSN_SIZE(type) computes the size, in 4-byte words, of369* a given type.370*/371#define F_INSN_SIZE(t) ((sizeof (t))/sizeof(u_int32_t))372373/*374* This is used to store an array of 16-bit entries (ports etc.)375*/376typedef struct _ipfw_insn_u16 {377ipfw_insn o;378u_int16_t ports[2]; /* there may be more */379} ipfw_insn_u16;380381/*382* This is used to store an array of 32-bit entries383* (uid, single IPv4 addresses etc.)384*/385typedef struct _ipfw_insn_u32 {386ipfw_insn o;387u_int32_t d[1]; /* one or more */388} ipfw_insn_u32;389390typedef struct _ipfw_insn_kidx {391ipfw_insn o;392uint32_t kidx;393} ipfw_insn_kidx;394395/*396* This is used to store IP addr-mask pairs.397*/398typedef struct _ipfw_insn_ip {399ipfw_insn o;400struct in_addr addr;401struct in_addr mask;402} ipfw_insn_ip;403404typedef struct _ipfw_insn_table {405ipfw_insn o; /* arg1 is optional lookup key */406uint32_t kidx; /* table name index */407uint32_t value; /* table value */408} ipfw_insn_table;409410#define IPFW_LOOKUP_TYPE_MASK 0x00FF411#define IPFW_LOOKUP_TYPE(insn) ((insn)->arg1 & IPFW_LOOKUP_TYPE_MASK)412#define IPFW_SET_LOOKUP_TYPE(insn, type) do { \413(insn)->arg1 &= ~IPFW_LOOKUP_TYPE_MASK; \414(insn)->arg1 |= (type) & IPFW_LOOKUP_TYPE_MASK; \415} while (0)416417/*418* Defines key types used by lookup instruction419*/420enum ipfw_table_lookup_type {421LOOKUP_NONE = 0,422LOOKUP_DST_IP,423LOOKUP_SRC_IP,424LOOKUP_DST_PORT,425LOOKUP_SRC_PORT,426LOOKUP_UID,427LOOKUP_JAIL,428LOOKUP_DSCP,429LOOKUP_DST_MAC,430LOOKUP_SRC_MAC,431LOOKUP_MARK,432LOOKUP_RULENUM,433};434435enum ipfw_return_type {436RETURN_NEXT_RULENUM = 0,437RETURN_NEXT_RULE,438};439440enum ipfw_skipto_cache_op {441SKIPTO_CACHE_DISABLE = 0,442SKIPTO_CACHE_ENABLE,443};444445/*446* This is used to forward to a given address (ip).447*/448typedef struct _ipfw_insn_sa {449ipfw_insn o;450struct sockaddr_in sa;451} ipfw_insn_sa;452453/*454* This is used to forward to a given address (ipv6).455*/456typedef struct _ipfw_insn_sa6 {457ipfw_insn o;458struct sockaddr_in6 sa;459} ipfw_insn_sa6;460461/*462* This is used for MAC addr-mask pairs.463*/464typedef struct _ipfw_insn_mac {465ipfw_insn o;466u_char addr[12]; /* dst[6] + src[6] */467u_char mask[12]; /* dst[6] + src[6] */468} ipfw_insn_mac;469470/*471* This is used for interface match rules (recv xx, xmit xx).472*/473typedef struct _ipfw_insn_if {474ipfw_insn o;475union {476struct in_addr ip;477int glob;478uint16_t kidx_v0;479uint32_t kidx;480} p;481char name[IFNAMSIZ];482} ipfw_insn_if;483484/*485* This is used for storing an altq queue id number.486*/487typedef struct _ipfw_insn_altq {488ipfw_insn o;489u_int32_t qid;490} ipfw_insn_altq;491492/*493* This is used for limit rules.494*/495typedef struct _ipfw_insn_limit {496ipfw_insn o;497u_int32_t kidx;498u_int8_t _pad;499u_int8_t limit_mask; /* combination of DYN_* below */500#define DYN_SRC_ADDR 0x1501#define DYN_SRC_PORT 0x2502#define DYN_DST_ADDR 0x4503#define DYN_DST_PORT 0x8504505u_int16_t conn_limit;506} ipfw_insn_limit;507508/* MAC/InfiniBand/etc address length */509#define IPFW_MAX_L2_ADDR_LEN 20510511/*512* This is used for log instructions.513*/514typedef struct _ipfw_insn_log {515ipfw_insn o;516u_int32_t max_log; /* how many do we log -- 0 = all */517u_int32_t log_left; /* how many left to log */518} ipfw_insn_log;519520/* ipfw_insn_log->o.arg1 bitmasks */521#define IPFW_LOG_DEFAULT 0x0000522#define IPFW_LOG_SYSLOG (1 << 15)523#define IPFW_LOG_IPFW0 (1 << 14)524#define IPFW_LOG_RTSOCK (1 << 13)525526typedef struct _ipfwlog_rtsock_hdr_v2 {527uint32_t rulenum;528uint32_t tablearg;529ipfw_insn cmd;530u_char ether_shost[IPFW_MAX_L2_ADDR_LEN];531u_char ether_dhost[IPFW_MAX_L2_ADDR_LEN];532uint32_t mark;533char comment[0];534} ipfwlog_rtsock_hdr_v2;535536/* Legacy NAT structures, compat only */537#ifndef _KERNEL538/*539* Data structures required by both ipfw(8) and ipfw(4) but not part of the540* management API are protected by IPFW_INTERNAL.541*/542#ifdef IPFW_INTERNAL543/* Server pool support (LSNAT). */544struct cfg_spool {545LIST_ENTRY(cfg_spool) _next; /* chain of spool instances */546struct in_addr addr;547u_short port;548};549#endif550551/* Redirect modes id. */552#define REDIR_ADDR 0x01553#define REDIR_PORT 0x02554#define REDIR_PROTO 0x04555556#ifdef IPFW_INTERNAL557/* Nat redirect configuration. */558struct cfg_redir {559LIST_ENTRY(cfg_redir) _next; /* chain of redir instances */560u_int16_t mode; /* type of redirect mode */561struct in_addr laddr; /* local ip address */562struct in_addr paddr; /* public ip address */563struct in_addr raddr; /* remote ip address */564u_short lport; /* local port */565u_short pport; /* public port */566u_short rport; /* remote port */567u_short pport_cnt; /* number of public ports */568u_short rport_cnt; /* number of remote ports */569int proto; /* protocol: tcp/udp */570struct alias_link **alink;571/* num of entry in spool chain */572u_int16_t spool_cnt;573/* chain of spool instances */574LIST_HEAD(spool_chain, cfg_spool) spool_chain;575};576#endif577578#ifdef IPFW_INTERNAL579/* Nat configuration data struct. */580struct cfg_nat {581/* chain of nat instances */582LIST_ENTRY(cfg_nat) _next;583int id; /* nat id */584struct in_addr ip; /* nat ip address */585char if_name[IF_NAMESIZE]; /* interface name */586int mode; /* aliasing mode */587struct libalias *lib; /* libalias instance */588/* number of entry in spool chain */589int redir_cnt;590/* chain of redir instances */591LIST_HEAD(redir_chain, cfg_redir) redir_chain;592};593#endif594595#define SOF_NAT sizeof(struct cfg_nat)596#define SOF_REDIR sizeof(struct cfg_redir)597#define SOF_SPOOL sizeof(struct cfg_spool)598599#endif /* ifndef _KERNEL */600601struct nat44_cfg_spool {602struct in_addr addr;603uint16_t port;604uint16_t spare;605};606#define NAT44_REDIR_ADDR 0x01607#define NAT44_REDIR_PORT 0x02608#define NAT44_REDIR_PROTO 0x04609610/* Nat redirect configuration. */611struct nat44_cfg_redir {612struct in_addr laddr; /* local ip address */613struct in_addr paddr; /* public ip address */614struct in_addr raddr; /* remote ip address */615uint16_t lport; /* local port */616uint16_t pport; /* public port */617uint16_t rport; /* remote port */618uint16_t pport_cnt; /* number of public ports */619uint16_t rport_cnt; /* number of remote ports */620uint16_t mode; /* type of redirect mode */621uint16_t spool_cnt; /* num of entry in spool chain */622uint16_t spare;623uint32_t proto; /* protocol: tcp/udp */624};625626/* Nat configuration data struct. */627struct nat44_cfg_nat {628char name[64]; /* nat name */629char if_name[64]; /* interface name */630uint32_t size; /* structure size incl. redirs */631struct in_addr ip; /* nat IPv4 address */632uint32_t mode; /* aliasing mode */633uint32_t redir_cnt; /* number of entry in spool chain */634u_short alias_port_lo; /* low range for port aliasing */635u_short alias_port_hi; /* high range for port aliasing */636};637638/* Nat command. */639typedef struct _ipfw_insn_nat {640ipfw_insn o;641struct cfg_nat *nat;642} ipfw_insn_nat;643644/* Apply ipv6 mask on ipv6 addr */645#define APPLY_MASK(addr,mask) do { \646(addr)->__u6_addr.__u6_addr32[0] &= (mask)->__u6_addr.__u6_addr32[0]; \647(addr)->__u6_addr.__u6_addr32[1] &= (mask)->__u6_addr.__u6_addr32[1]; \648(addr)->__u6_addr.__u6_addr32[2] &= (mask)->__u6_addr.__u6_addr32[2]; \649(addr)->__u6_addr.__u6_addr32[3] &= (mask)->__u6_addr.__u6_addr32[3]; \650} while (0)651652/* Structure for ipv6 */653typedef struct _ipfw_insn_ip6 {654ipfw_insn o;655struct in6_addr addr6;656struct in6_addr mask6;657} ipfw_insn_ip6;658659/* Used to support icmp6 types */660typedef struct _ipfw_insn_icmp6 {661ipfw_insn o;662uint32_t d[7]; /* XXX This number si related to the netinet/icmp6.h663* define ICMP6_MAXTYPE664* as follows: n = ICMP6_MAXTYPE/32 + 1665* Actually is 203666*/667} ipfw_insn_icmp6;668669/* Convert pointer to instruction with specified type */670#define insntod(p, type) ((ipfw_insn_ ## type *)(p))671#define insntoc(p, type) ((const ipfw_insn_ ## type *)(p))672673/*674* Here we have the structure representing an ipfw rule.675*676* Layout:677* struct ip_fw_rule678* [ counter block, size = rule->cntr_len ]679* [ one or more instructions, size = rule->cmd_len * 4 ]680*681* It starts with a general area (with link fields).682* Counter block may be next (if rule->cntr_len > 0),683* followed by an array of one or more instructions, which the code684* accesses as an array of 32-bit values. rule->cmd_len represents685* the total instructions legth in u32 worrd, while act_ofs represents686* rule action offset in u32 words.687*688* When assembling instruction, remember the following:689*690* + if a rule has a "keep-state" (or "limit") option, then the691* first instruction (at r->cmd) MUST BE an O_PROBE_STATE692* + if a rule has a "log" option, then the first action693* (at ACTION_PTR(r)) MUST be O_LOG694* + if a rule has an "altq" option, it comes after "log"695* + if a rule has an O_TAG option, it comes after "log" and "altq"696*697*698* All structures (excluding instructions) are u64-aligned.699* Please keep this.700*/701702struct ip_fw_rule {703uint16_t act_ofs; /* offset of action in 32-bit units */704uint16_t cmd_len; /* # of 32-bit words in cmd */705uint16_t spare;706uint8_t set; /* rule set (0..31) */707uint8_t flags; /* rule flags */708uint32_t rulenum; /* rule number */709uint32_t id; /* rule id */710711ipfw_insn cmd[1]; /* storage for commands */712};713#define IPFW_RULE_NOOPT 0x01 /* Has no options in body */714#define IPFW_RULE_JUSTOPTS 0x02 /* new format of rule body */715716/* Unaligned version */717718/* Base ipfw rule counter block. */719struct ip_fw_bcounter {720uint16_t size; /* Size of counter block, bytes */721uint8_t flags; /* flags for given block */722uint8_t spare;723uint32_t timestamp; /* tv_sec of last match */724uint64_t pcnt; /* Packet counter */725uint64_t bcnt; /* Byte counter */726};727728#ifndef _KERNEL729/*730* Legacy rule format731*/732struct ip_fw {733struct ip_fw *x_next; /* linked list of rules */734struct ip_fw *next_rule; /* ptr to next [skipto] rule */735/* 'next_rule' is used to pass up 'set_disable' status */736737uint16_t act_ofs; /* offset of action in 32-bit units */738uint16_t cmd_len; /* # of 32-bit words in cmd */739uint16_t rulenum; /* rule number */740uint8_t set; /* rule set (0..31) */741uint8_t _pad; /* padding */742uint32_t id; /* rule id */743744/* These fields are present in all rules. */745uint64_t pcnt; /* Packet counter */746uint64_t bcnt; /* Byte counter */747uint32_t timestamp; /* tv_sec of last match */748749ipfw_insn cmd[1]; /* storage for commands */750};751#endif752753#define ACTION_PTR(rule) \754(ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) )755756#define RULESIZE(rule) (sizeof(*(rule)) + (rule)->cmd_len * 4 - 4)757758#if 1 // should be moved to in.h759/*760* This structure is used as a flow mask and a flow id for various761* parts of the code.762* addr_type is used in userland and kernel to mark the address type.763* fib is used in the kernel to record the fib in use.764* _flags is used in the kernel to store tcp flags for dynamic rules.765*/766struct ipfw_flow_id {767uint32_t dst_ip;768uint32_t src_ip;769uint16_t dst_port;770uint16_t src_port;771uint8_t fib; /* XXX: must be uint16_t */772uint8_t proto;773uint8_t _flags; /* protocol-specific flags */774uint8_t addr_type; /* 4=ip4, 6=ip6, 1=ether ? */775struct in6_addr dst_ip6;776struct in6_addr src_ip6;777uint32_t flow_id6;778uint32_t extra; /* queue/pipe or frag_id */779};780#endif781782#define IS_IP4_FLOW_ID(id) ((id)->addr_type == 4)783#define IS_IP6_FLOW_ID(id) ((id)->addr_type == 6)784785/*786* Dynamic ipfw rule.787*/788#define IPFW_DYN_ORPHANED 0x40000 /* state's parent rule was deleted */789790typedef struct _ipfw_dyn_rule {791struct ipfw_flow_id id; /* (masked) flow id */792uint8_t set;793uint8_t type; /* rule type */794uint16_t pad;795uint32_t expire; /* expire time */796uint32_t rulenum; /* parent's rule number */797uint32_t kidx; /* index of named object */798uint64_t pcnt; /* packet match counter */799uint64_t bcnt; /* byte match counter */800uint32_t hashval; /* hash value */801union {802uint32_t state; /* state of this rule (typically a803* combination of TCP flags)804*/805uint32_t count; /* number of linked states */806};807uint32_t ack_fwd; /* most recent ACKs in forward */808uint32_t ack_rev; /* and reverse directions (used */809/* to generate keepalives) */810} __packed __aligned(8) ipfw_dyn_rule;811812/*813* Definitions for IP option names.814*/815#define IP_FW_IPOPT_LSRR 0x01816#define IP_FW_IPOPT_SSRR 0x02817#define IP_FW_IPOPT_RR 0x04818#define IP_FW_IPOPT_TS 0x08819820/*821* Definitions for TCP option names.822*/823#define IP_FW_TCPOPT_MSS 0x01824#define IP_FW_TCPOPT_WINDOW 0x02825#define IP_FW_TCPOPT_SACK 0x04826#define IP_FW_TCPOPT_TS 0x08827#define IP_FW_TCPOPT_CC 0x10828829#define ICMP_REJECT_RST 0x100 /* fake ICMP code (send a TCP RST) */830#define ICMP6_UNREACH_RST 0x100 /* fake ICMPv6 code (send a TCP RST) */831#define ICMP_REJECT_ABORT 0x101 /* fake ICMP code (send an SCTP ABORT) */832#define ICMP6_UNREACH_ABORT 0x101 /* fake ICMPv6 code (send an SCTP ABORT) */833834/*835* These are used for lookup tables.836*/837838#define IPFW_TABLE_ADDR 1 /* Table for holding IPv4/IPv6 prefixes */839#define IPFW_TABLE_INTERFACE 2 /* Table for holding interface names */840#define IPFW_TABLE_NUMBER 3 /* Table for holding ports/uid/gid/etc */841#define IPFW_TABLE_FLOW 4 /* Table for holding flow data */842#define IPFW_TABLE_MAC 5 /* Table for holding mac address prefixes */843#define IPFW_TABLE_MAXTYPE 5 /* Maximum valid number */844845#define IPFW_TABLE_CIDR IPFW_TABLE_ADDR /* compat */846847/* Value types */848#define IPFW_VTYPE_LEGACY 0xFFFFFFFF /* All data is filled in */849#define IPFW_VTYPE_SKIPTO 0x00000001 /* skipto/call/callreturn */850#define IPFW_VTYPE_PIPE 0x00000002 /* pipe/queue */851#define IPFW_VTYPE_FIB 0x00000004 /* setfib */852#define IPFW_VTYPE_NAT 0x00000008 /* nat */853#define IPFW_VTYPE_DSCP 0x00000010 /* dscp */854#define IPFW_VTYPE_TAG 0x00000020 /* tag/untag */855#define IPFW_VTYPE_DIVERT 0x00000040 /* divert/tee */856#define IPFW_VTYPE_NETGRAPH 0x00000080 /* netgraph/ngtee */857#define IPFW_VTYPE_LIMIT 0x00000100 /* limit */858#define IPFW_VTYPE_NH4 0x00000200 /* IPv4 nexthop */859#define IPFW_VTYPE_NH6 0x00000400 /* IPv6 nexthop */860#define IPFW_VTYPE_MARK 0x00000800 /* [fw]mark */861862typedef struct _ipfw_table_xentry {863uint16_t len; /* Total entry length */864uint8_t type; /* entry type */865uint8_t masklen; /* mask length */866uint16_t tbl; /* table number */867uint16_t flags; /* record flags */868uint32_t value; /* value */869union {870/* Longest field needs to be aligned by 4-byte boundary */871struct in6_addr addr6; /* IPv6 address */872char iface[IF_NAMESIZE]; /* interface name */873} k;874} ipfw_table_xentry;875#define IPFW_TCF_INET 0x01 /* CIDR flags: IPv4 record */876877typedef struct _ipfw_xtable {878ip_fw3_opheader opheader; /* IP_FW3 opcode */879uint32_t size; /* size of entries in bytes */880uint32_t cnt; /* # of entries */881uint16_t tbl; /* table number */882uint8_t type; /* table type */883ipfw_table_xentry xent[0]; /* entries */884} ipfw_xtable;885886typedef struct _ipfw_obj_tlv {887uint16_t type; /* TLV type */888uint16_t flags; /* TLV-specific flags */889uint32_t length; /* Total length, aligned to u64 */890} ipfw_obj_tlv;891#define IPFW_TLV_TBL_NAME 1892#define IPFW_TLV_TBLNAME_LIST 2893#define IPFW_TLV_RULE_LIST 3894#define IPFW_TLV_DYNSTATE_LIST 4895#define IPFW_TLV_TBL_ENT 5896#define IPFW_TLV_DYN_ENT 6897#define IPFW_TLV_RULE_ENT 7898#define IPFW_TLV_TBLENT_LIST 8899#define IPFW_TLV_RANGE 9900#define IPFW_TLV_EACTION 10901#define IPFW_TLV_COUNTERS 11902#define IPFW_TLV_OBJDATA 12903#define IPFW_TLV_STATE_NAME 14904905#define IPFW_TLV_EACTION_BASE 1000906#define IPFW_TLV_EACTION_NAME(arg) (IPFW_TLV_EACTION_BASE + (arg))907908typedef struct _ipfw_obj_data {909ipfw_obj_tlv head;910void *data[0];911} ipfw_obj_data;912913/* Object name TLV */914typedef struct _ipfw_obj_ntlv {915ipfw_obj_tlv head; /* TLV header */916uint32_t idx; /* Name index */917uint8_t set; /* set, if applicable */918uint8_t type; /* object type, if applicable */919uint16_t spare; /* unused */920char name[64]; /* Null-terminated name */921} ipfw_obj_ntlv;922923/* IPv4/IPv6 L4 flow description */924struct tflow_entry {925uint8_t af;926uint8_t proto;927uint16_t spare;928uint16_t sport;929uint16_t dport;930union {931struct {932struct in_addr sip;933struct in_addr dip;934} a4;935struct {936struct in6_addr sip6;937struct in6_addr dip6;938} a6;939} a;940};941942#define IPFW_TVALUE_TYPE_MASK 0xFF00943#define IPFW_TVALUE_TYPE(insn) (((insn)->arg1 & IPFW_TVALUE_TYPE_MASK) >> 8)944#define IPFW_SET_TVALUE_TYPE(insn, type) do { \945(insn)->arg1 &= ~IPFW_TVALUE_TYPE_MASK; \946(insn)->arg1 |= ((type) << 8) & IPFW_TVALUE_TYPE_MASK; \947} while (0)948949enum ipfw_table_value_type {950TVALUE_TAG = 0,951TVALUE_PIPE,952TVALUE_DIVERT,953TVALUE_SKIPTO,954TVALUE_NETGRAPH,955TVALUE_FIB,956TVALUE_NAT,957TVALUE_NH4,958TVALUE_DSCP,959TVALUE_LIMIT,960TVALUE_MARK,961};962963/* 64-byte structure representing multi-field table value */964typedef struct _ipfw_table_value {965uint32_t tag; /* O_TAG/O_TAGGED */966uint16_t pipe; /* O_PIPE/O_QUEUE */967uint16_t divert; /* O_DIVERT/O_TEE */968uint32_t skipto; /* skipto, CALLRET */969uint32_t netgraph; /* O_NETGRAPH/O_NGTEE */970uint32_t nat; /* O_NAT */971uint32_t nh4;972uint16_t fib; /* O_SETFIB */973uint8_t dscp;974uint8_t spare0;975uint32_t kidx; /* value kernel index */976struct in6_addr nh6;977uint32_t limit; /* O_LIMIT */978uint32_t zoneid; /* scope zone id for nh6 */979uint32_t mark; /* O_SETMARK/O_MARK */980uint32_t refcnt; /* XXX 64-bit in kernel */981} ipfw_table_value;982983/* Table entry TLV */984typedef struct _ipfw_obj_tentry {985ipfw_obj_tlv head; /* TLV header */986uint8_t subtype; /* subtype (IPv4,IPv6) */987uint8_t masklen; /* mask length */988uint8_t result; /* request result */989uint8_t spare0;990uint32_t idx; /* Table name index */991union {992/* Longest field needs to be aligned by 8-byte boundary */993struct in_addr addr; /* IPv4 address */994uint32_t key; /* uid/gid/port */995struct in6_addr addr6; /* IPv6 address */996char iface[IF_NAMESIZE]; /* interface name */997u_char mac[IPFW_MAX_L2_ADDR_LEN]; /* MAC address */998struct tflow_entry flow;999} k;1000union {1001ipfw_table_value value; /* value data */1002uint32_t kidx; /* value kernel index */1003} v;1004} ipfw_obj_tentry;1005#define IPFW_TF_UPDATE 0x01 /* Update record if exists */1006/* Container TLV */1007#define IPFW_CTF_ATOMIC 0x01 /* Perform atomic operation */1008/* Operation results */1009#define IPFW_TR_IGNORED 0 /* Entry was ignored (rollback) */1010#define IPFW_TR_ADDED 1 /* Entry was successfully added */1011#define IPFW_TR_UPDATED 2 /* Entry was successfully updated*/1012#define IPFW_TR_DELETED 3 /* Entry was successfully deleted*/1013#define IPFW_TR_LIMIT 4 /* Entry was ignored (limit) */1014#define IPFW_TR_NOTFOUND 5 /* Entry was not found */1015#define IPFW_TR_EXISTS 6 /* Entry already exists */1016#define IPFW_TR_ERROR 7 /* Request has failed (unknown) */10171018typedef struct _ipfw_obj_dyntlv {1019ipfw_obj_tlv head;1020ipfw_dyn_rule state;1021} ipfw_obj_dyntlv;1022#define IPFW_DF_LAST 0x01 /* Last state in chain */10231024/* Containter TLVs */1025typedef struct _ipfw_obj_ctlv {1026ipfw_obj_tlv head; /* TLV header */1027uint32_t count; /* Number of sub-TLVs */1028uint16_t objsize; /* Single object size */1029uint8_t version; /* TLV version */1030uint8_t flags; /* TLV-specific flags */1031} ipfw_obj_ctlv;10321033/* Range TLV */1034typedef struct _ipfw_range_tlv {1035ipfw_obj_tlv head; /* TLV header */1036uint32_t flags; /* Range flags */1037uint32_t start_rule; /* Range start */1038uint32_t end_rule; /* Range end */1039uint32_t set; /* Range set to match */1040uint32_t new_set; /* New set to move/swap to */1041} ipfw_range_tlv;1042#define IPFW_RCFLAG_RANGE 0x01 /* rule range is set */1043#define IPFW_RCFLAG_ALL 0x02 /* match ALL rules */1044#define IPFW_RCFLAG_SET 0x04 /* match rules in given set */1045#define IPFW_RCFLAG_DYNAMIC 0x08 /* match only dynamic states */1046/* User-settable flags */1047#define IPFW_RCFLAG_USER (IPFW_RCFLAG_RANGE | IPFW_RCFLAG_ALL | \1048IPFW_RCFLAG_SET | IPFW_RCFLAG_DYNAMIC)1049/* Internally used flags */1050#define IPFW_RCFLAG_DEFAULT 0x0100 /* Do not skip default rule */10511052typedef struct _ipfw_ta_tinfo {1053uint32_t flags; /* Format flags */1054uint32_t spare;1055uint8_t taclass4; /* algorithm class */1056uint8_t spare4;1057uint16_t itemsize4; /* item size in runtime */1058uint32_t size4; /* runtime structure size */1059uint32_t count4; /* number of items in runtime */1060uint8_t taclass6; /* algorithm class */1061uint8_t spare6;1062uint16_t itemsize6; /* item size in runtime */1063uint32_t size6; /* runtime structure size */1064uint32_t count6; /* number of items in runtime */1065} ipfw_ta_tinfo;1066#define IPFW_TACLASS_HASH 1 /* algo is based on hash */1067#define IPFW_TACLASS_ARRAY 2 /* algo is based on array */1068#define IPFW_TACLASS_RADIX 3 /* algo is based on radix tree */10691070#define IPFW_TATFLAGS_DATA 0x0001 /* Has data filled in */1071#define IPFW_TATFLAGS_AFDATA 0x0002 /* Separate data per AF */1072#define IPFW_TATFLAGS_AFITEM 0x0004 /* diff. items per AF */10731074typedef struct _ipfw_xtable_info {1075uint8_t type; /* table type (addr,iface,..) */1076uint8_t tflags; /* type flags */1077uint16_t mflags; /* modification flags */1078uint16_t flags; /* generic table flags */1079uint16_t spare[3];1080uint32_t vmask; /* bitmask with value types */1081uint32_t set; /* set table is in */1082uint32_t kidx; /* kernel index */1083uint32_t refcnt; /* number of references */1084uint32_t count; /* Number of records */1085uint32_t size; /* Total size of records(export)*/1086uint32_t limit; /* Max number of records */1087char tablename[64]; /* table name */1088char algoname[64]; /* algorithm name */1089ipfw_ta_tinfo ta_info; /* additional algo stats */1090} ipfw_xtable_info;1091/* Generic table flags */1092#define IPFW_TGFLAGS_LOCKED 0x01 /* Tables is locked from changes*/1093/* Table type-specific flags */1094#define IPFW_TFFLAG_SRCIP 0x011095#define IPFW_TFFLAG_DSTIP 0x021096#define IPFW_TFFLAG_SRCPORT 0x041097#define IPFW_TFFLAG_DSTPORT 0x081098#define IPFW_TFFLAG_PROTO 0x101099/* Table modification flags */1100#define IPFW_TMFLAGS_LIMIT 0x0002 /* Change limit value */1101#define IPFW_TMFLAGS_LOCK 0x0004 /* Change table lock state */11021103typedef struct _ipfw_iface_info {1104char ifname[64]; /* interface name */1105uint32_t ifindex; /* interface index */1106uint32_t flags; /* flags */1107uint32_t refcnt; /* number of references */1108uint32_t gencnt; /* number of changes */1109uint64_t spare;1110} ipfw_iface_info;1111#define IPFW_IFFLAG_RESOLVED 0x01 /* Interface exists */11121113typedef struct _ipfw_ta_info {1114char algoname[64]; /* algorithm name */1115uint32_t type; /* lookup type */1116uint32_t flags;1117uint32_t refcnt;1118uint32_t spare0;1119uint64_t spare1;1120} ipfw_ta_info;11211122typedef struct _ipfw_cmd_header { /* control command header */1123ip_fw3_opheader opheader; /* IP_FW3 opcode */1124uint32_t size; /* Total size (incl. header) */1125uint32_t cmd; /* command */1126} ipfw_cmd_header;11271128typedef struct _ipfw_obj_header {1129ip_fw3_opheader opheader; /* IP_FW3 opcode */1130uint32_t idx; /* object name index */1131uint16_t spare;1132uint8_t objtype; /* object type */1133uint8_t objsubtype; /* object subtype */1134ipfw_obj_ntlv ntlv; /* object name tlv */1135} ipfw_obj_header;11361137typedef struct _ipfw_obj_lheader {1138ip_fw3_opheader opheader; /* IP_FW3 opcode */1139uint32_t set_mask; /* disabled set mask */1140uint32_t count; /* Total objects count */1141uint32_t size; /* Total size (incl. header) */1142uint32_t objsize; /* Size of one object */1143} ipfw_obj_lheader;11441145#define IPFW_CFG_GET_STATIC 0x011146#define IPFW_CFG_GET_STATES 0x021147#define IPFW_CFG_GET_COUNTERS 0x041148typedef struct _ipfw_cfg_lheader {1149ip_fw3_opheader opheader; /* IP_FW3 opcode */1150uint32_t set_mask; /* enabled set mask */1151uint32_t spare;1152uint32_t flags; /* Request flags */1153uint32_t size; /* neded buffer size */1154uint32_t start_rule;1155uint32_t end_rule;1156} ipfw_cfg_lheader;11571158typedef struct _ipfw_range_header {1159ip_fw3_opheader opheader; /* IP_FW3 opcode */1160ipfw_range_tlv range;1161} ipfw_range_header;11621163typedef struct _ipfw_sopt_info {1164uint16_t opcode;1165uint8_t version;1166uint8_t dir;1167uint8_t spare;1168uint64_t refcnt;1169} ipfw_sopt_info;11701171#endif /* _IPFW2_H */117211731174