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