2 * Copyright (c) 2003 Apple Computer, 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>
103 #include <net/route.h>
104 #include <netinet/in_systm.h>
105 #include <netinet/in.h>
106 #include <netinet/ip.h>
108 #include <netinet/ip6.h>
109 #include <netinet6/ip6_var.h>
110 #include <netinet6/in6_var.h>
111 #include <netinet/icmp6.h>
113 #include <netinet/in_pcb.h>
115 #include <netinet6/ip6_fw.h>
116 #include <netinet/ip_var.h>
117 #include <netinet/tcp.h>
118 #include <netinet/tcp_seq.h>
119 #include <netinet/tcp_timer.h>
120 #include <netinet/tcp_var.h>
121 #include <netinet/udp.h>
123 #include <sys/sysctl.h>
125 #include <net/net_osdep.h>
127 MALLOC_DEFINE(M_IP6FW
, "Ip6Fw/Ip6Acct", "Ip6Fw/Ip6Acct chain's");
129 static int fw6_debug
= 1;
130 #ifdef IPV6FIREWALL_VERBOSE
131 static int fw6_verbose
= 1;
133 static int fw6_verbose
= 0;
135 #ifdef IPV6FIREWALL_VERBOSE_LIMIT
136 static int fw6_verbose_limit
= IPV6FIREWALL_VERBOSE_LIMIT
;
138 static int fw6_verbose_limit
= 0;
141 LIST_HEAD (ip6_fw_head
, ip6_fw_chain
) ip6_fw_chain
;
144 SYSCTL_DECL(_net_inet6_ip6
);
145 SYSCTL_NODE(_net_inet6_ip6
, OID_AUTO
, fw
, CTLFLAG_RW
, 0, "Firewall");
146 SYSCTL_INT(_net_inet6_ip6_fw
, OID_AUTO
, enable
, CTLFLAG_RW
,
147 &ip6_fw_enable
, 0, "Enable ip6fw");
148 SYSCTL_INT(_net_inet6_ip6_fw
, OID_AUTO
, debug
, CTLFLAG_RW
, &fw6_debug
, 0, "");
149 SYSCTL_INT(_net_inet6_ip6_fw
, OID_AUTO
, verbose
, CTLFLAG_RW
, &fw6_verbose
, 0, "");
150 SYSCTL_INT(_net_inet6_ip6_fw
, OID_AUTO
, verbose_limit
, CTLFLAG_RW
, &fw6_verbose_limit
, 0, "");
153 #define dprintf(a) do { \
157 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
159 static int add_entry6
__P((struct ip6_fw_head
*chainptr
, struct ip6_fw
*frwl
));
160 static int del_entry6
__P((struct ip6_fw_head
*chainptr
, u_short number
));
161 static int zero_entry6
__P((struct ip6_fw
*frwl
));
162 static struct ip6_fw
*check_ip6fw_struct
__P((struct ip6_fw
*m
));
163 static int ip6opts_match
__P((struct ip6_hdr
**ip6
, struct ip6_fw
*f
,
165 int *off
, int *nxt
, u_short
*offset
));
166 static int port_match6
__P((u_short
*portptr
, int nports
, u_short port
,
168 static int tcp6flg_match
__P((struct tcphdr
*tcp6
, struct ip6_fw
*f
));
169 static int icmp6type_match
__P((struct icmp6_hdr
* icmp
, struct ip6_fw
* f
));
170 static void ip6fw_report
__P((struct ip6_fw
*f
, struct ip6_hdr
*ip6
,
171 struct ifnet
*rif
, struct ifnet
*oif
, int off
, int nxt
));
173 static int ip6_fw_chk
__P((struct ip6_hdr
**pip6
,
174 struct ifnet
*oif
, u_int16_t
*cookie
, struct mbuf
**m
));
175 static int ip6_fw_ctl
__P((struct sockopt
*));
177 static char err_prefix
[] = "ip6_fw_ctl:";
178 extern lck_mtx_t
*ip6_mutex
;
181 * Returns 1 if the port is matched by the vector, 0 otherwise
185 port_match6(u_short
*portptr
, int nports
, u_short port
, int range_flag
)
190 if (portptr
[0] <= port
&& port
<= portptr
[1]) {
196 while (nports
-- > 0) {
197 if (*portptr
++ == port
) {
205 tcp6flg_match(struct tcphdr
*tcp6
, struct ip6_fw
*f
)
207 u_char flg_set
, flg_clr
;
210 * If an established connection is required, reject packets that
211 * have only SYN of RST|ACK|SYN set. Otherwise, fall through to
212 * other flag requirements.
214 if ((f
->fw_ipflg
& IPV6_FW_IF_TCPEST
) &&
215 ((tcp6
->th_flags
& (IPV6_FW_TCPF_RST
| IPV6_FW_TCPF_ACK
|
216 IPV6_FW_TCPF_SYN
)) == IPV6_FW_TCPF_SYN
))
219 flg_set
= tcp6
->th_flags
& f
->fw_tcpf
;
220 flg_clr
= tcp6
->th_flags
& f
->fw_tcpnf
;
222 if (flg_set
!= f
->fw_tcpf
)
231 icmp6type_match(struct icmp6_hdr
*icmp6
, struct ip6_fw
*f
)
235 if (!(f
->fw_flg
& IPV6_FW_F_ICMPBIT
))
238 type
= icmp6
->icmp6_type
;
240 /* check for matching type in the bitmap */
241 if (type
< IPV6_FW_ICMPTYPES_DIM
* sizeof(unsigned) * 8 &&
242 (f
->fw_icmp6types
[type
/ (sizeof(unsigned) * 8)] &
243 (1U << (type
% (8 * sizeof(unsigned))))))
246 return(0); /* no match */
250 is_icmp6_query(struct ip6_hdr
*ip6
, int off
)
252 const struct icmp6_hdr
*icmp6
;
255 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
256 icmp6_type
= icmp6
->icmp6_type
;
258 if (icmp6_type
== ICMP6_ECHO_REQUEST
||
259 icmp6_type
== ICMP6_MEMBERSHIP_QUERY
||
260 icmp6_type
== ICMP6_WRUREQUEST
||
261 icmp6_type
== ICMP6_FQDN_QUERY
||
262 icmp6_type
== ICMP6_NI_QUERY
)
269 ip6opts_match(struct ip6_hdr
**pip6
, struct ip6_fw
*f
, struct mbuf
**m
,
270 int *off
, int *nxt
, u_short
*offset
)
273 struct ip6_hdr
*ip6
= *pip6
;
274 struct ip6_ext
*ip6e
;
275 u_char opts
, nopts
, nopts_sve
;
278 nopts
= nopts_sve
= f
->fw_ip6nopt
;
281 *off
= sizeof(struct ip6_hdr
);
282 len
= ntohs(ip6
->ip6_plen
) + sizeof(struct ip6_hdr
);
284 ip6e
= (struct ip6_ext
*)((caddr_t
) ip6
+ *off
);
285 if ((*m
)->m_len
< *off
+ sizeof(*ip6e
))
286 goto opts_check
; /* XXX */
289 case IPPROTO_FRAGMENT
:
290 if ((*m
)->m_len
>= *off
+ sizeof(struct ip6_frag
)) {
291 struct ip6_frag
*ip6f
;
293 ip6f
= (struct ip6_frag
*) ((caddr_t
)ip6
+ *off
);
294 *offset
= ip6f
->ip6f_offlg
& IP6F_OFF_MASK
;
296 opts
&= ~IPV6_FW_IP6OPT_FRAG
;
297 nopts
&= ~IPV6_FW_IP6OPT_FRAG
;
298 *off
+= sizeof(struct ip6_frag
);
301 opts
&= ~IPV6_FW_IP6OPT_AH
;
302 nopts
&= ~IPV6_FW_IP6OPT_AH
;
303 *off
+= (ip6e
->ip6e_len
+ 2) << 2;
307 case IPPROTO_HOPOPTS
:
308 opts
&= ~IPV6_FW_IP6OPT_HOPOPT
;
309 nopts
&= ~IPV6_FW_IP6OPT_HOPOPT
;
311 case IPPROTO_ROUTING
:
312 opts
&= ~IPV6_FW_IP6OPT_ROUTE
;
313 nopts
&= ~IPV6_FW_IP6OPT_ROUTE
;
316 opts
&= ~IPV6_FW_IP6OPT_ESP
;
317 nopts
&= ~IPV6_FW_IP6OPT_ESP
;
320 opts
&= ~IPV6_FW_IP6OPT_NONXT
;
321 nopts
&= ~IPV6_FW_IP6OPT_NONXT
;
324 case IPPROTO_DSTOPTS
:
325 opts
&= ~IPV6_FW_IP6OPT_OPTS
;
326 nopts
&= ~IPV6_FW_IP6OPT_OPTS
;
332 *off
+= (ip6e
->ip6e_len
+ 1) << 3;
335 *nxt
= ip6e
->ip6e_nxt
;
339 if (f
->fw_ip6opt
== f
->fw_ip6nopt
) /* XXX */
342 if (opts
== 0 && nopts
== nopts_sve
)
350 iface_match(struct ifnet
*ifp
, union ip6_fw_if
*ifu
, int byname
)
352 /* Check by name or by IP address */
354 /* Check unit number (-1 is wildcard) */
355 if (ifu
->fu_via_if
.unit
!= -1
356 && ifp
->if_unit
!= ifu
->fu_via_if
.unit
)
359 if (strncmp(ifp
->if_name
, ifu
->fu_via_if
.name
, IP6FW_IFNLEN
))
362 } else if (!IN6_IS_ADDR_UNSPECIFIED(&ifu
->fu_via_ip6
)) { /* Zero == wildcard */
365 ifnet_lock_shared(ifp
);
366 for (ia
= ifp
->if_addrlist
.tqh_first
; ia
; ia
= ia
->ifa_list
.tqe_next
)
369 if (ia
->ifa_addr
== NULL
)
371 if (ia
->ifa_addr
->sa_family
!= AF_INET6
)
373 if (!IN6_ARE_ADDR_EQUAL(&ifu
->fu_via_ip6
,
374 &(((struct sockaddr_in6
*)
375 (ia
->ifa_addr
))->sin6_addr
)))
377 ifnet_lock_done(ifp
);
380 ifnet_lock_done(ifp
);
387 ip6fw_report(struct ip6_fw
*f
, struct ip6_hdr
*ip6
,
388 struct ifnet
*rif
, struct ifnet
*oif
, int off
, int nxt
)
391 struct tcphdr
*const tcp6
= (struct tcphdr
*) ((caddr_t
) ip6
+ off
);
392 struct udphdr
*const udp
= (struct udphdr
*) ((caddr_t
) ip6
+ off
);
393 struct icmp6_hdr
*const icmp6
= (struct icmp6_hdr
*) ((caddr_t
) ip6
+ off
);
396 char action2
[32], proto
[102], name
[18];
399 count
= f
? f
->fw_pcnt
: ++counter
;
400 if (fw6_verbose_limit
!= 0 && count
> fw6_verbose_limit
)
403 /* Print command name */
404 snprintf(SNPARGS(name
, 0), "ip6fw: %d", f
? f
->fw_number
: -1);
410 switch (f
->fw_flg
& IPV6_FW_F_COMMAND
) {
414 case IPV6_FW_F_REJECT
:
415 if (f
->fw_reject_code
== IPV6_FW_REJECT_RST
)
420 case IPV6_FW_F_ACCEPT
:
423 case IPV6_FW_F_COUNT
:
426 case IPV6_FW_F_DIVERT
:
427 snprintf(SNPARGS(action2
, 0), "Divert %d",
431 snprintf(SNPARGS(action2
, 0), "Tee %d",
434 case IPV6_FW_F_SKIPTO
:
435 snprintf(SNPARGS(action2
, 0), "SkipTo %d",
446 len
= snprintf(SNPARGS(proto
, 0), "TCP [%s]",
447 ip6_sprintf(&ip6
->ip6_src
));
449 len
+= snprintf(SNPARGS(proto
, len
), ":%d ",
450 ntohs(tcp6
->th_sport
));
452 len
+= snprintf(SNPARGS(proto
, len
), " ");
453 len
+= snprintf(SNPARGS(proto
, len
), "[%s]",
454 ip6_sprintf(&ip6
->ip6_dst
));
456 snprintf(SNPARGS(proto
, len
), ":%d",
457 ntohs(tcp6
->th_dport
));
460 len
= snprintf(SNPARGS(proto
, 0), "UDP [%s]",
461 ip6_sprintf(&ip6
->ip6_src
));
463 len
+= snprintf(SNPARGS(proto
, len
), ":%d ",
464 ntohs(udp
->uh_sport
));
466 len
+= snprintf(SNPARGS(proto
, len
), " ");
467 len
+= snprintf(SNPARGS(proto
, len
), "[%s]",
468 ip6_sprintf(&ip6
->ip6_dst
));
470 snprintf(SNPARGS(proto
, len
), ":%d",
471 ntohs(udp
->uh_dport
));
475 len
= snprintf(SNPARGS(proto
, 0), "IPV6-ICMP:%u.%u ",
476 icmp6
->icmp6_type
, icmp6
->icmp6_code
);
478 len
= snprintf(SNPARGS(proto
, 0), "IPV6-ICMP ");
479 len
+= snprintf(SNPARGS(proto
, len
), "[%s]",
480 ip6_sprintf(&ip6
->ip6_src
));
481 snprintf(SNPARGS(proto
, len
), " [%s]",
482 ip6_sprintf(&ip6
->ip6_dst
));
485 len
= snprintf(SNPARGS(proto
, 0), "P:%d [%s]", nxt
,
486 ip6_sprintf(&ip6
->ip6_src
));
487 snprintf(SNPARGS(proto
, len
), " [%s]",
488 ip6_sprintf(&ip6
->ip6_dst
));
493 log(LOG_AUTHPRIV
| LOG_INFO
, "%s %s %s out via %s\n",
494 name
, action
, proto
, if_name(oif
));
496 log(LOG_AUTHPRIV
| LOG_INFO
, "%s %s %s in via %s\n",
497 name
, action
, proto
, if_name(rif
));
499 log(LOG_AUTHPRIV
| LOG_INFO
, "%s %s %s",
500 name
, action
, proto
);
501 if (fw6_verbose_limit
!= 0 && count
== fw6_verbose_limit
)
502 log(LOG_AUTHPRIV
| LOG_INFO
, "ip6fw: limit reached on entry %d\n",
503 f
? f
->fw_number
: -1);
509 * ip Pointer to packet header (struct ip6_hdr *)
510 * hlen Packet header length
511 * oif Outgoing interface, or NULL if packet is incoming
512 * #ifndef IP6FW_DIVERT_RESTART
513 * *cookie Ignore all divert/tee rules to this port (if non-zero)
515 * *cookie Skip up to the first rule past this rule number;
517 * *m The packet; we set to NULL when/if we nuke it.
521 * 0 The packet is to be accepted and routed normally OR
522 * the packet was denied/rejected and has been dropped;
523 * in the latter case, *m is equal to NULL upon return.
524 * port Divert the packet to port.
528 ip6_fw_chk(struct ip6_hdr
**pip6
,
529 struct ifnet
*oif
, u_int16_t
*cookie
, struct mbuf
**m
)
531 struct ip6_fw_chain
*chain
;
532 struct ip6_fw
*rule
= NULL
;
533 struct ip6_hdr
*ip6
= *pip6
;
534 struct ifnet
*const rif
= (*m
)->m_pkthdr
.rcvif
;
536 int off
= sizeof(struct ip6_hdr
), nxt
= ip6
->ip6_nxt
;
537 u_short src_port
, dst_port
;
538 #ifdef IP6FW_DIVERT_RESTART
539 u_int16_t skipto
= *cookie
;
541 u_int16_t ignport
= ntohs(*cookie
);
543 struct timeval timenow
;
545 getmicrotime(&timenow
);
549 * Go down the chain, looking for enlightment
550 * #ifdef IP6FW_DIVERT_RESTART
551 * If we've been asked to start at a given rule immediatly, do so.
554 chain
= LIST_FIRST(&ip6_fw_chain
);
555 #ifdef IP6FW_DIVERT_RESTART
559 while (chain
&& (chain
->rule
->fw_number
<= skipto
)) {
560 chain
= LIST_NEXT(chain
, chain
);
562 if (! chain
) goto dropit
;
564 #endif /* IP6FW_DIVERT_RESTART */
565 for (; chain
; chain
= LIST_NEXT(chain
, chain
)) {
566 struct ip6_fw
*const f
= chain
->rule
;
569 /* Check direction outbound */
570 if (!(f
->fw_flg
& IPV6_FW_F_OUT
))
573 /* Check direction inbound */
574 if (!(f
->fw_flg
& IPV6_FW_F_IN
))
578 #define IN6_ARE_ADDR_MASKEQUAL(x,y,z) (\
579 (((x)->s6_addr32[0] & (y)->s6_addr32[0]) == (z)->s6_addr32[0]) && \
580 (((x)->s6_addr32[1] & (y)->s6_addr32[1]) == (z)->s6_addr32[1]) && \
581 (((x)->s6_addr32[2] & (y)->s6_addr32[2]) == (z)->s6_addr32[2]) && \
582 (((x)->s6_addr32[3] & (y)->s6_addr32[3]) == (z)->s6_addr32[3]))
584 /* If src-addr doesn't match, not this rule. */
585 if (((f
->fw_flg
& IPV6_FW_F_INVSRC
) != 0) ^
586 (!IN6_ARE_ADDR_MASKEQUAL(&ip6
->ip6_src
,&f
->fw_smsk
,&f
->fw_src
)))
589 /* If dest-addr doesn't match, not this rule. */
590 if (((f
->fw_flg
& IPV6_FW_F_INVDST
) != 0) ^
591 (!IN6_ARE_ADDR_MASKEQUAL(&ip6
->ip6_dst
,&f
->fw_dmsk
,&f
->fw_dst
)))
594 #undef IN6_ARE_ADDR_MASKEQUAL
595 /* Interface check */
596 if ((f
->fw_flg
& IF6_FW_F_VIAHACK
) == IF6_FW_F_VIAHACK
) {
597 struct ifnet
*const iface
= oif
? oif
: rif
;
599 /* Backwards compatibility hack for "via" */
600 if (!iface
|| !iface_match(iface
,
601 &f
->fw_in_if
, f
->fw_flg
& IPV6_FW_F_OIFNAME
))
604 /* Check receive interface */
605 if ((f
->fw_flg
& IPV6_FW_F_IIFACE
)
606 && (!rif
|| !iface_match(rif
,
607 &f
->fw_in_if
, f
->fw_flg
& IPV6_FW_F_IIFNAME
)))
609 /* Check outgoing interface */
610 if ((f
->fw_flg
& IPV6_FW_F_OIFACE
)
611 && (!oif
|| !iface_match(oif
,
612 &f
->fw_out_if
, f
->fw_flg
& IPV6_FW_F_OIFNAME
)))
616 /* Check IP options */
617 if (!ip6opts_match(&ip6
, f
, m
, &off
, &nxt
, &offset
))
621 if ((f
->fw_flg
& IPV6_FW_F_FRAG
) && !offset
)
624 /* Check protocol; if wildcard, match */
625 if (f
->fw_prot
== IPPROTO_IPV6
)
628 /* If different, don't match */
629 if (nxt
!= f
->fw_prot
)
632 #define PULLUP_TO(len) do { \
633 if ((*m)->m_len < (len) \
634 && (*m = m_pullup(*m, (len))) == 0) { \
637 *pip6 = ip6 = mtod(*m, struct ip6_hdr *); \
640 /* Protocol specific checks */
646 if (offset
== 1) { /* cf. RFC 1858 */
647 PULLUP_TO(off
+ 4); /* XXX ? */
652 * TCP flags and ports aren't available in this
653 * packet -- if this rule specified either one,
654 * we consider the rule a non-match.
656 if (f
->fw_nports
!= 0 ||
657 f
->fw_tcpf
!= f
->fw_tcpnf
)
663 tcp6
= (struct tcphdr
*) ((caddr_t
)ip6
+ off
);
664 if (((f
->fw_tcpf
!= f
->fw_tcpnf
) ||
665 (f
->fw_ipflg
& IPV6_FW_IF_TCPEST
)) &&
666 !tcp6flg_match(tcp6
, f
))
668 src_port
= ntohs(tcp6
->th_sport
);
669 dst_port
= ntohs(tcp6
->th_dport
);
679 * Port specification is unavailable -- if this
680 * rule specifies a port, we consider the rule
683 if (f
->fw_nports
!= 0)
689 udp
= (struct udphdr
*) ((caddr_t
)ip6
+ off
);
690 src_port
= ntohs(udp
->uh_sport
);
691 dst_port
= ntohs(udp
->uh_dport
);
693 if (!port_match6(&f
->fw_pts
[0],
694 IPV6_FW_GETNSRCP(f
), src_port
,
695 f
->fw_flg
& IPV6_FW_F_SRNG
))
697 if (!port_match6(&f
->fw_pts
[IPV6_FW_GETNSRCP(f
)],
698 IPV6_FW_GETNDSTP(f
), dst_port
,
699 f
->fw_flg
& IPV6_FW_F_DRNG
))
706 struct icmp6_hdr
*icmp
;
708 if (offset
!= 0) /* Type isn't valid */
711 icmp
= (struct icmp6_hdr
*) ((caddr_t
)ip6
+ off
);
712 if (!icmp6type_match(icmp
, f
))
720 ip6fw_report(NULL
, ip6
, rif
, oif
, off
, nxt
);
725 #ifndef IP6FW_DIVERT_RESTART
726 /* Ignore divert/tee rule if socket port is "ignport" */
727 switch (f
->fw_flg
& IPV6_FW_F_COMMAND
) {
728 case IPV6_FW_F_DIVERT
:
730 if (f
->fw_divert_port
== ignport
)
731 continue; /* ignore this rule */
735 #endif /* IP6FW_DIVERT_RESTART */
736 /* Update statistics */
738 f
->fw_bcnt
+= ntohs(ip6
->ip6_plen
);
739 f
->timestamp
= timenow
.tv_sec
;
741 /* Log to console if desired */
742 if ((f
->fw_flg
& IPV6_FW_F_PRN
) && fw6_verbose
)
743 ip6fw_report(f
, ip6
, rif
, oif
, off
, nxt
);
745 /* Take appropriate action */
746 switch (f
->fw_flg
& IPV6_FW_F_COMMAND
) {
747 case IPV6_FW_F_ACCEPT
:
749 case IPV6_FW_F_COUNT
:
751 case IPV6_FW_F_DIVERT
:
752 #ifdef IP6FW_DIVERT_RESTART
753 *cookie
= f
->fw_number
;
755 *cookie
= htons(f
->fw_divert_port
);
756 #endif /* IP6FW_DIVERT_RESTART */
757 return(f
->fw_divert_port
);
760 * XXX someday tee packet here, but beware that you
761 * can't use m_copym() or m_copypacket() because
762 * the divert input routine modifies the mbuf
763 * (and these routines only increment reference
764 * counts in the case of mbuf clusters), so need
765 * to write custom routine.
768 case IPV6_FW_F_SKIPTO
:
770 while (chain
->chain
.le_next
771 && chain
->chain
.le_next
->rule
->fw_number
774 while (chain
->chain
.le_next
->rule
->fw_number
777 chain
= chain
->chain
.le_next
;
781 /* Deny/reject this packet using this rule */
787 /* Rule 65535 should always be there and should always match */
789 panic("ip6_fw: chain");
793 * At this point, we're going to drop the packet.
794 * Send a reject notice if all of the following are true:
796 * - The packet matched a reject rule
797 * - The packet is not an ICMP packet, or is an ICMP query packet
798 * - The packet is not a multicast or broadcast packet
800 if ((rule
->fw_flg
& IPV6_FW_F_COMMAND
) == IPV6_FW_F_REJECT
801 && (nxt
!= IPPROTO_ICMPV6
|| is_icmp6_query(ip6
, off
))
802 && !((*m
)->m_flags
& (M_BCAST
|M_MCAST
))
803 && !IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
804 switch (rule
->fw_reject_code
) {
805 case IPV6_FW_REJECT_RST
:
807 struct tcphdr
*const tcp
=
808 (struct tcphdr
*) ((caddr_t
)ip6
+ off
);
816 if (offset
!= 0 || (tcp
->th_flags
& TH_RST
))
821 ti
.th
.th_seq
= ntohl(ti
.th
.th_seq
);
822 ti
.th
.th_ack
= ntohl(ti
.th
.th_ack
);
823 ti
.ip6
.ip6_nxt
= IPPROTO_TCP
;
824 if (ti
.th
.th_flags
& TH_ACK
) {
830 if (((*m
)->m_flags
& M_PKTHDR
) != 0) {
831 ack
+= (*m
)->m_pkthdr
.len
- off
832 - (ti
.th
.th_off
<< 2);
833 } else if (ip6
->ip6_plen
) {
834 ack
+= ntohs(ip6
->ip6_plen
) + sizeof(*ip6
)
835 - off
- (ti
.th
.th_off
<< 2);
842 flags
= TH_RST
|TH_ACK
;
844 bcopy(&ti
, ip6
, sizeof(ti
));
845 tcp_respond(NULL
, ip6
, (struct tcphdr
*)(ip6
+ 1),
846 *m
, ack
, seq
, flags
);
850 default: /* Send an ICMP unreachable using code */
852 (*m
)->m_pkthdr
.rcvif
= oif
;
853 lck_mtx_assert(ip6_mutex
, LCK_MTX_ASSERT_OWNED
);
854 lck_mtx_unlock(ip6_mutex
);
855 icmp6_error(*m
, ICMP6_DST_UNREACH
,
856 rule
->fw_reject_code
, 0);
857 lck_mtx_lock(ip6_mutex
);
865 * Finally, drop the packet.
875 add_entry6(struct ip6_fw_head
*chainptr
, struct ip6_fw
*frwl
)
877 struct ip6_fw
*ftmp
= 0;
878 struct ip6_fw_chain
*fwc
= 0, *fcp
, *fcpl
= 0;
882 fwc
= _MALLOC(sizeof *fwc
, M_IP6FW
, M_WAITOK
);
883 ftmp
= _MALLOC(sizeof *ftmp
, M_IP6FW
, M_WAITOK
);
885 dprintf(("%s malloc said no\n", err_prefix
));
886 if (fwc
) FREE(fwc
, M_IP6FW
);
887 if (ftmp
) FREE(ftmp
, M_IP6FW
);
891 bcopy(frwl
, ftmp
, sizeof(struct ip6_fw
));
892 ftmp
->fw_in_if
.fu_via_if
.name
[IP6FW_IFNLEN
- 1] = '\0';
899 if (!chainptr
->lh_first
) {
900 LIST_INSERT_HEAD(chainptr
, fwc
, chain
);
903 } else if (ftmp
->fw_number
== (u_short
)-1) {
904 if (fwc
) FREE(fwc
, M_IP6FW
);
905 if (ftmp
) FREE(ftmp
, M_IP6FW
);
907 dprintf(("%s bad rule number\n", err_prefix
));
911 /* If entry number is 0, find highest numbered rule and add 100 */
912 if (ftmp
->fw_number
== 0) {
913 for (fcp
= chainptr
->lh_first
; fcp
; fcp
= fcp
->chain
.le_next
) {
914 if (fcp
->rule
->fw_number
!= (u_short
)-1)
915 nbr
= fcp
->rule
->fw_number
;
919 if (nbr
< (u_short
)-1 - 100)
921 ftmp
->fw_number
= nbr
;
924 /* Got a valid number; now insert it, keeping the list ordered */
925 for (fcp
= chainptr
->lh_first
; fcp
; fcp
= fcp
->chain
.le_next
) {
926 if (fcp
->rule
->fw_number
> ftmp
->fw_number
) {
928 LIST_INSERT_AFTER(fcpl
, fwc
, chain
);
930 LIST_INSERT_HEAD(chainptr
, fwc
, chain
);
943 del_entry6(struct ip6_fw_head
*chainptr
, u_short number
)
945 struct ip6_fw_chain
*fcp
;
950 fcp
= chainptr
->lh_first
;
951 if (number
!= (u_short
)-1) {
952 for (; fcp
; fcp
= fcp
->chain
.le_next
) {
953 if (fcp
->rule
->fw_number
== number
) {
954 LIST_REMOVE(fcp
, chain
);
956 FREE(fcp
->rule
, M_IP6FW
);
968 zero_entry6(struct ip6_fw
*frwl
)
970 struct ip6_fw_chain
*fcp
;
974 * It's possible to insert multiple chain entries with the
975 * same number, so we don't stop after finding the first
976 * match if zeroing a specific entry.
979 for (fcp
= ip6_fw_chain
.lh_first
; fcp
; fcp
= fcp
->chain
.le_next
)
980 if (!frwl
|| frwl
->fw_number
== 0 || frwl
->fw_number
== fcp
->rule
->fw_number
) {
981 fcp
->rule
->fw_bcnt
= fcp
->rule
->fw_pcnt
= 0;
982 fcp
->rule
->timestamp
= 0;
988 log(LOG_AUTHPRIV
| LOG_NOTICE
,
989 "ip6fw: Entry %d cleared.\n", frwl
->fw_number
);
991 log(LOG_AUTHPRIV
| LOG_NOTICE
,
992 "ip6fw: Accounting cleared.\n");
998 static struct ip6_fw
*
999 check_ip6fw_struct(struct ip6_fw
*frwl
)
1001 /* Check for invalid flag bits */
1002 if ((frwl
->fw_flg
& ~IPV6_FW_F_MASK
) != 0) {
1003 dprintf(("%s undefined flag bits set (flags=%x)\n",
1004 err_prefix
, frwl
->fw_flg
));
1007 /* Must apply to incoming or outgoing (or both) */
1008 if (!(frwl
->fw_flg
& (IPV6_FW_F_IN
| IPV6_FW_F_OUT
))) {
1009 dprintf(("%s neither in nor out\n", err_prefix
));
1012 /* Empty interface name is no good */
1013 if (((frwl
->fw_flg
& IPV6_FW_F_IIFNAME
)
1014 && !*frwl
->fw_in_if
.fu_via_if
.name
)
1015 || ((frwl
->fw_flg
& IPV6_FW_F_OIFNAME
)
1016 && !*frwl
->fw_out_if
.fu_via_if
.name
)) {
1017 dprintf(("%s empty interface name\n", err_prefix
));
1020 /* Sanity check interface matching */
1021 if ((frwl
->fw_flg
& IF6_FW_F_VIAHACK
) == IF6_FW_F_VIAHACK
) {
1022 ; /* allow "via" backwards compatibility */
1023 } else if ((frwl
->fw_flg
& IPV6_FW_F_IN
)
1024 && (frwl
->fw_flg
& IPV6_FW_F_OIFACE
)) {
1025 dprintf(("%s outgoing interface check on incoming\n",
1029 /* Sanity check port ranges */
1030 if ((frwl
->fw_flg
& IPV6_FW_F_SRNG
) && IPV6_FW_GETNSRCP(frwl
) < 2) {
1031 dprintf(("%s src range set but n_src_p=%d\n",
1032 err_prefix
, IPV6_FW_GETNSRCP(frwl
)));
1035 if ((frwl
->fw_flg
& IPV6_FW_F_DRNG
) && IPV6_FW_GETNDSTP(frwl
) < 2) {
1036 dprintf(("%s dst range set but n_dst_p=%d\n",
1037 err_prefix
, IPV6_FW_GETNDSTP(frwl
)));
1040 if (IPV6_FW_GETNSRCP(frwl
) + IPV6_FW_GETNDSTP(frwl
) > IPV6_FW_MAX_PORTS
) {
1041 dprintf(("%s too many ports (%d+%d)\n",
1042 err_prefix
, IPV6_FW_GETNSRCP(frwl
), IPV6_FW_GETNDSTP(frwl
)));
1046 * Protocols other than TCP/UDP don't use port range
1048 if ((frwl
->fw_prot
!= IPPROTO_TCP
) &&
1049 (frwl
->fw_prot
!= IPPROTO_UDP
) &&
1050 (IPV6_FW_GETNSRCP(frwl
) || IPV6_FW_GETNDSTP(frwl
))) {
1051 dprintf(("%s port(s) specified for non TCP/UDP rule\n",
1057 * Rather than modify the entry to make such entries work,
1058 * we reject this rule and require user level utilities
1059 * to enforce whatever policy they deem appropriate.
1061 if ((frwl
->fw_src
.s6_addr32
[0] & (~frwl
->fw_smsk
.s6_addr32
[0])) ||
1062 (frwl
->fw_src
.s6_addr32
[1] & (~frwl
->fw_smsk
.s6_addr32
[1])) ||
1063 (frwl
->fw_src
.s6_addr32
[2] & (~frwl
->fw_smsk
.s6_addr32
[2])) ||
1064 (frwl
->fw_src
.s6_addr32
[3] & (~frwl
->fw_smsk
.s6_addr32
[3])) ||
1065 (frwl
->fw_dst
.s6_addr32
[0] & (~frwl
->fw_dmsk
.s6_addr32
[0])) ||
1066 (frwl
->fw_dst
.s6_addr32
[1] & (~frwl
->fw_dmsk
.s6_addr32
[1])) ||
1067 (frwl
->fw_dst
.s6_addr32
[2] & (~frwl
->fw_dmsk
.s6_addr32
[2])) ||
1068 (frwl
->fw_dst
.s6_addr32
[3] & (~frwl
->fw_dmsk
.s6_addr32
[3]))) {
1069 dprintf(("%s rule never matches\n", err_prefix
));
1073 if ((frwl
->fw_flg
& IPV6_FW_F_FRAG
) &&
1074 (frwl
->fw_prot
== IPPROTO_UDP
|| frwl
->fw_prot
== IPPROTO_TCP
)) {
1075 if (frwl
->fw_nports
) {
1076 dprintf(("%s cannot mix 'frag' and ports\n", err_prefix
));
1079 if (frwl
->fw_prot
== IPPROTO_TCP
&&
1080 frwl
->fw_tcpf
!= frwl
->fw_tcpnf
) {
1081 dprintf(("%s cannot mix 'frag' with TCP flags\n", err_prefix
));
1086 /* Check command specific stuff */
1087 switch (frwl
->fw_flg
& IPV6_FW_F_COMMAND
)
1089 case IPV6_FW_F_REJECT
:
1090 if (frwl
->fw_reject_code
>= 0x100
1091 && !(frwl
->fw_prot
== IPPROTO_TCP
1092 && frwl
->fw_reject_code
== IPV6_FW_REJECT_RST
)) {
1093 dprintf(("%s unknown reject code\n", err_prefix
));
1097 case IPV6_FW_F_DIVERT
: /* Diverting to port zero is invalid */
1099 if (frwl
->fw_divert_port
== 0) {
1100 dprintf(("%s can't divert to port 0\n", err_prefix
));
1104 case IPV6_FW_F_DENY
:
1105 case IPV6_FW_F_ACCEPT
:
1106 case IPV6_FW_F_COUNT
:
1107 case IPV6_FW_F_SKIPTO
:
1110 dprintf(("%s invalid command\n", err_prefix
));
1120 ip6_fw_ctl(int stage
, struct mbuf
**mm
)
1125 if (stage
== IPV6_FW_GET
) {
1126 struct ip6_fw_chain
*fcp
= ip6_fw_chain
.lh_first
;
1127 *mm
= m
= m_get(M_WAIT
, MT_DATA
); /* XXX */
1130 if (sizeof *(fcp
->rule
) > MLEN
) {
1132 if ((m
->m_flags
& M_EXT
) == 0) {
1137 for (; fcp
; fcp
= fcp
->chain
.le_next
) {
1138 bcopy(fcp
->rule
, m
->m_data
, sizeof *(fcp
->rule
));
1139 m
->m_len
= sizeof *(fcp
->rule
);
1140 m
->m_next
= m_get(M_WAIT
, MT_DATA
); /* XXX */
1146 if (sizeof *(fcp
->rule
) > MLEN
) {
1148 if ((m
->m_flags
& M_EXT
) == 0) {
1158 /* only allow get calls if secure mode > 2 */
1159 if (securelevel
> 2) {
1166 if (stage
== IPV6_FW_FLUSH
) {
1167 while (ip6_fw_chain
.lh_first
!= NULL
&&
1168 ip6_fw_chain
.lh_first
->rule
->fw_number
!= (u_short
)-1) {
1169 struct ip6_fw_chain
*fcp
= ip6_fw_chain
.lh_first
;
1171 LIST_REMOVE(ip6_fw_chain
.lh_first
, chain
);
1173 FREE(fcp
->rule
, M_IP6FW
);
1182 if (stage
== IPV6_FW_ZERO
) {
1183 error
= zero_entry6(m
);
1191 printf("%s NULL mbuf ptr\n", err_prefix
);
1195 if (stage
== IPV6_FW_ADD
) {
1196 struct ip6_fw
*frwl
= check_ip6fw_mbuf(m
);
1201 error
= add_entry6(&ip6_fw_chain
, frwl
);
1208 if (stage
== IPV6_FW_DEL
) {
1209 if (m
->m_len
!= sizeof(struct ip6_fw
)) {
1210 dprintf(("%s len=%ld, want %lu\n", err_prefix
, m
->m_len
,
1211 sizeof(struct ip6_fw
)));
1213 } else if (mtod(m
, struct ip6_fw
*)->fw_number
== (u_short
)-1) {
1214 dprintf(("%s can't delete rule 65535\n", err_prefix
));
1217 error
= del_entry6(&ip6_fw_chain
,
1218 mtod(m
, struct ip6_fw
*)->fw_number
);
1226 dprintf(("%s unknown request %d\n", err_prefix
, stage
));
1236 ip6_fw_ctl(struct sockopt
*sopt
)
1243 if (securelevel
>= 3 &&
1244 (sopt
->sopt_dir
!= SOPT_GET
|| sopt
->sopt_name
!= IPV6_FW_GET
))
1247 /* We ALWAYS expect the client to pass in a rule structure so that we can
1248 * check the version of the API that they are using. In the case of a
1249 * IPV6_FW_GET operation, the first rule of the output buffer passed to us
1250 * must have the version set. */
1251 if (!sopt
->sopt_val
|| sopt
->sopt_valsize
< sizeof rule
) return EINVAL
;
1253 /* save sopt->sopt_valsize */
1254 valsize
= sopt
->sopt_valsize
;
1255 if (error
= sooptcopyin(sopt
, &rule
, sizeof(rule
), sizeof(rule
)))
1258 if (rule
.version
!= IPV6_FW_CURRENT_API_VERSION
) return EINVAL
;
1259 rule
.version
= 0xFFFFFFFF; /* version is meaningless once rules "make it in the door". */
1261 switch (sopt
->sopt_name
)
1265 struct ip6_fw_chain
*fcp
;
1270 LIST_FOREACH(fcp
, &ip6_fw_chain
, chain
)
1271 size
+= sizeof *buf
;
1273 buf
= _MALLOC(size
, M_TEMP
, M_WAITOK
);
1274 if (!buf
) error
= ENOBUFS
;
1277 struct ip6_fw
*bp
= buf
;
1278 LIST_FOREACH(fcp
, &ip6_fw_chain
, chain
)
1280 bcopy(fcp
->rule
, bp
, sizeof *bp
);
1281 bp
->version
= IPV6_FW_CURRENT_API_VERSION
;
1289 sopt
->sopt_valsize
= valsize
;
1290 error
= sooptcopyout(sopt
, buf
, size
);
1299 while (ip6_fw_chain
.lh_first
&&
1300 ip6_fw_chain
.lh_first
->rule
->fw_number
!= (u_short
)-1)
1302 struct ip6_fw_chain
*fcp
= ip6_fw_chain
.lh_first
;
1303 LIST_REMOVE(ip6_fw_chain
.lh_first
, chain
);
1304 FREE(fcp
->rule
, M_IP6FW
);
1311 error
= zero_entry6(&rule
);
1315 if (check_ip6fw_struct(&rule
))
1316 error
= add_entry6(&ip6_fw_chain
, &rule
);
1322 if (rule
.fw_number
== (u_short
)-1)
1324 dprintf(("%s can't delete rule 65535\n", err_prefix
));
1328 error
= del_entry6(&ip6_fw_chain
, rule
.fw_number
);
1332 dprintf(("%s invalid option %d\n", err_prefix
, sopt
->sopt_name
));
1342 struct ip6_fw default_rule
;
1344 ip6_fw_chk_ptr
= ip6_fw_chk
;
1345 ip6_fw_ctl_ptr
= ip6_fw_ctl
;
1346 LIST_INIT(&ip6_fw_chain
);
1348 bzero(&default_rule
, sizeof default_rule
);
1349 default_rule
.fw_prot
= IPPROTO_IPV6
;
1350 default_rule
.fw_number
= (u_short
)-1;
1351 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1352 default_rule
.fw_flg
|= IPV6_FW_F_ACCEPT
;
1354 default_rule
.fw_flg
|= IPV6_FW_F_DENY
;
1356 default_rule
.fw_flg
|= IPV6_FW_F_IN
| IPV6_FW_F_OUT
;
1357 if (check_ip6fw_struct(&default_rule
) == NULL
||
1358 add_entry6(&ip6_fw_chain
, &default_rule
))
1359 panic(__FUNCTION__
);
1361 printf("IPv6 packet filtering initialized, ");
1362 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1363 printf("default to accept, ");
1365 #ifndef IPV6FIREWALL_VERBOSE
1366 printf("logging disabled\n");
1368 if (fw6_verbose_limit
== 0)
1369 printf("unlimited logging\n");
1371 printf("logging limited to %d packets/entry\n",