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