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>
54 #include <sys/gtaskqueue.h>
58 #include <net/if_var.h>
59 #include <net/if_dl.h>
60 #include <net/route.h>
63 #include <net/ethernet.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/in_fib.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/in_var.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/igmp_var.h>
74 #define KTR_IGMPV3 KTR_INET
77 #ifndef __SOCKUNION_DECLARED
79 struct sockaddr_storage ss;
81 struct sockaddr_dl sdl;
82 struct sockaddr_in sin;
84 typedef union sockunion sockunion_t;
85 #define __SOCKUNION_DECLARED
86 #endif /* __SOCKUNION_DECLARED */
88 static MALLOC_DEFINE(M_INMFILTER, "in_mfilter",
89 "IPv4 multicast PCB-layer source filter");
90 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "IPv4 multicast group");
91 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "IPv4 multicast options");
92 static MALLOC_DEFINE(M_IPMSOURCE, "ip_msource",
93 "IPv4 multicast IGMP-layer source filter");
98 * - Lock order is: Giant, IN_MULTI_LOCK, INP_WLOCK,
99 * IN_MULTI_LIST_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
100 * - The IF_ADDR_LOCK is implicitly taken by inm_lookup() earlier, however
101 * it can be taken by code in net/if.c also.
102 * - ip_moptions and in_mfilter are covered by the INP_WLOCK.
104 * struct in_multi is covered by IN_MULTI_LIST_LOCK. There isn't strictly
105 * any need for in_multi itself to be virtualized -- it is bound to an ifp
106 * anyway no matter what happens.
108 struct mtx in_multi_list_mtx;
109 MTX_SYSINIT(in_multi_mtx, &in_multi_list_mtx, "in_multi_list_mtx", MTX_DEF);
111 struct mtx in_multi_free_mtx;
112 MTX_SYSINIT(in_multi_free_mtx, &in_multi_free_mtx, "in_multi_free_mtx", MTX_DEF);
114 struct sx in_multi_sx;
115 SX_SYSINIT(in_multi_sx, &in_multi_sx, "in_multi_sx");
120 * Functions with non-static linkage defined in this file should be
121 * declared in in_var.h:
126 * in_joingroup_locked()
128 * in_leavegroup_locked()
134 * XXX: Both carp and pf need to use the legacy (*,G) KPIs in_addmulti()
137 static void imf_commit(struct in_mfilter *);
138 static int imf_get_source(struct in_mfilter *imf,
139 const struct sockaddr_in *psin,
140 struct in_msource **);
141 static struct in_msource *
142 imf_graft(struct in_mfilter *, const uint8_t,
143 const struct sockaddr_in *);
144 static void imf_leave(struct in_mfilter *);
145 static int imf_prune(struct in_mfilter *, const struct sockaddr_in *);
146 static void imf_purge(struct in_mfilter *);
147 static void imf_rollback(struct in_mfilter *);
148 static void imf_reap(struct in_mfilter *);
149 static struct in_mfilter *
150 imo_match_group(const struct ip_moptions *,
151 const struct ifnet *, const struct sockaddr *);
152 static struct in_msource *
153 imo_match_source(struct in_mfilter *, const struct sockaddr *);
154 static void ims_merge(struct ip_msource *ims,
155 const struct in_msource *lims, const int rollback);
156 static int in_getmulti(struct ifnet *, const struct in_addr *,
158 static int inm_get_source(struct in_multi *inm, const in_addr_t haddr,
159 const int noalloc, struct ip_msource **pims);
161 static int inm_is_ifp_detached(const struct in_multi *);
163 static int inm_merge(struct in_multi *, /*const*/ struct in_mfilter *);
164 static void inm_purge(struct in_multi *);
165 static void inm_reap(struct in_multi *);
166 static void inm_release(struct in_multi *);
167 static struct ip_moptions *
168 inp_findmoptions(struct inpcb *);
169 static int inp_get_source_filters(struct inpcb *, struct sockopt *);
170 static int inp_join_group(struct inpcb *, struct sockopt *);
171 static int inp_leave_group(struct inpcb *, struct sockopt *);
172 static struct ifnet *
173 inp_lookup_mcast_ifp(const struct inpcb *,
174 const struct sockaddr_in *, const struct in_addr);
175 static int inp_block_unblock_source(struct inpcb *, struct sockopt *);
176 static int inp_set_multicast_if(struct inpcb *, struct sockopt *);
177 static int inp_set_source_filters(struct inpcb *, struct sockopt *);
178 static int sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS);
180 static SYSCTL_NODE(_net_inet_ip, OID_AUTO, mcast,
181 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
184 static u_long in_mcast_maxgrpsrc = IP_MAX_GROUP_SRC_FILTER;
185 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxgrpsrc,
186 CTLFLAG_RWTUN, &in_mcast_maxgrpsrc, 0,
187 "Max source filters per group");
189 static u_long in_mcast_maxsocksrc = IP_MAX_SOCK_SRC_FILTER;
190 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxsocksrc,
191 CTLFLAG_RWTUN, &in_mcast_maxsocksrc, 0,
192 "Max source filters per socket");
194 int in_mcast_loop = IP_DEFAULT_MULTICAST_LOOP;
195 SYSCTL_INT(_net_inet_ip_mcast, OID_AUTO, loop, CTLFLAG_RWTUN,
196 &in_mcast_loop, 0, "Loopback multicast datagrams by default");
198 static SYSCTL_NODE(_net_inet_ip_mcast, OID_AUTO, filters,
199 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip_mcast_filters,
200 "Per-interface stack-wide source filters");
204 * Inline function which wraps assertions for a valid ifp.
205 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
209 inm_is_ifp_detached(const struct in_multi *inm)
213 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
214 ifp = inm->inm_ifma->ifma_ifp;
217 * Sanity check that netinet's notion of ifp is the
220 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
223 return (ifp == NULL);
227 static struct grouptask free_gtask;
228 static struct in_multi_head inm_free_list;
229 static void inm_release_task(void *arg __unused);
230 static void inm_init(void)
232 SLIST_INIT(&inm_free_list);
233 taskqgroup_config_gtask_init(NULL, &free_gtask, inm_release_task, "inm release task");
236 #ifdef EARLY_AP_STARTUP
237 SYSINIT(inm_init, SI_SUB_SMP + 1, SI_ORDER_FIRST,
240 SYSINIT(inm_init, SI_SUB_ROOT_CONF - 1, SI_ORDER_FIRST,
246 inm_release_list_deferred(struct in_multi_head *inmh)
249 if (SLIST_EMPTY(inmh))
251 mtx_lock(&in_multi_free_mtx);
252 SLIST_CONCAT(&inm_free_list, inmh, in_multi, inm_nrele);
253 mtx_unlock(&in_multi_free_mtx);
254 GROUPTASK_ENQUEUE(&free_gtask);
258 inm_disconnect(struct in_multi *inm)
261 struct ifmultiaddr *ifma, *ll_ifma;
264 IF_ADDR_WLOCK_ASSERT(ifp);
265 ifma = inm->inm_ifma;
268 if (ifma->ifma_flags & IFMA_F_ENQUEUED) {
269 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
270 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
272 MCDPRINTF("removed ifma: %p from %s\n", ifma, ifp->if_xname);
273 if ((ll_ifma = ifma->ifma_llifma) != NULL) {
274 MPASS(ifma != ll_ifma);
275 ifma->ifma_llifma = NULL;
276 MPASS(ll_ifma->ifma_llifma == NULL);
277 MPASS(ll_ifma->ifma_ifp == ifp);
278 if (--ll_ifma->ifma_refcount == 0) {
279 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
280 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, ifma_link);
281 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
283 MCDPRINTF("removed ll_ifma: %p from %s\n", ll_ifma, ifp->if_xname);
284 if_freemulti(ll_ifma);
291 inm_release_deferred(struct in_multi *inm)
293 struct in_multi_head tmp;
295 IN_MULTI_LIST_LOCK_ASSERT();
296 MPASS(inm->inm_refcount > 0);
297 if (--inm->inm_refcount == 0) {
300 inm->inm_ifma->ifma_protospec = NULL;
301 SLIST_INSERT_HEAD(&tmp, inm, inm_nrele);
302 inm_release_list_deferred(&tmp);
307 inm_release_task(void *arg __unused)
309 struct in_multi_head inm_free_tmp;
310 struct in_multi *inm, *tinm;
312 SLIST_INIT(&inm_free_tmp);
313 mtx_lock(&in_multi_free_mtx);
314 SLIST_CONCAT(&inm_free_tmp, &inm_free_list, in_multi, inm_nrele);
315 mtx_unlock(&in_multi_free_mtx);
317 SLIST_FOREACH_SAFE(inm, &inm_free_tmp, inm_nrele, tinm) {
318 SLIST_REMOVE_HEAD(&inm_free_tmp, inm_nrele);
326 * Initialize an in_mfilter structure to a known state at t0, t1
327 * with an empty source filter list.
330 imf_init(struct in_mfilter *imf, const int st0, const int st1)
332 memset(imf, 0, sizeof(struct in_mfilter));
333 RB_INIT(&imf->imf_sources);
334 imf->imf_st[0] = st0;
335 imf->imf_st[1] = st1;
339 ip_mfilter_alloc(const int mflags, const int st0, const int st1)
341 struct in_mfilter *imf;
343 imf = malloc(sizeof(*imf), M_INMFILTER, mflags);
345 imf_init(imf, st0, st1);
351 ip_mfilter_free(struct in_mfilter *imf)
355 free(imf, M_INMFILTER);
359 * Function for looking up an in_multi record for an IPv4 multicast address
360 * on a given interface. ifp must be valid. If no record found, return NULL.
361 * The IN_MULTI_LIST_LOCK and IF_ADDR_LOCK on ifp must be held.
364 inm_lookup_locked(struct ifnet *ifp, const struct in_addr ina)
366 struct ifmultiaddr *ifma;
367 struct in_multi *inm;
369 IN_MULTI_LIST_LOCK_ASSERT();
370 IF_ADDR_LOCK_ASSERT(ifp);
373 CK_STAILQ_FOREACH(ifma, &((ifp)->if_multiaddrs), ifma_link) {
374 if (ifma->ifma_addr->sa_family != AF_INET ||
375 ifma->ifma_protospec == NULL)
377 inm = (struct in_multi *)ifma->ifma_protospec;
378 if (inm->inm_addr.s_addr == ina.s_addr)
386 * Wrapper for inm_lookup_locked().
387 * The IF_ADDR_LOCK will be taken on ifp and released on return.
390 inm_lookup(struct ifnet *ifp, const struct in_addr ina)
392 struct epoch_tracker et;
393 struct in_multi *inm;
395 IN_MULTI_LIST_LOCK_ASSERT();
398 inm = inm_lookup_locked(ifp, ina);
405 * Find an IPv4 multicast group entry for this ip_moptions instance
406 * which matches the specified group, and optionally an interface.
407 * Return its index into the array, or -1 if not found.
409 static struct in_mfilter *
410 imo_match_group(const struct ip_moptions *imo, const struct ifnet *ifp,
411 const struct sockaddr *group)
413 const struct sockaddr_in *gsin;
414 struct in_mfilter *imf;
415 struct in_multi *inm;
417 gsin = (const struct sockaddr_in *)group;
419 IP_MFILTER_FOREACH(imf, &imo->imo_head) {
423 if ((ifp == NULL || (inm->inm_ifp == ifp)) &&
424 in_hosteq(inm->inm_addr, gsin->sin_addr)) {
432 * Find an IPv4 multicast source entry for this imo which matches
433 * the given group index for this socket, and source address.
435 * NOTE: This does not check if the entry is in-mode, merely if
436 * it exists, which may not be the desired behaviour.
438 static struct in_msource *
439 imo_match_source(struct in_mfilter *imf, const struct sockaddr *src)
441 struct ip_msource find;
442 struct ip_msource *ims;
443 const sockunion_t *psa;
445 KASSERT(src->sa_family == AF_INET, ("%s: !AF_INET", __func__));
447 /* Source trees are keyed in host byte order. */
448 psa = (const sockunion_t *)src;
449 find.ims_haddr = ntohl(psa->sin.sin_addr.s_addr);
450 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
452 return ((struct in_msource *)ims);
456 * Perform filtering for multicast datagrams on a socket by group and source.
458 * Returns 0 if a datagram should be allowed through, or various error codes
459 * if the socket was not a member of the group, or the source was muted, etc.
462 imo_multi_filter(const struct ip_moptions *imo, const struct ifnet *ifp,
463 const struct sockaddr *group, const struct sockaddr *src)
465 struct in_mfilter *imf;
466 struct in_msource *ims;
469 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
471 imf = imo_match_group(imo, ifp, group);
473 return (MCAST_NOTGMEMBER);
476 * Check if the source was included in an (S,G) join.
477 * Allow reception on exclusive memberships by default,
478 * reject reception on inclusive memberships by default.
479 * Exclude source only if an in-mode exclude filter exists.
480 * Include source only if an in-mode include filter exists.
481 * NOTE: We are comparing group state here at IGMP t1 (now)
482 * with socket-layer t0 (since last downcall).
484 mode = imf->imf_st[1];
485 ims = imo_match_source(imf, src);
487 if ((ims == NULL && mode == MCAST_INCLUDE) ||
488 (ims != NULL && ims->imsl_st[0] != mode))
489 return (MCAST_NOTSMEMBER);
495 * Find and return a reference to an in_multi record for (ifp, group),
496 * and bump its reference count.
497 * If one does not exist, try to allocate it, and update link-layer multicast
498 * filters on ifp to listen for group.
499 * Assumes the IN_MULTI lock is held across the call.
500 * Return 0 if successful, otherwise return an appropriate error code.
503 in_getmulti(struct ifnet *ifp, const struct in_addr *group,
504 struct in_multi **pinm)
506 struct sockaddr_in gsin;
507 struct ifmultiaddr *ifma;
508 struct in_ifinfo *ii;
509 struct in_multi *inm;
512 IN_MULTI_LOCK_ASSERT();
514 ii = (struct in_ifinfo *)ifp->if_afdata[AF_INET];
515 IN_MULTI_LIST_LOCK();
516 inm = inm_lookup(ifp, *group);
519 * If we already joined this group, just bump the
520 * refcount and return it.
522 KASSERT(inm->inm_refcount >= 1,
523 ("%s: bad refcount %d", __func__, inm->inm_refcount));
524 inm_acquire_locked(inm);
527 IN_MULTI_LIST_UNLOCK();
531 memset(&gsin, 0, sizeof(gsin));
532 gsin.sin_family = AF_INET;
533 gsin.sin_len = sizeof(struct sockaddr_in);
534 gsin.sin_addr = *group;
537 * Check if a link-layer group is already associated
538 * with this network-layer group on the given ifnet.
540 error = if_addmulti(ifp, (struct sockaddr *)&gsin, &ifma);
544 /* XXX ifma_protospec must be covered by IF_ADDR_LOCK */
545 IN_MULTI_LIST_LOCK();
549 * If something other than netinet is occupying the link-layer
550 * group, print a meaningful error message and back out of
552 * Otherwise, bump the refcount on the existing network-layer
553 * group association and return it.
555 if (ifma->ifma_protospec != NULL) {
556 inm = (struct in_multi *)ifma->ifma_protospec;
558 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
560 KASSERT(ifma->ifma_addr->sa_family == AF_INET,
561 ("%s: ifma not AF_INET", __func__));
562 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
563 if (inm->inm_ifma != ifma || inm->inm_ifp != ifp ||
564 !in_hosteq(inm->inm_addr, *group)) {
565 char addrbuf[INET_ADDRSTRLEN];
567 panic("%s: ifma %p is inconsistent with %p (%s)",
568 __func__, ifma, inm, inet_ntoa_r(*group, addrbuf));
571 inm_acquire_locked(inm);
576 IF_ADDR_WLOCK_ASSERT(ifp);
579 * A new in_multi record is needed; allocate and initialize it.
580 * We DO NOT perform an IGMP join as the in_ layer may need to
581 * push an initial source list down to IGMP to support SSM.
583 * The initial source filter state is INCLUDE, {} as per the RFC.
585 inm = malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT | M_ZERO);
587 IF_ADDR_WUNLOCK(ifp);
588 IN_MULTI_LIST_UNLOCK();
589 if_delmulti_ifma(ifma);
592 inm->inm_addr = *group;
594 inm->inm_igi = ii->ii_igmp;
595 inm->inm_ifma = ifma;
596 inm->inm_refcount = 1;
597 inm->inm_state = IGMP_NOT_MEMBER;
598 mbufq_init(&inm->inm_scq, IGMP_MAX_STATE_CHANGES);
599 inm->inm_st[0].iss_fmode = MCAST_UNDEFINED;
600 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
601 RB_INIT(&inm->inm_srcs);
603 ifma->ifma_protospec = inm;
607 IF_ADDR_WUNLOCK(ifp);
608 IN_MULTI_LIST_UNLOCK();
613 * Drop a reference to an in_multi record.
615 * If the refcount drops to 0, free the in_multi record and
616 * delete the underlying link-layer membership.
619 inm_release(struct in_multi *inm)
621 struct ifmultiaddr *ifma;
624 CTR2(KTR_IGMPV3, "%s: refcount is %d", __func__, inm->inm_refcount);
625 MPASS(inm->inm_refcount == 0);
626 CTR2(KTR_IGMPV3, "%s: freeing inm %p", __func__, inm);
628 ifma = inm->inm_ifma;
631 /* XXX this access is not covered by IF_ADDR_LOCK */
632 CTR2(KTR_IGMPV3, "%s: purging ifma %p", __func__, ifma);
634 CURVNET_SET(ifp->if_vnet);
636 free(inm, M_IPMADDR);
637 if_delmulti_ifma_flags(ifma, 1);
642 free(inm, M_IPMADDR);
643 if_delmulti_ifma_flags(ifma, 1);
648 * Clear recorded source entries for a group.
649 * Used by the IGMP code. Caller must hold the IN_MULTI lock.
650 * FIXME: Should reap.
653 inm_clear_recorded(struct in_multi *inm)
655 struct ip_msource *ims;
657 IN_MULTI_LIST_LOCK_ASSERT();
659 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
662 --inm->inm_st[1].iss_rec;
665 KASSERT(inm->inm_st[1].iss_rec == 0,
666 ("%s: iss_rec %d not 0", __func__, inm->inm_st[1].iss_rec));
670 * Record a source as pending for a Source-Group IGMPv3 query.
671 * This lives here as it modifies the shared tree.
673 * inm is the group descriptor.
674 * naddr is the address of the source to record in network-byte order.
676 * If the net.inet.igmp.sgalloc sysctl is non-zero, we will
677 * lazy-allocate a source node in response to an SG query.
678 * Otherwise, no allocation is performed. This saves some memory
679 * with the trade-off that the source will not be reported to the
680 * router if joined in the window between the query response and
681 * the group actually being joined on the local host.
683 * VIMAGE: XXX: Currently the igmp_sgalloc feature has been removed.
684 * This turns off the allocation of a recorded source entry if
685 * the group has not been joined.
687 * Return 0 if the source didn't exist or was already marked as recorded.
688 * Return 1 if the source was marked as recorded by this function.
689 * Return <0 if any error occurred (negated errno code).
692 inm_record_source(struct in_multi *inm, const in_addr_t naddr)
694 struct ip_msource find;
695 struct ip_msource *ims, *nims;
697 IN_MULTI_LIST_LOCK_ASSERT();
699 find.ims_haddr = ntohl(naddr);
700 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
701 if (ims && ims->ims_stp)
704 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
706 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
710 nims->ims_haddr = find.ims_haddr;
711 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
717 * Mark the source as recorded and update the recorded
721 ++inm->inm_st[1].iss_rec;
727 * Return a pointer to an in_msource owned by an in_mfilter,
728 * given its source address.
729 * Lazy-allocate if needed. If this is a new entry its filter state is
732 * imf is the filter set being modified.
733 * haddr is the source address in *host* byte-order.
735 * SMPng: May be called with locks held; malloc must not block.
738 imf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin,
739 struct in_msource **plims)
741 struct ip_msource find;
742 struct ip_msource *ims, *nims;
743 struct in_msource *lims;
750 /* key is host byte order */
751 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
752 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
753 lims = (struct in_msource *)ims;
755 if (imf->imf_nsrc == in_mcast_maxsocksrc)
757 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
761 lims = (struct in_msource *)nims;
762 lims->ims_haddr = find.ims_haddr;
763 lims->imsl_st[0] = MCAST_UNDEFINED;
764 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
774 * Graft a source entry into an existing socket-layer filter set,
775 * maintaining any required invariants and checking allocations.
777 * The source is marked as being in the new filter mode at t1.
779 * Return the pointer to the new node, otherwise return NULL.
781 static struct in_msource *
782 imf_graft(struct in_mfilter *imf, const uint8_t st1,
783 const struct sockaddr_in *psin)
785 struct ip_msource *nims;
786 struct in_msource *lims;
788 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
792 lims = (struct in_msource *)nims;
793 lims->ims_haddr = ntohl(psin->sin_addr.s_addr);
794 lims->imsl_st[0] = MCAST_UNDEFINED;
795 lims->imsl_st[1] = st1;
796 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
803 * Prune a source entry from an existing socket-layer filter set,
804 * maintaining any required invariants and checking allocations.
806 * The source is marked as being left at t1, it is not freed.
808 * Return 0 if no error occurred, otherwise return an errno value.
811 imf_prune(struct in_mfilter *imf, const struct sockaddr_in *psin)
813 struct ip_msource find;
814 struct ip_msource *ims;
815 struct in_msource *lims;
817 /* key is host byte order */
818 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
819 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
822 lims = (struct in_msource *)ims;
823 lims->imsl_st[1] = MCAST_UNDEFINED;
828 * Revert socket-layer filter set deltas at t1 to t0 state.
831 imf_rollback(struct in_mfilter *imf)
833 struct ip_msource *ims, *tims;
834 struct in_msource *lims;
836 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
837 lims = (struct in_msource *)ims;
838 if (lims->imsl_st[0] == lims->imsl_st[1]) {
839 /* no change at t1 */
841 } else if (lims->imsl_st[0] != MCAST_UNDEFINED) {
842 /* revert change to existing source at t1 */
843 lims->imsl_st[1] = lims->imsl_st[0];
845 /* revert source added t1 */
846 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
847 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
848 free(ims, M_INMFILTER);
852 imf->imf_st[1] = imf->imf_st[0];
856 * Mark socket-layer filter set as INCLUDE {} at t1.
859 imf_leave(struct in_mfilter *imf)
861 struct ip_msource *ims;
862 struct in_msource *lims;
864 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
865 lims = (struct in_msource *)ims;
866 lims->imsl_st[1] = MCAST_UNDEFINED;
868 imf->imf_st[1] = MCAST_INCLUDE;
872 * Mark socket-layer filter set deltas as committed.
875 imf_commit(struct in_mfilter *imf)
877 struct ip_msource *ims;
878 struct in_msource *lims;
880 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
881 lims = (struct in_msource *)ims;
882 lims->imsl_st[0] = lims->imsl_st[1];
884 imf->imf_st[0] = imf->imf_st[1];
888 * Reap unreferenced sources from socket-layer filter set.
891 imf_reap(struct in_mfilter *imf)
893 struct ip_msource *ims, *tims;
894 struct in_msource *lims;
896 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
897 lims = (struct in_msource *)ims;
898 if ((lims->imsl_st[0] == MCAST_UNDEFINED) &&
899 (lims->imsl_st[1] == MCAST_UNDEFINED)) {
900 CTR2(KTR_IGMPV3, "%s: free lims %p", __func__, ims);
901 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
902 free(ims, M_INMFILTER);
909 * Purge socket-layer filter set.
912 imf_purge(struct in_mfilter *imf)
914 struct ip_msource *ims, *tims;
916 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
917 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
918 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
919 free(ims, M_INMFILTER);
922 imf->imf_st[0] = imf->imf_st[1] = MCAST_UNDEFINED;
923 KASSERT(RB_EMPTY(&imf->imf_sources),
924 ("%s: imf_sources not empty", __func__));
928 * Look up a source filter entry for a multicast group.
930 * inm is the group descriptor to work with.
931 * haddr is the host-byte-order IPv4 address to look up.
932 * noalloc may be non-zero to suppress allocation of sources.
933 * *pims will be set to the address of the retrieved or allocated source.
935 * SMPng: NOTE: may be called with locks held.
936 * Return 0 if successful, otherwise return a non-zero error code.
939 inm_get_source(struct in_multi *inm, const in_addr_t haddr,
940 const int noalloc, struct ip_msource **pims)
942 struct ip_msource find;
943 struct ip_msource *ims, *nims;
945 find.ims_haddr = haddr;
946 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
947 if (ims == NULL && !noalloc) {
948 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
950 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
954 nims->ims_haddr = haddr;
955 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
959 CTR3(KTR_IGMPV3, "%s: allocated 0x%08x as %p", __func__,
969 * Merge socket-layer source into IGMP-layer source.
970 * If rollback is non-zero, perform the inverse of the merge.
973 ims_merge(struct ip_msource *ims, const struct in_msource *lims,
976 int n = rollback ? -1 : 1;
978 if (lims->imsl_st[0] == MCAST_EXCLUDE) {
979 CTR3(KTR_IGMPV3, "%s: t1 ex -= %d on 0x%08x",
980 __func__, n, ims->ims_haddr);
981 ims->ims_st[1].ex -= n;
982 } else if (lims->imsl_st[0] == MCAST_INCLUDE) {
983 CTR3(KTR_IGMPV3, "%s: t1 in -= %d on 0x%08x",
984 __func__, n, ims->ims_haddr);
985 ims->ims_st[1].in -= n;
988 if (lims->imsl_st[1] == MCAST_EXCLUDE) {
989 CTR3(KTR_IGMPV3, "%s: t1 ex += %d on 0x%08x",
990 __func__, n, ims->ims_haddr);
991 ims->ims_st[1].ex += n;
992 } else if (lims->imsl_st[1] == MCAST_INCLUDE) {
993 CTR3(KTR_IGMPV3, "%s: t1 in += %d on 0x%08x",
994 __func__, n, ims->ims_haddr);
995 ims->ims_st[1].in += n;
1000 * Atomically update the global in_multi state, when a membership's
1001 * filter list is being updated in any way.
1003 * imf is the per-inpcb-membership group filter pointer.
1004 * A fake imf may be passed for in-kernel consumers.
1006 * XXX This is a candidate for a set-symmetric-difference style loop
1007 * which would eliminate the repeated lookup from root of ims nodes,
1008 * as they share the same key space.
1010 * If any error occurred this function will back out of refcounts
1011 * and return a non-zero value.
1014 inm_merge(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1016 struct ip_msource *ims, *nims;
1017 struct in_msource *lims;
1018 int schanged, error;
1024 IN_MULTI_LIST_LOCK_ASSERT();
1027 * Update the source filters first, as this may fail.
1028 * Maintain count of in-mode filters at t0, t1. These are
1029 * used to work out if we transition into ASM mode or not.
1030 * Maintain a count of source filters whose state was
1031 * actually modified by this operation.
1033 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
1034 lims = (struct in_msource *)ims;
1035 if (lims->imsl_st[0] == imf->imf_st[0]) nsrc0++;
1036 if (lims->imsl_st[1] == imf->imf_st[1]) nsrc1++;
1037 if (lims->imsl_st[0] == lims->imsl_st[1]) continue;
1038 error = inm_get_source(inm, lims->ims_haddr, 0, &nims);
1042 ims_merge(nims, lims, 0);
1045 struct ip_msource *bims;
1047 RB_FOREACH_REVERSE_FROM(ims, ip_msource_tree, nims) {
1048 lims = (struct in_msource *)ims;
1049 if (lims->imsl_st[0] == lims->imsl_st[1])
1051 (void)inm_get_source(inm, lims->ims_haddr, 1, &bims);
1054 ims_merge(bims, lims, 1);
1059 CTR3(KTR_IGMPV3, "%s: imf filters in-mode: %d at t0, %d at t1",
1060 __func__, nsrc0, nsrc1);
1062 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1063 if (imf->imf_st[0] == imf->imf_st[1] &&
1064 imf->imf_st[1] == MCAST_INCLUDE) {
1066 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
1067 --inm->inm_st[1].iss_in;
1071 /* Handle filter mode transition on socket. */
1072 if (imf->imf_st[0] != imf->imf_st[1]) {
1073 CTR3(KTR_IGMPV3, "%s: imf transition %d to %d",
1074 __func__, imf->imf_st[0], imf->imf_st[1]);
1076 if (imf->imf_st[0] == 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[0] == MCAST_INCLUDE) {
1080 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
1081 --inm->inm_st[1].iss_in;
1084 if (imf->imf_st[1] == MCAST_EXCLUDE) {
1085 CTR1(KTR_IGMPV3, "%s: ex++ on inm at t1", __func__);
1086 inm->inm_st[1].iss_ex++;
1087 } else if (imf->imf_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
1088 CTR1(KTR_IGMPV3, "%s: in++ on inm at t1", __func__);
1089 inm->inm_st[1].iss_in++;
1094 * Track inm filter state in terms of listener counts.
1095 * If there are any exclusive listeners, stack-wide
1096 * membership is exclusive.
1097 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1098 * If no listeners remain, state is undefined at t1,
1099 * and the IGMP lifecycle for this group should finish.
1101 if (inm->inm_st[1].iss_ex > 0) {
1102 CTR1(KTR_IGMPV3, "%s: transition to EX", __func__);
1103 inm->inm_st[1].iss_fmode = MCAST_EXCLUDE;
1104 } else if (inm->inm_st[1].iss_in > 0) {
1105 CTR1(KTR_IGMPV3, "%s: transition to IN", __func__);
1106 inm->inm_st[1].iss_fmode = MCAST_INCLUDE;
1108 CTR1(KTR_IGMPV3, "%s: transition to UNDEF", __func__);
1109 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
1112 /* Decrement ASM listener count on transition out of ASM mode. */
1113 if (imf->imf_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1114 if ((imf->imf_st[1] != MCAST_EXCLUDE) ||
1115 (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;
1121 /* Increment ASM listener count on transition to ASM mode. */
1122 if (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1123 CTR1(KTR_IGMPV3, "%s: asm++ on inm at t1", __func__);
1124 inm->inm_st[1].iss_asm++;
1127 CTR3(KTR_IGMPV3, "%s: merged imf %p to inm %p", __func__, imf, inm);
1132 CTR1(KTR_IGMPV3, "%s: sources changed; reaping", __func__);
1139 * Mark an in_multi's filter set deltas as committed.
1140 * Called by IGMP after a state change has been enqueued.
1143 inm_commit(struct in_multi *inm)
1145 struct ip_msource *ims;
1147 CTR2(KTR_IGMPV3, "%s: commit inm %p", __func__, inm);
1148 CTR1(KTR_IGMPV3, "%s: pre commit:", __func__);
1151 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
1152 ims->ims_st[0] = ims->ims_st[1];
1154 inm->inm_st[0] = inm->inm_st[1];
1158 * Reap unreferenced nodes from an in_multi's filter set.
1161 inm_reap(struct in_multi *inm)
1163 struct ip_msource *ims, *tims;
1165 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1166 if (ims->ims_st[0].ex > 0 || ims->ims_st[0].in > 0 ||
1167 ims->ims_st[1].ex > 0 || ims->ims_st[1].in > 0 ||
1170 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1171 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1172 free(ims, M_IPMSOURCE);
1178 * Purge all source nodes from an in_multi's filter set.
1181 inm_purge(struct in_multi *inm)
1183 struct ip_msource *ims, *tims;
1185 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1186 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1187 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1188 free(ims, M_IPMSOURCE);
1194 * Join a multicast group; unlocked entry point.
1196 * SMPng: XXX: in_joingroup() is called from in_control() when Giant
1197 * is not held. Fortunately, ifp is unlikely to have been detached
1198 * at this point, so we assume it's OK to recurse.
1201 in_joingroup(struct ifnet *ifp, const struct in_addr *gina,
1202 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1207 error = in_joingroup_locked(ifp, gina, imf, pinm);
1214 * Join a multicast group; real entry point.
1216 * Only preserves atomicity at inm level.
1217 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1219 * If the IGMP downcall fails, the group is not joined, and an error
1223 in_joingroup_locked(struct ifnet *ifp, const struct in_addr *gina,
1224 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1226 struct in_mfilter timf;
1227 struct in_multi *inm;
1230 IN_MULTI_LOCK_ASSERT();
1231 IN_MULTI_LIST_UNLOCK_ASSERT();
1233 CTR4(KTR_IGMPV3, "%s: join 0x%08x on %p(%s))", __func__,
1234 ntohl(gina->s_addr), ifp, ifp->if_xname);
1240 * If no imf was specified (i.e. kernel consumer),
1241 * fake one up and assume it is an ASM join.
1244 imf_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1248 error = in_getmulti(ifp, gina, &inm);
1250 CTR1(KTR_IGMPV3, "%s: in_getmulti() failure", __func__);
1253 IN_MULTI_LIST_LOCK();
1254 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1255 error = inm_merge(inm, imf);
1257 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1258 goto out_inm_release;
1261 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1262 error = igmp_change_state(inm);
1264 CTR1(KTR_IGMPV3, "%s: failed to update source", __func__);
1265 goto out_inm_release;
1271 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1273 inm_release_deferred(inm);
1274 IF_ADDR_WUNLOCK(ifp);
1278 IN_MULTI_LIST_UNLOCK();
1284 * Leave a multicast group; unlocked entry point.
1287 in_leavegroup(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1292 error = in_leavegroup_locked(inm, imf);
1299 * Leave a multicast group; real entry point.
1300 * All source filters will be expunged.
1302 * Only preserves atomicity at inm level.
1304 * Holding the write lock for the INP which contains imf
1305 * is highly advisable. We can't assert for it as imf does not
1306 * contain a back-pointer to the owning inp.
1308 * Note: This is not the same as inm_release(*) as this function also
1309 * makes a state change downcall into IGMP.
1312 in_leavegroup_locked(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1314 struct in_mfilter timf;
1317 IN_MULTI_LOCK_ASSERT();
1318 IN_MULTI_LIST_UNLOCK_ASSERT();
1322 CTR5(KTR_IGMPV3, "%s: leave inm %p, 0x%08x/%s, imf %p", __func__,
1323 inm, ntohl(inm->inm_addr.s_addr),
1324 (inm_is_ifp_detached(inm) ? "null" : inm->inm_ifp->if_xname),
1328 * If no imf was specified (i.e. kernel consumer),
1329 * fake one up and assume it is an ASM join.
1332 imf_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1337 * Begin state merge transaction at IGMP layer.
1339 * As this particular invocation should not cause any memory
1340 * to be allocated, and there is no opportunity to roll back
1341 * the transaction, it MUST NOT fail.
1343 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1344 IN_MULTI_LIST_LOCK();
1345 error = inm_merge(inm, imf);
1346 KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1348 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1349 CURVNET_SET(inm->inm_ifp->if_vnet);
1350 error = igmp_change_state(inm);
1351 IF_ADDR_WLOCK(inm->inm_ifp);
1352 inm_release_deferred(inm);
1353 IF_ADDR_WUNLOCK(inm->inm_ifp);
1354 IN_MULTI_LIST_UNLOCK();
1357 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1359 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1364 /*#ifndef BURN_BRIDGES*/
1366 * Join an IPv4 multicast group in (*,G) exclusive mode.
1367 * The group must be a 224.0.0.0/24 link-scope group.
1368 * This KPI is for legacy kernel consumers only.
1371 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1373 struct in_multi *pinm;
1376 char addrbuf[INET_ADDRSTRLEN];
1379 KASSERT(IN_LOCAL_GROUP(ntohl(ap->s_addr)),
1380 ("%s: %s not in 224.0.0.0/24", __func__,
1381 inet_ntoa_r(*ap, addrbuf)));
1383 error = in_joingroup(ifp, ap, NULL, &pinm);
1391 * Block or unblock an ASM multicast source on an inpcb.
1392 * This implements the delta-based API described in RFC 3678.
1394 * The delta-based API applies only to exclusive-mode memberships.
1395 * An IGMP downcall will be performed.
1397 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1399 * Return 0 if successful, otherwise return an appropriate error code.
1402 inp_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1404 struct group_source_req gsr;
1405 struct rm_priotracker in_ifa_tracker;
1406 sockunion_t *gsa, *ssa;
1408 struct in_mfilter *imf;
1409 struct ip_moptions *imo;
1410 struct in_msource *ims;
1411 struct in_multi *inm;
1419 memset(&gsr, 0, sizeof(struct group_source_req));
1420 gsa = (sockunion_t *)&gsr.gsr_group;
1421 ssa = (sockunion_t *)&gsr.gsr_source;
1423 switch (sopt->sopt_name) {
1424 case IP_BLOCK_SOURCE:
1425 case IP_UNBLOCK_SOURCE: {
1426 struct ip_mreq_source mreqs;
1428 error = sooptcopyin(sopt, &mreqs,
1429 sizeof(struct ip_mreq_source),
1430 sizeof(struct ip_mreq_source));
1434 gsa->sin.sin_family = AF_INET;
1435 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1436 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1438 ssa->sin.sin_family = AF_INET;
1439 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1440 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1442 if (!in_nullhost(mreqs.imr_interface)) {
1443 IN_IFADDR_RLOCK(&in_ifa_tracker);
1444 INADDR_TO_IFP(mreqs.imr_interface, ifp);
1445 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1447 if (sopt->sopt_name == IP_BLOCK_SOURCE)
1450 CTR3(KTR_IGMPV3, "%s: imr_interface = 0x%08x, ifp = %p",
1451 __func__, ntohl(mreqs.imr_interface.s_addr), ifp);
1455 case MCAST_BLOCK_SOURCE:
1456 case MCAST_UNBLOCK_SOURCE:
1457 error = sooptcopyin(sopt, &gsr,
1458 sizeof(struct group_source_req),
1459 sizeof(struct group_source_req));
1463 if (gsa->sin.sin_family != AF_INET ||
1464 gsa->sin.sin_len != sizeof(struct sockaddr_in))
1467 if (ssa->sin.sin_family != AF_INET ||
1468 ssa->sin.sin_len != sizeof(struct sockaddr_in))
1471 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1472 return (EADDRNOTAVAIL);
1474 ifp = ifnet_byindex(gsr.gsr_interface);
1476 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1481 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
1482 __func__, sopt->sopt_name);
1483 return (EOPNOTSUPP);
1487 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1493 * Check if we are actually a member of this group.
1495 imo = inp_findmoptions(inp);
1496 imf = imo_match_group(imo, ifp, &gsa->sa);
1498 error = EADDRNOTAVAIL;
1499 goto out_inp_locked;
1504 * Attempting to use the delta-based API on an
1505 * non exclusive-mode membership is an error.
1507 fmode = imf->imf_st[0];
1508 if (fmode != MCAST_EXCLUDE) {
1510 goto out_inp_locked;
1514 * Deal with error cases up-front:
1515 * Asked to block, but already blocked; or
1516 * Asked to unblock, but nothing to unblock.
1517 * If adding a new block entry, allocate it.
1519 ims = imo_match_source(imf, &ssa->sa);
1520 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1521 CTR3(KTR_IGMPV3, "%s: source 0x%08x %spresent", __func__,
1522 ntohl(ssa->sin.sin_addr.s_addr), doblock ? "" : "not ");
1523 error = EADDRNOTAVAIL;
1524 goto out_inp_locked;
1527 INP_WLOCK_ASSERT(inp);
1530 * Begin state merge transaction at socket layer.
1533 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
1534 ims = imf_graft(imf, fmode, &ssa->sin);
1538 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
1539 error = imf_prune(imf, &ssa->sin);
1543 CTR1(KTR_IGMPV3, "%s: merge imf state failed", __func__);
1544 goto out_imf_rollback;
1548 * Begin state merge transaction at IGMP layer.
1550 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1551 IN_MULTI_LIST_LOCK();
1552 error = inm_merge(inm, imf);
1554 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1555 IN_MULTI_LIST_UNLOCK();
1556 goto out_imf_rollback;
1559 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1560 error = igmp_change_state(inm);
1561 IN_MULTI_LIST_UNLOCK();
1563 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1580 * Given an inpcb, return its multicast options structure pointer. Accepts
1581 * an unlocked inpcb pointer, but will return it locked. May sleep.
1583 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1584 * SMPng: NOTE: Returns with the INP write lock held.
1586 static struct ip_moptions *
1587 inp_findmoptions(struct inpcb *inp)
1589 struct ip_moptions *imo;
1592 if (inp->inp_moptions != NULL)
1593 return (inp->inp_moptions);
1597 imo = malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK);
1599 imo->imo_multicast_ifp = NULL;
1600 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1601 imo->imo_multicast_vif = -1;
1602 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1603 imo->imo_multicast_loop = in_mcast_loop;
1604 STAILQ_INIT(&imo->imo_head);
1607 if (inp->inp_moptions != NULL) {
1608 free(imo, M_IPMOPTS);
1609 return (inp->inp_moptions);
1611 inp->inp_moptions = imo;
1616 inp_gcmoptions(struct ip_moptions *imo)
1618 struct in_mfilter *imf;
1619 struct in_multi *inm;
1622 while ((imf = ip_mfilter_first(&imo->imo_head)) != NULL) {
1623 ip_mfilter_remove(&imo->imo_head, imf);
1626 if ((inm = imf->imf_inm) != NULL) {
1627 if ((ifp = inm->inm_ifp) != NULL) {
1628 CURVNET_SET(ifp->if_vnet);
1629 (void)in_leavegroup(inm, imf);
1632 (void)in_leavegroup(inm, imf);
1635 ip_mfilter_free(imf);
1637 free(imo, M_IPMOPTS);
1641 * Discard the IP multicast options (and source filters). To minimize
1642 * the amount of work done while holding locks such as the INP's
1643 * pcbinfo lock (which is used in the receive path), the free
1644 * operation is deferred to the epoch callback task.
1647 inp_freemoptions(struct ip_moptions *imo)
1651 inp_gcmoptions(imo);
1655 * Atomically get source filters on a socket for an IPv4 multicast group.
1656 * Called with INP lock held; returns with lock released.
1659 inp_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1661 struct __msfilterreq msfr;
1664 struct ip_moptions *imo;
1665 struct in_mfilter *imf;
1666 struct ip_msource *ims;
1667 struct in_msource *lims;
1668 struct sockaddr_in *psin;
1669 struct sockaddr_storage *ptss;
1670 struct sockaddr_storage *tss;
1672 size_t nsrcs, ncsrcs;
1674 INP_WLOCK_ASSERT(inp);
1676 imo = inp->inp_moptions;
1677 KASSERT(imo != NULL, ("%s: null ip_moptions", __func__));
1681 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1682 sizeof(struct __msfilterreq));
1686 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
1689 ifp = ifnet_byindex(msfr.msfr_ifindex);
1696 * Lookup group on the socket.
1698 gsa = (sockunion_t *)&msfr.msfr_group;
1699 imf = imo_match_group(imo, ifp, &gsa->sa);
1702 return (EADDRNOTAVAIL);
1706 * Ignore memberships which are in limbo.
1708 if (imf->imf_st[1] == MCAST_UNDEFINED) {
1712 msfr.msfr_fmode = imf->imf_st[1];
1715 * If the user specified a buffer, copy out the source filter
1716 * entries to userland gracefully.
1717 * We only copy out the number of entries which userland
1718 * has asked for, but we always tell userland how big the
1719 * buffer really needs to be.
1721 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
1722 msfr.msfr_nsrcs = in_mcast_maxsocksrc;
1724 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1725 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1726 M_TEMP, M_NOWAIT | M_ZERO);
1734 * Count number of sources in-mode at t0.
1735 * If buffer space exists and remains, copy out source entries.
1737 nsrcs = msfr.msfr_nsrcs;
1740 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
1741 lims = (struct in_msource *)ims;
1742 if (lims->imsl_st[0] == MCAST_UNDEFINED ||
1743 lims->imsl_st[0] != imf->imf_st[0])
1746 if (tss != NULL && nsrcs > 0) {
1747 psin = (struct sockaddr_in *)ptss;
1748 psin->sin_family = AF_INET;
1749 psin->sin_len = sizeof(struct sockaddr_in);
1750 psin->sin_addr.s_addr = htonl(lims->ims_haddr);
1760 error = copyout(tss, msfr.msfr_srcs,
1761 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1767 msfr.msfr_nsrcs = ncsrcs;
1768 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1774 * Return the IP multicast options in response to user getsockopt().
1777 inp_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1779 struct rm_priotracker in_ifa_tracker;
1780 struct ip_mreqn mreqn;
1781 struct ip_moptions *imo;
1783 struct in_ifaddr *ia;
1788 imo = inp->inp_moptions;
1790 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1791 * or is a divert socket, reject it.
1793 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1794 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1795 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
1797 return (EOPNOTSUPP);
1801 switch (sopt->sopt_name) {
1802 case IP_MULTICAST_VIF:
1804 optval = imo->imo_multicast_vif;
1808 error = sooptcopyout(sopt, &optval, sizeof(int));
1811 case IP_MULTICAST_IF:
1812 memset(&mreqn, 0, sizeof(struct ip_mreqn));
1814 ifp = imo->imo_multicast_ifp;
1815 if (!in_nullhost(imo->imo_multicast_addr)) {
1816 mreqn.imr_address = imo->imo_multicast_addr;
1817 } else if (ifp != NULL) {
1818 struct epoch_tracker et;
1820 mreqn.imr_ifindex = ifp->if_index;
1821 NET_EPOCH_ENTER(et);
1822 IFP_TO_IA(ifp, ia, &in_ifa_tracker);
1825 IA_SIN(ia)->sin_addr;
1830 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
1831 error = sooptcopyout(sopt, &mreqn,
1832 sizeof(struct ip_mreqn));
1834 error = sooptcopyout(sopt, &mreqn.imr_address,
1835 sizeof(struct in_addr));
1839 case IP_MULTICAST_TTL:
1841 optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1843 optval = coptval = imo->imo_multicast_ttl;
1845 if (sopt->sopt_valsize == sizeof(u_char))
1846 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1848 error = sooptcopyout(sopt, &optval, sizeof(int));
1851 case IP_MULTICAST_LOOP:
1853 optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1855 optval = coptval = imo->imo_multicast_loop;
1857 if (sopt->sopt_valsize == sizeof(u_char))
1858 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1860 error = sooptcopyout(sopt, &optval, sizeof(int));
1865 error = EADDRNOTAVAIL;
1868 error = inp_get_source_filters(inp, sopt);
1874 error = ENOPROTOOPT;
1878 INP_UNLOCK_ASSERT(inp);
1884 * Look up the ifnet to use for a multicast group membership,
1885 * given the IPv4 address of an interface, and the IPv4 group address.
1887 * This routine exists to support legacy multicast applications
1888 * which do not understand that multicast memberships are scoped to
1889 * specific physical links in the networking stack, or which need
1890 * to join link-scope groups before IPv4 addresses are configured.
1892 * If inp is non-NULL, use this socket's current FIB number for any
1893 * required FIB lookup.
1894 * If ina is INADDR_ANY, look up the group address in the unicast FIB,
1895 * and use its ifp; usually, this points to the default next-hop.
1897 * If the FIB lookup fails, attempt to use the first non-loopback
1898 * interface with multicast capability in the system as a
1899 * last resort. The legacy IPv4 ASM API requires that we do
1900 * this in order to allow groups to be joined when the routing
1901 * table has not yet been populated during boot.
1903 * Returns NULL if no ifp could be found.
1905 * FUTURE: Implement IPv4 source-address selection.
1907 static struct ifnet *
1908 inp_lookup_mcast_ifp(const struct inpcb *inp,
1909 const struct sockaddr_in *gsin, const struct in_addr ina)
1911 struct rm_priotracker in_ifa_tracker;
1913 struct nhop4_basic nh4;
1916 KASSERT(gsin->sin_family == AF_INET, ("%s: not AF_INET", __func__));
1917 KASSERT(IN_MULTICAST(ntohl(gsin->sin_addr.s_addr)),
1918 ("%s: not multicast", __func__));
1921 if (!in_nullhost(ina)) {
1922 IN_IFADDR_RLOCK(&in_ifa_tracker);
1923 INADDR_TO_IFP(ina, ifp);
1924 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1926 fibnum = inp ? inp->inp_inc.inc_fibnum : 0;
1927 if (fib4_lookup_nh_basic(fibnum, gsin->sin_addr, 0, 0, &nh4)==0)
1930 struct in_ifaddr *ia;
1934 IN_IFADDR_RLOCK(&in_ifa_tracker);
1935 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1937 if (!(mifp->if_flags & IFF_LOOPBACK) &&
1938 (mifp->if_flags & IFF_MULTICAST)) {
1943 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1951 * Join an IPv4 multicast group, possibly with a source.
1954 inp_join_group(struct inpcb *inp, struct sockopt *sopt)
1956 struct group_source_req gsr;
1957 sockunion_t *gsa, *ssa;
1959 struct in_mfilter *imf;
1960 struct ip_moptions *imo;
1961 struct in_multi *inm;
1962 struct in_msource *lims;
1969 memset(&gsr, 0, sizeof(struct group_source_req));
1970 gsa = (sockunion_t *)&gsr.gsr_group;
1971 gsa->ss.ss_family = AF_UNSPEC;
1972 ssa = (sockunion_t *)&gsr.gsr_source;
1973 ssa->ss.ss_family = AF_UNSPEC;
1975 switch (sopt->sopt_name) {
1976 case IP_ADD_MEMBERSHIP: {
1977 struct ip_mreqn mreqn;
1979 if (sopt->sopt_valsize == sizeof(struct ip_mreqn))
1980 error = sooptcopyin(sopt, &mreqn,
1981 sizeof(struct ip_mreqn), sizeof(struct ip_mreqn));
1983 error = sooptcopyin(sopt, &mreqn,
1984 sizeof(struct ip_mreq), sizeof(struct ip_mreq));
1988 gsa->sin.sin_family = AF_INET;
1989 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1990 gsa->sin.sin_addr = mreqn.imr_multiaddr;
1991 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1994 if (sopt->sopt_valsize == sizeof(struct ip_mreqn) &&
1995 mreqn.imr_ifindex != 0)
1996 ifp = ifnet_byindex(mreqn.imr_ifindex);
1998 ifp = inp_lookup_mcast_ifp(inp, &gsa->sin,
2002 case IP_ADD_SOURCE_MEMBERSHIP: {
2003 struct ip_mreq_source mreqs;
2005 error = sooptcopyin(sopt, &mreqs, sizeof(struct ip_mreq_source),
2006 sizeof(struct ip_mreq_source));
2010 gsa->sin.sin_family = ssa->sin.sin_family = AF_INET;
2011 gsa->sin.sin_len = ssa->sin.sin_len =
2012 sizeof(struct sockaddr_in);
2014 gsa->sin.sin_addr = mreqs.imr_multiaddr;
2015 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2018 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
2020 ifp = inp_lookup_mcast_ifp(inp, &gsa->sin,
2021 mreqs.imr_interface);
2022 CTR3(KTR_IGMPV3, "%s: imr_interface = 0x%08x, ifp = %p",
2023 __func__, ntohl(mreqs.imr_interface.s_addr), ifp);
2027 case MCAST_JOIN_GROUP:
2028 case MCAST_JOIN_SOURCE_GROUP:
2029 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
2030 error = sooptcopyin(sopt, &gsr,
2031 sizeof(struct group_req),
2032 sizeof(struct group_req));
2033 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
2034 error = sooptcopyin(sopt, &gsr,
2035 sizeof(struct group_source_req),
2036 sizeof(struct group_source_req));
2041 if (gsa->sin.sin_family != AF_INET ||
2042 gsa->sin.sin_len != sizeof(struct sockaddr_in))
2046 * Overwrite the port field if present, as the sockaddr
2047 * being copied in may be matched with a binary comparison.
2049 gsa->sin.sin_port = 0;
2050 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
2051 if (ssa->sin.sin_family != AF_INET ||
2052 ssa->sin.sin_len != sizeof(struct sockaddr_in))
2054 ssa->sin.sin_port = 0;
2057 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2060 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
2061 return (EADDRNOTAVAIL);
2062 ifp = ifnet_byindex(gsr.gsr_interface);
2066 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2067 __func__, sopt->sopt_name);
2068 return (EOPNOTSUPP);
2072 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
2073 return (EADDRNOTAVAIL);
2078 * Find the membership in the membership list.
2080 imo = inp_findmoptions(inp);
2081 imf = imo_match_group(imo, ifp, &gsa->sa);
2086 if (ip_mfilter_count(&imo->imo_head) >= IP_MAX_MEMBERSHIPS) {
2088 goto out_inp_locked;
2094 if (ssa->ss.ss_family != AF_UNSPEC) {
2096 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2097 * is an error. On an existing inclusive membership,
2098 * it just adds the source to the filter list.
2100 if (imf->imf_st[1] != MCAST_INCLUDE) {
2102 goto out_inp_locked;
2105 * Throw out duplicates.
2107 * XXX FIXME: This makes a naive assumption that
2108 * even if entries exist for *ssa in this imf,
2109 * they will be rejected as dupes, even if they
2110 * are not valid in the current mode (in-mode).
2112 * in_msource is transactioned just as for anything
2113 * else in SSM -- but note naive use of inm_graft()
2114 * below for allocating new filter entries.
2116 * This is only an issue if someone mixes the
2117 * full-state SSM API with the delta-based API,
2118 * which is discouraged in the relevant RFCs.
2120 lims = imo_match_source(imf, &ssa->sa);
2121 if (lims != NULL /*&&
2122 lims->imsl_st[1] == MCAST_INCLUDE*/) {
2123 error = EADDRNOTAVAIL;
2124 goto out_inp_locked;
2128 * MCAST_JOIN_GROUP on an existing exclusive
2129 * membership is an error; return EADDRINUSE
2130 * to preserve 4.4BSD API idempotence, and
2131 * avoid tedious detour to code below.
2132 * NOTE: This is bending RFC 3678 a bit.
2134 * On an existing inclusive membership, this is also
2135 * an error; if you want to change filter mode,
2136 * you must use the userland API setsourcefilter().
2137 * XXX We don't reject this for imf in UNDEFINED
2138 * state at t1, because allocation of a filter
2139 * is atomic with allocation of a membership.
2142 if (imf->imf_st[1] == MCAST_EXCLUDE)
2144 goto out_inp_locked;
2149 * Begin state merge transaction at socket layer.
2151 INP_WLOCK_ASSERT(inp);
2154 * Graft new source into filter list for this inpcb's
2155 * membership of the group. The in_multi may not have
2156 * been allocated yet if this is a new membership, however,
2157 * the in_mfilter slot will be allocated and must be initialized.
2159 * Note: Grafting of exclusive mode filters doesn't happen
2161 * XXX: Should check for non-NULL lims (node exists but may
2162 * not be in-mode) for interop with full-state API.
2164 if (ssa->ss.ss_family != AF_UNSPEC) {
2165 /* Membership starts in IN mode */
2167 CTR1(KTR_IGMPV3, "%s: new join w/source", __func__);
2168 imf = ip_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_INCLUDE);
2171 goto out_inp_locked;
2174 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
2176 lims = imf_graft(imf, MCAST_INCLUDE, &ssa->sin);
2178 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2181 goto out_inp_locked;
2184 /* No address specified; Membership starts in EX mode */
2186 CTR1(KTR_IGMPV3, "%s: new join w/o source", __func__);
2187 imf = ip_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_EXCLUDE);
2190 goto out_inp_locked;
2196 * Begin state merge transaction at IGMP layer.
2202 error = in_joingroup_locked(ifp, &gsa->sin.sin_addr, imf,
2206 if (in_pcbrele_wlocked(inp)) {
2208 goto out_inp_unlocked;
2211 CTR1(KTR_IGMPV3, "%s: in_joingroup_locked failed",
2213 goto out_inp_locked;
2216 * NOTE: Refcount from in_joingroup_locked()
2217 * is protecting membership.
2219 ip_mfilter_insert(&imo->imo_head, imf);
2221 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2222 IN_MULTI_LIST_LOCK();
2223 error = inm_merge(inm, imf);
2225 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2227 IN_MULTI_LIST_UNLOCK();
2230 goto out_inp_locked;
2232 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2233 error = igmp_change_state(inm);
2234 IN_MULTI_LIST_UNLOCK();
2236 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2240 goto out_inp_locked;
2252 if (is_new && imf) {
2253 if (imf->imf_inm != NULL) {
2254 IN_MULTI_LIST_LOCK();
2256 inm_release_deferred(imf->imf_inm);
2257 IF_ADDR_WUNLOCK(ifp);
2258 IN_MULTI_LIST_UNLOCK();
2260 ip_mfilter_free(imf);
2266 * Leave an IPv4 multicast group on an inpcb, possibly with a source.
2269 inp_leave_group(struct inpcb *inp, struct sockopt *sopt)
2271 struct group_source_req gsr;
2272 struct ip_mreq_source mreqs;
2273 struct rm_priotracker in_ifa_tracker;
2274 sockunion_t *gsa, *ssa;
2276 struct in_mfilter *imf;
2277 struct ip_moptions *imo;
2278 struct in_msource *ims;
2279 struct in_multi *inm;
2287 memset(&gsr, 0, sizeof(struct group_source_req));
2288 gsa = (sockunion_t *)&gsr.gsr_group;
2289 gsa->ss.ss_family = AF_UNSPEC;
2290 ssa = (sockunion_t *)&gsr.gsr_source;
2291 ssa->ss.ss_family = AF_UNSPEC;
2293 switch (sopt->sopt_name) {
2294 case IP_DROP_MEMBERSHIP:
2295 case IP_DROP_SOURCE_MEMBERSHIP:
2296 if (sopt->sopt_name == IP_DROP_MEMBERSHIP) {
2297 error = sooptcopyin(sopt, &mreqs,
2298 sizeof(struct ip_mreq),
2299 sizeof(struct ip_mreq));
2301 * Swap interface and sourceaddr arguments,
2302 * as ip_mreq and ip_mreq_source are laid
2305 mreqs.imr_interface = mreqs.imr_sourceaddr;
2306 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
2307 } else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2308 error = sooptcopyin(sopt, &mreqs,
2309 sizeof(struct ip_mreq_source),
2310 sizeof(struct ip_mreq_source));
2315 gsa->sin.sin_family = AF_INET;
2316 gsa->sin.sin_len = sizeof(struct sockaddr_in);
2317 gsa->sin.sin_addr = mreqs.imr_multiaddr;
2319 if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2320 ssa->sin.sin_family = AF_INET;
2321 ssa->sin.sin_len = sizeof(struct sockaddr_in);
2322 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
2326 * Attempt to look up hinted ifp from interface address.
2327 * Fallthrough with null ifp iff lookup fails, to
2328 * preserve 4.4BSD mcast API idempotence.
2329 * XXX NOTE WELL: The RFC 3678 API is preferred because
2330 * using an IPv4 address as a key is racy.
2332 if (!in_nullhost(mreqs.imr_interface)) {
2333 IN_IFADDR_RLOCK(&in_ifa_tracker);
2334 INADDR_TO_IFP(mreqs.imr_interface, ifp);
2335 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
2337 CTR3(KTR_IGMPV3, "%s: imr_interface = 0x%08x, ifp = %p",
2338 __func__, ntohl(mreqs.imr_interface.s_addr), ifp);
2342 case MCAST_LEAVE_GROUP:
2343 case MCAST_LEAVE_SOURCE_GROUP:
2344 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2345 error = sooptcopyin(sopt, &gsr,
2346 sizeof(struct group_req),
2347 sizeof(struct group_req));
2348 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2349 error = sooptcopyin(sopt, &gsr,
2350 sizeof(struct group_source_req),
2351 sizeof(struct group_source_req));
2356 if (gsa->sin.sin_family != AF_INET ||
2357 gsa->sin.sin_len != sizeof(struct sockaddr_in))
2360 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2361 if (ssa->sin.sin_family != AF_INET ||
2362 ssa->sin.sin_len != sizeof(struct sockaddr_in))
2366 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
2367 return (EADDRNOTAVAIL);
2369 ifp = ifnet_byindex(gsr.gsr_interface);
2372 return (EADDRNOTAVAIL);
2376 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2377 __func__, sopt->sopt_name);
2378 return (EOPNOTSUPP);
2382 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2388 * Find the membership in the membership list.
2390 imo = inp_findmoptions(inp);
2391 imf = imo_match_group(imo, ifp, &gsa->sa);
2393 error = EADDRNOTAVAIL;
2394 goto out_inp_locked;
2398 if (ssa->ss.ss_family != AF_UNSPEC)
2402 * Begin state merge transaction at socket layer.
2404 INP_WLOCK_ASSERT(inp);
2407 * If we were instructed only to leave a given source, do so.
2408 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2411 ip_mfilter_remove(&imo->imo_head, imf);
2415 * Give up the multicast address record to which
2416 * the membership points.
2418 (void) in_leavegroup_locked(imf->imf_inm, imf);
2420 if (imf->imf_st[0] == MCAST_EXCLUDE) {
2421 error = EADDRNOTAVAIL;
2422 goto out_inp_locked;
2424 ims = imo_match_source(imf, &ssa->sa);
2426 CTR3(KTR_IGMPV3, "%s: source 0x%08x %spresent",
2427 __func__, ntohl(ssa->sin.sin_addr.s_addr), "not ");
2428 error = EADDRNOTAVAIL;
2429 goto out_inp_locked;
2431 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
2432 error = imf_prune(imf, &ssa->sin);
2434 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2436 goto out_inp_locked;
2441 * Begin state merge transaction at IGMP layer.
2444 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2445 IN_MULTI_LIST_LOCK();
2446 error = inm_merge(inm, imf);
2448 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2450 IN_MULTI_LIST_UNLOCK();
2453 goto out_inp_locked;
2456 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2457 error = igmp_change_state(inm);
2458 IN_MULTI_LIST_UNLOCK();
2460 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2464 goto out_inp_locked;
2473 if (is_final && imf)
2474 ip_mfilter_free(imf);
2481 * Select the interface for transmitting IPv4 multicast datagrams.
2483 * Either an instance of struct in_addr or an instance of struct ip_mreqn
2484 * may be passed to this socket option. An address of INADDR_ANY or an
2485 * interface index of 0 is used to remove a previous selection.
2486 * When no interface is selected, one is chosen for every send.
2489 inp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2491 struct rm_priotracker in_ifa_tracker;
2492 struct in_addr addr;
2493 struct ip_mreqn mreqn;
2495 struct ip_moptions *imo;
2498 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
2500 * An interface index was specified using the
2501 * Linux-derived ip_mreqn structure.
2503 error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn),
2504 sizeof(struct ip_mreqn));
2508 if (mreqn.imr_ifindex < 0 || V_if_index < mreqn.imr_ifindex)
2511 if (mreqn.imr_ifindex == 0) {
2514 ifp = ifnet_byindex(mreqn.imr_ifindex);
2516 return (EADDRNOTAVAIL);
2520 * An interface was specified by IPv4 address.
2521 * This is the traditional BSD usage.
2523 error = sooptcopyin(sopt, &addr, sizeof(struct in_addr),
2524 sizeof(struct in_addr));
2527 if (in_nullhost(addr)) {
2530 IN_IFADDR_RLOCK(&in_ifa_tracker);
2531 INADDR_TO_IFP(addr, ifp);
2532 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
2534 return (EADDRNOTAVAIL);
2536 CTR3(KTR_IGMPV3, "%s: ifp = %p, addr = 0x%08x", __func__, ifp,
2537 ntohl(addr.s_addr));
2540 /* Reject interfaces which do not support multicast. */
2541 if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0)
2542 return (EOPNOTSUPP);
2544 imo = inp_findmoptions(inp);
2545 imo->imo_multicast_ifp = ifp;
2546 imo->imo_multicast_addr.s_addr = INADDR_ANY;
2553 * Atomically set source filters on a socket for an IPv4 multicast group.
2555 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
2558 inp_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2560 struct __msfilterreq msfr;
2563 struct in_mfilter *imf;
2564 struct ip_moptions *imo;
2565 struct in_multi *inm;
2568 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2569 sizeof(struct __msfilterreq));
2573 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
2576 if ((msfr.msfr_fmode != MCAST_EXCLUDE &&
2577 msfr.msfr_fmode != MCAST_INCLUDE))
2580 if (msfr.msfr_group.ss_family != AF_INET ||
2581 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in))
2584 gsa = (sockunion_t *)&msfr.msfr_group;
2585 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2588 gsa->sin.sin_port = 0; /* ignore port */
2590 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
2591 return (EADDRNOTAVAIL);
2593 ifp = ifnet_byindex(msfr.msfr_ifindex);
2595 return (EADDRNOTAVAIL);
2600 * Take the INP write lock.
2601 * Check if this socket is a member of this group.
2603 imo = inp_findmoptions(inp);
2604 imf = imo_match_group(imo, ifp, &gsa->sa);
2606 error = EADDRNOTAVAIL;
2607 goto out_inp_locked;
2612 * Begin state merge transaction at socket layer.
2614 INP_WLOCK_ASSERT(inp);
2616 imf->imf_st[1] = msfr.msfr_fmode;
2619 * Apply any new source filters, if present.
2620 * Make a copy of the user-space source vector so
2621 * that we may copy them with a single copyin. This
2622 * allows us to deal with page faults up-front.
2624 if (msfr.msfr_nsrcs > 0) {
2625 struct in_msource *lims;
2626 struct sockaddr_in *psin;
2627 struct sockaddr_storage *kss, *pkss;
2632 CTR2(KTR_IGMPV3, "%s: loading %lu source list entries",
2633 __func__, (unsigned long)msfr.msfr_nsrcs);
2634 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2636 error = copyin(msfr.msfr_srcs, kss,
2637 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2646 * Mark all source filters as UNDEFINED at t1.
2647 * Restore new group filter mode, as imf_leave()
2648 * will set it to INCLUDE.
2651 imf->imf_st[1] = msfr.msfr_fmode;
2654 * Update socket layer filters at t1, lazy-allocating
2655 * new entries. This saves a bunch of memory at the
2656 * cost of one RB_FIND() per source entry; duplicate
2657 * entries in the msfr_nsrcs vector are ignored.
2658 * If we encounter an error, rollback transaction.
2660 * XXX This too could be replaced with a set-symmetric
2661 * difference like loop to avoid walking from root
2662 * every time, as the key space is common.
2664 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2665 psin = (struct sockaddr_in *)pkss;
2666 if (psin->sin_family != AF_INET) {
2667 error = EAFNOSUPPORT;
2670 if (psin->sin_len != sizeof(struct sockaddr_in)) {
2674 error = imf_get_source(imf, psin, &lims);
2677 lims->imsl_st[1] = imf->imf_st[1];
2683 goto out_imf_rollback;
2685 INP_WLOCK_ASSERT(inp);
2688 * Begin state merge transaction at IGMP layer.
2690 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2691 IN_MULTI_LIST_LOCK();
2692 error = inm_merge(inm, imf);
2694 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
2695 IN_MULTI_LIST_UNLOCK();
2696 goto out_imf_rollback;
2699 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2700 error = igmp_change_state(inm);
2701 IN_MULTI_LIST_UNLOCK();
2703 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
2720 * Set the IP multicast options in response to user setsockopt().
2722 * Many of the socket options handled in this function duplicate the
2723 * functionality of socket options in the regular unicast API. However,
2724 * it is not possible to merge the duplicate code, because the idempotence
2725 * of the IPv4 multicast part of the BSD Sockets API must be preserved;
2726 * the effects of these options must be treated as separate and distinct.
2728 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2729 * FUTURE: The IP_MULTICAST_VIF option may be eliminated if MROUTING
2730 * is refactored to no longer use vifs.
2733 inp_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2735 struct ip_moptions *imo;
2741 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2742 * or is a divert socket, reject it.
2744 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
2745 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2746 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
2747 return (EOPNOTSUPP);
2749 switch (sopt->sopt_name) {
2750 case IP_MULTICAST_VIF: {
2753 * Select a multicast VIF for transmission.
2754 * Only useful if multicast forwarding is active.
2756 if (legal_vif_num == NULL) {
2760 error = sooptcopyin(sopt, &vifi, sizeof(int), sizeof(int));
2763 if (!legal_vif_num(vifi) && (vifi != -1)) {
2767 imo = inp_findmoptions(inp);
2768 imo->imo_multicast_vif = vifi;
2773 case IP_MULTICAST_IF:
2774 error = inp_set_multicast_if(inp, sopt);
2777 case IP_MULTICAST_TTL: {
2781 * Set the IP time-to-live for outgoing multicast packets.
2782 * The original multicast API required a char argument,
2783 * which is inconsistent with the rest of the socket API.
2784 * We allow either a char or an int.
2786 if (sopt->sopt_valsize == sizeof(u_char)) {
2787 error = sooptcopyin(sopt, &ttl, sizeof(u_char),
2794 error = sooptcopyin(sopt, &ittl, sizeof(u_int),
2804 imo = inp_findmoptions(inp);
2805 imo->imo_multicast_ttl = ttl;
2810 case IP_MULTICAST_LOOP: {
2814 * Set the loopback flag for outgoing multicast packets.
2815 * Must be zero or one. The original multicast API required a
2816 * char argument, which is inconsistent with the rest
2817 * of the socket API. We allow either a char or an int.
2819 if (sopt->sopt_valsize == sizeof(u_char)) {
2820 error = sooptcopyin(sopt, &loop, sizeof(u_char),
2827 error = sooptcopyin(sopt, &iloop, sizeof(u_int),
2831 loop = (u_char)iloop;
2833 imo = inp_findmoptions(inp);
2834 imo->imo_multicast_loop = !!loop;
2839 case IP_ADD_MEMBERSHIP:
2840 case IP_ADD_SOURCE_MEMBERSHIP:
2841 case MCAST_JOIN_GROUP:
2842 case MCAST_JOIN_SOURCE_GROUP:
2843 error = inp_join_group(inp, sopt);
2846 case IP_DROP_MEMBERSHIP:
2847 case IP_DROP_SOURCE_MEMBERSHIP:
2848 case MCAST_LEAVE_GROUP:
2849 case MCAST_LEAVE_SOURCE_GROUP:
2850 error = inp_leave_group(inp, sopt);
2853 case IP_BLOCK_SOURCE:
2854 case IP_UNBLOCK_SOURCE:
2855 case MCAST_BLOCK_SOURCE:
2856 case MCAST_UNBLOCK_SOURCE:
2857 error = inp_block_unblock_source(inp, sopt);
2861 error = inp_set_source_filters(inp, sopt);
2869 INP_UNLOCK_ASSERT(inp);
2875 * Expose IGMP's multicast filter mode and source list(s) to userland,
2876 * keyed by (ifindex, group).
2877 * The filter mode is written out as a uint32_t, followed by
2878 * 0..n of struct in_addr.
2879 * For use by ifmcstat(8).
2880 * SMPng: NOTE: unlocked read of ifindex space.
2883 sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS)
2885 struct in_addr src, group;
2886 struct epoch_tracker et;
2888 struct ifmultiaddr *ifma;
2889 struct in_multi *inm;
2890 struct ip_msource *ims;
2894 uint32_t fmode, ifindex;
2899 if (req->newptr != NULL)
2906 if (ifindex <= 0 || ifindex > V_if_index) {
2907 CTR2(KTR_IGMPV3, "%s: ifindex %u out of range",
2912 group.s_addr = name[1];
2913 if (!IN_MULTICAST(ntohl(group.s_addr))) {
2914 CTR2(KTR_IGMPV3, "%s: group 0x%08x is not multicast",
2915 __func__, ntohl(group.s_addr));
2919 NET_EPOCH_ENTER(et);
2920 ifp = ifnet_byindex(ifindex);
2923 CTR2(KTR_IGMPV3, "%s: no ifp for ifindex %u",
2928 retval = sysctl_wire_old_buffer(req,
2929 sizeof(uint32_t) + (in_mcast_maxgrpsrc * sizeof(struct in_addr)));
2935 IN_MULTI_LIST_LOCK();
2937 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2938 if (ifma->ifma_addr->sa_family != AF_INET ||
2939 ifma->ifma_protospec == NULL)
2941 inm = (struct in_multi *)ifma->ifma_protospec;
2942 if (!in_hosteq(inm->inm_addr, group))
2944 fmode = inm->inm_st[1].iss_fmode;
2945 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2948 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
2949 CTR2(KTR_IGMPV3, "%s: visit node 0x%08x", __func__,
2952 * Only copy-out sources which are in-mode.
2954 if (fmode != ims_get_mode(inm, ims, 1)) {
2955 CTR1(KTR_IGMPV3, "%s: skip non-in-mode",
2959 src.s_addr = htonl(ims->ims_haddr);
2960 retval = SYSCTL_OUT(req, &src, sizeof(struct in_addr));
2966 IN_MULTI_LIST_UNLOCK();
2972 #if defined(KTR) && (KTR_COMPILE & KTR_IGMPV3)
2974 static const char *inm_modestrs[] = {
2975 [MCAST_UNDEFINED] = "un",
2976 [MCAST_INCLUDE] = "in",
2977 [MCAST_EXCLUDE] = "ex",
2979 _Static_assert(MCAST_UNDEFINED == 0 &&
2980 MCAST_EXCLUDE + 1 == nitems(inm_modestrs),
2981 "inm_modestrs: no longer matches #defines");
2984 inm_mode_str(const int mode)
2987 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2988 return (inm_modestrs[mode]);
2992 static const char *inm_statestrs[] = {
2993 [IGMP_NOT_MEMBER] = "not-member",
2994 [IGMP_SILENT_MEMBER] = "silent",
2995 [IGMP_REPORTING_MEMBER] = "reporting",
2996 [IGMP_IDLE_MEMBER] = "idle",
2997 [IGMP_LAZY_MEMBER] = "lazy",
2998 [IGMP_SLEEPING_MEMBER] = "sleeping",
2999 [IGMP_AWAKENING_MEMBER] = "awakening",
3000 [IGMP_G_QUERY_PENDING_MEMBER] = "query-pending",
3001 [IGMP_SG_QUERY_PENDING_MEMBER] = "sg-query-pending",
3002 [IGMP_LEAVING_MEMBER] = "leaving",
3004 _Static_assert(IGMP_NOT_MEMBER == 0 &&
3005 IGMP_LEAVING_MEMBER + 1 == nitems(inm_statestrs),
3006 "inm_statetrs: no longer matches #defines");
3009 inm_state_str(const int state)
3012 if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
3013 return (inm_statestrs[state]);
3018 * Dump an in_multi structure to the console.
3021 inm_print(const struct in_multi *inm)
3024 char addrbuf[INET_ADDRSTRLEN];
3026 if ((ktr_mask & KTR_IGMPV3) == 0)
3029 printf("%s: --- begin inm %p ---\n", __func__, inm);
3030 printf("addr %s ifp %p(%s) ifma %p\n",
3031 inet_ntoa_r(inm->inm_addr, addrbuf),
3033 inm->inm_ifp->if_xname,
3035 printf("timer %u state %s refcount %u scq.len %u\n",
3037 inm_state_str(inm->inm_state),
3039 inm->inm_scq.mq_len);
3040 printf("igi %p nsrc %lu sctimer %u scrv %u\n",
3045 for (t = 0; t < 2; t++) {
3046 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
3047 inm_mode_str(inm->inm_st[t].iss_fmode),
3048 inm->inm_st[t].iss_asm,
3049 inm->inm_st[t].iss_ex,
3050 inm->inm_st[t].iss_in,
3051 inm->inm_st[t].iss_rec);
3053 printf("%s: --- end inm %p ---\n", __func__, inm);
3056 #else /* !KTR || !(KTR_COMPILE & KTR_IGMPV3) */
3059 inm_print(const struct in_multi *inm)
3064 #endif /* KTR && (KTR_COMPILE & KTR_IGMPV3) */
3066 RB_GENERATE(ip_msource_tree, ip_msource, ims_link, ip_msource_cmp);