]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/ip_fw.h
* Permit limiting number of items in table.
[FreeBSD/FreeBSD.git] / sys / netinet / ip_fw.h
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  * Default number of ipfw tables.
44  */
45 #define IPFW_TABLES_MAX         65535
46 #define IPFW_TABLES_DEFAULT     128
47
48 /*
49  * Most commands (queue, pipe, tag, untag, limit...) can have a 16-bit
50  * argument between 1 and 65534. The value 0 is unused, the value
51  * 65535 (IP_FW_TABLEARG) is used to represent 'tablearg', i.e. the
52  * can be 1..65534, or 65535 to indicate the use of a 'tablearg'
53  * result of the most recent table() lookup.
54  * Note that 16bit is only a historical limit, resulting from
55  * the use of a 16-bit fields for that value. In reality, we can have
56  * 2^32 pipes, queues, tag values and so on, and use 0 as a tablearg.
57  */
58 #define IPFW_ARG_MIN            1
59 #define IPFW_ARG_MAX            65534
60 #define IP_FW_TABLEARG          65535   /* XXX should use 0 */
61
62 /*
63  * Number of entries in the call stack of the call/return commands.
64  * Call stack currently is an uint16_t array with rule numbers.
65  */
66 #define IPFW_CALLSTACK_SIZE     16
67
68 /* IP_FW3 header/opcodes */
69 typedef struct _ip_fw3_opheader {
70         uint16_t opcode;        /* Operation opcode */
71         uint16_t version;       /* Opcode version */
72         uint16_t reserved[2];   /* Align to 64-bit boundary */
73 } ip_fw3_opheader;
74
75
76 /* IPFW extented tables support */
77 #define IP_FW_TABLE_XADD        86      /* add entry */
78 #define IP_FW_TABLE_XDEL        87      /* delete entry */
79 #define IP_FW_TABLE_XGETSIZE    88      /* get table size (deprecated) */
80 #define IP_FW_TABLE_XLIST       89      /* list table contents */
81 #define IP_FW_TABLE_XDESTROY    90      /* destroy table */
82 #define IP_FW_TABLES_XGETSIZE   91      /* get size for tables list */
83 #define IP_FW_TABLES_XLIST      92      /* list all tables  */
84 #define IP_FW_TABLE_XINFO       93      /* request info for one table */
85 #define IP_FW_TABLE_XFLUSH      94      /* flush table data */
86 #define IP_FW_TABLE_XCREATE     95      /* create new table  */
87 //#define       IP_FW_TABLE_XMODIFY     96      /* modify existing table */
88 #define IP_FW_XGET              97      /* Retrieve configuration */
89 #define IP_FW_XADD              98      /* add entry */
90 #define IP_FW_TABLE_XFIND       99      /* finds an entry */
91 #define IP_FW_XIFLIST           100     /* list tracked interfaces */
92 #define IP_FW_TABLES_ALIST      101     /* list table algorithms */
93
94 /*
95  * Usage guidelines:
96  *
97  * IP_FW_TABLE_XLIST(ver 1): Dumps all table data
98  *   Request(getsockopt): [ ipfw_obj_lheader ], size = ipfw_xtable_info.size
99  *   Reply: [ ipfw_obj_lheader ipfw_xtable_info ipfw_table_xentry x N ]
100  *
101  * IP_FW_TABLE_XDESTROY: Destroys given table
102  *   Request(setsockopt): [ ipfw_obj_header ]
103  *
104  * IP_FW_TABLES_XGETSIZE: Get buffer size needed to list info for all tables.
105  *   Request(getsockopt): [ empty ], size = sizeof(ipfw_obj_lheader)
106  *   Reply: [ ipfw_obj_lheader ]
107  *
108  * IP_FW_TABLES_XLIST: Lists all tables currently available in kernel.
109  *   Request(getsockopt): [ ipfw_obj_lheader ], size = ipfw_obj_lheader.size
110  *   Reply: [ ipfw_obj_lheader ipfw_xtable_info x N ]
111  *
112  * IP_FW_TABLE_XINFO: Store table info to buffer.
113  *   Request(getsockopt): [ ipfw_obj_header ipfw_xtable_info(empty)]
114  *   Reply: [ ipfw_obj_header ipfw_xtable_info ]
115  *
116  * IP_FW_TABLE_XFLUSH: Removes all data from given table leaving type etc..
117  *   Request(setsockopt): [ ipfw_obj_header ]
118  */
119
120 /*
121  * The kernel representation of ipfw rules is made of a list of
122  * 'instructions' (for all practical purposes equivalent to BPF
123  * instructions), which specify which fields of the packet
124  * (or its metadata) should be analysed.
125  *
126  * Each instruction is stored in a structure which begins with
127  * "ipfw_insn", and can contain extra fields depending on the
128  * instruction type (listed below).
129  * Note that the code is written so that individual instructions
130  * have a size which is a multiple of 32 bits. This means that, if
131  * such structures contain pointers or other 64-bit entities,
132  * (there is just one instance now) they may end up unaligned on
133  * 64-bit architectures, so the must be handled with care.
134  *
135  * "enum ipfw_opcodes" are the opcodes supported. We can have up
136  * to 256 different opcodes. When adding new opcodes, they should
137  * be appended to the end of the opcode list before O_LAST_OPCODE,
138  * this will prevent the ABI from being broken, otherwise users
139  * will have to recompile ipfw(8) when they update the kernel.
140  */
141
142 enum ipfw_opcodes {             /* arguments (4 byte each)      */
143         O_NOP,
144
145         O_IP_SRC,               /* u32 = IP                     */
146         O_IP_SRC_MASK,          /* ip = IP/mask                 */
147         O_IP_SRC_ME,            /* none                         */
148         O_IP_SRC_SET,           /* u32=base, arg1=len, bitmap   */
149
150         O_IP_DST,               /* u32 = IP                     */
151         O_IP_DST_MASK,          /* ip = IP/mask                 */
152         O_IP_DST_ME,            /* none                         */
153         O_IP_DST_SET,           /* u32=base, arg1=len, bitmap   */
154
155         O_IP_SRCPORT,           /* (n)port list:mask 4 byte ea  */
156         O_IP_DSTPORT,           /* (n)port list:mask 4 byte ea  */
157         O_PROTO,                /* arg1=protocol                */
158
159         O_MACADDR2,             /* 2 mac addr:mask              */
160         O_MAC_TYPE,             /* same as srcport              */
161
162         O_LAYER2,               /* none                         */
163         O_IN,                   /* none                         */
164         O_FRAG,                 /* none                         */
165
166         O_RECV,                 /* none                         */
167         O_XMIT,                 /* none                         */
168         O_VIA,                  /* none                         */
169
170         O_IPOPT,                /* arg1 = 2*u8 bitmap           */
171         O_IPLEN,                /* arg1 = len                   */
172         O_IPID,                 /* arg1 = id                    */
173
174         O_IPTOS,                /* arg1 = id                    */
175         O_IPPRECEDENCE,         /* arg1 = precedence << 5       */
176         O_IPTTL,                /* arg1 = TTL                   */
177
178         O_IPVER,                /* arg1 = version               */
179         O_UID,                  /* u32 = id                     */
180         O_GID,                  /* u32 = id                     */
181         O_ESTAB,                /* none (tcp established)       */
182         O_TCPFLAGS,             /* arg1 = 2*u8 bitmap           */
183         O_TCPWIN,               /* arg1 = desired win           */
184         O_TCPSEQ,               /* u32 = desired seq.           */
185         O_TCPACK,               /* u32 = desired seq.           */
186         O_ICMPTYPE,             /* u32 = icmp bitmap            */
187         O_TCPOPTS,              /* arg1 = 2*u8 bitmap           */
188
189         O_VERREVPATH,           /* none                         */
190         O_VERSRCREACH,          /* none                         */
191
192         O_PROBE_STATE,          /* none                         */
193         O_KEEP_STATE,           /* none                         */
194         O_LIMIT,                /* ipfw_insn_limit              */
195         O_LIMIT_PARENT,         /* dyn_type, not an opcode.     */
196
197         /*
198          * These are really 'actions'.
199          */
200
201         O_LOG,                  /* ipfw_insn_log                */
202         O_PROB,                 /* u32 = match probability      */
203
204         O_CHECK_STATE,          /* none                         */
205         O_ACCEPT,               /* none                         */
206         O_DENY,                 /* none                         */
207         O_REJECT,               /* arg1=icmp arg (same as deny) */
208         O_COUNT,                /* none                         */
209         O_SKIPTO,               /* arg1=next rule number        */
210         O_PIPE,                 /* arg1=pipe number             */
211         O_QUEUE,                /* arg1=queue number            */
212         O_DIVERT,               /* arg1=port number             */
213         O_TEE,                  /* arg1=port number             */
214         O_FORWARD_IP,           /* fwd sockaddr                 */
215         O_FORWARD_MAC,          /* fwd mac                      */
216         O_NAT,                  /* nope                         */
217         O_REASS,                /* none                         */
218         
219         /*
220          * More opcodes.
221          */
222         O_IPSEC,                /* has ipsec history            */
223         O_IP_SRC_LOOKUP,        /* arg1=table number, u32=value */
224         O_IP_DST_LOOKUP,        /* arg1=table number, u32=value */
225         O_ANTISPOOF,            /* none                         */
226         O_JAIL,                 /* u32 = id                     */
227         O_ALTQ,                 /* u32 = altq classif. qid      */
228         O_DIVERTED,             /* arg1=bitmap (1:loop, 2:out)  */
229         O_TCPDATALEN,           /* arg1 = tcp data len          */
230         O_IP6_SRC,              /* address without mask         */
231         O_IP6_SRC_ME,           /* my addresses                 */
232         O_IP6_SRC_MASK,         /* address with the mask        */
233         O_IP6_DST,
234         O_IP6_DST_ME,
235         O_IP6_DST_MASK,
236         O_FLOW6ID,              /* for flow id tag in the ipv6 pkt */
237         O_ICMP6TYPE,            /* icmp6 packet type filtering  */
238         O_EXT_HDR,              /* filtering for ipv6 extension header */
239         O_IP6,
240
241         /*
242          * actions for ng_ipfw
243          */
244         O_NETGRAPH,             /* send to ng_ipfw              */
245         O_NGTEE,                /* copy to ng_ipfw              */
246
247         O_IP4,
248
249         O_UNREACH6,             /* arg1=icmpv6 code arg (deny)  */
250
251         O_TAG,                  /* arg1=tag number */
252         O_TAGGED,               /* arg1=tag number */
253
254         O_SETFIB,               /* arg1=FIB number */
255         O_FIB,                  /* arg1=FIB desired fib number */
256         
257         O_SOCKARG,              /* socket argument */
258
259         O_CALLRETURN,           /* arg1=called rule number */
260
261         O_FORWARD_IP6,          /* fwd sockaddr_in6             */
262
263         O_DSCP,                 /* 2 u32 = DSCP mask */
264         O_SETDSCP,              /* arg1=DSCP value */
265         O_IP_FLOW_LOOKUP,       /* arg1=table number, u32=value */
266
267         O_LAST_OPCODE           /* not an opcode!               */
268 };
269
270
271 /*
272  * The extension header are filtered only for presence using a bit
273  * vector with a flag for each header.
274  */
275 #define EXT_FRAGMENT    0x1
276 #define EXT_HOPOPTS     0x2
277 #define EXT_ROUTING     0x4
278 #define EXT_AH          0x8
279 #define EXT_ESP         0x10
280 #define EXT_DSTOPTS     0x20
281 #define EXT_RTHDR0              0x40
282 #define EXT_RTHDR2              0x80
283
284 /*
285  * Template for instructions.
286  *
287  * ipfw_insn is used for all instructions which require no operands,
288  * a single 16-bit value (arg1), or a couple of 8-bit values.
289  *
290  * For other instructions which require different/larger arguments
291  * we have derived structures, ipfw_insn_*.
292  *
293  * The size of the instruction (in 32-bit words) is in the low
294  * 6 bits of "len". The 2 remaining bits are used to implement
295  * NOT and OR on individual instructions. Given a type, you can
296  * compute the length to be put in "len" using F_INSN_SIZE(t)
297  *
298  * F_NOT        negates the match result of the instruction.
299  *
300  * F_OR         is used to build or blocks. By default, instructions
301  *              are evaluated as part of a logical AND. An "or" block
302  *              { X or Y or Z } contains F_OR set in all but the last
303  *              instruction of the block. A match will cause the code
304  *              to skip past the last instruction of the block.
305  *
306  * NOTA BENE: in a couple of places we assume that
307  *      sizeof(ipfw_insn) == sizeof(u_int32_t)
308  * this needs to be fixed.
309  *
310  */
311 typedef struct  _ipfw_insn {    /* template for instructions */
312         u_int8_t        opcode;
313         u_int8_t        len;    /* number of 32-bit words */
314 #define F_NOT           0x80
315 #define F_OR            0x40
316 #define F_LEN_MASK      0x3f
317 #define F_LEN(cmd)      ((cmd)->len & F_LEN_MASK)
318
319         u_int16_t       arg1;
320 } ipfw_insn;
321
322 /*
323  * The F_INSN_SIZE(type) computes the size, in 4-byte words, of
324  * a given type.
325  */
326 #define F_INSN_SIZE(t)  ((sizeof (t))/sizeof(u_int32_t))
327
328 /*
329  * This is used to store an array of 16-bit entries (ports etc.)
330  */
331 typedef struct  _ipfw_insn_u16 {
332         ipfw_insn o;
333         u_int16_t ports[2];     /* there may be more */
334 } ipfw_insn_u16;
335
336 /*
337  * This is used to store an array of 32-bit entries
338  * (uid, single IPv4 addresses etc.)
339  */
340 typedef struct  _ipfw_insn_u32 {
341         ipfw_insn o;
342         u_int32_t d[1]; /* one or more */
343 } ipfw_insn_u32;
344
345 /*
346  * This is used to store IP addr-mask pairs.
347  */
348 typedef struct  _ipfw_insn_ip {
349         ipfw_insn o;
350         struct in_addr  addr;
351         struct in_addr  mask;
352 } ipfw_insn_ip;
353
354 /*
355  * This is used to forward to a given address (ip).
356  */
357 typedef struct  _ipfw_insn_sa {
358         ipfw_insn o;
359         struct sockaddr_in sa;
360 } ipfw_insn_sa;
361
362 /*
363  * This is used to forward to a given address (ipv6).
364  */
365 typedef struct _ipfw_insn_sa6 {
366         ipfw_insn o;
367         struct sockaddr_in6 sa;
368 } ipfw_insn_sa6;
369
370 /*
371  * This is used for MAC addr-mask pairs.
372  */
373 typedef struct  _ipfw_insn_mac {
374         ipfw_insn o;
375         u_char addr[12];        /* dst[6] + src[6] */
376         u_char mask[12];        /* dst[6] + src[6] */
377 } ipfw_insn_mac;
378
379 /*
380  * This is used for interface match rules (recv xx, xmit xx).
381  */
382 typedef struct  _ipfw_insn_if {
383         ipfw_insn o;
384         union {
385                 struct in_addr ip;
386                 int glob;
387         } p;
388         char name[IFNAMSIZ];
389 } ipfw_insn_if;
390
391 /*
392  * This is used for storing an altq queue id number.
393  */
394 typedef struct _ipfw_insn_altq {
395         ipfw_insn       o;
396         u_int32_t       qid;
397 } ipfw_insn_altq;
398
399 /*
400  * This is used for limit rules.
401  */
402 typedef struct  _ipfw_insn_limit {
403         ipfw_insn o;
404         u_int8_t _pad;
405         u_int8_t limit_mask;    /* combination of DYN_* below   */
406 #define DYN_SRC_ADDR    0x1
407 #define DYN_SRC_PORT    0x2
408 #define DYN_DST_ADDR    0x4
409 #define DYN_DST_PORT    0x8
410
411         u_int16_t conn_limit;
412 } ipfw_insn_limit;
413
414 /*
415  * This is used for log instructions.
416  */
417 typedef struct  _ipfw_insn_log {
418         ipfw_insn o;
419         u_int32_t max_log;      /* how many do we log -- 0 = all */
420         u_int32_t log_left;     /* how many left to log         */
421 } ipfw_insn_log;
422
423 /*
424  * Data structures required by both ipfw(8) and ipfw(4) but not part of the
425  * management API are protected by IPFW_INTERNAL.
426  */
427 #ifdef IPFW_INTERNAL
428 /* Server pool support (LSNAT). */
429 struct cfg_spool {
430         LIST_ENTRY(cfg_spool)   _next;          /* chain of spool instances */
431         struct in_addr          addr;
432         u_short                 port;
433 };
434 #endif
435
436 /* Redirect modes id. */
437 #define REDIR_ADDR      0x01
438 #define REDIR_PORT      0x02
439 #define REDIR_PROTO     0x04
440
441 #ifdef IPFW_INTERNAL
442 /* Nat redirect configuration. */
443 struct cfg_redir {
444         LIST_ENTRY(cfg_redir)   _next;          /* chain of redir instances */
445         u_int16_t               mode;           /* type of redirect mode */
446         struct in_addr          laddr;          /* local ip address */
447         struct in_addr          paddr;          /* public ip address */
448         struct in_addr          raddr;          /* remote ip address */
449         u_short                 lport;          /* local port */
450         u_short                 pport;          /* public port */
451         u_short                 rport;          /* remote port  */
452         u_short                 pport_cnt;      /* number of public ports */
453         u_short                 rport_cnt;      /* number of remote ports */
454         int                     proto;          /* protocol: tcp/udp */
455         struct alias_link       **alink;        
456         /* num of entry in spool chain */
457         u_int16_t               spool_cnt;      
458         /* chain of spool instances */
459         LIST_HEAD(spool_chain, cfg_spool) spool_chain;
460 };
461 #endif
462
463 #ifdef IPFW_INTERNAL
464 /* Nat configuration data struct. */
465 struct cfg_nat {
466         /* chain of nat instances */
467         LIST_ENTRY(cfg_nat)     _next;
468         int                     id;                     /* nat id */
469         struct in_addr          ip;                     /* nat ip address */
470         char                    if_name[IF_NAMESIZE];   /* interface name */
471         int                     mode;                   /* aliasing mode */
472         struct libalias         *lib;                   /* libalias instance */
473         /* number of entry in spool chain */
474         int                     redir_cnt;              
475         /* chain of redir instances */
476         LIST_HEAD(redir_chain, cfg_redir) redir_chain;  
477 };
478 #endif
479
480 #define SOF_NAT         sizeof(struct cfg_nat)
481 #define SOF_REDIR       sizeof(struct cfg_redir)
482 #define SOF_SPOOL       sizeof(struct cfg_spool)
483
484 /* Nat command. */
485 typedef struct  _ipfw_insn_nat {
486         ipfw_insn       o;
487         struct cfg_nat *nat;    
488 } ipfw_insn_nat;
489
490 /* Apply ipv6 mask on ipv6 addr */
491 #define APPLY_MASK(addr,mask)                          \
492     (addr)->__u6_addr.__u6_addr32[0] &= (mask)->__u6_addr.__u6_addr32[0]; \
493     (addr)->__u6_addr.__u6_addr32[1] &= (mask)->__u6_addr.__u6_addr32[1]; \
494     (addr)->__u6_addr.__u6_addr32[2] &= (mask)->__u6_addr.__u6_addr32[2]; \
495     (addr)->__u6_addr.__u6_addr32[3] &= (mask)->__u6_addr.__u6_addr32[3];
496
497 /* Structure for ipv6 */
498 typedef struct _ipfw_insn_ip6 {
499        ipfw_insn o;
500        struct in6_addr addr6;
501        struct in6_addr mask6;
502 } ipfw_insn_ip6;
503
504 /* Used to support icmp6 types */
505 typedef struct _ipfw_insn_icmp6 {
506        ipfw_insn o;
507        uint32_t d[7]; /* XXX This number si related to the netinet/icmp6.h
508                        *     define ICMP6_MAXTYPE
509                        *     as follows: n = ICMP6_MAXTYPE/32 + 1
510                         *     Actually is 203 
511                        */
512 } ipfw_insn_icmp6;
513
514 /*
515  * Here we have the structure representing an ipfw rule.
516  *
517  * Layout:
518  * struct ip_fw_rule
519  * [ counter block, size = rule->cntr_len ]
520  * [ one or more instructions, size = rule->cmd_len * 4 ]
521  *
522  * It starts with a general area (with link fields).
523  * Counter block may be next (if rule->cntr_len > 0),
524  * followed by an array of one or more instructions, which the code
525  * accesses as an array of 32-bit values. rule->cmd_len represents
526  * the total instructions legth in u32 worrd, while act_ofs represents
527  * rule action offset in u32 words.
528  *
529  * When assembling instruction, remember the following:
530  *
531  *  + if a rule has a "keep-state" (or "limit") option, then the
532  *      first instruction (at r->cmd) MUST BE an O_PROBE_STATE
533  *  + if a rule has a "log" option, then the first action
534  *      (at ACTION_PTR(r)) MUST be O_LOG
535  *  + if a rule has an "altq" option, it comes after "log"
536  *  + if a rule has an O_TAG option, it comes after "log" and "altq"
537  *
538  *
539  * All structures (excluding instructions) are u64-aligned.
540  * Please keep this.
541  */
542
543 struct ip_fw_rule {
544         uint16_t        act_ofs;        /* offset of action in 32-bit units */
545         uint16_t        cmd_len;        /* # of 32-bit words in cmd     */
546         uint16_t        spare;
547         uint8_t         set;            /* rule set (0..31)             */
548         uint8_t         flags;          /* rule flags                   */
549         uint32_t        rulenum;        /* rule number                  */
550         uint32_t        id;             /* rule id                      */
551
552         ipfw_insn       cmd[1];         /* storage for commands         */
553 };
554 #define IPFW_RULE_NOOPT         0x01    /* Has no options in body       */
555
556 /* Unaligned version */
557
558 /* Base ipfw rule counter block. */
559 struct ip_fw_bcounter {
560         uint16_t        size;           /* Size of counter block, bytes */
561         uint8_t         flags;          /* flags for given block        */
562         uint8_t         spare;
563         uint32_t        timestamp;      /* tv_sec of last match         */
564         uint64_t        pcnt;           /* Packet counter               */
565         uint64_t        bcnt;           /* Byte counter                 */
566 };
567
568
569 #ifndef _KERNEL
570 /*
571  * Legacy rule format
572  */
573 struct ip_fw {
574         struct ip_fw    *x_next;        /* linked list of rules         */
575         struct ip_fw    *next_rule;     /* ptr to next [skipto] rule    */
576         /* 'next_rule' is used to pass up 'set_disable' status          */
577
578         uint16_t        act_ofs;        /* offset of action in 32-bit units */
579         uint16_t        cmd_len;        /* # of 32-bit words in cmd     */
580         uint16_t        rulenum;        /* rule number                  */
581         uint8_t set;            /* rule set (0..31)             */
582         uint8_t         _pad;           /* padding                      */
583         uint32_t        id;             /* rule id */
584
585         /* These fields are present in all rules.                       */
586         uint64_t        pcnt;           /* Packet counter               */
587         uint64_t        bcnt;           /* Byte counter                 */
588         uint32_t        timestamp;      /* tv_sec of last match         */
589
590         ipfw_insn       cmd[1];         /* storage for commands         */
591 };
592 #endif
593
594 #define ACTION_PTR(rule)                                \
595         (ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) )
596
597 #define RULESIZE(rule)  (sizeof(*(rule)) + (rule)->cmd_len * 4 - 4)
598
599
600 #if 1 // should be moved to in.h
601 /*
602  * This structure is used as a flow mask and a flow id for various
603  * parts of the code.
604  * addr_type is used in userland and kernel to mark the address type.
605  * fib is used in the kernel to record the fib in use.
606  * _flags is used in the kernel to store tcp flags for dynamic rules.
607  */
608 struct ipfw_flow_id {
609         uint32_t        dst_ip;
610         uint32_t        src_ip;
611         uint16_t        dst_port;
612         uint16_t        src_port;
613         uint8_t         fib;
614         uint8_t         proto;
615         uint8_t         _flags; /* protocol-specific flags */
616         uint8_t         addr_type; /* 4=ip4, 6=ip6, 1=ether ? */
617         struct in6_addr dst_ip6;
618         struct in6_addr src_ip6;
619         uint32_t        flow_id6;
620         uint32_t        extra; /* queue/pipe or frag_id */
621 };
622 #endif
623
624 #define IS_IP6_FLOW_ID(id)      ((id)->addr_type == 6)
625
626 /*
627  * Dynamic ipfw rule.
628  */
629 typedef struct _ipfw_dyn_rule ipfw_dyn_rule;
630
631 struct _ipfw_dyn_rule {
632         ipfw_dyn_rule   *next;          /* linked list of rules.        */
633         struct ip_fw *rule;             /* pointer to rule              */
634         /* 'rule' is used to pass up the rule number (from the parent)  */
635
636         ipfw_dyn_rule *parent;          /* pointer to parent rule       */
637         u_int64_t       pcnt;           /* packet match counter         */
638         u_int64_t       bcnt;           /* byte match counter           */
639         struct ipfw_flow_id id;         /* (masked) flow id             */
640         u_int32_t       expire;         /* expire time                  */
641         u_int32_t       bucket;         /* which bucket in hash table   */
642         u_int32_t       state;          /* state of this rule (typically a
643                                          * combination of TCP flags)
644                                          */
645         u_int32_t       ack_fwd;        /* most recent ACKs in forward  */
646         u_int32_t       ack_rev;        /* and reverse directions (used */
647                                         /* to generate keepalives)      */
648         u_int16_t       dyn_type;       /* rule type                    */
649         u_int16_t       count;          /* refcount                     */
650 };
651
652 /*
653  * Definitions for IP option names.
654  */
655 #define IP_FW_IPOPT_LSRR        0x01
656 #define IP_FW_IPOPT_SSRR        0x02
657 #define IP_FW_IPOPT_RR          0x04
658 #define IP_FW_IPOPT_TS          0x08
659
660 /*
661  * Definitions for TCP option names.
662  */
663 #define IP_FW_TCPOPT_MSS        0x01
664 #define IP_FW_TCPOPT_WINDOW     0x02
665 #define IP_FW_TCPOPT_SACK       0x04
666 #define IP_FW_TCPOPT_TS         0x08
667 #define IP_FW_TCPOPT_CC         0x10
668
669 #define ICMP_REJECT_RST         0x100   /* fake ICMP code (send a TCP RST) */
670 #define ICMP6_UNREACH_RST       0x100   /* fake ICMPv6 code (send a TCP RST) */
671
672 /*
673  * These are used for lookup tables.
674  */
675
676 #define IPFW_TABLE_CIDR         1       /* Table for holding IPv4/IPv6 prefixes */
677 #define IPFW_TABLE_INTERFACE    2       /* Table for holding interface names */
678 #define IPFW_TABLE_NUMBER       3       /* Table for holding ports/uid/gid/etc */
679 #define IPFW_TABLE_FLOW         4       /* Table for holding flow data */
680 #define IPFW_TABLE_MAXTYPE      4       /* Maximum valid number */
681
682 #define IPFW_VTYPE_U32          1       /* Skipto/tablearg integer */
683 #define IPFW_VTYPE_IP           2       /* Nexthop IP address */
684 #define IPFW_VTYPE_DSCP         3       /* DiffServ codepoints */
685
686 typedef struct  _ipfw_table_entry {
687         in_addr_t       addr;           /* network address              */
688         u_int32_t       value;          /* value                        */
689         u_int16_t       tbl;            /* table number                 */
690         u_int8_t        masklen;        /* mask length                  */
691 } ipfw_table_entry;
692
693 typedef struct  _ipfw_table_xentry {
694         uint16_t        len;            /* Total entry length           */
695         uint8_t         type;           /* entry type                   */
696         uint8_t         masklen;        /* mask length                  */
697         uint16_t        tbl;            /* table number                 */
698         uint16_t        flags;          /* record flags                 */
699         uint32_t        value;          /* value                        */
700         union {
701                 /* Longest field needs to be aligned by 4-byte boundary */
702                 struct in6_addr addr6;  /* IPv6 address                 */
703                 char    iface[IF_NAMESIZE];     /* interface name       */
704         } k;
705 } ipfw_table_xentry;
706 #define IPFW_TCF_INET   0x01            /* CIDR flags: IPv4 record      */
707
708 typedef struct  _ipfw_table {
709         u_int32_t       size;           /* size of entries in bytes     */
710         u_int32_t       cnt;            /* # of entries                 */
711         u_int16_t       tbl;            /* table number                 */
712         ipfw_table_entry ent[0];        /* entries                      */
713 } ipfw_table;
714
715 typedef struct  _ipfw_xtable {
716         ip_fw3_opheader opheader;       /* IP_FW3 opcode */
717         uint32_t        size;           /* size of entries in bytes     */
718         uint32_t        cnt;            /* # of entries                 */
719         uint16_t        tbl;            /* table number                 */
720         uint8_t         type;           /* table type                   */
721         ipfw_table_xentry xent[0];      /* entries                      */
722 } ipfw_xtable;
723
724 typedef struct  _ipfw_obj_tlv {
725         uint16_t        type;           /* TLV type */
726         uint16_t        flags;          /* TLV-specific flags           */
727         uint32_t        length;         /* Total length, aligned to u64 */
728 } ipfw_obj_tlv;
729 #define IPFW_TLV_TBL_NAME       1
730 #define IPFW_TLV_TBLNAME_LIST   2
731 #define IPFW_TLV_RULE_LIST      3
732 #define IPFW_TLV_DYNSTATE_LIST  4
733 #define IPFW_TLV_TBL_ENT        5
734 #define IPFW_TLV_DYN_ENT        6
735 #define IPFW_TLV_RULE_ENT       7
736 #define IPFW_TLV_TBLENT_LIST    8
737
738 /* Object name TLV */
739 typedef struct _ipfw_obj_ntlv {
740         ipfw_obj_tlv    head;           /* TLV header                   */
741         uint16_t        idx;            /* Name index                   */
742         uint8_t         spare;          /* unused                       */
743         uint8_t         type;           /* object type, if applicable   */
744         uint32_t        set;            /* set, if applicable           */
745         char            name[64];       /* Null-terminated name         */
746 } ipfw_obj_ntlv;
747
748 /* IPv4/IPv6 L4 flow description */
749 struct tflow_entry {
750         uint8_t         af;
751         uint8_t         proto;
752         uint16_t        spare;
753         uint16_t        sport;
754         uint16_t        dport;
755         union {
756                 struct {
757                         struct in_addr  sip;
758                         struct in_addr  dip;
759                 } a4;
760                 struct {
761                         struct in6_addr sip6;
762                         struct in6_addr dip6;
763                 } a6;
764         } a;
765 };
766
767 /* Table entry TLV */
768 typedef struct  _ipfw_obj_tentry {
769         ipfw_obj_tlv    head;           /* TLV header                   */
770         uint8_t         subtype;        /* subtype (IPv4,IPv6)          */
771         uint8_t         masklen;        /* mask length                  */
772         uint16_t        idx;            /* Table name index             */
773         uint32_t        value;          /* value                        */
774         uint64_t        spare;
775         union {
776                 /* Longest field needs to be aligned by 8-byte boundary */
777                 struct in_addr          addr;   /* IPv4 address         */
778                 uint32_t                key;            /* uid/gid/port */
779                 struct in6_addr         addr6;  /* IPv6 address         */
780                 char    iface[IF_NAMESIZE];     /* interface name       */
781                 struct tflow_entry      flow;   
782         } k;
783 } ipfw_obj_tentry;
784 #define IPFW_TF_UPDATE  0x01            /* Update record if exists      */
785
786 typedef struct _ipfw_obj_dyntlv {
787         ipfw_obj_tlv    head;
788         ipfw_dyn_rule   state;
789 } ipfw_obj_dyntlv;
790 #define IPFW_DF_LAST    0x01            /* Last state in chain          */
791
792 /* Containter TLVs */
793 typedef struct _ipfw_obj_ctlv {
794         ipfw_obj_tlv    head;           /* TLV header                   */
795         uint32_t        count;          /* Number of sub-TLVs           */
796         uint16_t        objsize;        /* Single object size           */
797         uint8_t         version;        /* TLV version                  */
798         uint8_t         spare;
799 } ipfw_obj_ctlv;
800
801 typedef struct _ifpw_ta_tinfo {
802         uint32_t        flags;          /* Format flags                 */
803         uint8_t         taclass;        /* algorithm class              */
804         uint8_t         spare0;
805         uint16_t        spare1;
806         uint32_t        rssize4;        /* runtime structure size       */
807         uint32_t        rcount4;        /* number of items in runtime   */
808         uint32_t        rsize4;         /* item size in runtime         */
809         uint32_t        rssize6;        /* runtime structure size       */
810         uint32_t        rcount6;        /* number of items in runtime   */
811         uint32_t        rsize6;         /* item size in runtime         */
812 } ifpw_ta_tinfo;
813 #define IPFW_TACLASS_HASH       1       /* algo is based on hash        */
814 #define IPFW_TACLASS_ARRAY      2       /* algo is based on array       */
815 #define IPFW_TACLASS_RADIX      3       /* algo is based on radix tree  */
816
817 #define IPFW_TATFLAGS_DATA      0x0001          /* Has data filled in   */
818 #define IPFW_TATFLAGS_AF        0x0002          /* Separate data per AF */
819
820 typedef struct _ipfw_xtable_info {
821         uint8_t         type;           /* table type (cidr,iface,..)   */
822         uint8_t         tflags;         /* type flags                   */
823         uint8_t         ftype;          /* table value format type      */
824         uint8_t         vtype;          /* value type                   */
825         uint32_t        set;            /* set table is in              */
826         uint32_t        kidx;           /* kernel index                 */
827         uint32_t        refcnt;         /* number of references         */
828         uint32_t        count;          /* Number of records            */
829         uint32_t        size;           /* Total size of records(export)*/
830         uint32_t        limit;          /* Max number of records        */
831         uint32_t        spare;
832         char            tablename[64];  /* table name */
833         char            algoname[64];   /* algorithm name               */
834         ifpw_ta_tinfo   ta_info;        /* additional algo stats        */
835 } ipfw_xtable_info;
836 #define IPFW_TFFLAG_SRCIP       0x01
837 #define IPFW_TFFLAG_DSTIP       0x02
838 #define IPFW_TFFLAG_SRCPORT     0x04
839 #define IPFW_TFFLAG_DSTPORT     0x08
840 #define IPFW_TFFLAG_PROTO       0x10
841
842 typedef struct _ipfw_iface_info {
843         char            ifname[64];     /* interface name               */
844         uint32_t        ifindex;        /* interface index              */
845         uint32_t        flags;          /* flags                        */
846         uint32_t        refcnt;         /* number of references         */
847         uint32_t        gencnt;         /* number of changes            */
848         uint64_t        spare;
849 } ipfw_iface_info;
850 #define IPFW_IFFLAG_RESOLVED    0x01    /* Interface exists             */
851
852 typedef struct _ipfw_ta_info {
853         char            algoname[64];   /* algorithm name               */
854         uint32_t        type;           /* lookup type                  */
855         uint32_t        flags;
856         uint32_t        refcnt;
857         uint32_t        spare0;
858         uint64_t        spare1;
859 } ipfw_ta_info;
860
861 #define IPFW_OBJTYPE_TABLE      1
862 typedef struct _ipfw_obj_header {
863         ip_fw3_opheader opheader;       /* IP_FW3 opcode                */
864         uint32_t        spare;
865         uint16_t        idx;            /* object name index            */
866         uint8_t         objtype;        /* object type                  */
867         uint8_t         objsubtype;     /* object subtype               */
868         ipfw_obj_ntlv   ntlv;           /* object name tlv              */
869 } ipfw_obj_header;
870
871 typedef struct _ipfw_obj_lheader {
872         ip_fw3_opheader opheader;       /* IP_FW3 opcode                */
873         uint32_t        set_mask;       /* disabled set mask            */
874         uint32_t        count;          /* Total objects count          */
875         uint32_t        size;           /* Total size (incl. header)    */
876         uint32_t        objsize;        /* Size of one object           */
877 } ipfw_obj_lheader;
878
879 #define IPFW_CFG_GET_STATIC     0x01
880 #define IPFW_CFG_GET_STATES     0x02
881 #define IPFW_CFG_GET_COUNTERS   0x04
882 typedef struct _ipfw_cfg_lheader {
883         ip_fw3_opheader opheader;       /* IP_FW3 opcode                */
884         uint32_t        set_mask;       /* enabled set mask             */
885         uint32_t        spare;
886         uint32_t        flags;          /* Request flags                */
887         uint32_t        size;           /* neded buffer size            */
888         uint32_t        start_rule;
889         uint32_t        end_rule;
890 } ipfw_cfg_lheader;
891
892 #endif /* _IPFW2_H */