FreeBSD/Linux Kernel Cross Reference
sys/netinet/ip_fw.h
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD$
28 */
29
30 #ifndef _IPFW2_H
31 #define _IPFW2_H
32
33 /*
34 * The default rule number. By the design of ip_fw, the default rule
35 * is the last one, so its number can also serve as the highest number
36 * allowed for a rule. The ip_fw code relies on both meanings of this
37 * constant.
38 */
39 #define IPFW_DEFAULT_RULE 65535
40
41 #define RESVD_SET 31 /*set for default and persistent rules*/
42 #define IPFW_MAX_SETS 32 /* Number of sets supported by ipfw*/
43
44 /*
45 * Compat values for old clients
46 */
47 #ifndef _KERNEL
48 #define IPFW_TABLES_MAX 65535
49 #define IPFW_TABLES_DEFAULT 128
50 #endif
51
52 /*
53 * Most commands (queue, pipe, tag, untag, limit...) can have a 16-bit
54 * argument between 1 and 65534. The value 0 (IP_FW_TARG) is used
55 * to represent 'tablearg' value, e.g. indicate the use of a 'tablearg'
56 * result of the most recent table() lookup.
57 * Note that 16bit is only a historical limit, resulting from
58 * the use of a 16-bit fields for that value. In reality, we can have
59 * 2^32 pipes, queues, tag values and so on.
60 */
61 #define IPFW_ARG_MIN 1
62 #define IPFW_ARG_MAX 65534
63 #define IP_FW_TABLEARG 65535 /* Compat value for old clients */
64 #define IP_FW_TARG 0 /* Current tablearg value */
65 #define IP_FW_NAT44_GLOBAL 65535 /* arg1 value for "nat global" */
66
67 /*
68 * Number of entries in the call stack of the call/return commands.
69 * Call stack currently is an uint16_t array with rule numbers.
70 */
71 #define IPFW_CALLSTACK_SIZE 16
72
73 /* IP_FW3 header/opcodes */
74 typedef struct _ip_fw3_opheader {
75 uint16_t opcode; /* Operation opcode */
76 uint16_t version; /* Opcode version */
77 uint16_t reserved[2]; /* Align to 64-bit boundary */
78 } ip_fw3_opheader;
79
80 /* IP_FW3 opcodes */
81 #define IP_FW_TABLE_XADD 86 /* add entry */
82 #define IP_FW_TABLE_XDEL 87 /* delete entry */
83 #define IP_FW_TABLE_XGETSIZE 88 /* get table size (deprecated) */
84 #define IP_FW_TABLE_XLIST 89 /* list table contents */
85 #define IP_FW_TABLE_XDESTROY 90 /* destroy table */
86 #define IP_FW_TABLES_XLIST 92 /* list all tables */
87 #define IP_FW_TABLE_XINFO 93 /* request info for one table */
88 #define IP_FW_TABLE_XFLUSH 94 /* flush table data */
89 #define IP_FW_TABLE_XCREATE 95 /* create new table */
90 #define IP_FW_TABLE_XMODIFY 96 /* modify existing table */
91 #define IP_FW_XGET 97 /* Retrieve configuration */
92 #define IP_FW_XADD 98 /* add rule */
93 #define IP_FW_XDEL 99 /* del rule */
94 #define IP_FW_XMOVE 100 /* move rules to different set */
95 #define IP_FW_XZERO 101 /* clear accounting */
96 #define IP_FW_XRESETLOG 102 /* zero rules logs */
97 #define IP_FW_SET_SWAP 103 /* Swap between 2 sets */
98 #define IP_FW_SET_MOVE 104 /* Move one set to another one */
99 #define IP_FW_SET_ENABLE 105 /* Enable/disable sets */
100 #define IP_FW_TABLE_XFIND 106 /* finds an entry */
101 #define IP_FW_XIFLIST 107 /* list tracked interfaces */
102 #define IP_FW_TABLES_ALIST 108 /* list table algorithms */
103 #define IP_FW_TABLE_XSWAP 109 /* swap two tables */
104 #define IP_FW_TABLE_VLIST 110 /* dump table value hash */
105
106 #define IP_FW_NAT44_XCONFIG 111 /* Create/modify NAT44 instance */
107 #define IP_FW_NAT44_DESTROY 112 /* Destroys NAT44 instance */
108 #define IP_FW_NAT44_XGETCONFIG 113 /* Get NAT44 instance config */
109 #define IP_FW_NAT44_LIST_NAT 114 /* List all NAT44 instances */
110 #define IP_FW_NAT44_XGETLOG 115 /* Get log from NAT44 instance */
111
112 #define IP_FW_DUMP_SOPTCODES 116 /* Dump available sopts/versions */
113 #define IP_FW_DUMP_SRVOBJECTS 117 /* Dump existing named objects */
114
115 #define IP_FW_NAT64STL_CREATE 130 /* Create stateless NAT64 instance */
116 #define IP_FW_NAT64STL_DESTROY 131 /* Destroy stateless NAT64 instance */
117 #define IP_FW_NAT64STL_CONFIG 132 /* Modify stateless NAT64 instance */
118 #define IP_FW_NAT64STL_LIST 133 /* List stateless NAT64 instances */
119 #define IP_FW_NAT64STL_STATS 134 /* Get NAT64STL instance statistics */
120 #define IP_FW_NAT64STL_RESET_STATS 135 /* Reset NAT64STL instance statistics */
121
122 #define IP_FW_NAT64LSN_CREATE 140 /* Create stateful NAT64 instance */
123 #define IP_FW_NAT64LSN_DESTROY 141 /* Destroy stateful NAT64 instance */
124 #define IP_FW_NAT64LSN_CONFIG 142 /* Modify stateful NAT64 instance */
125 #define IP_FW_NAT64LSN_LIST 143 /* List stateful NAT64 instances */
126 #define IP_FW_NAT64LSN_STATS 144 /* Get NAT64LSN instance statistics */
127 #define IP_FW_NAT64LSN_LIST_STATES 145 /* Get stateful NAT64 states */
128 #define IP_FW_NAT64LSN_RESET_STATS 146 /* Reset NAT64LSN instance statistics */
129
130 #define IP_FW_NPTV6_CREATE 150 /* Create NPTv6 instance */
131 #define IP_FW_NPTV6_DESTROY 151 /* Destroy NPTv6 instance */
132 #define IP_FW_NPTV6_CONFIG 152 /* Modify NPTv6 instance */
133 #define IP_FW_NPTV6_LIST 153 /* List NPTv6 instances */
134 #define IP_FW_NPTV6_STATS 154 /* Get NPTv6 instance statistics */
135 #define IP_FW_NPTV6_RESET_STATS 155 /* Reset NPTv6 instance statistics */
136
137 #define IP_FW_NAT64CLAT_CREATE 160 /* Create clat NAT64 instance */
138 #define IP_FW_NAT64CLAT_DESTROY 161 /* Destroy clat NAT64 instance */
139 #define IP_FW_NAT64CLAT_CONFIG 162 /* Modify clat NAT64 instance */
140 #define IP_FW_NAT64CLAT_LIST 163 /* List clat NAT64 instances */
141 #define IP_FW_NAT64CLAT_STATS 164 /* Get NAT64CLAT instance statistics */
142 #define IP_FW_NAT64CLAT_RESET_STATS 165 /* Reset NAT64CLAT instance statistics */
143
144 /*
145 * The kernel representation of ipfw rules is made of a list of
146 * 'instructions' (for all practical purposes equivalent to BPF
147 * instructions), which specify which fields of the packet
148 * (or its metadata) should be analysed.
149 *
150 * Each instruction is stored in a structure which begins with
151 * "ipfw_insn", and can contain extra fields depending on the
152 * instruction type (listed below).
153 * Note that the code is written so that individual instructions
154 * have a size which is a multiple of 32 bits. This means that, if
155 * such structures contain pointers or other 64-bit entities,
156 * (there is just one instance now) they may end up unaligned on
157 * 64-bit architectures, so the must be handled with care.
158 *
159 * "enum ipfw_opcodes" are the opcodes supported. We can have up
160 * to 256 different opcodes. When adding new opcodes, they should
161 * be appended to the end of the opcode list before O_LAST_OPCODE,
162 * this will prevent the ABI from being broken, otherwise users
163 * will have to recompile ipfw(8) when they update the kernel.
164 */
165
166 enum ipfw_opcodes { /* arguments (4 byte each) */
167 O_NOP,
168
169 O_IP_SRC, /* u32 = IP */
170 O_IP_SRC_MASK, /* ip = IP/mask */
171 O_IP_SRC_ME, /* none */
172 O_IP_SRC_SET, /* u32=base, arg1=len, bitmap */
173
174 O_IP_DST, /* u32 = IP */
175 O_IP_DST_MASK, /* ip = IP/mask */
176 O_IP_DST_ME, /* none */
177 O_IP_DST_SET, /* u32=base, arg1=len, bitmap */
178
179 O_IP_SRCPORT, /* (n)port list:mask 4 byte ea */
180 O_IP_DSTPORT, /* (n)port list:mask 4 byte ea */
181 O_PROTO, /* arg1=protocol */
182
183 O_MACADDR2, /* 2 mac addr:mask */
184 O_MAC_TYPE, /* same as srcport */
185
186 O_LAYER2, /* none */
187 O_IN, /* none */
188 O_FRAG, /* none */
189
190 O_RECV, /* none */
191 O_XMIT, /* none */
192 O_VIA, /* none */
193
194 O_IPOPT, /* arg1 = 2*u8 bitmap */
195 O_IPLEN, /* arg1 = len */
196 O_IPID, /* arg1 = id */
197
198 O_IPTOS, /* arg1 = id */
199 O_IPPRECEDENCE, /* arg1 = precedence << 5 */
200 O_IPTTL, /* arg1 = TTL */
201
202 O_IPVER, /* arg1 = version */
203 O_UID, /* u32 = id */
204 O_GID, /* u32 = id */
205 O_ESTAB, /* none (tcp established) */
206 O_TCPFLAGS, /* arg1 = 2*u8 bitmap */
207 O_TCPWIN, /* arg1 = desired win */
208 O_TCPSEQ, /* u32 = desired seq. */
209 O_TCPACK, /* u32 = desired seq. */
210 O_ICMPTYPE, /* u32 = icmp bitmap */
211 O_TCPOPTS, /* arg1 = 2*u8 bitmap */
212
213 O_VERREVPATH, /* none */
214 O_VERSRCREACH, /* none */
215
216 O_PROBE_STATE, /* none */
217 O_KEEP_STATE, /* none */
218 O_LIMIT, /* ipfw_insn_limit */
219 O_LIMIT_PARENT, /* dyn_type, not an opcode. */
220
221 /*
222 * These are really 'actions'.
223 */
224
225 O_LOG, /* ipfw_insn_log */
226 O_PROB, /* u32 = match probability */
227
228 O_CHECK_STATE, /* none */
229 O_ACCEPT, /* none */
230 O_DENY, /* none */
231 O_REJECT, /* arg1=icmp arg (same as deny) */
232 O_COUNT, /* none */
233 O_SKIPTO, /* arg1=next rule number */
234 O_PIPE, /* arg1=pipe number */
235 O_QUEUE, /* arg1=queue number */
236 O_DIVERT, /* arg1=port number */
237 O_TEE, /* arg1=port number */
238 O_FORWARD_IP, /* fwd sockaddr */
239 O_FORWARD_MAC, /* fwd mac */
240 O_NAT, /* nope */
241 O_REASS, /* none */
242
243 /*
244 * More opcodes.
245 */
246 O_IPSEC, /* has ipsec history */
247 O_IP_SRC_LOOKUP, /* arg1=table number, u32=value */
248 O_IP_DST_LOOKUP, /* arg1=table number, u32=value */
249 O_ANTISPOOF, /* none */
250 O_JAIL, /* u32 = id */
251 O_ALTQ, /* u32 = altq classif. qid */
252 O_DIVERTED, /* arg1=bitmap (1:loop, 2:out) */
253 O_TCPDATALEN, /* arg1 = tcp data len */
254 O_IP6_SRC, /* address without mask */
255 O_IP6_SRC_ME, /* my addresses */
256 O_IP6_SRC_MASK, /* address with the mask */
257 O_IP6_DST,
258 O_IP6_DST_ME,
259 O_IP6_DST_MASK,
260 O_FLOW6ID, /* for flow id tag in the ipv6 pkt */
261 O_ICMP6TYPE, /* icmp6 packet type filtering */
262 O_EXT_HDR, /* filtering for ipv6 extension header */
263 O_IP6,
264
265 /*
266 * actions for ng_ipfw
267 */
268 O_NETGRAPH, /* send to ng_ipfw */
269 O_NGTEE, /* copy to ng_ipfw */
270
271 O_IP4,
272
273 O_UNREACH6, /* arg1=icmpv6 code arg (deny) */
274
275 O_TAG, /* arg1=tag number */
276 O_TAGGED, /* arg1=tag number */
277
278 O_SETFIB, /* arg1=FIB number */
279 O_FIB, /* arg1=FIB desired fib number */
280
281 O_SOCKARG, /* socket argument */
282
283 O_CALLRETURN, /* arg1=called rule number */
284
285 O_FORWARD_IP6, /* fwd sockaddr_in6 */
286
287 O_DSCP, /* 2 u32 = DSCP mask */
288 O_SETDSCP, /* arg1=DSCP value */
289 O_IP_FLOW_LOOKUP, /* arg1=table number, u32=value */
290
291 O_EXTERNAL_ACTION, /* arg1=id of external action handler */
292 O_EXTERNAL_INSTANCE, /* arg1=id of eaction handler instance */
293 O_EXTERNAL_DATA, /* variable length data */
294
295 O_SKIP_ACTION, /* none */
296 O_TCPMSS, /* arg1=MSS value */
297
298 O_MAC_SRC_LOOKUP, /* arg1=table number, u32=value */
299 O_MAC_DST_LOOKUP, /* arg1=table number, u32=value */
300
301 O_LAST_OPCODE /* not an opcode! */
302 };
303
304 /*
305 * Defines key types used by lookup instruction
306 */
307 enum ipfw_table_lookup_type {
308 LOOKUP_DST_IP,
309 LOOKUP_SRC_IP,
310 LOOKUP_DST_PORT,
311 LOOKUP_SRC_PORT,
312 LOOKUP_UID,
313 LOOKUP_JAIL,
314 LOOKUP_DSCP,
315 LOOKUP_DST_MAC,
316 LOOKUP_SRC_MAC,
317 };
318
319 /*
320 * The extension header are filtered only for presence using a bit
321 * vector with a flag for each header.
322 */
323 #define EXT_FRAGMENT 0x1
324 #define EXT_HOPOPTS 0x2
325 #define EXT_ROUTING 0x4
326 #define EXT_AH 0x8
327 #define EXT_ESP 0x10
328 #define EXT_DSTOPTS 0x20
329 #define EXT_RTHDR0 0x40
330 #define EXT_RTHDR2 0x80
331
332 /*
333 * Template for instructions.
334 *
335 * ipfw_insn is used for all instructions which require no operands,
336 * a single 16-bit value (arg1), or a couple of 8-bit values.
337 *
338 * For other instructions which require different/larger arguments
339 * we have derived structures, ipfw_insn_*.
340 *
341 * The size of the instruction (in 32-bit words) is in the low
342 * 6 bits of "len". The 2 remaining bits are used to implement
343 * NOT and OR on individual instructions. Given a type, you can
344 * compute the length to be put in "len" using F_INSN_SIZE(t)
345 *
346 * F_NOT negates the match result of the instruction.
347 *
348 * F_OR is used to build or blocks. By default, instructions
349 * are evaluated as part of a logical AND. An "or" block
350 * { X or Y or Z } contains F_OR set in all but the last
351 * instruction of the block. A match will cause the code
352 * to skip past the last instruction of the block.
353 *
354 * NOTA BENE: in a couple of places we assume that
355 * sizeof(ipfw_insn) == sizeof(u_int32_t)
356 * this needs to be fixed.
357 *
358 */
359 typedef struct _ipfw_insn { /* template for instructions */
360 _Alignas(_Alignof(u_int32_t)) u_int8_t opcode;
361 u_int8_t len; /* number of 32-bit words */
362 #define F_NOT 0x80
363 #define F_OR 0x40
364 #define F_LEN_MASK 0x3f
365 #define F_LEN(cmd) ((cmd)->len & F_LEN_MASK)
366
367 u_int16_t arg1;
368 } ipfw_insn;
369
370 /*
371 * The F_INSN_SIZE(type) computes the size, in 4-byte words, of
372 * a given type.
373 */
374 #define F_INSN_SIZE(t) ((sizeof (t))/sizeof(u_int32_t))
375
376 /*
377 * This is used to store an array of 16-bit entries (ports etc.)
378 */
379 typedef struct _ipfw_insn_u16 {
380 ipfw_insn o;
381 u_int16_t ports[2]; /* there may be more */
382 } ipfw_insn_u16;
383
384 /*
385 * This is used to store an array of 32-bit entries
386 * (uid, single IPv4 addresses etc.)
387 */
388 typedef struct _ipfw_insn_u32 {
389 ipfw_insn o;
390 u_int32_t d[1]; /* one or more */
391 } ipfw_insn_u32;
392
393 /*
394 * This is used to store IP addr-mask pairs.
395 */
396 typedef struct _ipfw_insn_ip {
397 ipfw_insn o;
398 struct in_addr addr;
399 struct in_addr mask;
400 } ipfw_insn_ip;
401
402 /*
403 * This is used to forward to a given address (ip).
404 */
405 typedef struct _ipfw_insn_sa {
406 ipfw_insn o;
407 struct sockaddr_in sa;
408 } ipfw_insn_sa;
409
410 /*
411 * This is used to forward to a given address (ipv6).
412 */
413 typedef struct _ipfw_insn_sa6 {
414 ipfw_insn o;
415 struct sockaddr_in6 sa;
416 } ipfw_insn_sa6;
417
418 /*
419 * This is used for MAC addr-mask pairs.
420 */
421 typedef struct _ipfw_insn_mac {
422 ipfw_insn o;
423 u_char addr[12]; /* dst[6] + src[6] */
424 u_char mask[12]; /* dst[6] + src[6] */
425 } ipfw_insn_mac;
426
427 /*
428 * This is used for interface match rules (recv xx, xmit xx).
429 */
430 typedef struct _ipfw_insn_if {
431 ipfw_insn o;
432 union {
433 struct in_addr ip;
434 int glob;
435 uint16_t kidx;
436 } p;
437 char name[IFNAMSIZ];
438 } ipfw_insn_if;
439
440 /*
441 * This is used for storing an altq queue id number.
442 */
443 typedef struct _ipfw_insn_altq {
444 ipfw_insn o;
445 u_int32_t qid;
446 } ipfw_insn_altq;
447
448 /*
449 * This is used for limit rules.
450 */
451 typedef struct _ipfw_insn_limit {
452 ipfw_insn o;
453 u_int8_t _pad;
454 u_int8_t limit_mask; /* combination of DYN_* below */
455 #define DYN_SRC_ADDR 0x1
456 #define DYN_SRC_PORT 0x2
457 #define DYN_DST_ADDR 0x4
458 #define DYN_DST_PORT 0x8
459
460 u_int16_t conn_limit;
461 } ipfw_insn_limit;
462
463 /*
464 * This is used for log instructions.
465 */
466 typedef struct _ipfw_insn_log {
467 ipfw_insn o;
468 u_int32_t max_log; /* how many do we log -- 0 = all */
469 u_int32_t log_left; /* how many left to log */
470 } ipfw_insn_log;
471
472 /* Legacy NAT structures, compat only */
473 #ifndef _KERNEL
474 /*
475 * Data structures required by both ipfw(8) and ipfw(4) but not part of the
476 * management API are protected by IPFW_INTERNAL.
477 */
478 #ifdef IPFW_INTERNAL
479 /* Server pool support (LSNAT). */
480 struct cfg_spool {
481 LIST_ENTRY(cfg_spool) _next; /* chain of spool instances */
482 struct in_addr addr;
483 u_short port;
484 };
485 #endif
486
487 /* Redirect modes id. */
488 #define REDIR_ADDR 0x01
489 #define REDIR_PORT 0x02
490 #define REDIR_PROTO 0x04
491
492 #ifdef IPFW_INTERNAL
493 /* Nat redirect configuration. */
494 struct cfg_redir {
495 LIST_ENTRY(cfg_redir) _next; /* chain of redir instances */
496 u_int16_t mode; /* type of redirect mode */
497 struct in_addr laddr; /* local ip address */
498 struct in_addr paddr; /* public ip address */
499 struct in_addr raddr; /* remote ip address */
500 u_short lport; /* local port */
501 u_short pport; /* public port */
502 u_short rport; /* remote port */
503 u_short pport_cnt; /* number of public ports */
504 u_short rport_cnt; /* number of remote ports */
505 int proto; /* protocol: tcp/udp */
506 struct alias_link **alink;
507 /* num of entry in spool chain */
508 u_int16_t spool_cnt;
509 /* chain of spool instances */
510 LIST_HEAD(spool_chain, cfg_spool) spool_chain;
511 };
512 #endif
513
514 #ifdef IPFW_INTERNAL
515 /* Nat configuration data struct. */
516 struct cfg_nat {
517 /* chain of nat instances */
518 LIST_ENTRY(cfg_nat) _next;
519 int id; /* nat id */
520 struct in_addr ip; /* nat ip address */
521 char if_name[IF_NAMESIZE]; /* interface name */
522 int mode; /* aliasing mode */
523 struct libalias *lib; /* libalias instance */
524 /* number of entry in spool chain */
525 int redir_cnt;
526 /* chain of redir instances */
527 LIST_HEAD(redir_chain, cfg_redir) redir_chain;
528 };
529 #endif
530
531 #define SOF_NAT sizeof(struct cfg_nat)
532 #define SOF_REDIR sizeof(struct cfg_redir)
533 #define SOF_SPOOL sizeof(struct cfg_spool)
534
535 #endif /* ifndef _KERNEL */
536
537 struct nat44_cfg_spool {
538 struct in_addr addr;
539 uint16_t port;
540 uint16_t spare;
541 };
542 #define NAT44_REDIR_ADDR 0x01
543 #define NAT44_REDIR_PORT 0x02
544 #define NAT44_REDIR_PROTO 0x04
545
546 /* Nat redirect configuration. */
547 struct nat44_cfg_redir {
548 struct in_addr laddr; /* local ip address */
549 struct in_addr paddr; /* public ip address */
550 struct in_addr raddr; /* remote ip address */
551 uint16_t lport; /* local port */
552 uint16_t pport; /* public port */
553 uint16_t rport; /* remote port */
554 uint16_t pport_cnt; /* number of public ports */
555 uint16_t rport_cnt; /* number of remote ports */
556 uint16_t mode; /* type of redirect mode */
557 uint16_t spool_cnt; /* num of entry in spool chain */
558 uint16_t spare;
559 uint32_t proto; /* protocol: tcp/udp */
560 };
561
562 /* Nat configuration data struct. */
563 struct nat44_cfg_nat {
564 char name[64]; /* nat name */
565 char if_name[64]; /* interface name */
566 uint32_t size; /* structure size incl. redirs */
567 struct in_addr ip; /* nat IPv4 address */
568 uint32_t mode; /* aliasing mode */
569 uint32_t redir_cnt; /* number of entry in spool chain */
570 u_short alias_port_lo; /* low range for port aliasing */
571 u_short alias_port_hi; /* high range for port aliasing */
572 };
573
574 /* Nat command. */
575 typedef struct _ipfw_insn_nat {
576 ipfw_insn o;
577 struct cfg_nat *nat;
578 } ipfw_insn_nat;
579
580 /* Apply ipv6 mask on ipv6 addr */
581 #define APPLY_MASK(addr,mask) do { \
582 (addr)->__u6_addr.__u6_addr32[0] &= (mask)->__u6_addr.__u6_addr32[0]; \
583 (addr)->__u6_addr.__u6_addr32[1] &= (mask)->__u6_addr.__u6_addr32[1]; \
584 (addr)->__u6_addr.__u6_addr32[2] &= (mask)->__u6_addr.__u6_addr32[2]; \
585 (addr)->__u6_addr.__u6_addr32[3] &= (mask)->__u6_addr.__u6_addr32[3]; \
586 } while (0)
587
588 /* Structure for ipv6 */
589 typedef struct _ipfw_insn_ip6 {
590 ipfw_insn o;
591 struct in6_addr addr6;
592 struct in6_addr mask6;
593 } ipfw_insn_ip6;
594
595 /* Used to support icmp6 types */
596 typedef struct _ipfw_insn_icmp6 {
597 ipfw_insn o;
598 uint32_t d[7]; /* XXX This number si related to the netinet/icmp6.h
599 * define ICMP6_MAXTYPE
600 * as follows: n = ICMP6_MAXTYPE/32 + 1
601 * Actually is 203
602 */
603 } ipfw_insn_icmp6;
604
605 /*
606 * Here we have the structure representing an ipfw rule.
607 *
608 * Layout:
609 * struct ip_fw_rule
610 * [ counter block, size = rule->cntr_len ]
611 * [ one or more instructions, size = rule->cmd_len * 4 ]
612 *
613 * It starts with a general area (with link fields).
614 * Counter block may be next (if rule->cntr_len > 0),
615 * followed by an array of one or more instructions, which the code
616 * accesses as an array of 32-bit values. rule->cmd_len represents
617 * the total instructions legth in u32 worrd, while act_ofs represents
618 * rule action offset in u32 words.
619 *
620 * When assembling instruction, remember the following:
621 *
622 * + if a rule has a "keep-state" (or "limit") option, then the
623 * first instruction (at r->cmd) MUST BE an O_PROBE_STATE
624 * + if a rule has a "log" option, then the first action
625 * (at ACTION_PTR(r)) MUST be O_LOG
626 * + if a rule has an "altq" option, it comes after "log"
627 * + if a rule has an O_TAG option, it comes after "log" and "altq"
628 *
629 *
630 * All structures (excluding instructions) are u64-aligned.
631 * Please keep this.
632 */
633
634 struct ip_fw_rule {
635 uint16_t act_ofs; /* offset of action in 32-bit units */
636 uint16_t cmd_len; /* # of 32-bit words in cmd */
637 uint16_t spare;
638 uint8_t set; /* rule set (0..31) */
639 uint8_t flags; /* rule flags */
640 uint32_t rulenum; /* rule number */
641 uint32_t id; /* rule id */
642
643 ipfw_insn cmd[1]; /* storage for commands */
644 };
645 #define IPFW_RULE_NOOPT 0x01 /* Has no options in body */
646 #define IPFW_RULE_JUSTOPTS 0x02 /* new format of rule body */
647
648 /* Unaligned version */
649
650 /* Base ipfw rule counter block. */
651 struct ip_fw_bcounter {
652 uint16_t size; /* Size of counter block, bytes */
653 uint8_t flags; /* flags for given block */
654 uint8_t spare;
655 uint32_t timestamp; /* tv_sec of last match */
656 uint64_t pcnt; /* Packet counter */
657 uint64_t bcnt; /* Byte counter */
658 };
659
660 #ifndef _KERNEL
661 /*
662 * Legacy rule format
663 */
664 struct ip_fw {
665 struct ip_fw *x_next; /* linked list of rules */
666 struct ip_fw *next_rule; /* ptr to next [skipto] rule */
667 /* 'next_rule' is used to pass up 'set_disable' status */
668
669 uint16_t act_ofs; /* offset of action in 32-bit units */
670 uint16_t cmd_len; /* # of 32-bit words in cmd */
671 uint16_t rulenum; /* rule number */
672 uint8_t set; /* rule set (0..31) */
673 uint8_t _pad; /* padding */
674 uint32_t id; /* rule id */
675
676 /* These fields are present in all rules. */
677 uint64_t pcnt; /* Packet counter */
678 uint64_t bcnt; /* Byte counter */
679 uint32_t timestamp; /* tv_sec of last match */
680
681 ipfw_insn cmd[1]; /* storage for commands */
682 };
683 #endif
684
685 #define ACTION_PTR(rule) \
686 (ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) )
687
688 #define RULESIZE(rule) (sizeof(*(rule)) + (rule)->cmd_len * 4 - 4)
689
690 #if 1 // should be moved to in.h
691 /*
692 * This structure is used as a flow mask and a flow id for various
693 * parts of the code.
694 * addr_type is used in userland and kernel to mark the address type.
695 * fib is used in the kernel to record the fib in use.
696 * _flags is used in the kernel to store tcp flags for dynamic rules.
697 */
698 struct ipfw_flow_id {
699 uint32_t dst_ip;
700 uint32_t src_ip;
701 uint16_t dst_port;
702 uint16_t src_port;
703 uint8_t fib; /* XXX: must be uint16_t */
704 uint8_t proto;
705 uint8_t _flags; /* protocol-specific flags */
706 uint8_t addr_type; /* 4=ip4, 6=ip6, 1=ether ? */
707 struct in6_addr dst_ip6;
708 struct in6_addr src_ip6;
709 uint32_t flow_id6;
710 uint32_t extra; /* queue/pipe or frag_id */
711 };
712 #endif
713
714 #define IS_IP4_FLOW_ID(id) ((id)->addr_type == 4)
715 #define IS_IP6_FLOW_ID(id) ((id)->addr_type == 6)
716
717 /*
718 * Dynamic ipfw rule.
719 */
720 typedef struct _ipfw_dyn_rule ipfw_dyn_rule;
721
722 struct _ipfw_dyn_rule {
723 ipfw_dyn_rule *next; /* linked list of rules. */
724 struct ip_fw *rule; /* pointer to rule */
725 /* 'rule' is used to pass up the rule number (from the parent) */
726
727 ipfw_dyn_rule *parent; /* pointer to parent rule */
728 u_int64_t pcnt; /* packet match counter */
729 u_int64_t bcnt; /* byte match counter */
730 struct ipfw_flow_id id; /* (masked) flow id */
731 u_int32_t expire; /* expire time */
732 u_int32_t bucket; /* which bucket in hash table */
733 u_int32_t state; /* state of this rule (typically a
734 * combination of TCP flags)
735 */
736 #define IPFW_DYN_ORPHANED 0x40000 /* state's parent rule was deleted */
737 u_int32_t ack_fwd; /* most recent ACKs in forward */
738 u_int32_t ack_rev; /* and reverse directions (used */
739 /* to generate keepalives) */
740 u_int16_t dyn_type; /* rule type */
741 u_int16_t count; /* refcount */
742 u_int16_t kidx; /* index of named object */
743 } __packed __aligned(8);
744
745 /*
746 * Definitions for IP option names.
747 */
748 #define IP_FW_IPOPT_LSRR 0x01
749 #define IP_FW_IPOPT_SSRR 0x02
750 #define IP_FW_IPOPT_RR 0x04
751 #define IP_FW_IPOPT_TS 0x08
752
753 /*
754 * Definitions for TCP option names.
755 */
756 #define IP_FW_TCPOPT_MSS 0x01
757 #define IP_FW_TCPOPT_WINDOW 0x02
758 #define IP_FW_TCPOPT_SACK 0x04
759 #define IP_FW_TCPOPT_TS 0x08
760 #define IP_FW_TCPOPT_CC 0x10
761
762 #define ICMP_REJECT_RST 0x100 /* fake ICMP code (send a TCP RST) */
763 #define ICMP6_UNREACH_RST 0x100 /* fake ICMPv6 code (send a TCP RST) */
764 #define ICMP_REJECT_ABORT 0x101 /* fake ICMP code (send an SCTP ABORT) */
765 #define ICMP6_UNREACH_ABORT 0x101 /* fake ICMPv6 code (send an SCTP ABORT) */
766
767 /*
768 * These are used for lookup tables.
769 */
770
771 #define IPFW_TABLE_ADDR 1 /* Table for holding IPv4/IPv6 prefixes */
772 #define IPFW_TABLE_INTERFACE 2 /* Table for holding interface names */
773 #define IPFW_TABLE_NUMBER 3 /* Table for holding ports/uid/gid/etc */
774 #define IPFW_TABLE_FLOW 4 /* Table for holding flow data */
775 #define IPFW_TABLE_MAC 5 /* Table for holding mac address prefixes */
776 #define IPFW_TABLE_MAXTYPE 5 /* Maximum valid number */
777
778 #define IPFW_TABLE_CIDR IPFW_TABLE_ADDR /* compat */
779
780 /* Value types */
781 #define IPFW_VTYPE_LEGACY 0xFFFFFFFF /* All data is filled in */
782 #define IPFW_VTYPE_SKIPTO 0x00000001 /* skipto/call/callreturn */
783 #define IPFW_VTYPE_PIPE 0x00000002 /* pipe/queue */
784 #define IPFW_VTYPE_FIB 0x00000004 /* setfib */
785 #define IPFW_VTYPE_NAT 0x00000008 /* nat */
786 #define IPFW_VTYPE_DSCP 0x00000010 /* dscp */
787 #define IPFW_VTYPE_TAG 0x00000020 /* tag/untag */
788 #define IPFW_VTYPE_DIVERT 0x00000040 /* divert/tee */
789 #define IPFW_VTYPE_NETGRAPH 0x00000080 /* netgraph/ngtee */
790 #define IPFW_VTYPE_LIMIT 0x00000100 /* limit */
791 #define IPFW_VTYPE_NH4 0x00000200 /* IPv4 nexthop */
792 #define IPFW_VTYPE_NH6 0x00000400 /* IPv6 nexthop */
793
794 /* MAC/InfiniBand/etc address length */
795 #define IPFW_MAX_L2_ADDR_LEN 20
796
797 typedef struct _ipfw_table_entry {
798 in_addr_t addr; /* network address */
799 u_int32_t value; /* value */
800 u_int16_t tbl; /* table number */
801 u_int8_t masklen; /* mask length */
802 } ipfw_table_entry;
803
804 typedef struct _ipfw_table_xentry {
805 uint16_t len; /* Total entry length */
806 uint8_t type; /* entry type */
807 uint8_t masklen; /* mask length */
808 uint16_t tbl; /* table number */
809 uint16_t flags; /* record flags */
810 uint32_t value; /* value */
811 union {
812 /* Longest field needs to be aligned by 4-byte boundary */
813 struct in6_addr addr6; /* IPv6 address */
814 char iface[IF_NAMESIZE]; /* interface name */
815 } k;
816 } ipfw_table_xentry;
817 #define IPFW_TCF_INET 0x01 /* CIDR flags: IPv4 record */
818
819 typedef struct _ipfw_table {
820 u_int32_t size; /* size of entries in bytes */
821 u_int32_t cnt; /* # of entries */
822 u_int16_t tbl; /* table number */
823 ipfw_table_entry ent[0]; /* entries */
824 } ipfw_table;
825
826 typedef struct _ipfw_xtable {
827 ip_fw3_opheader opheader; /* IP_FW3 opcode */
828 uint32_t size; /* size of entries in bytes */
829 uint32_t cnt; /* # of entries */
830 uint16_t tbl; /* table number */
831 uint8_t type; /* table type */
832 ipfw_table_xentry xent[0]; /* entries */
833 } ipfw_xtable;
834
835 typedef struct _ipfw_obj_tlv {
836 uint16_t type; /* TLV type */
837 uint16_t flags; /* TLV-specific flags */
838 uint32_t length; /* Total length, aligned to u64 */
839 } ipfw_obj_tlv;
840 #define IPFW_TLV_TBL_NAME 1
841 #define IPFW_TLV_TBLNAME_LIST 2
842 #define IPFW_TLV_RULE_LIST 3
843 #define IPFW_TLV_DYNSTATE_LIST 4
844 #define IPFW_TLV_TBL_ENT 5
845 #define IPFW_TLV_DYN_ENT 6
846 #define IPFW_TLV_RULE_ENT 7
847 #define IPFW_TLV_TBLENT_LIST 8
848 #define IPFW_TLV_RANGE 9
849 #define IPFW_TLV_EACTION 10
850 #define IPFW_TLV_COUNTERS 11
851 #define IPFW_TLV_OBJDATA 12
852 #define IPFW_TLV_STATE_NAME 14
853
854 #define IPFW_TLV_EACTION_BASE 1000
855 #define IPFW_TLV_EACTION_NAME(arg) (IPFW_TLV_EACTION_BASE + (arg))
856
857 typedef struct _ipfw_obj_data {
858 ipfw_obj_tlv head;
859 void *data[0];
860 } ipfw_obj_data;
861
862 /* Object name TLV */
863 typedef struct _ipfw_obj_ntlv {
864 ipfw_obj_tlv head; /* TLV header */
865 uint16_t idx; /* Name index */
866 uint8_t set; /* set, if applicable */
867 uint8_t type; /* object type, if applicable */
868 uint32_t spare; /* unused */
869 char name[64]; /* Null-terminated name */
870 } ipfw_obj_ntlv;
871
872 /* IPv4/IPv6 L4 flow description */
873 struct tflow_entry {
874 uint8_t af;
875 uint8_t proto;
876 uint16_t spare;
877 uint16_t sport;
878 uint16_t dport;
879 union {
880 struct {
881 struct in_addr sip;
882 struct in_addr dip;
883 } a4;
884 struct {
885 struct in6_addr sip6;
886 struct in6_addr dip6;
887 } a6;
888 } a;
889 };
890
891 typedef struct _ipfw_table_value {
892 uint32_t tag; /* O_TAG/O_TAGGED */
893 uint32_t pipe; /* O_PIPE/O_QUEUE */
894 uint16_t divert; /* O_DIVERT/O_TEE */
895 uint16_t skipto; /* skipto, CALLRET */
896 uint32_t netgraph; /* O_NETGRAPH/O_NGTEE */
897 uint32_t fib; /* O_SETFIB */
898 uint32_t nat; /* O_NAT */
899 uint32_t nh4;
900 uint8_t dscp;
901 uint8_t spare0;
902 uint16_t spare1;
903 struct in6_addr nh6;
904 uint32_t limit; /* O_LIMIT */
905 uint32_t zoneid; /* scope zone id for nh6 */
906 uint64_t reserved;
907 } ipfw_table_value;
908
909 /* Table entry TLV */
910 typedef struct _ipfw_obj_tentry {
911 ipfw_obj_tlv head; /* TLV header */
912 uint8_t subtype; /* subtype (IPv4,IPv6) */
913 uint8_t masklen; /* mask length */
914 uint8_t result; /* request result */
915 uint8_t spare0;
916 uint16_t idx; /* Table name index */
917 uint16_t spare1;
918 union {
919 /* Longest field needs to be aligned by 8-byte boundary */
920 struct in_addr addr; /* IPv4 address */
921 uint32_t key; /* uid/gid/port */
922 struct in6_addr addr6; /* IPv6 address */
923 char iface[IF_NAMESIZE]; /* interface name */
924 u_char mac[IPFW_MAX_L2_ADDR_LEN]; /* MAC address */
925 struct tflow_entry flow;
926 } k;
927 union {
928 ipfw_table_value value; /* value data */
929 uint32_t kidx; /* value kernel index */
930 } v;
931 } ipfw_obj_tentry;
932 #define IPFW_TF_UPDATE 0x01 /* Update record if exists */
933 /* Container TLV */
934 #define IPFW_CTF_ATOMIC 0x01 /* Perform atomic operation */
935 /* Operation results */
936 #define IPFW_TR_IGNORED 0 /* Entry was ignored (rollback) */
937 #define IPFW_TR_ADDED 1 /* Entry was successfully added */
938 #define IPFW_TR_UPDATED 2 /* Entry was successfully updated*/
939 #define IPFW_TR_DELETED 3 /* Entry was successfully deleted*/
940 #define IPFW_TR_LIMIT 4 /* Entry was ignored (limit) */
941 #define IPFW_TR_NOTFOUND 5 /* Entry was not found */
942 #define IPFW_TR_EXISTS 6 /* Entry already exists */
943 #define IPFW_TR_ERROR 7 /* Request has failed (unknown) */
944
945 typedef struct _ipfw_obj_dyntlv {
946 ipfw_obj_tlv head;
947 ipfw_dyn_rule state;
948 } ipfw_obj_dyntlv;
949 #define IPFW_DF_LAST 0x01 /* Last state in chain */
950
951 /* Containter TLVs */
952 typedef struct _ipfw_obj_ctlv {
953 ipfw_obj_tlv head; /* TLV header */
954 uint32_t count; /* Number of sub-TLVs */
955 uint16_t objsize; /* Single object size */
956 uint8_t version; /* TLV version */
957 uint8_t flags; /* TLV-specific flags */
958 } ipfw_obj_ctlv;
959
960 /* Range TLV */
961 typedef struct _ipfw_range_tlv {
962 ipfw_obj_tlv head; /* TLV header */
963 uint32_t flags; /* Range flags */
964 uint16_t start_rule; /* Range start */
965 uint16_t end_rule; /* Range end */
966 uint32_t set; /* Range set to match */
967 uint32_t new_set; /* New set to move/swap to */
968 } ipfw_range_tlv;
969 #define IPFW_RCFLAG_RANGE 0x01 /* rule range is set */
970 #define IPFW_RCFLAG_ALL 0x02 /* match ALL rules */
971 #define IPFW_RCFLAG_SET 0x04 /* match rules in given set */
972 #define IPFW_RCFLAG_DYNAMIC 0x08 /* match only dynamic states */
973 /* User-settable flags */
974 #define IPFW_RCFLAG_USER (IPFW_RCFLAG_RANGE | IPFW_RCFLAG_ALL | \
975 IPFW_RCFLAG_SET | IPFW_RCFLAG_DYNAMIC)
976 /* Internally used flags */
977 #define IPFW_RCFLAG_DEFAULT 0x0100 /* Do not skip defaul rule */
978
979 typedef struct _ipfw_ta_tinfo {
980 uint32_t flags; /* Format flags */
981 uint32_t spare;
982 uint8_t taclass4; /* algorithm class */
983 uint8_t spare4;
984 uint16_t itemsize4; /* item size in runtime */
985 uint32_t size4; /* runtime structure size */
986 uint32_t count4; /* number of items in runtime */
987 uint8_t taclass6; /* algorithm class */
988 uint8_t spare6;
989 uint16_t itemsize6; /* item size in runtime */
990 uint32_t size6; /* runtime structure size */
991 uint32_t count6; /* number of items in runtime */
992 } ipfw_ta_tinfo;
993 #define IPFW_TACLASS_HASH 1 /* algo is based on hash */
994 #define IPFW_TACLASS_ARRAY 2 /* algo is based on array */
995 #define IPFW_TACLASS_RADIX 3 /* algo is based on radix tree */
996
997 #define IPFW_TATFLAGS_DATA 0x0001 /* Has data filled in */
998 #define IPFW_TATFLAGS_AFDATA 0x0002 /* Separate data per AF */
999 #define IPFW_TATFLAGS_AFITEM 0x0004 /* diff. items per AF */
1000
1001 typedef struct _ipfw_xtable_info {
1002 uint8_t type; /* table type (addr,iface,..) */
1003 uint8_t tflags; /* type flags */
1004 uint16_t mflags; /* modification flags */
1005 uint16_t flags; /* generic table flags */
1006 uint16_t spare[3];
1007 uint32_t vmask; /* bitmask with value types */
1008 uint32_t set; /* set table is in */
1009 uint32_t kidx; /* kernel index */
1010 uint32_t refcnt; /* number of references */
1011 uint32_t count; /* Number of records */
1012 uint32_t size; /* Total size of records(export)*/
1013 uint32_t limit; /* Max number of records */
1014 char tablename[64]; /* table name */
1015 char algoname[64]; /* algorithm name */
1016 ipfw_ta_tinfo ta_info; /* additional algo stats */
1017 } ipfw_xtable_info;
1018 /* Generic table flags */
1019 #define IPFW_TGFLAGS_LOCKED 0x01 /* Tables is locked from changes*/
1020 /* Table type-specific flags */
1021 #define IPFW_TFFLAG_SRCIP 0x01
1022 #define IPFW_TFFLAG_DSTIP 0x02
1023 #define IPFW_TFFLAG_SRCPORT 0x04
1024 #define IPFW_TFFLAG_DSTPORT 0x08
1025 #define IPFW_TFFLAG_PROTO 0x10
1026 /* Table modification flags */
1027 #define IPFW_TMFLAGS_LIMIT 0x0002 /* Change limit value */
1028 #define IPFW_TMFLAGS_LOCK 0x0004 /* Change table lock state */
1029
1030 typedef struct _ipfw_iface_info {
1031 char ifname[64]; /* interface name */
1032 uint32_t ifindex; /* interface index */
1033 uint32_t flags; /* flags */
1034 uint32_t refcnt; /* number of references */
1035 uint32_t gencnt; /* number of changes */
1036 uint64_t spare;
1037 } ipfw_iface_info;
1038 #define IPFW_IFFLAG_RESOLVED 0x01 /* Interface exists */
1039
1040 typedef struct _ipfw_ta_info {
1041 char algoname[64]; /* algorithm name */
1042 uint32_t type; /* lookup type */
1043 uint32_t flags;
1044 uint32_t refcnt;
1045 uint32_t spare0;
1046 uint64_t spare1;
1047 } ipfw_ta_info;
1048
1049 typedef struct _ipfw_obj_header {
1050 ip_fw3_opheader opheader; /* IP_FW3 opcode */
1051 uint32_t spare;
1052 uint16_t idx; /* object name index */
1053 uint8_t objtype; /* object type */
1054 uint8_t objsubtype; /* object subtype */
1055 ipfw_obj_ntlv ntlv; /* object name tlv */
1056 } ipfw_obj_header;
1057
1058 typedef struct _ipfw_obj_lheader {
1059 ip_fw3_opheader opheader; /* IP_FW3 opcode */
1060 uint32_t set_mask; /* disabled set mask */
1061 uint32_t count; /* Total objects count */
1062 uint32_t size; /* Total size (incl. header) */
1063 uint32_t objsize; /* Size of one object */
1064 } ipfw_obj_lheader;
1065
1066 #define IPFW_CFG_GET_STATIC 0x01
1067 #define IPFW_CFG_GET_STATES 0x02
1068 #define IPFW_CFG_GET_COUNTERS 0x04
1069 typedef struct _ipfw_cfg_lheader {
1070 ip_fw3_opheader opheader; /* IP_FW3 opcode */
1071 uint32_t set_mask; /* enabled set mask */
1072 uint32_t spare;
1073 uint32_t flags; /* Request flags */
1074 uint32_t size; /* neded buffer size */
1075 uint32_t start_rule;
1076 uint32_t end_rule;
1077 } ipfw_cfg_lheader;
1078
1079 typedef struct _ipfw_range_header {
1080 ip_fw3_opheader opheader; /* IP_FW3 opcode */
1081 ipfw_range_tlv range;
1082 } ipfw_range_header;
1083
1084 typedef struct _ipfw_sopt_info {
1085 uint16_t opcode;
1086 uint8_t version;
1087 uint8_t dir;
1088 uint8_t spare;
1089 uint64_t refcnt;
1090 } ipfw_sopt_info;
1091
1092 #endif /* _IPFW2_H */
Cache object: e960104cbff3c8eb2f4b07c2de4c15c8
|