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