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