Home | History | Annotate | Line # | Download | only in netinet6
ip6_mroute.c revision 1.94
      1 /*	$NetBSD: ip6_mroute.c,v 1.94 2008/05/22 01:06:39 dyoung 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.94 2008/05/22 01:06:39 dyoung 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, &ifr);
    541 			}
    542 		}
    543 	}
    544 #ifdef notyet
    545 	bzero((void *)qtable, sizeof(qtable));
    546 	bzero((void *)tbftable, sizeof(tbftable));
    547 #endif
    548 	bzero((void *)mif6table, sizeof(mif6table));
    549 	nummifs = 0;
    550 
    551 	pim6 = 0; /* used to stub out/in pim specific code */
    552 
    553 	callout_stop(&expire_upcalls_ch);
    554 
    555 	/*
    556 	 * Free all multicast forwarding cache entries.
    557 	 */
    558 	for (i = 0; i < MF6CTBLSIZ; i++) {
    559 		rt = mf6ctable[i];
    560 		while (rt) {
    561 			struct mf6c *frt;
    562 
    563 			for (rte = rt->mf6c_stall; rte != NULL; ) {
    564 				struct rtdetq *n = rte->next;
    565 
    566 				m_free(rte->m);
    567 				free(rte, M_MRTABLE);
    568 				rte = n;
    569 			}
    570 			frt = rt;
    571 			rt = rt->mf6c_next;
    572 			free(frt, M_MRTABLE);
    573 		}
    574 	}
    575 
    576 	bzero((void *)mf6ctable, sizeof(mf6ctable));
    577 
    578 	/*
    579 	 * Reset register interface
    580 	 */
    581 	if (reg_mif_num != (mifi_t)-1) {
    582 		if_detach(&multicast_register_if6);
    583 		reg_mif_num = (mifi_t)-1;
    584 	}
    585 
    586 	ip6_mrouter = NULL;
    587 	ip6_mrouter_ver = 0;
    588 
    589 	splx(s);
    590 
    591 #ifdef MRT6DEBUG
    592 	if (mrt6debug)
    593 		log(LOG_DEBUG, "ip6_mrouter_done\n");
    594 #endif
    595 
    596 	return 0;
    597 }
    598 
    599 void
    600 ip6_mrouter_detach(struct ifnet *ifp)
    601 {
    602 	struct rtdetq *rte;
    603 	struct mf6c *mfc;
    604 	mifi_t mifi;
    605 	int i;
    606 
    607 	if (ip6_mrouter == NULL)
    608 		return;
    609 
    610 	/*
    611 	 * Delete a mif which points to ifp.
    612 	 */
    613 	for (mifi = 0; mifi < nummifs; mifi++)
    614 		if (mif6table[mifi].m6_ifp == ifp)
    615 			del_m6if(&mifi);
    616 
    617 	/*
    618 	 * Clear rte->ifp of cache entries received on ifp.
    619 	 */
    620 	for (i = 0; i < MF6CTBLSIZ; i++) {
    621 		if (n6expire[i] == 0)
    622 			continue;
    623 
    624 		for (mfc = mf6ctable[i]; mfc != NULL; mfc = mfc->mf6c_next) {
    625 			for (rte = mfc->mf6c_stall; rte != NULL; rte = rte->next) {
    626 				if (rte->ifp == ifp)
    627 					rte->ifp = NULL;
    628 			}
    629 		}
    630 	}
    631 }
    632 
    633 
    634 /*
    635  * Add a mif to the mif table
    636  */
    637 static int
    638 add_m6if(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 		ifp = &multicast_register_if6;
    664 
    665 		if (reg_mif_num == (mifi_t)-1) {
    666 			strlcpy(ifp->if_xname, "register_mif",
    667 			    sizeof(ifp->if_xname));
    668 			ifp->if_flags |= IFF_LOOPBACK;
    669 			ifp->if_index = mifcp->mif6c_mifi;
    670 			reg_mif_num = mifcp->mif6c_mifi;
    671 			if_attach(ifp);
    672 		}
    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, &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\n",
    715 		    mifcp->mif6c_mifi, ifp->if_xname);
    716 #endif
    717 
    718 	return 0;
    719 }
    720 
    721 /*
    722  * Delete a mif from the mif table
    723  */
    724 static int
    725 del_m6if(mifi_t *mifip)
    726 {
    727 	struct mif6 *mifp = mif6table + *mifip;
    728 	mifi_t mifi;
    729 	struct ifnet *ifp;
    730 	struct in6_ifreq ifr;
    731 	int s;
    732 
    733 	if (*mifip >= nummifs)
    734 		return EINVAL;
    735 	if (mifp->m6_ifp == NULL)
    736 		return EINVAL;
    737 
    738 	s = splsoftnet();
    739 
    740 	if (!(mifp->m6_flags & MIFF_REGISTER)) {
    741 		/*
    742 		 * XXX: what if there is yet IPv4 multicast daemon
    743 		 *      using the interface?
    744 		 */
    745 		ifp = mifp->m6_ifp;
    746 
    747 		ifr.ifr_addr.sin6_family = AF_INET6;
    748 		ifr.ifr_addr.sin6_addr = in6addr_any;
    749 		(*ifp->if_ioctl)(ifp, SIOCDELMULTI, &ifr);
    750 	} else {
    751 		if (reg_mif_num != (mifi_t)-1) {
    752 			if_detach(&multicast_register_if6);
    753 			reg_mif_num = (mifi_t)-1;
    754 		}
    755 	}
    756 
    757 #ifdef notyet
    758 	bzero((void *)qtable[*mifip], sizeof(qtable[*mifip]));
    759 	bzero((void *)mifp->m6_tbf, sizeof(*(mifp->m6_tbf)));
    760 #endif
    761 	bzero((void *)mifp, sizeof (*mifp));
    762 
    763 	/* Adjust nummifs down */
    764 	for (mifi = nummifs; mifi > 0; mifi--)
    765 		if (mif6table[mifi - 1].m6_ifp)
    766 			break;
    767 	nummifs = mifi;
    768 
    769 	splx(s);
    770 
    771 #ifdef MRT6DEBUG
    772 	if (mrt6debug)
    773 		log(LOG_DEBUG, "del_m6if %d, nummifs %d\n", *mifip, nummifs);
    774 #endif
    775 
    776 	return 0;
    777 }
    778 
    779 /*
    780  * Add an mfc entry
    781  */
    782 static int
    783 add_m6fc(struct mf6cctl *mfccp)
    784 {
    785 	struct mf6c *rt;
    786 	u_long hash;
    787 	struct rtdetq *rte;
    788 	u_short nstl;
    789 	int s;
    790 
    791 	MF6CFIND(mfccp->mf6cc_origin.sin6_addr,
    792 		 mfccp->mf6cc_mcastgrp.sin6_addr, rt);
    793 
    794 	/* If an entry already exists, just update the fields */
    795 	if (rt) {
    796 #ifdef MRT6DEBUG
    797 		if (mrt6debug & DEBUG_MFC)
    798 			log(LOG_DEBUG,"add_m6fc update o %s g %s p %x\n",
    799 			    ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
    800 			    ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
    801 			    mfccp->mf6cc_parent);
    802 #endif
    803 
    804 		s = splsoftnet();
    805 		rt->mf6c_parent = mfccp->mf6cc_parent;
    806 		rt->mf6c_ifset = mfccp->mf6cc_ifset;
    807 		splx(s);
    808 		return 0;
    809 	}
    810 
    811 	/*
    812 	 * Find the entry for which the upcall was made and update
    813 	 */
    814 	s = splsoftnet();
    815 	hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr,
    816 			mfccp->mf6cc_mcastgrp.sin6_addr);
    817 	for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) {
    818 		if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
    819 				       &mfccp->mf6cc_origin.sin6_addr) &&
    820 		    IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
    821 				       &mfccp->mf6cc_mcastgrp.sin6_addr) &&
    822 		    (rt->mf6c_stall != NULL)) {
    823 
    824 			if (nstl++)
    825 				log(LOG_ERR,
    826 				    "add_m6fc: %s o %s g %s p %x dbx %p\n",
    827 				    "multiple kernel entries",
    828 				    ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
    829 				    ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
    830 				    mfccp->mf6cc_parent, rt->mf6c_stall);
    831 
    832 #ifdef MRT6DEBUG
    833 			if (mrt6debug & DEBUG_MFC)
    834 				log(LOG_DEBUG,
    835 				    "add_m6fc o %s g %s p %x dbg %p\n",
    836 				    ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
    837 				    ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
    838 				    mfccp->mf6cc_parent, rt->mf6c_stall);
    839 #endif
    840 
    841 			rt->mf6c_origin     = mfccp->mf6cc_origin;
    842 			rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
    843 			rt->mf6c_parent     = mfccp->mf6cc_parent;
    844 			rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
    845 			/* initialize pkt counters per src-grp */
    846 			rt->mf6c_pkt_cnt    = 0;
    847 			rt->mf6c_byte_cnt   = 0;
    848 			rt->mf6c_wrong_if   = 0;
    849 
    850 			rt->mf6c_expire = 0;	/* Don't clean this guy up */
    851 			n6expire[hash]--;
    852 
    853 			/* free packets Qed at the end of this entry */
    854 			for (rte = rt->mf6c_stall; rte != NULL; ) {
    855 				struct rtdetq *n = rte->next;
    856 				if (rte->ifp) {
    857 					ip6_mdq(rte->m, rte->ifp, rt);
    858 				}
    859 				m_freem(rte->m);
    860 #ifdef UPCALL_TIMING
    861 				collate(&(rte->t));
    862 #endif /* UPCALL_TIMING */
    863 				free(rte, M_MRTABLE);
    864 				rte = n;
    865 			}
    866 			rt->mf6c_stall = NULL;
    867 		}
    868 	}
    869 
    870 	/*
    871 	 * It is possible that an entry is being inserted without an upcall
    872 	 */
    873 	if (nstl == 0) {
    874 #ifdef MRT6DEBUG
    875 		if (mrt6debug & DEBUG_MFC)
    876 			log(LOG_DEBUG,
    877 			    "add_mfc no upcall h %ld o %s g %s p %x\n",
    878 			    hash,
    879 			    ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
    880 			    ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
    881 			    mfccp->mf6cc_parent);
    882 #endif
    883 
    884 		for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
    885 
    886 			if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
    887 					       &mfccp->mf6cc_origin.sin6_addr)&&
    888 			    IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
    889 					       &mfccp->mf6cc_mcastgrp.sin6_addr)) {
    890 
    891 				rt->mf6c_origin     = mfccp->mf6cc_origin;
    892 				rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
    893 				rt->mf6c_parent     = mfccp->mf6cc_parent;
    894 				rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
    895 				/* initialize pkt counters per src-grp */
    896 				rt->mf6c_pkt_cnt    = 0;
    897 				rt->mf6c_byte_cnt   = 0;
    898 				rt->mf6c_wrong_if   = 0;
    899 
    900 				if (rt->mf6c_expire)
    901 					n6expire[hash]--;
    902 				rt->mf6c_expire	   = 0;
    903 			}
    904 		}
    905 		if (rt == NULL) {
    906 			/* no upcall, so make a new entry */
    907 			rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE,
    908 						  M_NOWAIT);
    909 			if (rt == NULL) {
    910 				splx(s);
    911 				return ENOBUFS;
    912 			}
    913 
    914 			/* insert new entry at head of hash chain */
    915 			rt->mf6c_origin     = mfccp->mf6cc_origin;
    916 			rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
    917 			rt->mf6c_parent     = mfccp->mf6cc_parent;
    918 			rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
    919 			/* initialize pkt counters per src-grp */
    920 			rt->mf6c_pkt_cnt    = 0;
    921 			rt->mf6c_byte_cnt   = 0;
    922 			rt->mf6c_wrong_if   = 0;
    923 			rt->mf6c_expire     = 0;
    924 			rt->mf6c_stall = NULL;
    925 
    926 			/* link into table */
    927 			rt->mf6c_next  = mf6ctable[hash];
    928 			mf6ctable[hash] = rt;
    929 		}
    930 	}
    931 	splx(s);
    932 	return 0;
    933 }
    934 
    935 #ifdef UPCALL_TIMING
    936 /*
    937  * collect delay statistics on the upcalls
    938  */
    939 static void
    940 collate(struct timeval *t)
    941 {
    942 	u_long d;
    943 	struct timeval tp;
    944 	u_long delta;
    945 
    946 	GET_TIME(tp);
    947 
    948 	if (TV_LT(*t, tp))
    949 	{
    950 		TV_DELTA(tp, *t, delta);
    951 
    952 		d = delta >> 10;
    953 		if (d > UPCALL_MAX)
    954 			d = UPCALL_MAX;
    955 
    956 		++upcall_data[d];
    957 	}
    958 }
    959 #endif /* UPCALL_TIMING */
    960 
    961 /*
    962  * Delete an mfc entry
    963  */
    964 static int
    965 del_m6fc(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(struct socket *s, struct mbuf *mm, struct sockaddr_in6 *src)
   1013 {
   1014 	if (s) {
   1015 		if (sbappendaddr(&s->so_rcv,
   1016 				 (struct sockaddr *)src,
   1017 				 mm, (struct mbuf *)0) != 0) {
   1018 			sorwakeup(s);
   1019 			return 0;
   1020 		}
   1021 	}
   1022 	m_freem(mm);
   1023 	return -1;
   1024 }
   1025 
   1026 /*
   1027  * IPv6 multicast forwarding function. This function assumes that the packet
   1028  * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
   1029  * pointed to by "ifp", and the packet is to be relayed to other networks
   1030  * that have members of the packet's destination IPv6 multicast group.
   1031  *
   1032  * The packet is returned unscathed to the caller, unless it is
   1033  * erroneous, in which case a non-zero return value tells the caller to
   1034  * discard it.
   1035  */
   1036 
   1037 int
   1038 ip6_mforward(struct ip6_hdr *ip6, struct ifnet *ifp, struct mbuf *m)
   1039 {
   1040 	struct mf6c *rt;
   1041 	struct mif6 *mifp;
   1042 	struct mbuf *mm;
   1043 	int s;
   1044 	mifi_t mifi;
   1045 	struct sockaddr_in6 sin6;
   1046 
   1047 #ifdef MRT6DEBUG
   1048 	if (mrt6debug & DEBUG_FORWARD)
   1049 		log(LOG_DEBUG, "ip6_mforward: src %s, dst %s, ifindex %d\n",
   1050 		    ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst),
   1051 		    ifp->if_index);
   1052 #endif
   1053 
   1054 	/*
   1055 	 * Don't forward a packet with Hop limit of zero or one,
   1056 	 * or a packet destined to a local-only group.
   1057 	 */
   1058 	if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst) ||
   1059 	    IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
   1060 		return 0;
   1061 	ip6->ip6_hlim--;
   1062 
   1063 	/*
   1064 	 * Source address check: do not forward packets with unspecified
   1065 	 * source. It was discussed in July 2000, on ipngwg mailing list.
   1066 	 * This is rather more serious than unicast cases, because some
   1067 	 * MLD packets can be sent with the unspecified source address
   1068 	 * (although such packets must normally set the hop limit field to 1).
   1069 	 */
   1070 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
   1071 		IP6_STATINC(IP6_STAT_CANTFORWARD);
   1072 		if (ip6_log_time + ip6_log_interval < time_second) {
   1073 			ip6_log_time = time_second;
   1074 			log(LOG_DEBUG,
   1075 			    "cannot forward "
   1076 			    "from %s to %s nxt %d received on %s\n",
   1077 			    ip6_sprintf(&ip6->ip6_src),
   1078 			    ip6_sprintf(&ip6->ip6_dst),
   1079 			    ip6->ip6_nxt,
   1080 			    m->m_pkthdr.rcvif ?
   1081 			    if_name(m->m_pkthdr.rcvif) : "?");
   1082 		}
   1083 		return 0;
   1084 	}
   1085 
   1086 	/*
   1087 	 * Determine forwarding mifs from the forwarding cache table
   1088 	 */
   1089 	s = splsoftnet();
   1090 	MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
   1091 
   1092 	/* Entry exists, so forward if necessary */
   1093 	if (rt) {
   1094 		splx(s);
   1095 		return (ip6_mdq(m, ifp, rt));
   1096 	} else {
   1097 		/*
   1098 		 * If we don't have a route for packet's origin,
   1099 		 * Make a copy of the packet &
   1100 		 * send message to routing daemon
   1101 		 */
   1102 
   1103 		struct mbuf *mb0;
   1104 		struct rtdetq *rte;
   1105 		u_long hash;
   1106 /*		int i, npkts;*/
   1107 #ifdef UPCALL_TIMING
   1108 		struct timeval tp;
   1109 
   1110 		GET_TIME(tp);
   1111 #endif /* UPCALL_TIMING */
   1112 
   1113 		mrt6stat.mrt6s_no_route++;
   1114 #ifdef MRT6DEBUG
   1115 		if (mrt6debug & (DEBUG_FORWARD | DEBUG_MFC))
   1116 			log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n",
   1117 			    ip6_sprintf(&ip6->ip6_src),
   1118 			    ip6_sprintf(&ip6->ip6_dst));
   1119 #endif
   1120 
   1121 		/*
   1122 		 * Allocate mbufs early so that we don't do extra work if we
   1123 		 * are just going to fail anyway.
   1124 		 */
   1125 		rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE,
   1126 					      M_NOWAIT);
   1127 		if (rte == NULL) {
   1128 			splx(s);
   1129 			return ENOBUFS;
   1130 		}
   1131 		mb0 = m_copy(m, 0, M_COPYALL);
   1132 		/*
   1133 		 * Pullup packet header if needed before storing it,
   1134 		 * as other references may modify it in the meantime.
   1135 		 */
   1136 		if (mb0 &&
   1137 		    (M_READONLY(mb0) || mb0->m_len < sizeof(struct ip6_hdr)))
   1138 			mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
   1139 		if (mb0 == NULL) {
   1140 			free(rte, M_MRTABLE);
   1141 			splx(s);
   1142 			return ENOBUFS;
   1143 		}
   1144 
   1145 		/* is there an upcall waiting for this packet? */
   1146 		hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
   1147 		for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
   1148 			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
   1149 					       &rt->mf6c_origin.sin6_addr) &&
   1150 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
   1151 					       &rt->mf6c_mcastgrp.sin6_addr) &&
   1152 			    (rt->mf6c_stall != NULL))
   1153 				break;
   1154 		}
   1155 
   1156 		if (rt == NULL) {
   1157 			struct mrt6msg *im;
   1158 			struct omrt6msg *oim;
   1159 
   1160 			/* no upcall, so make a new entry */
   1161 			rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE,
   1162 						  M_NOWAIT);
   1163 			if (rt == NULL) {
   1164 				free(rte, M_MRTABLE);
   1165 				m_freem(mb0);
   1166 				splx(s);
   1167 				return ENOBUFS;
   1168 			}
   1169 			/*
   1170 			 * Make a copy of the header to send to the user
   1171 			 * level process
   1172 			 */
   1173 			mm = m_copy(mb0, 0, sizeof(struct ip6_hdr));
   1174 
   1175 			if (mm == NULL) {
   1176 				free(rte, M_MRTABLE);
   1177 				m_freem(mb0);
   1178 				free(rt, M_MRTABLE);
   1179 				splx(s);
   1180 				return ENOBUFS;
   1181 			}
   1182 
   1183 			/*
   1184 			 * Send message to routing daemon
   1185 			 */
   1186 			sockaddr_in6_init(&sin6, &ip6->ip6_src, 0, 0, 0);
   1187 
   1188 			im = NULL;
   1189 			oim = NULL;
   1190 			switch (ip6_mrouter_ver) {
   1191 			case MRT6_OINIT:
   1192 				oim = mtod(mm, struct omrt6msg *);
   1193 				oim->im6_msgtype = MRT6MSG_NOCACHE;
   1194 				oim->im6_mbz = 0;
   1195 				break;
   1196 			case MRT6_INIT:
   1197 				im = mtod(mm, struct mrt6msg *);
   1198 				im->im6_msgtype = MRT6MSG_NOCACHE;
   1199 				im->im6_mbz = 0;
   1200 				break;
   1201 			default:
   1202 				free(rte, M_MRTABLE);
   1203 				m_freem(mb0);
   1204 				free(rt, M_MRTABLE);
   1205 				splx(s);
   1206 				return EINVAL;
   1207 			}
   1208 
   1209 #ifdef MRT6DEBUG
   1210 			if (mrt6debug & DEBUG_FORWARD)
   1211 				log(LOG_DEBUG,
   1212 				    "getting the iif info in the kernel\n");
   1213 #endif
   1214 
   1215 			for (mifp = mif6table, mifi = 0;
   1216 			     mifi < nummifs && mifp->m6_ifp != ifp;
   1217 			     mifp++, mifi++)
   1218 				;
   1219 
   1220 			switch (ip6_mrouter_ver) {
   1221 			case MRT6_OINIT:
   1222 				oim->im6_mif = mifi;
   1223 				break;
   1224 			case MRT6_INIT:
   1225 				im->im6_mif = mifi;
   1226 				break;
   1227 			}
   1228 
   1229 			if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
   1230 				log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
   1231 				    "socket queue full\n");
   1232 				mrt6stat.mrt6s_upq_sockfull++;
   1233 				free(rte, M_MRTABLE);
   1234 				m_freem(mb0);
   1235 				free(rt, M_MRTABLE);
   1236 				splx(s);
   1237 				return ENOBUFS;
   1238 			}
   1239 
   1240 			mrt6stat.mrt6s_upcalls++;
   1241 
   1242 			/* insert new entry at head of hash chain */
   1243 			bzero(rt, sizeof(*rt));
   1244 			sockaddr_in6_init(&rt->mf6c_origin, &ip6->ip6_src,
   1245 			    0, 0, 0);
   1246 			sockaddr_in6_init(&rt->mf6c_mcastgrp, &ip6->ip6_dst,
   1247 			    0, 0, 0);
   1248 			rt->mf6c_expire = UPCALL_EXPIRE;
   1249 			n6expire[hash]++;
   1250 			rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
   1251 
   1252 			/* link into table */
   1253 			rt->mf6c_next  = mf6ctable[hash];
   1254 			mf6ctable[hash] = rt;
   1255 			/* Add this entry to the end of the queue */
   1256 			rt->mf6c_stall = rte;
   1257 		} else {
   1258 			/* determine if q has overflowed */
   1259 			struct rtdetq **p;
   1260 			int npkts = 0;
   1261 
   1262 			for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next)
   1263 				if (++npkts > MAX_UPQ6) {
   1264 					mrt6stat.mrt6s_upq_ovflw++;
   1265 					free(rte, M_MRTABLE);
   1266 					m_freem(mb0);
   1267 					splx(s);
   1268 					return 0;
   1269 				}
   1270 
   1271 			/* Add this entry to the end of the queue */
   1272 			*p = rte;
   1273 		}
   1274 
   1275 		rte->next = NULL;
   1276 		rte->m = mb0;
   1277 		rte->ifp = ifp;
   1278 #ifdef UPCALL_TIMING
   1279 		rte->t = tp;
   1280 #endif /* UPCALL_TIMING */
   1281 
   1282 		splx(s);
   1283 
   1284 		return 0;
   1285 	}
   1286 }
   1287 
   1288 /*
   1289  * Clean up cache entries if upcalls are not serviced
   1290  * Call from the Slow Timeout mechanism, every 0.25 seconds.
   1291  */
   1292 static void
   1293 expire_upcalls(void *unused)
   1294 {
   1295 	struct rtdetq *rte;
   1296 	struct mf6c *mfc, **nptr;
   1297 	int i;
   1298 
   1299 	mutex_enter(softnet_lock);
   1300 	KERNEL_LOCK(1, NULL);
   1301 
   1302 	for (i = 0; i < MF6CTBLSIZ; i++) {
   1303 		if (n6expire[i] == 0)
   1304 			continue;
   1305 		nptr = &mf6ctable[i];
   1306 		while ((mfc = *nptr) != NULL) {
   1307 			rte = mfc->mf6c_stall;
   1308 			/*
   1309 			 * Skip real cache entries
   1310 			 * Make sure it wasn't marked to not expire (shouldn't happen)
   1311 			 * If it expires now
   1312 			 */
   1313 			if (rte != NULL &&
   1314 			    mfc->mf6c_expire != 0 &&
   1315 			    --mfc->mf6c_expire == 0) {
   1316 #ifdef MRT6DEBUG
   1317 				if (mrt6debug & DEBUG_EXPIRE)
   1318 					log(LOG_DEBUG, "expire_upcalls: expiring (%s %s)\n",
   1319 					    ip6_sprintf(&mfc->mf6c_origin.sin6_addr),
   1320 					    ip6_sprintf(&mfc->mf6c_mcastgrp.sin6_addr));
   1321 #endif
   1322 				/*
   1323 				 * drop all the packets
   1324 				 * free the mbuf with the pkt, if, timing info
   1325 				 */
   1326 				do {
   1327 					struct rtdetq *n = rte->next;
   1328 					m_freem(rte->m);
   1329 					free(rte, M_MRTABLE);
   1330 					rte = n;
   1331 				} while (rte != NULL);
   1332 				mrt6stat.mrt6s_cache_cleanups++;
   1333 				n6expire[i]--;
   1334 
   1335 				*nptr = mfc->mf6c_next;
   1336 				free(mfc, M_MRTABLE);
   1337 			} else {
   1338 				nptr = &mfc->mf6c_next;
   1339 			}
   1340 		}
   1341 	}
   1342 	callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
   1343 	    expire_upcalls, NULL);
   1344 
   1345 	KERNEL_UNLOCK_ONE(NULL);
   1346 	mutex_exit(softnet_lock);
   1347 }
   1348 
   1349 /*
   1350  * Packet forwarding routine once entry in the cache is made
   1351  */
   1352 static int
   1353 ip6_mdq(struct mbuf *m, struct ifnet *ifp, struct mf6c *rt)
   1354 {
   1355 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
   1356 	mifi_t mifi, iif;
   1357 	struct mif6 *mifp;
   1358 	int plen = m->m_pkthdr.len;
   1359 	struct in6_addr src0, dst0; /* copies for local work */
   1360 	u_int32_t iszone, idzone, oszone, odzone;
   1361 	int error = 0;
   1362 
   1363 /*
   1364  * Macro to send packet on mif.  Since RSVP packets don't get counted on
   1365  * input, they shouldn't get counted on output, so statistics keeping is
   1366  * separate.
   1367  */
   1368 
   1369 #define MC6_SEND(ip6, mifp, m) do {				\
   1370 	if ((mifp)->m6_flags & MIFF_REGISTER)			\
   1371 		register_send((ip6), (mifp), (m));		\
   1372 	else							\
   1373 		phyint_send((ip6), (mifp), (m));		\
   1374 } while (/*CONSTCOND*/ 0)
   1375 
   1376 	/*
   1377 	 * Don't forward if it didn't arrive from the parent mif
   1378 	 * for its origin.
   1379 	 */
   1380 	mifi = rt->mf6c_parent;
   1381 	if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
   1382 		/* came in the wrong interface */
   1383 #ifdef MRT6DEBUG
   1384 		if (mrt6debug & DEBUG_FORWARD)
   1385 			log(LOG_DEBUG,
   1386 			    "wrong if: ifid %d mifi %d mififid %x\n",
   1387 			    ifp->if_index, mifi,
   1388 			    mif6table[mifi].m6_ifp ?
   1389 			    mif6table[mifi].m6_ifp->if_index : -1);
   1390 #endif
   1391 		mrt6stat.mrt6s_wrong_if++;
   1392 		rt->mf6c_wrong_if++;
   1393 		/*
   1394 		 * If we are doing PIM processing, and we are forwarding
   1395 		 * packets on this interface, send a message to the
   1396 		 * routing daemon.
   1397 		 */
   1398 		/* have to make sure this is a valid mif */
   1399 		if (mifi < nummifs && mif6table[mifi].m6_ifp)
   1400 			if (pim6 && (m->m_flags & M_LOOP) == 0) {
   1401 				/*
   1402 				 * Check the M_LOOP flag to avoid an
   1403 				 * unnecessary PIM assert.
   1404 				 * XXX: M_LOOP is an ad-hoc hack...
   1405 				 */
   1406 				struct sockaddr_in6 sin6;
   1407 
   1408 				struct mbuf *mm;
   1409 				struct mrt6msg *im;
   1410 				struct omrt6msg *oim;
   1411 
   1412 				mm = m_copy(m, 0, sizeof(struct ip6_hdr));
   1413 				if (mm &&
   1414 				    (M_READONLY(mm) ||
   1415 				     mm->m_len < sizeof(struct ip6_hdr)))
   1416 					mm = m_pullup(mm, sizeof(struct ip6_hdr));
   1417 				if (mm == NULL)
   1418 					return ENOBUFS;
   1419 
   1420 				oim = NULL;
   1421 				im = NULL;
   1422 				switch (ip6_mrouter_ver) {
   1423 				case MRT6_OINIT:
   1424 					oim = mtod(mm, struct omrt6msg *);
   1425 					oim->im6_msgtype = MRT6MSG_WRONGMIF;
   1426 					oim->im6_mbz = 0;
   1427 					break;
   1428 				case MRT6_INIT:
   1429 					im = mtod(mm, struct mrt6msg *);
   1430 					im->im6_msgtype = MRT6MSG_WRONGMIF;
   1431 					im->im6_mbz = 0;
   1432 					break;
   1433 				default:
   1434 					m_freem(mm);
   1435 					return EINVAL;
   1436 				}
   1437 
   1438 				for (mifp = mif6table, iif = 0;
   1439 				     iif < nummifs && mifp &&
   1440 					     mifp->m6_ifp != ifp;
   1441 				     mifp++, iif++)
   1442 					;
   1443 
   1444 				bzero(&sin6, sizeof(sin6));
   1445 				sin6.sin6_len = sizeof(sin6);
   1446 				sin6.sin6_family = AF_INET6;
   1447 				switch (ip6_mrouter_ver) {
   1448 				case MRT6_OINIT:
   1449 					oim->im6_mif = iif;
   1450 					sin6.sin6_addr = oim->im6_src;
   1451 					break;
   1452 				case MRT6_INIT:
   1453 					im->im6_mif = iif;
   1454 					sin6.sin6_addr = im->im6_src;
   1455 					break;
   1456 				}
   1457 
   1458 				mrt6stat.mrt6s_upcalls++;
   1459 
   1460 				if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
   1461 #ifdef MRT6DEBUG
   1462 					if (mrt6debug)
   1463 						log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n");
   1464 #endif
   1465 					++mrt6stat.mrt6s_upq_sockfull;
   1466 					return ENOBUFS;
   1467 				}	/* if socket Q full */
   1468 			}		/* if PIM */
   1469 		return 0;
   1470 	}			/* if wrong iif */
   1471 
   1472 	/* If I sourced this packet, it counts as output, else it was input. */
   1473 	if (m->m_pkthdr.rcvif == NULL) {
   1474 		/* XXX: is rcvif really NULL when output?? */
   1475 		mif6table[mifi].m6_pkt_out++;
   1476 		mif6table[mifi].m6_bytes_out += plen;
   1477 	} else {
   1478 		mif6table[mifi].m6_pkt_in++;
   1479 		mif6table[mifi].m6_bytes_in += plen;
   1480 	}
   1481 	rt->mf6c_pkt_cnt++;
   1482 	rt->mf6c_byte_cnt += plen;
   1483 
   1484 	/*
   1485 	 * For each mif, forward a copy of the packet if there are group
   1486 	 * members downstream on the interface.
   1487 	 */
   1488 	src0 = ip6->ip6_src;
   1489 	dst0 = ip6->ip6_dst;
   1490 	if ((error = in6_setscope(&src0, ifp, &iszone)) != 0 ||
   1491 	    (error = in6_setscope(&dst0, ifp, &idzone)) != 0) {
   1492 		IP6_STATINC(IP6_STAT_BADSCOPE);
   1493 		return (error);
   1494 	}
   1495 	for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++)
   1496 		if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
   1497 			if (mif6table[mifi].m6_ifp == NULL)
   1498 				continue;
   1499 			/*
   1500 			 * check if the outgoing packet is going to break
   1501 			 * a scope boundary.
   1502 			 * XXX: For packets through PIM register tunnel
   1503 			 * interface, we believe the routing daemon.
   1504 			 */
   1505 			if ((mif6table[rt->mf6c_parent].m6_flags &
   1506 			     MIFF_REGISTER) == 0 &&
   1507 			    (mif6table[mifi].m6_flags & MIFF_REGISTER) == 0) {
   1508 				if (in6_setscope(&src0, mif6table[mifi].m6_ifp,
   1509 				    &oszone) ||
   1510 				    in6_setscope(&dst0, mif6table[mifi].m6_ifp,
   1511 				    &odzone) ||
   1512 				    iszone != oszone || idzone != odzone) {
   1513 					IP6_STATINC(IP6_STAT_BADSCOPE);
   1514 					continue;
   1515 				}
   1516 			}
   1517 
   1518 			mifp->m6_pkt_out++;
   1519 			mifp->m6_bytes_out += plen;
   1520 			MC6_SEND(ip6, mifp, m);
   1521 		}
   1522 	return 0;
   1523 }
   1524 
   1525 static void
   1526 phyint_send(struct ip6_hdr *ip6, struct mif6 *mifp, struct mbuf *m)
   1527 {
   1528 	struct mbuf *mb_copy;
   1529 	struct ifnet *ifp = mifp->m6_ifp;
   1530 	int error = 0;
   1531 	int s;
   1532 	static struct route ro;
   1533 	struct in6_multi *in6m;
   1534 	struct sockaddr_in6 dst6;
   1535 	u_long linkmtu;
   1536 
   1537 	s = splsoftnet();
   1538 	/*
   1539 	 * Make a new reference to the packet; make sure that
   1540 	 * the IPv6 header is actually copied, not just referenced,
   1541 	 * so that ip6_output() only scribbles on the copy.
   1542 	 */
   1543 	mb_copy = m_copy(m, 0, M_COPYALL);
   1544 	if (mb_copy &&
   1545 	    (M_READONLY(mb_copy) || mb_copy->m_len < sizeof(struct ip6_hdr)))
   1546 		mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr));
   1547 	if (mb_copy == NULL) {
   1548 		splx(s);
   1549 		return;
   1550 	}
   1551 	/* set MCAST flag to the outgoing packet */
   1552 	mb_copy->m_flags |= M_MCAST;
   1553 
   1554 	/*
   1555 	 * If we sourced the packet, call ip6_output since we may divide
   1556 	 * the packet into fragments when the packet is too big for the
   1557 	 * outgoing interface.
   1558 	 * Otherwise, we can simply send the packet to the interface
   1559 	 * sending queue.
   1560 	 */
   1561 	if (m->m_pkthdr.rcvif == NULL) {
   1562 		struct ip6_moptions im6o;
   1563 
   1564 		im6o.im6o_multicast_ifp = ifp;
   1565 		/* XXX: ip6_output will override ip6->ip6_hlim */
   1566 		im6o.im6o_multicast_hlim = ip6->ip6_hlim;
   1567 		im6o.im6o_multicast_loop = 1;
   1568 		error = ip6_output(mb_copy, NULL, &ro, IPV6_FORWARDING,
   1569 				   &im6o, NULL, NULL);
   1570 
   1571 #ifdef MRT6DEBUG
   1572 		if (mrt6debug & DEBUG_XMIT)
   1573 			log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
   1574 			    mifp - mif6table, error);
   1575 #endif
   1576 		splx(s);
   1577 		return;
   1578 	}
   1579 
   1580 	/*
   1581 	 * If we belong to the destination multicast group
   1582 	 * on the outgoing interface, loop back a copy.
   1583 	 */
   1584 	/*
   1585 	 * Does not have to check source info, as it's alreay covered by
   1586 	 * ip6_input
   1587 	 */
   1588 	sockaddr_in6_init(&dst6, &ip6->ip6_dst, 0, 0, 0);
   1589 
   1590 	IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
   1591 	if (in6m != NULL) {
   1592 		ip6_mloopback(ifp, m,
   1593 		    satocsin6(rtcache_getdst(&ro)));
   1594 	}
   1595 
   1596 	/*
   1597 	 * Put the packet into the sending queue of the outgoing interface
   1598 	 * if it would fit in the MTU of the interface.
   1599 	 */
   1600 	linkmtu = IN6_LINKMTU(ifp);
   1601 	if (mb_copy->m_pkthdr.len <= linkmtu || linkmtu < IPV6_MMTU) {
   1602 		/*
   1603 		 * We could call if_output directly here, but we use
   1604 		 * nd6_output on purpose to see if IPv6 operation is allowed
   1605 		 * on the interface.
   1606 		 */
   1607 		error = nd6_output(ifp, ifp, mb_copy, &dst6, NULL);
   1608 #ifdef MRT6DEBUG
   1609 		if (mrt6debug & DEBUG_XMIT)
   1610 			log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
   1611 			    mifp - mif6table, error);
   1612 #endif
   1613 	} else {
   1614 		/*
   1615 		 * pMTU discovery is intentionally disabled by default, since
   1616 		 * various router may notify pMTU in multicast, which can be
   1617 		 * a DDoS to a router
   1618 		 */
   1619 		if (ip6_mcast_pmtu)
   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 		}
   1634 	}
   1635 
   1636 	splx(s);
   1637 }
   1638 
   1639 static int
   1640 register_send(struct ip6_hdr *ip6, struct mif6 *mif, struct mbuf *m)
   1641 {
   1642 	struct mbuf *mm;
   1643 	int i, len = m->m_pkthdr.len;
   1644 	struct sockaddr_in6 sin6;
   1645 	struct mrt6msg *im6;
   1646 
   1647 #ifdef MRT6DEBUG
   1648 	if (mrt6debug)
   1649 		log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n",
   1650 		    ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst));
   1651 #endif
   1652 	PIM6_STATINC(PIM6_STAT_SND_REGISTERS);
   1653 
   1654 	/* Make a copy of the packet to send to the user level process */
   1655 	MGETHDR(mm, M_DONTWAIT, MT_HEADER);
   1656 	if (mm == NULL)
   1657 		return ENOBUFS;
   1658 	mm->m_data += max_linkhdr;
   1659 	mm->m_len = sizeof(struct ip6_hdr);
   1660 
   1661 	if ((mm->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
   1662 		m_freem(mm);
   1663 		return ENOBUFS;
   1664 	}
   1665 	i = MHLEN - M_LEADINGSPACE(mm);
   1666 	if (i > len)
   1667 		i = len;
   1668 	mm = m_pullup(mm, i);
   1669 	if (mm == NULL)
   1670 		return ENOBUFS;
   1671 /* TODO: check it! */
   1672 	mm->m_pkthdr.len = len + sizeof(struct ip6_hdr);
   1673 
   1674 	/*
   1675 	 * Send message to routing daemon
   1676 	 */
   1677 	sockaddr_in6_init(&sin6, &ip6->ip6_src, 0, 0, 0);
   1678 
   1679 	im6 = mtod(mm, struct mrt6msg *);
   1680 	im6->im6_msgtype      = MRT6MSG_WHOLEPKT;
   1681 	im6->im6_mbz          = 0;
   1682 
   1683 	im6->im6_mif = mif - mif6table;
   1684 
   1685 	/* iif info is not given for reg. encap.n */
   1686 	mrt6stat.mrt6s_upcalls++;
   1687 
   1688 	if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
   1689 #ifdef MRT6DEBUG
   1690 		if (mrt6debug)
   1691 			log(LOG_WARNING,
   1692 			    "register_send: ip6_mrouter socket queue full\n");
   1693 #endif
   1694 		++mrt6stat.mrt6s_upq_sockfull;
   1695 		return ENOBUFS;
   1696 	}
   1697 	return 0;
   1698 }
   1699 
   1700 /*
   1701  * PIM sparse mode hook
   1702  * Receives the pim control messages, and passes them up to the listening
   1703  * socket, using rip6_input.
   1704  * The only message processed is the REGISTER pim message; the pim header
   1705  * is stripped off, and the inner packet is passed to register_mforward.
   1706  */
   1707 int
   1708 pim6_input(struct mbuf **mp, int *offp, int proto)
   1709 {
   1710 	struct pim *pim; /* pointer to a pim struct */
   1711 	struct ip6_hdr *ip6;
   1712 	int pimlen;
   1713 	struct mbuf *m = *mp;
   1714 	int minlen;
   1715 	int off = *offp;
   1716 
   1717 	PIM6_STATINC(PIM6_STAT_RCV_TOTAL);
   1718 
   1719 	ip6 = mtod(m, struct ip6_hdr *);
   1720 	pimlen = m->m_pkthdr.len - *offp;
   1721 
   1722 	/*
   1723 	 * Validate lengths
   1724 	 */
   1725 	if (pimlen < PIM_MINLEN) {
   1726 		PIM6_STATINC(PIM6_STAT_RCV_TOOSHORT);
   1727 #ifdef MRT6DEBUG
   1728 		if (mrt6debug & DEBUG_PIM)
   1729 			log(LOG_DEBUG,"pim6_input: PIM packet too short\n");
   1730 #endif
   1731 		m_freem(m);
   1732 		return (IPPROTO_DONE);
   1733 	}
   1734 
   1735 	/*
   1736 	 * if the packet is at least as big as a REGISTER, go ahead
   1737 	 * and grab the PIM REGISTER header size, to avoid another
   1738 	 * possible m_pullup() later.
   1739 	 *
   1740 	 * PIM_MINLEN       == pimhdr + u_int32 == 8
   1741 	 * PIM6_REG_MINLEN   == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
   1742 	 */
   1743 	minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN;
   1744 
   1745 	/*
   1746 	 * Make sure that the IP6 and PIM headers in contiguous memory, and
   1747 	 * possibly the PIM REGISTER header
   1748 	 */
   1749 	IP6_EXTHDR_GET(pim, struct pim *, m, off, minlen);
   1750 	if (pim == NULL) {
   1751 		PIM6_STATINC(PIM6_STAT_RCV_TOOSHORT);
   1752 		return IPPROTO_DONE;
   1753 	}
   1754 
   1755 	/* PIM version check */
   1756 	if (pim->pim_ver != PIM_VERSION) {
   1757 		PIM6_STATINC(PIM6_STAT_RCV_BADVERSION);
   1758 #ifdef MRT6DEBUG
   1759 		log(LOG_ERR,
   1760 		    "pim6_input: incorrect version %d, expecting %d\n",
   1761 		    pim->pim_ver, PIM_VERSION);
   1762 #endif
   1763 		m_freem(m);
   1764 		return (IPPROTO_DONE);
   1765 	}
   1766 
   1767 #define PIM6_CHECKSUM
   1768 #ifdef PIM6_CHECKSUM
   1769 	{
   1770 		int cksumlen;
   1771 
   1772 		/*
   1773 		 * Validate checksum.
   1774 		 * If PIM REGISTER, exclude the data packet
   1775 		 */
   1776 		if (pim->pim_type == PIM_REGISTER)
   1777 			cksumlen = PIM_MINLEN;
   1778 		else
   1779 			cksumlen = pimlen;
   1780 
   1781 		if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) {
   1782 			PIM6_STATINC(PIM6_STAT_RCV_BADSUM);
   1783 #ifdef MRT6DEBUG
   1784 			if (mrt6debug & DEBUG_PIM)
   1785 				log(LOG_DEBUG,
   1786 				    "pim6_input: invalid checksum\n");
   1787 #endif
   1788 			m_freem(m);
   1789 			return (IPPROTO_DONE);
   1790 		}
   1791 	}
   1792 #endif /* PIM_CHECKSUM */
   1793 
   1794 	if (pim->pim_type == PIM_REGISTER) {
   1795 		/*
   1796 		 * since this is a REGISTER, we'll make a copy of the register
   1797 		 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
   1798 		 * routing daemon.
   1799 		 */
   1800 		static const struct sockaddr_in6 dst = {
   1801 			.sin6_len = sizeof(dst),
   1802 			.sin6_family = AF_INET6,
   1803 		};
   1804 
   1805 		struct mbuf *mcp;
   1806 		struct ip6_hdr *eip6;
   1807 		u_int32_t *reghdr;
   1808 
   1809 		PIM6_STATINC(PIM6_STAT_RCV_REGISTERS);
   1810 
   1811 		if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) {
   1812 #ifdef MRT6DEBUG
   1813 			if (mrt6debug & DEBUG_PIM)
   1814 				log(LOG_DEBUG,
   1815 				    "pim6_input: register mif not set: %d\n",
   1816 				    reg_mif_num);
   1817 #endif
   1818 			m_freem(m);
   1819 			return (IPPROTO_DONE);
   1820 		}
   1821 
   1822 		reghdr = (u_int32_t *)(pim + 1);
   1823 
   1824 		if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
   1825 			goto pim6_input_to_daemon;
   1826 
   1827 		/*
   1828 		 * Validate length
   1829 		 */
   1830 		if (pimlen < PIM6_REG_MINLEN) {
   1831 			PIM6_STATINC(PIM6_STAT_RCV_TOOSHORT);
   1832 			PIM6_STATINC(PIM6_STAT_RCV_BADREGISTERS);
   1833 #ifdef MRT6DEBUG
   1834 			log(LOG_ERR,
   1835 			    "pim6_input: register packet size too "
   1836 			    "small %d from %s\n",
   1837 			    pimlen, ip6_sprintf(&ip6->ip6_src));
   1838 #endif
   1839 			m_freem(m);
   1840 			return (IPPROTO_DONE);
   1841 		}
   1842 
   1843 		eip6 = (struct ip6_hdr *) (reghdr + 1);
   1844 #ifdef MRT6DEBUG
   1845 		if (mrt6debug & DEBUG_PIM)
   1846 			log(LOG_DEBUG,
   1847 			    "pim6_input[register], eip6: %s -> %s, "
   1848 			    "eip6 plen %d\n",
   1849 			    ip6_sprintf(&eip6->ip6_src),
   1850 			    ip6_sprintf(&eip6->ip6_dst),
   1851 			    ntohs(eip6->ip6_plen));
   1852 #endif
   1853 
   1854 		/* verify the version number of the inner packet */
   1855 		if ((eip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
   1856 			PIM6_STATINC(PIM6_STAT_RCV_BADREGISTERS);
   1857 #ifdef MRT6DEBUG
   1858 			log(LOG_DEBUG, "pim6_input: invalid IP version (%d) "
   1859 			    "of the inner packet\n",
   1860 			    (eip6->ip6_vfc & IPV6_VERSION));
   1861 #endif
   1862 			m_freem(m);
   1863 			return (IPPROTO_NONE);
   1864 		}
   1865 
   1866 		/* verify the inner packet is destined to a mcast group */
   1867 		if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) {
   1868 			PIM6_STATINC(PIM6_STAT_RCV_BADREGISTERS);
   1869 #ifdef MRT6DEBUG
   1870 			if (mrt6debug & DEBUG_PIM)
   1871 				log(LOG_DEBUG,
   1872 				    "pim6_input: inner packet of register "
   1873 				    "is not multicast %s\n",
   1874 				    ip6_sprintf(&eip6->ip6_dst));
   1875 #endif
   1876 			m_freem(m);
   1877 			return (IPPROTO_DONE);
   1878 		}
   1879 
   1880 		/*
   1881 		 * make a copy of the whole header to pass to the daemon later.
   1882 		 */
   1883 		mcp = m_copy(m, 0, off + PIM6_REG_MINLEN);
   1884 		if (mcp == NULL) {
   1885 #ifdef MRT6DEBUG
   1886 			log(LOG_ERR,
   1887 			    "pim6_input: pim register: "
   1888 			    "could not copy register head\n");
   1889 #endif
   1890 			m_freem(m);
   1891 			return (IPPROTO_DONE);
   1892 		}
   1893 
   1894 		/*
   1895 		 * forward the inner ip6 packet; point m_data at the inner ip6.
   1896 		 */
   1897 		m_adj(m, off + PIM_MINLEN);
   1898 #ifdef MRT6DEBUG
   1899 		if (mrt6debug & DEBUG_PIM) {
   1900 			log(LOG_DEBUG,
   1901 			    "pim6_input: forwarding decapsulated register: "
   1902 			    "src %s, dst %s, mif %d\n",
   1903 			    ip6_sprintf(&eip6->ip6_src),
   1904 			    ip6_sprintf(&eip6->ip6_dst),
   1905 			    reg_mif_num);
   1906 		}
   1907 #endif
   1908 
   1909 		looutput(mif6table[reg_mif_num].m6_ifp, m,
   1910 			      (struct sockaddr *)__UNCONST(&dst),
   1911 			      (struct rtentry *) NULL);
   1912 
   1913 		/* prepare the register head to send to the mrouting daemon */
   1914 		m = mcp;
   1915 	}
   1916 
   1917 	/*
   1918 	 * Pass the PIM message up to the daemon; if it is a register message
   1919 	 * pass the 'head' only up to the daemon. This includes the
   1920 	 * encapsulator ip6 header, pim header, register header and the
   1921 	 * encapsulated ip6 header.
   1922 	 */
   1923   pim6_input_to_daemon:
   1924 	rip6_input(&m, offp, proto);
   1925 	return (IPPROTO_DONE);
   1926 }
   1927 
   1928 static int
   1929 sysctl_net_inet6_pim6_stats(SYSCTLFN_ARGS)
   1930 {
   1931 
   1932 	return (NETSTAT_SYSCTL(pim6stat_percpu, PIM6_NSTATS));
   1933 }
   1934 
   1935 SYSCTL_SETUP(sysctl_net_inet6_pim6_setup, "sysctl net.inet6.pim6 subtree setup")
   1936 {
   1937 	sysctl_createv(clog, 0, NULL, NULL,
   1938 		       CTLFLAG_PERMANENT,
   1939 		       CTLTYPE_NODE, "net", NULL,
   1940 		       NULL, 0, NULL, 0,
   1941 		       CTL_NET, CTL_EOL);
   1942 	sysctl_createv(clog, 0, NULL, NULL,
   1943 		       CTLFLAG_PERMANENT,
   1944 		       CTLTYPE_NODE, "inet6", NULL,
   1945 		       NULL, 0, NULL, 0,
   1946 		       CTL_NET, PF_INET6, CTL_EOL);
   1947 	sysctl_createv(clog, 0, NULL, NULL,
   1948 		       CTLFLAG_PERMANENT,
   1949 		       CTLTYPE_NODE, "pim6",
   1950 		       SYSCTL_DESCR("PIMv6 settings"),
   1951 		       NULL, 0, NULL, 0,
   1952 		       CTL_NET, PF_INET6, IPPROTO_PIM, CTL_EOL);
   1953 
   1954 	sysctl_createv(clog, 0, NULL, NULL,
   1955 		       CTLFLAG_PERMANENT,
   1956 		       CTLTYPE_STRUCT, "stats",
   1957 		       SYSCTL_DESCR("PIMv6 statistics"),
   1958 		       sysctl_net_inet6_pim6_stats, 0, NULL, 0,
   1959 		       CTL_NET, PF_INET6, IPPROTO_PIM, PIM6CTL_STATS,
   1960 		       CTL_EOL);
   1961 }
   1962