in_mcast.c revision 233200
1170613Sbms/*-
2189592Sbms * Copyright (c) 2007-2009 Bruce Simpson.
3170613Sbms * Copyright (c) 2005 Robert N. M. Watson.
4170613Sbms * All rights reserved.
5170613Sbms *
6170613Sbms * Redistribution and use in source and binary forms, with or without
7170613Sbms * modification, are permitted provided that the following conditions
8170613Sbms * are met:
9170613Sbms * 1. Redistributions of source code must retain the above copyright
10170613Sbms *    notice, this list of conditions and the following disclaimer.
11170613Sbms * 2. Redistributions in binary form must reproduce the above copyright
12170613Sbms *    notice, this list of conditions and the following disclaimer in the
13170613Sbms *    documentation and/or other materials provided with the distribution.
14170613Sbms * 3. The name of the author may not be used to endorse or promote
15170613Sbms *    products derived from this software without specific prior written
16170613Sbms *    permission.
17170613Sbms *
18170613Sbms * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19170613Sbms * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20170613Sbms * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21170613Sbms * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22170613Sbms * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23170613Sbms * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24170613Sbms * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25170613Sbms * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26170613Sbms * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27170613Sbms * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28170613Sbms * SUCH DAMAGE.
29170613Sbms */
30170613Sbms
31170613Sbms/*
32170613Sbms * IPv4 multicast socket, group, and socket option processing module.
33170613Sbms */
34170613Sbms
35170613Sbms#include <sys/cdefs.h>
36170613Sbms__FBSDID("$FreeBSD: stable/9/sys/netinet/in_mcast.c 233200 2012-03-19 20:49:16Z jhb $");
37170613Sbms
38170613Sbms#include <sys/param.h>
39170613Sbms#include <sys/systm.h>
40170613Sbms#include <sys/kernel.h>
41170613Sbms#include <sys/malloc.h>
42170613Sbms#include <sys/mbuf.h>
43171746Scsjp#include <sys/protosw.h>
44170613Sbms#include <sys/socket.h>
45170613Sbms#include <sys/socketvar.h>
46189592Sbms#include <sys/protosw.h>
47170613Sbms#include <sys/sysctl.h>
48189592Sbms#include <sys/ktr.h>
49189592Sbms#include <sys/tree.h>
50170613Sbms
51170613Sbms#include <net/if.h>
52170613Sbms#include <net/if_dl.h>
53170613Sbms#include <net/route.h>
54185571Sbz#include <net/vnet.h>
55170613Sbms
56170613Sbms#include <netinet/in.h>
57170613Sbms#include <netinet/in_systm.h>
58170613Sbms#include <netinet/in_pcb.h>
59170613Sbms#include <netinet/in_var.h>
60170613Sbms#include <netinet/ip_var.h>
61170613Sbms#include <netinet/igmp_var.h>
62170613Sbms
63189592Sbms#ifndef KTR_IGMPV3
64191659Sbms#define KTR_IGMPV3 KTR_INET
65189592Sbms#endif
66189592Sbms
67170613Sbms#ifndef __SOCKUNION_DECLARED
68170613Sbmsunion sockunion {
69170613Sbms	struct sockaddr_storage	ss;
70170613Sbms	struct sockaddr		sa;
71170613Sbms	struct sockaddr_dl	sdl;
72170613Sbms	struct sockaddr_in	sin;
73170613Sbms};
74170613Sbmstypedef union sockunion sockunion_t;
75170613Sbms#define __SOCKUNION_DECLARED
76170613Sbms#endif /* __SOCKUNION_DECLARED */
77170613Sbms
78189592Sbmsstatic MALLOC_DEFINE(M_INMFILTER, "in_mfilter",
79189592Sbms    "IPv4 multicast PCB-layer source filter");
80170613Sbmsstatic MALLOC_DEFINE(M_IPMADDR, "in_multi", "IPv4 multicast group");
81170613Sbmsstatic MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "IPv4 multicast options");
82189592Sbmsstatic MALLOC_DEFINE(M_IPMSOURCE, "ip_msource",
83189592Sbms    "IPv4 multicast IGMP-layer source filter");
84170613Sbms
85170613Sbms/*
86189592Sbms * Locking:
87189592Sbms * - Lock order is: Giant, INP_WLOCK, IN_MULTI_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
88189592Sbms * - The IF_ADDR_LOCK is implicitly taken by inm_lookup() earlier, however
89189592Sbms *   it can be taken by code in net/if.c also.
90189592Sbms * - ip_moptions and in_mfilter are covered by the INP_WLOCK.
91189592Sbms *
92189592Sbms * struct in_multi is covered by IN_MULTI_LOCK. There isn't strictly
93189592Sbms * any need for in_multi itself to be virtualized -- it is bound to an ifp
94189592Sbms * anyway no matter what happens.
95170613Sbms */
96170613Sbmsstruct mtx in_multi_mtx;
97189592SbmsMTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF);
98170613Sbms
99170613Sbms/*
100170613Sbms * Functions with non-static linkage defined in this file should be
101170613Sbms * declared in in_var.h:
102189592Sbms *  imo_multi_filter()
103170613Sbms *  in_addmulti()
104170613Sbms *  in_delmulti()
105189592Sbms *  in_joingroup()
106189592Sbms *  in_joingroup_locked()
107189592Sbms *  in_leavegroup()
108189592Sbms *  in_leavegroup_locked()
109170613Sbms * and ip_var.h:
110170613Sbms *  inp_freemoptions()
111170613Sbms *  inp_getmoptions()
112170613Sbms *  inp_setmoptions()
113189592Sbms *
114189592Sbms * XXX: Both carp and pf need to use the legacy (*,G) KPIs in_addmulti()
115189592Sbms * and in_delmulti().
116170613Sbms */
117189592Sbmsstatic void	imf_commit(struct in_mfilter *);
118189592Sbmsstatic int	imf_get_source(struct in_mfilter *imf,
119189592Sbms		    const struct sockaddr_in *psin,
120189592Sbms		    struct in_msource **);
121189592Sbmsstatic struct in_msource *
122189592Sbms		imf_graft(struct in_mfilter *, const uint8_t,
123189592Sbms		    const struct sockaddr_in *);
124189592Sbmsstatic void	imf_leave(struct in_mfilter *);
125189592Sbmsstatic int	imf_prune(struct in_mfilter *, const struct sockaddr_in *);
126189592Sbmsstatic void	imf_purge(struct in_mfilter *);
127189592Sbmsstatic void	imf_rollback(struct in_mfilter *);
128189592Sbmsstatic void	imf_reap(struct in_mfilter *);
129170613Sbmsstatic int	imo_grow(struct ip_moptions *);
130189592Sbmsstatic size_t	imo_match_group(const struct ip_moptions *,
131189592Sbms		    const struct ifnet *, const struct sockaddr *);
132189592Sbmsstatic struct in_msource *
133189592Sbms		imo_match_source(const struct ip_moptions *, const size_t,
134189592Sbms		    const struct sockaddr *);
135189592Sbmsstatic void	ims_merge(struct ip_msource *ims,
136189592Sbms		    const struct in_msource *lims, const int rollback);
137189592Sbmsstatic int	in_getmulti(struct ifnet *, const struct in_addr *,
138189592Sbms		    struct in_multi **);
139189592Sbmsstatic int	inm_get_source(struct in_multi *inm, const in_addr_t haddr,
140189592Sbms		    const int noalloc, struct ip_msource **pims);
141189592Sbmsstatic int	inm_is_ifp_detached(const struct in_multi *);
142189592Sbmsstatic int	inm_merge(struct in_multi *, /*const*/ struct in_mfilter *);
143189592Sbmsstatic void	inm_purge(struct in_multi *);
144189592Sbmsstatic void	inm_reap(struct in_multi *);
145170613Sbmsstatic struct ip_moptions *
146170613Sbms		inp_findmoptions(struct inpcb *);
147170613Sbmsstatic int	inp_get_source_filters(struct inpcb *, struct sockopt *);
148170613Sbmsstatic int	inp_join_group(struct inpcb *, struct sockopt *);
149170613Sbmsstatic int	inp_leave_group(struct inpcb *, struct sockopt *);
150189592Sbmsstatic struct ifnet *
151189592Sbms		inp_lookup_mcast_ifp(const struct inpcb *,
152189592Sbms		    const struct sockaddr_in *, const struct in_addr);
153189592Sbmsstatic int	inp_block_unblock_source(struct inpcb *, struct sockopt *);
154170613Sbmsstatic int	inp_set_multicast_if(struct inpcb *, struct sockopt *);
155170613Sbmsstatic int	inp_set_source_filters(struct inpcb *, struct sockopt *);
156189592Sbmsstatic int	sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS);
157170613Sbms
158189357SbmsSYSCTL_NODE(_net_inet_ip, OID_AUTO, mcast, CTLFLAG_RW, 0, "IPv4 multicast");
159189357Sbms
160189592Sbmsstatic u_long in_mcast_maxgrpsrc = IP_MAX_GROUP_SRC_FILTER;
161189592SbmsSYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxgrpsrc,
162189592Sbms    CTLFLAG_RW | CTLFLAG_TUN, &in_mcast_maxgrpsrc, 0,
163189592Sbms    "Max source filters per group");
164189592SbmsTUNABLE_ULONG("net.inet.ip.mcast.maxgrpsrc", &in_mcast_maxgrpsrc);
165189592Sbms
166189592Sbmsstatic u_long in_mcast_maxsocksrc = IP_MAX_SOCK_SRC_FILTER;
167189592SbmsSYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxsocksrc,
168189592Sbms    CTLFLAG_RW | CTLFLAG_TUN, &in_mcast_maxsocksrc, 0,
169189592Sbms    "Max source filters per socket");
170189592SbmsTUNABLE_ULONG("net.inet.ip.mcast.maxsocksrc", &in_mcast_maxsocksrc);
171189592Sbms
172189357Sbmsint in_mcast_loop = IP_DEFAULT_MULTICAST_LOOP;
173189357SbmsSYSCTL_INT(_net_inet_ip_mcast, OID_AUTO, loop, CTLFLAG_RW | CTLFLAG_TUN,
174189357Sbms    &in_mcast_loop, 0, "Loopback multicast datagrams by default");
175189357SbmsTUNABLE_INT("net.inet.ip.mcast.loop", &in_mcast_loop);
176189357Sbms
177189592SbmsSYSCTL_NODE(_net_inet_ip_mcast, OID_AUTO, filters,
178189592Sbms    CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip_mcast_filters,
179189592Sbms    "Per-interface stack-wide source filters");
180189592Sbms
181170613Sbms/*
182189592Sbms * Inline function which wraps assertions for a valid ifp.
183189592Sbms * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
184189592Sbms * is detached.
185189592Sbms */
186189592Sbmsstatic int __inline
187189592Sbmsinm_is_ifp_detached(const struct in_multi *inm)
188189592Sbms{
189189592Sbms	struct ifnet *ifp;
190189592Sbms
191189592Sbms	KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
192189592Sbms	ifp = inm->inm_ifma->ifma_ifp;
193189592Sbms	if (ifp != NULL) {
194189592Sbms		/*
195189592Sbms		 * Sanity check that netinet's notion of ifp is the
196189592Sbms		 * same as net's.
197189592Sbms		 */
198189592Sbms		KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
199189592Sbms	}
200189592Sbms
201189592Sbms	return (ifp == NULL);
202189592Sbms}
203189592Sbms
204189592Sbms/*
205189592Sbms * Initialize an in_mfilter structure to a known state at t0, t1
206189592Sbms * with an empty source filter list.
207189592Sbms */
208189592Sbmsstatic __inline void
209189592Sbmsimf_init(struct in_mfilter *imf, const int st0, const int st1)
210189592Sbms{
211189592Sbms	memset(imf, 0, sizeof(struct in_mfilter));
212189592Sbms	RB_INIT(&imf->imf_sources);
213189592Sbms	imf->imf_st[0] = st0;
214189592Sbms	imf->imf_st[1] = st1;
215189592Sbms}
216189592Sbms
217189592Sbms/*
218170613Sbms * Resize the ip_moptions vector to the next power-of-two minus 1.
219170613Sbms * May be called with locks held; do not sleep.
220170613Sbms */
221170613Sbmsstatic int
222170613Sbmsimo_grow(struct ip_moptions *imo)
223170613Sbms{
224170613Sbms	struct in_multi		**nmships;
225170613Sbms	struct in_multi		**omships;
226170613Sbms	struct in_mfilter	 *nmfilters;
227170613Sbms	struct in_mfilter	 *omfilters;
228170613Sbms	size_t			  idx;
229170613Sbms	size_t			  newmax;
230170613Sbms	size_t			  oldmax;
231170613Sbms
232170613Sbms	nmships = NULL;
233170613Sbms	nmfilters = NULL;
234170613Sbms	omships = imo->imo_membership;
235170613Sbms	omfilters = imo->imo_mfilters;
236170613Sbms	oldmax = imo->imo_max_memberships;
237170613Sbms	newmax = ((oldmax + 1) * 2) - 1;
238170613Sbms
239170613Sbms	if (newmax <= IP_MAX_MEMBERSHIPS) {
240170613Sbms		nmships = (struct in_multi **)realloc(omships,
241170613Sbms		    sizeof(struct in_multi *) * newmax, M_IPMOPTS, M_NOWAIT);
242170613Sbms		nmfilters = (struct in_mfilter *)realloc(omfilters,
243189592Sbms		    sizeof(struct in_mfilter) * newmax, M_INMFILTER, M_NOWAIT);
244170613Sbms		if (nmships != NULL && nmfilters != NULL) {
245170613Sbms			/* Initialize newly allocated source filter heads. */
246170613Sbms			for (idx = oldmax; idx < newmax; idx++) {
247189592Sbms				imf_init(&nmfilters[idx], MCAST_UNDEFINED,
248189592Sbms				    MCAST_EXCLUDE);
249170613Sbms			}
250170613Sbms			imo->imo_max_memberships = newmax;
251170613Sbms			imo->imo_membership = nmships;
252170613Sbms			imo->imo_mfilters = nmfilters;
253170613Sbms		}
254170613Sbms	}
255170613Sbms
256170613Sbms	if (nmships == NULL || nmfilters == NULL) {
257170613Sbms		if (nmships != NULL)
258170613Sbms			free(nmships, M_IPMOPTS);
259170613Sbms		if (nmfilters != NULL)
260189592Sbms			free(nmfilters, M_INMFILTER);
261170613Sbms		return (ETOOMANYREFS);
262170613Sbms	}
263170613Sbms
264170613Sbms	return (0);
265170613Sbms}
266170613Sbms
267170613Sbms/*
268170613Sbms * Find an IPv4 multicast group entry for this ip_moptions instance
269170613Sbms * which matches the specified group, and optionally an interface.
270170613Sbms * Return its index into the array, or -1 if not found.
271170613Sbms */
272189592Sbmsstatic size_t
273189592Sbmsimo_match_group(const struct ip_moptions *imo, const struct ifnet *ifp,
274189592Sbms    const struct sockaddr *group)
275170613Sbms{
276189592Sbms	const struct sockaddr_in *gsin;
277170613Sbms	struct in_multi	**pinm;
278170613Sbms	int		  idx;
279170613Sbms	int		  nmships;
280170613Sbms
281189592Sbms	gsin = (const struct sockaddr_in *)group;
282170613Sbms
283170613Sbms	/* The imo_membership array may be lazy allocated. */
284170613Sbms	if (imo->imo_membership == NULL || imo->imo_num_memberships == 0)
285170613Sbms		return (-1);
286170613Sbms
287170613Sbms	nmships = imo->imo_num_memberships;
288170613Sbms	pinm = &imo->imo_membership[0];
289170613Sbms	for (idx = 0; idx < nmships; idx++, pinm++) {
290170613Sbms		if (*pinm == NULL)
291170613Sbms			continue;
292170613Sbms		if ((ifp == NULL || ((*pinm)->inm_ifp == ifp)) &&
293189592Sbms		    in_hosteq((*pinm)->inm_addr, gsin->sin_addr)) {
294170613Sbms			break;
295170613Sbms		}
296170613Sbms	}
297170613Sbms	if (idx >= nmships)
298170613Sbms		idx = -1;
299170613Sbms
300170613Sbms	return (idx);
301170613Sbms}
302170613Sbms
303170613Sbms/*
304189592Sbms * Find an IPv4 multicast source entry for this imo which matches
305170613Sbms * the given group index for this socket, and source address.
306189592Sbms *
307189592Sbms * NOTE: This does not check if the entry is in-mode, merely if
308189592Sbms * it exists, which may not be the desired behaviour.
309170613Sbms */
310189592Sbmsstatic struct in_msource *
311189592Sbmsimo_match_source(const struct ip_moptions *imo, const size_t gidx,
312189592Sbms    const struct sockaddr *src)
313170613Sbms{
314189592Sbms	struct ip_msource	 find;
315170613Sbms	struct in_mfilter	*imf;
316189592Sbms	struct ip_msource	*ims;
317189592Sbms	const sockunion_t	*psa;
318170613Sbms
319170613Sbms	KASSERT(src->sa_family == AF_INET, ("%s: !AF_INET", __func__));
320170613Sbms	KASSERT(gidx != -1 && gidx < imo->imo_num_memberships,
321170613Sbms	    ("%s: invalid index %d\n", __func__, (int)gidx));
322170613Sbms
323170613Sbms	/* The imo_mfilters array may be lazy allocated. */
324170613Sbms	if (imo->imo_mfilters == NULL)
325170613Sbms		return (NULL);
326170613Sbms	imf = &imo->imo_mfilters[gidx];
327170613Sbms
328189592Sbms	/* Source trees are keyed in host byte order. */
329189592Sbms	psa = (const sockunion_t *)src;
330189592Sbms	find.ims_haddr = ntohl(psa->sin.sin_addr.s_addr);
331189592Sbms	ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
332189592Sbms
333189592Sbms	return ((struct in_msource *)ims);
334170613Sbms}
335170613Sbms
336170613Sbms/*
337189592Sbms * Perform filtering for multicast datagrams on a socket by group and source.
338189592Sbms *
339189592Sbms * Returns 0 if a datagram should be allowed through, or various error codes
340189592Sbms * if the socket was not a member of the group, or the source was muted, etc.
341170613Sbms */
342189592Sbmsint
343189592Sbmsimo_multi_filter(const struct ip_moptions *imo, const struct ifnet *ifp,
344189592Sbms    const struct sockaddr *group, const struct sockaddr *src)
345170613Sbms{
346189592Sbms	size_t gidx;
347189592Sbms	struct in_msource *ims;
348189592Sbms	int mode;
349189592Sbms
350189592Sbms	KASSERT(ifp != NULL, ("%s: null ifp", __func__));
351189592Sbms
352189592Sbms	gidx = imo_match_group(imo, ifp, group);
353189592Sbms	if (gidx == -1)
354189592Sbms		return (MCAST_NOTGMEMBER);
355189592Sbms
356189592Sbms	/*
357189592Sbms	 * Check if the source was included in an (S,G) join.
358189592Sbms	 * Allow reception on exclusive memberships by default,
359189592Sbms	 * reject reception on inclusive memberships by default.
360189592Sbms	 * Exclude source only if an in-mode exclude filter exists.
361189592Sbms	 * Include source only if an in-mode include filter exists.
362189592Sbms	 * NOTE: We are comparing group state here at IGMP t1 (now)
363189592Sbms	 * with socket-layer t0 (since last downcall).
364189592Sbms	 */
365189592Sbms	mode = imo->imo_mfilters[gidx].imf_st[1];
366189592Sbms	ims = imo_match_source(imo, gidx, src);
367189592Sbms
368189592Sbms	if ((ims == NULL && mode == MCAST_INCLUDE) ||
369189592Sbms	    (ims != NULL && ims->imsl_st[0] != mode))
370189592Sbms		return (MCAST_NOTSMEMBER);
371189592Sbms
372189592Sbms	return (MCAST_PASS);
373189592Sbms}
374189592Sbms
375189592Sbms/*
376189592Sbms * Find and return a reference to an in_multi record for (ifp, group),
377189592Sbms * and bump its reference count.
378189592Sbms * If one does not exist, try to allocate it, and update link-layer multicast
379189592Sbms * filters on ifp to listen for group.
380189592Sbms * Assumes the IN_MULTI lock is held across the call.
381189592Sbms * Return 0 if successful, otherwise return an appropriate error code.
382189592Sbms */
383189592Sbmsstatic int
384189592Sbmsin_getmulti(struct ifnet *ifp, const struct in_addr *group,
385189592Sbms    struct in_multi **pinm)
386189592Sbms{
387189592Sbms	struct sockaddr_in	 gsin;
388189592Sbms	struct ifmultiaddr	*ifma;
389189592Sbms	struct in_ifinfo	*ii;
390189592Sbms	struct in_multi		*inm;
391189592Sbms	int error;
392170613Sbms
393189592Sbms	IN_MULTI_LOCK_ASSERT();
394170613Sbms
395189592Sbms	ii = (struct in_ifinfo *)ifp->if_afdata[AF_INET];
396170613Sbms
397189592Sbms	inm = inm_lookup(ifp, *group);
398170613Sbms	if (inm != NULL) {
399170613Sbms		/*
400170613Sbms		 * If we already joined this group, just bump the
401170613Sbms		 * refcount and return it.
402170613Sbms		 */
403170613Sbms		KASSERT(inm->inm_refcount >= 1,
404170613Sbms		    ("%s: bad refcount %d", __func__, inm->inm_refcount));
405170613Sbms		++inm->inm_refcount;
406189592Sbms		*pinm = inm;
407189592Sbms		return (0);
408189592Sbms	}
409170613Sbms
410189592Sbms	memset(&gsin, 0, sizeof(gsin));
411189592Sbms	gsin.sin_family = AF_INET;
412189592Sbms	gsin.sin_len = sizeof(struct sockaddr_in);
413189592Sbms	gsin.sin_addr = *group;
414170613Sbms
415189592Sbms	/*
416189592Sbms	 * Check if a link-layer group is already associated
417189592Sbms	 * with this network-layer group on the given ifnet.
418189592Sbms	 */
419189592Sbms	error = if_addmulti(ifp, (struct sockaddr *)&gsin, &ifma);
420189592Sbms	if (error != 0)
421189592Sbms		return (error);
422189592Sbms
423189931Sbms	/* XXX ifma_protospec must be covered by IF_ADDR_LOCK */
424233200Sjhb	IF_ADDR_WLOCK(ifp);
425189931Sbms
426189592Sbms	/*
427189592Sbms	 * If something other than netinet is occupying the link-layer
428189592Sbms	 * group, print a meaningful error message and back out of
429189592Sbms	 * the allocation.
430189592Sbms	 * Otherwise, bump the refcount on the existing network-layer
431189592Sbms	 * group association and return it.
432189592Sbms	 */
433189592Sbms	if (ifma->ifma_protospec != NULL) {
434189592Sbms		inm = (struct in_multi *)ifma->ifma_protospec;
435170613Sbms#ifdef INVARIANTS
436189592Sbms		KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
437189592Sbms		    __func__));
438189592Sbms		KASSERT(ifma->ifma_addr->sa_family == AF_INET,
439189592Sbms		    ("%s: ifma not AF_INET", __func__));
440189592Sbms		KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
441189592Sbms		if (inm->inm_ifma != ifma || inm->inm_ifp != ifp ||
442189592Sbms		    !in_hosteq(inm->inm_addr, *group))
443189592Sbms			panic("%s: ifma %p is inconsistent with %p (%s)",
444189592Sbms			    __func__, ifma, inm, inet_ntoa(*group));
445170613Sbms#endif
446189592Sbms		++inm->inm_refcount;
447189592Sbms		*pinm = inm;
448233200Sjhb		IF_ADDR_WUNLOCK(ifp);
449189592Sbms		return (0);
450189592Sbms	}
451189592Sbms
452233200Sjhb	IF_ADDR_WLOCK_ASSERT(ifp);
453189931Sbms
454189592Sbms	/*
455189592Sbms	 * A new in_multi record is needed; allocate and initialize it.
456189592Sbms	 * We DO NOT perform an IGMP join as the in_ layer may need to
457189592Sbms	 * push an initial source list down to IGMP to support SSM.
458189592Sbms	 *
459189592Sbms	 * The initial source filter state is INCLUDE, {} as per the RFC.
460189592Sbms	 */
461189592Sbms	inm = malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT | M_ZERO);
462189592Sbms	if (inm == NULL) {
463189592Sbms		if_delmulti_ifma(ifma);
464233200Sjhb		IF_ADDR_WUNLOCK(ifp);
465189592Sbms		return (ENOMEM);
466189592Sbms	}
467189592Sbms	inm->inm_addr = *group;
468189592Sbms	inm->inm_ifp = ifp;
469189592Sbms	inm->inm_igi = ii->ii_igmp;
470189592Sbms	inm->inm_ifma = ifma;
471189592Sbms	inm->inm_refcount = 1;
472189592Sbms	inm->inm_state = IGMP_NOT_MEMBER;
473189592Sbms
474189592Sbms	/*
475189592Sbms	 * Pending state-changes per group are subject to a bounds check.
476189592Sbms	 */
477189592Sbms	IFQ_SET_MAXLEN(&inm->inm_scq, IGMP_MAX_STATE_CHANGES);
478189592Sbms
479189592Sbms	inm->inm_st[0].iss_fmode = MCAST_UNDEFINED;
480189592Sbms	inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
481189592Sbms	RB_INIT(&inm->inm_srcs);
482189592Sbms
483189592Sbms	ifma->ifma_protospec = inm;
484189592Sbms
485189592Sbms	*pinm = inm;
486189592Sbms
487233200Sjhb	IF_ADDR_WUNLOCK(ifp);
488189592Sbms	return (0);
489189592Sbms}
490189592Sbms
491189592Sbms/*
492189592Sbms * Drop a reference to an in_multi record.
493189592Sbms *
494189592Sbms * If the refcount drops to 0, free the in_multi record and
495189592Sbms * delete the underlying link-layer membership.
496189592Sbms */
497189592Sbmsvoid
498189592Sbmsinm_release_locked(struct in_multi *inm)
499189592Sbms{
500189592Sbms	struct ifmultiaddr *ifma;
501189592Sbms
502189592Sbms	IN_MULTI_LOCK_ASSERT();
503189592Sbms
504189592Sbms	CTR2(KTR_IGMPV3, "%s: refcount is %d", __func__, inm->inm_refcount);
505189592Sbms
506189592Sbms	if (--inm->inm_refcount > 0) {
507189592Sbms		CTR2(KTR_IGMPV3, "%s: refcount is now %d", __func__,
508189592Sbms		    inm->inm_refcount);
509189592Sbms		return;
510189592Sbms	}
511189592Sbms
512189592Sbms	CTR2(KTR_IGMPV3, "%s: freeing inm %p", __func__, inm);
513189592Sbms
514189592Sbms	ifma = inm->inm_ifma;
515189592Sbms
516189931Sbms	/* XXX this access is not covered by IF_ADDR_LOCK */
517189592Sbms	CTR2(KTR_IGMPV3, "%s: purging ifma %p", __func__, ifma);
518189592Sbms	KASSERT(ifma->ifma_protospec == inm,
519189592Sbms	    ("%s: ifma_protospec != inm", __func__));
520189592Sbms	ifma->ifma_protospec = NULL;
521189592Sbms
522189592Sbms	inm_purge(inm);
523189592Sbms
524189592Sbms	free(inm, M_IPMADDR);
525189592Sbms
526189592Sbms	if_delmulti_ifma(ifma);
527189592Sbms}
528189592Sbms
529189592Sbms/*
530189592Sbms * Clear recorded source entries for a group.
531189592Sbms * Used by the IGMP code. Caller must hold the IN_MULTI lock.
532189592Sbms * FIXME: Should reap.
533189592Sbms */
534189592Sbmsvoid
535189592Sbmsinm_clear_recorded(struct in_multi *inm)
536189592Sbms{
537189592Sbms	struct ip_msource	*ims;
538189592Sbms
539189592Sbms	IN_MULTI_LOCK_ASSERT();
540189592Sbms
541189592Sbms	RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
542189592Sbms		if (ims->ims_stp) {
543189592Sbms			ims->ims_stp = 0;
544189592Sbms			--inm->inm_st[1].iss_rec;
545170613Sbms		}
546189592Sbms	}
547189592Sbms	KASSERT(inm->inm_st[1].iss_rec == 0,
548189592Sbms	    ("%s: iss_rec %d not 0", __func__, inm->inm_st[1].iss_rec));
549189592Sbms}
550170613Sbms
551189592Sbms/*
552189592Sbms * Record a source as pending for a Source-Group IGMPv3 query.
553189592Sbms * This lives here as it modifies the shared tree.
554189592Sbms *
555189592Sbms * inm is the group descriptor.
556189592Sbms * naddr is the address of the source to record in network-byte order.
557189592Sbms *
558189592Sbms * If the net.inet.igmp.sgalloc sysctl is non-zero, we will
559189592Sbms * lazy-allocate a source node in response to an SG query.
560189592Sbms * Otherwise, no allocation is performed. This saves some memory
561189592Sbms * with the trade-off that the source will not be reported to the
562189592Sbms * router if joined in the window between the query response and
563189592Sbms * the group actually being joined on the local host.
564189592Sbms *
565189592Sbms * VIMAGE: XXX: Currently the igmp_sgalloc feature has been removed.
566189592Sbms * This turns off the allocation of a recorded source entry if
567189592Sbms * the group has not been joined.
568189592Sbms *
569189592Sbms * Return 0 if the source didn't exist or was already marked as recorded.
570189592Sbms * Return 1 if the source was marked as recorded by this function.
571189592Sbms * Return <0 if any error occured (negated errno code).
572189592Sbms */
573189592Sbmsint
574189592Sbmsinm_record_source(struct in_multi *inm, const in_addr_t naddr)
575189592Sbms{
576189592Sbms	struct ip_msource	 find;
577189592Sbms	struct ip_msource	*ims, *nims;
578189592Sbms
579189592Sbms	IN_MULTI_LOCK_ASSERT();
580189592Sbms
581189592Sbms	find.ims_haddr = ntohl(naddr);
582189592Sbms	ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
583189592Sbms	if (ims && ims->ims_stp)
584189592Sbms		return (0);
585189592Sbms	if (ims == NULL) {
586189592Sbms		if (inm->inm_nsrc == in_mcast_maxgrpsrc)
587189592Sbms			return (-ENOSPC);
588189592Sbms		nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
589189592Sbms		    M_NOWAIT | M_ZERO);
590189592Sbms		if (nims == NULL)
591189592Sbms			return (-ENOMEM);
592189592Sbms		nims->ims_haddr = find.ims_haddr;
593189592Sbms		RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
594189592Sbms		++inm->inm_nsrc;
595189592Sbms		ims = nims;
596189592Sbms	}
597189592Sbms
598189592Sbms	/*
599189592Sbms	 * Mark the source as recorded and update the recorded
600189592Sbms	 * source count.
601189592Sbms	 */
602189592Sbms	++ims->ims_stp;
603189592Sbms	++inm->inm_st[1].iss_rec;
604189592Sbms
605189592Sbms	return (1);
606189592Sbms}
607189592Sbms
608189592Sbms/*
609189592Sbms * Return a pointer to an in_msource owned by an in_mfilter,
610189592Sbms * given its source address.
611189592Sbms * Lazy-allocate if needed. If this is a new entry its filter state is
612189592Sbms * undefined at t0.
613189592Sbms *
614189592Sbms * imf is the filter set being modified.
615189592Sbms * haddr is the source address in *host* byte-order.
616189592Sbms *
617189592Sbms * SMPng: May be called with locks held; malloc must not block.
618189592Sbms */
619189592Sbmsstatic int
620189592Sbmsimf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin,
621189592Sbms    struct in_msource **plims)
622189592Sbms{
623189592Sbms	struct ip_msource	 find;
624189592Sbms	struct ip_msource	*ims, *nims;
625189592Sbms	struct in_msource	*lims;
626189592Sbms	int			 error;
627189592Sbms
628189592Sbms	error = 0;
629189592Sbms	ims = NULL;
630189592Sbms	lims = NULL;
631189592Sbms
632189592Sbms	/* key is host byte order */
633189592Sbms	find.ims_haddr = ntohl(psin->sin_addr.s_addr);
634189592Sbms	ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
635189592Sbms	lims = (struct in_msource *)ims;
636189592Sbms	if (lims == NULL) {
637189592Sbms		if (imf->imf_nsrc == in_mcast_maxsocksrc)
638189592Sbms			return (ENOSPC);
639189592Sbms		nims = malloc(sizeof(struct in_msource), M_INMFILTER,
640189592Sbms		    M_NOWAIT | M_ZERO);
641189592Sbms		if (nims == NULL)
642189592Sbms			return (ENOMEM);
643189592Sbms		lims = (struct in_msource *)nims;
644189592Sbms		lims->ims_haddr = find.ims_haddr;
645189592Sbms		lims->imsl_st[0] = MCAST_UNDEFINED;
646189592Sbms		RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
647189592Sbms		++imf->imf_nsrc;
648189592Sbms	}
649189592Sbms
650189592Sbms	*plims = lims;
651189592Sbms
652189592Sbms	return (error);
653189592Sbms}
654189592Sbms
655189592Sbms/*
656189592Sbms * Graft a source entry into an existing socket-layer filter set,
657189592Sbms * maintaining any required invariants and checking allocations.
658189592Sbms *
659189592Sbms * The source is marked as being in the new filter mode at t1.
660189592Sbms *
661189592Sbms * Return the pointer to the new node, otherwise return NULL.
662189592Sbms */
663189592Sbmsstatic struct in_msource *
664189592Sbmsimf_graft(struct in_mfilter *imf, const uint8_t st1,
665189592Sbms    const struct sockaddr_in *psin)
666189592Sbms{
667189592Sbms	struct ip_msource	*nims;
668189592Sbms	struct in_msource	*lims;
669189592Sbms
670189592Sbms	nims = malloc(sizeof(struct in_msource), M_INMFILTER,
671189592Sbms	    M_NOWAIT | M_ZERO);
672189592Sbms	if (nims == NULL)
673189592Sbms		return (NULL);
674189592Sbms	lims = (struct in_msource *)nims;
675189592Sbms	lims->ims_haddr = ntohl(psin->sin_addr.s_addr);
676189592Sbms	lims->imsl_st[0] = MCAST_UNDEFINED;
677189592Sbms	lims->imsl_st[1] = st1;
678189592Sbms	RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
679189592Sbms	++imf->imf_nsrc;
680189592Sbms
681189592Sbms	return (lims);
682189592Sbms}
683189592Sbms
684189592Sbms/*
685189592Sbms * Prune a source entry from an existing socket-layer filter set,
686189592Sbms * maintaining any required invariants and checking allocations.
687189592Sbms *
688189592Sbms * The source is marked as being left at t1, it is not freed.
689189592Sbms *
690189592Sbms * Return 0 if no error occurred, otherwise return an errno value.
691189592Sbms */
692189592Sbmsstatic int
693189592Sbmsimf_prune(struct in_mfilter *imf, const struct sockaddr_in *psin)
694189592Sbms{
695189592Sbms	struct ip_msource	 find;
696189592Sbms	struct ip_msource	*ims;
697189592Sbms	struct in_msource	*lims;
698189592Sbms
699189592Sbms	/* key is host byte order */
700189592Sbms	find.ims_haddr = ntohl(psin->sin_addr.s_addr);
701189592Sbms	ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
702189592Sbms	if (ims == NULL)
703189592Sbms		return (ENOENT);
704189592Sbms	lims = (struct in_msource *)ims;
705189592Sbms	lims->imsl_st[1] = MCAST_UNDEFINED;
706189592Sbms	return (0);
707189592Sbms}
708189592Sbms
709189592Sbms/*
710189592Sbms * Revert socket-layer filter set deltas at t1 to t0 state.
711189592Sbms */
712189592Sbmsstatic void
713189592Sbmsimf_rollback(struct in_mfilter *imf)
714189592Sbms{
715189592Sbms	struct ip_msource	*ims, *tims;
716189592Sbms	struct in_msource	*lims;
717189592Sbms
718189592Sbms	RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
719189592Sbms		lims = (struct in_msource *)ims;
720189592Sbms		if (lims->imsl_st[0] == lims->imsl_st[1]) {
721189592Sbms			/* no change at t1 */
722189592Sbms			continue;
723189592Sbms		} else if (lims->imsl_st[0] != MCAST_UNDEFINED) {
724189592Sbms			/* revert change to existing source at t1 */
725189592Sbms			lims->imsl_st[1] = lims->imsl_st[0];
726189592Sbms		} else {
727189592Sbms			/* revert source added t1 */
728189592Sbms			CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
729189592Sbms			RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
730189592Sbms			free(ims, M_INMFILTER);
731189592Sbms			imf->imf_nsrc--;
732189592Sbms		}
733189592Sbms	}
734189592Sbms	imf->imf_st[1] = imf->imf_st[0];
735189592Sbms}
736189592Sbms
737189592Sbms/*
738189592Sbms * Mark socket-layer filter set as INCLUDE {} at t1.
739189592Sbms */
740189592Sbmsstatic void
741189592Sbmsimf_leave(struct in_mfilter *imf)
742189592Sbms{
743189592Sbms	struct ip_msource	*ims;
744189592Sbms	struct in_msource	*lims;
745189592Sbms
746189592Sbms	RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
747189592Sbms		lims = (struct in_msource *)ims;
748189592Sbms		lims->imsl_st[1] = MCAST_UNDEFINED;
749189592Sbms	}
750189592Sbms	imf->imf_st[1] = MCAST_INCLUDE;
751189592Sbms}
752189592Sbms
753189592Sbms/*
754189592Sbms * Mark socket-layer filter set deltas as committed.
755189592Sbms */
756189592Sbmsstatic void
757189592Sbmsimf_commit(struct in_mfilter *imf)
758189592Sbms{
759189592Sbms	struct ip_msource	*ims;
760189592Sbms	struct in_msource	*lims;
761189592Sbms
762189592Sbms	RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
763189592Sbms		lims = (struct in_msource *)ims;
764189592Sbms		lims->imsl_st[0] = lims->imsl_st[1];
765189592Sbms	}
766189592Sbms	imf->imf_st[0] = imf->imf_st[1];
767189592Sbms}
768189592Sbms
769189592Sbms/*
770189592Sbms * Reap unreferenced sources from socket-layer filter set.
771189592Sbms */
772189592Sbmsstatic void
773189592Sbmsimf_reap(struct in_mfilter *imf)
774189592Sbms{
775189592Sbms	struct ip_msource	*ims, *tims;
776189592Sbms	struct in_msource	*lims;
777189592Sbms
778189592Sbms	RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
779189592Sbms		lims = (struct in_msource *)ims;
780189592Sbms		if ((lims->imsl_st[0] == MCAST_UNDEFINED) &&
781189592Sbms		    (lims->imsl_st[1] == MCAST_UNDEFINED)) {
782189592Sbms			CTR2(KTR_IGMPV3, "%s: free lims %p", __func__, ims);
783189592Sbms			RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
784189592Sbms			free(ims, M_INMFILTER);
785189592Sbms			imf->imf_nsrc--;
786189592Sbms		}
787189592Sbms	}
788189592Sbms}
789189592Sbms
790189592Sbms/*
791189592Sbms * Purge socket-layer filter set.
792189592Sbms */
793189592Sbmsstatic void
794189592Sbmsimf_purge(struct in_mfilter *imf)
795189592Sbms{
796189592Sbms	struct ip_msource	*ims, *tims;
797189592Sbms
798189592Sbms	RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
799189592Sbms		CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
800189592Sbms		RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
801189592Sbms		free(ims, M_INMFILTER);
802189592Sbms		imf->imf_nsrc--;
803189592Sbms	}
804189592Sbms	imf->imf_st[0] = imf->imf_st[1] = MCAST_UNDEFINED;
805189592Sbms	KASSERT(RB_EMPTY(&imf->imf_sources),
806189592Sbms	    ("%s: imf_sources not empty", __func__));
807189592Sbms}
808189592Sbms
809189592Sbms/*
810189592Sbms * Look up a source filter entry for a multicast group.
811189592Sbms *
812189592Sbms * inm is the group descriptor to work with.
813189592Sbms * haddr is the host-byte-order IPv4 address to look up.
814189592Sbms * noalloc may be non-zero to suppress allocation of sources.
815189592Sbms * *pims will be set to the address of the retrieved or allocated source.
816189592Sbms *
817189592Sbms * SMPng: NOTE: may be called with locks held.
818189592Sbms * Return 0 if successful, otherwise return a non-zero error code.
819189592Sbms */
820189592Sbmsstatic int
821189592Sbmsinm_get_source(struct in_multi *inm, const in_addr_t haddr,
822189592Sbms    const int noalloc, struct ip_msource **pims)
823189592Sbms{
824189592Sbms	struct ip_msource	 find;
825189592Sbms	struct ip_msource	*ims, *nims;
826189592Sbms#ifdef KTR
827189592Sbms	struct in_addr ia;
828189592Sbms#endif
829189592Sbms
830189592Sbms	find.ims_haddr = haddr;
831189592Sbms	ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
832189592Sbms	if (ims == NULL && !noalloc) {
833189592Sbms		if (inm->inm_nsrc == in_mcast_maxgrpsrc)
834189592Sbms			return (ENOSPC);
835189592Sbms		nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
836189592Sbms		    M_NOWAIT | M_ZERO);
837189592Sbms		if (nims == NULL)
838189592Sbms			return (ENOMEM);
839189592Sbms		nims->ims_haddr = haddr;
840189592Sbms		RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
841189592Sbms		++inm->inm_nsrc;
842189592Sbms		ims = nims;
843189592Sbms#ifdef KTR
844189592Sbms		ia.s_addr = htonl(haddr);
845189592Sbms		CTR3(KTR_IGMPV3, "%s: allocated %s as %p", __func__,
846189592Sbms		    inet_ntoa(ia), ims);
847189592Sbms#endif
848189592Sbms	}
849189592Sbms
850189592Sbms	*pims = ims;
851189592Sbms	return (0);
852189592Sbms}
853189592Sbms
854189592Sbms/*
855189592Sbms * Merge socket-layer source into IGMP-layer source.
856189592Sbms * If rollback is non-zero, perform the inverse of the merge.
857189592Sbms */
858189592Sbmsstatic void
859189592Sbmsims_merge(struct ip_msource *ims, const struct in_msource *lims,
860189592Sbms    const int rollback)
861189592Sbms{
862189592Sbms	int n = rollback ? -1 : 1;
863189592Sbms#ifdef KTR
864189592Sbms	struct in_addr ia;
865189592Sbms
866189592Sbms	ia.s_addr = htonl(ims->ims_haddr);
867189592Sbms#endif
868189592Sbms
869189592Sbms	if (lims->imsl_st[0] == MCAST_EXCLUDE) {
870189592Sbms		CTR3(KTR_IGMPV3, "%s: t1 ex -= %d on %s",
871189592Sbms		    __func__, n, inet_ntoa(ia));
872189592Sbms		ims->ims_st[1].ex -= n;
873189592Sbms	} else if (lims->imsl_st[0] == MCAST_INCLUDE) {
874189592Sbms		CTR3(KTR_IGMPV3, "%s: t1 in -= %d on %s",
875189592Sbms		    __func__, n, inet_ntoa(ia));
876189592Sbms		ims->ims_st[1].in -= n;
877189592Sbms	}
878189592Sbms
879189592Sbms	if (lims->imsl_st[1] == MCAST_EXCLUDE) {
880189592Sbms		CTR3(KTR_IGMPV3, "%s: t1 ex += %d on %s",
881189592Sbms		    __func__, n, inet_ntoa(ia));
882189592Sbms		ims->ims_st[1].ex += n;
883189592Sbms	} else if (lims->imsl_st[1] == MCAST_INCLUDE) {
884189592Sbms		CTR3(KTR_IGMPV3, "%s: t1 in += %d on %s",
885189592Sbms		    __func__, n, inet_ntoa(ia));
886189592Sbms		ims->ims_st[1].in += n;
887189592Sbms	}
888189592Sbms}
889189592Sbms
890189592Sbms/*
891189592Sbms * Atomically update the global in_multi state, when a membership's
892189592Sbms * filter list is being updated in any way.
893189592Sbms *
894189592Sbms * imf is the per-inpcb-membership group filter pointer.
895189592Sbms * A fake imf may be passed for in-kernel consumers.
896189592Sbms *
897189592Sbms * XXX This is a candidate for a set-symmetric-difference style loop
898189592Sbms * which would eliminate the repeated lookup from root of ims nodes,
899189592Sbms * as they share the same key space.
900189592Sbms *
901189592Sbms * If any error occurred this function will back out of refcounts
902189592Sbms * and return a non-zero value.
903189592Sbms */
904189592Sbmsstatic int
905189592Sbmsinm_merge(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
906189592Sbms{
907189592Sbms	struct ip_msource	*ims, *nims;
908189592Sbms	struct in_msource	*lims;
909189592Sbms	int			 schanged, error;
910189592Sbms	int			 nsrc0, nsrc1;
911189592Sbms
912189592Sbms	schanged = 0;
913189592Sbms	error = 0;
914189592Sbms	nsrc1 = nsrc0 = 0;
915189592Sbms
916189592Sbms	/*
917189592Sbms	 * Update the source filters first, as this may fail.
918189592Sbms	 * Maintain count of in-mode filters at t0, t1. These are
919189592Sbms	 * used to work out if we transition into ASM mode or not.
920189592Sbms	 * Maintain a count of source filters whose state was
921189592Sbms	 * actually modified by this operation.
922189592Sbms	 */
923189592Sbms	RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
924189592Sbms		lims = (struct in_msource *)ims;
925189592Sbms		if (lims->imsl_st[0] == imf->imf_st[0]) nsrc0++;
926189592Sbms		if (lims->imsl_st[1] == imf->imf_st[1]) nsrc1++;
927189592Sbms		if (lims->imsl_st[0] == lims->imsl_st[1]) continue;
928189592Sbms		error = inm_get_source(inm, lims->ims_haddr, 0, &nims);
929189592Sbms		++schanged;
930189592Sbms		if (error)
931170613Sbms			break;
932189592Sbms		ims_merge(nims, lims, 0);
933189592Sbms	}
934189592Sbms	if (error) {
935189592Sbms		struct ip_msource *bims;
936189592Sbms
937189592Sbms		RB_FOREACH_REVERSE_FROM(ims, ip_msource_tree, nims) {
938189592Sbms			lims = (struct in_msource *)ims;
939189592Sbms			if (lims->imsl_st[0] == lims->imsl_st[1])
940189592Sbms				continue;
941189592Sbms			(void)inm_get_source(inm, lims->ims_haddr, 1, &bims);
942189592Sbms			if (bims == NULL)
943189592Sbms				continue;
944189592Sbms			ims_merge(bims, lims, 1);
945170613Sbms		}
946189592Sbms		goto out_reap;
947189592Sbms	}
948170613Sbms
949189592Sbms	CTR3(KTR_IGMPV3, "%s: imf filters in-mode: %d at t0, %d at t1",
950189592Sbms	    __func__, nsrc0, nsrc1);
951170613Sbms
952189592Sbms	/* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
953189592Sbms	if (imf->imf_st[0] == imf->imf_st[1] &&
954189592Sbms	    imf->imf_st[1] == MCAST_INCLUDE) {
955189592Sbms		if (nsrc1 == 0) {
956189592Sbms			CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
957189592Sbms			--inm->inm_st[1].iss_in;
958189592Sbms		}
959189592Sbms	}
960170613Sbms
961189592Sbms	/* Handle filter mode transition on socket. */
962189592Sbms	if (imf->imf_st[0] != imf->imf_st[1]) {
963189592Sbms		CTR3(KTR_IGMPV3, "%s: imf transition %d to %d",
964189592Sbms		    __func__, imf->imf_st[0], imf->imf_st[1]);
965189592Sbms
966189592Sbms		if (imf->imf_st[0] == MCAST_EXCLUDE) {
967189592Sbms			CTR1(KTR_IGMPV3, "%s: --ex on inm at t1", __func__);
968189592Sbms			--inm->inm_st[1].iss_ex;
969189592Sbms		} else if (imf->imf_st[0] == MCAST_INCLUDE) {
970189592Sbms			CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
971189592Sbms			--inm->inm_st[1].iss_in;
972189592Sbms		}
973189592Sbms
974189592Sbms		if (imf->imf_st[1] == MCAST_EXCLUDE) {
975189592Sbms			CTR1(KTR_IGMPV3, "%s: ex++ on inm at t1", __func__);
976189592Sbms			inm->inm_st[1].iss_ex++;
977189592Sbms		} else if (imf->imf_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
978189592Sbms			CTR1(KTR_IGMPV3, "%s: in++ on inm at t1", __func__);
979189592Sbms			inm->inm_st[1].iss_in++;
980189592Sbms		}
981189592Sbms	}
982189592Sbms
983189592Sbms	/*
984189592Sbms	 * Track inm filter state in terms of listener counts.
985189592Sbms	 * If there are any exclusive listeners, stack-wide
986189592Sbms	 * membership is exclusive.
987189592Sbms	 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
988189592Sbms	 * If no listeners remain, state is undefined at t1,
989189592Sbms	 * and the IGMP lifecycle for this group should finish.
990189592Sbms	 */
991189592Sbms	if (inm->inm_st[1].iss_ex > 0) {
992189592Sbms		CTR1(KTR_IGMPV3, "%s: transition to EX", __func__);
993189592Sbms		inm->inm_st[1].iss_fmode = MCAST_EXCLUDE;
994189592Sbms	} else if (inm->inm_st[1].iss_in > 0) {
995189592Sbms		CTR1(KTR_IGMPV3, "%s: transition to IN", __func__);
996189592Sbms		inm->inm_st[1].iss_fmode = MCAST_INCLUDE;
997189592Sbms	} else {
998189592Sbms		CTR1(KTR_IGMPV3, "%s: transition to UNDEF", __func__);
999189592Sbms		inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
1000189592Sbms	}
1001189592Sbms
1002189592Sbms	/* Decrement ASM listener count on transition out of ASM mode. */
1003189592Sbms	if (imf->imf_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1004189592Sbms		if ((imf->imf_st[1] != MCAST_EXCLUDE) ||
1005189592Sbms		    (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 > 0))
1006189592Sbms			CTR1(KTR_IGMPV3, "%s: --asm on inm at t1", __func__);
1007189592Sbms			--inm->inm_st[1].iss_asm;
1008189592Sbms	}
1009189592Sbms
1010189592Sbms	/* Increment ASM listener count on transition to ASM mode. */
1011189592Sbms	if (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1012189592Sbms		CTR1(KTR_IGMPV3, "%s: asm++ on inm at t1", __func__);
1013189592Sbms		inm->inm_st[1].iss_asm++;
1014189592Sbms	}
1015189592Sbms
1016189592Sbms	CTR3(KTR_IGMPV3, "%s: merged imf %p to inm %p", __func__, imf, inm);
1017189592Sbms	inm_print(inm);
1018189592Sbms
1019189592Sbmsout_reap:
1020189592Sbms	if (schanged > 0) {
1021189592Sbms		CTR1(KTR_IGMPV3, "%s: sources changed; reaping", __func__);
1022189592Sbms		inm_reap(inm);
1023189592Sbms	}
1024189592Sbms	return (error);
1025189592Sbms}
1026189592Sbms
1027189592Sbms/*
1028189592Sbms * Mark an in_multi's filter set deltas as committed.
1029189592Sbms * Called by IGMP after a state change has been enqueued.
1030189592Sbms */
1031189592Sbmsvoid
1032189592Sbmsinm_commit(struct in_multi *inm)
1033189592Sbms{
1034189592Sbms	struct ip_msource	*ims;
1035189592Sbms
1036189592Sbms	CTR2(KTR_IGMPV3, "%s: commit inm %p", __func__, inm);
1037189592Sbms	CTR1(KTR_IGMPV3, "%s: pre commit:", __func__);
1038189592Sbms	inm_print(inm);
1039189592Sbms
1040189592Sbms	RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
1041189592Sbms		ims->ims_st[0] = ims->ims_st[1];
1042189592Sbms	}
1043189592Sbms	inm->inm_st[0] = inm->inm_st[1];
1044189592Sbms}
1045189592Sbms
1046189592Sbms/*
1047189592Sbms * Reap unreferenced nodes from an in_multi's filter set.
1048189592Sbms */
1049189592Sbmsstatic void
1050189592Sbmsinm_reap(struct in_multi *inm)
1051189592Sbms{
1052189592Sbms	struct ip_msource	*ims, *tims;
1053189592Sbms
1054189592Sbms	RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1055189592Sbms		if (ims->ims_st[0].ex > 0 || ims->ims_st[0].in > 0 ||
1056189592Sbms		    ims->ims_st[1].ex > 0 || ims->ims_st[1].in > 0 ||
1057189592Sbms		    ims->ims_stp != 0)
1058189592Sbms			continue;
1059189592Sbms		CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1060189592Sbms		RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1061189592Sbms		free(ims, M_IPMSOURCE);
1062189592Sbms		inm->inm_nsrc--;
1063189592Sbms	}
1064189592Sbms}
1065189592Sbms
1066189592Sbms/*
1067189592Sbms * Purge all source nodes from an in_multi's filter set.
1068189592Sbms */
1069189592Sbmsstatic void
1070189592Sbmsinm_purge(struct in_multi *inm)
1071189592Sbms{
1072189592Sbms	struct ip_msource	*ims, *tims;
1073189592Sbms
1074189592Sbms	RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1075189592Sbms		CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1076189592Sbms		RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1077189592Sbms		free(ims, M_IPMSOURCE);
1078189592Sbms		inm->inm_nsrc--;
1079189592Sbms	}
1080189592Sbms}
1081189592Sbms
1082189592Sbms/*
1083189592Sbms * Join a multicast group; unlocked entry point.
1084189592Sbms *
1085189592Sbms * SMPng: XXX: in_joingroup() is called from in_control() when Giant
1086189592Sbms * is not held. Fortunately, ifp is unlikely to have been detached
1087189592Sbms * at this point, so we assume it's OK to recurse.
1088189592Sbms */
1089189592Sbmsint
1090189592Sbmsin_joingroup(struct ifnet *ifp, const struct in_addr *gina,
1091189592Sbms    /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1092189592Sbms{
1093189592Sbms	int error;
1094189592Sbms
1095189592Sbms	IN_MULTI_LOCK();
1096189592Sbms	error = in_joingroup_locked(ifp, gina, imf, pinm);
1097170613Sbms	IN_MULTI_UNLOCK();
1098170613Sbms
1099189592Sbms	return (error);
1100170613Sbms}
1101170613Sbms
1102170613Sbms/*
1103189592Sbms * Join a multicast group; real entry point.
1104170613Sbms *
1105189592Sbms * Only preserves atomicity at inm level.
1106189592Sbms * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1107170613Sbms *
1108189592Sbms * If the IGMP downcall fails, the group is not joined, and an error
1109189592Sbms * code is returned.
1110170613Sbms */
1111189592Sbmsint
1112189592Sbmsin_joingroup_locked(struct ifnet *ifp, const struct in_addr *gina,
1113189592Sbms    /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1114170613Sbms{
1115189592Sbms	struct in_mfilter	 timf;
1116189592Sbms	struct in_multi		*inm;
1117189592Sbms	int			 error;
1118170613Sbms
1119189592Sbms	IN_MULTI_LOCK_ASSERT();
1120170613Sbms
1121189592Sbms	CTR4(KTR_IGMPV3, "%s: join %s on %p(%s))", __func__,
1122189592Sbms	    inet_ntoa(*gina), ifp, ifp->if_xname);
1123189592Sbms
1124189592Sbms	error = 0;
1125189592Sbms	inm = NULL;
1126189592Sbms
1127189592Sbms	/*
1128189592Sbms	 * If no imf was specified (i.e. kernel consumer),
1129189592Sbms	 * fake one up and assume it is an ASM join.
1130189592Sbms	 */
1131189592Sbms	if (imf == NULL) {
1132189592Sbms		imf_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1133189592Sbms		imf = &timf;
1134170613Sbms	}
1135170613Sbms
1136189592Sbms	error = in_getmulti(ifp, gina, &inm);
1137189592Sbms	if (error) {
1138189592Sbms		CTR1(KTR_IGMPV3, "%s: in_getmulti() failure", __func__);
1139189592Sbms		return (error);
1140189592Sbms	}
1141189592Sbms
1142189592Sbms	CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1143189592Sbms	error = inm_merge(inm, imf);
1144189592Sbms	if (error) {
1145189592Sbms		CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1146189592Sbms		goto out_inm_release;
1147189592Sbms	}
1148189592Sbms
1149189592Sbms	CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1150189592Sbms	error = igmp_change_state(inm);
1151189592Sbms	if (error) {
1152189592Sbms		CTR1(KTR_IGMPV3, "%s: failed to update source", __func__);
1153189592Sbms		goto out_inm_release;
1154189592Sbms	}
1155189592Sbms
1156189592Sbmsout_inm_release:
1157189592Sbms	if (error) {
1158189592Sbms		CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1159189592Sbms		inm_release_locked(inm);
1160189592Sbms	} else {
1161189592Sbms		*pinm = inm;
1162189592Sbms	}
1163189592Sbms
1164189592Sbms	return (error);
1165189592Sbms}
1166189592Sbms
1167189592Sbms/*
1168189592Sbms * Leave a multicast group; unlocked entry point.
1169189592Sbms */
1170189592Sbmsint
1171189592Sbmsin_leavegroup(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1172189592Sbms{
1173189592Sbms	struct ifnet *ifp;
1174189851Srwatson	int error;
1175189592Sbms
1176189592Sbms	ifp = inm->inm_ifp;
1177189592Sbms
1178170613Sbms	IN_MULTI_LOCK();
1179189592Sbms	error = in_leavegroup_locked(inm, imf);
1180170613Sbms	IN_MULTI_UNLOCK();
1181170613Sbms
1182189592Sbms	return (error);
1183170613Sbms}
1184170613Sbms
1185170613Sbms/*
1186189592Sbms * Leave a multicast group; real entry point.
1187189592Sbms * All source filters will be expunged.
1188170613Sbms *
1189189592Sbms * Only preserves atomicity at inm level.
1190189592Sbms *
1191189592Sbms * Holding the write lock for the INP which contains imf
1192189592Sbms * is highly advisable. We can't assert for it as imf does not
1193189592Sbms * contain a back-pointer to the owning inp.
1194189592Sbms *
1195189592Sbms * Note: This is not the same as inm_release(*) as this function also
1196189592Sbms * makes a state change downcall into IGMP.
1197170613Sbms */
1198189592Sbmsint
1199189592Sbmsin_leavegroup_locked(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1200170613Sbms{
1201189592Sbms	struct in_mfilter	 timf;
1202189592Sbms	int			 error;
1203170613Sbms
1204189592Sbms	error = 0;
1205189592Sbms
1206170613Sbms	IN_MULTI_LOCK_ASSERT();
1207170613Sbms
1208189592Sbms	CTR5(KTR_IGMPV3, "%s: leave inm %p, %s/%s, imf %p", __func__,
1209189592Sbms	    inm, inet_ntoa(inm->inm_addr),
1210189592Sbms	    (inm_is_ifp_detached(inm) ? "null" : inm->inm_ifp->if_xname),
1211189592Sbms	    imf);
1212170613Sbms
1213189592Sbms	/*
1214189592Sbms	 * If no imf was specified (i.e. kernel consumer),
1215189592Sbms	 * fake one up and assume it is an ASM join.
1216189592Sbms	 */
1217189592Sbms	if (imf == NULL) {
1218189592Sbms		imf_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1219189592Sbms		imf = &timf;
1220189592Sbms	}
1221170613Sbms
1222189592Sbms	/*
1223189592Sbms	 * Begin state merge transaction at IGMP layer.
1224189592Sbms	 *
1225189592Sbms	 * As this particular invocation should not cause any memory
1226189592Sbms	 * to be allocated, and there is no opportunity to roll back
1227189592Sbms	 * the transaction, it MUST NOT fail.
1228189592Sbms	 */
1229189592Sbms	CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1230189592Sbms	error = inm_merge(inm, imf);
1231189592Sbms	KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1232170613Sbms
1233189592Sbms	CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1234189592Sbms	error = igmp_change_state(inm);
1235189592Sbms	if (error)
1236189592Sbms		CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1237189592Sbms
1238189592Sbms	CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1239189592Sbms	inm_release_locked(inm);
1240189592Sbms
1241189592Sbms	return (error);
1242170613Sbms}
1243170613Sbms
1244189592Sbms/*#ifndef BURN_BRIDGES*/
1245170613Sbms/*
1246189592Sbms * Join an IPv4 multicast group in (*,G) exclusive mode.
1247189592Sbms * The group must be a 224.0.0.0/24 link-scope group.
1248189592Sbms * This KPI is for legacy kernel consumers only.
1249170613Sbms */
1250189592Sbmsstruct in_multi *
1251189592Sbmsin_addmulti(struct in_addr *ap, struct ifnet *ifp)
1252189592Sbms{
1253189592Sbms	struct in_multi *pinm;
1254189592Sbms	int error;
1255189592Sbms
1256189592Sbms	KASSERT(IN_LOCAL_GROUP(ntohl(ap->s_addr)),
1257189592Sbms	    ("%s: %s not in 224.0.0.0/24", __func__, inet_ntoa(*ap)));
1258189592Sbms
1259189592Sbms	error = in_joingroup(ifp, ap, NULL, &pinm);
1260189592Sbms	if (error != 0)
1261189592Sbms		pinm = NULL;
1262189592Sbms
1263189592Sbms	return (pinm);
1264189592Sbms}
1265189592Sbms
1266189592Sbms/*
1267189592Sbms * Leave an IPv4 multicast group, assumed to be in exclusive (*,G) mode.
1268189592Sbms * This KPI is for legacy kernel consumers only.
1269189592Sbms */
1270189592Sbmsvoid
1271189592Sbmsin_delmulti(struct in_multi *inm)
1272189592Sbms{
1273189592Sbms
1274189592Sbms	(void)in_leavegroup(inm, NULL);
1275189592Sbms}
1276189592Sbms/*#endif*/
1277189592Sbms
1278189592Sbms/*
1279189592Sbms * Block or unblock an ASM multicast source on an inpcb.
1280189592Sbms * This implements the delta-based API described in RFC 3678.
1281189592Sbms *
1282189592Sbms * The delta-based API applies only to exclusive-mode memberships.
1283189592Sbms * An IGMP downcall will be performed.
1284189592Sbms *
1285189592Sbms * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1286189592Sbms *
1287189592Sbms * Return 0 if successful, otherwise return an appropriate error code.
1288189592Sbms */
1289170613Sbmsstatic int
1290189592Sbmsinp_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1291170613Sbms{
1292170613Sbms	struct group_source_req		 gsr;
1293170613Sbms	sockunion_t			*gsa, *ssa;
1294170613Sbms	struct ifnet			*ifp;
1295170613Sbms	struct in_mfilter		*imf;
1296170613Sbms	struct ip_moptions		*imo;
1297170613Sbms	struct in_msource		*ims;
1298189592Sbms	struct in_multi			*inm;
1299170613Sbms	size_t				 idx;
1300189592Sbms	uint16_t			 fmode;
1301189592Sbms	int				 error, doblock;
1302170613Sbms
1303170613Sbms	ifp = NULL;
1304170613Sbms	error = 0;
1305189592Sbms	doblock = 0;
1306170613Sbms
1307170613Sbms	memset(&gsr, 0, sizeof(struct group_source_req));
1308170613Sbms	gsa = (sockunion_t *)&gsr.gsr_group;
1309170613Sbms	ssa = (sockunion_t *)&gsr.gsr_source;
1310170613Sbms
1311170613Sbms	switch (sopt->sopt_name) {
1312170613Sbms	case IP_BLOCK_SOURCE:
1313170613Sbms	case IP_UNBLOCK_SOURCE: {
1314170613Sbms		struct ip_mreq_source	 mreqs;
1315170613Sbms
1316170613Sbms		error = sooptcopyin(sopt, &mreqs,
1317170613Sbms		    sizeof(struct ip_mreq_source),
1318170613Sbms		    sizeof(struct ip_mreq_source));
1319170613Sbms		if (error)
1320170613Sbms			return (error);
1321170613Sbms
1322170613Sbms		gsa->sin.sin_family = AF_INET;
1323170613Sbms		gsa->sin.sin_len = sizeof(struct sockaddr_in);
1324170613Sbms		gsa->sin.sin_addr = mreqs.imr_multiaddr;
1325170613Sbms
1326170613Sbms		ssa->sin.sin_family = AF_INET;
1327170613Sbms		ssa->sin.sin_len = sizeof(struct sockaddr_in);
1328170613Sbms		ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1329170613Sbms
1330189592Sbms		if (!in_nullhost(mreqs.imr_interface))
1331170613Sbms			INADDR_TO_IFP(mreqs.imr_interface, ifp);
1332170613Sbms
1333170613Sbms		if (sopt->sopt_name == IP_BLOCK_SOURCE)
1334189592Sbms			doblock = 1;
1335170613Sbms
1336189592Sbms		CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
1337189592Sbms		    __func__, inet_ntoa(mreqs.imr_interface), ifp);
1338170613Sbms		break;
1339170613Sbms	    }
1340170613Sbms
1341170613Sbms	case MCAST_BLOCK_SOURCE:
1342170613Sbms	case MCAST_UNBLOCK_SOURCE:
1343170613Sbms		error = sooptcopyin(sopt, &gsr,
1344170613Sbms		    sizeof(struct group_source_req),
1345170613Sbms		    sizeof(struct group_source_req));
1346170613Sbms		if (error)
1347170613Sbms			return (error);
1348170613Sbms
1349170613Sbms		if (gsa->sin.sin_family != AF_INET ||
1350170613Sbms		    gsa->sin.sin_len != sizeof(struct sockaddr_in))
1351170613Sbms			return (EINVAL);
1352170613Sbms
1353170613Sbms		if (ssa->sin.sin_family != AF_INET ||
1354170613Sbms		    ssa->sin.sin_len != sizeof(struct sockaddr_in))
1355170613Sbms			return (EINVAL);
1356170613Sbms
1357181803Sbz		if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1358170613Sbms			return (EADDRNOTAVAIL);
1359170613Sbms
1360170613Sbms		ifp = ifnet_byindex(gsr.gsr_interface);
1361170613Sbms
1362170613Sbms		if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1363189592Sbms			doblock = 1;
1364170613Sbms		break;
1365170613Sbms
1366170613Sbms	default:
1367189592Sbms		CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
1368189592Sbms		    __func__, sopt->sopt_name);
1369170613Sbms		return (EOPNOTSUPP);
1370170613Sbms		break;
1371170613Sbms	}
1372170613Sbms
1373170613Sbms	if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1374170613Sbms		return (EINVAL);
1375170613Sbms
1376170613Sbms	/*
1377170613Sbms	 * Check if we are actually a member of this group.
1378170613Sbms	 */
1379170613Sbms	imo = inp_findmoptions(inp);
1380170613Sbms	idx = imo_match_group(imo, ifp, &gsa->sa);
1381170613Sbms	if (idx == -1 || imo->imo_mfilters == NULL) {
1382170613Sbms		error = EADDRNOTAVAIL;
1383189592Sbms		goto out_inp_locked;
1384170613Sbms	}
1385170613Sbms
1386170613Sbms	KASSERT(imo->imo_mfilters != NULL,
1387170613Sbms	    ("%s: imo_mfilters not allocated", __func__));
1388170613Sbms	imf = &imo->imo_mfilters[idx];
1389189592Sbms	inm = imo->imo_membership[idx];
1390170613Sbms
1391170613Sbms	/*
1392189592Sbms	 * Attempting to use the delta-based API on an
1393189592Sbms	 * non exclusive-mode membership is an error.
1394170613Sbms	 */
1395189592Sbms	fmode = imf->imf_st[0];
1396189592Sbms	if (fmode != MCAST_EXCLUDE) {
1397189592Sbms		error = EINVAL;
1398189592Sbms		goto out_inp_locked;
1399170613Sbms	}
1400189592Sbms
1401189592Sbms	/*
1402189592Sbms	 * Deal with error cases up-front:
1403189592Sbms	 *  Asked to block, but already blocked; or
1404189592Sbms	 *  Asked to unblock, but nothing to unblock.
1405189592Sbms	 * If adding a new block entry, allocate it.
1406189592Sbms	 */
1407170613Sbms	ims = imo_match_source(imo, idx, &ssa->sa);
1408189592Sbms	if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1409189592Sbms		CTR3(KTR_IGMPV3, "%s: source %s %spresent", __func__,
1410189592Sbms		    inet_ntoa(ssa->sin.sin_addr), doblock ? "" : "not ");
1411189592Sbms		error = EADDRNOTAVAIL;
1412189592Sbms		goto out_inp_locked;
1413189592Sbms	}
1414189592Sbms
1415189592Sbms	INP_WLOCK_ASSERT(inp);
1416189592Sbms
1417189592Sbms	/*
1418189592Sbms	 * Begin state merge transaction at socket layer.
1419189592Sbms	 */
1420189592Sbms	if (doblock) {
1421189592Sbms		CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
1422189592Sbms		ims = imf_graft(imf, fmode, &ssa->sin);
1423189592Sbms		if (ims == NULL)
1424189592Sbms			error = ENOMEM;
1425170613Sbms	} else {
1426189592Sbms		CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
1427189592Sbms		error = imf_prune(imf, &ssa->sin);
1428170613Sbms	}
1429170613Sbms
1430189592Sbms	if (error) {
1431189592Sbms		CTR1(KTR_IGMPV3, "%s: merge imf state failed", __func__);
1432189592Sbms		goto out_imf_rollback;
1433189592Sbms	}
1434189592Sbms
1435189592Sbms	/*
1436189592Sbms	 * Begin state merge transaction at IGMP layer.
1437189592Sbms	 */
1438189592Sbms	IN_MULTI_LOCK();
1439189592Sbms
1440189592Sbms	CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1441189592Sbms	error = inm_merge(inm, imf);
1442189592Sbms	if (error) {
1443189592Sbms		CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1444189592Sbms		goto out_imf_rollback;
1445189592Sbms	}
1446189592Sbms
1447189592Sbms	CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1448189592Sbms	error = igmp_change_state(inm);
1449189592Sbms	if (error)
1450189592Sbms		CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1451189592Sbms
1452189592Sbms	IN_MULTI_UNLOCK();
1453189592Sbms
1454189592Sbmsout_imf_rollback:
1455189592Sbms	if (error)
1456189592Sbms		imf_rollback(imf);
1457189592Sbms	else
1458189592Sbms		imf_commit(imf);
1459189592Sbms
1460189592Sbms	imf_reap(imf);
1461189592Sbms
1462189592Sbmsout_inp_locked:
1463178285Srwatson	INP_WUNLOCK(inp);
1464170613Sbms	return (error);
1465170613Sbms}
1466170613Sbms
1467170613Sbms/*
1468170613Sbms * Given an inpcb, return its multicast options structure pointer.  Accepts
1469170613Sbms * an unlocked inpcb pointer, but will return it locked.  May sleep.
1470189592Sbms *
1471189592Sbms * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1472189592Sbms * SMPng: NOTE: Returns with the INP write lock held.
1473170613Sbms */
1474170613Sbmsstatic struct ip_moptions *
1475170613Sbmsinp_findmoptions(struct inpcb *inp)
1476170613Sbms{
1477170613Sbms	struct ip_moptions	 *imo;
1478170613Sbms	struct in_multi		**immp;
1479170613Sbms	struct in_mfilter	 *imfp;
1480170613Sbms	size_t			  idx;
1481170613Sbms
1482178285Srwatson	INP_WLOCK(inp);
1483170613Sbms	if (inp->inp_moptions != NULL)
1484170613Sbms		return (inp->inp_moptions);
1485170613Sbms
1486178285Srwatson	INP_WUNLOCK(inp);
1487170613Sbms
1488189592Sbms	imo = malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK);
1489189592Sbms	immp = malloc(sizeof(*immp) * IP_MIN_MEMBERSHIPS, M_IPMOPTS,
1490189592Sbms	    M_WAITOK | M_ZERO);
1491189592Sbms	imfp = malloc(sizeof(struct in_mfilter) * IP_MIN_MEMBERSHIPS,
1492189592Sbms	    M_INMFILTER, M_WAITOK);
1493170613Sbms
1494170613Sbms	imo->imo_multicast_ifp = NULL;
1495170613Sbms	imo->imo_multicast_addr.s_addr = INADDR_ANY;
1496170613Sbms	imo->imo_multicast_vif = -1;
1497170613Sbms	imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1498189357Sbms	imo->imo_multicast_loop = in_mcast_loop;
1499170613Sbms	imo->imo_num_memberships = 0;
1500170613Sbms	imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1501170613Sbms	imo->imo_membership = immp;
1502170613Sbms
1503170613Sbms	/* Initialize per-group source filters. */
1504189592Sbms	for (idx = 0; idx < IP_MIN_MEMBERSHIPS; idx++)
1505189592Sbms		imf_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE);
1506170613Sbms	imo->imo_mfilters = imfp;
1507170613Sbms
1508178285Srwatson	INP_WLOCK(inp);
1509170613Sbms	if (inp->inp_moptions != NULL) {
1510189592Sbms		free(imfp, M_INMFILTER);
1511170613Sbms		free(immp, M_IPMOPTS);
1512170613Sbms		free(imo, M_IPMOPTS);
1513170613Sbms		return (inp->inp_moptions);
1514170613Sbms	}
1515170613Sbms	inp->inp_moptions = imo;
1516170613Sbms	return (imo);
1517170613Sbms}
1518170613Sbms
1519170613Sbms/*
1520170613Sbms * Discard the IP multicast options (and source filters).
1521189592Sbms *
1522189592Sbms * SMPng: NOTE: assumes INP write lock is held.
1523170613Sbms */
1524170613Sbmsvoid
1525170613Sbmsinp_freemoptions(struct ip_moptions *imo)
1526170613Sbms{
1527170613Sbms	struct in_mfilter	*imf;
1528170613Sbms	size_t			 idx, nmships;
1529170613Sbms
1530170613Sbms	KASSERT(imo != NULL, ("%s: ip_moptions is NULL", __func__));
1531170613Sbms
1532170613Sbms	nmships = imo->imo_num_memberships;
1533170613Sbms	for (idx = 0; idx < nmships; ++idx) {
1534189592Sbms		imf = imo->imo_mfilters ? &imo->imo_mfilters[idx] : NULL;
1535189592Sbms		if (imf)
1536189592Sbms			imf_leave(imf);
1537189592Sbms		(void)in_leavegroup(imo->imo_membership[idx], imf);
1538189592Sbms		if (imf)
1539189592Sbms			imf_purge(imf);
1540170613Sbms	}
1541170613Sbms
1542189592Sbms	if (imo->imo_mfilters)
1543189592Sbms		free(imo->imo_mfilters, M_INMFILTER);
1544170613Sbms	free(imo->imo_membership, M_IPMOPTS);
1545170613Sbms	free(imo, M_IPMOPTS);
1546170613Sbms}
1547170613Sbms
1548170613Sbms/*
1549170613Sbms * Atomically get source filters on a socket for an IPv4 multicast group.
1550170613Sbms * Called with INP lock held; returns with lock released.
1551170613Sbms */
1552170613Sbmsstatic int
1553170613Sbmsinp_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1554170613Sbms{
1555170613Sbms	struct __msfilterreq	 msfr;
1556170613Sbms	sockunion_t		*gsa;
1557170613Sbms	struct ifnet		*ifp;
1558170613Sbms	struct ip_moptions	*imo;
1559170613Sbms	struct in_mfilter	*imf;
1560189592Sbms	struct ip_msource	*ims;
1561189592Sbms	struct in_msource	*lims;
1562189592Sbms	struct sockaddr_in	*psin;
1563170613Sbms	struct sockaddr_storage	*ptss;
1564170613Sbms	struct sockaddr_storage	*tss;
1565170613Sbms	int			 error;
1566189592Sbms	size_t			 idx, nsrcs, ncsrcs;
1567170613Sbms
1568178285Srwatson	INP_WLOCK_ASSERT(inp);
1569170613Sbms
1570170613Sbms	imo = inp->inp_moptions;
1571170613Sbms	KASSERT(imo != NULL, ("%s: null ip_moptions", __func__));
1572170613Sbms
1573178285Srwatson	INP_WUNLOCK(inp);
1574170613Sbms
1575170613Sbms	error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1576170613Sbms	    sizeof(struct __msfilterreq));
1577170613Sbms	if (error)
1578170613Sbms		return (error);
1579170613Sbms
1580181803Sbz	if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
1581170613Sbms		return (EINVAL);
1582170613Sbms
1583170613Sbms	ifp = ifnet_byindex(msfr.msfr_ifindex);
1584170613Sbms	if (ifp == NULL)
1585170613Sbms		return (EINVAL);
1586170613Sbms
1587178285Srwatson	INP_WLOCK(inp);
1588170613Sbms
1589170613Sbms	/*
1590170613Sbms	 * Lookup group on the socket.
1591170613Sbms	 */
1592170613Sbms	gsa = (sockunion_t *)&msfr.msfr_group;
1593170613Sbms	idx = imo_match_group(imo, ifp, &gsa->sa);
1594170613Sbms	if (idx == -1 || imo->imo_mfilters == NULL) {
1595178285Srwatson		INP_WUNLOCK(inp);
1596170613Sbms		return (EADDRNOTAVAIL);
1597170613Sbms	}
1598170613Sbms	imf = &imo->imo_mfilters[idx];
1599170613Sbms
1600170613Sbms	/*
1601189592Sbms	 * Ignore memberships which are in limbo.
1602189592Sbms	 */
1603189592Sbms	if (imf->imf_st[1] == MCAST_UNDEFINED) {
1604189592Sbms		INP_WUNLOCK(inp);
1605189592Sbms		return (EAGAIN);
1606189592Sbms	}
1607189592Sbms	msfr.msfr_fmode = imf->imf_st[1];
1608189592Sbms
1609189592Sbms	/*
1610170613Sbms	 * If the user specified a buffer, copy out the source filter
1611170613Sbms	 * entries to userland gracefully.
1612189592Sbms	 * We only copy out the number of entries which userland
1613189592Sbms	 * has asked for, but we always tell userland how big the
1614189592Sbms	 * buffer really needs to be.
1615170613Sbms	 */
1616170613Sbms	tss = NULL;
1617170613Sbms	if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1618184214Sdes		tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1619189592Sbms		    M_TEMP, M_NOWAIT | M_ZERO);
1620170613Sbms		if (tss == NULL) {
1621189592Sbms			INP_WUNLOCK(inp);
1622189592Sbms			return (ENOBUFS);
1623170613Sbms		}
1624170613Sbms	}
1625170613Sbms
1626189592Sbms	/*
1627189592Sbms	 * Count number of sources in-mode at t0.
1628189592Sbms	 * If buffer space exists and remains, copy out source entries.
1629189592Sbms	 */
1630189592Sbms	nsrcs = msfr.msfr_nsrcs;
1631189592Sbms	ncsrcs = 0;
1632189592Sbms	ptss = tss;
1633189592Sbms	RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
1634189592Sbms		lims = (struct in_msource *)ims;
1635189592Sbms		if (lims->imsl_st[0] == MCAST_UNDEFINED ||
1636189592Sbms		    lims->imsl_st[0] != imf->imf_st[0])
1637189592Sbms			continue;
1638189592Sbms		++ncsrcs;
1639191659Sbms		if (tss != NULL && nsrcs > 0) {
1640191659Sbms			psin = (struct sockaddr_in *)ptss;
1641189592Sbms			psin->sin_family = AF_INET;
1642189592Sbms			psin->sin_len = sizeof(struct sockaddr_in);
1643189592Sbms			psin->sin_addr.s_addr = htonl(lims->ims_haddr);
1644191659Sbms			psin->sin_port = 0;
1645191659Sbms			++ptss;
1646191659Sbms			--nsrcs;
1647189592Sbms		}
1648189592Sbms	}
1649189592Sbms
1650178285Srwatson	INP_WUNLOCK(inp);
1651170613Sbms
1652170613Sbms	if (tss != NULL) {
1653170613Sbms		error = copyout(tss, msfr.msfr_srcs,
1654170613Sbms		    sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1655184205Sdes		free(tss, M_TEMP);
1656189592Sbms		if (error)
1657189592Sbms			return (error);
1658170613Sbms	}
1659170613Sbms
1660189592Sbms	msfr.msfr_nsrcs = ncsrcs;
1661170613Sbms	error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1662170613Sbms
1663170613Sbms	return (error);
1664170613Sbms}
1665170613Sbms
1666170613Sbms/*
1667170613Sbms * Return the IP multicast options in response to user getsockopt().
1668170613Sbms */
1669170613Sbmsint
1670170613Sbmsinp_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1671170613Sbms{
1672170613Sbms	struct ip_mreqn		 mreqn;
1673170613Sbms	struct ip_moptions	*imo;
1674170613Sbms	struct ifnet		*ifp;
1675170613Sbms	struct in_ifaddr	*ia;
1676170613Sbms	int			 error, optval;
1677170613Sbms	u_char			 coptval;
1678170613Sbms
1679178285Srwatson	INP_WLOCK(inp);
1680170613Sbms	imo = inp->inp_moptions;
1681171746Scsjp	/*
1682171746Scsjp	 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1683171746Scsjp	 * or is a divert socket, reject it.
1684171746Scsjp	 */
1685171746Scsjp	if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1686171746Scsjp	    (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1687171746Scsjp	    inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
1688178285Srwatson		INP_WUNLOCK(inp);
1689171746Scsjp		return (EOPNOTSUPP);
1690171746Scsjp	}
1691170613Sbms
1692170613Sbms	error = 0;
1693170613Sbms	switch (sopt->sopt_name) {
1694170613Sbms	case IP_MULTICAST_VIF:
1695170613Sbms		if (imo != NULL)
1696170613Sbms			optval = imo->imo_multicast_vif;
1697170613Sbms		else
1698170613Sbms			optval = -1;
1699178285Srwatson		INP_WUNLOCK(inp);
1700170613Sbms		error = sooptcopyout(sopt, &optval, sizeof(int));
1701170613Sbms		break;
1702170613Sbms
1703170613Sbms	case IP_MULTICAST_IF:
1704170613Sbms		memset(&mreqn, 0, sizeof(struct ip_mreqn));
1705170613Sbms		if (imo != NULL) {
1706170613Sbms			ifp = imo->imo_multicast_ifp;
1707189592Sbms			if (!in_nullhost(imo->imo_multicast_addr)) {
1708170613Sbms				mreqn.imr_address = imo->imo_multicast_addr;
1709170613Sbms			} else if (ifp != NULL) {
1710170613Sbms				mreqn.imr_ifindex = ifp->if_index;
1711170613Sbms				IFP_TO_IA(ifp, ia);
1712170613Sbms				if (ia != NULL) {
1713170613Sbms					mreqn.imr_address =
1714170613Sbms					    IA_SIN(ia)->sin_addr;
1715194760Srwatson					ifa_free(&ia->ia_ifa);
1716170613Sbms				}
1717170613Sbms			}
1718170613Sbms		}
1719178285Srwatson		INP_WUNLOCK(inp);
1720170613Sbms		if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
1721170613Sbms			error = sooptcopyout(sopt, &mreqn,
1722170613Sbms			    sizeof(struct ip_mreqn));
1723170613Sbms		} else {
1724170613Sbms			error = sooptcopyout(sopt, &mreqn.imr_address,
1725170613Sbms			    sizeof(struct in_addr));
1726170613Sbms		}
1727170613Sbms		break;
1728170613Sbms
1729170613Sbms	case IP_MULTICAST_TTL:
1730170613Sbms		if (imo == 0)
1731170613Sbms			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1732170613Sbms		else
1733170613Sbms			optval = coptval = imo->imo_multicast_ttl;
1734178285Srwatson		INP_WUNLOCK(inp);
1735170613Sbms		if (sopt->sopt_valsize == sizeof(u_char))
1736170613Sbms			error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1737170613Sbms		else
1738170613Sbms			error = sooptcopyout(sopt, &optval, sizeof(int));
1739170613Sbms		break;
1740170613Sbms
1741170613Sbms	case IP_MULTICAST_LOOP:
1742170613Sbms		if (imo == 0)
1743170613Sbms			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1744170613Sbms		else
1745170613Sbms			optval = coptval = imo->imo_multicast_loop;
1746178285Srwatson		INP_WUNLOCK(inp);
1747170613Sbms		if (sopt->sopt_valsize == sizeof(u_char))
1748170613Sbms			error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1749170613Sbms		else
1750170613Sbms			error = sooptcopyout(sopt, &optval, sizeof(int));
1751170613Sbms		break;
1752170613Sbms
1753170613Sbms	case IP_MSFILTER:
1754170613Sbms		if (imo == NULL) {
1755170613Sbms			error = EADDRNOTAVAIL;
1756178285Srwatson			INP_WUNLOCK(inp);
1757170613Sbms		} else {
1758170613Sbms			error = inp_get_source_filters(inp, sopt);
1759170613Sbms		}
1760170613Sbms		break;
1761170613Sbms
1762170613Sbms	default:
1763178285Srwatson		INP_WUNLOCK(inp);
1764170613Sbms		error = ENOPROTOOPT;
1765170613Sbms		break;
1766170613Sbms	}
1767170613Sbms
1768170613Sbms	INP_UNLOCK_ASSERT(inp);
1769170613Sbms
1770170613Sbms	return (error);
1771170613Sbms}
1772170613Sbms
1773170613Sbms/*
1774189592Sbms * Look up the ifnet to use for a multicast group membership,
1775189592Sbms * given the IPv4 address of an interface, and the IPv4 group address.
1776189592Sbms *
1777189592Sbms * This routine exists to support legacy multicast applications
1778189592Sbms * which do not understand that multicast memberships are scoped to
1779189592Sbms * specific physical links in the networking stack, or which need
1780189592Sbms * to join link-scope groups before IPv4 addresses are configured.
1781189592Sbms *
1782189592Sbms * If inp is non-NULL, use this socket's current FIB number for any
1783189592Sbms * required FIB lookup.
1784189592Sbms * If ina is INADDR_ANY, look up the group address in the unicast FIB,
1785189592Sbms * and use its ifp; usually, this points to the default next-hop.
1786189592Sbms *
1787189592Sbms * If the FIB lookup fails, attempt to use the first non-loopback
1788189592Sbms * interface with multicast capability in the system as a
1789189592Sbms * last resort. The legacy IPv4 ASM API requires that we do
1790189592Sbms * this in order to allow groups to be joined when the routing
1791189592Sbms * table has not yet been populated during boot.
1792189592Sbms *
1793189592Sbms * Returns NULL if no ifp could be found.
1794189592Sbms *
1795189592Sbms * SMPng: TODO: Acquire the appropriate locks for INADDR_TO_IFP.
1796189592Sbms * FUTURE: Implement IPv4 source-address selection.
1797189592Sbms */
1798189592Sbmsstatic struct ifnet *
1799189592Sbmsinp_lookup_mcast_ifp(const struct inpcb *inp,
1800189592Sbms    const struct sockaddr_in *gsin, const struct in_addr ina)
1801189592Sbms{
1802189592Sbms	struct ifnet *ifp;
1803189592Sbms
1804189592Sbms	KASSERT(gsin->sin_family == AF_INET, ("%s: not AF_INET", __func__));
1805189592Sbms	KASSERT(IN_MULTICAST(ntohl(gsin->sin_addr.s_addr)),
1806189592Sbms	    ("%s: not multicast", __func__));
1807189592Sbms
1808189592Sbms	ifp = NULL;
1809189592Sbms	if (!in_nullhost(ina)) {
1810189592Sbms		INADDR_TO_IFP(ina, ifp);
1811189592Sbms	} else {
1812189592Sbms		struct route ro;
1813189592Sbms
1814189592Sbms		ro.ro_rt = NULL;
1815189592Sbms		memcpy(&ro.ro_dst, gsin, sizeof(struct sockaddr_in));
1816189592Sbms		in_rtalloc_ign(&ro, 0, inp ? inp->inp_inc.inc_fibnum : 0);
1817189592Sbms		if (ro.ro_rt != NULL) {
1818189592Sbms			ifp = ro.ro_rt->rt_ifp;
1819189592Sbms			KASSERT(ifp != NULL, ("%s: null ifp", __func__));
1820189592Sbms			RTFREE(ro.ro_rt);
1821189592Sbms		} else {
1822189592Sbms			struct in_ifaddr *ia;
1823189592Sbms			struct ifnet *mifp;
1824189592Sbms
1825189592Sbms			mifp = NULL;
1826194951Srwatson			IN_IFADDR_RLOCK();
1827189592Sbms			TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1828189592Sbms				mifp = ia->ia_ifp;
1829189592Sbms				if (!(mifp->if_flags & IFF_LOOPBACK) &&
1830189592Sbms				     (mifp->if_flags & IFF_MULTICAST)) {
1831189592Sbms					ifp = mifp;
1832189592Sbms					break;
1833189592Sbms				}
1834189592Sbms			}
1835194951Srwatson			IN_IFADDR_RUNLOCK();
1836189592Sbms		}
1837189592Sbms	}
1838189592Sbms
1839189592Sbms	return (ifp);
1840189592Sbms}
1841189592Sbms
1842189592Sbms/*
1843170613Sbms * Join an IPv4 multicast group, possibly with a source.
1844170613Sbms */
1845170613Sbmsstatic int
1846170613Sbmsinp_join_group(struct inpcb *inp, struct sockopt *sopt)
1847170613Sbms{
1848170613Sbms	struct group_source_req		 gsr;
1849170613Sbms	sockunion_t			*gsa, *ssa;
1850170613Sbms	struct ifnet			*ifp;
1851170613Sbms	struct in_mfilter		*imf;
1852170613Sbms	struct ip_moptions		*imo;
1853170613Sbms	struct in_multi			*inm;
1854189592Sbms	struct in_msource		*lims;
1855170613Sbms	size_t				 idx;
1856189592Sbms	int				 error, is_new;
1857170613Sbms
1858170613Sbms	ifp = NULL;
1859189592Sbms	imf = NULL;
1860197136Sbms	lims = NULL;
1861170613Sbms	error = 0;
1862189592Sbms	is_new = 0;
1863170613Sbms
1864170613Sbms	memset(&gsr, 0, sizeof(struct group_source_req));
1865170613Sbms	gsa = (sockunion_t *)&gsr.gsr_group;
1866170613Sbms	gsa->ss.ss_family = AF_UNSPEC;
1867170613Sbms	ssa = (sockunion_t *)&gsr.gsr_source;
1868170613Sbms	ssa->ss.ss_family = AF_UNSPEC;
1869170613Sbms
1870170613Sbms	switch (sopt->sopt_name) {
1871170613Sbms	case IP_ADD_MEMBERSHIP:
1872170613Sbms	case IP_ADD_SOURCE_MEMBERSHIP: {
1873170613Sbms		struct ip_mreq_source	 mreqs;
1874170613Sbms
1875170613Sbms		if (sopt->sopt_name == IP_ADD_MEMBERSHIP) {
1876170613Sbms			error = sooptcopyin(sopt, &mreqs,
1877170613Sbms			    sizeof(struct ip_mreq),
1878170613Sbms			    sizeof(struct ip_mreq));
1879170613Sbms			/*
1880170613Sbms			 * Do argument switcharoo from ip_mreq into
1881170613Sbms			 * ip_mreq_source to avoid using two instances.
1882170613Sbms			 */
1883170613Sbms			mreqs.imr_interface = mreqs.imr_sourceaddr;
1884170613Sbms			mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
1885170613Sbms		} else if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
1886170613Sbms			error = sooptcopyin(sopt, &mreqs,
1887170613Sbms			    sizeof(struct ip_mreq_source),
1888170613Sbms			    sizeof(struct ip_mreq_source));
1889170613Sbms		}
1890170613Sbms		if (error)
1891170613Sbms			return (error);
1892170613Sbms
1893170613Sbms		gsa->sin.sin_family = AF_INET;
1894170613Sbms		gsa->sin.sin_len = sizeof(struct sockaddr_in);
1895170613Sbms		gsa->sin.sin_addr = mreqs.imr_multiaddr;
1896170613Sbms
1897170613Sbms		if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
1898170613Sbms			ssa->sin.sin_family = AF_INET;
1899170613Sbms			ssa->sin.sin_len = sizeof(struct sockaddr_in);
1900170613Sbms			ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1901170613Sbms		}
1902170613Sbms
1903196932Ssyrinx		if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1904196932Ssyrinx			return (EINVAL);
1905196932Ssyrinx
1906189592Sbms		ifp = inp_lookup_mcast_ifp(inp, &gsa->sin,
1907189592Sbms		    mreqs.imr_interface);
1908189592Sbms		CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
1909189592Sbms		    __func__, inet_ntoa(mreqs.imr_interface), ifp);
1910170613Sbms		break;
1911170613Sbms	}
1912170613Sbms
1913170613Sbms	case MCAST_JOIN_GROUP:
1914170613Sbms	case MCAST_JOIN_SOURCE_GROUP:
1915170613Sbms		if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1916170613Sbms			error = sooptcopyin(sopt, &gsr,
1917170613Sbms			    sizeof(struct group_req),
1918170613Sbms			    sizeof(struct group_req));
1919170613Sbms		} else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1920170613Sbms			error = sooptcopyin(sopt, &gsr,
1921170613Sbms			    sizeof(struct group_source_req),
1922170613Sbms			    sizeof(struct group_source_req));
1923170613Sbms		}
1924170613Sbms		if (error)
1925170613Sbms			return (error);
1926170613Sbms
1927170613Sbms		if (gsa->sin.sin_family != AF_INET ||
1928170613Sbms		    gsa->sin.sin_len != sizeof(struct sockaddr_in))
1929170613Sbms			return (EINVAL);
1930170613Sbms
1931170613Sbms		/*
1932170613Sbms		 * Overwrite the port field if present, as the sockaddr
1933170613Sbms		 * being copied in may be matched with a binary comparison.
1934170613Sbms		 */
1935170613Sbms		gsa->sin.sin_port = 0;
1936170613Sbms		if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1937170613Sbms			if (ssa->sin.sin_family != AF_INET ||
1938170613Sbms			    ssa->sin.sin_len != sizeof(struct sockaddr_in))
1939170613Sbms				return (EINVAL);
1940170613Sbms			ssa->sin.sin_port = 0;
1941170613Sbms		}
1942170613Sbms
1943196932Ssyrinx		if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1944196932Ssyrinx			return (EINVAL);
1945196932Ssyrinx
1946181803Sbz		if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1947170613Sbms			return (EADDRNOTAVAIL);
1948170613Sbms		ifp = ifnet_byindex(gsr.gsr_interface);
1949170613Sbms		break;
1950170613Sbms
1951170613Sbms	default:
1952189592Sbms		CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
1953189592Sbms		    __func__, sopt->sopt_name);
1954170613Sbms		return (EOPNOTSUPP);
1955170613Sbms		break;
1956170613Sbms	}
1957170613Sbms
1958170613Sbms	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
1959170613Sbms		return (EADDRNOTAVAIL);
1960170613Sbms
1961170613Sbms	imo = inp_findmoptions(inp);
1962170613Sbms	idx = imo_match_group(imo, ifp, &gsa->sa);
1963189592Sbms	if (idx == -1) {
1964189592Sbms		is_new = 1;
1965189592Sbms	} else {
1966189592Sbms		inm = imo->imo_membership[idx];
1967189592Sbms		imf = &imo->imo_mfilters[idx];
1968197132Sbms		if (ssa->ss.ss_family != AF_UNSPEC) {
1969197132Sbms			/*
1970199525Sbms			 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
1971197132Sbms			 * is an error. On an existing inclusive membership,
1972197132Sbms			 * it just adds the source to the filter list.
1973197132Sbms			 */
1974197132Sbms			if (imf->imf_st[1] != MCAST_INCLUDE) {
1975197132Sbms				error = EINVAL;
1976197132Sbms				goto out_inp_locked;
1977197132Sbms			}
1978197136Sbms			/*
1979197136Sbms			 * Throw out duplicates.
1980197136Sbms			 *
1981197136Sbms			 * XXX FIXME: This makes a naive assumption that
1982197136Sbms			 * even if entries exist for *ssa in this imf,
1983197136Sbms			 * they will be rejected as dupes, even if they
1984197136Sbms			 * are not valid in the current mode (in-mode).
1985197136Sbms			 *
1986197136Sbms			 * in_msource is transactioned just as for anything
1987197136Sbms			 * else in SSM -- but note naive use of inm_graft()
1988197136Sbms			 * below for allocating new filter entries.
1989197136Sbms			 *
1990197136Sbms			 * This is only an issue if someone mixes the
1991197136Sbms			 * full-state SSM API with the delta-based API,
1992197136Sbms			 * which is discouraged in the relevant RFCs.
1993197136Sbms			 */
1994197132Sbms			lims = imo_match_source(imo, idx, &ssa->sa);
1995197136Sbms			if (lims != NULL /*&&
1996197136Sbms			    lims->imsl_st[1] == MCAST_INCLUDE*/) {
1997197132Sbms				error = EADDRNOTAVAIL;
1998197132Sbms				goto out_inp_locked;
1999197132Sbms			}
2000197132Sbms		} else {
2001197132Sbms			/*
2002206452Sbms			 * MCAST_JOIN_GROUP on an existing exclusive
2003206452Sbms			 * membership is an error; return EADDRINUSE
2004206452Sbms			 * to preserve 4.4BSD API idempotence, and
2005206452Sbms			 * avoid tedious detour to code below.
2006206452Sbms			 * NOTE: This is bending RFC 3678 a bit.
2007206452Sbms			 *
2008197135Sbms			 * On an existing inclusive membership, this is also
2009197135Sbms			 * an error; if you want to change filter mode,
2010197135Sbms			 * you must use the userland API setsourcefilter().
2011197135Sbms			 * XXX We don't reject this for imf in UNDEFINED
2012197135Sbms			 * state at t1, because allocation of a filter
2013197135Sbms			 * is atomic with allocation of a membership.
2014197132Sbms			 */
2015197135Sbms			error = EINVAL;
2016206452Sbms			if (imf->imf_st[1] == MCAST_EXCLUDE)
2017206452Sbms				error = EADDRINUSE;
2018197135Sbms			goto out_inp_locked;
2019189592Sbms		}
2020170613Sbms	}
2021170613Sbms
2022170613Sbms	/*
2023189592Sbms	 * Begin state merge transaction at socket layer.
2024170613Sbms	 */
2025189592Sbms	INP_WLOCK_ASSERT(inp);
2026189592Sbms
2027189592Sbms	if (is_new) {
2028189592Sbms		if (imo->imo_num_memberships == imo->imo_max_memberships) {
2029189592Sbms			error = imo_grow(imo);
2030189592Sbms			if (error)
2031189592Sbms				goto out_inp_locked;
2032189592Sbms		}
2033189592Sbms		/*
2034189592Sbms		 * Allocate the new slot upfront so we can deal with
2035189592Sbms		 * grafting the new source filter in same code path
2036189592Sbms		 * as for join-source on existing membership.
2037189592Sbms		 */
2038189592Sbms		idx = imo->imo_num_memberships;
2039189592Sbms		imo->imo_membership[idx] = NULL;
2040189592Sbms		imo->imo_num_memberships++;
2041189592Sbms		KASSERT(imo->imo_mfilters != NULL,
2042189592Sbms		    ("%s: imf_mfilters vector was not allocated", __func__));
2043189592Sbms		imf = &imo->imo_mfilters[idx];
2044189592Sbms		KASSERT(RB_EMPTY(&imf->imf_sources),
2045189592Sbms		    ("%s: imf_sources not empty", __func__));
2046170613Sbms	}
2047170613Sbms
2048170613Sbms	/*
2049189592Sbms	 * Graft new source into filter list for this inpcb's
2050189592Sbms	 * membership of the group. The in_multi may not have
2051197132Sbms	 * been allocated yet if this is a new membership, however,
2052197132Sbms	 * the in_mfilter slot will be allocated and must be initialized.
2053197135Sbms	 *
2054197135Sbms	 * Note: Grafting of exclusive mode filters doesn't happen
2055197135Sbms	 * in this path.
2056197136Sbms	 * XXX: Should check for non-NULL lims (node exists but may
2057197136Sbms	 * not be in-mode) for interop with full-state API.
2058170613Sbms	 */
2059189592Sbms	if (ssa->ss.ss_family != AF_UNSPEC) {
2060189592Sbms		/* Membership starts in IN mode */
2061189592Sbms		if (is_new) {
2062189592Sbms			CTR1(KTR_IGMPV3, "%s: new join w/source", __func__);
2063189592Sbms			imf_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE);
2064189592Sbms		} else {
2065189592Sbms			CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
2066189592Sbms		}
2067189592Sbms		lims = imf_graft(imf, MCAST_INCLUDE, &ssa->sin);
2068189592Sbms		if (lims == NULL) {
2069189592Sbms			CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2070189592Sbms			    __func__);
2071189592Sbms			error = ENOMEM;
2072189592Sbms			goto out_imo_free;
2073189592Sbms		}
2074197132Sbms	} else {
2075197132Sbms		/* No address specified; Membership starts in EX mode */
2076197132Sbms		if (is_new) {
2077197132Sbms			CTR1(KTR_IGMPV3, "%s: new join w/o source", __func__);
2078197132Sbms			imf_init(imf, MCAST_UNDEFINED, MCAST_EXCLUDE);
2079197132Sbms		}
2080170613Sbms	}
2081170613Sbms
2082170613Sbms	/*
2083189592Sbms	 * Begin state merge transaction at IGMP layer.
2084170613Sbms	 */
2085189592Sbms	IN_MULTI_LOCK();
2086170613Sbms
2087189592Sbms	if (is_new) {
2088189592Sbms		error = in_joingroup_locked(ifp, &gsa->sin.sin_addr, imf,
2089189592Sbms		    &inm);
2090189592Sbms		if (error)
2091189592Sbms			goto out_imo_free;
2092189592Sbms		imo->imo_membership[idx] = inm;
2093189592Sbms	} else {
2094189592Sbms		CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2095189592Sbms		error = inm_merge(inm, imf);
2096170613Sbms		if (error) {
2097189592Sbms			CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2098189592Sbms			    __func__);
2099189592Sbms			goto out_imf_rollback;
2100170613Sbms		}
2101189592Sbms		CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2102189592Sbms		error = igmp_change_state(inm);
2103189592Sbms		if (error) {
2104189592Sbms			CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2105189592Sbms			    __func__);
2106189592Sbms			goto out_imf_rollback;
2107189592Sbms		}
2108170613Sbms	}
2109170613Sbms
2110189592Sbms	IN_MULTI_UNLOCK();
2111189592Sbms
2112189592Sbmsout_imf_rollback:
2113189592Sbms	INP_WLOCK_ASSERT(inp);
2114189592Sbms	if (error) {
2115189592Sbms		imf_rollback(imf);
2116189592Sbms		if (is_new)
2117189592Sbms			imf_purge(imf);
2118189592Sbms		else
2119189592Sbms			imf_reap(imf);
2120189592Sbms	} else {
2121189592Sbms		imf_commit(imf);
2122189592Sbms	}
2123189592Sbms
2124189592Sbmsout_imo_free:
2125189592Sbms	if (error && is_new) {
2126189592Sbms		imo->imo_membership[idx] = NULL;
2127189592Sbms		--imo->imo_num_memberships;
2128189592Sbms	}
2129189592Sbms
2130189592Sbmsout_inp_locked:
2131178285Srwatson	INP_WUNLOCK(inp);
2132170613Sbms	return (error);
2133170613Sbms}
2134170613Sbms
2135170613Sbms/*
2136170613Sbms * Leave an IPv4 multicast group on an inpcb, possibly with a source.
2137170613Sbms */
2138170613Sbmsstatic int
2139170613Sbmsinp_leave_group(struct inpcb *inp, struct sockopt *sopt)
2140170613Sbms{
2141170613Sbms	struct group_source_req		 gsr;
2142170613Sbms	struct ip_mreq_source		 mreqs;
2143170613Sbms	sockunion_t			*gsa, *ssa;
2144170613Sbms	struct ifnet			*ifp;
2145170613Sbms	struct in_mfilter		*imf;
2146170613Sbms	struct ip_moptions		*imo;
2147189592Sbms	struct in_msource		*ims;
2148170613Sbms	struct in_multi			*inm;
2149170613Sbms	size_t				 idx;
2150189592Sbms	int				 error, is_final;
2151170613Sbms
2152170613Sbms	ifp = NULL;
2153170613Sbms	error = 0;
2154189592Sbms	is_final = 1;
2155170613Sbms
2156170613Sbms	memset(&gsr, 0, sizeof(struct group_source_req));
2157170613Sbms	gsa = (sockunion_t *)&gsr.gsr_group;
2158170613Sbms	gsa->ss.ss_family = AF_UNSPEC;
2159170613Sbms	ssa = (sockunion_t *)&gsr.gsr_source;
2160170613Sbms	ssa->ss.ss_family = AF_UNSPEC;
2161170613Sbms
2162170613Sbms	switch (sopt->sopt_name) {
2163170613Sbms	case IP_DROP_MEMBERSHIP:
2164170613Sbms	case IP_DROP_SOURCE_MEMBERSHIP:
2165170613Sbms		if (sopt->sopt_name == IP_DROP_MEMBERSHIP) {
2166170613Sbms			error = sooptcopyin(sopt, &mreqs,
2167170613Sbms			    sizeof(struct ip_mreq),
2168170613Sbms			    sizeof(struct ip_mreq));
2169170613Sbms			/*
2170170613Sbms			 * Swap interface and sourceaddr arguments,
2171170613Sbms			 * as ip_mreq and ip_mreq_source are laid
2172170613Sbms			 * out differently.
2173170613Sbms			 */
2174170613Sbms			mreqs.imr_interface = mreqs.imr_sourceaddr;
2175170613Sbms			mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
2176170613Sbms		} else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2177170613Sbms			error = sooptcopyin(sopt, &mreqs,
2178170613Sbms			    sizeof(struct ip_mreq_source),
2179170613Sbms			    sizeof(struct ip_mreq_source));
2180170613Sbms		}
2181170613Sbms		if (error)
2182170613Sbms			return (error);
2183170613Sbms
2184170613Sbms		gsa->sin.sin_family = AF_INET;
2185170613Sbms		gsa->sin.sin_len = sizeof(struct sockaddr_in);
2186170613Sbms		gsa->sin.sin_addr = mreqs.imr_multiaddr;
2187170613Sbms
2188170613Sbms		if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2189170613Sbms			ssa->sin.sin_family = AF_INET;
2190170613Sbms			ssa->sin.sin_len = sizeof(struct sockaddr_in);
2191170613Sbms			ssa->sin.sin_addr = mreqs.imr_sourceaddr;
2192170613Sbms		}
2193170613Sbms
2194206452Sbms		/*
2195206452Sbms		 * Attempt to look up hinted ifp from interface address.
2196206452Sbms		 * Fallthrough with null ifp iff lookup fails, to
2197206452Sbms		 * preserve 4.4BSD mcast API idempotence.
2198206452Sbms		 * XXX NOTE WELL: The RFC 3678 API is preferred because
2199206452Sbms		 * using an IPv4 address as a key is racy.
2200206452Sbms		 */
2201206452Sbms		if (!in_nullhost(mreqs.imr_interface))
2202170613Sbms			INADDR_TO_IFP(mreqs.imr_interface, ifp);
2203170613Sbms
2204189592Sbms		CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
2205189592Sbms		    __func__, inet_ntoa(mreqs.imr_interface), ifp);
2206189592Sbms
2207170613Sbms		break;
2208170613Sbms
2209170613Sbms	case MCAST_LEAVE_GROUP:
2210170613Sbms	case MCAST_LEAVE_SOURCE_GROUP:
2211170613Sbms		if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2212170613Sbms			error = sooptcopyin(sopt, &gsr,
2213170613Sbms			    sizeof(struct group_req),
2214170613Sbms			    sizeof(struct group_req));
2215170613Sbms		} else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2216170613Sbms			error = sooptcopyin(sopt, &gsr,
2217170613Sbms			    sizeof(struct group_source_req),
2218170613Sbms			    sizeof(struct group_source_req));
2219170613Sbms		}
2220170613Sbms		if (error)
2221170613Sbms			return (error);
2222170613Sbms
2223170613Sbms		if (gsa->sin.sin_family != AF_INET ||
2224170613Sbms		    gsa->sin.sin_len != sizeof(struct sockaddr_in))
2225170613Sbms			return (EINVAL);
2226170613Sbms
2227170613Sbms		if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2228170613Sbms			if (ssa->sin.sin_family != AF_INET ||
2229170613Sbms			    ssa->sin.sin_len != sizeof(struct sockaddr_in))
2230170613Sbms				return (EINVAL);
2231170613Sbms		}
2232170613Sbms
2233181803Sbz		if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
2234170613Sbms			return (EADDRNOTAVAIL);
2235170613Sbms
2236170613Sbms		ifp = ifnet_byindex(gsr.gsr_interface);
2237206452Sbms
2238206452Sbms		if (ifp == NULL)
2239206452Sbms			return (EADDRNOTAVAIL);
2240170613Sbms		break;
2241170613Sbms
2242170613Sbms	default:
2243189592Sbms		CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2244189592Sbms		    __func__, sopt->sopt_name);
2245170613Sbms		return (EOPNOTSUPP);
2246170613Sbms		break;
2247170613Sbms	}
2248170613Sbms
2249170613Sbms	if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2250170613Sbms		return (EINVAL);
2251170613Sbms
2252170613Sbms	/*
2253170613Sbms	 * Find the membership in the membership array.
2254170613Sbms	 */
2255170613Sbms	imo = inp_findmoptions(inp);
2256170613Sbms	idx = imo_match_group(imo, ifp, &gsa->sa);
2257170613Sbms	if (idx == -1) {
2258170613Sbms		error = EADDRNOTAVAIL;
2259189592Sbms		goto out_inp_locked;
2260170613Sbms	}
2261189592Sbms	inm = imo->imo_membership[idx];
2262170613Sbms	imf = &imo->imo_mfilters[idx];
2263170613Sbms
2264189592Sbms	if (ssa->ss.ss_family != AF_UNSPEC)
2265189592Sbms		is_final = 0;
2266189592Sbms
2267170613Sbms	/*
2268189592Sbms	 * Begin state merge transaction at socket layer.
2269189592Sbms	 */
2270189592Sbms	INP_WLOCK_ASSERT(inp);
2271189592Sbms
2272189592Sbms	/*
2273170613Sbms	 * If we were instructed only to leave a given source, do so.
2274189592Sbms	 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2275170613Sbms	 */
2276189592Sbms	if (is_final) {
2277189592Sbms		imf_leave(imf);
2278189592Sbms	} else {
2279189592Sbms		if (imf->imf_st[0] == MCAST_EXCLUDE) {
2280189592Sbms			error = EADDRNOTAVAIL;
2281189592Sbms			goto out_inp_locked;
2282170613Sbms		}
2283189592Sbms		ims = imo_match_source(imo, idx, &ssa->sa);
2284189592Sbms		if (ims == NULL) {
2285189592Sbms			CTR3(KTR_IGMPV3, "%s: source %s %spresent", __func__,
2286189592Sbms			    inet_ntoa(ssa->sin.sin_addr), "not ");
2287189592Sbms			error = EADDRNOTAVAIL;
2288189592Sbms			goto out_inp_locked;
2289189592Sbms		}
2290189592Sbms		CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
2291189592Sbms		error = imf_prune(imf, &ssa->sin);
2292189592Sbms		if (error) {
2293189592Sbms			CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2294189592Sbms			    __func__);
2295189592Sbms			goto out_inp_locked;
2296189592Sbms		}
2297170613Sbms	}
2298170613Sbms
2299170613Sbms	/*
2300189592Sbms	 * Begin state merge transaction at IGMP layer.
2301170613Sbms	 */
2302189592Sbms	IN_MULTI_LOCK();
2303170613Sbms
2304189592Sbms	if (is_final) {
2305189592Sbms		/*
2306189592Sbms		 * Give up the multicast address record to which
2307189592Sbms		 * the membership points.
2308189592Sbms		 */
2309189592Sbms		(void)in_leavegroup_locked(inm, imf);
2310189592Sbms	} else {
2311189592Sbms		CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2312189592Sbms		error = inm_merge(inm, imf);
2313189592Sbms		if (error) {
2314189592Sbms			CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2315189592Sbms			    __func__);
2316189592Sbms			goto out_imf_rollback;
2317170613Sbms		}
2318189592Sbms
2319189592Sbms		CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2320189592Sbms		error = igmp_change_state(inm);
2321189592Sbms		if (error) {
2322189592Sbms			CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2323189592Sbms			    __func__);
2324189592Sbms		}
2325170613Sbms	}
2326170613Sbms
2327189592Sbms	IN_MULTI_UNLOCK();
2328170613Sbms
2329189592Sbmsout_imf_rollback:
2330189592Sbms	if (error)
2331189592Sbms		imf_rollback(imf);
2332189592Sbms	else
2333189592Sbms		imf_commit(imf);
2334189592Sbms
2335189592Sbms	imf_reap(imf);
2336189592Sbms
2337189592Sbms	if (is_final) {
2338197130Sbms		/* Remove the gap in the membership and filter array. */
2339197130Sbms		for (++idx; idx < imo->imo_num_memberships; ++idx) {
2340189592Sbms			imo->imo_membership[idx-1] = imo->imo_membership[idx];
2341197130Sbms			imo->imo_mfilters[idx-1] = imo->imo_mfilters[idx];
2342197130Sbms		}
2343189592Sbms		imo->imo_num_memberships--;
2344189592Sbms	}
2345189592Sbms
2346189592Sbmsout_inp_locked:
2347178285Srwatson	INP_WUNLOCK(inp);
2348170613Sbms	return (error);
2349170613Sbms}
2350170613Sbms
2351170613Sbms/*
2352170613Sbms * Select the interface for transmitting IPv4 multicast datagrams.
2353170613Sbms *
2354170613Sbms * Either an instance of struct in_addr or an instance of struct ip_mreqn
2355170613Sbms * may be passed to this socket option. An address of INADDR_ANY or an
2356170613Sbms * interface index of 0 is used to remove a previous selection.
2357170613Sbms * When no interface is selected, one is chosen for every send.
2358170613Sbms */
2359170613Sbmsstatic int
2360170613Sbmsinp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2361170613Sbms{
2362170613Sbms	struct in_addr		 addr;
2363170613Sbms	struct ip_mreqn		 mreqn;
2364170613Sbms	struct ifnet		*ifp;
2365170613Sbms	struct ip_moptions	*imo;
2366170613Sbms	int			 error;
2367170613Sbms
2368170613Sbms	if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
2369170613Sbms		/*
2370170613Sbms		 * An interface index was specified using the
2371170613Sbms		 * Linux-derived ip_mreqn structure.
2372170613Sbms		 */
2373170613Sbms		error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn),
2374170613Sbms		    sizeof(struct ip_mreqn));
2375170613Sbms		if (error)
2376170613Sbms			return (error);
2377170613Sbms
2378181803Sbz		if (mreqn.imr_ifindex < 0 || V_if_index < mreqn.imr_ifindex)
2379170613Sbms			return (EINVAL);
2380170613Sbms
2381170613Sbms		if (mreqn.imr_ifindex == 0) {
2382170613Sbms			ifp = NULL;
2383170613Sbms		} else {
2384170613Sbms			ifp = ifnet_byindex(mreqn.imr_ifindex);
2385170613Sbms			if (ifp == NULL)
2386170613Sbms				return (EADDRNOTAVAIL);
2387170613Sbms		}
2388170613Sbms	} else {
2389170613Sbms		/*
2390170613Sbms		 * An interface was specified by IPv4 address.
2391170613Sbms		 * This is the traditional BSD usage.
2392170613Sbms		 */
2393170613Sbms		error = sooptcopyin(sopt, &addr, sizeof(struct in_addr),
2394170613Sbms		    sizeof(struct in_addr));
2395170613Sbms		if (error)
2396170613Sbms			return (error);
2397189592Sbms		if (in_nullhost(addr)) {
2398170613Sbms			ifp = NULL;
2399170613Sbms		} else {
2400170613Sbms			INADDR_TO_IFP(addr, ifp);
2401170613Sbms			if (ifp == NULL)
2402170613Sbms				return (EADDRNOTAVAIL);
2403170613Sbms		}
2404189592Sbms		CTR3(KTR_IGMPV3, "%s: ifp = %p, addr = %s", __func__, ifp,
2405189592Sbms		    inet_ntoa(addr));
2406170613Sbms	}
2407170613Sbms
2408170613Sbms	/* Reject interfaces which do not support multicast. */
2409170613Sbms	if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0)
2410170613Sbms		return (EOPNOTSUPP);
2411170613Sbms
2412170613Sbms	imo = inp_findmoptions(inp);
2413170613Sbms	imo->imo_multicast_ifp = ifp;
2414170613Sbms	imo->imo_multicast_addr.s_addr = INADDR_ANY;
2415178285Srwatson	INP_WUNLOCK(inp);
2416170613Sbms
2417170613Sbms	return (0);
2418170613Sbms}
2419170613Sbms
2420170613Sbms/*
2421170613Sbms * Atomically set source filters on a socket for an IPv4 multicast group.
2422189592Sbms *
2423189592Sbms * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
2424170613Sbms */
2425170613Sbmsstatic int
2426170613Sbmsinp_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2427170613Sbms{
2428170613Sbms	struct __msfilterreq	 msfr;
2429170613Sbms	sockunion_t		*gsa;
2430170613Sbms	struct ifnet		*ifp;
2431170613Sbms	struct in_mfilter	*imf;
2432170613Sbms	struct ip_moptions	*imo;
2433189592Sbms	struct in_multi		*inm;
2434170613Sbms	size_t			 idx;
2435170613Sbms	int			 error;
2436170613Sbms
2437170613Sbms	error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2438170613Sbms	    sizeof(struct __msfilterreq));
2439170613Sbms	if (error)
2440170613Sbms		return (error);
2441170613Sbms
2442197314Sbms	if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
2443197314Sbms		return (ENOBUFS);
2444197314Sbms
2445197314Sbms	if ((msfr.msfr_fmode != MCAST_EXCLUDE &&
2446170613Sbms	     msfr.msfr_fmode != MCAST_INCLUDE))
2447170613Sbms		return (EINVAL);
2448170613Sbms
2449170613Sbms	if (msfr.msfr_group.ss_family != AF_INET ||
2450170613Sbms	    msfr.msfr_group.ss_len != sizeof(struct sockaddr_in))
2451170613Sbms		return (EINVAL);
2452170613Sbms
2453170613Sbms	gsa = (sockunion_t *)&msfr.msfr_group;
2454170613Sbms	if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2455170613Sbms		return (EINVAL);
2456170613Sbms
2457170613Sbms	gsa->sin.sin_port = 0;	/* ignore port */
2458170613Sbms
2459181803Sbz	if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
2460170613Sbms		return (EADDRNOTAVAIL);
2461170613Sbms
2462170613Sbms	ifp = ifnet_byindex(msfr.msfr_ifindex);
2463170613Sbms	if (ifp == NULL)
2464170613Sbms		return (EADDRNOTAVAIL);
2465170613Sbms
2466170613Sbms	/*
2467189592Sbms	 * Take the INP write lock.
2468170613Sbms	 * Check if this socket is a member of this group.
2469170613Sbms	 */
2470170613Sbms	imo = inp_findmoptions(inp);
2471170613Sbms	idx = imo_match_group(imo, ifp, &gsa->sa);
2472170613Sbms	if (idx == -1 || imo->imo_mfilters == NULL) {
2473170613Sbms		error = EADDRNOTAVAIL;
2474189592Sbms		goto out_inp_locked;
2475170613Sbms	}
2476189592Sbms	inm = imo->imo_membership[idx];
2477170613Sbms	imf = &imo->imo_mfilters[idx];
2478170613Sbms
2479170613Sbms	/*
2480189592Sbms	 * Begin state merge transaction at socket layer.
2481170613Sbms	 */
2482189592Sbms	INP_WLOCK_ASSERT(inp);
2483170613Sbms
2484189592Sbms	imf->imf_st[1] = msfr.msfr_fmode;
2485189592Sbms
2486170613Sbms	/*
2487170613Sbms	 * Apply any new source filters, if present.
2488189592Sbms	 * Make a copy of the user-space source vector so
2489189592Sbms	 * that we may copy them with a single copyin. This
2490189592Sbms	 * allows us to deal with page faults up-front.
2491170613Sbms	 */
2492170613Sbms	if (msfr.msfr_nsrcs > 0) {
2493189592Sbms		struct in_msource	*lims;
2494189592Sbms		struct sockaddr_in	*psin;
2495189592Sbms		struct sockaddr_storage	*kss, *pkss;
2496189592Sbms		int			 i;
2497170613Sbms
2498178285Srwatson		INP_WUNLOCK(inp);
2499189592Sbms
2500189592Sbms		CTR2(KTR_IGMPV3, "%s: loading %lu source list entries",
2501189592Sbms		    __func__, (unsigned long)msfr.msfr_nsrcs);
2502184214Sdes		kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2503170613Sbms		    M_TEMP, M_WAITOK);
2504170613Sbms		error = copyin(msfr.msfr_srcs, kss,
2505170613Sbms		    sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2506170613Sbms		if (error) {
2507184205Sdes			free(kss, M_TEMP);
2508170613Sbms			return (error);
2509170613Sbms		}
2510170613Sbms
2511189592Sbms		INP_WLOCK(inp);
2512189592Sbms
2513170613Sbms		/*
2514189592Sbms		 * Mark all source filters as UNDEFINED at t1.
2515189592Sbms		 * Restore new group filter mode, as imf_leave()
2516189592Sbms		 * will set it to INCLUDE.
2517170613Sbms		 */
2518189592Sbms		imf_leave(imf);
2519189592Sbms		imf->imf_st[1] = msfr.msfr_fmode;
2520189592Sbms
2521189592Sbms		/*
2522189592Sbms		 * Update socket layer filters at t1, lazy-allocating
2523189592Sbms		 * new entries. This saves a bunch of memory at the
2524189592Sbms		 * cost of one RB_FIND() per source entry; duplicate
2525189592Sbms		 * entries in the msfr_nsrcs vector are ignored.
2526189592Sbms		 * If we encounter an error, rollback transaction.
2527189592Sbms		 *
2528189592Sbms		 * XXX This too could be replaced with a set-symmetric
2529189592Sbms		 * difference like loop to avoid walking from root
2530189592Sbms		 * every time, as the key space is common.
2531189592Sbms		 */
2532189592Sbms		for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2533189592Sbms			psin = (struct sockaddr_in *)pkss;
2534189592Sbms			if (psin->sin_family != AF_INET) {
2535170613Sbms				error = EAFNOSUPPORT;
2536170613Sbms				break;
2537170613Sbms			}
2538189592Sbms			if (psin->sin_len != sizeof(struct sockaddr_in)) {
2539189592Sbms				error = EINVAL;
2540189592Sbms				break;
2541189592Sbms			}
2542189592Sbms			error = imf_get_source(imf, psin, &lims);
2543170613Sbms			if (error)
2544170613Sbms				break;
2545189592Sbms			lims->imsl_st[1] = imf->imf_st[1];
2546170613Sbms		}
2547189592Sbms		free(kss, M_TEMP);
2548189592Sbms	}
2549170613Sbms
2550189592Sbms	if (error)
2551189592Sbms		goto out_imf_rollback;
2552170613Sbms
2553189592Sbms	INP_WLOCK_ASSERT(inp);
2554189592Sbms	IN_MULTI_LOCK();
2555170613Sbms
2556170613Sbms	/*
2557189592Sbms	 * Begin state merge transaction at IGMP layer.
2558170613Sbms	 */
2559189592Sbms	CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2560189592Sbms	error = inm_merge(inm, imf);
2561189592Sbms	if (error) {
2562189592Sbms		CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
2563189592Sbms		goto out_imf_rollback;
2564189592Sbms	}
2565170613Sbms
2566189592Sbms	CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2567189592Sbms	error = igmp_change_state(inm);
2568189592Sbms	if (error)
2569189592Sbms		CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
2570189592Sbms
2571189592Sbms	IN_MULTI_UNLOCK();
2572189592Sbms
2573189592Sbmsout_imf_rollback:
2574189592Sbms	if (error)
2575189592Sbms		imf_rollback(imf);
2576189592Sbms	else
2577189592Sbms		imf_commit(imf);
2578189592Sbms
2579189592Sbms	imf_reap(imf);
2580189592Sbms
2581189592Sbmsout_inp_locked:
2582178285Srwatson	INP_WUNLOCK(inp);
2583170613Sbms	return (error);
2584170613Sbms}
2585170613Sbms
2586170613Sbms/*
2587170613Sbms * Set the IP multicast options in response to user setsockopt().
2588170613Sbms *
2589170613Sbms * Many of the socket options handled in this function duplicate the
2590170613Sbms * functionality of socket options in the regular unicast API. However,
2591170613Sbms * it is not possible to merge the duplicate code, because the idempotence
2592170613Sbms * of the IPv4 multicast part of the BSD Sockets API must be preserved;
2593170613Sbms * the effects of these options must be treated as separate and distinct.
2594189592Sbms *
2595189592Sbms * SMPng: XXX: Unlocked read of inp_socket believed OK.
2596189592Sbms * FUTURE: The IP_MULTICAST_VIF option may be eliminated if MROUTING
2597189592Sbms * is refactored to no longer use vifs.
2598170613Sbms */
2599170613Sbmsint
2600170613Sbmsinp_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2601170613Sbms{
2602170613Sbms	struct ip_moptions	*imo;
2603170613Sbms	int			 error;
2604170613Sbms
2605170613Sbms	error = 0;
2606170613Sbms
2607171746Scsjp	/*
2608171746Scsjp	 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2609171746Scsjp	 * or is a divert socket, reject it.
2610171746Scsjp	 */
2611171746Scsjp	if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
2612171746Scsjp	    (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2613189592Sbms	     inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
2614171746Scsjp		return (EOPNOTSUPP);
2615171746Scsjp
2616170613Sbms	switch (sopt->sopt_name) {
2617170613Sbms	case IP_MULTICAST_VIF: {
2618170613Sbms		int vifi;
2619170613Sbms		/*
2620170613Sbms		 * Select a multicast VIF for transmission.
2621170613Sbms		 * Only useful if multicast forwarding is active.
2622170613Sbms		 */
2623170613Sbms		if (legal_vif_num == NULL) {
2624170613Sbms			error = EOPNOTSUPP;
2625170613Sbms			break;
2626170613Sbms		}
2627170613Sbms		error = sooptcopyin(sopt, &vifi, sizeof(int), sizeof(int));
2628170613Sbms		if (error)
2629170613Sbms			break;
2630170613Sbms		if (!legal_vif_num(vifi) && (vifi != -1)) {
2631170613Sbms			error = EINVAL;
2632170613Sbms			break;
2633170613Sbms		}
2634170613Sbms		imo = inp_findmoptions(inp);
2635170613Sbms		imo->imo_multicast_vif = vifi;
2636178285Srwatson		INP_WUNLOCK(inp);
2637170613Sbms		break;
2638170613Sbms	}
2639170613Sbms
2640170613Sbms	case IP_MULTICAST_IF:
2641170613Sbms		error = inp_set_multicast_if(inp, sopt);
2642170613Sbms		break;
2643170613Sbms
2644170613Sbms	case IP_MULTICAST_TTL: {
2645170613Sbms		u_char ttl;
2646170613Sbms
2647170613Sbms		/*
2648170613Sbms		 * Set the IP time-to-live for outgoing multicast packets.
2649170613Sbms		 * The original multicast API required a char argument,
2650170613Sbms		 * which is inconsistent with the rest of the socket API.
2651170613Sbms		 * We allow either a char or an int.
2652170613Sbms		 */
2653170613Sbms		if (sopt->sopt_valsize == sizeof(u_char)) {
2654170613Sbms			error = sooptcopyin(sopt, &ttl, sizeof(u_char),
2655170613Sbms			    sizeof(u_char));
2656170613Sbms			if (error)
2657170613Sbms				break;
2658170613Sbms		} else {
2659170613Sbms			u_int ittl;
2660170613Sbms
2661170613Sbms			error = sooptcopyin(sopt, &ittl, sizeof(u_int),
2662170613Sbms			    sizeof(u_int));
2663170613Sbms			if (error)
2664170613Sbms				break;
2665170613Sbms			if (ittl > 255) {
2666170613Sbms				error = EINVAL;
2667170613Sbms				break;
2668170613Sbms			}
2669170613Sbms			ttl = (u_char)ittl;
2670170613Sbms		}
2671170613Sbms		imo = inp_findmoptions(inp);
2672170613Sbms		imo->imo_multicast_ttl = ttl;
2673178285Srwatson		INP_WUNLOCK(inp);
2674170613Sbms		break;
2675170613Sbms	}
2676170613Sbms
2677170613Sbms	case IP_MULTICAST_LOOP: {
2678170613Sbms		u_char loop;
2679170613Sbms
2680170613Sbms		/*
2681170613Sbms		 * Set the loopback flag for outgoing multicast packets.
2682170613Sbms		 * Must be zero or one.  The original multicast API required a
2683170613Sbms		 * char argument, which is inconsistent with the rest
2684170613Sbms		 * of the socket API.  We allow either a char or an int.
2685170613Sbms		 */
2686170613Sbms		if (sopt->sopt_valsize == sizeof(u_char)) {
2687170613Sbms			error = sooptcopyin(sopt, &loop, sizeof(u_char),
2688170613Sbms			    sizeof(u_char));
2689170613Sbms			if (error)
2690170613Sbms				break;
2691170613Sbms		} else {
2692170613Sbms			u_int iloop;
2693170613Sbms
2694170613Sbms			error = sooptcopyin(sopt, &iloop, sizeof(u_int),
2695170613Sbms					    sizeof(u_int));
2696170613Sbms			if (error)
2697170613Sbms				break;
2698170613Sbms			loop = (u_char)iloop;
2699170613Sbms		}
2700170613Sbms		imo = inp_findmoptions(inp);
2701170613Sbms		imo->imo_multicast_loop = !!loop;
2702178285Srwatson		INP_WUNLOCK(inp);
2703170613Sbms		break;
2704170613Sbms	}
2705170613Sbms
2706170613Sbms	case IP_ADD_MEMBERSHIP:
2707170613Sbms	case IP_ADD_SOURCE_MEMBERSHIP:
2708170613Sbms	case MCAST_JOIN_GROUP:
2709170613Sbms	case MCAST_JOIN_SOURCE_GROUP:
2710170613Sbms		error = inp_join_group(inp, sopt);
2711170613Sbms		break;
2712170613Sbms
2713170613Sbms	case IP_DROP_MEMBERSHIP:
2714170613Sbms	case IP_DROP_SOURCE_MEMBERSHIP:
2715170613Sbms	case MCAST_LEAVE_GROUP:
2716170613Sbms	case MCAST_LEAVE_SOURCE_GROUP:
2717170613Sbms		error = inp_leave_group(inp, sopt);
2718170613Sbms		break;
2719170613Sbms
2720170613Sbms	case IP_BLOCK_SOURCE:
2721170613Sbms	case IP_UNBLOCK_SOURCE:
2722170613Sbms	case MCAST_BLOCK_SOURCE:
2723170613Sbms	case MCAST_UNBLOCK_SOURCE:
2724189592Sbms		error = inp_block_unblock_source(inp, sopt);
2725170613Sbms		break;
2726170613Sbms
2727170613Sbms	case IP_MSFILTER:
2728170613Sbms		error = inp_set_source_filters(inp, sopt);
2729170613Sbms		break;
2730170613Sbms
2731170613Sbms	default:
2732170613Sbms		error = EOPNOTSUPP;
2733170613Sbms		break;
2734170613Sbms	}
2735170613Sbms
2736170613Sbms	INP_UNLOCK_ASSERT(inp);
2737170613Sbms
2738170613Sbms	return (error);
2739170613Sbms}
2740189592Sbms
2741189592Sbms/*
2742189592Sbms * Expose IGMP's multicast filter mode and source list(s) to userland,
2743189592Sbms * keyed by (ifindex, group).
2744189592Sbms * The filter mode is written out as a uint32_t, followed by
2745189592Sbms * 0..n of struct in_addr.
2746189592Sbms * For use by ifmcstat(8).
2747189592Sbms * SMPng: NOTE: unlocked read of ifindex space.
2748189592Sbms */
2749189592Sbmsstatic int
2750189592Sbmssysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS)
2751189592Sbms{
2752189592Sbms	struct in_addr			 src, group;
2753189592Sbms	struct ifnet			*ifp;
2754189592Sbms	struct ifmultiaddr		*ifma;
2755189592Sbms	struct in_multi			*inm;
2756189592Sbms	struct ip_msource		*ims;
2757189592Sbms	int				*name;
2758189592Sbms	int				 retval;
2759189592Sbms	u_int				 namelen;
2760189592Sbms	uint32_t			 fmode, ifindex;
2761189592Sbms
2762189592Sbms	name = (int *)arg1;
2763189592Sbms	namelen = arg2;
2764189592Sbms
2765189592Sbms	if (req->newptr != NULL)
2766189592Sbms		return (EPERM);
2767189592Sbms
2768189592Sbms	if (namelen != 2)
2769189592Sbms		return (EINVAL);
2770189592Sbms
2771189592Sbms	ifindex = name[0];
2772189592Sbms	if (ifindex <= 0 || ifindex > V_if_index) {
2773189592Sbms		CTR2(KTR_IGMPV3, "%s: ifindex %u out of range",
2774189592Sbms		    __func__, ifindex);
2775189592Sbms		return (ENOENT);
2776189592Sbms	}
2777189592Sbms
2778189592Sbms	group.s_addr = name[1];
2779189592Sbms	if (!IN_MULTICAST(ntohl(group.s_addr))) {
2780189592Sbms		CTR2(KTR_IGMPV3, "%s: group %s is not multicast",
2781189592Sbms		    __func__, inet_ntoa(group));
2782189592Sbms		return (EINVAL);
2783189592Sbms	}
2784189592Sbms
2785189592Sbms	ifp = ifnet_byindex(ifindex);
2786189592Sbms	if (ifp == NULL) {
2787189592Sbms		CTR2(KTR_IGMPV3, "%s: no ifp for ifindex %u",
2788189592Sbms		    __func__, ifindex);
2789189592Sbms		return (ENOENT);
2790189592Sbms	}
2791189592Sbms
2792189592Sbms	retval = sysctl_wire_old_buffer(req,
2793189592Sbms	    sizeof(uint32_t) + (in_mcast_maxgrpsrc * sizeof(struct in_addr)));
2794189592Sbms	if (retval)
2795189592Sbms		return (retval);
2796189592Sbms
2797189592Sbms	IN_MULTI_LOCK();
2798189592Sbms
2799233200Sjhb	IF_ADDR_RLOCK(ifp);
2800189592Sbms	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2801189592Sbms		if (ifma->ifma_addr->sa_family != AF_INET ||
2802189592Sbms		    ifma->ifma_protospec == NULL)
2803189592Sbms			continue;
2804189592Sbms		inm = (struct in_multi *)ifma->ifma_protospec;
2805189592Sbms		if (!in_hosteq(inm->inm_addr, group))
2806189592Sbms			continue;
2807189592Sbms		fmode = inm->inm_st[1].iss_fmode;
2808189592Sbms		retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2809189592Sbms		if (retval != 0)
2810189592Sbms			break;
2811189592Sbms		RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
2812189592Sbms#ifdef KTR
2813189592Sbms			struct in_addr ina;
2814189592Sbms			ina.s_addr = htonl(ims->ims_haddr);
2815189592Sbms			CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2816189592Sbms			    inet_ntoa(ina));
2817189592Sbms#endif
2818189592Sbms			/*
2819189592Sbms			 * Only copy-out sources which are in-mode.
2820189592Sbms			 */
2821189592Sbms			if (fmode != ims_get_mode(inm, ims, 1)) {
2822189592Sbms				CTR1(KTR_IGMPV3, "%s: skip non-in-mode",
2823189592Sbms				    __func__);
2824189592Sbms				continue;
2825189592Sbms			}
2826189592Sbms			src.s_addr = htonl(ims->ims_haddr);
2827189592Sbms			retval = SYSCTL_OUT(req, &src, sizeof(struct in_addr));
2828189592Sbms			if (retval != 0)
2829189592Sbms				break;
2830189592Sbms		}
2831189592Sbms	}
2832233200Sjhb	IF_ADDR_RUNLOCK(ifp);
2833189592Sbms
2834189592Sbms	IN_MULTI_UNLOCK();
2835189592Sbms
2836189592Sbms	return (retval);
2837189592Sbms}
2838189592Sbms
2839189592Sbms#ifdef KTR
2840189592Sbms
2841189592Sbmsstatic const char *inm_modestrs[] = { "un", "in", "ex" };
2842189592Sbms
2843189592Sbmsstatic const char *
2844189592Sbmsinm_mode_str(const int mode)
2845189592Sbms{
2846189592Sbms
2847189592Sbms	if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2848189592Sbms		return (inm_modestrs[mode]);
2849189592Sbms	return ("??");
2850189592Sbms}
2851189592Sbms
2852189592Sbmsstatic const char *inm_statestrs[] = {
2853189592Sbms	"not-member",
2854189592Sbms	"silent",
2855189592Sbms	"idle",
2856189592Sbms	"lazy",
2857189592Sbms	"sleeping",
2858189592Sbms	"awakening",
2859189592Sbms	"query-pending",
2860189592Sbms	"sg-query-pending",
2861189592Sbms	"leaving"
2862189592Sbms};
2863189592Sbms
2864189592Sbmsstatic const char *
2865189592Sbmsinm_state_str(const int state)
2866189592Sbms{
2867189592Sbms
2868189592Sbms	if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
2869189592Sbms		return (inm_statestrs[state]);
2870189592Sbms	return ("??");
2871189592Sbms}
2872189592Sbms
2873189592Sbms/*
2874189592Sbms * Dump an in_multi structure to the console.
2875189592Sbms */
2876189592Sbmsvoid
2877189592Sbmsinm_print(const struct in_multi *inm)
2878189592Sbms{
2879189592Sbms	int t;
2880189592Sbms
2881190753Skan	if ((ktr_mask & KTR_IGMPV3) == 0)
2882189635Sbms		return;
2883189635Sbms
2884189592Sbms	printf("%s: --- begin inm %p ---\n", __func__, inm);
2885189592Sbms	printf("addr %s ifp %p(%s) ifma %p\n",
2886189592Sbms	    inet_ntoa(inm->inm_addr),
2887189592Sbms	    inm->inm_ifp,
2888189592Sbms	    inm->inm_ifp->if_xname,
2889189592Sbms	    inm->inm_ifma);
2890189592Sbms	printf("timer %u state %s refcount %u scq.len %u\n",
2891189592Sbms	    inm->inm_timer,
2892189592Sbms	    inm_state_str(inm->inm_state),
2893189592Sbms	    inm->inm_refcount,
2894189592Sbms	    inm->inm_scq.ifq_len);
2895189592Sbms	printf("igi %p nsrc %lu sctimer %u scrv %u\n",
2896189592Sbms	    inm->inm_igi,
2897189592Sbms	    inm->inm_nsrc,
2898189592Sbms	    inm->inm_sctimer,
2899189592Sbms	    inm->inm_scrv);
2900189592Sbms	for (t = 0; t < 2; t++) {
2901189592Sbms		printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
2902189592Sbms		    inm_mode_str(inm->inm_st[t].iss_fmode),
2903189592Sbms		    inm->inm_st[t].iss_asm,
2904189592Sbms		    inm->inm_st[t].iss_ex,
2905189592Sbms		    inm->inm_st[t].iss_in,
2906189592Sbms		    inm->inm_st[t].iss_rec);
2907189592Sbms	}
2908189592Sbms	printf("%s: --- end inm %p ---\n", __func__, inm);
2909189592Sbms}
2910189592Sbms
2911189592Sbms#else /* !KTR */
2912189592Sbms
2913189592Sbmsvoid
2914189592Sbmsinm_print(const struct in_multi *inm)
2915189592Sbms{
2916189592Sbms
2917189592Sbms}
2918189592Sbms
2919189592Sbms#endif /* KTR */
2920189592Sbms
2921189592SbmsRB_GENERATE(ip_msource_tree, ip_msource, ims_link, ip_msource_cmp);
2922