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
*));
199 static void cp_to_user_64( struct ip6_fw_64
*userrule_64
, struct ip6_fw
*rule
);
200 static void cp_from_user_64( struct ip6_fw_64
*userrule_64
, struct ip6_fw
*rule
);
201 static void cp_to_user_32( struct ip6_fw_32
*userrule_32
, struct ip6_fw
*rule
);
202 static void cp_from_user_32( struct ip6_fw_32
*userrule_32
, struct ip6_fw
*rule
);
204 static char err_prefix
[] = "ip6_fw_ctl:";
205 extern lck_mtx_t
*ip6_mutex
;
208 * Returns 1 if the port is matched by the vector, 0 otherwise
212 port_match6(u_short
*portptr
, int nports
, u_short port
, int range_flag
)
217 if (portptr
[0] <= port
&& port
<= portptr
[1]) {
223 while (nports
-- > 0) {
224 if (*portptr
++ == port
) {
232 tcp6flg_match(struct tcphdr
*tcp6
, struct ip6_fw
*f
)
234 u_char flg_set
, flg_clr
;
237 * If an established connection is required, reject packets that
238 * have only SYN of RST|ACK|SYN set. Otherwise, fall through to
239 * other flag requirements.
241 if ((f
->fw_ipflg
& IPV6_FW_IF_TCPEST
) &&
242 ((tcp6
->th_flags
& (IPV6_FW_TCPF_RST
| IPV6_FW_TCPF_ACK
|
243 IPV6_FW_TCPF_SYN
)) == IPV6_FW_TCPF_SYN
))
246 flg_set
= tcp6
->th_flags
& f
->fw_tcpf
;
247 flg_clr
= tcp6
->th_flags
& f
->fw_tcpnf
;
249 if (flg_set
!= f
->fw_tcpf
)
258 icmp6type_match(struct icmp6_hdr
*icmp6
, struct ip6_fw
*f
)
262 if (!(f
->fw_flg
& IPV6_FW_F_ICMPBIT
))
265 type
= icmp6
->icmp6_type
;
267 /* check for matching type in the bitmap */
268 if (type
< IPV6_FW_ICMPTYPES_DIM
* sizeof(unsigned) * 8 &&
269 (f
->fw_icmp6types
[type
/ (sizeof(unsigned) * 8)] &
270 (1U << (type
% (8 * sizeof(unsigned))))))
273 return(0); /* no match */
277 is_icmp6_query(struct ip6_hdr
*ip6
, int off
)
279 const struct icmp6_hdr
*icmp6
;
282 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
283 icmp6_type
= icmp6
->icmp6_type
;
285 if (icmp6_type
== ICMP6_ECHO_REQUEST
||
286 icmp6_type
== ICMP6_MEMBERSHIP_QUERY
||
287 icmp6_type
== ICMP6_WRUREQUEST
||
288 icmp6_type
== ICMP6_FQDN_QUERY
||
289 icmp6_type
== ICMP6_NI_QUERY
)
296 ip6opts_match(struct ip6_hdr
**pip6
, struct ip6_fw
*f
, struct mbuf
**m
,
297 int *off
, int *nxt
, u_short
*offset
)
300 struct ip6_hdr
*ip6
= *pip6
;
301 struct ip6_ext
*ip6e
;
302 u_char opts
, nopts
, nopts_sve
;
305 nopts
= nopts_sve
= f
->fw_ip6nopt
;
308 *off
= sizeof(struct ip6_hdr
);
309 len
= ntohs(ip6
->ip6_plen
) + sizeof(struct ip6_hdr
);
311 ip6e
= (struct ip6_ext
*)((caddr_t
) ip6
+ *off
);
312 if ((*m
)->m_len
< *off
+ sizeof(*ip6e
))
313 goto opts_check
; /* XXX */
316 case IPPROTO_FRAGMENT
:
317 if ((*m
)->m_len
>= *off
+ sizeof(struct ip6_frag
)) {
318 struct ip6_frag
*ip6f
;
320 ip6f
= (struct ip6_frag
*) ((caddr_t
)ip6
+ *off
);
321 *offset
= ip6f
->ip6f_offlg
& IP6F_OFF_MASK
;
323 opts
&= ~IPV6_FW_IP6OPT_FRAG
;
324 nopts
&= ~IPV6_FW_IP6OPT_FRAG
;
325 *off
+= sizeof(struct ip6_frag
);
328 opts
&= ~IPV6_FW_IP6OPT_AH
;
329 nopts
&= ~IPV6_FW_IP6OPT_AH
;
330 *off
+= (ip6e
->ip6e_len
+ 2) << 2;
334 case IPPROTO_HOPOPTS
:
335 opts
&= ~IPV6_FW_IP6OPT_HOPOPT
;
336 nopts
&= ~IPV6_FW_IP6OPT_HOPOPT
;
338 case IPPROTO_ROUTING
:
339 opts
&= ~IPV6_FW_IP6OPT_ROUTE
;
340 nopts
&= ~IPV6_FW_IP6OPT_ROUTE
;
343 opts
&= ~IPV6_FW_IP6OPT_ESP
;
344 nopts
&= ~IPV6_FW_IP6OPT_ESP
;
347 opts
&= ~IPV6_FW_IP6OPT_NONXT
;
348 nopts
&= ~IPV6_FW_IP6OPT_NONXT
;
351 case IPPROTO_DSTOPTS
:
352 opts
&= ~IPV6_FW_IP6OPT_OPTS
;
353 nopts
&= ~IPV6_FW_IP6OPT_OPTS
;
359 *off
+= (ip6e
->ip6e_len
+ 1) << 3;
362 *nxt
= ip6e
->ip6e_nxt
;
366 if (f
->fw_ip6opt
== f
->fw_ip6nopt
) /* XXX */
369 if (opts
== 0 && nopts
== nopts_sve
)
377 iface_match(struct ifnet
*ifp
, union ip6_fw_if
*ifu
, int byname
)
379 /* Check by name or by IP address */
381 /* Check unit number (-1 is wildcard) */
382 if (ifu
->fu_via_if
.unit
!= -1
383 && ifp
->if_unit
!= ifu
->fu_via_if
.unit
)
386 if (strncmp(ifp
->if_name
, ifu
->fu_via_if
.name
, IP6FW_IFNLEN
))
389 } else if (!IN6_IS_ADDR_UNSPECIFIED(&ifu
->fu_via_ip6
)) { /* Zero == wildcard */
392 ifnet_lock_shared(ifp
);
393 for (ia
= ifp
->if_addrlist
.tqh_first
; ia
; ia
= ia
->ifa_list
.tqe_next
)
396 if (ia
->ifa_addr
== NULL
)
398 if (ia
->ifa_addr
->sa_family
!= AF_INET6
)
400 if (!IN6_ARE_ADDR_EQUAL(&ifu
->fu_via_ip6
,
401 &(((struct sockaddr_in6
*)
402 (ia
->ifa_addr
))->sin6_addr
)))
404 ifnet_lock_done(ifp
);
407 ifnet_lock_done(ifp
);
414 ip6fw_report(struct ip6_fw
*f
, struct ip6_hdr
*ip6
,
415 struct ifnet
*rif
, struct ifnet
*oif
, int off
, int nxt
)
418 struct tcphdr
*const tcp6
= (struct tcphdr
*) ((caddr_t
) ip6
+ off
);
419 struct udphdr
*const udp
= (struct udphdr
*) ((caddr_t
) ip6
+ off
);
420 struct icmp6_hdr
*const icmp6
= (struct icmp6_hdr
*) ((caddr_t
) ip6
+ off
);
423 char action2
[32], proto
[102], name
[18];
426 count
= f
? f
->fw_pcnt
: ++counter
;
427 if (fw6_verbose_limit
!= 0 && count
> fw6_verbose_limit
)
430 /* Print command name */
431 snprintf(SNPARGS(name
, 0), "ip6fw: %d", f
? f
->fw_number
: -1);
437 switch (f
->fw_flg
& IPV6_FW_F_COMMAND
) {
441 case IPV6_FW_F_REJECT
:
442 if (f
->fw_reject_code
== IPV6_FW_REJECT_RST
)
447 case IPV6_FW_F_ACCEPT
:
450 case IPV6_FW_F_COUNT
:
453 case IPV6_FW_F_DIVERT
:
454 snprintf(SNPARGS(action2
, 0), "Divert %d",
458 snprintf(SNPARGS(action2
, 0), "Tee %d",
461 case IPV6_FW_F_SKIPTO
:
462 snprintf(SNPARGS(action2
, 0), "SkipTo %d",
473 len
= snprintf(SNPARGS(proto
, 0), "TCP [%s]",
474 ip6_sprintf(&ip6
->ip6_src
));
476 len
+= snprintf(SNPARGS(proto
, len
), ":%d ",
477 ntohs(tcp6
->th_sport
));
479 len
+= snprintf(SNPARGS(proto
, len
), " ");
480 len
+= snprintf(SNPARGS(proto
, len
), "[%s]",
481 ip6_sprintf(&ip6
->ip6_dst
));
483 snprintf(SNPARGS(proto
, len
), ":%d",
484 ntohs(tcp6
->th_dport
));
487 len
= snprintf(SNPARGS(proto
, 0), "UDP [%s]",
488 ip6_sprintf(&ip6
->ip6_src
));
490 len
+= snprintf(SNPARGS(proto
, len
), ":%d ",
491 ntohs(udp
->uh_sport
));
493 len
+= snprintf(SNPARGS(proto
, len
), " ");
494 len
+= snprintf(SNPARGS(proto
, len
), "[%s]",
495 ip6_sprintf(&ip6
->ip6_dst
));
497 snprintf(SNPARGS(proto
, len
), ":%d",
498 ntohs(udp
->uh_dport
));
502 len
= snprintf(SNPARGS(proto
, 0), "IPV6-ICMP:%u.%u ",
503 icmp6
->icmp6_type
, icmp6
->icmp6_code
);
505 len
= snprintf(SNPARGS(proto
, 0), "IPV6-ICMP ");
506 len
+= snprintf(SNPARGS(proto
, len
), "[%s]",
507 ip6_sprintf(&ip6
->ip6_src
));
508 snprintf(SNPARGS(proto
, len
), " [%s]",
509 ip6_sprintf(&ip6
->ip6_dst
));
512 len
= snprintf(SNPARGS(proto
, 0), "P:%d [%s]", nxt
,
513 ip6_sprintf(&ip6
->ip6_src
));
514 snprintf(SNPARGS(proto
, len
), " [%s]",
515 ip6_sprintf(&ip6
->ip6_dst
));
520 log(LOG_AUTHPRIV
| LOG_INFO
, "%s %s %s out via %s\n",
521 name
, action
, proto
, if_name(oif
));
523 log(LOG_AUTHPRIV
| LOG_INFO
, "%s %s %s in via %s\n",
524 name
, action
, proto
, if_name(rif
));
526 log(LOG_AUTHPRIV
| LOG_INFO
, "%s %s %s",
527 name
, action
, proto
);
528 if (fw6_verbose_limit
!= 0 && count
== fw6_verbose_limit
)
529 log(LOG_AUTHPRIV
| LOG_INFO
, "ip6fw: limit reached on entry %d\n",
530 f
? f
->fw_number
: -1);
536 * ip Pointer to packet header (struct ip6_hdr *)
537 * hlen Packet header length
538 * oif Outgoing interface, or NULL if packet is incoming
539 * #ifndef IP6FW_DIVERT_RESTART
540 * *cookie Ignore all divert/tee rules to this port (if non-zero)
542 * *cookie Skip up to the first rule past this rule number;
544 * *m The packet; we set to NULL when/if we nuke it.
548 * 0 The packet is to be accepted and routed normally OR
549 * the packet was denied/rejected and has been dropped;
550 * in the latter case, *m is equal to NULL upon return.
551 * port Divert the packet to port.
555 ip6_fw_chk(struct ip6_hdr
**pip6
,
556 struct ifnet
*oif
, u_int16_t
*cookie
, struct mbuf
**m
)
558 struct ip6_fw_chain
*chain
;
559 struct ip6_fw
*rule
= NULL
;
560 struct ip6_hdr
*ip6
= *pip6
;
561 struct ifnet
*const rif
= ((*m
)->m_flags
& M_LOOP
) ? ifunit("lo0") : (*m
)->m_pkthdr
.rcvif
;
563 int off
= sizeof(struct ip6_hdr
), nxt
= ip6
->ip6_nxt
;
564 u_short src_port
, dst_port
;
565 #ifdef IP6FW_DIVERT_RESTART
566 u_int16_t skipto
= *cookie
;
568 u_int16_t ignport
= ntohs(*cookie
);
570 struct timeval timenow
;
572 getmicrotime(&timenow
);
576 * Go down the chain, looking for enlightment
577 * #ifdef IP6FW_DIVERT_RESTART
578 * If we've been asked to start at a given rule immediatly, do so.
581 chain
= LIST_FIRST(&ip6_fw_chain
);
582 #ifdef IP6FW_DIVERT_RESTART
586 while (chain
&& (chain
->rule
->fw_number
<= skipto
)) {
587 chain
= LIST_NEXT(chain
, chain
);
589 if (! chain
) goto dropit
;
591 #endif /* IP6FW_DIVERT_RESTART */
592 for (; chain
; chain
= LIST_NEXT(chain
, chain
)) {
593 struct ip6_fw
*const f
= chain
->rule
;
596 /* Check direction outbound */
597 if (!(f
->fw_flg
& IPV6_FW_F_OUT
))
600 /* Check direction inbound */
601 if (!(f
->fw_flg
& IPV6_FW_F_IN
))
605 #define IN6_ARE_ADDR_MASKEQUAL(x,y,z) (\
606 (((x)->s6_addr32[0] & (y)->s6_addr32[0]) == (z)->s6_addr32[0]) && \
607 (((x)->s6_addr32[1] & (y)->s6_addr32[1]) == (z)->s6_addr32[1]) && \
608 (((x)->s6_addr32[2] & (y)->s6_addr32[2]) == (z)->s6_addr32[2]) && \
609 (((x)->s6_addr32[3] & (y)->s6_addr32[3]) == (z)->s6_addr32[3]))
611 /* If src-addr doesn't match, not this rule. */
612 if (((f
->fw_flg
& IPV6_FW_F_INVSRC
) != 0) ^
613 (!IN6_ARE_ADDR_MASKEQUAL(&ip6
->ip6_src
,&f
->fw_smsk
,&f
->fw_src
)))
616 /* If dest-addr doesn't match, not this rule. */
617 if (((f
->fw_flg
& IPV6_FW_F_INVDST
) != 0) ^
618 (!IN6_ARE_ADDR_MASKEQUAL(&ip6
->ip6_dst
,&f
->fw_dmsk
,&f
->fw_dst
)))
621 #undef IN6_ARE_ADDR_MASKEQUAL
622 /* Interface check */
623 if ((f
->fw_flg
& IF6_FW_F_VIAHACK
) == IF6_FW_F_VIAHACK
) {
624 struct ifnet
*const iface
= oif
? oif
: rif
;
626 /* Backwards compatibility hack for "via" */
627 if (!iface
|| !iface_match(iface
,
628 &f
->fw_in_if
, f
->fw_flg
& IPV6_FW_F_OIFNAME
))
631 /* Check receive interface */
632 if ((f
->fw_flg
& IPV6_FW_F_IIFACE
)
633 && (!rif
|| !iface_match(rif
,
634 &f
->fw_in_if
, f
->fw_flg
& IPV6_FW_F_IIFNAME
)))
636 /* Check outgoing interface */
637 if ((f
->fw_flg
& IPV6_FW_F_OIFACE
)
638 && (!oif
|| !iface_match(oif
,
639 &f
->fw_out_if
, f
->fw_flg
& IPV6_FW_F_OIFNAME
)))
643 /* Check IP options */
644 if (!ip6opts_match(&ip6
, f
, m
, &off
, &nxt
, &offset
))
648 if ((f
->fw_flg
& IPV6_FW_F_FRAG
) && !offset
)
651 /* Check protocol; if wildcard, match */
652 if (f
->fw_prot
== IPPROTO_IPV6
)
655 /* If different, don't match */
656 if (nxt
!= f
->fw_prot
)
659 #define PULLUP_TO(len) do { \
660 if ((*m)->m_len < (len) \
661 && (*m = m_pullup(*m, (len))) == 0) { \
664 *pip6 = ip6 = mtod(*m, struct ip6_hdr *); \
667 /* Protocol specific checks */
673 if (offset
== 1) { /* cf. RFC 1858 */
674 PULLUP_TO(off
+ 4); /* XXX ? */
679 * TCP flags and ports aren't available in this
680 * packet -- if this rule specified either one,
681 * we consider the rule a non-match.
683 if (f
->fw_nports
!= 0 ||
684 f
->fw_tcpf
!= f
->fw_tcpnf
)
690 tcp6
= (struct tcphdr
*) ((caddr_t
)ip6
+ off
);
691 if (((f
->fw_tcpf
!= f
->fw_tcpnf
) ||
692 (f
->fw_ipflg
& IPV6_FW_IF_TCPEST
)) &&
693 !tcp6flg_match(tcp6
, f
))
695 src_port
= ntohs(tcp6
->th_sport
);
696 dst_port
= ntohs(tcp6
->th_dport
);
706 * Port specification is unavailable -- if this
707 * rule specifies a port, we consider the rule
710 if (f
->fw_nports
!= 0)
716 udp
= (struct udphdr
*) ((caddr_t
)ip6
+ off
);
717 src_port
= ntohs(udp
->uh_sport
);
718 dst_port
= ntohs(udp
->uh_dport
);
720 if (!port_match6(&f
->fw_pts
[0],
721 IPV6_FW_GETNSRCP(f
), src_port
,
722 f
->fw_flg
& IPV6_FW_F_SRNG
))
724 if (!port_match6(&f
->fw_pts
[IPV6_FW_GETNSRCP(f
)],
725 IPV6_FW_GETNDSTP(f
), dst_port
,
726 f
->fw_flg
& IPV6_FW_F_DRNG
))
733 struct icmp6_hdr
*icmp
;
735 if (offset
!= 0) /* Type isn't valid */
738 icmp
= (struct icmp6_hdr
*) ((caddr_t
)ip6
+ off
);
739 if (!icmp6type_match(icmp
, f
))
747 ip6fw_report(NULL
, ip6
, rif
, oif
, off
, nxt
);
752 #ifndef IP6FW_DIVERT_RESTART
753 /* Ignore divert/tee rule if socket port is "ignport" */
754 switch (f
->fw_flg
& IPV6_FW_F_COMMAND
) {
755 case IPV6_FW_F_DIVERT
:
757 if (f
->fw_divert_port
== ignport
)
758 continue; /* ignore this rule */
762 #endif /* IP6FW_DIVERT_RESTART */
763 /* Update statistics */
765 f
->fw_bcnt
+= ntohs(ip6
->ip6_plen
);
766 f
->timestamp
= timenow
.tv_sec
;
768 /* Log to console if desired */
769 if ((f
->fw_flg
& IPV6_FW_F_PRN
) && fw6_verbose
)
770 ip6fw_report(f
, ip6
, rif
, oif
, off
, nxt
);
772 /* Take appropriate action */
773 switch (f
->fw_flg
& IPV6_FW_F_COMMAND
) {
774 case IPV6_FW_F_ACCEPT
:
776 case IPV6_FW_F_COUNT
:
778 case IPV6_FW_F_DIVERT
:
779 #ifdef IP6FW_DIVERT_RESTART
780 *cookie
= f
->fw_number
;
782 *cookie
= htons(f
->fw_divert_port
);
783 #endif /* IP6FW_DIVERT_RESTART */
784 return(f
->fw_divert_port
);
787 * XXX someday tee packet here, but beware that you
788 * can't use m_copym() or m_copypacket() because
789 * the divert input routine modifies the mbuf
790 * (and these routines only increment reference
791 * counts in the case of mbuf clusters), so need
792 * to write custom routine.
795 case IPV6_FW_F_SKIPTO
:
797 while (chain
->chain
.le_next
798 && chain
->chain
.le_next
->rule
->fw_number
801 while (chain
->chain
.le_next
->rule
->fw_number
804 chain
= chain
->chain
.le_next
;
808 /* Deny/reject this packet using this rule */
814 /* Rule 65535 should always be there and should always match */
816 panic("ip6_fw: chain");
820 * At this point, we're going to drop the packet.
821 * Send a reject notice if all of the following are true:
823 * - The packet matched a reject rule
824 * - The packet is not an ICMP packet, or is an ICMP query packet
825 * - The packet is not a multicast or broadcast packet
827 if ((rule
->fw_flg
& IPV6_FW_F_COMMAND
) == IPV6_FW_F_REJECT
828 && (nxt
!= IPPROTO_ICMPV6
|| is_icmp6_query(ip6
, off
))
829 && !((*m
)->m_flags
& (M_BCAST
|M_MCAST
))
830 && !IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
831 switch (rule
->fw_reject_code
) {
832 case IPV6_FW_REJECT_RST
:
834 struct tcphdr
*const tcp
=
835 (struct tcphdr
*) ((caddr_t
)ip6
+ off
);
843 if (offset
!= 0 || (tcp
->th_flags
& TH_RST
))
848 ti
.th
.th_seq
= ntohl(ti
.th
.th_seq
);
849 ti
.th
.th_ack
= ntohl(ti
.th
.th_ack
);
850 ti
.ip6
.ip6_nxt
= IPPROTO_TCP
;
851 if (ti
.th
.th_flags
& TH_ACK
) {
857 if (((*m
)->m_flags
& M_PKTHDR
) != 0) {
858 ack
+= (*m
)->m_pkthdr
.len
- off
859 - (ti
.th
.th_off
<< 2);
860 } else if (ip6
->ip6_plen
) {
861 ack
+= ntohs(ip6
->ip6_plen
) + sizeof(*ip6
)
862 - off
- (ti
.th
.th_off
<< 2);
869 flags
= TH_RST
|TH_ACK
;
871 bcopy(&ti
, ip6
, sizeof(ti
));
872 tcp_respond(NULL
, ip6
, (struct tcphdr
*)(ip6
+ 1),
873 *m
, ack
, seq
, flags
, IFSCOPE_NONE
);
877 default: /* Send an ICMP unreachable using code */
879 (*m
)->m_pkthdr
.rcvif
= oif
;
880 lck_mtx_assert(ip6_mutex
, LCK_MTX_ASSERT_OWNED
);
881 lck_mtx_unlock(ip6_mutex
);
882 icmp6_error(*m
, ICMP6_DST_UNREACH
,
883 rule
->fw_reject_code
, 0);
884 lck_mtx_lock(ip6_mutex
);
892 * Finally, drop the packet.
902 add_entry6(struct ip6_fw_head
*chainptr
, struct ip6_fw
*frwl
)
904 struct ip6_fw
*ftmp
= 0;
905 struct ip6_fw_chain
*fwc
= 0, *fcp
, *fcpl
= 0;
909 fwc
= _MALLOC(sizeof *fwc
, M_IP6FW
, M_WAITOK
);
910 ftmp
= _MALLOC(sizeof *ftmp
, M_IP6FW
, M_WAITOK
);
912 dprintf(("%s malloc said no\n", err_prefix
));
913 if (fwc
) FREE(fwc
, M_IP6FW
);
914 if (ftmp
) FREE(ftmp
, M_IP6FW
);
918 bcopy(frwl
, ftmp
, sizeof(struct ip6_fw
));
919 ftmp
->fw_in_if
.fu_via_if
.name
[IP6FW_IFNLEN
- 1] = '\0';
926 if (!chainptr
->lh_first
) {
927 LIST_INSERT_HEAD(chainptr
, fwc
, chain
);
930 } else if (ftmp
->fw_number
== (u_short
)-1) {
931 if (fwc
) FREE(fwc
, M_IP6FW
);
932 if (ftmp
) FREE(ftmp
, M_IP6FW
);
934 dprintf(("%s bad rule number\n", err_prefix
));
938 /* If entry number is 0, find highest numbered rule and add 100 */
939 if (ftmp
->fw_number
== 0) {
940 for (fcp
= chainptr
->lh_first
; fcp
; fcp
= fcp
->chain
.le_next
) {
941 if (fcp
->rule
->fw_number
!= (u_short
)-1)
942 nbr
= fcp
->rule
->fw_number
;
946 if (nbr
< (u_short
)-1 - 100)
948 ftmp
->fw_number
= nbr
;
951 /* Got a valid number; now insert it, keeping the list ordered */
952 for (fcp
= chainptr
->lh_first
; fcp
; fcp
= fcp
->chain
.le_next
) {
953 if (fcp
->rule
->fw_number
> ftmp
->fw_number
) {
955 LIST_INSERT_AFTER(fcpl
, fwc
, chain
);
957 LIST_INSERT_HEAD(chainptr
, fwc
, chain
);
970 del_entry6(struct ip6_fw_head
*chainptr
, u_short number
)
972 struct ip6_fw_chain
*fcp
;
977 fcp
= chainptr
->lh_first
;
978 if (number
!= (u_short
)-1) {
979 for (; fcp
; fcp
= fcp
->chain
.le_next
) {
980 if (fcp
->rule
->fw_number
== number
) {
981 LIST_REMOVE(fcp
, chain
);
983 FREE(fcp
->rule
, M_IP6FW
);
995 zero_entry6(struct ip6_fw
*frwl
)
997 struct ip6_fw_chain
*fcp
;
1001 * It's possible to insert multiple chain entries with the
1002 * same number, so we don't stop after finding the first
1003 * match if zeroing a specific entry.
1006 for (fcp
= ip6_fw_chain
.lh_first
; fcp
; fcp
= fcp
->chain
.le_next
)
1007 if (!frwl
|| frwl
->fw_number
== 0 || frwl
->fw_number
== fcp
->rule
->fw_number
) {
1008 fcp
->rule
->fw_bcnt
= fcp
->rule
->fw_pcnt
= 0;
1009 fcp
->rule
->timestamp
= 0;
1015 log(LOG_AUTHPRIV
| LOG_NOTICE
,
1016 "ip6fw: Entry %d cleared.\n", frwl
->fw_number
);
1018 log(LOG_AUTHPRIV
| LOG_NOTICE
,
1019 "ip6fw: Accounting cleared.\n");
1025 static struct ip6_fw
*
1026 check_ip6fw_struct(struct ip6_fw
*frwl
)
1028 /* Check for invalid flag bits */
1029 if ((frwl
->fw_flg
& ~IPV6_FW_F_MASK
) != 0) {
1030 dprintf(("%s undefined flag bits set (flags=%x)\n",
1031 err_prefix
, frwl
->fw_flg
));
1034 /* Must apply to incoming or outgoing (or both) */
1035 if (!(frwl
->fw_flg
& (IPV6_FW_F_IN
| IPV6_FW_F_OUT
))) {
1036 dprintf(("%s neither in nor out\n", err_prefix
));
1039 /* Empty interface name is no good */
1040 if (((frwl
->fw_flg
& IPV6_FW_F_IIFNAME
)
1041 && !*frwl
->fw_in_if
.fu_via_if
.name
)
1042 || ((frwl
->fw_flg
& IPV6_FW_F_OIFNAME
)
1043 && !*frwl
->fw_out_if
.fu_via_if
.name
)) {
1044 dprintf(("%s empty interface name\n", err_prefix
));
1047 /* Sanity check interface matching */
1048 if ((frwl
->fw_flg
& IF6_FW_F_VIAHACK
) == IF6_FW_F_VIAHACK
) {
1049 ; /* allow "via" backwards compatibility */
1050 } else if ((frwl
->fw_flg
& IPV6_FW_F_IN
)
1051 && (frwl
->fw_flg
& IPV6_FW_F_OIFACE
)) {
1052 dprintf(("%s outgoing interface check on incoming\n",
1056 /* Sanity check port ranges */
1057 if ((frwl
->fw_flg
& IPV6_FW_F_SRNG
) && IPV6_FW_GETNSRCP(frwl
) < 2) {
1058 dprintf(("%s src range set but n_src_p=%d\n",
1059 err_prefix
, IPV6_FW_GETNSRCP(frwl
)));
1062 if ((frwl
->fw_flg
& IPV6_FW_F_DRNG
) && IPV6_FW_GETNDSTP(frwl
) < 2) {
1063 dprintf(("%s dst range set but n_dst_p=%d\n",
1064 err_prefix
, IPV6_FW_GETNDSTP(frwl
)));
1067 if (IPV6_FW_GETNSRCP(frwl
) + IPV6_FW_GETNDSTP(frwl
) > IPV6_FW_MAX_PORTS
) {
1068 dprintf(("%s too many ports (%d+%d)\n",
1069 err_prefix
, IPV6_FW_GETNSRCP(frwl
), IPV6_FW_GETNDSTP(frwl
)));
1073 * Protocols other than TCP/UDP don't use port range
1075 if ((frwl
->fw_prot
!= IPPROTO_TCP
) &&
1076 (frwl
->fw_prot
!= IPPROTO_UDP
) &&
1077 (IPV6_FW_GETNSRCP(frwl
) || IPV6_FW_GETNDSTP(frwl
))) {
1078 dprintf(("%s port(s) specified for non TCP/UDP rule\n",
1084 * Rather than modify the entry to make such entries work,
1085 * we reject this rule and require user level utilities
1086 * to enforce whatever policy they deem appropriate.
1088 if ((frwl
->fw_src
.s6_addr32
[0] & (~frwl
->fw_smsk
.s6_addr32
[0])) ||
1089 (frwl
->fw_src
.s6_addr32
[1] & (~frwl
->fw_smsk
.s6_addr32
[1])) ||
1090 (frwl
->fw_src
.s6_addr32
[2] & (~frwl
->fw_smsk
.s6_addr32
[2])) ||
1091 (frwl
->fw_src
.s6_addr32
[3] & (~frwl
->fw_smsk
.s6_addr32
[3])) ||
1092 (frwl
->fw_dst
.s6_addr32
[0] & (~frwl
->fw_dmsk
.s6_addr32
[0])) ||
1093 (frwl
->fw_dst
.s6_addr32
[1] & (~frwl
->fw_dmsk
.s6_addr32
[1])) ||
1094 (frwl
->fw_dst
.s6_addr32
[2] & (~frwl
->fw_dmsk
.s6_addr32
[2])) ||
1095 (frwl
->fw_dst
.s6_addr32
[3] & (~frwl
->fw_dmsk
.s6_addr32
[3]))) {
1096 dprintf(("%s rule never matches\n", err_prefix
));
1100 if ((frwl
->fw_flg
& IPV6_FW_F_FRAG
) &&
1101 (frwl
->fw_prot
== IPPROTO_UDP
|| frwl
->fw_prot
== IPPROTO_TCP
)) {
1102 if (frwl
->fw_nports
) {
1103 dprintf(("%s cannot mix 'frag' and ports\n", err_prefix
));
1106 if (frwl
->fw_prot
== IPPROTO_TCP
&&
1107 frwl
->fw_tcpf
!= frwl
->fw_tcpnf
) {
1108 dprintf(("%s cannot mix 'frag' with TCP flags\n", err_prefix
));
1113 /* Check command specific stuff */
1114 switch (frwl
->fw_flg
& IPV6_FW_F_COMMAND
)
1116 case IPV6_FW_F_REJECT
:
1117 if (frwl
->fw_reject_code
>= 0x100
1118 && !(frwl
->fw_prot
== IPPROTO_TCP
1119 && frwl
->fw_reject_code
== IPV6_FW_REJECT_RST
)) {
1120 dprintf(("%s unknown reject code\n", err_prefix
));
1124 case IPV6_FW_F_DIVERT
: /* Diverting to port zero is invalid */
1126 if (frwl
->fw_divert_port
== 0) {
1127 dprintf(("%s can't divert to port 0\n", err_prefix
));
1131 case IPV6_FW_F_DENY
:
1132 case IPV6_FW_F_ACCEPT
:
1133 case IPV6_FW_F_COUNT
:
1134 case IPV6_FW_F_SKIPTO
:
1137 dprintf(("%s invalid command\n", err_prefix
));
1145 ip6fw_kev_post_msg(u_int32_t event_code
)
1147 struct kev_msg ev_msg
;
1149 bzero(&ev_msg
, sizeof(struct kev_msg
));
1151 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1152 ev_msg
.kev_class
= KEV_FIREWALL_CLASS
;
1153 ev_msg
.kev_subclass
= KEV_IP6FW_SUBCLASS
;
1154 ev_msg
.event_code
= event_code
;
1156 kev_post_msg(&ev_msg
);
1162 cp_to_user_64( struct ip6_fw_64
*userrule_64
, struct ip6_fw
*rule
)
1164 userrule_64
->version
= rule
->version
;
1165 userrule_64
->context
= CAST_USER_ADDR_T(rule
->context
);
1166 userrule_64
->fw_pcnt
= rule
->fw_pcnt
;
1167 userrule_64
->fw_bcnt
= rule
->fw_bcnt
;
1168 userrule_64
->fw_src
= rule
->fw_src
;
1169 userrule_64
->fw_dst
= rule
->fw_dst
;
1170 userrule_64
->fw_smsk
= rule
->fw_smsk
;
1171 userrule_64
->fw_dmsk
= rule
->fw_dmsk
;
1172 userrule_64
->fw_number
= rule
->fw_number
;
1173 userrule_64
->fw_flg
= rule
->fw_flg
;
1174 userrule_64
->fw_ipflg
= rule
->fw_ipflg
;
1175 bcopy( rule
->fw_pts
, userrule_64
->fw_pts
, IPV6_FW_MAX_PORTS
);
1176 userrule_64
->fw_ip6opt
= rule
->fw_ip6opt
;
1177 userrule_64
->fw_ip6nopt
= rule
->fw_ip6nopt
;
1178 userrule_64
->fw_tcpf
= rule
->fw_tcpf
;
1179 userrule_64
->fw_tcpnf
= rule
->fw_tcpnf
;
1180 bcopy( rule
->fw_icmp6types
, userrule_64
->fw_icmp6types
, sizeof(userrule_64
->fw_icmp6types
));
1181 userrule_64
->fw_in_if
= rule
->fw_in_if
;
1182 userrule_64
->fw_out_if
= rule
->fw_out_if
;
1183 userrule_64
->timestamp
= rule
->timestamp
;
1184 userrule_64
->fw_un
.fu_divert_port
= rule
->fw_un
.fu_divert_port
;
1185 userrule_64
->fw_prot
= rule
->fw_prot
;
1186 userrule_64
->fw_nports
= rule
->fw_nports
;
1191 cp_from_user_64( struct ip6_fw_64
*userrule_64
, struct ip6_fw
*rule
)
1193 rule
->version
= userrule_64
->version
;
1194 rule
->context
= CAST_DOWN(void *, userrule_64
->context
);
1195 rule
->fw_pcnt
= userrule_64
->fw_pcnt
;
1196 rule
->fw_bcnt
= userrule_64
->fw_bcnt
;
1197 rule
->fw_src
= userrule_64
->fw_src
;
1198 rule
->fw_dst
= userrule_64
->fw_dst
;
1199 rule
->fw_smsk
= userrule_64
->fw_smsk
;
1200 rule
->fw_dmsk
= userrule_64
->fw_dmsk
;
1201 rule
->fw_number
= userrule_64
->fw_number
;
1202 rule
->fw_flg
= userrule_64
->fw_flg
;
1203 rule
->fw_ipflg
= userrule_64
->fw_ipflg
;
1204 bcopy( userrule_64
->fw_pts
, rule
->fw_pts
, IPV6_FW_MAX_PORTS
);
1205 rule
->fw_ip6opt
= userrule_64
->fw_ip6opt
;
1206 rule
->fw_ip6nopt
= userrule_64
->fw_ip6nopt
;
1207 rule
->fw_tcpf
= userrule_64
->fw_tcpf
;
1208 rule
->fw_tcpnf
= userrule_64
->fw_tcpnf
;
1209 bcopy( userrule_64
->fw_icmp6types
, rule
->fw_icmp6types
, sizeof(userrule_64
->fw_icmp6types
));
1210 rule
->fw_in_if
= userrule_64
->fw_in_if
;
1211 rule
->fw_out_if
= userrule_64
->fw_out_if
;
1212 rule
->timestamp
= CAST_DOWN( long, userrule_64
->timestamp
);
1213 rule
->fw_un
.fu_divert_port
= userrule_64
->fw_un
.fu_divert_port
;
1214 rule
->fw_prot
= userrule_64
->fw_prot
;
1215 rule
->fw_nports
= userrule_64
->fw_nports
;
1220 cp_to_user_32( struct ip6_fw_32
*userrule_32
, struct ip6_fw
*rule
)
1222 userrule_32
->version
= rule
->version
;
1223 userrule_32
->context
= CAST_DOWN_EXPLICIT( user32_addr_t
, rule
->context
);
1224 userrule_32
->fw_pcnt
= rule
->fw_pcnt
;
1225 userrule_32
->fw_bcnt
= rule
->fw_bcnt
;
1226 userrule_32
->fw_src
= rule
->fw_src
;
1227 userrule_32
->fw_dst
= rule
->fw_dst
;
1228 userrule_32
->fw_smsk
= rule
->fw_smsk
;
1229 userrule_32
->fw_dmsk
= rule
->fw_dmsk
;
1230 userrule_32
->fw_number
= rule
->fw_number
;
1231 userrule_32
->fw_flg
= rule
->fw_flg
;
1232 userrule_32
->fw_ipflg
= rule
->fw_ipflg
;
1233 bcopy( rule
->fw_pts
, userrule_32
->fw_pts
, IPV6_FW_MAX_PORTS
);
1234 userrule_32
->fw_ip6opt
= rule
->fw_ip6opt
;
1235 userrule_32
->fw_ip6nopt
= rule
->fw_ip6nopt
;
1236 userrule_32
->fw_tcpf
= rule
->fw_tcpf
;
1237 userrule_32
->fw_tcpnf
= rule
->fw_tcpnf
;
1238 bcopy( rule
->fw_icmp6types
, userrule_32
->fw_icmp6types
, sizeof(rule
->fw_icmp6types
));
1239 userrule_32
->fw_in_if
= rule
->fw_in_if
;
1240 userrule_32
->fw_out_if
= rule
->fw_out_if
;
1241 userrule_32
->timestamp
= rule
->timestamp
;
1242 userrule_32
->fw_un
.fu_divert_port
= rule
->fw_un
.fu_divert_port
;
1243 userrule_32
->fw_prot
= rule
->fw_prot
;
1244 userrule_32
->fw_nports
= rule
->fw_nports
;
1249 cp_from_user_32( struct ip6_fw_32
*userrule_32
, struct ip6_fw
*rule
)
1251 rule
->version
= userrule_32
->version
;
1252 rule
->context
= CAST_DOWN(void *, userrule_32
->context
);
1253 rule
->fw_pcnt
= userrule_32
->fw_pcnt
;
1254 rule
->fw_bcnt
= userrule_32
->fw_bcnt
;
1255 rule
->fw_src
= userrule_32
->fw_src
;
1256 rule
->fw_dst
= userrule_32
->fw_dst
;
1257 rule
->fw_smsk
= userrule_32
->fw_smsk
;
1258 rule
->fw_dmsk
= userrule_32
->fw_dmsk
;
1259 rule
->fw_number
= userrule_32
->fw_number
;
1260 rule
->fw_flg
= userrule_32
->fw_flg
;
1261 rule
->fw_ipflg
= userrule_32
->fw_ipflg
;
1262 bcopy( userrule_32
->fw_pts
, rule
->fw_pts
, IPV6_FW_MAX_PORTS
);
1263 rule
->fw_ip6opt
= userrule_32
->fw_ip6opt
;
1264 rule
->fw_ip6nopt
= userrule_32
->fw_ip6nopt
;
1265 rule
->fw_tcpf
= userrule_32
->fw_tcpf
;
1266 rule
->fw_tcpnf
= userrule_32
->fw_tcpnf
;
1267 bcopy( userrule_32
->fw_icmp6types
, rule
->fw_icmp6types
, sizeof(userrule_32
->fw_icmp6types
));
1268 rule
->fw_in_if
= userrule_32
->fw_in_if
;
1269 rule
->fw_out_if
= userrule_32
->fw_out_if
;
1270 rule
->timestamp
= CAST_DOWN(long, userrule_32
->timestamp
);
1271 rule
->fw_un
.fu_divert_port
= userrule_32
->fw_un
.fu_divert_port
;
1272 rule
->fw_prot
= userrule_32
->fw_prot
;
1273 rule
->fw_nports
= userrule_32
->fw_nports
;
1277 ip6_fw_ctl(struct sockopt
*sopt
)
1284 size_t userrulesize
;
1286 if (securelevel
>= 3 &&
1287 (sopt
->sopt_dir
!= SOPT_GET
|| sopt
->sopt_name
!= IPV6_FW_GET
))
1290 if ( proc_is64bit(sopt
->sopt_p
) ){
1292 userrulesize
= sizeof( struct ip6_fw_64
);
1294 userrulesize
= sizeof( struct ip6_fw_32
);
1296 /* We ALWAYS expect the client to pass in a rule structure so that we can
1297 * check the version of the API that they are using. In the case of a
1298 * IPV6_FW_GET operation, the first rule of the output buffer passed to us
1299 * must have the version set. */
1300 if (!sopt
->sopt_val
|| sopt
->sopt_valsize
< userrulesize
) return EINVAL
;
1302 /* save sopt->sopt_valsize */
1303 valsize
= sopt
->sopt_valsize
;
1306 struct ip6_fw_64 userrule_64
;
1308 if ((error
= sooptcopyin(sopt
, &userrule_64
, userrulesize
, userrulesize
)))
1311 cp_from_user_64( &userrule_64
, &rule
);
1314 struct ip6_fw_32 userrule_32
;
1316 if ((error
= sooptcopyin(sopt
, &userrule_32
, userrulesize
, userrulesize
)))
1319 cp_from_user_32( &userrule_32
, &rule
);
1322 if (rule
.version
!= IPV6_FW_CURRENT_API_VERSION
) return EINVAL
;
1323 rule
.version
= 0xFFFFFFFF; /* version is meaningless once rules "make it in the door". */
1325 switch (sopt
->sopt_name
)
1329 struct ip6_fw_chain
*fcp
;
1332 size_t rulesize
= 0;
1337 rulesize
= sizeof(struct ip6_fw_64
);
1339 rulesize
= sizeof(struct ip6_fw_32
);
1341 LIST_FOREACH(fcp
, &ip6_fw_chain
, chain
)
1344 buf
= _MALLOC(size
, M_TEMP
, M_WAITOK
);
1345 if (!buf
) error
= ENOBUFS
;
1348 //struct ip6_fw *bp = buf;
1349 caddr_t bp
= (caddr_t
)buf
;
1351 LIST_FOREACH(fcp
, &ip6_fw_chain
, chain
)
1353 //bcopy(fcp->rule, bp, sizeof *bp);
1355 cp_to_user_64( (struct ip6_fw_64
*)bp
, fcp
->rule
);
1358 cp_to_user_32( (struct ip6_fw_32
*)bp
, fcp
->rule
);
1361 ( (struct ip6_fw
*)bp
)->version
= IPV6_FW_CURRENT_API_VERSION
;
1370 sopt
->sopt_valsize
= valsize
;
1371 error
= sooptcopyout(sopt
, buf
, size
);
1380 while (ip6_fw_chain
.lh_first
&&
1381 ip6_fw_chain
.lh_first
->rule
->fw_number
!= (u_short
)-1)
1383 struct ip6_fw_chain
*fcp
= ip6_fw_chain
.lh_first
;
1384 LIST_REMOVE(ip6_fw_chain
.lh_first
, chain
);
1385 FREE(fcp
->rule
, M_IP6FW
);
1389 ip6fw_kev_post_msg(KEV_IP6FW_FLUSH
);
1393 error
= zero_entry6(&rule
);
1397 if (check_ip6fw_struct(&rule
)) {
1398 error
= add_entry6(&ip6_fw_chain
, &rule
);
1400 ip6fw_kev_post_msg(KEV_IP6FW_ADD
);
1406 if (rule
.fw_number
== (u_short
)-1)
1408 dprintf(("%s can't delete rule 65535\n", err_prefix
));
1412 error
= del_entry6(&ip6_fw_chain
, rule
.fw_number
);
1414 ip6fw_kev_post_msg(KEV_IP6FW_DEL
);
1419 dprintf(("%s invalid option %d\n", err_prefix
, sopt
->sopt_name
));
1429 struct ip6_fw default_rule
;
1431 ip6_fw_chk_ptr
= ip6_fw_chk
;
1432 ip6_fw_ctl_ptr
= ip6_fw_ctl
;
1433 LIST_INIT(&ip6_fw_chain
);
1435 bzero(&default_rule
, sizeof default_rule
);
1436 default_rule
.fw_prot
= IPPROTO_IPV6
;
1437 default_rule
.fw_number
= (u_short
)-1;
1438 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1439 default_rule
.fw_flg
|= IPV6_FW_F_ACCEPT
;
1441 default_rule
.fw_flg
|= IPV6_FW_F_DENY
;
1443 default_rule
.fw_flg
|= IPV6_FW_F_IN
| IPV6_FW_F_OUT
;
1444 if (check_ip6fw_struct(&default_rule
) == NULL
||
1445 add_entry6(&ip6_fw_chain
, &default_rule
))
1446 panic("%s", __FUNCTION__
);
1448 printf("IPv6 packet filtering initialized, ");
1449 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1450 printf("default to accept, ");
1452 #ifndef IPV6FIREWALL_VERBOSE
1453 printf("logging disabled\n");
1455 if (fw6_verbose_limit
== 0)
1456 printf("unlimited logging\n");
1458 printf("logging limited to %d packets/entry\n",