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