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