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