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