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