ip6_mroute.c revision 252566
1236592Sobrien/*-
2236592Sobrien * Copyright (C) 1998 WIDE Project.
3236592Sobrien * All rights reserved.
4236592Sobrien *
5236592Sobrien * Redistribution and use in source and binary forms, with or without
6236592Sobrien * modification, are permitted provided that the following conditions
7236592Sobrien * are met:
8236592Sobrien * 1. Redistributions of source code must retain the above copyright
9236592Sobrien *    notice, this list of conditions and the following disclaimer.
10236592Sobrien * 2. Redistributions in binary form must reproduce the above copyright
11236592Sobrien *    notice, this list of conditions and the following disclaimer in the
12236592Sobrien *    documentation and/or other materials provided with the distribution.
13236592Sobrien * 3. Neither the name of the project nor the names of its contributors
14236592Sobrien *    may be used to endorse or promote products derived from this software
15236592Sobrien *    without specific prior written permission.
16236592Sobrien *
17236592Sobrien * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18236592Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19236592Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20236592Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21236592Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22236592Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23236592Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24236592Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25236592Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26236592Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27236592Sobrien * SUCH DAMAGE.
28236592Sobrien *
29236592Sobrien *	$KAME: ip6_mroute.c,v 1.58 2001/12/18 02:36:31 itojun Exp $
30236592Sobrien */
31236592Sobrien
32236592Sobrien/*-
33236592Sobrien * Copyright (c) 1989 Stephen Deering
34236592Sobrien * Copyright (c) 1992, 1993
35236592Sobrien *      The Regents of the University of California.  All rights reserved.
36236592Sobrien *
37236592Sobrien * This code is derived from software contributed to Berkeley by
38236592Sobrien * Stephen Deering of Stanford University.
39236592Sobrien *
40236592Sobrien * Redistribution and use in source and binary forms, with or without
41236592Sobrien * modification, are permitted provided that the following conditions
42236592Sobrien * are met:
43236592Sobrien * 1. Redistributions of source code must retain the above copyright
44236592Sobrien *    notice, this list of conditions and the following disclaimer.
45236592Sobrien * 2. Redistributions in binary form must reproduce the above copyright
46236592Sobrien *    notice, this list of conditions and the following disclaimer in the
47236592Sobrien *    documentation and/or other materials provided with the distribution.
48236592Sobrien * 4. Neither the name of the University nor the names of its contributors
49236592Sobrien *    may be used to endorse or promote products derived from this software
50236592Sobrien *    without specific prior written permission.
51236592Sobrien *
52236592Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53236592Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54236592Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55236592Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56236592Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57236592Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58236592Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59236592Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60236592Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61236592Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62236592Sobrien * SUCH DAMAGE.
63236592Sobrien *
64236592Sobrien *	@(#)ip_mroute.c	8.2 (Berkeley) 11/15/93
65236592Sobrien *	BSDI ip_mroute.c,v 2.10 1996/11/14 00:29:52 jch Exp
66236592Sobrien */
67236592Sobrien
68236592Sobrien/*
69236592Sobrien * IP multicast forwarding procedures
70236592Sobrien *
71236592Sobrien * Written by David Waitzman, BBN Labs, August 1988.
72236592Sobrien * Modified by Steve Deering, Stanford, February 1989.
73236592Sobrien * Modified by Mark J. Steiglitz, Stanford, May, 1991
74236592Sobrien * Modified by Van Jacobson, LBL, January 1993
75236592Sobrien * Modified by Ajit Thyagarajan, PARC, August 1993
76236592Sobrien * Modified by Bill Fenner, PARC, April 1994
77236592Sobrien *
78236592Sobrien * MROUTING Revision: 3.5.1.2 + PIM-SMv2 (pimd) Support
79236592Sobrien */
80236592Sobrien
81236592Sobrien#include <sys/cdefs.h>
82236592Sobrien__FBSDID("$FreeBSD: stable/9/sys/netinet6/ip6_mroute.c 252566 2013-07-03 13:03:09Z ae $");
83236592Sobrien
84236592Sobrien#include "opt_inet6.h"
85236592Sobrien
86236592Sobrien#include <sys/param.h>
87236592Sobrien#include <sys/callout.h>
88236592Sobrien#include <sys/errno.h>
89236592Sobrien#include <sys/kernel.h>
90236592Sobrien#include <sys/lock.h>
91236592Sobrien#include <sys/malloc.h>
92236592Sobrien#include <sys/mbuf.h>
93236592Sobrien#include <sys/module.h>
94236592Sobrien#include <sys/domain.h>
95236592Sobrien#include <sys/protosw.h>
96236592Sobrien#include <sys/signalvar.h>
97236592Sobrien#include <sys/socket.h>
98236592Sobrien#include <sys/socketvar.h>
99236592Sobrien#include <sys/sockio.h>
100236592Sobrien#include <sys/sx.h>
101236592Sobrien#include <sys/sysctl.h>
102236592Sobrien#include <sys/syslog.h>
103236592Sobrien#include <sys/systm.h>
104236592Sobrien#include <sys/time.h>
105236592Sobrien
106236592Sobrien#include <net/if.h>
107236592Sobrien#include <net/if_types.h>
108236592Sobrien#include <net/raw_cb.h>
109236592Sobrien#include <net/vnet.h>
110236592Sobrien
111236592Sobrien#include <netinet/in.h>
112236592Sobrien#include <netinet/in_var.h>
113236592Sobrien#include <netinet/icmp6.h>
114236592Sobrien#include <netinet/ip_encap.h>
115236592Sobrien
116236592Sobrien#include <netinet/ip6.h>
117236592Sobrien#include <netinet6/ip6_var.h>
118236592Sobrien#include <netinet6/scope6_var.h>
119236592Sobrien#include <netinet6/nd6.h>
120236592Sobrien#include <netinet6/ip6_mroute.h>
121236592Sobrien#include <netinet6/ip6protosw.h>
122236592Sobrien#include <netinet6/pim6.h>
123236592Sobrien#include <netinet6/pim6_var.h>
124236592Sobrien
125236592Sobrienstatic MALLOC_DEFINE(M_MRTABLE6, "mf6c", "multicast forwarding cache entry");
126236592Sobrien
127236592Sobrien/* XXX: this is a very common idiom; move to <sys/mbuf.h> ? */
128236592Sobrien#define M_HASCL(m) ((m)->m_flags & M_EXT)
129236592Sobrien
130236592Sobrienstatic int	ip6_mdq(struct mbuf *, struct ifnet *, struct mf6c *);
131236592Sobrienstatic void	phyint_send(struct ip6_hdr *, struct mif6 *, struct mbuf *);
132236592Sobrienstatic int	register_send(struct ip6_hdr *, struct mif6 *, struct mbuf *);
133236592Sobrienstatic int	set_pim6(int *);
134236592Sobrienstatic int	socket_send(struct socket *, struct mbuf *,
135236592Sobrien		    struct sockaddr_in6 *);
136236592Sobrien
137236592Sobrienextern int in6_mcast_loop;
138236592Sobrienextern struct domain inet6domain;
139236592Sobrien
140236592Sobrienstatic const struct encaptab *pim6_encap_cookie;
141236592Sobrienstatic const struct ip6protosw in6_pim_protosw = {
142236592Sobrien	.pr_type =		SOCK_RAW,
143236592Sobrien	.pr_domain =		&inet6domain,
144236592Sobrien	.pr_protocol =		IPPROTO_PIM,
145236592Sobrien	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
146236592Sobrien	.pr_input =		pim6_input,
147236592Sobrien	.pr_output =		rip6_output,
148236592Sobrien	.pr_ctloutput =		rip6_ctloutput,
149236592Sobrien	.pr_usrreqs =		&rip6_usrreqs
150236592Sobrien};
151236592Sobrienstatic int pim6_encapcheck(const struct mbuf *, int, int, void *);
152236592Sobrien
153236592Sobrienstatic VNET_DEFINE(int, ip6_mrouter_ver) = 0;
154236592Sobrien#define	V_ip6_mrouter_ver	VNET(ip6_mrouter_ver)
155236592Sobrien
156236592SobrienSYSCTL_DECL(_net_inet6);
157236592SobrienSYSCTL_DECL(_net_inet6_ip6);
158236592Sobrienstatic SYSCTL_NODE(_net_inet6, IPPROTO_PIM, pim, CTLFLAG_RW, 0, "PIM");
159236592Sobrien
160236592Sobrienstatic struct mrt6stat mrt6stat;
161236592SobrienSYSCTL_STRUCT(_net_inet6_ip6, OID_AUTO, mrt6stat, CTLFLAG_RW,
162236592Sobrien    &mrt6stat, mrt6stat,
163236592Sobrien    "Multicast Routing Statistics (struct mrt6stat, netinet6/ip6_mroute.h)");
164236592Sobrien
165236592Sobrien#define	MRT6STAT_INC(name)	mrt6stat.name += 1
166236592Sobrien#define NO_RTE_FOUND	0x1
167236592Sobrien#define RTE_FOUND	0x2
168236592Sobrien
169236592Sobrienstatic struct mtx mrouter6_mtx;
170236592Sobrien#define	MROUTER6_LOCK()		mtx_lock(&mrouter6_mtx)
171236592Sobrien#define	MROUTER6_UNLOCK()	mtx_unlock(&mrouter6_mtx)
172236592Sobrien#define	MROUTER6_LOCK_ASSERT()	do {					\
173236592Sobrien	mtx_assert(&mrouter6_mtx, MA_OWNED);				\
174236592Sobrien	NET_ASSERT_GIANT();						\
175236592Sobrien} while (0)
176236592Sobrien#define	MROUTER6_LOCK_INIT()	\
177236592Sobrien	mtx_init(&mrouter6_mtx, "IPv6 multicast forwarding", NULL, MTX_DEF)
178236592Sobrien#define	MROUTER6_LOCK_DESTROY()	mtx_destroy(&mrouter6_mtx)
179236592Sobrien
180236592Sobrienstatic struct mf6c *mf6ctable[MF6CTBLSIZ];
181236592SobrienSYSCTL_OPAQUE(_net_inet6_ip6, OID_AUTO, mf6ctable, CTLFLAG_RD,
182236592Sobrien    &mf6ctable, sizeof(mf6ctable), "S,*mf6ctable[MF6CTBLSIZ]",
183236592Sobrien    "IPv6 Multicast Forwarding Table (struct *mf6ctable[MF6CTBLSIZ], "
184236592Sobrien    "netinet6/ip6_mroute.h)");
185236592Sobrien
186236592Sobrienstatic struct mtx mfc6_mtx;
187236592Sobrien#define	MFC6_LOCK()		mtx_lock(&mfc6_mtx)
188236592Sobrien#define	MFC6_UNLOCK()		mtx_unlock(&mfc6_mtx)
189236592Sobrien#define	MFC6_LOCK_ASSERT()	do {					\
190236592Sobrien	mtx_assert(&mfc6_mtx, MA_OWNED);				\
191236592Sobrien	NET_ASSERT_GIANT();						\
192236592Sobrien} while (0)
193236592Sobrien#define	MFC6_LOCK_INIT()		\
194236592Sobrien	mtx_init(&mfc6_mtx, "IPv6 multicast forwarding cache", NULL, MTX_DEF)
195236592Sobrien#define	MFC6_LOCK_DESTROY()	mtx_destroy(&mfc6_mtx)
196236592Sobrien
197236592Sobrienstatic u_char n6expire[MF6CTBLSIZ];
198236592Sobrien
199236592Sobrienstatic struct mif6 mif6table[MAXMIFS];
200236592SobrienSYSCTL_OPAQUE(_net_inet6_ip6, OID_AUTO, mif6table, CTLFLAG_RD,
201236592Sobrien    &mif6table, sizeof(mif6table), "S,mif6[MAXMIFS]",
202236592Sobrien    "IPv6 Multicast Interfaces (struct mif6[MAXMIFS], netinet6/ip6_mroute.h)");
203236592Sobrien
204236592Sobrienstatic struct mtx mif6_mtx;
205236592Sobrien#define	MIF6_LOCK()		mtx_lock(&mif6_mtx)
206236592Sobrien#define	MIF6_UNLOCK()		mtx_unlock(&mif6_mtx)
207236592Sobrien#define	MIF6_LOCK_ASSERT()	mtx_assert(&mif6_mtx, MA_OWNED)
208236592Sobrien#define	MIF6_LOCK_INIT()	\
209236592Sobrien	mtx_init(&mif6_mtx, "IPv6 multicast interfaces", NULL, MTX_DEF)
210236592Sobrien#define	MIF6_LOCK_DESTROY()	mtx_destroy(&mif6_mtx)
211236592Sobrien
212236592Sobrien#ifdef MRT6DEBUG
213236592Sobrienstatic VNET_DEFINE(u_int, mrt6debug) = 0;	/* debug level */
214236592Sobrien#define	V_mrt6debug		VNET(mrt6debug)
215236592Sobrien#define DEBUG_MFC	0x02
216236592Sobrien#define DEBUG_FORWARD	0x04
217236592Sobrien#define DEBUG_EXPIRE	0x08
218236592Sobrien#define DEBUG_XMIT	0x10
219236592Sobrien#define DEBUG_REG	0x20
220236592Sobrien#define DEBUG_PIM	0x40
221236592Sobrien#endif
222236592Sobrien
223236592Sobrienstatic void	expire_upcalls(void *);
224236592Sobrien#define	EXPIRE_TIMEOUT	(hz / 4)	/* 4x / second */
225236592Sobrien#define	UPCALL_EXPIRE	6		/* number of timeouts */
226236592Sobrien
227236592Sobrien/*
228236592Sobrien * XXX TODO: maintain a count to if_allmulti() calls in struct ifnet.
229236592Sobrien */
230236592Sobrien
231236592Sobrien/*
232236592Sobrien * 'Interfaces' associated with decapsulator (so we can tell
233236592Sobrien * packets that went through it from ones that get reflected
234236592Sobrien * by a broken gateway).  Different from IPv4 register_if,
235236592Sobrien * these interfaces are linked into the system ifnet list,
236236592Sobrien * because per-interface IPv6 statistics are maintained in
237236592Sobrien * ifp->if_afdata.  But it does not have any routes point
238236592Sobrien * to them.  I.e., packets can't be sent this way.  They
239236592Sobrien * only exist as a placeholder for multicast source
240236592Sobrien * verification.
241236592Sobrien */
242236592Sobrienstatic struct ifnet *multicast_register_if6;
243236592Sobrien
244236592Sobrien#define ENCAP_HOPS 64
245236592Sobrien
246236592Sobrien/*
247236592Sobrien * Private variables.
248236592Sobrien */
249236592Sobrienstatic mifi_t nummifs = 0;
250236592Sobrienstatic mifi_t reg_mif_num = (mifi_t)-1;
251236592Sobrien
252236592Sobrienstatic struct pim6stat pim6stat;
253236592SobrienSYSCTL_STRUCT(_net_inet6_pim, PIM6CTL_STATS, stats, CTLFLAG_RW,
254236592Sobrien    &pim6stat, pim6stat,
255236592Sobrien    "PIM Statistics (struct pim6stat, netinet6/pim6_var.h)");
256236592Sobrien
257236592Sobrien#define	PIM6STAT_INC(name)	pim6stat.name += 1
258236592Sobrienstatic VNET_DEFINE(int, pim6);
259236592Sobrien#define	V_pim6		VNET(pim6)
260236592Sobrien
261236592Sobrien/*
262236592Sobrien * Hash function for a source, group entry
263236592Sobrien */
264236592Sobrien#define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
265236592Sobrien				   (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
266236592Sobrien				   (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
267236592Sobrien				   (g).s6_addr32[2] ^ (g).s6_addr32[3])
268236592Sobrien
269236592Sobrien/*
270236592Sobrien * Find a route for a given origin IPv6 address and Multicast group address.
271236592Sobrien */
272236592Sobrien#define MF6CFIND(o, g, rt) do { \
273236592Sobrien	struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
274236592Sobrien	rt = NULL; \
275236592Sobrien	MRT6STAT_INC(mrt6s_mfc_lookups); \
276236592Sobrien	while (_rt) { \
277236592Sobrien		if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
278236592Sobrien		    IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
279236592Sobrien		    (_rt->mf6c_stall == NULL)) { \
280236592Sobrien			rt = _rt; \
281236592Sobrien			break; \
282236592Sobrien		} \
283236592Sobrien		_rt = _rt->mf6c_next; \
284236592Sobrien	} \
285236592Sobrien	if (rt == NULL) { \
286236592Sobrien		MRT6STAT_INC(mrt6s_mfc_misses); \
287236592Sobrien	} \
288236592Sobrien} while (/*CONSTCOND*/ 0)
289236592Sobrien
290236592Sobrien/*
291236592Sobrien * Macros to compute elapsed time efficiently
292236592Sobrien * Borrowed from Van Jacobson's scheduling code
293236592Sobrien * XXX: replace with timersub() ?
294236592Sobrien */
295236592Sobrien#define TV_DELTA(a, b, delta) do { \
296236592Sobrien	    int xxs; \
297236592Sobrien		\
298236592Sobrien	    delta = (a).tv_usec - (b).tv_usec; \
299236592Sobrien	    if ((xxs = (a).tv_sec - (b).tv_sec)) { \
300236592Sobrien	       switch (xxs) { \
301236592Sobrien		      case 2: \
302236592Sobrien			  delta += 1000000; \
303236592Sobrien			      /* FALLTHROUGH */ \
304236592Sobrien		      case 1: \
305236592Sobrien			  delta += 1000000; \
306236592Sobrien			  break; \
307236592Sobrien		      default: \
308236592Sobrien			  delta += (1000000 * xxs); \
309236592Sobrien	       } \
310236592Sobrien	    } \
311236592Sobrien} while (/*CONSTCOND*/ 0)
312236592Sobrien
313236592Sobrien/* XXX: replace with timercmp(a, b, <) ? */
314236592Sobrien#define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
315236592Sobrien	      (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
316236592Sobrien
317236592Sobrien#ifdef UPCALL_TIMING
318236592Sobrien#define UPCALL_MAX	50
319236592Sobrienstatic u_long upcall_data[UPCALL_MAX + 1];
320236592Sobrienstatic void collate();
321236592Sobrien#endif /* UPCALL_TIMING */
322236592Sobrien
323236592Sobrienstatic int ip6_mrouter_init(struct socket *, int, int);
324236592Sobrienstatic int add_m6fc(struct mf6cctl *);
325236592Sobrienstatic int add_m6if(struct mif6ctl *);
326236592Sobrienstatic int del_m6fc(struct mf6cctl *);
327236592Sobrienstatic int del_m6if(mifi_t *);
328236592Sobrienstatic int del_m6if_locked(mifi_t *);
329236592Sobrienstatic int get_mif6_cnt(struct sioc_mif_req6 *);
330236592Sobrienstatic int get_sg_cnt(struct sioc_sg_req6 *);
331236592Sobrien
332236592Sobrienstatic struct callout expire_upcalls_ch;
333236592Sobrien
334236592Sobrienint X_ip6_mforward(struct ip6_hdr *, struct ifnet *, struct mbuf *);
335236592Sobrienint X_ip6_mrouter_done(void);
336236592Sobrienint X_ip6_mrouter_set(struct socket *, struct sockopt *);
337236592Sobrienint X_ip6_mrouter_get(struct socket *, struct sockopt *);
338236592Sobrienint X_mrt6_ioctl(u_long, caddr_t);
339236592Sobrien
340236592Sobrien/*
341236592Sobrien * Handle MRT setsockopt commands to modify the multicast routing tables.
342236592Sobrien */
343236592Sobrienint
344236592SobrienX_ip6_mrouter_set(struct socket *so, struct sockopt *sopt)
345236592Sobrien{
346236592Sobrien	int error = 0;
347236592Sobrien	int optval;
348236592Sobrien	struct mif6ctl mifc;
349236592Sobrien	struct mf6cctl mfcc;
350236592Sobrien	mifi_t mifi;
351236592Sobrien
352236592Sobrien	if (so != V_ip6_mrouter && sopt->sopt_name != MRT6_INIT)
353236592Sobrien		return (EACCES);
354236592Sobrien
355236592Sobrien	switch (sopt->sopt_name) {
356236592Sobrien	case MRT6_INIT:
357236592Sobrien#ifdef MRT6_OINIT
358236592Sobrien	case MRT6_OINIT:
359236592Sobrien#endif
360236592Sobrien		error = sooptcopyin(sopt, &optval, sizeof(optval),
361236592Sobrien		    sizeof(optval));
362236592Sobrien		if (error)
363236592Sobrien			break;
364236592Sobrien		error = ip6_mrouter_init(so, optval, sopt->sopt_name);
365236592Sobrien		break;
366236592Sobrien	case MRT6_DONE:
367236592Sobrien		error = X_ip6_mrouter_done();
368236592Sobrien		break;
369236592Sobrien	case MRT6_ADD_MIF:
370236592Sobrien		error = sooptcopyin(sopt, &mifc, sizeof(mifc), sizeof(mifc));
371236592Sobrien		if (error)
372236592Sobrien			break;
373236592Sobrien		error = add_m6if(&mifc);
374236592Sobrien		break;
375236592Sobrien	case MRT6_ADD_MFC:
376236592Sobrien		error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc));
377236592Sobrien		if (error)
378236592Sobrien			break;
379236592Sobrien		error = add_m6fc(&mfcc);
380236592Sobrien		break;
381236592Sobrien	case MRT6_DEL_MFC:
382236592Sobrien		error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc));
383236592Sobrien		if (error)
384236592Sobrien			break;
385236592Sobrien		error = del_m6fc(&mfcc);
386236592Sobrien		break;
387236592Sobrien	case MRT6_DEL_MIF:
388236592Sobrien		error = sooptcopyin(sopt, &mifi, sizeof(mifi), sizeof(mifi));
389236592Sobrien		if (error)
390236592Sobrien			break;
391236592Sobrien		error = del_m6if(&mifi);
392236592Sobrien		break;
393236592Sobrien	case MRT6_PIM:
394236592Sobrien		error = sooptcopyin(sopt, &optval, sizeof(optval),
395236592Sobrien		    sizeof(optval));
396236592Sobrien		if (error)
397236592Sobrien			break;
398236592Sobrien		error = set_pim6(&optval);
399236592Sobrien		break;
400236592Sobrien	default:
401236592Sobrien		error = EOPNOTSUPP;
402236592Sobrien		break;
403236592Sobrien	}
404236592Sobrien
405236592Sobrien	return (error);
406236592Sobrien}
407236592Sobrien
408236592Sobrien/*
409236592Sobrien * Handle MRT getsockopt commands
410236592Sobrien */
411236592Sobrienint
412236592SobrienX_ip6_mrouter_get(struct socket *so, struct sockopt *sopt)
413236592Sobrien{
414236592Sobrien	int error = 0;
415236592Sobrien
416236592Sobrien	if (so != V_ip6_mrouter)
417236592Sobrien		return (EACCES);
418236592Sobrien
419236592Sobrien	switch (sopt->sopt_name) {
420236592Sobrien		case MRT6_PIM:
421236592Sobrien			error = sooptcopyout(sopt, &V_pim6, sizeof(V_pim6));
422236592Sobrien			break;
423236592Sobrien	}
424236592Sobrien	return (error);
425236592Sobrien}
426236592Sobrien
427236592Sobrien/*
428236592Sobrien * Handle ioctl commands to obtain information from the cache
429236592Sobrien */
430236592Sobrienint
431236592SobrienX_mrt6_ioctl(u_long cmd, caddr_t data)
432236592Sobrien{
433236592Sobrien	int ret;
434236592Sobrien
435236592Sobrien	ret = EINVAL;
436236592Sobrien
437236592Sobrien	switch (cmd) {
438236592Sobrien	case SIOCGETSGCNT_IN6:
439236592Sobrien		ret = get_sg_cnt((struct sioc_sg_req6 *)data);
440236592Sobrien		break;
441236592Sobrien
442236592Sobrien	case SIOCGETMIFCNT_IN6:
443236592Sobrien		ret = get_mif6_cnt((struct sioc_mif_req6 *)data);
444236592Sobrien		break;
445236592Sobrien
446236592Sobrien	default:
447236592Sobrien		break;
448236592Sobrien	}
449236592Sobrien
450236592Sobrien	return (ret);
451236592Sobrien}
452236592Sobrien
453236592Sobrien/*
454236592Sobrien * returns the packet, byte, rpf-failure count for the source group provided
455236592Sobrien */
456236592Sobrienstatic int
457236592Sobrienget_sg_cnt(struct sioc_sg_req6 *req)
458236592Sobrien{
459236592Sobrien	struct mf6c *rt;
460236592Sobrien	int ret;
461236592Sobrien
462236592Sobrien	ret = 0;
463236592Sobrien
464236592Sobrien	MFC6_LOCK();
465236592Sobrien
466236592Sobrien	MF6CFIND(req->src.sin6_addr, req->grp.sin6_addr, rt);
467236592Sobrien	if (rt == NULL) {
468236592Sobrien		ret = ESRCH;
469236592Sobrien	} else {
470236592Sobrien		req->pktcnt = rt->mf6c_pkt_cnt;
471236592Sobrien		req->bytecnt = rt->mf6c_byte_cnt;
472236592Sobrien		req->wrong_if = rt->mf6c_wrong_if;
473236592Sobrien	}
474236592Sobrien
475236592Sobrien	MFC6_UNLOCK();
476236592Sobrien
477236592Sobrien	return (ret);
478236592Sobrien}
479236592Sobrien
480236592Sobrien/*
481236592Sobrien * returns the input and output packet and byte counts on the mif provided
482236592Sobrien */
483236592Sobrienstatic int
484236592Sobrienget_mif6_cnt(struct sioc_mif_req6 *req)
485236592Sobrien{
486236592Sobrien	mifi_t mifi;
487236592Sobrien	int ret;
488236592Sobrien
489236592Sobrien	ret = 0;
490236592Sobrien	mifi = req->mifi;
491236592Sobrien
492236592Sobrien	MIF6_LOCK();
493236592Sobrien
494236592Sobrien	if (mifi >= nummifs) {
495236592Sobrien		ret = EINVAL;
496236592Sobrien	} else {
497236592Sobrien		req->icount = mif6table[mifi].m6_pkt_in;
498236592Sobrien		req->ocount = mif6table[mifi].m6_pkt_out;
499236592Sobrien		req->ibytes = mif6table[mifi].m6_bytes_in;
500236592Sobrien		req->obytes = mif6table[mifi].m6_bytes_out;
501236592Sobrien	}
502236592Sobrien
503236592Sobrien	MIF6_UNLOCK();
504236592Sobrien
505236592Sobrien	return (ret);
506236592Sobrien}
507236592Sobrien
508236592Sobrienstatic int
509236592Sobrienset_pim6(int *i)
510236592Sobrien{
511236592Sobrien	if ((*i != 1) && (*i != 0))
512236592Sobrien		return (EINVAL);
513236592Sobrien
514236592Sobrien	V_pim6 = *i;
515236592Sobrien
516236592Sobrien	return (0);
517236592Sobrien}
518236592Sobrien
519236592Sobrien/*
520236592Sobrien * Enable multicast routing
521236592Sobrien */
522236592Sobrienstatic int
523236592Sobrienip6_mrouter_init(struct socket *so, int v, int cmd)
524236592Sobrien{
525236592Sobrien
526236592Sobrien#ifdef MRT6DEBUG
527236592Sobrien	if (V_mrt6debug)
528236592Sobrien		log(LOG_DEBUG,
529236592Sobrien		    "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n",
530236592Sobrien		    so->so_type, so->so_proto->pr_protocol);
531236592Sobrien#endif
532236592Sobrien
533236592Sobrien	if (so->so_type != SOCK_RAW ||
534236592Sobrien	    so->so_proto->pr_protocol != IPPROTO_ICMPV6)
535236592Sobrien		return (EOPNOTSUPP);
536236592Sobrien
537236592Sobrien	if (v != 1)
538236592Sobrien		return (ENOPROTOOPT);
539236592Sobrien
540236592Sobrien	MROUTER6_LOCK();
541236592Sobrien
542236592Sobrien	if (V_ip6_mrouter != NULL) {
543236592Sobrien		MROUTER6_UNLOCK();
544236592Sobrien		return (EADDRINUSE);
545236592Sobrien	}
546236592Sobrien
547236592Sobrien	V_ip6_mrouter = so;
548236592Sobrien	V_ip6_mrouter_ver = cmd;
549236592Sobrien
550236592Sobrien	bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
551236592Sobrien	bzero((caddr_t)n6expire, sizeof(n6expire));
552236592Sobrien
553236592Sobrien	V_pim6 = 0;/* used for stubbing out/in pim stuff */
554236592Sobrien
555236592Sobrien	callout_init(&expire_upcalls_ch, 0);
556236592Sobrien	callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
557236592Sobrien	    expire_upcalls, NULL);
558236592Sobrien
559236592Sobrien	MROUTER6_UNLOCK();
560236592Sobrien
561236592Sobrien#ifdef MRT6DEBUG
562236592Sobrien	if (V_mrt6debug)
563236592Sobrien		log(LOG_DEBUG, "ip6_mrouter_init\n");
564236592Sobrien#endif
565236592Sobrien
566236592Sobrien	return (0);
567236592Sobrien}
568236592Sobrien
569236592Sobrien/*
570236592Sobrien * Disable IPv6 multicast forwarding.
571236592Sobrien */
572236592Sobrienint
573236592SobrienX_ip6_mrouter_done(void)
574236592Sobrien{
575236592Sobrien	mifi_t mifi;
576236592Sobrien	int i;
577236592Sobrien	struct mf6c *rt;
578236592Sobrien	struct rtdetq *rte;
579236592Sobrien
580236592Sobrien	MROUTER6_LOCK();
581236592Sobrien
582236592Sobrien	if (V_ip6_mrouter == NULL) {
583236592Sobrien		MROUTER6_UNLOCK();
584236592Sobrien		return (EINVAL);
585236592Sobrien	}
586236592Sobrien
587236592Sobrien	/*
588236592Sobrien	 * For each phyint in use, disable promiscuous reception of all IPv6
589236592Sobrien	 * multicasts.
590236592Sobrien	 */
591236592Sobrien	for (mifi = 0; mifi < nummifs; mifi++) {
592236592Sobrien		if (mif6table[mifi].m6_ifp &&
593236592Sobrien		    !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
594236592Sobrien			if_allmulti(mif6table[mifi].m6_ifp, 0);
595236592Sobrien		}
596236592Sobrien	}
597236592Sobrien	bzero((caddr_t)mif6table, sizeof(mif6table));
598236592Sobrien	nummifs = 0;
599236592Sobrien
600236592Sobrien	V_pim6 = 0; /* used to stub out/in pim specific code */
601236592Sobrien
602236592Sobrien	callout_stop(&expire_upcalls_ch);
603236592Sobrien
604236592Sobrien	/*
605236592Sobrien	 * Free all multicast forwarding cache entries.
606236592Sobrien	 */
607236592Sobrien	MFC6_LOCK();
608236592Sobrien	for (i = 0; i < MF6CTBLSIZ; i++) {
609236592Sobrien		rt = mf6ctable[i];
610236592Sobrien		while (rt) {
611236592Sobrien			struct mf6c *frt;
612236592Sobrien
613236592Sobrien			for (rte = rt->mf6c_stall; rte != NULL; ) {
614236592Sobrien				struct rtdetq *n = rte->next;
615236592Sobrien
616236592Sobrien				m_free(rte->m);
617236592Sobrien				free(rte, M_MRTABLE6);
618236592Sobrien				rte = n;
619236592Sobrien			}
620236592Sobrien			frt = rt;
621236592Sobrien			rt = rt->mf6c_next;
622236592Sobrien			free(frt, M_MRTABLE6);
623236592Sobrien		}
624236592Sobrien	}
625236592Sobrien	bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
626236592Sobrien	MFC6_UNLOCK();
627236592Sobrien
628236592Sobrien	/*
629236592Sobrien	 * Reset register interface
630236592Sobrien	 */
631236592Sobrien	if (reg_mif_num != (mifi_t)-1 && multicast_register_if6 != NULL) {
632236592Sobrien		if_detach(multicast_register_if6);
633236592Sobrien		if_free(multicast_register_if6);
634236592Sobrien		reg_mif_num = (mifi_t)-1;
635236592Sobrien		multicast_register_if6 = NULL;
636236592Sobrien	}
637236592Sobrien
638236592Sobrien	V_ip6_mrouter = NULL;
639236592Sobrien	V_ip6_mrouter_ver = 0;
640236592Sobrien
641236592Sobrien	MROUTER6_UNLOCK();
642236592Sobrien
643236592Sobrien#ifdef MRT6DEBUG
644236592Sobrien	if (V_mrt6debug)
645236592Sobrien		log(LOG_DEBUG, "ip6_mrouter_done\n");
646236592Sobrien#endif
647236592Sobrien
648236592Sobrien	return (0);
649236592Sobrien}
650236592Sobrien
651236592Sobrienstatic struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
652236592Sobrien
653236592Sobrien/*
654236592Sobrien * Add a mif to the mif table
655236592Sobrien */
656236592Sobrienstatic int
657236592Sobrienadd_m6if(struct mif6ctl *mifcp)
658236592Sobrien{
659236592Sobrien	struct mif6 *mifp;
660236592Sobrien	struct ifnet *ifp;
661236592Sobrien	int error;
662236592Sobrien
663236592Sobrien	MIF6_LOCK();
664236592Sobrien
665236592Sobrien	if (mifcp->mif6c_mifi >= MAXMIFS) {
666236592Sobrien		MIF6_UNLOCK();
667236592Sobrien		return (EINVAL);
668236592Sobrien	}
669236592Sobrien	mifp = mif6table + mifcp->mif6c_mifi;
670236592Sobrien	if (mifp->m6_ifp != NULL) {
671236592Sobrien		MIF6_UNLOCK();
672236592Sobrien		return (EADDRINUSE); /* XXX: is it appropriate? */
673236592Sobrien	}
674236592Sobrien	if (mifcp->mif6c_pifi == 0 || mifcp->mif6c_pifi > V_if_index) {
675236592Sobrien		MIF6_UNLOCK();
676236592Sobrien		return (ENXIO);
677236592Sobrien	}
678236592Sobrien
679236592Sobrien	ifp = ifnet_byindex(mifcp->mif6c_pifi);
680236592Sobrien
681236592Sobrien	if (mifcp->mif6c_flags & MIFF_REGISTER) {
682236592Sobrien		if (reg_mif_num == (mifi_t)-1) {
683236592Sobrien			ifp = if_alloc(IFT_OTHER);
684236592Sobrien
685236592Sobrien			if_initname(ifp, "register_mif", 0);
686236592Sobrien			ifp->if_flags |= IFF_LOOPBACK;
687236592Sobrien			if_attach(ifp);
688236592Sobrien			multicast_register_if6 = ifp;
689236592Sobrien			reg_mif_num = mifcp->mif6c_mifi;
690236592Sobrien			/*
691236592Sobrien			 * it is impossible to guess the ifindex of the
692236592Sobrien			 * register interface.  So mif6c_pifi is automatically
693236592Sobrien			 * calculated.
694236592Sobrien			 */
695236592Sobrien			mifcp->mif6c_pifi = ifp->if_index;
696236592Sobrien		} else {
697236592Sobrien			ifp = multicast_register_if6;
698236592Sobrien		}
699236592Sobrien	} else {
700236592Sobrien		/* Make sure the interface supports multicast */
701236592Sobrien		if ((ifp->if_flags & IFF_MULTICAST) == 0) {
702236592Sobrien			MIF6_UNLOCK();
703236592Sobrien			return (EOPNOTSUPP);
704236592Sobrien		}
705236592Sobrien
706236592Sobrien		error = if_allmulti(ifp, 1);
707236592Sobrien		if (error) {
708236592Sobrien			MIF6_UNLOCK();
709236592Sobrien			return (error);
710236592Sobrien		}
711236592Sobrien	}
712236592Sobrien
713236592Sobrien	mifp->m6_flags     = mifcp->mif6c_flags;
714236592Sobrien	mifp->m6_ifp       = ifp;
715236592Sobrien
716236592Sobrien	/* initialize per mif pkt counters */
717236592Sobrien	mifp->m6_pkt_in    = 0;
718236592Sobrien	mifp->m6_pkt_out   = 0;
719236592Sobrien	mifp->m6_bytes_in  = 0;
720236592Sobrien	mifp->m6_bytes_out = 0;
721236592Sobrien
722236592Sobrien	/* Adjust nummifs up if the mifi is higher than nummifs */
723236592Sobrien	if (nummifs <= mifcp->mif6c_mifi)
724236592Sobrien		nummifs = mifcp->mif6c_mifi + 1;
725236592Sobrien
726236592Sobrien	MIF6_UNLOCK();
727236592Sobrien
728236592Sobrien#ifdef MRT6DEBUG
729236592Sobrien	if (V_mrt6debug)
730236592Sobrien		log(LOG_DEBUG,
731236592Sobrien		    "add_mif #%d, phyint %s\n",
732236592Sobrien		    mifcp->mif6c_mifi,
733236592Sobrien		    ifp->if_xname);
734236592Sobrien#endif
735236592Sobrien
736236592Sobrien	return (0);
737236592Sobrien}
738236592Sobrien
739236592Sobrien/*
740236592Sobrien * Delete a mif from the mif table
741236592Sobrien */
742236592Sobrienstatic int
743236592Sobriendel_m6if_locked(mifi_t *mifip)
744236592Sobrien{
745236592Sobrien	struct mif6 *mifp = mif6table + *mifip;
746236592Sobrien	mifi_t mifi;
747	struct ifnet *ifp;
748
749	MIF6_LOCK_ASSERT();
750
751	if (*mifip >= nummifs)
752		return (EINVAL);
753	if (mifp->m6_ifp == NULL)
754		return (EINVAL);
755
756	if (!(mifp->m6_flags & MIFF_REGISTER)) {
757		/* XXX: TODO: Maintain an ALLMULTI refcount in struct ifnet. */
758		ifp = mifp->m6_ifp;
759		if_allmulti(ifp, 0);
760	} else {
761		if (reg_mif_num != (mifi_t)-1 &&
762		    multicast_register_if6 != NULL) {
763			if_detach(multicast_register_if6);
764			if_free(multicast_register_if6);
765			reg_mif_num = (mifi_t)-1;
766			multicast_register_if6 = NULL;
767		}
768	}
769
770	bzero((caddr_t)mifp, sizeof(*mifp));
771
772	/* Adjust nummifs down */
773	for (mifi = nummifs; mifi > 0; mifi--)
774		if (mif6table[mifi - 1].m6_ifp)
775			break;
776	nummifs = mifi;
777
778#ifdef MRT6DEBUG
779	if (V_mrt6debug)
780		log(LOG_DEBUG, "del_m6if %d, nummifs %d\n", *mifip, nummifs);
781#endif
782
783	return (0);
784}
785
786static int
787del_m6if(mifi_t *mifip)
788{
789	int cc;
790
791	MIF6_LOCK();
792	cc = del_m6if_locked(mifip);
793	MIF6_UNLOCK();
794
795	return (cc);
796}
797
798/*
799 * Add an mfc entry
800 */
801static int
802add_m6fc(struct mf6cctl *mfccp)
803{
804	struct mf6c *rt;
805	u_long hash;
806	struct rtdetq *rte;
807	u_short nstl;
808	char ip6bufo[INET6_ADDRSTRLEN], ip6bufg[INET6_ADDRSTRLEN];
809
810	MFC6_LOCK();
811
812	MF6CFIND(mfccp->mf6cc_origin.sin6_addr,
813		 mfccp->mf6cc_mcastgrp.sin6_addr, rt);
814
815	/* If an entry already exists, just update the fields */
816	if (rt) {
817#ifdef MRT6DEBUG
818		if (V_mrt6debug & DEBUG_MFC) {
819		    log(LOG_DEBUG,
820			"add_m6fc no upcall h %d o %s g %s p %x\n",
821			ip6_sprintf(ip6bufo, &mfccp->mf6cc_origin.sin6_addr),
822			ip6_sprintf(ip6bufg, &mfccp->mf6cc_mcastgrp.sin6_addr),
823			mfccp->mf6cc_parent);
824		}
825#endif
826
827		rt->mf6c_parent = mfccp->mf6cc_parent;
828		rt->mf6c_ifset = mfccp->mf6cc_ifset;
829
830		MFC6_UNLOCK();
831		return (0);
832	}
833
834	/*
835	 * Find the entry for which the upcall was made and update
836	 */
837	hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr,
838			mfccp->mf6cc_mcastgrp.sin6_addr);
839	for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) {
840		if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
841				       &mfccp->mf6cc_origin.sin6_addr) &&
842		    IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
843				       &mfccp->mf6cc_mcastgrp.sin6_addr) &&
844		    (rt->mf6c_stall != NULL)) {
845
846			if (nstl++)
847				log(LOG_ERR,
848				    "add_m6fc: %s o %s g %s p %x dbx %p\n",
849				    "multiple kernel entries",
850				    ip6_sprintf(ip6bufo,
851					    &mfccp->mf6cc_origin.sin6_addr),
852				    ip6_sprintf(ip6bufg,
853					    &mfccp->mf6cc_mcastgrp.sin6_addr),
854				    mfccp->mf6cc_parent, rt->mf6c_stall);
855
856#ifdef MRT6DEBUG
857			if (V_mrt6debug & DEBUG_MFC)
858				log(LOG_DEBUG,
859				    "add_m6fc o %s g %s p %x dbg %x\n",
860				    ip6_sprintf(ip6bufo,
861					    &mfccp->mf6cc_origin.sin6_addr),
862				    ip6_sprintf(ip6bufg,
863					    &mfccp->mf6cc_mcastgrp.sin6_addr),
864				    mfccp->mf6cc_parent, rt->mf6c_stall);
865#endif
866
867			rt->mf6c_origin     = mfccp->mf6cc_origin;
868			rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
869			rt->mf6c_parent     = mfccp->mf6cc_parent;
870			rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
871			/* initialize pkt counters per src-grp */
872			rt->mf6c_pkt_cnt    = 0;
873			rt->mf6c_byte_cnt   = 0;
874			rt->mf6c_wrong_if   = 0;
875
876			rt->mf6c_expire = 0;	/* Don't clean this guy up */
877			n6expire[hash]--;
878
879			/* free packets Qed at the end of this entry */
880			for (rte = rt->mf6c_stall; rte != NULL; ) {
881				struct rtdetq *n = rte->next;
882				ip6_mdq(rte->m, rte->ifp, rt);
883				m_freem(rte->m);
884#ifdef UPCALL_TIMING
885				collate(&(rte->t));
886#endif /* UPCALL_TIMING */
887				free(rte, M_MRTABLE6);
888				rte = n;
889			}
890			rt->mf6c_stall = NULL;
891		}
892	}
893
894	/*
895	 * It is possible that an entry is being inserted without an upcall
896	 */
897	if (nstl == 0) {
898#ifdef MRT6DEBUG
899		if (V_mrt6debug & DEBUG_MFC)
900		    log(LOG_DEBUG,
901			"add_mfc no upcall h %d o %s g %s p %x\n",
902			hash,
903			ip6_sprintf(ip6bufo, &mfccp->mf6cc_origin.sin6_addr),
904			ip6_sprintf(ip6bufg, &mfccp->mf6cc_mcastgrp.sin6_addr),
905			mfccp->mf6cc_parent);
906#endif
907
908		for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
909
910			if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
911					       &mfccp->mf6cc_origin.sin6_addr)&&
912			    IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
913					       &mfccp->mf6cc_mcastgrp.sin6_addr)) {
914
915				rt->mf6c_origin     = mfccp->mf6cc_origin;
916				rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
917				rt->mf6c_parent     = mfccp->mf6cc_parent;
918				rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
919				/* initialize pkt counters per src-grp */
920				rt->mf6c_pkt_cnt    = 0;
921				rt->mf6c_byte_cnt   = 0;
922				rt->mf6c_wrong_if   = 0;
923
924				if (rt->mf6c_expire)
925					n6expire[hash]--;
926				rt->mf6c_expire	   = 0;
927			}
928		}
929		if (rt == NULL) {
930			/* no upcall, so make a new entry */
931			rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6,
932						  M_NOWAIT);
933			if (rt == NULL) {
934				MFC6_UNLOCK();
935				return (ENOBUFS);
936			}
937
938			/* insert new entry at head of hash chain */
939			rt->mf6c_origin     = mfccp->mf6cc_origin;
940			rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
941			rt->mf6c_parent     = mfccp->mf6cc_parent;
942			rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
943			/* initialize pkt counters per src-grp */
944			rt->mf6c_pkt_cnt    = 0;
945			rt->mf6c_byte_cnt   = 0;
946			rt->mf6c_wrong_if   = 0;
947			rt->mf6c_expire     = 0;
948			rt->mf6c_stall = NULL;
949
950			/* link into table */
951			rt->mf6c_next  = mf6ctable[hash];
952			mf6ctable[hash] = rt;
953		}
954	}
955
956	MFC6_UNLOCK();
957	return (0);
958}
959
960#ifdef UPCALL_TIMING
961/*
962 * collect delay statistics on the upcalls
963 */
964static void
965collate(struct timeval *t)
966{
967	u_long d;
968	struct timeval tp;
969	u_long delta;
970
971	GET_TIME(tp);
972
973	if (TV_LT(*t, tp))
974	{
975		TV_DELTA(tp, *t, delta);
976
977		d = delta >> 10;
978		if (d > UPCALL_MAX)
979			d = UPCALL_MAX;
980
981		++upcall_data[d];
982	}
983}
984#endif /* UPCALL_TIMING */
985
986/*
987 * Delete an mfc entry
988 */
989static int
990del_m6fc(struct mf6cctl *mfccp)
991{
992	struct sockaddr_in6	origin;
993	struct sockaddr_in6	mcastgrp;
994	struct mf6c		*rt;
995	struct mf6c		**nptr;
996	u_long		hash;
997
998	origin = mfccp->mf6cc_origin;
999	mcastgrp = mfccp->mf6cc_mcastgrp;
1000	hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr);
1001
1002#ifdef MRT6DEBUG
1003	if (V_mrt6debug & DEBUG_MFC) {
1004		char ip6bufo[INET6_ADDRSTRLEN], ip6bufg[INET6_ADDRSTRLEN];
1005		log(LOG_DEBUG,"del_m6fc orig %s mcastgrp %s\n",
1006		    ip6_sprintf(ip6bufo, &origin.sin6_addr),
1007		    ip6_sprintf(ip6bufg, &mcastgrp.sin6_addr));
1008	}
1009#endif
1010
1011	MFC6_LOCK();
1012
1013	nptr = &mf6ctable[hash];
1014	while ((rt = *nptr) != NULL) {
1015		if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr,
1016				       &rt->mf6c_origin.sin6_addr) &&
1017		    IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr,
1018				       &rt->mf6c_mcastgrp.sin6_addr) &&
1019		    rt->mf6c_stall == NULL)
1020			break;
1021
1022		nptr = &rt->mf6c_next;
1023	}
1024	if (rt == NULL) {
1025		MFC6_UNLOCK();
1026		return (EADDRNOTAVAIL);
1027	}
1028
1029	*nptr = rt->mf6c_next;
1030	free(rt, M_MRTABLE6);
1031
1032	MFC6_UNLOCK();
1033
1034	return (0);
1035}
1036
1037static int
1038socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in6 *src)
1039{
1040
1041	if (s) {
1042		if (sbappendaddr(&s->so_rcv,
1043				 (struct sockaddr *)src,
1044				 mm, (struct mbuf *)0) != 0) {
1045			sorwakeup(s);
1046			return (0);
1047		}
1048	}
1049	m_freem(mm);
1050	return (-1);
1051}
1052
1053/*
1054 * IPv6 multicast forwarding function. This function assumes that the packet
1055 * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
1056 * pointed to by "ifp", and the packet is to be relayed to other networks
1057 * that have members of the packet's destination IPv6 multicast group.
1058 *
1059 * The packet is returned unscathed to the caller, unless it is
1060 * erroneous, in which case a non-zero return value tells the caller to
1061 * discard it.
1062 *
1063 * NOTE: this implementation assumes that m->m_pkthdr.rcvif is NULL iff
1064 * this function is called in the originating context (i.e., not when
1065 * forwarding a packet from other node).  ip6_output(), which is currently the
1066 * only function that calls this function is called in the originating context,
1067 * explicitly ensures this condition.  It is caller's responsibility to ensure
1068 * that if this function is called from somewhere else in the originating
1069 * context in the future.
1070 */
1071int
1072X_ip6_mforward(struct ip6_hdr *ip6, struct ifnet *ifp, struct mbuf *m)
1073{
1074	struct mf6c *rt;
1075	struct mif6 *mifp;
1076	struct mbuf *mm;
1077	mifi_t mifi;
1078	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1079
1080#ifdef MRT6DEBUG
1081	if (V_mrt6debug & DEBUG_FORWARD)
1082		log(LOG_DEBUG, "ip6_mforward: src %s, dst %s, ifindex %d\n",
1083		    ip6_sprintf(ip6bufs, &ip6->ip6_src),
1084		    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
1085		    ifp->if_index);
1086#endif
1087
1088	/*
1089	 * Don't forward a packet with Hop limit of zero or one,
1090	 * or a packet destined to a local-only group.
1091	 */
1092	if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) ||
1093	    IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1094		return (0);
1095	ip6->ip6_hlim--;
1096
1097	/*
1098	 * Source address check: do not forward packets with unspecified
1099	 * source. It was discussed in July 2000, on ipngwg mailing list.
1100	 * This is rather more serious than unicast cases, because some
1101	 * MLD packets can be sent with the unspecified source address
1102	 * (although such packets must normally set 1 to the hop limit field).
1103	 */
1104	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
1105		IP6STAT_INC(ip6s_cantforward);
1106		if (V_ip6_log_time + V_ip6_log_interval < time_second) {
1107			V_ip6_log_time = time_second;
1108			log(LOG_DEBUG,
1109			    "cannot forward "
1110			    "from %s to %s nxt %d received on %s\n",
1111			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
1112			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
1113			    ip6->ip6_nxt,
1114			    if_name(m->m_pkthdr.rcvif));
1115		}
1116		return (0);
1117	}
1118
1119	MFC6_LOCK();
1120
1121	/*
1122	 * Determine forwarding mifs from the forwarding cache table
1123	 */
1124	MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
1125
1126	/* Entry exists, so forward if necessary */
1127	if (rt) {
1128		MFC6_UNLOCK();
1129		return (ip6_mdq(m, ifp, rt));
1130	} else {
1131		/*
1132		 * If we don't have a route for packet's origin,
1133		 * Make a copy of the packet &
1134		 * send message to routing daemon
1135		 */
1136
1137		struct mbuf *mb0;
1138		struct rtdetq *rte;
1139		u_long hash;
1140/*		int i, npkts;*/
1141#ifdef UPCALL_TIMING
1142		struct timeval tp;
1143
1144		GET_TIME(tp);
1145#endif /* UPCALL_TIMING */
1146
1147		MRT6STAT_INC(mrt6s_no_route);
1148#ifdef MRT6DEBUG
1149		if (V_mrt6debug & (DEBUG_FORWARD | DEBUG_MFC))
1150			log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n",
1151			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
1152			    ip6_sprintf(ip6bufd, &ip6->ip6_dst));
1153#endif
1154
1155		/*
1156		 * Allocate mbufs early so that we don't do extra work if we
1157		 * are just going to fail anyway.
1158		 */
1159		rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE6,
1160					      M_NOWAIT);
1161		if (rte == NULL) {
1162			MFC6_UNLOCK();
1163			return (ENOBUFS);
1164		}
1165		mb0 = m_copy(m, 0, M_COPYALL);
1166		/*
1167		 * Pullup packet header if needed before storing it,
1168		 * as other references may modify it in the meantime.
1169		 */
1170		if (mb0 &&
1171		    (M_HASCL(mb0) || mb0->m_len < sizeof(struct ip6_hdr)))
1172			mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
1173		if (mb0 == NULL) {
1174			free(rte, M_MRTABLE6);
1175			MFC6_UNLOCK();
1176			return (ENOBUFS);
1177		}
1178
1179		/* is there an upcall waiting for this packet? */
1180		hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
1181		for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
1182			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
1183					       &rt->mf6c_origin.sin6_addr) &&
1184			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1185					       &rt->mf6c_mcastgrp.sin6_addr) &&
1186			    (rt->mf6c_stall != NULL))
1187				break;
1188		}
1189
1190		if (rt == NULL) {
1191			struct mrt6msg *im;
1192#ifdef MRT6_OINIT
1193			struct omrt6msg *oim;
1194#endif
1195
1196			/* no upcall, so make a new entry */
1197			rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6,
1198						  M_NOWAIT);
1199			if (rt == NULL) {
1200				free(rte, M_MRTABLE6);
1201				m_freem(mb0);
1202				MFC6_UNLOCK();
1203				return (ENOBUFS);
1204			}
1205			/*
1206			 * Make a copy of the header to send to the user
1207			 * level process
1208			 */
1209			mm = m_copy(mb0, 0, sizeof(struct ip6_hdr));
1210
1211			if (mm == NULL) {
1212				free(rte, M_MRTABLE6);
1213				m_freem(mb0);
1214				free(rt, M_MRTABLE6);
1215				MFC6_UNLOCK();
1216				return (ENOBUFS);
1217			}
1218
1219			/*
1220			 * Send message to routing daemon
1221			 */
1222			sin6.sin6_addr = ip6->ip6_src;
1223
1224			im = NULL;
1225#ifdef MRT6_OINIT
1226			oim = NULL;
1227#endif
1228			switch (V_ip6_mrouter_ver) {
1229#ifdef MRT6_OINIT
1230			case MRT6_OINIT:
1231				oim = mtod(mm, struct omrt6msg *);
1232				oim->im6_msgtype = MRT6MSG_NOCACHE;
1233				oim->im6_mbz = 0;
1234				break;
1235#endif
1236			case MRT6_INIT:
1237				im = mtod(mm, struct mrt6msg *);
1238				im->im6_msgtype = MRT6MSG_NOCACHE;
1239				im->im6_mbz = 0;
1240				break;
1241			default:
1242				free(rte, M_MRTABLE6);
1243				m_freem(mb0);
1244				free(rt, M_MRTABLE6);
1245				MFC6_UNLOCK();
1246				return (EINVAL);
1247			}
1248
1249#ifdef MRT6DEBUG
1250			if (V_mrt6debug & DEBUG_FORWARD)
1251				log(LOG_DEBUG,
1252				    "getting the iif info in the kernel\n");
1253#endif
1254
1255			for (mifp = mif6table, mifi = 0;
1256			     mifi < nummifs && mifp->m6_ifp != ifp;
1257			     mifp++, mifi++)
1258				;
1259
1260			switch (V_ip6_mrouter_ver) {
1261#ifdef MRT6_OINIT
1262			case MRT6_OINIT:
1263				oim->im6_mif = mifi;
1264				break;
1265#endif
1266			case MRT6_INIT:
1267				im->im6_mif = mifi;
1268				break;
1269			}
1270
1271			if (socket_send(V_ip6_mrouter, mm, &sin6) < 0) {
1272				log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
1273				    "socket queue full\n");
1274				MRT6STAT_INC(mrt6s_upq_sockfull);
1275				free(rte, M_MRTABLE6);
1276				m_freem(mb0);
1277				free(rt, M_MRTABLE6);
1278				MFC6_UNLOCK();
1279				return (ENOBUFS);
1280			}
1281
1282			MRT6STAT_INC(mrt6s_upcalls);
1283
1284			/* insert new entry at head of hash chain */
1285			bzero(rt, sizeof(*rt));
1286			rt->mf6c_origin.sin6_family = AF_INET6;
1287			rt->mf6c_origin.sin6_len = sizeof(struct sockaddr_in6);
1288			rt->mf6c_origin.sin6_addr = ip6->ip6_src;
1289			rt->mf6c_mcastgrp.sin6_family = AF_INET6;
1290			rt->mf6c_mcastgrp.sin6_len = sizeof(struct sockaddr_in6);
1291			rt->mf6c_mcastgrp.sin6_addr = ip6->ip6_dst;
1292			rt->mf6c_expire = UPCALL_EXPIRE;
1293			n6expire[hash]++;
1294			rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
1295
1296			/* link into table */
1297			rt->mf6c_next  = mf6ctable[hash];
1298			mf6ctable[hash] = rt;
1299			/* Add this entry to the end of the queue */
1300			rt->mf6c_stall = rte;
1301		} else {
1302			/* determine if q has overflowed */
1303			struct rtdetq **p;
1304			int npkts = 0;
1305
1306			for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next)
1307				if (++npkts > MAX_UPQ6) {
1308					MRT6STAT_INC(mrt6s_upq_ovflw);
1309					free(rte, M_MRTABLE6);
1310					m_freem(mb0);
1311					MFC6_UNLOCK();
1312					return (0);
1313				}
1314
1315			/* Add this entry to the end of the queue */
1316			*p = rte;
1317		}
1318
1319		rte->next = NULL;
1320		rte->m = mb0;
1321		rte->ifp = ifp;
1322#ifdef UPCALL_TIMING
1323		rte->t = tp;
1324#endif /* UPCALL_TIMING */
1325
1326		MFC6_UNLOCK();
1327
1328		return (0);
1329	}
1330}
1331
1332/*
1333 * Clean up cache entries if upcalls are not serviced
1334 * Call from the Slow Timeout mechanism, every half second.
1335 */
1336static void
1337expire_upcalls(void *unused)
1338{
1339	struct rtdetq *rte;
1340	struct mf6c *mfc, **nptr;
1341	int i;
1342
1343	MFC6_LOCK();
1344	for (i = 0; i < MF6CTBLSIZ; i++) {
1345		if (n6expire[i] == 0)
1346			continue;
1347		nptr = &mf6ctable[i];
1348		while ((mfc = *nptr) != NULL) {
1349			rte = mfc->mf6c_stall;
1350			/*
1351			 * Skip real cache entries
1352			 * Make sure it wasn't marked to not expire (shouldn't happen)
1353			 * If it expires now
1354			 */
1355			if (rte != NULL &&
1356			    mfc->mf6c_expire != 0 &&
1357			    --mfc->mf6c_expire == 0) {
1358#ifdef MRT6DEBUG
1359				if (V_mrt6debug & DEBUG_EXPIRE) {
1360					char ip6bufo[INET6_ADDRSTRLEN];
1361					char ip6bufg[INET6_ADDRSTRLEN];
1362					log(LOG_DEBUG, "expire_upcalls: expiring (%s %s)\n",
1363					    ip6_sprintf(ip6bufo, &mfc->mf6c_origin.sin6_addr),
1364					    ip6_sprintf(ip6bufg, &mfc->mf6c_mcastgrp.sin6_addr));
1365				}
1366#endif
1367				/*
1368				 * drop all the packets
1369				 * free the mbuf with the pkt, if, timing info
1370				 */
1371				do {
1372					struct rtdetq *n = rte->next;
1373					m_freem(rte->m);
1374					free(rte, M_MRTABLE6);
1375					rte = n;
1376				} while (rte != NULL);
1377				MRT6STAT_INC(mrt6s_cache_cleanups);
1378				n6expire[i]--;
1379
1380				*nptr = mfc->mf6c_next;
1381				free(mfc, M_MRTABLE6);
1382			} else {
1383				nptr = &mfc->mf6c_next;
1384			}
1385		}
1386	}
1387	MFC6_UNLOCK();
1388	callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
1389	    expire_upcalls, NULL);
1390}
1391
1392/*
1393 * Packet forwarding routine once entry in the cache is made
1394 */
1395static int
1396ip6_mdq(struct mbuf *m, struct ifnet *ifp, struct mf6c *rt)
1397{
1398	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1399	mifi_t mifi, iif;
1400	struct mif6 *mifp;
1401	int plen = m->m_pkthdr.len;
1402	struct in6_addr src0, dst0; /* copies for local work */
1403	u_int32_t iszone, idzone, oszone, odzone;
1404	int error = 0;
1405
1406/*
1407 * Macro to send packet on mif.  Since RSVP packets don't get counted on
1408 * input, they shouldn't get counted on output, so statistics keeping is
1409 * separate.
1410 */
1411
1412#define MC6_SEND(ip6, mifp, m) do {				\
1413	if ((mifp)->m6_flags & MIFF_REGISTER)			\
1414		register_send((ip6), (mifp), (m));		\
1415	else							\
1416		phyint_send((ip6), (mifp), (m));		\
1417} while (/*CONSTCOND*/ 0)
1418
1419	/*
1420	 * Don't forward if it didn't arrive from the parent mif
1421	 * for its origin.
1422	 */
1423	mifi = rt->mf6c_parent;
1424	if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
1425		/* came in the wrong interface */
1426#ifdef MRT6DEBUG
1427		if (V_mrt6debug & DEBUG_FORWARD)
1428			log(LOG_DEBUG,
1429			    "wrong if: ifid %d mifi %d mififid %x\n",
1430			    ifp->if_index, mifi,
1431			    mif6table[mifi].m6_ifp->if_index);
1432#endif
1433		MRT6STAT_INC(mrt6s_wrong_if);
1434		rt->mf6c_wrong_if++;
1435		/*
1436		 * If we are doing PIM processing, and we are forwarding
1437		 * packets on this interface, send a message to the
1438		 * routing daemon.
1439		 */
1440		/* have to make sure this is a valid mif */
1441		if (mifi < nummifs && mif6table[mifi].m6_ifp)
1442			if (V_pim6 && (m->m_flags & M_LOOP) == 0) {
1443				/*
1444				 * Check the M_LOOP flag to avoid an
1445				 * unnecessary PIM assert.
1446				 * XXX: M_LOOP is an ad-hoc hack...
1447				 */
1448				static struct sockaddr_in6 sin6 =
1449				{ sizeof(sin6), AF_INET6 };
1450
1451				struct mbuf *mm;
1452				struct mrt6msg *im;
1453#ifdef MRT6_OINIT
1454				struct omrt6msg *oim;
1455#endif
1456
1457				mm = m_copy(m, 0, sizeof(struct ip6_hdr));
1458				if (mm &&
1459				    (M_HASCL(mm) ||
1460				     mm->m_len < sizeof(struct ip6_hdr)))
1461					mm = m_pullup(mm, sizeof(struct ip6_hdr));
1462				if (mm == NULL)
1463					return (ENOBUFS);
1464
1465#ifdef MRT6_OINIT
1466				oim = NULL;
1467#endif
1468				im = NULL;
1469				switch (V_ip6_mrouter_ver) {
1470#ifdef MRT6_OINIT
1471				case MRT6_OINIT:
1472					oim = mtod(mm, struct omrt6msg *);
1473					oim->im6_msgtype = MRT6MSG_WRONGMIF;
1474					oim->im6_mbz = 0;
1475					break;
1476#endif
1477				case MRT6_INIT:
1478					im = mtod(mm, struct mrt6msg *);
1479					im->im6_msgtype = MRT6MSG_WRONGMIF;
1480					im->im6_mbz = 0;
1481					break;
1482				default:
1483					m_freem(mm);
1484					return (EINVAL);
1485				}
1486
1487				for (mifp = mif6table, iif = 0;
1488				     iif < nummifs && mifp &&
1489					     mifp->m6_ifp != ifp;
1490				     mifp++, iif++)
1491					;
1492
1493				switch (V_ip6_mrouter_ver) {
1494#ifdef MRT6_OINIT
1495				case MRT6_OINIT:
1496					oim->im6_mif = iif;
1497					sin6.sin6_addr = oim->im6_src;
1498					break;
1499#endif
1500				case MRT6_INIT:
1501					im->im6_mif = iif;
1502					sin6.sin6_addr = im->im6_src;
1503					break;
1504				}
1505
1506				MRT6STAT_INC(mrt6s_upcalls);
1507
1508				if (socket_send(V_ip6_mrouter, mm, &sin6) < 0) {
1509#ifdef MRT6DEBUG
1510					if (V_mrt6debug)
1511						log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n");
1512#endif
1513					MRT6STAT_INC(mrt6s_upq_sockfull);
1514					return (ENOBUFS);
1515				}	/* if socket Q full */
1516			}		/* if PIM */
1517		return (0);
1518	}			/* if wrong iif */
1519
1520	/* If I sourced this packet, it counts as output, else it was input. */
1521	if (m->m_pkthdr.rcvif == NULL) {
1522		/* XXX: is rcvif really NULL when output?? */
1523		mif6table[mifi].m6_pkt_out++;
1524		mif6table[mifi].m6_bytes_out += plen;
1525	} else {
1526		mif6table[mifi].m6_pkt_in++;
1527		mif6table[mifi].m6_bytes_in += plen;
1528	}
1529	rt->mf6c_pkt_cnt++;
1530	rt->mf6c_byte_cnt += plen;
1531
1532	/*
1533	 * For each mif, forward a copy of the packet if there are group
1534	 * members downstream on the interface.
1535	 */
1536	src0 = ip6->ip6_src;
1537	dst0 = ip6->ip6_dst;
1538	if ((error = in6_setscope(&src0, ifp, &iszone)) != 0 ||
1539	    (error = in6_setscope(&dst0, ifp, &idzone)) != 0) {
1540		IP6STAT_INC(ip6s_badscope);
1541		return (error);
1542	}
1543	for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++) {
1544		if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
1545			/*
1546			 * check if the outgoing packet is going to break
1547			 * a scope boundary.
1548			 * XXX For packets through PIM register tunnel
1549			 * interface, we believe a routing daemon.
1550			 */
1551			if (!(mif6table[rt->mf6c_parent].m6_flags &
1552			      MIFF_REGISTER) &&
1553			    !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
1554				if (in6_setscope(&src0, mif6table[mifi].m6_ifp,
1555				    &oszone) ||
1556				    in6_setscope(&dst0, mif6table[mifi].m6_ifp,
1557				    &odzone) ||
1558				    iszone != oszone ||
1559				    idzone != odzone) {
1560					IP6STAT_INC(ip6s_badscope);
1561					continue;
1562				}
1563			}
1564
1565			mifp->m6_pkt_out++;
1566			mifp->m6_bytes_out += plen;
1567			MC6_SEND(ip6, mifp, m);
1568		}
1569	}
1570	return (0);
1571}
1572
1573static void
1574phyint_send(struct ip6_hdr *ip6, struct mif6 *mifp, struct mbuf *m)
1575{
1576	struct mbuf *mb_copy;
1577	struct ifnet *ifp = mifp->m6_ifp;
1578	int error = 0;
1579	u_long linkmtu;
1580
1581	/*
1582	 * Make a new reference to the packet; make sure that
1583	 * the IPv6 header is actually copied, not just referenced,
1584	 * so that ip6_output() only scribbles on the copy.
1585	 */
1586	mb_copy = m_copy(m, 0, M_COPYALL);
1587	if (mb_copy &&
1588	    (M_HASCL(mb_copy) || mb_copy->m_len < sizeof(struct ip6_hdr)))
1589		mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr));
1590	if (mb_copy == NULL) {
1591		return;
1592	}
1593	/* set MCAST flag to the outgoing packet */
1594	mb_copy->m_flags |= M_MCAST;
1595
1596	/*
1597	 * If we sourced the packet, call ip6_output since we may devide
1598	 * the packet into fragments when the packet is too big for the
1599	 * outgoing interface.
1600	 * Otherwise, we can simply send the packet to the interface
1601	 * sending queue.
1602	 */
1603	if (m->m_pkthdr.rcvif == NULL) {
1604		struct ip6_moptions im6o;
1605
1606		im6o.im6o_multicast_ifp = ifp;
1607		/* XXX: ip6_output will override ip6->ip6_hlim */
1608		im6o.im6o_multicast_hlim = ip6->ip6_hlim;
1609		im6o.im6o_multicast_loop = 1;
1610		error = ip6_output(mb_copy, NULL, NULL, IPV6_FORWARDING, &im6o,
1611		    NULL, NULL);
1612
1613#ifdef MRT6DEBUG
1614		if (V_mrt6debug & DEBUG_XMIT)
1615			log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1616			    mifp - mif6table, error);
1617#endif
1618		return;
1619	}
1620
1621	/*
1622	 * If configured to loop back multicasts by default,
1623	 * loop back a copy now.
1624	 */
1625	if (in6_mcast_loop) {
1626		struct sockaddr_in6 dst6;
1627
1628		bzero(&dst6, sizeof(dst6));
1629		dst6.sin6_len = sizeof(struct sockaddr_in6);
1630		dst6.sin6_family = AF_INET6;
1631		dst6.sin6_addr = ip6->ip6_dst;
1632		ip6_mloopback(ifp, m, &dst6);
1633	}
1634
1635	/*
1636	 * Put the packet into the sending queue of the outgoing interface
1637	 * if it would fit in the MTU of the interface.
1638	 */
1639	linkmtu = IN6_LINKMTU(ifp);
1640	if (mb_copy->m_pkthdr.len <= linkmtu || linkmtu < IPV6_MMTU) {
1641		struct sockaddr_in6 dst6;
1642
1643		bzero(&dst6, sizeof(dst6));
1644		dst6.sin6_len = sizeof(struct sockaddr_in6);
1645		dst6.sin6_family = AF_INET6;
1646		dst6.sin6_addr = ip6->ip6_dst;
1647		/*
1648		 * We just call if_output instead of nd6_output here, since
1649		 * we need no ND for a multicast forwarded packet...right?
1650		 */
1651		error = (*ifp->if_output)(ifp, mb_copy,
1652		    (struct sockaddr *)&dst6, NULL);
1653#ifdef MRT6DEBUG
1654		if (V_mrt6debug & DEBUG_XMIT)
1655			log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1656			    mifp - mif6table, error);
1657#endif
1658	} else {
1659		/*
1660		 * pMTU discovery is intentionally disabled by default, since
1661		 * various router may notify pMTU in multicast, which can be
1662		 * a DDoS to a router
1663		 */
1664		if (V_ip6_mcast_pmtu)
1665			icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, linkmtu);
1666		else {
1667#ifdef MRT6DEBUG
1668			if (V_mrt6debug & DEBUG_XMIT) {
1669				char ip6bufs[INET6_ADDRSTRLEN];
1670				char ip6bufd[INET6_ADDRSTRLEN];
1671				log(LOG_DEBUG,
1672				    "phyint_send: packet too big on %s o %s "
1673				    "g %s size %d(discarded)\n",
1674				    if_name(ifp),
1675				    ip6_sprintf(ip6bufs, &ip6->ip6_src),
1676				    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
1677				    mb_copy->m_pkthdr.len);
1678			}
1679#endif /* MRT6DEBUG */
1680			m_freem(mb_copy); /* simply discard the packet */
1681		}
1682	}
1683}
1684
1685static int
1686register_send(struct ip6_hdr *ip6, struct mif6 *mif, struct mbuf *m)
1687{
1688	struct mbuf *mm;
1689	int i, len = m->m_pkthdr.len;
1690	static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
1691	struct mrt6msg *im6;
1692
1693#ifdef MRT6DEBUG
1694	if (V_mrt6debug) {
1695		char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1696		log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n",
1697		    ip6_sprintf(ip6bufs, &ip6->ip6_src),
1698		    ip6_sprintf(ip6bufd, &ip6->ip6_dst));
1699	}
1700#endif
1701	PIM6STAT_INC(pim6s_snd_registers);
1702
1703	/* Make a copy of the packet to send to the user level process */
1704	MGETHDR(mm, M_DONTWAIT, MT_HEADER);
1705	if (mm == NULL)
1706		return (ENOBUFS);
1707	mm->m_pkthdr.rcvif = NULL;
1708	mm->m_data += max_linkhdr;
1709	mm->m_len = sizeof(struct ip6_hdr);
1710
1711	if ((mm->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1712		m_freem(mm);
1713		return (ENOBUFS);
1714	}
1715	i = MHLEN - M_LEADINGSPACE(mm);
1716	if (i > len)
1717		i = len;
1718	mm = m_pullup(mm, i);
1719	if (mm == NULL)
1720		return (ENOBUFS);
1721/* TODO: check it! */
1722	mm->m_pkthdr.len = len + sizeof(struct ip6_hdr);
1723
1724	/*
1725	 * Send message to routing daemon
1726	 */
1727	sin6.sin6_addr = ip6->ip6_src;
1728
1729	im6 = mtod(mm, struct mrt6msg *);
1730	im6->im6_msgtype      = MRT6MSG_WHOLEPKT;
1731	im6->im6_mbz          = 0;
1732
1733	im6->im6_mif = mif - mif6table;
1734
1735	/* iif info is not given for reg. encap.n */
1736	MRT6STAT_INC(mrt6s_upcalls);
1737
1738	if (socket_send(V_ip6_mrouter, mm, &sin6) < 0) {
1739#ifdef MRT6DEBUG
1740		if (V_mrt6debug)
1741			log(LOG_WARNING,
1742			    "register_send: ip6_mrouter socket queue full\n");
1743#endif
1744		MRT6STAT_INC(mrt6s_upq_sockfull);
1745		return (ENOBUFS);
1746	}
1747	return (0);
1748}
1749
1750/*
1751 * pim6_encapcheck() is called by the encap6_input() path at runtime to
1752 * determine if a packet is for PIM; allowing PIM to be dynamically loaded
1753 * into the kernel.
1754 */
1755static int
1756pim6_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
1757{
1758
1759#ifdef DIAGNOSTIC
1760    KASSERT(proto == IPPROTO_PIM, ("not for IPPROTO_PIM"));
1761#endif
1762    if (proto != IPPROTO_PIM)
1763	return 0;	/* not for us; reject the datagram. */
1764
1765    return 64;		/* claim the datagram. */
1766}
1767
1768/*
1769 * PIM sparse mode hook
1770 * Receives the pim control messages, and passes them up to the listening
1771 * socket, using rip6_input.
1772 * The only message processed is the REGISTER pim message; the pim header
1773 * is stripped off, and the inner packet is passed to register_mforward.
1774 */
1775int
1776pim6_input(struct mbuf **mp, int *offp, int proto)
1777{
1778	struct pim *pim; /* pointer to a pim struct */
1779	struct ip6_hdr *ip6;
1780	int pimlen;
1781	struct mbuf *m = *mp;
1782	int minlen;
1783	int off = *offp;
1784
1785	PIM6STAT_INC(pim6s_rcv_total);
1786
1787	ip6 = mtod(m, struct ip6_hdr *);
1788	pimlen = m->m_pkthdr.len - *offp;
1789
1790	/*
1791	 * Validate lengths
1792	 */
1793	if (pimlen < PIM_MINLEN) {
1794		PIM6STAT_INC(pim6s_rcv_tooshort);
1795#ifdef MRT6DEBUG
1796		if (V_mrt6debug & DEBUG_PIM)
1797			log(LOG_DEBUG,"pim6_input: PIM packet too short\n");
1798#endif
1799		m_freem(m);
1800		return (IPPROTO_DONE);
1801	}
1802
1803	/*
1804	 * if the packet is at least as big as a REGISTER, go ahead
1805	 * and grab the PIM REGISTER header size, to avoid another
1806	 * possible m_pullup() later.
1807	 *
1808	 * PIM_MINLEN       == pimhdr + u_int32 == 8
1809	 * PIM6_REG_MINLEN   == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1810	 */
1811	minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN;
1812
1813	/*
1814	 * Make sure that the IP6 and PIM headers in contiguous memory, and
1815	 * possibly the PIM REGISTER header
1816	 */
1817#ifndef PULLDOWN_TEST
1818	IP6_EXTHDR_CHECK(m, off, minlen, IPPROTO_DONE);
1819	/* adjust pointer */
1820	ip6 = mtod(m, struct ip6_hdr *);
1821
1822	/* adjust mbuf to point to the PIM header */
1823	pim = (struct pim *)((caddr_t)ip6 + off);
1824#else
1825	IP6_EXTHDR_GET(pim, struct pim *, m, off, minlen);
1826	if (pim == NULL) {
1827		PIM6STAT_INC(pim6s_rcv_tooshort);
1828		return (IPPROTO_DONE);
1829	}
1830#endif
1831
1832#define PIM6_CHECKSUM
1833#ifdef PIM6_CHECKSUM
1834	{
1835		int cksumlen;
1836
1837		/*
1838		 * Validate checksum.
1839		 * If PIM REGISTER, exclude the data packet
1840		 */
1841		if (pim->pim_type == PIM_REGISTER)
1842			cksumlen = PIM_MINLEN;
1843		else
1844			cksumlen = pimlen;
1845
1846		if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) {
1847			PIM6STAT_INC(pim6s_rcv_badsum);
1848#ifdef MRT6DEBUG
1849			if (V_mrt6debug & DEBUG_PIM)
1850				log(LOG_DEBUG,
1851				    "pim6_input: invalid checksum\n");
1852#endif
1853			m_freem(m);
1854			return (IPPROTO_DONE);
1855		}
1856	}
1857#endif /* PIM_CHECKSUM */
1858
1859	/* PIM version check */
1860	if (pim->pim_ver != PIM_VERSION) {
1861		PIM6STAT_INC(pim6s_rcv_badversion);
1862#ifdef MRT6DEBUG
1863		log(LOG_ERR,
1864		    "pim6_input: incorrect version %d, expecting %d\n",
1865		    pim->pim_ver, PIM_VERSION);
1866#endif
1867		m_freem(m);
1868		return (IPPROTO_DONE);
1869	}
1870
1871	if (pim->pim_type == PIM_REGISTER) {
1872		/*
1873		 * since this is a REGISTER, we'll make a copy of the register
1874		 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1875		 * routing daemon.
1876		 */
1877		static struct sockaddr_in6 dst = { sizeof(dst), AF_INET6 };
1878
1879		struct mbuf *mcp;
1880		struct ip6_hdr *eip6;
1881		u_int32_t *reghdr;
1882		int rc;
1883#ifdef MRT6DEBUG
1884		char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1885#endif
1886
1887		PIM6STAT_INC(pim6s_rcv_registers);
1888
1889		if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) {
1890#ifdef MRT6DEBUG
1891			if (V_mrt6debug & DEBUG_PIM)
1892				log(LOG_DEBUG,
1893				    "pim6_input: register mif not set: %d\n",
1894				    reg_mif_num);
1895#endif
1896			m_freem(m);
1897			return (IPPROTO_DONE);
1898		}
1899
1900		reghdr = (u_int32_t *)(pim + 1);
1901
1902		if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
1903			goto pim6_input_to_daemon;
1904
1905		/*
1906		 * Validate length
1907		 */
1908		if (pimlen < PIM6_REG_MINLEN) {
1909			PIM6STAT_INC(pim6s_rcv_tooshort);
1910			PIM6STAT_INC(pim6s_rcv_badregisters);
1911#ifdef MRT6DEBUG
1912			log(LOG_ERR,
1913			    "pim6_input: register packet size too "
1914			    "small %d from %s\n",
1915			    pimlen, ip6_sprintf(ip6bufs, &ip6->ip6_src));
1916#endif
1917			m_freem(m);
1918			return (IPPROTO_DONE);
1919		}
1920
1921		eip6 = (struct ip6_hdr *) (reghdr + 1);
1922#ifdef MRT6DEBUG
1923		if (V_mrt6debug & DEBUG_PIM)
1924			log(LOG_DEBUG,
1925			    "pim6_input[register], eip6: %s -> %s, "
1926			    "eip6 plen %d\n",
1927			    ip6_sprintf(ip6bufs, &eip6->ip6_src),
1928			    ip6_sprintf(ip6bufd, &eip6->ip6_dst),
1929			    ntohs(eip6->ip6_plen));
1930#endif
1931
1932		/* verify the version number of the inner packet */
1933		if ((eip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1934			PIM6STAT_INC(pim6s_rcv_badregisters);
1935#ifdef MRT6DEBUG
1936			log(LOG_DEBUG, "pim6_input: invalid IP version (%d) "
1937			    "of the inner packet\n",
1938			    (eip6->ip6_vfc & IPV6_VERSION));
1939#endif
1940			m_freem(m);
1941			return (IPPROTO_NONE);
1942		}
1943
1944		/* verify the inner packet is destined to a mcast group */
1945		if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) {
1946			PIM6STAT_INC(pim6s_rcv_badregisters);
1947#ifdef MRT6DEBUG
1948			if (V_mrt6debug & DEBUG_PIM)
1949				log(LOG_DEBUG,
1950				    "pim6_input: inner packet of register "
1951				    "is not multicast %s\n",
1952				    ip6_sprintf(ip6bufd, &eip6->ip6_dst));
1953#endif
1954			m_freem(m);
1955			return (IPPROTO_DONE);
1956		}
1957
1958		/*
1959		 * make a copy of the whole header to pass to the daemon later.
1960		 */
1961		mcp = m_copy(m, 0, off + PIM6_REG_MINLEN);
1962		if (mcp == NULL) {
1963#ifdef MRT6DEBUG
1964			log(LOG_ERR,
1965			    "pim6_input: pim register: "
1966			    "could not copy register head\n");
1967#endif
1968			m_freem(m);
1969			return (IPPROTO_DONE);
1970		}
1971
1972		/*
1973		 * forward the inner ip6 packet; point m_data at the inner ip6.
1974		 */
1975		m_adj(m, off + PIM_MINLEN);
1976#ifdef MRT6DEBUG
1977		if (V_mrt6debug & DEBUG_PIM) {
1978			log(LOG_DEBUG,
1979			    "pim6_input: forwarding decapsulated register: "
1980			    "src %s, dst %s, mif %d\n",
1981			    ip6_sprintf(ip6bufs, &eip6->ip6_src),
1982			    ip6_sprintf(ip6bufd, &eip6->ip6_dst),
1983			    reg_mif_num);
1984		}
1985#endif
1986
1987		rc = if_simloop(mif6table[reg_mif_num].m6_ifp, m,
1988				dst.sin6_family, 0);
1989
1990		/* prepare the register head to send to the mrouting daemon */
1991		m = mcp;
1992	}
1993
1994	/*
1995	 * Pass the PIM message up to the daemon; if it is a register message
1996	 * pass the 'head' only up to the daemon. This includes the
1997	 * encapsulator ip6 header, pim header, register header and the
1998	 * encapsulated ip6 header.
1999	 */
2000  pim6_input_to_daemon:
2001	rip6_input(&m, offp, proto);
2002	return (IPPROTO_DONE);
2003}
2004
2005static int
2006ip6_mroute_modevent(module_t mod, int type, void *unused)
2007{
2008
2009	switch (type) {
2010	case MOD_LOAD:
2011		MROUTER6_LOCK_INIT();
2012		MFC6_LOCK_INIT();
2013		MIF6_LOCK_INIT();
2014
2015		pim6_encap_cookie = encap_attach_func(AF_INET6, IPPROTO_PIM,
2016			pim6_encapcheck,
2017			(const struct protosw *)&in6_pim_protosw, NULL);
2018		if (pim6_encap_cookie == NULL) {
2019			printf("ip6_mroute: unable to attach pim6 encap\n");
2020			MIF6_LOCK_DESTROY();
2021			MFC6_LOCK_DESTROY();
2022			MROUTER6_LOCK_DESTROY();
2023			return (EINVAL);
2024		}
2025
2026		ip6_mforward = X_ip6_mforward;
2027		ip6_mrouter_done = X_ip6_mrouter_done;
2028		ip6_mrouter_get = X_ip6_mrouter_get;
2029		ip6_mrouter_set = X_ip6_mrouter_set;
2030		mrt6_ioctl = X_mrt6_ioctl;
2031		break;
2032
2033	case MOD_UNLOAD:
2034		if (V_ip6_mrouter != NULL)
2035			return EINVAL;
2036
2037		if (pim6_encap_cookie) {
2038			encap_detach(pim6_encap_cookie);
2039			pim6_encap_cookie = NULL;
2040		}
2041		X_ip6_mrouter_done();
2042		ip6_mforward = NULL;
2043		ip6_mrouter_done = NULL;
2044		ip6_mrouter_get = NULL;
2045		ip6_mrouter_set = NULL;
2046		mrt6_ioctl = NULL;
2047
2048		MIF6_LOCK_DESTROY();
2049		MFC6_LOCK_DESTROY();
2050		MROUTER6_LOCK_DESTROY();
2051		break;
2052
2053	default:
2054		return (EOPNOTSUPP);
2055	}
2056
2057	return (0);
2058}
2059
2060static moduledata_t ip6_mroutemod = {
2061	"ip6_mroute",
2062	ip6_mroute_modevent,
2063	0
2064};
2065
2066DECLARE_MODULE(ip6_mroute, ip6_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);
2067