2 * Copyright (c) 2007-2009 Bruce Simpson.
3 * Copyright (c) 2005 Robert N. M. Watson.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * IPv4 multicast socket, group, and socket option processing module.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/protosw.h>
47 #include <sys/sysctl.h>
49 #include <sys/taskqueue.h>
53 #include <net/if_var.h>
54 #include <net/if_dl.h>
55 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in_pcb.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/igmp_var.h>
66 #define KTR_IGMPV3 KTR_INET
69 #ifndef __SOCKUNION_DECLARED
71 struct sockaddr_storage ss;
73 struct sockaddr_dl sdl;
74 struct sockaddr_in sin;
76 typedef union sockunion sockunion_t;
77 #define __SOCKUNION_DECLARED
78 #endif /* __SOCKUNION_DECLARED */
80 static MALLOC_DEFINE(M_INMFILTER, "in_mfilter",
81 "IPv4 multicast PCB-layer source filter");
82 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "IPv4 multicast group");
83 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "IPv4 multicast options");
84 static MALLOC_DEFINE(M_IPMSOURCE, "ip_msource",
85 "IPv4 multicast IGMP-layer source filter");
89 * - Lock order is: Giant, INP_WLOCK, IN_MULTI_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
90 * - The IF_ADDR_LOCK is implicitly taken by inm_lookup() earlier, however
91 * it can be taken by code in net/if.c also.
92 * - ip_moptions and in_mfilter are covered by the INP_WLOCK.
94 * struct in_multi is covered by IN_MULTI_LOCK. There isn't strictly
95 * any need for in_multi itself to be virtualized -- it is bound to an ifp
96 * anyway no matter what happens.
98 struct mtx in_multi_mtx;
99 MTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF);
102 * Functions with non-static linkage defined in this file should be
103 * declared in in_var.h:
108 * in_joingroup_locked()
110 * in_leavegroup_locked()
116 * XXX: Both carp and pf need to use the legacy (*,G) KPIs in_addmulti()
119 static void imf_commit(struct in_mfilter *);
120 static int imf_get_source(struct in_mfilter *imf,
121 const struct sockaddr_in *psin,
122 struct in_msource **);
123 static struct in_msource *
124 imf_graft(struct in_mfilter *, const uint8_t,
125 const struct sockaddr_in *);
126 static void imf_leave(struct in_mfilter *);
127 static int imf_prune(struct in_mfilter *, const struct sockaddr_in *);
128 static void imf_purge(struct in_mfilter *);
129 static void imf_rollback(struct in_mfilter *);
130 static void imf_reap(struct in_mfilter *);
131 static int imo_grow(struct ip_moptions *);
132 static size_t imo_match_group(const struct ip_moptions *,
133 const struct ifnet *, const struct sockaddr *);
134 static struct in_msource *
135 imo_match_source(const struct ip_moptions *, const size_t,
136 const struct sockaddr *);
137 static void ims_merge(struct ip_msource *ims,
138 const struct in_msource *lims, const int rollback);
139 static int in_getmulti(struct ifnet *, const struct in_addr *,
141 static int inm_get_source(struct in_multi *inm, const in_addr_t haddr,
142 const int noalloc, struct ip_msource **pims);
144 static int inm_is_ifp_detached(const struct in_multi *);
146 static int inm_merge(struct in_multi *, /*const*/ struct in_mfilter *);
147 static void inm_purge(struct in_multi *);
148 static void inm_reap(struct in_multi *);
149 static struct ip_moptions *
150 inp_findmoptions(struct inpcb *);
151 static void inp_freemoptions_internal(struct ip_moptions *);
152 static void inp_gcmoptions(void *, int);
153 static int inp_get_source_filters(struct inpcb *, struct sockopt *);
154 static int inp_join_group(struct inpcb *, struct sockopt *);
155 static int inp_leave_group(struct inpcb *, struct sockopt *);
156 static struct ifnet *
157 inp_lookup_mcast_ifp(const struct inpcb *,
158 const struct sockaddr_in *, const struct in_addr);
159 static int inp_block_unblock_source(struct inpcb *, struct sockopt *);
160 static int inp_set_multicast_if(struct inpcb *, struct sockopt *);
161 static int inp_set_source_filters(struct inpcb *, struct sockopt *);
162 static int sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS);
164 static SYSCTL_NODE(_net_inet_ip, OID_AUTO, mcast, CTLFLAG_RW, 0,
167 static u_long in_mcast_maxgrpsrc = IP_MAX_GROUP_SRC_FILTER;
168 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxgrpsrc,
169 CTLFLAG_RWTUN, &in_mcast_maxgrpsrc, 0,
170 "Max source filters per group");
172 static u_long in_mcast_maxsocksrc = IP_MAX_SOCK_SRC_FILTER;
173 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxsocksrc,
174 CTLFLAG_RWTUN, &in_mcast_maxsocksrc, 0,
175 "Max source filters per socket");
177 int in_mcast_loop = IP_DEFAULT_MULTICAST_LOOP;
178 SYSCTL_INT(_net_inet_ip_mcast, OID_AUTO, loop, CTLFLAG_RWTUN,
179 &in_mcast_loop, 0, "Loopback multicast datagrams by default");
181 static SYSCTL_NODE(_net_inet_ip_mcast, OID_AUTO, filters,
182 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip_mcast_filters,
183 "Per-interface stack-wide source filters");
185 static STAILQ_HEAD(, ip_moptions) imo_gc_list =
186 STAILQ_HEAD_INITIALIZER(imo_gc_list);
187 static struct task imo_gc_task = TASK_INITIALIZER(0, inp_gcmoptions, NULL);
191 * Inline function which wraps assertions for a valid ifp.
192 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
196 inm_is_ifp_detached(const struct in_multi *inm)
200 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
201 ifp = inm->inm_ifma->ifma_ifp;
204 * Sanity check that netinet's notion of ifp is the
207 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
210 return (ifp == NULL);
215 * Initialize an in_mfilter structure to a known state at t0, t1
216 * with an empty source filter list.
219 imf_init(struct in_mfilter *imf, const int st0, const int st1)
221 memset(imf, 0, sizeof(struct in_mfilter));
222 RB_INIT(&imf->imf_sources);
223 imf->imf_st[0] = st0;
224 imf->imf_st[1] = st1;
228 * Function for looking up an in_multi record for an IPv4 multicast address
229 * on a given interface. ifp must be valid. If no record found, return NULL.
230 * The IN_MULTI_LOCK and IF_ADDR_LOCK on ifp must be held.
233 inm_lookup_locked(struct ifnet *ifp, const struct in_addr ina)
235 struct ifmultiaddr *ifma;
236 struct in_multi *inm;
238 IN_MULTI_LOCK_ASSERT();
239 IF_ADDR_LOCK_ASSERT(ifp);
242 TAILQ_FOREACH(ifma, &((ifp)->if_multiaddrs), ifma_link) {
243 if (ifma->ifma_addr->sa_family == AF_INET) {
244 inm = (struct in_multi *)ifma->ifma_protospec;
245 if (inm->inm_addr.s_addr == ina.s_addr)
254 * Wrapper for inm_lookup_locked().
255 * The IF_ADDR_LOCK will be taken on ifp and released on return.
258 inm_lookup(struct ifnet *ifp, const struct in_addr ina)
260 struct in_multi *inm;
262 IN_MULTI_LOCK_ASSERT();
264 inm = inm_lookup_locked(ifp, ina);
265 IF_ADDR_RUNLOCK(ifp);
271 * Resize the ip_moptions vector to the next power-of-two minus 1.
272 * May be called with locks held; do not sleep.
275 imo_grow(struct ip_moptions *imo)
277 struct in_multi **nmships;
278 struct in_multi **omships;
279 struct in_mfilter *nmfilters;
280 struct in_mfilter *omfilters;
287 omships = imo->imo_membership;
288 omfilters = imo->imo_mfilters;
289 oldmax = imo->imo_max_memberships;
290 newmax = ((oldmax + 1) * 2) - 1;
292 if (newmax <= IP_MAX_MEMBERSHIPS) {
293 nmships = (struct in_multi **)realloc(omships,
294 sizeof(struct in_multi *) * newmax, M_IPMOPTS, M_NOWAIT);
295 nmfilters = (struct in_mfilter *)realloc(omfilters,
296 sizeof(struct in_mfilter) * newmax, M_INMFILTER, M_NOWAIT);
297 if (nmships != NULL && nmfilters != NULL) {
298 /* Initialize newly allocated source filter heads. */
299 for (idx = oldmax; idx < newmax; idx++) {
300 imf_init(&nmfilters[idx], MCAST_UNDEFINED,
303 imo->imo_max_memberships = newmax;
304 imo->imo_membership = nmships;
305 imo->imo_mfilters = nmfilters;
309 if (nmships == NULL || nmfilters == NULL) {
311 free(nmships, M_IPMOPTS);
312 if (nmfilters != NULL)
313 free(nmfilters, M_INMFILTER);
314 return (ETOOMANYREFS);
321 * Find an IPv4 multicast group entry for this ip_moptions instance
322 * which matches the specified group, and optionally an interface.
323 * Return its index into the array, or -1 if not found.
326 imo_match_group(const struct ip_moptions *imo, const struct ifnet *ifp,
327 const struct sockaddr *group)
329 const struct sockaddr_in *gsin;
330 struct in_multi **pinm;
334 gsin = (const struct sockaddr_in *)group;
336 /* The imo_membership array may be lazy allocated. */
337 if (imo->imo_membership == NULL || imo->imo_num_memberships == 0)
340 nmships = imo->imo_num_memberships;
341 pinm = &imo->imo_membership[0];
342 for (idx = 0; idx < nmships; idx++, pinm++) {
345 if ((ifp == NULL || ((*pinm)->inm_ifp == ifp)) &&
346 in_hosteq((*pinm)->inm_addr, gsin->sin_addr)) {
357 * Find an IPv4 multicast source entry for this imo which matches
358 * the given group index for this socket, and source address.
360 * NOTE: This does not check if the entry is in-mode, merely if
361 * it exists, which may not be the desired behaviour.
363 static struct in_msource *
364 imo_match_source(const struct ip_moptions *imo, const size_t gidx,
365 const struct sockaddr *src)
367 struct ip_msource find;
368 struct in_mfilter *imf;
369 struct ip_msource *ims;
370 const sockunion_t *psa;
372 KASSERT(src->sa_family == AF_INET, ("%s: !AF_INET", __func__));
373 KASSERT(gidx != -1 && gidx < imo->imo_num_memberships,
374 ("%s: invalid index %d\n", __func__, (int)gidx));
376 /* The imo_mfilters array may be lazy allocated. */
377 if (imo->imo_mfilters == NULL)
379 imf = &imo->imo_mfilters[gidx];
381 /* Source trees are keyed in host byte order. */
382 psa = (const sockunion_t *)src;
383 find.ims_haddr = ntohl(psa->sin.sin_addr.s_addr);
384 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
386 return ((struct in_msource *)ims);
390 * Perform filtering for multicast datagrams on a socket by group and source.
392 * Returns 0 if a datagram should be allowed through, or various error codes
393 * if the socket was not a member of the group, or the source was muted, etc.
396 imo_multi_filter(const struct ip_moptions *imo, const struct ifnet *ifp,
397 const struct sockaddr *group, const struct sockaddr *src)
400 struct in_msource *ims;
403 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
405 gidx = imo_match_group(imo, ifp, group);
407 return (MCAST_NOTGMEMBER);
410 * Check if the source was included in an (S,G) join.
411 * Allow reception on exclusive memberships by default,
412 * reject reception on inclusive memberships by default.
413 * Exclude source only if an in-mode exclude filter exists.
414 * Include source only if an in-mode include filter exists.
415 * NOTE: We are comparing group state here at IGMP t1 (now)
416 * with socket-layer t0 (since last downcall).
418 mode = imo->imo_mfilters[gidx].imf_st[1];
419 ims = imo_match_source(imo, gidx, src);
421 if ((ims == NULL && mode == MCAST_INCLUDE) ||
422 (ims != NULL && ims->imsl_st[0] != mode))
423 return (MCAST_NOTSMEMBER);
429 * Find and return a reference to an in_multi record for (ifp, group),
430 * and bump its reference count.
431 * If one does not exist, try to allocate it, and update link-layer multicast
432 * filters on ifp to listen for group.
433 * Assumes the IN_MULTI lock is held across the call.
434 * Return 0 if successful, otherwise return an appropriate error code.
437 in_getmulti(struct ifnet *ifp, const struct in_addr *group,
438 struct in_multi **pinm)
440 struct sockaddr_in gsin;
441 struct ifmultiaddr *ifma;
442 struct in_ifinfo *ii;
443 struct in_multi *inm;
446 IN_MULTI_LOCK_ASSERT();
448 ii = (struct in_ifinfo *)ifp->if_afdata[AF_INET];
450 inm = inm_lookup(ifp, *group);
453 * If we already joined this group, just bump the
454 * refcount and return it.
456 KASSERT(inm->inm_refcount >= 1,
457 ("%s: bad refcount %d", __func__, inm->inm_refcount));
463 memset(&gsin, 0, sizeof(gsin));
464 gsin.sin_family = AF_INET;
465 gsin.sin_len = sizeof(struct sockaddr_in);
466 gsin.sin_addr = *group;
469 * Check if a link-layer group is already associated
470 * with this network-layer group on the given ifnet.
472 error = if_addmulti(ifp, (struct sockaddr *)&gsin, &ifma);
476 /* XXX ifma_protospec must be covered by IF_ADDR_LOCK */
480 * If something other than netinet is occupying the link-layer
481 * group, print a meaningful error message and back out of
483 * Otherwise, bump the refcount on the existing network-layer
484 * group association and return it.
486 if (ifma->ifma_protospec != NULL) {
487 inm = (struct in_multi *)ifma->ifma_protospec;
489 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
491 KASSERT(ifma->ifma_addr->sa_family == AF_INET,
492 ("%s: ifma not AF_INET", __func__));
493 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
494 if (inm->inm_ifma != ifma || inm->inm_ifp != ifp ||
495 !in_hosteq(inm->inm_addr, *group))
496 panic("%s: ifma %p is inconsistent with %p (%s)",
497 __func__, ifma, inm, inet_ntoa(*group));
501 IF_ADDR_WUNLOCK(ifp);
505 IF_ADDR_WLOCK_ASSERT(ifp);
508 * A new in_multi record is needed; allocate and initialize it.
509 * We DO NOT perform an IGMP join as the in_ layer may need to
510 * push an initial source list down to IGMP to support SSM.
512 * The initial source filter state is INCLUDE, {} as per the RFC.
514 inm = malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT | M_ZERO);
516 if_delmulti_ifma(ifma);
517 IF_ADDR_WUNLOCK(ifp);
520 inm->inm_addr = *group;
522 inm->inm_igi = ii->ii_igmp;
523 inm->inm_ifma = ifma;
524 inm->inm_refcount = 1;
525 inm->inm_state = IGMP_NOT_MEMBER;
528 * Pending state-changes per group are subject to a bounds check.
530 IFQ_SET_MAXLEN(&inm->inm_scq, IGMP_MAX_STATE_CHANGES);
532 inm->inm_st[0].iss_fmode = MCAST_UNDEFINED;
533 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
534 RB_INIT(&inm->inm_srcs);
536 ifma->ifma_protospec = inm;
540 IF_ADDR_WUNLOCK(ifp);
545 * Drop a reference to an in_multi record.
547 * If the refcount drops to 0, free the in_multi record and
548 * delete the underlying link-layer membership.
551 inm_release_locked(struct in_multi *inm)
553 struct ifmultiaddr *ifma;
555 IN_MULTI_LOCK_ASSERT();
557 CTR2(KTR_IGMPV3, "%s: refcount is %d", __func__, inm->inm_refcount);
559 if (--inm->inm_refcount > 0) {
560 CTR2(KTR_IGMPV3, "%s: refcount is now %d", __func__,
565 CTR2(KTR_IGMPV3, "%s: freeing inm %p", __func__, inm);
567 ifma = inm->inm_ifma;
569 /* XXX this access is not covered by IF_ADDR_LOCK */
570 CTR2(KTR_IGMPV3, "%s: purging ifma %p", __func__, ifma);
571 KASSERT(ifma->ifma_protospec == inm,
572 ("%s: ifma_protospec != inm", __func__));
573 ifma->ifma_protospec = NULL;
577 free(inm, M_IPMADDR);
579 if_delmulti_ifma(ifma);
583 * Clear recorded source entries for a group.
584 * Used by the IGMP code. Caller must hold the IN_MULTI lock.
585 * FIXME: Should reap.
588 inm_clear_recorded(struct in_multi *inm)
590 struct ip_msource *ims;
592 IN_MULTI_LOCK_ASSERT();
594 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
597 --inm->inm_st[1].iss_rec;
600 KASSERT(inm->inm_st[1].iss_rec == 0,
601 ("%s: iss_rec %d not 0", __func__, inm->inm_st[1].iss_rec));
605 * Record a source as pending for a Source-Group IGMPv3 query.
606 * This lives here as it modifies the shared tree.
608 * inm is the group descriptor.
609 * naddr is the address of the source to record in network-byte order.
611 * If the net.inet.igmp.sgalloc sysctl is non-zero, we will
612 * lazy-allocate a source node in response to an SG query.
613 * Otherwise, no allocation is performed. This saves some memory
614 * with the trade-off that the source will not be reported to the
615 * router if joined in the window between the query response and
616 * the group actually being joined on the local host.
618 * VIMAGE: XXX: Currently the igmp_sgalloc feature has been removed.
619 * This turns off the allocation of a recorded source entry if
620 * the group has not been joined.
622 * Return 0 if the source didn't exist or was already marked as recorded.
623 * Return 1 if the source was marked as recorded by this function.
624 * Return <0 if any error occured (negated errno code).
627 inm_record_source(struct in_multi *inm, const in_addr_t naddr)
629 struct ip_msource find;
630 struct ip_msource *ims, *nims;
632 IN_MULTI_LOCK_ASSERT();
634 find.ims_haddr = ntohl(naddr);
635 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
636 if (ims && ims->ims_stp)
639 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
641 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
645 nims->ims_haddr = find.ims_haddr;
646 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
652 * Mark the source as recorded and update the recorded
656 ++inm->inm_st[1].iss_rec;
662 * Return a pointer to an in_msource owned by an in_mfilter,
663 * given its source address.
664 * Lazy-allocate if needed. If this is a new entry its filter state is
667 * imf is the filter set being modified.
668 * haddr is the source address in *host* byte-order.
670 * SMPng: May be called with locks held; malloc must not block.
673 imf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin,
674 struct in_msource **plims)
676 struct ip_msource find;
677 struct ip_msource *ims, *nims;
678 struct in_msource *lims;
685 /* key is host byte order */
686 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
687 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
688 lims = (struct in_msource *)ims;
690 if (imf->imf_nsrc == in_mcast_maxsocksrc)
692 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
696 lims = (struct in_msource *)nims;
697 lims->ims_haddr = find.ims_haddr;
698 lims->imsl_st[0] = MCAST_UNDEFINED;
699 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
709 * Graft a source entry into an existing socket-layer filter set,
710 * maintaining any required invariants and checking allocations.
712 * The source is marked as being in the new filter mode at t1.
714 * Return the pointer to the new node, otherwise return NULL.
716 static struct in_msource *
717 imf_graft(struct in_mfilter *imf, const uint8_t st1,
718 const struct sockaddr_in *psin)
720 struct ip_msource *nims;
721 struct in_msource *lims;
723 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
727 lims = (struct in_msource *)nims;
728 lims->ims_haddr = ntohl(psin->sin_addr.s_addr);
729 lims->imsl_st[0] = MCAST_UNDEFINED;
730 lims->imsl_st[1] = st1;
731 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
738 * Prune a source entry from an existing socket-layer filter set,
739 * maintaining any required invariants and checking allocations.
741 * The source is marked as being left at t1, it is not freed.
743 * Return 0 if no error occurred, otherwise return an errno value.
746 imf_prune(struct in_mfilter *imf, const struct sockaddr_in *psin)
748 struct ip_msource find;
749 struct ip_msource *ims;
750 struct in_msource *lims;
752 /* key is host byte order */
753 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
754 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
757 lims = (struct in_msource *)ims;
758 lims->imsl_st[1] = MCAST_UNDEFINED;
763 * Revert socket-layer filter set deltas at t1 to t0 state.
766 imf_rollback(struct in_mfilter *imf)
768 struct ip_msource *ims, *tims;
769 struct in_msource *lims;
771 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
772 lims = (struct in_msource *)ims;
773 if (lims->imsl_st[0] == lims->imsl_st[1]) {
774 /* no change at t1 */
776 } else if (lims->imsl_st[0] != MCAST_UNDEFINED) {
777 /* revert change to existing source at t1 */
778 lims->imsl_st[1] = lims->imsl_st[0];
780 /* revert source added t1 */
781 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
782 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
783 free(ims, M_INMFILTER);
787 imf->imf_st[1] = imf->imf_st[0];
791 * Mark socket-layer filter set as INCLUDE {} at t1.
794 imf_leave(struct in_mfilter *imf)
796 struct ip_msource *ims;
797 struct in_msource *lims;
799 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
800 lims = (struct in_msource *)ims;
801 lims->imsl_st[1] = MCAST_UNDEFINED;
803 imf->imf_st[1] = MCAST_INCLUDE;
807 * Mark socket-layer filter set deltas as committed.
810 imf_commit(struct in_mfilter *imf)
812 struct ip_msource *ims;
813 struct in_msource *lims;
815 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
816 lims = (struct in_msource *)ims;
817 lims->imsl_st[0] = lims->imsl_st[1];
819 imf->imf_st[0] = imf->imf_st[1];
823 * Reap unreferenced sources from socket-layer filter set.
826 imf_reap(struct in_mfilter *imf)
828 struct ip_msource *ims, *tims;
829 struct in_msource *lims;
831 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
832 lims = (struct in_msource *)ims;
833 if ((lims->imsl_st[0] == MCAST_UNDEFINED) &&
834 (lims->imsl_st[1] == MCAST_UNDEFINED)) {
835 CTR2(KTR_IGMPV3, "%s: free lims %p", __func__, ims);
836 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
837 free(ims, M_INMFILTER);
844 * Purge socket-layer filter set.
847 imf_purge(struct in_mfilter *imf)
849 struct ip_msource *ims, *tims;
851 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
852 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
853 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
854 free(ims, M_INMFILTER);
857 imf->imf_st[0] = imf->imf_st[1] = MCAST_UNDEFINED;
858 KASSERT(RB_EMPTY(&imf->imf_sources),
859 ("%s: imf_sources not empty", __func__));
863 * Look up a source filter entry for a multicast group.
865 * inm is the group descriptor to work with.
866 * haddr is the host-byte-order IPv4 address to look up.
867 * noalloc may be non-zero to suppress allocation of sources.
868 * *pims will be set to the address of the retrieved or allocated source.
870 * SMPng: NOTE: may be called with locks held.
871 * Return 0 if successful, otherwise return a non-zero error code.
874 inm_get_source(struct in_multi *inm, const in_addr_t haddr,
875 const int noalloc, struct ip_msource **pims)
877 struct ip_msource find;
878 struct ip_msource *ims, *nims;
883 find.ims_haddr = haddr;
884 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
885 if (ims == NULL && !noalloc) {
886 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
888 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
892 nims->ims_haddr = haddr;
893 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
897 ia.s_addr = htonl(haddr);
898 CTR3(KTR_IGMPV3, "%s: allocated %s as %p", __func__,
908 * Merge socket-layer source into IGMP-layer source.
909 * If rollback is non-zero, perform the inverse of the merge.
912 ims_merge(struct ip_msource *ims, const struct in_msource *lims,
915 int n = rollback ? -1 : 1;
919 ia.s_addr = htonl(ims->ims_haddr);
922 if (lims->imsl_st[0] == MCAST_EXCLUDE) {
923 CTR3(KTR_IGMPV3, "%s: t1 ex -= %d on %s",
924 __func__, n, inet_ntoa(ia));
925 ims->ims_st[1].ex -= n;
926 } else if (lims->imsl_st[0] == MCAST_INCLUDE) {
927 CTR3(KTR_IGMPV3, "%s: t1 in -= %d on %s",
928 __func__, n, inet_ntoa(ia));
929 ims->ims_st[1].in -= n;
932 if (lims->imsl_st[1] == MCAST_EXCLUDE) {
933 CTR3(KTR_IGMPV3, "%s: t1 ex += %d on %s",
934 __func__, n, inet_ntoa(ia));
935 ims->ims_st[1].ex += n;
936 } else if (lims->imsl_st[1] == MCAST_INCLUDE) {
937 CTR3(KTR_IGMPV3, "%s: t1 in += %d on %s",
938 __func__, n, inet_ntoa(ia));
939 ims->ims_st[1].in += n;
944 * Atomically update the global in_multi state, when a membership's
945 * filter list is being updated in any way.
947 * imf is the per-inpcb-membership group filter pointer.
948 * A fake imf may be passed for in-kernel consumers.
950 * XXX This is a candidate for a set-symmetric-difference style loop
951 * which would eliminate the repeated lookup from root of ims nodes,
952 * as they share the same key space.
954 * If any error occurred this function will back out of refcounts
955 * and return a non-zero value.
958 inm_merge(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
960 struct ip_msource *ims, *nims;
961 struct in_msource *lims;
970 * Update the source filters first, as this may fail.
971 * Maintain count of in-mode filters at t0, t1. These are
972 * used to work out if we transition into ASM mode or not.
973 * Maintain a count of source filters whose state was
974 * actually modified by this operation.
976 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
977 lims = (struct in_msource *)ims;
978 if (lims->imsl_st[0] == imf->imf_st[0]) nsrc0++;
979 if (lims->imsl_st[1] == imf->imf_st[1]) nsrc1++;
980 if (lims->imsl_st[0] == lims->imsl_st[1]) continue;
981 error = inm_get_source(inm, lims->ims_haddr, 0, &nims);
985 ims_merge(nims, lims, 0);
988 struct ip_msource *bims;
990 RB_FOREACH_REVERSE_FROM(ims, ip_msource_tree, nims) {
991 lims = (struct in_msource *)ims;
992 if (lims->imsl_st[0] == lims->imsl_st[1])
994 (void)inm_get_source(inm, lims->ims_haddr, 1, &bims);
997 ims_merge(bims, lims, 1);
1002 CTR3(KTR_IGMPV3, "%s: imf filters in-mode: %d at t0, %d at t1",
1003 __func__, nsrc0, nsrc1);
1005 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1006 if (imf->imf_st[0] == imf->imf_st[1] &&
1007 imf->imf_st[1] == MCAST_INCLUDE) {
1009 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
1010 --inm->inm_st[1].iss_in;
1014 /* Handle filter mode transition on socket. */
1015 if (imf->imf_st[0] != imf->imf_st[1]) {
1016 CTR3(KTR_IGMPV3, "%s: imf transition %d to %d",
1017 __func__, imf->imf_st[0], imf->imf_st[1]);
1019 if (imf->imf_st[0] == MCAST_EXCLUDE) {
1020 CTR1(KTR_IGMPV3, "%s: --ex on inm at t1", __func__);
1021 --inm->inm_st[1].iss_ex;
1022 } else if (imf->imf_st[0] == MCAST_INCLUDE) {
1023 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
1024 --inm->inm_st[1].iss_in;
1027 if (imf->imf_st[1] == MCAST_EXCLUDE) {
1028 CTR1(KTR_IGMPV3, "%s: ex++ on inm at t1", __func__);
1029 inm->inm_st[1].iss_ex++;
1030 } else if (imf->imf_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
1031 CTR1(KTR_IGMPV3, "%s: in++ on inm at t1", __func__);
1032 inm->inm_st[1].iss_in++;
1037 * Track inm filter state in terms of listener counts.
1038 * If there are any exclusive listeners, stack-wide
1039 * membership is exclusive.
1040 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1041 * If no listeners remain, state is undefined at t1,
1042 * and the IGMP lifecycle for this group should finish.
1044 if (inm->inm_st[1].iss_ex > 0) {
1045 CTR1(KTR_IGMPV3, "%s: transition to EX", __func__);
1046 inm->inm_st[1].iss_fmode = MCAST_EXCLUDE;
1047 } else if (inm->inm_st[1].iss_in > 0) {
1048 CTR1(KTR_IGMPV3, "%s: transition to IN", __func__);
1049 inm->inm_st[1].iss_fmode = MCAST_INCLUDE;
1051 CTR1(KTR_IGMPV3, "%s: transition to UNDEF", __func__);
1052 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
1055 /* Decrement ASM listener count on transition out of ASM mode. */
1056 if (imf->imf_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1057 if ((imf->imf_st[1] != MCAST_EXCLUDE) ||
1058 (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 > 0))
1059 CTR1(KTR_IGMPV3, "%s: --asm on inm at t1", __func__);
1060 --inm->inm_st[1].iss_asm;
1063 /* Increment ASM listener count on transition to ASM mode. */
1064 if (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1065 CTR1(KTR_IGMPV3, "%s: asm++ on inm at t1", __func__);
1066 inm->inm_st[1].iss_asm++;
1069 CTR3(KTR_IGMPV3, "%s: merged imf %p to inm %p", __func__, imf, inm);
1074 CTR1(KTR_IGMPV3, "%s: sources changed; reaping", __func__);
1081 * Mark an in_multi's filter set deltas as committed.
1082 * Called by IGMP after a state change has been enqueued.
1085 inm_commit(struct in_multi *inm)
1087 struct ip_msource *ims;
1089 CTR2(KTR_IGMPV3, "%s: commit inm %p", __func__, inm);
1090 CTR1(KTR_IGMPV3, "%s: pre commit:", __func__);
1093 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
1094 ims->ims_st[0] = ims->ims_st[1];
1096 inm->inm_st[0] = inm->inm_st[1];
1100 * Reap unreferenced nodes from an in_multi's filter set.
1103 inm_reap(struct in_multi *inm)
1105 struct ip_msource *ims, *tims;
1107 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1108 if (ims->ims_st[0].ex > 0 || ims->ims_st[0].in > 0 ||
1109 ims->ims_st[1].ex > 0 || ims->ims_st[1].in > 0 ||
1112 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1113 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1114 free(ims, M_IPMSOURCE);
1120 * Purge all source nodes from an in_multi's filter set.
1123 inm_purge(struct in_multi *inm)
1125 struct ip_msource *ims, *tims;
1127 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1128 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1129 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1130 free(ims, M_IPMSOURCE);
1136 * Join a multicast group; unlocked entry point.
1138 * SMPng: XXX: in_joingroup() is called from in_control() when Giant
1139 * is not held. Fortunately, ifp is unlikely to have been detached
1140 * at this point, so we assume it's OK to recurse.
1143 in_joingroup(struct ifnet *ifp, const struct in_addr *gina,
1144 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1149 error = in_joingroup_locked(ifp, gina, imf, pinm);
1156 * Join a multicast group; real entry point.
1158 * Only preserves atomicity at inm level.
1159 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1161 * If the IGMP downcall fails, the group is not joined, and an error
1165 in_joingroup_locked(struct ifnet *ifp, const struct in_addr *gina,
1166 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1168 struct in_mfilter timf;
1169 struct in_multi *inm;
1172 IN_MULTI_LOCK_ASSERT();
1174 CTR4(KTR_IGMPV3, "%s: join %s on %p(%s))", __func__,
1175 inet_ntoa(*gina), ifp, ifp->if_xname);
1181 * If no imf was specified (i.e. kernel consumer),
1182 * fake one up and assume it is an ASM join.
1185 imf_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1189 error = in_getmulti(ifp, gina, &inm);
1191 CTR1(KTR_IGMPV3, "%s: in_getmulti() failure", __func__);
1195 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1196 error = inm_merge(inm, imf);
1198 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1199 goto out_inm_release;
1202 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1203 error = igmp_change_state(inm);
1205 CTR1(KTR_IGMPV3, "%s: failed to update source", __func__);
1206 goto out_inm_release;
1211 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1212 inm_release_locked(inm);
1221 * Leave a multicast group; unlocked entry point.
1224 in_leavegroup(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1229 error = in_leavegroup_locked(inm, imf);
1236 * Leave a multicast group; real entry point.
1237 * All source filters will be expunged.
1239 * Only preserves atomicity at inm level.
1241 * Holding the write lock for the INP which contains imf
1242 * is highly advisable. We can't assert for it as imf does not
1243 * contain a back-pointer to the owning inp.
1245 * Note: This is not the same as inm_release(*) as this function also
1246 * makes a state change downcall into IGMP.
1249 in_leavegroup_locked(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1251 struct in_mfilter timf;
1256 IN_MULTI_LOCK_ASSERT();
1258 CTR5(KTR_IGMPV3, "%s: leave inm %p, %s/%s, imf %p", __func__,
1259 inm, inet_ntoa(inm->inm_addr),
1260 (inm_is_ifp_detached(inm) ? "null" : inm->inm_ifp->if_xname),
1264 * If no imf was specified (i.e. kernel consumer),
1265 * fake one up and assume it is an ASM join.
1268 imf_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1273 * Begin state merge transaction at IGMP layer.
1275 * As this particular invocation should not cause any memory
1276 * to be allocated, and there is no opportunity to roll back
1277 * the transaction, it MUST NOT fail.
1279 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1280 error = inm_merge(inm, imf);
1281 KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1283 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1284 CURVNET_SET(inm->inm_ifp->if_vnet);
1285 error = igmp_change_state(inm);
1288 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1290 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1291 inm_release_locked(inm);
1296 /*#ifndef BURN_BRIDGES*/
1298 * Join an IPv4 multicast group in (*,G) exclusive mode.
1299 * The group must be a 224.0.0.0/24 link-scope group.
1300 * This KPI is for legacy kernel consumers only.
1303 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1305 struct in_multi *pinm;
1308 KASSERT(IN_LOCAL_GROUP(ntohl(ap->s_addr)),
1309 ("%s: %s not in 224.0.0.0/24", __func__, inet_ntoa(*ap)));
1311 error = in_joingroup(ifp, ap, NULL, &pinm);
1319 * Leave an IPv4 multicast group, assumed to be in exclusive (*,G) mode.
1320 * This KPI is for legacy kernel consumers only.
1323 in_delmulti(struct in_multi *inm)
1326 (void)in_leavegroup(inm, NULL);
1331 * Block or unblock an ASM multicast source on an inpcb.
1332 * This implements the delta-based API described in RFC 3678.
1334 * The delta-based API applies only to exclusive-mode memberships.
1335 * An IGMP downcall will be performed.
1337 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1339 * Return 0 if successful, otherwise return an appropriate error code.
1342 inp_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1344 struct group_source_req gsr;
1345 sockunion_t *gsa, *ssa;
1347 struct in_mfilter *imf;
1348 struct ip_moptions *imo;
1349 struct in_msource *ims;
1350 struct in_multi *inm;
1359 memset(&gsr, 0, sizeof(struct group_source_req));
1360 gsa = (sockunion_t *)&gsr.gsr_group;
1361 ssa = (sockunion_t *)&gsr.gsr_source;
1363 switch (sopt->sopt_name) {
1364 case IP_BLOCK_SOURCE:
1365 case IP_UNBLOCK_SOURCE: {
1366 struct ip_mreq_source mreqs;
1368 error = sooptcopyin(sopt, &mreqs,
1369 sizeof(struct ip_mreq_source),
1370 sizeof(struct ip_mreq_source));
1374 gsa->sin.sin_family = AF_INET;
1375 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1376 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1378 ssa->sin.sin_family = AF_INET;
1379 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1380 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1382 if (!in_nullhost(mreqs.imr_interface))
1383 INADDR_TO_IFP(mreqs.imr_interface, ifp);
1385 if (sopt->sopt_name == IP_BLOCK_SOURCE)
1388 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
1389 __func__, inet_ntoa(mreqs.imr_interface), ifp);
1393 case MCAST_BLOCK_SOURCE:
1394 case MCAST_UNBLOCK_SOURCE:
1395 error = sooptcopyin(sopt, &gsr,
1396 sizeof(struct group_source_req),
1397 sizeof(struct group_source_req));
1401 if (gsa->sin.sin_family != AF_INET ||
1402 gsa->sin.sin_len != sizeof(struct sockaddr_in))
1405 if (ssa->sin.sin_family != AF_INET ||
1406 ssa->sin.sin_len != sizeof(struct sockaddr_in))
1409 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1410 return (EADDRNOTAVAIL);
1412 ifp = ifnet_byindex(gsr.gsr_interface);
1414 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1419 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
1420 __func__, sopt->sopt_name);
1421 return (EOPNOTSUPP);
1425 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1429 * Check if we are actually a member of this group.
1431 imo = inp_findmoptions(inp);
1432 idx = imo_match_group(imo, ifp, &gsa->sa);
1433 if (idx == -1 || imo->imo_mfilters == NULL) {
1434 error = EADDRNOTAVAIL;
1435 goto out_inp_locked;
1438 KASSERT(imo->imo_mfilters != NULL,
1439 ("%s: imo_mfilters not allocated", __func__));
1440 imf = &imo->imo_mfilters[idx];
1441 inm = imo->imo_membership[idx];
1444 * Attempting to use the delta-based API on an
1445 * non exclusive-mode membership is an error.
1447 fmode = imf->imf_st[0];
1448 if (fmode != MCAST_EXCLUDE) {
1450 goto out_inp_locked;
1454 * Deal with error cases up-front:
1455 * Asked to block, but already blocked; or
1456 * Asked to unblock, but nothing to unblock.
1457 * If adding a new block entry, allocate it.
1459 ims = imo_match_source(imo, idx, &ssa->sa);
1460 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1461 CTR3(KTR_IGMPV3, "%s: source %s %spresent", __func__,
1462 inet_ntoa(ssa->sin.sin_addr), doblock ? "" : "not ");
1463 error = EADDRNOTAVAIL;
1464 goto out_inp_locked;
1467 INP_WLOCK_ASSERT(inp);
1470 * Begin state merge transaction at socket layer.
1473 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
1474 ims = imf_graft(imf, fmode, &ssa->sin);
1478 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
1479 error = imf_prune(imf, &ssa->sin);
1483 CTR1(KTR_IGMPV3, "%s: merge imf state failed", __func__);
1484 goto out_imf_rollback;
1488 * Begin state merge transaction at IGMP layer.
1492 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1493 error = inm_merge(inm, imf);
1495 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1496 goto out_in_multi_locked;
1499 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1500 error = igmp_change_state(inm);
1502 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1504 out_in_multi_locked:
1522 * Given an inpcb, return its multicast options structure pointer. Accepts
1523 * an unlocked inpcb pointer, but will return it locked. May sleep.
1525 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1526 * SMPng: NOTE: Returns with the INP write lock held.
1528 static struct ip_moptions *
1529 inp_findmoptions(struct inpcb *inp)
1531 struct ip_moptions *imo;
1532 struct in_multi **immp;
1533 struct in_mfilter *imfp;
1537 if (inp->inp_moptions != NULL)
1538 return (inp->inp_moptions);
1542 imo = malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK);
1543 immp = malloc(sizeof(*immp) * IP_MIN_MEMBERSHIPS, M_IPMOPTS,
1545 imfp = malloc(sizeof(struct in_mfilter) * IP_MIN_MEMBERSHIPS,
1546 M_INMFILTER, M_WAITOK);
1548 imo->imo_multicast_ifp = NULL;
1549 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1550 imo->imo_multicast_vif = -1;
1551 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1552 imo->imo_multicast_loop = in_mcast_loop;
1553 imo->imo_num_memberships = 0;
1554 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1555 imo->imo_membership = immp;
1557 /* Initialize per-group source filters. */
1558 for (idx = 0; idx < IP_MIN_MEMBERSHIPS; idx++)
1559 imf_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE);
1560 imo->imo_mfilters = imfp;
1563 if (inp->inp_moptions != NULL) {
1564 free(imfp, M_INMFILTER);
1565 free(immp, M_IPMOPTS);
1566 free(imo, M_IPMOPTS);
1567 return (inp->inp_moptions);
1569 inp->inp_moptions = imo;
1574 * Discard the IP multicast options (and source filters). To minimize
1575 * the amount of work done while holding locks such as the INP's
1576 * pcbinfo lock (which is used in the receive path), the free
1577 * operation is performed asynchronously in a separate task.
1579 * SMPng: NOTE: assumes INP write lock is held.
1582 inp_freemoptions(struct ip_moptions *imo)
1585 KASSERT(imo != NULL, ("%s: ip_moptions is NULL", __func__));
1587 STAILQ_INSERT_TAIL(&imo_gc_list, imo, imo_link);
1589 taskqueue_enqueue(taskqueue_thread, &imo_gc_task);
1593 inp_freemoptions_internal(struct ip_moptions *imo)
1595 struct in_mfilter *imf;
1596 size_t idx, nmships;
1598 nmships = imo->imo_num_memberships;
1599 for (idx = 0; idx < nmships; ++idx) {
1600 imf = imo->imo_mfilters ? &imo->imo_mfilters[idx] : NULL;
1603 (void)in_leavegroup(imo->imo_membership[idx], imf);
1608 if (imo->imo_mfilters)
1609 free(imo->imo_mfilters, M_INMFILTER);
1610 free(imo->imo_membership, M_IPMOPTS);
1611 free(imo, M_IPMOPTS);
1615 inp_gcmoptions(void *context, int pending)
1617 struct ip_moptions *imo;
1620 while (!STAILQ_EMPTY(&imo_gc_list)) {
1621 imo = STAILQ_FIRST(&imo_gc_list);
1622 STAILQ_REMOVE_HEAD(&imo_gc_list, imo_link);
1624 inp_freemoptions_internal(imo);
1631 * Atomically get source filters on a socket for an IPv4 multicast group.
1632 * Called with INP lock held; returns with lock released.
1635 inp_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1637 struct __msfilterreq msfr;
1640 struct ip_moptions *imo;
1641 struct in_mfilter *imf;
1642 struct ip_msource *ims;
1643 struct in_msource *lims;
1644 struct sockaddr_in *psin;
1645 struct sockaddr_storage *ptss;
1646 struct sockaddr_storage *tss;
1648 size_t idx, nsrcs, ncsrcs;
1650 INP_WLOCK_ASSERT(inp);
1652 imo = inp->inp_moptions;
1653 KASSERT(imo != NULL, ("%s: null ip_moptions", __func__));
1657 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1658 sizeof(struct __msfilterreq));
1662 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
1665 ifp = ifnet_byindex(msfr.msfr_ifindex);
1672 * Lookup group on the socket.
1674 gsa = (sockunion_t *)&msfr.msfr_group;
1675 idx = imo_match_group(imo, ifp, &gsa->sa);
1676 if (idx == -1 || imo->imo_mfilters == NULL) {
1678 return (EADDRNOTAVAIL);
1680 imf = &imo->imo_mfilters[idx];
1683 * Ignore memberships which are in limbo.
1685 if (imf->imf_st[1] == MCAST_UNDEFINED) {
1689 msfr.msfr_fmode = imf->imf_st[1];
1692 * If the user specified a buffer, copy out the source filter
1693 * entries to userland gracefully.
1694 * We only copy out the number of entries which userland
1695 * has asked for, but we always tell userland how big the
1696 * buffer really needs to be.
1698 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
1699 msfr.msfr_nsrcs = in_mcast_maxsocksrc;
1701 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1702 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1703 M_TEMP, M_NOWAIT | M_ZERO);
1711 * Count number of sources in-mode at t0.
1712 * If buffer space exists and remains, copy out source entries.
1714 nsrcs = msfr.msfr_nsrcs;
1717 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
1718 lims = (struct in_msource *)ims;
1719 if (lims->imsl_st[0] == MCAST_UNDEFINED ||
1720 lims->imsl_st[0] != imf->imf_st[0])
1723 if (tss != NULL && nsrcs > 0) {
1724 psin = (struct sockaddr_in *)ptss;
1725 psin->sin_family = AF_INET;
1726 psin->sin_len = sizeof(struct sockaddr_in);
1727 psin->sin_addr.s_addr = htonl(lims->ims_haddr);
1737 error = copyout(tss, msfr.msfr_srcs,
1738 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1744 msfr.msfr_nsrcs = ncsrcs;
1745 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1751 * Return the IP multicast options in response to user getsockopt().
1754 inp_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1756 struct ip_mreqn mreqn;
1757 struct ip_moptions *imo;
1759 struct in_ifaddr *ia;
1764 imo = inp->inp_moptions;
1766 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1767 * or is a divert socket, reject it.
1769 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1770 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1771 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
1773 return (EOPNOTSUPP);
1777 switch (sopt->sopt_name) {
1778 case IP_MULTICAST_VIF:
1780 optval = imo->imo_multicast_vif;
1784 error = sooptcopyout(sopt, &optval, sizeof(int));
1787 case IP_MULTICAST_IF:
1788 memset(&mreqn, 0, sizeof(struct ip_mreqn));
1790 ifp = imo->imo_multicast_ifp;
1791 if (!in_nullhost(imo->imo_multicast_addr)) {
1792 mreqn.imr_address = imo->imo_multicast_addr;
1793 } else if (ifp != NULL) {
1794 mreqn.imr_ifindex = ifp->if_index;
1798 IA_SIN(ia)->sin_addr;
1799 ifa_free(&ia->ia_ifa);
1804 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
1805 error = sooptcopyout(sopt, &mreqn,
1806 sizeof(struct ip_mreqn));
1808 error = sooptcopyout(sopt, &mreqn.imr_address,
1809 sizeof(struct in_addr));
1813 case IP_MULTICAST_TTL:
1815 optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1817 optval = coptval = imo->imo_multicast_ttl;
1819 if (sopt->sopt_valsize == sizeof(u_char))
1820 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1822 error = sooptcopyout(sopt, &optval, sizeof(int));
1825 case IP_MULTICAST_LOOP:
1827 optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1829 optval = coptval = imo->imo_multicast_loop;
1831 if (sopt->sopt_valsize == sizeof(u_char))
1832 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1834 error = sooptcopyout(sopt, &optval, sizeof(int));
1839 error = EADDRNOTAVAIL;
1842 error = inp_get_source_filters(inp, sopt);
1848 error = ENOPROTOOPT;
1852 INP_UNLOCK_ASSERT(inp);
1858 * Look up the ifnet to use for a multicast group membership,
1859 * given the IPv4 address of an interface, and the IPv4 group address.
1861 * This routine exists to support legacy multicast applications
1862 * which do not understand that multicast memberships are scoped to
1863 * specific physical links in the networking stack, or which need
1864 * to join link-scope groups before IPv4 addresses are configured.
1866 * If inp is non-NULL, use this socket's current FIB number for any
1867 * required FIB lookup.
1868 * If ina is INADDR_ANY, look up the group address in the unicast FIB,
1869 * and use its ifp; usually, this points to the default next-hop.
1871 * If the FIB lookup fails, attempt to use the first non-loopback
1872 * interface with multicast capability in the system as a
1873 * last resort. The legacy IPv4 ASM API requires that we do
1874 * this in order to allow groups to be joined when the routing
1875 * table has not yet been populated during boot.
1877 * Returns NULL if no ifp could be found.
1879 * SMPng: TODO: Acquire the appropriate locks for INADDR_TO_IFP.
1880 * FUTURE: Implement IPv4 source-address selection.
1882 static struct ifnet *
1883 inp_lookup_mcast_ifp(const struct inpcb *inp,
1884 const struct sockaddr_in *gsin, const struct in_addr ina)
1888 KASSERT(gsin->sin_family == AF_INET, ("%s: not AF_INET", __func__));
1889 KASSERT(IN_MULTICAST(ntohl(gsin->sin_addr.s_addr)),
1890 ("%s: not multicast", __func__));
1893 if (!in_nullhost(ina)) {
1894 INADDR_TO_IFP(ina, ifp);
1899 memcpy(&ro.ro_dst, gsin, sizeof(struct sockaddr_in));
1900 in_rtalloc_ign(&ro, 0, inp ? inp->inp_inc.inc_fibnum : 0);
1901 if (ro.ro_rt != NULL) {
1902 ifp = ro.ro_rt->rt_ifp;
1903 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
1906 struct in_ifaddr *ia;
1911 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1913 if (!(mifp->if_flags & IFF_LOOPBACK) &&
1914 (mifp->if_flags & IFF_MULTICAST)) {
1919 IN_IFADDR_RUNLOCK();
1927 * Join an IPv4 multicast group, possibly with a source.
1930 inp_join_group(struct inpcb *inp, struct sockopt *sopt)
1932 struct group_source_req gsr;
1933 sockunion_t *gsa, *ssa;
1935 struct in_mfilter *imf;
1936 struct ip_moptions *imo;
1937 struct in_multi *inm;
1938 struct in_msource *lims;
1948 memset(&gsr, 0, sizeof(struct group_source_req));
1949 gsa = (sockunion_t *)&gsr.gsr_group;
1950 gsa->ss.ss_family = AF_UNSPEC;
1951 ssa = (sockunion_t *)&gsr.gsr_source;
1952 ssa->ss.ss_family = AF_UNSPEC;
1954 switch (sopt->sopt_name) {
1955 case IP_ADD_MEMBERSHIP:
1956 case IP_ADD_SOURCE_MEMBERSHIP: {
1957 struct ip_mreq_source mreqs;
1959 if (sopt->sopt_name == IP_ADD_MEMBERSHIP) {
1960 error = sooptcopyin(sopt, &mreqs,
1961 sizeof(struct ip_mreq),
1962 sizeof(struct ip_mreq));
1964 * Do argument switcharoo from ip_mreq into
1965 * ip_mreq_source to avoid using two instances.
1967 mreqs.imr_interface = mreqs.imr_sourceaddr;
1968 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
1969 } else if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
1970 error = sooptcopyin(sopt, &mreqs,
1971 sizeof(struct ip_mreq_source),
1972 sizeof(struct ip_mreq_source));
1977 gsa->sin.sin_family = AF_INET;
1978 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1979 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1981 if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
1982 ssa->sin.sin_family = AF_INET;
1983 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1984 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1987 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1990 ifp = inp_lookup_mcast_ifp(inp, &gsa->sin,
1991 mreqs.imr_interface);
1992 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
1993 __func__, inet_ntoa(mreqs.imr_interface), ifp);
1997 case MCAST_JOIN_GROUP:
1998 case MCAST_JOIN_SOURCE_GROUP:
1999 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
2000 error = sooptcopyin(sopt, &gsr,
2001 sizeof(struct group_req),
2002 sizeof(struct group_req));
2003 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
2004 error = sooptcopyin(sopt, &gsr,
2005 sizeof(struct group_source_req),
2006 sizeof(struct group_source_req));
2011 if (gsa->sin.sin_family != AF_INET ||
2012 gsa->sin.sin_len != sizeof(struct sockaddr_in))
2016 * Overwrite the port field if present, as the sockaddr
2017 * being copied in may be matched with a binary comparison.
2019 gsa->sin.sin_port = 0;
2020 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
2021 if (ssa->sin.sin_family != AF_INET ||
2022 ssa->sin.sin_len != sizeof(struct sockaddr_in))
2024 ssa->sin.sin_port = 0;
2027 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2030 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
2031 return (EADDRNOTAVAIL);
2032 ifp = ifnet_byindex(gsr.gsr_interface);
2036 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2037 __func__, sopt->sopt_name);
2038 return (EOPNOTSUPP);
2042 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
2043 return (EADDRNOTAVAIL);
2045 imo = inp_findmoptions(inp);
2046 idx = imo_match_group(imo, ifp, &gsa->sa);
2050 inm = imo->imo_membership[idx];
2051 imf = &imo->imo_mfilters[idx];
2052 if (ssa->ss.ss_family != AF_UNSPEC) {
2054 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2055 * is an error. On an existing inclusive membership,
2056 * it just adds the source to the filter list.
2058 if (imf->imf_st[1] != MCAST_INCLUDE) {
2060 goto out_inp_locked;
2063 * Throw out duplicates.
2065 * XXX FIXME: This makes a naive assumption that
2066 * even if entries exist for *ssa in this imf,
2067 * they will be rejected as dupes, even if they
2068 * are not valid in the current mode (in-mode).
2070 * in_msource is transactioned just as for anything
2071 * else in SSM -- but note naive use of inm_graft()
2072 * below for allocating new filter entries.
2074 * This is only an issue if someone mixes the
2075 * full-state SSM API with the delta-based API,
2076 * which is discouraged in the relevant RFCs.
2078 lims = imo_match_source(imo, idx, &ssa->sa);
2079 if (lims != NULL /*&&
2080 lims->imsl_st[1] == MCAST_INCLUDE*/) {
2081 error = EADDRNOTAVAIL;
2082 goto out_inp_locked;
2086 * MCAST_JOIN_GROUP on an existing exclusive
2087 * membership is an error; return EADDRINUSE
2088 * to preserve 4.4BSD API idempotence, and
2089 * avoid tedious detour to code below.
2090 * NOTE: This is bending RFC 3678 a bit.
2092 * On an existing inclusive membership, this is also
2093 * an error; if you want to change filter mode,
2094 * you must use the userland API setsourcefilter().
2095 * XXX We don't reject this for imf in UNDEFINED
2096 * state at t1, because allocation of a filter
2097 * is atomic with allocation of a membership.
2100 if (imf->imf_st[1] == MCAST_EXCLUDE)
2102 goto out_inp_locked;
2107 * Begin state merge transaction at socket layer.
2109 INP_WLOCK_ASSERT(inp);
2112 if (imo->imo_num_memberships == imo->imo_max_memberships) {
2113 error = imo_grow(imo);
2115 goto out_inp_locked;
2118 * Allocate the new slot upfront so we can deal with
2119 * grafting the new source filter in same code path
2120 * as for join-source on existing membership.
2122 idx = imo->imo_num_memberships;
2123 imo->imo_membership[idx] = NULL;
2124 imo->imo_num_memberships++;
2125 KASSERT(imo->imo_mfilters != NULL,
2126 ("%s: imf_mfilters vector was not allocated", __func__));
2127 imf = &imo->imo_mfilters[idx];
2128 KASSERT(RB_EMPTY(&imf->imf_sources),
2129 ("%s: imf_sources not empty", __func__));
2133 * Graft new source into filter list for this inpcb's
2134 * membership of the group. The in_multi may not have
2135 * been allocated yet if this is a new membership, however,
2136 * the in_mfilter slot will be allocated and must be initialized.
2138 * Note: Grafting of exclusive mode filters doesn't happen
2140 * XXX: Should check for non-NULL lims (node exists but may
2141 * not be in-mode) for interop with full-state API.
2143 if (ssa->ss.ss_family != AF_UNSPEC) {
2144 /* Membership starts in IN mode */
2146 CTR1(KTR_IGMPV3, "%s: new join w/source", __func__);
2147 imf_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE);
2149 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
2151 lims = imf_graft(imf, MCAST_INCLUDE, &ssa->sin);
2153 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2159 /* No address specified; Membership starts in EX mode */
2161 CTR1(KTR_IGMPV3, "%s: new join w/o source", __func__);
2162 imf_init(imf, MCAST_UNDEFINED, MCAST_EXCLUDE);
2167 * Begin state merge transaction at IGMP layer.
2172 error = in_joingroup_locked(ifp, &gsa->sin.sin_addr, imf,
2175 CTR1(KTR_IGMPV3, "%s: in_joingroup_locked failed",
2180 imo->imo_membership[idx] = inm;
2182 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2183 error = inm_merge(inm, imf);
2185 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2187 goto out_in_multi_locked;
2189 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2190 error = igmp_change_state(inm);
2192 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2194 goto out_in_multi_locked;
2198 out_in_multi_locked:
2202 INP_WLOCK_ASSERT(inp);
2214 if (error && is_new) {
2215 imo->imo_membership[idx] = NULL;
2216 --imo->imo_num_memberships;
2225 * Leave an IPv4 multicast group on an inpcb, possibly with a source.
2228 inp_leave_group(struct inpcb *inp, struct sockopt *sopt)
2230 struct group_source_req gsr;
2231 struct ip_mreq_source mreqs;
2232 sockunion_t *gsa, *ssa;
2234 struct in_mfilter *imf;
2235 struct ip_moptions *imo;
2236 struct in_msource *ims;
2237 struct in_multi *inm;
2239 int error, is_final;
2245 memset(&gsr, 0, sizeof(struct group_source_req));
2246 gsa = (sockunion_t *)&gsr.gsr_group;
2247 gsa->ss.ss_family = AF_UNSPEC;
2248 ssa = (sockunion_t *)&gsr.gsr_source;
2249 ssa->ss.ss_family = AF_UNSPEC;
2251 switch (sopt->sopt_name) {
2252 case IP_DROP_MEMBERSHIP:
2253 case IP_DROP_SOURCE_MEMBERSHIP:
2254 if (sopt->sopt_name == IP_DROP_MEMBERSHIP) {
2255 error = sooptcopyin(sopt, &mreqs,
2256 sizeof(struct ip_mreq),
2257 sizeof(struct ip_mreq));
2259 * Swap interface and sourceaddr arguments,
2260 * as ip_mreq and ip_mreq_source are laid
2263 mreqs.imr_interface = mreqs.imr_sourceaddr;
2264 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
2265 } else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2266 error = sooptcopyin(sopt, &mreqs,
2267 sizeof(struct ip_mreq_source),
2268 sizeof(struct ip_mreq_source));
2273 gsa->sin.sin_family = AF_INET;
2274 gsa->sin.sin_len = sizeof(struct sockaddr_in);
2275 gsa->sin.sin_addr = mreqs.imr_multiaddr;
2277 if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2278 ssa->sin.sin_family = AF_INET;
2279 ssa->sin.sin_len = sizeof(struct sockaddr_in);
2280 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
2284 * Attempt to look up hinted ifp from interface address.
2285 * Fallthrough with null ifp iff lookup fails, to
2286 * preserve 4.4BSD mcast API idempotence.
2287 * XXX NOTE WELL: The RFC 3678 API is preferred because
2288 * using an IPv4 address as a key is racy.
2290 if (!in_nullhost(mreqs.imr_interface))
2291 INADDR_TO_IFP(mreqs.imr_interface, ifp);
2293 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
2294 __func__, inet_ntoa(mreqs.imr_interface), ifp);
2298 case MCAST_LEAVE_GROUP:
2299 case MCAST_LEAVE_SOURCE_GROUP:
2300 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2301 error = sooptcopyin(sopt, &gsr,
2302 sizeof(struct group_req),
2303 sizeof(struct group_req));
2304 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2305 error = sooptcopyin(sopt, &gsr,
2306 sizeof(struct group_source_req),
2307 sizeof(struct group_source_req));
2312 if (gsa->sin.sin_family != AF_INET ||
2313 gsa->sin.sin_len != sizeof(struct sockaddr_in))
2316 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2317 if (ssa->sin.sin_family != AF_INET ||
2318 ssa->sin.sin_len != sizeof(struct sockaddr_in))
2322 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
2323 return (EADDRNOTAVAIL);
2325 ifp = ifnet_byindex(gsr.gsr_interface);
2328 return (EADDRNOTAVAIL);
2332 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2333 __func__, sopt->sopt_name);
2334 return (EOPNOTSUPP);
2338 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2342 * Find the membership in the membership array.
2344 imo = inp_findmoptions(inp);
2345 idx = imo_match_group(imo, ifp, &gsa->sa);
2347 error = EADDRNOTAVAIL;
2348 goto out_inp_locked;
2350 inm = imo->imo_membership[idx];
2351 imf = &imo->imo_mfilters[idx];
2353 if (ssa->ss.ss_family != AF_UNSPEC)
2357 * Begin state merge transaction at socket layer.
2359 INP_WLOCK_ASSERT(inp);
2362 * If we were instructed only to leave a given source, do so.
2363 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2368 if (imf->imf_st[0] == MCAST_EXCLUDE) {
2369 error = EADDRNOTAVAIL;
2370 goto out_inp_locked;
2372 ims = imo_match_source(imo, idx, &ssa->sa);
2374 CTR3(KTR_IGMPV3, "%s: source %s %spresent", __func__,
2375 inet_ntoa(ssa->sin.sin_addr), "not ");
2376 error = EADDRNOTAVAIL;
2377 goto out_inp_locked;
2379 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
2380 error = imf_prune(imf, &ssa->sin);
2382 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2384 goto out_inp_locked;
2389 * Begin state merge transaction at IGMP layer.
2395 * Give up the multicast address record to which
2396 * the membership points.
2398 (void)in_leavegroup_locked(inm, imf);
2400 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2401 error = inm_merge(inm, imf);
2403 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2405 goto out_in_multi_locked;
2408 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2409 error = igmp_change_state(inm);
2411 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2416 out_in_multi_locked:
2428 /* Remove the gap in the membership and filter array. */
2429 for (++idx; idx < imo->imo_num_memberships; ++idx) {
2430 imo->imo_membership[idx-1] = imo->imo_membership[idx];
2431 imo->imo_mfilters[idx-1] = imo->imo_mfilters[idx];
2433 imo->imo_num_memberships--;
2442 * Select the interface for transmitting IPv4 multicast datagrams.
2444 * Either an instance of struct in_addr or an instance of struct ip_mreqn
2445 * may be passed to this socket option. An address of INADDR_ANY or an
2446 * interface index of 0 is used to remove a previous selection.
2447 * When no interface is selected, one is chosen for every send.
2450 inp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2452 struct in_addr addr;
2453 struct ip_mreqn mreqn;
2455 struct ip_moptions *imo;
2458 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
2460 * An interface index was specified using the
2461 * Linux-derived ip_mreqn structure.
2463 error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn),
2464 sizeof(struct ip_mreqn));
2468 if (mreqn.imr_ifindex < 0 || V_if_index < mreqn.imr_ifindex)
2471 if (mreqn.imr_ifindex == 0) {
2474 ifp = ifnet_byindex(mreqn.imr_ifindex);
2476 return (EADDRNOTAVAIL);
2480 * An interface was specified by IPv4 address.
2481 * This is the traditional BSD usage.
2483 error = sooptcopyin(sopt, &addr, sizeof(struct in_addr),
2484 sizeof(struct in_addr));
2487 if (in_nullhost(addr)) {
2490 INADDR_TO_IFP(addr, ifp);
2492 return (EADDRNOTAVAIL);
2494 CTR3(KTR_IGMPV3, "%s: ifp = %p, addr = %s", __func__, ifp,
2498 /* Reject interfaces which do not support multicast. */
2499 if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0)
2500 return (EOPNOTSUPP);
2502 imo = inp_findmoptions(inp);
2503 imo->imo_multicast_ifp = ifp;
2504 imo->imo_multicast_addr.s_addr = INADDR_ANY;
2511 * Atomically set source filters on a socket for an IPv4 multicast group.
2513 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
2516 inp_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2518 struct __msfilterreq msfr;
2521 struct in_mfilter *imf;
2522 struct ip_moptions *imo;
2523 struct in_multi *inm;
2527 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2528 sizeof(struct __msfilterreq));
2532 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
2535 if ((msfr.msfr_fmode != MCAST_EXCLUDE &&
2536 msfr.msfr_fmode != MCAST_INCLUDE))
2539 if (msfr.msfr_group.ss_family != AF_INET ||
2540 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in))
2543 gsa = (sockunion_t *)&msfr.msfr_group;
2544 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2547 gsa->sin.sin_port = 0; /* ignore port */
2549 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
2550 return (EADDRNOTAVAIL);
2552 ifp = ifnet_byindex(msfr.msfr_ifindex);
2554 return (EADDRNOTAVAIL);
2557 * Take the INP write lock.
2558 * Check if this socket is a member of this group.
2560 imo = inp_findmoptions(inp);
2561 idx = imo_match_group(imo, ifp, &gsa->sa);
2562 if (idx == -1 || imo->imo_mfilters == NULL) {
2563 error = EADDRNOTAVAIL;
2564 goto out_inp_locked;
2566 inm = imo->imo_membership[idx];
2567 imf = &imo->imo_mfilters[idx];
2570 * Begin state merge transaction at socket layer.
2572 INP_WLOCK_ASSERT(inp);
2574 imf->imf_st[1] = msfr.msfr_fmode;
2577 * Apply any new source filters, if present.
2578 * Make a copy of the user-space source vector so
2579 * that we may copy them with a single copyin. This
2580 * allows us to deal with page faults up-front.
2582 if (msfr.msfr_nsrcs > 0) {
2583 struct in_msource *lims;
2584 struct sockaddr_in *psin;
2585 struct sockaddr_storage *kss, *pkss;
2590 CTR2(KTR_IGMPV3, "%s: loading %lu source list entries",
2591 __func__, (unsigned long)msfr.msfr_nsrcs);
2592 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2594 error = copyin(msfr.msfr_srcs, kss,
2595 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2604 * Mark all source filters as UNDEFINED at t1.
2605 * Restore new group filter mode, as imf_leave()
2606 * will set it to INCLUDE.
2609 imf->imf_st[1] = msfr.msfr_fmode;
2612 * Update socket layer filters at t1, lazy-allocating
2613 * new entries. This saves a bunch of memory at the
2614 * cost of one RB_FIND() per source entry; duplicate
2615 * entries in the msfr_nsrcs vector are ignored.
2616 * If we encounter an error, rollback transaction.
2618 * XXX This too could be replaced with a set-symmetric
2619 * difference like loop to avoid walking from root
2620 * every time, as the key space is common.
2622 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2623 psin = (struct sockaddr_in *)pkss;
2624 if (psin->sin_family != AF_INET) {
2625 error = EAFNOSUPPORT;
2628 if (psin->sin_len != sizeof(struct sockaddr_in)) {
2632 error = imf_get_source(imf, psin, &lims);
2635 lims->imsl_st[1] = imf->imf_st[1];
2641 goto out_imf_rollback;
2643 INP_WLOCK_ASSERT(inp);
2647 * Begin state merge transaction at IGMP layer.
2649 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2650 error = inm_merge(inm, imf);
2652 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
2653 goto out_in_multi_locked;
2656 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2657 error = igmp_change_state(inm);
2659 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
2661 out_in_multi_locked:
2679 * Set the IP multicast options in response to user setsockopt().
2681 * Many of the socket options handled in this function duplicate the
2682 * functionality of socket options in the regular unicast API. However,
2683 * it is not possible to merge the duplicate code, because the idempotence
2684 * of the IPv4 multicast part of the BSD Sockets API must be preserved;
2685 * the effects of these options must be treated as separate and distinct.
2687 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2688 * FUTURE: The IP_MULTICAST_VIF option may be eliminated if MROUTING
2689 * is refactored to no longer use vifs.
2692 inp_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2694 struct ip_moptions *imo;
2700 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2701 * or is a divert socket, reject it.
2703 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
2704 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2705 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
2706 return (EOPNOTSUPP);
2708 switch (sopt->sopt_name) {
2709 case IP_MULTICAST_VIF: {
2712 * Select a multicast VIF for transmission.
2713 * Only useful if multicast forwarding is active.
2715 if (legal_vif_num == NULL) {
2719 error = sooptcopyin(sopt, &vifi, sizeof(int), sizeof(int));
2722 if (!legal_vif_num(vifi) && (vifi != -1)) {
2726 imo = inp_findmoptions(inp);
2727 imo->imo_multicast_vif = vifi;
2732 case IP_MULTICAST_IF:
2733 error = inp_set_multicast_if(inp, sopt);
2736 case IP_MULTICAST_TTL: {
2740 * Set the IP time-to-live for outgoing multicast packets.
2741 * The original multicast API required a char argument,
2742 * which is inconsistent with the rest of the socket API.
2743 * We allow either a char or an int.
2745 if (sopt->sopt_valsize == sizeof(u_char)) {
2746 error = sooptcopyin(sopt, &ttl, sizeof(u_char),
2753 error = sooptcopyin(sopt, &ittl, sizeof(u_int),
2763 imo = inp_findmoptions(inp);
2764 imo->imo_multicast_ttl = ttl;
2769 case IP_MULTICAST_LOOP: {
2773 * Set the loopback flag for outgoing multicast packets.
2774 * Must be zero or one. The original multicast API required a
2775 * char argument, which is inconsistent with the rest
2776 * of the socket API. We allow either a char or an int.
2778 if (sopt->sopt_valsize == sizeof(u_char)) {
2779 error = sooptcopyin(sopt, &loop, sizeof(u_char),
2786 error = sooptcopyin(sopt, &iloop, sizeof(u_int),
2790 loop = (u_char)iloop;
2792 imo = inp_findmoptions(inp);
2793 imo->imo_multicast_loop = !!loop;
2798 case IP_ADD_MEMBERSHIP:
2799 case IP_ADD_SOURCE_MEMBERSHIP:
2800 case MCAST_JOIN_GROUP:
2801 case MCAST_JOIN_SOURCE_GROUP:
2802 error = inp_join_group(inp, sopt);
2805 case IP_DROP_MEMBERSHIP:
2806 case IP_DROP_SOURCE_MEMBERSHIP:
2807 case MCAST_LEAVE_GROUP:
2808 case MCAST_LEAVE_SOURCE_GROUP:
2809 error = inp_leave_group(inp, sopt);
2812 case IP_BLOCK_SOURCE:
2813 case IP_UNBLOCK_SOURCE:
2814 case MCAST_BLOCK_SOURCE:
2815 case MCAST_UNBLOCK_SOURCE:
2816 error = inp_block_unblock_source(inp, sopt);
2820 error = inp_set_source_filters(inp, sopt);
2828 INP_UNLOCK_ASSERT(inp);
2834 * Expose IGMP's multicast filter mode and source list(s) to userland,
2835 * keyed by (ifindex, group).
2836 * The filter mode is written out as a uint32_t, followed by
2837 * 0..n of struct in_addr.
2838 * For use by ifmcstat(8).
2839 * SMPng: NOTE: unlocked read of ifindex space.
2842 sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS)
2844 struct in_addr src, group;
2846 struct ifmultiaddr *ifma;
2847 struct in_multi *inm;
2848 struct ip_msource *ims;
2852 uint32_t fmode, ifindex;
2857 if (req->newptr != NULL)
2864 if (ifindex <= 0 || ifindex > V_if_index) {
2865 CTR2(KTR_IGMPV3, "%s: ifindex %u out of range",
2870 group.s_addr = name[1];
2871 if (!IN_MULTICAST(ntohl(group.s_addr))) {
2872 CTR2(KTR_IGMPV3, "%s: group %s is not multicast",
2873 __func__, inet_ntoa(group));
2877 ifp = ifnet_byindex(ifindex);
2879 CTR2(KTR_IGMPV3, "%s: no ifp for ifindex %u",
2884 retval = sysctl_wire_old_buffer(req,
2885 sizeof(uint32_t) + (in_mcast_maxgrpsrc * sizeof(struct in_addr)));
2892 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2893 if (ifma->ifma_addr->sa_family != AF_INET ||
2894 ifma->ifma_protospec == NULL)
2896 inm = (struct in_multi *)ifma->ifma_protospec;
2897 if (!in_hosteq(inm->inm_addr, group))
2899 fmode = inm->inm_st[1].iss_fmode;
2900 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2903 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
2906 ina.s_addr = htonl(ims->ims_haddr);
2907 CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2911 * Only copy-out sources which are in-mode.
2913 if (fmode != ims_get_mode(inm, ims, 1)) {
2914 CTR1(KTR_IGMPV3, "%s: skip non-in-mode",
2918 src.s_addr = htonl(ims->ims_haddr);
2919 retval = SYSCTL_OUT(req, &src, sizeof(struct in_addr));
2924 IF_ADDR_RUNLOCK(ifp);
2931 #if defined(KTR) && (KTR_COMPILE & KTR_IGMPV3)
2933 static const char *inm_modestrs[] = { "un", "in", "ex" };
2936 inm_mode_str(const int mode)
2939 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2940 return (inm_modestrs[mode]);
2944 static const char *inm_statestrs[] = {
2957 inm_state_str(const int state)
2960 if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
2961 return (inm_statestrs[state]);
2966 * Dump an in_multi structure to the console.
2969 inm_print(const struct in_multi *inm)
2973 if ((ktr_mask & KTR_IGMPV3) == 0)
2976 printf("%s: --- begin inm %p ---\n", __func__, inm);
2977 printf("addr %s ifp %p(%s) ifma %p\n",
2978 inet_ntoa(inm->inm_addr),
2980 inm->inm_ifp->if_xname,
2982 printf("timer %u state %s refcount %u scq.len %u\n",
2984 inm_state_str(inm->inm_state),
2986 inm->inm_scq.ifq_len);
2987 printf("igi %p nsrc %lu sctimer %u scrv %u\n",
2992 for (t = 0; t < 2; t++) {
2993 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
2994 inm_mode_str(inm->inm_st[t].iss_fmode),
2995 inm->inm_st[t].iss_asm,
2996 inm->inm_st[t].iss_ex,
2997 inm->inm_st[t].iss_in,
2998 inm->inm_st[t].iss_rec);
3000 printf("%s: --- end inm %p ---\n", __func__, inm);
3003 #else /* !KTR || !(KTR_COMPILE & KTR_IGMPV3) */
3006 inm_print(const struct in_multi *inm)
3011 #endif /* KTR && (KTR_COMPILE & KTR_IGMPV3) */
3013 RB_GENERATE(ip_msource_tree, ip_msource, ims_link, ip_msource_cmp);