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