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, CTLFLAG_RW, 0,
183 static u_long in_mcast_maxgrpsrc = IP_MAX_GROUP_SRC_FILTER;
184 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxgrpsrc,
185 CTLFLAG_RWTUN, &in_mcast_maxgrpsrc, 0,
186 "Max source filters per group");
188 static u_long in_mcast_maxsocksrc = IP_MAX_SOCK_SRC_FILTER;
189 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxsocksrc,
190 CTLFLAG_RWTUN, &in_mcast_maxsocksrc, 0,
191 "Max source filters per socket");
193 int in_mcast_loop = IP_DEFAULT_MULTICAST_LOOP;
194 SYSCTL_INT(_net_inet_ip_mcast, OID_AUTO, loop, CTLFLAG_RWTUN,
195 &in_mcast_loop, 0, "Loopback multicast datagrams by default");
197 static SYSCTL_NODE(_net_inet_ip_mcast, OID_AUTO, filters,
198 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip_mcast_filters,
199 "Per-interface stack-wide source filters");
203 * Inline function which wraps assertions for a valid ifp.
204 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
208 inm_is_ifp_detached(const struct in_multi *inm)
212 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
213 ifp = inm->inm_ifma->ifma_ifp;
216 * Sanity check that netinet's notion of ifp is the
219 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
222 return (ifp == NULL);
226 static struct grouptask free_gtask;
227 static struct in_multi_head inm_free_list;
228 static void inm_release_task(void *arg __unused);
229 static void inm_init(void)
231 SLIST_INIT(&inm_free_list);
232 taskqgroup_config_gtask_init(NULL, &free_gtask, inm_release_task, "inm release task");
235 #ifdef EARLY_AP_STARTUP
236 SYSINIT(inm_init, SI_SUB_SMP + 1, SI_ORDER_FIRST,
239 SYSINIT(inm_init, SI_SUB_ROOT_CONF - 1, SI_ORDER_FIRST,
245 inm_release_list_deferred(struct in_multi_head *inmh)
248 if (SLIST_EMPTY(inmh))
250 mtx_lock(&in_multi_free_mtx);
251 SLIST_CONCAT(&inm_free_list, inmh, in_multi, inm_nrele);
252 mtx_unlock(&in_multi_free_mtx);
253 GROUPTASK_ENQUEUE(&free_gtask);
257 inm_disconnect(struct in_multi *inm)
260 struct ifmultiaddr *ifma, *ll_ifma;
263 IF_ADDR_WLOCK_ASSERT(ifp);
264 ifma = inm->inm_ifma;
267 if (ifma->ifma_flags & IFMA_F_ENQUEUED) {
268 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
269 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
271 MCDPRINTF("removed ifma: %p from %s\n", ifma, ifp->if_xname);
272 if ((ll_ifma = ifma->ifma_llifma) != NULL) {
273 MPASS(ifma != ll_ifma);
274 ifma->ifma_llifma = NULL;
275 MPASS(ll_ifma->ifma_llifma == NULL);
276 MPASS(ll_ifma->ifma_ifp == ifp);
277 if (--ll_ifma->ifma_refcount == 0) {
278 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
279 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, ifma_link);
280 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
282 MCDPRINTF("removed ll_ifma: %p from %s\n", ll_ifma, ifp->if_xname);
283 if_freemulti(ll_ifma);
290 inm_release_deferred(struct in_multi *inm)
292 struct in_multi_head tmp;
294 IN_MULTI_LIST_LOCK_ASSERT();
295 MPASS(inm->inm_refcount > 0);
296 if (--inm->inm_refcount == 0) {
299 inm->inm_ifma->ifma_protospec = NULL;
300 SLIST_INSERT_HEAD(&tmp, inm, inm_nrele);
301 inm_release_list_deferred(&tmp);
306 inm_release_task(void *arg __unused)
308 struct in_multi_head inm_free_tmp;
309 struct in_multi *inm, *tinm;
311 SLIST_INIT(&inm_free_tmp);
312 mtx_lock(&in_multi_free_mtx);
313 SLIST_CONCAT(&inm_free_tmp, &inm_free_list, in_multi, inm_nrele);
314 mtx_unlock(&in_multi_free_mtx);
316 SLIST_FOREACH_SAFE(inm, &inm_free_tmp, inm_nrele, tinm) {
317 SLIST_REMOVE_HEAD(&inm_free_tmp, inm_nrele);
325 * Initialize an in_mfilter structure to a known state at t0, t1
326 * with an empty source filter list.
329 imf_init(struct in_mfilter *imf, const int st0, const int st1)
331 memset(imf, 0, sizeof(struct in_mfilter));
332 RB_INIT(&imf->imf_sources);
333 imf->imf_st[0] = st0;
334 imf->imf_st[1] = st1;
338 ip_mfilter_alloc(const int mflags, const int st0, const int st1)
340 struct in_mfilter *imf;
342 imf = malloc(sizeof(*imf), M_INMFILTER, mflags);
344 imf_init(imf, st0, st1);
350 ip_mfilter_free(struct in_mfilter *imf)
354 free(imf, M_INMFILTER);
358 * Function for looking up an in_multi record for an IPv4 multicast address
359 * on a given interface. ifp must be valid. If no record found, return NULL.
360 * The IN_MULTI_LIST_LOCK and IF_ADDR_LOCK on ifp must be held.
363 inm_lookup_locked(struct ifnet *ifp, const struct in_addr ina)
365 struct ifmultiaddr *ifma;
366 struct in_multi *inm;
368 IN_MULTI_LIST_LOCK_ASSERT();
369 IF_ADDR_LOCK_ASSERT(ifp);
372 CK_STAILQ_FOREACH(ifma, &((ifp)->if_multiaddrs), ifma_link) {
373 if (ifma->ifma_addr->sa_family != AF_INET ||
374 ifma->ifma_protospec == NULL)
376 inm = (struct in_multi *)ifma->ifma_protospec;
377 if (inm->inm_addr.s_addr == ina.s_addr)
385 * Wrapper for inm_lookup_locked().
386 * The IF_ADDR_LOCK will be taken on ifp and released on return.
389 inm_lookup(struct ifnet *ifp, const struct in_addr ina)
391 struct epoch_tracker et;
392 struct in_multi *inm;
394 IN_MULTI_LIST_LOCK_ASSERT();
397 inm = inm_lookup_locked(ifp, ina);
404 * Find an IPv4 multicast group entry for this ip_moptions instance
405 * which matches the specified group, and optionally an interface.
406 * Return its index into the array, or -1 if not found.
408 static struct in_mfilter *
409 imo_match_group(const struct ip_moptions *imo, const struct ifnet *ifp,
410 const struct sockaddr *group)
412 const struct sockaddr_in *gsin;
413 struct in_mfilter *imf;
414 struct in_multi *inm;
416 gsin = (const struct sockaddr_in *)group;
418 IP_MFILTER_FOREACH(imf, &imo->imo_head) {
422 if ((ifp == NULL || (inm->inm_ifp == ifp)) &&
423 in_hosteq(inm->inm_addr, gsin->sin_addr)) {
431 * Find an IPv4 multicast source entry for this imo which matches
432 * the given group index for this socket, and source address.
434 * NOTE: This does not check if the entry is in-mode, merely if
435 * it exists, which may not be the desired behaviour.
437 static struct in_msource *
438 imo_match_source(struct in_mfilter *imf, const struct sockaddr *src)
440 struct ip_msource find;
441 struct ip_msource *ims;
442 const sockunion_t *psa;
444 KASSERT(src->sa_family == AF_INET, ("%s: !AF_INET", __func__));
446 /* Source trees are keyed in host byte order. */
447 psa = (const sockunion_t *)src;
448 find.ims_haddr = ntohl(psa->sin.sin_addr.s_addr);
449 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
451 return ((struct in_msource *)ims);
455 * Perform filtering for multicast datagrams on a socket by group and source.
457 * Returns 0 if a datagram should be allowed through, or various error codes
458 * if the socket was not a member of the group, or the source was muted, etc.
461 imo_multi_filter(const struct ip_moptions *imo, const struct ifnet *ifp,
462 const struct sockaddr *group, const struct sockaddr *src)
464 struct in_mfilter *imf;
465 struct in_msource *ims;
468 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
470 imf = imo_match_group(imo, ifp, group);
472 return (MCAST_NOTGMEMBER);
475 * Check if the source was included in an (S,G) join.
476 * Allow reception on exclusive memberships by default,
477 * reject reception on inclusive memberships by default.
478 * Exclude source only if an in-mode exclude filter exists.
479 * Include source only if an in-mode include filter exists.
480 * NOTE: We are comparing group state here at IGMP t1 (now)
481 * with socket-layer t0 (since last downcall).
483 mode = imf->imf_st[1];
484 ims = imo_match_source(imf, src);
486 if ((ims == NULL && mode == MCAST_INCLUDE) ||
487 (ims != NULL && ims->imsl_st[0] != mode))
488 return (MCAST_NOTSMEMBER);
494 * Find and return a reference to an in_multi record for (ifp, group),
495 * and bump its reference count.
496 * If one does not exist, try to allocate it, and update link-layer multicast
497 * filters on ifp to listen for group.
498 * Assumes the IN_MULTI lock is held across the call.
499 * Return 0 if successful, otherwise return an appropriate error code.
502 in_getmulti(struct ifnet *ifp, const struct in_addr *group,
503 struct in_multi **pinm)
505 struct sockaddr_in gsin;
506 struct ifmultiaddr *ifma;
507 struct in_ifinfo *ii;
508 struct in_multi *inm;
511 IN_MULTI_LOCK_ASSERT();
513 ii = (struct in_ifinfo *)ifp->if_afdata[AF_INET];
514 IN_MULTI_LIST_LOCK();
515 inm = inm_lookup(ifp, *group);
518 * If we already joined this group, just bump the
519 * refcount and return it.
521 KASSERT(inm->inm_refcount >= 1,
522 ("%s: bad refcount %d", __func__, inm->inm_refcount));
523 inm_acquire_locked(inm);
526 IN_MULTI_LIST_UNLOCK();
530 memset(&gsin, 0, sizeof(gsin));
531 gsin.sin_family = AF_INET;
532 gsin.sin_len = sizeof(struct sockaddr_in);
533 gsin.sin_addr = *group;
536 * Check if a link-layer group is already associated
537 * with this network-layer group on the given ifnet.
539 error = if_addmulti(ifp, (struct sockaddr *)&gsin, &ifma);
543 /* XXX ifma_protospec must be covered by IF_ADDR_LOCK */
544 IN_MULTI_LIST_LOCK();
548 * If something other than netinet is occupying the link-layer
549 * group, print a meaningful error message and back out of
551 * Otherwise, bump the refcount on the existing network-layer
552 * group association and return it.
554 if (ifma->ifma_protospec != NULL) {
555 inm = (struct in_multi *)ifma->ifma_protospec;
557 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
559 KASSERT(ifma->ifma_addr->sa_family == AF_INET,
560 ("%s: ifma not AF_INET", __func__));
561 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
562 if (inm->inm_ifma != ifma || inm->inm_ifp != ifp ||
563 !in_hosteq(inm->inm_addr, *group)) {
564 char addrbuf[INET_ADDRSTRLEN];
566 panic("%s: ifma %p is inconsistent with %p (%s)",
567 __func__, ifma, inm, inet_ntoa_r(*group, addrbuf));
570 inm_acquire_locked(inm);
575 IF_ADDR_WLOCK_ASSERT(ifp);
578 * A new in_multi record is needed; allocate and initialize it.
579 * We DO NOT perform an IGMP join as the in_ layer may need to
580 * push an initial source list down to IGMP to support SSM.
582 * The initial source filter state is INCLUDE, {} as per the RFC.
584 inm = malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT | M_ZERO);
586 IF_ADDR_WUNLOCK(ifp);
587 IN_MULTI_LIST_UNLOCK();
588 if_delmulti_ifma(ifma);
591 inm->inm_addr = *group;
593 inm->inm_igi = ii->ii_igmp;
594 inm->inm_ifma = ifma;
595 inm->inm_refcount = 1;
596 inm->inm_state = IGMP_NOT_MEMBER;
597 mbufq_init(&inm->inm_scq, IGMP_MAX_STATE_CHANGES);
598 inm->inm_st[0].iss_fmode = MCAST_UNDEFINED;
599 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
600 RB_INIT(&inm->inm_srcs);
602 ifma->ifma_protospec = inm;
606 IF_ADDR_WUNLOCK(ifp);
607 IN_MULTI_LIST_UNLOCK();
612 * Drop a reference to an in_multi record.
614 * If the refcount drops to 0, free the in_multi record and
615 * delete the underlying link-layer membership.
618 inm_release(struct in_multi *inm)
620 struct ifmultiaddr *ifma;
623 CTR2(KTR_IGMPV3, "%s: refcount is %d", __func__, inm->inm_refcount);
624 MPASS(inm->inm_refcount == 0);
625 CTR2(KTR_IGMPV3, "%s: freeing inm %p", __func__, inm);
627 ifma = inm->inm_ifma;
630 /* XXX this access is not covered by IF_ADDR_LOCK */
631 CTR2(KTR_IGMPV3, "%s: purging ifma %p", __func__, ifma);
633 CURVNET_SET(ifp->if_vnet);
635 free(inm, M_IPMADDR);
636 if_delmulti_ifma_flags(ifma, 1);
641 free(inm, M_IPMADDR);
642 if_delmulti_ifma_flags(ifma, 1);
647 * Clear recorded source entries for a group.
648 * Used by the IGMP code. Caller must hold the IN_MULTI lock.
649 * FIXME: Should reap.
652 inm_clear_recorded(struct in_multi *inm)
654 struct ip_msource *ims;
656 IN_MULTI_LIST_LOCK_ASSERT();
658 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
661 --inm->inm_st[1].iss_rec;
664 KASSERT(inm->inm_st[1].iss_rec == 0,
665 ("%s: iss_rec %d not 0", __func__, inm->inm_st[1].iss_rec));
669 * Record a source as pending for a Source-Group IGMPv3 query.
670 * This lives here as it modifies the shared tree.
672 * inm is the group descriptor.
673 * naddr is the address of the source to record in network-byte order.
675 * If the net.inet.igmp.sgalloc sysctl is non-zero, we will
676 * lazy-allocate a source node in response to an SG query.
677 * Otherwise, no allocation is performed. This saves some memory
678 * with the trade-off that the source will not be reported to the
679 * router if joined in the window between the query response and
680 * the group actually being joined on the local host.
682 * VIMAGE: XXX: Currently the igmp_sgalloc feature has been removed.
683 * This turns off the allocation of a recorded source entry if
684 * the group has not been joined.
686 * Return 0 if the source didn't exist or was already marked as recorded.
687 * Return 1 if the source was marked as recorded by this function.
688 * Return <0 if any error occurred (negated errno code).
691 inm_record_source(struct in_multi *inm, const in_addr_t naddr)
693 struct ip_msource find;
694 struct ip_msource *ims, *nims;
696 IN_MULTI_LIST_LOCK_ASSERT();
698 find.ims_haddr = ntohl(naddr);
699 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
700 if (ims && ims->ims_stp)
703 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
705 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
709 nims->ims_haddr = find.ims_haddr;
710 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
716 * Mark the source as recorded and update the recorded
720 ++inm->inm_st[1].iss_rec;
726 * Return a pointer to an in_msource owned by an in_mfilter,
727 * given its source address.
728 * Lazy-allocate if needed. If this is a new entry its filter state is
731 * imf is the filter set being modified.
732 * haddr is the source address in *host* byte-order.
734 * SMPng: May be called with locks held; malloc must not block.
737 imf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin,
738 struct in_msource **plims)
740 struct ip_msource find;
741 struct ip_msource *ims, *nims;
742 struct in_msource *lims;
749 /* key is host byte order */
750 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
751 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
752 lims = (struct in_msource *)ims;
754 if (imf->imf_nsrc == in_mcast_maxsocksrc)
756 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
760 lims = (struct in_msource *)nims;
761 lims->ims_haddr = find.ims_haddr;
762 lims->imsl_st[0] = MCAST_UNDEFINED;
763 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
773 * Graft a source entry into an existing socket-layer filter set,
774 * maintaining any required invariants and checking allocations.
776 * The source is marked as being in the new filter mode at t1.
778 * Return the pointer to the new node, otherwise return NULL.
780 static struct in_msource *
781 imf_graft(struct in_mfilter *imf, const uint8_t st1,
782 const struct sockaddr_in *psin)
784 struct ip_msource *nims;
785 struct in_msource *lims;
787 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
791 lims = (struct in_msource *)nims;
792 lims->ims_haddr = ntohl(psin->sin_addr.s_addr);
793 lims->imsl_st[0] = MCAST_UNDEFINED;
794 lims->imsl_st[1] = st1;
795 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
802 * Prune a source entry from an existing socket-layer filter set,
803 * maintaining any required invariants and checking allocations.
805 * The source is marked as being left at t1, it is not freed.
807 * Return 0 if no error occurred, otherwise return an errno value.
810 imf_prune(struct in_mfilter *imf, const struct sockaddr_in *psin)
812 struct ip_msource find;
813 struct ip_msource *ims;
814 struct in_msource *lims;
816 /* key is host byte order */
817 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
818 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
821 lims = (struct in_msource *)ims;
822 lims->imsl_st[1] = MCAST_UNDEFINED;
827 * Revert socket-layer filter set deltas at t1 to t0 state.
830 imf_rollback(struct in_mfilter *imf)
832 struct ip_msource *ims, *tims;
833 struct in_msource *lims;
835 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
836 lims = (struct in_msource *)ims;
837 if (lims->imsl_st[0] == lims->imsl_st[1]) {
838 /* no change at t1 */
840 } else if (lims->imsl_st[0] != MCAST_UNDEFINED) {
841 /* revert change to existing source at t1 */
842 lims->imsl_st[1] = lims->imsl_st[0];
844 /* revert source added t1 */
845 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
846 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
847 free(ims, M_INMFILTER);
851 imf->imf_st[1] = imf->imf_st[0];
855 * Mark socket-layer filter set as INCLUDE {} at t1.
858 imf_leave(struct in_mfilter *imf)
860 struct ip_msource *ims;
861 struct in_msource *lims;
863 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
864 lims = (struct in_msource *)ims;
865 lims->imsl_st[1] = MCAST_UNDEFINED;
867 imf->imf_st[1] = MCAST_INCLUDE;
871 * Mark socket-layer filter set deltas as committed.
874 imf_commit(struct in_mfilter *imf)
876 struct ip_msource *ims;
877 struct in_msource *lims;
879 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
880 lims = (struct in_msource *)ims;
881 lims->imsl_st[0] = lims->imsl_st[1];
883 imf->imf_st[0] = imf->imf_st[1];
887 * Reap unreferenced sources from socket-layer filter set.
890 imf_reap(struct in_mfilter *imf)
892 struct ip_msource *ims, *tims;
893 struct in_msource *lims;
895 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
896 lims = (struct in_msource *)ims;
897 if ((lims->imsl_st[0] == MCAST_UNDEFINED) &&
898 (lims->imsl_st[1] == MCAST_UNDEFINED)) {
899 CTR2(KTR_IGMPV3, "%s: free lims %p", __func__, ims);
900 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
901 free(ims, M_INMFILTER);
908 * Purge socket-layer filter set.
911 imf_purge(struct in_mfilter *imf)
913 struct ip_msource *ims, *tims;
915 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
916 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
917 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
918 free(ims, M_INMFILTER);
921 imf->imf_st[0] = imf->imf_st[1] = MCAST_UNDEFINED;
922 KASSERT(RB_EMPTY(&imf->imf_sources),
923 ("%s: imf_sources not empty", __func__));
927 * Look up a source filter entry for a multicast group.
929 * inm is the group descriptor to work with.
930 * haddr is the host-byte-order IPv4 address to look up.
931 * noalloc may be non-zero to suppress allocation of sources.
932 * *pims will be set to the address of the retrieved or allocated source.
934 * SMPng: NOTE: may be called with locks held.
935 * Return 0 if successful, otherwise return a non-zero error code.
938 inm_get_source(struct in_multi *inm, const in_addr_t haddr,
939 const int noalloc, struct ip_msource **pims)
941 struct ip_msource find;
942 struct ip_msource *ims, *nims;
944 find.ims_haddr = haddr;
945 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
946 if (ims == NULL && !noalloc) {
947 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
949 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
953 nims->ims_haddr = haddr;
954 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
958 CTR3(KTR_IGMPV3, "%s: allocated 0x%08x as %p", __func__,
968 * Merge socket-layer source into IGMP-layer source.
969 * If rollback is non-zero, perform the inverse of the merge.
972 ims_merge(struct ip_msource *ims, const struct in_msource *lims,
975 int n = rollback ? -1 : 1;
977 if (lims->imsl_st[0] == MCAST_EXCLUDE) {
978 CTR3(KTR_IGMPV3, "%s: t1 ex -= %d on 0x%08x",
979 __func__, n, ims->ims_haddr);
980 ims->ims_st[1].ex -= n;
981 } else if (lims->imsl_st[0] == MCAST_INCLUDE) {
982 CTR3(KTR_IGMPV3, "%s: t1 in -= %d on 0x%08x",
983 __func__, n, ims->ims_haddr);
984 ims->ims_st[1].in -= n;
987 if (lims->imsl_st[1] == MCAST_EXCLUDE) {
988 CTR3(KTR_IGMPV3, "%s: t1 ex += %d on 0x%08x",
989 __func__, n, ims->ims_haddr);
990 ims->ims_st[1].ex += n;
991 } else if (lims->imsl_st[1] == MCAST_INCLUDE) {
992 CTR3(KTR_IGMPV3, "%s: t1 in += %d on 0x%08x",
993 __func__, n, ims->ims_haddr);
994 ims->ims_st[1].in += n;
999 * Atomically update the global in_multi state, when a membership's
1000 * filter list is being updated in any way.
1002 * imf is the per-inpcb-membership group filter pointer.
1003 * A fake imf may be passed for in-kernel consumers.
1005 * XXX This is a candidate for a set-symmetric-difference style loop
1006 * which would eliminate the repeated lookup from root of ims nodes,
1007 * as they share the same key space.
1009 * If any error occurred this function will back out of refcounts
1010 * and return a non-zero value.
1013 inm_merge(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1015 struct ip_msource *ims, *nims;
1016 struct in_msource *lims;
1017 int schanged, error;
1023 IN_MULTI_LIST_LOCK_ASSERT();
1026 * Update the source filters first, as this may fail.
1027 * Maintain count of in-mode filters at t0, t1. These are
1028 * used to work out if we transition into ASM mode or not.
1029 * Maintain a count of source filters whose state was
1030 * actually modified by this operation.
1032 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
1033 lims = (struct in_msource *)ims;
1034 if (lims->imsl_st[0] == imf->imf_st[0]) nsrc0++;
1035 if (lims->imsl_st[1] == imf->imf_st[1]) nsrc1++;
1036 if (lims->imsl_st[0] == lims->imsl_st[1]) continue;
1037 error = inm_get_source(inm, lims->ims_haddr, 0, &nims);
1041 ims_merge(nims, lims, 0);
1044 struct ip_msource *bims;
1046 RB_FOREACH_REVERSE_FROM(ims, ip_msource_tree, nims) {
1047 lims = (struct in_msource *)ims;
1048 if (lims->imsl_st[0] == lims->imsl_st[1])
1050 (void)inm_get_source(inm, lims->ims_haddr, 1, &bims);
1053 ims_merge(bims, lims, 1);
1058 CTR3(KTR_IGMPV3, "%s: imf filters in-mode: %d at t0, %d at t1",
1059 __func__, nsrc0, nsrc1);
1061 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1062 if (imf->imf_st[0] == imf->imf_st[1] &&
1063 imf->imf_st[1] == MCAST_INCLUDE) {
1065 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
1066 --inm->inm_st[1].iss_in;
1070 /* Handle filter mode transition on socket. */
1071 if (imf->imf_st[0] != imf->imf_st[1]) {
1072 CTR3(KTR_IGMPV3, "%s: imf transition %d to %d",
1073 __func__, imf->imf_st[0], imf->imf_st[1]);
1075 if (imf->imf_st[0] == MCAST_EXCLUDE) {
1076 CTR1(KTR_IGMPV3, "%s: --ex on inm at t1", __func__);
1077 --inm->inm_st[1].iss_ex;
1078 } else if (imf->imf_st[0] == MCAST_INCLUDE) {
1079 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
1080 --inm->inm_st[1].iss_in;
1083 if (imf->imf_st[1] == MCAST_EXCLUDE) {
1084 CTR1(KTR_IGMPV3, "%s: ex++ on inm at t1", __func__);
1085 inm->inm_st[1].iss_ex++;
1086 } else if (imf->imf_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
1087 CTR1(KTR_IGMPV3, "%s: in++ on inm at t1", __func__);
1088 inm->inm_st[1].iss_in++;
1093 * Track inm filter state in terms of listener counts.
1094 * If there are any exclusive listeners, stack-wide
1095 * membership is exclusive.
1096 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1097 * If no listeners remain, state is undefined at t1,
1098 * and the IGMP lifecycle for this group should finish.
1100 if (inm->inm_st[1].iss_ex > 0) {
1101 CTR1(KTR_IGMPV3, "%s: transition to EX", __func__);
1102 inm->inm_st[1].iss_fmode = MCAST_EXCLUDE;
1103 } else if (inm->inm_st[1].iss_in > 0) {
1104 CTR1(KTR_IGMPV3, "%s: transition to IN", __func__);
1105 inm->inm_st[1].iss_fmode = MCAST_INCLUDE;
1107 CTR1(KTR_IGMPV3, "%s: transition to UNDEF", __func__);
1108 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
1111 /* Decrement ASM listener count on transition out of ASM mode. */
1112 if (imf->imf_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1113 if ((imf->imf_st[1] != MCAST_EXCLUDE) ||
1114 (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 > 0)) {
1115 CTR1(KTR_IGMPV3, "%s: --asm on inm at t1", __func__);
1116 --inm->inm_st[1].iss_asm;
1120 /* Increment ASM listener count on transition to ASM mode. */
1121 if (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1122 CTR1(KTR_IGMPV3, "%s: asm++ on inm at t1", __func__);
1123 inm->inm_st[1].iss_asm++;
1126 CTR3(KTR_IGMPV3, "%s: merged imf %p to inm %p", __func__, imf, inm);
1131 CTR1(KTR_IGMPV3, "%s: sources changed; reaping", __func__);
1138 * Mark an in_multi's filter set deltas as committed.
1139 * Called by IGMP after a state change has been enqueued.
1142 inm_commit(struct in_multi *inm)
1144 struct ip_msource *ims;
1146 CTR2(KTR_IGMPV3, "%s: commit inm %p", __func__, inm);
1147 CTR1(KTR_IGMPV3, "%s: pre commit:", __func__);
1150 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
1151 ims->ims_st[0] = ims->ims_st[1];
1153 inm->inm_st[0] = inm->inm_st[1];
1157 * Reap unreferenced nodes from an in_multi's filter set.
1160 inm_reap(struct in_multi *inm)
1162 struct ip_msource *ims, *tims;
1164 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1165 if (ims->ims_st[0].ex > 0 || ims->ims_st[0].in > 0 ||
1166 ims->ims_st[1].ex > 0 || ims->ims_st[1].in > 0 ||
1169 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1170 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1171 free(ims, M_IPMSOURCE);
1177 * Purge all source nodes from an in_multi's filter set.
1180 inm_purge(struct in_multi *inm)
1182 struct ip_msource *ims, *tims;
1184 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1185 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1186 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1187 free(ims, M_IPMSOURCE);
1193 * Join a multicast group; unlocked entry point.
1195 * SMPng: XXX: in_joingroup() is called from in_control() when Giant
1196 * is not held. Fortunately, ifp is unlikely to have been detached
1197 * at this point, so we assume it's OK to recurse.
1200 in_joingroup(struct ifnet *ifp, const struct in_addr *gina,
1201 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1206 error = in_joingroup_locked(ifp, gina, imf, pinm);
1213 * Join a multicast group; real entry point.
1215 * Only preserves atomicity at inm level.
1216 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1218 * If the IGMP downcall fails, the group is not joined, and an error
1222 in_joingroup_locked(struct ifnet *ifp, const struct in_addr *gina,
1223 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1225 struct in_mfilter timf;
1226 struct in_multi *inm;
1229 IN_MULTI_LOCK_ASSERT();
1230 IN_MULTI_LIST_UNLOCK_ASSERT();
1232 CTR4(KTR_IGMPV3, "%s: join 0x%08x on %p(%s))", __func__,
1233 ntohl(gina->s_addr), ifp, ifp->if_xname);
1239 * If no imf was specified (i.e. kernel consumer),
1240 * fake one up and assume it is an ASM join.
1243 imf_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1247 error = in_getmulti(ifp, gina, &inm);
1249 CTR1(KTR_IGMPV3, "%s: in_getmulti() failure", __func__);
1252 IN_MULTI_LIST_LOCK();
1253 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1254 error = inm_merge(inm, imf);
1256 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1257 goto out_inm_release;
1260 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1261 error = igmp_change_state(inm);
1263 CTR1(KTR_IGMPV3, "%s: failed to update source", __func__);
1264 goto out_inm_release;
1270 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1272 inm_release_deferred(inm);
1273 IF_ADDR_WUNLOCK(ifp);
1277 IN_MULTI_LIST_UNLOCK();
1283 * Leave a multicast group; unlocked entry point.
1286 in_leavegroup(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1291 error = in_leavegroup_locked(inm, imf);
1298 * Leave a multicast group; real entry point.
1299 * All source filters will be expunged.
1301 * Only preserves atomicity at inm level.
1303 * Holding the write lock for the INP which contains imf
1304 * is highly advisable. We can't assert for it as imf does not
1305 * contain a back-pointer to the owning inp.
1307 * Note: This is not the same as inm_release(*) as this function also
1308 * makes a state change downcall into IGMP.
1311 in_leavegroup_locked(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1313 struct in_mfilter timf;
1316 IN_MULTI_LOCK_ASSERT();
1317 IN_MULTI_LIST_UNLOCK_ASSERT();
1321 CTR5(KTR_IGMPV3, "%s: leave inm %p, 0x%08x/%s, imf %p", __func__,
1322 inm, ntohl(inm->inm_addr.s_addr),
1323 (inm_is_ifp_detached(inm) ? "null" : inm->inm_ifp->if_xname),
1327 * If no imf was specified (i.e. kernel consumer),
1328 * fake one up and assume it is an ASM join.
1331 imf_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1336 * Begin state merge transaction at IGMP layer.
1338 * As this particular invocation should not cause any memory
1339 * to be allocated, and there is no opportunity to roll back
1340 * the transaction, it MUST NOT fail.
1342 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1343 IN_MULTI_LIST_LOCK();
1344 error = inm_merge(inm, imf);
1345 KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1347 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1348 CURVNET_SET(inm->inm_ifp->if_vnet);
1349 error = igmp_change_state(inm);
1350 IF_ADDR_WLOCK(inm->inm_ifp);
1351 inm_release_deferred(inm);
1352 IF_ADDR_WUNLOCK(inm->inm_ifp);
1353 IN_MULTI_LIST_UNLOCK();
1356 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1358 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1363 /*#ifndef BURN_BRIDGES*/
1365 * Join an IPv4 multicast group in (*,G) exclusive mode.
1366 * The group must be a 224.0.0.0/24 link-scope group.
1367 * This KPI is for legacy kernel consumers only.
1370 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1372 struct in_multi *pinm;
1375 char addrbuf[INET_ADDRSTRLEN];
1378 KASSERT(IN_LOCAL_GROUP(ntohl(ap->s_addr)),
1379 ("%s: %s not in 224.0.0.0/24", __func__,
1380 inet_ntoa_r(*ap, addrbuf)));
1382 error = in_joingroup(ifp, ap, NULL, &pinm);
1390 * Block or unblock an ASM multicast source on an inpcb.
1391 * This implements the delta-based API described in RFC 3678.
1393 * The delta-based API applies only to exclusive-mode memberships.
1394 * An IGMP downcall will be performed.
1396 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1398 * Return 0 if successful, otherwise return an appropriate error code.
1401 inp_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1403 struct group_source_req gsr;
1404 struct rm_priotracker in_ifa_tracker;
1405 sockunion_t *gsa, *ssa;
1407 struct in_mfilter *imf;
1408 struct ip_moptions *imo;
1409 struct in_msource *ims;
1410 struct in_multi *inm;
1418 memset(&gsr, 0, sizeof(struct group_source_req));
1419 gsa = (sockunion_t *)&gsr.gsr_group;
1420 ssa = (sockunion_t *)&gsr.gsr_source;
1422 switch (sopt->sopt_name) {
1423 case IP_BLOCK_SOURCE:
1424 case IP_UNBLOCK_SOURCE: {
1425 struct ip_mreq_source mreqs;
1427 error = sooptcopyin(sopt, &mreqs,
1428 sizeof(struct ip_mreq_source),
1429 sizeof(struct ip_mreq_source));
1433 gsa->sin.sin_family = AF_INET;
1434 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1435 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1437 ssa->sin.sin_family = AF_INET;
1438 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1439 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1441 if (!in_nullhost(mreqs.imr_interface)) {
1442 IN_IFADDR_RLOCK(&in_ifa_tracker);
1443 INADDR_TO_IFP(mreqs.imr_interface, ifp);
1444 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1446 if (sopt->sopt_name == IP_BLOCK_SOURCE)
1449 CTR3(KTR_IGMPV3, "%s: imr_interface = 0x%08x, ifp = %p",
1450 __func__, ntohl(mreqs.imr_interface.s_addr), ifp);
1454 case MCAST_BLOCK_SOURCE:
1455 case MCAST_UNBLOCK_SOURCE:
1456 error = sooptcopyin(sopt, &gsr,
1457 sizeof(struct group_source_req),
1458 sizeof(struct group_source_req));
1462 if (gsa->sin.sin_family != AF_INET ||
1463 gsa->sin.sin_len != sizeof(struct sockaddr_in))
1466 if (ssa->sin.sin_family != AF_INET ||
1467 ssa->sin.sin_len != sizeof(struct sockaddr_in))
1470 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1471 return (EADDRNOTAVAIL);
1473 ifp = ifnet_byindex(gsr.gsr_interface);
1475 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1480 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
1481 __func__, sopt->sopt_name);
1482 return (EOPNOTSUPP);
1486 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1492 * Check if we are actually a member of this group.
1494 imo = inp_findmoptions(inp);
1495 imf = imo_match_group(imo, ifp, &gsa->sa);
1497 error = EADDRNOTAVAIL;
1498 goto out_inp_locked;
1503 * Attempting to use the delta-based API on an
1504 * non exclusive-mode membership is an error.
1506 fmode = imf->imf_st[0];
1507 if (fmode != MCAST_EXCLUDE) {
1509 goto out_inp_locked;
1513 * Deal with error cases up-front:
1514 * Asked to block, but already blocked; or
1515 * Asked to unblock, but nothing to unblock.
1516 * If adding a new block entry, allocate it.
1518 ims = imo_match_source(imf, &ssa->sa);
1519 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1520 CTR3(KTR_IGMPV3, "%s: source 0x%08x %spresent", __func__,
1521 ntohl(ssa->sin.sin_addr.s_addr), doblock ? "" : "not ");
1522 error = EADDRNOTAVAIL;
1523 goto out_inp_locked;
1526 INP_WLOCK_ASSERT(inp);
1529 * Begin state merge transaction at socket layer.
1532 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
1533 ims = imf_graft(imf, fmode, &ssa->sin);
1537 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
1538 error = imf_prune(imf, &ssa->sin);
1542 CTR1(KTR_IGMPV3, "%s: merge imf state failed", __func__);
1543 goto out_imf_rollback;
1547 * Begin state merge transaction at IGMP layer.
1549 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1550 IN_MULTI_LIST_LOCK();
1551 error = inm_merge(inm, imf);
1553 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1554 IN_MULTI_LIST_UNLOCK();
1555 goto out_imf_rollback;
1558 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1559 error = igmp_change_state(inm);
1560 IN_MULTI_LIST_UNLOCK();
1562 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1579 * Given an inpcb, return its multicast options structure pointer. Accepts
1580 * an unlocked inpcb pointer, but will return it locked. May sleep.
1582 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1583 * SMPng: NOTE: Returns with the INP write lock held.
1585 static struct ip_moptions *
1586 inp_findmoptions(struct inpcb *inp)
1588 struct ip_moptions *imo;
1591 if (inp->inp_moptions != NULL)
1592 return (inp->inp_moptions);
1596 imo = malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK);
1598 imo->imo_multicast_ifp = NULL;
1599 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1600 imo->imo_multicast_vif = -1;
1601 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1602 imo->imo_multicast_loop = in_mcast_loop;
1603 STAILQ_INIT(&imo->imo_head);
1606 if (inp->inp_moptions != NULL) {
1607 free(imo, M_IPMOPTS);
1608 return (inp->inp_moptions);
1610 inp->inp_moptions = imo;
1615 inp_gcmoptions(struct ip_moptions *imo)
1617 struct in_mfilter *imf;
1618 struct in_multi *inm;
1621 while ((imf = ip_mfilter_first(&imo->imo_head)) != NULL) {
1622 ip_mfilter_remove(&imo->imo_head, imf);
1625 if ((inm = imf->imf_inm) != NULL) {
1626 if ((ifp = inm->inm_ifp) != NULL) {
1627 CURVNET_SET(ifp->if_vnet);
1628 (void)in_leavegroup(inm, imf);
1631 (void)in_leavegroup(inm, imf);
1634 ip_mfilter_free(imf);
1636 free(imo, M_IPMOPTS);
1640 * Discard the IP multicast options (and source filters). To minimize
1641 * the amount of work done while holding locks such as the INP's
1642 * pcbinfo lock (which is used in the receive path), the free
1643 * operation is deferred to the epoch callback task.
1646 inp_freemoptions(struct ip_moptions *imo)
1650 inp_gcmoptions(imo);
1654 * Atomically get source filters on a socket for an IPv4 multicast group.
1655 * Called with INP lock held; returns with lock released.
1658 inp_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1660 struct __msfilterreq msfr;
1663 struct ip_moptions *imo;
1664 struct in_mfilter *imf;
1665 struct ip_msource *ims;
1666 struct in_msource *lims;
1667 struct sockaddr_in *psin;
1668 struct sockaddr_storage *ptss;
1669 struct sockaddr_storage *tss;
1671 size_t nsrcs, ncsrcs;
1673 INP_WLOCK_ASSERT(inp);
1675 imo = inp->inp_moptions;
1676 KASSERT(imo != NULL, ("%s: null ip_moptions", __func__));
1680 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1681 sizeof(struct __msfilterreq));
1685 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
1688 ifp = ifnet_byindex(msfr.msfr_ifindex);
1695 * Lookup group on the socket.
1697 gsa = (sockunion_t *)&msfr.msfr_group;
1698 imf = imo_match_group(imo, ifp, &gsa->sa);
1701 return (EADDRNOTAVAIL);
1705 * Ignore memberships which are in limbo.
1707 if (imf->imf_st[1] == MCAST_UNDEFINED) {
1711 msfr.msfr_fmode = imf->imf_st[1];
1714 * If the user specified a buffer, copy out the source filter
1715 * entries to userland gracefully.
1716 * We only copy out the number of entries which userland
1717 * has asked for, but we always tell userland how big the
1718 * buffer really needs to be.
1720 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
1721 msfr.msfr_nsrcs = in_mcast_maxsocksrc;
1723 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1724 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1725 M_TEMP, M_NOWAIT | M_ZERO);
1733 * Count number of sources in-mode at t0.
1734 * If buffer space exists and remains, copy out source entries.
1736 nsrcs = msfr.msfr_nsrcs;
1739 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
1740 lims = (struct in_msource *)ims;
1741 if (lims->imsl_st[0] == MCAST_UNDEFINED ||
1742 lims->imsl_st[0] != imf->imf_st[0])
1745 if (tss != NULL && nsrcs > 0) {
1746 psin = (struct sockaddr_in *)ptss;
1747 psin->sin_family = AF_INET;
1748 psin->sin_len = sizeof(struct sockaddr_in);
1749 psin->sin_addr.s_addr = htonl(lims->ims_haddr);
1759 error = copyout(tss, msfr.msfr_srcs,
1760 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1766 msfr.msfr_nsrcs = ncsrcs;
1767 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1773 * Return the IP multicast options in response to user getsockopt().
1776 inp_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1778 struct rm_priotracker in_ifa_tracker;
1779 struct ip_mreqn mreqn;
1780 struct ip_moptions *imo;
1782 struct in_ifaddr *ia;
1787 imo = inp->inp_moptions;
1789 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1790 * or is a divert socket, reject it.
1792 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1793 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1794 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
1796 return (EOPNOTSUPP);
1800 switch (sopt->sopt_name) {
1801 case IP_MULTICAST_VIF:
1803 optval = imo->imo_multicast_vif;
1807 error = sooptcopyout(sopt, &optval, sizeof(int));
1810 case IP_MULTICAST_IF:
1811 memset(&mreqn, 0, sizeof(struct ip_mreqn));
1813 ifp = imo->imo_multicast_ifp;
1814 if (!in_nullhost(imo->imo_multicast_addr)) {
1815 mreqn.imr_address = imo->imo_multicast_addr;
1816 } else if (ifp != NULL) {
1817 struct epoch_tracker et;
1819 mreqn.imr_ifindex = ifp->if_index;
1820 NET_EPOCH_ENTER(et);
1821 IFP_TO_IA(ifp, ia, &in_ifa_tracker);
1824 IA_SIN(ia)->sin_addr;
1829 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
1830 error = sooptcopyout(sopt, &mreqn,
1831 sizeof(struct ip_mreqn));
1833 error = sooptcopyout(sopt, &mreqn.imr_address,
1834 sizeof(struct in_addr));
1838 case IP_MULTICAST_TTL:
1840 optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1842 optval = coptval = imo->imo_multicast_ttl;
1844 if (sopt->sopt_valsize == sizeof(u_char))
1845 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1847 error = sooptcopyout(sopt, &optval, sizeof(int));
1850 case IP_MULTICAST_LOOP:
1852 optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1854 optval = coptval = imo->imo_multicast_loop;
1856 if (sopt->sopt_valsize == sizeof(u_char))
1857 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1859 error = sooptcopyout(sopt, &optval, sizeof(int));
1864 error = EADDRNOTAVAIL;
1867 error = inp_get_source_filters(inp, sopt);
1873 error = ENOPROTOOPT;
1877 INP_UNLOCK_ASSERT(inp);
1883 * Look up the ifnet to use for a multicast group membership,
1884 * given the IPv4 address of an interface, and the IPv4 group address.
1886 * This routine exists to support legacy multicast applications
1887 * which do not understand that multicast memberships are scoped to
1888 * specific physical links in the networking stack, or which need
1889 * to join link-scope groups before IPv4 addresses are configured.
1891 * If inp is non-NULL, use this socket's current FIB number for any
1892 * required FIB lookup.
1893 * If ina is INADDR_ANY, look up the group address in the unicast FIB,
1894 * and use its ifp; usually, this points to the default next-hop.
1896 * If the FIB lookup fails, attempt to use the first non-loopback
1897 * interface with multicast capability in the system as a
1898 * last resort. The legacy IPv4 ASM API requires that we do
1899 * this in order to allow groups to be joined when the routing
1900 * table has not yet been populated during boot.
1902 * Returns NULL if no ifp could be found.
1904 * FUTURE: Implement IPv4 source-address selection.
1906 static struct ifnet *
1907 inp_lookup_mcast_ifp(const struct inpcb *inp,
1908 const struct sockaddr_in *gsin, const struct in_addr ina)
1910 struct rm_priotracker in_ifa_tracker;
1912 struct nhop4_basic nh4;
1915 KASSERT(gsin->sin_family == AF_INET, ("%s: not AF_INET", __func__));
1916 KASSERT(IN_MULTICAST(ntohl(gsin->sin_addr.s_addr)),
1917 ("%s: not multicast", __func__));
1920 if (!in_nullhost(ina)) {
1921 IN_IFADDR_RLOCK(&in_ifa_tracker);
1922 INADDR_TO_IFP(ina, ifp);
1923 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1925 fibnum = inp ? inp->inp_inc.inc_fibnum : 0;
1926 if (fib4_lookup_nh_basic(fibnum, gsin->sin_addr, 0, 0, &nh4)==0)
1929 struct in_ifaddr *ia;
1933 IN_IFADDR_RLOCK(&in_ifa_tracker);
1934 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1936 if (!(mifp->if_flags & IFF_LOOPBACK) &&
1937 (mifp->if_flags & IFF_MULTICAST)) {
1942 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1950 * Join an IPv4 multicast group, possibly with a source.
1953 inp_join_group(struct inpcb *inp, struct sockopt *sopt)
1955 struct group_source_req gsr;
1956 sockunion_t *gsa, *ssa;
1958 struct in_mfilter *imf;
1959 struct ip_moptions *imo;
1960 struct in_multi *inm;
1961 struct in_msource *lims;
1968 memset(&gsr, 0, sizeof(struct group_source_req));
1969 gsa = (sockunion_t *)&gsr.gsr_group;
1970 gsa->ss.ss_family = AF_UNSPEC;
1971 ssa = (sockunion_t *)&gsr.gsr_source;
1972 ssa->ss.ss_family = AF_UNSPEC;
1974 switch (sopt->sopt_name) {
1975 case IP_ADD_MEMBERSHIP: {
1976 struct ip_mreqn mreqn;
1978 if (sopt->sopt_valsize == sizeof(struct ip_mreqn))
1979 error = sooptcopyin(sopt, &mreqn,
1980 sizeof(struct ip_mreqn), sizeof(struct ip_mreqn));
1982 error = sooptcopyin(sopt, &mreqn,
1983 sizeof(struct ip_mreq), sizeof(struct ip_mreq));
1987 gsa->sin.sin_family = AF_INET;
1988 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1989 gsa->sin.sin_addr = mreqn.imr_multiaddr;
1990 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1993 if (sopt->sopt_valsize == sizeof(struct ip_mreqn) &&
1994 mreqn.imr_ifindex != 0)
1995 ifp = ifnet_byindex(mreqn.imr_ifindex);
1997 ifp = inp_lookup_mcast_ifp(inp, &gsa->sin,
2001 case IP_ADD_SOURCE_MEMBERSHIP: {
2002 struct ip_mreq_source mreqs;
2004 error = sooptcopyin(sopt, &mreqs, sizeof(struct ip_mreq_source),
2005 sizeof(struct ip_mreq_source));
2009 gsa->sin.sin_family = ssa->sin.sin_family = AF_INET;
2010 gsa->sin.sin_len = ssa->sin.sin_len =
2011 sizeof(struct sockaddr_in);
2013 gsa->sin.sin_addr = mreqs.imr_multiaddr;
2014 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2017 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
2019 ifp = inp_lookup_mcast_ifp(inp, &gsa->sin,
2020 mreqs.imr_interface);
2021 CTR3(KTR_IGMPV3, "%s: imr_interface = 0x%08x, ifp = %p",
2022 __func__, ntohl(mreqs.imr_interface.s_addr), ifp);
2026 case MCAST_JOIN_GROUP:
2027 case MCAST_JOIN_SOURCE_GROUP:
2028 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
2029 error = sooptcopyin(sopt, &gsr,
2030 sizeof(struct group_req),
2031 sizeof(struct group_req));
2032 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
2033 error = sooptcopyin(sopt, &gsr,
2034 sizeof(struct group_source_req),
2035 sizeof(struct group_source_req));
2040 if (gsa->sin.sin_family != AF_INET ||
2041 gsa->sin.sin_len != sizeof(struct sockaddr_in))
2045 * Overwrite the port field if present, as the sockaddr
2046 * being copied in may be matched with a binary comparison.
2048 gsa->sin.sin_port = 0;
2049 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
2050 if (ssa->sin.sin_family != AF_INET ||
2051 ssa->sin.sin_len != sizeof(struct sockaddr_in))
2053 ssa->sin.sin_port = 0;
2056 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2059 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
2060 return (EADDRNOTAVAIL);
2061 ifp = ifnet_byindex(gsr.gsr_interface);
2065 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2066 __func__, sopt->sopt_name);
2067 return (EOPNOTSUPP);
2071 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
2072 return (EADDRNOTAVAIL);
2077 * Find the membership in the membership list.
2079 imo = inp_findmoptions(inp);
2080 imf = imo_match_group(imo, ifp, &gsa->sa);
2085 if (ip_mfilter_count(&imo->imo_head) >= IP_MAX_MEMBERSHIPS) {
2087 goto out_inp_locked;
2093 if (ssa->ss.ss_family != AF_UNSPEC) {
2095 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2096 * is an error. On an existing inclusive membership,
2097 * it just adds the source to the filter list.
2099 if (imf->imf_st[1] != MCAST_INCLUDE) {
2101 goto out_inp_locked;
2104 * Throw out duplicates.
2106 * XXX FIXME: This makes a naive assumption that
2107 * even if entries exist for *ssa in this imf,
2108 * they will be rejected as dupes, even if they
2109 * are not valid in the current mode (in-mode).
2111 * in_msource is transactioned just as for anything
2112 * else in SSM -- but note naive use of inm_graft()
2113 * below for allocating new filter entries.
2115 * This is only an issue if someone mixes the
2116 * full-state SSM API with the delta-based API,
2117 * which is discouraged in the relevant RFCs.
2119 lims = imo_match_source(imf, &ssa->sa);
2120 if (lims != NULL /*&&
2121 lims->imsl_st[1] == MCAST_INCLUDE*/) {
2122 error = EADDRNOTAVAIL;
2123 goto out_inp_locked;
2127 * MCAST_JOIN_GROUP on an existing exclusive
2128 * membership is an error; return EADDRINUSE
2129 * to preserve 4.4BSD API idempotence, and
2130 * avoid tedious detour to code below.
2131 * NOTE: This is bending RFC 3678 a bit.
2133 * On an existing inclusive membership, this is also
2134 * an error; if you want to change filter mode,
2135 * you must use the userland API setsourcefilter().
2136 * XXX We don't reject this for imf in UNDEFINED
2137 * state at t1, because allocation of a filter
2138 * is atomic with allocation of a membership.
2141 if (imf->imf_st[1] == MCAST_EXCLUDE)
2143 goto out_inp_locked;
2148 * Begin state merge transaction at socket layer.
2150 INP_WLOCK_ASSERT(inp);
2153 * Graft new source into filter list for this inpcb's
2154 * membership of the group. The in_multi may not have
2155 * been allocated yet if this is a new membership, however,
2156 * the in_mfilter slot will be allocated and must be initialized.
2158 * Note: Grafting of exclusive mode filters doesn't happen
2160 * XXX: Should check for non-NULL lims (node exists but may
2161 * not be in-mode) for interop with full-state API.
2163 if (ssa->ss.ss_family != AF_UNSPEC) {
2164 /* Membership starts in IN mode */
2166 CTR1(KTR_IGMPV3, "%s: new join w/source", __func__);
2167 imf = ip_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_INCLUDE);
2170 goto out_inp_locked;
2173 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
2175 lims = imf_graft(imf, MCAST_INCLUDE, &ssa->sin);
2177 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2180 goto out_inp_locked;
2183 /* No address specified; Membership starts in EX mode */
2185 CTR1(KTR_IGMPV3, "%s: new join w/o source", __func__);
2186 imf = ip_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_EXCLUDE);
2189 goto out_inp_locked;
2195 * Begin state merge transaction at IGMP layer.
2201 error = in_joingroup_locked(ifp, &gsa->sin.sin_addr, imf,
2205 if (in_pcbrele_wlocked(inp)) {
2207 goto out_inp_unlocked;
2210 CTR1(KTR_IGMPV3, "%s: in_joingroup_locked failed",
2212 goto out_inp_locked;
2215 * NOTE: Refcount from in_joingroup_locked()
2216 * is protecting membership.
2218 ip_mfilter_insert(&imo->imo_head, imf);
2220 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2221 IN_MULTI_LIST_LOCK();
2222 error = inm_merge(inm, imf);
2224 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2226 IN_MULTI_LIST_UNLOCK();
2229 goto out_inp_locked;
2231 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2232 error = igmp_change_state(inm);
2233 IN_MULTI_LIST_UNLOCK();
2235 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2239 goto out_inp_locked;
2251 if (is_new && imf) {
2252 if (imf->imf_inm != NULL) {
2253 IN_MULTI_LIST_LOCK();
2255 inm_release_deferred(imf->imf_inm);
2256 IF_ADDR_WUNLOCK(ifp);
2257 IN_MULTI_LIST_UNLOCK();
2259 ip_mfilter_free(imf);
2265 * Leave an IPv4 multicast group on an inpcb, possibly with a source.
2268 inp_leave_group(struct inpcb *inp, struct sockopt *sopt)
2270 struct group_source_req gsr;
2271 struct ip_mreq_source mreqs;
2272 struct rm_priotracker in_ifa_tracker;
2273 sockunion_t *gsa, *ssa;
2275 struct in_mfilter *imf;
2276 struct ip_moptions *imo;
2277 struct in_msource *ims;
2278 struct in_multi *inm;
2286 memset(&gsr, 0, sizeof(struct group_source_req));
2287 gsa = (sockunion_t *)&gsr.gsr_group;
2288 gsa->ss.ss_family = AF_UNSPEC;
2289 ssa = (sockunion_t *)&gsr.gsr_source;
2290 ssa->ss.ss_family = AF_UNSPEC;
2292 switch (sopt->sopt_name) {
2293 case IP_DROP_MEMBERSHIP:
2294 case IP_DROP_SOURCE_MEMBERSHIP:
2295 if (sopt->sopt_name == IP_DROP_MEMBERSHIP) {
2296 error = sooptcopyin(sopt, &mreqs,
2297 sizeof(struct ip_mreq),
2298 sizeof(struct ip_mreq));
2300 * Swap interface and sourceaddr arguments,
2301 * as ip_mreq and ip_mreq_source are laid
2304 mreqs.imr_interface = mreqs.imr_sourceaddr;
2305 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
2306 } else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2307 error = sooptcopyin(sopt, &mreqs,
2308 sizeof(struct ip_mreq_source),
2309 sizeof(struct ip_mreq_source));
2314 gsa->sin.sin_family = AF_INET;
2315 gsa->sin.sin_len = sizeof(struct sockaddr_in);
2316 gsa->sin.sin_addr = mreqs.imr_multiaddr;
2318 if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2319 ssa->sin.sin_family = AF_INET;
2320 ssa->sin.sin_len = sizeof(struct sockaddr_in);
2321 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
2325 * Attempt to look up hinted ifp from interface address.
2326 * Fallthrough with null ifp iff lookup fails, to
2327 * preserve 4.4BSD mcast API idempotence.
2328 * XXX NOTE WELL: The RFC 3678 API is preferred because
2329 * using an IPv4 address as a key is racy.
2331 if (!in_nullhost(mreqs.imr_interface)) {
2332 IN_IFADDR_RLOCK(&in_ifa_tracker);
2333 INADDR_TO_IFP(mreqs.imr_interface, ifp);
2334 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
2336 CTR3(KTR_IGMPV3, "%s: imr_interface = 0x%08x, ifp = %p",
2337 __func__, ntohl(mreqs.imr_interface.s_addr), ifp);
2341 case MCAST_LEAVE_GROUP:
2342 case MCAST_LEAVE_SOURCE_GROUP:
2343 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2344 error = sooptcopyin(sopt, &gsr,
2345 sizeof(struct group_req),
2346 sizeof(struct group_req));
2347 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2348 error = sooptcopyin(sopt, &gsr,
2349 sizeof(struct group_source_req),
2350 sizeof(struct group_source_req));
2355 if (gsa->sin.sin_family != AF_INET ||
2356 gsa->sin.sin_len != sizeof(struct sockaddr_in))
2359 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2360 if (ssa->sin.sin_family != AF_INET ||
2361 ssa->sin.sin_len != sizeof(struct sockaddr_in))
2365 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
2366 return (EADDRNOTAVAIL);
2368 ifp = ifnet_byindex(gsr.gsr_interface);
2371 return (EADDRNOTAVAIL);
2375 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2376 __func__, sopt->sopt_name);
2377 return (EOPNOTSUPP);
2381 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2387 * Find the membership in the membership list.
2389 imo = inp_findmoptions(inp);
2390 imf = imo_match_group(imo, ifp, &gsa->sa);
2392 error = EADDRNOTAVAIL;
2393 goto out_inp_locked;
2397 if (ssa->ss.ss_family != AF_UNSPEC)
2401 * Begin state merge transaction at socket layer.
2403 INP_WLOCK_ASSERT(inp);
2406 * If we were instructed only to leave a given source, do so.
2407 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2410 ip_mfilter_remove(&imo->imo_head, imf);
2414 * Give up the multicast address record to which
2415 * the membership points.
2417 (void) in_leavegroup_locked(imf->imf_inm, imf);
2419 if (imf->imf_st[0] == MCAST_EXCLUDE) {
2420 error = EADDRNOTAVAIL;
2421 goto out_inp_locked;
2423 ims = imo_match_source(imf, &ssa->sa);
2425 CTR3(KTR_IGMPV3, "%s: source 0x%08x %spresent",
2426 __func__, ntohl(ssa->sin.sin_addr.s_addr), "not ");
2427 error = EADDRNOTAVAIL;
2428 goto out_inp_locked;
2430 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
2431 error = imf_prune(imf, &ssa->sin);
2433 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2435 goto out_inp_locked;
2440 * Begin state merge transaction at IGMP layer.
2443 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2444 IN_MULTI_LIST_LOCK();
2445 error = inm_merge(inm, imf);
2447 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2449 IN_MULTI_LIST_UNLOCK();
2452 goto out_inp_locked;
2455 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2456 error = igmp_change_state(inm);
2457 IN_MULTI_LIST_UNLOCK();
2459 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2463 goto out_inp_locked;
2472 if (is_final && imf)
2473 ip_mfilter_free(imf);
2480 * Select the interface for transmitting IPv4 multicast datagrams.
2482 * Either an instance of struct in_addr or an instance of struct ip_mreqn
2483 * may be passed to this socket option. An address of INADDR_ANY or an
2484 * interface index of 0 is used to remove a previous selection.
2485 * When no interface is selected, one is chosen for every send.
2488 inp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2490 struct rm_priotracker in_ifa_tracker;
2491 struct in_addr addr;
2492 struct ip_mreqn mreqn;
2494 struct ip_moptions *imo;
2497 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
2499 * An interface index was specified using the
2500 * Linux-derived ip_mreqn structure.
2502 error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn),
2503 sizeof(struct ip_mreqn));
2507 if (mreqn.imr_ifindex < 0 || V_if_index < mreqn.imr_ifindex)
2510 if (mreqn.imr_ifindex == 0) {
2513 ifp = ifnet_byindex(mreqn.imr_ifindex);
2515 return (EADDRNOTAVAIL);
2519 * An interface was specified by IPv4 address.
2520 * This is the traditional BSD usage.
2522 error = sooptcopyin(sopt, &addr, sizeof(struct in_addr),
2523 sizeof(struct in_addr));
2526 if (in_nullhost(addr)) {
2529 IN_IFADDR_RLOCK(&in_ifa_tracker);
2530 INADDR_TO_IFP(addr, ifp);
2531 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
2533 return (EADDRNOTAVAIL);
2535 CTR3(KTR_IGMPV3, "%s: ifp = %p, addr = 0x%08x", __func__, ifp,
2536 ntohl(addr.s_addr));
2539 /* Reject interfaces which do not support multicast. */
2540 if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0)
2541 return (EOPNOTSUPP);
2543 imo = inp_findmoptions(inp);
2544 imo->imo_multicast_ifp = ifp;
2545 imo->imo_multicast_addr.s_addr = INADDR_ANY;
2552 * Atomically set source filters on a socket for an IPv4 multicast group.
2554 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
2557 inp_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2559 struct __msfilterreq msfr;
2562 struct in_mfilter *imf;
2563 struct ip_moptions *imo;
2564 struct in_multi *inm;
2567 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2568 sizeof(struct __msfilterreq));
2572 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
2575 if ((msfr.msfr_fmode != MCAST_EXCLUDE &&
2576 msfr.msfr_fmode != MCAST_INCLUDE))
2579 if (msfr.msfr_group.ss_family != AF_INET ||
2580 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in))
2583 gsa = (sockunion_t *)&msfr.msfr_group;
2584 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2587 gsa->sin.sin_port = 0; /* ignore port */
2589 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
2590 return (EADDRNOTAVAIL);
2592 ifp = ifnet_byindex(msfr.msfr_ifindex);
2594 return (EADDRNOTAVAIL);
2599 * Take the INP write lock.
2600 * Check if this socket is a member of this group.
2602 imo = inp_findmoptions(inp);
2603 imf = imo_match_group(imo, ifp, &gsa->sa);
2605 error = EADDRNOTAVAIL;
2606 goto out_inp_locked;
2611 * Begin state merge transaction at socket layer.
2613 INP_WLOCK_ASSERT(inp);
2615 imf->imf_st[1] = msfr.msfr_fmode;
2618 * Apply any new source filters, if present.
2619 * Make a copy of the user-space source vector so
2620 * that we may copy them with a single copyin. This
2621 * allows us to deal with page faults up-front.
2623 if (msfr.msfr_nsrcs > 0) {
2624 struct in_msource *lims;
2625 struct sockaddr_in *psin;
2626 struct sockaddr_storage *kss, *pkss;
2631 CTR2(KTR_IGMPV3, "%s: loading %lu source list entries",
2632 __func__, (unsigned long)msfr.msfr_nsrcs);
2633 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2635 error = copyin(msfr.msfr_srcs, kss,
2636 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2645 * Mark all source filters as UNDEFINED at t1.
2646 * Restore new group filter mode, as imf_leave()
2647 * will set it to INCLUDE.
2650 imf->imf_st[1] = msfr.msfr_fmode;
2653 * Update socket layer filters at t1, lazy-allocating
2654 * new entries. This saves a bunch of memory at the
2655 * cost of one RB_FIND() per source entry; duplicate
2656 * entries in the msfr_nsrcs vector are ignored.
2657 * If we encounter an error, rollback transaction.
2659 * XXX This too could be replaced with a set-symmetric
2660 * difference like loop to avoid walking from root
2661 * every time, as the key space is common.
2663 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2664 psin = (struct sockaddr_in *)pkss;
2665 if (psin->sin_family != AF_INET) {
2666 error = EAFNOSUPPORT;
2669 if (psin->sin_len != sizeof(struct sockaddr_in)) {
2673 error = imf_get_source(imf, psin, &lims);
2676 lims->imsl_st[1] = imf->imf_st[1];
2682 goto out_imf_rollback;
2684 INP_WLOCK_ASSERT(inp);
2687 * Begin state merge transaction at IGMP layer.
2689 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2690 IN_MULTI_LIST_LOCK();
2691 error = inm_merge(inm, imf);
2693 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
2694 IN_MULTI_LIST_UNLOCK();
2695 goto out_imf_rollback;
2698 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2699 error = igmp_change_state(inm);
2700 IN_MULTI_LIST_UNLOCK();
2702 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
2719 * Set the IP multicast options in response to user setsockopt().
2721 * Many of the socket options handled in this function duplicate the
2722 * functionality of socket options in the regular unicast API. However,
2723 * it is not possible to merge the duplicate code, because the idempotence
2724 * of the IPv4 multicast part of the BSD Sockets API must be preserved;
2725 * the effects of these options must be treated as separate and distinct.
2727 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2728 * FUTURE: The IP_MULTICAST_VIF option may be eliminated if MROUTING
2729 * is refactored to no longer use vifs.
2732 inp_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2734 struct ip_moptions *imo;
2740 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2741 * or is a divert socket, reject it.
2743 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
2744 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2745 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
2746 return (EOPNOTSUPP);
2748 switch (sopt->sopt_name) {
2749 case IP_MULTICAST_VIF: {
2752 * Select a multicast VIF for transmission.
2753 * Only useful if multicast forwarding is active.
2755 if (legal_vif_num == NULL) {
2759 error = sooptcopyin(sopt, &vifi, sizeof(int), sizeof(int));
2762 if (!legal_vif_num(vifi) && (vifi != -1)) {
2766 imo = inp_findmoptions(inp);
2767 imo->imo_multicast_vif = vifi;
2772 case IP_MULTICAST_IF:
2773 error = inp_set_multicast_if(inp, sopt);
2776 case IP_MULTICAST_TTL: {
2780 * Set the IP time-to-live for outgoing multicast packets.
2781 * The original multicast API required a char argument,
2782 * which is inconsistent with the rest of the socket API.
2783 * We allow either a char or an int.
2785 if (sopt->sopt_valsize == sizeof(u_char)) {
2786 error = sooptcopyin(sopt, &ttl, sizeof(u_char),
2793 error = sooptcopyin(sopt, &ittl, sizeof(u_int),
2803 imo = inp_findmoptions(inp);
2804 imo->imo_multicast_ttl = ttl;
2809 case IP_MULTICAST_LOOP: {
2813 * Set the loopback flag for outgoing multicast packets.
2814 * Must be zero or one. The original multicast API required a
2815 * char argument, which is inconsistent with the rest
2816 * of the socket API. We allow either a char or an int.
2818 if (sopt->sopt_valsize == sizeof(u_char)) {
2819 error = sooptcopyin(sopt, &loop, sizeof(u_char),
2826 error = sooptcopyin(sopt, &iloop, sizeof(u_int),
2830 loop = (u_char)iloop;
2832 imo = inp_findmoptions(inp);
2833 imo->imo_multicast_loop = !!loop;
2838 case IP_ADD_MEMBERSHIP:
2839 case IP_ADD_SOURCE_MEMBERSHIP:
2840 case MCAST_JOIN_GROUP:
2841 case MCAST_JOIN_SOURCE_GROUP:
2842 error = inp_join_group(inp, sopt);
2845 case IP_DROP_MEMBERSHIP:
2846 case IP_DROP_SOURCE_MEMBERSHIP:
2847 case MCAST_LEAVE_GROUP:
2848 case MCAST_LEAVE_SOURCE_GROUP:
2849 error = inp_leave_group(inp, sopt);
2852 case IP_BLOCK_SOURCE:
2853 case IP_UNBLOCK_SOURCE:
2854 case MCAST_BLOCK_SOURCE:
2855 case MCAST_UNBLOCK_SOURCE:
2856 error = inp_block_unblock_source(inp, sopt);
2860 error = inp_set_source_filters(inp, sopt);
2868 INP_UNLOCK_ASSERT(inp);
2874 * Expose IGMP's multicast filter mode and source list(s) to userland,
2875 * keyed by (ifindex, group).
2876 * The filter mode is written out as a uint32_t, followed by
2877 * 0..n of struct in_addr.
2878 * For use by ifmcstat(8).
2879 * SMPng: NOTE: unlocked read of ifindex space.
2882 sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS)
2884 struct in_addr src, group;
2885 struct epoch_tracker et;
2887 struct ifmultiaddr *ifma;
2888 struct in_multi *inm;
2889 struct ip_msource *ims;
2893 uint32_t fmode, ifindex;
2898 if (req->newptr != NULL)
2905 if (ifindex <= 0 || ifindex > V_if_index) {
2906 CTR2(KTR_IGMPV3, "%s: ifindex %u out of range",
2911 group.s_addr = name[1];
2912 if (!IN_MULTICAST(ntohl(group.s_addr))) {
2913 CTR2(KTR_IGMPV3, "%s: group 0x%08x is not multicast",
2914 __func__, ntohl(group.s_addr));
2918 NET_EPOCH_ENTER(et);
2919 ifp = ifnet_byindex(ifindex);
2922 CTR2(KTR_IGMPV3, "%s: no ifp for ifindex %u",
2927 retval = sysctl_wire_old_buffer(req,
2928 sizeof(uint32_t) + (in_mcast_maxgrpsrc * sizeof(struct in_addr)));
2934 IN_MULTI_LIST_LOCK();
2936 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2937 if (ifma->ifma_addr->sa_family != AF_INET ||
2938 ifma->ifma_protospec == NULL)
2940 inm = (struct in_multi *)ifma->ifma_protospec;
2941 if (!in_hosteq(inm->inm_addr, group))
2943 fmode = inm->inm_st[1].iss_fmode;
2944 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2947 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
2948 CTR2(KTR_IGMPV3, "%s: visit node 0x%08x", __func__,
2951 * Only copy-out sources which are in-mode.
2953 if (fmode != ims_get_mode(inm, ims, 1)) {
2954 CTR1(KTR_IGMPV3, "%s: skip non-in-mode",
2958 src.s_addr = htonl(ims->ims_haddr);
2959 retval = SYSCTL_OUT(req, &src, sizeof(struct in_addr));
2965 IN_MULTI_LIST_UNLOCK();
2971 #if defined(KTR) && (KTR_COMPILE & KTR_IGMPV3)
2973 static const char *inm_modestrs[] = {
2974 [MCAST_UNDEFINED] = "un",
2975 [MCAST_INCLUDE] = "in",
2976 [MCAST_EXCLUDE] = "ex",
2978 _Static_assert(MCAST_UNDEFINED == 0 &&
2979 MCAST_EXCLUDE + 1 == nitems(inm_modestrs),
2980 "inm_modestrs: no longer matches #defines");
2983 inm_mode_str(const int mode)
2986 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2987 return (inm_modestrs[mode]);
2991 static const char *inm_statestrs[] = {
2992 [IGMP_NOT_MEMBER] = "not-member",
2993 [IGMP_SILENT_MEMBER] = "silent",
2994 [IGMP_REPORTING_MEMBER] = "reporting",
2995 [IGMP_IDLE_MEMBER] = "idle",
2996 [IGMP_LAZY_MEMBER] = "lazy",
2997 [IGMP_SLEEPING_MEMBER] = "sleeping",
2998 [IGMP_AWAKENING_MEMBER] = "awakening",
2999 [IGMP_G_QUERY_PENDING_MEMBER] = "query-pending",
3000 [IGMP_SG_QUERY_PENDING_MEMBER] = "sg-query-pending",
3001 [IGMP_LEAVING_MEMBER] = "leaving",
3003 _Static_assert(IGMP_NOT_MEMBER == 0 &&
3004 IGMP_LEAVING_MEMBER + 1 == nitems(inm_statestrs),
3005 "inm_statetrs: no longer matches #defines");
3008 inm_state_str(const int state)
3011 if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
3012 return (inm_statestrs[state]);
3017 * Dump an in_multi structure to the console.
3020 inm_print(const struct in_multi *inm)
3023 char addrbuf[INET_ADDRSTRLEN];
3025 if ((ktr_mask & KTR_IGMPV3) == 0)
3028 printf("%s: --- begin inm %p ---\n", __func__, inm);
3029 printf("addr %s ifp %p(%s) ifma %p\n",
3030 inet_ntoa_r(inm->inm_addr, addrbuf),
3032 inm->inm_ifp->if_xname,
3034 printf("timer %u state %s refcount %u scq.len %u\n",
3036 inm_state_str(inm->inm_state),
3038 inm->inm_scq.mq_len);
3039 printf("igi %p nsrc %lu sctimer %u scrv %u\n",
3044 for (t = 0; t < 2; t++) {
3045 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
3046 inm_mode_str(inm->inm_st[t].iss_fmode),
3047 inm->inm_st[t].iss_asm,
3048 inm->inm_st[t].iss_ex,
3049 inm->inm_st[t].iss_in,
3050 inm->inm_st[t].iss_rec);
3052 printf("%s: --- end inm %p ---\n", __func__, inm);
3055 #else /* !KTR || !(KTR_COMPILE & KTR_IGMPV3) */
3058 inm_print(const struct in_multi *inm)
3063 #endif /* KTR && (KTR_COMPILE & KTR_IGMPV3) */
3065 RB_GENERATE(ip_msource_tree, ip_msource, ims_link, ip_msource_cmp);