]>
Commit | Line | Data |
---|---|---|
91447636 A |
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.4 2003/07/17 06:03:39 luigi Exp $ | |
26 | */ | |
27 | ||
28 | #ifndef _IPFW2_H | |
29 | #define _IPFW2_H | |
30 | ||
2d21ac55 A |
31 | /* |
32 | * Define IP Firewall event subclass, and associated events. | |
33 | */ | |
34 | ||
35 | /*! | |
36 | @defined KEV_IPFW_SUBCLASS | |
37 | @discussion The kernel event subclass for IP Firewall. | |
38 | */ | |
39 | #define KEV_IPFW_SUBCLASS 1 | |
40 | ||
41 | /*! | |
42 | @defined KEV_IPFW_ADD | |
43 | @discussion The event code indicating a rule has been added. | |
44 | */ | |
45 | #define KEV_IPFW_ADD 1 | |
46 | ||
47 | /*! | |
48 | @defined KEV_IPFW_DEL | |
49 | @discussion The event code indicating a rule has been removed. | |
50 | */ | |
51 | #define KEV_IPFW_DEL 2 | |
52 | ||
53 | /*! | |
54 | @defined KEV_IPFW_FLUSH | |
55 | @discussion The event code indicating the rule set has been flushed. | |
56 | */ | |
57 | #define KEV_IPFW_FLUSH 3 | |
58 | ||
59 | /*! | |
60 | @defined KEV_IPFW_ENABLE | |
61 | @discussion The event code indicating the enable flag has been changed | |
62 | */ | |
63 | #define KEV_IPFW_ENABLE 4 | |
64 | ||
65 | ||
66 | ||
67 | #if !__LP64__ | |
68 | ||
91447636 A |
69 | /* |
70 | * The kernel representation of ipfw rules is made of a list of | |
71 | * 'instructions' (for all practical purposes equivalent to BPF | |
72 | * instructions), which specify which fields of the packet | |
73 | * (or its metadata) should be analysed. | |
74 | * | |
75 | * Each instruction is stored in a structure which begins with | |
76 | * "ipfw_insn", and can contain extra fields depending on the | |
77 | * instruction type (listed below). | |
78 | * Note that the code is written so that individual instructions | |
79 | * have a size which is a multiple of 32 bits. This means that, if | |
80 | * such structures contain pointers or other 64-bit entities, | |
81 | * (there is just one instance now) they may end up unaligned on | |
82 | * 64-bit architectures, so the must be handled with care. | |
83 | * | |
84 | * "enum ipfw_opcodes" are the opcodes supported. We can have up | |
85 | * to 256 different opcodes. | |
86 | */ | |
87 | ||
88 | enum ipfw_opcodes { /* arguments (4 byte each) */ | |
89 | O_NOP, | |
90 | ||
91 | O_IP_SRC, /* u32 = IP */ | |
92 | O_IP_SRC_MASK, /* ip = IP/mask */ | |
93 | O_IP_SRC_ME, /* none */ | |
94 | O_IP_SRC_SET, /* u32=base, arg1=len, bitmap */ | |
95 | ||
96 | O_IP_DST, /* u32 = IP */ | |
97 | O_IP_DST_MASK, /* ip = IP/mask */ | |
98 | O_IP_DST_ME, /* none */ | |
99 | O_IP_DST_SET, /* u32=base, arg1=len, bitmap */ | |
100 | ||
101 | O_IP_SRCPORT, /* (n)port list:mask 4 byte ea */ | |
102 | O_IP_DSTPORT, /* (n)port list:mask 4 byte ea */ | |
103 | O_PROTO, /* arg1=protocol */ | |
104 | ||
105 | O_MACADDR2, /* 2 mac addr:mask */ | |
106 | O_MAC_TYPE, /* same as srcport */ | |
107 | ||
108 | O_LAYER2, /* none */ | |
109 | O_IN, /* none */ | |
110 | O_FRAG, /* none */ | |
111 | ||
112 | O_RECV, /* none */ | |
113 | O_XMIT, /* none */ | |
114 | O_VIA, /* none */ | |
115 | ||
116 | O_IPOPT, /* arg1 = 2*u8 bitmap */ | |
117 | O_IPLEN, /* arg1 = len */ | |
118 | O_IPID, /* arg1 = id */ | |
119 | ||
120 | O_IPTOS, /* arg1 = id */ | |
121 | O_IPPRECEDENCE, /* arg1 = precedence << 5 */ | |
122 | O_IPTTL, /* arg1 = TTL */ | |
123 | ||
124 | O_IPVER, /* arg1 = version */ | |
125 | O_UID, /* u32 = id */ | |
126 | O_GID, /* u32 = id */ | |
127 | O_ESTAB, /* none (tcp established) */ | |
128 | O_TCPFLAGS, /* arg1 = 2*u8 bitmap */ | |
129 | O_TCPWIN, /* arg1 = desired win */ | |
130 | O_TCPSEQ, /* u32 = desired seq. */ | |
131 | O_TCPACK, /* u32 = desired seq. */ | |
132 | O_ICMPTYPE, /* u32 = icmp bitmap */ | |
133 | O_TCPOPTS, /* arg1 = 2*u8 bitmap */ | |
134 | ||
135 | O_VERREVPATH, /* none */ | |
136 | ||
137 | O_PROBE_STATE, /* none */ | |
138 | O_KEEP_STATE, /* none */ | |
139 | O_LIMIT, /* ipfw_insn_limit */ | |
140 | O_LIMIT_PARENT, /* dyn_type, not an opcode. */ | |
141 | ||
142 | /* | |
143 | * These are really 'actions'. | |
144 | */ | |
145 | ||
146 | O_LOG, /* ipfw_insn_log */ | |
147 | O_PROB, /* u32 = match probability */ | |
148 | ||
149 | O_CHECK_STATE, /* none */ | |
150 | O_ACCEPT, /* none */ | |
151 | O_DENY, /* none */ | |
152 | O_REJECT, /* arg1=icmp arg (same as deny) */ | |
153 | O_COUNT, /* none */ | |
154 | O_SKIPTO, /* arg1=next rule number */ | |
155 | O_PIPE, /* arg1=pipe number */ | |
156 | O_QUEUE, /* arg1=queue number */ | |
157 | O_DIVERT, /* arg1=port number */ | |
158 | O_TEE, /* arg1=port number */ | |
159 | O_FORWARD_IP, /* fwd sockaddr */ | |
160 | O_FORWARD_MAC, /* fwd mac */ | |
161 | ||
162 | /* | |
163 | * More opcodes. | |
164 | */ | |
165 | O_IPSEC, /* has ipsec history */ | |
166 | ||
167 | O_LAST_OPCODE /* not an opcode! */ | |
168 | }; | |
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 | int32_t unit; | |
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 limit rules. | |
284 | */ | |
285 | typedef struct _ipfw_insn_limit { | |
286 | ipfw_insn o; | |
287 | u_int8_t _pad; | |
288 | u_int8_t limit_mask; /* combination of DYN_* below */ | |
289 | #define DYN_SRC_ADDR 0x1 | |
290 | #define DYN_SRC_PORT 0x2 | |
291 | #define DYN_DST_ADDR 0x4 | |
292 | #define DYN_DST_PORT 0x8 | |
293 | ||
294 | u_int16_t conn_limit; | |
295 | } ipfw_insn_limit; | |
296 | ||
297 | /* | |
298 | * This is used for log instructions. | |
299 | */ | |
300 | typedef struct _ipfw_insn_log { | |
301 | ipfw_insn o; | |
302 | u_int32_t max_log; /* how many do we log -- 0 = all */ | |
303 | u_int32_t log_left; /* how many left to log */ | |
304 | } ipfw_insn_log; | |
305 | ||
306 | /* Version of this API */ | |
307 | #define IP_FW_VERSION_NONE 0 | |
308 | #define IP_FW_VERSION_0 10 /* old ipfw */ | |
309 | #define IP_FW_VERSION_1 20 /* ipfw in Jaguar/Panther */ | |
310 | #define IP_FW_VERSION_2 30 /* ipfw2 */ | |
311 | #define IP_FW_CURRENT_API_VERSION IP_FW_VERSION_2 | |
312 | ||
313 | /* | |
314 | * Here we have the structure representing an ipfw rule. | |
315 | * | |
316 | * It starts with a general area (with link fields and counters) | |
317 | * followed by an array of one or more instructions, which the code | |
318 | * accesses as an array of 32-bit values. | |
319 | * | |
320 | * Given a rule pointer r: | |
321 | * | |
322 | * r->cmd is the start of the first instruction. | |
323 | * ACTION_PTR(r) is the start of the first action (things to do | |
324 | * once a rule matched). | |
325 | * | |
326 | * When assembling instruction, remember the following: | |
327 | * | |
328 | * + if a rule has a "keep-state" (or "limit") option, then the | |
329 | * first instruction (at r->cmd) MUST BE an O_PROBE_STATE | |
330 | * + if a rule has a "log" option, then the first action | |
331 | * (at ACTION_PTR(r)) MUST be O_LOG | |
332 | * | |
333 | * NOTE: we use a simple linked list of rules because we never need | |
334 | * to delete a rule without scanning the list. We do not use | |
335 | * queue(3) macros for portability and readability. | |
336 | */ | |
337 | ||
338 | struct ip_fw { | |
339 | u_int32_t version; /* Version of this structure. MUST be set */ | |
340 | /* by clients. Should always be */ | |
341 | /* set to IP_FW_CURRENT_API_VERSION. */ | |
342 | void *context; /* Context that is usable by user processes to */ | |
343 | /* identify this rule. */ | |
344 | struct ip_fw *next; /* linked list of rules */ | |
345 | struct ip_fw *next_rule; /* ptr to next [skipto] rule */ | |
346 | /* 'next_rule' is used to pass up 'set_disable' status */ | |
347 | ||
348 | u_int16_t act_ofs; /* offset of action in 32-bit units */ | |
349 | u_int16_t cmd_len; /* # of 32-bit words in cmd */ | |
350 | u_int16_t rulenum; /* rule number */ | |
351 | u_int8_t set; /* rule set (0..31) */ | |
352 | u_int32_t set_masks[2]; /* masks for manipulating sets atomically */ | |
353 | #define RESVD_SET 31 /* set for default and persistent rules */ | |
354 | u_int8_t _pad; /* padding */ | |
355 | ||
356 | /* These fields are present in all rules. */ | |
357 | u_int64_t pcnt; /* Packet counter */ | |
358 | u_int64_t bcnt; /* Byte counter */ | |
359 | u_int32_t timestamp; /* tv_sec of last match */ | |
360 | ||
361 | u_int32_t reserved_1; /* reserved - set to 0 */ | |
362 | u_int32_t reserved_2; /* reserved - set to 0 */ | |
363 | ||
364 | ipfw_insn cmd[1]; /* storage for commands */ | |
365 | }; | |
366 | ||
367 | #define ACTION_PTR(rule) \ | |
368 | (ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) ) | |
369 | ||
370 | #define RULESIZE(rule) (sizeof(struct ip_fw) + \ | |
371 | ((struct ip_fw *)(rule))->cmd_len * 4 - 4) | |
372 | ||
373 | /* | |
374 | * This structure is used as a flow mask and a flow id for various | |
375 | * parts of the code. | |
376 | */ | |
377 | struct ipfw_flow_id { | |
378 | u_int32_t dst_ip; | |
379 | u_int32_t src_ip; | |
380 | u_int16_t dst_port; | |
381 | u_int16_t src_port; | |
382 | u_int8_t proto; | |
383 | u_int8_t flags; /* protocol-specific flags */ | |
384 | }; | |
385 | ||
386 | /* | |
387 | * Dynamic ipfw rule. | |
388 | */ | |
389 | typedef struct _ipfw_dyn_rule ipfw_dyn_rule; | |
390 | ||
391 | struct _ipfw_dyn_rule { | |
392 | ipfw_dyn_rule *next; /* linked list of rules. */ | |
393 | struct ip_fw *rule; /* pointer to rule */ | |
394 | /* 'rule' is used to pass up the rule number (from the parent) */ | |
395 | ||
396 | ipfw_dyn_rule *parent; /* pointer to parent rule */ | |
397 | u_int64_t pcnt; /* packet match counter */ | |
398 | u_int64_t bcnt; /* byte match counter */ | |
399 | struct ipfw_flow_id id; /* (masked) flow id */ | |
400 | u_int32_t expire; /* expire time */ | |
401 | u_int32_t bucket; /* which bucket in hash table */ | |
402 | u_int32_t state; /* state of this rule (typically a | |
403 | * combination of TCP flags) | |
404 | */ | |
405 | u_int32_t ack_fwd; /* most recent ACKs in forward */ | |
406 | u_int32_t ack_rev; /* and reverse directions (used */ | |
407 | /* to generate keepalives) */ | |
408 | u_int16_t dyn_type; /* rule type */ | |
409 | u_int16_t count; /* refcount */ | |
410 | }; | |
411 | ||
412 | /* | |
413 | * Definitions for IP option names. | |
414 | */ | |
415 | #define IP_FW_IPOPT_LSRR 0x01 | |
416 | #define IP_FW_IPOPT_SSRR 0x02 | |
417 | #define IP_FW_IPOPT_RR 0x04 | |
418 | #define IP_FW_IPOPT_TS 0x08 | |
419 | ||
420 | /* | |
421 | * Definitions for TCP option names. | |
422 | */ | |
423 | #define IP_FW_TCPOPT_MSS 0x01 | |
424 | #define IP_FW_TCPOPT_WINDOW 0x02 | |
425 | #define IP_FW_TCPOPT_SACK 0x04 | |
426 | #define IP_FW_TCPOPT_TS 0x08 | |
427 | #define IP_FW_TCPOPT_CC 0x10 | |
428 | ||
429 | #define ICMP_REJECT_RST 0x100 /* fake ICMP code (send a TCP RST) */ | |
430 | ||
431 | /* | |
432 | * Main firewall chains definitions and global var's definitions. | |
433 | */ | |
434 | #ifdef KERNEL | |
4a3eedf9 | 435 | #if IPFIREWALL |
91447636 A |
436 | |
437 | #define IP_FW_PORT_DYNT_FLAG 0x10000 | |
438 | #define IP_FW_PORT_TEE_FLAG 0x20000 | |
439 | #define IP_FW_PORT_DENY_FLAG 0x40000 | |
440 | ||
441 | /* | |
442 | * Arguments for calling ipfw_chk() and dummynet_io(). We put them | |
443 | * all into a structure because this way it is easier and more | |
444 | * efficient to pass variables around and extend the interface. | |
445 | */ | |
446 | struct ip_fw_args { | |
447 | struct mbuf *m; /* the mbuf chain */ | |
448 | struct ifnet *oif; /* output interface */ | |
449 | struct sockaddr_in *next_hop; /* forward address */ | |
450 | struct ip_fw *rule; /* matching rule */ | |
451 | struct ether_header *eh; /* for bridged packets */ | |
452 | ||
453 | struct route *ro; /* for dummynet */ | |
454 | struct sockaddr_in *dst; /* for dummynet */ | |
455 | int flags; /* for dummynet */ | |
456 | ||
457 | struct ipfw_flow_id f_id; /* grabbed from IP header */ | |
458 | u_int16_t divert_rule; /* divert cookie */ | |
459 | u_int32_t retval; | |
460 | }; | |
4a3eedf9 | 461 | //struct ip_fw_args; |
91447636 A |
462 | |
463 | /* | |
464 | * Function definitions. | |
465 | */ | |
466 | ||
467 | /* Firewall hooks */ | |
468 | struct sockopt; | |
469 | struct dn_flow_set; | |
470 | ||
471 | void flush_pipe_ptrs(struct dn_flow_set *match); /* used by dummynet */ | |
472 | void ipfw_init(void); /* called from raw_ip.c: load_ipfw() */ | |
473 | ||
474 | typedef int ip_fw_chk_t (struct ip_fw_args *args); | |
475 | typedef int ip_fw_ctl_t (struct sockopt *); | |
476 | extern ip_fw_chk_t *ip_fw_chk_ptr; | |
477 | extern ip_fw_ctl_t *ip_fw_ctl_ptr; | |
478 | extern int fw_one_pass; | |
479 | extern int fw_enable; | |
480 | #define IPFW_LOADED (ip_fw_chk_ptr != NULL) | |
4a3eedf9 | 481 | #endif /* IPFIREWALL */ |
91447636 A |
482 | #endif /* KERNEL */ |
483 | ||
2d21ac55 | 484 | #endif /* !__LP64__ */ |
91447636 | 485 | #endif /* _IPFW2_H */ |