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