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