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