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