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