1127dd473Swhl739 /*- 2*d4a07e70Sfengbojiang * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*d4a07e70Sfengbojiang * 4127dd473Swhl739 * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa 5127dd473Swhl739 * 6127dd473Swhl739 * Redistribution and use in source and binary forms, with or without 7127dd473Swhl739 * modification, are permitted provided that the following conditions 8127dd473Swhl739 * are met: 9127dd473Swhl739 * 1. Redistributions of source code must retain the above copyright 10127dd473Swhl739 * notice, this list of conditions and the following disclaimer. 11127dd473Swhl739 * 2. Redistributions in binary form must reproduce the above copyright 12127dd473Swhl739 * notice, this list of conditions and the following disclaimer in the 13127dd473Swhl739 * documentation and/or other materials provided with the distribution. 14127dd473Swhl739 * 15127dd473Swhl739 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16127dd473Swhl739 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17127dd473Swhl739 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18127dd473Swhl739 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19127dd473Swhl739 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20127dd473Swhl739 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21127dd473Swhl739 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22127dd473Swhl739 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23127dd473Swhl739 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24127dd473Swhl739 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25127dd473Swhl739 * SUCH DAMAGE. 26127dd473Swhl739 * 27127dd473Swhl739 * $FreeBSD$ 28127dd473Swhl739 */ 29127dd473Swhl739 30127dd473Swhl739 #ifndef _IPFW2_H 31127dd473Swhl739 #define _IPFW2_H 32127dd473Swhl739 33127dd473Swhl739 /* 34127dd473Swhl739 * The default rule number. By the design of ip_fw, the default rule 35127dd473Swhl739 * is the last one, so its number can also serve as the highest number 36127dd473Swhl739 * allowed for a rule. The ip_fw code relies on both meanings of this 37127dd473Swhl739 * constant. 38127dd473Swhl739 */ 39127dd473Swhl739 #define IPFW_DEFAULT_RULE 65535 40127dd473Swhl739 41127dd473Swhl739 #define RESVD_SET 31 /*set for default and persistent rules*/ 42127dd473Swhl739 #define IPFW_MAX_SETS 32 /* Number of sets supported by ipfw*/ 43127dd473Swhl739 44127dd473Swhl739 /* 45127dd473Swhl739 * Compat values for old clients 46127dd473Swhl739 */ 47127dd473Swhl739 #ifndef _KERNEL 48127dd473Swhl739 #define IPFW_TABLES_MAX 65535 49127dd473Swhl739 #define IPFW_TABLES_DEFAULT 128 50127dd473Swhl739 #endif 51127dd473Swhl739 52127dd473Swhl739 /* 53127dd473Swhl739 * Most commands (queue, pipe, tag, untag, limit...) can have a 16-bit 54127dd473Swhl739 * argument between 1 and 65534. The value 0 (IP_FW_TARG) is used 55127dd473Swhl739 * to represent 'tablearg' value, e.g. indicate the use of a 'tablearg' 56127dd473Swhl739 * result of the most recent table() lookup. 57127dd473Swhl739 * Note that 16bit is only a historical limit, resulting from 58127dd473Swhl739 * the use of a 16-bit fields for that value. In reality, we can have 59127dd473Swhl739 * 2^32 pipes, queues, tag values and so on. 60127dd473Swhl739 */ 61127dd473Swhl739 #define IPFW_ARG_MIN 1 62127dd473Swhl739 #define IPFW_ARG_MAX 65534 63127dd473Swhl739 #define IP_FW_TABLEARG 65535 /* Compat value for old clients */ 64127dd473Swhl739 #define IP_FW_TARG 0 /* Current tablearg value */ 65127dd473Swhl739 #define IP_FW_NAT44_GLOBAL 65535 /* arg1 value for "nat global" */ 66127dd473Swhl739 67127dd473Swhl739 /* 68127dd473Swhl739 * Number of entries in the call stack of the call/return commands. 69127dd473Swhl739 * Call stack currently is an uint16_t array with rule numbers. 70127dd473Swhl739 */ 71127dd473Swhl739 #define IPFW_CALLSTACK_SIZE 16 72127dd473Swhl739 73127dd473Swhl739 /* IP_FW3 header/opcodes */ 74127dd473Swhl739 typedef struct _ip_fw3_opheader { 75127dd473Swhl739 uint16_t opcode; /* Operation opcode */ 76127dd473Swhl739 uint16_t version; /* Opcode version */ 77127dd473Swhl739 uint16_t reserved[2]; /* Align to 64-bit boundary */ 78127dd473Swhl739 } ip_fw3_opheader; 79127dd473Swhl739 80127dd473Swhl739 /* IP_FW3 opcodes */ 81127dd473Swhl739 #define IP_FW_TABLE_XADD 86 /* add entry */ 82127dd473Swhl739 #define IP_FW_TABLE_XDEL 87 /* delete entry */ 83127dd473Swhl739 #define IP_FW_TABLE_XGETSIZE 88 /* get table size (deprecated) */ 84127dd473Swhl739 #define IP_FW_TABLE_XLIST 89 /* list table contents */ 85127dd473Swhl739 #define IP_FW_TABLE_XDESTROY 90 /* destroy table */ 86127dd473Swhl739 #define IP_FW_TABLES_XLIST 92 /* list all tables */ 87127dd473Swhl739 #define IP_FW_TABLE_XINFO 93 /* request info for one table */ 88127dd473Swhl739 #define IP_FW_TABLE_XFLUSH 94 /* flush table data */ 89127dd473Swhl739 #define IP_FW_TABLE_XCREATE 95 /* create new table */ 90127dd473Swhl739 #define IP_FW_TABLE_XMODIFY 96 /* modify existing table */ 91127dd473Swhl739 #define IP_FW_XGET 97 /* Retrieve configuration */ 92127dd473Swhl739 #define IP_FW_XADD 98 /* add rule */ 93127dd473Swhl739 #define IP_FW_XDEL 99 /* del rule */ 94127dd473Swhl739 #define IP_FW_XMOVE 100 /* move rules to different set */ 95127dd473Swhl739 #define IP_FW_XZERO 101 /* clear accounting */ 96127dd473Swhl739 #define IP_FW_XRESETLOG 102 /* zero rules logs */ 97127dd473Swhl739 #define IP_FW_SET_SWAP 103 /* Swap between 2 sets */ 98127dd473Swhl739 #define IP_FW_SET_MOVE 104 /* Move one set to another one */ 99127dd473Swhl739 #define IP_FW_SET_ENABLE 105 /* Enable/disable sets */ 100127dd473Swhl739 #define IP_FW_TABLE_XFIND 106 /* finds an entry */ 101127dd473Swhl739 #define IP_FW_XIFLIST 107 /* list tracked interfaces */ 102127dd473Swhl739 #define IP_FW_TABLES_ALIST 108 /* list table algorithms */ 103127dd473Swhl739 #define IP_FW_TABLE_XSWAP 109 /* swap two tables */ 104127dd473Swhl739 #define IP_FW_TABLE_VLIST 110 /* dump table value hash */ 105127dd473Swhl739 106127dd473Swhl739 #define IP_FW_NAT44_XCONFIG 111 /* Create/modify NAT44 instance */ 107127dd473Swhl739 #define IP_FW_NAT44_DESTROY 112 /* Destroys NAT44 instance */ 108127dd473Swhl739 #define IP_FW_NAT44_XGETCONFIG 113 /* Get NAT44 instance config */ 109127dd473Swhl739 #define IP_FW_NAT44_LIST_NAT 114 /* List all NAT44 instances */ 110127dd473Swhl739 #define IP_FW_NAT44_XGETLOG 115 /* Get log from NAT44 instance */ 111127dd473Swhl739 112127dd473Swhl739 #define IP_FW_DUMP_SOPTCODES 116 /* Dump available sopts/versions */ 113127dd473Swhl739 #define IP_FW_DUMP_SRVOBJECTS 117 /* Dump existing named objects */ 114127dd473Swhl739 115*d4a07e70Sfengbojiang #define IP_FW_NAT64STL_CREATE 130 /* Create stateless NAT64 instance */ 116*d4a07e70Sfengbojiang #define IP_FW_NAT64STL_DESTROY 131 /* Destroy stateless NAT64 instance */ 117*d4a07e70Sfengbojiang #define IP_FW_NAT64STL_CONFIG 132 /* Modify stateless NAT64 instance */ 118*d4a07e70Sfengbojiang #define IP_FW_NAT64STL_LIST 133 /* List stateless NAT64 instances */ 119*d4a07e70Sfengbojiang #define IP_FW_NAT64STL_STATS 134 /* Get NAT64STL instance statistics */ 120*d4a07e70Sfengbojiang #define IP_FW_NAT64STL_RESET_STATS 135 /* Reset NAT64STL instance statistics */ 121*d4a07e70Sfengbojiang 122*d4a07e70Sfengbojiang #define IP_FW_NAT64LSN_CREATE 140 /* Create stateful NAT64 instance */ 123*d4a07e70Sfengbojiang #define IP_FW_NAT64LSN_DESTROY 141 /* Destroy stateful NAT64 instance */ 124*d4a07e70Sfengbojiang #define IP_FW_NAT64LSN_CONFIG 142 /* Modify stateful NAT64 instance */ 125*d4a07e70Sfengbojiang #define IP_FW_NAT64LSN_LIST 143 /* List stateful NAT64 instances */ 126*d4a07e70Sfengbojiang #define IP_FW_NAT64LSN_STATS 144 /* Get NAT64LSN instance statistics */ 127*d4a07e70Sfengbojiang #define IP_FW_NAT64LSN_LIST_STATES 145 /* Get stateful NAT64 states */ 128*d4a07e70Sfengbojiang #define IP_FW_NAT64LSN_RESET_STATS 146 /* Reset NAT64LSN instance statistics */ 129*d4a07e70Sfengbojiang 130*d4a07e70Sfengbojiang #define IP_FW_NPTV6_CREATE 150 /* Create NPTv6 instance */ 131*d4a07e70Sfengbojiang #define IP_FW_NPTV6_DESTROY 151 /* Destroy NPTv6 instance */ 132*d4a07e70Sfengbojiang #define IP_FW_NPTV6_CONFIG 152 /* Modify NPTv6 instance */ 133*d4a07e70Sfengbojiang #define IP_FW_NPTV6_LIST 153 /* List NPTv6 instances */ 134*d4a07e70Sfengbojiang #define IP_FW_NPTV6_STATS 154 /* Get NPTv6 instance statistics */ 135*d4a07e70Sfengbojiang #define IP_FW_NPTV6_RESET_STATS 155 /* Reset NPTv6 instance statistics */ 136*d4a07e70Sfengbojiang 137*d4a07e70Sfengbojiang #define IP_FW_NAT64CLAT_CREATE 160 /* Create clat NAT64 instance */ 138*d4a07e70Sfengbojiang #define IP_FW_NAT64CLAT_DESTROY 161 /* Destroy clat NAT64 instance */ 139*d4a07e70Sfengbojiang #define IP_FW_NAT64CLAT_CONFIG 162 /* Modify clat NAT64 instance */ 140*d4a07e70Sfengbojiang #define IP_FW_NAT64CLAT_LIST 163 /* List clat NAT64 instances */ 141*d4a07e70Sfengbojiang #define IP_FW_NAT64CLAT_STATS 164 /* Get NAT64CLAT instance statistics */ 142*d4a07e70Sfengbojiang #define IP_FW_NAT64CLAT_RESET_STATS 165 /* Reset NAT64CLAT instance statistics */ 143*d4a07e70Sfengbojiang 144127dd473Swhl739 /* 145127dd473Swhl739 * The kernel representation of ipfw rules is made of a list of 146127dd473Swhl739 * 'instructions' (for all practical purposes equivalent to BPF 147127dd473Swhl739 * instructions), which specify which fields of the packet 148127dd473Swhl739 * (or its metadata) should be analysed. 149127dd473Swhl739 * 150127dd473Swhl739 * Each instruction is stored in a structure which begins with 151127dd473Swhl739 * "ipfw_insn", and can contain extra fields depending on the 152127dd473Swhl739 * instruction type (listed below). 153127dd473Swhl739 * Note that the code is written so that individual instructions 154127dd473Swhl739 * have a size which is a multiple of 32 bits. This means that, if 155127dd473Swhl739 * such structures contain pointers or other 64-bit entities, 156127dd473Swhl739 * (there is just one instance now) they may end up unaligned on 157127dd473Swhl739 * 64-bit architectures, so the must be handled with care. 158127dd473Swhl739 * 159127dd473Swhl739 * "enum ipfw_opcodes" are the opcodes supported. We can have up 160127dd473Swhl739 * to 256 different opcodes. When adding new opcodes, they should 161127dd473Swhl739 * be appended to the end of the opcode list before O_LAST_OPCODE, 162127dd473Swhl739 * this will prevent the ABI from being broken, otherwise users 163127dd473Swhl739 * will have to recompile ipfw(8) when they update the kernel. 164127dd473Swhl739 */ 165127dd473Swhl739 166127dd473Swhl739 enum ipfw_opcodes { /* arguments (4 byte each) */ 167127dd473Swhl739 O_NOP, 168127dd473Swhl739 169127dd473Swhl739 O_IP_SRC, /* u32 = IP */ 170127dd473Swhl739 O_IP_SRC_MASK, /* ip = IP/mask */ 171127dd473Swhl739 O_IP_SRC_ME, /* none */ 172127dd473Swhl739 O_IP_SRC_SET, /* u32=base, arg1=len, bitmap */ 173127dd473Swhl739 174127dd473Swhl739 O_IP_DST, /* u32 = IP */ 175127dd473Swhl739 O_IP_DST_MASK, /* ip = IP/mask */ 176127dd473Swhl739 O_IP_DST_ME, /* none */ 177127dd473Swhl739 O_IP_DST_SET, /* u32=base, arg1=len, bitmap */ 178127dd473Swhl739 179127dd473Swhl739 O_IP_SRCPORT, /* (n)port list:mask 4 byte ea */ 180127dd473Swhl739 O_IP_DSTPORT, /* (n)port list:mask 4 byte ea */ 181127dd473Swhl739 O_PROTO, /* arg1=protocol */ 182127dd473Swhl739 183127dd473Swhl739 O_MACADDR2, /* 2 mac addr:mask */ 184127dd473Swhl739 O_MAC_TYPE, /* same as srcport */ 185127dd473Swhl739 186127dd473Swhl739 O_LAYER2, /* none */ 187127dd473Swhl739 O_IN, /* none */ 188127dd473Swhl739 O_FRAG, /* none */ 189127dd473Swhl739 190127dd473Swhl739 O_RECV, /* none */ 191127dd473Swhl739 O_XMIT, /* none */ 192127dd473Swhl739 O_VIA, /* none */ 193127dd473Swhl739 194127dd473Swhl739 O_IPOPT, /* arg1 = 2*u8 bitmap */ 195127dd473Swhl739 O_IPLEN, /* arg1 = len */ 196127dd473Swhl739 O_IPID, /* arg1 = id */ 197127dd473Swhl739 198127dd473Swhl739 O_IPTOS, /* arg1 = id */ 199127dd473Swhl739 O_IPPRECEDENCE, /* arg1 = precedence << 5 */ 200127dd473Swhl739 O_IPTTL, /* arg1 = TTL */ 201127dd473Swhl739 202127dd473Swhl739 O_IPVER, /* arg1 = version */ 203127dd473Swhl739 O_UID, /* u32 = id */ 204127dd473Swhl739 O_GID, /* u32 = id */ 205127dd473Swhl739 O_ESTAB, /* none (tcp established) */ 206127dd473Swhl739 O_TCPFLAGS, /* arg1 = 2*u8 bitmap */ 207127dd473Swhl739 O_TCPWIN, /* arg1 = desired win */ 208127dd473Swhl739 O_TCPSEQ, /* u32 = desired seq. */ 209127dd473Swhl739 O_TCPACK, /* u32 = desired seq. */ 210127dd473Swhl739 O_ICMPTYPE, /* u32 = icmp bitmap */ 211127dd473Swhl739 O_TCPOPTS, /* arg1 = 2*u8 bitmap */ 212127dd473Swhl739 213127dd473Swhl739 O_VERREVPATH, /* none */ 214127dd473Swhl739 O_VERSRCREACH, /* none */ 215127dd473Swhl739 216127dd473Swhl739 O_PROBE_STATE, /* none */ 217127dd473Swhl739 O_KEEP_STATE, /* none */ 218127dd473Swhl739 O_LIMIT, /* ipfw_insn_limit */ 219127dd473Swhl739 O_LIMIT_PARENT, /* dyn_type, not an opcode. */ 220127dd473Swhl739 221127dd473Swhl739 /* 222127dd473Swhl739 * These are really 'actions'. 223127dd473Swhl739 */ 224127dd473Swhl739 225127dd473Swhl739 O_LOG, /* ipfw_insn_log */ 226127dd473Swhl739 O_PROB, /* u32 = match probability */ 227127dd473Swhl739 228127dd473Swhl739 O_CHECK_STATE, /* none */ 229127dd473Swhl739 O_ACCEPT, /* none */ 230127dd473Swhl739 O_DENY, /* none */ 231127dd473Swhl739 O_REJECT, /* arg1=icmp arg (same as deny) */ 232127dd473Swhl739 O_COUNT, /* none */ 233127dd473Swhl739 O_SKIPTO, /* arg1=next rule number */ 234127dd473Swhl739 O_PIPE, /* arg1=pipe number */ 235127dd473Swhl739 O_QUEUE, /* arg1=queue number */ 236127dd473Swhl739 O_DIVERT, /* arg1=port number */ 237127dd473Swhl739 O_TEE, /* arg1=port number */ 238127dd473Swhl739 O_FORWARD_IP, /* fwd sockaddr */ 239127dd473Swhl739 O_FORWARD_MAC, /* fwd mac */ 240127dd473Swhl739 O_NAT, /* nope */ 241127dd473Swhl739 O_REASS, /* none */ 242127dd473Swhl739 243127dd473Swhl739 /* 244127dd473Swhl739 * More opcodes. 245127dd473Swhl739 */ 246127dd473Swhl739 O_IPSEC, /* has ipsec history */ 247127dd473Swhl739 O_IP_SRC_LOOKUP, /* arg1=table number, u32=value */ 248127dd473Swhl739 O_IP_DST_LOOKUP, /* arg1=table number, u32=value */ 249127dd473Swhl739 O_ANTISPOOF, /* none */ 250127dd473Swhl739 O_JAIL, /* u32 = id */ 251127dd473Swhl739 O_ALTQ, /* u32 = altq classif. qid */ 252127dd473Swhl739 O_DIVERTED, /* arg1=bitmap (1:loop, 2:out) */ 253127dd473Swhl739 O_TCPDATALEN, /* arg1 = tcp data len */ 254127dd473Swhl739 O_IP6_SRC, /* address without mask */ 255127dd473Swhl739 O_IP6_SRC_ME, /* my addresses */ 256127dd473Swhl739 O_IP6_SRC_MASK, /* address with the mask */ 257127dd473Swhl739 O_IP6_DST, 258127dd473Swhl739 O_IP6_DST_ME, 259127dd473Swhl739 O_IP6_DST_MASK, 260127dd473Swhl739 O_FLOW6ID, /* for flow id tag in the ipv6 pkt */ 261127dd473Swhl739 O_ICMP6TYPE, /* icmp6 packet type filtering */ 262127dd473Swhl739 O_EXT_HDR, /* filtering for ipv6 extension header */ 263127dd473Swhl739 O_IP6, 264127dd473Swhl739 265127dd473Swhl739 /* 266127dd473Swhl739 * actions for ng_ipfw 267127dd473Swhl739 */ 268127dd473Swhl739 O_NETGRAPH, /* send to ng_ipfw */ 269127dd473Swhl739 O_NGTEE, /* copy to ng_ipfw */ 270127dd473Swhl739 271127dd473Swhl739 O_IP4, 272127dd473Swhl739 273127dd473Swhl739 O_UNREACH6, /* arg1=icmpv6 code arg (deny) */ 274127dd473Swhl739 275127dd473Swhl739 O_TAG, /* arg1=tag number */ 276127dd473Swhl739 O_TAGGED, /* arg1=tag number */ 277127dd473Swhl739 278127dd473Swhl739 O_SETFIB, /* arg1=FIB number */ 279127dd473Swhl739 O_FIB, /* arg1=FIB desired fib number */ 280127dd473Swhl739 281127dd473Swhl739 O_SOCKARG, /* socket argument */ 282127dd473Swhl739 283127dd473Swhl739 O_CALLRETURN, /* arg1=called rule number */ 284127dd473Swhl739 285127dd473Swhl739 O_FORWARD_IP6, /* fwd sockaddr_in6 */ 286127dd473Swhl739 287127dd473Swhl739 O_DSCP, /* 2 u32 = DSCP mask */ 288127dd473Swhl739 O_SETDSCP, /* arg1=DSCP value */ 289127dd473Swhl739 O_IP_FLOW_LOOKUP, /* arg1=table number, u32=value */ 290127dd473Swhl739 291127dd473Swhl739 O_EXTERNAL_ACTION, /* arg1=id of external action handler */ 292127dd473Swhl739 O_EXTERNAL_INSTANCE, /* arg1=id of eaction handler instance */ 293*d4a07e70Sfengbojiang O_EXTERNAL_DATA, /* variable length data */ 294*d4a07e70Sfengbojiang 295*d4a07e70Sfengbojiang O_SKIP_ACTION, /* none */ 296*d4a07e70Sfengbojiang O_TCPMSS, /* arg1=MSS value */ 297127dd473Swhl739 298127dd473Swhl739 O_LAST_OPCODE /* not an opcode! */ 299127dd473Swhl739 }; 300127dd473Swhl739 301127dd473Swhl739 /* 302127dd473Swhl739 * The extension header are filtered only for presence using a bit 303127dd473Swhl739 * vector with a flag for each header. 304127dd473Swhl739 */ 305127dd473Swhl739 #define EXT_FRAGMENT 0x1 306127dd473Swhl739 #define EXT_HOPOPTS 0x2 307127dd473Swhl739 #define EXT_ROUTING 0x4 308127dd473Swhl739 #define EXT_AH 0x8 309127dd473Swhl739 #define EXT_ESP 0x10 310127dd473Swhl739 #define EXT_DSTOPTS 0x20 311127dd473Swhl739 #define EXT_RTHDR0 0x40 312127dd473Swhl739 #define EXT_RTHDR2 0x80 313127dd473Swhl739 314127dd473Swhl739 /* 315127dd473Swhl739 * Template for instructions. 316127dd473Swhl739 * 317127dd473Swhl739 * ipfw_insn is used for all instructions which require no operands, 318127dd473Swhl739 * a single 16-bit value (arg1), or a couple of 8-bit values. 319127dd473Swhl739 * 320127dd473Swhl739 * For other instructions which require different/larger arguments 321127dd473Swhl739 * we have derived structures, ipfw_insn_*. 322127dd473Swhl739 * 323127dd473Swhl739 * The size of the instruction (in 32-bit words) is in the low 324127dd473Swhl739 * 6 bits of "len". The 2 remaining bits are used to implement 325127dd473Swhl739 * NOT and OR on individual instructions. Given a type, you can 326127dd473Swhl739 * compute the length to be put in "len" using F_INSN_SIZE(t) 327127dd473Swhl739 * 328127dd473Swhl739 * F_NOT negates the match result of the instruction. 329127dd473Swhl739 * 330127dd473Swhl739 * F_OR is used to build or blocks. By default, instructions 331127dd473Swhl739 * are evaluated as part of a logical AND. An "or" block 332127dd473Swhl739 * { X or Y or Z } contains F_OR set in all but the last 333127dd473Swhl739 * instruction of the block. A match will cause the code 334127dd473Swhl739 * to skip past the last instruction of the block. 335127dd473Swhl739 * 336127dd473Swhl739 * NOTA BENE: in a couple of places we assume that 337127dd473Swhl739 * sizeof(ipfw_insn) == sizeof(u_int32_t) 338127dd473Swhl739 * this needs to be fixed. 339127dd473Swhl739 * 340127dd473Swhl739 */ 341127dd473Swhl739 typedef struct _ipfw_insn { /* template for instructions */ 342*d4a07e70Sfengbojiang _Alignas(_Alignof(u_int32_t)) u_int8_t opcode; 343127dd473Swhl739 u_int8_t len; /* number of 32-bit words */ 344127dd473Swhl739 #define F_NOT 0x80 345127dd473Swhl739 #define F_OR 0x40 346127dd473Swhl739 #define F_LEN_MASK 0x3f 347127dd473Swhl739 #define F_LEN(cmd) ((cmd)->len & F_LEN_MASK) 348127dd473Swhl739 349127dd473Swhl739 u_int16_t arg1; 350127dd473Swhl739 } ipfw_insn; 351127dd473Swhl739 352127dd473Swhl739 /* 353127dd473Swhl739 * The F_INSN_SIZE(type) computes the size, in 4-byte words, of 354127dd473Swhl739 * a given type. 355127dd473Swhl739 */ 356127dd473Swhl739 #define F_INSN_SIZE(t) ((sizeof (t))/sizeof(u_int32_t)) 357127dd473Swhl739 358127dd473Swhl739 /* 359127dd473Swhl739 * This is used to store an array of 16-bit entries (ports etc.) 360127dd473Swhl739 */ 361127dd473Swhl739 typedef struct _ipfw_insn_u16 { 362127dd473Swhl739 ipfw_insn o; 363127dd473Swhl739 u_int16_t ports[2]; /* there may be more */ 364127dd473Swhl739 } ipfw_insn_u16; 365127dd473Swhl739 366127dd473Swhl739 /* 367127dd473Swhl739 * This is used to store an array of 32-bit entries 368127dd473Swhl739 * (uid, single IPv4 addresses etc.) 369127dd473Swhl739 */ 370127dd473Swhl739 typedef struct _ipfw_insn_u32 { 371127dd473Swhl739 ipfw_insn o; 372127dd473Swhl739 u_int32_t d[1]; /* one or more */ 373127dd473Swhl739 } ipfw_insn_u32; 374127dd473Swhl739 375127dd473Swhl739 /* 376127dd473Swhl739 * This is used to store IP addr-mask pairs. 377127dd473Swhl739 */ 378127dd473Swhl739 typedef struct _ipfw_insn_ip { 379127dd473Swhl739 ipfw_insn o; 380127dd473Swhl739 struct in_addr addr; 381127dd473Swhl739 struct in_addr mask; 382127dd473Swhl739 } ipfw_insn_ip; 383127dd473Swhl739 384127dd473Swhl739 /* 385127dd473Swhl739 * This is used to forward to a given address (ip). 386127dd473Swhl739 */ 387127dd473Swhl739 typedef struct _ipfw_insn_sa { 388127dd473Swhl739 ipfw_insn o; 389127dd473Swhl739 struct sockaddr_in sa; 390127dd473Swhl739 } ipfw_insn_sa; 391127dd473Swhl739 392127dd473Swhl739 /* 393127dd473Swhl739 * This is used to forward to a given address (ipv6). 394127dd473Swhl739 */ 395127dd473Swhl739 typedef struct _ipfw_insn_sa6 { 396127dd473Swhl739 ipfw_insn o; 397127dd473Swhl739 struct sockaddr_in6 sa; 398127dd473Swhl739 } ipfw_insn_sa6; 399127dd473Swhl739 400127dd473Swhl739 /* 401127dd473Swhl739 * This is used for MAC addr-mask pairs. 402127dd473Swhl739 */ 403127dd473Swhl739 typedef struct _ipfw_insn_mac { 404127dd473Swhl739 ipfw_insn o; 405127dd473Swhl739 u_char addr[12]; /* dst[6] + src[6] */ 406127dd473Swhl739 u_char mask[12]; /* dst[6] + src[6] */ 407127dd473Swhl739 } ipfw_insn_mac; 408127dd473Swhl739 409127dd473Swhl739 /* 410127dd473Swhl739 * This is used for interface match rules (recv xx, xmit xx). 411127dd473Swhl739 */ 412127dd473Swhl739 typedef struct _ipfw_insn_if { 413127dd473Swhl739 ipfw_insn o; 414127dd473Swhl739 union { 415127dd473Swhl739 struct in_addr ip; 416127dd473Swhl739 int glob; 417127dd473Swhl739 uint16_t kidx; 418127dd473Swhl739 } p; 419127dd473Swhl739 char name[IFNAMSIZ]; 420127dd473Swhl739 } ipfw_insn_if; 421127dd473Swhl739 422127dd473Swhl739 /* 423127dd473Swhl739 * This is used for storing an altq queue id number. 424127dd473Swhl739 */ 425127dd473Swhl739 typedef struct _ipfw_insn_altq { 426127dd473Swhl739 ipfw_insn o; 427127dd473Swhl739 u_int32_t qid; 428127dd473Swhl739 } ipfw_insn_altq; 429127dd473Swhl739 430127dd473Swhl739 /* 431127dd473Swhl739 * This is used for limit rules. 432127dd473Swhl739 */ 433127dd473Swhl739 typedef struct _ipfw_insn_limit { 434127dd473Swhl739 ipfw_insn o; 435127dd473Swhl739 u_int8_t _pad; 436127dd473Swhl739 u_int8_t limit_mask; /* combination of DYN_* below */ 437127dd473Swhl739 #define DYN_SRC_ADDR 0x1 438127dd473Swhl739 #define DYN_SRC_PORT 0x2 439127dd473Swhl739 #define DYN_DST_ADDR 0x4 440127dd473Swhl739 #define DYN_DST_PORT 0x8 441127dd473Swhl739 442127dd473Swhl739 u_int16_t conn_limit; 443127dd473Swhl739 } ipfw_insn_limit; 444127dd473Swhl739 445127dd473Swhl739 /* 446127dd473Swhl739 * This is used for log instructions. 447127dd473Swhl739 */ 448127dd473Swhl739 typedef struct _ipfw_insn_log { 449127dd473Swhl739 ipfw_insn o; 450127dd473Swhl739 u_int32_t max_log; /* how many do we log -- 0 = all */ 451127dd473Swhl739 u_int32_t log_left; /* how many left to log */ 452127dd473Swhl739 } ipfw_insn_log; 453127dd473Swhl739 454127dd473Swhl739 /* Legacy NAT structures, compat only */ 455127dd473Swhl739 #ifndef _KERNEL 456127dd473Swhl739 /* 457127dd473Swhl739 * Data structures required by both ipfw(8) and ipfw(4) but not part of the 458127dd473Swhl739 * management API are protected by IPFW_INTERNAL. 459127dd473Swhl739 */ 460127dd473Swhl739 #ifdef IPFW_INTERNAL 461127dd473Swhl739 /* Server pool support (LSNAT). */ 462127dd473Swhl739 struct cfg_spool { 463127dd473Swhl739 LIST_ENTRY(cfg_spool) _next; /* chain of spool instances */ 464127dd473Swhl739 struct in_addr addr; 465127dd473Swhl739 u_short port; 466127dd473Swhl739 }; 467127dd473Swhl739 #endif 468127dd473Swhl739 469127dd473Swhl739 /* Redirect modes id. */ 470127dd473Swhl739 #define REDIR_ADDR 0x01 471127dd473Swhl739 #define REDIR_PORT 0x02 472127dd473Swhl739 #define REDIR_PROTO 0x04 473127dd473Swhl739 474127dd473Swhl739 #ifdef IPFW_INTERNAL 475127dd473Swhl739 /* Nat redirect configuration. */ 476127dd473Swhl739 struct cfg_redir { 477127dd473Swhl739 LIST_ENTRY(cfg_redir) _next; /* chain of redir instances */ 478127dd473Swhl739 u_int16_t mode; /* type of redirect mode */ 479127dd473Swhl739 struct in_addr laddr; /* local ip address */ 480127dd473Swhl739 struct in_addr paddr; /* public ip address */ 481127dd473Swhl739 struct in_addr raddr; /* remote ip address */ 482127dd473Swhl739 u_short lport; /* local port */ 483127dd473Swhl739 u_short pport; /* public port */ 484127dd473Swhl739 u_short rport; /* remote port */ 485127dd473Swhl739 u_short pport_cnt; /* number of public ports */ 486127dd473Swhl739 u_short rport_cnt; /* number of remote ports */ 487127dd473Swhl739 int proto; /* protocol: tcp/udp */ 488127dd473Swhl739 struct alias_link **alink; 489127dd473Swhl739 /* num of entry in spool chain */ 490127dd473Swhl739 u_int16_t spool_cnt; 491127dd473Swhl739 /* chain of spool instances */ 492127dd473Swhl739 LIST_HEAD(spool_chain, cfg_spool) spool_chain; 493127dd473Swhl739 }; 494127dd473Swhl739 #endif 495127dd473Swhl739 496127dd473Swhl739 #ifdef IPFW_INTERNAL 497127dd473Swhl739 /* Nat configuration data struct. */ 498127dd473Swhl739 struct cfg_nat { 499127dd473Swhl739 /* chain of nat instances */ 500127dd473Swhl739 LIST_ENTRY(cfg_nat) _next; 501127dd473Swhl739 int id; /* nat id */ 502127dd473Swhl739 struct in_addr ip; /* nat ip address */ 503127dd473Swhl739 char if_name[IF_NAMESIZE]; /* interface name */ 504127dd473Swhl739 int mode; /* aliasing mode */ 505127dd473Swhl739 struct libalias *lib; /* libalias instance */ 506127dd473Swhl739 /* number of entry in spool chain */ 507127dd473Swhl739 int redir_cnt; 508127dd473Swhl739 /* chain of redir instances */ 509127dd473Swhl739 LIST_HEAD(redir_chain, cfg_redir) redir_chain; 510127dd473Swhl739 }; 511127dd473Swhl739 #endif 512127dd473Swhl739 513127dd473Swhl739 #define SOF_NAT sizeof(struct cfg_nat) 514127dd473Swhl739 #define SOF_REDIR sizeof(struct cfg_redir) 515127dd473Swhl739 #define SOF_SPOOL sizeof(struct cfg_spool) 516127dd473Swhl739 517127dd473Swhl739 #endif /* ifndef _KERNEL */ 518127dd473Swhl739 519127dd473Swhl739 struct nat44_cfg_spool { 520127dd473Swhl739 struct in_addr addr; 521127dd473Swhl739 uint16_t port; 522127dd473Swhl739 uint16_t spare; 523127dd473Swhl739 }; 524127dd473Swhl739 #define NAT44_REDIR_ADDR 0x01 525127dd473Swhl739 #define NAT44_REDIR_PORT 0x02 526127dd473Swhl739 #define NAT44_REDIR_PROTO 0x04 527127dd473Swhl739 528127dd473Swhl739 /* Nat redirect configuration. */ 529127dd473Swhl739 struct nat44_cfg_redir { 530127dd473Swhl739 struct in_addr laddr; /* local ip address */ 531127dd473Swhl739 struct in_addr paddr; /* public ip address */ 532127dd473Swhl739 struct in_addr raddr; /* remote ip address */ 533127dd473Swhl739 uint16_t lport; /* local port */ 534127dd473Swhl739 uint16_t pport; /* public port */ 535127dd473Swhl739 uint16_t rport; /* remote port */ 536127dd473Swhl739 uint16_t pport_cnt; /* number of public ports */ 537127dd473Swhl739 uint16_t rport_cnt; /* number of remote ports */ 538127dd473Swhl739 uint16_t mode; /* type of redirect mode */ 539127dd473Swhl739 uint16_t spool_cnt; /* num of entry in spool chain */ 540127dd473Swhl739 uint16_t spare; 541127dd473Swhl739 uint32_t proto; /* protocol: tcp/udp */ 542127dd473Swhl739 }; 543127dd473Swhl739 544127dd473Swhl739 /* Nat configuration data struct. */ 545127dd473Swhl739 struct nat44_cfg_nat { 546127dd473Swhl739 char name[64]; /* nat name */ 547127dd473Swhl739 char if_name[64]; /* interface name */ 548127dd473Swhl739 uint32_t size; /* structure size incl. redirs */ 549127dd473Swhl739 struct in_addr ip; /* nat IPv4 address */ 550127dd473Swhl739 uint32_t mode; /* aliasing mode */ 551127dd473Swhl739 uint32_t redir_cnt; /* number of entry in spool chain */ 552127dd473Swhl739 }; 553127dd473Swhl739 554127dd473Swhl739 /* Nat command. */ 555127dd473Swhl739 typedef struct _ipfw_insn_nat { 556127dd473Swhl739 ipfw_insn o; 557127dd473Swhl739 struct cfg_nat *nat; 558127dd473Swhl739 } ipfw_insn_nat; 559127dd473Swhl739 560127dd473Swhl739 /* Apply ipv6 mask on ipv6 addr */ 561*d4a07e70Sfengbojiang #define APPLY_MASK(addr,mask) do { \ 562127dd473Swhl739 (addr)->__u6_addr.__u6_addr32[0] &= (mask)->__u6_addr.__u6_addr32[0]; \ 563127dd473Swhl739 (addr)->__u6_addr.__u6_addr32[1] &= (mask)->__u6_addr.__u6_addr32[1]; \ 564127dd473Swhl739 (addr)->__u6_addr.__u6_addr32[2] &= (mask)->__u6_addr.__u6_addr32[2]; \ 565*d4a07e70Sfengbojiang (addr)->__u6_addr.__u6_addr32[3] &= (mask)->__u6_addr.__u6_addr32[3]; \ 566*d4a07e70Sfengbojiang } while (0) 567127dd473Swhl739 568127dd473Swhl739 /* Structure for ipv6 */ 569127dd473Swhl739 typedef struct _ipfw_insn_ip6 { 570127dd473Swhl739 ipfw_insn o; 571127dd473Swhl739 struct in6_addr addr6; 572127dd473Swhl739 struct in6_addr mask6; 573127dd473Swhl739 } ipfw_insn_ip6; 574127dd473Swhl739 575127dd473Swhl739 /* Used to support icmp6 types */ 576127dd473Swhl739 typedef struct _ipfw_insn_icmp6 { 577127dd473Swhl739 ipfw_insn o; 578127dd473Swhl739 uint32_t d[7]; /* XXX This number si related to the netinet/icmp6.h 579127dd473Swhl739 * define ICMP6_MAXTYPE 580127dd473Swhl739 * as follows: n = ICMP6_MAXTYPE/32 + 1 581127dd473Swhl739 * Actually is 203 582127dd473Swhl739 */ 583127dd473Swhl739 } ipfw_insn_icmp6; 584127dd473Swhl739 585127dd473Swhl739 /* 586127dd473Swhl739 * Here we have the structure representing an ipfw rule. 587127dd473Swhl739 * 588127dd473Swhl739 * Layout: 589127dd473Swhl739 * struct ip_fw_rule 590127dd473Swhl739 * [ counter block, size = rule->cntr_len ] 591127dd473Swhl739 * [ one or more instructions, size = rule->cmd_len * 4 ] 592127dd473Swhl739 * 593127dd473Swhl739 * It starts with a general area (with link fields). 594127dd473Swhl739 * Counter block may be next (if rule->cntr_len > 0), 595127dd473Swhl739 * followed by an array of one or more instructions, which the code 596127dd473Swhl739 * accesses as an array of 32-bit values. rule->cmd_len represents 597127dd473Swhl739 * the total instructions legth in u32 worrd, while act_ofs represents 598127dd473Swhl739 * rule action offset in u32 words. 599127dd473Swhl739 * 600127dd473Swhl739 * When assembling instruction, remember the following: 601127dd473Swhl739 * 602127dd473Swhl739 * + if a rule has a "keep-state" (or "limit") option, then the 603127dd473Swhl739 * first instruction (at r->cmd) MUST BE an O_PROBE_STATE 604127dd473Swhl739 * + if a rule has a "log" option, then the first action 605127dd473Swhl739 * (at ACTION_PTR(r)) MUST be O_LOG 606127dd473Swhl739 * + if a rule has an "altq" option, it comes after "log" 607127dd473Swhl739 * + if a rule has an O_TAG option, it comes after "log" and "altq" 608127dd473Swhl739 * 609127dd473Swhl739 * 610127dd473Swhl739 * All structures (excluding instructions) are u64-aligned. 611127dd473Swhl739 * Please keep this. 612127dd473Swhl739 */ 613127dd473Swhl739 614127dd473Swhl739 struct ip_fw_rule { 615127dd473Swhl739 uint16_t act_ofs; /* offset of action in 32-bit units */ 616127dd473Swhl739 uint16_t cmd_len; /* # of 32-bit words in cmd */ 617127dd473Swhl739 uint16_t spare; 618127dd473Swhl739 uint8_t set; /* rule set (0..31) */ 619127dd473Swhl739 uint8_t flags; /* rule flags */ 620127dd473Swhl739 uint32_t rulenum; /* rule number */ 621127dd473Swhl739 uint32_t id; /* rule id */ 622127dd473Swhl739 623127dd473Swhl739 ipfw_insn cmd[1]; /* storage for commands */ 624127dd473Swhl739 }; 625127dd473Swhl739 #define IPFW_RULE_NOOPT 0x01 /* Has no options in body */ 626*d4a07e70Sfengbojiang #define IPFW_RULE_JUSTOPTS 0x02 /* new format of rule body */ 627127dd473Swhl739 628127dd473Swhl739 /* Unaligned version */ 629127dd473Swhl739 630127dd473Swhl739 /* Base ipfw rule counter block. */ 631127dd473Swhl739 struct ip_fw_bcounter { 632127dd473Swhl739 uint16_t size; /* Size of counter block, bytes */ 633127dd473Swhl739 uint8_t flags; /* flags for given block */ 634127dd473Swhl739 uint8_t spare; 635127dd473Swhl739 uint32_t timestamp; /* tv_sec of last match */ 636127dd473Swhl739 uint64_t pcnt; /* Packet counter */ 637127dd473Swhl739 uint64_t bcnt; /* Byte counter */ 638127dd473Swhl739 }; 639127dd473Swhl739 640127dd473Swhl739 #ifndef _KERNEL 641127dd473Swhl739 /* 642127dd473Swhl739 * Legacy rule format 643127dd473Swhl739 */ 644127dd473Swhl739 struct ip_fw { 645127dd473Swhl739 struct ip_fw *x_next; /* linked list of rules */ 646127dd473Swhl739 struct ip_fw *next_rule; /* ptr to next [skipto] rule */ 647127dd473Swhl739 /* 'next_rule' is used to pass up 'set_disable' status */ 648127dd473Swhl739 649127dd473Swhl739 uint16_t act_ofs; /* offset of action in 32-bit units */ 650127dd473Swhl739 uint16_t cmd_len; /* # of 32-bit words in cmd */ 651127dd473Swhl739 uint16_t rulenum; /* rule number */ 652127dd473Swhl739 uint8_t set; /* rule set (0..31) */ 653127dd473Swhl739 uint8_t _pad; /* padding */ 654127dd473Swhl739 uint32_t id; /* rule id */ 655127dd473Swhl739 656127dd473Swhl739 /* These fields are present in all rules. */ 657127dd473Swhl739 uint64_t pcnt; /* Packet counter */ 658127dd473Swhl739 uint64_t bcnt; /* Byte counter */ 659127dd473Swhl739 uint32_t timestamp; /* tv_sec of last match */ 660127dd473Swhl739 661127dd473Swhl739 ipfw_insn cmd[1]; /* storage for commands */ 662127dd473Swhl739 }; 663127dd473Swhl739 #endif 664127dd473Swhl739 665127dd473Swhl739 #define ACTION_PTR(rule) \ 666127dd473Swhl739 (ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) ) 667127dd473Swhl739 668127dd473Swhl739 #define RULESIZE(rule) (sizeof(*(rule)) + (rule)->cmd_len * 4 - 4) 669127dd473Swhl739 670127dd473Swhl739 #if 1 // should be moved to in.h 671127dd473Swhl739 /* 672127dd473Swhl739 * This structure is used as a flow mask and a flow id for various 673127dd473Swhl739 * parts of the code. 674127dd473Swhl739 * addr_type is used in userland and kernel to mark the address type. 675127dd473Swhl739 * fib is used in the kernel to record the fib in use. 676127dd473Swhl739 * _flags is used in the kernel to store tcp flags for dynamic rules. 677127dd473Swhl739 */ 678127dd473Swhl739 struct ipfw_flow_id { 679127dd473Swhl739 uint32_t dst_ip; 680127dd473Swhl739 uint32_t src_ip; 681127dd473Swhl739 uint16_t dst_port; 682127dd473Swhl739 uint16_t src_port; 683*d4a07e70Sfengbojiang uint8_t fib; /* XXX: must be uint16_t */ 684127dd473Swhl739 uint8_t proto; 685127dd473Swhl739 uint8_t _flags; /* protocol-specific flags */ 686127dd473Swhl739 uint8_t addr_type; /* 4=ip4, 6=ip6, 1=ether ? */ 687127dd473Swhl739 struct in6_addr dst_ip6; 688127dd473Swhl739 struct in6_addr src_ip6; 689127dd473Swhl739 uint32_t flow_id6; 690127dd473Swhl739 uint32_t extra; /* queue/pipe or frag_id */ 691127dd473Swhl739 }; 692127dd473Swhl739 #endif 693127dd473Swhl739 694*d4a07e70Sfengbojiang #define IS_IP4_FLOW_ID(id) ((id)->addr_type == 4) 695127dd473Swhl739 #define IS_IP6_FLOW_ID(id) ((id)->addr_type == 6) 696127dd473Swhl739 697127dd473Swhl739 /* 698127dd473Swhl739 * Dynamic ipfw rule. 699127dd473Swhl739 */ 700127dd473Swhl739 typedef struct _ipfw_dyn_rule ipfw_dyn_rule; 701127dd473Swhl739 702127dd473Swhl739 struct _ipfw_dyn_rule { 703127dd473Swhl739 ipfw_dyn_rule *next; /* linked list of rules. */ 704127dd473Swhl739 struct ip_fw *rule; /* pointer to rule */ 705127dd473Swhl739 /* 'rule' is used to pass up the rule number (from the parent) */ 706127dd473Swhl739 707127dd473Swhl739 ipfw_dyn_rule *parent; /* pointer to parent rule */ 708127dd473Swhl739 u_int64_t pcnt; /* packet match counter */ 709127dd473Swhl739 u_int64_t bcnt; /* byte match counter */ 710127dd473Swhl739 struct ipfw_flow_id id; /* (masked) flow id */ 711127dd473Swhl739 u_int32_t expire; /* expire time */ 712127dd473Swhl739 u_int32_t bucket; /* which bucket in hash table */ 713127dd473Swhl739 u_int32_t state; /* state of this rule (typically a 714127dd473Swhl739 * combination of TCP flags) 715127dd473Swhl739 */ 716*d4a07e70Sfengbojiang #define IPFW_DYN_ORPHANED 0x40000 /* state's parent rule was deleted */ 717127dd473Swhl739 u_int32_t ack_fwd; /* most recent ACKs in forward */ 718127dd473Swhl739 u_int32_t ack_rev; /* and reverse directions (used */ 719127dd473Swhl739 /* to generate keepalives) */ 720127dd473Swhl739 u_int16_t dyn_type; /* rule type */ 721127dd473Swhl739 u_int16_t count; /* refcount */ 722*d4a07e70Sfengbojiang u_int16_t kidx; /* index of named object */ 723*d4a07e70Sfengbojiang } __attribute__ ((packed)) __attribute__((__aligned__(8))); 724127dd473Swhl739 725127dd473Swhl739 /* 726127dd473Swhl739 * Definitions for IP option names. 727127dd473Swhl739 */ 728127dd473Swhl739 #define IP_FW_IPOPT_LSRR 0x01 729127dd473Swhl739 #define IP_FW_IPOPT_SSRR 0x02 730127dd473Swhl739 #define IP_FW_IPOPT_RR 0x04 731127dd473Swhl739 #define IP_FW_IPOPT_TS 0x08 732127dd473Swhl739 733127dd473Swhl739 /* 734127dd473Swhl739 * Definitions for TCP option names. 735127dd473Swhl739 */ 736127dd473Swhl739 #define IP_FW_TCPOPT_MSS 0x01 737127dd473Swhl739 #define IP_FW_TCPOPT_WINDOW 0x02 738127dd473Swhl739 #define IP_FW_TCPOPT_SACK 0x04 739127dd473Swhl739 #define IP_FW_TCPOPT_TS 0x08 740127dd473Swhl739 #define IP_FW_TCPOPT_CC 0x10 741127dd473Swhl739 742127dd473Swhl739 #define ICMP_REJECT_RST 0x100 /* fake ICMP code (send a TCP RST) */ 743127dd473Swhl739 #define ICMP6_UNREACH_RST 0x100 /* fake ICMPv6 code (send a TCP RST) */ 744*d4a07e70Sfengbojiang #define ICMP_REJECT_ABORT 0x101 /* fake ICMP code (send an SCTP ABORT) */ 745*d4a07e70Sfengbojiang #define ICMP6_UNREACH_ABORT 0x101 /* fake ICMPv6 code (send an SCTP ABORT) */ 746127dd473Swhl739 747127dd473Swhl739 /* 748127dd473Swhl739 * These are used for lookup tables. 749127dd473Swhl739 */ 750127dd473Swhl739 751127dd473Swhl739 #define IPFW_TABLE_ADDR 1 /* Table for holding IPv4/IPv6 prefixes */ 752127dd473Swhl739 #define IPFW_TABLE_INTERFACE 2 /* Table for holding interface names */ 753127dd473Swhl739 #define IPFW_TABLE_NUMBER 3 /* Table for holding ports/uid/gid/etc */ 754127dd473Swhl739 #define IPFW_TABLE_FLOW 4 /* Table for holding flow data */ 755127dd473Swhl739 #define IPFW_TABLE_MAXTYPE 4 /* Maximum valid number */ 756127dd473Swhl739 757127dd473Swhl739 #define IPFW_TABLE_CIDR IPFW_TABLE_ADDR /* compat */ 758127dd473Swhl739 759127dd473Swhl739 /* Value types */ 760127dd473Swhl739 #define IPFW_VTYPE_LEGACY 0xFFFFFFFF /* All data is filled in */ 761127dd473Swhl739 #define IPFW_VTYPE_SKIPTO 0x00000001 /* skipto/call/callreturn */ 762127dd473Swhl739 #define IPFW_VTYPE_PIPE 0x00000002 /* pipe/queue */ 763127dd473Swhl739 #define IPFW_VTYPE_FIB 0x00000004 /* setfib */ 764127dd473Swhl739 #define IPFW_VTYPE_NAT 0x00000008 /* nat */ 765127dd473Swhl739 #define IPFW_VTYPE_DSCP 0x00000010 /* dscp */ 766127dd473Swhl739 #define IPFW_VTYPE_TAG 0x00000020 /* tag/untag */ 767127dd473Swhl739 #define IPFW_VTYPE_DIVERT 0x00000040 /* divert/tee */ 768127dd473Swhl739 #define IPFW_VTYPE_NETGRAPH 0x00000080 /* netgraph/ngtee */ 769127dd473Swhl739 #define IPFW_VTYPE_LIMIT 0x00000100 /* limit */ 770127dd473Swhl739 #define IPFW_VTYPE_NH4 0x00000200 /* IPv4 nexthop */ 771127dd473Swhl739 #define IPFW_VTYPE_NH6 0x00000400 /* IPv6 nexthop */ 772127dd473Swhl739 773127dd473Swhl739 typedef struct _ipfw_table_entry { 774127dd473Swhl739 in_addr_t addr; /* network address */ 775127dd473Swhl739 u_int32_t value; /* value */ 776127dd473Swhl739 u_int16_t tbl; /* table number */ 777127dd473Swhl739 u_int8_t masklen; /* mask length */ 778127dd473Swhl739 } ipfw_table_entry; 779127dd473Swhl739 780127dd473Swhl739 typedef struct _ipfw_table_xentry { 781127dd473Swhl739 uint16_t len; /* Total entry length */ 782127dd473Swhl739 uint8_t type; /* entry type */ 783127dd473Swhl739 uint8_t masklen; /* mask length */ 784127dd473Swhl739 uint16_t tbl; /* table number */ 785127dd473Swhl739 uint16_t flags; /* record flags */ 786127dd473Swhl739 uint32_t value; /* value */ 787127dd473Swhl739 union { 788127dd473Swhl739 /* Longest field needs to be aligned by 4-byte boundary */ 789127dd473Swhl739 struct in6_addr addr6; /* IPv6 address */ 790127dd473Swhl739 char iface[IF_NAMESIZE]; /* interface name */ 791127dd473Swhl739 } k; 792127dd473Swhl739 } ipfw_table_xentry; 793127dd473Swhl739 #define IPFW_TCF_INET 0x01 /* CIDR flags: IPv4 record */ 794127dd473Swhl739 795127dd473Swhl739 typedef struct _ipfw_table { 796127dd473Swhl739 u_int32_t size; /* size of entries in bytes */ 797127dd473Swhl739 u_int32_t cnt; /* # of entries */ 798127dd473Swhl739 u_int16_t tbl; /* table number */ 799127dd473Swhl739 ipfw_table_entry ent[0]; /* entries */ 800127dd473Swhl739 } ipfw_table; 801127dd473Swhl739 802127dd473Swhl739 typedef struct _ipfw_xtable { 803127dd473Swhl739 ip_fw3_opheader opheader; /* IP_FW3 opcode */ 804127dd473Swhl739 uint32_t size; /* size of entries in bytes */ 805127dd473Swhl739 uint32_t cnt; /* # of entries */ 806127dd473Swhl739 uint16_t tbl; /* table number */ 807127dd473Swhl739 uint8_t type; /* table type */ 808127dd473Swhl739 ipfw_table_xentry xent[0]; /* entries */ 809127dd473Swhl739 } ipfw_xtable; 810127dd473Swhl739 811127dd473Swhl739 typedef struct _ipfw_obj_tlv { 812127dd473Swhl739 uint16_t type; /* TLV type */ 813127dd473Swhl739 uint16_t flags; /* TLV-specific flags */ 814127dd473Swhl739 uint32_t length; /* Total length, aligned to u64 */ 815127dd473Swhl739 } ipfw_obj_tlv; 816127dd473Swhl739 #define IPFW_TLV_TBL_NAME 1 817127dd473Swhl739 #define IPFW_TLV_TBLNAME_LIST 2 818127dd473Swhl739 #define IPFW_TLV_RULE_LIST 3 819127dd473Swhl739 #define IPFW_TLV_DYNSTATE_LIST 4 820127dd473Swhl739 #define IPFW_TLV_TBL_ENT 5 821127dd473Swhl739 #define IPFW_TLV_DYN_ENT 6 822127dd473Swhl739 #define IPFW_TLV_RULE_ENT 7 823127dd473Swhl739 #define IPFW_TLV_TBLENT_LIST 8 824127dd473Swhl739 #define IPFW_TLV_RANGE 9 825127dd473Swhl739 #define IPFW_TLV_EACTION 10 826*d4a07e70Sfengbojiang #define IPFW_TLV_COUNTERS 11 827*d4a07e70Sfengbojiang #define IPFW_TLV_OBJDATA 12 828*d4a07e70Sfengbojiang #define IPFW_TLV_STATE_NAME 14 829127dd473Swhl739 830127dd473Swhl739 #define IPFW_TLV_EACTION_BASE 1000 831127dd473Swhl739 #define IPFW_TLV_EACTION_NAME(arg) (IPFW_TLV_EACTION_BASE + (arg)) 832127dd473Swhl739 833*d4a07e70Sfengbojiang typedef struct _ipfw_obj_data { 834*d4a07e70Sfengbojiang ipfw_obj_tlv head; 835*d4a07e70Sfengbojiang void *data[0]; 836*d4a07e70Sfengbojiang } ipfw_obj_data; 837*d4a07e70Sfengbojiang 838127dd473Swhl739 /* Object name TLV */ 839127dd473Swhl739 typedef struct _ipfw_obj_ntlv { 840127dd473Swhl739 ipfw_obj_tlv head; /* TLV header */ 841127dd473Swhl739 uint16_t idx; /* Name index */ 842127dd473Swhl739 uint8_t set; /* set, if applicable */ 843127dd473Swhl739 uint8_t type; /* object type, if applicable */ 844127dd473Swhl739 uint32_t spare; /* unused */ 845127dd473Swhl739 char name[64]; /* Null-terminated name */ 846127dd473Swhl739 } ipfw_obj_ntlv; 847127dd473Swhl739 848127dd473Swhl739 /* IPv4/IPv6 L4 flow description */ 849127dd473Swhl739 struct tflow_entry { 850127dd473Swhl739 uint8_t af; 851127dd473Swhl739 uint8_t proto; 852127dd473Swhl739 uint16_t spare; 853127dd473Swhl739 uint16_t sport; 854127dd473Swhl739 uint16_t dport; 855127dd473Swhl739 union { 856127dd473Swhl739 struct { 857127dd473Swhl739 struct in_addr sip; 858127dd473Swhl739 struct in_addr dip; 859127dd473Swhl739 } a4; 860127dd473Swhl739 struct { 861127dd473Swhl739 struct in6_addr sip6; 862127dd473Swhl739 struct in6_addr dip6; 863127dd473Swhl739 } a6; 864127dd473Swhl739 } a; 865127dd473Swhl739 }; 866127dd473Swhl739 867127dd473Swhl739 typedef struct _ipfw_table_value { 868127dd473Swhl739 uint32_t tag; /* O_TAG/O_TAGGED */ 869127dd473Swhl739 uint32_t pipe; /* O_PIPE/O_QUEUE */ 870127dd473Swhl739 uint16_t divert; /* O_DIVERT/O_TEE */ 871127dd473Swhl739 uint16_t skipto; /* skipto, CALLRET */ 872127dd473Swhl739 uint32_t netgraph; /* O_NETGRAPH/O_NGTEE */ 873127dd473Swhl739 uint32_t fib; /* O_SETFIB */ 874127dd473Swhl739 uint32_t nat; /* O_NAT */ 875127dd473Swhl739 uint32_t nh4; 876127dd473Swhl739 uint8_t dscp; 877127dd473Swhl739 uint8_t spare0; 878127dd473Swhl739 uint16_t spare1; 879127dd473Swhl739 struct in6_addr nh6; 880127dd473Swhl739 uint32_t limit; /* O_LIMIT */ 881127dd473Swhl739 uint32_t zoneid; /* scope zone id for nh6 */ 882127dd473Swhl739 uint64_t reserved; 883127dd473Swhl739 } ipfw_table_value; 884127dd473Swhl739 885127dd473Swhl739 /* Table entry TLV */ 886127dd473Swhl739 typedef struct _ipfw_obj_tentry { 887127dd473Swhl739 ipfw_obj_tlv head; /* TLV header */ 888127dd473Swhl739 uint8_t subtype; /* subtype (IPv4,IPv6) */ 889127dd473Swhl739 uint8_t masklen; /* mask length */ 890127dd473Swhl739 uint8_t result; /* request result */ 891127dd473Swhl739 uint8_t spare0; 892127dd473Swhl739 uint16_t idx; /* Table name index */ 893127dd473Swhl739 uint16_t spare1; 894127dd473Swhl739 union { 895127dd473Swhl739 /* Longest field needs to be aligned by 8-byte boundary */ 896127dd473Swhl739 struct in_addr addr; /* IPv4 address */ 897127dd473Swhl739 uint32_t key; /* uid/gid/port */ 898127dd473Swhl739 struct in6_addr addr6; /* IPv6 address */ 899127dd473Swhl739 char iface[IF_NAMESIZE]; /* interface name */ 900127dd473Swhl739 struct tflow_entry flow; 901127dd473Swhl739 } k; 902127dd473Swhl739 union { 903127dd473Swhl739 ipfw_table_value value; /* value data */ 904127dd473Swhl739 uint32_t kidx; /* value kernel index */ 905127dd473Swhl739 } v; 906127dd473Swhl739 } ipfw_obj_tentry; 907127dd473Swhl739 #define IPFW_TF_UPDATE 0x01 /* Update record if exists */ 908127dd473Swhl739 /* Container TLV */ 909127dd473Swhl739 #define IPFW_CTF_ATOMIC 0x01 /* Perform atomic operation */ 910127dd473Swhl739 /* Operation results */ 911127dd473Swhl739 #define IPFW_TR_IGNORED 0 /* Entry was ignored (rollback) */ 912127dd473Swhl739 #define IPFW_TR_ADDED 1 /* Entry was successfully added */ 913127dd473Swhl739 #define IPFW_TR_UPDATED 2 /* Entry was successfully updated*/ 914127dd473Swhl739 #define IPFW_TR_DELETED 3 /* Entry was successfully deleted*/ 915127dd473Swhl739 #define IPFW_TR_LIMIT 4 /* Entry was ignored (limit) */ 916127dd473Swhl739 #define IPFW_TR_NOTFOUND 5 /* Entry was not found */ 917127dd473Swhl739 #define IPFW_TR_EXISTS 6 /* Entry already exists */ 918127dd473Swhl739 #define IPFW_TR_ERROR 7 /* Request has failed (unknown) */ 919127dd473Swhl739 920127dd473Swhl739 typedef struct _ipfw_obj_dyntlv { 921127dd473Swhl739 ipfw_obj_tlv head; 922127dd473Swhl739 ipfw_dyn_rule state; 923127dd473Swhl739 } ipfw_obj_dyntlv; 924127dd473Swhl739 #define IPFW_DF_LAST 0x01 /* Last state in chain */ 925127dd473Swhl739 926127dd473Swhl739 /* Containter TLVs */ 927127dd473Swhl739 typedef struct _ipfw_obj_ctlv { 928127dd473Swhl739 ipfw_obj_tlv head; /* TLV header */ 929127dd473Swhl739 uint32_t count; /* Number of sub-TLVs */ 930127dd473Swhl739 uint16_t objsize; /* Single object size */ 931127dd473Swhl739 uint8_t version; /* TLV version */ 932127dd473Swhl739 uint8_t flags; /* TLV-specific flags */ 933127dd473Swhl739 } ipfw_obj_ctlv; 934127dd473Swhl739 935127dd473Swhl739 /* Range TLV */ 936127dd473Swhl739 typedef struct _ipfw_range_tlv { 937127dd473Swhl739 ipfw_obj_tlv head; /* TLV header */ 938127dd473Swhl739 uint32_t flags; /* Range flags */ 939127dd473Swhl739 uint16_t start_rule; /* Range start */ 940127dd473Swhl739 uint16_t end_rule; /* Range end */ 941127dd473Swhl739 uint32_t set; /* Range set to match */ 942127dd473Swhl739 uint32_t new_set; /* New set to move/swap to */ 943127dd473Swhl739 } ipfw_range_tlv; 944127dd473Swhl739 #define IPFW_RCFLAG_RANGE 0x01 /* rule range is set */ 945127dd473Swhl739 #define IPFW_RCFLAG_ALL 0x02 /* match ALL rules */ 946127dd473Swhl739 #define IPFW_RCFLAG_SET 0x04 /* match rules in given set */ 947*d4a07e70Sfengbojiang #define IPFW_RCFLAG_DYNAMIC 0x08 /* match only dynamic states */ 948127dd473Swhl739 /* User-settable flags */ 949127dd473Swhl739 #define IPFW_RCFLAG_USER (IPFW_RCFLAG_RANGE | IPFW_RCFLAG_ALL | \ 950*d4a07e70Sfengbojiang IPFW_RCFLAG_SET | IPFW_RCFLAG_DYNAMIC) 951127dd473Swhl739 /* Internally used flags */ 952127dd473Swhl739 #define IPFW_RCFLAG_DEFAULT 0x0100 /* Do not skip defaul rule */ 953127dd473Swhl739 954127dd473Swhl739 typedef struct _ipfw_ta_tinfo { 955127dd473Swhl739 uint32_t flags; /* Format flags */ 956127dd473Swhl739 uint32_t spare; 957127dd473Swhl739 uint8_t taclass4; /* algorithm class */ 958127dd473Swhl739 uint8_t spare4; 959127dd473Swhl739 uint16_t itemsize4; /* item size in runtime */ 960127dd473Swhl739 uint32_t size4; /* runtime structure size */ 961127dd473Swhl739 uint32_t count4; /* number of items in runtime */ 962127dd473Swhl739 uint8_t taclass6; /* algorithm class */ 963127dd473Swhl739 uint8_t spare6; 964127dd473Swhl739 uint16_t itemsize6; /* item size in runtime */ 965127dd473Swhl739 uint32_t size6; /* runtime structure size */ 966127dd473Swhl739 uint32_t count6; /* number of items in runtime */ 967127dd473Swhl739 } ipfw_ta_tinfo; 968127dd473Swhl739 #define IPFW_TACLASS_HASH 1 /* algo is based on hash */ 969127dd473Swhl739 #define IPFW_TACLASS_ARRAY 2 /* algo is based on array */ 970127dd473Swhl739 #define IPFW_TACLASS_RADIX 3 /* algo is based on radix tree */ 971127dd473Swhl739 972127dd473Swhl739 #define IPFW_TATFLAGS_DATA 0x0001 /* Has data filled in */ 973127dd473Swhl739 #define IPFW_TATFLAGS_AFDATA 0x0002 /* Separate data per AF */ 974127dd473Swhl739 #define IPFW_TATFLAGS_AFITEM 0x0004 /* diff. items per AF */ 975127dd473Swhl739 976127dd473Swhl739 typedef struct _ipfw_xtable_info { 977127dd473Swhl739 uint8_t type; /* table type (addr,iface,..) */ 978127dd473Swhl739 uint8_t tflags; /* type flags */ 979127dd473Swhl739 uint16_t mflags; /* modification flags */ 980127dd473Swhl739 uint16_t flags; /* generic table flags */ 981127dd473Swhl739 uint16_t spare[3]; 982127dd473Swhl739 uint32_t vmask; /* bitmask with value types */ 983127dd473Swhl739 uint32_t set; /* set table is in */ 984127dd473Swhl739 uint32_t kidx; /* kernel index */ 985127dd473Swhl739 uint32_t refcnt; /* number of references */ 986127dd473Swhl739 uint32_t count; /* Number of records */ 987127dd473Swhl739 uint32_t size; /* Total size of records(export)*/ 988127dd473Swhl739 uint32_t limit; /* Max number of records */ 989127dd473Swhl739 char tablename[64]; /* table name */ 990127dd473Swhl739 char algoname[64]; /* algorithm name */ 991127dd473Swhl739 ipfw_ta_tinfo ta_info; /* additional algo stats */ 992127dd473Swhl739 } ipfw_xtable_info; 993127dd473Swhl739 /* Generic table flags */ 994127dd473Swhl739 #define IPFW_TGFLAGS_LOCKED 0x01 /* Tables is locked from changes*/ 995127dd473Swhl739 /* Table type-specific flags */ 996127dd473Swhl739 #define IPFW_TFFLAG_SRCIP 0x01 997127dd473Swhl739 #define IPFW_TFFLAG_DSTIP 0x02 998127dd473Swhl739 #define IPFW_TFFLAG_SRCPORT 0x04 999127dd473Swhl739 #define IPFW_TFFLAG_DSTPORT 0x08 1000127dd473Swhl739 #define IPFW_TFFLAG_PROTO 0x10 1001127dd473Swhl739 /* Table modification flags */ 1002127dd473Swhl739 #define IPFW_TMFLAGS_LIMIT 0x0002 /* Change limit value */ 1003127dd473Swhl739 #define IPFW_TMFLAGS_LOCK 0x0004 /* Change table lock state */ 1004127dd473Swhl739 1005127dd473Swhl739 typedef struct _ipfw_iface_info { 1006127dd473Swhl739 char ifname[64]; /* interface name */ 1007127dd473Swhl739 uint32_t ifindex; /* interface index */ 1008127dd473Swhl739 uint32_t flags; /* flags */ 1009127dd473Swhl739 uint32_t refcnt; /* number of references */ 1010127dd473Swhl739 uint32_t gencnt; /* number of changes */ 1011127dd473Swhl739 uint64_t spare; 1012127dd473Swhl739 } ipfw_iface_info; 1013127dd473Swhl739 #define IPFW_IFFLAG_RESOLVED 0x01 /* Interface exists */ 1014127dd473Swhl739 1015127dd473Swhl739 typedef struct _ipfw_ta_info { 1016127dd473Swhl739 char algoname[64]; /* algorithm name */ 1017127dd473Swhl739 uint32_t type; /* lookup type */ 1018127dd473Swhl739 uint32_t flags; 1019127dd473Swhl739 uint32_t refcnt; 1020127dd473Swhl739 uint32_t spare0; 1021127dd473Swhl739 uint64_t spare1; 1022127dd473Swhl739 } ipfw_ta_info; 1023127dd473Swhl739 1024127dd473Swhl739 typedef struct _ipfw_obj_header { 1025127dd473Swhl739 ip_fw3_opheader opheader; /* IP_FW3 opcode */ 1026127dd473Swhl739 uint32_t spare; 1027127dd473Swhl739 uint16_t idx; /* object name index */ 1028127dd473Swhl739 uint8_t objtype; /* object type */ 1029127dd473Swhl739 uint8_t objsubtype; /* object subtype */ 1030127dd473Swhl739 ipfw_obj_ntlv ntlv; /* object name tlv */ 1031127dd473Swhl739 } ipfw_obj_header; 1032127dd473Swhl739 1033127dd473Swhl739 typedef struct _ipfw_obj_lheader { 1034127dd473Swhl739 ip_fw3_opheader opheader; /* IP_FW3 opcode */ 1035127dd473Swhl739 uint32_t set_mask; /* disabled set mask */ 1036127dd473Swhl739 uint32_t count; /* Total objects count */ 1037127dd473Swhl739 uint32_t size; /* Total size (incl. header) */ 1038127dd473Swhl739 uint32_t objsize; /* Size of one object */ 1039127dd473Swhl739 } ipfw_obj_lheader; 1040127dd473Swhl739 1041127dd473Swhl739 #define IPFW_CFG_GET_STATIC 0x01 1042127dd473Swhl739 #define IPFW_CFG_GET_STATES 0x02 1043127dd473Swhl739 #define IPFW_CFG_GET_COUNTERS 0x04 1044127dd473Swhl739 typedef struct _ipfw_cfg_lheader { 1045127dd473Swhl739 ip_fw3_opheader opheader; /* IP_FW3 opcode */ 1046127dd473Swhl739 uint32_t set_mask; /* enabled set mask */ 1047127dd473Swhl739 uint32_t spare; 1048127dd473Swhl739 uint32_t flags; /* Request flags */ 1049127dd473Swhl739 uint32_t size; /* neded buffer size */ 1050127dd473Swhl739 uint32_t start_rule; 1051127dd473Swhl739 uint32_t end_rule; 1052127dd473Swhl739 } ipfw_cfg_lheader; 1053127dd473Swhl739 1054127dd473Swhl739 typedef struct _ipfw_range_header { 1055127dd473Swhl739 ip_fw3_opheader opheader; /* IP_FW3 opcode */ 1056127dd473Swhl739 ipfw_range_tlv range; 1057127dd473Swhl739 } ipfw_range_header; 1058127dd473Swhl739 1059127dd473Swhl739 typedef struct _ipfw_sopt_info { 1060127dd473Swhl739 uint16_t opcode; 1061127dd473Swhl739 uint8_t version; 1062127dd473Swhl739 uint8_t dir; 1063127dd473Swhl739 uint8_t spare; 1064127dd473Swhl739 uint64_t refcnt; 1065127dd473Swhl739 } ipfw_sopt_info; 1066127dd473Swhl739 1067127dd473Swhl739 #endif /* _IPFW2_H */ 1068