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