2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2007-2009 Bruce Simpson.
5 * Copyright (c) 2005 Robert N. M. Watson.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior written
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * IPv4 multicast socket, group, and socket option processing module.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/malloc.h>
46 #include <sys/protosw.h>
47 #include <sys/rmlock.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
51 #include <sys/sysctl.h>
53 #include <sys/taskqueue.h>
57 #include <net/if_var.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
62 #include <net/ethernet.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/in_fib.h>
67 #include <netinet/in_pcb.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip_var.h>
70 #include <netinet/igmp_var.h>
73 #define KTR_IGMPV3 KTR_INET
76 #ifndef __SOCKUNION_DECLARED
78 struct sockaddr_storage ss;
80 struct sockaddr_dl sdl;
81 struct sockaddr_in sin;
83 typedef union sockunion sockunion_t;
84 #define __SOCKUNION_DECLARED
85 #endif /* __SOCKUNION_DECLARED */
87 static MALLOC_DEFINE(M_INMFILTER, "in_mfilter",
88 "IPv4 multicast PCB-layer source filter");
89 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "IPv4 multicast group");
90 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "IPv4 multicast options");
91 static MALLOC_DEFINE(M_IPMSOURCE, "ip_msource",
92 "IPv4 multicast IGMP-layer source filter");
97 * - Lock order is: Giant, IN_MULTI_LOCK, INP_WLOCK,
98 * IN_MULTI_LIST_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
99 * - The IF_ADDR_LOCK is implicitly taken by inm_lookup() earlier, however
100 * it can be taken by code in net/if.c also.
101 * - ip_moptions and in_mfilter are covered by the INP_WLOCK.
103 * struct in_multi is covered by IN_MULTI_LIST_LOCK. There isn't strictly
104 * any need for in_multi itself to be virtualized -- it is bound to an ifp
105 * anyway no matter what happens.
107 struct mtx in_multi_list_mtx;
108 MTX_SYSINIT(in_multi_mtx, &in_multi_list_mtx, "in_multi_list_mtx", MTX_DEF);
110 struct mtx in_multi_free_mtx;
111 MTX_SYSINIT(in_multi_free_mtx, &in_multi_free_mtx, "in_multi_free_mtx", MTX_DEF);
113 struct sx in_multi_sx;
114 SX_SYSINIT(in_multi_sx, &in_multi_sx, "in_multi_sx");
119 * Functions with non-static linkage defined in this file should be
120 * declared in in_var.h:
125 * in_joingroup_locked()
127 * in_leavegroup_locked()
133 * XXX: Both carp and pf need to use the legacy (*,G) KPIs in_addmulti()
136 static void imf_commit(struct in_mfilter *);
137 static int imf_get_source(struct in_mfilter *imf,
138 const struct sockaddr_in *psin,
139 struct in_msource **);
140 static struct in_msource *
141 imf_graft(struct in_mfilter *, const uint8_t,
142 const struct sockaddr_in *);
143 static void imf_leave(struct in_mfilter *);
144 static int imf_prune(struct in_mfilter *, const struct sockaddr_in *);
145 static void imf_purge(struct in_mfilter *);
146 static void imf_rollback(struct in_mfilter *);
147 static void imf_reap(struct in_mfilter *);
148 static struct in_mfilter *
149 imo_match_group(const struct ip_moptions *,
150 const struct ifnet *, const struct sockaddr *);
151 static struct in_msource *
152 imo_match_source(struct in_mfilter *, const struct sockaddr *);
153 static void ims_merge(struct ip_msource *ims,
154 const struct in_msource *lims, const int rollback);
155 static int in_getmulti(struct ifnet *, const struct in_addr *,
157 static int inm_get_source(struct in_multi *inm, const in_addr_t haddr,
158 const int noalloc, struct ip_msource **pims);
160 static int inm_is_ifp_detached(const struct in_multi *);
162 static int inm_merge(struct in_multi *, /*const*/ struct in_mfilter *);
163 static void inm_purge(struct in_multi *);
164 static void inm_reap(struct in_multi *);
165 static void inm_release(struct in_multi *);
166 static struct ip_moptions *
167 inp_findmoptions(struct inpcb *);
168 static int inp_get_source_filters(struct inpcb *, struct sockopt *);
169 static int inp_join_group(struct inpcb *, struct sockopt *);
170 static int inp_leave_group(struct inpcb *, struct sockopt *);
171 static struct ifnet *
172 inp_lookup_mcast_ifp(const struct inpcb *,
173 const struct sockaddr_in *, const struct in_addr);
174 static int inp_block_unblock_source(struct inpcb *, struct sockopt *);
175 static int inp_set_multicast_if(struct inpcb *, struct sockopt *);
176 static int inp_set_source_filters(struct inpcb *, struct sockopt *);
177 static int sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS);
179 static SYSCTL_NODE(_net_inet_ip, OID_AUTO, mcast,
180 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
183 static u_long in_mcast_maxgrpsrc = IP_MAX_GROUP_SRC_FILTER;
184 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxgrpsrc,
185 CTLFLAG_RWTUN, &in_mcast_maxgrpsrc, 0,
186 "Max source filters per group");
188 static u_long in_mcast_maxsocksrc = IP_MAX_SOCK_SRC_FILTER;
189 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxsocksrc,
190 CTLFLAG_RWTUN, &in_mcast_maxsocksrc, 0,
191 "Max source filters per socket");
193 int in_mcast_loop = IP_DEFAULT_MULTICAST_LOOP;
194 SYSCTL_INT(_net_inet_ip_mcast, OID_AUTO, loop, CTLFLAG_RWTUN,
195 &in_mcast_loop, 0, "Loopback multicast datagrams by default");
197 static SYSCTL_NODE(_net_inet_ip_mcast, OID_AUTO, filters,
198 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip_mcast_filters,
199 "Per-interface stack-wide source filters");
203 * Inline function which wraps assertions for a valid ifp.
204 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
208 inm_is_ifp_detached(const struct in_multi *inm)
212 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
213 ifp = inm->inm_ifma->ifma_ifp;
216 * Sanity check that netinet's notion of ifp is the
219 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
222 return (ifp == NULL);
226 static struct task free_task;
227 static struct in_multi_head inm_free_list = SLIST_HEAD_INITIALIZER();
228 static void inm_release_task(void *arg __unused, int pending __unused);
233 TASK_INIT(&free_task, 0, inm_release_task, NULL);
235 SYSINIT(inm_init, SI_SUB_TASKQ, SI_ORDER_ANY, inm_init, NULL);
238 inm_release_list_deferred(struct in_multi_head *inmh)
241 if (SLIST_EMPTY(inmh))
243 mtx_lock(&in_multi_free_mtx);
244 SLIST_CONCAT(&inm_free_list, inmh, in_multi, inm_nrele);
245 mtx_unlock(&in_multi_free_mtx);
246 taskqueue_enqueue(taskqueue_thread, &free_task);
250 inm_disconnect(struct in_multi *inm)
253 struct ifmultiaddr *ifma, *ll_ifma;
256 IF_ADDR_WLOCK_ASSERT(ifp);
257 ifma = inm->inm_ifma;
260 if (ifma->ifma_flags & IFMA_F_ENQUEUED) {
261 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
262 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
264 MCDPRINTF("removed ifma: %p from %s\n", ifma, ifp->if_xname);
265 if ((ll_ifma = ifma->ifma_llifma) != NULL) {
266 MPASS(ifma != ll_ifma);
267 ifma->ifma_llifma = NULL;
268 MPASS(ll_ifma->ifma_llifma == NULL);
269 MPASS(ll_ifma->ifma_ifp == ifp);
270 if (--ll_ifma->ifma_refcount == 0) {
271 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
272 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, ifma_link);
273 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
275 MCDPRINTF("removed ll_ifma: %p from %s\n", ll_ifma, ifp->if_xname);
276 if_freemulti(ll_ifma);
283 inm_release_deferred(struct in_multi *inm)
285 struct in_multi_head tmp;
287 IN_MULTI_LIST_LOCK_ASSERT();
288 MPASS(inm->inm_refcount > 0);
289 if (--inm->inm_refcount == 0) {
292 inm->inm_ifma->ifma_protospec = NULL;
293 SLIST_INSERT_HEAD(&tmp, inm, inm_nrele);
294 inm_release_list_deferred(&tmp);
299 inm_release_task(void *arg __unused, int pending __unused)
301 struct in_multi_head inm_free_tmp;
302 struct in_multi *inm, *tinm;
304 SLIST_INIT(&inm_free_tmp);
305 mtx_lock(&in_multi_free_mtx);
306 SLIST_CONCAT(&inm_free_tmp, &inm_free_list, in_multi, inm_nrele);
307 mtx_unlock(&in_multi_free_mtx);
309 SLIST_FOREACH_SAFE(inm, &inm_free_tmp, inm_nrele, tinm) {
310 SLIST_REMOVE_HEAD(&inm_free_tmp, inm_nrele);
318 * Initialize an in_mfilter structure to a known state at t0, t1
319 * with an empty source filter list.
322 imf_init(struct in_mfilter *imf, const int st0, const int st1)
324 memset(imf, 0, sizeof(struct in_mfilter));
325 RB_INIT(&imf->imf_sources);
326 imf->imf_st[0] = st0;
327 imf->imf_st[1] = st1;
331 ip_mfilter_alloc(const int mflags, const int st0, const int st1)
333 struct in_mfilter *imf;
335 imf = malloc(sizeof(*imf), M_INMFILTER, mflags);
337 imf_init(imf, st0, st1);
343 ip_mfilter_free(struct in_mfilter *imf)
347 free(imf, M_INMFILTER);
351 * Function for looking up an in_multi record for an IPv4 multicast address
352 * on a given interface. ifp must be valid. If no record found, return NULL.
353 * The IN_MULTI_LIST_LOCK and IF_ADDR_LOCK on ifp must be held.
356 inm_lookup_locked(struct ifnet *ifp, const struct in_addr ina)
358 struct ifmultiaddr *ifma;
359 struct in_multi *inm;
361 IN_MULTI_LIST_LOCK_ASSERT();
362 IF_ADDR_LOCK_ASSERT(ifp);
365 CK_STAILQ_FOREACH(ifma, &((ifp)->if_multiaddrs), ifma_link) {
366 if (ifma->ifma_addr->sa_family != AF_INET ||
367 ifma->ifma_protospec == NULL)
369 inm = (struct in_multi *)ifma->ifma_protospec;
370 if (inm->inm_addr.s_addr == ina.s_addr)
378 * Wrapper for inm_lookup_locked().
379 * The IF_ADDR_LOCK will be taken on ifp and released on return.
382 inm_lookup(struct ifnet *ifp, const struct in_addr ina)
384 struct epoch_tracker et;
385 struct in_multi *inm;
387 IN_MULTI_LIST_LOCK_ASSERT();
390 inm = inm_lookup_locked(ifp, ina);
397 * Find an IPv4 multicast group entry for this ip_moptions instance
398 * which matches the specified group, and optionally an interface.
399 * Return its index into the array, or -1 if not found.
401 static struct in_mfilter *
402 imo_match_group(const struct ip_moptions *imo, const struct ifnet *ifp,
403 const struct sockaddr *group)
405 const struct sockaddr_in *gsin;
406 struct in_mfilter *imf;
407 struct in_multi *inm;
409 gsin = (const struct sockaddr_in *)group;
411 IP_MFILTER_FOREACH(imf, &imo->imo_head) {
415 if ((ifp == NULL || (inm->inm_ifp == ifp)) &&
416 in_hosteq(inm->inm_addr, gsin->sin_addr)) {
424 * Find an IPv4 multicast source entry for this imo which matches
425 * the given group index for this socket, and source address.
427 * NOTE: This does not check if the entry is in-mode, merely if
428 * it exists, which may not be the desired behaviour.
430 static struct in_msource *
431 imo_match_source(struct in_mfilter *imf, const struct sockaddr *src)
433 struct ip_msource find;
434 struct ip_msource *ims;
435 const sockunion_t *psa;
437 KASSERT(src->sa_family == AF_INET, ("%s: !AF_INET", __func__));
439 /* Source trees are keyed in host byte order. */
440 psa = (const sockunion_t *)src;
441 find.ims_haddr = ntohl(psa->sin.sin_addr.s_addr);
442 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
444 return ((struct in_msource *)ims);
448 * Perform filtering for multicast datagrams on a socket by group and source.
450 * Returns 0 if a datagram should be allowed through, or various error codes
451 * if the socket was not a member of the group, or the source was muted, etc.
454 imo_multi_filter(const struct ip_moptions *imo, const struct ifnet *ifp,
455 const struct sockaddr *group, const struct sockaddr *src)
457 struct in_mfilter *imf;
458 struct in_msource *ims;
461 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
463 imf = imo_match_group(imo, ifp, group);
465 return (MCAST_NOTGMEMBER);
468 * Check if the source was included in an (S,G) join.
469 * Allow reception on exclusive memberships by default,
470 * reject reception on inclusive memberships by default.
471 * Exclude source only if an in-mode exclude filter exists.
472 * Include source only if an in-mode include filter exists.
473 * NOTE: We are comparing group state here at IGMP t1 (now)
474 * with socket-layer t0 (since last downcall).
476 mode = imf->imf_st[1];
477 ims = imo_match_source(imf, src);
479 if ((ims == NULL && mode == MCAST_INCLUDE) ||
480 (ims != NULL && ims->imsl_st[0] != mode))
481 return (MCAST_NOTSMEMBER);
487 * Find and return a reference to an in_multi record for (ifp, group),
488 * and bump its reference count.
489 * If one does not exist, try to allocate it, and update link-layer multicast
490 * filters on ifp to listen for group.
491 * Assumes the IN_MULTI lock is held across the call.
492 * Return 0 if successful, otherwise return an appropriate error code.
495 in_getmulti(struct ifnet *ifp, const struct in_addr *group,
496 struct in_multi **pinm)
498 struct sockaddr_in gsin;
499 struct ifmultiaddr *ifma;
500 struct in_ifinfo *ii;
501 struct in_multi *inm;
504 IN_MULTI_LOCK_ASSERT();
506 ii = (struct in_ifinfo *)ifp->if_afdata[AF_INET];
507 IN_MULTI_LIST_LOCK();
508 inm = inm_lookup(ifp, *group);
511 * If we already joined this group, just bump the
512 * refcount and return it.
514 KASSERT(inm->inm_refcount >= 1,
515 ("%s: bad refcount %d", __func__, inm->inm_refcount));
516 inm_acquire_locked(inm);
519 IN_MULTI_LIST_UNLOCK();
523 memset(&gsin, 0, sizeof(gsin));
524 gsin.sin_family = AF_INET;
525 gsin.sin_len = sizeof(struct sockaddr_in);
526 gsin.sin_addr = *group;
529 * Check if a link-layer group is already associated
530 * with this network-layer group on the given ifnet.
532 error = if_addmulti(ifp, (struct sockaddr *)&gsin, &ifma);
536 /* XXX ifma_protospec must be covered by IF_ADDR_LOCK */
537 IN_MULTI_LIST_LOCK();
541 * If something other than netinet is occupying the link-layer
542 * group, print a meaningful error message and back out of
544 * Otherwise, bump the refcount on the existing network-layer
545 * group association and return it.
547 if (ifma->ifma_protospec != NULL) {
548 inm = (struct in_multi *)ifma->ifma_protospec;
550 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
552 KASSERT(ifma->ifma_addr->sa_family == AF_INET,
553 ("%s: ifma not AF_INET", __func__));
554 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
555 if (inm->inm_ifma != ifma || inm->inm_ifp != ifp ||
556 !in_hosteq(inm->inm_addr, *group)) {
557 char addrbuf[INET_ADDRSTRLEN];
559 panic("%s: ifma %p is inconsistent with %p (%s)",
560 __func__, ifma, inm, inet_ntoa_r(*group, addrbuf));
563 inm_acquire_locked(inm);
568 IF_ADDR_WLOCK_ASSERT(ifp);
571 * A new in_multi record is needed; allocate and initialize it.
572 * We DO NOT perform an IGMP join as the in_ layer may need to
573 * push an initial source list down to IGMP to support SSM.
575 * The initial source filter state is INCLUDE, {} as per the RFC.
577 inm = malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT | M_ZERO);
579 IF_ADDR_WUNLOCK(ifp);
580 IN_MULTI_LIST_UNLOCK();
581 if_delmulti_ifma(ifma);
584 inm->inm_addr = *group;
586 inm->inm_igi = ii->ii_igmp;
587 inm->inm_ifma = ifma;
588 inm->inm_refcount = 1;
589 inm->inm_state = IGMP_NOT_MEMBER;
590 mbufq_init(&inm->inm_scq, IGMP_MAX_STATE_CHANGES);
591 inm->inm_st[0].iss_fmode = MCAST_UNDEFINED;
592 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
593 RB_INIT(&inm->inm_srcs);
595 ifma->ifma_protospec = inm;
599 IF_ADDR_WUNLOCK(ifp);
600 IN_MULTI_LIST_UNLOCK();
605 * Drop a reference to an in_multi record.
607 * If the refcount drops to 0, free the in_multi record and
608 * delete the underlying link-layer membership.
611 inm_release(struct in_multi *inm)
613 struct ifmultiaddr *ifma;
616 CTR2(KTR_IGMPV3, "%s: refcount is %d", __func__, inm->inm_refcount);
617 MPASS(inm->inm_refcount == 0);
618 CTR2(KTR_IGMPV3, "%s: freeing inm %p", __func__, inm);
620 ifma = inm->inm_ifma;
623 /* XXX this access is not covered by IF_ADDR_LOCK */
624 CTR2(KTR_IGMPV3, "%s: purging ifma %p", __func__, ifma);
626 CURVNET_SET(ifp->if_vnet);
628 free(inm, M_IPMADDR);
629 if_delmulti_ifma_flags(ifma, 1);
634 free(inm, M_IPMADDR);
635 if_delmulti_ifma_flags(ifma, 1);
640 * Clear recorded source entries for a group.
641 * Used by the IGMP code. Caller must hold the IN_MULTI lock.
642 * FIXME: Should reap.
645 inm_clear_recorded(struct in_multi *inm)
647 struct ip_msource *ims;
649 IN_MULTI_LIST_LOCK_ASSERT();
651 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
654 --inm->inm_st[1].iss_rec;
657 KASSERT(inm->inm_st[1].iss_rec == 0,
658 ("%s: iss_rec %d not 0", __func__, inm->inm_st[1].iss_rec));
662 * Record a source as pending for a Source-Group IGMPv3 query.
663 * This lives here as it modifies the shared tree.
665 * inm is the group descriptor.
666 * naddr is the address of the source to record in network-byte order.
668 * If the net.inet.igmp.sgalloc sysctl is non-zero, we will
669 * lazy-allocate a source node in response to an SG query.
670 * Otherwise, no allocation is performed. This saves some memory
671 * with the trade-off that the source will not be reported to the
672 * router if joined in the window between the query response and
673 * the group actually being joined on the local host.
675 * VIMAGE: XXX: Currently the igmp_sgalloc feature has been removed.
676 * This turns off the allocation of a recorded source entry if
677 * the group has not been joined.
679 * Return 0 if the source didn't exist or was already marked as recorded.
680 * Return 1 if the source was marked as recorded by this function.
681 * Return <0 if any error occurred (negated errno code).
684 inm_record_source(struct in_multi *inm, const in_addr_t naddr)
686 struct ip_msource find;
687 struct ip_msource *ims, *nims;
689 IN_MULTI_LIST_LOCK_ASSERT();
691 find.ims_haddr = ntohl(naddr);
692 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
693 if (ims && ims->ims_stp)
696 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
698 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
702 nims->ims_haddr = find.ims_haddr;
703 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
709 * Mark the source as recorded and update the recorded
713 ++inm->inm_st[1].iss_rec;
719 * Return a pointer to an in_msource owned by an in_mfilter,
720 * given its source address.
721 * Lazy-allocate if needed. If this is a new entry its filter state is
724 * imf is the filter set being modified.
725 * haddr is the source address in *host* byte-order.
727 * SMPng: May be called with locks held; malloc must not block.
730 imf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin,
731 struct in_msource **plims)
733 struct ip_msource find;
734 struct ip_msource *ims, *nims;
735 struct in_msource *lims;
742 /* key is host byte order */
743 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
744 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
745 lims = (struct in_msource *)ims;
747 if (imf->imf_nsrc == in_mcast_maxsocksrc)
749 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
753 lims = (struct in_msource *)nims;
754 lims->ims_haddr = find.ims_haddr;
755 lims->imsl_st[0] = MCAST_UNDEFINED;
756 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
766 * Graft a source entry into an existing socket-layer filter set,
767 * maintaining any required invariants and checking allocations.
769 * The source is marked as being in the new filter mode at t1.
771 * Return the pointer to the new node, otherwise return NULL.
773 static struct in_msource *
774 imf_graft(struct in_mfilter *imf, const uint8_t st1,
775 const struct sockaddr_in *psin)
777 struct ip_msource *nims;
778 struct in_msource *lims;
780 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
784 lims = (struct in_msource *)nims;
785 lims->ims_haddr = ntohl(psin->sin_addr.s_addr);
786 lims->imsl_st[0] = MCAST_UNDEFINED;
787 lims->imsl_st[1] = st1;
788 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
795 * Prune a source entry from an existing socket-layer filter set,
796 * maintaining any required invariants and checking allocations.
798 * The source is marked as being left at t1, it is not freed.
800 * Return 0 if no error occurred, otherwise return an errno value.
803 imf_prune(struct in_mfilter *imf, const struct sockaddr_in *psin)
805 struct ip_msource find;
806 struct ip_msource *ims;
807 struct in_msource *lims;
809 /* key is host byte order */
810 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
811 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
814 lims = (struct in_msource *)ims;
815 lims->imsl_st[1] = MCAST_UNDEFINED;
820 * Revert socket-layer filter set deltas at t1 to t0 state.
823 imf_rollback(struct in_mfilter *imf)
825 struct ip_msource *ims, *tims;
826 struct in_msource *lims;
828 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
829 lims = (struct in_msource *)ims;
830 if (lims->imsl_st[0] == lims->imsl_st[1]) {
831 /* no change at t1 */
833 } else if (lims->imsl_st[0] != MCAST_UNDEFINED) {
834 /* revert change to existing source at t1 */
835 lims->imsl_st[1] = lims->imsl_st[0];
837 /* revert source added t1 */
838 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
839 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
840 free(ims, M_INMFILTER);
844 imf->imf_st[1] = imf->imf_st[0];
848 * Mark socket-layer filter set as INCLUDE {} at t1.
851 imf_leave(struct in_mfilter *imf)
853 struct ip_msource *ims;
854 struct in_msource *lims;
856 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
857 lims = (struct in_msource *)ims;
858 lims->imsl_st[1] = MCAST_UNDEFINED;
860 imf->imf_st[1] = MCAST_INCLUDE;
864 * Mark socket-layer filter set deltas as committed.
867 imf_commit(struct in_mfilter *imf)
869 struct ip_msource *ims;
870 struct in_msource *lims;
872 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
873 lims = (struct in_msource *)ims;
874 lims->imsl_st[0] = lims->imsl_st[1];
876 imf->imf_st[0] = imf->imf_st[1];
880 * Reap unreferenced sources from socket-layer filter set.
883 imf_reap(struct in_mfilter *imf)
885 struct ip_msource *ims, *tims;
886 struct in_msource *lims;
888 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
889 lims = (struct in_msource *)ims;
890 if ((lims->imsl_st[0] == MCAST_UNDEFINED) &&
891 (lims->imsl_st[1] == MCAST_UNDEFINED)) {
892 CTR2(KTR_IGMPV3, "%s: free lims %p", __func__, ims);
893 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
894 free(ims, M_INMFILTER);
901 * Purge socket-layer filter set.
904 imf_purge(struct in_mfilter *imf)
906 struct ip_msource *ims, *tims;
908 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
909 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
910 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
911 free(ims, M_INMFILTER);
914 imf->imf_st[0] = imf->imf_st[1] = MCAST_UNDEFINED;
915 KASSERT(RB_EMPTY(&imf->imf_sources),
916 ("%s: imf_sources not empty", __func__));
920 * Look up a source filter entry for a multicast group.
922 * inm is the group descriptor to work with.
923 * haddr is the host-byte-order IPv4 address to look up.
924 * noalloc may be non-zero to suppress allocation of sources.
925 * *pims will be set to the address of the retrieved or allocated source.
927 * SMPng: NOTE: may be called with locks held.
928 * Return 0 if successful, otherwise return a non-zero error code.
931 inm_get_source(struct in_multi *inm, const in_addr_t haddr,
932 const int noalloc, struct ip_msource **pims)
934 struct ip_msource find;
935 struct ip_msource *ims, *nims;
937 find.ims_haddr = haddr;
938 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
939 if (ims == NULL && !noalloc) {
940 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
942 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
946 nims->ims_haddr = haddr;
947 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
951 CTR3(KTR_IGMPV3, "%s: allocated 0x%08x as %p", __func__,
961 * Merge socket-layer source into IGMP-layer source.
962 * If rollback is non-zero, perform the inverse of the merge.
965 ims_merge(struct ip_msource *ims, const struct in_msource *lims,
968 int n = rollback ? -1 : 1;
970 if (lims->imsl_st[0] == MCAST_EXCLUDE) {
971 CTR3(KTR_IGMPV3, "%s: t1 ex -= %d on 0x%08x",
972 __func__, n, ims->ims_haddr);
973 ims->ims_st[1].ex -= n;
974 } else if (lims->imsl_st[0] == MCAST_INCLUDE) {
975 CTR3(KTR_IGMPV3, "%s: t1 in -= %d on 0x%08x",
976 __func__, n, ims->ims_haddr);
977 ims->ims_st[1].in -= n;
980 if (lims->imsl_st[1] == MCAST_EXCLUDE) {
981 CTR3(KTR_IGMPV3, "%s: t1 ex += %d on 0x%08x",
982 __func__, n, ims->ims_haddr);
983 ims->ims_st[1].ex += n;
984 } else if (lims->imsl_st[1] == MCAST_INCLUDE) {
985 CTR3(KTR_IGMPV3, "%s: t1 in += %d on 0x%08x",
986 __func__, n, ims->ims_haddr);
987 ims->ims_st[1].in += n;
992 * Atomically update the global in_multi state, when a membership's
993 * filter list is being updated in any way.
995 * imf is the per-inpcb-membership group filter pointer.
996 * A fake imf may be passed for in-kernel consumers.
998 * XXX This is a candidate for a set-symmetric-difference style loop
999 * which would eliminate the repeated lookup from root of ims nodes,
1000 * as they share the same key space.
1002 * If any error occurred this function will back out of refcounts
1003 * and return a non-zero value.
1006 inm_merge(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1008 struct ip_msource *ims, *nims;
1009 struct in_msource *lims;
1010 int schanged, error;
1016 IN_MULTI_LIST_LOCK_ASSERT();
1019 * Update the source filters first, as this may fail.
1020 * Maintain count of in-mode filters at t0, t1. These are
1021 * used to work out if we transition into ASM mode or not.
1022 * Maintain a count of source filters whose state was
1023 * actually modified by this operation.
1025 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
1026 lims = (struct in_msource *)ims;
1027 if (lims->imsl_st[0] == imf->imf_st[0]) nsrc0++;
1028 if (lims->imsl_st[1] == imf->imf_st[1]) nsrc1++;
1029 if (lims->imsl_st[0] == lims->imsl_st[1]) continue;
1030 error = inm_get_source(inm, lims->ims_haddr, 0, &nims);
1034 ims_merge(nims, lims, 0);
1037 struct ip_msource *bims;
1039 RB_FOREACH_REVERSE_FROM(ims, ip_msource_tree, nims) {
1040 lims = (struct in_msource *)ims;
1041 if (lims->imsl_st[0] == lims->imsl_st[1])
1043 (void)inm_get_source(inm, lims->ims_haddr, 1, &bims);
1046 ims_merge(bims, lims, 1);
1051 CTR3(KTR_IGMPV3, "%s: imf filters in-mode: %d at t0, %d at t1",
1052 __func__, nsrc0, nsrc1);
1054 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1055 if (imf->imf_st[0] == imf->imf_st[1] &&
1056 imf->imf_st[1] == MCAST_INCLUDE) {
1058 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
1059 --inm->inm_st[1].iss_in;
1063 /* Handle filter mode transition on socket. */
1064 if (imf->imf_st[0] != imf->imf_st[1]) {
1065 CTR3(KTR_IGMPV3, "%s: imf transition %d to %d",
1066 __func__, imf->imf_st[0], imf->imf_st[1]);
1068 if (imf->imf_st[0] == MCAST_EXCLUDE) {
1069 CTR1(KTR_IGMPV3, "%s: --ex on inm at t1", __func__);
1070 --inm->inm_st[1].iss_ex;
1071 } else if (imf->imf_st[0] == MCAST_INCLUDE) {
1072 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
1073 --inm->inm_st[1].iss_in;
1076 if (imf->imf_st[1] == MCAST_EXCLUDE) {
1077 CTR1(KTR_IGMPV3, "%s: ex++ on inm at t1", __func__);
1078 inm->inm_st[1].iss_ex++;
1079 } else if (imf->imf_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
1080 CTR1(KTR_IGMPV3, "%s: in++ on inm at t1", __func__);
1081 inm->inm_st[1].iss_in++;
1086 * Track inm filter state in terms of listener counts.
1087 * If there are any exclusive listeners, stack-wide
1088 * membership is exclusive.
1089 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1090 * If no listeners remain, state is undefined at t1,
1091 * and the IGMP lifecycle for this group should finish.
1093 if (inm->inm_st[1].iss_ex > 0) {
1094 CTR1(KTR_IGMPV3, "%s: transition to EX", __func__);
1095 inm->inm_st[1].iss_fmode = MCAST_EXCLUDE;
1096 } else if (inm->inm_st[1].iss_in > 0) {
1097 CTR1(KTR_IGMPV3, "%s: transition to IN", __func__);
1098 inm->inm_st[1].iss_fmode = MCAST_INCLUDE;
1100 CTR1(KTR_IGMPV3, "%s: transition to UNDEF", __func__);
1101 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
1104 /* Decrement ASM listener count on transition out of ASM mode. */
1105 if (imf->imf_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1106 if ((imf->imf_st[1] != MCAST_EXCLUDE) ||
1107 (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 > 0)) {
1108 CTR1(KTR_IGMPV3, "%s: --asm on inm at t1", __func__);
1109 --inm->inm_st[1].iss_asm;
1113 /* Increment ASM listener count on transition to ASM mode. */
1114 if (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1115 CTR1(KTR_IGMPV3, "%s: asm++ on inm at t1", __func__);
1116 inm->inm_st[1].iss_asm++;
1119 CTR3(KTR_IGMPV3, "%s: merged imf %p to inm %p", __func__, imf, inm);
1124 CTR1(KTR_IGMPV3, "%s: sources changed; reaping", __func__);
1131 * Mark an in_multi's filter set deltas as committed.
1132 * Called by IGMP after a state change has been enqueued.
1135 inm_commit(struct in_multi *inm)
1137 struct ip_msource *ims;
1139 CTR2(KTR_IGMPV3, "%s: commit inm %p", __func__, inm);
1140 CTR1(KTR_IGMPV3, "%s: pre commit:", __func__);
1143 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
1144 ims->ims_st[0] = ims->ims_st[1];
1146 inm->inm_st[0] = inm->inm_st[1];
1150 * Reap unreferenced nodes from an in_multi's filter set.
1153 inm_reap(struct in_multi *inm)
1155 struct ip_msource *ims, *tims;
1157 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1158 if (ims->ims_st[0].ex > 0 || ims->ims_st[0].in > 0 ||
1159 ims->ims_st[1].ex > 0 || ims->ims_st[1].in > 0 ||
1162 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1163 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1164 free(ims, M_IPMSOURCE);
1170 * Purge all source nodes from an in_multi's filter set.
1173 inm_purge(struct in_multi *inm)
1175 struct ip_msource *ims, *tims;
1177 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1178 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1179 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1180 free(ims, M_IPMSOURCE);
1186 * Join a multicast group; unlocked entry point.
1188 * SMPng: XXX: in_joingroup() is called from in_control() when Giant
1189 * is not held. Fortunately, ifp is unlikely to have been detached
1190 * at this point, so we assume it's OK to recurse.
1193 in_joingroup(struct ifnet *ifp, const struct in_addr *gina,
1194 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1199 error = in_joingroup_locked(ifp, gina, imf, pinm);
1206 * Join a multicast group; real entry point.
1208 * Only preserves atomicity at inm level.
1209 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1211 * If the IGMP downcall fails, the group is not joined, and an error
1215 in_joingroup_locked(struct ifnet *ifp, const struct in_addr *gina,
1216 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1218 struct in_mfilter timf;
1219 struct in_multi *inm;
1222 IN_MULTI_LOCK_ASSERT();
1223 IN_MULTI_LIST_UNLOCK_ASSERT();
1225 CTR4(KTR_IGMPV3, "%s: join 0x%08x on %p(%s))", __func__,
1226 ntohl(gina->s_addr), ifp, ifp->if_xname);
1232 * If no imf was specified (i.e. kernel consumer),
1233 * fake one up and assume it is an ASM join.
1236 imf_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1240 error = in_getmulti(ifp, gina, &inm);
1242 CTR1(KTR_IGMPV3, "%s: in_getmulti() failure", __func__);
1245 IN_MULTI_LIST_LOCK();
1246 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1247 error = inm_merge(inm, imf);
1249 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1250 goto out_inm_release;
1253 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1254 error = igmp_change_state(inm);
1256 CTR1(KTR_IGMPV3, "%s: failed to update source", __func__);
1257 goto out_inm_release;
1263 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1265 inm_release_deferred(inm);
1266 IF_ADDR_WUNLOCK(ifp);
1270 IN_MULTI_LIST_UNLOCK();
1276 * Leave a multicast group; unlocked entry point.
1279 in_leavegroup(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1284 error = in_leavegroup_locked(inm, imf);
1291 * Leave a multicast group; real entry point.
1292 * All source filters will be expunged.
1294 * Only preserves atomicity at inm level.
1296 * Holding the write lock for the INP which contains imf
1297 * is highly advisable. We can't assert for it as imf does not
1298 * contain a back-pointer to the owning inp.
1300 * Note: This is not the same as inm_release(*) as this function also
1301 * makes a state change downcall into IGMP.
1304 in_leavegroup_locked(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1306 struct in_mfilter timf;
1309 IN_MULTI_LOCK_ASSERT();
1310 IN_MULTI_LIST_UNLOCK_ASSERT();
1314 CTR5(KTR_IGMPV3, "%s: leave inm %p, 0x%08x/%s, imf %p", __func__,
1315 inm, ntohl(inm->inm_addr.s_addr),
1316 (inm_is_ifp_detached(inm) ? "null" : inm->inm_ifp->if_xname),
1320 * If no imf was specified (i.e. kernel consumer),
1321 * fake one up and assume it is an ASM join.
1324 imf_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1329 * Begin state merge transaction at IGMP layer.
1331 * As this particular invocation should not cause any memory
1332 * to be allocated, and there is no opportunity to roll back
1333 * the transaction, it MUST NOT fail.
1335 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1336 IN_MULTI_LIST_LOCK();
1337 error = inm_merge(inm, imf);
1338 KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1340 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1341 CURVNET_SET(inm->inm_ifp->if_vnet);
1342 error = igmp_change_state(inm);
1343 IF_ADDR_WLOCK(inm->inm_ifp);
1344 inm_release_deferred(inm);
1345 IF_ADDR_WUNLOCK(inm->inm_ifp);
1346 IN_MULTI_LIST_UNLOCK();
1349 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1351 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1356 /*#ifndef BURN_BRIDGES*/
1358 * Join an IPv4 multicast group in (*,G) exclusive mode.
1359 * The group must be a 224.0.0.0/24 link-scope group.
1360 * This KPI is for legacy kernel consumers only.
1363 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1365 struct in_multi *pinm;
1368 char addrbuf[INET_ADDRSTRLEN];
1371 KASSERT(IN_LOCAL_GROUP(ntohl(ap->s_addr)),
1372 ("%s: %s not in 224.0.0.0/24", __func__,
1373 inet_ntoa_r(*ap, addrbuf)));
1375 error = in_joingroup(ifp, ap, NULL, &pinm);
1383 * Block or unblock an ASM multicast source on an inpcb.
1384 * This implements the delta-based API described in RFC 3678.
1386 * The delta-based API applies only to exclusive-mode memberships.
1387 * An IGMP downcall will be performed.
1389 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1391 * Return 0 if successful, otherwise return an appropriate error code.
1394 inp_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1396 struct group_source_req gsr;
1397 struct rm_priotracker in_ifa_tracker;
1398 sockunion_t *gsa, *ssa;
1400 struct in_mfilter *imf;
1401 struct ip_moptions *imo;
1402 struct in_msource *ims;
1403 struct in_multi *inm;
1411 memset(&gsr, 0, sizeof(struct group_source_req));
1412 gsa = (sockunion_t *)&gsr.gsr_group;
1413 ssa = (sockunion_t *)&gsr.gsr_source;
1415 switch (sopt->sopt_name) {
1416 case IP_BLOCK_SOURCE:
1417 case IP_UNBLOCK_SOURCE: {
1418 struct ip_mreq_source mreqs;
1420 error = sooptcopyin(sopt, &mreqs,
1421 sizeof(struct ip_mreq_source),
1422 sizeof(struct ip_mreq_source));
1426 gsa->sin.sin_family = AF_INET;
1427 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1428 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1430 ssa->sin.sin_family = AF_INET;
1431 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1432 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1434 if (!in_nullhost(mreqs.imr_interface)) {
1435 IN_IFADDR_RLOCK(&in_ifa_tracker);
1436 INADDR_TO_IFP(mreqs.imr_interface, ifp);
1437 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1439 if (sopt->sopt_name == IP_BLOCK_SOURCE)
1442 CTR3(KTR_IGMPV3, "%s: imr_interface = 0x%08x, ifp = %p",
1443 __func__, ntohl(mreqs.imr_interface.s_addr), ifp);
1447 case MCAST_BLOCK_SOURCE:
1448 case MCAST_UNBLOCK_SOURCE:
1449 error = sooptcopyin(sopt, &gsr,
1450 sizeof(struct group_source_req),
1451 sizeof(struct group_source_req));
1455 if (gsa->sin.sin_family != AF_INET ||
1456 gsa->sin.sin_len != sizeof(struct sockaddr_in))
1459 if (ssa->sin.sin_family != AF_INET ||
1460 ssa->sin.sin_len != sizeof(struct sockaddr_in))
1463 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1464 return (EADDRNOTAVAIL);
1466 ifp = ifnet_byindex(gsr.gsr_interface);
1468 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1473 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
1474 __func__, sopt->sopt_name);
1475 return (EOPNOTSUPP);
1479 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1485 * Check if we are actually a member of this group.
1487 imo = inp_findmoptions(inp);
1488 imf = imo_match_group(imo, ifp, &gsa->sa);
1490 error = EADDRNOTAVAIL;
1491 goto out_inp_locked;
1496 * Attempting to use the delta-based API on an
1497 * non exclusive-mode membership is an error.
1499 fmode = imf->imf_st[0];
1500 if (fmode != MCAST_EXCLUDE) {
1502 goto out_inp_locked;
1506 * Deal with error cases up-front:
1507 * Asked to block, but already blocked; or
1508 * Asked to unblock, but nothing to unblock.
1509 * If adding a new block entry, allocate it.
1511 ims = imo_match_source(imf, &ssa->sa);
1512 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1513 CTR3(KTR_IGMPV3, "%s: source 0x%08x %spresent", __func__,
1514 ntohl(ssa->sin.sin_addr.s_addr), doblock ? "" : "not ");
1515 error = EADDRNOTAVAIL;
1516 goto out_inp_locked;
1519 INP_WLOCK_ASSERT(inp);
1522 * Begin state merge transaction at socket layer.
1525 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
1526 ims = imf_graft(imf, fmode, &ssa->sin);
1530 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
1531 error = imf_prune(imf, &ssa->sin);
1535 CTR1(KTR_IGMPV3, "%s: merge imf state failed", __func__);
1536 goto out_imf_rollback;
1540 * Begin state merge transaction at IGMP layer.
1542 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1543 IN_MULTI_LIST_LOCK();
1544 error = inm_merge(inm, imf);
1546 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1547 IN_MULTI_LIST_UNLOCK();
1548 goto out_imf_rollback;
1551 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1552 error = igmp_change_state(inm);
1553 IN_MULTI_LIST_UNLOCK();
1555 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1572 * Given an inpcb, return its multicast options structure pointer. Accepts
1573 * an unlocked inpcb pointer, but will return it locked. May sleep.
1575 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1576 * SMPng: NOTE: Returns with the INP write lock held.
1578 static struct ip_moptions *
1579 inp_findmoptions(struct inpcb *inp)
1581 struct ip_moptions *imo;
1584 if (inp->inp_moptions != NULL)
1585 return (inp->inp_moptions);
1589 imo = malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK);
1591 imo->imo_multicast_ifp = NULL;
1592 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1593 imo->imo_multicast_vif = -1;
1594 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1595 imo->imo_multicast_loop = in_mcast_loop;
1596 STAILQ_INIT(&imo->imo_head);
1599 if (inp->inp_moptions != NULL) {
1600 free(imo, M_IPMOPTS);
1601 return (inp->inp_moptions);
1603 inp->inp_moptions = imo;
1608 inp_gcmoptions(struct ip_moptions *imo)
1610 struct in_mfilter *imf;
1611 struct in_multi *inm;
1614 while ((imf = ip_mfilter_first(&imo->imo_head)) != NULL) {
1615 ip_mfilter_remove(&imo->imo_head, imf);
1618 if ((inm = imf->imf_inm) != NULL) {
1619 if ((ifp = inm->inm_ifp) != NULL) {
1620 CURVNET_SET(ifp->if_vnet);
1621 (void)in_leavegroup(inm, imf);
1624 (void)in_leavegroup(inm, imf);
1627 ip_mfilter_free(imf);
1629 free(imo, M_IPMOPTS);
1633 * Discard the IP multicast options (and source filters). To minimize
1634 * the amount of work done while holding locks such as the INP's
1635 * pcbinfo lock (which is used in the receive path), the free
1636 * operation is deferred to the epoch callback task.
1639 inp_freemoptions(struct ip_moptions *imo)
1643 inp_gcmoptions(imo);
1647 * Atomically get source filters on a socket for an IPv4 multicast group.
1648 * Called with INP lock held; returns with lock released.
1651 inp_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1653 struct __msfilterreq msfr;
1656 struct ip_moptions *imo;
1657 struct in_mfilter *imf;
1658 struct ip_msource *ims;
1659 struct in_msource *lims;
1660 struct sockaddr_in *psin;
1661 struct sockaddr_storage *ptss;
1662 struct sockaddr_storage *tss;
1664 size_t nsrcs, ncsrcs;
1666 INP_WLOCK_ASSERT(inp);
1668 imo = inp->inp_moptions;
1669 KASSERT(imo != NULL, ("%s: null ip_moptions", __func__));
1673 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1674 sizeof(struct __msfilterreq));
1678 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
1681 ifp = ifnet_byindex(msfr.msfr_ifindex);
1688 * Lookup group on the socket.
1690 gsa = (sockunion_t *)&msfr.msfr_group;
1691 imf = imo_match_group(imo, ifp, &gsa->sa);
1694 return (EADDRNOTAVAIL);
1698 * Ignore memberships which are in limbo.
1700 if (imf->imf_st[1] == MCAST_UNDEFINED) {
1704 msfr.msfr_fmode = imf->imf_st[1];
1707 * If the user specified a buffer, copy out the source filter
1708 * entries to userland gracefully.
1709 * We only copy out the number of entries which userland
1710 * has asked for, but we always tell userland how big the
1711 * buffer really needs to be.
1713 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
1714 msfr.msfr_nsrcs = in_mcast_maxsocksrc;
1716 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1717 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1718 M_TEMP, M_NOWAIT | M_ZERO);
1726 * Count number of sources in-mode at t0.
1727 * If buffer space exists and remains, copy out source entries.
1729 nsrcs = msfr.msfr_nsrcs;
1732 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
1733 lims = (struct in_msource *)ims;
1734 if (lims->imsl_st[0] == MCAST_UNDEFINED ||
1735 lims->imsl_st[0] != imf->imf_st[0])
1738 if (tss != NULL && nsrcs > 0) {
1739 psin = (struct sockaddr_in *)ptss;
1740 psin->sin_family = AF_INET;
1741 psin->sin_len = sizeof(struct sockaddr_in);
1742 psin->sin_addr.s_addr = htonl(lims->ims_haddr);
1752 error = copyout(tss, msfr.msfr_srcs,
1753 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1759 msfr.msfr_nsrcs = ncsrcs;
1760 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1766 * Return the IP multicast options in response to user getsockopt().
1769 inp_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1771 struct rm_priotracker in_ifa_tracker;
1772 struct ip_mreqn mreqn;
1773 struct ip_moptions *imo;
1775 struct in_ifaddr *ia;
1780 imo = inp->inp_moptions;
1782 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1783 * or is a divert socket, reject it.
1785 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1786 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1787 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
1789 return (EOPNOTSUPP);
1793 switch (sopt->sopt_name) {
1794 case IP_MULTICAST_VIF:
1796 optval = imo->imo_multicast_vif;
1800 error = sooptcopyout(sopt, &optval, sizeof(int));
1803 case IP_MULTICAST_IF:
1804 memset(&mreqn, 0, sizeof(struct ip_mreqn));
1806 ifp = imo->imo_multicast_ifp;
1807 if (!in_nullhost(imo->imo_multicast_addr)) {
1808 mreqn.imr_address = imo->imo_multicast_addr;
1809 } else if (ifp != NULL) {
1810 struct epoch_tracker et;
1812 mreqn.imr_ifindex = ifp->if_index;
1813 NET_EPOCH_ENTER(et);
1814 IFP_TO_IA(ifp, ia, &in_ifa_tracker);
1817 IA_SIN(ia)->sin_addr;
1822 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
1823 error = sooptcopyout(sopt, &mreqn,
1824 sizeof(struct ip_mreqn));
1826 error = sooptcopyout(sopt, &mreqn.imr_address,
1827 sizeof(struct in_addr));
1831 case IP_MULTICAST_TTL:
1833 optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1835 optval = coptval = imo->imo_multicast_ttl;
1837 if (sopt->sopt_valsize == sizeof(u_char))
1838 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1840 error = sooptcopyout(sopt, &optval, sizeof(int));
1843 case IP_MULTICAST_LOOP:
1845 optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1847 optval = coptval = imo->imo_multicast_loop;
1849 if (sopt->sopt_valsize == sizeof(u_char))
1850 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1852 error = sooptcopyout(sopt, &optval, sizeof(int));
1857 error = EADDRNOTAVAIL;
1860 error = inp_get_source_filters(inp, sopt);
1866 error = ENOPROTOOPT;
1870 INP_UNLOCK_ASSERT(inp);
1876 * Look up the ifnet to use for a multicast group membership,
1877 * given the IPv4 address of an interface, and the IPv4 group address.
1879 * This routine exists to support legacy multicast applications
1880 * which do not understand that multicast memberships are scoped to
1881 * specific physical links in the networking stack, or which need
1882 * to join link-scope groups before IPv4 addresses are configured.
1884 * If inp is non-NULL, use this socket's current FIB number for any
1885 * required FIB lookup.
1886 * If ina is INADDR_ANY, look up the group address in the unicast FIB,
1887 * and use its ifp; usually, this points to the default next-hop.
1889 * If the FIB lookup fails, attempt to use the first non-loopback
1890 * interface with multicast capability in the system as a
1891 * last resort. The legacy IPv4 ASM API requires that we do
1892 * this in order to allow groups to be joined when the routing
1893 * table has not yet been populated during boot.
1895 * Returns NULL if no ifp could be found.
1897 * FUTURE: Implement IPv4 source-address selection.
1899 static struct ifnet *
1900 inp_lookup_mcast_ifp(const struct inpcb *inp,
1901 const struct sockaddr_in *gsin, const struct in_addr ina)
1903 struct rm_priotracker in_ifa_tracker;
1905 struct nhop4_basic nh4;
1908 KASSERT(gsin->sin_family == AF_INET, ("%s: not AF_INET", __func__));
1909 KASSERT(IN_MULTICAST(ntohl(gsin->sin_addr.s_addr)),
1910 ("%s: not multicast", __func__));
1913 if (!in_nullhost(ina)) {
1914 IN_IFADDR_RLOCK(&in_ifa_tracker);
1915 INADDR_TO_IFP(ina, ifp);
1916 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1918 fibnum = inp ? inp->inp_inc.inc_fibnum : 0;
1919 if (fib4_lookup_nh_basic(fibnum, gsin->sin_addr, 0, 0, &nh4)==0)
1922 struct in_ifaddr *ia;
1926 IN_IFADDR_RLOCK(&in_ifa_tracker);
1927 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1929 if (!(mifp->if_flags & IFF_LOOPBACK) &&
1930 (mifp->if_flags & IFF_MULTICAST)) {
1935 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1943 * Join an IPv4 multicast group, possibly with a source.
1946 inp_join_group(struct inpcb *inp, struct sockopt *sopt)
1948 struct group_source_req gsr;
1949 sockunion_t *gsa, *ssa;
1951 struct in_mfilter *imf;
1952 struct ip_moptions *imo;
1953 struct in_multi *inm;
1954 struct in_msource *lims;
1961 memset(&gsr, 0, sizeof(struct group_source_req));
1962 gsa = (sockunion_t *)&gsr.gsr_group;
1963 gsa->ss.ss_family = AF_UNSPEC;
1964 ssa = (sockunion_t *)&gsr.gsr_source;
1965 ssa->ss.ss_family = AF_UNSPEC;
1967 switch (sopt->sopt_name) {
1968 case IP_ADD_MEMBERSHIP: {
1969 struct ip_mreqn mreqn;
1971 if (sopt->sopt_valsize == sizeof(struct ip_mreqn))
1972 error = sooptcopyin(sopt, &mreqn,
1973 sizeof(struct ip_mreqn), sizeof(struct ip_mreqn));
1975 error = sooptcopyin(sopt, &mreqn,
1976 sizeof(struct ip_mreq), sizeof(struct ip_mreq));
1980 gsa->sin.sin_family = AF_INET;
1981 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1982 gsa->sin.sin_addr = mreqn.imr_multiaddr;
1983 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1986 if (sopt->sopt_valsize == sizeof(struct ip_mreqn) &&
1987 mreqn.imr_ifindex != 0)
1988 ifp = ifnet_byindex(mreqn.imr_ifindex);
1990 ifp = inp_lookup_mcast_ifp(inp, &gsa->sin,
1994 case IP_ADD_SOURCE_MEMBERSHIP: {
1995 struct ip_mreq_source mreqs;
1997 error = sooptcopyin(sopt, &mreqs, sizeof(struct ip_mreq_source),
1998 sizeof(struct ip_mreq_source));
2002 gsa->sin.sin_family = ssa->sin.sin_family = AF_INET;
2003 gsa->sin.sin_len = ssa->sin.sin_len =
2004 sizeof(struct sockaddr_in);
2006 gsa->sin.sin_addr = mreqs.imr_multiaddr;
2007 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2010 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
2012 ifp = inp_lookup_mcast_ifp(inp, &gsa->sin,
2013 mreqs.imr_interface);
2014 CTR3(KTR_IGMPV3, "%s: imr_interface = 0x%08x, ifp = %p",
2015 __func__, ntohl(mreqs.imr_interface.s_addr), ifp);
2019 case MCAST_JOIN_GROUP:
2020 case MCAST_JOIN_SOURCE_GROUP:
2021 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
2022 error = sooptcopyin(sopt, &gsr,
2023 sizeof(struct group_req),
2024 sizeof(struct group_req));
2025 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
2026 error = sooptcopyin(sopt, &gsr,
2027 sizeof(struct group_source_req),
2028 sizeof(struct group_source_req));
2033 if (gsa->sin.sin_family != AF_INET ||
2034 gsa->sin.sin_len != sizeof(struct sockaddr_in))
2038 * Overwrite the port field if present, as the sockaddr
2039 * being copied in may be matched with a binary comparison.
2041 gsa->sin.sin_port = 0;
2042 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
2043 if (ssa->sin.sin_family != AF_INET ||
2044 ssa->sin.sin_len != sizeof(struct sockaddr_in))
2046 ssa->sin.sin_port = 0;
2049 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2052 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
2053 return (EADDRNOTAVAIL);
2054 ifp = ifnet_byindex(gsr.gsr_interface);
2058 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2059 __func__, sopt->sopt_name);
2060 return (EOPNOTSUPP);
2064 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
2065 return (EADDRNOTAVAIL);
2070 * Find the membership in the membership list.
2072 imo = inp_findmoptions(inp);
2073 imf = imo_match_group(imo, ifp, &gsa->sa);
2078 if (ip_mfilter_count(&imo->imo_head) >= IP_MAX_MEMBERSHIPS) {
2080 goto out_inp_locked;
2086 if (ssa->ss.ss_family != AF_UNSPEC) {
2088 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2089 * is an error. On an existing inclusive membership,
2090 * it just adds the source to the filter list.
2092 if (imf->imf_st[1] != MCAST_INCLUDE) {
2094 goto out_inp_locked;
2097 * Throw out duplicates.
2099 * XXX FIXME: This makes a naive assumption that
2100 * even if entries exist for *ssa in this imf,
2101 * they will be rejected as dupes, even if they
2102 * are not valid in the current mode (in-mode).
2104 * in_msource is transactioned just as for anything
2105 * else in SSM -- but note naive use of inm_graft()
2106 * below for allocating new filter entries.
2108 * This is only an issue if someone mixes the
2109 * full-state SSM API with the delta-based API,
2110 * which is discouraged in the relevant RFCs.
2112 lims = imo_match_source(imf, &ssa->sa);
2113 if (lims != NULL /*&&
2114 lims->imsl_st[1] == MCAST_INCLUDE*/) {
2115 error = EADDRNOTAVAIL;
2116 goto out_inp_locked;
2120 * MCAST_JOIN_GROUP on an existing exclusive
2121 * membership is an error; return EADDRINUSE
2122 * to preserve 4.4BSD API idempotence, and
2123 * avoid tedious detour to code below.
2124 * NOTE: This is bending RFC 3678 a bit.
2126 * On an existing inclusive membership, this is also
2127 * an error; if you want to change filter mode,
2128 * you must use the userland API setsourcefilter().
2129 * XXX We don't reject this for imf in UNDEFINED
2130 * state at t1, because allocation of a filter
2131 * is atomic with allocation of a membership.
2134 if (imf->imf_st[1] == MCAST_EXCLUDE)
2136 goto out_inp_locked;
2141 * Begin state merge transaction at socket layer.
2143 INP_WLOCK_ASSERT(inp);
2146 * Graft new source into filter list for this inpcb's
2147 * membership of the group. The in_multi may not have
2148 * been allocated yet if this is a new membership, however,
2149 * the in_mfilter slot will be allocated and must be initialized.
2151 * Note: Grafting of exclusive mode filters doesn't happen
2153 * XXX: Should check for non-NULL lims (node exists but may
2154 * not be in-mode) for interop with full-state API.
2156 if (ssa->ss.ss_family != AF_UNSPEC) {
2157 /* Membership starts in IN mode */
2159 CTR1(KTR_IGMPV3, "%s: new join w/source", __func__);
2160 imf = ip_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_INCLUDE);
2163 goto out_inp_locked;
2166 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
2168 lims = imf_graft(imf, MCAST_INCLUDE, &ssa->sin);
2170 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2173 goto out_inp_locked;
2176 /* No address specified; Membership starts in EX mode */
2178 CTR1(KTR_IGMPV3, "%s: new join w/o source", __func__);
2179 imf = ip_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_EXCLUDE);
2182 goto out_inp_locked;
2188 * Begin state merge transaction at IGMP layer.
2194 error = in_joingroup_locked(ifp, &gsa->sin.sin_addr, imf,
2198 if (in_pcbrele_wlocked(inp)) {
2200 goto out_inp_unlocked;
2203 CTR1(KTR_IGMPV3, "%s: in_joingroup_locked failed",
2205 goto out_inp_locked;
2208 * NOTE: Refcount from in_joingroup_locked()
2209 * is protecting membership.
2211 ip_mfilter_insert(&imo->imo_head, imf);
2213 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2214 IN_MULTI_LIST_LOCK();
2215 error = inm_merge(inm, imf);
2217 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2219 IN_MULTI_LIST_UNLOCK();
2222 goto out_inp_locked;
2224 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2225 error = igmp_change_state(inm);
2226 IN_MULTI_LIST_UNLOCK();
2228 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2232 goto out_inp_locked;
2244 if (is_new && imf) {
2245 if (imf->imf_inm != NULL) {
2246 IN_MULTI_LIST_LOCK();
2248 inm_release_deferred(imf->imf_inm);
2249 IF_ADDR_WUNLOCK(ifp);
2250 IN_MULTI_LIST_UNLOCK();
2252 ip_mfilter_free(imf);
2258 * Leave an IPv4 multicast group on an inpcb, possibly with a source.
2261 inp_leave_group(struct inpcb *inp, struct sockopt *sopt)
2263 struct group_source_req gsr;
2264 struct ip_mreq_source mreqs;
2265 struct rm_priotracker in_ifa_tracker;
2266 sockunion_t *gsa, *ssa;
2268 struct in_mfilter *imf;
2269 struct ip_moptions *imo;
2270 struct in_msource *ims;
2271 struct in_multi *inm;
2279 memset(&gsr, 0, sizeof(struct group_source_req));
2280 gsa = (sockunion_t *)&gsr.gsr_group;
2281 gsa->ss.ss_family = AF_UNSPEC;
2282 ssa = (sockunion_t *)&gsr.gsr_source;
2283 ssa->ss.ss_family = AF_UNSPEC;
2285 switch (sopt->sopt_name) {
2286 case IP_DROP_MEMBERSHIP:
2287 case IP_DROP_SOURCE_MEMBERSHIP:
2288 if (sopt->sopt_name == IP_DROP_MEMBERSHIP) {
2289 error = sooptcopyin(sopt, &mreqs,
2290 sizeof(struct ip_mreq),
2291 sizeof(struct ip_mreq));
2293 * Swap interface and sourceaddr arguments,
2294 * as ip_mreq and ip_mreq_source are laid
2297 mreqs.imr_interface = mreqs.imr_sourceaddr;
2298 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
2299 } else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2300 error = sooptcopyin(sopt, &mreqs,
2301 sizeof(struct ip_mreq_source),
2302 sizeof(struct ip_mreq_source));
2307 gsa->sin.sin_family = AF_INET;
2308 gsa->sin.sin_len = sizeof(struct sockaddr_in);
2309 gsa->sin.sin_addr = mreqs.imr_multiaddr;
2311 if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2312 ssa->sin.sin_family = AF_INET;
2313 ssa->sin.sin_len = sizeof(struct sockaddr_in);
2314 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
2318 * Attempt to look up hinted ifp from interface address.
2319 * Fallthrough with null ifp iff lookup fails, to
2320 * preserve 4.4BSD mcast API idempotence.
2321 * XXX NOTE WELL: The RFC 3678 API is preferred because
2322 * using an IPv4 address as a key is racy.
2324 if (!in_nullhost(mreqs.imr_interface)) {
2325 IN_IFADDR_RLOCK(&in_ifa_tracker);
2326 INADDR_TO_IFP(mreqs.imr_interface, ifp);
2327 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
2329 CTR3(KTR_IGMPV3, "%s: imr_interface = 0x%08x, ifp = %p",
2330 __func__, ntohl(mreqs.imr_interface.s_addr), ifp);
2334 case MCAST_LEAVE_GROUP:
2335 case MCAST_LEAVE_SOURCE_GROUP:
2336 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2337 error = sooptcopyin(sopt, &gsr,
2338 sizeof(struct group_req),
2339 sizeof(struct group_req));
2340 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2341 error = sooptcopyin(sopt, &gsr,
2342 sizeof(struct group_source_req),
2343 sizeof(struct group_source_req));
2348 if (gsa->sin.sin_family != AF_INET ||
2349 gsa->sin.sin_len != sizeof(struct sockaddr_in))
2352 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2353 if (ssa->sin.sin_family != AF_INET ||
2354 ssa->sin.sin_len != sizeof(struct sockaddr_in))
2358 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
2359 return (EADDRNOTAVAIL);
2361 ifp = ifnet_byindex(gsr.gsr_interface);
2364 return (EADDRNOTAVAIL);
2368 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2369 __func__, sopt->sopt_name);
2370 return (EOPNOTSUPP);
2374 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2380 * Find the membership in the membership list.
2382 imo = inp_findmoptions(inp);
2383 imf = imo_match_group(imo, ifp, &gsa->sa);
2385 error = EADDRNOTAVAIL;
2386 goto out_inp_locked;
2390 if (ssa->ss.ss_family != AF_UNSPEC)
2394 * Begin state merge transaction at socket layer.
2396 INP_WLOCK_ASSERT(inp);
2399 * If we were instructed only to leave a given source, do so.
2400 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2403 ip_mfilter_remove(&imo->imo_head, imf);
2407 * Give up the multicast address record to which
2408 * the membership points.
2410 (void) in_leavegroup_locked(imf->imf_inm, imf);
2412 if (imf->imf_st[0] == MCAST_EXCLUDE) {
2413 error = EADDRNOTAVAIL;
2414 goto out_inp_locked;
2416 ims = imo_match_source(imf, &ssa->sa);
2418 CTR3(KTR_IGMPV3, "%s: source 0x%08x %spresent",
2419 __func__, ntohl(ssa->sin.sin_addr.s_addr), "not ");
2420 error = EADDRNOTAVAIL;
2421 goto out_inp_locked;
2423 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
2424 error = imf_prune(imf, &ssa->sin);
2426 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2428 goto out_inp_locked;
2433 * Begin state merge transaction at IGMP layer.
2436 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2437 IN_MULTI_LIST_LOCK();
2438 error = inm_merge(inm, imf);
2440 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2442 IN_MULTI_LIST_UNLOCK();
2445 goto out_inp_locked;
2448 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2449 error = igmp_change_state(inm);
2450 IN_MULTI_LIST_UNLOCK();
2452 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2456 goto out_inp_locked;
2465 if (is_final && imf)
2466 ip_mfilter_free(imf);
2473 * Select the interface for transmitting IPv4 multicast datagrams.
2475 * Either an instance of struct in_addr or an instance of struct ip_mreqn
2476 * may be passed to this socket option. An address of INADDR_ANY or an
2477 * interface index of 0 is used to remove a previous selection.
2478 * When no interface is selected, one is chosen for every send.
2481 inp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2483 struct rm_priotracker in_ifa_tracker;
2484 struct in_addr addr;
2485 struct ip_mreqn mreqn;
2487 struct ip_moptions *imo;
2490 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
2492 * An interface index was specified using the
2493 * Linux-derived ip_mreqn structure.
2495 error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn),
2496 sizeof(struct ip_mreqn));
2500 if (mreqn.imr_ifindex < 0 || V_if_index < mreqn.imr_ifindex)
2503 if (mreqn.imr_ifindex == 0) {
2506 ifp = ifnet_byindex(mreqn.imr_ifindex);
2508 return (EADDRNOTAVAIL);
2512 * An interface was specified by IPv4 address.
2513 * This is the traditional BSD usage.
2515 error = sooptcopyin(sopt, &addr, sizeof(struct in_addr),
2516 sizeof(struct in_addr));
2519 if (in_nullhost(addr)) {
2522 IN_IFADDR_RLOCK(&in_ifa_tracker);
2523 INADDR_TO_IFP(addr, ifp);
2524 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
2526 return (EADDRNOTAVAIL);
2528 CTR3(KTR_IGMPV3, "%s: ifp = %p, addr = 0x%08x", __func__, ifp,
2529 ntohl(addr.s_addr));
2532 /* Reject interfaces which do not support multicast. */
2533 if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0)
2534 return (EOPNOTSUPP);
2536 imo = inp_findmoptions(inp);
2537 imo->imo_multicast_ifp = ifp;
2538 imo->imo_multicast_addr.s_addr = INADDR_ANY;
2545 * Atomically set source filters on a socket for an IPv4 multicast group.
2547 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
2550 inp_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2552 struct __msfilterreq msfr;
2555 struct in_mfilter *imf;
2556 struct ip_moptions *imo;
2557 struct in_multi *inm;
2560 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2561 sizeof(struct __msfilterreq));
2565 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
2568 if ((msfr.msfr_fmode != MCAST_EXCLUDE &&
2569 msfr.msfr_fmode != MCAST_INCLUDE))
2572 if (msfr.msfr_group.ss_family != AF_INET ||
2573 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in))
2576 gsa = (sockunion_t *)&msfr.msfr_group;
2577 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2580 gsa->sin.sin_port = 0; /* ignore port */
2582 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
2583 return (EADDRNOTAVAIL);
2585 ifp = ifnet_byindex(msfr.msfr_ifindex);
2587 return (EADDRNOTAVAIL);
2592 * Take the INP write lock.
2593 * Check if this socket is a member of this group.
2595 imo = inp_findmoptions(inp);
2596 imf = imo_match_group(imo, ifp, &gsa->sa);
2598 error = EADDRNOTAVAIL;
2599 goto out_inp_locked;
2604 * Begin state merge transaction at socket layer.
2606 INP_WLOCK_ASSERT(inp);
2608 imf->imf_st[1] = msfr.msfr_fmode;
2611 * Apply any new source filters, if present.
2612 * Make a copy of the user-space source vector so
2613 * that we may copy them with a single copyin. This
2614 * allows us to deal with page faults up-front.
2616 if (msfr.msfr_nsrcs > 0) {
2617 struct in_msource *lims;
2618 struct sockaddr_in *psin;
2619 struct sockaddr_storage *kss, *pkss;
2624 CTR2(KTR_IGMPV3, "%s: loading %lu source list entries",
2625 __func__, (unsigned long)msfr.msfr_nsrcs);
2626 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2628 error = copyin(msfr.msfr_srcs, kss,
2629 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2638 * Mark all source filters as UNDEFINED at t1.
2639 * Restore new group filter mode, as imf_leave()
2640 * will set it to INCLUDE.
2643 imf->imf_st[1] = msfr.msfr_fmode;
2646 * Update socket layer filters at t1, lazy-allocating
2647 * new entries. This saves a bunch of memory at the
2648 * cost of one RB_FIND() per source entry; duplicate
2649 * entries in the msfr_nsrcs vector are ignored.
2650 * If we encounter an error, rollback transaction.
2652 * XXX This too could be replaced with a set-symmetric
2653 * difference like loop to avoid walking from root
2654 * every time, as the key space is common.
2656 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2657 psin = (struct sockaddr_in *)pkss;
2658 if (psin->sin_family != AF_INET) {
2659 error = EAFNOSUPPORT;
2662 if (psin->sin_len != sizeof(struct sockaddr_in)) {
2666 error = imf_get_source(imf, psin, &lims);
2669 lims->imsl_st[1] = imf->imf_st[1];
2675 goto out_imf_rollback;
2677 INP_WLOCK_ASSERT(inp);
2680 * Begin state merge transaction at IGMP layer.
2682 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2683 IN_MULTI_LIST_LOCK();
2684 error = inm_merge(inm, imf);
2686 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
2687 IN_MULTI_LIST_UNLOCK();
2688 goto out_imf_rollback;
2691 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2692 error = igmp_change_state(inm);
2693 IN_MULTI_LIST_UNLOCK();
2695 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
2712 * Set the IP multicast options in response to user setsockopt().
2714 * Many of the socket options handled in this function duplicate the
2715 * functionality of socket options in the regular unicast API. However,
2716 * it is not possible to merge the duplicate code, because the idempotence
2717 * of the IPv4 multicast part of the BSD Sockets API must be preserved;
2718 * the effects of these options must be treated as separate and distinct.
2720 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2721 * FUTURE: The IP_MULTICAST_VIF option may be eliminated if MROUTING
2722 * is refactored to no longer use vifs.
2725 inp_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2727 struct ip_moptions *imo;
2733 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2734 * or is a divert socket, reject it.
2736 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
2737 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2738 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
2739 return (EOPNOTSUPP);
2741 switch (sopt->sopt_name) {
2742 case IP_MULTICAST_VIF: {
2745 * Select a multicast VIF for transmission.
2746 * Only useful if multicast forwarding is active.
2748 if (legal_vif_num == NULL) {
2752 error = sooptcopyin(sopt, &vifi, sizeof(int), sizeof(int));
2755 if (!legal_vif_num(vifi) && (vifi != -1)) {
2759 imo = inp_findmoptions(inp);
2760 imo->imo_multicast_vif = vifi;
2765 case IP_MULTICAST_IF:
2766 error = inp_set_multicast_if(inp, sopt);
2769 case IP_MULTICAST_TTL: {
2773 * Set the IP time-to-live for outgoing multicast packets.
2774 * The original multicast API required a char argument,
2775 * which is inconsistent with the rest of the socket API.
2776 * We allow either a char or an int.
2778 if (sopt->sopt_valsize == sizeof(u_char)) {
2779 error = sooptcopyin(sopt, &ttl, sizeof(u_char),
2786 error = sooptcopyin(sopt, &ittl, sizeof(u_int),
2796 imo = inp_findmoptions(inp);
2797 imo->imo_multicast_ttl = ttl;
2802 case IP_MULTICAST_LOOP: {
2806 * Set the loopback flag for outgoing multicast packets.
2807 * Must be zero or one. The original multicast API required a
2808 * char argument, which is inconsistent with the rest
2809 * of the socket API. We allow either a char or an int.
2811 if (sopt->sopt_valsize == sizeof(u_char)) {
2812 error = sooptcopyin(sopt, &loop, sizeof(u_char),
2819 error = sooptcopyin(sopt, &iloop, sizeof(u_int),
2823 loop = (u_char)iloop;
2825 imo = inp_findmoptions(inp);
2826 imo->imo_multicast_loop = !!loop;
2831 case IP_ADD_MEMBERSHIP:
2832 case IP_ADD_SOURCE_MEMBERSHIP:
2833 case MCAST_JOIN_GROUP:
2834 case MCAST_JOIN_SOURCE_GROUP:
2835 error = inp_join_group(inp, sopt);
2838 case IP_DROP_MEMBERSHIP:
2839 case IP_DROP_SOURCE_MEMBERSHIP:
2840 case MCAST_LEAVE_GROUP:
2841 case MCAST_LEAVE_SOURCE_GROUP:
2842 error = inp_leave_group(inp, sopt);
2845 case IP_BLOCK_SOURCE:
2846 case IP_UNBLOCK_SOURCE:
2847 case MCAST_BLOCK_SOURCE:
2848 case MCAST_UNBLOCK_SOURCE:
2849 error = inp_block_unblock_source(inp, sopt);
2853 error = inp_set_source_filters(inp, sopt);
2861 INP_UNLOCK_ASSERT(inp);
2867 * Expose IGMP's multicast filter mode and source list(s) to userland,
2868 * keyed by (ifindex, group).
2869 * The filter mode is written out as a uint32_t, followed by
2870 * 0..n of struct in_addr.
2871 * For use by ifmcstat(8).
2872 * SMPng: NOTE: unlocked read of ifindex space.
2875 sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS)
2877 struct in_addr src, group;
2878 struct epoch_tracker et;
2880 struct ifmultiaddr *ifma;
2881 struct in_multi *inm;
2882 struct ip_msource *ims;
2886 uint32_t fmode, ifindex;
2891 if (req->newptr != NULL)
2898 if (ifindex <= 0 || ifindex > V_if_index) {
2899 CTR2(KTR_IGMPV3, "%s: ifindex %u out of range",
2904 group.s_addr = name[1];
2905 if (!IN_MULTICAST(ntohl(group.s_addr))) {
2906 CTR2(KTR_IGMPV3, "%s: group 0x%08x is not multicast",
2907 __func__, ntohl(group.s_addr));
2911 NET_EPOCH_ENTER(et);
2912 ifp = ifnet_byindex(ifindex);
2915 CTR2(KTR_IGMPV3, "%s: no ifp for ifindex %u",
2920 retval = sysctl_wire_old_buffer(req,
2921 sizeof(uint32_t) + (in_mcast_maxgrpsrc * sizeof(struct in_addr)));
2927 IN_MULTI_LIST_LOCK();
2929 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2930 if (ifma->ifma_addr->sa_family != AF_INET ||
2931 ifma->ifma_protospec == NULL)
2933 inm = (struct in_multi *)ifma->ifma_protospec;
2934 if (!in_hosteq(inm->inm_addr, group))
2936 fmode = inm->inm_st[1].iss_fmode;
2937 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2940 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
2941 CTR2(KTR_IGMPV3, "%s: visit node 0x%08x", __func__,
2944 * Only copy-out sources which are in-mode.
2946 if (fmode != ims_get_mode(inm, ims, 1)) {
2947 CTR1(KTR_IGMPV3, "%s: skip non-in-mode",
2951 src.s_addr = htonl(ims->ims_haddr);
2952 retval = SYSCTL_OUT(req, &src, sizeof(struct in_addr));
2958 IN_MULTI_LIST_UNLOCK();
2964 #if defined(KTR) && (KTR_COMPILE & KTR_IGMPV3)
2966 static const char *inm_modestrs[] = {
2967 [MCAST_UNDEFINED] = "un",
2968 [MCAST_INCLUDE] = "in",
2969 [MCAST_EXCLUDE] = "ex",
2971 _Static_assert(MCAST_UNDEFINED == 0 &&
2972 MCAST_EXCLUDE + 1 == nitems(inm_modestrs),
2973 "inm_modestrs: no longer matches #defines");
2976 inm_mode_str(const int mode)
2979 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2980 return (inm_modestrs[mode]);
2984 static const char *inm_statestrs[] = {
2985 [IGMP_NOT_MEMBER] = "not-member",
2986 [IGMP_SILENT_MEMBER] = "silent",
2987 [IGMP_REPORTING_MEMBER] = "reporting",
2988 [IGMP_IDLE_MEMBER] = "idle",
2989 [IGMP_LAZY_MEMBER] = "lazy",
2990 [IGMP_SLEEPING_MEMBER] = "sleeping",
2991 [IGMP_AWAKENING_MEMBER] = "awakening",
2992 [IGMP_G_QUERY_PENDING_MEMBER] = "query-pending",
2993 [IGMP_SG_QUERY_PENDING_MEMBER] = "sg-query-pending",
2994 [IGMP_LEAVING_MEMBER] = "leaving",
2996 _Static_assert(IGMP_NOT_MEMBER == 0 &&
2997 IGMP_LEAVING_MEMBER + 1 == nitems(inm_statestrs),
2998 "inm_statetrs: no longer matches #defines");
3001 inm_state_str(const int state)
3004 if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
3005 return (inm_statestrs[state]);
3010 * Dump an in_multi structure to the console.
3013 inm_print(const struct in_multi *inm)
3016 char addrbuf[INET_ADDRSTRLEN];
3018 if ((ktr_mask & KTR_IGMPV3) == 0)
3021 printf("%s: --- begin inm %p ---\n", __func__, inm);
3022 printf("addr %s ifp %p(%s) ifma %p\n",
3023 inet_ntoa_r(inm->inm_addr, addrbuf),
3025 inm->inm_ifp->if_xname,
3027 printf("timer %u state %s refcount %u scq.len %u\n",
3029 inm_state_str(inm->inm_state),
3031 inm->inm_scq.mq_len);
3032 printf("igi %p nsrc %lu sctimer %u scrv %u\n",
3037 for (t = 0; t < 2; t++) {
3038 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
3039 inm_mode_str(inm->inm_st[t].iss_fmode),
3040 inm->inm_st[t].iss_asm,
3041 inm->inm_st[t].iss_ex,
3042 inm->inm_st[t].iss_in,
3043 inm->inm_st[t].iss_rec);
3045 printf("%s: --- end inm %p ---\n", __func__, inm);
3048 #else /* !KTR || !(KTR_COMPILE & KTR_IGMPV3) */
3051 inm_print(const struct in_multi *inm)
3056 #endif /* KTR && (KTR_COMPILE & KTR_IGMPV3) */
3058 RB_GENERATE(ip_msource_tree, ip_msource, ims_link, ip_msource_cmp);