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