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