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