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);
135 static int in6m_is_ifp_detached(const struct in6_multi *);
137 static int in6m_merge(struct in6_multi *, /*const*/ struct in6_mfilter *);
138 static void in6m_purge(struct in6_multi *);
139 static void in6m_reap(struct in6_multi *);
140 static struct ip6_moptions *
141 in6p_findmoptions(struct inpcb *);
142 static int in6p_get_source_filters(struct inpcb *, struct sockopt *);
143 static int in6p_join_group(struct inpcb *, struct sockopt *);
144 static int in6p_leave_group(struct inpcb *, struct sockopt *);
145 static struct ifnet *
146 in6p_lookup_mcast_ifp(const struct inpcb *,
147 const struct sockaddr_in6 *);
148 static int in6p_block_unblock_source(struct inpcb *, struct sockopt *);
149 static int in6p_set_multicast_if(struct inpcb *, struct sockopt *);
150 static int in6p_set_source_filters(struct inpcb *, struct sockopt *);
151 static int sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS);
153 SYSCTL_DECL(_net_inet6_ip6); /* XXX Not in any common header. */
155 static SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, mcast, CTLFLAG_RW, 0,
158 static u_long in6_mcast_maxgrpsrc = IPV6_MAX_GROUP_SRC_FILTER;
159 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxgrpsrc,
160 CTLFLAG_RW | CTLFLAG_TUN, &in6_mcast_maxgrpsrc, 0,
161 "Max source filters per group");
162 TUNABLE_ULONG("net.inet6.ip6.mcast.maxgrpsrc", &in6_mcast_maxgrpsrc);
164 static u_long in6_mcast_maxsocksrc = IPV6_MAX_SOCK_SRC_FILTER;
165 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxsocksrc,
166 CTLFLAG_RW | CTLFLAG_TUN, &in6_mcast_maxsocksrc, 0,
167 "Max source filters per socket");
168 TUNABLE_ULONG("net.inet6.ip6.mcast.maxsocksrc", &in6_mcast_maxsocksrc);
170 /* TODO Virtualize this switch. */
171 int in6_mcast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
172 SYSCTL_INT(_net_inet6_ip6_mcast, OID_AUTO, loop, CTLFLAG_RW | CTLFLAG_TUN,
173 &in6_mcast_loop, 0, "Loopback multicast datagrams by default");
174 TUNABLE_INT("net.inet6.ip6.mcast.loop", &in6_mcast_loop);
176 static SYSCTL_NODE(_net_inet6_ip6_mcast, OID_AUTO, filters,
177 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip6_mcast_filters,
178 "Per-interface stack-wide source filters");
182 * Inline function which wraps assertions for a valid ifp.
183 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
187 in6m_is_ifp_detached(const struct in6_multi *inm)
191 KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__));
192 ifp = inm->in6m_ifma->ifma_ifp;
195 * Sanity check that network-layer notion of ifp is the
196 * same as that of link-layer.
198 KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__));
201 return (ifp == NULL);
206 * Initialize an in6_mfilter structure to a known state at t0, t1
207 * with an empty source filter list.
210 im6f_init(struct in6_mfilter *imf, const int st0, const int st1)
212 memset(imf, 0, sizeof(struct in6_mfilter));
213 RB_INIT(&imf->im6f_sources);
214 imf->im6f_st[0] = st0;
215 imf->im6f_st[1] = st1;
219 * Resize the ip6_moptions vector to the next power-of-two minus 1.
220 * May be called with locks held; do not sleep.
223 im6o_grow(struct ip6_moptions *imo)
225 struct in6_multi **nmships;
226 struct in6_multi **omships;
227 struct in6_mfilter *nmfilters;
228 struct in6_mfilter *omfilters;
235 omships = imo->im6o_membership;
236 omfilters = imo->im6o_mfilters;
237 oldmax = imo->im6o_max_memberships;
238 newmax = ((oldmax + 1) * 2) - 1;
240 if (newmax <= IPV6_MAX_MEMBERSHIPS) {
241 nmships = (struct in6_multi **)realloc(omships,
242 sizeof(struct in6_multi *) * newmax, M_IP6MOPTS, M_NOWAIT);
243 nmfilters = (struct in6_mfilter *)realloc(omfilters,
244 sizeof(struct in6_mfilter) * newmax, M_IN6MFILTER,
246 if (nmships != NULL && nmfilters != NULL) {
247 /* Initialize newly allocated source filter heads. */
248 for (idx = oldmax; idx < newmax; idx++) {
249 im6f_init(&nmfilters[idx], MCAST_UNDEFINED,
252 imo->im6o_max_memberships = newmax;
253 imo->im6o_membership = nmships;
254 imo->im6o_mfilters = nmfilters;
258 if (nmships == NULL || nmfilters == NULL) {
260 free(nmships, M_IP6MOPTS);
261 if (nmfilters != NULL)
262 free(nmfilters, M_IN6MFILTER);
263 return (ETOOMANYREFS);
270 * Find an IPv6 multicast group entry for this ip6_moptions instance
271 * which matches the specified group, and optionally an interface.
272 * Return its index into the array, or -1 if not found.
275 im6o_match_group(const struct ip6_moptions *imo, const struct ifnet *ifp,
276 const struct sockaddr *group)
278 const struct sockaddr_in6 *gsin6;
279 struct in6_multi **pinm;
283 gsin6 = (const struct sockaddr_in6 *)group;
285 /* The im6o_membership array may be lazy allocated. */
286 if (imo->im6o_membership == NULL || imo->im6o_num_memberships == 0)
289 nmships = imo->im6o_num_memberships;
290 pinm = &imo->im6o_membership[0];
291 for (idx = 0; idx < nmships; idx++, pinm++) {
294 if ((ifp == NULL || ((*pinm)->in6m_ifp == ifp)) &&
295 IN6_ARE_ADDR_EQUAL(&(*pinm)->in6m_addr,
296 &gsin6->sin6_addr)) {
307 * Find an IPv6 multicast source entry for this imo which matches
308 * the given group index for this socket, and source address.
310 * XXX TODO: The scope ID, if present in src, is stripped before
311 * any comparison. We SHOULD enforce scope/zone checks where the source
312 * filter entry has a link scope.
314 * NOTE: This does not check if the entry is in-mode, merely if
315 * it exists, which may not be the desired behaviour.
317 static struct in6_msource *
318 im6o_match_source(const struct ip6_moptions *imo, const size_t gidx,
319 const struct sockaddr *src)
321 struct ip6_msource find;
322 struct in6_mfilter *imf;
323 struct ip6_msource *ims;
324 const sockunion_t *psa;
326 KASSERT(src->sa_family == AF_INET6, ("%s: !AF_INET6", __func__));
327 KASSERT(gidx != -1 && gidx < imo->im6o_num_memberships,
328 ("%s: invalid index %d\n", __func__, (int)gidx));
330 /* The im6o_mfilters array may be lazy allocated. */
331 if (imo->im6o_mfilters == NULL)
333 imf = &imo->im6o_mfilters[gidx];
335 psa = (const sockunion_t *)src;
336 find.im6s_addr = psa->sin6.sin6_addr;
337 in6_clearscope(&find.im6s_addr); /* XXX */
338 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
340 return ((struct in6_msource *)ims);
344 * Perform filtering for multicast datagrams on a socket by group and source.
346 * Returns 0 if a datagram should be allowed through, or various error codes
347 * if the socket was not a member of the group, or the source was muted, etc.
350 im6o_mc_filter(const struct ip6_moptions *imo, const struct ifnet *ifp,
351 const struct sockaddr *group, const struct sockaddr *src)
354 struct in6_msource *ims;
357 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
359 gidx = im6o_match_group(imo, ifp, group);
361 return (MCAST_NOTGMEMBER);
364 * Check if the source was included in an (S,G) join.
365 * Allow reception on exclusive memberships by default,
366 * reject reception on inclusive memberships by default.
367 * Exclude source only if an in-mode exclude filter exists.
368 * Include source only if an in-mode include filter exists.
369 * NOTE: We are comparing group state here at MLD t1 (now)
370 * with socket-layer t0 (since last downcall).
372 mode = imo->im6o_mfilters[gidx].im6f_st[1];
373 ims = im6o_match_source(imo, gidx, src);
375 if ((ims == NULL && mode == MCAST_INCLUDE) ||
376 (ims != NULL && ims->im6sl_st[0] != mode))
377 return (MCAST_NOTSMEMBER);
383 * Find and return a reference to an in6_multi record for (ifp, group),
384 * and bump its reference count.
385 * If one does not exist, try to allocate it, and update link-layer multicast
386 * filters on ifp to listen for group.
387 * Assumes the IN6_MULTI lock is held across the call.
388 * Return 0 if successful, otherwise return an appropriate error code.
391 in6_mc_get(struct ifnet *ifp, const struct in6_addr *group,
392 struct in6_multi **pinm)
394 struct sockaddr_in6 gsin6;
395 struct ifmultiaddr *ifma;
396 struct in6_multi *inm;
402 * XXX: Accesses to ifma_protospec must be covered by IF_ADDR_LOCK;
403 * if_addmulti() takes this mutex itself, so we must drop and
404 * re-acquire around the call.
406 IN6_MULTI_LOCK_ASSERT();
409 inm = in6m_lookup_locked(ifp, group);
412 * If we already joined this group, just bump the
413 * refcount and return it.
415 KASSERT(inm->in6m_refcount >= 1,
416 ("%s: bad refcount %d", __func__, inm->in6m_refcount));
417 ++inm->in6m_refcount;
422 memset(&gsin6, 0, sizeof(gsin6));
423 gsin6.sin6_family = AF_INET6;
424 gsin6.sin6_len = sizeof(struct sockaddr_in6);
425 gsin6.sin6_addr = *group;
428 * Check if a link-layer group is already associated
429 * with this network-layer group on the given ifnet.
431 IF_ADDR_WUNLOCK(ifp);
432 error = if_addmulti(ifp, (struct sockaddr *)&gsin6, &ifma);
438 * If something other than netinet6 is occupying the link-layer
439 * group, print a meaningful error message and back out of
441 * Otherwise, bump the refcount on the existing network-layer
442 * group association and return it.
444 if (ifma->ifma_protospec != NULL) {
445 inm = (struct in6_multi *)ifma->ifma_protospec;
447 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
449 KASSERT(ifma->ifma_addr->sa_family == AF_INET6,
450 ("%s: ifma not AF_INET6", __func__));
451 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
452 if (inm->in6m_ifma != ifma || inm->in6m_ifp != ifp ||
453 !IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, group))
454 panic("%s: ifma %p is inconsistent with %p (%p)",
455 __func__, ifma, inm, group);
457 ++inm->in6m_refcount;
462 IF_ADDR_WLOCK_ASSERT(ifp);
465 * A new in6_multi record is needed; allocate and initialize it.
466 * We DO NOT perform an MLD join as the in6_ layer may need to
467 * push an initial source list down to MLD to support SSM.
469 * The initial source filter state is INCLUDE, {} as per the RFC.
470 * Pending state-changes per group are subject to a bounds check.
472 inm = malloc(sizeof(*inm), M_IP6MADDR, M_NOWAIT | M_ZERO);
474 if_delmulti_ifma(ifma);
478 inm->in6m_addr = *group;
480 inm->in6m_mli = MLD_IFINFO(ifp);
481 inm->in6m_ifma = ifma;
482 inm->in6m_refcount = 1;
483 inm->in6m_state = MLD_NOT_MEMBER;
484 IFQ_SET_MAXLEN(&inm->in6m_scq, MLD_MAX_STATE_CHANGES);
486 inm->in6m_st[0].iss_fmode = MCAST_UNDEFINED;
487 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
488 RB_INIT(&inm->in6m_srcs);
490 ifma->ifma_protospec = inm;
494 IF_ADDR_WUNLOCK(ifp);
499 * Drop a reference to an in6_multi record.
501 * If the refcount drops to 0, free the in6_multi record and
502 * delete the underlying link-layer membership.
505 in6m_release_locked(struct in6_multi *inm)
507 struct ifmultiaddr *ifma;
509 IN6_MULTI_LOCK_ASSERT();
511 CTR2(KTR_MLD, "%s: refcount is %d", __func__, inm->in6m_refcount);
513 if (--inm->in6m_refcount > 0) {
514 CTR2(KTR_MLD, "%s: refcount is now %d", __func__,
519 CTR2(KTR_MLD, "%s: freeing inm %p", __func__, inm);
521 ifma = inm->in6m_ifma;
523 /* XXX this access is not covered by IF_ADDR_LOCK */
524 CTR2(KTR_MLD, "%s: purging ifma %p", __func__, ifma);
525 KASSERT(ifma->ifma_protospec == inm,
526 ("%s: ifma_protospec != inm", __func__));
527 ifma->ifma_protospec = NULL;
531 free(inm, M_IP6MADDR);
533 if_delmulti_ifma(ifma);
537 * Clear recorded source entries for a group.
538 * Used by the MLD code. Caller must hold the IN6_MULTI lock.
539 * FIXME: Should reap.
542 in6m_clear_recorded(struct in6_multi *inm)
544 struct ip6_msource *ims;
546 IN6_MULTI_LOCK_ASSERT();
548 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
551 --inm->in6m_st[1].iss_rec;
554 KASSERT(inm->in6m_st[1].iss_rec == 0,
555 ("%s: iss_rec %d not 0", __func__, inm->in6m_st[1].iss_rec));
559 * Record a source as pending for a Source-Group MLDv2 query.
560 * This lives here as it modifies the shared tree.
562 * inm is the group descriptor.
563 * naddr is the address of the source to record in network-byte order.
565 * If the net.inet6.mld.sgalloc sysctl is non-zero, we will
566 * lazy-allocate a source node in response to an SG query.
567 * Otherwise, no allocation is performed. This saves some memory
568 * with the trade-off that the source will not be reported to the
569 * router if joined in the window between the query response and
570 * the group actually being joined on the local host.
572 * VIMAGE: XXX: Currently the mld_sgalloc feature has been removed.
573 * This turns off the allocation of a recorded source entry if
574 * the group has not been joined.
576 * Return 0 if the source didn't exist or was already marked as recorded.
577 * Return 1 if the source was marked as recorded by this function.
578 * Return <0 if any error occured (negated errno code).
581 in6m_record_source(struct in6_multi *inm, const struct in6_addr *addr)
583 struct ip6_msource find;
584 struct ip6_msource *ims, *nims;
586 IN6_MULTI_LOCK_ASSERT();
588 find.im6s_addr = *addr;
589 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
590 if (ims && ims->im6s_stp)
593 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
595 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
599 nims->im6s_addr = find.im6s_addr;
600 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
606 * Mark the source as recorded and update the recorded
610 ++inm->in6m_st[1].iss_rec;
616 * Return a pointer to an in6_msource owned by an in6_mfilter,
617 * given its source address.
618 * Lazy-allocate if needed. If this is a new entry its filter state is
621 * imf is the filter set being modified.
622 * addr is the source address.
624 * SMPng: May be called with locks held; malloc must not block.
627 im6f_get_source(struct in6_mfilter *imf, const struct sockaddr_in6 *psin,
628 struct in6_msource **plims)
630 struct ip6_msource find;
631 struct ip6_msource *ims, *nims;
632 struct in6_msource *lims;
639 find.im6s_addr = psin->sin6_addr;
640 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
641 lims = (struct in6_msource *)ims;
643 if (imf->im6f_nsrc == in6_mcast_maxsocksrc)
645 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
649 lims = (struct in6_msource *)nims;
650 lims->im6s_addr = find.im6s_addr;
651 lims->im6sl_st[0] = MCAST_UNDEFINED;
652 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
662 * Graft a source entry into an existing socket-layer filter set,
663 * maintaining any required invariants and checking allocations.
665 * The source is marked as being in the new filter mode at t1.
667 * Return the pointer to the new node, otherwise return NULL.
669 static struct in6_msource *
670 im6f_graft(struct in6_mfilter *imf, const uint8_t st1,
671 const struct sockaddr_in6 *psin)
673 struct ip6_msource *nims;
674 struct in6_msource *lims;
676 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
680 lims = (struct in6_msource *)nims;
681 lims->im6s_addr = psin->sin6_addr;
682 lims->im6sl_st[0] = MCAST_UNDEFINED;
683 lims->im6sl_st[1] = st1;
684 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
691 * Prune a source entry from an existing socket-layer filter set,
692 * maintaining any required invariants and checking allocations.
694 * The source is marked as being left at t1, it is not freed.
696 * Return 0 if no error occurred, otherwise return an errno value.
699 im6f_prune(struct in6_mfilter *imf, const struct sockaddr_in6 *psin)
701 struct ip6_msource find;
702 struct ip6_msource *ims;
703 struct in6_msource *lims;
705 find.im6s_addr = psin->sin6_addr;
706 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
709 lims = (struct in6_msource *)ims;
710 lims->im6sl_st[1] = MCAST_UNDEFINED;
715 * Revert socket-layer filter set deltas at t1 to t0 state.
718 im6f_rollback(struct in6_mfilter *imf)
720 struct ip6_msource *ims, *tims;
721 struct in6_msource *lims;
723 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
724 lims = (struct in6_msource *)ims;
725 if (lims->im6sl_st[0] == lims->im6sl_st[1]) {
726 /* no change at t1 */
728 } else if (lims->im6sl_st[0] != MCAST_UNDEFINED) {
729 /* revert change to existing source at t1 */
730 lims->im6sl_st[1] = lims->im6sl_st[0];
732 /* revert source added t1 */
733 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
734 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
735 free(ims, M_IN6MFILTER);
739 imf->im6f_st[1] = imf->im6f_st[0];
743 * Mark socket-layer filter set as INCLUDE {} at t1.
746 im6f_leave(struct in6_mfilter *imf)
748 struct ip6_msource *ims;
749 struct in6_msource *lims;
751 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
752 lims = (struct in6_msource *)ims;
753 lims->im6sl_st[1] = MCAST_UNDEFINED;
755 imf->im6f_st[1] = MCAST_INCLUDE;
759 * Mark socket-layer filter set deltas as committed.
762 im6f_commit(struct in6_mfilter *imf)
764 struct ip6_msource *ims;
765 struct in6_msource *lims;
767 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
768 lims = (struct in6_msource *)ims;
769 lims->im6sl_st[0] = lims->im6sl_st[1];
771 imf->im6f_st[0] = imf->im6f_st[1];
775 * Reap unreferenced sources from socket-layer filter set.
778 im6f_reap(struct in6_mfilter *imf)
780 struct ip6_msource *ims, *tims;
781 struct in6_msource *lims;
783 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
784 lims = (struct in6_msource *)ims;
785 if ((lims->im6sl_st[0] == MCAST_UNDEFINED) &&
786 (lims->im6sl_st[1] == MCAST_UNDEFINED)) {
787 CTR2(KTR_MLD, "%s: free lims %p", __func__, ims);
788 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
789 free(ims, M_IN6MFILTER);
796 * Purge socket-layer filter set.
799 im6f_purge(struct in6_mfilter *imf)
801 struct ip6_msource *ims, *tims;
803 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
804 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
805 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
806 free(ims, M_IN6MFILTER);
809 imf->im6f_st[0] = imf->im6f_st[1] = MCAST_UNDEFINED;
810 KASSERT(RB_EMPTY(&imf->im6f_sources),
811 ("%s: im6f_sources not empty", __func__));
815 * Look up a source filter entry for a multicast group.
817 * inm is the group descriptor to work with.
818 * addr is the IPv6 address to look up.
819 * noalloc may be non-zero to suppress allocation of sources.
820 * *pims will be set to the address of the retrieved or allocated source.
822 * SMPng: NOTE: may be called with locks held.
823 * Return 0 if successful, otherwise return a non-zero error code.
826 in6m_get_source(struct in6_multi *inm, const struct in6_addr *addr,
827 const int noalloc, struct ip6_msource **pims)
829 struct ip6_msource find;
830 struct ip6_msource *ims, *nims;
832 char ip6tbuf[INET6_ADDRSTRLEN];
835 find.im6s_addr = *addr;
836 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
837 if (ims == NULL && !noalloc) {
838 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
840 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
844 nims->im6s_addr = *addr;
845 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
848 CTR3(KTR_MLD, "%s: allocated %s as %p", __func__,
849 ip6_sprintf(ip6tbuf, addr), ims);
857 * Merge socket-layer source into MLD-layer source.
858 * If rollback is non-zero, perform the inverse of the merge.
861 im6s_merge(struct ip6_msource *ims, const struct in6_msource *lims,
864 int n = rollback ? -1 : 1;
866 char ip6tbuf[INET6_ADDRSTRLEN];
868 ip6_sprintf(ip6tbuf, &lims->im6s_addr);
871 if (lims->im6sl_st[0] == MCAST_EXCLUDE) {
872 CTR3(KTR_MLD, "%s: t1 ex -= %d on %s", __func__, n, ip6tbuf);
873 ims->im6s_st[1].ex -= n;
874 } else if (lims->im6sl_st[0] == MCAST_INCLUDE) {
875 CTR3(KTR_MLD, "%s: t1 in -= %d on %s", __func__, n, ip6tbuf);
876 ims->im6s_st[1].in -= n;
879 if (lims->im6sl_st[1] == MCAST_EXCLUDE) {
880 CTR3(KTR_MLD, "%s: t1 ex += %d on %s", __func__, n, ip6tbuf);
881 ims->im6s_st[1].ex += n;
882 } else if (lims->im6sl_st[1] == MCAST_INCLUDE) {
883 CTR3(KTR_MLD, "%s: t1 in += %d on %s", __func__, n, ip6tbuf);
884 ims->im6s_st[1].in += n;
889 * Atomically update the global in6_multi state, when a membership's
890 * filter list is being updated in any way.
892 * imf is the per-inpcb-membership group filter pointer.
893 * A fake imf may be passed for in-kernel consumers.
895 * XXX This is a candidate for a set-symmetric-difference style loop
896 * which would eliminate the repeated lookup from root of ims nodes,
897 * as they share the same key space.
899 * If any error occurred this function will back out of refcounts
900 * and return a non-zero value.
903 in6m_merge(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
905 struct ip6_msource *ims, *nims;
906 struct in6_msource *lims;
915 * Update the source filters first, as this may fail.
916 * Maintain count of in-mode filters at t0, t1. These are
917 * used to work out if we transition into ASM mode or not.
918 * Maintain a count of source filters whose state was
919 * actually modified by this operation.
921 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
922 lims = (struct in6_msource *)ims;
923 if (lims->im6sl_st[0] == imf->im6f_st[0]) nsrc0++;
924 if (lims->im6sl_st[1] == imf->im6f_st[1]) nsrc1++;
925 if (lims->im6sl_st[0] == lims->im6sl_st[1]) continue;
926 error = in6m_get_source(inm, &lims->im6s_addr, 0, &nims);
930 im6s_merge(nims, lims, 0);
933 struct ip6_msource *bims;
935 RB_FOREACH_REVERSE_FROM(ims, ip6_msource_tree, nims) {
936 lims = (struct in6_msource *)ims;
937 if (lims->im6sl_st[0] == lims->im6sl_st[1])
939 (void)in6m_get_source(inm, &lims->im6s_addr, 1, &bims);
942 im6s_merge(bims, lims, 1);
947 CTR3(KTR_MLD, "%s: imf filters in-mode: %d at t0, %d at t1",
948 __func__, nsrc0, nsrc1);
950 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
951 if (imf->im6f_st[0] == imf->im6f_st[1] &&
952 imf->im6f_st[1] == MCAST_INCLUDE) {
954 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
955 --inm->in6m_st[1].iss_in;
959 /* Handle filter mode transition on socket. */
960 if (imf->im6f_st[0] != imf->im6f_st[1]) {
961 CTR3(KTR_MLD, "%s: imf transition %d to %d",
962 __func__, imf->im6f_st[0], imf->im6f_st[1]);
964 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
965 CTR1(KTR_MLD, "%s: --ex on inm at t1", __func__);
966 --inm->in6m_st[1].iss_ex;
967 } else if (imf->im6f_st[0] == MCAST_INCLUDE) {
968 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
969 --inm->in6m_st[1].iss_in;
972 if (imf->im6f_st[1] == MCAST_EXCLUDE) {
973 CTR1(KTR_MLD, "%s: ex++ on inm at t1", __func__);
974 inm->in6m_st[1].iss_ex++;
975 } else if (imf->im6f_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
976 CTR1(KTR_MLD, "%s: in++ on inm at t1", __func__);
977 inm->in6m_st[1].iss_in++;
982 * Track inm filter state in terms of listener counts.
983 * If there are any exclusive listeners, stack-wide
984 * membership is exclusive.
985 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
986 * If no listeners remain, state is undefined at t1,
987 * and the MLD lifecycle for this group should finish.
989 if (inm->in6m_st[1].iss_ex > 0) {
990 CTR1(KTR_MLD, "%s: transition to EX", __func__);
991 inm->in6m_st[1].iss_fmode = MCAST_EXCLUDE;
992 } else if (inm->in6m_st[1].iss_in > 0) {
993 CTR1(KTR_MLD, "%s: transition to IN", __func__);
994 inm->in6m_st[1].iss_fmode = MCAST_INCLUDE;
996 CTR1(KTR_MLD, "%s: transition to UNDEF", __func__);
997 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
1000 /* Decrement ASM listener count on transition out of ASM mode. */
1001 if (imf->im6f_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1002 if ((imf->im6f_st[1] != MCAST_EXCLUDE) ||
1003 (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 > 0))
1004 CTR1(KTR_MLD, "%s: --asm on inm at t1", __func__);
1005 --inm->in6m_st[1].iss_asm;
1008 /* Increment ASM listener count on transition to ASM mode. */
1009 if (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1010 CTR1(KTR_MLD, "%s: asm++ on inm at t1", __func__);
1011 inm->in6m_st[1].iss_asm++;
1014 CTR3(KTR_MLD, "%s: merged imf %p to inm %p", __func__, imf, inm);
1019 CTR1(KTR_MLD, "%s: sources changed; reaping", __func__);
1026 * Mark an in6_multi's filter set deltas as committed.
1027 * Called by MLD after a state change has been enqueued.
1030 in6m_commit(struct in6_multi *inm)
1032 struct ip6_msource *ims;
1034 CTR2(KTR_MLD, "%s: commit inm %p", __func__, inm);
1035 CTR1(KTR_MLD, "%s: pre commit:", __func__);
1038 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
1039 ims->im6s_st[0] = ims->im6s_st[1];
1041 inm->in6m_st[0] = inm->in6m_st[1];
1045 * Reap unreferenced nodes from an in6_multi's filter set.
1048 in6m_reap(struct in6_multi *inm)
1050 struct ip6_msource *ims, *tims;
1052 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1053 if (ims->im6s_st[0].ex > 0 || ims->im6s_st[0].in > 0 ||
1054 ims->im6s_st[1].ex > 0 || ims->im6s_st[1].in > 0 ||
1057 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1058 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1059 free(ims, M_IP6MSOURCE);
1065 * Purge all source nodes from an in6_multi's filter set.
1068 in6m_purge(struct in6_multi *inm)
1070 struct ip6_msource *ims, *tims;
1072 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1073 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1074 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1075 free(ims, M_IP6MSOURCE);
1078 /* Free state-change requests that might be queued. */
1079 _IF_DRAIN(&inm->in6m_scq);
1083 * Join a multicast address w/o sources.
1084 * KAME compatibility entry point.
1086 * SMPng: Assume no mc locks held by caller.
1088 struct in6_multi_mship *
1089 in6_joingroup(struct ifnet *ifp, struct in6_addr *mcaddr,
1090 int *errorp, int delay)
1092 struct in6_multi_mship *imm;
1095 imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT);
1101 delay = (delay * PR_FASTHZ) / hz;
1103 error = in6_mc_join(ifp, mcaddr, NULL, &imm->i6mm_maddr, delay);
1106 free(imm, M_IP6MADDR);
1114 * Leave a multicast address w/o sources.
1115 * KAME compatibility entry point.
1117 * SMPng: Assume no mc locks held by caller.
1120 in6_leavegroup(struct in6_multi_mship *imm)
1123 if (imm->i6mm_maddr != NULL)
1124 in6_mc_leave(imm->i6mm_maddr, NULL);
1125 free(imm, M_IP6MADDR);
1130 * Join a multicast group; unlocked entry point.
1132 * SMPng: XXX: in6_mc_join() is called from in6_control() when upper
1133 * locks are not held. Fortunately, ifp is unlikely to have been detached
1134 * at this point, so we assume it's OK to recurse.
1137 in6_mc_join(struct ifnet *ifp, const struct in6_addr *mcaddr,
1138 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1144 error = in6_mc_join_locked(ifp, mcaddr, imf, pinm, delay);
1151 * Join a multicast group; real entry point.
1153 * Only preserves atomicity at inm level.
1154 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1156 * If the MLD downcall fails, the group is not joined, and an error
1160 in6_mc_join_locked(struct ifnet *ifp, const struct in6_addr *mcaddr,
1161 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1164 struct in6_mfilter timf;
1165 struct in6_multi *inm;
1168 char ip6tbuf[INET6_ADDRSTRLEN];
1173 * Sanity: Check scope zone ID was set for ifp, if and
1174 * only if group is scoped to an interface.
1176 KASSERT(IN6_IS_ADDR_MULTICAST(mcaddr),
1177 ("%s: not a multicast address", __func__));
1178 if (IN6_IS_ADDR_MC_LINKLOCAL(mcaddr) ||
1179 IN6_IS_ADDR_MC_INTFACELOCAL(mcaddr)) {
1180 KASSERT(mcaddr->s6_addr16[1] != 0,
1181 ("%s: scope zone ID not set", __func__));
1185 IN6_MULTI_LOCK_ASSERT();
1187 CTR4(KTR_MLD, "%s: join %s on %p(%s))", __func__,
1188 ip6_sprintf(ip6tbuf, mcaddr), ifp, ifp->if_xname);
1194 * If no imf was specified (i.e. kernel consumer),
1195 * fake one up and assume it is an ASM join.
1198 im6f_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1202 error = in6_mc_get(ifp, mcaddr, &inm);
1204 CTR1(KTR_MLD, "%s: in6_mc_get() failure", __func__);
1208 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1209 error = in6m_merge(inm, imf);
1211 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1212 goto out_in6m_release;
1215 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1216 error = mld_change_state(inm, delay);
1218 CTR1(KTR_MLD, "%s: failed to update source", __func__);
1219 goto out_in6m_release;
1224 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1225 in6m_release_locked(inm);
1234 * Leave a multicast group; unlocked entry point.
1237 in6_mc_leave(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1242 ifp = inm->in6m_ifp;
1245 error = in6_mc_leave_locked(inm, imf);
1252 * Leave a multicast group; real entry point.
1253 * All source filters will be expunged.
1255 * Only preserves atomicity at inm level.
1257 * Holding the write lock for the INP which contains imf
1258 * is highly advisable. We can't assert for it as imf does not
1259 * contain a back-pointer to the owning inp.
1261 * Note: This is not the same as in6m_release(*) as this function also
1262 * makes a state change downcall into MLD.
1265 in6_mc_leave_locked(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1267 struct in6_mfilter timf;
1270 char ip6tbuf[INET6_ADDRSTRLEN];
1275 IN6_MULTI_LOCK_ASSERT();
1277 CTR5(KTR_MLD, "%s: leave inm %p, %s/%s, imf %p", __func__,
1278 inm, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1279 (in6m_is_ifp_detached(inm) ? "null" : inm->in6m_ifp->if_xname),
1283 * If no imf was specified (i.e. kernel consumer),
1284 * fake one up and assume it is an ASM join.
1287 im6f_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1292 * Begin state merge transaction at MLD layer.
1294 * As this particular invocation should not cause any memory
1295 * to be allocated, and there is no opportunity to roll back
1296 * the transaction, it MUST NOT fail.
1298 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1299 error = in6m_merge(inm, imf);
1300 KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1302 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1303 error = mld_change_state(inm, 0);
1305 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1307 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1308 in6m_release_locked(inm);
1314 * Block or unblock an ASM multicast source on an inpcb.
1315 * This implements the delta-based API described in RFC 3678.
1317 * The delta-based API applies only to exclusive-mode memberships.
1318 * An MLD downcall will be performed.
1320 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1322 * Return 0 if successful, otherwise return an appropriate error code.
1325 in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1327 struct group_source_req gsr;
1328 sockunion_t *gsa, *ssa;
1330 struct in6_mfilter *imf;
1331 struct ip6_moptions *imo;
1332 struct in6_msource *ims;
1333 struct in6_multi *inm;
1338 char ip6tbuf[INET6_ADDRSTRLEN];
1345 memset(&gsr, 0, sizeof(struct group_source_req));
1346 gsa = (sockunion_t *)&gsr.gsr_group;
1347 ssa = (sockunion_t *)&gsr.gsr_source;
1349 switch (sopt->sopt_name) {
1350 case MCAST_BLOCK_SOURCE:
1351 case MCAST_UNBLOCK_SOURCE:
1352 error = sooptcopyin(sopt, &gsr,
1353 sizeof(struct group_source_req),
1354 sizeof(struct group_source_req));
1358 if (gsa->sin6.sin6_family != AF_INET6 ||
1359 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1362 if (ssa->sin6.sin6_family != AF_INET6 ||
1363 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1366 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1367 return (EADDRNOTAVAIL);
1369 ifp = ifnet_byindex(gsr.gsr_interface);
1371 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1376 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1377 __func__, sopt->sopt_name);
1378 return (EOPNOTSUPP);
1382 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1385 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1388 * Check if we are actually a member of this group.
1390 imo = in6p_findmoptions(inp);
1391 idx = im6o_match_group(imo, ifp, &gsa->sa);
1392 if (idx == -1 || imo->im6o_mfilters == NULL) {
1393 error = EADDRNOTAVAIL;
1394 goto out_in6p_locked;
1397 KASSERT(imo->im6o_mfilters != NULL,
1398 ("%s: im6o_mfilters not allocated", __func__));
1399 imf = &imo->im6o_mfilters[idx];
1400 inm = imo->im6o_membership[idx];
1403 * Attempting to use the delta-based API on an
1404 * non exclusive-mode membership is an error.
1406 fmode = imf->im6f_st[0];
1407 if (fmode != MCAST_EXCLUDE) {
1409 goto out_in6p_locked;
1413 * Deal with error cases up-front:
1414 * Asked to block, but already blocked; or
1415 * Asked to unblock, but nothing to unblock.
1416 * If adding a new block entry, allocate it.
1418 ims = im6o_match_source(imo, idx, &ssa->sa);
1419 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1420 CTR3(KTR_MLD, "%s: source %s %spresent", __func__,
1421 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
1422 doblock ? "" : "not ");
1423 error = EADDRNOTAVAIL;
1424 goto out_in6p_locked;
1427 INP_WLOCK_ASSERT(inp);
1430 * Begin state merge transaction at socket layer.
1433 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
1434 ims = im6f_graft(imf, fmode, &ssa->sin6);
1438 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
1439 error = im6f_prune(imf, &ssa->sin6);
1443 CTR1(KTR_MLD, "%s: merge imf state failed", __func__);
1444 goto out_im6f_rollback;
1448 * Begin state merge transaction at MLD layer.
1452 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1453 error = in6m_merge(inm, imf);
1455 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1457 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1458 error = mld_change_state(inm, 0);
1460 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1479 * Given an inpcb, return its multicast options structure pointer. Accepts
1480 * an unlocked inpcb pointer, but will return it locked. May sleep.
1482 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1483 * SMPng: NOTE: Returns with the INP write lock held.
1485 static struct ip6_moptions *
1486 in6p_findmoptions(struct inpcb *inp)
1488 struct ip6_moptions *imo;
1489 struct in6_multi **immp;
1490 struct in6_mfilter *imfp;
1494 if (inp->in6p_moptions != NULL)
1495 return (inp->in6p_moptions);
1499 imo = malloc(sizeof(*imo), M_IP6MOPTS, M_WAITOK);
1500 immp = malloc(sizeof(*immp) * IPV6_MIN_MEMBERSHIPS, M_IP6MOPTS,
1502 imfp = malloc(sizeof(struct in6_mfilter) * IPV6_MIN_MEMBERSHIPS,
1503 M_IN6MFILTER, M_WAITOK);
1505 imo->im6o_multicast_ifp = NULL;
1506 imo->im6o_multicast_hlim = V_ip6_defmcasthlim;
1507 imo->im6o_multicast_loop = in6_mcast_loop;
1508 imo->im6o_num_memberships = 0;
1509 imo->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1510 imo->im6o_membership = immp;
1512 /* Initialize per-group source filters. */
1513 for (idx = 0; idx < IPV6_MIN_MEMBERSHIPS; idx++)
1514 im6f_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE);
1515 imo->im6o_mfilters = imfp;
1518 if (inp->in6p_moptions != NULL) {
1519 free(imfp, M_IN6MFILTER);
1520 free(immp, M_IP6MOPTS);
1521 free(imo, M_IP6MOPTS);
1522 return (inp->in6p_moptions);
1524 inp->in6p_moptions = imo;
1529 * Discard the IPv6 multicast options (and source filters).
1531 * SMPng: NOTE: assumes INP write lock is held.
1534 ip6_freemoptions(struct ip6_moptions *imo)
1536 struct in6_mfilter *imf;
1537 size_t idx, nmships;
1539 KASSERT(imo != NULL, ("%s: ip6_moptions is NULL", __func__));
1541 nmships = imo->im6o_num_memberships;
1542 for (idx = 0; idx < nmships; ++idx) {
1543 imf = imo->im6o_mfilters ? &imo->im6o_mfilters[idx] : NULL;
1546 /* XXX this will thrash the lock(s) */
1547 (void)in6_mc_leave(imo->im6o_membership[idx], imf);
1552 if (imo->im6o_mfilters)
1553 free(imo->im6o_mfilters, M_IN6MFILTER);
1554 free(imo->im6o_membership, M_IP6MOPTS);
1555 free(imo, M_IP6MOPTS);
1559 * Atomically get source filters on a socket for an IPv6 multicast group.
1560 * Called with INP lock held; returns with lock released.
1563 in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1565 struct __msfilterreq msfr;
1568 struct ip6_moptions *imo;
1569 struct in6_mfilter *imf;
1570 struct ip6_msource *ims;
1571 struct in6_msource *lims;
1572 struct sockaddr_in6 *psin;
1573 struct sockaddr_storage *ptss;
1574 struct sockaddr_storage *tss;
1576 size_t idx, nsrcs, ncsrcs;
1578 INP_WLOCK_ASSERT(inp);
1580 imo = inp->in6p_moptions;
1581 KASSERT(imo != NULL, ("%s: null ip6_moptions", __func__));
1585 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1586 sizeof(struct __msfilterreq));
1590 if (msfr.msfr_group.ss_family != AF_INET6 ||
1591 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
1594 gsa = (sockunion_t *)&msfr.msfr_group;
1595 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1598 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
1599 return (EADDRNOTAVAIL);
1600 ifp = ifnet_byindex(msfr.msfr_ifindex);
1602 return (EADDRNOTAVAIL);
1603 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1608 * Lookup group on the socket.
1610 idx = im6o_match_group(imo, ifp, &gsa->sa);
1611 if (idx == -1 || imo->im6o_mfilters == NULL) {
1613 return (EADDRNOTAVAIL);
1615 imf = &imo->im6o_mfilters[idx];
1618 * Ignore memberships which are in limbo.
1620 if (imf->im6f_st[1] == MCAST_UNDEFINED) {
1624 msfr.msfr_fmode = imf->im6f_st[1];
1627 * If the user specified a buffer, copy out the source filter
1628 * entries to userland gracefully.
1629 * We only copy out the number of entries which userland
1630 * has asked for, but we always tell userland how big the
1631 * buffer really needs to be.
1633 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
1634 msfr.msfr_nsrcs = in6_mcast_maxsocksrc;
1636 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1637 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1638 M_TEMP, M_NOWAIT | M_ZERO);
1646 * Count number of sources in-mode at t0.
1647 * If buffer space exists and remains, copy out source entries.
1649 nsrcs = msfr.msfr_nsrcs;
1652 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
1653 lims = (struct in6_msource *)ims;
1654 if (lims->im6sl_st[0] == MCAST_UNDEFINED ||
1655 lims->im6sl_st[0] != imf->im6f_st[0])
1658 if (tss != NULL && nsrcs > 0) {
1659 psin = (struct sockaddr_in6 *)ptss;
1660 psin->sin6_family = AF_INET6;
1661 psin->sin6_len = sizeof(struct sockaddr_in6);
1662 psin->sin6_addr = lims->im6s_addr;
1663 psin->sin6_port = 0;
1672 error = copyout(tss, msfr.msfr_srcs,
1673 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1679 msfr.msfr_nsrcs = ncsrcs;
1680 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1686 * Return the IP multicast options in response to user getsockopt().
1689 ip6_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1691 struct ip6_moptions *im6o;
1696 im6o = inp->in6p_moptions;
1698 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1699 * or is a divert socket, reject it.
1701 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1702 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1703 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
1705 return (EOPNOTSUPP);
1709 switch (sopt->sopt_name) {
1710 case IPV6_MULTICAST_IF:
1711 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) {
1714 optval = im6o->im6o_multicast_ifp->if_index;
1717 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1720 case IPV6_MULTICAST_HOPS:
1722 optval = V_ip6_defmcasthlim;
1724 optval = im6o->im6o_multicast_hlim;
1726 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1729 case IPV6_MULTICAST_LOOP:
1731 optval = in6_mcast_loop; /* XXX VIMAGE */
1733 optval = im6o->im6o_multicast_loop;
1735 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1740 error = EADDRNOTAVAIL;
1743 error = in6p_get_source_filters(inp, sopt);
1749 error = ENOPROTOOPT;
1753 INP_UNLOCK_ASSERT(inp);
1759 * Look up the ifnet to use for a multicast group membership,
1760 * given the address of an IPv6 group.
1762 * This routine exists to support legacy IPv6 multicast applications.
1764 * If inp is non-NULL, use this socket's current FIB number for any
1765 * required FIB lookup. Look up the group address in the unicast FIB,
1766 * and use its ifp; usually, this points to the default next-hop.
1767 * If the FIB lookup fails, return NULL.
1769 * FUTURE: Support multiple forwarding tables for IPv6.
1771 * Returns NULL if no ifp could be found.
1773 static struct ifnet *
1774 in6p_lookup_mcast_ifp(const struct inpcb *in6p,
1775 const struct sockaddr_in6 *gsin6)
1777 struct route_in6 ro6;
1780 KASSERT(in6p->inp_vflag & INP_IPV6,
1781 ("%s: not INP_IPV6 inpcb", __func__));
1782 KASSERT(gsin6->sin6_family == AF_INET6,
1783 ("%s: not AF_INET6 group", __func__));
1786 memset(&ro6, 0, sizeof(struct route_in6));
1787 memcpy(&ro6.ro_dst, gsin6, sizeof(struct sockaddr_in6));
1788 rtalloc_ign_fib((struct route *)&ro6, 0,
1789 in6p ? in6p->inp_inc.inc_fibnum : RT_DEFAULT_FIB);
1790 if (ro6.ro_rt != NULL) {
1791 ifp = ro6.ro_rt->rt_ifp;
1792 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
1800 * Join an IPv6 multicast group, possibly with a source.
1802 * FIXME: The KAME use of the unspecified address (::)
1803 * to join *all* multicast groups is currently unsupported.
1806 in6p_join_group(struct inpcb *inp, struct sockopt *sopt)
1808 struct group_source_req gsr;
1809 sockunion_t *gsa, *ssa;
1811 struct in6_mfilter *imf;
1812 struct ip6_moptions *imo;
1813 struct in6_multi *inm;
1814 struct in6_msource *lims;
1824 memset(&gsr, 0, sizeof(struct group_source_req));
1825 gsa = (sockunion_t *)&gsr.gsr_group;
1826 gsa->ss.ss_family = AF_UNSPEC;
1827 ssa = (sockunion_t *)&gsr.gsr_source;
1828 ssa->ss.ss_family = AF_UNSPEC;
1831 * Chew everything into struct group_source_req.
1832 * Overwrite the port field if present, as the sockaddr
1833 * being copied in may be matched with a binary comparison.
1834 * Ignore passed-in scope ID.
1836 switch (sopt->sopt_name) {
1837 case IPV6_JOIN_GROUP: {
1838 struct ipv6_mreq mreq;
1840 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
1841 sizeof(struct ipv6_mreq));
1845 gsa->sin6.sin6_family = AF_INET6;
1846 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
1847 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
1849 if (mreq.ipv6mr_interface == 0) {
1850 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
1852 if (V_if_index < mreq.ipv6mr_interface)
1853 return (EADDRNOTAVAIL);
1854 ifp = ifnet_byindex(mreq.ipv6mr_interface);
1856 CTR3(KTR_MLD, "%s: ipv6mr_interface = %d, ifp = %p",
1857 __func__, mreq.ipv6mr_interface, ifp);
1860 case MCAST_JOIN_GROUP:
1861 case MCAST_JOIN_SOURCE_GROUP:
1862 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1863 error = sooptcopyin(sopt, &gsr,
1864 sizeof(struct group_req),
1865 sizeof(struct group_req));
1866 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1867 error = sooptcopyin(sopt, &gsr,
1868 sizeof(struct group_source_req),
1869 sizeof(struct group_source_req));
1874 if (gsa->sin6.sin6_family != AF_INET6 ||
1875 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1878 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1879 if (ssa->sin6.sin6_family != AF_INET6 ||
1880 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1882 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
1885 * TODO: Validate embedded scope ID in source
1886 * list entry against passed-in ifp, if and only
1887 * if source list filter entry is iface or node local.
1889 in6_clearscope(&ssa->sin6.sin6_addr);
1890 ssa->sin6.sin6_port = 0;
1891 ssa->sin6.sin6_scope_id = 0;
1894 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1895 return (EADDRNOTAVAIL);
1896 ifp = ifnet_byindex(gsr.gsr_interface);
1900 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1901 __func__, sopt->sopt_name);
1902 return (EOPNOTSUPP);
1906 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1909 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
1910 return (EADDRNOTAVAIL);
1912 gsa->sin6.sin6_port = 0;
1913 gsa->sin6.sin6_scope_id = 0;
1916 * Always set the scope zone ID on memberships created from userland.
1917 * Use the passed-in ifp to do this.
1918 * XXX The in6_setscope() return value is meaningless.
1919 * XXX SCOPE6_LOCK() is taken by in6_setscope().
1921 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1923 imo = in6p_findmoptions(inp);
1924 idx = im6o_match_group(imo, ifp, &gsa->sa);
1928 inm = imo->im6o_membership[idx];
1929 imf = &imo->im6o_mfilters[idx];
1930 if (ssa->ss.ss_family != AF_UNSPEC) {
1932 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
1933 * is an error. On an existing inclusive membership,
1934 * it just adds the source to the filter list.
1936 if (imf->im6f_st[1] != MCAST_INCLUDE) {
1938 goto out_in6p_locked;
1941 * Throw out duplicates.
1943 * XXX FIXME: This makes a naive assumption that
1944 * even if entries exist for *ssa in this imf,
1945 * they will be rejected as dupes, even if they
1946 * are not valid in the current mode (in-mode).
1948 * in6_msource is transactioned just as for anything
1949 * else in SSM -- but note naive use of in6m_graft()
1950 * below for allocating new filter entries.
1952 * This is only an issue if someone mixes the
1953 * full-state SSM API with the delta-based API,
1954 * which is discouraged in the relevant RFCs.
1956 lims = im6o_match_source(imo, idx, &ssa->sa);
1957 if (lims != NULL /*&&
1958 lims->im6sl_st[1] == MCAST_INCLUDE*/) {
1959 error = EADDRNOTAVAIL;
1960 goto out_in6p_locked;
1964 * MCAST_JOIN_GROUP alone, on any existing membership,
1965 * is rejected, to stop the same inpcb tying up
1966 * multiple refs to the in_multi.
1967 * On an existing inclusive membership, this is also
1968 * an error; if you want to change filter mode,
1969 * you must use the userland API setsourcefilter().
1970 * XXX We don't reject this for imf in UNDEFINED
1971 * state at t1, because allocation of a filter
1972 * is atomic with allocation of a membership.
1975 goto out_in6p_locked;
1980 * Begin state merge transaction at socket layer.
1982 INP_WLOCK_ASSERT(inp);
1985 if (imo->im6o_num_memberships == imo->im6o_max_memberships) {
1986 error = im6o_grow(imo);
1988 goto out_in6p_locked;
1991 * Allocate the new slot upfront so we can deal with
1992 * grafting the new source filter in same code path
1993 * as for join-source on existing membership.
1995 idx = imo->im6o_num_memberships;
1996 imo->im6o_membership[idx] = NULL;
1997 imo->im6o_num_memberships++;
1998 KASSERT(imo->im6o_mfilters != NULL,
1999 ("%s: im6f_mfilters vector was not allocated", __func__));
2000 imf = &imo->im6o_mfilters[idx];
2001 KASSERT(RB_EMPTY(&imf->im6f_sources),
2002 ("%s: im6f_sources not empty", __func__));
2006 * Graft new source into filter list for this inpcb's
2007 * membership of the group. The in6_multi may not have
2008 * been allocated yet if this is a new membership, however,
2009 * the in_mfilter slot will be allocated and must be initialized.
2011 * Note: Grafting of exclusive mode filters doesn't happen
2013 * XXX: Should check for non-NULL lims (node exists but may
2014 * not be in-mode) for interop with full-state API.
2016 if (ssa->ss.ss_family != AF_UNSPEC) {
2017 /* Membership starts in IN mode */
2019 CTR1(KTR_MLD, "%s: new join w/source", __func__);
2020 im6f_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE);
2022 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
2024 lims = im6f_graft(imf, MCAST_INCLUDE, &ssa->sin6);
2026 CTR1(KTR_MLD, "%s: merge imf state failed",
2032 /* No address specified; Membership starts in EX mode */
2034 CTR1(KTR_MLD, "%s: new join w/o source", __func__);
2035 im6f_init(imf, MCAST_UNDEFINED, MCAST_EXCLUDE);
2040 * Begin state merge transaction at MLD layer.
2045 error = in6_mc_join_locked(ifp, &gsa->sin6.sin6_addr, imf,
2051 imo->im6o_membership[idx] = inm;
2053 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2054 error = in6m_merge(inm, imf);
2056 CTR1(KTR_MLD, "%s: failed to merge inm state",
2059 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2060 error = mld_change_state(inm, 0);
2062 CTR1(KTR_MLD, "%s: failed mld downcall",
2068 INP_WLOCK_ASSERT(inp);
2080 if (error && is_new) {
2081 imo->im6o_membership[idx] = NULL;
2082 --imo->im6o_num_memberships;
2091 * Leave an IPv6 multicast group on an inpcb, possibly with a source.
2094 in6p_leave_group(struct inpcb *inp, struct sockopt *sopt)
2096 struct ipv6_mreq mreq;
2097 struct group_source_req gsr;
2098 sockunion_t *gsa, *ssa;
2100 struct in6_mfilter *imf;
2101 struct ip6_moptions *imo;
2102 struct in6_msource *ims;
2103 struct in6_multi *inm;
2106 int error, is_final;
2108 char ip6tbuf[INET6_ADDRSTRLEN];
2116 memset(&gsr, 0, sizeof(struct group_source_req));
2117 gsa = (sockunion_t *)&gsr.gsr_group;
2118 gsa->ss.ss_family = AF_UNSPEC;
2119 ssa = (sockunion_t *)&gsr.gsr_source;
2120 ssa->ss.ss_family = AF_UNSPEC;
2123 * Chew everything passed in up into a struct group_source_req
2124 * as that is easier to process.
2125 * Note: Any embedded scope ID in the multicast group passed
2126 * in by userland is ignored, the interface index is the recommended
2127 * mechanism to specify an interface; see below.
2129 switch (sopt->sopt_name) {
2130 case IPV6_LEAVE_GROUP:
2131 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
2132 sizeof(struct ipv6_mreq));
2135 gsa->sin6.sin6_family = AF_INET6;
2136 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
2137 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
2138 gsa->sin6.sin6_port = 0;
2139 gsa->sin6.sin6_scope_id = 0;
2140 ifindex = mreq.ipv6mr_interface;
2143 case MCAST_LEAVE_GROUP:
2144 case MCAST_LEAVE_SOURCE_GROUP:
2145 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2146 error = sooptcopyin(sopt, &gsr,
2147 sizeof(struct group_req),
2148 sizeof(struct group_req));
2149 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2150 error = sooptcopyin(sopt, &gsr,
2151 sizeof(struct group_source_req),
2152 sizeof(struct group_source_req));
2157 if (gsa->sin6.sin6_family != AF_INET6 ||
2158 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2160 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2161 if (ssa->sin6.sin6_family != AF_INET6 ||
2162 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2164 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
2167 * TODO: Validate embedded scope ID in source
2168 * list entry against passed-in ifp, if and only
2169 * if source list filter entry is iface or node local.
2171 in6_clearscope(&ssa->sin6.sin6_addr);
2173 gsa->sin6.sin6_port = 0;
2174 gsa->sin6.sin6_scope_id = 0;
2175 ifindex = gsr.gsr_interface;
2179 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
2180 __func__, sopt->sopt_name);
2181 return (EOPNOTSUPP);
2185 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2189 * Validate interface index if provided. If no interface index
2190 * was provided separately, attempt to look the membership up
2191 * from the default scope as a last resort to disambiguate
2192 * the membership we are being asked to leave.
2193 * XXX SCOPE6 lock potentially taken here.
2196 if (V_if_index < ifindex)
2197 return (EADDRNOTAVAIL);
2198 ifp = ifnet_byindex(ifindex);
2200 return (EADDRNOTAVAIL);
2201 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2203 error = sa6_embedscope(&gsa->sin6, V_ip6_use_defzone);
2205 return (EADDRNOTAVAIL);
2207 * Some badly behaved applications don't pass an ifindex
2208 * or a scope ID, which is an API violation. In this case,
2209 * perform a lookup as per a v6 join.
2211 * XXX For now, stomp on zone ID for the corner case.
2212 * This is not the 'KAME way', but we need to see the ifp
2213 * directly until such time as this implementation is
2214 * refactored, assuming the scope IDs are the way to go.
2216 ifindex = ntohs(gsa->sin6.sin6_addr.s6_addr16[1]);
2218 CTR2(KTR_MLD, "%s: warning: no ifindex, looking up "
2219 "ifp for group %s.", __func__,
2220 ip6_sprintf(ip6tbuf, &gsa->sin6.sin6_addr));
2221 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
2223 ifp = ifnet_byindex(ifindex);
2226 return (EADDRNOTAVAIL);
2229 CTR2(KTR_MLD, "%s: ifp = %p", __func__, ifp);
2230 KASSERT(ifp != NULL, ("%s: ifp did not resolve", __func__));
2233 * Find the membership in the membership array.
2235 imo = in6p_findmoptions(inp);
2236 idx = im6o_match_group(imo, ifp, &gsa->sa);
2238 error = EADDRNOTAVAIL;
2239 goto out_in6p_locked;
2241 inm = imo->im6o_membership[idx];
2242 imf = &imo->im6o_mfilters[idx];
2244 if (ssa->ss.ss_family != AF_UNSPEC)
2248 * Begin state merge transaction at socket layer.
2250 INP_WLOCK_ASSERT(inp);
2253 * If we were instructed only to leave a given source, do so.
2254 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2259 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
2260 error = EADDRNOTAVAIL;
2261 goto out_in6p_locked;
2263 ims = im6o_match_source(imo, idx, &ssa->sa);
2265 CTR3(KTR_MLD, "%s: source %p %spresent", __func__,
2266 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
2268 error = EADDRNOTAVAIL;
2269 goto out_in6p_locked;
2271 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
2272 error = im6f_prune(imf, &ssa->sin6);
2274 CTR1(KTR_MLD, "%s: merge imf state failed",
2276 goto out_in6p_locked;
2281 * Begin state merge transaction at MLD layer.
2287 * Give up the multicast address record to which
2288 * the membership points.
2290 (void)in6_mc_leave_locked(inm, imf);
2292 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2293 error = in6m_merge(inm, imf);
2295 CTR1(KTR_MLD, "%s: failed to merge inm state",
2298 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2299 error = mld_change_state(inm, 0);
2301 CTR1(KTR_MLD, "%s: failed mld downcall",
2316 /* Remove the gap in the membership array. */
2317 for (++idx; idx < imo->im6o_num_memberships; ++idx) {
2318 imo->im6o_membership[idx-1] = imo->im6o_membership[idx];
2319 imo->im6o_mfilters[idx-1] = imo->im6o_mfilters[idx];
2321 imo->im6o_num_memberships--;
2330 * Select the interface for transmitting IPv6 multicast datagrams.
2332 * Either an instance of struct in6_addr or an instance of struct ipv6_mreqn
2333 * may be passed to this socket option. An address of in6addr_any or an
2334 * interface index of 0 is used to remove a previous selection.
2335 * When no interface is selected, one is chosen for every send.
2338 in6p_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2341 struct ip6_moptions *imo;
2345 if (sopt->sopt_valsize != sizeof(u_int))
2348 error = sooptcopyin(sopt, &ifindex, sizeof(u_int), sizeof(u_int));
2351 if (V_if_index < ifindex)
2356 ifp = ifnet_byindex(ifindex);
2359 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2360 return (EADDRNOTAVAIL);
2362 imo = in6p_findmoptions(inp);
2363 imo->im6o_multicast_ifp = ifp;
2370 * Atomically set source filters on a socket for an IPv6 multicast group.
2372 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
2375 in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2377 struct __msfilterreq msfr;
2380 struct in6_mfilter *imf;
2381 struct ip6_moptions *imo;
2382 struct in6_multi *inm;
2386 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2387 sizeof(struct __msfilterreq));
2391 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
2394 if (msfr.msfr_fmode != MCAST_EXCLUDE &&
2395 msfr.msfr_fmode != MCAST_INCLUDE)
2398 if (msfr.msfr_group.ss_family != AF_INET6 ||
2399 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
2402 gsa = (sockunion_t *)&msfr.msfr_group;
2403 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2406 gsa->sin6.sin6_port = 0; /* ignore port */
2408 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
2409 return (EADDRNOTAVAIL);
2410 ifp = ifnet_byindex(msfr.msfr_ifindex);
2412 return (EADDRNOTAVAIL);
2413 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2416 * Take the INP write lock.
2417 * Check if this socket is a member of this group.
2419 imo = in6p_findmoptions(inp);
2420 idx = im6o_match_group(imo, ifp, &gsa->sa);
2421 if (idx == -1 || imo->im6o_mfilters == NULL) {
2422 error = EADDRNOTAVAIL;
2423 goto out_in6p_locked;
2425 inm = imo->im6o_membership[idx];
2426 imf = &imo->im6o_mfilters[idx];
2429 * Begin state merge transaction at socket layer.
2431 INP_WLOCK_ASSERT(inp);
2433 imf->im6f_st[1] = msfr.msfr_fmode;
2436 * Apply any new source filters, if present.
2437 * Make a copy of the user-space source vector so
2438 * that we may copy them with a single copyin. This
2439 * allows us to deal with page faults up-front.
2441 if (msfr.msfr_nsrcs > 0) {
2442 struct in6_msource *lims;
2443 struct sockaddr_in6 *psin;
2444 struct sockaddr_storage *kss, *pkss;
2449 CTR2(KTR_MLD, "%s: loading %lu source list entries",
2450 __func__, (unsigned long)msfr.msfr_nsrcs);
2451 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2453 error = copyin(msfr.msfr_srcs, kss,
2454 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2463 * Mark all source filters as UNDEFINED at t1.
2464 * Restore new group filter mode, as im6f_leave()
2465 * will set it to INCLUDE.
2468 imf->im6f_st[1] = msfr.msfr_fmode;
2471 * Update socket layer filters at t1, lazy-allocating
2472 * new entries. This saves a bunch of memory at the
2473 * cost of one RB_FIND() per source entry; duplicate
2474 * entries in the msfr_nsrcs vector are ignored.
2475 * If we encounter an error, rollback transaction.
2477 * XXX This too could be replaced with a set-symmetric
2478 * difference like loop to avoid walking from root
2479 * every time, as the key space is common.
2481 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2482 psin = (struct sockaddr_in6 *)pkss;
2483 if (psin->sin6_family != AF_INET6) {
2484 error = EAFNOSUPPORT;
2487 if (psin->sin6_len != sizeof(struct sockaddr_in6)) {
2491 if (IN6_IS_ADDR_MULTICAST(&psin->sin6_addr)) {
2496 * TODO: Validate embedded scope ID in source
2497 * list entry against passed-in ifp, if and only
2498 * if source list filter entry is iface or node local.
2500 in6_clearscope(&psin->sin6_addr);
2501 error = im6f_get_source(imf, psin, &lims);
2504 lims->im6sl_st[1] = imf->im6f_st[1];
2510 goto out_im6f_rollback;
2512 INP_WLOCK_ASSERT(inp);
2516 * Begin state merge transaction at MLD layer.
2518 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2519 error = in6m_merge(inm, imf);
2521 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
2523 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2524 error = mld_change_state(inm, 0);
2526 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
2545 * Set the IP multicast options in response to user setsockopt().
2547 * Many of the socket options handled in this function duplicate the
2548 * functionality of socket options in the regular unicast API. However,
2549 * it is not possible to merge the duplicate code, because the idempotence
2550 * of the IPv6 multicast part of the BSD Sockets API must be preserved;
2551 * the effects of these options must be treated as separate and distinct.
2553 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2556 ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2558 struct ip6_moptions *im6o;
2564 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2565 * or is a divert socket, reject it.
2567 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
2568 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2569 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
2570 return (EOPNOTSUPP);
2572 switch (sopt->sopt_name) {
2573 case IPV6_MULTICAST_IF:
2574 error = in6p_set_multicast_if(inp, sopt);
2577 case IPV6_MULTICAST_HOPS: {
2580 if (sopt->sopt_valsize != sizeof(int)) {
2584 error = sooptcopyin(sopt, &hlim, sizeof(hlim), sizeof(int));
2587 if (hlim < -1 || hlim > 255) {
2590 } else if (hlim == -1) {
2591 hlim = V_ip6_defmcasthlim;
2593 im6o = in6p_findmoptions(inp);
2594 im6o->im6o_multicast_hlim = hlim;
2599 case IPV6_MULTICAST_LOOP: {
2603 * Set the loopback flag for outgoing multicast packets.
2604 * Must be zero or one.
2606 if (sopt->sopt_valsize != sizeof(u_int)) {
2610 error = sooptcopyin(sopt, &loop, sizeof(u_int), sizeof(u_int));
2617 im6o = in6p_findmoptions(inp);
2618 im6o->im6o_multicast_loop = loop;
2623 case IPV6_JOIN_GROUP:
2624 case MCAST_JOIN_GROUP:
2625 case MCAST_JOIN_SOURCE_GROUP:
2626 error = in6p_join_group(inp, sopt);
2629 case IPV6_LEAVE_GROUP:
2630 case MCAST_LEAVE_GROUP:
2631 case MCAST_LEAVE_SOURCE_GROUP:
2632 error = in6p_leave_group(inp, sopt);
2635 case MCAST_BLOCK_SOURCE:
2636 case MCAST_UNBLOCK_SOURCE:
2637 error = in6p_block_unblock_source(inp, sopt);
2641 error = in6p_set_source_filters(inp, sopt);
2649 INP_UNLOCK_ASSERT(inp);
2655 * Expose MLD's multicast filter mode and source list(s) to userland,
2656 * keyed by (ifindex, group).
2657 * The filter mode is written out as a uint32_t, followed by
2658 * 0..n of struct in6_addr.
2659 * For use by ifmcstat(8).
2660 * SMPng: NOTE: unlocked read of ifindex space.
2663 sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS)
2665 struct in6_addr mcaddr;
2666 struct in6_addr src;
2668 struct ifmultiaddr *ifma;
2669 struct in6_multi *inm;
2670 struct ip6_msource *ims;
2674 uint32_t fmode, ifindex;
2676 char ip6tbuf[INET6_ADDRSTRLEN];
2682 if (req->newptr != NULL)
2685 /* int: ifindex + 4 * 32 bits of IPv6 address */
2690 if (ifindex <= 0 || ifindex > V_if_index) {
2691 CTR2(KTR_MLD, "%s: ifindex %u out of range",
2696 memcpy(&mcaddr, &name[1], sizeof(struct in6_addr));
2697 if (!IN6_IS_ADDR_MULTICAST(&mcaddr)) {
2698 CTR2(KTR_MLD, "%s: group %s is not multicast",
2699 __func__, ip6_sprintf(ip6tbuf, &mcaddr));
2703 ifp = ifnet_byindex(ifindex);
2705 CTR2(KTR_MLD, "%s: no ifp for ifindex %u",
2710 * Internal MLD lookups require that scope/zone ID is set.
2712 (void)in6_setscope(&mcaddr, ifp, NULL);
2714 retval = sysctl_wire_old_buffer(req,
2715 sizeof(uint32_t) + (in6_mcast_maxgrpsrc * sizeof(struct in6_addr)));
2722 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2723 if (ifma->ifma_addr->sa_family != AF_INET6 ||
2724 ifma->ifma_protospec == NULL)
2726 inm = (struct in6_multi *)ifma->ifma_protospec;
2727 if (!IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, &mcaddr))
2729 fmode = inm->in6m_st[1].iss_fmode;
2730 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2733 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
2734 CTR2(KTR_MLD, "%s: visit node %p", __func__, ims);
2736 * Only copy-out sources which are in-mode.
2738 if (fmode != im6s_get_mode(inm, ims, 1)) {
2739 CTR1(KTR_MLD, "%s: skip non-in-mode",
2743 src = ims->im6s_addr;
2744 retval = SYSCTL_OUT(req, &src,
2745 sizeof(struct in6_addr));
2750 IF_ADDR_RUNLOCK(ifp);
2759 static const char *in6m_modestrs[] = { "un", "in", "ex" };
2762 in6m_mode_str(const int mode)
2765 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2766 return (in6m_modestrs[mode]);
2770 static const char *in6m_statestrs[] = {
2783 in6m_state_str(const int state)
2786 if (state >= MLD_NOT_MEMBER && state <= MLD_LEAVING_MEMBER)
2787 return (in6m_statestrs[state]);
2792 * Dump an in6_multi structure to the console.
2795 in6m_print(const struct in6_multi *inm)
2798 char ip6tbuf[INET6_ADDRSTRLEN];
2800 if ((ktr_mask & KTR_MLD) == 0)
2803 printf("%s: --- begin in6m %p ---\n", __func__, inm);
2804 printf("addr %s ifp %p(%s) ifma %p\n",
2805 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2807 inm->in6m_ifp->if_xname,
2809 printf("timer %u state %s refcount %u scq.len %u\n",
2811 in6m_state_str(inm->in6m_state),
2813 inm->in6m_scq.ifq_len);
2814 printf("mli %p nsrc %lu sctimer %u scrv %u\n",
2819 for (t = 0; t < 2; t++) {
2820 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
2821 in6m_mode_str(inm->in6m_st[t].iss_fmode),
2822 inm->in6m_st[t].iss_asm,
2823 inm->in6m_st[t].iss_ex,
2824 inm->in6m_st[t].iss_in,
2825 inm->in6m_st[t].iss_rec);
2827 printf("%s: --- end in6m %p ---\n", __func__, inm);
2833 in6m_print(const struct in6_multi *inm)