xref: /freebsd/sys/netinet/ip_fw.h (revision c37420b0d5b3b6ef875fbf0b84a13f6f09be56d6)
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  * The kernel representation of ipfw rules is made of a list of
33  * 'instructions' (for all practical purposes equivalent to BPF
34  * 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 
53 enum 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 	O_ANTISPOOF,		/* 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_JAIL,			/* u32 = id			*/
136 
137 	O_LAST_OPCODE		/* not an opcode!		*/
138 };
139 
140 /*
141  * Template for instructions.
142  *
143  * ipfw_insn is used for all instructions which require no operands,
144  * a single 16-bit value (arg1), or a couple of 8-bit values.
145  *
146  * For other instructions which require different/larger arguments
147  * we have derived structures, ipfw_insn_*.
148  *
149  * The size of the instruction (in 32-bit words) is in the low
150  * 6 bits of "len". The 2 remaining bits are used to implement
151  * NOT and OR on individual instructions. Given a type, you can
152  * compute the length to be put in "len" using F_INSN_SIZE(t)
153  *
154  * F_NOT	negates the match result of the instruction.
155  *
156  * F_OR		is used to build or blocks. By default, instructions
157  *		are evaluated as part of a logical AND. An "or" block
158  *		{ X or Y or Z } contains F_OR set in all but the last
159  *		instruction of the block. A match will cause the code
160  *		to skip past the last instruction of the block.
161  *
162  * NOTA BENE: in a couple of places we assume that
163  *	sizeof(ipfw_insn) == sizeof(u_int32_t)
164  * this needs to be fixed.
165  *
166  */
167 typedef struct	_ipfw_insn {	/* template for instructions */
168 	enum ipfw_opcodes	opcode:8;
169 	u_int8_t	len;	/* numer of 32-byte words */
170 #define	F_NOT		0x80
171 #define	F_OR		0x40
172 #define	F_LEN_MASK	0x3f
173 #define	F_LEN(cmd)	((cmd)->len & F_LEN_MASK)
174 
175 	u_int16_t	arg1;
176 } ipfw_insn;
177 
178 /*
179  * The F_INSN_SIZE(type) computes the size, in 4-byte words, of
180  * a given type.
181  */
182 #define	F_INSN_SIZE(t)	((sizeof (t))/sizeof(u_int32_t))
183 
184 /*
185  * This is used to store an array of 16-bit entries (ports etc.)
186  */
187 typedef struct	_ipfw_insn_u16 {
188 	ipfw_insn o;
189 	u_int16_t ports[2];	/* there may be more */
190 } ipfw_insn_u16;
191 
192 /*
193  * This is used to store an array of 32-bit entries
194  * (uid, single IPv4 addresses etc.)
195  */
196 typedef struct	_ipfw_insn_u32 {
197 	ipfw_insn o;
198 	u_int32_t d[1];	/* one or more */
199 } ipfw_insn_u32;
200 
201 /*
202  * This is used to store IP addr-mask pairs.
203  */
204 typedef struct	_ipfw_insn_ip {
205 	ipfw_insn o;
206 	struct in_addr	addr;
207 	struct in_addr	mask;
208 } ipfw_insn_ip;
209 
210 /*
211  * This is used to forward to a given address (ip).
212  */
213 typedef struct  _ipfw_insn_sa {
214 	ipfw_insn o;
215 	struct sockaddr_in sa;
216 } ipfw_insn_sa;
217 
218 /*
219  * This is used for MAC addr-mask pairs.
220  */
221 typedef struct	_ipfw_insn_mac {
222 	ipfw_insn o;
223 	u_char addr[12];	/* dst[6] + src[6] */
224 	u_char mask[12];	/* dst[6] + src[6] */
225 } ipfw_insn_mac;
226 
227 /*
228  * This is used for interface match rules (recv xx, xmit xx).
229  */
230 typedef struct	_ipfw_insn_if {
231 	ipfw_insn o;
232 	union {
233 		struct in_addr ip;
234 		int glob;
235 	} p;
236 	char name[IFNAMSIZ];
237 } ipfw_insn_if;
238 
239 /*
240  * This is used for pipe and queue actions, which need to store
241  * a single pointer (which can have different size on different
242  * architectures.
243  * Note that, because of previous instructions, pipe_ptr might
244  * be unaligned in the overall structure, so it needs to be
245  * manipulated with care.
246  */
247 typedef struct	_ipfw_insn_pipe {
248 	ipfw_insn	o;
249 	void		*pipe_ptr;	/* XXX */
250 } ipfw_insn_pipe;
251 
252 /*
253  * This is used for limit rules.
254  */
255 typedef struct	_ipfw_insn_limit {
256 	ipfw_insn o;
257 	u_int8_t _pad;
258 	u_int8_t limit_mask;	/* combination of DYN_* below	*/
259 #define	DYN_SRC_ADDR	0x1
260 #define	DYN_SRC_PORT	0x2
261 #define	DYN_DST_ADDR	0x4
262 #define	DYN_DST_PORT	0x8
263 
264 	u_int16_t conn_limit;
265 } ipfw_insn_limit;
266 
267 /*
268  * This is used for log instructions.
269  */
270 typedef struct  _ipfw_insn_log {
271         ipfw_insn o;
272 	u_int32_t max_log;	/* how many do we log -- 0 = all */
273 	u_int32_t log_left;	/* how many left to log 	*/
274 } ipfw_insn_log;
275 
276 /*
277  * Here we have the structure representing an ipfw rule.
278  *
279  * It starts with a general area (with link fields and counters)
280  * followed by an array of one or more instructions, which the code
281  * accesses as an array of 32-bit values.
282  *
283  * Given a rule pointer  r:
284  *
285  *  r->cmd		is the start of the first instruction.
286  *  ACTION_PTR(r)	is the start of the first action (things to do
287  *			once a rule matched).
288  *
289  * When assembling instruction, remember the following:
290  *
291  *  + if a rule has a "keep-state" (or "limit") option, then the
292  *	first instruction (at r->cmd) MUST BE an O_PROBE_STATE
293  *  + if a rule has a "log" option, then the first action
294  *	(at ACTION_PTR(r)) MUST be O_LOG
295  *
296  * NOTE: we use a simple linked list of rules because we never need
297  * 	to delete a rule without scanning the list. We do not use
298  *	queue(3) macros for portability and readability.
299  */
300 
301 struct ip_fw {
302 	struct ip_fw	*next;		/* linked list of rules		*/
303 	struct ip_fw	*next_rule;	/* ptr to next [skipto] rule	*/
304 	/* 'next_rule' is used to pass up 'set_disable' status		*/
305 
306 	u_int16_t	act_ofs;	/* offset of action in 32-bit units */
307 	u_int16_t	cmd_len;	/* # of 32-bit words in cmd	*/
308 	u_int16_t	rulenum;	/* rule number			*/
309 	u_int8_t	set;		/* rule set (0..31)		*/
310 #define	RESVD_SET	31	/* set for default and persistent rules */
311 	u_int8_t	_pad;		/* padding			*/
312 
313 	/* These fields are present in all rules.			*/
314 	u_int64_t	pcnt;		/* Packet counter		*/
315 	u_int64_t	bcnt;		/* Byte counter			*/
316 	u_int32_t	timestamp;	/* tv_sec of last match		*/
317 
318 	ipfw_insn	cmd[1];		/* storage for commands		*/
319 };
320 
321 #define ACTION_PTR(rule)				\
322 	(ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) )
323 
324 #define RULESIZE(rule)  (sizeof(struct ip_fw) + \
325 	((struct ip_fw *)(rule))->cmd_len * 4 - 4)
326 
327 /*
328  * This structure is used as a flow mask and a flow id for various
329  * parts of the code.
330  */
331 struct ipfw_flow_id {
332 	u_int32_t	dst_ip;
333 	u_int32_t	src_ip;
334 	u_int16_t	dst_port;
335 	u_int16_t	src_port;
336 	u_int8_t	proto;
337 	u_int8_t	flags;	/* protocol-specific flags */
338 };
339 
340 /*
341  * Dynamic ipfw rule.
342  */
343 typedef struct _ipfw_dyn_rule ipfw_dyn_rule;
344 
345 struct _ipfw_dyn_rule {
346 	ipfw_dyn_rule	*next;		/* linked list of rules.	*/
347 	struct ip_fw *rule;		/* pointer to rule		*/
348 	/* 'rule' is used to pass up the rule number (from the parent)	*/
349 
350 	ipfw_dyn_rule *parent;		/* pointer to parent rule	*/
351 	u_int64_t	pcnt;		/* packet match counter		*/
352 	u_int64_t	bcnt;		/* byte match counter		*/
353 	struct ipfw_flow_id id;		/* (masked) flow id		*/
354 	u_int32_t	expire;		/* expire time			*/
355 	u_int32_t	bucket;		/* which bucket in hash table	*/
356 	u_int32_t	state;		/* state of this rule (typically a
357 					 * combination of TCP flags)
358 					 */
359 	u_int32_t	ack_fwd;	/* most recent ACKs in forward	*/
360 	u_int32_t	ack_rev;	/* and reverse directions (used	*/
361 					/* to generate keepalives)	*/
362 	u_int16_t	dyn_type;	/* rule type			*/
363 	u_int16_t	count;		/* refcount			*/
364 };
365 
366 /*
367  * Definitions for IP option names.
368  */
369 #define	IP_FW_IPOPT_LSRR	0x01
370 #define	IP_FW_IPOPT_SSRR	0x02
371 #define	IP_FW_IPOPT_RR		0x04
372 #define	IP_FW_IPOPT_TS		0x08
373 
374 /*
375  * Definitions for TCP option names.
376  */
377 #define	IP_FW_TCPOPT_MSS	0x01
378 #define	IP_FW_TCPOPT_WINDOW	0x02
379 #define	IP_FW_TCPOPT_SACK	0x04
380 #define	IP_FW_TCPOPT_TS		0x08
381 #define	IP_FW_TCPOPT_CC		0x10
382 
383 #define	ICMP_REJECT_RST		0x100	/* fake ICMP code (send a TCP RST) */
384 
385 /*
386  * These are used for lookup tables.
387  */
388 typedef struct	_ipfw_table_entry {
389 	in_addr_t	addr;		/* network address		*/
390 	u_int32_t	value;		/* value			*/
391 	u_int16_t	tbl;		/* table number			*/
392 	u_int8_t	masklen;	/* mask length			*/
393 } ipfw_table_entry;
394 
395 typedef struct	_ipfw_table {
396 	u_int32_t	size;		/* size of entries in bytes	*/
397 	u_int32_t	cnt;		/* # of entries			*/
398 	u_int16_t	tbl;		/* table number			*/
399 	ipfw_table_entry ent[0];	/* entries			*/
400 } ipfw_table;
401 
402 /*
403  * Main firewall chains definitions and global var's definitions.
404  */
405 #ifdef _KERNEL
406 
407 #define	IP_FW_PORT_DYNT_FLAG	0x10000
408 #define	IP_FW_PORT_TEE_FLAG	0x20000
409 #define	IP_FW_PORT_DENY_FLAG	0x40000
410 
411 /*
412  * Arguments for calling ipfw_chk() and dummynet_io(). We put them
413  * all into a structure because this way it is easier and more
414  * efficient to pass variables around and extend the interface.
415  */
416 struct ip_fw_args {
417 	struct mbuf	*m;		/* the mbuf chain		*/
418 	struct ifnet	*oif;		/* output interface		*/
419 	struct sockaddr_in *next_hop;	/* forward address		*/
420 	struct ip_fw	*rule;		/* matching rule		*/
421 	struct ether_header *eh;	/* for bridged packets		*/
422 
423 	struct route	*ro;		/* for dummynet			*/
424 	struct sockaddr_in *dst;	/* for dummynet			*/
425 	int flags;			/* for dummynet			*/
426 
427 	struct ipfw_flow_id f_id;	/* grabbed from IP header	*/
428 	u_int32_t	retval;
429 };
430 
431 /*
432  * Function definitions.
433  */
434 
435 /* Firewall hooks */
436 struct sockopt;
437 struct dn_flow_set;
438 
439 void flush_pipe_ptrs(struct dn_flow_set *match); /* used by dummynet */
440 
441 typedef int ip_fw_chk_t (struct ip_fw_args *args);
442 typedef int ip_fw_ctl_t (struct sockopt *);
443 extern ip_fw_chk_t *ip_fw_chk_ptr;
444 extern ip_fw_ctl_t *ip_fw_ctl_ptr;
445 extern int fw_one_pass;
446 extern int fw_enable;
447 #define	IPFW_LOADED	(ip_fw_chk_ptr != NULL)
448 #endif /* _KERNEL */
449 
450 #endif /* _IPFW2_H */
451