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