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