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