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