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