xref: /netbsd-src/sys/netinet6/ip6_mroute.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /*	$NetBSD: ip6_mroute.c,v 1.127 2018/05/01 07:21:39 maxv 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.127 2018/05/01 07:21:39 maxv 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 #ifdef notyet
689 	/* scaling up here allows division by 1024 in critical code */
690 	mifp->m6_rate_limit = mifcp->mif6c_rate_limit * 1024 / 1000;
691 #endif
692 	/* initialize per mif pkt counters */
693 	mifp->m6_pkt_in    = 0;
694 	mifp->m6_pkt_out   = 0;
695 	mifp->m6_bytes_in  = 0;
696 	mifp->m6_bytes_out = 0;
697 	splx(s);
698 
699 	/* Adjust nummifs up if the mifi is higher than nummifs */
700 	if (nummifs <= mifcp->mif6c_mifi)
701 		nummifs = mifcp->mif6c_mifi + 1;
702 
703 #ifdef MRT6DEBUG
704 	if (mrt6debug)
705 		log(LOG_DEBUG,
706 		    "add_mif #%d, phyint %s\n",
707 		    mifcp->mif6c_mifi, ifp->if_xname);
708 #endif
709 
710 	return 0;
711 }
712 
713 /*
714  * Delete a mif from the mif table
715  */
716 static int
717 del_m6if(mifi_t *mifip)
718 {
719 	struct mif6 *mifp = mif6table + *mifip;
720 	mifi_t mifi;
721 	struct ifnet *ifp;
722 	struct sockaddr_in6 sin6;
723 	int s;
724 
725 	if (*mifip >= nummifs)
726 		return EINVAL;
727 	if (mifp->m6_ifp == NULL)
728 		return EINVAL;
729 
730 	s = splsoftnet();
731 
732 	if (!(mifp->m6_flags & MIFF_REGISTER)) {
733 		/*
734 		 * XXX: what if there is yet IPv4 multicast daemon
735 		 *      using the interface?
736 		 */
737 		ifp = mifp->m6_ifp;
738 
739 		sin6.sin6_family = AF_INET6;
740 		sin6.sin6_addr = in6addr_any;
741 		if_mcast_op(ifp, SIOCDELMULTI, sin6tosa(&sin6));
742 	} else {
743 		if (reg_mif_num != (mifi_t)-1) {
744 			if_detach(&multicast_register_if6);
745 			reg_mif_num = (mifi_t)-1;
746 		}
747 	}
748 
749 	memset((void *)mifp, 0, sizeof (*mifp));
750 
751 	/* Adjust nummifs down */
752 	for (mifi = nummifs; mifi > 0; mifi--)
753 		if (mif6table[mifi - 1].m6_ifp)
754 			break;
755 	nummifs = mifi;
756 
757 	splx(s);
758 
759 #ifdef MRT6DEBUG
760 	if (mrt6debug)
761 		log(LOG_DEBUG, "del_m6if %d, nummifs %d\n", *mifip, nummifs);
762 #endif
763 
764 	return 0;
765 }
766 
767 /*
768  * Add an mfc entry
769  */
770 static int
771 add_m6fc(struct mf6cctl *mfccp)
772 {
773 	struct mf6c *rt;
774 	u_long hash;
775 	struct rtdetq *rte;
776 	u_short nstl;
777 	int s;
778 	char ip6bufo[INET6_ADDRSTRLEN], ip6bufm[INET6_ADDRSTRLEN];
779 
780 	MF6CFIND(mfccp->mf6cc_origin.sin6_addr,
781 		 mfccp->mf6cc_mcastgrp.sin6_addr, rt);
782 
783 	/* If an entry already exists, just update the fields */
784 	if (rt) {
785 #ifdef MRT6DEBUG
786 		if (mrt6debug & DEBUG_MFC)
787 			log(LOG_DEBUG,"add_m6fc update o %s g %s p %x\n",
788 			    IN6_PRINT(ip6bufo,
789 			    &mfccp->mf6cc_origin.sin6_addr),
790 			    IN6_PRINT(ip6bufm,
791 			    &mfccp->mf6cc_mcastgrp.sin6_addr),
792 			    mfccp->mf6cc_parent);
793 #endif
794 
795 		s = splsoftnet();
796 		rt->mf6c_parent = mfccp->mf6cc_parent;
797 		rt->mf6c_ifset = mfccp->mf6cc_ifset;
798 		splx(s);
799 		return 0;
800 	}
801 
802 	/*
803 	 * Find the entry for which the upcall was made and update
804 	 */
805 	s = splsoftnet();
806 	hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr,
807 			mfccp->mf6cc_mcastgrp.sin6_addr);
808 	for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) {
809 		if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
810 				       &mfccp->mf6cc_origin.sin6_addr) &&
811 		    IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
812 				       &mfccp->mf6cc_mcastgrp.sin6_addr) &&
813 		    (rt->mf6c_stall != NULL)) {
814 
815 			if (nstl++)
816 				log(LOG_ERR,
817 				    "add_m6fc: %s o %s g %s p %x dbx %p\n",
818 				    "multiple kernel entries",
819 				    IN6_PRINT(ip6bufo,
820 				    &mfccp->mf6cc_origin.sin6_addr),
821 				    IN6_PRINT(ip6bufm,
822 				    &mfccp->mf6cc_mcastgrp.sin6_addr),
823 				    mfccp->mf6cc_parent, rt->mf6c_stall);
824 
825 #ifdef MRT6DEBUG
826 			if (mrt6debug & DEBUG_MFC)
827 				log(LOG_DEBUG,
828 				    "add_m6fc o %s g %s p %x dbg %p\n",
829 				    IN6_PRINT(ip6bufo,
830 				    &mfccp->mf6cc_origin.sin6_addr),
831 				    IN6_PRINT(ip6bufm,
832 				    &mfccp->mf6cc_mcastgrp.sin6_addr),
833 				    mfccp->mf6cc_parent, rt->mf6c_stall);
834 #endif
835 
836 			rt->mf6c_origin     = mfccp->mf6cc_origin;
837 			rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
838 			rt->mf6c_parent     = mfccp->mf6cc_parent;
839 			rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
840 			/* initialize pkt counters per src-grp */
841 			rt->mf6c_pkt_cnt    = 0;
842 			rt->mf6c_byte_cnt   = 0;
843 			rt->mf6c_wrong_if   = 0;
844 
845 			rt->mf6c_expire = 0;	/* Don't clean this guy up */
846 			n6expire[hash]--;
847 
848 			/* free packets Qed at the end of this entry */
849 			for (rte = rt->mf6c_stall; rte != NULL; ) {
850 				struct rtdetq *n = rte->next;
851 				if (rte->ifp) {
852 					ip6_mdq(rte->m, rte->ifp, rt);
853 				}
854 				m_freem(rte->m);
855 #ifdef UPCALL_TIMING
856 				collate(&(rte->t));
857 #endif
858 				free(rte, M_MRTABLE);
859 				rte = n;
860 			}
861 			rt->mf6c_stall = NULL;
862 		}
863 	}
864 
865 	/*
866 	 * It is possible that an entry is being inserted without an upcall
867 	 */
868 	if (nstl == 0) {
869 #ifdef MRT6DEBUG
870 		if (mrt6debug & DEBUG_MFC)
871 			log(LOG_DEBUG,
872 			    "add_mfc no upcall h %ld o %s g %s p %x\n",
873 			    hash,
874 			    IN6_PRINT(ip6bufo,
875 			    &mfccp->mf6cc_origin.sin6_addr),
876 			    IN6_PRINT(ip6bufm,
877 			    &mfccp->mf6cc_mcastgrp.sin6_addr),
878 			    mfccp->mf6cc_parent);
879 #endif
880 
881 		for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
882 
883 			if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
884 					       &mfccp->mf6cc_origin.sin6_addr)&&
885 			    IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
886 					       &mfccp->mf6cc_mcastgrp.sin6_addr)) {
887 
888 				rt->mf6c_origin     = mfccp->mf6cc_origin;
889 				rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
890 				rt->mf6c_parent     = mfccp->mf6cc_parent;
891 				rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
892 				/* initialize pkt counters per src-grp */
893 				rt->mf6c_pkt_cnt    = 0;
894 				rt->mf6c_byte_cnt   = 0;
895 				rt->mf6c_wrong_if   = 0;
896 
897 				if (rt->mf6c_expire)
898 					n6expire[hash]--;
899 				rt->mf6c_expire	   = 0;
900 			}
901 		}
902 		if (rt == NULL) {
903 			/* no upcall, so make a new entry */
904 			rt = malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
905 			if (rt == NULL) {
906 				splx(s);
907 				return ENOBUFS;
908 			}
909 
910 			/* insert new entry at head of hash chain */
911 			rt->mf6c_origin     = mfccp->mf6cc_origin;
912 			rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
913 			rt->mf6c_parent     = mfccp->mf6cc_parent;
914 			rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
915 			/* initialize pkt counters per src-grp */
916 			rt->mf6c_pkt_cnt    = 0;
917 			rt->mf6c_byte_cnt   = 0;
918 			rt->mf6c_wrong_if   = 0;
919 			rt->mf6c_expire     = 0;
920 			rt->mf6c_stall = NULL;
921 
922 			/* link into table */
923 			rt->mf6c_next  = mf6ctable[hash];
924 			mf6ctable[hash] = rt;
925 		}
926 	}
927 	splx(s);
928 	return 0;
929 }
930 
931 #ifdef UPCALL_TIMING
932 /*
933  * collect delay statistics on the upcalls
934  */
935 static void
936 collate(struct timeval *t)
937 {
938 	u_long d;
939 	struct timeval tp;
940 	u_long delta;
941 
942 	GET_TIME(tp);
943 
944 	if (TV_LT(*t, tp))
945 	{
946 		TV_DELTA(tp, *t, delta);
947 
948 		d = delta >> 10;
949 		if (d > UPCALL_MAX)
950 			d = UPCALL_MAX;
951 
952 		++upcall_data[d];
953 	}
954 }
955 #endif /* UPCALL_TIMING */
956 
957 /*
958  * Delete an mfc entry
959  */
960 static int
961 del_m6fc(struct mf6cctl *mfccp)
962 {
963 	struct sockaddr_in6 	origin;
964 	struct sockaddr_in6 	mcastgrp;
965 	struct mf6c 		*rt;
966 	struct mf6c	 	**nptr;
967 	u_long 		hash;
968 	int s;
969 
970 	origin = mfccp->mf6cc_origin;
971 	mcastgrp = mfccp->mf6cc_mcastgrp;
972 	hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr);
973 
974 #ifdef MRT6DEBUG
975 	if (mrt6debug & DEBUG_MFC) {
976 		char ip6bufo[INET6_ADDRSTRLEN], ip6bufm[INET6_ADDRSTRLEN];
977 		log(LOG_DEBUG,"del_m6fc orig %s mcastgrp %s\n",
978 		    IN6_PRINT(ip6bufo, &origin.sin6_addr),
979 		    IN6_PRINT(ip6bufm, &mcastgrp.sin6_addr));
980 	}
981 #endif
982 
983 	s = splsoftnet();
984 
985 	nptr = &mf6ctable[hash];
986 	while ((rt = *nptr) != NULL) {
987 		if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr,
988 				       &rt->mf6c_origin.sin6_addr) &&
989 		    IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr,
990 				       &rt->mf6c_mcastgrp.sin6_addr) &&
991 		    rt->mf6c_stall == NULL)
992 			break;
993 
994 		nptr = &rt->mf6c_next;
995 	}
996 	if (rt == NULL) {
997 		splx(s);
998 		return EADDRNOTAVAIL;
999 	}
1000 
1001 	*nptr = rt->mf6c_next;
1002 	free(rt, M_MRTABLE);
1003 
1004 	splx(s);
1005 
1006 	return 0;
1007 }
1008 
1009 static int
1010 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in6 *src)
1011 {
1012 	if (s) {
1013 		if (sbappendaddr(&s->so_rcv, sin6tosa(src), mm, NULL) != 0) {
1014 			sorwakeup(s);
1015 			return 0;
1016 		}
1017 		soroverflow(s);
1018 	}
1019 	m_freem(mm);
1020 	return -1;
1021 }
1022 
1023 /*
1024  * IPv6 multicast forwarding function. This function assumes that the packet
1025  * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
1026  * pointed to by "ifp", and the packet is to be relayed to other networks
1027  * that have members of the packet's destination IPv6 multicast group.
1028  *
1029  * The packet is returned unscathed to the caller, unless it is
1030  * erroneous, in which case a non-zero return value tells the caller to
1031  * discard it.
1032  */
1033 int
1034 ip6_mforward(struct ip6_hdr *ip6, struct ifnet *ifp, struct mbuf *m)
1035 {
1036 	struct mf6c *rt;
1037 	struct mif6 *mifp;
1038 	struct mbuf *mm;
1039 	int s;
1040 	mifi_t mifi;
1041 	struct sockaddr_in6 sin6;
1042 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1043 
1044 #ifdef MRT6DEBUG
1045 	if (mrt6debug & DEBUG_FORWARD)
1046 		log(LOG_DEBUG, "ip6_mforward: src %s, dst %s, ifindex %d\n",
1047 		    IN6_PRINT(ip6bufs, &ip6->ip6_src),
1048 		    IN6_PRINT(ip6bufd, &ip6->ip6_dst),
1049 		    ifp->if_index);
1050 #endif
1051 
1052 	/*
1053 	 * Don't forward a packet with Hop limit of zero or one,
1054 	 * or a packet destined to a local-only group.
1055 	 */
1056 	if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst) ||
1057 	    IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1058 		return 0;
1059 	ip6->ip6_hlim--;
1060 
1061 	/*
1062 	 * Source address check: do not forward packets with unspecified
1063 	 * source. It was discussed in July 2000, on ipngwg mailing list.
1064 	 * This is rather more serious than unicast cases, because some
1065 	 * MLD packets can be sent with the unspecified source address
1066 	 * (although such packets must normally set the hop limit field to 1).
1067 	 */
1068 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
1069 		IP6_STATINC(IP6_STAT_CANTFORWARD);
1070 		if (ip6_log_time + ip6_log_interval < time_uptime) {
1071 			ip6_log_time = time_uptime;
1072 			log(LOG_DEBUG,
1073 			    "cannot forward "
1074 			    "from %s to %s nxt %d received on %s\n",
1075 			    IN6_PRINT(ip6bufs, &ip6->ip6_src),
1076 			    IN6_PRINT(ip6bufd, &ip6->ip6_dst),
1077 			    ip6->ip6_nxt,
1078 			    m->m_pkthdr.rcvif_index ?
1079 			    if_name(m_get_rcvif_NOMPSAFE(m)) : "?");
1080 		}
1081 		return 0;
1082 	}
1083 
1084 	/*
1085 	 * Determine forwarding mifs from the forwarding cache table
1086 	 */
1087 	s = splsoftnet();
1088 	MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
1089 
1090 	/* Entry exists, so forward if necessary */
1091 	if (rt) {
1092 		splx(s);
1093 		return ip6_mdq(m, ifp, rt);
1094 	} else {
1095 		/*
1096 		 * If we don't have a route for packet's origin, make a copy
1097 		 * of the packet and send message to routing daemon.
1098 		 */
1099 
1100 		struct mbuf *mb0;
1101 		struct rtdetq *rte;
1102 		u_long hash;
1103 
1104 #ifdef UPCALL_TIMING
1105 		struct timeval tp;
1106 		GET_TIME(tp);
1107 #endif
1108 
1109 		mrt6stat.mrt6s_no_route++;
1110 #ifdef MRT6DEBUG
1111 		if (mrt6debug & (DEBUG_FORWARD | DEBUG_MFC))
1112 			log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n",
1113 			    IN6_PRINT(ip6bufs, &ip6->ip6_src),
1114 			    IN6_PRINT(ip6bufd, &ip6->ip6_dst));
1115 #endif
1116 
1117 		/*
1118 		 * Allocate mbufs early so that we don't do extra work if we
1119 		 * are just going to fail anyway.
1120 		 */
1121 		rte = malloc(sizeof(*rte), M_MRTABLE, M_NOWAIT);
1122 		if (rte == NULL) {
1123 			splx(s);
1124 			return ENOBUFS;
1125 		}
1126 		mb0 = m_copypacket(m, M_DONTWAIT);
1127 
1128 		/*
1129 		 * Pullup packet header if needed before storing it,
1130 		 * as other references may modify it in the meantime.
1131 		 */
1132 		if (mb0 && M_UNWRITABLE(mb0, sizeof(struct ip6_hdr)))
1133 			mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
1134 		if (mb0 == NULL) {
1135 			free(rte, M_MRTABLE);
1136 			splx(s);
1137 			return ENOBUFS;
1138 		}
1139 
1140 		/* is there an upcall waiting for this packet? */
1141 		hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
1142 		for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
1143 			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
1144 					       &rt->mf6c_origin.sin6_addr) &&
1145 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1146 					       &rt->mf6c_mcastgrp.sin6_addr) &&
1147 			    (rt->mf6c_stall != NULL))
1148 				break;
1149 		}
1150 
1151 		if (rt == NULL) {
1152 			struct mrt6msg *im;
1153 			struct omrt6msg *oim;
1154 
1155 			/* no upcall, so make a new entry */
1156 			rt = malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1157 			if (rt == NULL) {
1158 				free(rte, M_MRTABLE);
1159 				m_freem(mb0);
1160 				splx(s);
1161 				return ENOBUFS;
1162 			}
1163 
1164 			/*
1165 			 * Make a copy of the header to send to the user
1166 			 * level process
1167 			 */
1168 			mm = m_copym(mb0, 0, sizeof(struct ip6_hdr), M_DONTWAIT);
1169 
1170 			if (mm == NULL) {
1171 				free(rte, M_MRTABLE);
1172 				m_freem(mb0);
1173 				free(rt, M_MRTABLE);
1174 				splx(s);
1175 				return ENOBUFS;
1176 			}
1177 
1178 			/*
1179 			 * Send message to routing daemon
1180 			 */
1181 			sockaddr_in6_init(&sin6, &ip6->ip6_src, 0, 0, 0);
1182 
1183 			im = NULL;
1184 			oim = NULL;
1185 			switch (ip6_mrouter_ver) {
1186 			case MRT6_OINIT:
1187 				oim = mtod(mm, struct omrt6msg *);
1188 				oim->im6_msgtype = MRT6MSG_NOCACHE;
1189 				oim->im6_mbz = 0;
1190 				break;
1191 			case MRT6_INIT:
1192 				im = mtod(mm, struct mrt6msg *);
1193 				im->im6_msgtype = MRT6MSG_NOCACHE;
1194 				im->im6_mbz = 0;
1195 				break;
1196 			default:
1197 				free(rte, M_MRTABLE);
1198 				m_freem(mb0);
1199 				free(rt, M_MRTABLE);
1200 				splx(s);
1201 				return EINVAL;
1202 			}
1203 
1204 #ifdef MRT6DEBUG
1205 			if (mrt6debug & DEBUG_FORWARD)
1206 				log(LOG_DEBUG,
1207 				    "getting the iif info in the kernel\n");
1208 #endif
1209 
1210 			for (mifp = mif6table, mifi = 0;
1211 			     mifi < nummifs && mifp->m6_ifp != ifp;
1212 			     mifp++, mifi++)
1213 				;
1214 
1215 			switch (ip6_mrouter_ver) {
1216 			case MRT6_OINIT:
1217 				oim->im6_mif = mifi;
1218 				break;
1219 			case MRT6_INIT:
1220 				im->im6_mif = mifi;
1221 				break;
1222 			}
1223 
1224 			if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1225 				log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
1226 				    "socket queue full\n");
1227 				mrt6stat.mrt6s_upq_sockfull++;
1228 				free(rte, M_MRTABLE);
1229 				m_freem(mb0);
1230 				free(rt, M_MRTABLE);
1231 				splx(s);
1232 				return ENOBUFS;
1233 			}
1234 
1235 			mrt6stat.mrt6s_upcalls++;
1236 
1237 			/* insert new entry at head of hash chain */
1238 			memset(rt, 0, sizeof(*rt));
1239 			sockaddr_in6_init(&rt->mf6c_origin, &ip6->ip6_src,
1240 			    0, 0, 0);
1241 			sockaddr_in6_init(&rt->mf6c_mcastgrp, &ip6->ip6_dst,
1242 			    0, 0, 0);
1243 			rt->mf6c_expire = UPCALL_EXPIRE;
1244 			n6expire[hash]++;
1245 			rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
1246 
1247 			/* link into table */
1248 			rt->mf6c_next  = mf6ctable[hash];
1249 			mf6ctable[hash] = rt;
1250 			/* Add this entry to the end of the queue */
1251 			rt->mf6c_stall = rte;
1252 		} else {
1253 			/* determine if q has overflowed */
1254 			struct rtdetq **p;
1255 			int npkts = 0;
1256 
1257 			for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next) {
1258 				if (++npkts > MAX_UPQ6) {
1259 					mrt6stat.mrt6s_upq_ovflw++;
1260 					free(rte, M_MRTABLE);
1261 					m_freem(mb0);
1262 					splx(s);
1263 					return 0;
1264 				}
1265 			}
1266 
1267 			/* Add this entry to the end of the queue */
1268 			*p = rte;
1269 		}
1270 
1271 		rte->next = NULL;
1272 		rte->m = mb0;
1273 		rte->ifp = ifp;
1274 #ifdef UPCALL_TIMING
1275 		rte->t = tp;
1276 #endif
1277 
1278 		splx(s);
1279 
1280 		return 0;
1281 	}
1282 }
1283 
1284 /*
1285  * Clean up cache entries if upcalls are not serviced
1286  * Call from the Slow Timeout mechanism, every 0.25 seconds.
1287  */
1288 static void
1289 expire_upcalls(void *unused)
1290 {
1291 	struct rtdetq *rte;
1292 	struct mf6c *mfc, **nptr;
1293 	int i;
1294 
1295 	/* XXX NOMPSAFE still need softnet_lock */
1296 	mutex_enter(softnet_lock);
1297 	KERNEL_LOCK(1, NULL);
1298 
1299 	for (i = 0; i < MF6CTBLSIZ; i++) {
1300 		if (n6expire[i] == 0)
1301 			continue;
1302 		nptr = &mf6ctable[i];
1303 		while ((mfc = *nptr) != NULL) {
1304 			rte = mfc->mf6c_stall;
1305 			/*
1306 			 * Skip real cache entries
1307 			 * Make sure it wasn't marked to not expire (shouldn't happen)
1308 			 * If it expires now
1309 			 */
1310 			if (rte != NULL &&
1311 			    mfc->mf6c_expire != 0 &&
1312 			    --mfc->mf6c_expire == 0) {
1313 #ifdef MRT6DEBUG
1314 				if (mrt6debug & DEBUG_EXPIRE) {
1315 					char ip6bufo[INET6_ADDRSTRLEN];
1316 					char ip6bufm[INET6_ADDRSTRLEN];
1317 					log(LOG_DEBUG,
1318 					    "expire_upcalls: expiring (%s %s)\n",
1319 					    IN6_PRINT(ip6bufo,
1320 					    &mfc->mf6c_origin.sin6_addr),
1321 					    IN6_PRINT(ip6bufm,
1322 					    &mfc->mf6c_mcastgrp.sin6_addr));
1323 				}
1324 #endif
1325 				/*
1326 				 * drop all the packets
1327 				 * free the mbuf with the pkt, if, timing info
1328 				 */
1329 				do {
1330 					struct rtdetq *n = rte->next;
1331 					m_freem(rte->m);
1332 					free(rte, M_MRTABLE);
1333 					rte = n;
1334 				} while (rte != NULL);
1335 				mrt6stat.mrt6s_cache_cleanups++;
1336 				n6expire[i]--;
1337 
1338 				*nptr = mfc->mf6c_next;
1339 				free(mfc, M_MRTABLE);
1340 			} else {
1341 				nptr = &mfc->mf6c_next;
1342 			}
1343 		}
1344 	}
1345 	callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
1346 	    expire_upcalls, NULL);
1347 
1348 	KERNEL_UNLOCK_ONE(NULL);
1349 	mutex_exit(softnet_lock);
1350 }
1351 
1352 /*
1353  * Macro to send packet on mif.  Since RSVP packets don't get counted on
1354  * input, they shouldn't get counted on output, so statistics keeping is
1355  * separate.
1356  */
1357 #define MC6_SEND(ip6, mifp, m) do {				\
1358 	if ((mifp)->m6_flags & MIFF_REGISTER)			\
1359 		register_send((ip6), (mifp), (m));		\
1360 	else							\
1361 		phyint_send((ip6), (mifp), (m));		\
1362 } while (/*CONSTCOND*/ 0)
1363 
1364 /*
1365  * Packet forwarding routine once entry in the cache is made
1366  */
1367 static int
1368 ip6_mdq(struct mbuf *m, struct ifnet *ifp, struct mf6c *rt)
1369 {
1370 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1371 	mifi_t mifi, iif;
1372 	struct mif6 *mifp;
1373 	int plen = m->m_pkthdr.len;
1374 	struct in6_addr src0, dst0; /* copies for local work */
1375 	u_int32_t iszone, idzone, oszone, odzone;
1376 	int error = 0;
1377 
1378 	/*
1379 	 * Don't forward if it didn't arrive from the parent mif
1380 	 * for its origin.
1381 	 */
1382 	mifi = rt->mf6c_parent;
1383 	if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
1384 		/* came in the wrong interface */
1385 #ifdef MRT6DEBUG
1386 		if (mrt6debug & DEBUG_FORWARD)
1387 			log(LOG_DEBUG,
1388 			    "wrong if: ifid %d mifi %d mififid %x\n",
1389 			    ifp->if_index, mifi,
1390 			    mif6table[mifi].m6_ifp ?
1391 			    mif6table[mifi].m6_ifp->if_index : -1);
1392 #endif
1393 		mrt6stat.mrt6s_wrong_if++;
1394 		rt->mf6c_wrong_if++;
1395 
1396 		/*
1397 		 * If we are doing PIM processing, and we are forwarding
1398 		 * packets on this interface, send a message to the
1399 		 * routing daemon.
1400 		 */
1401 		/* have to make sure this is a valid mif */
1402 		if (mifi < nummifs && mif6table[mifi].m6_ifp) {
1403 			if (pim6 && (m->m_flags & M_LOOP) == 0) {
1404 				/*
1405 				 * Check the M_LOOP flag to avoid an
1406 				 * unnecessary PIM assert.
1407 				 * XXX: M_LOOP is an ad-hoc hack...
1408 				 */
1409 				struct sockaddr_in6 sin6;
1410 
1411 				struct mbuf *mm;
1412 				struct mrt6msg *im;
1413 				struct omrt6msg *oim;
1414 
1415 				mm = m_copym(m, 0, sizeof(struct ip6_hdr), M_DONTWAIT);
1416 				if (mm && M_UNWRITABLE(mm, sizeof(struct ip6_hdr)))
1417 					mm = m_pullup(mm, sizeof(struct ip6_hdr));
1418 				if (mm == NULL)
1419 					return ENOBUFS;
1420 
1421 				oim = NULL;
1422 				im = NULL;
1423 				switch (ip6_mrouter_ver) {
1424 				case MRT6_OINIT:
1425 					oim = mtod(mm, struct omrt6msg *);
1426 					oim->im6_msgtype = MRT6MSG_WRONGMIF;
1427 					oim->im6_mbz = 0;
1428 					break;
1429 				case MRT6_INIT:
1430 					im = mtod(mm, struct mrt6msg *);
1431 					im->im6_msgtype = MRT6MSG_WRONGMIF;
1432 					im->im6_mbz = 0;
1433 					break;
1434 				default:
1435 					m_freem(mm);
1436 					return EINVAL;
1437 				}
1438 
1439 				for (mifp = mif6table, iif = 0;
1440 				     iif < nummifs && mifp &&
1441 					     mifp->m6_ifp != ifp;
1442 				     mifp++, iif++)
1443 					;
1444 
1445 				memset(&sin6, 0, sizeof(sin6));
1446 				sin6.sin6_len = sizeof(sin6);
1447 				sin6.sin6_family = AF_INET6;
1448 				switch (ip6_mrouter_ver) {
1449 				case MRT6_OINIT:
1450 					oim->im6_mif = iif;
1451 					sin6.sin6_addr = oim->im6_src;
1452 					break;
1453 				case MRT6_INIT:
1454 					im->im6_mif = iif;
1455 					sin6.sin6_addr = im->im6_src;
1456 					break;
1457 				}
1458 
1459 				mrt6stat.mrt6s_upcalls++;
1460 
1461 				if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1462 #ifdef MRT6DEBUG
1463 					if (mrt6debug)
1464 						log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n");
1465 #endif
1466 					++mrt6stat.mrt6s_upq_sockfull;
1467 					return ENOBUFS;
1468 				}
1469 			}
1470 		}
1471 
1472 		return 0;
1473 	}
1474 
1475 	/* If I sourced this packet, it counts as output, else it was input. */
1476 	if (m->m_pkthdr.rcvif_index == 0) {
1477 		/* XXX: is rcvif really NULL when output?? */
1478 		mif6table[mifi].m6_pkt_out++;
1479 		mif6table[mifi].m6_bytes_out += plen;
1480 	} else {
1481 		mif6table[mifi].m6_pkt_in++;
1482 		mif6table[mifi].m6_bytes_in += plen;
1483 	}
1484 	rt->mf6c_pkt_cnt++;
1485 	rt->mf6c_byte_cnt += plen;
1486 
1487 	/*
1488 	 * For each mif, forward a copy of the packet if there are group
1489 	 * members downstream on the interface.
1490 	 */
1491 	src0 = ip6->ip6_src;
1492 	dst0 = ip6->ip6_dst;
1493 	if ((error = in6_setscope(&src0, ifp, &iszone)) != 0 ||
1494 	    (error = in6_setscope(&dst0, ifp, &idzone)) != 0) {
1495 		IP6_STATINC(IP6_STAT_BADSCOPE);
1496 		return error;
1497 	}
1498 	for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++) {
1499 		if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
1500 			if (mif6table[mifi].m6_ifp == NULL)
1501 				continue;
1502 			/*
1503 			 * check if the outgoing packet is going to break
1504 			 * a scope boundary.
1505 			 * XXX: For packets through PIM register tunnel
1506 			 * interface, we believe the routing daemon.
1507 			 */
1508 			if ((mif6table[rt->mf6c_parent].m6_flags &
1509 			     MIFF_REGISTER) == 0 &&
1510 			    (mif6table[mifi].m6_flags & MIFF_REGISTER) == 0) {
1511 				if (in6_setscope(&src0, mif6table[mifi].m6_ifp,
1512 				    &oszone) ||
1513 				    in6_setscope(&dst0, mif6table[mifi].m6_ifp,
1514 				    &odzone) ||
1515 				    iszone != oszone || idzone != odzone) {
1516 					IP6_STATINC(IP6_STAT_BADSCOPE);
1517 					continue;
1518 				}
1519 			}
1520 
1521 			mifp->m6_pkt_out++;
1522 			mifp->m6_bytes_out += plen;
1523 			MC6_SEND(ip6, mifp, m);
1524 		}
1525 	}
1526 
1527 	return 0;
1528 }
1529 
1530 static void
1531 phyint_send(struct ip6_hdr *ip6, struct mif6 *mifp, struct mbuf *m)
1532 {
1533 	struct mbuf *mb_copy;
1534 	struct ifnet *ifp = mifp->m6_ifp;
1535 	int error __mrt6debugused = 0;
1536 	int s;
1537 	static struct route ro;
1538 	bool ingroup;
1539 	struct sockaddr_in6 dst6;
1540 	u_long linkmtu;
1541 
1542 	s = splsoftnet();
1543 
1544 	/*
1545 	 * Make a new reference to the packet; make sure that
1546 	 * the IPv6 header is actually copied, not just referenced,
1547 	 * so that ip6_output() only scribbles on the copy.
1548 	 */
1549 	mb_copy = m_copypacket(m, M_DONTWAIT);
1550 	if (mb_copy && M_UNWRITABLE(mb_copy, sizeof(struct ip6_hdr)))
1551 		mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr));
1552 	if (mb_copy == NULL) {
1553 		splx(s);
1554 		return;
1555 	}
1556 
1557 	/* set MCAST flag to the outgoing packet */
1558 	mb_copy->m_flags |= M_MCAST;
1559 
1560 	/*
1561 	 * If we sourced the packet, call ip6_output since we may divide
1562 	 * the packet into fragments when the packet is too big for the
1563 	 * outgoing interface.
1564 	 * Otherwise, we can simply send the packet to the interface
1565 	 * sending queue.
1566 	 */
1567 	if (m->m_pkthdr.rcvif_index == 0) {
1568 		struct ip6_moptions im6o;
1569 
1570 		im6o.im6o_multicast_if_index = if_get_index(ifp);
1571 		/* XXX: ip6_output will override ip6->ip6_hlim */
1572 		im6o.im6o_multicast_hlim = ip6->ip6_hlim;
1573 		im6o.im6o_multicast_loop = 1;
1574 		error = ip6_output(mb_copy, NULL, &ro, IPV6_FORWARDING,
1575 		    &im6o, NULL, NULL);
1576 
1577 #ifdef MRT6DEBUG
1578 		if (mrt6debug & DEBUG_XMIT)
1579 			log(LOG_DEBUG, "phyint_send on mif %td err %d\n",
1580 			    mifp - mif6table, error);
1581 #endif
1582 		splx(s);
1583 		return;
1584 	}
1585 
1586 	/*
1587 	 * If we belong to the destination multicast group
1588 	 * on the outgoing interface, loop back a copy.
1589 	 */
1590 	/*
1591 	 * Does not have to check source info, as it's alreay covered by
1592 	 * ip6_input
1593 	 */
1594 	sockaddr_in6_init(&dst6, &ip6->ip6_dst, 0, 0, 0);
1595 
1596 	ingroup = in6_multi_group(&ip6->ip6_dst, ifp);
1597 	if (ingroup) {
1598 		ip6_mloopback(ifp, m,
1599 		    satocsin6(rtcache_getdst(&ro)));
1600 	}
1601 
1602 	/*
1603 	 * Put the packet into the sending queue of the outgoing interface
1604 	 * if it would fit in the MTU of the interface.
1605 	 */
1606 	linkmtu = IN6_LINKMTU(ifp);
1607 	if (mb_copy->m_pkthdr.len <= linkmtu || linkmtu < IPV6_MMTU) {
1608 		error = ip6_if_output(ifp, ifp, mb_copy, &dst6, NULL);
1609 #ifdef MRT6DEBUG
1610 		if (mrt6debug & DEBUG_XMIT)
1611 			log(LOG_DEBUG, "phyint_send on mif %td err %d\n",
1612 			    mifp - mif6table, error);
1613 #endif
1614 	} else {
1615 		/*
1616 		 * pMTU discovery is intentionally disabled by default, since
1617 		 * various routers may notify pMTU in multicast, which can be
1618 		 * a DDoS to a router.
1619 		 */
1620 		if (ip6_mcast_pmtu) {
1621 			icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, linkmtu);
1622 		} else {
1623 			/* simply discard the packet */
1624 #ifdef MRT6DEBUG
1625 			if (mrt6debug & DEBUG_XMIT) {
1626 				char ip6bufs[INET6_ADDRSTRLEN];
1627 				char ip6bufd[INET6_ADDRSTRLEN];
1628 				log(LOG_DEBUG,
1629 				    "phyint_send: packet too big on %s o %s g %s"
1630 				    " size %d(discarded)\n",
1631 				    if_name(ifp),
1632 				    IN6_PRINT(ip6bufs, &ip6->ip6_src),
1633 				    IN6_PRINT(ip6bufd, &ip6->ip6_dst),
1634 				    mb_copy->m_pkthdr.len);
1635 			}
1636 #endif
1637 			m_freem(mb_copy);
1638 		}
1639 	}
1640 
1641 	splx(s);
1642 }
1643 
1644 static int
1645 register_send(struct ip6_hdr *ip6, struct mif6 *mif, struct mbuf *m)
1646 {
1647 	struct mbuf *mm;
1648 	int i, len = m->m_pkthdr.len;
1649 	struct sockaddr_in6 sin6;
1650 	struct mrt6msg *im6;
1651 
1652 #ifdef MRT6DEBUG
1653 	if (mrt6debug) {
1654 		char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1655 		log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n",
1656 		    IN6_PRINT(ip6bufs, &ip6->ip6_src),
1657 		    IN6_PRINT(ip6bufd, &ip6->ip6_dst));
1658 	}
1659 #endif
1660 	PIM6_STATINC(PIM6_STAT_SND_REGISTERS);
1661 
1662 	/* Make a copy of the packet to send to the user level process */
1663 	MGETHDR(mm, M_DONTWAIT, MT_HEADER);
1664 	if (mm == NULL)
1665 		return ENOBUFS;
1666 	mm->m_data += max_linkhdr;
1667 	mm->m_len = sizeof(struct ip6_hdr);
1668 
1669 	if ((mm->m_next = m_copypacket(m, M_DONTWAIT)) == NULL) {
1670 		m_freem(mm);
1671 		return ENOBUFS;
1672 	}
1673 	i = MHLEN - M_LEADINGSPACE(mm);
1674 	if (i > len)
1675 		i = len;
1676 	mm = m_pullup(mm, i);
1677 	if (mm == NULL)
1678 		return ENOBUFS;
1679 	mm->m_pkthdr.len = len + sizeof(struct ip6_hdr);
1680 
1681 	/*
1682 	 * Send message to routing daemon
1683 	 */
1684 	sockaddr_in6_init(&sin6, &ip6->ip6_src, 0, 0, 0);
1685 
1686 	im6 = mtod(mm, struct mrt6msg *);
1687 	im6->im6_msgtype = MRT6MSG_WHOLEPKT;
1688 	im6->im6_mbz = 0;
1689 	im6->im6_mif = mif - mif6table;
1690 
1691 	/* iif info is not given for reg. encap.n */
1692 	mrt6stat.mrt6s_upcalls++;
1693 
1694 	if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1695 #ifdef MRT6DEBUG
1696 		if (mrt6debug)
1697 			log(LOG_WARNING,
1698 			    "register_send: ip6_mrouter socket queue full\n");
1699 #endif
1700 		++mrt6stat.mrt6s_upq_sockfull;
1701 		return ENOBUFS;
1702 	}
1703 
1704 	return 0;
1705 }
1706 
1707 /*
1708  * PIM sparse mode hook. Receives the pim control messages, and passes them up
1709  * to the listening socket, using rip6_input.
1710  *
1711  * The only message processed is the REGISTER pim message; the pim header
1712  * is stripped off, and the inner packet is passed to register_mforward.
1713  */
1714 int
1715 pim6_input(struct mbuf **mp, int *offp, int proto)
1716 {
1717 	struct pim *pim;
1718 	struct ip6_hdr *ip6 __mrt6debugused;
1719 	int pimlen;
1720 	struct mbuf *m = *mp;
1721 	int minlen;
1722 	int off = *offp;
1723 
1724 	PIM6_STATINC(PIM6_STAT_RCV_TOTAL);
1725 
1726 	ip6 = mtod(m, struct ip6_hdr *);
1727 	pimlen = m->m_pkthdr.len - off;
1728 
1729 	/*
1730 	 * Validate lengths
1731 	 */
1732 	if (pimlen < PIM_MINLEN) {
1733 		PIM6_STATINC(PIM6_STAT_RCV_TOOSHORT);
1734 #ifdef MRT6DEBUG
1735 		if (mrt6debug & DEBUG_PIM)
1736 			log(LOG_DEBUG,"pim6_input: PIM packet too short\n");
1737 #endif
1738 		m_freem(m);
1739 		return IPPROTO_DONE;
1740 	}
1741 
1742 	/*
1743 	 * If the packet is at least as big as a REGISTER, go ahead
1744 	 * and grab the PIM REGISTER header size, to avoid another
1745 	 * possible m_pullup() later.
1746 	 *
1747 	 * PIM_MINLEN       == pimhdr + u_int32 == 8
1748 	 * PIM6_REG_MINLEN   == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1749 	 */
1750 	minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN;
1751 
1752 	/*
1753 	 * Make sure that the IP6 and PIM headers in contiguous memory, and
1754 	 * possibly the PIM REGISTER header
1755 	 */
1756 	IP6_EXTHDR_GET(pim, struct pim *, m, off, minlen);
1757 	if (pim == NULL) {
1758 		PIM6_STATINC(PIM6_STAT_RCV_TOOSHORT);
1759 		return IPPROTO_DONE;
1760 	}
1761 
1762 	/* PIM version check */
1763 	if (pim->pim_ver != PIM_VERSION) {
1764 		PIM6_STATINC(PIM6_STAT_RCV_BADVERSION);
1765 #ifdef MRT6DEBUG
1766 		log(LOG_ERR,
1767 		    "pim6_input: incorrect version %d, expecting %d\n",
1768 		    pim->pim_ver, PIM_VERSION);
1769 #endif
1770 		m_freem(m);
1771 		return IPPROTO_DONE;
1772 	}
1773 
1774 #define PIM6_CHECKSUM
1775 #ifdef PIM6_CHECKSUM
1776 	{
1777 		int cksumlen;
1778 
1779 		/*
1780 		 * Validate checksum.
1781 		 * If PIM REGISTER, exclude the data packet
1782 		 */
1783 		if (pim->pim_type == PIM_REGISTER)
1784 			cksumlen = PIM_MINLEN;
1785 		else
1786 			cksumlen = pimlen;
1787 
1788 		if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) {
1789 			PIM6_STATINC(PIM6_STAT_RCV_BADSUM);
1790 #ifdef MRT6DEBUG
1791 			if (mrt6debug & DEBUG_PIM)
1792 				log(LOG_DEBUG,
1793 				    "pim6_input: invalid checksum\n");
1794 #endif
1795 			m_freem(m);
1796 			return IPPROTO_DONE;
1797 		}
1798 	}
1799 #endif /* PIM_CHECKSUM */
1800 
1801 	if (pim->pim_type == PIM_REGISTER) {
1802 		/*
1803 		 * since this is a REGISTER, we'll make a copy of the register
1804 		 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1805 		 * routing daemon.
1806 		 */
1807 		static const struct sockaddr_in6 dst = {
1808 			.sin6_len = sizeof(dst),
1809 			.sin6_family = AF_INET6,
1810 		};
1811 
1812 		struct mbuf *mcp;
1813 		struct ip6_hdr *eip6;
1814 		u_int32_t *reghdr;
1815 
1816 		PIM6_STATINC(PIM6_STAT_RCV_REGISTERS);
1817 
1818 		if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) {
1819 #ifdef MRT6DEBUG
1820 			if (mrt6debug & DEBUG_PIM)
1821 				log(LOG_DEBUG,
1822 				    "pim6_input: register mif not set: %d\n",
1823 				    reg_mif_num);
1824 #endif
1825 			m_freem(m);
1826 			return IPPROTO_DONE;
1827 		}
1828 
1829 		reghdr = (u_int32_t *)(pim + 1);
1830 
1831 		if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
1832 			goto pim6_input_to_daemon;
1833 
1834 		/*
1835 		 * Validate length
1836 		 */
1837 		if (pimlen < PIM6_REG_MINLEN) {
1838 #ifdef MRT6DEBUG
1839 			char ip6buf[INET6_ADDRSTRLEN];
1840 			log(LOG_ERR,
1841 			    "pim6_input: register packet size too "
1842 			    "small %d from %s\n",
1843 			    pimlen, IN6_PRINT(ip6buf, &ip6->ip6_src));
1844 #endif
1845 			PIM6_STATINC(PIM6_STAT_RCV_TOOSHORT);
1846 			PIM6_STATINC(PIM6_STAT_RCV_BADREGISTERS);
1847 			m_freem(m);
1848 			return IPPROTO_DONE;
1849 		}
1850 
1851 		eip6 = (struct ip6_hdr *)(reghdr + 1);
1852 #ifdef MRT6DEBUG
1853 		if (mrt6debug & DEBUG_PIM) {
1854 			char ip6bufs[INET6_ADDRSTRLEN];
1855 			char ip6bufd[INET6_ADDRSTRLEN];
1856 			log(LOG_DEBUG,
1857 			    "pim6_input[register], eip6: %s -> %s, "
1858 			    "eip6 plen %d\n",
1859 			    IN6_PRINT(ip6bufs, &eip6->ip6_src),
1860 			    IN6_PRINT(ip6bufd, &eip6->ip6_dst),
1861 			    ntohs(eip6->ip6_plen));
1862 		}
1863 #endif
1864 
1865 		/* verify the version number of the inner packet */
1866 		if ((eip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1867 			PIM6_STATINC(PIM6_STAT_RCV_BADREGISTERS);
1868 #ifdef MRT6DEBUG
1869 			log(LOG_DEBUG, "pim6_input: invalid IP version (%d) "
1870 			    "of the inner packet\n",
1871 			    (eip6->ip6_vfc & IPV6_VERSION));
1872 #endif
1873 			m_freem(m);
1874 			return IPPROTO_DONE;
1875 		}
1876 
1877 		/* verify the inner packet is destined to a mcast group */
1878 		if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) {
1879 			PIM6_STATINC(PIM6_STAT_RCV_BADREGISTERS);
1880 #ifdef MRT6DEBUG
1881 			if (mrt6debug & DEBUG_PIM) {
1882 				char ip6buf[INET6_ADDRSTRLEN];
1883 				log(LOG_DEBUG,
1884 				    "pim6_input: inner packet of register "
1885 				    "is not multicast %s\n",
1886 				    IN6_PRINT(ip6buf, &eip6->ip6_dst));
1887 			}
1888 #endif
1889 			m_freem(m);
1890 			return IPPROTO_DONE;
1891 		}
1892 
1893 		/*
1894 		 * make a copy of the whole header to pass to the daemon later.
1895 		 */
1896 		mcp = m_copym(m, 0, off + PIM6_REG_MINLEN, M_DONTWAIT);
1897 		if (mcp == NULL) {
1898 #ifdef MRT6DEBUG
1899 			log(LOG_ERR,
1900 			    "pim6_input: pim register: "
1901 			    "could not copy register head\n");
1902 #endif
1903 			m_freem(m);
1904 			return IPPROTO_DONE;
1905 		}
1906 
1907 		/*
1908 		 * forward the inner ip6 packet; point m_data at the inner ip6.
1909 		 */
1910 		m_adj(m, off + PIM_MINLEN);
1911 #ifdef MRT6DEBUG
1912 		if (mrt6debug & DEBUG_PIM) {
1913 			char ip6bufs[INET6_ADDRSTRLEN];
1914 			char ip6bufd[INET6_ADDRSTRLEN];
1915 			log(LOG_DEBUG,
1916 			    "pim6_input: forwarding decapsulated register: "
1917 			    "src %s, dst %s, mif %d\n",
1918 			    IN6_PRINT(ip6bufs, &eip6->ip6_src),
1919 			    IN6_PRINT(ip6bufd, &eip6->ip6_dst),
1920 			    reg_mif_num);
1921 		}
1922 #endif
1923 
1924 		looutput(mif6table[reg_mif_num].m6_ifp, m, sin6tocsa(&dst),
1925 		    NULL);
1926 
1927 		/* prepare the register head to send to the mrouting daemon */
1928 		m = mcp;
1929 	}
1930 
1931 	/*
1932 	 * Pass the PIM message up to the daemon; if it is a register message
1933 	 * pass the 'head' only up to the daemon. This includes the
1934 	 * encapsulator ip6 header, pim header, register header and the
1935 	 * encapsulated ip6 header.
1936 	 */
1937 pim6_input_to_daemon:
1938 	rip6_input(&m, offp, proto);
1939 	return IPPROTO_DONE;
1940 }
1941 
1942 static int
1943 sysctl_net_inet6_pim6_stats(SYSCTLFN_ARGS)
1944 {
1945 
1946 	return (NETSTAT_SYSCTL(pim6stat_percpu, PIM6_NSTATS));
1947 }
1948 
1949 static void
1950 sysctl_net_inet6_pim6_setup(struct sysctllog **clog)
1951 {
1952 
1953 	sysctl_createv(clog, 0, NULL, NULL,
1954 		       CTLFLAG_PERMANENT,
1955 		       CTLTYPE_NODE, "inet6", NULL,
1956 		       NULL, 0, NULL, 0,
1957 		       CTL_NET, PF_INET6, CTL_EOL);
1958 	sysctl_createv(clog, 0, NULL, NULL,
1959 		       CTLFLAG_PERMANENT,
1960 		       CTLTYPE_NODE, "pim6",
1961 		       SYSCTL_DESCR("PIMv6 settings"),
1962 		       NULL, 0, NULL, 0,
1963 		       CTL_NET, PF_INET6, IPPROTO_PIM, CTL_EOL);
1964 
1965 	sysctl_createv(clog, 0, NULL, NULL,
1966 		       CTLFLAG_PERMANENT,
1967 		       CTLTYPE_STRUCT, "stats",
1968 		       SYSCTL_DESCR("PIMv6 statistics"),
1969 		       sysctl_net_inet6_pim6_stats, 0, NULL, 0,
1970 		       CTL_NET, PF_INET6, IPPROTO_PIM, PIM6CTL_STATS,
1971 		       CTL_EOL);
1972 }
1973