2 * Copyright (c) 2007-2009 Bruce Simpson.
3 * Copyright (c) 2005 Robert N. M. Watson.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * IPv4 multicast socket, group, and socket option processing module.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/protosw.h>
47 #include <sys/sysctl.h>
52 #include <net/if_dl.h>
53 #include <net/route.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_pcb.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/igmp_var.h>
64 #define KTR_IGMPV3 KTR_INET
67 #ifndef __SOCKUNION_DECLARED
69 struct sockaddr_storage ss;
71 struct sockaddr_dl sdl;
72 struct sockaddr_in sin;
74 typedef union sockunion sockunion_t;
75 #define __SOCKUNION_DECLARED
76 #endif /* __SOCKUNION_DECLARED */
78 static MALLOC_DEFINE(M_INMFILTER, "in_mfilter",
79 "IPv4 multicast PCB-layer source filter");
80 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "IPv4 multicast group");
81 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "IPv4 multicast options");
82 static MALLOC_DEFINE(M_IPMSOURCE, "ip_msource",
83 "IPv4 multicast IGMP-layer source filter");
87 * - Lock order is: Giant, INP_WLOCK, IN_MULTI_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
88 * - The IF_ADDR_LOCK is implicitly taken by inm_lookup() earlier, however
89 * it can be taken by code in net/if.c also.
90 * - ip_moptions and in_mfilter are covered by the INP_WLOCK.
92 * struct in_multi is covered by IN_MULTI_LOCK. There isn't strictly
93 * any need for in_multi itself to be virtualized -- it is bound to an ifp
94 * anyway no matter what happens.
96 struct mtx in_multi_mtx;
97 MTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF);
100 * Functions with non-static linkage defined in this file should be
101 * declared in in_var.h:
106 * in_joingroup_locked()
108 * in_leavegroup_locked()
114 * XXX: Both carp and pf need to use the legacy (*,G) KPIs in_addmulti()
117 static void imf_commit(struct in_mfilter *);
118 static int imf_get_source(struct in_mfilter *imf,
119 const struct sockaddr_in *psin,
120 struct in_msource **);
121 static struct in_msource *
122 imf_graft(struct in_mfilter *, const uint8_t,
123 const struct sockaddr_in *);
124 static void imf_leave(struct in_mfilter *);
125 static int imf_prune(struct in_mfilter *, const struct sockaddr_in *);
126 static void imf_purge(struct in_mfilter *);
127 static void imf_rollback(struct in_mfilter *);
128 static void imf_reap(struct in_mfilter *);
129 static int imo_grow(struct ip_moptions *);
130 static size_t imo_match_group(const struct ip_moptions *,
131 const struct ifnet *, const struct sockaddr *);
132 static struct in_msource *
133 imo_match_source(const struct ip_moptions *, const size_t,
134 const struct sockaddr *);
135 static void ims_merge(struct ip_msource *ims,
136 const struct in_msource *lims, const int rollback);
137 static int in_getmulti(struct ifnet *, const struct in_addr *,
139 static int inm_get_source(struct in_multi *inm, const in_addr_t haddr,
140 const int noalloc, struct ip_msource **pims);
141 static int inm_is_ifp_detached(const struct in_multi *);
142 static int inm_merge(struct in_multi *, /*const*/ struct in_mfilter *);
143 static void inm_purge(struct in_multi *);
144 static void inm_reap(struct in_multi *);
145 static struct ip_moptions *
146 inp_findmoptions(struct inpcb *);
147 static int inp_get_source_filters(struct inpcb *, struct sockopt *);
148 static int inp_join_group(struct inpcb *, struct sockopt *);
149 static int inp_leave_group(struct inpcb *, struct sockopt *);
150 static struct ifnet *
151 inp_lookup_mcast_ifp(const struct inpcb *,
152 const struct sockaddr_in *, const struct in_addr);
153 static int inp_block_unblock_source(struct inpcb *, struct sockopt *);
154 static int inp_set_multicast_if(struct inpcb *, struct sockopt *);
155 static int inp_set_source_filters(struct inpcb *, struct sockopt *);
156 static int sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS);
158 static SYSCTL_NODE(_net_inet_ip, OID_AUTO, mcast, CTLFLAG_RW, 0,
161 static u_long in_mcast_maxgrpsrc = IP_MAX_GROUP_SRC_FILTER;
162 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxgrpsrc,
163 CTLFLAG_RW | CTLFLAG_TUN, &in_mcast_maxgrpsrc, 0,
164 "Max source filters per group");
165 TUNABLE_ULONG("net.inet.ip.mcast.maxgrpsrc", &in_mcast_maxgrpsrc);
167 static u_long in_mcast_maxsocksrc = IP_MAX_SOCK_SRC_FILTER;
168 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxsocksrc,
169 CTLFLAG_RW | CTLFLAG_TUN, &in_mcast_maxsocksrc, 0,
170 "Max source filters per socket");
171 TUNABLE_ULONG("net.inet.ip.mcast.maxsocksrc", &in_mcast_maxsocksrc);
173 int in_mcast_loop = IP_DEFAULT_MULTICAST_LOOP;
174 SYSCTL_INT(_net_inet_ip_mcast, OID_AUTO, loop, CTLFLAG_RW | CTLFLAG_TUN,
175 &in_mcast_loop, 0, "Loopback multicast datagrams by default");
176 TUNABLE_INT("net.inet.ip.mcast.loop", &in_mcast_loop);
178 static SYSCTL_NODE(_net_inet_ip_mcast, OID_AUTO, filters,
179 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip_mcast_filters,
180 "Per-interface stack-wide source filters");
183 * Inline function which wraps assertions for a valid ifp.
184 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
188 inm_is_ifp_detached(const struct in_multi *inm)
192 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
193 ifp = inm->inm_ifma->ifma_ifp;
196 * Sanity check that netinet's notion of ifp is the
199 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
202 return (ifp == NULL);
206 * Initialize an in_mfilter structure to a known state at t0, t1
207 * with an empty source filter list.
210 imf_init(struct in_mfilter *imf, const int st0, const int st1)
212 memset(imf, 0, sizeof(struct in_mfilter));
213 RB_INIT(&imf->imf_sources);
214 imf->imf_st[0] = st0;
215 imf->imf_st[1] = st1;
219 * Resize the ip_moptions vector to the next power-of-two minus 1.
220 * May be called with locks held; do not sleep.
223 imo_grow(struct ip_moptions *imo)
225 struct in_multi **nmships;
226 struct in_multi **omships;
227 struct in_mfilter *nmfilters;
228 struct in_mfilter *omfilters;
235 omships = imo->imo_membership;
236 omfilters = imo->imo_mfilters;
237 oldmax = imo->imo_max_memberships;
238 newmax = ((oldmax + 1) * 2) - 1;
240 if (newmax <= IP_MAX_MEMBERSHIPS) {
241 nmships = (struct in_multi **)realloc(omships,
242 sizeof(struct in_multi *) * newmax, M_IPMOPTS, M_NOWAIT);
243 nmfilters = (struct in_mfilter *)realloc(omfilters,
244 sizeof(struct in_mfilter) * newmax, M_INMFILTER, M_NOWAIT);
245 if (nmships != NULL && nmfilters != NULL) {
246 /* Initialize newly allocated source filter heads. */
247 for (idx = oldmax; idx < newmax; idx++) {
248 imf_init(&nmfilters[idx], MCAST_UNDEFINED,
251 imo->imo_max_memberships = newmax;
252 imo->imo_membership = nmships;
253 imo->imo_mfilters = nmfilters;
257 if (nmships == NULL || nmfilters == NULL) {
259 free(nmships, M_IPMOPTS);
260 if (nmfilters != NULL)
261 free(nmfilters, M_INMFILTER);
262 return (ETOOMANYREFS);
269 * Find an IPv4 multicast group entry for this ip_moptions instance
270 * which matches the specified group, and optionally an interface.
271 * Return its index into the array, or -1 if not found.
274 imo_match_group(const struct ip_moptions *imo, const struct ifnet *ifp,
275 const struct sockaddr *group)
277 const struct sockaddr_in *gsin;
278 struct in_multi **pinm;
282 gsin = (const struct sockaddr_in *)group;
284 /* The imo_membership array may be lazy allocated. */
285 if (imo->imo_membership == NULL || imo->imo_num_memberships == 0)
288 nmships = imo->imo_num_memberships;
289 pinm = &imo->imo_membership[0];
290 for (idx = 0; idx < nmships; idx++, pinm++) {
293 if ((ifp == NULL || ((*pinm)->inm_ifp == ifp)) &&
294 in_hosteq((*pinm)->inm_addr, gsin->sin_addr)) {
305 * Find an IPv4 multicast source entry for this imo which matches
306 * the given group index for this socket, and source address.
308 * NOTE: This does not check if the entry is in-mode, merely if
309 * it exists, which may not be the desired behaviour.
311 static struct in_msource *
312 imo_match_source(const struct ip_moptions *imo, const size_t gidx,
313 const struct sockaddr *src)
315 struct ip_msource find;
316 struct in_mfilter *imf;
317 struct ip_msource *ims;
318 const sockunion_t *psa;
320 KASSERT(src->sa_family == AF_INET, ("%s: !AF_INET", __func__));
321 KASSERT(gidx != -1 && gidx < imo->imo_num_memberships,
322 ("%s: invalid index %d\n", __func__, (int)gidx));
324 /* The imo_mfilters array may be lazy allocated. */
325 if (imo->imo_mfilters == NULL)
327 imf = &imo->imo_mfilters[gidx];
329 /* Source trees are keyed in host byte order. */
330 psa = (const sockunion_t *)src;
331 find.ims_haddr = ntohl(psa->sin.sin_addr.s_addr);
332 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
334 return ((struct in_msource *)ims);
338 * Perform filtering for multicast datagrams on a socket by group and source.
340 * Returns 0 if a datagram should be allowed through, or various error codes
341 * if the socket was not a member of the group, or the source was muted, etc.
344 imo_multi_filter(const struct ip_moptions *imo, const struct ifnet *ifp,
345 const struct sockaddr *group, const struct sockaddr *src)
348 struct in_msource *ims;
351 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
353 gidx = imo_match_group(imo, ifp, group);
355 return (MCAST_NOTGMEMBER);
358 * Check if the source was included in an (S,G) join.
359 * Allow reception on exclusive memberships by default,
360 * reject reception on inclusive memberships by default.
361 * Exclude source only if an in-mode exclude filter exists.
362 * Include source only if an in-mode include filter exists.
363 * NOTE: We are comparing group state here at IGMP t1 (now)
364 * with socket-layer t0 (since last downcall).
366 mode = imo->imo_mfilters[gidx].imf_st[1];
367 ims = imo_match_source(imo, gidx, src);
369 if ((ims == NULL && mode == MCAST_INCLUDE) ||
370 (ims != NULL && ims->imsl_st[0] != mode))
371 return (MCAST_NOTSMEMBER);
377 * Find and return a reference to an in_multi record for (ifp, group),
378 * and bump its reference count.
379 * If one does not exist, try to allocate it, and update link-layer multicast
380 * filters on ifp to listen for group.
381 * Assumes the IN_MULTI lock is held across the call.
382 * Return 0 if successful, otherwise return an appropriate error code.
385 in_getmulti(struct ifnet *ifp, const struct in_addr *group,
386 struct in_multi **pinm)
388 struct sockaddr_in gsin;
389 struct ifmultiaddr *ifma;
390 struct in_ifinfo *ii;
391 struct in_multi *inm;
394 IN_MULTI_LOCK_ASSERT();
396 ii = (struct in_ifinfo *)ifp->if_afdata[AF_INET];
398 inm = inm_lookup(ifp, *group);
401 * If we already joined this group, just bump the
402 * refcount and return it.
404 KASSERT(inm->inm_refcount >= 1,
405 ("%s: bad refcount %d", __func__, inm->inm_refcount));
411 memset(&gsin, 0, sizeof(gsin));
412 gsin.sin_family = AF_INET;
413 gsin.sin_len = sizeof(struct sockaddr_in);
414 gsin.sin_addr = *group;
417 * Check if a link-layer group is already associated
418 * with this network-layer group on the given ifnet.
420 error = if_addmulti(ifp, (struct sockaddr *)&gsin, &ifma);
424 /* XXX ifma_protospec must be covered by IF_ADDR_LOCK */
428 * If something other than netinet is occupying the link-layer
429 * group, print a meaningful error message and back out of
431 * Otherwise, bump the refcount on the existing network-layer
432 * group association and return it.
434 if (ifma->ifma_protospec != NULL) {
435 inm = (struct in_multi *)ifma->ifma_protospec;
437 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
439 KASSERT(ifma->ifma_addr->sa_family == AF_INET,
440 ("%s: ifma not AF_INET", __func__));
441 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
442 if (inm->inm_ifma != ifma || inm->inm_ifp != ifp ||
443 !in_hosteq(inm->inm_addr, *group))
444 panic("%s: ifma %p is inconsistent with %p (%s)",
445 __func__, ifma, inm, inet_ntoa(*group));
449 IF_ADDR_WUNLOCK(ifp);
453 IF_ADDR_WLOCK_ASSERT(ifp);
456 * A new in_multi record is needed; allocate and initialize it.
457 * We DO NOT perform an IGMP join as the in_ layer may need to
458 * push an initial source list down to IGMP to support SSM.
460 * The initial source filter state is INCLUDE, {} as per the RFC.
462 inm = malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT | M_ZERO);
464 if_delmulti_ifma(ifma);
465 IF_ADDR_WUNLOCK(ifp);
468 inm->inm_addr = *group;
470 inm->inm_igi = ii->ii_igmp;
471 inm->inm_ifma = ifma;
472 inm->inm_refcount = 1;
473 inm->inm_state = IGMP_NOT_MEMBER;
476 * Pending state-changes per group are subject to a bounds check.
478 IFQ_SET_MAXLEN(&inm->inm_scq, IGMP_MAX_STATE_CHANGES);
480 inm->inm_st[0].iss_fmode = MCAST_UNDEFINED;
481 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
482 RB_INIT(&inm->inm_srcs);
484 ifma->ifma_protospec = inm;
488 IF_ADDR_WUNLOCK(ifp);
493 * Drop a reference to an in_multi record.
495 * If the refcount drops to 0, free the in_multi record and
496 * delete the underlying link-layer membership.
499 inm_release_locked(struct in_multi *inm)
501 struct ifmultiaddr *ifma;
503 IN_MULTI_LOCK_ASSERT();
505 CTR2(KTR_IGMPV3, "%s: refcount is %d", __func__, inm->inm_refcount);
507 if (--inm->inm_refcount > 0) {
508 CTR2(KTR_IGMPV3, "%s: refcount is now %d", __func__,
513 CTR2(KTR_IGMPV3, "%s: freeing inm %p", __func__, inm);
515 ifma = inm->inm_ifma;
517 /* XXX this access is not covered by IF_ADDR_LOCK */
518 CTR2(KTR_IGMPV3, "%s: purging ifma %p", __func__, ifma);
519 KASSERT(ifma->ifma_protospec == inm,
520 ("%s: ifma_protospec != inm", __func__));
521 ifma->ifma_protospec = NULL;
525 free(inm, M_IPMADDR);
527 if_delmulti_ifma(ifma);
531 * Clear recorded source entries for a group.
532 * Used by the IGMP code. Caller must hold the IN_MULTI lock.
533 * FIXME: Should reap.
536 inm_clear_recorded(struct in_multi *inm)
538 struct ip_msource *ims;
540 IN_MULTI_LOCK_ASSERT();
542 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
545 --inm->inm_st[1].iss_rec;
548 KASSERT(inm->inm_st[1].iss_rec == 0,
549 ("%s: iss_rec %d not 0", __func__, inm->inm_st[1].iss_rec));
553 * Record a source as pending for a Source-Group IGMPv3 query.
554 * This lives here as it modifies the shared tree.
556 * inm is the group descriptor.
557 * naddr is the address of the source to record in network-byte order.
559 * If the net.inet.igmp.sgalloc sysctl is non-zero, we will
560 * lazy-allocate a source node in response to an SG query.
561 * Otherwise, no allocation is performed. This saves some memory
562 * with the trade-off that the source will not be reported to the
563 * router if joined in the window between the query response and
564 * the group actually being joined on the local host.
566 * VIMAGE: XXX: Currently the igmp_sgalloc feature has been removed.
567 * This turns off the allocation of a recorded source entry if
568 * the group has not been joined.
570 * Return 0 if the source didn't exist or was already marked as recorded.
571 * Return 1 if the source was marked as recorded by this function.
572 * Return <0 if any error occured (negated errno code).
575 inm_record_source(struct in_multi *inm, const in_addr_t naddr)
577 struct ip_msource find;
578 struct ip_msource *ims, *nims;
580 IN_MULTI_LOCK_ASSERT();
582 find.ims_haddr = ntohl(naddr);
583 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
584 if (ims && ims->ims_stp)
587 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
589 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
593 nims->ims_haddr = find.ims_haddr;
594 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
600 * Mark the source as recorded and update the recorded
604 ++inm->inm_st[1].iss_rec;
610 * Return a pointer to an in_msource owned by an in_mfilter,
611 * given its source address.
612 * Lazy-allocate if needed. If this is a new entry its filter state is
615 * imf is the filter set being modified.
616 * haddr is the source address in *host* byte-order.
618 * SMPng: May be called with locks held; malloc must not block.
621 imf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin,
622 struct in_msource **plims)
624 struct ip_msource find;
625 struct ip_msource *ims, *nims;
626 struct in_msource *lims;
633 /* key is host byte order */
634 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
635 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
636 lims = (struct in_msource *)ims;
638 if (imf->imf_nsrc == in_mcast_maxsocksrc)
640 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
644 lims = (struct in_msource *)nims;
645 lims->ims_haddr = find.ims_haddr;
646 lims->imsl_st[0] = MCAST_UNDEFINED;
647 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
657 * Graft a source entry into an existing socket-layer filter set,
658 * maintaining any required invariants and checking allocations.
660 * The source is marked as being in the new filter mode at t1.
662 * Return the pointer to the new node, otherwise return NULL.
664 static struct in_msource *
665 imf_graft(struct in_mfilter *imf, const uint8_t st1,
666 const struct sockaddr_in *psin)
668 struct ip_msource *nims;
669 struct in_msource *lims;
671 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
675 lims = (struct in_msource *)nims;
676 lims->ims_haddr = ntohl(psin->sin_addr.s_addr);
677 lims->imsl_st[0] = MCAST_UNDEFINED;
678 lims->imsl_st[1] = st1;
679 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
686 * Prune a source entry from an existing socket-layer filter set,
687 * maintaining any required invariants and checking allocations.
689 * The source is marked as being left at t1, it is not freed.
691 * Return 0 if no error occurred, otherwise return an errno value.
694 imf_prune(struct in_mfilter *imf, const struct sockaddr_in *psin)
696 struct ip_msource find;
697 struct ip_msource *ims;
698 struct in_msource *lims;
700 /* key is host byte order */
701 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
702 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
705 lims = (struct in_msource *)ims;
706 lims->imsl_st[1] = MCAST_UNDEFINED;
711 * Revert socket-layer filter set deltas at t1 to t0 state.
714 imf_rollback(struct in_mfilter *imf)
716 struct ip_msource *ims, *tims;
717 struct in_msource *lims;
719 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
720 lims = (struct in_msource *)ims;
721 if (lims->imsl_st[0] == lims->imsl_st[1]) {
722 /* no change at t1 */
724 } else if (lims->imsl_st[0] != MCAST_UNDEFINED) {
725 /* revert change to existing source at t1 */
726 lims->imsl_st[1] = lims->imsl_st[0];
728 /* revert source added t1 */
729 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
730 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
731 free(ims, M_INMFILTER);
735 imf->imf_st[1] = imf->imf_st[0];
739 * Mark socket-layer filter set as INCLUDE {} at t1.
742 imf_leave(struct in_mfilter *imf)
744 struct ip_msource *ims;
745 struct in_msource *lims;
747 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
748 lims = (struct in_msource *)ims;
749 lims->imsl_st[1] = MCAST_UNDEFINED;
751 imf->imf_st[1] = MCAST_INCLUDE;
755 * Mark socket-layer filter set deltas as committed.
758 imf_commit(struct in_mfilter *imf)
760 struct ip_msource *ims;
761 struct in_msource *lims;
763 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
764 lims = (struct in_msource *)ims;
765 lims->imsl_st[0] = lims->imsl_st[1];
767 imf->imf_st[0] = imf->imf_st[1];
771 * Reap unreferenced sources from socket-layer filter set.
774 imf_reap(struct in_mfilter *imf)
776 struct ip_msource *ims, *tims;
777 struct in_msource *lims;
779 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
780 lims = (struct in_msource *)ims;
781 if ((lims->imsl_st[0] == MCAST_UNDEFINED) &&
782 (lims->imsl_st[1] == MCAST_UNDEFINED)) {
783 CTR2(KTR_IGMPV3, "%s: free lims %p", __func__, ims);
784 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
785 free(ims, M_INMFILTER);
792 * Purge socket-layer filter set.
795 imf_purge(struct in_mfilter *imf)
797 struct ip_msource *ims, *tims;
799 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
800 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
801 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
802 free(ims, M_INMFILTER);
805 imf->imf_st[0] = imf->imf_st[1] = MCAST_UNDEFINED;
806 KASSERT(RB_EMPTY(&imf->imf_sources),
807 ("%s: imf_sources not empty", __func__));
811 * Look up a source filter entry for a multicast group.
813 * inm is the group descriptor to work with.
814 * haddr is the host-byte-order IPv4 address to look up.
815 * noalloc may be non-zero to suppress allocation of sources.
816 * *pims will be set to the address of the retrieved or allocated source.
818 * SMPng: NOTE: may be called with locks held.
819 * Return 0 if successful, otherwise return a non-zero error code.
822 inm_get_source(struct in_multi *inm, const in_addr_t haddr,
823 const int noalloc, struct ip_msource **pims)
825 struct ip_msource find;
826 struct ip_msource *ims, *nims;
831 find.ims_haddr = haddr;
832 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
833 if (ims == NULL && !noalloc) {
834 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
836 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
840 nims->ims_haddr = haddr;
841 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
845 ia.s_addr = htonl(haddr);
846 CTR3(KTR_IGMPV3, "%s: allocated %s as %p", __func__,
856 * Merge socket-layer source into IGMP-layer source.
857 * If rollback is non-zero, perform the inverse of the merge.
860 ims_merge(struct ip_msource *ims, const struct in_msource *lims,
863 int n = rollback ? -1 : 1;
867 ia.s_addr = htonl(ims->ims_haddr);
870 if (lims->imsl_st[0] == MCAST_EXCLUDE) {
871 CTR3(KTR_IGMPV3, "%s: t1 ex -= %d on %s",
872 __func__, n, inet_ntoa(ia));
873 ims->ims_st[1].ex -= n;
874 } else if (lims->imsl_st[0] == MCAST_INCLUDE) {
875 CTR3(KTR_IGMPV3, "%s: t1 in -= %d on %s",
876 __func__, n, inet_ntoa(ia));
877 ims->ims_st[1].in -= n;
880 if (lims->imsl_st[1] == MCAST_EXCLUDE) {
881 CTR3(KTR_IGMPV3, "%s: t1 ex += %d on %s",
882 __func__, n, inet_ntoa(ia));
883 ims->ims_st[1].ex += n;
884 } else if (lims->imsl_st[1] == MCAST_INCLUDE) {
885 CTR3(KTR_IGMPV3, "%s: t1 in += %d on %s",
886 __func__, n, inet_ntoa(ia));
887 ims->ims_st[1].in += n;
892 * Atomically update the global in_multi state, when a membership's
893 * filter list is being updated in any way.
895 * imf is the per-inpcb-membership group filter pointer.
896 * A fake imf may be passed for in-kernel consumers.
898 * XXX This is a candidate for a set-symmetric-difference style loop
899 * which would eliminate the repeated lookup from root of ims nodes,
900 * as they share the same key space.
902 * If any error occurred this function will back out of refcounts
903 * and return a non-zero value.
906 inm_merge(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
908 struct ip_msource *ims, *nims;
909 struct in_msource *lims;
918 * Update the source filters first, as this may fail.
919 * Maintain count of in-mode filters at t0, t1. These are
920 * used to work out if we transition into ASM mode or not.
921 * Maintain a count of source filters whose state was
922 * actually modified by this operation.
924 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
925 lims = (struct in_msource *)ims;
926 if (lims->imsl_st[0] == imf->imf_st[0]) nsrc0++;
927 if (lims->imsl_st[1] == imf->imf_st[1]) nsrc1++;
928 if (lims->imsl_st[0] == lims->imsl_st[1]) continue;
929 error = inm_get_source(inm, lims->ims_haddr, 0, &nims);
933 ims_merge(nims, lims, 0);
936 struct ip_msource *bims;
938 RB_FOREACH_REVERSE_FROM(ims, ip_msource_tree, nims) {
939 lims = (struct in_msource *)ims;
940 if (lims->imsl_st[0] == lims->imsl_st[1])
942 (void)inm_get_source(inm, lims->ims_haddr, 1, &bims);
945 ims_merge(bims, lims, 1);
950 CTR3(KTR_IGMPV3, "%s: imf filters in-mode: %d at t0, %d at t1",
951 __func__, nsrc0, nsrc1);
953 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
954 if (imf->imf_st[0] == imf->imf_st[1] &&
955 imf->imf_st[1] == MCAST_INCLUDE) {
957 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
958 --inm->inm_st[1].iss_in;
962 /* Handle filter mode transition on socket. */
963 if (imf->imf_st[0] != imf->imf_st[1]) {
964 CTR3(KTR_IGMPV3, "%s: imf transition %d to %d",
965 __func__, imf->imf_st[0], imf->imf_st[1]);
967 if (imf->imf_st[0] == MCAST_EXCLUDE) {
968 CTR1(KTR_IGMPV3, "%s: --ex on inm at t1", __func__);
969 --inm->inm_st[1].iss_ex;
970 } else if (imf->imf_st[0] == MCAST_INCLUDE) {
971 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
972 --inm->inm_st[1].iss_in;
975 if (imf->imf_st[1] == MCAST_EXCLUDE) {
976 CTR1(KTR_IGMPV3, "%s: ex++ on inm at t1", __func__);
977 inm->inm_st[1].iss_ex++;
978 } else if (imf->imf_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
979 CTR1(KTR_IGMPV3, "%s: in++ on inm at t1", __func__);
980 inm->inm_st[1].iss_in++;
985 * Track inm filter state in terms of listener counts.
986 * If there are any exclusive listeners, stack-wide
987 * membership is exclusive.
988 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
989 * If no listeners remain, state is undefined at t1,
990 * and the IGMP lifecycle for this group should finish.
992 if (inm->inm_st[1].iss_ex > 0) {
993 CTR1(KTR_IGMPV3, "%s: transition to EX", __func__);
994 inm->inm_st[1].iss_fmode = MCAST_EXCLUDE;
995 } else if (inm->inm_st[1].iss_in > 0) {
996 CTR1(KTR_IGMPV3, "%s: transition to IN", __func__);
997 inm->inm_st[1].iss_fmode = MCAST_INCLUDE;
999 CTR1(KTR_IGMPV3, "%s: transition to UNDEF", __func__);
1000 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
1003 /* Decrement ASM listener count on transition out of ASM mode. */
1004 if (imf->imf_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1005 if ((imf->imf_st[1] != MCAST_EXCLUDE) ||
1006 (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 > 0))
1007 CTR1(KTR_IGMPV3, "%s: --asm on inm at t1", __func__);
1008 --inm->inm_st[1].iss_asm;
1011 /* Increment ASM listener count on transition to ASM mode. */
1012 if (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1013 CTR1(KTR_IGMPV3, "%s: asm++ on inm at t1", __func__);
1014 inm->inm_st[1].iss_asm++;
1017 CTR3(KTR_IGMPV3, "%s: merged imf %p to inm %p", __func__, imf, inm);
1022 CTR1(KTR_IGMPV3, "%s: sources changed; reaping", __func__);
1029 * Mark an in_multi's filter set deltas as committed.
1030 * Called by IGMP after a state change has been enqueued.
1033 inm_commit(struct in_multi *inm)
1035 struct ip_msource *ims;
1037 CTR2(KTR_IGMPV3, "%s: commit inm %p", __func__, inm);
1038 CTR1(KTR_IGMPV3, "%s: pre commit:", __func__);
1041 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
1042 ims->ims_st[0] = ims->ims_st[1];
1044 inm->inm_st[0] = inm->inm_st[1];
1048 * Reap unreferenced nodes from an in_multi's filter set.
1051 inm_reap(struct in_multi *inm)
1053 struct ip_msource *ims, *tims;
1055 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1056 if (ims->ims_st[0].ex > 0 || ims->ims_st[0].in > 0 ||
1057 ims->ims_st[1].ex > 0 || ims->ims_st[1].in > 0 ||
1060 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1061 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1062 free(ims, M_IPMSOURCE);
1068 * Purge all source nodes from an in_multi's filter set.
1071 inm_purge(struct in_multi *inm)
1073 struct ip_msource *ims, *tims;
1075 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1076 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1077 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1078 free(ims, M_IPMSOURCE);
1084 * Join a multicast group; unlocked entry point.
1086 * SMPng: XXX: in_joingroup() is called from in_control() when Giant
1087 * is not held. Fortunately, ifp is unlikely to have been detached
1088 * at this point, so we assume it's OK to recurse.
1091 in_joingroup(struct ifnet *ifp, const struct in_addr *gina,
1092 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1097 error = in_joingroup_locked(ifp, gina, imf, pinm);
1104 * Join a multicast group; real entry point.
1106 * Only preserves atomicity at inm level.
1107 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1109 * If the IGMP downcall fails, the group is not joined, and an error
1113 in_joingroup_locked(struct ifnet *ifp, const struct in_addr *gina,
1114 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1116 struct in_mfilter timf;
1117 struct in_multi *inm;
1120 IN_MULTI_LOCK_ASSERT();
1122 CTR4(KTR_IGMPV3, "%s: join %s on %p(%s))", __func__,
1123 inet_ntoa(*gina), ifp, ifp->if_xname);
1129 * If no imf was specified (i.e. kernel consumer),
1130 * fake one up and assume it is an ASM join.
1133 imf_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1137 error = in_getmulti(ifp, gina, &inm);
1139 CTR1(KTR_IGMPV3, "%s: in_getmulti() failure", __func__);
1143 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1144 error = inm_merge(inm, imf);
1146 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1147 goto out_inm_release;
1150 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1151 error = igmp_change_state(inm);
1153 CTR1(KTR_IGMPV3, "%s: failed to update source", __func__);
1154 goto out_inm_release;
1159 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1160 inm_release_locked(inm);
1169 * Leave a multicast group; unlocked entry point.
1172 in_leavegroup(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1180 error = in_leavegroup_locked(inm, imf);
1187 * Leave a multicast group; real entry point.
1188 * All source filters will be expunged.
1190 * Only preserves atomicity at inm level.
1192 * Holding the write lock for the INP which contains imf
1193 * is highly advisable. We can't assert for it as imf does not
1194 * contain a back-pointer to the owning inp.
1196 * Note: This is not the same as inm_release(*) as this function also
1197 * makes a state change downcall into IGMP.
1200 in_leavegroup_locked(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1202 struct in_mfilter timf;
1207 IN_MULTI_LOCK_ASSERT();
1209 CTR5(KTR_IGMPV3, "%s: leave inm %p, %s/%s, imf %p", __func__,
1210 inm, inet_ntoa(inm->inm_addr),
1211 (inm_is_ifp_detached(inm) ? "null" : inm->inm_ifp->if_xname),
1215 * If no imf was specified (i.e. kernel consumer),
1216 * fake one up and assume it is an ASM join.
1219 imf_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1224 * Begin state merge transaction at IGMP layer.
1226 * As this particular invocation should not cause any memory
1227 * to be allocated, and there is no opportunity to roll back
1228 * the transaction, it MUST NOT fail.
1230 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1231 error = inm_merge(inm, imf);
1232 KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1234 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1235 error = igmp_change_state(inm);
1237 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1239 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1240 inm_release_locked(inm);
1245 /*#ifndef BURN_BRIDGES*/
1247 * Join an IPv4 multicast group in (*,G) exclusive mode.
1248 * The group must be a 224.0.0.0/24 link-scope group.
1249 * This KPI is for legacy kernel consumers only.
1252 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1254 struct in_multi *pinm;
1257 KASSERT(IN_LOCAL_GROUP(ntohl(ap->s_addr)),
1258 ("%s: %s not in 224.0.0.0/24", __func__, inet_ntoa(*ap)));
1260 error = in_joingroup(ifp, ap, NULL, &pinm);
1268 * Leave an IPv4 multicast group, assumed to be in exclusive (*,G) mode.
1269 * This KPI is for legacy kernel consumers only.
1272 in_delmulti(struct in_multi *inm)
1275 (void)in_leavegroup(inm, NULL);
1280 * Block or unblock an ASM multicast source on an inpcb.
1281 * This implements the delta-based API described in RFC 3678.
1283 * The delta-based API applies only to exclusive-mode memberships.
1284 * An IGMP downcall will be performed.
1286 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1288 * Return 0 if successful, otherwise return an appropriate error code.
1291 inp_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1293 struct group_source_req gsr;
1294 sockunion_t *gsa, *ssa;
1296 struct in_mfilter *imf;
1297 struct ip_moptions *imo;
1298 struct in_msource *ims;
1299 struct in_multi *inm;
1308 memset(&gsr, 0, sizeof(struct group_source_req));
1309 gsa = (sockunion_t *)&gsr.gsr_group;
1310 ssa = (sockunion_t *)&gsr.gsr_source;
1312 switch (sopt->sopt_name) {
1313 case IP_BLOCK_SOURCE:
1314 case IP_UNBLOCK_SOURCE: {
1315 struct ip_mreq_source mreqs;
1317 error = sooptcopyin(sopt, &mreqs,
1318 sizeof(struct ip_mreq_source),
1319 sizeof(struct ip_mreq_source));
1323 gsa->sin.sin_family = AF_INET;
1324 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1325 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1327 ssa->sin.sin_family = AF_INET;
1328 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1329 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1331 if (!in_nullhost(mreqs.imr_interface))
1332 INADDR_TO_IFP(mreqs.imr_interface, ifp);
1334 if (sopt->sopt_name == IP_BLOCK_SOURCE)
1337 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
1338 __func__, inet_ntoa(mreqs.imr_interface), ifp);
1342 case MCAST_BLOCK_SOURCE:
1343 case MCAST_UNBLOCK_SOURCE:
1344 error = sooptcopyin(sopt, &gsr,
1345 sizeof(struct group_source_req),
1346 sizeof(struct group_source_req));
1350 if (gsa->sin.sin_family != AF_INET ||
1351 gsa->sin.sin_len != sizeof(struct sockaddr_in))
1354 if (ssa->sin.sin_family != AF_INET ||
1355 ssa->sin.sin_len != sizeof(struct sockaddr_in))
1358 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1359 return (EADDRNOTAVAIL);
1361 ifp = ifnet_byindex(gsr.gsr_interface);
1363 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1368 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
1369 __func__, sopt->sopt_name);
1370 return (EOPNOTSUPP);
1374 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1378 * Check if we are actually a member of this group.
1380 imo = inp_findmoptions(inp);
1381 idx = imo_match_group(imo, ifp, &gsa->sa);
1382 if (idx == -1 || imo->imo_mfilters == NULL) {
1383 error = EADDRNOTAVAIL;
1384 goto out_inp_locked;
1387 KASSERT(imo->imo_mfilters != NULL,
1388 ("%s: imo_mfilters not allocated", __func__));
1389 imf = &imo->imo_mfilters[idx];
1390 inm = imo->imo_membership[idx];
1393 * Attempting to use the delta-based API on an
1394 * non exclusive-mode membership is an error.
1396 fmode = imf->imf_st[0];
1397 if (fmode != MCAST_EXCLUDE) {
1399 goto out_inp_locked;
1403 * Deal with error cases up-front:
1404 * Asked to block, but already blocked; or
1405 * Asked to unblock, but nothing to unblock.
1406 * If adding a new block entry, allocate it.
1408 ims = imo_match_source(imo, idx, &ssa->sa);
1409 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1410 CTR3(KTR_IGMPV3, "%s: source %s %spresent", __func__,
1411 inet_ntoa(ssa->sin.sin_addr), doblock ? "" : "not ");
1412 error = EADDRNOTAVAIL;
1413 goto out_inp_locked;
1416 INP_WLOCK_ASSERT(inp);
1419 * Begin state merge transaction at socket layer.
1422 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
1423 ims = imf_graft(imf, fmode, &ssa->sin);
1427 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
1428 error = imf_prune(imf, &ssa->sin);
1432 CTR1(KTR_IGMPV3, "%s: merge imf state failed", __func__);
1433 goto out_imf_rollback;
1437 * Begin state merge transaction at IGMP layer.
1441 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1442 error = inm_merge(inm, imf);
1444 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1445 goto out_imf_rollback;
1448 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1449 error = igmp_change_state(inm);
1451 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1469 * Given an inpcb, return its multicast options structure pointer. Accepts
1470 * an unlocked inpcb pointer, but will return it locked. May sleep.
1472 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1473 * SMPng: NOTE: Returns with the INP write lock held.
1475 static struct ip_moptions *
1476 inp_findmoptions(struct inpcb *inp)
1478 struct ip_moptions *imo;
1479 struct in_multi **immp;
1480 struct in_mfilter *imfp;
1484 if (inp->inp_moptions != NULL)
1485 return (inp->inp_moptions);
1489 imo = malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK);
1490 immp = malloc(sizeof(*immp) * IP_MIN_MEMBERSHIPS, M_IPMOPTS,
1492 imfp = malloc(sizeof(struct in_mfilter) * IP_MIN_MEMBERSHIPS,
1493 M_INMFILTER, M_WAITOK);
1495 imo->imo_multicast_ifp = NULL;
1496 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1497 imo->imo_multicast_vif = -1;
1498 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1499 imo->imo_multicast_loop = in_mcast_loop;
1500 imo->imo_num_memberships = 0;
1501 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1502 imo->imo_membership = immp;
1504 /* Initialize per-group source filters. */
1505 for (idx = 0; idx < IP_MIN_MEMBERSHIPS; idx++)
1506 imf_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE);
1507 imo->imo_mfilters = imfp;
1510 if (inp->inp_moptions != NULL) {
1511 free(imfp, M_INMFILTER);
1512 free(immp, M_IPMOPTS);
1513 free(imo, M_IPMOPTS);
1514 return (inp->inp_moptions);
1516 inp->inp_moptions = imo;
1521 * Discard the IP multicast options (and source filters).
1523 * SMPng: NOTE: assumes INP write lock is held.
1526 inp_freemoptions(struct ip_moptions *imo)
1528 struct in_mfilter *imf;
1529 size_t idx, nmships;
1531 KASSERT(imo != NULL, ("%s: ip_moptions is NULL", __func__));
1533 nmships = imo->imo_num_memberships;
1534 for (idx = 0; idx < nmships; ++idx) {
1535 imf = imo->imo_mfilters ? &imo->imo_mfilters[idx] : NULL;
1538 (void)in_leavegroup(imo->imo_membership[idx], imf);
1543 if (imo->imo_mfilters)
1544 free(imo->imo_mfilters, M_INMFILTER);
1545 free(imo->imo_membership, M_IPMOPTS);
1546 free(imo, M_IPMOPTS);
1550 * Atomically get source filters on a socket for an IPv4 multicast group.
1551 * Called with INP lock held; returns with lock released.
1554 inp_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1556 struct __msfilterreq msfr;
1559 struct ip_moptions *imo;
1560 struct in_mfilter *imf;
1561 struct ip_msource *ims;
1562 struct in_msource *lims;
1563 struct sockaddr_in *psin;
1564 struct sockaddr_storage *ptss;
1565 struct sockaddr_storage *tss;
1567 size_t idx, nsrcs, ncsrcs;
1569 INP_WLOCK_ASSERT(inp);
1571 imo = inp->inp_moptions;
1572 KASSERT(imo != NULL, ("%s: null ip_moptions", __func__));
1576 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1577 sizeof(struct __msfilterreq));
1581 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
1584 ifp = ifnet_byindex(msfr.msfr_ifindex);
1591 * Lookup group on the socket.
1593 gsa = (sockunion_t *)&msfr.msfr_group;
1594 idx = imo_match_group(imo, ifp, &gsa->sa);
1595 if (idx == -1 || imo->imo_mfilters == NULL) {
1597 return (EADDRNOTAVAIL);
1599 imf = &imo->imo_mfilters[idx];
1602 * Ignore memberships which are in limbo.
1604 if (imf->imf_st[1] == MCAST_UNDEFINED) {
1608 msfr.msfr_fmode = imf->imf_st[1];
1611 * If the user specified a buffer, copy out the source filter
1612 * entries to userland gracefully.
1613 * We only copy out the number of entries which userland
1614 * has asked for, but we always tell userland how big the
1615 * buffer really needs to be.
1617 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
1618 msfr.msfr_nsrcs = in_mcast_maxsocksrc;
1620 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1621 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1622 M_TEMP, M_NOWAIT | M_ZERO);
1630 * Count number of sources in-mode at t0.
1631 * If buffer space exists and remains, copy out source entries.
1633 nsrcs = msfr.msfr_nsrcs;
1636 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
1637 lims = (struct in_msource *)ims;
1638 if (lims->imsl_st[0] == MCAST_UNDEFINED ||
1639 lims->imsl_st[0] != imf->imf_st[0])
1642 if (tss != NULL && nsrcs > 0) {
1643 psin = (struct sockaddr_in *)ptss;
1644 psin->sin_family = AF_INET;
1645 psin->sin_len = sizeof(struct sockaddr_in);
1646 psin->sin_addr.s_addr = htonl(lims->ims_haddr);
1656 error = copyout(tss, msfr.msfr_srcs,
1657 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1663 msfr.msfr_nsrcs = ncsrcs;
1664 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1670 * Return the IP multicast options in response to user getsockopt().
1673 inp_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1675 struct ip_mreqn mreqn;
1676 struct ip_moptions *imo;
1678 struct in_ifaddr *ia;
1683 imo = inp->inp_moptions;
1685 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1686 * or is a divert socket, reject it.
1688 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1689 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1690 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
1692 return (EOPNOTSUPP);
1696 switch (sopt->sopt_name) {
1697 case IP_MULTICAST_VIF:
1699 optval = imo->imo_multicast_vif;
1703 error = sooptcopyout(sopt, &optval, sizeof(int));
1706 case IP_MULTICAST_IF:
1707 memset(&mreqn, 0, sizeof(struct ip_mreqn));
1709 ifp = imo->imo_multicast_ifp;
1710 if (!in_nullhost(imo->imo_multicast_addr)) {
1711 mreqn.imr_address = imo->imo_multicast_addr;
1712 } else if (ifp != NULL) {
1713 mreqn.imr_ifindex = ifp->if_index;
1717 IA_SIN(ia)->sin_addr;
1718 ifa_free(&ia->ia_ifa);
1723 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
1724 error = sooptcopyout(sopt, &mreqn,
1725 sizeof(struct ip_mreqn));
1727 error = sooptcopyout(sopt, &mreqn.imr_address,
1728 sizeof(struct in_addr));
1732 case IP_MULTICAST_TTL:
1734 optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1736 optval = coptval = imo->imo_multicast_ttl;
1738 if (sopt->sopt_valsize == sizeof(u_char))
1739 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1741 error = sooptcopyout(sopt, &optval, sizeof(int));
1744 case IP_MULTICAST_LOOP:
1746 optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1748 optval = coptval = imo->imo_multicast_loop;
1750 if (sopt->sopt_valsize == sizeof(u_char))
1751 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1753 error = sooptcopyout(sopt, &optval, sizeof(int));
1758 error = EADDRNOTAVAIL;
1761 error = inp_get_source_filters(inp, sopt);
1767 error = ENOPROTOOPT;
1771 INP_UNLOCK_ASSERT(inp);
1777 * Look up the ifnet to use for a multicast group membership,
1778 * given the IPv4 address of an interface, and the IPv4 group address.
1780 * This routine exists to support legacy multicast applications
1781 * which do not understand that multicast memberships are scoped to
1782 * specific physical links in the networking stack, or which need
1783 * to join link-scope groups before IPv4 addresses are configured.
1785 * If inp is non-NULL, use this socket's current FIB number for any
1786 * required FIB lookup.
1787 * If ina is INADDR_ANY, look up the group address in the unicast FIB,
1788 * and use its ifp; usually, this points to the default next-hop.
1790 * If the FIB lookup fails, attempt to use the first non-loopback
1791 * interface with multicast capability in the system as a
1792 * last resort. The legacy IPv4 ASM API requires that we do
1793 * this in order to allow groups to be joined when the routing
1794 * table has not yet been populated during boot.
1796 * Returns NULL if no ifp could be found.
1798 * SMPng: TODO: Acquire the appropriate locks for INADDR_TO_IFP.
1799 * FUTURE: Implement IPv4 source-address selection.
1801 static struct ifnet *
1802 inp_lookup_mcast_ifp(const struct inpcb *inp,
1803 const struct sockaddr_in *gsin, const struct in_addr ina)
1807 KASSERT(gsin->sin_family == AF_INET, ("%s: not AF_INET", __func__));
1808 KASSERT(IN_MULTICAST(ntohl(gsin->sin_addr.s_addr)),
1809 ("%s: not multicast", __func__));
1812 if (!in_nullhost(ina)) {
1813 INADDR_TO_IFP(ina, ifp);
1818 memcpy(&ro.ro_dst, gsin, sizeof(struct sockaddr_in));
1819 in_rtalloc_ign(&ro, 0, inp ? inp->inp_inc.inc_fibnum : 0);
1820 if (ro.ro_rt != NULL) {
1821 ifp = ro.ro_rt->rt_ifp;
1822 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
1825 struct in_ifaddr *ia;
1830 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1832 if (!(mifp->if_flags & IFF_LOOPBACK) &&
1833 (mifp->if_flags & IFF_MULTICAST)) {
1838 IN_IFADDR_RUNLOCK();
1846 * Join an IPv4 multicast group, possibly with a source.
1849 inp_join_group(struct inpcb *inp, struct sockopt *sopt)
1851 struct group_source_req gsr;
1852 sockunion_t *gsa, *ssa;
1854 struct in_mfilter *imf;
1855 struct ip_moptions *imo;
1856 struct in_multi *inm;
1857 struct in_msource *lims;
1867 memset(&gsr, 0, sizeof(struct group_source_req));
1868 gsa = (sockunion_t *)&gsr.gsr_group;
1869 gsa->ss.ss_family = AF_UNSPEC;
1870 ssa = (sockunion_t *)&gsr.gsr_source;
1871 ssa->ss.ss_family = AF_UNSPEC;
1873 switch (sopt->sopt_name) {
1874 case IP_ADD_MEMBERSHIP:
1875 case IP_ADD_SOURCE_MEMBERSHIP: {
1876 struct ip_mreq_source mreqs;
1878 if (sopt->sopt_name == IP_ADD_MEMBERSHIP) {
1879 error = sooptcopyin(sopt, &mreqs,
1880 sizeof(struct ip_mreq),
1881 sizeof(struct ip_mreq));
1883 * Do argument switcharoo from ip_mreq into
1884 * ip_mreq_source to avoid using two instances.
1886 mreqs.imr_interface = mreqs.imr_sourceaddr;
1887 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
1888 } else if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
1889 error = sooptcopyin(sopt, &mreqs,
1890 sizeof(struct ip_mreq_source),
1891 sizeof(struct ip_mreq_source));
1896 gsa->sin.sin_family = AF_INET;
1897 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1898 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1900 if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
1901 ssa->sin.sin_family = AF_INET;
1902 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1903 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1906 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1909 ifp = inp_lookup_mcast_ifp(inp, &gsa->sin,
1910 mreqs.imr_interface);
1911 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
1912 __func__, inet_ntoa(mreqs.imr_interface), ifp);
1916 case MCAST_JOIN_GROUP:
1917 case MCAST_JOIN_SOURCE_GROUP:
1918 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1919 error = sooptcopyin(sopt, &gsr,
1920 sizeof(struct group_req),
1921 sizeof(struct group_req));
1922 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1923 error = sooptcopyin(sopt, &gsr,
1924 sizeof(struct group_source_req),
1925 sizeof(struct group_source_req));
1930 if (gsa->sin.sin_family != AF_INET ||
1931 gsa->sin.sin_len != sizeof(struct sockaddr_in))
1935 * Overwrite the port field if present, as the sockaddr
1936 * being copied in may be matched with a binary comparison.
1938 gsa->sin.sin_port = 0;
1939 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1940 if (ssa->sin.sin_family != AF_INET ||
1941 ssa->sin.sin_len != sizeof(struct sockaddr_in))
1943 ssa->sin.sin_port = 0;
1946 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1949 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1950 return (EADDRNOTAVAIL);
1951 ifp = ifnet_byindex(gsr.gsr_interface);
1955 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
1956 __func__, sopt->sopt_name);
1957 return (EOPNOTSUPP);
1961 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
1962 return (EADDRNOTAVAIL);
1964 imo = inp_findmoptions(inp);
1965 idx = imo_match_group(imo, ifp, &gsa->sa);
1969 inm = imo->imo_membership[idx];
1970 imf = &imo->imo_mfilters[idx];
1971 if (ssa->ss.ss_family != AF_UNSPEC) {
1973 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
1974 * is an error. On an existing inclusive membership,
1975 * it just adds the source to the filter list.
1977 if (imf->imf_st[1] != MCAST_INCLUDE) {
1979 goto out_inp_locked;
1982 * Throw out duplicates.
1984 * XXX FIXME: This makes a naive assumption that
1985 * even if entries exist for *ssa in this imf,
1986 * they will be rejected as dupes, even if they
1987 * are not valid in the current mode (in-mode).
1989 * in_msource is transactioned just as for anything
1990 * else in SSM -- but note naive use of inm_graft()
1991 * below for allocating new filter entries.
1993 * This is only an issue if someone mixes the
1994 * full-state SSM API with the delta-based API,
1995 * which is discouraged in the relevant RFCs.
1997 lims = imo_match_source(imo, idx, &ssa->sa);
1998 if (lims != NULL /*&&
1999 lims->imsl_st[1] == MCAST_INCLUDE*/) {
2000 error = EADDRNOTAVAIL;
2001 goto out_inp_locked;
2005 * MCAST_JOIN_GROUP on an existing exclusive
2006 * membership is an error; return EADDRINUSE
2007 * to preserve 4.4BSD API idempotence, and
2008 * avoid tedious detour to code below.
2009 * NOTE: This is bending RFC 3678 a bit.
2011 * On an existing inclusive membership, this is also
2012 * an error; if you want to change filter mode,
2013 * you must use the userland API setsourcefilter().
2014 * XXX We don't reject this for imf in UNDEFINED
2015 * state at t1, because allocation of a filter
2016 * is atomic with allocation of a membership.
2019 if (imf->imf_st[1] == MCAST_EXCLUDE)
2021 goto out_inp_locked;
2026 * Begin state merge transaction at socket layer.
2028 INP_WLOCK_ASSERT(inp);
2031 if (imo->imo_num_memberships == imo->imo_max_memberships) {
2032 error = imo_grow(imo);
2034 goto out_inp_locked;
2037 * Allocate the new slot upfront so we can deal with
2038 * grafting the new source filter in same code path
2039 * as for join-source on existing membership.
2041 idx = imo->imo_num_memberships;
2042 imo->imo_membership[idx] = NULL;
2043 imo->imo_num_memberships++;
2044 KASSERT(imo->imo_mfilters != NULL,
2045 ("%s: imf_mfilters vector was not allocated", __func__));
2046 imf = &imo->imo_mfilters[idx];
2047 KASSERT(RB_EMPTY(&imf->imf_sources),
2048 ("%s: imf_sources not empty", __func__));
2052 * Graft new source into filter list for this inpcb's
2053 * membership of the group. The in_multi may not have
2054 * been allocated yet if this is a new membership, however,
2055 * the in_mfilter slot will be allocated and must be initialized.
2057 * Note: Grafting of exclusive mode filters doesn't happen
2059 * XXX: Should check for non-NULL lims (node exists but may
2060 * not be in-mode) for interop with full-state API.
2062 if (ssa->ss.ss_family != AF_UNSPEC) {
2063 /* Membership starts in IN mode */
2065 CTR1(KTR_IGMPV3, "%s: new join w/source", __func__);
2066 imf_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE);
2068 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
2070 lims = imf_graft(imf, MCAST_INCLUDE, &ssa->sin);
2072 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2078 /* No address specified; Membership starts in EX mode */
2080 CTR1(KTR_IGMPV3, "%s: new join w/o source", __func__);
2081 imf_init(imf, MCAST_UNDEFINED, MCAST_EXCLUDE);
2086 * Begin state merge transaction at IGMP layer.
2091 error = in_joingroup_locked(ifp, &gsa->sin.sin_addr, imf,
2095 imo->imo_membership[idx] = inm;
2097 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2098 error = inm_merge(inm, imf);
2100 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2102 goto out_imf_rollback;
2104 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2105 error = igmp_change_state(inm);
2107 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2109 goto out_imf_rollback;
2116 INP_WLOCK_ASSERT(inp);
2128 if (error && is_new) {
2129 imo->imo_membership[idx] = NULL;
2130 --imo->imo_num_memberships;
2139 * Leave an IPv4 multicast group on an inpcb, possibly with a source.
2142 inp_leave_group(struct inpcb *inp, struct sockopt *sopt)
2144 struct group_source_req gsr;
2145 struct ip_mreq_source mreqs;
2146 sockunion_t *gsa, *ssa;
2148 struct in_mfilter *imf;
2149 struct ip_moptions *imo;
2150 struct in_msource *ims;
2151 struct in_multi *inm;
2153 int error, is_final;
2159 memset(&gsr, 0, sizeof(struct group_source_req));
2160 gsa = (sockunion_t *)&gsr.gsr_group;
2161 gsa->ss.ss_family = AF_UNSPEC;
2162 ssa = (sockunion_t *)&gsr.gsr_source;
2163 ssa->ss.ss_family = AF_UNSPEC;
2165 switch (sopt->sopt_name) {
2166 case IP_DROP_MEMBERSHIP:
2167 case IP_DROP_SOURCE_MEMBERSHIP:
2168 if (sopt->sopt_name == IP_DROP_MEMBERSHIP) {
2169 error = sooptcopyin(sopt, &mreqs,
2170 sizeof(struct ip_mreq),
2171 sizeof(struct ip_mreq));
2173 * Swap interface and sourceaddr arguments,
2174 * as ip_mreq and ip_mreq_source are laid
2177 mreqs.imr_interface = mreqs.imr_sourceaddr;
2178 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
2179 } else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2180 error = sooptcopyin(sopt, &mreqs,
2181 sizeof(struct ip_mreq_source),
2182 sizeof(struct ip_mreq_source));
2187 gsa->sin.sin_family = AF_INET;
2188 gsa->sin.sin_len = sizeof(struct sockaddr_in);
2189 gsa->sin.sin_addr = mreqs.imr_multiaddr;
2191 if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2192 ssa->sin.sin_family = AF_INET;
2193 ssa->sin.sin_len = sizeof(struct sockaddr_in);
2194 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
2198 * Attempt to look up hinted ifp from interface address.
2199 * Fallthrough with null ifp iff lookup fails, to
2200 * preserve 4.4BSD mcast API idempotence.
2201 * XXX NOTE WELL: The RFC 3678 API is preferred because
2202 * using an IPv4 address as a key is racy.
2204 if (!in_nullhost(mreqs.imr_interface))
2205 INADDR_TO_IFP(mreqs.imr_interface, ifp);
2207 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
2208 __func__, inet_ntoa(mreqs.imr_interface), ifp);
2212 case MCAST_LEAVE_GROUP:
2213 case MCAST_LEAVE_SOURCE_GROUP:
2214 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2215 error = sooptcopyin(sopt, &gsr,
2216 sizeof(struct group_req),
2217 sizeof(struct group_req));
2218 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2219 error = sooptcopyin(sopt, &gsr,
2220 sizeof(struct group_source_req),
2221 sizeof(struct group_source_req));
2226 if (gsa->sin.sin_family != AF_INET ||
2227 gsa->sin.sin_len != sizeof(struct sockaddr_in))
2230 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2231 if (ssa->sin.sin_family != AF_INET ||
2232 ssa->sin.sin_len != sizeof(struct sockaddr_in))
2236 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
2237 return (EADDRNOTAVAIL);
2239 ifp = ifnet_byindex(gsr.gsr_interface);
2242 return (EADDRNOTAVAIL);
2246 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2247 __func__, sopt->sopt_name);
2248 return (EOPNOTSUPP);
2252 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2256 * Find the membership in the membership array.
2258 imo = inp_findmoptions(inp);
2259 idx = imo_match_group(imo, ifp, &gsa->sa);
2261 error = EADDRNOTAVAIL;
2262 goto out_inp_locked;
2264 inm = imo->imo_membership[idx];
2265 imf = &imo->imo_mfilters[idx];
2267 if (ssa->ss.ss_family != AF_UNSPEC)
2271 * Begin state merge transaction at socket layer.
2273 INP_WLOCK_ASSERT(inp);
2276 * If we were instructed only to leave a given source, do so.
2277 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2282 if (imf->imf_st[0] == MCAST_EXCLUDE) {
2283 error = EADDRNOTAVAIL;
2284 goto out_inp_locked;
2286 ims = imo_match_source(imo, idx, &ssa->sa);
2288 CTR3(KTR_IGMPV3, "%s: source %s %spresent", __func__,
2289 inet_ntoa(ssa->sin.sin_addr), "not ");
2290 error = EADDRNOTAVAIL;
2291 goto out_inp_locked;
2293 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
2294 error = imf_prune(imf, &ssa->sin);
2296 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2298 goto out_inp_locked;
2303 * Begin state merge transaction at IGMP layer.
2309 * Give up the multicast address record to which
2310 * the membership points.
2312 (void)in_leavegroup_locked(inm, imf);
2314 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2315 error = inm_merge(inm, imf);
2317 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2319 goto out_imf_rollback;
2322 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2323 error = igmp_change_state(inm);
2325 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2341 /* Remove the gap in the membership and filter array. */
2342 for (++idx; idx < imo->imo_num_memberships; ++idx) {
2343 imo->imo_membership[idx-1] = imo->imo_membership[idx];
2344 imo->imo_mfilters[idx-1] = imo->imo_mfilters[idx];
2346 imo->imo_num_memberships--;
2355 * Select the interface for transmitting IPv4 multicast datagrams.
2357 * Either an instance of struct in_addr or an instance of struct ip_mreqn
2358 * may be passed to this socket option. An address of INADDR_ANY or an
2359 * interface index of 0 is used to remove a previous selection.
2360 * When no interface is selected, one is chosen for every send.
2363 inp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2365 struct in_addr addr;
2366 struct ip_mreqn mreqn;
2368 struct ip_moptions *imo;
2371 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
2373 * An interface index was specified using the
2374 * Linux-derived ip_mreqn structure.
2376 error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn),
2377 sizeof(struct ip_mreqn));
2381 if (mreqn.imr_ifindex < 0 || V_if_index < mreqn.imr_ifindex)
2384 if (mreqn.imr_ifindex == 0) {
2387 ifp = ifnet_byindex(mreqn.imr_ifindex);
2389 return (EADDRNOTAVAIL);
2393 * An interface was specified by IPv4 address.
2394 * This is the traditional BSD usage.
2396 error = sooptcopyin(sopt, &addr, sizeof(struct in_addr),
2397 sizeof(struct in_addr));
2400 if (in_nullhost(addr)) {
2403 INADDR_TO_IFP(addr, ifp);
2405 return (EADDRNOTAVAIL);
2407 CTR3(KTR_IGMPV3, "%s: ifp = %p, addr = %s", __func__, ifp,
2411 /* Reject interfaces which do not support multicast. */
2412 if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0)
2413 return (EOPNOTSUPP);
2415 imo = inp_findmoptions(inp);
2416 imo->imo_multicast_ifp = ifp;
2417 imo->imo_multicast_addr.s_addr = INADDR_ANY;
2424 * Atomically set source filters on a socket for an IPv4 multicast group.
2426 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
2429 inp_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2431 struct __msfilterreq msfr;
2434 struct in_mfilter *imf;
2435 struct ip_moptions *imo;
2436 struct in_multi *inm;
2440 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2441 sizeof(struct __msfilterreq));
2445 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
2448 if ((msfr.msfr_fmode != MCAST_EXCLUDE &&
2449 msfr.msfr_fmode != MCAST_INCLUDE))
2452 if (msfr.msfr_group.ss_family != AF_INET ||
2453 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in))
2456 gsa = (sockunion_t *)&msfr.msfr_group;
2457 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2460 gsa->sin.sin_port = 0; /* ignore port */
2462 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
2463 return (EADDRNOTAVAIL);
2465 ifp = ifnet_byindex(msfr.msfr_ifindex);
2467 return (EADDRNOTAVAIL);
2470 * Take the INP write lock.
2471 * Check if this socket is a member of this group.
2473 imo = inp_findmoptions(inp);
2474 idx = imo_match_group(imo, ifp, &gsa->sa);
2475 if (idx == -1 || imo->imo_mfilters == NULL) {
2476 error = EADDRNOTAVAIL;
2477 goto out_inp_locked;
2479 inm = imo->imo_membership[idx];
2480 imf = &imo->imo_mfilters[idx];
2483 * Begin state merge transaction at socket layer.
2485 INP_WLOCK_ASSERT(inp);
2487 imf->imf_st[1] = msfr.msfr_fmode;
2490 * Apply any new source filters, if present.
2491 * Make a copy of the user-space source vector so
2492 * that we may copy them with a single copyin. This
2493 * allows us to deal with page faults up-front.
2495 if (msfr.msfr_nsrcs > 0) {
2496 struct in_msource *lims;
2497 struct sockaddr_in *psin;
2498 struct sockaddr_storage *kss, *pkss;
2503 CTR2(KTR_IGMPV3, "%s: loading %lu source list entries",
2504 __func__, (unsigned long)msfr.msfr_nsrcs);
2505 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2507 error = copyin(msfr.msfr_srcs, kss,
2508 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2517 * Mark all source filters as UNDEFINED at t1.
2518 * Restore new group filter mode, as imf_leave()
2519 * will set it to INCLUDE.
2522 imf->imf_st[1] = msfr.msfr_fmode;
2525 * Update socket layer filters at t1, lazy-allocating
2526 * new entries. This saves a bunch of memory at the
2527 * cost of one RB_FIND() per source entry; duplicate
2528 * entries in the msfr_nsrcs vector are ignored.
2529 * If we encounter an error, rollback transaction.
2531 * XXX This too could be replaced with a set-symmetric
2532 * difference like loop to avoid walking from root
2533 * every time, as the key space is common.
2535 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2536 psin = (struct sockaddr_in *)pkss;
2537 if (psin->sin_family != AF_INET) {
2538 error = EAFNOSUPPORT;
2541 if (psin->sin_len != sizeof(struct sockaddr_in)) {
2545 error = imf_get_source(imf, psin, &lims);
2548 lims->imsl_st[1] = imf->imf_st[1];
2554 goto out_imf_rollback;
2556 INP_WLOCK_ASSERT(inp);
2560 * Begin state merge transaction at IGMP layer.
2562 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2563 error = inm_merge(inm, imf);
2565 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
2566 goto out_imf_rollback;
2569 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2570 error = igmp_change_state(inm);
2572 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
2590 * Set the IP multicast options in response to user setsockopt().
2592 * Many of the socket options handled in this function duplicate the
2593 * functionality of socket options in the regular unicast API. However,
2594 * it is not possible to merge the duplicate code, because the idempotence
2595 * of the IPv4 multicast part of the BSD Sockets API must be preserved;
2596 * the effects of these options must be treated as separate and distinct.
2598 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2599 * FUTURE: The IP_MULTICAST_VIF option may be eliminated if MROUTING
2600 * is refactored to no longer use vifs.
2603 inp_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2605 struct ip_moptions *imo;
2611 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2612 * or is a divert socket, reject it.
2614 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
2615 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2616 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
2617 return (EOPNOTSUPP);
2619 switch (sopt->sopt_name) {
2620 case IP_MULTICAST_VIF: {
2623 * Select a multicast VIF for transmission.
2624 * Only useful if multicast forwarding is active.
2626 if (legal_vif_num == NULL) {
2630 error = sooptcopyin(sopt, &vifi, sizeof(int), sizeof(int));
2633 if (!legal_vif_num(vifi) && (vifi != -1)) {
2637 imo = inp_findmoptions(inp);
2638 imo->imo_multicast_vif = vifi;
2643 case IP_MULTICAST_IF:
2644 error = inp_set_multicast_if(inp, sopt);
2647 case IP_MULTICAST_TTL: {
2651 * Set the IP time-to-live for outgoing multicast packets.
2652 * The original multicast API required a char argument,
2653 * which is inconsistent with the rest of the socket API.
2654 * We allow either a char or an int.
2656 if (sopt->sopt_valsize == sizeof(u_char)) {
2657 error = sooptcopyin(sopt, &ttl, sizeof(u_char),
2664 error = sooptcopyin(sopt, &ittl, sizeof(u_int),
2674 imo = inp_findmoptions(inp);
2675 imo->imo_multicast_ttl = ttl;
2680 case IP_MULTICAST_LOOP: {
2684 * Set the loopback flag for outgoing multicast packets.
2685 * Must be zero or one. The original multicast API required a
2686 * char argument, which is inconsistent with the rest
2687 * of the socket API. We allow either a char or an int.
2689 if (sopt->sopt_valsize == sizeof(u_char)) {
2690 error = sooptcopyin(sopt, &loop, sizeof(u_char),
2697 error = sooptcopyin(sopt, &iloop, sizeof(u_int),
2701 loop = (u_char)iloop;
2703 imo = inp_findmoptions(inp);
2704 imo->imo_multicast_loop = !!loop;
2709 case IP_ADD_MEMBERSHIP:
2710 case IP_ADD_SOURCE_MEMBERSHIP:
2711 case MCAST_JOIN_GROUP:
2712 case MCAST_JOIN_SOURCE_GROUP:
2713 error = inp_join_group(inp, sopt);
2716 case IP_DROP_MEMBERSHIP:
2717 case IP_DROP_SOURCE_MEMBERSHIP:
2718 case MCAST_LEAVE_GROUP:
2719 case MCAST_LEAVE_SOURCE_GROUP:
2720 error = inp_leave_group(inp, sopt);
2723 case IP_BLOCK_SOURCE:
2724 case IP_UNBLOCK_SOURCE:
2725 case MCAST_BLOCK_SOURCE:
2726 case MCAST_UNBLOCK_SOURCE:
2727 error = inp_block_unblock_source(inp, sopt);
2731 error = inp_set_source_filters(inp, sopt);
2739 INP_UNLOCK_ASSERT(inp);
2745 * Expose IGMP's multicast filter mode and source list(s) to userland,
2746 * keyed by (ifindex, group).
2747 * The filter mode is written out as a uint32_t, followed by
2748 * 0..n of struct in_addr.
2749 * For use by ifmcstat(8).
2750 * SMPng: NOTE: unlocked read of ifindex space.
2753 sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS)
2755 struct in_addr src, group;
2757 struct ifmultiaddr *ifma;
2758 struct in_multi *inm;
2759 struct ip_msource *ims;
2763 uint32_t fmode, ifindex;
2768 if (req->newptr != NULL)
2775 if (ifindex <= 0 || ifindex > V_if_index) {
2776 CTR2(KTR_IGMPV3, "%s: ifindex %u out of range",
2781 group.s_addr = name[1];
2782 if (!IN_MULTICAST(ntohl(group.s_addr))) {
2783 CTR2(KTR_IGMPV3, "%s: group %s is not multicast",
2784 __func__, inet_ntoa(group));
2788 ifp = ifnet_byindex(ifindex);
2790 CTR2(KTR_IGMPV3, "%s: no ifp for ifindex %u",
2795 retval = sysctl_wire_old_buffer(req,
2796 sizeof(uint32_t) + (in_mcast_maxgrpsrc * sizeof(struct in_addr)));
2803 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2804 if (ifma->ifma_addr->sa_family != AF_INET ||
2805 ifma->ifma_protospec == NULL)
2807 inm = (struct in_multi *)ifma->ifma_protospec;
2808 if (!in_hosteq(inm->inm_addr, group))
2810 fmode = inm->inm_st[1].iss_fmode;
2811 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2814 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
2817 ina.s_addr = htonl(ims->ims_haddr);
2818 CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2822 * Only copy-out sources which are in-mode.
2824 if (fmode != ims_get_mode(inm, ims, 1)) {
2825 CTR1(KTR_IGMPV3, "%s: skip non-in-mode",
2829 src.s_addr = htonl(ims->ims_haddr);
2830 retval = SYSCTL_OUT(req, &src, sizeof(struct in_addr));
2835 IF_ADDR_RUNLOCK(ifp);
2844 static const char *inm_modestrs[] = { "un", "in", "ex" };
2847 inm_mode_str(const int mode)
2850 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2851 return (inm_modestrs[mode]);
2855 static const char *inm_statestrs[] = {
2868 inm_state_str(const int state)
2871 if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
2872 return (inm_statestrs[state]);
2877 * Dump an in_multi structure to the console.
2880 inm_print(const struct in_multi *inm)
2884 if ((ktr_mask & KTR_IGMPV3) == 0)
2887 printf("%s: --- begin inm %p ---\n", __func__, inm);
2888 printf("addr %s ifp %p(%s) ifma %p\n",
2889 inet_ntoa(inm->inm_addr),
2891 inm->inm_ifp->if_xname,
2893 printf("timer %u state %s refcount %u scq.len %u\n",
2895 inm_state_str(inm->inm_state),
2897 inm->inm_scq.ifq_len);
2898 printf("igi %p nsrc %lu sctimer %u scrv %u\n",
2903 for (t = 0; t < 2; t++) {
2904 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
2905 inm_mode_str(inm->inm_st[t].iss_fmode),
2906 inm->inm_st[t].iss_asm,
2907 inm->inm_st[t].iss_ex,
2908 inm->inm_st[t].iss_in,
2909 inm->inm_st[t].iss_rec);
2911 printf("%s: --- end inm %p ---\n", __func__, inm);
2917 inm_print(const struct in_multi *inm)
2924 RB_GENERATE(ip_msource_tree, ip_msource, ims_link, ip_msource_cmp);