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