2 * Copyright (c) 2003-2012 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 <sys/param.h>
90 #include <sys/systm.h>
91 #include <sys/malloc.h>
93 #include <sys/queue.h>
94 #include <sys/kernel.h>
95 #include <sys/socket.h>
96 #include <sys/socketvar.h>
97 #include <sys/syslog.h>
100 #include <sys/kern_event.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
= 0;
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
;
143 static void ip6fw_kev_post_msg(u_int32_t
);
146 static int ip6fw_sysctl SYSCTL_HANDLER_ARGS
;
148 SYSCTL_DECL(_net_inet6_ip6
);
149 SYSCTL_NODE(_net_inet6_ip6
, OID_AUTO
, fw
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Firewall");
150 SYSCTL_PROC(_net_inet6_ip6_fw
, OID_AUTO
, enable
,
151 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
152 &ip6_fw_enable
, 0, ip6fw_sysctl
, "I", "Enable ip6fw");
153 SYSCTL_INT(_net_inet6_ip6_fw
, OID_AUTO
, debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &fw6_debug
, 0, "");
154 SYSCTL_INT(_net_inet6_ip6_fw
, OID_AUTO
, verbose
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &fw6_verbose
, 0, "");
155 SYSCTL_INT(_net_inet6_ip6_fw
, OID_AUTO
, verbose_limit
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &fw6_verbose_limit
, 0, "");
158 ip6fw_sysctl SYSCTL_HANDLER_ARGS
160 #pragma unused(arg1, arg2)
163 error
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
164 if (error
|| !req
->newptr
)
167 ip6fw_kev_post_msg(KEV_IP6FW_ENABLE
);
174 #define dprintf(a) do { \
178 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
180 static int add_entry6
__P((struct ip6_fw_head
*chainptr
, struct ip6_fw
*frwl
));
181 static int del_entry6
__P((struct ip6_fw_head
*chainptr
, u_short number
));
182 static int zero_entry6
__P((struct ip6_fw
*frwl
));
183 static struct ip6_fw
*check_ip6fw_struct
__P((struct ip6_fw
*m
));
184 static int ip6opts_match
__P((struct ip6_hdr
**ip6
, struct ip6_fw
*f
,
186 int *off
, int *nxt
, u_short
*offset
));
187 static int port_match6
__P((u_short
*portptr
, int nports
, u_short port
,
189 static int tcp6flg_match
__P((struct tcphdr
*tcp6
, struct ip6_fw
*f
));
190 static int icmp6type_match
__P((struct icmp6_hdr
* icmp
, struct ip6_fw
* f
));
191 static void ip6fw_report
__P((struct ip6_fw
*f
, struct ip6_hdr
*ip6
,
192 struct ifnet
*rif
, struct ifnet
*oif
, int off
, int nxt
));
194 static int ip6_fw_chk
__P((struct ip6_hdr
**pip6
,
195 struct ifnet
*oif
, u_int16_t
*cookie
, struct mbuf
**m
));
196 static int ip6_fw_ctl
__P((struct sockopt
*));
197 static void cp_to_user_64( struct ip6_fw_64
*userrule_64
, struct ip6_fw
*rule
);
198 static void cp_from_user_64( struct ip6_fw_64
*userrule_64
, struct ip6_fw
*rule
);
199 static void cp_to_user_32( struct ip6_fw_32
*userrule_32
, struct ip6_fw
*rule
);
200 static void cp_from_user_32( struct ip6_fw_32
*userrule_32
, struct ip6_fw
*rule
);
202 static char err_prefix
[] = "ip6_fw_ctl:";
205 * Returns 1 if the port is matched by the vector, 0 otherwise
209 port_match6(u_short
*portptr
, int nports
, u_short port
, int range_flag
)
214 if (portptr
[0] <= port
&& port
<= portptr
[1]) {
220 while (nports
-- > 0) {
221 if (*portptr
++ == port
) {
229 tcp6flg_match(struct tcphdr
*tcp6
, struct ip6_fw
*f
)
231 u_char flg_set
, flg_clr
;
234 * If an established connection is required, reject packets that
235 * have only SYN of RST|ACK|SYN set. Otherwise, fall through to
236 * other flag requirements.
238 if ((f
->fw_ipflg
& IPV6_FW_IF_TCPEST
) &&
239 ((tcp6
->th_flags
& (IPV6_FW_TCPF_RST
| IPV6_FW_TCPF_ACK
|
240 IPV6_FW_TCPF_SYN
)) == IPV6_FW_TCPF_SYN
))
243 flg_set
= tcp6
->th_flags
& f
->fw_tcpf
;
244 flg_clr
= tcp6
->th_flags
& f
->fw_tcpnf
;
246 if (flg_set
!= f
->fw_tcpf
)
255 icmp6type_match(struct icmp6_hdr
*icmp6
, struct ip6_fw
*f
)
259 if (!(f
->fw_flg
& IPV6_FW_F_ICMPBIT
))
262 type
= icmp6
->icmp6_type
;
264 /* check for matching type in the bitmap */
265 if (type
< IPV6_FW_ICMPTYPES_DIM
* sizeof(unsigned) * 8 &&
266 (f
->fw_icmp6types
[type
/ (sizeof(unsigned) * 8)] &
267 (1U << (type
% (8 * sizeof(unsigned))))))
270 return(0); /* no match */
274 is_icmp6_query(struct ip6_hdr
*ip6
, int off
)
276 const struct icmp6_hdr
*icmp6
;
279 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
280 icmp6_type
= icmp6
->icmp6_type
;
282 if (icmp6_type
== ICMP6_ECHO_REQUEST
||
283 icmp6_type
== ICMP6_MEMBERSHIP_QUERY
||
284 icmp6_type
== ICMP6_WRUREQUEST
||
285 icmp6_type
== ICMP6_FQDN_QUERY
||
286 icmp6_type
== ICMP6_NI_QUERY
)
293 ip6opts_match(struct ip6_hdr
**pip6
, struct ip6_fw
*f
, struct mbuf
**m
,
294 int *off
, int *nxt
, u_short
*offset
)
297 struct ip6_hdr
*ip6
= *pip6
;
298 struct ip6_ext
*ip6e
;
299 u_char opts
, nopts
, nopts_sve
;
302 nopts
= nopts_sve
= f
->fw_ip6nopt
;
305 *off
= sizeof(struct ip6_hdr
);
306 len
= ntohs(ip6
->ip6_plen
) + sizeof(struct ip6_hdr
);
308 ip6e
= (struct ip6_ext
*)((caddr_t
) ip6
+ *off
);
309 if ((*m
)->m_len
< *off
+ sizeof(*ip6e
))
310 goto opts_check
; /* XXX */
313 case IPPROTO_FRAGMENT
:
314 if ((*m
)->m_len
>= *off
+ sizeof(struct ip6_frag
)) {
315 struct ip6_frag
*ip6f
;
317 ip6f
= (struct ip6_frag
*) ((caddr_t
)ip6
+ *off
);
318 *offset
= ip6f
->ip6f_offlg
& IP6F_OFF_MASK
;
320 opts
&= ~IPV6_FW_IP6OPT_FRAG
;
321 nopts
&= ~IPV6_FW_IP6OPT_FRAG
;
322 *off
+= sizeof(struct ip6_frag
);
325 opts
&= ~IPV6_FW_IP6OPT_AH
;
326 nopts
&= ~IPV6_FW_IP6OPT_AH
;
327 *off
+= (ip6e
->ip6e_len
+ 2) << 2;
331 case IPPROTO_HOPOPTS
:
332 opts
&= ~IPV6_FW_IP6OPT_HOPOPT
;
333 nopts
&= ~IPV6_FW_IP6OPT_HOPOPT
;
335 case IPPROTO_ROUTING
:
336 opts
&= ~IPV6_FW_IP6OPT_ROUTE
;
337 nopts
&= ~IPV6_FW_IP6OPT_ROUTE
;
340 opts
&= ~IPV6_FW_IP6OPT_ESP
;
341 nopts
&= ~IPV6_FW_IP6OPT_ESP
;
344 opts
&= ~IPV6_FW_IP6OPT_NONXT
;
345 nopts
&= ~IPV6_FW_IP6OPT_NONXT
;
348 case IPPROTO_DSTOPTS
:
349 opts
&= ~IPV6_FW_IP6OPT_OPTS
;
350 nopts
&= ~IPV6_FW_IP6OPT_OPTS
;
356 *off
+= (ip6e
->ip6e_len
+ 1) << 3;
359 *nxt
= ip6e
->ip6e_nxt
;
363 if (f
->fw_ip6opt
== f
->fw_ip6nopt
) /* XXX */
366 if (opts
== 0 && nopts
== nopts_sve
)
374 iface_match(struct ifnet
*ifp
, union ip6_fw_if
*ifu
, int byname
)
376 /* Check by name or by IP address */
378 /* Check unit number (-1 is wildcard) */
379 if (ifu
->fu_via_if
.unit
!= -1
380 && ifp
->if_unit
!= ifu
->fu_via_if
.unit
)
383 if (strncmp(ifp
->if_name
, ifu
->fu_via_if
.name
, IP6FW_IFNLEN
))
386 } else if (!IN6_IS_ADDR_UNSPECIFIED(&ifu
->fu_via_ip6
)) { /* Zero == wildcard */
389 ifnet_lock_shared(ifp
);
390 for (ia
= ifp
->if_addrlist
.tqh_first
; ia
;
391 ia
= ia
->ifa_list
.tqe_next
)
394 if (ia
->ifa_addr
->sa_family
!= AF_INET6
) {
398 if (!IN6_ARE_ADDR_EQUAL(&ifu
->fu_via_ip6
,
399 &(((struct sockaddr_in6
*)
400 (ia
->ifa_addr
))->sin6_addr
))) {
405 ifnet_lock_done(ifp
);
408 ifnet_lock_done(ifp
);
415 ip6fw_report(struct ip6_fw
*f
, struct ip6_hdr
*ip6
,
416 struct ifnet
*rif
, struct ifnet
*oif
, int off
, int nxt
)
419 struct tcphdr
*const tcp6
= (struct tcphdr
*) ((caddr_t
) ip6
+ off
);
420 struct udphdr
*const udp
= (struct udphdr
*) ((caddr_t
) ip6
+ off
);
421 struct icmp6_hdr
*const icmp6
= (struct icmp6_hdr
*) ((caddr_t
) ip6
+ off
);
424 char action2
[32], proto
[102], name
[18];
427 count
= f
? f
->fw_pcnt
: ++counter
;
428 if (fw6_verbose_limit
!= 0 && count
> fw6_verbose_limit
)
431 /* Print command name */
432 snprintf(SNPARGS(name
, 0), "ip6fw: %d", f
? f
->fw_number
: -1);
438 switch (f
->fw_flg
& IPV6_FW_F_COMMAND
) {
442 case IPV6_FW_F_REJECT
:
443 if (f
->fw_reject_code
== IPV6_FW_REJECT_RST
)
448 case IPV6_FW_F_ACCEPT
:
451 case IPV6_FW_F_COUNT
:
454 case IPV6_FW_F_DIVERT
:
455 snprintf(SNPARGS(action2
, 0), "Divert %d",
459 snprintf(SNPARGS(action2
, 0), "Tee %d",
462 case IPV6_FW_F_SKIPTO
:
463 snprintf(SNPARGS(action2
, 0), "SkipTo %d",
474 len
= snprintf(SNPARGS(proto
, 0), "TCP [%s]",
475 ip6_sprintf(&ip6
->ip6_src
));
477 len
+= snprintf(SNPARGS(proto
, len
), ":%d ",
478 ntohs(tcp6
->th_sport
));
480 len
+= snprintf(SNPARGS(proto
, len
), " ");
481 len
+= snprintf(SNPARGS(proto
, len
), "[%s]",
482 ip6_sprintf(&ip6
->ip6_dst
));
484 snprintf(SNPARGS(proto
, len
), ":%d",
485 ntohs(tcp6
->th_dport
));
488 len
= snprintf(SNPARGS(proto
, 0), "UDP [%s]",
489 ip6_sprintf(&ip6
->ip6_src
));
491 len
+= snprintf(SNPARGS(proto
, len
), ":%d ",
492 ntohs(udp
->uh_sport
));
494 len
+= snprintf(SNPARGS(proto
, len
), " ");
495 len
+= snprintf(SNPARGS(proto
, len
), "[%s]",
496 ip6_sprintf(&ip6
->ip6_dst
));
498 snprintf(SNPARGS(proto
, len
), ":%d",
499 ntohs(udp
->uh_dport
));
503 len
= snprintf(SNPARGS(proto
, 0), "IPV6-ICMP:%u.%u ",
504 icmp6
->icmp6_type
, icmp6
->icmp6_code
);
506 len
= snprintf(SNPARGS(proto
, 0), "IPV6-ICMP ");
507 len
+= snprintf(SNPARGS(proto
, len
), "[%s]",
508 ip6_sprintf(&ip6
->ip6_src
));
509 snprintf(SNPARGS(proto
, len
), " [%s]",
510 ip6_sprintf(&ip6
->ip6_dst
));
513 len
= snprintf(SNPARGS(proto
, 0), "P:%d [%s]", nxt
,
514 ip6_sprintf(&ip6
->ip6_src
));
515 snprintf(SNPARGS(proto
, len
), " [%s]",
516 ip6_sprintf(&ip6
->ip6_dst
));
521 log(LOG_AUTHPRIV
| LOG_INFO
, "%s %s %s out via %s\n",
522 name
, action
, proto
, if_name(oif
));
524 log(LOG_AUTHPRIV
| LOG_INFO
, "%s %s %s in via %s\n",
525 name
, action
, proto
, if_name(rif
));
527 log(LOG_AUTHPRIV
| LOG_INFO
, "%s %s %s",
528 name
, action
, proto
);
529 if (fw6_verbose_limit
!= 0 && count
== fw6_verbose_limit
)
530 log(LOG_AUTHPRIV
| LOG_INFO
, "ip6fw: limit reached on entry %d\n",
531 f
? f
->fw_number
: -1);
537 * ip Pointer to packet header (struct ip6_hdr *)
538 * hlen Packet header length
539 * oif Outgoing interface, or NULL if packet is incoming
540 * #ifndef IP6FW_DIVERT_RESTART
541 * *cookie Ignore all divert/tee rules to this port (if non-zero)
543 * *cookie Skip up to the first rule past this rule number;
545 * *m The packet; we set to NULL when/if we nuke it.
549 * 0 The packet is to be accepted and routed normally OR
550 * the packet was denied/rejected and has been dropped;
551 * in the latter case, *m is equal to NULL upon return.
552 * port Divert the packet to port.
556 ip6_fw_chk(struct ip6_hdr
**pip6
,
557 struct ifnet
*oif
, u_int16_t
*cookie
, struct mbuf
**m
)
559 struct ip6_fw_chain
*chain
;
560 struct ip6_fw
*rule
= NULL
;
561 struct ip6_hdr
*ip6
= *pip6
;
562 struct ifnet
*const rif
= ((*m
)->m_flags
& M_LOOP
) ? lo_ifp
: (*m
)->m_pkthdr
.rcvif
;
564 int off
= sizeof(struct ip6_hdr
), nxt
= ip6
->ip6_nxt
;
565 u_short src_port
, dst_port
;
566 #ifdef IP6FW_DIVERT_RESTART
567 u_int16_t skipto
= *cookie
;
569 u_int16_t ignport
= ntohs(*cookie
);
571 struct timeval timenow
;
572 struct tcp_respond_args tra
;
574 getmicrotime(&timenow
);
578 * Go down the chain, looking for enlightment
579 * #ifdef IP6FW_DIVERT_RESTART
580 * If we've been asked to start at a given rule immediatly, do so.
583 chain
= LIST_FIRST(&ip6_fw_chain
);
584 #ifdef IP6FW_DIVERT_RESTART
588 while (chain
&& (chain
->rule
->fw_number
<= skipto
)) {
589 chain
= LIST_NEXT(chain
, chain
);
591 if (! chain
) goto dropit
;
593 #endif /* IP6FW_DIVERT_RESTART */
594 for (; chain
; chain
= LIST_NEXT(chain
, chain
)) {
595 struct ip6_fw
*const f
= chain
->rule
;
598 /* Check direction outbound */
599 if (!(f
->fw_flg
& IPV6_FW_F_OUT
))
602 /* Check direction inbound */
603 if (!(f
->fw_flg
& IPV6_FW_F_IN
))
607 #define IN6_ARE_ADDR_MASKEQUAL(x,y,z) (\
608 (((x)->s6_addr32[0] & (y)->s6_addr32[0]) == (z)->s6_addr32[0]) && \
609 (((x)->s6_addr32[1] & (y)->s6_addr32[1]) == (z)->s6_addr32[1]) && \
610 (((x)->s6_addr32[2] & (y)->s6_addr32[2]) == (z)->s6_addr32[2]) && \
611 (((x)->s6_addr32[3] & (y)->s6_addr32[3]) == (z)->s6_addr32[3]))
613 /* If src-addr doesn't match, not this rule. */
614 if (((f
->fw_flg
& IPV6_FW_F_INVSRC
) != 0) ^
615 (!IN6_ARE_ADDR_MASKEQUAL(&ip6
->ip6_src
,&f
->fw_smsk
,&f
->fw_src
)))
618 /* If dest-addr doesn't match, not this rule. */
619 if (((f
->fw_flg
& IPV6_FW_F_INVDST
) != 0) ^
620 (!IN6_ARE_ADDR_MASKEQUAL(&ip6
->ip6_dst
,&f
->fw_dmsk
,&f
->fw_dst
)))
623 #undef IN6_ARE_ADDR_MASKEQUAL
624 /* Interface check */
625 if ((f
->fw_flg
& IF6_FW_F_VIAHACK
) == IF6_FW_F_VIAHACK
) {
626 struct ifnet
*const iface
= oif
? oif
: rif
;
628 /* Backwards compatibility hack for "via" */
629 if (!iface
|| !iface_match(iface
,
630 &f
->fw_in_if
, f
->fw_flg
& IPV6_FW_F_OIFNAME
))
633 /* Check receive interface */
634 if ((f
->fw_flg
& IPV6_FW_F_IIFACE
)
635 && (!rif
|| !iface_match(rif
,
636 &f
->fw_in_if
, f
->fw_flg
& IPV6_FW_F_IIFNAME
)))
638 /* Check outgoing interface */
639 if ((f
->fw_flg
& IPV6_FW_F_OIFACE
)
640 && (!oif
|| !iface_match(oif
,
641 &f
->fw_out_if
, f
->fw_flg
& IPV6_FW_F_OIFNAME
)))
645 /* Check IP options */
646 if (!ip6opts_match(&ip6
, f
, m
, &off
, &nxt
, &offset
))
650 if ((f
->fw_flg
& IPV6_FW_F_FRAG
) && !offset
)
653 /* Check protocol; if wildcard, match */
654 if (f
->fw_prot
== IPPROTO_IPV6
)
657 /* If different, don't match */
658 if (nxt
!= f
->fw_prot
)
661 #define PULLUP_TO(len) do { \
662 if ((*m)->m_len < (len) \
663 && (*m = m_pullup(*m, (len))) == 0) { \
666 *pip6 = ip6 = mtod(*m, struct ip6_hdr *); \
669 /* Protocol specific checks */
675 if (offset
== 1) { /* cf. RFC 1858 */
676 PULLUP_TO(off
+ 4); /* XXX ? */
681 * TCP flags and ports aren't available in this
682 * packet -- if this rule specified either one,
683 * we consider the rule a non-match.
685 if (f
->fw_nports
!= 0 ||
686 f
->fw_tcpf
!= f
->fw_tcpnf
)
692 tcp6
= (struct tcphdr
*) ((caddr_t
)ip6
+ off
);
693 if (((f
->fw_tcpf
!= f
->fw_tcpnf
) ||
694 (f
->fw_ipflg
& IPV6_FW_IF_TCPEST
)) &&
695 !tcp6flg_match(tcp6
, f
))
697 src_port
= ntohs(tcp6
->th_sport
);
698 dst_port
= ntohs(tcp6
->th_dport
);
708 * Port specification is unavailable -- if this
709 * rule specifies a port, we consider the rule
712 if (f
->fw_nports
!= 0)
718 udp
= (struct udphdr
*) ((caddr_t
)ip6
+ off
);
719 src_port
= ntohs(udp
->uh_sport
);
720 dst_port
= ntohs(udp
->uh_dport
);
722 if (!port_match6(&f
->fw_pts
[0],
723 IPV6_FW_GETNSRCP(f
), src_port
,
724 f
->fw_flg
& IPV6_FW_F_SRNG
))
726 if (!port_match6(&f
->fw_pts
[IPV6_FW_GETNSRCP(f
)],
727 IPV6_FW_GETNDSTP(f
), dst_port
,
728 f
->fw_flg
& IPV6_FW_F_DRNG
))
735 struct icmp6_hdr
*icmp
;
737 if (offset
!= 0) /* Type isn't valid */
740 icmp
= (struct icmp6_hdr
*) ((caddr_t
)ip6
+ off
);
741 if (!icmp6type_match(icmp
, f
))
749 ip6fw_report(NULL
, ip6
, rif
, oif
, off
, nxt
);
754 #ifndef IP6FW_DIVERT_RESTART
755 /* Ignore divert/tee rule if socket port is "ignport" */
756 switch (f
->fw_flg
& IPV6_FW_F_COMMAND
) {
757 case IPV6_FW_F_DIVERT
:
759 if (f
->fw_divert_port
== ignport
)
760 continue; /* ignore this rule */
764 #endif /* IP6FW_DIVERT_RESTART */
765 /* Update statistics */
767 f
->fw_bcnt
+= ntohs(ip6
->ip6_plen
);
768 f
->timestamp
= timenow
.tv_sec
;
770 /* Log to console if desired */
771 if ((f
->fw_flg
& IPV6_FW_F_PRN
) && fw6_verbose
)
772 ip6fw_report(f
, ip6
, rif
, oif
, off
, nxt
);
774 /* Take appropriate action */
775 switch (f
->fw_flg
& IPV6_FW_F_COMMAND
) {
776 case IPV6_FW_F_ACCEPT
:
778 case IPV6_FW_F_COUNT
:
780 case IPV6_FW_F_DIVERT
:
781 #ifdef IP6FW_DIVERT_RESTART
782 *cookie
= f
->fw_number
;
784 *cookie
= htons(f
->fw_divert_port
);
785 #endif /* IP6FW_DIVERT_RESTART */
786 return(f
->fw_divert_port
);
789 * XXX someday tee packet here, but beware that you
790 * can't use m_copym() or m_copypacket() because
791 * the divert input routine modifies the mbuf
792 * (and these routines only increment reference
793 * counts in the case of mbuf clusters), so need
794 * to write custom routine.
797 case IPV6_FW_F_SKIPTO
:
799 while (chain
->chain
.le_next
800 && chain
->chain
.le_next
->rule
->fw_number
803 while (chain
->chain
.le_next
->rule
->fw_number
806 chain
= chain
->chain
.le_next
;
810 /* Deny/reject this packet using this rule */
816 /* Rule 65535 should always be there and should always match */
818 panic("ip6_fw: chain");
822 * At this point, we're going to drop the packet.
823 * Send a reject notice if all of the following are true:
825 * - The packet matched a reject rule
826 * - The packet is not an ICMP packet, or is an ICMP query packet
827 * - The packet is not a multicast or broadcast packet
829 if ((rule
->fw_flg
& IPV6_FW_F_COMMAND
) == IPV6_FW_F_REJECT
830 && (nxt
!= IPPROTO_ICMPV6
|| is_icmp6_query(ip6
, off
))
831 && !((*m
)->m_flags
& (M_BCAST
|M_MCAST
))
832 && !IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
833 switch (rule
->fw_reject_code
) {
834 case IPV6_FW_REJECT_RST
:
836 struct tcphdr
*const tcp
=
837 (struct tcphdr
*) ((caddr_t
)ip6
+ off
);
845 if (offset
!= 0 || (tcp
->th_flags
& TH_RST
))
850 ti
.th
.th_seq
= ntohl(ti
.th
.th_seq
);
851 ti
.th
.th_ack
= ntohl(ti
.th
.th_ack
);
852 ti
.ip6
.ip6_nxt
= IPPROTO_TCP
;
853 if (ti
.th
.th_flags
& TH_ACK
) {
859 if (((*m
)->m_flags
& M_PKTHDR
) != 0) {
860 ack
+= (*m
)->m_pkthdr
.len
- off
861 - (ti
.th
.th_off
<< 2);
862 } else if (ip6
->ip6_plen
) {
863 ack
+= ntohs(ip6
->ip6_plen
) + sizeof(*ip6
)
864 - off
- (ti
.th
.th_off
<< 2);
871 flags
= TH_RST
|TH_ACK
;
873 bcopy(&ti
, ip6
, sizeof(ti
));
874 bzero(&tra
, sizeof(tra
));
875 tra
.ifscope
= IFSCOPE_NONE
;
876 tra
.awdl_unrestricted
= 1;
877 tcp_respond(NULL
, ip6
, (struct tcphdr
*)(ip6
+ 1),
878 *m
, ack
, seq
, flags
, &tra
);
882 default: /* Send an ICMP unreachable using code */
884 (*m
)->m_pkthdr
.rcvif
= oif
;
885 icmp6_error(*m
, ICMP6_DST_UNREACH
,
886 rule
->fw_reject_code
, 0);
894 * Finally, drop the packet.
904 add_entry6(struct ip6_fw_head
*chainptr
, struct ip6_fw
*frwl
)
906 struct ip6_fw
*ftmp
= 0;
907 struct ip6_fw_chain
*fwc
= 0, *fcp
, *fcpl
= 0;
910 fwc
= _MALLOC(sizeof *fwc
, M_IP6FW
, M_WAITOK
);
911 ftmp
= _MALLOC(sizeof *ftmp
, M_IP6FW
, M_WAITOK
);
913 dprintf(("%s malloc said no\n", err_prefix
));
914 if (fwc
) FREE(fwc
, M_IP6FW
);
915 if (ftmp
) FREE(ftmp
, M_IP6FW
);
919 bcopy(frwl
, ftmp
, sizeof(struct ip6_fw
));
920 ftmp
->fw_in_if
.fu_via_if
.name
[IP6FW_IFNLEN
- 1] = '\0';
925 if (!chainptr
->lh_first
) {
926 LIST_INSERT_HEAD(chainptr
, fwc
, chain
);
928 } else if (ftmp
->fw_number
== (u_short
)-1) {
929 if (fwc
) FREE(fwc
, M_IP6FW
);
930 if (ftmp
) FREE(ftmp
, M_IP6FW
);
931 dprintf(("%s bad rule number\n", err_prefix
));
935 /* If entry number is 0, find highest numbered rule and add 100 */
936 if (ftmp
->fw_number
== 0) {
937 for (fcp
= chainptr
->lh_first
; fcp
; fcp
= fcp
->chain
.le_next
) {
938 if (fcp
->rule
->fw_number
!= (u_short
)-1)
939 nbr
= fcp
->rule
->fw_number
;
943 if (nbr
< (u_short
)-1 - 100)
945 ftmp
->fw_number
= nbr
;
948 /* Got a valid number; now insert it, keeping the list ordered */
949 for (fcp
= chainptr
->lh_first
; fcp
; fcp
= fcp
->chain
.le_next
) {
950 if (fcp
->rule
->fw_number
> ftmp
->fw_number
) {
952 LIST_INSERT_AFTER(fcpl
, fwc
, chain
);
954 LIST_INSERT_HEAD(chainptr
, fwc
, chain
);
962 bcopy(ftmp
, frwl
, sizeof(struct ip6_fw
));
967 del_entry6(struct ip6_fw_head
*chainptr
, u_short number
)
969 struct ip6_fw_chain
*fcp
;
971 fcp
= chainptr
->lh_first
;
972 if (number
!= (u_short
)-1) {
973 for (; fcp
; fcp
= fcp
->chain
.le_next
) {
974 if (fcp
->rule
->fw_number
== number
) {
975 LIST_REMOVE(fcp
, chain
);
976 FREE(fcp
->rule
, M_IP6FW
);
987 zero_entry6(struct ip6_fw
*frwl
)
989 struct ip6_fw_chain
*fcp
;
992 * It's possible to insert multiple chain entries with the
993 * same number, so we don't stop after finding the first
994 * match if zeroing a specific entry.
996 for (fcp
= ip6_fw_chain
.lh_first
; fcp
; fcp
= fcp
->chain
.le_next
)
997 if (!frwl
|| frwl
->fw_number
== 0 || frwl
->fw_number
== fcp
->rule
->fw_number
) {
998 fcp
->rule
->fw_bcnt
= fcp
->rule
->fw_pcnt
= 0;
999 fcp
->rule
->timestamp
= 0;
1004 log(LOG_AUTHPRIV
| LOG_NOTICE
,
1005 "ip6fw: Entry %d cleared.\n", frwl
->fw_number
);
1007 log(LOG_AUTHPRIV
| LOG_NOTICE
,
1008 "ip6fw: Accounting cleared.\n");
1014 static struct ip6_fw
*
1015 check_ip6fw_struct(struct ip6_fw
*frwl
)
1017 /* Check for invalid flag bits */
1018 if ((frwl
->fw_flg
& ~IPV6_FW_F_MASK
) != 0) {
1019 dprintf(("%s undefined flag bits set (flags=%x)\n",
1020 err_prefix
, frwl
->fw_flg
));
1023 /* Must apply to incoming or outgoing (or both) */
1024 if (!(frwl
->fw_flg
& (IPV6_FW_F_IN
| IPV6_FW_F_OUT
))) {
1025 dprintf(("%s neither in nor out\n", err_prefix
));
1028 /* Empty interface name is no good */
1029 if (((frwl
->fw_flg
& IPV6_FW_F_IIFNAME
)
1030 && !*frwl
->fw_in_if
.fu_via_if
.name
)
1031 || ((frwl
->fw_flg
& IPV6_FW_F_OIFNAME
)
1032 && !*frwl
->fw_out_if
.fu_via_if
.name
)) {
1033 dprintf(("%s empty interface name\n", err_prefix
));
1036 /* Sanity check interface matching */
1037 if ((frwl
->fw_flg
& IF6_FW_F_VIAHACK
) == IF6_FW_F_VIAHACK
) {
1038 ; /* allow "via" backwards compatibility */
1039 } else if ((frwl
->fw_flg
& IPV6_FW_F_IN
)
1040 && (frwl
->fw_flg
& IPV6_FW_F_OIFACE
)) {
1041 dprintf(("%s outgoing interface check on incoming\n",
1045 /* Sanity check port ranges */
1046 if ((frwl
->fw_flg
& IPV6_FW_F_SRNG
) && IPV6_FW_GETNSRCP(frwl
) < 2) {
1047 dprintf(("%s src range set but n_src_p=%d\n",
1048 err_prefix
, IPV6_FW_GETNSRCP(frwl
)));
1051 if ((frwl
->fw_flg
& IPV6_FW_F_DRNG
) && IPV6_FW_GETNDSTP(frwl
) < 2) {
1052 dprintf(("%s dst range set but n_dst_p=%d\n",
1053 err_prefix
, IPV6_FW_GETNDSTP(frwl
)));
1056 if (IPV6_FW_GETNSRCP(frwl
) + IPV6_FW_GETNDSTP(frwl
) > IPV6_FW_MAX_PORTS
) {
1057 dprintf(("%s too many ports (%d+%d)\n",
1058 err_prefix
, IPV6_FW_GETNSRCP(frwl
), IPV6_FW_GETNDSTP(frwl
)));
1062 * Protocols other than TCP/UDP don't use port range
1064 if ((frwl
->fw_prot
!= IPPROTO_TCP
) &&
1065 (frwl
->fw_prot
!= IPPROTO_UDP
) &&
1066 (IPV6_FW_GETNSRCP(frwl
) || IPV6_FW_GETNDSTP(frwl
))) {
1067 dprintf(("%s port(s) specified for non TCP/UDP rule\n",
1073 * Rather than modify the entry to make such entries work,
1074 * we reject this rule and require user level utilities
1075 * to enforce whatever policy they deem appropriate.
1077 if ((frwl
->fw_src
.s6_addr32
[0] & (~frwl
->fw_smsk
.s6_addr32
[0])) ||
1078 (frwl
->fw_src
.s6_addr32
[1] & (~frwl
->fw_smsk
.s6_addr32
[1])) ||
1079 (frwl
->fw_src
.s6_addr32
[2] & (~frwl
->fw_smsk
.s6_addr32
[2])) ||
1080 (frwl
->fw_src
.s6_addr32
[3] & (~frwl
->fw_smsk
.s6_addr32
[3])) ||
1081 (frwl
->fw_dst
.s6_addr32
[0] & (~frwl
->fw_dmsk
.s6_addr32
[0])) ||
1082 (frwl
->fw_dst
.s6_addr32
[1] & (~frwl
->fw_dmsk
.s6_addr32
[1])) ||
1083 (frwl
->fw_dst
.s6_addr32
[2] & (~frwl
->fw_dmsk
.s6_addr32
[2])) ||
1084 (frwl
->fw_dst
.s6_addr32
[3] & (~frwl
->fw_dmsk
.s6_addr32
[3]))) {
1085 dprintf(("%s rule never matches\n", err_prefix
));
1089 if ((frwl
->fw_flg
& IPV6_FW_F_FRAG
) &&
1090 (frwl
->fw_prot
== IPPROTO_UDP
|| frwl
->fw_prot
== IPPROTO_TCP
)) {
1091 if (frwl
->fw_nports
) {
1092 dprintf(("%s cannot mix 'frag' and ports\n", err_prefix
));
1095 if (frwl
->fw_prot
== IPPROTO_TCP
&&
1096 frwl
->fw_tcpf
!= frwl
->fw_tcpnf
) {
1097 dprintf(("%s cannot mix 'frag' with TCP flags\n", err_prefix
));
1102 /* Check command specific stuff */
1103 switch (frwl
->fw_flg
& IPV6_FW_F_COMMAND
)
1105 case IPV6_FW_F_REJECT
:
1106 if (frwl
->fw_reject_code
>= 0x100
1107 && !(frwl
->fw_prot
== IPPROTO_TCP
1108 && frwl
->fw_reject_code
== IPV6_FW_REJECT_RST
)) {
1109 dprintf(("%s unknown reject code\n", err_prefix
));
1113 case IPV6_FW_F_DIVERT
: /* Diverting to port zero is invalid */
1115 if (frwl
->fw_divert_port
== 0) {
1116 dprintf(("%s can't divert to port 0\n", err_prefix
));
1120 case IPV6_FW_F_DENY
:
1121 case IPV6_FW_F_ACCEPT
:
1122 case IPV6_FW_F_COUNT
:
1123 case IPV6_FW_F_SKIPTO
:
1126 dprintf(("%s invalid command\n", err_prefix
));
1134 ip6fw_kev_post_msg(u_int32_t event_code
)
1136 struct kev_msg ev_msg
;
1138 bzero(&ev_msg
, sizeof(struct kev_msg
));
1140 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1141 ev_msg
.kev_class
= KEV_FIREWALL_CLASS
;
1142 ev_msg
.kev_subclass
= KEV_IP6FW_SUBCLASS
;
1143 ev_msg
.event_code
= event_code
;
1145 kev_post_msg(&ev_msg
);
1151 cp_to_user_64( struct ip6_fw_64
*userrule_64
, struct ip6_fw
*rule
)
1153 userrule_64
->version
= rule
->version
;
1154 userrule_64
->context
= CAST_USER_ADDR_T(rule
->context
);
1155 userrule_64
->fw_pcnt
= rule
->fw_pcnt
;
1156 userrule_64
->fw_bcnt
= rule
->fw_bcnt
;
1157 userrule_64
->fw_src
= rule
->fw_src
;
1158 userrule_64
->fw_dst
= rule
->fw_dst
;
1159 userrule_64
->fw_smsk
= rule
->fw_smsk
;
1160 userrule_64
->fw_dmsk
= rule
->fw_dmsk
;
1161 userrule_64
->fw_number
= rule
->fw_number
;
1162 userrule_64
->fw_flg
= rule
->fw_flg
;
1163 userrule_64
->fw_ipflg
= rule
->fw_ipflg
;
1164 bcopy( rule
->fw_pts
, userrule_64
->fw_pts
, IPV6_FW_MAX_PORTS
);
1165 userrule_64
->fw_ip6opt
= rule
->fw_ip6opt
;
1166 userrule_64
->fw_ip6nopt
= rule
->fw_ip6nopt
;
1167 userrule_64
->fw_tcpf
= rule
->fw_tcpf
;
1168 userrule_64
->fw_tcpnf
= rule
->fw_tcpnf
;
1169 bcopy( rule
->fw_icmp6types
, userrule_64
->fw_icmp6types
, sizeof(userrule_64
->fw_icmp6types
));
1170 userrule_64
->fw_in_if
= rule
->fw_in_if
;
1171 userrule_64
->fw_out_if
= rule
->fw_out_if
;
1172 userrule_64
->timestamp
= rule
->timestamp
;
1173 userrule_64
->fw_un
.fu_divert_port
= rule
->fw_un
.fu_divert_port
;
1174 userrule_64
->fw_prot
= rule
->fw_prot
;
1175 userrule_64
->fw_nports
= rule
->fw_nports
;
1180 cp_from_user_64( struct ip6_fw_64
*userrule_64
, struct ip6_fw
*rule
)
1182 rule
->version
= userrule_64
->version
;
1183 rule
->context
= CAST_DOWN(void *, userrule_64
->context
);
1184 rule
->fw_pcnt
= userrule_64
->fw_pcnt
;
1185 rule
->fw_bcnt
= userrule_64
->fw_bcnt
;
1186 rule
->fw_src
= userrule_64
->fw_src
;
1187 rule
->fw_dst
= userrule_64
->fw_dst
;
1188 rule
->fw_smsk
= userrule_64
->fw_smsk
;
1189 rule
->fw_dmsk
= userrule_64
->fw_dmsk
;
1190 rule
->fw_number
= userrule_64
->fw_number
;
1191 rule
->fw_flg
= userrule_64
->fw_flg
;
1192 rule
->fw_ipflg
= userrule_64
->fw_ipflg
;
1193 bcopy( userrule_64
->fw_pts
, rule
->fw_pts
, IPV6_FW_MAX_PORTS
);
1194 rule
->fw_ip6opt
= userrule_64
->fw_ip6opt
;
1195 rule
->fw_ip6nopt
= userrule_64
->fw_ip6nopt
;
1196 rule
->fw_tcpf
= userrule_64
->fw_tcpf
;
1197 rule
->fw_tcpnf
= userrule_64
->fw_tcpnf
;
1198 bcopy( userrule_64
->fw_icmp6types
, rule
->fw_icmp6types
, sizeof(userrule_64
->fw_icmp6types
));
1199 rule
->fw_in_if
= userrule_64
->fw_in_if
;
1200 rule
->fw_out_if
= userrule_64
->fw_out_if
;
1201 rule
->timestamp
= CAST_DOWN( long, userrule_64
->timestamp
);
1202 rule
->fw_un
.fu_divert_port
= userrule_64
->fw_un
.fu_divert_port
;
1203 rule
->fw_prot
= userrule_64
->fw_prot
;
1204 rule
->fw_nports
= userrule_64
->fw_nports
;
1209 cp_to_user_32( struct ip6_fw_32
*userrule_32
, struct ip6_fw
*rule
)
1211 userrule_32
->version
= rule
->version
;
1212 userrule_32
->context
= CAST_DOWN_EXPLICIT( user32_addr_t
, rule
->context
);
1213 userrule_32
->fw_pcnt
= rule
->fw_pcnt
;
1214 userrule_32
->fw_bcnt
= rule
->fw_bcnt
;
1215 userrule_32
->fw_src
= rule
->fw_src
;
1216 userrule_32
->fw_dst
= rule
->fw_dst
;
1217 userrule_32
->fw_smsk
= rule
->fw_smsk
;
1218 userrule_32
->fw_dmsk
= rule
->fw_dmsk
;
1219 userrule_32
->fw_number
= rule
->fw_number
;
1220 userrule_32
->fw_flg
= rule
->fw_flg
;
1221 userrule_32
->fw_ipflg
= rule
->fw_ipflg
;
1222 bcopy( rule
->fw_pts
, userrule_32
->fw_pts
, IPV6_FW_MAX_PORTS
);
1223 userrule_32
->fw_ip6opt
= rule
->fw_ip6opt
;
1224 userrule_32
->fw_ip6nopt
= rule
->fw_ip6nopt
;
1225 userrule_32
->fw_tcpf
= rule
->fw_tcpf
;
1226 userrule_32
->fw_tcpnf
= rule
->fw_tcpnf
;
1227 bcopy( rule
->fw_icmp6types
, userrule_32
->fw_icmp6types
, sizeof(rule
->fw_icmp6types
));
1228 userrule_32
->fw_in_if
= rule
->fw_in_if
;
1229 userrule_32
->fw_out_if
= rule
->fw_out_if
;
1230 userrule_32
->timestamp
= rule
->timestamp
;
1231 userrule_32
->fw_un
.fu_divert_port
= rule
->fw_un
.fu_divert_port
;
1232 userrule_32
->fw_prot
= rule
->fw_prot
;
1233 userrule_32
->fw_nports
= rule
->fw_nports
;
1238 cp_from_user_32( struct ip6_fw_32
*userrule_32
, struct ip6_fw
*rule
)
1240 rule
->version
= userrule_32
->version
;
1241 rule
->context
= CAST_DOWN(void *, userrule_32
->context
);
1242 rule
->fw_pcnt
= userrule_32
->fw_pcnt
;
1243 rule
->fw_bcnt
= userrule_32
->fw_bcnt
;
1244 rule
->fw_src
= userrule_32
->fw_src
;
1245 rule
->fw_dst
= userrule_32
->fw_dst
;
1246 rule
->fw_smsk
= userrule_32
->fw_smsk
;
1247 rule
->fw_dmsk
= userrule_32
->fw_dmsk
;
1248 rule
->fw_number
= userrule_32
->fw_number
;
1249 rule
->fw_flg
= userrule_32
->fw_flg
;
1250 rule
->fw_ipflg
= userrule_32
->fw_ipflg
;
1251 bcopy( userrule_32
->fw_pts
, rule
->fw_pts
, IPV6_FW_MAX_PORTS
);
1252 rule
->fw_ip6opt
= userrule_32
->fw_ip6opt
;
1253 rule
->fw_ip6nopt
= userrule_32
->fw_ip6nopt
;
1254 rule
->fw_tcpf
= userrule_32
->fw_tcpf
;
1255 rule
->fw_tcpnf
= userrule_32
->fw_tcpnf
;
1256 bcopy( userrule_32
->fw_icmp6types
, rule
->fw_icmp6types
, sizeof(userrule_32
->fw_icmp6types
));
1257 rule
->fw_in_if
= userrule_32
->fw_in_if
;
1258 rule
->fw_out_if
= userrule_32
->fw_out_if
;
1259 rule
->timestamp
= CAST_DOWN(long, userrule_32
->timestamp
);
1260 rule
->fw_un
.fu_divert_port
= userrule_32
->fw_un
.fu_divert_port
;
1261 rule
->fw_prot
= userrule_32
->fw_prot
;
1262 rule
->fw_nports
= userrule_32
->fw_nports
;
1266 ip6_fw_ctl(struct sockopt
*sopt
)
1272 size_t userrulesize
;
1274 if (securelevel
>= 3 &&
1275 (sopt
->sopt_dir
!= SOPT_GET
|| sopt
->sopt_name
!= IPV6_FW_GET
))
1278 if ( proc_is64bit(sopt
->sopt_p
) ){
1280 userrulesize
= sizeof( struct ip6_fw_64
);
1282 userrulesize
= sizeof( struct ip6_fw_32
);
1284 /* We ALWAYS expect the client to pass in a rule structure so that we can
1285 * check the version of the API that they are using. In the case of a
1286 * IPV6_FW_GET operation, the first rule of the output buffer passed to us
1287 * must have the version set. */
1288 if (!sopt
->sopt_val
|| sopt
->sopt_valsize
< userrulesize
) return EINVAL
;
1290 /* save sopt->sopt_valsize */
1291 valsize
= sopt
->sopt_valsize
;
1294 struct ip6_fw_64 userrule_64
;
1296 if ((error
= sooptcopyin(sopt
, &userrule_64
, userrulesize
, userrulesize
)))
1299 cp_from_user_64( &userrule_64
, &rule
);
1302 struct ip6_fw_32 userrule_32
;
1304 if ((error
= sooptcopyin(sopt
, &userrule_32
, userrulesize
, userrulesize
)))
1307 cp_from_user_32( &userrule_32
, &rule
);
1310 if (rule
.version
!= IPV6_FW_CURRENT_API_VERSION
) return EINVAL
;
1311 rule
.version
= 0xFFFFFFFF; /* version is meaningless once rules "make it in the door". */
1313 switch (sopt
->sopt_name
)
1317 struct ip6_fw_chain
*fcp
;
1320 size_t rulesize
= 0;
1323 rulesize
= sizeof(struct ip6_fw_64
);
1325 rulesize
= sizeof(struct ip6_fw_32
);
1327 LIST_FOREACH(fcp
, &ip6_fw_chain
, chain
)
1330 buf
= _MALLOC(size
, M_TEMP
, M_WAITOK
);
1331 if (!buf
) error
= ENOBUFS
;
1334 //struct ip6_fw *bp = buf;
1335 caddr_t bp
= (caddr_t
)buf
;
1337 LIST_FOREACH(fcp
, &ip6_fw_chain
, chain
)
1339 //bcopy(fcp->rule, bp, sizeof *bp);
1341 cp_to_user_64( (struct ip6_fw_64
*)bp
, fcp
->rule
);
1344 cp_to_user_32( (struct ip6_fw_32
*)bp
, fcp
->rule
);
1347 ( (struct ip6_fw
*)bp
)->version
= IPV6_FW_CURRENT_API_VERSION
;
1355 sopt
->sopt_valsize
= valsize
;
1356 error
= sooptcopyout(sopt
, buf
, size
);
1364 while (ip6_fw_chain
.lh_first
&&
1365 ip6_fw_chain
.lh_first
->rule
->fw_number
!= (u_short
)-1)
1367 struct ip6_fw_chain
*fcp
= ip6_fw_chain
.lh_first
;
1368 LIST_REMOVE(ip6_fw_chain
.lh_first
, chain
);
1369 FREE(fcp
->rule
, M_IP6FW
);
1372 ip6fw_kev_post_msg(KEV_IP6FW_FLUSH
);
1376 error
= zero_entry6(&rule
);
1380 if (check_ip6fw_struct(&rule
)) {
1381 error
= add_entry6(&ip6_fw_chain
, &rule
);
1383 ip6fw_kev_post_msg(KEV_IP6FW_ADD
);
1388 struct ip6_fw_64 userrule_64
;
1389 cp_to_user_64( &userrule_64
, &rule
);
1390 error
= sooptcopyout(sopt
, &userrule_64
, userrulesize
);
1393 struct ip6_fw_32 userrule_32
;
1394 cp_to_user_32( &userrule_32
, &rule
);
1395 error
= sooptcopyout(sopt
, &userrule_32
, userrulesize
);
1400 if (rule
.fw_number
== (u_short
)-1)
1402 dprintf(("%s can't delete rule 65535\n", err_prefix
));
1406 error
= del_entry6(&ip6_fw_chain
, rule
.fw_number
);
1408 ip6fw_kev_post_msg(KEV_IP6FW_DEL
);
1413 dprintf(("%s invalid option %d\n", err_prefix
, sopt
->sopt_name
));
1423 struct ip6_fw default_rule
;
1425 ip6_fw_chk_ptr
= ip6_fw_chk
;
1426 ip6_fw_ctl_ptr
= ip6_fw_ctl
;
1427 LIST_INIT(&ip6_fw_chain
);
1429 bzero(&default_rule
, sizeof default_rule
);
1430 default_rule
.fw_prot
= IPPROTO_IPV6
;
1431 default_rule
.fw_number
= (u_short
)-1;
1432 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1433 default_rule
.fw_flg
|= IPV6_FW_F_ACCEPT
;
1435 default_rule
.fw_flg
|= IPV6_FW_F_DENY
;
1437 default_rule
.fw_flg
|= IPV6_FW_F_IN
| IPV6_FW_F_OUT
;
1438 if (check_ip6fw_struct(&default_rule
) == NULL
||
1439 add_entry6(&ip6_fw_chain
, &default_rule
))
1440 panic("%s", __FUNCTION__
);
1442 printf("IPv6 packet filtering initialized, ");
1443 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1444 printf("default to accept, ");
1446 #ifndef IPV6FIREWALL_VERBOSE
1447 printf("logging disabled\n");
1449 if (fw6_verbose_limit
== 0)
1450 printf("unlimited logging\n");
1452 printf("logging limited to %d packets/entry\n",