2 * Copyright (c) 2008-2013 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 #ifdef XNU_KERNEL_PRIVATE
420 #include <netinet/ip_flowid.h>
424 * The internal version of "struct _ipfw_dyn_rule" differs from
425 * its external version because the field "id" is of type
426 * "struct ip_flow_id" in the internal version. The type of the
427 * field "id" for the external version is "ipfw_dyn_rule for
428 * backwards compatibility reasons.
431 struct _ipfw_dyn_rule
{
432 ipfw_dyn_rule
*next
; /* linked list of rules. */
433 struct ip_fw
*rule
; /* pointer to rule */
434 /* 'rule' is used to pass up the rule number (from the parent) */
436 ipfw_dyn_rule
*parent
; /* pointer to parent rule */
437 u_int64_t pcnt
; /* packet match counter */
438 u_int64_t bcnt
; /* byte match counter */
439 struct ip_flow_id id
; /* (masked) flow id */
440 u_int32_t expire
; /* expire time */
441 u_int32_t bucket
; /* which bucket in hash table */
442 u_int32_t state
; /* state of this rule (typically a
443 * combination of TCP flags)
445 u_int32_t ack_fwd
; /* most recent ACKs in forward */
446 u_int32_t ack_rev
; /* and reverse directions (used */
447 /* to generate keepalives) */
448 u_int16_t dyn_type
; /* rule type */
449 u_int16_t count
; /* refcount */
451 #else /* XNU_KERNEL_PRIVATE */
452 struct _ipfw_dyn_rule
{
453 ipfw_dyn_rule
*next
; /* linked list of rules. */
454 struct ip_fw
*rule
; /* pointer to rule */
455 /* 'rule' is used to pass up the rule number (from the parent) */
457 ipfw_dyn_rule
*parent
; /* pointer to parent rule */
458 u_int64_t pcnt
; /* packet match counter */
459 u_int64_t bcnt
; /* byte match counter */
460 struct ipfw_flow_id id
; /* (masked) flow id */
461 u_int32_t expire
; /* expire time */
462 u_int32_t bucket
; /* which bucket in hash table */
463 u_int32_t state
; /* state of this rule (typically a
464 * combination of TCP flags)
466 u_int32_t ack_fwd
; /* most recent ACKs in forward */
467 u_int32_t ack_rev
; /* and reverse directions (used */
468 /* to generate keepalives) */
469 u_int16_t dyn_type
; /* rule type */
470 u_int16_t count
; /* refcount */
472 #endif /* XNU_KERNEL_PRIVATE */
475 * Definitions for IP option names.
477 #define IP_FW_IPOPT_LSRR 0x01
478 #define IP_FW_IPOPT_SSRR 0x02
479 #define IP_FW_IPOPT_RR 0x04
480 #define IP_FW_IPOPT_TS 0x08
483 * Definitions for TCP option names.
485 #define IP_FW_TCPOPT_MSS 0x01
486 #define IP_FW_TCPOPT_WINDOW 0x02
487 #define IP_FW_TCPOPT_SACK 0x04
488 #define IP_FW_TCPOPT_TS 0x08
489 #define IP_FW_TCPOPT_CC 0x10
491 #define ICMP_REJECT_RST 0x100 /* fake ICMP code (send a TCP RST) */
494 * Main firewall chains definitions and global var's definitions.
496 #ifdef BSD_KERNEL_PRIVATE
500 u_int32_t version
; /* Version of this structure. MUST be set */
501 /* by clients. Should always be */
502 /* set to IP_FW_CURRENT_API_VERSION. */
503 user32_addr_t context
; /* Context that is usable by user processes to */
504 /* identify this rule. */
505 user32_addr_t next
; /* linked list of rules */
506 user32_addr_t next_rule
;/* ptr to next [skipto] rule */
507 /* 'next_rule' is used to pass up 'set_disable' status */
509 u_int16_t act_ofs
; /* offset of action in 32-bit units */
510 u_int16_t cmd_len
; /* # of 32-bit words in cmd */
511 u_int16_t rulenum
; /* rule number */
512 u_int8_t set
; /* rule set (0..31) */
513 u_int32_t set_masks
[2]; /* masks for manipulating sets atomically */
514 #define RESVD_SET 31 /* set for default and persistent rules */
515 u_int8_t _pad
; /* padding */
517 /* These fields are present in all rules. */
518 u_int64_t pcnt
; /* Packet counter */
519 u_int64_t bcnt
; /* Byte counter */
520 u_int32_t timestamp
; /* tv_sec of last match */
522 u_int32_t reserved_1
; /* reserved - set to 0 */
523 u_int32_t reserved_2
; /* reserved - set to 0 */
525 ipfw_insn cmd
[1]; /* storage for commands */
531 u_int32_t version
; /* Version of this structure. MUST be set */
532 /* by clients. Should always be */
533 /* set to IP_FW_CURRENT_API_VERSION. */
534 __uint64_t context
__attribute__((aligned(8))); /* Context that is usable by user processes to */
535 /* identify this rule. */
536 user64_addr_t next
; /* linked list of rules */
537 user64_addr_t next_rule
; /* ptr to next [skipto] rule */
538 /* 'next_rule' is used to pass up 'set_disable' status */
540 u_int16_t act_ofs
; /* offset of action in 32-bit units */
541 u_int16_t cmd_len
; /* # of 32-bit words in cmd */
542 u_int16_t rulenum
; /* rule number */
543 u_int8_t set
; /* rule set (0..31) */
544 u_int32_t set_masks
[2]; /* masks for manipulating sets atomically */
545 #define RESVD_SET 31 /* set for default and persistent rules */
546 u_int8_t _pad
; /* padding */
548 /* These fields are present in all rules. */
549 u_int64_t pcnt
__attribute__((aligned(8))); /* Packet counter */
550 u_int64_t bcnt
__attribute__((aligned(8))); /* Byte counter */
551 u_int32_t timestamp
; /* tv_sec of last match */
553 u_int32_t reserved_1
; /* reserved - set to 0 */
554 u_int32_t reserved_2
; /* reserved - set to 0 */
556 ipfw_insn cmd
[1]; /* storage for commands */
560 typedef struct _ipfw_dyn_rule_64 ipfw_dyn_rule_64
;
561 typedef struct _ipfw_dyn_rule_32 ipfw_dyn_rule_32
;
564 struct _ipfw_dyn_rule_32
{
565 user32_addr_t next
; /* linked list of rules. */
566 user32_addr_t rule
; /* pointer to rule */
567 /* 'rule' is used to pass up the rule number (from the parent) */
569 user32_addr_t parent
; /* pointer to parent rule */
570 u_int64_t pcnt
; /* packet match counter */
571 u_int64_t bcnt
; /* byte match counter */
572 struct ipfw_flow_id id
; /* (masked) flow id */
573 u_int32_t expire
; /* expire time */
574 u_int32_t bucket
; /* which bucket in hash table */
575 u_int32_t state
; /* state of this rule (typically a
576 * combination of TCP flags)
578 u_int32_t ack_fwd
; /* most recent ACKs in forward */
579 u_int32_t ack_rev
; /* and reverse directions (used */
580 /* to generate keepalives) */
581 u_int16_t dyn_type
; /* rule type */
582 u_int16_t count
; /* refcount */
587 struct _ipfw_dyn_rule_64
{
588 user64_addr_t next
; /* linked list of rules. */
589 user64_addr_t rule
; /* pointer to rule */
590 /* 'rule' is used to pass up the rule number (from the parent) */
592 user64_addr_t parent
; /* pointer to parent rule */
593 u_int64_t pcnt
; /* packet match counter */
594 u_int64_t bcnt
; /* byte match counter */
595 struct ipfw_flow_id id
; /* (masked) flow id */
596 u_int32_t expire
; /* expire time */
597 u_int32_t bucket
; /* which bucket in hash table */
598 u_int32_t state
; /* state of this rule (typically a
599 * combination of TCP flags)
601 u_int32_t ack_fwd
; /* most recent ACKs in forward */
602 u_int32_t ack_rev
; /* and reverse directions (used */
603 /* to generate keepalives) */
604 u_int16_t dyn_type
; /* rule type */
605 u_int16_t count
; /* refcount */
609 typedef struct _ipfw_insn_pipe_64
{
611 user64_addr_t pipe_ptr
; /* XXX */
614 typedef struct _ipfw_insn_pipe_32
{
616 user32_addr_t pipe_ptr
; /* XXX */
620 #define IPFW_DEFAULT_RULE 65535
624 #define IP_FW_PORT_DYNT_FLAG 0x10000
625 #define IP_FW_PORT_TEE_FLAG 0x20000
626 #define IP_FW_PORT_DENY_FLAG 0x40000
629 #include <netinet/ip_flowid.h>
634 * Function definitions.
641 void flush_pipe_ptrs(struct dn_flow_set
*match
); /* used by dummynet */
642 void ipfw_init(void); /* called from raw_ip.c: load_ipfw() */
644 typedef int ip_fw_chk_t (struct ip_fw_args
*args
);
645 typedef int ip_fw_ctl_t (struct sockopt
*);
646 extern ip_fw_chk_t
*ip_fw_chk_ptr
;
647 extern ip_fw_ctl_t
*ip_fw_ctl_ptr
;
648 extern int fw_one_pass
;
649 extern int fw_enable
;
650 #define IPFW_LOADED (ip_fw_chk_ptr != NULL)
651 #endif /* IPFIREWALL */
652 #endif /* BSD_KERNEL_PRIVATE */
654 #endif /* __APPLE_API_OBSOLETE */
655 #endif /* _IPFW2_H */