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