2 * Copyright (c) 2009 Bruce Simpson.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior written
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * IPv6 multicast socket, group, and socket option processing module.
32 * Normative references: RFC 2292, RFC 3492, RFC 3542, RFC 3678, RFC 3810.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_inet6.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/protosw.h>
49 #include <sys/sysctl.h>
55 #include <net/if_dl.h>
56 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet6/in6_var.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet6/ip6_var.h>
65 #include <netinet/in_pcb.h>
66 #include <netinet/tcp_var.h>
67 #include <netinet6/nd6.h>
68 #include <netinet6/mld6_var.h>
69 #include <netinet6/scope6_var.h>
72 #define KTR_MLD KTR_INET6
75 #ifndef __SOCKUNION_DECLARED
77 struct sockaddr_storage ss;
79 struct sockaddr_dl sdl;
80 struct sockaddr_in6 sin6;
82 typedef union sockunion sockunion_t;
83 #define __SOCKUNION_DECLARED
84 #endif /* __SOCKUNION_DECLARED */
86 static MALLOC_DEFINE(M_IN6MFILTER, "in6_mfilter",
87 "IPv6 multicast PCB-layer source filter");
88 static MALLOC_DEFINE(M_IP6MADDR, "in6_multi", "IPv6 multicast group");
89 static MALLOC_DEFINE(M_IP6MOPTS, "ip6_moptions", "IPv6 multicast options");
90 static MALLOC_DEFINE(M_IP6MSOURCE, "ip6_msource",
91 "IPv6 multicast MLD-layer source filter");
93 RB_GENERATE(ip6_msource_tree, ip6_msource, im6s_link, ip6_msource_cmp);
97 * - Lock order is: Giant, INP_WLOCK, IN6_MULTI_LOCK, MLD_LOCK, IF_ADDR_LOCK.
98 * - The IF_ADDR_LOCK is implicitly taken by in6m_lookup() earlier, however
99 * it can be taken by code in net/if.c also.
100 * - ip6_moptions and in6_mfilter are covered by the INP_WLOCK.
102 * struct in6_multi is covered by IN6_MULTI_LOCK. There isn't strictly
103 * any need for in6_multi itself to be virtualized -- it is bound to an ifp
104 * anyway no matter what happens.
106 struct mtx in6_multi_mtx;
107 MTX_SYSINIT(in6_multi_mtx, &in6_multi_mtx, "in6_multi_mtx", MTX_DEF);
109 static void im6f_commit(struct in6_mfilter *);
110 static int im6f_get_source(struct in6_mfilter *imf,
111 const struct sockaddr_in6 *psin,
112 struct in6_msource **);
113 static struct in6_msource *
114 im6f_graft(struct in6_mfilter *, const uint8_t,
115 const struct sockaddr_in6 *);
116 static void im6f_leave(struct in6_mfilter *);
117 static int im6f_prune(struct in6_mfilter *, const struct sockaddr_in6 *);
118 static void im6f_purge(struct in6_mfilter *);
119 static void im6f_rollback(struct in6_mfilter *);
120 static void im6f_reap(struct in6_mfilter *);
121 static int im6o_grow(struct ip6_moptions *);
122 static size_t im6o_match_group(const struct ip6_moptions *,
123 const struct ifnet *, const struct sockaddr *);
124 static struct in6_msource *
125 im6o_match_source(const struct ip6_moptions *, const size_t,
126 const struct sockaddr *);
127 static void im6s_merge(struct ip6_msource *ims,
128 const struct in6_msource *lims, const int rollback);
129 static int in6_mc_get(struct ifnet *, const struct in6_addr *,
130 struct in6_multi **);
131 static int in6m_get_source(struct in6_multi *inm,
132 const struct in6_addr *addr, const int noalloc,
133 struct ip6_msource **pims);
134 static int in6m_is_ifp_detached(const struct in6_multi *);
135 static int in6m_merge(struct in6_multi *, /*const*/ struct in6_mfilter *);
136 static void in6m_purge(struct in6_multi *);
137 static void in6m_reap(struct in6_multi *);
138 static struct ip6_moptions *
139 in6p_findmoptions(struct inpcb *);
140 static int in6p_get_source_filters(struct inpcb *, struct sockopt *);
141 static int in6p_join_group(struct inpcb *, struct sockopt *);
142 static int in6p_leave_group(struct inpcb *, struct sockopt *);
143 static struct ifnet *
144 in6p_lookup_mcast_ifp(const struct inpcb *,
145 const struct sockaddr_in6 *);
146 static int in6p_block_unblock_source(struct inpcb *, struct sockopt *);
147 static int in6p_set_multicast_if(struct inpcb *, struct sockopt *);
148 static int in6p_set_source_filters(struct inpcb *, struct sockopt *);
149 static int sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS);
151 SYSCTL_DECL(_net_inet6_ip6); /* XXX Not in any common header. */
153 SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, mcast, CTLFLAG_RW, 0, "IPv6 multicast");
155 static u_long in6_mcast_maxgrpsrc = IPV6_MAX_GROUP_SRC_FILTER;
156 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxgrpsrc,
157 CTLFLAG_RW | CTLFLAG_TUN, &in6_mcast_maxgrpsrc, 0,
158 "Max source filters per group");
159 TUNABLE_ULONG("net.inet6.ip6.mcast.maxgrpsrc", &in6_mcast_maxgrpsrc);
161 static u_long in6_mcast_maxsocksrc = IPV6_MAX_SOCK_SRC_FILTER;
162 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxsocksrc,
163 CTLFLAG_RW | CTLFLAG_TUN, &in6_mcast_maxsocksrc, 0,
164 "Max source filters per socket");
165 TUNABLE_ULONG("net.inet6.ip6.mcast.maxsocksrc", &in6_mcast_maxsocksrc);
167 /* TODO Virtualize this switch. */
168 int in6_mcast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
169 SYSCTL_INT(_net_inet6_ip6_mcast, OID_AUTO, loop, CTLFLAG_RW | CTLFLAG_TUN,
170 &in6_mcast_loop, 0, "Loopback multicast datagrams by default");
171 TUNABLE_INT("net.inet6.ip6.mcast.loop", &in6_mcast_loop);
173 SYSCTL_NODE(_net_inet6_ip6_mcast, OID_AUTO, filters,
174 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip6_mcast_filters,
175 "Per-interface stack-wide source filters");
178 * Inline function which wraps assertions for a valid ifp.
179 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
183 in6m_is_ifp_detached(const struct in6_multi *inm)
187 KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__));
188 ifp = inm->in6m_ifma->ifma_ifp;
191 * Sanity check that network-layer notion of ifp is the
192 * same as that of link-layer.
194 KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__));
197 return (ifp == NULL);
201 * Initialize an in6_mfilter structure to a known state at t0, t1
202 * with an empty source filter list.
205 im6f_init(struct in6_mfilter *imf, const int st0, const int st1)
207 memset(imf, 0, sizeof(struct in6_mfilter));
208 RB_INIT(&imf->im6f_sources);
209 imf->im6f_st[0] = st0;
210 imf->im6f_st[1] = st1;
214 * Resize the ip6_moptions vector to the next power-of-two minus 1.
215 * May be called with locks held; do not sleep.
218 im6o_grow(struct ip6_moptions *imo)
220 struct in6_multi **nmships;
221 struct in6_multi **omships;
222 struct in6_mfilter *nmfilters;
223 struct in6_mfilter *omfilters;
230 omships = imo->im6o_membership;
231 omfilters = imo->im6o_mfilters;
232 oldmax = imo->im6o_max_memberships;
233 newmax = ((oldmax + 1) * 2) - 1;
235 if (newmax <= IPV6_MAX_MEMBERSHIPS) {
236 nmships = (struct in6_multi **)realloc(omships,
237 sizeof(struct in6_multi *) * newmax, M_IP6MOPTS, M_NOWAIT);
238 nmfilters = (struct in6_mfilter *)realloc(omfilters,
239 sizeof(struct in6_mfilter) * newmax, M_IN6MFILTER,
241 if (nmships != NULL && nmfilters != NULL) {
242 /* Initialize newly allocated source filter heads. */
243 for (idx = oldmax; idx < newmax; idx++) {
244 im6f_init(&nmfilters[idx], MCAST_UNDEFINED,
247 imo->im6o_max_memberships = newmax;
248 imo->im6o_membership = nmships;
249 imo->im6o_mfilters = nmfilters;
253 if (nmships == NULL || nmfilters == NULL) {
255 free(nmships, M_IP6MOPTS);
256 if (nmfilters != NULL)
257 free(nmfilters, M_IN6MFILTER);
258 return (ETOOMANYREFS);
265 * Find an IPv6 multicast group entry for this ip6_moptions instance
266 * which matches the specified group, and optionally an interface.
267 * Return its index into the array, or -1 if not found.
270 im6o_match_group(const struct ip6_moptions *imo, const struct ifnet *ifp,
271 const struct sockaddr *group)
273 const struct sockaddr_in6 *gsin6;
274 struct in6_multi **pinm;
278 gsin6 = (const struct sockaddr_in6 *)group;
280 /* The im6o_membership array may be lazy allocated. */
281 if (imo->im6o_membership == NULL || imo->im6o_num_memberships == 0)
284 nmships = imo->im6o_num_memberships;
285 pinm = &imo->im6o_membership[0];
286 for (idx = 0; idx < nmships; idx++, pinm++) {
289 if ((ifp == NULL || ((*pinm)->in6m_ifp == ifp)) &&
290 IN6_ARE_ADDR_EQUAL(&(*pinm)->in6m_addr,
291 &gsin6->sin6_addr)) {
302 * Find an IPv6 multicast source entry for this imo which matches
303 * the given group index for this socket, and source address.
305 * XXX TODO: The scope ID, if present in src, is stripped before
306 * any comparison. We SHOULD enforce scope/zone checks where the source
307 * filter entry has a link scope.
309 * NOTE: This does not check if the entry is in-mode, merely if
310 * it exists, which may not be the desired behaviour.
312 static struct in6_msource *
313 im6o_match_source(const struct ip6_moptions *imo, const size_t gidx,
314 const struct sockaddr *src)
316 struct ip6_msource find;
317 struct in6_mfilter *imf;
318 struct ip6_msource *ims;
319 const sockunion_t *psa;
321 KASSERT(src->sa_family == AF_INET6, ("%s: !AF_INET6", __func__));
322 KASSERT(gidx != -1 && gidx < imo->im6o_num_memberships,
323 ("%s: invalid index %d\n", __func__, (int)gidx));
325 /* The im6o_mfilters array may be lazy allocated. */
326 if (imo->im6o_mfilters == NULL)
328 imf = &imo->im6o_mfilters[gidx];
330 psa = (const sockunion_t *)src;
331 find.im6s_addr = psa->sin6.sin6_addr;
332 in6_clearscope(&find.im6s_addr); /* XXX */
333 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
335 return ((struct in6_msource *)ims);
339 * Perform filtering for multicast datagrams on a socket by group and source.
341 * Returns 0 if a datagram should be allowed through, or various error codes
342 * if the socket was not a member of the group, or the source was muted, etc.
345 im6o_mc_filter(const struct ip6_moptions *imo, const struct ifnet *ifp,
346 const struct sockaddr *group, const struct sockaddr *src)
349 struct in6_msource *ims;
352 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
354 gidx = im6o_match_group(imo, ifp, group);
356 return (MCAST_NOTGMEMBER);
359 * Check if the source was included in an (S,G) join.
360 * Allow reception on exclusive memberships by default,
361 * reject reception on inclusive memberships by default.
362 * Exclude source only if an in-mode exclude filter exists.
363 * Include source only if an in-mode include filter exists.
364 * NOTE: We are comparing group state here at MLD t1 (now)
365 * with socket-layer t0 (since last downcall).
367 mode = imo->im6o_mfilters[gidx].im6f_st[1];
368 ims = im6o_match_source(imo, gidx, src);
370 if ((ims == NULL && mode == MCAST_INCLUDE) ||
371 (ims != NULL && ims->im6sl_st[0] != mode))
372 return (MCAST_NOTSMEMBER);
378 * Find and return a reference to an in6_multi record for (ifp, group),
379 * and bump its reference count.
380 * If one does not exist, try to allocate it, and update link-layer multicast
381 * filters on ifp to listen for group.
382 * Assumes the IN6_MULTI lock is held across the call.
383 * Return 0 if successful, otherwise return an appropriate error code.
386 in6_mc_get(struct ifnet *ifp, const struct in6_addr *group,
387 struct in6_multi **pinm)
389 struct sockaddr_in6 gsin6;
390 struct ifmultiaddr *ifma;
391 struct in6_multi *inm;
397 * XXX: Accesses to ifma_protospec must be covered by IF_ADDR_LOCK;
398 * if_addmulti() takes this mutex itself, so we must drop and
399 * re-acquire around the call.
401 IN6_MULTI_LOCK_ASSERT();
404 inm = in6m_lookup_locked(ifp, group);
407 * If we already joined this group, just bump the
408 * refcount and return it.
410 KASSERT(inm->in6m_refcount >= 1,
411 ("%s: bad refcount %d", __func__, inm->in6m_refcount));
412 ++inm->in6m_refcount;
417 memset(&gsin6, 0, sizeof(gsin6));
418 gsin6.sin6_family = AF_INET6;
419 gsin6.sin6_len = sizeof(struct sockaddr_in6);
420 gsin6.sin6_addr = *group;
423 * Check if a link-layer group is already associated
424 * with this network-layer group on the given ifnet.
426 IF_ADDR_WUNLOCK(ifp);
427 error = if_addmulti(ifp, (struct sockaddr *)&gsin6, &ifma);
433 * If something other than netinet6 is occupying the link-layer
434 * group, print a meaningful error message and back out of
436 * Otherwise, bump the refcount on the existing network-layer
437 * group association and return it.
439 if (ifma->ifma_protospec != NULL) {
440 inm = (struct in6_multi *)ifma->ifma_protospec;
442 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
444 KASSERT(ifma->ifma_addr->sa_family == AF_INET6,
445 ("%s: ifma not AF_INET6", __func__));
446 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
447 if (inm->in6m_ifma != ifma || inm->in6m_ifp != ifp ||
448 !IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, group))
449 panic("%s: ifma %p is inconsistent with %p (%p)",
450 __func__, ifma, inm, group);
452 ++inm->in6m_refcount;
457 IF_ADDR_WLOCK_ASSERT(ifp);
460 * A new in6_multi record is needed; allocate and initialize it.
461 * We DO NOT perform an MLD join as the in6_ layer may need to
462 * push an initial source list down to MLD to support SSM.
464 * The initial source filter state is INCLUDE, {} as per the RFC.
465 * Pending state-changes per group are subject to a bounds check.
467 inm = malloc(sizeof(*inm), M_IP6MADDR, M_NOWAIT | M_ZERO);
469 if_delmulti_ifma(ifma);
473 inm->in6m_addr = *group;
475 inm->in6m_mli = MLD_IFINFO(ifp);
476 inm->in6m_ifma = ifma;
477 inm->in6m_refcount = 1;
478 inm->in6m_state = MLD_NOT_MEMBER;
479 IFQ_SET_MAXLEN(&inm->in6m_scq, MLD_MAX_STATE_CHANGES);
481 inm->in6m_st[0].iss_fmode = MCAST_UNDEFINED;
482 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
483 RB_INIT(&inm->in6m_srcs);
485 ifma->ifma_protospec = inm;
489 IF_ADDR_WUNLOCK(ifp);
494 * Drop a reference to an in6_multi record.
496 * If the refcount drops to 0, free the in6_multi record and
497 * delete the underlying link-layer membership.
500 in6m_release_locked(struct in6_multi *inm)
502 struct ifmultiaddr *ifma;
504 IN6_MULTI_LOCK_ASSERT();
506 CTR2(KTR_MLD, "%s: refcount is %d", __func__, inm->in6m_refcount);
508 if (--inm->in6m_refcount > 0) {
509 CTR2(KTR_MLD, "%s: refcount is now %d", __func__,
514 CTR2(KTR_MLD, "%s: freeing inm %p", __func__, inm);
516 ifma = inm->in6m_ifma;
518 /* XXX this access is not covered by IF_ADDR_LOCK */
519 CTR2(KTR_MLD, "%s: purging ifma %p", __func__, ifma);
520 KASSERT(ifma->ifma_protospec == inm,
521 ("%s: ifma_protospec != inm", __func__));
522 ifma->ifma_protospec = NULL;
526 free(inm, M_IP6MADDR);
528 if_delmulti_ifma(ifma);
532 * Clear recorded source entries for a group.
533 * Used by the MLD code. Caller must hold the IN6_MULTI lock.
534 * FIXME: Should reap.
537 in6m_clear_recorded(struct in6_multi *inm)
539 struct ip6_msource *ims;
541 IN6_MULTI_LOCK_ASSERT();
543 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
546 --inm->in6m_st[1].iss_rec;
549 KASSERT(inm->in6m_st[1].iss_rec == 0,
550 ("%s: iss_rec %d not 0", __func__, inm->in6m_st[1].iss_rec));
554 * Record a source as pending for a Source-Group MLDv2 query.
555 * This lives here as it modifies the shared tree.
557 * inm is the group descriptor.
558 * naddr is the address of the source to record in network-byte order.
560 * If the net.inet6.mld.sgalloc sysctl is non-zero, we will
561 * lazy-allocate a source node in response to an SG query.
562 * Otherwise, no allocation is performed. This saves some memory
563 * with the trade-off that the source will not be reported to the
564 * router if joined in the window between the query response and
565 * the group actually being joined on the local host.
567 * VIMAGE: XXX: Currently the mld_sgalloc feature has been removed.
568 * This turns off the allocation of a recorded source entry if
569 * the group has not been joined.
571 * Return 0 if the source didn't exist or was already marked as recorded.
572 * Return 1 if the source was marked as recorded by this function.
573 * Return <0 if any error occured (negated errno code).
576 in6m_record_source(struct in6_multi *inm, const struct in6_addr *addr)
578 struct ip6_msource find;
579 struct ip6_msource *ims, *nims;
581 IN6_MULTI_LOCK_ASSERT();
583 find.im6s_addr = *addr;
584 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
585 if (ims && ims->im6s_stp)
588 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
590 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
594 nims->im6s_addr = find.im6s_addr;
595 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
601 * Mark the source as recorded and update the recorded
605 ++inm->in6m_st[1].iss_rec;
611 * Return a pointer to an in6_msource owned by an in6_mfilter,
612 * given its source address.
613 * Lazy-allocate if needed. If this is a new entry its filter state is
616 * imf is the filter set being modified.
617 * addr is the source address.
619 * SMPng: May be called with locks held; malloc must not block.
622 im6f_get_source(struct in6_mfilter *imf, const struct sockaddr_in6 *psin,
623 struct in6_msource **plims)
625 struct ip6_msource find;
626 struct ip6_msource *ims, *nims;
627 struct in6_msource *lims;
634 find.im6s_addr = psin->sin6_addr;
635 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
636 lims = (struct in6_msource *)ims;
638 if (imf->im6f_nsrc == in6_mcast_maxsocksrc)
640 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
644 lims = (struct in6_msource *)nims;
645 lims->im6s_addr = find.im6s_addr;
646 lims->im6sl_st[0] = MCAST_UNDEFINED;
647 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
657 * Graft a source entry into an existing socket-layer filter set,
658 * maintaining any required invariants and checking allocations.
660 * The source is marked as being in the new filter mode at t1.
662 * Return the pointer to the new node, otherwise return NULL.
664 static struct in6_msource *
665 im6f_graft(struct in6_mfilter *imf, const uint8_t st1,
666 const struct sockaddr_in6 *psin)
668 struct ip6_msource *nims;
669 struct in6_msource *lims;
671 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
675 lims = (struct in6_msource *)nims;
676 lims->im6s_addr = psin->sin6_addr;
677 lims->im6sl_st[0] = MCAST_UNDEFINED;
678 lims->im6sl_st[1] = st1;
679 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
686 * Prune a source entry from an existing socket-layer filter set,
687 * maintaining any required invariants and checking allocations.
689 * The source is marked as being left at t1, it is not freed.
691 * Return 0 if no error occurred, otherwise return an errno value.
694 im6f_prune(struct in6_mfilter *imf, const struct sockaddr_in6 *psin)
696 struct ip6_msource find;
697 struct ip6_msource *ims;
698 struct in6_msource *lims;
700 find.im6s_addr = psin->sin6_addr;
701 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
704 lims = (struct in6_msource *)ims;
705 lims->im6sl_st[1] = MCAST_UNDEFINED;
710 * Revert socket-layer filter set deltas at t1 to t0 state.
713 im6f_rollback(struct in6_mfilter *imf)
715 struct ip6_msource *ims, *tims;
716 struct in6_msource *lims;
718 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
719 lims = (struct in6_msource *)ims;
720 if (lims->im6sl_st[0] == lims->im6sl_st[1]) {
721 /* no change at t1 */
723 } else if (lims->im6sl_st[0] != MCAST_UNDEFINED) {
724 /* revert change to existing source at t1 */
725 lims->im6sl_st[1] = lims->im6sl_st[0];
727 /* revert source added t1 */
728 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
729 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
730 free(ims, M_IN6MFILTER);
734 imf->im6f_st[1] = imf->im6f_st[0];
738 * Mark socket-layer filter set as INCLUDE {} at t1.
741 im6f_leave(struct in6_mfilter *imf)
743 struct ip6_msource *ims;
744 struct in6_msource *lims;
746 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
747 lims = (struct in6_msource *)ims;
748 lims->im6sl_st[1] = MCAST_UNDEFINED;
750 imf->im6f_st[1] = MCAST_INCLUDE;
754 * Mark socket-layer filter set deltas as committed.
757 im6f_commit(struct in6_mfilter *imf)
759 struct ip6_msource *ims;
760 struct in6_msource *lims;
762 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
763 lims = (struct in6_msource *)ims;
764 lims->im6sl_st[0] = lims->im6sl_st[1];
766 imf->im6f_st[0] = imf->im6f_st[1];
770 * Reap unreferenced sources from socket-layer filter set.
773 im6f_reap(struct in6_mfilter *imf)
775 struct ip6_msource *ims, *tims;
776 struct in6_msource *lims;
778 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
779 lims = (struct in6_msource *)ims;
780 if ((lims->im6sl_st[0] == MCAST_UNDEFINED) &&
781 (lims->im6sl_st[1] == MCAST_UNDEFINED)) {
782 CTR2(KTR_MLD, "%s: free lims %p", __func__, ims);
783 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
784 free(ims, M_IN6MFILTER);
791 * Purge socket-layer filter set.
794 im6f_purge(struct in6_mfilter *imf)
796 struct ip6_msource *ims, *tims;
798 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
799 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
800 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
801 free(ims, M_IN6MFILTER);
804 imf->im6f_st[0] = imf->im6f_st[1] = MCAST_UNDEFINED;
805 KASSERT(RB_EMPTY(&imf->im6f_sources),
806 ("%s: im6f_sources not empty", __func__));
810 * Look up a source filter entry for a multicast group.
812 * inm is the group descriptor to work with.
813 * addr is the IPv6 address to look up.
814 * noalloc may be non-zero to suppress allocation of sources.
815 * *pims will be set to the address of the retrieved or allocated source.
817 * SMPng: NOTE: may be called with locks held.
818 * Return 0 if successful, otherwise return a non-zero error code.
821 in6m_get_source(struct in6_multi *inm, const struct in6_addr *addr,
822 const int noalloc, struct ip6_msource **pims)
824 struct ip6_msource find;
825 struct ip6_msource *ims, *nims;
827 char ip6tbuf[INET6_ADDRSTRLEN];
830 find.im6s_addr = *addr;
831 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
832 if (ims == NULL && !noalloc) {
833 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
835 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
839 nims->im6s_addr = *addr;
840 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
843 CTR3(KTR_MLD, "%s: allocated %s as %p", __func__,
844 ip6_sprintf(ip6tbuf, addr), ims);
852 * Merge socket-layer source into MLD-layer source.
853 * If rollback is non-zero, perform the inverse of the merge.
856 im6s_merge(struct ip6_msource *ims, const struct in6_msource *lims,
859 int n = rollback ? -1 : 1;
861 char ip6tbuf[INET6_ADDRSTRLEN];
863 ip6_sprintf(ip6tbuf, &lims->im6s_addr);
866 if (lims->im6sl_st[0] == MCAST_EXCLUDE) {
867 CTR3(KTR_MLD, "%s: t1 ex -= %d on %s", __func__, n, ip6tbuf);
868 ims->im6s_st[1].ex -= n;
869 } else if (lims->im6sl_st[0] == MCAST_INCLUDE) {
870 CTR3(KTR_MLD, "%s: t1 in -= %d on %s", __func__, n, ip6tbuf);
871 ims->im6s_st[1].in -= n;
874 if (lims->im6sl_st[1] == MCAST_EXCLUDE) {
875 CTR3(KTR_MLD, "%s: t1 ex += %d on %s", __func__, n, ip6tbuf);
876 ims->im6s_st[1].ex += n;
877 } else if (lims->im6sl_st[1] == MCAST_INCLUDE) {
878 CTR3(KTR_MLD, "%s: t1 in += %d on %s", __func__, n, ip6tbuf);
879 ims->im6s_st[1].in += n;
884 * Atomically update the global in6_multi state, when a membership's
885 * filter list is being updated in any way.
887 * imf is the per-inpcb-membership group filter pointer.
888 * A fake imf may be passed for in-kernel consumers.
890 * XXX This is a candidate for a set-symmetric-difference style loop
891 * which would eliminate the repeated lookup from root of ims nodes,
892 * as they share the same key space.
894 * If any error occurred this function will back out of refcounts
895 * and return a non-zero value.
898 in6m_merge(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
900 struct ip6_msource *ims, *nims;
901 struct in6_msource *lims;
910 * Update the source filters first, as this may fail.
911 * Maintain count of in-mode filters at t0, t1. These are
912 * used to work out if we transition into ASM mode or not.
913 * Maintain a count of source filters whose state was
914 * actually modified by this operation.
916 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
917 lims = (struct in6_msource *)ims;
918 if (lims->im6sl_st[0] == imf->im6f_st[0]) nsrc0++;
919 if (lims->im6sl_st[1] == imf->im6f_st[1]) nsrc1++;
920 if (lims->im6sl_st[0] == lims->im6sl_st[1]) continue;
921 error = in6m_get_source(inm, &lims->im6s_addr, 0, &nims);
925 im6s_merge(nims, lims, 0);
928 struct ip6_msource *bims;
930 RB_FOREACH_REVERSE_FROM(ims, ip6_msource_tree, nims) {
931 lims = (struct in6_msource *)ims;
932 if (lims->im6sl_st[0] == lims->im6sl_st[1])
934 (void)in6m_get_source(inm, &lims->im6s_addr, 1, &bims);
937 im6s_merge(bims, lims, 1);
942 CTR3(KTR_MLD, "%s: imf filters in-mode: %d at t0, %d at t1",
943 __func__, nsrc0, nsrc1);
945 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
946 if (imf->im6f_st[0] == imf->im6f_st[1] &&
947 imf->im6f_st[1] == MCAST_INCLUDE) {
949 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
950 --inm->in6m_st[1].iss_in;
954 /* Handle filter mode transition on socket. */
955 if (imf->im6f_st[0] != imf->im6f_st[1]) {
956 CTR3(KTR_MLD, "%s: imf transition %d to %d",
957 __func__, imf->im6f_st[0], imf->im6f_st[1]);
959 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
960 CTR1(KTR_MLD, "%s: --ex on inm at t1", __func__);
961 --inm->in6m_st[1].iss_ex;
962 } else if (imf->im6f_st[0] == MCAST_INCLUDE) {
963 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
964 --inm->in6m_st[1].iss_in;
967 if (imf->im6f_st[1] == MCAST_EXCLUDE) {
968 CTR1(KTR_MLD, "%s: ex++ on inm at t1", __func__);
969 inm->in6m_st[1].iss_ex++;
970 } else if (imf->im6f_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
971 CTR1(KTR_MLD, "%s: in++ on inm at t1", __func__);
972 inm->in6m_st[1].iss_in++;
977 * Track inm filter state in terms of listener counts.
978 * If there are any exclusive listeners, stack-wide
979 * membership is exclusive.
980 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
981 * If no listeners remain, state is undefined at t1,
982 * and the MLD lifecycle for this group should finish.
984 if (inm->in6m_st[1].iss_ex > 0) {
985 CTR1(KTR_MLD, "%s: transition to EX", __func__);
986 inm->in6m_st[1].iss_fmode = MCAST_EXCLUDE;
987 } else if (inm->in6m_st[1].iss_in > 0) {
988 CTR1(KTR_MLD, "%s: transition to IN", __func__);
989 inm->in6m_st[1].iss_fmode = MCAST_INCLUDE;
991 CTR1(KTR_MLD, "%s: transition to UNDEF", __func__);
992 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
995 /* Decrement ASM listener count on transition out of ASM mode. */
996 if (imf->im6f_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
997 if ((imf->im6f_st[1] != MCAST_EXCLUDE) ||
998 (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 > 0))
999 CTR1(KTR_MLD, "%s: --asm on inm at t1", __func__);
1000 --inm->in6m_st[1].iss_asm;
1003 /* Increment ASM listener count on transition to ASM mode. */
1004 if (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1005 CTR1(KTR_MLD, "%s: asm++ on inm at t1", __func__);
1006 inm->in6m_st[1].iss_asm++;
1009 CTR3(KTR_MLD, "%s: merged imf %p to inm %p", __func__, imf, inm);
1014 CTR1(KTR_MLD, "%s: sources changed; reaping", __func__);
1021 * Mark an in6_multi's filter set deltas as committed.
1022 * Called by MLD after a state change has been enqueued.
1025 in6m_commit(struct in6_multi *inm)
1027 struct ip6_msource *ims;
1029 CTR2(KTR_MLD, "%s: commit inm %p", __func__, inm);
1030 CTR1(KTR_MLD, "%s: pre commit:", __func__);
1033 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
1034 ims->im6s_st[0] = ims->im6s_st[1];
1036 inm->in6m_st[0] = inm->in6m_st[1];
1040 * Reap unreferenced nodes from an in6_multi's filter set.
1043 in6m_reap(struct in6_multi *inm)
1045 struct ip6_msource *ims, *tims;
1047 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1048 if (ims->im6s_st[0].ex > 0 || ims->im6s_st[0].in > 0 ||
1049 ims->im6s_st[1].ex > 0 || ims->im6s_st[1].in > 0 ||
1052 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1053 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1054 free(ims, M_IP6MSOURCE);
1060 * Purge all source nodes from an in6_multi's filter set.
1063 in6m_purge(struct in6_multi *inm)
1065 struct ip6_msource *ims, *tims;
1067 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1068 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1069 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1070 free(ims, M_IP6MSOURCE);
1076 * Join a multicast address w/o sources.
1077 * KAME compatibility entry point.
1079 * SMPng: Assume no mc locks held by caller.
1081 struct in6_multi_mship *
1082 in6_joingroup(struct ifnet *ifp, struct in6_addr *mcaddr,
1083 int *errorp, int delay)
1085 struct in6_multi_mship *imm;
1088 imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT);
1094 delay = (delay * PR_FASTHZ) / hz;
1096 error = in6_mc_join(ifp, mcaddr, NULL, &imm->i6mm_maddr, delay);
1099 free(imm, M_IP6MADDR);
1107 * Leave a multicast address w/o sources.
1108 * KAME compatibility entry point.
1110 * SMPng: Assume no mc locks held by caller.
1113 in6_leavegroup(struct in6_multi_mship *imm)
1116 if (imm->i6mm_maddr != NULL)
1117 in6_mc_leave(imm->i6mm_maddr, NULL);
1118 free(imm, M_IP6MADDR);
1123 * Join a multicast group; unlocked entry point.
1125 * SMPng: XXX: in6_mc_join() is called from in6_control() when upper
1126 * locks are not held. Fortunately, ifp is unlikely to have been detached
1127 * at this point, so we assume it's OK to recurse.
1130 in6_mc_join(struct ifnet *ifp, const struct in6_addr *mcaddr,
1131 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1137 error = in6_mc_join_locked(ifp, mcaddr, imf, pinm, delay);
1144 * Join a multicast group; real entry point.
1146 * Only preserves atomicity at inm level.
1147 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1149 * If the MLD downcall fails, the group is not joined, and an error
1153 in6_mc_join_locked(struct ifnet *ifp, const struct in6_addr *mcaddr,
1154 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1157 struct in6_mfilter timf;
1158 struct in6_multi *inm;
1161 char ip6tbuf[INET6_ADDRSTRLEN];
1166 * Sanity: Check scope zone ID was set for ifp, if and
1167 * only if group is scoped to an interface.
1169 KASSERT(IN6_IS_ADDR_MULTICAST(mcaddr),
1170 ("%s: not a multicast address", __func__));
1171 if (IN6_IS_ADDR_MC_LINKLOCAL(mcaddr) ||
1172 IN6_IS_ADDR_MC_INTFACELOCAL(mcaddr)) {
1173 KASSERT(mcaddr->s6_addr16[1] != 0,
1174 ("%s: scope zone ID not set", __func__));
1178 IN6_MULTI_LOCK_ASSERT();
1180 CTR4(KTR_MLD, "%s: join %s on %p(%s))", __func__,
1181 ip6_sprintf(ip6tbuf, mcaddr), ifp, ifp->if_xname);
1187 * If no imf was specified (i.e. kernel consumer),
1188 * fake one up and assume it is an ASM join.
1191 im6f_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1195 error = in6_mc_get(ifp, mcaddr, &inm);
1197 CTR1(KTR_MLD, "%s: in6_mc_get() failure", __func__);
1201 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1202 error = in6m_merge(inm, imf);
1204 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1205 goto out_in6m_release;
1208 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1209 error = mld_change_state(inm, delay);
1211 CTR1(KTR_MLD, "%s: failed to update source", __func__);
1212 goto out_in6m_release;
1217 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1218 in6m_release_locked(inm);
1227 * Leave a multicast group; unlocked entry point.
1230 in6_mc_leave(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1235 ifp = inm->in6m_ifp;
1238 error = in6_mc_leave_locked(inm, imf);
1245 * Leave a multicast group; real entry point.
1246 * All source filters will be expunged.
1248 * Only preserves atomicity at inm level.
1250 * Holding the write lock for the INP which contains imf
1251 * is highly advisable. We can't assert for it as imf does not
1252 * contain a back-pointer to the owning inp.
1254 * Note: This is not the same as in6m_release(*) as this function also
1255 * makes a state change downcall into MLD.
1258 in6_mc_leave_locked(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1260 struct in6_mfilter timf;
1263 char ip6tbuf[INET6_ADDRSTRLEN];
1268 IN6_MULTI_LOCK_ASSERT();
1270 CTR5(KTR_MLD, "%s: leave inm %p, %s/%s, imf %p", __func__,
1271 inm, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1272 (in6m_is_ifp_detached(inm) ? "null" : inm->in6m_ifp->if_xname),
1276 * If no imf was specified (i.e. kernel consumer),
1277 * fake one up and assume it is an ASM join.
1280 im6f_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1285 * Begin state merge transaction at MLD layer.
1287 * As this particular invocation should not cause any memory
1288 * to be allocated, and there is no opportunity to roll back
1289 * the transaction, it MUST NOT fail.
1291 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1292 error = in6m_merge(inm, imf);
1293 KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1295 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1296 error = mld_change_state(inm, 0);
1298 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1300 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1301 in6m_release_locked(inm);
1307 * Block or unblock an ASM multicast source on an inpcb.
1308 * This implements the delta-based API described in RFC 3678.
1310 * The delta-based API applies only to exclusive-mode memberships.
1311 * An MLD downcall will be performed.
1313 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1315 * Return 0 if successful, otherwise return an appropriate error code.
1318 in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1320 struct group_source_req gsr;
1321 sockunion_t *gsa, *ssa;
1323 struct in6_mfilter *imf;
1324 struct ip6_moptions *imo;
1325 struct in6_msource *ims;
1326 struct in6_multi *inm;
1331 char ip6tbuf[INET6_ADDRSTRLEN];
1338 memset(&gsr, 0, sizeof(struct group_source_req));
1339 gsa = (sockunion_t *)&gsr.gsr_group;
1340 ssa = (sockunion_t *)&gsr.gsr_source;
1342 switch (sopt->sopt_name) {
1343 case MCAST_BLOCK_SOURCE:
1344 case MCAST_UNBLOCK_SOURCE:
1345 error = sooptcopyin(sopt, &gsr,
1346 sizeof(struct group_source_req),
1347 sizeof(struct group_source_req));
1351 if (gsa->sin6.sin6_family != AF_INET6 ||
1352 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1355 if (ssa->sin6.sin6_family != AF_INET6 ||
1356 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1359 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1360 return (EADDRNOTAVAIL);
1362 ifp = ifnet_byindex(gsr.gsr_interface);
1364 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1369 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1370 __func__, sopt->sopt_name);
1371 return (EOPNOTSUPP);
1375 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1378 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1381 * Check if we are actually a member of this group.
1383 imo = in6p_findmoptions(inp);
1384 idx = im6o_match_group(imo, ifp, &gsa->sa);
1385 if (idx == -1 || imo->im6o_mfilters == NULL) {
1386 error = EADDRNOTAVAIL;
1387 goto out_in6p_locked;
1390 KASSERT(imo->im6o_mfilters != NULL,
1391 ("%s: im6o_mfilters not allocated", __func__));
1392 imf = &imo->im6o_mfilters[idx];
1393 inm = imo->im6o_membership[idx];
1396 * Attempting to use the delta-based API on an
1397 * non exclusive-mode membership is an error.
1399 fmode = imf->im6f_st[0];
1400 if (fmode != MCAST_EXCLUDE) {
1402 goto out_in6p_locked;
1406 * Deal with error cases up-front:
1407 * Asked to block, but already blocked; or
1408 * Asked to unblock, but nothing to unblock.
1409 * If adding a new block entry, allocate it.
1411 ims = im6o_match_source(imo, idx, &ssa->sa);
1412 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1413 CTR3(KTR_MLD, "%s: source %s %spresent", __func__,
1414 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
1415 doblock ? "" : "not ");
1416 error = EADDRNOTAVAIL;
1417 goto out_in6p_locked;
1420 INP_WLOCK_ASSERT(inp);
1423 * Begin state merge transaction at socket layer.
1426 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
1427 ims = im6f_graft(imf, fmode, &ssa->sin6);
1431 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
1432 error = im6f_prune(imf, &ssa->sin6);
1436 CTR1(KTR_MLD, "%s: merge imf state failed", __func__);
1437 goto out_im6f_rollback;
1441 * Begin state merge transaction at MLD layer.
1445 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1446 error = in6m_merge(inm, imf);
1448 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1450 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1451 error = mld_change_state(inm, 0);
1453 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1472 * Given an inpcb, return its multicast options structure pointer. Accepts
1473 * an unlocked inpcb pointer, but will return it locked. May sleep.
1475 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1476 * SMPng: NOTE: Returns with the INP write lock held.
1478 static struct ip6_moptions *
1479 in6p_findmoptions(struct inpcb *inp)
1481 struct ip6_moptions *imo;
1482 struct in6_multi **immp;
1483 struct in6_mfilter *imfp;
1487 if (inp->in6p_moptions != NULL)
1488 return (inp->in6p_moptions);
1492 imo = malloc(sizeof(*imo), M_IP6MOPTS, M_WAITOK);
1493 immp = malloc(sizeof(*immp) * IPV6_MIN_MEMBERSHIPS, M_IP6MOPTS,
1495 imfp = malloc(sizeof(struct in6_mfilter) * IPV6_MIN_MEMBERSHIPS,
1496 M_IN6MFILTER, M_WAITOK);
1498 imo->im6o_multicast_ifp = NULL;
1499 imo->im6o_multicast_hlim = V_ip6_defmcasthlim;
1500 imo->im6o_multicast_loop = in6_mcast_loop;
1501 imo->im6o_num_memberships = 0;
1502 imo->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1503 imo->im6o_membership = immp;
1505 /* Initialize per-group source filters. */
1506 for (idx = 0; idx < IPV6_MIN_MEMBERSHIPS; idx++)
1507 im6f_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE);
1508 imo->im6o_mfilters = imfp;
1511 if (inp->in6p_moptions != NULL) {
1512 free(imfp, M_IN6MFILTER);
1513 free(immp, M_IP6MOPTS);
1514 free(imo, M_IP6MOPTS);
1515 return (inp->in6p_moptions);
1517 inp->in6p_moptions = imo;
1522 * Discard the IPv6 multicast options (and source filters).
1524 * SMPng: NOTE: assumes INP write lock is held.
1527 ip6_freemoptions(struct ip6_moptions *imo)
1529 struct in6_mfilter *imf;
1530 size_t idx, nmships;
1532 KASSERT(imo != NULL, ("%s: ip6_moptions is NULL", __func__));
1534 nmships = imo->im6o_num_memberships;
1535 for (idx = 0; idx < nmships; ++idx) {
1536 imf = imo->im6o_mfilters ? &imo->im6o_mfilters[idx] : NULL;
1539 /* XXX this will thrash the lock(s) */
1540 (void)in6_mc_leave(imo->im6o_membership[idx], imf);
1545 if (imo->im6o_mfilters)
1546 free(imo->im6o_mfilters, M_IN6MFILTER);
1547 free(imo->im6o_membership, M_IP6MOPTS);
1548 free(imo, M_IP6MOPTS);
1552 * Atomically get source filters on a socket for an IPv6 multicast group.
1553 * Called with INP lock held; returns with lock released.
1556 in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1558 struct __msfilterreq msfr;
1561 struct ip6_moptions *imo;
1562 struct in6_mfilter *imf;
1563 struct ip6_msource *ims;
1564 struct in6_msource *lims;
1565 struct sockaddr_in6 *psin;
1566 struct sockaddr_storage *ptss;
1567 struct sockaddr_storage *tss;
1569 size_t idx, nsrcs, ncsrcs;
1571 INP_WLOCK_ASSERT(inp);
1573 imo = inp->in6p_moptions;
1574 KASSERT(imo != NULL, ("%s: null ip6_moptions", __func__));
1578 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1579 sizeof(struct __msfilterreq));
1583 if (msfr.msfr_group.ss_family != AF_INET6 ||
1584 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
1587 gsa = (sockunion_t *)&msfr.msfr_group;
1588 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1591 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
1592 return (EADDRNOTAVAIL);
1593 ifp = ifnet_byindex(msfr.msfr_ifindex);
1595 return (EADDRNOTAVAIL);
1596 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1601 * Lookup group on the socket.
1603 idx = im6o_match_group(imo, ifp, &gsa->sa);
1604 if (idx == -1 || imo->im6o_mfilters == NULL) {
1606 return (EADDRNOTAVAIL);
1608 imf = &imo->im6o_mfilters[idx];
1611 * Ignore memberships which are in limbo.
1613 if (imf->im6f_st[1] == MCAST_UNDEFINED) {
1617 msfr.msfr_fmode = imf->im6f_st[1];
1620 * If the user specified a buffer, copy out the source filter
1621 * entries to userland gracefully.
1622 * We only copy out the number of entries which userland
1623 * has asked for, but we always tell userland how big the
1624 * buffer really needs to be.
1626 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
1627 msfr.msfr_nsrcs = in6_mcast_maxsocksrc;
1629 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1630 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1631 M_TEMP, M_NOWAIT | M_ZERO);
1639 * Count number of sources in-mode at t0.
1640 * If buffer space exists and remains, copy out source entries.
1642 nsrcs = msfr.msfr_nsrcs;
1645 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
1646 lims = (struct in6_msource *)ims;
1647 if (lims->im6sl_st[0] == MCAST_UNDEFINED ||
1648 lims->im6sl_st[0] != imf->im6f_st[0])
1651 if (tss != NULL && nsrcs > 0) {
1652 psin = (struct sockaddr_in6 *)ptss;
1653 psin->sin6_family = AF_INET6;
1654 psin->sin6_len = sizeof(struct sockaddr_in6);
1655 psin->sin6_addr = lims->im6s_addr;
1656 psin->sin6_port = 0;
1665 error = copyout(tss, msfr.msfr_srcs,
1666 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1672 msfr.msfr_nsrcs = ncsrcs;
1673 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1679 * Return the IP multicast options in response to user getsockopt().
1682 ip6_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1684 struct ip6_moptions *im6o;
1689 im6o = inp->in6p_moptions;
1691 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1692 * or is a divert socket, reject it.
1694 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1695 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1696 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
1698 return (EOPNOTSUPP);
1702 switch (sopt->sopt_name) {
1703 case IPV6_MULTICAST_IF:
1704 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) {
1707 optval = im6o->im6o_multicast_ifp->if_index;
1710 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1713 case IPV6_MULTICAST_HOPS:
1715 optval = V_ip6_defmcasthlim;
1717 optval = im6o->im6o_multicast_hlim;
1719 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1722 case IPV6_MULTICAST_LOOP:
1724 optval = in6_mcast_loop; /* XXX VIMAGE */
1726 optval = im6o->im6o_multicast_loop;
1728 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1733 error = EADDRNOTAVAIL;
1736 error = in6p_get_source_filters(inp, sopt);
1742 error = ENOPROTOOPT;
1746 INP_UNLOCK_ASSERT(inp);
1752 * Look up the ifnet to use for a multicast group membership,
1753 * given the address of an IPv6 group.
1755 * This routine exists to support legacy IPv6 multicast applications.
1757 * If inp is non-NULL, use this socket's current FIB number for any
1758 * required FIB lookup. Look up the group address in the unicast FIB,
1759 * and use its ifp; usually, this points to the default next-hop.
1760 * If the FIB lookup fails, return NULL.
1762 * FUTURE: Support multiple forwarding tables for IPv6.
1764 * Returns NULL if no ifp could be found.
1766 static struct ifnet *
1767 in6p_lookup_mcast_ifp(const struct inpcb *in6p,
1768 const struct sockaddr_in6 *gsin6)
1770 struct route_in6 ro6;
1773 KASSERT(in6p->inp_vflag & INP_IPV6,
1774 ("%s: not INP_IPV6 inpcb", __func__));
1775 KASSERT(gsin6->sin6_family == AF_INET6,
1776 ("%s: not AF_INET6 group", __func__));
1777 KASSERT(IN6_IS_ADDR_MULTICAST(&gsin6->sin6_addr),
1778 ("%s: not multicast", __func__));
1781 memset(&ro6, 0, sizeof(struct route_in6));
1782 memcpy(&ro6.ro_dst, gsin6, sizeof(struct sockaddr_in6));
1783 rtalloc_ign_fib((struct route *)&ro6, 0,
1784 in6p ? in6p->inp_inc.inc_fibnum : RT_DEFAULT_FIB);
1785 if (ro6.ro_rt != NULL) {
1786 ifp = ro6.ro_rt->rt_ifp;
1787 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
1795 * Join an IPv6 multicast group, possibly with a source.
1797 * FIXME: The KAME use of the unspecified address (::)
1798 * to join *all* multicast groups is currently unsupported.
1801 in6p_join_group(struct inpcb *inp, struct sockopt *sopt)
1803 struct group_source_req gsr;
1804 sockunion_t *gsa, *ssa;
1806 struct in6_mfilter *imf;
1807 struct ip6_moptions *imo;
1808 struct in6_multi *inm;
1809 struct in6_msource *lims;
1819 memset(&gsr, 0, sizeof(struct group_source_req));
1820 gsa = (sockunion_t *)&gsr.gsr_group;
1821 gsa->ss.ss_family = AF_UNSPEC;
1822 ssa = (sockunion_t *)&gsr.gsr_source;
1823 ssa->ss.ss_family = AF_UNSPEC;
1826 * Chew everything into struct group_source_req.
1827 * Overwrite the port field if present, as the sockaddr
1828 * being copied in may be matched with a binary comparison.
1829 * Ignore passed-in scope ID.
1831 switch (sopt->sopt_name) {
1832 case IPV6_JOIN_GROUP: {
1833 struct ipv6_mreq mreq;
1835 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
1836 sizeof(struct ipv6_mreq));
1840 gsa->sin6.sin6_family = AF_INET6;
1841 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
1842 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
1844 if (mreq.ipv6mr_interface == 0) {
1845 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
1847 if (mreq.ipv6mr_interface < 0 ||
1848 V_if_index < mreq.ipv6mr_interface)
1849 return (EADDRNOTAVAIL);
1850 ifp = ifnet_byindex(mreq.ipv6mr_interface);
1852 CTR3(KTR_MLD, "%s: ipv6mr_interface = %d, ifp = %p",
1853 __func__, mreq.ipv6mr_interface, ifp);
1856 case MCAST_JOIN_GROUP:
1857 case MCAST_JOIN_SOURCE_GROUP:
1858 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1859 error = sooptcopyin(sopt, &gsr,
1860 sizeof(struct group_req),
1861 sizeof(struct group_req));
1862 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1863 error = sooptcopyin(sopt, &gsr,
1864 sizeof(struct group_source_req),
1865 sizeof(struct group_source_req));
1870 if (gsa->sin6.sin6_family != AF_INET6 ||
1871 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1874 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1875 if (ssa->sin6.sin6_family != AF_INET6 ||
1876 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1878 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
1881 * TODO: Validate embedded scope ID in source
1882 * list entry against passed-in ifp, if and only
1883 * if source list filter entry is iface or node local.
1885 in6_clearscope(&ssa->sin6.sin6_addr);
1886 ssa->sin6.sin6_port = 0;
1887 ssa->sin6.sin6_scope_id = 0;
1890 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1891 return (EADDRNOTAVAIL);
1892 ifp = ifnet_byindex(gsr.gsr_interface);
1896 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1897 __func__, sopt->sopt_name);
1898 return (EOPNOTSUPP);
1902 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1905 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
1906 return (EADDRNOTAVAIL);
1908 gsa->sin6.sin6_port = 0;
1909 gsa->sin6.sin6_scope_id = 0;
1912 * Always set the scope zone ID on memberships created from userland.
1913 * Use the passed-in ifp to do this.
1914 * XXX The in6_setscope() return value is meaningless.
1915 * XXX SCOPE6_LOCK() is taken by in6_setscope().
1917 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1919 imo = in6p_findmoptions(inp);
1920 idx = im6o_match_group(imo, ifp, &gsa->sa);
1924 inm = imo->im6o_membership[idx];
1925 imf = &imo->im6o_mfilters[idx];
1926 if (ssa->ss.ss_family != AF_UNSPEC) {
1928 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
1929 * is an error. On an existing inclusive membership,
1930 * it just adds the source to the filter list.
1932 if (imf->im6f_st[1] != MCAST_INCLUDE) {
1934 goto out_in6p_locked;
1937 * Throw out duplicates.
1939 * XXX FIXME: This makes a naive assumption that
1940 * even if entries exist for *ssa in this imf,
1941 * they will be rejected as dupes, even if they
1942 * are not valid in the current mode (in-mode).
1944 * in6_msource is transactioned just as for anything
1945 * else in SSM -- but note naive use of in6m_graft()
1946 * below for allocating new filter entries.
1948 * This is only an issue if someone mixes the
1949 * full-state SSM API with the delta-based API,
1950 * which is discouraged in the relevant RFCs.
1952 lims = im6o_match_source(imo, idx, &ssa->sa);
1953 if (lims != NULL /*&&
1954 lims->im6sl_st[1] == MCAST_INCLUDE*/) {
1955 error = EADDRNOTAVAIL;
1956 goto out_in6p_locked;
1960 * MCAST_JOIN_GROUP alone, on any existing membership,
1961 * is rejected, to stop the same inpcb tying up
1962 * multiple refs to the in_multi.
1963 * On an existing inclusive membership, this is also
1964 * an error; if you want to change filter mode,
1965 * you must use the userland API setsourcefilter().
1966 * XXX We don't reject this for imf in UNDEFINED
1967 * state at t1, because allocation of a filter
1968 * is atomic with allocation of a membership.
1971 goto out_in6p_locked;
1976 * Begin state merge transaction at socket layer.
1978 INP_WLOCK_ASSERT(inp);
1981 if (imo->im6o_num_memberships == imo->im6o_max_memberships) {
1982 error = im6o_grow(imo);
1984 goto out_in6p_locked;
1987 * Allocate the new slot upfront so we can deal with
1988 * grafting the new source filter in same code path
1989 * as for join-source on existing membership.
1991 idx = imo->im6o_num_memberships;
1992 imo->im6o_membership[idx] = NULL;
1993 imo->im6o_num_memberships++;
1994 KASSERT(imo->im6o_mfilters != NULL,
1995 ("%s: im6f_mfilters vector was not allocated", __func__));
1996 imf = &imo->im6o_mfilters[idx];
1997 KASSERT(RB_EMPTY(&imf->im6f_sources),
1998 ("%s: im6f_sources not empty", __func__));
2002 * Graft new source into filter list for this inpcb's
2003 * membership of the group. The in6_multi may not have
2004 * been allocated yet if this is a new membership, however,
2005 * the in_mfilter slot will be allocated and must be initialized.
2007 * Note: Grafting of exclusive mode filters doesn't happen
2009 * XXX: Should check for non-NULL lims (node exists but may
2010 * not be in-mode) for interop with full-state API.
2012 if (ssa->ss.ss_family != AF_UNSPEC) {
2013 /* Membership starts in IN mode */
2015 CTR1(KTR_MLD, "%s: new join w/source", __func__);
2016 im6f_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE);
2018 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
2020 lims = im6f_graft(imf, MCAST_INCLUDE, &ssa->sin6);
2022 CTR1(KTR_MLD, "%s: merge imf state failed",
2028 /* No address specified; Membership starts in EX mode */
2030 CTR1(KTR_MLD, "%s: new join w/o source", __func__);
2031 im6f_init(imf, MCAST_UNDEFINED, MCAST_EXCLUDE);
2036 * Begin state merge transaction at MLD layer.
2041 error = in6_mc_join_locked(ifp, &gsa->sin6.sin6_addr, imf,
2047 imo->im6o_membership[idx] = inm;
2049 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2050 error = in6m_merge(inm, imf);
2052 CTR1(KTR_MLD, "%s: failed to merge inm state",
2055 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2056 error = mld_change_state(inm, 0);
2058 CTR1(KTR_MLD, "%s: failed mld downcall",
2064 INP_WLOCK_ASSERT(inp);
2076 if (error && is_new) {
2077 imo->im6o_membership[idx] = NULL;
2078 --imo->im6o_num_memberships;
2087 * Leave an IPv6 multicast group on an inpcb, possibly with a source.
2090 in6p_leave_group(struct inpcb *inp, struct sockopt *sopt)
2092 struct ipv6_mreq mreq;
2093 struct group_source_req gsr;
2094 sockunion_t *gsa, *ssa;
2096 struct in6_mfilter *imf;
2097 struct ip6_moptions *imo;
2098 struct in6_msource *ims;
2099 struct in6_multi *inm;
2102 int error, is_final;
2104 char ip6tbuf[INET6_ADDRSTRLEN];
2112 memset(&gsr, 0, sizeof(struct group_source_req));
2113 gsa = (sockunion_t *)&gsr.gsr_group;
2114 gsa->ss.ss_family = AF_UNSPEC;
2115 ssa = (sockunion_t *)&gsr.gsr_source;
2116 ssa->ss.ss_family = AF_UNSPEC;
2119 * Chew everything passed in up into a struct group_source_req
2120 * as that is easier to process.
2121 * Note: Any embedded scope ID in the multicast group passed
2122 * in by userland is ignored, the interface index is the recommended
2123 * mechanism to specify an interface; see below.
2125 switch (sopt->sopt_name) {
2126 case IPV6_LEAVE_GROUP:
2127 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
2128 sizeof(struct ipv6_mreq));
2131 gsa->sin6.sin6_family = AF_INET6;
2132 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
2133 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
2134 gsa->sin6.sin6_port = 0;
2135 gsa->sin6.sin6_scope_id = 0;
2136 ifindex = mreq.ipv6mr_interface;
2139 case MCAST_LEAVE_GROUP:
2140 case MCAST_LEAVE_SOURCE_GROUP:
2141 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2142 error = sooptcopyin(sopt, &gsr,
2143 sizeof(struct group_req),
2144 sizeof(struct group_req));
2145 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2146 error = sooptcopyin(sopt, &gsr,
2147 sizeof(struct group_source_req),
2148 sizeof(struct group_source_req));
2153 if (gsa->sin6.sin6_family != AF_INET6 ||
2154 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2156 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2157 if (ssa->sin6.sin6_family != AF_INET6 ||
2158 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2160 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
2163 * TODO: Validate embedded scope ID in source
2164 * list entry against passed-in ifp, if and only
2165 * if source list filter entry is iface or node local.
2167 in6_clearscope(&ssa->sin6.sin6_addr);
2169 gsa->sin6.sin6_port = 0;
2170 gsa->sin6.sin6_scope_id = 0;
2171 ifindex = gsr.gsr_interface;
2175 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
2176 __func__, sopt->sopt_name);
2177 return (EOPNOTSUPP);
2181 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2185 * Validate interface index if provided. If no interface index
2186 * was provided separately, attempt to look the membership up
2187 * from the default scope as a last resort to disambiguate
2188 * the membership we are being asked to leave.
2189 * XXX SCOPE6 lock potentially taken here.
2192 if (ifindex < 0 || V_if_index < ifindex)
2193 return (EADDRNOTAVAIL);
2194 ifp = ifnet_byindex(ifindex);
2196 return (EADDRNOTAVAIL);
2197 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2199 error = sa6_embedscope(&gsa->sin6, V_ip6_use_defzone);
2201 return (EADDRNOTAVAIL);
2203 * Some badly behaved applications don't pass an ifindex
2204 * or a scope ID, which is an API violation. In this case,
2205 * perform a lookup as per a v6 join.
2207 * XXX For now, stomp on zone ID for the corner case.
2208 * This is not the 'KAME way', but we need to see the ifp
2209 * directly until such time as this implementation is
2210 * refactored, assuming the scope IDs are the way to go.
2212 ifindex = ntohs(gsa->sin6.sin6_addr.s6_addr16[1]);
2214 CTR2(KTR_MLD, "%s: warning: no ifindex, looking up "
2215 "ifp for group %s.", __func__,
2216 ip6_sprintf(ip6tbuf, &gsa->sin6.sin6_addr));
2217 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
2219 ifp = ifnet_byindex(ifindex);
2222 return (EADDRNOTAVAIL);
2225 CTR2(KTR_MLD, "%s: ifp = %p", __func__, ifp);
2226 KASSERT(ifp != NULL, ("%s: ifp did not resolve", __func__));
2229 * Find the membership in the membership array.
2231 imo = in6p_findmoptions(inp);
2232 idx = im6o_match_group(imo, ifp, &gsa->sa);
2234 error = EADDRNOTAVAIL;
2235 goto out_in6p_locked;
2237 inm = imo->im6o_membership[idx];
2238 imf = &imo->im6o_mfilters[idx];
2240 if (ssa->ss.ss_family != AF_UNSPEC)
2244 * Begin state merge transaction at socket layer.
2246 INP_WLOCK_ASSERT(inp);
2249 * If we were instructed only to leave a given source, do so.
2250 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2255 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
2256 error = EADDRNOTAVAIL;
2257 goto out_in6p_locked;
2259 ims = im6o_match_source(imo, idx, &ssa->sa);
2261 CTR3(KTR_MLD, "%s: source %p %spresent", __func__,
2262 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
2264 error = EADDRNOTAVAIL;
2265 goto out_in6p_locked;
2267 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
2268 error = im6f_prune(imf, &ssa->sin6);
2270 CTR1(KTR_MLD, "%s: merge imf state failed",
2272 goto out_in6p_locked;
2277 * Begin state merge transaction at MLD layer.
2283 * Give up the multicast address record to which
2284 * the membership points.
2286 (void)in6_mc_leave_locked(inm, imf);
2288 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2289 error = in6m_merge(inm, imf);
2291 CTR1(KTR_MLD, "%s: failed to merge inm state",
2294 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2295 error = mld_change_state(inm, 0);
2297 CTR1(KTR_MLD, "%s: failed mld downcall",
2312 /* Remove the gap in the membership array. */
2313 for (++idx; idx < imo->im6o_num_memberships; ++idx) {
2314 imo->im6o_membership[idx-1] = imo->im6o_membership[idx];
2315 imo->im6o_mfilters[idx-1] = imo->im6o_mfilters[idx];
2317 imo->im6o_num_memberships--;
2326 * Select the interface for transmitting IPv6 multicast datagrams.
2328 * Either an instance of struct in6_addr or an instance of struct ipv6_mreqn
2329 * may be passed to this socket option. An address of in6addr_any or an
2330 * interface index of 0 is used to remove a previous selection.
2331 * When no interface is selected, one is chosen for every send.
2334 in6p_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2337 struct ip6_moptions *imo;
2341 if (sopt->sopt_valsize != sizeof(u_int))
2344 error = sooptcopyin(sopt, &ifindex, sizeof(u_int), sizeof(u_int));
2347 if (ifindex < 0 || V_if_index < ifindex)
2350 ifp = ifnet_byindex(ifindex);
2351 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
2352 return (EADDRNOTAVAIL);
2354 imo = in6p_findmoptions(inp);
2355 imo->im6o_multicast_ifp = ifp;
2362 * Atomically set source filters on a socket for an IPv6 multicast group.
2364 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
2367 in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2369 struct __msfilterreq msfr;
2372 struct in6_mfilter *imf;
2373 struct ip6_moptions *imo;
2374 struct in6_multi *inm;
2378 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2379 sizeof(struct __msfilterreq));
2383 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
2386 if (msfr.msfr_fmode != MCAST_EXCLUDE &&
2387 msfr.msfr_fmode != MCAST_INCLUDE)
2390 if (msfr.msfr_group.ss_family != AF_INET6 ||
2391 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
2394 gsa = (sockunion_t *)&msfr.msfr_group;
2395 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2398 gsa->sin6.sin6_port = 0; /* ignore port */
2400 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
2401 return (EADDRNOTAVAIL);
2402 ifp = ifnet_byindex(msfr.msfr_ifindex);
2404 return (EADDRNOTAVAIL);
2405 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2408 * Take the INP write lock.
2409 * Check if this socket is a member of this group.
2411 imo = in6p_findmoptions(inp);
2412 idx = im6o_match_group(imo, ifp, &gsa->sa);
2413 if (idx == -1 || imo->im6o_mfilters == NULL) {
2414 error = EADDRNOTAVAIL;
2415 goto out_in6p_locked;
2417 inm = imo->im6o_membership[idx];
2418 imf = &imo->im6o_mfilters[idx];
2421 * Begin state merge transaction at socket layer.
2423 INP_WLOCK_ASSERT(inp);
2425 imf->im6f_st[1] = msfr.msfr_fmode;
2428 * Apply any new source filters, if present.
2429 * Make a copy of the user-space source vector so
2430 * that we may copy them with a single copyin. This
2431 * allows us to deal with page faults up-front.
2433 if (msfr.msfr_nsrcs > 0) {
2434 struct in6_msource *lims;
2435 struct sockaddr_in6 *psin;
2436 struct sockaddr_storage *kss, *pkss;
2441 CTR2(KTR_MLD, "%s: loading %lu source list entries",
2442 __func__, (unsigned long)msfr.msfr_nsrcs);
2443 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2445 error = copyin(msfr.msfr_srcs, kss,
2446 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2455 * Mark all source filters as UNDEFINED at t1.
2456 * Restore new group filter mode, as im6f_leave()
2457 * will set it to INCLUDE.
2460 imf->im6f_st[1] = msfr.msfr_fmode;
2463 * Update socket layer filters at t1, lazy-allocating
2464 * new entries. This saves a bunch of memory at the
2465 * cost of one RB_FIND() per source entry; duplicate
2466 * entries in the msfr_nsrcs vector are ignored.
2467 * If we encounter an error, rollback transaction.
2469 * XXX This too could be replaced with a set-symmetric
2470 * difference like loop to avoid walking from root
2471 * every time, as the key space is common.
2473 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2474 psin = (struct sockaddr_in6 *)pkss;
2475 if (psin->sin6_family != AF_INET6) {
2476 error = EAFNOSUPPORT;
2479 if (psin->sin6_len != sizeof(struct sockaddr_in6)) {
2483 if (IN6_IS_ADDR_MULTICAST(&psin->sin6_addr)) {
2488 * TODO: Validate embedded scope ID in source
2489 * list entry against passed-in ifp, if and only
2490 * if source list filter entry is iface or node local.
2492 in6_clearscope(&psin->sin6_addr);
2493 error = im6f_get_source(imf, psin, &lims);
2496 lims->im6sl_st[1] = imf->im6f_st[1];
2502 goto out_im6f_rollback;
2504 INP_WLOCK_ASSERT(inp);
2508 * Begin state merge transaction at MLD layer.
2510 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2511 error = in6m_merge(inm, imf);
2513 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
2515 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2516 error = mld_change_state(inm, 0);
2518 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
2537 * Set the IP multicast options in response to user setsockopt().
2539 * Many of the socket options handled in this function duplicate the
2540 * functionality of socket options in the regular unicast API. However,
2541 * it is not possible to merge the duplicate code, because the idempotence
2542 * of the IPv6 multicast part of the BSD Sockets API must be preserved;
2543 * the effects of these options must be treated as separate and distinct.
2545 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2548 ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2550 struct ip6_moptions *im6o;
2556 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2557 * or is a divert socket, reject it.
2559 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
2560 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2561 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
2562 return (EOPNOTSUPP);
2564 switch (sopt->sopt_name) {
2565 case IPV6_MULTICAST_IF:
2566 error = in6p_set_multicast_if(inp, sopt);
2569 case IPV6_MULTICAST_HOPS: {
2572 if (sopt->sopt_valsize != sizeof(int)) {
2576 error = sooptcopyin(sopt, &hlim, sizeof(hlim), sizeof(int));
2579 if (hlim < -1 || hlim > 255) {
2582 } else if (hlim == -1) {
2583 hlim = V_ip6_defmcasthlim;
2585 im6o = in6p_findmoptions(inp);
2586 im6o->im6o_multicast_hlim = hlim;
2591 case IPV6_MULTICAST_LOOP: {
2595 * Set the loopback flag for outgoing multicast packets.
2596 * Must be zero or one.
2598 if (sopt->sopt_valsize != sizeof(u_int)) {
2602 error = sooptcopyin(sopt, &loop, sizeof(u_int), sizeof(u_int));
2609 im6o = in6p_findmoptions(inp);
2610 im6o->im6o_multicast_loop = loop;
2615 case IPV6_JOIN_GROUP:
2616 case MCAST_JOIN_GROUP:
2617 case MCAST_JOIN_SOURCE_GROUP:
2618 error = in6p_join_group(inp, sopt);
2621 case IPV6_LEAVE_GROUP:
2622 case MCAST_LEAVE_GROUP:
2623 case MCAST_LEAVE_SOURCE_GROUP:
2624 error = in6p_leave_group(inp, sopt);
2627 case MCAST_BLOCK_SOURCE:
2628 case MCAST_UNBLOCK_SOURCE:
2629 error = in6p_block_unblock_source(inp, sopt);
2633 error = in6p_set_source_filters(inp, sopt);
2641 INP_UNLOCK_ASSERT(inp);
2647 * Expose MLD's multicast filter mode and source list(s) to userland,
2648 * keyed by (ifindex, group).
2649 * The filter mode is written out as a uint32_t, followed by
2650 * 0..n of struct in6_addr.
2651 * For use by ifmcstat(8).
2652 * SMPng: NOTE: unlocked read of ifindex space.
2655 sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS)
2657 struct in6_addr mcaddr;
2658 struct in6_addr src;
2660 struct ifmultiaddr *ifma;
2661 struct in6_multi *inm;
2662 struct ip6_msource *ims;
2666 uint32_t fmode, ifindex;
2668 char ip6tbuf[INET6_ADDRSTRLEN];
2674 if (req->newptr != NULL)
2677 /* int: ifindex + 4 * 32 bits of IPv6 address */
2682 if (ifindex <= 0 || ifindex > V_if_index) {
2683 CTR2(KTR_MLD, "%s: ifindex %u out of range",
2688 memcpy(&mcaddr, &name[1], sizeof(struct in6_addr));
2689 if (!IN6_IS_ADDR_MULTICAST(&mcaddr)) {
2690 CTR2(KTR_MLD, "%s: group %s is not multicast",
2691 __func__, ip6_sprintf(ip6tbuf, &mcaddr));
2695 ifp = ifnet_byindex(ifindex);
2697 CTR2(KTR_MLD, "%s: no ifp for ifindex %u",
2702 * Internal MLD lookups require that scope/zone ID is set.
2704 (void)in6_setscope(&mcaddr, ifp, NULL);
2706 retval = sysctl_wire_old_buffer(req,
2707 sizeof(uint32_t) + (in6_mcast_maxgrpsrc * sizeof(struct in6_addr)));
2714 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2715 if (ifma->ifma_addr->sa_family != AF_INET6 ||
2716 ifma->ifma_protospec == NULL)
2718 inm = (struct in6_multi *)ifma->ifma_protospec;
2719 if (!IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, &mcaddr))
2721 fmode = inm->in6m_st[1].iss_fmode;
2722 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2725 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
2726 CTR2(KTR_MLD, "%s: visit node %p", __func__, ims);
2728 * Only copy-out sources which are in-mode.
2730 if (fmode != im6s_get_mode(inm, ims, 1)) {
2731 CTR1(KTR_MLD, "%s: skip non-in-mode",
2735 src = ims->im6s_addr;
2736 retval = SYSCTL_OUT(req, &src,
2737 sizeof(struct in6_addr));
2742 IF_ADDR_RUNLOCK(ifp);
2751 static const char *in6m_modestrs[] = { "un", "in", "ex" };
2754 in6m_mode_str(const int mode)
2757 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2758 return (in6m_modestrs[mode]);
2762 static const char *in6m_statestrs[] = {
2775 in6m_state_str(const int state)
2778 if (state >= MLD_NOT_MEMBER && state <= MLD_LEAVING_MEMBER)
2779 return (in6m_statestrs[state]);
2784 * Dump an in6_multi structure to the console.
2787 in6m_print(const struct in6_multi *inm)
2790 char ip6tbuf[INET6_ADDRSTRLEN];
2792 if ((ktr_mask & KTR_MLD) == 0)
2795 printf("%s: --- begin in6m %p ---\n", __func__, inm);
2796 printf("addr %s ifp %p(%s) ifma %p\n",
2797 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2799 inm->in6m_ifp->if_xname,
2801 printf("timer %u state %s refcount %u scq.len %u\n",
2803 in6m_state_str(inm->in6m_state),
2805 inm->in6m_scq.ifq_len);
2806 printf("mli %p nsrc %lu sctimer %u scrv %u\n",
2811 for (t = 0; t < 2; t++) {
2812 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
2813 in6m_mode_str(inm->in6m_st[t].iss_fmode),
2814 inm->in6m_st[t].iss_asm,
2815 inm->in6m_st[t].iss_ex,
2816 inm->in6m_st[t].iss_in,
2817 inm->in6m_st[t].iss_rec);
2819 printf("%s: --- end in6m %p ---\n", __func__, inm);
2825 in6m_print(const struct in6_multi *inm)