xref: /freebsd/sys/netinet6/in6_mcast.c (revision dea72f062af179ccb8abe3d2e37f4eb9b1c152d4)
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>
44f3e1324bSStephen Hurd #include <sys/gtaskqueue.h>
458f002c6cSBruce M Simpson #include <sys/kernel.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>
538f002c6cSBruce M Simpson #include <sys/ktr.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>
608f002c6cSBruce M Simpson #include <net/vnet.h>
618f002c6cSBruce M Simpson 
62f3e1324bSStephen Hurd 
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:
1058f002c6cSBruce M Simpson  * - Lock order is: Giant, INP_WLOCK, IN6_MULTI_LOCK, MLD_LOCK, IF_ADDR_LOCK.
1068f002c6cSBruce M Simpson  * - The IF_ADDR_LOCK is implicitly taken by in6m_lookup() earlier, however
1078f002c6cSBruce M Simpson  *   it can be taken by code in net/if.c also.
1088f002c6cSBruce M Simpson  * - ip6_moptions and in6_mfilter are covered by the INP_WLOCK.
1098f002c6cSBruce M Simpson  *
1108f002c6cSBruce M Simpson  * struct in6_multi is covered by IN6_MULTI_LOCK. There isn't strictly
1118f002c6cSBruce M Simpson  * any need for in6_multi itself to be virtualized -- it is bound to an ifp
1128f002c6cSBruce M Simpson  * anyway no matter what happens.
1138f002c6cSBruce M Simpson  */
114f3e1324bSStephen Hurd struct mtx in6_multi_list_mtx;
115f3e1324bSStephen Hurd MTX_SYSINIT(in6_multi_mtx, &in6_multi_list_mtx, "in6_multi_list_mtx", MTX_DEF);
116f3e1324bSStephen Hurd 
117f3e1324bSStephen Hurd struct mtx in6_multi_free_mtx;
118f3e1324bSStephen Hurd MTX_SYSINIT(in6_multi_free_mtx, &in6_multi_free_mtx, "in6_multi_free_mtx", MTX_DEF);
119f3e1324bSStephen Hurd 
120f3e1324bSStephen Hurd struct sx in6_multi_sx;
121f3e1324bSStephen Hurd SX_SYSINIT(in6_multi_sx, &in6_multi_sx, "in6_multi_sx");
122f3e1324bSStephen Hurd 
123f3e1324bSStephen Hurd 
1248f002c6cSBruce M Simpson 
1258f002c6cSBruce M Simpson static void	im6f_commit(struct in6_mfilter *);
1268f002c6cSBruce M Simpson static int	im6f_get_source(struct in6_mfilter *imf,
1278f002c6cSBruce M Simpson 		    const struct sockaddr_in6 *psin,
1288f002c6cSBruce M Simpson 		    struct in6_msource **);
1298f002c6cSBruce M Simpson static struct in6_msource *
1308f002c6cSBruce M Simpson 		im6f_graft(struct in6_mfilter *, const uint8_t,
1318f002c6cSBruce M Simpson 		    const struct sockaddr_in6 *);
1328f002c6cSBruce M Simpson static void	im6f_leave(struct in6_mfilter *);
1338f002c6cSBruce M Simpson static int	im6f_prune(struct in6_mfilter *, const struct sockaddr_in6 *);
1348f002c6cSBruce M Simpson static void	im6f_purge(struct in6_mfilter *);
1358f002c6cSBruce M Simpson static void	im6f_rollback(struct in6_mfilter *);
1368f002c6cSBruce M Simpson static void	im6f_reap(struct in6_mfilter *);
1378f002c6cSBruce M Simpson static int	im6o_grow(struct ip6_moptions *);
1388f002c6cSBruce M Simpson static size_t	im6o_match_group(const struct ip6_moptions *,
1398f002c6cSBruce M Simpson 		    const struct ifnet *, const struct sockaddr *);
1408f002c6cSBruce M Simpson static struct in6_msource *
1418f002c6cSBruce M Simpson 		im6o_match_source(const struct ip6_moptions *, const size_t,
1428f002c6cSBruce M Simpson 		    const struct sockaddr *);
1438f002c6cSBruce M Simpson static void	im6s_merge(struct ip6_msource *ims,
1448f002c6cSBruce M Simpson 		    const struct in6_msource *lims, const int rollback);
145f3e1324bSStephen Hurd static int	in6_getmulti(struct ifnet *, const struct in6_addr *,
1468f002c6cSBruce M Simpson 		    struct in6_multi **);
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 
1716472ac3dSEd Schouten static SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, mcast, CTLFLAG_RW, 0,
1726472ac3dSEd Schouten     "IPv6 multicast");
1738f002c6cSBruce M Simpson 
1748f002c6cSBruce M Simpson static u_long in6_mcast_maxgrpsrc = IPV6_MAX_GROUP_SRC_FILTER;
1758f002c6cSBruce M Simpson SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxgrpsrc,
176af3b2549SHans Petter Selasky     CTLFLAG_RWTUN, &in6_mcast_maxgrpsrc, 0,
1778f002c6cSBruce M Simpson     "Max source filters per group");
1788f002c6cSBruce M Simpson 
1798f002c6cSBruce M Simpson static u_long in6_mcast_maxsocksrc = IPV6_MAX_SOCK_SRC_FILTER;
1808f002c6cSBruce M Simpson SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxsocksrc,
181af3b2549SHans Petter Selasky     CTLFLAG_RWTUN, &in6_mcast_maxsocksrc, 0,
1828f002c6cSBruce M Simpson     "Max source filters per socket");
1838f002c6cSBruce M Simpson 
1848f002c6cSBruce M Simpson /* TODO Virtualize this switch. */
1858f002c6cSBruce M Simpson int in6_mcast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
186af3b2549SHans Petter Selasky SYSCTL_INT(_net_inet6_ip6_mcast, OID_AUTO, loop, CTLFLAG_RWTUN,
1878f002c6cSBruce M Simpson     &in6_mcast_loop, 0, "Loopback multicast datagrams by default");
1888f002c6cSBruce M Simpson 
1896472ac3dSEd Schouten static SYSCTL_NODE(_net_inet6_ip6_mcast, OID_AUTO, filters,
1908f002c6cSBruce M Simpson     CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip6_mcast_filters,
1918f002c6cSBruce M Simpson     "Per-interface stack-wide source filters");
1928f002c6cSBruce M Simpson 
193b6f6f880SMatt Macy int ifma6_restart = 0;
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 
2328f002c6cSBruce M Simpson /*
2338f002c6cSBruce M Simpson  * Resize the ip6_moptions vector to the next power-of-two minus 1.
2348f002c6cSBruce M Simpson  * May be called with locks held; do not sleep.
2358f002c6cSBruce M Simpson  */
2368f002c6cSBruce M Simpson static int
2378f002c6cSBruce M Simpson im6o_grow(struct ip6_moptions *imo)
2388f002c6cSBruce M Simpson {
2398f002c6cSBruce M Simpson 	struct in6_multi	**nmships;
2408f002c6cSBruce M Simpson 	struct in6_multi	**omships;
2418f002c6cSBruce M Simpson 	struct in6_mfilter	 *nmfilters;
2428f002c6cSBruce M Simpson 	struct in6_mfilter	 *omfilters;
2438f002c6cSBruce M Simpson 	size_t			  idx;
2448f002c6cSBruce M Simpson 	size_t			  newmax;
2458f002c6cSBruce M Simpson 	size_t			  oldmax;
2468f002c6cSBruce M Simpson 
2478f002c6cSBruce M Simpson 	nmships = NULL;
2488f002c6cSBruce M Simpson 	nmfilters = NULL;
2498f002c6cSBruce M Simpson 	omships = imo->im6o_membership;
2508f002c6cSBruce M Simpson 	omfilters = imo->im6o_mfilters;
2518f002c6cSBruce M Simpson 	oldmax = imo->im6o_max_memberships;
2528f002c6cSBruce M Simpson 	newmax = ((oldmax + 1) * 2) - 1;
2538f002c6cSBruce M Simpson 
2548f002c6cSBruce M Simpson 	if (newmax <= IPV6_MAX_MEMBERSHIPS) {
2558f002c6cSBruce M Simpson 		nmships = (struct in6_multi **)realloc(omships,
2568f002c6cSBruce M Simpson 		    sizeof(struct in6_multi *) * newmax, M_IP6MOPTS, M_NOWAIT);
2578f002c6cSBruce M Simpson 		nmfilters = (struct in6_mfilter *)realloc(omfilters,
2588f002c6cSBruce M Simpson 		    sizeof(struct in6_mfilter) * newmax, M_IN6MFILTER,
2598f002c6cSBruce M Simpson 		    M_NOWAIT);
2608f002c6cSBruce M Simpson 		if (nmships != NULL && nmfilters != NULL) {
2618f002c6cSBruce M Simpson 			/* Initialize newly allocated source filter heads. */
2628f002c6cSBruce M Simpson 			for (idx = oldmax; idx < newmax; idx++) {
2638f002c6cSBruce M Simpson 				im6f_init(&nmfilters[idx], MCAST_UNDEFINED,
2648f002c6cSBruce M Simpson 				    MCAST_EXCLUDE);
2658f002c6cSBruce M Simpson 			}
2668f002c6cSBruce M Simpson 			imo->im6o_max_memberships = newmax;
2678f002c6cSBruce M Simpson 			imo->im6o_membership = nmships;
2688f002c6cSBruce M Simpson 			imo->im6o_mfilters = nmfilters;
2698f002c6cSBruce M Simpson 		}
2708f002c6cSBruce M Simpson 	}
2718f002c6cSBruce M Simpson 
2728f002c6cSBruce M Simpson 	if (nmships == NULL || nmfilters == NULL) {
2738f002c6cSBruce M Simpson 		if (nmships != NULL)
2748f002c6cSBruce M Simpson 			free(nmships, M_IP6MOPTS);
2758f002c6cSBruce M Simpson 		if (nmfilters != NULL)
2768f002c6cSBruce M Simpson 			free(nmfilters, M_IN6MFILTER);
2778f002c6cSBruce M Simpson 		return (ETOOMANYREFS);
2788f002c6cSBruce M Simpson 	}
2798f002c6cSBruce M Simpson 
2808f002c6cSBruce M Simpson 	return (0);
2818f002c6cSBruce M Simpson }
2828f002c6cSBruce M Simpson 
2838f002c6cSBruce M Simpson /*
2848f002c6cSBruce M Simpson  * Find an IPv6 multicast group entry for this ip6_moptions instance
2858f002c6cSBruce M Simpson  * which matches the specified group, and optionally an interface.
2868f002c6cSBruce M Simpson  * Return its index into the array, or -1 if not found.
2878f002c6cSBruce M Simpson  */
2888f002c6cSBruce M Simpson static size_t
2898f002c6cSBruce M Simpson im6o_match_group(const struct ip6_moptions *imo, const struct ifnet *ifp,
2908f002c6cSBruce M Simpson     const struct sockaddr *group)
2918f002c6cSBruce M Simpson {
2928f002c6cSBruce M Simpson 	const struct sockaddr_in6 *gsin6;
2938f002c6cSBruce M Simpson 	struct in6_multi	**pinm;
2948f002c6cSBruce M Simpson 	int		  idx;
2958f002c6cSBruce M Simpson 	int		  nmships;
2968f002c6cSBruce M Simpson 
2978f002c6cSBruce M Simpson 	gsin6 = (const struct sockaddr_in6 *)group;
2988f002c6cSBruce M Simpson 
2998f002c6cSBruce M Simpson 	/* The im6o_membership array may be lazy allocated. */
3008f002c6cSBruce M Simpson 	if (imo->im6o_membership == NULL || imo->im6o_num_memberships == 0)
3018f002c6cSBruce M Simpson 		return (-1);
3028f002c6cSBruce M Simpson 
3038f002c6cSBruce M Simpson 	nmships = imo->im6o_num_memberships;
3048f002c6cSBruce M Simpson 	pinm = &imo->im6o_membership[0];
3058f002c6cSBruce M Simpson 	for (idx = 0; idx < nmships; idx++, pinm++) {
3068f002c6cSBruce M Simpson 		if (*pinm == NULL)
3078f002c6cSBruce M Simpson 			continue;
3088f002c6cSBruce M Simpson 		if ((ifp == NULL || ((*pinm)->in6m_ifp == ifp)) &&
3098f002c6cSBruce M Simpson 		    IN6_ARE_ADDR_EQUAL(&(*pinm)->in6m_addr,
3108f002c6cSBruce M Simpson 		    &gsin6->sin6_addr)) {
3118f002c6cSBruce M Simpson 			break;
3128f002c6cSBruce M Simpson 		}
3138f002c6cSBruce M Simpson 	}
3148f002c6cSBruce M Simpson 	if (idx >= nmships)
3158f002c6cSBruce M Simpson 		idx = -1;
3168f002c6cSBruce M Simpson 
3178f002c6cSBruce M Simpson 	return (idx);
3188f002c6cSBruce M Simpson }
3198f002c6cSBruce M Simpson 
3208f002c6cSBruce M Simpson /*
3218f002c6cSBruce M Simpson  * Find an IPv6 multicast source entry for this imo which matches
3228f002c6cSBruce M Simpson  * the given group index for this socket, and source address.
3238f002c6cSBruce M Simpson  *
32429dc7bc6SBruce M Simpson  * XXX TODO: The scope ID, if present in src, is stripped before
32529dc7bc6SBruce M Simpson  * any comparison. We SHOULD enforce scope/zone checks where the source
32629dc7bc6SBruce M Simpson  * filter entry has a link scope.
32729dc7bc6SBruce M Simpson  *
3288f002c6cSBruce M Simpson  * NOTE: This does not check if the entry is in-mode, merely if
3298f002c6cSBruce M Simpson  * it exists, which may not be the desired behaviour.
3308f002c6cSBruce M Simpson  */
3318f002c6cSBruce M Simpson static struct in6_msource *
3328f002c6cSBruce M Simpson im6o_match_source(const struct ip6_moptions *imo, const size_t gidx,
3338f002c6cSBruce M Simpson     const struct sockaddr *src)
3348f002c6cSBruce M Simpson {
3358f002c6cSBruce M Simpson 	struct ip6_msource	 find;
3368f002c6cSBruce M Simpson 	struct in6_mfilter	*imf;
3378f002c6cSBruce M Simpson 	struct ip6_msource	*ims;
3388f002c6cSBruce M Simpson 	const sockunion_t	*psa;
3398f002c6cSBruce M Simpson 
3408f002c6cSBruce M Simpson 	KASSERT(src->sa_family == AF_INET6, ("%s: !AF_INET6", __func__));
3418f002c6cSBruce M Simpson 	KASSERT(gidx != -1 && gidx < imo->im6o_num_memberships,
3428f002c6cSBruce M Simpson 	    ("%s: invalid index %d\n", __func__, (int)gidx));
3438f002c6cSBruce M Simpson 
3448f002c6cSBruce M Simpson 	/* The im6o_mfilters array may be lazy allocated. */
3458f002c6cSBruce M Simpson 	if (imo->im6o_mfilters == NULL)
3468f002c6cSBruce M Simpson 		return (NULL);
3478f002c6cSBruce M Simpson 	imf = &imo->im6o_mfilters[gidx];
3488f002c6cSBruce M Simpson 
3498f002c6cSBruce M Simpson 	psa = (const sockunion_t *)src;
3508f002c6cSBruce M Simpson 	find.im6s_addr = psa->sin6.sin6_addr;
35129dc7bc6SBruce M Simpson 	in6_clearscope(&find.im6s_addr);		/* XXX */
3528f002c6cSBruce M Simpson 	ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
3538f002c6cSBruce M Simpson 
3548f002c6cSBruce M Simpson 	return ((struct in6_msource *)ims);
3558f002c6cSBruce M Simpson }
3568f002c6cSBruce M Simpson 
3578f002c6cSBruce M Simpson /*
3588f002c6cSBruce M Simpson  * Perform filtering for multicast datagrams on a socket by group and source.
3598f002c6cSBruce M Simpson  *
3608f002c6cSBruce M Simpson  * Returns 0 if a datagram should be allowed through, or various error codes
3618f002c6cSBruce M Simpson  * if the socket was not a member of the group, or the source was muted, etc.
3628f002c6cSBruce M Simpson  */
3638f002c6cSBruce M Simpson int
3648f002c6cSBruce M Simpson im6o_mc_filter(const struct ip6_moptions *imo, const struct ifnet *ifp,
3658f002c6cSBruce M Simpson     const struct sockaddr *group, const struct sockaddr *src)
3668f002c6cSBruce M Simpson {
3678f002c6cSBruce M Simpson 	size_t gidx;
3688f002c6cSBruce M Simpson 	struct in6_msource *ims;
3698f002c6cSBruce M Simpson 	int mode;
3708f002c6cSBruce M Simpson 
3718f002c6cSBruce M Simpson 	KASSERT(ifp != NULL, ("%s: null ifp", __func__));
3728f002c6cSBruce M Simpson 
3738f002c6cSBruce M Simpson 	gidx = im6o_match_group(imo, ifp, group);
3748f002c6cSBruce M Simpson 	if (gidx == -1)
3758f002c6cSBruce M Simpson 		return (MCAST_NOTGMEMBER);
3768f002c6cSBruce M Simpson 
3778f002c6cSBruce M Simpson 	/*
3788f002c6cSBruce M Simpson 	 * Check if the source was included in an (S,G) join.
3798f002c6cSBruce M Simpson 	 * Allow reception on exclusive memberships by default,
3808f002c6cSBruce M Simpson 	 * reject reception on inclusive memberships by default.
3818f002c6cSBruce M Simpson 	 * Exclude source only if an in-mode exclude filter exists.
3828f002c6cSBruce M Simpson 	 * Include source only if an in-mode include filter exists.
3838f002c6cSBruce M Simpson 	 * NOTE: We are comparing group state here at MLD t1 (now)
3848f002c6cSBruce M Simpson 	 * with socket-layer t0 (since last downcall).
3858f002c6cSBruce M Simpson 	 */
3868f002c6cSBruce M Simpson 	mode = imo->im6o_mfilters[gidx].im6f_st[1];
3878f002c6cSBruce M Simpson 	ims = im6o_match_source(imo, gidx, src);
3888f002c6cSBruce M Simpson 
3898f002c6cSBruce M Simpson 	if ((ims == NULL && mode == MCAST_INCLUDE) ||
3908f002c6cSBruce M Simpson 	    (ims != NULL && ims->im6sl_st[0] != mode))
3918f002c6cSBruce M Simpson 		return (MCAST_NOTSMEMBER);
3928f002c6cSBruce M Simpson 
3938f002c6cSBruce M Simpson 	return (MCAST_PASS);
3948f002c6cSBruce M Simpson }
3958f002c6cSBruce M Simpson 
3968f002c6cSBruce M Simpson /*
3978f002c6cSBruce M Simpson  * Find and return a reference to an in6_multi record for (ifp, group),
3988f002c6cSBruce M Simpson  * and bump its reference count.
3998f002c6cSBruce M Simpson  * If one does not exist, try to allocate it, and update link-layer multicast
4008f002c6cSBruce M Simpson  * filters on ifp to listen for group.
4018f002c6cSBruce M Simpson  * Assumes the IN6_MULTI lock is held across the call.
4028f002c6cSBruce M Simpson  * Return 0 if successful, otherwise return an appropriate error code.
4038f002c6cSBruce M Simpson  */
4048f002c6cSBruce M Simpson static int
405f3e1324bSStephen Hurd in6_getmulti(struct ifnet *ifp, const struct in6_addr *group,
4068f002c6cSBruce M Simpson     struct in6_multi **pinm)
4078f002c6cSBruce M Simpson {
4088f002c6cSBruce M Simpson 	struct sockaddr_in6	 gsin6;
4098f002c6cSBruce M Simpson 	struct ifmultiaddr	*ifma;
4108f002c6cSBruce M Simpson 	struct in6_multi	*inm;
4118f002c6cSBruce M Simpson 	int			 error;
4128f002c6cSBruce M Simpson 
4138f002c6cSBruce M Simpson 	error = 0;
4148f002c6cSBruce M Simpson 
4158f002c6cSBruce M Simpson 	/*
4168f002c6cSBruce M Simpson 	 * XXX: Accesses to ifma_protospec must be covered by IF_ADDR_LOCK;
4178f002c6cSBruce M Simpson 	 * if_addmulti() takes this mutex itself, so we must drop and
4188f002c6cSBruce M Simpson 	 * re-acquire around the call.
4198f002c6cSBruce M Simpson 	 */
4208f002c6cSBruce M Simpson 	IN6_MULTI_LOCK_ASSERT();
421f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK();
422137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
4238f002c6cSBruce M Simpson 	inm = in6m_lookup_locked(ifp, group);
4248f002c6cSBruce M Simpson 	if (inm != NULL) {
4258f002c6cSBruce M Simpson 		/*
4268f002c6cSBruce M Simpson 		 * If we already joined this group, just bump the
4278f002c6cSBruce M Simpson 		 * refcount and return it.
4288f002c6cSBruce M Simpson 		 */
4298f002c6cSBruce M Simpson 		KASSERT(inm->in6m_refcount >= 1,
4308f002c6cSBruce M Simpson 		    ("%s: bad refcount %d", __func__, inm->in6m_refcount));
431f3e1324bSStephen Hurd 		in6m_acquire_locked(inm);
4328f002c6cSBruce M Simpson 		*pinm = inm;
4338f002c6cSBruce M Simpson 		goto out_locked;
4348f002c6cSBruce M Simpson 	}
4358f002c6cSBruce M Simpson 
4368f002c6cSBruce M Simpson 	memset(&gsin6, 0, sizeof(gsin6));
4378f002c6cSBruce M Simpson 	gsin6.sin6_family = AF_INET6;
4388f002c6cSBruce M Simpson 	gsin6.sin6_len = sizeof(struct sockaddr_in6);
4398f002c6cSBruce M Simpson 	gsin6.sin6_addr = *group;
4408f002c6cSBruce M Simpson 
4418f002c6cSBruce M Simpson 	/*
4428f002c6cSBruce M Simpson 	 * Check if a link-layer group is already associated
4438f002c6cSBruce M Simpson 	 * with this network-layer group on the given ifnet.
4448f002c6cSBruce M Simpson 	 */
445f3e1324bSStephen Hurd 	IN6_MULTI_LIST_UNLOCK();
446137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
4478f002c6cSBruce M Simpson 	error = if_addmulti(ifp, (struct sockaddr *)&gsin6, &ifma);
4488f002c6cSBruce M Simpson 	if (error != 0)
4498f002c6cSBruce M Simpson 		return (error);
450f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK();
451137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
4528f002c6cSBruce M Simpson 
4538f002c6cSBruce M Simpson 	/*
4548f002c6cSBruce M Simpson 	 * If something other than netinet6 is occupying the link-layer
4558f002c6cSBruce M Simpson 	 * group, print a meaningful error message and back out of
4568f002c6cSBruce M Simpson 	 * the allocation.
4578f002c6cSBruce M Simpson 	 * Otherwise, bump the refcount on the existing network-layer
4588f002c6cSBruce M Simpson 	 * group association and return it.
4598f002c6cSBruce M Simpson 	 */
4608f002c6cSBruce M Simpson 	if (ifma->ifma_protospec != NULL) {
4618f002c6cSBruce M Simpson 		inm = (struct in6_multi *)ifma->ifma_protospec;
4628f002c6cSBruce M Simpson #ifdef INVARIANTS
4638f002c6cSBruce M Simpson 		KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
4648f002c6cSBruce M Simpson 		    __func__));
4658f002c6cSBruce M Simpson 		KASSERT(ifma->ifma_addr->sa_family == AF_INET6,
4668f002c6cSBruce M Simpson 		    ("%s: ifma not AF_INET6", __func__));
4678f002c6cSBruce M Simpson 		KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
4688f002c6cSBruce M Simpson 		if (inm->in6m_ifma != ifma || inm->in6m_ifp != ifp ||
4698f002c6cSBruce M Simpson 		    !IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, group))
4708f002c6cSBruce M Simpson 			panic("%s: ifma %p is inconsistent with %p (%p)",
4718f002c6cSBruce M Simpson 			    __func__, ifma, inm, group);
4728f002c6cSBruce M Simpson #endif
473f3e1324bSStephen Hurd 		in6m_acquire_locked(inm);
4748f002c6cSBruce M Simpson 		*pinm = inm;
4758f002c6cSBruce M Simpson 		goto out_locked;
4768f002c6cSBruce M Simpson 	}
4778f002c6cSBruce M Simpson 
478137f91e8SJohn Baldwin 	IF_ADDR_WLOCK_ASSERT(ifp);
4798f002c6cSBruce M Simpson 
4808f002c6cSBruce M Simpson 	/*
4818f002c6cSBruce M Simpson 	 * A new in6_multi record is needed; allocate and initialize it.
4828f002c6cSBruce M Simpson 	 * We DO NOT perform an MLD join as the in6_ layer may need to
4838f002c6cSBruce M Simpson 	 * push an initial source list down to MLD to support SSM.
4848f002c6cSBruce M Simpson 	 *
4858f002c6cSBruce M Simpson 	 * The initial source filter state is INCLUDE, {} as per the RFC.
4868f002c6cSBruce M Simpson 	 * Pending state-changes per group are subject to a bounds check.
4878f002c6cSBruce M Simpson 	 */
4888f002c6cSBruce M Simpson 	inm = malloc(sizeof(*inm), M_IP6MADDR, M_NOWAIT | M_ZERO);
4898f002c6cSBruce M Simpson 	if (inm == NULL) {
490f3e1324bSStephen Hurd 		IN6_MULTI_LIST_UNLOCK();
49155faae77SConrad Meyer 		IF_ADDR_WUNLOCK(ifp);
4928f002c6cSBruce M Simpson 		if_delmulti_ifma(ifma);
49355faae77SConrad Meyer 		return (ENOMEM);
4948f002c6cSBruce M Simpson 	}
4958f002c6cSBruce M Simpson 	inm->in6m_addr = *group;
4968f002c6cSBruce M Simpson 	inm->in6m_ifp = ifp;
4978f002c6cSBruce M Simpson 	inm->in6m_mli = MLD_IFINFO(ifp);
4988f002c6cSBruce M Simpson 	inm->in6m_ifma = ifma;
4998f002c6cSBruce M Simpson 	inm->in6m_refcount = 1;
5008f002c6cSBruce M Simpson 	inm->in6m_state = MLD_NOT_MEMBER;
501a99c84d4SGleb Smirnoff 	mbufq_init(&inm->in6m_scq, MLD_MAX_STATE_CHANGES);
5028f002c6cSBruce M Simpson 
5038f002c6cSBruce M Simpson 	inm->in6m_st[0].iss_fmode = MCAST_UNDEFINED;
5048f002c6cSBruce M Simpson 	inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
5058f002c6cSBruce M Simpson 	RB_INIT(&inm->in6m_srcs);
5068f002c6cSBruce M Simpson 
5078f002c6cSBruce M Simpson 	ifma->ifma_protospec = inm;
5088f002c6cSBruce M Simpson 	*pinm = inm;
5098f002c6cSBruce M Simpson 
5108f002c6cSBruce M Simpson  out_locked:
511f3e1324bSStephen Hurd 	IN6_MULTI_LIST_UNLOCK();
512137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
5138f002c6cSBruce M Simpson 	return (error);
5148f002c6cSBruce M Simpson }
5158f002c6cSBruce M Simpson 
5168f002c6cSBruce M Simpson /*
5178f002c6cSBruce M Simpson  * Drop a reference to an in6_multi record.
5188f002c6cSBruce M Simpson  *
5198f002c6cSBruce M Simpson  * If the refcount drops to 0, free the in6_multi record and
5208f002c6cSBruce M Simpson  * delete the underlying link-layer membership.
5218f002c6cSBruce M Simpson  */
522f3e1324bSStephen Hurd static void
523f3e1324bSStephen Hurd in6m_release(struct in6_multi *inm)
5248f002c6cSBruce M Simpson {
5258f002c6cSBruce M Simpson 	struct ifmultiaddr *ifma;
526f3e1324bSStephen Hurd 	struct ifnet *ifp;
5278f002c6cSBruce M Simpson 
5288f002c6cSBruce M Simpson 	CTR2(KTR_MLD, "%s: refcount is %d", __func__, inm->in6m_refcount);
5298f002c6cSBruce M Simpson 
530f3e1324bSStephen Hurd 	MPASS(inm->in6m_refcount == 0);
5318f002c6cSBruce M Simpson 	CTR2(KTR_MLD, "%s: freeing inm %p", __func__, inm);
5328f002c6cSBruce M Simpson 
5338f002c6cSBruce M Simpson 	ifma = inm->in6m_ifma;
534f3e1324bSStephen Hurd 	ifp = inm->in6m_ifp;
535b6f6f880SMatt Macy 	MPASS(ifma->ifma_llifma == NULL);
5368f002c6cSBruce M Simpson 
5378f002c6cSBruce M Simpson 	/* XXX this access is not covered by IF_ADDR_LOCK */
5388f002c6cSBruce M Simpson 	CTR2(KTR_MLD, "%s: purging ifma %p", __func__, ifma);
539f3e1324bSStephen Hurd 	KASSERT(ifma->ifma_protospec == NULL,
540f3e1324bSStephen Hurd 	    ("%s: ifma_protospec != NULL", __func__));
541f9be0386SMatt Macy 	if (ifp == NULL)
542f9be0386SMatt Macy 		ifp = ifma->ifma_ifp;
5438f002c6cSBruce M Simpson 
5443b9b6b17SEd Maste 	if (ifp != NULL) {
54515f8acc5SEd Maste 		CURVNET_SET(ifp->if_vnet);
5468f002c6cSBruce M Simpson 		in6m_purge(inm);
5478f002c6cSBruce M Simpson 		free(inm, M_IP6MADDR);
548b6f6f880SMatt Macy 		if_delmulti_ifma_flags(ifma, 1);
54915f8acc5SEd Maste 		CURVNET_RESTORE();
550b6f6f880SMatt Macy 		if_rele(ifp);
5513b9b6b17SEd Maste 	} else {
5523b9b6b17SEd Maste 		in6m_purge(inm);
5533b9b6b17SEd Maste 		free(inm, M_IP6MADDR);
5543b9b6b17SEd Maste 		if_delmulti_ifma_flags(ifma, 1);
555b6f6f880SMatt Macy 	}
556f3e1324bSStephen Hurd }
557f3e1324bSStephen Hurd 
558f3e1324bSStephen Hurd static struct grouptask free_gtask;
559f3e1324bSStephen Hurd static struct in6_multi_head in6m_free_list;
560f3e1324bSStephen Hurd static void in6m_release_task(void *arg __unused);
561f3e1324bSStephen Hurd static void in6m_init(void)
562f3e1324bSStephen Hurd {
563f3e1324bSStephen Hurd 	SLIST_INIT(&in6m_free_list);
564f3e1324bSStephen Hurd 	taskqgroup_config_gtask_init(NULL, &free_gtask, in6m_release_task, "in6m release task");
565f3e1324bSStephen Hurd }
566f3e1324bSStephen Hurd 
567c8e2123bSLeandro Lupori #ifdef EARLY_AP_STARTUP
568f3e1324bSStephen Hurd SYSINIT(in6m_init, SI_SUB_SMP + 1, SI_ORDER_FIRST,
569f3e1324bSStephen Hurd 	in6m_init, NULL);
570c8e2123bSLeandro Lupori #else
571c8e2123bSLeandro Lupori SYSINIT(in6m_init, SI_SUB_ROOT_CONF - 1, SI_ORDER_SECOND,
572c8e2123bSLeandro Lupori 	in6m_init, NULL);
573c8e2123bSLeandro Lupori #endif
574f3e1324bSStephen Hurd 
575f3e1324bSStephen Hurd 
576f3e1324bSStephen Hurd void
577f3e1324bSStephen Hurd in6m_release_list_deferred(struct in6_multi_head *inmh)
578f3e1324bSStephen Hurd {
579f3e1324bSStephen Hurd 	if (SLIST_EMPTY(inmh))
580f3e1324bSStephen Hurd 		return;
581f3e1324bSStephen Hurd 	mtx_lock(&in6_multi_free_mtx);
582f3e1324bSStephen Hurd 	SLIST_CONCAT(&in6m_free_list, inmh, in6_multi, in6m_nrele);
583f3e1324bSStephen Hurd 	mtx_unlock(&in6_multi_free_mtx);
584f3e1324bSStephen Hurd 	GROUPTASK_ENQUEUE(&free_gtask);
585f3e1324bSStephen Hurd }
586f3e1324bSStephen Hurd 
587f3e1324bSStephen Hurd void
588*dea72f06SHans Petter Selasky in6m_release_wait(void)
589*dea72f06SHans Petter Selasky {
590*dea72f06SHans Petter Selasky 
591*dea72f06SHans Petter Selasky 	/* Wait for all jobs to complete. */
592*dea72f06SHans Petter Selasky 	gtaskqueue_drain_all(free_gtask.gt_taskqueue);
593*dea72f06SHans Petter Selasky }
594*dea72f06SHans Petter Selasky 
595*dea72f06SHans Petter Selasky void
596b6f6f880SMatt Macy in6m_disconnect(struct in6_multi *inm)
597f3e1324bSStephen Hurd {
598f3e1324bSStephen Hurd 	struct ifnet *ifp;
599f3e1324bSStephen Hurd 	struct ifaddr *ifa;
600f3e1324bSStephen Hurd 	struct in6_ifaddr *ifa6;
60166fe09d8SAndrew Gallatin 	struct in6_multi_mship *imm, *imm_tmp;
602b6f6f880SMatt Macy 	struct ifmultiaddr *ifma, *ll_ifma;
603f3e1324bSStephen Hurd 
604f3e1324bSStephen Hurd 	ifp = inm->in6m_ifp;
605f9be0386SMatt Macy 
606f9be0386SMatt Macy 	if (ifp == NULL)
607f9be0386SMatt Macy 		return;
608f9be0386SMatt Macy 	inm->in6m_ifp = NULL;
609b6f6f880SMatt Macy 	IF_ADDR_WLOCK_ASSERT(ifp);
610b6f6f880SMatt Macy 	ifma = inm->in6m_ifma;
611f9be0386SMatt Macy 	if (ifma == NULL)
612f9be0386SMatt Macy 		return;
613b6f6f880SMatt Macy 
614b6f6f880SMatt Macy 	if_ref(ifp);
615f9be0386SMatt Macy 	if (ifma->ifma_flags & IFMA_F_ENQUEUED) {
616d7c5a620SMatt Macy 		CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
617f9be0386SMatt Macy 		ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
618f9be0386SMatt Macy 	}
619b6f6f880SMatt Macy 	MCDPRINTF("removed ifma: %p from %s\n", ifma, ifp->if_xname);
620b6f6f880SMatt Macy 	if ((ll_ifma = ifma->ifma_llifma) != NULL) {
621b6f6f880SMatt Macy 		MPASS(ifma != ll_ifma);
622b6f6f880SMatt Macy 		ifma->ifma_llifma = NULL;
623b6f6f880SMatt Macy 		MPASS(ll_ifma->ifma_llifma == NULL);
624b6f6f880SMatt Macy 		MPASS(ll_ifma->ifma_ifp == ifp);
625b6f6f880SMatt Macy 		if (--ll_ifma->ifma_refcount == 0) {
626b6f6f880SMatt Macy 			ifma6_restart = true;
627f9be0386SMatt Macy 			if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
628d7c5a620SMatt Macy 				CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, ifma_link);
629d3878608SMatt Macy 				ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
630f9be0386SMatt Macy 			}
631b6f6f880SMatt Macy 			MCDPRINTF("removed ll_ifma: %p from %s\n", ll_ifma, ifp->if_xname);
632b6f6f880SMatt Macy 			if_freemulti(ll_ifma);
633b6f6f880SMatt Macy 		}
634b6f6f880SMatt Macy 	}
635d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
636f3e1324bSStephen Hurd 		if (ifa->ifa_addr->sa_family != AF_INET6)
637f3e1324bSStephen Hurd 			continue;
638f3e1324bSStephen Hurd 		ifa6 = (void *)ifa;
63966fe09d8SAndrew Gallatin 		LIST_FOREACH_SAFE(imm, &ifa6->ia6_memberships,
64066fe09d8SAndrew Gallatin 		    i6mm_chain, imm_tmp) {
641b6f6f880SMatt Macy 			if (inm == imm->i6mm_maddr) {
642b6f6f880SMatt Macy 				LIST_REMOVE(imm, i6mm_chain);
643b6f6f880SMatt Macy 				free(imm, M_IP6MADDR);
644f3e1324bSStephen Hurd 			}
645f3e1324bSStephen Hurd 		}
646b6f6f880SMatt Macy 	}
647b6f6f880SMatt Macy }
648b6f6f880SMatt Macy 
649b6f6f880SMatt Macy void
650b6f6f880SMatt Macy in6m_release_deferred(struct in6_multi *inm)
651b6f6f880SMatt Macy {
652b6f6f880SMatt Macy 	struct in6_multi_head tmp;
653b6f6f880SMatt Macy 
654b6f6f880SMatt Macy 	IN6_MULTI_LIST_LOCK_ASSERT();
655b6f6f880SMatt Macy 	KASSERT(inm->in6m_refcount > 0, ("refcount == %d inm: %p", inm->in6m_refcount, inm));
656b6f6f880SMatt Macy 	if (--inm->in6m_refcount == 0) {
657f9be0386SMatt Macy 		MPASS(inm->in6m_ifp == NULL);
658f3e1324bSStephen Hurd 		SLIST_INIT(&tmp);
659f3e1324bSStephen Hurd 		inm->in6m_ifma->ifma_protospec = NULL;
660b6f6f880SMatt Macy 		MPASS(inm->in6m_ifma->ifma_llifma == NULL);
661f3e1324bSStephen Hurd 		SLIST_INSERT_HEAD(&tmp, inm, in6m_nrele);
662f3e1324bSStephen Hurd 		in6m_release_list_deferred(&tmp);
663f3e1324bSStephen Hurd 	}
664f3e1324bSStephen Hurd }
665f3e1324bSStephen Hurd 
666f3e1324bSStephen Hurd static void
667f3e1324bSStephen Hurd in6m_release_task(void *arg __unused)
668f3e1324bSStephen Hurd {
669f3e1324bSStephen Hurd 	struct in6_multi_head in6m_free_tmp;
670f3e1324bSStephen Hurd 	struct in6_multi *inm, *tinm;
671f3e1324bSStephen Hurd 
672f3e1324bSStephen Hurd 	SLIST_INIT(&in6m_free_tmp);
673f3e1324bSStephen Hurd 	mtx_lock(&in6_multi_free_mtx);
674f3e1324bSStephen Hurd 	SLIST_CONCAT(&in6m_free_tmp, &in6m_free_list, in6_multi, in6m_nrele);
675f3e1324bSStephen Hurd 	mtx_unlock(&in6_multi_free_mtx);
676f3e1324bSStephen Hurd 	IN6_MULTI_LOCK();
677f3e1324bSStephen Hurd 	SLIST_FOREACH_SAFE(inm, &in6m_free_tmp, in6m_nrele, tinm) {
678f3e1324bSStephen Hurd 		SLIST_REMOVE_HEAD(&in6m_free_tmp, in6m_nrele);
679f3e1324bSStephen Hurd 		in6m_release(inm);
680f3e1324bSStephen Hurd 	}
681f3e1324bSStephen Hurd 	IN6_MULTI_UNLOCK();
6828f002c6cSBruce M Simpson }
6838f002c6cSBruce M Simpson 
6848f002c6cSBruce M Simpson /*
6858f002c6cSBruce M Simpson  * Clear recorded source entries for a group.
6868f002c6cSBruce M Simpson  * Used by the MLD code. Caller must hold the IN6_MULTI lock.
6878f002c6cSBruce M Simpson  * FIXME: Should reap.
6888f002c6cSBruce M Simpson  */
6898f002c6cSBruce M Simpson void
6908f002c6cSBruce M Simpson in6m_clear_recorded(struct in6_multi *inm)
6918f002c6cSBruce M Simpson {
6928f002c6cSBruce M Simpson 	struct ip6_msource	*ims;
6938f002c6cSBruce M Simpson 
694f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK_ASSERT();
6958f002c6cSBruce M Simpson 
6968f002c6cSBruce M Simpson 	RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
6978f002c6cSBruce M Simpson 		if (ims->im6s_stp) {
6988f002c6cSBruce M Simpson 			ims->im6s_stp = 0;
6998f002c6cSBruce M Simpson 			--inm->in6m_st[1].iss_rec;
7008f002c6cSBruce M Simpson 		}
7018f002c6cSBruce M Simpson 	}
7028f002c6cSBruce M Simpson 	KASSERT(inm->in6m_st[1].iss_rec == 0,
7038f002c6cSBruce M Simpson 	    ("%s: iss_rec %d not 0", __func__, inm->in6m_st[1].iss_rec));
7048f002c6cSBruce M Simpson }
7058f002c6cSBruce M Simpson 
7068f002c6cSBruce M Simpson /*
7078f002c6cSBruce M Simpson  * Record a source as pending for a Source-Group MLDv2 query.
7088f002c6cSBruce M Simpson  * This lives here as it modifies the shared tree.
7098f002c6cSBruce M Simpson  *
7108f002c6cSBruce M Simpson  * inm is the group descriptor.
7118f002c6cSBruce M Simpson  * naddr is the address of the source to record in network-byte order.
7128f002c6cSBruce M Simpson  *
7138f002c6cSBruce M Simpson  * If the net.inet6.mld.sgalloc sysctl is non-zero, we will
7148f002c6cSBruce M Simpson  * lazy-allocate a source node in response to an SG query.
7158f002c6cSBruce M Simpson  * Otherwise, no allocation is performed. This saves some memory
7168f002c6cSBruce M Simpson  * with the trade-off that the source will not be reported to the
7178f002c6cSBruce M Simpson  * router if joined in the window between the query response and
7188f002c6cSBruce M Simpson  * the group actually being joined on the local host.
7198f002c6cSBruce M Simpson  *
7208f002c6cSBruce M Simpson  * VIMAGE: XXX: Currently the mld_sgalloc feature has been removed.
7218f002c6cSBruce M Simpson  * This turns off the allocation of a recorded source entry if
7228f002c6cSBruce M Simpson  * the group has not been joined.
7238f002c6cSBruce M Simpson  *
7248f002c6cSBruce M Simpson  * Return 0 if the source didn't exist or was already marked as recorded.
7258f002c6cSBruce M Simpson  * Return 1 if the source was marked as recorded by this function.
726a4641f4eSPedro F. Giffuni  * Return <0 if any error occurred (negated errno code).
7278f002c6cSBruce M Simpson  */
7288f002c6cSBruce M Simpson int
7298f002c6cSBruce M Simpson in6m_record_source(struct in6_multi *inm, const struct in6_addr *addr)
7308f002c6cSBruce M Simpson {
7318f002c6cSBruce M Simpson 	struct ip6_msource	 find;
7328f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *nims;
7338f002c6cSBruce M Simpson 
734f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK_ASSERT();
7358f002c6cSBruce M Simpson 
7368f002c6cSBruce M Simpson 	find.im6s_addr = *addr;
7378f002c6cSBruce M Simpson 	ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
7388f002c6cSBruce M Simpson 	if (ims && ims->im6s_stp)
7398f002c6cSBruce M Simpson 		return (0);
7408f002c6cSBruce M Simpson 	if (ims == NULL) {
7418f002c6cSBruce M Simpson 		if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
7428f002c6cSBruce M Simpson 			return (-ENOSPC);
7438f002c6cSBruce M Simpson 		nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
7448f002c6cSBruce M Simpson 		    M_NOWAIT | M_ZERO);
7458f002c6cSBruce M Simpson 		if (nims == NULL)
7468f002c6cSBruce M Simpson 			return (-ENOMEM);
7478f002c6cSBruce M Simpson 		nims->im6s_addr = find.im6s_addr;
7488f002c6cSBruce M Simpson 		RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
7498f002c6cSBruce M Simpson 		++inm->in6m_nsrc;
7508f002c6cSBruce M Simpson 		ims = nims;
7518f002c6cSBruce M Simpson 	}
7528f002c6cSBruce M Simpson 
7538f002c6cSBruce M Simpson 	/*
7548f002c6cSBruce M Simpson 	 * Mark the source as recorded and update the recorded
7558f002c6cSBruce M Simpson 	 * source count.
7568f002c6cSBruce M Simpson 	 */
7578f002c6cSBruce M Simpson 	++ims->im6s_stp;
7588f002c6cSBruce M Simpson 	++inm->in6m_st[1].iss_rec;
7598f002c6cSBruce M Simpson 
7608f002c6cSBruce M Simpson 	return (1);
7618f002c6cSBruce M Simpson }
7628f002c6cSBruce M Simpson 
7638f002c6cSBruce M Simpson /*
7648f002c6cSBruce M Simpson  * Return a pointer to an in6_msource owned by an in6_mfilter,
7658f002c6cSBruce M Simpson  * given its source address.
7668f002c6cSBruce M Simpson  * Lazy-allocate if needed. If this is a new entry its filter state is
7678f002c6cSBruce M Simpson  * undefined at t0.
7688f002c6cSBruce M Simpson  *
7698f002c6cSBruce M Simpson  * imf is the filter set being modified.
7708f002c6cSBruce M Simpson  * addr is the source address.
7718f002c6cSBruce M Simpson  *
7728f002c6cSBruce M Simpson  * SMPng: May be called with locks held; malloc must not block.
7738f002c6cSBruce M Simpson  */
7748f002c6cSBruce M Simpson static int
7758f002c6cSBruce M Simpson im6f_get_source(struct in6_mfilter *imf, const struct sockaddr_in6 *psin,
7768f002c6cSBruce M Simpson     struct in6_msource **plims)
7778f002c6cSBruce M Simpson {
7788f002c6cSBruce M Simpson 	struct ip6_msource	 find;
7798f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *nims;
7808f002c6cSBruce M Simpson 	struct in6_msource	*lims;
7818f002c6cSBruce M Simpson 	int			 error;
7828f002c6cSBruce M Simpson 
7838f002c6cSBruce M Simpson 	error = 0;
7848f002c6cSBruce M Simpson 	ims = NULL;
7858f002c6cSBruce M Simpson 	lims = NULL;
7868f002c6cSBruce M Simpson 
7878f002c6cSBruce M Simpson 	find.im6s_addr = psin->sin6_addr;
7888f002c6cSBruce M Simpson 	ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
7898f002c6cSBruce M Simpson 	lims = (struct in6_msource *)ims;
7908f002c6cSBruce M Simpson 	if (lims == NULL) {
7918f002c6cSBruce M Simpson 		if (imf->im6f_nsrc == in6_mcast_maxsocksrc)
7928f002c6cSBruce M Simpson 			return (ENOSPC);
7938f002c6cSBruce M Simpson 		nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
7948f002c6cSBruce M Simpson 		    M_NOWAIT | M_ZERO);
7958f002c6cSBruce M Simpson 		if (nims == NULL)
7968f002c6cSBruce M Simpson 			return (ENOMEM);
7978f002c6cSBruce M Simpson 		lims = (struct in6_msource *)nims;
7988f002c6cSBruce M Simpson 		lims->im6s_addr = find.im6s_addr;
7998f002c6cSBruce M Simpson 		lims->im6sl_st[0] = MCAST_UNDEFINED;
8008f002c6cSBruce M Simpson 		RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
8018f002c6cSBruce M Simpson 		++imf->im6f_nsrc;
8028f002c6cSBruce M Simpson 	}
8038f002c6cSBruce M Simpson 
8048f002c6cSBruce M Simpson 	*plims = lims;
8058f002c6cSBruce M Simpson 
8068f002c6cSBruce M Simpson 	return (error);
8078f002c6cSBruce M Simpson }
8088f002c6cSBruce M Simpson 
8098f002c6cSBruce M Simpson /*
8108f002c6cSBruce M Simpson  * Graft a source entry into an existing socket-layer filter set,
8118f002c6cSBruce M Simpson  * maintaining any required invariants and checking allocations.
8128f002c6cSBruce M Simpson  *
8138f002c6cSBruce M Simpson  * The source is marked as being in the new filter mode at t1.
8148f002c6cSBruce M Simpson  *
8158f002c6cSBruce M Simpson  * Return the pointer to the new node, otherwise return NULL.
8168f002c6cSBruce M Simpson  */
8178f002c6cSBruce M Simpson static struct in6_msource *
8188f002c6cSBruce M Simpson im6f_graft(struct in6_mfilter *imf, const uint8_t st1,
8198f002c6cSBruce M Simpson     const struct sockaddr_in6 *psin)
8208f002c6cSBruce M Simpson {
8218f002c6cSBruce M Simpson 	struct ip6_msource	*nims;
8228f002c6cSBruce M Simpson 	struct in6_msource	*lims;
8238f002c6cSBruce M Simpson 
8248f002c6cSBruce M Simpson 	nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
8258f002c6cSBruce M Simpson 	    M_NOWAIT | M_ZERO);
8268f002c6cSBruce M Simpson 	if (nims == NULL)
8278f002c6cSBruce M Simpson 		return (NULL);
8288f002c6cSBruce M Simpson 	lims = (struct in6_msource *)nims;
8298f002c6cSBruce M Simpson 	lims->im6s_addr = psin->sin6_addr;
8308f002c6cSBruce M Simpson 	lims->im6sl_st[0] = MCAST_UNDEFINED;
8318f002c6cSBruce M Simpson 	lims->im6sl_st[1] = st1;
8328f002c6cSBruce M Simpson 	RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
8338f002c6cSBruce M Simpson 	++imf->im6f_nsrc;
8348f002c6cSBruce M Simpson 
8358f002c6cSBruce M Simpson 	return (lims);
8368f002c6cSBruce M Simpson }
8378f002c6cSBruce M Simpson 
8388f002c6cSBruce M Simpson /*
8398f002c6cSBruce M Simpson  * Prune a source entry from an existing socket-layer filter set,
8408f002c6cSBruce M Simpson  * maintaining any required invariants and checking allocations.
8418f002c6cSBruce M Simpson  *
8428f002c6cSBruce M Simpson  * The source is marked as being left at t1, it is not freed.
8438f002c6cSBruce M Simpson  *
8448f002c6cSBruce M Simpson  * Return 0 if no error occurred, otherwise return an errno value.
8458f002c6cSBruce M Simpson  */
8468f002c6cSBruce M Simpson static int
8478f002c6cSBruce M Simpson im6f_prune(struct in6_mfilter *imf, const struct sockaddr_in6 *psin)
8488f002c6cSBruce M Simpson {
8498f002c6cSBruce M Simpson 	struct ip6_msource	 find;
8508f002c6cSBruce M Simpson 	struct ip6_msource	*ims;
8518f002c6cSBruce M Simpson 	struct in6_msource	*lims;
8528f002c6cSBruce M Simpson 
8538f002c6cSBruce M Simpson 	find.im6s_addr = psin->sin6_addr;
8548f002c6cSBruce M Simpson 	ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
8558f002c6cSBruce M Simpson 	if (ims == NULL)
8568f002c6cSBruce M Simpson 		return (ENOENT);
8578f002c6cSBruce M Simpson 	lims = (struct in6_msource *)ims;
8588f002c6cSBruce M Simpson 	lims->im6sl_st[1] = MCAST_UNDEFINED;
8598f002c6cSBruce M Simpson 	return (0);
8608f002c6cSBruce M Simpson }
8618f002c6cSBruce M Simpson 
8628f002c6cSBruce M Simpson /*
8638f002c6cSBruce M Simpson  * Revert socket-layer filter set deltas at t1 to t0 state.
8648f002c6cSBruce M Simpson  */
8658f002c6cSBruce M Simpson static void
8668f002c6cSBruce M Simpson im6f_rollback(struct in6_mfilter *imf)
8678f002c6cSBruce M Simpson {
8688f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *tims;
8698f002c6cSBruce M Simpson 	struct in6_msource	*lims;
8708f002c6cSBruce M Simpson 
8718f002c6cSBruce M Simpson 	RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
8728f002c6cSBruce M Simpson 		lims = (struct in6_msource *)ims;
8738f002c6cSBruce M Simpson 		if (lims->im6sl_st[0] == lims->im6sl_st[1]) {
8748f002c6cSBruce M Simpson 			/* no change at t1 */
8758f002c6cSBruce M Simpson 			continue;
8768f002c6cSBruce M Simpson 		} else if (lims->im6sl_st[0] != MCAST_UNDEFINED) {
8778f002c6cSBruce M Simpson 			/* revert change to existing source at t1 */
8788f002c6cSBruce M Simpson 			lims->im6sl_st[1] = lims->im6sl_st[0];
8798f002c6cSBruce M Simpson 		} else {
8808f002c6cSBruce M Simpson 			/* revert source added t1 */
8818f002c6cSBruce M Simpson 			CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
8828f002c6cSBruce M Simpson 			RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
8838f002c6cSBruce M Simpson 			free(ims, M_IN6MFILTER);
8848f002c6cSBruce M Simpson 			imf->im6f_nsrc--;
8858f002c6cSBruce M Simpson 		}
8868f002c6cSBruce M Simpson 	}
8878f002c6cSBruce M Simpson 	imf->im6f_st[1] = imf->im6f_st[0];
8888f002c6cSBruce M Simpson }
8898f002c6cSBruce M Simpson 
8908f002c6cSBruce M Simpson /*
8918f002c6cSBruce M Simpson  * Mark socket-layer filter set as INCLUDE {} at t1.
8928f002c6cSBruce M Simpson  */
8938f002c6cSBruce M Simpson static void
8948f002c6cSBruce M Simpson im6f_leave(struct in6_mfilter *imf)
8958f002c6cSBruce M Simpson {
8968f002c6cSBruce M Simpson 	struct ip6_msource	*ims;
8978f002c6cSBruce M Simpson 	struct in6_msource	*lims;
8988f002c6cSBruce M Simpson 
8998f002c6cSBruce M Simpson 	RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
9008f002c6cSBruce M Simpson 		lims = (struct in6_msource *)ims;
9018f002c6cSBruce M Simpson 		lims->im6sl_st[1] = MCAST_UNDEFINED;
9028f002c6cSBruce M Simpson 	}
9038f002c6cSBruce M Simpson 	imf->im6f_st[1] = MCAST_INCLUDE;
9048f002c6cSBruce M Simpson }
9058f002c6cSBruce M Simpson 
9068f002c6cSBruce M Simpson /*
9078f002c6cSBruce M Simpson  * Mark socket-layer filter set deltas as committed.
9088f002c6cSBruce M Simpson  */
9098f002c6cSBruce M Simpson static void
9108f002c6cSBruce M Simpson im6f_commit(struct in6_mfilter *imf)
9118f002c6cSBruce M Simpson {
9128f002c6cSBruce M Simpson 	struct ip6_msource	*ims;
9138f002c6cSBruce M Simpson 	struct in6_msource	*lims;
9148f002c6cSBruce M Simpson 
9158f002c6cSBruce M Simpson 	RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
9168f002c6cSBruce M Simpson 		lims = (struct in6_msource *)ims;
9178f002c6cSBruce M Simpson 		lims->im6sl_st[0] = lims->im6sl_st[1];
9188f002c6cSBruce M Simpson 	}
9198f002c6cSBruce M Simpson 	imf->im6f_st[0] = imf->im6f_st[1];
9208f002c6cSBruce M Simpson }
9218f002c6cSBruce M Simpson 
9228f002c6cSBruce M Simpson /*
9238f002c6cSBruce M Simpson  * Reap unreferenced sources from socket-layer filter set.
9248f002c6cSBruce M Simpson  */
9258f002c6cSBruce M Simpson static void
9268f002c6cSBruce M Simpson im6f_reap(struct in6_mfilter *imf)
9278f002c6cSBruce M Simpson {
9288f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *tims;
9298f002c6cSBruce M Simpson 	struct in6_msource	*lims;
9308f002c6cSBruce M Simpson 
9318f002c6cSBruce M Simpson 	RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
9328f002c6cSBruce M Simpson 		lims = (struct in6_msource *)ims;
9338f002c6cSBruce M Simpson 		if ((lims->im6sl_st[0] == MCAST_UNDEFINED) &&
9348f002c6cSBruce M Simpson 		    (lims->im6sl_st[1] == MCAST_UNDEFINED)) {
9358f002c6cSBruce M Simpson 			CTR2(KTR_MLD, "%s: free lims %p", __func__, ims);
9368f002c6cSBruce M Simpson 			RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
9378f002c6cSBruce M Simpson 			free(ims, M_IN6MFILTER);
9388f002c6cSBruce M Simpson 			imf->im6f_nsrc--;
9398f002c6cSBruce M Simpson 		}
9408f002c6cSBruce M Simpson 	}
9418f002c6cSBruce M Simpson }
9428f002c6cSBruce M Simpson 
9438f002c6cSBruce M Simpson /*
9448f002c6cSBruce M Simpson  * Purge socket-layer filter set.
9458f002c6cSBruce M Simpson  */
9468f002c6cSBruce M Simpson static void
9478f002c6cSBruce M Simpson im6f_purge(struct in6_mfilter *imf)
9488f002c6cSBruce M Simpson {
9498f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *tims;
9508f002c6cSBruce M Simpson 
9518f002c6cSBruce M Simpson 	RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
9528f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
9538f002c6cSBruce M Simpson 		RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
9548f002c6cSBruce M Simpson 		free(ims, M_IN6MFILTER);
9558f002c6cSBruce M Simpson 		imf->im6f_nsrc--;
9568f002c6cSBruce M Simpson 	}
9578f002c6cSBruce M Simpson 	imf->im6f_st[0] = imf->im6f_st[1] = MCAST_UNDEFINED;
9588f002c6cSBruce M Simpson 	KASSERT(RB_EMPTY(&imf->im6f_sources),
9598f002c6cSBruce M Simpson 	    ("%s: im6f_sources not empty", __func__));
9608f002c6cSBruce M Simpson }
9618f002c6cSBruce M Simpson 
9628f002c6cSBruce M Simpson /*
9638f002c6cSBruce M Simpson  * Look up a source filter entry for a multicast group.
9648f002c6cSBruce M Simpson  *
9658f002c6cSBruce M Simpson  * inm is the group descriptor to work with.
9668f002c6cSBruce M Simpson  * addr is the IPv6 address to look up.
9678f002c6cSBruce M Simpson  * noalloc may be non-zero to suppress allocation of sources.
9688f002c6cSBruce M Simpson  * *pims will be set to the address of the retrieved or allocated source.
9698f002c6cSBruce M Simpson  *
9708f002c6cSBruce M Simpson  * SMPng: NOTE: may be called with locks held.
9718f002c6cSBruce M Simpson  * Return 0 if successful, otherwise return a non-zero error code.
9728f002c6cSBruce M Simpson  */
9738f002c6cSBruce M Simpson static int
9748f002c6cSBruce M Simpson in6m_get_source(struct in6_multi *inm, const struct in6_addr *addr,
9758f002c6cSBruce M Simpson     const int noalloc, struct ip6_msource **pims)
9768f002c6cSBruce M Simpson {
9778f002c6cSBruce M Simpson 	struct ip6_msource	 find;
9788f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *nims;
9798f002c6cSBruce M Simpson #ifdef KTR
9808f002c6cSBruce M Simpson 	char			 ip6tbuf[INET6_ADDRSTRLEN];
9818f002c6cSBruce M Simpson #endif
9828f002c6cSBruce M Simpson 
9838f002c6cSBruce M Simpson 	find.im6s_addr = *addr;
9848f002c6cSBruce M Simpson 	ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
9858f002c6cSBruce M Simpson 	if (ims == NULL && !noalloc) {
9868f002c6cSBruce M Simpson 		if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
9878f002c6cSBruce M Simpson 			return (ENOSPC);
9888f002c6cSBruce M Simpson 		nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
9898f002c6cSBruce M Simpson 		    M_NOWAIT | M_ZERO);
9908f002c6cSBruce M Simpson 		if (nims == NULL)
9918f002c6cSBruce M Simpson 			return (ENOMEM);
9928f002c6cSBruce M Simpson 		nims->im6s_addr = *addr;
9938f002c6cSBruce M Simpson 		RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
9948f002c6cSBruce M Simpson 		++inm->in6m_nsrc;
9958f002c6cSBruce M Simpson 		ims = nims;
9968f002c6cSBruce M Simpson 		CTR3(KTR_MLD, "%s: allocated %s as %p", __func__,
9978f002c6cSBruce M Simpson 		    ip6_sprintf(ip6tbuf, addr), ims);
9988f002c6cSBruce M Simpson 	}
9998f002c6cSBruce M Simpson 
10008f002c6cSBruce M Simpson 	*pims = ims;
10018f002c6cSBruce M Simpson 	return (0);
10028f002c6cSBruce M Simpson }
10038f002c6cSBruce M Simpson 
10048f002c6cSBruce M Simpson /*
10058f002c6cSBruce M Simpson  * Merge socket-layer source into MLD-layer source.
10068f002c6cSBruce M Simpson  * If rollback is non-zero, perform the inverse of the merge.
10078f002c6cSBruce M Simpson  */
10088f002c6cSBruce M Simpson static void
10098f002c6cSBruce M Simpson im6s_merge(struct ip6_msource *ims, const struct in6_msource *lims,
10108f002c6cSBruce M Simpson     const int rollback)
10118f002c6cSBruce M Simpson {
10128f002c6cSBruce M Simpson 	int n = rollback ? -1 : 1;
10138f002c6cSBruce M Simpson #ifdef KTR
10148f002c6cSBruce M Simpson 	char ip6tbuf[INET6_ADDRSTRLEN];
10158f002c6cSBruce M Simpson 
10168f002c6cSBruce M Simpson 	ip6_sprintf(ip6tbuf, &lims->im6s_addr);
10178f002c6cSBruce M Simpson #endif
10188f002c6cSBruce M Simpson 
10198f002c6cSBruce M Simpson 	if (lims->im6sl_st[0] == MCAST_EXCLUDE) {
10208f002c6cSBruce M Simpson 		CTR3(KTR_MLD, "%s: t1 ex -= %d on %s", __func__, n, ip6tbuf);
10218f002c6cSBruce M Simpson 		ims->im6s_st[1].ex -= n;
10228f002c6cSBruce M Simpson 	} else if (lims->im6sl_st[0] == MCAST_INCLUDE) {
10238f002c6cSBruce M Simpson 		CTR3(KTR_MLD, "%s: t1 in -= %d on %s", __func__, n, ip6tbuf);
10248f002c6cSBruce M Simpson 		ims->im6s_st[1].in -= n;
10258f002c6cSBruce M Simpson 	}
10268f002c6cSBruce M Simpson 
10278f002c6cSBruce M Simpson 	if (lims->im6sl_st[1] == MCAST_EXCLUDE) {
10288f002c6cSBruce M Simpson 		CTR3(KTR_MLD, "%s: t1 ex += %d on %s", __func__, n, ip6tbuf);
10298f002c6cSBruce M Simpson 		ims->im6s_st[1].ex += n;
10308f002c6cSBruce M Simpson 	} else if (lims->im6sl_st[1] == MCAST_INCLUDE) {
10318f002c6cSBruce M Simpson 		CTR3(KTR_MLD, "%s: t1 in += %d on %s", __func__, n, ip6tbuf);
10328f002c6cSBruce M Simpson 		ims->im6s_st[1].in += n;
10338f002c6cSBruce M Simpson 	}
10348f002c6cSBruce M Simpson }
10358f002c6cSBruce M Simpson 
10368f002c6cSBruce M Simpson /*
10378f002c6cSBruce M Simpson  * Atomically update the global in6_multi state, when a membership's
10388f002c6cSBruce M Simpson  * filter list is being updated in any way.
10398f002c6cSBruce M Simpson  *
10408f002c6cSBruce M Simpson  * imf is the per-inpcb-membership group filter pointer.
10418f002c6cSBruce M Simpson  * A fake imf may be passed for in-kernel consumers.
10428f002c6cSBruce M Simpson  *
10438f002c6cSBruce M Simpson  * XXX This is a candidate for a set-symmetric-difference style loop
10448f002c6cSBruce M Simpson  * which would eliminate the repeated lookup from root of ims nodes,
10458f002c6cSBruce M Simpson  * as they share the same key space.
10468f002c6cSBruce M Simpson  *
10478f002c6cSBruce M Simpson  * If any error occurred this function will back out of refcounts
10488f002c6cSBruce M Simpson  * and return a non-zero value.
10498f002c6cSBruce M Simpson  */
10508f002c6cSBruce M Simpson static int
10518f002c6cSBruce M Simpson in6m_merge(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
10528f002c6cSBruce M Simpson {
10538f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *nims;
10548f002c6cSBruce M Simpson 	struct in6_msource	*lims;
10558f002c6cSBruce M Simpson 	int			 schanged, error;
10568f002c6cSBruce M Simpson 	int			 nsrc0, nsrc1;
10578f002c6cSBruce M Simpson 
10588f002c6cSBruce M Simpson 	schanged = 0;
10598f002c6cSBruce M Simpson 	error = 0;
10608f002c6cSBruce M Simpson 	nsrc1 = nsrc0 = 0;
1061f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK_ASSERT();
10628f002c6cSBruce M Simpson 
10638f002c6cSBruce M Simpson 	/*
10648f002c6cSBruce M Simpson 	 * Update the source filters first, as this may fail.
10658f002c6cSBruce M Simpson 	 * Maintain count of in-mode filters at t0, t1. These are
10668f002c6cSBruce M Simpson 	 * used to work out if we transition into ASM mode or not.
10678f002c6cSBruce M Simpson 	 * Maintain a count of source filters whose state was
10688f002c6cSBruce M Simpson 	 * actually modified by this operation.
10698f002c6cSBruce M Simpson 	 */
10708f002c6cSBruce M Simpson 	RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
10718f002c6cSBruce M Simpson 		lims = (struct in6_msource *)ims;
10728f002c6cSBruce M Simpson 		if (lims->im6sl_st[0] == imf->im6f_st[0]) nsrc0++;
10738f002c6cSBruce M Simpson 		if (lims->im6sl_st[1] == imf->im6f_st[1]) nsrc1++;
10748f002c6cSBruce M Simpson 		if (lims->im6sl_st[0] == lims->im6sl_st[1]) continue;
10758f002c6cSBruce M Simpson 		error = in6m_get_source(inm, &lims->im6s_addr, 0, &nims);
10768f002c6cSBruce M Simpson 		++schanged;
10778f002c6cSBruce M Simpson 		if (error)
10788f002c6cSBruce M Simpson 			break;
10798f002c6cSBruce M Simpson 		im6s_merge(nims, lims, 0);
10808f002c6cSBruce M Simpson 	}
10818f002c6cSBruce M Simpson 	if (error) {
10828f002c6cSBruce M Simpson 		struct ip6_msource *bims;
10838f002c6cSBruce M Simpson 
10848f002c6cSBruce M Simpson 		RB_FOREACH_REVERSE_FROM(ims, ip6_msource_tree, nims) {
10858f002c6cSBruce M Simpson 			lims = (struct in6_msource *)ims;
10868f002c6cSBruce M Simpson 			if (lims->im6sl_st[0] == lims->im6sl_st[1])
10878f002c6cSBruce M Simpson 				continue;
10888f002c6cSBruce M Simpson 			(void)in6m_get_source(inm, &lims->im6s_addr, 1, &bims);
10898f002c6cSBruce M Simpson 			if (bims == NULL)
10908f002c6cSBruce M Simpson 				continue;
10918f002c6cSBruce M Simpson 			im6s_merge(bims, lims, 1);
10928f002c6cSBruce M Simpson 		}
10938f002c6cSBruce M Simpson 		goto out_reap;
10948f002c6cSBruce M Simpson 	}
10958f002c6cSBruce M Simpson 
10968f002c6cSBruce M Simpson 	CTR3(KTR_MLD, "%s: imf filters in-mode: %d at t0, %d at t1",
10978f002c6cSBruce M Simpson 	    __func__, nsrc0, nsrc1);
10988f002c6cSBruce M Simpson 
10998f002c6cSBruce M Simpson 	/* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
11008f002c6cSBruce M Simpson 	if (imf->im6f_st[0] == imf->im6f_st[1] &&
11018f002c6cSBruce M Simpson 	    imf->im6f_st[1] == MCAST_INCLUDE) {
11028f002c6cSBruce M Simpson 		if (nsrc1 == 0) {
11038f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
11048f002c6cSBruce M Simpson 			--inm->in6m_st[1].iss_in;
11058f002c6cSBruce M Simpson 		}
11068f002c6cSBruce M Simpson 	}
11078f002c6cSBruce M Simpson 
11088f002c6cSBruce M Simpson 	/* Handle filter mode transition on socket. */
11098f002c6cSBruce M Simpson 	if (imf->im6f_st[0] != imf->im6f_st[1]) {
11108f002c6cSBruce M Simpson 		CTR3(KTR_MLD, "%s: imf transition %d to %d",
11118f002c6cSBruce M Simpson 		    __func__, imf->im6f_st[0], imf->im6f_st[1]);
11128f002c6cSBruce M Simpson 
11138f002c6cSBruce M Simpson 		if (imf->im6f_st[0] == MCAST_EXCLUDE) {
11148f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: --ex on inm at t1", __func__);
11158f002c6cSBruce M Simpson 			--inm->in6m_st[1].iss_ex;
11168f002c6cSBruce M Simpson 		} else if (imf->im6f_st[0] == MCAST_INCLUDE) {
11178f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
11188f002c6cSBruce M Simpson 			--inm->in6m_st[1].iss_in;
11198f002c6cSBruce M Simpson 		}
11208f002c6cSBruce M Simpson 
11218f002c6cSBruce M Simpson 		if (imf->im6f_st[1] == MCAST_EXCLUDE) {
11228f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: ex++ on inm at t1", __func__);
11238f002c6cSBruce M Simpson 			inm->in6m_st[1].iss_ex++;
11248f002c6cSBruce M Simpson 		} else if (imf->im6f_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
11258f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: in++ on inm at t1", __func__);
11268f002c6cSBruce M Simpson 			inm->in6m_st[1].iss_in++;
11278f002c6cSBruce M Simpson 		}
11288f002c6cSBruce M Simpson 	}
11298f002c6cSBruce M Simpson 
11308f002c6cSBruce M Simpson 	/*
11318f002c6cSBruce M Simpson 	 * Track inm filter state in terms of listener counts.
11328f002c6cSBruce M Simpson 	 * If there are any exclusive listeners, stack-wide
11338f002c6cSBruce M Simpson 	 * membership is exclusive.
11348f002c6cSBruce M Simpson 	 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
11358f002c6cSBruce M Simpson 	 * If no listeners remain, state is undefined at t1,
11368f002c6cSBruce M Simpson 	 * and the MLD lifecycle for this group should finish.
11378f002c6cSBruce M Simpson 	 */
11388f002c6cSBruce M Simpson 	if (inm->in6m_st[1].iss_ex > 0) {
11398f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: transition to EX", __func__);
11408f002c6cSBruce M Simpson 		inm->in6m_st[1].iss_fmode = MCAST_EXCLUDE;
11418f002c6cSBruce M Simpson 	} else if (inm->in6m_st[1].iss_in > 0) {
11428f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: transition to IN", __func__);
11438f002c6cSBruce M Simpson 		inm->in6m_st[1].iss_fmode = MCAST_INCLUDE;
11448f002c6cSBruce M Simpson 	} else {
11458f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: transition to UNDEF", __func__);
11468f002c6cSBruce M Simpson 		inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
11478f002c6cSBruce M Simpson 	}
11488f002c6cSBruce M Simpson 
11498f002c6cSBruce M Simpson 	/* Decrement ASM listener count on transition out of ASM mode. */
11508f002c6cSBruce M Simpson 	if (imf->im6f_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
11518f002c6cSBruce M Simpson 		if ((imf->im6f_st[1] != MCAST_EXCLUDE) ||
1152bd745936SEnji Cooper 		    (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 > 0)) {
11538f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: --asm on inm at t1", __func__);
11548f002c6cSBruce M Simpson 			--inm->in6m_st[1].iss_asm;
11558f002c6cSBruce M Simpson 		}
1156bd745936SEnji Cooper 	}
11578f002c6cSBruce M Simpson 
11588f002c6cSBruce M Simpson 	/* Increment ASM listener count on transition to ASM mode. */
11598f002c6cSBruce M Simpson 	if (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
11608f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: asm++ on inm at t1", __func__);
11618f002c6cSBruce M Simpson 		inm->in6m_st[1].iss_asm++;
11628f002c6cSBruce M Simpson 	}
11638f002c6cSBruce M Simpson 
11648f002c6cSBruce M Simpson 	CTR3(KTR_MLD, "%s: merged imf %p to inm %p", __func__, imf, inm);
11658f002c6cSBruce M Simpson 	in6m_print(inm);
11668f002c6cSBruce M Simpson 
11678f002c6cSBruce M Simpson out_reap:
11688f002c6cSBruce M Simpson 	if (schanged > 0) {
11698f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: sources changed; reaping", __func__);
11708f002c6cSBruce M Simpson 		in6m_reap(inm);
11718f002c6cSBruce M Simpson 	}
11728f002c6cSBruce M Simpson 	return (error);
11738f002c6cSBruce M Simpson }
11748f002c6cSBruce M Simpson 
11758f002c6cSBruce M Simpson /*
11768f002c6cSBruce M Simpson  * Mark an in6_multi's filter set deltas as committed.
11778f002c6cSBruce M Simpson  * Called by MLD after a state change has been enqueued.
11788f002c6cSBruce M Simpson  */
11798f002c6cSBruce M Simpson void
11808f002c6cSBruce M Simpson in6m_commit(struct in6_multi *inm)
11818f002c6cSBruce M Simpson {
11828f002c6cSBruce M Simpson 	struct ip6_msource	*ims;
11838f002c6cSBruce M Simpson 
11848f002c6cSBruce M Simpson 	CTR2(KTR_MLD, "%s: commit inm %p", __func__, inm);
11858f002c6cSBruce M Simpson 	CTR1(KTR_MLD, "%s: pre commit:", __func__);
11868f002c6cSBruce M Simpson 	in6m_print(inm);
11878f002c6cSBruce M Simpson 
11888f002c6cSBruce M Simpson 	RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
11898f002c6cSBruce M Simpson 		ims->im6s_st[0] = ims->im6s_st[1];
11908f002c6cSBruce M Simpson 	}
11918f002c6cSBruce M Simpson 	inm->in6m_st[0] = inm->in6m_st[1];
11928f002c6cSBruce M Simpson }
11938f002c6cSBruce M Simpson 
11948f002c6cSBruce M Simpson /*
11958f002c6cSBruce M Simpson  * Reap unreferenced nodes from an in6_multi's filter set.
11968f002c6cSBruce M Simpson  */
11978f002c6cSBruce M Simpson static void
11988f002c6cSBruce M Simpson in6m_reap(struct in6_multi *inm)
11998f002c6cSBruce M Simpson {
12008f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *tims;
12018f002c6cSBruce M Simpson 
12028f002c6cSBruce M Simpson 	RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
12038f002c6cSBruce M Simpson 		if (ims->im6s_st[0].ex > 0 || ims->im6s_st[0].in > 0 ||
12048f002c6cSBruce M Simpson 		    ims->im6s_st[1].ex > 0 || ims->im6s_st[1].in > 0 ||
12058f002c6cSBruce M Simpson 		    ims->im6s_stp != 0)
12068f002c6cSBruce M Simpson 			continue;
12078f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
12088f002c6cSBruce M Simpson 		RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
12098f002c6cSBruce M Simpson 		free(ims, M_IP6MSOURCE);
12108f002c6cSBruce M Simpson 		inm->in6m_nsrc--;
12118f002c6cSBruce M Simpson 	}
12128f002c6cSBruce M Simpson }
12138f002c6cSBruce M Simpson 
12148f002c6cSBruce M Simpson /*
12158f002c6cSBruce M Simpson  * Purge all source nodes from an in6_multi's filter set.
12168f002c6cSBruce M Simpson  */
12178f002c6cSBruce M Simpson static void
12188f002c6cSBruce M Simpson in6m_purge(struct in6_multi *inm)
12198f002c6cSBruce M Simpson {
12208f002c6cSBruce M Simpson 	struct ip6_msource	*ims, *tims;
12218f002c6cSBruce M Simpson 
12228f002c6cSBruce M Simpson 	RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
12238f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
12248f002c6cSBruce M Simpson 		RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
12258f002c6cSBruce M Simpson 		free(ims, M_IP6MSOURCE);
12268f002c6cSBruce M Simpson 		inm->in6m_nsrc--;
12278f002c6cSBruce M Simpson 	}
12283c268b3aSAndrey V. Elsukov 	/* Free state-change requests that might be queued. */
1229a99c84d4SGleb Smirnoff 	mbufq_drain(&inm->in6m_scq);
12308f002c6cSBruce M Simpson }
12318f002c6cSBruce M Simpson 
12328f002c6cSBruce M Simpson /*
12338f002c6cSBruce M Simpson  * Join a multicast address w/o sources.
12348f002c6cSBruce M Simpson  * KAME compatibility entry point.
12358f002c6cSBruce M Simpson  *
12368f002c6cSBruce M Simpson  * SMPng: Assume no mc locks held by caller.
12378f002c6cSBruce M Simpson  */
12388f002c6cSBruce M Simpson int
1239f3e1324bSStephen Hurd in6_joingroup(struct ifnet *ifp, const struct in6_addr *mcaddr,
12408f002c6cSBruce M Simpson     /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
12418f002c6cSBruce M Simpson     const int delay)
12428f002c6cSBruce M Simpson {
12438f002c6cSBruce M Simpson 	int error;
12448f002c6cSBruce M Simpson 
12458f002c6cSBruce M Simpson 	IN6_MULTI_LOCK();
1246f3e1324bSStephen Hurd 	error = in6_joingroup_locked(ifp, mcaddr, NULL, pinm, delay);
12478f002c6cSBruce M Simpson 	IN6_MULTI_UNLOCK();
12488f002c6cSBruce M Simpson 	return (error);
12498f002c6cSBruce M Simpson }
12508f002c6cSBruce M Simpson 
12518f002c6cSBruce M Simpson /*
12528f002c6cSBruce M Simpson  * Join a multicast group; real entry point.
12538f002c6cSBruce M Simpson  *
12548f002c6cSBruce M Simpson  * Only preserves atomicity at inm level.
12558f002c6cSBruce M Simpson  * NOTE: imf argument cannot be const due to sys/tree.h limitations.
12568f002c6cSBruce M Simpson  *
12578f002c6cSBruce M Simpson  * If the MLD downcall fails, the group is not joined, and an error
12588f002c6cSBruce M Simpson  * code is returned.
12598f002c6cSBruce M Simpson  */
12608f002c6cSBruce M Simpson int
1261f3e1324bSStephen Hurd in6_joingroup_locked(struct ifnet *ifp, const struct in6_addr *mcaddr,
12628f002c6cSBruce M Simpson     /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
12638f002c6cSBruce M Simpson     const int delay)
12648f002c6cSBruce M Simpson {
12658f002c6cSBruce M Simpson 	struct in6_mfilter	 timf;
12668f002c6cSBruce M Simpson 	struct in6_multi	*inm;
1267f3e1324bSStephen Hurd 	struct ifmultiaddr *ifma;
12688f002c6cSBruce M Simpson 	int			 error;
12698f002c6cSBruce M Simpson #ifdef KTR
12708f002c6cSBruce M Simpson 	char			 ip6tbuf[INET6_ADDRSTRLEN];
12718f002c6cSBruce M Simpson #endif
12728f002c6cSBruce M Simpson 
127329dc7bc6SBruce M Simpson 	/*
127429dc7bc6SBruce M Simpson 	 * Sanity: Check scope zone ID was set for ifp, if and
127529dc7bc6SBruce M Simpson 	 * only if group is scoped to an interface.
127629dc7bc6SBruce M Simpson 	 */
127729dc7bc6SBruce M Simpson 	KASSERT(IN6_IS_ADDR_MULTICAST(mcaddr),
127829dc7bc6SBruce M Simpson 	    ("%s: not a multicast address", __func__));
127929dc7bc6SBruce M Simpson 	if (IN6_IS_ADDR_MC_LINKLOCAL(mcaddr) ||
128029dc7bc6SBruce M Simpson 	    IN6_IS_ADDR_MC_INTFACELOCAL(mcaddr)) {
128129dc7bc6SBruce M Simpson 		KASSERT(mcaddr->s6_addr16[1] != 0,
128229dc7bc6SBruce M Simpson 		    ("%s: scope zone ID not set", __func__));
128329dc7bc6SBruce M Simpson 	}
128429dc7bc6SBruce M Simpson 
12858f002c6cSBruce M Simpson 	IN6_MULTI_LOCK_ASSERT();
1286f3e1324bSStephen Hurd 	IN6_MULTI_LIST_UNLOCK_ASSERT();
12878f002c6cSBruce M Simpson 
12888f002c6cSBruce M Simpson 	CTR4(KTR_MLD, "%s: join %s on %p(%s))", __func__,
1289e74966f6SAndrey V. Elsukov 	    ip6_sprintf(ip6tbuf, mcaddr), ifp, if_name(ifp));
12908f002c6cSBruce M Simpson 
12918f002c6cSBruce M Simpson 	error = 0;
12928f002c6cSBruce M Simpson 	inm = NULL;
12938f002c6cSBruce M Simpson 
12948f002c6cSBruce M Simpson 	/*
12958f002c6cSBruce M Simpson 	 * If no imf was specified (i.e. kernel consumer),
12968f002c6cSBruce M Simpson 	 * fake one up and assume it is an ASM join.
12978f002c6cSBruce M Simpson 	 */
12988f002c6cSBruce M Simpson 	if (imf == NULL) {
12998f002c6cSBruce M Simpson 		im6f_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
13008f002c6cSBruce M Simpson 		imf = &timf;
13018f002c6cSBruce M Simpson 	}
1302f3e1324bSStephen Hurd 	error = in6_getmulti(ifp, mcaddr, &inm);
13038f002c6cSBruce M Simpson 	if (error) {
1304f3e1324bSStephen Hurd 		CTR1(KTR_MLD, "%s: in6_getmulti() failure", __func__);
13058f002c6cSBruce M Simpson 		return (error);
13068f002c6cSBruce M Simpson 	}
13078f002c6cSBruce M Simpson 
1308f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK();
13098f002c6cSBruce M Simpson 	CTR1(KTR_MLD, "%s: merge inm state", __func__);
13108f002c6cSBruce M Simpson 	error = in6m_merge(inm, imf);
13118f002c6cSBruce M Simpson 	if (error) {
13128f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
13138f002c6cSBruce M Simpson 		goto out_in6m_release;
13148f002c6cSBruce M Simpson 	}
13158f002c6cSBruce M Simpson 
13168f002c6cSBruce M Simpson 	CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
13178f002c6cSBruce M Simpson 	error = mld_change_state(inm, delay);
13188f002c6cSBruce M Simpson 	if (error) {
13198f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: failed to update source", __func__);
13208f002c6cSBruce M Simpson 		goto out_in6m_release;
13218f002c6cSBruce M Simpson 	}
13228f002c6cSBruce M Simpson 
13238f002c6cSBruce M Simpson out_in6m_release:
13248f002c6cSBruce M Simpson 	if (error) {
1325a68cc388SGleb Smirnoff 		struct epoch_tracker et;
1326a68cc388SGleb Smirnoff 
13278f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1328a68cc388SGleb Smirnoff 		NET_EPOCH_ENTER(et);
1329d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1330f3e1324bSStephen Hurd 			if (ifma->ifma_protospec == inm) {
1331f3e1324bSStephen Hurd 				ifma->ifma_protospec = NULL;
1332f3e1324bSStephen Hurd 				break;
1333f3e1324bSStephen Hurd 			}
1334f3e1324bSStephen Hurd 		}
1335f9be0386SMatt Macy 		in6m_disconnect(inm);
1336f3e1324bSStephen Hurd 		in6m_release_deferred(inm);
1337a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
13388f002c6cSBruce M Simpson 	} else {
13398f002c6cSBruce M Simpson 		*pinm = inm;
13408f002c6cSBruce M Simpson 	}
1341f3e1324bSStephen Hurd 	IN6_MULTI_LIST_UNLOCK();
13428f002c6cSBruce M Simpson 	return (error);
13438f002c6cSBruce M Simpson }
13448f002c6cSBruce M Simpson 
13458f002c6cSBruce M Simpson /*
13468f002c6cSBruce M Simpson  * Leave a multicast group; unlocked entry point.
13478f002c6cSBruce M Simpson  */
13488f002c6cSBruce M Simpson int
1349f3e1324bSStephen Hurd in6_leavegroup(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
13508f002c6cSBruce M Simpson {
13518f002c6cSBruce M Simpson 	int error;
13528f002c6cSBruce M Simpson 
13538f002c6cSBruce M Simpson 	IN6_MULTI_LOCK();
1354f3e1324bSStephen Hurd 	error = in6_leavegroup_locked(inm, imf);
13558f002c6cSBruce M Simpson 	IN6_MULTI_UNLOCK();
13568f002c6cSBruce M Simpson 	return (error);
13578f002c6cSBruce M Simpson }
13588f002c6cSBruce M Simpson 
13598f002c6cSBruce M Simpson /*
13608f002c6cSBruce M Simpson  * Leave a multicast group; real entry point.
13618f002c6cSBruce M Simpson  * All source filters will be expunged.
13628f002c6cSBruce M Simpson  *
13638f002c6cSBruce M Simpson  * Only preserves atomicity at inm level.
13648f002c6cSBruce M Simpson  *
13658f002c6cSBruce M Simpson  * Holding the write lock for the INP which contains imf
13668f002c6cSBruce M Simpson  * is highly advisable. We can't assert for it as imf does not
13678f002c6cSBruce M Simpson  * contain a back-pointer to the owning inp.
13688f002c6cSBruce M Simpson  *
13698f002c6cSBruce M Simpson  * Note: This is not the same as in6m_release(*) as this function also
13708f002c6cSBruce M Simpson  * makes a state change downcall into MLD.
13718f002c6cSBruce M Simpson  */
13728f002c6cSBruce M Simpson int
1373f3e1324bSStephen Hurd in6_leavegroup_locked(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
13748f002c6cSBruce M Simpson {
13758f002c6cSBruce M Simpson 	struct in6_mfilter	 timf;
1376f3e1324bSStephen Hurd 	struct ifnet *ifp;
13778f002c6cSBruce M Simpson 	int			 error;
13788f002c6cSBruce M Simpson #ifdef KTR
13798f002c6cSBruce M Simpson 	char			 ip6tbuf[INET6_ADDRSTRLEN];
13808f002c6cSBruce M Simpson #endif
13818f002c6cSBruce M Simpson 
13828f002c6cSBruce M Simpson 	error = 0;
13838f002c6cSBruce M Simpson 
13848f002c6cSBruce M Simpson 	IN6_MULTI_LOCK_ASSERT();
13858f002c6cSBruce M Simpson 
13868f002c6cSBruce M Simpson 	CTR5(KTR_MLD, "%s: leave inm %p, %s/%s, imf %p", __func__,
13878f002c6cSBruce M Simpson 	    inm, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1388e74966f6SAndrey V. Elsukov 	    (in6m_is_ifp_detached(inm) ? "null" : if_name(inm->in6m_ifp)),
13898f002c6cSBruce M Simpson 	    imf);
13908f002c6cSBruce M Simpson 
13918f002c6cSBruce M Simpson 	/*
13928f002c6cSBruce M Simpson 	 * If no imf was specified (i.e. kernel consumer),
13938f002c6cSBruce M Simpson 	 * fake one up and assume it is an ASM join.
13948f002c6cSBruce M Simpson 	 */
13958f002c6cSBruce M Simpson 	if (imf == NULL) {
13968f002c6cSBruce M Simpson 		im6f_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
13978f002c6cSBruce M Simpson 		imf = &timf;
13988f002c6cSBruce M Simpson 	}
13998f002c6cSBruce M Simpson 
14008f002c6cSBruce M Simpson 	/*
14018f002c6cSBruce M Simpson 	 * Begin state merge transaction at MLD layer.
14028f002c6cSBruce M Simpson 	 *
14038f002c6cSBruce M Simpson 	 * As this particular invocation should not cause any memory
14048f002c6cSBruce M Simpson 	 * to be allocated, and there is no opportunity to roll back
14058f002c6cSBruce M Simpson 	 * the transaction, it MUST NOT fail.
14068f002c6cSBruce M Simpson 	 */
1407f3e1324bSStephen Hurd 
1408f3e1324bSStephen Hurd 	ifp = inm->in6m_ifp;
1409f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK();
14108f002c6cSBruce M Simpson 	CTR1(KTR_MLD, "%s: merge inm state", __func__);
14118f002c6cSBruce M Simpson 	error = in6m_merge(inm, imf);
14128f002c6cSBruce M Simpson 	KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
14138f002c6cSBruce M Simpson 
14148f002c6cSBruce M Simpson 	CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1415f9be0386SMatt Macy 	error = 0;
1416f9be0386SMatt Macy 	if (ifp)
14178f002c6cSBruce M Simpson 		error = mld_change_state(inm, 0);
14188f002c6cSBruce M Simpson 	if (error)
14198f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
14208f002c6cSBruce M Simpson 
14218f002c6cSBruce M Simpson 	CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1422f3e1324bSStephen Hurd 	if (ifp)
1423b6f6f880SMatt Macy 		IF_ADDR_WLOCK(ifp);
1424f9be0386SMatt Macy 	if (inm->in6m_refcount == 1 && inm->in6m_ifp != NULL)
1425f9be0386SMatt Macy 		in6m_disconnect(inm);
1426f3e1324bSStephen Hurd 	in6m_release_deferred(inm);
1427f3e1324bSStephen Hurd 	if (ifp)
1428b6f6f880SMatt Macy 		IF_ADDR_WUNLOCK(ifp);
1429f3e1324bSStephen Hurd 	IN6_MULTI_LIST_UNLOCK();
14308f002c6cSBruce M Simpson 
14318f002c6cSBruce M Simpson 	return (error);
14328f002c6cSBruce M Simpson }
14338f002c6cSBruce M Simpson 
1434f3e1324bSStephen Hurd 
14358f002c6cSBruce M Simpson /*
14368f002c6cSBruce M Simpson  * Block or unblock an ASM multicast source on an inpcb.
14378f002c6cSBruce M Simpson  * This implements the delta-based API described in RFC 3678.
14388f002c6cSBruce M Simpson  *
14398f002c6cSBruce M Simpson  * The delta-based API applies only to exclusive-mode memberships.
14408f002c6cSBruce M Simpson  * An MLD downcall will be performed.
14418f002c6cSBruce M Simpson  *
14428f002c6cSBruce M Simpson  * SMPng: NOTE: Must take Giant as a join may create a new ifma.
14438f002c6cSBruce M Simpson  *
14448f002c6cSBruce M Simpson  * Return 0 if successful, otherwise return an appropriate error code.
14458f002c6cSBruce M Simpson  */
14468f002c6cSBruce M Simpson static int
14478f002c6cSBruce M Simpson in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
14488f002c6cSBruce M Simpson {
14498f002c6cSBruce M Simpson 	struct group_source_req		 gsr;
14508f002c6cSBruce M Simpson 	sockunion_t			*gsa, *ssa;
14518f002c6cSBruce M Simpson 	struct ifnet			*ifp;
14528f002c6cSBruce M Simpson 	struct in6_mfilter		*imf;
14538f002c6cSBruce M Simpson 	struct ip6_moptions		*imo;
14548f002c6cSBruce M Simpson 	struct in6_msource		*ims;
14558f002c6cSBruce M Simpson 	struct in6_multi			*inm;
14568f002c6cSBruce M Simpson 	size_t				 idx;
14578f002c6cSBruce M Simpson 	uint16_t			 fmode;
14588f002c6cSBruce M Simpson 	int				 error, doblock;
14598f002c6cSBruce M Simpson #ifdef KTR
14608f002c6cSBruce M Simpson 	char				 ip6tbuf[INET6_ADDRSTRLEN];
14618f002c6cSBruce M Simpson #endif
14628f002c6cSBruce M Simpson 
14638f002c6cSBruce M Simpson 	ifp = NULL;
14648f002c6cSBruce M Simpson 	error = 0;
14658f002c6cSBruce M Simpson 	doblock = 0;
14668f002c6cSBruce M Simpson 
14678f002c6cSBruce M Simpson 	memset(&gsr, 0, sizeof(struct group_source_req));
14688f002c6cSBruce M Simpson 	gsa = (sockunion_t *)&gsr.gsr_group;
14698f002c6cSBruce M Simpson 	ssa = (sockunion_t *)&gsr.gsr_source;
14708f002c6cSBruce M Simpson 
14718f002c6cSBruce M Simpson 	switch (sopt->sopt_name) {
14728f002c6cSBruce M Simpson 	case MCAST_BLOCK_SOURCE:
14738f002c6cSBruce M Simpson 	case MCAST_UNBLOCK_SOURCE:
14748f002c6cSBruce M Simpson 		error = sooptcopyin(sopt, &gsr,
14758f002c6cSBruce M Simpson 		    sizeof(struct group_source_req),
14768f002c6cSBruce M Simpson 		    sizeof(struct group_source_req));
14778f002c6cSBruce M Simpson 		if (error)
14788f002c6cSBruce M Simpson 			return (error);
14798f002c6cSBruce M Simpson 
14808f002c6cSBruce M Simpson 		if (gsa->sin6.sin6_family != AF_INET6 ||
14818f002c6cSBruce M Simpson 		    gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
14828f002c6cSBruce M Simpson 			return (EINVAL);
14838f002c6cSBruce M Simpson 
14848f002c6cSBruce M Simpson 		if (ssa->sin6.sin6_family != AF_INET6 ||
14858f002c6cSBruce M Simpson 		    ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
14868f002c6cSBruce M Simpson 			return (EINVAL);
14878f002c6cSBruce M Simpson 
14888f002c6cSBruce M Simpson 		if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
14898f002c6cSBruce M Simpson 			return (EADDRNOTAVAIL);
14908f002c6cSBruce M Simpson 
14918f002c6cSBruce M Simpson 		ifp = ifnet_byindex(gsr.gsr_interface);
14928f002c6cSBruce M Simpson 
14938f002c6cSBruce M Simpson 		if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
14948f002c6cSBruce M Simpson 			doblock = 1;
14958f002c6cSBruce M Simpson 		break;
14968f002c6cSBruce M Simpson 
14978f002c6cSBruce M Simpson 	default:
14988f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: unknown sopt_name %d",
14998f002c6cSBruce M Simpson 		    __func__, sopt->sopt_name);
15008f002c6cSBruce M Simpson 		return (EOPNOTSUPP);
15018f002c6cSBruce M Simpson 		break;
15028f002c6cSBruce M Simpson 	}
15038f002c6cSBruce M Simpson 
15048f002c6cSBruce M Simpson 	if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
15058f002c6cSBruce M Simpson 		return (EINVAL);
15068f002c6cSBruce M Simpson 
150729dc7bc6SBruce M Simpson 	(void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
150829dc7bc6SBruce M Simpson 
15098f002c6cSBruce M Simpson 	/*
15108f002c6cSBruce M Simpson 	 * Check if we are actually a member of this group.
15118f002c6cSBruce M Simpson 	 */
15128f002c6cSBruce M Simpson 	imo = in6p_findmoptions(inp);
15138f002c6cSBruce M Simpson 	idx = im6o_match_group(imo, ifp, &gsa->sa);
15148f002c6cSBruce M Simpson 	if (idx == -1 || imo->im6o_mfilters == NULL) {
15158f002c6cSBruce M Simpson 		error = EADDRNOTAVAIL;
15168f002c6cSBruce M Simpson 		goto out_in6p_locked;
15178f002c6cSBruce M Simpson 	}
15188f002c6cSBruce M Simpson 
15198f002c6cSBruce M Simpson 	KASSERT(imo->im6o_mfilters != NULL,
15208f002c6cSBruce M Simpson 	    ("%s: im6o_mfilters not allocated", __func__));
15218f002c6cSBruce M Simpson 	imf = &imo->im6o_mfilters[idx];
15228f002c6cSBruce M Simpson 	inm = imo->im6o_membership[idx];
15238f002c6cSBruce M Simpson 
15248f002c6cSBruce M Simpson 	/*
15258f002c6cSBruce M Simpson 	 * Attempting to use the delta-based API on an
15268f002c6cSBruce M Simpson 	 * non exclusive-mode membership is an error.
15278f002c6cSBruce M Simpson 	 */
15288f002c6cSBruce M Simpson 	fmode = imf->im6f_st[0];
15298f002c6cSBruce M Simpson 	if (fmode != MCAST_EXCLUDE) {
15308f002c6cSBruce M Simpson 		error = EINVAL;
15318f002c6cSBruce M Simpson 		goto out_in6p_locked;
15328f002c6cSBruce M Simpson 	}
15338f002c6cSBruce M Simpson 
15348f002c6cSBruce M Simpson 	/*
15358f002c6cSBruce M Simpson 	 * Deal with error cases up-front:
15368f002c6cSBruce M Simpson 	 *  Asked to block, but already blocked; or
15378f002c6cSBruce M Simpson 	 *  Asked to unblock, but nothing to unblock.
15388f002c6cSBruce M Simpson 	 * If adding a new block entry, allocate it.
15398f002c6cSBruce M Simpson 	 */
15408f002c6cSBruce M Simpson 	ims = im6o_match_source(imo, idx, &ssa->sa);
15418f002c6cSBruce M Simpson 	if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
15428f002c6cSBruce M Simpson 		CTR3(KTR_MLD, "%s: source %s %spresent", __func__,
15438f002c6cSBruce M Simpson 		    ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
15448f002c6cSBruce M Simpson 		    doblock ? "" : "not ");
15458f002c6cSBruce M Simpson 		error = EADDRNOTAVAIL;
15468f002c6cSBruce M Simpson 		goto out_in6p_locked;
15478f002c6cSBruce M Simpson 	}
15488f002c6cSBruce M Simpson 
15498f002c6cSBruce M Simpson 	INP_WLOCK_ASSERT(inp);
15508f002c6cSBruce M Simpson 
15518f002c6cSBruce M Simpson 	/*
15528f002c6cSBruce M Simpson 	 * Begin state merge transaction at socket layer.
15538f002c6cSBruce M Simpson 	 */
15548f002c6cSBruce M Simpson 	if (doblock) {
15558f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: %s source", __func__, "block");
15568f002c6cSBruce M Simpson 		ims = im6f_graft(imf, fmode, &ssa->sin6);
15578f002c6cSBruce M Simpson 		if (ims == NULL)
15588f002c6cSBruce M Simpson 			error = ENOMEM;
15598f002c6cSBruce M Simpson 	} else {
15608f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
15618f002c6cSBruce M Simpson 		error = im6f_prune(imf, &ssa->sin6);
15628f002c6cSBruce M Simpson 	}
15638f002c6cSBruce M Simpson 
15648f002c6cSBruce M Simpson 	if (error) {
15658f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: merge imf state failed", __func__);
15668f002c6cSBruce M Simpson 		goto out_im6f_rollback;
15678f002c6cSBruce M Simpson 	}
15688f002c6cSBruce M Simpson 
15698f002c6cSBruce M Simpson 	/*
15708f002c6cSBruce M Simpson 	 * Begin state merge transaction at MLD layer.
15718f002c6cSBruce M Simpson 	 */
1572f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK();
15738f002c6cSBruce M Simpson 	CTR1(KTR_MLD, "%s: merge inm state", __func__);
15748f002c6cSBruce M Simpson 	error = in6m_merge(inm, imf);
15754fd91336SAndrey V. Elsukov 	if (error)
15768f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
15774fd91336SAndrey V. Elsukov 	else {
15788f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
15798f002c6cSBruce M Simpson 		error = mld_change_state(inm, 0);
15808f002c6cSBruce M Simpson 		if (error)
15818f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
15824fd91336SAndrey V. Elsukov 	}
15838f002c6cSBruce M Simpson 
1584f3e1324bSStephen Hurd 	IN6_MULTI_LIST_UNLOCK();
15858f002c6cSBruce M Simpson 
15868f002c6cSBruce M Simpson out_im6f_rollback:
15878f002c6cSBruce M Simpson 	if (error)
15888f002c6cSBruce M Simpson 		im6f_rollback(imf);
15898f002c6cSBruce M Simpson 	else
15908f002c6cSBruce M Simpson 		im6f_commit(imf);
15918f002c6cSBruce M Simpson 
15928f002c6cSBruce M Simpson 	im6f_reap(imf);
15938f002c6cSBruce M Simpson 
15948f002c6cSBruce M Simpson out_in6p_locked:
15958f002c6cSBruce M Simpson 	INP_WUNLOCK(inp);
15968f002c6cSBruce M Simpson 	return (error);
15978f002c6cSBruce M Simpson }
15988f002c6cSBruce M Simpson 
15998f002c6cSBruce M Simpson /*
16008f002c6cSBruce M Simpson  * Given an inpcb, return its multicast options structure pointer.  Accepts
16018f002c6cSBruce M Simpson  * an unlocked inpcb pointer, but will return it locked.  May sleep.
16028f002c6cSBruce M Simpson  *
16038f002c6cSBruce M Simpson  * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
16048f002c6cSBruce M Simpson  * SMPng: NOTE: Returns with the INP write lock held.
16058f002c6cSBruce M Simpson  */
16068f002c6cSBruce M Simpson static struct ip6_moptions *
16078f002c6cSBruce M Simpson in6p_findmoptions(struct inpcb *inp)
16088f002c6cSBruce M Simpson {
16098f002c6cSBruce M Simpson 	struct ip6_moptions	 *imo;
16108f002c6cSBruce M Simpson 	struct in6_multi		**immp;
16118f002c6cSBruce M Simpson 	struct in6_mfilter	 *imfp;
16128f002c6cSBruce M Simpson 	size_t			  idx;
16138f002c6cSBruce M Simpson 
16148f002c6cSBruce M Simpson 	INP_WLOCK(inp);
16158f002c6cSBruce M Simpson 	if (inp->in6p_moptions != NULL)
16168f002c6cSBruce M Simpson 		return (inp->in6p_moptions);
16178f002c6cSBruce M Simpson 
16188f002c6cSBruce M Simpson 	INP_WUNLOCK(inp);
16198f002c6cSBruce M Simpson 
16208f002c6cSBruce M Simpson 	imo = malloc(sizeof(*imo), M_IP6MOPTS, M_WAITOK);
16218f002c6cSBruce M Simpson 	immp = malloc(sizeof(*immp) * IPV6_MIN_MEMBERSHIPS, M_IP6MOPTS,
16228f002c6cSBruce M Simpson 	    M_WAITOK | M_ZERO);
16238f002c6cSBruce M Simpson 	imfp = malloc(sizeof(struct in6_mfilter) * IPV6_MIN_MEMBERSHIPS,
16248f002c6cSBruce M Simpson 	    M_IN6MFILTER, M_WAITOK);
16258f002c6cSBruce M Simpson 
16268f002c6cSBruce M Simpson 	imo->im6o_multicast_ifp = NULL;
16278f002c6cSBruce M Simpson 	imo->im6o_multicast_hlim = V_ip6_defmcasthlim;
16288f002c6cSBruce M Simpson 	imo->im6o_multicast_loop = in6_mcast_loop;
16298f002c6cSBruce M Simpson 	imo->im6o_num_memberships = 0;
16308f002c6cSBruce M Simpson 	imo->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
16318f002c6cSBruce M Simpson 	imo->im6o_membership = immp;
16328f002c6cSBruce M Simpson 
16338f002c6cSBruce M Simpson 	/* Initialize per-group source filters. */
16348f002c6cSBruce M Simpson 	for (idx = 0; idx < IPV6_MIN_MEMBERSHIPS; idx++)
16358f002c6cSBruce M Simpson 		im6f_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE);
16368f002c6cSBruce M Simpson 	imo->im6o_mfilters = imfp;
16378f002c6cSBruce M Simpson 
16388f002c6cSBruce M Simpson 	INP_WLOCK(inp);
16398f002c6cSBruce M Simpson 	if (inp->in6p_moptions != NULL) {
16408f002c6cSBruce M Simpson 		free(imfp, M_IN6MFILTER);
16418f002c6cSBruce M Simpson 		free(immp, M_IP6MOPTS);
16428f002c6cSBruce M Simpson 		free(imo, M_IP6MOPTS);
16438f002c6cSBruce M Simpson 		return (inp->in6p_moptions);
16448f002c6cSBruce M Simpson 	}
16458f002c6cSBruce M Simpson 	inp->in6p_moptions = imo;
16468f002c6cSBruce M Simpson 	return (imo);
16478f002c6cSBruce M Simpson }
16488f002c6cSBruce M Simpson 
16498f002c6cSBruce M Simpson /*
16508f002c6cSBruce M Simpson  * Discard the IPv6 multicast options (and source filters).
16518f002c6cSBruce M Simpson  *
16528f002c6cSBruce M Simpson  * SMPng: NOTE: assumes INP write lock is held.
1653cb6bb230SMatt Macy  *
1654cb6bb230SMatt Macy  * XXX can all be safely deferred to epoch_call
1655cb6bb230SMatt Macy  *
16568f002c6cSBruce M Simpson  */
1657cb6bb230SMatt Macy 
1658cb6bb230SMatt Macy static void
1659f9be0386SMatt Macy inp_gcmoptions(struct ip6_moptions *imo)
16608f002c6cSBruce M Simpson {
16618f002c6cSBruce M Simpson 	struct in6_mfilter	*imf;
1662b15253bbSMatt Macy 	struct in6_multi *inm;
1663b15253bbSMatt Macy 	struct ifnet *ifp;
16648f002c6cSBruce M Simpson 	size_t			 idx, nmships;
16658f002c6cSBruce M Simpson 
16668f002c6cSBruce M Simpson 	nmships = imo->im6o_num_memberships;
16678f002c6cSBruce M Simpson 	for (idx = 0; idx < nmships; ++idx) {
16688f002c6cSBruce M Simpson 		imf = imo->im6o_mfilters ? &imo->im6o_mfilters[idx] : NULL;
16698f002c6cSBruce M Simpson 		if (imf)
16708f002c6cSBruce M Simpson 			im6f_leave(imf);
1671b15253bbSMatt Macy 		inm = imo->im6o_membership[idx];
1672b15253bbSMatt Macy 		ifp = inm->in6m_ifp;
16733b9b6b17SEd Maste 		if (ifp != NULL) {
167415f8acc5SEd Maste 			CURVNET_SET(ifp->if_vnet);
1675b15253bbSMatt Macy 			(void)in6_leavegroup(inm, imf);
167615f8acc5SEd Maste 			CURVNET_RESTORE();
16773b9b6b17SEd Maste 		} else {
16783b9b6b17SEd Maste 			(void)in6_leavegroup(inm, imf);
16793b9b6b17SEd Maste 		}
16808f002c6cSBruce M Simpson 		if (imf)
16818f002c6cSBruce M Simpson 			im6f_purge(imf);
16828f002c6cSBruce M Simpson 	}
16838f002c6cSBruce M Simpson 
16848f002c6cSBruce M Simpson 	if (imo->im6o_mfilters)
16858f002c6cSBruce M Simpson 		free(imo->im6o_mfilters, M_IN6MFILTER);
16868f002c6cSBruce M Simpson 	free(imo->im6o_membership, M_IP6MOPTS);
16878f002c6cSBruce M Simpson 	free(imo, M_IP6MOPTS);
1688cb6bb230SMatt Macy }
1689cb6bb230SMatt Macy 
1690cb6bb230SMatt Macy void
1691cb6bb230SMatt Macy ip6_freemoptions(struct ip6_moptions *imo)
1692cb6bb230SMatt Macy {
1693cb6bb230SMatt Macy 	if (imo == NULL)
1694cb6bb230SMatt Macy 		return;
1695f9be0386SMatt Macy 	inp_gcmoptions(imo);
16968f002c6cSBruce M Simpson }
16978f002c6cSBruce M Simpson 
16988f002c6cSBruce M Simpson /*
16998f002c6cSBruce M Simpson  * Atomically get source filters on a socket for an IPv6 multicast group.
17008f002c6cSBruce M Simpson  * Called with INP lock held; returns with lock released.
17018f002c6cSBruce M Simpson  */
17028f002c6cSBruce M Simpson static int
17038f002c6cSBruce M Simpson in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
17048f002c6cSBruce M Simpson {
17058f002c6cSBruce M Simpson 	struct __msfilterreq	 msfr;
17068f002c6cSBruce M Simpson 	sockunion_t		*gsa;
17078f002c6cSBruce M Simpson 	struct ifnet		*ifp;
17088f002c6cSBruce M Simpson 	struct ip6_moptions	*imo;
17098f002c6cSBruce M Simpson 	struct in6_mfilter	*imf;
17108f002c6cSBruce M Simpson 	struct ip6_msource	*ims;
17118f002c6cSBruce M Simpson 	struct in6_msource	*lims;
17128f002c6cSBruce M Simpson 	struct sockaddr_in6	*psin;
17138f002c6cSBruce M Simpson 	struct sockaddr_storage	*ptss;
17148f002c6cSBruce M Simpson 	struct sockaddr_storage	*tss;
17158f002c6cSBruce M Simpson 	int			 error;
17168f002c6cSBruce M Simpson 	size_t			 idx, nsrcs, ncsrcs;
17178f002c6cSBruce M Simpson 
17188f002c6cSBruce M Simpson 	INP_WLOCK_ASSERT(inp);
17198f002c6cSBruce M Simpson 
17208f002c6cSBruce M Simpson 	imo = inp->in6p_moptions;
17218f002c6cSBruce M Simpson 	KASSERT(imo != NULL, ("%s: null ip6_moptions", __func__));
17228f002c6cSBruce M Simpson 
17238f002c6cSBruce M Simpson 	INP_WUNLOCK(inp);
17248f002c6cSBruce M Simpson 
17258f002c6cSBruce M Simpson 	error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
17268f002c6cSBruce M Simpson 	    sizeof(struct __msfilterreq));
17278f002c6cSBruce M Simpson 	if (error)
17288f002c6cSBruce M Simpson 		return (error);
17298f002c6cSBruce M Simpson 
173029dc7bc6SBruce M Simpson 	if (msfr.msfr_group.ss_family != AF_INET6 ||
173129dc7bc6SBruce M Simpson 	    msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
17328f002c6cSBruce M Simpson 		return (EINVAL);
17338f002c6cSBruce M Simpson 
173429dc7bc6SBruce M Simpson 	gsa = (sockunion_t *)&msfr.msfr_group;
173529dc7bc6SBruce M Simpson 	if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
173629dc7bc6SBruce M Simpson 		return (EINVAL);
173729dc7bc6SBruce M Simpson 
173829dc7bc6SBruce M Simpson 	if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
173929dc7bc6SBruce M Simpson 		return (EADDRNOTAVAIL);
17408f002c6cSBruce M Simpson 	ifp = ifnet_byindex(msfr.msfr_ifindex);
17418f002c6cSBruce M Simpson 	if (ifp == NULL)
174229dc7bc6SBruce M Simpson 		return (EADDRNOTAVAIL);
174329dc7bc6SBruce M Simpson 	(void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
17448f002c6cSBruce M Simpson 
17458f002c6cSBruce M Simpson 	INP_WLOCK(inp);
17468f002c6cSBruce M Simpson 
17478f002c6cSBruce M Simpson 	/*
17488f002c6cSBruce M Simpson 	 * Lookup group on the socket.
17498f002c6cSBruce M Simpson 	 */
17508f002c6cSBruce M Simpson 	idx = im6o_match_group(imo, ifp, &gsa->sa);
17518f002c6cSBruce M Simpson 	if (idx == -1 || imo->im6o_mfilters == NULL) {
17528f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
17538f002c6cSBruce M Simpson 		return (EADDRNOTAVAIL);
17548f002c6cSBruce M Simpson 	}
17558f002c6cSBruce M Simpson 	imf = &imo->im6o_mfilters[idx];
17568f002c6cSBruce M Simpson 
17578f002c6cSBruce M Simpson 	/*
17588f002c6cSBruce M Simpson 	 * Ignore memberships which are in limbo.
17598f002c6cSBruce M Simpson 	 */
17608f002c6cSBruce M Simpson 	if (imf->im6f_st[1] == MCAST_UNDEFINED) {
17618f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
17628f002c6cSBruce M Simpson 		return (EAGAIN);
17638f002c6cSBruce M Simpson 	}
17648f002c6cSBruce M Simpson 	msfr.msfr_fmode = imf->im6f_st[1];
17658f002c6cSBruce M Simpson 
17668f002c6cSBruce M Simpson 	/*
17678f002c6cSBruce M Simpson 	 * If the user specified a buffer, copy out the source filter
17688f002c6cSBruce M Simpson 	 * entries to userland gracefully.
17698f002c6cSBruce M Simpson 	 * We only copy out the number of entries which userland
17708f002c6cSBruce M Simpson 	 * has asked for, but we always tell userland how big the
17718f002c6cSBruce M Simpson 	 * buffer really needs to be.
17728f002c6cSBruce M Simpson 	 */
1773acde2476SXin LI 	if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
1774acde2476SXin LI 		msfr.msfr_nsrcs = in6_mcast_maxsocksrc;
17758f002c6cSBruce M Simpson 	tss = NULL;
17768f002c6cSBruce M Simpson 	if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
17778f002c6cSBruce M Simpson 		tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
17788f002c6cSBruce M Simpson 		    M_TEMP, M_NOWAIT | M_ZERO);
17798f002c6cSBruce M Simpson 		if (tss == NULL) {
17808f002c6cSBruce M Simpson 			INP_WUNLOCK(inp);
17818f002c6cSBruce M Simpson 			return (ENOBUFS);
17828f002c6cSBruce M Simpson 		}
17838f002c6cSBruce M Simpson 	}
17848f002c6cSBruce M Simpson 
17858f002c6cSBruce M Simpson 	/*
17868f002c6cSBruce M Simpson 	 * Count number of sources in-mode at t0.
17878f002c6cSBruce M Simpson 	 * If buffer space exists and remains, copy out source entries.
17888f002c6cSBruce M Simpson 	 */
17898f002c6cSBruce M Simpson 	nsrcs = msfr.msfr_nsrcs;
17908f002c6cSBruce M Simpson 	ncsrcs = 0;
17918f002c6cSBruce M Simpson 	ptss = tss;
17928f002c6cSBruce M Simpson 	RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
17938f002c6cSBruce M Simpson 		lims = (struct in6_msource *)ims;
17948f002c6cSBruce M Simpson 		if (lims->im6sl_st[0] == MCAST_UNDEFINED ||
17958f002c6cSBruce M Simpson 		    lims->im6sl_st[0] != imf->im6f_st[0])
17968f002c6cSBruce M Simpson 			continue;
17978f002c6cSBruce M Simpson 		++ncsrcs;
17988f002c6cSBruce M Simpson 		if (tss != NULL && nsrcs > 0) {
17998f002c6cSBruce M Simpson 			psin = (struct sockaddr_in6 *)ptss;
18008f002c6cSBruce M Simpson 			psin->sin6_family = AF_INET6;
18018f002c6cSBruce M Simpson 			psin->sin6_len = sizeof(struct sockaddr_in6);
18028f002c6cSBruce M Simpson 			psin->sin6_addr = lims->im6s_addr;
18038f002c6cSBruce M Simpson 			psin->sin6_port = 0;
18048f002c6cSBruce M Simpson 			--nsrcs;
18058f002c6cSBruce M Simpson 			++ptss;
18068f002c6cSBruce M Simpson 		}
18078f002c6cSBruce M Simpson 	}
18088f002c6cSBruce M Simpson 
18098f002c6cSBruce M Simpson 	INP_WUNLOCK(inp);
18108f002c6cSBruce M Simpson 
18118f002c6cSBruce M Simpson 	if (tss != NULL) {
18128f002c6cSBruce M Simpson 		error = copyout(tss, msfr.msfr_srcs,
18138f002c6cSBruce M Simpson 		    sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
18148f002c6cSBruce M Simpson 		free(tss, M_TEMP);
18158f002c6cSBruce M Simpson 		if (error)
18168f002c6cSBruce M Simpson 			return (error);
18178f002c6cSBruce M Simpson 	}
18188f002c6cSBruce M Simpson 
18198f002c6cSBruce M Simpson 	msfr.msfr_nsrcs = ncsrcs;
18208f002c6cSBruce M Simpson 	error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
18218f002c6cSBruce M Simpson 
18228f002c6cSBruce M Simpson 	return (error);
18238f002c6cSBruce M Simpson }
18248f002c6cSBruce M Simpson 
18258f002c6cSBruce M Simpson /*
18268f002c6cSBruce M Simpson  * Return the IP multicast options in response to user getsockopt().
18278f002c6cSBruce M Simpson  */
18288f002c6cSBruce M Simpson int
18298f002c6cSBruce M Simpson ip6_getmoptions(struct inpcb *inp, struct sockopt *sopt)
18308f002c6cSBruce M Simpson {
183133cde130SBruce M Simpson 	struct ip6_moptions	*im6o;
183233cde130SBruce M Simpson 	int			 error;
183333cde130SBruce M Simpson 	u_int			 optval;
18348f002c6cSBruce M Simpson 
18358f002c6cSBruce M Simpson 	INP_WLOCK(inp);
183633cde130SBruce M Simpson 	im6o = inp->in6p_moptions;
18378f002c6cSBruce M Simpson 	/*
18388f002c6cSBruce M Simpson 	 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
18398f002c6cSBruce M Simpson 	 * or is a divert socket, reject it.
18408f002c6cSBruce M Simpson 	 */
18418f002c6cSBruce M Simpson 	if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
18428f002c6cSBruce M Simpson 	    (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
18438f002c6cSBruce M Simpson 	    inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
18448f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
18458f002c6cSBruce M Simpson 		return (EOPNOTSUPP);
18468f002c6cSBruce M Simpson 	}
18478f002c6cSBruce M Simpson 
18488f002c6cSBruce M Simpson 	error = 0;
18498f002c6cSBruce M Simpson 	switch (sopt->sopt_name) {
18508f002c6cSBruce M Simpson 	case IPV6_MULTICAST_IF:
185133cde130SBruce M Simpson 		if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) {
18528f002c6cSBruce M Simpson 			optval = 0;
18538f002c6cSBruce M Simpson 		} else {
185433cde130SBruce M Simpson 			optval = im6o->im6o_multicast_ifp->if_index;
18558f002c6cSBruce M Simpson 		}
18568f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
185733cde130SBruce M Simpson 		error = sooptcopyout(sopt, &optval, sizeof(u_int));
18588f002c6cSBruce M Simpson 		break;
18598f002c6cSBruce M Simpson 
18608f002c6cSBruce M Simpson 	case IPV6_MULTICAST_HOPS:
186133cde130SBruce M Simpson 		if (im6o == NULL)
186233cde130SBruce M Simpson 			optval = V_ip6_defmcasthlim;
18638f002c6cSBruce M Simpson 		else
186408907004SBjoern A. Zeeb 			optval = im6o->im6o_multicast_hlim;
18658f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
18668f002c6cSBruce M Simpson 		error = sooptcopyout(sopt, &optval, sizeof(u_int));
18678f002c6cSBruce M Simpson 		break;
18688f002c6cSBruce M Simpson 
18698f002c6cSBruce M Simpson 	case IPV6_MULTICAST_LOOP:
187033cde130SBruce M Simpson 		if (im6o == NULL)
187133cde130SBruce M Simpson 			optval = in6_mcast_loop; /* XXX VIMAGE */
18728f002c6cSBruce M Simpson 		else
187333cde130SBruce M Simpson 			optval = im6o->im6o_multicast_loop;
18748f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
18758f002c6cSBruce M Simpson 		error = sooptcopyout(sopt, &optval, sizeof(u_int));
18768f002c6cSBruce M Simpson 		break;
18778f002c6cSBruce M Simpson 
18788f002c6cSBruce M Simpson 	case IPV6_MSFILTER:
187933cde130SBruce M Simpson 		if (im6o == NULL) {
18808f002c6cSBruce M Simpson 			error = EADDRNOTAVAIL;
18818f002c6cSBruce M Simpson 			INP_WUNLOCK(inp);
18828f002c6cSBruce M Simpson 		} else {
18838f002c6cSBruce M Simpson 			error = in6p_get_source_filters(inp, sopt);
18848f002c6cSBruce M Simpson 		}
18858f002c6cSBruce M Simpson 		break;
18868f002c6cSBruce M Simpson 
18878f002c6cSBruce M Simpson 	default:
18888f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
18898f002c6cSBruce M Simpson 		error = ENOPROTOOPT;
18908f002c6cSBruce M Simpson 		break;
18918f002c6cSBruce M Simpson 	}
18928f002c6cSBruce M Simpson 
18938f002c6cSBruce M Simpson 	INP_UNLOCK_ASSERT(inp);
18948f002c6cSBruce M Simpson 
18958f002c6cSBruce M Simpson 	return (error);
18968f002c6cSBruce M Simpson }
18978f002c6cSBruce M Simpson 
18988f002c6cSBruce M Simpson /*
189933cde130SBruce M Simpson  * Look up the ifnet to use for a multicast group membership,
190033cde130SBruce M Simpson  * given the address of an IPv6 group.
190133cde130SBruce M Simpson  *
190233cde130SBruce M Simpson  * This routine exists to support legacy IPv6 multicast applications.
190333cde130SBruce M Simpson  *
190433cde130SBruce M Simpson  * If inp is non-NULL, use this socket's current FIB number for any
190533cde130SBruce M Simpson  * required FIB lookup. Look up the group address in the unicast FIB,
190633cde130SBruce M Simpson  * and use its ifp; usually, this points to the default next-hop.
190733cde130SBruce M Simpson  * If the FIB lookup fails, return NULL.
190833cde130SBruce M Simpson  *
190933cde130SBruce M Simpson  * FUTURE: Support multiple forwarding tables for IPv6.
191033cde130SBruce M Simpson  *
191133cde130SBruce M Simpson  * Returns NULL if no ifp could be found.
191233cde130SBruce M Simpson  */
191333cde130SBruce M Simpson static struct ifnet *
191481d5d46bSBjoern A. Zeeb in6p_lookup_mcast_ifp(const struct inpcb *in6p,
191533cde130SBruce M Simpson     const struct sockaddr_in6 *gsin6)
191633cde130SBruce M Simpson {
19179977be4aSAlexander V. Chernikov 	struct nhop6_basic	nh6;
19189977be4aSAlexander V. Chernikov 	struct in6_addr		dst;
19199977be4aSAlexander V. Chernikov 	uint32_t		scopeid;
19209977be4aSAlexander V. Chernikov 	uint32_t		fibnum;
192133cde130SBruce M Simpson 
192233cde130SBruce M Simpson 	KASSERT(in6p->inp_vflag & INP_IPV6,
192333cde130SBruce M Simpson 	    ("%s: not INP_IPV6 inpcb", __func__));
192433cde130SBruce M Simpson 	KASSERT(gsin6->sin6_family == AF_INET6,
192533cde130SBruce M Simpson 	    ("%s: not AF_INET6 group", __func__));
192633cde130SBruce M Simpson 
19279977be4aSAlexander V. Chernikov 	in6_splitscope(&gsin6->sin6_addr, &dst, &scopeid);
19289977be4aSAlexander V. Chernikov 	fibnum = in6p ? in6p->inp_inc.inc_fibnum : RT_DEFAULT_FIB;
19299977be4aSAlexander V. Chernikov 	if (fib6_lookup_nh_basic(fibnum, &dst, scopeid, 0, 0, &nh6) != 0)
19309977be4aSAlexander V. Chernikov 		return (NULL);
193133cde130SBruce M Simpson 
19329977be4aSAlexander V. Chernikov 	return (nh6.nh_ifp);
193333cde130SBruce M Simpson }
193433cde130SBruce M Simpson 
193533cde130SBruce M Simpson /*
19368f002c6cSBruce M Simpson  * Join an IPv6 multicast group, possibly with a source.
193733cde130SBruce M Simpson  *
193833cde130SBruce M Simpson  * FIXME: The KAME use of the unspecified address (::)
193933cde130SBruce M Simpson  * to join *all* multicast groups is currently unsupported.
19408f002c6cSBruce M Simpson  */
19418f002c6cSBruce M Simpson static int
19428f002c6cSBruce M Simpson in6p_join_group(struct inpcb *inp, struct sockopt *sopt)
19438f002c6cSBruce M Simpson {
19448f002c6cSBruce M Simpson 	struct group_source_req		 gsr;
19458f002c6cSBruce M Simpson 	sockunion_t			*gsa, *ssa;
19468f002c6cSBruce M Simpson 	struct ifnet			*ifp;
19478f002c6cSBruce M Simpson 	struct in6_mfilter		*imf;
19488f002c6cSBruce M Simpson 	struct ip6_moptions		*imo;
19498f002c6cSBruce M Simpson 	struct in6_multi		*inm;
19508f002c6cSBruce M Simpson 	struct in6_msource		*lims;
19518f002c6cSBruce M Simpson 	size_t				 idx;
19528f002c6cSBruce M Simpson 	int				 error, is_new;
19538f002c6cSBruce M Simpson 
19548f002c6cSBruce M Simpson 	ifp = NULL;
19558f002c6cSBruce M Simpson 	imf = NULL;
19561f81c2b6SBruce M Simpson 	lims = NULL;
19578f002c6cSBruce M Simpson 	error = 0;
19588f002c6cSBruce M Simpson 	is_new = 0;
19598f002c6cSBruce M Simpson 
19608f002c6cSBruce M Simpson 	memset(&gsr, 0, sizeof(struct group_source_req));
19618f002c6cSBruce M Simpson 	gsa = (sockunion_t *)&gsr.gsr_group;
19628f002c6cSBruce M Simpson 	gsa->ss.ss_family = AF_UNSPEC;
19638f002c6cSBruce M Simpson 	ssa = (sockunion_t *)&gsr.gsr_source;
19648f002c6cSBruce M Simpson 	ssa->ss.ss_family = AF_UNSPEC;
19658f002c6cSBruce M Simpson 
196629dc7bc6SBruce M Simpson 	/*
196729dc7bc6SBruce M Simpson 	 * Chew everything into struct group_source_req.
196829dc7bc6SBruce M Simpson 	 * Overwrite the port field if present, as the sockaddr
196929dc7bc6SBruce M Simpson 	 * being copied in may be matched with a binary comparison.
197029dc7bc6SBruce M Simpson 	 * Ignore passed-in scope ID.
197129dc7bc6SBruce M Simpson 	 */
19728f002c6cSBruce M Simpson 	switch (sopt->sopt_name) {
19738f002c6cSBruce M Simpson 	case IPV6_JOIN_GROUP: {
19748f002c6cSBruce M Simpson 		struct ipv6_mreq mreq;
19758f002c6cSBruce M Simpson 
19768f002c6cSBruce M Simpson 		error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
19778f002c6cSBruce M Simpson 		    sizeof(struct ipv6_mreq));
19788f002c6cSBruce M Simpson 		if (error)
19798f002c6cSBruce M Simpson 			return (error);
19808f002c6cSBruce M Simpson 
19818f002c6cSBruce M Simpson 		gsa->sin6.sin6_family = AF_INET6;
19828f002c6cSBruce M Simpson 		gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
19838f002c6cSBruce M Simpson 		gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
19848f002c6cSBruce M Simpson 
198533cde130SBruce M Simpson 		if (mreq.ipv6mr_interface == 0) {
198633cde130SBruce M Simpson 			ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
198733cde130SBruce M Simpson 		} else {
1988010c2b81SBjoern A. Zeeb 			if (V_if_index < mreq.ipv6mr_interface)
198933cde130SBruce M Simpson 				return (EADDRNOTAVAIL);
19908f002c6cSBruce M Simpson 			ifp = ifnet_byindex(mreq.ipv6mr_interface);
199133cde130SBruce M Simpson 		}
19928f002c6cSBruce M Simpson 		CTR3(KTR_MLD, "%s: ipv6mr_interface = %d, ifp = %p",
19938f002c6cSBruce M Simpson 		    __func__, mreq.ipv6mr_interface, ifp);
19948f002c6cSBruce M Simpson 	} break;
19958f002c6cSBruce M Simpson 
19968f002c6cSBruce M Simpson 	case MCAST_JOIN_GROUP:
19978f002c6cSBruce M Simpson 	case MCAST_JOIN_SOURCE_GROUP:
19988f002c6cSBruce M Simpson 		if (sopt->sopt_name == MCAST_JOIN_GROUP) {
19998f002c6cSBruce M Simpson 			error = sooptcopyin(sopt, &gsr,
20008f002c6cSBruce M Simpson 			    sizeof(struct group_req),
20018f002c6cSBruce M Simpson 			    sizeof(struct group_req));
20028f002c6cSBruce M Simpson 		} else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
20038f002c6cSBruce M Simpson 			error = sooptcopyin(sopt, &gsr,
20048f002c6cSBruce M Simpson 			    sizeof(struct group_source_req),
20058f002c6cSBruce M Simpson 			    sizeof(struct group_source_req));
20068f002c6cSBruce M Simpson 		}
20078f002c6cSBruce M Simpson 		if (error)
20088f002c6cSBruce M Simpson 			return (error);
20098f002c6cSBruce M Simpson 
20108f002c6cSBruce M Simpson 		if (gsa->sin6.sin6_family != AF_INET6 ||
20118f002c6cSBruce M Simpson 		    gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
20128f002c6cSBruce M Simpson 			return (EINVAL);
20138f002c6cSBruce M Simpson 
20148f002c6cSBruce M Simpson 		if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
20158f002c6cSBruce M Simpson 			if (ssa->sin6.sin6_family != AF_INET6 ||
20168f002c6cSBruce M Simpson 			    ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
20178f002c6cSBruce M Simpson 				return (EINVAL);
201829dc7bc6SBruce M Simpson 			if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
201929dc7bc6SBruce M Simpson 				return (EINVAL);
202029dc7bc6SBruce M Simpson 			/*
202129dc7bc6SBruce M Simpson 			 * TODO: Validate embedded scope ID in source
202229dc7bc6SBruce M Simpson 			 * list entry against passed-in ifp, if and only
202329dc7bc6SBruce M Simpson 			 * if source list filter entry is iface or node local.
202429dc7bc6SBruce M Simpson 			 */
202529dc7bc6SBruce M Simpson 			in6_clearscope(&ssa->sin6.sin6_addr);
20268f002c6cSBruce M Simpson 			ssa->sin6.sin6_port = 0;
202729dc7bc6SBruce M Simpson 			ssa->sin6.sin6_scope_id = 0;
20288f002c6cSBruce M Simpson 		}
20298f002c6cSBruce M Simpson 
20308f002c6cSBruce M Simpson 		if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
20318f002c6cSBruce M Simpson 			return (EADDRNOTAVAIL);
20328f002c6cSBruce M Simpson 		ifp = ifnet_byindex(gsr.gsr_interface);
20338f002c6cSBruce M Simpson 		break;
20348f002c6cSBruce M Simpson 
20358f002c6cSBruce M Simpson 	default:
20368f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: unknown sopt_name %d",
20378f002c6cSBruce M Simpson 		    __func__, sopt->sopt_name);
20388f002c6cSBruce M Simpson 		return (EOPNOTSUPP);
20398f002c6cSBruce M Simpson 		break;
20408f002c6cSBruce M Simpson 	}
20418f002c6cSBruce M Simpson 
20428f002c6cSBruce M Simpson 	if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
20438f002c6cSBruce M Simpson 		return (EINVAL);
20448f002c6cSBruce M Simpson 
20458f002c6cSBruce M Simpson 	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
20468f002c6cSBruce M Simpson 		return (EADDRNOTAVAIL);
20478f002c6cSBruce M Simpson 
204829dc7bc6SBruce M Simpson 	gsa->sin6.sin6_port = 0;
204929dc7bc6SBruce M Simpson 	gsa->sin6.sin6_scope_id = 0;
205029dc7bc6SBruce M Simpson 
205133cde130SBruce M Simpson 	/*
205229dc7bc6SBruce M Simpson 	 * Always set the scope zone ID on memberships created from userland.
205329dc7bc6SBruce M Simpson 	 * Use the passed-in ifp to do this.
205429dc7bc6SBruce M Simpson 	 * XXX The in6_setscope() return value is meaningless.
205529dc7bc6SBruce M Simpson 	 * XXX SCOPE6_LOCK() is taken by in6_setscope().
205633cde130SBruce M Simpson 	 */
205729dc7bc6SBruce M Simpson 	(void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
205833cde130SBruce M Simpson 
20598f002c6cSBruce M Simpson 	imo = in6p_findmoptions(inp);
20608f002c6cSBruce M Simpson 	idx = im6o_match_group(imo, ifp, &gsa->sa);
20618f002c6cSBruce M Simpson 	if (idx == -1) {
20628f002c6cSBruce M Simpson 		is_new = 1;
20638f002c6cSBruce M Simpson 	} else {
20648f002c6cSBruce M Simpson 		inm = imo->im6o_membership[idx];
20658f002c6cSBruce M Simpson 		imf = &imo->im6o_mfilters[idx];
20661ee6b058SBruce M Simpson 		if (ssa->ss.ss_family != AF_UNSPEC) {
20671ee6b058SBruce M Simpson 			/*
20681ee6b058SBruce M Simpson 			 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
20691ee6b058SBruce M Simpson 			 * is an error. On an existing inclusive membership,
20701ee6b058SBruce M Simpson 			 * it just adds the source to the filter list.
20711ee6b058SBruce M Simpson 			 */
20721ee6b058SBruce M Simpson 			if (imf->im6f_st[1] != MCAST_INCLUDE) {
20738f002c6cSBruce M Simpson 				error = EINVAL;
20748f002c6cSBruce M Simpson 				goto out_in6p_locked;
20758f002c6cSBruce M Simpson 			}
20761f81c2b6SBruce M Simpson 			/*
20771f81c2b6SBruce M Simpson 			 * Throw out duplicates.
20781f81c2b6SBruce M Simpson 			 *
20791f81c2b6SBruce M Simpson 			 * XXX FIXME: This makes a naive assumption that
20801f81c2b6SBruce M Simpson 			 * even if entries exist for *ssa in this imf,
20811f81c2b6SBruce M Simpson 			 * they will be rejected as dupes, even if they
20821f81c2b6SBruce M Simpson 			 * are not valid in the current mode (in-mode).
20831f81c2b6SBruce M Simpson 			 *
20841f81c2b6SBruce M Simpson 			 * in6_msource is transactioned just as for anything
20851f81c2b6SBruce M Simpson 			 * else in SSM -- but note naive use of in6m_graft()
20861f81c2b6SBruce M Simpson 			 * below for allocating new filter entries.
20871f81c2b6SBruce M Simpson 			 *
20881f81c2b6SBruce M Simpson 			 * This is only an issue if someone mixes the
20891f81c2b6SBruce M Simpson 			 * full-state SSM API with the delta-based API,
20901f81c2b6SBruce M Simpson 			 * which is discouraged in the relevant RFCs.
20911f81c2b6SBruce M Simpson 			 */
20928f002c6cSBruce M Simpson 			lims = im6o_match_source(imo, idx, &ssa->sa);
20931f81c2b6SBruce M Simpson 			if (lims != NULL /*&&
20941f81c2b6SBruce M Simpson 			    lims->im6sl_st[1] == MCAST_INCLUDE*/) {
20958f002c6cSBruce M Simpson 				error = EADDRNOTAVAIL;
20968f002c6cSBruce M Simpson 				goto out_in6p_locked;
20978f002c6cSBruce M Simpson 			}
20981ee6b058SBruce M Simpson 		} else {
20991ee6b058SBruce M Simpson 			/*
2100604a60d1SBruce M Simpson 			 * MCAST_JOIN_GROUP alone, on any existing membership,
2101604a60d1SBruce M Simpson 			 * is rejected, to stop the same inpcb tying up
2102604a60d1SBruce M Simpson 			 * multiple refs to the in_multi.
2103604a60d1SBruce M Simpson 			 * On an existing inclusive membership, this is also
2104604a60d1SBruce M Simpson 			 * an error; if you want to change filter mode,
2105604a60d1SBruce M Simpson 			 * you must use the userland API setsourcefilter().
2106604a60d1SBruce M Simpson 			 * XXX We don't reject this for imf in UNDEFINED
2107604a60d1SBruce M Simpson 			 * state at t1, because allocation of a filter
2108604a60d1SBruce M Simpson 			 * is atomic with allocation of a membership.
21091ee6b058SBruce M Simpson 			 */
21101ee6b058SBruce M Simpson 			error = EINVAL;
21111ee6b058SBruce M Simpson 			goto out_in6p_locked;
21121ee6b058SBruce M Simpson 		}
21131ee6b058SBruce M Simpson 	}
21148f002c6cSBruce M Simpson 
21158f002c6cSBruce M Simpson 	/*
21168f002c6cSBruce M Simpson 	 * Begin state merge transaction at socket layer.
21178f002c6cSBruce M Simpson 	 */
21188f002c6cSBruce M Simpson 	INP_WLOCK_ASSERT(inp);
21198f002c6cSBruce M Simpson 
21208f002c6cSBruce M Simpson 	if (is_new) {
21218f002c6cSBruce M Simpson 		if (imo->im6o_num_memberships == imo->im6o_max_memberships) {
21228f002c6cSBruce M Simpson 			error = im6o_grow(imo);
21238f002c6cSBruce M Simpson 			if (error)
21248f002c6cSBruce M Simpson 				goto out_in6p_locked;
21258f002c6cSBruce M Simpson 		}
21268f002c6cSBruce M Simpson 		/*
21278f002c6cSBruce M Simpson 		 * Allocate the new slot upfront so we can deal with
21288f002c6cSBruce M Simpson 		 * grafting the new source filter in same code path
21298f002c6cSBruce M Simpson 		 * as for join-source on existing membership.
21308f002c6cSBruce M Simpson 		 */
21318f002c6cSBruce M Simpson 		idx = imo->im6o_num_memberships;
21328f002c6cSBruce M Simpson 		imo->im6o_membership[idx] = NULL;
21338f002c6cSBruce M Simpson 		imo->im6o_num_memberships++;
21348f002c6cSBruce M Simpson 		KASSERT(imo->im6o_mfilters != NULL,
21358f002c6cSBruce M Simpson 		    ("%s: im6f_mfilters vector was not allocated", __func__));
21368f002c6cSBruce M Simpson 		imf = &imo->im6o_mfilters[idx];
21378f002c6cSBruce M Simpson 		KASSERT(RB_EMPTY(&imf->im6f_sources),
21388f002c6cSBruce M Simpson 		    ("%s: im6f_sources not empty", __func__));
21398f002c6cSBruce M Simpson 	}
21408f002c6cSBruce M Simpson 
21418f002c6cSBruce M Simpson 	/*
21428f002c6cSBruce M Simpson 	 * Graft new source into filter list for this inpcb's
21438f002c6cSBruce M Simpson 	 * membership of the group. The in6_multi may not have
21441ee6b058SBruce M Simpson 	 * been allocated yet if this is a new membership, however,
21451ee6b058SBruce M Simpson 	 * the in_mfilter slot will be allocated and must be initialized.
2146604a60d1SBruce M Simpson 	 *
2147604a60d1SBruce M Simpson 	 * Note: Grafting of exclusive mode filters doesn't happen
2148604a60d1SBruce M Simpson 	 * in this path.
21491f81c2b6SBruce M Simpson 	 * XXX: Should check for non-NULL lims (node exists but may
21501f81c2b6SBruce M Simpson 	 * not be in-mode) for interop with full-state API.
21518f002c6cSBruce M Simpson 	 */
21528f002c6cSBruce M Simpson 	if (ssa->ss.ss_family != AF_UNSPEC) {
21538f002c6cSBruce M Simpson 		/* Membership starts in IN mode */
21548f002c6cSBruce M Simpson 		if (is_new) {
21558f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: new join w/source", __func__);
21568f002c6cSBruce M Simpson 			im6f_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE);
21578f002c6cSBruce M Simpson 		} else {
21588f002c6cSBruce M Simpson 			CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
21598f002c6cSBruce M Simpson 		}
21608f002c6cSBruce M Simpson 		lims = im6f_graft(imf, MCAST_INCLUDE, &ssa->sin6);
21618f002c6cSBruce M Simpson 		if (lims == NULL) {
21628f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: merge imf state failed",
21638f002c6cSBruce M Simpson 			    __func__);
21648f002c6cSBruce M Simpson 			error = ENOMEM;
21658f002c6cSBruce M Simpson 			goto out_im6o_free;
21668f002c6cSBruce M Simpson 		}
21671ee6b058SBruce M Simpson 	} else {
21681ee6b058SBruce M Simpson 		/* No address specified; Membership starts in EX mode */
21691ee6b058SBruce M Simpson 		if (is_new) {
21701ee6b058SBruce M Simpson 			CTR1(KTR_MLD, "%s: new join w/o source", __func__);
21711ee6b058SBruce M Simpson 			im6f_init(imf, MCAST_UNDEFINED, MCAST_EXCLUDE);
21721ee6b058SBruce M Simpson 		}
21738f002c6cSBruce M Simpson 	}
21748f002c6cSBruce M Simpson 
21758f002c6cSBruce M Simpson 	/*
21768f002c6cSBruce M Simpson 	 * Begin state merge transaction at MLD layer.
21778f002c6cSBruce M Simpson 	 */
2178f3e1324bSStephen Hurd 	in_pcbref(inp);
2179f3e1324bSStephen Hurd 	INP_WUNLOCK(inp);
21808f002c6cSBruce M Simpson 	IN6_MULTI_LOCK();
21818f002c6cSBruce M Simpson 
21828f002c6cSBruce M Simpson 	if (is_new) {
2183f3e1324bSStephen Hurd 		error = in6_joingroup_locked(ifp, &gsa->sin6.sin6_addr, imf,
21848f002c6cSBruce M Simpson 		    &inm, 0);
21854fd91336SAndrey V. Elsukov 		if (error) {
21864fd91336SAndrey V. Elsukov 			IN6_MULTI_UNLOCK();
21878f002c6cSBruce M Simpson 			goto out_im6o_free;
21884fd91336SAndrey V. Elsukov 		}
2189130f575dSHans Petter Selasky 		/*
2190130f575dSHans Petter Selasky 		 * NOTE: Refcount from in6_joingroup_locked()
2191130f575dSHans Petter Selasky 		 * is protecting membership.
2192130f575dSHans Petter Selasky 		 */
21938f002c6cSBruce M Simpson 		imo->im6o_membership[idx] = inm;
21948f002c6cSBruce M Simpson 	} else {
21958f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: merge inm state", __func__);
2196f3e1324bSStephen Hurd 		IN6_MULTI_LIST_LOCK();
21978f002c6cSBruce M Simpson 		error = in6m_merge(inm, imf);
21984fd91336SAndrey V. Elsukov 		if (error)
21998f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: failed to merge inm state",
22008f002c6cSBruce M Simpson 			    __func__);
22014fd91336SAndrey V. Elsukov 		else {
22028f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
22038f002c6cSBruce M Simpson 			error = mld_change_state(inm, 0);
22044fd91336SAndrey V. Elsukov 			if (error)
22058f002c6cSBruce M Simpson 				CTR1(KTR_MLD, "%s: failed mld downcall",
22068f002c6cSBruce M Simpson 				    __func__);
22078f002c6cSBruce M Simpson 		}
2208f3e1324bSStephen Hurd 		IN6_MULTI_LIST_UNLOCK();
22098f002c6cSBruce M Simpson 	}
22108f002c6cSBruce M Simpson 
22118f002c6cSBruce M Simpson 	IN6_MULTI_UNLOCK();
2212f3e1324bSStephen Hurd 	INP_WLOCK(inp);
2213f3e1324bSStephen Hurd 	if (in_pcbrele_wlocked(inp))
2214f3e1324bSStephen Hurd 		return (ENXIO);
22158f002c6cSBruce M Simpson 	if (error) {
22168f002c6cSBruce M Simpson 		im6f_rollback(imf);
22178f002c6cSBruce M Simpson 		if (is_new)
22188f002c6cSBruce M Simpson 			im6f_purge(imf);
22198f002c6cSBruce M Simpson 		else
22208f002c6cSBruce M Simpson 			im6f_reap(imf);
22218f002c6cSBruce M Simpson 	} else {
22228f002c6cSBruce M Simpson 		im6f_commit(imf);
22238f002c6cSBruce M Simpson 	}
22248f002c6cSBruce M Simpson 
22258f002c6cSBruce M Simpson out_im6o_free:
22268f002c6cSBruce M Simpson 	if (error && is_new) {
2227f9be0386SMatt Macy 		inm = imo->im6o_membership[idx];
2228f9be0386SMatt Macy 		if (inm != NULL) {
2229f9be0386SMatt Macy 			IN6_MULTI_LIST_LOCK();
2230f9be0386SMatt Macy 			in6m_release_deferred(inm);
2231f9be0386SMatt Macy 			IN6_MULTI_LIST_UNLOCK();
2232f9be0386SMatt Macy 		}
22338f002c6cSBruce M Simpson 		imo->im6o_membership[idx] = NULL;
22348f002c6cSBruce M Simpson 		--imo->im6o_num_memberships;
22358f002c6cSBruce M Simpson 	}
22368f002c6cSBruce M Simpson 
22378f002c6cSBruce M Simpson out_in6p_locked:
22388f002c6cSBruce M Simpson 	INP_WUNLOCK(inp);
22398f002c6cSBruce M Simpson 	return (error);
22408f002c6cSBruce M Simpson }
22418f002c6cSBruce M Simpson 
22428f002c6cSBruce M Simpson /*
22438f002c6cSBruce M Simpson  * Leave an IPv6 multicast group on an inpcb, possibly with a source.
22448f002c6cSBruce M Simpson  */
22458f002c6cSBruce M Simpson static int
22468f002c6cSBruce M Simpson in6p_leave_group(struct inpcb *inp, struct sockopt *sopt)
22478f002c6cSBruce M Simpson {
224829dc7bc6SBruce M Simpson 	struct ipv6_mreq		 mreq;
22498f002c6cSBruce M Simpson 	struct group_source_req		 gsr;
22508f002c6cSBruce M Simpson 	sockunion_t			*gsa, *ssa;
22518f002c6cSBruce M Simpson 	struct ifnet			*ifp;
22528f002c6cSBruce M Simpson 	struct in6_mfilter		*imf;
22538f002c6cSBruce M Simpson 	struct ip6_moptions		*imo;
22548f002c6cSBruce M Simpson 	struct in6_msource		*ims;
22558f002c6cSBruce M Simpson 	struct in6_multi		*inm;
225629dc7bc6SBruce M Simpson 	uint32_t			 ifindex;
22578f002c6cSBruce M Simpson 	size_t				 idx;
22588f002c6cSBruce M Simpson 	int				 error, is_final;
22598f002c6cSBruce M Simpson #ifdef KTR
22608f002c6cSBruce M Simpson 	char				 ip6tbuf[INET6_ADDRSTRLEN];
22618f002c6cSBruce M Simpson #endif
22628f002c6cSBruce M Simpson 
22638f002c6cSBruce M Simpson 	ifp = NULL;
226429dc7bc6SBruce M Simpson 	ifindex = 0;
22658f002c6cSBruce M Simpson 	error = 0;
22668f002c6cSBruce M Simpson 	is_final = 1;
22678f002c6cSBruce M Simpson 
22688f002c6cSBruce M Simpson 	memset(&gsr, 0, sizeof(struct group_source_req));
22698f002c6cSBruce M Simpson 	gsa = (sockunion_t *)&gsr.gsr_group;
22708f002c6cSBruce M Simpson 	gsa->ss.ss_family = AF_UNSPEC;
22718f002c6cSBruce M Simpson 	ssa = (sockunion_t *)&gsr.gsr_source;
22728f002c6cSBruce M Simpson 	ssa->ss.ss_family = AF_UNSPEC;
22738f002c6cSBruce M Simpson 
227429dc7bc6SBruce M Simpson 	/*
227529dc7bc6SBruce M Simpson 	 * Chew everything passed in up into a struct group_source_req
227629dc7bc6SBruce M Simpson 	 * as that is easier to process.
227729dc7bc6SBruce M Simpson 	 * Note: Any embedded scope ID in the multicast group passed
227829dc7bc6SBruce M Simpson 	 * in by userland is ignored, the interface index is the recommended
227929dc7bc6SBruce M Simpson 	 * mechanism to specify an interface; see below.
228029dc7bc6SBruce M Simpson 	 */
22818f002c6cSBruce M Simpson 	switch (sopt->sopt_name) {
228229dc7bc6SBruce M Simpson 	case IPV6_LEAVE_GROUP:
22838f002c6cSBruce M Simpson 		error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
22848f002c6cSBruce M Simpson 		    sizeof(struct ipv6_mreq));
22858f002c6cSBruce M Simpson 		if (error)
22868f002c6cSBruce M Simpson 			return (error);
22878f002c6cSBruce M Simpson 		gsa->sin6.sin6_family = AF_INET6;
22888f002c6cSBruce M Simpson 		gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
22898f002c6cSBruce M Simpson 		gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
229029dc7bc6SBruce M Simpson 		gsa->sin6.sin6_port = 0;
229129dc7bc6SBruce M Simpson 		gsa->sin6.sin6_scope_id = 0;
229229dc7bc6SBruce M Simpson 		ifindex = mreq.ipv6mr_interface;
229329dc7bc6SBruce M Simpson 		break;
22948f002c6cSBruce M Simpson 
22958f002c6cSBruce M Simpson 	case MCAST_LEAVE_GROUP:
22968f002c6cSBruce M Simpson 	case MCAST_LEAVE_SOURCE_GROUP:
22978f002c6cSBruce M Simpson 		if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
22988f002c6cSBruce M Simpson 			error = sooptcopyin(sopt, &gsr,
22998f002c6cSBruce M Simpson 			    sizeof(struct group_req),
23008f002c6cSBruce M Simpson 			    sizeof(struct group_req));
23018f002c6cSBruce M Simpson 		} else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
23028f002c6cSBruce M Simpson 			error = sooptcopyin(sopt, &gsr,
23038f002c6cSBruce M Simpson 			    sizeof(struct group_source_req),
23048f002c6cSBruce M Simpson 			    sizeof(struct group_source_req));
23058f002c6cSBruce M Simpson 		}
23068f002c6cSBruce M Simpson 		if (error)
23078f002c6cSBruce M Simpson 			return (error);
23088f002c6cSBruce M Simpson 
23098f002c6cSBruce M Simpson 		if (gsa->sin6.sin6_family != AF_INET6 ||
23108f002c6cSBruce M Simpson 		    gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
23118f002c6cSBruce M Simpson 			return (EINVAL);
23128f002c6cSBruce M Simpson 		if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
23138f002c6cSBruce M Simpson 			if (ssa->sin6.sin6_family != AF_INET6 ||
23148f002c6cSBruce M Simpson 			    ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
23158f002c6cSBruce M Simpson 				return (EINVAL);
231629dc7bc6SBruce M Simpson 			if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
231729dc7bc6SBruce M Simpson 				return (EINVAL);
231829dc7bc6SBruce M Simpson 			/*
231929dc7bc6SBruce M Simpson 			 * TODO: Validate embedded scope ID in source
232029dc7bc6SBruce M Simpson 			 * list entry against passed-in ifp, if and only
232129dc7bc6SBruce M Simpson 			 * if source list filter entry is iface or node local.
232229dc7bc6SBruce M Simpson 			 */
232329dc7bc6SBruce M Simpson 			in6_clearscope(&ssa->sin6.sin6_addr);
23248f002c6cSBruce M Simpson 		}
232529dc7bc6SBruce M Simpson 		gsa->sin6.sin6_port = 0;
232629dc7bc6SBruce M Simpson 		gsa->sin6.sin6_scope_id = 0;
232729dc7bc6SBruce M Simpson 		ifindex = gsr.gsr_interface;
23288f002c6cSBruce M Simpson 		break;
23298f002c6cSBruce M Simpson 
23308f002c6cSBruce M Simpson 	default:
23318f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: unknown sopt_name %d",
23328f002c6cSBruce M Simpson 		    __func__, sopt->sopt_name);
23338f002c6cSBruce M Simpson 		return (EOPNOTSUPP);
23348f002c6cSBruce M Simpson 		break;
23358f002c6cSBruce M Simpson 	}
23368f002c6cSBruce M Simpson 
23378f002c6cSBruce M Simpson 	if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
23388f002c6cSBruce M Simpson 		return (EINVAL);
23398f002c6cSBruce M Simpson 
234033cde130SBruce M Simpson 	/*
234129dc7bc6SBruce M Simpson 	 * Validate interface index if provided. If no interface index
234229dc7bc6SBruce M Simpson 	 * was provided separately, attempt to look the membership up
234329dc7bc6SBruce M Simpson 	 * from the default scope as a last resort to disambiguate
234429dc7bc6SBruce M Simpson 	 * the membership we are being asked to leave.
234529dc7bc6SBruce M Simpson 	 * XXX SCOPE6 lock potentially taken here.
234633cde130SBruce M Simpson 	 */
234729dc7bc6SBruce M Simpson 	if (ifindex != 0) {
2348010c2b81SBjoern A. Zeeb 		if (V_if_index < ifindex)
234929dc7bc6SBruce M Simpson 			return (EADDRNOTAVAIL);
235029dc7bc6SBruce M Simpson 		ifp = ifnet_byindex(ifindex);
235129dc7bc6SBruce M Simpson 		if (ifp == NULL)
235229dc7bc6SBruce M Simpson 			return (EADDRNOTAVAIL);
235329dc7bc6SBruce M Simpson 		(void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
235429dc7bc6SBruce M Simpson 	} else {
235529dc7bc6SBruce M Simpson 		error = sa6_embedscope(&gsa->sin6, V_ip6_use_defzone);
235629dc7bc6SBruce M Simpson 		if (error)
235729dc7bc6SBruce M Simpson 			return (EADDRNOTAVAIL);
235829dc7bc6SBruce M Simpson 		/*
2359b36c89e5SBruce M Simpson 		 * Some badly behaved applications don't pass an ifindex
2360b36c89e5SBruce M Simpson 		 * or a scope ID, which is an API violation. In this case,
2361b36c89e5SBruce M Simpson 		 * perform a lookup as per a v6 join.
2362b36c89e5SBruce M Simpson 		 *
236329dc7bc6SBruce M Simpson 		 * XXX For now, stomp on zone ID for the corner case.
236429dc7bc6SBruce M Simpson 		 * This is not the 'KAME way', but we need to see the ifp
236529dc7bc6SBruce M Simpson 		 * directly until such time as this implementation is
236629dc7bc6SBruce M Simpson 		 * refactored, assuming the scope IDs are the way to go.
236729dc7bc6SBruce M Simpson 		 */
236829dc7bc6SBruce M Simpson 		ifindex = ntohs(gsa->sin6.sin6_addr.s6_addr16[1]);
2369b36c89e5SBruce M Simpson 		if (ifindex == 0) {
2370b36c89e5SBruce M Simpson 			CTR2(KTR_MLD, "%s: warning: no ifindex, looking up "
2371b36c89e5SBruce M Simpson 			    "ifp for group %s.", __func__,
2372b36c89e5SBruce M Simpson 			    ip6_sprintf(ip6tbuf, &gsa->sin6.sin6_addr));
2373b36c89e5SBruce M Simpson 			ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
2374b36c89e5SBruce M Simpson 		} else {
237529dc7bc6SBruce M Simpson 			ifp = ifnet_byindex(ifindex);
2376b36c89e5SBruce M Simpson 		}
237729dc7bc6SBruce M Simpson 		if (ifp == NULL)
237829dc7bc6SBruce M Simpson 			return (EADDRNOTAVAIL);
237929dc7bc6SBruce M Simpson 	}
238029dc7bc6SBruce M Simpson 
238129dc7bc6SBruce M Simpson 	CTR2(KTR_MLD, "%s: ifp = %p", __func__, ifp);
238229dc7bc6SBruce M Simpson 	KASSERT(ifp != NULL, ("%s: ifp did not resolve", __func__));
238333cde130SBruce M Simpson 
23848f002c6cSBruce M Simpson 	/*
23858f002c6cSBruce M Simpson 	 * Find the membership in the membership array.
23868f002c6cSBruce M Simpson 	 */
23878f002c6cSBruce M Simpson 	imo = in6p_findmoptions(inp);
23888f002c6cSBruce M Simpson 	idx = im6o_match_group(imo, ifp, &gsa->sa);
23898f002c6cSBruce M Simpson 	if (idx == -1) {
23908f002c6cSBruce M Simpson 		error = EADDRNOTAVAIL;
23918f002c6cSBruce M Simpson 		goto out_in6p_locked;
23928f002c6cSBruce M Simpson 	}
23938f002c6cSBruce M Simpson 	inm = imo->im6o_membership[idx];
23948f002c6cSBruce M Simpson 	imf = &imo->im6o_mfilters[idx];
23958f002c6cSBruce M Simpson 
23968f002c6cSBruce M Simpson 	if (ssa->ss.ss_family != AF_UNSPEC)
23978f002c6cSBruce M Simpson 		is_final = 0;
23988f002c6cSBruce M Simpson 
23998f002c6cSBruce M Simpson 	/*
24008f002c6cSBruce M Simpson 	 * Begin state merge transaction at socket layer.
24018f002c6cSBruce M Simpson 	 */
24028f002c6cSBruce M Simpson 	INP_WLOCK_ASSERT(inp);
24038f002c6cSBruce M Simpson 
24048f002c6cSBruce M Simpson 	/*
24058f002c6cSBruce M Simpson 	 * If we were instructed only to leave a given source, do so.
24068f002c6cSBruce M Simpson 	 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
24078f002c6cSBruce M Simpson 	 */
24088f002c6cSBruce M Simpson 	if (is_final) {
24098f002c6cSBruce M Simpson 		im6f_leave(imf);
24108f002c6cSBruce M Simpson 	} else {
24118f002c6cSBruce M Simpson 		if (imf->im6f_st[0] == MCAST_EXCLUDE) {
24128f002c6cSBruce M Simpson 			error = EADDRNOTAVAIL;
24138f002c6cSBruce M Simpson 			goto out_in6p_locked;
24148f002c6cSBruce M Simpson 		}
24158f002c6cSBruce M Simpson 		ims = im6o_match_source(imo, idx, &ssa->sa);
24168f002c6cSBruce M Simpson 		if (ims == NULL) {
24178f002c6cSBruce M Simpson 			CTR3(KTR_MLD, "%s: source %p %spresent", __func__,
24188f002c6cSBruce M Simpson 			    ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
24198f002c6cSBruce M Simpson 			    "not ");
24208f002c6cSBruce M Simpson 			error = EADDRNOTAVAIL;
24218f002c6cSBruce M Simpson 			goto out_in6p_locked;
24228f002c6cSBruce M Simpson 		}
24238f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: %s source", __func__, "block");
24248f002c6cSBruce M Simpson 		error = im6f_prune(imf, &ssa->sin6);
24258f002c6cSBruce M Simpson 		if (error) {
24268f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: merge imf state failed",
24278f002c6cSBruce M Simpson 			    __func__);
24288f002c6cSBruce M Simpson 			goto out_in6p_locked;
24298f002c6cSBruce M Simpson 		}
24308f002c6cSBruce M Simpson 	}
24318f002c6cSBruce M Simpson 
24328f002c6cSBruce M Simpson 	/*
24338f002c6cSBruce M Simpson 	 * Begin state merge transaction at MLD layer.
24348f002c6cSBruce M Simpson 	 */
2435b69888c2SStephen Hurd 	in_pcbref(inp);
2436b69888c2SStephen Hurd 	INP_WUNLOCK(inp);
24378f002c6cSBruce M Simpson 	IN6_MULTI_LOCK();
24388f002c6cSBruce M Simpson 
24398f002c6cSBruce M Simpson 	if (is_final) {
24408f002c6cSBruce M Simpson 		/*
24418f002c6cSBruce M Simpson 		 * Give up the multicast address record to which
24428f002c6cSBruce M Simpson 		 * the membership points.
24438f002c6cSBruce M Simpson 		 */
2444f3e1324bSStephen Hurd 		(void)in6_leavegroup_locked(inm, imf);
24458f002c6cSBruce M Simpson 	} else {
24468f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: merge inm state", __func__);
2447f3e1324bSStephen Hurd 		IN6_MULTI_LIST_LOCK();
24488f002c6cSBruce M Simpson 		error = in6m_merge(inm, imf);
24494fd91336SAndrey V. Elsukov 		if (error)
24508f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: failed to merge inm state",
24518f002c6cSBruce M Simpson 			    __func__);
24524fd91336SAndrey V. Elsukov 		else {
24538f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
24548f002c6cSBruce M Simpson 			error = mld_change_state(inm, 0);
24554fd91336SAndrey V. Elsukov 			if (error)
24568f002c6cSBruce M Simpson 				CTR1(KTR_MLD, "%s: failed mld downcall",
24578f002c6cSBruce M Simpson 				    __func__);
24588f002c6cSBruce M Simpson 		}
2459f3e1324bSStephen Hurd 		IN6_MULTI_LIST_UNLOCK();
24608f002c6cSBruce M Simpson 	}
24618f002c6cSBruce M Simpson 
24628f002c6cSBruce M Simpson 	IN6_MULTI_UNLOCK();
2463b69888c2SStephen Hurd 	INP_WLOCK(inp);
2464b69888c2SStephen Hurd 	if (in_pcbrele_wlocked(inp))
2465b69888c2SStephen Hurd 		return (ENXIO);
24668f002c6cSBruce M Simpson 
24678f002c6cSBruce M Simpson 	if (error)
24688f002c6cSBruce M Simpson 		im6f_rollback(imf);
24698f002c6cSBruce M Simpson 	else
24708f002c6cSBruce M Simpson 		im6f_commit(imf);
24718f002c6cSBruce M Simpson 
24728f002c6cSBruce M Simpson 	im6f_reap(imf);
24738f002c6cSBruce M Simpson 
24748f002c6cSBruce M Simpson 	if (is_final) {
24758f002c6cSBruce M Simpson 		/* Remove the gap in the membership array. */
247657a9feeaSBruce M Simpson 		for (++idx; idx < imo->im6o_num_memberships; ++idx) {
24778f002c6cSBruce M Simpson 			imo->im6o_membership[idx-1] = imo->im6o_membership[idx];
247857a9feeaSBruce M Simpson 			imo->im6o_mfilters[idx-1] = imo->im6o_mfilters[idx];
247957a9feeaSBruce M Simpson 		}
24808f002c6cSBruce M Simpson 		imo->im6o_num_memberships--;
24818f002c6cSBruce M Simpson 	}
24828f002c6cSBruce M Simpson 
24838f002c6cSBruce M Simpson out_in6p_locked:
24848f002c6cSBruce M Simpson 	INP_WUNLOCK(inp);
24858f002c6cSBruce M Simpson 	return (error);
24868f002c6cSBruce M Simpson }
24878f002c6cSBruce M Simpson 
24888f002c6cSBruce M Simpson /*
24898f002c6cSBruce M Simpson  * Select the interface for transmitting IPv6 multicast datagrams.
24908f002c6cSBruce M Simpson  *
24918f002c6cSBruce M Simpson  * Either an instance of struct in6_addr or an instance of struct ipv6_mreqn
24928f002c6cSBruce M Simpson  * may be passed to this socket option. An address of in6addr_any or an
24938f002c6cSBruce M Simpson  * interface index of 0 is used to remove a previous selection.
24948f002c6cSBruce M Simpson  * When no interface is selected, one is chosen for every send.
24958f002c6cSBruce M Simpson  */
24968f002c6cSBruce M Simpson static int
24978f002c6cSBruce M Simpson in6p_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
24988f002c6cSBruce M Simpson {
24998f002c6cSBruce M Simpson 	struct ifnet		*ifp;
25008f002c6cSBruce M Simpson 	struct ip6_moptions	*imo;
25018f002c6cSBruce M Simpson 	u_int			 ifindex;
25028f002c6cSBruce M Simpson 	int			 error;
25038f002c6cSBruce M Simpson 
25048f002c6cSBruce M Simpson 	if (sopt->sopt_valsize != sizeof(u_int))
25058f002c6cSBruce M Simpson 		return (EINVAL);
25068f002c6cSBruce M Simpson 
25078f002c6cSBruce M Simpson 	error = sooptcopyin(sopt, &ifindex, sizeof(u_int), sizeof(u_int));
25088f002c6cSBruce M Simpson 	if (error)
25098f002c6cSBruce M Simpson 		return (error);
2510010c2b81SBjoern A. Zeeb 	if (V_if_index < ifindex)
25118f002c6cSBruce M Simpson 		return (EINVAL);
2512e2956804SAndrey V. Elsukov 	if (ifindex == 0)
2513e2956804SAndrey V. Elsukov 		ifp = NULL;
2514e2956804SAndrey V. Elsukov 	else {
25158f002c6cSBruce M Simpson 		ifp = ifnet_byindex(ifindex);
2516e2956804SAndrey V. Elsukov 		if (ifp == NULL)
2517e2956804SAndrey V. Elsukov 			return (EINVAL);
2518e2956804SAndrey V. Elsukov 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
25198f002c6cSBruce M Simpson 			return (EADDRNOTAVAIL);
2520e2956804SAndrey V. Elsukov 	}
25218f002c6cSBruce M Simpson 	imo = in6p_findmoptions(inp);
25228f002c6cSBruce M Simpson 	imo->im6o_multicast_ifp = ifp;
25238f002c6cSBruce M Simpson 	INP_WUNLOCK(inp);
25248f002c6cSBruce M Simpson 
25258f002c6cSBruce M Simpson 	return (0);
25268f002c6cSBruce M Simpson }
25278f002c6cSBruce M Simpson 
25288f002c6cSBruce M Simpson /*
25298f002c6cSBruce M Simpson  * Atomically set source filters on a socket for an IPv6 multicast group.
25308f002c6cSBruce M Simpson  *
25318f002c6cSBruce M Simpson  * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
25328f002c6cSBruce M Simpson  */
25338f002c6cSBruce M Simpson static int
25348f002c6cSBruce M Simpson in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
25358f002c6cSBruce M Simpson {
25368f002c6cSBruce M Simpson 	struct __msfilterreq	 msfr;
25378f002c6cSBruce M Simpson 	sockunion_t		*gsa;
25388f002c6cSBruce M Simpson 	struct ifnet		*ifp;
25398f002c6cSBruce M Simpson 	struct in6_mfilter	*imf;
25408f002c6cSBruce M Simpson 	struct ip6_moptions	*imo;
25418f002c6cSBruce M Simpson 	struct in6_multi		*inm;
25428f002c6cSBruce M Simpson 	size_t			 idx;
25438f002c6cSBruce M Simpson 	int			 error;
25448f002c6cSBruce M Simpson 
25458f002c6cSBruce M Simpson 	error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
25468f002c6cSBruce M Simpson 	    sizeof(struct __msfilterreq));
25478f002c6cSBruce M Simpson 	if (error)
25488f002c6cSBruce M Simpson 		return (error);
25498f002c6cSBruce M Simpson 
25500dc5893eSBruce M Simpson 	if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
25510dc5893eSBruce M Simpson 		return (ENOBUFS);
25520dc5893eSBruce M Simpson 
25530dc5893eSBruce M Simpson 	if (msfr.msfr_fmode != MCAST_EXCLUDE &&
25540dc5893eSBruce M Simpson 	    msfr.msfr_fmode != MCAST_INCLUDE)
25558f002c6cSBruce M Simpson 		return (EINVAL);
25568f002c6cSBruce M Simpson 
25578f002c6cSBruce M Simpson 	if (msfr.msfr_group.ss_family != AF_INET6 ||
25588f002c6cSBruce M Simpson 	    msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
25598f002c6cSBruce M Simpson 		return (EINVAL);
25608f002c6cSBruce M Simpson 
25618f002c6cSBruce M Simpson 	gsa = (sockunion_t *)&msfr.msfr_group;
25628f002c6cSBruce M Simpson 	if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
25638f002c6cSBruce M Simpson 		return (EINVAL);
25648f002c6cSBruce M Simpson 
25658f002c6cSBruce M Simpson 	gsa->sin6.sin6_port = 0;	/* ignore port */
25668f002c6cSBruce M Simpson 
25678f002c6cSBruce M Simpson 	if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
25688f002c6cSBruce M Simpson 		return (EADDRNOTAVAIL);
25698f002c6cSBruce M Simpson 	ifp = ifnet_byindex(msfr.msfr_ifindex);
25708f002c6cSBruce M Simpson 	if (ifp == NULL)
25718f002c6cSBruce M Simpson 		return (EADDRNOTAVAIL);
257229dc7bc6SBruce M Simpson 	(void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
25738f002c6cSBruce M Simpson 
25748f002c6cSBruce M Simpson 	/*
25758f002c6cSBruce M Simpson 	 * Take the INP write lock.
25768f002c6cSBruce M Simpson 	 * Check if this socket is a member of this group.
25778f002c6cSBruce M Simpson 	 */
25788f002c6cSBruce M Simpson 	imo = in6p_findmoptions(inp);
25798f002c6cSBruce M Simpson 	idx = im6o_match_group(imo, ifp, &gsa->sa);
25808f002c6cSBruce M Simpson 	if (idx == -1 || imo->im6o_mfilters == NULL) {
25818f002c6cSBruce M Simpson 		error = EADDRNOTAVAIL;
25828f002c6cSBruce M Simpson 		goto out_in6p_locked;
25838f002c6cSBruce M Simpson 	}
25848f002c6cSBruce M Simpson 	inm = imo->im6o_membership[idx];
25858f002c6cSBruce M Simpson 	imf = &imo->im6o_mfilters[idx];
25868f002c6cSBruce M Simpson 
25878f002c6cSBruce M Simpson 	/*
25888f002c6cSBruce M Simpson 	 * Begin state merge transaction at socket layer.
25898f002c6cSBruce M Simpson 	 */
25908f002c6cSBruce M Simpson 	INP_WLOCK_ASSERT(inp);
25918f002c6cSBruce M Simpson 
25928f002c6cSBruce M Simpson 	imf->im6f_st[1] = msfr.msfr_fmode;
25938f002c6cSBruce M Simpson 
25948f002c6cSBruce M Simpson 	/*
25958f002c6cSBruce M Simpson 	 * Apply any new source filters, if present.
25968f002c6cSBruce M Simpson 	 * Make a copy of the user-space source vector so
25978f002c6cSBruce M Simpson 	 * that we may copy them with a single copyin. This
25988f002c6cSBruce M Simpson 	 * allows us to deal with page faults up-front.
25998f002c6cSBruce M Simpson 	 */
26008f002c6cSBruce M Simpson 	if (msfr.msfr_nsrcs > 0) {
26018f002c6cSBruce M Simpson 		struct in6_msource	*lims;
26028f002c6cSBruce M Simpson 		struct sockaddr_in6	*psin;
26038f002c6cSBruce M Simpson 		struct sockaddr_storage	*kss, *pkss;
26048f002c6cSBruce M Simpson 		int			 i;
26058f002c6cSBruce M Simpson 
26068f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
26078f002c6cSBruce M Simpson 
26088f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: loading %lu source list entries",
26098f002c6cSBruce M Simpson 		    __func__, (unsigned long)msfr.msfr_nsrcs);
26108f002c6cSBruce M Simpson 		kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
26118f002c6cSBruce M Simpson 		    M_TEMP, M_WAITOK);
26128f002c6cSBruce M Simpson 		error = copyin(msfr.msfr_srcs, kss,
26138f002c6cSBruce M Simpson 		    sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
26148f002c6cSBruce M Simpson 		if (error) {
26158f002c6cSBruce M Simpson 			free(kss, M_TEMP);
26168f002c6cSBruce M Simpson 			return (error);
26178f002c6cSBruce M Simpson 		}
26188f002c6cSBruce M Simpson 
26198f002c6cSBruce M Simpson 		INP_WLOCK(inp);
26208f002c6cSBruce M Simpson 
26218f002c6cSBruce M Simpson 		/*
26228f002c6cSBruce M Simpson 		 * Mark all source filters as UNDEFINED at t1.
26238f002c6cSBruce M Simpson 		 * Restore new group filter mode, as im6f_leave()
26248f002c6cSBruce M Simpson 		 * will set it to INCLUDE.
26258f002c6cSBruce M Simpson 		 */
26268f002c6cSBruce M Simpson 		im6f_leave(imf);
26278f002c6cSBruce M Simpson 		imf->im6f_st[1] = msfr.msfr_fmode;
26288f002c6cSBruce M Simpson 
26298f002c6cSBruce M Simpson 		/*
26308f002c6cSBruce M Simpson 		 * Update socket layer filters at t1, lazy-allocating
26318f002c6cSBruce M Simpson 		 * new entries. This saves a bunch of memory at the
26328f002c6cSBruce M Simpson 		 * cost of one RB_FIND() per source entry; duplicate
26338f002c6cSBruce M Simpson 		 * entries in the msfr_nsrcs vector are ignored.
26348f002c6cSBruce M Simpson 		 * If we encounter an error, rollback transaction.
26358f002c6cSBruce M Simpson 		 *
26368f002c6cSBruce M Simpson 		 * XXX This too could be replaced with a set-symmetric
26378f002c6cSBruce M Simpson 		 * difference like loop to avoid walking from root
26388f002c6cSBruce M Simpson 		 * every time, as the key space is common.
26398f002c6cSBruce M Simpson 		 */
26408f002c6cSBruce M Simpson 		for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
26418f002c6cSBruce M Simpson 			psin = (struct sockaddr_in6 *)pkss;
26428f002c6cSBruce M Simpson 			if (psin->sin6_family != AF_INET6) {
26438f002c6cSBruce M Simpson 				error = EAFNOSUPPORT;
26448f002c6cSBruce M Simpson 				break;
26458f002c6cSBruce M Simpson 			}
26468f002c6cSBruce M Simpson 			if (psin->sin6_len != sizeof(struct sockaddr_in6)) {
26478f002c6cSBruce M Simpson 				error = EINVAL;
26488f002c6cSBruce M Simpson 				break;
26498f002c6cSBruce M Simpson 			}
265029dc7bc6SBruce M Simpson 			if (IN6_IS_ADDR_MULTICAST(&psin->sin6_addr)) {
265129dc7bc6SBruce M Simpson 				error = EINVAL;
265229dc7bc6SBruce M Simpson 				break;
265329dc7bc6SBruce M Simpson 			}
265429dc7bc6SBruce M Simpson 			/*
265529dc7bc6SBruce M Simpson 			 * TODO: Validate embedded scope ID in source
265629dc7bc6SBruce M Simpson 			 * list entry against passed-in ifp, if and only
265729dc7bc6SBruce M Simpson 			 * if source list filter entry is iface or node local.
265829dc7bc6SBruce M Simpson 			 */
265929dc7bc6SBruce M Simpson 			in6_clearscope(&psin->sin6_addr);
26608f002c6cSBruce M Simpson 			error = im6f_get_source(imf, psin, &lims);
26618f002c6cSBruce M Simpson 			if (error)
26628f002c6cSBruce M Simpson 				break;
26638f002c6cSBruce M Simpson 			lims->im6sl_st[1] = imf->im6f_st[1];
26648f002c6cSBruce M Simpson 		}
26658f002c6cSBruce M Simpson 		free(kss, M_TEMP);
26668f002c6cSBruce M Simpson 	}
26678f002c6cSBruce M Simpson 
26688f002c6cSBruce M Simpson 	if (error)
26698f002c6cSBruce M Simpson 		goto out_im6f_rollback;
26708f002c6cSBruce M Simpson 
26718f002c6cSBruce M Simpson 	INP_WLOCK_ASSERT(inp);
2672f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK();
26738f002c6cSBruce M Simpson 
26748f002c6cSBruce M Simpson 	/*
26758f002c6cSBruce M Simpson 	 * Begin state merge transaction at MLD layer.
26768f002c6cSBruce M Simpson 	 */
26778f002c6cSBruce M Simpson 	CTR1(KTR_MLD, "%s: merge inm state", __func__);
26788f002c6cSBruce M Simpson 	error = in6m_merge(inm, imf);
26794fd91336SAndrey V. Elsukov 	if (error)
26808f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
26814fd91336SAndrey V. Elsukov 	else {
26828f002c6cSBruce M Simpson 		CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
26838f002c6cSBruce M Simpson 		error = mld_change_state(inm, 0);
26848f002c6cSBruce M Simpson 		if (error)
26858f002c6cSBruce M Simpson 			CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
26864fd91336SAndrey V. Elsukov 	}
26878f002c6cSBruce M Simpson 
2688f3e1324bSStephen Hurd 	IN6_MULTI_LIST_UNLOCK();
26898f002c6cSBruce M Simpson 
26908f002c6cSBruce M Simpson out_im6f_rollback:
26918f002c6cSBruce M Simpson 	if (error)
26928f002c6cSBruce M Simpson 		im6f_rollback(imf);
26938f002c6cSBruce M Simpson 	else
26948f002c6cSBruce M Simpson 		im6f_commit(imf);
26958f002c6cSBruce M Simpson 
26968f002c6cSBruce M Simpson 	im6f_reap(imf);
26978f002c6cSBruce M Simpson 
26988f002c6cSBruce M Simpson out_in6p_locked:
26998f002c6cSBruce M Simpson 	INP_WUNLOCK(inp);
27008f002c6cSBruce M Simpson 	return (error);
27018f002c6cSBruce M Simpson }
27028f002c6cSBruce M Simpson 
27038f002c6cSBruce M Simpson /*
27048f002c6cSBruce M Simpson  * Set the IP multicast options in response to user setsockopt().
27058f002c6cSBruce M Simpson  *
27068f002c6cSBruce M Simpson  * Many of the socket options handled in this function duplicate the
27078f002c6cSBruce M Simpson  * functionality of socket options in the regular unicast API. However,
27088f002c6cSBruce M Simpson  * it is not possible to merge the duplicate code, because the idempotence
27098f002c6cSBruce M Simpson  * of the IPv6 multicast part of the BSD Sockets API must be preserved;
27108f002c6cSBruce M Simpson  * the effects of these options must be treated as separate and distinct.
27118f002c6cSBruce M Simpson  *
27128f002c6cSBruce M Simpson  * SMPng: XXX: Unlocked read of inp_socket believed OK.
27138f002c6cSBruce M Simpson  */
27148f002c6cSBruce M Simpson int
27158f002c6cSBruce M Simpson ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt)
27168f002c6cSBruce M Simpson {
271733cde130SBruce M Simpson 	struct ip6_moptions	*im6o;
27188f002c6cSBruce M Simpson 	int			 error;
27198f002c6cSBruce M Simpson 
27208f002c6cSBruce M Simpson 	error = 0;
27218f002c6cSBruce M Simpson 
27228f002c6cSBruce M Simpson 	/*
27238f002c6cSBruce M Simpson 	 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
27248f002c6cSBruce M Simpson 	 * or is a divert socket, reject it.
27258f002c6cSBruce M Simpson 	 */
27268f002c6cSBruce M Simpson 	if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
27278f002c6cSBruce M Simpson 	    (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
27288f002c6cSBruce M Simpson 	     inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
27298f002c6cSBruce M Simpson 		return (EOPNOTSUPP);
27308f002c6cSBruce M Simpson 
27318f002c6cSBruce M Simpson 	switch (sopt->sopt_name) {
27328f002c6cSBruce M Simpson 	case IPV6_MULTICAST_IF:
27338f002c6cSBruce M Simpson 		error = in6p_set_multicast_if(inp, sopt);
27348f002c6cSBruce M Simpson 		break;
27358f002c6cSBruce M Simpson 
27368f002c6cSBruce M Simpson 	case IPV6_MULTICAST_HOPS: {
27378f002c6cSBruce M Simpson 		int hlim;
27388f002c6cSBruce M Simpson 
27398f002c6cSBruce M Simpson 		if (sopt->sopt_valsize != sizeof(int)) {
27408f002c6cSBruce M Simpson 			error = EINVAL;
27418f002c6cSBruce M Simpson 			break;
27428f002c6cSBruce M Simpson 		}
27438f002c6cSBruce M Simpson 		error = sooptcopyin(sopt, &hlim, sizeof(hlim), sizeof(int));
27448f002c6cSBruce M Simpson 		if (error)
27458f002c6cSBruce M Simpson 			break;
27468f002c6cSBruce M Simpson 		if (hlim < -1 || hlim > 255) {
27478f002c6cSBruce M Simpson 			error = EINVAL;
27488f002c6cSBruce M Simpson 			break;
274933cde130SBruce M Simpson 		} else if (hlim == -1) {
275033cde130SBruce M Simpson 			hlim = V_ip6_defmcasthlim;
27518f002c6cSBruce M Simpson 		}
275233cde130SBruce M Simpson 		im6o = in6p_findmoptions(inp);
275333cde130SBruce M Simpson 		im6o->im6o_multicast_hlim = hlim;
27548f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
27558f002c6cSBruce M Simpson 		break;
27568f002c6cSBruce M Simpson 	}
27578f002c6cSBruce M Simpson 
27588f002c6cSBruce M Simpson 	case IPV6_MULTICAST_LOOP: {
27598f002c6cSBruce M Simpson 		u_int loop;
27608f002c6cSBruce M Simpson 
27618f002c6cSBruce M Simpson 		/*
27628f002c6cSBruce M Simpson 		 * Set the loopback flag for outgoing multicast packets.
276333cde130SBruce M Simpson 		 * Must be zero or one.
27648f002c6cSBruce M Simpson 		 */
27658f002c6cSBruce M Simpson 		if (sopt->sopt_valsize != sizeof(u_int)) {
27668f002c6cSBruce M Simpson 			error = EINVAL;
27678f002c6cSBruce M Simpson 			break;
27688f002c6cSBruce M Simpson 		}
27698f002c6cSBruce M Simpson 		error = sooptcopyin(sopt, &loop, sizeof(u_int), sizeof(u_int));
27708f002c6cSBruce M Simpson 		if (error)
27718f002c6cSBruce M Simpson 			break;
277233cde130SBruce M Simpson 		if (loop > 1) {
277333cde130SBruce M Simpson 			error = EINVAL;
277433cde130SBruce M Simpson 			break;
277533cde130SBruce M Simpson 		}
277633cde130SBruce M Simpson 		im6o = in6p_findmoptions(inp);
277733cde130SBruce M Simpson 		im6o->im6o_multicast_loop = loop;
27788f002c6cSBruce M Simpson 		INP_WUNLOCK(inp);
27798f002c6cSBruce M Simpson 		break;
27808f002c6cSBruce M Simpson 	}
27818f002c6cSBruce M Simpson 
27828f002c6cSBruce M Simpson 	case IPV6_JOIN_GROUP:
27838f002c6cSBruce M Simpson 	case MCAST_JOIN_GROUP:
27848f002c6cSBruce M Simpson 	case MCAST_JOIN_SOURCE_GROUP:
27858f002c6cSBruce M Simpson 		error = in6p_join_group(inp, sopt);
27868f002c6cSBruce M Simpson 		break;
27878f002c6cSBruce M Simpson 
27888f002c6cSBruce M Simpson 	case IPV6_LEAVE_GROUP:
27898f002c6cSBruce M Simpson 	case MCAST_LEAVE_GROUP:
27908f002c6cSBruce M Simpson 	case MCAST_LEAVE_SOURCE_GROUP:
27918f002c6cSBruce M Simpson 		error = in6p_leave_group(inp, sopt);
27928f002c6cSBruce M Simpson 		break;
27938f002c6cSBruce M Simpson 
27948f002c6cSBruce M Simpson 	case MCAST_BLOCK_SOURCE:
27958f002c6cSBruce M Simpson 	case MCAST_UNBLOCK_SOURCE:
27968f002c6cSBruce M Simpson 		error = in6p_block_unblock_source(inp, sopt);
27978f002c6cSBruce M Simpson 		break;
27988f002c6cSBruce M Simpson 
27998f002c6cSBruce M Simpson 	case IPV6_MSFILTER:
28008f002c6cSBruce M Simpson 		error = in6p_set_source_filters(inp, sopt);
28018f002c6cSBruce M Simpson 		break;
28028f002c6cSBruce M Simpson 
28038f002c6cSBruce M Simpson 	default:
28048f002c6cSBruce M Simpson 		error = EOPNOTSUPP;
28058f002c6cSBruce M Simpson 		break;
28068f002c6cSBruce M Simpson 	}
28078f002c6cSBruce M Simpson 
28088f002c6cSBruce M Simpson 	INP_UNLOCK_ASSERT(inp);
28098f002c6cSBruce M Simpson 
28108f002c6cSBruce M Simpson 	return (error);
28118f002c6cSBruce M Simpson }
28128f002c6cSBruce M Simpson 
28138f002c6cSBruce M Simpson /*
28148f002c6cSBruce M Simpson  * Expose MLD's multicast filter mode and source list(s) to userland,
28158f002c6cSBruce M Simpson  * keyed by (ifindex, group).
28168f002c6cSBruce M Simpson  * The filter mode is written out as a uint32_t, followed by
28178f002c6cSBruce M Simpson  * 0..n of struct in6_addr.
28188f002c6cSBruce M Simpson  * For use by ifmcstat(8).
28198f002c6cSBruce M Simpson  * SMPng: NOTE: unlocked read of ifindex space.
28208f002c6cSBruce M Simpson  */
28218f002c6cSBruce M Simpson static int
28228f002c6cSBruce M Simpson sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS)
28238f002c6cSBruce M Simpson {
282429dc7bc6SBruce M Simpson 	struct in6_addr			 mcaddr;
28258f002c6cSBruce M Simpson 	struct in6_addr			 src;
2826a68cc388SGleb Smirnoff 	struct epoch_tracker		 et;
28278f002c6cSBruce M Simpson 	struct ifnet			*ifp;
28288f002c6cSBruce M Simpson 	struct ifmultiaddr		*ifma;
28298f002c6cSBruce M Simpson 	struct in6_multi		*inm;
28308f002c6cSBruce M Simpson 	struct ip6_msource		*ims;
28318f002c6cSBruce M Simpson 	int				*name;
28328f002c6cSBruce M Simpson 	int				 retval;
28338f002c6cSBruce M Simpson 	u_int				 namelen;
28348f002c6cSBruce M Simpson 	uint32_t			 fmode, ifindex;
28358f002c6cSBruce M Simpson #ifdef KTR
28368f002c6cSBruce M Simpson 	char				 ip6tbuf[INET6_ADDRSTRLEN];
28378f002c6cSBruce M Simpson #endif
28388f002c6cSBruce M Simpson 
28398f002c6cSBruce M Simpson 	name = (int *)arg1;
28408f002c6cSBruce M Simpson 	namelen = arg2;
28418f002c6cSBruce M Simpson 
28428f002c6cSBruce M Simpson 	if (req->newptr != NULL)
28438f002c6cSBruce M Simpson 		return (EPERM);
28448f002c6cSBruce M Simpson 
28458f002c6cSBruce M Simpson 	/* int: ifindex + 4 * 32 bits of IPv6 address */
28468f002c6cSBruce M Simpson 	if (namelen != 5)
28478f002c6cSBruce M Simpson 		return (EINVAL);
28488f002c6cSBruce M Simpson 
28498f002c6cSBruce M Simpson 	ifindex = name[0];
28508f002c6cSBruce M Simpson 	if (ifindex <= 0 || ifindex > V_if_index) {
28518f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: ifindex %u out of range",
28528f002c6cSBruce M Simpson 		    __func__, ifindex);
28538f002c6cSBruce M Simpson 		return (ENOENT);
28548f002c6cSBruce M Simpson 	}
28558f002c6cSBruce M Simpson 
285629dc7bc6SBruce M Simpson 	memcpy(&mcaddr, &name[1], sizeof(struct in6_addr));
285729dc7bc6SBruce M Simpson 	if (!IN6_IS_ADDR_MULTICAST(&mcaddr)) {
28588f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: group %s is not multicast",
285929dc7bc6SBruce M Simpson 		    __func__, ip6_sprintf(ip6tbuf, &mcaddr));
28608f002c6cSBruce M Simpson 		return (EINVAL);
28618f002c6cSBruce M Simpson 	}
28628f002c6cSBruce M Simpson 
28638f002c6cSBruce M Simpson 	ifp = ifnet_byindex(ifindex);
28648f002c6cSBruce M Simpson 	if (ifp == NULL) {
28658f002c6cSBruce M Simpson 		CTR2(KTR_MLD, "%s: no ifp for ifindex %u",
28668f002c6cSBruce M Simpson 		    __func__, ifindex);
28678f002c6cSBruce M Simpson 		return (ENOENT);
28688f002c6cSBruce M Simpson 	}
286929dc7bc6SBruce M Simpson 	/*
287029dc7bc6SBruce M Simpson 	 * Internal MLD lookups require that scope/zone ID is set.
287129dc7bc6SBruce M Simpson 	 */
287229dc7bc6SBruce M Simpson 	(void)in6_setscope(&mcaddr, ifp, NULL);
28738f002c6cSBruce M Simpson 
28748f002c6cSBruce M Simpson 	retval = sysctl_wire_old_buffer(req,
28758f002c6cSBruce M Simpson 	    sizeof(uint32_t) + (in6_mcast_maxgrpsrc * sizeof(struct in6_addr)));
28768f002c6cSBruce M Simpson 	if (retval)
28778f002c6cSBruce M Simpson 		return (retval);
28788f002c6cSBruce M Simpson 
28798f002c6cSBruce M Simpson 	IN6_MULTI_LOCK();
2880f3e1324bSStephen Hurd 	IN6_MULTI_LIST_LOCK();
2881a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
2882d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
28838f002c6cSBruce M Simpson 		if (ifma->ifma_addr->sa_family != AF_INET6 ||
28848f002c6cSBruce M Simpson 		    ifma->ifma_protospec == NULL)
28858f002c6cSBruce M Simpson 			continue;
28868f002c6cSBruce M Simpson 		inm = (struct in6_multi *)ifma->ifma_protospec;
288729dc7bc6SBruce M Simpson 		if (!IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, &mcaddr))
28888f002c6cSBruce M Simpson 			continue;
28898f002c6cSBruce M Simpson 		fmode = inm->in6m_st[1].iss_fmode;
28908f002c6cSBruce M Simpson 		retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
28918f002c6cSBruce M Simpson 		if (retval != 0)
28928f002c6cSBruce M Simpson 			break;
28938f002c6cSBruce M Simpson 		RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
28948f002c6cSBruce M Simpson 			CTR2(KTR_MLD, "%s: visit node %p", __func__, ims);
28958f002c6cSBruce M Simpson 			/*
28968f002c6cSBruce M Simpson 			 * Only copy-out sources which are in-mode.
28978f002c6cSBruce M Simpson 			 */
28988f002c6cSBruce M Simpson 			if (fmode != im6s_get_mode(inm, ims, 1)) {
28998f002c6cSBruce M Simpson 				CTR1(KTR_MLD, "%s: skip non-in-mode",
29008f002c6cSBruce M Simpson 				    __func__);
29018f002c6cSBruce M Simpson 				continue;
29028f002c6cSBruce M Simpson 			}
29038f002c6cSBruce M Simpson 			src = ims->im6s_addr;
29048f002c6cSBruce M Simpson 			retval = SYSCTL_OUT(req, &src,
29058f002c6cSBruce M Simpson 			    sizeof(struct in6_addr));
29068f002c6cSBruce M Simpson 			if (retval != 0)
29078f002c6cSBruce M Simpson 				break;
29088f002c6cSBruce M Simpson 		}
29098f002c6cSBruce M Simpson 	}
2910a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
29118f002c6cSBruce M Simpson 
2912f3e1324bSStephen Hurd 	IN6_MULTI_LIST_UNLOCK();
29138f002c6cSBruce M Simpson 	IN6_MULTI_UNLOCK();
29148f002c6cSBruce M Simpson 
29158f002c6cSBruce M Simpson 	return (retval);
29168f002c6cSBruce M Simpson }
29178f002c6cSBruce M Simpson 
29188f002c6cSBruce M Simpson #ifdef KTR
29198f002c6cSBruce M Simpson 
29208f002c6cSBruce M Simpson static const char *in6m_modestrs[] = { "un", "in", "ex" };
29218f002c6cSBruce M Simpson 
29228f002c6cSBruce M Simpson static const char *
29238f002c6cSBruce M Simpson in6m_mode_str(const int mode)
29248f002c6cSBruce M Simpson {
29258f002c6cSBruce M Simpson 
29268f002c6cSBruce M Simpson 	if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
29278f002c6cSBruce M Simpson 		return (in6m_modestrs[mode]);
29288f002c6cSBruce M Simpson 	return ("??");
29298f002c6cSBruce M Simpson }
29308f002c6cSBruce M Simpson 
29318f002c6cSBruce M Simpson static const char *in6m_statestrs[] = {
29328f002c6cSBruce M Simpson 	"not-member",
29338f002c6cSBruce M Simpson 	"silent",
29348f002c6cSBruce M Simpson 	"idle",
29358f002c6cSBruce M Simpson 	"lazy",
29368f002c6cSBruce M Simpson 	"sleeping",
29378f002c6cSBruce M Simpson 	"awakening",
29388f002c6cSBruce M Simpson 	"query-pending",
29398f002c6cSBruce M Simpson 	"sg-query-pending",
29408f002c6cSBruce M Simpson 	"leaving"
29418f002c6cSBruce M Simpson };
29428f002c6cSBruce M Simpson 
29438f002c6cSBruce M Simpson static const char *
29448f002c6cSBruce M Simpson in6m_state_str(const int state)
29458f002c6cSBruce M Simpson {
29468f002c6cSBruce M Simpson 
29478f002c6cSBruce M Simpson 	if (state >= MLD_NOT_MEMBER && state <= MLD_LEAVING_MEMBER)
29488f002c6cSBruce M Simpson 		return (in6m_statestrs[state]);
29498f002c6cSBruce M Simpson 	return ("??");
29508f002c6cSBruce M Simpson }
29518f002c6cSBruce M Simpson 
29528f002c6cSBruce M Simpson /*
29538f002c6cSBruce M Simpson  * Dump an in6_multi structure to the console.
29548f002c6cSBruce M Simpson  */
29558f002c6cSBruce M Simpson void
29568f002c6cSBruce M Simpson in6m_print(const struct in6_multi *inm)
29578f002c6cSBruce M Simpson {
29588f002c6cSBruce M Simpson 	int t;
29598f002c6cSBruce M Simpson 	char ip6tbuf[INET6_ADDRSTRLEN];
29608f002c6cSBruce M Simpson 
29615b65b8bcSAlexander Kabaev 	if ((ktr_mask & KTR_MLD) == 0)
29628f002c6cSBruce M Simpson 		return;
29638f002c6cSBruce M Simpson 
29648f002c6cSBruce M Simpson 	printf("%s: --- begin in6m %p ---\n", __func__, inm);
29658f002c6cSBruce M Simpson 	printf("addr %s ifp %p(%s) ifma %p\n",
29668f002c6cSBruce M Simpson 	    ip6_sprintf(ip6tbuf, &inm->in6m_addr),
29678f002c6cSBruce M Simpson 	    inm->in6m_ifp,
2968e74966f6SAndrey V. Elsukov 	    if_name(inm->in6m_ifp),
29698f002c6cSBruce M Simpson 	    inm->in6m_ifma);
29708f002c6cSBruce M Simpson 	printf("timer %u state %s refcount %u scq.len %u\n",
29718f002c6cSBruce M Simpson 	    inm->in6m_timer,
29728f002c6cSBruce M Simpson 	    in6m_state_str(inm->in6m_state),
29738f002c6cSBruce M Simpson 	    inm->in6m_refcount,
2974a99c84d4SGleb Smirnoff 	    mbufq_len(&inm->in6m_scq));
29758f002c6cSBruce M Simpson 	printf("mli %p nsrc %lu sctimer %u scrv %u\n",
29768f002c6cSBruce M Simpson 	    inm->in6m_mli,
29778f002c6cSBruce M Simpson 	    inm->in6m_nsrc,
29788f002c6cSBruce M Simpson 	    inm->in6m_sctimer,
29798f002c6cSBruce M Simpson 	    inm->in6m_scrv);
29808f002c6cSBruce M Simpson 	for (t = 0; t < 2; t++) {
29818f002c6cSBruce M Simpson 		printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
29828f002c6cSBruce M Simpson 		    in6m_mode_str(inm->in6m_st[t].iss_fmode),
29838f002c6cSBruce M Simpson 		    inm->in6m_st[t].iss_asm,
29848f002c6cSBruce M Simpson 		    inm->in6m_st[t].iss_ex,
29858f002c6cSBruce M Simpson 		    inm->in6m_st[t].iss_in,
29868f002c6cSBruce M Simpson 		    inm->in6m_st[t].iss_rec);
29878f002c6cSBruce M Simpson 	}
29888f002c6cSBruce M Simpson 	printf("%s: --- end in6m %p ---\n", __func__, inm);
29898f002c6cSBruce M Simpson }
29908f002c6cSBruce M Simpson 
29918f002c6cSBruce M Simpson #else /* !KTR */
29928f002c6cSBruce M Simpson 
29938f002c6cSBruce M Simpson void
29948f002c6cSBruce M Simpson in6m_print(const struct in6_multi *inm)
29958f002c6cSBruce M Simpson {
29968f002c6cSBruce M Simpson 
29978f002c6cSBruce M Simpson }
29988f002c6cSBruce M Simpson 
29998f002c6cSBruce M Simpson #endif /* KTR */
3000