ip6_mroute.c revision 194739
193787Sdes/*-
294691Sdes * Copyright (C) 1998 WIDE Project.
393787Sdes * All rights reserved.
493787Sdes *
593787Sdes * Redistribution and use in source and binary forms, with or without
693787Sdes * modification, are permitted provided that the following conditions
793787Sdes * are met:
893787Sdes * 1. Redistributions of source code must retain the above copyright
993787Sdes *    notice, this list of conditions and the following disclaimer.
1093787Sdes * 2. Redistributions in binary form must reproduce the above copyright
1193787Sdes *    notice, this list of conditions and the following disclaimer in the
1293787Sdes *    documentation and/or other materials provided with the distribution.
1393787Sdes * 3. Neither the name of the project nor the names of its contributors
1493787Sdes *    may be used to endorse or promote products derived from this software
1593787Sdes *    without specific prior written permission.
1693787Sdes *
1793787Sdes * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1893787Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1993787Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2093787Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2193787Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2293787Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2393787Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2493787Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2593787Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2693787Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2793787Sdes * SUCH DAMAGE.
2893787Sdes *
2993787Sdes *	$KAME: ip6_mroute.c,v 1.58 2001/12/18 02:36:31 itojun Exp $
3093787Sdes */
3194691Sdes
3294691Sdes/*-
3393787Sdes * Copyright (c) 1989 Stephen Deering
3493787Sdes * Copyright (c) 1992, 1993
3594691Sdes *      The Regents of the University of California.  All rights reserved.
3694691Sdes *
3794691Sdes * This code is derived from software contributed to Berkeley by
3893787Sdes * Stephen Deering of Stanford University.
3994691Sdes *
4093787Sdes * Redistribution and use in source and binary forms, with or without
4194691Sdes * modification, are permitted provided that the following conditions
4293787Sdes * are met:
4393787Sdes * 1. Redistributions of source code must retain the above copyright
4493787Sdes *    notice, this list of conditions and the following disclaimer.
4593787Sdes * 2. Redistributions in binary form must reproduce the above copyright
4693787Sdes *    notice, this list of conditions and the following disclaimer in the
4793787Sdes *    documentation and/or other materials provided with the distribution.
4893787Sdes * 4. Neither the name of the University nor the names of its contributors
4993787Sdes *    may be used to endorse or promote products derived from this software
5093787Sdes *    without specific prior written permission.
5193787Sdes *
5293787Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5393787Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5493787Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5593787Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5693787Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5793787Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5893787Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5993787Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6093787Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6193787Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6293787Sdes * SUCH DAMAGE.
6393787Sdes *
6493787Sdes *	@(#)ip_mroute.c	8.2 (Berkeley) 11/15/93
6593787Sdes *	BSDI ip_mroute.c,v 2.10 1996/11/14 00:29:52 jch Exp
6693787Sdes */
6793787Sdes
6893787Sdes/*
6993787Sdes * IP multicast forwarding procedures
7093787Sdes *
7193787Sdes * Written by David Waitzman, BBN Labs, August 1988.
7293787Sdes * Modified by Steve Deering, Stanford, February 1989.
7393787Sdes * Modified by Mark J. Steiglitz, Stanford, May, 1991
7493787Sdes * Modified by Van Jacobson, LBL, January 1993
7593787Sdes * Modified by Ajit Thyagarajan, PARC, August 1993
7693787Sdes * Modified by Bill Fenner, PARC, April 1994
7793787Sdes *
7893787Sdes * MROUTING Revision: 3.5.1.2 + PIM-SMv2 (pimd) Support
7993787Sdes */
8093787Sdes
8193787Sdes#include <sys/cdefs.h>
8293787Sdes__FBSDID("$FreeBSD: head/sys/netinet6/ip6_mroute.c 194739 2009-06-23 17:03:45Z bz $");
8393787Sdes
8493787Sdes#include "opt_inet.h"
8593787Sdes#include "opt_inet6.h"
8693787Sdes
8793787Sdes#include <sys/param.h>
8893787Sdes#include <sys/callout.h>
8993787Sdes#include <sys/errno.h>
9093787Sdes#include <sys/kernel.h>
9193787Sdes#include <sys/lock.h>
9293787Sdes#include <sys/malloc.h>
9393787Sdes#include <sys/mbuf.h>
9493787Sdes#include <sys/module.h>
9593787Sdes#include <sys/domain.h>
9693787Sdes#include <sys/protosw.h>
9793787Sdes#include <sys/signalvar.h>
9893787Sdes#include <sys/socket.h>
9993787Sdes#include <sys/socketvar.h>
10093787Sdes#include <sys/sockio.h>
10193787Sdes#include <sys/sx.h>
10293787Sdes#include <sys/sysctl.h>
10393787Sdes#include <sys/syslog.h>
10493787Sdes#include <sys/systm.h>
10593787Sdes#include <sys/time.h>
10693787Sdes#include <sys/vimage.h>
10793787Sdes
10893787Sdes#include <net/if.h>
10993787Sdes#include <net/if_types.h>
11093787Sdes#include <net/raw_cb.h>
11193787Sdes#include <net/vnet.h>
11293787Sdes
11393787Sdes#include <netinet/in.h>
11493787Sdes#include <netinet/in_var.h>
11593787Sdes#include <netinet/icmp6.h>
11693787Sdes#include <netinet/vinet.h>
11793787Sdes#include <netinet/ip_encap.h>
11893787Sdes
11993787Sdes#include <netinet/ip6.h>
12093787Sdes#include <netinet6/ip6_var.h>
12193787Sdes#include <netinet6/scope6_var.h>
12293787Sdes#include <netinet6/nd6.h>
12393787Sdes#include <netinet6/ip6_mroute.h>
12493787Sdes#include <netinet6/ip6protosw.h>
12593787Sdes#include <netinet6/pim6.h>
12693787Sdes#include <netinet6/pim6_var.h>
12793787Sdes#include <netinet6/vinet6.h>
12893787Sdes
12993787Sdesstatic MALLOC_DEFINE(M_MRTABLE6, "mf6c", "multicast forwarding cache entry");
13093787Sdes
13193787Sdes/* XXX: this is a very common idiom; move to <sys/mbuf.h> ? */
13294691Sdes#define M_HASCL(m) ((m)->m_flags & M_EXT)
13393787Sdes
13493787Sdesstatic int	ip6_mdq(struct mbuf *, struct ifnet *, struct mf6c *);
13593787Sdesstatic void	phyint_send(struct ip6_hdr *, struct mif6 *, struct mbuf *);
13693787Sdesstatic void	pim6_init(void);
13793787Sdesstatic int	register_send(struct ip6_hdr *, struct mif6 *, struct mbuf *);
13893787Sdesstatic int	set_pim6(int *);
13993787Sdesstatic int	socket_send(struct socket *, struct mbuf *,
14093787Sdes		    struct sockaddr_in6 *);
14193787Sdes
14293787Sdesextern int in6_mcast_loop;
14393787Sdesextern struct domain inet6domain;
14493787Sdes
14593787Sdesstatic const struct encaptab *pim6_encap_cookie;
14693787Sdesstatic const struct ip6protosw in6_pim_protosw = {
14793787Sdes	.pr_type =		SOCK_RAW,
14894691Sdes	.pr_domain =		&inet6domain,
14993787Sdes	.pr_protocol =		IPPROTO_PIM,
15093787Sdes	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
15193787Sdes	.pr_input =		pim6_input,
15293787Sdes	.pr_output =		rip6_output,
15393787Sdes	.pr_ctloutput =		rip6_ctloutput,
15493787Sdes	.pr_init =		pim6_init,
15593787Sdes	.pr_usrreqs =		&rip6_usrreqs
15693787Sdes};
15793787Sdesstatic int pim6_encapcheck(const struct mbuf *, int, int, void *);
15893787Sdes
15993787Sdes#ifdef VIMAGE_GLOBALS
16093787Sdesstatic int ip6_mrouter_ver;
16193787Sdes#endif
16293787Sdes
16393787SdesSYSCTL_DECL(_net_inet6);
16493787SdesSYSCTL_DECL(_net_inet6_ip6);
16593787SdesSYSCTL_NODE(_net_inet6, IPPROTO_PIM, pim, CTLFLAG_RW, 0, "PIM");
16693787Sdes
16794691Sdesstatic struct mrt6stat mrt6stat;
16893787SdesSYSCTL_STRUCT(_net_inet6_ip6, OID_AUTO, mrt6stat, CTLFLAG_RW,
16993787Sdes    &mrt6stat, mrt6stat,
17093787Sdes    "Multicast Routing Statistics (struct mrt6stat, netinet6/ip6_mroute.h)");
17193787Sdes
17293787Sdes#define NO_RTE_FOUND	0x1
17393787Sdes#define RTE_FOUND	0x2
17493787Sdes
17593787Sdesstatic struct mtx mrouter6_mtx;
17693787Sdes#define	MROUTER6_LOCK()		mtx_lock(&mrouter6_mtx)
17793787Sdes#define	MROUTER6_UNLOCK()	mtx_unlock(&mrouter6_mtx)
17894691Sdes#define	MROUTER6_LOCK_ASSERT()	do {					\
17993787Sdes	mtx_assert(&mrouter6_mtx, MA_OWNED);				\
18094691Sdes	NET_ASSERT_GIANT();						\
18193787Sdes} while (0)
18293787Sdes#define	MROUTER6_LOCK_INIT()	\
18393787Sdes	mtx_init(&mrouter6_mtx, "IPv6 multicast forwarding", NULL, MTX_DEF)
18493787Sdes#define	MROUTER6_LOCK_DESTROY()	mtx_destroy(&mrouter6_mtx)
18593787Sdes
18693787Sdesstatic struct mf6c *mf6ctable[MF6CTBLSIZ];
18793787SdesSYSCTL_OPAQUE(_net_inet6_ip6, OID_AUTO, mf6ctable, CTLFLAG_RD,
18893787Sdes    &mf6ctable, sizeof(mf6ctable), "S,*mf6ctable[MF6CTBLSIZ]",
18993787Sdes    "IPv6 Multicast Forwarding Table (struct *mf6ctable[MF6CTBLSIZ], "
19093787Sdes    "netinet6/ip6_mroute.h)");
19193787Sdes
19293787Sdesstatic struct mtx mfc6_mtx;
19393787Sdes#define	MFC6_LOCK()		mtx_lock(&mfc6_mtx)
19493787Sdes#define	MFC6_UNLOCK()		mtx_unlock(&mfc6_mtx)
19594691Sdes#define	MFC6_LOCK_ASSERT()	do {					\
19693787Sdes	mtx_assert(&mfc6_mtx, MA_OWNED);				\
19793787Sdes	NET_ASSERT_GIANT();						\
19893787Sdes} while (0)
19993787Sdes#define	MFC6_LOCK_INIT()		\
20093787Sdes	mtx_init(&mfc6_mtx, "IPv6 multicast forwarding cache", NULL, MTX_DEF)
20193787Sdes#define	MFC6_LOCK_DESTROY()	mtx_destroy(&mfc6_mtx)
20293787Sdes
20393787Sdesstatic u_char n6expire[MF6CTBLSIZ];
20493787Sdes
20593787Sdesstatic struct mif6 mif6table[MAXMIFS];
20694691SdesSYSCTL_OPAQUE(_net_inet6_ip6, OID_AUTO, mif6table, CTLFLAG_RD,
20794691Sdes    &mif6table, sizeof(mif6table), "S,mif6[MAXMIFS]",
20894691Sdes    "IPv6 Multicast Interfaces (struct mif6[MAXMIFS], netinet6/ip6_mroute.h)");
20994691Sdes
21093787Sdesstatic struct mtx mif6_mtx;
21193787Sdes#define	MIF6_LOCK()		mtx_lock(&mif6_mtx)
21294691Sdes#define	MIF6_UNLOCK()		mtx_unlock(&mif6_mtx)
21393787Sdes#define	MIF6_LOCK_ASSERT()	mtx_assert(&mif6_mtx, MA_OWNED)
21493787Sdes#define	MIF6_LOCK_INIT()	\
21593787Sdes	mtx_init(&mif6_mtx, "IPv6 multicast interfaces", NULL, MTX_DEF)
21693787Sdes#define	MIF6_LOCK_DESTROY()	mtx_destroy(&mif6_mtx)
21793787Sdes
21894691Sdes#ifdef MRT6DEBUG
21994691Sdes#ifdef VIMAGE_GLOBALS
22093787Sdesstatic u_int mrt6debug;		/* debug level */
22193787Sdes#endif
22293787Sdes#define DEBUG_MFC	0x02
22394691Sdes#define DEBUG_FORWARD	0x04
22494691Sdes#define DEBUG_EXPIRE	0x08
22593787Sdes#define DEBUG_XMIT	0x10
22693787Sdes#define DEBUG_REG	0x20
22793787Sdes#define DEBUG_PIM	0x40
22894691Sdes#endif
22994691Sdes
23093787Sdesstatic void	expire_upcalls(void *);
23193787Sdes#define	EXPIRE_TIMEOUT	(hz / 4)	/* 4x / second */
23293787Sdes#define	UPCALL_EXPIRE	6		/* number of timeouts */
23393787Sdes
23493787Sdes/*
23593787Sdes * XXX TODO: maintain a count to if_allmulti() calls in struct ifnet.
23693787Sdes */
23793787Sdes
23893787Sdes/*
23993787Sdes * 'Interfaces' associated with decapsulator (so we can tell
24093787Sdes * packets that went through it from ones that get reflected
24193787Sdes * by a broken gateway).  Different from IPv4 register_if,
24294691Sdes * these interfaces are linked into the system ifnet list,
24394691Sdes * because per-interface IPv6 statistics are maintained in
24494691Sdes * ifp->if_afdata.  But it does not have any routes point
24593787Sdes * to them.  I.e., packets can't be sent this way.  They
24693787Sdes * only exist as a placeholder for multicast source
24794691Sdes * verification.
24893787Sdes */
24993787Sdesstatic struct ifnet *multicast_register_if6;
25093787Sdes
25193787Sdes#define ENCAP_HOPS 64
25293787Sdes
25393787Sdes/*
25493787Sdes * Private variables.
25593787Sdes */
25693787Sdesstatic mifi_t nummifs = 0;
25793787Sdesstatic mifi_t reg_mif_num = (mifi_t)-1;
25893787Sdes
25993787Sdesstatic struct pim6stat pim6stat;
26093787SdesSYSCTL_STRUCT(_net_inet6_pim, PIM6CTL_STATS, stats, CTLFLAG_RD,
26193787Sdes    &pim6stat, pim6stat,
26293787Sdes    "PIM Statistics (struct pim6stat, netinet6/pim_var.h)");
26393787Sdes
26493787Sdes#ifdef VIMAGE_GLOBALS
26593787Sdesstatic int pim6;
26694691Sdes#endif
26794691Sdes
26893787Sdes/*
26993787Sdes * Hash function for a source, group entry
27093787Sdes */
27193787Sdes#define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
27293787Sdes				   (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
27393787Sdes				   (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
27493787Sdes				   (g).s6_addr32[2] ^ (g).s6_addr32[3])
27593787Sdes
27693787Sdes/*
27793787Sdes * Find a route for a given origin IPv6 address and Multicast group address.
27893787Sdes */
27993787Sdes#define MF6CFIND(o, g, rt) do { \
28093787Sdes	struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
28193787Sdes	rt = NULL; \
28293787Sdes	mrt6stat.mrt6s_mfc_lookups++; \
28393787Sdes	while (_rt) { \
28493787Sdes		if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
28593787Sdes		    IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
28693787Sdes		    (_rt->mf6c_stall == NULL)) { \
28793787Sdes			rt = _rt; \
28893787Sdes			break; \
28993787Sdes		} \
29093787Sdes		_rt = _rt->mf6c_next; \
29193787Sdes	} \
29293787Sdes	if (rt == NULL) { \
29393787Sdes		mrt6stat.mrt6s_mfc_misses++; \
29493787Sdes	} \
29593787Sdes} while (/*CONSTCOND*/ 0)
29693787Sdes
29793787Sdes/*
29893787Sdes * Macros to compute elapsed time efficiently
29993787Sdes * Borrowed from Van Jacobson's scheduling code
30093787Sdes * XXX: replace with timersub() ?
30193787Sdes */
30293787Sdes#define TV_DELTA(a, b, delta) do { \
30393787Sdes	    int xxs; \
30493787Sdes		\
30593787Sdes	    delta = (a).tv_usec - (b).tv_usec; \
30693787Sdes	    if ((xxs = (a).tv_sec - (b).tv_sec)) { \
30793787Sdes	       switch (xxs) { \
30893787Sdes		      case 2: \
30993787Sdes			  delta += 1000000; \
31093787Sdes			      /* FALLTHROUGH */ \
31193787Sdes		      case 1: \
31293787Sdes			  delta += 1000000; \
31393787Sdes			  break; \
31493787Sdes		      default: \
31594691Sdes			  delta += (1000000 * xxs); \
31693787Sdes	       } \
31793787Sdes	    } \
31893787Sdes} while (/*CONSTCOND*/ 0)
31993787Sdes
32093787Sdes/* XXX: replace with timercmp(a, b, <) ? */
32193787Sdes#define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
32293787Sdes	      (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
32393787Sdes
32493787Sdes#ifdef UPCALL_TIMING
32593787Sdes#define UPCALL_MAX	50
32693787Sdesstatic u_long upcall_data[UPCALL_MAX + 1];
32793787Sdesstatic void collate();
32893787Sdes#endif /* UPCALL_TIMING */
32993787Sdes
33093787Sdesstatic int ip6_mrouter_init(struct socket *, int, int);
33193787Sdesstatic int add_m6fc(struct mf6cctl *);
33293787Sdesstatic int add_m6if(struct mif6ctl *);
33393787Sdesstatic int del_m6fc(struct mf6cctl *);
33493787Sdesstatic int del_m6if(mifi_t *);
33593787Sdesstatic int del_m6if_locked(mifi_t *);
33693787Sdesstatic int get_mif6_cnt(struct sioc_mif_req6 *);
33793787Sdesstatic int get_sg_cnt(struct sioc_sg_req6 *);
33893787Sdes
33993787Sdesstatic struct callout expire_upcalls_ch;
34093787Sdes
34193787Sdesint X_ip6_mforward(struct ip6_hdr *, struct ifnet *, struct mbuf *);
34293787Sdesint X_ip6_mrouter_done(void);
34393787Sdesint X_ip6_mrouter_set(struct socket *, struct sockopt *);
34493787Sdesint X_ip6_mrouter_get(struct socket *, struct sockopt *);
34593787Sdesint X_mrt6_ioctl(u_long, caddr_t);
34694691Sdes
34793787Sdesstatic void
34893787Sdespim6_init(void)
34993787Sdes{
35093787Sdes	INIT_VNET_INET6(curvnet);
35193787Sdes
35293787Sdes	V_ip6_mrouter_ver = 0;
35393787Sdes#ifdef MRT6DEBUG
35493787Sdes	V_mrt6debug = 0;	/* debug level */
35593787Sdes#endif
35693787Sdes}
35793787Sdes
35893787Sdes/*
35993787Sdes * Handle MRT setsockopt commands to modify the multicast routing tables.
36093787Sdes */
36193787Sdesint
36293787SdesX_ip6_mrouter_set(struct socket *so, struct sockopt *sopt)
36393787Sdes{
36493787Sdes	INIT_VNET_INET6(curvnet);
36593787Sdes	int error = 0;
36693787Sdes	int optval;
36793787Sdes	struct mif6ctl mifc;
36893787Sdes	struct mf6cctl mfcc;
36993787Sdes	mifi_t mifi;
37093787Sdes
37193787Sdes	if (so != V_ip6_mrouter && sopt->sopt_name != MRT6_INIT)
37293787Sdes		return (EACCES);
37393787Sdes
37493787Sdes	switch (sopt->sopt_name) {
37593787Sdes	case MRT6_INIT:
37693787Sdes#ifdef MRT6_OINIT
37793787Sdes	case MRT6_OINIT:
37893787Sdes#endif
37993787Sdes		error = sooptcopyin(sopt, &optval, sizeof(optval),
38093787Sdes		    sizeof(optval));
38193787Sdes		if (error)
38293787Sdes			break;
38393787Sdes		error = ip6_mrouter_init(so, optval, sopt->sopt_name);
38493787Sdes		break;
38593787Sdes	case MRT6_DONE:
38693787Sdes		error = X_ip6_mrouter_done();
38793787Sdes		break;
38893787Sdes	case MRT6_ADD_MIF:
38993787Sdes		error = sooptcopyin(sopt, &mifc, sizeof(mifc), sizeof(mifc));
39093787Sdes		if (error)
39193787Sdes			break;
39293787Sdes		error = add_m6if(&mifc);
39393787Sdes		break;
39493787Sdes	case MRT6_ADD_MFC:
39593787Sdes		error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc));
39693787Sdes		if (error)
39793787Sdes			break;
39893787Sdes		error = add_m6fc(&mfcc);
39993787Sdes		break;
40093787Sdes	case MRT6_DEL_MFC:
40193787Sdes		error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc));
40293787Sdes		if (error)
40393787Sdes			break;
40493787Sdes		error = del_m6fc(&mfcc);
40593787Sdes		break;
40693787Sdes	case MRT6_DEL_MIF:
40793787Sdes		error = sooptcopyin(sopt, &mifi, sizeof(mifi), sizeof(mifi));
40893787Sdes		if (error)
40993787Sdes			break;
41093787Sdes		error = del_m6if(&mifi);
41193787Sdes		break;
41293787Sdes	case MRT6_PIM:
41394889Sdes		error = sooptcopyin(sopt, &optval, sizeof(optval),
41493787Sdes		    sizeof(optval));
41593787Sdes		if (error)
41693787Sdes			break;
41793787Sdes		error = set_pim6(&optval);
41894889Sdes		break;
41993787Sdes	default:
42093787Sdes		error = EOPNOTSUPP;
42193787Sdes		break;
42293787Sdes	}
42393787Sdes
42493787Sdes	return (error);
42593787Sdes}
42693787Sdes
42793787Sdes/*
42893787Sdes * Handle MRT getsockopt commands
42993787Sdes */
43093787Sdesint
43193787SdesX_ip6_mrouter_get(struct socket *so, struct sockopt *sopt)
43293787Sdes{
43393787Sdes	INIT_VNET_INET6(curvnet);
43493787Sdes	int error = 0;
43593787Sdes
43693787Sdes	if (so != V_ip6_mrouter)
43793787Sdes		return (EACCES);
43893787Sdes
43993787Sdes	switch (sopt->sopt_name) {
44093787Sdes		case MRT6_PIM:
44193787Sdes			error = sooptcopyout(sopt, &V_pim6, sizeof(V_pim6));
44293787Sdes			break;
44393787Sdes	}
44493787Sdes	return (error);
44593787Sdes}
44693787Sdes
44793787Sdes/*
44893787Sdes * Handle ioctl commands to obtain information from the cache
44993787Sdes */
45093787Sdesint
45193787SdesX_mrt6_ioctl(u_long cmd, caddr_t data)
45293787Sdes{
45393787Sdes	int ret;
45493787Sdes
45593787Sdes	ret = EINVAL;
45693787Sdes
45793787Sdes	switch (cmd) {
45893787Sdes	case SIOCGETSGCNT_IN6:
45993787Sdes		ret = get_sg_cnt((struct sioc_sg_req6 *)data);
46093787Sdes		break;
46193787Sdes
46293787Sdes	case SIOCGETMIFCNT_IN6:
46393787Sdes		ret = get_mif6_cnt((struct sioc_mif_req6 *)data);
46493787Sdes		break;
46593787Sdes
46693787Sdes	default:
46793787Sdes		break;
46893787Sdes	}
46993787Sdes
47093787Sdes	return (ret);
47193787Sdes}
47294889Sdes
47393787Sdes/*
47493787Sdes * returns the packet, byte, rpf-failure count for the source group provided
47593787Sdes */
47693787Sdesstatic int
47794889Sdesget_sg_cnt(struct sioc_sg_req6 *req)
47893787Sdes{
47993787Sdes	struct mf6c *rt;
48093787Sdes	int ret;
48193787Sdes
48293787Sdes	ret = 0;
48393787Sdes
48493787Sdes	MFC6_LOCK();
48593787Sdes
48693787Sdes	MF6CFIND(req->src.sin6_addr, req->grp.sin6_addr, rt);
48793787Sdes	if (rt == NULL) {
48893787Sdes		ret = ESRCH;
48993787Sdes	} else {
49094889Sdes		req->pktcnt = rt->mf6c_pkt_cnt;
49193787Sdes		req->bytecnt = rt->mf6c_byte_cnt;
49293787Sdes		req->wrong_if = rt->mf6c_wrong_if;
49393787Sdes	}
49494889Sdes
49593787Sdes	MFC6_UNLOCK();
49693787Sdes
49793787Sdes	return (ret);
49893787Sdes}
49993787Sdes
50093787Sdes/*
50193787Sdes * returns the input and output packet and byte counts on the mif provided
50293787Sdes */
50393787Sdesstatic int
50493787Sdesget_mif6_cnt(struct sioc_mif_req6 *req)
50593787Sdes{
50693787Sdes	mifi_t mifi;
50793787Sdes	int ret;
50894889Sdes
50993787Sdes	ret = 0;
51093787Sdes	mifi = req->mifi;
51193787Sdes
51293787Sdes	MIF6_LOCK();
51393787Sdes
51493787Sdes	if (mifi >= nummifs) {
51593787Sdes		ret = EINVAL;
51693787Sdes	} else {
51793787Sdes		req->icount = mif6table[mifi].m6_pkt_in;
51893787Sdes		req->ocount = mif6table[mifi].m6_pkt_out;
51993787Sdes		req->ibytes = mif6table[mifi].m6_bytes_in;
52093787Sdes		req->obytes = mif6table[mifi].m6_bytes_out;
52193787Sdes	}
52294889Sdes
52393787Sdes	MIF6_UNLOCK();
52493787Sdes
52593787Sdes	return (ret);
52693787Sdes}
52793787Sdes
52894889Sdesstatic int
52993787Sdesset_pim6(int *i)
53093787Sdes{
53193787Sdes	INIT_VNET_INET6(curvnet);
53293787Sdes	if ((*i != 1) && (*i != 0))
53393787Sdes		return (EINVAL);
53493787Sdes
53593787Sdes	V_pim6 = *i;
53693787Sdes
53793787Sdes	return (0);
53893787Sdes}
53993787Sdes
54093787Sdes/*
54193787Sdes * Enable multicast routing
54293787Sdes */
54393787Sdesstatic int
54493787Sdesip6_mrouter_init(struct socket *so, int v, int cmd)
54593787Sdes{
54693787Sdes	INIT_VNET_INET6(curvnet);
54794691Sdes
54894691Sdes	V_ip6_mrouter_ver = 0;
54994691Sdes
55093787Sdes#ifdef MRT6DEBUG
55193787Sdes	V_mrt6debug = 0;
55293787Sdes
55393787Sdes	if (V_mrt6debug)
55493787Sdes		log(LOG_DEBUG,
55593787Sdes		    "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n",
55693787Sdes		    so->so_type, so->so_proto->pr_protocol);
55793787Sdes#endif
55893787Sdes
55993787Sdes	if (so->so_type != SOCK_RAW ||
560	    so->so_proto->pr_protocol != IPPROTO_ICMPV6)
561		return (EOPNOTSUPP);
562
563	if (v != 1)
564		return (ENOPROTOOPT);
565
566	MROUTER6_LOCK();
567
568	if (V_ip6_mrouter != NULL) {
569		MROUTER6_UNLOCK();
570		return (EADDRINUSE);
571	}
572
573	V_ip6_mrouter = so;
574	V_ip6_mrouter_ver = cmd;
575
576	bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
577	bzero((caddr_t)n6expire, sizeof(n6expire));
578
579	V_pim6 = 0;/* used for stubbing out/in pim stuff */
580
581	callout_init(&expire_upcalls_ch, 0);
582	callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
583	    expire_upcalls, NULL);
584
585	MROUTER6_UNLOCK();
586
587#ifdef MRT6DEBUG
588	if (V_mrt6debug)
589		log(LOG_DEBUG, "ip6_mrouter_init\n");
590#endif
591
592	return (0);
593}
594
595/*
596 * Disable IPv6 multicast forwarding.
597 */
598int
599X_ip6_mrouter_done(void)
600{
601	INIT_VNET_INET6(curvnet);
602	mifi_t mifi;
603	int i;
604	struct mf6c *rt;
605	struct rtdetq *rte;
606
607	MROUTER6_LOCK();
608
609	if (V_ip6_mrouter == NULL) {
610		MROUTER6_UNLOCK();
611		return (EINVAL);
612	}
613
614	/*
615	 * For each phyint in use, disable promiscuous reception of all IPv6
616	 * multicasts.
617	 */
618	for (mifi = 0; mifi < nummifs; mifi++) {
619		if (mif6table[mifi].m6_ifp &&
620		    !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
621			if_allmulti(mif6table[mifi].m6_ifp, 0);
622		}
623	}
624	bzero((caddr_t)mif6table, sizeof(mif6table));
625	nummifs = 0;
626
627	V_pim6 = 0; /* used to stub out/in pim specific code */
628
629	callout_stop(&expire_upcalls_ch);
630
631	/*
632	 * Free all multicast forwarding cache entries.
633	 */
634	MFC6_LOCK();
635	for (i = 0; i < MF6CTBLSIZ; i++) {
636		rt = mf6ctable[i];
637		while (rt) {
638			struct mf6c *frt;
639
640			for (rte = rt->mf6c_stall; rte != NULL; ) {
641				struct rtdetq *n = rte->next;
642
643				m_free(rte->m);
644				free(rte, M_MRTABLE6);
645				rte = n;
646			}
647			frt = rt;
648			rt = rt->mf6c_next;
649			free(frt, M_MRTABLE6);
650		}
651	}
652	bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
653	MFC6_UNLOCK();
654
655	/*
656	 * Reset register interface
657	 */
658	if (reg_mif_num != (mifi_t)-1 && multicast_register_if6 != NULL) {
659		if_detach(multicast_register_if6);
660		if_free(multicast_register_if6);
661		reg_mif_num = (mifi_t)-1;
662		multicast_register_if6 = NULL;
663	}
664
665	V_ip6_mrouter = NULL;
666	V_ip6_mrouter_ver = 0;
667
668	MROUTER6_UNLOCK();
669
670#ifdef MRT6DEBUG
671	if (V_mrt6debug)
672		log(LOG_DEBUG, "ip6_mrouter_done\n");
673#endif
674
675	return (0);
676}
677
678static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
679
680/*
681 * Add a mif to the mif table
682 */
683static int
684add_m6if(struct mif6ctl *mifcp)
685{
686	INIT_VNET_NET(curvnet);
687	struct mif6 *mifp;
688	struct ifnet *ifp;
689	int error;
690
691	MIF6_LOCK();
692
693	if (mifcp->mif6c_mifi >= MAXMIFS) {
694		MIF6_UNLOCK();
695		return (EINVAL);
696	}
697	mifp = mif6table + mifcp->mif6c_mifi;
698	if (mifp->m6_ifp != NULL) {
699		MIF6_UNLOCK();
700		return (EADDRINUSE); /* XXX: is it appropriate? */
701	}
702	if (mifcp->mif6c_pifi == 0 || mifcp->mif6c_pifi > V_if_index) {
703		MIF6_UNLOCK();
704		return (ENXIO);
705	}
706
707	ifp = ifnet_byindex(mifcp->mif6c_pifi);
708
709	if (mifcp->mif6c_flags & MIFF_REGISTER) {
710		if (reg_mif_num == (mifi_t)-1) {
711			ifp = if_alloc(IFT_OTHER);
712
713			if_initname(ifp, "register_mif", 0);
714			ifp->if_flags |= IFF_LOOPBACK;
715			if_attach(ifp);
716			multicast_register_if6 = ifp;
717			reg_mif_num = mifcp->mif6c_mifi;
718			/*
719			 * it is impossible to guess the ifindex of the
720			 * register interface.  So mif6c_pifi is automatically
721			 * calculated.
722			 */
723			mifcp->mif6c_pifi = ifp->if_index;
724		} else {
725			ifp = multicast_register_if6;
726		}
727	} else {
728		/* Make sure the interface supports multicast */
729		if ((ifp->if_flags & IFF_MULTICAST) == 0) {
730			MIF6_UNLOCK();
731			return (EOPNOTSUPP);
732		}
733
734		error = if_allmulti(ifp, 1);
735		if (error) {
736			MIF6_UNLOCK();
737			return (error);
738		}
739	}
740
741	mifp->m6_flags     = mifcp->mif6c_flags;
742	mifp->m6_ifp       = ifp;
743
744	/* initialize per mif pkt counters */
745	mifp->m6_pkt_in    = 0;
746	mifp->m6_pkt_out   = 0;
747	mifp->m6_bytes_in  = 0;
748	mifp->m6_bytes_out = 0;
749	bzero(&mifp->m6_route, sizeof(mifp->m6_route));
750
751	/* Adjust nummifs up if the mifi is higher than nummifs */
752	if (nummifs <= mifcp->mif6c_mifi)
753		nummifs = mifcp->mif6c_mifi + 1;
754
755	MIF6_UNLOCK();
756
757#ifdef MRT6DEBUG
758	if (V_mrt6debug)
759		log(LOG_DEBUG,
760		    "add_mif #%d, phyint %s\n",
761		    mifcp->mif6c_mifi,
762		    ifp->if_xname);
763#endif
764
765	return (0);
766}
767
768/*
769 * Delete a mif from the mif table
770 */
771static int
772del_m6if_locked(mifi_t *mifip)
773{
774	struct mif6 *mifp = mif6table + *mifip;
775	mifi_t mifi;
776	struct ifnet *ifp;
777
778	MIF6_LOCK_ASSERT();
779
780	if (*mifip >= nummifs)
781		return (EINVAL);
782	if (mifp->m6_ifp == NULL)
783		return (EINVAL);
784
785	if (!(mifp->m6_flags & MIFF_REGISTER)) {
786		/* XXX: TODO: Maintain an ALLMULTI refcount in struct ifnet. */
787		ifp = mifp->m6_ifp;
788		if_allmulti(ifp, 0);
789	} else {
790		if (reg_mif_num != (mifi_t)-1 &&
791		    multicast_register_if6 != NULL) {
792			if_detach(multicast_register_if6);
793			if_free(multicast_register_if6);
794			reg_mif_num = (mifi_t)-1;
795			multicast_register_if6 = NULL;
796		}
797	}
798
799	bzero((caddr_t)mifp, sizeof(*mifp));
800
801	/* Adjust nummifs down */
802	for (mifi = nummifs; mifi > 0; mifi--)
803		if (mif6table[mifi - 1].m6_ifp)
804			break;
805	nummifs = mifi;
806
807#ifdef MRT6DEBUG
808	if (V_mrt6debug)
809		log(LOG_DEBUG, "del_m6if %d, nummifs %d\n", *mifip, nummifs);
810#endif
811
812	return (0);
813}
814
815static int
816del_m6if(mifi_t *mifip)
817{
818	int cc;
819
820	MIF6_LOCK();
821	cc = del_m6if_locked(mifip);
822	MIF6_UNLOCK();
823
824	return (cc);
825}
826
827/*
828 * Add an mfc entry
829 */
830static int
831add_m6fc(struct mf6cctl *mfccp)
832{
833	struct mf6c *rt;
834	u_long hash;
835	struct rtdetq *rte;
836	u_short nstl;
837	char ip6bufo[INET6_ADDRSTRLEN], ip6bufg[INET6_ADDRSTRLEN];
838
839	MFC6_LOCK();
840
841	MF6CFIND(mfccp->mf6cc_origin.sin6_addr,
842		 mfccp->mf6cc_mcastgrp.sin6_addr, rt);
843
844	/* If an entry already exists, just update the fields */
845	if (rt) {
846#ifdef MRT6DEBUG
847		if (V_mrt6debug & DEBUG_MFC) {
848		    log(LOG_DEBUG,
849			"add_m6fc no upcall h %d o %s g %s p %x\n",
850			ip6_sprintf(ip6bufo, &mfccp->mf6cc_origin.sin6_addr),
851			ip6_sprintf(ip6bufg, &mfccp->mf6cc_mcastgrp.sin6_addr),
852			mfccp->mf6cc_parent);
853		}
854#endif
855
856		rt->mf6c_parent = mfccp->mf6cc_parent;
857		rt->mf6c_ifset = mfccp->mf6cc_ifset;
858
859		MFC6_UNLOCK();
860		return (0);
861	}
862
863	/*
864	 * Find the entry for which the upcall was made and update
865	 */
866	hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr,
867			mfccp->mf6cc_mcastgrp.sin6_addr);
868	for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) {
869		if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
870				       &mfccp->mf6cc_origin.sin6_addr) &&
871		    IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
872				       &mfccp->mf6cc_mcastgrp.sin6_addr) &&
873		    (rt->mf6c_stall != NULL)) {
874
875			if (nstl++)
876				log(LOG_ERR,
877				    "add_m6fc: %s o %s g %s p %x dbx %p\n",
878				    "multiple kernel entries",
879				    ip6_sprintf(ip6bufo,
880					    &mfccp->mf6cc_origin.sin6_addr),
881				    ip6_sprintf(ip6bufg,
882					    &mfccp->mf6cc_mcastgrp.sin6_addr),
883				    mfccp->mf6cc_parent, rt->mf6c_stall);
884
885#ifdef MRT6DEBUG
886			if (V_mrt6debug & DEBUG_MFC)
887				log(LOG_DEBUG,
888				    "add_m6fc o %s g %s p %x dbg %x\n",
889				    ip6_sprintf(ip6bufo,
890					    &mfccp->mf6cc_origin.sin6_addr),
891				    ip6_sprintf(ip6bufg,
892					    &mfccp->mf6cc_mcastgrp.sin6_addr),
893				    mfccp->mf6cc_parent, rt->mf6c_stall);
894#endif
895
896			rt->mf6c_origin     = mfccp->mf6cc_origin;
897			rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
898			rt->mf6c_parent     = mfccp->mf6cc_parent;
899			rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
900			/* initialize pkt counters per src-grp */
901			rt->mf6c_pkt_cnt    = 0;
902			rt->mf6c_byte_cnt   = 0;
903			rt->mf6c_wrong_if   = 0;
904
905			rt->mf6c_expire = 0;	/* Don't clean this guy up */
906			n6expire[hash]--;
907
908			/* free packets Qed at the end of this entry */
909			for (rte = rt->mf6c_stall; rte != NULL; ) {
910				struct rtdetq *n = rte->next;
911				ip6_mdq(rte->m, rte->ifp, rt);
912				m_freem(rte->m);
913#ifdef UPCALL_TIMING
914				collate(&(rte->t));
915#endif /* UPCALL_TIMING */
916				free(rte, M_MRTABLE6);
917				rte = n;
918			}
919			rt->mf6c_stall = NULL;
920		}
921	}
922
923	/*
924	 * It is possible that an entry is being inserted without an upcall
925	 */
926	if (nstl == 0) {
927#ifdef MRT6DEBUG
928		if (V_mrt6debug & DEBUG_MFC)
929		    log(LOG_DEBUG,
930			"add_mfc no upcall h %d o %s g %s p %x\n",
931			hash,
932			ip6_sprintf(ip6bufo, &mfccp->mf6cc_origin.sin6_addr),
933			ip6_sprintf(ip6bufg, &mfccp->mf6cc_mcastgrp.sin6_addr),
934			mfccp->mf6cc_parent);
935#endif
936
937		for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
938
939			if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
940					       &mfccp->mf6cc_origin.sin6_addr)&&
941			    IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
942					       &mfccp->mf6cc_mcastgrp.sin6_addr)) {
943
944				rt->mf6c_origin     = mfccp->mf6cc_origin;
945				rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
946				rt->mf6c_parent     = mfccp->mf6cc_parent;
947				rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
948				/* initialize pkt counters per src-grp */
949				rt->mf6c_pkt_cnt    = 0;
950				rt->mf6c_byte_cnt   = 0;
951				rt->mf6c_wrong_if   = 0;
952
953				if (rt->mf6c_expire)
954					n6expire[hash]--;
955				rt->mf6c_expire	   = 0;
956			}
957		}
958		if (rt == NULL) {
959			/* no upcall, so make a new entry */
960			rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6,
961						  M_NOWAIT);
962			if (rt == NULL) {
963				MFC6_UNLOCK();
964				return (ENOBUFS);
965			}
966
967			/* insert new entry at head of hash chain */
968			rt->mf6c_origin     = mfccp->mf6cc_origin;
969			rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
970			rt->mf6c_parent     = mfccp->mf6cc_parent;
971			rt->mf6c_ifset	    = mfccp->mf6cc_ifset;
972			/* initialize pkt counters per src-grp */
973			rt->mf6c_pkt_cnt    = 0;
974			rt->mf6c_byte_cnt   = 0;
975			rt->mf6c_wrong_if   = 0;
976			rt->mf6c_expire     = 0;
977			rt->mf6c_stall = NULL;
978
979			/* link into table */
980			rt->mf6c_next  = mf6ctable[hash];
981			mf6ctable[hash] = rt;
982		}
983	}
984
985	MFC6_UNLOCK();
986	return (0);
987}
988
989#ifdef UPCALL_TIMING
990/*
991 * collect delay statistics on the upcalls
992 */
993static void
994collate(struct timeval *t)
995{
996	u_long d;
997	struct timeval tp;
998	u_long delta;
999
1000	GET_TIME(tp);
1001
1002	if (TV_LT(*t, tp))
1003	{
1004		TV_DELTA(tp, *t, delta);
1005
1006		d = delta >> 10;
1007		if (d > UPCALL_MAX)
1008			d = UPCALL_MAX;
1009
1010		++upcall_data[d];
1011	}
1012}
1013#endif /* UPCALL_TIMING */
1014
1015/*
1016 * Delete an mfc entry
1017 */
1018static int
1019del_m6fc(struct mf6cctl *mfccp)
1020{
1021	struct sockaddr_in6	origin;
1022	struct sockaddr_in6	mcastgrp;
1023	struct mf6c		*rt;
1024	struct mf6c		**nptr;
1025	u_long		hash;
1026
1027	origin = mfccp->mf6cc_origin;
1028	mcastgrp = mfccp->mf6cc_mcastgrp;
1029	hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr);
1030
1031#ifdef MRT6DEBUG
1032	if (V_mrt6debug & DEBUG_MFC) {
1033		char ip6bufo[INET6_ADDRSTRLEN], ip6bufg[INET6_ADDRSTRLEN];
1034		log(LOG_DEBUG,"del_m6fc orig %s mcastgrp %s\n",
1035		    ip6_sprintf(ip6bufo, &origin.sin6_addr),
1036		    ip6_sprintf(ip6bufg, &mcastgrp.sin6_addr));
1037	}
1038#endif
1039
1040	MFC6_LOCK();
1041
1042	nptr = &mf6ctable[hash];
1043	while ((rt = *nptr) != NULL) {
1044		if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr,
1045				       &rt->mf6c_origin.sin6_addr) &&
1046		    IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr,
1047				       &rt->mf6c_mcastgrp.sin6_addr) &&
1048		    rt->mf6c_stall == NULL)
1049			break;
1050
1051		nptr = &rt->mf6c_next;
1052	}
1053	if (rt == NULL) {
1054		MFC6_UNLOCK();
1055		return (EADDRNOTAVAIL);
1056	}
1057
1058	*nptr = rt->mf6c_next;
1059	free(rt, M_MRTABLE6);
1060
1061	MFC6_UNLOCK();
1062
1063	return (0);
1064}
1065
1066static int
1067socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in6 *src)
1068{
1069
1070	if (s) {
1071		if (sbappendaddr(&s->so_rcv,
1072				 (struct sockaddr *)src,
1073				 mm, (struct mbuf *)0) != 0) {
1074			sorwakeup(s);
1075			return (0);
1076		}
1077	}
1078	m_freem(mm);
1079	return (-1);
1080}
1081
1082/*
1083 * IPv6 multicast forwarding function. This function assumes that the packet
1084 * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
1085 * pointed to by "ifp", and the packet is to be relayed to other networks
1086 * that have members of the packet's destination IPv6 multicast group.
1087 *
1088 * The packet is returned unscathed to the caller, unless it is
1089 * erroneous, in which case a non-zero return value tells the caller to
1090 * discard it.
1091 *
1092 * NOTE: this implementation assumes that m->m_pkthdr.rcvif is NULL iff
1093 * this function is called in the originating context (i.e., not when
1094 * forwarding a packet from other node).  ip6_output(), which is currently the
1095 * only function that calls this function is called in the originating context,
1096 * explicitly ensures this condition.  It is caller's responsibility to ensure
1097 * that if this function is called from somewhere else in the originating
1098 * context in the future.
1099 */
1100int
1101X_ip6_mforward(struct ip6_hdr *ip6, struct ifnet *ifp, struct mbuf *m)
1102{
1103	INIT_VNET_INET6(curvnet);
1104	struct mf6c *rt;
1105	struct mif6 *mifp;
1106	struct mbuf *mm;
1107	mifi_t mifi;
1108	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1109
1110#ifdef MRT6DEBUG
1111	if (V_mrt6debug & DEBUG_FORWARD)
1112		log(LOG_DEBUG, "ip6_mforward: src %s, dst %s, ifindex %d\n",
1113		    ip6_sprintf(ip6bufs, &ip6->ip6_src),
1114		    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
1115		    ifp->if_index);
1116#endif
1117
1118	/*
1119	 * Don't forward a packet with Hop limit of zero or one,
1120	 * or a packet destined to a local-only group.
1121	 */
1122	if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) ||
1123	    IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1124		return (0);
1125	ip6->ip6_hlim--;
1126
1127	/*
1128	 * Source address check: do not forward packets with unspecified
1129	 * source. It was discussed in July 2000, on ipngwg mailing list.
1130	 * This is rather more serious than unicast cases, because some
1131	 * MLD packets can be sent with the unspecified source address
1132	 * (although such packets must normally set 1 to the hop limit field).
1133	 */
1134	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
1135		V_ip6stat.ip6s_cantforward++;
1136		if (V_ip6_log_time + V_ip6_log_interval < time_second) {
1137			V_ip6_log_time = time_second;
1138			log(LOG_DEBUG,
1139			    "cannot forward "
1140			    "from %s to %s nxt %d received on %s\n",
1141			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
1142			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
1143			    ip6->ip6_nxt,
1144			    if_name(m->m_pkthdr.rcvif));
1145		}
1146		return (0);
1147	}
1148
1149	MFC6_LOCK();
1150
1151	/*
1152	 * Determine forwarding mifs from the forwarding cache table
1153	 */
1154	MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
1155
1156	/* Entry exists, so forward if necessary */
1157	if (rt) {
1158		MFC6_UNLOCK();
1159		return (ip6_mdq(m, ifp, rt));
1160	} else {
1161		/*
1162		 * If we don't have a route for packet's origin,
1163		 * Make a copy of the packet &
1164		 * send message to routing daemon
1165		 */
1166
1167		struct mbuf *mb0;
1168		struct rtdetq *rte;
1169		u_long hash;
1170/*		int i, npkts;*/
1171#ifdef UPCALL_TIMING
1172		struct timeval tp;
1173
1174		GET_TIME(tp);
1175#endif /* UPCALL_TIMING */
1176
1177		mrt6stat.mrt6s_no_route++;
1178#ifdef MRT6DEBUG
1179		if (V_mrt6debug & (DEBUG_FORWARD | DEBUG_MFC))
1180			log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n",
1181			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
1182			    ip6_sprintf(ip6bufd, &ip6->ip6_dst));
1183#endif
1184
1185		/*
1186		 * Allocate mbufs early so that we don't do extra work if we
1187		 * are just going to fail anyway.
1188		 */
1189		rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE6,
1190					      M_NOWAIT);
1191		if (rte == NULL) {
1192			MFC6_UNLOCK();
1193			return (ENOBUFS);
1194		}
1195		mb0 = m_copy(m, 0, M_COPYALL);
1196		/*
1197		 * Pullup packet header if needed before storing it,
1198		 * as other references may modify it in the meantime.
1199		 */
1200		if (mb0 &&
1201		    (M_HASCL(mb0) || mb0->m_len < sizeof(struct ip6_hdr)))
1202			mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
1203		if (mb0 == NULL) {
1204			free(rte, M_MRTABLE6);
1205			MFC6_UNLOCK();
1206			return (ENOBUFS);
1207		}
1208
1209		/* is there an upcall waiting for this packet? */
1210		hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
1211		for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
1212			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
1213					       &rt->mf6c_origin.sin6_addr) &&
1214			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1215					       &rt->mf6c_mcastgrp.sin6_addr) &&
1216			    (rt->mf6c_stall != NULL))
1217				break;
1218		}
1219
1220		if (rt == NULL) {
1221			struct mrt6msg *im;
1222#ifdef MRT6_OINIT
1223			struct omrt6msg *oim;
1224#endif
1225
1226			/* no upcall, so make a new entry */
1227			rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6,
1228						  M_NOWAIT);
1229			if (rt == NULL) {
1230				free(rte, M_MRTABLE6);
1231				m_freem(mb0);
1232				MFC6_UNLOCK();
1233				return (ENOBUFS);
1234			}
1235			/*
1236			 * Make a copy of the header to send to the user
1237			 * level process
1238			 */
1239			mm = m_copy(mb0, 0, sizeof(struct ip6_hdr));
1240
1241			if (mm == NULL) {
1242				free(rte, M_MRTABLE6);
1243				m_freem(mb0);
1244				free(rt, M_MRTABLE6);
1245				MFC6_UNLOCK();
1246				return (ENOBUFS);
1247			}
1248
1249			/*
1250			 * Send message to routing daemon
1251			 */
1252			sin6.sin6_addr = ip6->ip6_src;
1253
1254			im = NULL;
1255#ifdef MRT6_OINIT
1256			oim = NULL;
1257#endif
1258			switch (V_ip6_mrouter_ver) {
1259#ifdef MRT6_OINIT
1260			case MRT6_OINIT:
1261				oim = mtod(mm, struct omrt6msg *);
1262				oim->im6_msgtype = MRT6MSG_NOCACHE;
1263				oim->im6_mbz = 0;
1264				break;
1265#endif
1266			case MRT6_INIT:
1267				im = mtod(mm, struct mrt6msg *);
1268				im->im6_msgtype = MRT6MSG_NOCACHE;
1269				im->im6_mbz = 0;
1270				break;
1271			default:
1272				free(rte, M_MRTABLE6);
1273				m_freem(mb0);
1274				free(rt, M_MRTABLE6);
1275				MFC6_UNLOCK();
1276				return (EINVAL);
1277			}
1278
1279#ifdef MRT6DEBUG
1280			if (V_mrt6debug & DEBUG_FORWARD)
1281				log(LOG_DEBUG,
1282				    "getting the iif info in the kernel\n");
1283#endif
1284
1285			for (mifp = mif6table, mifi = 0;
1286			     mifi < nummifs && mifp->m6_ifp != ifp;
1287			     mifp++, mifi++)
1288				;
1289
1290			switch (V_ip6_mrouter_ver) {
1291#ifdef MRT6_OINIT
1292			case MRT6_OINIT:
1293				oim->im6_mif = mifi;
1294				break;
1295#endif
1296			case MRT6_INIT:
1297				im->im6_mif = mifi;
1298				break;
1299			}
1300
1301			if (socket_send(V_ip6_mrouter, mm, &sin6) < 0) {
1302				log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
1303				    "socket queue full\n");
1304				mrt6stat.mrt6s_upq_sockfull++;
1305				free(rte, M_MRTABLE6);
1306				m_freem(mb0);
1307				free(rt, M_MRTABLE6);
1308				MFC6_UNLOCK();
1309				return (ENOBUFS);
1310			}
1311
1312			mrt6stat.mrt6s_upcalls++;
1313
1314			/* insert new entry at head of hash chain */
1315			bzero(rt, sizeof(*rt));
1316			rt->mf6c_origin.sin6_family = AF_INET6;
1317			rt->mf6c_origin.sin6_len = sizeof(struct sockaddr_in6);
1318			rt->mf6c_origin.sin6_addr = ip6->ip6_src;
1319			rt->mf6c_mcastgrp.sin6_family = AF_INET6;
1320			rt->mf6c_mcastgrp.sin6_len = sizeof(struct sockaddr_in6);
1321			rt->mf6c_mcastgrp.sin6_addr = ip6->ip6_dst;
1322			rt->mf6c_expire = UPCALL_EXPIRE;
1323			n6expire[hash]++;
1324			rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
1325
1326			/* link into table */
1327			rt->mf6c_next  = mf6ctable[hash];
1328			mf6ctable[hash] = rt;
1329			/* Add this entry to the end of the queue */
1330			rt->mf6c_stall = rte;
1331		} else {
1332			/* determine if q has overflowed */
1333			struct rtdetq **p;
1334			int npkts = 0;
1335
1336			for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next)
1337				if (++npkts > MAX_UPQ6) {
1338					mrt6stat.mrt6s_upq_ovflw++;
1339					free(rte, M_MRTABLE6);
1340					m_freem(mb0);
1341					MFC6_UNLOCK();
1342					return (0);
1343				}
1344
1345			/* Add this entry to the end of the queue */
1346			*p = rte;
1347		}
1348
1349		rte->next = NULL;
1350		rte->m = mb0;
1351		rte->ifp = ifp;
1352#ifdef UPCALL_TIMING
1353		rte->t = tp;
1354#endif /* UPCALL_TIMING */
1355
1356		MFC6_UNLOCK();
1357
1358		return (0);
1359	}
1360}
1361
1362/*
1363 * Clean up cache entries if upcalls are not serviced
1364 * Call from the Slow Timeout mechanism, every half second.
1365 */
1366static void
1367expire_upcalls(void *unused)
1368{
1369	struct rtdetq *rte;
1370	struct mf6c *mfc, **nptr;
1371	int i;
1372
1373	MFC6_LOCK();
1374	for (i = 0; i < MF6CTBLSIZ; i++) {
1375		if (n6expire[i] == 0)
1376			continue;
1377		nptr = &mf6ctable[i];
1378		while ((mfc = *nptr) != NULL) {
1379			rte = mfc->mf6c_stall;
1380			/*
1381			 * Skip real cache entries
1382			 * Make sure it wasn't marked to not expire (shouldn't happen)
1383			 * If it expires now
1384			 */
1385			if (rte != NULL &&
1386			    mfc->mf6c_expire != 0 &&
1387			    --mfc->mf6c_expire == 0) {
1388#ifdef MRT6DEBUG
1389				if (V_mrt6debug & DEBUG_EXPIRE) {
1390					char ip6bufo[INET6_ADDRSTRLEN];
1391					char ip6bufg[INET6_ADDRSTRLEN];
1392					log(LOG_DEBUG, "expire_upcalls: expiring (%s %s)\n",
1393					    ip6_sprintf(ip6bufo, &mfc->mf6c_origin.sin6_addr),
1394					    ip6_sprintf(ip6bufg, &mfc->mf6c_mcastgrp.sin6_addr));
1395				}
1396#endif
1397				/*
1398				 * drop all the packets
1399				 * free the mbuf with the pkt, if, timing info
1400				 */
1401				do {
1402					struct rtdetq *n = rte->next;
1403					m_freem(rte->m);
1404					free(rte, M_MRTABLE6);
1405					rte = n;
1406				} while (rte != NULL);
1407				mrt6stat.mrt6s_cache_cleanups++;
1408				n6expire[i]--;
1409
1410				*nptr = mfc->mf6c_next;
1411				free(mfc, M_MRTABLE6);
1412			} else {
1413				nptr = &mfc->mf6c_next;
1414			}
1415		}
1416	}
1417	MFC6_UNLOCK();
1418	callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
1419	    expire_upcalls, NULL);
1420}
1421
1422/*
1423 * Packet forwarding routine once entry in the cache is made
1424 */
1425static int
1426ip6_mdq(struct mbuf *m, struct ifnet *ifp, struct mf6c *rt)
1427{
1428	INIT_VNET_INET6(curvnet);
1429	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1430	mifi_t mifi, iif;
1431	struct mif6 *mifp;
1432	int plen = m->m_pkthdr.len;
1433	struct in6_addr src0, dst0; /* copies for local work */
1434	u_int32_t iszone, idzone, oszone, odzone;
1435	int error = 0;
1436
1437/*
1438 * Macro to send packet on mif.  Since RSVP packets don't get counted on
1439 * input, they shouldn't get counted on output, so statistics keeping is
1440 * separate.
1441 */
1442
1443#define MC6_SEND(ip6, mifp, m) do {				\
1444	if ((mifp)->m6_flags & MIFF_REGISTER)			\
1445		register_send((ip6), (mifp), (m));		\
1446	else							\
1447		phyint_send((ip6), (mifp), (m));		\
1448} while (/*CONSTCOND*/ 0)
1449
1450	/*
1451	 * Don't forward if it didn't arrive from the parent mif
1452	 * for its origin.
1453	 */
1454	mifi = rt->mf6c_parent;
1455	if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
1456		/* came in the wrong interface */
1457#ifdef MRT6DEBUG
1458		if (V_mrt6debug & DEBUG_FORWARD)
1459			log(LOG_DEBUG,
1460			    "wrong if: ifid %d mifi %d mififid %x\n",
1461			    ifp->if_index, mifi,
1462			    mif6table[mifi].m6_ifp->if_index);
1463#endif
1464		mrt6stat.mrt6s_wrong_if++;
1465		rt->mf6c_wrong_if++;
1466		/*
1467		 * If we are doing PIM processing, and we are forwarding
1468		 * packets on this interface, send a message to the
1469		 * routing daemon.
1470		 */
1471		/* have to make sure this is a valid mif */
1472		if (mifi < nummifs && mif6table[mifi].m6_ifp)
1473			if (V_pim6 && (m->m_flags & M_LOOP) == 0) {
1474				/*
1475				 * Check the M_LOOP flag to avoid an
1476				 * unnecessary PIM assert.
1477				 * XXX: M_LOOP is an ad-hoc hack...
1478				 */
1479				static struct sockaddr_in6 sin6 =
1480				{ sizeof(sin6), AF_INET6 };
1481
1482				struct mbuf *mm;
1483				struct mrt6msg *im;
1484#ifdef MRT6_OINIT
1485				struct omrt6msg *oim;
1486#endif
1487
1488				mm = m_copy(m, 0, sizeof(struct ip6_hdr));
1489				if (mm &&
1490				    (M_HASCL(mm) ||
1491				     mm->m_len < sizeof(struct ip6_hdr)))
1492					mm = m_pullup(mm, sizeof(struct ip6_hdr));
1493				if (mm == NULL)
1494					return (ENOBUFS);
1495
1496#ifdef MRT6_OINIT
1497				oim = NULL;
1498#endif
1499				im = NULL;
1500				switch (V_ip6_mrouter_ver) {
1501#ifdef MRT6_OINIT
1502				case MRT6_OINIT:
1503					oim = mtod(mm, struct omrt6msg *);
1504					oim->im6_msgtype = MRT6MSG_WRONGMIF;
1505					oim->im6_mbz = 0;
1506					break;
1507#endif
1508				case MRT6_INIT:
1509					im = mtod(mm, struct mrt6msg *);
1510					im->im6_msgtype = MRT6MSG_WRONGMIF;
1511					im->im6_mbz = 0;
1512					break;
1513				default:
1514					m_freem(mm);
1515					return (EINVAL);
1516				}
1517
1518				for (mifp = mif6table, iif = 0;
1519				     iif < nummifs && mifp &&
1520					     mifp->m6_ifp != ifp;
1521				     mifp++, iif++)
1522					;
1523
1524				switch (V_ip6_mrouter_ver) {
1525#ifdef MRT6_OINIT
1526				case MRT6_OINIT:
1527					oim->im6_mif = iif;
1528					sin6.sin6_addr = oim->im6_src;
1529					break;
1530#endif
1531				case MRT6_INIT:
1532					im->im6_mif = iif;
1533					sin6.sin6_addr = im->im6_src;
1534					break;
1535				}
1536
1537				mrt6stat.mrt6s_upcalls++;
1538
1539				if (socket_send(V_ip6_mrouter, mm, &sin6) < 0) {
1540#ifdef MRT6DEBUG
1541					if (V_mrt6debug)
1542						log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n");
1543#endif
1544					++mrt6stat.mrt6s_upq_sockfull;
1545					return (ENOBUFS);
1546				}	/* if socket Q full */
1547			}		/* if PIM */
1548		return (0);
1549	}			/* if wrong iif */
1550
1551	/* If I sourced this packet, it counts as output, else it was input. */
1552	if (m->m_pkthdr.rcvif == NULL) {
1553		/* XXX: is rcvif really NULL when output?? */
1554		mif6table[mifi].m6_pkt_out++;
1555		mif6table[mifi].m6_bytes_out += plen;
1556	} else {
1557		mif6table[mifi].m6_pkt_in++;
1558		mif6table[mifi].m6_bytes_in += plen;
1559	}
1560	rt->mf6c_pkt_cnt++;
1561	rt->mf6c_byte_cnt += plen;
1562
1563	/*
1564	 * For each mif, forward a copy of the packet if there are group
1565	 * members downstream on the interface.
1566	 */
1567	src0 = ip6->ip6_src;
1568	dst0 = ip6->ip6_dst;
1569	if ((error = in6_setscope(&src0, ifp, &iszone)) != 0 ||
1570	    (error = in6_setscope(&dst0, ifp, &idzone)) != 0) {
1571		V_ip6stat.ip6s_badscope++;
1572		return (error);
1573	}
1574	for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++) {
1575		if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
1576			/*
1577			 * check if the outgoing packet is going to break
1578			 * a scope boundary.
1579			 * XXX For packets through PIM register tunnel
1580			 * interface, we believe a routing daemon.
1581			 */
1582			if (!(mif6table[rt->mf6c_parent].m6_flags &
1583			      MIFF_REGISTER) &&
1584			    !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
1585				if (in6_setscope(&src0, mif6table[mifi].m6_ifp,
1586				    &oszone) ||
1587				    in6_setscope(&dst0, mif6table[mifi].m6_ifp,
1588				    &odzone) ||
1589				    iszone != oszone ||
1590				    idzone != odzone) {
1591					V_ip6stat.ip6s_badscope++;
1592					continue;
1593				}
1594			}
1595
1596			mifp->m6_pkt_out++;
1597			mifp->m6_bytes_out += plen;
1598			MC6_SEND(ip6, mifp, m);
1599		}
1600	}
1601	return (0);
1602}
1603
1604static void
1605phyint_send(struct ip6_hdr *ip6, struct mif6 *mifp, struct mbuf *m)
1606{
1607	INIT_VNET_INET6(curvnet);
1608	struct mbuf *mb_copy;
1609	struct ifnet *ifp = mifp->m6_ifp;
1610	int error = 0;
1611	struct sockaddr_in6 *dst6;
1612	u_long linkmtu;
1613
1614	dst6 = &mifp->m6_route.ro_dst;
1615
1616	/*
1617	 * Make a new reference to the packet; make sure that
1618	 * the IPv6 header is actually copied, not just referenced,
1619	 * so that ip6_output() only scribbles on the copy.
1620	 */
1621	mb_copy = m_copy(m, 0, M_COPYALL);
1622	if (mb_copy &&
1623	    (M_HASCL(mb_copy) || mb_copy->m_len < sizeof(struct ip6_hdr)))
1624		mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr));
1625	if (mb_copy == NULL) {
1626		return;
1627	}
1628	/* set MCAST flag to the outgoing packet */
1629	mb_copy->m_flags |= M_MCAST;
1630
1631	/*
1632	 * If we sourced the packet, call ip6_output since we may devide
1633	 * the packet into fragments when the packet is too big for the
1634	 * outgoing interface.
1635	 * Otherwise, we can simply send the packet to the interface
1636	 * sending queue.
1637	 */
1638	if (m->m_pkthdr.rcvif == NULL) {
1639		struct ip6_moptions im6o;
1640
1641		im6o.im6o_multicast_ifp = ifp;
1642		/* XXX: ip6_output will override ip6->ip6_hlim */
1643		im6o.im6o_multicast_hlim = ip6->ip6_hlim;
1644		im6o.im6o_multicast_loop = 1;
1645		error = ip6_output(mb_copy, NULL, &mifp->m6_route,
1646				   IPV6_FORWARDING, &im6o, NULL, NULL);
1647
1648#ifdef MRT6DEBUG
1649		if (V_mrt6debug & DEBUG_XMIT)
1650			log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1651			    mifp - mif6table, error);
1652#endif
1653		return;
1654	}
1655
1656	/*
1657	 * If configured to loop back multicasts by default,
1658	 * loop back a copy now.
1659	 */
1660	if (in6_mcast_loop) {
1661		dst6->sin6_len = sizeof(struct sockaddr_in6);
1662		dst6->sin6_family = AF_INET6;
1663		dst6->sin6_addr = ip6->ip6_dst;
1664		ip6_mloopback(ifp, m, &mifp->m6_route.ro_dst);
1665	}
1666
1667	/*
1668	 * Put the packet into the sending queue of the outgoing interface
1669	 * if it would fit in the MTU of the interface.
1670	 */
1671	linkmtu = IN6_LINKMTU(ifp);
1672	if (mb_copy->m_pkthdr.len <= linkmtu || linkmtu < IPV6_MMTU) {
1673		dst6->sin6_len = sizeof(struct sockaddr_in6);
1674		dst6->sin6_family = AF_INET6;
1675		dst6->sin6_addr = ip6->ip6_dst;
1676		/*
1677		 * We just call if_output instead of nd6_output here, since
1678		 * we need no ND for a multicast forwarded packet...right?
1679		 */
1680		error = (*ifp->if_output)(ifp, mb_copy,
1681		    (struct sockaddr *)&mifp->m6_route.ro_dst, NULL);
1682#ifdef MRT6DEBUG
1683		if (V_mrt6debug & DEBUG_XMIT)
1684			log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
1685			    mifp - mif6table, error);
1686#endif
1687	} else {
1688		/*
1689		 * pMTU discovery is intentionally disabled by default, since
1690		 * various router may notify pMTU in multicast, which can be
1691		 * a DDoS to a router
1692		 */
1693		if (V_ip6_mcast_pmtu)
1694			icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, linkmtu);
1695		else {
1696#ifdef MRT6DEBUG
1697			if (V_mrt6debug & DEBUG_XMIT) {
1698				char ip6bufs[INET6_ADDRSTRLEN];
1699				char ip6bufd[INET6_ADDRSTRLEN];
1700				log(LOG_DEBUG,
1701				    "phyint_send: packet too big on %s o %s "
1702				    "g %s size %d(discarded)\n",
1703				    if_name(ifp),
1704				    ip6_sprintf(ip6bufs, &ip6->ip6_src),
1705				    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
1706				    mb_copy->m_pkthdr.len);
1707			}
1708#endif /* MRT6DEBUG */
1709			m_freem(mb_copy); /* simply discard the packet */
1710		}
1711	}
1712}
1713
1714static int
1715register_send(struct ip6_hdr *ip6, struct mif6 *mif, struct mbuf *m)
1716{
1717	INIT_VNET_INET6(curvnet);
1718	struct mbuf *mm;
1719	int i, len = m->m_pkthdr.len;
1720	static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
1721	struct mrt6msg *im6;
1722
1723#ifdef MRT6DEBUG
1724	if (V_mrt6debug) {
1725		char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1726		log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n",
1727		    ip6_sprintf(ip6bufs, &ip6->ip6_src),
1728		    ip6_sprintf(ip6bufd, &ip6->ip6_dst));
1729	}
1730#endif
1731	++pim6stat.pim6s_snd_registers;
1732
1733	/* Make a copy of the packet to send to the user level process */
1734	MGETHDR(mm, M_DONTWAIT, MT_HEADER);
1735	if (mm == NULL)
1736		return (ENOBUFS);
1737	mm->m_pkthdr.rcvif = NULL;
1738	mm->m_data += max_linkhdr;
1739	mm->m_len = sizeof(struct ip6_hdr);
1740
1741	if ((mm->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1742		m_freem(mm);
1743		return (ENOBUFS);
1744	}
1745	i = MHLEN - M_LEADINGSPACE(mm);
1746	if (i > len)
1747		i = len;
1748	mm = m_pullup(mm, i);
1749	if (mm == NULL)
1750		return (ENOBUFS);
1751/* TODO: check it! */
1752	mm->m_pkthdr.len = len + sizeof(struct ip6_hdr);
1753
1754	/*
1755	 * Send message to routing daemon
1756	 */
1757	sin6.sin6_addr = ip6->ip6_src;
1758
1759	im6 = mtod(mm, struct mrt6msg *);
1760	im6->im6_msgtype      = MRT6MSG_WHOLEPKT;
1761	im6->im6_mbz          = 0;
1762
1763	im6->im6_mif = mif - mif6table;
1764
1765	/* iif info is not given for reg. encap.n */
1766	mrt6stat.mrt6s_upcalls++;
1767
1768	if (socket_send(V_ip6_mrouter, mm, &sin6) < 0) {
1769#ifdef MRT6DEBUG
1770		if (V_mrt6debug)
1771			log(LOG_WARNING,
1772			    "register_send: ip6_mrouter socket queue full\n");
1773#endif
1774		++mrt6stat.mrt6s_upq_sockfull;
1775		return (ENOBUFS);
1776	}
1777	return (0);
1778}
1779
1780/*
1781 * pim6_encapcheck() is called by the encap6_input() path at runtime to
1782 * determine if a packet is for PIM; allowing PIM to be dynamically loaded
1783 * into the kernel.
1784 */
1785static int
1786pim6_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
1787{
1788
1789#ifdef DIAGNOSTIC
1790    KASSERT(proto == IPPROTO_PIM, ("not for IPPROTO_PIM"));
1791#endif
1792    if (proto != IPPROTO_PIM)
1793	return 0;	/* not for us; reject the datagram. */
1794
1795    return 64;		/* claim the datagram. */
1796}
1797
1798/*
1799 * PIM sparse mode hook
1800 * Receives the pim control messages, and passes them up to the listening
1801 * socket, using rip6_input.
1802 * The only message processed is the REGISTER pim message; the pim header
1803 * is stripped off, and the inner packet is passed to register_mforward.
1804 */
1805int
1806pim6_input(struct mbuf **mp, int *offp, int proto)
1807{
1808	INIT_VNET_INET6(curvnet);
1809	struct pim *pim; /* pointer to a pim struct */
1810	struct ip6_hdr *ip6;
1811	int pimlen;
1812	struct mbuf *m = *mp;
1813	int minlen;
1814	int off = *offp;
1815
1816	++pim6stat.pim6s_rcv_total;
1817
1818	ip6 = mtod(m, struct ip6_hdr *);
1819	pimlen = m->m_pkthdr.len - *offp;
1820
1821	/*
1822	 * Validate lengths
1823	 */
1824	if (pimlen < PIM_MINLEN) {
1825		++pim6stat.pim6s_rcv_tooshort;
1826#ifdef MRT6DEBUG
1827		if (V_mrt6debug & DEBUG_PIM)
1828			log(LOG_DEBUG,"pim6_input: PIM packet too short\n");
1829#endif
1830		m_freem(m);
1831		return (IPPROTO_DONE);
1832	}
1833
1834	/*
1835	 * if the packet is at least as big as a REGISTER, go ahead
1836	 * and grab the PIM REGISTER header size, to avoid another
1837	 * possible m_pullup() later.
1838	 *
1839	 * PIM_MINLEN       == pimhdr + u_int32 == 8
1840	 * PIM6_REG_MINLEN   == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
1841	 */
1842	minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN;
1843
1844	/*
1845	 * Make sure that the IP6 and PIM headers in contiguous memory, and
1846	 * possibly the PIM REGISTER header
1847	 */
1848#ifndef PULLDOWN_TEST
1849	IP6_EXTHDR_CHECK(m, off, minlen, IPPROTO_DONE);
1850	/* adjust pointer */
1851	ip6 = mtod(m, struct ip6_hdr *);
1852
1853	/* adjust mbuf to point to the PIM header */
1854	pim = (struct pim *)((caddr_t)ip6 + off);
1855#else
1856	IP6_EXTHDR_GET(pim, struct pim *, m, off, minlen);
1857	if (pim == NULL) {
1858		pim6stat.pim6s_rcv_tooshort++;
1859		return (IPPROTO_DONE);
1860	}
1861#endif
1862
1863#define PIM6_CHECKSUM
1864#ifdef PIM6_CHECKSUM
1865	{
1866		int cksumlen;
1867
1868		/*
1869		 * Validate checksum.
1870		 * If PIM REGISTER, exclude the data packet
1871		 */
1872		if (pim->pim_type == PIM_REGISTER)
1873			cksumlen = PIM_MINLEN;
1874		else
1875			cksumlen = pimlen;
1876
1877		if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) {
1878			++pim6stat.pim6s_rcv_badsum;
1879#ifdef MRT6DEBUG
1880			if (V_mrt6debug & DEBUG_PIM)
1881				log(LOG_DEBUG,
1882				    "pim6_input: invalid checksum\n");
1883#endif
1884			m_freem(m);
1885			return (IPPROTO_DONE);
1886		}
1887	}
1888#endif /* PIM_CHECKSUM */
1889
1890	/* PIM version check */
1891	if (pim->pim_ver != PIM_VERSION) {
1892		++pim6stat.pim6s_rcv_badversion;
1893#ifdef MRT6DEBUG
1894		log(LOG_ERR,
1895		    "pim6_input: incorrect version %d, expecting %d\n",
1896		    pim->pim_ver, PIM_VERSION);
1897#endif
1898		m_freem(m);
1899		return (IPPROTO_DONE);
1900	}
1901
1902	if (pim->pim_type == PIM_REGISTER) {
1903		/*
1904		 * since this is a REGISTER, we'll make a copy of the register
1905		 * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
1906		 * routing daemon.
1907		 */
1908		static struct sockaddr_in6 dst = { sizeof(dst), AF_INET6 };
1909
1910		struct mbuf *mcp;
1911		struct ip6_hdr *eip6;
1912		u_int32_t *reghdr;
1913		int rc;
1914#ifdef MRT6DEBUG
1915		char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1916#endif
1917
1918		++pim6stat.pim6s_rcv_registers;
1919
1920		if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) {
1921#ifdef MRT6DEBUG
1922			if (V_mrt6debug & DEBUG_PIM)
1923				log(LOG_DEBUG,
1924				    "pim6_input: register mif not set: %d\n",
1925				    reg_mif_num);
1926#endif
1927			m_freem(m);
1928			return (IPPROTO_DONE);
1929		}
1930
1931		reghdr = (u_int32_t *)(pim + 1);
1932
1933		if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
1934			goto pim6_input_to_daemon;
1935
1936		/*
1937		 * Validate length
1938		 */
1939		if (pimlen < PIM6_REG_MINLEN) {
1940			++pim6stat.pim6s_rcv_tooshort;
1941			++pim6stat.pim6s_rcv_badregisters;
1942#ifdef MRT6DEBUG
1943			log(LOG_ERR,
1944			    "pim6_input: register packet size too "
1945			    "small %d from %s\n",
1946			    pimlen, ip6_sprintf(ip6bufs, &ip6->ip6_src));
1947#endif
1948			m_freem(m);
1949			return (IPPROTO_DONE);
1950		}
1951
1952		eip6 = (struct ip6_hdr *) (reghdr + 1);
1953#ifdef MRT6DEBUG
1954		if (V_mrt6debug & DEBUG_PIM)
1955			log(LOG_DEBUG,
1956			    "pim6_input[register], eip6: %s -> %s, "
1957			    "eip6 plen %d\n",
1958			    ip6_sprintf(ip6bufs, &eip6->ip6_src),
1959			    ip6_sprintf(ip6bufd, &eip6->ip6_dst),
1960			    ntohs(eip6->ip6_plen));
1961#endif
1962
1963		/* verify the version number of the inner packet */
1964		if ((eip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1965			++pim6stat.pim6s_rcv_badregisters;
1966#ifdef MRT6DEBUG
1967			log(LOG_DEBUG, "pim6_input: invalid IP version (%d) "
1968			    "of the inner packet\n",
1969			    (eip6->ip6_vfc & IPV6_VERSION));
1970#endif
1971			m_freem(m);
1972			return (IPPROTO_NONE);
1973		}
1974
1975		/* verify the inner packet is destined to a mcast group */
1976		if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) {
1977			++pim6stat.pim6s_rcv_badregisters;
1978#ifdef MRT6DEBUG
1979			if (V_mrt6debug & DEBUG_PIM)
1980				log(LOG_DEBUG,
1981				    "pim6_input: inner packet of register "
1982				    "is not multicast %s\n",
1983				    ip6_sprintf(ip6bufd, &eip6->ip6_dst));
1984#endif
1985			m_freem(m);
1986			return (IPPROTO_DONE);
1987		}
1988
1989		/*
1990		 * make a copy of the whole header to pass to the daemon later.
1991		 */
1992		mcp = m_copy(m, 0, off + PIM6_REG_MINLEN);
1993		if (mcp == NULL) {
1994#ifdef MRT6DEBUG
1995			log(LOG_ERR,
1996			    "pim6_input: pim register: "
1997			    "could not copy register head\n");
1998#endif
1999			m_freem(m);
2000			return (IPPROTO_DONE);
2001		}
2002
2003		/*
2004		 * forward the inner ip6 packet; point m_data at the inner ip6.
2005		 */
2006		m_adj(m, off + PIM_MINLEN);
2007#ifdef MRT6DEBUG
2008		if (V_mrt6debug & DEBUG_PIM) {
2009			log(LOG_DEBUG,
2010			    "pim6_input: forwarding decapsulated register: "
2011			    "src %s, dst %s, mif %d\n",
2012			    ip6_sprintf(ip6bufs, &eip6->ip6_src),
2013			    ip6_sprintf(ip6bufd, &eip6->ip6_dst),
2014			    reg_mif_num);
2015		}
2016#endif
2017
2018		rc = if_simloop(mif6table[reg_mif_num].m6_ifp, m,
2019				dst.sin6_family, 0);
2020
2021		/* prepare the register head to send to the mrouting daemon */
2022		m = mcp;
2023	}
2024
2025	/*
2026	 * Pass the PIM message up to the daemon; if it is a register message
2027	 * pass the 'head' only up to the daemon. This includes the
2028	 * encapsulator ip6 header, pim header, register header and the
2029	 * encapsulated ip6 header.
2030	 */
2031  pim6_input_to_daemon:
2032	rip6_input(&m, offp, proto);
2033	return (IPPROTO_DONE);
2034}
2035
2036static int
2037ip6_mroute_modevent(module_t mod, int type, void *unused)
2038{
2039	INIT_VNET_INET6(curvnet);
2040
2041	switch (type) {
2042	case MOD_LOAD:
2043		MROUTER6_LOCK_INIT();
2044		MFC6_LOCK_INIT();
2045		MIF6_LOCK_INIT();
2046
2047		pim6_encap_cookie = encap_attach_func(AF_INET6, IPPROTO_PIM,
2048			pim6_encapcheck,
2049			(const struct protosw *)&in6_pim_protosw, NULL);
2050		if (pim6_encap_cookie == NULL) {
2051			printf("ip6_mroute: unable to attach pim6 encap\n");
2052			MIF6_LOCK_DESTROY();
2053			MFC6_LOCK_DESTROY();
2054			MROUTER6_LOCK_DESTROY();
2055			return (EINVAL);
2056		}
2057
2058		ip6_mforward = X_ip6_mforward;
2059		ip6_mrouter_done = X_ip6_mrouter_done;
2060		ip6_mrouter_get = X_ip6_mrouter_get;
2061		ip6_mrouter_set = X_ip6_mrouter_set;
2062		mrt6_ioctl = X_mrt6_ioctl;
2063		break;
2064
2065	case MOD_UNLOAD:
2066		if (V_ip6_mrouter != NULL)
2067			return EINVAL;
2068
2069		if (pim6_encap_cookie) {
2070			encap_detach(pim6_encap_cookie);
2071			pim6_encap_cookie = NULL;
2072		}
2073		X_ip6_mrouter_done();
2074		ip6_mforward = NULL;
2075		ip6_mrouter_done = NULL;
2076		ip6_mrouter_get = NULL;
2077		ip6_mrouter_set = NULL;
2078		mrt6_ioctl = NULL;
2079
2080		MIF6_LOCK_DESTROY();
2081		MFC6_LOCK_DESTROY();
2082		MROUTER6_LOCK_DESTROY();
2083		break;
2084
2085	default:
2086		return (EOPNOTSUPP);
2087	}
2088
2089	return (0);
2090}
2091
2092static moduledata_t ip6_mroutemod = {
2093	"ip6_mroute",
2094	ip6_mroute_modevent,
2095	0
2096};
2097
2098DECLARE_MODULE(ip6_mroute, ip6_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);
2099