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.c,v 1.6.2.18 2003/10/17 11:01:03 scottl Exp $
32 * Implement IP packet firewall (new version)
36 #error IPFIREWALL requires INET.
40 #include <machine/spl.h>
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
46 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/syslog.h>
52 #include <sys/ucred.h>
54 #include <net/route.h>
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/in_var.h>
58 #include <netinet/in_pcb.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/ip_icmp.h>
62 #include <netinet/ip_fw.h>
63 #include <netinet/ip_divert.h>
66 #include <netinet/ip_dummynet.h>
69 #include <netinet/tcp.h>
70 #include <netinet/tcp_timer.h>
71 #include <netinet/tcp_var.h>
72 #include <netinet/tcpip.h>
73 #include <netinet/udp.h>
74 #include <netinet/udp_var.h>
77 #include <netinet6/ipsec.h>
80 #include <netinet/if_ether.h> /* XXX for ETHERTYPE_IP */
82 #include "ip_fw2_compat.h"
84 #include <sys/kern_event.h>
88 #include <machine/in_cksum.h>
89 */ /* XXX for in_cksum */
92 * XXX This one should go in sys/mbuf.h. It is used to avoid that
93 * a firewall-generated packet loops forever through the firewall.
95 #ifndef M_SKIP_FIREWALL
96 #define M_SKIP_FIREWALL 0x4000
100 * set_disable contains one bit per set value (0..31).
101 * If the bit is set, all rules with the corresponding set
102 * are disabled. Set RESVD_SET(31) is reserved for the default rule
103 * and rules that are not deleted by the flush command,
104 * and CANNOT be disabled.
105 * Rules in set RESVD_SET can only be deleted explicitly.
107 static u_int32_t set_disable
;
110 static int verbose_limit
;
112 #define IPFW_DEFAULT_RULE 65535
114 #define IPFW_RULE_INACTIVE 1
117 * list of rules for layer 3
119 static struct ip_fw
*layer3_chain
;
121 MALLOC_DEFINE(M_IPFW
, "IpFw/IpAcct", "IpFw/IpAcct chain's");
123 static int fw_debug
= 1;
124 static int autoinc_step
= 100; /* bounded to 1..1000 in add_rule() */
127 SYSCTL_NODE(_net_inet_ip
, OID_AUTO
, fw
, CTLFLAG_RW
, 0, "Firewall");
128 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, enable
,
130 &fw_enable
, 0, "Enable ipfw");
131 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, autoinc_step
, CTLFLAG_RW
,
132 &autoinc_step
, 0, "Rule number autincrement step");
133 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, one_pass
,
136 "Only do a single pass through ipfw when using dummynet(4)");
137 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, debug
,
139 &fw_debug
, 0, "Enable printing of debug ip_fw statements");
140 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, verbose
,
142 &fw_verbose
, 0, "Log matches to ipfw rules");
143 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, verbose_limit
, CTLFLAG_RW
,
144 &verbose_limit
, 0, "Set upper limit of matches of ipfw rules logged");
147 * Description of dynamic rules.
149 * Dynamic rules are stored in lists accessed through a hash table
150 * (ipfw_dyn_v) whose size is curr_dyn_buckets. This value can
151 * be modified through the sysctl variable dyn_buckets which is
152 * updated when the table becomes empty.
154 * XXX currently there is only one list, ipfw_dyn.
156 * When a packet is received, its address fields are first masked
157 * with the mask defined for the rule, then hashed, then matched
158 * against the entries in the corresponding list.
159 * Dynamic rules can be used for different purposes:
161 * + enforcing limits on the number of sessions;
162 * + in-kernel NAT (not implemented yet)
164 * The lifetime of dynamic rules is regulated by dyn_*_lifetime,
165 * measured in seconds and depending on the flags.
167 * The total number of dynamic rules is stored in dyn_count.
168 * The max number of dynamic rules is dyn_max. When we reach
169 * the maximum number of rules we do not create anymore. This is
170 * done to avoid consuming too much memory, but also too much
171 * time when searching on each packet (ideally, we should try instead
172 * to put a limit on the length of the list on each bucket...).
174 * Each dynamic rule holds a pointer to the parent ipfw rule so
175 * we know what action to perform. Dynamic rules are removed when
176 * the parent rule is deleted. XXX we should make them survive.
178 * There are some limitations with dynamic rules -- we do not
179 * obey the 'randomized match', and we do not do multiple
180 * passes through the firewall. XXX check the latter!!!
182 static ipfw_dyn_rule
**ipfw_dyn_v
= NULL
;
183 static u_int32_t dyn_buckets
= 256; /* must be power of 2 */
184 static u_int32_t curr_dyn_buckets
= 256; /* must be power of 2 */
187 * Timeouts for various events in handing dynamic rules.
189 static u_int32_t dyn_ack_lifetime
= 300;
190 static u_int32_t dyn_syn_lifetime
= 20;
191 static u_int32_t dyn_fin_lifetime
= 1;
192 static u_int32_t dyn_rst_lifetime
= 1;
193 static u_int32_t dyn_udp_lifetime
= 10;
194 static u_int32_t dyn_short_lifetime
= 5;
197 * Keepalives are sent if dyn_keepalive is set. They are sent every
198 * dyn_keepalive_period seconds, in the last dyn_keepalive_interval
199 * seconds of lifetime of a rule.
200 * dyn_rst_lifetime and dyn_fin_lifetime should be strictly lower
201 * than dyn_keepalive_period.
204 static u_int32_t dyn_keepalive_interval
= 20;
205 static u_int32_t dyn_keepalive_period
= 5;
206 static u_int32_t dyn_keepalive
= 1; /* do send keepalives */
208 static u_int32_t static_count
; /* # of static rules */
209 static u_int32_t static_len
; /* size in bytes of static rules */
210 static u_int32_t dyn_count
; /* # of dynamic rules */
211 static u_int32_t dyn_max
= 4096; /* max # of dynamic rules */
213 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_buckets
, CTLFLAG_RW
,
214 &dyn_buckets
, 0, "Number of dyn. buckets");
215 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, curr_dyn_buckets
, CTLFLAG_RD
,
216 &curr_dyn_buckets
, 0, "Current Number of dyn. buckets");
217 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_count
, CTLFLAG_RD
,
218 &dyn_count
, 0, "Number of dyn. rules");
219 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_max
, CTLFLAG_RW
,
220 &dyn_max
, 0, "Max number of dyn. rules");
221 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, static_count
, CTLFLAG_RD
,
222 &static_count
, 0, "Number of static rules");
223 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_ack_lifetime
, CTLFLAG_RW
,
224 &dyn_ack_lifetime
, 0, "Lifetime of dyn. rules for acks");
225 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_syn_lifetime
, CTLFLAG_RW
,
226 &dyn_syn_lifetime
, 0, "Lifetime of dyn. rules for syn");
227 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_fin_lifetime
, CTLFLAG_RW
,
228 &dyn_fin_lifetime
, 0, "Lifetime of dyn. rules for fin");
229 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_rst_lifetime
, CTLFLAG_RW
,
230 &dyn_rst_lifetime
, 0, "Lifetime of dyn. rules for rst");
231 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_udp_lifetime
, CTLFLAG_RW
,
232 &dyn_udp_lifetime
, 0, "Lifetime of dyn. rules for UDP");
233 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_short_lifetime
, CTLFLAG_RW
,
234 &dyn_short_lifetime
, 0, "Lifetime of dyn. rules for other situations");
235 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_keepalive
, CTLFLAG_RW
,
236 &dyn_keepalive
, 0, "Enable keepalives for dyn. rules");
238 #endif /* SYSCTL_NODE */
241 extern lck_mtx_t
*ip_mutex
;
242 static ip_fw_chk_t ipfw_chk
;
245 lck_grp_t
*ipfw_mutex_grp
;
246 lck_grp_attr_t
*ipfw_mutex_grp_attr
;
247 lck_attr_t
*ipfw_mutex_attr
;
248 lck_mtx_t
*ipfw_mutex
;
250 extern void ipfwsyslog( int level
, char *format
,...);
253 ip_dn_ruledel_t
*ip_dn_ruledel_ptr
= NULL
; /* hook into dummynet */
254 #endif /* DUMMYNET */
256 #define KEV_LOG_SUBCLASS 10
257 #define IPFWLOGEVENT 0
259 #define ipfwstring "ipfw:"
260 static size_t ipfwstringlen
;
262 #define dolog( a ) { \
263 if ( fw_verbose == 2 ) /* Apple logging, log to ipfw.log */ \
268 void ipfwsyslog( int level
, char *format
,...)
272 struct kev_msg ev_msg
;
274 char msgBuf
[msgsize
];
279 va_start( ap
, format
);
280 loglen
= vsnprintf(msgBuf
, msgsize
, format
, ap
);
283 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
284 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
285 ev_msg
.kev_subclass
= KEV_LOG_SUBCLASS
;
286 ev_msg
.event_code
= IPFWLOGEVENT
;
288 /* get rid of the trailing \n */
291 pri
= LOG_PRI(level
);
293 /* remove "ipfw:" prefix if logging to ipfw log */
294 if ( !(strncmp( ipfwstring
, msgBuf
, ipfwstringlen
))){
295 dptr
= msgBuf
+ipfwstringlen
;
298 ev_msg
.dv
[0].data_ptr
= &pri
;
299 ev_msg
.dv
[0].data_length
= 1;
300 ev_msg
.dv
[1].data_ptr
= dptr
;
301 ev_msg
.dv
[1].data_length
= 100; /* bug in kern_post_msg, it can't handle size > 256-msghdr */
302 ev_msg
.dv
[2].data_length
= 0;
304 kev_post_msg(&ev_msg
);
308 * This macro maps an ip pointer into a layer3 header pointer of type T
310 #define L3HDR(T, ip) ((T *)((u_int32_t *)(ip) + (ip)->ip_hl))
313 icmptype_match(struct ip
*ip
, ipfw_insn_u32
*cmd
)
315 int type
= L3HDR(struct icmp
,ip
)->icmp_type
;
317 return (type
<= ICMP_MAXTYPE
&& (cmd
->d
[0] & (1<<type
)) );
320 #define TT ( (1 << ICMP_ECHO) | (1 << ICMP_ROUTERSOLICIT) | \
321 (1 << ICMP_TSTAMP) | (1 << ICMP_IREQ) | (1 << ICMP_MASKREQ) )
324 is_icmp_query(struct ip
*ip
)
326 int type
= L3HDR(struct icmp
, ip
)->icmp_type
;
327 return (type
<= ICMP_MAXTYPE
&& (TT
& (1<<type
)) );
332 * The following checks use two arrays of 8 or 16 bits to store the
333 * bits that we want set or clear, respectively. They are in the
334 * low and high half of cmd->arg1 or cmd->d[0].
336 * We scan options and store the bits we find set. We succeed if
338 * (want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear
340 * The code is sometimes optimized not to store additional variables.
344 flags_match(ipfw_insn
*cmd
, u_int8_t bits
)
349 if ( ((cmd
->arg1
& 0xff) & bits
) != 0)
350 return 0; /* some bits we want set were clear */
351 want_clear
= (cmd
->arg1
>> 8) & 0xff;
352 if ( (want_clear
& bits
) != want_clear
)
353 return 0; /* some bits we want clear were set */
358 ipopts_match(struct ip
*ip
, ipfw_insn
*cmd
)
360 int optlen
, bits
= 0;
361 u_char
*cp
= (u_char
*)(ip
+ 1);
362 int x
= (ip
->ip_hl
<< 2) - sizeof (struct ip
);
364 for (; x
> 0; x
-= optlen
, cp
+= optlen
) {
365 int opt
= cp
[IPOPT_OPTVAL
];
367 if (opt
== IPOPT_EOL
)
369 if (opt
== IPOPT_NOP
)
372 optlen
= cp
[IPOPT_OLEN
];
373 if (optlen
<= 0 || optlen
> x
)
374 return 0; /* invalid or truncated */
382 bits
|= IP_FW_IPOPT_LSRR
;
386 bits
|= IP_FW_IPOPT_SSRR
;
390 bits
|= IP_FW_IPOPT_RR
;
394 bits
|= IP_FW_IPOPT_TS
;
398 return (flags_match(cmd
, bits
));
402 tcpopts_match(struct ip
*ip
, ipfw_insn
*cmd
)
404 int optlen
, bits
= 0;
405 struct tcphdr
*tcp
= L3HDR(struct tcphdr
,ip
);
406 u_char
*cp
= (u_char
*)(tcp
+ 1);
407 int x
= (tcp
->th_off
<< 2) - sizeof(struct tcphdr
);
409 for (; x
> 0; x
-= optlen
, cp
+= optlen
) {
411 if (opt
== TCPOPT_EOL
)
413 if (opt
== TCPOPT_NOP
)
427 bits
|= IP_FW_TCPOPT_MSS
;
431 bits
|= IP_FW_TCPOPT_WINDOW
;
434 case TCPOPT_SACK_PERMITTED
:
436 bits
|= IP_FW_TCPOPT_SACK
;
439 case TCPOPT_TIMESTAMP
:
440 bits
|= IP_FW_TCPOPT_TS
;
446 bits
|= IP_FW_TCPOPT_CC
;
450 return (flags_match(cmd
, bits
));
454 iface_match(struct ifnet
*ifp
, ipfw_insn_if
*cmd
)
456 if (ifp
== NULL
) /* no iface with this packet, match fails */
458 /* Check by name or by IP address */
459 if (cmd
->name
[0] != '\0') { /* match by name */
460 /* Check unit number (-1 is wildcard) */
461 if (cmd
->p
.unit
!= -1 && cmd
->p
.unit
!= ifp
->if_unit
)
464 if (!strncmp(ifp
->if_name
, cmd
->name
, IFNAMSIZ
))
469 ifnet_lock_shared(ifp
);
470 TAILQ_FOREACH(ia
, &ifp
->if_addrhead
, ifa_link
) {
471 if (ia
->ifa_addr
== NULL
)
473 if (ia
->ifa_addr
->sa_family
!= AF_INET
)
475 if (cmd
->p
.ip
.s_addr
== ((struct sockaddr_in
*)
476 (ia
->ifa_addr
))->sin_addr
.s_addr
) {
477 ifnet_lock_done(ifp
);
478 return(1); /* match */
481 ifnet_lock_done(ifp
);
483 return(0); /* no match, fail ... */
487 * The 'verrevpath' option checks that the interface that an IP packet
488 * arrives on is the same interface that traffic destined for the
489 * packet's source address would be routed out of. This is a measure
490 * to block forged packets. This is also commonly known as "anti-spoofing"
491 * or Unicast Reverse Path Forwarding (Unicast RFP) in Cisco-ese. The
492 * name of the knob is purposely reminisent of the Cisco IOS command,
494 * ip verify unicast reverse-path
496 * which implements the same functionality. But note that syntax is
497 * misleading. The check may be performed on all IP packets whether unicast,
498 * multicast, or broadcast.
501 verify_rev_path(struct in_addr src
, struct ifnet
*ifp
)
503 static struct route ro
;
504 struct sockaddr_in
*dst
;
506 dst
= (struct sockaddr_in
*)&(ro
.ro_dst
);
508 /* Check if we've cached the route from the previous call. */
509 if (src
.s_addr
!= dst
->sin_addr
.s_addr
) {
512 bzero(dst
, sizeof(*dst
));
513 dst
->sin_family
= AF_INET
;
514 dst
->sin_len
= sizeof(*dst
);
517 rtalloc_ign(&ro
, RTF_CLONING
|RTF_PRCLONING
);
520 if ((ro
.ro_rt
== NULL
) || (ifp
== NULL
) ||
521 (ro
.ro_rt
->rt_ifp
->if_index
!= ifp
->if_index
))
528 static u_int64_t norule_counter
; /* counter for ipfw_log(NULL...) */
530 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
531 #define SNP(buf) buf, sizeof(buf)
534 * We enter here when we have a rule with O_LOG.
535 * XXX this function alone takes about 2Kbytes of code!
538 ipfw_log(struct ip_fw
*f
, u_int hlen
, struct ether_header
*eh
,
539 struct mbuf
*m
, struct ifnet
*oif
)
542 int limit_reached
= 0;
543 char ipv4str
[MAX_IPv4_STR_LEN
];
544 char action2
[40], proto
[48], fragment
[28];
549 if (f
== NULL
) { /* bogus pkt */
550 if (verbose_limit
!= 0 && norule_counter
>= verbose_limit
)
553 if (norule_counter
== verbose_limit
)
554 limit_reached
= verbose_limit
;
556 } else { /* O_LOG is the first action, find the real one */
557 ipfw_insn
*cmd
= ACTION_PTR(f
);
558 ipfw_insn_log
*l
= (ipfw_insn_log
*)cmd
;
560 if (l
->max_log
!= 0 && l
->log_left
== 0)
563 if (l
->log_left
== 0)
564 limit_reached
= l
->max_log
;
565 cmd
+= F_LEN(cmd
); /* point to first action */
566 if (cmd
->opcode
== O_PROB
)
570 switch (cmd
->opcode
) {
576 if (cmd
->arg1
==ICMP_REJECT_RST
)
578 else if (cmd
->arg1
==ICMP_UNREACH_HOST
)
581 snprintf(SNPARGS(action2
, 0), "Unreach %d",
592 snprintf(SNPARGS(action2
, 0), "Divert %d",
596 snprintf(SNPARGS(action2
, 0), "Tee %d",
600 snprintf(SNPARGS(action2
, 0), "SkipTo %d",
604 snprintf(SNPARGS(action2
, 0), "Pipe %d",
608 snprintf(SNPARGS(action2
, 0), "Queue %d",
612 ipfw_insn_sa
*sa
= (ipfw_insn_sa
*)cmd
;
615 if (f
->reserved_1
== IPFW_RULE_INACTIVE
) {
618 len
= snprintf(SNPARGS(action2
, 0), "Forward to %s",
619 inet_ntop(AF_INET
, &sa
->sa
.sin_addr
, ipv4str
, sizeof(ipv4str
)));
621 snprintf(SNPARGS(action2
, len
), ":%d",
631 if (hlen
== 0) { /* non-ip */
632 snprintf(SNPARGS(proto
, 0), "MAC");
634 struct ip
*ip
= mtod(m
, struct ip
*);
635 /* these three are all aliases to the same thing */
636 struct icmp
*const icmp
= L3HDR(struct icmp
, ip
);
637 struct tcphdr
*const tcp
= (struct tcphdr
*)icmp
;
638 struct udphdr
*const udp
= (struct udphdr
*)icmp
;
640 int ip_off
, offset
, ip_len
;
644 if (eh
!= NULL
) { /* layer 2 packets are as on the wire */
645 ip_off
= ntohs(ip
->ip_off
);
646 ip_len
= ntohs(ip
->ip_len
);
651 offset
= ip_off
& IP_OFFMASK
;
654 len
= snprintf(SNPARGS(proto
, 0), "TCP %s",
655 inet_ntop(AF_INET
, &ip
->ip_src
, ipv4str
, sizeof(ipv4str
)));
657 snprintf(SNPARGS(proto
, len
), ":%d %s:%d",
658 ntohs(tcp
->th_sport
),
659 inet_ntop(AF_INET
, &ip
->ip_dst
, ipv4str
, sizeof(ipv4str
)),
660 ntohs(tcp
->th_dport
));
662 snprintf(SNPARGS(proto
, len
), " %s",
663 inet_ntop(AF_INET
, &ip
->ip_dst
, ipv4str
, sizeof(ipv4str
)));
667 len
= snprintf(SNPARGS(proto
, 0), "UDP %s",
668 inet_ntop(AF_INET
, &ip
->ip_src
, ipv4str
, sizeof(ipv4str
)));
670 snprintf(SNPARGS(proto
, len
), ":%d %s:%d",
671 ntohs(udp
->uh_sport
),
672 inet_ntop(AF_INET
, &ip
->ip_dst
, ipv4str
, sizeof(ipv4str
)),
673 ntohs(udp
->uh_dport
));
675 snprintf(SNPARGS(proto
, len
), " %s",
676 inet_ntop(AF_INET
, &ip
->ip_dst
, ipv4str
, sizeof(ipv4str
)));
681 len
= snprintf(SNPARGS(proto
, 0),
683 icmp
->icmp_type
, icmp
->icmp_code
);
685 len
= snprintf(SNPARGS(proto
, 0), "ICMP ");
686 len
+= snprintf(SNPARGS(proto
, len
), "%s",
687 inet_ntop(AF_INET
, &ip
->ip_src
, ipv4str
, sizeof(ipv4str
)));
688 snprintf(SNPARGS(proto
, len
), " %s",
689 inet_ntop(AF_INET
, &ip
->ip_dst
, ipv4str
, sizeof(ipv4str
)));
693 len
= snprintf(SNPARGS(proto
, 0), "P:%d %s", ip
->ip_p
,
694 inet_ntop(AF_INET
, &ip
->ip_src
, ipv4str
, sizeof(ipv4str
)));
695 snprintf(SNPARGS(proto
, len
), " %s",
696 inet_ntop(AF_INET
, &ip
->ip_dst
, ipv4str
, sizeof(ipv4str
)));
700 if (ip_off
& (IP_MF
| IP_OFFMASK
))
701 snprintf(SNPARGS(fragment
, 0), " (frag %d:%d@%d%s)",
702 ntohs(ip
->ip_id
), ip_len
- (ip
->ip_hl
<< 2),
704 (ip_off
& IP_MF
) ? "+" : "");
706 if (oif
|| m
->m_pkthdr
.rcvif
)
708 dolog((LOG_AUTHPRIV
| LOG_INFO
,
709 "ipfw: %d %s %s %s via %s%d%s\n",
711 action
, proto
, oif
? "out" : "in",
712 oif
? oif
->if_name
: m
->m_pkthdr
.rcvif
->if_name
,
713 oif
? oif
->if_unit
: m
->m_pkthdr
.rcvif
->if_unit
,
717 dolog((LOG_AUTHPRIV
| LOG_INFO
,
718 "ipfw: %d %s %s [no if info]%s\n",
720 action
, proto
, fragment
));
723 dolog((LOG_AUTHPRIV
| LOG_NOTICE
,
724 "ipfw: limit %d reached on entry %d\n",
725 limit_reached
, f
? f
->rulenum
: -1));
730 * IMPORTANT: the hash function for dynamic rules must be commutative
731 * in source and destination (ip,port), because rules are bidirectional
732 * and we want to find both in the same bucket.
735 hash_packet(struct ipfw_flow_id
*id
)
739 i
= (id
->dst_ip
) ^ (id
->src_ip
) ^ (id
->dst_port
) ^ (id
->src_port
);
740 i
&= (curr_dyn_buckets
- 1);
745 * unlink a dynamic rule from a chain. prev is a pointer to
746 * the previous one, q is a pointer to the rule to delete,
747 * head is a pointer to the head of the queue.
748 * Modifies q and potentially also head.
750 #define UNLINK_DYN_RULE(prev, head, q) { \
751 ipfw_dyn_rule *old_q = q; \
753 /* remove a refcount to the parent */ \
754 if (q->dyn_type == O_LIMIT) \
755 q->parent->count--; \
756 DEB(printf("ipfw: unlink entry 0x%08x %d -> 0x%08x %d, %d left\n",\
757 (q->id.src_ip), (q->id.src_port), \
758 (q->id.dst_ip), (q->id.dst_port), dyn_count-1 ); ) \
760 prev->next = q = q->next; \
762 head = q = q->next; \
764 _FREE(old_q, M_IPFW); }
766 #define TIME_LEQ(a,b) ((int)((a)-(b)) <= 0)
769 * Remove dynamic rules pointing to "rule", or all of them if rule == NULL.
771 * If keep_me == NULL, rules are deleted even if not expired,
772 * otherwise only expired rules are removed.
774 * The value of the second parameter is also used to point to identify
775 * a rule we absolutely do not want to remove (e.g. because we are
776 * holding a reference to it -- this is the case with O_LIMIT_PARENT
777 * rules). The pointer is only used for comparison, so any non-null
781 remove_dyn_rule(struct ip_fw
*rule
, ipfw_dyn_rule
*keep_me
)
783 static u_int32_t last_remove
= 0;
785 #define FORCE (keep_me == NULL)
787 ipfw_dyn_rule
*prev
, *q
;
788 int i
, pass
= 0, max_pass
= 0;
789 struct timeval timenow
;
791 getmicrotime(&timenow
);
793 if (ipfw_dyn_v
== NULL
|| dyn_count
== 0)
795 /* do not expire more than once per second, it is useless */
796 if (!FORCE
&& last_remove
== timenow
.tv_sec
)
798 last_remove
= timenow
.tv_sec
;
801 * because O_LIMIT refer to parent rules, during the first pass only
802 * remove child and mark any pending LIMIT_PARENT, and remove
803 * them in a second pass.
806 for (i
= 0 ; i
< curr_dyn_buckets
; i
++) {
807 for (prev
=NULL
, q
= ipfw_dyn_v
[i
] ; q
; ) {
809 * Logic can become complex here, so we split tests.
813 if (rule
!= NULL
&& rule
!= q
->rule
)
814 goto next
; /* not the one we are looking for */
815 if (q
->dyn_type
== O_LIMIT_PARENT
) {
817 * handle parent in the second pass,
818 * record we need one.
823 if (FORCE
&& q
->count
!= 0 ) {
824 /* XXX should not happen! */
825 printf("ipfw: OUCH! cannot remove rule,"
826 " count %d\n", q
->count
);
830 !TIME_LEQ( q
->expire
, timenow
.tv_sec
))
833 if (q
->dyn_type
!= O_LIMIT_PARENT
|| !q
->count
) {
834 UNLINK_DYN_RULE(prev
, ipfw_dyn_v
[i
], q
);
842 if (pass
++ < max_pass
)
848 * lookup a dynamic rule.
850 static ipfw_dyn_rule
*
851 lookup_dyn_rule(struct ipfw_flow_id
*pkt
, int *match_direction
,
855 * stateful ipfw extensions.
856 * Lookup into dynamic session queue
858 #define MATCH_REVERSE 0
859 #define MATCH_FORWARD 1
861 #define MATCH_UNKNOWN 3
862 #define BOTH_SYN (TH_SYN | (TH_SYN << 8))
863 #define BOTH_FIN (TH_FIN | (TH_FIN << 8))
865 int i
, dir
= MATCH_NONE
;
866 ipfw_dyn_rule
*prev
, *q
=NULL
;
867 struct timeval timenow
;
869 getmicrotime(&timenow
);
871 if (ipfw_dyn_v
== NULL
)
872 goto done
; /* not found */
873 i
= hash_packet( pkt
);
874 for (prev
=NULL
, q
= ipfw_dyn_v
[i
] ; q
!= NULL
; ) {
875 if (q
->dyn_type
== O_LIMIT_PARENT
&& q
->count
)
877 if (TIME_LEQ( q
->expire
, timenow
.tv_sec
)) { /* expire entry */
880 /* check if entry is TCP */
881 if ( q
->id
.proto
== IPPROTO_TCP
)
883 /* do not delete an established TCP connection which hasn't been closed by both sides */
884 if ( (q
->state
& (BOTH_SYN
| BOTH_FIN
)) != (BOTH_SYN
| BOTH_FIN
) )
888 UNLINK_DYN_RULE(prev
, ipfw_dyn_v
[i
], q
);
892 if (pkt
->proto
== q
->id
.proto
&&
893 q
->dyn_type
!= O_LIMIT_PARENT
) {
894 if (pkt
->src_ip
== q
->id
.src_ip
&&
895 pkt
->dst_ip
== q
->id
.dst_ip
&&
896 pkt
->src_port
== q
->id
.src_port
&&
897 pkt
->dst_port
== q
->id
.dst_port
) {
901 if (pkt
->src_ip
== q
->id
.dst_ip
&&
902 pkt
->dst_ip
== q
->id
.src_ip
&&
903 pkt
->src_port
== q
->id
.dst_port
&&
904 pkt
->dst_port
== q
->id
.src_port
) {
914 goto done
; /* q = NULL, not found */
916 if ( prev
!= NULL
) { /* found and not in front */
917 prev
->next
= q
->next
;
918 q
->next
= ipfw_dyn_v
[i
];
921 if (pkt
->proto
== IPPROTO_TCP
) { /* update state according to flags */
922 u_char flags
= pkt
->flags
& (TH_FIN
|TH_SYN
|TH_RST
);
924 q
->state
|= (dir
== MATCH_FORWARD
) ? flags
: (flags
<< 8);
926 case TH_SYN
: /* opening */
927 q
->expire
= timenow
.tv_sec
+ dyn_syn_lifetime
;
930 case BOTH_SYN
: /* move to established */
931 case BOTH_SYN
| TH_FIN
: /* one side tries to close */
932 case BOTH_SYN
| (TH_FIN
<< 8) :
934 #define _SEQ_GE(a,b) ((int)(a) - (int)(b) >= 0)
935 u_int32_t ack
= ntohl(tcp
->th_ack
);
936 if (dir
== MATCH_FORWARD
) {
937 if (q
->ack_fwd
== 0 || _SEQ_GE(ack
, q
->ack_fwd
))
939 else { /* ignore out-of-sequence */
943 if (q
->ack_rev
== 0 || _SEQ_GE(ack
, q
->ack_rev
))
945 else { /* ignore out-of-sequence */
950 q
->expire
= timenow
.tv_sec
+ dyn_ack_lifetime
;
953 case BOTH_SYN
| BOTH_FIN
: /* both sides closed */
954 if (dyn_fin_lifetime
>= dyn_keepalive_period
)
955 dyn_fin_lifetime
= dyn_keepalive_period
- 1;
956 q
->expire
= timenow
.tv_sec
+ dyn_fin_lifetime
;
962 * reset or some invalid combination, but can also
963 * occur if we use keep-state the wrong way.
965 if ( (q
->state
& ((TH_RST
<< 8)|TH_RST
)) == 0)
966 printf("invalid state: 0x%x\n", q
->state
);
968 if (dyn_rst_lifetime
>= dyn_keepalive_period
)
969 dyn_rst_lifetime
= dyn_keepalive_period
- 1;
970 q
->expire
= timenow
.tv_sec
+ dyn_rst_lifetime
;
973 } else if (pkt
->proto
== IPPROTO_UDP
) {
974 q
->expire
= timenow
.tv_sec
+ dyn_udp_lifetime
;
976 /* other protocols */
977 q
->expire
= timenow
.tv_sec
+ dyn_short_lifetime
;
981 *match_direction
= dir
;
986 realloc_dynamic_table(void)
989 * Try reallocation, make sure we have a power of 2 and do
990 * not allow more than 64k entries. In case of overflow,
994 if (dyn_buckets
> 65536)
996 if ((dyn_buckets
& (dyn_buckets
-1)) != 0) { /* not a power of 2 */
997 dyn_buckets
= curr_dyn_buckets
; /* reset */
1000 curr_dyn_buckets
= dyn_buckets
;
1001 if (ipfw_dyn_v
!= NULL
)
1002 _FREE(ipfw_dyn_v
, M_IPFW
);
1004 ipfw_dyn_v
= _MALLOC(curr_dyn_buckets
* sizeof(ipfw_dyn_rule
*),
1005 M_IPFW
, M_NOWAIT
| M_ZERO
);
1006 if (ipfw_dyn_v
!= NULL
|| curr_dyn_buckets
<= 2)
1008 curr_dyn_buckets
/= 2;
1013 * Install state of type 'type' for a dynamic session.
1014 * The hash table contains two type of rules:
1015 * - regular rules (O_KEEP_STATE)
1016 * - rules for sessions with limited number of sess per user
1017 * (O_LIMIT). When they are created, the parent is
1018 * increased by 1, and decreased on delete. In this case,
1019 * the third parameter is the parent rule and not the chain.
1020 * - "parent" rules for the above (O_LIMIT_PARENT).
1022 static ipfw_dyn_rule
*
1023 add_dyn_rule(struct ipfw_flow_id
*id
, u_int8_t dyn_type
, struct ip_fw
*rule
)
1027 struct timeval timenow
;
1029 getmicrotime(&timenow
);
1031 if (ipfw_dyn_v
== NULL
||
1032 (dyn_count
== 0 && dyn_buckets
!= curr_dyn_buckets
)) {
1033 realloc_dynamic_table();
1034 if (ipfw_dyn_v
== NULL
)
1035 return NULL
; /* failed ! */
1037 i
= hash_packet(id
);
1039 r
= _MALLOC(sizeof *r
, M_IPFW
, M_NOWAIT
| M_ZERO
);
1042 printf ("ipfw: sorry cannot allocate state\n");
1047 /* increase refcount on parent, and set pointer */
1048 if (dyn_type
== O_LIMIT
) {
1049 ipfw_dyn_rule
*parent
= (ipfw_dyn_rule
*)rule
;
1050 if ( parent
->dyn_type
!= O_LIMIT_PARENT
)
1051 panic("invalid parent");
1054 rule
= parent
->rule
;
1058 r
->expire
= timenow
.tv_sec
+ dyn_syn_lifetime
;
1060 r
->dyn_type
= dyn_type
;
1061 r
->pcnt
= r
->bcnt
= 0;
1065 r
->next
= ipfw_dyn_v
[i
];
1068 DEB(printf("ipfw: add dyn entry ty %d 0x%08x %d -> 0x%08x %d, total %d\n",
1070 (r
->id
.src_ip
), (r
->id
.src_port
),
1071 (r
->id
.dst_ip
), (r
->id
.dst_port
),
1077 * lookup dynamic parent rule using pkt and rule as search keys.
1078 * If the lookup fails, then install one.
1080 static ipfw_dyn_rule
*
1081 lookup_dyn_parent(struct ipfw_flow_id
*pkt
, struct ip_fw
*rule
)
1085 struct timeval timenow
;
1087 getmicrotime(&timenow
);
1090 i
= hash_packet( pkt
);
1091 for (q
= ipfw_dyn_v
[i
] ; q
!= NULL
; q
=q
->next
)
1092 if (q
->dyn_type
== O_LIMIT_PARENT
&&
1094 pkt
->proto
== q
->id
.proto
&&
1095 pkt
->src_ip
== q
->id
.src_ip
&&
1096 pkt
->dst_ip
== q
->id
.dst_ip
&&
1097 pkt
->src_port
== q
->id
.src_port
&&
1098 pkt
->dst_port
== q
->id
.dst_port
) {
1099 q
->expire
= timenow
.tv_sec
+ dyn_short_lifetime
;
1100 DEB(printf("ipfw: lookup_dyn_parent found 0x%p\n",q
);)
1104 return add_dyn_rule(pkt
, O_LIMIT_PARENT
, rule
);
1108 * Install dynamic state for rule type cmd->o.opcode
1110 * Returns 1 (failure) if state is not installed because of errors or because
1111 * session limitations are enforced.
1114 install_state(struct ip_fw
*rule
, ipfw_insn_limit
*cmd
,
1115 struct ip_fw_args
*args
)
1117 static int last_log
;
1118 struct timeval timenow
;
1121 getmicrotime(&timenow
);
1123 DEB(printf("ipfw: install state type %d 0x%08x %u -> 0x%08x %u\n",
1125 (args
->f_id
.src_ip
), (args
->f_id
.src_port
),
1126 (args
->f_id
.dst_ip
), (args
->f_id
.dst_port
) );)
1128 q
= lookup_dyn_rule(&args
->f_id
, NULL
, NULL
);
1130 if (q
!= NULL
) { /* should never occur */
1131 if (last_log
!= timenow
.tv_sec
) {
1132 last_log
= timenow
.tv_sec
;
1133 printf("ipfw: install_state: entry already present, done\n");
1138 if (dyn_count
>= dyn_max
)
1140 * Run out of slots, try to remove any expired rule.
1142 remove_dyn_rule(NULL
, (ipfw_dyn_rule
*)1);
1144 if (dyn_count
>= dyn_max
) {
1145 if (last_log
!= timenow
.tv_sec
) {
1146 last_log
= timenow
.tv_sec
;
1147 printf("ipfw: install_state: Too many dynamic rules\n");
1149 return 1; /* cannot install, notify caller */
1152 switch (cmd
->o
.opcode
) {
1153 case O_KEEP_STATE
: /* bidir rule */
1154 add_dyn_rule(&args
->f_id
, O_KEEP_STATE
, rule
);
1157 case O_LIMIT
: /* limit number of sessions */
1159 u_int16_t limit_mask
= cmd
->limit_mask
;
1160 struct ipfw_flow_id id
;
1161 ipfw_dyn_rule
*parent
;
1163 DEB(printf("ipfw: installing dyn-limit rule %d\n",
1166 id
.dst_ip
= id
.src_ip
= 0;
1167 id
.dst_port
= id
.src_port
= 0;
1168 id
.proto
= args
->f_id
.proto
;
1170 if (limit_mask
& DYN_SRC_ADDR
)
1171 id
.src_ip
= args
->f_id
.src_ip
;
1172 if (limit_mask
& DYN_DST_ADDR
)
1173 id
.dst_ip
= args
->f_id
.dst_ip
;
1174 if (limit_mask
& DYN_SRC_PORT
)
1175 id
.src_port
= args
->f_id
.src_port
;
1176 if (limit_mask
& DYN_DST_PORT
)
1177 id
.dst_port
= args
->f_id
.dst_port
;
1178 parent
= lookup_dyn_parent(&id
, rule
);
1179 if (parent
== NULL
) {
1180 printf("ipfw: add parent failed\n");
1183 if (parent
->count
>= cmd
->conn_limit
) {
1185 * See if we can remove some expired rule.
1187 remove_dyn_rule(rule
, parent
);
1188 if (parent
->count
>= cmd
->conn_limit
) {
1189 if (fw_verbose
&& last_log
!= timenow
.tv_sec
) {
1190 last_log
= timenow
.tv_sec
;
1191 dolog((LOG_AUTHPRIV
| LOG_DEBUG
,
1192 "drop session, too many entries\n"));
1197 add_dyn_rule(&args
->f_id
, O_LIMIT
, (struct ip_fw
*)parent
);
1201 printf("ipfw: unknown dynamic rule type %u\n", cmd
->o
.opcode
);
1204 lookup_dyn_rule(&args
->f_id
, NULL
, NULL
); /* XXX just set lifetime */
1209 * Transmit a TCP packet, containing either a RST or a keepalive.
1210 * When flags & TH_RST, we are sending a RST packet, because of a
1211 * "reset" action matched the packet.
1212 * Otherwise we are sending a keepalive, and flags & TH_
1215 send_pkt(struct ipfw_flow_id
*id
, u_int32_t seq
, u_int32_t ack
, int flags
)
1220 struct route sro
; /* fake route */
1222 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
1225 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
1226 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct ip
) + sizeof(struct tcphdr
);
1227 m
->m_data
+= max_linkhdr
;
1229 ip
= mtod(m
, struct ip
*);
1230 bzero(ip
, m
->m_len
);
1231 tcp
= (struct tcphdr
*)(ip
+ 1); /* no IP options */
1232 ip
->ip_p
= IPPROTO_TCP
;
1235 * Assume we are sending a RST (or a keepalive in the reverse
1236 * direction), swap src and destination addresses and ports.
1238 ip
->ip_src
.s_addr
= htonl(id
->dst_ip
);
1239 ip
->ip_dst
.s_addr
= htonl(id
->src_ip
);
1240 tcp
->th_sport
= htons(id
->dst_port
);
1241 tcp
->th_dport
= htons(id
->src_port
);
1242 if (flags
& TH_RST
) { /* we are sending a RST */
1243 if (flags
& TH_ACK
) {
1244 tcp
->th_seq
= htonl(ack
);
1245 tcp
->th_ack
= htonl(0);
1246 tcp
->th_flags
= TH_RST
;
1250 tcp
->th_seq
= htonl(0);
1251 tcp
->th_ack
= htonl(seq
);
1252 tcp
->th_flags
= TH_RST
| TH_ACK
;
1256 * We are sending a keepalive. flags & TH_SYN determines
1257 * the direction, forward if set, reverse if clear.
1258 * NOTE: seq and ack are always assumed to be correct
1259 * as set by the caller. This may be confusing...
1261 if (flags
& TH_SYN
) {
1263 * we have to rewrite the correct addresses!
1265 ip
->ip_dst
.s_addr
= htonl(id
->dst_ip
);
1266 ip
->ip_src
.s_addr
= htonl(id
->src_ip
);
1267 tcp
->th_dport
= htons(id
->dst_port
);
1268 tcp
->th_sport
= htons(id
->src_port
);
1270 tcp
->th_seq
= htonl(seq
);
1271 tcp
->th_ack
= htonl(ack
);
1272 tcp
->th_flags
= TH_ACK
;
1275 * set ip_len to the payload size so we can compute
1276 * the tcp checksum on the pseudoheader
1277 * XXX check this, could save a couple of words ?
1279 ip
->ip_len
= htons(sizeof(struct tcphdr
));
1280 tcp
->th_sum
= in_cksum(m
, m
->m_pkthdr
.len
);
1282 * now fill fields left out earlier
1284 ip
->ip_ttl
= ip_defttl
;
1285 ip
->ip_len
= m
->m_pkthdr
.len
;
1286 bzero (&sro
, sizeof (sro
));
1287 ip_rtaddr(ip
->ip_dst
, &sro
);
1288 m
->m_flags
|= M_SKIP_FIREWALL
;
1289 ip_output_list(m
, 0, NULL
, &sro
, 0, NULL
);
1295 * sends a reject message, consuming the mbuf passed as an argument.
1298 send_reject(struct ip_fw_args
*args
, int code
, int offset
, int ip_len
)
1301 if (code
!= ICMP_REJECT_RST
) { /* Send an ICMP unreach */
1302 /* We need the IP header in host order for icmp_error(). */
1303 if (args
->eh
!= NULL
) {
1304 struct ip
*ip
= mtod(args
->m
, struct ip
*);
1305 ip
->ip_len
= ntohs(ip
->ip_len
);
1306 ip
->ip_off
= ntohs(ip
->ip_off
);
1308 lck_mtx_unlock(ip_mutex
);
1309 icmp_error(args
->m
, ICMP_UNREACH
, code
, 0L, 0);
1310 lck_mtx_lock(ip_mutex
);
1311 } else if (offset
== 0 && args
->f_id
.proto
== IPPROTO_TCP
) {
1312 struct tcphdr
*const tcp
=
1313 L3HDR(struct tcphdr
, mtod(args
->m
, struct ip
*));
1314 if ( (tcp
->th_flags
& TH_RST
) == 0) {
1315 lck_mtx_unlock(ip_mutex
);
1316 send_pkt(&(args
->f_id
), ntohl(tcp
->th_seq
),
1318 tcp
->th_flags
| TH_RST
);
1319 lck_mtx_lock(ip_mutex
);
1329 * Given an ip_fw *, lookup_next_rule will return a pointer
1330 * to the next rule, which can be either the jump
1331 * target (for skipto instructions) or the next one in the list (in
1332 * all other cases including a missing jump target).
1333 * The result is also written in the "next_rule" field of the rule.
1334 * Backward jumps are not allowed, so start looking from the next
1337 * This never returns NULL -- in case we do not have an exact match,
1338 * the next rule is returned. When the ruleset is changed,
1339 * pointers are flushed so we are always correct.
1342 static struct ip_fw
*
1343 lookup_next_rule(struct ip_fw
*me
)
1345 struct ip_fw
*rule
= NULL
;
1348 /* look for action, in case it is a skipto */
1349 cmd
= ACTION_PTR(me
);
1350 if (cmd
->opcode
== O_LOG
)
1352 if ( cmd
->opcode
== O_SKIPTO
)
1353 for (rule
= me
->next
; rule
; rule
= rule
->next
)
1354 if (rule
->rulenum
>= cmd
->arg1
)
1356 if (rule
== NULL
) /* failure or not a skipto */
1358 me
->next_rule
= rule
;
1363 * The main check routine for the firewall.
1365 * All arguments are in args so we can modify them and return them
1366 * back to the caller.
1370 * args->m (in/out) The packet; we set to NULL when/if we nuke it.
1371 * Starts with the IP header.
1372 * args->eh (in) Mac header if present, or NULL for layer3 packet.
1373 * args->oif Outgoing interface, or NULL if packet is incoming.
1374 * The incoming interface is in the mbuf. (in)
1375 * args->divert_rule (in/out)
1376 * Skip up to the first rule past this rule number;
1377 * upon return, non-zero port number for divert or tee.
1379 * args->rule Pointer to the last matching rule (in/out)
1380 * args->next_hop Socket we are forwarding to (out).
1381 * args->f_id Addresses grabbed from the packet (out)
1385 * IP_FW_PORT_DENY_FLAG the packet must be dropped.
1386 * 0 The packet is to be accepted and routed normally OR
1387 * the packet was denied/rejected and has been dropped;
1388 * in the latter case, *m is equal to NULL upon return.
1389 * port Divert the packet to port, with these caveats:
1391 * - If IP_FW_PORT_TEE_FLAG is set, tee the packet instead
1392 * of diverting it (ie, 'ipfw tee').
1394 * - If IP_FW_PORT_DYNT_FLAG is set, interpret the lower
1395 * 16 bits as a dummynet pipe number instead of diverting
1399 ipfw_chk(struct ip_fw_args
*args
)
1402 * Local variables hold state during the processing of a packet.
1404 * IMPORTANT NOTE: to speed up the processing of rules, there
1405 * are some assumption on the values of the variables, which
1406 * are documented here. Should you change them, please check
1407 * the implementation of the various instructions to make sure
1408 * that they still work.
1410 * args->eh The MAC header. It is non-null for a layer2
1411 * packet, it is NULL for a layer-3 packet.
1413 * m | args->m Pointer to the mbuf, as received from the caller.
1414 * It may change if ipfw_chk() does an m_pullup, or if it
1415 * consumes the packet because it calls send_reject().
1416 * XXX This has to change, so that ipfw_chk() never modifies
1417 * or consumes the buffer.
1418 * ip is simply an alias of the value of m, and it is kept
1419 * in sync with it (the packet is supposed to start with
1422 struct mbuf
*m
= args
->m
;
1423 struct ip
*ip
= mtod(m
, struct ip
*);
1426 * oif | args->oif If NULL, ipfw_chk has been called on the
1427 * inbound path (ether_input, bdg_forward, ip_input).
1428 * If non-NULL, ipfw_chk has been called on the outbound path
1429 * (ether_output, ip_output).
1431 struct ifnet
*oif
= args
->oif
;
1433 struct ip_fw
*f
= NULL
; /* matching rule */
1437 * hlen The length of the IPv4 header.
1438 * hlen >0 means we have an IPv4 packet.
1440 u_int hlen
= 0; /* hlen >0 means we have an IP pkt */
1443 * offset The offset of a fragment. offset != 0 means that
1444 * we have a fragment at this offset of an IPv4 packet.
1445 * offset == 0 means that (if this is an IPv4 packet)
1446 * this is the first or only fragment.
1451 * Local copies of addresses. They are only valid if we have
1454 * proto The protocol. Set to 0 for non-ip packets,
1455 * or to the protocol read from the packet otherwise.
1456 * proto != 0 means that we have an IPv4 packet.
1458 * src_port, dst_port port numbers, in HOST format. Only
1459 * valid for TCP and UDP packets.
1461 * src_ip, dst_ip ip addresses, in NETWORK format.
1462 * Only valid for IPv4 packets.
1465 u_int16_t src_port
= 0, dst_port
= 0; /* NOTE: host format */
1466 struct in_addr src_ip
, dst_ip
; /* NOTE: network format */
1469 int dyn_dir
= MATCH_UNKNOWN
;
1470 ipfw_dyn_rule
*q
= NULL
;
1471 struct timeval timenow
;
1473 if (m
->m_flags
& M_SKIP_FIREWALL
) {
1474 return 0; /* accept */
1477 lck_mtx_lock(ipfw_mutex
);
1479 getmicrotime(&timenow
);
1481 * dyn_dir = MATCH_UNKNOWN when rules unchecked,
1482 * MATCH_NONE when checked and not matched (q = NULL),
1483 * MATCH_FORWARD or MATCH_REVERSE otherwise (q != NULL)
1486 pktlen
= m
->m_pkthdr
.len
;
1487 if (args
->eh
== NULL
|| /* layer 3 packet */
1488 ( m
->m_pkthdr
.len
>= sizeof(struct ip
) &&
1489 ntohs(args
->eh
->ether_type
) == ETHERTYPE_IP
))
1490 hlen
= ip
->ip_hl
<< 2;
1493 * Collect parameters into local variables for faster matching.
1495 if (hlen
== 0) { /* do not grab addresses for non-ip pkts */
1496 proto
= args
->f_id
.proto
= 0; /* mark f_id invalid */
1497 goto after_ip_checks
;
1500 proto
= args
->f_id
.proto
= ip
->ip_p
;
1501 src_ip
= ip
->ip_src
;
1502 dst_ip
= ip
->ip_dst
;
1503 if (args
->eh
!= NULL
) { /* layer 2 packets are as on the wire */
1504 offset
= ntohs(ip
->ip_off
) & IP_OFFMASK
;
1505 ip_len
= ntohs(ip
->ip_len
);
1507 offset
= ip
->ip_off
& IP_OFFMASK
;
1508 ip_len
= ip
->ip_len
;
1510 pktlen
= ip_len
< pktlen
? ip_len
: pktlen
;
1512 #define PULLUP_TO(len) \
1514 if ((m)->m_len < (len)) { \
1515 args->m = m = m_pullup(m, (len)); \
1517 goto pullup_failed; \
1518 ip = mtod(m, struct ip *); \
1528 PULLUP_TO(hlen
+ sizeof(struct tcphdr
));
1529 tcp
= L3HDR(struct tcphdr
, ip
);
1530 dst_port
= tcp
->th_dport
;
1531 src_port
= tcp
->th_sport
;
1532 args
->f_id
.flags
= tcp
->th_flags
;
1540 PULLUP_TO(hlen
+ sizeof(struct udphdr
));
1541 udp
= L3HDR(struct udphdr
, ip
);
1542 dst_port
= udp
->uh_dport
;
1543 src_port
= udp
->uh_sport
;
1548 PULLUP_TO(hlen
+ 4); /* type, code and checksum. */
1549 args
->f_id
.flags
= L3HDR(struct icmp
, ip
)->icmp_type
;
1558 args
->f_id
.src_ip
= ntohl(src_ip
.s_addr
);
1559 args
->f_id
.dst_ip
= ntohl(dst_ip
.s_addr
);
1560 args
->f_id
.src_port
= src_port
= ntohs(src_port
);
1561 args
->f_id
.dst_port
= dst_port
= ntohs(dst_port
);
1566 * Packet has already been tagged. Look for the next rule
1567 * to restart processing.
1569 * If fw_one_pass != 0 then just accept it.
1570 * XXX should not happen here, but optimized out in
1574 lck_mtx_unlock(ipfw_mutex
);
1578 f
= args
->rule
->next_rule
;
1580 f
= lookup_next_rule(args
->rule
);
1583 * Find the starting rule. It can be either the first
1584 * one, or the one after divert_rule if asked so.
1586 int skipto
= args
->divert_rule
;
1589 if (args
->eh
== NULL
&& skipto
!= 0) {
1590 if (skipto
>= IPFW_DEFAULT_RULE
) {
1591 lck_mtx_unlock(ipfw_mutex
);
1592 return(IP_FW_PORT_DENY_FLAG
); /* invalid */
1594 while (f
&& f
->rulenum
<= skipto
)
1596 if (f
== NULL
) { /* drop packet */
1597 lck_mtx_unlock(ipfw_mutex
);
1598 return(IP_FW_PORT_DENY_FLAG
);
1602 args
->divert_rule
= 0; /* reset to avoid confusion later */
1605 * Now scan the rules, and parse microinstructions for each rule.
1607 for (; f
; f
= f
->next
) {
1610 int skip_or
; /* skip rest of OR block */
1613 if (f
->reserved_1
== IPFW_RULE_INACTIVE
) {
1617 if (set_disable
& (1 << f
->set
) )
1621 for (l
= f
->cmd_len
, cmd
= f
->cmd
; l
> 0 ;
1622 l
-= cmdlen
, cmd
+= cmdlen
) {
1626 * check_body is a jump target used when we find a
1627 * CHECK_STATE, and need to jump to the body of
1632 cmdlen
= F_LEN(cmd
);
1634 * An OR block (insn_1 || .. || insn_n) has the
1635 * F_OR bit set in all but the last instruction.
1636 * The first match will set "skip_or", and cause
1637 * the following instructions to be skipped until
1638 * past the one with the F_OR bit clear.
1640 if (skip_or
) { /* skip this instruction */
1641 if ((cmd
->len
& F_OR
) == 0)
1642 skip_or
= 0; /* next one is good */
1645 match
= 0; /* set to 1 if we succeed */
1647 switch (cmd
->opcode
) {
1649 * The first set of opcodes compares the packet's
1650 * fields with some pattern, setting 'match' if a
1651 * match is found. At the end of the loop there is
1652 * logic to deal with F_NOT and F_OR flags associated
1660 printf("ipfw: opcode %d unimplemented\n",
1669 * We only check offset == 0 && proto != 0,
1670 * as this ensures that we have an IPv4
1671 * packet with the ports info.
1677 struct inpcbinfo
*pi
;
1681 if (proto
== IPPROTO_TCP
) {
1684 } else if (proto
== IPPROTO_UDP
) {
1691 in_pcblookup_hash(pi
,
1692 dst_ip
, htons(dst_port
),
1693 src_ip
, htons(src_port
),
1695 in_pcblookup_hash(pi
,
1696 src_ip
, htons(src_port
),
1697 dst_ip
, htons(dst_port
),
1700 if (pcb
== NULL
|| pcb
->inp_socket
== NULL
)
1702 #if __FreeBSD_version < 500034
1703 #define socheckuid(a,b) (kauth_cred_getuid((a)->so_cred) != (b))
1705 if (cmd
->opcode
== O_UID
) {
1708 (pcb
->inp_socket
->so_uid
== (uid_t
)((ipfw_insn_u32
*)cmd
)->d
[0]);
1710 !socheckuid(pcb
->inp_socket
,
1711 (uid_t
)((ipfw_insn_u32
*)cmd
)->d
[0]);
1717 kauth_cred_ismember_gid(pcb
->inp_socket
->so_cred
,
1718 (gid_t
)((ipfw_insn_u32
*)cmd
)->d
[0], &match
);
1726 match
= iface_match(m
->m_pkthdr
.rcvif
,
1727 (ipfw_insn_if
*)cmd
);
1731 match
= iface_match(oif
, (ipfw_insn_if
*)cmd
);
1735 match
= iface_match(oif
? oif
:
1736 m
->m_pkthdr
.rcvif
, (ipfw_insn_if
*)cmd
);
1740 if (args
->eh
!= NULL
) { /* have MAC header */
1741 u_int32_t
*want
= (u_int32_t
*)
1742 ((ipfw_insn_mac
*)cmd
)->addr
;
1743 u_int32_t
*mask
= (u_int32_t
*)
1744 ((ipfw_insn_mac
*)cmd
)->mask
;
1745 u_int32_t
*hdr
= (u_int32_t
*)args
->eh
;
1748 ( want
[0] == (hdr
[0] & mask
[0]) &&
1749 want
[1] == (hdr
[1] & mask
[1]) &&
1750 want
[2] == (hdr
[2] & mask
[2]) );
1755 if (args
->eh
!= NULL
) {
1757 ntohs(args
->eh
->ether_type
);
1759 ((ipfw_insn_u16
*)cmd
)->ports
;
1762 for (i
= cmdlen
- 1; !match
&& i
>0;
1764 match
= (t
>=p
[0] && t
<=p
[1]);
1769 match
= (hlen
> 0 && offset
!= 0);
1772 case O_IN
: /* "out" is "not in" */
1773 match
= (oif
== NULL
);
1777 match
= (args
->eh
!= NULL
);
1782 * We do not allow an arg of 0 so the
1783 * check of "proto" only suffices.
1785 match
= (proto
== cmd
->arg1
);
1789 match
= (hlen
> 0 &&
1790 ((ipfw_insn_ip
*)cmd
)->addr
.s_addr
==
1798 (cmd
->opcode
== O_IP_DST_MASK
) ?
1799 dst_ip
.s_addr
: src_ip
.s_addr
;
1800 uint32_t *p
= ((ipfw_insn_u32
*)cmd
)->d
;
1803 for (; !match
&& i
>0; i
-= 2, p
+= 2)
1804 match
= (p
[0] == (a
& p
[1]));
1812 INADDR_TO_IFP(src_ip
, tif
);
1813 match
= (tif
!= NULL
);
1820 u_int32_t
*d
= (u_int32_t
*)(cmd
+1);
1822 cmd
->opcode
== O_IP_DST_SET
?
1828 addr
-= d
[0]; /* subtract base */
1829 match
= (addr
< cmd
->arg1
) &&
1830 ( d
[ 1 + (addr
>>5)] &
1831 (1<<(addr
& 0x1f)) );
1836 match
= (hlen
> 0 &&
1837 ((ipfw_insn_ip
*)cmd
)->addr
.s_addr
==
1845 INADDR_TO_IFP(dst_ip
, tif
);
1846 match
= (tif
!= NULL
);
1853 * offset == 0 && proto != 0 is enough
1854 * to guarantee that we have an IPv4
1855 * packet with port info.
1857 if ((proto
==IPPROTO_UDP
|| proto
==IPPROTO_TCP
)
1860 (cmd
->opcode
== O_IP_SRCPORT
) ?
1861 src_port
: dst_port
;
1863 ((ipfw_insn_u16
*)cmd
)->ports
;
1866 for (i
= cmdlen
- 1; !match
&& i
>0;
1868 match
= (x
>=p
[0] && x
<=p
[1]);
1873 match
= (offset
== 0 && proto
==IPPROTO_ICMP
&&
1874 icmptype_match(ip
, (ipfw_insn_u32
*)cmd
) );
1878 match
= (hlen
> 0 && ipopts_match(ip
, cmd
) );
1882 match
= (hlen
> 0 && cmd
->arg1
== ip
->ip_v
);
1888 if (hlen
> 0) { /* only for IP packets */
1893 if (cmd
->opcode
== O_IPLEN
)
1895 else if (cmd
->opcode
== O_IPTTL
)
1897 else /* must be IPID */
1898 x
= ntohs(ip
->ip_id
);
1900 match
= (cmd
->arg1
== x
);
1903 /* otherwise we have ranges */
1904 p
= ((ipfw_insn_u16
*)cmd
)->ports
;
1906 for (; !match
&& i
>0; i
--, p
+= 2)
1907 match
= (x
>= p
[0] && x
<= p
[1]);
1911 case O_IPPRECEDENCE
:
1912 match
= (hlen
> 0 &&
1913 (cmd
->arg1
== (ip
->ip_tos
& 0xe0)) );
1917 match
= (hlen
> 0 &&
1918 flags_match(cmd
, ip
->ip_tos
));
1922 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1924 L3HDR(struct tcphdr
,ip
)->th_flags
));
1928 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1929 tcpopts_match(ip
, cmd
));
1933 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1934 ((ipfw_insn_u32
*)cmd
)->d
[0] ==
1935 L3HDR(struct tcphdr
,ip
)->th_seq
);
1939 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1940 ((ipfw_insn_u32
*)cmd
)->d
[0] ==
1941 L3HDR(struct tcphdr
,ip
)->th_ack
);
1945 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1947 L3HDR(struct tcphdr
,ip
)->th_win
);
1951 /* reject packets which have SYN only */
1952 /* XXX should i also check for TH_ACK ? */
1953 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1954 (L3HDR(struct tcphdr
,ip
)->th_flags
&
1955 (TH_RST
| TH_ACK
| TH_SYN
)) != TH_SYN
);
1960 ipfw_log(f
, hlen
, args
->eh
, m
, oif
);
1965 match
= (random()<((ipfw_insn_u32
*)cmd
)->d
[0]);
1969 /* Outgoing packets automatically pass/match */
1970 match
= ((oif
!= NULL
) ||
1971 (m
->m_pkthdr
.rcvif
== NULL
) ||
1972 verify_rev_path(src_ip
, m
->m_pkthdr
.rcvif
));
1977 match
= (m_tag_find(m
,
1978 PACKET_TAG_IPSEC_IN_DONE
, NULL
) != NULL
);
1981 match
= (ipsec_gethist(m
, NULL
) != NULL
);
1983 /* otherwise no match */
1987 * The second set of opcodes represents 'actions',
1988 * i.e. the terminal part of a rule once the packet
1989 * matches all previous patterns.
1990 * Typically there is only one action for each rule,
1991 * and the opcode is stored at the end of the rule
1992 * (but there are exceptions -- see below).
1994 * In general, here we set retval and terminate the
1995 * outer loop (would be a 'break 3' in some language,
1996 * but we need to do a 'goto done').
1999 * O_COUNT and O_SKIPTO actions:
2000 * instead of terminating, we jump to the next rule
2001 * ('goto next_rule', equivalent to a 'break 2'),
2002 * or to the SKIPTO target ('goto again' after
2003 * having set f, cmd and l), respectively.
2005 * O_LIMIT and O_KEEP_STATE: these opcodes are
2006 * not real 'actions', and are stored right
2007 * before the 'action' part of the rule.
2008 * These opcodes try to install an entry in the
2009 * state tables; if successful, we continue with
2010 * the next opcode (match=1; break;), otherwise
2011 * the packet * must be dropped
2012 * ('goto done' after setting retval);
2014 * O_PROBE_STATE and O_CHECK_STATE: these opcodes
2015 * cause a lookup of the state table, and a jump
2016 * to the 'action' part of the parent rule
2017 * ('goto check_body') if an entry is found, or
2018 * (CHECK_STATE only) a jump to the next rule if
2019 * the entry is not found ('goto next_rule').
2020 * The result of the lookup is cached to make
2021 * further instances of these opcodes are
2026 if (install_state(f
,
2027 (ipfw_insn_limit
*)cmd
, args
)) {
2028 retval
= IP_FW_PORT_DENY_FLAG
;
2029 goto done
; /* error/limit violation */
2037 * dynamic rules are checked at the first
2038 * keep-state or check-state occurrence,
2039 * with the result being stored in dyn_dir.
2040 * The compiler introduces a PROBE_STATE
2041 * instruction for us when we have a
2042 * KEEP_STATE (because PROBE_STATE needs
2045 if (dyn_dir
== MATCH_UNKNOWN
&&
2046 (q
= lookup_dyn_rule(&args
->f_id
,
2047 &dyn_dir
, proto
== IPPROTO_TCP
?
2048 L3HDR(struct tcphdr
, ip
) : NULL
))
2051 * Found dynamic entry, update stats
2052 * and jump to the 'action' part of
2058 cmd
= ACTION_PTR(f
);
2059 l
= f
->cmd_len
- f
->act_ofs
;
2063 * Dynamic entry not found. If CHECK_STATE,
2064 * skip to next rule, if PROBE_STATE just
2065 * ignore and continue with next opcode.
2067 if (cmd
->opcode
== O_CHECK_STATE
)
2073 retval
= 0; /* accept */
2078 args
->rule
= f
; /* report matching rule */
2079 retval
= cmd
->arg1
| IP_FW_PORT_DYNT_FLAG
;
2084 if (args
->eh
) /* not on layer 2 */
2086 args
->divert_rule
= f
->rulenum
;
2087 retval
= (cmd
->opcode
== O_DIVERT
) ?
2089 cmd
->arg1
| IP_FW_PORT_TEE_FLAG
;
2094 f
->pcnt
++; /* update stats */
2096 f
->timestamp
= timenow
.tv_sec
;
2097 if (cmd
->opcode
== O_COUNT
)
2100 if (f
->next_rule
== NULL
)
2101 lookup_next_rule(f
);
2107 * Drop the packet and send a reject notice
2108 * if the packet is not ICMP (or is an ICMP
2109 * query), and it is not multicast/broadcast.
2112 (proto
!= IPPROTO_ICMP
||
2113 is_icmp_query(ip
)) &&
2114 !(m
->m_flags
& (M_BCAST
|M_MCAST
)) &&
2115 !IN_MULTICAST(dst_ip
.s_addr
)) {
2116 send_reject(args
, cmd
->arg1
,
2122 retval
= IP_FW_PORT_DENY_FLAG
;
2126 if (args
->eh
) /* not valid on layer2 pkts */
2128 if (!q
|| dyn_dir
== MATCH_FORWARD
)
2130 &((ipfw_insn_sa
*)cmd
)->sa
;
2135 panic("-- unknown opcode %d\n", cmd
->opcode
);
2136 } /* end of switch() on opcodes */
2138 if (cmd
->len
& F_NOT
)
2142 if (cmd
->len
& F_OR
)
2145 if (!(cmd
->len
& F_OR
)) /* not an OR block, */
2146 break; /* try next rule */
2149 } /* end of inner for, scan opcodes */
2151 next_rule
:; /* try next rule */
2153 } /* end of outer for, scan rules */
2154 printf("ipfw: ouch!, skip past end of rules, denying packet\n");
2155 lck_mtx_unlock(ipfw_mutex
);
2156 return(IP_FW_PORT_DENY_FLAG
);
2159 /* Update statistics */
2162 f
->timestamp
= timenow
.tv_sec
;
2163 lck_mtx_unlock(ipfw_mutex
);
2168 printf("ipfw: pullup failed\n");
2169 lck_mtx_unlock(ipfw_mutex
);
2170 return(IP_FW_PORT_DENY_FLAG
);
2174 * When a rule is added/deleted, clear the next_rule pointers in all rules.
2175 * These will be reconstructed on the fly as packets are matched.
2176 * Must be called at splimp().
2179 flush_rule_ptrs(void)
2183 for (rule
= layer3_chain
; rule
; rule
= rule
->next
)
2184 rule
->next_rule
= NULL
;
2188 * When pipes/queues are deleted, clear the "pipe_ptr" pointer to a given
2189 * pipe/queue, or to all of them (match == NULL).
2190 * Must be called at splimp().
2193 flush_pipe_ptrs(struct dn_flow_set
*match
)
2197 for (rule
= layer3_chain
; rule
; rule
= rule
->next
) {
2198 ipfw_insn_pipe
*cmd
= (ipfw_insn_pipe
*)ACTION_PTR(rule
);
2200 if (cmd
->o
.opcode
!= O_PIPE
&& cmd
->o
.opcode
!= O_QUEUE
)
2203 * XXX Use bcmp/bzero to handle pipe_ptr to overcome
2204 * possible alignment problems on 64-bit architectures.
2205 * This code is seldom used so we do not worry too
2206 * much about efficiency.
2208 if (match
== NULL
||
2209 !bcmp(&cmd
->pipe_ptr
, &match
, sizeof(match
)) )
2210 bzero(&cmd
->pipe_ptr
, sizeof(cmd
->pipe_ptr
));
2215 * Add a new rule to the list. Copy the rule into a malloc'ed area, then
2216 * possibly create a rule number and add the rule to the list.
2217 * Update the rule_number in the input struct so the caller knows it as well.
2220 add_rule(struct ip_fw
**head
, struct ip_fw
*input_rule
)
2222 struct ip_fw
*rule
, *f
, *prev
;
2224 int l
= RULESIZE(input_rule
);
2226 if (*head
== NULL
&& input_rule
->rulenum
!= IPFW_DEFAULT_RULE
)
2229 rule
= _MALLOC(l
, M_IPFW
, M_WAIT
);
2231 printf("ipfw2: add_rule MALLOC failed\n");
2236 bcopy(input_rule
, rule
, l
);
2239 rule
->next_rule
= NULL
;
2243 rule
->timestamp
= 0;
2245 if (*head
== NULL
) { /* default rule */
2251 * If rulenum is 0, find highest numbered rule before the
2252 * default rule, and add autoinc_step
2254 if (autoinc_step
< 1)
2256 else if (autoinc_step
> 1000)
2257 autoinc_step
= 1000;
2258 if (rule
->rulenum
== 0) {
2260 * locate the highest numbered rule before default
2262 for (f
= *head
; f
; f
= f
->next
) {
2263 if (f
->rulenum
== IPFW_DEFAULT_RULE
)
2265 rule
->rulenum
= f
->rulenum
;
2267 if (rule
->rulenum
< IPFW_DEFAULT_RULE
- autoinc_step
)
2268 rule
->rulenum
+= autoinc_step
;
2269 input_rule
->rulenum
= rule
->rulenum
;
2273 * Now insert the new rule in the right place in the sorted list.
2275 for (prev
= NULL
, f
= *head
; f
; prev
= f
, f
= f
->next
) {
2276 if (f
->rulenum
> rule
->rulenum
) { /* found the location */
2280 } else { /* head insert */
2291 DEB(printf("ipfw: installed rule %d, static count now %d\n",
2292 rule
->rulenum
, static_count
);)
2297 * Free storage associated with a static rule (including derived
2299 * The caller is in charge of clearing rule pointers to avoid
2300 * dangling pointers.
2301 * @return a pointer to the next entry.
2302 * Arguments are not checked, so they better be correct.
2303 * Must be called at splimp().
2305 static struct ip_fw
*
2306 delete_rule(struct ip_fw
**head
, struct ip_fw
*prev
, struct ip_fw
*rule
)
2309 int l
= RULESIZE(rule
);
2312 remove_dyn_rule(rule
, NULL
/* force removal */);
2321 if (DUMMYNET_LOADED
)
2322 ip_dn_ruledel_ptr(rule
);
2323 #endif /* DUMMYNET */
2324 _FREE(rule
, M_IPFW
);
2328 #if DEBUG_INACTIVE_RULES
2330 print_chain(struct ip_fw
**chain
)
2332 struct ip_fw
*rule
= *chain
;
2334 for (; rule
; rule
= rule
->next
) {
2335 ipfw_insn
*cmd
= ACTION_PTR(rule
);
2337 printf("ipfw: rule->rulenum = %d\n", rule
->rulenum
);
2339 if (rule
->reserved_1
== IPFW_RULE_INACTIVE
) {
2340 printf("ipfw: rule->reserved = IPFW_RULE_INACTIVE\n");
2343 switch (cmd
->opcode
) {
2345 printf("ipfw: ACTION: Deny\n");
2349 if (cmd
->arg1
==ICMP_REJECT_RST
)
2350 printf("ipfw: ACTION: Reset\n");
2351 else if (cmd
->arg1
==ICMP_UNREACH_HOST
)
2352 printf("ipfw: ACTION: Reject\n");
2356 printf("ipfw: ACTION: Accept\n");
2359 printf("ipfw: ACTION: Count\n");
2362 printf("ipfw: ACTION: Divert\n");
2365 printf("ipfw: ACTION: Tee\n");
2368 printf("ipfw: ACTION: SkipTo\n");
2371 printf("ipfw: ACTION: Pipe\n");
2374 printf("ipfw: ACTION: Queue\n");
2377 printf("ipfw: ACTION: Forward\n");
2380 printf("ipfw: invalid action! %d\n", cmd
->opcode
);
2384 #endif /* DEBUG_INACTIVE_RULES */
2387 flush_inactive(void *param
)
2389 struct ip_fw
*inactive_rule
= (struct ip_fw
*)param
;
2390 struct ip_fw
*rule
, *prev
;
2392 lck_mtx_lock(ipfw_mutex
);
2394 for (rule
= layer3_chain
, prev
= NULL
; rule
; ) {
2395 if (rule
== inactive_rule
&& rule
->reserved_1
== IPFW_RULE_INACTIVE
) {
2396 struct ip_fw
*n
= rule
;
2399 layer3_chain
= rule
->next
;
2402 prev
->next
= rule
->next
;
2413 #if DEBUG_INACTIVE_RULES
2414 print_chain(&layer3_chain
);
2416 lck_mtx_unlock(ipfw_mutex
);
2420 mark_inactive(struct ip_fw
**prev
, struct ip_fw
**rule
)
2422 int l
= RULESIZE(*rule
);
2424 if ((*rule
)->reserved_1
!= IPFW_RULE_INACTIVE
) {
2425 (*rule
)->reserved_1
= IPFW_RULE_INACTIVE
;
2429 timeout(flush_inactive
, *rule
, 30*hz
); /* 30 sec. */
2433 *rule
= (*rule
)->next
;
2437 * Deletes all rules from a chain (except rules in set RESVD_SET
2438 * unless kill_default = 1).
2439 * Must be called at splimp().
2442 free_chain(struct ip_fw
**chain
, int kill_default
)
2444 struct ip_fw
*prev
, *rule
;
2446 flush_rule_ptrs(); /* more efficient to do outside the loop */
2447 for (prev
= NULL
, rule
= *chain
; rule
; )
2448 if (kill_default
|| rule
->set
!= RESVD_SET
) {
2449 ipfw_insn
*cmd
= ACTION_PTR(rule
);
2451 /* skip over forwarding rules so struct isn't
2452 * deleted while pointer is still in use elsewhere
2454 if (cmd
->opcode
== O_FORWARD_IP
) {
2455 mark_inactive(&prev
, &rule
);
2458 rule
= delete_rule(chain
, prev
, rule
);
2468 * Remove all rules with given number, and also do set manipulation.
2469 * Assumes chain != NULL && *chain != NULL.
2471 * The argument is an u_int32_t. The low 16 bit are the rule or set number,
2472 * the next 8 bits are the new set, the top 8 bits are the command:
2474 * 0 delete rules with given number
2475 * 1 delete rules with given set number
2476 * 2 move rules with given number to new set
2477 * 3 move rules with given set number to new set
2478 * 4 swap sets with given numbers
2481 del_entry(struct ip_fw
**chain
, u_int32_t arg
)
2483 struct ip_fw
*prev
= NULL
, *rule
= *chain
;
2485 u_int16_t rulenum
; /* rule or old_set */
2486 u_int8_t cmd
, new_set
;
2488 rulenum
= arg
& 0xffff;
2489 cmd
= (arg
>> 24) & 0xff;
2490 new_set
= (arg
>> 16) & 0xff;
2494 if (new_set
> RESVD_SET
)
2496 if (cmd
== 0 || cmd
== 2) {
2497 if (rulenum
>= IPFW_DEFAULT_RULE
)
2500 if (rulenum
> RESVD_SET
) /* old_set */
2505 case 0: /* delete rules with given number */
2507 * locate first rule to delete
2509 for (; rule
->rulenum
< rulenum
; prev
= rule
, rule
= rule
->next
)
2511 if (rule
->rulenum
!= rulenum
)
2515 * flush pointers outside the loop, then delete all matching
2516 * rules. prev remains the same throughout the cycle.
2519 while (rule
->rulenum
== rulenum
) {
2520 ipfw_insn
*cmd
= ACTION_PTR(rule
);
2522 /* keep forwarding rules around so struct isn't
2523 * deleted while pointer is still in use elsewhere
2525 if (cmd
->opcode
== O_FORWARD_IP
) {
2526 mark_inactive(&prev
, &rule
);
2529 rule
= delete_rule(chain
, prev
, rule
);
2534 case 1: /* delete all rules with given set number */
2536 while (rule
->rulenum
< IPFW_DEFAULT_RULE
) {
2537 if (rule
->set
== rulenum
) {
2538 ipfw_insn
*cmd
= ACTION_PTR(rule
);
2540 /* keep forwarding rules around so struct isn't
2541 * deleted while pointer is still in use elsewhere
2543 if (cmd
->opcode
== O_FORWARD_IP
) {
2544 mark_inactive(&prev
, &rule
);
2547 rule
= delete_rule(chain
, prev
, rule
);
2557 case 2: /* move rules with given number to new set */
2558 for (; rule
->rulenum
< IPFW_DEFAULT_RULE
; rule
= rule
->next
)
2559 if (rule
->rulenum
== rulenum
)
2560 rule
->set
= new_set
;
2563 case 3: /* move rules with given set number to new set */
2564 for (; rule
->rulenum
< IPFW_DEFAULT_RULE
; rule
= rule
->next
)
2565 if (rule
->set
== rulenum
)
2566 rule
->set
= new_set
;
2569 case 4: /* swap two sets */
2570 for (; rule
->rulenum
< IPFW_DEFAULT_RULE
; rule
= rule
->next
)
2571 if (rule
->set
== rulenum
)
2572 rule
->set
= new_set
;
2573 else if (rule
->set
== new_set
)
2574 rule
->set
= rulenum
;
2581 * Clear counters for a specific rule.
2584 clear_counters(struct ip_fw
*rule
, int log_only
)
2586 ipfw_insn_log
*l
= (ipfw_insn_log
*)ACTION_PTR(rule
);
2588 if (log_only
== 0) {
2589 rule
->bcnt
= rule
->pcnt
= 0;
2590 rule
->timestamp
= 0;
2592 if (l
->o
.opcode
== O_LOG
)
2593 l
->log_left
= l
->max_log
;
2597 * Reset some or all counters on firewall rules.
2598 * @arg frwl is null to clear all entries, or contains a specific
2600 * @arg log_only is 1 if we only want to reset logs, zero otherwise.
2603 zero_entry(int rulenum
, int log_only
)
2611 for (rule
= layer3_chain
; rule
; rule
= rule
->next
)
2612 clear_counters(rule
, log_only
);
2613 msg
= log_only
? "ipfw: All logging counts reset.\n" :
2614 "ipfw: Accounting cleared.\n";
2618 * We can have multiple rules with the same number, so we
2619 * need to clear them all.
2621 for (rule
= layer3_chain
; rule
; rule
= rule
->next
)
2622 if (rule
->rulenum
== rulenum
) {
2623 while (rule
&& rule
->rulenum
== rulenum
) {
2624 clear_counters(rule
, log_only
);
2630 if (!cleared
) /* we did not find any matching rules */
2632 msg
= log_only
? "ipfw: Entry %d logging count reset.\n" :
2633 "ipfw: Entry %d cleared.\n";
2637 dolog((LOG_AUTHPRIV
| LOG_NOTICE
, msg
, rulenum
));
2643 * Check validity of the structure before insert.
2644 * Fortunately rules are simple, so this mostly need to check rule sizes.
2647 check_ipfw_struct(struct ip_fw
*rule
, int size
)
2653 if (size
< sizeof(*rule
)) {
2654 printf("ipfw: rule too short\n");
2657 /* first, check for valid size */
2660 printf("ipfw: size mismatch (have %d want %d)\n", size
, l
);
2664 * Now go for the individual checks. Very simple ones, basically only
2665 * instruction sizes.
2667 for (l
= rule
->cmd_len
, cmd
= rule
->cmd
;
2668 l
> 0 ; l
-= cmdlen
, cmd
+= cmdlen
) {
2669 cmdlen
= F_LEN(cmd
);
2671 printf("ipfw: opcode %d size truncated\n",
2675 DEB(printf("ipfw: opcode %d\n", cmd
->opcode
);)
2676 switch (cmd
->opcode
) {
2687 case O_IPPRECEDENCE
:
2695 if (cmdlen
!= F_INSN_SIZE(ipfw_insn
))
2701 #endif /* __APPLE__ */
2708 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_u32
))
2713 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_limit
))
2718 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_log
))
2721 /* enforce logging limit */
2723 ((ipfw_insn_log
*)cmd
)->max_log
== 0 && verbose_limit
!= 0) {
2724 ((ipfw_insn_log
*)cmd
)->max_log
= verbose_limit
;
2727 ((ipfw_insn_log
*)cmd
)->log_left
=
2728 ((ipfw_insn_log
*)cmd
)->max_log
;
2734 /* only odd command lengths */
2735 if ( !(cmdlen
& 1) || cmdlen
> 31)
2741 if (cmd
->arg1
== 0 || cmd
->arg1
> 256) {
2742 printf("ipfw: invalid set size %d\n",
2746 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_u32
) +
2752 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_mac
))
2760 if (cmdlen
< 1 || cmdlen
> 31)
2766 case O_IP_DSTPORT
: /* XXX artificial limit, 30 port pairs */
2767 if (cmdlen
< 2 || cmdlen
> 31)
2774 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_if
))
2780 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_pipe
))
2785 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_sa
))
2789 case O_FORWARD_MAC
: /* XXX not implemented yet */
2798 if (cmdlen
!= F_INSN_SIZE(ipfw_insn
))
2802 printf("ipfw: opcode %d, multiple actions"
2809 printf("ipfw: opcode %d, action must be"
2816 printf("ipfw: opcode %d, unknown opcode\n",
2821 if (have_action
== 0) {
2822 printf("ipfw: missing action\n");
2828 printf("ipfw: opcode %d size %d wrong\n",
2829 cmd
->opcode
, cmdlen
);
2835 * {set|get}sockopt parser.
2838 ipfw_ctl(struct sockopt
*sopt
)
2840 #define RULE_MAXSIZE (256*sizeof(u_int32_t))
2841 u_int32_t api_version
;
2845 struct ip_fw
*bp
, *buf
, *rule
;
2847 /* copy of orig sopt to send to ipfw_get_command_and_version() */
2848 struct sockopt tmp_sopt
= *sopt
;
2849 struct timeval timenow
;
2851 getmicrotime(&timenow
);
2854 * Disallow modifications in really-really secure mode, but still allow
2855 * the logging counters to be reset.
2857 if (sopt
->sopt_name
== IP_FW_ADD
||
2858 (sopt
->sopt_dir
== SOPT_SET
&& sopt
->sopt_name
!= IP_FW_RESETLOG
)) {
2859 #if __FreeBSD_version >= 500034
2860 error
= securelevel_ge(sopt
->sopt_td
->td_ucred
, 3);
2863 #else /* FreeBSD 4.x */
2864 if (securelevel
>= 3)
2869 /* first get the command and version, then do conversion as necessary */
2870 error
= ipfw_get_command_and_version(&tmp_sopt
, &command
, &api_version
);
2873 /* error getting the version */
2880 * pass up a copy of the current rules. Static rules
2881 * come first (the last of which has number IPFW_DEFAULT_RULE),
2882 * followed by a possibly empty list of dynamic rule.
2883 * The last dynamic rule has NULL in the "next" field.
2885 lck_mtx_lock(ipfw_mutex
);
2886 size
= static_len
; /* size of static rules */
2887 if (ipfw_dyn_v
) /* add size of dyn.rules */
2888 size
+= (dyn_count
* sizeof(ipfw_dyn_rule
));
2891 * XXX todo: if the user passes a short length just to know
2892 * how much room is needed, do not bother filling up the
2893 * buffer, just jump to the sooptcopyout.
2895 buf
= _MALLOC(size
, M_TEMP
, M_WAITOK
);
2897 lck_mtx_unlock(ipfw_mutex
);
2905 for (rule
= layer3_chain
; rule
; rule
= rule
->next
) {
2906 int i
= RULESIZE(rule
);
2908 if (rule
->reserved_1
== IPFW_RULE_INACTIVE
) {
2912 bcopy(&set_disable
, &(bp
->next_rule
),
2913 sizeof(set_disable
));
2914 bp
= (struct ip_fw
*)((char *)bp
+ i
);
2918 ipfw_dyn_rule
*p
, *dst
, *last
= NULL
;
2920 dst
= (ipfw_dyn_rule
*)bp
;
2921 for (i
= 0 ; i
< curr_dyn_buckets
; i
++ )
2922 for ( p
= ipfw_dyn_v
[i
] ; p
!= NULL
;
2923 p
= p
->next
, dst
++ ) {
2924 bcopy(p
, dst
, sizeof *p
);
2925 bcopy(&(p
->rule
->rulenum
), &(dst
->rule
),
2926 sizeof(p
->rule
->rulenum
));
2928 * store a non-null value in "next".
2929 * The userland code will interpret a
2930 * NULL here as a marker
2931 * for the last dynamic rule.
2933 bcopy(&dst
, &dst
->next
, sizeof(dst
));
2936 TIME_LEQ(dst
->expire
, timenow
.tv_sec
) ?
2937 0 : dst
->expire
- timenow
.tv_sec
;
2939 if (last
!= NULL
) /* mark last dynamic rule */
2940 bzero(&last
->next
, sizeof(last
));
2942 lck_mtx_unlock(ipfw_mutex
);
2944 /* convert back if necessary and copyout */
2945 if (api_version
== IP_FW_VERSION_0
) {
2947 struct ip_old_fw
*buf2
, *rule_vers0
;
2949 buf2
= _MALLOC(static_count
* sizeof(struct ip_old_fw
), M_TEMP
, M_WAITOK
);
2958 for (i
= 0; i
< static_count
; i
++) {
2959 /* static rules have different sizes */
2960 int j
= RULESIZE(bp
);
2961 ipfw_convert_from_latest(bp
, rule_vers0
, api_version
);
2962 bp
= (struct ip_fw
*)((char *)bp
+ j
);
2963 len
+= sizeof(*rule_vers0
);
2966 error
= sooptcopyout(sopt
, buf2
, len
);
2967 _FREE(buf2
, M_TEMP
);
2969 } else if (api_version
== IP_FW_VERSION_1
) {
2970 int i
, len
= 0, buf_size
;
2971 struct ip_fw_compat
*buf2
, *rule_vers1
;
2972 struct ipfw_dyn_rule_compat
*dyn_rule_vers1
, *dyn_last
= NULL
;
2975 buf_size
= static_count
* sizeof(struct ip_fw_compat
) +
2976 dyn_count
* sizeof(struct ipfw_dyn_rule_compat
);
2978 buf2
= _MALLOC(buf_size
, M_TEMP
, M_WAITOK
);
2987 /* first do static rules */
2988 for (i
= 0; i
< static_count
; i
++) {
2989 /* static rules have different sizes */
2990 int j
= RULESIZE(bp
);
2991 ipfw_convert_from_latest(bp
, rule_vers1
, api_version
);
2992 bp
= (struct ip_fw
*)((char *)bp
+ j
);
2993 len
+= sizeof(*rule_vers1
);
2997 /* now do dynamic rules */
2998 dyn_rule_vers1
= (struct ipfw_dyn_rule_compat
*)rule_vers1
;
3000 for (i
= 0; i
< curr_dyn_buckets
; i
++) {
3001 for ( p
= ipfw_dyn_v
[i
] ; p
!= NULL
; p
= p
->next
) {
3002 (int) dyn_rule_vers1
->chain
= p
->rule
->rulenum
;
3003 dyn_rule_vers1
->id
= p
->id
;
3004 dyn_rule_vers1
->mask
= p
->id
;
3005 dyn_rule_vers1
->type
= p
->dyn_type
;
3006 dyn_rule_vers1
->expire
= p
->expire
;
3007 dyn_rule_vers1
->pcnt
= p
->pcnt
;
3008 dyn_rule_vers1
->bcnt
= p
->bcnt
;
3009 dyn_rule_vers1
->bucket
= p
->bucket
;
3010 dyn_rule_vers1
->state
= p
->state
;
3012 dyn_rule_vers1
->next
= dyn_rule_vers1
;
3013 dyn_last
= dyn_rule_vers1
;
3015 len
+= sizeof(*dyn_rule_vers1
);
3020 if (dyn_last
!= NULL
) {
3021 dyn_last
->next
= NULL
;
3025 error
= sooptcopyout(sopt
, buf2
, len
);
3026 _FREE(buf2
, M_TEMP
);
3029 error
= sooptcopyout(sopt
, buf
, size
);
3037 * Normally we cannot release the lock on each iteration.
3038 * We could do it here only because we start from the head all
3039 * the times so there is no risk of missing some entries.
3040 * On the other hand, the risk is that we end up with
3041 * a very inconsistent ruleset, so better keep the lock
3042 * around the whole cycle.
3044 * XXX this code can be improved by resetting the head of
3045 * the list to point to the default rule, and then freeing
3046 * the old list without the need for a lock.
3049 lck_mtx_lock(ipfw_mutex
);
3050 free_chain(&layer3_chain
, 0 /* keep default rule */);
3051 #if DEBUG_INACTIVE_RULES
3052 print_chain(&layer3_chain
);
3054 lck_mtx_unlock(ipfw_mutex
);
3058 rule
= _MALLOC(RULE_MAXSIZE
, M_TEMP
, M_WAITOK
);
3064 bzero(rule
, RULE_MAXSIZE
);
3066 if (api_version
!= IP_FW_CURRENT_API_VERSION
) {
3067 error
= ipfw_convert_to_latest(sopt
, rule
, api_version
);
3070 error
= sooptcopyin(sopt
, rule
, RULE_MAXSIZE
,
3071 sizeof(struct ip_fw
) );
3075 if ((api_version
== IP_FW_VERSION_0
) || (api_version
== IP_FW_VERSION_1
)) {
3076 /* the rule has already been checked so just
3077 * adjust sopt_valsize to match what would be expected.
3079 sopt
->sopt_valsize
= RULESIZE(rule
);
3081 error
= check_ipfw_struct(rule
, sopt
->sopt_valsize
);
3083 lck_mtx_lock(ipfw_mutex
);
3084 error
= add_rule(&layer3_chain
, rule
);
3085 lck_mtx_unlock(ipfw_mutex
);
3087 size
= RULESIZE(rule
);
3088 if (!error
&& sopt
->sopt_dir
== SOPT_GET
) {
3089 /* convert back if necessary and copyout */
3090 if (api_version
== IP_FW_VERSION_0
) {
3091 struct ip_old_fw rule_vers0
;
3093 ipfw_convert_from_latest(rule
, &rule_vers0
, api_version
);
3094 sopt
->sopt_valsize
= sizeof(struct ip_old_fw
);
3096 error
= sooptcopyout(sopt
, &rule_vers0
, sizeof(struct ip_old_fw
));
3097 } else if (api_version
== IP_FW_VERSION_1
) {
3098 struct ip_fw_compat rule_vers1
;
3100 ipfw_convert_from_latest(rule
, &rule_vers1
, api_version
);
3101 sopt
->sopt_valsize
= sizeof(struct ip_fw_compat
);
3103 error
= sooptcopyout(sopt
, &rule_vers1
, sizeof(struct ip_fw_compat
));
3105 error
= sooptcopyout(sopt
, rule
, size
);
3111 _FREE(rule
, M_TEMP
);
3117 * IP_FW_DEL is used for deleting single rules or sets,
3118 * and (ab)used to atomically manipulate sets.
3119 * rule->set_masks is used to distinguish between the two:
3120 * rule->set_masks[0] == 0
3121 * delete single rule or set of rules,
3122 * or reassign rules (or sets) to a different set.
3123 * rule->set_masks[0] != 0
3124 * atomic disable/enable sets.
3125 * rule->set_masks[0] contains sets to be disabled,
3126 * rule->set_masks[1] contains sets to be enabled.
3128 /* there is only a simple rule passed in
3129 * (no cmds), so use a temp struct to copy
3131 struct ip_fw temp_rule
= { 0 };
3133 if (api_version
!= IP_FW_CURRENT_API_VERSION
) {
3134 error
= ipfw_convert_to_latest(sopt
, &temp_rule
, api_version
);
3137 error
= sooptcopyin(sopt
, &temp_rule
, sizeof(struct ip_fw
),
3138 sizeof(struct ip_fw
) );
3142 /* set_masks is used to distinguish between deleting
3143 * single rules or atomically manipulating sets
3145 lck_mtx_lock(ipfw_mutex
);
3147 if (temp_rule
.set_masks
[0] != 0) {
3148 /* set manipulation */
3150 (set_disable
| temp_rule
.set_masks
[0]) & ~temp_rule
.set_masks
[1] &
3151 ~(1<<RESVD_SET
); /* set RESVD_SET always enabled */
3155 error
= del_entry(&layer3_chain
, temp_rule
.rulenum
);
3156 #if DEBUG_INACTIVE_RULES
3157 print_chain(&layer3_chain
);
3162 lck_mtx_unlock(ipfw_mutex
);
3167 case IP_FW_RESETLOG
: /* using rule->rulenum */
3169 /* there is only a simple rule passed in
3170 * (no cmds), so use a temp struct to copy
3172 struct ip_fw temp_rule
= { 0 };
3174 if (api_version
!= IP_FW_CURRENT_API_VERSION
) {
3175 error
= ipfw_convert_to_latest(sopt
, &temp_rule
, api_version
);
3178 if (sopt
->sopt_val
!= 0) {
3179 error
= sooptcopyin(sopt
, &temp_rule
, sizeof(struct ip_fw
),
3180 sizeof(struct ip_fw
) );
3185 lck_mtx_lock(ipfw_mutex
);
3186 error
= zero_entry(temp_rule
.rulenum
, sopt
->sopt_name
== IP_FW_RESETLOG
);
3187 lck_mtx_unlock(ipfw_mutex
);
3192 printf("ipfw: ipfw_ctl invalid option %d\n", sopt
->sopt_name
);
3200 * dummynet needs a reference to the default rule, because rules can be
3201 * deleted while packets hold a reference to them. When this happens,
3202 * dummynet changes the reference to the default rule (it could well be a
3203 * NULL pointer, but this way we do not need to check for the special
3204 * case, plus here he have info on the default behaviour).
3206 struct ip_fw
*ip_fw_default_rule
;
3209 * This procedure is only used to handle keepalives. It is invoked
3210 * every dyn_keepalive_period
3213 ipfw_tick(void * __unused unused
)
3218 struct timeval timenow
;
3221 if (dyn_keepalive
== 0 || ipfw_dyn_v
== NULL
|| dyn_count
== 0)
3224 getmicrotime(&timenow
);
3226 lck_mtx_lock(ipfw_mutex
);
3227 for (i
= 0 ; i
< curr_dyn_buckets
; i
++) {
3228 for (q
= ipfw_dyn_v
[i
] ; q
; q
= q
->next
) {
3229 if (q
->dyn_type
== O_LIMIT_PARENT
)
3231 if (q
->id
.proto
!= IPPROTO_TCP
)
3233 if ( (q
->state
& BOTH_SYN
) != BOTH_SYN
)
3235 if (TIME_LEQ( timenow
.tv_sec
+dyn_keepalive_interval
,
3237 continue; /* too early */
3238 if (TIME_LEQ(q
->expire
, timenow
.tv_sec
))
3239 continue; /* too late, rule expired */
3241 send_pkt(&(q
->id
), q
->ack_rev
- 1, q
->ack_fwd
, TH_SYN
);
3242 send_pkt(&(q
->id
), q
->ack_fwd
- 1, q
->ack_rev
, 0);
3245 lck_mtx_unlock(ipfw_mutex
);
3247 timeout(ipfw_tick
, NULL
, dyn_keepalive_period
*hz
);
3253 struct ip_fw default_rule
;
3256 ipfw_mutex_grp_attr
= lck_grp_attr_alloc_init();
3257 ipfw_mutex_grp
= lck_grp_alloc_init("ipfw", ipfw_mutex_grp_attr
);
3258 ipfw_mutex_attr
= lck_attr_alloc_init();
3259 lck_attr_setdefault(ipfw_mutex_attr
);
3261 if ((ipfw_mutex
= lck_mtx_alloc_init(ipfw_mutex_grp
, ipfw_mutex_attr
)) == NULL
) {
3262 printf("ipfw_init: can't alloc ipfw_mutex\n");
3266 layer3_chain
= NULL
;
3268 bzero(&default_rule
, sizeof default_rule
);
3270 default_rule
.act_ofs
= 0;
3271 default_rule
.rulenum
= IPFW_DEFAULT_RULE
;
3272 default_rule
.cmd_len
= 1;
3273 default_rule
.set
= RESVD_SET
;
3275 default_rule
.cmd
[0].len
= 1;
3276 default_rule
.cmd
[0].opcode
=
3277 #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT
3282 if (add_rule(&layer3_chain
, &default_rule
)) {
3283 printf("ipfw2: add_rule failed adding default rule\n");
3284 printf("ipfw2 failed initialization!!\n");
3288 ip_fw_default_rule
= layer3_chain
;
3290 /* Radar 3920649, don't print unncessary messages to the log */
3291 printf("ipfw2 initialized, divert %s, "
3292 "rule-based forwarding enabled, default to %s, logging ",
3298 default_rule
.cmd
[0].opcode
== O_ACCEPT
? "accept" : "deny");
3301 #ifdef IPFIREWALL_VERBOSE
3304 #ifdef IPFIREWALL_VERBOSE_LIMIT
3305 verbose_limit
= IPFIREWALL_VERBOSE_LIMIT
;
3307 if (fw_verbose
== 0)
3308 printf("disabled\n");
3309 else if (verbose_limit
== 0)
3310 printf("unlimited\n");
3312 printf("limited to %d packets/entry by default\n",
3316 ip_fw_chk_ptr
= ipfw_chk
;
3317 ip_fw_ctl_ptr
= ipfw_ctl
;
3319 ipfwstringlen
= strlen( ipfwstring
);
3321 timeout(ipfw_tick
, NULL
, hz
);