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