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_var.h>
56 #include <net/if_dl.h>
57 #include <net/route.h>
60 #include <netinet/in.h>
61 #include <netinet/in_var.h>
62 #include <netinet6/in6_var.h>
63 #include <netinet/ip6.h>
64 #include <netinet/icmp6.h>
65 #include <netinet6/ip6_var.h>
66 #include <netinet/in_pcb.h>
67 #include <netinet/tcp_var.h>
68 #include <netinet6/nd6.h>
69 #include <netinet6/mld6_var.h>
70 #include <netinet6/scope6_var.h>
73 #define KTR_MLD KTR_INET6
76 #ifndef __SOCKUNION_DECLARED
78 struct sockaddr_storage ss;
80 struct sockaddr_dl sdl;
81 struct sockaddr_in6 sin6;
83 typedef union sockunion sockunion_t;
84 #define __SOCKUNION_DECLARED
85 #endif /* __SOCKUNION_DECLARED */
87 static MALLOC_DEFINE(M_IN6MFILTER, "in6_mfilter",
88 "IPv6 multicast PCB-layer source filter");
89 static MALLOC_DEFINE(M_IP6MADDR, "in6_multi", "IPv6 multicast group");
90 static MALLOC_DEFINE(M_IP6MOPTS, "ip6_moptions", "IPv6 multicast options");
91 static MALLOC_DEFINE(M_IP6MSOURCE, "ip6_msource",
92 "IPv6 multicast MLD-layer source filter");
94 RB_GENERATE(ip6_msource_tree, ip6_msource, im6s_link, ip6_msource_cmp);
98 * - Lock order is: Giant, INP_WLOCK, IN6_MULTI_LOCK, MLD_LOCK, IF_ADDR_LOCK.
99 * - The IF_ADDR_LOCK is implicitly taken by in6m_lookup() earlier, however
100 * it can be taken by code in net/if.c also.
101 * - ip6_moptions and in6_mfilter are covered by the INP_WLOCK.
103 * struct in6_multi is covered by IN6_MULTI_LOCK. There isn't strictly
104 * any need for in6_multi itself to be virtualized -- it is bound to an ifp
105 * anyway no matter what happens.
107 struct mtx in6_multi_mtx;
108 MTX_SYSINIT(in6_multi_mtx, &in6_multi_mtx, "in6_multi_mtx", MTX_DEF);
110 static void im6f_commit(struct in6_mfilter *);
111 static int im6f_get_source(struct in6_mfilter *imf,
112 const struct sockaddr_in6 *psin,
113 struct in6_msource **);
114 static struct in6_msource *
115 im6f_graft(struct in6_mfilter *, const uint8_t,
116 const struct sockaddr_in6 *);
117 static void im6f_leave(struct in6_mfilter *);
118 static int im6f_prune(struct in6_mfilter *, const struct sockaddr_in6 *);
119 static void im6f_purge(struct in6_mfilter *);
120 static void im6f_rollback(struct in6_mfilter *);
121 static void im6f_reap(struct in6_mfilter *);
122 static int im6o_grow(struct ip6_moptions *);
123 static size_t im6o_match_group(const struct ip6_moptions *,
124 const struct ifnet *, const struct sockaddr *);
125 static struct in6_msource *
126 im6o_match_source(const struct ip6_moptions *, const size_t,
127 const struct sockaddr *);
128 static void im6s_merge(struct ip6_msource *ims,
129 const struct in6_msource *lims, const int rollback);
130 static int in6_mc_get(struct ifnet *, const struct in6_addr *,
131 struct in6_multi **);
132 static int in6m_get_source(struct in6_multi *inm,
133 const struct in6_addr *addr, const int noalloc,
134 struct ip6_msource **pims);
136 static int in6m_is_ifp_detached(const struct in6_multi *);
138 static int in6m_merge(struct in6_multi *, /*const*/ struct in6_mfilter *);
139 static void in6m_purge(struct in6_multi *);
140 static void in6m_reap(struct in6_multi *);
141 static struct ip6_moptions *
142 in6p_findmoptions(struct inpcb *);
143 static int in6p_get_source_filters(struct inpcb *, struct sockopt *);
144 static int in6p_join_group(struct inpcb *, struct sockopt *);
145 static int in6p_leave_group(struct inpcb *, struct sockopt *);
146 static struct ifnet *
147 in6p_lookup_mcast_ifp(const struct inpcb *,
148 const struct sockaddr_in6 *);
149 static int in6p_block_unblock_source(struct inpcb *, struct sockopt *);
150 static int in6p_set_multicast_if(struct inpcb *, struct sockopt *);
151 static int in6p_set_source_filters(struct inpcb *, struct sockopt *);
152 static int sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS);
154 SYSCTL_DECL(_net_inet6_ip6); /* XXX Not in any common header. */
156 static SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, mcast, CTLFLAG_RW, 0,
159 static u_long in6_mcast_maxgrpsrc = IPV6_MAX_GROUP_SRC_FILTER;
160 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxgrpsrc,
161 CTLFLAG_RWTUN, &in6_mcast_maxgrpsrc, 0,
162 "Max source filters per group");
164 static u_long in6_mcast_maxsocksrc = IPV6_MAX_SOCK_SRC_FILTER;
165 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxsocksrc,
166 CTLFLAG_RWTUN, &in6_mcast_maxsocksrc, 0,
167 "Max source filters per socket");
169 /* TODO Virtualize this switch. */
170 int in6_mcast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
171 SYSCTL_INT(_net_inet6_ip6_mcast, OID_AUTO, loop, CTLFLAG_RWTUN,
172 &in6_mcast_loop, 0, "Loopback multicast datagrams by default");
174 static SYSCTL_NODE(_net_inet6_ip6_mcast, OID_AUTO, filters,
175 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip6_mcast_filters,
176 "Per-interface stack-wide source filters");
180 * Inline function which wraps assertions for a valid ifp.
181 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
185 in6m_is_ifp_detached(const struct in6_multi *inm)
189 KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__));
190 ifp = inm->in6m_ifma->ifma_ifp;
193 * Sanity check that network-layer notion of ifp is the
194 * same as that of link-layer.
196 KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__));
199 return (ifp == NULL);
204 * Initialize an in6_mfilter structure to a known state at t0, t1
205 * with an empty source filter list.
208 im6f_init(struct in6_mfilter *imf, const int st0, const int st1)
210 memset(imf, 0, sizeof(struct in6_mfilter));
211 RB_INIT(&imf->im6f_sources);
212 imf->im6f_st[0] = st0;
213 imf->im6f_st[1] = st1;
217 * Resize the ip6_moptions vector to the next power-of-two minus 1.
218 * May be called with locks held; do not sleep.
221 im6o_grow(struct ip6_moptions *imo)
223 struct in6_multi **nmships;
224 struct in6_multi **omships;
225 struct in6_mfilter *nmfilters;
226 struct in6_mfilter *omfilters;
233 omships = imo->im6o_membership;
234 omfilters = imo->im6o_mfilters;
235 oldmax = imo->im6o_max_memberships;
236 newmax = ((oldmax + 1) * 2) - 1;
238 if (newmax <= IPV6_MAX_MEMBERSHIPS) {
239 nmships = (struct in6_multi **)realloc(omships,
240 sizeof(struct in6_multi *) * newmax, M_IP6MOPTS, M_NOWAIT);
241 nmfilters = (struct in6_mfilter *)realloc(omfilters,
242 sizeof(struct in6_mfilter) * newmax, M_IN6MFILTER,
244 if (nmships != NULL && nmfilters != NULL) {
245 /* Initialize newly allocated source filter heads. */
246 for (idx = oldmax; idx < newmax; idx++) {
247 im6f_init(&nmfilters[idx], MCAST_UNDEFINED,
250 imo->im6o_max_memberships = newmax;
251 imo->im6o_membership = nmships;
252 imo->im6o_mfilters = nmfilters;
256 if (nmships == NULL || nmfilters == NULL) {
258 free(nmships, M_IP6MOPTS);
259 if (nmfilters != NULL)
260 free(nmfilters, M_IN6MFILTER);
261 return (ETOOMANYREFS);
268 * Find an IPv6 multicast group entry for this ip6_moptions instance
269 * which matches the specified group, and optionally an interface.
270 * Return its index into the array, or -1 if not found.
273 im6o_match_group(const struct ip6_moptions *imo, const struct ifnet *ifp,
274 const struct sockaddr *group)
276 const struct sockaddr_in6 *gsin6;
277 struct in6_multi **pinm;
281 gsin6 = (const struct sockaddr_in6 *)group;
283 /* The im6o_membership array may be lazy allocated. */
284 if (imo->im6o_membership == NULL || imo->im6o_num_memberships == 0)
287 nmships = imo->im6o_num_memberships;
288 pinm = &imo->im6o_membership[0];
289 for (idx = 0; idx < nmships; idx++, pinm++) {
292 if ((ifp == NULL || ((*pinm)->in6m_ifp == ifp)) &&
293 IN6_ARE_ADDR_EQUAL(&(*pinm)->in6m_addr,
294 &gsin6->sin6_addr)) {
305 * Find an IPv6 multicast source entry for this imo which matches
306 * the given group index for this socket, and source address.
308 * XXX TODO: The scope ID, if present in src, is stripped before
309 * any comparison. We SHOULD enforce scope/zone checks where the source
310 * filter entry has a link scope.
312 * NOTE: This does not check if the entry is in-mode, merely if
313 * it exists, which may not be the desired behaviour.
315 static struct in6_msource *
316 im6o_match_source(const struct ip6_moptions *imo, const size_t gidx,
317 const struct sockaddr *src)
319 struct ip6_msource find;
320 struct in6_mfilter *imf;
321 struct ip6_msource *ims;
322 const sockunion_t *psa;
324 KASSERT(src->sa_family == AF_INET6, ("%s: !AF_INET6", __func__));
325 KASSERT(gidx != -1 && gidx < imo->im6o_num_memberships,
326 ("%s: invalid index %d\n", __func__, (int)gidx));
328 /* The im6o_mfilters array may be lazy allocated. */
329 if (imo->im6o_mfilters == NULL)
331 imf = &imo->im6o_mfilters[gidx];
333 psa = (const sockunion_t *)src;
334 find.im6s_addr = psa->sin6.sin6_addr;
335 in6_clearscope(&find.im6s_addr); /* XXX */
336 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
338 return ((struct in6_msource *)ims);
342 * Perform filtering for multicast datagrams on a socket by group and source.
344 * Returns 0 if a datagram should be allowed through, or various error codes
345 * if the socket was not a member of the group, or the source was muted, etc.
348 im6o_mc_filter(const struct ip6_moptions *imo, const struct ifnet *ifp,
349 const struct sockaddr *group, const struct sockaddr *src)
352 struct in6_msource *ims;
355 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
357 gidx = im6o_match_group(imo, ifp, group);
359 return (MCAST_NOTGMEMBER);
362 * Check if the source was included in an (S,G) join.
363 * Allow reception on exclusive memberships by default,
364 * reject reception on inclusive memberships by default.
365 * Exclude source only if an in-mode exclude filter exists.
366 * Include source only if an in-mode include filter exists.
367 * NOTE: We are comparing group state here at MLD t1 (now)
368 * with socket-layer t0 (since last downcall).
370 mode = imo->im6o_mfilters[gidx].im6f_st[1];
371 ims = im6o_match_source(imo, gidx, src);
373 if ((ims == NULL && mode == MCAST_INCLUDE) ||
374 (ims != NULL && ims->im6sl_st[0] != mode))
375 return (MCAST_NOTSMEMBER);
381 * Find and return a reference to an in6_multi record for (ifp, group),
382 * and bump its reference count.
383 * If one does not exist, try to allocate it, and update link-layer multicast
384 * filters on ifp to listen for group.
385 * Assumes the IN6_MULTI lock is held across the call.
386 * Return 0 if successful, otherwise return an appropriate error code.
389 in6_mc_get(struct ifnet *ifp, const struct in6_addr *group,
390 struct in6_multi **pinm)
392 struct sockaddr_in6 gsin6;
393 struct ifmultiaddr *ifma;
394 struct in6_multi *inm;
400 * XXX: Accesses to ifma_protospec must be covered by IF_ADDR_LOCK;
401 * if_addmulti() takes this mutex itself, so we must drop and
402 * re-acquire around the call.
404 IN6_MULTI_LOCK_ASSERT();
407 inm = in6m_lookup_locked(ifp, group);
410 * If we already joined this group, just bump the
411 * refcount and return it.
413 KASSERT(inm->in6m_refcount >= 1,
414 ("%s: bad refcount %d", __func__, inm->in6m_refcount));
415 ++inm->in6m_refcount;
420 memset(&gsin6, 0, sizeof(gsin6));
421 gsin6.sin6_family = AF_INET6;
422 gsin6.sin6_len = sizeof(struct sockaddr_in6);
423 gsin6.sin6_addr = *group;
426 * Check if a link-layer group is already associated
427 * with this network-layer group on the given ifnet.
429 IF_ADDR_WUNLOCK(ifp);
430 error = if_addmulti(ifp, (struct sockaddr *)&gsin6, &ifma);
436 * If something other than netinet6 is occupying the link-layer
437 * group, print a meaningful error message and back out of
439 * Otherwise, bump the refcount on the existing network-layer
440 * group association and return it.
442 if (ifma->ifma_protospec != NULL) {
443 inm = (struct in6_multi *)ifma->ifma_protospec;
445 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
447 KASSERT(ifma->ifma_addr->sa_family == AF_INET6,
448 ("%s: ifma not AF_INET6", __func__));
449 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
450 if (inm->in6m_ifma != ifma || inm->in6m_ifp != ifp ||
451 !IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, group))
452 panic("%s: ifma %p is inconsistent with %p (%p)",
453 __func__, ifma, inm, group);
455 ++inm->in6m_refcount;
460 IF_ADDR_WLOCK_ASSERT(ifp);
463 * A new in6_multi record is needed; allocate and initialize it.
464 * We DO NOT perform an MLD join as the in6_ layer may need to
465 * push an initial source list down to MLD to support SSM.
467 * The initial source filter state is INCLUDE, {} as per the RFC.
468 * Pending state-changes per group are subject to a bounds check.
470 inm = malloc(sizeof(*inm), M_IP6MADDR, M_NOWAIT | M_ZERO);
472 IF_ADDR_WUNLOCK(ifp);
473 if_delmulti_ifma(ifma);
476 inm->in6m_addr = *group;
478 inm->in6m_mli = MLD_IFINFO(ifp);
479 inm->in6m_ifma = ifma;
480 inm->in6m_refcount = 1;
481 inm->in6m_state = MLD_NOT_MEMBER;
482 mbufq_init(&inm->in6m_scq, MLD_MAX_STATE_CHANGES);
484 inm->in6m_st[0].iss_fmode = MCAST_UNDEFINED;
485 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
486 RB_INIT(&inm->in6m_srcs);
488 ifma->ifma_protospec = inm;
492 IF_ADDR_WUNLOCK(ifp);
497 * Drop a reference to an in6_multi record.
499 * If the refcount drops to 0, free the in6_multi record and
500 * delete the underlying link-layer membership.
503 in6m_release_locked(struct in6_multi *inm)
505 struct ifmultiaddr *ifma;
507 IN6_MULTI_LOCK_ASSERT();
509 CTR2(KTR_MLD, "%s: refcount is %d", __func__, inm->in6m_refcount);
511 if (--inm->in6m_refcount > 0) {
512 CTR2(KTR_MLD, "%s: refcount is now %d", __func__,
517 CTR2(KTR_MLD, "%s: freeing inm %p", __func__, inm);
519 ifma = inm->in6m_ifma;
521 /* XXX this access is not covered by IF_ADDR_LOCK */
522 CTR2(KTR_MLD, "%s: purging ifma %p", __func__, ifma);
523 KASSERT(ifma->ifma_protospec == inm,
524 ("%s: ifma_protospec != inm", __func__));
525 ifma->ifma_protospec = NULL;
529 free(inm, M_IP6MADDR);
531 if_delmulti_ifma(ifma);
535 * Clear recorded source entries for a group.
536 * Used by the MLD code. Caller must hold the IN6_MULTI lock.
537 * FIXME: Should reap.
540 in6m_clear_recorded(struct in6_multi *inm)
542 struct ip6_msource *ims;
544 IN6_MULTI_LOCK_ASSERT();
546 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
549 --inm->in6m_st[1].iss_rec;
552 KASSERT(inm->in6m_st[1].iss_rec == 0,
553 ("%s: iss_rec %d not 0", __func__, inm->in6m_st[1].iss_rec));
557 * Record a source as pending for a Source-Group MLDv2 query.
558 * This lives here as it modifies the shared tree.
560 * inm is the group descriptor.
561 * naddr is the address of the source to record in network-byte order.
563 * If the net.inet6.mld.sgalloc sysctl is non-zero, we will
564 * lazy-allocate a source node in response to an SG query.
565 * Otherwise, no allocation is performed. This saves some memory
566 * with the trade-off that the source will not be reported to the
567 * router if joined in the window between the query response and
568 * the group actually being joined on the local host.
570 * VIMAGE: XXX: Currently the mld_sgalloc feature has been removed.
571 * This turns off the allocation of a recorded source entry if
572 * the group has not been joined.
574 * Return 0 if the source didn't exist or was already marked as recorded.
575 * Return 1 if the source was marked as recorded by this function.
576 * Return <0 if any error occured (negated errno code).
579 in6m_record_source(struct in6_multi *inm, const struct in6_addr *addr)
581 struct ip6_msource find;
582 struct ip6_msource *ims, *nims;
584 IN6_MULTI_LOCK_ASSERT();
586 find.im6s_addr = *addr;
587 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
588 if (ims && ims->im6s_stp)
591 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
593 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
597 nims->im6s_addr = find.im6s_addr;
598 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
604 * Mark the source as recorded and update the recorded
608 ++inm->in6m_st[1].iss_rec;
614 * Return a pointer to an in6_msource owned by an in6_mfilter,
615 * given its source address.
616 * Lazy-allocate if needed. If this is a new entry its filter state is
619 * imf is the filter set being modified.
620 * addr is the source address.
622 * SMPng: May be called with locks held; malloc must not block.
625 im6f_get_source(struct in6_mfilter *imf, const struct sockaddr_in6 *psin,
626 struct in6_msource **plims)
628 struct ip6_msource find;
629 struct ip6_msource *ims, *nims;
630 struct in6_msource *lims;
637 find.im6s_addr = psin->sin6_addr;
638 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
639 lims = (struct in6_msource *)ims;
641 if (imf->im6f_nsrc == in6_mcast_maxsocksrc)
643 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
647 lims = (struct in6_msource *)nims;
648 lims->im6s_addr = find.im6s_addr;
649 lims->im6sl_st[0] = MCAST_UNDEFINED;
650 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
660 * Graft a source entry into an existing socket-layer filter set,
661 * maintaining any required invariants and checking allocations.
663 * The source is marked as being in the new filter mode at t1.
665 * Return the pointer to the new node, otherwise return NULL.
667 static struct in6_msource *
668 im6f_graft(struct in6_mfilter *imf, const uint8_t st1,
669 const struct sockaddr_in6 *psin)
671 struct ip6_msource *nims;
672 struct in6_msource *lims;
674 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
678 lims = (struct in6_msource *)nims;
679 lims->im6s_addr = psin->sin6_addr;
680 lims->im6sl_st[0] = MCAST_UNDEFINED;
681 lims->im6sl_st[1] = st1;
682 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
689 * Prune a source entry from an existing socket-layer filter set,
690 * maintaining any required invariants and checking allocations.
692 * The source is marked as being left at t1, it is not freed.
694 * Return 0 if no error occurred, otherwise return an errno value.
697 im6f_prune(struct in6_mfilter *imf, const struct sockaddr_in6 *psin)
699 struct ip6_msource find;
700 struct ip6_msource *ims;
701 struct in6_msource *lims;
703 find.im6s_addr = psin->sin6_addr;
704 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
707 lims = (struct in6_msource *)ims;
708 lims->im6sl_st[1] = MCAST_UNDEFINED;
713 * Revert socket-layer filter set deltas at t1 to t0 state.
716 im6f_rollback(struct in6_mfilter *imf)
718 struct ip6_msource *ims, *tims;
719 struct in6_msource *lims;
721 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
722 lims = (struct in6_msource *)ims;
723 if (lims->im6sl_st[0] == lims->im6sl_st[1]) {
724 /* no change at t1 */
726 } else if (lims->im6sl_st[0] != MCAST_UNDEFINED) {
727 /* revert change to existing source at t1 */
728 lims->im6sl_st[1] = lims->im6sl_st[0];
730 /* revert source added t1 */
731 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
732 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
733 free(ims, M_IN6MFILTER);
737 imf->im6f_st[1] = imf->im6f_st[0];
741 * Mark socket-layer filter set as INCLUDE {} at t1.
744 im6f_leave(struct in6_mfilter *imf)
746 struct ip6_msource *ims;
747 struct in6_msource *lims;
749 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
750 lims = (struct in6_msource *)ims;
751 lims->im6sl_st[1] = MCAST_UNDEFINED;
753 imf->im6f_st[1] = MCAST_INCLUDE;
757 * Mark socket-layer filter set deltas as committed.
760 im6f_commit(struct in6_mfilter *imf)
762 struct ip6_msource *ims;
763 struct in6_msource *lims;
765 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
766 lims = (struct in6_msource *)ims;
767 lims->im6sl_st[0] = lims->im6sl_st[1];
769 imf->im6f_st[0] = imf->im6f_st[1];
773 * Reap unreferenced sources from socket-layer filter set.
776 im6f_reap(struct in6_mfilter *imf)
778 struct ip6_msource *ims, *tims;
779 struct in6_msource *lims;
781 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
782 lims = (struct in6_msource *)ims;
783 if ((lims->im6sl_st[0] == MCAST_UNDEFINED) &&
784 (lims->im6sl_st[1] == MCAST_UNDEFINED)) {
785 CTR2(KTR_MLD, "%s: free lims %p", __func__, ims);
786 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
787 free(ims, M_IN6MFILTER);
794 * Purge socket-layer filter set.
797 im6f_purge(struct in6_mfilter *imf)
799 struct ip6_msource *ims, *tims;
801 RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
802 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
803 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
804 free(ims, M_IN6MFILTER);
807 imf->im6f_st[0] = imf->im6f_st[1] = MCAST_UNDEFINED;
808 KASSERT(RB_EMPTY(&imf->im6f_sources),
809 ("%s: im6f_sources not empty", __func__));
813 * Look up a source filter entry for a multicast group.
815 * inm is the group descriptor to work with.
816 * addr is the IPv6 address to look up.
817 * noalloc may be non-zero to suppress allocation of sources.
818 * *pims will be set to the address of the retrieved or allocated source.
820 * SMPng: NOTE: may be called with locks held.
821 * Return 0 if successful, otherwise return a non-zero error code.
824 in6m_get_source(struct in6_multi *inm, const struct in6_addr *addr,
825 const int noalloc, struct ip6_msource **pims)
827 struct ip6_msource find;
828 struct ip6_msource *ims, *nims;
830 char ip6tbuf[INET6_ADDRSTRLEN];
833 find.im6s_addr = *addr;
834 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
835 if (ims == NULL && !noalloc) {
836 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
838 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
842 nims->im6s_addr = *addr;
843 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
846 CTR3(KTR_MLD, "%s: allocated %s as %p", __func__,
847 ip6_sprintf(ip6tbuf, addr), ims);
855 * Merge socket-layer source into MLD-layer source.
856 * If rollback is non-zero, perform the inverse of the merge.
859 im6s_merge(struct ip6_msource *ims, const struct in6_msource *lims,
862 int n = rollback ? -1 : 1;
864 char ip6tbuf[INET6_ADDRSTRLEN];
866 ip6_sprintf(ip6tbuf, &lims->im6s_addr);
869 if (lims->im6sl_st[0] == MCAST_EXCLUDE) {
870 CTR3(KTR_MLD, "%s: t1 ex -= %d on %s", __func__, n, ip6tbuf);
871 ims->im6s_st[1].ex -= n;
872 } else if (lims->im6sl_st[0] == MCAST_INCLUDE) {
873 CTR3(KTR_MLD, "%s: t1 in -= %d on %s", __func__, n, ip6tbuf);
874 ims->im6s_st[1].in -= n;
877 if (lims->im6sl_st[1] == MCAST_EXCLUDE) {
878 CTR3(KTR_MLD, "%s: t1 ex += %d on %s", __func__, n, ip6tbuf);
879 ims->im6s_st[1].ex += n;
880 } else if (lims->im6sl_st[1] == MCAST_INCLUDE) {
881 CTR3(KTR_MLD, "%s: t1 in += %d on %s", __func__, n, ip6tbuf);
882 ims->im6s_st[1].in += n;
887 * Atomically update the global in6_multi state, when a membership's
888 * filter list is being updated in any way.
890 * imf is the per-inpcb-membership group filter pointer.
891 * A fake imf may be passed for in-kernel consumers.
893 * XXX This is a candidate for a set-symmetric-difference style loop
894 * which would eliminate the repeated lookup from root of ims nodes,
895 * as they share the same key space.
897 * If any error occurred this function will back out of refcounts
898 * and return a non-zero value.
901 in6m_merge(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
903 struct ip6_msource *ims, *nims;
904 struct in6_msource *lims;
913 * Update the source filters first, as this may fail.
914 * Maintain count of in-mode filters at t0, t1. These are
915 * used to work out if we transition into ASM mode or not.
916 * Maintain a count of source filters whose state was
917 * actually modified by this operation.
919 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
920 lims = (struct in6_msource *)ims;
921 if (lims->im6sl_st[0] == imf->im6f_st[0]) nsrc0++;
922 if (lims->im6sl_st[1] == imf->im6f_st[1]) nsrc1++;
923 if (lims->im6sl_st[0] == lims->im6sl_st[1]) continue;
924 error = in6m_get_source(inm, &lims->im6s_addr, 0, &nims);
928 im6s_merge(nims, lims, 0);
931 struct ip6_msource *bims;
933 RB_FOREACH_REVERSE_FROM(ims, ip6_msource_tree, nims) {
934 lims = (struct in6_msource *)ims;
935 if (lims->im6sl_st[0] == lims->im6sl_st[1])
937 (void)in6m_get_source(inm, &lims->im6s_addr, 1, &bims);
940 im6s_merge(bims, lims, 1);
945 CTR3(KTR_MLD, "%s: imf filters in-mode: %d at t0, %d at t1",
946 __func__, nsrc0, nsrc1);
948 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
949 if (imf->im6f_st[0] == imf->im6f_st[1] &&
950 imf->im6f_st[1] == MCAST_INCLUDE) {
952 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
953 --inm->in6m_st[1].iss_in;
957 /* Handle filter mode transition on socket. */
958 if (imf->im6f_st[0] != imf->im6f_st[1]) {
959 CTR3(KTR_MLD, "%s: imf transition %d to %d",
960 __func__, imf->im6f_st[0], imf->im6f_st[1]);
962 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
963 CTR1(KTR_MLD, "%s: --ex on inm at t1", __func__);
964 --inm->in6m_st[1].iss_ex;
965 } else if (imf->im6f_st[0] == MCAST_INCLUDE) {
966 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
967 --inm->in6m_st[1].iss_in;
970 if (imf->im6f_st[1] == MCAST_EXCLUDE) {
971 CTR1(KTR_MLD, "%s: ex++ on inm at t1", __func__);
972 inm->in6m_st[1].iss_ex++;
973 } else if (imf->im6f_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
974 CTR1(KTR_MLD, "%s: in++ on inm at t1", __func__);
975 inm->in6m_st[1].iss_in++;
980 * Track inm filter state in terms of listener counts.
981 * If there are any exclusive listeners, stack-wide
982 * membership is exclusive.
983 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
984 * If no listeners remain, state is undefined at t1,
985 * and the MLD lifecycle for this group should finish.
987 if (inm->in6m_st[1].iss_ex > 0) {
988 CTR1(KTR_MLD, "%s: transition to EX", __func__);
989 inm->in6m_st[1].iss_fmode = MCAST_EXCLUDE;
990 } else if (inm->in6m_st[1].iss_in > 0) {
991 CTR1(KTR_MLD, "%s: transition to IN", __func__);
992 inm->in6m_st[1].iss_fmode = MCAST_INCLUDE;
994 CTR1(KTR_MLD, "%s: transition to UNDEF", __func__);
995 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
998 /* Decrement ASM listener count on transition out of ASM mode. */
999 if (imf->im6f_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1000 if ((imf->im6f_st[1] != MCAST_EXCLUDE) ||
1001 (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 > 0))
1002 CTR1(KTR_MLD, "%s: --asm on inm at t1", __func__);
1003 --inm->in6m_st[1].iss_asm;
1006 /* Increment ASM listener count on transition to ASM mode. */
1007 if (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1008 CTR1(KTR_MLD, "%s: asm++ on inm at t1", __func__);
1009 inm->in6m_st[1].iss_asm++;
1012 CTR3(KTR_MLD, "%s: merged imf %p to inm %p", __func__, imf, inm);
1017 CTR1(KTR_MLD, "%s: sources changed; reaping", __func__);
1024 * Mark an in6_multi's filter set deltas as committed.
1025 * Called by MLD after a state change has been enqueued.
1028 in6m_commit(struct in6_multi *inm)
1030 struct ip6_msource *ims;
1032 CTR2(KTR_MLD, "%s: commit inm %p", __func__, inm);
1033 CTR1(KTR_MLD, "%s: pre commit:", __func__);
1036 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
1037 ims->im6s_st[0] = ims->im6s_st[1];
1039 inm->in6m_st[0] = inm->in6m_st[1];
1043 * Reap unreferenced nodes from an in6_multi's filter set.
1046 in6m_reap(struct in6_multi *inm)
1048 struct ip6_msource *ims, *tims;
1050 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1051 if (ims->im6s_st[0].ex > 0 || ims->im6s_st[0].in > 0 ||
1052 ims->im6s_st[1].ex > 0 || ims->im6s_st[1].in > 0 ||
1055 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1056 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1057 free(ims, M_IP6MSOURCE);
1063 * Purge all source nodes from an in6_multi's filter set.
1066 in6m_purge(struct in6_multi *inm)
1068 struct ip6_msource *ims, *tims;
1070 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
1071 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
1072 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
1073 free(ims, M_IP6MSOURCE);
1076 /* Free state-change requests that might be queued. */
1077 mbufq_drain(&inm->in6m_scq);
1081 * Join a multicast address w/o sources.
1082 * KAME compatibility entry point.
1084 * SMPng: Assume no mc locks held by caller.
1086 struct in6_multi_mship *
1087 in6_joingroup(struct ifnet *ifp, struct in6_addr *mcaddr,
1088 int *errorp, int delay)
1090 struct in6_multi_mship *imm;
1093 imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT);
1099 delay = (delay * PR_FASTHZ) / hz;
1101 error = in6_mc_join(ifp, mcaddr, NULL, &imm->i6mm_maddr, delay);
1104 free(imm, M_IP6MADDR);
1112 * Leave a multicast address w/o sources.
1113 * KAME compatibility entry point.
1115 * SMPng: Assume no mc locks held by caller.
1118 in6_leavegroup(struct in6_multi_mship *imm)
1121 if (imm->i6mm_maddr != NULL)
1122 in6_mc_leave(imm->i6mm_maddr, NULL);
1123 free(imm, M_IP6MADDR);
1128 * Join a multicast group; unlocked entry point.
1130 * SMPng: XXX: in6_mc_join() is called from in6_control() when upper
1131 * locks are not held. Fortunately, ifp is unlikely to have been detached
1132 * at this point, so we assume it's OK to recurse.
1135 in6_mc_join(struct ifnet *ifp, const struct in6_addr *mcaddr,
1136 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1142 error = in6_mc_join_locked(ifp, mcaddr, imf, pinm, delay);
1149 * Join a multicast group; real entry point.
1151 * Only preserves atomicity at inm level.
1152 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1154 * If the MLD downcall fails, the group is not joined, and an error
1158 in6_mc_join_locked(struct ifnet *ifp, const struct in6_addr *mcaddr,
1159 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
1162 struct in6_mfilter timf;
1163 struct in6_multi *inm;
1166 char ip6tbuf[INET6_ADDRSTRLEN];
1171 * Sanity: Check scope zone ID was set for ifp, if and
1172 * only if group is scoped to an interface.
1174 KASSERT(IN6_IS_ADDR_MULTICAST(mcaddr),
1175 ("%s: not a multicast address", __func__));
1176 if (IN6_IS_ADDR_MC_LINKLOCAL(mcaddr) ||
1177 IN6_IS_ADDR_MC_INTFACELOCAL(mcaddr)) {
1178 KASSERT(mcaddr->s6_addr16[1] != 0,
1179 ("%s: scope zone ID not set", __func__));
1183 IN6_MULTI_LOCK_ASSERT();
1185 CTR4(KTR_MLD, "%s: join %s on %p(%s))", __func__,
1186 ip6_sprintf(ip6tbuf, mcaddr), ifp, if_name(ifp));
1192 * If no imf was specified (i.e. kernel consumer),
1193 * fake one up and assume it is an ASM join.
1196 im6f_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1200 error = in6_mc_get(ifp, mcaddr, &inm);
1202 CTR1(KTR_MLD, "%s: in6_mc_get() failure", __func__);
1206 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1207 error = in6m_merge(inm, imf);
1209 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1210 goto out_in6m_release;
1213 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1214 error = mld_change_state(inm, delay);
1216 CTR1(KTR_MLD, "%s: failed to update source", __func__);
1217 goto out_in6m_release;
1222 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1223 in6m_release_locked(inm);
1232 * Leave a multicast group; unlocked entry point.
1235 in6_mc_leave(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1240 ifp = inm->in6m_ifp;
1243 error = in6_mc_leave_locked(inm, imf);
1250 * Leave a multicast group; real entry point.
1251 * All source filters will be expunged.
1253 * Only preserves atomicity at inm level.
1255 * Holding the write lock for the INP which contains imf
1256 * is highly advisable. We can't assert for it as imf does not
1257 * contain a back-pointer to the owning inp.
1259 * Note: This is not the same as in6m_release(*) as this function also
1260 * makes a state change downcall into MLD.
1263 in6_mc_leave_locked(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
1265 struct in6_mfilter timf;
1268 char ip6tbuf[INET6_ADDRSTRLEN];
1273 IN6_MULTI_LOCK_ASSERT();
1275 CTR5(KTR_MLD, "%s: leave inm %p, %s/%s, imf %p", __func__,
1276 inm, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1277 (in6m_is_ifp_detached(inm) ? "null" : if_name(inm->in6m_ifp)),
1281 * If no imf was specified (i.e. kernel consumer),
1282 * fake one up and assume it is an ASM join.
1285 im6f_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1290 * Begin state merge transaction at MLD layer.
1292 * As this particular invocation should not cause any memory
1293 * to be allocated, and there is no opportunity to roll back
1294 * the transaction, it MUST NOT fail.
1296 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1297 error = in6m_merge(inm, imf);
1298 KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1300 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1301 error = mld_change_state(inm, 0);
1303 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1305 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
1306 in6m_release_locked(inm);
1312 * Block or unblock an ASM multicast source on an inpcb.
1313 * This implements the delta-based API described in RFC 3678.
1315 * The delta-based API applies only to exclusive-mode memberships.
1316 * An MLD downcall will be performed.
1318 * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1320 * Return 0 if successful, otherwise return an appropriate error code.
1323 in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1325 struct group_source_req gsr;
1326 sockunion_t *gsa, *ssa;
1328 struct in6_mfilter *imf;
1329 struct ip6_moptions *imo;
1330 struct in6_msource *ims;
1331 struct in6_multi *inm;
1336 char ip6tbuf[INET6_ADDRSTRLEN];
1343 memset(&gsr, 0, sizeof(struct group_source_req));
1344 gsa = (sockunion_t *)&gsr.gsr_group;
1345 ssa = (sockunion_t *)&gsr.gsr_source;
1347 switch (sopt->sopt_name) {
1348 case MCAST_BLOCK_SOURCE:
1349 case MCAST_UNBLOCK_SOURCE:
1350 error = sooptcopyin(sopt, &gsr,
1351 sizeof(struct group_source_req),
1352 sizeof(struct group_source_req));
1356 if (gsa->sin6.sin6_family != AF_INET6 ||
1357 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1360 if (ssa->sin6.sin6_family != AF_INET6 ||
1361 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1364 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1365 return (EADDRNOTAVAIL);
1367 ifp = ifnet_byindex(gsr.gsr_interface);
1369 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1374 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1375 __func__, sopt->sopt_name);
1376 return (EOPNOTSUPP);
1380 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1383 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1386 * Check if we are actually a member of this group.
1388 imo = in6p_findmoptions(inp);
1389 idx = im6o_match_group(imo, ifp, &gsa->sa);
1390 if (idx == -1 || imo->im6o_mfilters == NULL) {
1391 error = EADDRNOTAVAIL;
1392 goto out_in6p_locked;
1395 KASSERT(imo->im6o_mfilters != NULL,
1396 ("%s: im6o_mfilters not allocated", __func__));
1397 imf = &imo->im6o_mfilters[idx];
1398 inm = imo->im6o_membership[idx];
1401 * Attempting to use the delta-based API on an
1402 * non exclusive-mode membership is an error.
1404 fmode = imf->im6f_st[0];
1405 if (fmode != MCAST_EXCLUDE) {
1407 goto out_in6p_locked;
1411 * Deal with error cases up-front:
1412 * Asked to block, but already blocked; or
1413 * Asked to unblock, but nothing to unblock.
1414 * If adding a new block entry, allocate it.
1416 ims = im6o_match_source(imo, idx, &ssa->sa);
1417 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1418 CTR3(KTR_MLD, "%s: source %s %spresent", __func__,
1419 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
1420 doblock ? "" : "not ");
1421 error = EADDRNOTAVAIL;
1422 goto out_in6p_locked;
1425 INP_WLOCK_ASSERT(inp);
1428 * Begin state merge transaction at socket layer.
1431 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
1432 ims = im6f_graft(imf, fmode, &ssa->sin6);
1436 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
1437 error = im6f_prune(imf, &ssa->sin6);
1441 CTR1(KTR_MLD, "%s: merge imf state failed", __func__);
1442 goto out_im6f_rollback;
1446 * Begin state merge transaction at MLD layer.
1450 CTR1(KTR_MLD, "%s: merge inm state", __func__);
1451 error = in6m_merge(inm, imf);
1453 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
1455 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
1456 error = mld_change_state(inm, 0);
1458 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
1477 * Given an inpcb, return its multicast options structure pointer. Accepts
1478 * an unlocked inpcb pointer, but will return it locked. May sleep.
1480 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1481 * SMPng: NOTE: Returns with the INP write lock held.
1483 static struct ip6_moptions *
1484 in6p_findmoptions(struct inpcb *inp)
1486 struct ip6_moptions *imo;
1487 struct in6_multi **immp;
1488 struct in6_mfilter *imfp;
1492 if (inp->in6p_moptions != NULL)
1493 return (inp->in6p_moptions);
1497 imo = malloc(sizeof(*imo), M_IP6MOPTS, M_WAITOK);
1498 immp = malloc(sizeof(*immp) * IPV6_MIN_MEMBERSHIPS, M_IP6MOPTS,
1500 imfp = malloc(sizeof(struct in6_mfilter) * IPV6_MIN_MEMBERSHIPS,
1501 M_IN6MFILTER, M_WAITOK);
1503 imo->im6o_multicast_ifp = NULL;
1504 imo->im6o_multicast_hlim = V_ip6_defmcasthlim;
1505 imo->im6o_multicast_loop = in6_mcast_loop;
1506 imo->im6o_num_memberships = 0;
1507 imo->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1508 imo->im6o_membership = immp;
1510 /* Initialize per-group source filters. */
1511 for (idx = 0; idx < IPV6_MIN_MEMBERSHIPS; idx++)
1512 im6f_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE);
1513 imo->im6o_mfilters = imfp;
1516 if (inp->in6p_moptions != NULL) {
1517 free(imfp, M_IN6MFILTER);
1518 free(immp, M_IP6MOPTS);
1519 free(imo, M_IP6MOPTS);
1520 return (inp->in6p_moptions);
1522 inp->in6p_moptions = imo;
1527 * Discard the IPv6 multicast options (and source filters).
1529 * SMPng: NOTE: assumes INP write lock is held.
1532 ip6_freemoptions(struct ip6_moptions *imo)
1534 struct in6_mfilter *imf;
1535 size_t idx, nmships;
1537 KASSERT(imo != NULL, ("%s: ip6_moptions is NULL", __func__));
1539 nmships = imo->im6o_num_memberships;
1540 for (idx = 0; idx < nmships; ++idx) {
1541 imf = imo->im6o_mfilters ? &imo->im6o_mfilters[idx] : NULL;
1544 /* XXX this will thrash the lock(s) */
1545 (void)in6_mc_leave(imo->im6o_membership[idx], imf);
1550 if (imo->im6o_mfilters)
1551 free(imo->im6o_mfilters, M_IN6MFILTER);
1552 free(imo->im6o_membership, M_IP6MOPTS);
1553 free(imo, M_IP6MOPTS);
1557 * Atomically get source filters on a socket for an IPv6 multicast group.
1558 * Called with INP lock held; returns with lock released.
1561 in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1563 struct __msfilterreq msfr;
1566 struct ip6_moptions *imo;
1567 struct in6_mfilter *imf;
1568 struct ip6_msource *ims;
1569 struct in6_msource *lims;
1570 struct sockaddr_in6 *psin;
1571 struct sockaddr_storage *ptss;
1572 struct sockaddr_storage *tss;
1574 size_t idx, nsrcs, ncsrcs;
1576 INP_WLOCK_ASSERT(inp);
1578 imo = inp->in6p_moptions;
1579 KASSERT(imo != NULL, ("%s: null ip6_moptions", __func__));
1583 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1584 sizeof(struct __msfilterreq));
1588 if (msfr.msfr_group.ss_family != AF_INET6 ||
1589 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
1592 gsa = (sockunion_t *)&msfr.msfr_group;
1593 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1596 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
1597 return (EADDRNOTAVAIL);
1598 ifp = ifnet_byindex(msfr.msfr_ifindex);
1600 return (EADDRNOTAVAIL);
1601 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1606 * Lookup group on the socket.
1608 idx = im6o_match_group(imo, ifp, &gsa->sa);
1609 if (idx == -1 || imo->im6o_mfilters == NULL) {
1611 return (EADDRNOTAVAIL);
1613 imf = &imo->im6o_mfilters[idx];
1616 * Ignore memberships which are in limbo.
1618 if (imf->im6f_st[1] == MCAST_UNDEFINED) {
1622 msfr.msfr_fmode = imf->im6f_st[1];
1625 * If the user specified a buffer, copy out the source filter
1626 * entries to userland gracefully.
1627 * We only copy out the number of entries which userland
1628 * has asked for, but we always tell userland how big the
1629 * buffer really needs to be.
1631 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
1632 msfr.msfr_nsrcs = in6_mcast_maxsocksrc;
1634 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1635 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1636 M_TEMP, M_NOWAIT | M_ZERO);
1644 * Count number of sources in-mode at t0.
1645 * If buffer space exists and remains, copy out source entries.
1647 nsrcs = msfr.msfr_nsrcs;
1650 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
1651 lims = (struct in6_msource *)ims;
1652 if (lims->im6sl_st[0] == MCAST_UNDEFINED ||
1653 lims->im6sl_st[0] != imf->im6f_st[0])
1656 if (tss != NULL && nsrcs > 0) {
1657 psin = (struct sockaddr_in6 *)ptss;
1658 psin->sin6_family = AF_INET6;
1659 psin->sin6_len = sizeof(struct sockaddr_in6);
1660 psin->sin6_addr = lims->im6s_addr;
1661 psin->sin6_port = 0;
1670 error = copyout(tss, msfr.msfr_srcs,
1671 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1677 msfr.msfr_nsrcs = ncsrcs;
1678 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1684 * Return the IP multicast options in response to user getsockopt().
1687 ip6_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1689 struct ip6_moptions *im6o;
1694 im6o = inp->in6p_moptions;
1696 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1697 * or is a divert socket, reject it.
1699 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1700 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1701 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
1703 return (EOPNOTSUPP);
1707 switch (sopt->sopt_name) {
1708 case IPV6_MULTICAST_IF:
1709 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) {
1712 optval = im6o->im6o_multicast_ifp->if_index;
1715 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1718 case IPV6_MULTICAST_HOPS:
1720 optval = V_ip6_defmcasthlim;
1722 optval = im6o->im6o_multicast_hlim;
1724 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1727 case IPV6_MULTICAST_LOOP:
1729 optval = in6_mcast_loop; /* XXX VIMAGE */
1731 optval = im6o->im6o_multicast_loop;
1733 error = sooptcopyout(sopt, &optval, sizeof(u_int));
1738 error = EADDRNOTAVAIL;
1741 error = in6p_get_source_filters(inp, sopt);
1747 error = ENOPROTOOPT;
1751 INP_UNLOCK_ASSERT(inp);
1757 * Look up the ifnet to use for a multicast group membership,
1758 * given the address of an IPv6 group.
1760 * This routine exists to support legacy IPv6 multicast applications.
1762 * If inp is non-NULL, use this socket's current FIB number for any
1763 * required FIB lookup. Look up the group address in the unicast FIB,
1764 * and use its ifp; usually, this points to the default next-hop.
1765 * If the FIB lookup fails, return NULL.
1767 * FUTURE: Support multiple forwarding tables for IPv6.
1769 * Returns NULL if no ifp could be found.
1771 static struct ifnet *
1772 in6p_lookup_mcast_ifp(const struct inpcb *in6p,
1773 const struct sockaddr_in6 *gsin6)
1775 struct route_in6 ro6;
1778 KASSERT(in6p->inp_vflag & INP_IPV6,
1779 ("%s: not INP_IPV6 inpcb", __func__));
1780 KASSERT(gsin6->sin6_family == AF_INET6,
1781 ("%s: not AF_INET6 group", __func__));
1784 memset(&ro6, 0, sizeof(struct route_in6));
1785 memcpy(&ro6.ro_dst, gsin6, sizeof(struct sockaddr_in6));
1786 rtalloc_ign_fib((struct route *)&ro6, 0,
1787 in6p ? in6p->inp_inc.inc_fibnum : RT_DEFAULT_FIB);
1788 if (ro6.ro_rt != NULL) {
1789 ifp = ro6.ro_rt->rt_ifp;
1790 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
1798 * Join an IPv6 multicast group, possibly with a source.
1800 * FIXME: The KAME use of the unspecified address (::)
1801 * to join *all* multicast groups is currently unsupported.
1804 in6p_join_group(struct inpcb *inp, struct sockopt *sopt)
1806 struct group_source_req gsr;
1807 sockunion_t *gsa, *ssa;
1809 struct in6_mfilter *imf;
1810 struct ip6_moptions *imo;
1811 struct in6_multi *inm;
1812 struct in6_msource *lims;
1822 memset(&gsr, 0, sizeof(struct group_source_req));
1823 gsa = (sockunion_t *)&gsr.gsr_group;
1824 gsa->ss.ss_family = AF_UNSPEC;
1825 ssa = (sockunion_t *)&gsr.gsr_source;
1826 ssa->ss.ss_family = AF_UNSPEC;
1829 * Chew everything into struct group_source_req.
1830 * Overwrite the port field if present, as the sockaddr
1831 * being copied in may be matched with a binary comparison.
1832 * Ignore passed-in scope ID.
1834 switch (sopt->sopt_name) {
1835 case IPV6_JOIN_GROUP: {
1836 struct ipv6_mreq mreq;
1838 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
1839 sizeof(struct ipv6_mreq));
1843 gsa->sin6.sin6_family = AF_INET6;
1844 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
1845 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
1847 if (mreq.ipv6mr_interface == 0) {
1848 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
1850 if (V_if_index < mreq.ipv6mr_interface)
1851 return (EADDRNOTAVAIL);
1852 ifp = ifnet_byindex(mreq.ipv6mr_interface);
1854 CTR3(KTR_MLD, "%s: ipv6mr_interface = %d, ifp = %p",
1855 __func__, mreq.ipv6mr_interface, ifp);
1858 case MCAST_JOIN_GROUP:
1859 case MCAST_JOIN_SOURCE_GROUP:
1860 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1861 error = sooptcopyin(sopt, &gsr,
1862 sizeof(struct group_req),
1863 sizeof(struct group_req));
1864 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1865 error = sooptcopyin(sopt, &gsr,
1866 sizeof(struct group_source_req),
1867 sizeof(struct group_source_req));
1872 if (gsa->sin6.sin6_family != AF_INET6 ||
1873 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1876 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1877 if (ssa->sin6.sin6_family != AF_INET6 ||
1878 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
1880 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
1883 * TODO: Validate embedded scope ID in source
1884 * list entry against passed-in ifp, if and only
1885 * if source list filter entry is iface or node local.
1887 in6_clearscope(&ssa->sin6.sin6_addr);
1888 ssa->sin6.sin6_port = 0;
1889 ssa->sin6.sin6_scope_id = 0;
1892 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1893 return (EADDRNOTAVAIL);
1894 ifp = ifnet_byindex(gsr.gsr_interface);
1898 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
1899 __func__, sopt->sopt_name);
1900 return (EOPNOTSUPP);
1904 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
1907 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
1908 return (EADDRNOTAVAIL);
1910 gsa->sin6.sin6_port = 0;
1911 gsa->sin6.sin6_scope_id = 0;
1914 * Always set the scope zone ID on memberships created from userland.
1915 * Use the passed-in ifp to do this.
1916 * XXX The in6_setscope() return value is meaningless.
1917 * XXX SCOPE6_LOCK() is taken by in6_setscope().
1919 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
1921 imo = in6p_findmoptions(inp);
1922 idx = im6o_match_group(imo, ifp, &gsa->sa);
1926 inm = imo->im6o_membership[idx];
1927 imf = &imo->im6o_mfilters[idx];
1928 if (ssa->ss.ss_family != AF_UNSPEC) {
1930 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
1931 * is an error. On an existing inclusive membership,
1932 * it just adds the source to the filter list.
1934 if (imf->im6f_st[1] != MCAST_INCLUDE) {
1936 goto out_in6p_locked;
1939 * Throw out duplicates.
1941 * XXX FIXME: This makes a naive assumption that
1942 * even if entries exist for *ssa in this imf,
1943 * they will be rejected as dupes, even if they
1944 * are not valid in the current mode (in-mode).
1946 * in6_msource is transactioned just as for anything
1947 * else in SSM -- but note naive use of in6m_graft()
1948 * below for allocating new filter entries.
1950 * This is only an issue if someone mixes the
1951 * full-state SSM API with the delta-based API,
1952 * which is discouraged in the relevant RFCs.
1954 lims = im6o_match_source(imo, idx, &ssa->sa);
1955 if (lims != NULL /*&&
1956 lims->im6sl_st[1] == MCAST_INCLUDE*/) {
1957 error = EADDRNOTAVAIL;
1958 goto out_in6p_locked;
1962 * MCAST_JOIN_GROUP alone, on any existing membership,
1963 * is rejected, to stop the same inpcb tying up
1964 * multiple refs to the in_multi.
1965 * On an existing inclusive membership, this is also
1966 * an error; if you want to change filter mode,
1967 * you must use the userland API setsourcefilter().
1968 * XXX We don't reject this for imf in UNDEFINED
1969 * state at t1, because allocation of a filter
1970 * is atomic with allocation of a membership.
1973 goto out_in6p_locked;
1978 * Begin state merge transaction at socket layer.
1980 INP_WLOCK_ASSERT(inp);
1983 if (imo->im6o_num_memberships == imo->im6o_max_memberships) {
1984 error = im6o_grow(imo);
1986 goto out_in6p_locked;
1989 * Allocate the new slot upfront so we can deal with
1990 * grafting the new source filter in same code path
1991 * as for join-source on existing membership.
1993 idx = imo->im6o_num_memberships;
1994 imo->im6o_membership[idx] = NULL;
1995 imo->im6o_num_memberships++;
1996 KASSERT(imo->im6o_mfilters != NULL,
1997 ("%s: im6f_mfilters vector was not allocated", __func__));
1998 imf = &imo->im6o_mfilters[idx];
1999 KASSERT(RB_EMPTY(&imf->im6f_sources),
2000 ("%s: im6f_sources not empty", __func__));
2004 * Graft new source into filter list for this inpcb's
2005 * membership of the group. The in6_multi may not have
2006 * been allocated yet if this is a new membership, however,
2007 * the in_mfilter slot will be allocated and must be initialized.
2009 * Note: Grafting of exclusive mode filters doesn't happen
2011 * XXX: Should check for non-NULL lims (node exists but may
2012 * not be in-mode) for interop with full-state API.
2014 if (ssa->ss.ss_family != AF_UNSPEC) {
2015 /* Membership starts in IN mode */
2017 CTR1(KTR_MLD, "%s: new join w/source", __func__);
2018 im6f_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE);
2020 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
2022 lims = im6f_graft(imf, MCAST_INCLUDE, &ssa->sin6);
2024 CTR1(KTR_MLD, "%s: merge imf state failed",
2030 /* No address specified; Membership starts in EX mode */
2032 CTR1(KTR_MLD, "%s: new join w/o source", __func__);
2033 im6f_init(imf, MCAST_UNDEFINED, MCAST_EXCLUDE);
2038 * Begin state merge transaction at MLD layer.
2043 error = in6_mc_join_locked(ifp, &gsa->sin6.sin6_addr, imf,
2049 imo->im6o_membership[idx] = inm;
2051 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2052 error = in6m_merge(inm, imf);
2054 CTR1(KTR_MLD, "%s: failed to merge inm state",
2057 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2058 error = mld_change_state(inm, 0);
2060 CTR1(KTR_MLD, "%s: failed mld downcall",
2066 INP_WLOCK_ASSERT(inp);
2078 if (error && is_new) {
2079 imo->im6o_membership[idx] = NULL;
2080 --imo->im6o_num_memberships;
2089 * Leave an IPv6 multicast group on an inpcb, possibly with a source.
2092 in6p_leave_group(struct inpcb *inp, struct sockopt *sopt)
2094 struct ipv6_mreq mreq;
2095 struct group_source_req gsr;
2096 sockunion_t *gsa, *ssa;
2098 struct in6_mfilter *imf;
2099 struct ip6_moptions *imo;
2100 struct in6_msource *ims;
2101 struct in6_multi *inm;
2104 int error, is_final;
2106 char ip6tbuf[INET6_ADDRSTRLEN];
2114 memset(&gsr, 0, sizeof(struct group_source_req));
2115 gsa = (sockunion_t *)&gsr.gsr_group;
2116 gsa->ss.ss_family = AF_UNSPEC;
2117 ssa = (sockunion_t *)&gsr.gsr_source;
2118 ssa->ss.ss_family = AF_UNSPEC;
2121 * Chew everything passed in up into a struct group_source_req
2122 * as that is easier to process.
2123 * Note: Any embedded scope ID in the multicast group passed
2124 * in by userland is ignored, the interface index is the recommended
2125 * mechanism to specify an interface; see below.
2127 switch (sopt->sopt_name) {
2128 case IPV6_LEAVE_GROUP:
2129 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
2130 sizeof(struct ipv6_mreq));
2133 gsa->sin6.sin6_family = AF_INET6;
2134 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
2135 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
2136 gsa->sin6.sin6_port = 0;
2137 gsa->sin6.sin6_scope_id = 0;
2138 ifindex = mreq.ipv6mr_interface;
2141 case MCAST_LEAVE_GROUP:
2142 case MCAST_LEAVE_SOURCE_GROUP:
2143 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2144 error = sooptcopyin(sopt, &gsr,
2145 sizeof(struct group_req),
2146 sizeof(struct group_req));
2147 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2148 error = sooptcopyin(sopt, &gsr,
2149 sizeof(struct group_source_req),
2150 sizeof(struct group_source_req));
2155 if (gsa->sin6.sin6_family != AF_INET6 ||
2156 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2158 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2159 if (ssa->sin6.sin6_family != AF_INET6 ||
2160 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
2162 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
2165 * TODO: Validate embedded scope ID in source
2166 * list entry against passed-in ifp, if and only
2167 * if source list filter entry is iface or node local.
2169 in6_clearscope(&ssa->sin6.sin6_addr);
2171 gsa->sin6.sin6_port = 0;
2172 gsa->sin6.sin6_scope_id = 0;
2173 ifindex = gsr.gsr_interface;
2177 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
2178 __func__, sopt->sopt_name);
2179 return (EOPNOTSUPP);
2183 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2187 * Validate interface index if provided. If no interface index
2188 * was provided separately, attempt to look the membership up
2189 * from the default scope as a last resort to disambiguate
2190 * the membership we are being asked to leave.
2191 * XXX SCOPE6 lock potentially taken here.
2194 if (V_if_index < ifindex)
2195 return (EADDRNOTAVAIL);
2196 ifp = ifnet_byindex(ifindex);
2198 return (EADDRNOTAVAIL);
2199 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2201 error = sa6_embedscope(&gsa->sin6, V_ip6_use_defzone);
2203 return (EADDRNOTAVAIL);
2205 * Some badly behaved applications don't pass an ifindex
2206 * or a scope ID, which is an API violation. In this case,
2207 * perform a lookup as per a v6 join.
2209 * XXX For now, stomp on zone ID for the corner case.
2210 * This is not the 'KAME way', but we need to see the ifp
2211 * directly until such time as this implementation is
2212 * refactored, assuming the scope IDs are the way to go.
2214 ifindex = ntohs(gsa->sin6.sin6_addr.s6_addr16[1]);
2216 CTR2(KTR_MLD, "%s: warning: no ifindex, looking up "
2217 "ifp for group %s.", __func__,
2218 ip6_sprintf(ip6tbuf, &gsa->sin6.sin6_addr));
2219 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
2221 ifp = ifnet_byindex(ifindex);
2224 return (EADDRNOTAVAIL);
2227 CTR2(KTR_MLD, "%s: ifp = %p", __func__, ifp);
2228 KASSERT(ifp != NULL, ("%s: ifp did not resolve", __func__));
2231 * Find the membership in the membership array.
2233 imo = in6p_findmoptions(inp);
2234 idx = im6o_match_group(imo, ifp, &gsa->sa);
2236 error = EADDRNOTAVAIL;
2237 goto out_in6p_locked;
2239 inm = imo->im6o_membership[idx];
2240 imf = &imo->im6o_mfilters[idx];
2242 if (ssa->ss.ss_family != AF_UNSPEC)
2246 * Begin state merge transaction at socket layer.
2248 INP_WLOCK_ASSERT(inp);
2251 * If we were instructed only to leave a given source, do so.
2252 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2257 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
2258 error = EADDRNOTAVAIL;
2259 goto out_in6p_locked;
2261 ims = im6o_match_source(imo, idx, &ssa->sa);
2263 CTR3(KTR_MLD, "%s: source %p %spresent", __func__,
2264 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
2266 error = EADDRNOTAVAIL;
2267 goto out_in6p_locked;
2269 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
2270 error = im6f_prune(imf, &ssa->sin6);
2272 CTR1(KTR_MLD, "%s: merge imf state failed",
2274 goto out_in6p_locked;
2279 * Begin state merge transaction at MLD layer.
2285 * Give up the multicast address record to which
2286 * the membership points.
2288 (void)in6_mc_leave_locked(inm, imf);
2290 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2291 error = in6m_merge(inm, imf);
2293 CTR1(KTR_MLD, "%s: failed to merge inm state",
2296 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2297 error = mld_change_state(inm, 0);
2299 CTR1(KTR_MLD, "%s: failed mld downcall",
2314 /* Remove the gap in the membership array. */
2315 for (++idx; idx < imo->im6o_num_memberships; ++idx) {
2316 imo->im6o_membership[idx-1] = imo->im6o_membership[idx];
2317 imo->im6o_mfilters[idx-1] = imo->im6o_mfilters[idx];
2319 imo->im6o_num_memberships--;
2328 * Select the interface for transmitting IPv6 multicast datagrams.
2330 * Either an instance of struct in6_addr or an instance of struct ipv6_mreqn
2331 * may be passed to this socket option. An address of in6addr_any or an
2332 * interface index of 0 is used to remove a previous selection.
2333 * When no interface is selected, one is chosen for every send.
2336 in6p_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2339 struct ip6_moptions *imo;
2343 if (sopt->sopt_valsize != sizeof(u_int))
2346 error = sooptcopyin(sopt, &ifindex, sizeof(u_int), sizeof(u_int));
2349 if (V_if_index < ifindex)
2354 ifp = ifnet_byindex(ifindex);
2357 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2358 return (EADDRNOTAVAIL);
2360 imo = in6p_findmoptions(inp);
2361 imo->im6o_multicast_ifp = ifp;
2368 * Atomically set source filters on a socket for an IPv6 multicast group.
2370 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
2373 in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2375 struct __msfilterreq msfr;
2378 struct in6_mfilter *imf;
2379 struct ip6_moptions *imo;
2380 struct in6_multi *inm;
2384 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2385 sizeof(struct __msfilterreq));
2389 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
2392 if (msfr.msfr_fmode != MCAST_EXCLUDE &&
2393 msfr.msfr_fmode != MCAST_INCLUDE)
2396 if (msfr.msfr_group.ss_family != AF_INET6 ||
2397 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
2400 gsa = (sockunion_t *)&msfr.msfr_group;
2401 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
2404 gsa->sin6.sin6_port = 0; /* ignore port */
2406 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
2407 return (EADDRNOTAVAIL);
2408 ifp = ifnet_byindex(msfr.msfr_ifindex);
2410 return (EADDRNOTAVAIL);
2411 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
2414 * Take the INP write lock.
2415 * Check if this socket is a member of this group.
2417 imo = in6p_findmoptions(inp);
2418 idx = im6o_match_group(imo, ifp, &gsa->sa);
2419 if (idx == -1 || imo->im6o_mfilters == NULL) {
2420 error = EADDRNOTAVAIL;
2421 goto out_in6p_locked;
2423 inm = imo->im6o_membership[idx];
2424 imf = &imo->im6o_mfilters[idx];
2427 * Begin state merge transaction at socket layer.
2429 INP_WLOCK_ASSERT(inp);
2431 imf->im6f_st[1] = msfr.msfr_fmode;
2434 * Apply any new source filters, if present.
2435 * Make a copy of the user-space source vector so
2436 * that we may copy them with a single copyin. This
2437 * allows us to deal with page faults up-front.
2439 if (msfr.msfr_nsrcs > 0) {
2440 struct in6_msource *lims;
2441 struct sockaddr_in6 *psin;
2442 struct sockaddr_storage *kss, *pkss;
2447 CTR2(KTR_MLD, "%s: loading %lu source list entries",
2448 __func__, (unsigned long)msfr.msfr_nsrcs);
2449 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2451 error = copyin(msfr.msfr_srcs, kss,
2452 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2461 * Mark all source filters as UNDEFINED at t1.
2462 * Restore new group filter mode, as im6f_leave()
2463 * will set it to INCLUDE.
2466 imf->im6f_st[1] = msfr.msfr_fmode;
2469 * Update socket layer filters at t1, lazy-allocating
2470 * new entries. This saves a bunch of memory at the
2471 * cost of one RB_FIND() per source entry; duplicate
2472 * entries in the msfr_nsrcs vector are ignored.
2473 * If we encounter an error, rollback transaction.
2475 * XXX This too could be replaced with a set-symmetric
2476 * difference like loop to avoid walking from root
2477 * every time, as the key space is common.
2479 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2480 psin = (struct sockaddr_in6 *)pkss;
2481 if (psin->sin6_family != AF_INET6) {
2482 error = EAFNOSUPPORT;
2485 if (psin->sin6_len != sizeof(struct sockaddr_in6)) {
2489 if (IN6_IS_ADDR_MULTICAST(&psin->sin6_addr)) {
2494 * TODO: Validate embedded scope ID in source
2495 * list entry against passed-in ifp, if and only
2496 * if source list filter entry is iface or node local.
2498 in6_clearscope(&psin->sin6_addr);
2499 error = im6f_get_source(imf, psin, &lims);
2502 lims->im6sl_st[1] = imf->im6f_st[1];
2508 goto out_im6f_rollback;
2510 INP_WLOCK_ASSERT(inp);
2514 * Begin state merge transaction at MLD layer.
2516 CTR1(KTR_MLD, "%s: merge inm state", __func__);
2517 error = in6m_merge(inm, imf);
2519 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
2521 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
2522 error = mld_change_state(inm, 0);
2524 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
2543 * Set the IP multicast options in response to user setsockopt().
2545 * Many of the socket options handled in this function duplicate the
2546 * functionality of socket options in the regular unicast API. However,
2547 * it is not possible to merge the duplicate code, because the idempotence
2548 * of the IPv6 multicast part of the BSD Sockets API must be preserved;
2549 * the effects of these options must be treated as separate and distinct.
2551 * SMPng: XXX: Unlocked read of inp_socket believed OK.
2554 ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2556 struct ip6_moptions *im6o;
2562 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2563 * or is a divert socket, reject it.
2565 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
2566 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2567 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
2568 return (EOPNOTSUPP);
2570 switch (sopt->sopt_name) {
2571 case IPV6_MULTICAST_IF:
2572 error = in6p_set_multicast_if(inp, sopt);
2575 case IPV6_MULTICAST_HOPS: {
2578 if (sopt->sopt_valsize != sizeof(int)) {
2582 error = sooptcopyin(sopt, &hlim, sizeof(hlim), sizeof(int));
2585 if (hlim < -1 || hlim > 255) {
2588 } else if (hlim == -1) {
2589 hlim = V_ip6_defmcasthlim;
2591 im6o = in6p_findmoptions(inp);
2592 im6o->im6o_multicast_hlim = hlim;
2597 case IPV6_MULTICAST_LOOP: {
2601 * Set the loopback flag for outgoing multicast packets.
2602 * Must be zero or one.
2604 if (sopt->sopt_valsize != sizeof(u_int)) {
2608 error = sooptcopyin(sopt, &loop, sizeof(u_int), sizeof(u_int));
2615 im6o = in6p_findmoptions(inp);
2616 im6o->im6o_multicast_loop = loop;
2621 case IPV6_JOIN_GROUP:
2622 case MCAST_JOIN_GROUP:
2623 case MCAST_JOIN_SOURCE_GROUP:
2624 error = in6p_join_group(inp, sopt);
2627 case IPV6_LEAVE_GROUP:
2628 case MCAST_LEAVE_GROUP:
2629 case MCAST_LEAVE_SOURCE_GROUP:
2630 error = in6p_leave_group(inp, sopt);
2633 case MCAST_BLOCK_SOURCE:
2634 case MCAST_UNBLOCK_SOURCE:
2635 error = in6p_block_unblock_source(inp, sopt);
2639 error = in6p_set_source_filters(inp, sopt);
2647 INP_UNLOCK_ASSERT(inp);
2653 * Expose MLD's multicast filter mode and source list(s) to userland,
2654 * keyed by (ifindex, group).
2655 * The filter mode is written out as a uint32_t, followed by
2656 * 0..n of struct in6_addr.
2657 * For use by ifmcstat(8).
2658 * SMPng: NOTE: unlocked read of ifindex space.
2661 sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS)
2663 struct in6_addr mcaddr;
2664 struct in6_addr src;
2666 struct ifmultiaddr *ifma;
2667 struct in6_multi *inm;
2668 struct ip6_msource *ims;
2672 uint32_t fmode, ifindex;
2674 char ip6tbuf[INET6_ADDRSTRLEN];
2680 if (req->newptr != NULL)
2683 /* int: ifindex + 4 * 32 bits of IPv6 address */
2688 if (ifindex <= 0 || ifindex > V_if_index) {
2689 CTR2(KTR_MLD, "%s: ifindex %u out of range",
2694 memcpy(&mcaddr, &name[1], sizeof(struct in6_addr));
2695 if (!IN6_IS_ADDR_MULTICAST(&mcaddr)) {
2696 CTR2(KTR_MLD, "%s: group %s is not multicast",
2697 __func__, ip6_sprintf(ip6tbuf, &mcaddr));
2701 ifp = ifnet_byindex(ifindex);
2703 CTR2(KTR_MLD, "%s: no ifp for ifindex %u",
2708 * Internal MLD lookups require that scope/zone ID is set.
2710 (void)in6_setscope(&mcaddr, ifp, NULL);
2712 retval = sysctl_wire_old_buffer(req,
2713 sizeof(uint32_t) + (in6_mcast_maxgrpsrc * sizeof(struct in6_addr)));
2720 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2721 if (ifma->ifma_addr->sa_family != AF_INET6 ||
2722 ifma->ifma_protospec == NULL)
2724 inm = (struct in6_multi *)ifma->ifma_protospec;
2725 if (!IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, &mcaddr))
2727 fmode = inm->in6m_st[1].iss_fmode;
2728 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
2731 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
2732 CTR2(KTR_MLD, "%s: visit node %p", __func__, ims);
2734 * Only copy-out sources which are in-mode.
2736 if (fmode != im6s_get_mode(inm, ims, 1)) {
2737 CTR1(KTR_MLD, "%s: skip non-in-mode",
2741 src = ims->im6s_addr;
2742 retval = SYSCTL_OUT(req, &src,
2743 sizeof(struct in6_addr));
2748 IF_ADDR_RUNLOCK(ifp);
2757 static const char *in6m_modestrs[] = { "un", "in", "ex" };
2760 in6m_mode_str(const int mode)
2763 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
2764 return (in6m_modestrs[mode]);
2768 static const char *in6m_statestrs[] = {
2781 in6m_state_str(const int state)
2784 if (state >= MLD_NOT_MEMBER && state <= MLD_LEAVING_MEMBER)
2785 return (in6m_statestrs[state]);
2790 * Dump an in6_multi structure to the console.
2793 in6m_print(const struct in6_multi *inm)
2796 char ip6tbuf[INET6_ADDRSTRLEN];
2798 if ((ktr_mask & KTR_MLD) == 0)
2801 printf("%s: --- begin in6m %p ---\n", __func__, inm);
2802 printf("addr %s ifp %p(%s) ifma %p\n",
2803 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2805 if_name(inm->in6m_ifp),
2807 printf("timer %u state %s refcount %u scq.len %u\n",
2809 in6m_state_str(inm->in6m_state),
2811 mbufq_len(&inm->in6m_scq));
2812 printf("mli %p nsrc %lu sctimer %u scrv %u\n",
2817 for (t = 0; t < 2; t++) {
2818 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
2819 in6m_mode_str(inm->in6m_st[t].iss_fmode),
2820 inm->in6m_st[t].iss_asm,
2821 inm->in6m_st[t].iss_ex,
2822 inm->in6m_st[t].iss_in,
2823 inm->in6m_st[t].iss_rec);
2825 printf("%s: --- end in6m %p ---\n", __func__, inm);
2831 in6m_print(const struct in6_multi *inm)