xref: /netbsd-src/sys/netinet6/ip6_mroute.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: ip6_mroute.c,v 1.129 2018/06/21 10:37:50 knakahara Exp $	*/
2 /*	$KAME: ip6_mroute.c,v 1.49 2001/07/25 09:21:18 jinmei Exp $	*/
3 
4 /*
5  * Copyright (C) 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*	BSDI ip_mroute.c,v 2.10 1996/11/14 00:29:52 jch Exp	*/
34 
35 /*
36  * Copyright (c) 1992, 1993
37  *      The Regents of the University of California.  All rights reserved.
38  *
39  * This code is derived from software contributed to Berkeley by
40  * Stephen Deering of Stanford University.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *      @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
67  */
68 
69 /*
70  * Copyright (c) 1989 Stephen Deering
71  *
72  * This code is derived from software contributed to Berkeley by
73  * Stephen Deering of Stanford University.
74  *
75  * Redistribution and use in source and binary forms, with or without
76  * modification, are permitted provided that the following conditions
77  * are met:
78  * 1. Redistributions of source code must retain the above copyright
79  *    notice, this list of conditions and the following disclaimer.
80  * 2. Redistributions in binary form must reproduce the above copyright
81  *    notice, this list of conditions and the following disclaimer in the
82  *    documentation and/or other materials provided with the distribution.
83  * 3. All advertising materials mentioning features or use of this software
84  *    must display the following acknowledgement:
85  *      This product includes software developed by the University of
86  *      California, Berkeley and its contributors.
87  * 4. Neither the name of the University nor the names of its contributors
88  *    may be used to endorse or promote products derived from this software
89  *    without specific prior written permission.
90  *
91  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
92  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
94  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
95  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
96  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
97  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
98  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
99  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
100  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
101  * SUCH DAMAGE.
102  *
103  *      @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
104  */
105 
106 /*
107  * IP multicast forwarding procedures
108  *
109  * Written by David Waitzman, BBN Labs, August 1988.
110  * Modified by Steve Deering, Stanford, February 1989.
111  * Modified by Mark J. Steiglitz, Stanford, May, 1991
112  * Modified by Van Jacobson, LBL, January 1993
113  * Modified by Ajit Thyagarajan, PARC, August 1993
114  * Modified by Bill Fenner, PARC, April 1994
115  *
116  * MROUTING Revision: 3.5.1.2 + PIM-SMv2 (pimd) Support
117  */
118 
119 #include <sys/cdefs.h>
120 __KERNEL_RCSID(0, "$NetBSD: ip6_mroute.c,v 1.129 2018/06/21 10:37:50 knakahara Exp $");
121 
122 #ifdef _KERNEL_OPT
123 #include "opt_inet.h"
124 #include "opt_mrouting.h"
125 #endif
126 
127 #include <sys/param.h>
128 #include <sys/systm.h>
129 #include <sys/callout.h>
130 #include <sys/mbuf.h>
131 #include <sys/socket.h>
132 #include <sys/socketvar.h>
133 #include <sys/sockio.h>
134 #include <sys/errno.h>
135 #include <sys/time.h>
136 #include <sys/kernel.h>
137 #include <sys/ioctl.h>
138 #include <sys/sysctl.h>
139 #include <sys/syslog.h>
140 
141 #include <net/if.h>
142 #include <net/route.h>
143 #include <net/raw_cb.h>
144 #include <net/net_stats.h>
145 
146 #include <netinet/in.h>
147 #include <netinet/in_var.h>
148 #include <netinet/icmp6.h>
149 
150 #include <netinet/ip6.h>
151 #include <netinet6/ip6_var.h>
152 #include <netinet6/ip6_private.h>
153 #include <netinet6/ip6_mroute.h>
154 #include <netinet6/scope6_var.h>
155 #include <netinet6/pim6.h>
156 #include <netinet6/pim6_var.h>
157 #include <netinet6/nd6.h>
158 
159 static int ip6_mdq(struct mbuf *, struct ifnet *, struct mf6c *);
160 static void phyint_send(struct ip6_hdr *, struct mif6 *, struct mbuf *);
161 
162 static int set_pim6(int *);
163 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in6 *);
164 static int register_send(struct ip6_hdr *, struct mif6 *, struct mbuf *);
165 
166 /*
167  * Globals.  All but ip6_mrouter, ip6_mrtproto and mrt6stat could be static,
168  * except for netstat or debugging purposes.
169  */
170 struct socket  *ip6_mrouter = NULL;
171 int		ip6_mrouter_ver = 0;
172 int		ip6_mrtproto = IPPROTO_PIM;    /* for netstat only */
173 struct mrt6stat	mrt6stat;
174 
175 #define NO_RTE_FOUND 	0x1
176 #define RTE_FOUND	0x2
177 
178 struct mf6c	*mf6ctable[MF6CTBLSIZ];
179 u_char		n6expire[MF6CTBLSIZ];
180 struct mif6 mif6table[MAXMIFS];
181 #ifdef MRT6DEBUG
182 u_int		mrt6debug = 0;	  /* debug level 	*/
183 #define DEBUG_MFC	0x02
184 #define DEBUG_FORWARD	0x04
185 #define DEBUG_EXPIRE	0x08
186 #define DEBUG_XMIT	0x10
187 #define DEBUG_REG	0x20
188 #define DEBUG_PIM	0x40
189 #define __mrt6debugused     /* empty */
190 #else
191 #define __mrt6debugused     __unused
192 #endif
193 
194 static void	expire_upcalls(void *);
195 #define	EXPIRE_TIMEOUT	(hz / 4)	/* 4x / second */
196 #define	UPCALL_EXPIRE	6		/* number of timeouts */
197 
198 #ifdef INET
199 #ifdef MROUTING
200 extern struct socket *ip_mrouter;
201 #endif
202 #endif
203 
204 /*
205  * 'Interfaces' associated with decapsulator (so we can tell
206  * packets that went through it from ones that get reflected
207  * by a broken gateway).  These interfaces are never linked into
208  * the system ifnet list & no routes point to them.  I.e., packets
209  * can't be sent this way.  They only exist as a placeholder for
210  * multicast source verification.
211  */
212 struct ifnet multicast_register_if6;
213 
214 #define ENCAP_HOPS 64
215 
216 /*
217  * Private variables.
218  */
219 static mifi_t nummifs = 0;
220 static mifi_t reg_mif_num = (mifi_t)-1;
221 
222 static percpu_t *pim6stat_percpu;
223 
224 #define	PIM6_STATINC(x)		_NET_STATINC(pim6stat_percpu, x)
225 
226 static int pim6;
227 
228 /*
229  * Hash function for a source, group entry
230  */
231 #define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
232 				   (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
233 				   (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
234 				   (g).s6_addr32[2] ^ (g).s6_addr32[3])
235 
236 /*
237  * Find a route for a given origin IPv6 address and Multicast group address.
238  * Quality of service parameter to be added in the future!!!
239  */
240 
241 #define MF6CFIND(o, g, rt) do { \
242 	struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
243 	rt = NULL; \
244 	mrt6stat.mrt6s_mfc_lookups++; \
245 	while (_rt) { \
246 		if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
247 		    IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
248 		    (_rt->mf6c_stall == NULL)) { \
249 			rt = _rt; \
250 			break; \
251 		} \
252 		_rt = _rt->mf6c_next; \
253 	} \
254 	if (rt == NULL) { \
255 		mrt6stat.mrt6s_mfc_misses++; \
256 	} \
257 } while (/*CONSTCOND*/ 0)
258 
259 /*
260  * Macros to compute elapsed time efficiently
261  * Borrowed from Van Jacobson's scheduling code
262  */
263 #define TV_DELTA(a, b, delta) do { \
264 	    int xxs; \
265 		\
266 	    delta = (a).tv_usec - (b).tv_usec; \
267 	    if ((xxs = (a).tv_sec - (b).tv_sec)) { \
268 	       switch (xxs) { \
269 		      case 2: \
270 			  delta += 1000000; \
271 			      /* FALLTHROUGH */ \
272 		      case 1: \
273 			  delta += 1000000; \
274 			  break; \
275 		      default: \
276 			  delta += (1000000 * xxs); \
277 	       } \
278 	    } \
279 } while (/*CONSTCOND*/ 0)
280 
281 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
282 	      (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
283 
284 #ifdef UPCALL_TIMING
285 #define UPCALL_MAX	50
286 u_long upcall_data[UPCALL_MAX + 1];
287 static void collate();
288 #endif /* UPCALL_TIMING */
289 
290 static int get_sg_cnt(struct sioc_sg_req6 *);
291 static int get_mif6_cnt(struct sioc_mif_req6 *);
292 static int ip6_mrouter_init(struct socket *, int, int);
293 static int add_m6if(struct mif6ctl *);
294 static int del_m6if(mifi_t *);
295 static int add_m6fc(struct mf6cctl *);
296 static int del_m6fc(struct mf6cctl *);
297 static void sysctl_net_inet6_pim6_setup(struct sysctllog **);
298 
299 static callout_t expire_upcalls_ch;
300 
301 void
302 pim6_init(void)
303 {
304 
305 	sysctl_net_inet6_pim6_setup(NULL);
306 	pim6stat_percpu = percpu_alloc(sizeof(uint64_t) * PIM6_NSTATS);
307 }
308 
309 /*
310  * Handle MRT setsockopt commands to modify the multicast routing tables.
311  */
312 int
313 ip6_mrouter_set(struct socket *so, struct sockopt *sopt)
314 {
315 	int error, optval;
316 	struct mif6ctl mifc;
317 	struct mf6cctl mfcc;
318 	mifi_t mifi;
319 
320 	if (sopt->sopt_name != MRT6_INIT && so != ip6_mrouter)
321 		return (EACCES);
322 
323 	error = 0;
324 
325 	switch (sopt->sopt_name) {
326 #ifdef MRT6_OINIT
327 	case MRT6_OINIT:
328 #endif
329 	case MRT6_INIT:
330 		error = sockopt_getint(sopt, &optval);
331 		if (error)
332 			break;
333 		return (ip6_mrouter_init(so, optval, sopt->sopt_name));
334 	case MRT6_DONE:
335 		return (ip6_mrouter_done());
336 	case MRT6_ADD_MIF:
337 		error = sockopt_get(sopt, &mifc, sizeof(mifc));
338 		if (error)
339 			break;
340 		return (add_m6if(&mifc));
341 	case MRT6_DEL_MIF:
342 		error = sockopt_get(sopt, &mifi, sizeof(mifi));
343 		if (error)
344 			break;
345 		return (del_m6if(&mifi));
346 	case MRT6_ADD_MFC:
347 		error = sockopt_get(sopt, &mfcc, sizeof(mfcc));
348 		if (error)
349 			break;
350 		return (add_m6fc(&mfcc));
351 	case MRT6_DEL_MFC:
352 		error = sockopt_get(sopt, &mfcc, sizeof(mfcc));
353 		if (error)
354 			break;
355 		return (del_m6fc(&mfcc));
356 	case MRT6_PIM:
357 		error = sockopt_getint(sopt, &optval);
358 		if (error)
359 			break;
360 		return (set_pim6(&optval));
361 	default:
362 		error = EOPNOTSUPP;
363 	}
364 
365 	return (error);
366 }
367 
368 /*
369  * Handle MRT getsockopt commands
370  */
371 int
372 ip6_mrouter_get(struct socket *so, struct sockopt *sopt)
373 {
374 	int error;
375 
376 	if (so != ip6_mrouter)
377 		return EACCES;
378 
379 	error = 0;
380 
381 	switch (sopt->sopt_name) {
382 	case MRT6_PIM:
383 		error = sockopt_set(sopt, &pim6, sizeof(pim6));
384 		break;
385 	default:
386 		error = EOPNOTSUPP;
387 		break;
388 	}
389 
390 	return (error);
391 }
392 
393 /*
394  * Handle ioctl commands to obtain information from the cache
395  */
396 int
397 mrt6_ioctl(u_long cmd, void *data)
398 {
399 
400 	switch (cmd) {
401 	case SIOCGETSGCNT_IN6:
402 		return (get_sg_cnt((struct sioc_sg_req6 *)data));
403 	case SIOCGETMIFCNT_IN6:
404 		return (get_mif6_cnt((struct sioc_mif_req6 *)data));
405 	default:
406 		return (EINVAL);
407 	}
408 }
409 
410 /*
411  * returns the packet, byte, rpf-failure count for the source group provided
412  */
413 static int
414 get_sg_cnt(struct sioc_sg_req6 *req)
415 {
416 	struct mf6c *rt;
417 	int s;
418 
419 	s = splsoftnet();
420 	MF6CFIND(req->src.sin6_addr, req->grp.sin6_addr, rt);
421 	splx(s);
422 	if (rt != NULL) {
423 		req->pktcnt = rt->mf6c_pkt_cnt;
424 		req->bytecnt = rt->mf6c_byte_cnt;
425 		req->wrong_if = rt->mf6c_wrong_if;
426 	} else
427 		return (ESRCH);
428 #if 0
429 		req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
430 #endif
431 
432 	return 0;
433 }
434 
435 /*
436  * returns the input and output packet and byte counts on the mif provided
437  */
438 static int
439 get_mif6_cnt(struct sioc_mif_req6 *req)
440 {
441 	mifi_t mifi = req->mifi;
442 
443 	if (mifi >= nummifs)
444 		return EINVAL;
445 
446 	req->icount = mif6table[mifi].m6_pkt_in;
447 	req->ocount = mif6table[mifi].m6_pkt_out;
448 	req->ibytes = mif6table[mifi].m6_bytes_in;
449 	req->obytes = mif6table[mifi].m6_bytes_out;
450 
451 	return 0;
452 }
453 
454 static int
455 set_pim6(int *i)
456 {
457 	if ((*i != 1) && (*i != 0))
458 		return EINVAL;
459 
460 	pim6 = *i;
461 
462 	return 0;
463 }
464 
465 /*
466  * Enable multicast routing
467  */
468 static int
469 ip6_mrouter_init(struct socket *so, int v, int cmd)
470 {
471 #ifdef MRT6DEBUG
472 	if (mrt6debug)
473 		log(LOG_DEBUG,
474 		    "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n",
475 		    so->so_type, so->so_proto->pr_protocol);
476 #endif
477 
478 	if (so->so_type != SOCK_RAW ||
479 	    so->so_proto->pr_protocol != IPPROTO_ICMPV6)
480 		return EOPNOTSUPP;
481 
482 	if (v != 1)
483 		return ENOPROTOOPT;
484 
485 	if (ip6_mrouter != NULL)
486 		return EADDRINUSE;
487 
488 	ip6_mrouter = so;
489 	ip6_mrouter_ver = cmd;
490 
491 	memset((void *)mf6ctable, 0, sizeof(mf6ctable));
492 	memset((void *)n6expire, 0, sizeof(n6expire));
493 
494 	pim6 = 0;/* used for stubbing out/in pim stuff */
495 
496 	callout_init(&expire_upcalls_ch, CALLOUT_MPSAFE);
497 	callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
498 	    expire_upcalls, NULL);
499 
500 #ifdef MRT6DEBUG
501 	if (mrt6debug)
502 		log(LOG_DEBUG, "ip6_mrouter_init\n");
503 #endif
504 
505 	return 0;
506 }
507 
508 /*
509  * Disable multicast routing
510  */
511 int
512 ip6_mrouter_done(void)
513 {
514 	mifi_t mifi;
515 	int i;
516 	struct ifnet *ifp;
517 	struct sockaddr_in6 sin6;
518 	struct mf6c *rt;
519 	struct rtdetq *rte;
520 	int s;
521 
522 	s = splsoftnet();
523 
524 	/*
525 	 * For each phyint in use, disable promiscuous reception of all IPv6
526 	 * multicasts.
527 	 */
528 #ifdef INET
529 #ifdef MROUTING
530 	/*
531 	 * If there is still IPv4 multicast routing daemon,
532 	 * we remain interfaces to receive all muliticasted packets.
533 	 * XXX: there may be an interface in which the IPv4 multicast
534 	 * daemon is not interested...
535 	 */
536 	if (!ip_mrouter)
537 #endif
538 #endif
539 	{
540 		for (mifi = 0; mifi < nummifs; mifi++) {
541 			if (mif6table[mifi].m6_ifp &&
542 			    !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
543 				sin6.sin6_family = AF_INET6;
544 				sin6.sin6_addr = in6addr_any;
545 				ifp = mif6table[mifi].m6_ifp;
546 				if_mcast_op(ifp, SIOCDELMULTI,
547 				    sin6tocsa(&sin6));
548 			}
549 		}
550 	}
551 
552 	memset((void *)mif6table, 0, sizeof(mif6table));
553 	nummifs = 0;
554 
555 	pim6 = 0; /* used to stub out/in pim specific code */
556 
557 	callout_stop(&expire_upcalls_ch);
558 
559 	/*
560 	 * Free all multicast forwarding cache entries.
561 	 */
562 	for (i = 0; i < MF6CTBLSIZ; i++) {
563 		rt = mf6ctable[i];
564 		while (rt) {
565 			struct mf6c *frt;
566 
567 			for (rte = rt->mf6c_stall; rte != NULL; ) {
568 				struct rtdetq *n = rte->next;
569 
570 				m_freem(rte->m);
571 				free(rte, M_MRTABLE);
572 				rte = n;
573 			}
574 			frt = rt;
575 			rt = rt->mf6c_next;
576 			free(frt, M_MRTABLE);
577 		}
578 	}
579 
580 	memset((void *)mf6ctable, 0, sizeof(mf6ctable));
581 
582 	/*
583 	 * Reset register interface
584 	 */
585 	if (reg_mif_num != (mifi_t)-1) {
586 		if_detach(&multicast_register_if6);
587 		reg_mif_num = (mifi_t)-1;
588 	}
589 
590 	ip6_mrouter = NULL;
591 	ip6_mrouter_ver = 0;
592 
593 	splx(s);
594 
595 #ifdef MRT6DEBUG
596 	if (mrt6debug)
597 		log(LOG_DEBUG, "ip6_mrouter_done\n");
598 #endif
599 
600 	return 0;
601 }
602 
603 void
604 ip6_mrouter_detach(struct ifnet *ifp)
605 {
606 	struct rtdetq *rte;
607 	struct mf6c *mfc;
608 	mifi_t mifi;
609 	int i;
610 
611 	if (ip6_mrouter == NULL)
612 		return;
613 
614 	/*
615 	 * Delete a mif which points to ifp.
616 	 */
617 	for (mifi = 0; mifi < nummifs; mifi++)
618 		if (mif6table[mifi].m6_ifp == ifp)
619 			del_m6if(&mifi);
620 
621 	/*
622 	 * Clear rte->ifp of cache entries received on ifp.
623 	 */
624 	for (i = 0; i < MF6CTBLSIZ; i++) {
625 		if (n6expire[i] == 0)
626 			continue;
627 
628 		for (mfc = mf6ctable[i]; mfc != NULL; mfc = mfc->mf6c_next) {
629 			for (rte = mfc->mf6c_stall; rte != NULL; rte = rte->next) {
630 				if (rte->ifp == ifp)
631 					rte->ifp = NULL;
632 			}
633 		}
634 	}
635 }
636 
637 /*
638  * Add a mif to the mif table
639  */
640 static int
641 add_m6if(struct mif6ctl *mifcp)
642 {
643 	struct mif6 *mifp;
644 	struct ifnet *ifp;
645 	struct sockaddr_in6 sin6;
646 	int error, s;
647 
648 	if (mifcp->mif6c_mifi >= MAXMIFS)
649 		return EINVAL;
650 	mifp = mif6table + mifcp->mif6c_mifi;
651 	if (mifp->m6_ifp)
652 		return EADDRINUSE; /* XXX: is it appropriate? */
653 	if (!mifcp->mif6c_pifi || (ifp = if_byindex(mifcp->mif6c_pifi)) == NULL)
654 		return ENXIO;
655 
656 	if (mifcp->mif6c_flags & MIFF_REGISTER) {
657 		ifp = &multicast_register_if6;
658 
659 		if (reg_mif_num == (mifi_t)-1) {
660 			strlcpy(ifp->if_xname, "register_mif",
661 			    sizeof(ifp->if_xname));
662 			ifp->if_flags |= IFF_LOOPBACK;
663 			ifp->if_index = mifcp->mif6c_mifi;
664 			reg_mif_num = mifcp->mif6c_mifi;
665 			if_attach(ifp);
666 		}
667 	} else {
668 		/* Make sure the interface supports multicast */
669 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
670 			return EOPNOTSUPP;
671 
672 		s = splsoftnet();
673 		/*
674 		 * Enable promiscuous reception of all IPv6 multicasts
675 		 * from the interface.
676 		 */
677 		sin6.sin6_family = AF_INET6;
678 		sin6.sin6_addr = in6addr_any;
679 		error = if_mcast_op(ifp, SIOCADDMULTI, sin6tosa(&sin6));
680 		splx(s);
681 		if (error)
682 			return error;
683 	}
684 
685 	s = splsoftnet();
686 	mifp->m6_flags     = mifcp->mif6c_flags;
687 	mifp->m6_ifp       = ifp;
688 	/* initialize per mif pkt counters */
689 	mifp->m6_pkt_in    = 0;
690 	mifp->m6_pkt_out   = 0;
691 	mifp->m6_bytes_in  = 0;
692 	mifp->m6_bytes_out = 0;
693 	splx(s);
694 
695 	/* Adjust nummifs up if the mifi is higher than nummifs */
696 	if (nummifs <= mifcp->mif6c_mifi)
697 		nummifs = mifcp->mif6c_mifi + 1;
698 
699 #ifdef MRT6DEBUG
700 	if (mrt6debug)
701 		log(LOG_DEBUG,
702 		    "add_mif #%d, phyint %s\n",
703 		    mifcp->mif6c_mifi, ifp->if_xname);
704 #endif
705 
706 	return 0;
707 }
708 
709 /*
710  * Delete a mif from the mif table
711  */
712 static int
713 del_m6if(mifi_t *mifip)
714 {
715 	struct mif6 *mifp = mif6table + *mifip;
716 	mifi_t mifi;
717 	struct ifnet *ifp;
718 	struct sockaddr_in6 sin6;
719 	int s;
720 
721 	if (*mifip >= nummifs)
722 		return EINVAL;
723 	if (mifp->m6_ifp == NULL)
724 		return EINVAL;
725 
726 	s = splsoftnet();
727 
728 	if (!(mifp->m6_flags & MIFF_REGISTER)) {
729 		/*
730 		 * XXX: what if there is yet IPv4 multicast daemon
731 		 *      using the interface?
732 		 */
733 		ifp = mifp->m6_ifp;
734 
735 		sin6.sin6_family = AF_INET6;
736 		sin6.sin6_addr = in6addr_any;
737 		if_mcast_op(ifp, SIOCDELMULTI, sin6tosa(&sin6));
738 	} else {
739 		if (reg_mif_num != (mifi_t)-1) {
740 			if_detach(&multicast_register_if6);
741 			reg_mif_num = (mifi_t)-1;
742 		}
743 	}
744 
745 	memset((void *)mifp, 0, sizeof (*mifp));
746 
747 	/* Adjust nummifs down */
748 	for (mifi = nummifs; mifi > 0; mifi--)
749 		if (mif6table[mifi - 1].m6_ifp)
750 			break;
751 	nummifs = mifi;
752 
753 	splx(s);
754 
755 #ifdef MRT6DEBUG
756 	if (mrt6debug)
757 		log(LOG_DEBUG, "del_m6if %d, nummifs %d\n", *mifip, nummifs);
758 #endif
759 
760 	return 0;
761 }
762 
763 /*
764  * Add an mfc entry
765  */
766 static int
767 add_m6fc(struct mf6cctl *mfccp)
768 {
769 	struct mf6c *rt;
770 	u_long hash;
771 	struct rtdetq *rte;
772 	u_short nstl;
773 	int s;
774 	char ip6bufo[INET6_ADDRSTRLEN], ip6bufm[INET6_ADDRSTRLEN];
775 
776 	MF6CFIND(mfccp->mf6cc_origin.sin6_addr,
777 		 mfccp->mf6cc_mcastgrp.sin6_addr, rt);
778 
779 	/* If an entry already exists, just update the fields */
780 	if (rt) {
781 #ifdef MRT6DEBUG
782 		if (mrt6debug & DEBUG_MFC)
783 			log(LOG_DEBUG,"add_m6fc update o %s g %s p %x\n",
784 			    IN6_PRINT(ip6bufo,
785 			    &mfccp->mf6cc_origin.sin6_addr),
786 			    IN6_PRINT(ip6bufm,
787 			    &mfccp->mf6cc_mcastgrp.sin6_addr),
788 			    mfccp->mf6cc_parent);
789 #endif
790 
791 		s = splsoftnet();
792 		rt->mf6c_parent = mfccp->mf6cc_parent;
793 		rt->mf6c_ifset = mfccp->mf6cc_ifset;
794 		splx(s);
795 		return 0;
796 	}
797 
798 	/*
799 	 * Find the entry for which the upcall was made and update
800 	 */
801 	s = splsoftnet();
802 	hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr,
803 			mfccp->mf6cc_mcastgrp.sin6_addr);
804 	for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) {
805 		if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
806 				       &mfccp->mf6cc_origin.sin6_addr) &&
807 		    IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
808 				       &mfccp->mf6cc_mcastgrp.sin6_addr) &&
809 		    (rt->mf6c_stall != NULL)) {
810 
811 			if (nstl++)
812 				log(LOG_ERR,
813 				    "add_m6fc: %s o %s g %s p %x dbx %p\n",
814 				    "multiple kernel entries",
815 				    IN6_PRINT(ip6bufo,
816 				    &mfccp->mf6cc_origin.sin6_addr),
817 				    IN6_PRINT(ip6bufm,
818 				    &mfccp->mf6cc_mcastgrp.sin6_addr),
819 				    mfccp->mf6cc_parent, rt->mf6c_stall);
820 
821 #ifdef MRT6DEBUG
822 			if (mrt6debug & DEBUG_MFC)
823 				log(LOG_DEBUG,
824 				    "add_m6fc o %s g %s p %x dbg %p\n",
825 				    IN6_PRINT(ip6bufo,
826 				    &mfccp->mf6cc_origin.sin6_addr),
827 				    IN6_PRINT(ip6bufm,
828 				    &mfccp->mf6cc_mcastgrp.sin6_addr),
829 				    mfccp->mf6cc_parent, rt->mf6c_stall);
830 #endif
831 
832 			rt->mf6c_origin     = mfccp->mf6cc_origin;
833 			rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
834 			rt->mf6c_parent     = mfccp->mf6cc_parent;
835 			rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
836 			/* initialize pkt counters per src-grp */
837 			rt->mf6c_pkt_cnt    = 0;
838 			rt->mf6c_byte_cnt   = 0;
839 			rt->mf6c_wrong_if   = 0;
840 
841 			rt->mf6c_expire = 0;	/* Don't clean this guy up */
842 			n6expire[hash]--;
843 
844 			/* free packets Qed at the end of this entry */
845 			for (rte = rt->mf6c_stall; rte != NULL; ) {
846 				struct rtdetq *n = rte->next;
847 				if (rte->ifp) {
848 					ip6_mdq(rte->m, rte->ifp, rt);
849 				}
850 				m_freem(rte->m);
851 #ifdef UPCALL_TIMING
852 				collate(&(rte->t));
853 #endif
854 				free(rte, M_MRTABLE);
855 				rte = n;
856 			}
857 			rt->mf6c_stall = NULL;
858 		}
859 	}
860 
861 	/*
862 	 * It is possible that an entry is being inserted without an upcall
863 	 */
864 	if (nstl == 0) {
865 #ifdef MRT6DEBUG
866 		if (mrt6debug & DEBUG_MFC)
867 			log(LOG_DEBUG,
868 			    "add_mfc no upcall h %ld o %s g %s p %x\n",
869 			    hash,
870 			    IN6_PRINT(ip6bufo,
871 			    &mfccp->mf6cc_origin.sin6_addr),
872 			    IN6_PRINT(ip6bufm,
873 			    &mfccp->mf6cc_mcastgrp.sin6_addr),
874 			    mfccp->mf6cc_parent);
875 #endif
876 
877 		for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
878 
879 			if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
880 					       &mfccp->mf6cc_origin.sin6_addr)&&
881 			    IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
882 					       &mfccp->mf6cc_mcastgrp.sin6_addr)) {
883 
884 				rt->mf6c_origin     = mfccp->mf6cc_origin;
885 				rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
886 				rt->mf6c_parent     = mfccp->mf6cc_parent;
887 				rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
888 				/* initialize pkt counters per src-grp */
889 				rt->mf6c_pkt_cnt    = 0;
890 				rt->mf6c_byte_cnt   = 0;
891 				rt->mf6c_wrong_if   = 0;
892 
893 				if (rt->mf6c_expire)
894 					n6expire[hash]--;
895 				rt->mf6c_expire	   = 0;
896 			}
897 		}
898 		if (rt == NULL) {
899 			/* no upcall, so make a new entry */
900 			rt = malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
901 			if (rt == NULL) {
902 				splx(s);
903 				return ENOBUFS;
904 			}
905 
906 			/* insert new entry at head of hash chain */
907 			rt->mf6c_origin     = mfccp->mf6cc_origin;
908 			rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
909 			rt->mf6c_parent     = mfccp->mf6cc_parent;
910 			rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
911 			/* initialize pkt counters per src-grp */
912 			rt->mf6c_pkt_cnt    = 0;
913 			rt->mf6c_byte_cnt   = 0;
914 			rt->mf6c_wrong_if   = 0;
915 			rt->mf6c_expire     = 0;
916 			rt->mf6c_stall = NULL;
917 
918 			/* link into table */
919 			rt->mf6c_next  = mf6ctable[hash];
920 			mf6ctable[hash] = rt;
921 		}
922 	}
923 	splx(s);
924 	return 0;
925 }
926 
927 #ifdef UPCALL_TIMING
928 /*
929  * collect delay statistics on the upcalls
930  */
931 static void
932 collate(struct timeval *t)
933 {
934 	u_long d;
935 	struct timeval tp;
936 	u_long delta;
937 
938 	GET_TIME(tp);
939 
940 	if (TV_LT(*t, tp))
941 	{
942 		TV_DELTA(tp, *t, delta);
943 
944 		d = delta >> 10;
945 		if (d > UPCALL_MAX)
946 			d = UPCALL_MAX;
947 
948 		++upcall_data[d];
949 	}
950 }
951 #endif /* UPCALL_TIMING */
952 
953 /*
954  * Delete an mfc entry
955  */
956 static int
957 del_m6fc(struct mf6cctl *mfccp)
958 {
959 	struct sockaddr_in6 	origin;
960 	struct sockaddr_in6 	mcastgrp;
961 	struct mf6c 		*rt;
962 	struct mf6c	 	**nptr;
963 	u_long 		hash;
964 	int s;
965 
966 	origin = mfccp->mf6cc_origin;
967 	mcastgrp = mfccp->mf6cc_mcastgrp;
968 	hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr);
969 
970 #ifdef MRT6DEBUG
971 	if (mrt6debug & DEBUG_MFC) {
972 		char ip6bufo[INET6_ADDRSTRLEN], ip6bufm[INET6_ADDRSTRLEN];
973 		log(LOG_DEBUG,"del_m6fc orig %s mcastgrp %s\n",
974 		    IN6_PRINT(ip6bufo, &origin.sin6_addr),
975 		    IN6_PRINT(ip6bufm, &mcastgrp.sin6_addr));
976 	}
977 #endif
978 
979 	s = splsoftnet();
980 
981 	nptr = &mf6ctable[hash];
982 	while ((rt = *nptr) != NULL) {
983 		if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr,
984 				       &rt->mf6c_origin.sin6_addr) &&
985 		    IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr,
986 				       &rt->mf6c_mcastgrp.sin6_addr) &&
987 		    rt->mf6c_stall == NULL)
988 			break;
989 
990 		nptr = &rt->mf6c_next;
991 	}
992 	if (rt == NULL) {
993 		splx(s);
994 		return EADDRNOTAVAIL;
995 	}
996 
997 	*nptr = rt->mf6c_next;
998 	free(rt, M_MRTABLE);
999 
1000 	splx(s);
1001 
1002 	return 0;
1003 }
1004 
1005 static int
1006 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in6 *src)
1007 {
1008 	if (s) {
1009 		if (sbappendaddr(&s->so_rcv, sin6tosa(src), mm, NULL) != 0) {
1010 			sorwakeup(s);
1011 			return 0;
1012 		}
1013 		soroverflow(s);
1014 	}
1015 	m_freem(mm);
1016 	return -1;
1017 }
1018 
1019 /*
1020  * IPv6 multicast forwarding function. This function assumes that the packet
1021  * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
1022  * pointed to by "ifp", and the packet is to be relayed to other networks
1023  * that have members of the packet's destination IPv6 multicast group.
1024  *
1025  * The packet is returned unscathed to the caller, unless it is
1026  * erroneous, in which case a non-zero return value tells the caller to
1027  * discard it.
1028  */
1029 int
1030 ip6_mforward(struct ip6_hdr *ip6, struct ifnet *ifp, struct mbuf *m)
1031 {
1032 	struct mf6c *rt;
1033 	struct mif6 *mifp;
1034 	struct mbuf *mm;
1035 	int s;
1036 	mifi_t mifi;
1037 	struct sockaddr_in6 sin6;
1038 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1039 
1040 #ifdef MRT6DEBUG
1041 	if (mrt6debug & DEBUG_FORWARD)
1042 		log(LOG_DEBUG, "ip6_mforward: src %s, dst %s, ifindex %d\n",
1043 		    IN6_PRINT(ip6bufs, &ip6->ip6_src),
1044 		    IN6_PRINT(ip6bufd, &ip6->ip6_dst),
1045 		    ifp->if_index);
1046 #endif
1047 
1048 	/*
1049 	 * Don't forward a packet with Hop limit of zero or one,
1050 	 * or a packet destined to a local-only group.
1051 	 */
1052 	if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst) ||
1053 	    IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1054 		return 0;
1055 	ip6->ip6_hlim--;
1056 
1057 	/*
1058 	 * Source address check: do not forward packets with unspecified
1059 	 * source. It was discussed in July 2000, on ipngwg mailing list.
1060 	 * This is rather more serious than unicast cases, because some
1061 	 * MLD packets can be sent with the unspecified source address
1062 	 * (although such packets must normally set the hop limit field to 1).
1063 	 */
1064 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
1065 		IP6_STATINC(IP6_STAT_CANTFORWARD);
1066 		if (ip6_log_time + ip6_log_interval < time_uptime) {
1067 			ip6_log_time = time_uptime;
1068 			log(LOG_DEBUG,
1069 			    "cannot forward "
1070 			    "from %s to %s nxt %d received on %s\n",
1071 			    IN6_PRINT(ip6bufs, &ip6->ip6_src),
1072 			    IN6_PRINT(ip6bufd, &ip6->ip6_dst),
1073 			    ip6->ip6_nxt,
1074 			    m->m_pkthdr.rcvif_index ?
1075 			    if_name(m_get_rcvif_NOMPSAFE(m)) : "?");
1076 		}
1077 		return 0;
1078 	}
1079 
1080 	/*
1081 	 * Determine forwarding mifs from the forwarding cache table
1082 	 */
1083 	s = splsoftnet();
1084 	MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
1085 
1086 	/* Entry exists, so forward if necessary */
1087 	if (rt) {
1088 		splx(s);
1089 		return ip6_mdq(m, ifp, rt);
1090 	} else {
1091 		/*
1092 		 * If we don't have a route for packet's origin, make a copy
1093 		 * of the packet and send message to routing daemon.
1094 		 */
1095 
1096 		struct mbuf *mb0;
1097 		struct rtdetq *rte;
1098 		u_long hash;
1099 
1100 #ifdef UPCALL_TIMING
1101 		struct timeval tp;
1102 		GET_TIME(tp);
1103 #endif
1104 
1105 		mrt6stat.mrt6s_no_route++;
1106 #ifdef MRT6DEBUG
1107 		if (mrt6debug & (DEBUG_FORWARD | DEBUG_MFC))
1108 			log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n",
1109 			    IN6_PRINT(ip6bufs, &ip6->ip6_src),
1110 			    IN6_PRINT(ip6bufd, &ip6->ip6_dst));
1111 #endif
1112 
1113 		/*
1114 		 * Allocate mbufs early so that we don't do extra work if we
1115 		 * are just going to fail anyway.
1116 		 */
1117 		rte = malloc(sizeof(*rte), M_MRTABLE, M_NOWAIT);
1118 		if (rte == NULL) {
1119 			splx(s);
1120 			return ENOBUFS;
1121 		}
1122 		mb0 = m_copypacket(m, M_DONTWAIT);
1123 
1124 		/*
1125 		 * Pullup packet header if needed before storing it,
1126 		 * as other references may modify it in the meantime.
1127 		 */
1128 		if (mb0 && M_UNWRITABLE(mb0, sizeof(struct ip6_hdr)))
1129 			mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
1130 		if (mb0 == NULL) {
1131 			free(rte, M_MRTABLE);
1132 			splx(s);
1133 			return ENOBUFS;
1134 		}
1135 
1136 		/* is there an upcall waiting for this packet? */
1137 		hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
1138 		for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
1139 			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
1140 					       &rt->mf6c_origin.sin6_addr) &&
1141 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1142 					       &rt->mf6c_mcastgrp.sin6_addr) &&
1143 			    (rt->mf6c_stall != NULL))
1144 				break;
1145 		}
1146 
1147 		if (rt == NULL) {
1148 			struct mrt6msg *im;
1149 			struct omrt6msg *oim;
1150 
1151 			/* no upcall, so make a new entry */
1152 			rt = malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1153 			if (rt == NULL) {
1154 				free(rte, M_MRTABLE);
1155 				m_freem(mb0);
1156 				splx(s);
1157 				return ENOBUFS;
1158 			}
1159 
1160 			/*
1161 			 * Make a copy of the header to send to the user
1162 			 * level process
1163 			 */
1164 			mm = m_copym(mb0, 0, sizeof(struct ip6_hdr), M_DONTWAIT);
1165 
1166 			if (mm == NULL) {
1167 				free(rte, M_MRTABLE);
1168 				m_freem(mb0);
1169 				free(rt, M_MRTABLE);
1170 				splx(s);
1171 				return ENOBUFS;
1172 			}
1173 
1174 			/*
1175 			 * Send message to routing daemon
1176 			 */
1177 			sockaddr_in6_init(&sin6, &ip6->ip6_src, 0, 0, 0);
1178 
1179 			im = NULL;
1180 			oim = NULL;
1181 			switch (ip6_mrouter_ver) {
1182 			case MRT6_OINIT:
1183 				oim = mtod(mm, struct omrt6msg *);
1184 				oim->im6_msgtype = MRT6MSG_NOCACHE;
1185 				oim->im6_mbz = 0;
1186 				break;
1187 			case MRT6_INIT:
1188 				im = mtod(mm, struct mrt6msg *);
1189 				im->im6_msgtype = MRT6MSG_NOCACHE;
1190 				im->im6_mbz = 0;
1191 				break;
1192 			default:
1193 				free(rte, M_MRTABLE);
1194 				m_freem(mb0);
1195 				free(rt, M_MRTABLE);
1196 				splx(s);
1197 				return EINVAL;
1198 			}
1199 
1200 #ifdef MRT6DEBUG
1201 			if (mrt6debug & DEBUG_FORWARD)
1202 				log(LOG_DEBUG,
1203 				    "getting the iif info in the kernel\n");
1204 #endif
1205 
1206 			for (mifp = mif6table, mifi = 0;
1207 			     mifi < nummifs && mifp->m6_ifp != ifp;
1208 			     mifp++, mifi++)
1209 				;
1210 
1211 			switch (ip6_mrouter_ver) {
1212 			case MRT6_OINIT:
1213 				oim->im6_mif = mifi;
1214 				break;
1215 			case MRT6_INIT:
1216 				im->im6_mif = mifi;
1217 				break;
1218 			}
1219 
1220 			if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1221 				log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
1222 				    "socket queue full\n");
1223 				mrt6stat.mrt6s_upq_sockfull++;
1224 				free(rte, M_MRTABLE);
1225 				m_freem(mb0);
1226 				free(rt, M_MRTABLE);
1227 				splx(s);
1228 				return ENOBUFS;
1229 			}
1230 
1231 			mrt6stat.mrt6s_upcalls++;
1232 
1233 			/* insert new entry at head of hash chain */
1234 			memset(rt, 0, sizeof(*rt));
1235 			sockaddr_in6_init(&rt->mf6c_origin, &ip6->ip6_src,
1236 			    0, 0, 0);
1237 			sockaddr_in6_init(&rt->mf6c_mcastgrp, &ip6->ip6_dst,
1238 			    0, 0, 0);
1239 			rt->mf6c_expire = UPCALL_EXPIRE;
1240 			n6expire[hash]++;
1241 			rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
1242 
1243 			/* link into table */
1244 			rt->mf6c_next  = mf6ctable[hash];
1245 			mf6ctable[hash] = rt;
1246 			/* Add this entry to the end of the queue */
1247 			rt->mf6c_stall = rte;
1248 		} else {
1249 			/* determine if q has overflowed */
1250 			struct rtdetq **p;
1251 			int npkts = 0;
1252 
1253 			for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next) {
1254 				if (++npkts > MAX_UPQ6) {
1255 					mrt6stat.mrt6s_upq_ovflw++;
1256 					free(rte, M_MRTABLE);
1257 					m_freem(mb0);
1258 					splx(s);
1259 					return 0;
1260 				}
1261 			}
1262 
1263 			/* Add this entry to the end of the queue */
1264 			*p = rte;
1265 		}
1266 
1267 		rte->next = NULL;
1268 		rte->m = mb0;
1269 		rte->ifp = ifp;
1270 #ifdef UPCALL_TIMING
1271 		rte->t = tp;
1272 #endif
1273 
1274 		splx(s);
1275 
1276 		return 0;
1277 	}
1278 }
1279 
1280 /*
1281  * Clean up cache entries if upcalls are not serviced
1282  * Call from the Slow Timeout mechanism, every 0.25 seconds.
1283  */
1284 static void
1285 expire_upcalls(void *unused)
1286 {
1287 	struct rtdetq *rte;
1288 	struct mf6c *mfc, **nptr;
1289 	int i;
1290 
1291 	/* XXX NOMPSAFE still need softnet_lock */
1292 	mutex_enter(softnet_lock);
1293 	KERNEL_LOCK(1, NULL);
1294 
1295 	for (i = 0; i < MF6CTBLSIZ; i++) {
1296 		if (n6expire[i] == 0)
1297 			continue;
1298 		nptr = &mf6ctable[i];
1299 		while ((mfc = *nptr) != NULL) {
1300 			rte = mfc->mf6c_stall;
1301 			/*
1302 			 * Skip real cache entries
1303 			 * Make sure it wasn't marked to not expire (shouldn't happen)
1304 			 * If it expires now
1305 			 */
1306 			if (rte != NULL &&
1307 			    mfc->mf6c_expire != 0 &&
1308 			    --mfc->mf6c_expire == 0) {
1309 #ifdef MRT6DEBUG
1310 				if (mrt6debug & DEBUG_EXPIRE) {
1311 					char ip6bufo[INET6_ADDRSTRLEN];
1312 					char ip6bufm[INET6_ADDRSTRLEN];
1313 					log(LOG_DEBUG,
1314 					    "expire_upcalls: expiring (%s %s)\n",
1315 					    IN6_PRINT(ip6bufo,
1316 					    &mfc->mf6c_origin.sin6_addr),
1317 					    IN6_PRINT(ip6bufm,
1318 					    &mfc->mf6c_mcastgrp.sin6_addr));
1319 				}
1320 #endif
1321 				/*
1322 				 * drop all the packets
1323 				 * free the mbuf with the pkt, if, timing info
1324 				 */
1325 				do {
1326 					struct rtdetq *n = rte->next;
1327 					m_freem(rte->m);
1328 					free(rte, M_MRTABLE);
1329 					rte = n;
1330 				} while (rte != NULL);
1331 				mrt6stat.mrt6s_cache_cleanups++;
1332 				n6expire[i]--;
1333 
1334 				*nptr = mfc->mf6c_next;
1335 				free(mfc, M_MRTABLE);
1336 			} else {
1337 				nptr = &mfc->mf6c_next;
1338 			}
1339 		}
1340 	}
1341 	callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
1342 	    expire_upcalls, NULL);
1343 
1344 	KERNEL_UNLOCK_ONE(NULL);
1345 	mutex_exit(softnet_lock);
1346 }
1347 
1348 /*
1349  * Macro to send packet on mif.  Since RSVP packets don't get counted on
1350  * input, they shouldn't get counted on output, so statistics keeping is
1351  * separate.
1352  */
1353 #define MC6_SEND(ip6, mifp, m) do {				\
1354 	if ((mifp)->m6_flags & MIFF_REGISTER)			\
1355 		register_send((ip6), (mifp), (m));		\
1356 	else							\
1357 		phyint_send((ip6), (mifp), (m));		\
1358 } while (/*CONSTCOND*/ 0)
1359 
1360 /*
1361  * Packet forwarding routine once entry in the cache is made
1362  */
1363 static int
1364 ip6_mdq(struct mbuf *m, struct ifnet *ifp, struct mf6c *rt)
1365 {
1366 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1367 	mifi_t mifi, iif;
1368 	struct mif6 *mifp;
1369 	int plen = m->m_pkthdr.len;
1370 	struct in6_addr src0, dst0; /* copies for local work */
1371 	u_int32_t iszone, idzone, oszone, odzone;
1372 	int error = 0;
1373 
1374 	/*
1375 	 * Don't forward if it didn't arrive from the parent mif
1376 	 * for its origin.
1377 	 */
1378 	mifi = rt->mf6c_parent;
1379 	if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
1380 		/* came in the wrong interface */
1381 #ifdef MRT6DEBUG
1382 		if (mrt6debug & DEBUG_FORWARD)
1383 			log(LOG_DEBUG,
1384 			    "wrong if: ifid %d mifi %d mififid %x\n",
1385 			    ifp->if_index, mifi,
1386 			    mif6table[mifi].m6_ifp ?
1387 			    mif6table[mifi].m6_ifp->if_index : -1);
1388 #endif
1389 		mrt6stat.mrt6s_wrong_if++;
1390 		rt->mf6c_wrong_if++;
1391 
1392 		/*
1393 		 * If we are doing PIM processing, and we are forwarding
1394 		 * packets on this interface, send a message to the
1395 		 * routing daemon.
1396 		 */
1397 		/* have to make sure this is a valid mif */
1398 		if (mifi < nummifs && mif6table[mifi].m6_ifp) {
1399 			if (pim6 && (m->m_flags & M_LOOP) == 0) {
1400 				/*
1401 				 * Check the M_LOOP flag to avoid an
1402 				 * unnecessary PIM assert.
1403 				 * XXX: M_LOOP is an ad-hoc hack...
1404 				 */
1405 				struct sockaddr_in6 sin6;
1406 
1407 				struct mbuf *mm;
1408 				struct mrt6msg *im;
1409 				struct omrt6msg *oim;
1410 
1411 				mm = m_copym(m, 0, sizeof(struct ip6_hdr), M_DONTWAIT);
1412 				if (mm && M_UNWRITABLE(mm, sizeof(struct ip6_hdr)))
1413 					mm = m_pullup(mm, sizeof(struct ip6_hdr));
1414 				if (mm == NULL)
1415 					return ENOBUFS;
1416 
1417 				oim = NULL;
1418 				im = NULL;
1419 				switch (ip6_mrouter_ver) {
1420 				case MRT6_OINIT:
1421 					oim = mtod(mm, struct omrt6msg *);
1422 					oim->im6_msgtype = MRT6MSG_WRONGMIF;
1423 					oim->im6_mbz = 0;
1424 					break;
1425 				case MRT6_INIT:
1426 					im = mtod(mm, struct mrt6msg *);
1427 					im->im6_msgtype = MRT6MSG_WRONGMIF;
1428 					im->im6_mbz = 0;
1429 					break;
1430 				default:
1431 					m_freem(mm);
1432 					return EINVAL;
1433 				}
1434 
1435 				for (mifp = mif6table, iif = 0;
1436 				     iif < nummifs && mifp &&
1437 					     mifp->m6_ifp != ifp;
1438 				     mifp++, iif++)
1439 					;
1440 
1441 				memset(&sin6, 0, sizeof(sin6));
1442 				sin6.sin6_len = sizeof(sin6);
1443 				sin6.sin6_family = AF_INET6;
1444 				switch (ip6_mrouter_ver) {
1445 				case MRT6_OINIT:
1446 					oim->im6_mif = iif;
1447 					sin6.sin6_addr = oim->im6_src;
1448 					break;
1449 				case MRT6_INIT:
1450 					im->im6_mif = iif;
1451 					sin6.sin6_addr = im->im6_src;
1452 					break;
1453 				}
1454 
1455 				mrt6stat.mrt6s_upcalls++;
1456 
1457 				if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1458 #ifdef MRT6DEBUG
1459 					if (mrt6debug)
1460 						log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n");
1461 #endif
1462 					++mrt6stat.mrt6s_upq_sockfull;
1463 					return ENOBUFS;
1464 				}
1465 			}
1466 		}
1467 
1468 		return 0;
1469 	}
1470 
1471 	/* If I sourced this packet, it counts as output, else it was input. */
1472 	if (m->m_pkthdr.rcvif_index == 0) {
1473 		/* XXX: is rcvif really NULL when output?? */
1474 		mif6table[mifi].m6_pkt_out++;
1475 		mif6table[mifi].m6_bytes_out += plen;
1476 	} else {
1477 		mif6table[mifi].m6_pkt_in++;
1478 		mif6table[mifi].m6_bytes_in += plen;
1479 	}
1480 	rt->mf6c_pkt_cnt++;
1481 	rt->mf6c_byte_cnt += plen;
1482 
1483 	/*
1484 	 * For each mif, forward a copy of the packet if there are group
1485 	 * members downstream on the interface.
1486 	 */
1487 	src0 = ip6->ip6_src;
1488 	dst0 = ip6->ip6_dst;
1489 	if ((error = in6_setscope(&src0, ifp, &iszone)) != 0 ||
1490 	    (error = in6_setscope(&dst0, ifp, &idzone)) != 0) {
1491 		IP6_STATINC(IP6_STAT_BADSCOPE);
1492 		return error;
1493 	}
1494 	for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++) {
1495 		if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
1496 			if (mif6table[mifi].m6_ifp == NULL)
1497 				continue;
1498 			/*
1499 			 * check if the outgoing packet is going to break
1500 			 * a scope boundary.
1501 			 * XXX: For packets through PIM register tunnel
1502 			 * interface, we believe the routing daemon.
1503 			 */
1504 			if ((mif6table[rt->mf6c_parent].m6_flags &
1505 			     MIFF_REGISTER) == 0 &&
1506 			    (mif6table[mifi].m6_flags & MIFF_REGISTER) == 0) {
1507 				if (in6_setscope(&src0, mif6table[mifi].m6_ifp,
1508 				    &oszone) ||
1509 				    in6_setscope(&dst0, mif6table[mifi].m6_ifp,
1510 				    &odzone) ||
1511 				    iszone != oszone || idzone != odzone) {
1512 					IP6_STATINC(IP6_STAT_BADSCOPE);
1513 					continue;
1514 				}
1515 			}
1516 
1517 			mifp->m6_pkt_out++;
1518 			mifp->m6_bytes_out += plen;
1519 			MC6_SEND(ip6, mifp, m);
1520 		}
1521 	}
1522 
1523 	return 0;
1524 }
1525 
1526 static void
1527 phyint_send(struct ip6_hdr *ip6, struct mif6 *mifp, struct mbuf *m)
1528 {
1529 	struct mbuf *mb_copy;
1530 	struct ifnet *ifp = mifp->m6_ifp;
1531 	int error __mrt6debugused = 0;
1532 	int s;
1533 	static struct route ro;
1534 	bool ingroup;
1535 	struct sockaddr_in6 dst6;
1536 	u_long linkmtu;
1537 
1538 	s = splsoftnet();
1539 
1540 	/*
1541 	 * Make a new reference to the packet; make sure that
1542 	 * the IPv6 header is actually copied, not just referenced,
1543 	 * so that ip6_output() only scribbles on the copy.
1544 	 */
1545 	mb_copy = m_copypacket(m, M_DONTWAIT);
1546 	if (mb_copy && M_UNWRITABLE(mb_copy, sizeof(struct ip6_hdr)))
1547 		mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr));
1548 	if (mb_copy == NULL) {
1549 		splx(s);
1550 		return;
1551 	}
1552 
1553 	/* set MCAST flag to the outgoing packet */
1554 	mb_copy->m_flags |= M_MCAST;
1555 
1556 	/*
1557 	 * If we sourced the packet, call ip6_output since we may divide
1558 	 * the packet into fragments when the packet is too big for the
1559 	 * outgoing interface.
1560 	 * Otherwise, we can simply send the packet to the interface
1561 	 * sending queue.
1562 	 */
1563 	if (m->m_pkthdr.rcvif_index == 0) {
1564 		struct ip6_moptions im6o;
1565 
1566 		im6o.im6o_multicast_if_index = if_get_index(ifp);
1567 		/* XXX: ip6_output will override ip6->ip6_hlim */
1568 		im6o.im6o_multicast_hlim = ip6->ip6_hlim;
1569 		im6o.im6o_multicast_loop = 1;
1570 		error = ip6_output(mb_copy, NULL, &ro, IPV6_FORWARDING,
1571 		    &im6o, NULL, NULL);
1572 
1573 #ifdef MRT6DEBUG
1574 		if (mrt6debug & DEBUG_XMIT)
1575 			log(LOG_DEBUG, "phyint_send on mif %td err %d\n",
1576 			    mifp - mif6table, error);
1577 #endif
1578 		splx(s);
1579 		return;
1580 	}
1581 
1582 	/*
1583 	 * If we belong to the destination multicast group
1584 	 * on the outgoing interface, loop back a copy.
1585 	 */
1586 	/*
1587 	 * Does not have to check source info, as it's alreay covered by
1588 	 * ip6_input
1589 	 */
1590 	sockaddr_in6_init(&dst6, &ip6->ip6_dst, 0, 0, 0);
1591 
1592 	ingroup = in6_multi_group(&ip6->ip6_dst, ifp);
1593 	if (ingroup) {
1594 		ip6_mloopback(ifp, m,
1595 		    satocsin6(rtcache_getdst(&ro)));
1596 	}
1597 
1598 	/*
1599 	 * Put the packet into the sending queue of the outgoing interface
1600 	 * if it would fit in the MTU of the interface.
1601 	 */
1602 	linkmtu = IN6_LINKMTU(ifp);
1603 	if (mb_copy->m_pkthdr.len <= linkmtu || linkmtu < IPV6_MMTU) {
1604 		error = ip6_if_output(ifp, ifp, mb_copy, &dst6, NULL);
1605 #ifdef MRT6DEBUG
1606 		if (mrt6debug & DEBUG_XMIT)
1607 			log(LOG_DEBUG, "phyint_send on mif %td err %d\n",
1608 			    mifp - mif6table, error);
1609 #endif
1610 	} else {
1611 		/*
1612 		 * pMTU discovery is intentionally disabled by default, since
1613 		 * various routers may notify pMTU in multicast, which can be
1614 		 * a DDoS to a router.
1615 		 */
1616 		if (ip6_mcast_pmtu) {
1617 			icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, linkmtu);
1618 		} else {
1619 			/* simply discard the packet */
1620 #ifdef MRT6DEBUG
1621 			if (mrt6debug & DEBUG_XMIT) {
1622 				char ip6bufs[INET6_ADDRSTRLEN];
1623 				char ip6bufd[INET6_ADDRSTRLEN];
1624 				log(LOG_DEBUG,
1625 				    "phyint_send: packet too big on %s o %s g %s"
1626 				    " size %d(discarded)\n",
1627 				    if_name(ifp),
1628 				    IN6_PRINT(ip6bufs, &ip6->ip6_src),
1629 				    IN6_PRINT(ip6bufd, &ip6->ip6_dst),
1630 				    mb_copy->m_pkthdr.len);
1631 			}
1632 #endif
1633 			m_freem(mb_copy);
1634 		}
1635 	}
1636 
1637 	splx(s);
1638 }
1639 
1640 static int
1641 register_send(struct ip6_hdr *ip6, struct mif6 *mif, struct mbuf *m)
1642 {
1643 	struct mbuf *mm;
1644 	int i, len = m->m_pkthdr.len;
1645 	struct sockaddr_in6 sin6;
1646 	struct mrt6msg *im6;
1647 
1648 #ifdef MRT6DEBUG
1649 	if (mrt6debug) {
1650 		char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1651 		log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n",
1652 		    IN6_PRINT(ip6bufs, &ip6->ip6_src),
1653 		    IN6_PRINT(ip6bufd, &ip6->ip6_dst));
1654 	}
1655 #endif
1656 	PIM6_STATINC(PIM6_STAT_SND_REGISTERS);
1657 
1658 	/* Make a copy of the packet to send to the user level process */
1659 	MGETHDR(mm, M_DONTWAIT, MT_HEADER);
1660 	if (mm == NULL)
1661 		return ENOBUFS;
1662 	mm->m_data += max_linkhdr;
1663 	mm->m_len = sizeof(struct ip6_hdr);
1664 
1665 	if ((mm->m_next = m_copypacket(m, M_DONTWAIT)) == NULL) {
1666 		m_freem(mm);
1667 		return ENOBUFS;
1668 	}
1669 	i = MHLEN - M_LEADINGSPACE(mm);
1670 	if (i > len)
1671 		i = len;
1672 	mm = m_pullup(mm, i);
1673 	if (mm == NULL)
1674 		return ENOBUFS;
1675 	mm->m_pkthdr.len = len + sizeof(struct ip6_hdr);
1676 
1677 	/*
1678 	 * Send message to routing daemon
1679 	 */
1680 	sockaddr_in6_init(&sin6, &ip6->ip6_src, 0, 0, 0);
1681 
1682 	im6 = mtod(mm, struct mrt6msg *);
1683 	im6->im6_msgtype = MRT6MSG_WHOLEPKT;
1684 	im6->im6_mbz = 0;
1685 	im6->im6_mif = mif - mif6table;
1686 
1687 	/* iif info is not given for reg. encap.n */
1688 	mrt6stat.mrt6s_upcalls++;
1689 
1690 	if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1691 #ifdef MRT6DEBUG
1692 		if (mrt6debug)
1693 			log(LOG_WARNING,
1694 			    "register_send: ip6_mrouter socket queue full\n");
1695 #endif
1696 		++mrt6stat.mrt6s_upq_sockfull;
1697 		return ENOBUFS;
1698 	}
1699 
1700 	return 0;
1701 }
1702 
1703 /*
1704  * PIM sparse mode hook. Receives the pim control messages, and passes them up
1705  * to the listening socket, using rip6_input.
1706  *
1707  * The only message processed is the REGISTER pim message; the pim header
1708  * is stripped off, and the inner packet is passed to register_mforward.
1709  */
1710 int
1711 pim6_input(struct mbuf **mp, int *offp, int proto)
1712 {
1713 	struct pim *pim;
1714 	struct ip6_hdr *ip6 __mrt6debugused;
1715 	int pimlen;
1716 	struct mbuf *m = *mp;
1717 	int minlen;
1718 	int off = *offp;
1719 
1720 	PIM6_STATINC(PIM6_STAT_RCV_TOTAL);
1721 
1722 	ip6 = mtod(m, struct ip6_hdr *);
1723 	pimlen = m->m_pkthdr.len - off;
1724 
1725 	/*
1726 	 * Validate lengths
1727 	 */
1728 	if (pimlen < PIM_MINLEN) {
1729 		PIM6_STATINC(PIM6_STAT_RCV_TOOSHORT);
1730 #ifdef MRT6DEBUG
1731 		if (mrt6debug & DEBUG_PIM)
1732 			log(LOG_DEBUG,"pim6_input: PIM packet too short\n");
1733 #endif
1734 		m_freem(m);
1735 		return IPPROTO_DONE;
1736 	}
1737 
1738 	/*
1739 	 * If the packet is at least as big as a REGISTER, go ahead
1740 	 * and grab the PIM REGISTER header size, to avoid another
1741 	 * possible m_pullup() later.
1742 	 *
1743 	 * PIM_MINLEN       == pimhdr + u_int32 == 8
1744 	 * PIM6_REG_MINLEN   == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1745 	 */
1746 	minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN;
1747 
1748 	/*
1749 	 * Make sure that the IP6 and PIM headers in contiguous memory, and
1750 	 * possibly the PIM REGISTER header
1751 	 */
1752 	IP6_EXTHDR_GET(pim, struct pim *, m, off, minlen);
1753 	if (pim == NULL) {
1754 		PIM6_STATINC(PIM6_STAT_RCV_TOOSHORT);
1755 		return IPPROTO_DONE;
1756 	}
1757 
1758 	/* PIM version check */
1759 	if (pim->pim_ver != PIM_VERSION) {
1760 		PIM6_STATINC(PIM6_STAT_RCV_BADVERSION);
1761 #ifdef MRT6DEBUG
1762 		log(LOG_ERR,
1763 		    "pim6_input: incorrect version %d, expecting %d\n",
1764 		    pim->pim_ver, PIM_VERSION);
1765 #endif
1766 		m_freem(m);
1767 		return IPPROTO_DONE;
1768 	}
1769 
1770 #define PIM6_CHECKSUM
1771 #ifdef PIM6_CHECKSUM
1772 	{
1773 		int cksumlen;
1774 
1775 		/*
1776 		 * Validate checksum.
1777 		 * If PIM REGISTER, exclude the data packet
1778 		 */
1779 		if (pim->pim_type == PIM_REGISTER)
1780 			cksumlen = PIM_MINLEN;
1781 		else
1782 			cksumlen = pimlen;
1783 
1784 		if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) {
1785 			PIM6_STATINC(PIM6_STAT_RCV_BADSUM);
1786 #ifdef MRT6DEBUG
1787 			if (mrt6debug & DEBUG_PIM)
1788 				log(LOG_DEBUG,
1789 				    "pim6_input: invalid checksum\n");
1790 #endif
1791 			m_freem(m);
1792 			return IPPROTO_DONE;
1793 		}
1794 	}
1795 #endif /* PIM_CHECKSUM */
1796 
1797 	if (pim->pim_type == PIM_REGISTER) {
1798 		/*
1799 		 * since this is a REGISTER, we'll make a copy of the register
1800 		 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1801 		 * routing daemon.
1802 		 */
1803 		static const struct sockaddr_in6 dst = {
1804 			.sin6_len = sizeof(dst),
1805 			.sin6_family = AF_INET6,
1806 		};
1807 
1808 		struct mbuf *mcp;
1809 		struct ip6_hdr *eip6;
1810 		u_int32_t *reghdr;
1811 
1812 		PIM6_STATINC(PIM6_STAT_RCV_REGISTERS);
1813 
1814 		if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) {
1815 #ifdef MRT6DEBUG
1816 			if (mrt6debug & DEBUG_PIM)
1817 				log(LOG_DEBUG,
1818 				    "pim6_input: register mif not set: %d\n",
1819 				    reg_mif_num);
1820 #endif
1821 			m_freem(m);
1822 			return IPPROTO_DONE;
1823 		}
1824 
1825 		reghdr = (u_int32_t *)(pim + 1);
1826 
1827 		if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
1828 			goto pim6_input_to_daemon;
1829 
1830 		/*
1831 		 * Validate length
1832 		 */
1833 		if (pimlen < PIM6_REG_MINLEN) {
1834 #ifdef MRT6DEBUG
1835 			char ip6buf[INET6_ADDRSTRLEN];
1836 			log(LOG_ERR,
1837 			    "pim6_input: register packet size too "
1838 			    "small %d from %s\n",
1839 			    pimlen, IN6_PRINT(ip6buf, &ip6->ip6_src));
1840 #endif
1841 			PIM6_STATINC(PIM6_STAT_RCV_TOOSHORT);
1842 			PIM6_STATINC(PIM6_STAT_RCV_BADREGISTERS);
1843 			m_freem(m);
1844 			return IPPROTO_DONE;
1845 		}
1846 
1847 		eip6 = (struct ip6_hdr *)(reghdr + 1);
1848 #ifdef MRT6DEBUG
1849 		if (mrt6debug & DEBUG_PIM) {
1850 			char ip6bufs[INET6_ADDRSTRLEN];
1851 			char ip6bufd[INET6_ADDRSTRLEN];
1852 			log(LOG_DEBUG,
1853 			    "pim6_input[register], eip6: %s -> %s, "
1854 			    "eip6 plen %d\n",
1855 			    IN6_PRINT(ip6bufs, &eip6->ip6_src),
1856 			    IN6_PRINT(ip6bufd, &eip6->ip6_dst),
1857 			    ntohs(eip6->ip6_plen));
1858 		}
1859 #endif
1860 
1861 		/* verify the version number of the inner packet */
1862 		if ((eip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1863 			PIM6_STATINC(PIM6_STAT_RCV_BADREGISTERS);
1864 #ifdef MRT6DEBUG
1865 			log(LOG_DEBUG, "pim6_input: invalid IP version (%d) "
1866 			    "of the inner packet\n",
1867 			    (eip6->ip6_vfc & IPV6_VERSION));
1868 #endif
1869 			m_freem(m);
1870 			return IPPROTO_DONE;
1871 		}
1872 
1873 		/* verify the inner packet is destined to a mcast group */
1874 		if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) {
1875 			PIM6_STATINC(PIM6_STAT_RCV_BADREGISTERS);
1876 #ifdef MRT6DEBUG
1877 			if (mrt6debug & DEBUG_PIM) {
1878 				char ip6buf[INET6_ADDRSTRLEN];
1879 				log(LOG_DEBUG,
1880 				    "pim6_input: inner packet of register "
1881 				    "is not multicast %s\n",
1882 				    IN6_PRINT(ip6buf, &eip6->ip6_dst));
1883 			}
1884 #endif
1885 			m_freem(m);
1886 			return IPPROTO_DONE;
1887 		}
1888 
1889 		/*
1890 		 * make a copy of the whole header to pass to the daemon later.
1891 		 */
1892 		mcp = m_copym(m, 0, off + PIM6_REG_MINLEN, M_DONTWAIT);
1893 		if (mcp == NULL) {
1894 #ifdef MRT6DEBUG
1895 			log(LOG_ERR,
1896 			    "pim6_input: pim register: "
1897 			    "could not copy register head\n");
1898 #endif
1899 			m_freem(m);
1900 			return IPPROTO_DONE;
1901 		}
1902 
1903 		/*
1904 		 * forward the inner ip6 packet; point m_data at the inner ip6.
1905 		 */
1906 		m_adj(m, off + PIM_MINLEN);
1907 #ifdef MRT6DEBUG
1908 		if (mrt6debug & DEBUG_PIM) {
1909 			char ip6bufs[INET6_ADDRSTRLEN];
1910 			char ip6bufd[INET6_ADDRSTRLEN];
1911 			log(LOG_DEBUG,
1912 			    "pim6_input: forwarding decapsulated register: "
1913 			    "src %s, dst %s, mif %d\n",
1914 			    IN6_PRINT(ip6bufs, &eip6->ip6_src),
1915 			    IN6_PRINT(ip6bufd, &eip6->ip6_dst),
1916 			    reg_mif_num);
1917 		}
1918 #endif
1919 
1920 		looutput(mif6table[reg_mif_num].m6_ifp, m, sin6tocsa(&dst),
1921 		    NULL);
1922 
1923 		/* prepare the register head to send to the mrouting daemon */
1924 		m = mcp;
1925 	}
1926 
1927 	/*
1928 	 * Pass the PIM message up to the daemon; if it is a register message
1929 	 * pass the 'head' only up to the daemon. This includes the
1930 	 * encapsulator ip6 header, pim header, register header and the
1931 	 * encapsulated ip6 header.
1932 	 */
1933 pim6_input_to_daemon:
1934 	/*
1935 	 * Currently, rip6_input() is always called holding softnet_lock
1936 	 * by ipintr()(!NET_MPSAFE) or PR_INPUT_WRAP()(NET_MPSAFE).
1937 	 */
1938 	KASSERT(mutex_owned(softnet_lock));
1939 	rip6_input(&m, offp, proto);
1940 	return IPPROTO_DONE;
1941 }
1942 
1943 static int
1944 sysctl_net_inet6_pim6_stats(SYSCTLFN_ARGS)
1945 {
1946 
1947 	return (NETSTAT_SYSCTL(pim6stat_percpu, PIM6_NSTATS));
1948 }
1949 
1950 static void
1951 sysctl_net_inet6_pim6_setup(struct sysctllog **clog)
1952 {
1953 
1954 	sysctl_createv(clog, 0, NULL, NULL,
1955 		       CTLFLAG_PERMANENT,
1956 		       CTLTYPE_NODE, "inet6", NULL,
1957 		       NULL, 0, NULL, 0,
1958 		       CTL_NET, PF_INET6, CTL_EOL);
1959 	sysctl_createv(clog, 0, NULL, NULL,
1960 		       CTLFLAG_PERMANENT,
1961 		       CTLTYPE_NODE, "pim6",
1962 		       SYSCTL_DESCR("PIMv6 settings"),
1963 		       NULL, 0, NULL, 0,
1964 		       CTL_NET, PF_INET6, IPPROTO_PIM, CTL_EOL);
1965 
1966 	sysctl_createv(clog, 0, NULL, NULL,
1967 		       CTLFLAG_PERMANENT,
1968 		       CTLTYPE_STRUCT, "stats",
1969 		       SYSCTL_DESCR("PIMv6 statistics"),
1970 		       sysctl_net_inet6_pim6_stats, 0, NULL, 0,
1971 		       CTL_NET, PF_INET6, IPPROTO_PIM, PIM6CTL_STATS,
1972 		       CTL_EOL);
1973 }
1974