2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2009 Bruce Simpson.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * IPv6 multicast socket, group, and socket option processing module.
34 * Normative references: RFC 2292, RFC 3492, RFC 3542, RFC 3678, RFC 3810.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_inet6.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
53 #include <sys/taskqueue.h>
57 #include <net/if_var.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
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");
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 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, CTLFLAG_RW, 0,
173 static u_long in6_mcast_maxgrpsrc = IPV6_MAX_GROUP_SRC_FILTER;
174 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxgrpsrc,
175 CTLFLAG_RWTUN, &in6_mcast_maxgrpsrc, 0,
176 "Max source filters per group");
178 static u_long in6_mcast_maxsocksrc = IPV6_MAX_SOCK_SRC_FILTER;
179 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxsocksrc,
180 CTLFLAG_RWTUN, &in6_mcast_maxsocksrc, 0,
181 "Max source filters per socket");
183 /* TODO Virtualize this switch. */
184 int in6_mcast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
185 SYSCTL_INT(_net_inet6_ip6_mcast, OID_AUTO, loop, CTLFLAG_RWTUN,
186 &in6_mcast_loop, 0, "Loopback multicast datagrams by default");
188 static SYSCTL_NODE(_net_inet6_ip6_mcast, OID_AUTO, filters,
189 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip6_mcast_filters,
190 "Per-interface stack-wide source filters");
194 * Inline function which wraps assertions for a valid ifp.
195 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
199 in6m_is_ifp_detached(const struct in6_multi *inm)
203 KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__));
204 ifp = inm->in6m_ifma->ifma_ifp;
207 * Sanity check that network-layer notion of ifp is the
208 * same as that of link-layer.
210 KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__));
213 return (ifp == NULL);
218 * Initialize an in6_mfilter structure to a known state at t0, t1
219 * with an empty source filter list.
222 im6f_init(struct in6_mfilter *imf, const int st0, const int st1)
224 memset(imf, 0, sizeof(struct in6_mfilter));
225 RB_INIT(&imf->im6f_sources);
226 imf->im6f_st[0] = st0;
227 imf->im6f_st[1] = st1;
231 ip6_mfilter_alloc(const int mflags, const int st0, const int st1)
233 struct in6_mfilter *imf;
235 imf = malloc(sizeof(*imf), M_IN6MFILTER, mflags);
238 im6f_init(imf, st0, st1);
244 ip6_mfilter_free(struct in6_mfilter *imf)
248 free(imf, M_IN6MFILTER);
252 * Find an IPv6 multicast group entry for this ip6_moptions instance
253 * which matches the specified group, and optionally an interface.
254 * Return its index into the array, or -1 if not found.
256 static struct in6_mfilter *
257 im6o_match_group(const struct ip6_moptions *imo, const struct ifnet *ifp,
258 const struct sockaddr *group)
260 const struct sockaddr_in6 *gsin6;
261 struct in6_mfilter *imf;
262 struct in6_multi *inm;
264 gsin6 = (const struct sockaddr_in6 *)group;
266 IP6_MFILTER_FOREACH(imf, &imo->im6o_head) {
267 inm = imf->im6f_in6m;
270 if ((ifp == NULL || (inm->in6m_ifp == ifp)) &&
271 IN6_ARE_ADDR_EQUAL(&inm->in6m_addr,
272 &gsin6->sin6_addr)) {
280 * Find an IPv6 multicast source entry for this imo which matches
281 * the given group index for this socket, and source address.
283 * XXX TODO: The scope ID, if present in src, is stripped before
284 * any comparison. We SHOULD enforce scope/zone checks where the source
285 * filter entry has a link scope.
287 * NOTE: This does not check if the entry is in-mode, merely if
288 * it exists, which may not be the desired behaviour.
290 static struct in6_msource *
291 im6o_match_source(struct in6_mfilter *imf, const struct sockaddr *src)
293 struct ip6_msource find;
294 struct ip6_msource *ims;
295 const sockunion_t *psa;
297 KASSERT(src->sa_family == AF_INET6, ("%s: !AF_INET6", __func__));
299 psa = (const sockunion_t *)src;
300 find.im6s_addr = psa->sin6.sin6_addr;
301 in6_clearscope(&find.im6s_addr); /* XXX */
302 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
304 return ((struct in6_msource *)ims);
308 * Perform filtering for multicast datagrams on a socket by group and source.
310 * Returns 0 if a datagram should be allowed through, or various error codes
311 * if the socket was not a member of the group, or the source was muted, etc.
314 im6o_mc_filter(const struct ip6_moptions *imo, const struct ifnet *ifp,
315 const struct sockaddr *group, const struct sockaddr *src)
317 struct in6_mfilter *imf;
318 struct in6_msource *ims;
321 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
323 imf = im6o_match_group(imo, ifp, group);
325 return (MCAST_NOTGMEMBER);
328 * Check if the source was included in an (S,G) join.
329 * Allow reception on exclusive memberships by default,
330 * reject reception on inclusive memberships by default.
331 * Exclude source only if an in-mode exclude filter exists.
332 * Include source only if an in-mode include filter exists.
333 * NOTE: We are comparing group state here at MLD t1 (now)
334 * with socket-layer t0 (since last downcall).
336 mode = imf->im6f_st[1];
337 ims = im6o_match_source(imf, src);
339 if ((ims == NULL && mode == MCAST_INCLUDE) ||
340 (ims != NULL && ims->im6sl_st[0] != mode))
341 return (MCAST_NOTSMEMBER);
347 * Find and return a reference to an in6_multi record for (ifp, group),
348 * and bump its reference count.
349 * If one does not exist, try to allocate it, and update link-layer multicast
350 * filters on ifp to listen for group.
351 * Assumes the IN6_MULTI lock is held across the call.
352 * Return 0 if successful, otherwise return an appropriate error code.
355 in6_getmulti(struct ifnet *ifp, const struct in6_addr *group,
356 struct in6_multi **pinm)
358 struct epoch_tracker et;
359 struct sockaddr_in6 gsin6;
360 struct ifmultiaddr *ifma;
361 struct in6_multi *inm;
367 * XXX: Accesses to ifma_protospec must be covered by IF_ADDR_LOCK;
368 * if_addmulti() takes this mutex itself, so we must drop and
369 * re-acquire around the call.
371 IN6_MULTI_LOCK_ASSERT();
372 IN6_MULTI_LIST_LOCK();
374 NET_EPOCH_ENTER_ET(et);
376 * Does ifp support IPv6 multicasts?
378 if (ifp->if_afdata[AF_INET6] == NULL)
381 inm = in6m_lookup_locked(ifp, group);
382 NET_EPOCH_EXIT_ET(et);
389 * If we already joined this group, just bump the
390 * refcount and return it.
392 KASSERT(inm->in6m_refcount >= 1,
393 ("%s: bad refcount %d", __func__, inm->in6m_refcount));
394 in6m_acquire_locked(inm);
399 memset(&gsin6, 0, sizeof(gsin6));
400 gsin6.sin6_family = AF_INET6;
401 gsin6.sin6_len = sizeof(struct sockaddr_in6);
402 gsin6.sin6_addr = *group;
405 * Check if a link-layer group is already associated
406 * with this network-layer group on the given ifnet.
408 IN6_MULTI_LIST_UNLOCK();
409 IF_ADDR_WUNLOCK(ifp);
410 error = if_addmulti(ifp, (struct sockaddr *)&gsin6, &ifma);
413 IN6_MULTI_LIST_LOCK();
417 * If something other than netinet6 is occupying the link-layer
418 * group, print a meaningful error message and back out of
420 * Otherwise, bump the refcount on the existing network-layer
421 * group association and return it.
423 if (ifma->ifma_protospec != NULL) {
424 inm = (struct in6_multi *)ifma->ifma_protospec;
426 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
428 KASSERT(ifma->ifma_addr->sa_family == AF_INET6,
429 ("%s: ifma not AF_INET6", __func__));
430 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
431 if (inm->in6m_ifma != ifma || inm->in6m_ifp != ifp ||
432 !IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, group))
433 panic("%s: ifma %p is inconsistent with %p (%p)",
434 __func__, ifma, inm, group);
436 in6m_acquire_locked(inm);
441 IF_ADDR_WLOCK_ASSERT(ifp);
444 * A new in6_multi record is needed; allocate and initialize it.
445 * We DO NOT perform an MLD join as the in6_ layer may need to
446 * push an initial source list down to MLD to support SSM.
448 * The initial source filter state is INCLUDE, {} as per the RFC.
449 * Pending state-changes per group are subject to a bounds check.
451 inm = malloc(sizeof(*inm), M_IP6MADDR, M_NOWAIT | M_ZERO);
453 IN6_MULTI_LIST_UNLOCK();
454 IF_ADDR_WUNLOCK(ifp);
455 if_delmulti_ifma(ifma);
458 inm->in6m_addr = *group;
460 inm->in6m_mli = MLD_IFINFO(ifp);
461 inm->in6m_ifma = ifma;
462 inm->in6m_refcount = 1;
463 inm->in6m_state = MLD_NOT_MEMBER;
464 mbufq_init(&inm->in6m_scq, MLD_MAX_STATE_CHANGES);
466 inm->in6m_st[0].iss_fmode = MCAST_UNDEFINED;
467 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
468 RB_INIT(&inm->in6m_srcs);
470 ifma->ifma_protospec = inm;
474 IN6_MULTI_LIST_UNLOCK();
475 IF_ADDR_WUNLOCK(ifp);
480 * Drop a reference to an in6_multi record.
482 * If the refcount drops to 0, free the in6_multi record and
483 * delete the underlying link-layer membership.
486 in6m_release(struct in6_multi *inm)
488 struct ifmultiaddr *ifma;
491 CTR2(KTR_MLD, "%s: refcount is %d", __func__, inm->in6m_refcount);
493 MPASS(inm->in6m_refcount == 0);
494 CTR2(KTR_MLD, "%s: freeing inm %p", __func__, inm);
496 ifma = inm->in6m_ifma;
498 MPASS(ifma->ifma_llifma == NULL);
500 /* XXX this access is not covered by IF_ADDR_LOCK */
501 CTR2(KTR_MLD, "%s: purging ifma %p", __func__, ifma);
502 KASSERT(ifma->ifma_protospec == NULL,
503 ("%s: ifma_protospec != NULL", __func__));
505 ifp = ifma->ifma_ifp;
508 CURVNET_SET(ifp->if_vnet);
510 free(inm, M_IP6MADDR);
511 if_delmulti_ifma_flags(ifma, 1);
516 free(inm, M_IP6MADDR);
517 if_delmulti_ifma_flags(ifma, 1);
522 * Interface detach can happen in a taskqueue thread context, so we must use a
523 * dedicated thread to avoid deadlocks when draining in6m_release tasks.
525 TASKQUEUE_DEFINE_THREAD(in6m_free);
526 static struct in6_multi_head in6m_free_list = SLIST_HEAD_INITIALIZER();
527 static void in6m_release_task(void *arg __unused, int pending __unused);
528 static struct task in6m_free_task = TASK_INITIALIZER(0, in6m_release_task, NULL);
531 in6m_release_list_deferred(struct in6_multi_head *inmh)
533 if (SLIST_EMPTY(inmh))
535 mtx_lock(&in6_multi_free_mtx);
536 SLIST_CONCAT(&in6m_free_list, inmh, in6_multi, in6m_nrele);
537 mtx_unlock(&in6_multi_free_mtx);
538 taskqueue_enqueue(taskqueue_in6m_free, &in6m_free_task);
542 in6m_release_wait(void *arg __unused)
546 * Make sure all pending multicast addresses are freed before
547 * the VNET or network device is destroyed:
549 taskqueue_drain_all(taskqueue_in6m_free);
552 VNET_SYSUNINIT(in6m_release_wait, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, in6m_release_wait, NULL);
556 in6m_disconnect_locked(struct in6_multi_head *inmh, struct in6_multi *inm)
560 struct in6_ifaddr *ifa6;
561 struct in6_multi_mship *imm, *imm_tmp;
562 struct ifmultiaddr *ifma, *ll_ifma;
564 IN6_MULTI_LIST_LOCK_ASSERT();
568 return; /* already called */
570 inm->in6m_ifp = NULL;
571 IF_ADDR_WLOCK_ASSERT(ifp);
572 ifma = inm->in6m_ifma;
577 if (ifma->ifma_flags & IFMA_F_ENQUEUED) {
578 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
579 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
581 MCDPRINTF("removed ifma: %p from %s\n", ifma, ifp->if_xname);
582 if ((ll_ifma = ifma->ifma_llifma) != NULL) {
583 MPASS(ifma != ll_ifma);
584 ifma->ifma_llifma = NULL;
585 MPASS(ll_ifma->ifma_llifma == NULL);
586 MPASS(ll_ifma->ifma_ifp == ifp);
587 if (--ll_ifma->ifma_refcount == 0) {
588 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
589 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, ifma_link);
590 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
592 MCDPRINTF("removed ll_ifma: %p from %s\n", ll_ifma, ifp->if_xname);
593 if_freemulti(ll_ifma);
596 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
597 if (ifa->ifa_addr->sa_family != AF_INET6)
600 LIST_FOREACH_SAFE(imm, &ifa6->ia6_memberships,
601 i6mm_chain, imm_tmp) {
602 if (inm == imm->i6mm_maddr) {
603 LIST_REMOVE(imm, i6mm_chain);
604 free(imm, M_IP6MADDR);
605 in6m_rele_locked(inmh, inm);
612 in6m_release_task(void *arg __unused, int pending __unused)
614 struct in6_multi_head in6m_free_tmp;
615 struct in6_multi *inm, *tinm;
617 SLIST_INIT(&in6m_free_tmp);
618 mtx_lock(&in6_multi_free_mtx);
619 SLIST_CONCAT(&in6m_free_tmp, &in6m_free_list, in6_multi, in6m_nrele);
620 mtx_unlock(&in6_multi_free_mtx);
622 SLIST_FOREACH_SAFE(inm, &in6m_free_tmp, in6m_nrele, tinm) {
623 SLIST_REMOVE_HEAD(&in6m_free_tmp, in6m_nrele);
630 * Clear recorded source entries for a group.
631 * Used by the MLD code. Caller must hold the IN6_MULTI lock.
632 * FIXME: Should reap.
635 in6m_clear_recorded(struct in6_multi *inm)
637 struct ip6_msource *ims;
639 IN6_MULTI_LIST_LOCK_ASSERT();
641 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
644 --inm->in6m_st[1].iss_rec;
647 KASSERT(inm->in6m_st[1].iss_rec == 0,
648 ("%s: iss_rec %d not 0", __func__, inm->in6m_st[1].iss_rec));
652 * Record a source as pending for a Source-Group MLDv2 query.
653 * This lives here as it modifies the shared tree.
655 * inm is the group descriptor.
656 * naddr is the address of the source to record in network-byte order.
658 * If the net.inet6.mld.sgalloc sysctl is non-zero, we will
659 * lazy-allocate a source node in response to an SG query.
660 * Otherwise, no allocation is performed. This saves some memory
661 * with the trade-off that the source will not be reported to the
662 * router if joined in the window between the query response and
663 * the group actually being joined on the local host.
665 * VIMAGE: XXX: Currently the mld_sgalloc feature has been removed.
666 * This turns off the allocation of a recorded source entry if
667 * the group has not been joined.
669 * Return 0 if the source didn't exist or was already marked as recorded.
670 * Return 1 if the source was marked as recorded by this function.
671 * Return <0 if any error occurred (negated errno code).
674 in6m_record_source(struct in6_multi *inm, const struct in6_addr *addr)
676 struct ip6_msource find;
677 struct ip6_msource *ims, *nims;
679 IN6_MULTI_LIST_LOCK_ASSERT();
681 find.im6s_addr = *addr;
682 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
683 if (ims && ims->im6s_stp)
686 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
688 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
692 nims->im6s_addr = find.im6s_addr;
693 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
699 * Mark the source as recorded and update the recorded
703 ++inm->in6m_st[1].iss_rec;
709 * Return a pointer to an in6_msource owned by an in6_mfilter,
710 * given its source address.
711 * Lazy-allocate if needed. If this is a new entry its filter state is
714 * imf is the filter set being modified.
715 * addr is the source address.
717 * SMPng: May be called with locks held; malloc must not block.
720 im6f_get_source(struct in6_mfilter *imf, const struct sockaddr_in6 *psin,
721 struct in6_msource **plims)
723 struct ip6_msource find;
724 struct ip6_msource *ims, *nims;
725 struct in6_msource *lims;
732 find.im6s_addr = psin->sin6_addr;
733 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
734 lims = (struct in6_msource *)ims;
736 if (imf->im6f_nsrc == in6_mcast_maxsocksrc)
738 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
742 lims = (struct in6_msource *)nims;
743 lims->im6s_addr = find.im6s_addr;
744 lims->im6sl_st[0] = MCAST_UNDEFINED;
745 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
755 * Graft a source entry into an existing socket-layer filter set,
756 * maintaining any required invariants and checking allocations.
758 * The source is marked as being in the new filter mode at t1.
760 * Return the pointer to the new node, otherwise return NULL.
762 static struct in6_msource *
763 im6f_graft(struct in6_mfilter *imf, const uint8_t st1,
764 const struct sockaddr_in6 *psin)
766 struct ip6_msource *nims;
767 struct in6_msource *lims;
769 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
773 lims = (struct in6_msource *)nims;
774 lims->im6s_addr = psin->sin6_addr;
775 lims->im6sl_st[0] = MCAST_UNDEFINED;
776 lims->im6sl_st[1] = st1;
777 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
784 * Prune a source entry from an existing socket-layer filter set,
785 * maintaining any required invariants and checking allocations.
787 * The source is marked as being left at t1, it is not freed.
789 * Return 0 if no error occurred, otherwise return an errno value.
792 im6f_prune(struct in6_mfilter *imf, const struct sockaddr_in6 *psin)
794 struct ip6_msource find;
795 struct ip6_msource *ims;
796 struct in6_msource *lims;
798 find.im6s_addr = psin->sin6_addr;
799 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
802 lims = (struct in6_msource *)ims;
803 lims->im6sl_st[1] = MCAST_UNDEFINED;
808 * Revert socket-layer filter set deltas at t1 to t0 state.
811 im6f_rollback(struct in6_mfilter *imf)
813 struct ip6_msource *ims, *tims;
814 struct in6_msource *lims;
816 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
817 lims = (struct in6_msource *)ims;
818 if (lims->im6sl_st[0] == lims->im6sl_st[1]) {
819 /* no change at t1 */
821 } else if (lims->im6sl_st[0] != MCAST_UNDEFINED) {
822 /* revert change to existing source at t1 */
823 lims->im6sl_st[1] = lims->im6sl_st[0];
825 /* revert source added t1 */
826 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
827 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
828 free(ims, M_IN6MFILTER);
832 imf->im6f_st[1] = imf->im6f_st[0];
836 * Mark socket-layer filter set as INCLUDE {} at t1.
839 im6f_leave(struct in6_mfilter *imf)
841 struct ip6_msource *ims;
842 struct in6_msource *lims;
844 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
845 lims = (struct in6_msource *)ims;
846 lims->im6sl_st[1] = MCAST_UNDEFINED;
848 imf->im6f_st[1] = MCAST_INCLUDE;
852 * Mark socket-layer filter set deltas as committed.
855 im6f_commit(struct in6_mfilter *imf)
857 struct ip6_msource *ims;
858 struct in6_msource *lims;
860 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
861 lims = (struct in6_msource *)ims;
862 lims->im6sl_st[0] = lims->im6sl_st[1];
864 imf->im6f_st[0] = imf->im6f_st[1];
868 * Reap unreferenced sources from socket-layer filter set.
871 im6f_reap(struct in6_mfilter *imf)
873 struct ip6_msource *ims, *tims;
874 struct in6_msource *lims;
876 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
877 lims = (struct in6_msource *)ims;
878 if ((lims->im6sl_st[0] == MCAST_UNDEFINED) &&
879 (lims->im6sl_st[1] == MCAST_UNDEFINED)) {
880 CTR2(KTR_MLD, "%s: free lims %p", __func__, ims);
881 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
882 free(ims, M_IN6MFILTER);
889 * Purge socket-layer filter set.
892 im6f_purge(struct in6_mfilter *imf)
894 struct ip6_msource *ims, *tims;
896 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
897 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
898 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
899 free(ims, M_IN6MFILTER);
902 imf->im6f_st[0] = imf->im6f_st[1] = MCAST_UNDEFINED;
903 KASSERT(RB_EMPTY(&imf->im6f_sources),
904 ("%s: im6f_sources not empty", __func__));
908 * Look up a source filter entry for a multicast group.
910 * inm is the group descriptor to work with.
911 * addr is the IPv6 address to look up.
912 * noalloc may be non-zero to suppress allocation of sources.
913 * *pims will be set to the address of the retrieved or allocated source.
915 * SMPng: NOTE: may be called with locks held.
916 * Return 0 if successful, otherwise return a non-zero error code.
919 in6m_get_source(struct in6_multi *inm, const struct in6_addr *addr,
920 const int noalloc, struct ip6_msource **pims)
922 struct ip6_msource find;
923 struct ip6_msource *ims, *nims;
925 char ip6tbuf[INET6_ADDRSTRLEN];
928 find.im6s_addr = *addr;
929 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
930 if (ims == NULL && !noalloc) {
931 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
933 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
937 nims->im6s_addr = *addr;
938 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
941 CTR3(KTR_MLD, "%s: allocated %s as %p", __func__,
942 ip6_sprintf(ip6tbuf, addr), ims);
950 * Merge socket-layer source into MLD-layer source.
951 * If rollback is non-zero, perform the inverse of the merge.
954 im6s_merge(struct ip6_msource *ims, const struct in6_msource *lims,
957 int n = rollback ? -1 : 1;
959 char ip6tbuf[INET6_ADDRSTRLEN];
961 ip6_sprintf(ip6tbuf, &lims->im6s_addr);
964 if (lims->im6sl_st[0] == MCAST_EXCLUDE) {
965 CTR3(KTR_MLD, "%s: t1 ex -= %d on %s", __func__, n, ip6tbuf);
966 ims->im6s_st[1].ex -= n;
967 } else if (lims->im6sl_st[0] == MCAST_INCLUDE) {
968 CTR3(KTR_MLD, "%s: t1 in -= %d on %s", __func__, n, ip6tbuf);
969 ims->im6s_st[1].in -= n;
972 if (lims->im6sl_st[1] == MCAST_EXCLUDE) {
973 CTR3(KTR_MLD, "%s: t1 ex += %d on %s", __func__, n, ip6tbuf);
974 ims->im6s_st[1].ex += n;
975 } else if (lims->im6sl_st[1] == MCAST_INCLUDE) {
976 CTR3(KTR_MLD, "%s: t1 in += %d on %s", __func__, n, ip6tbuf);
977 ims->im6s_st[1].in += n;
982 * Atomically update the global in6_multi state, when a membership's
983 * filter list is being updated in any way.
985 * imf is the per-inpcb-membership group filter pointer.
986 * A fake imf may be passed for in-kernel consumers.
988 * XXX This is a candidate for a set-symmetric-difference style loop
989 * which would eliminate the repeated lookup from root of ims nodes,
990 * as they share the same key space.
992 * If any error occurred this function will back out of refcounts
993 * and return a non-zero value.
996 in6m_merge(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
998 struct ip6_msource *ims, *nims;
999 struct in6_msource *lims;
1000 int schanged, error;
1006 IN6_MULTI_LIST_LOCK_ASSERT();
1009 * Update the source filters first, as this may fail.
1010 * Maintain count of in-mode filters at t0, t1. These are
1011 * used to work out if we transition into ASM mode or not.
1012 * Maintain a count of source filters whose state was
1013 * actually modified by this operation.
1015 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
1016 lims = (struct in6_msource *)ims;
1017 if (lims->im6sl_st[0] == imf->im6f_st[0]) nsrc0++;
1018 if (lims->im6sl_st[1] == imf->im6f_st[1]) nsrc1++;
1019 if (lims->im6sl_st[0] == lims->im6sl_st[1]) continue;
1020 error = in6m_get_source(inm, &lims->im6s_addr, 0, &nims);
1024 im6s_merge(nims, lims, 0);
1027 struct ip6_msource *bims;
1029 RB_FOREACH_REVERSE_FROM(ims, ip6_msource_tree, nims) {
1030 lims = (struct in6_msource *)ims;
1031 if (lims->im6sl_st[0] == lims->im6sl_st[1])
1033 (void)in6m_get_source(inm, &lims->im6s_addr, 1, &bims);
1036 im6s_merge(bims, lims, 1);
1041 CTR3(KTR_MLD, "%s: imf filters in-mode: %d at t0, %d at t1",
1042 __func__, nsrc0, nsrc1);
1044 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1045 if (imf->im6f_st[0] == imf->im6f_st[1] &&
1046 imf->im6f_st[1] == MCAST_INCLUDE) {
1048 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
1049 --inm->in6m_st[1].iss_in;
1053 /* Handle filter mode transition on socket. */
1054 if (imf->im6f_st[0] != imf->im6f_st[1]) {
1055 CTR3(KTR_MLD, "%s: imf transition %d to %d",
1056 __func__, imf->im6f_st[0], imf->im6f_st[1]);
1058 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
1059 CTR1(KTR_MLD, "%s: --ex on inm at t1", __func__);
1060 --inm->in6m_st[1].iss_ex;
1061 } else if (imf->im6f_st[0] == MCAST_INCLUDE) {
1062 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
1063 --inm->in6m_st[1].iss_in;
1066 if (imf->im6f_st[1] == MCAST_EXCLUDE) {
1067 CTR1(KTR_MLD, "%s: ex++ on inm at t1", __func__);
1068 inm->in6m_st[1].iss_ex++;
1069 } else if (imf->im6f_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
1070 CTR1(KTR_MLD, "%s: in++ on inm at t1", __func__);
1071 inm->in6m_st[1].iss_in++;
1076 * Track inm filter state in terms of listener counts.
1077 * If there are any exclusive listeners, stack-wide
1078 * membership is exclusive.
1079 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1080 * If no listeners remain, state is undefined at t1,
1081 * and the MLD lifecycle for this group should finish.
1083 if (inm->in6m_st[1].iss_ex > 0) {
1084 CTR1(KTR_MLD, "%s: transition to EX", __func__);
1085 inm->in6m_st[1].iss_fmode = MCAST_EXCLUDE;
1086 } else if (inm->in6m_st[1].iss_in > 0) {
1087 CTR1(KTR_MLD, "%s: transition to IN", __func__);
1088 inm->in6m_st[1].iss_fmode = MCAST_INCLUDE;
1090 CTR1(KTR_MLD, "%s: transition to UNDEF", __func__);
1091 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
1094 /* Decrement ASM listener count on transition out of ASM mode. */
1095 if (imf->im6f_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1096 if ((imf->im6f_st[1] != MCAST_EXCLUDE) ||
1097 (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 > 0)) {
1098 CTR1(KTR_MLD, "%s: --asm on inm at t1", __func__);
1099 --inm->in6m_st[1].iss_asm;
1103 /* Increment ASM listener count on transition to ASM mode. */
1104 if (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1105 CTR1(KTR_MLD, "%s: asm++ on inm at t1", __func__);
1106 inm->in6m_st[1].iss_asm++;
1109 CTR3(KTR_MLD, "%s: merged imf %p to inm %p", __func__, imf, inm);
1114 CTR1(KTR_MLD, "%s: sources changed; reaping", __func__);
1121 * Mark an in6_multi's filter set deltas as committed.
1122 * Called by MLD after a state change has been enqueued.
1125 in6m_commit(struct in6_multi *inm)
1127 struct ip6_msource *ims;
1129 CTR2(KTR_MLD, "%s: commit inm %p", __func__, inm);
1130 CTR1(KTR_MLD, "%s: pre commit:", __func__);
1133 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
1134 ims->im6s_st[0] = ims->im6s_st[1];
1136 inm->in6m_st[0] = inm->in6m_st[1];
1140 * Reap unreferenced nodes from an in6_multi's filter set.
1143 in6m_reap(struct in6_multi *inm)
1145 struct ip6_msource *ims, *tims;
1147 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1148 if (ims->im6s_st[0].ex > 0 || ims->im6s_st[0].in > 0 ||
1149 ims->im6s_st[1].ex > 0 || ims->im6s_st[1].in > 0 ||
1152 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1153 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1154 free(ims, M_IP6MSOURCE);
1160 * Purge all source nodes from an in6_multi's filter set.
1163 in6m_purge(struct in6_multi *inm)
1165 struct ip6_msource *ims, *tims;
1167 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1168 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1169 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1170 free(ims, M_IP6MSOURCE);
1173 /* Free state-change requests that might be queued. */
1174 mbufq_drain(&inm->in6m_scq);
1178 * Join a multicast address w/o sources.
1179 * KAME compatibility entry point.
1181 * SMPng: Assume no mc locks held by caller.
1184 in6_joingroup(struct ifnet *ifp, const struct in6_addr *mcaddr,
1185 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1191 error = in6_joingroup_locked(ifp, mcaddr, NULL, pinm, delay);
1197 * Join a multicast group; real entry point.
1199 * Only preserves atomicity at inm level.
1200 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1202 * If the MLD downcall fails, the group is not joined, and an error
1206 in6_joingroup_locked(struct ifnet *ifp, const struct in6_addr *mcaddr,
1207 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1210 struct in6_multi_head inmh;
1211 struct in6_mfilter timf;
1212 struct in6_multi *inm;
1213 struct ifmultiaddr *ifma;
1216 char ip6tbuf[INET6_ADDRSTRLEN];
1221 * Sanity: Check scope zone ID was set for ifp, if and
1222 * only if group is scoped to an interface.
1224 KASSERT(IN6_IS_ADDR_MULTICAST(mcaddr),
1225 ("%s: not a multicast address", __func__));
1226 if (IN6_IS_ADDR_MC_LINKLOCAL(mcaddr) ||
1227 IN6_IS_ADDR_MC_INTFACELOCAL(mcaddr)) {
1228 KASSERT(mcaddr->s6_addr16[1] != 0,
1229 ("%s: scope zone ID not set", __func__));
1233 IN6_MULTI_LOCK_ASSERT();
1234 IN6_MULTI_LIST_UNLOCK_ASSERT();
1236 CTR4(KTR_MLD, "%s: join %s on %p(%s))", __func__,
1237 ip6_sprintf(ip6tbuf, mcaddr), ifp, if_name(ifp));
1243 * If no imf was specified (i.e. kernel consumer),
1244 * fake one up and assume it is an ASM join.
1247 im6f_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1250 error = in6_getmulti(ifp, mcaddr, &inm);
1252 CTR1(KTR_MLD, "%s: in6_getmulti() failure", __func__);
1256 IN6_MULTI_LIST_LOCK();
1257 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1258 error = in6m_merge(inm, imf);
1260 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1261 goto out_in6m_release;
1264 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1265 error = mld_change_state(inm, delay);
1267 CTR1(KTR_MLD, "%s: failed to update source", __func__);
1268 goto out_in6m_release;
1274 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1276 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1277 if (ifma->ifma_protospec == inm) {
1278 ifma->ifma_protospec = NULL;
1282 in6m_disconnect_locked(&inmh, inm);
1283 in6m_rele_locked(&inmh, inm);
1284 IF_ADDR_RUNLOCK(ifp);
1288 IN6_MULTI_LIST_UNLOCK();
1289 in6m_release_list_deferred(&inmh);
1294 * Leave a multicast group; unlocked entry point.
1297 in6_leavegroup(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1302 error = in6_leavegroup_locked(inm, imf);
1308 * Leave a multicast group; real entry point.
1309 * All source filters will be expunged.
1311 * Only preserves atomicity at inm level.
1313 * Holding the write lock for the INP which contains imf
1314 * is highly advisable. We can't assert for it as imf does not
1315 * contain a back-pointer to the owning inp.
1317 * Note: This is not the same as in6m_release(*) as this function also
1318 * makes a state change downcall into MLD.
1321 in6_leavegroup_locked(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1323 struct in6_multi_head inmh;
1324 struct in6_mfilter timf;
1328 char ip6tbuf[INET6_ADDRSTRLEN];
1333 IN6_MULTI_LOCK_ASSERT();
1335 CTR5(KTR_MLD, "%s: leave inm %p, %s/%s, imf %p", __func__,
1336 inm, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1337 (in6m_is_ifp_detached(inm) ? "null" : if_name(inm->in6m_ifp)),
1341 * If no imf was specified (i.e. kernel consumer),
1342 * fake one up and assume it is an ASM join.
1345 im6f_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1350 * Begin state merge transaction at MLD layer.
1352 * As this particular invocation should not cause any memory
1353 * to be allocated, and there is no opportunity to roll back
1354 * the transaction, it MUST NOT fail.
1357 ifp = inm->in6m_ifp;
1358 IN6_MULTI_LIST_LOCK();
1359 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1360 error = in6m_merge(inm, imf);
1361 KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1363 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1366 error = mld_change_state(inm, 0);
1368 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1370 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1375 if (inm->in6m_refcount == 1)
1376 in6m_disconnect_locked(&inmh, inm);
1377 in6m_rele_locked(&inmh, inm);
1379 IF_ADDR_WUNLOCK(ifp);
1380 IN6_MULTI_LIST_UNLOCK();
1381 in6m_release_list_deferred(&inmh);
1387 * Block or unblock an ASM multicast source on an inpcb.
1388 * This implements the delta-based API described in RFC 3678.
1390 * The delta-based API applies only to exclusive-mode memberships.
1391 * An MLD downcall will be performed.
1393 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1395 * Return 0 if successful, otherwise return an appropriate error code.
1398 in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1400 struct group_source_req gsr;
1401 sockunion_t *gsa, *ssa;
1403 struct in6_mfilter *imf;
1404 struct ip6_moptions *imo;
1405 struct in6_msource *ims;
1406 struct in6_multi *inm;
1410 char ip6tbuf[INET6_ADDRSTRLEN];
1417 memset(&gsr, 0, sizeof(struct group_source_req));
1418 gsa = (sockunion_t *)&gsr.gsr_group;
1419 ssa = (sockunion_t *)&gsr.gsr_source;
1421 switch (sopt->sopt_name) {
1422 case MCAST_BLOCK_SOURCE:
1423 case MCAST_UNBLOCK_SOURCE:
1424 error = sooptcopyin(sopt, &gsr,
1425 sizeof(struct group_source_req),
1426 sizeof(struct group_source_req));
1430 if (gsa->sin6.sin6_family != AF_INET6 ||
1431 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1434 if (ssa->sin6.sin6_family != AF_INET6 ||
1435 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1438 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1439 return (EADDRNOTAVAIL);
1441 ifp = ifnet_byindex(gsr.gsr_interface);
1443 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1448 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1449 __func__, sopt->sopt_name);
1450 return (EOPNOTSUPP);
1454 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1457 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1460 * Check if we are actually a member of this group.
1462 imo = in6p_findmoptions(inp);
1463 imf = im6o_match_group(imo, ifp, &gsa->sa);
1465 error = EADDRNOTAVAIL;
1466 goto out_in6p_locked;
1468 inm = imf->im6f_in6m;
1471 * Attempting to use the delta-based API on an
1472 * non exclusive-mode membership is an error.
1474 fmode = imf->im6f_st[0];
1475 if (fmode != MCAST_EXCLUDE) {
1477 goto out_in6p_locked;
1481 * Deal with error cases up-front:
1482 * Asked to block, but already blocked; or
1483 * Asked to unblock, but nothing to unblock.
1484 * If adding a new block entry, allocate it.
1486 ims = im6o_match_source(imf, &ssa->sa);
1487 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1488 CTR3(KTR_MLD, "%s: source %s %spresent", __func__,
1489 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
1490 doblock ? "" : "not ");
1491 error = EADDRNOTAVAIL;
1492 goto out_in6p_locked;
1495 INP_WLOCK_ASSERT(inp);
1498 * Begin state merge transaction at socket layer.
1501 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
1502 ims = im6f_graft(imf, fmode, &ssa->sin6);
1506 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
1507 error = im6f_prune(imf, &ssa->sin6);
1511 CTR1(KTR_MLD, "%s: merge imf state failed", __func__);
1512 goto out_im6f_rollback;
1516 * Begin state merge transaction at MLD layer.
1518 IN6_MULTI_LIST_LOCK();
1519 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1520 error = in6m_merge(inm, imf);
1522 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1524 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1525 error = mld_change_state(inm, 0);
1527 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1530 IN6_MULTI_LIST_UNLOCK();
1546 * Given an inpcb, return its multicast options structure pointer. Accepts
1547 * an unlocked inpcb pointer, but will return it locked. May sleep.
1549 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1550 * SMPng: NOTE: Returns with the INP write lock held.
1552 static struct ip6_moptions *
1553 in6p_findmoptions(struct inpcb *inp)
1555 struct ip6_moptions *imo;
1558 if (inp->in6p_moptions != NULL)
1559 return (inp->in6p_moptions);
1563 imo = malloc(sizeof(*imo), M_IP6MOPTS, M_WAITOK);
1565 imo->im6o_multicast_ifp = NULL;
1566 imo->im6o_multicast_hlim = V_ip6_defmcasthlim;
1567 imo->im6o_multicast_loop = in6_mcast_loop;
1568 STAILQ_INIT(&imo->im6o_head);
1571 if (inp->in6p_moptions != NULL) {
1572 free(imo, M_IP6MOPTS);
1573 return (inp->in6p_moptions);
1575 inp->in6p_moptions = imo;
1580 * Discard the IPv6 multicast options (and source filters).
1582 * SMPng: NOTE: assumes INP write lock is held.
1584 * XXX can all be safely deferred to epoch_call
1589 inp_gcmoptions(struct ip6_moptions *imo)
1591 struct in6_mfilter *imf;
1592 struct in6_multi *inm;
1595 while ((imf = ip6_mfilter_first(&imo->im6o_head)) != NULL) {
1596 ip6_mfilter_remove(&imo->im6o_head, imf);
1599 if ((inm = imf->im6f_in6m) != NULL) {
1600 if ((ifp = inm->in6m_ifp) != NULL) {
1601 CURVNET_SET(ifp->if_vnet);
1602 (void)in6_leavegroup(inm, imf);
1605 (void)in6_leavegroup(inm, imf);
1608 ip6_mfilter_free(imf);
1610 free(imo, M_IP6MOPTS);
1614 ip6_freemoptions(struct ip6_moptions *imo)
1618 inp_gcmoptions(imo);
1622 * Atomically get source filters on a socket for an IPv6 multicast group.
1623 * Called with INP lock held; returns with lock released.
1626 in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1628 struct __msfilterreq msfr;
1631 struct ip6_moptions *imo;
1632 struct in6_mfilter *imf;
1633 struct ip6_msource *ims;
1634 struct in6_msource *lims;
1635 struct sockaddr_in6 *psin;
1636 struct sockaddr_storage *ptss;
1637 struct sockaddr_storage *tss;
1639 size_t nsrcs, ncsrcs;
1641 INP_WLOCK_ASSERT(inp);
1643 imo = inp->in6p_moptions;
1644 KASSERT(imo != NULL, ("%s: null ip6_moptions", __func__));
1648 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1649 sizeof(struct __msfilterreq));
1653 if (msfr.msfr_group.ss_family != AF_INET6 ||
1654 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
1657 gsa = (sockunion_t *)&msfr.msfr_group;
1658 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1661 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
1662 return (EADDRNOTAVAIL);
1663 ifp = ifnet_byindex(msfr.msfr_ifindex);
1665 return (EADDRNOTAVAIL);
1666 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1671 * Lookup group on the socket.
1673 imf = im6o_match_group(imo, ifp, &gsa->sa);
1676 return (EADDRNOTAVAIL);
1680 * Ignore memberships which are in limbo.
1682 if (imf->im6f_st[1] == MCAST_UNDEFINED) {
1686 msfr.msfr_fmode = imf->im6f_st[1];
1689 * If the user specified a buffer, copy out the source filter
1690 * entries to userland gracefully.
1691 * We only copy out the number of entries which userland
1692 * has asked for, but we always tell userland how big the
1693 * buffer really needs to be.
1695 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
1696 msfr.msfr_nsrcs = in6_mcast_maxsocksrc;
1698 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1699 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1700 M_TEMP, M_NOWAIT | M_ZERO);
1708 * Count number of sources in-mode at t0.
1709 * If buffer space exists and remains, copy out source entries.
1711 nsrcs = msfr.msfr_nsrcs;
1714 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
1715 lims = (struct in6_msource *)ims;
1716 if (lims->im6sl_st[0] == MCAST_UNDEFINED ||
1717 lims->im6sl_st[0] != imf->im6f_st[0])
1720 if (tss != NULL && nsrcs > 0) {
1721 psin = (struct sockaddr_in6 *)ptss;
1722 psin->sin6_family = AF_INET6;
1723 psin->sin6_len = sizeof(struct sockaddr_in6);
1724 psin->sin6_addr = lims->im6s_addr;
1725 psin->sin6_port = 0;
1734 error = copyout(tss, msfr.msfr_srcs,
1735 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1741 msfr.msfr_nsrcs = ncsrcs;
1742 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1748 * Return the IP multicast options in response to user getsockopt().
1751 ip6_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1753 struct ip6_moptions *im6o;
1758 im6o = inp->in6p_moptions;
1760 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1761 * or is a divert socket, reject it.
1763 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1764 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1765 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
1767 return (EOPNOTSUPP);
1771 switch (sopt->sopt_name) {
1772 case IPV6_MULTICAST_IF:
1773 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) {
1776 optval = im6o->im6o_multicast_ifp->if_index;
1779 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1782 case IPV6_MULTICAST_HOPS:
1784 optval = V_ip6_defmcasthlim;
1786 optval = im6o->im6o_multicast_hlim;
1788 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1791 case IPV6_MULTICAST_LOOP:
1793 optval = in6_mcast_loop; /* XXX VIMAGE */
1795 optval = im6o->im6o_multicast_loop;
1797 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1802 error = EADDRNOTAVAIL;
1805 error = in6p_get_source_filters(inp, sopt);
1811 error = ENOPROTOOPT;
1815 INP_UNLOCK_ASSERT(inp);
1821 * Look up the ifnet to use for a multicast group membership,
1822 * given the address of an IPv6 group.
1824 * This routine exists to support legacy IPv6 multicast applications.
1826 * Use the socket's current FIB number for any required FIB lookup. Look up the
1827 * group address in the unicast FIB, and use its ifp; usually, this points to
1828 * the default next-hop. If the FIB lookup fails, return NULL.
1830 * FUTURE: Support multiple forwarding tables for IPv6.
1832 * Returns NULL if no ifp could be found.
1834 static struct ifnet *
1835 in6p_lookup_mcast_ifp(const struct inpcb *inp, const struct sockaddr_in6 *gsin6)
1837 struct nhop6_basic nh6;
1838 struct in6_addr dst;
1842 KASSERT(gsin6->sin6_family == AF_INET6,
1843 ("%s: not AF_INET6 group", __func__));
1845 in6_splitscope(&gsin6->sin6_addr, &dst, &scopeid);
1846 fibnum = inp->inp_inc.inc_fibnum;
1847 if (fib6_lookup_nh_basic(fibnum, &dst, scopeid, 0, 0, &nh6) != 0)
1850 return (nh6.nh_ifp);
1854 * Join an IPv6 multicast group, possibly with a source.
1856 * FIXME: The KAME use of the unspecified address (::)
1857 * to join *all* multicast groups is currently unsupported.
1860 in6p_join_group(struct inpcb *inp, struct sockopt *sopt)
1862 struct in6_multi_head inmh;
1863 struct group_source_req gsr;
1864 sockunion_t *gsa, *ssa;
1866 struct in6_mfilter *imf;
1867 struct ip6_moptions *imo;
1868 struct in6_multi *inm;
1869 struct in6_msource *lims;
1877 memset(&gsr, 0, sizeof(struct group_source_req));
1878 gsa = (sockunion_t *)&gsr.gsr_group;
1879 gsa->ss.ss_family = AF_UNSPEC;
1880 ssa = (sockunion_t *)&gsr.gsr_source;
1881 ssa->ss.ss_family = AF_UNSPEC;
1884 * Chew everything into struct group_source_req.
1885 * Overwrite the port field if present, as the sockaddr
1886 * being copied in may be matched with a binary comparison.
1887 * Ignore passed-in scope ID.
1889 switch (sopt->sopt_name) {
1890 case IPV6_JOIN_GROUP: {
1891 struct ipv6_mreq mreq;
1893 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
1894 sizeof(struct ipv6_mreq));
1898 gsa->sin6.sin6_family = AF_INET6;
1899 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
1900 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
1902 if (mreq.ipv6mr_interface == 0) {
1903 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
1905 if (V_if_index < mreq.ipv6mr_interface)
1906 return (EADDRNOTAVAIL);
1907 ifp = ifnet_byindex(mreq.ipv6mr_interface);
1909 CTR3(KTR_MLD, "%s: ipv6mr_interface = %d, ifp = %p",
1910 __func__, mreq.ipv6mr_interface, ifp);
1913 case MCAST_JOIN_GROUP:
1914 case MCAST_JOIN_SOURCE_GROUP:
1915 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1916 error = sooptcopyin(sopt, &gsr,
1917 sizeof(struct group_req),
1918 sizeof(struct group_req));
1919 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1920 error = sooptcopyin(sopt, &gsr,
1921 sizeof(struct group_source_req),
1922 sizeof(struct group_source_req));
1927 if (gsa->sin6.sin6_family != AF_INET6 ||
1928 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1931 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1932 if (ssa->sin6.sin6_family != AF_INET6 ||
1933 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1935 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
1938 * TODO: Validate embedded scope ID in source
1939 * list entry against passed-in ifp, if and only
1940 * if source list filter entry is iface or node local.
1942 in6_clearscope(&ssa->sin6.sin6_addr);
1943 ssa->sin6.sin6_port = 0;
1944 ssa->sin6.sin6_scope_id = 0;
1947 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1948 return (EADDRNOTAVAIL);
1949 ifp = ifnet_byindex(gsr.gsr_interface);
1953 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1954 __func__, sopt->sopt_name);
1955 return (EOPNOTSUPP);
1959 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1962 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
1963 return (EADDRNOTAVAIL);
1965 gsa->sin6.sin6_port = 0;
1966 gsa->sin6.sin6_scope_id = 0;
1969 * Always set the scope zone ID on memberships created from userland.
1970 * Use the passed-in ifp to do this.
1971 * XXX The in6_setscope() return value is meaningless.
1972 * XXX SCOPE6_LOCK() is taken by in6_setscope().
1974 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1979 * Find the membership in the membership list.
1981 imo = in6p_findmoptions(inp);
1982 imf = im6o_match_group(imo, ifp, &gsa->sa);
1987 if (ip6_mfilter_count(&imo->im6o_head) >= IPV6_MAX_MEMBERSHIPS) {
1989 goto out_in6p_locked;
1993 inm = imf->im6f_in6m;
1995 if (ssa->ss.ss_family != AF_UNSPEC) {
1997 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
1998 * is an error. On an existing inclusive membership,
1999 * it just adds the source to the filter list.
2001 if (imf->im6f_st[1] != MCAST_INCLUDE) {
2003 goto out_in6p_locked;
2006 * Throw out duplicates.
2008 * XXX FIXME: This makes a naive assumption that
2009 * even if entries exist for *ssa in this imf,
2010 * they will be rejected as dupes, even if they
2011 * are not valid in the current mode (in-mode).
2013 * in6_msource is transactioned just as for anything
2014 * else in SSM -- but note naive use of in6m_graft()
2015 * below for allocating new filter entries.
2017 * This is only an issue if someone mixes the
2018 * full-state SSM API with the delta-based API,
2019 * which is discouraged in the relevant RFCs.
2021 lims = im6o_match_source(imf, &ssa->sa);
2022 if (lims != NULL /*&&
2023 lims->im6sl_st[1] == MCAST_INCLUDE*/) {
2024 error = EADDRNOTAVAIL;
2025 goto out_in6p_locked;
2029 * MCAST_JOIN_GROUP alone, on any existing membership,
2030 * is rejected, to stop the same inpcb tying up
2031 * multiple refs to the in_multi.
2032 * On an existing inclusive membership, this is also
2033 * an error; if you want to change filter mode,
2034 * you must use the userland API setsourcefilter().
2035 * XXX We don't reject this for imf in UNDEFINED
2036 * state at t1, because allocation of a filter
2037 * is atomic with allocation of a membership.
2040 goto out_in6p_locked;
2045 * Begin state merge transaction at socket layer.
2047 INP_WLOCK_ASSERT(inp);
2050 * Graft new source into filter list for this inpcb's
2051 * membership of the group. The in6_multi may not have
2052 * been allocated yet if this is a new membership, however,
2053 * the in_mfilter slot will be allocated and must be initialized.
2055 * Note: Grafting of exclusive mode filters doesn't happen
2057 * XXX: Should check for non-NULL lims (node exists but may
2058 * not be in-mode) for interop with full-state API.
2060 if (ssa->ss.ss_family != AF_UNSPEC) {
2061 /* Membership starts in IN mode */
2063 CTR1(KTR_MLD, "%s: new join w/source", __func__);
2064 imf = ip6_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_INCLUDE);
2067 goto out_in6p_locked;
2070 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
2072 lims = im6f_graft(imf, MCAST_INCLUDE, &ssa->sin6);
2074 CTR1(KTR_MLD, "%s: merge imf state failed",
2077 goto out_in6p_locked;
2080 /* No address specified; Membership starts in EX mode */
2082 CTR1(KTR_MLD, "%s: new join w/o source", __func__);
2083 imf = ip6_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_EXCLUDE);
2086 goto out_in6p_locked;
2092 * Begin state merge transaction at MLD layer.
2098 error = in6_joingroup_locked(ifp, &gsa->sin6.sin6_addr, imf,
2099 &imf->im6f_in6m, 0);
2102 if (in_pcbrele_wlocked(inp)) {
2104 goto out_in6p_unlocked;
2107 goto out_in6p_locked;
2110 * NOTE: Refcount from in6_joingroup_locked()
2111 * is protecting membership.
2113 ip6_mfilter_insert(&imo->im6o_head, imf);
2115 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2116 IN6_MULTI_LIST_LOCK();
2117 error = in6m_merge(inm, imf);
2119 CTR1(KTR_MLD, "%s: failed to merge inm state",
2121 IN6_MULTI_LIST_UNLOCK();
2124 goto out_in6p_locked;
2126 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2127 error = mld_change_state(inm, 0);
2128 IN6_MULTI_LIST_UNLOCK();
2131 CTR1(KTR_MLD, "%s: failed mld downcall",
2135 goto out_in6p_locked;
2147 if (is_new && imf) {
2148 if (imf->im6f_in6m != NULL) {
2149 struct in6_multi_head inmh;
2152 SLIST_INSERT_HEAD(&inmh, imf->im6f_in6m, in6m_defer);
2153 in6m_release_list_deferred(&inmh);
2155 ip6_mfilter_free(imf);
2161 * Leave an IPv6 multicast group on an inpcb, possibly with a source.
2164 in6p_leave_group(struct inpcb *inp, struct sockopt *sopt)
2166 struct ipv6_mreq mreq;
2167 struct group_source_req gsr;
2168 sockunion_t *gsa, *ssa;
2170 struct in6_mfilter *imf;
2171 struct ip6_moptions *imo;
2172 struct in6_msource *ims;
2173 struct in6_multi *inm;
2178 char ip6tbuf[INET6_ADDRSTRLEN];
2186 memset(&gsr, 0, sizeof(struct group_source_req));
2187 gsa = (sockunion_t *)&gsr.gsr_group;
2188 gsa->ss.ss_family = AF_UNSPEC;
2189 ssa = (sockunion_t *)&gsr.gsr_source;
2190 ssa->ss.ss_family = AF_UNSPEC;
2193 * Chew everything passed in up into a struct group_source_req
2194 * as that is easier to process.
2195 * Note: Any embedded scope ID in the multicast group passed
2196 * in by userland is ignored, the interface index is the recommended
2197 * mechanism to specify an interface; see below.
2199 switch (sopt->sopt_name) {
2200 case IPV6_LEAVE_GROUP:
2201 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
2202 sizeof(struct ipv6_mreq));
2205 gsa->sin6.sin6_family = AF_INET6;
2206 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
2207 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
2208 gsa->sin6.sin6_port = 0;
2209 gsa->sin6.sin6_scope_id = 0;
2210 ifindex = mreq.ipv6mr_interface;
2213 case MCAST_LEAVE_GROUP:
2214 case MCAST_LEAVE_SOURCE_GROUP:
2215 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2216 error = sooptcopyin(sopt, &gsr,
2217 sizeof(struct group_req),
2218 sizeof(struct group_req));
2219 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2220 error = sooptcopyin(sopt, &gsr,
2221 sizeof(struct group_source_req),
2222 sizeof(struct group_source_req));
2227 if (gsa->sin6.sin6_family != AF_INET6 ||
2228 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2230 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2231 if (ssa->sin6.sin6_family != AF_INET6 ||
2232 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2234 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
2237 * TODO: Validate embedded scope ID in source
2238 * list entry against passed-in ifp, if and only
2239 * if source list filter entry is iface or node local.
2241 in6_clearscope(&ssa->sin6.sin6_addr);
2243 gsa->sin6.sin6_port = 0;
2244 gsa->sin6.sin6_scope_id = 0;
2245 ifindex = gsr.gsr_interface;
2249 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
2250 __func__, sopt->sopt_name);
2251 return (EOPNOTSUPP);
2255 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2259 * Validate interface index if provided. If no interface index
2260 * was provided separately, attempt to look the membership up
2261 * from the default scope as a last resort to disambiguate
2262 * the membership we are being asked to leave.
2263 * XXX SCOPE6 lock potentially taken here.
2266 if (V_if_index < ifindex)
2267 return (EADDRNOTAVAIL);
2268 ifp = ifnet_byindex(ifindex);
2270 return (EADDRNOTAVAIL);
2271 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2273 error = sa6_embedscope(&gsa->sin6, V_ip6_use_defzone);
2275 return (EADDRNOTAVAIL);
2277 * Some badly behaved applications don't pass an ifindex
2278 * or a scope ID, which is an API violation. In this case,
2279 * perform a lookup as per a v6 join.
2281 * XXX For now, stomp on zone ID for the corner case.
2282 * This is not the 'KAME way', but we need to see the ifp
2283 * directly until such time as this implementation is
2284 * refactored, assuming the scope IDs are the way to go.
2286 ifindex = ntohs(gsa->sin6.sin6_addr.s6_addr16[1]);
2288 CTR2(KTR_MLD, "%s: warning: no ifindex, looking up "
2289 "ifp for group %s.", __func__,
2290 ip6_sprintf(ip6tbuf, &gsa->sin6.sin6_addr));
2291 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
2293 ifp = ifnet_byindex(ifindex);
2296 return (EADDRNOTAVAIL);
2299 CTR2(KTR_MLD, "%s: ifp = %p", __func__, ifp);
2300 KASSERT(ifp != NULL, ("%s: ifp did not resolve", __func__));
2305 * Find the membership in the membership list.
2307 imo = in6p_findmoptions(inp);
2308 imf = im6o_match_group(imo, ifp, &gsa->sa);
2310 error = EADDRNOTAVAIL;
2311 goto out_in6p_locked;
2313 inm = imf->im6f_in6m;
2315 if (ssa->ss.ss_family != AF_UNSPEC)
2319 * Begin state merge transaction at socket layer.
2321 INP_WLOCK_ASSERT(inp);
2324 * If we were instructed only to leave a given source, do so.
2325 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2328 ip6_mfilter_remove(&imo->im6o_head, imf);
2332 * Give up the multicast address record to which
2333 * the membership points.
2335 (void)in6_leavegroup_locked(inm, imf);
2337 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
2338 error = EADDRNOTAVAIL;
2339 goto out_in6p_locked;
2341 ims = im6o_match_source(imf, &ssa->sa);
2343 CTR3(KTR_MLD, "%s: source %p %spresent", __func__,
2344 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
2346 error = EADDRNOTAVAIL;
2347 goto out_in6p_locked;
2349 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
2350 error = im6f_prune(imf, &ssa->sin6);
2352 CTR1(KTR_MLD, "%s: merge imf state failed",
2354 goto out_in6p_locked;
2359 * Begin state merge transaction at MLD layer.
2362 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2363 IN6_MULTI_LIST_LOCK();
2364 error = in6m_merge(inm, imf);
2366 CTR1(KTR_MLD, "%s: failed to merge inm state",
2368 IN6_MULTI_LIST_UNLOCK();
2371 goto out_in6p_locked;
2374 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2375 error = mld_change_state(inm, 0);
2376 IN6_MULTI_LIST_UNLOCK();
2378 CTR1(KTR_MLD, "%s: failed mld downcall",
2382 goto out_in6p_locked;
2392 if (is_final && imf)
2393 ip6_mfilter_free(imf);
2400 * Select the interface for transmitting IPv6 multicast datagrams.
2402 * Either an instance of struct in6_addr or an instance of struct ipv6_mreqn
2403 * may be passed to this socket option. An address of in6addr_any or an
2404 * interface index of 0 is used to remove a previous selection.
2405 * When no interface is selected, one is chosen for every send.
2408 in6p_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2411 struct ip6_moptions *imo;
2415 if (sopt->sopt_valsize != sizeof(u_int))
2418 error = sooptcopyin(sopt, &ifindex, sizeof(u_int), sizeof(u_int));
2421 if (V_if_index < ifindex)
2426 ifp = ifnet_byindex(ifindex);
2429 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2430 return (EADDRNOTAVAIL);
2432 imo = in6p_findmoptions(inp);
2433 imo->im6o_multicast_ifp = ifp;
2440 * Atomically set source filters on a socket for an IPv6 multicast group.
2442 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
2445 in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2447 struct __msfilterreq msfr;
2450 struct in6_mfilter *imf;
2451 struct ip6_moptions *imo;
2452 struct in6_multi *inm;
2455 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2456 sizeof(struct __msfilterreq));
2460 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
2463 if (msfr.msfr_fmode != MCAST_EXCLUDE &&
2464 msfr.msfr_fmode != MCAST_INCLUDE)
2467 if (msfr.msfr_group.ss_family != AF_INET6 ||
2468 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
2471 gsa = (sockunion_t *)&msfr.msfr_group;
2472 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2475 gsa->sin6.sin6_port = 0; /* ignore port */
2477 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
2478 return (EADDRNOTAVAIL);
2479 ifp = ifnet_byindex(msfr.msfr_ifindex);
2481 return (EADDRNOTAVAIL);
2482 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2485 * Take the INP write lock.
2486 * Check if this socket is a member of this group.
2488 imo = in6p_findmoptions(inp);
2489 imf = im6o_match_group(imo, ifp, &gsa->sa);
2491 error = EADDRNOTAVAIL;
2492 goto out_in6p_locked;
2494 inm = imf->im6f_in6m;
2497 * Begin state merge transaction at socket layer.
2499 INP_WLOCK_ASSERT(inp);
2501 imf->im6f_st[1] = msfr.msfr_fmode;
2504 * Apply any new source filters, if present.
2505 * Make a copy of the user-space source vector so
2506 * that we may copy them with a single copyin. This
2507 * allows us to deal with page faults up-front.
2509 if (msfr.msfr_nsrcs > 0) {
2510 struct in6_msource *lims;
2511 struct sockaddr_in6 *psin;
2512 struct sockaddr_storage *kss, *pkss;
2517 CTR2(KTR_MLD, "%s: loading %lu source list entries",
2518 __func__, (unsigned long)msfr.msfr_nsrcs);
2519 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2521 error = copyin(msfr.msfr_srcs, kss,
2522 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2531 * Mark all source filters as UNDEFINED at t1.
2532 * Restore new group filter mode, as im6f_leave()
2533 * will set it to INCLUDE.
2536 imf->im6f_st[1] = msfr.msfr_fmode;
2539 * Update socket layer filters at t1, lazy-allocating
2540 * new entries. This saves a bunch of memory at the
2541 * cost of one RB_FIND() per source entry; duplicate
2542 * entries in the msfr_nsrcs vector are ignored.
2543 * If we encounter an error, rollback transaction.
2545 * XXX This too could be replaced with a set-symmetric
2546 * difference like loop to avoid walking from root
2547 * every time, as the key space is common.
2549 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2550 psin = (struct sockaddr_in6 *)pkss;
2551 if (psin->sin6_family != AF_INET6) {
2552 error = EAFNOSUPPORT;
2555 if (psin->sin6_len != sizeof(struct sockaddr_in6)) {
2559 if (IN6_IS_ADDR_MULTICAST(&psin->sin6_addr)) {
2564 * TODO: Validate embedded scope ID in source
2565 * list entry against passed-in ifp, if and only
2566 * if source list filter entry is iface or node local.
2568 in6_clearscope(&psin->sin6_addr);
2569 error = im6f_get_source(imf, psin, &lims);
2572 lims->im6sl_st[1] = imf->im6f_st[1];
2578 goto out_im6f_rollback;
2580 INP_WLOCK_ASSERT(inp);
2581 IN6_MULTI_LIST_LOCK();
2584 * Begin state merge transaction at MLD layer.
2586 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2587 error = in6m_merge(inm, imf);
2589 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
2591 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2592 error = mld_change_state(inm, 0);
2594 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
2597 IN6_MULTI_LIST_UNLOCK();
2613 * Set the IP multicast options in response to user setsockopt().
2615 * Many of the socket options handled in this function duplicate the
2616 * functionality of socket options in the regular unicast API. However,
2617 * it is not possible to merge the duplicate code, because the idempotence
2618 * of the IPv6 multicast part of the BSD Sockets API must be preserved;
2619 * the effects of these options must be treated as separate and distinct.
2621 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2624 ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2626 struct ip6_moptions *im6o;
2632 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2633 * or is a divert socket, reject it.
2635 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
2636 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2637 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
2638 return (EOPNOTSUPP);
2640 switch (sopt->sopt_name) {
2641 case IPV6_MULTICAST_IF:
2642 error = in6p_set_multicast_if(inp, sopt);
2645 case IPV6_MULTICAST_HOPS: {
2648 if (sopt->sopt_valsize != sizeof(int)) {
2652 error = sooptcopyin(sopt, &hlim, sizeof(hlim), sizeof(int));
2655 if (hlim < -1 || hlim > 255) {
2658 } else if (hlim == -1) {
2659 hlim = V_ip6_defmcasthlim;
2661 im6o = in6p_findmoptions(inp);
2662 im6o->im6o_multicast_hlim = hlim;
2667 case IPV6_MULTICAST_LOOP: {
2671 * Set the loopback flag for outgoing multicast packets.
2672 * Must be zero or one.
2674 if (sopt->sopt_valsize != sizeof(u_int)) {
2678 error = sooptcopyin(sopt, &loop, sizeof(u_int), sizeof(u_int));
2685 im6o = in6p_findmoptions(inp);
2686 im6o->im6o_multicast_loop = loop;
2691 case IPV6_JOIN_GROUP:
2692 case MCAST_JOIN_GROUP:
2693 case MCAST_JOIN_SOURCE_GROUP:
2694 error = in6p_join_group(inp, sopt);
2697 case IPV6_LEAVE_GROUP:
2698 case MCAST_LEAVE_GROUP:
2699 case MCAST_LEAVE_SOURCE_GROUP:
2700 error = in6p_leave_group(inp, sopt);
2703 case MCAST_BLOCK_SOURCE:
2704 case MCAST_UNBLOCK_SOURCE:
2705 error = in6p_block_unblock_source(inp, sopt);
2709 error = in6p_set_source_filters(inp, sopt);
2717 INP_UNLOCK_ASSERT(inp);
2723 * Expose MLD's multicast filter mode and source list(s) to userland,
2724 * keyed by (ifindex, group).
2725 * The filter mode is written out as a uint32_t, followed by
2726 * 0..n of struct in6_addr.
2727 * For use by ifmcstat(8).
2728 * SMPng: NOTE: unlocked read of ifindex space.
2731 sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS)
2733 struct in6_addr mcaddr;
2734 struct in6_addr src;
2736 struct ifmultiaddr *ifma;
2737 struct in6_multi *inm;
2738 struct ip6_msource *ims;
2742 uint32_t fmode, ifindex;
2744 char ip6tbuf[INET6_ADDRSTRLEN];
2750 if (req->newptr != NULL)
2753 /* int: ifindex + 4 * 32 bits of IPv6 address */
2758 if (ifindex <= 0 || ifindex > V_if_index) {
2759 CTR2(KTR_MLD, "%s: ifindex %u out of range",
2764 memcpy(&mcaddr, &name[1], sizeof(struct in6_addr));
2765 if (!IN6_IS_ADDR_MULTICAST(&mcaddr)) {
2766 CTR2(KTR_MLD, "%s: group %s is not multicast",
2767 __func__, ip6_sprintf(ip6tbuf, &mcaddr));
2771 ifp = ifnet_byindex(ifindex);
2773 CTR2(KTR_MLD, "%s: no ifp for ifindex %u",
2778 * Internal MLD lookups require that scope/zone ID is set.
2780 (void)in6_setscope(&mcaddr, ifp, NULL);
2782 retval = sysctl_wire_old_buffer(req,
2783 sizeof(uint32_t) + (in6_mcast_maxgrpsrc * sizeof(struct in6_addr)));
2788 IN6_MULTI_LIST_LOCK();
2790 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2791 inm = in6m_ifmultiaddr_get_inm(ifma);
2794 if (!IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, &mcaddr))
2796 fmode = inm->in6m_st[1].iss_fmode;
2797 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2800 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
2801 CTR2(KTR_MLD, "%s: visit node %p", __func__, ims);
2803 * Only copy-out sources which are in-mode.
2805 if (fmode != im6s_get_mode(inm, ims, 1)) {
2806 CTR1(KTR_MLD, "%s: skip non-in-mode",
2810 src = ims->im6s_addr;
2811 retval = SYSCTL_OUT(req, &src,
2812 sizeof(struct in6_addr));
2817 IF_ADDR_RUNLOCK(ifp);
2819 IN6_MULTI_LIST_UNLOCK();
2827 static const char *in6m_modestrs[] = { "un", "in", "ex" };
2830 in6m_mode_str(const int mode)
2833 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2834 return (in6m_modestrs[mode]);
2838 static const char *in6m_statestrs[] = {
2851 in6m_state_str(const int state)
2854 if (state >= MLD_NOT_MEMBER && state <= MLD_LEAVING_MEMBER)
2855 return (in6m_statestrs[state]);
2860 * Dump an in6_multi structure to the console.
2863 in6m_print(const struct in6_multi *inm)
2866 char ip6tbuf[INET6_ADDRSTRLEN];
2868 if ((ktr_mask & KTR_MLD) == 0)
2871 printf("%s: --- begin in6m %p ---\n", __func__, inm);
2872 printf("addr %s ifp %p(%s) ifma %p\n",
2873 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2875 if_name(inm->in6m_ifp),
2877 printf("timer %u state %s refcount %u scq.len %u\n",
2879 in6m_state_str(inm->in6m_state),
2881 mbufq_len(&inm->in6m_scq));
2882 printf("mli %p nsrc %lu sctimer %u scrv %u\n",
2887 for (t = 0; t < 2; t++) {
2888 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
2889 in6m_mode_str(inm->in6m_st[t].iss_fmode),
2890 inm->in6m_st[t].iss_asm,
2891 inm->in6m_st[t].iss_ex,
2892 inm->in6m_st[t].iss_in,
2893 inm->in6m_st[t].iss_rec);
2895 printf("%s: --- end in6m %p ---\n", __func__, inm);
2901 in6m_print(const struct in6_multi *inm)