2 * Copyright (c) 2009 Bruce Simpson.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior written
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * IPv6 multicast socket, group, and socket option processing module.
32 * Normative references: RFC 2292, RFC 3492, RFC 3542, RFC 3678, RFC 3810.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_inet6.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/protosw.h>
49 #include <sys/sysctl.h>
55 #include <net/if_var.h>
56 #include <net/if_dl.h>
57 #include <net/route.h>
60 #include <netinet/in.h>
61 #include <netinet/in_var.h>
62 #include <netinet6/in6_var.h>
63 #include <netinet/ip6.h>
64 #include <netinet/icmp6.h>
65 #include <netinet6/ip6_var.h>
66 #include <netinet/in_pcb.h>
67 #include <netinet/tcp_var.h>
68 #include <netinet6/nd6.h>
69 #include <netinet6/mld6_var.h>
70 #include <netinet6/scope6_var.h>
73 #define KTR_MLD KTR_INET6
76 #ifndef __SOCKUNION_DECLARED
78 struct sockaddr_storage ss;
80 struct sockaddr_dl sdl;
81 struct sockaddr_in6 sin6;
83 typedef union sockunion sockunion_t;
84 #define __SOCKUNION_DECLARED
85 #endif /* __SOCKUNION_DECLARED */
87 static MALLOC_DEFINE(M_IN6MFILTER, "in6_mfilter",
88 "IPv6 multicast PCB-layer source filter");
89 static MALLOC_DEFINE(M_IP6MADDR, "in6_multi", "IPv6 multicast group");
90 static MALLOC_DEFINE(M_IP6MOPTS, "ip6_moptions", "IPv6 multicast options");
91 static MALLOC_DEFINE(M_IP6MSOURCE, "ip6_msource",
92 "IPv6 multicast MLD-layer source filter");
94 RB_GENERATE(ip6_msource_tree, ip6_msource, im6s_link, ip6_msource_cmp);
98 * - Lock order is: Giant, INP_WLOCK, IN6_MULTI_LOCK, MLD_LOCK, IF_ADDR_LOCK.
99 * - The IF_ADDR_LOCK is implicitly taken by in6m_lookup() earlier, however
100 * it can be taken by code in net/if.c also.
101 * - ip6_moptions and in6_mfilter are covered by the INP_WLOCK.
103 * struct in6_multi is covered by IN6_MULTI_LOCK. There isn't strictly
104 * any need for in6_multi itself to be virtualized -- it is bound to an ifp
105 * anyway no matter what happens.
107 struct mtx in6_multi_mtx;
108 MTX_SYSINIT(in6_multi_mtx, &in6_multi_mtx, "in6_multi_mtx", MTX_DEF);
110 static void im6f_commit(struct in6_mfilter *);
111 static int im6f_get_source(struct in6_mfilter *imf,
112 const struct sockaddr_in6 *psin,
113 struct in6_msource **);
114 static struct in6_msource *
115 im6f_graft(struct in6_mfilter *, const uint8_t,
116 const struct sockaddr_in6 *);
117 static void im6f_leave(struct in6_mfilter *);
118 static int im6f_prune(struct in6_mfilter *, const struct sockaddr_in6 *);
119 static void im6f_purge(struct in6_mfilter *);
120 static void im6f_rollback(struct in6_mfilter *);
121 static void im6f_reap(struct in6_mfilter *);
122 static int im6o_grow(struct ip6_moptions *);
123 static size_t im6o_match_group(const struct ip6_moptions *,
124 const struct ifnet *, const struct sockaddr *);
125 static struct in6_msource *
126 im6o_match_source(const struct ip6_moptions *, const size_t,
127 const struct sockaddr *);
128 static void im6s_merge(struct ip6_msource *ims,
129 const struct in6_msource *lims, const int rollback);
130 static int in6_mc_get(struct ifnet *, const struct in6_addr *,
131 struct in6_multi **);
132 static int in6m_get_source(struct in6_multi *inm,
133 const struct in6_addr *addr, const int noalloc,
134 struct ip6_msource **pims);
136 static int in6m_is_ifp_detached(const struct in6_multi *);
138 static int in6m_merge(struct in6_multi *, /*const*/ struct in6_mfilter *);
139 static void in6m_purge(struct in6_multi *);
140 static void in6m_reap(struct in6_multi *);
141 static struct ip6_moptions *
142 in6p_findmoptions(struct inpcb *);
143 static int in6p_get_source_filters(struct inpcb *, struct sockopt *);
144 static int in6p_join_group(struct inpcb *, struct sockopt *);
145 static int in6p_leave_group(struct inpcb *, struct sockopt *);
146 static struct ifnet *
147 in6p_lookup_mcast_ifp(const struct inpcb *,
148 const struct sockaddr_in6 *);
149 static int in6p_block_unblock_source(struct inpcb *, struct sockopt *);
150 static int in6p_set_multicast_if(struct inpcb *, struct sockopt *);
151 static int in6p_set_source_filters(struct inpcb *, struct sockopt *);
152 static int sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS);
154 SYSCTL_DECL(_net_inet6_ip6); /* XXX Not in any common header. */
156 static SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, mcast, CTLFLAG_RW, 0,
159 static u_long in6_mcast_maxgrpsrc = IPV6_MAX_GROUP_SRC_FILTER;
160 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxgrpsrc,
161 CTLFLAG_RW | CTLFLAG_TUN, &in6_mcast_maxgrpsrc, 0,
162 "Max source filters per group");
163 TUNABLE_ULONG("net.inet6.ip6.mcast.maxgrpsrc", &in6_mcast_maxgrpsrc);
165 static u_long in6_mcast_maxsocksrc = IPV6_MAX_SOCK_SRC_FILTER;
166 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxsocksrc,
167 CTLFLAG_RW | CTLFLAG_TUN, &in6_mcast_maxsocksrc, 0,
168 "Max source filters per socket");
169 TUNABLE_ULONG("net.inet6.ip6.mcast.maxsocksrc", &in6_mcast_maxsocksrc);
171 /* TODO Virtualize this switch. */
172 int in6_mcast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
173 SYSCTL_INT(_net_inet6_ip6_mcast, OID_AUTO, loop, CTLFLAG_RW | CTLFLAG_TUN,
174 &in6_mcast_loop, 0, "Loopback multicast datagrams by default");
175 TUNABLE_INT("net.inet6.ip6.mcast.loop", &in6_mcast_loop);
177 static SYSCTL_NODE(_net_inet6_ip6_mcast, OID_AUTO, filters,
178 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip6_mcast_filters,
179 "Per-interface stack-wide source filters");
183 * Inline function which wraps assertions for a valid ifp.
184 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
188 in6m_is_ifp_detached(const struct in6_multi *inm)
192 KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__));
193 ifp = inm->in6m_ifma->ifma_ifp;
196 * Sanity check that network-layer notion of ifp is the
197 * same as that of link-layer.
199 KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__));
202 return (ifp == NULL);
207 * Initialize an in6_mfilter structure to a known state at t0, t1
208 * with an empty source filter list.
211 im6f_init(struct in6_mfilter *imf, const int st0, const int st1)
213 memset(imf, 0, sizeof(struct in6_mfilter));
214 RB_INIT(&imf->im6f_sources);
215 imf->im6f_st[0] = st0;
216 imf->im6f_st[1] = st1;
220 * Resize the ip6_moptions vector to the next power-of-two minus 1.
221 * May be called with locks held; do not sleep.
224 im6o_grow(struct ip6_moptions *imo)
226 struct in6_multi **nmships;
227 struct in6_multi **omships;
228 struct in6_mfilter *nmfilters;
229 struct in6_mfilter *omfilters;
236 omships = imo->im6o_membership;
237 omfilters = imo->im6o_mfilters;
238 oldmax = imo->im6o_max_memberships;
239 newmax = ((oldmax + 1) * 2) - 1;
241 if (newmax <= IPV6_MAX_MEMBERSHIPS) {
242 nmships = (struct in6_multi **)realloc(omships,
243 sizeof(struct in6_multi *) * newmax, M_IP6MOPTS, M_NOWAIT);
244 nmfilters = (struct in6_mfilter *)realloc(omfilters,
245 sizeof(struct in6_mfilter) * newmax, M_IN6MFILTER,
247 if (nmships != NULL && nmfilters != NULL) {
248 /* Initialize newly allocated source filter heads. */
249 for (idx = oldmax; idx < newmax; idx++) {
250 im6f_init(&nmfilters[idx], MCAST_UNDEFINED,
253 imo->im6o_max_memberships = newmax;
254 imo->im6o_membership = nmships;
255 imo->im6o_mfilters = nmfilters;
259 if (nmships == NULL || nmfilters == NULL) {
261 free(nmships, M_IP6MOPTS);
262 if (nmfilters != NULL)
263 free(nmfilters, M_IN6MFILTER);
264 return (ETOOMANYREFS);
271 * Find an IPv6 multicast group entry for this ip6_moptions instance
272 * which matches the specified group, and optionally an interface.
273 * Return its index into the array, or -1 if not found.
276 im6o_match_group(const struct ip6_moptions *imo, const struct ifnet *ifp,
277 const struct sockaddr *group)
279 const struct sockaddr_in6 *gsin6;
280 struct in6_multi **pinm;
284 gsin6 = (const struct sockaddr_in6 *)group;
286 /* The im6o_membership array may be lazy allocated. */
287 if (imo->im6o_membership == NULL || imo->im6o_num_memberships == 0)
290 nmships = imo->im6o_num_memberships;
291 pinm = &imo->im6o_membership[0];
292 for (idx = 0; idx < nmships; idx++, pinm++) {
295 if ((ifp == NULL || ((*pinm)->in6m_ifp == ifp)) &&
296 IN6_ARE_ADDR_EQUAL(&(*pinm)->in6m_addr,
297 &gsin6->sin6_addr)) {
308 * Find an IPv6 multicast source entry for this imo which matches
309 * the given group index for this socket, and source address.
311 * XXX TODO: The scope ID, if present in src, is stripped before
312 * any comparison. We SHOULD enforce scope/zone checks where the source
313 * filter entry has a link scope.
315 * NOTE: This does not check if the entry is in-mode, merely if
316 * it exists, which may not be the desired behaviour.
318 static struct in6_msource *
319 im6o_match_source(const struct ip6_moptions *imo, const size_t gidx,
320 const struct sockaddr *src)
322 struct ip6_msource find;
323 struct in6_mfilter *imf;
324 struct ip6_msource *ims;
325 const sockunion_t *psa;
327 KASSERT(src->sa_family == AF_INET6, ("%s: !AF_INET6", __func__));
328 KASSERT(gidx != -1 && gidx < imo->im6o_num_memberships,
329 ("%s: invalid index %d\n", __func__, (int)gidx));
331 /* The im6o_mfilters array may be lazy allocated. */
332 if (imo->im6o_mfilters == NULL)
334 imf = &imo->im6o_mfilters[gidx];
336 psa = (const sockunion_t *)src;
337 find.im6s_addr = psa->sin6.sin6_addr;
338 in6_clearscope(&find.im6s_addr); /* XXX */
339 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
341 return ((struct in6_msource *)ims);
345 * Perform filtering for multicast datagrams on a socket by group and source.
347 * Returns 0 if a datagram should be allowed through, or various error codes
348 * if the socket was not a member of the group, or the source was muted, etc.
351 im6o_mc_filter(const struct ip6_moptions *imo, const struct ifnet *ifp,
352 const struct sockaddr *group, const struct sockaddr *src)
355 struct in6_msource *ims;
358 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
360 gidx = im6o_match_group(imo, ifp, group);
362 return (MCAST_NOTGMEMBER);
365 * Check if the source was included in an (S,G) join.
366 * Allow reception on exclusive memberships by default,
367 * reject reception on inclusive memberships by default.
368 * Exclude source only if an in-mode exclude filter exists.
369 * Include source only if an in-mode include filter exists.
370 * NOTE: We are comparing group state here at MLD t1 (now)
371 * with socket-layer t0 (since last downcall).
373 mode = imo->im6o_mfilters[gidx].im6f_st[1];
374 ims = im6o_match_source(imo, gidx, src);
376 if ((ims == NULL && mode == MCAST_INCLUDE) ||
377 (ims != NULL && ims->im6sl_st[0] != mode))
378 return (MCAST_NOTSMEMBER);
384 * Find and return a reference to an in6_multi record for (ifp, group),
385 * and bump its reference count.
386 * If one does not exist, try to allocate it, and update link-layer multicast
387 * filters on ifp to listen for group.
388 * Assumes the IN6_MULTI lock is held across the call.
389 * Return 0 if successful, otherwise return an appropriate error code.
392 in6_mc_get(struct ifnet *ifp, const struct in6_addr *group,
393 struct in6_multi **pinm)
395 struct sockaddr_in6 gsin6;
396 struct ifmultiaddr *ifma;
397 struct in6_multi *inm;
403 * XXX: Accesses to ifma_protospec must be covered by IF_ADDR_LOCK;
404 * if_addmulti() takes this mutex itself, so we must drop and
405 * re-acquire around the call.
407 IN6_MULTI_LOCK_ASSERT();
410 inm = in6m_lookup_locked(ifp, group);
413 * If we already joined this group, just bump the
414 * refcount and return it.
416 KASSERT(inm->in6m_refcount >= 1,
417 ("%s: bad refcount %d", __func__, inm->in6m_refcount));
418 ++inm->in6m_refcount;
423 memset(&gsin6, 0, sizeof(gsin6));
424 gsin6.sin6_family = AF_INET6;
425 gsin6.sin6_len = sizeof(struct sockaddr_in6);
426 gsin6.sin6_addr = *group;
429 * Check if a link-layer group is already associated
430 * with this network-layer group on the given ifnet.
432 IF_ADDR_WUNLOCK(ifp);
433 error = if_addmulti(ifp, (struct sockaddr *)&gsin6, &ifma);
439 * If something other than netinet6 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 in6_multi *)ifma->ifma_protospec;
448 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
450 KASSERT(ifma->ifma_addr->sa_family == AF_INET6,
451 ("%s: ifma not AF_INET6", __func__));
452 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
453 if (inm->in6m_ifma != ifma || inm->in6m_ifp != ifp ||
454 !IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, group))
455 panic("%s: ifma %p is inconsistent with %p (%p)",
456 __func__, ifma, inm, group);
458 ++inm->in6m_refcount;
463 IF_ADDR_WLOCK_ASSERT(ifp);
466 * A new in6_multi record is needed; allocate and initialize it.
467 * We DO NOT perform an MLD join as the in6_ layer may need to
468 * push an initial source list down to MLD to support SSM.
470 * The initial source filter state is INCLUDE, {} as per the RFC.
471 * Pending state-changes per group are subject to a bounds check.
473 inm = malloc(sizeof(*inm), M_IP6MADDR, M_NOWAIT | M_ZERO);
475 if_delmulti_ifma(ifma);
479 inm->in6m_addr = *group;
481 inm->in6m_mli = MLD_IFINFO(ifp);
482 inm->in6m_ifma = ifma;
483 inm->in6m_refcount = 1;
484 inm->in6m_state = MLD_NOT_MEMBER;
485 IFQ_SET_MAXLEN(&inm->in6m_scq, MLD_MAX_STATE_CHANGES);
487 inm->in6m_st[0].iss_fmode = MCAST_UNDEFINED;
488 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
489 RB_INIT(&inm->in6m_srcs);
491 ifma->ifma_protospec = inm;
495 IF_ADDR_WUNLOCK(ifp);
500 * Drop a reference to an in6_multi record.
502 * If the refcount drops to 0, free the in6_multi record and
503 * delete the underlying link-layer membership.
506 in6m_release_locked(struct in6_multi *inm)
508 struct ifmultiaddr *ifma;
510 IN6_MULTI_LOCK_ASSERT();
512 CTR2(KTR_MLD, "%s: refcount is %d", __func__, inm->in6m_refcount);
514 if (--inm->in6m_refcount > 0) {
515 CTR2(KTR_MLD, "%s: refcount is now %d", __func__,
520 CTR2(KTR_MLD, "%s: freeing inm %p", __func__, inm);
522 ifma = inm->in6m_ifma;
524 /* XXX this access is not covered by IF_ADDR_LOCK */
525 CTR2(KTR_MLD, "%s: purging ifma %p", __func__, ifma);
526 KASSERT(ifma->ifma_protospec == inm,
527 ("%s: ifma_protospec != inm", __func__));
528 ifma->ifma_protospec = NULL;
532 free(inm, M_IP6MADDR);
534 if_delmulti_ifma(ifma);
538 * Clear recorded source entries for a group.
539 * Used by the MLD code. Caller must hold the IN6_MULTI lock.
540 * FIXME: Should reap.
543 in6m_clear_recorded(struct in6_multi *inm)
545 struct ip6_msource *ims;
547 IN6_MULTI_LOCK_ASSERT();
549 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
552 --inm->in6m_st[1].iss_rec;
555 KASSERT(inm->in6m_st[1].iss_rec == 0,
556 ("%s: iss_rec %d not 0", __func__, inm->in6m_st[1].iss_rec));
560 * Record a source as pending for a Source-Group MLDv2 query.
561 * This lives here as it modifies the shared tree.
563 * inm is the group descriptor.
564 * naddr is the address of the source to record in network-byte order.
566 * If the net.inet6.mld.sgalloc sysctl is non-zero, we will
567 * lazy-allocate a source node in response to an SG query.
568 * Otherwise, no allocation is performed. This saves some memory
569 * with the trade-off that the source will not be reported to the
570 * router if joined in the window between the query response and
571 * the group actually being joined on the local host.
573 * VIMAGE: XXX: Currently the mld_sgalloc feature has been removed.
574 * This turns off the allocation of a recorded source entry if
575 * the group has not been joined.
577 * Return 0 if the source didn't exist or was already marked as recorded.
578 * Return 1 if the source was marked as recorded by this function.
579 * Return <0 if any error occured (negated errno code).
582 in6m_record_source(struct in6_multi *inm, const struct in6_addr *addr)
584 struct ip6_msource find;
585 struct ip6_msource *ims, *nims;
587 IN6_MULTI_LOCK_ASSERT();
589 find.im6s_addr = *addr;
590 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
591 if (ims && ims->im6s_stp)
594 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
596 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
600 nims->im6s_addr = find.im6s_addr;
601 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
607 * Mark the source as recorded and update the recorded
611 ++inm->in6m_st[1].iss_rec;
617 * Return a pointer to an in6_msource owned by an in6_mfilter,
618 * given its source address.
619 * Lazy-allocate if needed. If this is a new entry its filter state is
622 * imf is the filter set being modified.
623 * addr is the source address.
625 * SMPng: May be called with locks held; malloc must not block.
628 im6f_get_source(struct in6_mfilter *imf, const struct sockaddr_in6 *psin,
629 struct in6_msource **plims)
631 struct ip6_msource find;
632 struct ip6_msource *ims, *nims;
633 struct in6_msource *lims;
640 find.im6s_addr = psin->sin6_addr;
641 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
642 lims = (struct in6_msource *)ims;
644 if (imf->im6f_nsrc == in6_mcast_maxsocksrc)
646 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
650 lims = (struct in6_msource *)nims;
651 lims->im6s_addr = find.im6s_addr;
652 lims->im6sl_st[0] = MCAST_UNDEFINED;
653 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
663 * Graft a source entry into an existing socket-layer filter set,
664 * maintaining any required invariants and checking allocations.
666 * The source is marked as being in the new filter mode at t1.
668 * Return the pointer to the new node, otherwise return NULL.
670 static struct in6_msource *
671 im6f_graft(struct in6_mfilter *imf, const uint8_t st1,
672 const struct sockaddr_in6 *psin)
674 struct ip6_msource *nims;
675 struct in6_msource *lims;
677 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
681 lims = (struct in6_msource *)nims;
682 lims->im6s_addr = psin->sin6_addr;
683 lims->im6sl_st[0] = MCAST_UNDEFINED;
684 lims->im6sl_st[1] = st1;
685 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
692 * Prune a source entry from an existing socket-layer filter set,
693 * maintaining any required invariants and checking allocations.
695 * The source is marked as being left at t1, it is not freed.
697 * Return 0 if no error occurred, otherwise return an errno value.
700 im6f_prune(struct in6_mfilter *imf, const struct sockaddr_in6 *psin)
702 struct ip6_msource find;
703 struct ip6_msource *ims;
704 struct in6_msource *lims;
706 find.im6s_addr = psin->sin6_addr;
707 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
710 lims = (struct in6_msource *)ims;
711 lims->im6sl_st[1] = MCAST_UNDEFINED;
716 * Revert socket-layer filter set deltas at t1 to t0 state.
719 im6f_rollback(struct in6_mfilter *imf)
721 struct ip6_msource *ims, *tims;
722 struct in6_msource *lims;
724 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
725 lims = (struct in6_msource *)ims;
726 if (lims->im6sl_st[0] == lims->im6sl_st[1]) {
727 /* no change at t1 */
729 } else if (lims->im6sl_st[0] != MCAST_UNDEFINED) {
730 /* revert change to existing source at t1 */
731 lims->im6sl_st[1] = lims->im6sl_st[0];
733 /* revert source added t1 */
734 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
735 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
736 free(ims, M_IN6MFILTER);
740 imf->im6f_st[1] = imf->im6f_st[0];
744 * Mark socket-layer filter set as INCLUDE {} at t1.
747 im6f_leave(struct in6_mfilter *imf)
749 struct ip6_msource *ims;
750 struct in6_msource *lims;
752 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
753 lims = (struct in6_msource *)ims;
754 lims->im6sl_st[1] = MCAST_UNDEFINED;
756 imf->im6f_st[1] = MCAST_INCLUDE;
760 * Mark socket-layer filter set deltas as committed.
763 im6f_commit(struct in6_mfilter *imf)
765 struct ip6_msource *ims;
766 struct in6_msource *lims;
768 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
769 lims = (struct in6_msource *)ims;
770 lims->im6sl_st[0] = lims->im6sl_st[1];
772 imf->im6f_st[0] = imf->im6f_st[1];
776 * Reap unreferenced sources from socket-layer filter set.
779 im6f_reap(struct in6_mfilter *imf)
781 struct ip6_msource *ims, *tims;
782 struct in6_msource *lims;
784 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
785 lims = (struct in6_msource *)ims;
786 if ((lims->im6sl_st[0] == MCAST_UNDEFINED) &&
787 (lims->im6sl_st[1] == MCAST_UNDEFINED)) {
788 CTR2(KTR_MLD, "%s: free lims %p", __func__, ims);
789 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
790 free(ims, M_IN6MFILTER);
797 * Purge socket-layer filter set.
800 im6f_purge(struct in6_mfilter *imf)
802 struct ip6_msource *ims, *tims;
804 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
805 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
806 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
807 free(ims, M_IN6MFILTER);
810 imf->im6f_st[0] = imf->im6f_st[1] = MCAST_UNDEFINED;
811 KASSERT(RB_EMPTY(&imf->im6f_sources),
812 ("%s: im6f_sources not empty", __func__));
816 * Look up a source filter entry for a multicast group.
818 * inm is the group descriptor to work with.
819 * addr is the IPv6 address to look up.
820 * noalloc may be non-zero to suppress allocation of sources.
821 * *pims will be set to the address of the retrieved or allocated source.
823 * SMPng: NOTE: may be called with locks held.
824 * Return 0 if successful, otherwise return a non-zero error code.
827 in6m_get_source(struct in6_multi *inm, const struct in6_addr *addr,
828 const int noalloc, struct ip6_msource **pims)
830 struct ip6_msource find;
831 struct ip6_msource *ims, *nims;
833 char ip6tbuf[INET6_ADDRSTRLEN];
836 find.im6s_addr = *addr;
837 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
838 if (ims == NULL && !noalloc) {
839 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
841 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
845 nims->im6s_addr = *addr;
846 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
849 CTR3(KTR_MLD, "%s: allocated %s as %p", __func__,
850 ip6_sprintf(ip6tbuf, addr), ims);
858 * Merge socket-layer source into MLD-layer source.
859 * If rollback is non-zero, perform the inverse of the merge.
862 im6s_merge(struct ip6_msource *ims, const struct in6_msource *lims,
865 int n = rollback ? -1 : 1;
867 char ip6tbuf[INET6_ADDRSTRLEN];
869 ip6_sprintf(ip6tbuf, &lims->im6s_addr);
872 if (lims->im6sl_st[0] == MCAST_EXCLUDE) {
873 CTR3(KTR_MLD, "%s: t1 ex -= %d on %s", __func__, n, ip6tbuf);
874 ims->im6s_st[1].ex -= n;
875 } else if (lims->im6sl_st[0] == MCAST_INCLUDE) {
876 CTR3(KTR_MLD, "%s: t1 in -= %d on %s", __func__, n, ip6tbuf);
877 ims->im6s_st[1].in -= n;
880 if (lims->im6sl_st[1] == MCAST_EXCLUDE) {
881 CTR3(KTR_MLD, "%s: t1 ex += %d on %s", __func__, n, ip6tbuf);
882 ims->im6s_st[1].ex += n;
883 } else if (lims->im6sl_st[1] == MCAST_INCLUDE) {
884 CTR3(KTR_MLD, "%s: t1 in += %d on %s", __func__, n, ip6tbuf);
885 ims->im6s_st[1].in += n;
890 * Atomically update the global in6_multi state, when a membership's
891 * filter list is being updated in any way.
893 * imf is the per-inpcb-membership group filter pointer.
894 * A fake imf may be passed for in-kernel consumers.
896 * XXX This is a candidate for a set-symmetric-difference style loop
897 * which would eliminate the repeated lookup from root of ims nodes,
898 * as they share the same key space.
900 * If any error occurred this function will back out of refcounts
901 * and return a non-zero value.
904 in6m_merge(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
906 struct ip6_msource *ims, *nims;
907 struct in6_msource *lims;
916 * Update the source filters first, as this may fail.
917 * Maintain count of in-mode filters at t0, t1. These are
918 * used to work out if we transition into ASM mode or not.
919 * Maintain a count of source filters whose state was
920 * actually modified by this operation.
922 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
923 lims = (struct in6_msource *)ims;
924 if (lims->im6sl_st[0] == imf->im6f_st[0]) nsrc0++;
925 if (lims->im6sl_st[1] == imf->im6f_st[1]) nsrc1++;
926 if (lims->im6sl_st[0] == lims->im6sl_st[1]) continue;
927 error = in6m_get_source(inm, &lims->im6s_addr, 0, &nims);
931 im6s_merge(nims, lims, 0);
934 struct ip6_msource *bims;
936 RB_FOREACH_REVERSE_FROM(ims, ip6_msource_tree, nims) {
937 lims = (struct in6_msource *)ims;
938 if (lims->im6sl_st[0] == lims->im6sl_st[1])
940 (void)in6m_get_source(inm, &lims->im6s_addr, 1, &bims);
943 im6s_merge(bims, lims, 1);
948 CTR3(KTR_MLD, "%s: imf filters in-mode: %d at t0, %d at t1",
949 __func__, nsrc0, nsrc1);
951 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
952 if (imf->im6f_st[0] == imf->im6f_st[1] &&
953 imf->im6f_st[1] == MCAST_INCLUDE) {
955 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
956 --inm->in6m_st[1].iss_in;
960 /* Handle filter mode transition on socket. */
961 if (imf->im6f_st[0] != imf->im6f_st[1]) {
962 CTR3(KTR_MLD, "%s: imf transition %d to %d",
963 __func__, imf->im6f_st[0], imf->im6f_st[1]);
965 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
966 CTR1(KTR_MLD, "%s: --ex on inm at t1", __func__);
967 --inm->in6m_st[1].iss_ex;
968 } else if (imf->im6f_st[0] == MCAST_INCLUDE) {
969 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
970 --inm->in6m_st[1].iss_in;
973 if (imf->im6f_st[1] == MCAST_EXCLUDE) {
974 CTR1(KTR_MLD, "%s: ex++ on inm at t1", __func__);
975 inm->in6m_st[1].iss_ex++;
976 } else if (imf->im6f_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
977 CTR1(KTR_MLD, "%s: in++ on inm at t1", __func__);
978 inm->in6m_st[1].iss_in++;
983 * Track inm filter state in terms of listener counts.
984 * If there are any exclusive listeners, stack-wide
985 * membership is exclusive.
986 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
987 * If no listeners remain, state is undefined at t1,
988 * and the MLD lifecycle for this group should finish.
990 if (inm->in6m_st[1].iss_ex > 0) {
991 CTR1(KTR_MLD, "%s: transition to EX", __func__);
992 inm->in6m_st[1].iss_fmode = MCAST_EXCLUDE;
993 } else if (inm->in6m_st[1].iss_in > 0) {
994 CTR1(KTR_MLD, "%s: transition to IN", __func__);
995 inm->in6m_st[1].iss_fmode = MCAST_INCLUDE;
997 CTR1(KTR_MLD, "%s: transition to UNDEF", __func__);
998 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
1001 /* Decrement ASM listener count on transition out of ASM mode. */
1002 if (imf->im6f_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1003 if ((imf->im6f_st[1] != MCAST_EXCLUDE) ||
1004 (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 > 0))
1005 CTR1(KTR_MLD, "%s: --asm on inm at t1", __func__);
1006 --inm->in6m_st[1].iss_asm;
1009 /* Increment ASM listener count on transition to ASM mode. */
1010 if (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1011 CTR1(KTR_MLD, "%s: asm++ on inm at t1", __func__);
1012 inm->in6m_st[1].iss_asm++;
1015 CTR3(KTR_MLD, "%s: merged imf %p to inm %p", __func__, imf, inm);
1020 CTR1(KTR_MLD, "%s: sources changed; reaping", __func__);
1027 * Mark an in6_multi's filter set deltas as committed.
1028 * Called by MLD after a state change has been enqueued.
1031 in6m_commit(struct in6_multi *inm)
1033 struct ip6_msource *ims;
1035 CTR2(KTR_MLD, "%s: commit inm %p", __func__, inm);
1036 CTR1(KTR_MLD, "%s: pre commit:", __func__);
1039 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
1040 ims->im6s_st[0] = ims->im6s_st[1];
1042 inm->in6m_st[0] = inm->in6m_st[1];
1046 * Reap unreferenced nodes from an in6_multi's filter set.
1049 in6m_reap(struct in6_multi *inm)
1051 struct ip6_msource *ims, *tims;
1053 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1054 if (ims->im6s_st[0].ex > 0 || ims->im6s_st[0].in > 0 ||
1055 ims->im6s_st[1].ex > 0 || ims->im6s_st[1].in > 0 ||
1058 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1059 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1060 free(ims, M_IP6MSOURCE);
1066 * Purge all source nodes from an in6_multi's filter set.
1069 in6m_purge(struct in6_multi *inm)
1071 struct ip6_msource *ims, *tims;
1073 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1074 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1075 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1076 free(ims, M_IP6MSOURCE);
1082 * Join a multicast address w/o sources.
1083 * KAME compatibility entry point.
1085 * SMPng: Assume no mc locks held by caller.
1087 struct in6_multi_mship *
1088 in6_joingroup(struct ifnet *ifp, struct in6_addr *mcaddr,
1089 int *errorp, int delay)
1091 struct in6_multi_mship *imm;
1094 imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT);
1100 delay = (delay * PR_FASTHZ) / hz;
1102 error = in6_mc_join(ifp, mcaddr, NULL, &imm->i6mm_maddr, delay);
1105 free(imm, M_IP6MADDR);
1113 * Leave a multicast address w/o sources.
1114 * KAME compatibility entry point.
1116 * SMPng: Assume no mc locks held by caller.
1119 in6_leavegroup(struct in6_multi_mship *imm)
1122 if (imm->i6mm_maddr != NULL)
1123 in6_mc_leave(imm->i6mm_maddr, NULL);
1124 free(imm, M_IP6MADDR);
1129 * Join a multicast group; unlocked entry point.
1131 * SMPng: XXX: in6_mc_join() is called from in6_control() when upper
1132 * locks are not held. Fortunately, ifp is unlikely to have been detached
1133 * at this point, so we assume it's OK to recurse.
1136 in6_mc_join(struct ifnet *ifp, const struct in6_addr *mcaddr,
1137 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1143 error = in6_mc_join_locked(ifp, mcaddr, imf, pinm, delay);
1150 * Join a multicast group; real entry point.
1152 * Only preserves atomicity at inm level.
1153 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1155 * If the MLD downcall fails, the group is not joined, and an error
1159 in6_mc_join_locked(struct ifnet *ifp, const struct in6_addr *mcaddr,
1160 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1163 struct in6_mfilter timf;
1164 struct in6_multi *inm;
1167 char ip6tbuf[INET6_ADDRSTRLEN];
1172 * Sanity: Check scope zone ID was set for ifp, if and
1173 * only if group is scoped to an interface.
1175 KASSERT(IN6_IS_ADDR_MULTICAST(mcaddr),
1176 ("%s: not a multicast address", __func__));
1177 if (IN6_IS_ADDR_MC_LINKLOCAL(mcaddr) ||
1178 IN6_IS_ADDR_MC_INTFACELOCAL(mcaddr)) {
1179 KASSERT(mcaddr->s6_addr16[1] != 0,
1180 ("%s: scope zone ID not set", __func__));
1184 IN6_MULTI_LOCK_ASSERT();
1186 CTR4(KTR_MLD, "%s: join %s on %p(%s))", __func__,
1187 ip6_sprintf(ip6tbuf, mcaddr), ifp, if_name(ifp));
1193 * If no imf was specified (i.e. kernel consumer),
1194 * fake one up and assume it is an ASM join.
1197 im6f_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1201 error = in6_mc_get(ifp, mcaddr, &inm);
1203 CTR1(KTR_MLD, "%s: in6_mc_get() failure", __func__);
1207 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1208 error = in6m_merge(inm, imf);
1210 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1211 goto out_in6m_release;
1214 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1215 error = mld_change_state(inm, delay);
1217 CTR1(KTR_MLD, "%s: failed to update source", __func__);
1218 goto out_in6m_release;
1223 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1224 in6m_release_locked(inm);
1233 * Leave a multicast group; unlocked entry point.
1236 in6_mc_leave(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1241 ifp = inm->in6m_ifp;
1244 error = in6_mc_leave_locked(inm, imf);
1251 * Leave a multicast group; real entry point.
1252 * All source filters will be expunged.
1254 * Only preserves atomicity at inm level.
1256 * Holding the write lock for the INP which contains imf
1257 * is highly advisable. We can't assert for it as imf does not
1258 * contain a back-pointer to the owning inp.
1260 * Note: This is not the same as in6m_release(*) as this function also
1261 * makes a state change downcall into MLD.
1264 in6_mc_leave_locked(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1266 struct in6_mfilter timf;
1269 char ip6tbuf[INET6_ADDRSTRLEN];
1274 IN6_MULTI_LOCK_ASSERT();
1276 CTR5(KTR_MLD, "%s: leave inm %p, %s/%s, imf %p", __func__,
1277 inm, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1278 (in6m_is_ifp_detached(inm) ? "null" : if_name(inm->in6m_ifp)),
1282 * If no imf was specified (i.e. kernel consumer),
1283 * fake one up and assume it is an ASM join.
1286 im6f_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1291 * Begin state merge transaction at MLD layer.
1293 * As this particular invocation should not cause any memory
1294 * to be allocated, and there is no opportunity to roll back
1295 * the transaction, it MUST NOT fail.
1297 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1298 error = in6m_merge(inm, imf);
1299 KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1301 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1302 error = mld_change_state(inm, 0);
1304 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1306 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1307 in6m_release_locked(inm);
1313 * Block or unblock an ASM multicast source on an inpcb.
1314 * This implements the delta-based API described in RFC 3678.
1316 * The delta-based API applies only to exclusive-mode memberships.
1317 * An MLD downcall will be performed.
1319 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1321 * Return 0 if successful, otherwise return an appropriate error code.
1324 in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1326 struct group_source_req gsr;
1327 sockunion_t *gsa, *ssa;
1329 struct in6_mfilter *imf;
1330 struct ip6_moptions *imo;
1331 struct in6_msource *ims;
1332 struct in6_multi *inm;
1337 char ip6tbuf[INET6_ADDRSTRLEN];
1344 memset(&gsr, 0, sizeof(struct group_source_req));
1345 gsa = (sockunion_t *)&gsr.gsr_group;
1346 ssa = (sockunion_t *)&gsr.gsr_source;
1348 switch (sopt->sopt_name) {
1349 case MCAST_BLOCK_SOURCE:
1350 case MCAST_UNBLOCK_SOURCE:
1351 error = sooptcopyin(sopt, &gsr,
1352 sizeof(struct group_source_req),
1353 sizeof(struct group_source_req));
1357 if (gsa->sin6.sin6_family != AF_INET6 ||
1358 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1361 if (ssa->sin6.sin6_family != AF_INET6 ||
1362 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1365 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1366 return (EADDRNOTAVAIL);
1368 ifp = ifnet_byindex(gsr.gsr_interface);
1370 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1375 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1376 __func__, sopt->sopt_name);
1377 return (EOPNOTSUPP);
1381 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1384 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1387 * Check if we are actually a member of this group.
1389 imo = in6p_findmoptions(inp);
1390 idx = im6o_match_group(imo, ifp, &gsa->sa);
1391 if (idx == -1 || imo->im6o_mfilters == NULL) {
1392 error = EADDRNOTAVAIL;
1393 goto out_in6p_locked;
1396 KASSERT(imo->im6o_mfilters != NULL,
1397 ("%s: im6o_mfilters not allocated", __func__));
1398 imf = &imo->im6o_mfilters[idx];
1399 inm = imo->im6o_membership[idx];
1402 * Attempting to use the delta-based API on an
1403 * non exclusive-mode membership is an error.
1405 fmode = imf->im6f_st[0];
1406 if (fmode != MCAST_EXCLUDE) {
1408 goto out_in6p_locked;
1412 * Deal with error cases up-front:
1413 * Asked to block, but already blocked; or
1414 * Asked to unblock, but nothing to unblock.
1415 * If adding a new block entry, allocate it.
1417 ims = im6o_match_source(imo, idx, &ssa->sa);
1418 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1419 CTR3(KTR_MLD, "%s: source %s %spresent", __func__,
1420 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
1421 doblock ? "" : "not ");
1422 error = EADDRNOTAVAIL;
1423 goto out_in6p_locked;
1426 INP_WLOCK_ASSERT(inp);
1429 * Begin state merge transaction at socket layer.
1432 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
1433 ims = im6f_graft(imf, fmode, &ssa->sin6);
1437 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
1438 error = im6f_prune(imf, &ssa->sin6);
1442 CTR1(KTR_MLD, "%s: merge imf state failed", __func__);
1443 goto out_im6f_rollback;
1447 * Begin state merge transaction at MLD layer.
1451 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1452 error = in6m_merge(inm, imf);
1454 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1456 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1457 error = mld_change_state(inm, 0);
1459 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1478 * Given an inpcb, return its multicast options structure pointer. Accepts
1479 * an unlocked inpcb pointer, but will return it locked. May sleep.
1481 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1482 * SMPng: NOTE: Returns with the INP write lock held.
1484 static struct ip6_moptions *
1485 in6p_findmoptions(struct inpcb *inp)
1487 struct ip6_moptions *imo;
1488 struct in6_multi **immp;
1489 struct in6_mfilter *imfp;
1493 if (inp->in6p_moptions != NULL)
1494 return (inp->in6p_moptions);
1498 imo = malloc(sizeof(*imo), M_IP6MOPTS, M_WAITOK);
1499 immp = malloc(sizeof(*immp) * IPV6_MIN_MEMBERSHIPS, M_IP6MOPTS,
1501 imfp = malloc(sizeof(struct in6_mfilter) * IPV6_MIN_MEMBERSHIPS,
1502 M_IN6MFILTER, M_WAITOK);
1504 imo->im6o_multicast_ifp = NULL;
1505 imo->im6o_multicast_hlim = V_ip6_defmcasthlim;
1506 imo->im6o_multicast_loop = in6_mcast_loop;
1507 imo->im6o_num_memberships = 0;
1508 imo->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1509 imo->im6o_membership = immp;
1511 /* Initialize per-group source filters. */
1512 for (idx = 0; idx < IPV6_MIN_MEMBERSHIPS; idx++)
1513 im6f_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE);
1514 imo->im6o_mfilters = imfp;
1517 if (inp->in6p_moptions != NULL) {
1518 free(imfp, M_IN6MFILTER);
1519 free(immp, M_IP6MOPTS);
1520 free(imo, M_IP6MOPTS);
1521 return (inp->in6p_moptions);
1523 inp->in6p_moptions = imo;
1528 * Discard the IPv6 multicast options (and source filters).
1530 * SMPng: NOTE: assumes INP write lock is held.
1533 ip6_freemoptions(struct ip6_moptions *imo)
1535 struct in6_mfilter *imf;
1536 size_t idx, nmships;
1538 KASSERT(imo != NULL, ("%s: ip6_moptions is NULL", __func__));
1540 nmships = imo->im6o_num_memberships;
1541 for (idx = 0; idx < nmships; ++idx) {
1542 imf = imo->im6o_mfilters ? &imo->im6o_mfilters[idx] : NULL;
1545 /* XXX this will thrash the lock(s) */
1546 (void)in6_mc_leave(imo->im6o_membership[idx], imf);
1551 if (imo->im6o_mfilters)
1552 free(imo->im6o_mfilters, M_IN6MFILTER);
1553 free(imo->im6o_membership, M_IP6MOPTS);
1554 free(imo, M_IP6MOPTS);
1558 * Atomically get source filters on a socket for an IPv6 multicast group.
1559 * Called with INP lock held; returns with lock released.
1562 in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1564 struct __msfilterreq msfr;
1567 struct ip6_moptions *imo;
1568 struct in6_mfilter *imf;
1569 struct ip6_msource *ims;
1570 struct in6_msource *lims;
1571 struct sockaddr_in6 *psin;
1572 struct sockaddr_storage *ptss;
1573 struct sockaddr_storage *tss;
1575 size_t idx, nsrcs, ncsrcs;
1577 INP_WLOCK_ASSERT(inp);
1579 imo = inp->in6p_moptions;
1580 KASSERT(imo != NULL, ("%s: null ip6_moptions", __func__));
1584 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1585 sizeof(struct __msfilterreq));
1589 if (msfr.msfr_group.ss_family != AF_INET6 ||
1590 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
1593 gsa = (sockunion_t *)&msfr.msfr_group;
1594 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1597 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
1598 return (EADDRNOTAVAIL);
1599 ifp = ifnet_byindex(msfr.msfr_ifindex);
1601 return (EADDRNOTAVAIL);
1602 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1607 * Lookup group on the socket.
1609 idx = im6o_match_group(imo, ifp, &gsa->sa);
1610 if (idx == -1 || imo->im6o_mfilters == NULL) {
1612 return (EADDRNOTAVAIL);
1614 imf = &imo->im6o_mfilters[idx];
1617 * Ignore memberships which are in limbo.
1619 if (imf->im6f_st[1] == MCAST_UNDEFINED) {
1623 msfr.msfr_fmode = imf->im6f_st[1];
1626 * If the user specified a buffer, copy out the source filter
1627 * entries to userland gracefully.
1628 * We only copy out the number of entries which userland
1629 * has asked for, but we always tell userland how big the
1630 * buffer really needs to be.
1632 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
1633 msfr.msfr_nsrcs = in6_mcast_maxsocksrc;
1635 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1636 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1637 M_TEMP, M_NOWAIT | M_ZERO);
1645 * Count number of sources in-mode at t0.
1646 * If buffer space exists and remains, copy out source entries.
1648 nsrcs = msfr.msfr_nsrcs;
1651 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
1652 lims = (struct in6_msource *)ims;
1653 if (lims->im6sl_st[0] == MCAST_UNDEFINED ||
1654 lims->im6sl_st[0] != imf->im6f_st[0])
1657 if (tss != NULL && nsrcs > 0) {
1658 psin = (struct sockaddr_in6 *)ptss;
1659 psin->sin6_family = AF_INET6;
1660 psin->sin6_len = sizeof(struct sockaddr_in6);
1661 psin->sin6_addr = lims->im6s_addr;
1662 psin->sin6_port = 0;
1671 error = copyout(tss, msfr.msfr_srcs,
1672 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1678 msfr.msfr_nsrcs = ncsrcs;
1679 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1685 * Return the IP multicast options in response to user getsockopt().
1688 ip6_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1690 struct ip6_moptions *im6o;
1695 im6o = inp->in6p_moptions;
1697 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1698 * or is a divert socket, reject it.
1700 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1701 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1702 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
1704 return (EOPNOTSUPP);
1708 switch (sopt->sopt_name) {
1709 case IPV6_MULTICAST_IF:
1710 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) {
1713 optval = im6o->im6o_multicast_ifp->if_index;
1716 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1719 case IPV6_MULTICAST_HOPS:
1721 optval = V_ip6_defmcasthlim;
1723 optval = im6o->im6o_multicast_hlim;
1725 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1728 case IPV6_MULTICAST_LOOP:
1730 optval = in6_mcast_loop; /* XXX VIMAGE */
1732 optval = im6o->im6o_multicast_loop;
1734 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1739 error = EADDRNOTAVAIL;
1742 error = in6p_get_source_filters(inp, sopt);
1748 error = ENOPROTOOPT;
1752 INP_UNLOCK_ASSERT(inp);
1758 * Look up the ifnet to use for a multicast group membership,
1759 * given the address of an IPv6 group.
1761 * This routine exists to support legacy IPv6 multicast applications.
1763 * If inp is non-NULL, use this socket's current FIB number for any
1764 * required FIB lookup. Look up the group address in the unicast FIB,
1765 * and use its ifp; usually, this points to the default next-hop.
1766 * If the FIB lookup fails, return NULL.
1768 * FUTURE: Support multiple forwarding tables for IPv6.
1770 * Returns NULL if no ifp could be found.
1772 static struct ifnet *
1773 in6p_lookup_mcast_ifp(const struct inpcb *in6p,
1774 const struct sockaddr_in6 *gsin6)
1776 struct route_in6 ro6;
1779 KASSERT(in6p->inp_vflag & INP_IPV6,
1780 ("%s: not INP_IPV6 inpcb", __func__));
1781 KASSERT(gsin6->sin6_family == AF_INET6,
1782 ("%s: not AF_INET6 group", __func__));
1785 memset(&ro6, 0, sizeof(struct route_in6));
1786 memcpy(&ro6.ro_dst, gsin6, sizeof(struct sockaddr_in6));
1787 rtalloc_ign_fib((struct route *)&ro6, 0,
1788 in6p ? in6p->inp_inc.inc_fibnum : RT_DEFAULT_FIB);
1789 if (ro6.ro_rt != NULL) {
1790 ifp = ro6.ro_rt->rt_ifp;
1791 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
1799 * Join an IPv6 multicast group, possibly with a source.
1801 * FIXME: The KAME use of the unspecified address (::)
1802 * to join *all* multicast groups is currently unsupported.
1805 in6p_join_group(struct inpcb *inp, struct sockopt *sopt)
1807 struct group_source_req gsr;
1808 sockunion_t *gsa, *ssa;
1810 struct in6_mfilter *imf;
1811 struct ip6_moptions *imo;
1812 struct in6_multi *inm;
1813 struct in6_msource *lims;
1823 memset(&gsr, 0, sizeof(struct group_source_req));
1824 gsa = (sockunion_t *)&gsr.gsr_group;
1825 gsa->ss.ss_family = AF_UNSPEC;
1826 ssa = (sockunion_t *)&gsr.gsr_source;
1827 ssa->ss.ss_family = AF_UNSPEC;
1830 * Chew everything into struct group_source_req.
1831 * Overwrite the port field if present, as the sockaddr
1832 * being copied in may be matched with a binary comparison.
1833 * Ignore passed-in scope ID.
1835 switch (sopt->sopt_name) {
1836 case IPV6_JOIN_GROUP: {
1837 struct ipv6_mreq mreq;
1839 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
1840 sizeof(struct ipv6_mreq));
1844 gsa->sin6.sin6_family = AF_INET6;
1845 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
1846 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
1848 if (mreq.ipv6mr_interface == 0) {
1849 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
1851 if (V_if_index < mreq.ipv6mr_interface)
1852 return (EADDRNOTAVAIL);
1853 ifp = ifnet_byindex(mreq.ipv6mr_interface);
1855 CTR3(KTR_MLD, "%s: ipv6mr_interface = %d, ifp = %p",
1856 __func__, mreq.ipv6mr_interface, ifp);
1859 case MCAST_JOIN_GROUP:
1860 case MCAST_JOIN_SOURCE_GROUP:
1861 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1862 error = sooptcopyin(sopt, &gsr,
1863 sizeof(struct group_req),
1864 sizeof(struct group_req));
1865 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1866 error = sooptcopyin(sopt, &gsr,
1867 sizeof(struct group_source_req),
1868 sizeof(struct group_source_req));
1873 if (gsa->sin6.sin6_family != AF_INET6 ||
1874 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1877 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1878 if (ssa->sin6.sin6_family != AF_INET6 ||
1879 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1881 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
1884 * TODO: Validate embedded scope ID in source
1885 * list entry against passed-in ifp, if and only
1886 * if source list filter entry is iface or node local.
1888 in6_clearscope(&ssa->sin6.sin6_addr);
1889 ssa->sin6.sin6_port = 0;
1890 ssa->sin6.sin6_scope_id = 0;
1893 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1894 return (EADDRNOTAVAIL);
1895 ifp = ifnet_byindex(gsr.gsr_interface);
1899 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1900 __func__, sopt->sopt_name);
1901 return (EOPNOTSUPP);
1905 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1908 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
1909 return (EADDRNOTAVAIL);
1911 gsa->sin6.sin6_port = 0;
1912 gsa->sin6.sin6_scope_id = 0;
1915 * Always set the scope zone ID on memberships created from userland.
1916 * Use the passed-in ifp to do this.
1917 * XXX The in6_setscope() return value is meaningless.
1918 * XXX SCOPE6_LOCK() is taken by in6_setscope().
1920 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1922 imo = in6p_findmoptions(inp);
1923 idx = im6o_match_group(imo, ifp, &gsa->sa);
1927 inm = imo->im6o_membership[idx];
1928 imf = &imo->im6o_mfilters[idx];
1929 if (ssa->ss.ss_family != AF_UNSPEC) {
1931 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
1932 * is an error. On an existing inclusive membership,
1933 * it just adds the source to the filter list.
1935 if (imf->im6f_st[1] != MCAST_INCLUDE) {
1937 goto out_in6p_locked;
1940 * Throw out duplicates.
1942 * XXX FIXME: This makes a naive assumption that
1943 * even if entries exist for *ssa in this imf,
1944 * they will be rejected as dupes, even if they
1945 * are not valid in the current mode (in-mode).
1947 * in6_msource is transactioned just as for anything
1948 * else in SSM -- but note naive use of in6m_graft()
1949 * below for allocating new filter entries.
1951 * This is only an issue if someone mixes the
1952 * full-state SSM API with the delta-based API,
1953 * which is discouraged in the relevant RFCs.
1955 lims = im6o_match_source(imo, idx, &ssa->sa);
1956 if (lims != NULL /*&&
1957 lims->im6sl_st[1] == MCAST_INCLUDE*/) {
1958 error = EADDRNOTAVAIL;
1959 goto out_in6p_locked;
1963 * MCAST_JOIN_GROUP alone, on any existing membership,
1964 * is rejected, to stop the same inpcb tying up
1965 * multiple refs to the in_multi.
1966 * On an existing inclusive membership, this is also
1967 * an error; if you want to change filter mode,
1968 * you must use the userland API setsourcefilter().
1969 * XXX We don't reject this for imf in UNDEFINED
1970 * state at t1, because allocation of a filter
1971 * is atomic with allocation of a membership.
1974 goto out_in6p_locked;
1979 * Begin state merge transaction at socket layer.
1981 INP_WLOCK_ASSERT(inp);
1984 if (imo->im6o_num_memberships == imo->im6o_max_memberships) {
1985 error = im6o_grow(imo);
1987 goto out_in6p_locked;
1990 * Allocate the new slot upfront so we can deal with
1991 * grafting the new source filter in same code path
1992 * as for join-source on existing membership.
1994 idx = imo->im6o_num_memberships;
1995 imo->im6o_membership[idx] = NULL;
1996 imo->im6o_num_memberships++;
1997 KASSERT(imo->im6o_mfilters != NULL,
1998 ("%s: im6f_mfilters vector was not allocated", __func__));
1999 imf = &imo->im6o_mfilters[idx];
2000 KASSERT(RB_EMPTY(&imf->im6f_sources),
2001 ("%s: im6f_sources not empty", __func__));
2005 * Graft new source into filter list for this inpcb's
2006 * membership of the group. The in6_multi may not have
2007 * been allocated yet if this is a new membership, however,
2008 * the in_mfilter slot will be allocated and must be initialized.
2010 * Note: Grafting of exclusive mode filters doesn't happen
2012 * XXX: Should check for non-NULL lims (node exists but may
2013 * not be in-mode) for interop with full-state API.
2015 if (ssa->ss.ss_family != AF_UNSPEC) {
2016 /* Membership starts in IN mode */
2018 CTR1(KTR_MLD, "%s: new join w/source", __func__);
2019 im6f_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE);
2021 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
2023 lims = im6f_graft(imf, MCAST_INCLUDE, &ssa->sin6);
2025 CTR1(KTR_MLD, "%s: merge imf state failed",
2031 /* No address specified; Membership starts in EX mode */
2033 CTR1(KTR_MLD, "%s: new join w/o source", __func__);
2034 im6f_init(imf, MCAST_UNDEFINED, MCAST_EXCLUDE);
2039 * Begin state merge transaction at MLD layer.
2044 error = in6_mc_join_locked(ifp, &gsa->sin6.sin6_addr, imf,
2050 imo->im6o_membership[idx] = inm;
2052 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2053 error = in6m_merge(inm, imf);
2055 CTR1(KTR_MLD, "%s: failed to merge inm state",
2058 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2059 error = mld_change_state(inm, 0);
2061 CTR1(KTR_MLD, "%s: failed mld downcall",
2067 INP_WLOCK_ASSERT(inp);
2079 if (error && is_new) {
2080 imo->im6o_membership[idx] = NULL;
2081 --imo->im6o_num_memberships;
2090 * Leave an IPv6 multicast group on an inpcb, possibly with a source.
2093 in6p_leave_group(struct inpcb *inp, struct sockopt *sopt)
2095 struct ipv6_mreq mreq;
2096 struct group_source_req gsr;
2097 sockunion_t *gsa, *ssa;
2099 struct in6_mfilter *imf;
2100 struct ip6_moptions *imo;
2101 struct in6_msource *ims;
2102 struct in6_multi *inm;
2105 int error, is_final;
2107 char ip6tbuf[INET6_ADDRSTRLEN];
2115 memset(&gsr, 0, sizeof(struct group_source_req));
2116 gsa = (sockunion_t *)&gsr.gsr_group;
2117 gsa->ss.ss_family = AF_UNSPEC;
2118 ssa = (sockunion_t *)&gsr.gsr_source;
2119 ssa->ss.ss_family = AF_UNSPEC;
2122 * Chew everything passed in up into a struct group_source_req
2123 * as that is easier to process.
2124 * Note: Any embedded scope ID in the multicast group passed
2125 * in by userland is ignored, the interface index is the recommended
2126 * mechanism to specify an interface; see below.
2128 switch (sopt->sopt_name) {
2129 case IPV6_LEAVE_GROUP:
2130 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
2131 sizeof(struct ipv6_mreq));
2134 gsa->sin6.sin6_family = AF_INET6;
2135 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
2136 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
2137 gsa->sin6.sin6_port = 0;
2138 gsa->sin6.sin6_scope_id = 0;
2139 ifindex = mreq.ipv6mr_interface;
2142 case MCAST_LEAVE_GROUP:
2143 case MCAST_LEAVE_SOURCE_GROUP:
2144 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2145 error = sooptcopyin(sopt, &gsr,
2146 sizeof(struct group_req),
2147 sizeof(struct group_req));
2148 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2149 error = sooptcopyin(sopt, &gsr,
2150 sizeof(struct group_source_req),
2151 sizeof(struct group_source_req));
2156 if (gsa->sin6.sin6_family != AF_INET6 ||
2157 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2159 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2160 if (ssa->sin6.sin6_family != AF_INET6 ||
2161 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2163 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
2166 * TODO: Validate embedded scope ID in source
2167 * list entry against passed-in ifp, if and only
2168 * if source list filter entry is iface or node local.
2170 in6_clearscope(&ssa->sin6.sin6_addr);
2172 gsa->sin6.sin6_port = 0;
2173 gsa->sin6.sin6_scope_id = 0;
2174 ifindex = gsr.gsr_interface;
2178 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
2179 __func__, sopt->sopt_name);
2180 return (EOPNOTSUPP);
2184 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2188 * Validate interface index if provided. If no interface index
2189 * was provided separately, attempt to look the membership up
2190 * from the default scope as a last resort to disambiguate
2191 * the membership we are being asked to leave.
2192 * XXX SCOPE6 lock potentially taken here.
2195 if (V_if_index < ifindex)
2196 return (EADDRNOTAVAIL);
2197 ifp = ifnet_byindex(ifindex);
2199 return (EADDRNOTAVAIL);
2200 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2202 error = sa6_embedscope(&gsa->sin6, V_ip6_use_defzone);
2204 return (EADDRNOTAVAIL);
2206 * Some badly behaved applications don't pass an ifindex
2207 * or a scope ID, which is an API violation. In this case,
2208 * perform a lookup as per a v6 join.
2210 * XXX For now, stomp on zone ID for the corner case.
2211 * This is not the 'KAME way', but we need to see the ifp
2212 * directly until such time as this implementation is
2213 * refactored, assuming the scope IDs are the way to go.
2215 ifindex = ntohs(gsa->sin6.sin6_addr.s6_addr16[1]);
2217 CTR2(KTR_MLD, "%s: warning: no ifindex, looking up "
2218 "ifp for group %s.", __func__,
2219 ip6_sprintf(ip6tbuf, &gsa->sin6.sin6_addr));
2220 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
2222 ifp = ifnet_byindex(ifindex);
2225 return (EADDRNOTAVAIL);
2228 CTR2(KTR_MLD, "%s: ifp = %p", __func__, ifp);
2229 KASSERT(ifp != NULL, ("%s: ifp did not resolve", __func__));
2232 * Find the membership in the membership array.
2234 imo = in6p_findmoptions(inp);
2235 idx = im6o_match_group(imo, ifp, &gsa->sa);
2237 error = EADDRNOTAVAIL;
2238 goto out_in6p_locked;
2240 inm = imo->im6o_membership[idx];
2241 imf = &imo->im6o_mfilters[idx];
2243 if (ssa->ss.ss_family != AF_UNSPEC)
2247 * Begin state merge transaction at socket layer.
2249 INP_WLOCK_ASSERT(inp);
2252 * If we were instructed only to leave a given source, do so.
2253 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2258 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
2259 error = EADDRNOTAVAIL;
2260 goto out_in6p_locked;
2262 ims = im6o_match_source(imo, idx, &ssa->sa);
2264 CTR3(KTR_MLD, "%s: source %p %spresent", __func__,
2265 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
2267 error = EADDRNOTAVAIL;
2268 goto out_in6p_locked;
2270 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
2271 error = im6f_prune(imf, &ssa->sin6);
2273 CTR1(KTR_MLD, "%s: merge imf state failed",
2275 goto out_in6p_locked;
2280 * Begin state merge transaction at MLD layer.
2286 * Give up the multicast address record to which
2287 * the membership points.
2289 (void)in6_mc_leave_locked(inm, imf);
2291 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2292 error = in6m_merge(inm, imf);
2294 CTR1(KTR_MLD, "%s: failed to merge inm state",
2297 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2298 error = mld_change_state(inm, 0);
2300 CTR1(KTR_MLD, "%s: failed mld downcall",
2315 /* Remove the gap in the membership array. */
2316 for (++idx; idx < imo->im6o_num_memberships; ++idx) {
2317 imo->im6o_membership[idx-1] = imo->im6o_membership[idx];
2318 imo->im6o_mfilters[idx-1] = imo->im6o_mfilters[idx];
2320 imo->im6o_num_memberships--;
2329 * Select the interface for transmitting IPv6 multicast datagrams.
2331 * Either an instance of struct in6_addr or an instance of struct ipv6_mreqn
2332 * may be passed to this socket option. An address of in6addr_any or an
2333 * interface index of 0 is used to remove a previous selection.
2334 * When no interface is selected, one is chosen for every send.
2337 in6p_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2340 struct ip6_moptions *imo;
2344 if (sopt->sopt_valsize != sizeof(u_int))
2347 error = sooptcopyin(sopt, &ifindex, sizeof(u_int), sizeof(u_int));
2350 if (V_if_index < ifindex)
2353 ifp = ifnet_byindex(ifindex);
2354 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
2355 return (EADDRNOTAVAIL);
2357 imo = in6p_findmoptions(inp);
2358 imo->im6o_multicast_ifp = ifp;
2365 * Atomically set source filters on a socket for an IPv6 multicast group.
2367 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
2370 in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2372 struct __msfilterreq msfr;
2375 struct in6_mfilter *imf;
2376 struct ip6_moptions *imo;
2377 struct in6_multi *inm;
2381 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2382 sizeof(struct __msfilterreq));
2386 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
2389 if (msfr.msfr_fmode != MCAST_EXCLUDE &&
2390 msfr.msfr_fmode != MCAST_INCLUDE)
2393 if (msfr.msfr_group.ss_family != AF_INET6 ||
2394 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
2397 gsa = (sockunion_t *)&msfr.msfr_group;
2398 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2401 gsa->sin6.sin6_port = 0; /* ignore port */
2403 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
2404 return (EADDRNOTAVAIL);
2405 ifp = ifnet_byindex(msfr.msfr_ifindex);
2407 return (EADDRNOTAVAIL);
2408 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2411 * Take the INP write lock.
2412 * Check if this socket is a member of this group.
2414 imo = in6p_findmoptions(inp);
2415 idx = im6o_match_group(imo, ifp, &gsa->sa);
2416 if (idx == -1 || imo->im6o_mfilters == NULL) {
2417 error = EADDRNOTAVAIL;
2418 goto out_in6p_locked;
2420 inm = imo->im6o_membership[idx];
2421 imf = &imo->im6o_mfilters[idx];
2424 * Begin state merge transaction at socket layer.
2426 INP_WLOCK_ASSERT(inp);
2428 imf->im6f_st[1] = msfr.msfr_fmode;
2431 * Apply any new source filters, if present.
2432 * Make a copy of the user-space source vector so
2433 * that we may copy them with a single copyin. This
2434 * allows us to deal with page faults up-front.
2436 if (msfr.msfr_nsrcs > 0) {
2437 struct in6_msource *lims;
2438 struct sockaddr_in6 *psin;
2439 struct sockaddr_storage *kss, *pkss;
2444 CTR2(KTR_MLD, "%s: loading %lu source list entries",
2445 __func__, (unsigned long)msfr.msfr_nsrcs);
2446 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2448 error = copyin(msfr.msfr_srcs, kss,
2449 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2458 * Mark all source filters as UNDEFINED at t1.
2459 * Restore new group filter mode, as im6f_leave()
2460 * will set it to INCLUDE.
2463 imf->im6f_st[1] = msfr.msfr_fmode;
2466 * Update socket layer filters at t1, lazy-allocating
2467 * new entries. This saves a bunch of memory at the
2468 * cost of one RB_FIND() per source entry; duplicate
2469 * entries in the msfr_nsrcs vector are ignored.
2470 * If we encounter an error, rollback transaction.
2472 * XXX This too could be replaced with a set-symmetric
2473 * difference like loop to avoid walking from root
2474 * every time, as the key space is common.
2476 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2477 psin = (struct sockaddr_in6 *)pkss;
2478 if (psin->sin6_family != AF_INET6) {
2479 error = EAFNOSUPPORT;
2482 if (psin->sin6_len != sizeof(struct sockaddr_in6)) {
2486 if (IN6_IS_ADDR_MULTICAST(&psin->sin6_addr)) {
2491 * TODO: Validate embedded scope ID in source
2492 * list entry against passed-in ifp, if and only
2493 * if source list filter entry is iface or node local.
2495 in6_clearscope(&psin->sin6_addr);
2496 error = im6f_get_source(imf, psin, &lims);
2499 lims->im6sl_st[1] = imf->im6f_st[1];
2505 goto out_im6f_rollback;
2507 INP_WLOCK_ASSERT(inp);
2511 * Begin state merge transaction at MLD layer.
2513 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2514 error = in6m_merge(inm, imf);
2516 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
2518 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2519 error = mld_change_state(inm, 0);
2521 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
2540 * Set the IP multicast options in response to user setsockopt().
2542 * Many of the socket options handled in this function duplicate the
2543 * functionality of socket options in the regular unicast API. However,
2544 * it is not possible to merge the duplicate code, because the idempotence
2545 * of the IPv6 multicast part of the BSD Sockets API must be preserved;
2546 * the effects of these options must be treated as separate and distinct.
2548 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2551 ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2553 struct ip6_moptions *im6o;
2559 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2560 * or is a divert socket, reject it.
2562 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
2563 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2564 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
2565 return (EOPNOTSUPP);
2567 switch (sopt->sopt_name) {
2568 case IPV6_MULTICAST_IF:
2569 error = in6p_set_multicast_if(inp, sopt);
2572 case IPV6_MULTICAST_HOPS: {
2575 if (sopt->sopt_valsize != sizeof(int)) {
2579 error = sooptcopyin(sopt, &hlim, sizeof(hlim), sizeof(int));
2582 if (hlim < -1 || hlim > 255) {
2585 } else if (hlim == -1) {
2586 hlim = V_ip6_defmcasthlim;
2588 im6o = in6p_findmoptions(inp);
2589 im6o->im6o_multicast_hlim = hlim;
2594 case IPV6_MULTICAST_LOOP: {
2598 * Set the loopback flag for outgoing multicast packets.
2599 * Must be zero or one.
2601 if (sopt->sopt_valsize != sizeof(u_int)) {
2605 error = sooptcopyin(sopt, &loop, sizeof(u_int), sizeof(u_int));
2612 im6o = in6p_findmoptions(inp);
2613 im6o->im6o_multicast_loop = loop;
2618 case IPV6_JOIN_GROUP:
2619 case MCAST_JOIN_GROUP:
2620 case MCAST_JOIN_SOURCE_GROUP:
2621 error = in6p_join_group(inp, sopt);
2624 case IPV6_LEAVE_GROUP:
2625 case MCAST_LEAVE_GROUP:
2626 case MCAST_LEAVE_SOURCE_GROUP:
2627 error = in6p_leave_group(inp, sopt);
2630 case MCAST_BLOCK_SOURCE:
2631 case MCAST_UNBLOCK_SOURCE:
2632 error = in6p_block_unblock_source(inp, sopt);
2636 error = in6p_set_source_filters(inp, sopt);
2644 INP_UNLOCK_ASSERT(inp);
2650 * Expose MLD's multicast filter mode and source list(s) to userland,
2651 * keyed by (ifindex, group).
2652 * The filter mode is written out as a uint32_t, followed by
2653 * 0..n of struct in6_addr.
2654 * For use by ifmcstat(8).
2655 * SMPng: NOTE: unlocked read of ifindex space.
2658 sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS)
2660 struct in6_addr mcaddr;
2661 struct in6_addr src;
2663 struct ifmultiaddr *ifma;
2664 struct in6_multi *inm;
2665 struct ip6_msource *ims;
2669 uint32_t fmode, ifindex;
2671 char ip6tbuf[INET6_ADDRSTRLEN];
2677 if (req->newptr != NULL)
2680 /* int: ifindex + 4 * 32 bits of IPv6 address */
2685 if (ifindex <= 0 || ifindex > V_if_index) {
2686 CTR2(KTR_MLD, "%s: ifindex %u out of range",
2691 memcpy(&mcaddr, &name[1], sizeof(struct in6_addr));
2692 if (!IN6_IS_ADDR_MULTICAST(&mcaddr)) {
2693 CTR2(KTR_MLD, "%s: group %s is not multicast",
2694 __func__, ip6_sprintf(ip6tbuf, &mcaddr));
2698 ifp = ifnet_byindex(ifindex);
2700 CTR2(KTR_MLD, "%s: no ifp for ifindex %u",
2705 * Internal MLD lookups require that scope/zone ID is set.
2707 (void)in6_setscope(&mcaddr, ifp, NULL);
2709 retval = sysctl_wire_old_buffer(req,
2710 sizeof(uint32_t) + (in6_mcast_maxgrpsrc * sizeof(struct in6_addr)));
2717 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2718 if (ifma->ifma_addr->sa_family != AF_INET6 ||
2719 ifma->ifma_protospec == NULL)
2721 inm = (struct in6_multi *)ifma->ifma_protospec;
2722 if (!IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, &mcaddr))
2724 fmode = inm->in6m_st[1].iss_fmode;
2725 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2728 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
2729 CTR2(KTR_MLD, "%s: visit node %p", __func__, ims);
2731 * Only copy-out sources which are in-mode.
2733 if (fmode != im6s_get_mode(inm, ims, 1)) {
2734 CTR1(KTR_MLD, "%s: skip non-in-mode",
2738 src = ims->im6s_addr;
2739 retval = SYSCTL_OUT(req, &src,
2740 sizeof(struct in6_addr));
2745 IF_ADDR_RUNLOCK(ifp);
2754 static const char *in6m_modestrs[] = { "un", "in", "ex" };
2757 in6m_mode_str(const int mode)
2760 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2761 return (in6m_modestrs[mode]);
2765 static const char *in6m_statestrs[] = {
2778 in6m_state_str(const int state)
2781 if (state >= MLD_NOT_MEMBER && state <= MLD_LEAVING_MEMBER)
2782 return (in6m_statestrs[state]);
2787 * Dump an in6_multi structure to the console.
2790 in6m_print(const struct in6_multi *inm)
2793 char ip6tbuf[INET6_ADDRSTRLEN];
2795 if ((ktr_mask & KTR_MLD) == 0)
2798 printf("%s: --- begin in6m %p ---\n", __func__, inm);
2799 printf("addr %s ifp %p(%s) ifma %p\n",
2800 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2802 if_name(inm->in6m_ifp),
2804 printf("timer %u state %s refcount %u scq.len %u\n",
2806 in6m_state_str(inm->in6m_state),
2808 inm->in6m_scq.ifq_len);
2809 printf("mli %p nsrc %lu sctimer %u scrv %u\n",
2814 for (t = 0; t < 2; t++) {
2815 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
2816 in6m_mode_str(inm->in6m_st[t].iss_fmode),
2817 inm->in6m_st[t].iss_asm,
2818 inm->in6m_st[t].iss_ex,
2819 inm->in6m_st[t].iss_in,
2820 inm->in6m_st[t].iss_rec);
2822 printf("%s: --- end in6m %p ---\n", __func__, inm);
2828 in6m_print(const struct in6_multi *inm)