FreeBSD/Linux Kernel Cross Reference
sys/netinet/ip_fw.h
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: releng/6.4/sys/netinet/ip_fw.h 170398 2007-06-07 09:50:53Z bz $
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 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
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_IP4,
157
158 O_UNREACH6, /* arg1=icmpv6 code arg (deny) */
159
160 O_TAG, /* arg1=tag number */
161 O_TAGGED, /* arg1=tag number */
162
163 O_LAST_OPCODE /* not an opcode! */
164 };
165
166 /*
167 * The extension header are filtered only for presence using a bit
168 * vector with a flag for each header.
169 */
170 #define EXT_FRAGMENT 0x1
171 #define EXT_HOPOPTS 0x2
172 #define EXT_ROUTING 0x4
173 #define EXT_AH 0x8
174 #define EXT_ESP 0x10
175 #define EXT_DSTOPTS 0x20
176 #define EXT_RTHDR0 0x40
177 #define EXT_RTHDR2 0x80
178
179 /*
180 * Template for instructions.
181 *
182 * ipfw_insn is used for all instructions which require no operands,
183 * a single 16-bit value (arg1), or a couple of 8-bit values.
184 *
185 * For other instructions which require different/larger arguments
186 * we have derived structures, ipfw_insn_*.
187 *
188 * The size of the instruction (in 32-bit words) is in the low
189 * 6 bits of "len". The 2 remaining bits are used to implement
190 * NOT and OR on individual instructions. Given a type, you can
191 * compute the length to be put in "len" using F_INSN_SIZE(t)
192 *
193 * F_NOT negates the match result of the instruction.
194 *
195 * F_OR is used to build or blocks. By default, instructions
196 * are evaluated as part of a logical AND. An "or" block
197 * { X or Y or Z } contains F_OR set in all but the last
198 * instruction of the block. A match will cause the code
199 * to skip past the last instruction of the block.
200 *
201 * NOTA BENE: in a couple of places we assume that
202 * sizeof(ipfw_insn) == sizeof(u_int32_t)
203 * this needs to be fixed.
204 *
205 */
206 typedef struct _ipfw_insn { /* template for instructions */
207 enum ipfw_opcodes opcode:8;
208 u_int8_t len; /* numer of 32-byte words */
209 #define F_NOT 0x80
210 #define F_OR 0x40
211 #define F_LEN_MASK 0x3f
212 #define F_LEN(cmd) ((cmd)->len & F_LEN_MASK)
213
214 u_int16_t arg1;
215 } ipfw_insn;
216
217 /*
218 * The F_INSN_SIZE(type) computes the size, in 4-byte words, of
219 * a given type.
220 */
221 #define F_INSN_SIZE(t) ((sizeof (t))/sizeof(u_int32_t))
222
223 #define MTAG_IPFW 1148380143 /* IPFW-tagged cookie */
224
225 /*
226 * This is used to store an array of 16-bit entries (ports etc.)
227 */
228 typedef struct _ipfw_insn_u16 {
229 ipfw_insn o;
230 u_int16_t ports[2]; /* there may be more */
231 } ipfw_insn_u16;
232
233 /*
234 * This is used to store an array of 32-bit entries
235 * (uid, single IPv4 addresses etc.)
236 */
237 typedef struct _ipfw_insn_u32 {
238 ipfw_insn o;
239 u_int32_t d[1]; /* one or more */
240 } ipfw_insn_u32;
241
242 /*
243 * This is used to store IP addr-mask pairs.
244 */
245 typedef struct _ipfw_insn_ip {
246 ipfw_insn o;
247 struct in_addr addr;
248 struct in_addr mask;
249 } ipfw_insn_ip;
250
251 /*
252 * This is used to forward to a given address (ip).
253 */
254 typedef struct _ipfw_insn_sa {
255 ipfw_insn o;
256 struct sockaddr_in sa;
257 } ipfw_insn_sa;
258
259 /*
260 * This is used for MAC addr-mask pairs.
261 */
262 typedef struct _ipfw_insn_mac {
263 ipfw_insn o;
264 u_char addr[12]; /* dst[6] + src[6] */
265 u_char mask[12]; /* dst[6] + src[6] */
266 } ipfw_insn_mac;
267
268 /*
269 * This is used for interface match rules (recv xx, xmit xx).
270 */
271 typedef struct _ipfw_insn_if {
272 ipfw_insn o;
273 union {
274 struct in_addr ip;
275 int glob;
276 } p;
277 char name[IFNAMSIZ];
278 } ipfw_insn_if;
279
280 /*
281 * This is used for pipe and queue actions, which need to store
282 * a single pointer (which can have different size on different
283 * architectures.
284 * Note that, because of previous instructions, pipe_ptr might
285 * be unaligned in the overall structure, so it needs to be
286 * manipulated with care.
287 */
288 typedef struct _ipfw_insn_pipe {
289 ipfw_insn o;
290 void *pipe_ptr; /* XXX */
291 } ipfw_insn_pipe;
292
293 /*
294 * This is used for storing an altq queue id number.
295 */
296 typedef struct _ipfw_insn_altq {
297 ipfw_insn o;
298 u_int32_t qid;
299 } ipfw_insn_altq;
300
301 /*
302 * This is used for limit rules.
303 */
304 typedef struct _ipfw_insn_limit {
305 ipfw_insn o;
306 u_int8_t _pad;
307 u_int8_t limit_mask; /* combination of DYN_* below */
308 #define DYN_SRC_ADDR 0x1
309 #define DYN_SRC_PORT 0x2
310 #define DYN_DST_ADDR 0x4
311 #define DYN_DST_PORT 0x8
312
313 u_int16_t conn_limit;
314 } ipfw_insn_limit;
315
316 /*
317 * This is used for log instructions.
318 */
319 typedef struct _ipfw_insn_log {
320 ipfw_insn o;
321 u_int32_t max_log; /* how many do we log -- 0 = all */
322 u_int32_t log_left; /* how many left to log */
323 } ipfw_insn_log;
324
325 /* Apply ipv6 mask on ipv6 addr */
326 #define APPLY_MASK(addr,mask) \
327 (addr)->__u6_addr.__u6_addr32[0] &= (mask)->__u6_addr.__u6_addr32[0]; \
328 (addr)->__u6_addr.__u6_addr32[1] &= (mask)->__u6_addr.__u6_addr32[1]; \
329 (addr)->__u6_addr.__u6_addr32[2] &= (mask)->__u6_addr.__u6_addr32[2]; \
330 (addr)->__u6_addr.__u6_addr32[3] &= (mask)->__u6_addr.__u6_addr32[3];
331
332 /* Structure for ipv6 */
333 typedef struct _ipfw_insn_ip6 {
334 ipfw_insn o;
335 struct in6_addr addr6;
336 struct in6_addr mask6;
337 } ipfw_insn_ip6;
338
339 /* Used to support icmp6 types */
340 typedef struct _ipfw_insn_icmp6 {
341 ipfw_insn o;
342 uint32_t d[7]; /* XXX This number si related to the netinet/icmp6.h
343 * define ICMP6_MAXTYPE
344 * as follows: n = ICMP6_MAXTYPE/32 + 1
345 * Actually is 203
346 */
347 } ipfw_insn_icmp6;
348
349 /*
350 * Here we have the structure representing an ipfw rule.
351 *
352 * It starts with a general area (with link fields and counters)
353 * followed by an array of one or more instructions, which the code
354 * accesses as an array of 32-bit values.
355 *
356 * Given a rule pointer r:
357 *
358 * r->cmd is the start of the first instruction.
359 * ACTION_PTR(r) is the start of the first action (things to do
360 * once a rule matched).
361 *
362 * When assembling instruction, remember the following:
363 *
364 * + if a rule has a "keep-state" (or "limit") option, then the
365 * first instruction (at r->cmd) MUST BE an O_PROBE_STATE
366 * + if a rule has a "log" option, then the first action
367 * (at ACTION_PTR(r)) MUST be O_LOG
368 * + if a rule has an "altq" option, it comes after "log"
369 * + if a rule has an O_TAG option, it comes after "log" and "altq"
370 *
371 * NOTE: we use a simple linked list of rules because we never need
372 * to delete a rule without scanning the list. We do not use
373 * queue(3) macros for portability and readability.
374 */
375
376 struct ip_fw {
377 struct ip_fw *next; /* linked list of rules */
378 struct ip_fw *next_rule; /* ptr to next [skipto] rule */
379 /* 'next_rule' is used to pass up 'set_disable' status */
380
381 u_int16_t act_ofs; /* offset of action in 32-bit units */
382 u_int16_t cmd_len; /* # of 32-bit words in cmd */
383 u_int16_t rulenum; /* rule number */
384 u_int8_t set; /* rule set (0..31) */
385 #define RESVD_SET 31 /* set for default and persistent rules */
386 u_int8_t _pad; /* padding */
387
388 /* These fields are present in all rules. */
389 u_int64_t pcnt; /* Packet counter */
390 u_int64_t bcnt; /* Byte counter */
391 u_int32_t timestamp; /* tv_sec of last match */
392
393 ipfw_insn cmd[1]; /* storage for commands */
394 };
395
396 #define ACTION_PTR(rule) \
397 (ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) )
398
399 #define RULESIZE(rule) (sizeof(struct ip_fw) + \
400 ((struct ip_fw *)(rule))->cmd_len * 4 - 4)
401
402 /*
403 * This structure is used as a flow mask and a flow id for various
404 * parts of the code.
405 */
406 struct ipfw_flow_id {
407 u_int32_t dst_ip;
408 u_int32_t src_ip;
409 u_int16_t dst_port;
410 u_int16_t src_port;
411 u_int8_t proto;
412 u_int8_t flags; /* protocol-specific flags */
413 uint8_t addr_type; /* 4 = ipv4, 6 = ipv6, 1=ether ? */
414 struct in6_addr dst_ip6; /* could also store MAC addr! */
415 struct in6_addr src_ip6;
416 u_int32_t flow_id6;
417 u_int32_t frag_id6;
418 };
419
420 #define IS_IP6_FLOW_ID(id) ((id)->addr_type == 6)
421
422 /*
423 * Dynamic ipfw rule.
424 */
425 typedef struct _ipfw_dyn_rule ipfw_dyn_rule;
426
427 struct _ipfw_dyn_rule {
428 ipfw_dyn_rule *next; /* linked list of rules. */
429 struct ip_fw *rule; /* pointer to rule */
430 /* 'rule' is used to pass up the rule number (from the parent) */
431
432 ipfw_dyn_rule *parent; /* pointer to parent rule */
433 u_int64_t pcnt; /* packet match counter */
434 u_int64_t bcnt; /* byte match counter */
435 struct ipfw_flow_id id; /* (masked) flow id */
436 u_int32_t expire; /* expire time */
437 u_int32_t bucket; /* which bucket in hash table */
438 u_int32_t state; /* state of this rule (typically a
439 * combination of TCP flags)
440 */
441 u_int32_t ack_fwd; /* most recent ACKs in forward */
442 u_int32_t ack_rev; /* and reverse directions (used */
443 /* to generate keepalives) */
444 u_int16_t dyn_type; /* rule type */
445 u_int16_t count; /* refcount */
446 };
447
448 /*
449 * Definitions for IP option names.
450 */
451 #define IP_FW_IPOPT_LSRR 0x01
452 #define IP_FW_IPOPT_SSRR 0x02
453 #define IP_FW_IPOPT_RR 0x04
454 #define IP_FW_IPOPT_TS 0x08
455
456 /*
457 * Definitions for TCP option names.
458 */
459 #define IP_FW_TCPOPT_MSS 0x01
460 #define IP_FW_TCPOPT_WINDOW 0x02
461 #define IP_FW_TCPOPT_SACK 0x04
462 #define IP_FW_TCPOPT_TS 0x08
463 #define IP_FW_TCPOPT_CC 0x10
464
465 #define ICMP_REJECT_RST 0x100 /* fake ICMP code (send a TCP RST) */
466 #define ICMP6_UNREACH_RST 0x100 /* fake ICMPv6 code (send a TCP RST) */
467
468 /*
469 * These are used for lookup tables.
470 */
471 typedef struct _ipfw_table_entry {
472 in_addr_t addr; /* network address */
473 u_int32_t value; /* value */
474 u_int16_t tbl; /* table number */
475 u_int8_t masklen; /* mask length */
476 } ipfw_table_entry;
477
478 typedef struct _ipfw_table {
479 u_int32_t size; /* size of entries in bytes */
480 u_int32_t cnt; /* # of entries */
481 u_int16_t tbl; /* table number */
482 ipfw_table_entry ent[0]; /* entries */
483 } ipfw_table;
484
485 #define IP_FW_TABLEARG 65535
486
487 /*
488 * Main firewall chains definitions and global var's definitions.
489 */
490 #ifdef _KERNEL
491
492 /* Return values from ipfw_chk() */
493 enum {
494 IP_FW_PASS = 0,
495 IP_FW_DENY,
496 IP_FW_DIVERT,
497 IP_FW_TEE,
498 IP_FW_DUMMYNET,
499 IP_FW_NETGRAPH,
500 IP_FW_NGTEE,
501 };
502
503 /* flags for divert mtag */
504 #define IP_FW_DIVERT_LOOPBACK_FLAG 0x00080000
505 #define IP_FW_DIVERT_OUTPUT_FLAG 0x00100000
506
507 /*
508 * Structure for collecting parameters to dummynet for ip6_output forwarding
509 */
510 struct _ip6dn_args {
511 struct ip6_pktopts *opt_or;
512 struct route_in6 ro_or;
513 int flags_or;
514 struct ip6_moptions *im6o_or;
515 struct ifnet *origifp_or;
516 struct ifnet *ifp_or;
517 struct sockaddr_in6 dst_or;
518 u_long mtu_or;
519 struct route_in6 ro_pmtu_or;
520 };
521
522 /*
523 * Arguments for calling ipfw_chk() and dummynet_io(). We put them
524 * all into a structure because this way it is easier and more
525 * efficient to pass variables around and extend the interface.
526 */
527 struct ip_fw_args {
528 struct mbuf *m; /* the mbuf chain */
529 struct ifnet *oif; /* output interface */
530 struct sockaddr_in *next_hop; /* forward address */
531 struct ip_fw *rule; /* matching rule */
532 struct ether_header *eh; /* for bridged packets */
533
534 struct ipfw_flow_id f_id; /* grabbed from IP header */
535 u_int32_t cookie; /* a cookie depending on rule action */
536 struct inpcb *inp;
537
538 struct _ip6dn_args dummypar; /* dummynet->ip6_output */
539 struct sockaddr_in hopstore; /* store here if cannot use a pointer */
540 };
541
542 /*
543 * Function definitions.
544 */
545
546 /* Firewall hooks */
547 struct sockopt;
548 struct dn_flow_set;
549
550 int ipfw_check_in(void *, struct mbuf **, struct ifnet *, int, struct inpcb *inp);
551 int ipfw_check_out(void *, struct mbuf **, struct ifnet *, int, struct inpcb *inp);
552
553 int ipfw_chk(struct ip_fw_args *);
554
555 int ipfw_init(void);
556 void ipfw_destroy(void);
557
558 void flush_pipe_ptrs(struct dn_flow_set *match); /* used by dummynet */
559
560 typedef int ip_fw_ctl_t(struct sockopt *);
561 extern ip_fw_ctl_t *ip_fw_ctl_ptr;
562 extern int fw_one_pass;
563 extern int fw_enable;
564
565 /* For kernel ipfw_ether and ipfw_bridge. */
566 typedef int ip_fw_chk_t(struct ip_fw_args *args);
567 extern ip_fw_chk_t *ip_fw_chk_ptr;
568 #define IPFW_LOADED (ip_fw_chk_ptr != NULL)
569
570 #endif /* _KERNEL */
571 #endif /* _IPFW2_H */
Cache object: c83a6a3f7f2b41c65d8a50cd4b08f5ca
|