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