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>
42 #include <sys/malloc.h>
44 #include <sys/protosw.h>
45 #include <sys/rmlock.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/protosw.h>
49 #include <sys/sysctl.h>
51 #include <sys/taskqueue.h>
55 #include <net/if_var.h>
56 #include <net/if_dl.h>
57 #include <net/route.h>
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in_fib.h>
63 #include <netinet/in_pcb.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip_var.h>
66 #include <netinet/igmp_var.h>
69 #define KTR_IGMPV3 KTR_INET
72 #ifndef __SOCKUNION_DECLARED
74 struct sockaddr_storage ss;
76 struct sockaddr_dl sdl;
77 struct sockaddr_in sin;
79 typedef union sockunion sockunion_t;
80 #define __SOCKUNION_DECLARED
81 #endif /* __SOCKUNION_DECLARED */
83 static MALLOC_DEFINE(M_INMFILTER, "in_mfilter",
84 "IPv4 multicast PCB-layer source filter");
85 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "IPv4 multicast group");
86 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "IPv4 multicast options");
87 static MALLOC_DEFINE(M_IPMSOURCE, "ip_msource",
88 "IPv4 multicast IGMP-layer source filter");
92 * - Lock order is: Giant, INP_WLOCK, IN_MULTI_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
93 * - The IF_ADDR_LOCK is implicitly taken by inm_lookup() earlier, however
94 * it can be taken by code in net/if.c also.
95 * - ip_moptions and in_mfilter are covered by the INP_WLOCK.
97 * struct in_multi is covered by IN_MULTI_LOCK. There isn't strictly
98 * any need for in_multi itself to be virtualized -- it is bound to an ifp
99 * anyway no matter what happens.
101 struct mtx in_multi_mtx;
102 MTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF);
105 * Functions with non-static linkage defined in this file should be
106 * declared in in_var.h:
111 * in_joingroup_locked()
113 * in_leavegroup_locked()
119 * XXX: Both carp and pf need to use the legacy (*,G) KPIs in_addmulti()
122 static void imf_commit(struct in_mfilter *);
123 static int imf_get_source(struct in_mfilter *imf,
124 const struct sockaddr_in *psin,
125 struct in_msource **);
126 static struct in_msource *
127 imf_graft(struct in_mfilter *, const uint8_t,
128 const struct sockaddr_in *);
129 static void imf_leave(struct in_mfilter *);
130 static int imf_prune(struct in_mfilter *, const struct sockaddr_in *);
131 static void imf_purge(struct in_mfilter *);
132 static void imf_rollback(struct in_mfilter *);
133 static void imf_reap(struct in_mfilter *);
134 static int imo_grow(struct ip_moptions *);
135 static size_t imo_match_group(const struct ip_moptions *,
136 const struct ifnet *, const struct sockaddr *);
137 static struct in_msource *
138 imo_match_source(const struct ip_moptions *, const size_t,
139 const struct sockaddr *);
140 static void ims_merge(struct ip_msource *ims,
141 const struct in_msource *lims, const int rollback);
142 static int in_getmulti(struct ifnet *, const struct in_addr *,
144 static int inm_get_source(struct in_multi *inm, const in_addr_t haddr,
145 const int noalloc, struct ip_msource **pims);
147 static int inm_is_ifp_detached(const struct in_multi *);
149 static int inm_merge(struct in_multi *, /*const*/ struct in_mfilter *);
150 static void inm_purge(struct in_multi *);
151 static void inm_reap(struct in_multi *);
152 static struct ip_moptions *
153 inp_findmoptions(struct inpcb *);
154 static void inp_freemoptions_internal(struct ip_moptions *);
155 static void inp_gcmoptions(void *, int);
156 static int inp_get_source_filters(struct inpcb *, struct sockopt *);
157 static int inp_join_group(struct inpcb *, struct sockopt *);
158 static int inp_leave_group(struct inpcb *, struct sockopt *);
159 static struct ifnet *
160 inp_lookup_mcast_ifp(const struct inpcb *,
161 const struct sockaddr_in *, const struct in_addr);
162 static int inp_block_unblock_source(struct inpcb *, struct sockopt *);
163 static int inp_set_multicast_if(struct inpcb *, struct sockopt *);
164 static int inp_set_source_filters(struct inpcb *, struct sockopt *);
165 static int sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS);
167 static SYSCTL_NODE(_net_inet_ip, OID_AUTO, mcast, CTLFLAG_RW, 0,
170 static u_long in_mcast_maxgrpsrc = IP_MAX_GROUP_SRC_FILTER;
171 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxgrpsrc,
172 CTLFLAG_RWTUN, &in_mcast_maxgrpsrc, 0,
173 "Max source filters per group");
175 static u_long in_mcast_maxsocksrc = IP_MAX_SOCK_SRC_FILTER;
176 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxsocksrc,
177 CTLFLAG_RWTUN, &in_mcast_maxsocksrc, 0,
178 "Max source filters per socket");
180 int in_mcast_loop = IP_DEFAULT_MULTICAST_LOOP;
181 SYSCTL_INT(_net_inet_ip_mcast, OID_AUTO, loop, CTLFLAG_RWTUN,
182 &in_mcast_loop, 0, "Loopback multicast datagrams by default");
184 static SYSCTL_NODE(_net_inet_ip_mcast, OID_AUTO, filters,
185 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip_mcast_filters,
186 "Per-interface stack-wide source filters");
188 static STAILQ_HEAD(, ip_moptions) imo_gc_list =
189 STAILQ_HEAD_INITIALIZER(imo_gc_list);
190 static struct task imo_gc_task = TASK_INITIALIZER(0, inp_gcmoptions, NULL);
194 * Inline function which wraps assertions for a valid ifp.
195 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
199 inm_is_ifp_detached(const struct in_multi *inm)
203 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
204 ifp = inm->inm_ifma->ifma_ifp;
207 * Sanity check that netinet's notion of ifp is the
210 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
213 return (ifp == NULL);
218 * Initialize an in_mfilter structure to a known state at t0, t1
219 * with an empty source filter list.
222 imf_init(struct in_mfilter *imf, const int st0, const int st1)
224 memset(imf, 0, sizeof(struct in_mfilter));
225 RB_INIT(&imf->imf_sources);
226 imf->imf_st[0] = st0;
227 imf->imf_st[1] = st1;
231 * Function for looking up an in_multi record for an IPv4 multicast address
232 * on a given interface. ifp must be valid. If no record found, return NULL.
233 * The IN_MULTI_LOCK and IF_ADDR_LOCK on ifp must be held.
236 inm_lookup_locked(struct ifnet *ifp, const struct in_addr ina)
238 struct ifmultiaddr *ifma;
239 struct in_multi *inm;
241 IN_MULTI_LOCK_ASSERT();
242 IF_ADDR_LOCK_ASSERT(ifp);
245 TAILQ_FOREACH(ifma, &((ifp)->if_multiaddrs), ifma_link) {
246 if (ifma->ifma_addr->sa_family == AF_INET) {
247 inm = (struct in_multi *)ifma->ifma_protospec;
248 if (inm->inm_addr.s_addr == ina.s_addr)
257 * Wrapper for inm_lookup_locked().
258 * The IF_ADDR_LOCK will be taken on ifp and released on return.
261 inm_lookup(struct ifnet *ifp, const struct in_addr ina)
263 struct in_multi *inm;
265 IN_MULTI_LOCK_ASSERT();
267 inm = inm_lookup_locked(ifp, ina);
268 IF_ADDR_RUNLOCK(ifp);
274 * Resize the ip_moptions vector to the next power-of-two minus 1.
275 * May be called with locks held; do not sleep.
278 imo_grow(struct ip_moptions *imo)
280 struct in_multi **nmships;
281 struct in_multi **omships;
282 struct in_mfilter *nmfilters;
283 struct in_mfilter *omfilters;
290 omships = imo->imo_membership;
291 omfilters = imo->imo_mfilters;
292 oldmax = imo->imo_max_memberships;
293 newmax = ((oldmax + 1) * 2) - 1;
295 if (newmax <= IP_MAX_MEMBERSHIPS) {
296 nmships = (struct in_multi **)realloc(omships,
297 sizeof(struct in_multi *) * newmax, M_IPMOPTS, M_NOWAIT);
298 nmfilters = (struct in_mfilter *)realloc(omfilters,
299 sizeof(struct in_mfilter) * newmax, M_INMFILTER, M_NOWAIT);
300 if (nmships != NULL && nmfilters != NULL) {
301 /* Initialize newly allocated source filter heads. */
302 for (idx = oldmax; idx < newmax; idx++) {
303 imf_init(&nmfilters[idx], MCAST_UNDEFINED,
306 imo->imo_max_memberships = newmax;
307 imo->imo_membership = nmships;
308 imo->imo_mfilters = nmfilters;
312 if (nmships == NULL || nmfilters == NULL) {
314 free(nmships, M_IPMOPTS);
315 if (nmfilters != NULL)
316 free(nmfilters, M_INMFILTER);
317 return (ETOOMANYREFS);
324 * Find an IPv4 multicast group entry for this ip_moptions instance
325 * which matches the specified group, and optionally an interface.
326 * Return its index into the array, or -1 if not found.
329 imo_match_group(const struct ip_moptions *imo, const struct ifnet *ifp,
330 const struct sockaddr *group)
332 const struct sockaddr_in *gsin;
333 struct in_multi **pinm;
337 gsin = (const struct sockaddr_in *)group;
339 /* The imo_membership array may be lazy allocated. */
340 if (imo->imo_membership == NULL || imo->imo_num_memberships == 0)
343 nmships = imo->imo_num_memberships;
344 pinm = &imo->imo_membership[0];
345 for (idx = 0; idx < nmships; idx++, pinm++) {
348 if ((ifp == NULL || ((*pinm)->inm_ifp == ifp)) &&
349 in_hosteq((*pinm)->inm_addr, gsin->sin_addr)) {
360 * Find an IPv4 multicast source entry for this imo which matches
361 * the given group index for this socket, and source address.
363 * NOTE: This does not check if the entry is in-mode, merely if
364 * it exists, which may not be the desired behaviour.
366 static struct in_msource *
367 imo_match_source(const struct ip_moptions *imo, const size_t gidx,
368 const struct sockaddr *src)
370 struct ip_msource find;
371 struct in_mfilter *imf;
372 struct ip_msource *ims;
373 const sockunion_t *psa;
375 KASSERT(src->sa_family == AF_INET, ("%s: !AF_INET", __func__));
376 KASSERT(gidx != -1 && gidx < imo->imo_num_memberships,
377 ("%s: invalid index %d\n", __func__, (int)gidx));
379 /* The imo_mfilters array may be lazy allocated. */
380 if (imo->imo_mfilters == NULL)
382 imf = &imo->imo_mfilters[gidx];
384 /* Source trees are keyed in host byte order. */
385 psa = (const sockunion_t *)src;
386 find.ims_haddr = ntohl(psa->sin.sin_addr.s_addr);
387 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
389 return ((struct in_msource *)ims);
393 * Perform filtering for multicast datagrams on a socket by group and source.
395 * Returns 0 if a datagram should be allowed through, or various error codes
396 * if the socket was not a member of the group, or the source was muted, etc.
399 imo_multi_filter(const struct ip_moptions *imo, const struct ifnet *ifp,
400 const struct sockaddr *group, const struct sockaddr *src)
403 struct in_msource *ims;
406 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
408 gidx = imo_match_group(imo, ifp, group);
410 return (MCAST_NOTGMEMBER);
413 * Check if the source was included in an (S,G) join.
414 * Allow reception on exclusive memberships by default,
415 * reject reception on inclusive memberships by default.
416 * Exclude source only if an in-mode exclude filter exists.
417 * Include source only if an in-mode include filter exists.
418 * NOTE: We are comparing group state here at IGMP t1 (now)
419 * with socket-layer t0 (since last downcall).
421 mode = imo->imo_mfilters[gidx].imf_st[1];
422 ims = imo_match_source(imo, gidx, src);
424 if ((ims == NULL && mode == MCAST_INCLUDE) ||
425 (ims != NULL && ims->imsl_st[0] != mode))
426 return (MCAST_NOTSMEMBER);
432 * Find and return a reference to an in_multi record for (ifp, group),
433 * and bump its reference count.
434 * If one does not exist, try to allocate it, and update link-layer multicast
435 * filters on ifp to listen for group.
436 * Assumes the IN_MULTI lock is held across the call.
437 * Return 0 if successful, otherwise return an appropriate error code.
440 in_getmulti(struct ifnet *ifp, const struct in_addr *group,
441 struct in_multi **pinm)
443 struct sockaddr_in gsin;
444 struct ifmultiaddr *ifma;
445 struct in_ifinfo *ii;
446 struct in_multi *inm;
449 IN_MULTI_LOCK_ASSERT();
451 ii = (struct in_ifinfo *)ifp->if_afdata[AF_INET];
453 inm = inm_lookup(ifp, *group);
456 * If we already joined this group, just bump the
457 * refcount and return it.
459 KASSERT(inm->inm_refcount >= 1,
460 ("%s: bad refcount %d", __func__, inm->inm_refcount));
466 memset(&gsin, 0, sizeof(gsin));
467 gsin.sin_family = AF_INET;
468 gsin.sin_len = sizeof(struct sockaddr_in);
469 gsin.sin_addr = *group;
472 * Check if a link-layer group is already associated
473 * with this network-layer group on the given ifnet.
475 error = if_addmulti(ifp, (struct sockaddr *)&gsin, &ifma);
479 /* XXX ifma_protospec must be covered by IF_ADDR_LOCK */
483 * If something other than netinet is occupying the link-layer
484 * group, print a meaningful error message and back out of
486 * Otherwise, bump the refcount on the existing network-layer
487 * group association and return it.
489 if (ifma->ifma_protospec != NULL) {
490 inm = (struct in_multi *)ifma->ifma_protospec;
492 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
494 KASSERT(ifma->ifma_addr->sa_family == AF_INET,
495 ("%s: ifma not AF_INET", __func__));
496 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
497 if (inm->inm_ifma != ifma || inm->inm_ifp != ifp ||
498 !in_hosteq(inm->inm_addr, *group))
499 panic("%s: ifma %p is inconsistent with %p (%s)",
500 __func__, ifma, inm, inet_ntoa(*group));
504 IF_ADDR_WUNLOCK(ifp);
508 IF_ADDR_WLOCK_ASSERT(ifp);
511 * A new in_multi record is needed; allocate and initialize it.
512 * We DO NOT perform an IGMP join as the in_ layer may need to
513 * push an initial source list down to IGMP to support SSM.
515 * The initial source filter state is INCLUDE, {} as per the RFC.
517 inm = malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT | M_ZERO);
519 IF_ADDR_WUNLOCK(ifp);
520 if_delmulti_ifma(ifma);
523 inm->inm_addr = *group;
525 inm->inm_igi = ii->ii_igmp;
526 inm->inm_ifma = ifma;
527 inm->inm_refcount = 1;
528 inm->inm_state = IGMP_NOT_MEMBER;
529 mbufq_init(&inm->inm_scq, IGMP_MAX_STATE_CHANGES);
530 inm->inm_st[0].iss_fmode = MCAST_UNDEFINED;
531 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
532 RB_INIT(&inm->inm_srcs);
534 ifma->ifma_protospec = inm;
538 IF_ADDR_WUNLOCK(ifp);
543 * Drop a reference to an in_multi record.
545 * If the refcount drops to 0, free the in_multi record and
546 * delete the underlying link-layer membership.
549 inm_release_locked(struct in_multi *inm)
551 struct ifmultiaddr *ifma;
553 IN_MULTI_LOCK_ASSERT();
555 CTR2(KTR_IGMPV3, "%s: refcount is %d", __func__, inm->inm_refcount);
557 if (--inm->inm_refcount > 0) {
558 CTR2(KTR_IGMPV3, "%s: refcount is now %d", __func__,
563 CTR2(KTR_IGMPV3, "%s: freeing inm %p", __func__, inm);
565 ifma = inm->inm_ifma;
567 /* XXX this access is not covered by IF_ADDR_LOCK */
568 CTR2(KTR_IGMPV3, "%s: purging ifma %p", __func__, ifma);
569 KASSERT(ifma->ifma_protospec == inm,
570 ("%s: ifma_protospec != inm", __func__));
571 ifma->ifma_protospec = NULL;
575 free(inm, M_IPMADDR);
577 if_delmulti_ifma(ifma);
581 * Clear recorded source entries for a group.
582 * Used by the IGMP code. Caller must hold the IN_MULTI lock.
583 * FIXME: Should reap.
586 inm_clear_recorded(struct in_multi *inm)
588 struct ip_msource *ims;
590 IN_MULTI_LOCK_ASSERT();
592 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
595 --inm->inm_st[1].iss_rec;
598 KASSERT(inm->inm_st[1].iss_rec == 0,
599 ("%s: iss_rec %d not 0", __func__, inm->inm_st[1].iss_rec));
603 * Record a source as pending for a Source-Group IGMPv3 query.
604 * This lives here as it modifies the shared tree.
606 * inm is the group descriptor.
607 * naddr is the address of the source to record in network-byte order.
609 * If the net.inet.igmp.sgalloc sysctl is non-zero, we will
610 * lazy-allocate a source node in response to an SG query.
611 * Otherwise, no allocation is performed. This saves some memory
612 * with the trade-off that the source will not be reported to the
613 * router if joined in the window between the query response and
614 * the group actually being joined on the local host.
616 * VIMAGE: XXX: Currently the igmp_sgalloc feature has been removed.
617 * This turns off the allocation of a recorded source entry if
618 * the group has not been joined.
620 * Return 0 if the source didn't exist or was already marked as recorded.
621 * Return 1 if the source was marked as recorded by this function.
622 * Return <0 if any error occured (negated errno code).
625 inm_record_source(struct in_multi *inm, const in_addr_t naddr)
627 struct ip_msource find;
628 struct ip_msource *ims, *nims;
630 IN_MULTI_LOCK_ASSERT();
632 find.ims_haddr = ntohl(naddr);
633 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
634 if (ims && ims->ims_stp)
637 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
639 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
643 nims->ims_haddr = find.ims_haddr;
644 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
650 * Mark the source as recorded and update the recorded
654 ++inm->inm_st[1].iss_rec;
660 * Return a pointer to an in_msource owned by an in_mfilter,
661 * given its source address.
662 * Lazy-allocate if needed. If this is a new entry its filter state is
665 * imf is the filter set being modified.
666 * haddr is the source address in *host* byte-order.
668 * SMPng: May be called with locks held; malloc must not block.
671 imf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin,
672 struct in_msource **plims)
674 struct ip_msource find;
675 struct ip_msource *ims, *nims;
676 struct in_msource *lims;
683 /* key is host byte order */
684 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
685 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
686 lims = (struct in_msource *)ims;
688 if (imf->imf_nsrc == in_mcast_maxsocksrc)
690 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
694 lims = (struct in_msource *)nims;
695 lims->ims_haddr = find.ims_haddr;
696 lims->imsl_st[0] = MCAST_UNDEFINED;
697 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
707 * Graft a source entry into an existing socket-layer filter set,
708 * maintaining any required invariants and checking allocations.
710 * The source is marked as being in the new filter mode at t1.
712 * Return the pointer to the new node, otherwise return NULL.
714 static struct in_msource *
715 imf_graft(struct in_mfilter *imf, const uint8_t st1,
716 const struct sockaddr_in *psin)
718 struct ip_msource *nims;
719 struct in_msource *lims;
721 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
725 lims = (struct in_msource *)nims;
726 lims->ims_haddr = ntohl(psin->sin_addr.s_addr);
727 lims->imsl_st[0] = MCAST_UNDEFINED;
728 lims->imsl_st[1] = st1;
729 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
736 * Prune a source entry from an existing socket-layer filter set,
737 * maintaining any required invariants and checking allocations.
739 * The source is marked as being left at t1, it is not freed.
741 * Return 0 if no error occurred, otherwise return an errno value.
744 imf_prune(struct in_mfilter *imf, const struct sockaddr_in *psin)
746 struct ip_msource find;
747 struct ip_msource *ims;
748 struct in_msource *lims;
750 /* key is host byte order */
751 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
752 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
755 lims = (struct in_msource *)ims;
756 lims->imsl_st[1] = MCAST_UNDEFINED;
761 * Revert socket-layer filter set deltas at t1 to t0 state.
764 imf_rollback(struct in_mfilter *imf)
766 struct ip_msource *ims, *tims;
767 struct in_msource *lims;
769 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
770 lims = (struct in_msource *)ims;
771 if (lims->imsl_st[0] == lims->imsl_st[1]) {
772 /* no change at t1 */
774 } else if (lims->imsl_st[0] != MCAST_UNDEFINED) {
775 /* revert change to existing source at t1 */
776 lims->imsl_st[1] = lims->imsl_st[0];
778 /* revert source added t1 */
779 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
780 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
781 free(ims, M_INMFILTER);
785 imf->imf_st[1] = imf->imf_st[0];
789 * Mark socket-layer filter set as INCLUDE {} at t1.
792 imf_leave(struct in_mfilter *imf)
794 struct ip_msource *ims;
795 struct in_msource *lims;
797 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
798 lims = (struct in_msource *)ims;
799 lims->imsl_st[1] = MCAST_UNDEFINED;
801 imf->imf_st[1] = MCAST_INCLUDE;
805 * Mark socket-layer filter set deltas as committed.
808 imf_commit(struct in_mfilter *imf)
810 struct ip_msource *ims;
811 struct in_msource *lims;
813 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
814 lims = (struct in_msource *)ims;
815 lims->imsl_st[0] = lims->imsl_st[1];
817 imf->imf_st[0] = imf->imf_st[1];
821 * Reap unreferenced sources from socket-layer filter set.
824 imf_reap(struct in_mfilter *imf)
826 struct ip_msource *ims, *tims;
827 struct in_msource *lims;
829 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
830 lims = (struct in_msource *)ims;
831 if ((lims->imsl_st[0] == MCAST_UNDEFINED) &&
832 (lims->imsl_st[1] == MCAST_UNDEFINED)) {
833 CTR2(KTR_IGMPV3, "%s: free lims %p", __func__, ims);
834 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
835 free(ims, M_INMFILTER);
842 * Purge socket-layer filter set.
845 imf_purge(struct in_mfilter *imf)
847 struct ip_msource *ims, *tims;
849 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
850 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
851 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
852 free(ims, M_INMFILTER);
855 imf->imf_st[0] = imf->imf_st[1] = MCAST_UNDEFINED;
856 KASSERT(RB_EMPTY(&imf->imf_sources),
857 ("%s: imf_sources not empty", __func__));
861 * Look up a source filter entry for a multicast group.
863 * inm is the group descriptor to work with.
864 * haddr is the host-byte-order IPv4 address to look up.
865 * noalloc may be non-zero to suppress allocation of sources.
866 * *pims will be set to the address of the retrieved or allocated source.
868 * SMPng: NOTE: may be called with locks held.
869 * Return 0 if successful, otherwise return a non-zero error code.
872 inm_get_source(struct in_multi *inm, const in_addr_t haddr,
873 const int noalloc, struct ip_msource **pims)
875 struct ip_msource find;
876 struct ip_msource *ims, *nims;
881 find.ims_haddr = haddr;
882 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
883 if (ims == NULL && !noalloc) {
884 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
886 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
890 nims->ims_haddr = haddr;
891 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
895 ia.s_addr = htonl(haddr);
896 CTR3(KTR_IGMPV3, "%s: allocated %s as %p", __func__,
906 * Merge socket-layer source into IGMP-layer source.
907 * If rollback is non-zero, perform the inverse of the merge.
910 ims_merge(struct ip_msource *ims, const struct in_msource *lims,
913 int n = rollback ? -1 : 1;
917 ia.s_addr = htonl(ims->ims_haddr);
920 if (lims->imsl_st[0] == MCAST_EXCLUDE) {
921 CTR3(KTR_IGMPV3, "%s: t1 ex -= %d on %s",
922 __func__, n, inet_ntoa(ia));
923 ims->ims_st[1].ex -= n;
924 } else if (lims->imsl_st[0] == MCAST_INCLUDE) {
925 CTR3(KTR_IGMPV3, "%s: t1 in -= %d on %s",
926 __func__, n, inet_ntoa(ia));
927 ims->ims_st[1].in -= n;
930 if (lims->imsl_st[1] == MCAST_EXCLUDE) {
931 CTR3(KTR_IGMPV3, "%s: t1 ex += %d on %s",
932 __func__, n, inet_ntoa(ia));
933 ims->ims_st[1].ex += n;
934 } else if (lims->imsl_st[1] == MCAST_INCLUDE) {
935 CTR3(KTR_IGMPV3, "%s: t1 in += %d on %s",
936 __func__, n, inet_ntoa(ia));
937 ims->ims_st[1].in += n;
942 * Atomically update the global in_multi state, when a membership's
943 * filter list is being updated in any way.
945 * imf is the per-inpcb-membership group filter pointer.
946 * A fake imf may be passed for in-kernel consumers.
948 * XXX This is a candidate for a set-symmetric-difference style loop
949 * which would eliminate the repeated lookup from root of ims nodes,
950 * as they share the same key space.
952 * If any error occurred this function will back out of refcounts
953 * and return a non-zero value.
956 inm_merge(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
958 struct ip_msource *ims, *nims;
959 struct in_msource *lims;
968 * Update the source filters first, as this may fail.
969 * Maintain count of in-mode filters at t0, t1. These are
970 * used to work out if we transition into ASM mode or not.
971 * Maintain a count of source filters whose state was
972 * actually modified by this operation.
974 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
975 lims = (struct in_msource *)ims;
976 if (lims->imsl_st[0] == imf->imf_st[0]) nsrc0++;
977 if (lims->imsl_st[1] == imf->imf_st[1]) nsrc1++;
978 if (lims->imsl_st[0] == lims->imsl_st[1]) continue;
979 error = inm_get_source(inm, lims->ims_haddr, 0, &nims);
983 ims_merge(nims, lims, 0);
986 struct ip_msource *bims;
988 RB_FOREACH_REVERSE_FROM(ims, ip_msource_tree, nims) {
989 lims = (struct in_msource *)ims;
990 if (lims->imsl_st[0] == lims->imsl_st[1])
992 (void)inm_get_source(inm, lims->ims_haddr, 1, &bims);
995 ims_merge(bims, lims, 1);
1000 CTR3(KTR_IGMPV3, "%s: imf filters in-mode: %d at t0, %d at t1",
1001 __func__, nsrc0, nsrc1);
1003 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1004 if (imf->imf_st[0] == imf->imf_st[1] &&
1005 imf->imf_st[1] == MCAST_INCLUDE) {
1007 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
1008 --inm->inm_st[1].iss_in;
1012 /* Handle filter mode transition on socket. */
1013 if (imf->imf_st[0] != imf->imf_st[1]) {
1014 CTR3(KTR_IGMPV3, "%s: imf transition %d to %d",
1015 __func__, imf->imf_st[0], imf->imf_st[1]);
1017 if (imf->imf_st[0] == MCAST_EXCLUDE) {
1018 CTR1(KTR_IGMPV3, "%s: --ex on inm at t1", __func__);
1019 --inm->inm_st[1].iss_ex;
1020 } else if (imf->imf_st[0] == MCAST_INCLUDE) {
1021 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
1022 --inm->inm_st[1].iss_in;
1025 if (imf->imf_st[1] == MCAST_EXCLUDE) {
1026 CTR1(KTR_IGMPV3, "%s: ex++ on inm at t1", __func__);
1027 inm->inm_st[1].iss_ex++;
1028 } else if (imf->imf_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
1029 CTR1(KTR_IGMPV3, "%s: in++ on inm at t1", __func__);
1030 inm->inm_st[1].iss_in++;
1035 * Track inm filter state in terms of listener counts.
1036 * If there are any exclusive listeners, stack-wide
1037 * membership is exclusive.
1038 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1039 * If no listeners remain, state is undefined at t1,
1040 * and the IGMP lifecycle for this group should finish.
1042 if (inm->inm_st[1].iss_ex > 0) {
1043 CTR1(KTR_IGMPV3, "%s: transition to EX", __func__);
1044 inm->inm_st[1].iss_fmode = MCAST_EXCLUDE;
1045 } else if (inm->inm_st[1].iss_in > 0) {
1046 CTR1(KTR_IGMPV3, "%s: transition to IN", __func__);
1047 inm->inm_st[1].iss_fmode = MCAST_INCLUDE;
1049 CTR1(KTR_IGMPV3, "%s: transition to UNDEF", __func__);
1050 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
1053 /* Decrement ASM listener count on transition out of ASM mode. */
1054 if (imf->imf_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1055 if ((imf->imf_st[1] != MCAST_EXCLUDE) ||
1056 (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 > 0))
1057 CTR1(KTR_IGMPV3, "%s: --asm on inm at t1", __func__);
1058 --inm->inm_st[1].iss_asm;
1061 /* Increment ASM listener count on transition to ASM mode. */
1062 if (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1063 CTR1(KTR_IGMPV3, "%s: asm++ on inm at t1", __func__);
1064 inm->inm_st[1].iss_asm++;
1067 CTR3(KTR_IGMPV3, "%s: merged imf %p to inm %p", __func__, imf, inm);
1072 CTR1(KTR_IGMPV3, "%s: sources changed; reaping", __func__);
1079 * Mark an in_multi's filter set deltas as committed.
1080 * Called by IGMP after a state change has been enqueued.
1083 inm_commit(struct in_multi *inm)
1085 struct ip_msource *ims;
1087 CTR2(KTR_IGMPV3, "%s: commit inm %p", __func__, inm);
1088 CTR1(KTR_IGMPV3, "%s: pre commit:", __func__);
1091 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
1092 ims->ims_st[0] = ims->ims_st[1];
1094 inm->inm_st[0] = inm->inm_st[1];
1098 * Reap unreferenced nodes from an in_multi's filter set.
1101 inm_reap(struct in_multi *inm)
1103 struct ip_msource *ims, *tims;
1105 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1106 if (ims->ims_st[0].ex > 0 || ims->ims_st[0].in > 0 ||
1107 ims->ims_st[1].ex > 0 || ims->ims_st[1].in > 0 ||
1110 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1111 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1112 free(ims, M_IPMSOURCE);
1118 * Purge all source nodes from an in_multi's filter set.
1121 inm_purge(struct in_multi *inm)
1123 struct ip_msource *ims, *tims;
1125 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1126 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1127 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1128 free(ims, M_IPMSOURCE);
1134 * Join a multicast group; unlocked entry point.
1136 * SMPng: XXX: in_joingroup() is called from in_control() when Giant
1137 * is not held. Fortunately, ifp is unlikely to have been detached
1138 * at this point, so we assume it's OK to recurse.
1141 in_joingroup(struct ifnet *ifp, const struct in_addr *gina,
1142 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1147 error = in_joingroup_locked(ifp, gina, imf, pinm);
1154 * Join a multicast group; real entry point.
1156 * Only preserves atomicity at inm level.
1157 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1159 * If the IGMP downcall fails, the group is not joined, and an error
1163 in_joingroup_locked(struct ifnet *ifp, const struct in_addr *gina,
1164 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1166 struct in_mfilter timf;
1167 struct in_multi *inm;
1170 IN_MULTI_LOCK_ASSERT();
1172 CTR4(KTR_IGMPV3, "%s: join %s on %p(%s))", __func__,
1173 inet_ntoa(*gina), ifp, ifp->if_xname);
1179 * If no imf was specified (i.e. kernel consumer),
1180 * fake one up and assume it is an ASM join.
1183 imf_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1187 error = in_getmulti(ifp, gina, &inm);
1189 CTR1(KTR_IGMPV3, "%s: in_getmulti() failure", __func__);
1193 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1194 error = inm_merge(inm, imf);
1196 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1197 goto out_inm_release;
1200 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1201 error = igmp_change_state(inm);
1203 CTR1(KTR_IGMPV3, "%s: failed to update source", __func__);
1204 goto out_inm_release;
1209 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1210 inm_release_locked(inm);
1219 * Leave a multicast group; unlocked entry point.
1222 in_leavegroup(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1227 error = in_leavegroup_locked(inm, imf);
1234 * Leave a multicast group; real entry point.
1235 * All source filters will be expunged.
1237 * Only preserves atomicity at inm level.
1239 * Holding the write lock for the INP which contains imf
1240 * is highly advisable. We can't assert for it as imf does not
1241 * contain a back-pointer to the owning inp.
1243 * Note: This is not the same as inm_release(*) as this function also
1244 * makes a state change downcall into IGMP.
1247 in_leavegroup_locked(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1249 struct in_mfilter timf;
1254 IN_MULTI_LOCK_ASSERT();
1256 CTR5(KTR_IGMPV3, "%s: leave inm %p, %s/%s, imf %p", __func__,
1257 inm, inet_ntoa(inm->inm_addr),
1258 (inm_is_ifp_detached(inm) ? "null" : inm->inm_ifp->if_xname),
1262 * If no imf was specified (i.e. kernel consumer),
1263 * fake one up and assume it is an ASM join.
1266 imf_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1271 * Begin state merge transaction at IGMP layer.
1273 * As this particular invocation should not cause any memory
1274 * to be allocated, and there is no opportunity to roll back
1275 * the transaction, it MUST NOT fail.
1277 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1278 error = inm_merge(inm, imf);
1279 KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1281 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1282 CURVNET_SET(inm->inm_ifp->if_vnet);
1283 error = igmp_change_state(inm);
1286 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1288 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1289 inm_release_locked(inm);
1294 /*#ifndef BURN_BRIDGES*/
1296 * Join an IPv4 multicast group in (*,G) exclusive mode.
1297 * The group must be a 224.0.0.0/24 link-scope group.
1298 * This KPI is for legacy kernel consumers only.
1301 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1303 struct in_multi *pinm;
1306 KASSERT(IN_LOCAL_GROUP(ntohl(ap->s_addr)),
1307 ("%s: %s not in 224.0.0.0/24", __func__, inet_ntoa(*ap)));
1309 error = in_joingroup(ifp, ap, NULL, &pinm);
1317 * Leave an IPv4 multicast group, assumed to be in exclusive (*,G) mode.
1318 * This KPI is for legacy kernel consumers only.
1321 in_delmulti(struct in_multi *inm)
1324 (void)in_leavegroup(inm, NULL);
1329 * Block or unblock an ASM multicast source on an inpcb.
1330 * This implements the delta-based API described in RFC 3678.
1332 * The delta-based API applies only to exclusive-mode memberships.
1333 * An IGMP downcall will be performed.
1335 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1337 * Return 0 if successful, otherwise return an appropriate error code.
1340 inp_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1342 struct group_source_req gsr;
1343 sockunion_t *gsa, *ssa;
1345 struct in_mfilter *imf;
1346 struct ip_moptions *imo;
1347 struct in_msource *ims;
1348 struct in_multi *inm;
1357 memset(&gsr, 0, sizeof(struct group_source_req));
1358 gsa = (sockunion_t *)&gsr.gsr_group;
1359 ssa = (sockunion_t *)&gsr.gsr_source;
1361 switch (sopt->sopt_name) {
1362 case IP_BLOCK_SOURCE:
1363 case IP_UNBLOCK_SOURCE: {
1364 struct ip_mreq_source mreqs;
1366 error = sooptcopyin(sopt, &mreqs,
1367 sizeof(struct ip_mreq_source),
1368 sizeof(struct ip_mreq_source));
1372 gsa->sin.sin_family = AF_INET;
1373 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1374 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1376 ssa->sin.sin_family = AF_INET;
1377 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1378 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1380 if (!in_nullhost(mreqs.imr_interface))
1381 INADDR_TO_IFP(mreqs.imr_interface, ifp);
1383 if (sopt->sopt_name == IP_BLOCK_SOURCE)
1386 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
1387 __func__, inet_ntoa(mreqs.imr_interface), ifp);
1391 case MCAST_BLOCK_SOURCE:
1392 case MCAST_UNBLOCK_SOURCE:
1393 error = sooptcopyin(sopt, &gsr,
1394 sizeof(struct group_source_req),
1395 sizeof(struct group_source_req));
1399 if (gsa->sin.sin_family != AF_INET ||
1400 gsa->sin.sin_len != sizeof(struct sockaddr_in))
1403 if (ssa->sin.sin_family != AF_INET ||
1404 ssa->sin.sin_len != sizeof(struct sockaddr_in))
1407 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1408 return (EADDRNOTAVAIL);
1410 ifp = ifnet_byindex(gsr.gsr_interface);
1412 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1417 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
1418 __func__, sopt->sopt_name);
1419 return (EOPNOTSUPP);
1423 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1427 * Check if we are actually a member of this group.
1429 imo = inp_findmoptions(inp);
1430 idx = imo_match_group(imo, ifp, &gsa->sa);
1431 if (idx == -1 || imo->imo_mfilters == NULL) {
1432 error = EADDRNOTAVAIL;
1433 goto out_inp_locked;
1436 KASSERT(imo->imo_mfilters != NULL,
1437 ("%s: imo_mfilters not allocated", __func__));
1438 imf = &imo->imo_mfilters[idx];
1439 inm = imo->imo_membership[idx];
1442 * Attempting to use the delta-based API on an
1443 * non exclusive-mode membership is an error.
1445 fmode = imf->imf_st[0];
1446 if (fmode != MCAST_EXCLUDE) {
1448 goto out_inp_locked;
1452 * Deal with error cases up-front:
1453 * Asked to block, but already blocked; or
1454 * Asked to unblock, but nothing to unblock.
1455 * If adding a new block entry, allocate it.
1457 ims = imo_match_source(imo, idx, &ssa->sa);
1458 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1459 CTR3(KTR_IGMPV3, "%s: source %s %spresent", __func__,
1460 inet_ntoa(ssa->sin.sin_addr), doblock ? "" : "not ");
1461 error = EADDRNOTAVAIL;
1462 goto out_inp_locked;
1465 INP_WLOCK_ASSERT(inp);
1468 * Begin state merge transaction at socket layer.
1471 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
1472 ims = imf_graft(imf, fmode, &ssa->sin);
1476 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
1477 error = imf_prune(imf, &ssa->sin);
1481 CTR1(KTR_IGMPV3, "%s: merge imf state failed", __func__);
1482 goto out_imf_rollback;
1486 * Begin state merge transaction at IGMP layer.
1490 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1491 error = inm_merge(inm, imf);
1493 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1494 goto out_in_multi_locked;
1497 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1498 error = igmp_change_state(inm);
1500 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1502 out_in_multi_locked:
1520 * Given an inpcb, return its multicast options structure pointer. Accepts
1521 * an unlocked inpcb pointer, but will return it locked. May sleep.
1523 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1524 * SMPng: NOTE: Returns with the INP write lock held.
1526 static struct ip_moptions *
1527 inp_findmoptions(struct inpcb *inp)
1529 struct ip_moptions *imo;
1530 struct in_multi **immp;
1531 struct in_mfilter *imfp;
1535 if (inp->inp_moptions != NULL)
1536 return (inp->inp_moptions);
1540 imo = malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK);
1541 immp = malloc(sizeof(*immp) * IP_MIN_MEMBERSHIPS, M_IPMOPTS,
1543 imfp = malloc(sizeof(struct in_mfilter) * IP_MIN_MEMBERSHIPS,
1544 M_INMFILTER, M_WAITOK);
1546 imo->imo_multicast_ifp = NULL;
1547 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1548 imo->imo_multicast_vif = -1;
1549 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1550 imo->imo_multicast_loop = in_mcast_loop;
1551 imo->imo_num_memberships = 0;
1552 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1553 imo->imo_membership = immp;
1555 /* Initialize per-group source filters. */
1556 for (idx = 0; idx < IP_MIN_MEMBERSHIPS; idx++)
1557 imf_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE);
1558 imo->imo_mfilters = imfp;
1561 if (inp->inp_moptions != NULL) {
1562 free(imfp, M_INMFILTER);
1563 free(immp, M_IPMOPTS);
1564 free(imo, M_IPMOPTS);
1565 return (inp->inp_moptions);
1567 inp->inp_moptions = imo;
1572 * Discard the IP multicast options (and source filters). To minimize
1573 * the amount of work done while holding locks such as the INP's
1574 * pcbinfo lock (which is used in the receive path), the free
1575 * operation is performed asynchronously in a separate task.
1577 * SMPng: NOTE: assumes INP write lock is held.
1580 inp_freemoptions(struct ip_moptions *imo)
1583 KASSERT(imo != NULL, ("%s: ip_moptions is NULL", __func__));
1585 STAILQ_INSERT_TAIL(&imo_gc_list, imo, imo_link);
1587 taskqueue_enqueue(taskqueue_thread, &imo_gc_task);
1591 inp_freemoptions_internal(struct ip_moptions *imo)
1593 struct in_mfilter *imf;
1594 size_t idx, nmships;
1596 nmships = imo->imo_num_memberships;
1597 for (idx = 0; idx < nmships; ++idx) {
1598 imf = imo->imo_mfilters ? &imo->imo_mfilters[idx] : NULL;
1601 (void)in_leavegroup(imo->imo_membership[idx], imf);
1606 if (imo->imo_mfilters)
1607 free(imo->imo_mfilters, M_INMFILTER);
1608 free(imo->imo_membership, M_IPMOPTS);
1609 free(imo, M_IPMOPTS);
1613 inp_gcmoptions(void *context, int pending)
1615 struct ip_moptions *imo;
1618 while (!STAILQ_EMPTY(&imo_gc_list)) {
1619 imo = STAILQ_FIRST(&imo_gc_list);
1620 STAILQ_REMOVE_HEAD(&imo_gc_list, imo_link);
1622 inp_freemoptions_internal(imo);
1629 * Atomically get source filters on a socket for an IPv4 multicast group.
1630 * Called with INP lock held; returns with lock released.
1633 inp_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1635 struct __msfilterreq msfr;
1638 struct ip_moptions *imo;
1639 struct in_mfilter *imf;
1640 struct ip_msource *ims;
1641 struct in_msource *lims;
1642 struct sockaddr_in *psin;
1643 struct sockaddr_storage *ptss;
1644 struct sockaddr_storage *tss;
1646 size_t idx, nsrcs, ncsrcs;
1648 INP_WLOCK_ASSERT(inp);
1650 imo = inp->inp_moptions;
1651 KASSERT(imo != NULL, ("%s: null ip_moptions", __func__));
1655 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1656 sizeof(struct __msfilterreq));
1660 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
1663 ifp = ifnet_byindex(msfr.msfr_ifindex);
1670 * Lookup group on the socket.
1672 gsa = (sockunion_t *)&msfr.msfr_group;
1673 idx = imo_match_group(imo, ifp, &gsa->sa);
1674 if (idx == -1 || imo->imo_mfilters == NULL) {
1676 return (EADDRNOTAVAIL);
1678 imf = &imo->imo_mfilters[idx];
1681 * Ignore memberships which are in limbo.
1683 if (imf->imf_st[1] == MCAST_UNDEFINED) {
1687 msfr.msfr_fmode = imf->imf_st[1];
1690 * If the user specified a buffer, copy out the source filter
1691 * entries to userland gracefully.
1692 * We only copy out the number of entries which userland
1693 * has asked for, but we always tell userland how big the
1694 * buffer really needs to be.
1696 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
1697 msfr.msfr_nsrcs = in_mcast_maxsocksrc;
1699 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1700 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1701 M_TEMP, M_NOWAIT | M_ZERO);
1709 * Count number of sources in-mode at t0.
1710 * If buffer space exists and remains, copy out source entries.
1712 nsrcs = msfr.msfr_nsrcs;
1715 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
1716 lims = (struct in_msource *)ims;
1717 if (lims->imsl_st[0] == MCAST_UNDEFINED ||
1718 lims->imsl_st[0] != imf->imf_st[0])
1721 if (tss != NULL && nsrcs > 0) {
1722 psin = (struct sockaddr_in *)ptss;
1723 psin->sin_family = AF_INET;
1724 psin->sin_len = sizeof(struct sockaddr_in);
1725 psin->sin_addr.s_addr = htonl(lims->ims_haddr);
1735 error = copyout(tss, msfr.msfr_srcs,
1736 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1742 msfr.msfr_nsrcs = ncsrcs;
1743 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1749 * Return the IP multicast options in response to user getsockopt().
1752 inp_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1754 struct rm_priotracker in_ifa_tracker;
1755 struct ip_mreqn mreqn;
1756 struct ip_moptions *imo;
1758 struct in_ifaddr *ia;
1763 imo = inp->inp_moptions;
1765 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1766 * or is a divert socket, reject it.
1768 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1769 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1770 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
1772 return (EOPNOTSUPP);
1776 switch (sopt->sopt_name) {
1777 case IP_MULTICAST_VIF:
1779 optval = imo->imo_multicast_vif;
1783 error = sooptcopyout(sopt, &optval, sizeof(int));
1786 case IP_MULTICAST_IF:
1787 memset(&mreqn, 0, sizeof(struct ip_mreqn));
1789 ifp = imo->imo_multicast_ifp;
1790 if (!in_nullhost(imo->imo_multicast_addr)) {
1791 mreqn.imr_address = imo->imo_multicast_addr;
1792 } else if (ifp != NULL) {
1793 mreqn.imr_ifindex = ifp->if_index;
1794 IFP_TO_IA(ifp, ia, &in_ifa_tracker);
1797 IA_SIN(ia)->sin_addr;
1798 ifa_free(&ia->ia_ifa);
1803 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
1804 error = sooptcopyout(sopt, &mreqn,
1805 sizeof(struct ip_mreqn));
1807 error = sooptcopyout(sopt, &mreqn.imr_address,
1808 sizeof(struct in_addr));
1812 case IP_MULTICAST_TTL:
1814 optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1816 optval = coptval = imo->imo_multicast_ttl;
1818 if (sopt->sopt_valsize == sizeof(u_char))
1819 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1821 error = sooptcopyout(sopt, &optval, sizeof(int));
1824 case IP_MULTICAST_LOOP:
1826 optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1828 optval = coptval = imo->imo_multicast_loop;
1830 if (sopt->sopt_valsize == sizeof(u_char))
1831 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1833 error = sooptcopyout(sopt, &optval, sizeof(int));
1838 error = EADDRNOTAVAIL;
1841 error = inp_get_source_filters(inp, sopt);
1847 error = ENOPROTOOPT;
1851 INP_UNLOCK_ASSERT(inp);
1857 * Look up the ifnet to use for a multicast group membership,
1858 * given the IPv4 address of an interface, and the IPv4 group address.
1860 * This routine exists to support legacy multicast applications
1861 * which do not understand that multicast memberships are scoped to
1862 * specific physical links in the networking stack, or which need
1863 * to join link-scope groups before IPv4 addresses are configured.
1865 * If inp is non-NULL, use this socket's current FIB number for any
1866 * required FIB lookup.
1867 * If ina is INADDR_ANY, look up the group address in the unicast FIB,
1868 * and use its ifp; usually, this points to the default next-hop.
1870 * If the FIB lookup fails, attempt to use the first non-loopback
1871 * interface with multicast capability in the system as a
1872 * last resort. The legacy IPv4 ASM API requires that we do
1873 * this in order to allow groups to be joined when the routing
1874 * table has not yet been populated during boot.
1876 * Returns NULL if no ifp could be found.
1878 * SMPng: TODO: Acquire the appropriate locks for INADDR_TO_IFP.
1879 * FUTURE: Implement IPv4 source-address selection.
1881 static struct ifnet *
1882 inp_lookup_mcast_ifp(const struct inpcb *inp,
1883 const struct sockaddr_in *gsin, const struct in_addr ina)
1885 struct rm_priotracker in_ifa_tracker;
1887 struct nhop4_basic nh4;
1890 KASSERT(gsin->sin_family == AF_INET, ("%s: not AF_INET", __func__));
1891 KASSERT(IN_MULTICAST(ntohl(gsin->sin_addr.s_addr)),
1892 ("%s: not multicast", __func__));
1895 if (!in_nullhost(ina)) {
1896 INADDR_TO_IFP(ina, ifp);
1898 fibnum = inp ? inp->inp_inc.inc_fibnum : 0;
1899 if (fib4_lookup_nh_basic(fibnum, gsin->sin_addr, 0, 0, &nh4)==0)
1902 struct in_ifaddr *ia;
1906 IN_IFADDR_RLOCK(&in_ifa_tracker);
1907 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1909 if (!(mifp->if_flags & IFF_LOOPBACK) &&
1910 (mifp->if_flags & IFF_MULTICAST)) {
1915 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1923 * Join an IPv4 multicast group, possibly with a source.
1926 inp_join_group(struct inpcb *inp, struct sockopt *sopt)
1928 struct group_source_req gsr;
1929 sockunion_t *gsa, *ssa;
1931 struct in_mfilter *imf;
1932 struct ip_moptions *imo;
1933 struct in_multi *inm;
1934 struct in_msource *lims;
1944 memset(&gsr, 0, sizeof(struct group_source_req));
1945 gsa = (sockunion_t *)&gsr.gsr_group;
1946 gsa->ss.ss_family = AF_UNSPEC;
1947 ssa = (sockunion_t *)&gsr.gsr_source;
1948 ssa->ss.ss_family = AF_UNSPEC;
1950 switch (sopt->sopt_name) {
1951 case IP_ADD_MEMBERSHIP:
1952 case IP_ADD_SOURCE_MEMBERSHIP: {
1953 struct ip_mreq_source mreqs;
1955 if (sopt->sopt_name == IP_ADD_MEMBERSHIP) {
1956 error = sooptcopyin(sopt, &mreqs,
1957 sizeof(struct ip_mreq),
1958 sizeof(struct ip_mreq));
1960 * Do argument switcharoo from ip_mreq into
1961 * ip_mreq_source to avoid using two instances.
1963 mreqs.imr_interface = mreqs.imr_sourceaddr;
1964 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
1965 } else if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
1966 error = sooptcopyin(sopt, &mreqs,
1967 sizeof(struct ip_mreq_source),
1968 sizeof(struct ip_mreq_source));
1973 gsa->sin.sin_family = AF_INET;
1974 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1975 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1977 if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
1978 ssa->sin.sin_family = AF_INET;
1979 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1980 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1983 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1986 ifp = inp_lookup_mcast_ifp(inp, &gsa->sin,
1987 mreqs.imr_interface);
1988 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
1989 __func__, inet_ntoa(mreqs.imr_interface), ifp);
1993 case MCAST_JOIN_GROUP:
1994 case MCAST_JOIN_SOURCE_GROUP:
1995 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1996 error = sooptcopyin(sopt, &gsr,
1997 sizeof(struct group_req),
1998 sizeof(struct group_req));
1999 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
2000 error = sooptcopyin(sopt, &gsr,
2001 sizeof(struct group_source_req),
2002 sizeof(struct group_source_req));
2007 if (gsa->sin.sin_family != AF_INET ||
2008 gsa->sin.sin_len != sizeof(struct sockaddr_in))
2012 * Overwrite the port field if present, as the sockaddr
2013 * being copied in may be matched with a binary comparison.
2015 gsa->sin.sin_port = 0;
2016 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
2017 if (ssa->sin.sin_family != AF_INET ||
2018 ssa->sin.sin_len != sizeof(struct sockaddr_in))
2020 ssa->sin.sin_port = 0;
2023 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2026 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
2027 return (EADDRNOTAVAIL);
2028 ifp = ifnet_byindex(gsr.gsr_interface);
2032 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2033 __func__, sopt->sopt_name);
2034 return (EOPNOTSUPP);
2038 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
2039 return (EADDRNOTAVAIL);
2041 imo = inp_findmoptions(inp);
2042 idx = imo_match_group(imo, ifp, &gsa->sa);
2046 inm = imo->imo_membership[idx];
2047 imf = &imo->imo_mfilters[idx];
2048 if (ssa->ss.ss_family != AF_UNSPEC) {
2050 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2051 * is an error. On an existing inclusive membership,
2052 * it just adds the source to the filter list.
2054 if (imf->imf_st[1] != MCAST_INCLUDE) {
2056 goto out_inp_locked;
2059 * Throw out duplicates.
2061 * XXX FIXME: This makes a naive assumption that
2062 * even if entries exist for *ssa in this imf,
2063 * they will be rejected as dupes, even if they
2064 * are not valid in the current mode (in-mode).
2066 * in_msource is transactioned just as for anything
2067 * else in SSM -- but note naive use of inm_graft()
2068 * below for allocating new filter entries.
2070 * This is only an issue if someone mixes the
2071 * full-state SSM API with the delta-based API,
2072 * which is discouraged in the relevant RFCs.
2074 lims = imo_match_source(imo, idx, &ssa->sa);
2075 if (lims != NULL /*&&
2076 lims->imsl_st[1] == MCAST_INCLUDE*/) {
2077 error = EADDRNOTAVAIL;
2078 goto out_inp_locked;
2082 * MCAST_JOIN_GROUP on an existing exclusive
2083 * membership is an error; return EADDRINUSE
2084 * to preserve 4.4BSD API idempotence, and
2085 * avoid tedious detour to code below.
2086 * NOTE: This is bending RFC 3678 a bit.
2088 * On an existing inclusive membership, this is also
2089 * an error; if you want to change filter mode,
2090 * you must use the userland API setsourcefilter().
2091 * XXX We don't reject this for imf in UNDEFINED
2092 * state at t1, because allocation of a filter
2093 * is atomic with allocation of a membership.
2096 if (imf->imf_st[1] == MCAST_EXCLUDE)
2098 goto out_inp_locked;
2103 * Begin state merge transaction at socket layer.
2105 INP_WLOCK_ASSERT(inp);
2108 if (imo->imo_num_memberships == imo->imo_max_memberships) {
2109 error = imo_grow(imo);
2111 goto out_inp_locked;
2114 * Allocate the new slot upfront so we can deal with
2115 * grafting the new source filter in same code path
2116 * as for join-source on existing membership.
2118 idx = imo->imo_num_memberships;
2119 imo->imo_membership[idx] = NULL;
2120 imo->imo_num_memberships++;
2121 KASSERT(imo->imo_mfilters != NULL,
2122 ("%s: imf_mfilters vector was not allocated", __func__));
2123 imf = &imo->imo_mfilters[idx];
2124 KASSERT(RB_EMPTY(&imf->imf_sources),
2125 ("%s: imf_sources not empty", __func__));
2129 * Graft new source into filter list for this inpcb's
2130 * membership of the group. The in_multi may not have
2131 * been allocated yet if this is a new membership, however,
2132 * the in_mfilter slot will be allocated and must be initialized.
2134 * Note: Grafting of exclusive mode filters doesn't happen
2136 * XXX: Should check for non-NULL lims (node exists but may
2137 * not be in-mode) for interop with full-state API.
2139 if (ssa->ss.ss_family != AF_UNSPEC) {
2140 /* Membership starts in IN mode */
2142 CTR1(KTR_IGMPV3, "%s: new join w/source", __func__);
2143 imf_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE);
2145 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
2147 lims = imf_graft(imf, MCAST_INCLUDE, &ssa->sin);
2149 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2155 /* No address specified; Membership starts in EX mode */
2157 CTR1(KTR_IGMPV3, "%s: new join w/o source", __func__);
2158 imf_init(imf, MCAST_UNDEFINED, MCAST_EXCLUDE);
2163 * Begin state merge transaction at IGMP layer.
2168 error = in_joingroup_locked(ifp, &gsa->sin.sin_addr, imf,
2171 CTR1(KTR_IGMPV3, "%s: in_joingroup_locked failed",
2176 imo->imo_membership[idx] = inm;
2178 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2179 error = inm_merge(inm, imf);
2181 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2183 goto out_in_multi_locked;
2185 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2186 error = igmp_change_state(inm);
2188 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2190 goto out_in_multi_locked;
2194 out_in_multi_locked:
2198 INP_WLOCK_ASSERT(inp);
2210 if (error && is_new) {
2211 imo->imo_membership[idx] = NULL;
2212 --imo->imo_num_memberships;
2221 * Leave an IPv4 multicast group on an inpcb, possibly with a source.
2224 inp_leave_group(struct inpcb *inp, struct sockopt *sopt)
2226 struct group_source_req gsr;
2227 struct ip_mreq_source mreqs;
2228 sockunion_t *gsa, *ssa;
2230 struct in_mfilter *imf;
2231 struct ip_moptions *imo;
2232 struct in_msource *ims;
2233 struct in_multi *inm;
2235 int error, is_final;
2241 memset(&gsr, 0, sizeof(struct group_source_req));
2242 gsa = (sockunion_t *)&gsr.gsr_group;
2243 gsa->ss.ss_family = AF_UNSPEC;
2244 ssa = (sockunion_t *)&gsr.gsr_source;
2245 ssa->ss.ss_family = AF_UNSPEC;
2247 switch (sopt->sopt_name) {
2248 case IP_DROP_MEMBERSHIP:
2249 case IP_DROP_SOURCE_MEMBERSHIP:
2250 if (sopt->sopt_name == IP_DROP_MEMBERSHIP) {
2251 error = sooptcopyin(sopt, &mreqs,
2252 sizeof(struct ip_mreq),
2253 sizeof(struct ip_mreq));
2255 * Swap interface and sourceaddr arguments,
2256 * as ip_mreq and ip_mreq_source are laid
2259 mreqs.imr_interface = mreqs.imr_sourceaddr;
2260 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
2261 } else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2262 error = sooptcopyin(sopt, &mreqs,
2263 sizeof(struct ip_mreq_source),
2264 sizeof(struct ip_mreq_source));
2269 gsa->sin.sin_family = AF_INET;
2270 gsa->sin.sin_len = sizeof(struct sockaddr_in);
2271 gsa->sin.sin_addr = mreqs.imr_multiaddr;
2273 if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2274 ssa->sin.sin_family = AF_INET;
2275 ssa->sin.sin_len = sizeof(struct sockaddr_in);
2276 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
2280 * Attempt to look up hinted ifp from interface address.
2281 * Fallthrough with null ifp iff lookup fails, to
2282 * preserve 4.4BSD mcast API idempotence.
2283 * XXX NOTE WELL: The RFC 3678 API is preferred because
2284 * using an IPv4 address as a key is racy.
2286 if (!in_nullhost(mreqs.imr_interface))
2287 INADDR_TO_IFP(mreqs.imr_interface, ifp);
2289 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
2290 __func__, inet_ntoa(mreqs.imr_interface), ifp);
2294 case MCAST_LEAVE_GROUP:
2295 case MCAST_LEAVE_SOURCE_GROUP:
2296 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2297 error = sooptcopyin(sopt, &gsr,
2298 sizeof(struct group_req),
2299 sizeof(struct group_req));
2300 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2301 error = sooptcopyin(sopt, &gsr,
2302 sizeof(struct group_source_req),
2303 sizeof(struct group_source_req));
2308 if (gsa->sin.sin_family != AF_INET ||
2309 gsa->sin.sin_len != sizeof(struct sockaddr_in))
2312 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2313 if (ssa->sin.sin_family != AF_INET ||
2314 ssa->sin.sin_len != sizeof(struct sockaddr_in))
2318 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
2319 return (EADDRNOTAVAIL);
2321 ifp = ifnet_byindex(gsr.gsr_interface);
2324 return (EADDRNOTAVAIL);
2328 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2329 __func__, sopt->sopt_name);
2330 return (EOPNOTSUPP);
2334 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2338 * Find the membership in the membership array.
2340 imo = inp_findmoptions(inp);
2341 idx = imo_match_group(imo, ifp, &gsa->sa);
2343 error = EADDRNOTAVAIL;
2344 goto out_inp_locked;
2346 inm = imo->imo_membership[idx];
2347 imf = &imo->imo_mfilters[idx];
2349 if (ssa->ss.ss_family != AF_UNSPEC)
2353 * Begin state merge transaction at socket layer.
2355 INP_WLOCK_ASSERT(inp);
2358 * If we were instructed only to leave a given source, do so.
2359 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2364 if (imf->imf_st[0] == MCAST_EXCLUDE) {
2365 error = EADDRNOTAVAIL;
2366 goto out_inp_locked;
2368 ims = imo_match_source(imo, idx, &ssa->sa);
2370 CTR3(KTR_IGMPV3, "%s: source %s %spresent", __func__,
2371 inet_ntoa(ssa->sin.sin_addr), "not ");
2372 error = EADDRNOTAVAIL;
2373 goto out_inp_locked;
2375 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
2376 error = imf_prune(imf, &ssa->sin);
2378 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2380 goto out_inp_locked;
2385 * Begin state merge transaction at IGMP layer.
2391 * Give up the multicast address record to which
2392 * the membership points.
2394 (void)in_leavegroup_locked(inm, imf);
2396 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2397 error = inm_merge(inm, imf);
2399 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2401 goto out_in_multi_locked;
2404 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2405 error = igmp_change_state(inm);
2407 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2412 out_in_multi_locked:
2424 /* Remove the gap in the membership and filter array. */
2425 for (++idx; idx < imo->imo_num_memberships; ++idx) {
2426 imo->imo_membership[idx-1] = imo->imo_membership[idx];
2427 imo->imo_mfilters[idx-1] = imo->imo_mfilters[idx];
2429 imo->imo_num_memberships--;
2438 * Select the interface for transmitting IPv4 multicast datagrams.
2440 * Either an instance of struct in_addr or an instance of struct ip_mreqn
2441 * may be passed to this socket option. An address of INADDR_ANY or an
2442 * interface index of 0 is used to remove a previous selection.
2443 * When no interface is selected, one is chosen for every send.
2446 inp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2448 struct in_addr addr;
2449 struct ip_mreqn mreqn;
2451 struct ip_moptions *imo;
2454 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
2456 * An interface index was specified using the
2457 * Linux-derived ip_mreqn structure.
2459 error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn),
2460 sizeof(struct ip_mreqn));
2464 if (mreqn.imr_ifindex < 0 || V_if_index < mreqn.imr_ifindex)
2467 if (mreqn.imr_ifindex == 0) {
2470 ifp = ifnet_byindex(mreqn.imr_ifindex);
2472 return (EADDRNOTAVAIL);
2476 * An interface was specified by IPv4 address.
2477 * This is the traditional BSD usage.
2479 error = sooptcopyin(sopt, &addr, sizeof(struct in_addr),
2480 sizeof(struct in_addr));
2483 if (in_nullhost(addr)) {
2486 INADDR_TO_IFP(addr, ifp);
2488 return (EADDRNOTAVAIL);
2490 CTR3(KTR_IGMPV3, "%s: ifp = %p, addr = %s", __func__, ifp,
2494 /* Reject interfaces which do not support multicast. */
2495 if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0)
2496 return (EOPNOTSUPP);
2498 imo = inp_findmoptions(inp);
2499 imo->imo_multicast_ifp = ifp;
2500 imo->imo_multicast_addr.s_addr = INADDR_ANY;
2507 * Atomically set source filters on a socket for an IPv4 multicast group.
2509 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
2512 inp_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2514 struct __msfilterreq msfr;
2517 struct in_mfilter *imf;
2518 struct ip_moptions *imo;
2519 struct in_multi *inm;
2523 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2524 sizeof(struct __msfilterreq));
2528 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
2531 if ((msfr.msfr_fmode != MCAST_EXCLUDE &&
2532 msfr.msfr_fmode != MCAST_INCLUDE))
2535 if (msfr.msfr_group.ss_family != AF_INET ||
2536 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in))
2539 gsa = (sockunion_t *)&msfr.msfr_group;
2540 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2543 gsa->sin.sin_port = 0; /* ignore port */
2545 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
2546 return (EADDRNOTAVAIL);
2548 ifp = ifnet_byindex(msfr.msfr_ifindex);
2550 return (EADDRNOTAVAIL);
2553 * Take the INP write lock.
2554 * Check if this socket is a member of this group.
2556 imo = inp_findmoptions(inp);
2557 idx = imo_match_group(imo, ifp, &gsa->sa);
2558 if (idx == -1 || imo->imo_mfilters == NULL) {
2559 error = EADDRNOTAVAIL;
2560 goto out_inp_locked;
2562 inm = imo->imo_membership[idx];
2563 imf = &imo->imo_mfilters[idx];
2566 * Begin state merge transaction at socket layer.
2568 INP_WLOCK_ASSERT(inp);
2570 imf->imf_st[1] = msfr.msfr_fmode;
2573 * Apply any new source filters, if present.
2574 * Make a copy of the user-space source vector so
2575 * that we may copy them with a single copyin. This
2576 * allows us to deal with page faults up-front.
2578 if (msfr.msfr_nsrcs > 0) {
2579 struct in_msource *lims;
2580 struct sockaddr_in *psin;
2581 struct sockaddr_storage *kss, *pkss;
2586 CTR2(KTR_IGMPV3, "%s: loading %lu source list entries",
2587 __func__, (unsigned long)msfr.msfr_nsrcs);
2588 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2590 error = copyin(msfr.msfr_srcs, kss,
2591 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2600 * Mark all source filters as UNDEFINED at t1.
2601 * Restore new group filter mode, as imf_leave()
2602 * will set it to INCLUDE.
2605 imf->imf_st[1] = msfr.msfr_fmode;
2608 * Update socket layer filters at t1, lazy-allocating
2609 * new entries. This saves a bunch of memory at the
2610 * cost of one RB_FIND() per source entry; duplicate
2611 * entries in the msfr_nsrcs vector are ignored.
2612 * If we encounter an error, rollback transaction.
2614 * XXX This too could be replaced with a set-symmetric
2615 * difference like loop to avoid walking from root
2616 * every time, as the key space is common.
2618 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2619 psin = (struct sockaddr_in *)pkss;
2620 if (psin->sin_family != AF_INET) {
2621 error = EAFNOSUPPORT;
2624 if (psin->sin_len != sizeof(struct sockaddr_in)) {
2628 error = imf_get_source(imf, psin, &lims);
2631 lims->imsl_st[1] = imf->imf_st[1];
2637 goto out_imf_rollback;
2639 INP_WLOCK_ASSERT(inp);
2643 * Begin state merge transaction at IGMP layer.
2645 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2646 error = inm_merge(inm, imf);
2648 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
2649 goto out_in_multi_locked;
2652 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2653 error = igmp_change_state(inm);
2655 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
2657 out_in_multi_locked:
2675 * Set the IP multicast options in response to user setsockopt().
2677 * Many of the socket options handled in this function duplicate the
2678 * functionality of socket options in the regular unicast API. However,
2679 * it is not possible to merge the duplicate code, because the idempotence
2680 * of the IPv4 multicast part of the BSD Sockets API must be preserved;
2681 * the effects of these options must be treated as separate and distinct.
2683 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2684 * FUTURE: The IP_MULTICAST_VIF option may be eliminated if MROUTING
2685 * is refactored to no longer use vifs.
2688 inp_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2690 struct ip_moptions *imo;
2696 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2697 * or is a divert socket, reject it.
2699 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
2700 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2701 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
2702 return (EOPNOTSUPP);
2704 switch (sopt->sopt_name) {
2705 case IP_MULTICAST_VIF: {
2708 * Select a multicast VIF for transmission.
2709 * Only useful if multicast forwarding is active.
2711 if (legal_vif_num == NULL) {
2715 error = sooptcopyin(sopt, &vifi, sizeof(int), sizeof(int));
2718 if (!legal_vif_num(vifi) && (vifi != -1)) {
2722 imo = inp_findmoptions(inp);
2723 imo->imo_multicast_vif = vifi;
2728 case IP_MULTICAST_IF:
2729 error = inp_set_multicast_if(inp, sopt);
2732 case IP_MULTICAST_TTL: {
2736 * Set the IP time-to-live for outgoing multicast packets.
2737 * The original multicast API required a char argument,
2738 * which is inconsistent with the rest of the socket API.
2739 * We allow either a char or an int.
2741 if (sopt->sopt_valsize == sizeof(u_char)) {
2742 error = sooptcopyin(sopt, &ttl, sizeof(u_char),
2749 error = sooptcopyin(sopt, &ittl, sizeof(u_int),
2759 imo = inp_findmoptions(inp);
2760 imo->imo_multicast_ttl = ttl;
2765 case IP_MULTICAST_LOOP: {
2769 * Set the loopback flag for outgoing multicast packets.
2770 * Must be zero or one. The original multicast API required a
2771 * char argument, which is inconsistent with the rest
2772 * of the socket API. We allow either a char or an int.
2774 if (sopt->sopt_valsize == sizeof(u_char)) {
2775 error = sooptcopyin(sopt, &loop, sizeof(u_char),
2782 error = sooptcopyin(sopt, &iloop, sizeof(u_int),
2786 loop = (u_char)iloop;
2788 imo = inp_findmoptions(inp);
2789 imo->imo_multicast_loop = !!loop;
2794 case IP_ADD_MEMBERSHIP:
2795 case IP_ADD_SOURCE_MEMBERSHIP:
2796 case MCAST_JOIN_GROUP:
2797 case MCAST_JOIN_SOURCE_GROUP:
2798 error = inp_join_group(inp, sopt);
2801 case IP_DROP_MEMBERSHIP:
2802 case IP_DROP_SOURCE_MEMBERSHIP:
2803 case MCAST_LEAVE_GROUP:
2804 case MCAST_LEAVE_SOURCE_GROUP:
2805 error = inp_leave_group(inp, sopt);
2808 case IP_BLOCK_SOURCE:
2809 case IP_UNBLOCK_SOURCE:
2810 case MCAST_BLOCK_SOURCE:
2811 case MCAST_UNBLOCK_SOURCE:
2812 error = inp_block_unblock_source(inp, sopt);
2816 error = inp_set_source_filters(inp, sopt);
2824 INP_UNLOCK_ASSERT(inp);
2830 * Expose IGMP's multicast filter mode and source list(s) to userland,
2831 * keyed by (ifindex, group).
2832 * The filter mode is written out as a uint32_t, followed by
2833 * 0..n of struct in_addr.
2834 * For use by ifmcstat(8).
2835 * SMPng: NOTE: unlocked read of ifindex space.
2838 sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS)
2840 struct in_addr src, group;
2842 struct ifmultiaddr *ifma;
2843 struct in_multi *inm;
2844 struct ip_msource *ims;
2848 uint32_t fmode, ifindex;
2853 if (req->newptr != NULL)
2860 if (ifindex <= 0 || ifindex > V_if_index) {
2861 CTR2(KTR_IGMPV3, "%s: ifindex %u out of range",
2866 group.s_addr = name[1];
2867 if (!IN_MULTICAST(ntohl(group.s_addr))) {
2868 CTR2(KTR_IGMPV3, "%s: group %s is not multicast",
2869 __func__, inet_ntoa(group));
2873 ifp = ifnet_byindex(ifindex);
2875 CTR2(KTR_IGMPV3, "%s: no ifp for ifindex %u",
2880 retval = sysctl_wire_old_buffer(req,
2881 sizeof(uint32_t) + (in_mcast_maxgrpsrc * sizeof(struct in_addr)));
2888 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2889 if (ifma->ifma_addr->sa_family != AF_INET ||
2890 ifma->ifma_protospec == NULL)
2892 inm = (struct in_multi *)ifma->ifma_protospec;
2893 if (!in_hosteq(inm->inm_addr, group))
2895 fmode = inm->inm_st[1].iss_fmode;
2896 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2899 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
2902 ina.s_addr = htonl(ims->ims_haddr);
2903 CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2907 * Only copy-out sources which are in-mode.
2909 if (fmode != ims_get_mode(inm, ims, 1)) {
2910 CTR1(KTR_IGMPV3, "%s: skip non-in-mode",
2914 src.s_addr = htonl(ims->ims_haddr);
2915 retval = SYSCTL_OUT(req, &src, sizeof(struct in_addr));
2920 IF_ADDR_RUNLOCK(ifp);
2927 #if defined(KTR) && (KTR_COMPILE & KTR_IGMPV3)
2929 static const char *inm_modestrs[] = { "un", "in", "ex" };
2932 inm_mode_str(const int mode)
2935 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2936 return (inm_modestrs[mode]);
2940 static const char *inm_statestrs[] = {
2953 inm_state_str(const int state)
2956 if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
2957 return (inm_statestrs[state]);
2962 * Dump an in_multi structure to the console.
2965 inm_print(const struct in_multi *inm)
2969 if ((ktr_mask & KTR_IGMPV3) == 0)
2972 printf("%s: --- begin inm %p ---\n", __func__, inm);
2973 printf("addr %s ifp %p(%s) ifma %p\n",
2974 inet_ntoa(inm->inm_addr),
2976 inm->inm_ifp->if_xname,
2978 printf("timer %u state %s refcount %u scq.len %u\n",
2980 inm_state_str(inm->inm_state),
2982 inm->inm_scq.mq_len);
2983 printf("igi %p nsrc %lu sctimer %u scrv %u\n",
2988 for (t = 0; t < 2; t++) {
2989 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
2990 inm_mode_str(inm->inm_st[t].iss_fmode),
2991 inm->inm_st[t].iss_asm,
2992 inm->inm_st[t].iss_ex,
2993 inm->inm_st[t].iss_in,
2994 inm->inm_st[t].iss_rec);
2996 printf("%s: --- end inm %p ---\n", __func__, inm);
2999 #else /* !KTR || !(KTR_COMPILE & KTR_IGMPV3) */
3002 inm_print(const struct in_multi *inm)
3007 #endif /* KTR && (KTR_COMPILE & KTR_IGMPV3) */
3009 RB_GENERATE(ip_msource_tree, ip_msource, ims_link, ip_msource_cmp);