ip_fw.h revision 136073
1129203Scognet/* 2129203Scognet * Copyright (c) 2002 Luigi Rizzo, Universita` di Pisa 3129203Scognet * 4129203Scognet * Redistribution and use in source and binary forms, with or without 5129203Scognet * modification, are permitted provided that the following conditions 6129203Scognet * are met: 7129203Scognet * 1. Redistributions of source code must retain the above copyright 8129203Scognet * notice, this list of conditions and the following disclaimer. 9129203Scognet * 2. Redistributions in binary form must reproduce the above copyright 10129203Scognet * notice, this list of conditions and the following disclaimer in the 11129203Scognet * documentation and/or other materials provided with the distribution. 12129203Scognet * 13129203Scognet * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14129203Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15129203Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16129203Scognet * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17129203Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18129203Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19129203Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20129203Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21129203Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22129203Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 * $FreeBSD: head/sys/netinet/ip_fw.h 136073 2004-10-03 00:26:35Z green $ 26 */ 27 28#ifndef _IPFW2_H 29#define _IPFW2_H 30#define IPFW2 1 31 32/* 33 * The kernel representation of ipfw rules is made of a list of 34 * 'instructions' (for all practical purposes equivalent to BPF 35 * instructions), which specify which fields of the packet 36 * (or its metadata) should be analysed. 37 * 38 * Each instruction is stored in a structure which begins with 39 * "ipfw_insn", and can contain extra fields depending on the 40 * instruction type (listed below). 41 * Note that the code is written so that individual instructions 42 * have a size which is a multiple of 32 bits. This means that, if 43 * such structures contain pointers or other 64-bit entities, 44 * (there is just one instance now) they may end up unaligned on 45 * 64-bit architectures, so the must be handled with care. 46 * 47 * "enum ipfw_opcodes" are the opcodes supported. We can have up 48 * to 256 different opcodes. When adding new opcodes, they should 49 * be appended to the end of the opcode list before O_LAST_OPCODE, 50 * this will prevent the ABI from being broken, otherwise users 51 * will have to recompile ipfw(8) when they update the kernel. 52 */ 53 54enum ipfw_opcodes { /* arguments (4 byte each) */ 55 O_NOP, 56 57 O_IP_SRC, /* u32 = IP */ 58 O_IP_SRC_MASK, /* ip = IP/mask */ 59 O_IP_SRC_ME, /* none */ 60 O_IP_SRC_SET, /* u32=base, arg1=len, bitmap */ 61 62 O_IP_DST, /* u32 = IP */ 63 O_IP_DST_MASK, /* ip = IP/mask */ 64 O_IP_DST_ME, /* none */ 65 O_IP_DST_SET, /* u32=base, arg1=len, bitmap */ 66 67 O_IP_SRCPORT, /* (n)port list:mask 4 byte ea */ 68 O_IP_DSTPORT, /* (n)port list:mask 4 byte ea */ 69 O_PROTO, /* arg1=protocol */ 70 71 O_MACADDR2, /* 2 mac addr:mask */ 72 O_MAC_TYPE, /* same as srcport */ 73 74 O_LAYER2, /* none */ 75 O_IN, /* none */ 76 O_FRAG, /* none */ 77 78 O_RECV, /* none */ 79 O_XMIT, /* none */ 80 O_VIA, /* none */ 81 82 O_IPOPT, /* arg1 = 2*u8 bitmap */ 83 O_IPLEN, /* arg1 = len */ 84 O_IPID, /* arg1 = id */ 85 86 O_IPTOS, /* arg1 = id */ 87 O_IPPRECEDENCE, /* arg1 = precedence << 5 */ 88 O_IPTTL, /* arg1 = TTL */ 89 90 O_IPVER, /* arg1 = version */ 91 O_UID, /* u32 = id */ 92 O_GID, /* u32 = id */ 93 O_ESTAB, /* none (tcp established) */ 94 O_TCPFLAGS, /* arg1 = 2*u8 bitmap */ 95 O_TCPWIN, /* arg1 = desired win */ 96 O_TCPSEQ, /* u32 = desired seq. */ 97 O_TCPACK, /* u32 = desired seq. */ 98 O_ICMPTYPE, /* u32 = icmp bitmap */ 99 O_TCPOPTS, /* arg1 = 2*u8 bitmap */ 100 101 O_VERREVPATH, /* none */ 102 O_VERSRCREACH, /* none */ 103 104 O_PROBE_STATE, /* none */ 105 O_KEEP_STATE, /* none */ 106 O_LIMIT, /* ipfw_insn_limit */ 107 O_LIMIT_PARENT, /* dyn_type, not an opcode. */ 108 109 /* 110 * These are really 'actions'. 111 */ 112 113 O_LOG, /* ipfw_insn_log */ 114 O_PROB, /* u32 = match probability */ 115 116 O_CHECK_STATE, /* none */ 117 O_ACCEPT, /* none */ 118 O_DENY, /* none */ 119 O_REJECT, /* arg1=icmp arg (same as deny) */ 120 O_COUNT, /* none */ 121 O_SKIPTO, /* arg1=next rule number */ 122 O_PIPE, /* arg1=pipe number */ 123 O_QUEUE, /* arg1=queue number */ 124 O_DIVERT, /* arg1=port number */ 125 O_TEE, /* arg1=port number */ 126 O_FORWARD_IP, /* fwd sockaddr */ 127 O_FORWARD_MAC, /* fwd mac */ 128 129 /* 130 * More opcodes. 131 */ 132 O_IPSEC, /* has ipsec history */ 133 O_IP_SRC_LOOKUP, /* arg1=table number, u32=value */ 134 O_IP_DST_LOOKUP, /* arg1=table number, u32=value */ 135 O_ANTISPOOF, /* none */ 136 O_JAIL, /* u32 = id */ 137 O_ALTQ, /* u32 = altq classif. qid */ 138 O_DIVERTED, /* arg1=bitmap (1:loop, 2:out) */ 139 140 O_LAST_OPCODE /* not an opcode! */ 141}; 142 143/* 144 * Template for instructions. 145 * 146 * ipfw_insn is used for all instructions which require no operands, 147 * a single 16-bit value (arg1), or a couple of 8-bit values. 148 * 149 * For other instructions which require different/larger arguments 150 * we have derived structures, ipfw_insn_*. 151 * 152 * The size of the instruction (in 32-bit words) is in the low 153 * 6 bits of "len". The 2 remaining bits are used to implement 154 * NOT and OR on individual instructions. Given a type, you can 155 * compute the length to be put in "len" using F_INSN_SIZE(t) 156 * 157 * F_NOT negates the match result of the instruction. 158 * 159 * F_OR is used to build or blocks. By default, instructions 160 * are evaluated as part of a logical AND. An "or" block 161 * { X or Y or Z } contains F_OR set in all but the last 162 * instruction of the block. A match will cause the code 163 * to skip past the last instruction of the block. 164 * 165 * NOTA BENE: in a couple of places we assume that 166 * sizeof(ipfw_insn) == sizeof(u_int32_t) 167 * this needs to be fixed. 168 * 169 */ 170typedef struct _ipfw_insn { /* template for instructions */ 171 enum ipfw_opcodes opcode:8; 172 u_int8_t len; /* numer of 32-byte words */ 173#define F_NOT 0x80 174#define F_OR 0x40 175#define F_LEN_MASK 0x3f 176#define F_LEN(cmd) ((cmd)->len & F_LEN_MASK) 177 178 u_int16_t arg1; 179} ipfw_insn; 180 181/* 182 * The F_INSN_SIZE(type) computes the size, in 4-byte words, of 183 * a given type. 184 */ 185#define F_INSN_SIZE(t) ((sizeof (t))/sizeof(u_int32_t)) 186 187/* 188 * This is used to store an array of 16-bit entries (ports etc.) 189 */ 190typedef struct _ipfw_insn_u16 { 191 ipfw_insn o; 192 u_int16_t ports[2]; /* there may be more */ 193} ipfw_insn_u16; 194 195/* 196 * This is used to store an array of 32-bit entries 197 * (uid, single IPv4 addresses etc.) 198 */ 199typedef struct _ipfw_insn_u32 { 200 ipfw_insn o; 201 u_int32_t d[1]; /* one or more */ 202} ipfw_insn_u32; 203 204/* 205 * This is used to store IP addr-mask pairs. 206 */ 207typedef struct _ipfw_insn_ip { 208 ipfw_insn o; 209 struct in_addr addr; 210 struct in_addr mask; 211} ipfw_insn_ip; 212 213/* 214 * This is used to forward to a given address (ip). 215 */ 216typedef struct _ipfw_insn_sa { 217 ipfw_insn o; 218 struct sockaddr_in sa; 219} ipfw_insn_sa; 220 221/* 222 * This is used for MAC addr-mask pairs. 223 */ 224typedef struct _ipfw_insn_mac { 225 ipfw_insn o; 226 u_char addr[12]; /* dst[6] + src[6] */ 227 u_char mask[12]; /* dst[6] + src[6] */ 228} ipfw_insn_mac; 229 230/* 231 * This is used for interface match rules (recv xx, xmit xx). 232 */ 233typedef struct _ipfw_insn_if { 234 ipfw_insn o; 235 union { 236 struct in_addr ip; 237 int glob; 238 } p; 239 char name[IFNAMSIZ]; 240} ipfw_insn_if; 241 242/* 243 * This is used for pipe and queue actions, which need to store 244 * a single pointer (which can have different size on different 245 * architectures. 246 * Note that, because of previous instructions, pipe_ptr might 247 * be unaligned in the overall structure, so it needs to be 248 * manipulated with care. 249 */ 250typedef struct _ipfw_insn_pipe { 251 ipfw_insn o; 252 void *pipe_ptr; /* XXX */ 253} ipfw_insn_pipe; 254 255/* 256 * This is used for storing an altq queue id number. 257 */ 258typedef struct _ipfw_insn_altq { 259 ipfw_insn o; 260 u_int32_t qid; 261} ipfw_insn_altq; 262 263/* 264 * This is used for limit rules. 265 */ 266typedef struct _ipfw_insn_limit { 267 ipfw_insn o; 268 u_int8_t _pad; 269 u_int8_t limit_mask; /* combination of DYN_* below */ 270#define DYN_SRC_ADDR 0x1 271#define DYN_SRC_PORT 0x2 272#define DYN_DST_ADDR 0x4 273#define DYN_DST_PORT 0x8 274 275 u_int16_t conn_limit; 276} ipfw_insn_limit; 277 278/* 279 * This is used for log instructions. 280 */ 281typedef struct _ipfw_insn_log { 282 ipfw_insn o; 283 u_int32_t max_log; /* how many do we log -- 0 = all */ 284 u_int32_t log_left; /* how many left to log */ 285} ipfw_insn_log; 286 287/* 288 * Here we have the structure representing an ipfw rule. 289 * 290 * It starts with a general area (with link fields and counters) 291 * followed by an array of one or more instructions, which the code 292 * accesses as an array of 32-bit values. 293 * 294 * Given a rule pointer r: 295 * 296 * r->cmd is the start of the first instruction. 297 * ACTION_PTR(r) is the start of the first action (things to do 298 * once a rule matched). 299 * 300 * When assembling instruction, remember the following: 301 * 302 * + if a rule has a "keep-state" (or "limit") option, then the 303 * first instruction (at r->cmd) MUST BE an O_PROBE_STATE 304 * + if a rule has a "log" option, then the first action 305 * (at ACTION_PTR(r)) MUST be O_LOG 306 * + if a rule has an "altq" option, it comes after "log" 307 * 308 * NOTE: we use a simple linked list of rules because we never need 309 * to delete a rule without scanning the list. We do not use 310 * queue(3) macros for portability and readability. 311 */ 312 313struct ip_fw { 314 struct ip_fw *next; /* linked list of rules */ 315 struct ip_fw *next_rule; /* ptr to next [skipto] rule */ 316 /* 'next_rule' is used to pass up 'set_disable' status */ 317 318 u_int16_t act_ofs; /* offset of action in 32-bit units */ 319 u_int16_t cmd_len; /* # of 32-bit words in cmd */ 320 u_int16_t rulenum; /* rule number */ 321 u_int8_t set; /* rule set (0..31) */ 322#define RESVD_SET 31 /* set for default and persistent rules */ 323 u_int8_t _pad; /* padding */ 324 325 /* These fields are present in all rules. */ 326 u_int64_t pcnt; /* Packet counter */ 327 u_int64_t bcnt; /* Byte counter */ 328 u_int32_t timestamp; /* tv_sec of last match */ 329 330 ipfw_insn cmd[1]; /* storage for commands */ 331}; 332 333#define ACTION_PTR(rule) \ 334 (ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) ) 335 336#define RULESIZE(rule) (sizeof(struct ip_fw) + \ 337 ((struct ip_fw *)(rule))->cmd_len * 4 - 4) 338 339/* 340 * This structure is used as a flow mask and a flow id for various 341 * parts of the code. 342 */ 343struct ipfw_flow_id { 344 u_int32_t dst_ip; 345 u_int32_t src_ip; 346 u_int16_t dst_port; 347 u_int16_t src_port; 348 u_int8_t proto; 349 u_int8_t flags; /* protocol-specific flags */ 350}; 351 352/* 353 * Dynamic ipfw rule. 354 */ 355typedef struct _ipfw_dyn_rule ipfw_dyn_rule; 356 357struct _ipfw_dyn_rule { 358 ipfw_dyn_rule *next; /* linked list of rules. */ 359 struct ip_fw *rule; /* pointer to rule */ 360 /* 'rule' is used to pass up the rule number (from the parent) */ 361 362 ipfw_dyn_rule *parent; /* pointer to parent rule */ 363 u_int64_t pcnt; /* packet match counter */ 364 u_int64_t bcnt; /* byte match counter */ 365 struct ipfw_flow_id id; /* (masked) flow id */ 366 u_int32_t expire; /* expire time */ 367 u_int32_t bucket; /* which bucket in hash table */ 368 u_int32_t state; /* state of this rule (typically a 369 * combination of TCP flags) 370 */ 371 u_int32_t ack_fwd; /* most recent ACKs in forward */ 372 u_int32_t ack_rev; /* and reverse directions (used */ 373 /* to generate keepalives) */ 374 u_int16_t dyn_type; /* rule type */ 375 u_int16_t count; /* refcount */ 376}; 377 378/* 379 * Definitions for IP option names. 380 */ 381#define IP_FW_IPOPT_LSRR 0x01 382#define IP_FW_IPOPT_SSRR 0x02 383#define IP_FW_IPOPT_RR 0x04 384#define IP_FW_IPOPT_TS 0x08 385 386/* 387 * Definitions for TCP option names. 388 */ 389#define IP_FW_TCPOPT_MSS 0x01 390#define IP_FW_TCPOPT_WINDOW 0x02 391#define IP_FW_TCPOPT_SACK 0x04 392#define IP_FW_TCPOPT_TS 0x08 393#define IP_FW_TCPOPT_CC 0x10 394 395#define ICMP_REJECT_RST 0x100 /* fake ICMP code (send a TCP RST) */ 396 397/* 398 * These are used for lookup tables. 399 */ 400typedef struct _ipfw_table_entry { 401 in_addr_t addr; /* network address */ 402 u_int32_t value; /* value */ 403 u_int16_t tbl; /* table number */ 404 u_int8_t masklen; /* mask length */ 405} ipfw_table_entry; 406 407typedef struct _ipfw_table { 408 u_int32_t size; /* size of entries in bytes */ 409 u_int32_t cnt; /* # of entries */ 410 u_int16_t tbl; /* table number */ 411 ipfw_table_entry ent[0]; /* entries */ 412} ipfw_table; 413 414/* 415 * Main firewall chains definitions and global var's definitions. 416 */ 417#ifdef _KERNEL 418 419#define IP_FW_PORT_DYNT_FLAG 0x00010000 420#define IP_FW_PORT_TEE_FLAG 0x00020000 421#define IP_FW_PORT_DENY_FLAG 0x00040000 422#define IP_FW_DIVERT_LOOPBACK_FLAG 0x00080000 423#define IP_FW_DIVERT_OUTPUT_FLAG 0x00100000 424 425/* 426 * Arguments for calling ipfw_chk() and dummynet_io(). We put them 427 * all into a structure because this way it is easier and more 428 * efficient to pass variables around and extend the interface. 429 */ 430struct ip_fw_args { 431 struct mbuf *m; /* the mbuf chain */ 432 struct ifnet *oif; /* output interface */ 433 struct sockaddr_in *next_hop; /* forward address */ 434 struct ip_fw *rule; /* matching rule */ 435 struct ether_header *eh; /* for bridged packets */ 436 437 int flags; /* for dummynet */ 438 439 struct ipfw_flow_id f_id; /* grabbed from IP header */ 440 u_int32_t retval; 441 struct inpcb *inp; 442}; 443 444/* 445 * Function definitions. 446 */ 447 448/* Firewall hooks */ 449struct sockopt; 450struct dn_flow_set; 451 452int ipfw_check_in(void *, struct mbuf **, struct ifnet *, int, struct inpcb *inp); 453int ipfw_check_out(void *, struct mbuf **, struct ifnet *, int, struct inpcb *inp); 454 455int ipfw_chk(struct ip_fw_args *); 456 457int ipfw_init(void); 458void ipfw_destroy(void); 459 460void flush_pipe_ptrs(struct dn_flow_set *match); /* used by dummynet */ 461 462typedef int ip_fw_ctl_t(struct sockopt *); 463extern ip_fw_ctl_t *ip_fw_ctl_ptr; 464extern int fw_one_pass; 465extern int fw_enable; 466 467/* For kernel ipfw_ether and ipfw_bridge. */ 468typedef int ip_fw_chk_t(struct ip_fw_args *args); 469extern ip_fw_chk_t *ip_fw_chk_ptr; 470#define IPFW_LOADED (ip_fw_chk_ptr != NULL) 471 472#endif /* _KERNEL */ 473#endif /* _IPFW2_H */ 474