xref: /freebsd/sys/netinet/ip_fw.h (revision 2357939bc239bd5334a169b62313806178dd8f30)
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.
48  */
49 
50 enum ipfw_opcodes {		/* arguments (4 byte each)	*/
51 	O_NOP,
52 
53 	O_IP_SRC,		/* u32 = IP			*/
54 	O_IP_SRC_MASK,		/* ip = IP/mask			*/
55 	O_IP_SRC_ME,		/* none				*/
56 	O_IP_SRC_SET,		/* u32=base, arg1=len, bitmap	*/
57 
58 	O_IP_DST,		/* u32 = IP			*/
59 	O_IP_DST_MASK,		/* ip = IP/mask			*/
60 	O_IP_DST_ME,		/* none				*/
61 	O_IP_DST_SET,		/* u32=base, arg1=len, bitmap	*/
62 
63 	O_IP_SRCPORT,		/* (n)port list:mask 4 byte ea	*/
64 	O_IP_DSTPORT,		/* (n)port list:mask 4 byte ea	*/
65 	O_PROTO,		/* arg1=protocol		*/
66 
67 	O_MACADDR2,		/* 2 mac addr:mask		*/
68 	O_MAC_TYPE,		/* same as srcport		*/
69 
70 	O_LAYER2,		/* none				*/
71 	O_IN,			/* none				*/
72 	O_FRAG,			/* none				*/
73 
74 	O_RECV,			/* none				*/
75 	O_XMIT,			/* none				*/
76 	O_VIA,			/* none				*/
77 
78 	O_IPOPT,		/* arg1 = 2*u8 bitmap		*/
79 	O_IPLEN,		/* arg1 = len			*/
80 	O_IPID,			/* arg1 = id			*/
81 
82 	O_IPTOS,		/* arg1 = id			*/
83 	O_IPPRECEDENCE,		/* arg1 = precedence << 5	*/
84 	O_IPTTL,		/* arg1 = TTL			*/
85 
86 	O_IPVER,		/* arg1 = version		*/
87 	O_UID,			/* u32 = id			*/
88 	O_GID,			/* u32 = id			*/
89 	O_ESTAB,		/* none (tcp established)	*/
90 	O_TCPFLAGS,		/* arg1 = 2*u8 bitmap		*/
91 	O_TCPWIN,		/* arg1 = desired win		*/
92 	O_TCPSEQ,		/* u32 = desired seq.		*/
93 	O_TCPACK,		/* u32 = desired seq.		*/
94 	O_ICMPTYPE,		/* u32 = icmp bitmap		*/
95 	O_TCPOPTS,		/* arg1 = 2*u8 bitmap		*/
96 
97 	O_VERREVPATH,		/* none				*/
98 	O_VERSRCREACH,		/* none				*/
99 
100 	O_PROBE_STATE,		/* none				*/
101 	O_KEEP_STATE,		/* none				*/
102 	O_LIMIT,		/* ipfw_insn_limit		*/
103 	O_LIMIT_PARENT,		/* dyn_type, not an opcode.	*/
104 
105 	/*
106 	 * These are really 'actions'.
107 	 */
108 
109 	O_LOG,			/* ipfw_insn_log		*/
110 	O_PROB,			/* u32 = match probability	*/
111 
112 	O_CHECK_STATE,		/* none				*/
113 	O_ACCEPT,		/* none				*/
114 	O_DENY,			/* none 			*/
115 	O_REJECT,		/* arg1=icmp arg (same as deny)	*/
116 	O_COUNT,		/* none				*/
117 	O_SKIPTO,		/* arg1=next rule number	*/
118 	O_PIPE,			/* arg1=pipe number		*/
119 	O_QUEUE,		/* arg1=queue number		*/
120 	O_DIVERT,		/* arg1=port number		*/
121 	O_TEE,			/* arg1=port number		*/
122 	O_FORWARD_IP,		/* fwd sockaddr			*/
123 	O_FORWARD_MAC,		/* fwd mac			*/
124 
125 	/*
126 	 * More opcodes.
127 	 */
128 	O_IPSEC,		/* has ipsec history 		*/
129 
130 	O_LAST_OPCODE		/* not an opcode!		*/
131 };
132 
133 /*
134  * Template for instructions.
135  *
136  * ipfw_insn is used for all instructions which require no operands,
137  * a single 16-bit value (arg1), or a couple of 8-bit values.
138  *
139  * For other instructions which require different/larger arguments
140  * we have derived structures, ipfw_insn_*.
141  *
142  * The size of the instruction (in 32-bit words) is in the low
143  * 6 bits of "len". The 2 remaining bits are used to implement
144  * NOT and OR on individual instructions. Given a type, you can
145  * compute the length to be put in "len" using F_INSN_SIZE(t)
146  *
147  * F_NOT	negates the match result of the instruction.
148  *
149  * F_OR		is used to build or blocks. By default, instructions
150  *		are evaluated as part of a logical AND. An "or" block
151  *		{ X or Y or Z } contains F_OR set in all but the last
152  *		instruction of the block. A match will cause the code
153  *		to skip past the last instruction of the block.
154  *
155  * NOTA BENE: in a couple of places we assume that
156  *	sizeof(ipfw_insn) == sizeof(u_int32_t)
157  * this needs to be fixed.
158  *
159  */
160 typedef struct	_ipfw_insn {	/* template for instructions */
161 	enum ipfw_opcodes	opcode:8;
162 	u_int8_t	len;	/* numer of 32-byte words */
163 #define	F_NOT		0x80
164 #define	F_OR		0x40
165 #define	F_LEN_MASK	0x3f
166 #define	F_LEN(cmd)	((cmd)->len & F_LEN_MASK)
167 
168 	u_int16_t	arg1;
169 } ipfw_insn;
170 
171 /*
172  * The F_INSN_SIZE(type) computes the size, in 4-byte words, of
173  * a given type.
174  */
175 #define	F_INSN_SIZE(t)	((sizeof (t))/sizeof(u_int32_t))
176 
177 /*
178  * This is used to store an array of 16-bit entries (ports etc.)
179  */
180 typedef struct	_ipfw_insn_u16 {
181 	ipfw_insn o;
182 	u_int16_t ports[2];	/* there may be more */
183 } ipfw_insn_u16;
184 
185 /*
186  * This is used to store an array of 32-bit entries
187  * (uid, single IPv4 addresses etc.)
188  */
189 typedef struct	_ipfw_insn_u32 {
190 	ipfw_insn o;
191 	u_int32_t d[1];	/* one or more */
192 } ipfw_insn_u32;
193 
194 /*
195  * This is used to store IP addr-mask pairs.
196  */
197 typedef struct	_ipfw_insn_ip {
198 	ipfw_insn o;
199 	struct in_addr	addr;
200 	struct in_addr	mask;
201 } ipfw_insn_ip;
202 
203 /*
204  * This is used to forward to a given address (ip).
205  */
206 typedef struct  _ipfw_insn_sa {
207 	ipfw_insn o;
208 	struct sockaddr_in sa;
209 } ipfw_insn_sa;
210 
211 /*
212  * This is used for MAC addr-mask pairs.
213  */
214 typedef struct	_ipfw_insn_mac {
215 	ipfw_insn o;
216 	u_char addr[12];	/* dst[6] + src[6] */
217 	u_char mask[12];	/* dst[6] + src[6] */
218 } ipfw_insn_mac;
219 
220 /*
221  * This is used for interface match rules (recv xx, xmit xx).
222  */
223 typedef struct	_ipfw_insn_if {
224 	ipfw_insn o;
225 	union {
226 		struct in_addr ip;
227 		int glob;
228 	} p;
229 	char name[IFNAMSIZ];
230 } ipfw_insn_if;
231 
232 /*
233  * This is used for pipe and queue actions, which need to store
234  * a single pointer (which can have different size on different
235  * architectures.
236  * Note that, because of previous instructions, pipe_ptr might
237  * be unaligned in the overall structure, so it needs to be
238  * manipulated with care.
239  */
240 typedef struct	_ipfw_insn_pipe {
241 	ipfw_insn	o;
242 	void		*pipe_ptr;	/* XXX */
243 } ipfw_insn_pipe;
244 
245 /*
246  * This is used for limit rules.
247  */
248 typedef struct	_ipfw_insn_limit {
249 	ipfw_insn o;
250 	u_int8_t _pad;
251 	u_int8_t limit_mask;	/* combination of DYN_* below	*/
252 #define	DYN_SRC_ADDR	0x1
253 #define	DYN_SRC_PORT	0x2
254 #define	DYN_DST_ADDR	0x4
255 #define	DYN_DST_PORT	0x8
256 
257 	u_int16_t conn_limit;
258 } ipfw_insn_limit;
259 
260 /*
261  * This is used for log instructions.
262  */
263 typedef struct  _ipfw_insn_log {
264         ipfw_insn o;
265 	u_int32_t max_log;	/* how many do we log -- 0 = all */
266 	u_int32_t log_left;	/* how many left to log 	*/
267 } ipfw_insn_log;
268 
269 /*
270  * Here we have the structure representing an ipfw rule.
271  *
272  * It starts with a general area (with link fields and counters)
273  * followed by an array of one or more instructions, which the code
274  * accesses as an array of 32-bit values.
275  *
276  * Given a rule pointer  r:
277  *
278  *  r->cmd		is the start of the first instruction.
279  *  ACTION_PTR(r)	is the start of the first action (things to do
280  *			once a rule matched).
281  *
282  * When assembling instruction, remember the following:
283  *
284  *  + if a rule has a "keep-state" (or "limit") option, then the
285  *	first instruction (at r->cmd) MUST BE an O_PROBE_STATE
286  *  + if a rule has a "log" option, then the first action
287  *	(at ACTION_PTR(r)) MUST be O_LOG
288  *
289  * NOTE: we use a simple linked list of rules because we never need
290  * 	to delete a rule without scanning the list. We do not use
291  *	queue(3) macros for portability and readability.
292  */
293 
294 struct ip_fw {
295 	struct ip_fw	*next;		/* linked list of rules		*/
296 	struct ip_fw	*next_rule;	/* ptr to next [skipto] rule	*/
297 	/* 'next_rule' is used to pass up 'set_disable' status		*/
298 
299 	u_int16_t	act_ofs;	/* offset of action in 32-bit units */
300 	u_int16_t	cmd_len;	/* # of 32-bit words in cmd	*/
301 	u_int16_t	rulenum;	/* rule number			*/
302 	u_int8_t	set;		/* rule set (0..31)		*/
303 #define	RESVD_SET	31	/* set for default and persistent rules */
304 	u_int8_t	_pad;		/* padding			*/
305 
306 	/* These fields are present in all rules.			*/
307 	u_int64_t	pcnt;		/* Packet counter		*/
308 	u_int64_t	bcnt;		/* Byte counter			*/
309 	u_int32_t	timestamp;	/* tv_sec of last match		*/
310 
311 	ipfw_insn	cmd[1];		/* storage for commands		*/
312 };
313 
314 #define ACTION_PTR(rule)				\
315 	(ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) )
316 
317 #define RULESIZE(rule)  (sizeof(struct ip_fw) + \
318 	((struct ip_fw *)(rule))->cmd_len * 4 - 4)
319 
320 /*
321  * This structure is used as a flow mask and a flow id for various
322  * parts of the code.
323  */
324 struct ipfw_flow_id {
325 	u_int32_t	dst_ip;
326 	u_int32_t	src_ip;
327 	u_int16_t	dst_port;
328 	u_int16_t	src_port;
329 	u_int8_t	proto;
330 	u_int8_t	flags;	/* protocol-specific flags */
331 };
332 
333 /*
334  * Dynamic ipfw rule.
335  */
336 typedef struct _ipfw_dyn_rule ipfw_dyn_rule;
337 
338 struct _ipfw_dyn_rule {
339 	ipfw_dyn_rule	*next;		/* linked list of rules.	*/
340 	struct ip_fw *rule;		/* pointer to rule		*/
341 	/* 'rule' is used to pass up the rule number (from the parent)	*/
342 
343 	ipfw_dyn_rule *parent;		/* pointer to parent rule	*/
344 	u_int64_t	pcnt;		/* packet match counter		*/
345 	u_int64_t	bcnt;		/* byte match counter		*/
346 	struct ipfw_flow_id id;		/* (masked) flow id		*/
347 	u_int32_t	expire;		/* expire time			*/
348 	u_int32_t	bucket;		/* which bucket in hash table	*/
349 	u_int32_t	state;		/* state of this rule (typically a
350 					 * combination of TCP flags)
351 					 */
352 	u_int32_t	ack_fwd;	/* most recent ACKs in forward	*/
353 	u_int32_t	ack_rev;	/* and reverse directions (used	*/
354 					/* to generate keepalives)	*/
355 	u_int16_t	dyn_type;	/* rule type			*/
356 	u_int16_t	count;		/* refcount			*/
357 };
358 
359 /*
360  * Definitions for IP option names.
361  */
362 #define	IP_FW_IPOPT_LSRR	0x01
363 #define	IP_FW_IPOPT_SSRR	0x02
364 #define	IP_FW_IPOPT_RR		0x04
365 #define	IP_FW_IPOPT_TS		0x08
366 
367 /*
368  * Definitions for TCP option names.
369  */
370 #define	IP_FW_TCPOPT_MSS	0x01
371 #define	IP_FW_TCPOPT_WINDOW	0x02
372 #define	IP_FW_TCPOPT_SACK	0x04
373 #define	IP_FW_TCPOPT_TS		0x08
374 #define	IP_FW_TCPOPT_CC		0x10
375 
376 #define	ICMP_REJECT_RST		0x100	/* fake ICMP code (send a TCP RST) */
377 
378 /*
379  * Main firewall chains definitions and global var's definitions.
380  */
381 #ifdef _KERNEL
382 
383 #define	IP_FW_PORT_DYNT_FLAG	0x10000
384 #define	IP_FW_PORT_TEE_FLAG	0x20000
385 #define	IP_FW_PORT_DENY_FLAG	0x40000
386 
387 /*
388  * Arguments for calling ipfw_chk() and dummynet_io(). We put them
389  * all into a structure because this way it is easier and more
390  * efficient to pass variables around and extend the interface.
391  */
392 struct ip_fw_args {
393 	struct mbuf	*m;		/* the mbuf chain		*/
394 	struct ifnet	*oif;		/* output interface		*/
395 	struct sockaddr_in *next_hop;	/* forward address		*/
396 	struct ip_fw	*rule;		/* matching rule		*/
397 	struct ether_header *eh;	/* for bridged packets		*/
398 
399 	struct route	*ro;		/* for dummynet			*/
400 	struct sockaddr_in *dst;	/* for dummynet			*/
401 	int flags;			/* for dummynet			*/
402 
403 	struct ipfw_flow_id f_id;	/* grabbed from IP header	*/
404 	u_int32_t	retval;
405 };
406 
407 /*
408  * Function definitions.
409  */
410 
411 /* Firewall hooks */
412 struct sockopt;
413 struct dn_flow_set;
414 
415 void flush_pipe_ptrs(struct dn_flow_set *match); /* used by dummynet */
416 
417 typedef int ip_fw_chk_t (struct ip_fw_args *args);
418 typedef int ip_fw_ctl_t (struct sockopt *);
419 extern ip_fw_chk_t *ip_fw_chk_ptr;
420 extern ip_fw_ctl_t *ip_fw_ctl_ptr;
421 extern int fw_one_pass;
422 extern int fw_enable;
423 #define	IPFW_LOADED	(ip_fw_chk_ptr != NULL)
424 #endif /* _KERNEL */
425 
426 #endif /* _IPFW2_H */
427