1 /*- 2 * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 * $FreeBSD$ 26 */ 27 28 #ifndef _IPFW2_H 29 #define _IPFW2_H 30 31 /* 32 * The default rule number. By the design of ip_fw, the default rule 33 * is the last one, so its number can also serve as the highest number 34 * allowed for a rule. The ip_fw code relies on both meanings of this 35 * constant. 36 */ 37 #define IPFW_DEFAULT_RULE 65535 38 39 #define RESVD_SET 31 /*set for default and persistent rules*/ 40 #define IPFW_MAX_SETS 32 /* Number of sets supported by ipfw*/ 41 42 /* 43 * Compat values for old clients 44 */ 45 #ifndef _KERNEL 46 #define IPFW_TABLES_MAX 65535 47 #define IPFW_TABLES_DEFAULT 128 48 #endif 49 50 /* 51 * Most commands (queue, pipe, tag, untag, limit...) can have a 16-bit 52 * argument between 1 and 65534. The value 0 (IP_FW_TARG) is used 53 * to represent 'tablearg' value, e.g. indicate the use of a 'tablearg' 54 * result of the most recent table() lookup. 55 * Note that 16bit is only a historical limit, resulting from 56 * the use of a 16-bit fields for that value. In reality, we can have 57 * 2^32 pipes, queues, tag values and so on. 58 */ 59 #define IPFW_ARG_MIN 1 60 #define IPFW_ARG_MAX 65534 61 #define IP_FW_TABLEARG 65535 /* Compat value for old clients */ 62 #define IP_FW_TARG 0 /* Current tablearg value */ 63 #define IP_FW_NAT44_GLOBAL 65535 /* arg1 value for "nat global" */ 64 65 /* 66 * Number of entries in the call stack of the call/return commands. 67 * Call stack currently is an uint16_t array with rule numbers. 68 */ 69 #define IPFW_CALLSTACK_SIZE 16 70 71 /* IP_FW3 header/opcodes */ 72 typedef struct _ip_fw3_opheader { 73 uint16_t opcode; /* Operation opcode */ 74 uint16_t version; /* Opcode version */ 75 uint16_t reserved[2]; /* Align to 64-bit boundary */ 76 } ip_fw3_opheader; 77 78 /* IP_FW3 opcodes */ 79 #define IP_FW_TABLE_XADD 86 /* add entry */ 80 #define IP_FW_TABLE_XDEL 87 /* delete entry */ 81 #define IP_FW_TABLE_XGETSIZE 88 /* get table size (deprecated) */ 82 #define IP_FW_TABLE_XLIST 89 /* list table contents */ 83 #define IP_FW_TABLE_XDESTROY 90 /* destroy table */ 84 #define IP_FW_TABLES_XLIST 92 /* list all tables */ 85 #define IP_FW_TABLE_XINFO 93 /* request info for one table */ 86 #define IP_FW_TABLE_XFLUSH 94 /* flush table data */ 87 #define IP_FW_TABLE_XCREATE 95 /* create new table */ 88 #define IP_FW_TABLE_XMODIFY 96 /* modify existing table */ 89 #define IP_FW_XGET 97 /* Retrieve configuration */ 90 #define IP_FW_XADD 98 /* add rule */ 91 #define IP_FW_XDEL 99 /* del rule */ 92 #define IP_FW_XMOVE 100 /* move rules to different set */ 93 #define IP_FW_XZERO 101 /* clear accounting */ 94 #define IP_FW_XRESETLOG 102 /* zero rules logs */ 95 #define IP_FW_SET_SWAP 103 /* Swap between 2 sets */ 96 #define IP_FW_SET_MOVE 104 /* Move one set to another one */ 97 #define IP_FW_SET_ENABLE 105 /* Enable/disable sets */ 98 #define IP_FW_TABLE_XFIND 106 /* finds an entry */ 99 #define IP_FW_XIFLIST 107 /* list tracked interfaces */ 100 #define IP_FW_TABLES_ALIST 108 /* list table algorithms */ 101 #define IP_FW_TABLE_XSWAP 109 /* swap two tables */ 102 #define IP_FW_TABLE_VLIST 110 /* dump table value hash */ 103 104 #define IP_FW_NAT44_XCONFIG 111 /* Create/modify NAT44 instance */ 105 #define IP_FW_NAT44_DESTROY 112 /* Destroys NAT44 instance */ 106 #define IP_FW_NAT44_XGETCONFIG 113 /* Get NAT44 instance config */ 107 #define IP_FW_NAT44_LIST_NAT 114 /* List all NAT44 instances */ 108 #define IP_FW_NAT44_XGETLOG 115 /* Get log from NAT44 instance */ 109 110 #define IP_FW_DUMP_SOPTCODES 116 /* Dump available sopts/versions */ 111 #define IP_FW_DUMP_SRVOBJECTS 117 /* Dump existing named objects */ 112 113 #define IP_FW_NAT64STL_CREATE 130 /* Create stateless NAT64 instance */ 114 #define IP_FW_NAT64STL_DESTROY 131 /* Destroy stateless NAT64 instance */ 115 #define IP_FW_NAT64STL_CONFIG 132 /* Modify stateless NAT64 instance */ 116 #define IP_FW_NAT64STL_LIST 133 /* List stateless NAT64 instances */ 117 #define IP_FW_NAT64STL_STATS 134 /* Get NAT64STL instance statistics */ 118 #define IP_FW_NAT64STL_RESET_STATS 135 /* Reset NAT64STL instance statistics */ 119 120 #define IP_FW_NAT64LSN_CREATE 140 /* Create stateful NAT64 instance */ 121 #define IP_FW_NAT64LSN_DESTROY 141 /* Destroy stateful NAT64 instance */ 122 #define IP_FW_NAT64LSN_CONFIG 142 /* Modify stateful NAT64 instance */ 123 #define IP_FW_NAT64LSN_LIST 143 /* List stateful NAT64 instances */ 124 #define IP_FW_NAT64LSN_STATS 144 /* Get NAT64LSN instance statistics */ 125 #define IP_FW_NAT64LSN_LIST_STATES 145 /* Get stateful NAT64 states */ 126 #define IP_FW_NAT64LSN_RESET_STATS 146 /* Reset NAT64LSN instance statistics */ 127 128 #define IP_FW_NPTV6_CREATE 150 /* Create NPTv6 instance */ 129 #define IP_FW_NPTV6_DESTROY 151 /* Destroy NPTv6 instance */ 130 #define IP_FW_NPTV6_CONFIG 152 /* Modify NPTv6 instance */ 131 #define IP_FW_NPTV6_LIST 153 /* List NPTv6 instances */ 132 #define IP_FW_NPTV6_STATS 154 /* Get NPTv6 instance statistics */ 133 #define IP_FW_NPTV6_RESET_STATS 155 /* Reset NPTv6 instance statistics */ 134 135 /* 136 * The kernel representation of ipfw rules is made of a list of 137 * 'instructions' (for all practical purposes equivalent to BPF 138 * instructions), which specify which fields of the packet 139 * (or its metadata) should be analysed. 140 * 141 * Each instruction is stored in a structure which begins with 142 * "ipfw_insn", and can contain extra fields depending on the 143 * instruction type (listed below). 144 * Note that the code is written so that individual instructions 145 * have a size which is a multiple of 32 bits. This means that, if 146 * such structures contain pointers or other 64-bit entities, 147 * (there is just one instance now) they may end up unaligned on 148 * 64-bit architectures, so the must be handled with care. 149 * 150 * "enum ipfw_opcodes" are the opcodes supported. We can have up 151 * to 256 different opcodes. When adding new opcodes, they should 152 * be appended to the end of the opcode list before O_LAST_OPCODE, 153 * this will prevent the ABI from being broken, otherwise users 154 * will have to recompile ipfw(8) when they update the kernel. 155 */ 156 157 enum ipfw_opcodes { /* arguments (4 byte each) */ 158 O_NOP, 159 160 O_IP_SRC, /* u32 = IP */ 161 O_IP_SRC_MASK, /* ip = IP/mask */ 162 O_IP_SRC_ME, /* none */ 163 O_IP_SRC_SET, /* u32=base, arg1=len, bitmap */ 164 165 O_IP_DST, /* u32 = IP */ 166 O_IP_DST_MASK, /* ip = IP/mask */ 167 O_IP_DST_ME, /* none */ 168 O_IP_DST_SET, /* u32=base, arg1=len, bitmap */ 169 170 O_IP_SRCPORT, /* (n)port list:mask 4 byte ea */ 171 O_IP_DSTPORT, /* (n)port list:mask 4 byte ea */ 172 O_PROTO, /* arg1=protocol */ 173 174 O_MACADDR2, /* 2 mac addr:mask */ 175 O_MAC_TYPE, /* same as srcport */ 176 177 O_LAYER2, /* none */ 178 O_IN, /* none */ 179 O_FRAG, /* none */ 180 181 O_RECV, /* none */ 182 O_XMIT, /* none */ 183 O_VIA, /* none */ 184 185 O_IPOPT, /* arg1 = 2*u8 bitmap */ 186 O_IPLEN, /* arg1 = len */ 187 O_IPID, /* arg1 = id */ 188 189 O_IPTOS, /* arg1 = id */ 190 O_IPPRECEDENCE, /* arg1 = precedence << 5 */ 191 O_IPTTL, /* arg1 = TTL */ 192 193 O_IPVER, /* arg1 = version */ 194 O_UID, /* u32 = id */ 195 O_GID, /* u32 = id */ 196 O_ESTAB, /* none (tcp established) */ 197 O_TCPFLAGS, /* arg1 = 2*u8 bitmap */ 198 O_TCPWIN, /* arg1 = desired win */ 199 O_TCPSEQ, /* u32 = desired seq. */ 200 O_TCPACK, /* u32 = desired seq. */ 201 O_ICMPTYPE, /* u32 = icmp bitmap */ 202 O_TCPOPTS, /* arg1 = 2*u8 bitmap */ 203 204 O_VERREVPATH, /* none */ 205 O_VERSRCREACH, /* none */ 206 207 O_PROBE_STATE, /* none */ 208 O_KEEP_STATE, /* none */ 209 O_LIMIT, /* ipfw_insn_limit */ 210 O_LIMIT_PARENT, /* dyn_type, not an opcode. */ 211 212 /* 213 * These are really 'actions'. 214 */ 215 216 O_LOG, /* ipfw_insn_log */ 217 O_PROB, /* u32 = match probability */ 218 219 O_CHECK_STATE, /* none */ 220 O_ACCEPT, /* none */ 221 O_DENY, /* none */ 222 O_REJECT, /* arg1=icmp arg (same as deny) */ 223 O_COUNT, /* none */ 224 O_SKIPTO, /* arg1=next rule number */ 225 O_PIPE, /* arg1=pipe number */ 226 O_QUEUE, /* arg1=queue number */ 227 O_DIVERT, /* arg1=port number */ 228 O_TEE, /* arg1=port number */ 229 O_FORWARD_IP, /* fwd sockaddr */ 230 O_FORWARD_MAC, /* fwd mac */ 231 O_NAT, /* nope */ 232 O_REASS, /* none */ 233 234 /* 235 * More opcodes. 236 */ 237 O_IPSEC, /* has ipsec history */ 238 O_IP_SRC_LOOKUP, /* arg1=table number, u32=value */ 239 O_IP_DST_LOOKUP, /* arg1=table number, u32=value */ 240 O_ANTISPOOF, /* none */ 241 O_JAIL, /* u32 = id */ 242 O_ALTQ, /* u32 = altq classif. qid */ 243 O_DIVERTED, /* arg1=bitmap (1:loop, 2:out) */ 244 O_TCPDATALEN, /* arg1 = tcp data len */ 245 O_IP6_SRC, /* address without mask */ 246 O_IP6_SRC_ME, /* my addresses */ 247 O_IP6_SRC_MASK, /* address with the mask */ 248 O_IP6_DST, 249 O_IP6_DST_ME, 250 O_IP6_DST_MASK, 251 O_FLOW6ID, /* for flow id tag in the ipv6 pkt */ 252 O_ICMP6TYPE, /* icmp6 packet type filtering */ 253 O_EXT_HDR, /* filtering for ipv6 extension header */ 254 O_IP6, 255 256 /* 257 * actions for ng_ipfw 258 */ 259 O_NETGRAPH, /* send to ng_ipfw */ 260 O_NGTEE, /* copy to ng_ipfw */ 261 262 O_IP4, 263 264 O_UNREACH6, /* arg1=icmpv6 code arg (deny) */ 265 266 O_TAG, /* arg1=tag number */ 267 O_TAGGED, /* arg1=tag number */ 268 269 O_SETFIB, /* arg1=FIB number */ 270 O_FIB, /* arg1=FIB desired fib number */ 271 272 O_SOCKARG, /* socket argument */ 273 274 O_CALLRETURN, /* arg1=called rule number */ 275 276 O_FORWARD_IP6, /* fwd sockaddr_in6 */ 277 278 O_DSCP, /* 2 u32 = DSCP mask */ 279 O_SETDSCP, /* arg1=DSCP value */ 280 O_IP_FLOW_LOOKUP, /* arg1=table number, u32=value */ 281 282 O_EXTERNAL_ACTION, /* arg1=id of external action handler */ 283 O_EXTERNAL_INSTANCE, /* arg1=id of eaction handler instance */ 284 O_EXTERNAL_DATA, /* variable length data */ 285 286 O_LAST_OPCODE /* not an opcode! */ 287 }; 288 289 /* 290 * The extension header are filtered only for presence using a bit 291 * vector with a flag for each header. 292 */ 293 #define EXT_FRAGMENT 0x1 294 #define EXT_HOPOPTS 0x2 295 #define EXT_ROUTING 0x4 296 #define EXT_AH 0x8 297 #define EXT_ESP 0x10 298 #define EXT_DSTOPTS 0x20 299 #define EXT_RTHDR0 0x40 300 #define EXT_RTHDR2 0x80 301 302 /* 303 * Template for instructions. 304 * 305 * ipfw_insn is used for all instructions which require no operands, 306 * a single 16-bit value (arg1), or a couple of 8-bit values. 307 * 308 * For other instructions which require different/larger arguments 309 * we have derived structures, ipfw_insn_*. 310 * 311 * The size of the instruction (in 32-bit words) is in the low 312 * 6 bits of "len". The 2 remaining bits are used to implement 313 * NOT and OR on individual instructions. Given a type, you can 314 * compute the length to be put in "len" using F_INSN_SIZE(t) 315 * 316 * F_NOT negates the match result of the instruction. 317 * 318 * F_OR is used to build or blocks. By default, instructions 319 * are evaluated as part of a logical AND. An "or" block 320 * { X or Y or Z } contains F_OR set in all but the last 321 * instruction of the block. A match will cause the code 322 * to skip past the last instruction of the block. 323 * 324 * NOTA BENE: in a couple of places we assume that 325 * sizeof(ipfw_insn) == sizeof(u_int32_t) 326 * this needs to be fixed. 327 * 328 */ 329 typedef struct _ipfw_insn { /* template for instructions */ 330 u_int8_t opcode; 331 u_int8_t len; /* number of 32-bit words */ 332 #define F_NOT 0x80 333 #define F_OR 0x40 334 #define F_LEN_MASK 0x3f 335 #define F_LEN(cmd) ((cmd)->len & F_LEN_MASK) 336 337 u_int16_t arg1; 338 } ipfw_insn; 339 340 /* 341 * The F_INSN_SIZE(type) computes the size, in 4-byte words, of 342 * a given type. 343 */ 344 #define F_INSN_SIZE(t) ((sizeof (t))/sizeof(u_int32_t)) 345 346 /* 347 * This is used to store an array of 16-bit entries (ports etc.) 348 */ 349 typedef struct _ipfw_insn_u16 { 350 ipfw_insn o; 351 u_int16_t ports[2]; /* there may be more */ 352 } ipfw_insn_u16; 353 354 /* 355 * This is used to store an array of 32-bit entries 356 * (uid, single IPv4 addresses etc.) 357 */ 358 typedef struct _ipfw_insn_u32 { 359 ipfw_insn o; 360 u_int32_t d[1]; /* one or more */ 361 } ipfw_insn_u32; 362 363 /* 364 * This is used to store IP addr-mask pairs. 365 */ 366 typedef struct _ipfw_insn_ip { 367 ipfw_insn o; 368 struct in_addr addr; 369 struct in_addr mask; 370 } ipfw_insn_ip; 371 372 /* 373 * This is used to forward to a given address (ip). 374 */ 375 typedef struct _ipfw_insn_sa { 376 ipfw_insn o; 377 struct sockaddr_in sa; 378 } ipfw_insn_sa; 379 380 /* 381 * This is used to forward to a given address (ipv6). 382 */ 383 typedef struct _ipfw_insn_sa6 { 384 ipfw_insn o; 385 struct sockaddr_in6 sa; 386 } ipfw_insn_sa6; 387 388 /* 389 * This is used for MAC addr-mask pairs. 390 */ 391 typedef struct _ipfw_insn_mac { 392 ipfw_insn o; 393 u_char addr[12]; /* dst[6] + src[6] */ 394 u_char mask[12]; /* dst[6] + src[6] */ 395 } ipfw_insn_mac; 396 397 /* 398 * This is used for interface match rules (recv xx, xmit xx). 399 */ 400 typedef struct _ipfw_insn_if { 401 ipfw_insn o; 402 union { 403 struct in_addr ip; 404 int glob; 405 uint16_t kidx; 406 } p; 407 char name[IFNAMSIZ]; 408 } ipfw_insn_if; 409 410 /* 411 * This is used for storing an altq queue id number. 412 */ 413 typedef struct _ipfw_insn_altq { 414 ipfw_insn o; 415 u_int32_t qid; 416 } ipfw_insn_altq; 417 418 /* 419 * This is used for limit rules. 420 */ 421 typedef struct _ipfw_insn_limit { 422 ipfw_insn o; 423 u_int8_t _pad; 424 u_int8_t limit_mask; /* combination of DYN_* below */ 425 #define DYN_SRC_ADDR 0x1 426 #define DYN_SRC_PORT 0x2 427 #define DYN_DST_ADDR 0x4 428 #define DYN_DST_PORT 0x8 429 430 u_int16_t conn_limit; 431 } ipfw_insn_limit; 432 433 /* 434 * This is used for log instructions. 435 */ 436 typedef struct _ipfw_insn_log { 437 ipfw_insn o; 438 u_int32_t max_log; /* how many do we log -- 0 = all */ 439 u_int32_t log_left; /* how many left to log */ 440 } ipfw_insn_log; 441 442 /* Legacy NAT structures, compat only */ 443 #ifndef _KERNEL 444 /* 445 * Data structures required by both ipfw(8) and ipfw(4) but not part of the 446 * management API are protected by IPFW_INTERNAL. 447 */ 448 #ifdef IPFW_INTERNAL 449 /* Server pool support (LSNAT). */ 450 struct cfg_spool { 451 LIST_ENTRY(cfg_spool) _next; /* chain of spool instances */ 452 struct in_addr addr; 453 u_short port; 454 }; 455 #endif 456 457 /* Redirect modes id. */ 458 #define REDIR_ADDR 0x01 459 #define REDIR_PORT 0x02 460 #define REDIR_PROTO 0x04 461 462 #ifdef IPFW_INTERNAL 463 /* Nat redirect configuration. */ 464 struct cfg_redir { 465 LIST_ENTRY(cfg_redir) _next; /* chain of redir instances */ 466 u_int16_t mode; /* type of redirect mode */ 467 struct in_addr laddr; /* local ip address */ 468 struct in_addr paddr; /* public ip address */ 469 struct in_addr raddr; /* remote ip address */ 470 u_short lport; /* local port */ 471 u_short pport; /* public port */ 472 u_short rport; /* remote port */ 473 u_short pport_cnt; /* number of public ports */ 474 u_short rport_cnt; /* number of remote ports */ 475 int proto; /* protocol: tcp/udp */ 476 struct alias_link **alink; 477 /* num of entry in spool chain */ 478 u_int16_t spool_cnt; 479 /* chain of spool instances */ 480 LIST_HEAD(spool_chain, cfg_spool) spool_chain; 481 }; 482 #endif 483 484 #ifdef IPFW_INTERNAL 485 /* Nat configuration data struct. */ 486 struct cfg_nat { 487 /* chain of nat instances */ 488 LIST_ENTRY(cfg_nat) _next; 489 int id; /* nat id */ 490 struct in_addr ip; /* nat ip address */ 491 char if_name[IF_NAMESIZE]; /* interface name */ 492 int mode; /* aliasing mode */ 493 struct libalias *lib; /* libalias instance */ 494 /* number of entry in spool chain */ 495 int redir_cnt; 496 /* chain of redir instances */ 497 LIST_HEAD(redir_chain, cfg_redir) redir_chain; 498 }; 499 #endif 500 501 #define SOF_NAT sizeof(struct cfg_nat) 502 #define SOF_REDIR sizeof(struct cfg_redir) 503 #define SOF_SPOOL sizeof(struct cfg_spool) 504 505 #endif /* ifndef _KERNEL */ 506 507 508 struct nat44_cfg_spool { 509 struct in_addr addr; 510 uint16_t port; 511 uint16_t spare; 512 }; 513 #define NAT44_REDIR_ADDR 0x01 514 #define NAT44_REDIR_PORT 0x02 515 #define NAT44_REDIR_PROTO 0x04 516 517 /* Nat redirect configuration. */ 518 struct nat44_cfg_redir { 519 struct in_addr laddr; /* local ip address */ 520 struct in_addr paddr; /* public ip address */ 521 struct in_addr raddr; /* remote ip address */ 522 uint16_t lport; /* local port */ 523 uint16_t pport; /* public port */ 524 uint16_t rport; /* remote port */ 525 uint16_t pport_cnt; /* number of public ports */ 526 uint16_t rport_cnt; /* number of remote ports */ 527 uint16_t mode; /* type of redirect mode */ 528 uint16_t spool_cnt; /* num of entry in spool chain */ 529 uint16_t spare; 530 uint32_t proto; /* protocol: tcp/udp */ 531 }; 532 533 /* Nat configuration data struct. */ 534 struct nat44_cfg_nat { 535 char name[64]; /* nat name */ 536 char if_name[64]; /* interface name */ 537 uint32_t size; /* structure size incl. redirs */ 538 struct in_addr ip; /* nat IPv4 address */ 539 uint32_t mode; /* aliasing mode */ 540 uint32_t redir_cnt; /* number of entry in spool chain */ 541 }; 542 543 /* Nat command. */ 544 typedef struct _ipfw_insn_nat { 545 ipfw_insn o; 546 struct cfg_nat *nat; 547 } ipfw_insn_nat; 548 549 /* Apply ipv6 mask on ipv6 addr */ 550 #define APPLY_MASK(addr,mask) \ 551 (addr)->__u6_addr.__u6_addr32[0] &= (mask)->__u6_addr.__u6_addr32[0]; \ 552 (addr)->__u6_addr.__u6_addr32[1] &= (mask)->__u6_addr.__u6_addr32[1]; \ 553 (addr)->__u6_addr.__u6_addr32[2] &= (mask)->__u6_addr.__u6_addr32[2]; \ 554 (addr)->__u6_addr.__u6_addr32[3] &= (mask)->__u6_addr.__u6_addr32[3]; 555 556 /* Structure for ipv6 */ 557 typedef struct _ipfw_insn_ip6 { 558 ipfw_insn o; 559 struct in6_addr addr6; 560 struct in6_addr mask6; 561 } ipfw_insn_ip6; 562 563 /* Used to support icmp6 types */ 564 typedef struct _ipfw_insn_icmp6 { 565 ipfw_insn o; 566 uint32_t d[7]; /* XXX This number si related to the netinet/icmp6.h 567 * define ICMP6_MAXTYPE 568 * as follows: n = ICMP6_MAXTYPE/32 + 1 569 * Actually is 203 570 */ 571 } ipfw_insn_icmp6; 572 573 /* 574 * Here we have the structure representing an ipfw rule. 575 * 576 * Layout: 577 * struct ip_fw_rule 578 * [ counter block, size = rule->cntr_len ] 579 * [ one or more instructions, size = rule->cmd_len * 4 ] 580 * 581 * It starts with a general area (with link fields). 582 * Counter block may be next (if rule->cntr_len > 0), 583 * followed by an array of one or more instructions, which the code 584 * accesses as an array of 32-bit values. rule->cmd_len represents 585 * the total instructions legth in u32 worrd, while act_ofs represents 586 * rule action offset in u32 words. 587 * 588 * When assembling instruction, remember the following: 589 * 590 * + if a rule has a "keep-state" (or "limit") option, then the 591 * first instruction (at r->cmd) MUST BE an O_PROBE_STATE 592 * + if a rule has a "log" option, then the first action 593 * (at ACTION_PTR(r)) MUST be O_LOG 594 * + if a rule has an "altq" option, it comes after "log" 595 * + if a rule has an O_TAG option, it comes after "log" and "altq" 596 * 597 * 598 * All structures (excluding instructions) are u64-aligned. 599 * Please keep this. 600 */ 601 602 struct ip_fw_rule { 603 uint16_t act_ofs; /* offset of action in 32-bit units */ 604 uint16_t cmd_len; /* # of 32-bit words in cmd */ 605 uint16_t spare; 606 uint8_t set; /* rule set (0..31) */ 607 uint8_t flags; /* rule flags */ 608 uint32_t rulenum; /* rule number */ 609 uint32_t id; /* rule id */ 610 611 ipfw_insn cmd[1]; /* storage for commands */ 612 }; 613 #define IPFW_RULE_NOOPT 0x01 /* Has no options in body */ 614 615 /* Unaligned version */ 616 617 /* Base ipfw rule counter block. */ 618 struct ip_fw_bcounter { 619 uint16_t size; /* Size of counter block, bytes */ 620 uint8_t flags; /* flags for given block */ 621 uint8_t spare; 622 uint32_t timestamp; /* tv_sec of last match */ 623 uint64_t pcnt; /* Packet counter */ 624 uint64_t bcnt; /* Byte counter */ 625 }; 626 627 628 #ifndef _KERNEL 629 /* 630 * Legacy rule format 631 */ 632 struct ip_fw { 633 struct ip_fw *x_next; /* linked list of rules */ 634 struct ip_fw *next_rule; /* ptr to next [skipto] rule */ 635 /* 'next_rule' is used to pass up 'set_disable' status */ 636 637 uint16_t act_ofs; /* offset of action in 32-bit units */ 638 uint16_t cmd_len; /* # of 32-bit words in cmd */ 639 uint16_t rulenum; /* rule number */ 640 uint8_t set; /* rule set (0..31) */ 641 uint8_t _pad; /* padding */ 642 uint32_t id; /* rule id */ 643 644 /* These fields are present in all rules. */ 645 uint64_t pcnt; /* Packet counter */ 646 uint64_t bcnt; /* Byte counter */ 647 uint32_t timestamp; /* tv_sec of last match */ 648 649 ipfw_insn cmd[1]; /* storage for commands */ 650 }; 651 #endif 652 653 #define ACTION_PTR(rule) \ 654 (ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) ) 655 656 #define RULESIZE(rule) (sizeof(*(rule)) + (rule)->cmd_len * 4 - 4) 657 658 659 #if 1 // should be moved to in.h 660 /* 661 * This structure is used as a flow mask and a flow id for various 662 * parts of the code. 663 * addr_type is used in userland and kernel to mark the address type. 664 * fib is used in the kernel to record the fib in use. 665 * _flags is used in the kernel to store tcp flags for dynamic rules. 666 */ 667 struct ipfw_flow_id { 668 uint32_t dst_ip; 669 uint32_t src_ip; 670 uint16_t dst_port; 671 uint16_t src_port; 672 uint8_t fib; 673 uint8_t proto; 674 uint8_t _flags; /* protocol-specific flags */ 675 uint8_t addr_type; /* 4=ip4, 6=ip6, 1=ether ? */ 676 struct in6_addr dst_ip6; 677 struct in6_addr src_ip6; 678 uint32_t flow_id6; 679 uint32_t extra; /* queue/pipe or frag_id */ 680 }; 681 #endif 682 683 #define IS_IP6_FLOW_ID(id) ((id)->addr_type == 6) 684 685 /* 686 * Dynamic ipfw rule. 687 */ 688 typedef struct _ipfw_dyn_rule ipfw_dyn_rule; 689 690 struct _ipfw_dyn_rule { 691 ipfw_dyn_rule *next; /* linked list of rules. */ 692 struct ip_fw *rule; /* pointer to rule */ 693 /* 'rule' is used to pass up the rule number (from the parent) */ 694 695 ipfw_dyn_rule *parent; /* pointer to parent rule */ 696 u_int64_t pcnt; /* packet match counter */ 697 u_int64_t bcnt; /* byte match counter */ 698 struct ipfw_flow_id id; /* (masked) flow id */ 699 u_int32_t expire; /* expire time */ 700 u_int32_t bucket; /* which bucket in hash table */ 701 u_int32_t state; /* state of this rule (typically a 702 * combination of TCP flags) 703 */ 704 u_int32_t ack_fwd; /* most recent ACKs in forward */ 705 u_int32_t ack_rev; /* and reverse directions (used */ 706 /* to generate keepalives) */ 707 u_int16_t dyn_type; /* rule type */ 708 u_int16_t count; /* refcount */ 709 u_int16_t kidx; /* index of named object */ 710 } __packed __aligned(8); 711 712 /* 713 * Definitions for IP option names. 714 */ 715 #define IP_FW_IPOPT_LSRR 0x01 716 #define IP_FW_IPOPT_SSRR 0x02 717 #define IP_FW_IPOPT_RR 0x04 718 #define IP_FW_IPOPT_TS 0x08 719 720 /* 721 * Definitions for TCP option names. 722 */ 723 #define IP_FW_TCPOPT_MSS 0x01 724 #define IP_FW_TCPOPT_WINDOW 0x02 725 #define IP_FW_TCPOPT_SACK 0x04 726 #define IP_FW_TCPOPT_TS 0x08 727 #define IP_FW_TCPOPT_CC 0x10 728 729 #define ICMP_REJECT_RST 0x100 /* fake ICMP code (send a TCP RST) */ 730 #define ICMP6_UNREACH_RST 0x100 /* fake ICMPv6 code (send a TCP RST) */ 731 732 /* 733 * These are used for lookup tables. 734 */ 735 736 #define IPFW_TABLE_ADDR 1 /* Table for holding IPv4/IPv6 prefixes */ 737 #define IPFW_TABLE_INTERFACE 2 /* Table for holding interface names */ 738 #define IPFW_TABLE_NUMBER 3 /* Table for holding ports/uid/gid/etc */ 739 #define IPFW_TABLE_FLOW 4 /* Table for holding flow data */ 740 #define IPFW_TABLE_MAXTYPE 4 /* Maximum valid number */ 741 742 #define IPFW_TABLE_CIDR IPFW_TABLE_ADDR /* compat */ 743 744 /* Value types */ 745 #define IPFW_VTYPE_LEGACY 0xFFFFFFFF /* All data is filled in */ 746 #define IPFW_VTYPE_SKIPTO 0x00000001 /* skipto/call/callreturn */ 747 #define IPFW_VTYPE_PIPE 0x00000002 /* pipe/queue */ 748 #define IPFW_VTYPE_FIB 0x00000004 /* setfib */ 749 #define IPFW_VTYPE_NAT 0x00000008 /* nat */ 750 #define IPFW_VTYPE_DSCP 0x00000010 /* dscp */ 751 #define IPFW_VTYPE_TAG 0x00000020 /* tag/untag */ 752 #define IPFW_VTYPE_DIVERT 0x00000040 /* divert/tee */ 753 #define IPFW_VTYPE_NETGRAPH 0x00000080 /* netgraph/ngtee */ 754 #define IPFW_VTYPE_LIMIT 0x00000100 /* limit */ 755 #define IPFW_VTYPE_NH4 0x00000200 /* IPv4 nexthop */ 756 #define IPFW_VTYPE_NH6 0x00000400 /* IPv6 nexthop */ 757 758 typedef struct _ipfw_table_entry { 759 in_addr_t addr; /* network address */ 760 u_int32_t value; /* value */ 761 u_int16_t tbl; /* table number */ 762 u_int8_t masklen; /* mask length */ 763 } ipfw_table_entry; 764 765 typedef struct _ipfw_table_xentry { 766 uint16_t len; /* Total entry length */ 767 uint8_t type; /* entry type */ 768 uint8_t masklen; /* mask length */ 769 uint16_t tbl; /* table number */ 770 uint16_t flags; /* record flags */ 771 uint32_t value; /* value */ 772 union { 773 /* Longest field needs to be aligned by 4-byte boundary */ 774 struct in6_addr addr6; /* IPv6 address */ 775 char iface[IF_NAMESIZE]; /* interface name */ 776 } k; 777 } ipfw_table_xentry; 778 #define IPFW_TCF_INET 0x01 /* CIDR flags: IPv4 record */ 779 780 typedef struct _ipfw_table { 781 u_int32_t size; /* size of entries in bytes */ 782 u_int32_t cnt; /* # of entries */ 783 u_int16_t tbl; /* table number */ 784 ipfw_table_entry ent[0]; /* entries */ 785 } ipfw_table; 786 787 typedef struct _ipfw_xtable { 788 ip_fw3_opheader opheader; /* IP_FW3 opcode */ 789 uint32_t size; /* size of entries in bytes */ 790 uint32_t cnt; /* # of entries */ 791 uint16_t tbl; /* table number */ 792 uint8_t type; /* table type */ 793 ipfw_table_xentry xent[0]; /* entries */ 794 } ipfw_xtable; 795 796 typedef struct _ipfw_obj_tlv { 797 uint16_t type; /* TLV type */ 798 uint16_t flags; /* TLV-specific flags */ 799 uint32_t length; /* Total length, aligned to u64 */ 800 } ipfw_obj_tlv; 801 #define IPFW_TLV_TBL_NAME 1 802 #define IPFW_TLV_TBLNAME_LIST 2 803 #define IPFW_TLV_RULE_LIST 3 804 #define IPFW_TLV_DYNSTATE_LIST 4 805 #define IPFW_TLV_TBL_ENT 5 806 #define IPFW_TLV_DYN_ENT 6 807 #define IPFW_TLV_RULE_ENT 7 808 #define IPFW_TLV_TBLENT_LIST 8 809 #define IPFW_TLV_RANGE 9 810 #define IPFW_TLV_EACTION 10 811 #define IPFW_TLV_COUNTERS 11 812 #define IPFW_TLV_OBJDATA 12 813 #define IPFW_TLV_STATE_NAME 14 814 815 #define IPFW_TLV_EACTION_BASE 1000 816 #define IPFW_TLV_EACTION_NAME(arg) (IPFW_TLV_EACTION_BASE + (arg)) 817 818 typedef struct _ipfw_obj_data { 819 ipfw_obj_tlv head; 820 void *data[0]; 821 } ipfw_obj_data; 822 823 /* Object name TLV */ 824 typedef struct _ipfw_obj_ntlv { 825 ipfw_obj_tlv head; /* TLV header */ 826 uint16_t idx; /* Name index */ 827 uint8_t set; /* set, if applicable */ 828 uint8_t type; /* object type, if applicable */ 829 uint32_t spare; /* unused */ 830 char name[64]; /* Null-terminated name */ 831 } ipfw_obj_ntlv; 832 833 /* IPv4/IPv6 L4 flow description */ 834 struct tflow_entry { 835 uint8_t af; 836 uint8_t proto; 837 uint16_t spare; 838 uint16_t sport; 839 uint16_t dport; 840 union { 841 struct { 842 struct in_addr sip; 843 struct in_addr dip; 844 } a4; 845 struct { 846 struct in6_addr sip6; 847 struct in6_addr dip6; 848 } a6; 849 } a; 850 }; 851 852 typedef struct _ipfw_table_value { 853 uint32_t tag; /* O_TAG/O_TAGGED */ 854 uint32_t pipe; /* O_PIPE/O_QUEUE */ 855 uint16_t divert; /* O_DIVERT/O_TEE */ 856 uint16_t skipto; /* skipto, CALLRET */ 857 uint32_t netgraph; /* O_NETGRAPH/O_NGTEE */ 858 uint32_t fib; /* O_SETFIB */ 859 uint32_t nat; /* O_NAT */ 860 uint32_t nh4; 861 uint8_t dscp; 862 uint8_t spare0; 863 uint16_t spare1; 864 struct in6_addr nh6; 865 uint32_t limit; /* O_LIMIT */ 866 uint32_t zoneid; /* scope zone id for nh6 */ 867 uint64_t reserved; 868 } ipfw_table_value; 869 870 /* Table entry TLV */ 871 typedef struct _ipfw_obj_tentry { 872 ipfw_obj_tlv head; /* TLV header */ 873 uint8_t subtype; /* subtype (IPv4,IPv6) */ 874 uint8_t masklen; /* mask length */ 875 uint8_t result; /* request result */ 876 uint8_t spare0; 877 uint16_t idx; /* Table name index */ 878 uint16_t spare1; 879 union { 880 /* Longest field needs to be aligned by 8-byte boundary */ 881 struct in_addr addr; /* IPv4 address */ 882 uint32_t key; /* uid/gid/port */ 883 struct in6_addr addr6; /* IPv6 address */ 884 char iface[IF_NAMESIZE]; /* interface name */ 885 struct tflow_entry flow; 886 } k; 887 union { 888 ipfw_table_value value; /* value data */ 889 uint32_t kidx; /* value kernel index */ 890 } v; 891 } ipfw_obj_tentry; 892 #define IPFW_TF_UPDATE 0x01 /* Update record if exists */ 893 /* Container TLV */ 894 #define IPFW_CTF_ATOMIC 0x01 /* Perform atomic operation */ 895 /* Operation results */ 896 #define IPFW_TR_IGNORED 0 /* Entry was ignored (rollback) */ 897 #define IPFW_TR_ADDED 1 /* Entry was successfully added */ 898 #define IPFW_TR_UPDATED 2 /* Entry was successfully updated*/ 899 #define IPFW_TR_DELETED 3 /* Entry was successfully deleted*/ 900 #define IPFW_TR_LIMIT 4 /* Entry was ignored (limit) */ 901 #define IPFW_TR_NOTFOUND 5 /* Entry was not found */ 902 #define IPFW_TR_EXISTS 6 /* Entry already exists */ 903 #define IPFW_TR_ERROR 7 /* Request has failed (unknown) */ 904 905 typedef struct _ipfw_obj_dyntlv { 906 ipfw_obj_tlv head; 907 ipfw_dyn_rule state; 908 } ipfw_obj_dyntlv; 909 #define IPFW_DF_LAST 0x01 /* Last state in chain */ 910 911 /* Containter TLVs */ 912 typedef struct _ipfw_obj_ctlv { 913 ipfw_obj_tlv head; /* TLV header */ 914 uint32_t count; /* Number of sub-TLVs */ 915 uint16_t objsize; /* Single object size */ 916 uint8_t version; /* TLV version */ 917 uint8_t flags; /* TLV-specific flags */ 918 } ipfw_obj_ctlv; 919 920 /* Range TLV */ 921 typedef struct _ipfw_range_tlv { 922 ipfw_obj_tlv head; /* TLV header */ 923 uint32_t flags; /* Range flags */ 924 uint16_t start_rule; /* Range start */ 925 uint16_t end_rule; /* Range end */ 926 uint32_t set; /* Range set to match */ 927 uint32_t new_set; /* New set to move/swap to */ 928 } ipfw_range_tlv; 929 #define IPFW_RCFLAG_RANGE 0x01 /* rule range is set */ 930 #define IPFW_RCFLAG_ALL 0x02 /* match ALL rules */ 931 #define IPFW_RCFLAG_SET 0x04 /* match rules in given set */ 932 /* User-settable flags */ 933 #define IPFW_RCFLAG_USER (IPFW_RCFLAG_RANGE | IPFW_RCFLAG_ALL | \ 934 IPFW_RCFLAG_SET) 935 /* Internally used flags */ 936 #define IPFW_RCFLAG_DEFAULT 0x0100 /* Do not skip defaul rule */ 937 938 typedef struct _ipfw_ta_tinfo { 939 uint32_t flags; /* Format flags */ 940 uint32_t spare; 941 uint8_t taclass4; /* algorithm class */ 942 uint8_t spare4; 943 uint16_t itemsize4; /* item size in runtime */ 944 uint32_t size4; /* runtime structure size */ 945 uint32_t count4; /* number of items in runtime */ 946 uint8_t taclass6; /* algorithm class */ 947 uint8_t spare6; 948 uint16_t itemsize6; /* item size in runtime */ 949 uint32_t size6; /* runtime structure size */ 950 uint32_t count6; /* number of items in runtime */ 951 } ipfw_ta_tinfo; 952 #define IPFW_TACLASS_HASH 1 /* algo is based on hash */ 953 #define IPFW_TACLASS_ARRAY 2 /* algo is based on array */ 954 #define IPFW_TACLASS_RADIX 3 /* algo is based on radix tree */ 955 956 #define IPFW_TATFLAGS_DATA 0x0001 /* Has data filled in */ 957 #define IPFW_TATFLAGS_AFDATA 0x0002 /* Separate data per AF */ 958 #define IPFW_TATFLAGS_AFITEM 0x0004 /* diff. items per AF */ 959 960 typedef struct _ipfw_xtable_info { 961 uint8_t type; /* table type (addr,iface,..) */ 962 uint8_t tflags; /* type flags */ 963 uint16_t mflags; /* modification flags */ 964 uint16_t flags; /* generic table flags */ 965 uint16_t spare[3]; 966 uint32_t vmask; /* bitmask with value types */ 967 uint32_t set; /* set table is in */ 968 uint32_t kidx; /* kernel index */ 969 uint32_t refcnt; /* number of references */ 970 uint32_t count; /* Number of records */ 971 uint32_t size; /* Total size of records(export)*/ 972 uint32_t limit; /* Max number of records */ 973 char tablename[64]; /* table name */ 974 char algoname[64]; /* algorithm name */ 975 ipfw_ta_tinfo ta_info; /* additional algo stats */ 976 } ipfw_xtable_info; 977 /* Generic table flags */ 978 #define IPFW_TGFLAGS_LOCKED 0x01 /* Tables is locked from changes*/ 979 /* Table type-specific flags */ 980 #define IPFW_TFFLAG_SRCIP 0x01 981 #define IPFW_TFFLAG_DSTIP 0x02 982 #define IPFW_TFFLAG_SRCPORT 0x04 983 #define IPFW_TFFLAG_DSTPORT 0x08 984 #define IPFW_TFFLAG_PROTO 0x10 985 /* Table modification flags */ 986 #define IPFW_TMFLAGS_LIMIT 0x0002 /* Change limit value */ 987 #define IPFW_TMFLAGS_LOCK 0x0004 /* Change table lock state */ 988 989 typedef struct _ipfw_iface_info { 990 char ifname[64]; /* interface name */ 991 uint32_t ifindex; /* interface index */ 992 uint32_t flags; /* flags */ 993 uint32_t refcnt; /* number of references */ 994 uint32_t gencnt; /* number of changes */ 995 uint64_t spare; 996 } ipfw_iface_info; 997 #define IPFW_IFFLAG_RESOLVED 0x01 /* Interface exists */ 998 999 typedef struct _ipfw_ta_info { 1000 char algoname[64]; /* algorithm name */ 1001 uint32_t type; /* lookup type */ 1002 uint32_t flags; 1003 uint32_t refcnt; 1004 uint32_t spare0; 1005 uint64_t spare1; 1006 } ipfw_ta_info; 1007 1008 typedef struct _ipfw_obj_header { 1009 ip_fw3_opheader opheader; /* IP_FW3 opcode */ 1010 uint32_t spare; 1011 uint16_t idx; /* object name index */ 1012 uint8_t objtype; /* object type */ 1013 uint8_t objsubtype; /* object subtype */ 1014 ipfw_obj_ntlv ntlv; /* object name tlv */ 1015 } ipfw_obj_header; 1016 1017 typedef struct _ipfw_obj_lheader { 1018 ip_fw3_opheader opheader; /* IP_FW3 opcode */ 1019 uint32_t set_mask; /* disabled set mask */ 1020 uint32_t count; /* Total objects count */ 1021 uint32_t size; /* Total size (incl. header) */ 1022 uint32_t objsize; /* Size of one object */ 1023 } ipfw_obj_lheader; 1024 1025 #define IPFW_CFG_GET_STATIC 0x01 1026 #define IPFW_CFG_GET_STATES 0x02 1027 #define IPFW_CFG_GET_COUNTERS 0x04 1028 typedef struct _ipfw_cfg_lheader { 1029 ip_fw3_opheader opheader; /* IP_FW3 opcode */ 1030 uint32_t set_mask; /* enabled set mask */ 1031 uint32_t spare; 1032 uint32_t flags; /* Request flags */ 1033 uint32_t size; /* neded buffer size */ 1034 uint32_t start_rule; 1035 uint32_t end_rule; 1036 } ipfw_cfg_lheader; 1037 1038 typedef struct _ipfw_range_header { 1039 ip_fw3_opheader opheader; /* IP_FW3 opcode */ 1040 ipfw_range_tlv range; 1041 } ipfw_range_header; 1042 1043 typedef struct _ipfw_sopt_info { 1044 uint16_t opcode; 1045 uint8_t version; 1046 uint8_t dir; 1047 uint8_t spare; 1048 uint64_t refcnt; 1049 } ipfw_sopt_info; 1050 1051 #endif /* _IPFW2_H */ 1052