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