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