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