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