2 * Copyright (c) 2002 Luigi Rizzo, Universita` di Pisa
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
25 * $FreeBSD: src/sys/netinet/ip_fw2.h,v 1.1.2.4 2003/07/17 06:03:39 luigi Exp $
32 * Define IP Firewall event subclass, and associated events.
36 @defined KEV_IPFW_SUBCLASS
37 @discussion The kernel event subclass for IP Firewall.
39 #define KEV_IPFW_SUBCLASS 1
43 @discussion The event code indicating a rule has been added.
45 #define KEV_IPFW_ADD 1
49 @discussion The event code indicating a rule has been removed.
51 #define KEV_IPFW_DEL 2
54 @defined KEV_IPFW_FLUSH
55 @discussion The event code indicating the rule set has been flushed.
57 #define KEV_IPFW_FLUSH 3
60 @defined KEV_IPFW_ENABLE
61 @discussion The event code indicating the enable flag has been changed
63 #define KEV_IPFW_ENABLE 4
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.
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.
84 * "enum ipfw_opcodes" are the opcodes supported. We can have up
85 * to 256 different opcodes.
88 enum ipfw_opcodes
{ /* arguments (4 byte each) */
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 */
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 */
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 */
105 O_MACADDR2
, /* 2 mac addr:mask */
106 O_MAC_TYPE
, /* same as srcport */
116 O_IPOPT
, /* arg1 = 2*u8 bitmap */
117 O_IPLEN
, /* arg1 = len */
118 O_IPID
, /* arg1 = id */
120 O_IPTOS
, /* arg1 = id */
121 O_IPPRECEDENCE
, /* arg1 = precedence << 5 */
122 O_IPTTL
, /* arg1 = TTL */
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 */
135 O_VERREVPATH
, /* none */
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. */
143 * These are really 'actions'.
146 O_LOG
, /* ipfw_insn_log */
147 O_PROB
, /* u32 = match probability */
149 O_CHECK_STATE
, /* none */
152 O_REJECT
, /* arg1=icmp arg (same as deny) */
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 */
165 O_IPSEC
, /* has ipsec history */
167 O_LAST_OPCODE
/* not an opcode! */
171 * Template for instructions.
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.
176 * For other instructions which require different/larger arguments
177 * we have derived structures, ipfw_insn_*.
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)
184 * F_NOT negates the match result of the instruction.
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.
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.
197 typedef struct _ipfw_insn
{ /* template for instructions */
198 enum ipfw_opcodes opcode
:8;
199 u_int8_t len
; /* numer of 32-byte words */
202 #define F_LEN_MASK 0x3f
203 #define F_LEN(cmd) ((cmd)->len & F_LEN_MASK)
209 * The F_INSN_SIZE(type) computes the size, in 4-byte words, of
212 #define F_INSN_SIZE(t) ((sizeof (t))/sizeof(u_int32_t))
215 * This is used to store an array of 16-bit entries (ports etc.)
217 typedef struct _ipfw_insn_u16
{
219 u_int16_t ports
[2]; /* there may be more */
223 * This is used to store an array of 32-bit entries
224 * (uid, single IPv4 addresses etc.)
226 typedef struct _ipfw_insn_u32
{
228 u_int32_t d
[1]; /* one or more */
232 * This is used to store IP addr-mask pairs.
234 typedef struct _ipfw_insn_ip
{
241 * This is used to forward to a given address (ip).
243 typedef struct _ipfw_insn_sa
{
245 struct sockaddr_in sa
;
249 * This is used for MAC addr-mask pairs.
251 typedef struct _ipfw_insn_mac
{
253 u_char addr
[12]; /* dst[6] + src[6] */
254 u_char mask
[12]; /* dst[6] + src[6] */
258 * This is used for interface match rules (recv xx, xmit xx).
260 typedef struct _ipfw_insn_if
{
270 * This is used for pipe and queue actions, which need to store
271 * a single pointer (which can have different size on different
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.
277 typedef struct _ipfw_insn_pipe
{
279 void *pipe_ptr
; /* XXX */
283 * This is used for limit rules.
285 typedef struct _ipfw_insn_limit
{
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
294 u_int16_t conn_limit
;
298 * This is used for log instructions.
300 typedef struct _ipfw_insn_log
{
302 u_int32_t max_log
; /* how many do we log -- 0 = all */
303 u_int32_t log_left
; /* how many left to log */
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
314 * Here we have the structure representing an ipfw rule.
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.
320 * Given a rule pointer r:
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).
326 * When assembling instruction, remember the following:
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
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.
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 */
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 */
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 */
361 u_int32_t reserved_1
; /* reserved - set to 0 */
362 u_int32_t reserved_2
; /* reserved - set to 0 */
364 ipfw_insn cmd
[1]; /* storage for commands */
367 #define ACTION_PTR(rule) \
368 (ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) )
370 #define RULESIZE(rule) (sizeof(struct ip_fw) + \
371 ((struct ip_fw *)(rule))->cmd_len * 4 - 4)
374 * This structure is used as a flow mask and a flow id for various
377 struct ipfw_flow_id
{
383 u_int8_t flags
; /* protocol-specific flags */
389 typedef struct _ipfw_dyn_rule ipfw_dyn_rule
;
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) */
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)
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 */
413 * Definitions for IP option names.
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
421 * Definitions for TCP option names.
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
429 #define ICMP_REJECT_RST 0x100 /* fake ICMP code (send a TCP RST) */
432 * Main firewall chains definitions and global var's definitions.
436 #define IP_FW_PORT_DYNT_FLAG 0x10000
437 #define IP_FW_PORT_TEE_FLAG 0x20000
438 #define IP_FW_PORT_DENY_FLAG 0x40000
441 * Arguments for calling ipfw_chk() and dummynet_io(). We put them
442 * all into a structure because this way it is easier and more
443 * efficient to pass variables around and extend the interface.
446 struct mbuf
*m
; /* the mbuf chain */
447 struct ifnet
*oif
; /* output interface */
448 struct sockaddr_in
*next_hop
; /* forward address */
449 struct ip_fw
*rule
; /* matching rule */
450 struct ether_header
*eh
; /* for bridged packets */
452 struct route
*ro
; /* for dummynet */
453 struct sockaddr_in
*dst
; /* for dummynet */
454 int flags
; /* for dummynet */
456 struct ipfw_flow_id f_id
; /* grabbed from IP header */
457 u_int16_t divert_rule
; /* divert cookie */
462 * Function definitions.
469 void flush_pipe_ptrs(struct dn_flow_set
*match
); /* used by dummynet */
470 void ipfw_init(void); /* called from raw_ip.c: load_ipfw() */
472 typedef int ip_fw_chk_t (struct ip_fw_args
*args
);
473 typedef int ip_fw_ctl_t (struct sockopt
*);
474 extern ip_fw_chk_t
*ip_fw_chk_ptr
;
475 extern ip_fw_ctl_t
*ip_fw_ctl_ptr
;
476 extern int fw_one_pass
;
477 extern int fw_enable
;
478 #define IPFW_LOADED (ip_fw_chk_ptr != NULL)
481 #endif /* !__LP64__ */
482 #endif /* _IPFW2_H */