2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2009 Bruce Simpson.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * IPv6 multicast socket, group, and socket option processing module.
34 * Normative references: RFC 2292, RFC 3492, RFC 3542, RFC 3678, RFC 3810.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_inet6.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
53 #include <sys/taskqueue.h>
57 #include <net/if_var.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <net/route/nhop.h>
63 #include <netinet/in.h>
64 #include <netinet/udp.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/udp_var.h>
68 #include <netinet6/in6_fib.h>
69 #include <netinet6/in6_var.h>
70 #include <netinet/ip6.h>
71 #include <netinet/icmp6.h>
72 #include <netinet6/ip6_var.h>
73 #include <netinet/in_pcb.h>
74 #include <netinet/tcp_var.h>
75 #include <netinet6/nd6.h>
76 #include <netinet6/mld6_var.h>
77 #include <netinet6/scope6_var.h>
80 #define KTR_MLD KTR_INET6
83 #ifndef __SOCKUNION_DECLARED
85 struct sockaddr_storage ss;
87 struct sockaddr_dl sdl;
88 struct sockaddr_in6 sin6;
90 typedef union sockunion sockunion_t;
91 #define __SOCKUNION_DECLARED
92 #endif /* __SOCKUNION_DECLARED */
94 static MALLOC_DEFINE(M_IN6MFILTER, "in6_mfilter",
95 "IPv6 multicast PCB-layer source filter");
96 MALLOC_DEFINE(M_IP6MADDR, "in6_multi", "IPv6 multicast group");
97 static MALLOC_DEFINE(M_IP6MOPTS, "ip6_moptions", "IPv6 multicast options");
98 static MALLOC_DEFINE(M_IP6MSOURCE, "ip6_msource",
99 "IPv6 multicast MLD-layer source filter");
101 RB_GENERATE(ip6_msource_tree, ip6_msource, im6s_link, ip6_msource_cmp);
105 * - Lock order is: Giant, IN6_MULTI_LOCK, INP_WLOCK,
106 * IN6_MULTI_LIST_LOCK, MLD_LOCK, IF_ADDR_LOCK.
107 * - The IF_ADDR_LOCK is implicitly taken by in6m_lookup() earlier, however
108 * it can be taken by code in net/if.c also.
109 * - ip6_moptions and in6_mfilter are covered by the INP_WLOCK.
111 * struct in6_multi is covered by IN6_MULTI_LOCK. There isn't strictly
112 * any need for in6_multi itself to be virtualized -- it is bound to an ifp
113 * anyway no matter what happens.
115 struct mtx in6_multi_list_mtx;
116 MTX_SYSINIT(in6_multi_mtx, &in6_multi_list_mtx, "in6_multi_list_mtx", MTX_DEF);
118 struct mtx in6_multi_free_mtx;
119 MTX_SYSINIT(in6_multi_free_mtx, &in6_multi_free_mtx, "in6_multi_free_mtx", MTX_DEF);
121 struct sx in6_multi_sx;
122 SX_SYSINIT(in6_multi_sx, &in6_multi_sx, "in6_multi_sx");
124 static void im6f_commit(struct in6_mfilter *);
125 static int im6f_get_source(struct in6_mfilter *imf,
126 const struct sockaddr_in6 *psin,
127 struct in6_msource **);
128 static struct in6_msource *
129 im6f_graft(struct in6_mfilter *, const uint8_t,
130 const struct sockaddr_in6 *);
131 static void im6f_leave(struct in6_mfilter *);
132 static int im6f_prune(struct in6_mfilter *, const struct sockaddr_in6 *);
133 static void im6f_purge(struct in6_mfilter *);
134 static void im6f_rollback(struct in6_mfilter *);
135 static void im6f_reap(struct in6_mfilter *);
136 static struct in6_mfilter *
137 im6o_match_group(const struct ip6_moptions *,
138 const struct ifnet *, const struct sockaddr *);
139 static struct in6_msource *
140 im6o_match_source(struct in6_mfilter *, const struct sockaddr *);
141 static void im6s_merge(struct ip6_msource *ims,
142 const struct in6_msource *lims, const int rollback);
143 static int in6_getmulti(struct ifnet *, const struct in6_addr *,
144 struct in6_multi **);
145 static int in6_joingroup_locked(struct ifnet *, const struct in6_addr *,
146 struct in6_mfilter *, struct in6_multi **, int);
147 static int in6m_get_source(struct in6_multi *inm,
148 const struct in6_addr *addr, const int noalloc,
149 struct ip6_msource **pims);
151 static int in6m_is_ifp_detached(const struct in6_multi *);
153 static int in6m_merge(struct in6_multi *, /*const*/ struct in6_mfilter *);
154 static void in6m_purge(struct in6_multi *);
155 static void in6m_reap(struct in6_multi *);
156 static struct ip6_moptions *
157 in6p_findmoptions(struct inpcb *);
158 static int in6p_get_source_filters(struct inpcb *, struct sockopt *);
159 static int in6p_join_group(struct inpcb *, struct sockopt *);
160 static int in6p_leave_group(struct inpcb *, struct sockopt *);
161 static struct ifnet *
162 in6p_lookup_mcast_ifp(const struct inpcb *,
163 const struct sockaddr_in6 *);
164 static int in6p_block_unblock_source(struct inpcb *, struct sockopt *);
165 static int in6p_set_multicast_if(struct inpcb *, struct sockopt *);
166 static int in6p_set_source_filters(struct inpcb *, struct sockopt *);
167 static int sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS);
169 SYSCTL_DECL(_net_inet6_ip6); /* XXX Not in any common header. */
171 static SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, mcast,
172 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
175 static u_long in6_mcast_maxgrpsrc = IPV6_MAX_GROUP_SRC_FILTER;
176 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxgrpsrc,
177 CTLFLAG_RWTUN, &in6_mcast_maxgrpsrc, 0,
178 "Max source filters per group");
180 static u_long in6_mcast_maxsocksrc = IPV6_MAX_SOCK_SRC_FILTER;
181 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxsocksrc,
182 CTLFLAG_RWTUN, &in6_mcast_maxsocksrc, 0,
183 "Max source filters per socket");
185 /* TODO Virtualize this switch. */
186 int in6_mcast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
187 SYSCTL_INT(_net_inet6_ip6_mcast, OID_AUTO, loop, CTLFLAG_RWTUN,
188 &in6_mcast_loop, 0, "Loopback multicast datagrams by default");
190 static SYSCTL_NODE(_net_inet6_ip6_mcast, OID_AUTO, filters,
191 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip6_mcast_filters,
192 "Per-interface stack-wide source filters");
196 * Inline function which wraps assertions for a valid ifp.
197 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
201 in6m_is_ifp_detached(const struct in6_multi *inm)
205 KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__));
206 ifp = inm->in6m_ifma->ifma_ifp;
209 * Sanity check that network-layer notion of ifp is the
210 * same as that of link-layer.
212 KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__));
215 return (ifp == NULL);
220 * Initialize an in6_mfilter structure to a known state at t0, t1
221 * with an empty source filter list.
224 im6f_init(struct in6_mfilter *imf, const int st0, const int st1)
226 memset(imf, 0, sizeof(struct in6_mfilter));
227 RB_INIT(&imf->im6f_sources);
228 imf->im6f_st[0] = st0;
229 imf->im6f_st[1] = st1;
233 ip6_mfilter_alloc(const int mflags, const int st0, const int st1)
235 struct in6_mfilter *imf;
237 imf = malloc(sizeof(*imf), M_IN6MFILTER, mflags);
240 im6f_init(imf, st0, st1);
246 ip6_mfilter_free(struct in6_mfilter *imf)
250 free(imf, M_IN6MFILTER);
254 * Find an IPv6 multicast group entry for this ip6_moptions instance
255 * which matches the specified group, and optionally an interface.
256 * Return its index into the array, or -1 if not found.
258 static struct in6_mfilter *
259 im6o_match_group(const struct ip6_moptions *imo, const struct ifnet *ifp,
260 const struct sockaddr *group)
262 const struct sockaddr_in6 *gsin6;
263 struct in6_mfilter *imf;
264 struct in6_multi *inm;
266 gsin6 = (const struct sockaddr_in6 *)group;
268 IP6_MFILTER_FOREACH(imf, &imo->im6o_head) {
269 inm = imf->im6f_in6m;
272 if ((ifp == NULL || (inm->in6m_ifp == ifp)) &&
273 IN6_ARE_ADDR_EQUAL(&inm->in6m_addr,
274 &gsin6->sin6_addr)) {
282 * Find an IPv6 multicast source entry for this imo which matches
283 * the given group index for this socket, and source address.
285 * XXX TODO: The scope ID, if present in src, is stripped before
286 * any comparison. We SHOULD enforce scope/zone checks where the source
287 * filter entry has a link scope.
289 * NOTE: This does not check if the entry is in-mode, merely if
290 * it exists, which may not be the desired behaviour.
292 static struct in6_msource *
293 im6o_match_source(struct in6_mfilter *imf, const struct sockaddr *src)
295 struct ip6_msource find;
296 struct ip6_msource *ims;
297 const sockunion_t *psa;
299 KASSERT(src->sa_family == AF_INET6, ("%s: !AF_INET6", __func__));
301 psa = (const sockunion_t *)src;
302 find.im6s_addr = psa->sin6.sin6_addr;
303 in6_clearscope(&find.im6s_addr); /* XXX */
304 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
306 return ((struct in6_msource *)ims);
310 * Perform filtering for multicast datagrams on a socket by group and source.
312 * Returns 0 if a datagram should be allowed through, or various error codes
313 * if the socket was not a member of the group, or the source was muted, etc.
316 im6o_mc_filter(const struct ip6_moptions *imo, const struct ifnet *ifp,
317 const struct sockaddr *group, const struct sockaddr *src)
319 struct in6_mfilter *imf;
320 struct in6_msource *ims;
323 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
325 imf = im6o_match_group(imo, ifp, group);
327 return (MCAST_NOTGMEMBER);
330 * Check if the source was included in an (S,G) join.
331 * Allow reception on exclusive memberships by default,
332 * reject reception on inclusive memberships by default.
333 * Exclude source only if an in-mode exclude filter exists.
334 * Include source only if an in-mode include filter exists.
335 * NOTE: We are comparing group state here at MLD t1 (now)
336 * with socket-layer t0 (since last downcall).
338 mode = imf->im6f_st[1];
339 ims = im6o_match_source(imf, src);
341 if ((ims == NULL && mode == MCAST_INCLUDE) ||
342 (ims != NULL && ims->im6sl_st[0] != mode))
343 return (MCAST_NOTSMEMBER);
349 * Find and return a reference to an in6_multi record for (ifp, group),
350 * and bump its reference count.
351 * If one does not exist, try to allocate it, and update link-layer multicast
352 * filters on ifp to listen for group.
353 * Assumes the IN6_MULTI lock is held across the call.
354 * Return 0 if successful, otherwise return an appropriate error code.
357 in6_getmulti(struct ifnet *ifp, const struct in6_addr *group,
358 struct in6_multi **pinm)
360 struct epoch_tracker et;
361 struct sockaddr_in6 gsin6;
362 struct ifmultiaddr *ifma;
363 struct in6_multi *inm;
369 * XXX: Accesses to ifma_protospec must be covered by IF_ADDR_LOCK;
370 * if_addmulti() takes this mutex itself, so we must drop and
371 * re-acquire around the call.
373 IN6_MULTI_LOCK_ASSERT();
374 IN6_MULTI_LIST_LOCK();
378 * Does ifp support IPv6 multicasts?
380 if (ifp->if_afdata[AF_INET6] == NULL)
383 inm = in6m_lookup_locked(ifp, group);
391 * If we already joined this group, just bump the
392 * refcount and return it.
394 KASSERT(inm->in6m_refcount >= 1,
395 ("%s: bad refcount %d", __func__, inm->in6m_refcount));
396 in6m_acquire_locked(inm);
401 memset(&gsin6, 0, sizeof(gsin6));
402 gsin6.sin6_family = AF_INET6;
403 gsin6.sin6_len = sizeof(struct sockaddr_in6);
404 gsin6.sin6_addr = *group;
407 * Check if a link-layer group is already associated
408 * with this network-layer group on the given ifnet.
410 IN6_MULTI_LIST_UNLOCK();
411 IF_ADDR_WUNLOCK(ifp);
412 error = if_addmulti(ifp, (struct sockaddr *)&gsin6, &ifma);
415 IN6_MULTI_LIST_LOCK();
419 * If something other than netinet6 is occupying the link-layer
420 * group, print a meaningful error message and back out of
422 * Otherwise, bump the refcount on the existing network-layer
423 * group association and return it.
425 if (ifma->ifma_protospec != NULL) {
426 inm = (struct in6_multi *)ifma->ifma_protospec;
428 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
430 KASSERT(ifma->ifma_addr->sa_family == AF_INET6,
431 ("%s: ifma not AF_INET6", __func__));
432 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
433 if (inm->in6m_ifma != ifma || inm->in6m_ifp != ifp ||
434 !IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, group))
435 panic("%s: ifma %p is inconsistent with %p (%p)",
436 __func__, ifma, inm, group);
438 in6m_acquire_locked(inm);
443 IF_ADDR_WLOCK_ASSERT(ifp);
446 * A new in6_multi record is needed; allocate and initialize it.
447 * We DO NOT perform an MLD join as the in6_ layer may need to
448 * push an initial source list down to MLD to support SSM.
450 * The initial source filter state is INCLUDE, {} as per the RFC.
451 * Pending state-changes per group are subject to a bounds check.
453 inm = malloc(sizeof(*inm), M_IP6MADDR, M_NOWAIT | M_ZERO);
455 IN6_MULTI_LIST_UNLOCK();
456 IF_ADDR_WUNLOCK(ifp);
457 if_delmulti_ifma(ifma);
460 inm->in6m_addr = *group;
462 inm->in6m_mli = MLD_IFINFO(ifp);
463 inm->in6m_ifma = ifma;
464 inm->in6m_refcount = 1;
465 inm->in6m_state = MLD_NOT_MEMBER;
466 mbufq_init(&inm->in6m_scq, MLD_MAX_STATE_CHANGES);
468 inm->in6m_st[0].iss_fmode = MCAST_UNDEFINED;
469 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
470 RB_INIT(&inm->in6m_srcs);
472 ifma->ifma_protospec = inm;
476 IN6_MULTI_LIST_UNLOCK();
477 IF_ADDR_WUNLOCK(ifp);
482 * Drop a reference to an in6_multi record.
484 * If the refcount drops to 0, free the in6_multi record and
485 * delete the underlying link-layer membership.
488 in6m_release(struct in6_multi *inm)
490 struct ifmultiaddr *ifma;
493 CTR2(KTR_MLD, "%s: refcount is %d", __func__, inm->in6m_refcount);
495 MPASS(inm->in6m_refcount == 0);
496 CTR2(KTR_MLD, "%s: freeing inm %p", __func__, inm);
498 ifma = inm->in6m_ifma;
500 MPASS(ifma->ifma_llifma == NULL);
502 /* XXX this access is not covered by IF_ADDR_LOCK */
503 CTR2(KTR_MLD, "%s: purging ifma %p", __func__, ifma);
504 KASSERT(ifma->ifma_protospec == NULL,
505 ("%s: ifma_protospec != NULL", __func__));
507 ifp = ifma->ifma_ifp;
510 CURVNET_SET(ifp->if_vnet);
512 free(inm, M_IP6MADDR);
513 if_delmulti_ifma_flags(ifma, 1);
518 free(inm, M_IP6MADDR);
519 if_delmulti_ifma_flags(ifma, 1);
524 * Interface detach can happen in a taskqueue thread context, so we must use a
525 * dedicated thread to avoid deadlocks when draining in6m_release tasks.
527 TASKQUEUE_DEFINE_THREAD(in6m_free);
528 static struct in6_multi_head in6m_free_list = SLIST_HEAD_INITIALIZER();
529 static void in6m_release_task(void *arg __unused, int pending __unused);
530 static struct task in6m_free_task = TASK_INITIALIZER(0, in6m_release_task, NULL);
533 in6m_release_list_deferred(struct in6_multi_head *inmh)
535 if (SLIST_EMPTY(inmh))
537 mtx_lock(&in6_multi_free_mtx);
538 SLIST_CONCAT(&in6m_free_list, inmh, in6_multi, in6m_nrele);
539 mtx_unlock(&in6_multi_free_mtx);
540 taskqueue_enqueue(taskqueue_in6m_free, &in6m_free_task);
544 in6m_release_wait(void *arg __unused)
548 * Make sure all pending multicast addresses are freed before
549 * the VNET or network device is destroyed:
551 taskqueue_drain_all(taskqueue_in6m_free);
554 /* XXX-BZ FIXME, see D24914. */
555 VNET_SYSUNINIT(in6m_release_wait, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, in6m_release_wait, NULL);
559 in6m_disconnect_locked(struct in6_multi_head *inmh, struct in6_multi *inm)
563 struct in6_ifaddr *ifa6;
564 struct in6_multi_mship *imm, *imm_tmp;
565 struct ifmultiaddr *ifma, *ll_ifma;
567 IN6_MULTI_LIST_LOCK_ASSERT();
571 return; /* already called */
573 inm->in6m_ifp = NULL;
574 IF_ADDR_WLOCK_ASSERT(ifp);
575 ifma = inm->in6m_ifma;
580 if (ifma->ifma_flags & IFMA_F_ENQUEUED) {
581 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
582 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
584 MCDPRINTF("removed ifma: %p from %s\n", ifma, ifp->if_xname);
585 if ((ll_ifma = ifma->ifma_llifma) != NULL) {
586 MPASS(ifma != ll_ifma);
587 ifma->ifma_llifma = NULL;
588 MPASS(ll_ifma->ifma_llifma == NULL);
589 MPASS(ll_ifma->ifma_ifp == ifp);
590 if (--ll_ifma->ifma_refcount == 0) {
591 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
592 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, ifma_link);
593 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
595 MCDPRINTF("removed ll_ifma: %p from %s\n", ll_ifma, ifp->if_xname);
596 if_freemulti(ll_ifma);
599 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
600 if (ifa->ifa_addr->sa_family != AF_INET6)
603 LIST_FOREACH_SAFE(imm, &ifa6->ia6_memberships,
604 i6mm_chain, imm_tmp) {
605 if (inm == imm->i6mm_maddr) {
606 LIST_REMOVE(imm, i6mm_chain);
607 free(imm, M_IP6MADDR);
608 in6m_rele_locked(inmh, inm);
615 in6m_release_task(void *arg __unused, int pending __unused)
617 struct in6_multi_head in6m_free_tmp;
618 struct in6_multi *inm, *tinm;
620 SLIST_INIT(&in6m_free_tmp);
621 mtx_lock(&in6_multi_free_mtx);
622 SLIST_CONCAT(&in6m_free_tmp, &in6m_free_list, in6_multi, in6m_nrele);
623 mtx_unlock(&in6_multi_free_mtx);
625 SLIST_FOREACH_SAFE(inm, &in6m_free_tmp, in6m_nrele, tinm) {
626 SLIST_REMOVE_HEAD(&in6m_free_tmp, in6m_nrele);
633 * Clear recorded source entries for a group.
634 * Used by the MLD code. Caller must hold the IN6_MULTI lock.
635 * FIXME: Should reap.
638 in6m_clear_recorded(struct in6_multi *inm)
640 struct ip6_msource *ims;
642 IN6_MULTI_LIST_LOCK_ASSERT();
644 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
647 --inm->in6m_st[1].iss_rec;
650 KASSERT(inm->in6m_st[1].iss_rec == 0,
651 ("%s: iss_rec %d not 0", __func__, inm->in6m_st[1].iss_rec));
655 * Record a source as pending for a Source-Group MLDv2 query.
656 * This lives here as it modifies the shared tree.
658 * inm is the group descriptor.
659 * naddr is the address of the source to record in network-byte order.
661 * If the net.inet6.mld.sgalloc sysctl is non-zero, we will
662 * lazy-allocate a source node in response to an SG query.
663 * Otherwise, no allocation is performed. This saves some memory
664 * with the trade-off that the source will not be reported to the
665 * router if joined in the window between the query response and
666 * the group actually being joined on the local host.
668 * VIMAGE: XXX: Currently the mld_sgalloc feature has been removed.
669 * This turns off the allocation of a recorded source entry if
670 * the group has not been joined.
672 * Return 0 if the source didn't exist or was already marked as recorded.
673 * Return 1 if the source was marked as recorded by this function.
674 * Return <0 if any error occurred (negated errno code).
677 in6m_record_source(struct in6_multi *inm, const struct in6_addr *addr)
679 struct ip6_msource find;
680 struct ip6_msource *ims, *nims;
682 IN6_MULTI_LIST_LOCK_ASSERT();
684 find.im6s_addr = *addr;
685 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
686 if (ims && ims->im6s_stp)
689 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
691 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
695 nims->im6s_addr = find.im6s_addr;
696 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
702 * Mark the source as recorded and update the recorded
706 ++inm->in6m_st[1].iss_rec;
712 * Return a pointer to an in6_msource owned by an in6_mfilter,
713 * given its source address.
714 * Lazy-allocate if needed. If this is a new entry its filter state is
717 * imf is the filter set being modified.
718 * addr is the source address.
720 * SMPng: May be called with locks held; malloc must not block.
723 im6f_get_source(struct in6_mfilter *imf, const struct sockaddr_in6 *psin,
724 struct in6_msource **plims)
726 struct ip6_msource find;
727 struct ip6_msource *ims, *nims;
728 struct in6_msource *lims;
735 find.im6s_addr = psin->sin6_addr;
736 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
737 lims = (struct in6_msource *)ims;
739 if (imf->im6f_nsrc == in6_mcast_maxsocksrc)
741 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
745 lims = (struct in6_msource *)nims;
746 lims->im6s_addr = find.im6s_addr;
747 lims->im6sl_st[0] = MCAST_UNDEFINED;
748 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
758 * Graft a source entry into an existing socket-layer filter set,
759 * maintaining any required invariants and checking allocations.
761 * The source is marked as being in the new filter mode at t1.
763 * Return the pointer to the new node, otherwise return NULL.
765 static struct in6_msource *
766 im6f_graft(struct in6_mfilter *imf, const uint8_t st1,
767 const struct sockaddr_in6 *psin)
769 struct ip6_msource *nims;
770 struct in6_msource *lims;
772 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
776 lims = (struct in6_msource *)nims;
777 lims->im6s_addr = psin->sin6_addr;
778 lims->im6sl_st[0] = MCAST_UNDEFINED;
779 lims->im6sl_st[1] = st1;
780 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
787 * Prune a source entry from an existing socket-layer filter set,
788 * maintaining any required invariants and checking allocations.
790 * The source is marked as being left at t1, it is not freed.
792 * Return 0 if no error occurred, otherwise return an errno value.
795 im6f_prune(struct in6_mfilter *imf, const struct sockaddr_in6 *psin)
797 struct ip6_msource find;
798 struct ip6_msource *ims;
799 struct in6_msource *lims;
801 find.im6s_addr = psin->sin6_addr;
802 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
805 lims = (struct in6_msource *)ims;
806 lims->im6sl_st[1] = MCAST_UNDEFINED;
811 * Revert socket-layer filter set deltas at t1 to t0 state.
814 im6f_rollback(struct in6_mfilter *imf)
816 struct ip6_msource *ims, *tims;
817 struct in6_msource *lims;
819 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
820 lims = (struct in6_msource *)ims;
821 if (lims->im6sl_st[0] == lims->im6sl_st[1]) {
822 /* no change at t1 */
824 } else if (lims->im6sl_st[0] != MCAST_UNDEFINED) {
825 /* revert change to existing source at t1 */
826 lims->im6sl_st[1] = lims->im6sl_st[0];
828 /* revert source added t1 */
829 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
830 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
831 free(ims, M_IN6MFILTER);
835 imf->im6f_st[1] = imf->im6f_st[0];
839 * Mark socket-layer filter set as INCLUDE {} at t1.
842 im6f_leave(struct in6_mfilter *imf)
844 struct ip6_msource *ims;
845 struct in6_msource *lims;
847 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
848 lims = (struct in6_msource *)ims;
849 lims->im6sl_st[1] = MCAST_UNDEFINED;
851 imf->im6f_st[1] = MCAST_INCLUDE;
855 * Mark socket-layer filter set deltas as committed.
858 im6f_commit(struct in6_mfilter *imf)
860 struct ip6_msource *ims;
861 struct in6_msource *lims;
863 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
864 lims = (struct in6_msource *)ims;
865 lims->im6sl_st[0] = lims->im6sl_st[1];
867 imf->im6f_st[0] = imf->im6f_st[1];
871 * Reap unreferenced sources from socket-layer filter set.
874 im6f_reap(struct in6_mfilter *imf)
876 struct ip6_msource *ims, *tims;
877 struct in6_msource *lims;
879 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
880 lims = (struct in6_msource *)ims;
881 if ((lims->im6sl_st[0] == MCAST_UNDEFINED) &&
882 (lims->im6sl_st[1] == MCAST_UNDEFINED)) {
883 CTR2(KTR_MLD, "%s: free lims %p", __func__, ims);
884 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
885 free(ims, M_IN6MFILTER);
892 * Purge socket-layer filter set.
895 im6f_purge(struct in6_mfilter *imf)
897 struct ip6_msource *ims, *tims;
899 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
900 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
901 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
902 free(ims, M_IN6MFILTER);
905 imf->im6f_st[0] = imf->im6f_st[1] = MCAST_UNDEFINED;
906 KASSERT(RB_EMPTY(&imf->im6f_sources),
907 ("%s: im6f_sources not empty", __func__));
911 * Look up a source filter entry for a multicast group.
913 * inm is the group descriptor to work with.
914 * addr is the IPv6 address to look up.
915 * noalloc may be non-zero to suppress allocation of sources.
916 * *pims will be set to the address of the retrieved or allocated source.
918 * SMPng: NOTE: may be called with locks held.
919 * Return 0 if successful, otherwise return a non-zero error code.
922 in6m_get_source(struct in6_multi *inm, const struct in6_addr *addr,
923 const int noalloc, struct ip6_msource **pims)
925 struct ip6_msource find;
926 struct ip6_msource *ims, *nims;
928 char ip6tbuf[INET6_ADDRSTRLEN];
931 find.im6s_addr = *addr;
932 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
933 if (ims == NULL && !noalloc) {
934 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
936 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
940 nims->im6s_addr = *addr;
941 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
944 CTR3(KTR_MLD, "%s: allocated %s as %p", __func__,
945 ip6_sprintf(ip6tbuf, addr), ims);
953 * Merge socket-layer source into MLD-layer source.
954 * If rollback is non-zero, perform the inverse of the merge.
957 im6s_merge(struct ip6_msource *ims, const struct in6_msource *lims,
960 int n = rollback ? -1 : 1;
962 char ip6tbuf[INET6_ADDRSTRLEN];
964 ip6_sprintf(ip6tbuf, &lims->im6s_addr);
967 if (lims->im6sl_st[0] == MCAST_EXCLUDE) {
968 CTR3(KTR_MLD, "%s: t1 ex -= %d on %s", __func__, n, ip6tbuf);
969 ims->im6s_st[1].ex -= n;
970 } else if (lims->im6sl_st[0] == MCAST_INCLUDE) {
971 CTR3(KTR_MLD, "%s: t1 in -= %d on %s", __func__, n, ip6tbuf);
972 ims->im6s_st[1].in -= n;
975 if (lims->im6sl_st[1] == MCAST_EXCLUDE) {
976 CTR3(KTR_MLD, "%s: t1 ex += %d on %s", __func__, n, ip6tbuf);
977 ims->im6s_st[1].ex += n;
978 } else if (lims->im6sl_st[1] == MCAST_INCLUDE) {
979 CTR3(KTR_MLD, "%s: t1 in += %d on %s", __func__, n, ip6tbuf);
980 ims->im6s_st[1].in += n;
985 * Atomically update the global in6_multi state, when a membership's
986 * filter list is being updated in any way.
988 * imf is the per-inpcb-membership group filter pointer.
989 * A fake imf may be passed for in-kernel consumers.
991 * XXX This is a candidate for a set-symmetric-difference style loop
992 * which would eliminate the repeated lookup from root of ims nodes,
993 * as they share the same key space.
995 * If any error occurred this function will back out of refcounts
996 * and return a non-zero value.
999 in6m_merge(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1001 struct ip6_msource *ims, *nims;
1002 struct in6_msource *lims;
1003 int schanged, error;
1009 IN6_MULTI_LIST_LOCK_ASSERT();
1012 * Update the source filters first, as this may fail.
1013 * Maintain count of in-mode filters at t0, t1. These are
1014 * used to work out if we transition into ASM mode or not.
1015 * Maintain a count of source filters whose state was
1016 * actually modified by this operation.
1018 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
1019 lims = (struct in6_msource *)ims;
1020 if (lims->im6sl_st[0] == imf->im6f_st[0]) nsrc0++;
1021 if (lims->im6sl_st[1] == imf->im6f_st[1]) nsrc1++;
1022 if (lims->im6sl_st[0] == lims->im6sl_st[1]) continue;
1023 error = in6m_get_source(inm, &lims->im6s_addr, 0, &nims);
1027 im6s_merge(nims, lims, 0);
1030 struct ip6_msource *bims;
1032 RB_FOREACH_REVERSE_FROM(ims, ip6_msource_tree, nims) {
1033 lims = (struct in6_msource *)ims;
1034 if (lims->im6sl_st[0] == lims->im6sl_st[1])
1036 (void)in6m_get_source(inm, &lims->im6s_addr, 1, &bims);
1039 im6s_merge(bims, lims, 1);
1044 CTR3(KTR_MLD, "%s: imf filters in-mode: %d at t0, %d at t1",
1045 __func__, nsrc0, nsrc1);
1047 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1048 if (imf->im6f_st[0] == imf->im6f_st[1] &&
1049 imf->im6f_st[1] == MCAST_INCLUDE) {
1051 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
1052 --inm->in6m_st[1].iss_in;
1056 /* Handle filter mode transition on socket. */
1057 if (imf->im6f_st[0] != imf->im6f_st[1]) {
1058 CTR3(KTR_MLD, "%s: imf transition %d to %d",
1059 __func__, imf->im6f_st[0], imf->im6f_st[1]);
1061 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
1062 CTR1(KTR_MLD, "%s: --ex on inm at t1", __func__);
1063 --inm->in6m_st[1].iss_ex;
1064 } else if (imf->im6f_st[0] == MCAST_INCLUDE) {
1065 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
1066 --inm->in6m_st[1].iss_in;
1069 if (imf->im6f_st[1] == MCAST_EXCLUDE) {
1070 CTR1(KTR_MLD, "%s: ex++ on inm at t1", __func__);
1071 inm->in6m_st[1].iss_ex++;
1072 } else if (imf->im6f_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
1073 CTR1(KTR_MLD, "%s: in++ on inm at t1", __func__);
1074 inm->in6m_st[1].iss_in++;
1079 * Track inm filter state in terms of listener counts.
1080 * If there are any exclusive listeners, stack-wide
1081 * membership is exclusive.
1082 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1083 * If no listeners remain, state is undefined at t1,
1084 * and the MLD lifecycle for this group should finish.
1086 if (inm->in6m_st[1].iss_ex > 0) {
1087 CTR1(KTR_MLD, "%s: transition to EX", __func__);
1088 inm->in6m_st[1].iss_fmode = MCAST_EXCLUDE;
1089 } else if (inm->in6m_st[1].iss_in > 0) {
1090 CTR1(KTR_MLD, "%s: transition to IN", __func__);
1091 inm->in6m_st[1].iss_fmode = MCAST_INCLUDE;
1093 CTR1(KTR_MLD, "%s: transition to UNDEF", __func__);
1094 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
1097 /* Decrement ASM listener count on transition out of ASM mode. */
1098 if (imf->im6f_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1099 if ((imf->im6f_st[1] != MCAST_EXCLUDE) ||
1100 (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 > 0)) {
1101 CTR1(KTR_MLD, "%s: --asm on inm at t1", __func__);
1102 --inm->in6m_st[1].iss_asm;
1106 /* Increment ASM listener count on transition to ASM mode. */
1107 if (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1108 CTR1(KTR_MLD, "%s: asm++ on inm at t1", __func__);
1109 inm->in6m_st[1].iss_asm++;
1112 CTR3(KTR_MLD, "%s: merged imf %p to inm %p", __func__, imf, inm);
1117 CTR1(KTR_MLD, "%s: sources changed; reaping", __func__);
1124 * Mark an in6_multi's filter set deltas as committed.
1125 * Called by MLD after a state change has been enqueued.
1128 in6m_commit(struct in6_multi *inm)
1130 struct ip6_msource *ims;
1132 CTR2(KTR_MLD, "%s: commit inm %p", __func__, inm);
1133 CTR1(KTR_MLD, "%s: pre commit:", __func__);
1136 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
1137 ims->im6s_st[0] = ims->im6s_st[1];
1139 inm->in6m_st[0] = inm->in6m_st[1];
1143 * Reap unreferenced nodes from an in6_multi's filter set.
1146 in6m_reap(struct in6_multi *inm)
1148 struct ip6_msource *ims, *tims;
1150 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1151 if (ims->im6s_st[0].ex > 0 || ims->im6s_st[0].in > 0 ||
1152 ims->im6s_st[1].ex > 0 || ims->im6s_st[1].in > 0 ||
1155 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1156 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1157 free(ims, M_IP6MSOURCE);
1163 * Purge all source nodes from an in6_multi's filter set.
1166 in6m_purge(struct in6_multi *inm)
1168 struct ip6_msource *ims, *tims;
1170 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1171 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1172 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1173 free(ims, M_IP6MSOURCE);
1176 /* Free state-change requests that might be queued. */
1177 mbufq_drain(&inm->in6m_scq);
1181 * Join a multicast address w/o sources.
1182 * KAME compatibility entry point.
1184 * SMPng: Assume no mc locks held by caller.
1187 in6_joingroup(struct ifnet *ifp, const struct in6_addr *mcaddr,
1188 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1194 error = in6_joingroup_locked(ifp, mcaddr, NULL, pinm, delay);
1200 * Join a multicast group; real entry point.
1202 * Only preserves atomicity at inm level.
1203 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1205 * If the MLD downcall fails, the group is not joined, and an error
1209 in6_joingroup_locked(struct ifnet *ifp, const struct in6_addr *mcaddr,
1210 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1213 struct in6_multi_head inmh;
1214 struct in6_mfilter timf;
1215 struct in6_multi *inm;
1216 struct ifmultiaddr *ifma;
1219 char ip6tbuf[INET6_ADDRSTRLEN];
1223 * Sanity: Check scope zone ID was set for ifp, if and
1224 * only if group is scoped to an interface.
1226 KASSERT(IN6_IS_ADDR_MULTICAST(mcaddr),
1227 ("%s: not a multicast address", __func__));
1228 if (IN6_IS_ADDR_MC_LINKLOCAL(mcaddr) ||
1229 IN6_IS_ADDR_MC_INTFACELOCAL(mcaddr)) {
1230 KASSERT(mcaddr->s6_addr16[1] != 0,
1231 ("%s: scope zone ID not set", __func__));
1234 IN6_MULTI_LOCK_ASSERT();
1235 IN6_MULTI_LIST_UNLOCK_ASSERT();
1237 CTR4(KTR_MLD, "%s: join %s on %p(%s))", __func__,
1238 ip6_sprintf(ip6tbuf, mcaddr), ifp, if_name(ifp));
1244 * If no imf was specified (i.e. kernel consumer),
1245 * fake one up and assume it is an ASM join.
1248 im6f_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1251 error = in6_getmulti(ifp, mcaddr, &inm);
1253 CTR1(KTR_MLD, "%s: in6_getmulti() failure", __func__);
1257 IN6_MULTI_LIST_LOCK();
1258 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1259 error = in6m_merge(inm, imf);
1261 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1262 goto out_in6m_release;
1265 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1266 error = mld_change_state(inm, delay);
1268 CTR1(KTR_MLD, "%s: failed to update source", __func__);
1269 goto out_in6m_release;
1275 struct epoch_tracker et;
1277 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1279 NET_EPOCH_ENTER(et);
1280 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1281 if (ifma->ifma_protospec == inm) {
1282 ifma->ifma_protospec = NULL;
1286 in6m_disconnect_locked(&inmh, inm);
1287 in6m_rele_locked(&inmh, inm);
1289 IF_ADDR_WUNLOCK(ifp);
1293 IN6_MULTI_LIST_UNLOCK();
1294 in6m_release_list_deferred(&inmh);
1299 * Leave a multicast group; unlocked entry point.
1302 in6_leavegroup(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1307 error = in6_leavegroup_locked(inm, imf);
1313 * Leave a multicast group; real entry point.
1314 * All source filters will be expunged.
1316 * Only preserves atomicity at inm level.
1318 * Holding the write lock for the INP which contains imf
1319 * is highly advisable. We can't assert for it as imf does not
1320 * contain a back-pointer to the owning inp.
1322 * Note: This is not the same as in6m_release(*) as this function also
1323 * makes a state change downcall into MLD.
1326 in6_leavegroup_locked(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1328 struct in6_multi_head inmh;
1329 struct in6_mfilter timf;
1333 char ip6tbuf[INET6_ADDRSTRLEN];
1338 IN6_MULTI_LOCK_ASSERT();
1340 CTR5(KTR_MLD, "%s: leave inm %p, %s/%s, imf %p", __func__,
1341 inm, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1342 (in6m_is_ifp_detached(inm) ? "null" : if_name(inm->in6m_ifp)),
1346 * If no imf was specified (i.e. kernel consumer),
1347 * fake one up and assume it is an ASM join.
1350 im6f_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1355 * Begin state merge transaction at MLD layer.
1357 * As this particular invocation should not cause any memory
1358 * to be allocated, and there is no opportunity to roll back
1359 * the transaction, it MUST NOT fail.
1362 ifp = inm->in6m_ifp;
1363 IN6_MULTI_LIST_LOCK();
1364 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1365 error = in6m_merge(inm, imf);
1366 KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1368 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1371 error = mld_change_state(inm, 0);
1373 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1375 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1380 if (inm->in6m_refcount == 1)
1381 in6m_disconnect_locked(&inmh, inm);
1382 in6m_rele_locked(&inmh, inm);
1384 IF_ADDR_WUNLOCK(ifp);
1385 IN6_MULTI_LIST_UNLOCK();
1386 in6m_release_list_deferred(&inmh);
1391 * Block or unblock an ASM multicast source on an inpcb.
1392 * This implements the delta-based API described in RFC 3678.
1394 * The delta-based API applies only to exclusive-mode memberships.
1395 * An MLD downcall will be performed.
1397 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1399 * Return 0 if successful, otherwise return an appropriate error code.
1402 in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1404 struct group_source_req gsr;
1405 struct epoch_tracker et;
1406 sockunion_t *gsa, *ssa;
1408 struct in6_mfilter *imf;
1409 struct ip6_moptions *imo;
1410 struct in6_msource *ims;
1411 struct in6_multi *inm;
1415 char ip6tbuf[INET6_ADDRSTRLEN];
1422 memset(&gsr, 0, sizeof(struct group_source_req));
1423 gsa = (sockunion_t *)&gsr.gsr_group;
1424 ssa = (sockunion_t *)&gsr.gsr_source;
1426 switch (sopt->sopt_name) {
1427 case MCAST_BLOCK_SOURCE:
1428 case MCAST_UNBLOCK_SOURCE:
1429 error = sooptcopyin(sopt, &gsr,
1430 sizeof(struct group_source_req),
1431 sizeof(struct group_source_req));
1435 if (gsa->sin6.sin6_family != AF_INET6 ||
1436 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1439 if (ssa->sin6.sin6_family != AF_INET6 ||
1440 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1444 * XXXGL: this function should use ifnet_byindex_ref, or
1445 * expand the epoch section all the way to where we put
1448 NET_EPOCH_ENTER(et);
1449 ifp = ifnet_byindex(gsr.gsr_interface);
1452 return (EADDRNOTAVAIL);
1454 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1459 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1460 __func__, sopt->sopt_name);
1461 return (EOPNOTSUPP);
1465 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1468 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1471 * Check if we are actually a member of this group.
1473 imo = in6p_findmoptions(inp);
1474 imf = im6o_match_group(imo, ifp, &gsa->sa);
1476 error = EADDRNOTAVAIL;
1477 goto out_in6p_locked;
1479 inm = imf->im6f_in6m;
1482 * Attempting to use the delta-based API on an
1483 * non exclusive-mode membership is an error.
1485 fmode = imf->im6f_st[0];
1486 if (fmode != MCAST_EXCLUDE) {
1488 goto out_in6p_locked;
1492 * Deal with error cases up-front:
1493 * Asked to block, but already blocked; or
1494 * Asked to unblock, but nothing to unblock.
1495 * If adding a new block entry, allocate it.
1497 ims = im6o_match_source(imf, &ssa->sa);
1498 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1499 CTR3(KTR_MLD, "%s: source %s %spresent", __func__,
1500 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
1501 doblock ? "" : "not ");
1502 error = EADDRNOTAVAIL;
1503 goto out_in6p_locked;
1506 INP_WLOCK_ASSERT(inp);
1509 * Begin state merge transaction at socket layer.
1512 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
1513 ims = im6f_graft(imf, fmode, &ssa->sin6);
1517 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
1518 error = im6f_prune(imf, &ssa->sin6);
1522 CTR1(KTR_MLD, "%s: merge imf state failed", __func__);
1523 goto out_im6f_rollback;
1527 * Begin state merge transaction at MLD layer.
1529 IN6_MULTI_LIST_LOCK();
1530 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1531 error = in6m_merge(inm, imf);
1533 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1535 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1536 error = mld_change_state(inm, 0);
1538 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1541 IN6_MULTI_LIST_UNLOCK();
1557 * Given an inpcb, return its multicast options structure pointer. Accepts
1558 * an unlocked inpcb pointer, but will return it locked. May sleep.
1560 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1561 * SMPng: NOTE: Returns with the INP write lock held.
1563 static struct ip6_moptions *
1564 in6p_findmoptions(struct inpcb *inp)
1566 struct ip6_moptions *imo;
1569 if (inp->in6p_moptions != NULL)
1570 return (inp->in6p_moptions);
1574 imo = malloc(sizeof(*imo), M_IP6MOPTS, M_WAITOK);
1576 imo->im6o_multicast_ifp = NULL;
1577 imo->im6o_multicast_hlim = V_ip6_defmcasthlim;
1578 imo->im6o_multicast_loop = in6_mcast_loop;
1579 STAILQ_INIT(&imo->im6o_head);
1582 if (inp->in6p_moptions != NULL) {
1583 free(imo, M_IP6MOPTS);
1584 return (inp->in6p_moptions);
1586 inp->in6p_moptions = imo;
1591 * Discard the IPv6 multicast options (and source filters).
1593 * SMPng: NOTE: assumes INP write lock is held.
1595 * XXX can all be safely deferred to epoch_call
1600 inp_gcmoptions(struct ip6_moptions *imo)
1602 struct in6_mfilter *imf;
1603 struct in6_multi *inm;
1606 while ((imf = ip6_mfilter_first(&imo->im6o_head)) != NULL) {
1607 ip6_mfilter_remove(&imo->im6o_head, imf);
1610 if ((inm = imf->im6f_in6m) != NULL) {
1611 if ((ifp = inm->in6m_ifp) != NULL) {
1612 CURVNET_SET(ifp->if_vnet);
1613 (void)in6_leavegroup(inm, imf);
1616 (void)in6_leavegroup(inm, imf);
1619 ip6_mfilter_free(imf);
1621 free(imo, M_IP6MOPTS);
1625 ip6_freemoptions(struct ip6_moptions *imo)
1629 inp_gcmoptions(imo);
1633 * Atomically get source filters on a socket for an IPv6 multicast group.
1634 * Called with INP lock held; returns with lock released.
1637 in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1639 struct epoch_tracker et;
1640 struct __msfilterreq msfr;
1643 struct ip6_moptions *imo;
1644 struct in6_mfilter *imf;
1645 struct ip6_msource *ims;
1646 struct in6_msource *lims;
1647 struct sockaddr_in6 *psin;
1648 struct sockaddr_storage *ptss;
1649 struct sockaddr_storage *tss;
1651 size_t nsrcs, ncsrcs;
1653 INP_WLOCK_ASSERT(inp);
1655 imo = inp->in6p_moptions;
1656 KASSERT(imo != NULL, ("%s: null ip6_moptions", __func__));
1660 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1661 sizeof(struct __msfilterreq));
1665 if (msfr.msfr_group.ss_family != AF_INET6 ||
1666 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
1669 gsa = (sockunion_t *)&msfr.msfr_group;
1670 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1674 * XXXGL: this function should use ifnet_byindex_ref, or expand the
1675 * epoch section all the way to where the interface is referenced.
1677 NET_EPOCH_ENTER(et);
1678 ifp = ifnet_byindex(msfr.msfr_ifindex);
1681 return (EADDRNOTAVAIL);
1682 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1687 * Lookup group on the socket.
1689 imf = im6o_match_group(imo, ifp, &gsa->sa);
1692 return (EADDRNOTAVAIL);
1696 * Ignore memberships which are in limbo.
1698 if (imf->im6f_st[1] == MCAST_UNDEFINED) {
1702 msfr.msfr_fmode = imf->im6f_st[1];
1705 * If the user specified a buffer, copy out the source filter
1706 * entries to userland gracefully.
1707 * We only copy out the number of entries which userland
1708 * has asked for, but we always tell userland how big the
1709 * buffer really needs to be.
1711 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
1712 msfr.msfr_nsrcs = in6_mcast_maxsocksrc;
1714 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1715 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1716 M_TEMP, M_NOWAIT | M_ZERO);
1724 * Count number of sources in-mode at t0.
1725 * If buffer space exists and remains, copy out source entries.
1727 nsrcs = msfr.msfr_nsrcs;
1730 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
1731 lims = (struct in6_msource *)ims;
1732 if (lims->im6sl_st[0] == MCAST_UNDEFINED ||
1733 lims->im6sl_st[0] != imf->im6f_st[0])
1736 if (tss != NULL && nsrcs > 0) {
1737 psin = (struct sockaddr_in6 *)ptss;
1738 psin->sin6_family = AF_INET6;
1739 psin->sin6_len = sizeof(struct sockaddr_in6);
1740 psin->sin6_addr = lims->im6s_addr;
1741 psin->sin6_port = 0;
1750 error = copyout(tss, msfr.msfr_srcs,
1751 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1757 msfr.msfr_nsrcs = ncsrcs;
1758 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1764 * Return the IP multicast options in response to user getsockopt().
1767 ip6_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1769 struct ip6_moptions *im6o;
1774 im6o = inp->in6p_moptions;
1776 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1777 * or is a divert socket, reject it.
1779 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1780 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1781 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
1783 return (EOPNOTSUPP);
1787 switch (sopt->sopt_name) {
1788 case IPV6_MULTICAST_IF:
1789 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) {
1792 optval = im6o->im6o_multicast_ifp->if_index;
1795 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1798 case IPV6_MULTICAST_HOPS:
1800 optval = V_ip6_defmcasthlim;
1802 optval = im6o->im6o_multicast_hlim;
1804 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1807 case IPV6_MULTICAST_LOOP:
1809 optval = in6_mcast_loop; /* XXX VIMAGE */
1811 optval = im6o->im6o_multicast_loop;
1813 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1818 error = EADDRNOTAVAIL;
1821 error = in6p_get_source_filters(inp, sopt);
1827 error = ENOPROTOOPT;
1831 INP_UNLOCK_ASSERT(inp);
1837 * Look up the ifnet to use for a multicast group membership,
1838 * given the address of an IPv6 group.
1840 * This routine exists to support legacy IPv6 multicast applications.
1842 * Use the socket's current FIB number for any required FIB lookup. Look up the
1843 * group address in the unicast FIB, and use its ifp; usually, this points to
1844 * the default next-hop. If the FIB lookup fails, return NULL.
1846 * FUTURE: Support multiple forwarding tables for IPv6.
1848 * Returns NULL if no ifp could be found.
1850 static struct ifnet *
1851 in6p_lookup_mcast_ifp(const struct inpcb *inp, const struct sockaddr_in6 *gsin6)
1853 struct nhop_object *nh;
1854 struct in6_addr dst;
1858 KASSERT(gsin6->sin6_family == AF_INET6,
1859 ("%s: not AF_INET6 group", __func__));
1861 in6_splitscope(&gsin6->sin6_addr, &dst, &scopeid);
1862 fibnum = inp->inp_inc.inc_fibnum;
1863 nh = fib6_lookup(fibnum, &dst, scopeid, 0, 0);
1865 return (nh ? nh->nh_ifp : NULL);
1869 * Join an IPv6 multicast group, possibly with a source.
1871 * FIXME: The KAME use of the unspecified address (::)
1872 * to join *all* multicast groups is currently unsupported.
1874 * XXXGL: this function multiple times uses ifnet_byindex() without
1875 * proper protection - staying in epoch, or putting reference on ifnet.
1878 in6p_join_group(struct inpcb *inp, struct sockopt *sopt)
1880 struct in6_multi_head inmh;
1881 struct group_source_req gsr;
1882 struct epoch_tracker et;
1883 sockunion_t *gsa, *ssa;
1885 struct in6_mfilter *imf;
1886 struct ip6_moptions *imo;
1887 struct in6_multi *inm;
1888 struct in6_msource *lims;
1896 memset(&gsr, 0, sizeof(struct group_source_req));
1897 gsa = (sockunion_t *)&gsr.gsr_group;
1898 gsa->ss.ss_family = AF_UNSPEC;
1899 ssa = (sockunion_t *)&gsr.gsr_source;
1900 ssa->ss.ss_family = AF_UNSPEC;
1903 * Chew everything into struct group_source_req.
1904 * Overwrite the port field if present, as the sockaddr
1905 * being copied in may be matched with a binary comparison.
1906 * Ignore passed-in scope ID.
1908 switch (sopt->sopt_name) {
1909 case IPV6_JOIN_GROUP: {
1910 struct ipv6_mreq mreq;
1912 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
1913 sizeof(struct ipv6_mreq));
1917 gsa->sin6.sin6_family = AF_INET6;
1918 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
1919 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
1921 if (mreq.ipv6mr_interface == 0) {
1922 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
1924 NET_EPOCH_ENTER(et);
1925 ifp = ifnet_byindex(mreq.ipv6mr_interface);
1928 return (EADDRNOTAVAIL);
1930 CTR3(KTR_MLD, "%s: ipv6mr_interface = %d, ifp = %p",
1931 __func__, mreq.ipv6mr_interface, ifp);
1934 case MCAST_JOIN_GROUP:
1935 case MCAST_JOIN_SOURCE_GROUP:
1936 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1937 error = sooptcopyin(sopt, &gsr,
1938 sizeof(struct group_req),
1939 sizeof(struct group_req));
1940 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1941 error = sooptcopyin(sopt, &gsr,
1942 sizeof(struct group_source_req),
1943 sizeof(struct group_source_req));
1948 if (gsa->sin6.sin6_family != AF_INET6 ||
1949 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1952 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1953 if (ssa->sin6.sin6_family != AF_INET6 ||
1954 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1956 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
1959 * TODO: Validate embedded scope ID in source
1960 * list entry against passed-in ifp, if and only
1961 * if source list filter entry is iface or node local.
1963 in6_clearscope(&ssa->sin6.sin6_addr);
1964 ssa->sin6.sin6_port = 0;
1965 ssa->sin6.sin6_scope_id = 0;
1967 NET_EPOCH_ENTER(et);
1968 ifp = ifnet_byindex(gsr.gsr_interface);
1971 return (EADDRNOTAVAIL);
1975 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1976 __func__, sopt->sopt_name);
1977 return (EOPNOTSUPP);
1981 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1984 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
1985 return (EADDRNOTAVAIL);
1987 gsa->sin6.sin6_port = 0;
1988 gsa->sin6.sin6_scope_id = 0;
1991 * Always set the scope zone ID on memberships created from userland.
1992 * Use the passed-in ifp to do this.
1993 * XXX The in6_setscope() return value is meaningless.
1994 * XXX SCOPE6_LOCK() is taken by in6_setscope().
1996 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2001 * Find the membership in the membership list.
2003 imo = in6p_findmoptions(inp);
2004 imf = im6o_match_group(imo, ifp, &gsa->sa);
2009 if (ip6_mfilter_count(&imo->im6o_head) >= IPV6_MAX_MEMBERSHIPS) {
2011 goto out_in6p_locked;
2015 inm = imf->im6f_in6m;
2017 if (ssa->ss.ss_family != AF_UNSPEC) {
2019 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2020 * is an error. On an existing inclusive membership,
2021 * it just adds the source to the filter list.
2023 if (imf->im6f_st[1] != MCAST_INCLUDE) {
2025 goto out_in6p_locked;
2028 * Throw out duplicates.
2030 * XXX FIXME: This makes a naive assumption that
2031 * even if entries exist for *ssa in this imf,
2032 * they will be rejected as dupes, even if they
2033 * are not valid in the current mode (in-mode).
2035 * in6_msource is transactioned just as for anything
2036 * else in SSM -- but note naive use of in6m_graft()
2037 * below for allocating new filter entries.
2039 * This is only an issue if someone mixes the
2040 * full-state SSM API with the delta-based API,
2041 * which is discouraged in the relevant RFCs.
2043 lims = im6o_match_source(imf, &ssa->sa);
2044 if (lims != NULL /*&&
2045 lims->im6sl_st[1] == MCAST_INCLUDE*/) {
2046 error = EADDRNOTAVAIL;
2047 goto out_in6p_locked;
2051 * MCAST_JOIN_GROUP alone, on any existing membership,
2052 * is rejected, to stop the same inpcb tying up
2053 * multiple refs to the in_multi.
2054 * On an existing inclusive membership, this is also
2055 * an error; if you want to change filter mode,
2056 * you must use the userland API setsourcefilter().
2057 * XXX We don't reject this for imf in UNDEFINED
2058 * state at t1, because allocation of a filter
2059 * is atomic with allocation of a membership.
2062 goto out_in6p_locked;
2067 * Begin state merge transaction at socket layer.
2069 INP_WLOCK_ASSERT(inp);
2072 * Graft new source into filter list for this inpcb's
2073 * membership of the group. The in6_multi may not have
2074 * been allocated yet if this is a new membership, however,
2075 * the in_mfilter slot will be allocated and must be initialized.
2077 * Note: Grafting of exclusive mode filters doesn't happen
2079 * XXX: Should check for non-NULL lims (node exists but may
2080 * not be in-mode) for interop with full-state API.
2082 if (ssa->ss.ss_family != AF_UNSPEC) {
2083 /* Membership starts in IN mode */
2085 CTR1(KTR_MLD, "%s: new join w/source", __func__);
2086 imf = ip6_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_INCLUDE);
2089 goto out_in6p_locked;
2092 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
2094 lims = im6f_graft(imf, MCAST_INCLUDE, &ssa->sin6);
2096 CTR1(KTR_MLD, "%s: merge imf state failed",
2099 goto out_in6p_locked;
2102 /* No address specified; Membership starts in EX mode */
2104 CTR1(KTR_MLD, "%s: new join w/o source", __func__);
2105 imf = ip6_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_EXCLUDE);
2108 goto out_in6p_locked;
2114 * Begin state merge transaction at MLD layer.
2120 error = in6_joingroup_locked(ifp, &gsa->sin6.sin6_addr, imf,
2121 &imf->im6f_in6m, 0);
2124 if (in_pcbrele_wlocked(inp)) {
2126 goto out_in6p_unlocked;
2129 goto out_in6p_locked;
2132 * NOTE: Refcount from in6_joingroup_locked()
2133 * is protecting membership.
2135 ip6_mfilter_insert(&imo->im6o_head, imf);
2137 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2138 IN6_MULTI_LIST_LOCK();
2139 error = in6m_merge(inm, imf);
2141 CTR1(KTR_MLD, "%s: failed to merge inm state",
2143 IN6_MULTI_LIST_UNLOCK();
2146 goto out_in6p_locked;
2148 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2149 error = mld_change_state(inm, 0);
2150 IN6_MULTI_LIST_UNLOCK();
2153 CTR1(KTR_MLD, "%s: failed mld downcall",
2157 goto out_in6p_locked;
2169 if (is_new && imf) {
2170 if (imf->im6f_in6m != NULL) {
2171 struct in6_multi_head inmh;
2174 SLIST_INSERT_HEAD(&inmh, imf->im6f_in6m, in6m_defer);
2175 in6m_release_list_deferred(&inmh);
2177 ip6_mfilter_free(imf);
2183 * Leave an IPv6 multicast group on an inpcb, possibly with a source.
2186 in6p_leave_group(struct inpcb *inp, struct sockopt *sopt)
2188 struct ipv6_mreq mreq;
2189 struct group_source_req gsr;
2190 struct epoch_tracker et;
2191 sockunion_t *gsa, *ssa;
2193 struct in6_mfilter *imf;
2194 struct ip6_moptions *imo;
2195 struct in6_msource *ims;
2196 struct in6_multi *inm;
2201 char ip6tbuf[INET6_ADDRSTRLEN];
2209 memset(&gsr, 0, sizeof(struct group_source_req));
2210 gsa = (sockunion_t *)&gsr.gsr_group;
2211 gsa->ss.ss_family = AF_UNSPEC;
2212 ssa = (sockunion_t *)&gsr.gsr_source;
2213 ssa->ss.ss_family = AF_UNSPEC;
2216 * Chew everything passed in up into a struct group_source_req
2217 * as that is easier to process.
2218 * Note: Any embedded scope ID in the multicast group passed
2219 * in by userland is ignored, the interface index is the recommended
2220 * mechanism to specify an interface; see below.
2222 switch (sopt->sopt_name) {
2223 case IPV6_LEAVE_GROUP:
2224 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
2225 sizeof(struct ipv6_mreq));
2228 gsa->sin6.sin6_family = AF_INET6;
2229 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
2230 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
2231 gsa->sin6.sin6_port = 0;
2232 gsa->sin6.sin6_scope_id = 0;
2233 ifindex = mreq.ipv6mr_interface;
2236 case MCAST_LEAVE_GROUP:
2237 case MCAST_LEAVE_SOURCE_GROUP:
2238 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2239 error = sooptcopyin(sopt, &gsr,
2240 sizeof(struct group_req),
2241 sizeof(struct group_req));
2242 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2243 error = sooptcopyin(sopt, &gsr,
2244 sizeof(struct group_source_req),
2245 sizeof(struct group_source_req));
2250 if (gsa->sin6.sin6_family != AF_INET6 ||
2251 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2253 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2254 if (ssa->sin6.sin6_family != AF_INET6 ||
2255 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2257 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
2260 * TODO: Validate embedded scope ID in source
2261 * list entry against passed-in ifp, if and only
2262 * if source list filter entry is iface or node local.
2264 in6_clearscope(&ssa->sin6.sin6_addr);
2266 gsa->sin6.sin6_port = 0;
2267 gsa->sin6.sin6_scope_id = 0;
2268 ifindex = gsr.gsr_interface;
2272 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
2273 __func__, sopt->sopt_name);
2274 return (EOPNOTSUPP);
2278 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2282 * Validate interface index if provided. If no interface index
2283 * was provided separately, attempt to look the membership up
2284 * from the default scope as a last resort to disambiguate
2285 * the membership we are being asked to leave.
2286 * XXX SCOPE6 lock potentially taken here.
2289 NET_EPOCH_ENTER(et);
2290 ifp = ifnet_byindex(ifindex);
2291 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */
2293 return (EADDRNOTAVAIL);
2294 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2296 error = sa6_embedscope(&gsa->sin6, V_ip6_use_defzone);
2298 return (EADDRNOTAVAIL);
2300 * Some badly behaved applications don't pass an ifindex
2301 * or a scope ID, which is an API violation. In this case,
2302 * perform a lookup as per a v6 join.
2304 * XXX For now, stomp on zone ID for the corner case.
2305 * This is not the 'KAME way', but we need to see the ifp
2306 * directly until such time as this implementation is
2307 * refactored, assuming the scope IDs are the way to go.
2309 ifindex = ntohs(gsa->sin6.sin6_addr.s6_addr16[1]);
2311 CTR2(KTR_MLD, "%s: warning: no ifindex, looking up "
2312 "ifp for group %s.", __func__,
2313 ip6_sprintf(ip6tbuf, &gsa->sin6.sin6_addr));
2314 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
2316 NET_EPOCH_ENTER(et);
2317 ifp = ifnet_byindex(ifindex);
2318 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */
2321 return (EADDRNOTAVAIL);
2324 CTR2(KTR_MLD, "%s: ifp = %p", __func__, ifp);
2325 KASSERT(ifp != NULL, ("%s: ifp did not resolve", __func__));
2330 * Find the membership in the membership list.
2332 imo = in6p_findmoptions(inp);
2333 imf = im6o_match_group(imo, ifp, &gsa->sa);
2335 error = EADDRNOTAVAIL;
2336 goto out_in6p_locked;
2338 inm = imf->im6f_in6m;
2340 if (ssa->ss.ss_family != AF_UNSPEC)
2344 * Begin state merge transaction at socket layer.
2346 INP_WLOCK_ASSERT(inp);
2349 * If we were instructed only to leave a given source, do so.
2350 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2353 ip6_mfilter_remove(&imo->im6o_head, imf);
2357 * Give up the multicast address record to which
2358 * the membership points.
2360 (void)in6_leavegroup_locked(inm, imf);
2362 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
2363 error = EADDRNOTAVAIL;
2364 goto out_in6p_locked;
2366 ims = im6o_match_source(imf, &ssa->sa);
2368 CTR3(KTR_MLD, "%s: source %p %spresent", __func__,
2369 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
2371 error = EADDRNOTAVAIL;
2372 goto out_in6p_locked;
2374 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
2375 error = im6f_prune(imf, &ssa->sin6);
2377 CTR1(KTR_MLD, "%s: merge imf state failed",
2379 goto out_in6p_locked;
2384 * Begin state merge transaction at MLD layer.
2387 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2388 IN6_MULTI_LIST_LOCK();
2389 error = in6m_merge(inm, imf);
2391 CTR1(KTR_MLD, "%s: failed to merge inm state",
2393 IN6_MULTI_LIST_UNLOCK();
2396 goto out_in6p_locked;
2399 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2400 error = mld_change_state(inm, 0);
2401 IN6_MULTI_LIST_UNLOCK();
2403 CTR1(KTR_MLD, "%s: failed mld downcall",
2407 goto out_in6p_locked;
2417 if (is_final && imf)
2418 ip6_mfilter_free(imf);
2425 * Select the interface for transmitting IPv6 multicast datagrams.
2427 * Either an instance of struct in6_addr or an instance of struct ipv6_mreqn
2428 * may be passed to this socket option. An address of in6addr_any or an
2429 * interface index of 0 is used to remove a previous selection.
2430 * When no interface is selected, one is chosen for every send.
2433 in6p_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2435 struct epoch_tracker et;
2437 struct ip6_moptions *imo;
2441 if (sopt->sopt_valsize != sizeof(u_int))
2444 error = sooptcopyin(sopt, &ifindex, sizeof(u_int), sizeof(u_int));
2447 NET_EPOCH_ENTER(et);
2451 ifp = ifnet_byindex(ifindex);
2452 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2454 return (EADDRNOTAVAIL);
2457 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */
2458 imo = in6p_findmoptions(inp);
2459 imo->im6o_multicast_ifp = ifp;
2466 * Atomically set source filters on a socket for an IPv6 multicast group.
2468 * XXXGL: unsafely exits epoch with ifnet pointer
2471 in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2473 struct __msfilterreq msfr;
2474 struct epoch_tracker et;
2477 struct in6_mfilter *imf;
2478 struct ip6_moptions *imo;
2479 struct in6_multi *inm;
2482 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2483 sizeof(struct __msfilterreq));
2487 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
2490 if (msfr.msfr_fmode != MCAST_EXCLUDE &&
2491 msfr.msfr_fmode != MCAST_INCLUDE)
2494 if (msfr.msfr_group.ss_family != AF_INET6 ||
2495 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
2498 gsa = (sockunion_t *)&msfr.msfr_group;
2499 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2502 gsa->sin6.sin6_port = 0; /* ignore port */
2504 NET_EPOCH_ENTER(et);
2505 ifp = ifnet_byindex(msfr.msfr_ifindex);
2508 return (EADDRNOTAVAIL);
2509 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2512 * Take the INP write lock.
2513 * Check if this socket is a member of this group.
2515 imo = in6p_findmoptions(inp);
2516 imf = im6o_match_group(imo, ifp, &gsa->sa);
2518 error = EADDRNOTAVAIL;
2519 goto out_in6p_locked;
2521 inm = imf->im6f_in6m;
2524 * Begin state merge transaction at socket layer.
2526 INP_WLOCK_ASSERT(inp);
2528 imf->im6f_st[1] = msfr.msfr_fmode;
2531 * Apply any new source filters, if present.
2532 * Make a copy of the user-space source vector so
2533 * that we may copy them with a single copyin. This
2534 * allows us to deal with page faults up-front.
2536 if (msfr.msfr_nsrcs > 0) {
2537 struct in6_msource *lims;
2538 struct sockaddr_in6 *psin;
2539 struct sockaddr_storage *kss, *pkss;
2544 CTR2(KTR_MLD, "%s: loading %lu source list entries",
2545 __func__, (unsigned long)msfr.msfr_nsrcs);
2546 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2548 error = copyin(msfr.msfr_srcs, kss,
2549 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2558 * Mark all source filters as UNDEFINED at t1.
2559 * Restore new group filter mode, as im6f_leave()
2560 * will set it to INCLUDE.
2563 imf->im6f_st[1] = msfr.msfr_fmode;
2566 * Update socket layer filters at t1, lazy-allocating
2567 * new entries. This saves a bunch of memory at the
2568 * cost of one RB_FIND() per source entry; duplicate
2569 * entries in the msfr_nsrcs vector are ignored.
2570 * If we encounter an error, rollback transaction.
2572 * XXX This too could be replaced with a set-symmetric
2573 * difference like loop to avoid walking from root
2574 * every time, as the key space is common.
2576 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2577 psin = (struct sockaddr_in6 *)pkss;
2578 if (psin->sin6_family != AF_INET6) {
2579 error = EAFNOSUPPORT;
2582 if (psin->sin6_len != sizeof(struct sockaddr_in6)) {
2586 if (IN6_IS_ADDR_MULTICAST(&psin->sin6_addr)) {
2591 * TODO: Validate embedded scope ID in source
2592 * list entry against passed-in ifp, if and only
2593 * if source list filter entry is iface or node local.
2595 in6_clearscope(&psin->sin6_addr);
2596 error = im6f_get_source(imf, psin, &lims);
2599 lims->im6sl_st[1] = imf->im6f_st[1];
2605 goto out_im6f_rollback;
2607 INP_WLOCK_ASSERT(inp);
2608 IN6_MULTI_LIST_LOCK();
2611 * Begin state merge transaction at MLD layer.
2613 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2614 error = in6m_merge(inm, imf);
2616 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
2618 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2619 error = mld_change_state(inm, 0);
2621 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
2624 IN6_MULTI_LIST_UNLOCK();
2640 * Set the IP multicast options in response to user setsockopt().
2642 * Many of the socket options handled in this function duplicate the
2643 * functionality of socket options in the regular unicast API. However,
2644 * it is not possible to merge the duplicate code, because the idempotence
2645 * of the IPv6 multicast part of the BSD Sockets API must be preserved;
2646 * the effects of these options must be treated as separate and distinct.
2648 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2651 ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2653 struct ip6_moptions *im6o;
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 IPV6_MULTICAST_IF:
2669 error = in6p_set_multicast_if(inp, sopt);
2672 case IPV6_MULTICAST_HOPS: {
2675 if (sopt->sopt_valsize != sizeof(int)) {
2679 error = sooptcopyin(sopt, &hlim, sizeof(hlim), sizeof(int));
2682 if (hlim < -1 || hlim > 255) {
2685 } else if (hlim == -1) {
2686 hlim = V_ip6_defmcasthlim;
2688 im6o = in6p_findmoptions(inp);
2689 im6o->im6o_multicast_hlim = hlim;
2694 case IPV6_MULTICAST_LOOP: {
2698 * Set the loopback flag for outgoing multicast packets.
2699 * Must be zero or one.
2701 if (sopt->sopt_valsize != sizeof(u_int)) {
2705 error = sooptcopyin(sopt, &loop, sizeof(u_int), sizeof(u_int));
2712 im6o = in6p_findmoptions(inp);
2713 im6o->im6o_multicast_loop = loop;
2718 case IPV6_JOIN_GROUP:
2719 case MCAST_JOIN_GROUP:
2720 case MCAST_JOIN_SOURCE_GROUP:
2721 error = in6p_join_group(inp, sopt);
2724 case IPV6_LEAVE_GROUP:
2725 case MCAST_LEAVE_GROUP:
2726 case MCAST_LEAVE_SOURCE_GROUP:
2727 error = in6p_leave_group(inp, sopt);
2730 case MCAST_BLOCK_SOURCE:
2731 case MCAST_UNBLOCK_SOURCE:
2732 error = in6p_block_unblock_source(inp, sopt);
2736 error = in6p_set_source_filters(inp, sopt);
2744 INP_UNLOCK_ASSERT(inp);
2750 * Expose MLD's multicast filter mode and source list(s) to userland,
2751 * keyed by (ifindex, group).
2752 * The filter mode is written out as a uint32_t, followed by
2753 * 0..n of struct in6_addr.
2754 * For use by ifmcstat(8).
2755 * SMPng: NOTE: unlocked read of ifindex space.
2758 sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS)
2760 struct in6_addr mcaddr;
2761 struct in6_addr src;
2762 struct epoch_tracker et;
2764 struct ifmultiaddr *ifma;
2765 struct in6_multi *inm;
2766 struct ip6_msource *ims;
2770 uint32_t fmode, ifindex;
2772 char ip6tbuf[INET6_ADDRSTRLEN];
2778 if (req->newptr != NULL)
2781 /* int: ifindex + 4 * 32 bits of IPv6 address */
2785 memcpy(&mcaddr, &name[1], sizeof(struct in6_addr));
2786 if (!IN6_IS_ADDR_MULTICAST(&mcaddr)) {
2787 CTR2(KTR_MLD, "%s: group %s is not multicast",
2788 __func__, ip6_sprintf(ip6tbuf, &mcaddr));
2793 NET_EPOCH_ENTER(et);
2794 ifp = ifnet_byindex(ifindex);
2797 CTR2(KTR_MLD, "%s: no ifp for ifindex %u",
2802 * Internal MLD lookups require that scope/zone ID is set.
2804 (void)in6_setscope(&mcaddr, ifp, NULL);
2806 retval = sysctl_wire_old_buffer(req,
2807 sizeof(uint32_t) + (in6_mcast_maxgrpsrc * sizeof(struct in6_addr)));
2814 IN6_MULTI_LIST_LOCK();
2815 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2816 inm = in6m_ifmultiaddr_get_inm(ifma);
2819 if (!IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, &mcaddr))
2821 fmode = inm->in6m_st[1].iss_fmode;
2822 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2825 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
2826 CTR2(KTR_MLD, "%s: visit node %p", __func__, ims);
2828 * Only copy-out sources which are in-mode.
2830 if (fmode != im6s_get_mode(inm, ims, 1)) {
2831 CTR1(KTR_MLD, "%s: skip non-in-mode",
2835 src = ims->im6s_addr;
2836 retval = SYSCTL_OUT(req, &src,
2837 sizeof(struct in6_addr));
2842 IN6_MULTI_LIST_UNLOCK();
2851 static const char *in6m_modestrs[] = { "un", "in", "ex" };
2854 in6m_mode_str(const int mode)
2857 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2858 return (in6m_modestrs[mode]);
2862 static const char *in6m_statestrs[] = {
2875 in6m_state_str(const int state)
2878 if (state >= MLD_NOT_MEMBER && state <= MLD_LEAVING_MEMBER)
2879 return (in6m_statestrs[state]);
2884 * Dump an in6_multi structure to the console.
2887 in6m_print(const struct in6_multi *inm)
2890 char ip6tbuf[INET6_ADDRSTRLEN];
2892 if ((ktr_mask & KTR_MLD) == 0)
2895 printf("%s: --- begin in6m %p ---\n", __func__, inm);
2896 printf("addr %s ifp %p(%s) ifma %p\n",
2897 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2899 if_name(inm->in6m_ifp),
2901 printf("timer %u state %s refcount %u scq.len %u\n",
2903 in6m_state_str(inm->in6m_state),
2905 mbufq_len(&inm->in6m_scq));
2906 printf("mli %p nsrc %lu sctimer %u scrv %u\n",
2911 for (t = 0; t < 2; t++) {
2912 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
2913 in6m_mode_str(inm->in6m_st[t].iss_fmode),
2914 inm->in6m_st[t].iss_asm,
2915 inm->in6m_st[t].iss_ex,
2916 inm->in6m_st[t].iss_in,
2917 inm->in6m_st[t].iss_rec);
2919 printf("%s: --- end in6m %p ---\n", __func__, inm);
2925 in6m_print(const struct in6_multi *inm)