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_dl.h>
54 #include <net/route.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in_pcb.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/igmp_var.h>
65 #define KTR_IGMPV3 KTR_INET
68 #ifndef __SOCKUNION_DECLARED
70 struct sockaddr_storage ss;
72 struct sockaddr_dl sdl;
73 struct sockaddr_in sin;
75 typedef union sockunion sockunion_t;
76 #define __SOCKUNION_DECLARED
77 #endif /* __SOCKUNION_DECLARED */
79 static MALLOC_DEFINE(M_INMFILTER, "in_mfilter",
80 "IPv4 multicast PCB-layer source filter");
81 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "IPv4 multicast group");
82 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "IPv4 multicast options");
83 static MALLOC_DEFINE(M_IPMSOURCE, "ip_msource",
84 "IPv4 multicast IGMP-layer source filter");
88 * - Lock order is: Giant, INP_WLOCK, IN_MULTI_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
89 * - The IF_ADDR_LOCK is implicitly taken by inm_lookup() earlier, however
90 * it can be taken by code in net/if.c also.
91 * - ip_moptions and in_mfilter are covered by the INP_WLOCK.
93 * struct in_multi is covered by IN_MULTI_LOCK. There isn't strictly
94 * any need for in_multi itself to be virtualized -- it is bound to an ifp
95 * anyway no matter what happens.
97 struct mtx in_multi_mtx;
98 MTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF);
101 * Functions with non-static linkage defined in this file should be
102 * declared in in_var.h:
107 * in_joingroup_locked()
109 * in_leavegroup_locked()
115 * XXX: Both carp and pf need to use the legacy (*,G) KPIs in_addmulti()
118 static void imf_commit(struct in_mfilter *);
119 static int imf_get_source(struct in_mfilter *imf,
120 const struct sockaddr_in *psin,
121 struct in_msource **);
122 static struct in_msource *
123 imf_graft(struct in_mfilter *, const uint8_t,
124 const struct sockaddr_in *);
125 static void imf_leave(struct in_mfilter *);
126 static int imf_prune(struct in_mfilter *, const struct sockaddr_in *);
127 static void imf_purge(struct in_mfilter *);
128 static void imf_rollback(struct in_mfilter *);
129 static void imf_reap(struct in_mfilter *);
130 static int imo_grow(struct ip_moptions *);
131 static size_t imo_match_group(const struct ip_moptions *,
132 const struct ifnet *, const struct sockaddr *);
133 static struct in_msource *
134 imo_match_source(const struct ip_moptions *, const size_t,
135 const struct sockaddr *);
136 static void ims_merge(struct ip_msource *ims,
137 const struct in_msource *lims, const int rollback);
138 static int in_getmulti(struct ifnet *, const struct in_addr *,
140 static int inm_get_source(struct in_multi *inm, const in_addr_t haddr,
141 const int noalloc, struct ip_msource **pims);
143 static int inm_is_ifp_detached(const struct in_multi *);
145 static int inm_merge(struct in_multi *, /*const*/ struct in_mfilter *);
146 static void inm_purge(struct in_multi *);
147 static void inm_reap(struct in_multi *);
148 static struct ip_moptions *
149 inp_findmoptions(struct inpcb *);
150 static void inp_freemoptions_internal(struct ip_moptions *);
151 static void inp_gcmoptions(void *, int);
152 static int inp_get_source_filters(struct inpcb *, struct sockopt *);
153 static int inp_join_group(struct inpcb *, struct sockopt *);
154 static int inp_leave_group(struct inpcb *, struct sockopt *);
155 static struct ifnet *
156 inp_lookup_mcast_ifp(const struct inpcb *,
157 const struct sockaddr_in *, const struct in_addr);
158 static int inp_block_unblock_source(struct inpcb *, struct sockopt *);
159 static int inp_set_multicast_if(struct inpcb *, struct sockopt *);
160 static int inp_set_source_filters(struct inpcb *, struct sockopt *);
161 static int sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS);
163 static SYSCTL_NODE(_net_inet_ip, OID_AUTO, mcast, CTLFLAG_RW, 0,
166 static u_long in_mcast_maxgrpsrc = IP_MAX_GROUP_SRC_FILTER;
167 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxgrpsrc,
168 CTLFLAG_RW | CTLFLAG_TUN, &in_mcast_maxgrpsrc, 0,
169 "Max source filters per group");
170 TUNABLE_ULONG("net.inet.ip.mcast.maxgrpsrc", &in_mcast_maxgrpsrc);
172 static u_long in_mcast_maxsocksrc = IP_MAX_SOCK_SRC_FILTER;
173 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxsocksrc,
174 CTLFLAG_RW | CTLFLAG_TUN, &in_mcast_maxsocksrc, 0,
175 "Max source filters per socket");
176 TUNABLE_ULONG("net.inet.ip.mcast.maxsocksrc", &in_mcast_maxsocksrc);
178 int in_mcast_loop = IP_DEFAULT_MULTICAST_LOOP;
179 SYSCTL_INT(_net_inet_ip_mcast, OID_AUTO, loop, CTLFLAG_RW | CTLFLAG_TUN,
180 &in_mcast_loop, 0, "Loopback multicast datagrams by default");
181 TUNABLE_INT("net.inet.ip.mcast.loop", &in_mcast_loop);
183 static SYSCTL_NODE(_net_inet_ip_mcast, OID_AUTO, filters,
184 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip_mcast_filters,
185 "Per-interface stack-wide source filters");
187 static STAILQ_HEAD(, ip_moptions) imo_gc_list =
188 STAILQ_HEAD_INITIALIZER(imo_gc_list);
189 static struct task imo_gc_task = TASK_INITIALIZER(0, inp_gcmoptions, NULL);
193 * Inline function which wraps assertions for a valid ifp.
194 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
198 inm_is_ifp_detached(const struct in_multi *inm)
202 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
203 ifp = inm->inm_ifma->ifma_ifp;
206 * Sanity check that netinet's notion of ifp is the
209 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
212 return (ifp == NULL);
217 * Initialize an in_mfilter structure to a known state at t0, t1
218 * with an empty source filter list.
221 imf_init(struct in_mfilter *imf, const int st0, const int st1)
223 memset(imf, 0, sizeof(struct in_mfilter));
224 RB_INIT(&imf->imf_sources);
225 imf->imf_st[0] = st0;
226 imf->imf_st[1] = st1;
230 * Resize the ip_moptions vector to the next power-of-two minus 1.
231 * May be called with locks held; do not sleep.
234 imo_grow(struct ip_moptions *imo)
236 struct in_multi **nmships;
237 struct in_multi **omships;
238 struct in_mfilter *nmfilters;
239 struct in_mfilter *omfilters;
246 omships = imo->imo_membership;
247 omfilters = imo->imo_mfilters;
248 oldmax = imo->imo_max_memberships;
249 newmax = ((oldmax + 1) * 2) - 1;
251 if (newmax <= IP_MAX_MEMBERSHIPS) {
252 nmships = (struct in_multi **)realloc(omships,
253 sizeof(struct in_multi *) * newmax, M_IPMOPTS, M_NOWAIT);
254 nmfilters = (struct in_mfilter *)realloc(omfilters,
255 sizeof(struct in_mfilter) * newmax, M_INMFILTER, M_NOWAIT);
256 if (nmships != NULL && nmfilters != NULL) {
257 /* Initialize newly allocated source filter heads. */
258 for (idx = oldmax; idx < newmax; idx++) {
259 imf_init(&nmfilters[idx], MCAST_UNDEFINED,
262 imo->imo_max_memberships = newmax;
263 imo->imo_membership = nmships;
264 imo->imo_mfilters = nmfilters;
268 if (nmships == NULL || nmfilters == NULL) {
270 free(nmships, M_IPMOPTS);
271 if (nmfilters != NULL)
272 free(nmfilters, M_INMFILTER);
273 return (ETOOMANYREFS);
280 * Find an IPv4 multicast group entry for this ip_moptions instance
281 * which matches the specified group, and optionally an interface.
282 * Return its index into the array, or -1 if not found.
285 imo_match_group(const struct ip_moptions *imo, const struct ifnet *ifp,
286 const struct sockaddr *group)
288 const struct sockaddr_in *gsin;
289 struct in_multi **pinm;
293 gsin = (const struct sockaddr_in *)group;
295 /* The imo_membership array may be lazy allocated. */
296 if (imo->imo_membership == NULL || imo->imo_num_memberships == 0)
299 nmships = imo->imo_num_memberships;
300 pinm = &imo->imo_membership[0];
301 for (idx = 0; idx < nmships; idx++, pinm++) {
304 if ((ifp == NULL || ((*pinm)->inm_ifp == ifp)) &&
305 in_hosteq((*pinm)->inm_addr, gsin->sin_addr)) {
316 * Find an IPv4 multicast source entry for this imo which matches
317 * the given group index for this socket, and source address.
319 * NOTE: This does not check if the entry is in-mode, merely if
320 * it exists, which may not be the desired behaviour.
322 static struct in_msource *
323 imo_match_source(const struct ip_moptions *imo, const size_t gidx,
324 const struct sockaddr *src)
326 struct ip_msource find;
327 struct in_mfilter *imf;
328 struct ip_msource *ims;
329 const sockunion_t *psa;
331 KASSERT(src->sa_family == AF_INET, ("%s: !AF_INET", __func__));
332 KASSERT(gidx != -1 && gidx < imo->imo_num_memberships,
333 ("%s: invalid index %d\n", __func__, (int)gidx));
335 /* The imo_mfilters array may be lazy allocated. */
336 if (imo->imo_mfilters == NULL)
338 imf = &imo->imo_mfilters[gidx];
340 /* Source trees are keyed in host byte order. */
341 psa = (const sockunion_t *)src;
342 find.ims_haddr = ntohl(psa->sin.sin_addr.s_addr);
343 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
345 return ((struct in_msource *)ims);
349 * Perform filtering for multicast datagrams on a socket by group and source.
351 * Returns 0 if a datagram should be allowed through, or various error codes
352 * if the socket was not a member of the group, or the source was muted, etc.
355 imo_multi_filter(const struct ip_moptions *imo, const struct ifnet *ifp,
356 const struct sockaddr *group, const struct sockaddr *src)
359 struct in_msource *ims;
362 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
364 gidx = imo_match_group(imo, ifp, group);
366 return (MCAST_NOTGMEMBER);
369 * Check if the source was included in an (S,G) join.
370 * Allow reception on exclusive memberships by default,
371 * reject reception on inclusive memberships by default.
372 * Exclude source only if an in-mode exclude filter exists.
373 * Include source only if an in-mode include filter exists.
374 * NOTE: We are comparing group state here at IGMP t1 (now)
375 * with socket-layer t0 (since last downcall).
377 mode = imo->imo_mfilters[gidx].imf_st[1];
378 ims = imo_match_source(imo, gidx, src);
380 if ((ims == NULL && mode == MCAST_INCLUDE) ||
381 (ims != NULL && ims->imsl_st[0] != mode))
382 return (MCAST_NOTSMEMBER);
388 * Find and return a reference to an in_multi record for (ifp, group),
389 * and bump its reference count.
390 * If one does not exist, try to allocate it, and update link-layer multicast
391 * filters on ifp to listen for group.
392 * Assumes the IN_MULTI lock is held across the call.
393 * Return 0 if successful, otherwise return an appropriate error code.
396 in_getmulti(struct ifnet *ifp, const struct in_addr *group,
397 struct in_multi **pinm)
399 struct sockaddr_in gsin;
400 struct ifmultiaddr *ifma;
401 struct in_ifinfo *ii;
402 struct in_multi *inm;
405 IN_MULTI_LOCK_ASSERT();
407 ii = (struct in_ifinfo *)ifp->if_afdata[AF_INET];
409 inm = inm_lookup(ifp, *group);
412 * If we already joined this group, just bump the
413 * refcount and return it.
415 KASSERT(inm->inm_refcount >= 1,
416 ("%s: bad refcount %d", __func__, inm->inm_refcount));
422 memset(&gsin, 0, sizeof(gsin));
423 gsin.sin_family = AF_INET;
424 gsin.sin_len = sizeof(struct sockaddr_in);
425 gsin.sin_addr = *group;
428 * Check if a link-layer group is already associated
429 * with this network-layer group on the given ifnet.
431 error = if_addmulti(ifp, (struct sockaddr *)&gsin, &ifma);
435 /* XXX ifma_protospec must be covered by IF_ADDR_LOCK */
439 * If something other than netinet is occupying the link-layer
440 * group, print a meaningful error message and back out of
442 * Otherwise, bump the refcount on the existing network-layer
443 * group association and return it.
445 if (ifma->ifma_protospec != NULL) {
446 inm = (struct in_multi *)ifma->ifma_protospec;
448 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
450 KASSERT(ifma->ifma_addr->sa_family == AF_INET,
451 ("%s: ifma not AF_INET", __func__));
452 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
453 if (inm->inm_ifma != ifma || inm->inm_ifp != ifp ||
454 !in_hosteq(inm->inm_addr, *group))
455 panic("%s: ifma %p is inconsistent with %p (%s)",
456 __func__, ifma, inm, inet_ntoa(*group));
460 IF_ADDR_WUNLOCK(ifp);
464 IF_ADDR_WLOCK_ASSERT(ifp);
467 * A new in_multi record is needed; allocate and initialize it.
468 * We DO NOT perform an IGMP join as the in_ layer may need to
469 * push an initial source list down to IGMP to support SSM.
471 * The initial source filter state is INCLUDE, {} as per the RFC.
473 inm = malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT | M_ZERO);
475 if_delmulti_ifma(ifma);
476 IF_ADDR_WUNLOCK(ifp);
479 inm->inm_addr = *group;
481 inm->inm_igi = ii->ii_igmp;
482 inm->inm_ifma = ifma;
483 inm->inm_refcount = 1;
484 inm->inm_state = IGMP_NOT_MEMBER;
487 * Pending state-changes per group are subject to a bounds check.
489 IFQ_SET_MAXLEN(&inm->inm_scq, IGMP_MAX_STATE_CHANGES);
491 inm->inm_st[0].iss_fmode = MCAST_UNDEFINED;
492 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
493 RB_INIT(&inm->inm_srcs);
495 ifma->ifma_protospec = inm;
499 IF_ADDR_WUNLOCK(ifp);
504 * Drop a reference to an in_multi record.
506 * If the refcount drops to 0, free the in_multi record and
507 * delete the underlying link-layer membership.
510 inm_release_locked(struct in_multi *inm)
512 struct ifmultiaddr *ifma;
514 IN_MULTI_LOCK_ASSERT();
516 CTR2(KTR_IGMPV3, "%s: refcount is %d", __func__, inm->inm_refcount);
518 if (--inm->inm_refcount > 0) {
519 CTR2(KTR_IGMPV3, "%s: refcount is now %d", __func__,
524 CTR2(KTR_IGMPV3, "%s: freeing inm %p", __func__, inm);
526 ifma = inm->inm_ifma;
528 /* XXX this access is not covered by IF_ADDR_LOCK */
529 CTR2(KTR_IGMPV3, "%s: purging ifma %p", __func__, ifma);
530 KASSERT(ifma->ifma_protospec == inm,
531 ("%s: ifma_protospec != inm", __func__));
532 ifma->ifma_protospec = NULL;
536 free(inm, M_IPMADDR);
538 if_delmulti_ifma(ifma);
542 * Clear recorded source entries for a group.
543 * Used by the IGMP code. Caller must hold the IN_MULTI lock.
544 * FIXME: Should reap.
547 inm_clear_recorded(struct in_multi *inm)
549 struct ip_msource *ims;
551 IN_MULTI_LOCK_ASSERT();
553 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
556 --inm->inm_st[1].iss_rec;
559 KASSERT(inm->inm_st[1].iss_rec == 0,
560 ("%s: iss_rec %d not 0", __func__, inm->inm_st[1].iss_rec));
564 * Record a source as pending for a Source-Group IGMPv3 query.
565 * This lives here as it modifies the shared tree.
567 * inm is the group descriptor.
568 * naddr is the address of the source to record in network-byte order.
570 * If the net.inet.igmp.sgalloc sysctl is non-zero, we will
571 * lazy-allocate a source node in response to an SG query.
572 * Otherwise, no allocation is performed. This saves some memory
573 * with the trade-off that the source will not be reported to the
574 * router if joined in the window between the query response and
575 * the group actually being joined on the local host.
577 * VIMAGE: XXX: Currently the igmp_sgalloc feature has been removed.
578 * This turns off the allocation of a recorded source entry if
579 * the group has not been joined.
581 * Return 0 if the source didn't exist or was already marked as recorded.
582 * Return 1 if the source was marked as recorded by this function.
583 * Return <0 if any error occured (negated errno code).
586 inm_record_source(struct in_multi *inm, const in_addr_t naddr)
588 struct ip_msource find;
589 struct ip_msource *ims, *nims;
591 IN_MULTI_LOCK_ASSERT();
593 find.ims_haddr = ntohl(naddr);
594 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
595 if (ims && ims->ims_stp)
598 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
600 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
604 nims->ims_haddr = find.ims_haddr;
605 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
611 * Mark the source as recorded and update the recorded
615 ++inm->inm_st[1].iss_rec;
621 * Return a pointer to an in_msource owned by an in_mfilter,
622 * given its source address.
623 * Lazy-allocate if needed. If this is a new entry its filter state is
626 * imf is the filter set being modified.
627 * haddr is the source address in *host* byte-order.
629 * SMPng: May be called with locks held; malloc must not block.
632 imf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin,
633 struct in_msource **plims)
635 struct ip_msource find;
636 struct ip_msource *ims, *nims;
637 struct in_msource *lims;
644 /* key is host byte order */
645 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
646 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
647 lims = (struct in_msource *)ims;
649 if (imf->imf_nsrc == in_mcast_maxsocksrc)
651 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
655 lims = (struct in_msource *)nims;
656 lims->ims_haddr = find.ims_haddr;
657 lims->imsl_st[0] = MCAST_UNDEFINED;
658 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
668 * Graft a source entry into an existing socket-layer filter set,
669 * maintaining any required invariants and checking allocations.
671 * The source is marked as being in the new filter mode at t1.
673 * Return the pointer to the new node, otherwise return NULL.
675 static struct in_msource *
676 imf_graft(struct in_mfilter *imf, const uint8_t st1,
677 const struct sockaddr_in *psin)
679 struct ip_msource *nims;
680 struct in_msource *lims;
682 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
686 lims = (struct in_msource *)nims;
687 lims->ims_haddr = ntohl(psin->sin_addr.s_addr);
688 lims->imsl_st[0] = MCAST_UNDEFINED;
689 lims->imsl_st[1] = st1;
690 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
697 * Prune a source entry from an existing socket-layer filter set,
698 * maintaining any required invariants and checking allocations.
700 * The source is marked as being left at t1, it is not freed.
702 * Return 0 if no error occurred, otherwise return an errno value.
705 imf_prune(struct in_mfilter *imf, const struct sockaddr_in *psin)
707 struct ip_msource find;
708 struct ip_msource *ims;
709 struct in_msource *lims;
711 /* key is host byte order */
712 find.ims_haddr = ntohl(psin->sin_addr.s_addr);
713 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
716 lims = (struct in_msource *)ims;
717 lims->imsl_st[1] = MCAST_UNDEFINED;
722 * Revert socket-layer filter set deltas at t1 to t0 state.
725 imf_rollback(struct in_mfilter *imf)
727 struct ip_msource *ims, *tims;
728 struct in_msource *lims;
730 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
731 lims = (struct in_msource *)ims;
732 if (lims->imsl_st[0] == lims->imsl_st[1]) {
733 /* no change at t1 */
735 } else if (lims->imsl_st[0] != MCAST_UNDEFINED) {
736 /* revert change to existing source at t1 */
737 lims->imsl_st[1] = lims->imsl_st[0];
739 /* revert source added t1 */
740 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
741 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
742 free(ims, M_INMFILTER);
746 imf->imf_st[1] = imf->imf_st[0];
750 * Mark socket-layer filter set as INCLUDE {} at t1.
753 imf_leave(struct in_mfilter *imf)
755 struct ip_msource *ims;
756 struct in_msource *lims;
758 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
759 lims = (struct in_msource *)ims;
760 lims->imsl_st[1] = MCAST_UNDEFINED;
762 imf->imf_st[1] = MCAST_INCLUDE;
766 * Mark socket-layer filter set deltas as committed.
769 imf_commit(struct in_mfilter *imf)
771 struct ip_msource *ims;
772 struct in_msource *lims;
774 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
775 lims = (struct in_msource *)ims;
776 lims->imsl_st[0] = lims->imsl_st[1];
778 imf->imf_st[0] = imf->imf_st[1];
782 * Reap unreferenced sources from socket-layer filter set.
785 imf_reap(struct in_mfilter *imf)
787 struct ip_msource *ims, *tims;
788 struct in_msource *lims;
790 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
791 lims = (struct in_msource *)ims;
792 if ((lims->imsl_st[0] == MCAST_UNDEFINED) &&
793 (lims->imsl_st[1] == MCAST_UNDEFINED)) {
794 CTR2(KTR_IGMPV3, "%s: free lims %p", __func__, ims);
795 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
796 free(ims, M_INMFILTER);
803 * Purge socket-layer filter set.
806 imf_purge(struct in_mfilter *imf)
808 struct ip_msource *ims, *tims;
810 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
811 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
812 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
813 free(ims, M_INMFILTER);
816 imf->imf_st[0] = imf->imf_st[1] = MCAST_UNDEFINED;
817 KASSERT(RB_EMPTY(&imf->imf_sources),
818 ("%s: imf_sources not empty", __func__));
822 * Look up a source filter entry for a multicast group.
824 * inm is the group descriptor to work with.
825 * haddr is the host-byte-order IPv4 address to look up.
826 * noalloc may be non-zero to suppress allocation of sources.
827 * *pims will be set to the address of the retrieved or allocated source.
829 * SMPng: NOTE: may be called with locks held.
830 * Return 0 if successful, otherwise return a non-zero error code.
833 inm_get_source(struct in_multi *inm, const in_addr_t haddr,
834 const int noalloc, struct ip_msource **pims)
836 struct ip_msource find;
837 struct ip_msource *ims, *nims;
842 find.ims_haddr = haddr;
843 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
844 if (ims == NULL && !noalloc) {
845 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
847 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
851 nims->ims_haddr = haddr;
852 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
856 ia.s_addr = htonl(haddr);
857 CTR3(KTR_IGMPV3, "%s: allocated %s as %p", __func__,
867 * Merge socket-layer source into IGMP-layer source.
868 * If rollback is non-zero, perform the inverse of the merge.
871 ims_merge(struct ip_msource *ims, const struct in_msource *lims,
874 int n = rollback ? -1 : 1;
878 ia.s_addr = htonl(ims->ims_haddr);
881 if (lims->imsl_st[0] == MCAST_EXCLUDE) {
882 CTR3(KTR_IGMPV3, "%s: t1 ex -= %d on %s",
883 __func__, n, inet_ntoa(ia));
884 ims->ims_st[1].ex -= n;
885 } else if (lims->imsl_st[0] == MCAST_INCLUDE) {
886 CTR3(KTR_IGMPV3, "%s: t1 in -= %d on %s",
887 __func__, n, inet_ntoa(ia));
888 ims->ims_st[1].in -= n;
891 if (lims->imsl_st[1] == MCAST_EXCLUDE) {
892 CTR3(KTR_IGMPV3, "%s: t1 ex += %d on %s",
893 __func__, n, inet_ntoa(ia));
894 ims->ims_st[1].ex += n;
895 } else if (lims->imsl_st[1] == MCAST_INCLUDE) {
896 CTR3(KTR_IGMPV3, "%s: t1 in += %d on %s",
897 __func__, n, inet_ntoa(ia));
898 ims->ims_st[1].in += n;
903 * Atomically update the global in_multi state, when a membership's
904 * filter list is being updated in any way.
906 * imf is the per-inpcb-membership group filter pointer.
907 * A fake imf may be passed for in-kernel consumers.
909 * XXX This is a candidate for a set-symmetric-difference style loop
910 * which would eliminate the repeated lookup from root of ims nodes,
911 * as they share the same key space.
913 * If any error occurred this function will back out of refcounts
914 * and return a non-zero value.
917 inm_merge(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
919 struct ip_msource *ims, *nims;
920 struct in_msource *lims;
929 * Update the source filters first, as this may fail.
930 * Maintain count of in-mode filters at t0, t1. These are
931 * used to work out if we transition into ASM mode or not.
932 * Maintain a count of source filters whose state was
933 * actually modified by this operation.
935 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
936 lims = (struct in_msource *)ims;
937 if (lims->imsl_st[0] == imf->imf_st[0]) nsrc0++;
938 if (lims->imsl_st[1] == imf->imf_st[1]) nsrc1++;
939 if (lims->imsl_st[0] == lims->imsl_st[1]) continue;
940 error = inm_get_source(inm, lims->ims_haddr, 0, &nims);
944 ims_merge(nims, lims, 0);
947 struct ip_msource *bims;
949 RB_FOREACH_REVERSE_FROM(ims, ip_msource_tree, nims) {
950 lims = (struct in_msource *)ims;
951 if (lims->imsl_st[0] == lims->imsl_st[1])
953 (void)inm_get_source(inm, lims->ims_haddr, 1, &bims);
956 ims_merge(bims, lims, 1);
961 CTR3(KTR_IGMPV3, "%s: imf filters in-mode: %d at t0, %d at t1",
962 __func__, nsrc0, nsrc1);
964 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
965 if (imf->imf_st[0] == imf->imf_st[1] &&
966 imf->imf_st[1] == MCAST_INCLUDE) {
968 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
969 --inm->inm_st[1].iss_in;
973 /* Handle filter mode transition on socket. */
974 if (imf->imf_st[0] != imf->imf_st[1]) {
975 CTR3(KTR_IGMPV3, "%s: imf transition %d to %d",
976 __func__, imf->imf_st[0], imf->imf_st[1]);
978 if (imf->imf_st[0] == MCAST_EXCLUDE) {
979 CTR1(KTR_IGMPV3, "%s: --ex on inm at t1", __func__);
980 --inm->inm_st[1].iss_ex;
981 } else if (imf->imf_st[0] == MCAST_INCLUDE) {
982 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
983 --inm->inm_st[1].iss_in;
986 if (imf->imf_st[1] == MCAST_EXCLUDE) {
987 CTR1(KTR_IGMPV3, "%s: ex++ on inm at t1", __func__);
988 inm->inm_st[1].iss_ex++;
989 } else if (imf->imf_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
990 CTR1(KTR_IGMPV3, "%s: in++ on inm at t1", __func__);
991 inm->inm_st[1].iss_in++;
996 * Track inm filter state in terms of listener counts.
997 * If there are any exclusive listeners, stack-wide
998 * membership is exclusive.
999 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1000 * If no listeners remain, state is undefined at t1,
1001 * and the IGMP lifecycle for this group should finish.
1003 if (inm->inm_st[1].iss_ex > 0) {
1004 CTR1(KTR_IGMPV3, "%s: transition to EX", __func__);
1005 inm->inm_st[1].iss_fmode = MCAST_EXCLUDE;
1006 } else if (inm->inm_st[1].iss_in > 0) {
1007 CTR1(KTR_IGMPV3, "%s: transition to IN", __func__);
1008 inm->inm_st[1].iss_fmode = MCAST_INCLUDE;
1010 CTR1(KTR_IGMPV3, "%s: transition to UNDEF", __func__);
1011 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
1014 /* Decrement ASM listener count on transition out of ASM mode. */
1015 if (imf->imf_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1016 if ((imf->imf_st[1] != MCAST_EXCLUDE) ||
1017 (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 > 0))
1018 CTR1(KTR_IGMPV3, "%s: --asm on inm at t1", __func__);
1019 --inm->inm_st[1].iss_asm;
1022 /* Increment ASM listener count on transition to ASM mode. */
1023 if (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1024 CTR1(KTR_IGMPV3, "%s: asm++ on inm at t1", __func__);
1025 inm->inm_st[1].iss_asm++;
1028 CTR3(KTR_IGMPV3, "%s: merged imf %p to inm %p", __func__, imf, inm);
1033 CTR1(KTR_IGMPV3, "%s: sources changed; reaping", __func__);
1040 * Mark an in_multi's filter set deltas as committed.
1041 * Called by IGMP after a state change has been enqueued.
1044 inm_commit(struct in_multi *inm)
1046 struct ip_msource *ims;
1048 CTR2(KTR_IGMPV3, "%s: commit inm %p", __func__, inm);
1049 CTR1(KTR_IGMPV3, "%s: pre commit:", __func__);
1052 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
1053 ims->ims_st[0] = ims->ims_st[1];
1055 inm->inm_st[0] = inm->inm_st[1];
1059 * Reap unreferenced nodes from an in_multi's filter set.
1062 inm_reap(struct in_multi *inm)
1064 struct ip_msource *ims, *tims;
1066 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1067 if (ims->ims_st[0].ex > 0 || ims->ims_st[0].in > 0 ||
1068 ims->ims_st[1].ex > 0 || ims->ims_st[1].in > 0 ||
1071 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1072 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1073 free(ims, M_IPMSOURCE);
1079 * Purge all source nodes from an in_multi's filter set.
1082 inm_purge(struct in_multi *inm)
1084 struct ip_msource *ims, *tims;
1086 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1087 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1088 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1089 free(ims, M_IPMSOURCE);
1095 * Join a multicast group; unlocked entry point.
1097 * SMPng: XXX: in_joingroup() is called from in_control() when Giant
1098 * is not held. Fortunately, ifp is unlikely to have been detached
1099 * at this point, so we assume it's OK to recurse.
1102 in_joingroup(struct ifnet *ifp, const struct in_addr *gina,
1103 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1108 error = in_joingroup_locked(ifp, gina, imf, pinm);
1115 * Join a multicast group; real entry point.
1117 * Only preserves atomicity at inm level.
1118 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1120 * If the IGMP downcall fails, the group is not joined, and an error
1124 in_joingroup_locked(struct ifnet *ifp, const struct in_addr *gina,
1125 /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1127 struct in_mfilter timf;
1128 struct in_multi *inm;
1131 IN_MULTI_LOCK_ASSERT();
1133 CTR4(KTR_IGMPV3, "%s: join %s on %p(%s))", __func__,
1134 inet_ntoa(*gina), ifp, ifp->if_xname);
1140 * If no imf was specified (i.e. kernel consumer),
1141 * fake one up and assume it is an ASM join.
1144 imf_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1148 error = in_getmulti(ifp, gina, &inm);
1150 CTR1(KTR_IGMPV3, "%s: in_getmulti() failure", __func__);
1154 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1155 error = inm_merge(inm, imf);
1157 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1158 goto out_inm_release;
1161 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1162 error = igmp_change_state(inm);
1164 CTR1(KTR_IGMPV3, "%s: failed to update source", __func__);
1165 goto out_inm_release;
1170 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1171 inm_release_locked(inm);
1180 * Leave a multicast group; unlocked entry point.
1183 in_leavegroup(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1188 error = in_leavegroup_locked(inm, imf);
1195 * Leave a multicast group; real entry point.
1196 * All source filters will be expunged.
1198 * Only preserves atomicity at inm level.
1200 * Holding the write lock for the INP which contains imf
1201 * is highly advisable. We can't assert for it as imf does not
1202 * contain a back-pointer to the owning inp.
1204 * Note: This is not the same as inm_release(*) as this function also
1205 * makes a state change downcall into IGMP.
1208 in_leavegroup_locked(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1210 struct in_mfilter timf;
1215 IN_MULTI_LOCK_ASSERT();
1217 CTR5(KTR_IGMPV3, "%s: leave inm %p, %s/%s, imf %p", __func__,
1218 inm, inet_ntoa(inm->inm_addr),
1219 (inm_is_ifp_detached(inm) ? "null" : inm->inm_ifp->if_xname),
1223 * If no imf was specified (i.e. kernel consumer),
1224 * fake one up and assume it is an ASM join.
1227 imf_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1232 * Begin state merge transaction at IGMP layer.
1234 * As this particular invocation should not cause any memory
1235 * to be allocated, and there is no opportunity to roll back
1236 * the transaction, it MUST NOT fail.
1238 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1239 error = inm_merge(inm, imf);
1240 KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1242 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1243 CURVNET_SET(inm->inm_ifp->if_vnet);
1244 error = igmp_change_state(inm);
1247 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1249 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1250 inm_release_locked(inm);
1255 /*#ifndef BURN_BRIDGES*/
1257 * Join an IPv4 multicast group in (*,G) exclusive mode.
1258 * The group must be a 224.0.0.0/24 link-scope group.
1259 * This KPI is for legacy kernel consumers only.
1262 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1264 struct in_multi *pinm;
1267 KASSERT(IN_LOCAL_GROUP(ntohl(ap->s_addr)),
1268 ("%s: %s not in 224.0.0.0/24", __func__, inet_ntoa(*ap)));
1270 error = in_joingroup(ifp, ap, NULL, &pinm);
1278 * Leave an IPv4 multicast group, assumed to be in exclusive (*,G) mode.
1279 * This KPI is for legacy kernel consumers only.
1282 in_delmulti(struct in_multi *inm)
1285 (void)in_leavegroup(inm, NULL);
1290 * Block or unblock an ASM multicast source on an inpcb.
1291 * This implements the delta-based API described in RFC 3678.
1293 * The delta-based API applies only to exclusive-mode memberships.
1294 * An IGMP downcall will be performed.
1296 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1298 * Return 0 if successful, otherwise return an appropriate error code.
1301 inp_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1303 struct group_source_req gsr;
1304 sockunion_t *gsa, *ssa;
1306 struct in_mfilter *imf;
1307 struct ip_moptions *imo;
1308 struct in_msource *ims;
1309 struct in_multi *inm;
1318 memset(&gsr, 0, sizeof(struct group_source_req));
1319 gsa = (sockunion_t *)&gsr.gsr_group;
1320 ssa = (sockunion_t *)&gsr.gsr_source;
1322 switch (sopt->sopt_name) {
1323 case IP_BLOCK_SOURCE:
1324 case IP_UNBLOCK_SOURCE: {
1325 struct ip_mreq_source mreqs;
1327 error = sooptcopyin(sopt, &mreqs,
1328 sizeof(struct ip_mreq_source),
1329 sizeof(struct ip_mreq_source));
1333 gsa->sin.sin_family = AF_INET;
1334 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1335 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1337 ssa->sin.sin_family = AF_INET;
1338 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1339 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1341 if (!in_nullhost(mreqs.imr_interface))
1342 INADDR_TO_IFP(mreqs.imr_interface, ifp);
1344 if (sopt->sopt_name == IP_BLOCK_SOURCE)
1347 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
1348 __func__, inet_ntoa(mreqs.imr_interface), ifp);
1352 case MCAST_BLOCK_SOURCE:
1353 case MCAST_UNBLOCK_SOURCE:
1354 error = sooptcopyin(sopt, &gsr,
1355 sizeof(struct group_source_req),
1356 sizeof(struct group_source_req));
1360 if (gsa->sin.sin_family != AF_INET ||
1361 gsa->sin.sin_len != sizeof(struct sockaddr_in))
1364 if (ssa->sin.sin_family != AF_INET ||
1365 ssa->sin.sin_len != sizeof(struct sockaddr_in))
1368 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1369 return (EADDRNOTAVAIL);
1371 ifp = ifnet_byindex(gsr.gsr_interface);
1373 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1378 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
1379 __func__, sopt->sopt_name);
1380 return (EOPNOTSUPP);
1384 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1388 * Check if we are actually a member of this group.
1390 imo = inp_findmoptions(inp);
1391 idx = imo_match_group(imo, ifp, &gsa->sa);
1392 if (idx == -1 || imo->imo_mfilters == NULL) {
1393 error = EADDRNOTAVAIL;
1394 goto out_inp_locked;
1397 KASSERT(imo->imo_mfilters != NULL,
1398 ("%s: imo_mfilters not allocated", __func__));
1399 imf = &imo->imo_mfilters[idx];
1400 inm = imo->imo_membership[idx];
1403 * Attempting to use the delta-based API on an
1404 * non exclusive-mode membership is an error.
1406 fmode = imf->imf_st[0];
1407 if (fmode != MCAST_EXCLUDE) {
1409 goto out_inp_locked;
1413 * Deal with error cases up-front:
1414 * Asked to block, but already blocked; or
1415 * Asked to unblock, but nothing to unblock.
1416 * If adding a new block entry, allocate it.
1418 ims = imo_match_source(imo, idx, &ssa->sa);
1419 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1420 CTR3(KTR_IGMPV3, "%s: source %s %spresent", __func__,
1421 inet_ntoa(ssa->sin.sin_addr), doblock ? "" : "not ");
1422 error = EADDRNOTAVAIL;
1423 goto out_inp_locked;
1426 INP_WLOCK_ASSERT(inp);
1429 * Begin state merge transaction at socket layer.
1432 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
1433 ims = imf_graft(imf, fmode, &ssa->sin);
1437 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
1438 error = imf_prune(imf, &ssa->sin);
1442 CTR1(KTR_IGMPV3, "%s: merge imf state failed", __func__);
1443 goto out_imf_rollback;
1447 * Begin state merge transaction at IGMP layer.
1451 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1452 error = inm_merge(inm, imf);
1454 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1455 goto out_in_multi_locked;
1458 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1459 error = igmp_change_state(inm);
1461 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1463 out_in_multi_locked:
1481 * Given an inpcb, return its multicast options structure pointer. Accepts
1482 * an unlocked inpcb pointer, but will return it locked. May sleep.
1484 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1485 * SMPng: NOTE: Returns with the INP write lock held.
1487 static struct ip_moptions *
1488 inp_findmoptions(struct inpcb *inp)
1490 struct ip_moptions *imo;
1491 struct in_multi **immp;
1492 struct in_mfilter *imfp;
1496 if (inp->inp_moptions != NULL)
1497 return (inp->inp_moptions);
1501 imo = malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK);
1502 immp = malloc(sizeof(*immp) * IP_MIN_MEMBERSHIPS, M_IPMOPTS,
1504 imfp = malloc(sizeof(struct in_mfilter) * IP_MIN_MEMBERSHIPS,
1505 M_INMFILTER, M_WAITOK);
1507 imo->imo_multicast_ifp = NULL;
1508 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1509 imo->imo_multicast_vif = -1;
1510 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1511 imo->imo_multicast_loop = in_mcast_loop;
1512 imo->imo_num_memberships = 0;
1513 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1514 imo->imo_membership = immp;
1516 /* Initialize per-group source filters. */
1517 for (idx = 0; idx < IP_MIN_MEMBERSHIPS; idx++)
1518 imf_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE);
1519 imo->imo_mfilters = imfp;
1522 if (inp->inp_moptions != NULL) {
1523 free(imfp, M_INMFILTER);
1524 free(immp, M_IPMOPTS);
1525 free(imo, M_IPMOPTS);
1526 return (inp->inp_moptions);
1528 inp->inp_moptions = imo;
1533 * Discard the IP multicast options (and source filters). To minimize
1534 * the amount of work done while holding locks such as the INP's
1535 * pcbinfo lock (which is used in the receive path), the free
1536 * operation is performed asynchronously in a separate task.
1538 * SMPng: NOTE: assumes INP write lock is held.
1541 inp_freemoptions(struct ip_moptions *imo)
1544 KASSERT(imo != NULL, ("%s: ip_moptions is NULL", __func__));
1546 STAILQ_INSERT_TAIL(&imo_gc_list, imo, imo_link);
1548 taskqueue_enqueue(taskqueue_thread, &imo_gc_task);
1552 inp_freemoptions_internal(struct ip_moptions *imo)
1554 struct in_mfilter *imf;
1555 size_t idx, nmships;
1557 nmships = imo->imo_num_memberships;
1558 for (idx = 0; idx < nmships; ++idx) {
1559 imf = imo->imo_mfilters ? &imo->imo_mfilters[idx] : NULL;
1562 (void)in_leavegroup(imo->imo_membership[idx], imf);
1567 if (imo->imo_mfilters)
1568 free(imo->imo_mfilters, M_INMFILTER);
1569 free(imo->imo_membership, M_IPMOPTS);
1570 free(imo, M_IPMOPTS);
1574 inp_gcmoptions(void *context, int pending)
1576 struct ip_moptions *imo;
1579 while (!STAILQ_EMPTY(&imo_gc_list)) {
1580 imo = STAILQ_FIRST(&imo_gc_list);
1581 STAILQ_REMOVE_HEAD(&imo_gc_list, imo_link);
1583 inp_freemoptions_internal(imo);
1590 * Atomically get source filters on a socket for an IPv4 multicast group.
1591 * Called with INP lock held; returns with lock released.
1594 inp_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1596 struct __msfilterreq msfr;
1599 struct ip_moptions *imo;
1600 struct in_mfilter *imf;
1601 struct ip_msource *ims;
1602 struct in_msource *lims;
1603 struct sockaddr_in *psin;
1604 struct sockaddr_storage *ptss;
1605 struct sockaddr_storage *tss;
1607 size_t idx, nsrcs, ncsrcs;
1609 INP_WLOCK_ASSERT(inp);
1611 imo = inp->inp_moptions;
1612 KASSERT(imo != NULL, ("%s: null ip_moptions", __func__));
1616 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1617 sizeof(struct __msfilterreq));
1621 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
1624 ifp = ifnet_byindex(msfr.msfr_ifindex);
1631 * Lookup group on the socket.
1633 gsa = (sockunion_t *)&msfr.msfr_group;
1634 idx = imo_match_group(imo, ifp, &gsa->sa);
1635 if (idx == -1 || imo->imo_mfilters == NULL) {
1637 return (EADDRNOTAVAIL);
1639 imf = &imo->imo_mfilters[idx];
1642 * Ignore memberships which are in limbo.
1644 if (imf->imf_st[1] == MCAST_UNDEFINED) {
1648 msfr.msfr_fmode = imf->imf_st[1];
1651 * If the user specified a buffer, copy out the source filter
1652 * entries to userland gracefully.
1653 * We only copy out the number of entries which userland
1654 * has asked for, but we always tell userland how big the
1655 * buffer really needs to be.
1657 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
1658 msfr.msfr_nsrcs = in_mcast_maxsocksrc;
1660 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1661 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1662 M_TEMP, M_NOWAIT | M_ZERO);
1670 * Count number of sources in-mode at t0.
1671 * If buffer space exists and remains, copy out source entries.
1673 nsrcs = msfr.msfr_nsrcs;
1676 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
1677 lims = (struct in_msource *)ims;
1678 if (lims->imsl_st[0] == MCAST_UNDEFINED ||
1679 lims->imsl_st[0] != imf->imf_st[0])
1682 if (tss != NULL && nsrcs > 0) {
1683 psin = (struct sockaddr_in *)ptss;
1684 psin->sin_family = AF_INET;
1685 psin->sin_len = sizeof(struct sockaddr_in);
1686 psin->sin_addr.s_addr = htonl(lims->ims_haddr);
1696 error = copyout(tss, msfr.msfr_srcs,
1697 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1703 msfr.msfr_nsrcs = ncsrcs;
1704 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1710 * Return the IP multicast options in response to user getsockopt().
1713 inp_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1715 struct ip_mreqn mreqn;
1716 struct ip_moptions *imo;
1718 struct in_ifaddr *ia;
1723 imo = inp->inp_moptions;
1725 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1726 * or is a divert socket, reject it.
1728 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1729 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1730 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
1732 return (EOPNOTSUPP);
1736 switch (sopt->sopt_name) {
1737 case IP_MULTICAST_VIF:
1739 optval = imo->imo_multicast_vif;
1743 error = sooptcopyout(sopt, &optval, sizeof(int));
1746 case IP_MULTICAST_IF:
1747 memset(&mreqn, 0, sizeof(struct ip_mreqn));
1749 ifp = imo->imo_multicast_ifp;
1750 if (!in_nullhost(imo->imo_multicast_addr)) {
1751 mreqn.imr_address = imo->imo_multicast_addr;
1752 } else if (ifp != NULL) {
1753 mreqn.imr_ifindex = ifp->if_index;
1757 IA_SIN(ia)->sin_addr;
1758 ifa_free(&ia->ia_ifa);
1763 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
1764 error = sooptcopyout(sopt, &mreqn,
1765 sizeof(struct ip_mreqn));
1767 error = sooptcopyout(sopt, &mreqn.imr_address,
1768 sizeof(struct in_addr));
1772 case IP_MULTICAST_TTL:
1774 optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1776 optval = coptval = imo->imo_multicast_ttl;
1778 if (sopt->sopt_valsize == sizeof(u_char))
1779 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1781 error = sooptcopyout(sopt, &optval, sizeof(int));
1784 case IP_MULTICAST_LOOP:
1786 optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1788 optval = coptval = imo->imo_multicast_loop;
1790 if (sopt->sopt_valsize == sizeof(u_char))
1791 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1793 error = sooptcopyout(sopt, &optval, sizeof(int));
1798 error = EADDRNOTAVAIL;
1801 error = inp_get_source_filters(inp, sopt);
1807 error = ENOPROTOOPT;
1811 INP_UNLOCK_ASSERT(inp);
1817 * Look up the ifnet to use for a multicast group membership,
1818 * given the IPv4 address of an interface, and the IPv4 group address.
1820 * This routine exists to support legacy multicast applications
1821 * which do not understand that multicast memberships are scoped to
1822 * specific physical links in the networking stack, or which need
1823 * to join link-scope groups before IPv4 addresses are configured.
1825 * If inp is non-NULL, use this socket's current FIB number for any
1826 * required FIB lookup.
1827 * If ina is INADDR_ANY, look up the group address in the unicast FIB,
1828 * and use its ifp; usually, this points to the default next-hop.
1830 * If the FIB lookup fails, attempt to use the first non-loopback
1831 * interface with multicast capability in the system as a
1832 * last resort. The legacy IPv4 ASM API requires that we do
1833 * this in order to allow groups to be joined when the routing
1834 * table has not yet been populated during boot.
1836 * Returns NULL if no ifp could be found.
1838 * SMPng: TODO: Acquire the appropriate locks for INADDR_TO_IFP.
1839 * FUTURE: Implement IPv4 source-address selection.
1841 static struct ifnet *
1842 inp_lookup_mcast_ifp(const struct inpcb *inp,
1843 const struct sockaddr_in *gsin, const struct in_addr ina)
1847 KASSERT(gsin->sin_family == AF_INET, ("%s: not AF_INET", __func__));
1848 KASSERT(IN_MULTICAST(ntohl(gsin->sin_addr.s_addr)),
1849 ("%s: not multicast", __func__));
1852 if (!in_nullhost(ina)) {
1853 INADDR_TO_IFP(ina, ifp);
1858 memcpy(&ro.ro_dst, gsin, sizeof(struct sockaddr_in));
1859 in_rtalloc_ign(&ro, 0, inp ? inp->inp_inc.inc_fibnum : 0);
1860 if (ro.ro_rt != NULL) {
1861 ifp = ro.ro_rt->rt_ifp;
1862 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
1865 struct in_ifaddr *ia;
1870 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1872 if (!(mifp->if_flags & IFF_LOOPBACK) &&
1873 (mifp->if_flags & IFF_MULTICAST)) {
1878 IN_IFADDR_RUNLOCK();
1886 * Join an IPv4 multicast group, possibly with a source.
1889 inp_join_group(struct inpcb *inp, struct sockopt *sopt)
1891 struct group_source_req gsr;
1892 sockunion_t *gsa, *ssa;
1894 struct in_mfilter *imf;
1895 struct ip_moptions *imo;
1896 struct in_multi *inm;
1897 struct in_msource *lims;
1907 memset(&gsr, 0, sizeof(struct group_source_req));
1908 gsa = (sockunion_t *)&gsr.gsr_group;
1909 gsa->ss.ss_family = AF_UNSPEC;
1910 ssa = (sockunion_t *)&gsr.gsr_source;
1911 ssa->ss.ss_family = AF_UNSPEC;
1913 switch (sopt->sopt_name) {
1914 case IP_ADD_MEMBERSHIP:
1915 case IP_ADD_SOURCE_MEMBERSHIP: {
1916 struct ip_mreq_source mreqs;
1918 if (sopt->sopt_name == IP_ADD_MEMBERSHIP) {
1919 error = sooptcopyin(sopt, &mreqs,
1920 sizeof(struct ip_mreq),
1921 sizeof(struct ip_mreq));
1923 * Do argument switcharoo from ip_mreq into
1924 * ip_mreq_source to avoid using two instances.
1926 mreqs.imr_interface = mreqs.imr_sourceaddr;
1927 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
1928 } else if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
1929 error = sooptcopyin(sopt, &mreqs,
1930 sizeof(struct ip_mreq_source),
1931 sizeof(struct ip_mreq_source));
1936 gsa->sin.sin_family = AF_INET;
1937 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1938 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1940 if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
1941 ssa->sin.sin_family = AF_INET;
1942 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1943 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1946 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1949 ifp = inp_lookup_mcast_ifp(inp, &gsa->sin,
1950 mreqs.imr_interface);
1951 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
1952 __func__, inet_ntoa(mreqs.imr_interface), ifp);
1956 case MCAST_JOIN_GROUP:
1957 case MCAST_JOIN_SOURCE_GROUP:
1958 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1959 error = sooptcopyin(sopt, &gsr,
1960 sizeof(struct group_req),
1961 sizeof(struct group_req));
1962 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1963 error = sooptcopyin(sopt, &gsr,
1964 sizeof(struct group_source_req),
1965 sizeof(struct group_source_req));
1970 if (gsa->sin.sin_family != AF_INET ||
1971 gsa->sin.sin_len != sizeof(struct sockaddr_in))
1975 * Overwrite the port field if present, as the sockaddr
1976 * being copied in may be matched with a binary comparison.
1978 gsa->sin.sin_port = 0;
1979 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1980 if (ssa->sin.sin_family != AF_INET ||
1981 ssa->sin.sin_len != sizeof(struct sockaddr_in))
1983 ssa->sin.sin_port = 0;
1986 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1989 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1990 return (EADDRNOTAVAIL);
1991 ifp = ifnet_byindex(gsr.gsr_interface);
1995 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
1996 __func__, sopt->sopt_name);
1997 return (EOPNOTSUPP);
2001 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
2002 return (EADDRNOTAVAIL);
2004 imo = inp_findmoptions(inp);
2005 idx = imo_match_group(imo, ifp, &gsa->sa);
2009 inm = imo->imo_membership[idx];
2010 imf = &imo->imo_mfilters[idx];
2011 if (ssa->ss.ss_family != AF_UNSPEC) {
2013 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2014 * is an error. On an existing inclusive membership,
2015 * it just adds the source to the filter list.
2017 if (imf->imf_st[1] != MCAST_INCLUDE) {
2019 goto out_inp_locked;
2022 * Throw out duplicates.
2024 * XXX FIXME: This makes a naive assumption that
2025 * even if entries exist for *ssa in this imf,
2026 * they will be rejected as dupes, even if they
2027 * are not valid in the current mode (in-mode).
2029 * in_msource is transactioned just as for anything
2030 * else in SSM -- but note naive use of inm_graft()
2031 * below for allocating new filter entries.
2033 * This is only an issue if someone mixes the
2034 * full-state SSM API with the delta-based API,
2035 * which is discouraged in the relevant RFCs.
2037 lims = imo_match_source(imo, idx, &ssa->sa);
2038 if (lims != NULL /*&&
2039 lims->imsl_st[1] == MCAST_INCLUDE*/) {
2040 error = EADDRNOTAVAIL;
2041 goto out_inp_locked;
2045 * MCAST_JOIN_GROUP on an existing exclusive
2046 * membership is an error; return EADDRINUSE
2047 * to preserve 4.4BSD API idempotence, and
2048 * avoid tedious detour to code below.
2049 * NOTE: This is bending RFC 3678 a bit.
2051 * On an existing inclusive membership, this is also
2052 * an error; if you want to change filter mode,
2053 * you must use the userland API setsourcefilter().
2054 * XXX We don't reject this for imf in UNDEFINED
2055 * state at t1, because allocation of a filter
2056 * is atomic with allocation of a membership.
2059 if (imf->imf_st[1] == MCAST_EXCLUDE)
2061 goto out_inp_locked;
2066 * Begin state merge transaction at socket layer.
2068 INP_WLOCK_ASSERT(inp);
2071 if (imo->imo_num_memberships == imo->imo_max_memberships) {
2072 error = imo_grow(imo);
2074 goto out_inp_locked;
2077 * Allocate the new slot upfront so we can deal with
2078 * grafting the new source filter in same code path
2079 * as for join-source on existing membership.
2081 idx = imo->imo_num_memberships;
2082 imo->imo_membership[idx] = NULL;
2083 imo->imo_num_memberships++;
2084 KASSERT(imo->imo_mfilters != NULL,
2085 ("%s: imf_mfilters vector was not allocated", __func__));
2086 imf = &imo->imo_mfilters[idx];
2087 KASSERT(RB_EMPTY(&imf->imf_sources),
2088 ("%s: imf_sources not empty", __func__));
2092 * Graft new source into filter list for this inpcb's
2093 * membership of the group. The in_multi may not have
2094 * been allocated yet if this is a new membership, however,
2095 * the in_mfilter slot will be allocated and must be initialized.
2097 * Note: Grafting of exclusive mode filters doesn't happen
2099 * XXX: Should check for non-NULL lims (node exists but may
2100 * not be in-mode) for interop with full-state API.
2102 if (ssa->ss.ss_family != AF_UNSPEC) {
2103 /* Membership starts in IN mode */
2105 CTR1(KTR_IGMPV3, "%s: new join w/source", __func__);
2106 imf_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE);
2108 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
2110 lims = imf_graft(imf, MCAST_INCLUDE, &ssa->sin);
2112 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2118 /* No address specified; Membership starts in EX mode */
2120 CTR1(KTR_IGMPV3, "%s: new join w/o source", __func__);
2121 imf_init(imf, MCAST_UNDEFINED, MCAST_EXCLUDE);
2126 * Begin state merge transaction at IGMP layer.
2131 error = in_joingroup_locked(ifp, &gsa->sin.sin_addr, imf,
2134 CTR1(KTR_IGMPV3, "%s: in_joingroup_locked failed",
2139 imo->imo_membership[idx] = inm;
2141 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2142 error = inm_merge(inm, imf);
2144 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2146 goto out_in_multi_locked;
2148 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2149 error = igmp_change_state(inm);
2151 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2153 goto out_in_multi_locked;
2157 out_in_multi_locked:
2161 INP_WLOCK_ASSERT(inp);
2173 if (error && is_new) {
2174 imo->imo_membership[idx] = NULL;
2175 --imo->imo_num_memberships;
2184 * Leave an IPv4 multicast group on an inpcb, possibly with a source.
2187 inp_leave_group(struct inpcb *inp, struct sockopt *sopt)
2189 struct group_source_req gsr;
2190 struct ip_mreq_source mreqs;
2191 sockunion_t *gsa, *ssa;
2193 struct in_mfilter *imf;
2194 struct ip_moptions *imo;
2195 struct in_msource *ims;
2196 struct in_multi *inm;
2198 int error, is_final;
2204 memset(&gsr, 0, sizeof(struct group_source_req));
2205 gsa = (sockunion_t *)&gsr.gsr_group;
2206 gsa->ss.ss_family = AF_UNSPEC;
2207 ssa = (sockunion_t *)&gsr.gsr_source;
2208 ssa->ss.ss_family = AF_UNSPEC;
2210 switch (sopt->sopt_name) {
2211 case IP_DROP_MEMBERSHIP:
2212 case IP_DROP_SOURCE_MEMBERSHIP:
2213 if (sopt->sopt_name == IP_DROP_MEMBERSHIP) {
2214 error = sooptcopyin(sopt, &mreqs,
2215 sizeof(struct ip_mreq),
2216 sizeof(struct ip_mreq));
2218 * Swap interface and sourceaddr arguments,
2219 * as ip_mreq and ip_mreq_source are laid
2222 mreqs.imr_interface = mreqs.imr_sourceaddr;
2223 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
2224 } else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2225 error = sooptcopyin(sopt, &mreqs,
2226 sizeof(struct ip_mreq_source),
2227 sizeof(struct ip_mreq_source));
2232 gsa->sin.sin_family = AF_INET;
2233 gsa->sin.sin_len = sizeof(struct sockaddr_in);
2234 gsa->sin.sin_addr = mreqs.imr_multiaddr;
2236 if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2237 ssa->sin.sin_family = AF_INET;
2238 ssa->sin.sin_len = sizeof(struct sockaddr_in);
2239 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
2243 * Attempt to look up hinted ifp from interface address.
2244 * Fallthrough with null ifp iff lookup fails, to
2245 * preserve 4.4BSD mcast API idempotence.
2246 * XXX NOTE WELL: The RFC 3678 API is preferred because
2247 * using an IPv4 address as a key is racy.
2249 if (!in_nullhost(mreqs.imr_interface))
2250 INADDR_TO_IFP(mreqs.imr_interface, ifp);
2252 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p",
2253 __func__, inet_ntoa(mreqs.imr_interface), ifp);
2257 case MCAST_LEAVE_GROUP:
2258 case MCAST_LEAVE_SOURCE_GROUP:
2259 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2260 error = sooptcopyin(sopt, &gsr,
2261 sizeof(struct group_req),
2262 sizeof(struct group_req));
2263 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2264 error = sooptcopyin(sopt, &gsr,
2265 sizeof(struct group_source_req),
2266 sizeof(struct group_source_req));
2271 if (gsa->sin.sin_family != AF_INET ||
2272 gsa->sin.sin_len != sizeof(struct sockaddr_in))
2275 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2276 if (ssa->sin.sin_family != AF_INET ||
2277 ssa->sin.sin_len != sizeof(struct sockaddr_in))
2281 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
2282 return (EADDRNOTAVAIL);
2284 ifp = ifnet_byindex(gsr.gsr_interface);
2287 return (EADDRNOTAVAIL);
2291 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2292 __func__, sopt->sopt_name);
2293 return (EOPNOTSUPP);
2297 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2301 * Find the membership in the membership array.
2303 imo = inp_findmoptions(inp);
2304 idx = imo_match_group(imo, ifp, &gsa->sa);
2306 error = EADDRNOTAVAIL;
2307 goto out_inp_locked;
2309 inm = imo->imo_membership[idx];
2310 imf = &imo->imo_mfilters[idx];
2312 if (ssa->ss.ss_family != AF_UNSPEC)
2316 * Begin state merge transaction at socket layer.
2318 INP_WLOCK_ASSERT(inp);
2321 * If we were instructed only to leave a given source, do so.
2322 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2327 if (imf->imf_st[0] == MCAST_EXCLUDE) {
2328 error = EADDRNOTAVAIL;
2329 goto out_inp_locked;
2331 ims = imo_match_source(imo, idx, &ssa->sa);
2333 CTR3(KTR_IGMPV3, "%s: source %s %spresent", __func__,
2334 inet_ntoa(ssa->sin.sin_addr), "not ");
2335 error = EADDRNOTAVAIL;
2336 goto out_inp_locked;
2338 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
2339 error = imf_prune(imf, &ssa->sin);
2341 CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2343 goto out_inp_locked;
2348 * Begin state merge transaction at IGMP layer.
2354 * Give up the multicast address record to which
2355 * the membership points.
2357 (void)in_leavegroup_locked(inm, imf);
2359 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2360 error = inm_merge(inm, imf);
2362 CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2364 goto out_in_multi_locked;
2367 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2368 error = igmp_change_state(inm);
2370 CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2375 out_in_multi_locked:
2387 /* Remove the gap in the membership and filter array. */
2388 for (++idx; idx < imo->imo_num_memberships; ++idx) {
2389 imo->imo_membership[idx-1] = imo->imo_membership[idx];
2390 imo->imo_mfilters[idx-1] = imo->imo_mfilters[idx];
2392 imo->imo_num_memberships--;
2401 * Select the interface for transmitting IPv4 multicast datagrams.
2403 * Either an instance of struct in_addr or an instance of struct ip_mreqn
2404 * may be passed to this socket option. An address of INADDR_ANY or an
2405 * interface index of 0 is used to remove a previous selection.
2406 * When no interface is selected, one is chosen for every send.
2409 inp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2411 struct in_addr addr;
2412 struct ip_mreqn mreqn;
2414 struct ip_moptions *imo;
2417 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
2419 * An interface index was specified using the
2420 * Linux-derived ip_mreqn structure.
2422 error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn),
2423 sizeof(struct ip_mreqn));
2427 if (mreqn.imr_ifindex < 0 || V_if_index < mreqn.imr_ifindex)
2430 if (mreqn.imr_ifindex == 0) {
2433 ifp = ifnet_byindex(mreqn.imr_ifindex);
2435 return (EADDRNOTAVAIL);
2439 * An interface was specified by IPv4 address.
2440 * This is the traditional BSD usage.
2442 error = sooptcopyin(sopt, &addr, sizeof(struct in_addr),
2443 sizeof(struct in_addr));
2446 if (in_nullhost(addr)) {
2449 INADDR_TO_IFP(addr, ifp);
2451 return (EADDRNOTAVAIL);
2453 CTR3(KTR_IGMPV3, "%s: ifp = %p, addr = %s", __func__, ifp,
2457 /* Reject interfaces which do not support multicast. */
2458 if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0)
2459 return (EOPNOTSUPP);
2461 imo = inp_findmoptions(inp);
2462 imo->imo_multicast_ifp = ifp;
2463 imo->imo_multicast_addr.s_addr = INADDR_ANY;
2470 * Atomically set source filters on a socket for an IPv4 multicast group.
2472 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
2475 inp_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2477 struct __msfilterreq msfr;
2480 struct in_mfilter *imf;
2481 struct ip_moptions *imo;
2482 struct in_multi *inm;
2486 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2487 sizeof(struct __msfilterreq));
2491 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
2494 if ((msfr.msfr_fmode != MCAST_EXCLUDE &&
2495 msfr.msfr_fmode != MCAST_INCLUDE))
2498 if (msfr.msfr_group.ss_family != AF_INET ||
2499 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in))
2502 gsa = (sockunion_t *)&msfr.msfr_group;
2503 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2506 gsa->sin.sin_port = 0; /* ignore port */
2508 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
2509 return (EADDRNOTAVAIL);
2511 ifp = ifnet_byindex(msfr.msfr_ifindex);
2513 return (EADDRNOTAVAIL);
2516 * Take the INP write lock.
2517 * Check if this socket is a member of this group.
2519 imo = inp_findmoptions(inp);
2520 idx = imo_match_group(imo, ifp, &gsa->sa);
2521 if (idx == -1 || imo->imo_mfilters == NULL) {
2522 error = EADDRNOTAVAIL;
2523 goto out_inp_locked;
2525 inm = imo->imo_membership[idx];
2526 imf = &imo->imo_mfilters[idx];
2529 * Begin state merge transaction at socket layer.
2531 INP_WLOCK_ASSERT(inp);
2533 imf->imf_st[1] = msfr.msfr_fmode;
2536 * Apply any new source filters, if present.
2537 * Make a copy of the user-space source vector so
2538 * that we may copy them with a single copyin. This
2539 * allows us to deal with page faults up-front.
2541 if (msfr.msfr_nsrcs > 0) {
2542 struct in_msource *lims;
2543 struct sockaddr_in *psin;
2544 struct sockaddr_storage *kss, *pkss;
2549 CTR2(KTR_IGMPV3, "%s: loading %lu source list entries",
2550 __func__, (unsigned long)msfr.msfr_nsrcs);
2551 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2553 error = copyin(msfr.msfr_srcs, kss,
2554 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2563 * Mark all source filters as UNDEFINED at t1.
2564 * Restore new group filter mode, as imf_leave()
2565 * will set it to INCLUDE.
2568 imf->imf_st[1] = msfr.msfr_fmode;
2571 * Update socket layer filters at t1, lazy-allocating
2572 * new entries. This saves a bunch of memory at the
2573 * cost of one RB_FIND() per source entry; duplicate
2574 * entries in the msfr_nsrcs vector are ignored.
2575 * If we encounter an error, rollback transaction.
2577 * XXX This too could be replaced with a set-symmetric
2578 * difference like loop to avoid walking from root
2579 * every time, as the key space is common.
2581 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2582 psin = (struct sockaddr_in *)pkss;
2583 if (psin->sin_family != AF_INET) {
2584 error = EAFNOSUPPORT;
2587 if (psin->sin_len != sizeof(struct sockaddr_in)) {
2591 error = imf_get_source(imf, psin, &lims);
2594 lims->imsl_st[1] = imf->imf_st[1];
2600 goto out_imf_rollback;
2602 INP_WLOCK_ASSERT(inp);
2606 * Begin state merge transaction at IGMP layer.
2608 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2609 error = inm_merge(inm, imf);
2611 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
2612 goto out_in_multi_locked;
2615 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2616 error = igmp_change_state(inm);
2618 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
2620 out_in_multi_locked:
2638 * Set the IP multicast options in response to user setsockopt().
2640 * Many of the socket options handled in this function duplicate the
2641 * functionality of socket options in the regular unicast API. However,
2642 * it is not possible to merge the duplicate code, because the idempotence
2643 * of the IPv4 multicast part of the BSD Sockets API must be preserved;
2644 * the effects of these options must be treated as separate and distinct.
2646 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2647 * FUTURE: The IP_MULTICAST_VIF option may be eliminated if MROUTING
2648 * is refactored to no longer use vifs.
2651 inp_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2653 struct ip_moptions *imo;
2659 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2660 * or is a divert socket, reject it.
2662 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
2663 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2664 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
2665 return (EOPNOTSUPP);
2667 switch (sopt->sopt_name) {
2668 case IP_MULTICAST_VIF: {
2671 * Select a multicast VIF for transmission.
2672 * Only useful if multicast forwarding is active.
2674 if (legal_vif_num == NULL) {
2678 error = sooptcopyin(sopt, &vifi, sizeof(int), sizeof(int));
2681 if (!legal_vif_num(vifi) && (vifi != -1)) {
2685 imo = inp_findmoptions(inp);
2686 imo->imo_multicast_vif = vifi;
2691 case IP_MULTICAST_IF:
2692 error = inp_set_multicast_if(inp, sopt);
2695 case IP_MULTICAST_TTL: {
2699 * Set the IP time-to-live for outgoing multicast packets.
2700 * The original multicast API required a char argument,
2701 * which is inconsistent with the rest of the socket API.
2702 * We allow either a char or an int.
2704 if (sopt->sopt_valsize == sizeof(u_char)) {
2705 error = sooptcopyin(sopt, &ttl, sizeof(u_char),
2712 error = sooptcopyin(sopt, &ittl, sizeof(u_int),
2722 imo = inp_findmoptions(inp);
2723 imo->imo_multicast_ttl = ttl;
2728 case IP_MULTICAST_LOOP: {
2732 * Set the loopback flag for outgoing multicast packets.
2733 * Must be zero or one. The original multicast API required a
2734 * char argument, which is inconsistent with the rest
2735 * of the socket API. We allow either a char or an int.
2737 if (sopt->sopt_valsize == sizeof(u_char)) {
2738 error = sooptcopyin(sopt, &loop, sizeof(u_char),
2745 error = sooptcopyin(sopt, &iloop, sizeof(u_int),
2749 loop = (u_char)iloop;
2751 imo = inp_findmoptions(inp);
2752 imo->imo_multicast_loop = !!loop;
2757 case IP_ADD_MEMBERSHIP:
2758 case IP_ADD_SOURCE_MEMBERSHIP:
2759 case MCAST_JOIN_GROUP:
2760 case MCAST_JOIN_SOURCE_GROUP:
2761 error = inp_join_group(inp, sopt);
2764 case IP_DROP_MEMBERSHIP:
2765 case IP_DROP_SOURCE_MEMBERSHIP:
2766 case MCAST_LEAVE_GROUP:
2767 case MCAST_LEAVE_SOURCE_GROUP:
2768 error = inp_leave_group(inp, sopt);
2771 case IP_BLOCK_SOURCE:
2772 case IP_UNBLOCK_SOURCE:
2773 case MCAST_BLOCK_SOURCE:
2774 case MCAST_UNBLOCK_SOURCE:
2775 error = inp_block_unblock_source(inp, sopt);
2779 error = inp_set_source_filters(inp, sopt);
2787 INP_UNLOCK_ASSERT(inp);
2793 * Expose IGMP's multicast filter mode and source list(s) to userland,
2794 * keyed by (ifindex, group).
2795 * The filter mode is written out as a uint32_t, followed by
2796 * 0..n of struct in_addr.
2797 * For use by ifmcstat(8).
2798 * SMPng: NOTE: unlocked read of ifindex space.
2801 sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS)
2803 struct in_addr src, group;
2805 struct ifmultiaddr *ifma;
2806 struct in_multi *inm;
2807 struct ip_msource *ims;
2811 uint32_t fmode, ifindex;
2816 if (req->newptr != NULL)
2823 if (ifindex <= 0 || ifindex > V_if_index) {
2824 CTR2(KTR_IGMPV3, "%s: ifindex %u out of range",
2829 group.s_addr = name[1];
2830 if (!IN_MULTICAST(ntohl(group.s_addr))) {
2831 CTR2(KTR_IGMPV3, "%s: group %s is not multicast",
2832 __func__, inet_ntoa(group));
2836 ifp = ifnet_byindex(ifindex);
2838 CTR2(KTR_IGMPV3, "%s: no ifp for ifindex %u",
2843 retval = sysctl_wire_old_buffer(req,
2844 sizeof(uint32_t) + (in_mcast_maxgrpsrc * sizeof(struct in_addr)));
2851 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2852 if (ifma->ifma_addr->sa_family != AF_INET ||
2853 ifma->ifma_protospec == NULL)
2855 inm = (struct in_multi *)ifma->ifma_protospec;
2856 if (!in_hosteq(inm->inm_addr, group))
2858 fmode = inm->inm_st[1].iss_fmode;
2859 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2862 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
2865 ina.s_addr = htonl(ims->ims_haddr);
2866 CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2870 * Only copy-out sources which are in-mode.
2872 if (fmode != ims_get_mode(inm, ims, 1)) {
2873 CTR1(KTR_IGMPV3, "%s: skip non-in-mode",
2877 src.s_addr = htonl(ims->ims_haddr);
2878 retval = SYSCTL_OUT(req, &src, sizeof(struct in_addr));
2883 IF_ADDR_RUNLOCK(ifp);
2892 static const char *inm_modestrs[] = { "un", "in", "ex" };
2895 inm_mode_str(const int mode)
2898 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2899 return (inm_modestrs[mode]);
2903 static const char *inm_statestrs[] = {
2916 inm_state_str(const int state)
2919 if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
2920 return (inm_statestrs[state]);
2925 * Dump an in_multi structure to the console.
2928 inm_print(const struct in_multi *inm)
2932 if ((ktr_mask & KTR_IGMPV3) == 0)
2935 printf("%s: --- begin inm %p ---\n", __func__, inm);
2936 printf("addr %s ifp %p(%s) ifma %p\n",
2937 inet_ntoa(inm->inm_addr),
2939 inm->inm_ifp->if_xname,
2941 printf("timer %u state %s refcount %u scq.len %u\n",
2943 inm_state_str(inm->inm_state),
2945 inm->inm_scq.ifq_len);
2946 printf("igi %p nsrc %lu sctimer %u scrv %u\n",
2951 for (t = 0; t < 2; t++) {
2952 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
2953 inm_mode_str(inm->inm_st[t].iss_fmode),
2954 inm->inm_st[t].iss_asm,
2955 inm->inm_st[t].iss_ex,
2956 inm->inm_st[t].iss_in,
2957 inm->inm_st[t].iss_rec);
2959 printf("%s: --- end inm %p ---\n", __func__, inm);
2965 inm_print(const struct in_multi *inm)
2972 RB_GENERATE(ip_msource_tree, ip_msource, ims_link, ip_msource_cmp);