]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/ip6_fw.c
xnu-1228.15.4.tar.gz
[apple/xnu.git] / bsd / netinet6 / ip6_fw.c
1 /*
2 * Copyright (c) 2003-2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
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 $ */
31
32 /*
33 * Copyright (C) 1998, 1999, 2000 and 2001 WIDE Project.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 */
60
61 /*
62 * Copyright (c) 1993 Daniel Boulet
63 * Copyright (c) 1994 Ugen J.S.Antsilevich
64 * Copyright (c) 1996 Alex Nash
65 *
66 * Redistribution and use in source forms, with and without modification,
67 * are permitted provided that this entire comment appears intact.
68 *
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.
72 *
73 * This software is provided ``AS IS'' without any warranties of any kind.
74 */
75
76 /*
77 * Implement IPv6 packet firewall
78 */
79
80
81 #ifdef IP6DIVERT
82 #error "NOT SUPPORTED IPV6 DIVERT"
83 #endif
84 #ifdef IP6FW_DIVERT_RESTART
85 #error "NOT SUPPORTED IPV6 DIVERT"
86 #endif
87
88 #include <string.h>
89 #include <machine/spl.h>
90
91 #include <sys/param.h>
92 #include <sys/systm.h>
93 #include <sys/malloc.h>
94 #include <sys/mbuf.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>
103
104 #include <net/if.h>
105 #include <net/route.h>
106 #include <netinet/in_systm.h>
107 #include <netinet/in.h>
108 #include <netinet/ip.h>
109
110 #include <netinet/ip6.h>
111 #include <netinet6/ip6_var.h>
112 #include <netinet6/in6_var.h>
113 #include <netinet/icmp6.h>
114
115 #include <netinet/in_pcb.h>
116
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>
124
125 #include <sys/sysctl.h>
126
127 #include <net/net_osdep.h>
128
129 MALLOC_DEFINE(M_IP6FW, "Ip6Fw/Ip6Acct", "Ip6Fw/Ip6Acct chain's");
130
131 static int fw6_debug = 0;
132 #ifdef IPV6FIREWALL_VERBOSE
133 static int fw6_verbose = 1;
134 #else
135 static int fw6_verbose = 0;
136 #endif
137 #ifdef IPV6FIREWALL_VERBOSE_LIMIT
138 static int fw6_verbose_limit = IPV6FIREWALL_VERBOSE_LIMIT;
139 #else
140 static int fw6_verbose_limit = 0;
141 #endif
142
143 LIST_HEAD (ip6_fw_head, ip6_fw_chain) ip6_fw_chain;
144
145 static void ip6fw_kev_post_msg(u_int32_t );
146
147 #ifdef SYSCTL_NODE
148 static int ip6fw_sysctl SYSCTL_HANDLER_ARGS;
149
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, "");
158
159 static int
160 ip6fw_sysctl SYSCTL_HANDLER_ARGS
161 {
162 #pragma unused(arg1, arg2)
163 int error;
164
165 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
166 if (error || !req->newptr)
167 return (error);
168
169 ip6fw_kev_post_msg(KEV_IP6FW_ENABLE);
170
171 return error;
172 }
173
174 #endif
175
176 #define dprintf(a) do { \
177 if (fw6_debug) \
178 printf a; \
179 } while (0)
180 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
181
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,
187 struct mbuf **m,
188 int *off, int *nxt, u_short *offset));
189 static int port_match6 __P((u_short *portptr, int nports, u_short port,
190 int range_flag));
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));
195
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
200 static char err_prefix[] = "ip6_fw_ctl:";
201 extern lck_mtx_t *ip6_mutex;
202
203 /*
204 * Returns 1 if the port is matched by the vector, 0 otherwise
205 */
206 static
207 __inline int
208 port_match6(u_short *portptr, int nports, u_short port, int range_flag)
209 {
210 if (!nports)
211 return 1;
212 if (range_flag) {
213 if (portptr[0] <= port && port <= portptr[1]) {
214 return 1;
215 }
216 nports -= 2;
217 portptr += 2;
218 }
219 while (nports-- > 0) {
220 if (*portptr++ == port) {
221 return 1;
222 }
223 }
224 return 0;
225 }
226
227 static int
228 tcp6flg_match(struct tcphdr *tcp6, struct ip6_fw *f)
229 {
230 u_char flg_set, flg_clr;
231
232 /*
233 * If an established connection is required, reject packets that
234 * have only SYN of RST|ACK|SYN set. Otherwise, fall through to
235 * other flag requirements.
236 */
237 if ((f->fw_ipflg & IPV6_FW_IF_TCPEST) &&
238 ((tcp6->th_flags & (IPV6_FW_TCPF_RST | IPV6_FW_TCPF_ACK |
239 IPV6_FW_TCPF_SYN)) == IPV6_FW_TCPF_SYN))
240 return 0;
241
242 flg_set = tcp6->th_flags & f->fw_tcpf;
243 flg_clr = tcp6->th_flags & f->fw_tcpnf;
244
245 if (flg_set != f->fw_tcpf)
246 return 0;
247 if (flg_clr)
248 return 0;
249
250 return 1;
251 }
252
253 static int
254 icmp6type_match(struct icmp6_hdr *icmp6, struct ip6_fw *f)
255 {
256 int type;
257
258 if (!(f->fw_flg & IPV6_FW_F_ICMPBIT))
259 return(1);
260
261 type = icmp6->icmp6_type;
262
263 /* check for matching type in the bitmap */
264 if (type < IPV6_FW_ICMPTYPES_DIM * sizeof(unsigned) * 8 &&
265 (f->fw_icmp6types[type / (sizeof(unsigned) * 8)] &
266 (1U << (type % (8 * sizeof(unsigned))))))
267 return(1);
268
269 return(0); /* no match */
270 }
271
272 static int
273 is_icmp6_query(struct ip6_hdr *ip6, int off)
274 {
275 const struct icmp6_hdr *icmp6;
276 int icmp6_type;
277
278 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
279 icmp6_type = icmp6->icmp6_type;
280
281 if (icmp6_type == ICMP6_ECHO_REQUEST ||
282 icmp6_type == ICMP6_MEMBERSHIP_QUERY ||
283 icmp6_type == ICMP6_WRUREQUEST ||
284 icmp6_type == ICMP6_FQDN_QUERY ||
285 icmp6_type == ICMP6_NI_QUERY)
286 return(1);
287
288 return(0);
289 }
290
291 static int
292 ip6opts_match(struct ip6_hdr **pip6, struct ip6_fw *f, struct mbuf **m,
293 int *off, int *nxt, u_short *offset)
294 {
295 int len;
296 struct ip6_hdr *ip6 = *pip6;
297 struct ip6_ext *ip6e;
298 u_char opts, nopts, nopts_sve;
299
300 opts = f->fw_ip6opt;
301 nopts = nopts_sve = f->fw_ip6nopt;
302
303 *nxt = ip6->ip6_nxt;
304 *off = sizeof(struct ip6_hdr);
305 len = ntohs(ip6->ip6_plen) + sizeof(struct ip6_hdr);
306 while (*off < len) {
307 ip6e = (struct ip6_ext *)((caddr_t) ip6 + *off);
308 if ((*m)->m_len < *off + sizeof(*ip6e))
309 goto opts_check; /* XXX */
310
311 switch(*nxt) {
312 case IPPROTO_FRAGMENT:
313 if ((*m)->m_len >= *off + sizeof(struct ip6_frag)) {
314 struct ip6_frag *ip6f;
315
316 ip6f = (struct ip6_frag *) ((caddr_t)ip6 + *off);
317 *offset = ip6f->ip6f_offlg & IP6F_OFF_MASK;
318 }
319 opts &= ~IPV6_FW_IP6OPT_FRAG;
320 nopts &= ~IPV6_FW_IP6OPT_FRAG;
321 *off += sizeof(struct ip6_frag);
322 break;
323 case IPPROTO_AH:
324 opts &= ~IPV6_FW_IP6OPT_AH;
325 nopts &= ~IPV6_FW_IP6OPT_AH;
326 *off += (ip6e->ip6e_len + 2) << 2;
327 break;
328 default:
329 switch (*nxt) {
330 case IPPROTO_HOPOPTS:
331 opts &= ~IPV6_FW_IP6OPT_HOPOPT;
332 nopts &= ~IPV6_FW_IP6OPT_HOPOPT;
333 break;
334 case IPPROTO_ROUTING:
335 opts &= ~IPV6_FW_IP6OPT_ROUTE;
336 nopts &= ~IPV6_FW_IP6OPT_ROUTE;
337 break;
338 case IPPROTO_ESP:
339 opts &= ~IPV6_FW_IP6OPT_ESP;
340 nopts &= ~IPV6_FW_IP6OPT_ESP;
341 break;
342 case IPPROTO_NONE:
343 opts &= ~IPV6_FW_IP6OPT_NONXT;
344 nopts &= ~IPV6_FW_IP6OPT_NONXT;
345 goto opts_check;
346 break;
347 case IPPROTO_DSTOPTS:
348 opts &= ~IPV6_FW_IP6OPT_OPTS;
349 nopts &= ~IPV6_FW_IP6OPT_OPTS;
350 break;
351 default:
352 goto opts_check;
353 break;
354 }
355 *off += (ip6e->ip6e_len + 1) << 3;
356 break;
357 }
358 *nxt = ip6e->ip6e_nxt;
359
360 }
361 opts_check:
362 if (f->fw_ip6opt == f->fw_ip6nopt) /* XXX */
363 return 1;
364
365 if (opts == 0 && nopts == nopts_sve)
366 return 1;
367 else
368 return 0;
369 }
370
371 static
372 __inline int
373 iface_match(struct ifnet *ifp, union ip6_fw_if *ifu, int byname)
374 {
375 /* Check by name or by IP address */
376 if (byname) {
377 /* Check unit number (-1 is wildcard) */
378 if (ifu->fu_via_if.unit != -1
379 && ifp->if_unit != ifu->fu_via_if.unit)
380 return(0);
381 /* Check name */
382 if (strncmp(ifp->if_name, ifu->fu_via_if.name, IP6FW_IFNLEN))
383 return(0);
384 return(1);
385 } else if (!IN6_IS_ADDR_UNSPECIFIED(&ifu->fu_via_ip6)) { /* Zero == wildcard */
386 struct ifaddr *ia;
387
388 ifnet_lock_shared(ifp);
389 for (ia = ifp->if_addrlist.tqh_first; ia; ia = ia->ifa_list.tqe_next)
390 {
391
392 if (ia->ifa_addr == NULL)
393 continue;
394 if (ia->ifa_addr->sa_family != AF_INET6)
395 continue;
396 if (!IN6_ARE_ADDR_EQUAL(&ifu->fu_via_ip6,
397 &(((struct sockaddr_in6 *)
398 (ia->ifa_addr))->sin6_addr)))
399 continue;
400 ifnet_lock_done(ifp);
401 return(1);
402 }
403 ifnet_lock_done(ifp);
404 return(0);
405 }
406 return(1);
407 }
408
409 static void
410 ip6fw_report(struct ip6_fw *f, struct ip6_hdr *ip6,
411 struct ifnet *rif, struct ifnet *oif, int off, int nxt)
412 {
413 static int counter;
414 struct tcphdr *const tcp6 = (struct tcphdr *) ((caddr_t) ip6+ off);
415 struct udphdr *const udp = (struct udphdr *) ((caddr_t) ip6+ off);
416 struct icmp6_hdr *const icmp6 = (struct icmp6_hdr *) ((caddr_t) ip6+ off);
417 int count;
418 const char *action;
419 char action2[32], proto[102], name[18];
420 int len;
421
422 count = f ? f->fw_pcnt : ++counter;
423 if (fw6_verbose_limit != 0 && count > fw6_verbose_limit)
424 return;
425
426 /* Print command name */
427 snprintf(SNPARGS(name, 0), "ip6fw: %d", f ? f->fw_number : -1);
428
429 action = action2;
430 if (!f)
431 action = "Refuse";
432 else {
433 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
434 case IPV6_FW_F_DENY:
435 action = "Deny";
436 break;
437 case IPV6_FW_F_REJECT:
438 if (f->fw_reject_code == IPV6_FW_REJECT_RST)
439 action = "Reset";
440 else
441 action = "Unreach";
442 break;
443 case IPV6_FW_F_ACCEPT:
444 action = "Accept";
445 break;
446 case IPV6_FW_F_COUNT:
447 action = "Count";
448 break;
449 case IPV6_FW_F_DIVERT:
450 snprintf(SNPARGS(action2, 0), "Divert %d",
451 f->fw_divert_port);
452 break;
453 case IPV6_FW_F_TEE:
454 snprintf(SNPARGS(action2, 0), "Tee %d",
455 f->fw_divert_port);
456 break;
457 case IPV6_FW_F_SKIPTO:
458 snprintf(SNPARGS(action2, 0), "SkipTo %d",
459 f->fw_skipto_rule);
460 break;
461 default:
462 action = "UNKNOWN";
463 break;
464 }
465 }
466
467 switch (nxt) {
468 case IPPROTO_TCP:
469 len = snprintf(SNPARGS(proto, 0), "TCP [%s]",
470 ip6_sprintf(&ip6->ip6_src));
471 if (off > 0)
472 len += snprintf(SNPARGS(proto, len), ":%d ",
473 ntohs(tcp6->th_sport));
474 else
475 len += snprintf(SNPARGS(proto, len), " ");
476 len += snprintf(SNPARGS(proto, len), "[%s]",
477 ip6_sprintf(&ip6->ip6_dst));
478 if (off > 0)
479 snprintf(SNPARGS(proto, len), ":%d",
480 ntohs(tcp6->th_dport));
481 break;
482 case IPPROTO_UDP:
483 len = snprintf(SNPARGS(proto, 0), "UDP [%s]",
484 ip6_sprintf(&ip6->ip6_src));
485 if (off > 0)
486 len += snprintf(SNPARGS(proto, len), ":%d ",
487 ntohs(udp->uh_sport));
488 else
489 len += snprintf(SNPARGS(proto, len), " ");
490 len += snprintf(SNPARGS(proto, len), "[%s]",
491 ip6_sprintf(&ip6->ip6_dst));
492 if (off > 0)
493 snprintf(SNPARGS(proto, len), ":%d",
494 ntohs(udp->uh_dport));
495 break;
496 case IPPROTO_ICMPV6:
497 if (off > 0)
498 len = snprintf(SNPARGS(proto, 0), "IPV6-ICMP:%u.%u ",
499 icmp6->icmp6_type, icmp6->icmp6_code);
500 else
501 len = snprintf(SNPARGS(proto, 0), "IPV6-ICMP ");
502 len += snprintf(SNPARGS(proto, len), "[%s]",
503 ip6_sprintf(&ip6->ip6_src));
504 snprintf(SNPARGS(proto, len), " [%s]",
505 ip6_sprintf(&ip6->ip6_dst));
506 break;
507 default:
508 len = snprintf(SNPARGS(proto, 0), "P:%d [%s]", nxt,
509 ip6_sprintf(&ip6->ip6_src));
510 snprintf(SNPARGS(proto, len), " [%s]",
511 ip6_sprintf(&ip6->ip6_dst));
512 break;
513 }
514
515 if (oif)
516 log(LOG_AUTHPRIV | LOG_INFO, "%s %s %s out via %s\n",
517 name, action, proto, if_name(oif));
518 else if (rif)
519 log(LOG_AUTHPRIV | LOG_INFO, "%s %s %s in via %s\n",
520 name, action, proto, if_name(rif));
521 else
522 log(LOG_AUTHPRIV | LOG_INFO, "%s %s %s",
523 name, action, proto);
524 if (fw6_verbose_limit != 0 && count == fw6_verbose_limit)
525 log(LOG_AUTHPRIV | LOG_INFO, "ip6fw: limit reached on entry %d\n",
526 f ? f->fw_number : -1);
527 }
528
529 /*
530 * Parameters:
531 *
532 * ip Pointer to packet header (struct ip6_hdr *)
533 * hlen Packet header length
534 * oif Outgoing interface, or NULL if packet is incoming
535 * #ifndef IP6FW_DIVERT_RESTART
536 * *cookie Ignore all divert/tee rules to this port (if non-zero)
537 * #else
538 * *cookie Skip up to the first rule past this rule number;
539 * #endif
540 * *m The packet; we set to NULL when/if we nuke it.
541 *
542 * Return value:
543 *
544 * 0 The packet is to be accepted and routed normally OR
545 * the packet was denied/rejected and has been dropped;
546 * in the latter case, *m is equal to NULL upon return.
547 * port Divert the packet to port.
548 */
549
550 static int
551 ip6_fw_chk(struct ip6_hdr **pip6,
552 struct ifnet *oif, u_int16_t *cookie, struct mbuf **m)
553 {
554 struct ip6_fw_chain *chain;
555 struct ip6_fw *rule = NULL;
556 struct ip6_hdr *ip6 = *pip6;
557 struct ifnet *const rif = (*m)->m_pkthdr.rcvif;
558 u_short offset = 0;
559 int off = sizeof(struct ip6_hdr), nxt = ip6->ip6_nxt;
560 u_short src_port, dst_port;
561 #ifdef IP6FW_DIVERT_RESTART
562 u_int16_t skipto = *cookie;
563 #else
564 u_int16_t ignport = ntohs(*cookie);
565 #endif
566 struct timeval timenow;
567
568 getmicrotime(&timenow);
569
570 *cookie = 0;
571 /*
572 * Go down the chain, looking for enlightment
573 * #ifdef IP6FW_DIVERT_RESTART
574 * If we've been asked to start at a given rule immediatly, do so.
575 * #endif
576 */
577 chain = LIST_FIRST(&ip6_fw_chain);
578 #ifdef IP6FW_DIVERT_RESTART
579 if (skipto) {
580 if (skipto >= 65535)
581 goto dropit;
582 while (chain && (chain->rule->fw_number <= skipto)) {
583 chain = LIST_NEXT(chain, chain);
584 }
585 if (! chain) goto dropit;
586 }
587 #endif /* IP6FW_DIVERT_RESTART */
588 for (; chain; chain = LIST_NEXT(chain, chain)) {
589 struct ip6_fw *const f = chain->rule;
590
591 if (oif) {
592 /* Check direction outbound */
593 if (!(f->fw_flg & IPV6_FW_F_OUT))
594 continue;
595 } else {
596 /* Check direction inbound */
597 if (!(f->fw_flg & IPV6_FW_F_IN))
598 continue;
599 }
600
601 #define IN6_ARE_ADDR_MASKEQUAL(x,y,z) (\
602 (((x)->s6_addr32[0] & (y)->s6_addr32[0]) == (z)->s6_addr32[0]) && \
603 (((x)->s6_addr32[1] & (y)->s6_addr32[1]) == (z)->s6_addr32[1]) && \
604 (((x)->s6_addr32[2] & (y)->s6_addr32[2]) == (z)->s6_addr32[2]) && \
605 (((x)->s6_addr32[3] & (y)->s6_addr32[3]) == (z)->s6_addr32[3]))
606
607 /* If src-addr doesn't match, not this rule. */
608 if (((f->fw_flg & IPV6_FW_F_INVSRC) != 0) ^
609 (!IN6_ARE_ADDR_MASKEQUAL(&ip6->ip6_src,&f->fw_smsk,&f->fw_src)))
610 continue;
611
612 /* If dest-addr doesn't match, not this rule. */
613 if (((f->fw_flg & IPV6_FW_F_INVDST) != 0) ^
614 (!IN6_ARE_ADDR_MASKEQUAL(&ip6->ip6_dst,&f->fw_dmsk,&f->fw_dst)))
615 continue;
616
617 #undef IN6_ARE_ADDR_MASKEQUAL
618 /* Interface check */
619 if ((f->fw_flg & IF6_FW_F_VIAHACK) == IF6_FW_F_VIAHACK) {
620 struct ifnet *const iface = oif ? oif : rif;
621
622 /* Backwards compatibility hack for "via" */
623 if (!iface || !iface_match(iface,
624 &f->fw_in_if, f->fw_flg & IPV6_FW_F_OIFNAME))
625 continue;
626 } else {
627 /* Check receive interface */
628 if ((f->fw_flg & IPV6_FW_F_IIFACE)
629 && (!rif || !iface_match(rif,
630 &f->fw_in_if, f->fw_flg & IPV6_FW_F_IIFNAME)))
631 continue;
632 /* Check outgoing interface */
633 if ((f->fw_flg & IPV6_FW_F_OIFACE)
634 && (!oif || !iface_match(oif,
635 &f->fw_out_if, f->fw_flg & IPV6_FW_F_OIFNAME)))
636 continue;
637 }
638
639 /* Check IP options */
640 if (!ip6opts_match(&ip6, f, m, &off, &nxt, &offset))
641 continue;
642
643 /* Fragments */
644 if ((f->fw_flg & IPV6_FW_F_FRAG) && !offset)
645 continue;
646
647 /* Check protocol; if wildcard, match */
648 if (f->fw_prot == IPPROTO_IPV6)
649 goto got_match;
650
651 /* If different, don't match */
652 if (nxt != f->fw_prot)
653 continue;
654
655 #define PULLUP_TO(len) do { \
656 if ((*m)->m_len < (len) \
657 && (*m = m_pullup(*m, (len))) == 0) { \
658 goto dropit; \
659 } \
660 *pip6 = ip6 = mtod(*m, struct ip6_hdr *); \
661 } while (0)
662
663 /* Protocol specific checks */
664 switch (nxt) {
665 case IPPROTO_TCP:
666 {
667 struct tcphdr *tcp6;
668
669 if (offset == 1) { /* cf. RFC 1858 */
670 PULLUP_TO(off + 4); /* XXX ? */
671 goto bogusfrag;
672 }
673 if (offset != 0) {
674 /*
675 * TCP flags and ports aren't available in this
676 * packet -- if this rule specified either one,
677 * we consider the rule a non-match.
678 */
679 if (f->fw_nports != 0 ||
680 f->fw_tcpf != f->fw_tcpnf)
681 continue;
682
683 break;
684 }
685 PULLUP_TO(off + 14);
686 tcp6 = (struct tcphdr *) ((caddr_t)ip6 + off);
687 if (((f->fw_tcpf != f->fw_tcpnf) ||
688 (f->fw_ipflg & IPV6_FW_IF_TCPEST)) &&
689 !tcp6flg_match(tcp6, f))
690 continue;
691 src_port = ntohs(tcp6->th_sport);
692 dst_port = ntohs(tcp6->th_dport);
693 goto check_ports;
694 }
695
696 case IPPROTO_UDP:
697 {
698 struct udphdr *udp;
699
700 if (offset != 0) {
701 /*
702 * Port specification is unavailable -- if this
703 * rule specifies a port, we consider the rule
704 * a non-match.
705 */
706 if (f->fw_nports != 0)
707 continue;
708
709 break;
710 }
711 PULLUP_TO(off + 4);
712 udp = (struct udphdr *) ((caddr_t)ip6 + off);
713 src_port = ntohs(udp->uh_sport);
714 dst_port = ntohs(udp->uh_dport);
715 check_ports:
716 if (!port_match6(&f->fw_pts[0],
717 IPV6_FW_GETNSRCP(f), src_port,
718 f->fw_flg & IPV6_FW_F_SRNG))
719 continue;
720 if (!port_match6(&f->fw_pts[IPV6_FW_GETNSRCP(f)],
721 IPV6_FW_GETNDSTP(f), dst_port,
722 f->fw_flg & IPV6_FW_F_DRNG))
723 continue;
724 break;
725 }
726
727 case IPPROTO_ICMPV6:
728 {
729 struct icmp6_hdr *icmp;
730
731 if (offset != 0) /* Type isn't valid */
732 break;
733 PULLUP_TO(off + 2);
734 icmp = (struct icmp6_hdr *) ((caddr_t)ip6 + off);
735 if (!icmp6type_match(icmp, f))
736 continue;
737 break;
738 }
739 #undef PULLUP_TO
740
741 bogusfrag:
742 if (fw6_verbose)
743 ip6fw_report(NULL, ip6, rif, oif, off, nxt);
744 goto dropit;
745 }
746
747 got_match:
748 #ifndef IP6FW_DIVERT_RESTART
749 /* Ignore divert/tee rule if socket port is "ignport" */
750 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
751 case IPV6_FW_F_DIVERT:
752 case IPV6_FW_F_TEE:
753 if (f->fw_divert_port == ignport)
754 continue; /* ignore this rule */
755 break;
756 }
757
758 #endif /* IP6FW_DIVERT_RESTART */
759 /* Update statistics */
760 f->fw_pcnt += 1;
761 f->fw_bcnt += ntohs(ip6->ip6_plen);
762 f->timestamp = timenow.tv_sec;
763
764 /* Log to console if desired */
765 if ((f->fw_flg & IPV6_FW_F_PRN) && fw6_verbose)
766 ip6fw_report(f, ip6, rif, oif, off, nxt);
767
768 /* Take appropriate action */
769 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
770 case IPV6_FW_F_ACCEPT:
771 return(0);
772 case IPV6_FW_F_COUNT:
773 continue;
774 case IPV6_FW_F_DIVERT:
775 #ifdef IP6FW_DIVERT_RESTART
776 *cookie = f->fw_number;
777 #else
778 *cookie = htons(f->fw_divert_port);
779 #endif /* IP6FW_DIVERT_RESTART */
780 return(f->fw_divert_port);
781 case IPV6_FW_F_TEE:
782 /*
783 * XXX someday tee packet here, but beware that you
784 * can't use m_copym() or m_copypacket() because
785 * the divert input routine modifies the mbuf
786 * (and these routines only increment reference
787 * counts in the case of mbuf clusters), so need
788 * to write custom routine.
789 */
790 continue;
791 case IPV6_FW_F_SKIPTO:
792 #ifdef DIAGNOSTIC
793 while (chain->chain.le_next
794 && chain->chain.le_next->rule->fw_number
795 < f->fw_skipto_rule)
796 #else
797 while (chain->chain.le_next->rule->fw_number
798 < f->fw_skipto_rule)
799 #endif
800 chain = chain->chain.le_next;
801 continue;
802 }
803
804 /* Deny/reject this packet using this rule */
805 rule = f;
806 break;
807 }
808
809 #ifdef DIAGNOSTIC
810 /* Rule 65535 should always be there and should always match */
811 if (!chain)
812 panic("ip6_fw: chain");
813 #endif
814
815 /*
816 * At this point, we're going to drop the packet.
817 * Send a reject notice if all of the following are true:
818 *
819 * - The packet matched a reject rule
820 * - The packet is not an ICMP packet, or is an ICMP query packet
821 * - The packet is not a multicast or broadcast packet
822 */
823 if ((rule->fw_flg & IPV6_FW_F_COMMAND) == IPV6_FW_F_REJECT
824 && (nxt != IPPROTO_ICMPV6 || is_icmp6_query(ip6, off))
825 && !((*m)->m_flags & (M_BCAST|M_MCAST))
826 && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
827 switch (rule->fw_reject_code) {
828 case IPV6_FW_REJECT_RST:
829 {
830 struct tcphdr *const tcp =
831 (struct tcphdr *) ((caddr_t)ip6 + off);
832 struct {
833 struct ip6_hdr ip6;
834 struct tcphdr th;
835 } ti;
836 tcp_seq ack, seq;
837 int flags;
838
839 if (offset != 0 || (tcp->th_flags & TH_RST))
840 break;
841
842 ti.ip6 = *ip6;
843 ti.th = *tcp;
844 ti.th.th_seq = ntohl(ti.th.th_seq);
845 ti.th.th_ack = ntohl(ti.th.th_ack);
846 ti.ip6.ip6_nxt = IPPROTO_TCP;
847 if (ti.th.th_flags & TH_ACK) {
848 ack = 0;
849 seq = ti.th.th_ack;
850 flags = TH_RST;
851 } else {
852 ack = ti.th.th_seq;
853 if (((*m)->m_flags & M_PKTHDR) != 0) {
854 ack += (*m)->m_pkthdr.len - off
855 - (ti.th.th_off << 2);
856 } else if (ip6->ip6_plen) {
857 ack += ntohs(ip6->ip6_plen) + sizeof(*ip6)
858 - off - (ti.th.th_off << 2);
859 } else {
860 m_freem(*m);
861 *m = 0;
862 break;
863 }
864 seq = 0;
865 flags = TH_RST|TH_ACK;
866 }
867 bcopy(&ti, ip6, sizeof(ti));
868 tcp_respond(NULL, ip6, (struct tcphdr *)(ip6 + 1),
869 *m, ack, seq, flags, IFSCOPE_NONE);
870 *m = NULL;
871 break;
872 }
873 default: /* Send an ICMP unreachable using code */
874 if (oif)
875 (*m)->m_pkthdr.rcvif = oif;
876 lck_mtx_assert(ip6_mutex, LCK_MTX_ASSERT_OWNED);
877 lck_mtx_unlock(ip6_mutex);
878 icmp6_error(*m, ICMP6_DST_UNREACH,
879 rule->fw_reject_code, 0);
880 lck_mtx_lock(ip6_mutex);
881 *m = NULL;
882 break;
883 }
884 }
885
886 dropit:
887 /*
888 * Finally, drop the packet.
889 */
890 if (*m) {
891 m_freem(*m);
892 *m = NULL;
893 }
894 return(0);
895 }
896
897 static int
898 add_entry6(struct ip6_fw_head *chainptr, struct ip6_fw *frwl)
899 {
900 struct ip6_fw *ftmp = 0;
901 struct ip6_fw_chain *fwc = 0, *fcp, *fcpl = 0;
902 u_short nbr = 0;
903 int s;
904
905 fwc = _MALLOC(sizeof *fwc, M_IP6FW, M_WAITOK);
906 ftmp = _MALLOC(sizeof *ftmp, M_IP6FW, M_WAITOK);
907 if (!fwc || !ftmp) {
908 dprintf(("%s malloc said no\n", err_prefix));
909 if (fwc) FREE(fwc, M_IP6FW);
910 if (ftmp) FREE(ftmp, M_IP6FW);
911 return (ENOSPC);
912 }
913
914 bcopy(frwl, ftmp, sizeof(struct ip6_fw));
915 ftmp->fw_in_if.fu_via_if.name[IP6FW_IFNLEN - 1] = '\0';
916 ftmp->fw_pcnt = 0L;
917 ftmp->fw_bcnt = 0L;
918 fwc->rule = ftmp;
919
920 s = splnet();
921
922 if (!chainptr->lh_first) {
923 LIST_INSERT_HEAD(chainptr, fwc, chain);
924 splx(s);
925 return(0);
926 } else if (ftmp->fw_number == (u_short)-1) {
927 if (fwc) FREE(fwc, M_IP6FW);
928 if (ftmp) FREE(ftmp, M_IP6FW);
929 splx(s);
930 dprintf(("%s bad rule number\n", err_prefix));
931 return (EINVAL);
932 }
933
934 /* If entry number is 0, find highest numbered rule and add 100 */
935 if (ftmp->fw_number == 0) {
936 for (fcp = chainptr->lh_first; fcp; fcp = fcp->chain.le_next) {
937 if (fcp->rule->fw_number != (u_short)-1)
938 nbr = fcp->rule->fw_number;
939 else
940 break;
941 }
942 if (nbr < (u_short)-1 - 100)
943 nbr += 100;
944 ftmp->fw_number = nbr;
945 }
946
947 /* Got a valid number; now insert it, keeping the list ordered */
948 for (fcp = chainptr->lh_first; fcp; fcp = fcp->chain.le_next) {
949 if (fcp->rule->fw_number > ftmp->fw_number) {
950 if (fcpl) {
951 LIST_INSERT_AFTER(fcpl, fwc, chain);
952 } else {
953 LIST_INSERT_HEAD(chainptr, fwc, chain);
954 }
955 break;
956 } else {
957 fcpl = fcp;
958 }
959 }
960
961 splx(s);
962 return (0);
963 }
964
965 static int
966 del_entry6(struct ip6_fw_head *chainptr, u_short number)
967 {
968 struct ip6_fw_chain *fcp;
969 int s;
970
971 s = splnet();
972
973 fcp = chainptr->lh_first;
974 if (number != (u_short)-1) {
975 for (; fcp; fcp = fcp->chain.le_next) {
976 if (fcp->rule->fw_number == number) {
977 LIST_REMOVE(fcp, chain);
978 splx(s);
979 FREE(fcp->rule, M_IP6FW);
980 FREE(fcp, M_IP6FW);
981 return 0;
982 }
983 }
984 }
985
986 splx(s);
987 return (EINVAL);
988 }
989
990 static int
991 zero_entry6(struct ip6_fw *frwl)
992 {
993 struct ip6_fw_chain *fcp;
994 int s;
995
996 /*
997 * It's possible to insert multiple chain entries with the
998 * same number, so we don't stop after finding the first
999 * match if zeroing a specific entry.
1000 */
1001 s = splnet();
1002 for (fcp = ip6_fw_chain.lh_first; fcp; fcp = fcp->chain.le_next)
1003 if (!frwl || frwl->fw_number == 0 || frwl->fw_number == fcp->rule->fw_number) {
1004 fcp->rule->fw_bcnt = fcp->rule->fw_pcnt = 0;
1005 fcp->rule->timestamp = 0;
1006 }
1007 splx(s);
1008
1009 if (fw6_verbose) {
1010 if (frwl)
1011 log(LOG_AUTHPRIV | LOG_NOTICE,
1012 "ip6fw: Entry %d cleared.\n", frwl->fw_number);
1013 else
1014 log(LOG_AUTHPRIV | LOG_NOTICE,
1015 "ip6fw: Accounting cleared.\n");
1016 }
1017
1018 return(0);
1019 }
1020
1021 static struct ip6_fw *
1022 check_ip6fw_struct(struct ip6_fw *frwl)
1023 {
1024 /* Check for invalid flag bits */
1025 if ((frwl->fw_flg & ~IPV6_FW_F_MASK) != 0) {
1026 dprintf(("%s undefined flag bits set (flags=%x)\n",
1027 err_prefix, frwl->fw_flg));
1028 return (NULL);
1029 }
1030 /* Must apply to incoming or outgoing (or both) */
1031 if (!(frwl->fw_flg & (IPV6_FW_F_IN | IPV6_FW_F_OUT))) {
1032 dprintf(("%s neither in nor out\n", err_prefix));
1033 return (NULL);
1034 }
1035 /* Empty interface name is no good */
1036 if (((frwl->fw_flg & IPV6_FW_F_IIFNAME)
1037 && !*frwl->fw_in_if.fu_via_if.name)
1038 || ((frwl->fw_flg & IPV6_FW_F_OIFNAME)
1039 && !*frwl->fw_out_if.fu_via_if.name)) {
1040 dprintf(("%s empty interface name\n", err_prefix));
1041 return (NULL);
1042 }
1043 /* Sanity check interface matching */
1044 if ((frwl->fw_flg & IF6_FW_F_VIAHACK) == IF6_FW_F_VIAHACK) {
1045 ; /* allow "via" backwards compatibility */
1046 } else if ((frwl->fw_flg & IPV6_FW_F_IN)
1047 && (frwl->fw_flg & IPV6_FW_F_OIFACE)) {
1048 dprintf(("%s outgoing interface check on incoming\n",
1049 err_prefix));
1050 return (NULL);
1051 }
1052 /* Sanity check port ranges */
1053 if ((frwl->fw_flg & IPV6_FW_F_SRNG) && IPV6_FW_GETNSRCP(frwl) < 2) {
1054 dprintf(("%s src range set but n_src_p=%d\n",
1055 err_prefix, IPV6_FW_GETNSRCP(frwl)));
1056 return (NULL);
1057 }
1058 if ((frwl->fw_flg & IPV6_FW_F_DRNG) && IPV6_FW_GETNDSTP(frwl) < 2) {
1059 dprintf(("%s dst range set but n_dst_p=%d\n",
1060 err_prefix, IPV6_FW_GETNDSTP(frwl)));
1061 return (NULL);
1062 }
1063 if (IPV6_FW_GETNSRCP(frwl) + IPV6_FW_GETNDSTP(frwl) > IPV6_FW_MAX_PORTS) {
1064 dprintf(("%s too many ports (%d+%d)\n",
1065 err_prefix, IPV6_FW_GETNSRCP(frwl), IPV6_FW_GETNDSTP(frwl)));
1066 return (NULL);
1067 }
1068 /*
1069 * Protocols other than TCP/UDP don't use port range
1070 */
1071 if ((frwl->fw_prot != IPPROTO_TCP) &&
1072 (frwl->fw_prot != IPPROTO_UDP) &&
1073 (IPV6_FW_GETNSRCP(frwl) || IPV6_FW_GETNDSTP(frwl))) {
1074 dprintf(("%s port(s) specified for non TCP/UDP rule\n",
1075 err_prefix));
1076 return(NULL);
1077 }
1078
1079 /*
1080 * Rather than modify the entry to make such entries work,
1081 * we reject this rule and require user level utilities
1082 * to enforce whatever policy they deem appropriate.
1083 */
1084 if ((frwl->fw_src.s6_addr32[0] & (~frwl->fw_smsk.s6_addr32[0])) ||
1085 (frwl->fw_src.s6_addr32[1] & (~frwl->fw_smsk.s6_addr32[1])) ||
1086 (frwl->fw_src.s6_addr32[2] & (~frwl->fw_smsk.s6_addr32[2])) ||
1087 (frwl->fw_src.s6_addr32[3] & (~frwl->fw_smsk.s6_addr32[3])) ||
1088 (frwl->fw_dst.s6_addr32[0] & (~frwl->fw_dmsk.s6_addr32[0])) ||
1089 (frwl->fw_dst.s6_addr32[1] & (~frwl->fw_dmsk.s6_addr32[1])) ||
1090 (frwl->fw_dst.s6_addr32[2] & (~frwl->fw_dmsk.s6_addr32[2])) ||
1091 (frwl->fw_dst.s6_addr32[3] & (~frwl->fw_dmsk.s6_addr32[3]))) {
1092 dprintf(("%s rule never matches\n", err_prefix));
1093 return(NULL);
1094 }
1095
1096 if ((frwl->fw_flg & IPV6_FW_F_FRAG) &&
1097 (frwl->fw_prot == IPPROTO_UDP || frwl->fw_prot == IPPROTO_TCP)) {
1098 if (frwl->fw_nports) {
1099 dprintf(("%s cannot mix 'frag' and ports\n", err_prefix));
1100 return(NULL);
1101 }
1102 if (frwl->fw_prot == IPPROTO_TCP &&
1103 frwl->fw_tcpf != frwl->fw_tcpnf) {
1104 dprintf(("%s cannot mix 'frag' with TCP flags\n", err_prefix));
1105 return(NULL);
1106 }
1107 }
1108
1109 /* Check command specific stuff */
1110 switch (frwl->fw_flg & IPV6_FW_F_COMMAND)
1111 {
1112 case IPV6_FW_F_REJECT:
1113 if (frwl->fw_reject_code >= 0x100
1114 && !(frwl->fw_prot == IPPROTO_TCP
1115 && frwl->fw_reject_code == IPV6_FW_REJECT_RST)) {
1116 dprintf(("%s unknown reject code\n", err_prefix));
1117 return(NULL);
1118 }
1119 break;
1120 case IPV6_FW_F_DIVERT: /* Diverting to port zero is invalid */
1121 case IPV6_FW_F_TEE:
1122 if (frwl->fw_divert_port == 0) {
1123 dprintf(("%s can't divert to port 0\n", err_prefix));
1124 return (NULL);
1125 }
1126 break;
1127 case IPV6_FW_F_DENY:
1128 case IPV6_FW_F_ACCEPT:
1129 case IPV6_FW_F_COUNT:
1130 case IPV6_FW_F_SKIPTO:
1131 break;
1132 default:
1133 dprintf(("%s invalid command\n", err_prefix));
1134 return(NULL);
1135 }
1136
1137 return frwl;
1138 }
1139
1140 /*#####*/
1141 #if 0
1142 static int
1143 ip6_fw_ctl(int stage, struct mbuf **mm)
1144 {
1145 int error;
1146 struct mbuf *m;
1147
1148 if (stage == IPV6_FW_GET) {
1149 struct ip6_fw_chain *fcp = ip6_fw_chain.lh_first;
1150 *mm = m = m_get(M_WAIT, MT_DATA); /* XXX */
1151 if (!m)
1152 return(ENOBUFS);
1153 if (sizeof *(fcp->rule) > MLEN) {
1154 MCLGET(m, M_WAIT);
1155 if ((m->m_flags & M_EXT) == 0) {
1156 m_free(m);
1157 return(ENOBUFS);
1158 }
1159 }
1160 for (; fcp; fcp = fcp->chain.le_next) {
1161 bcopy(fcp->rule, m->m_data, sizeof *(fcp->rule));
1162 m->m_len = sizeof *(fcp->rule);
1163 m->m_next = m_get(M_WAIT, MT_DATA); /* XXX */
1164 if (!m->m_next) {
1165 m_freem(*mm);
1166 return(ENOBUFS);
1167 }
1168 m = m->m_next;
1169 if (sizeof *(fcp->rule) > MLEN) {
1170 MCLGET(m, M_WAIT);
1171 if ((m->m_flags & M_EXT) == 0) {
1172 m_freem(*mm);
1173 return(ENOBUFS);
1174 }
1175 }
1176 m->m_len = 0;
1177 }
1178 return (0);
1179 }
1180 m = *mm;
1181 /* only allow get calls if secure mode > 2 */
1182 if (securelevel > 2) {
1183 if (m) {
1184 (void)m_freem(m);
1185 *mm = 0;
1186 }
1187 return(EPERM);
1188 }
1189 if (stage == IPV6_FW_FLUSH) {
1190 while (ip6_fw_chain.lh_first != NULL &&
1191 ip6_fw_chain.lh_first->rule->fw_number != (u_short)-1) {
1192 struct ip6_fw_chain *fcp = ip6_fw_chain.lh_first;
1193 int s = splnet();
1194 LIST_REMOVE(ip6_fw_chain.lh_first, chain);
1195 splx(s);
1196 FREE(fcp->rule, M_IP6FW);
1197 FREE(fcp, M_IP6FW);
1198 }
1199 if (m) {
1200 (void)m_freem(m);
1201 *mm = 0;
1202 }
1203 return (0);
1204 }
1205 if (stage == IPV6_FW_ZERO) {
1206 error = zero_entry6(m);
1207 if (m) {
1208 (void)m_freem(m);
1209 *mm = 0;
1210 }
1211 return (error);
1212 }
1213 if (m == NULL) {
1214 printf("%s NULL mbuf ptr\n", err_prefix);
1215 return (EINVAL);
1216 }
1217
1218 if (stage == IPV6_FW_ADD) {
1219 struct ip6_fw *frwl = check_ip6fw_mbuf(m);
1220
1221 if (!frwl)
1222 error = EINVAL;
1223 else
1224 error = add_entry6(&ip6_fw_chain, frwl);
1225 if (m) {
1226 (void)m_freem(m);
1227 *mm = 0;
1228 }
1229 return error;
1230 }
1231 if (stage == IPV6_FW_DEL) {
1232 if (m->m_len != sizeof(struct ip6_fw)) {
1233 dprintf(("%s len=%ld, want %lu\n", err_prefix, m->m_len,
1234 sizeof(struct ip6_fw)));
1235 error = EINVAL;
1236 } else if (mtod(m, struct ip6_fw *)->fw_number == (u_short)-1) {
1237 dprintf(("%s can't delete rule 65535\n", err_prefix));
1238 error = EINVAL;
1239 } else
1240 error = del_entry6(&ip6_fw_chain,
1241 mtod(m, struct ip6_fw *)->fw_number);
1242 if (m) {
1243 (void)m_freem(m);
1244 *mm = 0;
1245 }
1246 return error;
1247 }
1248
1249 dprintf(("%s unknown request %d\n", err_prefix, stage));
1250 if (m) {
1251 (void)m_freem(m);
1252 *mm = 0;
1253 }
1254 return (EINVAL);
1255 }
1256 #endif
1257
1258 static void
1259 ip6fw_kev_post_msg(u_int32_t event_code)
1260 {
1261 struct kev_msg ev_msg;
1262
1263 bzero(&ev_msg, sizeof(struct kev_msg));
1264
1265 ev_msg.vendor_code = KEV_VENDOR_APPLE;
1266 ev_msg.kev_class = KEV_FIREWALL_CLASS;
1267 ev_msg.kev_subclass = KEV_IP6FW_SUBCLASS;
1268 ev_msg.event_code = event_code;
1269
1270 kev_post_msg(&ev_msg);
1271
1272 }
1273
1274
1275 static int
1276 ip6_fw_ctl(struct sockopt *sopt)
1277 {
1278 int error = 0;
1279 int spl;
1280 int valsize;
1281 struct ip6_fw rule;
1282
1283 if (securelevel >= 3 &&
1284 (sopt->sopt_dir != SOPT_GET || sopt->sopt_name != IPV6_FW_GET))
1285 return (EPERM);
1286
1287 /* We ALWAYS expect the client to pass in a rule structure so that we can
1288 * check the version of the API that they are using. In the case of a
1289 * IPV6_FW_GET operation, the first rule of the output buffer passed to us
1290 * must have the version set. */
1291 if (!sopt->sopt_val || sopt->sopt_valsize < sizeof rule) return EINVAL;
1292
1293 /* save sopt->sopt_valsize */
1294 valsize = sopt->sopt_valsize;
1295 if ((error = sooptcopyin(sopt, &rule, sizeof(rule), sizeof(rule))))
1296 return error;
1297
1298 if (rule.version != IPV6_FW_CURRENT_API_VERSION) return EINVAL;
1299 rule.version = 0xFFFFFFFF; /* version is meaningless once rules "make it in the door". */
1300
1301 switch (sopt->sopt_name)
1302 {
1303 case IPV6_FW_GET:
1304 {
1305 struct ip6_fw_chain *fcp;
1306 struct ip6_fw *buf;
1307 size_t size = 0;
1308
1309 spl = splnet();
1310 LIST_FOREACH(fcp, &ip6_fw_chain, chain)
1311 size += sizeof *buf;
1312
1313 buf = _MALLOC(size, M_TEMP, M_WAITOK);
1314 if (!buf) error = ENOBUFS;
1315 else
1316 {
1317 struct ip6_fw *bp = buf;
1318 LIST_FOREACH(fcp, &ip6_fw_chain, chain)
1319 {
1320 bcopy(fcp->rule, bp, sizeof *bp);
1321 bp->version = IPV6_FW_CURRENT_API_VERSION;
1322 bp++;
1323 }
1324 }
1325
1326 splx(spl);
1327 if (buf)
1328 {
1329 sopt->sopt_valsize = valsize;
1330 error = sooptcopyout(sopt, buf, size);
1331 FREE(buf, M_TEMP);
1332 }
1333
1334 break;
1335 }
1336
1337 case IPV6_FW_FLUSH:
1338 spl = splnet();
1339 while (ip6_fw_chain.lh_first &&
1340 ip6_fw_chain.lh_first->rule->fw_number != (u_short)-1)
1341 {
1342 struct ip6_fw_chain *fcp = ip6_fw_chain.lh_first;
1343 LIST_REMOVE(ip6_fw_chain.lh_first, chain);
1344 FREE(fcp->rule, M_IP6FW);
1345 FREE(fcp, M_IP6FW);
1346 }
1347 splx(spl);
1348 ip6fw_kev_post_msg(KEV_IP6FW_FLUSH);
1349 break;
1350
1351 case IPV6_FW_ZERO:
1352 error = zero_entry6(&rule);
1353 break;
1354
1355 case IPV6_FW_ADD:
1356 if (check_ip6fw_struct(&rule)) {
1357 error = add_entry6(&ip6_fw_chain, &rule);
1358
1359 ip6fw_kev_post_msg(KEV_IP6FW_ADD);
1360 } else
1361 error = EINVAL;
1362 break;
1363
1364 case IPV6_FW_DEL:
1365 if (rule.fw_number == (u_short)-1)
1366 {
1367 dprintf(("%s can't delete rule 65535\n", err_prefix));
1368 error = EINVAL;
1369 }
1370 else {
1371 error = del_entry6(&ip6_fw_chain, rule.fw_number);
1372
1373 ip6fw_kev_post_msg(KEV_IP6FW_DEL);
1374 }
1375 break;
1376
1377 default:
1378 dprintf(("%s invalid option %d\n", err_prefix, sopt->sopt_name));
1379 error = EINVAL;
1380 }
1381
1382 return error;
1383 }
1384
1385 void
1386 ip6_fw_init(void)
1387 {
1388 struct ip6_fw default_rule;
1389
1390 ip6_fw_chk_ptr = ip6_fw_chk;
1391 ip6_fw_ctl_ptr = ip6_fw_ctl;
1392 LIST_INIT(&ip6_fw_chain);
1393
1394 bzero(&default_rule, sizeof default_rule);
1395 default_rule.fw_prot = IPPROTO_IPV6;
1396 default_rule.fw_number = (u_short)-1;
1397 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1398 default_rule.fw_flg |= IPV6_FW_F_ACCEPT;
1399 #else
1400 default_rule.fw_flg |= IPV6_FW_F_DENY;
1401 #endif
1402 default_rule.fw_flg |= IPV6_FW_F_IN | IPV6_FW_F_OUT;
1403 if (check_ip6fw_struct(&default_rule) == NULL ||
1404 add_entry6(&ip6_fw_chain, &default_rule))
1405 panic("%s", __FUNCTION__);
1406
1407 printf("IPv6 packet filtering initialized, ");
1408 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1409 printf("default to accept, ");
1410 #endif
1411 #ifndef IPV6FIREWALL_VERBOSE
1412 printf("logging disabled\n");
1413 #else
1414 if (fw6_verbose_limit == 0)
1415 printf("unlimited logging\n");
1416 else
1417 printf("logging limited to %d packets/entry\n",
1418 fw6_verbose_limit);
1419 #endif
1420 }
1421