2 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (c) 2002 Luigi Rizzo, Universita` di Pisa
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
41 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * $FreeBSD: src/sys/netinet/ip_fw2.h,v 1.1.2.4 2003/07/17 06:03:39 luigi Exp $
58 #ifdef __APPLE_API_OBSOLETE
61 * Define IP Firewall event subclass, and associated events.
65 @defined KEV_IPFW_SUBCLASS
66 @discussion The kernel event subclass for IP Firewall.
68 #define KEV_IPFW_SUBCLASS 1
72 @discussion The event code indicating a rule has been added.
74 #define KEV_IPFW_ADD 1
78 @discussion The event code indicating a rule has been removed.
80 #define KEV_IPFW_DEL 2
83 @defined KEV_IPFW_FLUSH
84 @discussion The event code indicating the rule set has been flushed.
86 #define KEV_IPFW_FLUSH 3
89 @defined KEV_IPFW_ENABLE
90 @discussion The event code indicating the enable flag has been changed
92 #define KEV_IPFW_ENABLE 4
97 * The kernel representation of ipfw rules is made of a list of
98 * 'instructions' (for all practical purposes equivalent to BPF
99 * instructions), which specify which fields of the packet
100 * (or its metadata) should be analysed.
102 * Each instruction is stored in a structure which begins with
103 * "ipfw_insn", and can contain extra fields depending on the
104 * instruction type (listed below).
105 * Note that the code is written so that individual instructions
106 * have a size which is a multiple of 32 bits. This means that, if
107 * such structures contain pointers or other 64-bit entities,
108 * (there is just one instance now) they may end up unaligned on
109 * 64-bit architectures, so the must be handled with care.
111 * "enum ipfw_opcodes" are the opcodes supported. We can have up
112 * to 256 different opcodes.
115 enum ipfw_opcodes
{ /* arguments (4 byte each) */
118 O_IP_SRC
, /* u32 = IP */
119 O_IP_SRC_MASK
, /* ip = IP/mask */
120 O_IP_SRC_ME
, /* none */
121 O_IP_SRC_SET
, /* u32=base, arg1=len, bitmap */
123 O_IP_DST
, /* u32 = IP */
124 O_IP_DST_MASK
, /* ip = IP/mask */
125 O_IP_DST_ME
, /* none */
126 O_IP_DST_SET
, /* u32=base, arg1=len, bitmap */
128 O_IP_SRCPORT
, /* (n)port list:mask 4 byte ea */
129 O_IP_DSTPORT
, /* (n)port list:mask 4 byte ea */
130 O_PROTO
, /* arg1=protocol */
132 O_MACADDR2
, /* 2 mac addr:mask */
133 O_MAC_TYPE
, /* same as srcport */
143 O_IPOPT
, /* arg1 = 2*u8 bitmap */
144 O_IPLEN
, /* arg1 = len */
145 O_IPID
, /* arg1 = id */
147 O_IPTOS
, /* arg1 = id */
148 O_IPPRECEDENCE
, /* arg1 = precedence << 5 */
149 O_IPTTL
, /* arg1 = TTL */
151 O_IPVER
, /* arg1 = version */
152 O_UID
, /* u32 = id */
153 O_GID
, /* u32 = id */
154 O_ESTAB
, /* none (tcp established) */
155 O_TCPFLAGS
, /* arg1 = 2*u8 bitmap */
156 O_TCPWIN
, /* arg1 = desired win */
157 O_TCPSEQ
, /* u32 = desired seq. */
158 O_TCPACK
, /* u32 = desired seq. */
159 O_ICMPTYPE
, /* u32 = icmp bitmap */
160 O_TCPOPTS
, /* arg1 = 2*u8 bitmap */
162 O_VERREVPATH
, /* none */
164 O_PROBE_STATE
, /* none */
165 O_KEEP_STATE
, /* none */
166 O_LIMIT
, /* ipfw_insn_limit */
167 O_LIMIT_PARENT
, /* dyn_type, not an opcode. */
170 * These are really 'actions'.
173 O_LOG
, /* ipfw_insn_log */
174 O_PROB
, /* u32 = match probability */
176 O_CHECK_STATE
, /* none */
179 O_REJECT
, /* arg1=icmp arg (same as deny) */
181 O_SKIPTO
, /* arg1=next rule number */
182 O_PIPE
, /* arg1=pipe number */
183 O_QUEUE
, /* arg1=queue number */
184 O_DIVERT
, /* arg1=port number */
185 O_TEE
, /* arg1=port number */
186 O_FORWARD_IP
, /* fwd sockaddr */
187 O_FORWARD_MAC
, /* fwd mac */
192 O_IPSEC
, /* has ipsec history */
194 O_LAST_OPCODE
/* not an opcode! */
198 * Template for instructions.
200 * ipfw_insn is used for all instructions which require no operands,
201 * a single 16-bit value (arg1), or a couple of 8-bit values.
203 * For other instructions which require different/larger arguments
204 * we have derived structures, ipfw_insn_*.
206 * The size of the instruction (in 32-bit words) is in the low
207 * 6 bits of "len". The 2 remaining bits are used to implement
208 * NOT and OR on individual instructions. Given a type, you can
209 * compute the length to be put in "len" using F_INSN_SIZE(t)
211 * F_NOT negates the match result of the instruction.
213 * F_OR is used to build or blocks. By default, instructions
214 * are evaluated as part of a logical AND. An "or" block
215 * { X or Y or Z } contains F_OR set in all but the last
216 * instruction of the block. A match will cause the code
217 * to skip past the last instruction of the block.
219 * NOTA BENE: in a couple of places we assume that
220 * sizeof(ipfw_insn) == sizeof(u_int32_t)
221 * this needs to be fixed.
224 typedef struct _ipfw_insn
{ /* template for instructions */
225 enum ipfw_opcodes opcode
:8;
226 u_int8_t len
; /* numer of 32-byte words */
229 #define F_LEN_MASK 0x3f
230 #define F_LEN(cmd) ((cmd)->len & F_LEN_MASK)
236 * The F_INSN_SIZE(type) computes the size, in 4-byte words, of
239 #define F_INSN_SIZE(t) ((sizeof (t))/sizeof(u_int32_t))
242 * This is used to store an array of 16-bit entries (ports etc.)
244 typedef struct _ipfw_insn_u16
{
246 u_int16_t ports
[2]; /* there may be more */
250 * This is used to store an array of 32-bit entries
251 * (uid, single IPv4 addresses etc.)
253 typedef struct _ipfw_insn_u32
{
255 u_int32_t d
[1]; /* one or more */
259 * This is used to store IP addr-mask pairs.
261 typedef struct _ipfw_insn_ip
{
268 * This is used to forward to a given address (ip).
270 typedef struct _ipfw_insn_sa
{
272 struct sockaddr_in sa
;
276 * This is used for MAC addr-mask pairs.
278 typedef struct _ipfw_insn_mac
{
280 u_char addr
[12]; /* dst[6] + src[6] */
281 u_char mask
[12]; /* dst[6] + src[6] */
285 * This is used for interface match rules (recv xx, xmit xx).
287 typedef struct _ipfw_insn_if
{
297 * This is used for pipe and queue actions, which need to store
298 * a single pointer (which can have different size on different
300 * Note that, because of previous instructions, pipe_ptr might
301 * be unaligned in the overall structure, so it needs to be
302 * manipulated with care.
304 typedef struct _ipfw_insn_pipe
{
306 void *pipe_ptr
; /* XXX */
310 * This is used for limit rules.
312 typedef struct _ipfw_insn_limit
{
315 u_int8_t limit_mask
; /* combination of DYN_* below */
316 #define DYN_SRC_ADDR 0x1
317 #define DYN_SRC_PORT 0x2
318 #define DYN_DST_ADDR 0x4
319 #define DYN_DST_PORT 0x8
321 u_int16_t conn_limit
;
325 * This is used for log instructions.
327 typedef struct _ipfw_insn_log
{
329 u_int32_t max_log
; /* how many do we log -- 0 = all */
330 u_int32_t log_left
; /* how many left to log */
333 /* Version of this API */
334 #define IP_FW_VERSION_NONE 0
335 #define IP_FW_VERSION_0 10 /* old ipfw */
336 #define IP_FW_VERSION_1 20 /* ipfw in Jaguar/Panther */
337 #define IP_FW_VERSION_2 30 /* ipfw2 */
338 #define IP_FW_CURRENT_API_VERSION IP_FW_VERSION_2
341 * Here we have the structure representing an ipfw rule.
343 * It starts with a general area (with link fields and counters)
344 * followed by an array of one or more instructions, which the code
345 * accesses as an array of 32-bit values.
347 * Given a rule pointer r:
349 * r->cmd is the start of the first instruction.
350 * ACTION_PTR(r) is the start of the first action (things to do
351 * once a rule matched).
353 * When assembling instruction, remember the following:
355 * + if a rule has a "keep-state" (or "limit") option, then the
356 * first instruction (at r->cmd) MUST BE an O_PROBE_STATE
357 * + if a rule has a "log" option, then the first action
358 * (at ACTION_PTR(r)) MUST be O_LOG
360 * NOTE: we use a simple linked list of rules because we never need
361 * to delete a rule without scanning the list. We do not use
362 * queue(3) macros for portability and readability.
366 u_int32_t version
; /* Version of this structure. MUST be set */
367 /* by clients. Should always be */
368 /* set to IP_FW_CURRENT_API_VERSION. */
369 void *context
; /* Context that is usable by user processes to */
370 /* identify this rule. */
371 struct ip_fw
*next
; /* linked list of rules */
372 struct ip_fw
*next_rule
; /* ptr to next [skipto] rule */
373 /* 'next_rule' is used to pass up 'set_disable' status */
375 u_int16_t act_ofs
; /* offset of action in 32-bit units */
376 u_int16_t cmd_len
; /* # of 32-bit words in cmd */
377 u_int16_t rulenum
; /* rule number */
378 u_int8_t set
; /* rule set (0..31) */
379 u_int32_t set_masks
[2]; /* masks for manipulating sets atomically */
380 #define RESVD_SET 31 /* set for default and persistent rules */
381 u_int8_t _pad
; /* padding */
383 /* These fields are present in all rules. */
384 u_int64_t pcnt
; /* Packet counter */
385 u_int64_t bcnt
; /* Byte counter */
386 u_int32_t timestamp
; /* tv_sec of last match */
388 u_int32_t reserved_1
; /* reserved - set to 0 */
389 u_int32_t reserved_2
; /* reserved - set to 0 */
391 ipfw_insn cmd
[1]; /* storage for commands */
394 #define ACTION_PTR(rule) \
395 (ipfw_insn *)( (u_int32_t *)((rule)->cmd) + ((rule)->act_ofs) )
397 #define RULESIZE(rule) (sizeof(struct ip_fw) + \
398 ((struct ip_fw *)(rule))->cmd_len * 4 - 4)
401 * This structure is used as a flow mask and a flow id for various
404 struct ipfw_flow_id
{
410 u_int8_t flags
; /* protocol-specific flags */
416 typedef struct _ipfw_dyn_rule ipfw_dyn_rule
;
418 struct _ipfw_dyn_rule
{
419 ipfw_dyn_rule
*next
; /* linked list of rules. */
420 struct ip_fw
*rule
; /* pointer to rule */
421 /* 'rule' is used to pass up the rule number (from the parent) */
423 ipfw_dyn_rule
*parent
; /* pointer to parent rule */
424 u_int64_t pcnt
; /* packet match counter */
425 u_int64_t bcnt
; /* byte match counter */
426 struct ipfw_flow_id id
; /* (masked) flow id */
427 u_int32_t expire
; /* expire time */
428 u_int32_t bucket
; /* which bucket in hash table */
429 u_int32_t state
; /* state of this rule (typically a
430 * combination of TCP flags)
432 u_int32_t ack_fwd
; /* most recent ACKs in forward */
433 u_int32_t ack_rev
; /* and reverse directions (used */
434 /* to generate keepalives) */
435 u_int16_t dyn_type
; /* rule type */
436 u_int16_t count
; /* refcount */
440 * Definitions for IP option names.
442 #define IP_FW_IPOPT_LSRR 0x01
443 #define IP_FW_IPOPT_SSRR 0x02
444 #define IP_FW_IPOPT_RR 0x04
445 #define IP_FW_IPOPT_TS 0x08
448 * Definitions for TCP option names.
450 #define IP_FW_TCPOPT_MSS 0x01
451 #define IP_FW_TCPOPT_WINDOW 0x02
452 #define IP_FW_TCPOPT_SACK 0x04
453 #define IP_FW_TCPOPT_TS 0x08
454 #define IP_FW_TCPOPT_CC 0x10
456 #define ICMP_REJECT_RST 0x100 /* fake ICMP code (send a TCP RST) */
459 * Main firewall chains definitions and global var's definitions.
465 u_int32_t version
; /* Version of this structure. MUST be set */
466 /* by clients. Should always be */
467 /* set to IP_FW_CURRENT_API_VERSION. */
468 user32_addr_t context
; /* Context that is usable by user processes to */
469 /* identify this rule. */
470 user32_addr_t next
; /* linked list of rules */
471 user32_addr_t next_rule
;/* ptr to next [skipto] rule */
472 /* 'next_rule' is used to pass up 'set_disable' status */
474 u_int16_t act_ofs
; /* offset of action in 32-bit units */
475 u_int16_t cmd_len
; /* # of 32-bit words in cmd */
476 u_int16_t rulenum
; /* rule number */
477 u_int8_t set
; /* rule set (0..31) */
478 u_int32_t set_masks
[2]; /* masks for manipulating sets atomically */
479 #define RESVD_SET 31 /* set for default and persistent rules */
480 u_int8_t _pad
; /* padding */
482 /* These fields are present in all rules. */
483 u_int64_t pcnt
; /* Packet counter */
484 u_int64_t bcnt
; /* Byte counter */
485 u_int32_t timestamp
; /* tv_sec of last match */
487 u_int32_t reserved_1
; /* reserved - set to 0 */
488 u_int32_t reserved_2
; /* reserved - set to 0 */
490 ipfw_insn cmd
[1]; /* storage for commands */
496 u_int32_t version
; /* Version of this structure. MUST be set */
497 /* by clients. Should always be */
498 /* set to IP_FW_CURRENT_API_VERSION. */
499 __uint64_t context
__attribute__((aligned(8))); /* Context that is usable by user processes to */
500 /* identify this rule. */
501 user64_addr_t next
; /* linked list of rules */
502 user64_addr_t next_rule
; /* ptr to next [skipto] rule */
503 /* 'next_rule' is used to pass up 'set_disable' status */
505 u_int16_t act_ofs
; /* offset of action in 32-bit units */
506 u_int16_t cmd_len
; /* # of 32-bit words in cmd */
507 u_int16_t rulenum
; /* rule number */
508 u_int8_t set
; /* rule set (0..31) */
509 u_int32_t set_masks
[2]; /* masks for manipulating sets atomically */
510 #define RESVD_SET 31 /* set for default and persistent rules */
511 u_int8_t _pad
; /* padding */
513 /* These fields are present in all rules. */
514 u_int64_t pcnt
__attribute__((aligned(8))); /* Packet counter */
515 u_int64_t bcnt
__attribute__((aligned(8))); /* Byte counter */
516 u_int32_t timestamp
; /* tv_sec of last match */
518 u_int32_t reserved_1
; /* reserved - set to 0 */
519 u_int32_t reserved_2
; /* reserved - set to 0 */
521 ipfw_insn cmd
[1]; /* storage for commands */
525 typedef struct _ipfw_dyn_rule_64 ipfw_dyn_rule_64
;
526 typedef struct _ipfw_dyn_rule_32 ipfw_dyn_rule_32
;
529 struct _ipfw_dyn_rule_32
{
530 user32_addr_t next
; /* linked list of rules. */
531 user32_addr_t rule
; /* pointer to rule */
532 /* 'rule' is used to pass up the rule number (from the parent) */
534 user32_addr_t parent
; /* pointer to parent rule */
535 u_int64_t pcnt
; /* packet match counter */
536 u_int64_t bcnt
; /* byte match counter */
537 struct ipfw_flow_id id
; /* (masked) flow id */
538 u_int32_t expire
; /* expire time */
539 u_int32_t bucket
; /* which bucket in hash table */
540 u_int32_t state
; /* state of this rule (typically a
541 * combination of TCP flags)
543 u_int32_t ack_fwd
; /* most recent ACKs in forward */
544 u_int32_t ack_rev
; /* and reverse directions (used */
545 /* to generate keepalives) */
546 u_int16_t dyn_type
; /* rule type */
547 u_int16_t count
; /* refcount */
552 struct _ipfw_dyn_rule_64
{
553 user64_addr_t next
; /* linked list of rules. */
554 user64_addr_t rule
; /* pointer to rule */
555 /* 'rule' is used to pass up the rule number (from the parent) */
557 user64_addr_t parent
; /* pointer to parent rule */
558 u_int64_t pcnt
; /* packet match counter */
559 u_int64_t bcnt
; /* byte match counter */
560 struct ipfw_flow_id id
; /* (masked) flow id */
561 u_int32_t expire
; /* expire time */
562 u_int32_t bucket
; /* which bucket in hash table */
563 u_int32_t state
; /* state of this rule (typically a
564 * combination of TCP flags)
566 u_int32_t ack_fwd
; /* most recent ACKs in forward */
567 u_int32_t ack_rev
; /* and reverse directions (used */
568 /* to generate keepalives) */
569 u_int16_t dyn_type
; /* rule type */
570 u_int16_t count
; /* refcount */
574 typedef struct _ipfw_insn_pipe_64
{
576 user64_addr_t pipe_ptr
; /* XXX */
579 typedef struct _ipfw_insn_pipe_32
{
581 user32_addr_t pipe_ptr
; /* XXX */
590 #define IP_FW_PORT_DYNT_FLAG 0x10000
591 #define IP_FW_PORT_TEE_FLAG 0x20000
592 #define IP_FW_PORT_DENY_FLAG 0x40000
595 * Arguments for calling ipfw_chk() and dummynet_io(). We put them
596 * all into a structure because this way it is easier and more
597 * efficient to pass variables around and extend the interface.
600 struct mbuf
*m
; /* the mbuf chain */
601 struct ifnet
*oif
; /* output interface */
602 struct sockaddr_in
*next_hop
; /* forward address */
603 struct ip_fw
*rule
; /* matching rule */
604 struct ether_header
*eh
; /* for bridged packets */
606 struct route
*ro
; /* for dummynet */
607 struct sockaddr_in
*dst
; /* for dummynet */
608 int flags
; /* for dummynet */
609 struct ip_out_args
*ipoa
; /* for dummynet */
611 struct ipfw_flow_id f_id
; /* grabbed from IP header */
612 u_int16_t divert_rule
; /* divert cookie */
618 * Function definitions.
625 void flush_pipe_ptrs(struct dn_flow_set
*match
); /* used by dummynet */
626 void ipfw_init(void); /* called from raw_ip.c: load_ipfw() */
628 typedef int ip_fw_chk_t (struct ip_fw_args
*args
);
629 typedef int ip_fw_ctl_t (struct sockopt
*);
630 extern ip_fw_chk_t
*ip_fw_chk_ptr
;
631 extern ip_fw_ctl_t
*ip_fw_ctl_ptr
;
632 extern int fw_one_pass
;
633 extern int fw_enable
;
634 #define IPFW_LOADED (ip_fw_chk_ptr != NULL)
635 #endif /* IPFIREWALL */
638 #endif /* __APPLE_API_OBSOLETE */
639 #endif /* _IPFW2_H */