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