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