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