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