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 static ip_fw_chk_t ipfw_chk
;
244 lck_grp_t
*ipfw_mutex_grp
;
245 lck_grp_attr_t
*ipfw_mutex_grp_attr
;
246 lck_attr_t
*ipfw_mutex_attr
;
247 lck_mtx_t
*ipfw_mutex
;
249 extern void ipfwsyslog( int level
, char *format
,...);
252 ip_dn_ruledel_t
*ip_dn_ruledel_ptr
= NULL
; /* hook into dummynet */
253 #endif /* DUMMYNET */
255 #define KEV_LOG_SUBCLASS 10
256 #define IPFWLOGEVENT 0
258 #define ipfwstring "ipfw:"
259 static size_t ipfwstringlen
;
261 #define dolog( a ) { \
262 if ( fw_verbose == 2 ) /* Apple logging, log to ipfw.log */ \
267 void ipfwsyslog( int level
, char *format
,...)
271 struct kev_msg ev_msg
;
273 char msgBuf
[msgsize
];
278 va_start( ap
, format
);
279 loglen
= vsnprintf(msgBuf
, msgsize
, format
, ap
);
282 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
283 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
284 ev_msg
.kev_subclass
= KEV_LOG_SUBCLASS
;
285 ev_msg
.event_code
= IPFWLOGEVENT
;
287 /* get rid of the trailing \n */
290 pri
= LOG_PRI(level
);
292 /* remove "ipfw:" prefix if logging to ipfw log */
293 if ( !(strncmp( ipfwstring
, msgBuf
, ipfwstringlen
))){
294 dptr
= msgBuf
+ipfwstringlen
;
297 ev_msg
.dv
[0].data_ptr
= &pri
;
298 ev_msg
.dv
[0].data_length
= 1;
299 ev_msg
.dv
[1].data_ptr
= dptr
;
300 ev_msg
.dv
[1].data_length
= 100; /* bug in kern_post_msg, it can't handle size > 256-msghdr */
301 ev_msg
.dv
[2].data_length
= 0;
303 kev_post_msg(&ev_msg
);
307 * This macro maps an ip pointer into a layer3 header pointer of type T
309 #define L3HDR(T, ip) ((T *)((u_int32_t *)(ip) + (ip)->ip_hl))
312 icmptype_match(struct ip
*ip
, ipfw_insn_u32
*cmd
)
314 int type
= L3HDR(struct icmp
,ip
)->icmp_type
;
316 return (type
<= ICMP_MAXTYPE
&& (cmd
->d
[0] & (1<<type
)) );
319 #define TT ( (1 << ICMP_ECHO) | (1 << ICMP_ROUTERSOLICIT) | \
320 (1 << ICMP_TSTAMP) | (1 << ICMP_IREQ) | (1 << ICMP_MASKREQ) )
323 is_icmp_query(struct ip
*ip
)
325 int type
= L3HDR(struct icmp
, ip
)->icmp_type
;
326 return (type
<= ICMP_MAXTYPE
&& (TT
& (1<<type
)) );
331 * The following checks use two arrays of 8 or 16 bits to store the
332 * bits that we want set or clear, respectively. They are in the
333 * low and high half of cmd->arg1 or cmd->d[0].
335 * We scan options and store the bits we find set. We succeed if
337 * (want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear
339 * The code is sometimes optimized not to store additional variables.
343 flags_match(ipfw_insn
*cmd
, u_int8_t bits
)
348 if ( ((cmd
->arg1
& 0xff) & bits
) != 0)
349 return 0; /* some bits we want set were clear */
350 want_clear
= (cmd
->arg1
>> 8) & 0xff;
351 if ( (want_clear
& bits
) != want_clear
)
352 return 0; /* some bits we want clear were set */
357 ipopts_match(struct ip
*ip
, ipfw_insn
*cmd
)
359 int optlen
, bits
= 0;
360 u_char
*cp
= (u_char
*)(ip
+ 1);
361 int x
= (ip
->ip_hl
<< 2) - sizeof (struct ip
);
363 for (; x
> 0; x
-= optlen
, cp
+= optlen
) {
364 int opt
= cp
[IPOPT_OPTVAL
];
366 if (opt
== IPOPT_EOL
)
368 if (opt
== IPOPT_NOP
)
371 optlen
= cp
[IPOPT_OLEN
];
372 if (optlen
<= 0 || optlen
> x
)
373 return 0; /* invalid or truncated */
381 bits
|= IP_FW_IPOPT_LSRR
;
385 bits
|= IP_FW_IPOPT_SSRR
;
389 bits
|= IP_FW_IPOPT_RR
;
393 bits
|= IP_FW_IPOPT_TS
;
397 return (flags_match(cmd
, bits
));
401 tcpopts_match(struct ip
*ip
, ipfw_insn
*cmd
)
403 int optlen
, bits
= 0;
404 struct tcphdr
*tcp
= L3HDR(struct tcphdr
,ip
);
405 u_char
*cp
= (u_char
*)(tcp
+ 1);
406 int x
= (tcp
->th_off
<< 2) - sizeof(struct tcphdr
);
408 for (; x
> 0; x
-= optlen
, cp
+= optlen
) {
410 if (opt
== TCPOPT_EOL
)
412 if (opt
== TCPOPT_NOP
)
426 bits
|= IP_FW_TCPOPT_MSS
;
430 bits
|= IP_FW_TCPOPT_WINDOW
;
433 case TCPOPT_SACK_PERMITTED
:
435 bits
|= IP_FW_TCPOPT_SACK
;
438 case TCPOPT_TIMESTAMP
:
439 bits
|= IP_FW_TCPOPT_TS
;
445 bits
|= IP_FW_TCPOPT_CC
;
449 return (flags_match(cmd
, bits
));
453 iface_match(struct ifnet
*ifp
, ipfw_insn_if
*cmd
)
455 if (ifp
== NULL
) /* no iface with this packet, match fails */
457 /* Check by name or by IP address */
458 if (cmd
->name
[0] != '\0') { /* match by name */
459 /* Check unit number (-1 is wildcard) */
460 if (cmd
->p
.unit
!= -1 && cmd
->p
.unit
!= ifp
->if_unit
)
463 if (!strncmp(ifp
->if_name
, cmd
->name
, IFNAMSIZ
))
468 ifnet_lock_shared(ifp
);
469 TAILQ_FOREACH(ia
, &ifp
->if_addrhead
, ifa_link
) {
470 if (ia
->ifa_addr
== NULL
)
472 if (ia
->ifa_addr
->sa_family
!= AF_INET
)
474 if (cmd
->p
.ip
.s_addr
== ((struct sockaddr_in
*)
475 (ia
->ifa_addr
))->sin_addr
.s_addr
) {
476 ifnet_lock_done(ifp
);
477 return(1); /* match */
480 ifnet_lock_done(ifp
);
482 return(0); /* no match, fail ... */
486 * The 'verrevpath' option checks that the interface that an IP packet
487 * arrives on is the same interface that traffic destined for the
488 * packet's source address would be routed out of. This is a measure
489 * to block forged packets. This is also commonly known as "anti-spoofing"
490 * or Unicast Reverse Path Forwarding (Unicast RFP) in Cisco-ese. The
491 * name of the knob is purposely reminisent of the Cisco IOS command,
493 * ip verify unicast reverse-path
495 * which implements the same functionality. But note that syntax is
496 * misleading. The check may be performed on all IP packets whether unicast,
497 * multicast, or broadcast.
500 verify_rev_path(struct in_addr src
, struct ifnet
*ifp
)
502 static struct route ro
;
503 struct sockaddr_in
*dst
;
505 dst
= (struct sockaddr_in
*)&(ro
.ro_dst
);
507 /* Check if we've cached the route from the previous call. */
508 if (src
.s_addr
!= dst
->sin_addr
.s_addr
) {
511 bzero(dst
, sizeof(*dst
));
512 dst
->sin_family
= AF_INET
;
513 dst
->sin_len
= sizeof(*dst
);
516 rtalloc_ign(&ro
, RTF_CLONING
|RTF_PRCLONING
);
519 if ((ro
.ro_rt
== NULL
) || (ifp
== NULL
) ||
520 (ro
.ro_rt
->rt_ifp
->if_index
!= ifp
->if_index
))
527 static u_int64_t norule_counter
; /* counter for ipfw_log(NULL...) */
529 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
530 #define SNP(buf) buf, sizeof(buf)
533 * We enter here when we have a rule with O_LOG.
534 * XXX this function alone takes about 2Kbytes of code!
537 ipfw_log(struct ip_fw
*f
, u_int hlen
, struct ether_header
*eh
,
538 struct mbuf
*m
, struct ifnet
*oif
)
541 int limit_reached
= 0;
542 char ipv4str
[MAX_IPv4_STR_LEN
];
543 char action2
[40], proto
[48], fragment
[28];
548 if (f
== NULL
) { /* bogus pkt */
549 if (verbose_limit
!= 0 && norule_counter
>= verbose_limit
)
552 if (norule_counter
== verbose_limit
)
553 limit_reached
= verbose_limit
;
555 } else { /* O_LOG is the first action, find the real one */
556 ipfw_insn
*cmd
= ACTION_PTR(f
);
557 ipfw_insn_log
*l
= (ipfw_insn_log
*)cmd
;
559 if (l
->max_log
!= 0 && l
->log_left
== 0)
562 if (l
->log_left
== 0)
563 limit_reached
= l
->max_log
;
564 cmd
+= F_LEN(cmd
); /* point to first action */
565 if (cmd
->opcode
== O_PROB
)
569 switch (cmd
->opcode
) {
575 if (cmd
->arg1
==ICMP_REJECT_RST
)
577 else if (cmd
->arg1
==ICMP_UNREACH_HOST
)
580 snprintf(SNPARGS(action2
, 0), "Unreach %d",
591 snprintf(SNPARGS(action2
, 0), "Divert %d",
595 snprintf(SNPARGS(action2
, 0), "Tee %d",
599 snprintf(SNPARGS(action2
, 0), "SkipTo %d",
603 snprintf(SNPARGS(action2
, 0), "Pipe %d",
607 snprintf(SNPARGS(action2
, 0), "Queue %d",
611 ipfw_insn_sa
*sa
= (ipfw_insn_sa
*)cmd
;
614 if (f
->reserved_1
== IPFW_RULE_INACTIVE
) {
617 len
= snprintf(SNPARGS(action2
, 0), "Forward to %s",
618 inet_ntop(AF_INET
, &sa
->sa
.sin_addr
, ipv4str
, sizeof(ipv4str
)));
620 snprintf(SNPARGS(action2
, len
), ":%d",
630 if (hlen
== 0) { /* non-ip */
631 snprintf(SNPARGS(proto
, 0), "MAC");
633 struct ip
*ip
= mtod(m
, struct ip
*);
634 /* these three are all aliases to the same thing */
635 struct icmp
*const icmp
= L3HDR(struct icmp
, ip
);
636 struct tcphdr
*const tcp
= (struct tcphdr
*)icmp
;
637 struct udphdr
*const udp
= (struct udphdr
*)icmp
;
639 int ip_off
, offset
, ip_len
;
643 if (eh
!= NULL
) { /* layer 2 packets are as on the wire */
644 ip_off
= ntohs(ip
->ip_off
);
645 ip_len
= ntohs(ip
->ip_len
);
650 offset
= ip_off
& IP_OFFMASK
;
653 len
= snprintf(SNPARGS(proto
, 0), "TCP %s",
654 inet_ntop(AF_INET
, &ip
->ip_src
, ipv4str
, sizeof(ipv4str
)));
656 snprintf(SNPARGS(proto
, len
), ":%d %s:%d",
657 ntohs(tcp
->th_sport
),
658 inet_ntop(AF_INET
, &ip
->ip_dst
, ipv4str
, sizeof(ipv4str
)),
659 ntohs(tcp
->th_dport
));
661 snprintf(SNPARGS(proto
, len
), " %s",
662 inet_ntop(AF_INET
, &ip
->ip_dst
, ipv4str
, sizeof(ipv4str
)));
666 len
= snprintf(SNPARGS(proto
, 0), "UDP %s",
667 inet_ntop(AF_INET
, &ip
->ip_src
, ipv4str
, sizeof(ipv4str
)));
669 snprintf(SNPARGS(proto
, len
), ":%d %s:%d",
670 ntohs(udp
->uh_sport
),
671 inet_ntop(AF_INET
, &ip
->ip_dst
, ipv4str
, sizeof(ipv4str
)),
672 ntohs(udp
->uh_dport
));
674 snprintf(SNPARGS(proto
, len
), " %s",
675 inet_ntop(AF_INET
, &ip
->ip_dst
, ipv4str
, sizeof(ipv4str
)));
680 len
= snprintf(SNPARGS(proto
, 0),
682 icmp
->icmp_type
, icmp
->icmp_code
);
684 len
= snprintf(SNPARGS(proto
, 0), "ICMP ");
685 len
+= snprintf(SNPARGS(proto
, len
), "%s",
686 inet_ntop(AF_INET
, &ip
->ip_src
, ipv4str
, sizeof(ipv4str
)));
687 snprintf(SNPARGS(proto
, len
), " %s",
688 inet_ntop(AF_INET
, &ip
->ip_dst
, ipv4str
, sizeof(ipv4str
)));
692 len
= snprintf(SNPARGS(proto
, 0), "P:%d %s", ip
->ip_p
,
693 inet_ntop(AF_INET
, &ip
->ip_src
, ipv4str
, sizeof(ipv4str
)));
694 snprintf(SNPARGS(proto
, len
), " %s",
695 inet_ntop(AF_INET
, &ip
->ip_dst
, ipv4str
, sizeof(ipv4str
)));
699 if (ip_off
& (IP_MF
| IP_OFFMASK
))
700 snprintf(SNPARGS(fragment
, 0), " (frag %d:%d@%d%s)",
701 ntohs(ip
->ip_id
), ip_len
- (ip
->ip_hl
<< 2),
703 (ip_off
& IP_MF
) ? "+" : "");
705 if (oif
|| m
->m_pkthdr
.rcvif
)
707 dolog((LOG_AUTHPRIV
| LOG_INFO
,
708 "ipfw: %d %s %s %s via %s%d%s\n",
710 action
, proto
, oif
? "out" : "in",
711 oif
? oif
->if_name
: m
->m_pkthdr
.rcvif
->if_name
,
712 oif
? oif
->if_unit
: m
->m_pkthdr
.rcvif
->if_unit
,
716 dolog((LOG_AUTHPRIV
| LOG_INFO
,
717 "ipfw: %d %s %s [no if info]%s\n",
719 action
, proto
, fragment
));
722 dolog((LOG_AUTHPRIV
| LOG_NOTICE
,
723 "ipfw: limit %d reached on entry %d\n",
724 limit_reached
, f
? f
->rulenum
: -1));
729 * IMPORTANT: the hash function for dynamic rules must be commutative
730 * in source and destination (ip,port), because rules are bidirectional
731 * and we want to find both in the same bucket.
734 hash_packet(struct ipfw_flow_id
*id
)
738 i
= (id
->dst_ip
) ^ (id
->src_ip
) ^ (id
->dst_port
) ^ (id
->src_port
);
739 i
&= (curr_dyn_buckets
- 1);
744 * unlink a dynamic rule from a chain. prev is a pointer to
745 * the previous one, q is a pointer to the rule to delete,
746 * head is a pointer to the head of the queue.
747 * Modifies q and potentially also head.
749 #define UNLINK_DYN_RULE(prev, head, q) { \
750 ipfw_dyn_rule *old_q = q; \
752 /* remove a refcount to the parent */ \
753 if (q->dyn_type == O_LIMIT) \
754 q->parent->count--; \
755 DEB(printf("ipfw: unlink entry 0x%08x %d -> 0x%08x %d, %d left\n",\
756 (q->id.src_ip), (q->id.src_port), \
757 (q->id.dst_ip), (q->id.dst_port), dyn_count-1 ); ) \
759 prev->next = q = q->next; \
761 head = q = q->next; \
763 _FREE(old_q, M_IPFW); }
765 #define TIME_LEQ(a,b) ((int)((a)-(b)) <= 0)
768 * Remove dynamic rules pointing to "rule", or all of them if rule == NULL.
770 * If keep_me == NULL, rules are deleted even if not expired,
771 * otherwise only expired rules are removed.
773 * The value of the second parameter is also used to point to identify
774 * a rule we absolutely do not want to remove (e.g. because we are
775 * holding a reference to it -- this is the case with O_LIMIT_PARENT
776 * rules). The pointer is only used for comparison, so any non-null
780 remove_dyn_rule(struct ip_fw
*rule
, ipfw_dyn_rule
*keep_me
)
782 static u_int32_t last_remove
= 0;
784 #define FORCE (keep_me == NULL)
786 ipfw_dyn_rule
*prev
, *q
;
787 int i
, pass
= 0, max_pass
= 0;
788 struct timeval timenow
;
790 getmicrotime(&timenow
);
792 if (ipfw_dyn_v
== NULL
|| dyn_count
== 0)
794 /* do not expire more than once per second, it is useless */
795 if (!FORCE
&& last_remove
== timenow
.tv_sec
)
797 last_remove
= timenow
.tv_sec
;
800 * because O_LIMIT refer to parent rules, during the first pass only
801 * remove child and mark any pending LIMIT_PARENT, and remove
802 * them in a second pass.
805 for (i
= 0 ; i
< curr_dyn_buckets
; i
++) {
806 for (prev
=NULL
, q
= ipfw_dyn_v
[i
] ; q
; ) {
808 * Logic can become complex here, so we split tests.
812 if (rule
!= NULL
&& rule
!= q
->rule
)
813 goto next
; /* not the one we are looking for */
814 if (q
->dyn_type
== O_LIMIT_PARENT
) {
816 * handle parent in the second pass,
817 * record we need one.
822 if (FORCE
&& q
->count
!= 0 ) {
823 /* XXX should not happen! */
824 printf("ipfw: OUCH! cannot remove rule,"
825 " count %d\n", q
->count
);
829 !TIME_LEQ( q
->expire
, timenow
.tv_sec
))
832 if (q
->dyn_type
!= O_LIMIT_PARENT
|| !q
->count
) {
833 UNLINK_DYN_RULE(prev
, ipfw_dyn_v
[i
], q
);
841 if (pass
++ < max_pass
)
847 * lookup a dynamic rule.
849 static ipfw_dyn_rule
*
850 lookup_dyn_rule(struct ipfw_flow_id
*pkt
, int *match_direction
,
854 * stateful ipfw extensions.
855 * Lookup into dynamic session queue
857 #define MATCH_REVERSE 0
858 #define MATCH_FORWARD 1
860 #define MATCH_UNKNOWN 3
861 #define BOTH_SYN (TH_SYN | (TH_SYN << 8))
862 #define BOTH_FIN (TH_FIN | (TH_FIN << 8))
864 int i
, dir
= MATCH_NONE
;
865 ipfw_dyn_rule
*prev
, *q
=NULL
;
866 struct timeval timenow
;
868 getmicrotime(&timenow
);
870 if (ipfw_dyn_v
== NULL
)
871 goto done
; /* not found */
872 i
= hash_packet( pkt
);
873 for (prev
=NULL
, q
= ipfw_dyn_v
[i
] ; q
!= NULL
; ) {
874 if (q
->dyn_type
== O_LIMIT_PARENT
&& q
->count
)
876 if (TIME_LEQ( q
->expire
, timenow
.tv_sec
)) { /* expire entry */
879 /* check if entry is TCP */
880 if ( q
->id
.proto
== IPPROTO_TCP
)
882 /* do not delete an established TCP connection which hasn't been closed by both sides */
883 if ( (q
->state
& (BOTH_SYN
| BOTH_FIN
)) != (BOTH_SYN
| BOTH_FIN
) )
887 UNLINK_DYN_RULE(prev
, ipfw_dyn_v
[i
], q
);
891 if (pkt
->proto
== q
->id
.proto
&&
892 q
->dyn_type
!= O_LIMIT_PARENT
) {
893 if (pkt
->src_ip
== q
->id
.src_ip
&&
894 pkt
->dst_ip
== q
->id
.dst_ip
&&
895 pkt
->src_port
== q
->id
.src_port
&&
896 pkt
->dst_port
== q
->id
.dst_port
) {
900 if (pkt
->src_ip
== q
->id
.dst_ip
&&
901 pkt
->dst_ip
== q
->id
.src_ip
&&
902 pkt
->src_port
== q
->id
.dst_port
&&
903 pkt
->dst_port
== q
->id
.src_port
) {
913 goto done
; /* q = NULL, not found */
915 if ( prev
!= NULL
) { /* found and not in front */
916 prev
->next
= q
->next
;
917 q
->next
= ipfw_dyn_v
[i
];
920 if (pkt
->proto
== IPPROTO_TCP
) { /* update state according to flags */
921 u_char flags
= pkt
->flags
& (TH_FIN
|TH_SYN
|TH_RST
);
923 q
->state
|= (dir
== MATCH_FORWARD
) ? flags
: (flags
<< 8);
925 case TH_SYN
: /* opening */
926 q
->expire
= timenow
.tv_sec
+ dyn_syn_lifetime
;
929 case BOTH_SYN
: /* move to established */
930 case BOTH_SYN
| TH_FIN
: /* one side tries to close */
931 case BOTH_SYN
| (TH_FIN
<< 8) :
933 #define _SEQ_GE(a,b) ((int)(a) - (int)(b) >= 0)
934 u_int32_t ack
= ntohl(tcp
->th_ack
);
935 if (dir
== MATCH_FORWARD
) {
936 if (q
->ack_fwd
== 0 || _SEQ_GE(ack
, q
->ack_fwd
))
938 else { /* ignore out-of-sequence */
942 if (q
->ack_rev
== 0 || _SEQ_GE(ack
, q
->ack_rev
))
944 else { /* ignore out-of-sequence */
949 q
->expire
= timenow
.tv_sec
+ dyn_ack_lifetime
;
952 case BOTH_SYN
| BOTH_FIN
: /* both sides closed */
953 if (dyn_fin_lifetime
>= dyn_keepalive_period
)
954 dyn_fin_lifetime
= dyn_keepalive_period
- 1;
955 q
->expire
= timenow
.tv_sec
+ dyn_fin_lifetime
;
961 * reset or some invalid combination, but can also
962 * occur if we use keep-state the wrong way.
964 if ( (q
->state
& ((TH_RST
<< 8)|TH_RST
)) == 0)
965 printf("invalid state: 0x%x\n", q
->state
);
967 if (dyn_rst_lifetime
>= dyn_keepalive_period
)
968 dyn_rst_lifetime
= dyn_keepalive_period
- 1;
969 q
->expire
= timenow
.tv_sec
+ dyn_rst_lifetime
;
972 } else if (pkt
->proto
== IPPROTO_UDP
) {
973 q
->expire
= timenow
.tv_sec
+ dyn_udp_lifetime
;
975 /* other protocols */
976 q
->expire
= timenow
.tv_sec
+ dyn_short_lifetime
;
980 *match_direction
= dir
;
985 realloc_dynamic_table(void)
988 * Try reallocation, make sure we have a power of 2 and do
989 * not allow more than 64k entries. In case of overflow,
993 if (dyn_buckets
> 65536)
995 if ((dyn_buckets
& (dyn_buckets
-1)) != 0) { /* not a power of 2 */
996 dyn_buckets
= curr_dyn_buckets
; /* reset */
999 curr_dyn_buckets
= dyn_buckets
;
1000 if (ipfw_dyn_v
!= NULL
)
1001 _FREE(ipfw_dyn_v
, M_IPFW
);
1003 ipfw_dyn_v
= _MALLOC(curr_dyn_buckets
* sizeof(ipfw_dyn_rule
*),
1004 M_IPFW
, M_NOWAIT
| M_ZERO
);
1005 if (ipfw_dyn_v
!= NULL
|| curr_dyn_buckets
<= 2)
1007 curr_dyn_buckets
/= 2;
1012 * Install state of type 'type' for a dynamic session.
1013 * The hash table contains two type of rules:
1014 * - regular rules (O_KEEP_STATE)
1015 * - rules for sessions with limited number of sess per user
1016 * (O_LIMIT). When they are created, the parent is
1017 * increased by 1, and decreased on delete. In this case,
1018 * the third parameter is the parent rule and not the chain.
1019 * - "parent" rules for the above (O_LIMIT_PARENT).
1021 static ipfw_dyn_rule
*
1022 add_dyn_rule(struct ipfw_flow_id
*id
, u_int8_t dyn_type
, struct ip_fw
*rule
)
1026 struct timeval timenow
;
1028 getmicrotime(&timenow
);
1030 if (ipfw_dyn_v
== NULL
||
1031 (dyn_count
== 0 && dyn_buckets
!= curr_dyn_buckets
)) {
1032 realloc_dynamic_table();
1033 if (ipfw_dyn_v
== NULL
)
1034 return NULL
; /* failed ! */
1036 i
= hash_packet(id
);
1038 r
= _MALLOC(sizeof *r
, M_IPFW
, M_NOWAIT
| M_ZERO
);
1041 printf ("ipfw: sorry cannot allocate state\n");
1046 /* increase refcount on parent, and set pointer */
1047 if (dyn_type
== O_LIMIT
) {
1048 ipfw_dyn_rule
*parent
= (ipfw_dyn_rule
*)rule
;
1049 if ( parent
->dyn_type
!= O_LIMIT_PARENT
)
1050 panic("invalid parent");
1053 rule
= parent
->rule
;
1057 r
->expire
= timenow
.tv_sec
+ dyn_syn_lifetime
;
1059 r
->dyn_type
= dyn_type
;
1060 r
->pcnt
= r
->bcnt
= 0;
1064 r
->next
= ipfw_dyn_v
[i
];
1067 DEB(printf("ipfw: add dyn entry ty %d 0x%08x %d -> 0x%08x %d, total %d\n",
1069 (r
->id
.src_ip
), (r
->id
.src_port
),
1070 (r
->id
.dst_ip
), (r
->id
.dst_port
),
1076 * lookup dynamic parent rule using pkt and rule as search keys.
1077 * If the lookup fails, then install one.
1079 static ipfw_dyn_rule
*
1080 lookup_dyn_parent(struct ipfw_flow_id
*pkt
, struct ip_fw
*rule
)
1084 struct timeval timenow
;
1086 getmicrotime(&timenow
);
1089 i
= hash_packet( pkt
);
1090 for (q
= ipfw_dyn_v
[i
] ; q
!= NULL
; q
=q
->next
)
1091 if (q
->dyn_type
== O_LIMIT_PARENT
&&
1093 pkt
->proto
== q
->id
.proto
&&
1094 pkt
->src_ip
== q
->id
.src_ip
&&
1095 pkt
->dst_ip
== q
->id
.dst_ip
&&
1096 pkt
->src_port
== q
->id
.src_port
&&
1097 pkt
->dst_port
== q
->id
.dst_port
) {
1098 q
->expire
= timenow
.tv_sec
+ dyn_short_lifetime
;
1099 DEB(printf("ipfw: lookup_dyn_parent found 0x%p\n",q
);)
1103 return add_dyn_rule(pkt
, O_LIMIT_PARENT
, rule
);
1107 * Install dynamic state for rule type cmd->o.opcode
1109 * Returns 1 (failure) if state is not installed because of errors or because
1110 * session limitations are enforced.
1113 install_state(struct ip_fw
*rule
, ipfw_insn_limit
*cmd
,
1114 struct ip_fw_args
*args
)
1116 static int last_log
;
1117 struct timeval timenow
;
1120 getmicrotime(&timenow
);
1122 DEB(printf("ipfw: install state type %d 0x%08x %u -> 0x%08x %u\n",
1124 (args
->f_id
.src_ip
), (args
->f_id
.src_port
),
1125 (args
->f_id
.dst_ip
), (args
->f_id
.dst_port
) );)
1127 q
= lookup_dyn_rule(&args
->f_id
, NULL
, NULL
);
1129 if (q
!= NULL
) { /* should never occur */
1130 if (last_log
!= timenow
.tv_sec
) {
1131 last_log
= timenow
.tv_sec
;
1132 printf("ipfw: install_state: entry already present, done\n");
1137 if (dyn_count
>= dyn_max
)
1139 * Run out of slots, try to remove any expired rule.
1141 remove_dyn_rule(NULL
, (ipfw_dyn_rule
*)1);
1143 if (dyn_count
>= dyn_max
) {
1144 if (last_log
!= timenow
.tv_sec
) {
1145 last_log
= timenow
.tv_sec
;
1146 printf("ipfw: install_state: Too many dynamic rules\n");
1148 return 1; /* cannot install, notify caller */
1151 switch (cmd
->o
.opcode
) {
1152 case O_KEEP_STATE
: /* bidir rule */
1153 add_dyn_rule(&args
->f_id
, O_KEEP_STATE
, rule
);
1156 case O_LIMIT
: /* limit number of sessions */
1158 u_int16_t limit_mask
= cmd
->limit_mask
;
1159 struct ipfw_flow_id id
;
1160 ipfw_dyn_rule
*parent
;
1162 DEB(printf("ipfw: installing dyn-limit rule %d\n",
1165 id
.dst_ip
= id
.src_ip
= 0;
1166 id
.dst_port
= id
.src_port
= 0;
1167 id
.proto
= args
->f_id
.proto
;
1169 if (limit_mask
& DYN_SRC_ADDR
)
1170 id
.src_ip
= args
->f_id
.src_ip
;
1171 if (limit_mask
& DYN_DST_ADDR
)
1172 id
.dst_ip
= args
->f_id
.dst_ip
;
1173 if (limit_mask
& DYN_SRC_PORT
)
1174 id
.src_port
= args
->f_id
.src_port
;
1175 if (limit_mask
& DYN_DST_PORT
)
1176 id
.dst_port
= args
->f_id
.dst_port
;
1177 parent
= lookup_dyn_parent(&id
, rule
);
1178 if (parent
== NULL
) {
1179 printf("ipfw: add parent failed\n");
1182 if (parent
->count
>= cmd
->conn_limit
) {
1184 * See if we can remove some expired rule.
1186 remove_dyn_rule(rule
, parent
);
1187 if (parent
->count
>= cmd
->conn_limit
) {
1188 if (fw_verbose
&& last_log
!= timenow
.tv_sec
) {
1189 last_log
= timenow
.tv_sec
;
1190 dolog((LOG_AUTHPRIV
| LOG_DEBUG
,
1191 "drop session, too many entries\n"));
1196 add_dyn_rule(&args
->f_id
, O_LIMIT
, (struct ip_fw
*)parent
);
1200 printf("ipfw: unknown dynamic rule type %u\n", cmd
->o
.opcode
);
1203 lookup_dyn_rule(&args
->f_id
, NULL
, NULL
); /* XXX just set lifetime */
1208 * Transmit a TCP packet, containing either a RST or a keepalive.
1209 * When flags & TH_RST, we are sending a RST packet, because of a
1210 * "reset" action matched the packet.
1211 * Otherwise we are sending a keepalive, and flags & TH_
1214 send_pkt(struct ipfw_flow_id
*id
, u_int32_t seq
, u_int32_t ack
, int flags
)
1219 struct route sro
; /* fake route */
1221 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
1224 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
1225 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct ip
) + sizeof(struct tcphdr
);
1226 m
->m_data
+= max_linkhdr
;
1228 ip
= mtod(m
, struct ip
*);
1229 bzero(ip
, m
->m_len
);
1230 tcp
= (struct tcphdr
*)(ip
+ 1); /* no IP options */
1231 ip
->ip_p
= IPPROTO_TCP
;
1234 * Assume we are sending a RST (or a keepalive in the reverse
1235 * direction), swap src and destination addresses and ports.
1237 ip
->ip_src
.s_addr
= htonl(id
->dst_ip
);
1238 ip
->ip_dst
.s_addr
= htonl(id
->src_ip
);
1239 tcp
->th_sport
= htons(id
->dst_port
);
1240 tcp
->th_dport
= htons(id
->src_port
);
1241 if (flags
& TH_RST
) { /* we are sending a RST */
1242 if (flags
& TH_ACK
) {
1243 tcp
->th_seq
= htonl(ack
);
1244 tcp
->th_ack
= htonl(0);
1245 tcp
->th_flags
= TH_RST
;
1249 tcp
->th_seq
= htonl(0);
1250 tcp
->th_ack
= htonl(seq
);
1251 tcp
->th_flags
= TH_RST
| TH_ACK
;
1255 * We are sending a keepalive. flags & TH_SYN determines
1256 * the direction, forward if set, reverse if clear.
1257 * NOTE: seq and ack are always assumed to be correct
1258 * as set by the caller. This may be confusing...
1260 if (flags
& TH_SYN
) {
1262 * we have to rewrite the correct addresses!
1264 ip
->ip_dst
.s_addr
= htonl(id
->dst_ip
);
1265 ip
->ip_src
.s_addr
= htonl(id
->src_ip
);
1266 tcp
->th_dport
= htons(id
->dst_port
);
1267 tcp
->th_sport
= htons(id
->src_port
);
1269 tcp
->th_seq
= htonl(seq
);
1270 tcp
->th_ack
= htonl(ack
);
1271 tcp
->th_flags
= TH_ACK
;
1274 * set ip_len to the payload size so we can compute
1275 * the tcp checksum on the pseudoheader
1276 * XXX check this, could save a couple of words ?
1278 ip
->ip_len
= htons(sizeof(struct tcphdr
));
1279 tcp
->th_sum
= in_cksum(m
, m
->m_pkthdr
.len
);
1281 * now fill fields left out earlier
1283 ip
->ip_ttl
= ip_defttl
;
1284 ip
->ip_len
= m
->m_pkthdr
.len
;
1285 bzero (&sro
, sizeof (sro
));
1286 ip_rtaddr(ip
->ip_dst
, &sro
);
1287 m
->m_flags
|= M_SKIP_FIREWALL
;
1288 ip_output_list(m
, 0, NULL
, &sro
, 0, NULL
);
1294 * sends a reject message, consuming the mbuf passed as an argument.
1297 send_reject(struct ip_fw_args
*args
, int code
, int offset
, int ip_len
)
1300 if (code
!= ICMP_REJECT_RST
) { /* Send an ICMP unreach */
1301 /* We need the IP header in host order for icmp_error(). */
1302 if (args
->eh
!= NULL
) {
1303 struct ip
*ip
= mtod(args
->m
, struct ip
*);
1304 ip
->ip_len
= ntohs(ip
->ip_len
);
1305 ip
->ip_off
= ntohs(ip
->ip_off
);
1307 args
->m
->m_flags
|= M_SKIP_FIREWALL
;
1308 icmp_error(args
->m
, ICMP_UNREACH
, code
, 0L, 0);
1309 } else if (offset
== 0 && args
->f_id
.proto
== IPPROTO_TCP
) {
1310 struct tcphdr
*const tcp
=
1311 L3HDR(struct tcphdr
, mtod(args
->m
, struct ip
*));
1312 if ( (tcp
->th_flags
& TH_RST
) == 0) {
1313 send_pkt(&(args
->f_id
), ntohl(tcp
->th_seq
),
1315 tcp
->th_flags
| TH_RST
);
1325 * Given an ip_fw *, lookup_next_rule will return a pointer
1326 * to the next rule, which can be either the jump
1327 * target (for skipto instructions) or the next one in the list (in
1328 * all other cases including a missing jump target).
1329 * The result is also written in the "next_rule" field of the rule.
1330 * Backward jumps are not allowed, so start looking from the next
1333 * This never returns NULL -- in case we do not have an exact match,
1334 * the next rule is returned. When the ruleset is changed,
1335 * pointers are flushed so we are always correct.
1338 static struct ip_fw
*
1339 lookup_next_rule(struct ip_fw
*me
)
1341 struct ip_fw
*rule
= NULL
;
1344 /* look for action, in case it is a skipto */
1345 cmd
= ACTION_PTR(me
);
1346 if (cmd
->opcode
== O_LOG
)
1348 if ( cmd
->opcode
== O_SKIPTO
)
1349 for (rule
= me
->next
; rule
; rule
= rule
->next
)
1350 if (rule
->rulenum
>= cmd
->arg1
)
1352 if (rule
== NULL
) /* failure or not a skipto */
1354 me
->next_rule
= rule
;
1359 * The main check routine for the firewall.
1361 * All arguments are in args so we can modify them and return them
1362 * back to the caller.
1366 * args->m (in/out) The packet; we set to NULL when/if we nuke it.
1367 * Starts with the IP header.
1368 * args->eh (in) Mac header if present, or NULL for layer3 packet.
1369 * args->oif Outgoing interface, or NULL if packet is incoming.
1370 * The incoming interface is in the mbuf. (in)
1371 * args->divert_rule (in/out)
1372 * Skip up to the first rule past this rule number;
1373 * upon return, non-zero port number for divert or tee.
1375 * args->rule Pointer to the last matching rule (in/out)
1376 * args->next_hop Socket we are forwarding to (out).
1377 * args->f_id Addresses grabbed from the packet (out)
1381 * IP_FW_PORT_DENY_FLAG the packet must be dropped.
1382 * 0 The packet is to be accepted and routed normally OR
1383 * the packet was denied/rejected and has been dropped;
1384 * in the latter case, *m is equal to NULL upon return.
1385 * port Divert the packet to port, with these caveats:
1387 * - If IP_FW_PORT_TEE_FLAG is set, tee the packet instead
1388 * of diverting it (ie, 'ipfw tee').
1390 * - If IP_FW_PORT_DYNT_FLAG is set, interpret the lower
1391 * 16 bits as a dummynet pipe number instead of diverting
1395 ipfw_chk(struct ip_fw_args
*args
)
1398 * Local variables hold state during the processing of a packet.
1400 * IMPORTANT NOTE: to speed up the processing of rules, there
1401 * are some assumption on the values of the variables, which
1402 * are documented here. Should you change them, please check
1403 * the implementation of the various instructions to make sure
1404 * that they still work.
1406 * args->eh The MAC header. It is non-null for a layer2
1407 * packet, it is NULL for a layer-3 packet.
1409 * m | args->m Pointer to the mbuf, as received from the caller.
1410 * It may change if ipfw_chk() does an m_pullup, or if it
1411 * consumes the packet because it calls send_reject().
1412 * XXX This has to change, so that ipfw_chk() never modifies
1413 * or consumes the buffer.
1414 * ip is simply an alias of the value of m, and it is kept
1415 * in sync with it (the packet is supposed to start with
1418 struct mbuf
*m
= args
->m
;
1419 struct ip
*ip
= mtod(m
, struct ip
*);
1422 * oif | args->oif If NULL, ipfw_chk has been called on the
1423 * inbound path (ether_input, bdg_forward, ip_input).
1424 * If non-NULL, ipfw_chk has been called on the outbound path
1425 * (ether_output, ip_output).
1427 struct ifnet
*oif
= args
->oif
;
1429 struct ip_fw
*f
= NULL
; /* matching rule */
1433 * hlen The length of the IPv4 header.
1434 * hlen >0 means we have an IPv4 packet.
1436 u_int hlen
= 0; /* hlen >0 means we have an IP pkt */
1439 * offset The offset of a fragment. offset != 0 means that
1440 * we have a fragment at this offset of an IPv4 packet.
1441 * offset == 0 means that (if this is an IPv4 packet)
1442 * this is the first or only fragment.
1447 * Local copies of addresses. They are only valid if we have
1450 * proto The protocol. Set to 0 for non-ip packets,
1451 * or to the protocol read from the packet otherwise.
1452 * proto != 0 means that we have an IPv4 packet.
1454 * src_port, dst_port port numbers, in HOST format. Only
1455 * valid for TCP and UDP packets.
1457 * src_ip, dst_ip ip addresses, in NETWORK format.
1458 * Only valid for IPv4 packets.
1461 u_int16_t src_port
= 0, dst_port
= 0; /* NOTE: host format */
1462 struct in_addr src_ip
, dst_ip
; /* NOTE: network format */
1465 int dyn_dir
= MATCH_UNKNOWN
;
1466 ipfw_dyn_rule
*q
= NULL
;
1467 struct timeval timenow
;
1469 if (m
->m_flags
& M_SKIP_FIREWALL
) {
1470 return 0; /* accept */
1473 lck_mtx_lock(ipfw_mutex
);
1475 getmicrotime(&timenow
);
1477 * dyn_dir = MATCH_UNKNOWN when rules unchecked,
1478 * MATCH_NONE when checked and not matched (q = NULL),
1479 * MATCH_FORWARD or MATCH_REVERSE otherwise (q != NULL)
1482 pktlen
= m
->m_pkthdr
.len
;
1483 if (args
->eh
== NULL
|| /* layer 3 packet */
1484 ( m
->m_pkthdr
.len
>= sizeof(struct ip
) &&
1485 ntohs(args
->eh
->ether_type
) == ETHERTYPE_IP
))
1486 hlen
= ip
->ip_hl
<< 2;
1489 * Collect parameters into local variables for faster matching.
1491 if (hlen
== 0) { /* do not grab addresses for non-ip pkts */
1492 proto
= args
->f_id
.proto
= 0; /* mark f_id invalid */
1493 goto after_ip_checks
;
1496 proto
= args
->f_id
.proto
= ip
->ip_p
;
1497 src_ip
= ip
->ip_src
;
1498 dst_ip
= ip
->ip_dst
;
1499 if (args
->eh
!= NULL
) { /* layer 2 packets are as on the wire */
1500 offset
= ntohs(ip
->ip_off
) & IP_OFFMASK
;
1501 ip_len
= ntohs(ip
->ip_len
);
1503 offset
= ip
->ip_off
& IP_OFFMASK
;
1504 ip_len
= ip
->ip_len
;
1506 pktlen
= ip_len
< pktlen
? ip_len
: pktlen
;
1508 #define PULLUP_TO(len) \
1510 if ((m)->m_len < (len)) { \
1511 args->m = m = m_pullup(m, (len)); \
1513 goto pullup_failed; \
1514 ip = mtod(m, struct ip *); \
1524 PULLUP_TO(hlen
+ sizeof(struct tcphdr
));
1525 tcp
= L3HDR(struct tcphdr
, ip
);
1526 dst_port
= tcp
->th_dport
;
1527 src_port
= tcp
->th_sport
;
1528 args
->f_id
.flags
= tcp
->th_flags
;
1536 PULLUP_TO(hlen
+ sizeof(struct udphdr
));
1537 udp
= L3HDR(struct udphdr
, ip
);
1538 dst_port
= udp
->uh_dport
;
1539 src_port
= udp
->uh_sport
;
1544 PULLUP_TO(hlen
+ 4); /* type, code and checksum. */
1545 args
->f_id
.flags
= L3HDR(struct icmp
, ip
)->icmp_type
;
1554 args
->f_id
.src_ip
= ntohl(src_ip
.s_addr
);
1555 args
->f_id
.dst_ip
= ntohl(dst_ip
.s_addr
);
1556 args
->f_id
.src_port
= src_port
= ntohs(src_port
);
1557 args
->f_id
.dst_port
= dst_port
= ntohs(dst_port
);
1562 * Packet has already been tagged. Look for the next rule
1563 * to restart processing.
1565 * If fw_one_pass != 0 then just accept it.
1566 * XXX should not happen here, but optimized out in
1570 lck_mtx_unlock(ipfw_mutex
);
1574 f
= args
->rule
->next_rule
;
1576 f
= lookup_next_rule(args
->rule
);
1579 * Find the starting rule. It can be either the first
1580 * one, or the one after divert_rule if asked so.
1582 int skipto
= args
->divert_rule
;
1585 if (args
->eh
== NULL
&& skipto
!= 0) {
1586 if (skipto
>= IPFW_DEFAULT_RULE
) {
1587 lck_mtx_unlock(ipfw_mutex
);
1588 return(IP_FW_PORT_DENY_FLAG
); /* invalid */
1590 while (f
&& f
->rulenum
<= skipto
)
1592 if (f
== NULL
) { /* drop packet */
1593 lck_mtx_unlock(ipfw_mutex
);
1594 return(IP_FW_PORT_DENY_FLAG
);
1598 args
->divert_rule
= 0; /* reset to avoid confusion later */
1601 * Now scan the rules, and parse microinstructions for each rule.
1603 for (; f
; f
= f
->next
) {
1606 int skip_or
; /* skip rest of OR block */
1609 if (f
->reserved_1
== IPFW_RULE_INACTIVE
) {
1613 if (set_disable
& (1 << f
->set
) )
1617 for (l
= f
->cmd_len
, cmd
= f
->cmd
; l
> 0 ;
1618 l
-= cmdlen
, cmd
+= cmdlen
) {
1622 * check_body is a jump target used when we find a
1623 * CHECK_STATE, and need to jump to the body of
1628 cmdlen
= F_LEN(cmd
);
1630 * An OR block (insn_1 || .. || insn_n) has the
1631 * F_OR bit set in all but the last instruction.
1632 * The first match will set "skip_or", and cause
1633 * the following instructions to be skipped until
1634 * past the one with the F_OR bit clear.
1636 if (skip_or
) { /* skip this instruction */
1637 if ((cmd
->len
& F_OR
) == 0)
1638 skip_or
= 0; /* next one is good */
1641 match
= 0; /* set to 1 if we succeed */
1643 switch (cmd
->opcode
) {
1645 * The first set of opcodes compares the packet's
1646 * fields with some pattern, setting 'match' if a
1647 * match is found. At the end of the loop there is
1648 * logic to deal with F_NOT and F_OR flags associated
1656 printf("ipfw: opcode %d unimplemented\n",
1665 * We only check offset == 0 && proto != 0,
1666 * as this ensures that we have an IPv4
1667 * packet with the ports info.
1673 struct inpcbinfo
*pi
;
1677 if (proto
== IPPROTO_TCP
) {
1680 } else if (proto
== IPPROTO_UDP
) {
1687 in_pcblookup_hash(pi
,
1688 dst_ip
, htons(dst_port
),
1689 src_ip
, htons(src_port
),
1691 in_pcblookup_hash(pi
,
1692 src_ip
, htons(src_port
),
1693 dst_ip
, htons(dst_port
),
1696 if (pcb
== NULL
|| pcb
->inp_socket
== NULL
)
1698 #if __FreeBSD_version < 500034
1699 #define socheckuid(a,b) (kauth_cred_getuid((a)->so_cred) != (b))
1701 if (cmd
->opcode
== O_UID
) {
1704 (pcb
->inp_socket
->so_uid
== (uid_t
)((ipfw_insn_u32
*)cmd
)->d
[0]);
1706 !socheckuid(pcb
->inp_socket
,
1707 (uid_t
)((ipfw_insn_u32
*)cmd
)->d
[0]);
1713 kauth_cred_ismember_gid(pcb
->inp_socket
->so_cred
,
1714 (gid_t
)((ipfw_insn_u32
*)cmd
)->d
[0], &match
);
1722 match
= iface_match(m
->m_pkthdr
.rcvif
,
1723 (ipfw_insn_if
*)cmd
);
1727 match
= iface_match(oif
, (ipfw_insn_if
*)cmd
);
1731 match
= iface_match(oif
? oif
:
1732 m
->m_pkthdr
.rcvif
, (ipfw_insn_if
*)cmd
);
1736 if (args
->eh
!= NULL
) { /* have MAC header */
1737 u_int32_t
*want
= (u_int32_t
*)
1738 ((ipfw_insn_mac
*)cmd
)->addr
;
1739 u_int32_t
*mask
= (u_int32_t
*)
1740 ((ipfw_insn_mac
*)cmd
)->mask
;
1741 u_int32_t
*hdr
= (u_int32_t
*)args
->eh
;
1744 ( want
[0] == (hdr
[0] & mask
[0]) &&
1745 want
[1] == (hdr
[1] & mask
[1]) &&
1746 want
[2] == (hdr
[2] & mask
[2]) );
1751 if (args
->eh
!= NULL
) {
1753 ntohs(args
->eh
->ether_type
);
1755 ((ipfw_insn_u16
*)cmd
)->ports
;
1758 for (i
= cmdlen
- 1; !match
&& i
>0;
1760 match
= (t
>=p
[0] && t
<=p
[1]);
1765 match
= (hlen
> 0 && offset
!= 0);
1768 case O_IN
: /* "out" is "not in" */
1769 match
= (oif
== NULL
);
1773 match
= (args
->eh
!= NULL
);
1778 * We do not allow an arg of 0 so the
1779 * check of "proto" only suffices.
1781 match
= (proto
== cmd
->arg1
);
1785 match
= (hlen
> 0 &&
1786 ((ipfw_insn_ip
*)cmd
)->addr
.s_addr
==
1794 (cmd
->opcode
== O_IP_DST_MASK
) ?
1795 dst_ip
.s_addr
: src_ip
.s_addr
;
1796 uint32_t *p
= ((ipfw_insn_u32
*)cmd
)->d
;
1799 for (; !match
&& i
>0; i
-= 2, p
+= 2)
1800 match
= (p
[0] == (a
& p
[1]));
1808 INADDR_TO_IFP(src_ip
, tif
);
1809 match
= (tif
!= NULL
);
1816 u_int32_t
*d
= (u_int32_t
*)(cmd
+1);
1818 cmd
->opcode
== O_IP_DST_SET
?
1824 addr
-= d
[0]; /* subtract base */
1825 match
= (addr
< cmd
->arg1
) &&
1826 ( d
[ 1 + (addr
>>5)] &
1827 (1<<(addr
& 0x1f)) );
1832 match
= (hlen
> 0 &&
1833 ((ipfw_insn_ip
*)cmd
)->addr
.s_addr
==
1841 INADDR_TO_IFP(dst_ip
, tif
);
1842 match
= (tif
!= NULL
);
1849 * offset == 0 && proto != 0 is enough
1850 * to guarantee that we have an IPv4
1851 * packet with port info.
1853 if ((proto
==IPPROTO_UDP
|| proto
==IPPROTO_TCP
)
1856 (cmd
->opcode
== O_IP_SRCPORT
) ?
1857 src_port
: dst_port
;
1859 ((ipfw_insn_u16
*)cmd
)->ports
;
1862 for (i
= cmdlen
- 1; !match
&& i
>0;
1864 match
= (x
>=p
[0] && x
<=p
[1]);
1869 match
= (offset
== 0 && proto
==IPPROTO_ICMP
&&
1870 icmptype_match(ip
, (ipfw_insn_u32
*)cmd
) );
1874 match
= (hlen
> 0 && ipopts_match(ip
, cmd
) );
1878 match
= (hlen
> 0 && cmd
->arg1
== ip
->ip_v
);
1884 if (hlen
> 0) { /* only for IP packets */
1889 if (cmd
->opcode
== O_IPLEN
)
1891 else if (cmd
->opcode
== O_IPTTL
)
1893 else /* must be IPID */
1894 x
= ntohs(ip
->ip_id
);
1896 match
= (cmd
->arg1
== x
);
1899 /* otherwise we have ranges */
1900 p
= ((ipfw_insn_u16
*)cmd
)->ports
;
1902 for (; !match
&& i
>0; i
--, p
+= 2)
1903 match
= (x
>= p
[0] && x
<= p
[1]);
1907 case O_IPPRECEDENCE
:
1908 match
= (hlen
> 0 &&
1909 (cmd
->arg1
== (ip
->ip_tos
& 0xe0)) );
1913 match
= (hlen
> 0 &&
1914 flags_match(cmd
, ip
->ip_tos
));
1918 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1920 L3HDR(struct tcphdr
,ip
)->th_flags
));
1924 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1925 tcpopts_match(ip
, cmd
));
1929 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1930 ((ipfw_insn_u32
*)cmd
)->d
[0] ==
1931 L3HDR(struct tcphdr
,ip
)->th_seq
);
1935 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1936 ((ipfw_insn_u32
*)cmd
)->d
[0] ==
1937 L3HDR(struct tcphdr
,ip
)->th_ack
);
1941 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1943 L3HDR(struct tcphdr
,ip
)->th_win
);
1947 /* reject packets which have SYN only */
1948 /* XXX should i also check for TH_ACK ? */
1949 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1950 (L3HDR(struct tcphdr
,ip
)->th_flags
&
1951 (TH_RST
| TH_ACK
| TH_SYN
)) != TH_SYN
);
1956 ipfw_log(f
, hlen
, args
->eh
, m
, oif
);
1961 match
= (random()<((ipfw_insn_u32
*)cmd
)->d
[0]);
1965 /* Outgoing packets automatically pass/match */
1966 match
= ((oif
!= NULL
) ||
1967 (m
->m_pkthdr
.rcvif
== NULL
) ||
1968 verify_rev_path(src_ip
, m
->m_pkthdr
.rcvif
));
1973 match
= (m_tag_find(m
,
1974 PACKET_TAG_IPSEC_IN_DONE
, NULL
) != NULL
);
1977 match
= (ipsec_gethist(m
, NULL
) != NULL
);
1979 /* otherwise no match */
1983 * The second set of opcodes represents 'actions',
1984 * i.e. the terminal part of a rule once the packet
1985 * matches all previous patterns.
1986 * Typically there is only one action for each rule,
1987 * and the opcode is stored at the end of the rule
1988 * (but there are exceptions -- see below).
1990 * In general, here we set retval and terminate the
1991 * outer loop (would be a 'break 3' in some language,
1992 * but we need to do a 'goto done').
1995 * O_COUNT and O_SKIPTO actions:
1996 * instead of terminating, we jump to the next rule
1997 * ('goto next_rule', equivalent to a 'break 2'),
1998 * or to the SKIPTO target ('goto again' after
1999 * having set f, cmd and l), respectively.
2001 * O_LIMIT and O_KEEP_STATE: these opcodes are
2002 * not real 'actions', and are stored right
2003 * before the 'action' part of the rule.
2004 * These opcodes try to install an entry in the
2005 * state tables; if successful, we continue with
2006 * the next opcode (match=1; break;), otherwise
2007 * the packet * must be dropped
2008 * ('goto done' after setting retval);
2010 * O_PROBE_STATE and O_CHECK_STATE: these opcodes
2011 * cause a lookup of the state table, and a jump
2012 * to the 'action' part of the parent rule
2013 * ('goto check_body') if an entry is found, or
2014 * (CHECK_STATE only) a jump to the next rule if
2015 * the entry is not found ('goto next_rule').
2016 * The result of the lookup is cached to make
2017 * further instances of these opcodes are
2022 if (install_state(f
,
2023 (ipfw_insn_limit
*)cmd
, args
)) {
2024 retval
= IP_FW_PORT_DENY_FLAG
;
2025 goto done
; /* error/limit violation */
2033 * dynamic rules are checked at the first
2034 * keep-state or check-state occurrence,
2035 * with the result being stored in dyn_dir.
2036 * The compiler introduces a PROBE_STATE
2037 * instruction for us when we have a
2038 * KEEP_STATE (because PROBE_STATE needs
2041 if (dyn_dir
== MATCH_UNKNOWN
&&
2042 (q
= lookup_dyn_rule(&args
->f_id
,
2043 &dyn_dir
, proto
== IPPROTO_TCP
?
2044 L3HDR(struct tcphdr
, ip
) : NULL
))
2047 * Found dynamic entry, update stats
2048 * and jump to the 'action' part of
2054 cmd
= ACTION_PTR(f
);
2055 l
= f
->cmd_len
- f
->act_ofs
;
2059 * Dynamic entry not found. If CHECK_STATE,
2060 * skip to next rule, if PROBE_STATE just
2061 * ignore and continue with next opcode.
2063 if (cmd
->opcode
== O_CHECK_STATE
)
2069 retval
= 0; /* accept */
2074 args
->rule
= f
; /* report matching rule */
2075 retval
= cmd
->arg1
| IP_FW_PORT_DYNT_FLAG
;
2080 if (args
->eh
) /* not on layer 2 */
2082 args
->divert_rule
= f
->rulenum
;
2083 retval
= (cmd
->opcode
== O_DIVERT
) ?
2085 cmd
->arg1
| IP_FW_PORT_TEE_FLAG
;
2090 f
->pcnt
++; /* update stats */
2092 f
->timestamp
= timenow
.tv_sec
;
2093 if (cmd
->opcode
== O_COUNT
)
2096 if (f
->next_rule
== NULL
)
2097 lookup_next_rule(f
);
2103 * Drop the packet and send a reject notice
2104 * if the packet is not ICMP (or is an ICMP
2105 * query), and it is not multicast/broadcast.
2108 (proto
!= IPPROTO_ICMP
||
2109 is_icmp_query(ip
)) &&
2110 !(m
->m_flags
& (M_BCAST
|M_MCAST
)) &&
2111 !IN_MULTICAST(dst_ip
.s_addr
)) {
2112 send_reject(args
, cmd
->arg1
,
2118 retval
= IP_FW_PORT_DENY_FLAG
;
2122 if (args
->eh
) /* not valid on layer2 pkts */
2124 if (!q
|| dyn_dir
== MATCH_FORWARD
)
2126 &((ipfw_insn_sa
*)cmd
)->sa
;
2131 panic("-- unknown opcode %d\n", cmd
->opcode
);
2132 } /* end of switch() on opcodes */
2134 if (cmd
->len
& F_NOT
)
2138 if (cmd
->len
& F_OR
)
2141 if (!(cmd
->len
& F_OR
)) /* not an OR block, */
2142 break; /* try next rule */
2145 } /* end of inner for, scan opcodes */
2147 next_rule
:; /* try next rule */
2149 } /* end of outer for, scan rules */
2150 printf("ipfw: ouch!, skip past end of rules, denying packet\n");
2151 lck_mtx_unlock(ipfw_mutex
);
2152 return(IP_FW_PORT_DENY_FLAG
);
2155 /* Update statistics */
2158 f
->timestamp
= timenow
.tv_sec
;
2159 lck_mtx_unlock(ipfw_mutex
);
2164 printf("ipfw: pullup failed\n");
2165 lck_mtx_unlock(ipfw_mutex
);
2166 return(IP_FW_PORT_DENY_FLAG
);
2170 * When a rule is added/deleted, clear the next_rule pointers in all rules.
2171 * These will be reconstructed on the fly as packets are matched.
2172 * Must be called at splimp().
2175 flush_rule_ptrs(void)
2179 for (rule
= layer3_chain
; rule
; rule
= rule
->next
)
2180 rule
->next_rule
= NULL
;
2184 * When pipes/queues are deleted, clear the "pipe_ptr" pointer to a given
2185 * pipe/queue, or to all of them (match == NULL).
2186 * Must be called at splimp().
2189 flush_pipe_ptrs(struct dn_flow_set
*match
)
2193 for (rule
= layer3_chain
; rule
; rule
= rule
->next
) {
2194 ipfw_insn_pipe
*cmd
= (ipfw_insn_pipe
*)ACTION_PTR(rule
);
2196 if (cmd
->o
.opcode
!= O_PIPE
&& cmd
->o
.opcode
!= O_QUEUE
)
2199 * XXX Use bcmp/bzero to handle pipe_ptr to overcome
2200 * possible alignment problems on 64-bit architectures.
2201 * This code is seldom used so we do not worry too
2202 * much about efficiency.
2204 if (match
== NULL
||
2205 !bcmp(&cmd
->pipe_ptr
, &match
, sizeof(match
)) )
2206 bzero(&cmd
->pipe_ptr
, sizeof(cmd
->pipe_ptr
));
2211 * Add a new rule to the list. Copy the rule into a malloc'ed area, then
2212 * possibly create a rule number and add the rule to the list.
2213 * Update the rule_number in the input struct so the caller knows it as well.
2216 add_rule(struct ip_fw
**head
, struct ip_fw
*input_rule
)
2218 struct ip_fw
*rule
, *f
, *prev
;
2220 int l
= RULESIZE(input_rule
);
2222 if (*head
== NULL
&& input_rule
->rulenum
!= IPFW_DEFAULT_RULE
)
2225 rule
= _MALLOC(l
, M_IPFW
, M_WAIT
);
2227 printf("ipfw2: add_rule MALLOC failed\n");
2232 bcopy(input_rule
, rule
, l
);
2235 rule
->next_rule
= NULL
;
2239 rule
->timestamp
= 0;
2241 if (*head
== NULL
) { /* default rule */
2247 * If rulenum is 0, find highest numbered rule before the
2248 * default rule, and add autoinc_step
2250 if (autoinc_step
< 1)
2252 else if (autoinc_step
> 1000)
2253 autoinc_step
= 1000;
2254 if (rule
->rulenum
== 0) {
2256 * locate the highest numbered rule before default
2258 for (f
= *head
; f
; f
= f
->next
) {
2259 if (f
->rulenum
== IPFW_DEFAULT_RULE
)
2261 rule
->rulenum
= f
->rulenum
;
2263 if (rule
->rulenum
< IPFW_DEFAULT_RULE
- autoinc_step
)
2264 rule
->rulenum
+= autoinc_step
;
2265 input_rule
->rulenum
= rule
->rulenum
;
2269 * Now insert the new rule in the right place in the sorted list.
2271 for (prev
= NULL
, f
= *head
; f
; prev
= f
, f
= f
->next
) {
2272 if (f
->rulenum
> rule
->rulenum
) { /* found the location */
2276 } else { /* head insert */
2287 DEB(printf("ipfw: installed rule %d, static count now %d\n",
2288 rule
->rulenum
, static_count
);)
2293 * Free storage associated with a static rule (including derived
2295 * The caller is in charge of clearing rule pointers to avoid
2296 * dangling pointers.
2297 * @return a pointer to the next entry.
2298 * Arguments are not checked, so they better be correct.
2299 * Must be called at splimp().
2301 static struct ip_fw
*
2302 delete_rule(struct ip_fw
**head
, struct ip_fw
*prev
, struct ip_fw
*rule
)
2305 int l
= RULESIZE(rule
);
2308 remove_dyn_rule(rule
, NULL
/* force removal */);
2317 if (DUMMYNET_LOADED
)
2318 ip_dn_ruledel_ptr(rule
);
2319 #endif /* DUMMYNET */
2320 _FREE(rule
, M_IPFW
);
2324 #if DEBUG_INACTIVE_RULES
2326 print_chain(struct ip_fw
**chain
)
2328 struct ip_fw
*rule
= *chain
;
2330 for (; rule
; rule
= rule
->next
) {
2331 ipfw_insn
*cmd
= ACTION_PTR(rule
);
2333 printf("ipfw: rule->rulenum = %d\n", rule
->rulenum
);
2335 if (rule
->reserved_1
== IPFW_RULE_INACTIVE
) {
2336 printf("ipfw: rule->reserved = IPFW_RULE_INACTIVE\n");
2339 switch (cmd
->opcode
) {
2341 printf("ipfw: ACTION: Deny\n");
2345 if (cmd
->arg1
==ICMP_REJECT_RST
)
2346 printf("ipfw: ACTION: Reset\n");
2347 else if (cmd
->arg1
==ICMP_UNREACH_HOST
)
2348 printf("ipfw: ACTION: Reject\n");
2352 printf("ipfw: ACTION: Accept\n");
2355 printf("ipfw: ACTION: Count\n");
2358 printf("ipfw: ACTION: Divert\n");
2361 printf("ipfw: ACTION: Tee\n");
2364 printf("ipfw: ACTION: SkipTo\n");
2367 printf("ipfw: ACTION: Pipe\n");
2370 printf("ipfw: ACTION: Queue\n");
2373 printf("ipfw: ACTION: Forward\n");
2376 printf("ipfw: invalid action! %d\n", cmd
->opcode
);
2380 #endif /* DEBUG_INACTIVE_RULES */
2383 flush_inactive(void *param
)
2385 struct ip_fw
*inactive_rule
= (struct ip_fw
*)param
;
2386 struct ip_fw
*rule
, *prev
;
2388 lck_mtx_lock(ipfw_mutex
);
2390 for (rule
= layer3_chain
, prev
= NULL
; rule
; ) {
2391 if (rule
== inactive_rule
&& rule
->reserved_1
== IPFW_RULE_INACTIVE
) {
2392 struct ip_fw
*n
= rule
;
2395 layer3_chain
= rule
->next
;
2398 prev
->next
= rule
->next
;
2409 #if DEBUG_INACTIVE_RULES
2410 print_chain(&layer3_chain
);
2412 lck_mtx_unlock(ipfw_mutex
);
2416 mark_inactive(struct ip_fw
**prev
, struct ip_fw
**rule
)
2418 int l
= RULESIZE(*rule
);
2420 if ((*rule
)->reserved_1
!= IPFW_RULE_INACTIVE
) {
2421 (*rule
)->reserved_1
= IPFW_RULE_INACTIVE
;
2425 timeout(flush_inactive
, *rule
, 30*hz
); /* 30 sec. */
2429 *rule
= (*rule
)->next
;
2433 * Deletes all rules from a chain (except rules in set RESVD_SET
2434 * unless kill_default = 1).
2435 * Must be called at splimp().
2438 free_chain(struct ip_fw
**chain
, int kill_default
)
2440 struct ip_fw
*prev
, *rule
;
2442 flush_rule_ptrs(); /* more efficient to do outside the loop */
2443 for (prev
= NULL
, rule
= *chain
; rule
; )
2444 if (kill_default
|| rule
->set
!= RESVD_SET
) {
2445 ipfw_insn
*cmd
= ACTION_PTR(rule
);
2447 /* skip over forwarding rules so struct isn't
2448 * deleted while pointer is still in use elsewhere
2450 if (cmd
->opcode
== O_FORWARD_IP
) {
2451 mark_inactive(&prev
, &rule
);
2454 rule
= delete_rule(chain
, prev
, rule
);
2464 * Remove all rules with given number, and also do set manipulation.
2465 * Assumes chain != NULL && *chain != NULL.
2467 * The argument is an u_int32_t. The low 16 bit are the rule or set number,
2468 * the next 8 bits are the new set, the top 8 bits are the command:
2470 * 0 delete rules with given number
2471 * 1 delete rules with given set number
2472 * 2 move rules with given number to new set
2473 * 3 move rules with given set number to new set
2474 * 4 swap sets with given numbers
2477 del_entry(struct ip_fw
**chain
, u_int32_t arg
)
2479 struct ip_fw
*prev
= NULL
, *rule
= *chain
;
2481 u_int16_t rulenum
; /* rule or old_set */
2482 u_int8_t cmd
, new_set
;
2484 rulenum
= arg
& 0xffff;
2485 cmd
= (arg
>> 24) & 0xff;
2486 new_set
= (arg
>> 16) & 0xff;
2490 if (new_set
> RESVD_SET
)
2492 if (cmd
== 0 || cmd
== 2) {
2493 if (rulenum
>= IPFW_DEFAULT_RULE
)
2496 if (rulenum
> RESVD_SET
) /* old_set */
2501 case 0: /* delete rules with given number */
2503 * locate first rule to delete
2505 for (; rule
->rulenum
< rulenum
; prev
= rule
, rule
= rule
->next
)
2507 if (rule
->rulenum
!= rulenum
)
2511 * flush pointers outside the loop, then delete all matching
2512 * rules. prev remains the same throughout the cycle.
2515 while (rule
->rulenum
== rulenum
) {
2516 ipfw_insn
*cmd
= ACTION_PTR(rule
);
2518 /* keep forwarding rules around so struct isn't
2519 * deleted while pointer is still in use elsewhere
2521 if (cmd
->opcode
== O_FORWARD_IP
) {
2522 mark_inactive(&prev
, &rule
);
2525 rule
= delete_rule(chain
, prev
, rule
);
2530 case 1: /* delete all rules with given set number */
2532 while (rule
->rulenum
< IPFW_DEFAULT_RULE
) {
2533 if (rule
->set
== rulenum
) {
2534 ipfw_insn
*cmd
= ACTION_PTR(rule
);
2536 /* keep forwarding rules around so struct isn't
2537 * deleted while pointer is still in use elsewhere
2539 if (cmd
->opcode
== O_FORWARD_IP
) {
2540 mark_inactive(&prev
, &rule
);
2543 rule
= delete_rule(chain
, prev
, rule
);
2553 case 2: /* move rules with given number to new set */
2554 for (; rule
->rulenum
< IPFW_DEFAULT_RULE
; rule
= rule
->next
)
2555 if (rule
->rulenum
== rulenum
)
2556 rule
->set
= new_set
;
2559 case 3: /* move rules with given set number to new set */
2560 for (; rule
->rulenum
< IPFW_DEFAULT_RULE
; rule
= rule
->next
)
2561 if (rule
->set
== rulenum
)
2562 rule
->set
= new_set
;
2565 case 4: /* swap two sets */
2566 for (; rule
->rulenum
< IPFW_DEFAULT_RULE
; rule
= rule
->next
)
2567 if (rule
->set
== rulenum
)
2568 rule
->set
= new_set
;
2569 else if (rule
->set
== new_set
)
2570 rule
->set
= rulenum
;
2577 * Clear counters for a specific rule.
2580 clear_counters(struct ip_fw
*rule
, int log_only
)
2582 ipfw_insn_log
*l
= (ipfw_insn_log
*)ACTION_PTR(rule
);
2584 if (log_only
== 0) {
2585 rule
->bcnt
= rule
->pcnt
= 0;
2586 rule
->timestamp
= 0;
2588 if (l
->o
.opcode
== O_LOG
)
2589 l
->log_left
= l
->max_log
;
2593 * Reset some or all counters on firewall rules.
2594 * @arg frwl is null to clear all entries, or contains a specific
2596 * @arg log_only is 1 if we only want to reset logs, zero otherwise.
2599 zero_entry(int rulenum
, int log_only
)
2607 for (rule
= layer3_chain
; rule
; rule
= rule
->next
)
2608 clear_counters(rule
, log_only
);
2609 msg
= log_only
? "ipfw: All logging counts reset.\n" :
2610 "ipfw: Accounting cleared.\n";
2614 * We can have multiple rules with the same number, so we
2615 * need to clear them all.
2617 for (rule
= layer3_chain
; rule
; rule
= rule
->next
)
2618 if (rule
->rulenum
== rulenum
) {
2619 while (rule
&& rule
->rulenum
== rulenum
) {
2620 clear_counters(rule
, log_only
);
2626 if (!cleared
) /* we did not find any matching rules */
2628 msg
= log_only
? "ipfw: Entry %d logging count reset.\n" :
2629 "ipfw: Entry %d cleared.\n";
2633 dolog((LOG_AUTHPRIV
| LOG_NOTICE
, msg
, rulenum
));
2639 * Check validity of the structure before insert.
2640 * Fortunately rules are simple, so this mostly need to check rule sizes.
2643 check_ipfw_struct(struct ip_fw
*rule
, int size
)
2649 if (size
< sizeof(*rule
)) {
2650 printf("ipfw: rule too short\n");
2653 /* first, check for valid size */
2656 printf("ipfw: size mismatch (have %d want %d)\n", size
, l
);
2660 * Now go for the individual checks. Very simple ones, basically only
2661 * instruction sizes.
2663 for (l
= rule
->cmd_len
, cmd
= rule
->cmd
;
2664 l
> 0 ; l
-= cmdlen
, cmd
+= cmdlen
) {
2665 cmdlen
= F_LEN(cmd
);
2667 printf("ipfw: opcode %d size truncated\n",
2671 DEB(printf("ipfw: opcode %d\n", cmd
->opcode
);)
2672 switch (cmd
->opcode
) {
2683 case O_IPPRECEDENCE
:
2691 if (cmdlen
!= F_INSN_SIZE(ipfw_insn
))
2697 #endif /* __APPLE__ */
2704 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_u32
))
2709 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_limit
))
2714 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_log
))
2717 /* enforce logging limit */
2719 ((ipfw_insn_log
*)cmd
)->max_log
== 0 && verbose_limit
!= 0) {
2720 ((ipfw_insn_log
*)cmd
)->max_log
= verbose_limit
;
2723 ((ipfw_insn_log
*)cmd
)->log_left
=
2724 ((ipfw_insn_log
*)cmd
)->max_log
;
2730 /* only odd command lengths */
2731 if ( !(cmdlen
& 1) || cmdlen
> 31)
2737 if (cmd
->arg1
== 0 || cmd
->arg1
> 256) {
2738 printf("ipfw: invalid set size %d\n",
2742 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_u32
) +
2748 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_mac
))
2756 if (cmdlen
< 1 || cmdlen
> 31)
2762 case O_IP_DSTPORT
: /* XXX artificial limit, 30 port pairs */
2763 if (cmdlen
< 2 || cmdlen
> 31)
2770 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_if
))
2776 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_pipe
))
2781 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_sa
))
2785 case O_FORWARD_MAC
: /* XXX not implemented yet */
2794 if (cmdlen
!= F_INSN_SIZE(ipfw_insn
))
2798 printf("ipfw: opcode %d, multiple actions"
2805 printf("ipfw: opcode %d, action must be"
2812 printf("ipfw: opcode %d, unknown opcode\n",
2817 if (have_action
== 0) {
2818 printf("ipfw: missing action\n");
2824 printf("ipfw: opcode %d size %d wrong\n",
2825 cmd
->opcode
, cmdlen
);
2831 * {set|get}sockopt parser.
2834 ipfw_ctl(struct sockopt
*sopt
)
2836 #define RULE_MAXSIZE (256*sizeof(u_int32_t))
2837 u_int32_t api_version
;
2841 struct ip_fw
*bp
, *buf
, *rule
;
2843 /* copy of orig sopt to send to ipfw_get_command_and_version() */
2844 struct sockopt tmp_sopt
= *sopt
;
2845 struct timeval timenow
;
2847 getmicrotime(&timenow
);
2850 * Disallow modifications in really-really secure mode, but still allow
2851 * the logging counters to be reset.
2853 if (sopt
->sopt_name
== IP_FW_ADD
||
2854 (sopt
->sopt_dir
== SOPT_SET
&& sopt
->sopt_name
!= IP_FW_RESETLOG
)) {
2855 #if __FreeBSD_version >= 500034
2856 error
= securelevel_ge(sopt
->sopt_td
->td_ucred
, 3);
2859 #else /* FreeBSD 4.x */
2860 if (securelevel
>= 3)
2865 /* first get the command and version, then do conversion as necessary */
2866 error
= ipfw_get_command_and_version(&tmp_sopt
, &command
, &api_version
);
2869 /* error getting the version */
2876 * pass up a copy of the current rules. Static rules
2877 * come first (the last of which has number IPFW_DEFAULT_RULE),
2878 * followed by a possibly empty list of dynamic rule.
2879 * The last dynamic rule has NULL in the "next" field.
2881 lck_mtx_lock(ipfw_mutex
);
2882 size
= static_len
; /* size of static rules */
2883 if (ipfw_dyn_v
) /* add size of dyn.rules */
2884 size
+= (dyn_count
* sizeof(ipfw_dyn_rule
));
2887 * XXX todo: if the user passes a short length just to know
2888 * how much room is needed, do not bother filling up the
2889 * buffer, just jump to the sooptcopyout.
2891 buf
= _MALLOC(size
, M_TEMP
, M_WAITOK
);
2893 lck_mtx_unlock(ipfw_mutex
);
2901 for (rule
= layer3_chain
; rule
; rule
= rule
->next
) {
2902 int i
= RULESIZE(rule
);
2904 if (rule
->reserved_1
== IPFW_RULE_INACTIVE
) {
2908 bcopy(&set_disable
, &(bp
->next_rule
),
2909 sizeof(set_disable
));
2910 bp
= (struct ip_fw
*)((char *)bp
+ i
);
2914 ipfw_dyn_rule
*p
, *dst
, *last
= NULL
;
2916 dst
= (ipfw_dyn_rule
*)bp
;
2917 for (i
= 0 ; i
< curr_dyn_buckets
; i
++ )
2918 for ( p
= ipfw_dyn_v
[i
] ; p
!= NULL
;
2919 p
= p
->next
, dst
++ ) {
2920 bcopy(p
, dst
, sizeof *p
);
2921 bcopy(&(p
->rule
->rulenum
), &(dst
->rule
),
2922 sizeof(p
->rule
->rulenum
));
2924 * store a non-null value in "next".
2925 * The userland code will interpret a
2926 * NULL here as a marker
2927 * for the last dynamic rule.
2929 bcopy(&dst
, &dst
->next
, sizeof(dst
));
2932 TIME_LEQ(dst
->expire
, timenow
.tv_sec
) ?
2933 0 : dst
->expire
- timenow
.tv_sec
;
2935 if (last
!= NULL
) /* mark last dynamic rule */
2936 bzero(&last
->next
, sizeof(last
));
2938 lck_mtx_unlock(ipfw_mutex
);
2940 /* convert back if necessary and copyout */
2941 if (api_version
== IP_FW_VERSION_0
) {
2943 struct ip_old_fw
*buf2
, *rule_vers0
;
2945 buf2
= _MALLOC(static_count
* sizeof(struct ip_old_fw
), M_TEMP
, M_WAITOK
);
2954 for (i
= 0; i
< static_count
; i
++) {
2955 /* static rules have different sizes */
2956 int j
= RULESIZE(bp
);
2957 ipfw_convert_from_latest(bp
, rule_vers0
, api_version
);
2958 bp
= (struct ip_fw
*)((char *)bp
+ j
);
2959 len
+= sizeof(*rule_vers0
);
2962 error
= sooptcopyout(sopt
, buf2
, len
);
2963 _FREE(buf2
, M_TEMP
);
2965 } else if (api_version
== IP_FW_VERSION_1
) {
2966 int i
, len
= 0, buf_size
;
2967 struct ip_fw_compat
*buf2
, *rule_vers1
;
2968 struct ipfw_dyn_rule_compat
*dyn_rule_vers1
, *dyn_last
= NULL
;
2971 buf_size
= static_count
* sizeof(struct ip_fw_compat
) +
2972 dyn_count
* sizeof(struct ipfw_dyn_rule_compat
);
2974 buf2
= _MALLOC(buf_size
, M_TEMP
, M_WAITOK
);
2983 /* first do static rules */
2984 for (i
= 0; i
< static_count
; i
++) {
2985 /* static rules have different sizes */
2986 int j
= RULESIZE(bp
);
2987 ipfw_convert_from_latest(bp
, rule_vers1
, api_version
);
2988 bp
= (struct ip_fw
*)((char *)bp
+ j
);
2989 len
+= sizeof(*rule_vers1
);
2993 /* now do dynamic rules */
2994 dyn_rule_vers1
= (struct ipfw_dyn_rule_compat
*)rule_vers1
;
2996 for (i
= 0; i
< curr_dyn_buckets
; i
++) {
2997 for ( p
= ipfw_dyn_v
[i
] ; p
!= NULL
; p
= p
->next
) {
2998 (int) dyn_rule_vers1
->chain
= p
->rule
->rulenum
;
2999 dyn_rule_vers1
->id
= p
->id
;
3000 dyn_rule_vers1
->mask
= p
->id
;
3001 dyn_rule_vers1
->type
= p
->dyn_type
;
3002 dyn_rule_vers1
->expire
= p
->expire
;
3003 dyn_rule_vers1
->pcnt
= p
->pcnt
;
3004 dyn_rule_vers1
->bcnt
= p
->bcnt
;
3005 dyn_rule_vers1
->bucket
= p
->bucket
;
3006 dyn_rule_vers1
->state
= p
->state
;
3008 dyn_rule_vers1
->next
= dyn_rule_vers1
;
3009 dyn_last
= dyn_rule_vers1
;
3011 len
+= sizeof(*dyn_rule_vers1
);
3016 if (dyn_last
!= NULL
) {
3017 dyn_last
->next
= NULL
;
3021 error
= sooptcopyout(sopt
, buf2
, len
);
3022 _FREE(buf2
, M_TEMP
);
3025 error
= sooptcopyout(sopt
, buf
, size
);
3033 * Normally we cannot release the lock on each iteration.
3034 * We could do it here only because we start from the head all
3035 * the times so there is no risk of missing some entries.
3036 * On the other hand, the risk is that we end up with
3037 * a very inconsistent ruleset, so better keep the lock
3038 * around the whole cycle.
3040 * XXX this code can be improved by resetting the head of
3041 * the list to point to the default rule, and then freeing
3042 * the old list without the need for a lock.
3045 lck_mtx_lock(ipfw_mutex
);
3046 free_chain(&layer3_chain
, 0 /* keep default rule */);
3047 #if DEBUG_INACTIVE_RULES
3048 print_chain(&layer3_chain
);
3050 lck_mtx_unlock(ipfw_mutex
);
3054 rule
= _MALLOC(RULE_MAXSIZE
, M_TEMP
, M_WAITOK
);
3060 bzero(rule
, RULE_MAXSIZE
);
3062 if (api_version
!= IP_FW_CURRENT_API_VERSION
) {
3063 error
= ipfw_convert_to_latest(sopt
, rule
, api_version
);
3066 error
= sooptcopyin(sopt
, rule
, RULE_MAXSIZE
,
3067 sizeof(struct ip_fw
) );
3071 if ((api_version
== IP_FW_VERSION_0
) || (api_version
== IP_FW_VERSION_1
)) {
3072 /* the rule has already been checked so just
3073 * adjust sopt_valsize to match what would be expected.
3075 sopt
->sopt_valsize
= RULESIZE(rule
);
3077 error
= check_ipfw_struct(rule
, sopt
->sopt_valsize
);
3079 lck_mtx_lock(ipfw_mutex
);
3080 error
= add_rule(&layer3_chain
, rule
);
3081 lck_mtx_unlock(ipfw_mutex
);
3083 size
= RULESIZE(rule
);
3084 if (!error
&& sopt
->sopt_dir
== SOPT_GET
) {
3085 /* convert back if necessary and copyout */
3086 if (api_version
== IP_FW_VERSION_0
) {
3087 struct ip_old_fw rule_vers0
;
3089 ipfw_convert_from_latest(rule
, &rule_vers0
, api_version
);
3090 sopt
->sopt_valsize
= sizeof(struct ip_old_fw
);
3092 error
= sooptcopyout(sopt
, &rule_vers0
, sizeof(struct ip_old_fw
));
3093 } else if (api_version
== IP_FW_VERSION_1
) {
3094 struct ip_fw_compat rule_vers1
;
3096 ipfw_convert_from_latest(rule
, &rule_vers1
, api_version
);
3097 sopt
->sopt_valsize
= sizeof(struct ip_fw_compat
);
3099 error
= sooptcopyout(sopt
, &rule_vers1
, sizeof(struct ip_fw_compat
));
3101 error
= sooptcopyout(sopt
, rule
, size
);
3107 _FREE(rule
, M_TEMP
);
3113 * IP_FW_DEL is used for deleting single rules or sets,
3114 * and (ab)used to atomically manipulate sets.
3115 * rule->rulenum != 0 indicates single rule delete
3116 * rule->set_masks used to manipulate sets
3117 * rule->set_masks[0] contains info on sets to be
3118 * disabled, swapped, or moved
3119 * rule->set_masks[1] contains sets to be enabled.
3122 /* there is only a simple rule passed in
3123 * (no cmds), so use a temp struct to copy
3125 struct ip_fw temp_rule
;
3129 bzero(&temp_rule
, sizeof(struct ip_fw
));
3130 if (api_version
!= IP_FW_CURRENT_API_VERSION
) {
3131 error
= ipfw_convert_to_latest(sopt
, &temp_rule
, api_version
);
3134 error
= sooptcopyin(sopt
, &temp_rule
, sizeof(struct ip_fw
),
3135 sizeof(struct ip_fw
) );
3139 /* set_masks is used to distinguish between deleting
3140 * single rules or atomically manipulating sets
3142 lck_mtx_lock(ipfw_mutex
);
3144 arg
= temp_rule
.set_masks
[0];
3145 cmd
= (arg
>> 24) & 0xff;
3147 if (temp_rule
.rulenum
) {
3149 error
= del_entry(&layer3_chain
, temp_rule
.rulenum
);
3150 #if DEBUG_INACTIVE_RULES
3151 print_chain(&layer3_chain
);
3155 /* set reassignment - see comment above del_entry() for details */
3156 error
= del_entry(&layer3_chain
, temp_rule
.set_masks
[0]);
3157 #if DEBUG_INACTIVE_RULES
3158 print_chain(&layer3_chain
);
3161 else if (temp_rule
.set_masks
[0] != 0 ||
3162 temp_rule
.set_masks
[1] != 0) {
3163 /* set enable/disable */
3165 (set_disable
| temp_rule
.set_masks
[0]) & ~temp_rule
.set_masks
[1] &
3166 ~(1<<RESVD_SET
); /* set RESVD_SET always enabled */
3169 lck_mtx_unlock(ipfw_mutex
);
3174 case IP_FW_RESETLOG
: /* using rule->rulenum */
3176 /* there is only a simple rule passed in
3177 * (no cmds), so use a temp struct to copy
3179 struct ip_fw temp_rule
= { 0 };
3181 if (api_version
!= IP_FW_CURRENT_API_VERSION
) {
3182 error
= ipfw_convert_to_latest(sopt
, &temp_rule
, api_version
);
3185 if (sopt
->sopt_val
!= 0) {
3186 error
= sooptcopyin(sopt
, &temp_rule
, sizeof(struct ip_fw
),
3187 sizeof(struct ip_fw
) );
3192 lck_mtx_lock(ipfw_mutex
);
3193 error
= zero_entry(temp_rule
.rulenum
, sopt
->sopt_name
== IP_FW_RESETLOG
);
3194 lck_mtx_unlock(ipfw_mutex
);
3199 printf("ipfw: ipfw_ctl invalid option %d\n", sopt
->sopt_name
);
3207 * dummynet needs a reference to the default rule, because rules can be
3208 * deleted while packets hold a reference to them. When this happens,
3209 * dummynet changes the reference to the default rule (it could well be a
3210 * NULL pointer, but this way we do not need to check for the special
3211 * case, plus here he have info on the default behaviour).
3213 struct ip_fw
*ip_fw_default_rule
;
3216 * This procedure is only used to handle keepalives. It is invoked
3217 * every dyn_keepalive_period
3220 ipfw_tick(void * __unused unused
)
3225 struct timeval timenow
;
3228 if (dyn_keepalive
== 0 || ipfw_dyn_v
== NULL
|| dyn_count
== 0)
3231 getmicrotime(&timenow
);
3233 lck_mtx_lock(ipfw_mutex
);
3234 for (i
= 0 ; i
< curr_dyn_buckets
; i
++) {
3235 for (q
= ipfw_dyn_v
[i
] ; q
; q
= q
->next
) {
3236 if (q
->dyn_type
== O_LIMIT_PARENT
)
3238 if (q
->id
.proto
!= IPPROTO_TCP
)
3240 if ( (q
->state
& BOTH_SYN
) != BOTH_SYN
)
3242 if (TIME_LEQ( timenow
.tv_sec
+dyn_keepalive_interval
,
3244 continue; /* too early */
3245 if (TIME_LEQ(q
->expire
, timenow
.tv_sec
))
3246 continue; /* too late, rule expired */
3248 send_pkt(&(q
->id
), q
->ack_rev
- 1, q
->ack_fwd
, TH_SYN
);
3249 send_pkt(&(q
->id
), q
->ack_fwd
- 1, q
->ack_rev
, 0);
3252 lck_mtx_unlock(ipfw_mutex
);
3254 timeout(ipfw_tick
, NULL
, dyn_keepalive_period
*hz
);
3260 struct ip_fw default_rule
;
3263 ipfw_mutex_grp_attr
= lck_grp_attr_alloc_init();
3264 ipfw_mutex_grp
= lck_grp_alloc_init("ipfw", ipfw_mutex_grp_attr
);
3265 ipfw_mutex_attr
= lck_attr_alloc_init();
3266 lck_attr_setdefault(ipfw_mutex_attr
);
3268 if ((ipfw_mutex
= lck_mtx_alloc_init(ipfw_mutex_grp
, ipfw_mutex_attr
)) == NULL
) {
3269 printf("ipfw_init: can't alloc ipfw_mutex\n");
3273 layer3_chain
= NULL
;
3275 bzero(&default_rule
, sizeof default_rule
);
3277 default_rule
.act_ofs
= 0;
3278 default_rule
.rulenum
= IPFW_DEFAULT_RULE
;
3279 default_rule
.cmd_len
= 1;
3280 default_rule
.set
= RESVD_SET
;
3282 default_rule
.cmd
[0].len
= 1;
3283 default_rule
.cmd
[0].opcode
=
3284 #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT
3289 if (add_rule(&layer3_chain
, &default_rule
)) {
3290 printf("ipfw2: add_rule failed adding default rule\n");
3291 printf("ipfw2 failed initialization!!\n");
3295 ip_fw_default_rule
= layer3_chain
;
3297 /* Radar 3920649, don't print unncessary messages to the log */
3298 printf("ipfw2 initialized, divert %s, "
3299 "rule-based forwarding enabled, default to %s, logging ",
3305 default_rule
.cmd
[0].opcode
== O_ACCEPT
? "accept" : "deny");
3308 #ifdef IPFIREWALL_VERBOSE
3311 #ifdef IPFIREWALL_VERBOSE_LIMIT
3312 verbose_limit
= IPFIREWALL_VERBOSE_LIMIT
;
3314 if (fw_verbose
== 0)
3315 printf("disabled\n");
3316 else if (verbose_limit
== 0)
3317 printf("unlimited\n");
3319 printf("limited to %d packets/entry by default\n",
3323 ip_fw_chk_ptr
= ipfw_chk
;
3324 ip_fw_ctl_ptr
= ipfw_ctl
;
3326 ipfwstringlen
= strlen( ipfwstring
);
3328 timeout(ipfw_tick
, NULL
, hz
);