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