ip_fw.h revision 145864
1237433Skib/*-
2237433Skib * Copyright (c) 2002 Luigi Rizzo, Universita` di Pisa
3237433Skib *
4237433Skib * Redistribution and use in source and binary forms, with or without
5237433Skib * modification, are permitted provided that the following conditions
6237433Skib * are met:
7237433Skib * 1. Redistributions of source code must retain the above copyright
8237433Skib *    notice, this list of conditions and the following disclaimer.
9237433Skib * 2. Redistributions in binary form must reproduce the above copyright
10237433Skib *    notice, this list of conditions and the following disclaimer in the
11237433Skib *    documentation and/or other materials provided with the distribution.
12237433Skib *
13237433Skib * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14237433Skib * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15237433Skib * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16237433Skib * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17237433Skib * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18237433Skib * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19237433Skib * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20237433Skib * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21237433Skib * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22237433Skib * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23237433Skib * SUCH DAMAGE.
24237433Skib *
25237433Skib * $FreeBSD: head/sys/netinet/ip_fw.h 145864 2005-05-04 13:12:52Z glebius $
26237433Skib */
27237433Skib
28237433Skib#ifndef _IPFW2_H
29237433Skib#define _IPFW2_H
30237433Skib
31237433Skib/*
32237433Skib * The kernel representation of ipfw rules is made of a list of
33237433Skib * 'instructions' (for all practical purposes equivalent to BPF
34237433Skib * instructions), which specify which fields of the packet
35 * (or its metadata) should be analysed.
36 *
37 * Each instruction is stored in a structure which begins with
38 * "ipfw_insn", and can contain extra fields depending on the
39 * instruction type (listed below).
40 * Note that the code is written so that individual instructions
41 * have a size which is a multiple of 32 bits. This means that, if
42 * such structures contain pointers or other 64-bit entities,
43 * (there is just one instance now) they may end up unaligned on
44 * 64-bit architectures, so the must be handled with care.
45 *
46 * "enum ipfw_opcodes" are the opcodes supported. We can have up
47 * to 256 different opcodes. When adding new opcodes, they should
48 * be appended to the end of the opcode list before O_LAST_OPCODE,
49 * this will prevent the ABI from being broken, otherwise users
50 * will have to recompile ipfw(8) when they update the kernel.
51 */
52
53enum ipfw_opcodes {		/* arguments (4 byte each)	*/
54	O_NOP,
55
56	O_IP_SRC,		/* u32 = IP			*/
57	O_IP_SRC_MASK,		/* ip = IP/mask			*/
58	O_IP_SRC_ME,		/* none				*/
59	O_IP_SRC_SET,		/* u32=base, arg1=len, bitmap	*/
60
61	O_IP_DST,		/* u32 = IP			*/
62	O_IP_DST_MASK,		/* ip = IP/mask			*/
63	O_IP_DST_ME,		/* none				*/
64	O_IP_DST_SET,		/* u32=base, arg1=len, bitmap	*/
65
66	O_IP_SRCPORT,		/* (n)port list:mask 4 byte ea	*/
67	O_IP_DSTPORT,		/* (n)port list:mask 4 byte ea	*/
68	O_PROTO,		/* arg1=protocol		*/
69
70	O_MACADDR2,		/* 2 mac addr:mask		*/
71	O_MAC_TYPE,		/* same as srcport		*/
72
73	O_LAYER2,		/* none				*/
74	O_IN,			/* none				*/
75	O_FRAG,			/* none				*/
76
77	O_RECV,			/* none				*/
78	O_XMIT,			/* none				*/
79	O_VIA,			/* none				*/
80
81	O_IPOPT,		/* arg1 = 2*u8 bitmap		*/
82	O_IPLEN,		/* arg1 = len			*/
83	O_IPID,			/* arg1 = id			*/
84
85	O_IPTOS,		/* arg1 = id			*/
86	O_IPPRECEDENCE,		/* arg1 = precedence << 5	*/
87	O_IPTTL,		/* arg1 = TTL			*/
88
89	O_IPVER,		/* arg1 = version		*/
90	O_UID,			/* u32 = id			*/
91	O_GID,			/* u32 = id			*/
92	O_ESTAB,		/* none (tcp established)	*/
93	O_TCPFLAGS,		/* arg1 = 2*u8 bitmap		*/
94	O_TCPWIN,		/* arg1 = desired win		*/
95	O_TCPSEQ,		/* u32 = desired seq.		*/
96	O_TCPACK,		/* u32 = desired seq.		*/
97	O_ICMPTYPE,		/* u32 = icmp bitmap		*/
98	O_TCPOPTS,		/* arg1 = 2*u8 bitmap		*/
99
100	O_VERREVPATH,		/* none				*/
101	O_VERSRCREACH,		/* none				*/
102
103	O_PROBE_STATE,		/* none				*/
104	O_KEEP_STATE,		/* none				*/
105	O_LIMIT,		/* ipfw_insn_limit		*/
106	O_LIMIT_PARENT,		/* dyn_type, not an opcode.	*/
107
108	/*
109	 * These are really 'actions'.
110	 */
111
112	O_LOG,			/* ipfw_insn_log		*/
113	O_PROB,			/* u32 = match probability	*/
114
115	O_CHECK_STATE,		/* none				*/
116	O_ACCEPT,		/* none				*/
117	O_DENY,			/* none 			*/
118	O_REJECT,		/* arg1=icmp arg (same as deny)	*/
119	O_COUNT,		/* none				*/
120	O_SKIPTO,		/* arg1=next rule number	*/
121	O_PIPE,			/* arg1=pipe number		*/
122	O_QUEUE,		/* arg1=queue number		*/
123	O_DIVERT,		/* arg1=port number		*/
124	O_TEE,			/* arg1=port number		*/
125	O_FORWARD_IP,		/* fwd sockaddr			*/
126	O_FORWARD_MAC,		/* fwd mac			*/
127
128	/*
129	 * More opcodes.
130	 */
131	O_IPSEC,		/* has ipsec history 		*/
132	O_IP_SRC_LOOKUP,	/* arg1=table number, u32=value	*/
133	O_IP_DST_LOOKUP,	/* arg1=table number, u32=value	*/
134	O_ANTISPOOF,		/* none				*/
135	O_JAIL,			/* u32 = id			*/
136	O_ALTQ,			/* u32 = altq classif. qid	*/
137	O_DIVERTED,		/* arg1=bitmap (1:loop, 2:out)	*/
138	O_TCPDATALEN,		/* arg1 = tcp data len		*/
139	O_IP6_SRC,		/* address without mask		*/
140	O_IP6_SRC_ME,		/* my addresses			*/
141	O_IP6_SRC_MASK,		/* address with the mask	*/
142	O_IP6_DST,
143	O_IP6_DST_ME,
144	O_IP6_DST_MASK,
145	O_FLOW6ID,		/* for flow id tag in the ipv6 pkt */
146	O_ICMP6TYPE,		/* icmp6 packet type filtering	*/
147	O_EXT_HDR,		/* filtering for ipv6 extension header */
148	O_IP6,
149
150	/*
151	 * actions for ng_ipfw
152	 */
153	O_NETGRAPH,		/* send to ng_ipfw		*/
154	O_NGTEE,		/* copy to ng_ipfw		*/
155
156	O_LAST_OPCODE		/* not an opcode!		*/
157};
158
159/*
160 * The extension header are filtered only for presence using a bit
161 * vector with a flag for each header.
162 */
163#define EXT_FRAGMENT	0x1
164#define EXT_HOPOPTS	0x2
165#define EXT_ROUTING	0x4
166#define EXT_AH		0x8
167#define EXT_ESP		0x10
168
169/*
170 * Template for instructions.
171 *
172 * ipfw_insn is used for all instructions which require no operands,
173 * a single 16-bit value (arg1), or a couple of 8-bit values.
174 *
175 * For other instructions which require different/larger arguments
176 * we have derived structures, ipfw_insn_*.
177 *
178 * The size of the instruction (in 32-bit words) is in the low
179 * 6 bits of "len". The 2 remaining bits are used to implement
180 * NOT and OR on individual instructions. Given a type, you can
181 * compute the length to be put in "len" using F_INSN_SIZE(t)
182 *
183 * F_NOT	negates the match result of the instruction.
184 *
185 * F_OR		is used to build or blocks. By default, instructions
186 *		are evaluated as part of a logical AND. An "or" block
187 *		{ X or Y or Z } contains F_OR set in all but the last
188 *		instruction of the block. A match will cause the code
189 *		to skip past the last instruction of the block.
190 *
191 * NOTA BENE: in a couple of places we assume that
192 *	sizeof(ipfw_insn) == sizeof(u_int32_t)
193 * this needs to be fixed.
194 *
195 */
196typedef struct	_ipfw_insn {	/* template for instructions */
197	enum ipfw_opcodes	opcode:8;
198	u_int8_t	len;	/* numer of 32-byte words */
199#define	F_NOT		0x80
200#define	F_OR		0x40
201#define	F_LEN_MASK	0x3f
202#define	F_LEN(cmd)	((cmd)->len & F_LEN_MASK)
203
204	u_int16_t	arg1;
205} ipfw_insn;
206
207/*
208 * The F_INSN_SIZE(type) computes the size, in 4-byte words, of
209 * a given type.
210 */
211#define	F_INSN_SIZE(t)	((sizeof (t))/sizeof(u_int32_t))
212
213/*
214 * This is used to store an array of 16-bit entries (ports etc.)
215 */
216typedef struct	_ipfw_insn_u16 {
217	ipfw_insn o;
218	u_int16_t ports[2];	/* there may be more */
219} ipfw_insn_u16;
220
221/*
222 * This is used to store an array of 32-bit entries
223 * (uid, single IPv4 addresses etc.)
224 */
225typedef struct	_ipfw_insn_u32 {
226	ipfw_insn o;
227	u_int32_t d[1];	/* one or more */
228} ipfw_insn_u32;
229
230/*
231 * This is used to store IP addr-mask pairs.
232 */
233typedef struct	_ipfw_insn_ip {
234	ipfw_insn o;
235	struct in_addr	addr;
236	struct in_addr	mask;
237} ipfw_insn_ip;
238
239/*
240 * This is used to forward to a given address (ip).
241 */
242typedef struct  _ipfw_insn_sa {
243	ipfw_insn o;
244	struct sockaddr_in sa;
245} ipfw_insn_sa;
246
247/*
248 * This is used for MAC addr-mask pairs.
249 */
250typedef struct	_ipfw_insn_mac {
251	ipfw_insn o;
252	u_char addr[12];	/* dst[6] + src[6] */
253	u_char mask[12];	/* dst[6] + src[6] */
254} ipfw_insn_mac;
255
256/*
257 * This is used for interface match rules (recv xx, xmit xx).
258 */
259typedef struct	_ipfw_insn_if {
260	ipfw_insn o;
261	union {
262		struct in_addr ip;
263		int glob;
264	} p;
265	char name[IFNAMSIZ];
266} ipfw_insn_if;
267
268/*
269 * This is used for pipe and queue actions, which need to store
270 * a single pointer (which can have different size on different
271 * architectures.
272 * Note that, because of previous instructions, pipe_ptr might
273 * be unaligned in the overall structure, so it needs to be
274 * manipulated with care.
275 */
276typedef struct	_ipfw_insn_pipe {
277	ipfw_insn	o;
278	void		*pipe_ptr;	/* XXX */
279} ipfw_insn_pipe;
280
281/*
282 * This is used for storing an altq queue id number.
283 */
284typedef struct _ipfw_insn_altq {
285	ipfw_insn	o;
286	u_int32_t	qid;
287} ipfw_insn_altq;
288
289/*
290 * This is used for limit rules.
291 */
292typedef struct	_ipfw_insn_limit {
293	ipfw_insn o;
294	u_int8_t _pad;
295	u_int8_t limit_mask;	/* combination of DYN_* below	*/
296#define	DYN_SRC_ADDR	0x1
297#define	DYN_SRC_PORT	0x2
298#define	DYN_DST_ADDR	0x4
299#define	DYN_DST_PORT	0x8
300
301	u_int16_t conn_limit;
302} ipfw_insn_limit;
303
304/*
305 * This is used for log instructions.
306 */
307typedef struct  _ipfw_insn_log {
308        ipfw_insn o;
309	u_int32_t max_log;	/* how many do we log -- 0 = all */
310	u_int32_t log_left;	/* how many left to log 	*/
311} ipfw_insn_log;
312
313/* Apply ipv6 mask on ipv6 addr */
314#define APPLY_MASK(addr,mask)                          \
315    (addr)->__u6_addr.__u6_addr32[0] &= (mask)->__u6_addr.__u6_addr32[0]; \
316    (addr)->__u6_addr.__u6_addr32[1] &= (mask)->__u6_addr.__u6_addr32[1]; \
317    (addr)->__u6_addr.__u6_addr32[2] &= (mask)->__u6_addr.__u6_addr32[2]; \
318    (addr)->__u6_addr.__u6_addr32[3] &= (mask)->__u6_addr.__u6_addr32[3];
319
320/* Structure for ipv6 */
321typedef struct _ipfw_insn_ip6 {
322       ipfw_insn o;
323       struct in6_addr addr6;
324       struct in6_addr mask6;
325} ipfw_insn_ip6;
326
327/* Used to support icmp6 types */
328typedef struct _ipfw_insn_icmp6 {
329       ipfw_insn o;
330       uint32_t d[7]; /* XXX This number si related to the netinet/icmp6.h
331                       *     define ICMP6_MAXTYPE
332                       *     as follows: n = ICMP6_MAXTYPE/32 + 1
333                        *     Actually is 203
334                       */
335} ipfw_insn_icmp6;
336
337/*
338 * Here we have the structure representing an ipfw rule.
339 *
340 * It starts with a general area (with link fields and counters)
341 * followed by an array of one or more instructions, which the code
342 * accesses as an array of 32-bit values.
343 *
344 * Given a rule pointer  r:
345 *
346 *  r->cmd		is the start of the first instruction.
347 *  ACTION_PTR(r)	is the start of the first action (things to do
348 *			once a rule matched).
349 *
350 * When assembling instruction, remember the following:
351 *
352 *  + if a rule has a "keep-state" (or "limit") option, then the
353 *	first instruction (at r->cmd) MUST BE an O_PROBE_STATE
354 *  + if a rule has a "log" option, then the first action
355 *	(at ACTION_PTR(r)) MUST be O_LOG
356 *  + if a rule has an "altq" option, it comes after "log"
357 *
358 * NOTE: we use a simple linked list of rules because we never need
359 * 	to delete a rule without scanning the list. We do not use
360 *	queue(3) macros for portability and readability.
361 */
362
363struct ip_fw {
364	struct ip_fw	*next;		/* linked list of rules		*/
365	struct ip_fw	*next_rule;	/* ptr to next [skipto] rule	*/
366	/* 'next_rule' is used to pass up 'set_disable' status		*/
367
368	u_int16_t	act_ofs;	/* offset of action in 32-bit units */
369	u_int16_t	cmd_len;	/* # of 32-bit words in cmd	*/
370	u_int16_t	rulenum;	/* rule number			*/
371	u_int8_t	set;		/* rule set (0..31)		*/
372#define	RESVD_SET	31	/* set for default and persistent rules */
373	u_int8_t	_pad;		/* padding			*/
374
375	/* These fields are present in all rules.			*/
376	u_int64_t	pcnt;		/* Packet counter		*/
377	u_int64_t	bcnt;		/* Byte counter			*/
378	u_int32_t	timestamp;	/* tv_sec of last match		*/
379
380	ipfw_insn	cmd[1];		/* storage for commands		*/
381};
382
383#define ACTION_PTR(rule)				\
384	(ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) )
385
386#define RULESIZE(rule)  (sizeof(struct ip_fw) + \
387	((struct ip_fw *)(rule))->cmd_len * 4 - 4)
388
389/*
390 * This structure is used as a flow mask and a flow id for various
391 * parts of the code.
392 */
393struct ipfw_flow_id {
394	u_int32_t	dst_ip;
395	u_int32_t	src_ip;
396	u_int16_t	dst_port;
397	u_int16_t	src_port;
398	u_int8_t	proto;
399	u_int8_t	flags;	/* protocol-specific flags */
400	uint8_t		addr_type; /* 4 = ipv4, 6 = ipv6, 1=ether ? */
401	struct in6_addr dst_ip6;	/* could also store MAC addr! */
402	struct in6_addr src_ip6;
403	u_int32_t	flow_id6;
404};
405
406#define IS_IP6_FLOW_ID(id)	((id)->addr_type == 6)
407
408/*
409 * Dynamic ipfw rule.
410 */
411typedef struct _ipfw_dyn_rule ipfw_dyn_rule;
412
413struct _ipfw_dyn_rule {
414	ipfw_dyn_rule	*next;		/* linked list of rules.	*/
415	struct ip_fw *rule;		/* pointer to rule		*/
416	/* 'rule' is used to pass up the rule number (from the parent)	*/
417
418	ipfw_dyn_rule *parent;		/* pointer to parent rule	*/
419	u_int64_t	pcnt;		/* packet match counter		*/
420	u_int64_t	bcnt;		/* byte match counter		*/
421	struct ipfw_flow_id id;		/* (masked) flow id		*/
422	u_int32_t	expire;		/* expire time			*/
423	u_int32_t	bucket;		/* which bucket in hash table	*/
424	u_int32_t	state;		/* state of this rule (typically a
425					 * combination of TCP flags)
426					 */
427	u_int32_t	ack_fwd;	/* most recent ACKs in forward	*/
428	u_int32_t	ack_rev;	/* and reverse directions (used	*/
429					/* to generate keepalives)	*/
430	u_int16_t	dyn_type;	/* rule type			*/
431	u_int16_t	count;		/* refcount			*/
432};
433
434/*
435 * Definitions for IP option names.
436 */
437#define	IP_FW_IPOPT_LSRR	0x01
438#define	IP_FW_IPOPT_SSRR	0x02
439#define	IP_FW_IPOPT_RR		0x04
440#define	IP_FW_IPOPT_TS		0x08
441
442/*
443 * Definitions for TCP option names.
444 */
445#define	IP_FW_TCPOPT_MSS	0x01
446#define	IP_FW_TCPOPT_WINDOW	0x02
447#define	IP_FW_TCPOPT_SACK	0x04
448#define	IP_FW_TCPOPT_TS		0x08
449#define	IP_FW_TCPOPT_CC		0x10
450
451#define	ICMP_REJECT_RST		0x100	/* fake ICMP code (send a TCP RST) */
452
453/*
454 * These are used for lookup tables.
455 */
456typedef struct	_ipfw_table_entry {
457	in_addr_t	addr;		/* network address		*/
458	u_int32_t	value;		/* value			*/
459	u_int16_t	tbl;		/* table number			*/
460	u_int8_t	masklen;	/* mask length			*/
461} ipfw_table_entry;
462
463typedef struct	_ipfw_table {
464	u_int32_t	size;		/* size of entries in bytes	*/
465	u_int32_t	cnt;		/* # of entries			*/
466	u_int16_t	tbl;		/* table number			*/
467	ipfw_table_entry ent[0];	/* entries			*/
468} ipfw_table;
469
470/*
471 * Main firewall chains definitions and global var's definitions.
472 */
473#ifdef _KERNEL
474
475/* Return values from ipfw_chk() */
476enum {
477	IP_FW_PASS = 0,
478	IP_FW_DENY,
479	IP_FW_DIVERT,
480	IP_FW_TEE,
481	IP_FW_DUMMYNET,
482	IP_FW_NETGRAPH,
483	IP_FW_NGTEE,
484};
485
486/* flags for divert mtag */
487#define	IP_FW_DIVERT_LOOPBACK_FLAG	0x00080000
488#define	IP_FW_DIVERT_OUTPUT_FLAG	0x00100000
489
490/*
491 * Structure for collecting parameters to dummynet for ip6_output forwarding
492 */
493struct _ip6dn_args {
494       struct ip6_pktopts *opt_or;
495       struct route_in6 ro_or;
496       int flags_or;
497       struct ip6_moptions *im6o_or;
498       struct ifnet *origifp_or;
499       struct ifnet *ifp_or;
500       struct sockaddr_in6 dst_or;
501       u_long mtu_or;
502       struct route_in6 ro_pmtu_or;
503};
504
505/*
506 * Arguments for calling ipfw_chk() and dummynet_io(). We put them
507 * all into a structure because this way it is easier and more
508 * efficient to pass variables around and extend the interface.
509 */
510struct ip_fw_args {
511	struct mbuf	*m;		/* the mbuf chain		*/
512	struct ifnet	*oif;		/* output interface		*/
513	struct sockaddr_in *next_hop;	/* forward address		*/
514	struct ip_fw	*rule;		/* matching rule		*/
515	struct ether_header *eh;	/* for bridged packets		*/
516
517	int flags;			/* for dummynet			*/
518
519	struct ipfw_flow_id f_id;	/* grabbed from IP header	*/
520	u_int32_t	cookie;		/* a cookie depending on rule action */
521	struct inpcb	*inp;
522
523	struct _ip6dn_args	dummypar; /* dummynet->ip6_output */
524};
525
526/*
527 * Function definitions.
528 */
529
530/* Firewall hooks */
531struct sockopt;
532struct dn_flow_set;
533
534int ipfw_check_in(void *, struct mbuf **, struct ifnet *, int, struct inpcb *inp);
535int ipfw_check_out(void *, struct mbuf **, struct ifnet *, int, struct inpcb *inp);
536
537int ipfw_chk(struct ip_fw_args *);
538
539int ipfw_init(void);
540void ipfw_destroy(void);
541
542void flush_pipe_ptrs(struct dn_flow_set *match); /* used by dummynet */
543
544typedef int ip_fw_ctl_t(struct sockopt *);
545extern ip_fw_ctl_t *ip_fw_ctl_ptr;
546extern int fw_one_pass;
547extern int fw_enable;
548
549/* For kernel ipfw_ether and ipfw_bridge. */
550typedef	int ip_fw_chk_t(struct ip_fw_args *args);
551extern	ip_fw_chk_t	*ip_fw_chk_ptr;
552#define	IPFW_LOADED	(ip_fw_chk_ptr != NULL)
553
554#endif /* _KERNEL */
555#endif /* _IPFW2_H */
556