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