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