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