ip_fw.h revision 126239
1230557Sjimharris/* 2230557Sjimharris * Copyright (c) 2002 Luigi Rizzo, Universita` di Pisa 3230557Sjimharris * 4230557Sjimharris * Redistribution and use in source and binary forms, with or without 5230557Sjimharris * modification, are permitted provided that the following conditions 6230557Sjimharris * are met: 7230557Sjimharris * 1. Redistributions of source code must retain the above copyright 8230557Sjimharris * notice, this list of conditions and the following disclaimer. 9230557Sjimharris * 2. Redistributions in binary form must reproduce the above copyright 10230557Sjimharris * notice, this list of conditions and the following disclaimer in the 11230557Sjimharris * documentation and/or other materials provided with the distribution. 12230557Sjimharris * 13230557Sjimharris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14230557Sjimharris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15230557Sjimharris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16230557Sjimharris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17230557Sjimharris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18230557Sjimharris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19230557Sjimharris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20230557Sjimharris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21230557Sjimharris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22230557Sjimharris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23230557Sjimharris * SUCH DAMAGE. 24230557Sjimharris * 25230557Sjimharris * $FreeBSD: head/sys/netinet/ip_fw.h 126239 2004-02-25 19:55:29Z mlaier $ 26230557Sjimharris */ 27230557Sjimharris 28230557Sjimharris#ifndef _IPFW2_H 29230557Sjimharris#define _IPFW2_H 30230557Sjimharris#define IPFW2 1 31230557Sjimharris/* 32230557Sjimharris * The kernel representation of ipfw rules is made of a list of 33230557Sjimharris * 'instructions' (for all practical purposes equivalent to BPF 34230557Sjimharris * instructions), which specify which fields of the packet 35230557Sjimharris * (or its metadata) should be analysed. 36230557Sjimharris * 37230557Sjimharris * Each instruction is stored in a structure which begins with 38230557Sjimharris * "ipfw_insn", and can contain extra fields depending on the 39230557Sjimharris * instruction type (listed below). 40230557Sjimharris * Note that the code is written so that individual instructions 41240965Sjimharris * have a size which is a multiple of 32 bits. This means that, if 42240965Sjimharris * such structures contain pointers or other 64-bit entities, 43230557Sjimharris * (there is just one instance now) they may end up unaligned on 44230557Sjimharris * 64-bit architectures, so the must be handled with care. 45230557Sjimharris * 46230557Sjimharris * "enum ipfw_opcodes" are the opcodes supported. We can have up 47230557Sjimharris * to 256 different opcodes. 48240965Sjimharris */ 49230557Sjimharris 50230557Sjimharrisenum ipfw_opcodes { /* arguments (4 byte each) */ 51230557Sjimharris O_NOP, 52230557Sjimharris 53230557Sjimharris O_IP_SRC, /* u32 = IP */ 54230557Sjimharris O_IP_SRC_MASK, /* ip = IP/mask */ 55230557Sjimharris O_IP_SRC_ME, /* none */ 56230557Sjimharris O_IP_SRC_SET, /* u32=base, arg1=len, bitmap */ 57230557Sjimharris 58230557Sjimharris O_IP_DST, /* u32 = IP */ 59230557Sjimharris O_IP_DST_MASK, /* ip = IP/mask */ 60230557Sjimharris O_IP_DST_ME, /* none */ 61230557Sjimharris O_IP_DST_SET, /* u32=base, arg1=len, bitmap */ 62230557Sjimharris 63230557Sjimharris O_IP_SRCPORT, /* (n)port list:mask 4 byte ea */ 64230557Sjimharris O_IP_DSTPORT, /* (n)port list:mask 4 byte ea */ 65230557Sjimharris O_PROTO, /* arg1=protocol */ 66230557Sjimharris 67230557Sjimharris O_MACADDR2, /* 2 mac addr:mask */ 68230557Sjimharris O_MAC_TYPE, /* same as srcport */ 69230557Sjimharris 70230557Sjimharris O_LAYER2, /* none */ 71230557Sjimharris O_IN, /* none */ 72230557Sjimharris O_FRAG, /* none */ 73230557Sjimharris 74230557Sjimharris O_RECV, /* none */ 75230557Sjimharris O_XMIT, /* none */ 76230557Sjimharris O_VIA, /* none */ 77230557Sjimharris 78230557Sjimharris O_IPOPT, /* arg1 = 2*u8 bitmap */ 79230557Sjimharris O_IPLEN, /* arg1 = len */ 80230557Sjimharris O_IPID, /* arg1 = id */ 81230557Sjimharris 82230557Sjimharris O_IPTOS, /* arg1 = id */ 83230557Sjimharris O_IPPRECEDENCE, /* arg1 = precedence << 5 */ 84230557Sjimharris O_IPTTL, /* arg1 = TTL */ 85230557Sjimharris 86230557Sjimharris O_IPVER, /* arg1 = version */ 87256125Skib O_UID, /* u32 = id */ 88230557Sjimharris O_GID, /* u32 = id */ 89230557Sjimharris O_ESTAB, /* none (tcp established) */ 90230557Sjimharris O_TCPFLAGS, /* arg1 = 2*u8 bitmap */ 91230557Sjimharris O_TCPWIN, /* arg1 = desired win */ 92230557Sjimharris O_TCPSEQ, /* u32 = desired seq. */ 93230557Sjimharris O_TCPACK, /* u32 = desired seq. */ 94230557Sjimharris O_ICMPTYPE, /* u32 = icmp bitmap */ 95230557Sjimharris O_TCPOPTS, /* arg1 = 2*u8 bitmap */ 96230557Sjimharris 97230557Sjimharris O_VERREVPATH, /* none */ 98230557Sjimharris 99230557Sjimharris O_PROBE_STATE, /* none */ 100230557Sjimharris O_KEEP_STATE, /* none */ 101230557Sjimharris O_LIMIT, /* ipfw_insn_limit */ 102230557Sjimharris O_LIMIT_PARENT, /* dyn_type, not an opcode. */ 103230557Sjimharris 104230557Sjimharris /* 105232225Ssbruno * These are really 'actions'. 106232225Ssbruno */ 107232225Ssbruno 108232225Ssbruno O_LOG, /* ipfw_insn_log */ 109232225Ssbruno O_PROB, /* u32 = match probability */ 110230557Sjimharris 111230557Sjimharris O_CHECK_STATE, /* none */ 112230557Sjimharris O_ACCEPT, /* none */ 113230557Sjimharris O_DENY, /* none */ 114230557Sjimharris O_REJECT, /* arg1=icmp arg (same as deny) */ 115230557Sjimharris O_COUNT, /* none */ 116230557Sjimharris O_SKIPTO, /* arg1=next rule number */ 117230557Sjimharris O_PIPE, /* arg1=pipe number */ 118230557Sjimharris O_QUEUE, /* arg1=queue number */ 119230557Sjimharris O_DIVERT, /* arg1=port number */ 120230557Sjimharris O_TEE, /* arg1=port number */ 121230557Sjimharris O_FORWARD_IP, /* fwd sockaddr */ 122230557Sjimharris O_FORWARD_MAC, /* fwd mac */ 123230557Sjimharris 124230557Sjimharris /* 125232225Ssbruno * More opcodes. 126230557Sjimharris */ 127230557Sjimharris O_IPSEC, /* has ipsec history */ 128230557Sjimharris 129230557Sjimharris O_LAST_OPCODE /* not an opcode! */ 130230557Sjimharris}; 131230557Sjimharris 132230557Sjimharris/* 133230557Sjimharris * Template for instructions. 134230557Sjimharris * 135230557Sjimharris * ipfw_insn is used for all instructions which require no operands, 136230557Sjimharris * a single 16-bit value (arg1), or a couple of 8-bit values. 137230557Sjimharris * 138230557Sjimharris * For other instructions which require different/larger arguments 139230557Sjimharris * we have derived structures, ipfw_insn_*. 140230557Sjimharris * 141230557Sjimharris * The size of the instruction (in 32-bit words) is in the low 142230557Sjimharris * 6 bits of "len". The 2 remaining bits are used to implement 143230557Sjimharris * NOT and OR on individual instructions. Given a type, you can 144230557Sjimharris * compute the length to be put in "len" using F_INSN_SIZE(t) 145230557Sjimharris * 146230557Sjimharris * F_NOT negates the match result of the instruction. 147230557Sjimharris * 148230557Sjimharris * F_OR is used to build or blocks. By default, instructions 149230557Sjimharris * are evaluated as part of a logical AND. An "or" block 150230557Sjimharris * { X or Y or Z } contains F_OR set in all but the last 151230557Sjimharris * instruction of the block. A match will cause the code 152230557Sjimharris * to skip past the last instruction of the block. 153230557Sjimharris * 154230557Sjimharris * NOTA BENE: in a couple of places we assume that 155230557Sjimharris * sizeof(ipfw_insn) == sizeof(u_int32_t) 156230557Sjimharris * this needs to be fixed. 157230557Sjimharris * 158230557Sjimharris */ 159230557Sjimharristypedef struct _ipfw_insn { /* template for instructions */ 160230557Sjimharris enum ipfw_opcodes opcode:8; 161230557Sjimharris u_int8_t len; /* numer of 32-byte words */ 162230557Sjimharris#define F_NOT 0x80 163230557Sjimharris#define F_OR 0x40 164230557Sjimharris#define F_LEN_MASK 0x3f 165230557Sjimharris#define F_LEN(cmd) ((cmd)->len & F_LEN_MASK) 166230557Sjimharris 167230557Sjimharris u_int16_t arg1; 168230557Sjimharris} ipfw_insn; 169230557Sjimharris 170230557Sjimharris/* 171230557Sjimharris * The F_INSN_SIZE(type) computes the size, in 4-byte words, of 172230557Sjimharris * a given type. 173230557Sjimharris */ 174230557Sjimharris#define F_INSN_SIZE(t) ((sizeof (t))/sizeof(u_int32_t)) 175230557Sjimharris 176230557Sjimharris/* 177230557Sjimharris * This is used to store an array of 16-bit entries (ports etc.) 178230557Sjimharris */ 179230557Sjimharristypedef struct _ipfw_insn_u16 { 180230557Sjimharris ipfw_insn o; 181230557Sjimharris u_int16_t ports[2]; /* there may be more */ 182230557Sjimharris} ipfw_insn_u16; 183230557Sjimharris 184230557Sjimharris/* 185230557Sjimharris * This is used to store an array of 32-bit entries 186230557Sjimharris * (uid, single IPv4 addresses etc.) 187240965Sjimharris */ 188230557Sjimharristypedef struct _ipfw_insn_u32 { 189230557Sjimharris ipfw_insn o; 190230557Sjimharris u_int32_t d[1]; /* one or more */ 191230557Sjimharris} ipfw_insn_u32; 192239545Sjimharris 193230557Sjimharris/* 194230557Sjimharris * This is used to store IP addr-mask pairs. 195230557Sjimharris */ 196230557Sjimharristypedef struct _ipfw_insn_ip { 197230557Sjimharris ipfw_insn o; 198230557Sjimharris struct in_addr addr; 199230557Sjimharris struct in_addr mask; 200230557Sjimharris} ipfw_insn_ip; 201230557Sjimharris 202230557Sjimharris/* 203230557Sjimharris * This is used to forward to a given address (ip). 204230557Sjimharris */ 205230557Sjimharristypedef struct _ipfw_insn_sa { 206230557Sjimharris ipfw_insn o; 207230557Sjimharris struct sockaddr_in sa; 208230557Sjimharris} ipfw_insn_sa; 209230557Sjimharris 210230557Sjimharris/* 211230557Sjimharris * This is used for MAC addr-mask pairs. 212230557Sjimharris */ 213230557Sjimharristypedef struct _ipfw_insn_mac { 214230557Sjimharris ipfw_insn o; 215230557Sjimharris u_char addr[12]; /* dst[6] + src[6] */ 216230557Sjimharris u_char mask[12]; /* dst[6] + src[6] */ 217230557Sjimharris} ipfw_insn_mac; 218230557Sjimharris 219230557Sjimharris/* 220230557Sjimharris * This is used for interface match rules (recv xx, xmit xx). 221230557Sjimharris */ 222230557Sjimharristypedef struct _ipfw_insn_if { 223230557Sjimharris ipfw_insn o; 224230557Sjimharris union { 225230557Sjimharris struct in_addr ip; 226239545Sjimharris int glob; 227241403Sjimharris } p; 228241403Sjimharris char name[IFNAMSIZ]; 229241403Sjimharris} ipfw_insn_if; 230240965Sjimharris 231241403Sjimharris/* 232241403Sjimharris * This is used for pipe and queue actions, which need to store 233241403Sjimharris * a single pointer (which can have different size on different 234241403Sjimharris * architectures. 235239545Sjimharris * Note that, because of previous instructions, pipe_ptr might 236239545Sjimharris * be unaligned in the overall structure, so it needs to be 237239545Sjimharris * manipulated with care. 238239545Sjimharris */ 239239545Sjimharristypedef struct _ipfw_insn_pipe { 240239545Sjimharris ipfw_insn o; 241230557Sjimharris void *pipe_ptr; /* XXX */ 242230557Sjimharris} ipfw_insn_pipe; 243230557Sjimharris 244230557Sjimharris/* 245230557Sjimharris * This is used for limit rules. 246230557Sjimharris */ 247230557Sjimharristypedef struct _ipfw_insn_limit { 248230557Sjimharris ipfw_insn o; 249230557Sjimharris u_int8_t _pad; 250230557Sjimharris u_int8_t limit_mask; /* combination of DYN_* below */ 251230557Sjimharris#define DYN_SRC_ADDR 0x1 252230557Sjimharris#define DYN_SRC_PORT 0x2 253230557Sjimharris#define DYN_DST_ADDR 0x4 254230557Sjimharris#define DYN_DST_PORT 0x8 255230557Sjimharris 256230557Sjimharris u_int16_t conn_limit; 257230557Sjimharris} ipfw_insn_limit; 258230557Sjimharris 259230557Sjimharris/* 260230557Sjimharris * This is used for log instructions. 261230557Sjimharris */ 262230557Sjimharristypedef struct _ipfw_insn_log { 263230557Sjimharris ipfw_insn o; 264230557Sjimharris u_int32_t max_log; /* how many do we log -- 0 = all */ 265230557Sjimharris u_int32_t log_left; /* how many left to log */ 266230557Sjimharris} ipfw_insn_log; 267230557Sjimharris 268230557Sjimharris/* 269230557Sjimharris * Here we have the structure representing an ipfw rule. 270230557Sjimharris * 271230557Sjimharris * It starts with a general area (with link fields and counters) 272230557Sjimharris * followed by an array of one or more instructions, which the code 273230557Sjimharris * accesses as an array of 32-bit values. 274230557Sjimharris * 275230557Sjimharris * Given a rule pointer r: 276230557Sjimharris * 277230557Sjimharris * r->cmd is the start of the first instruction. 278230557Sjimharris * ACTION_PTR(r) is the start of the first action (things to do 279230557Sjimharris * once a rule matched). 280230557Sjimharris * 281230557Sjimharris * When assembling instruction, remember the following: 282230557Sjimharris * 283230557Sjimharris * + if a rule has a "keep-state" (or "limit") option, then the 284230557Sjimharris * first instruction (at r->cmd) MUST BE an O_PROBE_STATE 285230557Sjimharris * + if a rule has a "log" option, then the first action 286230557Sjimharris * (at ACTION_PTR(r)) MUST be O_LOG 287230557Sjimharris * 288230557Sjimharris * NOTE: we use a simple linked list of rules because we never need 289230557Sjimharris * to delete a rule without scanning the list. We do not use 290230557Sjimharris * queue(3) macros for portability and readability. 291230557Sjimharris */ 292230557Sjimharris 293230557Sjimharrisstruct ip_fw { 294230557Sjimharris struct ip_fw *next; /* linked list of rules */ 295230557Sjimharris struct ip_fw *next_rule; /* ptr to next [skipto] rule */ 296230557Sjimharris /* 'next_rule' is used to pass up 'set_disable' status */ 297230557Sjimharris 298230557Sjimharris u_int16_t act_ofs; /* offset of action in 32-bit units */ 299230557Sjimharris u_int16_t cmd_len; /* # of 32-bit words in cmd */ 300230557Sjimharris u_int16_t rulenum; /* rule number */ 301230557Sjimharris u_int8_t set; /* rule set (0..31) */ 302230557Sjimharris#define RESVD_SET 31 /* set for default and persistent rules */ 303230557Sjimharris u_int8_t _pad; /* padding */ 304230557Sjimharris 305230557Sjimharris /* These fields are present in all rules. */ 306230557Sjimharris u_int64_t pcnt; /* Packet counter */ 307230557Sjimharris u_int64_t bcnt; /* Byte counter */ 308230557Sjimharris u_int32_t timestamp; /* tv_sec of last match */ 309230557Sjimharris 310230557Sjimharris ipfw_insn cmd[1]; /* storage for commands */ 311230557Sjimharris}; 312230557Sjimharris 313230557Sjimharris#define ACTION_PTR(rule) \ 314230557Sjimharris (ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) ) 315230557Sjimharris 316230557Sjimharris#define RULESIZE(rule) (sizeof(struct ip_fw) + \ 317230557Sjimharris ((struct ip_fw *)(rule))->cmd_len * 4 - 4) 318230557Sjimharris 319230557Sjimharris/* 320230557Sjimharris * This structure is used as a flow mask and a flow id for various 321230557Sjimharris * parts of the code. 322230557Sjimharris */ 323230557Sjimharrisstruct ipfw_flow_id { 324230557Sjimharris u_int32_t dst_ip; 325230557Sjimharris u_int32_t src_ip; 326230557Sjimharris u_int16_t dst_port; 327230557Sjimharris u_int16_t src_port; 328230557Sjimharris u_int8_t proto; 329230557Sjimharris u_int8_t flags; /* protocol-specific flags */ 330230557Sjimharris}; 331230557Sjimharris 332230557Sjimharris/* 333230557Sjimharris * Dynamic ipfw rule. 334230557Sjimharris */ 335230557Sjimharristypedef struct _ipfw_dyn_rule ipfw_dyn_rule; 336230557Sjimharris 337230557Sjimharrisstruct _ipfw_dyn_rule { 338230557Sjimharris ipfw_dyn_rule *next; /* linked list of rules. */ 339230557Sjimharris struct ip_fw *rule; /* pointer to rule */ 340230557Sjimharris /* 'rule' is used to pass up the rule number (from the parent) */ 341230557Sjimharris 342230557Sjimharris ipfw_dyn_rule *parent; /* pointer to parent rule */ 343230557Sjimharris u_int64_t pcnt; /* packet match counter */ 344230557Sjimharris u_int64_t bcnt; /* byte match counter */ 345230557Sjimharris struct ipfw_flow_id id; /* (masked) flow id */ 346230557Sjimharris u_int32_t expire; /* expire time */ 347230557Sjimharris u_int32_t bucket; /* which bucket in hash table */ 348230557Sjimharris u_int32_t state; /* state of this rule (typically a 349230557Sjimharris * combination of TCP flags) 350230557Sjimharris */ 351230557Sjimharris u_int32_t ack_fwd; /* most recent ACKs in forward */ 352230557Sjimharris u_int32_t ack_rev; /* and reverse directions (used */ 353230557Sjimharris /* to generate keepalives) */ 354230557Sjimharris u_int16_t dyn_type; /* rule type */ 355230557Sjimharris u_int16_t count; /* refcount */ 356230557Sjimharris}; 357230557Sjimharris 358230557Sjimharris/* 359230557Sjimharris * Definitions for IP option names. 360230557Sjimharris */ 361230557Sjimharris#define IP_FW_IPOPT_LSRR 0x01 362230557Sjimharris#define IP_FW_IPOPT_SSRR 0x02 363230557Sjimharris#define IP_FW_IPOPT_RR 0x04 364230557Sjimharris#define IP_FW_IPOPT_TS 0x08 365230557Sjimharris 366230557Sjimharris/* 367230557Sjimharris * Definitions for TCP option names. 368230557Sjimharris */ 369230557Sjimharris#define IP_FW_TCPOPT_MSS 0x01 370230557Sjimharris#define IP_FW_TCPOPT_WINDOW 0x02 371230557Sjimharris#define IP_FW_TCPOPT_SACK 0x04 372230557Sjimharris#define IP_FW_TCPOPT_TS 0x08 373230557Sjimharris#define IP_FW_TCPOPT_CC 0x10 374230557Sjimharris 375230557Sjimharris#define ICMP_REJECT_RST 0x100 /* fake ICMP code (send a TCP RST) */ 376230557Sjimharris 377230557Sjimharris/* 378230557Sjimharris * Main firewall chains definitions and global var's definitions. 379230557Sjimharris */ 380230557Sjimharris#ifdef _KERNEL 381230557Sjimharris 382230557Sjimharris#define IP_FW_PORT_DYNT_FLAG 0x10000 383230557Sjimharris#define IP_FW_PORT_TEE_FLAG 0x20000 384230557Sjimharris#define IP_FW_PORT_DENY_FLAG 0x40000 385230557Sjimharris 386230557Sjimharris/* 387230557Sjimharris * Arguments for calling ipfw_chk() and dummynet_io(). We put them 388230557Sjimharris * all into a structure because this way it is easier and more 389230557Sjimharris * efficient to pass variables around and extend the interface. 390230557Sjimharris */ 391230557Sjimharrisstruct ip_fw_args { 392230557Sjimharris struct mbuf *m; /* the mbuf chain */ 393230557Sjimharris struct ifnet *oif; /* output interface */ 394230557Sjimharris struct sockaddr_in *next_hop; /* forward address */ 395230557Sjimharris struct ip_fw *rule; /* matching rule */ 396230557Sjimharris struct ether_header *eh; /* for bridged packets */ 397230557Sjimharris 398230557Sjimharris struct route *ro; /* for dummynet */ 399230557Sjimharris struct sockaddr_in *dst; /* for dummynet */ 400230557Sjimharris int flags; /* for dummynet */ 401230557Sjimharris 402230557Sjimharris struct ipfw_flow_id f_id; /* grabbed from IP header */ 403230557Sjimharris u_int32_t retval; 404230557Sjimharris}; 405230557Sjimharris 406230557Sjimharris/* 407230557Sjimharris * Function definitions. 408230557Sjimharris */ 409230557Sjimharris 410230557Sjimharris/* Firewall hooks */ 411230557Sjimharrisstruct sockopt; 412230557Sjimharrisstruct dn_flow_set; 413230557Sjimharris 414230557Sjimharrisvoid flush_pipe_ptrs(struct dn_flow_set *match); /* used by dummynet */ 415230557Sjimharris 416230557Sjimharristypedef int ip_fw_chk_t (struct ip_fw_args *args); 417230557Sjimharristypedef int ip_fw_ctl_t (struct sockopt *); 418230557Sjimharrisextern ip_fw_chk_t *ip_fw_chk_ptr; 419230557Sjimharrisextern ip_fw_ctl_t *ip_fw_ctl_ptr; 420230557Sjimharrisextern int fw_one_pass; 421230557Sjimharrisextern int fw_enable; 422230557Sjimharris#define IPFW_LOADED (ip_fw_chk_ptr != NULL) 423230557Sjimharris#endif /* _KERNEL */ 424230557Sjimharris 425230557Sjimharris#endif /* _IPFW2_H */ 426230557Sjimharris