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 #include "opt_inet6.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/malloc.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/sysctl.h>
51 #include <sys/taskqueue.h>
55 #include <net/if_var.h>
56 #include <net/if_dl.h>
57 #include <net/if_private.h>
58 #include <net/route.h>
59 #include <net/route/nhop.h>
62 #include <netinet/in.h>
63 #include <netinet/udp.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip_var.h>
66 #include <netinet/udp_var.h>
67 #include <netinet6/in6_fib.h>
68 #include <netinet6/in6_var.h>
69 #include <netinet/ip6.h>
70 #include <netinet/icmp6.h>
71 #include <netinet6/ip6_var.h>
72 #include <netinet/in_pcb.h>
73 #include <netinet/tcp_var.h>
74 #include <netinet6/nd6.h>
75 #include <netinet6/mld6_var.h>
76 #include <netinet6/scope6_var.h>
79 #define KTR_MLD KTR_INET6
82 #ifndef __SOCKUNION_DECLARED
84 struct sockaddr_storage ss;
86 struct sockaddr_dl sdl;
87 struct sockaddr_in6 sin6;
89 typedef union sockunion sockunion_t;
90 #define __SOCKUNION_DECLARED
91 #endif /* __SOCKUNION_DECLARED */
93 static MALLOC_DEFINE(M_IN6MFILTER, "in6_mfilter",
94 "IPv6 multicast PCB-layer source filter");
95 MALLOC_DEFINE(M_IP6MADDR, "in6_multi", "IPv6 multicast group");
96 static MALLOC_DEFINE(M_IP6MOPTS, "ip6_moptions", "IPv6 multicast options");
97 static MALLOC_DEFINE(M_IP6MSOURCE, "ip6_msource",
98 "IPv6 multicast MLD-layer source filter");
100 RB_GENERATE(ip6_msource_tree, ip6_msource, im6s_link, ip6_msource_cmp);
104 * - Lock order is: IN6_MULTI_LOCK, INP_WLOCK, IN6_MULTI_LIST_LOCK, MLD_LOCK,
106 * - The IF_ADDR_LOCK is implicitly taken by in6m_lookup() earlier, however
107 * it can be taken by code in net/if.c also.
108 * - ip6_moptions and in6_mfilter are covered by the INP_WLOCK.
110 * struct in6_multi is covered by IN6_MULTI_LOCK. There isn't strictly
111 * any need for in6_multi itself to be virtualized -- it is bound to an ifp
112 * anyway no matter what happens.
114 struct mtx in6_multi_list_mtx;
115 MTX_SYSINIT(in6_multi_mtx, &in6_multi_list_mtx, "in6_multi_list_mtx", MTX_DEF);
117 struct mtx in6_multi_free_mtx;
118 MTX_SYSINIT(in6_multi_free_mtx, &in6_multi_free_mtx, "in6_multi_free_mtx", MTX_DEF);
120 struct sx in6_multi_sx;
121 SX_SYSINIT(in6_multi_sx, &in6_multi_sx, "in6_multi_sx");
123 static void im6f_commit(struct in6_mfilter *);
124 static int im6f_get_source(struct in6_mfilter *imf,
125 const struct sockaddr_in6 *psin,
126 struct in6_msource **);
127 static struct in6_msource *
128 im6f_graft(struct in6_mfilter *, const uint8_t,
129 const struct sockaddr_in6 *);
130 static void im6f_leave(struct in6_mfilter *);
131 static int im6f_prune(struct in6_mfilter *, const struct sockaddr_in6 *);
132 static void im6f_purge(struct in6_mfilter *);
133 static void im6f_rollback(struct in6_mfilter *);
134 static void im6f_reap(struct in6_mfilter *);
135 static struct in6_mfilter *
136 im6o_match_group(const struct ip6_moptions *,
137 const struct ifnet *, const struct sockaddr *);
138 static struct in6_msource *
139 im6o_match_source(struct in6_mfilter *, const struct sockaddr *);
140 static void im6s_merge(struct ip6_msource *ims,
141 const struct in6_msource *lims, const int rollback);
142 static int in6_getmulti(struct ifnet *, const struct in6_addr *,
143 struct in6_multi **);
144 static int in6_joingroup_locked(struct ifnet *, const struct in6_addr *,
145 struct in6_mfilter *, struct in6_multi **, int);
146 static int in6m_get_source(struct in6_multi *inm,
147 const struct in6_addr *addr, const int noalloc,
148 struct ip6_msource **pims);
150 static int in6m_is_ifp_detached(const struct in6_multi *);
152 static int in6m_merge(struct in6_multi *, /*const*/ struct in6_mfilter *);
153 static void in6m_purge(struct in6_multi *);
154 static void in6m_reap(struct in6_multi *);
155 static struct ip6_moptions *
156 in6p_findmoptions(struct inpcb *);
157 static int in6p_get_source_filters(struct inpcb *, struct sockopt *);
158 static int in6p_join_group(struct inpcb *, struct sockopt *);
159 static int in6p_leave_group(struct inpcb *, struct sockopt *);
160 static struct ifnet *
161 in6p_lookup_mcast_ifp(const struct inpcb *,
162 const struct sockaddr_in6 *);
163 static int in6p_block_unblock_source(struct inpcb *, struct sockopt *);
164 static int in6p_set_multicast_if(struct inpcb *, struct sockopt *);
165 static int in6p_set_source_filters(struct inpcb *, struct sockopt *);
166 static int sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS);
168 SYSCTL_DECL(_net_inet6_ip6); /* XXX Not in any common header. */
170 static SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, mcast,
171 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
174 static u_long in6_mcast_maxgrpsrc = IPV6_MAX_GROUP_SRC_FILTER;
175 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxgrpsrc,
176 CTLFLAG_RWTUN, &in6_mcast_maxgrpsrc, 0,
177 "Max source filters per group");
179 static u_long in6_mcast_maxsocksrc = IPV6_MAX_SOCK_SRC_FILTER;
180 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxsocksrc,
181 CTLFLAG_RWTUN, &in6_mcast_maxsocksrc, 0,
182 "Max source filters per socket");
184 /* TODO Virtualize this switch. */
185 int in6_mcast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
186 SYSCTL_INT(_net_inet6_ip6_mcast, OID_AUTO, loop, CTLFLAG_RWTUN,
187 &in6_mcast_loop, 0, "Loopback multicast datagrams by default");
189 static SYSCTL_NODE(_net_inet6_ip6_mcast, OID_AUTO, filters,
190 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip6_mcast_filters,
191 "Per-interface stack-wide source filters");
195 * Inline function which wraps assertions for a valid ifp.
196 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
200 in6m_is_ifp_detached(const struct in6_multi *inm)
204 KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__));
205 ifp = inm->in6m_ifma->ifma_ifp;
208 * Sanity check that network-layer notion of ifp is the
209 * same as that of link-layer.
211 KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__));
214 return (ifp == NULL);
219 * Initialize an in6_mfilter structure to a known state at t0, t1
220 * with an empty source filter list.
223 im6f_init(struct in6_mfilter *imf, const int st0, const int st1)
225 memset(imf, 0, sizeof(struct in6_mfilter));
226 RB_INIT(&imf->im6f_sources);
227 imf->im6f_st[0] = st0;
228 imf->im6f_st[1] = st1;
232 ip6_mfilter_alloc(const int mflags, const int st0, const int st1)
234 struct in6_mfilter *imf;
236 imf = malloc(sizeof(*imf), M_IN6MFILTER, mflags);
239 im6f_init(imf, st0, st1);
245 ip6_mfilter_free(struct in6_mfilter *imf)
249 free(imf, M_IN6MFILTER);
253 * Find an IPv6 multicast group entry for this ip6_moptions instance
254 * which matches the specified group, and optionally an interface.
255 * Return its index into the array, or -1 if not found.
257 static struct in6_mfilter *
258 im6o_match_group(const struct ip6_moptions *imo, const struct ifnet *ifp,
259 const struct sockaddr *group)
261 const struct sockaddr_in6 *gsin6;
262 struct in6_mfilter *imf;
263 struct in6_multi *inm;
265 gsin6 = (const struct sockaddr_in6 *)group;
267 IP6_MFILTER_FOREACH(imf, &imo->im6o_head) {
268 inm = imf->im6f_in6m;
271 if ((ifp == NULL || (inm->in6m_ifp == ifp)) &&
272 IN6_ARE_ADDR_EQUAL(&inm->in6m_addr,
273 &gsin6->sin6_addr)) {
281 * Find an IPv6 multicast source entry for this imo which matches
282 * the given group index for this socket, and source address.
284 * XXX TODO: The scope ID, if present in src, is stripped before
285 * any comparison. We SHOULD enforce scope/zone checks where the source
286 * filter entry has a link scope.
288 * NOTE: This does not check if the entry is in-mode, merely if
289 * it exists, which may not be the desired behaviour.
291 static struct in6_msource *
292 im6o_match_source(struct in6_mfilter *imf, const struct sockaddr *src)
294 struct ip6_msource find;
295 struct ip6_msource *ims;
296 const sockunion_t *psa;
298 KASSERT(src->sa_family == AF_INET6, ("%s: !AF_INET6", __func__));
300 psa = (const sockunion_t *)src;
301 find.im6s_addr = psa->sin6.sin6_addr;
302 in6_clearscope(&find.im6s_addr); /* XXX */
303 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
305 return ((struct in6_msource *)ims);
309 * Perform filtering for multicast datagrams on a socket by group and source.
311 * Returns 0 if a datagram should be allowed through, or various error codes
312 * if the socket was not a member of the group, or the source was muted, etc.
315 im6o_mc_filter(const struct ip6_moptions *imo, const struct ifnet *ifp,
316 const struct sockaddr *group, const struct sockaddr *src)
318 struct in6_mfilter *imf;
319 struct in6_msource *ims;
322 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
324 imf = im6o_match_group(imo, ifp, group);
326 return (MCAST_NOTGMEMBER);
329 * Check if the source was included in an (S,G) join.
330 * Allow reception on exclusive memberships by default,
331 * reject reception on inclusive memberships by default.
332 * Exclude source only if an in-mode exclude filter exists.
333 * Include source only if an in-mode include filter exists.
334 * NOTE: We are comparing group state here at MLD t1 (now)
335 * with socket-layer t0 (since last downcall).
337 mode = imf->im6f_st[1];
338 ims = im6o_match_source(imf, src);
340 if ((ims == NULL && mode == MCAST_INCLUDE) ||
341 (ims != NULL && ims->im6sl_st[0] != mode))
342 return (MCAST_NOTSMEMBER);
348 * Look up an in6_multi record for an IPv6 multicast address
349 * on the interface ifp.
350 * If no record found, return NULL.
352 * SMPng: The IN6_MULTI_LOCK and must be held and must be in network epoch.
355 in6m_lookup_locked(struct ifnet *ifp, const struct in6_addr *mcaddr)
357 struct ifmultiaddr *ifma;
358 struct in6_multi *inm;
362 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
363 inm = in6m_ifmultiaddr_get_inm(ifma);
366 if (IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, mcaddr))
373 * Find and return a reference to an in6_multi record for (ifp, group),
374 * and bump its reference count.
375 * If one does not exist, try to allocate it, and update link-layer multicast
376 * filters on ifp to listen for group.
377 * Assumes the IN6_MULTI lock is held across the call.
378 * Return 0 if successful, otherwise return an appropriate error code.
381 in6_getmulti(struct ifnet *ifp, const struct in6_addr *group,
382 struct in6_multi **pinm)
384 struct epoch_tracker et;
385 struct sockaddr_in6 gsin6;
386 struct ifmultiaddr *ifma;
387 struct in6_multi *inm;
393 * XXX: Accesses to ifma_protospec must be covered by IF_ADDR_LOCK;
394 * if_addmulti() takes this mutex itself, so we must drop and
395 * re-acquire around the call.
397 IN6_MULTI_LOCK_ASSERT();
398 IN6_MULTI_LIST_LOCK();
402 * Does ifp support IPv6 multicasts?
404 if (ifp->if_afdata[AF_INET6] == NULL)
407 inm = in6m_lookup_locked(ifp, group);
415 * If we already joined this group, just bump the
416 * refcount and return it.
418 KASSERT(inm->in6m_refcount >= 1,
419 ("%s: bad refcount %d", __func__, inm->in6m_refcount));
420 in6m_acquire_locked(inm);
425 memset(&gsin6, 0, sizeof(gsin6));
426 gsin6.sin6_family = AF_INET6;
427 gsin6.sin6_len = sizeof(struct sockaddr_in6);
428 gsin6.sin6_addr = *group;
431 * Check if a link-layer group is already associated
432 * with this network-layer group on the given ifnet.
434 IN6_MULTI_LIST_UNLOCK();
435 IF_ADDR_WUNLOCK(ifp);
436 error = if_addmulti(ifp, (struct sockaddr *)&gsin6, &ifma);
439 IN6_MULTI_LIST_LOCK();
443 * If something other than netinet6 is occupying the link-layer
444 * group, print a meaningful error message and back out of
446 * Otherwise, bump the refcount on the existing network-layer
447 * group association and return it.
449 if (ifma->ifma_protospec != NULL) {
450 inm = (struct in6_multi *)ifma->ifma_protospec;
452 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
454 KASSERT(ifma->ifma_addr->sa_family == AF_INET6,
455 ("%s: ifma not AF_INET6", __func__));
456 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
457 if (inm->in6m_ifma != ifma || inm->in6m_ifp != ifp ||
458 !IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, group))
459 panic("%s: ifma %p is inconsistent with %p (%p)",
460 __func__, ifma, inm, group);
462 in6m_acquire_locked(inm);
467 IF_ADDR_WLOCK_ASSERT(ifp);
470 * A new in6_multi record is needed; allocate and initialize it.
471 * We DO NOT perform an MLD join as the in6_ layer may need to
472 * push an initial source list down to MLD to support SSM.
474 * The initial source filter state is INCLUDE, {} as per the RFC.
475 * Pending state-changes per group are subject to a bounds check.
477 inm = malloc(sizeof(*inm), M_IP6MADDR, M_NOWAIT | M_ZERO);
479 IN6_MULTI_LIST_UNLOCK();
480 IF_ADDR_WUNLOCK(ifp);
481 if_delmulti_ifma(ifma);
484 inm->in6m_addr = *group;
486 inm->in6m_mli = MLD_IFINFO(ifp);
487 inm->in6m_ifma = ifma;
488 inm->in6m_refcount = 1;
489 inm->in6m_state = MLD_NOT_MEMBER;
490 mbufq_init(&inm->in6m_scq, MLD_MAX_STATE_CHANGES);
492 inm->in6m_st[0].iss_fmode = MCAST_UNDEFINED;
493 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
494 RB_INIT(&inm->in6m_srcs);
496 ifma->ifma_protospec = inm;
500 IN6_MULTI_LIST_UNLOCK();
501 IF_ADDR_WUNLOCK(ifp);
506 * Drop a reference to an in6_multi record.
508 * If the refcount drops to 0, free the in6_multi record and
509 * delete the underlying link-layer membership.
512 in6m_release(struct in6_multi *inm)
514 struct ifmultiaddr *ifma;
517 CTR2(KTR_MLD, "%s: refcount is %d", __func__, inm->in6m_refcount);
519 MPASS(inm->in6m_refcount == 0);
520 CTR2(KTR_MLD, "%s: freeing inm %p", __func__, inm);
522 ifma = inm->in6m_ifma;
524 MPASS(ifma->ifma_llifma == NULL);
526 /* XXX this access is not covered by IF_ADDR_LOCK */
527 CTR2(KTR_MLD, "%s: purging ifma %p", __func__, ifma);
528 KASSERT(ifma->ifma_protospec == NULL,
529 ("%s: ifma_protospec != NULL", __func__));
531 ifp = ifma->ifma_ifp;
534 CURVNET_SET(ifp->if_vnet);
536 free(inm, M_IP6MADDR);
537 if_delmulti_ifma_flags(ifma, 1);
542 free(inm, M_IP6MADDR);
543 if_delmulti_ifma_flags(ifma, 1);
548 * Interface detach can happen in a taskqueue thread context, so we must use a
549 * dedicated thread to avoid deadlocks when draining in6m_release tasks.
551 TASKQUEUE_DEFINE_THREAD(in6m_free);
552 static struct in6_multi_head in6m_free_list = SLIST_HEAD_INITIALIZER();
553 static void in6m_release_task(void *arg __unused, int pending __unused);
554 static struct task in6m_free_task = TASK_INITIALIZER(0, in6m_release_task, NULL);
557 in6m_release_list_deferred(struct in6_multi_head *inmh)
559 if (SLIST_EMPTY(inmh))
561 mtx_lock(&in6_multi_free_mtx);
562 SLIST_CONCAT(&in6m_free_list, inmh, in6_multi, in6m_nrele);
563 mtx_unlock(&in6_multi_free_mtx);
564 taskqueue_enqueue(taskqueue_in6m_free, &in6m_free_task);
568 in6m_release_wait(void *arg __unused)
572 * Make sure all pending multicast addresses are freed before
573 * the VNET or network device is destroyed:
575 taskqueue_drain_all(taskqueue_in6m_free);
578 /* XXX-BZ FIXME, see D24914. */
579 VNET_SYSUNINIT(in6m_release_wait, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, in6m_release_wait, NULL);
583 in6m_disconnect_locked(struct in6_multi_head *inmh, struct in6_multi *inm)
587 struct in6_ifaddr *ifa6;
588 struct in6_multi_mship *imm, *imm_tmp;
589 struct ifmultiaddr *ifma, *ll_ifma;
591 IN6_MULTI_LIST_LOCK_ASSERT();
595 return; /* already called */
597 inm->in6m_ifp = NULL;
598 IF_ADDR_WLOCK_ASSERT(ifp);
599 ifma = inm->in6m_ifma;
604 if (ifma->ifma_flags & IFMA_F_ENQUEUED) {
605 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
606 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
608 MCDPRINTF("removed ifma: %p from %s\n", ifma, ifp->if_xname);
609 if ((ll_ifma = ifma->ifma_llifma) != NULL) {
610 MPASS(ifma != ll_ifma);
611 ifma->ifma_llifma = NULL;
612 MPASS(ll_ifma->ifma_llifma == NULL);
613 MPASS(ll_ifma->ifma_ifp == ifp);
614 if (--ll_ifma->ifma_refcount == 0) {
615 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
616 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, ifma_link);
617 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
619 MCDPRINTF("removed ll_ifma: %p from %s\n", ll_ifma, ifp->if_xname);
620 if_freemulti(ll_ifma);
623 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
624 if (ifa->ifa_addr->sa_family != AF_INET6)
627 LIST_FOREACH_SAFE(imm, &ifa6->ia6_memberships,
628 i6mm_chain, imm_tmp) {
629 if (inm == imm->i6mm_maddr) {
630 LIST_REMOVE(imm, i6mm_chain);
631 free(imm, M_IP6MADDR);
632 in6m_rele_locked(inmh, inm);
639 in6m_release_task(void *arg __unused, int pending __unused)
641 struct in6_multi_head in6m_free_tmp;
642 struct in6_multi *inm, *tinm;
644 SLIST_INIT(&in6m_free_tmp);
645 mtx_lock(&in6_multi_free_mtx);
646 SLIST_CONCAT(&in6m_free_tmp, &in6m_free_list, in6_multi, in6m_nrele);
647 mtx_unlock(&in6_multi_free_mtx);
649 SLIST_FOREACH_SAFE(inm, &in6m_free_tmp, in6m_nrele, tinm) {
650 SLIST_REMOVE_HEAD(&in6m_free_tmp, in6m_nrele);
657 * Clear recorded source entries for a group.
658 * Used by the MLD code. Caller must hold the IN6_MULTI lock.
659 * FIXME: Should reap.
662 in6m_clear_recorded(struct in6_multi *inm)
664 struct ip6_msource *ims;
666 IN6_MULTI_LIST_LOCK_ASSERT();
668 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
671 --inm->in6m_st[1].iss_rec;
674 KASSERT(inm->in6m_st[1].iss_rec == 0,
675 ("%s: iss_rec %d not 0", __func__, inm->in6m_st[1].iss_rec));
679 * Record a source as pending for a Source-Group MLDv2 query.
680 * This lives here as it modifies the shared tree.
682 * inm is the group descriptor.
683 * naddr is the address of the source to record in network-byte order.
685 * If the net.inet6.mld.sgalloc sysctl is non-zero, we will
686 * lazy-allocate a source node in response to an SG query.
687 * Otherwise, no allocation is performed. This saves some memory
688 * with the trade-off that the source will not be reported to the
689 * router if joined in the window between the query response and
690 * the group actually being joined on the local host.
692 * VIMAGE: XXX: Currently the mld_sgalloc feature has been removed.
693 * This turns off the allocation of a recorded source entry if
694 * the group has not been joined.
696 * Return 0 if the source didn't exist or was already marked as recorded.
697 * Return 1 if the source was marked as recorded by this function.
698 * Return <0 if any error occurred (negated errno code).
701 in6m_record_source(struct in6_multi *inm, const struct in6_addr *addr)
703 struct ip6_msource find;
704 struct ip6_msource *ims, *nims;
706 IN6_MULTI_LIST_LOCK_ASSERT();
708 find.im6s_addr = *addr;
709 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
710 if (ims && ims->im6s_stp)
713 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
715 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
719 nims->im6s_addr = find.im6s_addr;
720 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
726 * Mark the source as recorded and update the recorded
730 ++inm->in6m_st[1].iss_rec;
736 * Return a pointer to an in6_msource owned by an in6_mfilter,
737 * given its source address.
738 * Lazy-allocate if needed. If this is a new entry its filter state is
741 * imf is the filter set being modified.
742 * addr is the source address.
744 * SMPng: May be called with locks held; malloc must not block.
747 im6f_get_source(struct in6_mfilter *imf, const struct sockaddr_in6 *psin,
748 struct in6_msource **plims)
750 struct ip6_msource find;
751 struct ip6_msource *ims, *nims;
752 struct in6_msource *lims;
759 find.im6s_addr = psin->sin6_addr;
760 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
761 lims = (struct in6_msource *)ims;
763 if (imf->im6f_nsrc == in6_mcast_maxsocksrc)
765 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
769 lims = (struct in6_msource *)nims;
770 lims->im6s_addr = find.im6s_addr;
771 lims->im6sl_st[0] = MCAST_UNDEFINED;
772 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
782 * Graft a source entry into an existing socket-layer filter set,
783 * maintaining any required invariants and checking allocations.
785 * The source is marked as being in the new filter mode at t1.
787 * Return the pointer to the new node, otherwise return NULL.
789 static struct in6_msource *
790 im6f_graft(struct in6_mfilter *imf, const uint8_t st1,
791 const struct sockaddr_in6 *psin)
793 struct ip6_msource *nims;
794 struct in6_msource *lims;
796 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
800 lims = (struct in6_msource *)nims;
801 lims->im6s_addr = psin->sin6_addr;
802 lims->im6sl_st[0] = MCAST_UNDEFINED;
803 lims->im6sl_st[1] = st1;
804 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
811 * Prune a source entry from an existing socket-layer filter set,
812 * maintaining any required invariants and checking allocations.
814 * The source is marked as being left at t1, it is not freed.
816 * Return 0 if no error occurred, otherwise return an errno value.
819 im6f_prune(struct in6_mfilter *imf, const struct sockaddr_in6 *psin)
821 struct ip6_msource find;
822 struct ip6_msource *ims;
823 struct in6_msource *lims;
825 find.im6s_addr = psin->sin6_addr;
826 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
829 lims = (struct in6_msource *)ims;
830 lims->im6sl_st[1] = MCAST_UNDEFINED;
835 * Revert socket-layer filter set deltas at t1 to t0 state.
838 im6f_rollback(struct in6_mfilter *imf)
840 struct ip6_msource *ims, *tims;
841 struct in6_msource *lims;
843 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
844 lims = (struct in6_msource *)ims;
845 if (lims->im6sl_st[0] == lims->im6sl_st[1]) {
846 /* no change at t1 */
848 } else if (lims->im6sl_st[0] != MCAST_UNDEFINED) {
849 /* revert change to existing source at t1 */
850 lims->im6sl_st[1] = lims->im6sl_st[0];
852 /* revert source added t1 */
853 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
854 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
855 free(ims, M_IN6MFILTER);
859 imf->im6f_st[1] = imf->im6f_st[0];
863 * Mark socket-layer filter set as INCLUDE {} at t1.
866 im6f_leave(struct in6_mfilter *imf)
868 struct ip6_msource *ims;
869 struct in6_msource *lims;
871 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
872 lims = (struct in6_msource *)ims;
873 lims->im6sl_st[1] = MCAST_UNDEFINED;
875 imf->im6f_st[1] = MCAST_INCLUDE;
879 * Mark socket-layer filter set deltas as committed.
882 im6f_commit(struct in6_mfilter *imf)
884 struct ip6_msource *ims;
885 struct in6_msource *lims;
887 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
888 lims = (struct in6_msource *)ims;
889 lims->im6sl_st[0] = lims->im6sl_st[1];
891 imf->im6f_st[0] = imf->im6f_st[1];
895 * Reap unreferenced sources from socket-layer filter set.
898 im6f_reap(struct in6_mfilter *imf)
900 struct ip6_msource *ims, *tims;
901 struct in6_msource *lims;
903 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
904 lims = (struct in6_msource *)ims;
905 if ((lims->im6sl_st[0] == MCAST_UNDEFINED) &&
906 (lims->im6sl_st[1] == MCAST_UNDEFINED)) {
907 CTR2(KTR_MLD, "%s: free lims %p", __func__, ims);
908 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
909 free(ims, M_IN6MFILTER);
916 * Purge socket-layer filter set.
919 im6f_purge(struct in6_mfilter *imf)
921 struct ip6_msource *ims, *tims;
923 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
924 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
925 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
926 free(ims, M_IN6MFILTER);
929 imf->im6f_st[0] = imf->im6f_st[1] = MCAST_UNDEFINED;
930 KASSERT(RB_EMPTY(&imf->im6f_sources),
931 ("%s: im6f_sources not empty", __func__));
935 * Look up a source filter entry for a multicast group.
937 * inm is the group descriptor to work with.
938 * addr is the IPv6 address to look up.
939 * noalloc may be non-zero to suppress allocation of sources.
940 * *pims will be set to the address of the retrieved or allocated source.
942 * SMPng: NOTE: may be called with locks held.
943 * Return 0 if successful, otherwise return a non-zero error code.
946 in6m_get_source(struct in6_multi *inm, const struct in6_addr *addr,
947 const int noalloc, struct ip6_msource **pims)
949 struct ip6_msource find;
950 struct ip6_msource *ims, *nims;
952 char ip6tbuf[INET6_ADDRSTRLEN];
955 find.im6s_addr = *addr;
956 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
957 if (ims == NULL && !noalloc) {
958 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
960 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
964 nims->im6s_addr = *addr;
965 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
968 CTR3(KTR_MLD, "%s: allocated %s as %p", __func__,
969 ip6_sprintf(ip6tbuf, addr), ims);
977 * Merge socket-layer source into MLD-layer source.
978 * If rollback is non-zero, perform the inverse of the merge.
981 im6s_merge(struct ip6_msource *ims, const struct in6_msource *lims,
984 int n = rollback ? -1 : 1;
986 char ip6tbuf[INET6_ADDRSTRLEN];
988 ip6_sprintf(ip6tbuf, &lims->im6s_addr);
991 if (lims->im6sl_st[0] == MCAST_EXCLUDE) {
992 CTR3(KTR_MLD, "%s: t1 ex -= %d on %s", __func__, n, ip6tbuf);
993 ims->im6s_st[1].ex -= n;
994 } else if (lims->im6sl_st[0] == MCAST_INCLUDE) {
995 CTR3(KTR_MLD, "%s: t1 in -= %d on %s", __func__, n, ip6tbuf);
996 ims->im6s_st[1].in -= n;
999 if (lims->im6sl_st[1] == MCAST_EXCLUDE) {
1000 CTR3(KTR_MLD, "%s: t1 ex += %d on %s", __func__, n, ip6tbuf);
1001 ims->im6s_st[1].ex += n;
1002 } else if (lims->im6sl_st[1] == MCAST_INCLUDE) {
1003 CTR3(KTR_MLD, "%s: t1 in += %d on %s", __func__, n, ip6tbuf);
1004 ims->im6s_st[1].in += n;
1009 * Atomically update the global in6_multi state, when a membership's
1010 * filter list is being updated in any way.
1012 * imf is the per-inpcb-membership group filter pointer.
1013 * A fake imf may be passed for in-kernel consumers.
1015 * XXX This is a candidate for a set-symmetric-difference style loop
1016 * which would eliminate the repeated lookup from root of ims nodes,
1017 * as they share the same key space.
1019 * If any error occurred this function will back out of refcounts
1020 * and return a non-zero value.
1023 in6m_merge(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1025 struct ip6_msource *ims, *nims;
1026 struct in6_msource *lims;
1027 int schanged, error;
1033 IN6_MULTI_LIST_LOCK_ASSERT();
1036 * Update the source filters first, as this may fail.
1037 * Maintain count of in-mode filters at t0, t1. These are
1038 * used to work out if we transition into ASM mode or not.
1039 * Maintain a count of source filters whose state was
1040 * actually modified by this operation.
1042 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
1043 lims = (struct in6_msource *)ims;
1044 if (lims->im6sl_st[0] == imf->im6f_st[0]) nsrc0++;
1045 if (lims->im6sl_st[1] == imf->im6f_st[1]) nsrc1++;
1046 if (lims->im6sl_st[0] == lims->im6sl_st[1]) continue;
1047 error = in6m_get_source(inm, &lims->im6s_addr, 0, &nims);
1051 im6s_merge(nims, lims, 0);
1054 struct ip6_msource *bims;
1056 RB_FOREACH_REVERSE_FROM(ims, ip6_msource_tree, nims) {
1057 lims = (struct in6_msource *)ims;
1058 if (lims->im6sl_st[0] == lims->im6sl_st[1])
1060 (void)in6m_get_source(inm, &lims->im6s_addr, 1, &bims);
1063 im6s_merge(bims, lims, 1);
1068 CTR3(KTR_MLD, "%s: imf filters in-mode: %d at t0, %d at t1",
1069 __func__, nsrc0, nsrc1);
1071 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1072 if (imf->im6f_st[0] == imf->im6f_st[1] &&
1073 imf->im6f_st[1] == MCAST_INCLUDE) {
1075 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
1076 --inm->in6m_st[1].iss_in;
1080 /* Handle filter mode transition on socket. */
1081 if (imf->im6f_st[0] != imf->im6f_st[1]) {
1082 CTR3(KTR_MLD, "%s: imf transition %d to %d",
1083 __func__, imf->im6f_st[0], imf->im6f_st[1]);
1085 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
1086 CTR1(KTR_MLD, "%s: --ex on inm at t1", __func__);
1087 --inm->in6m_st[1].iss_ex;
1088 } else if (imf->im6f_st[0] == MCAST_INCLUDE) {
1089 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
1090 --inm->in6m_st[1].iss_in;
1093 if (imf->im6f_st[1] == MCAST_EXCLUDE) {
1094 CTR1(KTR_MLD, "%s: ex++ on inm at t1", __func__);
1095 inm->in6m_st[1].iss_ex++;
1096 } else if (imf->im6f_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
1097 CTR1(KTR_MLD, "%s: in++ on inm at t1", __func__);
1098 inm->in6m_st[1].iss_in++;
1103 * Track inm filter state in terms of listener counts.
1104 * If there are any exclusive listeners, stack-wide
1105 * membership is exclusive.
1106 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1107 * If no listeners remain, state is undefined at t1,
1108 * and the MLD lifecycle for this group should finish.
1110 if (inm->in6m_st[1].iss_ex > 0) {
1111 CTR1(KTR_MLD, "%s: transition to EX", __func__);
1112 inm->in6m_st[1].iss_fmode = MCAST_EXCLUDE;
1113 } else if (inm->in6m_st[1].iss_in > 0) {
1114 CTR1(KTR_MLD, "%s: transition to IN", __func__);
1115 inm->in6m_st[1].iss_fmode = MCAST_INCLUDE;
1117 CTR1(KTR_MLD, "%s: transition to UNDEF", __func__);
1118 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
1121 /* Decrement ASM listener count on transition out of ASM mode. */
1122 if (imf->im6f_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1123 if ((imf->im6f_st[1] != MCAST_EXCLUDE) ||
1124 (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 > 0)) {
1125 CTR1(KTR_MLD, "%s: --asm on inm at t1", __func__);
1126 --inm->in6m_st[1].iss_asm;
1130 /* Increment ASM listener count on transition to ASM mode. */
1131 if (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1132 CTR1(KTR_MLD, "%s: asm++ on inm at t1", __func__);
1133 inm->in6m_st[1].iss_asm++;
1136 CTR3(KTR_MLD, "%s: merged imf %p to inm %p", __func__, imf, inm);
1141 CTR1(KTR_MLD, "%s: sources changed; reaping", __func__);
1148 * Mark an in6_multi's filter set deltas as committed.
1149 * Called by MLD after a state change has been enqueued.
1152 in6m_commit(struct in6_multi *inm)
1154 struct ip6_msource *ims;
1156 CTR2(KTR_MLD, "%s: commit inm %p", __func__, inm);
1157 CTR1(KTR_MLD, "%s: pre commit:", __func__);
1160 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
1161 ims->im6s_st[0] = ims->im6s_st[1];
1163 inm->in6m_st[0] = inm->in6m_st[1];
1167 * Reap unreferenced nodes from an in6_multi's filter set.
1170 in6m_reap(struct in6_multi *inm)
1172 struct ip6_msource *ims, *tims;
1174 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1175 if (ims->im6s_st[0].ex > 0 || ims->im6s_st[0].in > 0 ||
1176 ims->im6s_st[1].ex > 0 || ims->im6s_st[1].in > 0 ||
1179 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1180 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1181 free(ims, M_IP6MSOURCE);
1187 * Purge all source nodes from an in6_multi's filter set.
1190 in6m_purge(struct in6_multi *inm)
1192 struct ip6_msource *ims, *tims;
1194 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1195 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1196 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1197 free(ims, M_IP6MSOURCE);
1200 /* Free state-change requests that might be queued. */
1201 mbufq_drain(&inm->in6m_scq);
1205 * Join a multicast address w/o sources.
1206 * KAME compatibility entry point.
1208 * SMPng: Assume no mc locks held by caller.
1211 in6_joingroup(struct ifnet *ifp, const struct in6_addr *mcaddr,
1212 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1218 error = in6_joingroup_locked(ifp, mcaddr, NULL, pinm, delay);
1224 * Join a multicast group; real entry point.
1226 * Only preserves atomicity at inm level.
1227 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1229 * If the MLD downcall fails, the group is not joined, and an error
1233 in6_joingroup_locked(struct ifnet *ifp, const struct in6_addr *mcaddr,
1234 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1237 struct in6_multi_head inmh;
1238 struct in6_mfilter timf;
1239 struct in6_multi *inm;
1240 struct ifmultiaddr *ifma;
1243 char ip6tbuf[INET6_ADDRSTRLEN];
1247 * Sanity: Check scope zone ID was set for ifp, if and
1248 * only if group is scoped to an interface.
1250 KASSERT(IN6_IS_ADDR_MULTICAST(mcaddr),
1251 ("%s: not a multicast address", __func__));
1252 if (IN6_IS_ADDR_MC_LINKLOCAL(mcaddr) ||
1253 IN6_IS_ADDR_MC_INTFACELOCAL(mcaddr)) {
1254 KASSERT(mcaddr->s6_addr16[1] != 0,
1255 ("%s: scope zone ID not set", __func__));
1258 IN6_MULTI_LOCK_ASSERT();
1259 IN6_MULTI_LIST_UNLOCK_ASSERT();
1261 CTR4(KTR_MLD, "%s: join %s on %p(%s))", __func__,
1262 ip6_sprintf(ip6tbuf, mcaddr), ifp, if_name(ifp));
1268 * If no imf was specified (i.e. kernel consumer),
1269 * fake one up and assume it is an ASM join.
1272 im6f_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1275 error = in6_getmulti(ifp, mcaddr, &inm);
1277 CTR1(KTR_MLD, "%s: in6_getmulti() failure", __func__);
1281 IN6_MULTI_LIST_LOCK();
1282 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1283 error = in6m_merge(inm, imf);
1285 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1286 goto out_in6m_release;
1289 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1290 error = mld_change_state(inm, delay);
1292 CTR1(KTR_MLD, "%s: failed to update source", __func__);
1293 goto out_in6m_release;
1299 struct epoch_tracker et;
1301 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1303 NET_EPOCH_ENTER(et);
1304 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1305 if (ifma->ifma_protospec == inm) {
1306 ifma->ifma_protospec = NULL;
1310 in6m_disconnect_locked(&inmh, inm);
1311 in6m_rele_locked(&inmh, inm);
1313 IF_ADDR_WUNLOCK(ifp);
1317 IN6_MULTI_LIST_UNLOCK();
1318 in6m_release_list_deferred(&inmh);
1323 * Leave a multicast group; unlocked entry point.
1326 in6_leavegroup(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1331 error = in6_leavegroup_locked(inm, imf);
1337 * Leave a multicast group; real entry point.
1338 * All source filters will be expunged.
1340 * Only preserves atomicity at inm level.
1342 * Holding the write lock for the INP which contains imf
1343 * is highly advisable. We can't assert for it as imf does not
1344 * contain a back-pointer to the owning inp.
1346 * Note: This is not the same as in6m_release(*) as this function also
1347 * makes a state change downcall into MLD.
1350 in6_leavegroup_locked(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1352 struct in6_multi_head inmh;
1353 struct in6_mfilter timf;
1357 char ip6tbuf[INET6_ADDRSTRLEN];
1362 IN6_MULTI_LOCK_ASSERT();
1364 CTR5(KTR_MLD, "%s: leave inm %p, %s/%s, imf %p", __func__,
1365 inm, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1366 (in6m_is_ifp_detached(inm) ? "null" : if_name(inm->in6m_ifp)),
1370 * If no imf was specified (i.e. kernel consumer),
1371 * fake one up and assume it is an ASM join.
1374 im6f_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1379 * Begin state merge transaction at MLD layer.
1381 * As this particular invocation should not cause any memory
1382 * to be allocated, and there is no opportunity to roll back
1383 * the transaction, it MUST NOT fail.
1386 ifp = inm->in6m_ifp;
1387 IN6_MULTI_LIST_LOCK();
1388 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1389 error = in6m_merge(inm, imf);
1390 KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1392 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1395 error = mld_change_state(inm, 0);
1397 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1399 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1404 if (inm->in6m_refcount == 1)
1405 in6m_disconnect_locked(&inmh, inm);
1406 in6m_rele_locked(&inmh, inm);
1408 IF_ADDR_WUNLOCK(ifp);
1409 IN6_MULTI_LIST_UNLOCK();
1410 in6m_release_list_deferred(&inmh);
1415 * Block or unblock an ASM multicast source on an inpcb.
1416 * This implements the delta-based API described in RFC 3678.
1418 * The delta-based API applies only to exclusive-mode memberships.
1419 * An MLD downcall will be performed.
1421 * Return 0 if successful, otherwise return an appropriate error code.
1424 in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1426 struct group_source_req gsr;
1427 struct epoch_tracker et;
1428 sockunion_t *gsa, *ssa;
1430 struct in6_mfilter *imf;
1431 struct ip6_moptions *imo;
1432 struct in6_msource *ims;
1433 struct in6_multi *inm;
1437 char ip6tbuf[INET6_ADDRSTRLEN];
1444 memset(&gsr, 0, sizeof(struct group_source_req));
1445 gsa = (sockunion_t *)&gsr.gsr_group;
1446 ssa = (sockunion_t *)&gsr.gsr_source;
1448 switch (sopt->sopt_name) {
1449 case MCAST_BLOCK_SOURCE:
1450 case MCAST_UNBLOCK_SOURCE:
1451 error = sooptcopyin(sopt, &gsr,
1452 sizeof(struct group_source_req),
1453 sizeof(struct group_source_req));
1457 if (gsa->sin6.sin6_family != AF_INET6 ||
1458 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1461 if (ssa->sin6.sin6_family != AF_INET6 ||
1462 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1466 * XXXGL: this function should use ifnet_byindex_ref, or
1467 * expand the epoch section all the way to where we put
1470 NET_EPOCH_ENTER(et);
1471 ifp = ifnet_byindex(gsr.gsr_interface);
1474 return (EADDRNOTAVAIL);
1476 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1481 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1482 __func__, sopt->sopt_name);
1483 return (EOPNOTSUPP);
1487 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1490 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1493 * Check if we are actually a member of this group.
1495 imo = in6p_findmoptions(inp);
1496 imf = im6o_match_group(imo, ifp, &gsa->sa);
1498 error = EADDRNOTAVAIL;
1499 goto out_in6p_locked;
1501 inm = imf->im6f_in6m;
1504 * Attempting to use the delta-based API on an
1505 * non exclusive-mode membership is an error.
1507 fmode = imf->im6f_st[0];
1508 if (fmode != MCAST_EXCLUDE) {
1510 goto out_in6p_locked;
1514 * Deal with error cases up-front:
1515 * Asked to block, but already blocked; or
1516 * Asked to unblock, but nothing to unblock.
1517 * If adding a new block entry, allocate it.
1519 ims = im6o_match_source(imf, &ssa->sa);
1520 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1521 CTR3(KTR_MLD, "%s: source %s %spresent", __func__,
1522 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
1523 doblock ? "" : "not ");
1524 error = EADDRNOTAVAIL;
1525 goto out_in6p_locked;
1528 INP_WLOCK_ASSERT(inp);
1531 * Begin state merge transaction at socket layer.
1534 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
1535 ims = im6f_graft(imf, fmode, &ssa->sin6);
1539 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
1540 error = im6f_prune(imf, &ssa->sin6);
1544 CTR1(KTR_MLD, "%s: merge imf state failed", __func__);
1545 goto out_im6f_rollback;
1549 * Begin state merge transaction at MLD layer.
1551 IN6_MULTI_LIST_LOCK();
1552 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1553 error = in6m_merge(inm, imf);
1555 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1557 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1558 error = mld_change_state(inm, 0);
1560 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1563 IN6_MULTI_LIST_UNLOCK();
1579 * Given an inpcb, return its multicast options structure pointer. Accepts
1580 * an unlocked inpcb pointer, but will return it locked. May sleep.
1582 * SMPng: NOTE: Returns with the INP write lock held.
1584 static struct ip6_moptions *
1585 in6p_findmoptions(struct inpcb *inp)
1587 struct ip6_moptions *imo;
1590 if (inp->in6p_moptions != NULL)
1591 return (inp->in6p_moptions);
1595 imo = malloc(sizeof(*imo), M_IP6MOPTS, M_WAITOK);
1597 imo->im6o_multicast_ifp = NULL;
1598 imo->im6o_multicast_hlim = V_ip6_defmcasthlim;
1599 imo->im6o_multicast_loop = in6_mcast_loop;
1600 STAILQ_INIT(&imo->im6o_head);
1603 if (inp->in6p_moptions != NULL) {
1604 free(imo, M_IP6MOPTS);
1605 return (inp->in6p_moptions);
1607 inp->in6p_moptions = imo;
1612 * Discard the IPv6 multicast options (and source filters).
1614 * SMPng: NOTE: assumes INP write lock is held.
1616 * XXX can all be safely deferred to epoch_call
1621 inp_gcmoptions(struct ip6_moptions *imo)
1623 struct in6_mfilter *imf;
1624 struct in6_multi *inm;
1627 while ((imf = ip6_mfilter_first(&imo->im6o_head)) != NULL) {
1628 ip6_mfilter_remove(&imo->im6o_head, imf);
1631 if ((inm = imf->im6f_in6m) != NULL) {
1632 if ((ifp = inm->in6m_ifp) != NULL) {
1633 CURVNET_SET(ifp->if_vnet);
1634 (void)in6_leavegroup(inm, imf);
1637 (void)in6_leavegroup(inm, imf);
1640 ip6_mfilter_free(imf);
1642 free(imo, M_IP6MOPTS);
1646 ip6_freemoptions(struct ip6_moptions *imo)
1650 inp_gcmoptions(imo);
1654 * Atomically get source filters on a socket for an IPv6 multicast group.
1655 * Called with INP lock held; returns with lock released.
1658 in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1660 struct epoch_tracker et;
1661 struct __msfilterreq msfr;
1664 struct ip6_moptions *imo;
1665 struct in6_mfilter *imf;
1666 struct ip6_msource *ims;
1667 struct in6_msource *lims;
1668 struct sockaddr_in6 *psin;
1669 struct sockaddr_storage *ptss;
1670 struct sockaddr_storage *tss;
1672 size_t nsrcs, ncsrcs;
1674 INP_WLOCK_ASSERT(inp);
1676 imo = inp->in6p_moptions;
1677 KASSERT(imo != NULL, ("%s: null ip6_moptions", __func__));
1681 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1682 sizeof(struct __msfilterreq));
1686 if (msfr.msfr_group.ss_family != AF_INET6 ||
1687 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
1690 gsa = (sockunion_t *)&msfr.msfr_group;
1691 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1695 * XXXGL: this function should use ifnet_byindex_ref, or expand the
1696 * epoch section all the way to where the interface is referenced.
1698 NET_EPOCH_ENTER(et);
1699 ifp = ifnet_byindex(msfr.msfr_ifindex);
1702 return (EADDRNOTAVAIL);
1703 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1708 * Lookup group on the socket.
1710 imf = im6o_match_group(imo, ifp, &gsa->sa);
1713 return (EADDRNOTAVAIL);
1717 * Ignore memberships which are in limbo.
1719 if (imf->im6f_st[1] == MCAST_UNDEFINED) {
1723 msfr.msfr_fmode = imf->im6f_st[1];
1726 * If the user specified a buffer, copy out the source filter
1727 * entries to userland gracefully.
1728 * We only copy out the number of entries which userland
1729 * has asked for, but we always tell userland how big the
1730 * buffer really needs to be.
1732 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
1733 msfr.msfr_nsrcs = in6_mcast_maxsocksrc;
1735 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1736 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1737 M_TEMP, M_NOWAIT | M_ZERO);
1745 * Count number of sources in-mode at t0.
1746 * If buffer space exists and remains, copy out source entries.
1748 nsrcs = msfr.msfr_nsrcs;
1751 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
1752 lims = (struct in6_msource *)ims;
1753 if (lims->im6sl_st[0] == MCAST_UNDEFINED ||
1754 lims->im6sl_st[0] != imf->im6f_st[0])
1757 if (tss != NULL && nsrcs > 0) {
1758 psin = (struct sockaddr_in6 *)ptss;
1759 psin->sin6_family = AF_INET6;
1760 psin->sin6_len = sizeof(struct sockaddr_in6);
1761 psin->sin6_addr = lims->im6s_addr;
1762 psin->sin6_port = 0;
1771 error = copyout(tss, msfr.msfr_srcs,
1772 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1778 msfr.msfr_nsrcs = ncsrcs;
1779 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1785 * Return the IP multicast options in response to user getsockopt().
1788 ip6_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1790 struct ip6_moptions *im6o;
1795 im6o = inp->in6p_moptions;
1796 /* If socket is neither of type SOCK_RAW or SOCK_DGRAM, reject it. */
1797 if (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1798 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM) {
1800 return (EOPNOTSUPP);
1804 switch (sopt->sopt_name) {
1805 case IPV6_MULTICAST_IF:
1806 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) {
1809 optval = im6o->im6o_multicast_ifp->if_index;
1812 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1815 case IPV6_MULTICAST_HOPS:
1817 optval = V_ip6_defmcasthlim;
1819 optval = im6o->im6o_multicast_hlim;
1821 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1824 case IPV6_MULTICAST_LOOP:
1826 optval = in6_mcast_loop; /* XXX VIMAGE */
1828 optval = im6o->im6o_multicast_loop;
1830 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1835 error = EADDRNOTAVAIL;
1838 error = in6p_get_source_filters(inp, sopt);
1844 error = ENOPROTOOPT;
1848 INP_UNLOCK_ASSERT(inp);
1854 * Look up the ifnet to use for a multicast group membership,
1855 * given the address of an IPv6 group.
1857 * This routine exists to support legacy IPv6 multicast applications.
1859 * Use the socket's current FIB number for any required FIB lookup. Look up the
1860 * group address in the unicast FIB, and use its ifp; usually, this points to
1861 * the default next-hop. If the FIB lookup fails, return NULL.
1863 * FUTURE: Support multiple forwarding tables for IPv6.
1865 * Returns NULL if no ifp could be found.
1867 static struct ifnet *
1868 in6p_lookup_mcast_ifp(const struct inpcb *inp, const struct sockaddr_in6 *gsin6)
1870 struct nhop_object *nh;
1871 struct in6_addr dst;
1875 KASSERT(gsin6->sin6_family == AF_INET6,
1876 ("%s: not AF_INET6 group", __func__));
1878 in6_splitscope(&gsin6->sin6_addr, &dst, &scopeid);
1879 fibnum = inp->inp_inc.inc_fibnum;
1880 nh = fib6_lookup(fibnum, &dst, scopeid, 0, 0);
1882 return (nh ? nh->nh_ifp : NULL);
1886 * Join an IPv6 multicast group, possibly with a source.
1888 * FIXME: The KAME use of the unspecified address (::)
1889 * to join *all* multicast groups is currently unsupported.
1891 * XXXGL: this function multiple times uses ifnet_byindex() without
1892 * proper protection - staying in epoch, or putting reference on ifnet.
1895 in6p_join_group(struct inpcb *inp, struct sockopt *sopt)
1897 struct in6_multi_head inmh;
1898 struct group_source_req gsr;
1899 struct epoch_tracker et;
1900 sockunion_t *gsa, *ssa;
1902 struct in6_mfilter *imf;
1903 struct ip6_moptions *imo;
1904 struct in6_multi *inm;
1905 struct in6_msource *lims;
1913 memset(&gsr, 0, sizeof(struct group_source_req));
1914 gsa = (sockunion_t *)&gsr.gsr_group;
1915 gsa->ss.ss_family = AF_UNSPEC;
1916 ssa = (sockunion_t *)&gsr.gsr_source;
1917 ssa->ss.ss_family = AF_UNSPEC;
1920 * Chew everything into struct group_source_req.
1921 * Overwrite the port field if present, as the sockaddr
1922 * being copied in may be matched with a binary comparison.
1923 * Ignore passed-in scope ID.
1925 switch (sopt->sopt_name) {
1926 case IPV6_JOIN_GROUP: {
1927 struct ipv6_mreq mreq;
1929 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
1930 sizeof(struct ipv6_mreq));
1934 gsa->sin6.sin6_family = AF_INET6;
1935 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
1936 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
1938 if (mreq.ipv6mr_interface == 0) {
1939 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
1941 NET_EPOCH_ENTER(et);
1942 ifp = ifnet_byindex(mreq.ipv6mr_interface);
1945 return (EADDRNOTAVAIL);
1947 CTR3(KTR_MLD, "%s: ipv6mr_interface = %d, ifp = %p",
1948 __func__, mreq.ipv6mr_interface, ifp);
1951 case MCAST_JOIN_GROUP:
1952 case MCAST_JOIN_SOURCE_GROUP:
1953 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1954 error = sooptcopyin(sopt, &gsr,
1955 sizeof(struct group_req),
1956 sizeof(struct group_req));
1957 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1958 error = sooptcopyin(sopt, &gsr,
1959 sizeof(struct group_source_req),
1960 sizeof(struct group_source_req));
1965 if (gsa->sin6.sin6_family != AF_INET6 ||
1966 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1969 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1970 if (ssa->sin6.sin6_family != AF_INET6 ||
1971 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1973 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
1976 * TODO: Validate embedded scope ID in source
1977 * list entry against passed-in ifp, if and only
1978 * if source list filter entry is iface or node local.
1980 in6_clearscope(&ssa->sin6.sin6_addr);
1981 ssa->sin6.sin6_port = 0;
1982 ssa->sin6.sin6_scope_id = 0;
1984 NET_EPOCH_ENTER(et);
1985 ifp = ifnet_byindex(gsr.gsr_interface);
1988 return (EADDRNOTAVAIL);
1992 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1993 __func__, sopt->sopt_name);
1994 return (EOPNOTSUPP);
1998 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2001 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
2002 return (EADDRNOTAVAIL);
2004 gsa->sin6.sin6_port = 0;
2005 gsa->sin6.sin6_scope_id = 0;
2008 * Always set the scope zone ID on memberships created from userland.
2009 * Use the passed-in ifp to do this.
2010 * XXX The in6_setscope() return value is meaningless.
2011 * XXX SCOPE6_LOCK() is taken by in6_setscope().
2013 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2018 * Find the membership in the membership list.
2020 imo = in6p_findmoptions(inp);
2021 imf = im6o_match_group(imo, ifp, &gsa->sa);
2026 if (ip6_mfilter_count(&imo->im6o_head) >= IPV6_MAX_MEMBERSHIPS) {
2028 goto out_in6p_locked;
2032 inm = imf->im6f_in6m;
2034 if (ssa->ss.ss_family != AF_UNSPEC) {
2036 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2037 * is an error. On an existing inclusive membership,
2038 * it just adds the source to the filter list.
2040 if (imf->im6f_st[1] != MCAST_INCLUDE) {
2042 goto out_in6p_locked;
2045 * Throw out duplicates.
2047 * XXX FIXME: This makes a naive assumption that
2048 * even if entries exist for *ssa in this imf,
2049 * they will be rejected as dupes, even if they
2050 * are not valid in the current mode (in-mode).
2052 * in6_msource is transactioned just as for anything
2053 * else in SSM -- but note naive use of in6m_graft()
2054 * below for allocating new filter entries.
2056 * This is only an issue if someone mixes the
2057 * full-state SSM API with the delta-based API,
2058 * which is discouraged in the relevant RFCs.
2060 lims = im6o_match_source(imf, &ssa->sa);
2061 if (lims != NULL /*&&
2062 lims->im6sl_st[1] == MCAST_INCLUDE*/) {
2063 error = EADDRNOTAVAIL;
2064 goto out_in6p_locked;
2068 * MCAST_JOIN_GROUP alone, on any existing membership,
2069 * is rejected, to stop the same inpcb tying up
2070 * multiple refs to the in_multi.
2071 * On an existing inclusive membership, this is also
2072 * an error; if you want to change filter mode,
2073 * you must use the userland API setsourcefilter().
2074 * XXX We don't reject this for imf in UNDEFINED
2075 * state at t1, because allocation of a filter
2076 * is atomic with allocation of a membership.
2079 goto out_in6p_locked;
2084 * Begin state merge transaction at socket layer.
2086 INP_WLOCK_ASSERT(inp);
2089 * Graft new source into filter list for this inpcb's
2090 * membership of the group. The in6_multi may not have
2091 * been allocated yet if this is a new membership, however,
2092 * the in_mfilter slot will be allocated and must be initialized.
2094 * Note: Grafting of exclusive mode filters doesn't happen
2096 * XXX: Should check for non-NULL lims (node exists but may
2097 * not be in-mode) for interop with full-state API.
2099 if (ssa->ss.ss_family != AF_UNSPEC) {
2100 /* Membership starts in IN mode */
2102 CTR1(KTR_MLD, "%s: new join w/source", __func__);
2103 imf = ip6_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_INCLUDE);
2106 goto out_in6p_locked;
2109 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
2111 lims = im6f_graft(imf, MCAST_INCLUDE, &ssa->sin6);
2113 CTR1(KTR_MLD, "%s: merge imf state failed",
2116 goto out_in6p_locked;
2119 /* No address specified; Membership starts in EX mode */
2121 CTR1(KTR_MLD, "%s: new join w/o source", __func__);
2122 imf = ip6_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_EXCLUDE);
2125 goto out_in6p_locked;
2131 * Begin state merge transaction at MLD layer.
2137 error = in6_joingroup_locked(ifp, &gsa->sin6.sin6_addr, imf,
2138 &imf->im6f_in6m, 0);
2141 if (in_pcbrele_wlocked(inp)) {
2143 goto out_in6p_unlocked;
2146 goto out_in6p_locked;
2149 * NOTE: Refcount from in6_joingroup_locked()
2150 * is protecting membership.
2152 ip6_mfilter_insert(&imo->im6o_head, imf);
2154 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2155 IN6_MULTI_LIST_LOCK();
2156 error = in6m_merge(inm, imf);
2158 CTR1(KTR_MLD, "%s: failed to merge inm state",
2160 IN6_MULTI_LIST_UNLOCK();
2163 goto out_in6p_locked;
2165 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2166 error = mld_change_state(inm, 0);
2167 IN6_MULTI_LIST_UNLOCK();
2170 CTR1(KTR_MLD, "%s: failed mld downcall",
2174 goto out_in6p_locked;
2186 if (is_new && imf) {
2187 if (imf->im6f_in6m != NULL) {
2188 struct in6_multi_head inmh;
2191 SLIST_INSERT_HEAD(&inmh, imf->im6f_in6m, in6m_defer);
2192 in6m_release_list_deferred(&inmh);
2194 ip6_mfilter_free(imf);
2200 * Leave an IPv6 multicast group on an inpcb, possibly with a source.
2203 in6p_leave_group(struct inpcb *inp, struct sockopt *sopt)
2205 struct ipv6_mreq mreq;
2206 struct group_source_req gsr;
2207 struct epoch_tracker et;
2208 sockunion_t *gsa, *ssa;
2210 struct in6_mfilter *imf;
2211 struct ip6_moptions *imo;
2212 struct in6_msource *ims;
2213 struct in6_multi *inm;
2218 char ip6tbuf[INET6_ADDRSTRLEN];
2226 memset(&gsr, 0, sizeof(struct group_source_req));
2227 gsa = (sockunion_t *)&gsr.gsr_group;
2228 gsa->ss.ss_family = AF_UNSPEC;
2229 ssa = (sockunion_t *)&gsr.gsr_source;
2230 ssa->ss.ss_family = AF_UNSPEC;
2233 * Chew everything passed in up into a struct group_source_req
2234 * as that is easier to process.
2235 * Note: Any embedded scope ID in the multicast group passed
2236 * in by userland is ignored, the interface index is the recommended
2237 * mechanism to specify an interface; see below.
2239 switch (sopt->sopt_name) {
2240 case IPV6_LEAVE_GROUP:
2241 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
2242 sizeof(struct ipv6_mreq));
2245 gsa->sin6.sin6_family = AF_INET6;
2246 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
2247 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
2248 gsa->sin6.sin6_port = 0;
2249 gsa->sin6.sin6_scope_id = 0;
2250 ifindex = mreq.ipv6mr_interface;
2253 case MCAST_LEAVE_GROUP:
2254 case MCAST_LEAVE_SOURCE_GROUP:
2255 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2256 error = sooptcopyin(sopt, &gsr,
2257 sizeof(struct group_req),
2258 sizeof(struct group_req));
2259 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2260 error = sooptcopyin(sopt, &gsr,
2261 sizeof(struct group_source_req),
2262 sizeof(struct group_source_req));
2267 if (gsa->sin6.sin6_family != AF_INET6 ||
2268 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2270 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2271 if (ssa->sin6.sin6_family != AF_INET6 ||
2272 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2274 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
2277 * TODO: Validate embedded scope ID in source
2278 * list entry against passed-in ifp, if and only
2279 * if source list filter entry is iface or node local.
2281 in6_clearscope(&ssa->sin6.sin6_addr);
2283 gsa->sin6.sin6_port = 0;
2284 gsa->sin6.sin6_scope_id = 0;
2285 ifindex = gsr.gsr_interface;
2289 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
2290 __func__, sopt->sopt_name);
2291 return (EOPNOTSUPP);
2295 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2299 * Validate interface index if provided. If no interface index
2300 * was provided separately, attempt to look the membership up
2301 * from the default scope as a last resort to disambiguate
2302 * the membership we are being asked to leave.
2303 * XXX SCOPE6 lock potentially taken here.
2306 NET_EPOCH_ENTER(et);
2307 ifp = ifnet_byindex(ifindex);
2308 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */
2310 return (EADDRNOTAVAIL);
2311 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2313 error = sa6_embedscope(&gsa->sin6, V_ip6_use_defzone);
2315 return (EADDRNOTAVAIL);
2317 * Some badly behaved applications don't pass an ifindex
2318 * or a scope ID, which is an API violation. In this case,
2319 * perform a lookup as per a v6 join.
2321 * XXX For now, stomp on zone ID for the corner case.
2322 * This is not the 'KAME way', but we need to see the ifp
2323 * directly until such time as this implementation is
2324 * refactored, assuming the scope IDs are the way to go.
2326 ifindex = ntohs(gsa->sin6.sin6_addr.s6_addr16[1]);
2328 CTR2(KTR_MLD, "%s: warning: no ifindex, looking up "
2329 "ifp for group %s.", __func__,
2330 ip6_sprintf(ip6tbuf, &gsa->sin6.sin6_addr));
2331 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
2333 NET_EPOCH_ENTER(et);
2334 ifp = ifnet_byindex(ifindex);
2335 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */
2338 return (EADDRNOTAVAIL);
2341 CTR2(KTR_MLD, "%s: ifp = %p", __func__, ifp);
2342 KASSERT(ifp != NULL, ("%s: ifp did not resolve", __func__));
2347 * Find the membership in the membership list.
2349 imo = in6p_findmoptions(inp);
2350 imf = im6o_match_group(imo, ifp, &gsa->sa);
2352 error = EADDRNOTAVAIL;
2353 goto out_in6p_locked;
2355 inm = imf->im6f_in6m;
2357 if (ssa->ss.ss_family != AF_UNSPEC)
2361 * Begin state merge transaction at socket layer.
2363 INP_WLOCK_ASSERT(inp);
2366 * If we were instructed only to leave a given source, do so.
2367 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2370 ip6_mfilter_remove(&imo->im6o_head, imf);
2374 * Give up the multicast address record to which
2375 * the membership points.
2377 (void)in6_leavegroup_locked(inm, imf);
2379 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
2380 error = EADDRNOTAVAIL;
2381 goto out_in6p_locked;
2383 ims = im6o_match_source(imf, &ssa->sa);
2385 CTR3(KTR_MLD, "%s: source %p %spresent", __func__,
2386 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
2388 error = EADDRNOTAVAIL;
2389 goto out_in6p_locked;
2391 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
2392 error = im6f_prune(imf, &ssa->sin6);
2394 CTR1(KTR_MLD, "%s: merge imf state failed",
2396 goto out_in6p_locked;
2401 * Begin state merge transaction at MLD layer.
2404 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2405 IN6_MULTI_LIST_LOCK();
2406 error = in6m_merge(inm, imf);
2408 CTR1(KTR_MLD, "%s: failed to merge inm state",
2410 IN6_MULTI_LIST_UNLOCK();
2413 goto out_in6p_locked;
2416 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2417 error = mld_change_state(inm, 0);
2418 IN6_MULTI_LIST_UNLOCK();
2420 CTR1(KTR_MLD, "%s: failed mld downcall",
2424 goto out_in6p_locked;
2434 if (is_final && imf)
2435 ip6_mfilter_free(imf);
2442 * Select the interface for transmitting IPv6 multicast datagrams.
2444 * Either an instance of struct in6_addr or an instance of struct ipv6_mreqn
2445 * may be passed to this socket option. An address of in6addr_any or an
2446 * interface index of 0 is used to remove a previous selection.
2447 * When no interface is selected, one is chosen for every send.
2450 in6p_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2452 struct epoch_tracker et;
2454 struct ip6_moptions *imo;
2458 if (sopt->sopt_valsize != sizeof(u_int))
2461 error = sooptcopyin(sopt, &ifindex, sizeof(u_int), sizeof(u_int));
2464 NET_EPOCH_ENTER(et);
2468 ifp = ifnet_byindex(ifindex);
2469 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2471 return (EADDRNOTAVAIL);
2474 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */
2475 imo = in6p_findmoptions(inp);
2476 imo->im6o_multicast_ifp = ifp;
2483 * Atomically set source filters on a socket for an IPv6 multicast group.
2485 * XXXGL: unsafely exits epoch with ifnet pointer
2488 in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2490 struct __msfilterreq msfr;
2491 struct epoch_tracker et;
2494 struct in6_mfilter *imf;
2495 struct ip6_moptions *imo;
2496 struct in6_multi *inm;
2499 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2500 sizeof(struct __msfilterreq));
2504 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
2507 if (msfr.msfr_fmode != MCAST_EXCLUDE &&
2508 msfr.msfr_fmode != MCAST_INCLUDE)
2511 if (msfr.msfr_group.ss_family != AF_INET6 ||
2512 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
2515 gsa = (sockunion_t *)&msfr.msfr_group;
2516 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2519 gsa->sin6.sin6_port = 0; /* ignore port */
2521 NET_EPOCH_ENTER(et);
2522 ifp = ifnet_byindex(msfr.msfr_ifindex);
2525 return (EADDRNOTAVAIL);
2526 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2529 * Take the INP write lock.
2530 * Check if this socket is a member of this group.
2532 imo = in6p_findmoptions(inp);
2533 imf = im6o_match_group(imo, ifp, &gsa->sa);
2535 error = EADDRNOTAVAIL;
2536 goto out_in6p_locked;
2538 inm = imf->im6f_in6m;
2541 * Begin state merge transaction at socket layer.
2543 INP_WLOCK_ASSERT(inp);
2545 imf->im6f_st[1] = msfr.msfr_fmode;
2548 * Apply any new source filters, if present.
2549 * Make a copy of the user-space source vector so
2550 * that we may copy them with a single copyin. This
2551 * allows us to deal with page faults up-front.
2553 if (msfr.msfr_nsrcs > 0) {
2554 struct in6_msource *lims;
2555 struct sockaddr_in6 *psin;
2556 struct sockaddr_storage *kss, *pkss;
2561 CTR2(KTR_MLD, "%s: loading %lu source list entries",
2562 __func__, (unsigned long)msfr.msfr_nsrcs);
2563 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2565 error = copyin(msfr.msfr_srcs, kss,
2566 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2575 * Mark all source filters as UNDEFINED at t1.
2576 * Restore new group filter mode, as im6f_leave()
2577 * will set it to INCLUDE.
2580 imf->im6f_st[1] = msfr.msfr_fmode;
2583 * Update socket layer filters at t1, lazy-allocating
2584 * new entries. This saves a bunch of memory at the
2585 * cost of one RB_FIND() per source entry; duplicate
2586 * entries in the msfr_nsrcs vector are ignored.
2587 * If we encounter an error, rollback transaction.
2589 * XXX This too could be replaced with a set-symmetric
2590 * difference like loop to avoid walking from root
2591 * every time, as the key space is common.
2593 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2594 psin = (struct sockaddr_in6 *)pkss;
2595 if (psin->sin6_family != AF_INET6) {
2596 error = EAFNOSUPPORT;
2599 if (psin->sin6_len != sizeof(struct sockaddr_in6)) {
2603 if (IN6_IS_ADDR_MULTICAST(&psin->sin6_addr)) {
2608 * TODO: Validate embedded scope ID in source
2609 * list entry against passed-in ifp, if and only
2610 * if source list filter entry is iface or node local.
2612 in6_clearscope(&psin->sin6_addr);
2613 error = im6f_get_source(imf, psin, &lims);
2616 lims->im6sl_st[1] = imf->im6f_st[1];
2622 goto out_im6f_rollback;
2624 INP_WLOCK_ASSERT(inp);
2625 IN6_MULTI_LIST_LOCK();
2628 * Begin state merge transaction at MLD layer.
2630 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2631 error = in6m_merge(inm, imf);
2633 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
2635 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2636 error = mld_change_state(inm, 0);
2638 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
2641 IN6_MULTI_LIST_UNLOCK();
2657 * Set the IP multicast options in response to user setsockopt().
2659 * Many of the socket options handled in this function duplicate the
2660 * functionality of socket options in the regular unicast API. However,
2661 * it is not possible to merge the duplicate code, because the idempotence
2662 * of the IPv6 multicast part of the BSD Sockets API must be preserved;
2663 * the effects of these options must be treated as separate and distinct.
2665 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2668 ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2670 struct ip6_moptions *im6o;
2675 /* If socket is neither of type SOCK_RAW or SOCK_DGRAM, reject it. */
2676 if (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2677 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)
2678 return (EOPNOTSUPP);
2680 switch (sopt->sopt_name) {
2681 case IPV6_MULTICAST_IF:
2682 error = in6p_set_multicast_if(inp, sopt);
2685 case IPV6_MULTICAST_HOPS: {
2688 if (sopt->sopt_valsize != sizeof(int)) {
2692 error = sooptcopyin(sopt, &hlim, sizeof(hlim), sizeof(int));
2695 if (hlim < -1 || hlim > 255) {
2698 } else if (hlim == -1) {
2699 hlim = V_ip6_defmcasthlim;
2701 im6o = in6p_findmoptions(inp);
2702 im6o->im6o_multicast_hlim = hlim;
2707 case IPV6_MULTICAST_LOOP: {
2711 * Set the loopback flag for outgoing multicast packets.
2712 * Must be zero or one.
2714 if (sopt->sopt_valsize != sizeof(u_int)) {
2718 error = sooptcopyin(sopt, &loop, sizeof(u_int), sizeof(u_int));
2725 im6o = in6p_findmoptions(inp);
2726 im6o->im6o_multicast_loop = loop;
2731 case IPV6_JOIN_GROUP:
2732 case MCAST_JOIN_GROUP:
2733 case MCAST_JOIN_SOURCE_GROUP:
2734 error = in6p_join_group(inp, sopt);
2737 case IPV6_LEAVE_GROUP:
2738 case MCAST_LEAVE_GROUP:
2739 case MCAST_LEAVE_SOURCE_GROUP:
2740 error = in6p_leave_group(inp, sopt);
2743 case MCAST_BLOCK_SOURCE:
2744 case MCAST_UNBLOCK_SOURCE:
2745 error = in6p_block_unblock_source(inp, sopt);
2749 error = in6p_set_source_filters(inp, sopt);
2757 INP_UNLOCK_ASSERT(inp);
2763 * Expose MLD's multicast filter mode and source list(s) to userland,
2764 * keyed by (ifindex, group).
2765 * The filter mode is written out as a uint32_t, followed by
2766 * 0..n of struct in6_addr.
2767 * For use by ifmcstat(8).
2768 * SMPng: NOTE: unlocked read of ifindex space.
2771 sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS)
2773 struct in6_addr mcaddr;
2774 struct in6_addr src;
2775 struct epoch_tracker et;
2777 struct ifmultiaddr *ifma;
2778 struct in6_multi *inm;
2779 struct ip6_msource *ims;
2783 uint32_t fmode, ifindex;
2785 char ip6tbuf[INET6_ADDRSTRLEN];
2791 if (req->newptr != NULL)
2794 /* int: ifindex + 4 * 32 bits of IPv6 address */
2798 memcpy(&mcaddr, &name[1], sizeof(struct in6_addr));
2799 if (!IN6_IS_ADDR_MULTICAST(&mcaddr)) {
2800 CTR2(KTR_MLD, "%s: group %s is not multicast",
2801 __func__, ip6_sprintf(ip6tbuf, &mcaddr));
2806 NET_EPOCH_ENTER(et);
2807 ifp = ifnet_byindex(ifindex);
2810 CTR2(KTR_MLD, "%s: no ifp for ifindex %u",
2815 * Internal MLD lookups require that scope/zone ID is set.
2817 (void)in6_setscope(&mcaddr, ifp, NULL);
2819 retval = sysctl_wire_old_buffer(req,
2820 sizeof(uint32_t) + (in6_mcast_maxgrpsrc * sizeof(struct in6_addr)));
2827 IN6_MULTI_LIST_LOCK();
2828 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2829 inm = in6m_ifmultiaddr_get_inm(ifma);
2832 if (!IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, &mcaddr))
2834 fmode = inm->in6m_st[1].iss_fmode;
2835 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2838 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
2839 CTR2(KTR_MLD, "%s: visit node %p", __func__, ims);
2841 * Only copy-out sources which are in-mode.
2843 if (fmode != im6s_get_mode(inm, ims, 1)) {
2844 CTR1(KTR_MLD, "%s: skip non-in-mode",
2848 src = ims->im6s_addr;
2849 retval = SYSCTL_OUT(req, &src,
2850 sizeof(struct in6_addr));
2855 IN6_MULTI_LIST_UNLOCK();
2864 static const char *in6m_modestrs[] = { "un", "in", "ex" };
2867 in6m_mode_str(const int mode)
2870 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2871 return (in6m_modestrs[mode]);
2875 static const char *in6m_statestrs[] = {
2887 _Static_assert(nitems(in6m_statestrs) ==
2888 MLD_LEAVING_MEMBER - MLD_NOT_MEMBER + 1, "Missing MLD group state");
2891 in6m_state_str(const int state)
2894 if (state >= MLD_NOT_MEMBER && state <= MLD_LEAVING_MEMBER)
2895 return (in6m_statestrs[state]);
2900 * Dump an in6_multi structure to the console.
2903 in6m_print(const struct in6_multi *inm)
2906 char ip6tbuf[INET6_ADDRSTRLEN];
2908 if ((ktr_mask & KTR_MLD) == 0)
2911 printf("%s: --- begin in6m %p ---\n", __func__, inm);
2912 printf("addr %s ifp %p(%s) ifma %p\n",
2913 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2915 if_name(inm->in6m_ifp),
2917 printf("timer %u state %s refcount %u scq.len %u\n",
2919 in6m_state_str(inm->in6m_state),
2921 mbufq_len(&inm->in6m_scq));
2922 printf("mli %p nsrc %lu sctimer %u scrv %u\n",
2927 for (t = 0; t < 2; t++) {
2928 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
2929 in6m_mode_str(inm->in6m_st[t].iss_fmode),
2930 inm->in6m_st[t].iss_asm,
2931 inm->in6m_st[t].iss_ex,
2932 inm->in6m_st[t].iss_in,
2933 inm->in6m_st[t].iss_rec);
2935 printf("%s: --- end in6m %p ---\n", __func__, inm);
2941 in6m_print(const struct in6_multi *inm)