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