ipfw: Add ipfrag filter.
[dragonfly.git] / sys / net / ipfw / ip_fw2.h
CommitLineData
984263bc
MD
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
45enum 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, /* u32 = icmp bitmap */
90 O_TCPOPTS, /* arg1 = 2*u8 bitmap */
91
92 O_PROBE_STATE, /* none */
93 O_KEEP_STATE, /* none */
94 O_LIMIT, /* ipfw_insn_limit */
95 O_LIMIT_PARENT, /* dyn_type, not an opcode. */
984263bc 96
6998b243 97 /* Actions. */
984263bc
MD
98 O_LOG, /* ipfw_insn_log */
99 O_PROB, /* u32 = match probability */
984263bc
MD
100 O_CHECK_STATE, /* none */
101 O_ACCEPT, /* none */
102 O_DENY, /* none */
103 O_REJECT, /* arg1=icmp arg (same as deny) */
104 O_COUNT, /* none */
105 O_SKIPTO, /* arg1=next rule number */
106 O_PIPE, /* arg1=pipe number */
107 O_QUEUE, /* arg1=queue number */
108 O_DIVERT, /* arg1=port number */
109 O_TEE, /* arg1=port number */
110 O_FORWARD_IP, /* fwd sockaddr */
111 O_FORWARD_MAC, /* fwd mac */
6998b243
SZ
112
113 /* Table based filters. */
d938108c
SZ
114 O_IP_SRC_TABLE, /* arg1 = tableid */
115 O_IP_DST_TABLE, /* arg1 = tableid */
6998b243
SZ
116
117 /* Action. */
118 O_DEFRAG, /* none */
119
cc9ef3d3
SZ
120 /* Filter. */
121 O_IPFRAG, /* none */
122
984263bc
MD
123 O_LAST_OPCODE /* not an opcode! */
124};
125
126/*
127 * Template for instructions.
128 *
129 * ipfw_insn is used for all instructions which require no operands,
130 * a single 16-bit value (arg1), or a couple of 8-bit values.
131 *
132 * For other instructions which require different/larger arguments
133 * we have derived structures, ipfw_insn_*.
134 *
135 * The size of the instruction (in 32-bit words) is in the low
136 * 6 bits of "len". The 2 remaining bits are used to implement
137 * NOT and OR on individual instructions. Given a type, you can
138 * compute the length to be put in "len" using F_INSN_SIZE(t)
139 *
140 * F_NOT negates the match result of the instruction.
141 *
142 * F_OR is used to build or blocks. By default, instructions
143 * are evaluated as part of a logical AND. An "or" block
144 * { X or Y or Z } contains F_OR set in all but the last
145 * instruction of the block. A match will cause the code
146 * to skip past the last instruction of the block.
147 *
148 * NOTA BENE: in a couple of places we assume that
a998c492 149 * sizeof(ipfw_insn) == sizeof(uint32_t)
984263bc
MD
150 * this needs to be fixed.
151 *
152 */
153typedef struct _ipfw_insn { /* template for instructions */
154 enum ipfw_opcodes opcode:8;
a998c492 155 uint8_t len; /* numer of 32-byte words */
984263bc
MD
156#define F_NOT 0x80
157#define F_OR 0x40
158#define F_LEN_MASK 0x3f
159#define F_LEN(cmd) ((cmd)->len & F_LEN_MASK)
160
a998c492 161 uint16_t arg1;
984263bc
MD
162} ipfw_insn;
163
164/*
165 * The F_INSN_SIZE(type) computes the size, in 4-byte words, of
166 * a given type.
167 */
a998c492 168#define F_INSN_SIZE(t) ((sizeof (t))/sizeof(uint32_t))
984263bc
MD
169
170/*
171 * This is used to store an array of 16-bit entries (ports etc.)
172 */
173typedef struct _ipfw_insn_u16 {
174 ipfw_insn o;
a998c492 175 uint16_t ports[2]; /* there may be more */
984263bc
MD
176} ipfw_insn_u16;
177
178/*
179 * This is used to store an array of 32-bit entries
180 * (uid, single IPv4 addresses etc.)
181 */
182typedef struct _ipfw_insn_u32 {
183 ipfw_insn o;
a998c492 184 uint32_t d[1]; /* one or more */
984263bc
MD
185} ipfw_insn_u32;
186
187/*
188 * This is used to store IP addr-mask pairs.
189 */
190typedef struct _ipfw_insn_ip {
191 ipfw_insn o;
192 struct in_addr addr;
193 struct in_addr mask;
194} ipfw_insn_ip;
195
196/*
197 * This is used to forward to a given address (ip)
198 */
199typedef struct _ipfw_insn_sa {
200 ipfw_insn o;
201 struct sockaddr_in sa;
202} ipfw_insn_sa;
203
204/*
205 * This is used for MAC addr-mask pairs.
206 */
207typedef struct _ipfw_insn_mac {
208 ipfw_insn o;
209 u_char addr[12]; /* dst[6] + src[6] */
210 u_char mask[12]; /* dst[6] + src[6] */
211} ipfw_insn_mac;
212
213/*
214 * This is used for interface match rules (recv xx, xmit xx)
215 */
216typedef struct _ipfw_insn_if {
217 ipfw_insn o;
218 union {
219 struct in_addr ip;
3e4a09e7 220 int glob;
984263bc
MD
221 } p;
222 char name[IFNAMSIZ];
223} ipfw_insn_if;
224
225/*
226 * This is used for pipe and queue actions, which need to store
227 * a single pointer (which can have different size on different
228 * architectures.
229 */
230typedef struct _ipfw_insn_pipe {
231 ipfw_insn o;
232 void *pipe_ptr;
233} ipfw_insn_pipe;
234
235/*
236 * This is used for limit rules.
237 */
238typedef struct _ipfw_insn_limit {
239 ipfw_insn o;
a998c492
SZ
240 uint8_t _pad;
241 uint8_t limit_mask; /* combination of DYN_* below */
984263bc
MD
242#define DYN_SRC_ADDR 0x1
243#define DYN_SRC_PORT 0x2
244#define DYN_DST_ADDR 0x4
245#define DYN_DST_PORT 0x8
246
a998c492 247 uint16_t conn_limit;
984263bc
MD
248} ipfw_insn_limit;
249
250/*
251 * This is used for log instructions
252 */
253typedef struct _ipfw_insn_log {
254 ipfw_insn o;
a998c492
SZ
255 uint32_t max_log; /* how many do we log -- 0 = all */
256 uint32_t log_left; /* how many left to log */
984263bc
MD
257} ipfw_insn_log;
258
b78533e2
SZ
259#ifdef _KERNEL
260
984263bc
MD
261/*
262 * Here we have the structure representing an ipfw rule.
263 *
264 * It starts with a general area (with link fields and counters)
265 * followed by an array of one or more instructions, which the code
266 * accesses as an array of 32-bit values.
267 *
268 * Given a rule pointer r:
269 *
270 * r->cmd is the start of the first instruction.
271 * ACTION_PTR(r) is the start of the first action (things to do
272 * once a rule matched).
273 *
274 * When assembling instruction, remember the following:
275 *
276 * + if a rule has a "keep-state" (or "limit") option, then the
277 * first instruction (at r->cmd) MUST BE an O_PROBE_STATE
278 * + if a rule has a "log" option, then the first action
279 * (at ACTION_PTR(r)) MUST be O_LOG
280 *
281 * NOTE: we use a simple linked list of rules because we never need
282 * to delete a rule without scanning the list. We do not use
283 * queue(3) macros for portability and readability.
284 */
285
286struct ip_fw {
287 struct ip_fw *next; /* linked list of rules */
288 struct ip_fw *next_rule; /* ptr to next [skipto] rule */
a998c492
SZ
289 uint16_t act_ofs; /* offset of action in 32-bit units */
290 uint16_t cmd_len; /* # of 32-bit words in cmd */
291 uint16_t rulenum; /* rule number */
292 uint8_t set; /* rule set (0..31) */
293 uint8_t usr_flags; /* IPFW_USR_F_ */
984263bc
MD
294
295 /* These fields are present in all rules. */
a998c492
SZ
296 uint64_t pcnt; /* Packet counter */
297 uint64_t bcnt; /* Byte counter */
298 uint32_t timestamp; /* tv_sec of last match */
984263bc 299
ca12e259 300 int cpuid; /* owner cpu */
6998b243
SZ
301 struct ip_fw *sibling; /* clone on next cpu */
302
303 struct ip_fw **cross_rules; /* cross referenced rules */
304 uint64_t cross_refs; /* cross references */
ca12e259 305
84a3e25a
SZ
306 uint32_t refcnt; /* Ref count for transit pkts */
307 uint32_t rule_flags; /* IPFW_RULE_F_ */
2187815d 308 uintptr_t track_ruleid; /* ruleid for src/dst tracks */
84a3e25a 309
984263bc
MD
310 ipfw_insn cmd[1]; /* storage for commands */
311};
312
84a3e25a 313#define IPFW_RULE_F_INVALID 0x1
2187815d
SZ
314/* unused 0x2 */
315#define IPFW_RULE_F_GENSTATE 0x4
316#define IPFW_RULE_F_GENTRACK 0x8
6998b243 317#define IPFW_RULE_F_CROSSREF 0x10
84a3e25a 318
b78533e2 319#define RULESIZE(rule) (sizeof(struct ip_fw) + (rule)->cmd_len * 4 - 4)
984263bc
MD
320
321/*
322 * This structure is used as a flow mask and a flow id for various
323 * parts of the code.
324 */
325struct ipfw_flow_id {
a998c492
SZ
326 uint32_t dst_ip;
327 uint32_t src_ip;
328 uint16_t dst_port;
329 uint16_t src_port;
330 uint8_t proto;
331 uint8_t flags; /* protocol-specific flags */
984263bc
MD
332};
333
984263bc
MD
334/*
335 * Main firewall chains definitions and global var's definitions.
336 */
984263bc 337
29b27cb7
SZ
338/* ipfw_chk/ip_fw_chk_ptr return values */
339#define IP_FW_PASS 0
340#define IP_FW_DENY 1
341#define IP_FW_DIVERT 2
342#define IP_FW_TEE 3
343#define IP_FW_DUMMYNET 4
6998b243 344#define IP_FW_CONTINUE 5
984263bc
MD
345
346/*
347 * arguments for calling ipfw_chk() and dummynet_io(). We put them
348 * all into a structure because this way it is easier and more
349 * efficient to pass variables around and extend the interface.
350 */
351struct ip_fw_args {
352 struct mbuf *m; /* the mbuf chain */
353 struct ifnet *oif; /* output interface */
984263bc
MD
354 struct ip_fw *rule; /* matching rule */
355 struct ether_header *eh; /* for bridged packets */
356
984263bc 357 struct ipfw_flow_id f_id; /* grabbed from IP header */
6998b243 358 uint8_t cont;
29b27cb7
SZ
359
360 /*
361 * Depend on the return value of ipfw_chk/ip_fw_chk_ptr
362 * 'cookie' field may save following information:
363 *
364 * IP_FW_TEE or IP_FW_DIVERT
365 * The divert port number
366 *
367 * IP_FW_DUMMYNET
368 * The pipe or queue number
369 */
370 uint32_t cookie;
984263bc
MD
371};
372
373/*
374 * Function definitions.
375 */
aed810cb 376int ip_fw_sockopt(struct sockopt *);
984263bc
MD
377
378/* Firewall hooks */
379struct sockopt;
380struct dn_flow_set;
381
84a3e25a
SZ
382typedef int ip_fw_chk_t(struct ip_fw_args *);
383typedef int ip_fw_ctl_t(struct sockopt *);
b089787f
SZ
384typedef struct mbuf
385 *ip_fw_dn_io_t(struct mbuf *, int, int, struct ip_fw_args *);
84a3e25a
SZ
386
387extern ip_fw_chk_t *ip_fw_chk_ptr;
388extern ip_fw_ctl_t *ip_fw_ctl_ptr;
389extern ip_fw_dn_io_t *ip_fw_dn_io_ptr;
390
984263bc
MD
391extern int fw_one_pass;
392extern int fw_enable;
2930a997
SZ
393
394extern int ip_fw_loaded;
395#define IPFW_LOADED (ip_fw_loaded)
b78533e2 396
984263bc
MD
397#endif /* _KERNEL */
398
b78533e2
SZ
399#define ACTION_PTR(rule) \
400 (ipfw_insn *)((uint32_t *)((rule)->cmd) + ((rule)->act_ofs))
401
402struct ipfw_ioc_rule {
403 uint16_t act_ofs; /* offset of action in 32-bit units */
404 uint16_t cmd_len; /* # of 32-bit words in cmd */
405 uint16_t rulenum; /* rule number */
406 uint8_t set; /* rule set (0..31) */
407 uint8_t usr_flags; /* IPFW_USR_F_ */
408
409 /* Rule set information */
410 uint32_t set_disable; /* disabled rule sets */
411 uint32_t static_count; /* # of static rules */
412 uint32_t static_len; /* total length of static rules */
413
414 /* Statistics */
415 uint64_t pcnt; /* Packet counter */
416 uint64_t bcnt; /* Byte counter */
417 uint32_t timestamp; /* tv_sec of last match */
418
419 uint8_t reserved[16];
420
421 ipfw_insn cmd[1]; /* storage for commands */
422};
423
424#define IPFW_USR_F_NORULE 0x01
425
426#define IPFW_RULE_SIZE_MAX 255 /* unit: uint32_t */
427
428#define IOC_RULESIZE(rule) \
429 (sizeof(struct ipfw_ioc_rule) + (rule)->cmd_len * 4 - 4)
430
431struct ipfw_ioc_flowid {
432 uint16_t type; /* ETHERTYPE_ */
433 uint16_t pad;
434 union {
435 struct {
436 uint32_t dst_ip;
437 uint32_t src_ip;
438 uint16_t dst_port;
439 uint16_t src_port;
440 uint8_t proto;
441 } ip;
442 uint8_t pad[64];
443 } u;
444};
445
446struct ipfw_ioc_state {
447 uint32_t expire; /* expire time */
448 uint64_t pcnt; /* packet match counter */
449 uint64_t bcnt; /* byte match counter */
450
451 uint16_t dyn_type; /* rule type */
452 uint16_t count; /* refcount */
453
454 uint16_t rulenum;
455 uint16_t pad;
456
457 int cpu; /* reserved */
458
459 struct ipfw_ioc_flowid id; /* (masked) flow id */
460 uint8_t reserved[16];
461};
462
463/*
464 * Definitions for IP option names.
465 */
466#define IP_FW_IPOPT_LSRR 0x01
467#define IP_FW_IPOPT_SSRR 0x02
468#define IP_FW_IPOPT_RR 0x04
469#define IP_FW_IPOPT_TS 0x08
470
471/*
472 * Definitions for TCP option names.
473 */
474#define IP_FW_TCPOPT_MSS 0x01
475#define IP_FW_TCPOPT_WINDOW 0x02
476#define IP_FW_TCPOPT_SACK 0x04
477#define IP_FW_TCPOPT_TS 0x08
478#define IP_FW_TCPOPT_CC 0x10
479
480#define ICMP_REJECT_RST 0x100 /* fake ICMP code (send a TCP RST) */
481
d938108c
SZ
482/*
483 * IP_FW_TBL_CREATE, tableid >= 0.
484 * IP_FW_TBL_FLUSH, tableid >= 0.
485 * IP_FW_TBL_FLUSH, tableid < 0, flush all tables.
486 * IP_FW_TBL_DESTROY, tableid >= 0.
487 * IP_FW_TBL_ZERO, tableid >= 0.
488 * IP_FW_TBL_ZERO, tableid < 0, zero all tables' counters.
489 */
490struct ipfw_ioc_table {
491 int tableid;
492};
493
494struct ipfw_ioc_tblent {
495 struct sockaddr_in key;
496 struct sockaddr_in netmask;
497 u_long use;
498 time_t last_used;
499 long unused[2];
500};
501
502/*
503 * IP_FW_TBL_GET, tableid < 0, list of all tables.
504 */
505struct ipfw_ioc_tbllist {
506 int tableid; /* MUST be the first field */
507 int tablecnt;
508 uint16_t tables[];
509};
510
511/*
512 * IP_FW_TBL_GET, tableid >= 0, entries in the table.
513 * IP_FW_TBL_ADD, tableid >= 0, entcnt == 1.
514 * IP_FW_TBL_DEL, tableid >= 0, entcnt == 1.
515 */
516struct ipfw_ioc_tblcont {
517 int tableid; /* MUST be the first field */
518 int entcnt;
519 struct ipfw_ioc_tblent ent[1];
520};
521
522/*
523 * IP_FW_TBL_EXPIRE, tableid < 0, expire all tables.
524 * IP_FW_TBL_EXPIRE, tableid >= 0.
525 */
526struct ipfw_ioc_tblexp {
527 int tableid;
528 int expcnt;
529 time_t expire;
530 u_long unused1[2];
531};
532
984263bc 533#endif /* _IPFW2_H */