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