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 icmp_error(args
->m
, ICMP_UNREACH
, code
, 0L, 0);
1308 } else if (offset
== 0 && args
->f_id
.proto
== IPPROTO_TCP
) {
1309 struct tcphdr
*const tcp
=
1310 L3HDR(struct tcphdr
, mtod(args
->m
, struct ip
*));
1311 if ( (tcp
->th_flags
& TH_RST
) == 0) {
1312 send_pkt(&(args
->f_id
), ntohl(tcp
->th_seq
),
1314 tcp
->th_flags
| TH_RST
);
1324 * Given an ip_fw *, lookup_next_rule will return a pointer
1325 * to the next rule, which can be either the jump
1326 * target (for skipto instructions) or the next one in the list (in
1327 * all other cases including a missing jump target).
1328 * The result is also written in the "next_rule" field of the rule.
1329 * Backward jumps are not allowed, so start looking from the next
1332 * This never returns NULL -- in case we do not have an exact match,
1333 * the next rule is returned. When the ruleset is changed,
1334 * pointers are flushed so we are always correct.
1337 static struct ip_fw
*
1338 lookup_next_rule(struct ip_fw
*me
)
1340 struct ip_fw
*rule
= NULL
;
1343 /* look for action, in case it is a skipto */
1344 cmd
= ACTION_PTR(me
);
1345 if (cmd
->opcode
== O_LOG
)
1347 if ( cmd
->opcode
== O_SKIPTO
)
1348 for (rule
= me
->next
; rule
; rule
= rule
->next
)
1349 if (rule
->rulenum
>= cmd
->arg1
)
1351 if (rule
== NULL
) /* failure or not a skipto */
1353 me
->next_rule
= rule
;
1358 * The main check routine for the firewall.
1360 * All arguments are in args so we can modify them and return them
1361 * back to the caller.
1365 * args->m (in/out) The packet; we set to NULL when/if we nuke it.
1366 * Starts with the IP header.
1367 * args->eh (in) Mac header if present, or NULL for layer3 packet.
1368 * args->oif Outgoing interface, or NULL if packet is incoming.
1369 * The incoming interface is in the mbuf. (in)
1370 * args->divert_rule (in/out)
1371 * Skip up to the first rule past this rule number;
1372 * upon return, non-zero port number for divert or tee.
1374 * args->rule Pointer to the last matching rule (in/out)
1375 * args->next_hop Socket we are forwarding to (out).
1376 * args->f_id Addresses grabbed from the packet (out)
1380 * IP_FW_PORT_DENY_FLAG the packet must be dropped.
1381 * 0 The packet is to be accepted and routed normally OR
1382 * the packet was denied/rejected and has been dropped;
1383 * in the latter case, *m is equal to NULL upon return.
1384 * port Divert the packet to port, with these caveats:
1386 * - If IP_FW_PORT_TEE_FLAG is set, tee the packet instead
1387 * of diverting it (ie, 'ipfw tee').
1389 * - If IP_FW_PORT_DYNT_FLAG is set, interpret the lower
1390 * 16 bits as a dummynet pipe number instead of diverting
1394 ipfw_chk(struct ip_fw_args
*args
)
1397 * Local variables hold state during the processing of a packet.
1399 * IMPORTANT NOTE: to speed up the processing of rules, there
1400 * are some assumption on the values of the variables, which
1401 * are documented here. Should you change them, please check
1402 * the implementation of the various instructions to make sure
1403 * that they still work.
1405 * args->eh The MAC header. It is non-null for a layer2
1406 * packet, it is NULL for a layer-3 packet.
1408 * m | args->m Pointer to the mbuf, as received from the caller.
1409 * It may change if ipfw_chk() does an m_pullup, or if it
1410 * consumes the packet because it calls send_reject().
1411 * XXX This has to change, so that ipfw_chk() never modifies
1412 * or consumes the buffer.
1413 * ip is simply an alias of the value of m, and it is kept
1414 * in sync with it (the packet is supposed to start with
1417 struct mbuf
*m
= args
->m
;
1418 struct ip
*ip
= mtod(m
, struct ip
*);
1421 * oif | args->oif If NULL, ipfw_chk has been called on the
1422 * inbound path (ether_input, bdg_forward, ip_input).
1423 * If non-NULL, ipfw_chk has been called on the outbound path
1424 * (ether_output, ip_output).
1426 struct ifnet
*oif
= args
->oif
;
1428 struct ip_fw
*f
= NULL
; /* matching rule */
1432 * hlen The length of the IPv4 header.
1433 * hlen >0 means we have an IPv4 packet.
1435 u_int hlen
= 0; /* hlen >0 means we have an IP pkt */
1438 * offset The offset of a fragment. offset != 0 means that
1439 * we have a fragment at this offset of an IPv4 packet.
1440 * offset == 0 means that (if this is an IPv4 packet)
1441 * this is the first or only fragment.
1446 * Local copies of addresses. They are only valid if we have
1449 * proto The protocol. Set to 0 for non-ip packets,
1450 * or to the protocol read from the packet otherwise.
1451 * proto != 0 means that we have an IPv4 packet.
1453 * src_port, dst_port port numbers, in HOST format. Only
1454 * valid for TCP and UDP packets.
1456 * src_ip, dst_ip ip addresses, in NETWORK format.
1457 * Only valid for IPv4 packets.
1460 u_int16_t src_port
= 0, dst_port
= 0; /* NOTE: host format */
1461 struct in_addr src_ip
, dst_ip
; /* NOTE: network format */
1464 int dyn_dir
= MATCH_UNKNOWN
;
1465 ipfw_dyn_rule
*q
= NULL
;
1466 struct timeval timenow
;
1468 if (m
->m_flags
& M_SKIP_FIREWALL
) {
1469 return 0; /* accept */
1472 lck_mtx_lock(ipfw_mutex
);
1474 getmicrotime(&timenow
);
1476 * dyn_dir = MATCH_UNKNOWN when rules unchecked,
1477 * MATCH_NONE when checked and not matched (q = NULL),
1478 * MATCH_FORWARD or MATCH_REVERSE otherwise (q != NULL)
1481 pktlen
= m
->m_pkthdr
.len
;
1482 if (args
->eh
== NULL
|| /* layer 3 packet */
1483 ( m
->m_pkthdr
.len
>= sizeof(struct ip
) &&
1484 ntohs(args
->eh
->ether_type
) == ETHERTYPE_IP
))
1485 hlen
= ip
->ip_hl
<< 2;
1488 * Collect parameters into local variables for faster matching.
1490 if (hlen
== 0) { /* do not grab addresses for non-ip pkts */
1491 proto
= args
->f_id
.proto
= 0; /* mark f_id invalid */
1492 goto after_ip_checks
;
1495 proto
= args
->f_id
.proto
= ip
->ip_p
;
1496 src_ip
= ip
->ip_src
;
1497 dst_ip
= ip
->ip_dst
;
1498 if (args
->eh
!= NULL
) { /* layer 2 packets are as on the wire */
1499 offset
= ntohs(ip
->ip_off
) & IP_OFFMASK
;
1500 ip_len
= ntohs(ip
->ip_len
);
1502 offset
= ip
->ip_off
& IP_OFFMASK
;
1503 ip_len
= ip
->ip_len
;
1505 pktlen
= ip_len
< pktlen
? ip_len
: pktlen
;
1507 #define PULLUP_TO(len) \
1509 if ((m)->m_len < (len)) { \
1510 args->m = m = m_pullup(m, (len)); \
1512 goto pullup_failed; \
1513 ip = mtod(m, struct ip *); \
1523 PULLUP_TO(hlen
+ sizeof(struct tcphdr
));
1524 tcp
= L3HDR(struct tcphdr
, ip
);
1525 dst_port
= tcp
->th_dport
;
1526 src_port
= tcp
->th_sport
;
1527 args
->f_id
.flags
= tcp
->th_flags
;
1535 PULLUP_TO(hlen
+ sizeof(struct udphdr
));
1536 udp
= L3HDR(struct udphdr
, ip
);
1537 dst_port
= udp
->uh_dport
;
1538 src_port
= udp
->uh_sport
;
1543 PULLUP_TO(hlen
+ 4); /* type, code and checksum. */
1544 args
->f_id
.flags
= L3HDR(struct icmp
, ip
)->icmp_type
;
1553 args
->f_id
.src_ip
= ntohl(src_ip
.s_addr
);
1554 args
->f_id
.dst_ip
= ntohl(dst_ip
.s_addr
);
1555 args
->f_id
.src_port
= src_port
= ntohs(src_port
);
1556 args
->f_id
.dst_port
= dst_port
= ntohs(dst_port
);
1561 * Packet has already been tagged. Look for the next rule
1562 * to restart processing.
1564 * If fw_one_pass != 0 then just accept it.
1565 * XXX should not happen here, but optimized out in
1569 lck_mtx_unlock(ipfw_mutex
);
1573 f
= args
->rule
->next_rule
;
1575 f
= lookup_next_rule(args
->rule
);
1578 * Find the starting rule. It can be either the first
1579 * one, or the one after divert_rule if asked so.
1581 int skipto
= args
->divert_rule
;
1584 if (args
->eh
== NULL
&& skipto
!= 0) {
1585 if (skipto
>= IPFW_DEFAULT_RULE
) {
1586 lck_mtx_unlock(ipfw_mutex
);
1587 return(IP_FW_PORT_DENY_FLAG
); /* invalid */
1589 while (f
&& f
->rulenum
<= skipto
)
1591 if (f
== NULL
) { /* drop packet */
1592 lck_mtx_unlock(ipfw_mutex
);
1593 return(IP_FW_PORT_DENY_FLAG
);
1597 args
->divert_rule
= 0; /* reset to avoid confusion later */
1600 * Now scan the rules, and parse microinstructions for each rule.
1602 for (; f
; f
= f
->next
) {
1605 int skip_or
; /* skip rest of OR block */
1608 if (f
->reserved_1
== IPFW_RULE_INACTIVE
) {
1612 if (set_disable
& (1 << f
->set
) )
1616 for (l
= f
->cmd_len
, cmd
= f
->cmd
; l
> 0 ;
1617 l
-= cmdlen
, cmd
+= cmdlen
) {
1621 * check_body is a jump target used when we find a
1622 * CHECK_STATE, and need to jump to the body of
1627 cmdlen
= F_LEN(cmd
);
1629 * An OR block (insn_1 || .. || insn_n) has the
1630 * F_OR bit set in all but the last instruction.
1631 * The first match will set "skip_or", and cause
1632 * the following instructions to be skipped until
1633 * past the one with the F_OR bit clear.
1635 if (skip_or
) { /* skip this instruction */
1636 if ((cmd
->len
& F_OR
) == 0)
1637 skip_or
= 0; /* next one is good */
1640 match
= 0; /* set to 1 if we succeed */
1642 switch (cmd
->opcode
) {
1644 * The first set of opcodes compares the packet's
1645 * fields with some pattern, setting 'match' if a
1646 * match is found. At the end of the loop there is
1647 * logic to deal with F_NOT and F_OR flags associated
1655 printf("ipfw: opcode %d unimplemented\n",
1664 * We only check offset == 0 && proto != 0,
1665 * as this ensures that we have an IPv4
1666 * packet with the ports info.
1672 struct inpcbinfo
*pi
;
1676 if (proto
== IPPROTO_TCP
) {
1679 } else if (proto
== IPPROTO_UDP
) {
1686 in_pcblookup_hash(pi
,
1687 dst_ip
, htons(dst_port
),
1688 src_ip
, htons(src_port
),
1690 in_pcblookup_hash(pi
,
1691 src_ip
, htons(src_port
),
1692 dst_ip
, htons(dst_port
),
1695 if (pcb
== NULL
|| pcb
->inp_socket
== NULL
)
1697 #if __FreeBSD_version < 500034
1698 #define socheckuid(a,b) (kauth_cred_getuid((a)->so_cred) != (b))
1700 if (cmd
->opcode
== O_UID
) {
1703 (pcb
->inp_socket
->so_uid
== (uid_t
)((ipfw_insn_u32
*)cmd
)->d
[0]);
1705 !socheckuid(pcb
->inp_socket
,
1706 (uid_t
)((ipfw_insn_u32
*)cmd
)->d
[0]);
1712 kauth_cred_ismember_gid(pcb
->inp_socket
->so_cred
,
1713 (gid_t
)((ipfw_insn_u32
*)cmd
)->d
[0], &match
);
1721 match
= iface_match(m
->m_pkthdr
.rcvif
,
1722 (ipfw_insn_if
*)cmd
);
1726 match
= iface_match(oif
, (ipfw_insn_if
*)cmd
);
1730 match
= iface_match(oif
? oif
:
1731 m
->m_pkthdr
.rcvif
, (ipfw_insn_if
*)cmd
);
1735 if (args
->eh
!= NULL
) { /* have MAC header */
1736 u_int32_t
*want
= (u_int32_t
*)
1737 ((ipfw_insn_mac
*)cmd
)->addr
;
1738 u_int32_t
*mask
= (u_int32_t
*)
1739 ((ipfw_insn_mac
*)cmd
)->mask
;
1740 u_int32_t
*hdr
= (u_int32_t
*)args
->eh
;
1743 ( want
[0] == (hdr
[0] & mask
[0]) &&
1744 want
[1] == (hdr
[1] & mask
[1]) &&
1745 want
[2] == (hdr
[2] & mask
[2]) );
1750 if (args
->eh
!= NULL
) {
1752 ntohs(args
->eh
->ether_type
);
1754 ((ipfw_insn_u16
*)cmd
)->ports
;
1757 for (i
= cmdlen
- 1; !match
&& i
>0;
1759 match
= (t
>=p
[0] && t
<=p
[1]);
1764 match
= (hlen
> 0 && offset
!= 0);
1767 case O_IN
: /* "out" is "not in" */
1768 match
= (oif
== NULL
);
1772 match
= (args
->eh
!= NULL
);
1777 * We do not allow an arg of 0 so the
1778 * check of "proto" only suffices.
1780 match
= (proto
== cmd
->arg1
);
1784 match
= (hlen
> 0 &&
1785 ((ipfw_insn_ip
*)cmd
)->addr
.s_addr
==
1793 (cmd
->opcode
== O_IP_DST_MASK
) ?
1794 dst_ip
.s_addr
: src_ip
.s_addr
;
1795 uint32_t *p
= ((ipfw_insn_u32
*)cmd
)->d
;
1798 for (; !match
&& i
>0; i
-= 2, p
+= 2)
1799 match
= (p
[0] == (a
& p
[1]));
1807 INADDR_TO_IFP(src_ip
, tif
);
1808 match
= (tif
!= NULL
);
1815 u_int32_t
*d
= (u_int32_t
*)(cmd
+1);
1817 cmd
->opcode
== O_IP_DST_SET
?
1823 addr
-= d
[0]; /* subtract base */
1824 match
= (addr
< cmd
->arg1
) &&
1825 ( d
[ 1 + (addr
>>5)] &
1826 (1<<(addr
& 0x1f)) );
1831 match
= (hlen
> 0 &&
1832 ((ipfw_insn_ip
*)cmd
)->addr
.s_addr
==
1840 INADDR_TO_IFP(dst_ip
, tif
);
1841 match
= (tif
!= NULL
);
1848 * offset == 0 && proto != 0 is enough
1849 * to guarantee that we have an IPv4
1850 * packet with port info.
1852 if ((proto
==IPPROTO_UDP
|| proto
==IPPROTO_TCP
)
1855 (cmd
->opcode
== O_IP_SRCPORT
) ?
1856 src_port
: dst_port
;
1858 ((ipfw_insn_u16
*)cmd
)->ports
;
1861 for (i
= cmdlen
- 1; !match
&& i
>0;
1863 match
= (x
>=p
[0] && x
<=p
[1]);
1868 match
= (offset
== 0 && proto
==IPPROTO_ICMP
&&
1869 icmptype_match(ip
, (ipfw_insn_u32
*)cmd
) );
1873 match
= (hlen
> 0 && ipopts_match(ip
, cmd
) );
1877 match
= (hlen
> 0 && cmd
->arg1
== ip
->ip_v
);
1883 if (hlen
> 0) { /* only for IP packets */
1888 if (cmd
->opcode
== O_IPLEN
)
1890 else if (cmd
->opcode
== O_IPTTL
)
1892 else /* must be IPID */
1893 x
= ntohs(ip
->ip_id
);
1895 match
= (cmd
->arg1
== x
);
1898 /* otherwise we have ranges */
1899 p
= ((ipfw_insn_u16
*)cmd
)->ports
;
1901 for (; !match
&& i
>0; i
--, p
+= 2)
1902 match
= (x
>= p
[0] && x
<= p
[1]);
1906 case O_IPPRECEDENCE
:
1907 match
= (hlen
> 0 &&
1908 (cmd
->arg1
== (ip
->ip_tos
& 0xe0)) );
1912 match
= (hlen
> 0 &&
1913 flags_match(cmd
, ip
->ip_tos
));
1917 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1919 L3HDR(struct tcphdr
,ip
)->th_flags
));
1923 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1924 tcpopts_match(ip
, cmd
));
1928 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1929 ((ipfw_insn_u32
*)cmd
)->d
[0] ==
1930 L3HDR(struct tcphdr
,ip
)->th_seq
);
1934 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1935 ((ipfw_insn_u32
*)cmd
)->d
[0] ==
1936 L3HDR(struct tcphdr
,ip
)->th_ack
);
1940 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1942 L3HDR(struct tcphdr
,ip
)->th_win
);
1946 /* reject packets which have SYN only */
1947 /* XXX should i also check for TH_ACK ? */
1948 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1949 (L3HDR(struct tcphdr
,ip
)->th_flags
&
1950 (TH_RST
| TH_ACK
| TH_SYN
)) != TH_SYN
);
1955 ipfw_log(f
, hlen
, args
->eh
, m
, oif
);
1960 match
= (random()<((ipfw_insn_u32
*)cmd
)->d
[0]);
1964 /* Outgoing packets automatically pass/match */
1965 match
= ((oif
!= NULL
) ||
1966 (m
->m_pkthdr
.rcvif
== NULL
) ||
1967 verify_rev_path(src_ip
, m
->m_pkthdr
.rcvif
));
1972 match
= (m_tag_find(m
,
1973 PACKET_TAG_IPSEC_IN_DONE
, NULL
) != NULL
);
1976 match
= (ipsec_gethist(m
, NULL
) != NULL
);
1978 /* otherwise no match */
1982 * The second set of opcodes represents 'actions',
1983 * i.e. the terminal part of a rule once the packet
1984 * matches all previous patterns.
1985 * Typically there is only one action for each rule,
1986 * and the opcode is stored at the end of the rule
1987 * (but there are exceptions -- see below).
1989 * In general, here we set retval and terminate the
1990 * outer loop (would be a 'break 3' in some language,
1991 * but we need to do a 'goto done').
1994 * O_COUNT and O_SKIPTO actions:
1995 * instead of terminating, we jump to the next rule
1996 * ('goto next_rule', equivalent to a 'break 2'),
1997 * or to the SKIPTO target ('goto again' after
1998 * having set f, cmd and l), respectively.
2000 * O_LIMIT and O_KEEP_STATE: these opcodes are
2001 * not real 'actions', and are stored right
2002 * before the 'action' part of the rule.
2003 * These opcodes try to install an entry in the
2004 * state tables; if successful, we continue with
2005 * the next opcode (match=1; break;), otherwise
2006 * the packet * must be dropped
2007 * ('goto done' after setting retval);
2009 * O_PROBE_STATE and O_CHECK_STATE: these opcodes
2010 * cause a lookup of the state table, and a jump
2011 * to the 'action' part of the parent rule
2012 * ('goto check_body') if an entry is found, or
2013 * (CHECK_STATE only) a jump to the next rule if
2014 * the entry is not found ('goto next_rule').
2015 * The result of the lookup is cached to make
2016 * further instances of these opcodes are
2021 if (install_state(f
,
2022 (ipfw_insn_limit
*)cmd
, args
)) {
2023 retval
= IP_FW_PORT_DENY_FLAG
;
2024 goto done
; /* error/limit violation */
2032 * dynamic rules are checked at the first
2033 * keep-state or check-state occurrence,
2034 * with the result being stored in dyn_dir.
2035 * The compiler introduces a PROBE_STATE
2036 * instruction for us when we have a
2037 * KEEP_STATE (because PROBE_STATE needs
2040 if (dyn_dir
== MATCH_UNKNOWN
&&
2041 (q
= lookup_dyn_rule(&args
->f_id
,
2042 &dyn_dir
, proto
== IPPROTO_TCP
?
2043 L3HDR(struct tcphdr
, ip
) : NULL
))
2046 * Found dynamic entry, update stats
2047 * and jump to the 'action' part of
2053 cmd
= ACTION_PTR(f
);
2054 l
= f
->cmd_len
- f
->act_ofs
;
2058 * Dynamic entry not found. If CHECK_STATE,
2059 * skip to next rule, if PROBE_STATE just
2060 * ignore and continue with next opcode.
2062 if (cmd
->opcode
== O_CHECK_STATE
)
2068 retval
= 0; /* accept */
2073 args
->rule
= f
; /* report matching rule */
2074 retval
= cmd
->arg1
| IP_FW_PORT_DYNT_FLAG
;
2079 if (args
->eh
) /* not on layer 2 */
2081 args
->divert_rule
= f
->rulenum
;
2082 retval
= (cmd
->opcode
== O_DIVERT
) ?
2084 cmd
->arg1
| IP_FW_PORT_TEE_FLAG
;
2089 f
->pcnt
++; /* update stats */
2091 f
->timestamp
= timenow
.tv_sec
;
2092 if (cmd
->opcode
== O_COUNT
)
2095 if (f
->next_rule
== NULL
)
2096 lookup_next_rule(f
);
2102 * Drop the packet and send a reject notice
2103 * if the packet is not ICMP (or is an ICMP
2104 * query), and it is not multicast/broadcast.
2107 (proto
!= IPPROTO_ICMP
||
2108 is_icmp_query(ip
)) &&
2109 !(m
->m_flags
& (M_BCAST
|M_MCAST
)) &&
2110 !IN_MULTICAST(dst_ip
.s_addr
)) {
2111 send_reject(args
, cmd
->arg1
,
2117 retval
= IP_FW_PORT_DENY_FLAG
;
2121 if (args
->eh
) /* not valid on layer2 pkts */
2123 if (!q
|| dyn_dir
== MATCH_FORWARD
)
2125 &((ipfw_insn_sa
*)cmd
)->sa
;
2130 panic("-- unknown opcode %d\n", cmd
->opcode
);
2131 } /* end of switch() on opcodes */
2133 if (cmd
->len
& F_NOT
)
2137 if (cmd
->len
& F_OR
)
2140 if (!(cmd
->len
& F_OR
)) /* not an OR block, */
2141 break; /* try next rule */
2144 } /* end of inner for, scan opcodes */
2146 next_rule
:; /* try next rule */
2148 } /* end of outer for, scan rules */
2149 printf("ipfw: ouch!, skip past end of rules, denying packet\n");
2150 lck_mtx_unlock(ipfw_mutex
);
2151 return(IP_FW_PORT_DENY_FLAG
);
2154 /* Update statistics */
2157 f
->timestamp
= timenow
.tv_sec
;
2158 lck_mtx_unlock(ipfw_mutex
);
2163 printf("ipfw: pullup failed\n");
2164 lck_mtx_unlock(ipfw_mutex
);
2165 return(IP_FW_PORT_DENY_FLAG
);
2169 * When a rule is added/deleted, clear the next_rule pointers in all rules.
2170 * These will be reconstructed on the fly as packets are matched.
2171 * Must be called at splimp().
2174 flush_rule_ptrs(void)
2178 for (rule
= layer3_chain
; rule
; rule
= rule
->next
)
2179 rule
->next_rule
= NULL
;
2183 * When pipes/queues are deleted, clear the "pipe_ptr" pointer to a given
2184 * pipe/queue, or to all of them (match == NULL).
2185 * Must be called at splimp().
2188 flush_pipe_ptrs(struct dn_flow_set
*match
)
2192 for (rule
= layer3_chain
; rule
; rule
= rule
->next
) {
2193 ipfw_insn_pipe
*cmd
= (ipfw_insn_pipe
*)ACTION_PTR(rule
);
2195 if (cmd
->o
.opcode
!= O_PIPE
&& cmd
->o
.opcode
!= O_QUEUE
)
2198 * XXX Use bcmp/bzero to handle pipe_ptr to overcome
2199 * possible alignment problems on 64-bit architectures.
2200 * This code is seldom used so we do not worry too
2201 * much about efficiency.
2203 if (match
== NULL
||
2204 !bcmp(&cmd
->pipe_ptr
, &match
, sizeof(match
)) )
2205 bzero(&cmd
->pipe_ptr
, sizeof(cmd
->pipe_ptr
));
2210 * Add a new rule to the list. Copy the rule into a malloc'ed area, then
2211 * possibly create a rule number and add the rule to the list.
2212 * Update the rule_number in the input struct so the caller knows it as well.
2215 add_rule(struct ip_fw
**head
, struct ip_fw
*input_rule
)
2217 struct ip_fw
*rule
, *f
, *prev
;
2219 int l
= RULESIZE(input_rule
);
2221 if (*head
== NULL
&& input_rule
->rulenum
!= IPFW_DEFAULT_RULE
)
2224 rule
= _MALLOC(l
, M_IPFW
, M_WAIT
);
2226 printf("ipfw2: add_rule MALLOC failed\n");
2231 bcopy(input_rule
, rule
, l
);
2234 rule
->next_rule
= NULL
;
2238 rule
->timestamp
= 0;
2240 if (*head
== NULL
) { /* default rule */
2246 * If rulenum is 0, find highest numbered rule before the
2247 * default rule, and add autoinc_step
2249 if (autoinc_step
< 1)
2251 else if (autoinc_step
> 1000)
2252 autoinc_step
= 1000;
2253 if (rule
->rulenum
== 0) {
2255 * locate the highest numbered rule before default
2257 for (f
= *head
; f
; f
= f
->next
) {
2258 if (f
->rulenum
== IPFW_DEFAULT_RULE
)
2260 rule
->rulenum
= f
->rulenum
;
2262 if (rule
->rulenum
< IPFW_DEFAULT_RULE
- autoinc_step
)
2263 rule
->rulenum
+= autoinc_step
;
2264 input_rule
->rulenum
= rule
->rulenum
;
2268 * Now insert the new rule in the right place in the sorted list.
2270 for (prev
= NULL
, f
= *head
; f
; prev
= f
, f
= f
->next
) {
2271 if (f
->rulenum
> rule
->rulenum
) { /* found the location */
2275 } else { /* head insert */
2286 DEB(printf("ipfw: installed rule %d, static count now %d\n",
2287 rule
->rulenum
, static_count
);)
2292 * Free storage associated with a static rule (including derived
2294 * The caller is in charge of clearing rule pointers to avoid
2295 * dangling pointers.
2296 * @return a pointer to the next entry.
2297 * Arguments are not checked, so they better be correct.
2298 * Must be called at splimp().
2300 static struct ip_fw
*
2301 delete_rule(struct ip_fw
**head
, struct ip_fw
*prev
, struct ip_fw
*rule
)
2304 int l
= RULESIZE(rule
);
2307 remove_dyn_rule(rule
, NULL
/* force removal */);
2316 if (DUMMYNET_LOADED
)
2317 ip_dn_ruledel_ptr(rule
);
2318 #endif /* DUMMYNET */
2319 _FREE(rule
, M_IPFW
);
2323 #if DEBUG_INACTIVE_RULES
2325 print_chain(struct ip_fw
**chain
)
2327 struct ip_fw
*rule
= *chain
;
2329 for (; rule
; rule
= rule
->next
) {
2330 ipfw_insn
*cmd
= ACTION_PTR(rule
);
2332 printf("ipfw: rule->rulenum = %d\n", rule
->rulenum
);
2334 if (rule
->reserved_1
== IPFW_RULE_INACTIVE
) {
2335 printf("ipfw: rule->reserved = IPFW_RULE_INACTIVE\n");
2338 switch (cmd
->opcode
) {
2340 printf("ipfw: ACTION: Deny\n");
2344 if (cmd
->arg1
==ICMP_REJECT_RST
)
2345 printf("ipfw: ACTION: Reset\n");
2346 else if (cmd
->arg1
==ICMP_UNREACH_HOST
)
2347 printf("ipfw: ACTION: Reject\n");
2351 printf("ipfw: ACTION: Accept\n");
2354 printf("ipfw: ACTION: Count\n");
2357 printf("ipfw: ACTION: Divert\n");
2360 printf("ipfw: ACTION: Tee\n");
2363 printf("ipfw: ACTION: SkipTo\n");
2366 printf("ipfw: ACTION: Pipe\n");
2369 printf("ipfw: ACTION: Queue\n");
2372 printf("ipfw: ACTION: Forward\n");
2375 printf("ipfw: invalid action! %d\n", cmd
->opcode
);
2379 #endif /* DEBUG_INACTIVE_RULES */
2382 flush_inactive(void *param
)
2384 struct ip_fw
*inactive_rule
= (struct ip_fw
*)param
;
2385 struct ip_fw
*rule
, *prev
;
2387 lck_mtx_lock(ipfw_mutex
);
2389 for (rule
= layer3_chain
, prev
= NULL
; rule
; ) {
2390 if (rule
== inactive_rule
&& rule
->reserved_1
== IPFW_RULE_INACTIVE
) {
2391 struct ip_fw
*n
= rule
;
2394 layer3_chain
= rule
->next
;
2397 prev
->next
= rule
->next
;
2408 #if DEBUG_INACTIVE_RULES
2409 print_chain(&layer3_chain
);
2411 lck_mtx_unlock(ipfw_mutex
);
2415 mark_inactive(struct ip_fw
**prev
, struct ip_fw
**rule
)
2417 int l
= RULESIZE(*rule
);
2419 if ((*rule
)->reserved_1
!= IPFW_RULE_INACTIVE
) {
2420 (*rule
)->reserved_1
= IPFW_RULE_INACTIVE
;
2424 timeout(flush_inactive
, *rule
, 30*hz
); /* 30 sec. */
2428 *rule
= (*rule
)->next
;
2432 * Deletes all rules from a chain (except rules in set RESVD_SET
2433 * unless kill_default = 1).
2434 * Must be called at splimp().
2437 free_chain(struct ip_fw
**chain
, int kill_default
)
2439 struct ip_fw
*prev
, *rule
;
2441 flush_rule_ptrs(); /* more efficient to do outside the loop */
2442 for (prev
= NULL
, rule
= *chain
; rule
; )
2443 if (kill_default
|| rule
->set
!= RESVD_SET
) {
2444 ipfw_insn
*cmd
= ACTION_PTR(rule
);
2446 /* skip over forwarding rules so struct isn't
2447 * deleted while pointer is still in use elsewhere
2449 if (cmd
->opcode
== O_FORWARD_IP
) {
2450 mark_inactive(&prev
, &rule
);
2453 rule
= delete_rule(chain
, prev
, rule
);
2463 * Remove all rules with given number, and also do set manipulation.
2464 * Assumes chain != NULL && *chain != NULL.
2466 * The argument is an u_int32_t. The low 16 bit are the rule or set number,
2467 * the next 8 bits are the new set, the top 8 bits are the command:
2469 * 0 delete rules with given number
2470 * 1 delete rules with given set number
2471 * 2 move rules with given number to new set
2472 * 3 move rules with given set number to new set
2473 * 4 swap sets with given numbers
2476 del_entry(struct ip_fw
**chain
, u_int32_t arg
)
2478 struct ip_fw
*prev
= NULL
, *rule
= *chain
;
2480 u_int16_t rulenum
; /* rule or old_set */
2481 u_int8_t cmd
, new_set
;
2483 rulenum
= arg
& 0xffff;
2484 cmd
= (arg
>> 24) & 0xff;
2485 new_set
= (arg
>> 16) & 0xff;
2489 if (new_set
> RESVD_SET
)
2491 if (cmd
== 0 || cmd
== 2) {
2492 if (rulenum
>= IPFW_DEFAULT_RULE
)
2495 if (rulenum
> RESVD_SET
) /* old_set */
2500 case 0: /* delete rules with given number */
2502 * locate first rule to delete
2504 for (; rule
->rulenum
< rulenum
; prev
= rule
, rule
= rule
->next
)
2506 if (rule
->rulenum
!= rulenum
)
2510 * flush pointers outside the loop, then delete all matching
2511 * rules. prev remains the same throughout the cycle.
2514 while (rule
->rulenum
== rulenum
) {
2515 ipfw_insn
*cmd
= ACTION_PTR(rule
);
2517 /* keep forwarding rules around so struct isn't
2518 * deleted while pointer is still in use elsewhere
2520 if (cmd
->opcode
== O_FORWARD_IP
) {
2521 mark_inactive(&prev
, &rule
);
2524 rule
= delete_rule(chain
, prev
, rule
);
2529 case 1: /* delete all rules with given set number */
2531 while (rule
->rulenum
< IPFW_DEFAULT_RULE
) {
2532 if (rule
->set
== rulenum
) {
2533 ipfw_insn
*cmd
= ACTION_PTR(rule
);
2535 /* keep forwarding rules around so struct isn't
2536 * deleted while pointer is still in use elsewhere
2538 if (cmd
->opcode
== O_FORWARD_IP
) {
2539 mark_inactive(&prev
, &rule
);
2542 rule
= delete_rule(chain
, prev
, rule
);
2552 case 2: /* move rules with given number to new set */
2553 for (; rule
->rulenum
< IPFW_DEFAULT_RULE
; rule
= rule
->next
)
2554 if (rule
->rulenum
== rulenum
)
2555 rule
->set
= new_set
;
2558 case 3: /* move rules with given set number to new set */
2559 for (; rule
->rulenum
< IPFW_DEFAULT_RULE
; rule
= rule
->next
)
2560 if (rule
->set
== rulenum
)
2561 rule
->set
= new_set
;
2564 case 4: /* swap two sets */
2565 for (; rule
->rulenum
< IPFW_DEFAULT_RULE
; rule
= rule
->next
)
2566 if (rule
->set
== rulenum
)
2567 rule
->set
= new_set
;
2568 else if (rule
->set
== new_set
)
2569 rule
->set
= rulenum
;
2576 * Clear counters for a specific rule.
2579 clear_counters(struct ip_fw
*rule
, int log_only
)
2581 ipfw_insn_log
*l
= (ipfw_insn_log
*)ACTION_PTR(rule
);
2583 if (log_only
== 0) {
2584 rule
->bcnt
= rule
->pcnt
= 0;
2585 rule
->timestamp
= 0;
2587 if (l
->o
.opcode
== O_LOG
)
2588 l
->log_left
= l
->max_log
;
2592 * Reset some or all counters on firewall rules.
2593 * @arg frwl is null to clear all entries, or contains a specific
2595 * @arg log_only is 1 if we only want to reset logs, zero otherwise.
2598 zero_entry(int rulenum
, int log_only
)
2606 for (rule
= layer3_chain
; rule
; rule
= rule
->next
)
2607 clear_counters(rule
, log_only
);
2608 msg
= log_only
? "ipfw: All logging counts reset.\n" :
2609 "ipfw: Accounting cleared.\n";
2613 * We can have multiple rules with the same number, so we
2614 * need to clear them all.
2616 for (rule
= layer3_chain
; rule
; rule
= rule
->next
)
2617 if (rule
->rulenum
== rulenum
) {
2618 while (rule
&& rule
->rulenum
== rulenum
) {
2619 clear_counters(rule
, log_only
);
2625 if (!cleared
) /* we did not find any matching rules */
2627 msg
= log_only
? "ipfw: Entry %d logging count reset.\n" :
2628 "ipfw: Entry %d cleared.\n";
2632 dolog((LOG_AUTHPRIV
| LOG_NOTICE
, msg
, rulenum
));
2638 * Check validity of the structure before insert.
2639 * Fortunately rules are simple, so this mostly need to check rule sizes.
2642 check_ipfw_struct(struct ip_fw
*rule
, int size
)
2648 if (size
< sizeof(*rule
)) {
2649 printf("ipfw: rule too short\n");
2652 /* first, check for valid size */
2655 printf("ipfw: size mismatch (have %d want %d)\n", size
, l
);
2659 * Now go for the individual checks. Very simple ones, basically only
2660 * instruction sizes.
2662 for (l
= rule
->cmd_len
, cmd
= rule
->cmd
;
2663 l
> 0 ; l
-= cmdlen
, cmd
+= cmdlen
) {
2664 cmdlen
= F_LEN(cmd
);
2666 printf("ipfw: opcode %d size truncated\n",
2670 DEB(printf("ipfw: opcode %d\n", cmd
->opcode
);)
2671 switch (cmd
->opcode
) {
2682 case O_IPPRECEDENCE
:
2690 if (cmdlen
!= F_INSN_SIZE(ipfw_insn
))
2696 #endif /* __APPLE__ */
2703 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_u32
))
2708 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_limit
))
2713 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_log
))
2716 /* enforce logging limit */
2718 ((ipfw_insn_log
*)cmd
)->max_log
== 0 && verbose_limit
!= 0) {
2719 ((ipfw_insn_log
*)cmd
)->max_log
= verbose_limit
;
2722 ((ipfw_insn_log
*)cmd
)->log_left
=
2723 ((ipfw_insn_log
*)cmd
)->max_log
;
2729 /* only odd command lengths */
2730 if ( !(cmdlen
& 1) || cmdlen
> 31)
2736 if (cmd
->arg1
== 0 || cmd
->arg1
> 256) {
2737 printf("ipfw: invalid set size %d\n",
2741 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_u32
) +
2747 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_mac
))
2755 if (cmdlen
< 1 || cmdlen
> 31)
2761 case O_IP_DSTPORT
: /* XXX artificial limit, 30 port pairs */
2762 if (cmdlen
< 2 || cmdlen
> 31)
2769 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_if
))
2775 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_pipe
))
2780 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_sa
))
2784 case O_FORWARD_MAC
: /* XXX not implemented yet */
2793 if (cmdlen
!= F_INSN_SIZE(ipfw_insn
))
2797 printf("ipfw: opcode %d, multiple actions"
2804 printf("ipfw: opcode %d, action must be"
2811 printf("ipfw: opcode %d, unknown opcode\n",
2816 if (have_action
== 0) {
2817 printf("ipfw: missing action\n");
2823 printf("ipfw: opcode %d size %d wrong\n",
2824 cmd
->opcode
, cmdlen
);
2830 * {set|get}sockopt parser.
2833 ipfw_ctl(struct sockopt
*sopt
)
2835 #define RULE_MAXSIZE (256*sizeof(u_int32_t))
2836 u_int32_t api_version
;
2840 struct ip_fw
*bp
, *buf
, *rule
;
2842 /* copy of orig sopt to send to ipfw_get_command_and_version() */
2843 struct sockopt tmp_sopt
= *sopt
;
2844 struct timeval timenow
;
2846 getmicrotime(&timenow
);
2849 * Disallow modifications in really-really secure mode, but still allow
2850 * the logging counters to be reset.
2852 if (sopt
->sopt_name
== IP_FW_ADD
||
2853 (sopt
->sopt_dir
== SOPT_SET
&& sopt
->sopt_name
!= IP_FW_RESETLOG
)) {
2854 #if __FreeBSD_version >= 500034
2855 error
= securelevel_ge(sopt
->sopt_td
->td_ucred
, 3);
2858 #else /* FreeBSD 4.x */
2859 if (securelevel
>= 3)
2864 /* first get the command and version, then do conversion as necessary */
2865 error
= ipfw_get_command_and_version(&tmp_sopt
, &command
, &api_version
);
2868 /* error getting the version */
2875 * pass up a copy of the current rules. Static rules
2876 * come first (the last of which has number IPFW_DEFAULT_RULE),
2877 * followed by a possibly empty list of dynamic rule.
2878 * The last dynamic rule has NULL in the "next" field.
2880 lck_mtx_lock(ipfw_mutex
);
2881 size
= static_len
; /* size of static rules */
2882 if (ipfw_dyn_v
) /* add size of dyn.rules */
2883 size
+= (dyn_count
* sizeof(ipfw_dyn_rule
));
2886 * XXX todo: if the user passes a short length just to know
2887 * how much room is needed, do not bother filling up the
2888 * buffer, just jump to the sooptcopyout.
2890 buf
= _MALLOC(size
, M_TEMP
, M_WAITOK
);
2892 lck_mtx_unlock(ipfw_mutex
);
2900 for (rule
= layer3_chain
; rule
; rule
= rule
->next
) {
2901 int i
= RULESIZE(rule
);
2903 if (rule
->reserved_1
== IPFW_RULE_INACTIVE
) {
2907 bcopy(&set_disable
, &(bp
->next_rule
),
2908 sizeof(set_disable
));
2909 bp
= (struct ip_fw
*)((char *)bp
+ i
);
2913 ipfw_dyn_rule
*p
, *dst
, *last
= NULL
;
2915 dst
= (ipfw_dyn_rule
*)bp
;
2916 for (i
= 0 ; i
< curr_dyn_buckets
; i
++ )
2917 for ( p
= ipfw_dyn_v
[i
] ; p
!= NULL
;
2918 p
= p
->next
, dst
++ ) {
2919 bcopy(p
, dst
, sizeof *p
);
2920 bcopy(&(p
->rule
->rulenum
), &(dst
->rule
),
2921 sizeof(p
->rule
->rulenum
));
2923 * store a non-null value in "next".
2924 * The userland code will interpret a
2925 * NULL here as a marker
2926 * for the last dynamic rule.
2928 bcopy(&dst
, &dst
->next
, sizeof(dst
));
2931 TIME_LEQ(dst
->expire
, timenow
.tv_sec
) ?
2932 0 : dst
->expire
- timenow
.tv_sec
;
2934 if (last
!= NULL
) /* mark last dynamic rule */
2935 bzero(&last
->next
, sizeof(last
));
2937 lck_mtx_unlock(ipfw_mutex
);
2939 /* convert back if necessary and copyout */
2940 if (api_version
== IP_FW_VERSION_0
) {
2942 struct ip_old_fw
*buf2
, *rule_vers0
;
2944 buf2
= _MALLOC(static_count
* sizeof(struct ip_old_fw
), M_TEMP
, M_WAITOK
);
2953 for (i
= 0; i
< static_count
; i
++) {
2954 /* static rules have different sizes */
2955 int j
= RULESIZE(bp
);
2956 ipfw_convert_from_latest(bp
, rule_vers0
, api_version
);
2957 bp
= (struct ip_fw
*)((char *)bp
+ j
);
2958 len
+= sizeof(*rule_vers0
);
2961 error
= sooptcopyout(sopt
, buf2
, len
);
2962 _FREE(buf2
, M_TEMP
);
2964 } else if (api_version
== IP_FW_VERSION_1
) {
2965 int i
, len
= 0, buf_size
;
2966 struct ip_fw_compat
*buf2
, *rule_vers1
;
2967 struct ipfw_dyn_rule_compat
*dyn_rule_vers1
, *dyn_last
= NULL
;
2970 buf_size
= static_count
* sizeof(struct ip_fw_compat
) +
2971 dyn_count
* sizeof(struct ipfw_dyn_rule_compat
);
2973 buf2
= _MALLOC(buf_size
, M_TEMP
, M_WAITOK
);
2982 /* first do static rules */
2983 for (i
= 0; i
< static_count
; i
++) {
2984 /* static rules have different sizes */
2985 int j
= RULESIZE(bp
);
2986 ipfw_convert_from_latest(bp
, rule_vers1
, api_version
);
2987 bp
= (struct ip_fw
*)((char *)bp
+ j
);
2988 len
+= sizeof(*rule_vers1
);
2992 /* now do dynamic rules */
2993 dyn_rule_vers1
= (struct ipfw_dyn_rule_compat
*)rule_vers1
;
2995 for (i
= 0; i
< curr_dyn_buckets
; i
++) {
2996 for ( p
= ipfw_dyn_v
[i
] ; p
!= NULL
; p
= p
->next
) {
2997 (int) dyn_rule_vers1
->chain
= p
->rule
->rulenum
;
2998 dyn_rule_vers1
->id
= p
->id
;
2999 dyn_rule_vers1
->mask
= p
->id
;
3000 dyn_rule_vers1
->type
= p
->dyn_type
;
3001 dyn_rule_vers1
->expire
= p
->expire
;
3002 dyn_rule_vers1
->pcnt
= p
->pcnt
;
3003 dyn_rule_vers1
->bcnt
= p
->bcnt
;
3004 dyn_rule_vers1
->bucket
= p
->bucket
;
3005 dyn_rule_vers1
->state
= p
->state
;
3007 dyn_rule_vers1
->next
= dyn_rule_vers1
;
3008 dyn_last
= dyn_rule_vers1
;
3010 len
+= sizeof(*dyn_rule_vers1
);
3015 if (dyn_last
!= NULL
) {
3016 dyn_last
->next
= NULL
;
3020 error
= sooptcopyout(sopt
, buf2
, len
);
3021 _FREE(buf2
, M_TEMP
);
3024 error
= sooptcopyout(sopt
, buf
, size
);
3032 * Normally we cannot release the lock on each iteration.
3033 * We could do it here only because we start from the head all
3034 * the times so there is no risk of missing some entries.
3035 * On the other hand, the risk is that we end up with
3036 * a very inconsistent ruleset, so better keep the lock
3037 * around the whole cycle.
3039 * XXX this code can be improved by resetting the head of
3040 * the list to point to the default rule, and then freeing
3041 * the old list without the need for a lock.
3044 lck_mtx_lock(ipfw_mutex
);
3045 free_chain(&layer3_chain
, 0 /* keep default rule */);
3046 #if DEBUG_INACTIVE_RULES
3047 print_chain(&layer3_chain
);
3049 lck_mtx_unlock(ipfw_mutex
);
3053 rule
= _MALLOC(RULE_MAXSIZE
, M_TEMP
, M_WAITOK
);
3059 bzero(rule
, RULE_MAXSIZE
);
3061 if (api_version
!= IP_FW_CURRENT_API_VERSION
) {
3062 error
= ipfw_convert_to_latest(sopt
, rule
, api_version
);
3065 error
= sooptcopyin(sopt
, rule
, RULE_MAXSIZE
,
3066 sizeof(struct ip_fw
) );
3070 if ((api_version
== IP_FW_VERSION_0
) || (api_version
== IP_FW_VERSION_1
)) {
3071 /* the rule has already been checked so just
3072 * adjust sopt_valsize to match what would be expected.
3074 sopt
->sopt_valsize
= RULESIZE(rule
);
3076 error
= check_ipfw_struct(rule
, sopt
->sopt_valsize
);
3078 lck_mtx_lock(ipfw_mutex
);
3079 error
= add_rule(&layer3_chain
, rule
);
3080 lck_mtx_unlock(ipfw_mutex
);
3082 size
= RULESIZE(rule
);
3083 if (!error
&& sopt
->sopt_dir
== SOPT_GET
) {
3084 /* convert back if necessary and copyout */
3085 if (api_version
== IP_FW_VERSION_0
) {
3086 struct ip_old_fw rule_vers0
;
3088 ipfw_convert_from_latest(rule
, &rule_vers0
, api_version
);
3089 sopt
->sopt_valsize
= sizeof(struct ip_old_fw
);
3091 error
= sooptcopyout(sopt
, &rule_vers0
, sizeof(struct ip_old_fw
));
3092 } else if (api_version
== IP_FW_VERSION_1
) {
3093 struct ip_fw_compat rule_vers1
;
3095 ipfw_convert_from_latest(rule
, &rule_vers1
, api_version
);
3096 sopt
->sopt_valsize
= sizeof(struct ip_fw_compat
);
3098 error
= sooptcopyout(sopt
, &rule_vers1
, sizeof(struct ip_fw_compat
));
3100 error
= sooptcopyout(sopt
, rule
, size
);
3106 _FREE(rule
, M_TEMP
);
3112 * IP_FW_DEL is used for deleting single rules or sets,
3113 * and (ab)used to atomically manipulate sets.
3114 * rule->rulenum != 0 indicates single rule delete
3115 * rule->set_masks used to manipulate sets
3116 * rule->set_masks[0] contains info on sets to be
3117 * disabled, swapped, or moved
3118 * rule->set_masks[1] contains sets to be enabled.
3121 /* there is only a simple rule passed in
3122 * (no cmds), so use a temp struct to copy
3124 struct ip_fw temp_rule
;
3128 bzero(&temp_rule
, sizeof(struct ip_fw
));
3129 if (api_version
!= IP_FW_CURRENT_API_VERSION
) {
3130 error
= ipfw_convert_to_latest(sopt
, &temp_rule
, api_version
);
3133 error
= sooptcopyin(sopt
, &temp_rule
, sizeof(struct ip_fw
),
3134 sizeof(struct ip_fw
) );
3138 /* set_masks is used to distinguish between deleting
3139 * single rules or atomically manipulating sets
3141 lck_mtx_lock(ipfw_mutex
);
3143 arg
= temp_rule
.set_masks
[0];
3144 cmd
= (arg
>> 24) & 0xff;
3146 if (temp_rule
.rulenum
) {
3148 error
= del_entry(&layer3_chain
, temp_rule
.rulenum
);
3149 #if DEBUG_INACTIVE_RULES
3150 print_chain(&layer3_chain
);
3154 /* set reassignment - see comment above del_entry() for details */
3155 error
= del_entry(&layer3_chain
, temp_rule
.set_masks
[0]);
3156 #if DEBUG_INACTIVE_RULES
3157 print_chain(&layer3_chain
);
3160 else if (temp_rule
.set_masks
[0] != 0 ||
3161 temp_rule
.set_masks
[1] != 0) {
3162 /* set enable/disable */
3164 (set_disable
| temp_rule
.set_masks
[0]) & ~temp_rule
.set_masks
[1] &
3165 ~(1<<RESVD_SET
); /* set RESVD_SET always enabled */
3168 lck_mtx_unlock(ipfw_mutex
);
3173 case IP_FW_RESETLOG
: /* using rule->rulenum */
3175 /* there is only a simple rule passed in
3176 * (no cmds), so use a temp struct to copy
3178 struct ip_fw temp_rule
= { 0 };
3180 if (api_version
!= IP_FW_CURRENT_API_VERSION
) {
3181 error
= ipfw_convert_to_latest(sopt
, &temp_rule
, api_version
);
3184 if (sopt
->sopt_val
!= 0) {
3185 error
= sooptcopyin(sopt
, &temp_rule
, sizeof(struct ip_fw
),
3186 sizeof(struct ip_fw
) );
3191 lck_mtx_lock(ipfw_mutex
);
3192 error
= zero_entry(temp_rule
.rulenum
, sopt
->sopt_name
== IP_FW_RESETLOG
);
3193 lck_mtx_unlock(ipfw_mutex
);
3198 printf("ipfw: ipfw_ctl invalid option %d\n", sopt
->sopt_name
);
3206 * dummynet needs a reference to the default rule, because rules can be
3207 * deleted while packets hold a reference to them. When this happens,
3208 * dummynet changes the reference to the default rule (it could well be a
3209 * NULL pointer, but this way we do not need to check for the special
3210 * case, plus here he have info on the default behaviour).
3212 struct ip_fw
*ip_fw_default_rule
;
3215 * This procedure is only used to handle keepalives. It is invoked
3216 * every dyn_keepalive_period
3219 ipfw_tick(void * __unused unused
)
3224 struct timeval timenow
;
3227 if (dyn_keepalive
== 0 || ipfw_dyn_v
== NULL
|| dyn_count
== 0)
3230 getmicrotime(&timenow
);
3232 lck_mtx_lock(ipfw_mutex
);
3233 for (i
= 0 ; i
< curr_dyn_buckets
; i
++) {
3234 for (q
= ipfw_dyn_v
[i
] ; q
; q
= q
->next
) {
3235 if (q
->dyn_type
== O_LIMIT_PARENT
)
3237 if (q
->id
.proto
!= IPPROTO_TCP
)
3239 if ( (q
->state
& BOTH_SYN
) != BOTH_SYN
)
3241 if (TIME_LEQ( timenow
.tv_sec
+dyn_keepalive_interval
,
3243 continue; /* too early */
3244 if (TIME_LEQ(q
->expire
, timenow
.tv_sec
))
3245 continue; /* too late, rule expired */
3247 send_pkt(&(q
->id
), q
->ack_rev
- 1, q
->ack_fwd
, TH_SYN
);
3248 send_pkt(&(q
->id
), q
->ack_fwd
- 1, q
->ack_rev
, 0);
3251 lck_mtx_unlock(ipfw_mutex
);
3253 timeout(ipfw_tick
, NULL
, dyn_keepalive_period
*hz
);
3259 struct ip_fw default_rule
;
3262 ipfw_mutex_grp_attr
= lck_grp_attr_alloc_init();
3263 ipfw_mutex_grp
= lck_grp_alloc_init("ipfw", ipfw_mutex_grp_attr
);
3264 ipfw_mutex_attr
= lck_attr_alloc_init();
3265 lck_attr_setdefault(ipfw_mutex_attr
);
3267 if ((ipfw_mutex
= lck_mtx_alloc_init(ipfw_mutex_grp
, ipfw_mutex_attr
)) == NULL
) {
3268 printf("ipfw_init: can't alloc ipfw_mutex\n");
3272 layer3_chain
= NULL
;
3274 bzero(&default_rule
, sizeof default_rule
);
3276 default_rule
.act_ofs
= 0;
3277 default_rule
.rulenum
= IPFW_DEFAULT_RULE
;
3278 default_rule
.cmd_len
= 1;
3279 default_rule
.set
= RESVD_SET
;
3281 default_rule
.cmd
[0].len
= 1;
3282 default_rule
.cmd
[0].opcode
=
3283 #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT
3288 if (add_rule(&layer3_chain
, &default_rule
)) {
3289 printf("ipfw2: add_rule failed adding default rule\n");
3290 printf("ipfw2 failed initialization!!\n");
3294 ip_fw_default_rule
= layer3_chain
;
3296 /* Radar 3920649, don't print unncessary messages to the log */
3297 printf("ipfw2 initialized, divert %s, "
3298 "rule-based forwarding enabled, default to %s, logging ",
3304 default_rule
.cmd
[0].opcode
== O_ACCEPT
? "accept" : "deny");
3307 #ifdef IPFIREWALL_VERBOSE
3310 #ifdef IPFIREWALL_VERBOSE_LIMIT
3311 verbose_limit
= IPFIREWALL_VERBOSE_LIMIT
;
3313 if (fw_verbose
== 0)
3314 printf("disabled\n");
3315 else if (verbose_limit
== 0)
3316 printf("unlimited\n");
3318 printf("limited to %d packets/entry by default\n",
3322 ip_fw_chk_ptr
= ipfw_chk
;
3323 ip_fw_ctl_ptr
= ipfw_ctl
;
3325 ipfwstringlen
= strlen( ipfwstring
);
3327 timeout(ipfw_tick
, NULL
, hz
);