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