]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/ip_fw.h
* Add new IP_FW_XADD opcode which permits to
[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 /*
40  * Number of sets supported by ipfw
41  */
42 #define IPFW_MAX_SETS           32
43
44 /*
45  * Default number of ipfw tables.
46  */
47 #define IPFW_TABLES_MAX         65535
48 #define IPFW_TABLES_DEFAULT     128
49
50 /*
51  * Most commands (queue, pipe, tag, untag, limit...) can have a 16-bit
52  * argument between 1 and 65534. The value 0 is unused, the value
53  * 65535 (IP_FW_TABLEARG) is used to represent 'tablearg', i.e. the
54  * can be 1..65534, or 65535 to indicate the use of a 'tablearg'
55  * result of the most recent table() lookup.
56  * Note that 16bit is only a historical limit, resulting from
57  * the use of a 16-bit fields for that value. In reality, we can have
58  * 2^32 pipes, queues, tag values and so on, and use 0 as a tablearg.
59  */
60 #define IPFW_ARG_MIN            1
61 #define IPFW_ARG_MAX            65534
62 #define IP_FW_TABLEARG          65535   /* XXX should use 0 */
63
64 /*
65  * Number of entries in the call stack of the call/return commands.
66  * Call stack currently is an uint16_t array with rule numbers.
67  */
68 #define IPFW_CALLSTACK_SIZE     16
69
70 /* IP_FW3 header/opcodes */
71 typedef struct _ip_fw3_opheader {
72         uint16_t opcode;        /* Operation opcode */
73         uint16_t version;       /* Opcode version */
74         uint16_t reserved[2];   /* Align to 64-bit boundary */
75 } ip_fw3_opheader;
76
77
78 /* IPFW extented tables support */
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_XGETSIZE   91      /* get size for tables list */
85 #define IP_FW_TABLES_XLIST      92      /* list all tables  */
86 #define IP_FW_TABLE_XINFO       93      /* request info for one table */
87 #define IP_FW_TABLE_XFLUSH      94      /* flush table data */
88 #define IP_FW_TABLE_XCREATE     95      /* create new table  */
89 #define IP_FW_TABLE_XMODIFY     96      /* modify existing table */
90 #define IP_FW_XGET              97      /* Retrieve configuration */
91 #define IP_FW_XADD              98      /* add entry */
92
93 /*
94  * Usage guidelines:
95  *
96  * IP_FW_TABLE_XLIST(ver 1): Dumps all table data
97  *   Request(getsockopt): [ ipfw_obj_lheader ], size = ipfw_xtable_info.size
98  *   Reply: [ ipfw_obj_lheader ipfw_xtable_info ipfw_table_xentry x N ]
99  *
100  * IP_FW_TABLE_XDESTROY: Destroys given table
101  *   Request(setsockopt): [ ipfw_obj_header ]
102  *
103  * IP_FW_TABLES_XGETSIZE: Get buffer size needed to list info for all tables.
104  *   Request(getsockopt): [ empty ], size = sizeof(ipfw_obj_lheader)
105  *   Reply: [ ipfw_obj_lheader ]
106  *
107  * IP_FW_TABLES_XLIST: Lists all tables currently available in kernel.
108  *   Request(getsockopt): [ ipfw_obj_lheader ], size = ipfw_obj_lheader.size
109  *   Reply: [ ipfw_obj_lheader ipfw_xtable_info x N ]
110  *
111  * IP_FW_TABLE_XINFO: Store table info to buffer.
112  *   Request(getsockopt): [ ipfw_obj_header ipfw_xtable_info(empty)]
113  *   Reply: [ ipfw_obj_header ipfw_xtable_info ]
114  *
115  * IP_FW_TABLE_XFLUSH: Removes all data from given table leaving type etc..
116  *   Request(setsockopt): [ ipfw_obj_header ]
117  */
118
119 /*
120  * The kernel representation of ipfw rules is made of a list of
121  * 'instructions' (for all practical purposes equivalent to BPF
122  * instructions), which specify which fields of the packet
123  * (or its metadata) should be analysed.
124  *
125  * Each instruction is stored in a structure which begins with
126  * "ipfw_insn", and can contain extra fields depending on the
127  * instruction type (listed below).
128  * Note that the code is written so that individual instructions
129  * have a size which is a multiple of 32 bits. This means that, if
130  * such structures contain pointers or other 64-bit entities,
131  * (there is just one instance now) they may end up unaligned on
132  * 64-bit architectures, so the must be handled with care.
133  *
134  * "enum ipfw_opcodes" are the opcodes supported. We can have up
135  * to 256 different opcodes. When adding new opcodes, they should
136  * be appended to the end of the opcode list before O_LAST_OPCODE,
137  * this will prevent the ABI from being broken, otherwise users
138  * will have to recompile ipfw(8) when they update the kernel.
139  */
140
141 enum ipfw_opcodes {             /* arguments (4 byte each)      */
142         O_NOP,
143
144         O_IP_SRC,               /* u32 = IP                     */
145         O_IP_SRC_MASK,          /* ip = IP/mask                 */
146         O_IP_SRC_ME,            /* none                         */
147         O_IP_SRC_SET,           /* u32=base, arg1=len, bitmap   */
148
149         O_IP_DST,               /* u32 = IP                     */
150         O_IP_DST_MASK,          /* ip = IP/mask                 */
151         O_IP_DST_ME,            /* none                         */
152         O_IP_DST_SET,           /* u32=base, arg1=len, bitmap   */
153
154         O_IP_SRCPORT,           /* (n)port list:mask 4 byte ea  */
155         O_IP_DSTPORT,           /* (n)port list:mask 4 byte ea  */
156         O_PROTO,                /* arg1=protocol                */
157
158         O_MACADDR2,             /* 2 mac addr:mask              */
159         O_MAC_TYPE,             /* same as srcport              */
160
161         O_LAYER2,               /* none                         */
162         O_IN,                   /* none                         */
163         O_FRAG,                 /* none                         */
164
165         O_RECV,                 /* none                         */
166         O_XMIT,                 /* none                         */
167         O_VIA,                  /* none                         */
168
169         O_IPOPT,                /* arg1 = 2*u8 bitmap           */
170         O_IPLEN,                /* arg1 = len                   */
171         O_IPID,                 /* arg1 = id                    */
172
173         O_IPTOS,                /* arg1 = id                    */
174         O_IPPRECEDENCE,         /* arg1 = precedence << 5       */
175         O_IPTTL,                /* arg1 = TTL                   */
176
177         O_IPVER,                /* arg1 = version               */
178         O_UID,                  /* u32 = id                     */
179         O_GID,                  /* u32 = id                     */
180         O_ESTAB,                /* none (tcp established)       */
181         O_TCPFLAGS,             /* arg1 = 2*u8 bitmap           */
182         O_TCPWIN,               /* arg1 = desired win           */
183         O_TCPSEQ,               /* u32 = desired seq.           */
184         O_TCPACK,               /* u32 = desired seq.           */
185         O_ICMPTYPE,             /* u32 = icmp bitmap            */
186         O_TCPOPTS,              /* arg1 = 2*u8 bitmap           */
187
188         O_VERREVPATH,           /* none                         */
189         O_VERSRCREACH,          /* none                         */
190
191         O_PROBE_STATE,          /* none                         */
192         O_KEEP_STATE,           /* none                         */
193         O_LIMIT,                /* ipfw_insn_limit              */
194         O_LIMIT_PARENT,         /* dyn_type, not an opcode.     */
195
196         /*
197          * These are really 'actions'.
198          */
199
200         O_LOG,                  /* ipfw_insn_log                */
201         O_PROB,                 /* u32 = match probability      */
202
203         O_CHECK_STATE,          /* none                         */
204         O_ACCEPT,               /* none                         */
205         O_DENY,                 /* none                         */
206         O_REJECT,               /* arg1=icmp arg (same as deny) */
207         O_COUNT,                /* none                         */
208         O_SKIPTO,               /* arg1=next rule number        */
209         O_PIPE,                 /* arg1=pipe number             */
210         O_QUEUE,                /* arg1=queue number            */
211         O_DIVERT,               /* arg1=port number             */
212         O_TEE,                  /* arg1=port number             */
213         O_FORWARD_IP,           /* fwd sockaddr                 */
214         O_FORWARD_MAC,          /* fwd mac                      */
215         O_NAT,                  /* nope                         */
216         O_REASS,                /* none                         */
217         
218         /*
219          * More opcodes.
220          */
221         O_IPSEC,                /* has ipsec history            */
222         O_IP_SRC_LOOKUP,        /* arg1=table number, u32=value */
223         O_IP_DST_LOOKUP,        /* arg1=table number, u32=value */
224         O_ANTISPOOF,            /* none                         */
225         O_JAIL,                 /* u32 = id                     */
226         O_ALTQ,                 /* u32 = altq classif. qid      */
227         O_DIVERTED,             /* arg1=bitmap (1:loop, 2:out)  */
228         O_TCPDATALEN,           /* arg1 = tcp data len          */
229         O_IP6_SRC,              /* address without mask         */
230         O_IP6_SRC_ME,           /* my addresses                 */
231         O_IP6_SRC_MASK,         /* address with the mask        */
232         O_IP6_DST,
233         O_IP6_DST_ME,
234         O_IP6_DST_MASK,
235         O_FLOW6ID,              /* for flow id tag in the ipv6 pkt */
236         O_ICMP6TYPE,            /* icmp6 packet type filtering  */
237         O_EXT_HDR,              /* filtering for ipv6 extension header */
238         O_IP6,
239
240         /*
241          * actions for ng_ipfw
242          */
243         O_NETGRAPH,             /* send to ng_ipfw              */
244         O_NGTEE,                /* copy to ng_ipfw              */
245
246         O_IP4,
247
248         O_UNREACH6,             /* arg1=icmpv6 code arg (deny)  */
249
250         O_TAG,                  /* arg1=tag number */
251         O_TAGGED,               /* arg1=tag number */
252
253         O_SETFIB,               /* arg1=FIB number */
254         O_FIB,                  /* arg1=FIB desired fib number */
255         
256         O_SOCKARG,              /* socket argument */
257
258         O_CALLRETURN,           /* arg1=called rule number */
259
260         O_FORWARD_IP6,          /* fwd sockaddr_in6             */
261
262         O_DSCP,                 /* 2 u32 = DSCP mask */
263         O_SETDSCP,              /* arg1=DSCP value */
264
265         O_LAST_OPCODE           /* not an opcode!               */
266 };
267
268
269 /*
270  * The extension header are filtered only for presence using a bit
271  * vector with a flag for each header.
272  */
273 #define EXT_FRAGMENT    0x1
274 #define EXT_HOPOPTS     0x2
275 #define EXT_ROUTING     0x4
276 #define EXT_AH          0x8
277 #define EXT_ESP         0x10
278 #define EXT_DSTOPTS     0x20
279 #define EXT_RTHDR0              0x40
280 #define EXT_RTHDR2              0x80
281
282 /*
283  * Template for instructions.
284  *
285  * ipfw_insn is used for all instructions which require no operands,
286  * a single 16-bit value (arg1), or a couple of 8-bit values.
287  *
288  * For other instructions which require different/larger arguments
289  * we have derived structures, ipfw_insn_*.
290  *
291  * The size of the instruction (in 32-bit words) is in the low
292  * 6 bits of "len". The 2 remaining bits are used to implement
293  * NOT and OR on individual instructions. Given a type, you can
294  * compute the length to be put in "len" using F_INSN_SIZE(t)
295  *
296  * F_NOT        negates the match result of the instruction.
297  *
298  * F_OR         is used to build or blocks. By default, instructions
299  *              are evaluated as part of a logical AND. An "or" block
300  *              { X or Y or Z } contains F_OR set in all but the last
301  *              instruction of the block. A match will cause the code
302  *              to skip past the last instruction of the block.
303  *
304  * NOTA BENE: in a couple of places we assume that
305  *      sizeof(ipfw_insn) == sizeof(u_int32_t)
306  * this needs to be fixed.
307  *
308  */
309 typedef struct  _ipfw_insn {    /* template for instructions */
310         u_int8_t        opcode;
311         u_int8_t        len;    /* number of 32-bit words */
312 #define F_NOT           0x80
313 #define F_OR            0x40
314 #define F_LEN_MASK      0x3f
315 #define F_LEN(cmd)      ((cmd)->len & F_LEN_MASK)
316
317         u_int16_t       arg1;
318 } ipfw_insn;
319
320 /*
321  * The F_INSN_SIZE(type) computes the size, in 4-byte words, of
322  * a given type.
323  */
324 #define F_INSN_SIZE(t)  ((sizeof (t))/sizeof(u_int32_t))
325
326 /*
327  * This is used to store an array of 16-bit entries (ports etc.)
328  */
329 typedef struct  _ipfw_insn_u16 {
330         ipfw_insn o;
331         u_int16_t ports[2];     /* there may be more */
332 } ipfw_insn_u16;
333
334 /*
335  * This is used to store an array of 32-bit entries
336  * (uid, single IPv4 addresses etc.)
337  */
338 typedef struct  _ipfw_insn_u32 {
339         ipfw_insn o;
340         u_int32_t d[1]; /* one or more */
341 } ipfw_insn_u32;
342
343 /*
344  * This is used to store IP addr-mask pairs.
345  */
346 typedef struct  _ipfw_insn_ip {
347         ipfw_insn o;
348         struct in_addr  addr;
349         struct in_addr  mask;
350 } ipfw_insn_ip;
351
352 /*
353  * This is used to forward to a given address (ip).
354  */
355 typedef struct  _ipfw_insn_sa {
356         ipfw_insn o;
357         struct sockaddr_in sa;
358 } ipfw_insn_sa;
359
360 /*
361  * This is used to forward to a given address (ipv6).
362  */
363 typedef struct _ipfw_insn_sa6 {
364         ipfw_insn o;
365         struct sockaddr_in6 sa;
366 } ipfw_insn_sa6;
367
368 /*
369  * This is used for MAC addr-mask pairs.
370  */
371 typedef struct  _ipfw_insn_mac {
372         ipfw_insn o;
373         u_char addr[12];        /* dst[6] + src[6] */
374         u_char mask[12];        /* dst[6] + src[6] */
375 } ipfw_insn_mac;
376
377 /*
378  * This is used for interface match rules (recv xx, xmit xx).
379  */
380 typedef struct  _ipfw_insn_if {
381         ipfw_insn o;
382         union {
383                 struct in_addr ip;
384                 int glob;
385         } p;
386         char name[IFNAMSIZ];
387 } ipfw_insn_if;
388
389 /*
390  * This is used for storing an altq queue id number.
391  */
392 typedef struct _ipfw_insn_altq {
393         ipfw_insn       o;
394         u_int32_t       qid;
395 } ipfw_insn_altq;
396
397 /*
398  * This is used for limit rules.
399  */
400 typedef struct  _ipfw_insn_limit {
401         ipfw_insn o;
402         u_int8_t _pad;
403         u_int8_t limit_mask;    /* combination of DYN_* below   */
404 #define DYN_SRC_ADDR    0x1
405 #define DYN_SRC_PORT    0x2
406 #define DYN_DST_ADDR    0x4
407 #define DYN_DST_PORT    0x8
408
409         u_int16_t conn_limit;
410 } ipfw_insn_limit;
411
412 /*
413  * This is used for log instructions.
414  */
415 typedef struct  _ipfw_insn_log {
416         ipfw_insn o;
417         u_int32_t max_log;      /* how many do we log -- 0 = all */
418         u_int32_t log_left;     /* how many left to log         */
419 } ipfw_insn_log;
420
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 /* Nat command. */
483 typedef struct  _ipfw_insn_nat {
484         ipfw_insn       o;
485         struct cfg_nat *nat;    
486 } ipfw_insn_nat;
487
488 /* Apply ipv6 mask on ipv6 addr */
489 #define APPLY_MASK(addr,mask)                          \
490     (addr)->__u6_addr.__u6_addr32[0] &= (mask)->__u6_addr.__u6_addr32[0]; \
491     (addr)->__u6_addr.__u6_addr32[1] &= (mask)->__u6_addr.__u6_addr32[1]; \
492     (addr)->__u6_addr.__u6_addr32[2] &= (mask)->__u6_addr.__u6_addr32[2]; \
493     (addr)->__u6_addr.__u6_addr32[3] &= (mask)->__u6_addr.__u6_addr32[3];
494
495 /* Structure for ipv6 */
496 typedef struct _ipfw_insn_ip6 {
497        ipfw_insn o;
498        struct in6_addr addr6;
499        struct in6_addr mask6;
500 } ipfw_insn_ip6;
501
502 /* Used to support icmp6 types */
503 typedef struct _ipfw_insn_icmp6 {
504        ipfw_insn o;
505        uint32_t d[7]; /* XXX This number si related to the netinet/icmp6.h
506                        *     define ICMP6_MAXTYPE
507                        *     as follows: n = ICMP6_MAXTYPE/32 + 1
508                         *     Actually is 203 
509                        */
510 } ipfw_insn_icmp6;
511
512 /*
513  * Here we have the structure representing an ipfw rule.
514  *
515  * It starts with a general area (with link fields and counters)
516  * followed by an array of one or more instructions, which the code
517  * accesses as an array of 32-bit values.
518  *
519  * Given a rule pointer  r:
520  *
521  *  r->cmd              is the start of the first instruction.
522  *  ACTION_PTR(r)       is the start of the first action (things to do
523  *                      once a rule matched).
524  *
525  * When assembling instruction, remember the following:
526  *
527  *  + if a rule has a "keep-state" (or "limit") option, then the
528  *      first instruction (at r->cmd) MUST BE an O_PROBE_STATE
529  *  + if a rule has a "log" option, then the first action
530  *      (at ACTION_PTR(r)) MUST be O_LOG
531  *  + if a rule has an "altq" option, it comes after "log"
532  *  + if a rule has an O_TAG option, it comes after "log" and "altq"
533  *
534  * NOTE: we use a simple linked list of rules because we never need
535  *      to delete a rule without scanning the list. We do not use
536  *      queue(3) macros for portability and readability.
537  */
538
539 struct ip_fw {
540         struct ip_fw    *x_next;        /* linked list of rules         */
541         struct ip_fw    *next_rule;     /* ptr to next [skipto] rule    */
542         /* 'next_rule' is used to pass up 'set_disable' status          */
543
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        rulenum;        /* rule number                  */
547         uint8_t set;            /* rule set (0..31)             */
548 #define RESVD_SET       31      /* set for default and persistent rules */
549         uint8_t         _pad;           /* padding                      */
550         uint32_t        id;             /* rule id */
551
552         /* These fields are present in all rules.                       */
553         uint64_t        pcnt;           /* Packet counter               */
554         uint64_t        bcnt;           /* Byte counter                 */
555         uint32_t        timestamp;      /* tv_sec of last match         */
556
557         ipfw_insn       cmd[1];         /* storage for commands         */
558 };
559
560 #define ACTION_PTR(rule)                                \
561         (ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) )
562
563 #define RULESIZE(rule)  (sizeof(struct ip_fw) + \
564         ((struct ip_fw *)(rule))->cmd_len * 4 - 4)
565
566 #if 1 // should be moved to in.h
567 /*
568  * This structure is used as a flow mask and a flow id for various
569  * parts of the code.
570  * addr_type is used in userland and kernel to mark the address type.
571  * fib is used in the kernel to record the fib in use.
572  * _flags is used in the kernel to store tcp flags for dynamic rules.
573  */
574 struct ipfw_flow_id {
575         uint32_t        dst_ip;
576         uint32_t        src_ip;
577         uint16_t        dst_port;
578         uint16_t        src_port;
579         uint8_t         fib;
580         uint8_t         proto;
581         uint8_t         _flags; /* protocol-specific flags */
582         uint8_t         addr_type; /* 4=ip4, 6=ip6, 1=ether ? */
583         struct in6_addr dst_ip6;
584         struct in6_addr src_ip6;
585         uint32_t        flow_id6;
586         uint32_t        extra; /* queue/pipe or frag_id */
587 };
588 #endif
589
590 #define IS_IP6_FLOW_ID(id)      ((id)->addr_type == 6)
591
592 /*
593  * Dynamic ipfw rule.
594  */
595 typedef struct _ipfw_dyn_rule ipfw_dyn_rule;
596
597 struct _ipfw_dyn_rule {
598         ipfw_dyn_rule   *next;          /* linked list of rules.        */
599         struct ip_fw *rule;             /* pointer to rule              */
600         /* 'rule' is used to pass up the rule number (from the parent)  */
601
602         ipfw_dyn_rule *parent;          /* pointer to parent rule       */
603         u_int64_t       pcnt;           /* packet match counter         */
604         u_int64_t       bcnt;           /* byte match counter           */
605         struct ipfw_flow_id id;         /* (masked) flow id             */
606         u_int32_t       expire;         /* expire time                  */
607         u_int32_t       bucket;         /* which bucket in hash table   */
608         u_int32_t       state;          /* state of this rule (typically a
609                                          * combination of TCP flags)
610                                          */
611         u_int32_t       ack_fwd;        /* most recent ACKs in forward  */
612         u_int32_t       ack_rev;        /* and reverse directions (used */
613                                         /* to generate keepalives)      */
614         u_int16_t       dyn_type;       /* rule type                    */
615         u_int16_t       count;          /* refcount                     */
616 };
617
618 /*
619  * Definitions for IP option names.
620  */
621 #define IP_FW_IPOPT_LSRR        0x01
622 #define IP_FW_IPOPT_SSRR        0x02
623 #define IP_FW_IPOPT_RR          0x04
624 #define IP_FW_IPOPT_TS          0x08
625
626 /*
627  * Definitions for TCP option names.
628  */
629 #define IP_FW_TCPOPT_MSS        0x01
630 #define IP_FW_TCPOPT_WINDOW     0x02
631 #define IP_FW_TCPOPT_SACK       0x04
632 #define IP_FW_TCPOPT_TS         0x08
633 #define IP_FW_TCPOPT_CC         0x10
634
635 #define ICMP_REJECT_RST         0x100   /* fake ICMP code (send a TCP RST) */
636 #define ICMP6_UNREACH_RST       0x100   /* fake ICMPv6 code (send a TCP RST) */
637
638 /*
639  * These are used for lookup tables.
640  */
641
642 #define IPFW_TABLE_CIDR         1       /* Table for holding IPv4/IPv6 prefixes */
643 #define IPFW_TABLE_INTERFACE    2       /* Table for holding interface names */
644 #define IPFW_TABLE_MAXTYPE      2       /* Maximum valid number */
645
646 typedef struct  _ipfw_table_entry {
647         in_addr_t       addr;           /* network address              */
648         u_int32_t       value;          /* value                        */
649         u_int16_t       tbl;            /* table number                 */
650         u_int8_t        masklen;        /* mask length                  */
651 } ipfw_table_entry;
652
653 typedef struct  _ipfw_table_xentry {
654         uint16_t        len;            /* Total entry length           */
655         uint8_t         type;           /* entry type                   */
656         uint8_t         masklen;        /* mask length                  */
657         uint16_t        tbl;            /* table number                 */
658         uint16_t        flags;          /* record flags                 */
659         uint32_t        value;          /* value                        */
660         union {
661                 /* Longest field needs to be aligned by 4-byte boundary */
662                 struct in6_addr addr6;  /* IPv6 address                 */
663                 char    iface[IF_NAMESIZE];     /* interface name       */
664         } k;
665 } ipfw_table_xentry;
666 #define IPFW_TCF_INET   0x01            /* CIDR flags: IPv4 record      */
667
668 typedef struct  _ipfw_table {
669         u_int32_t       size;           /* size of entries in bytes     */
670         u_int32_t       cnt;            /* # of entries                 */
671         u_int16_t       tbl;            /* table number                 */
672         ipfw_table_entry ent[0];        /* entries                      */
673 } ipfw_table;
674
675 typedef struct  _ipfw_xtable {
676         ip_fw3_opheader opheader;       /* IP_FW3 opcode */
677         uint32_t        size;           /* size of entries in bytes     */
678         uint32_t        cnt;            /* # of entries                 */
679         uint16_t        tbl;            /* table number                 */
680         uint8_t         type;           /* table type                   */
681         ipfw_table_xentry xent[0];      /* entries                      */
682 } ipfw_xtable;
683
684 typedef struct  _ipfw_obj_tlv {
685         uint16_t        type;           /* TLV type */
686         uint16_t        flags;          /* unused */
687         uint32_t        length;         /* Total length, aligned to u64 */
688 } ipfw_obj_tlv;
689 #define IPFW_TLV_TBL_NAME       1
690 #define IPFW_TLV_TBLNAME_LIST   2
691 #define IPFW_TLV_RULE_LIST      3
692 #define IPFW_TLV_STATE_LIST     4
693
694 /* Object name TLV */
695 typedef struct _ipfw_obj_ntlv {
696         ipfw_obj_tlv    head;           /* TLV header                   */
697         uint16_t        idx;            /* Name index                   */
698         uint16_t        spare0;         /* unused                       */
699         uint32_t        set;            /* set, if applicable           */
700         char            name[64];       /* Null-terminated name         */
701 } ipfw_obj_ntlv;
702
703 /* Containter TLVs */
704 typedef struct _ipfw_obj_ctlv {
705         ipfw_obj_tlv    head;           /* TLV header                   */
706         uint32_t        count;          /* Number of sub-TLVs           */
707         uint32_t        objsize;        /* Single object size           */
708 } ipfw_obj_ctlv;
709
710 typedef struct _ipfw_xtable_info {
711         uint8_t         type;           /* table type (cidr,iface,..)   */
712         uint8_t         ftype;          /* table value format type      */
713         uint8_t         atype;          /* algorithm type               */
714         uint8_t         spare0;
715         uint32_t        set;            /* set table is in              */
716         uint32_t        kidx;           /* kernel index                 */
717         uint32_t        refcnt;         /* number of references         */
718         uint32_t        count;          /* Number of records            */
719         uint32_t        size;           /* Total size of records        */
720         char            tablename[64];  /* table name */
721         char            algoname[32];   /* algorithm name               */
722 } ipfw_xtable_info;
723
724 #define IPFW_OBJTYPE_TABLE      1
725 typedef struct _ipfw_obj_header {
726         ip_fw3_opheader opheader;       /* IP_FW3 opcode                */
727         uint32_t        set;            /* Set we're operating          */
728         uint16_t        idx;            /* object name index            */
729         uint8_t         objtype;        /* object type                  */
730         uint8_t         objsubtype;     /* object subtype               */
731         ipfw_obj_ntlv   ntlv;           /* object name tlv              */
732 } ipfw_obj_header;
733
734 typedef struct _ipfw_obj_lheader {
735         ip_fw3_opheader opheader;       /* IP_FW3 opcode                */
736         uint32_t        spare;
737         uint32_t        count;          /* Total objects count          */
738         uint32_t        size;           /* Total objects size           */
739         uint32_t        objsize;        /* Size of one object           */
740 } ipfw_obj_lheader;
741
742 #define IPFW_CFG_GET_STATIC     1
743 #define IPFW_CFG_GET_STATES     2
744 typedef struct _ipfw_cfg_lheader {
745         ip_fw3_opheader opheader;       /* IP_FW3 opcode                */
746         uint32_t        set_mask;       /* disabled set mask            */
747         uint32_t        flags;          /* Request flags                */
748         uint32_t        size;           /* neded buffer size            */
749         uint32_t        start_rule;
750         uint32_t        end_rule;
751 } ipfw_cfg_lheader;
752
753 #endif /* _IPFW2_H */