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>
49 #include <sys/taskqueue.h>
53 #include <net/if_dl.h>
54 #include <net/route.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in_pcb.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/igmp_var.h>
65 #define KTR_IGMPV3 KTR_INET
68 #ifndef __SOCKUNION_DECLARED
70 struct sockaddr_storage ss;
72 struct sockaddr_dl sdl;
73 struct sockaddr_in sin;
75 typedef union sockunion sockunion_t;
76 #define __SOCKUNION_DECLARED
77 #endif /* __SOCKUNION_DECLARED */
79 static MALLOC_DEFINE(M_INMFILTER, "in_mfilter",
80 "IPv4 multicast PCB-layer source filter");
81 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "IPv4 multicast group");
82 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "IPv4 multicast options");
83 static MALLOC_DEFINE(M_IPMSOURCE, "ip_msource",
84 "IPv4 multicast IGMP-layer source filter");
88 * - Lock order is: Giant, INP_WLOCK, IN_MULTI_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
89 * - The IF_ADDR_LOCK is implicitly taken by inm_lookup() earlier, however
90 * it can be taken by code in net/if.c also.
91 * - ip_moptions and in_mfilter are covered by the INP_WLOCK.
93 * struct in_multi is covered by IN_MULTI_LOCK. There isn't strictly
94 * any need for in_multi itself to be virtualized -- it is bound to an ifp
95 * anyway no matter what happens.
97 struct mtx in_multi_mtx;
98 MTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF);
101 * Functions with non-static linkage defined in this file should be
102 * declared in in_var.h:
107 * in_joingroup_locked()
109 * in_leavegroup_locked()
115 * XXX: Both carp and pf need to use the legacy (*,G) KPIs in_addmulti()
118 static void imf_commit(struct in_mfilter *);
119 static int imf_get_source(struct in_mfilter *imf,
120 const struct sockaddr_in *psin,
121 struct in_msource **);
122 static struct in_msource *
123 imf_graft(struct in_mfilter *, const uint8_t,
124 const struct sockaddr_in *);
125 static void imf_leave(struct in_mfilter *);
126 static int imf_prune(struct in_mfilter *, const struct sockaddr_in *);
127 static void imf_purge(struct in_mfilter *);
128 static void imf_rollback(struct in_mfilter *);
129 static void imf_reap(struct in_mfilter *);
130 static int imo_grow(struct ip_moptions *);
131 static size_t imo_match_group(const struct ip_moptions *,
132 const struct ifnet *, const struct sockaddr *);
133 static struct in_msource *
134 imo_match_source(const struct ip_moptions *, const size_t,
135 const struct sockaddr *);
136 static void ims_merge(struct ip_msource *ims,
137 const struct in_msource *lims, const int rollback);
138 static int in_getmulti(struct ifnet *, const struct in_addr *,
140 static int inm_get_source(struct in_multi *inm, const in_addr_t haddr,
141 const int noalloc, struct ip_msource **pims);
142 static int inm_is_ifp_detached(const struct in_multi *);
143 static int inm_merge(struct in_multi *, /*const*/ struct in_mfilter *);
144 static void inm_purge(struct in_multi *);
145 static void inm_reap(struct in_multi *);
146 static struct ip_moptions *
147 inp_findmoptions(struct inpcb *);
148 static void inp_freemoptions_internal(struct ip_moptions *);
149 static void inp_gcmoptions(void *, int);
150 static int inp_get_source_filters(struct inpcb *, struct sockopt *);
151 static int inp_join_group(struct inpcb *, struct sockopt *);
152 static int inp_leave_group(struct inpcb *, struct sockopt *);
153 static struct ifnet *
154 inp_lookup_mcast_ifp(const struct inpcb *,
155 const struct sockaddr_in *, const struct in_addr);
156 static int inp_block_unblock_source(struct inpcb *, struct sockopt *);
157 static int inp_set_multicast_if(struct inpcb *, struct sockopt *);
158 static int inp_set_source_filters(struct inpcb *, struct sockopt *);
159 static int sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS);
161 static SYSCTL_NODE(_net_inet_ip, OID_AUTO, mcast, CTLFLAG_RW, 0,
164 static u_long in_mcast_maxgrpsrc = IP_MAX_GROUP_SRC_FILTER;
165 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxgrpsrc,
166 CTLFLAG_RW | CTLFLAG_TUN, &in_mcast_maxgrpsrc, 0,
167 "Max source filters per group");
168 TUNABLE_ULONG("net.inet.ip.mcast.maxgrpsrc", &in_mcast_maxgrpsrc);
170 static u_long in_mcast_maxsocksrc = IP_MAX_SOCK_SRC_FILTER;
171 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxsocksrc,
172 CTLFLAG_RW | CTLFLAG_TUN, &in_mcast_maxsocksrc, 0,
173 "Max source filters per socket");
174 TUNABLE_ULONG("net.inet.ip.mcast.maxsocksrc", &in_mcast_maxsocksrc);
176 int in_mcast_loop = IP_DEFAULT_MULTICAST_LOOP;
177 SYSCTL_INT(_net_inet_ip_mcast, OID_AUTO, loop, CTLFLAG_RW | CTLFLAG_TUN,
178 &in_mcast_loop, 0, "Loopback multicast datagrams by default");
179 TUNABLE_INT("net.inet.ip.mcast.loop", &in_mcast_loop);
181 static SYSCTL_NODE(_net_inet_ip_mcast, OID_AUTO, filters,
182 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip_mcast_filters,
183 "Per-interface stack-wide source filters");
185 static STAILQ_HEAD(, ip_moptions) imo_gc_list =
186 STAILQ_HEAD_INITIALIZER(imo_gc_list);
187 static struct task imo_gc_task = TASK_INITIALIZER(0, inp_gcmoptions, NULL);
190 * Inline function which wraps assertions for a valid ifp.
191 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
195 inm_is_ifp_detached(const struct in_multi *inm)
199 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
200 ifp = inm->inm_ifma->ifma_ifp;
203 * Sanity check that netinet's notion of ifp is the
206 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
209 return (ifp == NULL);
213 * Initialize an in_mfilter structure to a known state at t0, t1
214 * with an empty source filter list.
217 imf_init(struct in_mfilter *imf, const int st0, const int st1)
219 memset(imf, 0, sizeof(struct in_mfilter));
220 RB_INIT(&imf->imf_sources);
221 imf->imf_st[0] = st0;
222 imf->imf_st[1] = st1;
226 * Resize the ip_moptions vector to the next power-of-two minus 1.
227 * May be called with locks held; do not sleep.
230 imo_grow(struct ip_moptions *imo)
232 struct in_multi **nmships;
233 struct in_multi **omships;
234 struct in_mfilter *nmfilters;
235 struct in_mfilter *omfilters;
242 omships = imo->imo_membership;
243 omfilters = imo->imo_mfilters;
244 oldmax = imo->imo_max_memberships;
245 newmax = ((oldmax + 1) * 2) - 1;
247 if (newmax <= IP_MAX_MEMBERSHIPS) {
248 nmships = (struct in_multi **)realloc(omships,
249 sizeof(struct in_multi *) * newmax, M_IPMOPTS, M_NOWAIT);
250 nmfilters = (struct in_mfilter *)realloc(omfilters,
251 sizeof(struct in_mfilter) * newmax, M_INMFILTER, M_NOWAIT);
252 if (nmships != NULL && nmfilters != NULL) {
253 /* Initialize newly allocated source filter heads. */
254 for (idx = oldmax; idx < newmax; idx++) {
255 imf_init(&nmfilters[idx], MCAST_UNDEFINED,
258 imo->imo_max_memberships = newmax;
259 imo->imo_membership = nmships;
260 imo->imo_mfilters = nmfilters;
264 if (nmships == NULL || nmfilters == NULL) {
266 free(nmships, M_IPMOPTS);
267 if (nmfilters != NULL)
268 free(nmfilters, M_INMFILTER);
269 return (ETOOMANYREFS);
276 * Find an IPv4 multicast group entry for this ip_moptions instance
277 * which matches the specified group, and optionally an interface.
278 * Return its index into the array, or -1 if not found.
281 imo_match_group(const struct ip_moptions *imo, const struct ifnet *ifp,
282 const struct sockaddr *group)
284 const struct sockaddr_in *gsin;
285 struct in_multi **pinm;
289 gsin = (const struct sockaddr_in *)group;
291 /* The imo_membership array may be lazy allocated. */
292 if (imo->imo_membership == NULL || imo->imo_num_memberships == 0)
295 nmships = imo->imo_num_memberships;
296 pinm = &imo->imo_membership[0];
297 for (idx = 0; idx < nmships; idx++, pinm++) {
300 if ((ifp == NULL || ((*pinm)->inm_ifp == ifp)) &&
301 in_hosteq((*pinm)->inm_addr, gsin->sin_addr)) {
312 * Find an IPv4 multicast source entry for this imo which matches
313 * the given group index for this socket, and source address.
315 * NOTE: This does not check if the entry is in-mode, merely if
316 * it exists, which may not be the desired behaviour.
318 static struct in_msource *
319 imo_match_source(const struct ip_moptions *imo, const size_t gidx,
320 const struct sockaddr *src)
322 struct ip_msource find;
323 struct in_mfilter *imf;
324 struct ip_msource *ims;
325 const sockunion_t *psa;
327 KASSERT(src->sa_family == AF_INET, ("%s: !AF_INET", __func__));
328 KASSERT(gidx != -1 && gidx < imo->imo_num_memberships,
329 ("%s: invalid index %d\n", __func__, (int)gidx));
331 /* The imo_mfilters array may be lazy allocated. */
332 if (imo->imo_mfilters == NULL)
334 imf = &imo->imo_mfilters[gidx];
336 /* Source trees are keyed in host byte order. */
337 psa = (const sockunion_t *)src;
338 find.ims_haddr = ntohl(psa->sin.sin_addr.s_addr);
339 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
341 return ((struct in_msource *)ims);
345 * Perform filtering for multicast datagrams on a socket by group and source.
347 * Returns 0 if a datagram should be allowed through, or various error codes
348 * if the socket was not a member of the group, or the source was muted, etc.
351 imo_multi_filter(const struct ip_moptions *imo, const struct ifnet *ifp,
352 const struct sockaddr *group, const struct sockaddr *src)
355 struct in_msource *ims;
358 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
360 gidx = imo_match_group(imo, ifp, group);
362 return (MCAST_NOTGMEMBER);
365 * Check if the source was included in an (S,G) join.
366 * Allow reception on exclusive memberships by default,
367 * reject reception on inclusive memberships by default.
368 * Exclude source only if an in-mode exclude filter exists.
369 * Include source only if an in-mode include filter exists.
370 * NOTE: We are comparing group state here at IGMP t1 (now)
371 * with socket-layer t0 (since last downcall).
373 mode = imo->imo_mfilters[gidx].imf_st[1];
374 ims = imo_match_source(imo, gidx, src);
376 if ((ims == NULL && mode == MCAST_INCLUDE) ||
377 (ims != NULL && ims->imsl_st[0] != mode))
378 return (MCAST_NOTSMEMBER);
384 * Find and return a reference to an in_multi record for (ifp, group),
385 * and bump its reference count.
386 * If one does not exist, try to allocate it, and update link-layer multicast
387 * filters on ifp to listen for group.
388 * Assumes the IN_MULTI lock is held across the call.
389 * Return 0 if successful, otherwise return an appropriate error code.
392 in_getmulti(struct ifnet *ifp, const struct in_addr *group,
393 struct in_multi **pinm)
395 struct sockaddr_in gsin;
396 struct ifmultiaddr *ifma;
397 struct in_ifinfo *ii;
398 struct in_multi *inm;
401 IN_MULTI_LOCK_ASSERT();
403 ii = (struct in_ifinfo *)ifp->if_afdata[AF_INET];
405 inm = inm_lookup(ifp, *group);
408 * If we already joined this group, just bump the
409 * refcount and return it.
411 KASSERT(inm->inm_refcount >= 1,
412 ("%s: bad refcount %d", __func__, inm->inm_refcount));
418 memset(&gsin, 0, sizeof(gsin));
419 gsin.sin_family = AF_INET;
420 gsin.sin_len = sizeof(struct sockaddr_in);
421 gsin.sin_addr = *group;
424 * Check if a link-layer group is already associated
425 * with this network-layer group on the given ifnet.
427 error = if_addmulti(ifp, (struct sockaddr *)&gsin, &ifma);
431 /* XXX ifma_protospec must be covered by IF_ADDR_LOCK */
435 * If something other than netinet is occupying the link-layer
436 * group, print a meaningful error message and back out of
438 * Otherwise, bump the refcount on the existing network-layer
439 * group association and return it.
441 if (ifma->ifma_protospec != NULL) {
442 inm = (struct in_multi *)ifma->ifma_protospec;
444 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
446 KASSERT(ifma->ifma_addr->sa_family == AF_INET,
447 ("%s: ifma not AF_INET", __func__));
448 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
449 if (inm->inm_ifma != ifma || inm->inm_ifp != ifp ||
450 !in_hosteq(inm->inm_addr, *group))
451 panic("%s: ifma %p is inconsistent with %p (%s)",
452 __func__, ifma, inm, inet_ntoa(*group));
456 IF_ADDR_WUNLOCK(ifp);
460 IF_ADDR_WLOCK_ASSERT(ifp);
463 * A new in_multi record is needed; allocate and initialize it.
464 * We DO NOT perform an IGMP join as the in_ layer may need to
465 * push an initial source list down to IGMP to support SSM.
467 * The initial source filter state is INCLUDE, {} as per the RFC.
469 inm = malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT | M_ZERO);
471 if_delmulti_ifma(ifma);
472 IF_ADDR_WUNLOCK(ifp);
475 inm->inm_addr = *group;
477 inm->inm_igi = ii->ii_igmp;
478 inm->inm_ifma = ifma;
479 inm->inm_refcount = 1;
480 inm->inm_state = IGMP_NOT_MEMBER;
483 * Pending state-changes per group are subject to a bounds check.
485 IFQ_SET_MAXLEN(&inm->inm_scq, IGMP_MAX_STATE_CHANGES);
487 inm->inm_st[0].iss_fmode = MCAST_UNDEFINED;
488 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
489 RB_INIT(&inm->inm_srcs);
491 ifma->ifma_protospec = inm;
495 IF_ADDR_WUNLOCK(ifp);
500 * Drop a reference to an in_multi record.
502 * If the refcount drops to 0, free the in_multi record and
503 * delete the underlying link-layer membership.
506 inm_release_locked(struct in_multi *inm)
508 struct ifmultiaddr *ifma;
510 IN_MULTI_LOCK_ASSERT();
512 CTR2(KTR_IGMPV3, "%s: refcount is %d", __func__, inm->inm_refcount);
514 if (--inm->inm_refcount > 0) {
515 CTR2(KTR_IGMPV3, "%s: refcount is now %d", __func__,
520 CTR2(KTR_IGMPV3, "%s: freeing inm %p", __func__, inm);
522 ifma = inm->inm_ifma;
524 /* XXX this access is not covered by IF_ADDR_LOCK */
525 CTR2(KTR_IGMPV3, "%s: purging ifma %p", __func__, ifma);
526 KASSERT(ifma->ifma_protospec == inm,
527 ("%s: ifma_protospec != inm", __func__));
528 ifma->ifma_protospec = NULL;
532 free(inm, M_IPMADDR);
534 if_delmulti_ifma(ifma);
538 * Clear recorded source entries for a group.
539 * Used by the IGMP code. Caller must hold the IN_MULTI lock.
540 * FIXME: Should reap.
543 inm_clear_recorded(struct in_multi *inm)
545 struct ip_msource *ims;
547 IN_MULTI_LOCK_ASSERT();
549 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
552 --inm->inm_st[1].iss_rec;
555 KASSERT(inm->inm_st[1].iss_rec == 0,
556 ("%s: iss_rec %d not 0", __func__, inm->inm_st[1].iss_rec));
560 * Record a source as pending for a Source-Group IGMPv3 query.
561 * This lives here as it modifies the shared tree.
563 * inm is the group descriptor.
564 * naddr is the address of the source to record in network-byte order.
566 * If the net.inet.igmp.sgalloc sysctl is non-zero, we will
567 * lazy-allocate a source node in response to an SG query.
568 * Otherwise, no allocation is performed. This saves some memory
569 * with the trade-off that the source will not be reported to the
570 * router if joined in the window between the query response and
571 * the group actually being joined on the local host.
573 * VIMAGE: XXX: Currently the igmp_sgalloc feature has been removed.
574 * This turns off the allocation of a recorded source entry if
575 * the group has not been joined.
577 * Return 0 if the source didn't exist or was already marked as recorded.
578 * Return 1 if the source was marked as recorded by this function.
579 * Return <0 if any error occured (negated errno code).
582 inm_record_source(struct in_multi *inm, const in_addr_t naddr)
584 struct ip_msource find;
585 struct ip_msource *ims, *nims;
587 IN_MULTI_LOCK_ASSERT();
589 find.ims_haddr = ntohl(naddr);
590 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
591 if (ims && ims->ims_stp)
594 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
596 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
600 nims->ims_haddr = find.ims_haddr;
601 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
607 * Mark the source as recorded and update the recorded
611 ++inm->inm_st[1].iss_rec;
617 * Return a pointer to an in_msource owned by an in_mfilter,
618 * given its source address.
619 * Lazy-allocate if needed. If this is a new entry its filter state is
622 * imf is the filter set being modified.
623 * haddr is the source address in *host* byte-order.
625 * SMPng: May be called with locks held; malloc must not block.
628 imf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin,
629 struct in_msource **plims)
631 struct ip_msource find;
632 struct ip_msource *ims, *nims;
633 struct in_msource *lims;
640 /* key is host byte order */
641 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
642 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
643 lims = (struct in_msource *)ims;
645 if (imf->imf_nsrc == in_mcast_maxsocksrc)
647 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
651 lims = (struct in_msource *)nims;
652 lims->ims_haddr = find.ims_haddr;
653 lims->imsl_st[0] = MCAST_UNDEFINED;
654 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
664 * Graft a source entry into an existing socket-layer filter set,
665 * maintaining any required invariants and checking allocations.
667 * The source is marked as being in the new filter mode at t1.
669 * Return the pointer to the new node, otherwise return NULL.
671 static struct in_msource *
672 imf_graft(struct in_mfilter *imf, const uint8_t st1,
673 const struct sockaddr_in *psin)
675 struct ip_msource *nims;
676 struct in_msource *lims;
678 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
682 lims = (struct in_msource *)nims;
683 lims->ims_haddr = ntohl(psin->sin_addr.s_addr);
684 lims->imsl_st[0] = MCAST_UNDEFINED;
685 lims->imsl_st[1] = st1;
686 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
693 * Prune a source entry from an existing socket-layer filter set,
694 * maintaining any required invariants and checking allocations.
696 * The source is marked as being left at t1, it is not freed.
698 * Return 0 if no error occurred, otherwise return an errno value.
701 imf_prune(struct in_mfilter *imf, const struct sockaddr_in *psin)
703 struct ip_msource find;
704 struct ip_msource *ims;
705 struct in_msource *lims;
707 /* key is host byte order */
708 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
709 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
712 lims = (struct in_msource *)ims;
713 lims->imsl_st[1] = MCAST_UNDEFINED;
718 * Revert socket-layer filter set deltas at t1 to t0 state.
721 imf_rollback(struct in_mfilter *imf)
723 struct ip_msource *ims, *tims;
724 struct in_msource *lims;
726 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
727 lims = (struct in_msource *)ims;
728 if (lims->imsl_st[0] == lims->imsl_st[1]) {
729 /* no change at t1 */
731 } else if (lims->imsl_st[0] != MCAST_UNDEFINED) {
732 /* revert change to existing source at t1 */
733 lims->imsl_st[1] = lims->imsl_st[0];
735 /* revert source added t1 */
736 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
737 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
738 free(ims, M_INMFILTER);
742 imf->imf_st[1] = imf->imf_st[0];
746 * Mark socket-layer filter set as INCLUDE {} at t1.
749 imf_leave(struct in_mfilter *imf)
751 struct ip_msource *ims;
752 struct in_msource *lims;
754 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
755 lims = (struct in_msource *)ims;
756 lims->imsl_st[1] = MCAST_UNDEFINED;
758 imf->imf_st[1] = MCAST_INCLUDE;
762 * Mark socket-layer filter set deltas as committed.
765 imf_commit(struct in_mfilter *imf)
767 struct ip_msource *ims;
768 struct in_msource *lims;
770 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
771 lims = (struct in_msource *)ims;
772 lims->imsl_st[0] = lims->imsl_st[1];
774 imf->imf_st[0] = imf->imf_st[1];
778 * Reap unreferenced sources from socket-layer filter set.
781 imf_reap(struct in_mfilter *imf)
783 struct ip_msource *ims, *tims;
784 struct in_msource *lims;
786 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
787 lims = (struct in_msource *)ims;
788 if ((lims->imsl_st[0] == MCAST_UNDEFINED) &&
789 (lims->imsl_st[1] == MCAST_UNDEFINED)) {
790 CTR2(KTR_IGMPV3, "%s: free lims %p", __func__, ims);
791 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
792 free(ims, M_INMFILTER);
799 * Purge socket-layer filter set.
802 imf_purge(struct in_mfilter *imf)
804 struct ip_msource *ims, *tims;
806 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
807 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
808 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
809 free(ims, M_INMFILTER);
812 imf->imf_st[0] = imf->imf_st[1] = MCAST_UNDEFINED;
813 KASSERT(RB_EMPTY(&imf->imf_sources),
814 ("%s: imf_sources not empty", __func__));
818 * Look up a source filter entry for a multicast group.
820 * inm is the group descriptor to work with.
821 * haddr is the host-byte-order IPv4 address to look up.
822 * noalloc may be non-zero to suppress allocation of sources.
823 * *pims will be set to the address of the retrieved or allocated source.
825 * SMPng: NOTE: may be called with locks held.
826 * Return 0 if successful, otherwise return a non-zero error code.
829 inm_get_source(struct in_multi *inm, const in_addr_t haddr,
830 const int noalloc, struct ip_msource **pims)
832 struct ip_msource find;
833 struct ip_msource *ims, *nims;
838 find.ims_haddr = haddr;
839 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
840 if (ims == NULL && !noalloc) {
841 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
843 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
847 nims->ims_haddr = haddr;
848 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
852 ia.s_addr = htonl(haddr);
853 CTR3(KTR_IGMPV3, "%s: allocated %s as %p", __func__,
863 * Merge socket-layer source into IGMP-layer source.
864 * If rollback is non-zero, perform the inverse of the merge.
867 ims_merge(struct ip_msource *ims, const struct in_msource *lims,
870 int n = rollback ? -1 : 1;
874 ia.s_addr = htonl(ims->ims_haddr);
877 if (lims->imsl_st[0] == MCAST_EXCLUDE) {
878 CTR3(KTR_IGMPV3, "%s: t1 ex -= %d on %s",
879 __func__, n, inet_ntoa(ia));
880 ims->ims_st[1].ex -= n;
881 } else if (lims->imsl_st[0] == MCAST_INCLUDE) {
882 CTR3(KTR_IGMPV3, "%s: t1 in -= %d on %s",
883 __func__, n, inet_ntoa(ia));
884 ims->ims_st[1].in -= n;
887 if (lims->imsl_st[1] == MCAST_EXCLUDE) {
888 CTR3(KTR_IGMPV3, "%s: t1 ex += %d on %s",
889 __func__, n, inet_ntoa(ia));
890 ims->ims_st[1].ex += n;
891 } else if (lims->imsl_st[1] == MCAST_INCLUDE) {
892 CTR3(KTR_IGMPV3, "%s: t1 in += %d on %s",
893 __func__, n, inet_ntoa(ia));
894 ims->ims_st[1].in += n;
899 * Atomically update the global in_multi state, when a membership's
900 * filter list is being updated in any way.
902 * imf is the per-inpcb-membership group filter pointer.
903 * A fake imf may be passed for in-kernel consumers.
905 * XXX This is a candidate for a set-symmetric-difference style loop
906 * which would eliminate the repeated lookup from root of ims nodes,
907 * as they share the same key space.
909 * If any error occurred this function will back out of refcounts
910 * and return a non-zero value.
913 inm_merge(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
915 struct ip_msource *ims, *nims;
916 struct in_msource *lims;
925 * Update the source filters first, as this may fail.
926 * Maintain count of in-mode filters at t0, t1. These are
927 * used to work out if we transition into ASM mode or not.
928 * Maintain a count of source filters whose state was
929 * actually modified by this operation.
931 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
932 lims = (struct in_msource *)ims;
933 if (lims->imsl_st[0] == imf->imf_st[0]) nsrc0++;
934 if (lims->imsl_st[1] == imf->imf_st[1]) nsrc1++;
935 if (lims->imsl_st[0] == lims->imsl_st[1]) continue;
936 error = inm_get_source(inm, lims->ims_haddr, 0, &nims);
940 ims_merge(nims, lims, 0);
943 struct ip_msource *bims;
945 RB_FOREACH_REVERSE_FROM(ims, ip_msource_tree, nims) {
946 lims = (struct in_msource *)ims;
947 if (lims->imsl_st[0] == lims->imsl_st[1])
949 (void)inm_get_source(inm, lims->ims_haddr, 1, &bims);
952 ims_merge(bims, lims, 1);
957 CTR3(KTR_IGMPV3, "%s: imf filters in-mode: %d at t0, %d at t1",
958 __func__, nsrc0, nsrc1);
960 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
961 if (imf->imf_st[0] == imf->imf_st[1] &&
962 imf->imf_st[1] == MCAST_INCLUDE) {
964 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
965 --inm->inm_st[1].iss_in;
969 /* Handle filter mode transition on socket. */
970 if (imf->imf_st[0] != imf->imf_st[1]) {
971 CTR3(KTR_IGMPV3, "%s: imf transition %d to %d",
972 __func__, imf->imf_st[0], imf->imf_st[1]);
974 if (imf->imf_st[0] == MCAST_EXCLUDE) {
975 CTR1(KTR_IGMPV3, "%s: --ex on inm at t1", __func__);
976 --inm->inm_st[1].iss_ex;
977 } else if (imf->imf_st[0] == MCAST_INCLUDE) {
978 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
979 --inm->inm_st[1].iss_in;
982 if (imf->imf_st[1] == MCAST_EXCLUDE) {
983 CTR1(KTR_IGMPV3, "%s: ex++ on inm at t1", __func__);
984 inm->inm_st[1].iss_ex++;
985 } else if (imf->imf_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
986 CTR1(KTR_IGMPV3, "%s: in++ on inm at t1", __func__);
987 inm->inm_st[1].iss_in++;
992 * Track inm filter state in terms of listener counts.
993 * If there are any exclusive listeners, stack-wide
994 * membership is exclusive.
995 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
996 * If no listeners remain, state is undefined at t1,
997 * and the IGMP lifecycle for this group should finish.
999 if (inm->inm_st[1].iss_ex > 0) {
1000 CTR1(KTR_IGMPV3, "%s: transition to EX", __func__);
1001 inm->inm_st[1].iss_fmode = MCAST_EXCLUDE;
1002 } else if (inm->inm_st[1].iss_in > 0) {
1003 CTR1(KTR_IGMPV3, "%s: transition to IN", __func__);
1004 inm->inm_st[1].iss_fmode = MCAST_INCLUDE;
1006 CTR1(KTR_IGMPV3, "%s: transition to UNDEF", __func__);
1007 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
1010 /* Decrement ASM listener count on transition out of ASM mode. */
1011 if (imf->imf_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1012 if ((imf->imf_st[1] != MCAST_EXCLUDE) ||
1013 (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 > 0))
1014 CTR1(KTR_IGMPV3, "%s: --asm on inm at t1", __func__);
1015 --inm->inm_st[1].iss_asm;
1018 /* Increment ASM listener count on transition to ASM mode. */
1019 if (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1020 CTR1(KTR_IGMPV3, "%s: asm++ on inm at t1", __func__);
1021 inm->inm_st[1].iss_asm++;
1024 CTR3(KTR_IGMPV3, "%s: merged imf %p to inm %p", __func__, imf, inm);
1029 CTR1(KTR_IGMPV3, "%s: sources changed; reaping", __func__);
1036 * Mark an in_multi's filter set deltas as committed.
1037 * Called by IGMP after a state change has been enqueued.
1040 inm_commit(struct in_multi *inm)
1042 struct ip_msource *ims;
1044 CTR2(KTR_IGMPV3, "%s: commit inm %p", __func__, inm);
1045 CTR1(KTR_IGMPV3, "%s: pre commit:", __func__);
1048 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
1049 ims->ims_st[0] = ims->ims_st[1];
1051 inm->inm_st[0] = inm->inm_st[1];
1055 * Reap unreferenced nodes from an in_multi's filter set.
1058 inm_reap(struct in_multi *inm)
1060 struct ip_msource *ims, *tims;
1062 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1063 if (ims->ims_st[0].ex > 0 || ims->ims_st[0].in > 0 ||
1064 ims->ims_st[1].ex > 0 || ims->ims_st[1].in > 0 ||
1067 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1068 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1069 free(ims, M_IPMSOURCE);
1075 * Purge all source nodes from an in_multi's filter set.
1078 inm_purge(struct in_multi *inm)
1080 struct ip_msource *ims, *tims;
1082 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1083 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1084 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1085 free(ims, M_IPMSOURCE);
1091 * Join a multicast group; unlocked entry point.
1093 * SMPng: XXX: in_joingroup() is called from in_control() when Giant
1094 * is not held. Fortunately, ifp is unlikely to have been detached
1095 * at this point, so we assume it's OK to recurse.
1098 in_joingroup(struct ifnet *ifp, const struct in_addr *gina,
1099 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1104 error = in_joingroup_locked(ifp, gina, imf, pinm);
1111 * Join a multicast group; real entry point.
1113 * Only preserves atomicity at inm level.
1114 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1116 * If the IGMP downcall fails, the group is not joined, and an error
1120 in_joingroup_locked(struct ifnet *ifp, const struct in_addr *gina,
1121 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1123 struct in_mfilter timf;
1124 struct in_multi *inm;
1127 IN_MULTI_LOCK_ASSERT();
1129 CTR4(KTR_IGMPV3, "%s: join %s on %p(%s))", __func__,
1130 inet_ntoa(*gina), ifp, ifp->if_xname);
1136 * If no imf was specified (i.e. kernel consumer),
1137 * fake one up and assume it is an ASM join.
1140 imf_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1144 error = in_getmulti(ifp, gina, &inm);
1146 CTR1(KTR_IGMPV3, "%s: in_getmulti() failure", __func__);
1150 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1151 error = inm_merge(inm, imf);
1153 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1154 goto out_inm_release;
1157 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1158 error = igmp_change_state(inm);
1160 CTR1(KTR_IGMPV3, "%s: failed to update source", __func__);
1161 goto out_inm_release;
1166 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1167 inm_release_locked(inm);
1176 * Leave a multicast group; unlocked entry point.
1179 in_leavegroup(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1184 error = in_leavegroup_locked(inm, imf);
1191 * Leave a multicast group; real entry point.
1192 * All source filters will be expunged.
1194 * Only preserves atomicity at inm level.
1196 * Holding the write lock for the INP which contains imf
1197 * is highly advisable. We can't assert for it as imf does not
1198 * contain a back-pointer to the owning inp.
1200 * Note: This is not the same as inm_release(*) as this function also
1201 * makes a state change downcall into IGMP.
1204 in_leavegroup_locked(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1206 struct in_mfilter timf;
1211 IN_MULTI_LOCK_ASSERT();
1213 CTR5(KTR_IGMPV3, "%s: leave inm %p, %s/%s, imf %p", __func__,
1214 inm, inet_ntoa(inm->inm_addr),
1215 (inm_is_ifp_detached(inm) ? "null" : inm->inm_ifp->if_xname),
1219 * If no imf was specified (i.e. kernel consumer),
1220 * fake one up and assume it is an ASM join.
1223 imf_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1228 * Begin state merge transaction at IGMP layer.
1230 * As this particular invocation should not cause any memory
1231 * to be allocated, and there is no opportunity to roll back
1232 * the transaction, it MUST NOT fail.
1234 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1235 error = inm_merge(inm, imf);
1236 KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1238 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1239 error = igmp_change_state(inm);
1241 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1243 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1244 inm_release_locked(inm);
1249 /*#ifndef BURN_BRIDGES*/
1251 * Join an IPv4 multicast group in (*,G) exclusive mode.
1252 * The group must be a 224.0.0.0/24 link-scope group.
1253 * This KPI is for legacy kernel consumers only.
1256 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1258 struct in_multi *pinm;
1261 KASSERT(IN_LOCAL_GROUP(ntohl(ap->s_addr)),
1262 ("%s: %s not in 224.0.0.0/24", __func__, inet_ntoa(*ap)));
1264 error = in_joingroup(ifp, ap, NULL, &pinm);
1272 * Leave an IPv4 multicast group, assumed to be in exclusive (*,G) mode.
1273 * This KPI is for legacy kernel consumers only.
1276 in_delmulti(struct in_multi *inm)
1279 (void)in_leavegroup(inm, NULL);
1284 * Block or unblock an ASM multicast source on an inpcb.
1285 * This implements the delta-based API described in RFC 3678.
1287 * The delta-based API applies only to exclusive-mode memberships.
1288 * An IGMP downcall will be performed.
1290 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1292 * Return 0 if successful, otherwise return an appropriate error code.
1295 inp_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1297 struct group_source_req gsr;
1298 sockunion_t *gsa, *ssa;
1300 struct in_mfilter *imf;
1301 struct ip_moptions *imo;
1302 struct in_msource *ims;
1303 struct in_multi *inm;
1312 memset(&gsr, 0, sizeof(struct group_source_req));
1313 gsa = (sockunion_t *)&gsr.gsr_group;
1314 ssa = (sockunion_t *)&gsr.gsr_source;
1316 switch (sopt->sopt_name) {
1317 case IP_BLOCK_SOURCE:
1318 case IP_UNBLOCK_SOURCE: {
1319 struct ip_mreq_source mreqs;
1321 error = sooptcopyin(sopt, &mreqs,
1322 sizeof(struct ip_mreq_source),
1323 sizeof(struct ip_mreq_source));
1327 gsa->sin.sin_family = AF_INET;
1328 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1329 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1331 ssa->sin.sin_family = AF_INET;
1332 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1333 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1335 if (!in_nullhost(mreqs.imr_interface))
1336 INADDR_TO_IFP(mreqs.imr_interface, ifp);
1338 if (sopt->sopt_name == IP_BLOCK_SOURCE)
1341 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
1342 __func__, inet_ntoa(mreqs.imr_interface), ifp);
1346 case MCAST_BLOCK_SOURCE:
1347 case MCAST_UNBLOCK_SOURCE:
1348 error = sooptcopyin(sopt, &gsr,
1349 sizeof(struct group_source_req),
1350 sizeof(struct group_source_req));
1354 if (gsa->sin.sin_family != AF_INET ||
1355 gsa->sin.sin_len != sizeof(struct sockaddr_in))
1358 if (ssa->sin.sin_family != AF_INET ||
1359 ssa->sin.sin_len != sizeof(struct sockaddr_in))
1362 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1363 return (EADDRNOTAVAIL);
1365 ifp = ifnet_byindex(gsr.gsr_interface);
1367 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1372 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
1373 __func__, sopt->sopt_name);
1374 return (EOPNOTSUPP);
1378 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1382 * Check if we are actually a member of this group.
1384 imo = inp_findmoptions(inp);
1385 idx = imo_match_group(imo, ifp, &gsa->sa);
1386 if (idx == -1 || imo->imo_mfilters == NULL) {
1387 error = EADDRNOTAVAIL;
1388 goto out_inp_locked;
1391 KASSERT(imo->imo_mfilters != NULL,
1392 ("%s: imo_mfilters not allocated", __func__));
1393 imf = &imo->imo_mfilters[idx];
1394 inm = imo->imo_membership[idx];
1397 * Attempting to use the delta-based API on an
1398 * non exclusive-mode membership is an error.
1400 fmode = imf->imf_st[0];
1401 if (fmode != MCAST_EXCLUDE) {
1403 goto out_inp_locked;
1407 * Deal with error cases up-front:
1408 * Asked to block, but already blocked; or
1409 * Asked to unblock, but nothing to unblock.
1410 * If adding a new block entry, allocate it.
1412 ims = imo_match_source(imo, idx, &ssa->sa);
1413 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1414 CTR3(KTR_IGMPV3, "%s: source %s %spresent", __func__,
1415 inet_ntoa(ssa->sin.sin_addr), doblock ? "" : "not ");
1416 error = EADDRNOTAVAIL;
1417 goto out_inp_locked;
1420 INP_WLOCK_ASSERT(inp);
1423 * Begin state merge transaction at socket layer.
1426 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
1427 ims = imf_graft(imf, fmode, &ssa->sin);
1431 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
1432 error = imf_prune(imf, &ssa->sin);
1436 CTR1(KTR_IGMPV3, "%s: merge imf state failed", __func__);
1437 goto out_imf_rollback;
1441 * Begin state merge transaction at IGMP layer.
1445 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1446 error = inm_merge(inm, imf);
1448 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1449 goto out_imf_rollback;
1452 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1453 error = igmp_change_state(inm);
1455 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1473 * Given an inpcb, return its multicast options structure pointer. Accepts
1474 * an unlocked inpcb pointer, but will return it locked. May sleep.
1476 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1477 * SMPng: NOTE: Returns with the INP write lock held.
1479 static struct ip_moptions *
1480 inp_findmoptions(struct inpcb *inp)
1482 struct ip_moptions *imo;
1483 struct in_multi **immp;
1484 struct in_mfilter *imfp;
1488 if (inp->inp_moptions != NULL)
1489 return (inp->inp_moptions);
1493 imo = malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK);
1494 immp = malloc(sizeof(*immp) * IP_MIN_MEMBERSHIPS, M_IPMOPTS,
1496 imfp = malloc(sizeof(struct in_mfilter) * IP_MIN_MEMBERSHIPS,
1497 M_INMFILTER, M_WAITOK);
1499 imo->imo_multicast_ifp = NULL;
1500 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1501 imo->imo_multicast_vif = -1;
1502 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1503 imo->imo_multicast_loop = in_mcast_loop;
1504 imo->imo_num_memberships = 0;
1505 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1506 imo->imo_membership = immp;
1508 /* Initialize per-group source filters. */
1509 for (idx = 0; idx < IP_MIN_MEMBERSHIPS; idx++)
1510 imf_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE);
1511 imo->imo_mfilters = imfp;
1514 if (inp->inp_moptions != NULL) {
1515 free(imfp, M_INMFILTER);
1516 free(immp, M_IPMOPTS);
1517 free(imo, M_IPMOPTS);
1518 return (inp->inp_moptions);
1520 inp->inp_moptions = imo;
1525 * Discard the IP multicast options (and source filters). To minimize
1526 * the amount of work done while holding locks such as the INP's
1527 * pcbinfo lock (which is used in the receive path), the free
1528 * operation is performed asynchronously in a separate task.
1530 * SMPng: NOTE: assumes INP write lock is held.
1533 inp_freemoptions(struct ip_moptions *imo)
1536 KASSERT(imo != NULL, ("%s: ip_moptions is NULL", __func__));
1538 STAILQ_INSERT_TAIL(&imo_gc_list, imo, imo_link);
1540 taskqueue_enqueue(taskqueue_thread, &imo_gc_task);
1544 inp_freemoptions_internal(struct ip_moptions *imo)
1546 struct in_mfilter *imf;
1547 size_t idx, nmships;
1549 nmships = imo->imo_num_memberships;
1550 for (idx = 0; idx < nmships; ++idx) {
1551 imf = imo->imo_mfilters ? &imo->imo_mfilters[idx] : NULL;
1554 (void)in_leavegroup(imo->imo_membership[idx], imf);
1559 if (imo->imo_mfilters)
1560 free(imo->imo_mfilters, M_INMFILTER);
1561 free(imo->imo_membership, M_IPMOPTS);
1562 free(imo, M_IPMOPTS);
1566 inp_gcmoptions(void *context, int pending)
1568 struct ip_moptions *imo;
1571 while (!STAILQ_EMPTY(&imo_gc_list)) {
1572 imo = STAILQ_FIRST(&imo_gc_list);
1573 STAILQ_REMOVE_HEAD(&imo_gc_list, imo_link);
1575 inp_freemoptions_internal(imo);
1582 * Atomically get source filters on a socket for an IPv4 multicast group.
1583 * Called with INP lock held; returns with lock released.
1586 inp_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1588 struct __msfilterreq msfr;
1591 struct ip_moptions *imo;
1592 struct in_mfilter *imf;
1593 struct ip_msource *ims;
1594 struct in_msource *lims;
1595 struct sockaddr_in *psin;
1596 struct sockaddr_storage *ptss;
1597 struct sockaddr_storage *tss;
1599 size_t idx, nsrcs, ncsrcs;
1601 INP_WLOCK_ASSERT(inp);
1603 imo = inp->inp_moptions;
1604 KASSERT(imo != NULL, ("%s: null ip_moptions", __func__));
1608 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1609 sizeof(struct __msfilterreq));
1613 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
1616 ifp = ifnet_byindex(msfr.msfr_ifindex);
1623 * Lookup group on the socket.
1625 gsa = (sockunion_t *)&msfr.msfr_group;
1626 idx = imo_match_group(imo, ifp, &gsa->sa);
1627 if (idx == -1 || imo->imo_mfilters == NULL) {
1629 return (EADDRNOTAVAIL);
1631 imf = &imo->imo_mfilters[idx];
1634 * Ignore memberships which are in limbo.
1636 if (imf->imf_st[1] == MCAST_UNDEFINED) {
1640 msfr.msfr_fmode = imf->imf_st[1];
1643 * If the user specified a buffer, copy out the source filter
1644 * entries to userland gracefully.
1645 * We only copy out the number of entries which userland
1646 * has asked for, but we always tell userland how big the
1647 * buffer really needs to be.
1650 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1651 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1652 M_TEMP, M_NOWAIT | M_ZERO);
1660 * Count number of sources in-mode at t0.
1661 * If buffer space exists and remains, copy out source entries.
1663 nsrcs = msfr.msfr_nsrcs;
1666 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
1667 lims = (struct in_msource *)ims;
1668 if (lims->imsl_st[0] == MCAST_UNDEFINED ||
1669 lims->imsl_st[0] != imf->imf_st[0])
1672 if (tss != NULL && nsrcs > 0) {
1673 psin = (struct sockaddr_in *)ptss;
1674 psin->sin_family = AF_INET;
1675 psin->sin_len = sizeof(struct sockaddr_in);
1676 psin->sin_addr.s_addr = htonl(lims->ims_haddr);
1686 error = copyout(tss, msfr.msfr_srcs,
1687 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1693 msfr.msfr_nsrcs = ncsrcs;
1694 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1700 * Return the IP multicast options in response to user getsockopt().
1703 inp_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1705 struct ip_mreqn mreqn;
1706 struct ip_moptions *imo;
1708 struct in_ifaddr *ia;
1713 imo = inp->inp_moptions;
1715 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1716 * or is a divert socket, reject it.
1718 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1719 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1720 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
1722 return (EOPNOTSUPP);
1726 switch (sopt->sopt_name) {
1727 case IP_MULTICAST_VIF:
1729 optval = imo->imo_multicast_vif;
1733 error = sooptcopyout(sopt, &optval, sizeof(int));
1736 case IP_MULTICAST_IF:
1737 memset(&mreqn, 0, sizeof(struct ip_mreqn));
1739 ifp = imo->imo_multicast_ifp;
1740 if (!in_nullhost(imo->imo_multicast_addr)) {
1741 mreqn.imr_address = imo->imo_multicast_addr;
1742 } else if (ifp != NULL) {
1743 mreqn.imr_ifindex = ifp->if_index;
1747 IA_SIN(ia)->sin_addr;
1748 ifa_free(&ia->ia_ifa);
1753 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
1754 error = sooptcopyout(sopt, &mreqn,
1755 sizeof(struct ip_mreqn));
1757 error = sooptcopyout(sopt, &mreqn.imr_address,
1758 sizeof(struct in_addr));
1762 case IP_MULTICAST_TTL:
1764 optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1766 optval = coptval = imo->imo_multicast_ttl;
1768 if (sopt->sopt_valsize == sizeof(u_char))
1769 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1771 error = sooptcopyout(sopt, &optval, sizeof(int));
1774 case IP_MULTICAST_LOOP:
1776 optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1778 optval = coptval = imo->imo_multicast_loop;
1780 if (sopt->sopt_valsize == sizeof(u_char))
1781 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1783 error = sooptcopyout(sopt, &optval, sizeof(int));
1788 error = EADDRNOTAVAIL;
1791 error = inp_get_source_filters(inp, sopt);
1797 error = ENOPROTOOPT;
1801 INP_UNLOCK_ASSERT(inp);
1807 * Look up the ifnet to use for a multicast group membership,
1808 * given the IPv4 address of an interface, and the IPv4 group address.
1810 * This routine exists to support legacy multicast applications
1811 * which do not understand that multicast memberships are scoped to
1812 * specific physical links in the networking stack, or which need
1813 * to join link-scope groups before IPv4 addresses are configured.
1815 * If inp is non-NULL, use this socket's current FIB number for any
1816 * required FIB lookup.
1817 * If ina is INADDR_ANY, look up the group address in the unicast FIB,
1818 * and use its ifp; usually, this points to the default next-hop.
1820 * If the FIB lookup fails, attempt to use the first non-loopback
1821 * interface with multicast capability in the system as a
1822 * last resort. The legacy IPv4 ASM API requires that we do
1823 * this in order to allow groups to be joined when the routing
1824 * table has not yet been populated during boot.
1826 * Returns NULL if no ifp could be found.
1828 * SMPng: TODO: Acquire the appropriate locks for INADDR_TO_IFP.
1829 * FUTURE: Implement IPv4 source-address selection.
1831 static struct ifnet *
1832 inp_lookup_mcast_ifp(const struct inpcb *inp,
1833 const struct sockaddr_in *gsin, const struct in_addr ina)
1837 KASSERT(gsin->sin_family == AF_INET, ("%s: not AF_INET", __func__));
1838 KASSERT(IN_MULTICAST(ntohl(gsin->sin_addr.s_addr)),
1839 ("%s: not multicast", __func__));
1842 if (!in_nullhost(ina)) {
1843 INADDR_TO_IFP(ina, ifp);
1848 memcpy(&ro.ro_dst, gsin, sizeof(struct sockaddr_in));
1849 in_rtalloc_ign(&ro, 0, inp ? inp->inp_inc.inc_fibnum : 0);
1850 if (ro.ro_rt != NULL) {
1851 ifp = ro.ro_rt->rt_ifp;
1852 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
1855 struct in_ifaddr *ia;
1860 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1862 if (!(mifp->if_flags & IFF_LOOPBACK) &&
1863 (mifp->if_flags & IFF_MULTICAST)) {
1868 IN_IFADDR_RUNLOCK();
1876 * Join an IPv4 multicast group, possibly with a source.
1879 inp_join_group(struct inpcb *inp, struct sockopt *sopt)
1881 struct group_source_req gsr;
1882 sockunion_t *gsa, *ssa;
1884 struct in_mfilter *imf;
1885 struct ip_moptions *imo;
1886 struct in_multi *inm;
1887 struct in_msource *lims;
1897 memset(&gsr, 0, sizeof(struct group_source_req));
1898 gsa = (sockunion_t *)&gsr.gsr_group;
1899 gsa->ss.ss_family = AF_UNSPEC;
1900 ssa = (sockunion_t *)&gsr.gsr_source;
1901 ssa->ss.ss_family = AF_UNSPEC;
1903 switch (sopt->sopt_name) {
1904 case IP_ADD_MEMBERSHIP:
1905 case IP_ADD_SOURCE_MEMBERSHIP: {
1906 struct ip_mreq_source mreqs;
1908 if (sopt->sopt_name == IP_ADD_MEMBERSHIP) {
1909 error = sooptcopyin(sopt, &mreqs,
1910 sizeof(struct ip_mreq),
1911 sizeof(struct ip_mreq));
1913 * Do argument switcharoo from ip_mreq into
1914 * ip_mreq_source to avoid using two instances.
1916 mreqs.imr_interface = mreqs.imr_sourceaddr;
1917 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
1918 } else if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
1919 error = sooptcopyin(sopt, &mreqs,
1920 sizeof(struct ip_mreq_source),
1921 sizeof(struct ip_mreq_source));
1926 gsa->sin.sin_family = AF_INET;
1927 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1928 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1930 if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
1931 ssa->sin.sin_family = AF_INET;
1932 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1933 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1936 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1939 ifp = inp_lookup_mcast_ifp(inp, &gsa->sin,
1940 mreqs.imr_interface);
1941 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
1942 __func__, inet_ntoa(mreqs.imr_interface), ifp);
1946 case MCAST_JOIN_GROUP:
1947 case MCAST_JOIN_SOURCE_GROUP:
1948 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1949 error = sooptcopyin(sopt, &gsr,
1950 sizeof(struct group_req),
1951 sizeof(struct group_req));
1952 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1953 error = sooptcopyin(sopt, &gsr,
1954 sizeof(struct group_source_req),
1955 sizeof(struct group_source_req));
1960 if (gsa->sin.sin_family != AF_INET ||
1961 gsa->sin.sin_len != sizeof(struct sockaddr_in))
1965 * Overwrite the port field if present, as the sockaddr
1966 * being copied in may be matched with a binary comparison.
1968 gsa->sin.sin_port = 0;
1969 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1970 if (ssa->sin.sin_family != AF_INET ||
1971 ssa->sin.sin_len != sizeof(struct sockaddr_in))
1973 ssa->sin.sin_port = 0;
1976 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1979 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1980 return (EADDRNOTAVAIL);
1981 ifp = ifnet_byindex(gsr.gsr_interface);
1985 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
1986 __func__, sopt->sopt_name);
1987 return (EOPNOTSUPP);
1991 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
1992 return (EADDRNOTAVAIL);
1994 imo = inp_findmoptions(inp);
1995 idx = imo_match_group(imo, ifp, &gsa->sa);
1999 inm = imo->imo_membership[idx];
2000 imf = &imo->imo_mfilters[idx];
2001 if (ssa->ss.ss_family != AF_UNSPEC) {
2003 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2004 * is an error. On an existing inclusive membership,
2005 * it just adds the source to the filter list.
2007 if (imf->imf_st[1] != MCAST_INCLUDE) {
2009 goto out_inp_locked;
2012 * Throw out duplicates.
2014 * XXX FIXME: This makes a naive assumption that
2015 * even if entries exist for *ssa in this imf,
2016 * they will be rejected as dupes, even if they
2017 * are not valid in the current mode (in-mode).
2019 * in_msource is transactioned just as for anything
2020 * else in SSM -- but note naive use of inm_graft()
2021 * below for allocating new filter entries.
2023 * This is only an issue if someone mixes the
2024 * full-state SSM API with the delta-based API,
2025 * which is discouraged in the relevant RFCs.
2027 lims = imo_match_source(imo, idx, &ssa->sa);
2028 if (lims != NULL /*&&
2029 lims->imsl_st[1] == MCAST_INCLUDE*/) {
2030 error = EADDRNOTAVAIL;
2031 goto out_inp_locked;
2035 * MCAST_JOIN_GROUP on an existing exclusive
2036 * membership is an error; return EADDRINUSE
2037 * to preserve 4.4BSD API idempotence, and
2038 * avoid tedious detour to code below.
2039 * NOTE: This is bending RFC 3678 a bit.
2041 * On an existing inclusive membership, this is also
2042 * an error; if you want to change filter mode,
2043 * you must use the userland API setsourcefilter().
2044 * XXX We don't reject this for imf in UNDEFINED
2045 * state at t1, because allocation of a filter
2046 * is atomic with allocation of a membership.
2049 if (imf->imf_st[1] == MCAST_EXCLUDE)
2051 goto out_inp_locked;
2056 * Begin state merge transaction at socket layer.
2058 INP_WLOCK_ASSERT(inp);
2061 if (imo->imo_num_memberships == imo->imo_max_memberships) {
2062 error = imo_grow(imo);
2064 goto out_inp_locked;
2067 * Allocate the new slot upfront so we can deal with
2068 * grafting the new source filter in same code path
2069 * as for join-source on existing membership.
2071 idx = imo->imo_num_memberships;
2072 imo->imo_membership[idx] = NULL;
2073 imo->imo_num_memberships++;
2074 KASSERT(imo->imo_mfilters != NULL,
2075 ("%s: imf_mfilters vector was not allocated", __func__));
2076 imf = &imo->imo_mfilters[idx];
2077 KASSERT(RB_EMPTY(&imf->imf_sources),
2078 ("%s: imf_sources not empty", __func__));
2082 * Graft new source into filter list for this inpcb's
2083 * membership of the group. The in_multi may not have
2084 * been allocated yet if this is a new membership, however,
2085 * the in_mfilter slot will be allocated and must be initialized.
2087 * Note: Grafting of exclusive mode filters doesn't happen
2089 * XXX: Should check for non-NULL lims (node exists but may
2090 * not be in-mode) for interop with full-state API.
2092 if (ssa->ss.ss_family != AF_UNSPEC) {
2093 /* Membership starts in IN mode */
2095 CTR1(KTR_IGMPV3, "%s: new join w/source", __func__);
2096 imf_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE);
2098 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
2100 lims = imf_graft(imf, MCAST_INCLUDE, &ssa->sin);
2102 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2108 /* No address specified; Membership starts in EX mode */
2110 CTR1(KTR_IGMPV3, "%s: new join w/o source", __func__);
2111 imf_init(imf, MCAST_UNDEFINED, MCAST_EXCLUDE);
2116 * Begin state merge transaction at IGMP layer.
2121 error = in_joingroup_locked(ifp, &gsa->sin.sin_addr, imf,
2125 imo->imo_membership[idx] = inm;
2127 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2128 error = inm_merge(inm, imf);
2130 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2132 goto out_imf_rollback;
2134 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2135 error = igmp_change_state(inm);
2137 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2139 goto out_imf_rollback;
2146 INP_WLOCK_ASSERT(inp);
2158 if (error && is_new) {
2159 imo->imo_membership[idx] = NULL;
2160 --imo->imo_num_memberships;
2169 * Leave an IPv4 multicast group on an inpcb, possibly with a source.
2172 inp_leave_group(struct inpcb *inp, struct sockopt *sopt)
2174 struct group_source_req gsr;
2175 struct ip_mreq_source mreqs;
2176 sockunion_t *gsa, *ssa;
2178 struct in_mfilter *imf;
2179 struct ip_moptions *imo;
2180 struct in_msource *ims;
2181 struct in_multi *inm;
2183 int error, is_final;
2189 memset(&gsr, 0, sizeof(struct group_source_req));
2190 gsa = (sockunion_t *)&gsr.gsr_group;
2191 gsa->ss.ss_family = AF_UNSPEC;
2192 ssa = (sockunion_t *)&gsr.gsr_source;
2193 ssa->ss.ss_family = AF_UNSPEC;
2195 switch (sopt->sopt_name) {
2196 case IP_DROP_MEMBERSHIP:
2197 case IP_DROP_SOURCE_MEMBERSHIP:
2198 if (sopt->sopt_name == IP_DROP_MEMBERSHIP) {
2199 error = sooptcopyin(sopt, &mreqs,
2200 sizeof(struct ip_mreq),
2201 sizeof(struct ip_mreq));
2203 * Swap interface and sourceaddr arguments,
2204 * as ip_mreq and ip_mreq_source are laid
2207 mreqs.imr_interface = mreqs.imr_sourceaddr;
2208 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
2209 } else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2210 error = sooptcopyin(sopt, &mreqs,
2211 sizeof(struct ip_mreq_source),
2212 sizeof(struct ip_mreq_source));
2217 gsa->sin.sin_family = AF_INET;
2218 gsa->sin.sin_len = sizeof(struct sockaddr_in);
2219 gsa->sin.sin_addr = mreqs.imr_multiaddr;
2221 if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2222 ssa->sin.sin_family = AF_INET;
2223 ssa->sin.sin_len = sizeof(struct sockaddr_in);
2224 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
2228 * Attempt to look up hinted ifp from interface address.
2229 * Fallthrough with null ifp iff lookup fails, to
2230 * preserve 4.4BSD mcast API idempotence.
2231 * XXX NOTE WELL: The RFC 3678 API is preferred because
2232 * using an IPv4 address as a key is racy.
2234 if (!in_nullhost(mreqs.imr_interface))
2235 INADDR_TO_IFP(mreqs.imr_interface, ifp);
2237 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
2238 __func__, inet_ntoa(mreqs.imr_interface), ifp);
2242 case MCAST_LEAVE_GROUP:
2243 case MCAST_LEAVE_SOURCE_GROUP:
2244 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2245 error = sooptcopyin(sopt, &gsr,
2246 sizeof(struct group_req),
2247 sizeof(struct group_req));
2248 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2249 error = sooptcopyin(sopt, &gsr,
2250 sizeof(struct group_source_req),
2251 sizeof(struct group_source_req));
2256 if (gsa->sin.sin_family != AF_INET ||
2257 gsa->sin.sin_len != sizeof(struct sockaddr_in))
2260 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2261 if (ssa->sin.sin_family != AF_INET ||
2262 ssa->sin.sin_len != sizeof(struct sockaddr_in))
2266 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
2267 return (EADDRNOTAVAIL);
2269 ifp = ifnet_byindex(gsr.gsr_interface);
2272 return (EADDRNOTAVAIL);
2276 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2277 __func__, sopt->sopt_name);
2278 return (EOPNOTSUPP);
2282 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2286 * Find the membership in the membership array.
2288 imo = inp_findmoptions(inp);
2289 idx = imo_match_group(imo, ifp, &gsa->sa);
2291 error = EADDRNOTAVAIL;
2292 goto out_inp_locked;
2294 inm = imo->imo_membership[idx];
2295 imf = &imo->imo_mfilters[idx];
2297 if (ssa->ss.ss_family != AF_UNSPEC)
2301 * Begin state merge transaction at socket layer.
2303 INP_WLOCK_ASSERT(inp);
2306 * If we were instructed only to leave a given source, do so.
2307 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2312 if (imf->imf_st[0] == MCAST_EXCLUDE) {
2313 error = EADDRNOTAVAIL;
2314 goto out_inp_locked;
2316 ims = imo_match_source(imo, idx, &ssa->sa);
2318 CTR3(KTR_IGMPV3, "%s: source %s %spresent", __func__,
2319 inet_ntoa(ssa->sin.sin_addr), "not ");
2320 error = EADDRNOTAVAIL;
2321 goto out_inp_locked;
2323 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
2324 error = imf_prune(imf, &ssa->sin);
2326 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2328 goto out_inp_locked;
2333 * Begin state merge transaction at IGMP layer.
2339 * Give up the multicast address record to which
2340 * the membership points.
2342 (void)in_leavegroup_locked(inm, imf);
2344 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2345 error = inm_merge(inm, imf);
2347 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2349 goto out_imf_rollback;
2352 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2353 error = igmp_change_state(inm);
2355 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2371 /* Remove the gap in the membership and filter array. */
2372 for (++idx; idx < imo->imo_num_memberships; ++idx) {
2373 imo->imo_membership[idx-1] = imo->imo_membership[idx];
2374 imo->imo_mfilters[idx-1] = imo->imo_mfilters[idx];
2376 imo->imo_num_memberships--;
2385 * Select the interface for transmitting IPv4 multicast datagrams.
2387 * Either an instance of struct in_addr or an instance of struct ip_mreqn
2388 * may be passed to this socket option. An address of INADDR_ANY or an
2389 * interface index of 0 is used to remove a previous selection.
2390 * When no interface is selected, one is chosen for every send.
2393 inp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2395 struct in_addr addr;
2396 struct ip_mreqn mreqn;
2398 struct ip_moptions *imo;
2401 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
2403 * An interface index was specified using the
2404 * Linux-derived ip_mreqn structure.
2406 error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn),
2407 sizeof(struct ip_mreqn));
2411 if (mreqn.imr_ifindex < 0 || V_if_index < mreqn.imr_ifindex)
2414 if (mreqn.imr_ifindex == 0) {
2417 ifp = ifnet_byindex(mreqn.imr_ifindex);
2419 return (EADDRNOTAVAIL);
2423 * An interface was specified by IPv4 address.
2424 * This is the traditional BSD usage.
2426 error = sooptcopyin(sopt, &addr, sizeof(struct in_addr),
2427 sizeof(struct in_addr));
2430 if (in_nullhost(addr)) {
2433 INADDR_TO_IFP(addr, ifp);
2435 return (EADDRNOTAVAIL);
2437 CTR3(KTR_IGMPV3, "%s: ifp = %p, addr = %s", __func__, ifp,
2441 /* Reject interfaces which do not support multicast. */
2442 if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0)
2443 return (EOPNOTSUPP);
2445 imo = inp_findmoptions(inp);
2446 imo->imo_multicast_ifp = ifp;
2447 imo->imo_multicast_addr.s_addr = INADDR_ANY;
2454 * Atomically set source filters on a socket for an IPv4 multicast group.
2456 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
2459 inp_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2461 struct __msfilterreq msfr;
2464 struct in_mfilter *imf;
2465 struct ip_moptions *imo;
2466 struct in_multi *inm;
2470 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2471 sizeof(struct __msfilterreq));
2475 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
2478 if ((msfr.msfr_fmode != MCAST_EXCLUDE &&
2479 msfr.msfr_fmode != MCAST_INCLUDE))
2482 if (msfr.msfr_group.ss_family != AF_INET ||
2483 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in))
2486 gsa = (sockunion_t *)&msfr.msfr_group;
2487 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2490 gsa->sin.sin_port = 0; /* ignore port */
2492 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
2493 return (EADDRNOTAVAIL);
2495 ifp = ifnet_byindex(msfr.msfr_ifindex);
2497 return (EADDRNOTAVAIL);
2500 * Take the INP write lock.
2501 * Check if this socket is a member of this group.
2503 imo = inp_findmoptions(inp);
2504 idx = imo_match_group(imo, ifp, &gsa->sa);
2505 if (idx == -1 || imo->imo_mfilters == NULL) {
2506 error = EADDRNOTAVAIL;
2507 goto out_inp_locked;
2509 inm = imo->imo_membership[idx];
2510 imf = &imo->imo_mfilters[idx];
2513 * Begin state merge transaction at socket layer.
2515 INP_WLOCK_ASSERT(inp);
2517 imf->imf_st[1] = msfr.msfr_fmode;
2520 * Apply any new source filters, if present.
2521 * Make a copy of the user-space source vector so
2522 * that we may copy them with a single copyin. This
2523 * allows us to deal with page faults up-front.
2525 if (msfr.msfr_nsrcs > 0) {
2526 struct in_msource *lims;
2527 struct sockaddr_in *psin;
2528 struct sockaddr_storage *kss, *pkss;
2533 CTR2(KTR_IGMPV3, "%s: loading %lu source list entries",
2534 __func__, (unsigned long)msfr.msfr_nsrcs);
2535 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2537 error = copyin(msfr.msfr_srcs, kss,
2538 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2547 * Mark all source filters as UNDEFINED at t1.
2548 * Restore new group filter mode, as imf_leave()
2549 * will set it to INCLUDE.
2552 imf->imf_st[1] = msfr.msfr_fmode;
2555 * Update socket layer filters at t1, lazy-allocating
2556 * new entries. This saves a bunch of memory at the
2557 * cost of one RB_FIND() per source entry; duplicate
2558 * entries in the msfr_nsrcs vector are ignored.
2559 * If we encounter an error, rollback transaction.
2561 * XXX This too could be replaced with a set-symmetric
2562 * difference like loop to avoid walking from root
2563 * every time, as the key space is common.
2565 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2566 psin = (struct sockaddr_in *)pkss;
2567 if (psin->sin_family != AF_INET) {
2568 error = EAFNOSUPPORT;
2571 if (psin->sin_len != sizeof(struct sockaddr_in)) {
2575 error = imf_get_source(imf, psin, &lims);
2578 lims->imsl_st[1] = imf->imf_st[1];
2584 goto out_imf_rollback;
2586 INP_WLOCK_ASSERT(inp);
2590 * Begin state merge transaction at IGMP layer.
2592 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2593 error = inm_merge(inm, imf);
2595 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
2596 goto out_imf_rollback;
2599 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2600 error = igmp_change_state(inm);
2602 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
2620 * Set the IP multicast options in response to user setsockopt().
2622 * Many of the socket options handled in this function duplicate the
2623 * functionality of socket options in the regular unicast API. However,
2624 * it is not possible to merge the duplicate code, because the idempotence
2625 * of the IPv4 multicast part of the BSD Sockets API must be preserved;
2626 * the effects of these options must be treated as separate and distinct.
2628 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2629 * FUTURE: The IP_MULTICAST_VIF option may be eliminated if MROUTING
2630 * is refactored to no longer use vifs.
2633 inp_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2635 struct ip_moptions *imo;
2641 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2642 * or is a divert socket, reject it.
2644 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
2645 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2646 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
2647 return (EOPNOTSUPP);
2649 switch (sopt->sopt_name) {
2650 case IP_MULTICAST_VIF: {
2653 * Select a multicast VIF for transmission.
2654 * Only useful if multicast forwarding is active.
2656 if (legal_vif_num == NULL) {
2660 error = sooptcopyin(sopt, &vifi, sizeof(int), sizeof(int));
2663 if (!legal_vif_num(vifi) && (vifi != -1)) {
2667 imo = inp_findmoptions(inp);
2668 imo->imo_multicast_vif = vifi;
2673 case IP_MULTICAST_IF:
2674 error = inp_set_multicast_if(inp, sopt);
2677 case IP_MULTICAST_TTL: {
2681 * Set the IP time-to-live for outgoing multicast packets.
2682 * The original multicast API required a char argument,
2683 * which is inconsistent with the rest of the socket API.
2684 * We allow either a char or an int.
2686 if (sopt->sopt_valsize == sizeof(u_char)) {
2687 error = sooptcopyin(sopt, &ttl, sizeof(u_char),
2694 error = sooptcopyin(sopt, &ittl, sizeof(u_int),
2704 imo = inp_findmoptions(inp);
2705 imo->imo_multicast_ttl = ttl;
2710 case IP_MULTICAST_LOOP: {
2714 * Set the loopback flag for outgoing multicast packets.
2715 * Must be zero or one. The original multicast API required a
2716 * char argument, which is inconsistent with the rest
2717 * of the socket API. We allow either a char or an int.
2719 if (sopt->sopt_valsize == sizeof(u_char)) {
2720 error = sooptcopyin(sopt, &loop, sizeof(u_char),
2727 error = sooptcopyin(sopt, &iloop, sizeof(u_int),
2731 loop = (u_char)iloop;
2733 imo = inp_findmoptions(inp);
2734 imo->imo_multicast_loop = !!loop;
2739 case IP_ADD_MEMBERSHIP:
2740 case IP_ADD_SOURCE_MEMBERSHIP:
2741 case MCAST_JOIN_GROUP:
2742 case MCAST_JOIN_SOURCE_GROUP:
2743 error = inp_join_group(inp, sopt);
2746 case IP_DROP_MEMBERSHIP:
2747 case IP_DROP_SOURCE_MEMBERSHIP:
2748 case MCAST_LEAVE_GROUP:
2749 case MCAST_LEAVE_SOURCE_GROUP:
2750 error = inp_leave_group(inp, sopt);
2753 case IP_BLOCK_SOURCE:
2754 case IP_UNBLOCK_SOURCE:
2755 case MCAST_BLOCK_SOURCE:
2756 case MCAST_UNBLOCK_SOURCE:
2757 error = inp_block_unblock_source(inp, sopt);
2761 error = inp_set_source_filters(inp, sopt);
2769 INP_UNLOCK_ASSERT(inp);
2775 * Expose IGMP's multicast filter mode and source list(s) to userland,
2776 * keyed by (ifindex, group).
2777 * The filter mode is written out as a uint32_t, followed by
2778 * 0..n of struct in_addr.
2779 * For use by ifmcstat(8).
2780 * SMPng: NOTE: unlocked read of ifindex space.
2783 sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS)
2785 struct in_addr src, group;
2787 struct ifmultiaddr *ifma;
2788 struct in_multi *inm;
2789 struct ip_msource *ims;
2793 uint32_t fmode, ifindex;
2798 if (req->newptr != NULL)
2805 if (ifindex <= 0 || ifindex > V_if_index) {
2806 CTR2(KTR_IGMPV3, "%s: ifindex %u out of range",
2811 group.s_addr = name[1];
2812 if (!IN_MULTICAST(ntohl(group.s_addr))) {
2813 CTR2(KTR_IGMPV3, "%s: group %s is not multicast",
2814 __func__, inet_ntoa(group));
2818 ifp = ifnet_byindex(ifindex);
2820 CTR2(KTR_IGMPV3, "%s: no ifp for ifindex %u",
2825 retval = sysctl_wire_old_buffer(req,
2826 sizeof(uint32_t) + (in_mcast_maxgrpsrc * sizeof(struct in_addr)));
2833 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2834 if (ifma->ifma_addr->sa_family != AF_INET ||
2835 ifma->ifma_protospec == NULL)
2837 inm = (struct in_multi *)ifma->ifma_protospec;
2838 if (!in_hosteq(inm->inm_addr, group))
2840 fmode = inm->inm_st[1].iss_fmode;
2841 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2844 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
2847 ina.s_addr = htonl(ims->ims_haddr);
2848 CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2852 * Only copy-out sources which are in-mode.
2854 if (fmode != ims_get_mode(inm, ims, 1)) {
2855 CTR1(KTR_IGMPV3, "%s: skip non-in-mode",
2859 src.s_addr = htonl(ims->ims_haddr);
2860 retval = SYSCTL_OUT(req, &src, sizeof(struct in_addr));
2865 IF_ADDR_RUNLOCK(ifp);
2874 static const char *inm_modestrs[] = { "un", "in", "ex" };
2877 inm_mode_str(const int mode)
2880 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2881 return (inm_modestrs[mode]);
2885 static const char *inm_statestrs[] = {
2898 inm_state_str(const int state)
2901 if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
2902 return (inm_statestrs[state]);
2907 * Dump an in_multi structure to the console.
2910 inm_print(const struct in_multi *inm)
2914 if ((ktr_mask & KTR_IGMPV3) == 0)
2917 printf("%s: --- begin inm %p ---\n", __func__, inm);
2918 printf("addr %s ifp %p(%s) ifma %p\n",
2919 inet_ntoa(inm->inm_addr),
2921 inm->inm_ifp->if_xname,
2923 printf("timer %u state %s refcount %u scq.len %u\n",
2925 inm_state_str(inm->inm_state),
2927 inm->inm_scq.ifq_len);
2928 printf("igi %p nsrc %lu sctimer %u scrv %u\n",
2933 for (t = 0; t < 2; t++) {
2934 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
2935 inm_mode_str(inm->inm_st[t].iss_fmode),
2936 inm->inm_st[t].iss_asm,
2937 inm->inm_st[t].iss_ex,
2938 inm->inm_st[t].iss_in,
2939 inm->inm_st[t].iss_rec);
2941 printf("%s: --- end inm %p ---\n", __func__, inm);
2947 inm_print(const struct in_multi *inm)
2954 RB_GENERATE(ip_msource_tree, ip_msource, ims_link, ip_msource_cmp);