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