in6_mcast.c revision 192923
1/* 2 * Copyright (c) 2009 Bruce Simpson. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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 15 * permission. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30/* 31 * IPv6 multicast socket, group, and socket option processing module. 32 * Normative references: RFC 2292, RFC 3492, RFC 3542, RFC 3678, RFC 3810. 33 */ 34 35#include <sys/cdefs.h> 36__FBSDID("$FreeBSD: head/sys/netinet6/in6_mcast.c 192923 2009-05-27 18:57:13Z bms $"); 37 38#include "opt_inet6.h" 39#include "opt_route.h" 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/kernel.h> 44#include <sys/malloc.h> 45#include <sys/mbuf.h> 46#include <sys/protosw.h> 47#include <sys/socket.h> 48#include <sys/socketvar.h> 49#include <sys/protosw.h> 50#include <sys/sysctl.h> 51#include <sys/priv.h> 52#include <sys/ktr.h> 53#include <sys/tree.h> 54#include <sys/vimage.h> 55 56#include <net/if.h> 57#include <net/if_dl.h> 58#include <net/route.h> 59#include <net/vnet.h> 60 61#include <netinet/in.h> 62#include <netinet/in_var.h> 63#include <netinet6/in6_var.h> 64#include <netinet/ip6.h> 65#include <netinet/icmp6.h> 66#include <netinet6/ip6_var.h> 67#include <netinet/in_pcb.h> 68#include <netinet/tcp_var.h> 69#include <netinet6/nd6.h> 70#include <netinet/vinet.h> 71#include <netinet6/vinet6.h> 72#include <netinet6/mld6_var.h> 73 74#ifndef KTR_MLD 75#define KTR_MLD KTR_INET6 76#endif 77 78#ifndef __SOCKUNION_DECLARED 79union sockunion { 80 struct sockaddr_storage ss; 81 struct sockaddr sa; 82 struct sockaddr_dl sdl; 83 struct sockaddr_in6 sin6; 84}; 85typedef union sockunion sockunion_t; 86#define __SOCKUNION_DECLARED 87#endif /* __SOCKUNION_DECLARED */ 88 89static MALLOC_DEFINE(M_IN6MFILTER, "in6_mfilter", 90 "IPv6 multicast PCB-layer source filter"); 91static MALLOC_DEFINE(M_IP6MADDR, "in6_multi", "IPv6 multicast group"); 92static MALLOC_DEFINE(M_IP6MOPTS, "ip6_moptions", "IPv6 multicast options"); 93static MALLOC_DEFINE(M_IP6MSOURCE, "ip6_msource", 94 "IPv6 multicast MLD-layer source filter"); 95 96RB_GENERATE(ip6_msource_tree, ip6_msource, im6s_link, ip6_msource_cmp); 97 98/* 99 * Locking: 100 * - Lock order is: Giant, INP_WLOCK, IN6_MULTI_LOCK, MLD_LOCK, IF_ADDR_LOCK. 101 * - The IF_ADDR_LOCK is implicitly taken by in6m_lookup() earlier, however 102 * it can be taken by code in net/if.c also. 103 * - ip6_moptions and in6_mfilter are covered by the INP_WLOCK. 104 * 105 * struct in6_multi is covered by IN6_MULTI_LOCK. There isn't strictly 106 * any need for in6_multi itself to be virtualized -- it is bound to an ifp 107 * anyway no matter what happens. 108 */ 109struct mtx in6_multi_mtx; 110MTX_SYSINIT(in6_multi_mtx, &in6_multi_mtx, "in6_multi_mtx", MTX_DEF); 111 112static void im6f_commit(struct in6_mfilter *); 113static int im6f_get_source(struct in6_mfilter *imf, 114 const struct sockaddr_in6 *psin, 115 struct in6_msource **); 116static struct in6_msource * 117 im6f_graft(struct in6_mfilter *, const uint8_t, 118 const struct sockaddr_in6 *); 119static void im6f_leave(struct in6_mfilter *); 120static int im6f_prune(struct in6_mfilter *, const struct sockaddr_in6 *); 121static void im6f_purge(struct in6_mfilter *); 122static void im6f_rollback(struct in6_mfilter *); 123static void im6f_reap(struct in6_mfilter *); 124static int im6o_grow(struct ip6_moptions *); 125static size_t im6o_match_group(const struct ip6_moptions *, 126 const struct ifnet *, const struct sockaddr *); 127static struct in6_msource * 128 im6o_match_source(const struct ip6_moptions *, const size_t, 129 const struct sockaddr *); 130static void im6s_merge(struct ip6_msource *ims, 131 const struct in6_msource *lims, const int rollback); 132static int in6_mc_get(struct ifnet *, const struct in6_addr *, 133 struct in6_multi **); 134static int in6m_get_source(struct in6_multi *inm, 135 const struct in6_addr *addr, const int noalloc, 136 struct ip6_msource **pims); 137static int in6m_is_ifp_detached(const struct in6_multi *); 138static int in6m_merge(struct in6_multi *, /*const*/ struct in6_mfilter *); 139static void in6m_purge(struct in6_multi *); 140static void in6m_reap(struct in6_multi *); 141static struct ip6_moptions * 142 in6p_findmoptions(struct inpcb *); 143static int in6p_get_source_filters(struct inpcb *, struct sockopt *); 144static int in6p_join_group(struct inpcb *, struct sockopt *); 145static int in6p_leave_group(struct inpcb *, struct sockopt *); 146static struct ifnet * 147 in6p_lookup_mcast_ifp(const struct inpcb *, 148 const struct sockaddr_in6 *); 149static int in6p_block_unblock_source(struct inpcb *, struct sockopt *); 150static int in6p_set_multicast_if(struct inpcb *, struct sockopt *); 151static int in6p_set_source_filters(struct inpcb *, struct sockopt *); 152static int sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS); 153 154SYSCTL_DECL(_net_inet6_ip6); /* XXX Not in any common header. */ 155 156SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, mcast, CTLFLAG_RW, 0, "IPv6 multicast"); 157 158static u_long in6_mcast_maxgrpsrc = IPV6_MAX_GROUP_SRC_FILTER; 159SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxgrpsrc, 160 CTLFLAG_RW | CTLFLAG_TUN, &in6_mcast_maxgrpsrc, 0, 161 "Max source filters per group"); 162TUNABLE_ULONG("net.inet6.ip6.mcast.maxgrpsrc", &in6_mcast_maxgrpsrc); 163 164static u_long in6_mcast_maxsocksrc = IPV6_MAX_SOCK_SRC_FILTER; 165SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxsocksrc, 166 CTLFLAG_RW | CTLFLAG_TUN, &in6_mcast_maxsocksrc, 0, 167 "Max source filters per socket"); 168TUNABLE_ULONG("net.inet6.ip6.mcast.maxsocksrc", &in6_mcast_maxsocksrc); 169 170/* TODO Virtualize this switch. */ 171int in6_mcast_loop = IPV6_DEFAULT_MULTICAST_LOOP; 172SYSCTL_INT(_net_inet6_ip6_mcast, OID_AUTO, loop, CTLFLAG_RW | CTLFLAG_TUN, 173 &in6_mcast_loop, 0, "Loopback multicast datagrams by default"); 174TUNABLE_INT("net.inet6.ip6.mcast.loop", &in6_mcast_loop); 175 176SYSCTL_NODE(_net_inet6_ip6_mcast, OID_AUTO, filters, 177 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip6_mcast_filters, 178 "Per-interface stack-wide source filters"); 179 180/* 181 * Inline function which wraps assertions for a valid ifp. 182 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp 183 * is detached. 184 */ 185static int __inline 186in6m_is_ifp_detached(const struct in6_multi *inm) 187{ 188 struct ifnet *ifp; 189 190 KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__)); 191 ifp = inm->in6m_ifma->ifma_ifp; 192 if (ifp != NULL) { 193 /* 194 * Sanity check that network-layer notion of ifp is the 195 * same as that of link-layer. 196 */ 197 KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__)); 198 } 199 200 return (ifp == NULL); 201} 202 203/* 204 * Initialize an in6_mfilter structure to a known state at t0, t1 205 * with an empty source filter list. 206 */ 207static __inline void 208im6f_init(struct in6_mfilter *imf, const int st0, const int st1) 209{ 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; 214} 215 216/* 217 * Resize the ip6_moptions vector to the next power-of-two minus 1. 218 * May be called with locks held; do not sleep. 219 */ 220static int 221im6o_grow(struct ip6_moptions *imo) 222{ 223 struct in6_multi **nmships; 224 struct in6_multi **omships; 225 struct in6_mfilter *nmfilters; 226 struct in6_mfilter *omfilters; 227 size_t idx; 228 size_t newmax; 229 size_t oldmax; 230 231 nmships = NULL; 232 nmfilters = NULL; 233 omships = imo->im6o_membership; 234 omfilters = imo->im6o_mfilters; 235 oldmax = imo->im6o_max_memberships; 236 newmax = ((oldmax + 1) * 2) - 1; 237 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, 243 M_NOWAIT); 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, 248 MCAST_EXCLUDE); 249 } 250 imo->im6o_max_memberships = newmax; 251 imo->im6o_membership = nmships; 252 imo->im6o_mfilters = nmfilters; 253 } 254 } 255 256 if (nmships == NULL || nmfilters == NULL) { 257 if (nmships != NULL) 258 free(nmships, M_IP6MOPTS); 259 if (nmfilters != NULL) 260 free(nmfilters, M_IN6MFILTER); 261 return (ETOOMANYREFS); 262 } 263 264 return (0); 265} 266 267/* 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. 271 */ 272static size_t 273im6o_match_group(const struct ip6_moptions *imo, const struct ifnet *ifp, 274 const struct sockaddr *group) 275{ 276 const struct sockaddr_in6 *gsin6; 277 struct in6_multi **pinm; 278 int idx; 279 int nmships; 280 281 gsin6 = (const struct sockaddr_in6 *)group; 282 283 /* The im6o_membership array may be lazy allocated. */ 284 if (imo->im6o_membership == NULL || imo->im6o_num_memberships == 0) 285 return (-1); 286 287 nmships = imo->im6o_num_memberships; 288 pinm = &imo->im6o_membership[0]; 289 for (idx = 0; idx < nmships; idx++, pinm++) { 290 if (*pinm == NULL) 291 continue; 292 if ((ifp == NULL || ((*pinm)->in6m_ifp == ifp)) && 293 IN6_ARE_ADDR_EQUAL(&(*pinm)->in6m_addr, 294 &gsin6->sin6_addr)) { 295 break; 296 } 297 } 298 if (idx >= nmships) 299 idx = -1; 300 301 return (idx); 302} 303 304/* 305 * Find an IPv6 multicast source entry for this imo which matches 306 * the given group index for this socket, and source address. 307 * 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. 311 * 312 * NOTE: This does not check if the entry is in-mode, merely if 313 * it exists, which may not be the desired behaviour. 314 */ 315static struct in6_msource * 316im6o_match_source(const struct ip6_moptions *imo, const size_t gidx, 317 const struct sockaddr *src) 318{ 319 struct ip6_msource find; 320 struct in6_mfilter *imf; 321 struct ip6_msource *ims; 322 const sockunion_t *psa; 323 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)); 327 328 /* The im6o_mfilters array may be lazy allocated. */ 329 if (imo->im6o_mfilters == NULL) 330 return (NULL); 331 imf = &imo->im6o_mfilters[gidx]; 332 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); 337 338 return ((struct in6_msource *)ims); 339} 340 341/* 342 * Perform filtering for multicast datagrams on a socket by group and source. 343 * 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. 346 */ 347int 348im6o_mc_filter(const struct ip6_moptions *imo, const struct ifnet *ifp, 349 const struct sockaddr *group, const struct sockaddr *src) 350{ 351 size_t gidx; 352 struct in6_msource *ims; 353 int mode; 354 355 KASSERT(ifp != NULL, ("%s: null ifp", __func__)); 356 357 gidx = im6o_match_group(imo, ifp, group); 358 if (gidx == -1) 359 return (MCAST_NOTGMEMBER); 360 361 /* 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). 369 */ 370 mode = imo->im6o_mfilters[gidx].im6f_st[1]; 371 ims = im6o_match_source(imo, gidx, src); 372 373 if ((ims == NULL && mode == MCAST_INCLUDE) || 374 (ims != NULL && ims->im6sl_st[0] != mode)) 375 return (MCAST_NOTSMEMBER); 376 377 return (MCAST_PASS); 378} 379 380/* 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. 387 */ 388static int 389in6_mc_get(struct ifnet *ifp, const struct in6_addr *group, 390 struct in6_multi **pinm) 391{ 392 struct sockaddr_in6 gsin6; 393 struct ifmultiaddr *ifma; 394 struct in6_multi *inm; 395 int error; 396 397 error = 0; 398 399 /* 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. 403 */ 404 IN6_MULTI_LOCK_ASSERT(); 405 IF_ADDR_LOCK(ifp); 406 407 inm = in6m_lookup_locked(ifp, group); 408 if (inm != NULL) { 409 /* 410 * If we already joined this group, just bump the 411 * refcount and return it. 412 */ 413 KASSERT(inm->in6m_refcount >= 1, 414 ("%s: bad refcount %d", __func__, inm->in6m_refcount)); 415 ++inm->in6m_refcount; 416 *pinm = inm; 417 goto out_locked; 418 } 419 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; 424 425 /* 426 * Check if a link-layer group is already associated 427 * with this network-layer group on the given ifnet. 428 */ 429 IF_ADDR_UNLOCK(ifp); 430 error = if_addmulti(ifp, (struct sockaddr *)&gsin6, &ifma); 431 if (error != 0) 432 return (error); 433 IF_ADDR_LOCK(ifp); 434 435 /* 436 * If something other than netinet6 is occupying the link-layer 437 * group, print a meaningful error message and back out of 438 * the allocation. 439 * Otherwise, bump the refcount on the existing network-layer 440 * group association and return it. 441 */ 442 if (ifma->ifma_protospec != NULL) { 443 inm = (struct in6_multi *)ifma->ifma_protospec; 444#ifdef INVARIANTS 445 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr", 446 __func__)); 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); 454#endif 455 ++inm->in6m_refcount; 456 *pinm = inm; 457 goto out_locked; 458 } 459 460 IF_ADDR_LOCK_ASSERT(ifp); 461 462 /* 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. 466 * 467 * The initial source filter state is INCLUDE, {} as per the RFC. 468 * Pending state-changes per group are subject to a bounds check. 469 */ 470 inm = malloc(sizeof(*inm), M_IP6MADDR, M_NOWAIT | M_ZERO); 471 if (inm == NULL) { 472 if_delmulti_ifma(ifma); 473 error = ENOMEM; 474 goto out_locked; 475 } 476 inm->in6m_addr = *group; 477 inm->in6m_ifp = ifp; 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 IFQ_SET_MAXLEN(&inm->in6m_scq, MLD_MAX_STATE_CHANGES); 483 484 inm->in6m_st[0].iss_fmode = MCAST_UNDEFINED; 485 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED; 486 RB_INIT(&inm->in6m_srcs); 487 488 ifma->ifma_protospec = inm; 489 *pinm = inm; 490 491out_locked: 492 IF_ADDR_UNLOCK(ifp); 493 return (error); 494} 495 496/* 497 * Drop a reference to an in6_multi record. 498 * 499 * If the refcount drops to 0, free the in6_multi record and 500 * delete the underlying link-layer membership. 501 */ 502void 503in6m_release_locked(struct in6_multi *inm) 504{ 505 struct ifmultiaddr *ifma; 506 507 IN6_MULTI_LOCK_ASSERT(); 508 509 CTR2(KTR_MLD, "%s: refcount is %d", __func__, inm->in6m_refcount); 510 511 if (--inm->in6m_refcount > 0) { 512 CTR2(KTR_MLD, "%s: refcount is now %d", __func__, 513 inm->in6m_refcount); 514 return; 515 } 516 517 CTR2(KTR_MLD, "%s: freeing inm %p", __func__, inm); 518 519 ifma = inm->in6m_ifma; 520 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; 526 527 in6m_purge(inm); 528 529 free(inm, M_IP6MADDR); 530 531 if_delmulti_ifma(ifma); 532} 533 534/* 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. 538 */ 539void 540in6m_clear_recorded(struct in6_multi *inm) 541{ 542 struct ip6_msource *ims; 543 544 IN6_MULTI_LOCK_ASSERT(); 545 546 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) { 547 if (ims->im6s_stp) { 548 ims->im6s_stp = 0; 549 --inm->in6m_st[1].iss_rec; 550 } 551 } 552 KASSERT(inm->in6m_st[1].iss_rec == 0, 553 ("%s: iss_rec %d not 0", __func__, inm->in6m_st[1].iss_rec)); 554} 555 556/* 557 * Record a source as pending for a Source-Group MLDv2 query. 558 * This lives here as it modifies the shared tree. 559 * 560 * inm is the group descriptor. 561 * naddr is the address of the source to record in network-byte order. 562 * 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. 569 * 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. 573 * 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). 577 */ 578int 579in6m_record_source(struct in6_multi *inm, const struct in6_addr *addr) 580{ 581 struct ip6_msource find; 582 struct ip6_msource *ims, *nims; 583 584 IN6_MULTI_LOCK_ASSERT(); 585 586 find.im6s_addr = *addr; 587 ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find); 588 if (ims && ims->im6s_stp) 589 return (0); 590 if (ims == NULL) { 591 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc) 592 return (-ENOSPC); 593 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE, 594 M_NOWAIT | M_ZERO); 595 if (nims == NULL) 596 return (-ENOMEM); 597 nims->im6s_addr = find.im6s_addr; 598 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims); 599 ++inm->in6m_nsrc; 600 ims = nims; 601 } 602 603 /* 604 * Mark the source as recorded and update the recorded 605 * source count. 606 */ 607 ++ims->im6s_stp; 608 ++inm->in6m_st[1].iss_rec; 609 610 return (1); 611} 612 613/* 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 617 * undefined at t0. 618 * 619 * imf is the filter set being modified. 620 * addr is the source address. 621 * 622 * SMPng: May be called with locks held; malloc must not block. 623 */ 624static int 625im6f_get_source(struct in6_mfilter *imf, const struct sockaddr_in6 *psin, 626 struct in6_msource **plims) 627{ 628 struct ip6_msource find; 629 struct ip6_msource *ims, *nims; 630 struct in6_msource *lims; 631 int error; 632 633 error = 0; 634 ims = NULL; 635 lims = NULL; 636 637 find.im6s_addr = psin->sin6_addr; 638 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find); 639 lims = (struct in6_msource *)ims; 640 if (lims == NULL) { 641 if (imf->im6f_nsrc == in6_mcast_maxsocksrc) 642 return (ENOSPC); 643 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER, 644 M_NOWAIT | M_ZERO); 645 if (nims == NULL) 646 return (ENOMEM); 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); 651 ++imf->im6f_nsrc; 652 } 653 654 *plims = lims; 655 656 return (error); 657} 658 659/* 660 * Graft a source entry into an existing socket-layer filter set, 661 * maintaining any required invariants and checking allocations. 662 * 663 * The source is marked as being in the new filter mode at t1. 664 * 665 * Return the pointer to the new node, otherwise return NULL. 666 */ 667static struct in6_msource * 668im6f_graft(struct in6_mfilter *imf, const uint8_t st1, 669 const struct sockaddr_in6 *psin) 670{ 671 struct ip6_msource *nims; 672 struct in6_msource *lims; 673 674 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER, 675 M_NOWAIT | M_ZERO); 676 if (nims == NULL) 677 return (NULL); 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); 683 ++imf->im6f_nsrc; 684 685 return (lims); 686} 687 688/* 689 * Prune a source entry from an existing socket-layer filter set, 690 * maintaining any required invariants and checking allocations. 691 * 692 * The source is marked as being left at t1, it is not freed. 693 * 694 * Return 0 if no error occurred, otherwise return an errno value. 695 */ 696static int 697im6f_prune(struct in6_mfilter *imf, const struct sockaddr_in6 *psin) 698{ 699 struct ip6_msource find; 700 struct ip6_msource *ims; 701 struct in6_msource *lims; 702 703 find.im6s_addr = psin->sin6_addr; 704 ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find); 705 if (ims == NULL) 706 return (ENOENT); 707 lims = (struct in6_msource *)ims; 708 lims->im6sl_st[1] = MCAST_UNDEFINED; 709 return (0); 710} 711 712/* 713 * Revert socket-layer filter set deltas at t1 to t0 state. 714 */ 715static void 716im6f_rollback(struct in6_mfilter *imf) 717{ 718 struct ip6_msource *ims, *tims; 719 struct in6_msource *lims; 720 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 */ 725 continue; 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]; 729 } else { 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); 734 imf->im6f_nsrc--; 735 } 736 } 737 imf->im6f_st[1] = imf->im6f_st[0]; 738} 739 740/* 741 * Mark socket-layer filter set as INCLUDE {} at t1. 742 */ 743static void 744im6f_leave(struct in6_mfilter *imf) 745{ 746 struct ip6_msource *ims; 747 struct in6_msource *lims; 748 749 RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) { 750 lims = (struct in6_msource *)ims; 751 lims->im6sl_st[1] = MCAST_UNDEFINED; 752 } 753 imf->im6f_st[1] = MCAST_INCLUDE; 754} 755 756/* 757 * Mark socket-layer filter set deltas as committed. 758 */ 759static void 760im6f_commit(struct in6_mfilter *imf) 761{ 762 struct ip6_msource *ims; 763 struct in6_msource *lims; 764 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]; 768 } 769 imf->im6f_st[0] = imf->im6f_st[1]; 770} 771 772/* 773 * Reap unreferenced sources from socket-layer filter set. 774 */ 775static void 776im6f_reap(struct in6_mfilter *imf) 777{ 778 struct ip6_msource *ims, *tims; 779 struct in6_msource *lims; 780 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); 788 imf->im6f_nsrc--; 789 } 790 } 791} 792 793/* 794 * Purge socket-layer filter set. 795 */ 796static void 797im6f_purge(struct in6_mfilter *imf) 798{ 799 struct ip6_msource *ims, *tims; 800 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); 805 imf->im6f_nsrc--; 806 } 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__)); 810} 811 812/* 813 * Look up a source filter entry for a multicast group. 814 * 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. 819 * 820 * SMPng: NOTE: may be called with locks held. 821 * Return 0 if successful, otherwise return a non-zero error code. 822 */ 823static int 824in6m_get_source(struct in6_multi *inm, const struct in6_addr *addr, 825 const int noalloc, struct ip6_msource **pims) 826{ 827 struct ip6_msource find; 828 struct ip6_msource *ims, *nims; 829#ifdef KTR 830 char ip6tbuf[INET6_ADDRSTRLEN]; 831#endif 832 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) 837 return (ENOSPC); 838 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE, 839 M_NOWAIT | M_ZERO); 840 if (nims == NULL) 841 return (ENOMEM); 842 nims->im6s_addr = *addr; 843 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims); 844 ++inm->in6m_nsrc; 845 ims = nims; 846 CTR3(KTR_MLD, "%s: allocated %s as %p", __func__, 847 ip6_sprintf(ip6tbuf, addr), ims); 848 } 849 850 *pims = ims; 851 return (0); 852} 853 854/* 855 * Merge socket-layer source into MLD-layer source. 856 * If rollback is non-zero, perform the inverse of the merge. 857 */ 858static void 859im6s_merge(struct ip6_msource *ims, const struct in6_msource *lims, 860 const int rollback) 861{ 862 int n = rollback ? -1 : 1; 863#ifdef KTR 864 char ip6tbuf[INET6_ADDRSTRLEN]; 865 866 ip6_sprintf(ip6tbuf, &lims->im6s_addr); 867#endif 868 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; 875 } 876 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; 883 } 884} 885 886/* 887 * Atomically update the global in6_multi state, when a membership's 888 * filter list is being updated in any way. 889 * 890 * imf is the per-inpcb-membership group filter pointer. 891 * A fake imf may be passed for in-kernel consumers. 892 * 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. 896 * 897 * If any error occurred this function will back out of refcounts 898 * and return a non-zero value. 899 */ 900static int 901in6m_merge(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf) 902{ 903 struct ip6_msource *ims, *nims; 904 struct in6_msource *lims; 905 int schanged, error; 906 int nsrc0, nsrc1; 907 908 schanged = 0; 909 error = 0; 910 nsrc1 = nsrc0 = 0; 911 912 /* 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. 918 */ 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); 925 ++schanged; 926 if (error) 927 break; 928 im6s_merge(nims, lims, 0); 929 } 930 if (error) { 931 struct ip6_msource *bims; 932 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]) 936 continue; 937 (void)in6m_get_source(inm, &lims->im6s_addr, 1, &bims); 938 if (bims == NULL) 939 continue; 940 im6s_merge(bims, lims, 1); 941 } 942 goto out_reap; 943 } 944 945 CTR3(KTR_MLD, "%s: imf filters in-mode: %d at t0, %d at t1", 946 __func__, nsrc0, nsrc1); 947 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) { 951 if (nsrc1 == 0) { 952 CTR1(KTR_MLD, "%s: --in on inm at t1", __func__); 953 --inm->in6m_st[1].iss_in; 954 } 955 } 956 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]); 961 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; 968 } 969 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++; 976 } 977 } 978 979 /* 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. 986 */ 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; 993 } else { 994 CTR1(KTR_MLD, "%s: transition to UNDEF", __func__); 995 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED; 996 } 997 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; 1004 } 1005 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++; 1010 } 1011 1012 CTR3(KTR_MLD, "%s: merged imf %p to inm %p", __func__, imf, inm); 1013 in6m_print(inm); 1014 1015out_reap: 1016 if (schanged > 0) { 1017 CTR1(KTR_MLD, "%s: sources changed; reaping", __func__); 1018 in6m_reap(inm); 1019 } 1020 return (error); 1021} 1022 1023/* 1024 * Mark an in6_multi's filter set deltas as committed. 1025 * Called by MLD after a state change has been enqueued. 1026 */ 1027void 1028in6m_commit(struct in6_multi *inm) 1029{ 1030 struct ip6_msource *ims; 1031 1032 CTR2(KTR_MLD, "%s: commit inm %p", __func__, inm); 1033 CTR1(KTR_MLD, "%s: pre commit:", __func__); 1034 in6m_print(inm); 1035 1036 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) { 1037 ims->im6s_st[0] = ims->im6s_st[1]; 1038 } 1039 inm->in6m_st[0] = inm->in6m_st[1]; 1040} 1041 1042/* 1043 * Reap unreferenced nodes from an in6_multi's filter set. 1044 */ 1045static void 1046in6m_reap(struct in6_multi *inm) 1047{ 1048 struct ip6_msource *ims, *tims; 1049 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 || 1053 ims->im6s_stp != 0) 1054 continue; 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); 1058 inm->in6m_nsrc--; 1059 } 1060} 1061 1062/* 1063 * Purge all source nodes from an in6_multi's filter set. 1064 */ 1065static void 1066in6m_purge(struct in6_multi *inm) 1067{ 1068 struct ip6_msource *ims, *tims; 1069 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); 1074 inm->in6m_nsrc--; 1075 } 1076} 1077 1078/* 1079 * Join a multicast address w/o sources. 1080 * KAME compatibility entry point. 1081 * 1082 * SMPng: Assume no mc locks held by caller. 1083 */ 1084struct in6_multi_mship * 1085in6_joingroup(struct ifnet *ifp, struct in6_addr *mcaddr, 1086 int *errorp, int delay) 1087{ 1088 struct in6_multi_mship *imm; 1089 int error; 1090 1091 imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT); 1092 if (imm == NULL) { 1093 *errorp = ENOBUFS; 1094 return (NULL); 1095 } 1096 1097 delay = (delay * PR_FASTHZ) / hz; 1098 1099 error = in6_mc_join(ifp, mcaddr, NULL, &imm->i6mm_maddr, delay); 1100 if (error) { 1101 *errorp = error; 1102 free(imm, M_IP6MADDR); 1103 return (NULL); 1104 } 1105 1106 return (imm); 1107} 1108 1109/* 1110 * Leave a multicast address w/o sources. 1111 * KAME compatibility entry point. 1112 * 1113 * SMPng: Assume no mc locks held by caller. 1114 */ 1115int 1116in6_leavegroup(struct in6_multi_mship *imm) 1117{ 1118 1119 if (imm->i6mm_maddr != NULL) 1120 in6_mc_leave(imm->i6mm_maddr, NULL); 1121 free(imm, M_IP6MADDR); 1122 return 0; 1123} 1124 1125/* 1126 * Join a multicast group; unlocked entry point. 1127 * 1128 * SMPng: XXX: in6_mc_join() is called from in6_control() when upper 1129 * locks are not held. Fortunately, ifp is unlikely to have been detached 1130 * at this point, so we assume it's OK to recurse. 1131 */ 1132int 1133in6_mc_join(struct ifnet *ifp, const struct in6_addr *mcaddr, 1134 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm, 1135 const int delay) 1136{ 1137 int error; 1138 1139 IN6_MULTI_LOCK(); 1140 error = in6_mc_join_locked(ifp, mcaddr, imf, pinm, delay); 1141 IN6_MULTI_UNLOCK(); 1142 1143 return (error); 1144} 1145 1146/* 1147 * Join a multicast group; real entry point. 1148 * 1149 * Only preserves atomicity at inm level. 1150 * NOTE: imf argument cannot be const due to sys/tree.h limitations. 1151 * 1152 * If the MLD downcall fails, the group is not joined, and an error 1153 * code is returned. 1154 */ 1155int 1156in6_mc_join_locked(struct ifnet *ifp, const struct in6_addr *mcaddr, 1157 /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm, 1158 const int delay) 1159{ 1160 struct in6_mfilter timf; 1161 struct in6_multi *inm; 1162 int error; 1163#ifdef KTR 1164 char ip6tbuf[INET6_ADDRSTRLEN]; 1165#endif 1166 1167#ifdef INVARIANTS 1168 /* 1169 * Sanity: Check scope zone ID was set for ifp, if and 1170 * only if group is scoped to an interface. 1171 */ 1172 KASSERT(IN6_IS_ADDR_MULTICAST(mcaddr), 1173 ("%s: not a multicast address", __func__)); 1174 if (IN6_IS_ADDR_MC_LINKLOCAL(mcaddr) || 1175 IN6_IS_ADDR_MC_INTFACELOCAL(mcaddr)) { 1176 KASSERT(mcaddr->s6_addr16[1] != 0, 1177 ("%s: scope zone ID not set", __func__)); 1178 } 1179#endif 1180 1181 IN6_MULTI_LOCK_ASSERT(); 1182 1183 CTR4(KTR_MLD, "%s: join %s on %p(%s))", __func__, 1184 ip6_sprintf(ip6tbuf, mcaddr), ifp, ifp->if_xname); 1185 1186 error = 0; 1187 inm = NULL; 1188 1189 /* 1190 * If no imf was specified (i.e. kernel consumer), 1191 * fake one up and assume it is an ASM join. 1192 */ 1193 if (imf == NULL) { 1194 im6f_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE); 1195 imf = &timf; 1196 } 1197 1198 error = in6_mc_get(ifp, mcaddr, &inm); 1199 if (error) { 1200 CTR1(KTR_MLD, "%s: in6_mc_get() failure", __func__); 1201 return (error); 1202 } 1203 1204 CTR1(KTR_MLD, "%s: merge inm state", __func__); 1205 error = in6m_merge(inm, imf); 1206 if (error) { 1207 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__); 1208 goto out_in6m_release; 1209 } 1210 1211 CTR1(KTR_MLD, "%s: doing mld downcall", __func__); 1212 error = mld_change_state(inm, delay); 1213 if (error) { 1214 CTR1(KTR_MLD, "%s: failed to update source", __func__); 1215 goto out_in6m_release; 1216 } 1217 1218out_in6m_release: 1219 if (error) { 1220 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm); 1221 in6m_release_locked(inm); 1222 } else { 1223 *pinm = inm; 1224 } 1225 1226 return (error); 1227} 1228 1229/* 1230 * Leave a multicast group; unlocked entry point. 1231 */ 1232int 1233in6_mc_leave(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf) 1234{ 1235 struct ifnet *ifp; 1236 int error; 1237 1238 ifp = inm->in6m_ifp; 1239 1240 IN6_MULTI_LOCK(); 1241 error = in6_mc_leave_locked(inm, imf); 1242 IN6_MULTI_UNLOCK(); 1243 1244 return (error); 1245} 1246 1247/* 1248 * Leave a multicast group; real entry point. 1249 * All source filters will be expunged. 1250 * 1251 * Only preserves atomicity at inm level. 1252 * 1253 * Holding the write lock for the INP which contains imf 1254 * is highly advisable. We can't assert for it as imf does not 1255 * contain a back-pointer to the owning inp. 1256 * 1257 * Note: This is not the same as in6m_release(*) as this function also 1258 * makes a state change downcall into MLD. 1259 */ 1260int 1261in6_mc_leave_locked(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf) 1262{ 1263 struct in6_mfilter timf; 1264 int error; 1265#ifdef KTR 1266 char ip6tbuf[INET6_ADDRSTRLEN]; 1267#endif 1268 1269 error = 0; 1270 1271 IN6_MULTI_LOCK_ASSERT(); 1272 1273 CTR5(KTR_MLD, "%s: leave inm %p, %s/%s, imf %p", __func__, 1274 inm, ip6_sprintf(ip6tbuf, &inm->in6m_addr), 1275 (in6m_is_ifp_detached(inm) ? "null" : inm->in6m_ifp->if_xname), 1276 imf); 1277 1278 /* 1279 * If no imf was specified (i.e. kernel consumer), 1280 * fake one up and assume it is an ASM join. 1281 */ 1282 if (imf == NULL) { 1283 im6f_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED); 1284 imf = &timf; 1285 } 1286 1287 /* 1288 * Begin state merge transaction at MLD layer. 1289 * 1290 * As this particular invocation should not cause any memory 1291 * to be allocated, and there is no opportunity to roll back 1292 * the transaction, it MUST NOT fail. 1293 */ 1294 CTR1(KTR_MLD, "%s: merge inm state", __func__); 1295 error = in6m_merge(inm, imf); 1296 KASSERT(error == 0, ("%s: failed to merge inm state", __func__)); 1297 1298 CTR1(KTR_MLD, "%s: doing mld downcall", __func__); 1299 error = mld_change_state(inm, 0); 1300 if (error) 1301 CTR1(KTR_MLD, "%s: failed mld downcall", __func__); 1302 1303 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm); 1304 in6m_release_locked(inm); 1305 1306 return (error); 1307} 1308 1309/* 1310 * Block or unblock an ASM multicast source on an inpcb. 1311 * This implements the delta-based API described in RFC 3678. 1312 * 1313 * The delta-based API applies only to exclusive-mode memberships. 1314 * An MLD downcall will be performed. 1315 * 1316 * SMPng: NOTE: Must take Giant as a join may create a new ifma. 1317 * 1318 * Return 0 if successful, otherwise return an appropriate error code. 1319 */ 1320static int 1321in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt) 1322{ 1323 INIT_VNET_NET(curvnet); 1324 struct group_source_req gsr; 1325 sockunion_t *gsa, *ssa; 1326 struct ifnet *ifp; 1327 struct in6_mfilter *imf; 1328 struct ip6_moptions *imo; 1329 struct in6_msource *ims; 1330 struct in6_multi *inm; 1331 size_t idx; 1332 uint16_t fmode; 1333 int error, doblock; 1334#ifdef KTR 1335 char ip6tbuf[INET6_ADDRSTRLEN]; 1336#endif 1337 1338 ifp = NULL; 1339 error = 0; 1340 doblock = 0; 1341 1342 memset(&gsr, 0, sizeof(struct group_source_req)); 1343 gsa = (sockunion_t *)&gsr.gsr_group; 1344 ssa = (sockunion_t *)&gsr.gsr_source; 1345 1346 switch (sopt->sopt_name) { 1347 case MCAST_BLOCK_SOURCE: 1348 case MCAST_UNBLOCK_SOURCE: 1349 error = sooptcopyin(sopt, &gsr, 1350 sizeof(struct group_source_req), 1351 sizeof(struct group_source_req)); 1352 if (error) 1353 return (error); 1354 1355 if (gsa->sin6.sin6_family != AF_INET6 || 1356 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6)) 1357 return (EINVAL); 1358 1359 if (ssa->sin6.sin6_family != AF_INET6 || 1360 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6)) 1361 return (EINVAL); 1362 1363 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface) 1364 return (EADDRNOTAVAIL); 1365 1366 ifp = ifnet_byindex(gsr.gsr_interface); 1367 1368 if (sopt->sopt_name == MCAST_BLOCK_SOURCE) 1369 doblock = 1; 1370 break; 1371 1372 default: 1373 CTR2(KTR_MLD, "%s: unknown sopt_name %d", 1374 __func__, sopt->sopt_name); 1375 return (EOPNOTSUPP); 1376 break; 1377 } 1378 1379 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr)) 1380 return (EINVAL); 1381 1382 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL); 1383 1384 /* 1385 * Check if we are actually a member of this group. 1386 */ 1387 imo = in6p_findmoptions(inp); 1388 idx = im6o_match_group(imo, ifp, &gsa->sa); 1389 if (idx == -1 || imo->im6o_mfilters == NULL) { 1390 error = EADDRNOTAVAIL; 1391 goto out_in6p_locked; 1392 } 1393 1394 KASSERT(imo->im6o_mfilters != NULL, 1395 ("%s: im6o_mfilters not allocated", __func__)); 1396 imf = &imo->im6o_mfilters[idx]; 1397 inm = imo->im6o_membership[idx]; 1398 1399 /* 1400 * Attempting to use the delta-based API on an 1401 * non exclusive-mode membership is an error. 1402 */ 1403 fmode = imf->im6f_st[0]; 1404 if (fmode != MCAST_EXCLUDE) { 1405 error = EINVAL; 1406 goto out_in6p_locked; 1407 } 1408 1409 /* 1410 * Deal with error cases up-front: 1411 * Asked to block, but already blocked; or 1412 * Asked to unblock, but nothing to unblock. 1413 * If adding a new block entry, allocate it. 1414 */ 1415 ims = im6o_match_source(imo, idx, &ssa->sa); 1416 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) { 1417 CTR3(KTR_MLD, "%s: source %s %spresent", __func__, 1418 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr), 1419 doblock ? "" : "not "); 1420 error = EADDRNOTAVAIL; 1421 goto out_in6p_locked; 1422 } 1423 1424 INP_WLOCK_ASSERT(inp); 1425 1426 /* 1427 * Begin state merge transaction at socket layer. 1428 */ 1429 if (doblock) { 1430 CTR2(KTR_MLD, "%s: %s source", __func__, "block"); 1431 ims = im6f_graft(imf, fmode, &ssa->sin6); 1432 if (ims == NULL) 1433 error = ENOMEM; 1434 } else { 1435 CTR2(KTR_MLD, "%s: %s source", __func__, "allow"); 1436 error = im6f_prune(imf, &ssa->sin6); 1437 } 1438 1439 if (error) { 1440 CTR1(KTR_MLD, "%s: merge imf state failed", __func__); 1441 goto out_im6f_rollback; 1442 } 1443 1444 /* 1445 * Begin state merge transaction at MLD layer. 1446 */ 1447 IN6_MULTI_LOCK(); 1448 1449 CTR1(KTR_MLD, "%s: merge inm state", __func__); 1450 error = in6m_merge(inm, imf); 1451 if (error) { 1452 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__); 1453 goto out_im6f_rollback; 1454 } 1455 1456 CTR1(KTR_MLD, "%s: doing mld downcall", __func__); 1457 error = mld_change_state(inm, 0); 1458 if (error) 1459 CTR1(KTR_MLD, "%s: failed mld downcall", __func__); 1460 1461 IN6_MULTI_UNLOCK(); 1462 1463out_im6f_rollback: 1464 if (error) 1465 im6f_rollback(imf); 1466 else 1467 im6f_commit(imf); 1468 1469 im6f_reap(imf); 1470 1471out_in6p_locked: 1472 INP_WUNLOCK(inp); 1473 return (error); 1474} 1475 1476/* 1477 * Given an inpcb, return its multicast options structure pointer. Accepts 1478 * an unlocked inpcb pointer, but will return it locked. May sleep. 1479 * 1480 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held. 1481 * SMPng: NOTE: Returns with the INP write lock held. 1482 */ 1483static struct ip6_moptions * 1484in6p_findmoptions(struct inpcb *inp) 1485{ 1486 INIT_VNET_INET6(curvnet); 1487 struct ip6_moptions *imo; 1488 struct in6_multi **immp; 1489 struct in6_mfilter *imfp; 1490 size_t idx; 1491 1492 INP_WLOCK(inp); 1493 if (inp->in6p_moptions != NULL) 1494 return (inp->in6p_moptions); 1495 1496 INP_WUNLOCK(inp); 1497 1498 imo = malloc(sizeof(*imo), M_IP6MOPTS, M_WAITOK); 1499 immp = malloc(sizeof(*immp) * IPV6_MIN_MEMBERSHIPS, M_IP6MOPTS, 1500 M_WAITOK | M_ZERO); 1501 imfp = malloc(sizeof(struct in6_mfilter) * IPV6_MIN_MEMBERSHIPS, 1502 M_IN6MFILTER, M_WAITOK); 1503 1504 imo->im6o_multicast_ifp = NULL; 1505 imo->im6o_multicast_hlim = V_ip6_defmcasthlim; 1506 imo->im6o_multicast_loop = in6_mcast_loop; 1507 imo->im6o_num_memberships = 0; 1508 imo->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS; 1509 imo->im6o_membership = immp; 1510 1511 /* Initialize per-group source filters. */ 1512 for (idx = 0; idx < IPV6_MIN_MEMBERSHIPS; idx++) 1513 im6f_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE); 1514 imo->im6o_mfilters = imfp; 1515 1516 INP_WLOCK(inp); 1517 if (inp->in6p_moptions != NULL) { 1518 free(imfp, M_IN6MFILTER); 1519 free(immp, M_IP6MOPTS); 1520 free(imo, M_IP6MOPTS); 1521 return (inp->in6p_moptions); 1522 } 1523 inp->in6p_moptions = imo; 1524 return (imo); 1525} 1526 1527/* 1528 * Discard the IPv6 multicast options (and source filters). 1529 * 1530 * SMPng: NOTE: assumes INP write lock is held. 1531 */ 1532void 1533ip6_freemoptions(struct ip6_moptions *imo) 1534{ 1535 struct in6_mfilter *imf; 1536 size_t idx, nmships; 1537 1538 KASSERT(imo != NULL, ("%s: ip6_moptions is NULL", __func__)); 1539 1540 nmships = imo->im6o_num_memberships; 1541 for (idx = 0; idx < nmships; ++idx) { 1542 imf = imo->im6o_mfilters ? &imo->im6o_mfilters[idx] : NULL; 1543 if (imf) 1544 im6f_leave(imf); 1545 /* XXX this will thrash the lock(s) */ 1546 (void)in6_mc_leave(imo->im6o_membership[idx], imf); 1547 if (imf) 1548 im6f_purge(imf); 1549 } 1550 1551 if (imo->im6o_mfilters) 1552 free(imo->im6o_mfilters, M_IN6MFILTER); 1553 free(imo->im6o_membership, M_IP6MOPTS); 1554 free(imo, M_IP6MOPTS); 1555} 1556 1557/* 1558 * Atomically get source filters on a socket for an IPv6 multicast group. 1559 * Called with INP lock held; returns with lock released. 1560 */ 1561static int 1562in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt) 1563{ 1564 INIT_VNET_NET(curvnet); 1565 struct __msfilterreq msfr; 1566 sockunion_t *gsa; 1567 struct ifnet *ifp; 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; 1575 int error; 1576 size_t idx, nsrcs, ncsrcs; 1577 1578 INP_WLOCK_ASSERT(inp); 1579 1580 imo = inp->in6p_moptions; 1581 KASSERT(imo != NULL, ("%s: null ip6_moptions", __func__)); 1582 1583 INP_WUNLOCK(inp); 1584 1585 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq), 1586 sizeof(struct __msfilterreq)); 1587 if (error) 1588 return (error); 1589 1590 if (msfr.msfr_group.ss_family != AF_INET6 || 1591 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6)) 1592 return (EINVAL); 1593 1594 gsa = (sockunion_t *)&msfr.msfr_group; 1595 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr)) 1596 return (EINVAL); 1597 1598 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex) 1599 return (EADDRNOTAVAIL); 1600 ifp = ifnet_byindex(msfr.msfr_ifindex); 1601 if (ifp == NULL) 1602 return (EADDRNOTAVAIL); 1603 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL); 1604 1605 INP_WLOCK(inp); 1606 1607 /* 1608 * Lookup group on the socket. 1609 */ 1610 idx = im6o_match_group(imo, ifp, &gsa->sa); 1611 if (idx == -1 || imo->im6o_mfilters == NULL) { 1612 INP_WUNLOCK(inp); 1613 return (EADDRNOTAVAIL); 1614 } 1615 imf = &imo->im6o_mfilters[idx]; 1616 1617 /* 1618 * Ignore memberships which are in limbo. 1619 */ 1620 if (imf->im6f_st[1] == MCAST_UNDEFINED) { 1621 INP_WUNLOCK(inp); 1622 return (EAGAIN); 1623 } 1624 msfr.msfr_fmode = imf->im6f_st[1]; 1625 1626 /* 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. 1632 */ 1633 tss = NULL; 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); 1637 if (tss == NULL) { 1638 INP_WUNLOCK(inp); 1639 return (ENOBUFS); 1640 } 1641 } 1642 1643 /* 1644 * Count number of sources in-mode at t0. 1645 * If buffer space exists and remains, copy out source entries. 1646 */ 1647 nsrcs = msfr.msfr_nsrcs; 1648 ncsrcs = 0; 1649 ptss = tss; 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]) 1654 continue; 1655 ++ncsrcs; 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; 1662 --nsrcs; 1663 ++ptss; 1664 } 1665 } 1666 1667 INP_WUNLOCK(inp); 1668 1669 if (tss != NULL) { 1670 error = copyout(tss, msfr.msfr_srcs, 1671 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs); 1672 free(tss, M_TEMP); 1673 if (error) 1674 return (error); 1675 } 1676 1677 msfr.msfr_nsrcs = ncsrcs; 1678 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq)); 1679 1680 return (error); 1681} 1682 1683/* 1684 * Return the IP multicast options in response to user getsockopt(). 1685 */ 1686int 1687ip6_getmoptions(struct inpcb *inp, struct sockopt *sopt) 1688{ 1689 INIT_VNET_INET6(curvnet); 1690 struct ip6_moptions *im6o; 1691 int error; 1692 u_int optval; 1693 1694 INP_WLOCK(inp); 1695 im6o = inp->in6p_moptions; 1696 /* 1697 * If socket is neither of type SOCK_RAW or SOCK_DGRAM, 1698 * or is a divert socket, reject it. 1699 */ 1700 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT || 1701 (inp->inp_socket->so_proto->pr_type != SOCK_RAW && 1702 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) { 1703 INP_WUNLOCK(inp); 1704 return (EOPNOTSUPP); 1705 } 1706 1707 error = 0; 1708 switch (sopt->sopt_name) { 1709 case IPV6_MULTICAST_IF: 1710 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) { 1711 optval = 0; 1712 } else { 1713 optval = im6o->im6o_multicast_ifp->if_index; 1714 } 1715 INP_WUNLOCK(inp); 1716 error = sooptcopyout(sopt, &optval, sizeof(u_int)); 1717 break; 1718 1719 case IPV6_MULTICAST_HOPS: 1720 if (im6o == NULL) 1721 optval = V_ip6_defmcasthlim; 1722 else 1723 optval = im6o->im6o_multicast_loop; 1724 INP_WUNLOCK(inp); 1725 error = sooptcopyout(sopt, &optval, sizeof(u_int)); 1726 break; 1727 1728 case IPV6_MULTICAST_LOOP: 1729 if (im6o == NULL) 1730 optval = in6_mcast_loop; /* XXX VIMAGE */ 1731 else 1732 optval = im6o->im6o_multicast_loop; 1733 INP_WUNLOCK(inp); 1734 error = sooptcopyout(sopt, &optval, sizeof(u_int)); 1735 break; 1736 1737 case IPV6_MSFILTER: 1738 if (im6o == NULL) { 1739 error = EADDRNOTAVAIL; 1740 INP_WUNLOCK(inp); 1741 } else { 1742 error = in6p_get_source_filters(inp, sopt); 1743 } 1744 break; 1745 1746 default: 1747 INP_WUNLOCK(inp); 1748 error = ENOPROTOOPT; 1749 break; 1750 } 1751 1752 INP_UNLOCK_ASSERT(inp); 1753 1754 return (error); 1755} 1756 1757/* 1758 * Look up the ifnet to use for a multicast group membership, 1759 * given the address of an IPv6 group. 1760 * 1761 * This routine exists to support legacy IPv6 multicast applications. 1762 * 1763 * If inp is non-NULL, use this socket's current FIB number for any 1764 * required FIB lookup. Look up the group address in the unicast FIB, 1765 * and use its ifp; usually, this points to the default next-hop. 1766 * If the FIB lookup fails, return NULL. 1767 * 1768 * FUTURE: Support multiple forwarding tables for IPv6. 1769 * 1770 * Returns NULL if no ifp could be found. 1771 */ 1772static struct ifnet * 1773in6p_lookup_mcast_ifp(const struct inpcb *in6p __unused, 1774 const struct sockaddr_in6 *gsin6) 1775{ 1776 struct route_in6 ro6; 1777 struct ifnet *ifp; 1778 1779 KASSERT(in6p->inp_vflag & INP_IPV6, 1780 ("%s: not INP_IPV6 inpcb", __func__)); 1781 KASSERT(gsin6->sin6_family == AF_INET6, 1782 ("%s: not AF_INET6 group", __func__)); 1783 KASSERT(IN6_IS_ADDR_MULTICAST(&gsin6->sin6_addr), 1784 ("%s: not multicast", __func__)); 1785 1786 ifp = NULL; 1787 memset(&ro6, 0, sizeof(struct route_in6)); 1788 memcpy(&ro6.ro_dst, gsin6, sizeof(struct sockaddr_in6)); 1789#ifdef notyet 1790 rtalloc_ign_fib(&ro6, 0, inp ? inp->inp_inc.inc_fibnum : 0); 1791#else 1792 rtalloc_ign((struct route *)&ro6, 0); 1793#endif 1794 if (ro6.ro_rt != NULL) { 1795 ifp = ro6.ro_rt->rt_ifp; 1796 KASSERT(ifp != NULL, ("%s: null ifp", __func__)); 1797 RTFREE(ro6.ro_rt); 1798 } 1799 1800 return (ifp); 1801} 1802 1803/* 1804 * Join an IPv6 multicast group, possibly with a source. 1805 * 1806 * FIXME: The KAME use of the unspecified address (::) 1807 * to join *all* multicast groups is currently unsupported. 1808 */ 1809static int 1810in6p_join_group(struct inpcb *inp, struct sockopt *sopt) 1811{ 1812 INIT_VNET_NET(curvnet); 1813 struct group_source_req gsr; 1814 sockunion_t *gsa, *ssa; 1815 struct ifnet *ifp; 1816 struct in6_mfilter *imf; 1817 struct ip6_moptions *imo; 1818 struct in6_multi *inm; 1819 struct in6_msource *lims; 1820 size_t idx; 1821 int error, is_new; 1822 1823 ifp = NULL; 1824 imf = NULL; 1825 error = 0; 1826 is_new = 0; 1827 1828 memset(&gsr, 0, sizeof(struct group_source_req)); 1829 gsa = (sockunion_t *)&gsr.gsr_group; 1830 gsa->ss.ss_family = AF_UNSPEC; 1831 ssa = (sockunion_t *)&gsr.gsr_source; 1832 ssa->ss.ss_family = AF_UNSPEC; 1833 1834 /* 1835 * Chew everything into struct group_source_req. 1836 * Overwrite the port field if present, as the sockaddr 1837 * being copied in may be matched with a binary comparison. 1838 * Ignore passed-in scope ID. 1839 */ 1840 switch (sopt->sopt_name) { 1841 case IPV6_JOIN_GROUP: { 1842 struct ipv6_mreq mreq; 1843 1844 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq), 1845 sizeof(struct ipv6_mreq)); 1846 if (error) 1847 return (error); 1848 1849 gsa->sin6.sin6_family = AF_INET6; 1850 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6); 1851 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr; 1852 1853 if (mreq.ipv6mr_interface == 0) { 1854 ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6); 1855 } else { 1856 if (mreq.ipv6mr_interface < 0 || 1857 V_if_index < mreq.ipv6mr_interface) 1858 return (EADDRNOTAVAIL); 1859 ifp = ifnet_byindex(mreq.ipv6mr_interface); 1860 } 1861 CTR3(KTR_MLD, "%s: ipv6mr_interface = %d, ifp = %p", 1862 __func__, mreq.ipv6mr_interface, ifp); 1863 } break; 1864 1865 case MCAST_JOIN_GROUP: 1866 case MCAST_JOIN_SOURCE_GROUP: 1867 if (sopt->sopt_name == MCAST_JOIN_GROUP) { 1868 error = sooptcopyin(sopt, &gsr, 1869 sizeof(struct group_req), 1870 sizeof(struct group_req)); 1871 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) { 1872 error = sooptcopyin(sopt, &gsr, 1873 sizeof(struct group_source_req), 1874 sizeof(struct group_source_req)); 1875 } 1876 if (error) 1877 return (error); 1878 1879 if (gsa->sin6.sin6_family != AF_INET6 || 1880 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6)) 1881 return (EINVAL); 1882 1883 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) { 1884 if (ssa->sin6.sin6_family != AF_INET6 || 1885 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6)) 1886 return (EINVAL); 1887 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr)) 1888 return (EINVAL); 1889 /* 1890 * TODO: Validate embedded scope ID in source 1891 * list entry against passed-in ifp, if and only 1892 * if source list filter entry is iface or node local. 1893 */ 1894 in6_clearscope(&ssa->sin6.sin6_addr); 1895 ssa->sin6.sin6_port = 0; 1896 ssa->sin6.sin6_scope_id = 0; 1897 } 1898 1899 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface) 1900 return (EADDRNOTAVAIL); 1901 ifp = ifnet_byindex(gsr.gsr_interface); 1902 break; 1903 1904 default: 1905 CTR2(KTR_MLD, "%s: unknown sopt_name %d", 1906 __func__, sopt->sopt_name); 1907 return (EOPNOTSUPP); 1908 break; 1909 } 1910 1911 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr)) 1912 return (EINVAL); 1913 1914 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) 1915 return (EADDRNOTAVAIL); 1916 1917 gsa->sin6.sin6_port = 0; 1918 gsa->sin6.sin6_scope_id = 0; 1919 1920 /* 1921 * Always set the scope zone ID on memberships created from userland. 1922 * Use the passed-in ifp to do this. 1923 * XXX The in6_setscope() return value is meaningless. 1924 * XXX SCOPE6_LOCK() is taken by in6_setscope(). 1925 */ 1926 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL); 1927 1928 /* 1929 * MCAST_JOIN_SOURCE on an exclusive membership is an error. 1930 * On an existing inclusive membership, it just adds the 1931 * source to the filter list. 1932 */ 1933 imo = in6p_findmoptions(inp); 1934 idx = im6o_match_group(imo, ifp, &gsa->sa); 1935 if (idx == -1) { 1936 is_new = 1; 1937 } else { 1938 inm = imo->im6o_membership[idx]; 1939 imf = &imo->im6o_mfilters[idx]; 1940 if (ssa->ss.ss_family != AF_UNSPEC && 1941 imf->im6f_st[1] != MCAST_INCLUDE) { 1942 error = EINVAL; 1943 goto out_in6p_locked; 1944 } 1945 lims = im6o_match_source(imo, idx, &ssa->sa); 1946 if (lims != NULL) { 1947 error = EADDRNOTAVAIL; 1948 goto out_in6p_locked; 1949 } 1950 } 1951 1952 /* 1953 * Begin state merge transaction at socket layer. 1954 */ 1955 INP_WLOCK_ASSERT(inp); 1956 1957 if (is_new) { 1958 if (imo->im6o_num_memberships == imo->im6o_max_memberships) { 1959 error = im6o_grow(imo); 1960 if (error) 1961 goto out_in6p_locked; 1962 } 1963 /* 1964 * Allocate the new slot upfront so we can deal with 1965 * grafting the new source filter in same code path 1966 * as for join-source on existing membership. 1967 */ 1968 idx = imo->im6o_num_memberships; 1969 imo->im6o_membership[idx] = NULL; 1970 imo->im6o_num_memberships++; 1971 KASSERT(imo->im6o_mfilters != NULL, 1972 ("%s: im6f_mfilters vector was not allocated", __func__)); 1973 imf = &imo->im6o_mfilters[idx]; 1974 KASSERT(RB_EMPTY(&imf->im6f_sources), 1975 ("%s: im6f_sources not empty", __func__)); 1976 } 1977 1978 /* 1979 * Graft new source into filter list for this inpcb's 1980 * membership of the group. The in6_multi may not have 1981 * been allocated yet if this is a new membership. 1982 */ 1983 if (ssa->ss.ss_family != AF_UNSPEC) { 1984 /* Membership starts in IN mode */ 1985 if (is_new) { 1986 CTR1(KTR_MLD, "%s: new join w/source", __func__); 1987 im6f_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE); 1988 } else { 1989 CTR2(KTR_MLD, "%s: %s source", __func__, "allow"); 1990 } 1991 lims = im6f_graft(imf, MCAST_INCLUDE, &ssa->sin6); 1992 if (lims == NULL) { 1993 CTR1(KTR_MLD, "%s: merge imf state failed", 1994 __func__); 1995 error = ENOMEM; 1996 goto out_im6o_free; 1997 } 1998 } 1999 2000 /* 2001 * Begin state merge transaction at MLD layer. 2002 */ 2003 IN6_MULTI_LOCK(); 2004 2005 if (is_new) { 2006 error = in6_mc_join_locked(ifp, &gsa->sin6.sin6_addr, imf, 2007 &inm, 0); 2008 if (error) 2009 goto out_im6o_free; 2010 imo->im6o_membership[idx] = inm; 2011 } else { 2012 CTR1(KTR_MLD, "%s: merge inm state", __func__); 2013 error = in6m_merge(inm, imf); 2014 if (error) { 2015 CTR1(KTR_MLD, "%s: failed to merge inm state", 2016 __func__); 2017 goto out_im6f_rollback; 2018 } 2019 CTR1(KTR_MLD, "%s: doing mld downcall", __func__); 2020 error = mld_change_state(inm, 0); 2021 if (error) { 2022 CTR1(KTR_MLD, "%s: failed mld downcall", 2023 __func__); 2024 goto out_im6f_rollback; 2025 } 2026 } 2027 2028 IN6_MULTI_UNLOCK(); 2029 2030out_im6f_rollback: 2031 INP_WLOCK_ASSERT(inp); 2032 if (error) { 2033 im6f_rollback(imf); 2034 if (is_new) 2035 im6f_purge(imf); 2036 else 2037 im6f_reap(imf); 2038 } else { 2039 im6f_commit(imf); 2040 } 2041 2042out_im6o_free: 2043 if (error && is_new) { 2044 imo->im6o_membership[idx] = NULL; 2045 --imo->im6o_num_memberships; 2046 } 2047 2048out_in6p_locked: 2049 INP_WUNLOCK(inp); 2050 return (error); 2051} 2052 2053/* 2054 * Leave an IPv6 multicast group on an inpcb, possibly with a source. 2055 */ 2056static int 2057in6p_leave_group(struct inpcb *inp, struct sockopt *sopt) 2058{ 2059 INIT_VNET_NET(curvnet); 2060 INIT_VNET_INET6(curvnet); 2061 struct ipv6_mreq mreq; 2062 struct group_source_req gsr; 2063 sockunion_t *gsa, *ssa; 2064 struct ifnet *ifp; 2065 struct in6_mfilter *imf; 2066 struct ip6_moptions *imo; 2067 struct in6_msource *ims; 2068 struct in6_multi *inm; 2069 uint32_t ifindex; 2070 size_t idx; 2071 int error, is_final; 2072#ifdef KTR 2073 char ip6tbuf[INET6_ADDRSTRLEN]; 2074#endif 2075 2076 ifp = NULL; 2077 ifindex = 0; 2078 error = 0; 2079 is_final = 1; 2080 2081 memset(&gsr, 0, sizeof(struct group_source_req)); 2082 gsa = (sockunion_t *)&gsr.gsr_group; 2083 gsa->ss.ss_family = AF_UNSPEC; 2084 ssa = (sockunion_t *)&gsr.gsr_source; 2085 ssa->ss.ss_family = AF_UNSPEC; 2086 2087 /* 2088 * Chew everything passed in up into a struct group_source_req 2089 * as that is easier to process. 2090 * Note: Any embedded scope ID in the multicast group passed 2091 * in by userland is ignored, the interface index is the recommended 2092 * mechanism to specify an interface; see below. 2093 */ 2094 switch (sopt->sopt_name) { 2095 case IPV6_LEAVE_GROUP: 2096 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq), 2097 sizeof(struct ipv6_mreq)); 2098 if (error) 2099 return (error); 2100 gsa->sin6.sin6_family = AF_INET6; 2101 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6); 2102 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr; 2103 gsa->sin6.sin6_port = 0; 2104 gsa->sin6.sin6_scope_id = 0; 2105 ifindex = mreq.ipv6mr_interface; 2106 break; 2107 2108 case MCAST_LEAVE_GROUP: 2109 case MCAST_LEAVE_SOURCE_GROUP: 2110 if (sopt->sopt_name == MCAST_LEAVE_GROUP) { 2111 error = sooptcopyin(sopt, &gsr, 2112 sizeof(struct group_req), 2113 sizeof(struct group_req)); 2114 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) { 2115 error = sooptcopyin(sopt, &gsr, 2116 sizeof(struct group_source_req), 2117 sizeof(struct group_source_req)); 2118 } 2119 if (error) 2120 return (error); 2121 2122 if (gsa->sin6.sin6_family != AF_INET6 || 2123 gsa->sin6.sin6_len != sizeof(struct sockaddr_in6)) 2124 return (EINVAL); 2125 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) { 2126 if (ssa->sin6.sin6_family != AF_INET6 || 2127 ssa->sin6.sin6_len != sizeof(struct sockaddr_in6)) 2128 return (EINVAL); 2129 if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr)) 2130 return (EINVAL); 2131 /* 2132 * TODO: Validate embedded scope ID in source 2133 * list entry against passed-in ifp, if and only 2134 * if source list filter entry is iface or node local. 2135 */ 2136 in6_clearscope(&ssa->sin6.sin6_addr); 2137 } 2138 gsa->sin6.sin6_port = 0; 2139 gsa->sin6.sin6_scope_id = 0; 2140 ifindex = gsr.gsr_interface; 2141 break; 2142 2143 default: 2144 CTR2(KTR_MLD, "%s: unknown sopt_name %d", 2145 __func__, sopt->sopt_name); 2146 return (EOPNOTSUPP); 2147 break; 2148 } 2149 2150 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr)) 2151 return (EINVAL); 2152 2153 /* 2154 * Validate interface index if provided. If no interface index 2155 * was provided separately, attempt to look the membership up 2156 * from the default scope as a last resort to disambiguate 2157 * the membership we are being asked to leave. 2158 * XXX SCOPE6 lock potentially taken here. 2159 */ 2160 if (ifindex != 0) { 2161 if (ifindex < 0 || V_if_index < ifindex) 2162 return (EADDRNOTAVAIL); 2163 ifp = ifnet_byindex(ifindex); 2164 if (ifp == NULL) 2165 return (EADDRNOTAVAIL); 2166 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL); 2167 } else { 2168 error = sa6_embedscope(&gsa->sin6, V_ip6_use_defzone); 2169 if (error) 2170 return (EADDRNOTAVAIL); 2171 /* 2172 * XXX For now, stomp on zone ID for the corner case. 2173 * This is not the 'KAME way', but we need to see the ifp 2174 * directly until such time as this implementation is 2175 * refactored, assuming the scope IDs are the way to go. 2176 */ 2177 ifindex = ntohs(gsa->sin6.sin6_addr.s6_addr16[1]); 2178 KASSERT(ifindex != 0, ("%s: bad zone ID", __func__)); 2179 ifp = ifnet_byindex(ifindex); 2180 if (ifp == NULL) 2181 return (EADDRNOTAVAIL); 2182 } 2183 2184 CTR2(KTR_MLD, "%s: ifp = %p", __func__, ifp); 2185 KASSERT(ifp != NULL, ("%s: ifp did not resolve", __func__)); 2186 2187 /* 2188 * Find the membership in the membership array. 2189 */ 2190 imo = in6p_findmoptions(inp); 2191 idx = im6o_match_group(imo, ifp, &gsa->sa); 2192 if (idx == -1) { 2193 error = EADDRNOTAVAIL; 2194 goto out_in6p_locked; 2195 } 2196 inm = imo->im6o_membership[idx]; 2197 imf = &imo->im6o_mfilters[idx]; 2198 2199 if (ssa->ss.ss_family != AF_UNSPEC) 2200 is_final = 0; 2201 2202 /* 2203 * Begin state merge transaction at socket layer. 2204 */ 2205 INP_WLOCK_ASSERT(inp); 2206 2207 /* 2208 * If we were instructed only to leave a given source, do so. 2209 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships. 2210 */ 2211 if (is_final) { 2212 im6f_leave(imf); 2213 } else { 2214 if (imf->im6f_st[0] == MCAST_EXCLUDE) { 2215 error = EADDRNOTAVAIL; 2216 goto out_in6p_locked; 2217 } 2218 ims = im6o_match_source(imo, idx, &ssa->sa); 2219 if (ims == NULL) { 2220 CTR3(KTR_MLD, "%s: source %p %spresent", __func__, 2221 ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr), 2222 "not "); 2223 error = EADDRNOTAVAIL; 2224 goto out_in6p_locked; 2225 } 2226 CTR2(KTR_MLD, "%s: %s source", __func__, "block"); 2227 error = im6f_prune(imf, &ssa->sin6); 2228 if (error) { 2229 CTR1(KTR_MLD, "%s: merge imf state failed", 2230 __func__); 2231 goto out_in6p_locked; 2232 } 2233 } 2234 2235 /* 2236 * Begin state merge transaction at MLD layer. 2237 */ 2238 IN6_MULTI_LOCK(); 2239 2240 if (is_final) { 2241 /* 2242 * Give up the multicast address record to which 2243 * the membership points. 2244 */ 2245 (void)in6_mc_leave_locked(inm, imf); 2246 } else { 2247 CTR1(KTR_MLD, "%s: merge inm state", __func__); 2248 error = in6m_merge(inm, imf); 2249 if (error) { 2250 CTR1(KTR_MLD, "%s: failed to merge inm state", 2251 __func__); 2252 goto out_im6f_rollback; 2253 } 2254 2255 CTR1(KTR_MLD, "%s: doing mld downcall", __func__); 2256 error = mld_change_state(inm, 0); 2257 if (error) { 2258 CTR1(KTR_MLD, "%s: failed mld downcall", 2259 __func__); 2260 } 2261 } 2262 2263 IN6_MULTI_UNLOCK(); 2264 2265out_im6f_rollback: 2266 if (error) 2267 im6f_rollback(imf); 2268 else 2269 im6f_commit(imf); 2270 2271 im6f_reap(imf); 2272 2273 if (is_final) { 2274 /* Remove the gap in the membership array. */ 2275 for (++idx; idx < imo->im6o_num_memberships; ++idx) 2276 imo->im6o_membership[idx-1] = imo->im6o_membership[idx]; 2277 imo->im6o_num_memberships--; 2278 } 2279 2280out_in6p_locked: 2281 INP_WUNLOCK(inp); 2282 return (error); 2283} 2284 2285/* 2286 * Select the interface for transmitting IPv6 multicast datagrams. 2287 * 2288 * Either an instance of struct in6_addr or an instance of struct ipv6_mreqn 2289 * may be passed to this socket option. An address of in6addr_any or an 2290 * interface index of 0 is used to remove a previous selection. 2291 * When no interface is selected, one is chosen for every send. 2292 */ 2293static int 2294in6p_set_multicast_if(struct inpcb *inp, struct sockopt *sopt) 2295{ 2296 INIT_VNET_NET(curvnet); 2297 struct ifnet *ifp; 2298 struct ip6_moptions *imo; 2299 u_int ifindex; 2300 int error; 2301 2302 if (sopt->sopt_valsize != sizeof(u_int)) 2303 return (EINVAL); 2304 2305 error = sooptcopyin(sopt, &ifindex, sizeof(u_int), sizeof(u_int)); 2306 if (error) 2307 return (error); 2308 if (ifindex < 0 || V_if_index < ifindex) 2309 return (EINVAL); 2310 2311 ifp = ifnet_byindex(ifindex); 2312 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) 2313 return (EADDRNOTAVAIL); 2314 2315 imo = in6p_findmoptions(inp); 2316 imo->im6o_multicast_ifp = ifp; 2317 INP_WUNLOCK(inp); 2318 2319 return (0); 2320} 2321 2322/* 2323 * Atomically set source filters on a socket for an IPv6 multicast group. 2324 * 2325 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held. 2326 */ 2327static int 2328in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt) 2329{ 2330 INIT_VNET_NET(curvnet); 2331 struct __msfilterreq msfr; 2332 sockunion_t *gsa; 2333 struct ifnet *ifp; 2334 struct in6_mfilter *imf; 2335 struct ip6_moptions *imo; 2336 struct in6_multi *inm; 2337 size_t idx; 2338 int error; 2339 2340 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq), 2341 sizeof(struct __msfilterreq)); 2342 if (error) 2343 return (error); 2344 2345 if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc || 2346 (msfr.msfr_fmode != MCAST_EXCLUDE && 2347 msfr.msfr_fmode != MCAST_INCLUDE)) 2348 return (EINVAL); 2349 2350 if (msfr.msfr_group.ss_family != AF_INET6 || 2351 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6)) 2352 return (EINVAL); 2353 2354 gsa = (sockunion_t *)&msfr.msfr_group; 2355 if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr)) 2356 return (EINVAL); 2357 2358 gsa->sin6.sin6_port = 0; /* ignore port */ 2359 2360 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex) 2361 return (EADDRNOTAVAIL); 2362 ifp = ifnet_byindex(msfr.msfr_ifindex); 2363 if (ifp == NULL) 2364 return (EADDRNOTAVAIL); 2365 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL); 2366 2367 /* 2368 * Take the INP write lock. 2369 * Check if this socket is a member of this group. 2370 */ 2371 imo = in6p_findmoptions(inp); 2372 idx = im6o_match_group(imo, ifp, &gsa->sa); 2373 if (idx == -1 || imo->im6o_mfilters == NULL) { 2374 error = EADDRNOTAVAIL; 2375 goto out_in6p_locked; 2376 } 2377 inm = imo->im6o_membership[idx]; 2378 imf = &imo->im6o_mfilters[idx]; 2379 2380 /* 2381 * Begin state merge transaction at socket layer. 2382 */ 2383 INP_WLOCK_ASSERT(inp); 2384 2385 imf->im6f_st[1] = msfr.msfr_fmode; 2386 2387 /* 2388 * Apply any new source filters, if present. 2389 * Make a copy of the user-space source vector so 2390 * that we may copy them with a single copyin. This 2391 * allows us to deal with page faults up-front. 2392 */ 2393 if (msfr.msfr_nsrcs > 0) { 2394 struct in6_msource *lims; 2395 struct sockaddr_in6 *psin; 2396 struct sockaddr_storage *kss, *pkss; 2397 int i; 2398 2399 INP_WUNLOCK(inp); 2400 2401 CTR2(KTR_MLD, "%s: loading %lu source list entries", 2402 __func__, (unsigned long)msfr.msfr_nsrcs); 2403 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs, 2404 M_TEMP, M_WAITOK); 2405 error = copyin(msfr.msfr_srcs, kss, 2406 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs); 2407 if (error) { 2408 free(kss, M_TEMP); 2409 return (error); 2410 } 2411 2412 INP_WLOCK(inp); 2413 2414 /* 2415 * Mark all source filters as UNDEFINED at t1. 2416 * Restore new group filter mode, as im6f_leave() 2417 * will set it to INCLUDE. 2418 */ 2419 im6f_leave(imf); 2420 imf->im6f_st[1] = msfr.msfr_fmode; 2421 2422 /* 2423 * Update socket layer filters at t1, lazy-allocating 2424 * new entries. This saves a bunch of memory at the 2425 * cost of one RB_FIND() per source entry; duplicate 2426 * entries in the msfr_nsrcs vector are ignored. 2427 * If we encounter an error, rollback transaction. 2428 * 2429 * XXX This too could be replaced with a set-symmetric 2430 * difference like loop to avoid walking from root 2431 * every time, as the key space is common. 2432 */ 2433 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) { 2434 psin = (struct sockaddr_in6 *)pkss; 2435 if (psin->sin6_family != AF_INET6) { 2436 error = EAFNOSUPPORT; 2437 break; 2438 } 2439 if (psin->sin6_len != sizeof(struct sockaddr_in6)) { 2440 error = EINVAL; 2441 break; 2442 } 2443 if (IN6_IS_ADDR_MULTICAST(&psin->sin6_addr)) { 2444 error = EINVAL; 2445 break; 2446 } 2447 /* 2448 * TODO: Validate embedded scope ID in source 2449 * list entry against passed-in ifp, if and only 2450 * if source list filter entry is iface or node local. 2451 */ 2452 in6_clearscope(&psin->sin6_addr); 2453 error = im6f_get_source(imf, psin, &lims); 2454 if (error) 2455 break; 2456 lims->im6sl_st[1] = imf->im6f_st[1]; 2457 } 2458 free(kss, M_TEMP); 2459 } 2460 2461 if (error) 2462 goto out_im6f_rollback; 2463 2464 INP_WLOCK_ASSERT(inp); 2465 IN6_MULTI_LOCK(); 2466 2467 /* 2468 * Begin state merge transaction at MLD layer. 2469 */ 2470 CTR1(KTR_MLD, "%s: merge inm state", __func__); 2471 error = in6m_merge(inm, imf); 2472 if (error) { 2473 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__); 2474 goto out_im6f_rollback; 2475 } 2476 2477 CTR1(KTR_MLD, "%s: doing mld downcall", __func__); 2478 error = mld_change_state(inm, 0); 2479 if (error) 2480 CTR1(KTR_MLD, "%s: failed mld downcall", __func__); 2481 2482 IN6_MULTI_UNLOCK(); 2483 2484out_im6f_rollback: 2485 if (error) 2486 im6f_rollback(imf); 2487 else 2488 im6f_commit(imf); 2489 2490 im6f_reap(imf); 2491 2492out_in6p_locked: 2493 INP_WUNLOCK(inp); 2494 return (error); 2495} 2496 2497/* 2498 * Set the IP multicast options in response to user setsockopt(). 2499 * 2500 * Many of the socket options handled in this function duplicate the 2501 * functionality of socket options in the regular unicast API. However, 2502 * it is not possible to merge the duplicate code, because the idempotence 2503 * of the IPv6 multicast part of the BSD Sockets API must be preserved; 2504 * the effects of these options must be treated as separate and distinct. 2505 * 2506 * SMPng: XXX: Unlocked read of inp_socket believed OK. 2507 */ 2508int 2509ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt) 2510{ 2511 INIT_VNET_INET6(curvnet); 2512 struct ip6_moptions *im6o; 2513 int error; 2514 2515 error = 0; 2516 2517 /* 2518 * If socket is neither of type SOCK_RAW or SOCK_DGRAM, 2519 * or is a divert socket, reject it. 2520 */ 2521 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT || 2522 (inp->inp_socket->so_proto->pr_type != SOCK_RAW && 2523 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) 2524 return (EOPNOTSUPP); 2525 2526 switch (sopt->sopt_name) { 2527 case IPV6_MULTICAST_IF: 2528 error = in6p_set_multicast_if(inp, sopt); 2529 break; 2530 2531 case IPV6_MULTICAST_HOPS: { 2532 int hlim; 2533 2534 if (sopt->sopt_valsize != sizeof(int)) { 2535 error = EINVAL; 2536 break; 2537 } 2538 error = sooptcopyin(sopt, &hlim, sizeof(hlim), sizeof(int)); 2539 if (error) 2540 break; 2541 if (hlim < -1 || hlim > 255) { 2542 error = EINVAL; 2543 break; 2544 } else if (hlim == -1) { 2545 hlim = V_ip6_defmcasthlim; 2546 } 2547 im6o = in6p_findmoptions(inp); 2548 im6o->im6o_multicast_hlim = hlim; 2549 INP_WUNLOCK(inp); 2550 break; 2551 } 2552 2553 case IPV6_MULTICAST_LOOP: { 2554 u_int loop; 2555 2556 /* 2557 * Set the loopback flag for outgoing multicast packets. 2558 * Must be zero or one. 2559 */ 2560 if (sopt->sopt_valsize != sizeof(u_int)) { 2561 error = EINVAL; 2562 break; 2563 } 2564 error = sooptcopyin(sopt, &loop, sizeof(u_int), sizeof(u_int)); 2565 if (error) 2566 break; 2567 if (loop > 1) { 2568 error = EINVAL; 2569 break; 2570 } 2571 im6o = in6p_findmoptions(inp); 2572 im6o->im6o_multicast_loop = loop; 2573 INP_WUNLOCK(inp); 2574 break; 2575 } 2576 2577 case IPV6_JOIN_GROUP: 2578 case MCAST_JOIN_GROUP: 2579 case MCAST_JOIN_SOURCE_GROUP: 2580 error = in6p_join_group(inp, sopt); 2581 break; 2582 2583 case IPV6_LEAVE_GROUP: 2584 case MCAST_LEAVE_GROUP: 2585 case MCAST_LEAVE_SOURCE_GROUP: 2586 error = in6p_leave_group(inp, sopt); 2587 break; 2588 2589 case MCAST_BLOCK_SOURCE: 2590 case MCAST_UNBLOCK_SOURCE: 2591 error = in6p_block_unblock_source(inp, sopt); 2592 break; 2593 2594 case IPV6_MSFILTER: 2595 error = in6p_set_source_filters(inp, sopt); 2596 break; 2597 2598 default: 2599 error = EOPNOTSUPP; 2600 break; 2601 } 2602 2603 INP_UNLOCK_ASSERT(inp); 2604 2605 return (error); 2606} 2607 2608/* 2609 * Expose MLD's multicast filter mode and source list(s) to userland, 2610 * keyed by (ifindex, group). 2611 * The filter mode is written out as a uint32_t, followed by 2612 * 0..n of struct in6_addr. 2613 * For use by ifmcstat(8). 2614 * SMPng: NOTE: unlocked read of ifindex space. 2615 */ 2616static int 2617sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS) 2618{ 2619 INIT_VNET_NET(curvnet); 2620 struct in6_addr mcaddr; 2621 struct in6_addr src; 2622 struct ifnet *ifp; 2623 struct ifmultiaddr *ifma; 2624 struct in6_multi *inm; 2625 struct ip6_msource *ims; 2626 int *name; 2627 int retval; 2628 u_int namelen; 2629 uint32_t fmode, ifindex; 2630#ifdef KTR 2631 char ip6tbuf[INET6_ADDRSTRLEN]; 2632#endif 2633 2634 name = (int *)arg1; 2635 namelen = arg2; 2636 2637 if (req->newptr != NULL) 2638 return (EPERM); 2639 2640 /* int: ifindex + 4 * 32 bits of IPv6 address */ 2641 if (namelen != 5) 2642 return (EINVAL); 2643 2644 ifindex = name[0]; 2645 if (ifindex <= 0 || ifindex > V_if_index) { 2646 CTR2(KTR_MLD, "%s: ifindex %u out of range", 2647 __func__, ifindex); 2648 return (ENOENT); 2649 } 2650 2651 memcpy(&mcaddr, &name[1], sizeof(struct in6_addr)); 2652 if (!IN6_IS_ADDR_MULTICAST(&mcaddr)) { 2653 CTR2(KTR_MLD, "%s: group %s is not multicast", 2654 __func__, ip6_sprintf(ip6tbuf, &mcaddr)); 2655 return (EINVAL); 2656 } 2657 2658 ifp = ifnet_byindex(ifindex); 2659 if (ifp == NULL) { 2660 CTR2(KTR_MLD, "%s: no ifp for ifindex %u", 2661 __func__, ifindex); 2662 return (ENOENT); 2663 } 2664 /* 2665 * Internal MLD lookups require that scope/zone ID is set. 2666 */ 2667 (void)in6_setscope(&mcaddr, ifp, NULL); 2668 2669 retval = sysctl_wire_old_buffer(req, 2670 sizeof(uint32_t) + (in6_mcast_maxgrpsrc * sizeof(struct in6_addr))); 2671 if (retval) 2672 return (retval); 2673 2674 IN6_MULTI_LOCK(); 2675 2676 IF_ADDR_LOCK(ifp); 2677 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2678 if (ifma->ifma_addr->sa_family != AF_INET6 || 2679 ifma->ifma_protospec == NULL) 2680 continue; 2681 inm = (struct in6_multi *)ifma->ifma_protospec; 2682 if (!IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, &mcaddr)) 2683 continue; 2684 fmode = inm->in6m_st[1].iss_fmode; 2685 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t)); 2686 if (retval != 0) 2687 break; 2688 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) { 2689 CTR2(KTR_MLD, "%s: visit node %p", __func__, ims); 2690 /* 2691 * Only copy-out sources which are in-mode. 2692 */ 2693 if (fmode != im6s_get_mode(inm, ims, 1)) { 2694 CTR1(KTR_MLD, "%s: skip non-in-mode", 2695 __func__); 2696 continue; 2697 } 2698 src = ims->im6s_addr; 2699 retval = SYSCTL_OUT(req, &src, 2700 sizeof(struct in6_addr)); 2701 if (retval != 0) 2702 break; 2703 } 2704 } 2705 IF_ADDR_UNLOCK(ifp); 2706 2707 IN6_MULTI_UNLOCK(); 2708 2709 return (retval); 2710} 2711 2712#ifdef KTR 2713 2714static const char *in6m_modestrs[] = { "un", "in", "ex" }; 2715 2716static const char * 2717in6m_mode_str(const int mode) 2718{ 2719 2720 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE) 2721 return (in6m_modestrs[mode]); 2722 return ("??"); 2723} 2724 2725static const char *in6m_statestrs[] = { 2726 "not-member", 2727 "silent", 2728 "idle", 2729 "lazy", 2730 "sleeping", 2731 "awakening", 2732 "query-pending", 2733 "sg-query-pending", 2734 "leaving" 2735}; 2736 2737static const char * 2738in6m_state_str(const int state) 2739{ 2740 2741 if (state >= MLD_NOT_MEMBER && state <= MLD_LEAVING_MEMBER) 2742 return (in6m_statestrs[state]); 2743 return ("??"); 2744} 2745 2746/* 2747 * Dump an in6_multi structure to the console. 2748 */ 2749void 2750in6m_print(const struct in6_multi *inm) 2751{ 2752 int t; 2753 char ip6tbuf[INET6_ADDRSTRLEN]; 2754 2755 if ((ktr_mask & KTR_MLD) == 0) 2756 return; 2757 2758 printf("%s: --- begin in6m %p ---\n", __func__, inm); 2759 printf("addr %s ifp %p(%s) ifma %p\n", 2760 ip6_sprintf(ip6tbuf, &inm->in6m_addr), 2761 inm->in6m_ifp, 2762 inm->in6m_ifp->if_xname, 2763 inm->in6m_ifma); 2764 printf("timer %u state %s refcount %u scq.len %u\n", 2765 inm->in6m_timer, 2766 in6m_state_str(inm->in6m_state), 2767 inm->in6m_refcount, 2768 inm->in6m_scq.ifq_len); 2769 printf("mli %p nsrc %lu sctimer %u scrv %u\n", 2770 inm->in6m_mli, 2771 inm->in6m_nsrc, 2772 inm->in6m_sctimer, 2773 inm->in6m_scrv); 2774 for (t = 0; t < 2; t++) { 2775 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t, 2776 in6m_mode_str(inm->in6m_st[t].iss_fmode), 2777 inm->in6m_st[t].iss_asm, 2778 inm->in6m_st[t].iss_ex, 2779 inm->in6m_st[t].iss_in, 2780 inm->in6m_st[t].iss_rec); 2781 } 2782 printf("%s: --- end in6m %p ---\n", __func__, inm); 2783} 2784 2785#else /* !KTR */ 2786 2787void 2788in6m_print(const struct in6_multi *inm) 2789{ 2790 2791} 2792 2793#endif /* KTR */ 2794