2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 /* $FreeBSD: src/sys/netinet6/ip6_fw.c,v 1.2.2.9 2002/04/28 05:40:27 suz Exp $ */
32 /* $KAME: ip6_fw.c,v 1.21 2001/01/24 01:25:32 itojun Exp $ */
35 * Copyright (C) 1998, 1999, 2000 and 2001 WIDE Project.
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the project nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * Copyright (c) 1993 Daniel Boulet
65 * Copyright (c) 1994 Ugen J.S.Antsilevich
66 * Copyright (c) 1996 Alex Nash
68 * Redistribution and use in source forms, with and without modification,
69 * are permitted provided that this entire comment appears intact.
71 * Redistribution in binary form may occur without any restrictions.
72 * Obviously, it would be nice if you gave credit where credit is due
73 * but requiring it would be too onerous.
75 * This software is provided ``AS IS'' without any warranties of any kind.
79 * Implement IPv6 packet firewall
84 #error "NOT SUPPORTED IPV6 DIVERT"
86 #ifdef IP6FW_DIVERT_RESTART
87 #error "NOT SUPPORTED IPV6 DIVERT"
91 #include <machine/spl.h>
93 #include <sys/param.h>
94 #include <sys/systm.h>
95 #include <sys/malloc.h>
97 #include <sys/queue.h>
98 #include <sys/kernel.h>
99 #include <sys/socket.h>
100 #include <sys/socketvar.h>
101 #include <sys/syslog.h>
102 #include <sys/lock.h>
103 #include <sys/time.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
= 1;
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
;
146 SYSCTL_DECL(_net_inet6_ip6
);
147 SYSCTL_NODE(_net_inet6_ip6
, OID_AUTO
, fw
, CTLFLAG_RW
, 0, "Firewall");
148 SYSCTL_INT(_net_inet6_ip6_fw
, OID_AUTO
, enable
, CTLFLAG_RW
,
149 &ip6_fw_enable
, 0, "Enable ip6fw");
150 SYSCTL_INT(_net_inet6_ip6_fw
, OID_AUTO
, debug
, CTLFLAG_RW
, &fw6_debug
, 0, "");
151 SYSCTL_INT(_net_inet6_ip6_fw
, OID_AUTO
, verbose
, CTLFLAG_RW
, &fw6_verbose
, 0, "");
152 SYSCTL_INT(_net_inet6_ip6_fw
, OID_AUTO
, verbose_limit
, CTLFLAG_RW
, &fw6_verbose_limit
, 0, "");
155 #define dprintf(a) do { \
159 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
161 static int add_entry6
__P((struct ip6_fw_head
*chainptr
, struct ip6_fw
*frwl
));
162 static int del_entry6
__P((struct ip6_fw_head
*chainptr
, u_short number
));
163 static int zero_entry6
__P((struct ip6_fw
*frwl
));
164 static struct ip6_fw
*check_ip6fw_struct
__P((struct ip6_fw
*m
));
165 static int ip6opts_match
__P((struct ip6_hdr
**ip6
, struct ip6_fw
*f
,
167 int *off
, int *nxt
, u_short
*offset
));
168 static int port_match6
__P((u_short
*portptr
, int nports
, u_short port
,
170 static int tcp6flg_match
__P((struct tcphdr
*tcp6
, struct ip6_fw
*f
));
171 static int icmp6type_match
__P((struct icmp6_hdr
* icmp
, struct ip6_fw
* f
));
172 static void ip6fw_report
__P((struct ip6_fw
*f
, struct ip6_hdr
*ip6
,
173 struct ifnet
*rif
, struct ifnet
*oif
, int off
, int nxt
));
175 static int ip6_fw_chk
__P((struct ip6_hdr
**pip6
,
176 struct ifnet
*oif
, u_int16_t
*cookie
, struct mbuf
**m
));
177 static int ip6_fw_ctl
__P((struct sockopt
*));
179 static char err_prefix
[] = "ip6_fw_ctl:";
180 extern lck_mtx_t
*ip6_mutex
;
183 * Returns 1 if the port is matched by the vector, 0 otherwise
187 port_match6(u_short
*portptr
, int nports
, u_short port
, int range_flag
)
192 if (portptr
[0] <= port
&& port
<= portptr
[1]) {
198 while (nports
-- > 0) {
199 if (*portptr
++ == port
) {
207 tcp6flg_match(struct tcphdr
*tcp6
, struct ip6_fw
*f
)
209 u_char flg_set
, flg_clr
;
212 * If an established connection is required, reject packets that
213 * have only SYN of RST|ACK|SYN set. Otherwise, fall through to
214 * other flag requirements.
216 if ((f
->fw_ipflg
& IPV6_FW_IF_TCPEST
) &&
217 ((tcp6
->th_flags
& (IPV6_FW_TCPF_RST
| IPV6_FW_TCPF_ACK
|
218 IPV6_FW_TCPF_SYN
)) == IPV6_FW_TCPF_SYN
))
221 flg_set
= tcp6
->th_flags
& f
->fw_tcpf
;
222 flg_clr
= tcp6
->th_flags
& f
->fw_tcpnf
;
224 if (flg_set
!= f
->fw_tcpf
)
233 icmp6type_match(struct icmp6_hdr
*icmp6
, struct ip6_fw
*f
)
237 if (!(f
->fw_flg
& IPV6_FW_F_ICMPBIT
))
240 type
= icmp6
->icmp6_type
;
242 /* check for matching type in the bitmap */
243 if (type
< IPV6_FW_ICMPTYPES_DIM
* sizeof(unsigned) * 8 &&
244 (f
->fw_icmp6types
[type
/ (sizeof(unsigned) * 8)] &
245 (1U << (type
% (8 * sizeof(unsigned))))))
248 return(0); /* no match */
252 is_icmp6_query(struct ip6_hdr
*ip6
, int off
)
254 const struct icmp6_hdr
*icmp6
;
257 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
258 icmp6_type
= icmp6
->icmp6_type
;
260 if (icmp6_type
== ICMP6_ECHO_REQUEST
||
261 icmp6_type
== ICMP6_MEMBERSHIP_QUERY
||
262 icmp6_type
== ICMP6_WRUREQUEST
||
263 icmp6_type
== ICMP6_FQDN_QUERY
||
264 icmp6_type
== ICMP6_NI_QUERY
)
271 ip6opts_match(struct ip6_hdr
**pip6
, struct ip6_fw
*f
, struct mbuf
**m
,
272 int *off
, int *nxt
, u_short
*offset
)
275 struct ip6_hdr
*ip6
= *pip6
;
276 struct ip6_ext
*ip6e
;
277 u_char opts
, nopts
, nopts_sve
;
280 nopts
= nopts_sve
= f
->fw_ip6nopt
;
283 *off
= sizeof(struct ip6_hdr
);
284 len
= ntohs(ip6
->ip6_plen
) + sizeof(struct ip6_hdr
);
286 ip6e
= (struct ip6_ext
*)((caddr_t
) ip6
+ *off
);
287 if ((*m
)->m_len
< *off
+ sizeof(*ip6e
))
288 goto opts_check
; /* XXX */
291 case IPPROTO_FRAGMENT
:
292 if ((*m
)->m_len
>= *off
+ sizeof(struct ip6_frag
)) {
293 struct ip6_frag
*ip6f
;
295 ip6f
= (struct ip6_frag
*) ((caddr_t
)ip6
+ *off
);
296 *offset
= ip6f
->ip6f_offlg
& IP6F_OFF_MASK
;
298 opts
&= ~IPV6_FW_IP6OPT_FRAG
;
299 nopts
&= ~IPV6_FW_IP6OPT_FRAG
;
300 *off
+= sizeof(struct ip6_frag
);
303 opts
&= ~IPV6_FW_IP6OPT_AH
;
304 nopts
&= ~IPV6_FW_IP6OPT_AH
;
305 *off
+= (ip6e
->ip6e_len
+ 2) << 2;
309 case IPPROTO_HOPOPTS
:
310 opts
&= ~IPV6_FW_IP6OPT_HOPOPT
;
311 nopts
&= ~IPV6_FW_IP6OPT_HOPOPT
;
313 case IPPROTO_ROUTING
:
314 opts
&= ~IPV6_FW_IP6OPT_ROUTE
;
315 nopts
&= ~IPV6_FW_IP6OPT_ROUTE
;
318 opts
&= ~IPV6_FW_IP6OPT_ESP
;
319 nopts
&= ~IPV6_FW_IP6OPT_ESP
;
322 opts
&= ~IPV6_FW_IP6OPT_NONXT
;
323 nopts
&= ~IPV6_FW_IP6OPT_NONXT
;
326 case IPPROTO_DSTOPTS
:
327 opts
&= ~IPV6_FW_IP6OPT_OPTS
;
328 nopts
&= ~IPV6_FW_IP6OPT_OPTS
;
334 *off
+= (ip6e
->ip6e_len
+ 1) << 3;
337 *nxt
= ip6e
->ip6e_nxt
;
341 if (f
->fw_ip6opt
== f
->fw_ip6nopt
) /* XXX */
344 if (opts
== 0 && nopts
== nopts_sve
)
352 iface_match(struct ifnet
*ifp
, union ip6_fw_if
*ifu
, int byname
)
354 /* Check by name or by IP address */
356 /* Check unit number (-1 is wildcard) */
357 if (ifu
->fu_via_if
.unit
!= -1
358 && ifp
->if_unit
!= ifu
->fu_via_if
.unit
)
361 if (strncmp(ifp
->if_name
, ifu
->fu_via_if
.name
, IP6FW_IFNLEN
))
364 } else if (!IN6_IS_ADDR_UNSPECIFIED(&ifu
->fu_via_ip6
)) { /* Zero == wildcard */
367 ifnet_lock_shared(ifp
);
368 for (ia
= ifp
->if_addrlist
.tqh_first
; ia
; ia
= ia
->ifa_list
.tqe_next
)
371 if (ia
->ifa_addr
== NULL
)
373 if (ia
->ifa_addr
->sa_family
!= AF_INET6
)
375 if (!IN6_ARE_ADDR_EQUAL(&ifu
->fu_via_ip6
,
376 &(((struct sockaddr_in6
*)
377 (ia
->ifa_addr
))->sin6_addr
)))
379 ifnet_lock_done(ifp
);
382 ifnet_lock_done(ifp
);
389 ip6fw_report(struct ip6_fw
*f
, struct ip6_hdr
*ip6
,
390 struct ifnet
*rif
, struct ifnet
*oif
, int off
, int nxt
)
393 struct tcphdr
*const tcp6
= (struct tcphdr
*) ((caddr_t
) ip6
+ off
);
394 struct udphdr
*const udp
= (struct udphdr
*) ((caddr_t
) ip6
+ off
);
395 struct icmp6_hdr
*const icmp6
= (struct icmp6_hdr
*) ((caddr_t
) ip6
+ off
);
398 char action2
[32], proto
[102], name
[18];
401 count
= f
? f
->fw_pcnt
: ++counter
;
402 if (fw6_verbose_limit
!= 0 && count
> fw6_verbose_limit
)
405 /* Print command name */
406 snprintf(SNPARGS(name
, 0), "ip6fw: %d", f
? f
->fw_number
: -1);
412 switch (f
->fw_flg
& IPV6_FW_F_COMMAND
) {
416 case IPV6_FW_F_REJECT
:
417 if (f
->fw_reject_code
== IPV6_FW_REJECT_RST
)
422 case IPV6_FW_F_ACCEPT
:
425 case IPV6_FW_F_COUNT
:
428 case IPV6_FW_F_DIVERT
:
429 snprintf(SNPARGS(action2
, 0), "Divert %d",
433 snprintf(SNPARGS(action2
, 0), "Tee %d",
436 case IPV6_FW_F_SKIPTO
:
437 snprintf(SNPARGS(action2
, 0), "SkipTo %d",
448 len
= snprintf(SNPARGS(proto
, 0), "TCP [%s]",
449 ip6_sprintf(&ip6
->ip6_src
));
451 len
+= snprintf(SNPARGS(proto
, len
), ":%d ",
452 ntohs(tcp6
->th_sport
));
454 len
+= snprintf(SNPARGS(proto
, len
), " ");
455 len
+= snprintf(SNPARGS(proto
, len
), "[%s]",
456 ip6_sprintf(&ip6
->ip6_dst
));
458 snprintf(SNPARGS(proto
, len
), ":%d",
459 ntohs(tcp6
->th_dport
));
462 len
= snprintf(SNPARGS(proto
, 0), "UDP [%s]",
463 ip6_sprintf(&ip6
->ip6_src
));
465 len
+= snprintf(SNPARGS(proto
, len
), ":%d ",
466 ntohs(udp
->uh_sport
));
468 len
+= snprintf(SNPARGS(proto
, len
), " ");
469 len
+= snprintf(SNPARGS(proto
, len
), "[%s]",
470 ip6_sprintf(&ip6
->ip6_dst
));
472 snprintf(SNPARGS(proto
, len
), ":%d",
473 ntohs(udp
->uh_dport
));
477 len
= snprintf(SNPARGS(proto
, 0), "IPV6-ICMP:%u.%u ",
478 icmp6
->icmp6_type
, icmp6
->icmp6_code
);
480 len
= snprintf(SNPARGS(proto
, 0), "IPV6-ICMP ");
481 len
+= snprintf(SNPARGS(proto
, len
), "[%s]",
482 ip6_sprintf(&ip6
->ip6_src
));
483 snprintf(SNPARGS(proto
, len
), " [%s]",
484 ip6_sprintf(&ip6
->ip6_dst
));
487 len
= snprintf(SNPARGS(proto
, 0), "P:%d [%s]", nxt
,
488 ip6_sprintf(&ip6
->ip6_src
));
489 snprintf(SNPARGS(proto
, len
), " [%s]",
490 ip6_sprintf(&ip6
->ip6_dst
));
495 log(LOG_AUTHPRIV
| LOG_INFO
, "%s %s %s out via %s\n",
496 name
, action
, proto
, if_name(oif
));
498 log(LOG_AUTHPRIV
| LOG_INFO
, "%s %s %s in via %s\n",
499 name
, action
, proto
, if_name(rif
));
501 log(LOG_AUTHPRIV
| LOG_INFO
, "%s %s %s",
502 name
, action
, proto
);
503 if (fw6_verbose_limit
!= 0 && count
== fw6_verbose_limit
)
504 log(LOG_AUTHPRIV
| LOG_INFO
, "ip6fw: limit reached on entry %d\n",
505 f
? f
->fw_number
: -1);
511 * ip Pointer to packet header (struct ip6_hdr *)
512 * hlen Packet header length
513 * oif Outgoing interface, or NULL if packet is incoming
514 * #ifndef IP6FW_DIVERT_RESTART
515 * *cookie Ignore all divert/tee rules to this port (if non-zero)
517 * *cookie Skip up to the first rule past this rule number;
519 * *m The packet; we set to NULL when/if we nuke it.
523 * 0 The packet is to be accepted and routed normally OR
524 * the packet was denied/rejected and has been dropped;
525 * in the latter case, *m is equal to NULL upon return.
526 * port Divert the packet to port.
530 ip6_fw_chk(struct ip6_hdr
**pip6
,
531 struct ifnet
*oif
, u_int16_t
*cookie
, struct mbuf
**m
)
533 struct ip6_fw_chain
*chain
;
534 struct ip6_fw
*rule
= NULL
;
535 struct ip6_hdr
*ip6
= *pip6
;
536 struct ifnet
*const rif
= (*m
)->m_pkthdr
.rcvif
;
538 int off
= sizeof(struct ip6_hdr
), nxt
= ip6
->ip6_nxt
;
539 u_short src_port
, dst_port
;
540 #ifdef IP6FW_DIVERT_RESTART
541 u_int16_t skipto
= *cookie
;
543 u_int16_t ignport
= ntohs(*cookie
);
545 struct timeval timenow
;
547 getmicrotime(&timenow
);
551 * Go down the chain, looking for enlightment
552 * #ifdef IP6FW_DIVERT_RESTART
553 * If we've been asked to start at a given rule immediatly, do so.
556 chain
= LIST_FIRST(&ip6_fw_chain
);
557 #ifdef IP6FW_DIVERT_RESTART
561 while (chain
&& (chain
->rule
->fw_number
<= skipto
)) {
562 chain
= LIST_NEXT(chain
, chain
);
564 if (! chain
) goto dropit
;
566 #endif /* IP6FW_DIVERT_RESTART */
567 for (; chain
; chain
= LIST_NEXT(chain
, chain
)) {
568 struct ip6_fw
*const f
= chain
->rule
;
571 /* Check direction outbound */
572 if (!(f
->fw_flg
& IPV6_FW_F_OUT
))
575 /* Check direction inbound */
576 if (!(f
->fw_flg
& IPV6_FW_F_IN
))
580 #define IN6_ARE_ADDR_MASKEQUAL(x,y,z) (\
581 (((x)->s6_addr32[0] & (y)->s6_addr32[0]) == (z)->s6_addr32[0]) && \
582 (((x)->s6_addr32[1] & (y)->s6_addr32[1]) == (z)->s6_addr32[1]) && \
583 (((x)->s6_addr32[2] & (y)->s6_addr32[2]) == (z)->s6_addr32[2]) && \
584 (((x)->s6_addr32[3] & (y)->s6_addr32[3]) == (z)->s6_addr32[3]))
586 /* If src-addr doesn't match, not this rule. */
587 if (((f
->fw_flg
& IPV6_FW_F_INVSRC
) != 0) ^
588 (!IN6_ARE_ADDR_MASKEQUAL(&ip6
->ip6_src
,&f
->fw_smsk
,&f
->fw_src
)))
591 /* If dest-addr doesn't match, not this rule. */
592 if (((f
->fw_flg
& IPV6_FW_F_INVDST
) != 0) ^
593 (!IN6_ARE_ADDR_MASKEQUAL(&ip6
->ip6_dst
,&f
->fw_dmsk
,&f
->fw_dst
)))
596 #undef IN6_ARE_ADDR_MASKEQUAL
597 /* Interface check */
598 if ((f
->fw_flg
& IF6_FW_F_VIAHACK
) == IF6_FW_F_VIAHACK
) {
599 struct ifnet
*const iface
= oif
? oif
: rif
;
601 /* Backwards compatibility hack for "via" */
602 if (!iface
|| !iface_match(iface
,
603 &f
->fw_in_if
, f
->fw_flg
& IPV6_FW_F_OIFNAME
))
606 /* Check receive interface */
607 if ((f
->fw_flg
& IPV6_FW_F_IIFACE
)
608 && (!rif
|| !iface_match(rif
,
609 &f
->fw_in_if
, f
->fw_flg
& IPV6_FW_F_IIFNAME
)))
611 /* Check outgoing interface */
612 if ((f
->fw_flg
& IPV6_FW_F_OIFACE
)
613 && (!oif
|| !iface_match(oif
,
614 &f
->fw_out_if
, f
->fw_flg
& IPV6_FW_F_OIFNAME
)))
618 /* Check IP options */
619 if (!ip6opts_match(&ip6
, f
, m
, &off
, &nxt
, &offset
))
623 if ((f
->fw_flg
& IPV6_FW_F_FRAG
) && !offset
)
626 /* Check protocol; if wildcard, match */
627 if (f
->fw_prot
== IPPROTO_IPV6
)
630 /* If different, don't match */
631 if (nxt
!= f
->fw_prot
)
634 #define PULLUP_TO(len) do { \
635 if ((*m)->m_len < (len) \
636 && (*m = m_pullup(*m, (len))) == 0) { \
639 *pip6 = ip6 = mtod(*m, struct ip6_hdr *); \
642 /* Protocol specific checks */
648 if (offset
== 1) { /* cf. RFC 1858 */
649 PULLUP_TO(off
+ 4); /* XXX ? */
654 * TCP flags and ports aren't available in this
655 * packet -- if this rule specified either one,
656 * we consider the rule a non-match.
658 if (f
->fw_nports
!= 0 ||
659 f
->fw_tcpf
!= f
->fw_tcpnf
)
665 tcp6
= (struct tcphdr
*) ((caddr_t
)ip6
+ off
);
666 if (((f
->fw_tcpf
!= f
->fw_tcpnf
) ||
667 (f
->fw_ipflg
& IPV6_FW_IF_TCPEST
)) &&
668 !tcp6flg_match(tcp6
, f
))
670 src_port
= ntohs(tcp6
->th_sport
);
671 dst_port
= ntohs(tcp6
->th_dport
);
681 * Port specification is unavailable -- if this
682 * rule specifies a port, we consider the rule
685 if (f
->fw_nports
!= 0)
691 udp
= (struct udphdr
*) ((caddr_t
)ip6
+ off
);
692 src_port
= ntohs(udp
->uh_sport
);
693 dst_port
= ntohs(udp
->uh_dport
);
695 if (!port_match6(&f
->fw_pts
[0],
696 IPV6_FW_GETNSRCP(f
), src_port
,
697 f
->fw_flg
& IPV6_FW_F_SRNG
))
699 if (!port_match6(&f
->fw_pts
[IPV6_FW_GETNSRCP(f
)],
700 IPV6_FW_GETNDSTP(f
), dst_port
,
701 f
->fw_flg
& IPV6_FW_F_DRNG
))
708 struct icmp6_hdr
*icmp
;
710 if (offset
!= 0) /* Type isn't valid */
713 icmp
= (struct icmp6_hdr
*) ((caddr_t
)ip6
+ off
);
714 if (!icmp6type_match(icmp
, f
))
722 ip6fw_report(NULL
, ip6
, rif
, oif
, off
, nxt
);
727 #ifndef IP6FW_DIVERT_RESTART
728 /* Ignore divert/tee rule if socket port is "ignport" */
729 switch (f
->fw_flg
& IPV6_FW_F_COMMAND
) {
730 case IPV6_FW_F_DIVERT
:
732 if (f
->fw_divert_port
== ignport
)
733 continue; /* ignore this rule */
737 #endif /* IP6FW_DIVERT_RESTART */
738 /* Update statistics */
740 f
->fw_bcnt
+= ntohs(ip6
->ip6_plen
);
741 f
->timestamp
= timenow
.tv_sec
;
743 /* Log to console if desired */
744 if ((f
->fw_flg
& IPV6_FW_F_PRN
) && fw6_verbose
)
745 ip6fw_report(f
, ip6
, rif
, oif
, off
, nxt
);
747 /* Take appropriate action */
748 switch (f
->fw_flg
& IPV6_FW_F_COMMAND
) {
749 case IPV6_FW_F_ACCEPT
:
751 case IPV6_FW_F_COUNT
:
753 case IPV6_FW_F_DIVERT
:
754 #ifdef IP6FW_DIVERT_RESTART
755 *cookie
= f
->fw_number
;
757 *cookie
= htons(f
->fw_divert_port
);
758 #endif /* IP6FW_DIVERT_RESTART */
759 return(f
->fw_divert_port
);
762 * XXX someday tee packet here, but beware that you
763 * can't use m_copym() or m_copypacket() because
764 * the divert input routine modifies the mbuf
765 * (and these routines only increment reference
766 * counts in the case of mbuf clusters), so need
767 * to write custom routine.
770 case IPV6_FW_F_SKIPTO
:
772 while (chain
->chain
.le_next
773 && chain
->chain
.le_next
->rule
->fw_number
776 while (chain
->chain
.le_next
->rule
->fw_number
779 chain
= chain
->chain
.le_next
;
783 /* Deny/reject this packet using this rule */
789 /* Rule 65535 should always be there and should always match */
791 panic("ip6_fw: chain");
795 * At this point, we're going to drop the packet.
796 * Send a reject notice if all of the following are true:
798 * - The packet matched a reject rule
799 * - The packet is not an ICMP packet, or is an ICMP query packet
800 * - The packet is not a multicast or broadcast packet
802 if ((rule
->fw_flg
& IPV6_FW_F_COMMAND
) == IPV6_FW_F_REJECT
803 && (nxt
!= IPPROTO_ICMPV6
|| is_icmp6_query(ip6
, off
))
804 && !((*m
)->m_flags
& (M_BCAST
|M_MCAST
))
805 && !IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
806 switch (rule
->fw_reject_code
) {
807 case IPV6_FW_REJECT_RST
:
809 struct tcphdr
*const tcp
=
810 (struct tcphdr
*) ((caddr_t
)ip6
+ off
);
818 if (offset
!= 0 || (tcp
->th_flags
& TH_RST
))
823 ti
.th
.th_seq
= ntohl(ti
.th
.th_seq
);
824 ti
.th
.th_ack
= ntohl(ti
.th
.th_ack
);
825 ti
.ip6
.ip6_nxt
= IPPROTO_TCP
;
826 if (ti
.th
.th_flags
& TH_ACK
) {
832 if (((*m
)->m_flags
& M_PKTHDR
) != 0) {
833 ack
+= (*m
)->m_pkthdr
.len
- off
834 - (ti
.th
.th_off
<< 2);
835 } else if (ip6
->ip6_plen
) {
836 ack
+= ntohs(ip6
->ip6_plen
) + sizeof(*ip6
)
837 - off
- (ti
.th
.th_off
<< 2);
844 flags
= TH_RST
|TH_ACK
;
846 bcopy(&ti
, ip6
, sizeof(ti
));
847 tcp_respond(NULL
, ip6
, (struct tcphdr
*)(ip6
+ 1),
848 *m
, ack
, seq
, flags
);
852 default: /* Send an ICMP unreachable using code */
854 (*m
)->m_pkthdr
.rcvif
= oif
;
855 lck_mtx_assert(ip6_mutex
, LCK_MTX_ASSERT_OWNED
);
856 lck_mtx_unlock(ip6_mutex
);
857 icmp6_error(*m
, ICMP6_DST_UNREACH
,
858 rule
->fw_reject_code
, 0);
859 lck_mtx_lock(ip6_mutex
);
867 * Finally, drop the packet.
877 add_entry6(struct ip6_fw_head
*chainptr
, struct ip6_fw
*frwl
)
879 struct ip6_fw
*ftmp
= 0;
880 struct ip6_fw_chain
*fwc
= 0, *fcp
, *fcpl
= 0;
884 fwc
= _MALLOC(sizeof *fwc
, M_IP6FW
, M_WAITOK
);
885 ftmp
= _MALLOC(sizeof *ftmp
, M_IP6FW
, M_WAITOK
);
887 dprintf(("%s malloc said no\n", err_prefix
));
888 if (fwc
) FREE(fwc
, M_IP6FW
);
889 if (ftmp
) FREE(ftmp
, M_IP6FW
);
893 bcopy(frwl
, ftmp
, sizeof(struct ip6_fw
));
894 ftmp
->fw_in_if
.fu_via_if
.name
[IP6FW_IFNLEN
- 1] = '\0';
901 if (!chainptr
->lh_first
) {
902 LIST_INSERT_HEAD(chainptr
, fwc
, chain
);
905 } else if (ftmp
->fw_number
== (u_short
)-1) {
906 if (fwc
) FREE(fwc
, M_IP6FW
);
907 if (ftmp
) FREE(ftmp
, M_IP6FW
);
909 dprintf(("%s bad rule number\n", err_prefix
));
913 /* If entry number is 0, find highest numbered rule and add 100 */
914 if (ftmp
->fw_number
== 0) {
915 for (fcp
= chainptr
->lh_first
; fcp
; fcp
= fcp
->chain
.le_next
) {
916 if (fcp
->rule
->fw_number
!= (u_short
)-1)
917 nbr
= fcp
->rule
->fw_number
;
921 if (nbr
< (u_short
)-1 - 100)
923 ftmp
->fw_number
= nbr
;
926 /* Got a valid number; now insert it, keeping the list ordered */
927 for (fcp
= chainptr
->lh_first
; fcp
; fcp
= fcp
->chain
.le_next
) {
928 if (fcp
->rule
->fw_number
> ftmp
->fw_number
) {
930 LIST_INSERT_AFTER(fcpl
, fwc
, chain
);
932 LIST_INSERT_HEAD(chainptr
, fwc
, chain
);
945 del_entry6(struct ip6_fw_head
*chainptr
, u_short number
)
947 struct ip6_fw_chain
*fcp
;
952 fcp
= chainptr
->lh_first
;
953 if (number
!= (u_short
)-1) {
954 for (; fcp
; fcp
= fcp
->chain
.le_next
) {
955 if (fcp
->rule
->fw_number
== number
) {
956 LIST_REMOVE(fcp
, chain
);
958 FREE(fcp
->rule
, M_IP6FW
);
970 zero_entry6(struct ip6_fw
*frwl
)
972 struct ip6_fw_chain
*fcp
;
976 * It's possible to insert multiple chain entries with the
977 * same number, so we don't stop after finding the first
978 * match if zeroing a specific entry.
981 for (fcp
= ip6_fw_chain
.lh_first
; fcp
; fcp
= fcp
->chain
.le_next
)
982 if (!frwl
|| frwl
->fw_number
== 0 || frwl
->fw_number
== fcp
->rule
->fw_number
) {
983 fcp
->rule
->fw_bcnt
= fcp
->rule
->fw_pcnt
= 0;
984 fcp
->rule
->timestamp
= 0;
990 log(LOG_AUTHPRIV
| LOG_NOTICE
,
991 "ip6fw: Entry %d cleared.\n", frwl
->fw_number
);
993 log(LOG_AUTHPRIV
| LOG_NOTICE
,
994 "ip6fw: Accounting cleared.\n");
1000 static struct ip6_fw
*
1001 check_ip6fw_struct(struct ip6_fw
*frwl
)
1003 /* Check for invalid flag bits */
1004 if ((frwl
->fw_flg
& ~IPV6_FW_F_MASK
) != 0) {
1005 dprintf(("%s undefined flag bits set (flags=%x)\n",
1006 err_prefix
, frwl
->fw_flg
));
1009 /* Must apply to incoming or outgoing (or both) */
1010 if (!(frwl
->fw_flg
& (IPV6_FW_F_IN
| IPV6_FW_F_OUT
))) {
1011 dprintf(("%s neither in nor out\n", err_prefix
));
1014 /* Empty interface name is no good */
1015 if (((frwl
->fw_flg
& IPV6_FW_F_IIFNAME
)
1016 && !*frwl
->fw_in_if
.fu_via_if
.name
)
1017 || ((frwl
->fw_flg
& IPV6_FW_F_OIFNAME
)
1018 && !*frwl
->fw_out_if
.fu_via_if
.name
)) {
1019 dprintf(("%s empty interface name\n", err_prefix
));
1022 /* Sanity check interface matching */
1023 if ((frwl
->fw_flg
& IF6_FW_F_VIAHACK
) == IF6_FW_F_VIAHACK
) {
1024 ; /* allow "via" backwards compatibility */
1025 } else if ((frwl
->fw_flg
& IPV6_FW_F_IN
)
1026 && (frwl
->fw_flg
& IPV6_FW_F_OIFACE
)) {
1027 dprintf(("%s outgoing interface check on incoming\n",
1031 /* Sanity check port ranges */
1032 if ((frwl
->fw_flg
& IPV6_FW_F_SRNG
) && IPV6_FW_GETNSRCP(frwl
) < 2) {
1033 dprintf(("%s src range set but n_src_p=%d\n",
1034 err_prefix
, IPV6_FW_GETNSRCP(frwl
)));
1037 if ((frwl
->fw_flg
& IPV6_FW_F_DRNG
) && IPV6_FW_GETNDSTP(frwl
) < 2) {
1038 dprintf(("%s dst range set but n_dst_p=%d\n",
1039 err_prefix
, IPV6_FW_GETNDSTP(frwl
)));
1042 if (IPV6_FW_GETNSRCP(frwl
) + IPV6_FW_GETNDSTP(frwl
) > IPV6_FW_MAX_PORTS
) {
1043 dprintf(("%s too many ports (%d+%d)\n",
1044 err_prefix
, IPV6_FW_GETNSRCP(frwl
), IPV6_FW_GETNDSTP(frwl
)));
1048 * Protocols other than TCP/UDP don't use port range
1050 if ((frwl
->fw_prot
!= IPPROTO_TCP
) &&
1051 (frwl
->fw_prot
!= IPPROTO_UDP
) &&
1052 (IPV6_FW_GETNSRCP(frwl
) || IPV6_FW_GETNDSTP(frwl
))) {
1053 dprintf(("%s port(s) specified for non TCP/UDP rule\n",
1059 * Rather than modify the entry to make such entries work,
1060 * we reject this rule and require user level utilities
1061 * to enforce whatever policy they deem appropriate.
1063 if ((frwl
->fw_src
.s6_addr32
[0] & (~frwl
->fw_smsk
.s6_addr32
[0])) ||
1064 (frwl
->fw_src
.s6_addr32
[1] & (~frwl
->fw_smsk
.s6_addr32
[1])) ||
1065 (frwl
->fw_src
.s6_addr32
[2] & (~frwl
->fw_smsk
.s6_addr32
[2])) ||
1066 (frwl
->fw_src
.s6_addr32
[3] & (~frwl
->fw_smsk
.s6_addr32
[3])) ||
1067 (frwl
->fw_dst
.s6_addr32
[0] & (~frwl
->fw_dmsk
.s6_addr32
[0])) ||
1068 (frwl
->fw_dst
.s6_addr32
[1] & (~frwl
->fw_dmsk
.s6_addr32
[1])) ||
1069 (frwl
->fw_dst
.s6_addr32
[2] & (~frwl
->fw_dmsk
.s6_addr32
[2])) ||
1070 (frwl
->fw_dst
.s6_addr32
[3] & (~frwl
->fw_dmsk
.s6_addr32
[3]))) {
1071 dprintf(("%s rule never matches\n", err_prefix
));
1075 if ((frwl
->fw_flg
& IPV6_FW_F_FRAG
) &&
1076 (frwl
->fw_prot
== IPPROTO_UDP
|| frwl
->fw_prot
== IPPROTO_TCP
)) {
1077 if (frwl
->fw_nports
) {
1078 dprintf(("%s cannot mix 'frag' and ports\n", err_prefix
));
1081 if (frwl
->fw_prot
== IPPROTO_TCP
&&
1082 frwl
->fw_tcpf
!= frwl
->fw_tcpnf
) {
1083 dprintf(("%s cannot mix 'frag' with TCP flags\n", err_prefix
));
1088 /* Check command specific stuff */
1089 switch (frwl
->fw_flg
& IPV6_FW_F_COMMAND
)
1091 case IPV6_FW_F_REJECT
:
1092 if (frwl
->fw_reject_code
>= 0x100
1093 && !(frwl
->fw_prot
== IPPROTO_TCP
1094 && frwl
->fw_reject_code
== IPV6_FW_REJECT_RST
)) {
1095 dprintf(("%s unknown reject code\n", err_prefix
));
1099 case IPV6_FW_F_DIVERT
: /* Diverting to port zero is invalid */
1101 if (frwl
->fw_divert_port
== 0) {
1102 dprintf(("%s can't divert to port 0\n", err_prefix
));
1106 case IPV6_FW_F_DENY
:
1107 case IPV6_FW_F_ACCEPT
:
1108 case IPV6_FW_F_COUNT
:
1109 case IPV6_FW_F_SKIPTO
:
1112 dprintf(("%s invalid command\n", err_prefix
));
1122 ip6_fw_ctl(int stage
, struct mbuf
**mm
)
1127 if (stage
== IPV6_FW_GET
) {
1128 struct ip6_fw_chain
*fcp
= ip6_fw_chain
.lh_first
;
1129 *mm
= m
= m_get(M_WAIT
, MT_DATA
); /* XXX */
1132 if (sizeof *(fcp
->rule
) > MLEN
) {
1134 if ((m
->m_flags
& M_EXT
) == 0) {
1139 for (; fcp
; fcp
= fcp
->chain
.le_next
) {
1140 bcopy(fcp
->rule
, m
->m_data
, sizeof *(fcp
->rule
));
1141 m
->m_len
= sizeof *(fcp
->rule
);
1142 m
->m_next
= m_get(M_WAIT
, MT_DATA
); /* XXX */
1148 if (sizeof *(fcp
->rule
) > MLEN
) {
1150 if ((m
->m_flags
& M_EXT
) == 0) {
1160 /* only allow get calls if secure mode > 2 */
1161 if (securelevel
> 2) {
1168 if (stage
== IPV6_FW_FLUSH
) {
1169 while (ip6_fw_chain
.lh_first
!= NULL
&&
1170 ip6_fw_chain
.lh_first
->rule
->fw_number
!= (u_short
)-1) {
1171 struct ip6_fw_chain
*fcp
= ip6_fw_chain
.lh_first
;
1173 LIST_REMOVE(ip6_fw_chain
.lh_first
, chain
);
1175 FREE(fcp
->rule
, M_IP6FW
);
1184 if (stage
== IPV6_FW_ZERO
) {
1185 error
= zero_entry6(m
);
1193 printf("%s NULL mbuf ptr\n", err_prefix
);
1197 if (stage
== IPV6_FW_ADD
) {
1198 struct ip6_fw
*frwl
= check_ip6fw_mbuf(m
);
1203 error
= add_entry6(&ip6_fw_chain
, frwl
);
1210 if (stage
== IPV6_FW_DEL
) {
1211 if (m
->m_len
!= sizeof(struct ip6_fw
)) {
1212 dprintf(("%s len=%ld, want %lu\n", err_prefix
, m
->m_len
,
1213 sizeof(struct ip6_fw
)));
1215 } else if (mtod(m
, struct ip6_fw
*)->fw_number
== (u_short
)-1) {
1216 dprintf(("%s can't delete rule 65535\n", err_prefix
));
1219 error
= del_entry6(&ip6_fw_chain
,
1220 mtod(m
, struct ip6_fw
*)->fw_number
);
1228 dprintf(("%s unknown request %d\n", err_prefix
, stage
));
1238 ip6_fw_ctl(struct sockopt
*sopt
)
1245 if (securelevel
>= 3 &&
1246 (sopt
->sopt_dir
!= SOPT_GET
|| sopt
->sopt_name
!= IPV6_FW_GET
))
1249 /* We ALWAYS expect the client to pass in a rule structure so that we can
1250 * check the version of the API that they are using. In the case of a
1251 * IPV6_FW_GET operation, the first rule of the output buffer passed to us
1252 * must have the version set. */
1253 if (!sopt
->sopt_val
|| sopt
->sopt_valsize
< sizeof rule
) return EINVAL
;
1255 /* save sopt->sopt_valsize */
1256 valsize
= sopt
->sopt_valsize
;
1257 if (error
= sooptcopyin(sopt
, &rule
, sizeof(rule
), sizeof(rule
)))
1260 if (rule
.version
!= IPV6_FW_CURRENT_API_VERSION
) return EINVAL
;
1261 rule
.version
= 0xFFFFFFFF; /* version is meaningless once rules "make it in the door". */
1263 switch (sopt
->sopt_name
)
1267 struct ip6_fw_chain
*fcp
;
1272 LIST_FOREACH(fcp
, &ip6_fw_chain
, chain
)
1273 size
+= sizeof *buf
;
1275 buf
= _MALLOC(size
, M_TEMP
, M_WAITOK
);
1276 if (!buf
) error
= ENOBUFS
;
1279 struct ip6_fw
*bp
= buf
;
1280 LIST_FOREACH(fcp
, &ip6_fw_chain
, chain
)
1282 bcopy(fcp
->rule
, bp
, sizeof *bp
);
1283 bp
->version
= IPV6_FW_CURRENT_API_VERSION
;
1291 sopt
->sopt_valsize
= valsize
;
1292 error
= sooptcopyout(sopt
, buf
, size
);
1301 while (ip6_fw_chain
.lh_first
&&
1302 ip6_fw_chain
.lh_first
->rule
->fw_number
!= (u_short
)-1)
1304 struct ip6_fw_chain
*fcp
= ip6_fw_chain
.lh_first
;
1305 LIST_REMOVE(ip6_fw_chain
.lh_first
, chain
);
1306 FREE(fcp
->rule
, M_IP6FW
);
1313 error
= zero_entry6(&rule
);
1317 if (check_ip6fw_struct(&rule
))
1318 error
= add_entry6(&ip6_fw_chain
, &rule
);
1324 if (rule
.fw_number
== (u_short
)-1)
1326 dprintf(("%s can't delete rule 65535\n", err_prefix
));
1330 error
= del_entry6(&ip6_fw_chain
, rule
.fw_number
);
1334 dprintf(("%s invalid option %d\n", err_prefix
, sopt
->sopt_name
));
1344 struct ip6_fw default_rule
;
1346 ip6_fw_chk_ptr
= ip6_fw_chk
;
1347 ip6_fw_ctl_ptr
= ip6_fw_ctl
;
1348 LIST_INIT(&ip6_fw_chain
);
1350 bzero(&default_rule
, sizeof default_rule
);
1351 default_rule
.fw_prot
= IPPROTO_IPV6
;
1352 default_rule
.fw_number
= (u_short
)-1;
1353 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1354 default_rule
.fw_flg
|= IPV6_FW_F_ACCEPT
;
1356 default_rule
.fw_flg
|= IPV6_FW_F_DENY
;
1358 default_rule
.fw_flg
|= IPV6_FW_F_IN
| IPV6_FW_F_OUT
;
1359 if (check_ip6fw_struct(&default_rule
) == NULL
||
1360 add_entry6(&ip6_fw_chain
, &default_rule
))
1361 panic(__FUNCTION__
);
1363 printf("IPv6 packet filtering initialized, ");
1364 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1365 printf("default to accept, ");
1367 #ifndef IPV6FIREWALL_VERBOSE
1368 printf("logging disabled\n");
1370 if (fw6_verbose_limit
== 0)
1371 printf("unlimited logging\n");
1373 printf("logging limited to %d packets/entry\n",