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