xref: /dragonfly/sys/netinet6/ip6_mroute.c (revision 9bb2a92d)
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.5 2004/01/08 18:39:18 asmodai 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 			strlcpy(multicast_register_if.if_xname, "register_mif",
550 			    IFNAMSIZ);
551 			multicast_register_if.if_flags |= IFF_LOOPBACK;
552 			multicast_register_if.if_index = mifcp->mif6c_mifi;
553 			reg_mif_num = mifcp->mif6c_mifi;
554 		}
555 
556 		ifp = &multicast_register_if;
557 
558 	} /* if REGISTER */
559 	else {
560 		/* Make sure the interface supports multicast */
561 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
562 			return EOPNOTSUPP;
563 
564 		s = splnet();
565 		error = if_allmulti(ifp, 1);
566 		splx(s);
567 		if (error)
568 			return error;
569 	}
570 
571 	s = splnet();
572 	mifp->m6_flags     = mifcp->mif6c_flags;
573 	mifp->m6_ifp       = ifp;
574 #ifdef notyet
575 	/* scaling up here allows division by 1024 in critical code */
576 	mifp->m6_rate_limit = mifcp->mif6c_rate_limit * 1024 / 1000;
577 #endif
578 	/* initialize per mif pkt counters */
579 	mifp->m6_pkt_in    = 0;
580 	mifp->m6_pkt_out   = 0;
581 	mifp->m6_bytes_in  = 0;
582 	mifp->m6_bytes_out = 0;
583 	splx(s);
584 
585 	/* Adjust nummifs up if the mifi is higher than nummifs */
586 	if (nummifs <= mifcp->mif6c_mifi)
587 		nummifs = mifcp->mif6c_mifi + 1;
588 
589 #ifdef MRT6DEBUG
590 	if (mrt6debug)
591 		log(LOG_DEBUG,
592 		    "add_mif #%d, phyint %s\n",
593 		    mifcp->mif6c_mifi,
594 		    ifp->if_xname);
595 #endif
596 
597 	return 0;
598 }
599 
600 /*
601  * Delete a mif from the mif table
602  */
603 static int
604 del_m6if(mifip)
605 	mifi_t *mifip;
606 {
607 	struct mif6 *mifp = mif6table + *mifip;
608 	mifi_t mifi;
609 	struct ifnet *ifp;
610 	int s;
611 
612 	if (*mifip >= nummifs)
613 		return EINVAL;
614 	if (mifp->m6_ifp == NULL)
615 		return EINVAL;
616 
617 	s = splnet();
618 
619 	if (!(mifp->m6_flags & MIFF_REGISTER)) {
620 		/*
621 		 * XXX: what if there is yet IPv4 multicast daemon
622 		 *      using the interface?
623 		 */
624 		ifp = mifp->m6_ifp;
625 
626 		if_allmulti(ifp, 0);
627 	}
628 
629 #ifdef notyet
630 	bzero((caddr_t)qtable[*mifip], sizeof(qtable[*mifip]));
631 	bzero((caddr_t)mifp->m6_tbf, sizeof(*(mifp->m6_tbf)));
632 #endif
633 	bzero((caddr_t)mifp, sizeof (*mifp));
634 
635 	/* Adjust nummifs down */
636 	for (mifi = nummifs; mifi > 0; mifi--)
637 		if (mif6table[mifi - 1].m6_ifp)
638 			break;
639 	nummifs = mifi;
640 
641 	splx(s);
642 
643 #ifdef MRT6DEBUG
644 	if (mrt6debug)
645 		log(LOG_DEBUG, "del_m6if %d, nummifs %d\n", *mifip, nummifs);
646 #endif
647 
648 	return 0;
649 }
650 
651 /*
652  * Add an mfc entry
653  */
654 static int
655 add_m6fc(mfccp)
656 	struct mf6cctl *mfccp;
657 {
658 	struct mf6c *rt;
659 	u_long hash;
660 	struct rtdetq *rte;
661 	u_short nstl;
662 	int s;
663 
664 	MF6CFIND(mfccp->mf6cc_origin.sin6_addr,
665 		 mfccp->mf6cc_mcastgrp.sin6_addr, rt);
666 
667 	/* If an entry already exists, just update the fields */
668 	if (rt) {
669 #ifdef MRT6DEBUG
670 		if (mrt6debug & DEBUG_MFC)
671 			log(LOG_DEBUG,
672 			    "add_m6fc no upcall h %d o %s g %s p %x\n",
673 			    ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
674 			    ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
675 			    mfccp->mf6cc_parent);
676 #endif
677 
678 		s = splnet();
679 		rt->mf6c_parent = mfccp->mf6cc_parent;
680 		rt->mf6c_ifset = mfccp->mf6cc_ifset;
681 		splx(s);
682 		return 0;
683 	}
684 
685 	/*
686 	 * Find the entry for which the upcall was made and update
687 	 */
688 	s = splnet();
689 	hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr,
690 			mfccp->mf6cc_mcastgrp.sin6_addr);
691 	for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) {
692 		if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
693 				       &mfccp->mf6cc_origin.sin6_addr) &&
694 		    IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
695 				       &mfccp->mf6cc_mcastgrp.sin6_addr) &&
696 		    (rt->mf6c_stall != NULL)) {
697 
698 			if (nstl++)
699 				log(LOG_ERR,
700 				    "add_m6fc: %s o %s g %s p %x dbx %p\n",
701 				    "multiple kernel entries",
702 				    ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
703 				    ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
704 				    mfccp->mf6cc_parent, rt->mf6c_stall);
705 
706 #ifdef MRT6DEBUG
707 			if (mrt6debug & DEBUG_MFC)
708 				log(LOG_DEBUG,
709 				    "add_m6fc o %s g %s p %x dbg %x\n",
710 				    ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
711 				    ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
712 				    mfccp->mf6cc_parent, rt->mf6c_stall);
713 #endif
714 
715 			rt->mf6c_origin     = mfccp->mf6cc_origin;
716 			rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
717 			rt->mf6c_parent     = mfccp->mf6cc_parent;
718 			rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
719 			/* initialize pkt counters per src-grp */
720 			rt->mf6c_pkt_cnt    = 0;
721 			rt->mf6c_byte_cnt   = 0;
722 			rt->mf6c_wrong_if   = 0;
723 
724 			rt->mf6c_expire = 0;	/* Don't clean this guy up */
725 			n6expire[hash]--;
726 
727 			/* free packets Qed at the end of this entry */
728 			for (rte = rt->mf6c_stall; rte != NULL; ) {
729 				struct rtdetq *n = rte->next;
730 				ip6_mdq(rte->m, rte->ifp, rt);
731 				m_freem(rte->m);
732 #ifdef UPCALL_TIMING
733 				collate(&(rte->t));
734 #endif /* UPCALL_TIMING */
735 				free(rte, M_MRTABLE);
736 				rte = n;
737 			}
738 			rt->mf6c_stall = NULL;
739 		}
740 	}
741 
742 	/*
743 	 * It is possible that an entry is being inserted without an upcall
744 	 */
745 	if (nstl == 0) {
746 #ifdef MRT6DEBUG
747 		if (mrt6debug & DEBUG_MFC)
748 			log(LOG_DEBUG,"add_mfc no upcall h %d o %s g %s p %x\n",
749 			    hash,
750 			    ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
751 			    ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
752 			    mfccp->mf6cc_parent);
753 #endif
754 
755 		for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
756 
757 			if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
758 					       &mfccp->mf6cc_origin.sin6_addr)&&
759 			    IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
760 					       &mfccp->mf6cc_mcastgrp.sin6_addr)) {
761 
762 				rt->mf6c_origin     = mfccp->mf6cc_origin;
763 				rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
764 				rt->mf6c_parent     = mfccp->mf6cc_parent;
765 				rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
766 				/* initialize pkt counters per src-grp */
767 				rt->mf6c_pkt_cnt    = 0;
768 				rt->mf6c_byte_cnt   = 0;
769 				rt->mf6c_wrong_if   = 0;
770 
771 				if (rt->mf6c_expire)
772 					n6expire[hash]--;
773 				rt->mf6c_expire	   = 0;
774 			}
775 		}
776 		if (rt == NULL) {
777 			/* no upcall, so make a new entry */
778 			rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE,
779 						  M_NOWAIT);
780 			if (rt == NULL) {
781 				splx(s);
782 				return ENOBUFS;
783 			}
784 
785 			/* insert new entry at head of hash chain */
786 			rt->mf6c_origin     = mfccp->mf6cc_origin;
787 			rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
788 			rt->mf6c_parent     = mfccp->mf6cc_parent;
789 			rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
790 			/* initialize pkt counters per src-grp */
791 			rt->mf6c_pkt_cnt    = 0;
792 			rt->mf6c_byte_cnt   = 0;
793 			rt->mf6c_wrong_if   = 0;
794 			rt->mf6c_expire     = 0;
795 			rt->mf6c_stall = NULL;
796 
797 			/* link into table */
798 			rt->mf6c_next  = mf6ctable[hash];
799 			mf6ctable[hash] = rt;
800 		}
801 	}
802 	splx(s);
803 	return 0;
804 }
805 
806 #ifdef UPCALL_TIMING
807 /*
808  * collect delay statistics on the upcalls
809  */
810 static void
811 collate(t)
812 	struct timeval *t;
813 {
814 	u_long d;
815 	struct timeval tp;
816 	u_long delta;
817 
818 	GET_TIME(tp);
819 
820 	if (TV_LT(*t, tp))
821 	{
822 		TV_DELTA(tp, *t, delta);
823 
824 		d = delta >> 10;
825 		if (d > UPCALL_MAX)
826 			d = UPCALL_MAX;
827 
828 		++upcall_data[d];
829 	}
830 }
831 #endif /* UPCALL_TIMING */
832 
833 /*
834  * Delete an mfc entry
835  */
836 static int
837 del_m6fc(mfccp)
838 	struct mf6cctl *mfccp;
839 {
840 	struct sockaddr_in6 	origin;
841 	struct sockaddr_in6 	mcastgrp;
842 	struct mf6c 		*rt;
843 	struct mf6c	 	**nptr;
844 	u_long 		hash;
845 	int s;
846 
847 	origin = mfccp->mf6cc_origin;
848 	mcastgrp = mfccp->mf6cc_mcastgrp;
849 	hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr);
850 
851 #ifdef MRT6DEBUG
852 	if (mrt6debug & DEBUG_MFC)
853 		log(LOG_DEBUG,"del_m6fc orig %s mcastgrp %s\n",
854 		    ip6_sprintf(&origin.sin6_addr),
855 		    ip6_sprintf(&mcastgrp.sin6_addr));
856 #endif
857 
858 	s = splnet();
859 
860 	nptr = &mf6ctable[hash];
861 	while ((rt = *nptr) != NULL) {
862 		if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr,
863 				       &rt->mf6c_origin.sin6_addr) &&
864 		    IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr,
865 				       &rt->mf6c_mcastgrp.sin6_addr) &&
866 		    rt->mf6c_stall == NULL)
867 			break;
868 
869 		nptr = &rt->mf6c_next;
870 	}
871 	if (rt == NULL) {
872 		splx(s);
873 		return EADDRNOTAVAIL;
874 	}
875 
876 	*nptr = rt->mf6c_next;
877 	free(rt, M_MRTABLE);
878 
879 	splx(s);
880 
881 	return 0;
882 }
883 
884 static int
885 socket_send(s, mm, src)
886 	struct socket *s;
887 	struct mbuf *mm;
888 	struct sockaddr_in6 *src;
889 {
890 	if (s) {
891 		if (sbappendaddr(&s->so_rcv,
892 				 (struct sockaddr *)src,
893 				 mm, (struct mbuf *)0) != 0) {
894 			sorwakeup(s);
895 			return 0;
896 		}
897 	}
898 	m_freem(mm);
899 	return -1;
900 }
901 
902 /*
903  * IPv6 multicast forwarding function. This function assumes that the packet
904  * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
905  * pointed to by "ifp", and the packet is to be relayed to other networks
906  * that have members of the packet's destination IPv6 multicast group.
907  *
908  * The packet is returned unscathed to the caller, unless it is
909  * erroneous, in which case a non-zero return value tells the caller to
910  * discard it.
911  */
912 
913 int
914 ip6_mforward(ip6, ifp, m)
915 	struct ip6_hdr *ip6;
916 	struct ifnet *ifp;
917 	struct mbuf *m;
918 {
919 	struct mf6c *rt;
920 	struct mif6 *mifp;
921 	struct mbuf *mm;
922 	int s;
923 	mifi_t mifi;
924 
925 #ifdef MRT6DEBUG
926 	if (mrt6debug & DEBUG_FORWARD)
927 		log(LOG_DEBUG, "ip6_mforward: src %s, dst %s, ifindex %d\n",
928 		    ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst),
929 		    ifp->if_index);
930 #endif
931 
932 	/*
933 	 * Don't forward a packet with Hop limit of zero or one,
934 	 * or a packet destined to a local-only group.
935 	 */
936 	if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst) ||
937 	    IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
938 		return 0;
939 	ip6->ip6_hlim--;
940 
941 	/*
942 	 * Source address check: do not forward packets with unspecified
943 	 * source. It was discussed in July 2000, on ipngwg mailing list.
944 	 * This is rather more serious than unicast cases, because some
945 	 * MLD packets can be sent with the unspecified source address
946 	 * (although such packets must normally set 1 to the hop limit field).
947 	 */
948 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
949 		ip6stat.ip6s_cantforward++;
950 		if (ip6_log_time + ip6_log_interval < time_second) {
951 			ip6_log_time = time_second;
952 			log(LOG_DEBUG,
953 			    "cannot forward "
954 			    "from %s to %s nxt %d received on %s\n",
955 			    ip6_sprintf(&ip6->ip6_src),
956 			    ip6_sprintf(&ip6->ip6_dst),
957 			    ip6->ip6_nxt,
958 			    if_name(m->m_pkthdr.rcvif));
959 		}
960 		return 0;
961 	}
962 
963 	/*
964 	 * Determine forwarding mifs from the forwarding cache table
965 	 */
966 	s = splnet();
967 	MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
968 
969 	/* Entry exists, so forward if necessary */
970 	if (rt) {
971 		splx(s);
972 		return (ip6_mdq(m, ifp, rt));
973 	} else {
974 		/*
975 		 * If we don't have a route for packet's origin,
976 		 * Make a copy of the packet &
977 		 * send message to routing daemon
978 		 */
979 
980 		struct mbuf *mb0;
981 		struct rtdetq *rte;
982 		u_long hash;
983 /*		int i, npkts;*/
984 #ifdef UPCALL_TIMING
985 		struct timeval tp;
986 
987 		GET_TIME(tp);
988 #endif /* UPCALL_TIMING */
989 
990 		mrt6stat.mrt6s_no_route++;
991 #ifdef MRT6DEBUG
992 		if (mrt6debug & (DEBUG_FORWARD | DEBUG_MFC))
993 			log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n",
994 			    ip6_sprintf(&ip6->ip6_src),
995 			    ip6_sprintf(&ip6->ip6_dst));
996 #endif
997 
998 		/*
999 		 * Allocate mbufs early so that we don't do extra work if we
1000 		 * are just going to fail anyway.
1001 		 */
1002 		rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE,
1003 					      M_NOWAIT);
1004 		if (rte == NULL) {
1005 			splx(s);
1006 			return ENOBUFS;
1007 		}
1008 		mb0 = m_copy(m, 0, M_COPYALL);
1009 		/*
1010 		 * Pullup packet header if needed before storing it,
1011 		 * as other references may modify it in the meantime.
1012 		 */
1013 		if (mb0 &&
1014 		    (M_HASCL(mb0) || mb0->m_len < sizeof(struct ip6_hdr)))
1015 			mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
1016 		if (mb0 == NULL) {
1017 			free(rte, M_MRTABLE);
1018 			splx(s);
1019 			return ENOBUFS;
1020 		}
1021 
1022 		/* is there an upcall waiting for this packet? */
1023 		hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
1024 		for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
1025 			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
1026 					       &rt->mf6c_origin.sin6_addr) &&
1027 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1028 					       &rt->mf6c_mcastgrp.sin6_addr) &&
1029 			    (rt->mf6c_stall != NULL))
1030 				break;
1031 		}
1032 
1033 		if (rt == NULL) {
1034 			struct mrt6msg *im;
1035 #ifdef MRT6_OINIT
1036 			struct omrt6msg *oim;
1037 #endif
1038 
1039 			/* no upcall, so make a new entry */
1040 			rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE,
1041 						  M_NOWAIT);
1042 			if (rt == NULL) {
1043 				free(rte, M_MRTABLE);
1044 				m_freem(mb0);
1045 				splx(s);
1046 				return ENOBUFS;
1047 			}
1048 			/*
1049 			 * Make a copy of the header to send to the user
1050 			 * level process
1051 			 */
1052 			mm = m_copy(mb0, 0, sizeof(struct ip6_hdr));
1053 
1054 			if (mm == NULL) {
1055 				free(rte, M_MRTABLE);
1056 				m_freem(mb0);
1057 				free(rt, M_MRTABLE);
1058 				splx(s);
1059 				return ENOBUFS;
1060 			}
1061 
1062 			/*
1063 			 * Send message to routing daemon
1064 			 */
1065 			sin6.sin6_addr = ip6->ip6_src;
1066 
1067 			im = NULL;
1068 #ifdef MRT6_OINIT
1069 			oim = NULL;
1070 #endif
1071 			switch (ip6_mrouter_ver) {
1072 #ifdef MRT6_OINIT
1073 			case MRT6_OINIT:
1074 				oim = mtod(mm, struct omrt6msg *);
1075 				oim->im6_msgtype = MRT6MSG_NOCACHE;
1076 				oim->im6_mbz = 0;
1077 				break;
1078 #endif
1079 			case MRT6_INIT:
1080 				im = mtod(mm, struct mrt6msg *);
1081 				im->im6_msgtype = MRT6MSG_NOCACHE;
1082 				im->im6_mbz = 0;
1083 				break;
1084 			default:
1085 				free(rte, M_MRTABLE);
1086 				m_freem(mb0);
1087 				free(rt, M_MRTABLE);
1088 				splx(s);
1089 				return EINVAL;
1090 			}
1091 
1092 #ifdef MRT6DEBUG
1093 			if (mrt6debug & DEBUG_FORWARD)
1094 				log(LOG_DEBUG,
1095 				    "getting the iif info in the kernel\n");
1096 #endif
1097 
1098 			for (mifp = mif6table, mifi = 0;
1099 			     mifi < nummifs && mifp->m6_ifp != ifp;
1100 			     mifp++, mifi++)
1101 				;
1102 
1103 			switch (ip6_mrouter_ver) {
1104 #ifdef MRT6_OINIT
1105 			case MRT6_OINIT:
1106 				oim->im6_mif = mifi;
1107 				break;
1108 #endif
1109 			case MRT6_INIT:
1110 				im->im6_mif = mifi;
1111 				break;
1112 			}
1113 
1114 			if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1115 				log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
1116 				    "socket queue full\n");
1117 				mrt6stat.mrt6s_upq_sockfull++;
1118 				free(rte, M_MRTABLE);
1119 				m_freem(mb0);
1120 				free(rt, M_MRTABLE);
1121 				splx(s);
1122 				return ENOBUFS;
1123 			}
1124 
1125 			mrt6stat.mrt6s_upcalls++;
1126 
1127 			/* insert new entry at head of hash chain */
1128 			bzero(rt, sizeof(*rt));
1129 			rt->mf6c_origin.sin6_family = AF_INET6;
1130 			rt->mf6c_origin.sin6_len = sizeof(struct sockaddr_in6);
1131 			rt->mf6c_origin.sin6_addr = ip6->ip6_src;
1132 			rt->mf6c_mcastgrp.sin6_family = AF_INET6;
1133 			rt->mf6c_mcastgrp.sin6_len = sizeof(struct sockaddr_in6);
1134 			rt->mf6c_mcastgrp.sin6_addr = ip6->ip6_dst;
1135 			rt->mf6c_expire = UPCALL_EXPIRE;
1136 			n6expire[hash]++;
1137 			rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
1138 
1139 			/* link into table */
1140 			rt->mf6c_next  = mf6ctable[hash];
1141 			mf6ctable[hash] = rt;
1142 			/* Add this entry to the end of the queue */
1143 			rt->mf6c_stall = rte;
1144 		} else {
1145 			/* determine if q has overflowed */
1146 			struct rtdetq **p;
1147 			int npkts = 0;
1148 
1149 			for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next)
1150 				if (++npkts > MAX_UPQ6) {
1151 					mrt6stat.mrt6s_upq_ovflw++;
1152 					free(rte, M_MRTABLE);
1153 					m_freem(mb0);
1154 					splx(s);
1155 					return 0;
1156 				}
1157 
1158 			/* Add this entry to the end of the queue */
1159 			*p = rte;
1160 		}
1161 
1162 		rte->next = NULL;
1163 		rte->m = mb0;
1164 		rte->ifp = ifp;
1165 #ifdef UPCALL_TIMING
1166 		rte->t = tp;
1167 #endif /* UPCALL_TIMING */
1168 
1169 		splx(s);
1170 
1171 		return 0;
1172 	}
1173 }
1174 
1175 /*
1176  * Clean up cache entries if upcalls are not serviced
1177  * Call from the Slow Timeout mechanism, every half second.
1178  */
1179 static void
1180 expire_upcalls(unused)
1181 	void *unused;
1182 {
1183 	struct rtdetq *rte;
1184 	struct mf6c *mfc, **nptr;
1185 	int i;
1186 	int s;
1187 
1188 	s = splnet();
1189 	for (i = 0; i < MF6CTBLSIZ; i++) {
1190 		if (n6expire[i] == 0)
1191 			continue;
1192 		nptr = &mf6ctable[i];
1193 		while ((mfc = *nptr) != NULL) {
1194 			rte = mfc->mf6c_stall;
1195 			/*
1196 			 * Skip real cache entries
1197 			 * Make sure it wasn't marked to not expire (shouldn't happen)
1198 			 * If it expires now
1199 			 */
1200 			if (rte != NULL &&
1201 			    mfc->mf6c_expire != 0 &&
1202 			    --mfc->mf6c_expire == 0) {
1203 #ifdef MRT6DEBUG
1204 				if (mrt6debug & DEBUG_EXPIRE)
1205 					log(LOG_DEBUG, "expire_upcalls: expiring (%s %s)\n",
1206 					    ip6_sprintf(&mfc->mf6c_origin.sin6_addr),
1207 					    ip6_sprintf(&mfc->mf6c_mcastgrp.sin6_addr));
1208 #endif
1209 				/*
1210 				 * drop all the packets
1211 				 * free the mbuf with the pkt, if, timing info
1212 				 */
1213 				do {
1214 					struct rtdetq *n = rte->next;
1215 					m_freem(rte->m);
1216 					free(rte, M_MRTABLE);
1217 					rte = n;
1218 				} while (rte != NULL);
1219 				mrt6stat.mrt6s_cache_cleanups++;
1220 				n6expire[i]--;
1221 
1222 				*nptr = mfc->mf6c_next;
1223 				free(mfc, M_MRTABLE);
1224 			} else {
1225 				nptr = &mfc->mf6c_next;
1226 			}
1227 		}
1228 	}
1229 	splx(s);
1230 	callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
1231 	    expire_upcalls, NULL);
1232 }
1233 
1234 /*
1235  * Packet forwarding routine once entry in the cache is made
1236  */
1237 static int
1238 ip6_mdq(m, ifp, rt)
1239 	struct mbuf *m;
1240 	struct ifnet *ifp;
1241 	struct mf6c *rt;
1242 {
1243 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1244 	mifi_t mifi, iif;
1245 	struct mif6 *mifp;
1246 	int plen = m->m_pkthdr.len;
1247 
1248 /*
1249  * Macro to send packet on mif.  Since RSVP packets don't get counted on
1250  * input, they shouldn't get counted on output, so statistics keeping is
1251  * separate.
1252  */
1253 
1254 #define MC6_SEND(ip6, mifp, m) do {				\
1255 		if ((mifp)->m6_flags & MIFF_REGISTER)		\
1256 		    register_send((ip6), (mifp), (m));		\
1257 		else						\
1258 		    phyint_send((ip6), (mifp), (m));		\
1259 } while (0)
1260 
1261 	/*
1262 	 * Don't forward if it didn't arrive from the parent mif
1263 	 * for its origin.
1264 	 */
1265 	mifi = rt->mf6c_parent;
1266 	if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
1267 		/* came in the wrong interface */
1268 #ifdef MRT6DEBUG
1269 		if (mrt6debug & DEBUG_FORWARD)
1270 			log(LOG_DEBUG,
1271 			    "wrong if: ifid %d mifi %d mififid %x\n",
1272 			    ifp->if_index, mifi,
1273 			    mif6table[mifi].m6_ifp->if_index);
1274 #endif
1275 		mrt6stat.mrt6s_wrong_if++;
1276 		rt->mf6c_wrong_if++;
1277 		/*
1278 		 * If we are doing PIM processing, and we are forwarding
1279 		 * packets on this interface, send a message to the
1280 		 * routing daemon.
1281 		 */
1282 		/* have to make sure this is a valid mif */
1283 		if (mifi < nummifs && mif6table[mifi].m6_ifp)
1284 			if (pim6 && (m->m_flags & M_LOOP) == 0) {
1285 				/*
1286 				 * Check the M_LOOP flag to avoid an
1287 				 * unnecessary PIM assert.
1288 				 * XXX: M_LOOP is an ad-hoc hack...
1289 				 */
1290 				static struct sockaddr_in6 sin6 =
1291 				{ sizeof(sin6), AF_INET6 };
1292 
1293 				struct mbuf *mm;
1294 				struct mrt6msg *im;
1295 #ifdef MRT6_OINIT
1296 				struct omrt6msg *oim;
1297 #endif
1298 
1299 				mm = m_copy(m, 0, sizeof(struct ip6_hdr));
1300 				if (mm &&
1301 				    (M_HASCL(mm) ||
1302 				     mm->m_len < sizeof(struct ip6_hdr)))
1303 					mm = m_pullup(mm, sizeof(struct ip6_hdr));
1304 				if (mm == NULL)
1305 					return ENOBUFS;
1306 
1307 #ifdef MRT6_OINIT
1308 				oim = NULL;
1309 #endif
1310 				im = NULL;
1311 				switch (ip6_mrouter_ver) {
1312 #ifdef MRT6_OINIT
1313 				case MRT6_OINIT:
1314 					oim = mtod(mm, struct omrt6msg *);
1315 					oim->im6_msgtype = MRT6MSG_WRONGMIF;
1316 					oim->im6_mbz = 0;
1317 					break;
1318 #endif
1319 				case MRT6_INIT:
1320 					im = mtod(mm, struct mrt6msg *);
1321 					im->im6_msgtype = MRT6MSG_WRONGMIF;
1322 					im->im6_mbz = 0;
1323 					break;
1324 				default:
1325 					m_freem(mm);
1326 					return EINVAL;
1327 				}
1328 
1329 				for (mifp = mif6table, iif = 0;
1330 				     iif < nummifs && mifp &&
1331 					     mifp->m6_ifp != ifp;
1332 				     mifp++, iif++)
1333 					;
1334 
1335 				switch (ip6_mrouter_ver) {
1336 #ifdef MRT6_OINIT
1337 				case MRT6_OINIT:
1338 					oim->im6_mif = iif;
1339 					sin6.sin6_addr = oim->im6_src;
1340 					break;
1341 #endif
1342 				case MRT6_INIT:
1343 					im->im6_mif = iif;
1344 					sin6.sin6_addr = im->im6_src;
1345 					break;
1346 				}
1347 
1348 				mrt6stat.mrt6s_upcalls++;
1349 
1350 				if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1351 #ifdef MRT6DEBUG
1352 					if (mrt6debug)
1353 						log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n");
1354 #endif
1355 					++mrt6stat.mrt6s_upq_sockfull;
1356 					return ENOBUFS;
1357 				}	/* if socket Q full */
1358 			}		/* if PIM */
1359 		return 0;
1360 	}			/* if wrong iif */
1361 
1362 	/* If I sourced this packet, it counts as output, else it was input. */
1363 	if (m->m_pkthdr.rcvif == NULL) {
1364 		/* XXX: is rcvif really NULL when output?? */
1365 		mif6table[mifi].m6_pkt_out++;
1366 		mif6table[mifi].m6_bytes_out += plen;
1367 	} else {
1368 		mif6table[mifi].m6_pkt_in++;
1369 		mif6table[mifi].m6_bytes_in += plen;
1370 	}
1371 	rt->mf6c_pkt_cnt++;
1372 	rt->mf6c_byte_cnt += plen;
1373 
1374 	/*
1375 	 * For each mif, forward a copy of the packet if there are group
1376 	 * members downstream on the interface.
1377 	 */
1378 	for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++)
1379 		if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
1380 			/*
1381 			 * check if the outgoing packet is going to break
1382 			 * a scope boundary.
1383 			 * XXX For packets through PIM register tunnel
1384 			 * interface, we believe a routing daemon.
1385 			 */
1386 			if ((mif6table[rt->mf6c_parent].m6_flags &
1387 			     MIFF_REGISTER) == 0 &&
1388 			    (mif6table[mifi].m6_flags & MIFF_REGISTER) == 0 &&
1389 			    (in6_addr2scopeid(ifp, &ip6->ip6_dst) !=
1390 			     in6_addr2scopeid(mif6table[mifi].m6_ifp,
1391 					      &ip6->ip6_dst) ||
1392 			     in6_addr2scopeid(ifp, &ip6->ip6_src) !=
1393 			     in6_addr2scopeid(mif6table[mifi].m6_ifp,
1394 					      &ip6->ip6_src))) {
1395 				ip6stat.ip6s_badscope++;
1396 				continue;
1397 			}
1398 
1399 			mifp->m6_pkt_out++;
1400 			mifp->m6_bytes_out += plen;
1401 			MC6_SEND(ip6, mifp, m);
1402 		}
1403 	return 0;
1404 }
1405 
1406 static void
1407 phyint_send(ip6, mifp, m)
1408     struct ip6_hdr *ip6;
1409     struct mif6 *mifp;
1410     struct mbuf *m;
1411 {
1412 	struct mbuf *mb_copy;
1413 	struct ifnet *ifp = mifp->m6_ifp;
1414 	int error = 0;
1415 	int s = splnet();	/* needs to protect static "ro" below. */
1416 	static struct route_in6 ro;
1417 	struct	in6_multi *in6m;
1418 	struct sockaddr_in6 *dst6;
1419 
1420 	/*
1421 	 * Make a new reference to the packet; make sure that
1422 	 * the IPv6 header is actually copied, not just referenced,
1423 	 * so that ip6_output() only scribbles on the copy.
1424 	 */
1425 	mb_copy = m_copy(m, 0, M_COPYALL);
1426 	if (mb_copy &&
1427 	    (M_HASCL(mb_copy) || mb_copy->m_len < sizeof(struct ip6_hdr)))
1428 		mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr));
1429 	if (mb_copy == NULL) {
1430 		splx(s);
1431 		return;
1432 	}
1433 	/* set MCAST flag to the outgoing packet */
1434 	mb_copy->m_flags |= M_MCAST;
1435 
1436 	/*
1437 	 * If we sourced the packet, call ip6_output since we may devide
1438 	 * the packet into fragments when the packet is too big for the
1439 	 * outgoing interface.
1440 	 * Otherwise, we can simply send the packet to the interface
1441 	 * sending queue.
1442 	 */
1443 	if (m->m_pkthdr.rcvif == NULL) {
1444 		struct ip6_moptions im6o;
1445 
1446 		im6o.im6o_multicast_ifp = ifp;
1447 		/* XXX: ip6_output will override ip6->ip6_hlim */
1448 		im6o.im6o_multicast_hlim = ip6->ip6_hlim;
1449 		im6o.im6o_multicast_loop = 1;
1450 		error = ip6_output(mb_copy, NULL, &ro,
1451 				   IPV6_FORWARDING, &im6o, NULL, NULL);
1452 
1453 #ifdef MRT6DEBUG
1454 		if (mrt6debug & DEBUG_XMIT)
1455 			log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1456 			    mifp - mif6table, error);
1457 #endif
1458 		splx(s);
1459 		return;
1460 	}
1461 
1462 	/*
1463 	 * If we belong to the destination multicast group
1464 	 * on the outgoing interface, loop back a copy.
1465 	 */
1466 	dst6 = (struct sockaddr_in6 *)&ro.ro_dst;
1467 	IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
1468 	if (in6m != NULL) {
1469 		dst6->sin6_len = sizeof(struct sockaddr_in6);
1470 		dst6->sin6_family = AF_INET6;
1471 		dst6->sin6_addr = ip6->ip6_dst;
1472 		ip6_mloopback(ifp, m, (struct sockaddr_in6 *)&ro.ro_dst);
1473 	}
1474 	/*
1475 	 * Put the packet into the sending queue of the outgoing interface
1476 	 * if it would fit in the MTU of the interface.
1477 	 */
1478 	if (mb_copy->m_pkthdr.len <= ifp->if_mtu || ifp->if_mtu < IPV6_MMTU) {
1479 		dst6->sin6_len = sizeof(struct sockaddr_in6);
1480 		dst6->sin6_family = AF_INET6;
1481 		dst6->sin6_addr = ip6->ip6_dst;
1482 		/*
1483 		 * We just call if_output instead of nd6_output here, since
1484 		 * we need no ND for a multicast forwarded packet...right?
1485 		 */
1486 		error = (*ifp->if_output)(ifp, mb_copy,
1487 		    (struct sockaddr *)&ro.ro_dst, NULL);
1488 #ifdef MRT6DEBUG
1489 		if (mrt6debug & DEBUG_XMIT)
1490 			log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1491 			    mifp - mif6table, error);
1492 #endif
1493 	} else {
1494 #ifdef MULTICAST_PMTUD
1495 		icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu);
1496 #else
1497 #ifdef MRT6DEBUG
1498 		if (mrt6debug & DEBUG_XMIT)
1499 			log(LOG_DEBUG,
1500 			    "phyint_send: packet too big on %s o %s g %s"
1501 			    " size %d(discarded)\n",
1502 			    if_name(ifp),
1503 			    ip6_sprintf(&ip6->ip6_src),
1504 			    ip6_sprintf(&ip6->ip6_dst),
1505 			    mb_copy->m_pkthdr.len);
1506 #endif /* MRT6DEBUG */
1507 		m_freem(mb_copy); /* simply discard the packet */
1508 #endif
1509 	}
1510 
1511 	splx(s);
1512 }
1513 
1514 static int
1515 register_send(ip6, mif, m)
1516 	struct ip6_hdr *ip6;
1517 	struct mif6 *mif;
1518 	struct mbuf *m;
1519 {
1520 	struct mbuf *mm;
1521 	int i, len = m->m_pkthdr.len;
1522 	static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
1523 	struct mrt6msg *im6;
1524 
1525 #ifdef MRT6DEBUG
1526 	if (mrt6debug)
1527 		log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n",
1528 		    ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst));
1529 #endif
1530 	++pim6stat.pim6s_snd_registers;
1531 
1532 	/* Make a copy of the packet to send to the user level process */
1533 	MGETHDR(mm, M_DONTWAIT, MT_HEADER);
1534 	if (mm == NULL)
1535 		return ENOBUFS;
1536 	mm->m_pkthdr.rcvif = NULL;
1537 	mm->m_data += max_linkhdr;
1538 	mm->m_len = sizeof(struct ip6_hdr);
1539 
1540 	if ((mm->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1541 		m_freem(mm);
1542 		return ENOBUFS;
1543 	}
1544 	i = MHLEN - M_LEADINGSPACE(mm);
1545 	if (i > len)
1546 		i = len;
1547 	mm = m_pullup(mm, i);
1548 	if (mm == NULL)
1549 		return ENOBUFS;
1550 /* TODO: check it! */
1551 	mm->m_pkthdr.len = len + sizeof(struct ip6_hdr);
1552 
1553 	/*
1554 	 * Send message to routing daemon
1555 	 */
1556 	sin6.sin6_addr = ip6->ip6_src;
1557 
1558 	im6 = mtod(mm, struct mrt6msg *);
1559 	im6->im6_msgtype      = MRT6MSG_WHOLEPKT;
1560 	im6->im6_mbz          = 0;
1561 
1562 	im6->im6_mif = mif - mif6table;
1563 
1564 	/* iif info is not given for reg. encap.n */
1565 	mrt6stat.mrt6s_upcalls++;
1566 
1567 	if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
1568 #ifdef MRT6DEBUG
1569 		if (mrt6debug)
1570 			log(LOG_WARNING,
1571 			    "register_send: ip6_mrouter socket queue full\n");
1572 #endif
1573 		++mrt6stat.mrt6s_upq_sockfull;
1574 		return ENOBUFS;
1575 	}
1576 	return 0;
1577 }
1578 
1579 /*
1580  * PIM sparse mode hook
1581  * Receives the pim control messages, and passes them up to the listening
1582  * socket, using rip6_input.
1583  * The only message processed is the REGISTER pim message; the pim header
1584  * is stripped off, and the inner packet is passed to register_mforward.
1585  */
1586 int
1587 pim6_input(mp, offp, proto)
1588 	struct mbuf **mp;
1589 	int *offp, proto;
1590 {
1591 	struct pim *pim; /* pointer to a pim struct */
1592 	struct ip6_hdr *ip6;
1593 	int pimlen;
1594 	struct mbuf *m = *mp;
1595 	int minlen;
1596 	int off = *offp;
1597 
1598 	++pim6stat.pim6s_rcv_total;
1599 
1600 	ip6 = mtod(m, struct ip6_hdr *);
1601 	pimlen = m->m_pkthdr.len - *offp;
1602 
1603 	/*
1604 	 * Validate lengths
1605 	 */
1606 	if (pimlen < PIM_MINLEN) {
1607 		++pim6stat.pim6s_rcv_tooshort;
1608 #ifdef MRT6DEBUG
1609 		if (mrt6debug & DEBUG_PIM)
1610 			log(LOG_DEBUG,"pim6_input: PIM packet too short\n");
1611 #endif
1612 		m_freem(m);
1613 		return(IPPROTO_DONE);
1614 	}
1615 
1616 	/*
1617 	 * if the packet is at least as big as a REGISTER, go ahead
1618 	 * and grab the PIM REGISTER header size, to avoid another
1619 	 * possible m_pullup() later.
1620 	 *
1621 	 * PIM_MINLEN       == pimhdr + u_int32 == 8
1622 	 * PIM6_REG_MINLEN   == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1623 	 */
1624 	minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN;
1625 
1626 	/*
1627 	 * Make sure that the IP6 and PIM headers in contiguous memory, and
1628 	 * possibly the PIM REGISTER header
1629 	 */
1630 #ifndef PULLDOWN_TEST
1631 	IP6_EXTHDR_CHECK(m, off, minlen, IPPROTO_DONE);
1632 	/* adjust pointer */
1633 	ip6 = mtod(m, struct ip6_hdr *);
1634 
1635 	/* adjust mbuf to point to the PIM header */
1636 	pim = (struct pim *)((caddr_t)ip6 + off);
1637 #else
1638 	IP6_EXTHDR_GET(pim, struct pim *, m, off, minlen);
1639 	if (pim == NULL) {
1640 		pim6stat.pim6s_rcv_tooshort++;
1641 		return IPPROTO_DONE;
1642 	}
1643 #endif
1644 
1645 #define PIM6_CHECKSUM
1646 #ifdef PIM6_CHECKSUM
1647 	{
1648 		int cksumlen;
1649 
1650 		/*
1651 		 * Validate checksum.
1652 		 * If PIM REGISTER, exclude the data packet
1653 		 */
1654 		if (pim->pim_type == PIM_REGISTER)
1655 			cksumlen = PIM_MINLEN;
1656 		else
1657 			cksumlen = pimlen;
1658 
1659 		if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) {
1660 			++pim6stat.pim6s_rcv_badsum;
1661 #ifdef MRT6DEBUG
1662 			if (mrt6debug & DEBUG_PIM)
1663 				log(LOG_DEBUG,
1664 				    "pim6_input: invalid checksum\n");
1665 #endif
1666 			m_freem(m);
1667 			return(IPPROTO_DONE);
1668 		}
1669 	}
1670 #endif /* PIM_CHECKSUM */
1671 
1672 	/* PIM version check */
1673 	if (pim->pim_ver != PIM_VERSION) {
1674 		++pim6stat.pim6s_rcv_badversion;
1675 #ifdef MRT6DEBUG
1676 		log(LOG_ERR,
1677 		    "pim6_input: incorrect version %d, expecting %d\n",
1678 		    pim->pim_ver, PIM_VERSION);
1679 #endif
1680 		m_freem(m);
1681 		return(IPPROTO_DONE);
1682 	}
1683 
1684 	if (pim->pim_type == PIM_REGISTER) {
1685 		/*
1686 		 * since this is a REGISTER, we'll make a copy of the register
1687 		 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1688 		 * routing daemon.
1689 		 */
1690 		static struct sockaddr_in6 dst = { sizeof(dst), AF_INET6 };
1691 
1692 		struct mbuf *mcp;
1693 		struct ip6_hdr *eip6;
1694 		u_int32_t *reghdr;
1695 		int rc;
1696 
1697 		++pim6stat.pim6s_rcv_registers;
1698 
1699 		if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) {
1700 #ifdef MRT6DEBUG
1701 			if (mrt6debug & DEBUG_PIM)
1702 				log(LOG_DEBUG,
1703 				    "pim6_input: register mif not set: %d\n",
1704 				    reg_mif_num);
1705 #endif
1706 			m_freem(m);
1707 			return(IPPROTO_DONE);
1708 		}
1709 
1710 		reghdr = (u_int32_t *)(pim + 1);
1711 
1712 		if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
1713 			goto pim6_input_to_daemon;
1714 
1715 		/*
1716 		 * Validate length
1717 		 */
1718 		if (pimlen < PIM6_REG_MINLEN) {
1719 			++pim6stat.pim6s_rcv_tooshort;
1720 			++pim6stat.pim6s_rcv_badregisters;
1721 #ifdef MRT6DEBUG
1722 			log(LOG_ERR,
1723 			    "pim6_input: register packet size too "
1724 			    "small %d from %s\n",
1725 			    pimlen, ip6_sprintf(&ip6->ip6_src));
1726 #endif
1727 			m_freem(m);
1728 			return(IPPROTO_DONE);
1729 		}
1730 
1731 		eip6 = (struct ip6_hdr *) (reghdr + 1);
1732 #ifdef MRT6DEBUG
1733 		if (mrt6debug & DEBUG_PIM)
1734 			log(LOG_DEBUG,
1735 			    "pim6_input[register], eip6: %s -> %s, "
1736 			    "eip6 plen %d\n",
1737 			    ip6_sprintf(&eip6->ip6_src),
1738 			    ip6_sprintf(&eip6->ip6_dst),
1739 			    ntohs(eip6->ip6_plen));
1740 #endif
1741 
1742 		/* verify the version number of the inner packet */
1743 		if ((eip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1744 			++pim6stat.pim6s_rcv_badregisters;
1745 #ifdef MRT6DEBUG
1746 			log(LOG_DEBUG, "pim6_input: invalid IP version (%d) "
1747 			    "of the inner packet\n",
1748 			    (eip6->ip6_vfc & IPV6_VERSION));
1749 #endif
1750 			m_freem(m);
1751 			return(IPPROTO_NONE);
1752 		}
1753 
1754 		/* verify the inner packet is destined to a mcast group */
1755 		if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) {
1756 			++pim6stat.pim6s_rcv_badregisters;
1757 #ifdef MRT6DEBUG
1758 			if (mrt6debug & DEBUG_PIM)
1759 				log(LOG_DEBUG,
1760 				    "pim6_input: inner packet of register "
1761 				    "is not multicast %s\n",
1762 				    ip6_sprintf(&eip6->ip6_dst));
1763 #endif
1764 			m_freem(m);
1765 			return(IPPROTO_DONE);
1766 		}
1767 
1768 		/*
1769 		 * make a copy of the whole header to pass to the daemon later.
1770 		 */
1771 		mcp = m_copy(m, 0, off + PIM6_REG_MINLEN);
1772 		if (mcp == NULL) {
1773 #ifdef MRT6DEBUG
1774 			log(LOG_ERR,
1775 			    "pim6_input: pim register: "
1776 			    "could not copy register head\n");
1777 #endif
1778 			m_freem(m);
1779 			return(IPPROTO_DONE);
1780 		}
1781 
1782 		/*
1783 		 * forward the inner ip6 packet; point m_data at the inner ip6.
1784 		 */
1785 		m_adj(m, off + PIM_MINLEN);
1786 #ifdef MRT6DEBUG
1787 		if (mrt6debug & DEBUG_PIM) {
1788 			log(LOG_DEBUG,
1789 			    "pim6_input: forwarding decapsulated register: "
1790 			    "src %s, dst %s, mif %d\n",
1791 			    ip6_sprintf(&eip6->ip6_src),
1792 			    ip6_sprintf(&eip6->ip6_dst),
1793 			    reg_mif_num);
1794 		}
1795 #endif
1796 
1797  		rc = if_simloop(mif6table[reg_mif_num].m6_ifp, m,
1798 				dst.sin6_family, NULL);
1799 
1800 		/* prepare the register head to send to the mrouting daemon */
1801 		m = mcp;
1802 	}
1803 
1804 	/*
1805 	 * Pass the PIM message up to the daemon; if it is a register message
1806 	 * pass the 'head' only up to the daemon. This includes the
1807 	 * encapsulator ip6 header, pim header, register header and the
1808 	 * encapsulated ip6 header.
1809 	 */
1810   pim6_input_to_daemon:
1811 	rip6_input(&m, offp, proto);
1812 	return(IPPROTO_DONE);
1813 }
1814