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