xref: /dragonfly/sys/net/ipfw/ip_fw2.h (revision ee46a572)
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: src/sys/netinet/ip_fw2.h,v 1.1.2.2 2002/08/16 11:03:11 luigi Exp $
26  */
27 
28 #ifndef _IPFW2_H
29 #define _IPFW2_H
30 
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 metatada) 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  *
41  * "enum ipfw_opcodes" are the opcodes supported. We can have up
42  * to 256 different opcodes.
43  */
44 
45 enum ipfw_opcodes {		/* arguments (4 byte each)	*/
46 	O_NOP,
47 
48 	O_IP_SRC,		/* u32 = IP			*/
49 	O_IP_SRC_MASK,		/* ip = IP/mask			*/
50 	O_IP_SRC_ME,		/* none				*/
51 	O_IP_SRC_SET,		/* u32=base, arg1=len, bitmap	*/
52 
53 	O_IP_DST,		/* u32 = IP			*/
54 	O_IP_DST_MASK,		/* ip = IP/mask			*/
55 	O_IP_DST_ME,		/* none				*/
56 	O_IP_DST_SET,		/* u32=base, arg1=len, bitmap	*/
57 
58 	O_IP_SRCPORT,		/* (n)port list:mask 4 byte ea	*/
59 	O_IP_DSTPORT,		/* (n)port list:mask 4 byte ea	*/
60 	O_PROTO,		/* arg1=protocol		*/
61 
62 	O_MACADDR2,		/* 2 mac addr:mask		*/
63 	O_MAC_TYPE,		/* same as srcport		*/
64 
65 	O_LAYER2,		/* none				*/
66 	O_IN,			/* none				*/
67 	O_FRAG,			/* none				*/
68 
69 	O_RECV,			/* none				*/
70 	O_XMIT,			/* none				*/
71 	O_VIA,			/* none				*/
72 
73 	O_IPOPT,		/* arg1 = 2*u8 bitmap		*/
74 	O_IPLEN,		/* arg1 = len			*/
75 	O_IPID,			/* arg1 = id			*/
76 
77 	O_IPTOS,		/* arg1 = id			*/
78 	O_IPPRECEDENCE,		/* arg1 = precedence << 5	*/
79 	O_IPTTL,		/* arg1 = TTL			*/
80 
81 	O_IPVER,		/* arg1 = version		*/
82 	O_UID,			/* u32 = id			*/
83 	O_GID,			/* u32 = id			*/
84 	O_ESTAB,		/* none (tcp established)	*/
85 	O_TCPFLAGS,		/* arg1 = 2*u8 bitmap		*/
86 	O_TCPWIN,		/* arg1 = desired win		*/
87 	O_TCPSEQ,		/* u32 = desired seq.		*/
88 	O_TCPACK,		/* u32 = desired seq.		*/
89 	O_ICMPTYPE,		/* 1*u32 = icmp type bitmap	*/
90 	O_TCPOPTS,		/* arg1 = 2*u8 bitmap		*/
91 
92 	/* States. */
93 	O_PROBE_STATE,		/* none				*/
94 	O_KEEP_STATE,		/* none				*/
95 	O_LIMIT,		/* ipfw_insn_limit		*/
96 	O_LIMIT_PARENT,		/* dyn_type, not an opcode.	*/
97 
98 	/* Actions. */
99 	O_LOG,			/* ipfw_insn_log		*/
100 	O_PROB,			/* u32 = match probability	*/
101 	O_CHECK_STATE,		/* none				*/
102 	O_ACCEPT,		/* none				*/
103 	O_DENY,			/* none 			*/
104 	O_REJECT,		/* arg1=icmp arg (same as deny)	*/
105 	O_COUNT,		/* none				*/
106 	O_SKIPTO,		/* arg1=next rule number	*/
107 	O_PIPE,			/* arg1=pipe number		*/
108 	O_QUEUE,		/* arg1=queue number		*/
109 	O_DIVERT,		/* arg1=port number		*/
110 	O_TEE,			/* arg1=port number		*/
111 	O_FORWARD_IP,		/* fwd sockaddr			*/
112 	O_FORWARD_MAC,		/* fwd mac			*/
113 
114 	/* Table based filters. */
115 	O_IP_SRC_TABLE,		/* arg1 = tableid		*/
116 	O_IP_DST_TABLE,		/* arg1 = tableid		*/
117 
118 	/* Action. */
119 	O_DEFRAG,		/* none				*/
120 
121 	/* Filters. */
122 	O_IPFRAG,		/* none				*/
123 	O_IP_SRC_IFIP,		/* ipfw_insn_ifip		*/
124 	O_IP_DST_IFIP,		/* ipfw_insn_ifip		*/
125 
126 	/* Translates. */
127 	O_REDIRECT,		/* ipfw_insn_rdr		*/
128 	O_RESERVED1,		/* reserved for NAT		*/
129 
130 	O_ICMPCODE,		/* 1*u32 = icmp code bitmap	*/
131 
132 	O_LAST_OPCODE		/* not an opcode!		*/
133 };
134 #ifdef _KERNEL
135 CTASSERT(O_LAST_OPCODE <= 256);
136 #endif
137 
138 /*
139  * Template for instructions.
140  *
141  * ipfw_insn is used for all instructions which require no operands,
142  * a single 16-bit value (arg1), or a couple of 8-bit values.
143  *
144  * For other instructions which require different/larger arguments
145  * we have derived structures, ipfw_insn_*.
146  *
147  * The size of the instruction (in 32-bit words) is in the low
148  * 6 bits of "len". The 2 remaining bits are used to implement
149  * NOT and OR on individual instructions. Given a type, you can
150  * compute the length to be put in "len" using F_INSN_SIZE(t)
151  *
152  * F_NOT	negates the match result of the instruction.
153  *
154  * F_OR		is used to build or blocks. By default, instructions
155  *		are evaluated as part of a logical AND. An "or" block
156  *		{ X or Y or Z } contains F_OR set in all but the last
157  *		instruction of the block. A match will cause the code
158  *		to skip past the last instruction of the block.
159  *
160  * NOTA BENE: in a couple of places we assume that
161  *	sizeof(ipfw_insn) == sizeof(uint32_t)
162  * this needs to be fixed.
163  *
164  */
165 typedef struct	_ipfw_insn {	/* template for instructions */
166 	enum ipfw_opcodes	opcode:8;
167 	uint8_t		len;	/* numer of 32-byte words */
168 #define	F_NOT		0x80
169 #define	F_OR		0x40
170 #define	F_LEN_MASK	0x3f
171 #define	F_LEN(cmd)	((cmd)->len & F_LEN_MASK)
172 
173 	uint16_t	arg1;
174 } ipfw_insn;
175 
176 #define IPFW_INSN_SIZE_MAX	63	/* unit: uint32_t */
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(uint32_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 	uint16_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 	uint32_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  */
244 typedef struct	_ipfw_insn_pipe {
245 	ipfw_insn	o;
246 	void		*pipe_ptr;
247 } ipfw_insn_pipe;
248 
249 /*
250  * This is used for limit rules.
251  */
252 typedef struct	_ipfw_insn_limit {
253 	ipfw_insn o;
254 	uint8_t _pad;
255 	uint8_t limit_mask;	/* combination of DYN_* below	*/
256 #define	DYN_SRC_ADDR	0x1
257 #define	DYN_SRC_PORT	0x2
258 #define	DYN_DST_ADDR	0x4
259 #define	DYN_DST_PORT	0x8
260 
261 	uint16_t conn_limit;
262 } ipfw_insn_limit;
263 
264 /*
265  * This is used for log instructions
266  */
267 typedef struct  _ipfw_insn_log {
268         ipfw_insn o;
269 	uint32_t max_log;	/* how many do we log -- 0 = all */
270 	uint32_t log_left;	/* how many left to log 	*/
271 } ipfw_insn_log;
272 
273 /*
274  * This is used by O_IP_{SRC,DST}_IFIP.
275  */
276 typedef struct _ipfw_insn_ifip {
277 	ipfw_insn o;		/* arg1 & 0x1, addr is valid */
278 #define IPFW_IFIP_VALID		0x0001
279 #define IPFW_IFIP_NET		0x0002
280 #define IPFW_IFIP_SETTINGS	IPFW_IFIP_NET
281 	char ifname[IFNAMSIZ];
282 	struct in_addr addr;
283 	struct in_addr mask;
284 } ipfw_insn_ifip;
285 
286 /*
287  * This is used by O_REDIRECT.
288  */
289 typedef struct _ipfw_insn_rdr {
290 	ipfw_insn o;
291 	struct in_addr addr;
292 	uint16_t port;		/* network byte order, 0 = same port */
293 	uint16_t set;		/* reserved for set, 0xffff */
294 } ipfw_insn_rdr;
295 
296 #ifdef _KERNEL
297 
298 /*
299  * Here we have the structure representing an ipfw rule.
300  *
301  * It starts with a general area (with link fields and counters)
302  * followed by an array of one or more instructions, which the code
303  * accesses as an array of 32-bit values.
304  *
305  * Given a rule pointer  r:
306  *
307  *  r->cmd		is the start of the first instruction.
308  *  ACTION_PTR(r)	is the start of the first action (things to do
309  *			once a rule matched).
310  *
311  * When assembling instruction, remember the following:
312  *
313  *  + if a rule has a "keep-state" (or "limit") option, then the
314  *	first instruction (at r->cmd) MUST BE an O_PROBE_STATE
315  *  + if a rule has a "log" option, then the first action
316  *	(at ACTION_PTR(r)) MUST be O_LOG
317  *
318  * NOTE: we use a simple linked list of rules because we never need
319  * 	to delete a rule without scanning the list. We do not use
320  *	queue(3) macros for portability and readability.
321  */
322 
323 struct ip_fw {
324 	struct ip_fw	*next;		/* linked list of rules		*/
325 	struct ip_fw	*next_rule;	/* ptr to next [skipto] rule	*/
326 	uint16_t	act_ofs;	/* offset of action in 32-bit units */
327 	uint16_t	cmd_len;	/* # of 32-bit words in cmd	*/
328 	uint16_t	rulenum;	/* rule number			*/
329 	uint8_t		set;		/* rule set (0..31)		*/
330 	uint8_t		usr_flags;	/* IPFW_USR_F_			*/
331 
332 	/* These fields are present in all rules.			*/
333 	uint64_t	pcnt;		/* Packet counter		*/
334 	uint64_t	bcnt;		/* Byte counter			*/
335 	uint32_t	timestamp;	/* tv_sec of last match		*/
336 
337 	int		cpuid;		/* owner cpu			*/
338 	struct ip_fw	*sibling;	/* clone on next cpu		*/
339 
340 	struct ip_fw	**cross_rules;	/* cross referenced rules	*/
341 	volatile uint64_t cross_refs;	/* cross references		*/
342 
343 	uint32_t	refcnt;		/* Ref count for transit pkts	*/
344 	uint32_t	rule_flags;	/* IPFW_RULE_F_			*/
345 	uintptr_t	track_ruleid;	/* ruleid for src/dst tracks	*/
346 
347 	ipfw_insn	cmd[1];		/* storage for commands		*/
348 };
349 
350 #define IPFW_RULE_F_INVALID	0x1
351 /* unused			0x2 */
352 #define IPFW_RULE_F_GENSTATE	0x4
353 #define IPFW_RULE_F_GENTRACK	0x8
354 #define IPFW_RULE_F_CROSSREF	0x10
355 #define IPFW_RULE_F_DYNIFADDR	0x20
356 
357 #define RULESIZE(rule)	(sizeof(struct ip_fw) + (rule)->cmd_len * 4 - 4)
358 
359 /*
360  * This structure is used as a flow mask and a flow id for various
361  * parts of the code.
362  */
363 struct ipfw_flow_id {
364 	uint32_t	dst_ip;		/* host byte order */
365 	uint32_t	src_ip;		/* host byte order */
366 	uint16_t	dst_port;	/* host byte order */
367 	uint16_t	src_port;	/* host byte order */
368 	uint8_t		proto;
369 	uint8_t		flags;		/* protocol-specific flags */
370 };
371 
372 /*
373  * Main firewall chains definitions and global var's definitions.
374  */
375 
376 /* ipfw_chk/ip_fw_chk_ptr return values */
377 #define IP_FW_PASS		0
378 #define IP_FW_DENY		1
379 #define IP_FW_DIVERT		2
380 #define IP_FW_TEE		3
381 #define IP_FW_DUMMYNET		4
382 #define IP_FW_REDISPATCH	6
383 
384 /*
385  * arguments for calling ipfw_chk() and dummynet_io(). We put them
386  * all into a structure because this way it is easier and more
387  * efficient to pass variables around and extend the interface.
388  */
389 struct ip_fw_args {
390 	struct mbuf	*m;		/* the mbuf chain		*/
391 	struct ifnet	*oif;		/* output interface		*/
392 	struct ip_fw	*rule;		/* matching rule		*/
393 	struct ipfw_xlat *xlat;		/* matching xlate		*/
394 	struct ether_header *eh;	/* for bridged packets		*/
395 
396 	struct ipfw_flow_id f_id;	/* grabbed from IP header	*/
397 	uint8_t		flags;
398 #define IP_FWARG_F_CONT		0x01
399 #define IP_FWARG_F_XLATINS	0x02
400 #define IP_FWARG_F_XLATFWD	0x04
401 
402 	/*
403 	 * Depend on the return value of ipfw_chk/ip_fw_chk_ptr
404 	 * 'cookie' field may save following information:
405 	 *
406 	 * IP_FW_TEE or IP_FW_DIVERT
407 	 *   The divert port number
408 	 *
409 	 * IP_FW_DUMMYNET
410 	 *   The pipe or queue number
411 	 */
412 	uint32_t	cookie;
413 };
414 
415 /*
416  * Function definitions.
417  */
418 int	ip_fw_sockopt(struct sockopt *);
419 
420 /* Firewall hooks */
421 struct sockopt;
422 struct dn_flow_set;
423 
424 typedef int	ip_fw_chk_t(struct ip_fw_args *);
425 typedef int	ip_fw_ctl_t(struct sockopt *);
426 typedef struct mbuf
427 		*ip_fw_dn_io_t(struct mbuf *, int, int, struct ip_fw_args *);
428 
429 extern ip_fw_chk_t	*ip_fw_chk_ptr;
430 extern ip_fw_ctl_t	*ip_fw_ctl_ptr;
431 extern ip_fw_dn_io_t	*ip_fw_dn_io_ptr;
432 
433 extern int fw_one_pass;
434 extern int fw_enable;
435 
436 extern int ip_fw_loaded;
437 #define	IPFW_LOADED	(ip_fw_loaded)
438 
439 #endif /* _KERNEL */
440 
441 #define ACTION_PTR(rule)	\
442 	(ipfw_insn *)((uint32_t *)((rule)->cmd) + ((rule)->act_ofs))
443 
444 struct ipfw_ioc_rule {
445 	uint16_t	act_ofs;	/* offset of action in 32-bit units */
446 	uint16_t	cmd_len;	/* # of 32-bit words in cmd	*/
447 	uint16_t	rulenum;	/* rule number			*/
448 	uint8_t		set;		/* rule set (0..31)		*/
449 	uint8_t		usr_flags;	/* IPFW_USR_F_ 			*/
450 
451 	/* Rule set information */
452 	uint32_t	set_disable;	/* disabled rule sets		*/
453 	uint32_t	static_count;	/* # of static rules		*/
454 	uint32_t	static_len;	/* total length of static rules	*/
455 
456 	/* Statistics */
457 	uint64_t	pcnt;		/* Packet counter		*/
458 	uint64_t	bcnt;		/* Byte counter			*/
459 	uint32_t	timestamp;	/* tv_sec of last match		*/
460 
461 	uint8_t		reserved[16];
462 
463 	ipfw_insn	cmd[1];		/* storage for commands		*/
464 };
465 
466 #define IPFW_USR_F_NORULE	0x01
467 
468 #define IPFW_RULE_SIZE_MAX	255	/* unit: uint32_t */
469 
470 #define IOC_RULESIZE(rule)	\
471 	(sizeof(struct ipfw_ioc_rule) + (rule)->cmd_len * 4 - 4)
472 
473 struct ipfw_ioc_flowid {
474 	uint16_t	type;	/* ETHERTYPE_ */
475 	uint16_t	pad;
476 	union {
477 		struct {
478 			uint32_t dst_ip;	/* host byte order */
479 			uint32_t src_ip;	/* host byte order */
480 			uint16_t dst_port;	/* host byte order */
481 			uint16_t src_port;	/* host byte order */
482 			uint8_t proto;
483 		} ip;
484 		uint8_t pad[64];
485 	} u;
486 };
487 
488 struct ipfw_ioc_state {
489 	uint32_t	expire;		/* expire time			*/
490 	uint64_t	pcnt;		/* packet match counter		*/
491 	uint64_t	bcnt;		/* byte match counter		*/
492 
493 	uint16_t	dyn_type;	/* rule type			*/
494 	uint16_t	count;		/* refcount			*/
495 
496 	uint16_t	rulenum;	/* rule number			*/
497 
498 	uint16_t	xlat_port;	/* xlate port, host byte order	*/
499 	uint32_t	xlat_addr;	/* xlate addr, host byte order	*/
500 
501 	struct ipfw_ioc_flowid id;	/* (masked) flow id		*/
502 	uint8_t		reserved[16];
503 };
504 
505 /*
506  * Definitions for IP option names.
507  */
508 #define	IP_FW_IPOPT_LSRR	0x01
509 #define	IP_FW_IPOPT_SSRR	0x02
510 #define	IP_FW_IPOPT_RR		0x04
511 #define	IP_FW_IPOPT_TS		0x08
512 
513 /*
514  * Definitions for TCP option names.
515  */
516 #define	IP_FW_TCPOPT_MSS	0x01
517 #define	IP_FW_TCPOPT_WINDOW	0x02
518 #define	IP_FW_TCPOPT_SACK	0x04
519 #define	IP_FW_TCPOPT_TS		0x08
520 #define	IP_FW_TCPOPT_CC		0x10
521 
522 #define	ICMP_REJECT_RST		0x100	/* fake ICMP code (send a TCP RST) */
523 
524 /*
525  * IP_FW_TBL_CREATE, tableid >= 0.
526  * IP_FW_TBL_FLUSH, tableid >= 0.
527  * IP_FW_TBL_FLUSH, tableid < 0, flush all tables.
528  * IP_FW_TBL_DESTROY, tableid >= 0.
529  * IP_FW_TBL_ZERO, tableid >= 0.
530  * IP_FW_TBL_ZERO, tableid < 0, zero all tables' counters.
531  */
532 struct ipfw_ioc_table {
533 	int		tableid;
534 };
535 
536 struct ipfw_ioc_tblent {
537 	struct sockaddr_in key;
538 	struct sockaddr_in netmask;
539 	u_long		use;
540 	time_t		last_used;
541 	long		unused[2];
542 };
543 
544 /*
545  * IP_FW_TBL_GET, tableid < 0, list of all tables.
546  */
547 struct ipfw_ioc_tbllist {
548 	int		tableid;	/* MUST be the first field */
549 	int		tablecnt;
550 	uint16_t	tables[];
551 };
552 
553 /*
554  * IP_FW_TBL_GET, tableid >= 0, entries in the table.
555  * IP_FW_TBL_ADD, tableid >= 0, entcnt == 1.
556  * IP_FW_TBL_DEL, tableid >= 0, entcnt == 1.
557  */
558 struct ipfw_ioc_tblcont {
559 	int		tableid;	/* MUST be the first field */
560 	int		entcnt;
561 	struct ipfw_ioc_tblent ent[1];
562 };
563 
564 /*
565  * IP_FW_TBL_EXPIRE, tableid < 0, expire all tables.
566  * IP_FW_TBL_EXPIRE, tableid >= 0.
567  */
568 struct ipfw_ioc_tblexp {
569 	int		tableid;
570 	int		expcnt;
571 	time_t		expire;
572 	u_long		unused1[2];
573 };
574 
575 #endif /* _IPFW2_H */
576