xref: /freebsd/sys/netinet6/in6_mcast.c (revision 2ef5d803)
1fe267a55SPedro F. Giffuni /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3fe267a55SPedro F. Giffuni  *
48f002c6cSBruce M Simpson  * Copyright (c) 2009 Bruce Simpson.
58f002c6cSBruce M Simpson  * All rights reserved.
68f002c6cSBruce M Simpson  *
78f002c6cSBruce M Simpson  * Redistribution and use in source and binary forms, with or without
88f002c6cSBruce M Simpson  * modification, are permitted provided that the following conditions
98f002c6cSBruce M Simpson  * are met:
108f002c6cSBruce M Simpson  * 1. Redistributions of source code must retain the above copyright
118f002c6cSBruce M Simpson  *    notice, this list of conditions and the following disclaimer.
128f002c6cSBruce M Simpson  * 2. Redistributions in binary form must reproduce the above copyright
138f002c6cSBruce M Simpson  *    notice, this list of conditions and the following disclaimer in the
148f002c6cSBruce M Simpson  *    documentation and/or other materials provided with the distribution.
158f002c6cSBruce M Simpson  * 3. The name of the author may not be used to endorse or promote
168f002c6cSBruce M Simpson  *    products derived from this software without specific prior written
178f002c6cSBruce M Simpson  *    permission.
188f002c6cSBruce M Simpson  *
198f002c6cSBruce M Simpson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
208f002c6cSBruce M Simpson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218f002c6cSBruce M Simpson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228f002c6cSBruce M Simpson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
238f002c6cSBruce M Simpson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248f002c6cSBruce M Simpson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258f002c6cSBruce M Simpson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268f002c6cSBruce M Simpson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278f002c6cSBruce M Simpson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288f002c6cSBruce M Simpson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298f002c6cSBruce M Simpson  * SUCH DAMAGE.
308f002c6cSBruce M Simpson  */
318f002c6cSBruce M Simpson 
328f002c6cSBruce M Simpson /*
338f002c6cSBruce M Simpson  * IPv6 multicast socket, group, and socket option processing module.
3433cde130SBruce M Simpson  * Normative references: RFC 2292, RFC 3492, RFC 3542, RFC 3678, RFC 3810.
358f002c6cSBruce M Simpson  */
368f002c6cSBruce M Simpson 
378f002c6cSBruce M Simpson #include <sys/cdefs.h>
388f002c6cSBruce M Simpson __FBSDID("$FreeBSD$");
398f002c6cSBruce M Simpson 
408f002c6cSBruce M Simpson #include "opt_inet6.h"
418f002c6cSBruce M Simpson 
428f002c6cSBruce M Simpson #include <sys/param.h>
438f002c6cSBruce M Simpson #include <sys/systm.h>
448f002c6cSBruce M Simpson #include <sys/kernel.h>
459b1d850bSMark Johnston #include <sys/ktr.h>
468f002c6cSBruce M Simpson #include <sys/malloc.h>
478f002c6cSBruce M Simpson #include <sys/mbuf.h>
488f002c6cSBruce M Simpson #include <sys/protosw.h>
498f002c6cSBruce M Simpson #include <sys/socket.h>
508f002c6cSBruce M Simpson #include <sys/socketvar.h>
518f002c6cSBruce M Simpson #include <sys/sysctl.h>
528f002c6cSBruce M Simpson #include <sys/priv.h>
539b1d850bSMark Johnston #include <sys/taskqueue.h>
548f002c6cSBruce M Simpson #include <sys/tree.h>
558f002c6cSBruce M Simpson 
568f002c6cSBruce M Simpson #include <net/if.h>
5776039bc8SGleb Smirnoff #include <net/if_var.h>
588f002c6cSBruce M Simpson #include <net/if_dl.h>
598f002c6cSBruce M Simpson #include <net/route.h>
606ad7446cSAlexander V. Chernikov #include <net/route/nhop.h>
618f002c6cSBruce M Simpson #include <net/vnet.h>
628f002c6cSBruce M Simpson 
638f002c6cSBruce M Simpson #include <netinet/in.h>
64f3e1324bSStephen Hurd #include <netinet/udp.h>
658f002c6cSBruce M Simpson #include <netinet/in_var.h>
66f3e1324bSStephen Hurd #include <netinet/ip_var.h>
67f3e1324bSStephen Hurd #include <netinet/udp_var.h>
689977be4aSAlexander V. Chernikov #include <netinet6/in6_fib.h>
698f002c6cSBruce M Simpson #include <netinet6/in6_var.h>
708f002c6cSBruce M Simpson #include <netinet/ip6.h>
718f002c6cSBruce M Simpson #include <netinet/icmp6.h>
728f002c6cSBruce M Simpson #include <netinet6/ip6_var.h>
738f002c6cSBruce M Simpson #include <netinet/in_pcb.h>
748f002c6cSBruce M Simpson #include <netinet/tcp_var.h>
758f002c6cSBruce M Simpson #include <netinet6/nd6.h>
768f002c6cSBruce M Simpson #include <netinet6/mld6_var.h>
77eddfbb76SRobert Watson #include <netinet6/scope6_var.h>
788f002c6cSBruce M Simpson 
798f002c6cSBruce M Simpson #ifndef KTR_MLD
808f002c6cSBruce M Simpson #define KTR_MLD KTR_INET6
818f002c6cSBruce M Simpson #endif
828f002c6cSBruce M Simpson 
838f002c6cSBruce M Simpson #ifndef __SOCKUNION_DECLARED
848f002c6cSBruce M Simpson union sockunion {
858f002c6cSBruce M Simpson 	struct sockaddr_storage	ss;
868f002c6cSBruce M Simpson 	struct sockaddr		sa;
878f002c6cSBruce M Simpson 	struct sockaddr_dl	sdl;
888f002c6cSBruce M Simpson 	struct sockaddr_in6	sin6;
898f002c6cSBruce M Simpson };
908f002c6cSBruce M Simpson typedef union sockunion sockunion_t;
918f002c6cSBruce M Simpson #define __SOCKUNION_DECLARED
928f002c6cSBruce M Simpson #endif /* __SOCKUNION_DECLARED */
938f002c6cSBruce M Simpson 
948f002c6cSBruce M Simpson static MALLOC_DEFINE(M_IN6MFILTER, "in6_mfilter",
958f002c6cSBruce M Simpson     "IPv6 multicast PCB-layer source filter");
96f3e1324bSStephen Hurd MALLOC_DEFINE(M_IP6MADDR, "in6_multi", "IPv6 multicast group");
978f002c6cSBruce M Simpson static MALLOC_DEFINE(M_IP6MOPTS, "ip6_moptions", "IPv6 multicast options");
988f002c6cSBruce M Simpson static MALLOC_DEFINE(M_IP6MSOURCE, "ip6_msource",
998f002c6cSBruce M Simpson     "IPv6 multicast MLD-layer source filter");
1008f002c6cSBruce M Simpson 
1018f002c6cSBruce M Simpson RB_GENERATE(ip6_msource_tree, ip6_msource, im6s_link, ip6_msource_cmp);
1028f002c6cSBruce M Simpson 
1038f002c6cSBruce M Simpson /*
1048f002c6cSBruce M Simpson  * Locking:
10559854ecfSHans Petter Selasky  * - Lock order is: Giant, IN6_MULTI_LOCK, INP_WLOCK,
10659854ecfSHans Petter Selasky  *   IN6_MULTI_LIST_LOCK, MLD_LOCK, IF_ADDR_LOCK.
1078f002c6cSBruce M Simpson  * - The IF_ADDR_LOCK is implicitly taken by in6m_lookup() earlier, however
1088f002c6cSBruce M Simpson  *   it can be taken by code in net/if.c also.
1098f002c6cSBruce M Simpson  * - ip6_moptions and in6_mfilter are covered by the INP_WLOCK.
1108f002c6cSBruce M Simpson  *
1118f002c6cSBruce M Simpson  * struct in6_multi is covered by IN6_MULTI_LOCK. There isn't strictly
1128f002c6cSBruce M Simpson  * any need for in6_multi itself to be virtualized -- it is bound to an ifp
1138f002c6cSBruce M Simpson  * anyway no matter what happens.
1148f002c6cSBruce M Simpson  */
115f3e1324bSStephen Hurd struct mtx in6_multi_list_mtx;
116f3e1324bSStephen Hurd MTX_SYSINIT(in6_multi_mtx, &in6_multi_list_mtx, "in6_multi_list_mtx", MTX_DEF);
117f3e1324bSStephen Hurd 
118f3e1324bSStephen Hurd struct mtx in6_multi_free_mtx;
119f3e1324bSStephen Hurd MTX_SYSINIT(in6_multi_free_mtx, &in6_multi_free_mtx, "in6_multi_free_mtx", MTX_DEF);
120f3e1324bSStephen Hurd 
121f3e1324bSStephen Hurd struct sx in6_multi_sx;
122f3e1324bSStephen Hurd SX_SYSINIT(in6_multi_sx, &in6_multi_sx, "in6_multi_sx");
123f3e1324bSStephen Hurd 
1248f002c6cSBruce M Simpson static void	im6f_commit(struct in6_mfilter *);
1258f002c6cSBruce M Simpson static int	im6f_get_source(struct in6_mfilter *imf,
1268f002c6cSBruce M Simpson 		    const struct sockaddr_in6 *psin,
1278f002c6cSBruce M Simpson 		    struct in6_msource **);
1288f002c6cSBruce M Simpson static struct in6_msource *
1298f002c6cSBruce M Simpson 		im6f_graft(struct in6_mfilter *, const uint8_t,
1308f002c6cSBruce M Simpson 		    const struct sockaddr_in6 *);
1318f002c6cSBruce M Simpson static void	im6f_leave(struct in6_mfilter *);
1328f002c6cSBruce M Simpson static int	im6f_prune(struct in6_mfilter *, const struct sockaddr_in6 *);
1338f002c6cSBruce M Simpson static void	im6f_purge(struct in6_mfilter *);
1348f002c6cSBruce M Simpson static void	im6f_rollback(struct in6_mfilter *);
1358f002c6cSBruce M Simpson static void	im6f_reap(struct in6_mfilter *);
13659854ecfSHans Petter Selasky static struct in6_mfilter *
13759854ecfSHans Petter Selasky 		im6o_match_group(const struct ip6_moptions *,
1388f002c6cSBruce M Simpson 		    const struct ifnet *, const struct sockaddr *);
1398f002c6cSBruce M Simpson static struct in6_msource *
14059854ecfSHans Petter Selasky 		im6o_match_source(struct in6_mfilter *, const struct sockaddr *);
1418f002c6cSBruce M Simpson static void	im6s_merge(struct ip6_msource *ims,
1428f002c6cSBruce M Simpson 		    const struct in6_msource *lims, const int rollback);
143f3e1324bSStephen Hurd static int	in6_getmulti(struct ifnet *, const struct in6_addr *,
1448f002c6cSBruce M Simpson 		    struct in6_multi **);
145e1891232SBjoern A. Zeeb static int	in6_joingroup_locked(struct ifnet *, const struct in6_addr *,
146e1891232SBjoern A. Zeeb 		    struct in6_mfilter *, struct in6_multi **, int);
1478f002c6cSBruce M Simpson static int	in6m_get_source(struct in6_multi *inm,
1488f002c6cSBruce M Simpson 		    const struct in6_addr *addr, const int noalloc,
1498f002c6cSBruce M Simpson 		    struct ip6_msource **pims);
1506c5a340eSDimitry Andric #ifdef KTR
1518f002c6cSBruce M Simpson static int	in6m_is_ifp_detached(const struct in6_multi *);
1526c5a340eSDimitry Andric #endif
1538f002c6cSBruce M Simpson static int	in6m_merge(struct in6_multi *, /*const*/ struct in6_mfilter *);
1548f002c6cSBruce M Simpson static void	in6m_purge(struct in6_multi *);
1558f002c6cSBruce M Simpson static void	in6m_reap(struct in6_multi *);
1568f002c6cSBruce M Simpson static struct ip6_moptions *
1578f002c6cSBruce M Simpson 		in6p_findmoptions(struct inpcb *);
1588f002c6cSBruce M Simpson static int	in6p_get_source_filters(struct inpcb *, struct sockopt *);
1598f002c6cSBruce M Simpson static int	in6p_join_group(struct inpcb *, struct sockopt *);
1608f002c6cSBruce M Simpson static int	in6p_leave_group(struct inpcb *, struct sockopt *);
16133cde130SBruce M Simpson static struct ifnet *
16233cde130SBruce M Simpson 		in6p_lookup_mcast_ifp(const struct inpcb *,
16333cde130SBruce M Simpson 		    const struct sockaddr_in6 *);
1648f002c6cSBruce M Simpson static int	in6p_block_unblock_source(struct inpcb *, struct sockopt *);
1658f002c6cSBruce M Simpson static int	in6p_set_multicast_if(struct inpcb *, struct sockopt *);
1668f002c6cSBruce M Simpson static int	in6p_set_source_filters(struct inpcb *, struct sockopt *);
1678f002c6cSBruce M Simpson static int	sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS);
1688f002c6cSBruce M Simpson 
1698f002c6cSBruce M Simpson SYSCTL_DECL(_net_inet6_ip6);	/* XXX Not in any common header. */
1708f002c6cSBruce M Simpson 
1717029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, mcast,
1727029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1736472ac3dSEd Schouten     "IPv6 multicast");
1748f002c6cSBruce M Simpson 
1758f002c6cSBruce M Simpson static u_long in6_mcast_maxgrpsrc = IPV6_MAX_GROUP_SRC_FILTER;
1768f002c6cSBruce M Simpson SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxgrpsrc,
177af3b2549SHans Petter Selasky     CTLFLAG_RWTUN, &in6_mcast_maxgrpsrc, 0,
1788f002c6cSBruce M Simpson     "Max source filters per group");
1798f002c6cSBruce M Simpson 
1808f002c6cSBruce M Simpson static u_long in6_mcast_maxsocksrc = IPV6_MAX_SOCK_SRC_FILTER;
1818f002c6cSBruce M Simpson SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxsocksrc,
182af3b2549SHans Petter Selasky     CTLFLAG_RWTUN, &in6_mcast_maxsocksrc, 0,
1838f002c6cSBruce M Simpson     "Max source filters per socket");
1848f002c6cSBruce M Simpson 
1858f002c6cSBruce M Simpson /* TODO Virtualize this switch. */
1868f002c6cSBruce M Simpson int in6_mcast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
187af3b2549SHans Petter Selasky SYSCTL_INT(_net_inet6_ip6_mcast, OID_AUTO, loop, CTLFLAG_RWTUN,
1888f002c6cSBruce M Simpson     &in6_mcast_loop, 0, "Loopback multicast datagrams by default");
1898f002c6cSBruce M Simpson 
1906472ac3dSEd Schouten static SYSCTL_NODE(_net_inet6_ip6_mcast, OID_AUTO, filters,
1918f002c6cSBruce M Simpson     CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip6_mcast_filters,
1928f002c6cSBruce M Simpson     "Per-interface stack-wide source filters");
1938f002c6cSBruce M Simpson 
1946c5a340eSDimitry Andric #ifdef KTR
1958f002c6cSBruce M Simpson /*
1968f002c6cSBruce M Simpson  * Inline function which wraps assertions for a valid ifp.
1978f002c6cSBruce M Simpson  * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
1988f002c6cSBruce M Simpson  * is detached.
1998f002c6cSBruce M Simpson  */
2008f002c6cSBruce M Simpson static int __inline
2018f002c6cSBruce M Simpson in6m_is_ifp_detached(const struct in6_multi *inm)
2028f002c6cSBruce M Simpson {
2038f002c6cSBruce M Simpson 	struct ifnet *ifp;
2048f002c6cSBruce M Simpson 
2058f002c6cSBruce M Simpson 	KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__));
2068f002c6cSBruce M Simpson 	ifp = inm->in6m_ifma->ifma_ifp;
2078f002c6cSBruce M Simpson 	if (ifp != NULL) {
2088f002c6cSBruce M Simpson 		/*
2098f002c6cSBruce M Simpson 		 * Sanity check that network-layer notion of ifp is the
2108f002c6cSBruce M Simpson 		 * same as that of link-layer.
2118f002c6cSBruce M Simpson 		 */
2128f002c6cSBruce M Simpson 		KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__));
2138f002c6cSBruce M Simpson 	}
2148f002c6cSBruce M Simpson 
2158f002c6cSBruce M Simpson 	return (ifp == NULL);
2168f002c6cSBruce M Simpson }
2176c5a340eSDimitry Andric #endif
2188f002c6cSBruce M Simpson 
2198f002c6cSBruce M Simpson /*
2208f002c6cSBruce M Simpson  * Initialize an in6_mfilter structure to a known state at t0, t1
2218f002c6cSBruce M Simpson  * with an empty source filter list.
2228f002c6cSBruce M Simpson  */
2238f002c6cSBruce M Simpson static __inline void
2248f002c6cSBruce M Simpson im6f_init(struct in6_mfilter *imf, const int st0, const int st1)
2258f002c6cSBruce M Simpson {
2268f002c6cSBruce M Simpson 	memset(imf, 0, sizeof(struct in6_mfilter));
2278f002c6cSBruce M Simpson 	RB_INIT(&imf->im6f_sources);
2288f002c6cSBruce M Simpson 	imf->im6f_st[0] = st0;
2298f002c6cSBruce M Simpson 	imf->im6f_st[1] = st1;
2308f002c6cSBruce M Simpson }
2318f002c6cSBruce M Simpson 
23259854ecfSHans Petter Selasky struct in6_mfilter *
23359854ecfSHans Petter Selasky ip6_mfilter_alloc(const int mflags, const int st0, const int st1)
2348f002c6cSBruce M Simpson {
23559854ecfSHans Petter Selasky 	struct in6_mfilter *imf;
2368f002c6cSBruce M Simpson 
23759854ecfSHans Petter Selasky 	imf = malloc(sizeof(*imf), M_IN6MFILTER, mflags);
2388f002c6cSBruce M Simpson 
23959854ecfSHans Petter Selasky 	if (imf != NULL)
24059854ecfSHans Petter Selasky 		im6f_init(imf, st0, st1);
24159854ecfSHans Petter Selasky 
24259854ecfSHans Petter Selasky 	return (imf);
2438f002c6cSBruce M Simpson }
2448f002c6cSBruce M Simpson 
24559854ecfSHans Petter Selasky void
24659854ecfSHans Petter Selasky ip6_mfilter_free(struct in6_mfilter *imf)
24759854ecfSHans Petter Selasky {
2488f002c6cSBruce M Simpson 
24959854ecfSHans Petter Selasky 	im6f_purge(imf);
25059854ecfSHans Petter Selasky 	free(imf, M_IN6MFILTER);
2518f002c6cSBruce M Simpson }
2528f002c6cSBruce M Simpson 
2538f002c6cSBruce M Simpson /*
2548f002c6cSBruce M Simpson  * Find an IPv6 multicast group entry for this ip6_moptions instance
2558f002c6cSBruce M Simpson  * which matches the specified group, and optionally an interface.
2568f002c6cSBruce M Simpson  * Return its index into the array, or -1 if not found.
2578f002c6cSBruce M Simpson  */
25859854ecfSHans Petter Selasky static struct in6_mfilter *
2598f002c6cSBruce M Simpson im6o_match_group(const struct ip6_moptions *imo, const struct ifnet *ifp,
2608f002c6cSBruce M Simpson     const struct sockaddr *group)
2618f002c6cSBruce M Simpson {
2628f002c6cSBruce M Simpson 	const struct sockaddr_in6 *gsin6;
26359854ecfSHans Petter Selasky         struct in6_mfilter *imf;
26459854ecfSHans Petter Selasky         struct in6_multi *inm;
2658f002c6cSBruce M Simpson 
2668f002c6cSBruce M Simpson         gsin6 = (const struct sockaddr_in6 *)group;
2678f002c6cSBruce M Simpson 
26859854ecfSHans Petter Selasky 	IP6_MFILTER_FOREACH(imf, &imo->im6o_head) {
26959854ecfSHans Petter Selasky 		inm = imf->im6f_in6m;
27059854ecfSHans Petter Selasky 		if (inm == NULL)
2718f002c6cSBruce M Simpson 			continue;
27259854ecfSHans Petter Selasky 		if ((ifp == NULL || (inm->in6m_ifp == ifp)) &&
27359854ecfSHans Petter Selasky 		    IN6_ARE_ADDR_EQUAL(&inm->in6m_addr,
2748f002c6cSBruce M Simpson 		    &gsin6->sin6_addr)) {
2758f002c6cSBruce M Simpson 			break;
2768f002c6cSBruce M Simpson 		}
2778f002c6cSBruce M Simpson 	}
27859854ecfSHans Petter Selasky 	return (imf);
2798f002c6cSBruce M Simpson }
2808f002c6cSBruce M Simpson 
2818f002c6cSBruce M Simpson /*
2828f002c6cSBruce M Simpson  * Find an IPv6 multicast source entry for this imo which matches
2838f002c6cSBruce M Simpson  * the given group index for this socket, and source address.
2848f002c6cSBruce M Simpson  *
28529dc7bc6SBruce M Simpson  * XXX TODO: The scope ID, if present in src, is stripped before
28629dc7bc6SBruce M Simpson  * any comparison. We SHOULD enforce scope/zone checks where the source
28729dc7bc6SBruce M Simpson  * filter entry has a link scope.
28829dc7bc6SBruce M Simpson  *
2898f002c6cSBruce M Simpson  * NOTE: This does not check if the entry is in-mode, merely if
2908f002c6cSBruce M Simpson  * it exists, which may not be the desired behaviour.
2918f002c6cSBruce M Simpson  */
2928f002c6cSBruce M Simpson static struct in6_msource *
29359854ecfSHans Petter Selasky im6o_match_source(struct in6_mfilter *imf, const struct sockaddr *src)
2948f002c6cSBruce M Simpson {
2958f002c6cSBruce M Simpson 	struct ip6_msource	 find;
2968f002c6cSBruce M Simpson 	struct ip6_msource	*ims;
2978f002c6cSBruce M Simpson 	const sockunion_t	*psa;
2988f002c6cSBruce M Simpson 
2998f002c6cSBruce M Simpson 	KASSERT(src->sa_family == AF_INET6, ("%s: !AF_INET6", __func__));
3008f002c6cSBruce M Simpson 
3018f002c6cSBruce M Simpson 	psa = (const sockunion_t *)src;
3028f002c6cSBruce M Simpson 	find.im6s_addr = psa->sin6.sin6_addr;
30329dc7bc6SBruce M Simpson 	in6_clearscope(&find.im6s_addr);		/* XXX */
3048f002c6cSBruce M Simpson 	ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
3058f002c6cSBruce M Simpson 
3068f002c6cSBruce M Simpson 	return ((struct in6_msource *)ims);
3078f002c6cSBruce M Simpson }
3088f002c6cSBruce M Simpson 
3098f002c6cSBruce M Simpson /*
3108f002c6cSBruce M Simpson  * Perform filtering for multicast datagrams on a socket by group and source.
3118f002c6cSBruce M Simpson  *
3128f002c6cSBruce M Simpson  * Returns 0 if a datagram should be allowed through, or various error codes
3138f002c6cSBruce M Simpson  * if the socket was not a member of the group, or the source was muted, etc.
3148f002c6cSBruce M Simpson  */
3158f002c6cSBruce M Simpson int
3168f002c6cSBruce M Simpson im6o_mc_filter(const struct ip6_moptions *imo, const struct ifnet *ifp,
3178f002c6cSBruce M Simpson     const struct sockaddr *group, const struct sockaddr *src)
3188f002c6cSBruce M Simpson {
31959854ecfSHans Petter Selasky 	struct in6_mfilter *imf;
3208f002c6cSBruce M Simpson 	struct in6_msource *ims;
3218f002c6cSBruce M Simpson 	int mode;
3228f002c6cSBruce M Simpson 
3238f002c6cSBruce M Simpson 	KASSERT(ifp != NULL, ("%s: null ifp", __func__));
3248f002c6cSBruce M Simpson 
32559854ecfSHans Petter Selasky 	imf = im6o_match_group(imo, ifp, group);
32659854ecfSHans Petter Selasky 	if (imf == NULL)
3278f002c6cSBruce M Simpson 		return (MCAST_NOTGMEMBER);
3288f002c6cSBruce M Simpson 
3298f002c6cSBruce M Simpson 	/*
3308f002c6cSBruce M Simpson 	 * Check if the source was included in an (S,G) join.
3318f002c6cSBruce M Simpson 	 * Allow reception on exclusive memberships by default,
3328f002c6cSBruce M Simpson 	 * reject reception on inclusive memberships by default.
3338f002c6cSBruce M Simpson 	 * Exclude source only if an in-mode exclude filter exists.
3348f002c6cSBruce M Simpson 	 * Include source only if an in-mode include filter exists.
3358f002c6cSBruce M Simpson 	 * NOTE: We are comparing group state here at MLD t1 (now)
3368f002c6cSBruce M Simpson 	 * with socket-layer t0 (since last downcall).
3378f002c6cSBruce M Simpson 	 */
33859854ecfSHans Petter Selasky 	mode = imf->im6f_st[1];
33959854ecfSHans Petter Selasky 	ims = im6o_match_source(imf, src);
3408f002c6cSBruce M Simpson 
3418f002c6cSBruce M Simpson 	if ((ims == NULL && mode == MCAST_INCLUDE) ||
3428f002c6cSBruce M Simpson 	    (ims != NULL && ims->im6sl_st[0] != mode))
3438f002c6cSBruce M Simpson 		return (MCAST_NOTSMEMBER);
3448f002c6cSBruce M Simpson 
3458f002c6cSBruce M Simpson 	return (MCAST_PASS);
3468f002c6cSBruce M Simpson }
3478f002c6cSBruce M Simpson 
3488f002c6cSBruce M Simpson /*
3498f002c6cSBruce M Simpson  * Find and return a reference to an in6_multi record for (ifp, group),
3508f002c6cSBruce M Simpson  * and bump its reference count.
3518f002c6cSBruce M Simpson  * If one does not exist, try to allocate it, and update link-layer multicast
3528f002c6cSBruce M Simpson  * filters on ifp to listen for group.
3538f002c6cSBruce M Simpson  * Assumes the IN6_MULTI lock is held across the call.
3548f002c6cSBruce M Simpson  * Return 0 if successful, otherwise return an appropriate error code.
3558f002c6cSBruce M Simpson  */
3568f002c6cSBruce M Simpson static int
357f3e1324bSStephen Hurd in6_getmulti(struct ifnet *ifp, const struct in6_addr *group,
3588f002c6cSBruce M Simpson     struct in6_multi **pinm)
3598f002c6cSBruce M Simpson {
3602cd6ad76SHans Petter Selasky 	struct epoch_tracker	 et;
3618f002c6cSBruce M Simpson 	struct sockaddr_in6	 gsin6;
3628f002c6cSBruce M Simpson 	struct ifmultiaddr	*ifma;
3638f002c6cSBruce M Simpson 	struct in6_multi	*inm;
3648f002c6cSBruce M Simpson 	int			 error;
3658f002c6cSBruce M Simpson 
3668f002c6cSBruce M Simpson 	error = 0;
3678f002c6cSBruce M Simpson 
3688f002c6cSBruce M Simpson 	/*
3698f002c6cSBruce M Simpson 	 * XXX: Accesses to ifma_protospec must be covered by IF_ADDR_LOCK;
3708f002c6cSBruce M Simpson 	 * if_addmulti() takes this mutex itself, so we must drop and
3718f002c6cSBruce M Simpson 	 * re-acquire around the call.
3728f002c6cSBruce M Simpson 	 */
3738f002c6cSBruce M Simpson 	IN6_MULTI_LOCK_ASSERT();
374f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK();
375137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
3762cd6ad76SHans Petter Selasky 	NET_EPOCH_ENTER(et);
3778f002c6cSBruce M Simpson 	inm = in6m_lookup_locked(ifp, group);
3782cd6ad76SHans Petter Selasky 	NET_EPOCH_EXIT(et);
3792cd6ad76SHans Petter Selasky 
3808f002c6cSBruce M Simpson 	if (inm != NULL) {
3818f002c6cSBruce M Simpson 		/*
3828f002c6cSBruce M Simpson 		 * If we already joined this group, just bump the
3838f002c6cSBruce M Simpson 		 * refcount and return it.
3848f002c6cSBruce M Simpson 		 */
3858f002c6cSBruce M Simpson 		KASSERT(inm->in6m_refcount >= 1,
3868f002c6cSBruce M Simpson 		    ("%s: bad refcount %d", __func__, inm->in6m_refcount));
387f3e1324bSStephen Hurd 		in6m_acquire_locked(inm);
3888f002c6cSBruce M Simpson 		*pinm = inm;
3898f002c6cSBruce M Simpson 		goto out_locked;
3908f002c6cSBruce M Simpson 	}
3918f002c6cSBruce M Simpson 
3928f002c6cSBruce M Simpson 	memset(&gsin6, 0, sizeof(gsin6));
3938f002c6cSBruce M Simpson 	gsin6.sin6_family = AF_INET6;
3948f002c6cSBruce M Simpson 	gsin6.sin6_len = sizeof(struct sockaddr_in6);
3958f002c6cSBruce M Simpson 	gsin6.sin6_addr = *group;
3968f002c6cSBruce M Simpson 
3978f002c6cSBruce M Simpson 	/*
3988f002c6cSBruce M Simpson 	 * Check if a link-layer group is already associated
3998f002c6cSBruce M Simpson 	 * with this network-layer group on the given ifnet.
4008f002c6cSBruce M Simpson 	 */
401f3e1324bSStephen Hurd 	IN6_MULTI_LIST_UNLOCK();
402137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
4038f002c6cSBruce M Simpson 	error = if_addmulti(ifp, (struct sockaddr *)&gsin6, &ifma);
4048f002c6cSBruce M Simpson 	if (error != 0)
4058f002c6cSBruce M Simpson 		return (error);
406f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK();
407137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
4088f002c6cSBruce M Simpson 
4098f002c6cSBruce M Simpson 	/*
4108f002c6cSBruce M Simpson 	 * If something other than netinet6 is occupying the link-layer
4118f002c6cSBruce M Simpson 	 * group, print a meaningful error message and back out of
4128f002c6cSBruce M Simpson 	 * the allocation.
4138f002c6cSBruce M Simpson 	 * Otherwise, bump the refcount on the existing network-layer
4148f002c6cSBruce M Simpson 	 * group association and return it.
4158f002c6cSBruce M Simpson 	 */
4168f002c6cSBruce M Simpson 	if (ifma->ifma_protospec != NULL) {
4178f002c6cSBruce M Simpson 		inm = (struct in6_multi *)ifma->ifma_protospec;
4188f002c6cSBruce M Simpson #ifdef INVARIANTS
4198f002c6cSBruce M Simpson 		KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
4208f002c6cSBruce M Simpson 		    __func__));
4218f002c6cSBruce M Simpson 		KASSERT(ifma->ifma_addr->sa_family == AF_INET6,
4228f002c6cSBruce M Simpson 		    ("%s: ifma not AF_INET6", __func__));
4238f002c6cSBruce M Simpson 		KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
4248f002c6cSBruce M Simpson 		if (inm->in6m_ifma != ifma || inm->in6m_ifp != ifp ||
4258f002c6cSBruce M Simpson 		    !IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, group))
4268f002c6cSBruce M Simpson 			panic("%s: ifma %p is inconsistent with %p (%p)",
4278f002c6cSBruce M Simpson 			    __func__, ifma, inm, group);
4288f002c6cSBruce M Simpson #endif
429f3e1324bSStephen Hurd 		in6m_acquire_locked(inm);
4308f002c6cSBruce M Simpson 		*pinm = inm;
4318f002c6cSBruce M Simpson 		goto out_locked;
4328f002c6cSBruce M Simpson 	}
4338f002c6cSBruce M Simpson 
434137f91e8SJohn Baldwin 	IF_ADDR_WLOCK_ASSERT(ifp);
4358f002c6cSBruce M Simpson 
4368f002c6cSBruce M Simpson 	/*
4378f002c6cSBruce M Simpson 	 * A new in6_multi record is needed; allocate and initialize it.
4388f002c6cSBruce M Simpson 	 * We DO NOT perform an MLD join as the in6_ layer may need to
4398f002c6cSBruce M Simpson 	 * push an initial source list down to MLD to support SSM.
4408f002c6cSBruce M Simpson 	 *
4418f002c6cSBruce M Simpson 	 * The initial source filter state is INCLUDE, {} as per the RFC.
4428f002c6cSBruce M Simpson 	 * Pending state-changes per group are subject to a bounds check.
4438f002c6cSBruce M Simpson 	 */
4448f002c6cSBruce M Simpson 	inm = malloc(sizeof(*inm), M_IP6MADDR, M_NOWAIT | M_ZERO);
4458f002c6cSBruce M Simpson 	if (inm == NULL) {
446f3e1324bSStephen Hurd 		IN6_MULTI_LIST_UNLOCK();
44755faae77SConrad Meyer 		IF_ADDR_WUNLOCK(ifp);
4488f002c6cSBruce M Simpson 		if_delmulti_ifma(ifma);
44955faae77SConrad Meyer 		return (ENOMEM);
4508f002c6cSBruce M Simpson 	}
4518f002c6cSBruce M Simpson 	inm->in6m_addr = *group;
4528f002c6cSBruce M Simpson 	inm->in6m_ifp = ifp;
4538f002c6cSBruce M Simpson 	inm->in6m_mli = MLD_IFINFO(ifp);
4548f002c6cSBruce M Simpson 	inm->in6m_ifma = ifma;
4558f002c6cSBruce M Simpson 	inm->in6m_refcount = 1;
4568f002c6cSBruce M Simpson 	inm->in6m_state = MLD_NOT_MEMBER;
457a99c84d4SGleb Smirnoff 	mbufq_init(&inm->in6m_scq, MLD_MAX_STATE_CHANGES);
4588f002c6cSBruce M Simpson 
4598f002c6cSBruce M Simpson 	inm->in6m_st[0].iss_fmode = MCAST_UNDEFINED;
4608f002c6cSBruce M Simpson 	inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
4618f002c6cSBruce M Simpson 	RB_INIT(&inm->in6m_srcs);
4628f002c6cSBruce M Simpson 
4638f002c6cSBruce M Simpson 	ifma->ifma_protospec = inm;
4648f002c6cSBruce M Simpson 	*pinm = inm;
4658f002c6cSBruce M Simpson 
4668f002c6cSBruce M Simpson  out_locked:
467f3e1324bSStephen Hurd 	IN6_MULTI_LIST_UNLOCK();
468137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
4698f002c6cSBruce M Simpson 	return (error);
4708f002c6cSBruce M Simpson }
4718f002c6cSBruce M Simpson 
4728f002c6cSBruce M Simpson /*
4738f002c6cSBruce M Simpson  * Drop a reference to an in6_multi record.
4748f002c6cSBruce M Simpson  *
4758f002c6cSBruce M Simpson  * If the refcount drops to 0, free the in6_multi record and
4768f002c6cSBruce M Simpson  * delete the underlying link-layer membership.
4778f002c6cSBruce M Simpson  */
478f3e1324bSStephen Hurd static void
479f3e1324bSStephen Hurd in6m_release(struct in6_multi *inm)
4808f002c6cSBruce M Simpson {
4818f002c6cSBruce M Simpson 	struct ifmultiaddr *ifma;
482f3e1324bSStephen Hurd 	struct ifnet *ifp;
4838f002c6cSBruce M Simpson 
4848f002c6cSBruce M Simpson 	CTR2(KTR_MLD, "%s: refcount is %d", __func__, inm->in6m_refcount);
4858f002c6cSBruce M Simpson 
486f3e1324bSStephen Hurd 	MPASS(inm->in6m_refcount == 0);
4878f002c6cSBruce M Simpson 	CTR2(KTR_MLD, "%s: freeing inm %p", __func__, inm);
4888f002c6cSBruce M Simpson 
4898f002c6cSBruce M Simpson 	ifma = inm->in6m_ifma;
490f3e1324bSStephen Hurd 	ifp = inm->in6m_ifp;
491b6f6f880SMatt Macy 	MPASS(ifma->ifma_llifma == NULL);
4928f002c6cSBruce M Simpson 
4938f002c6cSBruce M Simpson 	/* XXX this access is not covered by IF_ADDR_LOCK */
4948f002c6cSBruce M Simpson 	CTR2(KTR_MLD, "%s: purging ifma %p", __func__, ifma);
495f3e1324bSStephen Hurd 	KASSERT(ifma->ifma_protospec == NULL,
496f3e1324bSStephen Hurd 	    ("%s: ifma_protospec != NULL", __func__));
497f9be0386SMatt Macy 	if (ifp == NULL)
498f9be0386SMatt Macy 		ifp = ifma->ifma_ifp;
4998f002c6cSBruce M Simpson 
5003b9b6b17SEd Maste 	if (ifp != NULL) {
50115f8acc5SEd Maste 		CURVNET_SET(ifp->if_vnet);
5028f002c6cSBruce M Simpson 		in6m_purge(inm);
5038f002c6cSBruce M Simpson 		free(inm, M_IP6MADDR);
504b6f6f880SMatt Macy 		if_delmulti_ifma_flags(ifma, 1);
50515f8acc5SEd Maste 		CURVNET_RESTORE();
506b6f6f880SMatt Macy 		if_rele(ifp);
5073b9b6b17SEd Maste 	} else {
5083b9b6b17SEd Maste 		in6m_purge(inm);
5093b9b6b17SEd Maste 		free(inm, M_IP6MADDR);
5103b9b6b17SEd Maste 		if_delmulti_ifma_flags(ifma, 1);
511b6f6f880SMatt Macy 	}
512f3e1324bSStephen Hurd }
513f3e1324bSStephen Hurd 
514431f2b87SMark Johnston /*
515431f2b87SMark Johnston  * Interface detach can happen in a taskqueue thread context, so we must use a
516431f2b87SMark Johnston  * dedicated thread to avoid deadlocks when draining in6m_release tasks.
517431f2b87SMark Johnston  */
518431f2b87SMark Johnston TASKQUEUE_DEFINE_THREAD(in6m_free);
5199b1d850bSMark Johnston static struct in6_multi_head in6m_free_list = SLIST_HEAD_INITIALIZER();
5209b1d850bSMark Johnston static void in6m_release_task(void *arg __unused, int pending __unused);
521b453d3d2SHans Petter Selasky static struct task in6m_free_task = TASK_INITIALIZER(0, in6m_release_task, NULL);
522f3e1324bSStephen Hurd 
523f3e1324bSStephen Hurd void
524f3e1324bSStephen Hurd in6m_release_list_deferred(struct in6_multi_head *inmh)
525f3e1324bSStephen Hurd {
526f3e1324bSStephen Hurd 	if (SLIST_EMPTY(inmh))
527f3e1324bSStephen Hurd 		return;
528f3e1324bSStephen Hurd 	mtx_lock(&in6_multi_free_mtx);
529f3e1324bSStephen Hurd 	SLIST_CONCAT(&in6m_free_list, inmh, in6_multi, in6m_nrele);
530f3e1324bSStephen Hurd 	mtx_unlock(&in6_multi_free_mtx);
531431f2b87SMark Johnston 	taskqueue_enqueue(taskqueue_in6m_free, &in6m_free_task);
532f3e1324bSStephen Hurd }
533f3e1324bSStephen Hurd 
534f3e1324bSStephen Hurd void
5353689652cSHans Petter Selasky in6m_release_wait(void *arg __unused)
536dea72f06SHans Petter Selasky {
5373689652cSHans Petter Selasky 
5383689652cSHans Petter Selasky 	/*
5393689652cSHans Petter Selasky 	 * Make sure all pending multicast addresses are freed before
5403689652cSHans Petter Selasky 	 * the VNET or network device is destroyed:
5413689652cSHans Petter Selasky 	 */
542431f2b87SMark Johnston 	taskqueue_drain_all(taskqueue_in6m_free);
543dea72f06SHans Petter Selasky }
5443689652cSHans Petter Selasky #ifdef VIMAGE
545f9461246SBjoern A. Zeeb /* XXX-BZ FIXME, see D24914. */
5463689652cSHans Petter Selasky VNET_SYSUNINIT(in6m_release_wait, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, in6m_release_wait, NULL);
5473689652cSHans Petter Selasky #endif
548dea72f06SHans Petter Selasky 
549dea72f06SHans Petter Selasky void
5502cd6ad76SHans Petter Selasky in6m_disconnect_locked(struct in6_multi_head *inmh, struct in6_multi *inm)
551f3e1324bSStephen Hurd {
552f3e1324bSStephen Hurd 	struct ifnet *ifp;
553f3e1324bSStephen Hurd 	struct ifaddr *ifa;
554f3e1324bSStephen Hurd 	struct in6_ifaddr *ifa6;
55566fe09d8SAndrew Gallatin 	struct in6_multi_mship *imm, *imm_tmp;
556b6f6f880SMatt Macy 	struct ifmultiaddr *ifma, *ll_ifma;
557f3e1324bSStephen Hurd 
5582cd6ad76SHans Petter Selasky 	IN6_MULTI_LIST_LOCK_ASSERT();
559f9be0386SMatt Macy 
5602cd6ad76SHans Petter Selasky 	ifp = inm->in6m_ifp;
561f9be0386SMatt Macy 	if (ifp == NULL)
5622cd6ad76SHans Petter Selasky 		return;		/* already called */
5632cd6ad76SHans Petter Selasky 
564f9be0386SMatt Macy 	inm->in6m_ifp = NULL;
565b6f6f880SMatt Macy 	IF_ADDR_WLOCK_ASSERT(ifp);
566b6f6f880SMatt Macy 	ifma = inm->in6m_ifma;
567f9be0386SMatt Macy 	if (ifma == NULL)
568f9be0386SMatt Macy 		return;
569b6f6f880SMatt Macy 
570b6f6f880SMatt Macy 	if_ref(ifp);
571f9be0386SMatt Macy 	if (ifma->ifma_flags & IFMA_F_ENQUEUED) {
572d7c5a620SMatt Macy 		CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
573f9be0386SMatt Macy 		ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
574f9be0386SMatt Macy 	}
575b6f6f880SMatt Macy 	MCDPRINTF("removed ifma: %p from %s\n", ifma, ifp->if_xname);
576b6f6f880SMatt Macy 	if ((ll_ifma = ifma->ifma_llifma) != NULL) {
577b6f6f880SMatt Macy 		MPASS(ifma != ll_ifma);
578b6f6f880SMatt Macy 		ifma->ifma_llifma = NULL;
579b6f6f880SMatt Macy 		MPASS(ll_ifma->ifma_llifma == NULL);
580b6f6f880SMatt Macy 		MPASS(ll_ifma->ifma_ifp == ifp);
581b6f6f880SMatt Macy 		if (--ll_ifma->ifma_refcount == 0) {
582f9be0386SMatt Macy 			if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
583d7c5a620SMatt Macy 				CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, ifma_link);
584d3878608SMatt Macy 				ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
585f9be0386SMatt Macy 			}
586b6f6f880SMatt Macy 			MCDPRINTF("removed ll_ifma: %p from %s\n", ll_ifma, ifp->if_xname);
587b6f6f880SMatt Macy 			if_freemulti(ll_ifma);
588b6f6f880SMatt Macy 		}
589b6f6f880SMatt Macy 	}
590d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
591f3e1324bSStephen Hurd 		if (ifa->ifa_addr->sa_family != AF_INET6)
592f3e1324bSStephen Hurd 			continue;
593f3e1324bSStephen Hurd 		ifa6 = (void *)ifa;
59466fe09d8SAndrew Gallatin 		LIST_FOREACH_SAFE(imm, &ifa6->ia6_memberships,
59566fe09d8SAndrew Gallatin 		    i6mm_chain, imm_tmp) {
596b6f6f880SMatt Macy 			if (inm == imm->i6mm_maddr) {
597b6f6f880SMatt Macy 				LIST_REMOVE(imm, i6mm_chain);
598b6f6f880SMatt Macy 				free(imm, M_IP6MADDR);
5992cd6ad76SHans Petter Selasky 				in6m_rele_locked(inmh, inm);
600f3e1324bSStephen Hurd 			}
601f3e1324bSStephen Hurd 		}
602b6f6f880SMatt Macy 	}
603b6f6f880SMatt Macy }
604b6f6f880SMatt Macy 
605f3e1324bSStephen Hurd static void
6069b1d850bSMark Johnston in6m_release_task(void *arg __unused, int pending __unused)
607f3e1324bSStephen Hurd {
608f3e1324bSStephen Hurd 	struct in6_multi_head in6m_free_tmp;
609f3e1324bSStephen Hurd 	struct in6_multi *inm, *tinm;
610f3e1324bSStephen Hurd 
611f3e1324bSStephen Hurd 	SLIST_INIT(&in6m_free_tmp);
612f3e1324bSStephen Hurd 	mtx_lock(&in6_multi_free_mtx);
613f3e1324bSStephen Hurd 	SLIST_CONCAT(&in6m_free_tmp, &in6m_free_list, in6_multi, in6m_nrele);
614f3e1324bSStephen Hurd 	mtx_unlock(&in6_multi_free_mtx);
615f3e1324bSStephen Hurd 	IN6_MULTI_LOCK();
616f3e1324bSStephen Hurd 	SLIST_FOREACH_SAFE(inm, &in6m_free_tmp, in6m_nrele, tinm) {
617f3e1324bSStephen Hurd 		SLIST_REMOVE_HEAD(&in6m_free_tmp, in6m_nrele);
618f3e1324bSStephen Hurd 		in6m_release(inm);
619f3e1324bSStephen Hurd 	}
620f3e1324bSStephen Hurd 	IN6_MULTI_UNLOCK();
6218f002c6cSBruce M Simpson }
6228f002c6cSBruce M Simpson 
6238f002c6cSBruce M Simpson /*
6248f002c6cSBruce M Simpson  * Clear recorded source entries for a group.
6258f002c6cSBruce M Simpson  * Used by the MLD code. Caller must hold the IN6_MULTI lock.
6268f002c6cSBruce M Simpson  * FIXME: Should reap.
6278f002c6cSBruce M Simpson  */
6288f002c6cSBruce M Simpson void
6298f002c6cSBruce M Simpson in6m_clear_recorded(struct in6_multi *inm)
6308f002c6cSBruce M Simpson {
6318f002c6cSBruce M Simpson 	struct ip6_msource	*ims;
6328f002c6cSBruce M Simpson 
633f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK_ASSERT();
6348f002c6cSBruce M Simpson 
6358f002c6cSBruce M Simpson 	RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
6368f002c6cSBruce M Simpson 		if (ims->im6s_stp) {
6378f002c6cSBruce M Simpson 			ims->im6s_stp = 0;
6388f002c6cSBruce M Simpson 			--inm->in6m_st[1].iss_rec;
6398f002c6cSBruce M Simpson 		}
6408f002c6cSBruce M Simpson 	}
6418f002c6cSBruce M Simpson 	KASSERT(inm->in6m_st[1].iss_rec == 0,
6428f002c6cSBruce M Simpson 	    ("%s: iss_rec %d not 0", __func__, inm->in6m_st[1].iss_rec));
6438f002c6cSBruce M Simpson }
6448f002c6cSBruce M Simpson 
6458f002c6cSBruce M Simpson /*
6468f002c6cSBruce M Simpson  * Record a source as pending for a Source-Group MLDv2 query.
6478f002c6cSBruce M Simpson  * This lives here as it modifies the shared tree.
6488f002c6cSBruce M Simpson  *
6498f002c6cSBruce M Simpson  * inm is the group descriptor.
6508f002c6cSBruce M Simpson  * naddr is the address of the source to record in network-byte order.
6518f002c6cSBruce M Simpson  *
6528f002c6cSBruce M Simpson  * If the net.inet6.mld.sgalloc sysctl is non-zero, we will
6538f002c6cSBruce M Simpson  * lazy-allocate a source node in response to an SG query.
6548f002c6cSBruce M Simpson  * Otherwise, no allocation is performed. This saves some memory
6558f002c6cSBruce M Simpson  * with the trade-off that the source will not be reported to the
6568f002c6cSBruce M Simpson  * router if joined in the window between the query response and
6578f002c6cSBruce M Simpson  * the group actually being joined on the local host.
6588f002c6cSBruce M Simpson  *
6598f002c6cSBruce M Simpson  * VIMAGE: XXX: Currently the mld_sgalloc feature has been removed.
6608f002c6cSBruce M Simpson  * This turns off the allocation of a recorded source entry if
6618f002c6cSBruce M Simpson  * the group has not been joined.
6628f002c6cSBruce M Simpson  *
6638f002c6cSBruce M Simpson  * Return 0 if the source didn't exist or was already marked as recorded.
6648f002c6cSBruce M Simpson  * Return 1 if the source was marked as recorded by this function.
665a4641f4eSPedro F. Giffuni  * Return <0 if any error occurred (negated errno code).
6668f002c6cSBruce M Simpson  */
6678f002c6cSBruce M Simpson int
6688f002c6cSBruce M Simpson in6m_record_source(struct in6_multi *inm, const struct in6_addr *addr)
6698f002c6cSBruce M Simpson {
6708f002c6cSBruce M Simpson 	struct ip6_msource	 find;
6718f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *nims;
6728f002c6cSBruce M Simpson 
673f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK_ASSERT();
6748f002c6cSBruce M Simpson 
6758f002c6cSBruce M Simpson 	find.im6s_addr = *addr;
6768f002c6cSBruce M Simpson 	ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
6778f002c6cSBruce M Simpson 	if (ims && ims->im6s_stp)
6788f002c6cSBruce M Simpson 		return (0);
6798f002c6cSBruce M Simpson 	if (ims == NULL) {
6808f002c6cSBruce M Simpson 		if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
6818f002c6cSBruce M Simpson 			return (-ENOSPC);
6828f002c6cSBruce M Simpson 		nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
6838f002c6cSBruce M Simpson 		    M_NOWAIT | M_ZERO);
6848f002c6cSBruce M Simpson 		if (nims == NULL)
6858f002c6cSBruce M Simpson 			return (-ENOMEM);
6868f002c6cSBruce M Simpson 		nims->im6s_addr = find.im6s_addr;
6878f002c6cSBruce M Simpson 		RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
6888f002c6cSBruce M Simpson 		++inm->in6m_nsrc;
6898f002c6cSBruce M Simpson 		ims = nims;
6908f002c6cSBruce M Simpson 	}
6918f002c6cSBruce M Simpson 
6928f002c6cSBruce M Simpson 	/*
6938f002c6cSBruce M Simpson 	 * Mark the source as recorded and update the recorded
6948f002c6cSBruce M Simpson 	 * source count.
6958f002c6cSBruce M Simpson 	 */
6968f002c6cSBruce M Simpson 	++ims->im6s_stp;
6978f002c6cSBruce M Simpson 	++inm->in6m_st[1].iss_rec;
6988f002c6cSBruce M Simpson 
6998f002c6cSBruce M Simpson 	return (1);
7008f002c6cSBruce M Simpson }
7018f002c6cSBruce M Simpson 
7028f002c6cSBruce M Simpson /*
7038f002c6cSBruce M Simpson  * Return a pointer to an in6_msource owned by an in6_mfilter,
7048f002c6cSBruce M Simpson  * given its source address.
7058f002c6cSBruce M Simpson  * Lazy-allocate if needed. If this is a new entry its filter state is
7068f002c6cSBruce M Simpson  * undefined at t0.
7078f002c6cSBruce M Simpson  *
7088f002c6cSBruce M Simpson  * imf is the filter set being modified.
7098f002c6cSBruce M Simpson  * addr is the source address.
7108f002c6cSBruce M Simpson  *
7118f002c6cSBruce M Simpson  * SMPng: May be called with locks held; malloc must not block.
7128f002c6cSBruce M Simpson  */
7138f002c6cSBruce M Simpson static int
7148f002c6cSBruce M Simpson im6f_get_source(struct in6_mfilter *imf, const struct sockaddr_in6 *psin,
7158f002c6cSBruce M Simpson     struct in6_msource **plims)
7168f002c6cSBruce M Simpson {
7178f002c6cSBruce M Simpson 	struct ip6_msource	 find;
7188f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *nims;
7198f002c6cSBruce M Simpson 	struct in6_msource	*lims;
7208f002c6cSBruce M Simpson 	int			 error;
7218f002c6cSBruce M Simpson 
7228f002c6cSBruce M Simpson 	error = 0;
7238f002c6cSBruce M Simpson 	ims = NULL;
7248f002c6cSBruce M Simpson 	lims = NULL;
7258f002c6cSBruce M Simpson 
7268f002c6cSBruce M Simpson 	find.im6s_addr = psin->sin6_addr;
7278f002c6cSBruce M Simpson 	ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
7288f002c6cSBruce M Simpson 	lims = (struct in6_msource *)ims;
7298f002c6cSBruce M Simpson 	if (lims == NULL) {
7308f002c6cSBruce M Simpson 		if (imf->im6f_nsrc == in6_mcast_maxsocksrc)
7318f002c6cSBruce M Simpson 			return (ENOSPC);
7328f002c6cSBruce M Simpson 		nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
7338f002c6cSBruce M Simpson 		    M_NOWAIT | M_ZERO);
7348f002c6cSBruce M Simpson 		if (nims == NULL)
7358f002c6cSBruce M Simpson 			return (ENOMEM);
7368f002c6cSBruce M Simpson 		lims = (struct in6_msource *)nims;
7378f002c6cSBruce M Simpson 		lims->im6s_addr = find.im6s_addr;
7388f002c6cSBruce M Simpson 		lims->im6sl_st[0] = MCAST_UNDEFINED;
7398f002c6cSBruce M Simpson 		RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
7408f002c6cSBruce M Simpson 		++imf->im6f_nsrc;
7418f002c6cSBruce M Simpson 	}
7428f002c6cSBruce M Simpson 
7438f002c6cSBruce M Simpson 	*plims = lims;
7448f002c6cSBruce M Simpson 
7458f002c6cSBruce M Simpson 	return (error);
7468f002c6cSBruce M Simpson }
7478f002c6cSBruce M Simpson 
7488f002c6cSBruce M Simpson /*
7498f002c6cSBruce M Simpson  * Graft a source entry into an existing socket-layer filter set,
7508f002c6cSBruce M Simpson  * maintaining any required invariants and checking allocations.
7518f002c6cSBruce M Simpson  *
7528f002c6cSBruce M Simpson  * The source is marked as being in the new filter mode at t1.
7538f002c6cSBruce M Simpson  *
7548f002c6cSBruce M Simpson  * Return the pointer to the new node, otherwise return NULL.
7558f002c6cSBruce M Simpson  */
7568f002c6cSBruce M Simpson static struct in6_msource *
7578f002c6cSBruce M Simpson im6f_graft(struct in6_mfilter *imf, const uint8_t st1,
7588f002c6cSBruce M Simpson     const struct sockaddr_in6 *psin)
7598f002c6cSBruce M Simpson {
7608f002c6cSBruce M Simpson 	struct ip6_msource	*nims;
7618f002c6cSBruce M Simpson 	struct in6_msource	*lims;
7628f002c6cSBruce M Simpson 
7638f002c6cSBruce M Simpson 	nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
7648f002c6cSBruce M Simpson 	    M_NOWAIT | M_ZERO);
7658f002c6cSBruce M Simpson 	if (nims == NULL)
7668f002c6cSBruce M Simpson 		return (NULL);
7678f002c6cSBruce M Simpson 	lims = (struct in6_msource *)nims;
7688f002c6cSBruce M Simpson 	lims->im6s_addr = psin->sin6_addr;
7698f002c6cSBruce M Simpson 	lims->im6sl_st[0] = MCAST_UNDEFINED;
7708f002c6cSBruce M Simpson 	lims->im6sl_st[1] = st1;
7718f002c6cSBruce M Simpson 	RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
7728f002c6cSBruce M Simpson 	++imf->im6f_nsrc;
7738f002c6cSBruce M Simpson 
7748f002c6cSBruce M Simpson 	return (lims);
7758f002c6cSBruce M Simpson }
7768f002c6cSBruce M Simpson 
7778f002c6cSBruce M Simpson /*
7788f002c6cSBruce M Simpson  * Prune a source entry from an existing socket-layer filter set,
7798f002c6cSBruce M Simpson  * maintaining any required invariants and checking allocations.
7808f002c6cSBruce M Simpson  *
7818f002c6cSBruce M Simpson  * The source is marked as being left at t1, it is not freed.
7828f002c6cSBruce M Simpson  *
7838f002c6cSBruce M Simpson  * Return 0 if no error occurred, otherwise return an errno value.
7848f002c6cSBruce M Simpson  */
7858f002c6cSBruce M Simpson static int
7868f002c6cSBruce M Simpson im6f_prune(struct in6_mfilter *imf, const struct sockaddr_in6 *psin)
7878f002c6cSBruce M Simpson {
7888f002c6cSBruce M Simpson 	struct ip6_msource	 find;
7898f002c6cSBruce M Simpson 	struct ip6_msource	*ims;
7908f002c6cSBruce M Simpson 	struct in6_msource	*lims;
7918f002c6cSBruce M Simpson 
7928f002c6cSBruce M Simpson 	find.im6s_addr = psin->sin6_addr;
7938f002c6cSBruce M Simpson 	ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
7948f002c6cSBruce M Simpson 	if (ims == NULL)
7958f002c6cSBruce M Simpson 		return (ENOENT);
7968f002c6cSBruce M Simpson 	lims = (struct in6_msource *)ims;
7978f002c6cSBruce M Simpson 	lims->im6sl_st[1] = MCAST_UNDEFINED;
7988f002c6cSBruce M Simpson 	return (0);
7998f002c6cSBruce M Simpson }
8008f002c6cSBruce M Simpson 
8018f002c6cSBruce M Simpson /*
8028f002c6cSBruce M Simpson  * Revert socket-layer filter set deltas at t1 to t0 state.
8038f002c6cSBruce M Simpson  */
8048f002c6cSBruce M Simpson static void
8058f002c6cSBruce M Simpson im6f_rollback(struct in6_mfilter *imf)
8068f002c6cSBruce M Simpson {
8078f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *tims;
8088f002c6cSBruce M Simpson 	struct in6_msource	*lims;
8098f002c6cSBruce M Simpson 
8108f002c6cSBruce M Simpson 	RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
8118f002c6cSBruce M Simpson 		lims = (struct in6_msource *)ims;
8128f002c6cSBruce M Simpson 		if (lims->im6sl_st[0] == lims->im6sl_st[1]) {
8138f002c6cSBruce M Simpson 			/* no change at t1 */
8148f002c6cSBruce M Simpson 			continue;
8158f002c6cSBruce M Simpson 		} else if (lims->im6sl_st[0] != MCAST_UNDEFINED) {
8168f002c6cSBruce M Simpson 			/* revert change to existing source at t1 */
8178f002c6cSBruce M Simpson 			lims->im6sl_st[1] = lims->im6sl_st[0];
8188f002c6cSBruce M Simpson 		} else {
8198f002c6cSBruce M Simpson 			/* revert source added t1 */
8208f002c6cSBruce M Simpson 			CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
8218f002c6cSBruce M Simpson 			RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
8228f002c6cSBruce M Simpson 			free(ims, M_IN6MFILTER);
8238f002c6cSBruce M Simpson 			imf->im6f_nsrc--;
8248f002c6cSBruce M Simpson 		}
8258f002c6cSBruce M Simpson 	}
8268f002c6cSBruce M Simpson 	imf->im6f_st[1] = imf->im6f_st[0];
8278f002c6cSBruce M Simpson }
8288f002c6cSBruce M Simpson 
8298f002c6cSBruce M Simpson /*
8308f002c6cSBruce M Simpson  * Mark socket-layer filter set as INCLUDE {} at t1.
8318f002c6cSBruce M Simpson  */
8328f002c6cSBruce M Simpson static void
8338f002c6cSBruce M Simpson im6f_leave(struct in6_mfilter *imf)
8348f002c6cSBruce M Simpson {
8358f002c6cSBruce M Simpson 	struct ip6_msource	*ims;
8368f002c6cSBruce M Simpson 	struct in6_msource	*lims;
8378f002c6cSBruce M Simpson 
8388f002c6cSBruce M Simpson 	RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
8398f002c6cSBruce M Simpson 		lims = (struct in6_msource *)ims;
8408f002c6cSBruce M Simpson 		lims->im6sl_st[1] = MCAST_UNDEFINED;
8418f002c6cSBruce M Simpson 	}
8428f002c6cSBruce M Simpson 	imf->im6f_st[1] = MCAST_INCLUDE;
8438f002c6cSBruce M Simpson }
8448f002c6cSBruce M Simpson 
8458f002c6cSBruce M Simpson /*
8468f002c6cSBruce M Simpson  * Mark socket-layer filter set deltas as committed.
8478f002c6cSBruce M Simpson  */
8488f002c6cSBruce M Simpson static void
8498f002c6cSBruce M Simpson im6f_commit(struct in6_mfilter *imf)
8508f002c6cSBruce M Simpson {
8518f002c6cSBruce M Simpson 	struct ip6_msource	*ims;
8528f002c6cSBruce M Simpson 	struct in6_msource	*lims;
8538f002c6cSBruce M Simpson 
8548f002c6cSBruce M Simpson 	RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
8558f002c6cSBruce M Simpson 		lims = (struct in6_msource *)ims;
8568f002c6cSBruce M Simpson 		lims->im6sl_st[0] = lims->im6sl_st[1];
8578f002c6cSBruce M Simpson 	}
8588f002c6cSBruce M Simpson 	imf->im6f_st[0] = imf->im6f_st[1];
8598f002c6cSBruce M Simpson }
8608f002c6cSBruce M Simpson 
8618f002c6cSBruce M Simpson /*
8628f002c6cSBruce M Simpson  * Reap unreferenced sources from socket-layer filter set.
8638f002c6cSBruce M Simpson  */
8648f002c6cSBruce M Simpson static void
8658f002c6cSBruce M Simpson im6f_reap(struct in6_mfilter *imf)
8668f002c6cSBruce M Simpson {
8678f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *tims;
8688f002c6cSBruce M Simpson 	struct in6_msource	*lims;
8698f002c6cSBruce M Simpson 
8708f002c6cSBruce M Simpson 	RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
8718f002c6cSBruce M Simpson 		lims = (struct in6_msource *)ims;
8728f002c6cSBruce M Simpson 		if ((lims->im6sl_st[0] == MCAST_UNDEFINED) &&
8738f002c6cSBruce M Simpson 		    (lims->im6sl_st[1] == MCAST_UNDEFINED)) {
8748f002c6cSBruce M Simpson 			CTR2(KTR_MLD, "%s: free lims %p", __func__, ims);
8758f002c6cSBruce M Simpson 			RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
8768f002c6cSBruce M Simpson 			free(ims, M_IN6MFILTER);
8778f002c6cSBruce M Simpson 			imf->im6f_nsrc--;
8788f002c6cSBruce M Simpson 		}
8798f002c6cSBruce M Simpson 	}
8808f002c6cSBruce M Simpson }
8818f002c6cSBruce M Simpson 
8828f002c6cSBruce M Simpson /*
8838f002c6cSBruce M Simpson  * Purge socket-layer filter set.
8848f002c6cSBruce M Simpson  */
8858f002c6cSBruce M Simpson static void
8868f002c6cSBruce M Simpson im6f_purge(struct in6_mfilter *imf)
8878f002c6cSBruce M Simpson {
8888f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *tims;
8898f002c6cSBruce M Simpson 
8908f002c6cSBruce M Simpson 	RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
8918f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
8928f002c6cSBruce M Simpson 		RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
8938f002c6cSBruce M Simpson 		free(ims, M_IN6MFILTER);
8948f002c6cSBruce M Simpson 		imf->im6f_nsrc--;
8958f002c6cSBruce M Simpson 	}
8968f002c6cSBruce M Simpson 	imf->im6f_st[0] = imf->im6f_st[1] = MCAST_UNDEFINED;
8978f002c6cSBruce M Simpson 	KASSERT(RB_EMPTY(&imf->im6f_sources),
8988f002c6cSBruce M Simpson 	    ("%s: im6f_sources not empty", __func__));
8998f002c6cSBruce M Simpson }
9008f002c6cSBruce M Simpson 
9018f002c6cSBruce M Simpson /*
9028f002c6cSBruce M Simpson  * Look up a source filter entry for a multicast group.
9038f002c6cSBruce M Simpson  *
9048f002c6cSBruce M Simpson  * inm is the group descriptor to work with.
9058f002c6cSBruce M Simpson  * addr is the IPv6 address to look up.
9068f002c6cSBruce M Simpson  * noalloc may be non-zero to suppress allocation of sources.
9078f002c6cSBruce M Simpson  * *pims will be set to the address of the retrieved or allocated source.
9088f002c6cSBruce M Simpson  *
9098f002c6cSBruce M Simpson  * SMPng: NOTE: may be called with locks held.
9108f002c6cSBruce M Simpson  * Return 0 if successful, otherwise return a non-zero error code.
9118f002c6cSBruce M Simpson  */
9128f002c6cSBruce M Simpson static int
9138f002c6cSBruce M Simpson in6m_get_source(struct in6_multi *inm, const struct in6_addr *addr,
9148f002c6cSBruce M Simpson     const int noalloc, struct ip6_msource **pims)
9158f002c6cSBruce M Simpson {
9168f002c6cSBruce M Simpson 	struct ip6_msource	 find;
9178f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *nims;
9188f002c6cSBruce M Simpson #ifdef KTR
9198f002c6cSBruce M Simpson 	char			 ip6tbuf[INET6_ADDRSTRLEN];
9208f002c6cSBruce M Simpson #endif
9218f002c6cSBruce M Simpson 
9228f002c6cSBruce M Simpson 	find.im6s_addr = *addr;
9238f002c6cSBruce M Simpson 	ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
9248f002c6cSBruce M Simpson 	if (ims == NULL && !noalloc) {
9258f002c6cSBruce M Simpson 		if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
9268f002c6cSBruce M Simpson 			return (ENOSPC);
9278f002c6cSBruce M Simpson 		nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
9288f002c6cSBruce M Simpson 		    M_NOWAIT | M_ZERO);
9298f002c6cSBruce M Simpson 		if (nims == NULL)
9308f002c6cSBruce M Simpson 			return (ENOMEM);
9318f002c6cSBruce M Simpson 		nims->im6s_addr = *addr;
9328f002c6cSBruce M Simpson 		RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
9338f002c6cSBruce M Simpson 		++inm->in6m_nsrc;
9348f002c6cSBruce M Simpson 		ims = nims;
9358f002c6cSBruce M Simpson 		CTR3(KTR_MLD, "%s: allocated %s as %p", __func__,
9368f002c6cSBruce M Simpson 		    ip6_sprintf(ip6tbuf, addr), ims);
9378f002c6cSBruce M Simpson 	}
9388f002c6cSBruce M Simpson 
9398f002c6cSBruce M Simpson 	*pims = ims;
9408f002c6cSBruce M Simpson 	return (0);
9418f002c6cSBruce M Simpson }
9428f002c6cSBruce M Simpson 
9438f002c6cSBruce M Simpson /*
9448f002c6cSBruce M Simpson  * Merge socket-layer source into MLD-layer source.
9458f002c6cSBruce M Simpson  * If rollback is non-zero, perform the inverse of the merge.
9468f002c6cSBruce M Simpson  */
9478f002c6cSBruce M Simpson static void
9488f002c6cSBruce M Simpson im6s_merge(struct ip6_msource *ims, const struct in6_msource *lims,
9498f002c6cSBruce M Simpson     const int rollback)
9508f002c6cSBruce M Simpson {
9518f002c6cSBruce M Simpson 	int n = rollback ? -1 : 1;
9528f002c6cSBruce M Simpson #ifdef KTR
9538f002c6cSBruce M Simpson 	char ip6tbuf[INET6_ADDRSTRLEN];
9548f002c6cSBruce M Simpson 
9558f002c6cSBruce M Simpson 	ip6_sprintf(ip6tbuf, &lims->im6s_addr);
9568f002c6cSBruce M Simpson #endif
9578f002c6cSBruce M Simpson 
9588f002c6cSBruce M Simpson 	if (lims->im6sl_st[0] == MCAST_EXCLUDE) {
9598f002c6cSBruce M Simpson 		CTR3(KTR_MLD, "%s: t1 ex -= %d on %s", __func__, n, ip6tbuf);
9608f002c6cSBruce M Simpson 		ims->im6s_st[1].ex -= n;
9618f002c6cSBruce M Simpson 	} else if (lims->im6sl_st[0] == MCAST_INCLUDE) {
9628f002c6cSBruce M Simpson 		CTR3(KTR_MLD, "%s: t1 in -= %d on %s", __func__, n, ip6tbuf);
9638f002c6cSBruce M Simpson 		ims->im6s_st[1].in -= n;
9648f002c6cSBruce M Simpson 	}
9658f002c6cSBruce M Simpson 
9668f002c6cSBruce M Simpson 	if (lims->im6sl_st[1] == MCAST_EXCLUDE) {
9678f002c6cSBruce M Simpson 		CTR3(KTR_MLD, "%s: t1 ex += %d on %s", __func__, n, ip6tbuf);
9688f002c6cSBruce M Simpson 		ims->im6s_st[1].ex += n;
9698f002c6cSBruce M Simpson 	} else if (lims->im6sl_st[1] == MCAST_INCLUDE) {
9708f002c6cSBruce M Simpson 		CTR3(KTR_MLD, "%s: t1 in += %d on %s", __func__, n, ip6tbuf);
9718f002c6cSBruce M Simpson 		ims->im6s_st[1].in += n;
9728f002c6cSBruce M Simpson 	}
9738f002c6cSBruce M Simpson }
9748f002c6cSBruce M Simpson 
9758f002c6cSBruce M Simpson /*
9768f002c6cSBruce M Simpson  * Atomically update the global in6_multi state, when a membership's
9778f002c6cSBruce M Simpson  * filter list is being updated in any way.
9788f002c6cSBruce M Simpson  *
9798f002c6cSBruce M Simpson  * imf is the per-inpcb-membership group filter pointer.
9808f002c6cSBruce M Simpson  * A fake imf may be passed for in-kernel consumers.
9818f002c6cSBruce M Simpson  *
9828f002c6cSBruce M Simpson  * XXX This is a candidate for a set-symmetric-difference style loop
9838f002c6cSBruce M Simpson  * which would eliminate the repeated lookup from root of ims nodes,
9848f002c6cSBruce M Simpson  * as they share the same key space.
9858f002c6cSBruce M Simpson  *
9868f002c6cSBruce M Simpson  * If any error occurred this function will back out of refcounts
9878f002c6cSBruce M Simpson  * and return a non-zero value.
9888f002c6cSBruce M Simpson  */
9898f002c6cSBruce M Simpson static int
9908f002c6cSBruce M Simpson in6m_merge(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
9918f002c6cSBruce M Simpson {
9928f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *nims;
9938f002c6cSBruce M Simpson 	struct in6_msource	*lims;
9948f002c6cSBruce M Simpson 	int			 schanged, error;
9958f002c6cSBruce M Simpson 	int			 nsrc0, nsrc1;
9968f002c6cSBruce M Simpson 
9978f002c6cSBruce M Simpson 	schanged = 0;
9988f002c6cSBruce M Simpson 	error = 0;
9998f002c6cSBruce M Simpson 	nsrc1 = nsrc0 = 0;
1000f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK_ASSERT();
10018f002c6cSBruce M Simpson 
10028f002c6cSBruce M Simpson 	/*
10038f002c6cSBruce M Simpson 	 * Update the source filters first, as this may fail.
10048f002c6cSBruce M Simpson 	 * Maintain count of in-mode filters at t0, t1. These are
10058f002c6cSBruce M Simpson 	 * used to work out if we transition into ASM mode or not.
10068f002c6cSBruce M Simpson 	 * Maintain a count of source filters whose state was
10078f002c6cSBruce M Simpson 	 * actually modified by this operation.
10088f002c6cSBruce M Simpson 	 */
10098f002c6cSBruce M Simpson 	RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
10108f002c6cSBruce M Simpson 		lims = (struct in6_msource *)ims;
10118f002c6cSBruce M Simpson 		if (lims->im6sl_st[0] == imf->im6f_st[0]) nsrc0++;
10128f002c6cSBruce M Simpson 		if (lims->im6sl_st[1] == imf->im6f_st[1]) nsrc1++;
10138f002c6cSBruce M Simpson 		if (lims->im6sl_st[0] == lims->im6sl_st[1]) continue;
10148f002c6cSBruce M Simpson 		error = in6m_get_source(inm, &lims->im6s_addr, 0, &nims);
10158f002c6cSBruce M Simpson 		++schanged;
10168f002c6cSBruce M Simpson 		if (error)
10178f002c6cSBruce M Simpson 			break;
10188f002c6cSBruce M Simpson 		im6s_merge(nims, lims, 0);
10198f002c6cSBruce M Simpson 	}
10208f002c6cSBruce M Simpson 	if (error) {
10218f002c6cSBruce M Simpson 		struct ip6_msource *bims;
10228f002c6cSBruce M Simpson 
10238f002c6cSBruce M Simpson 		RB_FOREACH_REVERSE_FROM(ims, ip6_msource_tree, nims) {
10248f002c6cSBruce M Simpson 			lims = (struct in6_msource *)ims;
10258f002c6cSBruce M Simpson 			if (lims->im6sl_st[0] == lims->im6sl_st[1])
10268f002c6cSBruce M Simpson 				continue;
10278f002c6cSBruce M Simpson 			(void)in6m_get_source(inm, &lims->im6s_addr, 1, &bims);
10288f002c6cSBruce M Simpson 			if (bims == NULL)
10298f002c6cSBruce M Simpson 				continue;
10308f002c6cSBruce M Simpson 			im6s_merge(bims, lims, 1);
10318f002c6cSBruce M Simpson 		}
10328f002c6cSBruce M Simpson 		goto out_reap;
10338f002c6cSBruce M Simpson 	}
10348f002c6cSBruce M Simpson 
10358f002c6cSBruce M Simpson 	CTR3(KTR_MLD, "%s: imf filters in-mode: %d at t0, %d at t1",
10368f002c6cSBruce M Simpson 	    __func__, nsrc0, nsrc1);
10378f002c6cSBruce M Simpson 
10388f002c6cSBruce M Simpson 	/* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
10398f002c6cSBruce M Simpson 	if (imf->im6f_st[0] == imf->im6f_st[1] &&
10408f002c6cSBruce M Simpson 	    imf->im6f_st[1] == MCAST_INCLUDE) {
10418f002c6cSBruce M Simpson 		if (nsrc1 == 0) {
10428f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
10438f002c6cSBruce M Simpson 			--inm->in6m_st[1].iss_in;
10448f002c6cSBruce M Simpson 		}
10458f002c6cSBruce M Simpson 	}
10468f002c6cSBruce M Simpson 
10478f002c6cSBruce M Simpson 	/* Handle filter mode transition on socket. */
10488f002c6cSBruce M Simpson 	if (imf->im6f_st[0] != imf->im6f_st[1]) {
10498f002c6cSBruce M Simpson 		CTR3(KTR_MLD, "%s: imf transition %d to %d",
10508f002c6cSBruce M Simpson 		    __func__, imf->im6f_st[0], imf->im6f_st[1]);
10518f002c6cSBruce M Simpson 
10528f002c6cSBruce M Simpson 		if (imf->im6f_st[0] == MCAST_EXCLUDE) {
10538f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: --ex on inm at t1", __func__);
10548f002c6cSBruce M Simpson 			--inm->in6m_st[1].iss_ex;
10558f002c6cSBruce M Simpson 		} else if (imf->im6f_st[0] == MCAST_INCLUDE) {
10568f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
10578f002c6cSBruce M Simpson 			--inm->in6m_st[1].iss_in;
10588f002c6cSBruce M Simpson 		}
10598f002c6cSBruce M Simpson 
10608f002c6cSBruce M Simpson 		if (imf->im6f_st[1] == MCAST_EXCLUDE) {
10618f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: ex++ on inm at t1", __func__);
10628f002c6cSBruce M Simpson 			inm->in6m_st[1].iss_ex++;
10638f002c6cSBruce M Simpson 		} else if (imf->im6f_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
10648f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: in++ on inm at t1", __func__);
10658f002c6cSBruce M Simpson 			inm->in6m_st[1].iss_in++;
10668f002c6cSBruce M Simpson 		}
10678f002c6cSBruce M Simpson 	}
10688f002c6cSBruce M Simpson 
10698f002c6cSBruce M Simpson 	/*
10708f002c6cSBruce M Simpson 	 * Track inm filter state in terms of listener counts.
10718f002c6cSBruce M Simpson 	 * If there are any exclusive listeners, stack-wide
10728f002c6cSBruce M Simpson 	 * membership is exclusive.
10738f002c6cSBruce M Simpson 	 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
10748f002c6cSBruce M Simpson 	 * If no listeners remain, state is undefined at t1,
10758f002c6cSBruce M Simpson 	 * and the MLD lifecycle for this group should finish.
10768f002c6cSBruce M Simpson 	 */
10778f002c6cSBruce M Simpson 	if (inm->in6m_st[1].iss_ex > 0) {
10788f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: transition to EX", __func__);
10798f002c6cSBruce M Simpson 		inm->in6m_st[1].iss_fmode = MCAST_EXCLUDE;
10808f002c6cSBruce M Simpson 	} else if (inm->in6m_st[1].iss_in > 0) {
10818f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: transition to IN", __func__);
10828f002c6cSBruce M Simpson 		inm->in6m_st[1].iss_fmode = MCAST_INCLUDE;
10838f002c6cSBruce M Simpson 	} else {
10848f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: transition to UNDEF", __func__);
10858f002c6cSBruce M Simpson 		inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
10868f002c6cSBruce M Simpson 	}
10878f002c6cSBruce M Simpson 
10888f002c6cSBruce M Simpson 	/* Decrement ASM listener count on transition out of ASM mode. */
10898f002c6cSBruce M Simpson 	if (imf->im6f_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
10908f002c6cSBruce M Simpson 		if ((imf->im6f_st[1] != MCAST_EXCLUDE) ||
1091bd745936SEnji Cooper 		    (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 > 0)) {
10928f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: --asm on inm at t1", __func__);
10938f002c6cSBruce M Simpson 			--inm->in6m_st[1].iss_asm;
10948f002c6cSBruce M Simpson 		}
1095bd745936SEnji Cooper 	}
10968f002c6cSBruce M Simpson 
10978f002c6cSBruce M Simpson 	/* Increment ASM listener count on transition to ASM mode. */
10988f002c6cSBruce M Simpson 	if (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
10998f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: asm++ on inm at t1", __func__);
11008f002c6cSBruce M Simpson 		inm->in6m_st[1].iss_asm++;
11018f002c6cSBruce M Simpson 	}
11028f002c6cSBruce M Simpson 
11038f002c6cSBruce M Simpson 	CTR3(KTR_MLD, "%s: merged imf %p to inm %p", __func__, imf, inm);
11048f002c6cSBruce M Simpson 	in6m_print(inm);
11058f002c6cSBruce M Simpson 
11068f002c6cSBruce M Simpson out_reap:
11078f002c6cSBruce M Simpson 	if (schanged > 0) {
11088f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: sources changed; reaping", __func__);
11098f002c6cSBruce M Simpson 		in6m_reap(inm);
11108f002c6cSBruce M Simpson 	}
11118f002c6cSBruce M Simpson 	return (error);
11128f002c6cSBruce M Simpson }
11138f002c6cSBruce M Simpson 
11148f002c6cSBruce M Simpson /*
11158f002c6cSBruce M Simpson  * Mark an in6_multi's filter set deltas as committed.
11168f002c6cSBruce M Simpson  * Called by MLD after a state change has been enqueued.
11178f002c6cSBruce M Simpson  */
11188f002c6cSBruce M Simpson void
11198f002c6cSBruce M Simpson in6m_commit(struct in6_multi *inm)
11208f002c6cSBruce M Simpson {
11218f002c6cSBruce M Simpson 	struct ip6_msource	*ims;
11228f002c6cSBruce M Simpson 
11238f002c6cSBruce M Simpson 	CTR2(KTR_MLD, "%s: commit inm %p", __func__, inm);
11248f002c6cSBruce M Simpson 	CTR1(KTR_MLD, "%s: pre commit:", __func__);
11258f002c6cSBruce M Simpson 	in6m_print(inm);
11268f002c6cSBruce M Simpson 
11278f002c6cSBruce M Simpson 	RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
11288f002c6cSBruce M Simpson 		ims->im6s_st[0] = ims->im6s_st[1];
11298f002c6cSBruce M Simpson 	}
11308f002c6cSBruce M Simpson 	inm->in6m_st[0] = inm->in6m_st[1];
11318f002c6cSBruce M Simpson }
11328f002c6cSBruce M Simpson 
11338f002c6cSBruce M Simpson /*
11348f002c6cSBruce M Simpson  * Reap unreferenced nodes from an in6_multi's filter set.
11358f002c6cSBruce M Simpson  */
11368f002c6cSBruce M Simpson static void
11378f002c6cSBruce M Simpson in6m_reap(struct in6_multi *inm)
11388f002c6cSBruce M Simpson {
11398f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *tims;
11408f002c6cSBruce M Simpson 
11418f002c6cSBruce M Simpson 	RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
11428f002c6cSBruce M Simpson 		if (ims->im6s_st[0].ex > 0 || ims->im6s_st[0].in > 0 ||
11438f002c6cSBruce M Simpson 		    ims->im6s_st[1].ex > 0 || ims->im6s_st[1].in > 0 ||
11448f002c6cSBruce M Simpson 		    ims->im6s_stp != 0)
11458f002c6cSBruce M Simpson 			continue;
11468f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
11478f002c6cSBruce M Simpson 		RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
11488f002c6cSBruce M Simpson 		free(ims, M_IP6MSOURCE);
11498f002c6cSBruce M Simpson 		inm->in6m_nsrc--;
11508f002c6cSBruce M Simpson 	}
11518f002c6cSBruce M Simpson }
11528f002c6cSBruce M Simpson 
11538f002c6cSBruce M Simpson /*
11548f002c6cSBruce M Simpson  * Purge all source nodes from an in6_multi's filter set.
11558f002c6cSBruce M Simpson  */
11568f002c6cSBruce M Simpson static void
11578f002c6cSBruce M Simpson in6m_purge(struct in6_multi *inm)
11588f002c6cSBruce M Simpson {
11598f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *tims;
11608f002c6cSBruce M Simpson 
11618f002c6cSBruce M Simpson 	RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
11628f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
11638f002c6cSBruce M Simpson 		RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
11648f002c6cSBruce M Simpson 		free(ims, M_IP6MSOURCE);
11658f002c6cSBruce M Simpson 		inm->in6m_nsrc--;
11668f002c6cSBruce M Simpson 	}
11673c268b3aSAndrey V. Elsukov 	/* Free state-change requests that might be queued. */
1168a99c84d4SGleb Smirnoff 	mbufq_drain(&inm->in6m_scq);
11698f002c6cSBruce M Simpson }
11708f002c6cSBruce M Simpson 
11718f002c6cSBruce M Simpson /*
11728f002c6cSBruce M Simpson  * Join a multicast address w/o sources.
11738f002c6cSBruce M Simpson  * KAME compatibility entry point.
11748f002c6cSBruce M Simpson  *
11758f002c6cSBruce M Simpson  * SMPng: Assume no mc locks held by caller.
11768f002c6cSBruce M Simpson  */
11778f002c6cSBruce M Simpson int
1178f3e1324bSStephen Hurd in6_joingroup(struct ifnet *ifp, const struct in6_addr *mcaddr,
11798f002c6cSBruce M Simpson     /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
11808f002c6cSBruce M Simpson     const int delay)
11818f002c6cSBruce M Simpson {
11828f002c6cSBruce M Simpson 	int error;
11838f002c6cSBruce M Simpson 
11848f002c6cSBruce M Simpson 	IN6_MULTI_LOCK();
1185f3e1324bSStephen Hurd 	error = in6_joingroup_locked(ifp, mcaddr, NULL, pinm, delay);
11868f002c6cSBruce M Simpson 	IN6_MULTI_UNLOCK();
11878f002c6cSBruce M Simpson 	return (error);
11888f002c6cSBruce M Simpson }
11898f002c6cSBruce M Simpson 
11908f002c6cSBruce M Simpson /*
11918f002c6cSBruce M Simpson  * Join a multicast group; real entry point.
11928f002c6cSBruce M Simpson  *
11938f002c6cSBruce M Simpson  * Only preserves atomicity at inm level.
11948f002c6cSBruce M Simpson  * NOTE: imf argument cannot be const due to sys/tree.h limitations.
11958f002c6cSBruce M Simpson  *
11968f002c6cSBruce M Simpson  * If the MLD downcall fails, the group is not joined, and an error
11978f002c6cSBruce M Simpson  * code is returned.
11988f002c6cSBruce M Simpson  */
1199e1891232SBjoern A. Zeeb static int
1200f3e1324bSStephen Hurd in6_joingroup_locked(struct ifnet *ifp, const struct in6_addr *mcaddr,
12018f002c6cSBruce M Simpson     /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
12028f002c6cSBruce M Simpson     const int delay)
12038f002c6cSBruce M Simpson {
12042cd6ad76SHans Petter Selasky 	struct in6_multi_head    inmh;
12058f002c6cSBruce M Simpson 	struct in6_mfilter	 timf;
12068f002c6cSBruce M Simpson 	struct in6_multi	*inm;
1207f3e1324bSStephen Hurd 	struct ifmultiaddr *ifma;
12088f002c6cSBruce M Simpson 	int			 error;
12098f002c6cSBruce M Simpson #ifdef KTR
12108f002c6cSBruce M Simpson 	char			 ip6tbuf[INET6_ADDRSTRLEN];
12118f002c6cSBruce M Simpson #endif
12128f002c6cSBruce M Simpson 
121329dc7bc6SBruce M Simpson 	/*
121429dc7bc6SBruce M Simpson 	 * Sanity: Check scope zone ID was set for ifp, if and
121529dc7bc6SBruce M Simpson 	 * only if group is scoped to an interface.
121629dc7bc6SBruce M Simpson 	 */
121729dc7bc6SBruce M Simpson 	KASSERT(IN6_IS_ADDR_MULTICAST(mcaddr),
121829dc7bc6SBruce M Simpson 	    ("%s: not a multicast address", __func__));
121929dc7bc6SBruce M Simpson 	if (IN6_IS_ADDR_MC_LINKLOCAL(mcaddr) ||
122029dc7bc6SBruce M Simpson 	    IN6_IS_ADDR_MC_INTFACELOCAL(mcaddr)) {
122129dc7bc6SBruce M Simpson 		KASSERT(mcaddr->s6_addr16[1] != 0,
122229dc7bc6SBruce M Simpson 		    ("%s: scope zone ID not set", __func__));
122329dc7bc6SBruce M Simpson 	}
122429dc7bc6SBruce M Simpson 
12258f002c6cSBruce M Simpson 	IN6_MULTI_LOCK_ASSERT();
1226f3e1324bSStephen Hurd 	IN6_MULTI_LIST_UNLOCK_ASSERT();
12278f002c6cSBruce M Simpson 
12288f002c6cSBruce M Simpson 	CTR4(KTR_MLD, "%s: join %s on %p(%s))", __func__,
1229e74966f6SAndrey V. Elsukov 	    ip6_sprintf(ip6tbuf, mcaddr), ifp, if_name(ifp));
12308f002c6cSBruce M Simpson 
12318f002c6cSBruce M Simpson 	error = 0;
12328f002c6cSBruce M Simpson 	inm = NULL;
12338f002c6cSBruce M Simpson 
12348f002c6cSBruce M Simpson 	/*
12358f002c6cSBruce M Simpson 	 * If no imf was specified (i.e. kernel consumer),
12368f002c6cSBruce M Simpson 	 * fake one up and assume it is an ASM join.
12378f002c6cSBruce M Simpson 	 */
12388f002c6cSBruce M Simpson 	if (imf == NULL) {
12398f002c6cSBruce M Simpson 		im6f_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
12408f002c6cSBruce M Simpson 		imf = &timf;
12418f002c6cSBruce M Simpson 	}
1242f3e1324bSStephen Hurd 	error = in6_getmulti(ifp, mcaddr, &inm);
12438f002c6cSBruce M Simpson 	if (error) {
1244f3e1324bSStephen Hurd 		CTR1(KTR_MLD, "%s: in6_getmulti() failure", __func__);
12458f002c6cSBruce M Simpson 		return (error);
12468f002c6cSBruce M Simpson 	}
12478f002c6cSBruce M Simpson 
1248f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK();
12498f002c6cSBruce M Simpson 	CTR1(KTR_MLD, "%s: merge inm state", __func__);
12508f002c6cSBruce M Simpson 	error = in6m_merge(inm, imf);
12518f002c6cSBruce M Simpson 	if (error) {
12528f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
12538f002c6cSBruce M Simpson 		goto out_in6m_release;
12548f002c6cSBruce M Simpson 	}
12558f002c6cSBruce M Simpson 
12568f002c6cSBruce M Simpson 	CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
12578f002c6cSBruce M Simpson 	error = mld_change_state(inm, delay);
12588f002c6cSBruce M Simpson 	if (error) {
12598f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: failed to update source", __func__);
12608f002c6cSBruce M Simpson 		goto out_in6m_release;
12618f002c6cSBruce M Simpson 	}
12628f002c6cSBruce M Simpson 
12638f002c6cSBruce M Simpson out_in6m_release:
12642cd6ad76SHans Petter Selasky 	SLIST_INIT(&inmh);
12658f002c6cSBruce M Simpson 	if (error) {
1266a68cc388SGleb Smirnoff 		struct epoch_tracker et;
1267a68cc388SGleb Smirnoff 
12688f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
12696afe56f9SRyan Libby 		IF_ADDR_WLOCK(ifp);
1270a68cc388SGleb Smirnoff 		NET_EPOCH_ENTER(et);
1271d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1272f3e1324bSStephen Hurd 			if (ifma->ifma_protospec == inm) {
1273f3e1324bSStephen Hurd 				ifma->ifma_protospec = NULL;
1274f3e1324bSStephen Hurd 				break;
1275f3e1324bSStephen Hurd 			}
1276f3e1324bSStephen Hurd 		}
12772cd6ad76SHans Petter Selasky 		in6m_disconnect_locked(&inmh, inm);
12782cd6ad76SHans Petter Selasky 		in6m_rele_locked(&inmh, inm);
1279a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
12806afe56f9SRyan Libby 		IF_ADDR_WUNLOCK(ifp);
12818f002c6cSBruce M Simpson 	} else {
12828f002c6cSBruce M Simpson 		*pinm = inm;
12838f002c6cSBruce M Simpson 	}
1284f3e1324bSStephen Hurd 	IN6_MULTI_LIST_UNLOCK();
12852cd6ad76SHans Petter Selasky 	in6m_release_list_deferred(&inmh);
12868f002c6cSBruce M Simpson 	return (error);
12878f002c6cSBruce M Simpson }
12888f002c6cSBruce M Simpson 
12898f002c6cSBruce M Simpson /*
12908f002c6cSBruce M Simpson  * Leave a multicast group; unlocked entry point.
12918f002c6cSBruce M Simpson  */
12928f002c6cSBruce M Simpson int
1293f3e1324bSStephen Hurd in6_leavegroup(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
12948f002c6cSBruce M Simpson {
12958f002c6cSBruce M Simpson 	int error;
12968f002c6cSBruce M Simpson 
12978f002c6cSBruce M Simpson 	IN6_MULTI_LOCK();
1298f3e1324bSStephen Hurd 	error = in6_leavegroup_locked(inm, imf);
12998f002c6cSBruce M Simpson 	IN6_MULTI_UNLOCK();
13008f002c6cSBruce M Simpson 	return (error);
13018f002c6cSBruce M Simpson }
13028f002c6cSBruce M Simpson 
13038f002c6cSBruce M Simpson /*
13048f002c6cSBruce M Simpson  * Leave a multicast group; real entry point.
13058f002c6cSBruce M Simpson  * All source filters will be expunged.
13068f002c6cSBruce M Simpson  *
13078f002c6cSBruce M Simpson  * Only preserves atomicity at inm level.
13088f002c6cSBruce M Simpson  *
13098f002c6cSBruce M Simpson  * Holding the write lock for the INP which contains imf
13108f002c6cSBruce M Simpson  * is highly advisable. We can't assert for it as imf does not
13118f002c6cSBruce M Simpson  * contain a back-pointer to the owning inp.
13128f002c6cSBruce M Simpson  *
13138f002c6cSBruce M Simpson  * Note: This is not the same as in6m_release(*) as this function also
13148f002c6cSBruce M Simpson  * makes a state change downcall into MLD.
13158f002c6cSBruce M Simpson  */
13168f002c6cSBruce M Simpson int
1317f3e1324bSStephen Hurd in6_leavegroup_locked(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
13188f002c6cSBruce M Simpson {
13192cd6ad76SHans Petter Selasky 	struct in6_multi_head	 inmh;
13208f002c6cSBruce M Simpson 	struct in6_mfilter	 timf;
1321f3e1324bSStephen Hurd 	struct ifnet *ifp;
13228f002c6cSBruce M Simpson 	int			 error;
13238f002c6cSBruce M Simpson #ifdef KTR
13248f002c6cSBruce M Simpson 	char			 ip6tbuf[INET6_ADDRSTRLEN];
13258f002c6cSBruce M Simpson #endif
13268f002c6cSBruce M Simpson 
13278f002c6cSBruce M Simpson 	error = 0;
13288f002c6cSBruce M Simpson 
13298f002c6cSBruce M Simpson 	IN6_MULTI_LOCK_ASSERT();
13308f002c6cSBruce M Simpson 
13318f002c6cSBruce M Simpson 	CTR5(KTR_MLD, "%s: leave inm %p, %s/%s, imf %p", __func__,
13328f002c6cSBruce M Simpson 	    inm, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1333e74966f6SAndrey V. Elsukov 	    (in6m_is_ifp_detached(inm) ? "null" : if_name(inm->in6m_ifp)),
13348f002c6cSBruce M Simpson 	    imf);
13358f002c6cSBruce M Simpson 
13368f002c6cSBruce M Simpson 	/*
13378f002c6cSBruce M Simpson 	 * If no imf was specified (i.e. kernel consumer),
13388f002c6cSBruce M Simpson 	 * fake one up and assume it is an ASM join.
13398f002c6cSBruce M Simpson 	 */
13408f002c6cSBruce M Simpson 	if (imf == NULL) {
13418f002c6cSBruce M Simpson 		im6f_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
13428f002c6cSBruce M Simpson 		imf = &timf;
13438f002c6cSBruce M Simpson 	}
13448f002c6cSBruce M Simpson 
13458f002c6cSBruce M Simpson 	/*
13468f002c6cSBruce M Simpson 	 * Begin state merge transaction at MLD layer.
13478f002c6cSBruce M Simpson 	 *
13488f002c6cSBruce M Simpson 	 * As this particular invocation should not cause any memory
13498f002c6cSBruce M Simpson 	 * to be allocated, and there is no opportunity to roll back
13508f002c6cSBruce M Simpson 	 * the transaction, it MUST NOT fail.
13518f002c6cSBruce M Simpson 	 */
1352f3e1324bSStephen Hurd 
1353f3e1324bSStephen Hurd 	ifp = inm->in6m_ifp;
1354f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK();
13558f002c6cSBruce M Simpson 	CTR1(KTR_MLD, "%s: merge inm state", __func__);
13568f002c6cSBruce M Simpson 	error = in6m_merge(inm, imf);
13578f002c6cSBruce M Simpson 	KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
13588f002c6cSBruce M Simpson 
13598f002c6cSBruce M Simpson 	CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1360f9be0386SMatt Macy 	error = 0;
1361f9be0386SMatt Macy 	if (ifp)
13628f002c6cSBruce M Simpson 		error = mld_change_state(inm, 0);
13638f002c6cSBruce M Simpson 	if (error)
13648f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
13658f002c6cSBruce M Simpson 
13668f002c6cSBruce M Simpson 	CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1367f3e1324bSStephen Hurd 	if (ifp)
1368b6f6f880SMatt Macy 		IF_ADDR_WLOCK(ifp);
13692cd6ad76SHans Petter Selasky 
13702cd6ad76SHans Petter Selasky 	SLIST_INIT(&inmh);
13712cd6ad76SHans Petter Selasky 	if (inm->in6m_refcount == 1)
13722cd6ad76SHans Petter Selasky 		in6m_disconnect_locked(&inmh, inm);
13732cd6ad76SHans Petter Selasky 	in6m_rele_locked(&inmh, inm);
1374f3e1324bSStephen Hurd 	if (ifp)
1375b6f6f880SMatt Macy 		IF_ADDR_WUNLOCK(ifp);
1376f3e1324bSStephen Hurd 	IN6_MULTI_LIST_UNLOCK();
13772cd6ad76SHans Petter Selasky 	in6m_release_list_deferred(&inmh);
13788f002c6cSBruce M Simpson 	return (error);
13798f002c6cSBruce M Simpson }
13808f002c6cSBruce M Simpson 
13818f002c6cSBruce M Simpson /*
13828f002c6cSBruce M Simpson  * Block or unblock an ASM multicast source on an inpcb.
13838f002c6cSBruce M Simpson  * This implements the delta-based API described in RFC 3678.
13848f002c6cSBruce M Simpson  *
13858f002c6cSBruce M Simpson  * The delta-based API applies only to exclusive-mode memberships.
13868f002c6cSBruce M Simpson  * An MLD downcall will be performed.
13878f002c6cSBruce M Simpson  *
13888f002c6cSBruce M Simpson  * SMPng: NOTE: Must take Giant as a join may create a new ifma.
13898f002c6cSBruce M Simpson  *
13908f002c6cSBruce M Simpson  * Return 0 if successful, otherwise return an appropriate error code.
13918f002c6cSBruce M Simpson  */
13928f002c6cSBruce M Simpson static int
13938f002c6cSBruce M Simpson in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
13948f002c6cSBruce M Simpson {
13958f002c6cSBruce M Simpson 	struct group_source_req		 gsr;
13968f002c6cSBruce M Simpson 	sockunion_t			*gsa, *ssa;
13978f002c6cSBruce M Simpson 	struct ifnet			*ifp;
13988f002c6cSBruce M Simpson 	struct in6_mfilter		*imf;
13998f002c6cSBruce M Simpson 	struct ip6_moptions		*imo;
14008f002c6cSBruce M Simpson 	struct in6_msource		*ims;
14018f002c6cSBruce M Simpson 	struct in6_multi			*inm;
14028f002c6cSBruce M Simpson 	uint16_t			 fmode;
14038f002c6cSBruce M Simpson 	int				 error, doblock;
14048f002c6cSBruce M Simpson #ifdef KTR
14058f002c6cSBruce M Simpson 	char				 ip6tbuf[INET6_ADDRSTRLEN];
14068f002c6cSBruce M Simpson #endif
14078f002c6cSBruce M Simpson 
14088f002c6cSBruce M Simpson 	ifp = NULL;
14098f002c6cSBruce M Simpson 	error = 0;
14108f002c6cSBruce M Simpson 	doblock = 0;
14118f002c6cSBruce M Simpson 
14128f002c6cSBruce M Simpson 	memset(&gsr, 0, sizeof(struct group_source_req));
14138f002c6cSBruce M Simpson 	gsa = (sockunion_t *)&gsr.gsr_group;
14148f002c6cSBruce M Simpson 	ssa = (sockunion_t *)&gsr.gsr_source;
14158f002c6cSBruce M Simpson 
14168f002c6cSBruce M Simpson 	switch (sopt->sopt_name) {
14178f002c6cSBruce M Simpson 	case MCAST_BLOCK_SOURCE:
14188f002c6cSBruce M Simpson 	case MCAST_UNBLOCK_SOURCE:
14198f002c6cSBruce M Simpson 		error = sooptcopyin(sopt, &gsr,
14208f002c6cSBruce M Simpson 		    sizeof(struct group_source_req),
14218f002c6cSBruce M Simpson 		    sizeof(struct group_source_req));
14228f002c6cSBruce M Simpson 		if (error)
14238f002c6cSBruce M Simpson 			return (error);
14248f002c6cSBruce M Simpson 
14258f002c6cSBruce M Simpson 		if (gsa->sin6.sin6_family != AF_INET6 ||
14268f002c6cSBruce M Simpson 		    gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
14278f002c6cSBruce M Simpson 			return (EINVAL);
14288f002c6cSBruce M Simpson 
14298f002c6cSBruce M Simpson 		if (ssa->sin6.sin6_family != AF_INET6 ||
14308f002c6cSBruce M Simpson 		    ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
14318f002c6cSBruce M Simpson 			return (EINVAL);
14328f002c6cSBruce M Simpson 
14338f002c6cSBruce M Simpson 		if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
14348f002c6cSBruce M Simpson 			return (EADDRNOTAVAIL);
14358f002c6cSBruce M Simpson 
14368f002c6cSBruce M Simpson 		ifp = ifnet_byindex(gsr.gsr_interface);
14378f002c6cSBruce M Simpson 
14388f002c6cSBruce M Simpson 		if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
14398f002c6cSBruce M Simpson 			doblock = 1;
14408f002c6cSBruce M Simpson 		break;
14418f002c6cSBruce M Simpson 
14428f002c6cSBruce M Simpson 	default:
14438f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: unknown sopt_name %d",
14448f002c6cSBruce M Simpson 		    __func__, sopt->sopt_name);
14458f002c6cSBruce M Simpson 		return (EOPNOTSUPP);
14468f002c6cSBruce M Simpson 		break;
14478f002c6cSBruce M Simpson 	}
14488f002c6cSBruce M Simpson 
14498f002c6cSBruce M Simpson 	if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
14508f002c6cSBruce M Simpson 		return (EINVAL);
14518f002c6cSBruce M Simpson 
145229dc7bc6SBruce M Simpson 	(void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
145329dc7bc6SBruce M Simpson 
14548f002c6cSBruce M Simpson 	/*
14558f002c6cSBruce M Simpson 	 * Check if we are actually a member of this group.
14568f002c6cSBruce M Simpson 	 */
14578f002c6cSBruce M Simpson 	imo = in6p_findmoptions(inp);
145859854ecfSHans Petter Selasky 	imf = im6o_match_group(imo, ifp, &gsa->sa);
145959854ecfSHans Petter Selasky 	if (imf == NULL) {
14608f002c6cSBruce M Simpson 		error = EADDRNOTAVAIL;
14618f002c6cSBruce M Simpson 		goto out_in6p_locked;
14628f002c6cSBruce M Simpson 	}
146359854ecfSHans Petter Selasky 	inm = imf->im6f_in6m;
14648f002c6cSBruce M Simpson 
14658f002c6cSBruce M Simpson 	/*
14668f002c6cSBruce M Simpson 	 * Attempting to use the delta-based API on an
14678f002c6cSBruce M Simpson 	 * non exclusive-mode membership is an error.
14688f002c6cSBruce M Simpson 	 */
14698f002c6cSBruce M Simpson 	fmode = imf->im6f_st[0];
14708f002c6cSBruce M Simpson 	if (fmode != MCAST_EXCLUDE) {
14718f002c6cSBruce M Simpson 		error = EINVAL;
14728f002c6cSBruce M Simpson 		goto out_in6p_locked;
14738f002c6cSBruce M Simpson 	}
14748f002c6cSBruce M Simpson 
14758f002c6cSBruce M Simpson 	/*
14768f002c6cSBruce M Simpson 	 * Deal with error cases up-front:
14778f002c6cSBruce M Simpson 	 *  Asked to block, but already blocked; or
14788f002c6cSBruce M Simpson 	 *  Asked to unblock, but nothing to unblock.
14798f002c6cSBruce M Simpson 	 * If adding a new block entry, allocate it.
14808f002c6cSBruce M Simpson 	 */
148159854ecfSHans Petter Selasky 	ims = im6o_match_source(imf, &ssa->sa);
14828f002c6cSBruce M Simpson 	if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
14838f002c6cSBruce M Simpson 		CTR3(KTR_MLD, "%s: source %s %spresent", __func__,
14848f002c6cSBruce M Simpson 		    ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
14858f002c6cSBruce M Simpson 		    doblock ? "" : "not ");
14868f002c6cSBruce M Simpson 		error = EADDRNOTAVAIL;
14878f002c6cSBruce M Simpson 		goto out_in6p_locked;
14888f002c6cSBruce M Simpson 	}
14898f002c6cSBruce M Simpson 
14908f002c6cSBruce M Simpson 	INP_WLOCK_ASSERT(inp);
14918f002c6cSBruce M Simpson 
14928f002c6cSBruce M Simpson 	/*
14938f002c6cSBruce M Simpson 	 * Begin state merge transaction at socket layer.
14948f002c6cSBruce M Simpson 	 */
14958f002c6cSBruce M Simpson 	if (doblock) {
14968f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: %s source", __func__, "block");
14978f002c6cSBruce M Simpson 		ims = im6f_graft(imf, fmode, &ssa->sin6);
14988f002c6cSBruce M Simpson 		if (ims == NULL)
14998f002c6cSBruce M Simpson 			error = ENOMEM;
15008f002c6cSBruce M Simpson 	} else {
15018f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
15028f002c6cSBruce M Simpson 		error = im6f_prune(imf, &ssa->sin6);
15038f002c6cSBruce M Simpson 	}
15048f002c6cSBruce M Simpson 
15058f002c6cSBruce M Simpson 	if (error) {
15068f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: merge imf state failed", __func__);
15078f002c6cSBruce M Simpson 		goto out_im6f_rollback;
15088f002c6cSBruce M Simpson 	}
15098f002c6cSBruce M Simpson 
15108f002c6cSBruce M Simpson 	/*
15118f002c6cSBruce M Simpson 	 * Begin state merge transaction at MLD layer.
15128f002c6cSBruce M Simpson 	 */
1513f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK();
15148f002c6cSBruce M Simpson 	CTR1(KTR_MLD, "%s: merge inm state", __func__);
15158f002c6cSBruce M Simpson 	error = in6m_merge(inm, imf);
15164fd91336SAndrey V. Elsukov 	if (error)
15178f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
15184fd91336SAndrey V. Elsukov 	else {
15198f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
15208f002c6cSBruce M Simpson 		error = mld_change_state(inm, 0);
15218f002c6cSBruce M Simpson 		if (error)
15228f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
15234fd91336SAndrey V. Elsukov 	}
15248f002c6cSBruce M Simpson 
1525f3e1324bSStephen Hurd 	IN6_MULTI_LIST_UNLOCK();
15268f002c6cSBruce M Simpson 
15278f002c6cSBruce M Simpson out_im6f_rollback:
15288f002c6cSBruce M Simpson 	if (error)
15298f002c6cSBruce M Simpson 		im6f_rollback(imf);
15308f002c6cSBruce M Simpson 	else
15318f002c6cSBruce M Simpson 		im6f_commit(imf);
15328f002c6cSBruce M Simpson 
15338f002c6cSBruce M Simpson 	im6f_reap(imf);
15348f002c6cSBruce M Simpson 
15358f002c6cSBruce M Simpson out_in6p_locked:
15368f002c6cSBruce M Simpson 	INP_WUNLOCK(inp);
15378f002c6cSBruce M Simpson 	return (error);
15388f002c6cSBruce M Simpson }
15398f002c6cSBruce M Simpson 
15408f002c6cSBruce M Simpson /*
15418f002c6cSBruce M Simpson  * Given an inpcb, return its multicast options structure pointer.  Accepts
15428f002c6cSBruce M Simpson  * an unlocked inpcb pointer, but will return it locked.  May sleep.
15438f002c6cSBruce M Simpson  *
15448f002c6cSBruce M Simpson  * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
15458f002c6cSBruce M Simpson  * SMPng: NOTE: Returns with the INP write lock held.
15468f002c6cSBruce M Simpson  */
15478f002c6cSBruce M Simpson static struct ip6_moptions *
15488f002c6cSBruce M Simpson in6p_findmoptions(struct inpcb *inp)
15498f002c6cSBruce M Simpson {
15508f002c6cSBruce M Simpson 	struct ip6_moptions	 *imo;
15518f002c6cSBruce M Simpson 
15528f002c6cSBruce M Simpson 	INP_WLOCK(inp);
15538f002c6cSBruce M Simpson 	if (inp->in6p_moptions != NULL)
15548f002c6cSBruce M Simpson 		return (inp->in6p_moptions);
15558f002c6cSBruce M Simpson 
15568f002c6cSBruce M Simpson 	INP_WUNLOCK(inp);
15578f002c6cSBruce M Simpson 
15588f002c6cSBruce M Simpson 	imo = malloc(sizeof(*imo), M_IP6MOPTS, M_WAITOK);
15598f002c6cSBruce M Simpson 
15608f002c6cSBruce M Simpson 	imo->im6o_multicast_ifp = NULL;
15618f002c6cSBruce M Simpson 	imo->im6o_multicast_hlim = V_ip6_defmcasthlim;
15628f002c6cSBruce M Simpson 	imo->im6o_multicast_loop = in6_mcast_loop;
156359854ecfSHans Petter Selasky 	STAILQ_INIT(&imo->im6o_head);
15648f002c6cSBruce M Simpson 
15658f002c6cSBruce M Simpson 	INP_WLOCK(inp);
15668f002c6cSBruce M Simpson 	if (inp->in6p_moptions != NULL) {
15678f002c6cSBruce M Simpson 		free(imo, M_IP6MOPTS);
15688f002c6cSBruce M Simpson 		return (inp->in6p_moptions);
15698f002c6cSBruce M Simpson 	}
15708f002c6cSBruce M Simpson 	inp->in6p_moptions = imo;
15718f002c6cSBruce M Simpson 	return (imo);
15728f002c6cSBruce M Simpson }
15738f002c6cSBruce M Simpson 
15748f002c6cSBruce M Simpson /*
15758f002c6cSBruce M Simpson  * Discard the IPv6 multicast options (and source filters).
15768f002c6cSBruce M Simpson  *
15778f002c6cSBruce M Simpson  * SMPng: NOTE: assumes INP write lock is held.
1578cb6bb230SMatt Macy  *
1579cb6bb230SMatt Macy  * XXX can all be safely deferred to epoch_call
1580cb6bb230SMatt Macy  *
15818f002c6cSBruce M Simpson  */
1582cb6bb230SMatt Macy 
1583cb6bb230SMatt Macy static void
1584f9be0386SMatt Macy inp_gcmoptions(struct ip6_moptions *imo)
15858f002c6cSBruce M Simpson {
15868f002c6cSBruce M Simpson 	struct in6_mfilter *imf;
1587b15253bbSMatt Macy 	struct in6_multi *inm;
1588b15253bbSMatt Macy 	struct ifnet *ifp;
15898f002c6cSBruce M Simpson 
159059854ecfSHans Petter Selasky 	while ((imf = ip6_mfilter_first(&imo->im6o_head)) != NULL) {
159159854ecfSHans Petter Selasky                 ip6_mfilter_remove(&imo->im6o_head, imf);
159259854ecfSHans Petter Selasky 
15938f002c6cSBruce M Simpson                 im6f_leave(imf);
159459854ecfSHans Petter Selasky                 if ((inm = imf->im6f_in6m) != NULL) {
159559854ecfSHans Petter Selasky                         if ((ifp = inm->in6m_ifp) != NULL) {
159615f8acc5SEd Maste                                 CURVNET_SET(ifp->if_vnet);
1597b15253bbSMatt Macy                                 (void)in6_leavegroup(inm, imf);
159815f8acc5SEd Maste                                 CURVNET_RESTORE();
15993b9b6b17SEd Maste                         } else {
16003b9b6b17SEd Maste                                 (void)in6_leavegroup(inm, imf);
16013b9b6b17SEd Maste                         }
16028f002c6cSBruce M Simpson                 }
160359854ecfSHans Petter Selasky                 ip6_mfilter_free(imf);
160459854ecfSHans Petter Selasky         }
16058f002c6cSBruce M Simpson         free(imo, M_IP6MOPTS);
1606cb6bb230SMatt Macy }
1607cb6bb230SMatt Macy 
1608cb6bb230SMatt Macy void
1609cb6bb230SMatt Macy ip6_freemoptions(struct ip6_moptions *imo)
1610cb6bb230SMatt Macy {
1611cb6bb230SMatt Macy 	if (imo == NULL)
1612cb6bb230SMatt Macy 		return;
1613f9be0386SMatt Macy 	inp_gcmoptions(imo);
16148f002c6cSBruce M Simpson }
16158f002c6cSBruce M Simpson 
16168f002c6cSBruce M Simpson /*
16178f002c6cSBruce M Simpson  * Atomically get source filters on a socket for an IPv6 multicast group.
16188f002c6cSBruce M Simpson  * Called with INP lock held; returns with lock released.
16198f002c6cSBruce M Simpson  */
16208f002c6cSBruce M Simpson static int
16218f002c6cSBruce M Simpson in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
16228f002c6cSBruce M Simpson {
16238f002c6cSBruce M Simpson 	struct __msfilterreq	 msfr;
16248f002c6cSBruce M Simpson 	sockunion_t		*gsa;
16258f002c6cSBruce M Simpson 	struct ifnet		*ifp;
16268f002c6cSBruce M Simpson 	struct ip6_moptions	*imo;
16278f002c6cSBruce M Simpson 	struct in6_mfilter	*imf;
16288f002c6cSBruce M Simpson 	struct ip6_msource	*ims;
16298f002c6cSBruce M Simpson 	struct in6_msource	*lims;
16308f002c6cSBruce M Simpson 	struct sockaddr_in6	*psin;
16318f002c6cSBruce M Simpson 	struct sockaddr_storage	*ptss;
16328f002c6cSBruce M Simpson 	struct sockaddr_storage	*tss;
16338f002c6cSBruce M Simpson 	int			 error;
163459854ecfSHans Petter Selasky 	size_t			 nsrcs, ncsrcs;
16358f002c6cSBruce M Simpson 
16368f002c6cSBruce M Simpson 	INP_WLOCK_ASSERT(inp);
16378f002c6cSBruce M Simpson 
16388f002c6cSBruce M Simpson 	imo = inp->in6p_moptions;
16398f002c6cSBruce M Simpson 	KASSERT(imo != NULL, ("%s: null ip6_moptions", __func__));
16408f002c6cSBruce M Simpson 
16418f002c6cSBruce M Simpson 	INP_WUNLOCK(inp);
16428f002c6cSBruce M Simpson 
16438f002c6cSBruce M Simpson 	error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
16448f002c6cSBruce M Simpson 	    sizeof(struct __msfilterreq));
16458f002c6cSBruce M Simpson 	if (error)
16468f002c6cSBruce M Simpson 		return (error);
16478f002c6cSBruce M Simpson 
164829dc7bc6SBruce M Simpson 	if (msfr.msfr_group.ss_family != AF_INET6 ||
164929dc7bc6SBruce M Simpson 	    msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
16508f002c6cSBruce M Simpson 		return (EINVAL);
16518f002c6cSBruce M Simpson 
165229dc7bc6SBruce M Simpson 	gsa = (sockunion_t *)&msfr.msfr_group;
165329dc7bc6SBruce M Simpson 	if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
165429dc7bc6SBruce M Simpson 		return (EINVAL);
165529dc7bc6SBruce M Simpson 
165629dc7bc6SBruce M Simpson 	if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
165729dc7bc6SBruce M Simpson 		return (EADDRNOTAVAIL);
16588f002c6cSBruce M Simpson 	ifp = ifnet_byindex(msfr.msfr_ifindex);
16598f002c6cSBruce M Simpson 	if (ifp == NULL)
166029dc7bc6SBruce M Simpson 		return (EADDRNOTAVAIL);
166129dc7bc6SBruce M Simpson 	(void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
16628f002c6cSBruce M Simpson 
16638f002c6cSBruce M Simpson 	INP_WLOCK(inp);
16648f002c6cSBruce M Simpson 
16658f002c6cSBruce M Simpson 	/*
16668f002c6cSBruce M Simpson 	 * Lookup group on the socket.
16678f002c6cSBruce M Simpson 	 */
166859854ecfSHans Petter Selasky 	imf = im6o_match_group(imo, ifp, &gsa->sa);
166959854ecfSHans Petter Selasky 	if (imf == NULL) {
16708f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
16718f002c6cSBruce M Simpson 		return (EADDRNOTAVAIL);
16728f002c6cSBruce M Simpson 	}
16738f002c6cSBruce M Simpson 
16748f002c6cSBruce M Simpson 	/*
16758f002c6cSBruce M Simpson 	 * Ignore memberships which are in limbo.
16768f002c6cSBruce M Simpson 	 */
16778f002c6cSBruce M Simpson 	if (imf->im6f_st[1] == MCAST_UNDEFINED) {
16788f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
16798f002c6cSBruce M Simpson 		return (EAGAIN);
16808f002c6cSBruce M Simpson 	}
16818f002c6cSBruce M Simpson 	msfr.msfr_fmode = imf->im6f_st[1];
16828f002c6cSBruce M Simpson 
16838f002c6cSBruce M Simpson 	/*
16848f002c6cSBruce M Simpson 	 * If the user specified a buffer, copy out the source filter
16858f002c6cSBruce M Simpson 	 * entries to userland gracefully.
16868f002c6cSBruce M Simpson 	 * We only copy out the number of entries which userland
16878f002c6cSBruce M Simpson 	 * has asked for, but we always tell userland how big the
16888f002c6cSBruce M Simpson 	 * buffer really needs to be.
16898f002c6cSBruce M Simpson 	 */
1690acde2476SXin LI 	if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
1691acde2476SXin LI 		msfr.msfr_nsrcs = in6_mcast_maxsocksrc;
16928f002c6cSBruce M Simpson 	tss = NULL;
16938f002c6cSBruce M Simpson 	if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
16948f002c6cSBruce M Simpson 		tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
16958f002c6cSBruce M Simpson 		    M_TEMP, M_NOWAIT | M_ZERO);
16968f002c6cSBruce M Simpson 		if (tss == NULL) {
16978f002c6cSBruce M Simpson 			INP_WUNLOCK(inp);
16988f002c6cSBruce M Simpson 			return (ENOBUFS);
16998f002c6cSBruce M Simpson 		}
17008f002c6cSBruce M Simpson 	}
17018f002c6cSBruce M Simpson 
17028f002c6cSBruce M Simpson 	/*
17038f002c6cSBruce M Simpson 	 * Count number of sources in-mode at t0.
17048f002c6cSBruce M Simpson 	 * If buffer space exists and remains, copy out source entries.
17058f002c6cSBruce M Simpson 	 */
17068f002c6cSBruce M Simpson 	nsrcs = msfr.msfr_nsrcs;
17078f002c6cSBruce M Simpson 	ncsrcs = 0;
17088f002c6cSBruce M Simpson 	ptss = tss;
17098f002c6cSBruce M Simpson 	RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
17108f002c6cSBruce M Simpson 		lims = (struct in6_msource *)ims;
17118f002c6cSBruce M Simpson 		if (lims->im6sl_st[0] == MCAST_UNDEFINED ||
17128f002c6cSBruce M Simpson 		    lims->im6sl_st[0] != imf->im6f_st[0])
17138f002c6cSBruce M Simpson 			continue;
17148f002c6cSBruce M Simpson 		++ncsrcs;
17158f002c6cSBruce M Simpson 		if (tss != NULL && nsrcs > 0) {
17168f002c6cSBruce M Simpson 			psin = (struct sockaddr_in6 *)ptss;
17178f002c6cSBruce M Simpson 			psin->sin6_family = AF_INET6;
17188f002c6cSBruce M Simpson 			psin->sin6_len = sizeof(struct sockaddr_in6);
17198f002c6cSBruce M Simpson 			psin->sin6_addr = lims->im6s_addr;
17208f002c6cSBruce M Simpson 			psin->sin6_port = 0;
17218f002c6cSBruce M Simpson 			--nsrcs;
17228f002c6cSBruce M Simpson 			++ptss;
17238f002c6cSBruce M Simpson 		}
17248f002c6cSBruce M Simpson 	}
17258f002c6cSBruce M Simpson 
17268f002c6cSBruce M Simpson 	INP_WUNLOCK(inp);
17278f002c6cSBruce M Simpson 
17288f002c6cSBruce M Simpson 	if (tss != NULL) {
17298f002c6cSBruce M Simpson 		error = copyout(tss, msfr.msfr_srcs,
17308f002c6cSBruce M Simpson 		    sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
17318f002c6cSBruce M Simpson 		free(tss, M_TEMP);
17328f002c6cSBruce M Simpson 		if (error)
17338f002c6cSBruce M Simpson 			return (error);
17348f002c6cSBruce M Simpson 	}
17358f002c6cSBruce M Simpson 
17368f002c6cSBruce M Simpson 	msfr.msfr_nsrcs = ncsrcs;
17378f002c6cSBruce M Simpson 	error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
17388f002c6cSBruce M Simpson 
17398f002c6cSBruce M Simpson 	return (error);
17408f002c6cSBruce M Simpson }
17418f002c6cSBruce M Simpson 
17428f002c6cSBruce M Simpson /*
17438f002c6cSBruce M Simpson  * Return the IP multicast options in response to user getsockopt().
17448f002c6cSBruce M Simpson  */
17458f002c6cSBruce M Simpson int
17468f002c6cSBruce M Simpson ip6_getmoptions(struct inpcb *inp, struct sockopt *sopt)
17478f002c6cSBruce M Simpson {
174833cde130SBruce M Simpson 	struct ip6_moptions	*im6o;
174933cde130SBruce M Simpson 	int			 error;
175033cde130SBruce M Simpson 	u_int			 optval;
17518f002c6cSBruce M Simpson 
17528f002c6cSBruce M Simpson 	INP_WLOCK(inp);
175333cde130SBruce M Simpson 	im6o = inp->in6p_moptions;
17548f002c6cSBruce M Simpson 	/*
17558f002c6cSBruce M Simpson 	 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
17568f002c6cSBruce M Simpson 	 * or is a divert socket, reject it.
17578f002c6cSBruce M Simpson 	 */
17588f002c6cSBruce M Simpson 	if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
17598f002c6cSBruce M Simpson 	    (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
17608f002c6cSBruce M Simpson 	    inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
17618f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
17628f002c6cSBruce M Simpson 		return (EOPNOTSUPP);
17638f002c6cSBruce M Simpson 	}
17648f002c6cSBruce M Simpson 
17658f002c6cSBruce M Simpson 	error = 0;
17668f002c6cSBruce M Simpson 	switch (sopt->sopt_name) {
17678f002c6cSBruce M Simpson 	case IPV6_MULTICAST_IF:
176833cde130SBruce M Simpson 		if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) {
17698f002c6cSBruce M Simpson 			optval = 0;
17708f002c6cSBruce M Simpson 		} else {
177133cde130SBruce M Simpson 			optval = im6o->im6o_multicast_ifp->if_index;
17728f002c6cSBruce M Simpson 		}
17738f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
177433cde130SBruce M Simpson 		error = sooptcopyout(sopt, &optval, sizeof(u_int));
17758f002c6cSBruce M Simpson 		break;
17768f002c6cSBruce M Simpson 
17778f002c6cSBruce M Simpson 	case IPV6_MULTICAST_HOPS:
177833cde130SBruce M Simpson 		if (im6o == NULL)
177933cde130SBruce M Simpson 			optval = V_ip6_defmcasthlim;
17808f002c6cSBruce M Simpson 		else
178108907004SBjoern A. Zeeb 			optval = im6o->im6o_multicast_hlim;
17828f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
17838f002c6cSBruce M Simpson 		error = sooptcopyout(sopt, &optval, sizeof(u_int));
17848f002c6cSBruce M Simpson 		break;
17858f002c6cSBruce M Simpson 
17868f002c6cSBruce M Simpson 	case IPV6_MULTICAST_LOOP:
178733cde130SBruce M Simpson 		if (im6o == NULL)
178833cde130SBruce M Simpson 			optval = in6_mcast_loop; /* XXX VIMAGE */
17898f002c6cSBruce M Simpson 		else
179033cde130SBruce M Simpson 			optval = im6o->im6o_multicast_loop;
17918f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
17928f002c6cSBruce M Simpson 		error = sooptcopyout(sopt, &optval, sizeof(u_int));
17938f002c6cSBruce M Simpson 		break;
17948f002c6cSBruce M Simpson 
17958f002c6cSBruce M Simpson 	case IPV6_MSFILTER:
179633cde130SBruce M Simpson 		if (im6o == NULL) {
17978f002c6cSBruce M Simpson 			error = EADDRNOTAVAIL;
17988f002c6cSBruce M Simpson 			INP_WUNLOCK(inp);
17998f002c6cSBruce M Simpson 		} else {
18008f002c6cSBruce M Simpson 			error = in6p_get_source_filters(inp, sopt);
18018f002c6cSBruce M Simpson 		}
18028f002c6cSBruce M Simpson 		break;
18038f002c6cSBruce M Simpson 
18048f002c6cSBruce M Simpson 	default:
18058f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
18068f002c6cSBruce M Simpson 		error = ENOPROTOOPT;
18078f002c6cSBruce M Simpson 		break;
18088f002c6cSBruce M Simpson 	}
18098f002c6cSBruce M Simpson 
18108f002c6cSBruce M Simpson 	INP_UNLOCK_ASSERT(inp);
18118f002c6cSBruce M Simpson 
18128f002c6cSBruce M Simpson 	return (error);
18138f002c6cSBruce M Simpson }
18148f002c6cSBruce M Simpson 
18158f002c6cSBruce M Simpson /*
181633cde130SBruce M Simpson  * Look up the ifnet to use for a multicast group membership,
181733cde130SBruce M Simpson  * given the address of an IPv6 group.
181833cde130SBruce M Simpson  *
181933cde130SBruce M Simpson  * This routine exists to support legacy IPv6 multicast applications.
182033cde130SBruce M Simpson  *
1821cfae6a92SMark Johnston  * Use the socket's current FIB number for any required FIB lookup. Look up the
1822cfae6a92SMark Johnston  * group address in the unicast FIB, and use its ifp; usually, this points to
1823cfae6a92SMark Johnston  * the default next-hop.  If the FIB lookup fails, return NULL.
182433cde130SBruce M Simpson  *
182533cde130SBruce M Simpson  * FUTURE: Support multiple forwarding tables for IPv6.
182633cde130SBruce M Simpson  *
182733cde130SBruce M Simpson  * Returns NULL if no ifp could be found.
182833cde130SBruce M Simpson  */
182933cde130SBruce M Simpson static struct ifnet *
1830cfae6a92SMark Johnston in6p_lookup_mcast_ifp(const struct inpcb *inp, const struct sockaddr_in6 *gsin6)
183133cde130SBruce M Simpson {
18326ad7446cSAlexander V. Chernikov 	struct nhop_object	*nh;
18339977be4aSAlexander V. Chernikov 	struct in6_addr		dst;
18349977be4aSAlexander V. Chernikov 	uint32_t		scopeid;
18359977be4aSAlexander V. Chernikov 	uint32_t		fibnum;
183633cde130SBruce M Simpson 
183733cde130SBruce M Simpson 	KASSERT(gsin6->sin6_family == AF_INET6,
183833cde130SBruce M Simpson 	    ("%s: not AF_INET6 group", __func__));
183933cde130SBruce M Simpson 
18409977be4aSAlexander V. Chernikov 	in6_splitscope(&gsin6->sin6_addr, &dst, &scopeid);
1841cfae6a92SMark Johnston 	fibnum = inp->inp_inc.inc_fibnum;
18426ad7446cSAlexander V. Chernikov 	nh = fib6_lookup(fibnum, &dst, scopeid, 0, 0);
184333cde130SBruce M Simpson 
18446ad7446cSAlexander V. Chernikov 	return (nh ? nh->nh_ifp : NULL);
184533cde130SBruce M Simpson }
184633cde130SBruce M Simpson 
184733cde130SBruce M Simpson /*
18488f002c6cSBruce M Simpson  * Join an IPv6 multicast group, possibly with a source.
184933cde130SBruce M Simpson  *
185033cde130SBruce M Simpson  * FIXME: The KAME use of the unspecified address (::)
185133cde130SBruce M Simpson  * to join *all* multicast groups is currently unsupported.
18528f002c6cSBruce M Simpson  */
18538f002c6cSBruce M Simpson static int
18548f002c6cSBruce M Simpson in6p_join_group(struct inpcb *inp, struct sockopt *sopt)
18558f002c6cSBruce M Simpson {
18562cd6ad76SHans Petter Selasky 	struct in6_multi_head		 inmh;
18578f002c6cSBruce M Simpson 	struct group_source_req		 gsr;
18588f002c6cSBruce M Simpson 	sockunion_t			*gsa, *ssa;
18598f002c6cSBruce M Simpson 	struct ifnet			*ifp;
18608f002c6cSBruce M Simpson 	struct in6_mfilter		*imf;
18618f002c6cSBruce M Simpson 	struct ip6_moptions		*imo;
18628f002c6cSBruce M Simpson 	struct in6_multi		*inm;
18638f002c6cSBruce M Simpson 	struct in6_msource		*lims;
18648f002c6cSBruce M Simpson 	int				 error, is_new;
18658f002c6cSBruce M Simpson 
18662cd6ad76SHans Petter Selasky 	SLIST_INIT(&inmh);
18678f002c6cSBruce M Simpson 	ifp = NULL;
18681f81c2b6SBruce M Simpson 	lims = NULL;
18698f002c6cSBruce M Simpson 	error = 0;
18708f002c6cSBruce M Simpson 
18718f002c6cSBruce M Simpson 	memset(&gsr, 0, sizeof(struct group_source_req));
18728f002c6cSBruce M Simpson 	gsa = (sockunion_t *)&gsr.gsr_group;
18738f002c6cSBruce M Simpson 	gsa->ss.ss_family = AF_UNSPEC;
18748f002c6cSBruce M Simpson 	ssa = (sockunion_t *)&gsr.gsr_source;
18758f002c6cSBruce M Simpson 	ssa->ss.ss_family = AF_UNSPEC;
18768f002c6cSBruce M Simpson 
187729dc7bc6SBruce M Simpson 	/*
187829dc7bc6SBruce M Simpson 	 * Chew everything into struct group_source_req.
187929dc7bc6SBruce M Simpson 	 * Overwrite the port field if present, as the sockaddr
188029dc7bc6SBruce M Simpson 	 * being copied in may be matched with a binary comparison.
188129dc7bc6SBruce M Simpson 	 * Ignore passed-in scope ID.
188229dc7bc6SBruce M Simpson 	 */
18838f002c6cSBruce M Simpson 	switch (sopt->sopt_name) {
18848f002c6cSBruce M Simpson 	case IPV6_JOIN_GROUP: {
18858f002c6cSBruce M Simpson 		struct ipv6_mreq mreq;
18868f002c6cSBruce M Simpson 
18878f002c6cSBruce M Simpson 		error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
18888f002c6cSBruce M Simpson 		    sizeof(struct ipv6_mreq));
18898f002c6cSBruce M Simpson 		if (error)
18908f002c6cSBruce M Simpson 			return (error);
18918f002c6cSBruce M Simpson 
18928f002c6cSBruce M Simpson 		gsa->sin6.sin6_family = AF_INET6;
18938f002c6cSBruce M Simpson 		gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
18948f002c6cSBruce M Simpson 		gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
18958f002c6cSBruce M Simpson 
189633cde130SBruce M Simpson 		if (mreq.ipv6mr_interface == 0) {
189733cde130SBruce M Simpson 			ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
189833cde130SBruce M Simpson 		} else {
1899010c2b81SBjoern A. Zeeb 			if (V_if_index < mreq.ipv6mr_interface)
190033cde130SBruce M Simpson 				return (EADDRNOTAVAIL);
19018f002c6cSBruce M Simpson 			ifp = ifnet_byindex(mreq.ipv6mr_interface);
190233cde130SBruce M Simpson 		}
19038f002c6cSBruce M Simpson 		CTR3(KTR_MLD, "%s: ipv6mr_interface = %d, ifp = %p",
19048f002c6cSBruce M Simpson 		    __func__, mreq.ipv6mr_interface, ifp);
19058f002c6cSBruce M Simpson 	} break;
19068f002c6cSBruce M Simpson 
19078f002c6cSBruce M Simpson 	case MCAST_JOIN_GROUP:
19088f002c6cSBruce M Simpson 	case MCAST_JOIN_SOURCE_GROUP:
19098f002c6cSBruce M Simpson 		if (sopt->sopt_name == MCAST_JOIN_GROUP) {
19108f002c6cSBruce M Simpson 			error = sooptcopyin(sopt, &gsr,
19118f002c6cSBruce M Simpson 			    sizeof(struct group_req),
19128f002c6cSBruce M Simpson 			    sizeof(struct group_req));
19138f002c6cSBruce M Simpson 		} else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
19148f002c6cSBruce M Simpson 			error = sooptcopyin(sopt, &gsr,
19158f002c6cSBruce M Simpson 			    sizeof(struct group_source_req),
19168f002c6cSBruce M Simpson 			    sizeof(struct group_source_req));
19178f002c6cSBruce M Simpson 		}
19188f002c6cSBruce M Simpson 		if (error)
19198f002c6cSBruce M Simpson 			return (error);
19208f002c6cSBruce M Simpson 
19218f002c6cSBruce M Simpson 		if (gsa->sin6.sin6_family != AF_INET6 ||
19228f002c6cSBruce M Simpson 		    gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
19238f002c6cSBruce M Simpson 			return (EINVAL);
19248f002c6cSBruce M Simpson 
19258f002c6cSBruce M Simpson 		if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
19268f002c6cSBruce M Simpson 			if (ssa->sin6.sin6_family != AF_INET6 ||
19278f002c6cSBruce M Simpson 			    ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
19288f002c6cSBruce M Simpson 				return (EINVAL);
192929dc7bc6SBruce M Simpson 			if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
193029dc7bc6SBruce M Simpson 				return (EINVAL);
193129dc7bc6SBruce M Simpson 			/*
193229dc7bc6SBruce M Simpson 			 * TODO: Validate embedded scope ID in source
193329dc7bc6SBruce M Simpson 			 * list entry against passed-in ifp, if and only
193429dc7bc6SBruce M Simpson 			 * if source list filter entry is iface or node local.
193529dc7bc6SBruce M Simpson 			 */
193629dc7bc6SBruce M Simpson 			in6_clearscope(&ssa->sin6.sin6_addr);
19378f002c6cSBruce M Simpson 			ssa->sin6.sin6_port = 0;
193829dc7bc6SBruce M Simpson 			ssa->sin6.sin6_scope_id = 0;
19398f002c6cSBruce M Simpson 		}
19408f002c6cSBruce M Simpson 
19418f002c6cSBruce M Simpson 		if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
19428f002c6cSBruce M Simpson 			return (EADDRNOTAVAIL);
19438f002c6cSBruce M Simpson 		ifp = ifnet_byindex(gsr.gsr_interface);
19448f002c6cSBruce M Simpson 		break;
19458f002c6cSBruce M Simpson 
19468f002c6cSBruce M Simpson 	default:
19478f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: unknown sopt_name %d",
19488f002c6cSBruce M Simpson 		    __func__, sopt->sopt_name);
19498f002c6cSBruce M Simpson 		return (EOPNOTSUPP);
19508f002c6cSBruce M Simpson 		break;
19518f002c6cSBruce M Simpson 	}
19528f002c6cSBruce M Simpson 
19538f002c6cSBruce M Simpson 	if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
19548f002c6cSBruce M Simpson 		return (EINVAL);
19558f002c6cSBruce M Simpson 
19568f002c6cSBruce M Simpson 	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
19578f002c6cSBruce M Simpson 		return (EADDRNOTAVAIL);
19588f002c6cSBruce M Simpson 
195929dc7bc6SBruce M Simpson 	gsa->sin6.sin6_port = 0;
196029dc7bc6SBruce M Simpson 	gsa->sin6.sin6_scope_id = 0;
196129dc7bc6SBruce M Simpson 
196233cde130SBruce M Simpson 	/*
196329dc7bc6SBruce M Simpson 	 * Always set the scope zone ID on memberships created from userland.
196429dc7bc6SBruce M Simpson 	 * Use the passed-in ifp to do this.
196529dc7bc6SBruce M Simpson 	 * XXX The in6_setscope() return value is meaningless.
196629dc7bc6SBruce M Simpson 	 * XXX SCOPE6_LOCK() is taken by in6_setscope().
196733cde130SBruce M Simpson 	 */
196829dc7bc6SBruce M Simpson 	(void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
196933cde130SBruce M Simpson 
197059854ecfSHans Petter Selasky 	IN6_MULTI_LOCK();
197159854ecfSHans Petter Selasky 
197259854ecfSHans Petter Selasky 	/*
197359854ecfSHans Petter Selasky 	 * Find the membership in the membership list.
197459854ecfSHans Petter Selasky 	 */
19758f002c6cSBruce M Simpson 	imo = in6p_findmoptions(inp);
197659854ecfSHans Petter Selasky 	imf = im6o_match_group(imo, ifp, &gsa->sa);
197759854ecfSHans Petter Selasky 	if (imf == NULL) {
19788f002c6cSBruce M Simpson 		is_new = 1;
197959854ecfSHans Petter Selasky 		inm = NULL;
198059854ecfSHans Petter Selasky 
198159854ecfSHans Petter Selasky 		if (ip6_mfilter_count(&imo->im6o_head) >= IPV6_MAX_MEMBERSHIPS) {
198259854ecfSHans Petter Selasky 			error = ENOMEM;
198359854ecfSHans Petter Selasky 			goto out_in6p_locked;
198459854ecfSHans Petter Selasky 		}
19858f002c6cSBruce M Simpson 	} else {
198659854ecfSHans Petter Selasky 		is_new = 0;
198759854ecfSHans Petter Selasky 		inm = imf->im6f_in6m;
198859854ecfSHans Petter Selasky 
19891ee6b058SBruce M Simpson 		if (ssa->ss.ss_family != AF_UNSPEC) {
19901ee6b058SBruce M Simpson 			/*
19911ee6b058SBruce M Simpson 			 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
19921ee6b058SBruce M Simpson 			 * is an error. On an existing inclusive membership,
19931ee6b058SBruce M Simpson 			 * it just adds the source to the filter list.
19941ee6b058SBruce M Simpson 			 */
19951ee6b058SBruce M Simpson 			if (imf->im6f_st[1] != MCAST_INCLUDE) {
19968f002c6cSBruce M Simpson 				error = EINVAL;
19978f002c6cSBruce M Simpson 				goto out_in6p_locked;
19988f002c6cSBruce M Simpson 			}
19991f81c2b6SBruce M Simpson 			/*
20001f81c2b6SBruce M Simpson 			 * Throw out duplicates.
20011f81c2b6SBruce M Simpson 			 *
20021f81c2b6SBruce M Simpson 			 * XXX FIXME: This makes a naive assumption that
20031f81c2b6SBruce M Simpson 			 * even if entries exist for *ssa in this imf,
20041f81c2b6SBruce M Simpson 			 * they will be rejected as dupes, even if they
20051f81c2b6SBruce M Simpson 			 * are not valid in the current mode (in-mode).
20061f81c2b6SBruce M Simpson 			 *
20071f81c2b6SBruce M Simpson 			 * in6_msource is transactioned just as for anything
20081f81c2b6SBruce M Simpson 			 * else in SSM -- but note naive use of in6m_graft()
20091f81c2b6SBruce M Simpson 			 * below for allocating new filter entries.
20101f81c2b6SBruce M Simpson 			 *
20111f81c2b6SBruce M Simpson 			 * This is only an issue if someone mixes the
20121f81c2b6SBruce M Simpson 			 * full-state SSM API with the delta-based API,
20131f81c2b6SBruce M Simpson 			 * which is discouraged in the relevant RFCs.
20141f81c2b6SBruce M Simpson 			 */
201559854ecfSHans Petter Selasky 			lims = im6o_match_source(imf, &ssa->sa);
20161f81c2b6SBruce M Simpson 			if (lims != NULL /*&&
20171f81c2b6SBruce M Simpson 			    lims->im6sl_st[1] == MCAST_INCLUDE*/) {
20188f002c6cSBruce M Simpson 				error = EADDRNOTAVAIL;
20198f002c6cSBruce M Simpson 				goto out_in6p_locked;
20208f002c6cSBruce M Simpson 			}
20211ee6b058SBruce M Simpson 		} else {
20221ee6b058SBruce M Simpson 			/*
2023604a60d1SBruce M Simpson 			 * MCAST_JOIN_GROUP alone, on any existing membership,
2024604a60d1SBruce M Simpson 			 * is rejected, to stop the same inpcb tying up
2025604a60d1SBruce M Simpson 			 * multiple refs to the in_multi.
2026604a60d1SBruce M Simpson 			 * On an existing inclusive membership, this is also
2027604a60d1SBruce M Simpson 			 * an error; if you want to change filter mode,
2028604a60d1SBruce M Simpson 			 * you must use the userland API setsourcefilter().
2029604a60d1SBruce M Simpson 			 * XXX We don't reject this for imf in UNDEFINED
2030604a60d1SBruce M Simpson 			 * state at t1, because allocation of a filter
2031604a60d1SBruce M Simpson 			 * is atomic with allocation of a membership.
20321ee6b058SBruce M Simpson 			 */
20332ef5d803SKristof Provost 			error = EADDRINUSE;
20341ee6b058SBruce M Simpson 			goto out_in6p_locked;
20351ee6b058SBruce M Simpson 		}
20361ee6b058SBruce M Simpson 	}
20378f002c6cSBruce M Simpson 
20388f002c6cSBruce M Simpson 	/*
20398f002c6cSBruce M Simpson 	 * Begin state merge transaction at socket layer.
20408f002c6cSBruce M Simpson 	 */
20418f002c6cSBruce M Simpson 	INP_WLOCK_ASSERT(inp);
20428f002c6cSBruce M Simpson 
20438f002c6cSBruce M Simpson 	/*
20448f002c6cSBruce M Simpson 	 * Graft new source into filter list for this inpcb's
20458f002c6cSBruce M Simpson 	 * membership of the group. The in6_multi may not have
20461ee6b058SBruce M Simpson 	 * been allocated yet if this is a new membership, however,
20471ee6b058SBruce M Simpson 	 * the in_mfilter slot will be allocated and must be initialized.
2048604a60d1SBruce M Simpson 	 *
2049604a60d1SBruce M Simpson 	 * Note: Grafting of exclusive mode filters doesn't happen
2050604a60d1SBruce M Simpson 	 * in this path.
20511f81c2b6SBruce M Simpson 	 * XXX: Should check for non-NULL lims (node exists but may
20521f81c2b6SBruce M Simpson 	 * not be in-mode) for interop with full-state API.
20538f002c6cSBruce M Simpson 	 */
20548f002c6cSBruce M Simpson 	if (ssa->ss.ss_family != AF_UNSPEC) {
20558f002c6cSBruce M Simpson 		/* Membership starts in IN mode */
20568f002c6cSBruce M Simpson 		if (is_new) {
20578f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: new join w/source", __func__);
205859854ecfSHans Petter Selasky 			imf = ip6_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_INCLUDE);
205959854ecfSHans Petter Selasky 			if (imf == NULL) {
206059854ecfSHans Petter Selasky 				error = ENOMEM;
206159854ecfSHans Petter Selasky 				goto out_in6p_locked;
206259854ecfSHans Petter Selasky 			}
20638f002c6cSBruce M Simpson 		} else {
20648f002c6cSBruce M Simpson 			CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
20658f002c6cSBruce M Simpson 		}
20668f002c6cSBruce M Simpson 		lims = im6f_graft(imf, MCAST_INCLUDE, &ssa->sin6);
20678f002c6cSBruce M Simpson 		if (lims == NULL) {
20688f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: merge imf state failed",
20698f002c6cSBruce M Simpson 			    __func__);
20708f002c6cSBruce M Simpson 			error = ENOMEM;
207159854ecfSHans Petter Selasky 			goto out_in6p_locked;
20728f002c6cSBruce M Simpson 		}
20731ee6b058SBruce M Simpson 	} else {
20741ee6b058SBruce M Simpson 		/* No address specified; Membership starts in EX mode */
20751ee6b058SBruce M Simpson 		if (is_new) {
20761ee6b058SBruce M Simpson 			CTR1(KTR_MLD, "%s: new join w/o source", __func__);
207759854ecfSHans Petter Selasky 			imf = ip6_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_EXCLUDE);
207859854ecfSHans Petter Selasky 			if (imf == NULL) {
207959854ecfSHans Petter Selasky 				error = ENOMEM;
208059854ecfSHans Petter Selasky 				goto out_in6p_locked;
208159854ecfSHans Petter Selasky 			}
20821ee6b058SBruce M Simpson 		}
20838f002c6cSBruce M Simpson 	}
20848f002c6cSBruce M Simpson 
20858f002c6cSBruce M Simpson 	/*
20868f002c6cSBruce M Simpson 	 * Begin state merge transaction at MLD layer.
20878f002c6cSBruce M Simpson 	 */
208859854ecfSHans Petter Selasky 	if (is_new) {
2089f00876fbSMark Johnston 		in_pcbref(inp);
2090f00876fbSMark Johnston 		INP_WUNLOCK(inp);
2091f00876fbSMark Johnston 
2092f3e1324bSStephen Hurd 		error = in6_joingroup_locked(ifp, &gsa->sin6.sin6_addr, imf,
209359854ecfSHans Petter Selasky 		    &imf->im6f_in6m, 0);
209459854ecfSHans Petter Selasky 
209559854ecfSHans Petter Selasky 		INP_WLOCK(inp);
209659854ecfSHans Petter Selasky 		if (in_pcbrele_wlocked(inp)) {
209759854ecfSHans Petter Selasky 			error = ENXIO;
209859854ecfSHans Petter Selasky 			goto out_in6p_unlocked;
209959854ecfSHans Petter Selasky 		}
21004fd91336SAndrey V. Elsukov 		if (error) {
210159854ecfSHans Petter Selasky 			goto out_in6p_locked;
21024fd91336SAndrey V. Elsukov 		}
2103130f575dSHans Petter Selasky 		/*
2104130f575dSHans Petter Selasky 		 * NOTE: Refcount from in6_joingroup_locked()
2105130f575dSHans Petter Selasky 		 * is protecting membership.
2106130f575dSHans Petter Selasky 		 */
21075b64b824SHans Petter Selasky 		ip6_mfilter_insert(&imo->im6o_head, imf);
21088f002c6cSBruce M Simpson 	} else {
21098f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: merge inm state", __func__);
2110f3e1324bSStephen Hurd 		IN6_MULTI_LIST_LOCK();
21118f002c6cSBruce M Simpson 		error = in6m_merge(inm, imf);
211259854ecfSHans Petter Selasky 		if (error) {
21138f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: failed to merge inm state",
21148f002c6cSBruce M Simpson 			    __func__);
211559854ecfSHans Petter Selasky 			IN6_MULTI_LIST_UNLOCK();
211659854ecfSHans Petter Selasky 			im6f_rollback(imf);
211759854ecfSHans Petter Selasky 			im6f_reap(imf);
211859854ecfSHans Petter Selasky 			goto out_in6p_locked;
211959854ecfSHans Petter Selasky 		}
21208f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
21218f002c6cSBruce M Simpson 		error = mld_change_state(inm, 0);
212259854ecfSHans Petter Selasky 		IN6_MULTI_LIST_UNLOCK();
212359854ecfSHans Petter Selasky 
212459854ecfSHans Petter Selasky 		if (error) {
21258f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: failed mld downcall",
21268f002c6cSBruce M Simpson 			     __func__);
21278f002c6cSBruce M Simpson 			im6f_rollback(imf);
21288f002c6cSBruce M Simpson 			im6f_reap(imf);
212959854ecfSHans Petter Selasky 			goto out_in6p_locked;
213059854ecfSHans Petter Selasky 		}
21318f002c6cSBruce M Simpson 	}
21328f002c6cSBruce M Simpson 
213359854ecfSHans Petter Selasky 	im6f_commit(imf);
213459854ecfSHans Petter Selasky 	imf = NULL;
21358f002c6cSBruce M Simpson 
21368f002c6cSBruce M Simpson out_in6p_locked:
21378f002c6cSBruce M Simpson 	INP_WUNLOCK(inp);
213859854ecfSHans Petter Selasky out_in6p_unlocked:
213959854ecfSHans Petter Selasky 	IN6_MULTI_UNLOCK();
214059854ecfSHans Petter Selasky 
214159854ecfSHans Petter Selasky 	if (is_new && imf) {
214259854ecfSHans Petter Selasky 		if (imf->im6f_in6m != NULL) {
214359854ecfSHans Petter Selasky 			struct in6_multi_head inmh;
214459854ecfSHans Petter Selasky 
214559854ecfSHans Petter Selasky 			SLIST_INIT(&inmh);
214659854ecfSHans Petter Selasky 			SLIST_INSERT_HEAD(&inmh, imf->im6f_in6m, in6m_defer);
21472cd6ad76SHans Petter Selasky 			in6m_release_list_deferred(&inmh);
214859854ecfSHans Petter Selasky 		}
214959854ecfSHans Petter Selasky 		ip6_mfilter_free(imf);
215059854ecfSHans Petter Selasky 	}
21518f002c6cSBruce M Simpson 	return (error);
21528f002c6cSBruce M Simpson }
21538f002c6cSBruce M Simpson 
21548f002c6cSBruce M Simpson /*
21558f002c6cSBruce M Simpson  * Leave an IPv6 multicast group on an inpcb, possibly with a source.
21568f002c6cSBruce M Simpson  */
21578f002c6cSBruce M Simpson static int
21588f002c6cSBruce M Simpson in6p_leave_group(struct inpcb *inp, struct sockopt *sopt)
21598f002c6cSBruce M Simpson {
216029dc7bc6SBruce M Simpson 	struct ipv6_mreq		 mreq;
21618f002c6cSBruce M Simpson 	struct group_source_req		 gsr;
21628f002c6cSBruce M Simpson 	sockunion_t			*gsa, *ssa;
21638f002c6cSBruce M Simpson 	struct ifnet			*ifp;
21648f002c6cSBruce M Simpson 	struct in6_mfilter		*imf;
21658f002c6cSBruce M Simpson 	struct ip6_moptions		*imo;
21668f002c6cSBruce M Simpson 	struct in6_msource		*ims;
21678f002c6cSBruce M Simpson 	struct in6_multi		*inm;
216829dc7bc6SBruce M Simpson 	uint32_t			 ifindex;
216959854ecfSHans Petter Selasky 	int				 error;
217059854ecfSHans Petter Selasky 	bool				 is_final;
21718f002c6cSBruce M Simpson #ifdef KTR
21728f002c6cSBruce M Simpson 	char				 ip6tbuf[INET6_ADDRSTRLEN];
21738f002c6cSBruce M Simpson #endif
21748f002c6cSBruce M Simpson 
21758f002c6cSBruce M Simpson 	ifp = NULL;
217629dc7bc6SBruce M Simpson 	ifindex = 0;
21778f002c6cSBruce M Simpson 	error = 0;
217859854ecfSHans Petter Selasky 	is_final = true;
21798f002c6cSBruce M Simpson 
21808f002c6cSBruce M Simpson 	memset(&gsr, 0, sizeof(struct group_source_req));
21818f002c6cSBruce M Simpson 	gsa = (sockunion_t *)&gsr.gsr_group;
21828f002c6cSBruce M Simpson 	gsa->ss.ss_family = AF_UNSPEC;
21838f002c6cSBruce M Simpson 	ssa = (sockunion_t *)&gsr.gsr_source;
21848f002c6cSBruce M Simpson 	ssa->ss.ss_family = AF_UNSPEC;
21858f002c6cSBruce M Simpson 
218629dc7bc6SBruce M Simpson 	/*
218729dc7bc6SBruce M Simpson 	 * Chew everything passed in up into a struct group_source_req
218829dc7bc6SBruce M Simpson 	 * as that is easier to process.
218929dc7bc6SBruce M Simpson 	 * Note: Any embedded scope ID in the multicast group passed
219029dc7bc6SBruce M Simpson 	 * in by userland is ignored, the interface index is the recommended
219129dc7bc6SBruce M Simpson 	 * mechanism to specify an interface; see below.
219229dc7bc6SBruce M Simpson 	 */
21938f002c6cSBruce M Simpson 	switch (sopt->sopt_name) {
219429dc7bc6SBruce M Simpson 	case IPV6_LEAVE_GROUP:
21958f002c6cSBruce M Simpson 		error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
21968f002c6cSBruce M Simpson 		    sizeof(struct ipv6_mreq));
21978f002c6cSBruce M Simpson 		if (error)
21988f002c6cSBruce M Simpson 			return (error);
21998f002c6cSBruce M Simpson 		gsa->sin6.sin6_family = AF_INET6;
22008f002c6cSBruce M Simpson 		gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
22018f002c6cSBruce M Simpson 		gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
220229dc7bc6SBruce M Simpson 		gsa->sin6.sin6_port = 0;
220329dc7bc6SBruce M Simpson 		gsa->sin6.sin6_scope_id = 0;
220429dc7bc6SBruce M Simpson 		ifindex = mreq.ipv6mr_interface;
220529dc7bc6SBruce M Simpson 		break;
22068f002c6cSBruce M Simpson 
22078f002c6cSBruce M Simpson 	case MCAST_LEAVE_GROUP:
22088f002c6cSBruce M Simpson 	case MCAST_LEAVE_SOURCE_GROUP:
22098f002c6cSBruce M Simpson 		if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
22108f002c6cSBruce M Simpson 			error = sooptcopyin(sopt, &gsr,
22118f002c6cSBruce M Simpson 			    sizeof(struct group_req),
22128f002c6cSBruce M Simpson 			    sizeof(struct group_req));
22138f002c6cSBruce M Simpson 		} else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
22148f002c6cSBruce M Simpson 			error = sooptcopyin(sopt, &gsr,
22158f002c6cSBruce M Simpson 			    sizeof(struct group_source_req),
22168f002c6cSBruce M Simpson 			    sizeof(struct group_source_req));
22178f002c6cSBruce M Simpson 		}
22188f002c6cSBruce M Simpson 		if (error)
22198f002c6cSBruce M Simpson 			return (error);
22208f002c6cSBruce M Simpson 
22218f002c6cSBruce M Simpson 		if (gsa->sin6.sin6_family != AF_INET6 ||
22228f002c6cSBruce M Simpson 		    gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
22238f002c6cSBruce M Simpson 			return (EINVAL);
22248f002c6cSBruce M Simpson 		if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
22258f002c6cSBruce M Simpson 			if (ssa->sin6.sin6_family != AF_INET6 ||
22268f002c6cSBruce M Simpson 			    ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
22278f002c6cSBruce M Simpson 				return (EINVAL);
222829dc7bc6SBruce M Simpson 			if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
222929dc7bc6SBruce M Simpson 				return (EINVAL);
223029dc7bc6SBruce M Simpson 			/*
223129dc7bc6SBruce M Simpson 			 * TODO: Validate embedded scope ID in source
223229dc7bc6SBruce M Simpson 			 * list entry against passed-in ifp, if and only
223329dc7bc6SBruce M Simpson 			 * if source list filter entry is iface or node local.
223429dc7bc6SBruce M Simpson 			 */
223529dc7bc6SBruce M Simpson 			in6_clearscope(&ssa->sin6.sin6_addr);
22368f002c6cSBruce M Simpson 		}
223729dc7bc6SBruce M Simpson 		gsa->sin6.sin6_port = 0;
223829dc7bc6SBruce M Simpson 		gsa->sin6.sin6_scope_id = 0;
223929dc7bc6SBruce M Simpson 		ifindex = gsr.gsr_interface;
22408f002c6cSBruce M Simpson 		break;
22418f002c6cSBruce M Simpson 
22428f002c6cSBruce M Simpson 	default:
22438f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: unknown sopt_name %d",
22448f002c6cSBruce M Simpson 		    __func__, sopt->sopt_name);
22458f002c6cSBruce M Simpson 		return (EOPNOTSUPP);
22468f002c6cSBruce M Simpson 		break;
22478f002c6cSBruce M Simpson 	}
22488f002c6cSBruce M Simpson 
22498f002c6cSBruce M Simpson 	if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
22508f002c6cSBruce M Simpson 		return (EINVAL);
22518f002c6cSBruce M Simpson 
225233cde130SBruce M Simpson 	/*
225329dc7bc6SBruce M Simpson 	 * Validate interface index if provided. If no interface index
225429dc7bc6SBruce M Simpson 	 * was provided separately, attempt to look the membership up
225529dc7bc6SBruce M Simpson 	 * from the default scope as a last resort to disambiguate
225629dc7bc6SBruce M Simpson 	 * the membership we are being asked to leave.
225729dc7bc6SBruce M Simpson 	 * XXX SCOPE6 lock potentially taken here.
225833cde130SBruce M Simpson 	 */
225929dc7bc6SBruce M Simpson 	if (ifindex != 0) {
2260010c2b81SBjoern A. Zeeb 		if (V_if_index < ifindex)
226129dc7bc6SBruce M Simpson 			return (EADDRNOTAVAIL);
226229dc7bc6SBruce M Simpson 		ifp = ifnet_byindex(ifindex);
226329dc7bc6SBruce M Simpson 		if (ifp == NULL)
226429dc7bc6SBruce M Simpson 			return (EADDRNOTAVAIL);
226529dc7bc6SBruce M Simpson 		(void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
226629dc7bc6SBruce M Simpson 	} else {
226729dc7bc6SBruce M Simpson 		error = sa6_embedscope(&gsa->sin6, V_ip6_use_defzone);
226829dc7bc6SBruce M Simpson 		if (error)
226929dc7bc6SBruce M Simpson 			return (EADDRNOTAVAIL);
227029dc7bc6SBruce M Simpson 		/*
2271b36c89e5SBruce M Simpson 		 * Some badly behaved applications don't pass an ifindex
2272b36c89e5SBruce M Simpson 		 * or a scope ID, which is an API violation. In this case,
2273b36c89e5SBruce M Simpson 		 * perform a lookup as per a v6 join.
2274b36c89e5SBruce M Simpson 		 *
227529dc7bc6SBruce M Simpson 		 * XXX For now, stomp on zone ID for the corner case.
227629dc7bc6SBruce M Simpson 		 * This is not the 'KAME way', but we need to see the ifp
227729dc7bc6SBruce M Simpson 		 * directly until such time as this implementation is
227829dc7bc6SBruce M Simpson 		 * refactored, assuming the scope IDs are the way to go.
227929dc7bc6SBruce M Simpson 		 */
228029dc7bc6SBruce M Simpson 		ifindex = ntohs(gsa->sin6.sin6_addr.s6_addr16[1]);
2281b36c89e5SBruce M Simpson 		if (ifindex == 0) {
2282b36c89e5SBruce M Simpson 			CTR2(KTR_MLD, "%s: warning: no ifindex, looking up "
2283b36c89e5SBruce M Simpson 			    "ifp for group %s.", __func__,
2284b36c89e5SBruce M Simpson 			    ip6_sprintf(ip6tbuf, &gsa->sin6.sin6_addr));
2285b36c89e5SBruce M Simpson 			ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
2286b36c89e5SBruce M Simpson 		} else {
228729dc7bc6SBruce M Simpson 			ifp = ifnet_byindex(ifindex);
2288b36c89e5SBruce M Simpson 		}
228929dc7bc6SBruce M Simpson 		if (ifp == NULL)
229029dc7bc6SBruce M Simpson 			return (EADDRNOTAVAIL);
229129dc7bc6SBruce M Simpson 	}
229229dc7bc6SBruce M Simpson 
229329dc7bc6SBruce M Simpson 	CTR2(KTR_MLD, "%s: ifp = %p", __func__, ifp);
229429dc7bc6SBruce M Simpson 	KASSERT(ifp != NULL, ("%s: ifp did not resolve", __func__));
229533cde130SBruce M Simpson 
229659854ecfSHans Petter Selasky 	IN6_MULTI_LOCK();
229759854ecfSHans Petter Selasky 
22988f002c6cSBruce M Simpson 	/*
229959854ecfSHans Petter Selasky 	 * Find the membership in the membership list.
23008f002c6cSBruce M Simpson 	 */
23018f002c6cSBruce M Simpson 	imo = in6p_findmoptions(inp);
230259854ecfSHans Petter Selasky 	imf = im6o_match_group(imo, ifp, &gsa->sa);
230359854ecfSHans Petter Selasky 	if (imf == NULL) {
23048f002c6cSBruce M Simpson 		error = EADDRNOTAVAIL;
23058f002c6cSBruce M Simpson 		goto out_in6p_locked;
23068f002c6cSBruce M Simpson 	}
230759854ecfSHans Petter Selasky 	inm = imf->im6f_in6m;
23088f002c6cSBruce M Simpson 
23098f002c6cSBruce M Simpson 	if (ssa->ss.ss_family != AF_UNSPEC)
231059854ecfSHans Petter Selasky 		is_final = false;
23118f002c6cSBruce M Simpson 
23128f002c6cSBruce M Simpson 	/*
23138f002c6cSBruce M Simpson 	 * Begin state merge transaction at socket layer.
23148f002c6cSBruce M Simpson 	 */
23158f002c6cSBruce M Simpson 	INP_WLOCK_ASSERT(inp);
23168f002c6cSBruce M Simpson 
23178f002c6cSBruce M Simpson 	/*
23188f002c6cSBruce M Simpson 	 * If we were instructed only to leave a given source, do so.
23198f002c6cSBruce M Simpson 	 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
23208f002c6cSBruce M Simpson 	 */
23218f002c6cSBruce M Simpson 	if (is_final) {
232259854ecfSHans Petter Selasky 		ip6_mfilter_remove(&imo->im6o_head, imf);
23238f002c6cSBruce M Simpson 		im6f_leave(imf);
2324a4c5668dSHans Petter Selasky 
2325a4c5668dSHans Petter Selasky 		/*
2326a4c5668dSHans Petter Selasky 		 * Give up the multicast address record to which
2327a4c5668dSHans Petter Selasky 		 * the membership points.
2328a4c5668dSHans Petter Selasky 		 */
2329a4c5668dSHans Petter Selasky 		(void)in6_leavegroup_locked(inm, imf);
23308f002c6cSBruce M Simpson 	} else {
23318f002c6cSBruce M Simpson 		if (imf->im6f_st[0] == MCAST_EXCLUDE) {
23328f002c6cSBruce M Simpson 			error = EADDRNOTAVAIL;
23338f002c6cSBruce M Simpson 			goto out_in6p_locked;
23348f002c6cSBruce M Simpson 		}
233559854ecfSHans Petter Selasky 		ims = im6o_match_source(imf, &ssa->sa);
23368f002c6cSBruce M Simpson 		if (ims == NULL) {
23378f002c6cSBruce M Simpson 			CTR3(KTR_MLD, "%s: source %p %spresent", __func__,
23388f002c6cSBruce M Simpson 			    ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
23398f002c6cSBruce M Simpson 			    "not ");
23408f002c6cSBruce M Simpson 			error = EADDRNOTAVAIL;
23418f002c6cSBruce M Simpson 			goto out_in6p_locked;
23428f002c6cSBruce M Simpson 		}
23438f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: %s source", __func__, "block");
23448f002c6cSBruce M Simpson 		error = im6f_prune(imf, &ssa->sin6);
23458f002c6cSBruce M Simpson 		if (error) {
23468f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: merge imf state failed",
23478f002c6cSBruce M Simpson 			    __func__);
23488f002c6cSBruce M Simpson 			goto out_in6p_locked;
23498f002c6cSBruce M Simpson 		}
23508f002c6cSBruce M Simpson 	}
23518f002c6cSBruce M Simpson 
23528f002c6cSBruce M Simpson 	/*
23538f002c6cSBruce M Simpson 	 * Begin state merge transaction at MLD layer.
23548f002c6cSBruce M Simpson 	 */
235559854ecfSHans Petter Selasky 	if (!is_final) {
235659854ecfSHans Petter Selasky 		CTR1(KTR_MLD, "%s: merge inm state", __func__);
235759854ecfSHans Petter Selasky 		IN6_MULTI_LIST_LOCK();
235859854ecfSHans Petter Selasky 		error = in6m_merge(inm, imf);
235959854ecfSHans Petter Selasky 		if (error) {
236059854ecfSHans Petter Selasky 			CTR1(KTR_MLD, "%s: failed to merge inm state",
236159854ecfSHans Petter Selasky 			    __func__);
236259854ecfSHans Petter Selasky 			IN6_MULTI_LIST_UNLOCK();
236359854ecfSHans Petter Selasky 			im6f_rollback(imf);
236459854ecfSHans Petter Selasky 			im6f_reap(imf);
236559854ecfSHans Petter Selasky                         goto out_in6p_locked;
236659854ecfSHans Petter Selasky 		}
2367f00876fbSMark Johnston 
236859854ecfSHans Petter Selasky 		CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
236959854ecfSHans Petter Selasky 		error = mld_change_state(inm, 0);
237059854ecfSHans Petter Selasky 		IN6_MULTI_LIST_UNLOCK();
237159854ecfSHans Petter Selasky 		if (error) {
237259854ecfSHans Petter Selasky 			CTR1(KTR_MLD, "%s: failed mld downcall",
237359854ecfSHans Petter Selasky 			     __func__);
237459854ecfSHans Petter Selasky 			im6f_rollback(imf);
237559854ecfSHans Petter Selasky 			im6f_reap(imf);
237659854ecfSHans Petter Selasky                         goto out_in6p_locked;
237759854ecfSHans Petter Selasky 		}
237859854ecfSHans Petter Selasky 	}
237959854ecfSHans Petter Selasky 
238059854ecfSHans Petter Selasky 	im6f_commit(imf);
238159854ecfSHans Petter Selasky 	im6f_reap(imf);
238259854ecfSHans Petter Selasky 
238359854ecfSHans Petter Selasky out_in6p_locked:
238459854ecfSHans Petter Selasky 	INP_WUNLOCK(inp);
238559854ecfSHans Petter Selasky 
2386a4c5668dSHans Petter Selasky 	if (is_final && imf)
238759854ecfSHans Petter Selasky 		ip6_mfilter_free(imf);
23888f002c6cSBruce M Simpson 
2389f00876fbSMark Johnston 	IN6_MULTI_UNLOCK();
23908f002c6cSBruce M Simpson 	return (error);
23918f002c6cSBruce M Simpson }
23928f002c6cSBruce M Simpson 
23938f002c6cSBruce M Simpson /*
23948f002c6cSBruce M Simpson  * Select the interface for transmitting IPv6 multicast datagrams.
23958f002c6cSBruce M Simpson  *
23968f002c6cSBruce M Simpson  * Either an instance of struct in6_addr or an instance of struct ipv6_mreqn
23978f002c6cSBruce M Simpson  * may be passed to this socket option. An address of in6addr_any or an
23988f002c6cSBruce M Simpson  * interface index of 0 is used to remove a previous selection.
23998f002c6cSBruce M Simpson  * When no interface is selected, one is chosen for every send.
24008f002c6cSBruce M Simpson  */
24018f002c6cSBruce M Simpson static int
24028f002c6cSBruce M Simpson in6p_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
24038f002c6cSBruce M Simpson {
24048f002c6cSBruce M Simpson 	struct ifnet		*ifp;
24058f002c6cSBruce M Simpson 	struct ip6_moptions	*imo;
24068f002c6cSBruce M Simpson 	u_int			 ifindex;
24078f002c6cSBruce M Simpson 	int			 error;
24088f002c6cSBruce M Simpson 
24098f002c6cSBruce M Simpson 	if (sopt->sopt_valsize != sizeof(u_int))
24108f002c6cSBruce M Simpson 		return (EINVAL);
24118f002c6cSBruce M Simpson 
24128f002c6cSBruce M Simpson 	error = sooptcopyin(sopt, &ifindex, sizeof(u_int), sizeof(u_int));
24138f002c6cSBruce M Simpson 	if (error)
24148f002c6cSBruce M Simpson 		return (error);
2415010c2b81SBjoern A. Zeeb 	if (V_if_index < ifindex)
24168f002c6cSBruce M Simpson 		return (EINVAL);
2417e2956804SAndrey V. Elsukov 	if (ifindex == 0)
2418e2956804SAndrey V. Elsukov 		ifp = NULL;
2419e2956804SAndrey V. Elsukov 	else {
24208f002c6cSBruce M Simpson 		ifp = ifnet_byindex(ifindex);
2421e2956804SAndrey V. Elsukov 		if (ifp == NULL)
2422e2956804SAndrey V. Elsukov 			return (EINVAL);
2423e2956804SAndrey V. Elsukov 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
24248f002c6cSBruce M Simpson 			return (EADDRNOTAVAIL);
2425e2956804SAndrey V. Elsukov 	}
24268f002c6cSBruce M Simpson 	imo = in6p_findmoptions(inp);
24278f002c6cSBruce M Simpson 	imo->im6o_multicast_ifp = ifp;
24288f002c6cSBruce M Simpson 	INP_WUNLOCK(inp);
24298f002c6cSBruce M Simpson 
24308f002c6cSBruce M Simpson 	return (0);
24318f002c6cSBruce M Simpson }
24328f002c6cSBruce M Simpson 
24338f002c6cSBruce M Simpson /*
24348f002c6cSBruce M Simpson  * Atomically set source filters on a socket for an IPv6 multicast group.
2435f00876fbSMark Johnston  *
2436f00876fbSMark Johnston  * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
24378f002c6cSBruce M Simpson  */
24388f002c6cSBruce M Simpson static int
24398f002c6cSBruce M Simpson in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
24408f002c6cSBruce M Simpson {
24418f002c6cSBruce M Simpson 	struct __msfilterreq	 msfr;
24428f002c6cSBruce M Simpson 	sockunion_t		*gsa;
24438f002c6cSBruce M Simpson 	struct ifnet		*ifp;
24448f002c6cSBruce M Simpson 	struct in6_mfilter	*imf;
24458f002c6cSBruce M Simpson 	struct ip6_moptions	*imo;
24468f002c6cSBruce M Simpson 	struct in6_multi		*inm;
24478f002c6cSBruce M Simpson 	int			 error;
24488f002c6cSBruce M Simpson 
24498f002c6cSBruce M Simpson 	error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
24508f002c6cSBruce M Simpson 	    sizeof(struct __msfilterreq));
24518f002c6cSBruce M Simpson 	if (error)
24528f002c6cSBruce M Simpson 		return (error);
24538f002c6cSBruce M Simpson 
24540dc5893eSBruce M Simpson 	if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
24550dc5893eSBruce M Simpson 		return (ENOBUFS);
24560dc5893eSBruce M Simpson 
24570dc5893eSBruce M Simpson 	if (msfr.msfr_fmode != MCAST_EXCLUDE &&
24580dc5893eSBruce M Simpson 	    msfr.msfr_fmode != MCAST_INCLUDE)
24598f002c6cSBruce M Simpson 		return (EINVAL);
24608f002c6cSBruce M Simpson 
24618f002c6cSBruce M Simpson 	if (msfr.msfr_group.ss_family != AF_INET6 ||
24628f002c6cSBruce M Simpson 	    msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
24638f002c6cSBruce M Simpson 		return (EINVAL);
24648f002c6cSBruce M Simpson 
24658f002c6cSBruce M Simpson 	gsa = (sockunion_t *)&msfr.msfr_group;
24668f002c6cSBruce M Simpson 	if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
24678f002c6cSBruce M Simpson 		return (EINVAL);
24688f002c6cSBruce M Simpson 
24698f002c6cSBruce M Simpson 	gsa->sin6.sin6_port = 0;	/* ignore port */
24708f002c6cSBruce M Simpson 
24718f002c6cSBruce M Simpson 	if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
24728f002c6cSBruce M Simpson 		return (EADDRNOTAVAIL);
24738f002c6cSBruce M Simpson 	ifp = ifnet_byindex(msfr.msfr_ifindex);
24748f002c6cSBruce M Simpson 	if (ifp == NULL)
24758f002c6cSBruce M Simpson 		return (EADDRNOTAVAIL);
247629dc7bc6SBruce M Simpson 	(void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
24778f002c6cSBruce M Simpson 
24788f002c6cSBruce M Simpson 	/*
24798f002c6cSBruce M Simpson 	 * Take the INP write lock.
24808f002c6cSBruce M Simpson 	 * Check if this socket is a member of this group.
24818f002c6cSBruce M Simpson 	 */
24828f002c6cSBruce M Simpson 	imo = in6p_findmoptions(inp);
248359854ecfSHans Petter Selasky 	imf = im6o_match_group(imo, ifp, &gsa->sa);
248459854ecfSHans Petter Selasky 	if (imf == NULL) {
24858f002c6cSBruce M Simpson 		error = EADDRNOTAVAIL;
24868f002c6cSBruce M Simpson 		goto out_in6p_locked;
24878f002c6cSBruce M Simpson 	}
248859854ecfSHans Petter Selasky 	inm = imf->im6f_in6m;
24898f002c6cSBruce M Simpson 
24908f002c6cSBruce M Simpson 	/*
24918f002c6cSBruce M Simpson 	 * Begin state merge transaction at socket layer.
24928f002c6cSBruce M Simpson 	 */
24938f002c6cSBruce M Simpson 	INP_WLOCK_ASSERT(inp);
24948f002c6cSBruce M Simpson 
24958f002c6cSBruce M Simpson 	imf->im6f_st[1] = msfr.msfr_fmode;
24968f002c6cSBruce M Simpson 
24978f002c6cSBruce M Simpson 	/*
24988f002c6cSBruce M Simpson 	 * Apply any new source filters, if present.
24998f002c6cSBruce M Simpson 	 * Make a copy of the user-space source vector so
25008f002c6cSBruce M Simpson 	 * that we may copy them with a single copyin. This
25018f002c6cSBruce M Simpson 	 * allows us to deal with page faults up-front.
25028f002c6cSBruce M Simpson 	 */
25038f002c6cSBruce M Simpson 	if (msfr.msfr_nsrcs > 0) {
25048f002c6cSBruce M Simpson 		struct in6_msource	*lims;
25058f002c6cSBruce M Simpson 		struct sockaddr_in6	*psin;
25068f002c6cSBruce M Simpson 		struct sockaddr_storage	*kss, *pkss;
25078f002c6cSBruce M Simpson 		int			 i;
25088f002c6cSBruce M Simpson 
25098f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
25108f002c6cSBruce M Simpson 
25118f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: loading %lu source list entries",
25128f002c6cSBruce M Simpson 		    __func__, (unsigned long)msfr.msfr_nsrcs);
25138f002c6cSBruce M Simpson 		kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
25148f002c6cSBruce M Simpson 		    M_TEMP, M_WAITOK);
25158f002c6cSBruce M Simpson 		error = copyin(msfr.msfr_srcs, kss,
25168f002c6cSBruce M Simpson 		    sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
25178f002c6cSBruce M Simpson 		if (error) {
25188f002c6cSBruce M Simpson 			free(kss, M_TEMP);
25198f002c6cSBruce M Simpson 			return (error);
25208f002c6cSBruce M Simpson 		}
25218f002c6cSBruce M Simpson 
25228f002c6cSBruce M Simpson 		INP_WLOCK(inp);
25238f002c6cSBruce M Simpson 
25248f002c6cSBruce M Simpson 		/*
25258f002c6cSBruce M Simpson 		 * Mark all source filters as UNDEFINED at t1.
25268f002c6cSBruce M Simpson 		 * Restore new group filter mode, as im6f_leave()
25278f002c6cSBruce M Simpson 		 * will set it to INCLUDE.
25288f002c6cSBruce M Simpson 		 */
25298f002c6cSBruce M Simpson 		im6f_leave(imf);
25308f002c6cSBruce M Simpson 		imf->im6f_st[1] = msfr.msfr_fmode;
25318f002c6cSBruce M Simpson 
25328f002c6cSBruce M Simpson 		/*
25338f002c6cSBruce M Simpson 		 * Update socket layer filters at t1, lazy-allocating
25348f002c6cSBruce M Simpson 		 * new entries. This saves a bunch of memory at the
25358f002c6cSBruce M Simpson 		 * cost of one RB_FIND() per source entry; duplicate
25368f002c6cSBruce M Simpson 		 * entries in the msfr_nsrcs vector are ignored.
25378f002c6cSBruce M Simpson 		 * If we encounter an error, rollback transaction.
25388f002c6cSBruce M Simpson 		 *
25398f002c6cSBruce M Simpson 		 * XXX This too could be replaced with a set-symmetric
25408f002c6cSBruce M Simpson 		 * difference like loop to avoid walking from root
25418f002c6cSBruce M Simpson 		 * every time, as the key space is common.
25428f002c6cSBruce M Simpson 		 */
25438f002c6cSBruce M Simpson 		for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
25448f002c6cSBruce M Simpson 			psin = (struct sockaddr_in6 *)pkss;
25458f002c6cSBruce M Simpson 			if (psin->sin6_family != AF_INET6) {
25468f002c6cSBruce M Simpson 				error = EAFNOSUPPORT;
25478f002c6cSBruce M Simpson 				break;
25488f002c6cSBruce M Simpson 			}
25498f002c6cSBruce M Simpson 			if (psin->sin6_len != sizeof(struct sockaddr_in6)) {
25508f002c6cSBruce M Simpson 				error = EINVAL;
25518f002c6cSBruce M Simpson 				break;
25528f002c6cSBruce M Simpson 			}
255329dc7bc6SBruce M Simpson 			if (IN6_IS_ADDR_MULTICAST(&psin->sin6_addr)) {
255429dc7bc6SBruce M Simpson 				error = EINVAL;
255529dc7bc6SBruce M Simpson 				break;
255629dc7bc6SBruce M Simpson 			}
255729dc7bc6SBruce M Simpson 			/*
255829dc7bc6SBruce M Simpson 			 * TODO: Validate embedded scope ID in source
255929dc7bc6SBruce M Simpson 			 * list entry against passed-in ifp, if and only
256029dc7bc6SBruce M Simpson 			 * if source list filter entry is iface or node local.
256129dc7bc6SBruce M Simpson 			 */
256229dc7bc6SBruce M Simpson 			in6_clearscope(&psin->sin6_addr);
25638f002c6cSBruce M Simpson 			error = im6f_get_source(imf, psin, &lims);
25648f002c6cSBruce M Simpson 			if (error)
25658f002c6cSBruce M Simpson 				break;
25668f002c6cSBruce M Simpson 			lims->im6sl_st[1] = imf->im6f_st[1];
25678f002c6cSBruce M Simpson 		}
25688f002c6cSBruce M Simpson 		free(kss, M_TEMP);
25698f002c6cSBruce M Simpson 	}
25708f002c6cSBruce M Simpson 
25718f002c6cSBruce M Simpson 	if (error)
25728f002c6cSBruce M Simpson 		goto out_im6f_rollback;
25738f002c6cSBruce M Simpson 
25748f002c6cSBruce M Simpson 	INP_WLOCK_ASSERT(inp);
2575f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK();
25768f002c6cSBruce M Simpson 
25778f002c6cSBruce M Simpson 	/*
25788f002c6cSBruce M Simpson 	 * Begin state merge transaction at MLD layer.
25798f002c6cSBruce M Simpson 	 */
25808f002c6cSBruce M Simpson 	CTR1(KTR_MLD, "%s: merge inm state", __func__);
25818f002c6cSBruce M Simpson 	error = in6m_merge(inm, imf);
25824fd91336SAndrey V. Elsukov 	if (error)
25838f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
25844fd91336SAndrey V. Elsukov 	else {
25858f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
25868f002c6cSBruce M Simpson 		error = mld_change_state(inm, 0);
25878f002c6cSBruce M Simpson 		if (error)
25888f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
25894fd91336SAndrey V. Elsukov 	}
25908f002c6cSBruce M Simpson 
2591f3e1324bSStephen Hurd 	IN6_MULTI_LIST_UNLOCK();
25928f002c6cSBruce M Simpson 
25938f002c6cSBruce M Simpson out_im6f_rollback:
25948f002c6cSBruce M Simpson 	if (error)
25958f002c6cSBruce M Simpson 		im6f_rollback(imf);
25968f002c6cSBruce M Simpson 	else
25978f002c6cSBruce M Simpson 		im6f_commit(imf);
25988f002c6cSBruce M Simpson 
25998f002c6cSBruce M Simpson 	im6f_reap(imf);
26008f002c6cSBruce M Simpson 
26018f002c6cSBruce M Simpson out_in6p_locked:
26028f002c6cSBruce M Simpson 	INP_WUNLOCK(inp);
26038f002c6cSBruce M Simpson 	return (error);
26048f002c6cSBruce M Simpson }
26058f002c6cSBruce M Simpson 
26068f002c6cSBruce M Simpson /*
26078f002c6cSBruce M Simpson  * Set the IP multicast options in response to user setsockopt().
26088f002c6cSBruce M Simpson  *
26098f002c6cSBruce M Simpson  * Many of the socket options handled in this function duplicate the
26108f002c6cSBruce M Simpson  * functionality of socket options in the regular unicast API. However,
26118f002c6cSBruce M Simpson  * it is not possible to merge the duplicate code, because the idempotence
26128f002c6cSBruce M Simpson  * of the IPv6 multicast part of the BSD Sockets API must be preserved;
26138f002c6cSBruce M Simpson  * the effects of these options must be treated as separate and distinct.
26148f002c6cSBruce M Simpson  *
26158f002c6cSBruce M Simpson  * SMPng: XXX: Unlocked read of inp_socket believed OK.
26168f002c6cSBruce M Simpson  */
26178f002c6cSBruce M Simpson int
26188f002c6cSBruce M Simpson ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt)
26198f002c6cSBruce M Simpson {
262033cde130SBruce M Simpson 	struct ip6_moptions	*im6o;
26218f002c6cSBruce M Simpson 	int			 error;
26228f002c6cSBruce M Simpson 
26238f002c6cSBruce M Simpson 	error = 0;
26248f002c6cSBruce M Simpson 
26258f002c6cSBruce M Simpson 	/*
26268f002c6cSBruce M Simpson 	 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
26278f002c6cSBruce M Simpson 	 * or is a divert socket, reject it.
26288f002c6cSBruce M Simpson 	 */
26298f002c6cSBruce M Simpson 	if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
26308f002c6cSBruce M Simpson 	    (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
26318f002c6cSBruce M Simpson 	     inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
26328f002c6cSBruce M Simpson 		return (EOPNOTSUPP);
26338f002c6cSBruce M Simpson 
26348f002c6cSBruce M Simpson 	switch (sopt->sopt_name) {
26358f002c6cSBruce M Simpson 	case IPV6_MULTICAST_IF:
26368f002c6cSBruce M Simpson 		error = in6p_set_multicast_if(inp, sopt);
26378f002c6cSBruce M Simpson 		break;
26388f002c6cSBruce M Simpson 
26398f002c6cSBruce M Simpson 	case IPV6_MULTICAST_HOPS: {
26408f002c6cSBruce M Simpson 		int hlim;
26418f002c6cSBruce M Simpson 
26428f002c6cSBruce M Simpson 		if (sopt->sopt_valsize != sizeof(int)) {
26438f002c6cSBruce M Simpson 			error = EINVAL;
26448f002c6cSBruce M Simpson 			break;
26458f002c6cSBruce M Simpson 		}
26468f002c6cSBruce M Simpson 		error = sooptcopyin(sopt, &hlim, sizeof(hlim), sizeof(int));
26478f002c6cSBruce M Simpson 		if (error)
26488f002c6cSBruce M Simpson 			break;
26498f002c6cSBruce M Simpson 		if (hlim < -1 || hlim > 255) {
26508f002c6cSBruce M Simpson 			error = EINVAL;
26518f002c6cSBruce M Simpson 			break;
265233cde130SBruce M Simpson 		} else if (hlim == -1) {
265333cde130SBruce M Simpson 			hlim = V_ip6_defmcasthlim;
26548f002c6cSBruce M Simpson 		}
265533cde130SBruce M Simpson 		im6o = in6p_findmoptions(inp);
265633cde130SBruce M Simpson 		im6o->im6o_multicast_hlim = hlim;
26578f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
26588f002c6cSBruce M Simpson 		break;
26598f002c6cSBruce M Simpson 	}
26608f002c6cSBruce M Simpson 
26618f002c6cSBruce M Simpson 	case IPV6_MULTICAST_LOOP: {
26628f002c6cSBruce M Simpson 		u_int loop;
26638f002c6cSBruce M Simpson 
26648f002c6cSBruce M Simpson 		/*
26658f002c6cSBruce M Simpson 		 * Set the loopback flag for outgoing multicast packets.
266633cde130SBruce M Simpson 		 * Must be zero or one.
26678f002c6cSBruce M Simpson 		 */
26688f002c6cSBruce M Simpson 		if (sopt->sopt_valsize != sizeof(u_int)) {
26698f002c6cSBruce M Simpson 			error = EINVAL;
26708f002c6cSBruce M Simpson 			break;
26718f002c6cSBruce M Simpson 		}
26728f002c6cSBruce M Simpson 		error = sooptcopyin(sopt, &loop, sizeof(u_int), sizeof(u_int));
26738f002c6cSBruce M Simpson 		if (error)
26748f002c6cSBruce M Simpson 			break;
267533cde130SBruce M Simpson 		if (loop > 1) {
267633cde130SBruce M Simpson 			error = EINVAL;
267733cde130SBruce M Simpson 			break;
267833cde130SBruce M Simpson 		}
267933cde130SBruce M Simpson 		im6o = in6p_findmoptions(inp);
268033cde130SBruce M Simpson 		im6o->im6o_multicast_loop = loop;
26818f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
26828f002c6cSBruce M Simpson 		break;
26838f002c6cSBruce M Simpson 	}
26848f002c6cSBruce M Simpson 
26858f002c6cSBruce M Simpson 	case IPV6_JOIN_GROUP:
26868f002c6cSBruce M Simpson 	case MCAST_JOIN_GROUP:
26878f002c6cSBruce M Simpson 	case MCAST_JOIN_SOURCE_GROUP:
26888f002c6cSBruce M Simpson 		error = in6p_join_group(inp, sopt);
26898f002c6cSBruce M Simpson 		break;
26908f002c6cSBruce M Simpson 
26918f002c6cSBruce M Simpson 	case IPV6_LEAVE_GROUP:
26928f002c6cSBruce M Simpson 	case MCAST_LEAVE_GROUP:
26938f002c6cSBruce M Simpson 	case MCAST_LEAVE_SOURCE_GROUP:
26948f002c6cSBruce M Simpson 		error = in6p_leave_group(inp, sopt);
26958f002c6cSBruce M Simpson 		break;
26968f002c6cSBruce M Simpson 
26978f002c6cSBruce M Simpson 	case MCAST_BLOCK_SOURCE:
26988f002c6cSBruce M Simpson 	case MCAST_UNBLOCK_SOURCE:
26998f002c6cSBruce M Simpson 		error = in6p_block_unblock_source(inp, sopt);
27008f002c6cSBruce M Simpson 		break;
27018f002c6cSBruce M Simpson 
27028f002c6cSBruce M Simpson 	case IPV6_MSFILTER:
27038f002c6cSBruce M Simpson 		error = in6p_set_source_filters(inp, sopt);
27048f002c6cSBruce M Simpson 		break;
27058f002c6cSBruce M Simpson 
27068f002c6cSBruce M Simpson 	default:
27078f002c6cSBruce M Simpson 		error = EOPNOTSUPP;
27088f002c6cSBruce M Simpson 		break;
27098f002c6cSBruce M Simpson 	}
27108f002c6cSBruce M Simpson 
27118f002c6cSBruce M Simpson 	INP_UNLOCK_ASSERT(inp);
27128f002c6cSBruce M Simpson 
27138f002c6cSBruce M Simpson 	return (error);
27148f002c6cSBruce M Simpson }
27158f002c6cSBruce M Simpson 
27168f002c6cSBruce M Simpson /*
27178f002c6cSBruce M Simpson  * Expose MLD's multicast filter mode and source list(s) to userland,
27188f002c6cSBruce M Simpson  * keyed by (ifindex, group).
27198f002c6cSBruce M Simpson  * The filter mode is written out as a uint32_t, followed by
27208f002c6cSBruce M Simpson  * 0..n of struct in6_addr.
27218f002c6cSBruce M Simpson  * For use by ifmcstat(8).
27228f002c6cSBruce M Simpson  * SMPng: NOTE: unlocked read of ifindex space.
27238f002c6cSBruce M Simpson  */
27248f002c6cSBruce M Simpson static int
27258f002c6cSBruce M Simpson sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS)
27268f002c6cSBruce M Simpson {
272729dc7bc6SBruce M Simpson 	struct in6_addr			 mcaddr;
27288f002c6cSBruce M Simpson 	struct in6_addr			 src;
2729a68cc388SGleb Smirnoff 	struct epoch_tracker		 et;
27308f002c6cSBruce M Simpson 	struct ifnet			*ifp;
27318f002c6cSBruce M Simpson 	struct ifmultiaddr		*ifma;
27328f002c6cSBruce M Simpson 	struct in6_multi		*inm;
27338f002c6cSBruce M Simpson 	struct ip6_msource		*ims;
27348f002c6cSBruce M Simpson 	int				*name;
27358f002c6cSBruce M Simpson 	int				 retval;
27368f002c6cSBruce M Simpson 	u_int				 namelen;
27378f002c6cSBruce M Simpson 	uint32_t			 fmode, ifindex;
27388f002c6cSBruce M Simpson #ifdef KTR
27398f002c6cSBruce M Simpson 	char				 ip6tbuf[INET6_ADDRSTRLEN];
27408f002c6cSBruce M Simpson #endif
27418f002c6cSBruce M Simpson 
27428f002c6cSBruce M Simpson 	name = (int *)arg1;
27438f002c6cSBruce M Simpson 	namelen = arg2;
27448f002c6cSBruce M Simpson 
27458f002c6cSBruce M Simpson 	if (req->newptr != NULL)
27468f002c6cSBruce M Simpson 		return (EPERM);
27478f002c6cSBruce M Simpson 
27488f002c6cSBruce M Simpson 	/* int: ifindex + 4 * 32 bits of IPv6 address */
27498f002c6cSBruce M Simpson 	if (namelen != 5)
27508f002c6cSBruce M Simpson 		return (EINVAL);
27518f002c6cSBruce M Simpson 
27528f002c6cSBruce M Simpson 	ifindex = name[0];
27538f002c6cSBruce M Simpson 	if (ifindex <= 0 || ifindex > V_if_index) {
27548f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: ifindex %u out of range",
27558f002c6cSBruce M Simpson 		    __func__, ifindex);
27568f002c6cSBruce M Simpson 		return (ENOENT);
27578f002c6cSBruce M Simpson 	}
27588f002c6cSBruce M Simpson 
275929dc7bc6SBruce M Simpson 	memcpy(&mcaddr, &name[1], sizeof(struct in6_addr));
276029dc7bc6SBruce M Simpson 	if (!IN6_IS_ADDR_MULTICAST(&mcaddr)) {
27618f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: group %s is not multicast",
276229dc7bc6SBruce M Simpson 		    __func__, ip6_sprintf(ip6tbuf, &mcaddr));
27638f002c6cSBruce M Simpson 		return (EINVAL);
27648f002c6cSBruce M Simpson 	}
27658f002c6cSBruce M Simpson 
2766b8a6e03fSGleb Smirnoff 	NET_EPOCH_ENTER(et);
27678f002c6cSBruce M Simpson 	ifp = ifnet_byindex(ifindex);
27688f002c6cSBruce M Simpson 	if (ifp == NULL) {
2769b8a6e03fSGleb Smirnoff 		NET_EPOCH_EXIT(et);
27708f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: no ifp for ifindex %u",
27718f002c6cSBruce M Simpson 		    __func__, ifindex);
27728f002c6cSBruce M Simpson 		return (ENOENT);
27738f002c6cSBruce M Simpson 	}
277429dc7bc6SBruce M Simpson 	/*
277529dc7bc6SBruce M Simpson 	 * Internal MLD lookups require that scope/zone ID is set.
277629dc7bc6SBruce M Simpson 	 */
277729dc7bc6SBruce M Simpson 	(void)in6_setscope(&mcaddr, ifp, NULL);
27788f002c6cSBruce M Simpson 
27798f002c6cSBruce M Simpson 	retval = sysctl_wire_old_buffer(req,
27808f002c6cSBruce M Simpson 	    sizeof(uint32_t) + (in6_mcast_maxgrpsrc * sizeof(struct in6_addr)));
2781b8a6e03fSGleb Smirnoff 	if (retval) {
2782b8a6e03fSGleb Smirnoff 		NET_EPOCH_EXIT(et);
27838f002c6cSBruce M Simpson 		return (retval);
2784b8a6e03fSGleb Smirnoff 	}
27858f002c6cSBruce M Simpson 
27868f002c6cSBruce M Simpson 	IN6_MULTI_LOCK();
2787f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK();
2788d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
27892cd6ad76SHans Petter Selasky 		inm = in6m_ifmultiaddr_get_inm(ifma);
27902cd6ad76SHans Petter Selasky 		if (inm == NULL)
27918f002c6cSBruce M Simpson 			continue;
279229dc7bc6SBruce M Simpson 		if (!IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, &mcaddr))
27938f002c6cSBruce M Simpson 			continue;
27948f002c6cSBruce M Simpson 		fmode = inm->in6m_st[1].iss_fmode;
27958f002c6cSBruce M Simpson 		retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
27968f002c6cSBruce M Simpson 		if (retval != 0)
27978f002c6cSBruce M Simpson 			break;
27988f002c6cSBruce M Simpson 		RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
27998f002c6cSBruce M Simpson 			CTR2(KTR_MLD, "%s: visit node %p", __func__, ims);
28008f002c6cSBruce M Simpson 			/*
28018f002c6cSBruce M Simpson 			 * Only copy-out sources which are in-mode.
28028f002c6cSBruce M Simpson 			 */
28038f002c6cSBruce M Simpson 			if (fmode != im6s_get_mode(inm, ims, 1)) {
28048f002c6cSBruce M Simpson 				CTR1(KTR_MLD, "%s: skip non-in-mode",
28058f002c6cSBruce M Simpson 				    __func__);
28068f002c6cSBruce M Simpson 				continue;
28078f002c6cSBruce M Simpson 			}
28088f002c6cSBruce M Simpson 			src = ims->im6s_addr;
28098f002c6cSBruce M Simpson 			retval = SYSCTL_OUT(req, &src,
28108f002c6cSBruce M Simpson 			    sizeof(struct in6_addr));
28118f002c6cSBruce M Simpson 			if (retval != 0)
28128f002c6cSBruce M Simpson 				break;
28138f002c6cSBruce M Simpson 		}
28148f002c6cSBruce M Simpson 	}
2815f3e1324bSStephen Hurd 	IN6_MULTI_LIST_UNLOCK();
28168f002c6cSBruce M Simpson 	IN6_MULTI_UNLOCK();
2817b8a6e03fSGleb Smirnoff 	NET_EPOCH_EXIT(et);
28188f002c6cSBruce M Simpson 
28198f002c6cSBruce M Simpson 	return (retval);
28208f002c6cSBruce M Simpson }
28218f002c6cSBruce M Simpson 
28228f002c6cSBruce M Simpson #ifdef KTR
28238f002c6cSBruce M Simpson 
28248f002c6cSBruce M Simpson static const char *in6m_modestrs[] = { "un", "in", "ex" };
28258f002c6cSBruce M Simpson 
28268f002c6cSBruce M Simpson static const char *
28278f002c6cSBruce M Simpson in6m_mode_str(const int mode)
28288f002c6cSBruce M Simpson {
28298f002c6cSBruce M Simpson 
28308f002c6cSBruce M Simpson 	if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
28318f002c6cSBruce M Simpson 		return (in6m_modestrs[mode]);
28328f002c6cSBruce M Simpson 	return ("??");
28338f002c6cSBruce M Simpson }
28348f002c6cSBruce M Simpson 
28358f002c6cSBruce M Simpson static const char *in6m_statestrs[] = {
28368f002c6cSBruce M Simpson 	"not-member",
28378f002c6cSBruce M Simpson 	"silent",
28388f002c6cSBruce M Simpson 	"idle",
28398f002c6cSBruce M Simpson 	"lazy",
28408f002c6cSBruce M Simpson 	"sleeping",
28418f002c6cSBruce M Simpson 	"awakening",
28428f002c6cSBruce M Simpson 	"query-pending",
28438f002c6cSBruce M Simpson 	"sg-query-pending",
28448f002c6cSBruce M Simpson 	"leaving"
28458f002c6cSBruce M Simpson };
28468f002c6cSBruce M Simpson 
28478f002c6cSBruce M Simpson static const char *
28488f002c6cSBruce M Simpson in6m_state_str(const int state)
28498f002c6cSBruce M Simpson {
28508f002c6cSBruce M Simpson 
28518f002c6cSBruce M Simpson 	if (state >= MLD_NOT_MEMBER && state <= MLD_LEAVING_MEMBER)
28528f002c6cSBruce M Simpson 		return (in6m_statestrs[state]);
28538f002c6cSBruce M Simpson 	return ("??");
28548f002c6cSBruce M Simpson }
28558f002c6cSBruce M Simpson 
28568f002c6cSBruce M Simpson /*
28578f002c6cSBruce M Simpson  * Dump an in6_multi structure to the console.
28588f002c6cSBruce M Simpson  */
28598f002c6cSBruce M Simpson void
28608f002c6cSBruce M Simpson in6m_print(const struct in6_multi *inm)
28618f002c6cSBruce M Simpson {
28628f002c6cSBruce M Simpson 	int t;
28638f002c6cSBruce M Simpson 	char ip6tbuf[INET6_ADDRSTRLEN];
28648f002c6cSBruce M Simpson 
28655b65b8bcSAlexander Kabaev 	if ((ktr_mask & KTR_MLD) == 0)
28668f002c6cSBruce M Simpson 		return;
28678f002c6cSBruce M Simpson 
28688f002c6cSBruce M Simpson 	printf("%s: --- begin in6m %p ---\n", __func__, inm);
28698f002c6cSBruce M Simpson 	printf("addr %s ifp %p(%s) ifma %p\n",
28708f002c6cSBruce M Simpson 	    ip6_sprintf(ip6tbuf, &inm->in6m_addr),
28718f002c6cSBruce M Simpson 	    inm->in6m_ifp,
2872e74966f6SAndrey V. Elsukov 	    if_name(inm->in6m_ifp),
28738f002c6cSBruce M Simpson 	    inm->in6m_ifma);
28748f002c6cSBruce M Simpson 	printf("timer %u state %s refcount %u scq.len %u\n",
28758f002c6cSBruce M Simpson 	    inm->in6m_timer,
28768f002c6cSBruce M Simpson 	    in6m_state_str(inm->in6m_state),
28778f002c6cSBruce M Simpson 	    inm->in6m_refcount,
2878a99c84d4SGleb Smirnoff 	    mbufq_len(&inm->in6m_scq));
28798f002c6cSBruce M Simpson 	printf("mli %p nsrc %lu sctimer %u scrv %u\n",
28808f002c6cSBruce M Simpson 	    inm->in6m_mli,
28818f002c6cSBruce M Simpson 	    inm->in6m_nsrc,
28828f002c6cSBruce M Simpson 	    inm->in6m_sctimer,
28838f002c6cSBruce M Simpson 	    inm->in6m_scrv);
28848f002c6cSBruce M Simpson 	for (t = 0; t < 2; t++) {
28858f002c6cSBruce M Simpson 		printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
28868f002c6cSBruce M Simpson 		    in6m_mode_str(inm->in6m_st[t].iss_fmode),
28878f002c6cSBruce M Simpson 		    inm->in6m_st[t].iss_asm,
28888f002c6cSBruce M Simpson 		    inm->in6m_st[t].iss_ex,
28898f002c6cSBruce M Simpson 		    inm->in6m_st[t].iss_in,
28908f002c6cSBruce M Simpson 		    inm->in6m_st[t].iss_rec);
28918f002c6cSBruce M Simpson 	}
28928f002c6cSBruce M Simpson 	printf("%s: --- end in6m %p ---\n", __func__, inm);
28938f002c6cSBruce M Simpson }
28948f002c6cSBruce M Simpson 
28958f002c6cSBruce M Simpson #else /* !KTR */
28968f002c6cSBruce M Simpson 
28978f002c6cSBruce M Simpson void
28988f002c6cSBruce M Simpson in6m_print(const struct in6_multi *inm)
28998f002c6cSBruce M Simpson {
29008f002c6cSBruce M Simpson 
29018f002c6cSBruce M Simpson }
29028f002c6cSBruce M Simpson 
29038f002c6cSBruce M Simpson #endif /* KTR */
2904