2 * Copyright (c) 2003-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $FreeBSD: src/sys/netinet6/ip6_fw.c,v 1.2.2.9 2002/04/28 05:40:27 suz Exp $ */
30 /* $KAME: ip6_fw.c,v 1.21 2001/01/24 01:25:32 itojun Exp $ */
33 * Copyright (C) 1998, 1999, 2000 and 2001 WIDE Project.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * Copyright (c) 1993 Daniel Boulet
63 * Copyright (c) 1994 Ugen J.S.Antsilevich
64 * Copyright (c) 1996 Alex Nash
66 * Redistribution and use in source forms, with and without modification,
67 * are permitted provided that this entire comment appears intact.
69 * Redistribution in binary form may occur without any restrictions.
70 * Obviously, it would be nice if you gave credit where credit is due
71 * but requiring it would be too onerous.
73 * This software is provided ``AS IS'' without any warranties of any kind.
77 * Implement IPv6 packet firewall
82 #error "NOT SUPPORTED IPV6 DIVERT"
84 #ifdef IP6FW_DIVERT_RESTART
85 #error "NOT SUPPORTED IPV6 DIVERT"
89 #include <machine/spl.h>
91 #include <sys/param.h>
92 #include <sys/systm.h>
93 #include <sys/malloc.h>
95 #include <sys/queue.h>
96 #include <sys/kernel.h>
97 #include <sys/socket.h>
98 #include <sys/socketvar.h>
99 #include <sys/syslog.h>
100 #include <sys/lock.h>
101 #include <sys/time.h>
102 #include <sys/kern_event.h>
105 #include <net/route.h>
106 #include <netinet/in_systm.h>
107 #include <netinet/in.h>
108 #include <netinet/ip.h>
110 #include <netinet/ip6.h>
111 #include <netinet6/ip6_var.h>
112 #include <netinet6/in6_var.h>
113 #include <netinet/icmp6.h>
115 #include <netinet/in_pcb.h>
117 #include <netinet6/ip6_fw.h>
118 #include <netinet/ip_var.h>
119 #include <netinet/tcp.h>
120 #include <netinet/tcp_seq.h>
121 #include <netinet/tcp_timer.h>
122 #include <netinet/tcp_var.h>
123 #include <netinet/udp.h>
125 #include <sys/sysctl.h>
127 #include <net/net_osdep.h>
129 MALLOC_DEFINE(M_IP6FW
, "Ip6Fw/Ip6Acct", "Ip6Fw/Ip6Acct chain's");
131 static int fw6_debug
= 0;
132 #ifdef IPV6FIREWALL_VERBOSE
133 static int fw6_verbose
= 1;
135 static int fw6_verbose
= 0;
137 #ifdef IPV6FIREWALL_VERBOSE_LIMIT
138 static int fw6_verbose_limit
= IPV6FIREWALL_VERBOSE_LIMIT
;
140 static int fw6_verbose_limit
= 0;
143 LIST_HEAD (ip6_fw_head
, ip6_fw_chain
) ip6_fw_chain
;
145 static void ip6fw_kev_post_msg(u_int32_t
);
148 static int ip6fw_sysctl SYSCTL_HANDLER_ARGS
;
150 SYSCTL_DECL(_net_inet6_ip6
);
151 SYSCTL_NODE(_net_inet6_ip6
, OID_AUTO
, fw
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Firewall");
152 SYSCTL_PROC(_net_inet6_ip6_fw
, OID_AUTO
, enable
,
153 CTLTYPE_INT
| CTLFLAG_RW
,
154 &ip6_fw_enable
, 0, ip6fw_sysctl
, "I", "Enable ip6fw");
155 SYSCTL_INT(_net_inet6_ip6_fw
, OID_AUTO
, debug
, CTLFLAG_RW
, &fw6_debug
, 0, "");
156 SYSCTL_INT(_net_inet6_ip6_fw
, OID_AUTO
, verbose
, CTLFLAG_RW
, &fw6_verbose
, 0, "");
157 SYSCTL_INT(_net_inet6_ip6_fw
, OID_AUTO
, verbose_limit
, CTLFLAG_RW
, &fw6_verbose_limit
, 0, "");
160 ip6fw_sysctl SYSCTL_HANDLER_ARGS
162 #pragma unused(arg1, arg2)
165 error
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
166 if (error
|| !req
->newptr
)
169 ip6fw_kev_post_msg(KEV_IP6FW_ENABLE
);
176 #define dprintf(a) do { \
180 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
182 static int add_entry6
__P((struct ip6_fw_head
*chainptr
, struct ip6_fw
*frwl
));
183 static int del_entry6
__P((struct ip6_fw_head
*chainptr
, u_short number
));
184 static int zero_entry6
__P((struct ip6_fw
*frwl
));
185 static struct ip6_fw
*check_ip6fw_struct
__P((struct ip6_fw
*m
));
186 static int ip6opts_match
__P((struct ip6_hdr
**ip6
, struct ip6_fw
*f
,
188 int *off
, int *nxt
, u_short
*offset
));
189 static int port_match6
__P((u_short
*portptr
, int nports
, u_short port
,
191 static int tcp6flg_match
__P((struct tcphdr
*tcp6
, struct ip6_fw
*f
));
192 static int icmp6type_match
__P((struct icmp6_hdr
* icmp
, struct ip6_fw
* f
));
193 static void ip6fw_report
__P((struct ip6_fw
*f
, struct ip6_hdr
*ip6
,
194 struct ifnet
*rif
, struct ifnet
*oif
, int off
, int nxt
));
196 static int ip6_fw_chk
__P((struct ip6_hdr
**pip6
,
197 struct ifnet
*oif
, u_int16_t
*cookie
, struct mbuf
**m
));
198 static int ip6_fw_ctl
__P((struct sockopt
*));
200 static char err_prefix
[] = "ip6_fw_ctl:";
201 extern lck_mtx_t
*ip6_mutex
;
204 * Returns 1 if the port is matched by the vector, 0 otherwise
208 port_match6(u_short
*portptr
, int nports
, u_short port
, int range_flag
)
213 if (portptr
[0] <= port
&& port
<= portptr
[1]) {
219 while (nports
-- > 0) {
220 if (*portptr
++ == port
) {
228 tcp6flg_match(struct tcphdr
*tcp6
, struct ip6_fw
*f
)
230 u_char flg_set
, flg_clr
;
233 * If an established connection is required, reject packets that
234 * have only SYN of RST|ACK|SYN set. Otherwise, fall through to
235 * other flag requirements.
237 if ((f
->fw_ipflg
& IPV6_FW_IF_TCPEST
) &&
238 ((tcp6
->th_flags
& (IPV6_FW_TCPF_RST
| IPV6_FW_TCPF_ACK
|
239 IPV6_FW_TCPF_SYN
)) == IPV6_FW_TCPF_SYN
))
242 flg_set
= tcp6
->th_flags
& f
->fw_tcpf
;
243 flg_clr
= tcp6
->th_flags
& f
->fw_tcpnf
;
245 if (flg_set
!= f
->fw_tcpf
)
254 icmp6type_match(struct icmp6_hdr
*icmp6
, struct ip6_fw
*f
)
258 if (!(f
->fw_flg
& IPV6_FW_F_ICMPBIT
))
261 type
= icmp6
->icmp6_type
;
263 /* check for matching type in the bitmap */
264 if (type
< IPV6_FW_ICMPTYPES_DIM
* sizeof(unsigned) * 8 &&
265 (f
->fw_icmp6types
[type
/ (sizeof(unsigned) * 8)] &
266 (1U << (type
% (8 * sizeof(unsigned))))))
269 return(0); /* no match */
273 is_icmp6_query(struct ip6_hdr
*ip6
, int off
)
275 const struct icmp6_hdr
*icmp6
;
278 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
279 icmp6_type
= icmp6
->icmp6_type
;
281 if (icmp6_type
== ICMP6_ECHO_REQUEST
||
282 icmp6_type
== ICMP6_MEMBERSHIP_QUERY
||
283 icmp6_type
== ICMP6_WRUREQUEST
||
284 icmp6_type
== ICMP6_FQDN_QUERY
||
285 icmp6_type
== ICMP6_NI_QUERY
)
292 ip6opts_match(struct ip6_hdr
**pip6
, struct ip6_fw
*f
, struct mbuf
**m
,
293 int *off
, int *nxt
, u_short
*offset
)
296 struct ip6_hdr
*ip6
= *pip6
;
297 struct ip6_ext
*ip6e
;
298 u_char opts
, nopts
, nopts_sve
;
301 nopts
= nopts_sve
= f
->fw_ip6nopt
;
304 *off
= sizeof(struct ip6_hdr
);
305 len
= ntohs(ip6
->ip6_plen
) + sizeof(struct ip6_hdr
);
307 ip6e
= (struct ip6_ext
*)((caddr_t
) ip6
+ *off
);
308 if ((*m
)->m_len
< *off
+ sizeof(*ip6e
))
309 goto opts_check
; /* XXX */
312 case IPPROTO_FRAGMENT
:
313 if ((*m
)->m_len
>= *off
+ sizeof(struct ip6_frag
)) {
314 struct ip6_frag
*ip6f
;
316 ip6f
= (struct ip6_frag
*) ((caddr_t
)ip6
+ *off
);
317 *offset
= ip6f
->ip6f_offlg
& IP6F_OFF_MASK
;
319 opts
&= ~IPV6_FW_IP6OPT_FRAG
;
320 nopts
&= ~IPV6_FW_IP6OPT_FRAG
;
321 *off
+= sizeof(struct ip6_frag
);
324 opts
&= ~IPV6_FW_IP6OPT_AH
;
325 nopts
&= ~IPV6_FW_IP6OPT_AH
;
326 *off
+= (ip6e
->ip6e_len
+ 2) << 2;
330 case IPPROTO_HOPOPTS
:
331 opts
&= ~IPV6_FW_IP6OPT_HOPOPT
;
332 nopts
&= ~IPV6_FW_IP6OPT_HOPOPT
;
334 case IPPROTO_ROUTING
:
335 opts
&= ~IPV6_FW_IP6OPT_ROUTE
;
336 nopts
&= ~IPV6_FW_IP6OPT_ROUTE
;
339 opts
&= ~IPV6_FW_IP6OPT_ESP
;
340 nopts
&= ~IPV6_FW_IP6OPT_ESP
;
343 opts
&= ~IPV6_FW_IP6OPT_NONXT
;
344 nopts
&= ~IPV6_FW_IP6OPT_NONXT
;
347 case IPPROTO_DSTOPTS
:
348 opts
&= ~IPV6_FW_IP6OPT_OPTS
;
349 nopts
&= ~IPV6_FW_IP6OPT_OPTS
;
355 *off
+= (ip6e
->ip6e_len
+ 1) << 3;
358 *nxt
= ip6e
->ip6e_nxt
;
362 if (f
->fw_ip6opt
== f
->fw_ip6nopt
) /* XXX */
365 if (opts
== 0 && nopts
== nopts_sve
)
373 iface_match(struct ifnet
*ifp
, union ip6_fw_if
*ifu
, int byname
)
375 /* Check by name or by IP address */
377 /* Check unit number (-1 is wildcard) */
378 if (ifu
->fu_via_if
.unit
!= -1
379 && ifp
->if_unit
!= ifu
->fu_via_if
.unit
)
382 if (strncmp(ifp
->if_name
, ifu
->fu_via_if
.name
, IP6FW_IFNLEN
))
385 } else if (!IN6_IS_ADDR_UNSPECIFIED(&ifu
->fu_via_ip6
)) { /* Zero == wildcard */
388 ifnet_lock_shared(ifp
);
389 for (ia
= ifp
->if_addrlist
.tqh_first
; ia
; ia
= ia
->ifa_list
.tqe_next
)
392 if (ia
->ifa_addr
== NULL
)
394 if (ia
->ifa_addr
->sa_family
!= AF_INET6
)
396 if (!IN6_ARE_ADDR_EQUAL(&ifu
->fu_via_ip6
,
397 &(((struct sockaddr_in6
*)
398 (ia
->ifa_addr
))->sin6_addr
)))
400 ifnet_lock_done(ifp
);
403 ifnet_lock_done(ifp
);
410 ip6fw_report(struct ip6_fw
*f
, struct ip6_hdr
*ip6
,
411 struct ifnet
*rif
, struct ifnet
*oif
, int off
, int nxt
)
414 struct tcphdr
*const tcp6
= (struct tcphdr
*) ((caddr_t
) ip6
+ off
);
415 struct udphdr
*const udp
= (struct udphdr
*) ((caddr_t
) ip6
+ off
);
416 struct icmp6_hdr
*const icmp6
= (struct icmp6_hdr
*) ((caddr_t
) ip6
+ off
);
419 char action2
[32], proto
[102], name
[18];
422 count
= f
? f
->fw_pcnt
: ++counter
;
423 if (fw6_verbose_limit
!= 0 && count
> fw6_verbose_limit
)
426 /* Print command name */
427 snprintf(SNPARGS(name
, 0), "ip6fw: %d", f
? f
->fw_number
: -1);
433 switch (f
->fw_flg
& IPV6_FW_F_COMMAND
) {
437 case IPV6_FW_F_REJECT
:
438 if (f
->fw_reject_code
== IPV6_FW_REJECT_RST
)
443 case IPV6_FW_F_ACCEPT
:
446 case IPV6_FW_F_COUNT
:
449 case IPV6_FW_F_DIVERT
:
450 snprintf(SNPARGS(action2
, 0), "Divert %d",
454 snprintf(SNPARGS(action2
, 0), "Tee %d",
457 case IPV6_FW_F_SKIPTO
:
458 snprintf(SNPARGS(action2
, 0), "SkipTo %d",
469 len
= snprintf(SNPARGS(proto
, 0), "TCP [%s]",
470 ip6_sprintf(&ip6
->ip6_src
));
472 len
+= snprintf(SNPARGS(proto
, len
), ":%d ",
473 ntohs(tcp6
->th_sport
));
475 len
+= snprintf(SNPARGS(proto
, len
), " ");
476 len
+= snprintf(SNPARGS(proto
, len
), "[%s]",
477 ip6_sprintf(&ip6
->ip6_dst
));
479 snprintf(SNPARGS(proto
, len
), ":%d",
480 ntohs(tcp6
->th_dport
));
483 len
= snprintf(SNPARGS(proto
, 0), "UDP [%s]",
484 ip6_sprintf(&ip6
->ip6_src
));
486 len
+= snprintf(SNPARGS(proto
, len
), ":%d ",
487 ntohs(udp
->uh_sport
));
489 len
+= snprintf(SNPARGS(proto
, len
), " ");
490 len
+= snprintf(SNPARGS(proto
, len
), "[%s]",
491 ip6_sprintf(&ip6
->ip6_dst
));
493 snprintf(SNPARGS(proto
, len
), ":%d",
494 ntohs(udp
->uh_dport
));
498 len
= snprintf(SNPARGS(proto
, 0), "IPV6-ICMP:%u.%u ",
499 icmp6
->icmp6_type
, icmp6
->icmp6_code
);
501 len
= snprintf(SNPARGS(proto
, 0), "IPV6-ICMP ");
502 len
+= snprintf(SNPARGS(proto
, len
), "[%s]",
503 ip6_sprintf(&ip6
->ip6_src
));
504 snprintf(SNPARGS(proto
, len
), " [%s]",
505 ip6_sprintf(&ip6
->ip6_dst
));
508 len
= snprintf(SNPARGS(proto
, 0), "P:%d [%s]", nxt
,
509 ip6_sprintf(&ip6
->ip6_src
));
510 snprintf(SNPARGS(proto
, len
), " [%s]",
511 ip6_sprintf(&ip6
->ip6_dst
));
516 log(LOG_AUTHPRIV
| LOG_INFO
, "%s %s %s out via %s\n",
517 name
, action
, proto
, if_name(oif
));
519 log(LOG_AUTHPRIV
| LOG_INFO
, "%s %s %s in via %s\n",
520 name
, action
, proto
, if_name(rif
));
522 log(LOG_AUTHPRIV
| LOG_INFO
, "%s %s %s",
523 name
, action
, proto
);
524 if (fw6_verbose_limit
!= 0 && count
== fw6_verbose_limit
)
525 log(LOG_AUTHPRIV
| LOG_INFO
, "ip6fw: limit reached on entry %d\n",
526 f
? f
->fw_number
: -1);
532 * ip Pointer to packet header (struct ip6_hdr *)
533 * hlen Packet header length
534 * oif Outgoing interface, or NULL if packet is incoming
535 * #ifndef IP6FW_DIVERT_RESTART
536 * *cookie Ignore all divert/tee rules to this port (if non-zero)
538 * *cookie Skip up to the first rule past this rule number;
540 * *m The packet; we set to NULL when/if we nuke it.
544 * 0 The packet is to be accepted and routed normally OR
545 * the packet was denied/rejected and has been dropped;
546 * in the latter case, *m is equal to NULL upon return.
547 * port Divert the packet to port.
551 ip6_fw_chk(struct ip6_hdr
**pip6
,
552 struct ifnet
*oif
, u_int16_t
*cookie
, struct mbuf
**m
)
554 struct ip6_fw_chain
*chain
;
555 struct ip6_fw
*rule
= NULL
;
556 struct ip6_hdr
*ip6
= *pip6
;
557 struct ifnet
*const rif
= (*m
)->m_pkthdr
.rcvif
;
559 int off
= sizeof(struct ip6_hdr
), nxt
= ip6
->ip6_nxt
;
560 u_short src_port
, dst_port
;
561 #ifdef IP6FW_DIVERT_RESTART
562 u_int16_t skipto
= *cookie
;
564 u_int16_t ignport
= ntohs(*cookie
);
566 struct timeval timenow
;
568 getmicrotime(&timenow
);
572 * Go down the chain, looking for enlightment
573 * #ifdef IP6FW_DIVERT_RESTART
574 * If we've been asked to start at a given rule immediatly, do so.
577 chain
= LIST_FIRST(&ip6_fw_chain
);
578 #ifdef IP6FW_DIVERT_RESTART
582 while (chain
&& (chain
->rule
->fw_number
<= skipto
)) {
583 chain
= LIST_NEXT(chain
, chain
);
585 if (! chain
) goto dropit
;
587 #endif /* IP6FW_DIVERT_RESTART */
588 for (; chain
; chain
= LIST_NEXT(chain
, chain
)) {
589 struct ip6_fw
*const f
= chain
->rule
;
592 /* Check direction outbound */
593 if (!(f
->fw_flg
& IPV6_FW_F_OUT
))
596 /* Check direction inbound */
597 if (!(f
->fw_flg
& IPV6_FW_F_IN
))
601 #define IN6_ARE_ADDR_MASKEQUAL(x,y,z) (\
602 (((x)->s6_addr32[0] & (y)->s6_addr32[0]) == (z)->s6_addr32[0]) && \
603 (((x)->s6_addr32[1] & (y)->s6_addr32[1]) == (z)->s6_addr32[1]) && \
604 (((x)->s6_addr32[2] & (y)->s6_addr32[2]) == (z)->s6_addr32[2]) && \
605 (((x)->s6_addr32[3] & (y)->s6_addr32[3]) == (z)->s6_addr32[3]))
607 /* If src-addr doesn't match, not this rule. */
608 if (((f
->fw_flg
& IPV6_FW_F_INVSRC
) != 0) ^
609 (!IN6_ARE_ADDR_MASKEQUAL(&ip6
->ip6_src
,&f
->fw_smsk
,&f
->fw_src
)))
612 /* If dest-addr doesn't match, not this rule. */
613 if (((f
->fw_flg
& IPV6_FW_F_INVDST
) != 0) ^
614 (!IN6_ARE_ADDR_MASKEQUAL(&ip6
->ip6_dst
,&f
->fw_dmsk
,&f
->fw_dst
)))
617 #undef IN6_ARE_ADDR_MASKEQUAL
618 /* Interface check */
619 if ((f
->fw_flg
& IF6_FW_F_VIAHACK
) == IF6_FW_F_VIAHACK
) {
620 struct ifnet
*const iface
= oif
? oif
: rif
;
622 /* Backwards compatibility hack for "via" */
623 if (!iface
|| !iface_match(iface
,
624 &f
->fw_in_if
, f
->fw_flg
& IPV6_FW_F_OIFNAME
))
627 /* Check receive interface */
628 if ((f
->fw_flg
& IPV6_FW_F_IIFACE
)
629 && (!rif
|| !iface_match(rif
,
630 &f
->fw_in_if
, f
->fw_flg
& IPV6_FW_F_IIFNAME
)))
632 /* Check outgoing interface */
633 if ((f
->fw_flg
& IPV6_FW_F_OIFACE
)
634 && (!oif
|| !iface_match(oif
,
635 &f
->fw_out_if
, f
->fw_flg
& IPV6_FW_F_OIFNAME
)))
639 /* Check IP options */
640 if (!ip6opts_match(&ip6
, f
, m
, &off
, &nxt
, &offset
))
644 if ((f
->fw_flg
& IPV6_FW_F_FRAG
) && !offset
)
647 /* Check protocol; if wildcard, match */
648 if (f
->fw_prot
== IPPROTO_IPV6
)
651 /* If different, don't match */
652 if (nxt
!= f
->fw_prot
)
655 #define PULLUP_TO(len) do { \
656 if ((*m)->m_len < (len) \
657 && (*m = m_pullup(*m, (len))) == 0) { \
660 *pip6 = ip6 = mtod(*m, struct ip6_hdr *); \
663 /* Protocol specific checks */
669 if (offset
== 1) { /* cf. RFC 1858 */
670 PULLUP_TO(off
+ 4); /* XXX ? */
675 * TCP flags and ports aren't available in this
676 * packet -- if this rule specified either one,
677 * we consider the rule a non-match.
679 if (f
->fw_nports
!= 0 ||
680 f
->fw_tcpf
!= f
->fw_tcpnf
)
686 tcp6
= (struct tcphdr
*) ((caddr_t
)ip6
+ off
);
687 if (((f
->fw_tcpf
!= f
->fw_tcpnf
) ||
688 (f
->fw_ipflg
& IPV6_FW_IF_TCPEST
)) &&
689 !tcp6flg_match(tcp6
, f
))
691 src_port
= ntohs(tcp6
->th_sport
);
692 dst_port
= ntohs(tcp6
->th_dport
);
702 * Port specification is unavailable -- if this
703 * rule specifies a port, we consider the rule
706 if (f
->fw_nports
!= 0)
712 udp
= (struct udphdr
*) ((caddr_t
)ip6
+ off
);
713 src_port
= ntohs(udp
->uh_sport
);
714 dst_port
= ntohs(udp
->uh_dport
);
716 if (!port_match6(&f
->fw_pts
[0],
717 IPV6_FW_GETNSRCP(f
), src_port
,
718 f
->fw_flg
& IPV6_FW_F_SRNG
))
720 if (!port_match6(&f
->fw_pts
[IPV6_FW_GETNSRCP(f
)],
721 IPV6_FW_GETNDSTP(f
), dst_port
,
722 f
->fw_flg
& IPV6_FW_F_DRNG
))
729 struct icmp6_hdr
*icmp
;
731 if (offset
!= 0) /* Type isn't valid */
734 icmp
= (struct icmp6_hdr
*) ((caddr_t
)ip6
+ off
);
735 if (!icmp6type_match(icmp
, f
))
743 ip6fw_report(NULL
, ip6
, rif
, oif
, off
, nxt
);
748 #ifndef IP6FW_DIVERT_RESTART
749 /* Ignore divert/tee rule if socket port is "ignport" */
750 switch (f
->fw_flg
& IPV6_FW_F_COMMAND
) {
751 case IPV6_FW_F_DIVERT
:
753 if (f
->fw_divert_port
== ignport
)
754 continue; /* ignore this rule */
758 #endif /* IP6FW_DIVERT_RESTART */
759 /* Update statistics */
761 f
->fw_bcnt
+= ntohs(ip6
->ip6_plen
);
762 f
->timestamp
= timenow
.tv_sec
;
764 /* Log to console if desired */
765 if ((f
->fw_flg
& IPV6_FW_F_PRN
) && fw6_verbose
)
766 ip6fw_report(f
, ip6
, rif
, oif
, off
, nxt
);
768 /* Take appropriate action */
769 switch (f
->fw_flg
& IPV6_FW_F_COMMAND
) {
770 case IPV6_FW_F_ACCEPT
:
772 case IPV6_FW_F_COUNT
:
774 case IPV6_FW_F_DIVERT
:
775 #ifdef IP6FW_DIVERT_RESTART
776 *cookie
= f
->fw_number
;
778 *cookie
= htons(f
->fw_divert_port
);
779 #endif /* IP6FW_DIVERT_RESTART */
780 return(f
->fw_divert_port
);
783 * XXX someday tee packet here, but beware that you
784 * can't use m_copym() or m_copypacket() because
785 * the divert input routine modifies the mbuf
786 * (and these routines only increment reference
787 * counts in the case of mbuf clusters), so need
788 * to write custom routine.
791 case IPV6_FW_F_SKIPTO
:
793 while (chain
->chain
.le_next
794 && chain
->chain
.le_next
->rule
->fw_number
797 while (chain
->chain
.le_next
->rule
->fw_number
800 chain
= chain
->chain
.le_next
;
804 /* Deny/reject this packet using this rule */
810 /* Rule 65535 should always be there and should always match */
812 panic("ip6_fw: chain");
816 * At this point, we're going to drop the packet.
817 * Send a reject notice if all of the following are true:
819 * - The packet matched a reject rule
820 * - The packet is not an ICMP packet, or is an ICMP query packet
821 * - The packet is not a multicast or broadcast packet
823 if ((rule
->fw_flg
& IPV6_FW_F_COMMAND
) == IPV6_FW_F_REJECT
824 && (nxt
!= IPPROTO_ICMPV6
|| is_icmp6_query(ip6
, off
))
825 && !((*m
)->m_flags
& (M_BCAST
|M_MCAST
))
826 && !IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
827 switch (rule
->fw_reject_code
) {
828 case IPV6_FW_REJECT_RST
:
830 struct tcphdr
*const tcp
=
831 (struct tcphdr
*) ((caddr_t
)ip6
+ off
);
839 if (offset
!= 0 || (tcp
->th_flags
& TH_RST
))
844 ti
.th
.th_seq
= ntohl(ti
.th
.th_seq
);
845 ti
.th
.th_ack
= ntohl(ti
.th
.th_ack
);
846 ti
.ip6
.ip6_nxt
= IPPROTO_TCP
;
847 if (ti
.th
.th_flags
& TH_ACK
) {
853 if (((*m
)->m_flags
& M_PKTHDR
) != 0) {
854 ack
+= (*m
)->m_pkthdr
.len
- off
855 - (ti
.th
.th_off
<< 2);
856 } else if (ip6
->ip6_plen
) {
857 ack
+= ntohs(ip6
->ip6_plen
) + sizeof(*ip6
)
858 - off
- (ti
.th
.th_off
<< 2);
865 flags
= TH_RST
|TH_ACK
;
867 bcopy(&ti
, ip6
, sizeof(ti
));
868 tcp_respond(NULL
, ip6
, (struct tcphdr
*)(ip6
+ 1),
869 *m
, ack
, seq
, flags
, IFSCOPE_NONE
);
873 default: /* Send an ICMP unreachable using code */
875 (*m
)->m_pkthdr
.rcvif
= oif
;
876 lck_mtx_assert(ip6_mutex
, LCK_MTX_ASSERT_OWNED
);
877 lck_mtx_unlock(ip6_mutex
);
878 icmp6_error(*m
, ICMP6_DST_UNREACH
,
879 rule
->fw_reject_code
, 0);
880 lck_mtx_lock(ip6_mutex
);
888 * Finally, drop the packet.
898 add_entry6(struct ip6_fw_head
*chainptr
, struct ip6_fw
*frwl
)
900 struct ip6_fw
*ftmp
= 0;
901 struct ip6_fw_chain
*fwc
= 0, *fcp
, *fcpl
= 0;
905 fwc
= _MALLOC(sizeof *fwc
, M_IP6FW
, M_WAITOK
);
906 ftmp
= _MALLOC(sizeof *ftmp
, M_IP6FW
, M_WAITOK
);
908 dprintf(("%s malloc said no\n", err_prefix
));
909 if (fwc
) FREE(fwc
, M_IP6FW
);
910 if (ftmp
) FREE(ftmp
, M_IP6FW
);
914 bcopy(frwl
, ftmp
, sizeof(struct ip6_fw
));
915 ftmp
->fw_in_if
.fu_via_if
.name
[IP6FW_IFNLEN
- 1] = '\0';
922 if (!chainptr
->lh_first
) {
923 LIST_INSERT_HEAD(chainptr
, fwc
, chain
);
926 } else if (ftmp
->fw_number
== (u_short
)-1) {
927 if (fwc
) FREE(fwc
, M_IP6FW
);
928 if (ftmp
) FREE(ftmp
, M_IP6FW
);
930 dprintf(("%s bad rule number\n", err_prefix
));
934 /* If entry number is 0, find highest numbered rule and add 100 */
935 if (ftmp
->fw_number
== 0) {
936 for (fcp
= chainptr
->lh_first
; fcp
; fcp
= fcp
->chain
.le_next
) {
937 if (fcp
->rule
->fw_number
!= (u_short
)-1)
938 nbr
= fcp
->rule
->fw_number
;
942 if (nbr
< (u_short
)-1 - 100)
944 ftmp
->fw_number
= nbr
;
947 /* Got a valid number; now insert it, keeping the list ordered */
948 for (fcp
= chainptr
->lh_first
; fcp
; fcp
= fcp
->chain
.le_next
) {
949 if (fcp
->rule
->fw_number
> ftmp
->fw_number
) {
951 LIST_INSERT_AFTER(fcpl
, fwc
, chain
);
953 LIST_INSERT_HEAD(chainptr
, fwc
, chain
);
966 del_entry6(struct ip6_fw_head
*chainptr
, u_short number
)
968 struct ip6_fw_chain
*fcp
;
973 fcp
= chainptr
->lh_first
;
974 if (number
!= (u_short
)-1) {
975 for (; fcp
; fcp
= fcp
->chain
.le_next
) {
976 if (fcp
->rule
->fw_number
== number
) {
977 LIST_REMOVE(fcp
, chain
);
979 FREE(fcp
->rule
, M_IP6FW
);
991 zero_entry6(struct ip6_fw
*frwl
)
993 struct ip6_fw_chain
*fcp
;
997 * It's possible to insert multiple chain entries with the
998 * same number, so we don't stop after finding the first
999 * match if zeroing a specific entry.
1002 for (fcp
= ip6_fw_chain
.lh_first
; fcp
; fcp
= fcp
->chain
.le_next
)
1003 if (!frwl
|| frwl
->fw_number
== 0 || frwl
->fw_number
== fcp
->rule
->fw_number
) {
1004 fcp
->rule
->fw_bcnt
= fcp
->rule
->fw_pcnt
= 0;
1005 fcp
->rule
->timestamp
= 0;
1011 log(LOG_AUTHPRIV
| LOG_NOTICE
,
1012 "ip6fw: Entry %d cleared.\n", frwl
->fw_number
);
1014 log(LOG_AUTHPRIV
| LOG_NOTICE
,
1015 "ip6fw: Accounting cleared.\n");
1021 static struct ip6_fw
*
1022 check_ip6fw_struct(struct ip6_fw
*frwl
)
1024 /* Check for invalid flag bits */
1025 if ((frwl
->fw_flg
& ~IPV6_FW_F_MASK
) != 0) {
1026 dprintf(("%s undefined flag bits set (flags=%x)\n",
1027 err_prefix
, frwl
->fw_flg
));
1030 /* Must apply to incoming or outgoing (or both) */
1031 if (!(frwl
->fw_flg
& (IPV6_FW_F_IN
| IPV6_FW_F_OUT
))) {
1032 dprintf(("%s neither in nor out\n", err_prefix
));
1035 /* Empty interface name is no good */
1036 if (((frwl
->fw_flg
& IPV6_FW_F_IIFNAME
)
1037 && !*frwl
->fw_in_if
.fu_via_if
.name
)
1038 || ((frwl
->fw_flg
& IPV6_FW_F_OIFNAME
)
1039 && !*frwl
->fw_out_if
.fu_via_if
.name
)) {
1040 dprintf(("%s empty interface name\n", err_prefix
));
1043 /* Sanity check interface matching */
1044 if ((frwl
->fw_flg
& IF6_FW_F_VIAHACK
) == IF6_FW_F_VIAHACK
) {
1045 ; /* allow "via" backwards compatibility */
1046 } else if ((frwl
->fw_flg
& IPV6_FW_F_IN
)
1047 && (frwl
->fw_flg
& IPV6_FW_F_OIFACE
)) {
1048 dprintf(("%s outgoing interface check on incoming\n",
1052 /* Sanity check port ranges */
1053 if ((frwl
->fw_flg
& IPV6_FW_F_SRNG
) && IPV6_FW_GETNSRCP(frwl
) < 2) {
1054 dprintf(("%s src range set but n_src_p=%d\n",
1055 err_prefix
, IPV6_FW_GETNSRCP(frwl
)));
1058 if ((frwl
->fw_flg
& IPV6_FW_F_DRNG
) && IPV6_FW_GETNDSTP(frwl
) < 2) {
1059 dprintf(("%s dst range set but n_dst_p=%d\n",
1060 err_prefix
, IPV6_FW_GETNDSTP(frwl
)));
1063 if (IPV6_FW_GETNSRCP(frwl
) + IPV6_FW_GETNDSTP(frwl
) > IPV6_FW_MAX_PORTS
) {
1064 dprintf(("%s too many ports (%d+%d)\n",
1065 err_prefix
, IPV6_FW_GETNSRCP(frwl
), IPV6_FW_GETNDSTP(frwl
)));
1069 * Protocols other than TCP/UDP don't use port range
1071 if ((frwl
->fw_prot
!= IPPROTO_TCP
) &&
1072 (frwl
->fw_prot
!= IPPROTO_UDP
) &&
1073 (IPV6_FW_GETNSRCP(frwl
) || IPV6_FW_GETNDSTP(frwl
))) {
1074 dprintf(("%s port(s) specified for non TCP/UDP rule\n",
1080 * Rather than modify the entry to make such entries work,
1081 * we reject this rule and require user level utilities
1082 * to enforce whatever policy they deem appropriate.
1084 if ((frwl
->fw_src
.s6_addr32
[0] & (~frwl
->fw_smsk
.s6_addr32
[0])) ||
1085 (frwl
->fw_src
.s6_addr32
[1] & (~frwl
->fw_smsk
.s6_addr32
[1])) ||
1086 (frwl
->fw_src
.s6_addr32
[2] & (~frwl
->fw_smsk
.s6_addr32
[2])) ||
1087 (frwl
->fw_src
.s6_addr32
[3] & (~frwl
->fw_smsk
.s6_addr32
[3])) ||
1088 (frwl
->fw_dst
.s6_addr32
[0] & (~frwl
->fw_dmsk
.s6_addr32
[0])) ||
1089 (frwl
->fw_dst
.s6_addr32
[1] & (~frwl
->fw_dmsk
.s6_addr32
[1])) ||
1090 (frwl
->fw_dst
.s6_addr32
[2] & (~frwl
->fw_dmsk
.s6_addr32
[2])) ||
1091 (frwl
->fw_dst
.s6_addr32
[3] & (~frwl
->fw_dmsk
.s6_addr32
[3]))) {
1092 dprintf(("%s rule never matches\n", err_prefix
));
1096 if ((frwl
->fw_flg
& IPV6_FW_F_FRAG
) &&
1097 (frwl
->fw_prot
== IPPROTO_UDP
|| frwl
->fw_prot
== IPPROTO_TCP
)) {
1098 if (frwl
->fw_nports
) {
1099 dprintf(("%s cannot mix 'frag' and ports\n", err_prefix
));
1102 if (frwl
->fw_prot
== IPPROTO_TCP
&&
1103 frwl
->fw_tcpf
!= frwl
->fw_tcpnf
) {
1104 dprintf(("%s cannot mix 'frag' with TCP flags\n", err_prefix
));
1109 /* Check command specific stuff */
1110 switch (frwl
->fw_flg
& IPV6_FW_F_COMMAND
)
1112 case IPV6_FW_F_REJECT
:
1113 if (frwl
->fw_reject_code
>= 0x100
1114 && !(frwl
->fw_prot
== IPPROTO_TCP
1115 && frwl
->fw_reject_code
== IPV6_FW_REJECT_RST
)) {
1116 dprintf(("%s unknown reject code\n", err_prefix
));
1120 case IPV6_FW_F_DIVERT
: /* Diverting to port zero is invalid */
1122 if (frwl
->fw_divert_port
== 0) {
1123 dprintf(("%s can't divert to port 0\n", err_prefix
));
1127 case IPV6_FW_F_DENY
:
1128 case IPV6_FW_F_ACCEPT
:
1129 case IPV6_FW_F_COUNT
:
1130 case IPV6_FW_F_SKIPTO
:
1133 dprintf(("%s invalid command\n", err_prefix
));
1143 ip6_fw_ctl(int stage
, struct mbuf
**mm
)
1148 if (stage
== IPV6_FW_GET
) {
1149 struct ip6_fw_chain
*fcp
= ip6_fw_chain
.lh_first
;
1150 *mm
= m
= m_get(M_WAIT
, MT_DATA
); /* XXX */
1153 if (sizeof *(fcp
->rule
) > MLEN
) {
1155 if ((m
->m_flags
& M_EXT
) == 0) {
1160 for (; fcp
; fcp
= fcp
->chain
.le_next
) {
1161 bcopy(fcp
->rule
, m
->m_data
, sizeof *(fcp
->rule
));
1162 m
->m_len
= sizeof *(fcp
->rule
);
1163 m
->m_next
= m_get(M_WAIT
, MT_DATA
); /* XXX */
1169 if (sizeof *(fcp
->rule
) > MLEN
) {
1171 if ((m
->m_flags
& M_EXT
) == 0) {
1181 /* only allow get calls if secure mode > 2 */
1182 if (securelevel
> 2) {
1189 if (stage
== IPV6_FW_FLUSH
) {
1190 while (ip6_fw_chain
.lh_first
!= NULL
&&
1191 ip6_fw_chain
.lh_first
->rule
->fw_number
!= (u_short
)-1) {
1192 struct ip6_fw_chain
*fcp
= ip6_fw_chain
.lh_first
;
1194 LIST_REMOVE(ip6_fw_chain
.lh_first
, chain
);
1196 FREE(fcp
->rule
, M_IP6FW
);
1205 if (stage
== IPV6_FW_ZERO
) {
1206 error
= zero_entry6(m
);
1214 printf("%s NULL mbuf ptr\n", err_prefix
);
1218 if (stage
== IPV6_FW_ADD
) {
1219 struct ip6_fw
*frwl
= check_ip6fw_mbuf(m
);
1224 error
= add_entry6(&ip6_fw_chain
, frwl
);
1231 if (stage
== IPV6_FW_DEL
) {
1232 if (m
->m_len
!= sizeof(struct ip6_fw
)) {
1233 dprintf(("%s len=%ld, want %lu\n", err_prefix
, m
->m_len
,
1234 sizeof(struct ip6_fw
)));
1236 } else if (mtod(m
, struct ip6_fw
*)->fw_number
== (u_short
)-1) {
1237 dprintf(("%s can't delete rule 65535\n", err_prefix
));
1240 error
= del_entry6(&ip6_fw_chain
,
1241 mtod(m
, struct ip6_fw
*)->fw_number
);
1249 dprintf(("%s unknown request %d\n", err_prefix
, stage
));
1259 ip6fw_kev_post_msg(u_int32_t event_code
)
1261 struct kev_msg ev_msg
;
1263 bzero(&ev_msg
, sizeof(struct kev_msg
));
1265 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1266 ev_msg
.kev_class
= KEV_FIREWALL_CLASS
;
1267 ev_msg
.kev_subclass
= KEV_IP6FW_SUBCLASS
;
1268 ev_msg
.event_code
= event_code
;
1270 kev_post_msg(&ev_msg
);
1276 ip6_fw_ctl(struct sockopt
*sopt
)
1283 if (securelevel
>= 3 &&
1284 (sopt
->sopt_dir
!= SOPT_GET
|| sopt
->sopt_name
!= IPV6_FW_GET
))
1287 /* We ALWAYS expect the client to pass in a rule structure so that we can
1288 * check the version of the API that they are using. In the case of a
1289 * IPV6_FW_GET operation, the first rule of the output buffer passed to us
1290 * must have the version set. */
1291 if (!sopt
->sopt_val
|| sopt
->sopt_valsize
< sizeof rule
) return EINVAL
;
1293 /* save sopt->sopt_valsize */
1294 valsize
= sopt
->sopt_valsize
;
1295 if ((error
= sooptcopyin(sopt
, &rule
, sizeof(rule
), sizeof(rule
))))
1298 if (rule
.version
!= IPV6_FW_CURRENT_API_VERSION
) return EINVAL
;
1299 rule
.version
= 0xFFFFFFFF; /* version is meaningless once rules "make it in the door". */
1301 switch (sopt
->sopt_name
)
1305 struct ip6_fw_chain
*fcp
;
1310 LIST_FOREACH(fcp
, &ip6_fw_chain
, chain
)
1311 size
+= sizeof *buf
;
1313 buf
= _MALLOC(size
, M_TEMP
, M_WAITOK
);
1314 if (!buf
) error
= ENOBUFS
;
1317 struct ip6_fw
*bp
= buf
;
1318 LIST_FOREACH(fcp
, &ip6_fw_chain
, chain
)
1320 bcopy(fcp
->rule
, bp
, sizeof *bp
);
1321 bp
->version
= IPV6_FW_CURRENT_API_VERSION
;
1329 sopt
->sopt_valsize
= valsize
;
1330 error
= sooptcopyout(sopt
, buf
, size
);
1339 while (ip6_fw_chain
.lh_first
&&
1340 ip6_fw_chain
.lh_first
->rule
->fw_number
!= (u_short
)-1)
1342 struct ip6_fw_chain
*fcp
= ip6_fw_chain
.lh_first
;
1343 LIST_REMOVE(ip6_fw_chain
.lh_first
, chain
);
1344 FREE(fcp
->rule
, M_IP6FW
);
1348 ip6fw_kev_post_msg(KEV_IP6FW_FLUSH
);
1352 error
= zero_entry6(&rule
);
1356 if (check_ip6fw_struct(&rule
)) {
1357 error
= add_entry6(&ip6_fw_chain
, &rule
);
1359 ip6fw_kev_post_msg(KEV_IP6FW_ADD
);
1365 if (rule
.fw_number
== (u_short
)-1)
1367 dprintf(("%s can't delete rule 65535\n", err_prefix
));
1371 error
= del_entry6(&ip6_fw_chain
, rule
.fw_number
);
1373 ip6fw_kev_post_msg(KEV_IP6FW_DEL
);
1378 dprintf(("%s invalid option %d\n", err_prefix
, sopt
->sopt_name
));
1388 struct ip6_fw default_rule
;
1390 ip6_fw_chk_ptr
= ip6_fw_chk
;
1391 ip6_fw_ctl_ptr
= ip6_fw_ctl
;
1392 LIST_INIT(&ip6_fw_chain
);
1394 bzero(&default_rule
, sizeof default_rule
);
1395 default_rule
.fw_prot
= IPPROTO_IPV6
;
1396 default_rule
.fw_number
= (u_short
)-1;
1397 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1398 default_rule
.fw_flg
|= IPV6_FW_F_ACCEPT
;
1400 default_rule
.fw_flg
|= IPV6_FW_F_DENY
;
1402 default_rule
.fw_flg
|= IPV6_FW_F_IN
| IPV6_FW_F_OUT
;
1403 if (check_ip6fw_struct(&default_rule
) == NULL
||
1404 add_entry6(&ip6_fw_chain
, &default_rule
))
1405 panic("%s", __FUNCTION__
);
1407 printf("IPv6 packet filtering initialized, ");
1408 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1409 printf("default to accept, ");
1411 #ifndef IPV6FIREWALL_VERBOSE
1412 printf("logging disabled\n");
1414 if (fw6_verbose_limit
== 0)
1415 printf("unlimited logging\n");
1417 printf("logging limited to %d packets/entry\n",