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 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/malloc.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/protosw.h>
48 #include <sys/sysctl.h>
50 #include <sys/taskqueue.h>
54 #include <net/if_var.h>
55 #include <net/if_dl.h>
56 #include <net/route.h>
57 #include <net/route/nhop.h>
60 #include <net/ethernet.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_fib.h>
65 #include <netinet/in_pcb.h>
66 #include <netinet/in_var.h>
67 #include <net/if_private.h>
68 #include <netinet/ip_var.h>
69 #include <netinet/igmp_var.h>
72 #define KTR_IGMPV3 KTR_INET
75 #ifndef __SOCKUNION_DECLARED
77 struct sockaddr_storage ss;
79 struct sockaddr_dl sdl;
80 struct sockaddr_in sin;
82 typedef union sockunion sockunion_t;
83 #define __SOCKUNION_DECLARED
84 #endif /* __SOCKUNION_DECLARED */
86 static MALLOC_DEFINE(M_INMFILTER, "in_mfilter",
87 "IPv4 multicast PCB-layer source filter");
88 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "IPv4 multicast group");
89 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "IPv4 multicast options");
90 static MALLOC_DEFINE(M_IPMSOURCE, "ip_msource",
91 "IPv4 multicast IGMP-layer source filter");
96 * - Lock order is: Giant, IN_MULTI_LOCK, INP_WLOCK,
97 * IN_MULTI_LIST_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
98 * - The IF_ADDR_LOCK is implicitly taken by inm_lookup() earlier, however
99 * it can be taken by code in net/if.c also.
100 * - ip_moptions and in_mfilter are covered by the INP_WLOCK.
102 * struct in_multi is covered by IN_MULTI_LIST_LOCK. There isn't strictly
103 * any need for in_multi itself to be virtualized -- it is bound to an ifp
104 * anyway no matter what happens.
106 struct mtx in_multi_list_mtx;
107 MTX_SYSINIT(in_multi_mtx, &in_multi_list_mtx, "in_multi_list_mtx", MTX_DEF);
109 struct mtx in_multi_free_mtx;
110 MTX_SYSINIT(in_multi_free_mtx, &in_multi_free_mtx, "in_multi_free_mtx", MTX_DEF);
112 struct sx in_multi_sx;
113 SX_SYSINIT(in_multi_sx, &in_multi_sx, "in_multi_sx");
116 * Functions with non-static linkage defined in this file should be
117 * declared in in_var.h:
120 * in_joingroup_locked()
122 * in_leavegroup_locked()
128 static void imf_commit(struct in_mfilter *);
129 static int imf_get_source(struct in_mfilter *imf,
130 const struct sockaddr_in *psin,
131 struct in_msource **);
132 static struct in_msource *
133 imf_graft(struct in_mfilter *, const uint8_t,
134 const struct sockaddr_in *);
135 static void imf_leave(struct in_mfilter *);
136 static int imf_prune(struct in_mfilter *, const struct sockaddr_in *);
137 static void imf_purge(struct in_mfilter *);
138 static void imf_rollback(struct in_mfilter *);
139 static void imf_reap(struct in_mfilter *);
140 static struct in_mfilter *
141 imo_match_group(const struct ip_moptions *,
142 const struct ifnet *, const struct sockaddr *);
143 static struct in_msource *
144 imo_match_source(struct in_mfilter *, const struct sockaddr *);
145 static void ims_merge(struct ip_msource *ims,
146 const struct in_msource *lims, const int rollback);
147 static int in_getmulti(struct ifnet *, const struct in_addr *,
149 static int inm_get_source(struct in_multi *inm, const in_addr_t haddr,
150 const int noalloc, struct ip_msource **pims);
152 static int inm_is_ifp_detached(const struct in_multi *);
154 static int inm_merge(struct in_multi *, /*const*/ struct in_mfilter *);
155 static void inm_purge(struct in_multi *);
156 static void inm_reap(struct in_multi *);
157 static void inm_release(struct in_multi *);
158 static struct ip_moptions *
159 inp_findmoptions(struct inpcb *);
160 static int inp_get_source_filters(struct inpcb *, struct sockopt *);
161 static int inp_join_group(struct inpcb *, struct sockopt *);
162 static int inp_leave_group(struct inpcb *, struct sockopt *);
163 static struct ifnet *
164 inp_lookup_mcast_ifp(const struct inpcb *,
165 const struct sockaddr_in *, const struct in_addr);
166 static int inp_block_unblock_source(struct inpcb *, struct sockopt *);
167 static int inp_set_multicast_if(struct inpcb *, struct sockopt *);
168 static int inp_set_source_filters(struct inpcb *, struct sockopt *);
169 static int sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS);
171 static SYSCTL_NODE(_net_inet_ip, OID_AUTO, mcast,
172 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
175 static u_long in_mcast_maxgrpsrc = IP_MAX_GROUP_SRC_FILTER;
176 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxgrpsrc,
177 CTLFLAG_RWTUN, &in_mcast_maxgrpsrc, 0,
178 "Max source filters per group");
180 static u_long in_mcast_maxsocksrc = IP_MAX_SOCK_SRC_FILTER;
181 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxsocksrc,
182 CTLFLAG_RWTUN, &in_mcast_maxsocksrc, 0,
183 "Max source filters per socket");
185 int in_mcast_loop = IP_DEFAULT_MULTICAST_LOOP;
186 SYSCTL_INT(_net_inet_ip_mcast, OID_AUTO, loop, CTLFLAG_RWTUN,
187 &in_mcast_loop, 0, "Loopback multicast datagrams by default");
189 static SYSCTL_NODE(_net_inet_ip_mcast, OID_AUTO, filters,
190 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip_mcast_filters,
191 "Per-interface stack-wide source filters");
195 * Inline function which wraps assertions for a valid ifp.
196 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
200 inm_is_ifp_detached(const struct in_multi *inm)
204 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
205 ifp = inm->inm_ifma->ifma_ifp;
208 * Sanity check that netinet's notion of ifp is the
211 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
214 return (ifp == NULL);
219 * Interface detach can happen in a taskqueue thread context, so we must use a
220 * dedicated thread to avoid deadlocks when draining inm_release tasks.
222 TASKQUEUE_DEFINE_THREAD(inm_free);
223 static struct in_multi_head inm_free_list = SLIST_HEAD_INITIALIZER();
224 static void inm_release_task(void *arg __unused, int pending __unused);
225 static struct task inm_free_task = TASK_INITIALIZER(0, inm_release_task, NULL);
228 inm_release_wait(void *arg __unused)
232 * Make sure all pending multicast addresses are freed before
233 * the VNET or network device is destroyed:
235 taskqueue_drain(taskqueue_inm_free, &inm_free_task);
238 /* XXX-BZ FIXME, see D24914. */
239 VNET_SYSUNINIT(inm_release_wait, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, inm_release_wait, NULL);
243 inm_release_list_deferred(struct in_multi_head *inmh)
246 if (SLIST_EMPTY(inmh))
248 mtx_lock(&in_multi_free_mtx);
249 SLIST_CONCAT(&inm_free_list, inmh, in_multi, inm_nrele);
250 mtx_unlock(&in_multi_free_mtx);
251 taskqueue_enqueue(taskqueue_inm_free, &inm_free_task);
255 inm_disconnect(struct in_multi *inm)
258 struct ifmultiaddr *ifma, *ll_ifma;
261 IF_ADDR_WLOCK_ASSERT(ifp);
262 ifma = inm->inm_ifma;
265 if (ifma->ifma_flags & IFMA_F_ENQUEUED) {
266 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
267 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
269 MCDPRINTF("removed ifma: %p from %s\n", ifma, ifp->if_xname);
270 if ((ll_ifma = ifma->ifma_llifma) != NULL) {
271 MPASS(ifma != ll_ifma);
272 ifma->ifma_llifma = NULL;
273 MPASS(ll_ifma->ifma_llifma == NULL);
274 MPASS(ll_ifma->ifma_ifp == ifp);
275 if (--ll_ifma->ifma_refcount == 0) {
276 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
277 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, ifma_link);
278 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
280 MCDPRINTF("removed ll_ifma: %p from %s\n", ll_ifma, ifp->if_xname);
281 if_freemulti(ll_ifma);
287 inm_release_deferred(struct in_multi *inm)
289 struct in_multi_head tmp;
291 IN_MULTI_LIST_LOCK_ASSERT();
292 MPASS(inm->inm_refcount > 0);
293 if (--inm->inm_refcount == 0) {
296 inm->inm_ifma->ifma_protospec = NULL;
297 SLIST_INSERT_HEAD(&tmp, inm, inm_nrele);
298 inm_release_list_deferred(&tmp);
303 inm_release_task(void *arg __unused, int pending __unused)
305 struct in_multi_head inm_free_tmp;
306 struct in_multi *inm, *tinm;
308 SLIST_INIT(&inm_free_tmp);
309 mtx_lock(&in_multi_free_mtx);
310 SLIST_CONCAT(&inm_free_tmp, &inm_free_list, in_multi, inm_nrele);
311 mtx_unlock(&in_multi_free_mtx);
313 SLIST_FOREACH_SAFE(inm, &inm_free_tmp, inm_nrele, tinm) {
314 SLIST_REMOVE_HEAD(&inm_free_tmp, inm_nrele);
322 * Initialize an in_mfilter structure to a known state at t0, t1
323 * with an empty source filter list.
326 imf_init(struct in_mfilter *imf, const int st0, const int st1)
328 memset(imf, 0, sizeof(struct in_mfilter));
329 RB_INIT(&imf->imf_sources);
330 imf->imf_st[0] = st0;
331 imf->imf_st[1] = st1;
335 ip_mfilter_alloc(const int mflags, const int st0, const int st1)
337 struct in_mfilter *imf;
339 imf = malloc(sizeof(*imf), M_INMFILTER, mflags);
341 imf_init(imf, st0, st1);
347 ip_mfilter_free(struct in_mfilter *imf)
351 free(imf, M_INMFILTER);
355 * Function for looking up an in_multi record for an IPv4 multicast address
356 * on a given interface. ifp must be valid. If no record found, return NULL.
357 * The IN_MULTI_LIST_LOCK and IF_ADDR_LOCK on ifp must be held.
360 inm_lookup_locked(struct ifnet *ifp, const struct in_addr ina)
362 struct ifmultiaddr *ifma;
363 struct in_multi *inm;
365 IN_MULTI_LIST_LOCK_ASSERT();
366 IF_ADDR_LOCK_ASSERT(ifp);
368 CK_STAILQ_FOREACH(ifma, &((ifp)->if_multiaddrs), ifma_link) {
369 inm = inm_ifmultiaddr_get_inm(ifma);
372 if (inm->inm_addr.s_addr == ina.s_addr)
379 * Wrapper for inm_lookup_locked().
380 * The IF_ADDR_LOCK will be taken on ifp and released on return.
383 inm_lookup(struct ifnet *ifp, const struct in_addr ina)
385 struct epoch_tracker et;
386 struct in_multi *inm;
388 IN_MULTI_LIST_LOCK_ASSERT();
391 inm = inm_lookup_locked(ifp, ina);
398 * Find an IPv4 multicast group entry for this ip_moptions instance
399 * which matches the specified group, and optionally an interface.
400 * Return its index into the array, or -1 if not found.
402 static struct in_mfilter *
403 imo_match_group(const struct ip_moptions *imo, const struct ifnet *ifp,
404 const struct sockaddr *group)
406 const struct sockaddr_in *gsin;
407 struct in_mfilter *imf;
408 struct in_multi *inm;
410 gsin = (const struct sockaddr_in *)group;
412 IP_MFILTER_FOREACH(imf, &imo->imo_head) {
416 if ((ifp == NULL || (inm->inm_ifp == ifp)) &&
417 in_hosteq(inm->inm_addr, gsin->sin_addr)) {
425 * Find an IPv4 multicast source entry for this imo which matches
426 * the given group index for this socket, and source address.
428 * NOTE: This does not check if the entry is in-mode, merely if
429 * it exists, which may not be the desired behaviour.
431 static struct in_msource *
432 imo_match_source(struct in_mfilter *imf, const struct sockaddr *src)
434 struct ip_msource find;
435 struct ip_msource *ims;
436 const sockunion_t *psa;
438 KASSERT(src->sa_family == AF_INET, ("%s: !AF_INET", __func__));
440 /* Source trees are keyed in host byte order. */
441 psa = (const sockunion_t *)src;
442 find.ims_haddr = ntohl(psa->sin.sin_addr.s_addr);
443 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
445 return ((struct in_msource *)ims);
449 * Perform filtering for multicast datagrams on a socket by group and source.
451 * Returns 0 if a datagram should be allowed through, or various error codes
452 * if the socket was not a member of the group, or the source was muted, etc.
455 imo_multi_filter(const struct ip_moptions *imo, const struct ifnet *ifp,
456 const struct sockaddr *group, const struct sockaddr *src)
458 struct in_mfilter *imf;
459 struct in_msource *ims;
462 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
464 imf = imo_match_group(imo, ifp, group);
466 return (MCAST_NOTGMEMBER);
469 * Check if the source was included in an (S,G) join.
470 * Allow reception on exclusive memberships by default,
471 * reject reception on inclusive memberships by default.
472 * Exclude source only if an in-mode exclude filter exists.
473 * Include source only if an in-mode include filter exists.
474 * NOTE: We are comparing group state here at IGMP t1 (now)
475 * with socket-layer t0 (since last downcall).
477 mode = imf->imf_st[1];
478 ims = imo_match_source(imf, src);
480 if ((ims == NULL && mode == MCAST_INCLUDE) ||
481 (ims != NULL && ims->imsl_st[0] == MCAST_EXCLUDE))
482 return (MCAST_NOTSMEMBER);
488 * Find and return a reference to an in_multi record for (ifp, group),
489 * and bump its reference count.
490 * If one does not exist, try to allocate it, and update link-layer multicast
491 * filters on ifp to listen for group.
492 * Assumes the IN_MULTI lock is held across the call.
493 * Return 0 if successful, otherwise return an appropriate error code.
496 in_getmulti(struct ifnet *ifp, const struct in_addr *group,
497 struct in_multi **pinm)
499 struct sockaddr_in gsin;
500 struct ifmultiaddr *ifma;
501 struct in_ifinfo *ii;
502 struct in_multi *inm;
505 IN_MULTI_LOCK_ASSERT();
507 ii = (struct in_ifinfo *)ifp->if_afdata[AF_INET];
508 IN_MULTI_LIST_LOCK();
509 inm = inm_lookup(ifp, *group);
512 * If we already joined this group, just bump the
513 * refcount and return it.
515 KASSERT(inm->inm_refcount >= 1,
516 ("%s: bad refcount %d", __func__, inm->inm_refcount));
517 inm_acquire_locked(inm);
520 IN_MULTI_LIST_UNLOCK();
524 memset(&gsin, 0, sizeof(gsin));
525 gsin.sin_family = AF_INET;
526 gsin.sin_len = sizeof(struct sockaddr_in);
527 gsin.sin_addr = *group;
530 * Check if a link-layer group is already associated
531 * with this network-layer group on the given ifnet.
533 error = if_addmulti(ifp, (struct sockaddr *)&gsin, &ifma);
537 /* XXX ifma_protospec must be covered by IF_ADDR_LOCK */
538 IN_MULTI_LIST_LOCK();
542 * If something other than netinet is occupying the link-layer
543 * group, print a meaningful error message and back out of
545 * Otherwise, bump the refcount on the existing network-layer
546 * group association and return it.
548 if (ifma->ifma_protospec != NULL) {
549 inm = (struct in_multi *)ifma->ifma_protospec;
551 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
553 KASSERT(ifma->ifma_addr->sa_family == AF_INET,
554 ("%s: ifma not AF_INET", __func__));
555 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
556 if (inm->inm_ifma != ifma || inm->inm_ifp != ifp ||
557 !in_hosteq(inm->inm_addr, *group)) {
558 char addrbuf[INET_ADDRSTRLEN];
560 panic("%s: ifma %p is inconsistent with %p (%s)",
561 __func__, ifma, inm, inet_ntoa_r(*group, addrbuf));
564 inm_acquire_locked(inm);
569 IF_ADDR_WLOCK_ASSERT(ifp);
572 * A new in_multi record is needed; allocate and initialize it.
573 * We DO NOT perform an IGMP join as the in_ layer may need to
574 * push an initial source list down to IGMP to support SSM.
576 * The initial source filter state is INCLUDE, {} as per the RFC.
578 inm = malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT | M_ZERO);
580 IF_ADDR_WUNLOCK(ifp);
581 IN_MULTI_LIST_UNLOCK();
582 if_delmulti_ifma(ifma);
585 inm->inm_addr = *group;
587 inm->inm_igi = ii->ii_igmp;
588 inm->inm_ifma = ifma;
589 inm->inm_refcount = 1;
590 inm->inm_state = IGMP_NOT_MEMBER;
591 mbufq_init(&inm->inm_scq, IGMP_MAX_STATE_CHANGES);
592 inm->inm_st[0].iss_fmode = MCAST_UNDEFINED;
593 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
594 RB_INIT(&inm->inm_srcs);
596 ifma->ifma_protospec = inm;
600 IF_ADDR_WUNLOCK(ifp);
601 IN_MULTI_LIST_UNLOCK();
606 * Drop a reference to an in_multi record.
608 * If the refcount drops to 0, free the in_multi record and
609 * delete the underlying link-layer membership.
612 inm_release(struct in_multi *inm)
614 struct ifmultiaddr *ifma;
617 CTR2(KTR_IGMPV3, "%s: refcount is %d", __func__, inm->inm_refcount);
618 MPASS(inm->inm_refcount == 0);
619 CTR2(KTR_IGMPV3, "%s: freeing inm %p", __func__, inm);
621 ifma = inm->inm_ifma;
624 /* XXX this access is not covered by IF_ADDR_LOCK */
625 CTR2(KTR_IGMPV3, "%s: purging ifma %p", __func__, ifma);
627 CURVNET_SET(ifp->if_vnet);
629 free(inm, M_IPMADDR);
630 if_delmulti_ifma_flags(ifma, 1);
635 free(inm, M_IPMADDR);
636 if_delmulti_ifma_flags(ifma, 1);
641 * Clear recorded source entries for a group.
642 * Used by the IGMP code. Caller must hold the IN_MULTI lock.
643 * FIXME: Should reap.
646 inm_clear_recorded(struct in_multi *inm)
648 struct ip_msource *ims;
650 IN_MULTI_LIST_LOCK_ASSERT();
652 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
655 --inm->inm_st[1].iss_rec;
658 KASSERT(inm->inm_st[1].iss_rec == 0,
659 ("%s: iss_rec %d not 0", __func__, inm->inm_st[1].iss_rec));
663 * Record a source as pending for a Source-Group IGMPv3 query.
664 * This lives here as it modifies the shared tree.
666 * inm is the group descriptor.
667 * naddr is the address of the source to record in network-byte order.
669 * If the net.inet.igmp.sgalloc sysctl is non-zero, we will
670 * lazy-allocate a source node in response to an SG query.
671 * Otherwise, no allocation is performed. This saves some memory
672 * with the trade-off that the source will not be reported to the
673 * router if joined in the window between the query response and
674 * the group actually being joined on the local host.
676 * VIMAGE: XXX: Currently the igmp_sgalloc feature has been removed.
677 * This turns off the allocation of a recorded source entry if
678 * the group has not been joined.
680 * Return 0 if the source didn't exist or was already marked as recorded.
681 * Return 1 if the source was marked as recorded by this function.
682 * Return <0 if any error occurred (negated errno code).
685 inm_record_source(struct in_multi *inm, const in_addr_t naddr)
687 struct ip_msource find;
688 struct ip_msource *ims, *nims;
690 IN_MULTI_LIST_LOCK_ASSERT();
692 find.ims_haddr = ntohl(naddr);
693 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
694 if (ims && ims->ims_stp)
697 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
699 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
703 nims->ims_haddr = find.ims_haddr;
704 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
710 * Mark the source as recorded and update the recorded
714 ++inm->inm_st[1].iss_rec;
720 * Return a pointer to an in_msource owned by an in_mfilter,
721 * given its source address.
722 * Lazy-allocate if needed. If this is a new entry its filter state is
725 * imf is the filter set being modified.
726 * haddr is the source address in *host* byte-order.
728 * SMPng: May be called with locks held; malloc must not block.
731 imf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin,
732 struct in_msource **plims)
734 struct ip_msource find;
735 struct ip_msource *ims, *nims;
736 struct in_msource *lims;
743 /* key is host byte order */
744 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
745 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
746 lims = (struct in_msource *)ims;
748 if (imf->imf_nsrc == in_mcast_maxsocksrc)
750 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
754 lims = (struct in_msource *)nims;
755 lims->ims_haddr = find.ims_haddr;
756 lims->imsl_st[0] = MCAST_UNDEFINED;
757 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
767 * Graft a source entry into an existing socket-layer filter set,
768 * maintaining any required invariants and checking allocations.
770 * The source is marked as being in the new filter mode at t1.
772 * Return the pointer to the new node, otherwise return NULL.
774 static struct in_msource *
775 imf_graft(struct in_mfilter *imf, const uint8_t st1,
776 const struct sockaddr_in *psin)
778 struct ip_msource *nims;
779 struct in_msource *lims;
781 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
785 lims = (struct in_msource *)nims;
786 lims->ims_haddr = ntohl(psin->sin_addr.s_addr);
787 lims->imsl_st[0] = MCAST_UNDEFINED;
788 lims->imsl_st[1] = st1;
789 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
796 * Prune a source entry from an existing socket-layer filter set,
797 * maintaining any required invariants and checking allocations.
799 * The source is marked as being left at t1, it is not freed.
801 * Return 0 if no error occurred, otherwise return an errno value.
804 imf_prune(struct in_mfilter *imf, const struct sockaddr_in *psin)
806 struct ip_msource find;
807 struct ip_msource *ims;
808 struct in_msource *lims;
810 /* key is host byte order */
811 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
812 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
815 lims = (struct in_msource *)ims;
816 lims->imsl_st[1] = MCAST_UNDEFINED;
821 * Revert socket-layer filter set deltas at t1 to t0 state.
824 imf_rollback(struct in_mfilter *imf)
826 struct ip_msource *ims, *tims;
827 struct in_msource *lims;
829 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
830 lims = (struct in_msource *)ims;
831 if (lims->imsl_st[0] == lims->imsl_st[1]) {
832 /* no change at t1 */
834 } else if (lims->imsl_st[0] != MCAST_UNDEFINED) {
835 /* revert change to existing source at t1 */
836 lims->imsl_st[1] = lims->imsl_st[0];
838 /* revert source added t1 */
839 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
840 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
841 free(ims, M_INMFILTER);
845 imf->imf_st[1] = imf->imf_st[0];
849 * Mark socket-layer filter set as INCLUDE {} at t1.
852 imf_leave(struct in_mfilter *imf)
854 struct ip_msource *ims;
855 struct in_msource *lims;
857 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
858 lims = (struct in_msource *)ims;
859 lims->imsl_st[1] = MCAST_UNDEFINED;
861 imf->imf_st[1] = MCAST_INCLUDE;
865 * Mark socket-layer filter set deltas as committed.
868 imf_commit(struct in_mfilter *imf)
870 struct ip_msource *ims;
871 struct in_msource *lims;
873 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
874 lims = (struct in_msource *)ims;
875 lims->imsl_st[0] = lims->imsl_st[1];
877 imf->imf_st[0] = imf->imf_st[1];
881 * Reap unreferenced sources from socket-layer filter set.
884 imf_reap(struct in_mfilter *imf)
886 struct ip_msource *ims, *tims;
887 struct in_msource *lims;
889 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
890 lims = (struct in_msource *)ims;
891 if ((lims->imsl_st[0] == MCAST_UNDEFINED) &&
892 (lims->imsl_st[1] == MCAST_UNDEFINED)) {
893 CTR2(KTR_IGMPV3, "%s: free lims %p", __func__, ims);
894 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
895 free(ims, M_INMFILTER);
902 * Purge socket-layer filter set.
905 imf_purge(struct in_mfilter *imf)
907 struct ip_msource *ims, *tims;
909 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
910 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
911 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
912 free(ims, M_INMFILTER);
915 imf->imf_st[0] = imf->imf_st[1] = MCAST_UNDEFINED;
916 KASSERT(RB_EMPTY(&imf->imf_sources),
917 ("%s: imf_sources not empty", __func__));
921 * Look up a source filter entry for a multicast group.
923 * inm is the group descriptor to work with.
924 * haddr is the host-byte-order IPv4 address to look up.
925 * noalloc may be non-zero to suppress allocation of sources.
926 * *pims will be set to the address of the retrieved or allocated source.
928 * SMPng: NOTE: may be called with locks held.
929 * Return 0 if successful, otherwise return a non-zero error code.
932 inm_get_source(struct in_multi *inm, const in_addr_t haddr,
933 const int noalloc, struct ip_msource **pims)
935 struct ip_msource find;
936 struct ip_msource *ims, *nims;
938 find.ims_haddr = haddr;
939 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
940 if (ims == NULL && !noalloc) {
941 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
943 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
947 nims->ims_haddr = haddr;
948 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
952 CTR3(KTR_IGMPV3, "%s: allocated 0x%08x as %p", __func__,
962 * Merge socket-layer source into IGMP-layer source.
963 * If rollback is non-zero, perform the inverse of the merge.
966 ims_merge(struct ip_msource *ims, const struct in_msource *lims,
969 int n = rollback ? -1 : 1;
971 if (lims->imsl_st[0] == MCAST_EXCLUDE) {
972 CTR3(KTR_IGMPV3, "%s: t1 ex -= %d on 0x%08x",
973 __func__, n, ims->ims_haddr);
974 ims->ims_st[1].ex -= n;
975 } else if (lims->imsl_st[0] == MCAST_INCLUDE) {
976 CTR3(KTR_IGMPV3, "%s: t1 in -= %d on 0x%08x",
977 __func__, n, ims->ims_haddr);
978 ims->ims_st[1].in -= n;
981 if (lims->imsl_st[1] == MCAST_EXCLUDE) {
982 CTR3(KTR_IGMPV3, "%s: t1 ex += %d on 0x%08x",
983 __func__, n, ims->ims_haddr);
984 ims->ims_st[1].ex += n;
985 } else if (lims->imsl_st[1] == MCAST_INCLUDE) {
986 CTR3(KTR_IGMPV3, "%s: t1 in += %d on 0x%08x",
987 __func__, n, ims->ims_haddr);
988 ims->ims_st[1].in += n;
993 * Atomically update the global in_multi state, when a membership's
994 * filter list is being updated in any way.
996 * imf is the per-inpcb-membership group filter pointer.
997 * A fake imf may be passed for in-kernel consumers.
999 * XXX This is a candidate for a set-symmetric-difference style loop
1000 * which would eliminate the repeated lookup from root of ims nodes,
1001 * as they share the same key space.
1003 * If any error occurred this function will back out of refcounts
1004 * and return a non-zero value.
1007 inm_merge(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1009 struct ip_msource *ims, *nims;
1010 struct in_msource *lims;
1011 int schanged, error;
1017 IN_MULTI_LIST_LOCK_ASSERT();
1020 * Update the source filters first, as this may fail.
1021 * Maintain count of in-mode filters at t0, t1. These are
1022 * used to work out if we transition into ASM mode or not.
1023 * Maintain a count of source filters whose state was
1024 * actually modified by this operation.
1026 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
1027 lims = (struct in_msource *)ims;
1028 if (lims->imsl_st[0] == imf->imf_st[0]) nsrc0++;
1029 if (lims->imsl_st[1] == imf->imf_st[1]) nsrc1++;
1030 if (lims->imsl_st[0] == lims->imsl_st[1]) continue;
1031 error = inm_get_source(inm, lims->ims_haddr, 0, &nims);
1035 ims_merge(nims, lims, 0);
1038 struct ip_msource *bims;
1040 RB_FOREACH_REVERSE_FROM(ims, ip_msource_tree, nims) {
1041 lims = (struct in_msource *)ims;
1042 if (lims->imsl_st[0] == lims->imsl_st[1])
1044 (void)inm_get_source(inm, lims->ims_haddr, 1, &bims);
1047 ims_merge(bims, lims, 1);
1052 CTR3(KTR_IGMPV3, "%s: imf filters in-mode: %d at t0, %d at t1",
1053 __func__, nsrc0, nsrc1);
1055 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1056 if (imf->imf_st[0] == imf->imf_st[1] &&
1057 imf->imf_st[1] == MCAST_INCLUDE) {
1059 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
1060 --inm->inm_st[1].iss_in;
1064 /* Handle filter mode transition on socket. */
1065 if (imf->imf_st[0] != imf->imf_st[1]) {
1066 CTR3(KTR_IGMPV3, "%s: imf transition %d to %d",
1067 __func__, imf->imf_st[0], imf->imf_st[1]);
1069 if (imf->imf_st[0] == MCAST_EXCLUDE) {
1070 CTR1(KTR_IGMPV3, "%s: --ex on inm at t1", __func__);
1071 --inm->inm_st[1].iss_ex;
1072 } else if (imf->imf_st[0] == MCAST_INCLUDE) {
1073 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
1074 --inm->inm_st[1].iss_in;
1077 if (imf->imf_st[1] == MCAST_EXCLUDE) {
1078 CTR1(KTR_IGMPV3, "%s: ex++ on inm at t1", __func__);
1079 inm->inm_st[1].iss_ex++;
1080 } else if (imf->imf_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
1081 CTR1(KTR_IGMPV3, "%s: in++ on inm at t1", __func__);
1082 inm->inm_st[1].iss_in++;
1087 * Track inm filter state in terms of listener counts.
1088 * If there are any exclusive listeners, stack-wide
1089 * membership is exclusive.
1090 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1091 * If no listeners remain, state is undefined at t1,
1092 * and the IGMP lifecycle for this group should finish.
1094 if (inm->inm_st[1].iss_ex > 0) {
1095 CTR1(KTR_IGMPV3, "%s: transition to EX", __func__);
1096 inm->inm_st[1].iss_fmode = MCAST_EXCLUDE;
1097 } else if (inm->inm_st[1].iss_in > 0) {
1098 CTR1(KTR_IGMPV3, "%s: transition to IN", __func__);
1099 inm->inm_st[1].iss_fmode = MCAST_INCLUDE;
1101 CTR1(KTR_IGMPV3, "%s: transition to UNDEF", __func__);
1102 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
1105 /* Decrement ASM listener count on transition out of ASM mode. */
1106 if (imf->imf_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1107 if ((imf->imf_st[1] != MCAST_EXCLUDE) ||
1108 (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 > 0)) {
1109 CTR1(KTR_IGMPV3, "%s: --asm on inm at t1", __func__);
1110 --inm->inm_st[1].iss_asm;
1114 /* Increment ASM listener count on transition to ASM mode. */
1115 if (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1116 CTR1(KTR_IGMPV3, "%s: asm++ on inm at t1", __func__);
1117 inm->inm_st[1].iss_asm++;
1120 CTR3(KTR_IGMPV3, "%s: merged imf %p to inm %p", __func__, imf, inm);
1125 CTR1(KTR_IGMPV3, "%s: sources changed; reaping", __func__);
1132 * Mark an in_multi's filter set deltas as committed.
1133 * Called by IGMP after a state change has been enqueued.
1136 inm_commit(struct in_multi *inm)
1138 struct ip_msource *ims;
1140 CTR2(KTR_IGMPV3, "%s: commit inm %p", __func__, inm);
1141 CTR1(KTR_IGMPV3, "%s: pre commit:", __func__);
1144 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
1145 ims->ims_st[0] = ims->ims_st[1];
1147 inm->inm_st[0] = inm->inm_st[1];
1151 * Reap unreferenced nodes from an in_multi's filter set.
1154 inm_reap(struct in_multi *inm)
1156 struct ip_msource *ims, *tims;
1158 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1159 if (ims->ims_st[0].ex > 0 || ims->ims_st[0].in > 0 ||
1160 ims->ims_st[1].ex > 0 || ims->ims_st[1].in > 0 ||
1163 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1164 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1165 free(ims, M_IPMSOURCE);
1171 * Purge all source nodes from an in_multi's filter set.
1174 inm_purge(struct in_multi *inm)
1176 struct ip_msource *ims, *tims;
1178 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1179 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1180 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1181 free(ims, M_IPMSOURCE);
1187 * Join a multicast group; unlocked entry point.
1189 * SMPng: XXX: in_joingroup() is called from in_control() when Giant
1190 * is not held. Fortunately, ifp is unlikely to have been detached
1191 * at this point, so we assume it's OK to recurse.
1194 in_joingroup(struct ifnet *ifp, const struct in_addr *gina,
1195 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1200 error = in_joingroup_locked(ifp, gina, imf, pinm);
1207 * Join a multicast group; real entry point.
1209 * Only preserves atomicity at inm level.
1210 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1212 * If the IGMP downcall fails, the group is not joined, and an error
1216 in_joingroup_locked(struct ifnet *ifp, const struct in_addr *gina,
1217 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1219 struct in_mfilter timf;
1220 struct in_multi *inm;
1223 IN_MULTI_LOCK_ASSERT();
1224 IN_MULTI_LIST_UNLOCK_ASSERT();
1226 CTR4(KTR_IGMPV3, "%s: join 0x%08x on %p(%s))", __func__,
1227 ntohl(gina->s_addr), ifp, ifp->if_xname);
1233 * If no imf was specified (i.e. kernel consumer),
1234 * fake one up and assume it is an ASM join.
1237 imf_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1241 error = in_getmulti(ifp, gina, &inm);
1243 CTR1(KTR_IGMPV3, "%s: in_getmulti() failure", __func__);
1246 IN_MULTI_LIST_LOCK();
1247 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1248 error = inm_merge(inm, imf);
1250 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1251 goto out_inm_release;
1254 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1255 error = igmp_change_state(inm);
1257 CTR1(KTR_IGMPV3, "%s: failed to update source", __func__);
1258 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*/
1359 * Block or unblock an ASM multicast source on an inpcb.
1360 * This implements the delta-based API described in RFC 3678.
1362 * The delta-based API applies only to exclusive-mode memberships.
1363 * An IGMP downcall will be performed.
1365 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1367 * Return 0 if successful, otherwise return an appropriate error code.
1370 inp_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1372 struct epoch_tracker et;
1373 struct group_source_req gsr;
1374 sockunion_t *gsa, *ssa;
1376 struct in_mfilter *imf;
1377 struct ip_moptions *imo;
1378 struct in_msource *ims;
1379 struct in_multi *inm;
1387 memset(&gsr, 0, sizeof(struct group_source_req));
1388 gsa = (sockunion_t *)&gsr.gsr_group;
1389 ssa = (sockunion_t *)&gsr.gsr_source;
1391 switch (sopt->sopt_name) {
1392 case IP_BLOCK_SOURCE:
1393 case IP_UNBLOCK_SOURCE: {
1394 struct ip_mreq_source mreqs;
1396 error = sooptcopyin(sopt, &mreqs,
1397 sizeof(struct ip_mreq_source),
1398 sizeof(struct ip_mreq_source));
1402 gsa->sin.sin_family = AF_INET;
1403 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1404 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1406 ssa->sin.sin_family = AF_INET;
1407 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1408 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1410 if (!in_nullhost(mreqs.imr_interface)) {
1411 NET_EPOCH_ENTER(et);
1412 INADDR_TO_IFP(mreqs.imr_interface, ifp);
1416 if (sopt->sopt_name == IP_BLOCK_SOURCE)
1419 CTR3(KTR_IGMPV3, "%s: imr_interface = 0x%08x, ifp = %p",
1420 __func__, ntohl(mreqs.imr_interface.s_addr), ifp);
1424 case MCAST_BLOCK_SOURCE:
1425 case MCAST_UNBLOCK_SOURCE:
1426 error = sooptcopyin(sopt, &gsr,
1427 sizeof(struct group_source_req),
1428 sizeof(struct group_source_req));
1432 if (gsa->sin.sin_family != AF_INET ||
1433 gsa->sin.sin_len != sizeof(struct sockaddr_in))
1436 if (ssa->sin.sin_family != AF_INET ||
1437 ssa->sin.sin_len != sizeof(struct sockaddr_in))
1440 NET_EPOCH_ENTER(et);
1441 ifp = ifnet_byindex(gsr.gsr_interface);
1444 return (EADDRNOTAVAIL);
1446 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1451 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
1452 __func__, sopt->sopt_name);
1453 return (EOPNOTSUPP);
1457 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1463 * Check if we are actually a member of this group.
1465 imo = inp_findmoptions(inp);
1466 imf = imo_match_group(imo, ifp, &gsa->sa);
1468 error = EADDRNOTAVAIL;
1469 goto out_inp_locked;
1474 * Attempting to use the delta-based API on an
1475 * non exclusive-mode membership is an error.
1477 fmode = imf->imf_st[0];
1478 if (fmode != MCAST_EXCLUDE) {
1480 goto out_inp_locked;
1484 * Deal with error cases up-front:
1485 * Asked to block, but already blocked; or
1486 * Asked to unblock, but nothing to unblock.
1487 * If adding a new block entry, allocate it.
1489 ims = imo_match_source(imf, &ssa->sa);
1490 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1491 CTR3(KTR_IGMPV3, "%s: source 0x%08x %spresent", __func__,
1492 ntohl(ssa->sin.sin_addr.s_addr), doblock ? "" : "not ");
1493 error = EADDRNOTAVAIL;
1494 goto out_inp_locked;
1497 INP_WLOCK_ASSERT(inp);
1500 * Begin state merge transaction at socket layer.
1503 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
1504 ims = imf_graft(imf, fmode, &ssa->sin);
1508 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
1509 error = imf_prune(imf, &ssa->sin);
1513 CTR1(KTR_IGMPV3, "%s: merge imf state failed", __func__);
1514 goto out_imf_rollback;
1518 * Begin state merge transaction at IGMP layer.
1520 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1521 IN_MULTI_LIST_LOCK();
1522 error = inm_merge(inm, imf);
1524 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1525 IN_MULTI_LIST_UNLOCK();
1526 goto out_imf_rollback;
1529 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1530 error = igmp_change_state(inm);
1531 IN_MULTI_LIST_UNLOCK();
1533 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1550 * Given an inpcb, return its multicast options structure pointer. Accepts
1551 * an unlocked inpcb pointer, but will return it locked. May sleep.
1553 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1554 * SMPng: NOTE: Returns with the INP write lock held.
1556 static struct ip_moptions *
1557 inp_findmoptions(struct inpcb *inp)
1559 struct ip_moptions *imo;
1562 if (inp->inp_moptions != NULL)
1563 return (inp->inp_moptions);
1567 imo = malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK);
1569 imo->imo_multicast_ifp = NULL;
1570 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1571 imo->imo_multicast_vif = -1;
1572 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1573 imo->imo_multicast_loop = in_mcast_loop;
1574 STAILQ_INIT(&imo->imo_head);
1577 if (inp->inp_moptions != NULL) {
1578 free(imo, M_IPMOPTS);
1579 return (inp->inp_moptions);
1581 inp->inp_moptions = imo;
1586 inp_freemoptions(struct ip_moptions *imo)
1588 struct in_mfilter *imf;
1589 struct in_multi *inm;
1595 while ((imf = ip_mfilter_first(&imo->imo_head)) != NULL) {
1596 ip_mfilter_remove(&imo->imo_head, imf);
1599 if ((inm = imf->imf_inm) != NULL) {
1600 if ((ifp = inm->inm_ifp) != NULL) {
1601 CURVNET_SET(ifp->if_vnet);
1602 (void)in_leavegroup(inm, imf);
1605 (void)in_leavegroup(inm, imf);
1608 ip_mfilter_free(imf);
1610 free(imo, M_IPMOPTS);
1614 * Atomically get source filters on a socket for an IPv4 multicast group.
1615 * Called with INP lock held; returns with lock released.
1618 inp_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1620 struct epoch_tracker et;
1621 struct __msfilterreq msfr;
1624 struct ip_moptions *imo;
1625 struct in_mfilter *imf;
1626 struct ip_msource *ims;
1627 struct in_msource *lims;
1628 struct sockaddr_in *psin;
1629 struct sockaddr_storage *ptss;
1630 struct sockaddr_storage *tss;
1632 size_t nsrcs, ncsrcs;
1634 INP_WLOCK_ASSERT(inp);
1636 imo = inp->inp_moptions;
1637 KASSERT(imo != NULL, ("%s: null ip_moptions", __func__));
1641 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1642 sizeof(struct __msfilterreq));
1646 NET_EPOCH_ENTER(et);
1647 ifp = ifnet_byindex(msfr.msfr_ifindex);
1648 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifnet pointer left */
1655 * Lookup group on the socket.
1657 gsa = (sockunion_t *)&msfr.msfr_group;
1658 imf = imo_match_group(imo, ifp, &gsa->sa);
1661 return (EADDRNOTAVAIL);
1665 * Ignore memberships which are in limbo.
1667 if (imf->imf_st[1] == MCAST_UNDEFINED) {
1671 msfr.msfr_fmode = imf->imf_st[1];
1674 * If the user specified a buffer, copy out the source filter
1675 * entries to userland gracefully.
1676 * We only copy out the number of entries which userland
1677 * has asked for, but we always tell userland how big the
1678 * buffer really needs to be.
1680 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
1681 msfr.msfr_nsrcs = in_mcast_maxsocksrc;
1683 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1684 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1685 M_TEMP, M_NOWAIT | M_ZERO);
1693 * Count number of sources in-mode at t0.
1694 * If buffer space exists and remains, copy out source entries.
1696 nsrcs = msfr.msfr_nsrcs;
1699 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
1700 lims = (struct in_msource *)ims;
1701 if (lims->imsl_st[0] == MCAST_UNDEFINED ||
1702 lims->imsl_st[0] != imf->imf_st[0])
1705 if (tss != NULL && nsrcs > 0) {
1706 psin = (struct sockaddr_in *)ptss;
1707 psin->sin_family = AF_INET;
1708 psin->sin_len = sizeof(struct sockaddr_in);
1709 psin->sin_addr.s_addr = htonl(lims->ims_haddr);
1719 error = copyout(tss, msfr.msfr_srcs,
1720 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1726 msfr.msfr_nsrcs = ncsrcs;
1727 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1733 * Return the IP multicast options in response to user getsockopt().
1736 inp_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1738 struct ip_mreqn mreqn;
1739 struct ip_moptions *imo;
1741 struct in_ifaddr *ia;
1746 imo = inp->inp_moptions;
1747 /* If socket is neither of type SOCK_RAW or SOCK_DGRAM reject it. */
1748 if (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1749 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM) {
1751 return (EOPNOTSUPP);
1755 switch (sopt->sopt_name) {
1756 case IP_MULTICAST_VIF:
1758 optval = imo->imo_multicast_vif;
1762 error = sooptcopyout(sopt, &optval, sizeof(int));
1765 case IP_MULTICAST_IF:
1766 memset(&mreqn, 0, sizeof(struct ip_mreqn));
1768 ifp = imo->imo_multicast_ifp;
1769 if (!in_nullhost(imo->imo_multicast_addr)) {
1770 mreqn.imr_address = imo->imo_multicast_addr;
1771 } else if (ifp != NULL) {
1772 struct epoch_tracker et;
1774 mreqn.imr_ifindex = ifp->if_index;
1775 NET_EPOCH_ENTER(et);
1779 IA_SIN(ia)->sin_addr;
1784 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
1785 error = sooptcopyout(sopt, &mreqn,
1786 sizeof(struct ip_mreqn));
1788 error = sooptcopyout(sopt, &mreqn.imr_address,
1789 sizeof(struct in_addr));
1793 case IP_MULTICAST_TTL:
1795 optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1797 optval = coptval = imo->imo_multicast_ttl;
1799 if (sopt->sopt_valsize == sizeof(u_char))
1800 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1802 error = sooptcopyout(sopt, &optval, sizeof(int));
1805 case IP_MULTICAST_LOOP:
1807 optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1809 optval = coptval = imo->imo_multicast_loop;
1811 if (sopt->sopt_valsize == sizeof(u_char))
1812 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1814 error = sooptcopyout(sopt, &optval, sizeof(int));
1819 error = EADDRNOTAVAIL;
1822 error = inp_get_source_filters(inp, sopt);
1828 error = ENOPROTOOPT;
1832 INP_UNLOCK_ASSERT(inp);
1838 * Look up the ifnet to use for a multicast group membership,
1839 * given the IPv4 address of an interface, and the IPv4 group address.
1841 * This routine exists to support legacy multicast applications
1842 * which do not understand that multicast memberships are scoped to
1843 * specific physical links in the networking stack, or which need
1844 * to join link-scope groups before IPv4 addresses are configured.
1846 * Use this socket's current FIB number for any required FIB lookup.
1847 * If ina is INADDR_ANY, look up the group address in the unicast FIB,
1848 * and use its ifp; usually, this points to the default next-hop.
1850 * If the FIB lookup fails, attempt to use the first non-loopback
1851 * interface with multicast capability in the system as a
1852 * last resort. The legacy IPv4 ASM API requires that we do
1853 * this in order to allow groups to be joined when the routing
1854 * table has not yet been populated during boot.
1856 * Returns NULL if no ifp could be found, otherwise return referenced ifp.
1858 * FUTURE: Implement IPv4 source-address selection.
1860 static struct ifnet *
1861 inp_lookup_mcast_ifp(const struct inpcb *inp,
1862 const struct sockaddr_in *gsin, const struct in_addr ina)
1865 struct nhop_object *nh;
1868 KASSERT(inp != NULL, ("%s: inp must not be NULL", __func__));
1869 KASSERT(gsin->sin_family == AF_INET, ("%s: not AF_INET", __func__));
1870 KASSERT(IN_MULTICAST(ntohl(gsin->sin_addr.s_addr)),
1871 ("%s: not multicast", __func__));
1874 if (!in_nullhost(ina)) {
1875 INADDR_TO_IFP(ina, ifp);
1879 nh = fib4_lookup(inp->inp_inc.inc_fibnum, gsin->sin_addr, 0, NHR_NONE, 0);
1884 struct in_ifaddr *ia;
1888 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1890 if (!(mifp->if_flags & IFF_LOOPBACK) &&
1891 (mifp->if_flags & IFF_MULTICAST)) {
1904 * Join an IPv4 multicast group, possibly with a source.
1907 inp_join_group(struct inpcb *inp, struct sockopt *sopt)
1909 struct group_source_req gsr;
1910 sockunion_t *gsa, *ssa;
1912 struct in_mfilter *imf;
1913 struct ip_moptions *imo;
1914 struct in_multi *inm;
1915 struct in_msource *lims;
1916 struct epoch_tracker et;
1923 memset(&gsr, 0, sizeof(struct group_source_req));
1924 gsa = (sockunion_t *)&gsr.gsr_group;
1925 gsa->ss.ss_family = AF_UNSPEC;
1926 ssa = (sockunion_t *)&gsr.gsr_source;
1927 ssa->ss.ss_family = AF_UNSPEC;
1929 switch (sopt->sopt_name) {
1930 case IP_ADD_MEMBERSHIP: {
1931 struct ip_mreqn mreqn;
1933 if (sopt->sopt_valsize == sizeof(struct ip_mreqn))
1934 error = sooptcopyin(sopt, &mreqn,
1935 sizeof(struct ip_mreqn), sizeof(struct ip_mreqn));
1937 error = sooptcopyin(sopt, &mreqn,
1938 sizeof(struct ip_mreq), sizeof(struct ip_mreq));
1942 gsa->sin.sin_family = AF_INET;
1943 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1944 gsa->sin.sin_addr = mreqn.imr_multiaddr;
1945 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1948 NET_EPOCH_ENTER(et);
1949 if (sopt->sopt_valsize == sizeof(struct ip_mreqn) &&
1950 mreqn.imr_ifindex != 0)
1951 ifp = ifnet_byindex_ref(mreqn.imr_ifindex);
1953 ifp = inp_lookup_mcast_ifp(inp, &gsa->sin,
1958 case IP_ADD_SOURCE_MEMBERSHIP: {
1959 struct ip_mreq_source mreqs;
1961 error = sooptcopyin(sopt, &mreqs, sizeof(struct ip_mreq_source),
1962 sizeof(struct ip_mreq_source));
1966 gsa->sin.sin_family = ssa->sin.sin_family = AF_INET;
1967 gsa->sin.sin_len = ssa->sin.sin_len =
1968 sizeof(struct sockaddr_in);
1970 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1971 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1974 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1976 NET_EPOCH_ENTER(et);
1977 ifp = inp_lookup_mcast_ifp(inp, &gsa->sin,
1978 mreqs.imr_interface);
1980 CTR3(KTR_IGMPV3, "%s: imr_interface = 0x%08x, ifp = %p",
1981 __func__, ntohl(mreqs.imr_interface.s_addr), ifp);
1985 case MCAST_JOIN_GROUP:
1986 case MCAST_JOIN_SOURCE_GROUP:
1987 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1988 error = sooptcopyin(sopt, &gsr,
1989 sizeof(struct group_req),
1990 sizeof(struct group_req));
1991 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1992 error = sooptcopyin(sopt, &gsr,
1993 sizeof(struct group_source_req),
1994 sizeof(struct group_source_req));
1999 if (gsa->sin.sin_family != AF_INET ||
2000 gsa->sin.sin_len != sizeof(struct sockaddr_in))
2004 * Overwrite the port field if present, as the sockaddr
2005 * being copied in may be matched with a binary comparison.
2007 gsa->sin.sin_port = 0;
2008 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
2009 if (ssa->sin.sin_family != AF_INET ||
2010 ssa->sin.sin_len != sizeof(struct sockaddr_in))
2012 ssa->sin.sin_port = 0;
2015 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2018 NET_EPOCH_ENTER(et);
2019 ifp = ifnet_byindex_ref(gsr.gsr_interface);
2022 return (EADDRNOTAVAIL);
2026 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2027 __func__, sopt->sopt_name);
2028 return (EOPNOTSUPP);
2032 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2035 return (EADDRNOTAVAIL);
2041 * Find the membership in the membership list.
2043 imo = inp_findmoptions(inp);
2044 imf = imo_match_group(imo, ifp, &gsa->sa);
2049 if (ip_mfilter_count(&imo->imo_head) >= IP_MAX_MEMBERSHIPS) {
2051 goto out_inp_locked;
2057 if (ssa->ss.ss_family != AF_UNSPEC) {
2059 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2060 * is an error. On an existing inclusive membership,
2061 * it just adds the source to the filter list.
2063 if (imf->imf_st[1] != MCAST_INCLUDE) {
2065 goto out_inp_locked;
2068 * Throw out duplicates.
2070 * XXX FIXME: This makes a naive assumption that
2071 * even if entries exist for *ssa in this imf,
2072 * they will be rejected as dupes, even if they
2073 * are not valid in the current mode (in-mode).
2075 * in_msource is transactioned just as for anything
2076 * else in SSM -- but note naive use of inm_graft()
2077 * below for allocating new filter entries.
2079 * This is only an issue if someone mixes the
2080 * full-state SSM API with the delta-based API,
2081 * which is discouraged in the relevant RFCs.
2083 lims = imo_match_source(imf, &ssa->sa);
2084 if (lims != NULL /*&&
2085 lims->imsl_st[1] == MCAST_INCLUDE*/) {
2086 error = EADDRNOTAVAIL;
2087 goto out_inp_locked;
2091 * MCAST_JOIN_GROUP on an existing exclusive
2092 * membership is an error; return EADDRINUSE
2093 * to preserve 4.4BSD API idempotence, and
2094 * avoid tedious detour to code below.
2095 * NOTE: This is bending RFC 3678 a bit.
2097 * On an existing inclusive membership, this is also
2098 * an error; if you want to change filter mode,
2099 * you must use the userland API setsourcefilter().
2100 * XXX We don't reject this for imf in UNDEFINED
2101 * state at t1, because allocation of a filter
2102 * is atomic with allocation of a membership.
2105 if (imf->imf_st[1] == MCAST_EXCLUDE)
2107 goto out_inp_locked;
2112 * Begin state merge transaction at socket layer.
2114 INP_WLOCK_ASSERT(inp);
2117 * Graft new source into filter list for this inpcb's
2118 * membership of the group. The in_multi may not have
2119 * been allocated yet if this is a new membership, however,
2120 * the in_mfilter slot will be allocated and must be initialized.
2122 * Note: Grafting of exclusive mode filters doesn't happen
2124 * XXX: Should check for non-NULL lims (node exists but may
2125 * not be in-mode) for interop with full-state API.
2127 if (ssa->ss.ss_family != AF_UNSPEC) {
2128 /* Membership starts in IN mode */
2130 CTR1(KTR_IGMPV3, "%s: new join w/source", __func__);
2131 imf = ip_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_INCLUDE);
2134 goto out_inp_locked;
2137 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
2139 lims = imf_graft(imf, MCAST_INCLUDE, &ssa->sin);
2141 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2144 goto out_inp_locked;
2147 /* No address specified; Membership starts in EX mode */
2149 CTR1(KTR_IGMPV3, "%s: new join w/o source", __func__);
2150 imf = ip_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_EXCLUDE);
2153 goto out_inp_locked;
2159 * Begin state merge transaction at IGMP layer.
2165 error = in_joingroup_locked(ifp, &gsa->sin.sin_addr, imf,
2169 if (in_pcbrele_wlocked(inp)) {
2171 goto out_inp_unlocked;
2174 CTR1(KTR_IGMPV3, "%s: in_joingroup_locked failed",
2176 goto out_inp_locked;
2179 * NOTE: Refcount from in_joingroup_locked()
2180 * is protecting membership.
2182 ip_mfilter_insert(&imo->imo_head, imf);
2184 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2185 IN_MULTI_LIST_LOCK();
2186 error = inm_merge(inm, imf);
2188 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2190 IN_MULTI_LIST_UNLOCK();
2193 goto out_inp_locked;
2195 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2196 error = igmp_change_state(inm);
2197 IN_MULTI_LIST_UNLOCK();
2199 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2203 goto out_inp_locked;
2215 if (is_new && imf) {
2216 if (imf->imf_inm != NULL) {
2217 IN_MULTI_LIST_LOCK();
2219 inm_release_deferred(imf->imf_inm);
2220 IF_ADDR_WUNLOCK(ifp);
2221 IN_MULTI_LIST_UNLOCK();
2223 ip_mfilter_free(imf);
2230 * Leave an IPv4 multicast group on an inpcb, possibly with a source.
2233 inp_leave_group(struct inpcb *inp, struct sockopt *sopt)
2235 struct epoch_tracker et;
2236 struct group_source_req gsr;
2237 struct ip_mreq_source mreqs;
2238 sockunion_t *gsa, *ssa;
2240 struct in_mfilter *imf;
2241 struct ip_moptions *imo;
2242 struct in_msource *ims;
2243 struct in_multi *inm;
2251 memset(&gsr, 0, sizeof(struct group_source_req));
2252 gsa = (sockunion_t *)&gsr.gsr_group;
2253 gsa->ss.ss_family = AF_UNSPEC;
2254 ssa = (sockunion_t *)&gsr.gsr_source;
2255 ssa->ss.ss_family = AF_UNSPEC;
2257 switch (sopt->sopt_name) {
2258 case IP_DROP_MEMBERSHIP:
2259 case IP_DROP_SOURCE_MEMBERSHIP:
2260 if (sopt->sopt_name == IP_DROP_MEMBERSHIP) {
2261 error = sooptcopyin(sopt, &mreqs,
2262 sizeof(struct ip_mreq),
2263 sizeof(struct ip_mreq));
2265 * Swap interface and sourceaddr arguments,
2266 * as ip_mreq and ip_mreq_source are laid
2269 mreqs.imr_interface = mreqs.imr_sourceaddr;
2270 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
2271 } else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2272 error = sooptcopyin(sopt, &mreqs,
2273 sizeof(struct ip_mreq_source),
2274 sizeof(struct ip_mreq_source));
2279 gsa->sin.sin_family = AF_INET;
2280 gsa->sin.sin_len = sizeof(struct sockaddr_in);
2281 gsa->sin.sin_addr = mreqs.imr_multiaddr;
2283 if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2284 ssa->sin.sin_family = AF_INET;
2285 ssa->sin.sin_len = sizeof(struct sockaddr_in);
2286 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
2290 * Attempt to look up hinted ifp from interface address.
2291 * Fallthrough with null ifp iff lookup fails, to
2292 * preserve 4.4BSD mcast API idempotence.
2293 * XXX NOTE WELL: The RFC 3678 API is preferred because
2294 * using an IPv4 address as a key is racy.
2296 if (!in_nullhost(mreqs.imr_interface)) {
2297 NET_EPOCH_ENTER(et);
2298 INADDR_TO_IFP(mreqs.imr_interface, ifp);
2302 CTR3(KTR_IGMPV3, "%s: imr_interface = 0x%08x, ifp = %p",
2303 __func__, ntohl(mreqs.imr_interface.s_addr), ifp);
2307 case MCAST_LEAVE_GROUP:
2308 case MCAST_LEAVE_SOURCE_GROUP:
2309 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2310 error = sooptcopyin(sopt, &gsr,
2311 sizeof(struct group_req),
2312 sizeof(struct group_req));
2313 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2314 error = sooptcopyin(sopt, &gsr,
2315 sizeof(struct group_source_req),
2316 sizeof(struct group_source_req));
2321 if (gsa->sin.sin_family != AF_INET ||
2322 gsa->sin.sin_len != sizeof(struct sockaddr_in))
2325 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2326 if (ssa->sin.sin_family != AF_INET ||
2327 ssa->sin.sin_len != sizeof(struct sockaddr_in))
2331 NET_EPOCH_ENTER(et);
2332 ifp = ifnet_byindex(gsr.gsr_interface);
2333 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */
2335 return (EADDRNOTAVAIL);
2339 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2340 __func__, sopt->sopt_name);
2341 return (EOPNOTSUPP);
2345 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2351 * Find the membership in the membership list.
2353 imo = inp_findmoptions(inp);
2354 imf = imo_match_group(imo, ifp, &gsa->sa);
2356 error = EADDRNOTAVAIL;
2357 goto out_inp_locked;
2361 if (ssa->ss.ss_family != AF_UNSPEC)
2365 * Begin state merge transaction at socket layer.
2367 INP_WLOCK_ASSERT(inp);
2370 * If we were instructed only to leave a given source, do so.
2371 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2374 ip_mfilter_remove(&imo->imo_head, imf);
2378 * Give up the multicast address record to which
2379 * the membership points.
2381 (void) in_leavegroup_locked(imf->imf_inm, imf);
2383 if (imf->imf_st[0] == MCAST_EXCLUDE) {
2384 error = EADDRNOTAVAIL;
2385 goto out_inp_locked;
2387 ims = imo_match_source(imf, &ssa->sa);
2389 CTR3(KTR_IGMPV3, "%s: source 0x%08x %spresent",
2390 __func__, ntohl(ssa->sin.sin_addr.s_addr), "not ");
2391 error = EADDRNOTAVAIL;
2392 goto out_inp_locked;
2394 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
2395 error = imf_prune(imf, &ssa->sin);
2397 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2399 goto out_inp_locked;
2404 * Begin state merge transaction at IGMP layer.
2407 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2408 IN_MULTI_LIST_LOCK();
2409 error = inm_merge(inm, imf);
2411 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2413 IN_MULTI_LIST_UNLOCK();
2416 goto out_inp_locked;
2419 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2420 error = igmp_change_state(inm);
2421 IN_MULTI_LIST_UNLOCK();
2423 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2427 goto out_inp_locked;
2436 if (is_final && imf)
2437 ip_mfilter_free(imf);
2444 * Select the interface for transmitting IPv4 multicast datagrams.
2446 * Either an instance of struct in_addr or an instance of struct ip_mreqn
2447 * may be passed to this socket option. An address of INADDR_ANY or an
2448 * interface index of 0 is used to remove a previous selection.
2449 * When no interface is selected, one is chosen for every send.
2452 inp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2454 struct in_addr addr;
2455 struct ip_mreqn mreqn;
2457 struct ip_moptions *imo;
2460 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
2462 * An interface index was specified using the
2463 * Linux-derived ip_mreqn structure.
2465 error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn),
2466 sizeof(struct ip_mreqn));
2470 if (mreqn.imr_ifindex < 0)
2473 if (mreqn.imr_ifindex == 0) {
2476 struct epoch_tracker et;
2478 NET_EPOCH_ENTER(et);
2479 ifp = ifnet_byindex(mreqn.imr_ifindex);
2480 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */
2482 return (EADDRNOTAVAIL);
2486 * An interface was specified by IPv4 address.
2487 * This is the traditional BSD usage.
2489 error = sooptcopyin(sopt, &addr, sizeof(struct in_addr),
2490 sizeof(struct in_addr));
2493 if (in_nullhost(addr)) {
2496 struct epoch_tracker et;
2498 NET_EPOCH_ENTER(et);
2499 INADDR_TO_IFP(addr, ifp);
2503 return (EADDRNOTAVAIL);
2505 CTR3(KTR_IGMPV3, "%s: ifp = %p, addr = 0x%08x", __func__, ifp,
2506 ntohl(addr.s_addr));
2509 /* Reject interfaces which do not support multicast. */
2510 if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0)
2511 return (EOPNOTSUPP);
2513 imo = inp_findmoptions(inp);
2514 imo->imo_multicast_ifp = ifp;
2515 imo->imo_multicast_addr.s_addr = INADDR_ANY;
2522 * Atomically set source filters on a socket for an IPv4 multicast group.
2524 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
2527 inp_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2529 struct epoch_tracker et;
2530 struct __msfilterreq msfr;
2533 struct in_mfilter *imf;
2534 struct ip_moptions *imo;
2535 struct in_multi *inm;
2538 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2539 sizeof(struct __msfilterreq));
2543 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
2546 if ((msfr.msfr_fmode != MCAST_EXCLUDE &&
2547 msfr.msfr_fmode != MCAST_INCLUDE))
2550 if (msfr.msfr_group.ss_family != AF_INET ||
2551 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in))
2554 gsa = (sockunion_t *)&msfr.msfr_group;
2555 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2558 gsa->sin.sin_port = 0; /* ignore port */
2560 NET_EPOCH_ENTER(et);
2561 ifp = ifnet_byindex(msfr.msfr_ifindex);
2562 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */
2564 return (EADDRNOTAVAIL);
2569 * Take the INP write lock.
2570 * Check if this socket is a member of this group.
2572 imo = inp_findmoptions(inp);
2573 imf = imo_match_group(imo, ifp, &gsa->sa);
2575 error = EADDRNOTAVAIL;
2576 goto out_inp_locked;
2581 * Begin state merge transaction at socket layer.
2583 INP_WLOCK_ASSERT(inp);
2585 imf->imf_st[1] = msfr.msfr_fmode;
2588 * Apply any new source filters, if present.
2589 * Make a copy of the user-space source vector so
2590 * that we may copy them with a single copyin. This
2591 * allows us to deal with page faults up-front.
2593 if (msfr.msfr_nsrcs > 0) {
2594 struct in_msource *lims;
2595 struct sockaddr_in *psin;
2596 struct sockaddr_storage *kss, *pkss;
2601 CTR2(KTR_IGMPV3, "%s: loading %lu source list entries",
2602 __func__, (unsigned long)msfr.msfr_nsrcs);
2603 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2605 error = copyin(msfr.msfr_srcs, kss,
2606 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2615 * Mark all source filters as UNDEFINED at t1.
2616 * Restore new group filter mode, as imf_leave()
2617 * will set it to INCLUDE.
2620 imf->imf_st[1] = msfr.msfr_fmode;
2623 * Update socket layer filters at t1, lazy-allocating
2624 * new entries. This saves a bunch of memory at the
2625 * cost of one RB_FIND() per source entry; duplicate
2626 * entries in the msfr_nsrcs vector are ignored.
2627 * If we encounter an error, rollback transaction.
2629 * XXX This too could be replaced with a set-symmetric
2630 * difference like loop to avoid walking from root
2631 * every time, as the key space is common.
2633 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2634 psin = (struct sockaddr_in *)pkss;
2635 if (psin->sin_family != AF_INET) {
2636 error = EAFNOSUPPORT;
2639 if (psin->sin_len != sizeof(struct sockaddr_in)) {
2643 error = imf_get_source(imf, psin, &lims);
2646 lims->imsl_st[1] = imf->imf_st[1];
2652 goto out_imf_rollback;
2654 INP_WLOCK_ASSERT(inp);
2657 * Begin state merge transaction at IGMP layer.
2659 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2660 IN_MULTI_LIST_LOCK();
2661 error = inm_merge(inm, imf);
2663 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
2664 IN_MULTI_LIST_UNLOCK();
2665 goto out_imf_rollback;
2668 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2669 error = igmp_change_state(inm);
2670 IN_MULTI_LIST_UNLOCK();
2672 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
2689 * Set the IP multicast options in response to user setsockopt().
2691 * Many of the socket options handled in this function duplicate the
2692 * functionality of socket options in the regular unicast API. However,
2693 * it is not possible to merge the duplicate code, because the idempotence
2694 * of the IPv4 multicast part of the BSD Sockets API must be preserved;
2695 * the effects of these options must be treated as separate and distinct.
2697 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2698 * FUTURE: The IP_MULTICAST_VIF option may be eliminated if MROUTING
2699 * is refactored to no longer use vifs.
2702 inp_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2704 struct ip_moptions *imo;
2709 /* If socket is neither of type SOCK_RAW or SOCK_DGRAM, reject it. */
2710 if (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2711 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)
2712 return (EOPNOTSUPP);
2714 switch (sopt->sopt_name) {
2715 case IP_MULTICAST_VIF: {
2718 * Select a multicast VIF for transmission.
2719 * Only useful if multicast forwarding is active.
2721 if (legal_vif_num == NULL) {
2725 error = sooptcopyin(sopt, &vifi, sizeof(int), sizeof(int));
2728 if (!legal_vif_num(vifi) && (vifi != -1)) {
2732 imo = inp_findmoptions(inp);
2733 imo->imo_multicast_vif = vifi;
2738 case IP_MULTICAST_IF:
2739 error = inp_set_multicast_if(inp, sopt);
2742 case IP_MULTICAST_TTL: {
2746 * Set the IP time-to-live for outgoing multicast packets.
2747 * The original multicast API required a char argument,
2748 * which is inconsistent with the rest of the socket API.
2749 * We allow either a char or an int.
2751 if (sopt->sopt_valsize == sizeof(u_char)) {
2752 error = sooptcopyin(sopt, &ttl, sizeof(u_char),
2759 error = sooptcopyin(sopt, &ittl, sizeof(u_int),
2769 imo = inp_findmoptions(inp);
2770 imo->imo_multicast_ttl = ttl;
2775 case IP_MULTICAST_LOOP: {
2779 * Set the loopback flag for outgoing multicast packets.
2780 * Must be zero or one. The original multicast API required a
2781 * char argument, which is inconsistent with the rest
2782 * of the socket API. We allow either a char or an int.
2784 if (sopt->sopt_valsize == sizeof(u_char)) {
2785 error = sooptcopyin(sopt, &loop, sizeof(u_char),
2792 error = sooptcopyin(sopt, &iloop, sizeof(u_int),
2796 loop = (u_char)iloop;
2798 imo = inp_findmoptions(inp);
2799 imo->imo_multicast_loop = !!loop;
2804 case IP_ADD_MEMBERSHIP:
2805 case IP_ADD_SOURCE_MEMBERSHIP:
2806 case MCAST_JOIN_GROUP:
2807 case MCAST_JOIN_SOURCE_GROUP:
2808 error = inp_join_group(inp, sopt);
2811 case IP_DROP_MEMBERSHIP:
2812 case IP_DROP_SOURCE_MEMBERSHIP:
2813 case MCAST_LEAVE_GROUP:
2814 case MCAST_LEAVE_SOURCE_GROUP:
2815 error = inp_leave_group(inp, sopt);
2818 case IP_BLOCK_SOURCE:
2819 case IP_UNBLOCK_SOURCE:
2820 case MCAST_BLOCK_SOURCE:
2821 case MCAST_UNBLOCK_SOURCE:
2822 error = inp_block_unblock_source(inp, sopt);
2826 error = inp_set_source_filters(inp, sopt);
2834 INP_UNLOCK_ASSERT(inp);
2840 * Expose IGMP's multicast filter mode and source list(s) to userland,
2841 * keyed by (ifindex, group).
2842 * The filter mode is written out as a uint32_t, followed by
2843 * 0..n of struct in_addr.
2844 * For use by ifmcstat(8).
2845 * SMPng: NOTE: unlocked read of ifindex space.
2848 sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS)
2850 struct in_addr src, group;
2851 struct epoch_tracker et;
2853 struct ifmultiaddr *ifma;
2854 struct in_multi *inm;
2855 struct ip_msource *ims;
2859 uint32_t fmode, ifindex;
2864 if (req->newptr != NULL)
2870 group.s_addr = name[1];
2871 if (!IN_MULTICAST(ntohl(group.s_addr))) {
2872 CTR2(KTR_IGMPV3, "%s: group 0x%08x is not multicast",
2873 __func__, ntohl(group.s_addr));
2878 NET_EPOCH_ENTER(et);
2879 ifp = ifnet_byindex(ifindex);
2882 CTR2(KTR_IGMPV3, "%s: no ifp for ifindex %u",
2887 retval = sysctl_wire_old_buffer(req,
2888 sizeof(uint32_t) + (in_mcast_maxgrpsrc * sizeof(struct in_addr)));
2894 IN_MULTI_LIST_LOCK();
2896 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2897 inm = inm_ifmultiaddr_get_inm(ifma);
2900 if (!in_hosteq(inm->inm_addr, group))
2902 fmode = inm->inm_st[1].iss_fmode;
2903 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2906 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
2907 CTR2(KTR_IGMPV3, "%s: visit node 0x%08x", __func__,
2910 * Only copy-out sources which are in-mode.
2912 if (fmode != ims_get_mode(inm, ims, 1)) {
2913 CTR1(KTR_IGMPV3, "%s: skip non-in-mode",
2917 src.s_addr = htonl(ims->ims_haddr);
2918 retval = SYSCTL_OUT(req, &src, sizeof(struct in_addr));
2924 IN_MULTI_LIST_UNLOCK();
2930 #if defined(KTR) && (KTR_COMPILE & KTR_IGMPV3)
2932 static const char *inm_modestrs[] = {
2933 [MCAST_UNDEFINED] = "un",
2934 [MCAST_INCLUDE] = "in",
2935 [MCAST_EXCLUDE] = "ex",
2937 _Static_assert(MCAST_UNDEFINED == 0 &&
2938 MCAST_EXCLUDE + 1 == nitems(inm_modestrs),
2939 "inm_modestrs: no longer matches #defines");
2942 inm_mode_str(const int mode)
2945 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2946 return (inm_modestrs[mode]);
2950 static const char *inm_statestrs[] = {
2951 [IGMP_NOT_MEMBER] = "not-member",
2952 [IGMP_SILENT_MEMBER] = "silent",
2953 [IGMP_REPORTING_MEMBER] = "reporting",
2954 [IGMP_IDLE_MEMBER] = "idle",
2955 [IGMP_LAZY_MEMBER] = "lazy",
2956 [IGMP_SLEEPING_MEMBER] = "sleeping",
2957 [IGMP_AWAKENING_MEMBER] = "awakening",
2958 [IGMP_G_QUERY_PENDING_MEMBER] = "query-pending",
2959 [IGMP_SG_QUERY_PENDING_MEMBER] = "sg-query-pending",
2960 [IGMP_LEAVING_MEMBER] = "leaving",
2962 _Static_assert(IGMP_NOT_MEMBER == 0 &&
2963 IGMP_LEAVING_MEMBER + 1 == nitems(inm_statestrs),
2964 "inm_statetrs: no longer matches #defines");
2967 inm_state_str(const int state)
2970 if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
2971 return (inm_statestrs[state]);
2976 * Dump an in_multi structure to the console.
2979 inm_print(const struct in_multi *inm)
2982 char addrbuf[INET_ADDRSTRLEN];
2984 if ((ktr_mask & KTR_IGMPV3) == 0)
2987 printf("%s: --- begin inm %p ---\n", __func__, inm);
2988 printf("addr %s ifp %p(%s) ifma %p\n",
2989 inet_ntoa_r(inm->inm_addr, addrbuf),
2991 inm->inm_ifp->if_xname,
2993 printf("timer %u state %s refcount %u scq.len %u\n",
2995 inm_state_str(inm->inm_state),
2997 inm->inm_scq.mq_len);
2998 printf("igi %p nsrc %lu sctimer %u scrv %u\n",
3003 for (t = 0; t < 2; t++) {
3004 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
3005 inm_mode_str(inm->inm_st[t].iss_fmode),
3006 inm->inm_st[t].iss_asm,
3007 inm->inm_st[t].iss_ex,
3008 inm->inm_st[t].iss_in,
3009 inm->inm_st[t].iss_rec);
3011 printf("%s: --- end inm %p ---\n", __func__, inm);
3014 #else /* !KTR || !(KTR_COMPILE & KTR_IGMPV3) */
3017 inm_print(const struct in_multi *inm)
3022 #endif /* KTR && (KTR_COMPILE & KTR_IGMPV3) */
3024 RB_GENERATE(ip_msource_tree, ip_msource, ims_link, ip_msource_cmp);