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: Giant, IN6_MULTI_LOCK, INP_WLOCK,
105 * IN6_MULTI_LIST_LOCK, MLD_LOCK, IF_ADDR_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 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1423 * Return 0 if successful, otherwise return an appropriate error code.
1426 in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1428 struct group_source_req gsr;
1429 struct epoch_tracker et;
1430 sockunion_t *gsa, *ssa;
1432 struct in6_mfilter *imf;
1433 struct ip6_moptions *imo;
1434 struct in6_msource *ims;
1435 struct in6_multi *inm;
1439 char ip6tbuf[INET6_ADDRSTRLEN];
1446 memset(&gsr, 0, sizeof(struct group_source_req));
1447 gsa = (sockunion_t *)&gsr.gsr_group;
1448 ssa = (sockunion_t *)&gsr.gsr_source;
1450 switch (sopt->sopt_name) {
1451 case MCAST_BLOCK_SOURCE:
1452 case MCAST_UNBLOCK_SOURCE:
1453 error = sooptcopyin(sopt, &gsr,
1454 sizeof(struct group_source_req),
1455 sizeof(struct group_source_req));
1459 if (gsa->sin6.sin6_family != AF_INET6 ||
1460 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1463 if (ssa->sin6.sin6_family != AF_INET6 ||
1464 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1468 * XXXGL: this function should use ifnet_byindex_ref, or
1469 * expand the epoch section all the way to where we put
1472 NET_EPOCH_ENTER(et);
1473 ifp = ifnet_byindex(gsr.gsr_interface);
1476 return (EADDRNOTAVAIL);
1478 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1483 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1484 __func__, sopt->sopt_name);
1485 return (EOPNOTSUPP);
1489 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1492 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1495 * Check if we are actually a member of this group.
1497 imo = in6p_findmoptions(inp);
1498 imf = im6o_match_group(imo, ifp, &gsa->sa);
1500 error = EADDRNOTAVAIL;
1501 goto out_in6p_locked;
1503 inm = imf->im6f_in6m;
1506 * Attempting to use the delta-based API on an
1507 * non exclusive-mode membership is an error.
1509 fmode = imf->im6f_st[0];
1510 if (fmode != MCAST_EXCLUDE) {
1512 goto out_in6p_locked;
1516 * Deal with error cases up-front:
1517 * Asked to block, but already blocked; or
1518 * Asked to unblock, but nothing to unblock.
1519 * If adding a new block entry, allocate it.
1521 ims = im6o_match_source(imf, &ssa->sa);
1522 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1523 CTR3(KTR_MLD, "%s: source %s %spresent", __func__,
1524 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
1525 doblock ? "" : "not ");
1526 error = EADDRNOTAVAIL;
1527 goto out_in6p_locked;
1530 INP_WLOCK_ASSERT(inp);
1533 * Begin state merge transaction at socket layer.
1536 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
1537 ims = im6f_graft(imf, fmode, &ssa->sin6);
1541 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
1542 error = im6f_prune(imf, &ssa->sin6);
1546 CTR1(KTR_MLD, "%s: merge imf state failed", __func__);
1547 goto out_im6f_rollback;
1551 * Begin state merge transaction at MLD layer.
1553 IN6_MULTI_LIST_LOCK();
1554 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1555 error = in6m_merge(inm, imf);
1557 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1559 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1560 error = mld_change_state(inm, 0);
1562 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1565 IN6_MULTI_LIST_UNLOCK();
1581 * Given an inpcb, return its multicast options structure pointer. Accepts
1582 * an unlocked inpcb pointer, but will return it locked. May sleep.
1584 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1585 * SMPng: NOTE: Returns with the INP write lock held.
1587 static struct ip6_moptions *
1588 in6p_findmoptions(struct inpcb *inp)
1590 struct ip6_moptions *imo;
1593 if (inp->in6p_moptions != NULL)
1594 return (inp->in6p_moptions);
1598 imo = malloc(sizeof(*imo), M_IP6MOPTS, M_WAITOK);
1600 imo->im6o_multicast_ifp = NULL;
1601 imo->im6o_multicast_hlim = V_ip6_defmcasthlim;
1602 imo->im6o_multicast_loop = in6_mcast_loop;
1603 STAILQ_INIT(&imo->im6o_head);
1606 if (inp->in6p_moptions != NULL) {
1607 free(imo, M_IP6MOPTS);
1608 return (inp->in6p_moptions);
1610 inp->in6p_moptions = imo;
1615 * Discard the IPv6 multicast options (and source filters).
1617 * SMPng: NOTE: assumes INP write lock is held.
1619 * XXX can all be safely deferred to epoch_call
1624 inp_gcmoptions(struct ip6_moptions *imo)
1626 struct in6_mfilter *imf;
1627 struct in6_multi *inm;
1630 while ((imf = ip6_mfilter_first(&imo->im6o_head)) != NULL) {
1631 ip6_mfilter_remove(&imo->im6o_head, imf);
1634 if ((inm = imf->im6f_in6m) != NULL) {
1635 if ((ifp = inm->in6m_ifp) != NULL) {
1636 CURVNET_SET(ifp->if_vnet);
1637 (void)in6_leavegroup(inm, imf);
1640 (void)in6_leavegroup(inm, imf);
1643 ip6_mfilter_free(imf);
1645 free(imo, M_IP6MOPTS);
1649 ip6_freemoptions(struct ip6_moptions *imo)
1653 inp_gcmoptions(imo);
1657 * Atomically get source filters on a socket for an IPv6 multicast group.
1658 * Called with INP lock held; returns with lock released.
1661 in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1663 struct epoch_tracker et;
1664 struct __msfilterreq msfr;
1667 struct ip6_moptions *imo;
1668 struct in6_mfilter *imf;
1669 struct ip6_msource *ims;
1670 struct in6_msource *lims;
1671 struct sockaddr_in6 *psin;
1672 struct sockaddr_storage *ptss;
1673 struct sockaddr_storage *tss;
1675 size_t nsrcs, ncsrcs;
1677 INP_WLOCK_ASSERT(inp);
1679 imo = inp->in6p_moptions;
1680 KASSERT(imo != NULL, ("%s: null ip6_moptions", __func__));
1684 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1685 sizeof(struct __msfilterreq));
1689 if (msfr.msfr_group.ss_family != AF_INET6 ||
1690 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
1693 gsa = (sockunion_t *)&msfr.msfr_group;
1694 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1698 * XXXGL: this function should use ifnet_byindex_ref, or expand the
1699 * epoch section all the way to where the interface is referenced.
1701 NET_EPOCH_ENTER(et);
1702 ifp = ifnet_byindex(msfr.msfr_ifindex);
1705 return (EADDRNOTAVAIL);
1706 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1711 * Lookup group on the socket.
1713 imf = im6o_match_group(imo, ifp, &gsa->sa);
1716 return (EADDRNOTAVAIL);
1720 * Ignore memberships which are in limbo.
1722 if (imf->im6f_st[1] == MCAST_UNDEFINED) {
1726 msfr.msfr_fmode = imf->im6f_st[1];
1729 * If the user specified a buffer, copy out the source filter
1730 * entries to userland gracefully.
1731 * We only copy out the number of entries which userland
1732 * has asked for, but we always tell userland how big the
1733 * buffer really needs to be.
1735 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
1736 msfr.msfr_nsrcs = in6_mcast_maxsocksrc;
1738 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1739 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1740 M_TEMP, M_NOWAIT | M_ZERO);
1748 * Count number of sources in-mode at t0.
1749 * If buffer space exists and remains, copy out source entries.
1751 nsrcs = msfr.msfr_nsrcs;
1754 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
1755 lims = (struct in6_msource *)ims;
1756 if (lims->im6sl_st[0] == MCAST_UNDEFINED ||
1757 lims->im6sl_st[0] != imf->im6f_st[0])
1760 if (tss != NULL && nsrcs > 0) {
1761 psin = (struct sockaddr_in6 *)ptss;
1762 psin->sin6_family = AF_INET6;
1763 psin->sin6_len = sizeof(struct sockaddr_in6);
1764 psin->sin6_addr = lims->im6s_addr;
1765 psin->sin6_port = 0;
1774 error = copyout(tss, msfr.msfr_srcs,
1775 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1781 msfr.msfr_nsrcs = ncsrcs;
1782 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1788 * Return the IP multicast options in response to user getsockopt().
1791 ip6_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1793 struct ip6_moptions *im6o;
1798 im6o = inp->in6p_moptions;
1799 /* If socket is neither of type SOCK_RAW or SOCK_DGRAM, reject it. */
1800 if (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1801 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM) {
1803 return (EOPNOTSUPP);
1807 switch (sopt->sopt_name) {
1808 case IPV6_MULTICAST_IF:
1809 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) {
1812 optval = im6o->im6o_multicast_ifp->if_index;
1815 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1818 case IPV6_MULTICAST_HOPS:
1820 optval = V_ip6_defmcasthlim;
1822 optval = im6o->im6o_multicast_hlim;
1824 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1827 case IPV6_MULTICAST_LOOP:
1829 optval = in6_mcast_loop; /* XXX VIMAGE */
1831 optval = im6o->im6o_multicast_loop;
1833 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1838 error = EADDRNOTAVAIL;
1841 error = in6p_get_source_filters(inp, sopt);
1847 error = ENOPROTOOPT;
1851 INP_UNLOCK_ASSERT(inp);
1857 * Look up the ifnet to use for a multicast group membership,
1858 * given the address of an IPv6 group.
1860 * This routine exists to support legacy IPv6 multicast applications.
1862 * Use the socket's current FIB number for any required FIB lookup. Look up the
1863 * group address in the unicast FIB, and use its ifp; usually, this points to
1864 * the default next-hop. If the FIB lookup fails, return NULL.
1866 * FUTURE: Support multiple forwarding tables for IPv6.
1868 * Returns NULL if no ifp could be found.
1870 static struct ifnet *
1871 in6p_lookup_mcast_ifp(const struct inpcb *inp, const struct sockaddr_in6 *gsin6)
1873 struct nhop_object *nh;
1874 struct in6_addr dst;
1878 KASSERT(gsin6->sin6_family == AF_INET6,
1879 ("%s: not AF_INET6 group", __func__));
1881 in6_splitscope(&gsin6->sin6_addr, &dst, &scopeid);
1882 fibnum = inp->inp_inc.inc_fibnum;
1883 nh = fib6_lookup(fibnum, &dst, scopeid, 0, 0);
1885 return (nh ? nh->nh_ifp : NULL);
1889 * Join an IPv6 multicast group, possibly with a source.
1891 * FIXME: The KAME use of the unspecified address (::)
1892 * to join *all* multicast groups is currently unsupported.
1894 * XXXGL: this function multiple times uses ifnet_byindex() without
1895 * proper protection - staying in epoch, or putting reference on ifnet.
1898 in6p_join_group(struct inpcb *inp, struct sockopt *sopt)
1900 struct in6_multi_head inmh;
1901 struct group_source_req gsr;
1902 struct epoch_tracker et;
1903 sockunion_t *gsa, *ssa;
1905 struct in6_mfilter *imf;
1906 struct ip6_moptions *imo;
1907 struct in6_multi *inm;
1908 struct in6_msource *lims;
1916 memset(&gsr, 0, sizeof(struct group_source_req));
1917 gsa = (sockunion_t *)&gsr.gsr_group;
1918 gsa->ss.ss_family = AF_UNSPEC;
1919 ssa = (sockunion_t *)&gsr.gsr_source;
1920 ssa->ss.ss_family = AF_UNSPEC;
1923 * Chew everything into struct group_source_req.
1924 * Overwrite the port field if present, as the sockaddr
1925 * being copied in may be matched with a binary comparison.
1926 * Ignore passed-in scope ID.
1928 switch (sopt->sopt_name) {
1929 case IPV6_JOIN_GROUP: {
1930 struct ipv6_mreq mreq;
1932 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
1933 sizeof(struct ipv6_mreq));
1937 gsa->sin6.sin6_family = AF_INET6;
1938 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
1939 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
1941 if (mreq.ipv6mr_interface == 0) {
1942 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
1944 NET_EPOCH_ENTER(et);
1945 ifp = ifnet_byindex(mreq.ipv6mr_interface);
1948 return (EADDRNOTAVAIL);
1950 CTR3(KTR_MLD, "%s: ipv6mr_interface = %d, ifp = %p",
1951 __func__, mreq.ipv6mr_interface, ifp);
1954 case MCAST_JOIN_GROUP:
1955 case MCAST_JOIN_SOURCE_GROUP:
1956 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1957 error = sooptcopyin(sopt, &gsr,
1958 sizeof(struct group_req),
1959 sizeof(struct group_req));
1960 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1961 error = sooptcopyin(sopt, &gsr,
1962 sizeof(struct group_source_req),
1963 sizeof(struct group_source_req));
1968 if (gsa->sin6.sin6_family != AF_INET6 ||
1969 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1972 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1973 if (ssa->sin6.sin6_family != AF_INET6 ||
1974 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1976 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
1979 * TODO: Validate embedded scope ID in source
1980 * list entry against passed-in ifp, if and only
1981 * if source list filter entry is iface or node local.
1983 in6_clearscope(&ssa->sin6.sin6_addr);
1984 ssa->sin6.sin6_port = 0;
1985 ssa->sin6.sin6_scope_id = 0;
1987 NET_EPOCH_ENTER(et);
1988 ifp = ifnet_byindex(gsr.gsr_interface);
1991 return (EADDRNOTAVAIL);
1995 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1996 __func__, sopt->sopt_name);
1997 return (EOPNOTSUPP);
2001 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2004 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
2005 return (EADDRNOTAVAIL);
2007 gsa->sin6.sin6_port = 0;
2008 gsa->sin6.sin6_scope_id = 0;
2011 * Always set the scope zone ID on memberships created from userland.
2012 * Use the passed-in ifp to do this.
2013 * XXX The in6_setscope() return value is meaningless.
2014 * XXX SCOPE6_LOCK() is taken by in6_setscope().
2016 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2021 * Find the membership in the membership list.
2023 imo = in6p_findmoptions(inp);
2024 imf = im6o_match_group(imo, ifp, &gsa->sa);
2029 if (ip6_mfilter_count(&imo->im6o_head) >= IPV6_MAX_MEMBERSHIPS) {
2031 goto out_in6p_locked;
2035 inm = imf->im6f_in6m;
2037 if (ssa->ss.ss_family != AF_UNSPEC) {
2039 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2040 * is an error. On an existing inclusive membership,
2041 * it just adds the source to the filter list.
2043 if (imf->im6f_st[1] != MCAST_INCLUDE) {
2045 goto out_in6p_locked;
2048 * Throw out duplicates.
2050 * XXX FIXME: This makes a naive assumption that
2051 * even if entries exist for *ssa in this imf,
2052 * they will be rejected as dupes, even if they
2053 * are not valid in the current mode (in-mode).
2055 * in6_msource is transactioned just as for anything
2056 * else in SSM -- but note naive use of in6m_graft()
2057 * below for allocating new filter entries.
2059 * This is only an issue if someone mixes the
2060 * full-state SSM API with the delta-based API,
2061 * which is discouraged in the relevant RFCs.
2063 lims = im6o_match_source(imf, &ssa->sa);
2064 if (lims != NULL /*&&
2065 lims->im6sl_st[1] == MCAST_INCLUDE*/) {
2066 error = EADDRNOTAVAIL;
2067 goto out_in6p_locked;
2071 * MCAST_JOIN_GROUP alone, on any existing membership,
2072 * is rejected, to stop the same inpcb tying up
2073 * multiple refs to the in_multi.
2074 * On an existing inclusive membership, this is also
2075 * an error; if you want to change filter mode,
2076 * you must use the userland API setsourcefilter().
2077 * XXX We don't reject this for imf in UNDEFINED
2078 * state at t1, because allocation of a filter
2079 * is atomic with allocation of a membership.
2082 goto out_in6p_locked;
2087 * Begin state merge transaction at socket layer.
2089 INP_WLOCK_ASSERT(inp);
2092 * Graft new source into filter list for this inpcb's
2093 * membership of the group. The in6_multi may not have
2094 * been allocated yet if this is a new membership, however,
2095 * the in_mfilter slot will be allocated and must be initialized.
2097 * Note: Grafting of exclusive mode filters doesn't happen
2099 * XXX: Should check for non-NULL lims (node exists but may
2100 * not be in-mode) for interop with full-state API.
2102 if (ssa->ss.ss_family != AF_UNSPEC) {
2103 /* Membership starts in IN mode */
2105 CTR1(KTR_MLD, "%s: new join w/source", __func__);
2106 imf = ip6_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_INCLUDE);
2109 goto out_in6p_locked;
2112 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
2114 lims = im6f_graft(imf, MCAST_INCLUDE, &ssa->sin6);
2116 CTR1(KTR_MLD, "%s: merge imf state failed",
2119 goto out_in6p_locked;
2122 /* No address specified; Membership starts in EX mode */
2124 CTR1(KTR_MLD, "%s: new join w/o source", __func__);
2125 imf = ip6_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_EXCLUDE);
2128 goto out_in6p_locked;
2134 * Begin state merge transaction at MLD layer.
2140 error = in6_joingroup_locked(ifp, &gsa->sin6.sin6_addr, imf,
2141 &imf->im6f_in6m, 0);
2144 if (in_pcbrele_wlocked(inp)) {
2146 goto out_in6p_unlocked;
2149 goto out_in6p_locked;
2152 * NOTE: Refcount from in6_joingroup_locked()
2153 * is protecting membership.
2155 ip6_mfilter_insert(&imo->im6o_head, imf);
2157 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2158 IN6_MULTI_LIST_LOCK();
2159 error = in6m_merge(inm, imf);
2161 CTR1(KTR_MLD, "%s: failed to merge inm state",
2163 IN6_MULTI_LIST_UNLOCK();
2166 goto out_in6p_locked;
2168 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2169 error = mld_change_state(inm, 0);
2170 IN6_MULTI_LIST_UNLOCK();
2173 CTR1(KTR_MLD, "%s: failed mld downcall",
2177 goto out_in6p_locked;
2189 if (is_new && imf) {
2190 if (imf->im6f_in6m != NULL) {
2191 struct in6_multi_head inmh;
2194 SLIST_INSERT_HEAD(&inmh, imf->im6f_in6m, in6m_defer);
2195 in6m_release_list_deferred(&inmh);
2197 ip6_mfilter_free(imf);
2203 * Leave an IPv6 multicast group on an inpcb, possibly with a source.
2206 in6p_leave_group(struct inpcb *inp, struct sockopt *sopt)
2208 struct ipv6_mreq mreq;
2209 struct group_source_req gsr;
2210 struct epoch_tracker et;
2211 sockunion_t *gsa, *ssa;
2213 struct in6_mfilter *imf;
2214 struct ip6_moptions *imo;
2215 struct in6_msource *ims;
2216 struct in6_multi *inm;
2221 char ip6tbuf[INET6_ADDRSTRLEN];
2229 memset(&gsr, 0, sizeof(struct group_source_req));
2230 gsa = (sockunion_t *)&gsr.gsr_group;
2231 gsa->ss.ss_family = AF_UNSPEC;
2232 ssa = (sockunion_t *)&gsr.gsr_source;
2233 ssa->ss.ss_family = AF_UNSPEC;
2236 * Chew everything passed in up into a struct group_source_req
2237 * as that is easier to process.
2238 * Note: Any embedded scope ID in the multicast group passed
2239 * in by userland is ignored, the interface index is the recommended
2240 * mechanism to specify an interface; see below.
2242 switch (sopt->sopt_name) {
2243 case IPV6_LEAVE_GROUP:
2244 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
2245 sizeof(struct ipv6_mreq));
2248 gsa->sin6.sin6_family = AF_INET6;
2249 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
2250 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
2251 gsa->sin6.sin6_port = 0;
2252 gsa->sin6.sin6_scope_id = 0;
2253 ifindex = mreq.ipv6mr_interface;
2256 case MCAST_LEAVE_GROUP:
2257 case MCAST_LEAVE_SOURCE_GROUP:
2258 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2259 error = sooptcopyin(sopt, &gsr,
2260 sizeof(struct group_req),
2261 sizeof(struct group_req));
2262 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2263 error = sooptcopyin(sopt, &gsr,
2264 sizeof(struct group_source_req),
2265 sizeof(struct group_source_req));
2270 if (gsa->sin6.sin6_family != AF_INET6 ||
2271 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2273 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2274 if (ssa->sin6.sin6_family != AF_INET6 ||
2275 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2277 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
2280 * TODO: Validate embedded scope ID in source
2281 * list entry against passed-in ifp, if and only
2282 * if source list filter entry is iface or node local.
2284 in6_clearscope(&ssa->sin6.sin6_addr);
2286 gsa->sin6.sin6_port = 0;
2287 gsa->sin6.sin6_scope_id = 0;
2288 ifindex = gsr.gsr_interface;
2292 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
2293 __func__, sopt->sopt_name);
2294 return (EOPNOTSUPP);
2298 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2302 * Validate interface index if provided. If no interface index
2303 * was provided separately, attempt to look the membership up
2304 * from the default scope as a last resort to disambiguate
2305 * the membership we are being asked to leave.
2306 * XXX SCOPE6 lock potentially taken here.
2309 NET_EPOCH_ENTER(et);
2310 ifp = ifnet_byindex(ifindex);
2311 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */
2313 return (EADDRNOTAVAIL);
2314 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2316 error = sa6_embedscope(&gsa->sin6, V_ip6_use_defzone);
2318 return (EADDRNOTAVAIL);
2320 * Some badly behaved applications don't pass an ifindex
2321 * or a scope ID, which is an API violation. In this case,
2322 * perform a lookup as per a v6 join.
2324 * XXX For now, stomp on zone ID for the corner case.
2325 * This is not the 'KAME way', but we need to see the ifp
2326 * directly until such time as this implementation is
2327 * refactored, assuming the scope IDs are the way to go.
2329 ifindex = ntohs(gsa->sin6.sin6_addr.s6_addr16[1]);
2331 CTR2(KTR_MLD, "%s: warning: no ifindex, looking up "
2332 "ifp for group %s.", __func__,
2333 ip6_sprintf(ip6tbuf, &gsa->sin6.sin6_addr));
2334 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
2336 NET_EPOCH_ENTER(et);
2337 ifp = ifnet_byindex(ifindex);
2338 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */
2341 return (EADDRNOTAVAIL);
2344 CTR2(KTR_MLD, "%s: ifp = %p", __func__, ifp);
2345 KASSERT(ifp != NULL, ("%s: ifp did not resolve", __func__));
2350 * Find the membership in the membership list.
2352 imo = in6p_findmoptions(inp);
2353 imf = im6o_match_group(imo, ifp, &gsa->sa);
2355 error = EADDRNOTAVAIL;
2356 goto out_in6p_locked;
2358 inm = imf->im6f_in6m;
2360 if (ssa->ss.ss_family != AF_UNSPEC)
2364 * Begin state merge transaction at socket layer.
2366 INP_WLOCK_ASSERT(inp);
2369 * If we were instructed only to leave a given source, do so.
2370 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2373 ip6_mfilter_remove(&imo->im6o_head, imf);
2377 * Give up the multicast address record to which
2378 * the membership points.
2380 (void)in6_leavegroup_locked(inm, imf);
2382 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
2383 error = EADDRNOTAVAIL;
2384 goto out_in6p_locked;
2386 ims = im6o_match_source(imf, &ssa->sa);
2388 CTR3(KTR_MLD, "%s: source %p %spresent", __func__,
2389 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
2391 error = EADDRNOTAVAIL;
2392 goto out_in6p_locked;
2394 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
2395 error = im6f_prune(imf, &ssa->sin6);
2397 CTR1(KTR_MLD, "%s: merge imf state failed",
2399 goto out_in6p_locked;
2404 * Begin state merge transaction at MLD layer.
2407 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2408 IN6_MULTI_LIST_LOCK();
2409 error = in6m_merge(inm, imf);
2411 CTR1(KTR_MLD, "%s: failed to merge inm state",
2413 IN6_MULTI_LIST_UNLOCK();
2416 goto out_in6p_locked;
2419 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2420 error = mld_change_state(inm, 0);
2421 IN6_MULTI_LIST_UNLOCK();
2423 CTR1(KTR_MLD, "%s: failed mld downcall",
2427 goto out_in6p_locked;
2437 if (is_final && imf)
2438 ip6_mfilter_free(imf);
2445 * Select the interface for transmitting IPv6 multicast datagrams.
2447 * Either an instance of struct in6_addr or an instance of struct ipv6_mreqn
2448 * may be passed to this socket option. An address of in6addr_any or an
2449 * interface index of 0 is used to remove a previous selection.
2450 * When no interface is selected, one is chosen for every send.
2453 in6p_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2455 struct epoch_tracker et;
2457 struct ip6_moptions *imo;
2461 if (sopt->sopt_valsize != sizeof(u_int))
2464 error = sooptcopyin(sopt, &ifindex, sizeof(u_int), sizeof(u_int));
2467 NET_EPOCH_ENTER(et);
2471 ifp = ifnet_byindex(ifindex);
2472 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2474 return (EADDRNOTAVAIL);
2477 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */
2478 imo = in6p_findmoptions(inp);
2479 imo->im6o_multicast_ifp = ifp;
2486 * Atomically set source filters on a socket for an IPv6 multicast group.
2488 * XXXGL: unsafely exits epoch with ifnet pointer
2491 in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2493 struct __msfilterreq msfr;
2494 struct epoch_tracker et;
2497 struct in6_mfilter *imf;
2498 struct ip6_moptions *imo;
2499 struct in6_multi *inm;
2502 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2503 sizeof(struct __msfilterreq));
2507 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
2510 if (msfr.msfr_fmode != MCAST_EXCLUDE &&
2511 msfr.msfr_fmode != MCAST_INCLUDE)
2514 if (msfr.msfr_group.ss_family != AF_INET6 ||
2515 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
2518 gsa = (sockunion_t *)&msfr.msfr_group;
2519 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2522 gsa->sin6.sin6_port = 0; /* ignore port */
2524 NET_EPOCH_ENTER(et);
2525 ifp = ifnet_byindex(msfr.msfr_ifindex);
2528 return (EADDRNOTAVAIL);
2529 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2532 * Take the INP write lock.
2533 * Check if this socket is a member of this group.
2535 imo = in6p_findmoptions(inp);
2536 imf = im6o_match_group(imo, ifp, &gsa->sa);
2538 error = EADDRNOTAVAIL;
2539 goto out_in6p_locked;
2541 inm = imf->im6f_in6m;
2544 * Begin state merge transaction at socket layer.
2546 INP_WLOCK_ASSERT(inp);
2548 imf->im6f_st[1] = msfr.msfr_fmode;
2551 * Apply any new source filters, if present.
2552 * Make a copy of the user-space source vector so
2553 * that we may copy them with a single copyin. This
2554 * allows us to deal with page faults up-front.
2556 if (msfr.msfr_nsrcs > 0) {
2557 struct in6_msource *lims;
2558 struct sockaddr_in6 *psin;
2559 struct sockaddr_storage *kss, *pkss;
2564 CTR2(KTR_MLD, "%s: loading %lu source list entries",
2565 __func__, (unsigned long)msfr.msfr_nsrcs);
2566 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2568 error = copyin(msfr.msfr_srcs, kss,
2569 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2578 * Mark all source filters as UNDEFINED at t1.
2579 * Restore new group filter mode, as im6f_leave()
2580 * will set it to INCLUDE.
2583 imf->im6f_st[1] = msfr.msfr_fmode;
2586 * Update socket layer filters at t1, lazy-allocating
2587 * new entries. This saves a bunch of memory at the
2588 * cost of one RB_FIND() per source entry; duplicate
2589 * entries in the msfr_nsrcs vector are ignored.
2590 * If we encounter an error, rollback transaction.
2592 * XXX This too could be replaced with a set-symmetric
2593 * difference like loop to avoid walking from root
2594 * every time, as the key space is common.
2596 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2597 psin = (struct sockaddr_in6 *)pkss;
2598 if (psin->sin6_family != AF_INET6) {
2599 error = EAFNOSUPPORT;
2602 if (psin->sin6_len != sizeof(struct sockaddr_in6)) {
2606 if (IN6_IS_ADDR_MULTICAST(&psin->sin6_addr)) {
2611 * TODO: Validate embedded scope ID in source
2612 * list entry against passed-in ifp, if and only
2613 * if source list filter entry is iface or node local.
2615 in6_clearscope(&psin->sin6_addr);
2616 error = im6f_get_source(imf, psin, &lims);
2619 lims->im6sl_st[1] = imf->im6f_st[1];
2625 goto out_im6f_rollback;
2627 INP_WLOCK_ASSERT(inp);
2628 IN6_MULTI_LIST_LOCK();
2631 * Begin state merge transaction at MLD layer.
2633 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2634 error = in6m_merge(inm, imf);
2636 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
2638 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2639 error = mld_change_state(inm, 0);
2641 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
2644 IN6_MULTI_LIST_UNLOCK();
2660 * Set the IP multicast options in response to user setsockopt().
2662 * Many of the socket options handled in this function duplicate the
2663 * functionality of socket options in the regular unicast API. However,
2664 * it is not possible to merge the duplicate code, because the idempotence
2665 * of the IPv6 multicast part of the BSD Sockets API must be preserved;
2666 * the effects of these options must be treated as separate and distinct.
2668 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2671 ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2673 struct ip6_moptions *im6o;
2678 /* If socket is neither of type SOCK_RAW or SOCK_DGRAM, reject it. */
2679 if (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2680 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)
2681 return (EOPNOTSUPP);
2683 switch (sopt->sopt_name) {
2684 case IPV6_MULTICAST_IF:
2685 error = in6p_set_multicast_if(inp, sopt);
2688 case IPV6_MULTICAST_HOPS: {
2691 if (sopt->sopt_valsize != sizeof(int)) {
2695 error = sooptcopyin(sopt, &hlim, sizeof(hlim), sizeof(int));
2698 if (hlim < -1 || hlim > 255) {
2701 } else if (hlim == -1) {
2702 hlim = V_ip6_defmcasthlim;
2704 im6o = in6p_findmoptions(inp);
2705 im6o->im6o_multicast_hlim = hlim;
2710 case IPV6_MULTICAST_LOOP: {
2714 * Set the loopback flag for outgoing multicast packets.
2715 * Must be zero or one.
2717 if (sopt->sopt_valsize != sizeof(u_int)) {
2721 error = sooptcopyin(sopt, &loop, sizeof(u_int), sizeof(u_int));
2728 im6o = in6p_findmoptions(inp);
2729 im6o->im6o_multicast_loop = loop;
2734 case IPV6_JOIN_GROUP:
2735 case MCAST_JOIN_GROUP:
2736 case MCAST_JOIN_SOURCE_GROUP:
2737 error = in6p_join_group(inp, sopt);
2740 case IPV6_LEAVE_GROUP:
2741 case MCAST_LEAVE_GROUP:
2742 case MCAST_LEAVE_SOURCE_GROUP:
2743 error = in6p_leave_group(inp, sopt);
2746 case MCAST_BLOCK_SOURCE:
2747 case MCAST_UNBLOCK_SOURCE:
2748 error = in6p_block_unblock_source(inp, sopt);
2752 error = in6p_set_source_filters(inp, sopt);
2760 INP_UNLOCK_ASSERT(inp);
2766 * Expose MLD's multicast filter mode and source list(s) to userland,
2767 * keyed by (ifindex, group).
2768 * The filter mode is written out as a uint32_t, followed by
2769 * 0..n of struct in6_addr.
2770 * For use by ifmcstat(8).
2771 * SMPng: NOTE: unlocked read of ifindex space.
2774 sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS)
2776 struct in6_addr mcaddr;
2777 struct in6_addr src;
2778 struct epoch_tracker et;
2780 struct ifmultiaddr *ifma;
2781 struct in6_multi *inm;
2782 struct ip6_msource *ims;
2786 uint32_t fmode, ifindex;
2788 char ip6tbuf[INET6_ADDRSTRLEN];
2794 if (req->newptr != NULL)
2797 /* int: ifindex + 4 * 32 bits of IPv6 address */
2801 memcpy(&mcaddr, &name[1], sizeof(struct in6_addr));
2802 if (!IN6_IS_ADDR_MULTICAST(&mcaddr)) {
2803 CTR2(KTR_MLD, "%s: group %s is not multicast",
2804 __func__, ip6_sprintf(ip6tbuf, &mcaddr));
2809 NET_EPOCH_ENTER(et);
2810 ifp = ifnet_byindex(ifindex);
2813 CTR2(KTR_MLD, "%s: no ifp for ifindex %u",
2818 * Internal MLD lookups require that scope/zone ID is set.
2820 (void)in6_setscope(&mcaddr, ifp, NULL);
2822 retval = sysctl_wire_old_buffer(req,
2823 sizeof(uint32_t) + (in6_mcast_maxgrpsrc * sizeof(struct in6_addr)));
2830 IN6_MULTI_LIST_LOCK();
2831 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2832 inm = in6m_ifmultiaddr_get_inm(ifma);
2835 if (!IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, &mcaddr))
2837 fmode = inm->in6m_st[1].iss_fmode;
2838 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2841 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
2842 CTR2(KTR_MLD, "%s: visit node %p", __func__, ims);
2844 * Only copy-out sources which are in-mode.
2846 if (fmode != im6s_get_mode(inm, ims, 1)) {
2847 CTR1(KTR_MLD, "%s: skip non-in-mode",
2851 src = ims->im6s_addr;
2852 retval = SYSCTL_OUT(req, &src,
2853 sizeof(struct in6_addr));
2858 IN6_MULTI_LIST_UNLOCK();
2867 static const char *in6m_modestrs[] = { "un", "in", "ex" };
2870 in6m_mode_str(const int mode)
2873 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2874 return (in6m_modestrs[mode]);
2878 static const char *in6m_statestrs[] = {
2890 _Static_assert(nitems(in6m_statestrs) ==
2891 MLD_LEAVING_MEMBER - MLD_NOT_MEMBER + 1, "Missing MLD group state");
2894 in6m_state_str(const int state)
2897 if (state >= MLD_NOT_MEMBER && state <= MLD_LEAVING_MEMBER)
2898 return (in6m_statestrs[state]);
2903 * Dump an in6_multi structure to the console.
2906 in6m_print(const struct in6_multi *inm)
2909 char ip6tbuf[INET6_ADDRSTRLEN];
2911 if ((ktr_mask & KTR_MLD) == 0)
2914 printf("%s: --- begin in6m %p ---\n", __func__, inm);
2915 printf("addr %s ifp %p(%s) ifma %p\n",
2916 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2918 if_name(inm->in6m_ifp),
2920 printf("timer %u state %s refcount %u scq.len %u\n",
2922 in6m_state_str(inm->in6m_state),
2924 mbufq_len(&inm->in6m_scq));
2925 printf("mli %p nsrc %lu sctimer %u scrv %u\n",
2930 for (t = 0; t < 2; t++) {
2931 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
2932 in6m_mode_str(inm->in6m_st[t].iss_fmode),
2933 inm->in6m_st[t].iss_asm,
2934 inm->in6m_st[t].iss_ex,
2935 inm->in6m_st[t].iss_in,
2936 inm->in6m_st[t].iss_rec);
2938 printf("%s: --- end in6m %p ---\n", __func__, inm);
2944 in6m_print(const struct in6_multi *inm)