xref: /freebsd/sys/netinet/ip_fw.h (revision c0020399a650364d0134f79f3fa319f84064372d)
1 /*-
2  * Copyright (c) 2002 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  * The number of ipfw tables.  The maximum allowed table number is the
41  * (IPFW_TABLES_MAX - 1).
42  */
43 #define	IPFW_TABLES_MAX		128
44 
45 /*
46  * The kernel representation of ipfw rules is made of a list of
47  * 'instructions' (for all practical purposes equivalent to BPF
48  * instructions), which specify which fields of the packet
49  * (or its metadata) should be analysed.
50  *
51  * Each instruction is stored in a structure which begins with
52  * "ipfw_insn", and can contain extra fields depending on the
53  * instruction type (listed below).
54  * Note that the code is written so that individual instructions
55  * have a size which is a multiple of 32 bits. This means that, if
56  * such structures contain pointers or other 64-bit entities,
57  * (there is just one instance now) they may end up unaligned on
58  * 64-bit architectures, so the must be handled with care.
59  *
60  * "enum ipfw_opcodes" are the opcodes supported. We can have up
61  * to 256 different opcodes. When adding new opcodes, they should
62  * be appended to the end of the opcode list before O_LAST_OPCODE,
63  * this will prevent the ABI from being broken, otherwise users
64  * will have to recompile ipfw(8) when they update the kernel.
65  */
66 
67 enum ipfw_opcodes {		/* arguments (4 byte each)	*/
68 	O_NOP,
69 
70 	O_IP_SRC,		/* u32 = IP			*/
71 	O_IP_SRC_MASK,		/* ip = IP/mask			*/
72 	O_IP_SRC_ME,		/* none				*/
73 	O_IP_SRC_SET,		/* u32=base, arg1=len, bitmap	*/
74 
75 	O_IP_DST,		/* u32 = IP			*/
76 	O_IP_DST_MASK,		/* ip = IP/mask			*/
77 	O_IP_DST_ME,		/* none				*/
78 	O_IP_DST_SET,		/* u32=base, arg1=len, bitmap	*/
79 
80 	O_IP_SRCPORT,		/* (n)port list:mask 4 byte ea	*/
81 	O_IP_DSTPORT,		/* (n)port list:mask 4 byte ea	*/
82 	O_PROTO,		/* arg1=protocol		*/
83 
84 	O_MACADDR2,		/* 2 mac addr:mask		*/
85 	O_MAC_TYPE,		/* same as srcport		*/
86 
87 	O_LAYER2,		/* none				*/
88 	O_IN,			/* none				*/
89 	O_FRAG,			/* none				*/
90 
91 	O_RECV,			/* none				*/
92 	O_XMIT,			/* none				*/
93 	O_VIA,			/* none				*/
94 
95 	O_IPOPT,		/* arg1 = 2*u8 bitmap		*/
96 	O_IPLEN,		/* arg1 = len			*/
97 	O_IPID,			/* arg1 = id			*/
98 
99 	O_IPTOS,		/* arg1 = id			*/
100 	O_IPPRECEDENCE,		/* arg1 = precedence << 5	*/
101 	O_IPTTL,		/* arg1 = TTL			*/
102 
103 	O_IPVER,		/* arg1 = version		*/
104 	O_UID,			/* u32 = id			*/
105 	O_GID,			/* u32 = id			*/
106 	O_ESTAB,		/* none (tcp established)	*/
107 	O_TCPFLAGS,		/* arg1 = 2*u8 bitmap		*/
108 	O_TCPWIN,		/* arg1 = desired win		*/
109 	O_TCPSEQ,		/* u32 = desired seq.		*/
110 	O_TCPACK,		/* u32 = desired seq.		*/
111 	O_ICMPTYPE,		/* u32 = icmp bitmap		*/
112 	O_TCPOPTS,		/* arg1 = 2*u8 bitmap		*/
113 
114 	O_VERREVPATH,		/* none				*/
115 	O_VERSRCREACH,		/* none				*/
116 
117 	O_PROBE_STATE,		/* none				*/
118 	O_KEEP_STATE,		/* none				*/
119 	O_LIMIT,		/* ipfw_insn_limit		*/
120 	O_LIMIT_PARENT,		/* dyn_type, not an opcode.	*/
121 
122 	/*
123 	 * These are really 'actions'.
124 	 */
125 
126 	O_LOG,			/* ipfw_insn_log		*/
127 	O_PROB,			/* u32 = match probability	*/
128 
129 	O_CHECK_STATE,		/* none				*/
130 	O_ACCEPT,		/* none				*/
131 	O_DENY,			/* none 			*/
132 	O_REJECT,		/* arg1=icmp arg (same as deny)	*/
133 	O_COUNT,		/* none				*/
134 	O_SKIPTO,		/* arg1=next rule number	*/
135 	O_PIPE,			/* arg1=pipe number		*/
136 	O_QUEUE,		/* arg1=queue number		*/
137 	O_DIVERT,		/* arg1=port number		*/
138 	O_TEE,			/* arg1=port number		*/
139 	O_FORWARD_IP,		/* fwd sockaddr			*/
140 	O_FORWARD_MAC,		/* fwd mac			*/
141 	O_NAT,                  /* nope                         */
142 	O_REASS,                /* none                         */
143 
144 	/*
145 	 * More opcodes.
146 	 */
147 	O_IPSEC,		/* has ipsec history 		*/
148 	O_IP_SRC_LOOKUP,	/* arg1=table number, u32=value	*/
149 	O_IP_DST_LOOKUP,	/* arg1=table number, u32=value	*/
150 	O_ANTISPOOF,		/* none				*/
151 	O_JAIL,			/* u32 = id			*/
152 	O_ALTQ,			/* u32 = altq classif. qid	*/
153 	O_DIVERTED,		/* arg1=bitmap (1:loop, 2:out)	*/
154 	O_TCPDATALEN,		/* arg1 = tcp data len		*/
155 	O_IP6_SRC,		/* address without mask		*/
156 	O_IP6_SRC_ME,		/* my addresses			*/
157 	O_IP6_SRC_MASK,		/* address with the mask	*/
158 	O_IP6_DST,
159 	O_IP6_DST_ME,
160 	O_IP6_DST_MASK,
161 	O_FLOW6ID,		/* for flow id tag in the ipv6 pkt */
162 	O_ICMP6TYPE,		/* icmp6 packet type filtering	*/
163 	O_EXT_HDR,		/* filtering for ipv6 extension header */
164 	O_IP6,
165 
166 	/*
167 	 * actions for ng_ipfw
168 	 */
169 	O_NETGRAPH,		/* send to ng_ipfw		*/
170 	O_NGTEE,		/* copy to ng_ipfw		*/
171 
172 	O_IP4,
173 
174 	O_UNREACH6,		/* arg1=icmpv6 code arg (deny)  */
175 
176 	O_TAG,   		/* arg1=tag number */
177 	O_TAGGED,		/* arg1=tag number */
178 
179 	O_SETFIB,		/* arg1=FIB number */
180 	O_FIB,			/* arg1=FIB desired fib number */
181 
182 	O_LAST_OPCODE		/* not an opcode!		*/
183 };
184 
185 /*
186  * The extension header are filtered only for presence using a bit
187  * vector with a flag for each header.
188  */
189 #define EXT_FRAGMENT	0x1
190 #define EXT_HOPOPTS	0x2
191 #define EXT_ROUTING	0x4
192 #define EXT_AH		0x8
193 #define EXT_ESP		0x10
194 #define EXT_DSTOPTS	0x20
195 #define EXT_RTHDR0		0x40
196 #define EXT_RTHDR2		0x80
197 
198 /*
199  * Template for instructions.
200  *
201  * ipfw_insn is used for all instructions which require no operands,
202  * a single 16-bit value (arg1), or a couple of 8-bit values.
203  *
204  * For other instructions which require different/larger arguments
205  * we have derived structures, ipfw_insn_*.
206  *
207  * The size of the instruction (in 32-bit words) is in the low
208  * 6 bits of "len". The 2 remaining bits are used to implement
209  * NOT and OR on individual instructions. Given a type, you can
210  * compute the length to be put in "len" using F_INSN_SIZE(t)
211  *
212  * F_NOT	negates the match result of the instruction.
213  *
214  * F_OR		is used to build or blocks. By default, instructions
215  *		are evaluated as part of a logical AND. An "or" block
216  *		{ X or Y or Z } contains F_OR set in all but the last
217  *		instruction of the block. A match will cause the code
218  *		to skip past the last instruction of the block.
219  *
220  * NOTA BENE: in a couple of places we assume that
221  *	sizeof(ipfw_insn) == sizeof(u_int32_t)
222  * this needs to be fixed.
223  *
224  */
225 typedef struct	_ipfw_insn {	/* template for instructions */
226 	enum ipfw_opcodes	opcode:8;
227 	u_int8_t	len;	/* number of 32-bit words */
228 #define	F_NOT		0x80
229 #define	F_OR		0x40
230 #define	F_LEN_MASK	0x3f
231 #define	F_LEN(cmd)	((cmd)->len & F_LEN_MASK)
232 
233 	u_int16_t	arg1;
234 } ipfw_insn;
235 
236 /*
237  * The F_INSN_SIZE(type) computes the size, in 4-byte words, of
238  * a given type.
239  */
240 #define	F_INSN_SIZE(t)	((sizeof (t))/sizeof(u_int32_t))
241 
242 #define MTAG_IPFW	1148380143	/* IPFW-tagged cookie */
243 
244 /*
245  * This is used to store an array of 16-bit entries (ports etc.)
246  */
247 typedef struct	_ipfw_insn_u16 {
248 	ipfw_insn o;
249 	u_int16_t ports[2];	/* there may be more */
250 } ipfw_insn_u16;
251 
252 /*
253  * This is used to store an array of 32-bit entries
254  * (uid, single IPv4 addresses etc.)
255  */
256 typedef struct	_ipfw_insn_u32 {
257 	ipfw_insn o;
258 	u_int32_t d[1];	/* one or more */
259 } ipfw_insn_u32;
260 
261 /*
262  * This is used to store IP addr-mask pairs.
263  */
264 typedef struct	_ipfw_insn_ip {
265 	ipfw_insn o;
266 	struct in_addr	addr;
267 	struct in_addr	mask;
268 } ipfw_insn_ip;
269 
270 /*
271  * This is used to forward to a given address (ip).
272  */
273 typedef struct  _ipfw_insn_sa {
274 	ipfw_insn o;
275 	struct sockaddr_in sa;
276 } ipfw_insn_sa;
277 
278 /*
279  * This is used for MAC addr-mask pairs.
280  */
281 typedef struct	_ipfw_insn_mac {
282 	ipfw_insn o;
283 	u_char addr[12];	/* dst[6] + src[6] */
284 	u_char mask[12];	/* dst[6] + src[6] */
285 } ipfw_insn_mac;
286 
287 /*
288  * This is used for interface match rules (recv xx, xmit xx).
289  */
290 typedef struct	_ipfw_insn_if {
291 	ipfw_insn o;
292 	union {
293 		struct in_addr ip;
294 		int glob;
295 	} p;
296 	char name[IFNAMSIZ];
297 } ipfw_insn_if;
298 
299 /*
300  * This is used for storing an altq queue id number.
301  */
302 typedef struct _ipfw_insn_altq {
303 	ipfw_insn	o;
304 	u_int32_t	qid;
305 } ipfw_insn_altq;
306 
307 /*
308  * This is used for limit rules.
309  */
310 typedef struct	_ipfw_insn_limit {
311 	ipfw_insn o;
312 	u_int8_t _pad;
313 	u_int8_t limit_mask;	/* combination of DYN_* below	*/
314 #define	DYN_SRC_ADDR	0x1
315 #define	DYN_SRC_PORT	0x2
316 #define	DYN_DST_ADDR	0x4
317 #define	DYN_DST_PORT	0x8
318 
319 	u_int16_t conn_limit;
320 } ipfw_insn_limit;
321 
322 /*
323  * This is used for log instructions.
324  */
325 typedef struct  _ipfw_insn_log {
326         ipfw_insn o;
327 	u_int32_t max_log;	/* how many do we log -- 0 = all */
328 	u_int32_t log_left;	/* how many left to log 	*/
329 } ipfw_insn_log;
330 
331 /*
332  * Data structures required by both ipfw(8) and ipfw(4) but not part of the
333  * management API are protected by IPFW_INTERNAL.
334  */
335 #ifdef IPFW_INTERNAL
336 /* Server pool support (LSNAT). */
337 struct cfg_spool {
338 	LIST_ENTRY(cfg_spool)   _next;          /* chain of spool instances */
339 	struct in_addr          addr;
340 	u_short                 port;
341 };
342 #endif
343 
344 /* Redirect modes id. */
345 #define REDIR_ADDR      0x01
346 #define REDIR_PORT      0x02
347 #define REDIR_PROTO     0x04
348 
349 #ifdef IPFW_INTERNAL
350 /* Nat redirect configuration. */
351 struct cfg_redir {
352 	LIST_ENTRY(cfg_redir)   _next;          /* chain of redir instances */
353 	u_int16_t               mode;           /* type of redirect mode */
354 	struct in_addr	        laddr;          /* local ip address */
355 	struct in_addr	        paddr;          /* public ip address */
356 	struct in_addr	        raddr;          /* remote ip address */
357 	u_short                 lport;          /* local port */
358 	u_short                 pport;          /* public port */
359 	u_short                 rport;          /* remote port  */
360 	u_short                 pport_cnt;      /* number of public ports */
361 	u_short                 rport_cnt;      /* number of remote ports */
362 	int                     proto;          /* protocol: tcp/udp */
363 	struct alias_link       **alink;
364 	/* num of entry in spool chain */
365 	u_int16_t               spool_cnt;
366 	/* chain of spool instances */
367 	LIST_HEAD(spool_chain, cfg_spool) spool_chain;
368 };
369 #endif
370 
371 #define NAT_BUF_LEN     1024
372 
373 #ifdef IPFW_INTERNAL
374 /* Nat configuration data struct. */
375 struct cfg_nat {
376 	/* chain of nat instances */
377 	LIST_ENTRY(cfg_nat)     _next;
378 	int                     id;                     /* nat id */
379 	struct in_addr          ip;                     /* nat ip address */
380 	char                    if_name[IF_NAMESIZE];   /* interface name */
381 	int                     mode;                   /* aliasing mode */
382 	struct libalias	        *lib;                   /* libalias instance */
383 	/* number of entry in spool chain */
384 	int                     redir_cnt;
385 	/* chain of redir instances */
386 	LIST_HEAD(redir_chain, cfg_redir) redir_chain;
387 };
388 #endif
389 
390 #define SOF_NAT         sizeof(struct cfg_nat)
391 #define SOF_REDIR       sizeof(struct cfg_redir)
392 #define SOF_SPOOL       sizeof(struct cfg_spool)
393 
394 /* Nat command. */
395 typedef struct	_ipfw_insn_nat {
396  	ipfw_insn	o;
397  	struct cfg_nat *nat;
398 } ipfw_insn_nat;
399 
400 /* Apply ipv6 mask on ipv6 addr */
401 #define APPLY_MASK(addr,mask)                          \
402     (addr)->__u6_addr.__u6_addr32[0] &= (mask)->__u6_addr.__u6_addr32[0]; \
403     (addr)->__u6_addr.__u6_addr32[1] &= (mask)->__u6_addr.__u6_addr32[1]; \
404     (addr)->__u6_addr.__u6_addr32[2] &= (mask)->__u6_addr.__u6_addr32[2]; \
405     (addr)->__u6_addr.__u6_addr32[3] &= (mask)->__u6_addr.__u6_addr32[3];
406 
407 /* Structure for ipv6 */
408 typedef struct _ipfw_insn_ip6 {
409        ipfw_insn o;
410        struct in6_addr addr6;
411        struct in6_addr mask6;
412 } ipfw_insn_ip6;
413 
414 /* Used to support icmp6 types */
415 typedef struct _ipfw_insn_icmp6 {
416        ipfw_insn o;
417        uint32_t d[7]; /* XXX This number si related to the netinet/icmp6.h
418                        *     define ICMP6_MAXTYPE
419                        *     as follows: n = ICMP6_MAXTYPE/32 + 1
420                         *     Actually is 203
421                        */
422 } ipfw_insn_icmp6;
423 
424 /*
425  * Here we have the structure representing an ipfw rule.
426  *
427  * It starts with a general area (with link fields and counters)
428  * followed by an array of one or more instructions, which the code
429  * accesses as an array of 32-bit values.
430  *
431  * Given a rule pointer  r:
432  *
433  *  r->cmd		is the start of the first instruction.
434  *  ACTION_PTR(r)	is the start of the first action (things to do
435  *			once a rule matched).
436  *
437  * When assembling instruction, remember the following:
438  *
439  *  + if a rule has a "keep-state" (or "limit") option, then the
440  *	first instruction (at r->cmd) MUST BE an O_PROBE_STATE
441  *  + if a rule has a "log" option, then the first action
442  *	(at ACTION_PTR(r)) MUST be O_LOG
443  *  + if a rule has an "altq" option, it comes after "log"
444  *  + if a rule has an O_TAG option, it comes after "log" and "altq"
445  *
446  * NOTE: we use a simple linked list of rules because we never need
447  * 	to delete a rule without scanning the list. We do not use
448  *	queue(3) macros for portability and readability.
449  */
450 
451 struct ip_fw {
452 	struct ip_fw	*next;		/* linked list of rules		*/
453 	struct ip_fw	*next_rule;	/* ptr to next [skipto] rule	*/
454 	/* 'next_rule' is used to pass up 'set_disable' status		*/
455 
456 	u_int16_t	act_ofs;	/* offset of action in 32-bit units */
457 	u_int16_t	cmd_len;	/* # of 32-bit words in cmd	*/
458 	u_int16_t	rulenum;	/* rule number			*/
459 	u_int8_t	set;		/* rule set (0..31)		*/
460 #define	RESVD_SET	31	/* set for default and persistent rules */
461 	u_int8_t	_pad;		/* padding			*/
462 
463 	/* These fields are present in all rules.			*/
464 	u_int64_t	pcnt;		/* Packet counter		*/
465 	u_int64_t	bcnt;		/* Byte counter			*/
466 	u_int32_t	timestamp;	/* tv_sec of last match		*/
467 
468 	ipfw_insn	cmd[1];		/* storage for commands		*/
469 };
470 
471 #define ACTION_PTR(rule)				\
472 	(ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) )
473 
474 #define RULESIZE(rule)  (sizeof(struct ip_fw) + \
475 	((struct ip_fw *)(rule))->cmd_len * 4 - 4)
476 
477 /*
478  * This structure is used as a flow mask and a flow id for various
479  * parts of the code.
480  */
481 struct ipfw_flow_id {
482 	u_int32_t	dst_ip;
483 	u_int32_t	src_ip;
484 	u_int16_t	dst_port;
485 	u_int16_t	src_port;
486 	u_int8_t	fib;
487 	u_int8_t	proto;
488 	u_int8_t	flags;	/* protocol-specific flags */
489 	uint8_t		addr_type; /* 4 = ipv4, 6 = ipv6, 1=ether ? */
490 	struct in6_addr dst_ip6;	/* could also store MAC addr! */
491 	struct in6_addr src_ip6;
492 	u_int32_t	flow_id6;
493 	u_int32_t	frag_id6;
494 };
495 
496 #define IS_IP6_FLOW_ID(id)	((id)->addr_type == 6)
497 
498 /*
499  * Dynamic ipfw rule.
500  */
501 typedef struct _ipfw_dyn_rule ipfw_dyn_rule;
502 
503 struct _ipfw_dyn_rule {
504 	ipfw_dyn_rule	*next;		/* linked list of rules.	*/
505 	struct ip_fw *rule;		/* pointer to rule		*/
506 	/* 'rule' is used to pass up the rule number (from the parent)	*/
507 
508 	ipfw_dyn_rule *parent;		/* pointer to parent rule	*/
509 	u_int64_t	pcnt;		/* packet match counter		*/
510 	u_int64_t	bcnt;		/* byte match counter		*/
511 	struct ipfw_flow_id id;		/* (masked) flow id		*/
512 	u_int32_t	expire;		/* expire time			*/
513 	u_int32_t	bucket;		/* which bucket in hash table	*/
514 	u_int32_t	state;		/* state of this rule (typically a
515 					 * combination of TCP flags)
516 					 */
517 	u_int32_t	ack_fwd;	/* most recent ACKs in forward	*/
518 	u_int32_t	ack_rev;	/* and reverse directions (used	*/
519 					/* to generate keepalives)	*/
520 	u_int16_t	dyn_type;	/* rule type			*/
521 	u_int16_t	count;		/* refcount			*/
522 };
523 
524 /*
525  * Definitions for IP option names.
526  */
527 #define	IP_FW_IPOPT_LSRR	0x01
528 #define	IP_FW_IPOPT_SSRR	0x02
529 #define	IP_FW_IPOPT_RR		0x04
530 #define	IP_FW_IPOPT_TS		0x08
531 
532 /*
533  * Definitions for TCP option names.
534  */
535 #define	IP_FW_TCPOPT_MSS	0x01
536 #define	IP_FW_TCPOPT_WINDOW	0x02
537 #define	IP_FW_TCPOPT_SACK	0x04
538 #define	IP_FW_TCPOPT_TS		0x08
539 #define	IP_FW_TCPOPT_CC		0x10
540 
541 #define	ICMP_REJECT_RST		0x100	/* fake ICMP code (send a TCP RST) */
542 #define	ICMP6_UNREACH_RST	0x100	/* fake ICMPv6 code (send a TCP RST) */
543 
544 /*
545  * These are used for lookup tables.
546  */
547 typedef struct	_ipfw_table_entry {
548 	in_addr_t	addr;		/* network address		*/
549 	u_int32_t	value;		/* value			*/
550 	u_int16_t	tbl;		/* table number			*/
551 	u_int8_t	masklen;	/* mask length			*/
552 } ipfw_table_entry;
553 
554 typedef struct	_ipfw_table {
555 	u_int32_t	size;		/* size of entries in bytes	*/
556 	u_int32_t	cnt;		/* # of entries			*/
557 	u_int16_t	tbl;		/* table number			*/
558 	ipfw_table_entry ent[0];	/* entries			*/
559 } ipfw_table;
560 
561 #define IP_FW_TABLEARG	65535
562 
563 /*
564  * Main firewall chains definitions and global var's definitions.
565  */
566 #ifdef _KERNEL
567 
568 /* Return values from ipfw_chk() */
569 enum {
570 	IP_FW_PASS = 0,
571 	IP_FW_DENY,
572 	IP_FW_DIVERT,
573 	IP_FW_TEE,
574 	IP_FW_DUMMYNET,
575 	IP_FW_NETGRAPH,
576 	IP_FW_NGTEE,
577 	IP_FW_NAT,
578 	IP_FW_REASS,
579 };
580 
581 /* flags for divert mtag */
582 #define	IP_FW_DIVERT_LOOPBACK_FLAG	0x00080000
583 #define	IP_FW_DIVERT_OUTPUT_FLAG	0x00100000
584 
585 /*
586  * Structure for collecting parameters to dummynet for ip6_output forwarding
587  */
588 struct _ip6dn_args {
589        struct ip6_pktopts *opt_or;
590        struct route_in6 ro_or;
591        int flags_or;
592        struct ip6_moptions *im6o_or;
593        struct ifnet *origifp_or;
594        struct ifnet *ifp_or;
595        struct sockaddr_in6 dst_or;
596        u_long mtu_or;
597        struct route_in6 ro_pmtu_or;
598 };
599 
600 /*
601  * Arguments for calling ipfw_chk() and dummynet_io(). We put them
602  * all into a structure because this way it is easier and more
603  * efficient to pass variables around and extend the interface.
604  */
605 struct ip_fw_args {
606 	struct mbuf	*m;		/* the mbuf chain		*/
607 	struct ifnet	*oif;		/* output interface		*/
608 	struct sockaddr_in *next_hop;	/* forward address		*/
609 	struct ip_fw	*rule;		/* matching rule		*/
610 	struct ether_header *eh;	/* for bridged packets		*/
611 
612 	struct ipfw_flow_id f_id;	/* grabbed from IP header	*/
613 	u_int32_t	cookie;		/* a cookie depending on rule action */
614 	struct inpcb	*inp;
615 
616 	struct _ip6dn_args	dummypar; /* dummynet->ip6_output */
617 	struct sockaddr_in hopstore;	/* store here if cannot use a pointer */
618 };
619 
620 /*
621  * Function definitions.
622  */
623 
624 /* Firewall hooks */
625 struct sockopt;
626 struct dn_flow_set;
627 
628 int ipfw_check_in(void *, struct mbuf **, struct ifnet *, int, struct inpcb *inp);
629 int ipfw_check_out(void *, struct mbuf **, struct ifnet *, int, struct inpcb *inp);
630 
631 int ipfw_chk(struct ip_fw_args *);
632 
633 int ipfw_init(void);
634 void ipfw_destroy(void);
635 #ifdef NOTYET
636 void ipfw_nat_destroy(void);
637 #endif
638 
639 typedef int ip_fw_ctl_t(struct sockopt *);
640 extern ip_fw_ctl_t *ip_fw_ctl_ptr;
641 
642 #ifdef VIMAGE_GLOBALS
643 extern int fw_one_pass;
644 extern int fw_enable;
645 #ifdef INET6
646 extern int fw6_enable;
647 #endif
648 #endif
649 
650 /* For kernel ipfw_ether and ipfw_bridge. */
651 typedef	int ip_fw_chk_t(struct ip_fw_args *args);
652 extern	ip_fw_chk_t	*ip_fw_chk_ptr;
653 #define	IPFW_LOADED	(ip_fw_chk_ptr != NULL)
654 
655 struct ip_fw_chain {
656 	struct ip_fw	*rules;		/* list of rules */
657 	struct ip_fw	*reap;		/* list of rules to reap */
658 	LIST_HEAD(, cfg_nat) nat;       /* list of nat entries */
659 	struct radix_node_head *tables[IPFW_TABLES_MAX];
660 	struct rwlock	rwmtx;
661 };
662 
663 #ifdef IPFW_INTERNAL
664 
665 #define	IPFW_LOCK_INIT(_chain) \
666 	rw_init(&(_chain)->rwmtx, "IPFW static rules")
667 #define	IPFW_LOCK_DESTROY(_chain)	rw_destroy(&(_chain)->rwmtx)
668 #define	IPFW_WLOCK_ASSERT(_chain)	rw_assert(&(_chain)->rwmtx, RA_WLOCKED)
669 
670 #define IPFW_RLOCK(p) rw_rlock(&(p)->rwmtx)
671 #define IPFW_RUNLOCK(p) rw_runlock(&(p)->rwmtx)
672 #define IPFW_WLOCK(p) rw_wlock(&(p)->rwmtx)
673 #define IPFW_WUNLOCK(p) rw_wunlock(&(p)->rwmtx)
674 
675 #define LOOKUP_NAT(l, i, p) do {					\
676 		LIST_FOREACH((p), &(l.nat), _next) {			\
677 			if ((p)->id == (i)) {				\
678 				break;					\
679 			} 						\
680 		}							\
681 	} while (0)
682 
683 typedef int ipfw_nat_t(struct ip_fw_args *, struct cfg_nat *, struct mbuf *);
684 typedef int ipfw_nat_cfg_t(struct sockopt *);
685 #endif
686 
687 struct eventhandler_entry;
688 /*
689  * Stack virtualization support.
690  */
691 struct vnet_ipfw {
692 	int	_fw_enable;
693 	int	_fw6_enable;
694 	u_int32_t _set_disable;
695 	int	_fw_deny_unknown_exthdrs;
696 	int	_fw_verbose;
697 	int	_verbose_limit;
698 	int	_fw_debug;		/* actually unused */
699 	int	_autoinc_step;
700 	ipfw_dyn_rule **_ipfw_dyn_v;
701 	uma_zone_t _ipfw_dyn_rule_zone;
702 	struct ip_fw_chain _layer3_chain;
703 	u_int32_t _dyn_buckets;
704 	u_int32_t _curr_dyn_buckets;
705 	u_int32_t _dyn_ack_lifetime;
706 	u_int32_t _dyn_syn_lifetime;
707 	u_int32_t _dyn_fin_lifetime;
708 	u_int32_t _dyn_rst_lifetime;
709 	u_int32_t _dyn_udp_lifetime;
710 	u_int32_t _dyn_short_lifetime;
711 	u_int32_t _dyn_keepalive_interval;
712 	u_int32_t _dyn_keepalive_period;
713 	u_int32_t _dyn_keepalive;
714 	u_int32_t _static_count;
715 	u_int32_t _static_len;
716 	u_int32_t _dyn_count;
717 	u_int32_t _dyn_max;
718 	u_int64_t _norule_counter;
719 	struct callout _ipfw_timeout;
720 	struct eventhandler_entry *_ifaddr_event_tag;
721 };
722 
723 #ifndef VIMAGE
724 #ifndef VIMAGE_GLOBALS
725 extern struct vnet_ipfw vnet_ipfw_0;
726 #endif
727 #endif
728 
729 /*
730  * Symbol translation macros
731  */
732 #define	INIT_VNET_IPFW(vnet) \
733 	INIT_FROM_VNET(vnet, VNET_MOD_IPFW, struct vnet_ipfw, vnet_ipfw)
734 
735 #define	VNET_IPFW(sym)		VSYM(vnet_ipfw, sym)
736 
737 #define	V_fw_enable		VNET_IPFW(fw_enable)
738 #define	V_fw6_enable		VNET_IPFW(fw6_enable)
739 #define	V_set_disable		VNET_IPFW(set_disable)
740 #define	V_fw_deny_unknown_exthdrs VNET_IPFW(fw_deny_unknown_exthdrs)
741 #define	V_fw_verbose		VNET_IPFW(fw_verbose)
742 #define	V_verbose_limit		VNET_IPFW(verbose_limit)
743 #define	V_fw_debug		VNET_IPFW(fw_debug)
744 #define	V_autoinc_step		VNET_IPFW(autoinc_step)
745 #define	V_ipfw_dyn_v		VNET_IPFW(ipfw_dyn_v)
746 #define	V_ipfw_dyn_rule_zone	VNET_IPFW(ipfw_dyn_rule_zone)
747 #define	V_layer3_chain		VNET_IPFW(layer3_chain)
748 #define	V_dyn_buckets		VNET_IPFW(dyn_buckets)
749 #define	V_curr_dyn_buckets	VNET_IPFW(curr_dyn_buckets)
750 #define	V_dyn_ack_lifetime	VNET_IPFW(dyn_ack_lifetime)
751 #define	V_dyn_syn_lifetime	VNET_IPFW(dyn_syn_lifetime)
752 #define	V_dyn_fin_lifetime	VNET_IPFW(dyn_fin_lifetime)
753 #define	V_dyn_rst_lifetime	VNET_IPFW(dyn_rst_lifetime)
754 #define	V_dyn_udp_lifetime	VNET_IPFW(dyn_udp_lifetime)
755 #define	V_dyn_short_lifetime	VNET_IPFW(dyn_short_lifetime)
756 #define	V_dyn_keepalive_interval VNET_IPFW(dyn_keepalive_interval)
757 #define	V_dyn_keepalive_period	VNET_IPFW(dyn_keepalive_period)
758 #define	V_dyn_keepalive		VNET_IPFW(dyn_keepalive)
759 #define	V_static_count		VNET_IPFW(static_count)
760 #define	V_static_len		VNET_IPFW(static_len)
761 #define	V_dyn_count		VNET_IPFW(dyn_count)
762 #define	V_dyn_max		VNET_IPFW(dyn_max)
763 #define	V_norule_counter	VNET_IPFW(norule_counter)
764 #define	V_ipfw_timeout		VNET_IPFW(ipfw_timeout)
765 #define	V_ifaddr_event_tag	VNET_IPFW(ifaddr_event_tag)
766 
767 #endif /* _KERNEL */
768 #endif /* _IPFW2_H */
769