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