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