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