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);
375 inm = in6m_lookup_locked(ifp, group);
376 NET_EPOCH_EXIT_ET(et);
380 * If we already joined this group, just bump the
381 * refcount and return it.
383 KASSERT(inm->in6m_refcount >= 1,
384 ("%s: bad refcount %d", __func__, inm->in6m_refcount));
385 in6m_acquire_locked(inm);
390 memset(&gsin6, 0, sizeof(gsin6));
391 gsin6.sin6_family = AF_INET6;
392 gsin6.sin6_len = sizeof(struct sockaddr_in6);
393 gsin6.sin6_addr = *group;
396 * Check if a link-layer group is already associated
397 * with this network-layer group on the given ifnet.
399 IN6_MULTI_LIST_UNLOCK();
400 IF_ADDR_WUNLOCK(ifp);
401 error = if_addmulti(ifp, (struct sockaddr *)&gsin6, &ifma);
404 IN6_MULTI_LIST_LOCK();
408 * If something other than netinet6 is occupying the link-layer
409 * group, print a meaningful error message and back out of
411 * Otherwise, bump the refcount on the existing network-layer
412 * group association and return it.
414 if (ifma->ifma_protospec != NULL) {
415 inm = (struct in6_multi *)ifma->ifma_protospec;
417 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
419 KASSERT(ifma->ifma_addr->sa_family == AF_INET6,
420 ("%s: ifma not AF_INET6", __func__));
421 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
422 if (inm->in6m_ifma != ifma || inm->in6m_ifp != ifp ||
423 !IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, group))
424 panic("%s: ifma %p is inconsistent with %p (%p)",
425 __func__, ifma, inm, group);
427 in6m_acquire_locked(inm);
432 IF_ADDR_WLOCK_ASSERT(ifp);
435 * A new in6_multi record is needed; allocate and initialize it.
436 * We DO NOT perform an MLD join as the in6_ layer may need to
437 * push an initial source list down to MLD to support SSM.
439 * The initial source filter state is INCLUDE, {} as per the RFC.
440 * Pending state-changes per group are subject to a bounds check.
442 inm = malloc(sizeof(*inm), M_IP6MADDR, M_NOWAIT | M_ZERO);
444 IN6_MULTI_LIST_UNLOCK();
445 IF_ADDR_WUNLOCK(ifp);
446 if_delmulti_ifma(ifma);
449 inm->in6m_addr = *group;
451 inm->in6m_mli = MLD_IFINFO(ifp);
452 inm->in6m_ifma = ifma;
453 inm->in6m_refcount = 1;
454 inm->in6m_state = MLD_NOT_MEMBER;
455 mbufq_init(&inm->in6m_scq, MLD_MAX_STATE_CHANGES);
457 inm->in6m_st[0].iss_fmode = MCAST_UNDEFINED;
458 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
459 RB_INIT(&inm->in6m_srcs);
461 ifma->ifma_protospec = inm;
465 IN6_MULTI_LIST_UNLOCK();
466 IF_ADDR_WUNLOCK(ifp);
471 * Drop a reference to an in6_multi record.
473 * If the refcount drops to 0, free the in6_multi record and
474 * delete the underlying link-layer membership.
477 in6m_release(struct in6_multi *inm)
479 struct ifmultiaddr *ifma;
482 CTR2(KTR_MLD, "%s: refcount is %d", __func__, inm->in6m_refcount);
484 MPASS(inm->in6m_refcount == 0);
485 CTR2(KTR_MLD, "%s: freeing inm %p", __func__, inm);
487 ifma = inm->in6m_ifma;
489 MPASS(ifma->ifma_llifma == NULL);
491 /* XXX this access is not covered by IF_ADDR_LOCK */
492 CTR2(KTR_MLD, "%s: purging ifma %p", __func__, ifma);
493 KASSERT(ifma->ifma_protospec == NULL,
494 ("%s: ifma_protospec != NULL", __func__));
496 ifp = ifma->ifma_ifp;
499 CURVNET_SET(ifp->if_vnet);
501 free(inm, M_IP6MADDR);
502 if_delmulti_ifma_flags(ifma, 1);
507 free(inm, M_IP6MADDR);
508 if_delmulti_ifma_flags(ifma, 1);
513 * Interface detach can happen in a taskqueue thread context, so we must use a
514 * dedicated thread to avoid deadlocks when draining in6m_release tasks.
516 TASKQUEUE_DEFINE_THREAD(in6m_free);
517 static struct task in6m_free_task;
518 static struct in6_multi_head in6m_free_list = SLIST_HEAD_INITIALIZER();
519 static void in6m_release_task(void *arg __unused, int pending __unused);
524 TASK_INIT(&in6m_free_task, 0, in6m_release_task, NULL);
526 SYSINIT(in6m_init, SI_SUB_TASKQ, SI_ORDER_ANY, in6m_init, NULL);
529 in6m_release_list_deferred(struct in6_multi_head *inmh)
531 if (SLIST_EMPTY(inmh))
533 mtx_lock(&in6_multi_free_mtx);
534 SLIST_CONCAT(&in6m_free_list, inmh, in6_multi, in6m_nrele);
535 mtx_unlock(&in6_multi_free_mtx);
536 taskqueue_enqueue(taskqueue_in6m_free, &in6m_free_task);
540 in6m_release_wait(void)
542 taskqueue_drain_all(taskqueue_in6m_free);
546 in6m_disconnect_locked(struct in6_multi_head *inmh, struct in6_multi *inm)
550 struct in6_ifaddr *ifa6;
551 struct in6_multi_mship *imm, *imm_tmp;
552 struct ifmultiaddr *ifma, *ll_ifma;
554 IN6_MULTI_LIST_LOCK_ASSERT();
558 return; /* already called */
560 inm->in6m_ifp = NULL;
561 IF_ADDR_WLOCK_ASSERT(ifp);
562 ifma = inm->in6m_ifma;
567 if (ifma->ifma_flags & IFMA_F_ENQUEUED) {
568 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
569 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
571 MCDPRINTF("removed ifma: %p from %s\n", ifma, ifp->if_xname);
572 if ((ll_ifma = ifma->ifma_llifma) != NULL) {
573 MPASS(ifma != ll_ifma);
574 ifma->ifma_llifma = NULL;
575 MPASS(ll_ifma->ifma_llifma == NULL);
576 MPASS(ll_ifma->ifma_ifp == ifp);
577 if (--ll_ifma->ifma_refcount == 0) {
578 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
579 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, ifma_link);
580 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
582 MCDPRINTF("removed ll_ifma: %p from %s\n", ll_ifma, ifp->if_xname);
583 if_freemulti(ll_ifma);
586 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
587 if (ifa->ifa_addr->sa_family != AF_INET6)
590 LIST_FOREACH_SAFE(imm, &ifa6->ia6_memberships,
591 i6mm_chain, imm_tmp) {
592 if (inm == imm->i6mm_maddr) {
593 LIST_REMOVE(imm, i6mm_chain);
594 free(imm, M_IP6MADDR);
595 in6m_rele_locked(inmh, inm);
602 in6m_release_task(void *arg __unused, int pending __unused)
604 struct in6_multi_head in6m_free_tmp;
605 struct in6_multi *inm, *tinm;
607 SLIST_INIT(&in6m_free_tmp);
608 mtx_lock(&in6_multi_free_mtx);
609 SLIST_CONCAT(&in6m_free_tmp, &in6m_free_list, in6_multi, in6m_nrele);
610 mtx_unlock(&in6_multi_free_mtx);
612 SLIST_FOREACH_SAFE(inm, &in6m_free_tmp, in6m_nrele, tinm) {
613 SLIST_REMOVE_HEAD(&in6m_free_tmp, in6m_nrele);
620 * Clear recorded source entries for a group.
621 * Used by the MLD code. Caller must hold the IN6_MULTI lock.
622 * FIXME: Should reap.
625 in6m_clear_recorded(struct in6_multi *inm)
627 struct ip6_msource *ims;
629 IN6_MULTI_LIST_LOCK_ASSERT();
631 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
634 --inm->in6m_st[1].iss_rec;
637 KASSERT(inm->in6m_st[1].iss_rec == 0,
638 ("%s: iss_rec %d not 0", __func__, inm->in6m_st[1].iss_rec));
642 * Record a source as pending for a Source-Group MLDv2 query.
643 * This lives here as it modifies the shared tree.
645 * inm is the group descriptor.
646 * naddr is the address of the source to record in network-byte order.
648 * If the net.inet6.mld.sgalloc sysctl is non-zero, we will
649 * lazy-allocate a source node in response to an SG query.
650 * Otherwise, no allocation is performed. This saves some memory
651 * with the trade-off that the source will not be reported to the
652 * router if joined in the window between the query response and
653 * the group actually being joined on the local host.
655 * VIMAGE: XXX: Currently the mld_sgalloc feature has been removed.
656 * This turns off the allocation of a recorded source entry if
657 * the group has not been joined.
659 * Return 0 if the source didn't exist or was already marked as recorded.
660 * Return 1 if the source was marked as recorded by this function.
661 * Return <0 if any error occurred (negated errno code).
664 in6m_record_source(struct in6_multi *inm, const struct in6_addr *addr)
666 struct ip6_msource find;
667 struct ip6_msource *ims, *nims;
669 IN6_MULTI_LIST_LOCK_ASSERT();
671 find.im6s_addr = *addr;
672 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
673 if (ims && ims->im6s_stp)
676 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
678 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
682 nims->im6s_addr = find.im6s_addr;
683 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
689 * Mark the source as recorded and update the recorded
693 ++inm->in6m_st[1].iss_rec;
699 * Return a pointer to an in6_msource owned by an in6_mfilter,
700 * given its source address.
701 * Lazy-allocate if needed. If this is a new entry its filter state is
704 * imf is the filter set being modified.
705 * addr is the source address.
707 * SMPng: May be called with locks held; malloc must not block.
710 im6f_get_source(struct in6_mfilter *imf, const struct sockaddr_in6 *psin,
711 struct in6_msource **plims)
713 struct ip6_msource find;
714 struct ip6_msource *ims, *nims;
715 struct in6_msource *lims;
722 find.im6s_addr = psin->sin6_addr;
723 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
724 lims = (struct in6_msource *)ims;
726 if (imf->im6f_nsrc == in6_mcast_maxsocksrc)
728 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
732 lims = (struct in6_msource *)nims;
733 lims->im6s_addr = find.im6s_addr;
734 lims->im6sl_st[0] = MCAST_UNDEFINED;
735 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
745 * Graft a source entry into an existing socket-layer filter set,
746 * maintaining any required invariants and checking allocations.
748 * The source is marked as being in the new filter mode at t1.
750 * Return the pointer to the new node, otherwise return NULL.
752 static struct in6_msource *
753 im6f_graft(struct in6_mfilter *imf, const uint8_t st1,
754 const struct sockaddr_in6 *psin)
756 struct ip6_msource *nims;
757 struct in6_msource *lims;
759 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
763 lims = (struct in6_msource *)nims;
764 lims->im6s_addr = psin->sin6_addr;
765 lims->im6sl_st[0] = MCAST_UNDEFINED;
766 lims->im6sl_st[1] = st1;
767 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
774 * Prune a source entry from an existing socket-layer filter set,
775 * maintaining any required invariants and checking allocations.
777 * The source is marked as being left at t1, it is not freed.
779 * Return 0 if no error occurred, otherwise return an errno value.
782 im6f_prune(struct in6_mfilter *imf, const struct sockaddr_in6 *psin)
784 struct ip6_msource find;
785 struct ip6_msource *ims;
786 struct in6_msource *lims;
788 find.im6s_addr = psin->sin6_addr;
789 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
792 lims = (struct in6_msource *)ims;
793 lims->im6sl_st[1] = MCAST_UNDEFINED;
798 * Revert socket-layer filter set deltas at t1 to t0 state.
801 im6f_rollback(struct in6_mfilter *imf)
803 struct ip6_msource *ims, *tims;
804 struct in6_msource *lims;
806 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
807 lims = (struct in6_msource *)ims;
808 if (lims->im6sl_st[0] == lims->im6sl_st[1]) {
809 /* no change at t1 */
811 } else if (lims->im6sl_st[0] != MCAST_UNDEFINED) {
812 /* revert change to existing source at t1 */
813 lims->im6sl_st[1] = lims->im6sl_st[0];
815 /* revert source added t1 */
816 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
817 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
818 free(ims, M_IN6MFILTER);
822 imf->im6f_st[1] = imf->im6f_st[0];
826 * Mark socket-layer filter set as INCLUDE {} at t1.
829 im6f_leave(struct in6_mfilter *imf)
831 struct ip6_msource *ims;
832 struct in6_msource *lims;
834 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
835 lims = (struct in6_msource *)ims;
836 lims->im6sl_st[1] = MCAST_UNDEFINED;
838 imf->im6f_st[1] = MCAST_INCLUDE;
842 * Mark socket-layer filter set deltas as committed.
845 im6f_commit(struct in6_mfilter *imf)
847 struct ip6_msource *ims;
848 struct in6_msource *lims;
850 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
851 lims = (struct in6_msource *)ims;
852 lims->im6sl_st[0] = lims->im6sl_st[1];
854 imf->im6f_st[0] = imf->im6f_st[1];
858 * Reap unreferenced sources from socket-layer filter set.
861 im6f_reap(struct in6_mfilter *imf)
863 struct ip6_msource *ims, *tims;
864 struct in6_msource *lims;
866 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
867 lims = (struct in6_msource *)ims;
868 if ((lims->im6sl_st[0] == MCAST_UNDEFINED) &&
869 (lims->im6sl_st[1] == MCAST_UNDEFINED)) {
870 CTR2(KTR_MLD, "%s: free lims %p", __func__, ims);
871 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
872 free(ims, M_IN6MFILTER);
879 * Purge socket-layer filter set.
882 im6f_purge(struct in6_mfilter *imf)
884 struct ip6_msource *ims, *tims;
886 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
887 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
888 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
889 free(ims, M_IN6MFILTER);
892 imf->im6f_st[0] = imf->im6f_st[1] = MCAST_UNDEFINED;
893 KASSERT(RB_EMPTY(&imf->im6f_sources),
894 ("%s: im6f_sources not empty", __func__));
898 * Look up a source filter entry for a multicast group.
900 * inm is the group descriptor to work with.
901 * addr is the IPv6 address to look up.
902 * noalloc may be non-zero to suppress allocation of sources.
903 * *pims will be set to the address of the retrieved or allocated source.
905 * SMPng: NOTE: may be called with locks held.
906 * Return 0 if successful, otherwise return a non-zero error code.
909 in6m_get_source(struct in6_multi *inm, const struct in6_addr *addr,
910 const int noalloc, struct ip6_msource **pims)
912 struct ip6_msource find;
913 struct ip6_msource *ims, *nims;
915 char ip6tbuf[INET6_ADDRSTRLEN];
918 find.im6s_addr = *addr;
919 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
920 if (ims == NULL && !noalloc) {
921 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
923 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
927 nims->im6s_addr = *addr;
928 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
931 CTR3(KTR_MLD, "%s: allocated %s as %p", __func__,
932 ip6_sprintf(ip6tbuf, addr), ims);
940 * Merge socket-layer source into MLD-layer source.
941 * If rollback is non-zero, perform the inverse of the merge.
944 im6s_merge(struct ip6_msource *ims, const struct in6_msource *lims,
947 int n = rollback ? -1 : 1;
949 char ip6tbuf[INET6_ADDRSTRLEN];
951 ip6_sprintf(ip6tbuf, &lims->im6s_addr);
954 if (lims->im6sl_st[0] == MCAST_EXCLUDE) {
955 CTR3(KTR_MLD, "%s: t1 ex -= %d on %s", __func__, n, ip6tbuf);
956 ims->im6s_st[1].ex -= n;
957 } else if (lims->im6sl_st[0] == MCAST_INCLUDE) {
958 CTR3(KTR_MLD, "%s: t1 in -= %d on %s", __func__, n, ip6tbuf);
959 ims->im6s_st[1].in -= n;
962 if (lims->im6sl_st[1] == MCAST_EXCLUDE) {
963 CTR3(KTR_MLD, "%s: t1 ex += %d on %s", __func__, n, ip6tbuf);
964 ims->im6s_st[1].ex += n;
965 } else if (lims->im6sl_st[1] == MCAST_INCLUDE) {
966 CTR3(KTR_MLD, "%s: t1 in += %d on %s", __func__, n, ip6tbuf);
967 ims->im6s_st[1].in += n;
972 * Atomically update the global in6_multi state, when a membership's
973 * filter list is being updated in any way.
975 * imf is the per-inpcb-membership group filter pointer.
976 * A fake imf may be passed for in-kernel consumers.
978 * XXX This is a candidate for a set-symmetric-difference style loop
979 * which would eliminate the repeated lookup from root of ims nodes,
980 * as they share the same key space.
982 * If any error occurred this function will back out of refcounts
983 * and return a non-zero value.
986 in6m_merge(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
988 struct ip6_msource *ims, *nims;
989 struct in6_msource *lims;
996 IN6_MULTI_LIST_LOCK_ASSERT();
999 * Update the source filters first, as this may fail.
1000 * Maintain count of in-mode filters at t0, t1. These are
1001 * used to work out if we transition into ASM mode or not.
1002 * Maintain a count of source filters whose state was
1003 * actually modified by this operation.
1005 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
1006 lims = (struct in6_msource *)ims;
1007 if (lims->im6sl_st[0] == imf->im6f_st[0]) nsrc0++;
1008 if (lims->im6sl_st[1] == imf->im6f_st[1]) nsrc1++;
1009 if (lims->im6sl_st[0] == lims->im6sl_st[1]) continue;
1010 error = in6m_get_source(inm, &lims->im6s_addr, 0, &nims);
1014 im6s_merge(nims, lims, 0);
1017 struct ip6_msource *bims;
1019 RB_FOREACH_REVERSE_FROM(ims, ip6_msource_tree, nims) {
1020 lims = (struct in6_msource *)ims;
1021 if (lims->im6sl_st[0] == lims->im6sl_st[1])
1023 (void)in6m_get_source(inm, &lims->im6s_addr, 1, &bims);
1026 im6s_merge(bims, lims, 1);
1031 CTR3(KTR_MLD, "%s: imf filters in-mode: %d at t0, %d at t1",
1032 __func__, nsrc0, nsrc1);
1034 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1035 if (imf->im6f_st[0] == imf->im6f_st[1] &&
1036 imf->im6f_st[1] == MCAST_INCLUDE) {
1038 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
1039 --inm->in6m_st[1].iss_in;
1043 /* Handle filter mode transition on socket. */
1044 if (imf->im6f_st[0] != imf->im6f_st[1]) {
1045 CTR3(KTR_MLD, "%s: imf transition %d to %d",
1046 __func__, imf->im6f_st[0], imf->im6f_st[1]);
1048 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
1049 CTR1(KTR_MLD, "%s: --ex on inm at t1", __func__);
1050 --inm->in6m_st[1].iss_ex;
1051 } else if (imf->im6f_st[0] == MCAST_INCLUDE) {
1052 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
1053 --inm->in6m_st[1].iss_in;
1056 if (imf->im6f_st[1] == MCAST_EXCLUDE) {
1057 CTR1(KTR_MLD, "%s: ex++ on inm at t1", __func__);
1058 inm->in6m_st[1].iss_ex++;
1059 } else if (imf->im6f_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
1060 CTR1(KTR_MLD, "%s: in++ on inm at t1", __func__);
1061 inm->in6m_st[1].iss_in++;
1066 * Track inm filter state in terms of listener counts.
1067 * If there are any exclusive listeners, stack-wide
1068 * membership is exclusive.
1069 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1070 * If no listeners remain, state is undefined at t1,
1071 * and the MLD lifecycle for this group should finish.
1073 if (inm->in6m_st[1].iss_ex > 0) {
1074 CTR1(KTR_MLD, "%s: transition to EX", __func__);
1075 inm->in6m_st[1].iss_fmode = MCAST_EXCLUDE;
1076 } else if (inm->in6m_st[1].iss_in > 0) {
1077 CTR1(KTR_MLD, "%s: transition to IN", __func__);
1078 inm->in6m_st[1].iss_fmode = MCAST_INCLUDE;
1080 CTR1(KTR_MLD, "%s: transition to UNDEF", __func__);
1081 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
1084 /* Decrement ASM listener count on transition out of ASM mode. */
1085 if (imf->im6f_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1086 if ((imf->im6f_st[1] != MCAST_EXCLUDE) ||
1087 (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 > 0)) {
1088 CTR1(KTR_MLD, "%s: --asm on inm at t1", __func__);
1089 --inm->in6m_st[1].iss_asm;
1093 /* Increment ASM listener count on transition to ASM mode. */
1094 if (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1095 CTR1(KTR_MLD, "%s: asm++ on inm at t1", __func__);
1096 inm->in6m_st[1].iss_asm++;
1099 CTR3(KTR_MLD, "%s: merged imf %p to inm %p", __func__, imf, inm);
1104 CTR1(KTR_MLD, "%s: sources changed; reaping", __func__);
1111 * Mark an in6_multi's filter set deltas as committed.
1112 * Called by MLD after a state change has been enqueued.
1115 in6m_commit(struct in6_multi *inm)
1117 struct ip6_msource *ims;
1119 CTR2(KTR_MLD, "%s: commit inm %p", __func__, inm);
1120 CTR1(KTR_MLD, "%s: pre commit:", __func__);
1123 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
1124 ims->im6s_st[0] = ims->im6s_st[1];
1126 inm->in6m_st[0] = inm->in6m_st[1];
1130 * Reap unreferenced nodes from an in6_multi's filter set.
1133 in6m_reap(struct in6_multi *inm)
1135 struct ip6_msource *ims, *tims;
1137 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1138 if (ims->im6s_st[0].ex > 0 || ims->im6s_st[0].in > 0 ||
1139 ims->im6s_st[1].ex > 0 || ims->im6s_st[1].in > 0 ||
1142 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1143 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1144 free(ims, M_IP6MSOURCE);
1150 * Purge all source nodes from an in6_multi's filter set.
1153 in6m_purge(struct in6_multi *inm)
1155 struct ip6_msource *ims, *tims;
1157 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1158 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1159 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1160 free(ims, M_IP6MSOURCE);
1163 /* Free state-change requests that might be queued. */
1164 mbufq_drain(&inm->in6m_scq);
1168 * Join a multicast address w/o sources.
1169 * KAME compatibility entry point.
1171 * SMPng: Assume no mc locks held by caller.
1174 in6_joingroup(struct ifnet *ifp, const struct in6_addr *mcaddr,
1175 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1181 error = in6_joingroup_locked(ifp, mcaddr, NULL, pinm, delay);
1187 * Join a multicast group; real entry point.
1189 * Only preserves atomicity at inm level.
1190 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1192 * If the MLD downcall fails, the group is not joined, and an error
1196 in6_joingroup_locked(struct ifnet *ifp, const struct in6_addr *mcaddr,
1197 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1200 struct in6_multi_head inmh;
1201 struct in6_mfilter timf;
1202 struct in6_multi *inm;
1203 struct ifmultiaddr *ifma;
1206 char ip6tbuf[INET6_ADDRSTRLEN];
1211 * Sanity: Check scope zone ID was set for ifp, if and
1212 * only if group is scoped to an interface.
1214 KASSERT(IN6_IS_ADDR_MULTICAST(mcaddr),
1215 ("%s: not a multicast address", __func__));
1216 if (IN6_IS_ADDR_MC_LINKLOCAL(mcaddr) ||
1217 IN6_IS_ADDR_MC_INTFACELOCAL(mcaddr)) {
1218 KASSERT(mcaddr->s6_addr16[1] != 0,
1219 ("%s: scope zone ID not set", __func__));
1223 IN6_MULTI_LOCK_ASSERT();
1224 IN6_MULTI_LIST_UNLOCK_ASSERT();
1226 CTR4(KTR_MLD, "%s: join %s on %p(%s))", __func__,
1227 ip6_sprintf(ip6tbuf, mcaddr), ifp, if_name(ifp));
1233 * If no imf was specified (i.e. kernel consumer),
1234 * fake one up and assume it is an ASM join.
1237 im6f_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1240 error = in6_getmulti(ifp, mcaddr, &inm);
1242 CTR1(KTR_MLD, "%s: in6_getmulti() failure", __func__);
1246 IN6_MULTI_LIST_LOCK();
1247 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1248 error = in6m_merge(inm, imf);
1250 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1251 goto out_in6m_release;
1254 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1255 error = mld_change_state(inm, delay);
1257 CTR1(KTR_MLD, "%s: failed to update source", __func__);
1258 goto out_in6m_release;
1264 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1266 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1267 if (ifma->ifma_protospec == inm) {
1268 ifma->ifma_protospec = NULL;
1272 in6m_disconnect_locked(&inmh, inm);
1273 in6m_rele_locked(&inmh, inm);
1274 IF_ADDR_RUNLOCK(ifp);
1278 IN6_MULTI_LIST_UNLOCK();
1279 in6m_release_list_deferred(&inmh);
1284 * Leave a multicast group; unlocked entry point.
1287 in6_leavegroup(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1292 error = in6_leavegroup_locked(inm, imf);
1298 * Leave a multicast group; real entry point.
1299 * All source filters will be expunged.
1301 * Only preserves atomicity at inm level.
1303 * Holding the write lock for the INP which contains imf
1304 * is highly advisable. We can't assert for it as imf does not
1305 * contain a back-pointer to the owning inp.
1307 * Note: This is not the same as in6m_release(*) as this function also
1308 * makes a state change downcall into MLD.
1311 in6_leavegroup_locked(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1313 struct in6_multi_head inmh;
1314 struct in6_mfilter timf;
1318 char ip6tbuf[INET6_ADDRSTRLEN];
1323 IN6_MULTI_LOCK_ASSERT();
1325 CTR5(KTR_MLD, "%s: leave inm %p, %s/%s, imf %p", __func__,
1326 inm, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1327 (in6m_is_ifp_detached(inm) ? "null" : if_name(inm->in6m_ifp)),
1331 * If no imf was specified (i.e. kernel consumer),
1332 * fake one up and assume it is an ASM join.
1335 im6f_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1340 * Begin state merge transaction at MLD layer.
1342 * As this particular invocation should not cause any memory
1343 * to be allocated, and there is no opportunity to roll back
1344 * the transaction, it MUST NOT fail.
1347 ifp = inm->in6m_ifp;
1348 IN6_MULTI_LIST_LOCK();
1349 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1350 error = in6m_merge(inm, imf);
1351 KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1353 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1356 error = mld_change_state(inm, 0);
1358 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1360 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1365 if (inm->in6m_refcount == 1)
1366 in6m_disconnect_locked(&inmh, inm);
1367 in6m_rele_locked(&inmh, inm);
1369 IF_ADDR_WUNLOCK(ifp);
1370 IN6_MULTI_LIST_UNLOCK();
1371 in6m_release_list_deferred(&inmh);
1377 * Block or unblock an ASM multicast source on an inpcb.
1378 * This implements the delta-based API described in RFC 3678.
1380 * The delta-based API applies only to exclusive-mode memberships.
1381 * An MLD downcall will be performed.
1383 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1385 * Return 0 if successful, otherwise return an appropriate error code.
1388 in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1390 struct group_source_req gsr;
1391 sockunion_t *gsa, *ssa;
1393 struct in6_mfilter *imf;
1394 struct ip6_moptions *imo;
1395 struct in6_msource *ims;
1396 struct in6_multi *inm;
1400 char ip6tbuf[INET6_ADDRSTRLEN];
1407 memset(&gsr, 0, sizeof(struct group_source_req));
1408 gsa = (sockunion_t *)&gsr.gsr_group;
1409 ssa = (sockunion_t *)&gsr.gsr_source;
1411 switch (sopt->sopt_name) {
1412 case MCAST_BLOCK_SOURCE:
1413 case MCAST_UNBLOCK_SOURCE:
1414 error = sooptcopyin(sopt, &gsr,
1415 sizeof(struct group_source_req),
1416 sizeof(struct group_source_req));
1420 if (gsa->sin6.sin6_family != AF_INET6 ||
1421 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1424 if (ssa->sin6.sin6_family != AF_INET6 ||
1425 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1428 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1429 return (EADDRNOTAVAIL);
1431 ifp = ifnet_byindex(gsr.gsr_interface);
1433 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1438 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1439 __func__, sopt->sopt_name);
1440 return (EOPNOTSUPP);
1444 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1447 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1450 * Check if we are actually a member of this group.
1452 imo = in6p_findmoptions(inp);
1453 imf = im6o_match_group(imo, ifp, &gsa->sa);
1455 error = EADDRNOTAVAIL;
1456 goto out_in6p_locked;
1458 inm = imf->im6f_in6m;
1461 * Attempting to use the delta-based API on an
1462 * non exclusive-mode membership is an error.
1464 fmode = imf->im6f_st[0];
1465 if (fmode != MCAST_EXCLUDE) {
1467 goto out_in6p_locked;
1471 * Deal with error cases up-front:
1472 * Asked to block, but already blocked; or
1473 * Asked to unblock, but nothing to unblock.
1474 * If adding a new block entry, allocate it.
1476 ims = im6o_match_source(imf, &ssa->sa);
1477 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1478 CTR3(KTR_MLD, "%s: source %s %spresent", __func__,
1479 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
1480 doblock ? "" : "not ");
1481 error = EADDRNOTAVAIL;
1482 goto out_in6p_locked;
1485 INP_WLOCK_ASSERT(inp);
1488 * Begin state merge transaction at socket layer.
1491 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
1492 ims = im6f_graft(imf, fmode, &ssa->sin6);
1496 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
1497 error = im6f_prune(imf, &ssa->sin6);
1501 CTR1(KTR_MLD, "%s: merge imf state failed", __func__);
1502 goto out_im6f_rollback;
1506 * Begin state merge transaction at MLD layer.
1508 IN6_MULTI_LIST_LOCK();
1509 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1510 error = in6m_merge(inm, imf);
1512 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1514 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1515 error = mld_change_state(inm, 0);
1517 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1520 IN6_MULTI_LIST_UNLOCK();
1536 * Given an inpcb, return its multicast options structure pointer. Accepts
1537 * an unlocked inpcb pointer, but will return it locked. May sleep.
1539 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1540 * SMPng: NOTE: Returns with the INP write lock held.
1542 static struct ip6_moptions *
1543 in6p_findmoptions(struct inpcb *inp)
1545 struct ip6_moptions *imo;
1548 if (inp->in6p_moptions != NULL)
1549 return (inp->in6p_moptions);
1553 imo = malloc(sizeof(*imo), M_IP6MOPTS, M_WAITOK);
1555 imo->im6o_multicast_ifp = NULL;
1556 imo->im6o_multicast_hlim = V_ip6_defmcasthlim;
1557 imo->im6o_multicast_loop = in6_mcast_loop;
1558 STAILQ_INIT(&imo->im6o_head);
1561 if (inp->in6p_moptions != NULL) {
1562 free(imo, M_IP6MOPTS);
1563 return (inp->in6p_moptions);
1565 inp->in6p_moptions = imo;
1570 * Discard the IPv6 multicast options (and source filters).
1572 * SMPng: NOTE: assumes INP write lock is held.
1574 * XXX can all be safely deferred to epoch_call
1579 inp_gcmoptions(struct ip6_moptions *imo)
1581 struct in6_mfilter *imf;
1582 struct in6_multi *inm;
1585 while ((imf = ip6_mfilter_first(&imo->im6o_head)) != NULL) {
1586 ip6_mfilter_remove(&imo->im6o_head, imf);
1589 if ((inm = imf->im6f_in6m) != NULL) {
1590 if ((ifp = inm->in6m_ifp) != NULL) {
1591 CURVNET_SET(ifp->if_vnet);
1592 (void)in6_leavegroup(inm, imf);
1595 (void)in6_leavegroup(inm, imf);
1598 ip6_mfilter_free(imf);
1600 free(imo, M_IP6MOPTS);
1604 ip6_freemoptions(struct ip6_moptions *imo)
1608 inp_gcmoptions(imo);
1612 * Atomically get source filters on a socket for an IPv6 multicast group.
1613 * Called with INP lock held; returns with lock released.
1616 in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1618 struct __msfilterreq msfr;
1621 struct ip6_moptions *imo;
1622 struct in6_mfilter *imf;
1623 struct ip6_msource *ims;
1624 struct in6_msource *lims;
1625 struct sockaddr_in6 *psin;
1626 struct sockaddr_storage *ptss;
1627 struct sockaddr_storage *tss;
1629 size_t nsrcs, ncsrcs;
1631 INP_WLOCK_ASSERT(inp);
1633 imo = inp->in6p_moptions;
1634 KASSERT(imo != NULL, ("%s: null ip6_moptions", __func__));
1638 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1639 sizeof(struct __msfilterreq));
1643 if (msfr.msfr_group.ss_family != AF_INET6 ||
1644 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
1647 gsa = (sockunion_t *)&msfr.msfr_group;
1648 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1651 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
1652 return (EADDRNOTAVAIL);
1653 ifp = ifnet_byindex(msfr.msfr_ifindex);
1655 return (EADDRNOTAVAIL);
1656 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1661 * Lookup group on the socket.
1663 imf = im6o_match_group(imo, ifp, &gsa->sa);
1666 return (EADDRNOTAVAIL);
1670 * Ignore memberships which are in limbo.
1672 if (imf->im6f_st[1] == MCAST_UNDEFINED) {
1676 msfr.msfr_fmode = imf->im6f_st[1];
1679 * If the user specified a buffer, copy out the source filter
1680 * entries to userland gracefully.
1681 * We only copy out the number of entries which userland
1682 * has asked for, but we always tell userland how big the
1683 * buffer really needs to be.
1685 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
1686 msfr.msfr_nsrcs = in6_mcast_maxsocksrc;
1688 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1689 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1690 M_TEMP, M_NOWAIT | M_ZERO);
1698 * Count number of sources in-mode at t0.
1699 * If buffer space exists and remains, copy out source entries.
1701 nsrcs = msfr.msfr_nsrcs;
1704 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
1705 lims = (struct in6_msource *)ims;
1706 if (lims->im6sl_st[0] == MCAST_UNDEFINED ||
1707 lims->im6sl_st[0] != imf->im6f_st[0])
1710 if (tss != NULL && nsrcs > 0) {
1711 psin = (struct sockaddr_in6 *)ptss;
1712 psin->sin6_family = AF_INET6;
1713 psin->sin6_len = sizeof(struct sockaddr_in6);
1714 psin->sin6_addr = lims->im6s_addr;
1715 psin->sin6_port = 0;
1724 error = copyout(tss, msfr.msfr_srcs,
1725 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1731 msfr.msfr_nsrcs = ncsrcs;
1732 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1738 * Return the IP multicast options in response to user getsockopt().
1741 ip6_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1743 struct ip6_moptions *im6o;
1748 im6o = inp->in6p_moptions;
1750 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1751 * or is a divert socket, reject it.
1753 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1754 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1755 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
1757 return (EOPNOTSUPP);
1761 switch (sopt->sopt_name) {
1762 case IPV6_MULTICAST_IF:
1763 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) {
1766 optval = im6o->im6o_multicast_ifp->if_index;
1769 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1772 case IPV6_MULTICAST_HOPS:
1774 optval = V_ip6_defmcasthlim;
1776 optval = im6o->im6o_multicast_hlim;
1778 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1781 case IPV6_MULTICAST_LOOP:
1783 optval = in6_mcast_loop; /* XXX VIMAGE */
1785 optval = im6o->im6o_multicast_loop;
1787 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1792 error = EADDRNOTAVAIL;
1795 error = in6p_get_source_filters(inp, sopt);
1801 error = ENOPROTOOPT;
1805 INP_UNLOCK_ASSERT(inp);
1811 * Look up the ifnet to use for a multicast group membership,
1812 * given the address of an IPv6 group.
1814 * This routine exists to support legacy IPv6 multicast applications.
1816 * Use the socket's current FIB number for any required FIB lookup. Look up the
1817 * group address in the unicast FIB, and use its ifp; usually, this points to
1818 * the default next-hop. If the FIB lookup fails, return NULL.
1820 * FUTURE: Support multiple forwarding tables for IPv6.
1822 * Returns NULL if no ifp could be found.
1824 static struct ifnet *
1825 in6p_lookup_mcast_ifp(const struct inpcb *inp, const struct sockaddr_in6 *gsin6)
1827 struct nhop6_basic nh6;
1828 struct in6_addr dst;
1832 KASSERT(gsin6->sin6_family == AF_INET6,
1833 ("%s: not AF_INET6 group", __func__));
1835 in6_splitscope(&gsin6->sin6_addr, &dst, &scopeid);
1836 fibnum = inp->inp_inc.inc_fibnum;
1837 if (fib6_lookup_nh_basic(fibnum, &dst, scopeid, 0, 0, &nh6) != 0)
1840 return (nh6.nh_ifp);
1844 * Join an IPv6 multicast group, possibly with a source.
1846 * FIXME: The KAME use of the unspecified address (::)
1847 * to join *all* multicast groups is currently unsupported.
1850 in6p_join_group(struct inpcb *inp, struct sockopt *sopt)
1852 struct in6_multi_head inmh;
1853 struct group_source_req gsr;
1854 sockunion_t *gsa, *ssa;
1856 struct in6_mfilter *imf;
1857 struct ip6_moptions *imo;
1858 struct in6_multi *inm;
1859 struct in6_msource *lims;
1867 memset(&gsr, 0, sizeof(struct group_source_req));
1868 gsa = (sockunion_t *)&gsr.gsr_group;
1869 gsa->ss.ss_family = AF_UNSPEC;
1870 ssa = (sockunion_t *)&gsr.gsr_source;
1871 ssa->ss.ss_family = AF_UNSPEC;
1874 * Chew everything into struct group_source_req.
1875 * Overwrite the port field if present, as the sockaddr
1876 * being copied in may be matched with a binary comparison.
1877 * Ignore passed-in scope ID.
1879 switch (sopt->sopt_name) {
1880 case IPV6_JOIN_GROUP: {
1881 struct ipv6_mreq mreq;
1883 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
1884 sizeof(struct ipv6_mreq));
1888 gsa->sin6.sin6_family = AF_INET6;
1889 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
1890 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
1892 if (mreq.ipv6mr_interface == 0) {
1893 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
1895 if (V_if_index < mreq.ipv6mr_interface)
1896 return (EADDRNOTAVAIL);
1897 ifp = ifnet_byindex(mreq.ipv6mr_interface);
1899 CTR3(KTR_MLD, "%s: ipv6mr_interface = %d, ifp = %p",
1900 __func__, mreq.ipv6mr_interface, ifp);
1903 case MCAST_JOIN_GROUP:
1904 case MCAST_JOIN_SOURCE_GROUP:
1905 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1906 error = sooptcopyin(sopt, &gsr,
1907 sizeof(struct group_req),
1908 sizeof(struct group_req));
1909 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1910 error = sooptcopyin(sopt, &gsr,
1911 sizeof(struct group_source_req),
1912 sizeof(struct group_source_req));
1917 if (gsa->sin6.sin6_family != AF_INET6 ||
1918 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1921 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1922 if (ssa->sin6.sin6_family != AF_INET6 ||
1923 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1925 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
1928 * TODO: Validate embedded scope ID in source
1929 * list entry against passed-in ifp, if and only
1930 * if source list filter entry is iface or node local.
1932 in6_clearscope(&ssa->sin6.sin6_addr);
1933 ssa->sin6.sin6_port = 0;
1934 ssa->sin6.sin6_scope_id = 0;
1937 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1938 return (EADDRNOTAVAIL);
1939 ifp = ifnet_byindex(gsr.gsr_interface);
1943 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1944 __func__, sopt->sopt_name);
1945 return (EOPNOTSUPP);
1949 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1952 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
1953 return (EADDRNOTAVAIL);
1955 gsa->sin6.sin6_port = 0;
1956 gsa->sin6.sin6_scope_id = 0;
1959 * Always set the scope zone ID on memberships created from userland.
1960 * Use the passed-in ifp to do this.
1961 * XXX The in6_setscope() return value is meaningless.
1962 * XXX SCOPE6_LOCK() is taken by in6_setscope().
1964 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1969 * Find the membership in the membership list.
1971 imo = in6p_findmoptions(inp);
1972 imf = im6o_match_group(imo, ifp, &gsa->sa);
1977 if (ip6_mfilter_count(&imo->im6o_head) >= IPV6_MAX_MEMBERSHIPS) {
1979 goto out_in6p_locked;
1983 inm = imf->im6f_in6m;
1985 if (ssa->ss.ss_family != AF_UNSPEC) {
1987 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
1988 * is an error. On an existing inclusive membership,
1989 * it just adds the source to the filter list.
1991 if (imf->im6f_st[1] != MCAST_INCLUDE) {
1993 goto out_in6p_locked;
1996 * Throw out duplicates.
1998 * XXX FIXME: This makes a naive assumption that
1999 * even if entries exist for *ssa in this imf,
2000 * they will be rejected as dupes, even if they
2001 * are not valid in the current mode (in-mode).
2003 * in6_msource is transactioned just as for anything
2004 * else in SSM -- but note naive use of in6m_graft()
2005 * below for allocating new filter entries.
2007 * This is only an issue if someone mixes the
2008 * full-state SSM API with the delta-based API,
2009 * which is discouraged in the relevant RFCs.
2011 lims = im6o_match_source(imf, &ssa->sa);
2012 if (lims != NULL /*&&
2013 lims->im6sl_st[1] == MCAST_INCLUDE*/) {
2014 error = EADDRNOTAVAIL;
2015 goto out_in6p_locked;
2019 * MCAST_JOIN_GROUP alone, on any existing membership,
2020 * is rejected, to stop the same inpcb tying up
2021 * multiple refs to the in_multi.
2022 * On an existing inclusive membership, this is also
2023 * an error; if you want to change filter mode,
2024 * you must use the userland API setsourcefilter().
2025 * XXX We don't reject this for imf in UNDEFINED
2026 * state at t1, because allocation of a filter
2027 * is atomic with allocation of a membership.
2030 goto out_in6p_locked;
2035 * Begin state merge transaction at socket layer.
2037 INP_WLOCK_ASSERT(inp);
2040 * Graft new source into filter list for this inpcb's
2041 * membership of the group. The in6_multi may not have
2042 * been allocated yet if this is a new membership, however,
2043 * the in_mfilter slot will be allocated and must be initialized.
2045 * Note: Grafting of exclusive mode filters doesn't happen
2047 * XXX: Should check for non-NULL lims (node exists but may
2048 * not be in-mode) for interop with full-state API.
2050 if (ssa->ss.ss_family != AF_UNSPEC) {
2051 /* Membership starts in IN mode */
2053 CTR1(KTR_MLD, "%s: new join w/source", __func__);
2054 imf = ip6_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_INCLUDE);
2057 goto out_in6p_locked;
2060 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
2062 lims = im6f_graft(imf, MCAST_INCLUDE, &ssa->sin6);
2064 CTR1(KTR_MLD, "%s: merge imf state failed",
2067 goto out_in6p_locked;
2070 /* No address specified; Membership starts in EX mode */
2072 CTR1(KTR_MLD, "%s: new join w/o source", __func__);
2073 imf = ip6_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_EXCLUDE);
2076 goto out_in6p_locked;
2082 * Begin state merge transaction at MLD layer.
2088 error = in6_joingroup_locked(ifp, &gsa->sin6.sin6_addr, imf,
2089 &imf->im6f_in6m, 0);
2092 if (in_pcbrele_wlocked(inp)) {
2094 goto out_in6p_unlocked;
2097 goto out_in6p_locked;
2100 * NOTE: Refcount from in6_joingroup_locked()
2101 * is protecting membership.
2103 ip6_mfilter_insert(&imo->im6o_head, imf);
2105 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2106 IN6_MULTI_LIST_LOCK();
2107 error = in6m_merge(inm, imf);
2109 CTR1(KTR_MLD, "%s: failed to merge inm state",
2111 IN6_MULTI_LIST_UNLOCK();
2114 goto out_in6p_locked;
2116 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2117 error = mld_change_state(inm, 0);
2118 IN6_MULTI_LIST_UNLOCK();
2121 CTR1(KTR_MLD, "%s: failed mld downcall",
2125 goto out_in6p_locked;
2137 if (is_new && imf) {
2138 if (imf->im6f_in6m != NULL) {
2139 struct in6_multi_head inmh;
2142 SLIST_INSERT_HEAD(&inmh, imf->im6f_in6m, in6m_defer);
2143 in6m_release_list_deferred(&inmh);
2145 ip6_mfilter_free(imf);
2151 * Leave an IPv6 multicast group on an inpcb, possibly with a source.
2154 in6p_leave_group(struct inpcb *inp, struct sockopt *sopt)
2156 struct ipv6_mreq mreq;
2157 struct group_source_req gsr;
2158 sockunion_t *gsa, *ssa;
2160 struct in6_mfilter *imf;
2161 struct ip6_moptions *imo;
2162 struct in6_msource *ims;
2163 struct in6_multi *inm;
2168 char ip6tbuf[INET6_ADDRSTRLEN];
2176 memset(&gsr, 0, sizeof(struct group_source_req));
2177 gsa = (sockunion_t *)&gsr.gsr_group;
2178 gsa->ss.ss_family = AF_UNSPEC;
2179 ssa = (sockunion_t *)&gsr.gsr_source;
2180 ssa->ss.ss_family = AF_UNSPEC;
2183 * Chew everything passed in up into a struct group_source_req
2184 * as that is easier to process.
2185 * Note: Any embedded scope ID in the multicast group passed
2186 * in by userland is ignored, the interface index is the recommended
2187 * mechanism to specify an interface; see below.
2189 switch (sopt->sopt_name) {
2190 case IPV6_LEAVE_GROUP:
2191 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
2192 sizeof(struct ipv6_mreq));
2195 gsa->sin6.sin6_family = AF_INET6;
2196 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
2197 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
2198 gsa->sin6.sin6_port = 0;
2199 gsa->sin6.sin6_scope_id = 0;
2200 ifindex = mreq.ipv6mr_interface;
2203 case MCAST_LEAVE_GROUP:
2204 case MCAST_LEAVE_SOURCE_GROUP:
2205 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2206 error = sooptcopyin(sopt, &gsr,
2207 sizeof(struct group_req),
2208 sizeof(struct group_req));
2209 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2210 error = sooptcopyin(sopt, &gsr,
2211 sizeof(struct group_source_req),
2212 sizeof(struct group_source_req));
2217 if (gsa->sin6.sin6_family != AF_INET6 ||
2218 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2220 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2221 if (ssa->sin6.sin6_family != AF_INET6 ||
2222 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2224 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
2227 * TODO: Validate embedded scope ID in source
2228 * list entry against passed-in ifp, if and only
2229 * if source list filter entry is iface or node local.
2231 in6_clearscope(&ssa->sin6.sin6_addr);
2233 gsa->sin6.sin6_port = 0;
2234 gsa->sin6.sin6_scope_id = 0;
2235 ifindex = gsr.gsr_interface;
2239 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
2240 __func__, sopt->sopt_name);
2241 return (EOPNOTSUPP);
2245 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2249 * Validate interface index if provided. If no interface index
2250 * was provided separately, attempt to look the membership up
2251 * from the default scope as a last resort to disambiguate
2252 * the membership we are being asked to leave.
2253 * XXX SCOPE6 lock potentially taken here.
2256 if (V_if_index < ifindex)
2257 return (EADDRNOTAVAIL);
2258 ifp = ifnet_byindex(ifindex);
2260 return (EADDRNOTAVAIL);
2261 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2263 error = sa6_embedscope(&gsa->sin6, V_ip6_use_defzone);
2265 return (EADDRNOTAVAIL);
2267 * Some badly behaved applications don't pass an ifindex
2268 * or a scope ID, which is an API violation. In this case,
2269 * perform a lookup as per a v6 join.
2271 * XXX For now, stomp on zone ID for the corner case.
2272 * This is not the 'KAME way', but we need to see the ifp
2273 * directly until such time as this implementation is
2274 * refactored, assuming the scope IDs are the way to go.
2276 ifindex = ntohs(gsa->sin6.sin6_addr.s6_addr16[1]);
2278 CTR2(KTR_MLD, "%s: warning: no ifindex, looking up "
2279 "ifp for group %s.", __func__,
2280 ip6_sprintf(ip6tbuf, &gsa->sin6.sin6_addr));
2281 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
2283 ifp = ifnet_byindex(ifindex);
2286 return (EADDRNOTAVAIL);
2289 CTR2(KTR_MLD, "%s: ifp = %p", __func__, ifp);
2290 KASSERT(ifp != NULL, ("%s: ifp did not resolve", __func__));
2295 * Find the membership in the membership list.
2297 imo = in6p_findmoptions(inp);
2298 imf = im6o_match_group(imo, ifp, &gsa->sa);
2300 error = EADDRNOTAVAIL;
2301 goto out_in6p_locked;
2303 inm = imf->im6f_in6m;
2305 if (ssa->ss.ss_family != AF_UNSPEC)
2309 * Begin state merge transaction at socket layer.
2311 INP_WLOCK_ASSERT(inp);
2314 * If we were instructed only to leave a given source, do so.
2315 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2318 ip6_mfilter_remove(&imo->im6o_head, imf);
2322 * Give up the multicast address record to which
2323 * the membership points.
2325 (void)in6_leavegroup_locked(inm, imf);
2327 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
2328 error = EADDRNOTAVAIL;
2329 goto out_in6p_locked;
2331 ims = im6o_match_source(imf, &ssa->sa);
2333 CTR3(KTR_MLD, "%s: source %p %spresent", __func__,
2334 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
2336 error = EADDRNOTAVAIL;
2337 goto out_in6p_locked;
2339 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
2340 error = im6f_prune(imf, &ssa->sin6);
2342 CTR1(KTR_MLD, "%s: merge imf state failed",
2344 goto out_in6p_locked;
2349 * Begin state merge transaction at MLD layer.
2352 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2353 IN6_MULTI_LIST_LOCK();
2354 error = in6m_merge(inm, imf);
2356 CTR1(KTR_MLD, "%s: failed to merge inm state",
2358 IN6_MULTI_LIST_UNLOCK();
2361 goto out_in6p_locked;
2364 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2365 error = mld_change_state(inm, 0);
2366 IN6_MULTI_LIST_UNLOCK();
2368 CTR1(KTR_MLD, "%s: failed mld downcall",
2372 goto out_in6p_locked;
2382 if (is_final && imf)
2383 ip6_mfilter_free(imf);
2390 * Select the interface for transmitting IPv6 multicast datagrams.
2392 * Either an instance of struct in6_addr or an instance of struct ipv6_mreqn
2393 * may be passed to this socket option. An address of in6addr_any or an
2394 * interface index of 0 is used to remove a previous selection.
2395 * When no interface is selected, one is chosen for every send.
2398 in6p_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2401 struct ip6_moptions *imo;
2405 if (sopt->sopt_valsize != sizeof(u_int))
2408 error = sooptcopyin(sopt, &ifindex, sizeof(u_int), sizeof(u_int));
2411 if (V_if_index < ifindex)
2416 ifp = ifnet_byindex(ifindex);
2419 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2420 return (EADDRNOTAVAIL);
2422 imo = in6p_findmoptions(inp);
2423 imo->im6o_multicast_ifp = ifp;
2430 * Atomically set source filters on a socket for an IPv6 multicast group.
2432 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
2435 in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2437 struct __msfilterreq msfr;
2440 struct in6_mfilter *imf;
2441 struct ip6_moptions *imo;
2442 struct in6_multi *inm;
2445 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2446 sizeof(struct __msfilterreq));
2450 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
2453 if (msfr.msfr_fmode != MCAST_EXCLUDE &&
2454 msfr.msfr_fmode != MCAST_INCLUDE)
2457 if (msfr.msfr_group.ss_family != AF_INET6 ||
2458 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
2461 gsa = (sockunion_t *)&msfr.msfr_group;
2462 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2465 gsa->sin6.sin6_port = 0; /* ignore port */
2467 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
2468 return (EADDRNOTAVAIL);
2469 ifp = ifnet_byindex(msfr.msfr_ifindex);
2471 return (EADDRNOTAVAIL);
2472 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2475 * Take the INP write lock.
2476 * Check if this socket is a member of this group.
2478 imo = in6p_findmoptions(inp);
2479 imf = im6o_match_group(imo, ifp, &gsa->sa);
2481 error = EADDRNOTAVAIL;
2482 goto out_in6p_locked;
2484 inm = imf->im6f_in6m;
2487 * Begin state merge transaction at socket layer.
2489 INP_WLOCK_ASSERT(inp);
2491 imf->im6f_st[1] = msfr.msfr_fmode;
2494 * Apply any new source filters, if present.
2495 * Make a copy of the user-space source vector so
2496 * that we may copy them with a single copyin. This
2497 * allows us to deal with page faults up-front.
2499 if (msfr.msfr_nsrcs > 0) {
2500 struct in6_msource *lims;
2501 struct sockaddr_in6 *psin;
2502 struct sockaddr_storage *kss, *pkss;
2507 CTR2(KTR_MLD, "%s: loading %lu source list entries",
2508 __func__, (unsigned long)msfr.msfr_nsrcs);
2509 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2511 error = copyin(msfr.msfr_srcs, kss,
2512 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2521 * Mark all source filters as UNDEFINED at t1.
2522 * Restore new group filter mode, as im6f_leave()
2523 * will set it to INCLUDE.
2526 imf->im6f_st[1] = msfr.msfr_fmode;
2529 * Update socket layer filters at t1, lazy-allocating
2530 * new entries. This saves a bunch of memory at the
2531 * cost of one RB_FIND() per source entry; duplicate
2532 * entries in the msfr_nsrcs vector are ignored.
2533 * If we encounter an error, rollback transaction.
2535 * XXX This too could be replaced with a set-symmetric
2536 * difference like loop to avoid walking from root
2537 * every time, as the key space is common.
2539 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2540 psin = (struct sockaddr_in6 *)pkss;
2541 if (psin->sin6_family != AF_INET6) {
2542 error = EAFNOSUPPORT;
2545 if (psin->sin6_len != sizeof(struct sockaddr_in6)) {
2549 if (IN6_IS_ADDR_MULTICAST(&psin->sin6_addr)) {
2554 * TODO: Validate embedded scope ID in source
2555 * list entry against passed-in ifp, if and only
2556 * if source list filter entry is iface or node local.
2558 in6_clearscope(&psin->sin6_addr);
2559 error = im6f_get_source(imf, psin, &lims);
2562 lims->im6sl_st[1] = imf->im6f_st[1];
2568 goto out_im6f_rollback;
2570 INP_WLOCK_ASSERT(inp);
2571 IN6_MULTI_LIST_LOCK();
2574 * Begin state merge transaction at MLD layer.
2576 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2577 error = in6m_merge(inm, imf);
2579 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
2581 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2582 error = mld_change_state(inm, 0);
2584 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
2587 IN6_MULTI_LIST_UNLOCK();
2603 * Set the IP multicast options in response to user setsockopt().
2605 * Many of the socket options handled in this function duplicate the
2606 * functionality of socket options in the regular unicast API. However,
2607 * it is not possible to merge the duplicate code, because the idempotence
2608 * of the IPv6 multicast part of the BSD Sockets API must be preserved;
2609 * the effects of these options must be treated as separate and distinct.
2611 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2614 ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2616 struct ip6_moptions *im6o;
2622 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2623 * or is a divert socket, reject it.
2625 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
2626 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2627 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
2628 return (EOPNOTSUPP);
2630 switch (sopt->sopt_name) {
2631 case IPV6_MULTICAST_IF:
2632 error = in6p_set_multicast_if(inp, sopt);
2635 case IPV6_MULTICAST_HOPS: {
2638 if (sopt->sopt_valsize != sizeof(int)) {
2642 error = sooptcopyin(sopt, &hlim, sizeof(hlim), sizeof(int));
2645 if (hlim < -1 || hlim > 255) {
2648 } else if (hlim == -1) {
2649 hlim = V_ip6_defmcasthlim;
2651 im6o = in6p_findmoptions(inp);
2652 im6o->im6o_multicast_hlim = hlim;
2657 case IPV6_MULTICAST_LOOP: {
2661 * Set the loopback flag for outgoing multicast packets.
2662 * Must be zero or one.
2664 if (sopt->sopt_valsize != sizeof(u_int)) {
2668 error = sooptcopyin(sopt, &loop, sizeof(u_int), sizeof(u_int));
2675 im6o = in6p_findmoptions(inp);
2676 im6o->im6o_multicast_loop = loop;
2681 case IPV6_JOIN_GROUP:
2682 case MCAST_JOIN_GROUP:
2683 case MCAST_JOIN_SOURCE_GROUP:
2684 error = in6p_join_group(inp, sopt);
2687 case IPV6_LEAVE_GROUP:
2688 case MCAST_LEAVE_GROUP:
2689 case MCAST_LEAVE_SOURCE_GROUP:
2690 error = in6p_leave_group(inp, sopt);
2693 case MCAST_BLOCK_SOURCE:
2694 case MCAST_UNBLOCK_SOURCE:
2695 error = in6p_block_unblock_source(inp, sopt);
2699 error = in6p_set_source_filters(inp, sopt);
2707 INP_UNLOCK_ASSERT(inp);
2713 * Expose MLD's multicast filter mode and source list(s) to userland,
2714 * keyed by (ifindex, group).
2715 * The filter mode is written out as a uint32_t, followed by
2716 * 0..n of struct in6_addr.
2717 * For use by ifmcstat(8).
2718 * SMPng: NOTE: unlocked read of ifindex space.
2721 sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS)
2723 struct in6_addr mcaddr;
2724 struct in6_addr src;
2726 struct ifmultiaddr *ifma;
2727 struct in6_multi *inm;
2728 struct ip6_msource *ims;
2732 uint32_t fmode, ifindex;
2734 char ip6tbuf[INET6_ADDRSTRLEN];
2740 if (req->newptr != NULL)
2743 /* int: ifindex + 4 * 32 bits of IPv6 address */
2748 if (ifindex <= 0 || ifindex > V_if_index) {
2749 CTR2(KTR_MLD, "%s: ifindex %u out of range",
2754 memcpy(&mcaddr, &name[1], sizeof(struct in6_addr));
2755 if (!IN6_IS_ADDR_MULTICAST(&mcaddr)) {
2756 CTR2(KTR_MLD, "%s: group %s is not multicast",
2757 __func__, ip6_sprintf(ip6tbuf, &mcaddr));
2761 ifp = ifnet_byindex(ifindex);
2763 CTR2(KTR_MLD, "%s: no ifp for ifindex %u",
2768 * Internal MLD lookups require that scope/zone ID is set.
2770 (void)in6_setscope(&mcaddr, ifp, NULL);
2772 retval = sysctl_wire_old_buffer(req,
2773 sizeof(uint32_t) + (in6_mcast_maxgrpsrc * sizeof(struct in6_addr)));
2778 IN6_MULTI_LIST_LOCK();
2780 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2781 inm = in6m_ifmultiaddr_get_inm(ifma);
2784 if (!IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, &mcaddr))
2786 fmode = inm->in6m_st[1].iss_fmode;
2787 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2790 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
2791 CTR2(KTR_MLD, "%s: visit node %p", __func__, ims);
2793 * Only copy-out sources which are in-mode.
2795 if (fmode != im6s_get_mode(inm, ims, 1)) {
2796 CTR1(KTR_MLD, "%s: skip non-in-mode",
2800 src = ims->im6s_addr;
2801 retval = SYSCTL_OUT(req, &src,
2802 sizeof(struct in6_addr));
2807 IF_ADDR_RUNLOCK(ifp);
2809 IN6_MULTI_LIST_UNLOCK();
2817 static const char *in6m_modestrs[] = { "un", "in", "ex" };
2820 in6m_mode_str(const int mode)
2823 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2824 return (in6m_modestrs[mode]);
2828 static const char *in6m_statestrs[] = {
2841 in6m_state_str(const int state)
2844 if (state >= MLD_NOT_MEMBER && state <= MLD_LEAVING_MEMBER)
2845 return (in6m_statestrs[state]);
2850 * Dump an in6_multi structure to the console.
2853 in6m_print(const struct in6_multi *inm)
2856 char ip6tbuf[INET6_ADDRSTRLEN];
2858 if ((ktr_mask & KTR_MLD) == 0)
2861 printf("%s: --- begin in6m %p ---\n", __func__, inm);
2862 printf("addr %s ifp %p(%s) ifma %p\n",
2863 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2865 if_name(inm->in6m_ifp),
2867 printf("timer %u state %s refcount %u scq.len %u\n",
2869 in6m_state_str(inm->in6m_state),
2871 mbufq_len(&inm->in6m_scq));
2872 printf("mli %p nsrc %lu sctimer %u scrv %u\n",
2877 for (t = 0; t < 2; t++) {
2878 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
2879 in6m_mode_str(inm->in6m_st[t].iss_fmode),
2880 inm->in6m_st[t].iss_asm,
2881 inm->in6m_st[t].iss_ex,
2882 inm->in6m_st[t].iss_in,
2883 inm->in6m_st[t].iss_rec);
2885 printf("%s: --- end in6m %p ---\n", __func__, inm);
2891 in6m_print(const struct in6_multi *inm)