2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2009 Bruce Simpson.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * IPv6 multicast socket, group, and socket option processing module.
34 * Normative references: RFC 2292, RFC 3492, RFC 3542, RFC 3678, RFC 3810.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_inet6.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
53 #include <sys/taskqueue.h>
57 #include <net/if_var.h>
58 #include <net/if_dl.h>
59 #include <net/if_private.h>
60 #include <net/route.h>
61 #include <net/route/nhop.h>
64 #include <netinet/in.h>
65 #include <netinet/udp.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/udp_var.h>
69 #include <netinet6/in6_fib.h>
70 #include <netinet6/in6_var.h>
71 #include <netinet/ip6.h>
72 #include <netinet/icmp6.h>
73 #include <netinet6/ip6_var.h>
74 #include <netinet/in_pcb.h>
75 #include <netinet/tcp_var.h>
76 #include <netinet6/nd6.h>
77 #include <netinet6/mld6_var.h>
78 #include <netinet6/scope6_var.h>
81 #define KTR_MLD KTR_INET6
84 #ifndef __SOCKUNION_DECLARED
86 struct sockaddr_storage ss;
88 struct sockaddr_dl sdl;
89 struct sockaddr_in6 sin6;
91 typedef union sockunion sockunion_t;
92 #define __SOCKUNION_DECLARED
93 #endif /* __SOCKUNION_DECLARED */
95 static MALLOC_DEFINE(M_IN6MFILTER, "in6_mfilter",
96 "IPv6 multicast PCB-layer source filter");
97 MALLOC_DEFINE(M_IP6MADDR, "in6_multi", "IPv6 multicast group");
98 static MALLOC_DEFINE(M_IP6MOPTS, "ip6_moptions", "IPv6 multicast options");
99 static MALLOC_DEFINE(M_IP6MSOURCE, "ip6_msource",
100 "IPv6 multicast MLD-layer source filter");
102 RB_GENERATE(ip6_msource_tree, ip6_msource, im6s_link, ip6_msource_cmp);
106 * - Lock order is: Giant, IN6_MULTI_LOCK, INP_WLOCK,
107 * IN6_MULTI_LIST_LOCK, MLD_LOCK, IF_ADDR_LOCK.
108 * - The IF_ADDR_LOCK is implicitly taken by in6m_lookup() earlier, however
109 * it can be taken by code in net/if.c also.
110 * - ip6_moptions and in6_mfilter are covered by the INP_WLOCK.
112 * struct in6_multi is covered by IN6_MULTI_LOCK. There isn't strictly
113 * any need for in6_multi itself to be virtualized -- it is bound to an ifp
114 * anyway no matter what happens.
116 struct mtx in6_multi_list_mtx;
117 MTX_SYSINIT(in6_multi_mtx, &in6_multi_list_mtx, "in6_multi_list_mtx", MTX_DEF);
119 struct mtx in6_multi_free_mtx;
120 MTX_SYSINIT(in6_multi_free_mtx, &in6_multi_free_mtx, "in6_multi_free_mtx", MTX_DEF);
122 struct sx in6_multi_sx;
123 SX_SYSINIT(in6_multi_sx, &in6_multi_sx, "in6_multi_sx");
125 static void im6f_commit(struct in6_mfilter *);
126 static int im6f_get_source(struct in6_mfilter *imf,
127 const struct sockaddr_in6 *psin,
128 struct in6_msource **);
129 static struct in6_msource *
130 im6f_graft(struct in6_mfilter *, const uint8_t,
131 const struct sockaddr_in6 *);
132 static void im6f_leave(struct in6_mfilter *);
133 static int im6f_prune(struct in6_mfilter *, const struct sockaddr_in6 *);
134 static void im6f_purge(struct in6_mfilter *);
135 static void im6f_rollback(struct in6_mfilter *);
136 static void im6f_reap(struct in6_mfilter *);
137 static struct in6_mfilter *
138 im6o_match_group(const struct ip6_moptions *,
139 const struct ifnet *, const struct sockaddr *);
140 static struct in6_msource *
141 im6o_match_source(struct in6_mfilter *, const struct sockaddr *);
142 static void im6s_merge(struct ip6_msource *ims,
143 const struct in6_msource *lims, const int rollback);
144 static int in6_getmulti(struct ifnet *, const struct in6_addr *,
145 struct in6_multi **);
146 static int in6_joingroup_locked(struct ifnet *, const struct in6_addr *,
147 struct in6_mfilter *, struct in6_multi **, int);
148 static int in6m_get_source(struct in6_multi *inm,
149 const struct in6_addr *addr, const int noalloc,
150 struct ip6_msource **pims);
152 static int in6m_is_ifp_detached(const struct in6_multi *);
154 static int in6m_merge(struct in6_multi *, /*const*/ struct in6_mfilter *);
155 static void in6m_purge(struct in6_multi *);
156 static void in6m_reap(struct in6_multi *);
157 static struct ip6_moptions *
158 in6p_findmoptions(struct inpcb *);
159 static int in6p_get_source_filters(struct inpcb *, struct sockopt *);
160 static int in6p_join_group(struct inpcb *, struct sockopt *);
161 static int in6p_leave_group(struct inpcb *, struct sockopt *);
162 static struct ifnet *
163 in6p_lookup_mcast_ifp(const struct inpcb *,
164 const struct sockaddr_in6 *);
165 static int in6p_block_unblock_source(struct inpcb *, struct sockopt *);
166 static int in6p_set_multicast_if(struct inpcb *, struct sockopt *);
167 static int in6p_set_source_filters(struct inpcb *, struct sockopt *);
168 static int sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS);
170 SYSCTL_DECL(_net_inet6_ip6); /* XXX Not in any common header. */
172 static SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, mcast,
173 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
176 static u_long in6_mcast_maxgrpsrc = IPV6_MAX_GROUP_SRC_FILTER;
177 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxgrpsrc,
178 CTLFLAG_RWTUN, &in6_mcast_maxgrpsrc, 0,
179 "Max source filters per group");
181 static u_long in6_mcast_maxsocksrc = IPV6_MAX_SOCK_SRC_FILTER;
182 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxsocksrc,
183 CTLFLAG_RWTUN, &in6_mcast_maxsocksrc, 0,
184 "Max source filters per socket");
186 /* TODO Virtualize this switch. */
187 int in6_mcast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
188 SYSCTL_INT(_net_inet6_ip6_mcast, OID_AUTO, loop, CTLFLAG_RWTUN,
189 &in6_mcast_loop, 0, "Loopback multicast datagrams by default");
191 static SYSCTL_NODE(_net_inet6_ip6_mcast, OID_AUTO, filters,
192 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip6_mcast_filters,
193 "Per-interface stack-wide source filters");
197 * Inline function which wraps assertions for a valid ifp.
198 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
202 in6m_is_ifp_detached(const struct in6_multi *inm)
206 KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__));
207 ifp = inm->in6m_ifma->ifma_ifp;
210 * Sanity check that network-layer notion of ifp is the
211 * same as that of link-layer.
213 KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__));
216 return (ifp == NULL);
221 * Initialize an in6_mfilter structure to a known state at t0, t1
222 * with an empty source filter list.
225 im6f_init(struct in6_mfilter *imf, const int st0, const int st1)
227 memset(imf, 0, sizeof(struct in6_mfilter));
228 RB_INIT(&imf->im6f_sources);
229 imf->im6f_st[0] = st0;
230 imf->im6f_st[1] = st1;
234 ip6_mfilter_alloc(const int mflags, const int st0, const int st1)
236 struct in6_mfilter *imf;
238 imf = malloc(sizeof(*imf), M_IN6MFILTER, mflags);
241 im6f_init(imf, st0, st1);
247 ip6_mfilter_free(struct in6_mfilter *imf)
251 free(imf, M_IN6MFILTER);
255 * Find an IPv6 multicast group entry for this ip6_moptions instance
256 * which matches the specified group, and optionally an interface.
257 * Return its index into the array, or -1 if not found.
259 static struct in6_mfilter *
260 im6o_match_group(const struct ip6_moptions *imo, const struct ifnet *ifp,
261 const struct sockaddr *group)
263 const struct sockaddr_in6 *gsin6;
264 struct in6_mfilter *imf;
265 struct in6_multi *inm;
267 gsin6 = (const struct sockaddr_in6 *)group;
269 IP6_MFILTER_FOREACH(imf, &imo->im6o_head) {
270 inm = imf->im6f_in6m;
273 if ((ifp == NULL || (inm->in6m_ifp == ifp)) &&
274 IN6_ARE_ADDR_EQUAL(&inm->in6m_addr,
275 &gsin6->sin6_addr)) {
283 * Find an IPv6 multicast source entry for this imo which matches
284 * the given group index for this socket, and source address.
286 * XXX TODO: The scope ID, if present in src, is stripped before
287 * any comparison. We SHOULD enforce scope/zone checks where the source
288 * filter entry has a link scope.
290 * NOTE: This does not check if the entry is in-mode, merely if
291 * it exists, which may not be the desired behaviour.
293 static struct in6_msource *
294 im6o_match_source(struct in6_mfilter *imf, const struct sockaddr *src)
296 struct ip6_msource find;
297 struct ip6_msource *ims;
298 const sockunion_t *psa;
300 KASSERT(src->sa_family == AF_INET6, ("%s: !AF_INET6", __func__));
302 psa = (const sockunion_t *)src;
303 find.im6s_addr = psa->sin6.sin6_addr;
304 in6_clearscope(&find.im6s_addr); /* XXX */
305 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
307 return ((struct in6_msource *)ims);
311 * Perform filtering for multicast datagrams on a socket by group and source.
313 * Returns 0 if a datagram should be allowed through, or various error codes
314 * if the socket was not a member of the group, or the source was muted, etc.
317 im6o_mc_filter(const struct ip6_moptions *imo, const struct ifnet *ifp,
318 const struct sockaddr *group, const struct sockaddr *src)
320 struct in6_mfilter *imf;
321 struct in6_msource *ims;
324 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
326 imf = im6o_match_group(imo, ifp, group);
328 return (MCAST_NOTGMEMBER);
331 * Check if the source was included in an (S,G) join.
332 * Allow reception on exclusive memberships by default,
333 * reject reception on inclusive memberships by default.
334 * Exclude source only if an in-mode exclude filter exists.
335 * Include source only if an in-mode include filter exists.
336 * NOTE: We are comparing group state here at MLD t1 (now)
337 * with socket-layer t0 (since last downcall).
339 mode = imf->im6f_st[1];
340 ims = im6o_match_source(imf, src);
342 if ((ims == NULL && mode == MCAST_INCLUDE) ||
343 (ims != NULL && ims->im6sl_st[0] != mode))
344 return (MCAST_NOTSMEMBER);
350 * Look up an in6_multi record for an IPv6 multicast address
351 * on the interface ifp.
352 * If no record found, return NULL.
354 * SMPng: The IN6_MULTI_LOCK and must be held and must be in network epoch.
357 in6m_lookup_locked(struct ifnet *ifp, const struct in6_addr *mcaddr)
359 struct ifmultiaddr *ifma;
360 struct in6_multi *inm;
364 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
365 inm = in6m_ifmultiaddr_get_inm(ifma);
368 if (IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, mcaddr))
375 * Find and return a reference to an in6_multi record for (ifp, group),
376 * and bump its reference count.
377 * If one does not exist, try to allocate it, and update link-layer multicast
378 * filters on ifp to listen for group.
379 * Assumes the IN6_MULTI lock is held across the call.
380 * Return 0 if successful, otherwise return an appropriate error code.
383 in6_getmulti(struct ifnet *ifp, const struct in6_addr *group,
384 struct in6_multi **pinm)
386 struct epoch_tracker et;
387 struct sockaddr_in6 gsin6;
388 struct ifmultiaddr *ifma;
389 struct in6_multi *inm;
395 * XXX: Accesses to ifma_protospec must be covered by IF_ADDR_LOCK;
396 * if_addmulti() takes this mutex itself, so we must drop and
397 * re-acquire around the call.
399 IN6_MULTI_LOCK_ASSERT();
400 IN6_MULTI_LIST_LOCK();
404 * Does ifp support IPv6 multicasts?
406 if (ifp->if_afdata[AF_INET6] == NULL)
409 inm = in6m_lookup_locked(ifp, group);
417 * If we already joined this group, just bump the
418 * refcount and return it.
420 KASSERT(inm->in6m_refcount >= 1,
421 ("%s: bad refcount %d", __func__, inm->in6m_refcount));
422 in6m_acquire_locked(inm);
427 memset(&gsin6, 0, sizeof(gsin6));
428 gsin6.sin6_family = AF_INET6;
429 gsin6.sin6_len = sizeof(struct sockaddr_in6);
430 gsin6.sin6_addr = *group;
433 * Check if a link-layer group is already associated
434 * with this network-layer group on the given ifnet.
436 IN6_MULTI_LIST_UNLOCK();
437 IF_ADDR_WUNLOCK(ifp);
438 error = if_addmulti(ifp, (struct sockaddr *)&gsin6, &ifma);
441 IN6_MULTI_LIST_LOCK();
445 * If something other than netinet6 is occupying the link-layer
446 * group, print a meaningful error message and back out of
448 * Otherwise, bump the refcount on the existing network-layer
449 * group association and return it.
451 if (ifma->ifma_protospec != NULL) {
452 inm = (struct in6_multi *)ifma->ifma_protospec;
454 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
456 KASSERT(ifma->ifma_addr->sa_family == AF_INET6,
457 ("%s: ifma not AF_INET6", __func__));
458 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
459 if (inm->in6m_ifma != ifma || inm->in6m_ifp != ifp ||
460 !IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, group))
461 panic("%s: ifma %p is inconsistent with %p (%p)",
462 __func__, ifma, inm, group);
464 in6m_acquire_locked(inm);
469 IF_ADDR_WLOCK_ASSERT(ifp);
472 * A new in6_multi record is needed; allocate and initialize it.
473 * We DO NOT perform an MLD join as the in6_ layer may need to
474 * push an initial source list down to MLD to support SSM.
476 * The initial source filter state is INCLUDE, {} as per the RFC.
477 * Pending state-changes per group are subject to a bounds check.
479 inm = malloc(sizeof(*inm), M_IP6MADDR, M_NOWAIT | M_ZERO);
481 IN6_MULTI_LIST_UNLOCK();
482 IF_ADDR_WUNLOCK(ifp);
483 if_delmulti_ifma(ifma);
486 inm->in6m_addr = *group;
488 inm->in6m_mli = MLD_IFINFO(ifp);
489 inm->in6m_ifma = ifma;
490 inm->in6m_refcount = 1;
491 inm->in6m_state = MLD_NOT_MEMBER;
492 mbufq_init(&inm->in6m_scq, MLD_MAX_STATE_CHANGES);
494 inm->in6m_st[0].iss_fmode = MCAST_UNDEFINED;
495 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
496 RB_INIT(&inm->in6m_srcs);
498 ifma->ifma_protospec = inm;
502 IN6_MULTI_LIST_UNLOCK();
503 IF_ADDR_WUNLOCK(ifp);
508 * Drop a reference to an in6_multi record.
510 * If the refcount drops to 0, free the in6_multi record and
511 * delete the underlying link-layer membership.
514 in6m_release(struct in6_multi *inm)
516 struct ifmultiaddr *ifma;
519 CTR2(KTR_MLD, "%s: refcount is %d", __func__, inm->in6m_refcount);
521 MPASS(inm->in6m_refcount == 0);
522 CTR2(KTR_MLD, "%s: freeing inm %p", __func__, inm);
524 ifma = inm->in6m_ifma;
526 MPASS(ifma->ifma_llifma == NULL);
528 /* XXX this access is not covered by IF_ADDR_LOCK */
529 CTR2(KTR_MLD, "%s: purging ifma %p", __func__, ifma);
530 KASSERT(ifma->ifma_protospec == NULL,
531 ("%s: ifma_protospec != NULL", __func__));
533 ifp = ifma->ifma_ifp;
536 CURVNET_SET(ifp->if_vnet);
538 free(inm, M_IP6MADDR);
539 if_delmulti_ifma_flags(ifma, 1);
544 free(inm, M_IP6MADDR);
545 if_delmulti_ifma_flags(ifma, 1);
550 * Interface detach can happen in a taskqueue thread context, so we must use a
551 * dedicated thread to avoid deadlocks when draining in6m_release tasks.
553 TASKQUEUE_DEFINE_THREAD(in6m_free);
554 static struct in6_multi_head in6m_free_list = SLIST_HEAD_INITIALIZER();
555 static void in6m_release_task(void *arg __unused, int pending __unused);
556 static struct task in6m_free_task = TASK_INITIALIZER(0, in6m_release_task, NULL);
559 in6m_release_list_deferred(struct in6_multi_head *inmh)
561 if (SLIST_EMPTY(inmh))
563 mtx_lock(&in6_multi_free_mtx);
564 SLIST_CONCAT(&in6m_free_list, inmh, in6_multi, in6m_nrele);
565 mtx_unlock(&in6_multi_free_mtx);
566 taskqueue_enqueue(taskqueue_in6m_free, &in6m_free_task);
570 in6m_release_wait(void *arg __unused)
574 * Make sure all pending multicast addresses are freed before
575 * the VNET or network device is destroyed:
577 taskqueue_drain_all(taskqueue_in6m_free);
580 /* XXX-BZ FIXME, see D24914. */
581 VNET_SYSUNINIT(in6m_release_wait, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, in6m_release_wait, NULL);
585 in6m_disconnect_locked(struct in6_multi_head *inmh, struct in6_multi *inm)
589 struct in6_ifaddr *ifa6;
590 struct in6_multi_mship *imm, *imm_tmp;
591 struct ifmultiaddr *ifma, *ll_ifma;
593 IN6_MULTI_LIST_LOCK_ASSERT();
597 return; /* already called */
599 inm->in6m_ifp = NULL;
600 IF_ADDR_WLOCK_ASSERT(ifp);
601 ifma = inm->in6m_ifma;
606 if (ifma->ifma_flags & IFMA_F_ENQUEUED) {
607 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
608 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
610 MCDPRINTF("removed ifma: %p from %s\n", ifma, ifp->if_xname);
611 if ((ll_ifma = ifma->ifma_llifma) != NULL) {
612 MPASS(ifma != ll_ifma);
613 ifma->ifma_llifma = NULL;
614 MPASS(ll_ifma->ifma_llifma == NULL);
615 MPASS(ll_ifma->ifma_ifp == ifp);
616 if (--ll_ifma->ifma_refcount == 0) {
617 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
618 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, ifma_link);
619 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
621 MCDPRINTF("removed ll_ifma: %p from %s\n", ll_ifma, ifp->if_xname);
622 if_freemulti(ll_ifma);
625 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
626 if (ifa->ifa_addr->sa_family != AF_INET6)
629 LIST_FOREACH_SAFE(imm, &ifa6->ia6_memberships,
630 i6mm_chain, imm_tmp) {
631 if (inm == imm->i6mm_maddr) {
632 LIST_REMOVE(imm, i6mm_chain);
633 free(imm, M_IP6MADDR);
634 in6m_rele_locked(inmh, inm);
641 in6m_release_task(void *arg __unused, int pending __unused)
643 struct in6_multi_head in6m_free_tmp;
644 struct in6_multi *inm, *tinm;
646 SLIST_INIT(&in6m_free_tmp);
647 mtx_lock(&in6_multi_free_mtx);
648 SLIST_CONCAT(&in6m_free_tmp, &in6m_free_list, in6_multi, in6m_nrele);
649 mtx_unlock(&in6_multi_free_mtx);
651 SLIST_FOREACH_SAFE(inm, &in6m_free_tmp, in6m_nrele, tinm) {
652 SLIST_REMOVE_HEAD(&in6m_free_tmp, in6m_nrele);
659 * Clear recorded source entries for a group.
660 * Used by the MLD code. Caller must hold the IN6_MULTI lock.
661 * FIXME: Should reap.
664 in6m_clear_recorded(struct in6_multi *inm)
666 struct ip6_msource *ims;
668 IN6_MULTI_LIST_LOCK_ASSERT();
670 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
673 --inm->in6m_st[1].iss_rec;
676 KASSERT(inm->in6m_st[1].iss_rec == 0,
677 ("%s: iss_rec %d not 0", __func__, inm->in6m_st[1].iss_rec));
681 * Record a source as pending for a Source-Group MLDv2 query.
682 * This lives here as it modifies the shared tree.
684 * inm is the group descriptor.
685 * naddr is the address of the source to record in network-byte order.
687 * If the net.inet6.mld.sgalloc sysctl is non-zero, we will
688 * lazy-allocate a source node in response to an SG query.
689 * Otherwise, no allocation is performed. This saves some memory
690 * with the trade-off that the source will not be reported to the
691 * router if joined in the window between the query response and
692 * the group actually being joined on the local host.
694 * VIMAGE: XXX: Currently the mld_sgalloc feature has been removed.
695 * This turns off the allocation of a recorded source entry if
696 * the group has not been joined.
698 * Return 0 if the source didn't exist or was already marked as recorded.
699 * Return 1 if the source was marked as recorded by this function.
700 * Return <0 if any error occurred (negated errno code).
703 in6m_record_source(struct in6_multi *inm, const struct in6_addr *addr)
705 struct ip6_msource find;
706 struct ip6_msource *ims, *nims;
708 IN6_MULTI_LIST_LOCK_ASSERT();
710 find.im6s_addr = *addr;
711 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
712 if (ims && ims->im6s_stp)
715 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
717 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
721 nims->im6s_addr = find.im6s_addr;
722 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
728 * Mark the source as recorded and update the recorded
732 ++inm->in6m_st[1].iss_rec;
738 * Return a pointer to an in6_msource owned by an in6_mfilter,
739 * given its source address.
740 * Lazy-allocate if needed. If this is a new entry its filter state is
743 * imf is the filter set being modified.
744 * addr is the source address.
746 * SMPng: May be called with locks held; malloc must not block.
749 im6f_get_source(struct in6_mfilter *imf, const struct sockaddr_in6 *psin,
750 struct in6_msource **plims)
752 struct ip6_msource find;
753 struct ip6_msource *ims, *nims;
754 struct in6_msource *lims;
761 find.im6s_addr = psin->sin6_addr;
762 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
763 lims = (struct in6_msource *)ims;
765 if (imf->im6f_nsrc == in6_mcast_maxsocksrc)
767 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
771 lims = (struct in6_msource *)nims;
772 lims->im6s_addr = find.im6s_addr;
773 lims->im6sl_st[0] = MCAST_UNDEFINED;
774 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
784 * Graft a source entry into an existing socket-layer filter set,
785 * maintaining any required invariants and checking allocations.
787 * The source is marked as being in the new filter mode at t1.
789 * Return the pointer to the new node, otherwise return NULL.
791 static struct in6_msource *
792 im6f_graft(struct in6_mfilter *imf, const uint8_t st1,
793 const struct sockaddr_in6 *psin)
795 struct ip6_msource *nims;
796 struct in6_msource *lims;
798 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
802 lims = (struct in6_msource *)nims;
803 lims->im6s_addr = psin->sin6_addr;
804 lims->im6sl_st[0] = MCAST_UNDEFINED;
805 lims->im6sl_st[1] = st1;
806 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
813 * Prune a source entry from an existing socket-layer filter set,
814 * maintaining any required invariants and checking allocations.
816 * The source is marked as being left at t1, it is not freed.
818 * Return 0 if no error occurred, otherwise return an errno value.
821 im6f_prune(struct in6_mfilter *imf, const struct sockaddr_in6 *psin)
823 struct ip6_msource find;
824 struct ip6_msource *ims;
825 struct in6_msource *lims;
827 find.im6s_addr = psin->sin6_addr;
828 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
831 lims = (struct in6_msource *)ims;
832 lims->im6sl_st[1] = MCAST_UNDEFINED;
837 * Revert socket-layer filter set deltas at t1 to t0 state.
840 im6f_rollback(struct in6_mfilter *imf)
842 struct ip6_msource *ims, *tims;
843 struct in6_msource *lims;
845 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
846 lims = (struct in6_msource *)ims;
847 if (lims->im6sl_st[0] == lims->im6sl_st[1]) {
848 /* no change at t1 */
850 } else if (lims->im6sl_st[0] != MCAST_UNDEFINED) {
851 /* revert change to existing source at t1 */
852 lims->im6sl_st[1] = lims->im6sl_st[0];
854 /* revert source added t1 */
855 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
856 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
857 free(ims, M_IN6MFILTER);
861 imf->im6f_st[1] = imf->im6f_st[0];
865 * Mark socket-layer filter set as INCLUDE {} at t1.
868 im6f_leave(struct in6_mfilter *imf)
870 struct ip6_msource *ims;
871 struct in6_msource *lims;
873 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
874 lims = (struct in6_msource *)ims;
875 lims->im6sl_st[1] = MCAST_UNDEFINED;
877 imf->im6f_st[1] = MCAST_INCLUDE;
881 * Mark socket-layer filter set deltas as committed.
884 im6f_commit(struct in6_mfilter *imf)
886 struct ip6_msource *ims;
887 struct in6_msource *lims;
889 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
890 lims = (struct in6_msource *)ims;
891 lims->im6sl_st[0] = lims->im6sl_st[1];
893 imf->im6f_st[0] = imf->im6f_st[1];
897 * Reap unreferenced sources from socket-layer filter set.
900 im6f_reap(struct in6_mfilter *imf)
902 struct ip6_msource *ims, *tims;
903 struct in6_msource *lims;
905 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
906 lims = (struct in6_msource *)ims;
907 if ((lims->im6sl_st[0] == MCAST_UNDEFINED) &&
908 (lims->im6sl_st[1] == MCAST_UNDEFINED)) {
909 CTR2(KTR_MLD, "%s: free lims %p", __func__, ims);
910 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
911 free(ims, M_IN6MFILTER);
918 * Purge socket-layer filter set.
921 im6f_purge(struct in6_mfilter *imf)
923 struct ip6_msource *ims, *tims;
925 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
926 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
927 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
928 free(ims, M_IN6MFILTER);
931 imf->im6f_st[0] = imf->im6f_st[1] = MCAST_UNDEFINED;
932 KASSERT(RB_EMPTY(&imf->im6f_sources),
933 ("%s: im6f_sources not empty", __func__));
937 * Look up a source filter entry for a multicast group.
939 * inm is the group descriptor to work with.
940 * addr is the IPv6 address to look up.
941 * noalloc may be non-zero to suppress allocation of sources.
942 * *pims will be set to the address of the retrieved or allocated source.
944 * SMPng: NOTE: may be called with locks held.
945 * Return 0 if successful, otherwise return a non-zero error code.
948 in6m_get_source(struct in6_multi *inm, const struct in6_addr *addr,
949 const int noalloc, struct ip6_msource **pims)
951 struct ip6_msource find;
952 struct ip6_msource *ims, *nims;
954 char ip6tbuf[INET6_ADDRSTRLEN];
957 find.im6s_addr = *addr;
958 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
959 if (ims == NULL && !noalloc) {
960 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
962 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
966 nims->im6s_addr = *addr;
967 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
970 CTR3(KTR_MLD, "%s: allocated %s as %p", __func__,
971 ip6_sprintf(ip6tbuf, addr), ims);
979 * Merge socket-layer source into MLD-layer source.
980 * If rollback is non-zero, perform the inverse of the merge.
983 im6s_merge(struct ip6_msource *ims, const struct in6_msource *lims,
986 int n = rollback ? -1 : 1;
988 char ip6tbuf[INET6_ADDRSTRLEN];
990 ip6_sprintf(ip6tbuf, &lims->im6s_addr);
993 if (lims->im6sl_st[0] == MCAST_EXCLUDE) {
994 CTR3(KTR_MLD, "%s: t1 ex -= %d on %s", __func__, n, ip6tbuf);
995 ims->im6s_st[1].ex -= n;
996 } else if (lims->im6sl_st[0] == MCAST_INCLUDE) {
997 CTR3(KTR_MLD, "%s: t1 in -= %d on %s", __func__, n, ip6tbuf);
998 ims->im6s_st[1].in -= n;
1001 if (lims->im6sl_st[1] == MCAST_EXCLUDE) {
1002 CTR3(KTR_MLD, "%s: t1 ex += %d on %s", __func__, n, ip6tbuf);
1003 ims->im6s_st[1].ex += n;
1004 } else if (lims->im6sl_st[1] == MCAST_INCLUDE) {
1005 CTR3(KTR_MLD, "%s: t1 in += %d on %s", __func__, n, ip6tbuf);
1006 ims->im6s_st[1].in += n;
1011 * Atomically update the global in6_multi state, when a membership's
1012 * filter list is being updated in any way.
1014 * imf is the per-inpcb-membership group filter pointer.
1015 * A fake imf may be passed for in-kernel consumers.
1017 * XXX This is a candidate for a set-symmetric-difference style loop
1018 * which would eliminate the repeated lookup from root of ims nodes,
1019 * as they share the same key space.
1021 * If any error occurred this function will back out of refcounts
1022 * and return a non-zero value.
1025 in6m_merge(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1027 struct ip6_msource *ims, *nims;
1028 struct in6_msource *lims;
1029 int schanged, error;
1035 IN6_MULTI_LIST_LOCK_ASSERT();
1038 * Update the source filters first, as this may fail.
1039 * Maintain count of in-mode filters at t0, t1. These are
1040 * used to work out if we transition into ASM mode or not.
1041 * Maintain a count of source filters whose state was
1042 * actually modified by this operation.
1044 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
1045 lims = (struct in6_msource *)ims;
1046 if (lims->im6sl_st[0] == imf->im6f_st[0]) nsrc0++;
1047 if (lims->im6sl_st[1] == imf->im6f_st[1]) nsrc1++;
1048 if (lims->im6sl_st[0] == lims->im6sl_st[1]) continue;
1049 error = in6m_get_source(inm, &lims->im6s_addr, 0, &nims);
1053 im6s_merge(nims, lims, 0);
1056 struct ip6_msource *bims;
1058 RB_FOREACH_REVERSE_FROM(ims, ip6_msource_tree, nims) {
1059 lims = (struct in6_msource *)ims;
1060 if (lims->im6sl_st[0] == lims->im6sl_st[1])
1062 (void)in6m_get_source(inm, &lims->im6s_addr, 1, &bims);
1065 im6s_merge(bims, lims, 1);
1070 CTR3(KTR_MLD, "%s: imf filters in-mode: %d at t0, %d at t1",
1071 __func__, nsrc0, nsrc1);
1073 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1074 if (imf->im6f_st[0] == imf->im6f_st[1] &&
1075 imf->im6f_st[1] == MCAST_INCLUDE) {
1077 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
1078 --inm->in6m_st[1].iss_in;
1082 /* Handle filter mode transition on socket. */
1083 if (imf->im6f_st[0] != imf->im6f_st[1]) {
1084 CTR3(KTR_MLD, "%s: imf transition %d to %d",
1085 __func__, imf->im6f_st[0], imf->im6f_st[1]);
1087 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
1088 CTR1(KTR_MLD, "%s: --ex on inm at t1", __func__);
1089 --inm->in6m_st[1].iss_ex;
1090 } else if (imf->im6f_st[0] == MCAST_INCLUDE) {
1091 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
1092 --inm->in6m_st[1].iss_in;
1095 if (imf->im6f_st[1] == MCAST_EXCLUDE) {
1096 CTR1(KTR_MLD, "%s: ex++ on inm at t1", __func__);
1097 inm->in6m_st[1].iss_ex++;
1098 } else if (imf->im6f_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
1099 CTR1(KTR_MLD, "%s: in++ on inm at t1", __func__);
1100 inm->in6m_st[1].iss_in++;
1105 * Track inm filter state in terms of listener counts.
1106 * If there are any exclusive listeners, stack-wide
1107 * membership is exclusive.
1108 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1109 * If no listeners remain, state is undefined at t1,
1110 * and the MLD lifecycle for this group should finish.
1112 if (inm->in6m_st[1].iss_ex > 0) {
1113 CTR1(KTR_MLD, "%s: transition to EX", __func__);
1114 inm->in6m_st[1].iss_fmode = MCAST_EXCLUDE;
1115 } else if (inm->in6m_st[1].iss_in > 0) {
1116 CTR1(KTR_MLD, "%s: transition to IN", __func__);
1117 inm->in6m_st[1].iss_fmode = MCAST_INCLUDE;
1119 CTR1(KTR_MLD, "%s: transition to UNDEF", __func__);
1120 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
1123 /* Decrement ASM listener count on transition out of ASM mode. */
1124 if (imf->im6f_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1125 if ((imf->im6f_st[1] != MCAST_EXCLUDE) ||
1126 (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 > 0)) {
1127 CTR1(KTR_MLD, "%s: --asm on inm at t1", __func__);
1128 --inm->in6m_st[1].iss_asm;
1132 /* Increment ASM listener count on transition to ASM mode. */
1133 if (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1134 CTR1(KTR_MLD, "%s: asm++ on inm at t1", __func__);
1135 inm->in6m_st[1].iss_asm++;
1138 CTR3(KTR_MLD, "%s: merged imf %p to inm %p", __func__, imf, inm);
1143 CTR1(KTR_MLD, "%s: sources changed; reaping", __func__);
1150 * Mark an in6_multi's filter set deltas as committed.
1151 * Called by MLD after a state change has been enqueued.
1154 in6m_commit(struct in6_multi *inm)
1156 struct ip6_msource *ims;
1158 CTR2(KTR_MLD, "%s: commit inm %p", __func__, inm);
1159 CTR1(KTR_MLD, "%s: pre commit:", __func__);
1162 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
1163 ims->im6s_st[0] = ims->im6s_st[1];
1165 inm->in6m_st[0] = inm->in6m_st[1];
1169 * Reap unreferenced nodes from an in6_multi's filter set.
1172 in6m_reap(struct in6_multi *inm)
1174 struct ip6_msource *ims, *tims;
1176 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1177 if (ims->im6s_st[0].ex > 0 || ims->im6s_st[0].in > 0 ||
1178 ims->im6s_st[1].ex > 0 || ims->im6s_st[1].in > 0 ||
1181 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1182 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1183 free(ims, M_IP6MSOURCE);
1189 * Purge all source nodes from an in6_multi's filter set.
1192 in6m_purge(struct in6_multi *inm)
1194 struct ip6_msource *ims, *tims;
1196 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1197 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1198 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1199 free(ims, M_IP6MSOURCE);
1202 /* Free state-change requests that might be queued. */
1203 mbufq_drain(&inm->in6m_scq);
1207 * Join a multicast address w/o sources.
1208 * KAME compatibility entry point.
1210 * SMPng: Assume no mc locks held by caller.
1213 in6_joingroup(struct ifnet *ifp, const struct in6_addr *mcaddr,
1214 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1220 error = in6_joingroup_locked(ifp, mcaddr, NULL, pinm, delay);
1226 * Join a multicast group; real entry point.
1228 * Only preserves atomicity at inm level.
1229 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1231 * If the MLD downcall fails, the group is not joined, and an error
1235 in6_joingroup_locked(struct ifnet *ifp, const struct in6_addr *mcaddr,
1236 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1239 struct in6_multi_head inmh;
1240 struct in6_mfilter timf;
1241 struct in6_multi *inm;
1242 struct ifmultiaddr *ifma;
1245 char ip6tbuf[INET6_ADDRSTRLEN];
1249 * Sanity: Check scope zone ID was set for ifp, if and
1250 * only if group is scoped to an interface.
1252 KASSERT(IN6_IS_ADDR_MULTICAST(mcaddr),
1253 ("%s: not a multicast address", __func__));
1254 if (IN6_IS_ADDR_MC_LINKLOCAL(mcaddr) ||
1255 IN6_IS_ADDR_MC_INTFACELOCAL(mcaddr)) {
1256 KASSERT(mcaddr->s6_addr16[1] != 0,
1257 ("%s: scope zone ID not set", __func__));
1260 IN6_MULTI_LOCK_ASSERT();
1261 IN6_MULTI_LIST_UNLOCK_ASSERT();
1263 CTR4(KTR_MLD, "%s: join %s on %p(%s))", __func__,
1264 ip6_sprintf(ip6tbuf, mcaddr), ifp, if_name(ifp));
1270 * If no imf was specified (i.e. kernel consumer),
1271 * fake one up and assume it is an ASM join.
1274 im6f_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1277 error = in6_getmulti(ifp, mcaddr, &inm);
1279 CTR1(KTR_MLD, "%s: in6_getmulti() failure", __func__);
1283 IN6_MULTI_LIST_LOCK();
1284 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1285 error = in6m_merge(inm, imf);
1287 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1288 goto out_in6m_release;
1291 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1292 error = mld_change_state(inm, delay);
1294 CTR1(KTR_MLD, "%s: failed to update source", __func__);
1295 goto out_in6m_release;
1301 struct epoch_tracker et;
1303 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1305 NET_EPOCH_ENTER(et);
1306 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1307 if (ifma->ifma_protospec == inm) {
1308 ifma->ifma_protospec = NULL;
1312 in6m_disconnect_locked(&inmh, inm);
1313 in6m_rele_locked(&inmh, inm);
1315 IF_ADDR_WUNLOCK(ifp);
1319 IN6_MULTI_LIST_UNLOCK();
1320 in6m_release_list_deferred(&inmh);
1325 * Leave a multicast group; unlocked entry point.
1328 in6_leavegroup(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1333 error = in6_leavegroup_locked(inm, imf);
1339 * Leave a multicast group; real entry point.
1340 * All source filters will be expunged.
1342 * Only preserves atomicity at inm level.
1344 * Holding the write lock for the INP which contains imf
1345 * is highly advisable. We can't assert for it as imf does not
1346 * contain a back-pointer to the owning inp.
1348 * Note: This is not the same as in6m_release(*) as this function also
1349 * makes a state change downcall into MLD.
1352 in6_leavegroup_locked(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1354 struct in6_multi_head inmh;
1355 struct in6_mfilter timf;
1359 char ip6tbuf[INET6_ADDRSTRLEN];
1364 IN6_MULTI_LOCK_ASSERT();
1366 CTR5(KTR_MLD, "%s: leave inm %p, %s/%s, imf %p", __func__,
1367 inm, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1368 (in6m_is_ifp_detached(inm) ? "null" : if_name(inm->in6m_ifp)),
1372 * If no imf was specified (i.e. kernel consumer),
1373 * fake one up and assume it is an ASM join.
1376 im6f_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1381 * Begin state merge transaction at MLD layer.
1383 * As this particular invocation should not cause any memory
1384 * to be allocated, and there is no opportunity to roll back
1385 * the transaction, it MUST NOT fail.
1388 ifp = inm->in6m_ifp;
1389 IN6_MULTI_LIST_LOCK();
1390 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1391 error = in6m_merge(inm, imf);
1392 KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1394 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1397 error = mld_change_state(inm, 0);
1399 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1401 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1406 if (inm->in6m_refcount == 1)
1407 in6m_disconnect_locked(&inmh, inm);
1408 in6m_rele_locked(&inmh, inm);
1410 IF_ADDR_WUNLOCK(ifp);
1411 IN6_MULTI_LIST_UNLOCK();
1412 in6m_release_list_deferred(&inmh);
1417 * Block or unblock an ASM multicast source on an inpcb.
1418 * This implements the delta-based API described in RFC 3678.
1420 * The delta-based API applies only to exclusive-mode memberships.
1421 * An MLD downcall will be performed.
1423 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1425 * Return 0 if successful, otherwise return an appropriate error code.
1428 in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1430 struct group_source_req gsr;
1431 struct epoch_tracker et;
1432 sockunion_t *gsa, *ssa;
1434 struct in6_mfilter *imf;
1435 struct ip6_moptions *imo;
1436 struct in6_msource *ims;
1437 struct in6_multi *inm;
1441 char ip6tbuf[INET6_ADDRSTRLEN];
1448 memset(&gsr, 0, sizeof(struct group_source_req));
1449 gsa = (sockunion_t *)&gsr.gsr_group;
1450 ssa = (sockunion_t *)&gsr.gsr_source;
1452 switch (sopt->sopt_name) {
1453 case MCAST_BLOCK_SOURCE:
1454 case MCAST_UNBLOCK_SOURCE:
1455 error = sooptcopyin(sopt, &gsr,
1456 sizeof(struct group_source_req),
1457 sizeof(struct group_source_req));
1461 if (gsa->sin6.sin6_family != AF_INET6 ||
1462 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1465 if (ssa->sin6.sin6_family != AF_INET6 ||
1466 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1470 * XXXGL: this function should use ifnet_byindex_ref, or
1471 * expand the epoch section all the way to where we put
1474 NET_EPOCH_ENTER(et);
1475 ifp = ifnet_byindex(gsr.gsr_interface);
1478 return (EADDRNOTAVAIL);
1480 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1485 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1486 __func__, sopt->sopt_name);
1487 return (EOPNOTSUPP);
1491 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1494 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1497 * Check if we are actually a member of this group.
1499 imo = in6p_findmoptions(inp);
1500 imf = im6o_match_group(imo, ifp, &gsa->sa);
1502 error = EADDRNOTAVAIL;
1503 goto out_in6p_locked;
1505 inm = imf->im6f_in6m;
1508 * Attempting to use the delta-based API on an
1509 * non exclusive-mode membership is an error.
1511 fmode = imf->im6f_st[0];
1512 if (fmode != MCAST_EXCLUDE) {
1514 goto out_in6p_locked;
1518 * Deal with error cases up-front:
1519 * Asked to block, but already blocked; or
1520 * Asked to unblock, but nothing to unblock.
1521 * If adding a new block entry, allocate it.
1523 ims = im6o_match_source(imf, &ssa->sa);
1524 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1525 CTR3(KTR_MLD, "%s: source %s %spresent", __func__,
1526 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
1527 doblock ? "" : "not ");
1528 error = EADDRNOTAVAIL;
1529 goto out_in6p_locked;
1532 INP_WLOCK_ASSERT(inp);
1535 * Begin state merge transaction at socket layer.
1538 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
1539 ims = im6f_graft(imf, fmode, &ssa->sin6);
1543 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
1544 error = im6f_prune(imf, &ssa->sin6);
1548 CTR1(KTR_MLD, "%s: merge imf state failed", __func__);
1549 goto out_im6f_rollback;
1553 * Begin state merge transaction at MLD layer.
1555 IN6_MULTI_LIST_LOCK();
1556 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1557 error = in6m_merge(inm, imf);
1559 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1561 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1562 error = mld_change_state(inm, 0);
1564 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1567 IN6_MULTI_LIST_UNLOCK();
1583 * Given an inpcb, return its multicast options structure pointer. Accepts
1584 * an unlocked inpcb pointer, but will return it locked. May sleep.
1586 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1587 * SMPng: NOTE: Returns with the INP write lock held.
1589 static struct ip6_moptions *
1590 in6p_findmoptions(struct inpcb *inp)
1592 struct ip6_moptions *imo;
1595 if (inp->in6p_moptions != NULL)
1596 return (inp->in6p_moptions);
1600 imo = malloc(sizeof(*imo), M_IP6MOPTS, M_WAITOK);
1602 imo->im6o_multicast_ifp = NULL;
1603 imo->im6o_multicast_hlim = V_ip6_defmcasthlim;
1604 imo->im6o_multicast_loop = in6_mcast_loop;
1605 STAILQ_INIT(&imo->im6o_head);
1608 if (inp->in6p_moptions != NULL) {
1609 free(imo, M_IP6MOPTS);
1610 return (inp->in6p_moptions);
1612 inp->in6p_moptions = imo;
1617 * Discard the IPv6 multicast options (and source filters).
1619 * SMPng: NOTE: assumes INP write lock is held.
1621 * XXX can all be safely deferred to epoch_call
1626 inp_gcmoptions(struct ip6_moptions *imo)
1628 struct in6_mfilter *imf;
1629 struct in6_multi *inm;
1632 while ((imf = ip6_mfilter_first(&imo->im6o_head)) != NULL) {
1633 ip6_mfilter_remove(&imo->im6o_head, imf);
1636 if ((inm = imf->im6f_in6m) != NULL) {
1637 if ((ifp = inm->in6m_ifp) != NULL) {
1638 CURVNET_SET(ifp->if_vnet);
1639 (void)in6_leavegroup(inm, imf);
1642 (void)in6_leavegroup(inm, imf);
1645 ip6_mfilter_free(imf);
1647 free(imo, M_IP6MOPTS);
1651 ip6_freemoptions(struct ip6_moptions *imo)
1655 inp_gcmoptions(imo);
1659 * Atomically get source filters on a socket for an IPv6 multicast group.
1660 * Called with INP lock held; returns with lock released.
1663 in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1665 struct epoch_tracker et;
1666 struct __msfilterreq msfr;
1669 struct ip6_moptions *imo;
1670 struct in6_mfilter *imf;
1671 struct ip6_msource *ims;
1672 struct in6_msource *lims;
1673 struct sockaddr_in6 *psin;
1674 struct sockaddr_storage *ptss;
1675 struct sockaddr_storage *tss;
1677 size_t nsrcs, ncsrcs;
1679 INP_WLOCK_ASSERT(inp);
1681 imo = inp->in6p_moptions;
1682 KASSERT(imo != NULL, ("%s: null ip6_moptions", __func__));
1686 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1687 sizeof(struct __msfilterreq));
1691 if (msfr.msfr_group.ss_family != AF_INET6 ||
1692 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
1695 gsa = (sockunion_t *)&msfr.msfr_group;
1696 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1700 * XXXGL: this function should use ifnet_byindex_ref, or expand the
1701 * epoch section all the way to where the interface is referenced.
1703 NET_EPOCH_ENTER(et);
1704 ifp = ifnet_byindex(msfr.msfr_ifindex);
1707 return (EADDRNOTAVAIL);
1708 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1713 * Lookup group on the socket.
1715 imf = im6o_match_group(imo, ifp, &gsa->sa);
1718 return (EADDRNOTAVAIL);
1722 * Ignore memberships which are in limbo.
1724 if (imf->im6f_st[1] == MCAST_UNDEFINED) {
1728 msfr.msfr_fmode = imf->im6f_st[1];
1731 * If the user specified a buffer, copy out the source filter
1732 * entries to userland gracefully.
1733 * We only copy out the number of entries which userland
1734 * has asked for, but we always tell userland how big the
1735 * buffer really needs to be.
1737 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
1738 msfr.msfr_nsrcs = in6_mcast_maxsocksrc;
1740 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1741 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1742 M_TEMP, M_NOWAIT | M_ZERO);
1750 * Count number of sources in-mode at t0.
1751 * If buffer space exists and remains, copy out source entries.
1753 nsrcs = msfr.msfr_nsrcs;
1756 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
1757 lims = (struct in6_msource *)ims;
1758 if (lims->im6sl_st[0] == MCAST_UNDEFINED ||
1759 lims->im6sl_st[0] != imf->im6f_st[0])
1762 if (tss != NULL && nsrcs > 0) {
1763 psin = (struct sockaddr_in6 *)ptss;
1764 psin->sin6_family = AF_INET6;
1765 psin->sin6_len = sizeof(struct sockaddr_in6);
1766 psin->sin6_addr = lims->im6s_addr;
1767 psin->sin6_port = 0;
1776 error = copyout(tss, msfr.msfr_srcs,
1777 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1783 msfr.msfr_nsrcs = ncsrcs;
1784 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1790 * Return the IP multicast options in response to user getsockopt().
1793 ip6_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1795 struct ip6_moptions *im6o;
1800 im6o = inp->in6p_moptions;
1801 /* If socket is neither of type SOCK_RAW or SOCK_DGRAM, reject it. */
1802 if (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1803 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM) {
1805 return (EOPNOTSUPP);
1809 switch (sopt->sopt_name) {
1810 case IPV6_MULTICAST_IF:
1811 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) {
1814 optval = im6o->im6o_multicast_ifp->if_index;
1817 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1820 case IPV6_MULTICAST_HOPS:
1822 optval = V_ip6_defmcasthlim;
1824 optval = im6o->im6o_multicast_hlim;
1826 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1829 case IPV6_MULTICAST_LOOP:
1831 optval = in6_mcast_loop; /* XXX VIMAGE */
1833 optval = im6o->im6o_multicast_loop;
1835 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1840 error = EADDRNOTAVAIL;
1843 error = in6p_get_source_filters(inp, sopt);
1849 error = ENOPROTOOPT;
1853 INP_UNLOCK_ASSERT(inp);
1859 * Look up the ifnet to use for a multicast group membership,
1860 * given the address of an IPv6 group.
1862 * This routine exists to support legacy IPv6 multicast applications.
1864 * Use the socket's current FIB number for any required FIB lookup. Look up the
1865 * group address in the unicast FIB, and use its ifp; usually, this points to
1866 * the default next-hop. If the FIB lookup fails, return NULL.
1868 * FUTURE: Support multiple forwarding tables for IPv6.
1870 * Returns NULL if no ifp could be found.
1872 static struct ifnet *
1873 in6p_lookup_mcast_ifp(const struct inpcb *inp, const struct sockaddr_in6 *gsin6)
1875 struct nhop_object *nh;
1876 struct in6_addr dst;
1880 KASSERT(gsin6->sin6_family == AF_INET6,
1881 ("%s: not AF_INET6 group", __func__));
1883 in6_splitscope(&gsin6->sin6_addr, &dst, &scopeid);
1884 fibnum = inp->inp_inc.inc_fibnum;
1885 nh = fib6_lookup(fibnum, &dst, scopeid, 0, 0);
1887 return (nh ? nh->nh_ifp : NULL);
1891 * Join an IPv6 multicast group, possibly with a source.
1893 * FIXME: The KAME use of the unspecified address (::)
1894 * to join *all* multicast groups is currently unsupported.
1896 * XXXGL: this function multiple times uses ifnet_byindex() without
1897 * proper protection - staying in epoch, or putting reference on ifnet.
1900 in6p_join_group(struct inpcb *inp, struct sockopt *sopt)
1902 struct in6_multi_head inmh;
1903 struct group_source_req gsr;
1904 struct epoch_tracker et;
1905 sockunion_t *gsa, *ssa;
1907 struct in6_mfilter *imf;
1908 struct ip6_moptions *imo;
1909 struct in6_multi *inm;
1910 struct in6_msource *lims;
1918 memset(&gsr, 0, sizeof(struct group_source_req));
1919 gsa = (sockunion_t *)&gsr.gsr_group;
1920 gsa->ss.ss_family = AF_UNSPEC;
1921 ssa = (sockunion_t *)&gsr.gsr_source;
1922 ssa->ss.ss_family = AF_UNSPEC;
1925 * Chew everything into struct group_source_req.
1926 * Overwrite the port field if present, as the sockaddr
1927 * being copied in may be matched with a binary comparison.
1928 * Ignore passed-in scope ID.
1930 switch (sopt->sopt_name) {
1931 case IPV6_JOIN_GROUP: {
1932 struct ipv6_mreq mreq;
1934 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
1935 sizeof(struct ipv6_mreq));
1939 gsa->sin6.sin6_family = AF_INET6;
1940 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
1941 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
1943 if (mreq.ipv6mr_interface == 0) {
1944 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
1946 NET_EPOCH_ENTER(et);
1947 ifp = ifnet_byindex(mreq.ipv6mr_interface);
1950 return (EADDRNOTAVAIL);
1952 CTR3(KTR_MLD, "%s: ipv6mr_interface = %d, ifp = %p",
1953 __func__, mreq.ipv6mr_interface, ifp);
1956 case MCAST_JOIN_GROUP:
1957 case MCAST_JOIN_SOURCE_GROUP:
1958 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1959 error = sooptcopyin(sopt, &gsr,
1960 sizeof(struct group_req),
1961 sizeof(struct group_req));
1962 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1963 error = sooptcopyin(sopt, &gsr,
1964 sizeof(struct group_source_req),
1965 sizeof(struct group_source_req));
1970 if (gsa->sin6.sin6_family != AF_INET6 ||
1971 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1974 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1975 if (ssa->sin6.sin6_family != AF_INET6 ||
1976 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1978 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
1981 * TODO: Validate embedded scope ID in source
1982 * list entry against passed-in ifp, if and only
1983 * if source list filter entry is iface or node local.
1985 in6_clearscope(&ssa->sin6.sin6_addr);
1986 ssa->sin6.sin6_port = 0;
1987 ssa->sin6.sin6_scope_id = 0;
1989 NET_EPOCH_ENTER(et);
1990 ifp = ifnet_byindex(gsr.gsr_interface);
1993 return (EADDRNOTAVAIL);
1997 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1998 __func__, sopt->sopt_name);
1999 return (EOPNOTSUPP);
2003 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2006 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
2007 return (EADDRNOTAVAIL);
2009 gsa->sin6.sin6_port = 0;
2010 gsa->sin6.sin6_scope_id = 0;
2013 * Always set the scope zone ID on memberships created from userland.
2014 * Use the passed-in ifp to do this.
2015 * XXX The in6_setscope() return value is meaningless.
2016 * XXX SCOPE6_LOCK() is taken by in6_setscope().
2018 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2023 * Find the membership in the membership list.
2025 imo = in6p_findmoptions(inp);
2026 imf = im6o_match_group(imo, ifp, &gsa->sa);
2031 if (ip6_mfilter_count(&imo->im6o_head) >= IPV6_MAX_MEMBERSHIPS) {
2033 goto out_in6p_locked;
2037 inm = imf->im6f_in6m;
2039 if (ssa->ss.ss_family != AF_UNSPEC) {
2041 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2042 * is an error. On an existing inclusive membership,
2043 * it just adds the source to the filter list.
2045 if (imf->im6f_st[1] != MCAST_INCLUDE) {
2047 goto out_in6p_locked;
2050 * Throw out duplicates.
2052 * XXX FIXME: This makes a naive assumption that
2053 * even if entries exist for *ssa in this imf,
2054 * they will be rejected as dupes, even if they
2055 * are not valid in the current mode (in-mode).
2057 * in6_msource is transactioned just as for anything
2058 * else in SSM -- but note naive use of in6m_graft()
2059 * below for allocating new filter entries.
2061 * This is only an issue if someone mixes the
2062 * full-state SSM API with the delta-based API,
2063 * which is discouraged in the relevant RFCs.
2065 lims = im6o_match_source(imf, &ssa->sa);
2066 if (lims != NULL /*&&
2067 lims->im6sl_st[1] == MCAST_INCLUDE*/) {
2068 error = EADDRNOTAVAIL;
2069 goto out_in6p_locked;
2073 * MCAST_JOIN_GROUP alone, on any existing membership,
2074 * is rejected, to stop the same inpcb tying up
2075 * multiple refs to the in_multi.
2076 * On an existing inclusive membership, this is also
2077 * an error; if you want to change filter mode,
2078 * you must use the userland API setsourcefilter().
2079 * XXX We don't reject this for imf in UNDEFINED
2080 * state at t1, because allocation of a filter
2081 * is atomic with allocation of a membership.
2084 goto out_in6p_locked;
2089 * Begin state merge transaction at socket layer.
2091 INP_WLOCK_ASSERT(inp);
2094 * Graft new source into filter list for this inpcb's
2095 * membership of the group. The in6_multi may not have
2096 * been allocated yet if this is a new membership, however,
2097 * the in_mfilter slot will be allocated and must be initialized.
2099 * Note: Grafting of exclusive mode filters doesn't happen
2101 * XXX: Should check for non-NULL lims (node exists but may
2102 * not be in-mode) for interop with full-state API.
2104 if (ssa->ss.ss_family != AF_UNSPEC) {
2105 /* Membership starts in IN mode */
2107 CTR1(KTR_MLD, "%s: new join w/source", __func__);
2108 imf = ip6_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_INCLUDE);
2111 goto out_in6p_locked;
2114 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
2116 lims = im6f_graft(imf, MCAST_INCLUDE, &ssa->sin6);
2118 CTR1(KTR_MLD, "%s: merge imf state failed",
2121 goto out_in6p_locked;
2124 /* No address specified; Membership starts in EX mode */
2126 CTR1(KTR_MLD, "%s: new join w/o source", __func__);
2127 imf = ip6_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_EXCLUDE);
2130 goto out_in6p_locked;
2136 * Begin state merge transaction at MLD layer.
2142 error = in6_joingroup_locked(ifp, &gsa->sin6.sin6_addr, imf,
2143 &imf->im6f_in6m, 0);
2146 if (in_pcbrele_wlocked(inp)) {
2148 goto out_in6p_unlocked;
2151 goto out_in6p_locked;
2154 * NOTE: Refcount from in6_joingroup_locked()
2155 * is protecting membership.
2157 ip6_mfilter_insert(&imo->im6o_head, imf);
2159 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2160 IN6_MULTI_LIST_LOCK();
2161 error = in6m_merge(inm, imf);
2163 CTR1(KTR_MLD, "%s: failed to merge inm state",
2165 IN6_MULTI_LIST_UNLOCK();
2168 goto out_in6p_locked;
2170 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2171 error = mld_change_state(inm, 0);
2172 IN6_MULTI_LIST_UNLOCK();
2175 CTR1(KTR_MLD, "%s: failed mld downcall",
2179 goto out_in6p_locked;
2191 if (is_new && imf) {
2192 if (imf->im6f_in6m != NULL) {
2193 struct in6_multi_head inmh;
2196 SLIST_INSERT_HEAD(&inmh, imf->im6f_in6m, in6m_defer);
2197 in6m_release_list_deferred(&inmh);
2199 ip6_mfilter_free(imf);
2205 * Leave an IPv6 multicast group on an inpcb, possibly with a source.
2208 in6p_leave_group(struct inpcb *inp, struct sockopt *sopt)
2210 struct ipv6_mreq mreq;
2211 struct group_source_req gsr;
2212 struct epoch_tracker et;
2213 sockunion_t *gsa, *ssa;
2215 struct in6_mfilter *imf;
2216 struct ip6_moptions *imo;
2217 struct in6_msource *ims;
2218 struct in6_multi *inm;
2223 char ip6tbuf[INET6_ADDRSTRLEN];
2231 memset(&gsr, 0, sizeof(struct group_source_req));
2232 gsa = (sockunion_t *)&gsr.gsr_group;
2233 gsa->ss.ss_family = AF_UNSPEC;
2234 ssa = (sockunion_t *)&gsr.gsr_source;
2235 ssa->ss.ss_family = AF_UNSPEC;
2238 * Chew everything passed in up into a struct group_source_req
2239 * as that is easier to process.
2240 * Note: Any embedded scope ID in the multicast group passed
2241 * in by userland is ignored, the interface index is the recommended
2242 * mechanism to specify an interface; see below.
2244 switch (sopt->sopt_name) {
2245 case IPV6_LEAVE_GROUP:
2246 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
2247 sizeof(struct ipv6_mreq));
2250 gsa->sin6.sin6_family = AF_INET6;
2251 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
2252 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
2253 gsa->sin6.sin6_port = 0;
2254 gsa->sin6.sin6_scope_id = 0;
2255 ifindex = mreq.ipv6mr_interface;
2258 case MCAST_LEAVE_GROUP:
2259 case MCAST_LEAVE_SOURCE_GROUP:
2260 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2261 error = sooptcopyin(sopt, &gsr,
2262 sizeof(struct group_req),
2263 sizeof(struct group_req));
2264 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2265 error = sooptcopyin(sopt, &gsr,
2266 sizeof(struct group_source_req),
2267 sizeof(struct group_source_req));
2272 if (gsa->sin6.sin6_family != AF_INET6 ||
2273 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2275 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2276 if (ssa->sin6.sin6_family != AF_INET6 ||
2277 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2279 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
2282 * TODO: Validate embedded scope ID in source
2283 * list entry against passed-in ifp, if and only
2284 * if source list filter entry is iface or node local.
2286 in6_clearscope(&ssa->sin6.sin6_addr);
2288 gsa->sin6.sin6_port = 0;
2289 gsa->sin6.sin6_scope_id = 0;
2290 ifindex = gsr.gsr_interface;
2294 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
2295 __func__, sopt->sopt_name);
2296 return (EOPNOTSUPP);
2300 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2304 * Validate interface index if provided. If no interface index
2305 * was provided separately, attempt to look the membership up
2306 * from the default scope as a last resort to disambiguate
2307 * the membership we are being asked to leave.
2308 * XXX SCOPE6 lock potentially taken here.
2311 NET_EPOCH_ENTER(et);
2312 ifp = ifnet_byindex(ifindex);
2313 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */
2315 return (EADDRNOTAVAIL);
2316 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2318 error = sa6_embedscope(&gsa->sin6, V_ip6_use_defzone);
2320 return (EADDRNOTAVAIL);
2322 * Some badly behaved applications don't pass an ifindex
2323 * or a scope ID, which is an API violation. In this case,
2324 * perform a lookup as per a v6 join.
2326 * XXX For now, stomp on zone ID for the corner case.
2327 * This is not the 'KAME way', but we need to see the ifp
2328 * directly until such time as this implementation is
2329 * refactored, assuming the scope IDs are the way to go.
2331 ifindex = ntohs(gsa->sin6.sin6_addr.s6_addr16[1]);
2333 CTR2(KTR_MLD, "%s: warning: no ifindex, looking up "
2334 "ifp for group %s.", __func__,
2335 ip6_sprintf(ip6tbuf, &gsa->sin6.sin6_addr));
2336 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
2338 NET_EPOCH_ENTER(et);
2339 ifp = ifnet_byindex(ifindex);
2340 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */
2343 return (EADDRNOTAVAIL);
2346 CTR2(KTR_MLD, "%s: ifp = %p", __func__, ifp);
2347 KASSERT(ifp != NULL, ("%s: ifp did not resolve", __func__));
2352 * Find the membership in the membership list.
2354 imo = in6p_findmoptions(inp);
2355 imf = im6o_match_group(imo, ifp, &gsa->sa);
2357 error = EADDRNOTAVAIL;
2358 goto out_in6p_locked;
2360 inm = imf->im6f_in6m;
2362 if (ssa->ss.ss_family != AF_UNSPEC)
2366 * Begin state merge transaction at socket layer.
2368 INP_WLOCK_ASSERT(inp);
2371 * If we were instructed only to leave a given source, do so.
2372 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2375 ip6_mfilter_remove(&imo->im6o_head, imf);
2379 * Give up the multicast address record to which
2380 * the membership points.
2382 (void)in6_leavegroup_locked(inm, imf);
2384 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
2385 error = EADDRNOTAVAIL;
2386 goto out_in6p_locked;
2388 ims = im6o_match_source(imf, &ssa->sa);
2390 CTR3(KTR_MLD, "%s: source %p %spresent", __func__,
2391 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
2393 error = EADDRNOTAVAIL;
2394 goto out_in6p_locked;
2396 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
2397 error = im6f_prune(imf, &ssa->sin6);
2399 CTR1(KTR_MLD, "%s: merge imf state failed",
2401 goto out_in6p_locked;
2406 * Begin state merge transaction at MLD layer.
2409 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2410 IN6_MULTI_LIST_LOCK();
2411 error = in6m_merge(inm, imf);
2413 CTR1(KTR_MLD, "%s: failed to merge inm state",
2415 IN6_MULTI_LIST_UNLOCK();
2418 goto out_in6p_locked;
2421 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2422 error = mld_change_state(inm, 0);
2423 IN6_MULTI_LIST_UNLOCK();
2425 CTR1(KTR_MLD, "%s: failed mld downcall",
2429 goto out_in6p_locked;
2439 if (is_final && imf)
2440 ip6_mfilter_free(imf);
2447 * Select the interface for transmitting IPv6 multicast datagrams.
2449 * Either an instance of struct in6_addr or an instance of struct ipv6_mreqn
2450 * may be passed to this socket option. An address of in6addr_any or an
2451 * interface index of 0 is used to remove a previous selection.
2452 * When no interface is selected, one is chosen for every send.
2455 in6p_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2457 struct epoch_tracker et;
2459 struct ip6_moptions *imo;
2463 if (sopt->sopt_valsize != sizeof(u_int))
2466 error = sooptcopyin(sopt, &ifindex, sizeof(u_int), sizeof(u_int));
2469 NET_EPOCH_ENTER(et);
2473 ifp = ifnet_byindex(ifindex);
2474 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2476 return (EADDRNOTAVAIL);
2479 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */
2480 imo = in6p_findmoptions(inp);
2481 imo->im6o_multicast_ifp = ifp;
2488 * Atomically set source filters on a socket for an IPv6 multicast group.
2490 * XXXGL: unsafely exits epoch with ifnet pointer
2493 in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2495 struct __msfilterreq msfr;
2496 struct epoch_tracker et;
2499 struct in6_mfilter *imf;
2500 struct ip6_moptions *imo;
2501 struct in6_multi *inm;
2504 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2505 sizeof(struct __msfilterreq));
2509 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
2512 if (msfr.msfr_fmode != MCAST_EXCLUDE &&
2513 msfr.msfr_fmode != MCAST_INCLUDE)
2516 if (msfr.msfr_group.ss_family != AF_INET6 ||
2517 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
2520 gsa = (sockunion_t *)&msfr.msfr_group;
2521 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2524 gsa->sin6.sin6_port = 0; /* ignore port */
2526 NET_EPOCH_ENTER(et);
2527 ifp = ifnet_byindex(msfr.msfr_ifindex);
2530 return (EADDRNOTAVAIL);
2531 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2534 * Take the INP write lock.
2535 * Check if this socket is a member of this group.
2537 imo = in6p_findmoptions(inp);
2538 imf = im6o_match_group(imo, ifp, &gsa->sa);
2540 error = EADDRNOTAVAIL;
2541 goto out_in6p_locked;
2543 inm = imf->im6f_in6m;
2546 * Begin state merge transaction at socket layer.
2548 INP_WLOCK_ASSERT(inp);
2550 imf->im6f_st[1] = msfr.msfr_fmode;
2553 * Apply any new source filters, if present.
2554 * Make a copy of the user-space source vector so
2555 * that we may copy them with a single copyin. This
2556 * allows us to deal with page faults up-front.
2558 if (msfr.msfr_nsrcs > 0) {
2559 struct in6_msource *lims;
2560 struct sockaddr_in6 *psin;
2561 struct sockaddr_storage *kss, *pkss;
2566 CTR2(KTR_MLD, "%s: loading %lu source list entries",
2567 __func__, (unsigned long)msfr.msfr_nsrcs);
2568 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2570 error = copyin(msfr.msfr_srcs, kss,
2571 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2580 * Mark all source filters as UNDEFINED at t1.
2581 * Restore new group filter mode, as im6f_leave()
2582 * will set it to INCLUDE.
2585 imf->im6f_st[1] = msfr.msfr_fmode;
2588 * Update socket layer filters at t1, lazy-allocating
2589 * new entries. This saves a bunch of memory at the
2590 * cost of one RB_FIND() per source entry; duplicate
2591 * entries in the msfr_nsrcs vector are ignored.
2592 * If we encounter an error, rollback transaction.
2594 * XXX This too could be replaced with a set-symmetric
2595 * difference like loop to avoid walking from root
2596 * every time, as the key space is common.
2598 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2599 psin = (struct sockaddr_in6 *)pkss;
2600 if (psin->sin6_family != AF_INET6) {
2601 error = EAFNOSUPPORT;
2604 if (psin->sin6_len != sizeof(struct sockaddr_in6)) {
2608 if (IN6_IS_ADDR_MULTICAST(&psin->sin6_addr)) {
2613 * TODO: Validate embedded scope ID in source
2614 * list entry against passed-in ifp, if and only
2615 * if source list filter entry is iface or node local.
2617 in6_clearscope(&psin->sin6_addr);
2618 error = im6f_get_source(imf, psin, &lims);
2621 lims->im6sl_st[1] = imf->im6f_st[1];
2627 goto out_im6f_rollback;
2629 INP_WLOCK_ASSERT(inp);
2630 IN6_MULTI_LIST_LOCK();
2633 * Begin state merge transaction at MLD layer.
2635 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2636 error = in6m_merge(inm, imf);
2638 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
2640 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2641 error = mld_change_state(inm, 0);
2643 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
2646 IN6_MULTI_LIST_UNLOCK();
2662 * Set the IP multicast options in response to user setsockopt().
2664 * Many of the socket options handled in this function duplicate the
2665 * functionality of socket options in the regular unicast API. However,
2666 * it is not possible to merge the duplicate code, because the idempotence
2667 * of the IPv6 multicast part of the BSD Sockets API must be preserved;
2668 * the effects of these options must be treated as separate and distinct.
2670 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2673 ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2675 struct ip6_moptions *im6o;
2680 /* If socket is neither of type SOCK_RAW or SOCK_DGRAM, reject it. */
2681 if (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2682 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)
2683 return (EOPNOTSUPP);
2685 switch (sopt->sopt_name) {
2686 case IPV6_MULTICAST_IF:
2687 error = in6p_set_multicast_if(inp, sopt);
2690 case IPV6_MULTICAST_HOPS: {
2693 if (sopt->sopt_valsize != sizeof(int)) {
2697 error = sooptcopyin(sopt, &hlim, sizeof(hlim), sizeof(int));
2700 if (hlim < -1 || hlim > 255) {
2703 } else if (hlim == -1) {
2704 hlim = V_ip6_defmcasthlim;
2706 im6o = in6p_findmoptions(inp);
2707 im6o->im6o_multicast_hlim = hlim;
2712 case IPV6_MULTICAST_LOOP: {
2716 * Set the loopback flag for outgoing multicast packets.
2717 * Must be zero or one.
2719 if (sopt->sopt_valsize != sizeof(u_int)) {
2723 error = sooptcopyin(sopt, &loop, sizeof(u_int), sizeof(u_int));
2730 im6o = in6p_findmoptions(inp);
2731 im6o->im6o_multicast_loop = loop;
2736 case IPV6_JOIN_GROUP:
2737 case MCAST_JOIN_GROUP:
2738 case MCAST_JOIN_SOURCE_GROUP:
2739 error = in6p_join_group(inp, sopt);
2742 case IPV6_LEAVE_GROUP:
2743 case MCAST_LEAVE_GROUP:
2744 case MCAST_LEAVE_SOURCE_GROUP:
2745 error = in6p_leave_group(inp, sopt);
2748 case MCAST_BLOCK_SOURCE:
2749 case MCAST_UNBLOCK_SOURCE:
2750 error = in6p_block_unblock_source(inp, sopt);
2754 error = in6p_set_source_filters(inp, sopt);
2762 INP_UNLOCK_ASSERT(inp);
2768 * Expose MLD's multicast filter mode and source list(s) to userland,
2769 * keyed by (ifindex, group).
2770 * The filter mode is written out as a uint32_t, followed by
2771 * 0..n of struct in6_addr.
2772 * For use by ifmcstat(8).
2773 * SMPng: NOTE: unlocked read of ifindex space.
2776 sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS)
2778 struct in6_addr mcaddr;
2779 struct in6_addr src;
2780 struct epoch_tracker et;
2782 struct ifmultiaddr *ifma;
2783 struct in6_multi *inm;
2784 struct ip6_msource *ims;
2788 uint32_t fmode, ifindex;
2790 char ip6tbuf[INET6_ADDRSTRLEN];
2796 if (req->newptr != NULL)
2799 /* int: ifindex + 4 * 32 bits of IPv6 address */
2803 memcpy(&mcaddr, &name[1], sizeof(struct in6_addr));
2804 if (!IN6_IS_ADDR_MULTICAST(&mcaddr)) {
2805 CTR2(KTR_MLD, "%s: group %s is not multicast",
2806 __func__, ip6_sprintf(ip6tbuf, &mcaddr));
2811 NET_EPOCH_ENTER(et);
2812 ifp = ifnet_byindex(ifindex);
2815 CTR2(KTR_MLD, "%s: no ifp for ifindex %u",
2820 * Internal MLD lookups require that scope/zone ID is set.
2822 (void)in6_setscope(&mcaddr, ifp, NULL);
2824 retval = sysctl_wire_old_buffer(req,
2825 sizeof(uint32_t) + (in6_mcast_maxgrpsrc * sizeof(struct in6_addr)));
2832 IN6_MULTI_LIST_LOCK();
2833 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2834 inm = in6m_ifmultiaddr_get_inm(ifma);
2837 if (!IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, &mcaddr))
2839 fmode = inm->in6m_st[1].iss_fmode;
2840 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2843 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
2844 CTR2(KTR_MLD, "%s: visit node %p", __func__, ims);
2846 * Only copy-out sources which are in-mode.
2848 if (fmode != im6s_get_mode(inm, ims, 1)) {
2849 CTR1(KTR_MLD, "%s: skip non-in-mode",
2853 src = ims->im6s_addr;
2854 retval = SYSCTL_OUT(req, &src,
2855 sizeof(struct in6_addr));
2860 IN6_MULTI_LIST_UNLOCK();
2869 static const char *in6m_modestrs[] = { "un", "in", "ex" };
2872 in6m_mode_str(const int mode)
2875 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2876 return (in6m_modestrs[mode]);
2880 static const char *in6m_statestrs[] = {
2892 _Static_assert(nitems(in6m_statestrs) ==
2893 MLD_LEAVING_MEMBER - MLD_NOT_MEMBER + 1, "Missing MLD group state");
2896 in6m_state_str(const int state)
2899 if (state >= MLD_NOT_MEMBER && state <= MLD_LEAVING_MEMBER)
2900 return (in6m_statestrs[state]);
2905 * Dump an in6_multi structure to the console.
2908 in6m_print(const struct in6_multi *inm)
2911 char ip6tbuf[INET6_ADDRSTRLEN];
2913 if ((ktr_mask & KTR_MLD) == 0)
2916 printf("%s: --- begin in6m %p ---\n", __func__, inm);
2917 printf("addr %s ifp %p(%s) ifma %p\n",
2918 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2920 if_name(inm->in6m_ifp),
2922 printf("timer %u state %s refcount %u scq.len %u\n",
2924 in6m_state_str(inm->in6m_state),
2926 mbufq_len(&inm->in6m_scq));
2927 printf("mli %p nsrc %lu sctimer %u scrv %u\n",
2932 for (t = 0; t < 2; t++) {
2933 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
2934 in6m_mode_str(inm->in6m_st[t].iss_fmode),
2935 inm->in6m_st[t].iss_asm,
2936 inm->in6m_st[t].iss_ex,
2937 inm->in6m_st[t].iss_in,
2938 inm->in6m_st[t].iss_rec);
2940 printf("%s: --- end in6m %p ---\n", __func__, inm);
2946 in6m_print(const struct in6_multi *inm)