1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2009 Bruce Simpson. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote 15 * products derived from this software without specific prior written 16 * permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $KAME: mld6.c,v 1.27 2001/04/04 05:17:30 itojun Exp $ 31 */ 32 33 /*- 34 * Copyright (c) 1988 Stephen Deering. 35 * Copyright (c) 1992, 1993 36 * The Regents of the University of California. All rights reserved. 37 * 38 * This code is derived from software contributed to Berkeley by 39 * Stephen Deering of Stanford University. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 3. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)igmp.c 8.1 (Berkeley) 7/19/93 66 */ 67 68 #include <sys/cdefs.h> 69 __FBSDID("$FreeBSD$"); 70 71 #include "opt_inet.h" 72 #include "opt_inet6.h" 73 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 #include <sys/mbuf.h> 77 #include <sys/socket.h> 78 #include <sys/sysctl.h> 79 #include <sys/kernel.h> 80 #include <sys/callout.h> 81 #include <sys/malloc.h> 82 #include <sys/module.h> 83 #include <sys/ktr.h> 84 85 #include <net/if.h> 86 #include <net/if_var.h> 87 #include <net/if_private.h> 88 #include <net/route.h> 89 #include <net/vnet.h> 90 91 #include <netinet/in.h> 92 #include <netinet/in_var.h> 93 #include <netinet6/in6_var.h> 94 #include <netinet/ip6.h> 95 #include <netinet6/ip6_var.h> 96 #include <netinet6/scope6_var.h> 97 #include <netinet/icmp6.h> 98 #include <netinet6/mld6.h> 99 #include <netinet6/mld6_var.h> 100 101 #include <security/mac/mac_framework.h> 102 103 #ifndef KTR_MLD 104 #define KTR_MLD KTR_INET6 105 #endif 106 107 static void mli_delete_locked(struct ifnet *); 108 static void mld_dispatch_packet(struct mbuf *); 109 static void mld_dispatch_queue(struct mbufq *, int); 110 static void mld_final_leave(struct in6_multi *, struct mld_ifsoftc *); 111 static void mld_fasttimo_vnet(struct in6_multi_head *inmh); 112 static int mld_handle_state_change(struct in6_multi *, 113 struct mld_ifsoftc *); 114 static int mld_initial_join(struct in6_multi *, struct mld_ifsoftc *, 115 const int); 116 #ifdef KTR 117 static char * mld_rec_type_to_str(const int); 118 #endif 119 static void mld_set_version(struct mld_ifsoftc *, const int); 120 static void mld_slowtimo_vnet(void); 121 static int mld_v1_input_query(struct ifnet *, const struct ip6_hdr *, 122 /*const*/ struct mld_hdr *); 123 static int mld_v1_input_report(struct ifnet *, const struct ip6_hdr *, 124 /*const*/ struct mld_hdr *); 125 static void mld_v1_process_group_timer(struct in6_multi_head *, 126 struct in6_multi *); 127 static void mld_v1_process_querier_timers(struct mld_ifsoftc *); 128 static int mld_v1_transmit_report(struct in6_multi *, const int); 129 static void mld_v1_update_group(struct in6_multi *, const int); 130 static void mld_v2_cancel_link_timers(struct mld_ifsoftc *); 131 static void mld_v2_dispatch_general_query(struct mld_ifsoftc *); 132 static struct mbuf * 133 mld_v2_encap_report(struct ifnet *, struct mbuf *); 134 static int mld_v2_enqueue_filter_change(struct mbufq *, 135 struct in6_multi *); 136 static int mld_v2_enqueue_group_record(struct mbufq *, 137 struct in6_multi *, const int, const int, const int, 138 const int); 139 static int mld_v2_input_query(struct ifnet *, const struct ip6_hdr *, 140 struct mbuf *, struct mldv2_query *, const int, const int); 141 static int mld_v2_merge_state_changes(struct in6_multi *, 142 struct mbufq *); 143 static void mld_v2_process_group_timers(struct in6_multi_head *, 144 struct mbufq *, struct mbufq *, 145 struct in6_multi *, const int); 146 static int mld_v2_process_group_query(struct in6_multi *, 147 struct mld_ifsoftc *mli, int, struct mbuf *, 148 struct mldv2_query *, const int); 149 static int sysctl_mld_gsr(SYSCTL_HANDLER_ARGS); 150 static int sysctl_mld_ifinfo(SYSCTL_HANDLER_ARGS); 151 152 /* 153 * Normative references: RFC 2710, RFC 3590, RFC 3810. 154 * 155 * Locking: 156 * * The MLD subsystem lock ends up being system-wide for the moment, 157 * but could be per-VIMAGE later on. 158 * * The permitted lock order is: IN6_MULTI_LOCK, MLD_LOCK, IF_ADDR_LOCK. 159 * Any may be taken independently; if any are held at the same 160 * time, the above lock order must be followed. 161 * * IN6_MULTI_LOCK covers in_multi. 162 * * MLD_LOCK covers per-link state and any global variables in this file. 163 * * IF_ADDR_LOCK covers if_multiaddrs, which is used for a variety of 164 * per-link state iterators. 165 * 166 * XXX LOR PREVENTION 167 * A special case for IPv6 is the in6_setscope() routine. ip6_output() 168 * will not accept an ifp; it wants an embedded scope ID, unlike 169 * ip_output(), which happily takes the ifp given to it. The embedded 170 * scope ID is only used by MLD to select the outgoing interface. 171 * 172 * During interface attach and detach, MLD will take MLD_LOCK *after* 173 * the IF_AFDATA_LOCK. 174 * As in6_setscope() takes IF_AFDATA_LOCK then SCOPE_LOCK, we can't call 175 * it with MLD_LOCK held without triggering an LOR. A netisr with indirect 176 * dispatch could work around this, but we'd rather not do that, as it 177 * can introduce other races. 178 * 179 * As such, we exploit the fact that the scope ID is just the interface 180 * index, and embed it in the IPv6 destination address accordingly. 181 * This is potentially NOT VALID for MLDv1 reports, as they 182 * are always sent to the multicast group itself; as MLDv2 183 * reports are always sent to ff02::16, this is not an issue 184 * when MLDv2 is in use. 185 * 186 * This does not however eliminate the LOR when ip6_output() itself 187 * calls in6_setscope() internally whilst MLD_LOCK is held. This will 188 * trigger a LOR warning in WITNESS when the ifnet is detached. 189 * 190 * The right answer is probably to make IF_AFDATA_LOCK an rwlock, given 191 * how it's used across the network stack. Here we're simply exploiting 192 * the fact that MLD runs at a similar layer in the stack to scope6.c. 193 * 194 * VIMAGE: 195 * * Each in6_multi corresponds to an ifp, and each ifp corresponds 196 * to a vnet in ifp->if_vnet. 197 */ 198 static struct mtx mld_mtx; 199 static MALLOC_DEFINE(M_MLD, "mld", "mld state"); 200 201 #define MLD_EMBEDSCOPE(pin6, zoneid) \ 202 if (IN6_IS_SCOPE_LINKLOCAL(pin6) || \ 203 IN6_IS_ADDR_MC_INTFACELOCAL(pin6)) \ 204 (pin6)->s6_addr16[1] = htons((zoneid) & 0xFFFF) \ 205 206 /* 207 * VIMAGE-wide globals. 208 */ 209 VNET_DEFINE_STATIC(struct timeval, mld_gsrdelay) = {10, 0}; 210 VNET_DEFINE_STATIC(LIST_HEAD(, mld_ifsoftc), mli_head); 211 VNET_DEFINE_STATIC(int, interface_timers_running6); 212 VNET_DEFINE_STATIC(int, state_change_timers_running6); 213 VNET_DEFINE_STATIC(int, current_state_timers_running6); 214 215 #define V_mld_gsrdelay VNET(mld_gsrdelay) 216 #define V_mli_head VNET(mli_head) 217 #define V_interface_timers_running6 VNET(interface_timers_running6) 218 #define V_state_change_timers_running6 VNET(state_change_timers_running6) 219 #define V_current_state_timers_running6 VNET(current_state_timers_running6) 220 221 SYSCTL_DECL(_net_inet6); /* Note: Not in any common header. */ 222 223 SYSCTL_NODE(_net_inet6, OID_AUTO, mld, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 224 "IPv6 Multicast Listener Discovery"); 225 226 /* 227 * Virtualized sysctls. 228 */ 229 SYSCTL_PROC(_net_inet6_mld, OID_AUTO, gsrdelay, 230 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 231 &VNET_NAME(mld_gsrdelay.tv_sec), 0, sysctl_mld_gsr, "I", 232 "Rate limit for MLDv2 Group-and-Source queries in seconds"); 233 234 /* 235 * Non-virtualized sysctls. 236 */ 237 static SYSCTL_NODE(_net_inet6_mld, OID_AUTO, ifinfo, 238 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_mld_ifinfo, 239 "Per-interface MLDv2 state"); 240 241 static int mld_v1enable = 1; 242 SYSCTL_INT(_net_inet6_mld, OID_AUTO, v1enable, CTLFLAG_RWTUN, 243 &mld_v1enable, 0, "Enable fallback to MLDv1"); 244 245 static int mld_v2enable = 1; 246 SYSCTL_INT(_net_inet6_mld, OID_AUTO, v2enable, CTLFLAG_RWTUN, 247 &mld_v2enable, 0, "Enable MLDv2"); 248 249 static int mld_use_allow = 1; 250 SYSCTL_INT(_net_inet6_mld, OID_AUTO, use_allow, CTLFLAG_RWTUN, 251 &mld_use_allow, 0, "Use ALLOW/BLOCK for RFC 4604 SSM joins/leaves"); 252 253 /* 254 * Packed Router Alert option structure declaration. 255 */ 256 struct mld_raopt { 257 struct ip6_hbh hbh; 258 struct ip6_opt pad; 259 struct ip6_opt_router ra; 260 } __packed; 261 262 /* 263 * Router Alert hop-by-hop option header. 264 */ 265 static struct mld_raopt mld_ra = { 266 .hbh = { 0, 0 }, 267 .pad = { .ip6o_type = IP6OPT_PADN, 0 }, 268 .ra = { 269 .ip6or_type = IP6OPT_ROUTER_ALERT, 270 .ip6or_len = IP6OPT_RTALERT_LEN - 2, 271 .ip6or_value[0] = ((IP6OPT_RTALERT_MLD >> 8) & 0xFF), 272 .ip6or_value[1] = (IP6OPT_RTALERT_MLD & 0xFF) 273 } 274 }; 275 static struct ip6_pktopts mld_po; 276 277 static __inline void 278 mld_save_context(struct mbuf *m, struct ifnet *ifp) 279 { 280 281 #ifdef VIMAGE 282 m->m_pkthdr.PH_loc.ptr = ifp->if_vnet; 283 #endif /* VIMAGE */ 284 m->m_pkthdr.rcvif = ifp; 285 m->m_pkthdr.flowid = ifp->if_index; 286 } 287 288 static __inline void 289 mld_scrub_context(struct mbuf *m) 290 { 291 292 m->m_pkthdr.PH_loc.ptr = NULL; 293 m->m_pkthdr.flowid = 0; 294 } 295 296 /* 297 * Restore context from a queued output chain. 298 * Return saved ifindex. 299 * 300 * VIMAGE: The assertion is there to make sure that we 301 * actually called CURVNET_SET() with what's in the mbuf chain. 302 */ 303 static __inline uint32_t 304 mld_restore_context(struct mbuf *m) 305 { 306 307 #if defined(VIMAGE) && defined(INVARIANTS) 308 KASSERT(curvnet == m->m_pkthdr.PH_loc.ptr, 309 ("%s: called when curvnet was not restored: cuvnet %p m ptr %p", 310 __func__, curvnet, m->m_pkthdr.PH_loc.ptr)); 311 #endif 312 return (m->m_pkthdr.flowid); 313 } 314 315 /* 316 * Retrieve or set threshold between group-source queries in seconds. 317 * 318 * VIMAGE: Assume curvnet set by caller. 319 * SMPng: NOTE: Serialized by MLD lock. 320 */ 321 static int 322 sysctl_mld_gsr(SYSCTL_HANDLER_ARGS) 323 { 324 int error; 325 int i; 326 327 error = sysctl_wire_old_buffer(req, sizeof(int)); 328 if (error) 329 return (error); 330 331 MLD_LOCK(); 332 333 i = V_mld_gsrdelay.tv_sec; 334 335 error = sysctl_handle_int(oidp, &i, 0, req); 336 if (error || !req->newptr) 337 goto out_locked; 338 339 if (i < -1 || i >= 60) { 340 error = EINVAL; 341 goto out_locked; 342 } 343 344 CTR2(KTR_MLD, "change mld_gsrdelay from %d to %d", 345 V_mld_gsrdelay.tv_sec, i); 346 V_mld_gsrdelay.tv_sec = i; 347 348 out_locked: 349 MLD_UNLOCK(); 350 return (error); 351 } 352 353 /* 354 * Expose struct mld_ifsoftc to userland, keyed by ifindex. 355 * For use by ifmcstat(8). 356 * 357 * VIMAGE: Assume curvnet set by caller. The node handler itself 358 * is not directly virtualized. 359 */ 360 static int 361 sysctl_mld_ifinfo(SYSCTL_HANDLER_ARGS) 362 { 363 struct epoch_tracker et; 364 int *name; 365 int error; 366 u_int namelen; 367 struct ifnet *ifp; 368 struct mld_ifsoftc *mli; 369 370 name = (int *)arg1; 371 namelen = arg2; 372 373 if (req->newptr != NULL) 374 return (EPERM); 375 376 if (namelen != 1) 377 return (EINVAL); 378 379 error = sysctl_wire_old_buffer(req, sizeof(struct mld_ifinfo)); 380 if (error) 381 return (error); 382 383 IN6_MULTI_LOCK(); 384 IN6_MULTI_LIST_LOCK(); 385 MLD_LOCK(); 386 NET_EPOCH_ENTER(et); 387 388 error = ENOENT; 389 ifp = ifnet_byindex(name[0]); 390 if (ifp == NULL) 391 goto out_locked; 392 393 LIST_FOREACH(mli, &V_mli_head, mli_link) { 394 if (ifp == mli->mli_ifp) { 395 struct mld_ifinfo info; 396 397 info.mli_version = mli->mli_version; 398 info.mli_v1_timer = mli->mli_v1_timer; 399 info.mli_v2_timer = mli->mli_v2_timer; 400 info.mli_flags = mli->mli_flags; 401 info.mli_rv = mli->mli_rv; 402 info.mli_qi = mli->mli_qi; 403 info.mli_qri = mli->mli_qri; 404 info.mli_uri = mli->mli_uri; 405 error = SYSCTL_OUT(req, &info, sizeof(info)); 406 break; 407 } 408 } 409 410 out_locked: 411 NET_EPOCH_EXIT(et); 412 MLD_UNLOCK(); 413 IN6_MULTI_LIST_UNLOCK(); 414 IN6_MULTI_UNLOCK(); 415 return (error); 416 } 417 418 /* 419 * Dispatch an entire queue of pending packet chains. 420 * VIMAGE: Assumes the vnet pointer has been set. 421 */ 422 static void 423 mld_dispatch_queue(struct mbufq *mq, int limit) 424 { 425 struct mbuf *m; 426 427 while ((m = mbufq_dequeue(mq)) != NULL) { 428 CTR3(KTR_MLD, "%s: dispatch %p from %p", __func__, mq, m); 429 mld_dispatch_packet(m); 430 if (--limit == 0) 431 break; 432 } 433 } 434 435 /* 436 * Filter outgoing MLD report state by group. 437 * 438 * Reports are ALWAYS suppressed for ALL-HOSTS (ff02::1) 439 * and node-local addresses. However, kernel and socket consumers 440 * always embed the KAME scope ID in the address provided, so strip it 441 * when performing comparison. 442 * Note: This is not the same as the *multicast* scope. 443 * 444 * Return zero if the given group is one for which MLD reports 445 * should be suppressed, or non-zero if reports should be issued. 446 */ 447 static __inline int 448 mld_is_addr_reported(const struct in6_addr *addr) 449 { 450 451 KASSERT(IN6_IS_ADDR_MULTICAST(addr), ("%s: not multicast", __func__)); 452 453 if (IPV6_ADDR_MC_SCOPE(addr) == IPV6_ADDR_SCOPE_NODELOCAL) 454 return (0); 455 456 if (IPV6_ADDR_MC_SCOPE(addr) == IPV6_ADDR_SCOPE_LINKLOCAL) { 457 struct in6_addr tmp = *addr; 458 in6_clearscope(&tmp); 459 if (IN6_ARE_ADDR_EQUAL(&tmp, &in6addr_linklocal_allnodes)) 460 return (0); 461 } 462 463 return (1); 464 } 465 466 /* 467 * Attach MLD when PF_INET6 is attached to an interface. Assumes that the 468 * current VNET is set by the caller. 469 */ 470 struct mld_ifsoftc * 471 mld_domifattach(struct ifnet *ifp) 472 { 473 struct mld_ifsoftc *mli; 474 475 CTR3(KTR_MLD, "%s: called for ifp %p(%s)", __func__, ifp, if_name(ifp)); 476 477 mli = malloc(sizeof(struct mld_ifsoftc), M_MLD, M_WAITOK | M_ZERO); 478 mli->mli_ifp = ifp; 479 mli->mli_version = MLD_VERSION_2; 480 mli->mli_flags = 0; 481 mli->mli_rv = MLD_RV_INIT; 482 mli->mli_qi = MLD_QI_INIT; 483 mli->mli_qri = MLD_QRI_INIT; 484 mli->mli_uri = MLD_URI_INIT; 485 mbufq_init(&mli->mli_gq, MLD_MAX_RESPONSE_PACKETS); 486 if ((ifp->if_flags & IFF_MULTICAST) == 0) 487 mli->mli_flags |= MLIF_SILENT; 488 if (mld_use_allow) 489 mli->mli_flags |= MLIF_USEALLOW; 490 491 MLD_LOCK(); 492 LIST_INSERT_HEAD(&V_mli_head, mli, mli_link); 493 MLD_UNLOCK(); 494 495 return (mli); 496 } 497 498 /* 499 * Hook for ifdetach. 500 * 501 * NOTE: Some finalization tasks need to run before the protocol domain 502 * is detached, but also before the link layer does its cleanup. 503 * Run before link-layer cleanup; cleanup groups, but do not free MLD state. 504 * 505 * SMPng: Caller must hold IN6_MULTI_LOCK(). 506 * Must take IF_ADDR_LOCK() to cover if_multiaddrs iterator. 507 * XXX This routine is also bitten by unlocked ifma_protospec access. 508 */ 509 void 510 mld_ifdetach(struct ifnet *ifp, struct in6_multi_head *inmh) 511 { 512 struct epoch_tracker et; 513 struct mld_ifsoftc *mli; 514 struct ifmultiaddr *ifma; 515 struct in6_multi *inm; 516 517 CTR3(KTR_MLD, "%s: called for ifp %p(%s)", __func__, ifp, 518 if_name(ifp)); 519 520 IN6_MULTI_LIST_LOCK_ASSERT(); 521 MLD_LOCK(); 522 523 mli = MLD_IFINFO(ifp); 524 IF_ADDR_WLOCK(ifp); 525 /* 526 * Extract list of in6_multi associated with the detaching ifp 527 * which the PF_INET6 layer is about to release. 528 */ 529 NET_EPOCH_ENTER(et); 530 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 531 inm = in6m_ifmultiaddr_get_inm(ifma); 532 if (inm == NULL) 533 continue; 534 in6m_disconnect_locked(inmh, inm); 535 536 if (mli->mli_version == MLD_VERSION_2) { 537 in6m_clear_recorded(inm); 538 539 /* 540 * We need to release the final reference held 541 * for issuing the INCLUDE {}. 542 */ 543 if (inm->in6m_state == MLD_LEAVING_MEMBER) { 544 inm->in6m_state = MLD_NOT_MEMBER; 545 in6m_rele_locked(inmh, inm); 546 } 547 } 548 } 549 NET_EPOCH_EXIT(et); 550 IF_ADDR_WUNLOCK(ifp); 551 MLD_UNLOCK(); 552 } 553 554 /* 555 * Hook for domifdetach. 556 * Runs after link-layer cleanup; free MLD state. 557 * 558 * SMPng: Normally called with IF_AFDATA_LOCK held. 559 */ 560 void 561 mld_domifdetach(struct ifnet *ifp) 562 { 563 564 CTR3(KTR_MLD, "%s: called for ifp %p(%s)", 565 __func__, ifp, if_name(ifp)); 566 567 MLD_LOCK(); 568 mli_delete_locked(ifp); 569 MLD_UNLOCK(); 570 } 571 572 static void 573 mli_delete_locked(struct ifnet *ifp) 574 { 575 struct mld_ifsoftc *mli, *tmli; 576 577 CTR3(KTR_MLD, "%s: freeing mld_ifsoftc for ifp %p(%s)", 578 __func__, ifp, if_name(ifp)); 579 580 MLD_LOCK_ASSERT(); 581 582 LIST_FOREACH_SAFE(mli, &V_mli_head, mli_link, tmli) { 583 if (mli->mli_ifp == ifp) { 584 /* 585 * Free deferred General Query responses. 586 */ 587 mbufq_drain(&mli->mli_gq); 588 589 LIST_REMOVE(mli, mli_link); 590 591 free(mli, M_MLD); 592 return; 593 } 594 } 595 } 596 597 /* 598 * Process a received MLDv1 general or address-specific query. 599 * Assumes that the query header has been pulled up to sizeof(mld_hdr). 600 * 601 * NOTE: Can't be fully const correct as we temporarily embed scope ID in 602 * mld_addr. This is OK as we own the mbuf chain. 603 */ 604 static int 605 mld_v1_input_query(struct ifnet *ifp, const struct ip6_hdr *ip6, 606 /*const*/ struct mld_hdr *mld) 607 { 608 struct ifmultiaddr *ifma; 609 struct mld_ifsoftc *mli; 610 struct in6_multi *inm; 611 int is_general_query; 612 uint16_t timer; 613 #ifdef KTR 614 char ip6tbuf[INET6_ADDRSTRLEN]; 615 #endif 616 617 NET_EPOCH_ASSERT(); 618 619 is_general_query = 0; 620 621 if (!mld_v1enable) { 622 CTR3(KTR_MLD, "ignore v1 query %s on ifp %p(%s)", 623 ip6_sprintf(ip6tbuf, &mld->mld_addr), 624 ifp, if_name(ifp)); 625 return (0); 626 } 627 628 /* 629 * RFC3810 Section 6.2: MLD queries must originate from 630 * a router's link-local address. 631 */ 632 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) { 633 CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)", 634 ip6_sprintf(ip6tbuf, &ip6->ip6_src), 635 ifp, if_name(ifp)); 636 return (0); 637 } 638 639 /* 640 * Do address field validation upfront before we accept 641 * the query. 642 */ 643 if (IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr)) { 644 /* 645 * MLDv1 General Query. 646 * If this was not sent to the all-nodes group, ignore it. 647 */ 648 struct in6_addr dst; 649 650 dst = ip6->ip6_dst; 651 in6_clearscope(&dst); 652 if (!IN6_ARE_ADDR_EQUAL(&dst, &in6addr_linklocal_allnodes)) 653 return (EINVAL); 654 is_general_query = 1; 655 } else { 656 /* 657 * Embed scope ID of receiving interface in MLD query for 658 * lookup whilst we don't hold other locks. 659 */ 660 in6_setscope(&mld->mld_addr, ifp, NULL); 661 } 662 663 IN6_MULTI_LIST_LOCK(); 664 MLD_LOCK(); 665 666 /* 667 * Switch to MLDv1 host compatibility mode. 668 */ 669 mli = MLD_IFINFO(ifp); 670 KASSERT(mli != NULL, ("%s: no mld_ifsoftc for ifp %p", __func__, ifp)); 671 mld_set_version(mli, MLD_VERSION_1); 672 673 timer = (ntohs(mld->mld_maxdelay) * MLD_FASTHZ) / MLD_TIMER_SCALE; 674 if (timer == 0) 675 timer = 1; 676 677 if (is_general_query) { 678 /* 679 * For each reporting group joined on this 680 * interface, kick the report timer. 681 */ 682 CTR2(KTR_MLD, "process v1 general query on ifp %p(%s)", 683 ifp, if_name(ifp)); 684 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 685 inm = in6m_ifmultiaddr_get_inm(ifma); 686 if (inm == NULL) 687 continue; 688 mld_v1_update_group(inm, timer); 689 } 690 } else { 691 /* 692 * MLDv1 Group-Specific Query. 693 * If this is a group-specific MLDv1 query, we need only 694 * look up the single group to process it. 695 */ 696 inm = in6m_lookup_locked(ifp, &mld->mld_addr); 697 if (inm != NULL) { 698 CTR3(KTR_MLD, "process v1 query %s on ifp %p(%s)", 699 ip6_sprintf(ip6tbuf, &mld->mld_addr), 700 ifp, if_name(ifp)); 701 mld_v1_update_group(inm, timer); 702 } 703 /* XXX Clear embedded scope ID as userland won't expect it. */ 704 in6_clearscope(&mld->mld_addr); 705 } 706 707 MLD_UNLOCK(); 708 IN6_MULTI_LIST_UNLOCK(); 709 710 return (0); 711 } 712 713 /* 714 * Update the report timer on a group in response to an MLDv1 query. 715 * 716 * If we are becoming the reporting member for this group, start the timer. 717 * If we already are the reporting member for this group, and timer is 718 * below the threshold, reset it. 719 * 720 * We may be updating the group for the first time since we switched 721 * to MLDv2. If we are, then we must clear any recorded source lists, 722 * and transition to REPORTING state; the group timer is overloaded 723 * for group and group-source query responses. 724 * 725 * Unlike MLDv2, the delay per group should be jittered 726 * to avoid bursts of MLDv1 reports. 727 */ 728 static void 729 mld_v1_update_group(struct in6_multi *inm, const int timer) 730 { 731 #ifdef KTR 732 char ip6tbuf[INET6_ADDRSTRLEN]; 733 #endif 734 735 CTR4(KTR_MLD, "%s: %s/%s timer=%d", __func__, 736 ip6_sprintf(ip6tbuf, &inm->in6m_addr), 737 if_name(inm->in6m_ifp), timer); 738 739 IN6_MULTI_LIST_LOCK_ASSERT(); 740 741 switch (inm->in6m_state) { 742 case MLD_NOT_MEMBER: 743 case MLD_SILENT_MEMBER: 744 break; 745 case MLD_REPORTING_MEMBER: 746 if (inm->in6m_timer != 0 && 747 inm->in6m_timer <= timer) { 748 CTR1(KTR_MLD, "%s: REPORTING and timer running, " 749 "skipping.", __func__); 750 break; 751 } 752 /* FALLTHROUGH */ 753 case MLD_SG_QUERY_PENDING_MEMBER: 754 case MLD_G_QUERY_PENDING_MEMBER: 755 case MLD_IDLE_MEMBER: 756 case MLD_LAZY_MEMBER: 757 case MLD_AWAKENING_MEMBER: 758 CTR1(KTR_MLD, "%s: ->REPORTING", __func__); 759 inm->in6m_state = MLD_REPORTING_MEMBER; 760 inm->in6m_timer = MLD_RANDOM_DELAY(timer); 761 V_current_state_timers_running6 = 1; 762 break; 763 case MLD_SLEEPING_MEMBER: 764 CTR1(KTR_MLD, "%s: ->AWAKENING", __func__); 765 inm->in6m_state = MLD_AWAKENING_MEMBER; 766 break; 767 case MLD_LEAVING_MEMBER: 768 break; 769 } 770 } 771 772 /* 773 * Process a received MLDv2 general, group-specific or 774 * group-and-source-specific query. 775 * 776 * Assumes that mld points to a struct mldv2_query which is stored in 777 * contiguous memory. 778 * 779 * Return 0 if successful, otherwise an appropriate error code is returned. 780 */ 781 static int 782 mld_v2_input_query(struct ifnet *ifp, const struct ip6_hdr *ip6, 783 struct mbuf *m, struct mldv2_query *mld, const int off, const int icmp6len) 784 { 785 struct mld_ifsoftc *mli; 786 struct in6_multi *inm; 787 uint32_t maxdelay, nsrc, qqi; 788 int is_general_query; 789 uint16_t timer; 790 uint8_t qrv; 791 #ifdef KTR 792 char ip6tbuf[INET6_ADDRSTRLEN]; 793 #endif 794 795 NET_EPOCH_ASSERT(); 796 797 if (!mld_v2enable) { 798 CTR3(KTR_MLD, "ignore v2 query src %s on ifp %p(%s)", 799 ip6_sprintf(ip6tbuf, &ip6->ip6_src), 800 ifp, if_name(ifp)); 801 return (0); 802 } 803 804 /* 805 * RFC3810 Section 6.2: MLD queries must originate from 806 * a router's link-local address. 807 */ 808 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) { 809 CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)", 810 ip6_sprintf(ip6tbuf, &ip6->ip6_src), 811 ifp, if_name(ifp)); 812 return (0); 813 } 814 815 is_general_query = 0; 816 817 CTR2(KTR_MLD, "input v2 query on ifp %p(%s)", ifp, if_name(ifp)); 818 819 maxdelay = ntohs(mld->mld_maxdelay); /* in 1/10ths of a second */ 820 if (maxdelay >= 32768) { 821 maxdelay = (MLD_MRC_MANT(maxdelay) | 0x1000) << 822 (MLD_MRC_EXP(maxdelay) + 3); 823 } 824 timer = (maxdelay * MLD_FASTHZ) / MLD_TIMER_SCALE; 825 if (timer == 0) 826 timer = 1; 827 828 qrv = MLD_QRV(mld->mld_misc); 829 if (qrv < 2) { 830 CTR3(KTR_MLD, "%s: clamping qrv %d to %d", __func__, 831 qrv, MLD_RV_INIT); 832 qrv = MLD_RV_INIT; 833 } 834 835 qqi = mld->mld_qqi; 836 if (qqi >= 128) { 837 qqi = MLD_QQIC_MANT(mld->mld_qqi) << 838 (MLD_QQIC_EXP(mld->mld_qqi) + 3); 839 } 840 841 nsrc = ntohs(mld->mld_numsrc); 842 if (nsrc > MLD_MAX_GS_SOURCES) 843 return (EMSGSIZE); 844 if (icmp6len < sizeof(struct mldv2_query) + 845 (nsrc * sizeof(struct in6_addr))) 846 return (EMSGSIZE); 847 848 /* 849 * Do further input validation upfront to avoid resetting timers 850 * should we need to discard this query. 851 */ 852 if (IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr)) { 853 /* 854 * A general query with a source list has undefined 855 * behaviour; discard it. 856 */ 857 if (nsrc > 0) 858 return (EINVAL); 859 is_general_query = 1; 860 } else { 861 /* 862 * Embed scope ID of receiving interface in MLD query for 863 * lookup whilst we don't hold other locks (due to KAME 864 * locking lameness). We own this mbuf chain just now. 865 */ 866 in6_setscope(&mld->mld_addr, ifp, NULL); 867 } 868 869 IN6_MULTI_LIST_LOCK(); 870 MLD_LOCK(); 871 872 mli = MLD_IFINFO(ifp); 873 KASSERT(mli != NULL, ("%s: no mld_ifsoftc for ifp %p", __func__, ifp)); 874 875 /* 876 * Discard the v2 query if we're in Compatibility Mode. 877 * The RFC is pretty clear that hosts need to stay in MLDv1 mode 878 * until the Old Version Querier Present timer expires. 879 */ 880 if (mli->mli_version != MLD_VERSION_2) 881 goto out_locked; 882 883 mld_set_version(mli, MLD_VERSION_2); 884 mli->mli_rv = qrv; 885 mli->mli_qi = qqi; 886 mli->mli_qri = maxdelay; 887 888 CTR4(KTR_MLD, "%s: qrv %d qi %d maxdelay %d", __func__, qrv, qqi, 889 maxdelay); 890 891 if (is_general_query) { 892 /* 893 * MLDv2 General Query. 894 * 895 * Schedule a current-state report on this ifp for 896 * all groups, possibly containing source lists. 897 * 898 * If there is a pending General Query response 899 * scheduled earlier than the selected delay, do 900 * not schedule any other reports. 901 * Otherwise, reset the interface timer. 902 */ 903 CTR2(KTR_MLD, "process v2 general query on ifp %p(%s)", 904 ifp, if_name(ifp)); 905 if (mli->mli_v2_timer == 0 || mli->mli_v2_timer >= timer) { 906 mli->mli_v2_timer = MLD_RANDOM_DELAY(timer); 907 V_interface_timers_running6 = 1; 908 } 909 } else { 910 /* 911 * MLDv2 Group-specific or Group-and-source-specific Query. 912 * 913 * Group-source-specific queries are throttled on 914 * a per-group basis to defeat denial-of-service attempts. 915 * Queries for groups we are not a member of on this 916 * link are simply ignored. 917 */ 918 inm = in6m_lookup_locked(ifp, &mld->mld_addr); 919 if (inm == NULL) 920 goto out_locked; 921 if (nsrc > 0) { 922 if (!ratecheck(&inm->in6m_lastgsrtv, 923 &V_mld_gsrdelay)) { 924 CTR1(KTR_MLD, "%s: GS query throttled.", 925 __func__); 926 goto out_locked; 927 } 928 } 929 CTR2(KTR_MLD, "process v2 group query on ifp %p(%s)", 930 ifp, if_name(ifp)); 931 /* 932 * If there is a pending General Query response 933 * scheduled sooner than the selected delay, no 934 * further report need be scheduled. 935 * Otherwise, prepare to respond to the 936 * group-specific or group-and-source query. 937 */ 938 if (mli->mli_v2_timer == 0 || mli->mli_v2_timer >= timer) 939 mld_v2_process_group_query(inm, mli, timer, m, mld, off); 940 941 /* XXX Clear embedded scope ID as userland won't expect it. */ 942 in6_clearscope(&mld->mld_addr); 943 } 944 945 out_locked: 946 MLD_UNLOCK(); 947 IN6_MULTI_LIST_UNLOCK(); 948 949 return (0); 950 } 951 952 /* 953 * Process a received MLDv2 group-specific or group-and-source-specific 954 * query. 955 * Return <0 if any error occurred. Currently this is ignored. 956 */ 957 static int 958 mld_v2_process_group_query(struct in6_multi *inm, struct mld_ifsoftc *mli, 959 int timer, struct mbuf *m0, struct mldv2_query *mld, const int off) 960 { 961 int retval; 962 uint16_t nsrc; 963 964 IN6_MULTI_LIST_LOCK_ASSERT(); 965 MLD_LOCK_ASSERT(); 966 967 retval = 0; 968 969 switch (inm->in6m_state) { 970 case MLD_NOT_MEMBER: 971 case MLD_SILENT_MEMBER: 972 case MLD_SLEEPING_MEMBER: 973 case MLD_LAZY_MEMBER: 974 case MLD_AWAKENING_MEMBER: 975 case MLD_IDLE_MEMBER: 976 case MLD_LEAVING_MEMBER: 977 return (retval); 978 break; 979 case MLD_REPORTING_MEMBER: 980 case MLD_G_QUERY_PENDING_MEMBER: 981 case MLD_SG_QUERY_PENDING_MEMBER: 982 break; 983 } 984 985 nsrc = ntohs(mld->mld_numsrc); 986 987 /* Length should be checked by calling function. */ 988 KASSERT((m0->m_flags & M_PKTHDR) == 0 || 989 m0->m_pkthdr.len >= off + sizeof(struct mldv2_query) + 990 nsrc * sizeof(struct in6_addr), 991 ("mldv2 packet is too short: (%d bytes < %zd bytes, m=%p)", 992 m0->m_pkthdr.len, off + sizeof(struct mldv2_query) + 993 nsrc * sizeof(struct in6_addr), m0)); 994 995 /* 996 * Deal with group-specific queries upfront. 997 * If any group query is already pending, purge any recorded 998 * source-list state if it exists, and schedule a query response 999 * for this group-specific query. 1000 */ 1001 if (nsrc == 0) { 1002 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER || 1003 inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER) { 1004 in6m_clear_recorded(inm); 1005 timer = min(inm->in6m_timer, timer); 1006 } 1007 inm->in6m_state = MLD_G_QUERY_PENDING_MEMBER; 1008 inm->in6m_timer = MLD_RANDOM_DELAY(timer); 1009 V_current_state_timers_running6 = 1; 1010 return (retval); 1011 } 1012 1013 /* 1014 * Deal with the case where a group-and-source-specific query has 1015 * been received but a group-specific query is already pending. 1016 */ 1017 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER) { 1018 timer = min(inm->in6m_timer, timer); 1019 inm->in6m_timer = MLD_RANDOM_DELAY(timer); 1020 V_current_state_timers_running6 = 1; 1021 return (retval); 1022 } 1023 1024 /* 1025 * Finally, deal with the case where a group-and-source-specific 1026 * query has been received, where a response to a previous g-s-r 1027 * query exists, or none exists. 1028 * In this case, we need to parse the source-list which the Querier 1029 * has provided us with and check if we have any source list filter 1030 * entries at T1 for these sources. If we do not, there is no need 1031 * schedule a report and the query may be dropped. 1032 * If we do, we must record them and schedule a current-state 1033 * report for those sources. 1034 */ 1035 if (inm->in6m_nsrc > 0) { 1036 struct in6_addr srcaddr; 1037 int i, nrecorded; 1038 int soff; 1039 1040 soff = off + sizeof(struct mldv2_query); 1041 nrecorded = 0; 1042 for (i = 0; i < nsrc; i++) { 1043 m_copydata(m0, soff, sizeof(struct in6_addr), 1044 (caddr_t)&srcaddr); 1045 retval = in6m_record_source(inm, &srcaddr); 1046 if (retval < 0) 1047 break; 1048 nrecorded += retval; 1049 soff += sizeof(struct in6_addr); 1050 } 1051 if (nrecorded > 0) { 1052 CTR1(KTR_MLD, 1053 "%s: schedule response to SG query", __func__); 1054 inm->in6m_state = MLD_SG_QUERY_PENDING_MEMBER; 1055 inm->in6m_timer = MLD_RANDOM_DELAY(timer); 1056 V_current_state_timers_running6 = 1; 1057 } 1058 } 1059 1060 return (retval); 1061 } 1062 1063 /* 1064 * Process a received MLDv1 host membership report. 1065 * Assumes mld points to mld_hdr in pulled up mbuf chain. 1066 * 1067 * NOTE: Can't be fully const correct as we temporarily embed scope ID in 1068 * mld_addr. This is OK as we own the mbuf chain. 1069 */ 1070 static int 1071 mld_v1_input_report(struct ifnet *ifp, const struct ip6_hdr *ip6, 1072 /*const*/ struct mld_hdr *mld) 1073 { 1074 struct in6_addr src, dst; 1075 struct in6_ifaddr *ia; 1076 struct in6_multi *inm; 1077 #ifdef KTR 1078 char ip6tbuf[INET6_ADDRSTRLEN]; 1079 #endif 1080 1081 NET_EPOCH_ASSERT(); 1082 1083 if (!mld_v1enable) { 1084 CTR3(KTR_MLD, "ignore v1 report %s on ifp %p(%s)", 1085 ip6_sprintf(ip6tbuf, &mld->mld_addr), 1086 ifp, if_name(ifp)); 1087 return (0); 1088 } 1089 1090 if (ifp->if_flags & IFF_LOOPBACK) 1091 return (0); 1092 1093 /* 1094 * MLDv1 reports must originate from a host's link-local address, 1095 * or the unspecified address (when booting). 1096 */ 1097 src = ip6->ip6_src; 1098 in6_clearscope(&src); 1099 if (!IN6_IS_SCOPE_LINKLOCAL(&src) && !IN6_IS_ADDR_UNSPECIFIED(&src)) { 1100 CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)", 1101 ip6_sprintf(ip6tbuf, &ip6->ip6_src), 1102 ifp, if_name(ifp)); 1103 return (EINVAL); 1104 } 1105 1106 /* 1107 * RFC2710 Section 4: MLDv1 reports must pertain to a multicast 1108 * group, and must be directed to the group itself. 1109 */ 1110 dst = ip6->ip6_dst; 1111 in6_clearscope(&dst); 1112 if (!IN6_IS_ADDR_MULTICAST(&mld->mld_addr) || 1113 !IN6_ARE_ADDR_EQUAL(&mld->mld_addr, &dst)) { 1114 CTR3(KTR_MLD, "ignore v1 query dst %s on ifp %p(%s)", 1115 ip6_sprintf(ip6tbuf, &ip6->ip6_dst), 1116 ifp, if_name(ifp)); 1117 return (EINVAL); 1118 } 1119 1120 /* 1121 * Make sure we don't hear our own membership report, as fast 1122 * leave requires knowing that we are the only member of a 1123 * group. Assume we used the link-local address if available, 1124 * otherwise look for ::. 1125 * 1126 * XXX Note that scope ID comparison is needed for the address 1127 * returned by in6ifa_ifpforlinklocal(), but SHOULD NOT be 1128 * performed for the on-wire address. 1129 */ 1130 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST); 1131 if ((ia && IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, IA6_IN6(ia))) || 1132 (ia == NULL && IN6_IS_ADDR_UNSPECIFIED(&src))) { 1133 if (ia != NULL) 1134 ifa_free(&ia->ia_ifa); 1135 return (0); 1136 } 1137 if (ia != NULL) 1138 ifa_free(&ia->ia_ifa); 1139 1140 CTR3(KTR_MLD, "process v1 report %s on ifp %p(%s)", 1141 ip6_sprintf(ip6tbuf, &mld->mld_addr), ifp, if_name(ifp)); 1142 1143 /* 1144 * Embed scope ID of receiving interface in MLD query for lookup 1145 * whilst we don't hold other locks (due to KAME locking lameness). 1146 */ 1147 if (!IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr)) 1148 in6_setscope(&mld->mld_addr, ifp, NULL); 1149 1150 IN6_MULTI_LIST_LOCK(); 1151 MLD_LOCK(); 1152 1153 /* 1154 * MLDv1 report suppression. 1155 * If we are a member of this group, and our membership should be 1156 * reported, and our group timer is pending or about to be reset, 1157 * stop our group timer by transitioning to the 'lazy' state. 1158 */ 1159 inm = in6m_lookup_locked(ifp, &mld->mld_addr); 1160 if (inm != NULL) { 1161 struct mld_ifsoftc *mli; 1162 1163 mli = inm->in6m_mli; 1164 KASSERT(mli != NULL, 1165 ("%s: no mli for ifp %p", __func__, ifp)); 1166 1167 /* 1168 * If we are in MLDv2 host mode, do not allow the 1169 * other host's MLDv1 report to suppress our reports. 1170 */ 1171 if (mli->mli_version == MLD_VERSION_2) 1172 goto out_locked; 1173 1174 inm->in6m_timer = 0; 1175 1176 switch (inm->in6m_state) { 1177 case MLD_NOT_MEMBER: 1178 case MLD_SILENT_MEMBER: 1179 case MLD_SLEEPING_MEMBER: 1180 break; 1181 case MLD_REPORTING_MEMBER: 1182 case MLD_IDLE_MEMBER: 1183 case MLD_AWAKENING_MEMBER: 1184 CTR3(KTR_MLD, 1185 "report suppressed for %s on ifp %p(%s)", 1186 ip6_sprintf(ip6tbuf, &mld->mld_addr), 1187 ifp, if_name(ifp)); 1188 case MLD_LAZY_MEMBER: 1189 inm->in6m_state = MLD_LAZY_MEMBER; 1190 break; 1191 case MLD_G_QUERY_PENDING_MEMBER: 1192 case MLD_SG_QUERY_PENDING_MEMBER: 1193 case MLD_LEAVING_MEMBER: 1194 break; 1195 } 1196 } 1197 1198 out_locked: 1199 MLD_UNLOCK(); 1200 IN6_MULTI_LIST_UNLOCK(); 1201 1202 /* XXX Clear embedded scope ID as userland won't expect it. */ 1203 in6_clearscope(&mld->mld_addr); 1204 1205 return (0); 1206 } 1207 1208 /* 1209 * MLD input path. 1210 * 1211 * Assume query messages which fit in a single ICMPv6 message header 1212 * have been pulled up. 1213 * Assume that userland will want to see the message, even if it 1214 * otherwise fails kernel input validation; do not free it. 1215 * Pullup may however free the mbuf chain m if it fails. 1216 * 1217 * Return IPPROTO_DONE if we freed m. Otherwise, return 0. 1218 */ 1219 int 1220 mld_input(struct mbuf **mp, int off, int icmp6len) 1221 { 1222 struct ifnet *ifp; 1223 struct ip6_hdr *ip6; 1224 struct mbuf *m; 1225 struct mld_hdr *mld; 1226 int mldlen; 1227 1228 m = *mp; 1229 CTR3(KTR_MLD, "%s: called w/mbuf (%p,%d)", __func__, m, off); 1230 1231 ifp = m->m_pkthdr.rcvif; 1232 1233 /* Pullup to appropriate size. */ 1234 if (m->m_len < off + sizeof(*mld)) { 1235 m = m_pullup(m, off + sizeof(*mld)); 1236 if (m == NULL) { 1237 ICMP6STAT_INC(icp6s_badlen); 1238 return (IPPROTO_DONE); 1239 } 1240 } 1241 mld = (struct mld_hdr *)(mtod(m, uint8_t *) + off); 1242 if (mld->mld_type == MLD_LISTENER_QUERY && 1243 icmp6len >= sizeof(struct mldv2_query)) { 1244 mldlen = sizeof(struct mldv2_query); 1245 } else { 1246 mldlen = sizeof(struct mld_hdr); 1247 } 1248 if (m->m_len < off + mldlen) { 1249 m = m_pullup(m, off + mldlen); 1250 if (m == NULL) { 1251 ICMP6STAT_INC(icp6s_badlen); 1252 return (IPPROTO_DONE); 1253 } 1254 } 1255 *mp = m; 1256 ip6 = mtod(m, struct ip6_hdr *); 1257 mld = (struct mld_hdr *)(mtod(m, uint8_t *) + off); 1258 1259 /* 1260 * Userland needs to see all of this traffic for implementing 1261 * the endpoint discovery portion of multicast routing. 1262 */ 1263 switch (mld->mld_type) { 1264 case MLD_LISTENER_QUERY: 1265 icmp6_ifstat_inc(ifp, ifs6_in_mldquery); 1266 if (icmp6len == sizeof(struct mld_hdr)) { 1267 if (mld_v1_input_query(ifp, ip6, mld) != 0) 1268 return (0); 1269 } else if (icmp6len >= sizeof(struct mldv2_query)) { 1270 if (mld_v2_input_query(ifp, ip6, m, 1271 (struct mldv2_query *)mld, off, icmp6len) != 0) 1272 return (0); 1273 } 1274 break; 1275 case MLD_LISTENER_REPORT: 1276 icmp6_ifstat_inc(ifp, ifs6_in_mldreport); 1277 if (mld_v1_input_report(ifp, ip6, mld) != 0) 1278 return (0); 1279 break; 1280 case MLDV2_LISTENER_REPORT: 1281 icmp6_ifstat_inc(ifp, ifs6_in_mldreport); 1282 break; 1283 case MLD_LISTENER_DONE: 1284 icmp6_ifstat_inc(ifp, ifs6_in_mlddone); 1285 break; 1286 default: 1287 break; 1288 } 1289 1290 return (0); 1291 } 1292 1293 /* 1294 * Fast timeout handler (global). 1295 * VIMAGE: Timeout handlers are expected to service all vimages. 1296 */ 1297 static struct callout mldfast_callout; 1298 static void 1299 mld_fasttimo(void *arg __unused) 1300 { 1301 struct epoch_tracker et; 1302 struct in6_multi_head inmh; 1303 VNET_ITERATOR_DECL(vnet_iter); 1304 1305 SLIST_INIT(&inmh); 1306 1307 NET_EPOCH_ENTER(et); 1308 VNET_LIST_RLOCK_NOSLEEP(); 1309 VNET_FOREACH(vnet_iter) { 1310 CURVNET_SET(vnet_iter); 1311 mld_fasttimo_vnet(&inmh); 1312 CURVNET_RESTORE(); 1313 } 1314 VNET_LIST_RUNLOCK_NOSLEEP(); 1315 NET_EPOCH_EXIT(et); 1316 in6m_release_list_deferred(&inmh); 1317 1318 callout_reset(&mldfast_callout, hz / MLD_FASTHZ, mld_fasttimo, NULL); 1319 } 1320 1321 /* 1322 * Fast timeout handler (per-vnet). 1323 * 1324 * VIMAGE: Assume caller has set up our curvnet. 1325 */ 1326 static void 1327 mld_fasttimo_vnet(struct in6_multi_head *inmh) 1328 { 1329 struct mbufq scq; /* State-change packets */ 1330 struct mbufq qrq; /* Query response packets */ 1331 struct ifnet *ifp; 1332 struct mld_ifsoftc *mli; 1333 struct ifmultiaddr *ifma; 1334 struct in6_multi *inm; 1335 int uri_fasthz; 1336 1337 uri_fasthz = 0; 1338 1339 /* 1340 * Quick check to see if any work needs to be done, in order to 1341 * minimize the overhead of fasttimo processing. 1342 * SMPng: XXX Unlocked reads. 1343 */ 1344 if (!V_current_state_timers_running6 && 1345 !V_interface_timers_running6 && 1346 !V_state_change_timers_running6) 1347 return; 1348 1349 IN6_MULTI_LIST_LOCK(); 1350 MLD_LOCK(); 1351 1352 /* 1353 * MLDv2 General Query response timer processing. 1354 */ 1355 if (V_interface_timers_running6) { 1356 CTR1(KTR_MLD, "%s: interface timers running", __func__); 1357 1358 V_interface_timers_running6 = 0; 1359 LIST_FOREACH(mli, &V_mli_head, mli_link) { 1360 if (mli->mli_v2_timer == 0) { 1361 /* Do nothing. */ 1362 } else if (--mli->mli_v2_timer == 0) { 1363 mld_v2_dispatch_general_query(mli); 1364 } else { 1365 V_interface_timers_running6 = 1; 1366 } 1367 } 1368 } 1369 1370 if (!V_current_state_timers_running6 && 1371 !V_state_change_timers_running6) 1372 goto out_locked; 1373 1374 V_current_state_timers_running6 = 0; 1375 V_state_change_timers_running6 = 0; 1376 1377 CTR1(KTR_MLD, "%s: state change timers running", __func__); 1378 1379 /* 1380 * MLD host report and state-change timer processing. 1381 * Note: Processing a v2 group timer may remove a node. 1382 */ 1383 LIST_FOREACH(mli, &V_mli_head, mli_link) { 1384 ifp = mli->mli_ifp; 1385 1386 if (mli->mli_version == MLD_VERSION_2) { 1387 uri_fasthz = MLD_RANDOM_DELAY(mli->mli_uri * 1388 MLD_FASTHZ); 1389 mbufq_init(&qrq, MLD_MAX_G_GS_PACKETS); 1390 mbufq_init(&scq, MLD_MAX_STATE_CHANGE_PACKETS); 1391 } 1392 1393 IF_ADDR_WLOCK(ifp); 1394 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1395 inm = in6m_ifmultiaddr_get_inm(ifma); 1396 if (inm == NULL) 1397 continue; 1398 switch (mli->mli_version) { 1399 case MLD_VERSION_1: 1400 mld_v1_process_group_timer(inmh, inm); 1401 break; 1402 case MLD_VERSION_2: 1403 mld_v2_process_group_timers(inmh, &qrq, 1404 &scq, inm, uri_fasthz); 1405 break; 1406 } 1407 } 1408 IF_ADDR_WUNLOCK(ifp); 1409 1410 switch (mli->mli_version) { 1411 case MLD_VERSION_1: 1412 /* 1413 * Transmit reports for this lifecycle. This 1414 * is done while not holding IF_ADDR_LOCK 1415 * since this can call 1416 * in6ifa_ifpforlinklocal() which locks 1417 * IF_ADDR_LOCK internally as well as 1418 * ip6_output() to transmit a packet. 1419 */ 1420 while ((inm = SLIST_FIRST(inmh)) != NULL) { 1421 SLIST_REMOVE_HEAD(inmh, in6m_defer); 1422 (void)mld_v1_transmit_report(inm, 1423 MLD_LISTENER_REPORT); 1424 } 1425 break; 1426 case MLD_VERSION_2: 1427 mld_dispatch_queue(&qrq, 0); 1428 mld_dispatch_queue(&scq, 0); 1429 break; 1430 } 1431 } 1432 1433 out_locked: 1434 MLD_UNLOCK(); 1435 IN6_MULTI_LIST_UNLOCK(); 1436 } 1437 1438 /* 1439 * Update host report group timer. 1440 * Will update the global pending timer flags. 1441 */ 1442 static void 1443 mld_v1_process_group_timer(struct in6_multi_head *inmh, struct in6_multi *inm) 1444 { 1445 int report_timer_expired; 1446 1447 IN6_MULTI_LIST_LOCK_ASSERT(); 1448 MLD_LOCK_ASSERT(); 1449 1450 if (inm->in6m_timer == 0) { 1451 report_timer_expired = 0; 1452 } else if (--inm->in6m_timer == 0) { 1453 report_timer_expired = 1; 1454 } else { 1455 V_current_state_timers_running6 = 1; 1456 return; 1457 } 1458 1459 switch (inm->in6m_state) { 1460 case MLD_NOT_MEMBER: 1461 case MLD_SILENT_MEMBER: 1462 case MLD_IDLE_MEMBER: 1463 case MLD_LAZY_MEMBER: 1464 case MLD_SLEEPING_MEMBER: 1465 case MLD_AWAKENING_MEMBER: 1466 break; 1467 case MLD_REPORTING_MEMBER: 1468 if (report_timer_expired) { 1469 inm->in6m_state = MLD_IDLE_MEMBER; 1470 SLIST_INSERT_HEAD(inmh, inm, in6m_defer); 1471 } 1472 break; 1473 case MLD_G_QUERY_PENDING_MEMBER: 1474 case MLD_SG_QUERY_PENDING_MEMBER: 1475 case MLD_LEAVING_MEMBER: 1476 break; 1477 } 1478 } 1479 1480 /* 1481 * Update a group's timers for MLDv2. 1482 * Will update the global pending timer flags. 1483 * Note: Unlocked read from mli. 1484 */ 1485 static void 1486 mld_v2_process_group_timers(struct in6_multi_head *inmh, 1487 struct mbufq *qrq, struct mbufq *scq, 1488 struct in6_multi *inm, const int uri_fasthz) 1489 { 1490 int query_response_timer_expired; 1491 int state_change_retransmit_timer_expired; 1492 #ifdef KTR 1493 char ip6tbuf[INET6_ADDRSTRLEN]; 1494 #endif 1495 1496 IN6_MULTI_LIST_LOCK_ASSERT(); 1497 MLD_LOCK_ASSERT(); 1498 1499 query_response_timer_expired = 0; 1500 state_change_retransmit_timer_expired = 0; 1501 1502 /* 1503 * During a transition from compatibility mode back to MLDv2, 1504 * a group record in REPORTING state may still have its group 1505 * timer active. This is a no-op in this function; it is easier 1506 * to deal with it here than to complicate the slow-timeout path. 1507 */ 1508 if (inm->in6m_timer == 0) { 1509 query_response_timer_expired = 0; 1510 } else if (--inm->in6m_timer == 0) { 1511 query_response_timer_expired = 1; 1512 } else { 1513 V_current_state_timers_running6 = 1; 1514 } 1515 1516 if (inm->in6m_sctimer == 0) { 1517 state_change_retransmit_timer_expired = 0; 1518 } else if (--inm->in6m_sctimer == 0) { 1519 state_change_retransmit_timer_expired = 1; 1520 } else { 1521 V_state_change_timers_running6 = 1; 1522 } 1523 1524 /* We are in fasttimo, so be quick about it. */ 1525 if (!state_change_retransmit_timer_expired && 1526 !query_response_timer_expired) 1527 return; 1528 1529 switch (inm->in6m_state) { 1530 case MLD_NOT_MEMBER: 1531 case MLD_SILENT_MEMBER: 1532 case MLD_SLEEPING_MEMBER: 1533 case MLD_LAZY_MEMBER: 1534 case MLD_AWAKENING_MEMBER: 1535 case MLD_IDLE_MEMBER: 1536 break; 1537 case MLD_G_QUERY_PENDING_MEMBER: 1538 case MLD_SG_QUERY_PENDING_MEMBER: 1539 /* 1540 * Respond to a previously pending Group-Specific 1541 * or Group-and-Source-Specific query by enqueueing 1542 * the appropriate Current-State report for 1543 * immediate transmission. 1544 */ 1545 if (query_response_timer_expired) { 1546 int retval __unused; 1547 1548 retval = mld_v2_enqueue_group_record(qrq, inm, 0, 1, 1549 (inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER), 1550 0); 1551 CTR2(KTR_MLD, "%s: enqueue record = %d", 1552 __func__, retval); 1553 inm->in6m_state = MLD_REPORTING_MEMBER; 1554 in6m_clear_recorded(inm); 1555 } 1556 /* FALLTHROUGH */ 1557 case MLD_REPORTING_MEMBER: 1558 case MLD_LEAVING_MEMBER: 1559 if (state_change_retransmit_timer_expired) { 1560 /* 1561 * State-change retransmission timer fired. 1562 * If there are any further pending retransmissions, 1563 * set the global pending state-change flag, and 1564 * reset the timer. 1565 */ 1566 if (--inm->in6m_scrv > 0) { 1567 inm->in6m_sctimer = uri_fasthz; 1568 V_state_change_timers_running6 = 1; 1569 } 1570 /* 1571 * Retransmit the previously computed state-change 1572 * report. If there are no further pending 1573 * retransmissions, the mbuf queue will be consumed. 1574 * Update T0 state to T1 as we have now sent 1575 * a state-change. 1576 */ 1577 (void)mld_v2_merge_state_changes(inm, scq); 1578 1579 in6m_commit(inm); 1580 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__, 1581 ip6_sprintf(ip6tbuf, &inm->in6m_addr), 1582 if_name(inm->in6m_ifp)); 1583 1584 /* 1585 * If we are leaving the group for good, make sure 1586 * we release MLD's reference to it. 1587 * This release must be deferred using a SLIST, 1588 * as we are called from a loop which traverses 1589 * the in_ifmultiaddr TAILQ. 1590 */ 1591 if (inm->in6m_state == MLD_LEAVING_MEMBER && 1592 inm->in6m_scrv == 0) { 1593 inm->in6m_state = MLD_NOT_MEMBER; 1594 in6m_disconnect_locked(inmh, inm); 1595 in6m_rele_locked(inmh, inm); 1596 } 1597 } 1598 break; 1599 } 1600 } 1601 1602 /* 1603 * Switch to a different version on the given interface, 1604 * as per Section 9.12. 1605 */ 1606 static void 1607 mld_set_version(struct mld_ifsoftc *mli, const int version) 1608 { 1609 int old_version_timer; 1610 1611 MLD_LOCK_ASSERT(); 1612 1613 CTR4(KTR_MLD, "%s: switching to v%d on ifp %p(%s)", __func__, 1614 version, mli->mli_ifp, if_name(mli->mli_ifp)); 1615 1616 if (version == MLD_VERSION_1) { 1617 /* 1618 * Compute the "Older Version Querier Present" timer as per 1619 * Section 9.12. 1620 */ 1621 old_version_timer = (mli->mli_rv * mli->mli_qi) + mli->mli_qri; 1622 old_version_timer *= MLD_SLOWHZ; 1623 mli->mli_v1_timer = old_version_timer; 1624 } 1625 1626 if (mli->mli_v1_timer > 0 && mli->mli_version != MLD_VERSION_1) { 1627 mli->mli_version = MLD_VERSION_1; 1628 mld_v2_cancel_link_timers(mli); 1629 } 1630 } 1631 1632 /* 1633 * Cancel pending MLDv2 timers for the given link and all groups 1634 * joined on it; state-change, general-query, and group-query timers. 1635 */ 1636 static void 1637 mld_v2_cancel_link_timers(struct mld_ifsoftc *mli) 1638 { 1639 struct epoch_tracker et; 1640 struct in6_multi_head inmh; 1641 struct ifmultiaddr *ifma; 1642 struct ifnet *ifp; 1643 struct in6_multi *inm; 1644 1645 CTR3(KTR_MLD, "%s: cancel v2 timers on ifp %p(%s)", __func__, 1646 mli->mli_ifp, if_name(mli->mli_ifp)); 1647 1648 SLIST_INIT(&inmh); 1649 IN6_MULTI_LIST_LOCK_ASSERT(); 1650 MLD_LOCK_ASSERT(); 1651 1652 /* 1653 * Fast-track this potentially expensive operation 1654 * by checking all the global 'timer pending' flags. 1655 */ 1656 if (!V_interface_timers_running6 && 1657 !V_state_change_timers_running6 && 1658 !V_current_state_timers_running6) 1659 return; 1660 1661 mli->mli_v2_timer = 0; 1662 1663 ifp = mli->mli_ifp; 1664 1665 IF_ADDR_WLOCK(ifp); 1666 NET_EPOCH_ENTER(et); 1667 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1668 inm = in6m_ifmultiaddr_get_inm(ifma); 1669 if (inm == NULL) 1670 continue; 1671 switch (inm->in6m_state) { 1672 case MLD_NOT_MEMBER: 1673 case MLD_SILENT_MEMBER: 1674 case MLD_IDLE_MEMBER: 1675 case MLD_LAZY_MEMBER: 1676 case MLD_SLEEPING_MEMBER: 1677 case MLD_AWAKENING_MEMBER: 1678 break; 1679 case MLD_LEAVING_MEMBER: 1680 /* 1681 * If we are leaving the group and switching 1682 * version, we need to release the final 1683 * reference held for issuing the INCLUDE {}. 1684 */ 1685 if (inm->in6m_refcount == 1) 1686 in6m_disconnect_locked(&inmh, inm); 1687 in6m_rele_locked(&inmh, inm); 1688 /* FALLTHROUGH */ 1689 case MLD_G_QUERY_PENDING_MEMBER: 1690 case MLD_SG_QUERY_PENDING_MEMBER: 1691 in6m_clear_recorded(inm); 1692 /* FALLTHROUGH */ 1693 case MLD_REPORTING_MEMBER: 1694 inm->in6m_sctimer = 0; 1695 inm->in6m_timer = 0; 1696 inm->in6m_state = MLD_REPORTING_MEMBER; 1697 /* 1698 * Free any pending MLDv2 state-change records. 1699 */ 1700 mbufq_drain(&inm->in6m_scq); 1701 break; 1702 } 1703 } 1704 NET_EPOCH_EXIT(et); 1705 IF_ADDR_WUNLOCK(ifp); 1706 in6m_release_list_deferred(&inmh); 1707 } 1708 1709 /* 1710 * Global slowtimo handler. 1711 * VIMAGE: Timeout handlers are expected to service all vimages. 1712 */ 1713 static struct callout mldslow_callout; 1714 static void 1715 mld_slowtimo(void *arg __unused) 1716 { 1717 VNET_ITERATOR_DECL(vnet_iter); 1718 1719 VNET_LIST_RLOCK_NOSLEEP(); 1720 VNET_FOREACH(vnet_iter) { 1721 CURVNET_SET(vnet_iter); 1722 mld_slowtimo_vnet(); 1723 CURVNET_RESTORE(); 1724 } 1725 VNET_LIST_RUNLOCK_NOSLEEP(); 1726 1727 callout_reset(&mldslow_callout, hz / MLD_SLOWHZ, mld_slowtimo, NULL); 1728 } 1729 1730 /* 1731 * Per-vnet slowtimo handler. 1732 */ 1733 static void 1734 mld_slowtimo_vnet(void) 1735 { 1736 struct mld_ifsoftc *mli; 1737 1738 MLD_LOCK(); 1739 1740 LIST_FOREACH(mli, &V_mli_head, mli_link) { 1741 mld_v1_process_querier_timers(mli); 1742 } 1743 1744 MLD_UNLOCK(); 1745 } 1746 1747 /* 1748 * Update the Older Version Querier Present timers for a link. 1749 * See Section 9.12 of RFC 3810. 1750 */ 1751 static void 1752 mld_v1_process_querier_timers(struct mld_ifsoftc *mli) 1753 { 1754 1755 MLD_LOCK_ASSERT(); 1756 1757 if (mli->mli_version != MLD_VERSION_2 && --mli->mli_v1_timer == 0) { 1758 /* 1759 * MLDv1 Querier Present timer expired; revert to MLDv2. 1760 */ 1761 CTR5(KTR_MLD, 1762 "%s: transition from v%d -> v%d on %p(%s)", 1763 __func__, mli->mli_version, MLD_VERSION_2, 1764 mli->mli_ifp, if_name(mli->mli_ifp)); 1765 mli->mli_version = MLD_VERSION_2; 1766 } 1767 } 1768 1769 /* 1770 * Transmit an MLDv1 report immediately. 1771 */ 1772 static int 1773 mld_v1_transmit_report(struct in6_multi *in6m, const int type) 1774 { 1775 struct ifnet *ifp; 1776 struct in6_ifaddr *ia; 1777 struct ip6_hdr *ip6; 1778 struct mbuf *mh, *md; 1779 struct mld_hdr *mld; 1780 1781 NET_EPOCH_ASSERT(); 1782 IN6_MULTI_LIST_LOCK_ASSERT(); 1783 MLD_LOCK_ASSERT(); 1784 1785 ifp = in6m->in6m_ifp; 1786 /* in process of being freed */ 1787 if (ifp == NULL) 1788 return (0); 1789 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST); 1790 /* ia may be NULL if link-local address is tentative. */ 1791 1792 mh = m_gethdr(M_NOWAIT, MT_DATA); 1793 if (mh == NULL) { 1794 if (ia != NULL) 1795 ifa_free(&ia->ia_ifa); 1796 return (ENOMEM); 1797 } 1798 md = m_get(M_NOWAIT, MT_DATA); 1799 if (md == NULL) { 1800 m_free(mh); 1801 if (ia != NULL) 1802 ifa_free(&ia->ia_ifa); 1803 return (ENOMEM); 1804 } 1805 mh->m_next = md; 1806 1807 /* 1808 * FUTURE: Consider increasing alignment by ETHER_HDR_LEN, so 1809 * that ether_output() does not need to allocate another mbuf 1810 * for the header in the most common case. 1811 */ 1812 M_ALIGN(mh, sizeof(struct ip6_hdr)); 1813 mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld_hdr); 1814 mh->m_len = sizeof(struct ip6_hdr); 1815 1816 ip6 = mtod(mh, struct ip6_hdr *); 1817 ip6->ip6_flow = 0; 1818 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 1819 ip6->ip6_vfc |= IPV6_VERSION; 1820 ip6->ip6_nxt = IPPROTO_ICMPV6; 1821 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any; 1822 ip6->ip6_dst = in6m->in6m_addr; 1823 1824 md->m_len = sizeof(struct mld_hdr); 1825 mld = mtod(md, struct mld_hdr *); 1826 mld->mld_type = type; 1827 mld->mld_code = 0; 1828 mld->mld_cksum = 0; 1829 mld->mld_maxdelay = 0; 1830 mld->mld_reserved = 0; 1831 mld->mld_addr = in6m->in6m_addr; 1832 in6_clearscope(&mld->mld_addr); 1833 mld->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6, 1834 sizeof(struct ip6_hdr), sizeof(struct mld_hdr)); 1835 1836 mld_save_context(mh, ifp); 1837 mh->m_flags |= M_MLDV1; 1838 1839 mld_dispatch_packet(mh); 1840 1841 if (ia != NULL) 1842 ifa_free(&ia->ia_ifa); 1843 return (0); 1844 } 1845 1846 /* 1847 * Process a state change from the upper layer for the given IPv6 group. 1848 * 1849 * Each socket holds a reference on the in_multi in its own ip_moptions. 1850 * The socket layer will have made the necessary updates to.the group 1851 * state, it is now up to MLD to issue a state change report if there 1852 * has been any change between T0 (when the last state-change was issued) 1853 * and T1 (now). 1854 * 1855 * We use the MLDv2 state machine at group level. The MLd module 1856 * however makes the decision as to which MLD protocol version to speak. 1857 * A state change *from* INCLUDE {} always means an initial join. 1858 * A state change *to* INCLUDE {} always means a final leave. 1859 * 1860 * If delay is non-zero, and the state change is an initial multicast 1861 * join, the state change report will be delayed by 'delay' ticks 1862 * in units of MLD_FASTHZ if MLDv1 is active on the link; otherwise 1863 * the initial MLDv2 state change report will be delayed by whichever 1864 * is sooner, a pending state-change timer or delay itself. 1865 * 1866 * VIMAGE: curvnet should have been set by caller, as this routine 1867 * is called from the socket option handlers. 1868 */ 1869 int 1870 mld_change_state(struct in6_multi *inm, const int delay) 1871 { 1872 struct mld_ifsoftc *mli; 1873 struct ifnet *ifp; 1874 int error; 1875 1876 IN6_MULTI_LIST_LOCK_ASSERT(); 1877 1878 error = 0; 1879 1880 /* 1881 * Check if the in6_multi has already been disconnected. 1882 */ 1883 if (inm->in6m_ifp == NULL) { 1884 CTR1(KTR_MLD, "%s: inm is disconnected", __func__); 1885 return (0); 1886 } 1887 1888 /* 1889 * Try to detect if the upper layer just asked us to change state 1890 * for an interface which has now gone away. 1891 */ 1892 KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__)); 1893 ifp = inm->in6m_ifma->ifma_ifp; 1894 if (ifp == NULL) 1895 return (0); 1896 /* 1897 * Sanity check that netinet6's notion of ifp is the 1898 * same as net's. 1899 */ 1900 KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__)); 1901 1902 MLD_LOCK(); 1903 mli = MLD_IFINFO(ifp); 1904 KASSERT(mli != NULL, ("%s: no mld_ifsoftc for ifp %p", __func__, ifp)); 1905 1906 /* 1907 * If we detect a state transition to or from MCAST_UNDEFINED 1908 * for this group, then we are starting or finishing an MLD 1909 * life cycle for this group. 1910 */ 1911 if (inm->in6m_st[1].iss_fmode != inm->in6m_st[0].iss_fmode) { 1912 CTR3(KTR_MLD, "%s: inm transition %d -> %d", __func__, 1913 inm->in6m_st[0].iss_fmode, inm->in6m_st[1].iss_fmode); 1914 if (inm->in6m_st[0].iss_fmode == MCAST_UNDEFINED) { 1915 CTR1(KTR_MLD, "%s: initial join", __func__); 1916 error = mld_initial_join(inm, mli, delay); 1917 goto out_locked; 1918 } else if (inm->in6m_st[1].iss_fmode == MCAST_UNDEFINED) { 1919 CTR1(KTR_MLD, "%s: final leave", __func__); 1920 mld_final_leave(inm, mli); 1921 goto out_locked; 1922 } 1923 } else { 1924 CTR1(KTR_MLD, "%s: filter set change", __func__); 1925 } 1926 1927 error = mld_handle_state_change(inm, mli); 1928 1929 out_locked: 1930 MLD_UNLOCK(); 1931 return (error); 1932 } 1933 1934 /* 1935 * Perform the initial join for an MLD group. 1936 * 1937 * When joining a group: 1938 * If the group should have its MLD traffic suppressed, do nothing. 1939 * MLDv1 starts sending MLDv1 host membership reports. 1940 * MLDv2 will schedule an MLDv2 state-change report containing the 1941 * initial state of the membership. 1942 * 1943 * If the delay argument is non-zero, then we must delay sending the 1944 * initial state change for delay ticks (in units of MLD_FASTHZ). 1945 */ 1946 static int 1947 mld_initial_join(struct in6_multi *inm, struct mld_ifsoftc *mli, 1948 const int delay) 1949 { 1950 struct epoch_tracker et; 1951 struct ifnet *ifp; 1952 struct mbufq *mq; 1953 int error, retval, syncstates; 1954 int odelay; 1955 #ifdef KTR 1956 char ip6tbuf[INET6_ADDRSTRLEN]; 1957 #endif 1958 1959 CTR4(KTR_MLD, "%s: initial join %s on ifp %p(%s)", 1960 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr), 1961 inm->in6m_ifp, if_name(inm->in6m_ifp)); 1962 1963 error = 0; 1964 syncstates = 1; 1965 1966 ifp = inm->in6m_ifp; 1967 1968 IN6_MULTI_LIST_LOCK_ASSERT(); 1969 MLD_LOCK_ASSERT(); 1970 1971 KASSERT(mli && mli->mli_ifp == ifp, ("%s: inconsistent ifp", __func__)); 1972 1973 /* 1974 * Groups joined on loopback or marked as 'not reported', 1975 * enter the MLD_SILENT_MEMBER state and 1976 * are never reported in any protocol exchanges. 1977 * All other groups enter the appropriate state machine 1978 * for the version in use on this link. 1979 * A link marked as MLIF_SILENT causes MLD to be completely 1980 * disabled for the link. 1981 */ 1982 if ((ifp->if_flags & IFF_LOOPBACK) || 1983 (mli->mli_flags & MLIF_SILENT) || 1984 !mld_is_addr_reported(&inm->in6m_addr)) { 1985 CTR1(KTR_MLD, 1986 "%s: not kicking state machine for silent group", __func__); 1987 inm->in6m_state = MLD_SILENT_MEMBER; 1988 inm->in6m_timer = 0; 1989 } else { 1990 /* 1991 * Deal with overlapping in_multi lifecycle. 1992 * If this group was LEAVING, then make sure 1993 * we drop the reference we picked up to keep the 1994 * group around for the final INCLUDE {} enqueue. 1995 */ 1996 if (mli->mli_version == MLD_VERSION_2 && 1997 inm->in6m_state == MLD_LEAVING_MEMBER) { 1998 inm->in6m_refcount--; 1999 MPASS(inm->in6m_refcount > 0); 2000 } 2001 inm->in6m_state = MLD_REPORTING_MEMBER; 2002 2003 switch (mli->mli_version) { 2004 case MLD_VERSION_1: 2005 /* 2006 * If a delay was provided, only use it if 2007 * it is greater than the delay normally 2008 * used for an MLDv1 state change report, 2009 * and delay sending the initial MLDv1 report 2010 * by not transitioning to the IDLE state. 2011 */ 2012 odelay = MLD_RANDOM_DELAY(MLD_V1_MAX_RI * MLD_FASTHZ); 2013 if (delay) { 2014 inm->in6m_timer = max(delay, odelay); 2015 V_current_state_timers_running6 = 1; 2016 } else { 2017 inm->in6m_state = MLD_IDLE_MEMBER; 2018 NET_EPOCH_ENTER(et); 2019 error = mld_v1_transmit_report(inm, 2020 MLD_LISTENER_REPORT); 2021 NET_EPOCH_EXIT(et); 2022 if (error == 0) { 2023 inm->in6m_timer = odelay; 2024 V_current_state_timers_running6 = 1; 2025 } 2026 } 2027 break; 2028 2029 case MLD_VERSION_2: 2030 /* 2031 * Defer update of T0 to T1, until the first copy 2032 * of the state change has been transmitted. 2033 */ 2034 syncstates = 0; 2035 2036 /* 2037 * Immediately enqueue a State-Change Report for 2038 * this interface, freeing any previous reports. 2039 * Don't kick the timers if there is nothing to do, 2040 * or if an error occurred. 2041 */ 2042 mq = &inm->in6m_scq; 2043 mbufq_drain(mq); 2044 retval = mld_v2_enqueue_group_record(mq, inm, 1, 2045 0, 0, (mli->mli_flags & MLIF_USEALLOW)); 2046 CTR2(KTR_MLD, "%s: enqueue record = %d", 2047 __func__, retval); 2048 if (retval <= 0) { 2049 error = retval * -1; 2050 break; 2051 } 2052 2053 /* 2054 * Schedule transmission of pending state-change 2055 * report up to RV times for this link. The timer 2056 * will fire at the next mld_fasttimo (~200ms), 2057 * giving us an opportunity to merge the reports. 2058 * 2059 * If a delay was provided to this function, only 2060 * use this delay if sooner than the existing one. 2061 */ 2062 KASSERT(mli->mli_rv > 1, 2063 ("%s: invalid robustness %d", __func__, 2064 mli->mli_rv)); 2065 inm->in6m_scrv = mli->mli_rv; 2066 if (delay) { 2067 if (inm->in6m_sctimer > 1) { 2068 inm->in6m_sctimer = 2069 min(inm->in6m_sctimer, delay); 2070 } else 2071 inm->in6m_sctimer = delay; 2072 } else 2073 inm->in6m_sctimer = 1; 2074 V_state_change_timers_running6 = 1; 2075 2076 error = 0; 2077 break; 2078 } 2079 } 2080 2081 /* 2082 * Only update the T0 state if state change is atomic, 2083 * i.e. we don't need to wait for a timer to fire before we 2084 * can consider the state change to have been communicated. 2085 */ 2086 if (syncstates) { 2087 in6m_commit(inm); 2088 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__, 2089 ip6_sprintf(ip6tbuf, &inm->in6m_addr), 2090 if_name(inm->in6m_ifp)); 2091 } 2092 2093 return (error); 2094 } 2095 2096 /* 2097 * Issue an intermediate state change during the life-cycle. 2098 */ 2099 static int 2100 mld_handle_state_change(struct in6_multi *inm, struct mld_ifsoftc *mli) 2101 { 2102 struct ifnet *ifp; 2103 int retval; 2104 #ifdef KTR 2105 char ip6tbuf[INET6_ADDRSTRLEN]; 2106 #endif 2107 2108 CTR4(KTR_MLD, "%s: state change for %s on ifp %p(%s)", 2109 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr), 2110 inm->in6m_ifp, if_name(inm->in6m_ifp)); 2111 2112 ifp = inm->in6m_ifp; 2113 2114 IN6_MULTI_LIST_LOCK_ASSERT(); 2115 MLD_LOCK_ASSERT(); 2116 2117 KASSERT(mli && mli->mli_ifp == ifp, 2118 ("%s: inconsistent ifp", __func__)); 2119 2120 if ((ifp->if_flags & IFF_LOOPBACK) || 2121 (mli->mli_flags & MLIF_SILENT) || 2122 !mld_is_addr_reported(&inm->in6m_addr) || 2123 (mli->mli_version != MLD_VERSION_2)) { 2124 if (!mld_is_addr_reported(&inm->in6m_addr)) { 2125 CTR1(KTR_MLD, 2126 "%s: not kicking state machine for silent group", __func__); 2127 } 2128 CTR1(KTR_MLD, "%s: nothing to do", __func__); 2129 in6m_commit(inm); 2130 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__, 2131 ip6_sprintf(ip6tbuf, &inm->in6m_addr), 2132 if_name(inm->in6m_ifp)); 2133 return (0); 2134 } 2135 2136 mbufq_drain(&inm->in6m_scq); 2137 2138 retval = mld_v2_enqueue_group_record(&inm->in6m_scq, inm, 1, 0, 0, 2139 (mli->mli_flags & MLIF_USEALLOW)); 2140 CTR2(KTR_MLD, "%s: enqueue record = %d", __func__, retval); 2141 if (retval <= 0) 2142 return (-retval); 2143 2144 /* 2145 * If record(s) were enqueued, start the state-change 2146 * report timer for this group. 2147 */ 2148 inm->in6m_scrv = mli->mli_rv; 2149 inm->in6m_sctimer = 1; 2150 V_state_change_timers_running6 = 1; 2151 2152 return (0); 2153 } 2154 2155 /* 2156 * Perform the final leave for a multicast address. 2157 * 2158 * When leaving a group: 2159 * MLDv1 sends a DONE message, if and only if we are the reporter. 2160 * MLDv2 enqueues a state-change report containing a transition 2161 * to INCLUDE {} for immediate transmission. 2162 */ 2163 static void 2164 mld_final_leave(struct in6_multi *inm, struct mld_ifsoftc *mli) 2165 { 2166 struct epoch_tracker et; 2167 int syncstates; 2168 #ifdef KTR 2169 char ip6tbuf[INET6_ADDRSTRLEN]; 2170 #endif 2171 2172 syncstates = 1; 2173 2174 CTR4(KTR_MLD, "%s: final leave %s on ifp %p(%s)", 2175 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr), 2176 inm->in6m_ifp, if_name(inm->in6m_ifp)); 2177 2178 IN6_MULTI_LIST_LOCK_ASSERT(); 2179 MLD_LOCK_ASSERT(); 2180 2181 switch (inm->in6m_state) { 2182 case MLD_NOT_MEMBER: 2183 case MLD_SILENT_MEMBER: 2184 case MLD_LEAVING_MEMBER: 2185 /* Already leaving or left; do nothing. */ 2186 CTR1(KTR_MLD, 2187 "%s: not kicking state machine for silent group", __func__); 2188 break; 2189 case MLD_REPORTING_MEMBER: 2190 case MLD_IDLE_MEMBER: 2191 case MLD_G_QUERY_PENDING_MEMBER: 2192 case MLD_SG_QUERY_PENDING_MEMBER: 2193 if (mli->mli_version == MLD_VERSION_1) { 2194 #ifdef INVARIANTS 2195 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER || 2196 inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER) 2197 panic("%s: MLDv2 state reached, not MLDv2 mode", 2198 __func__); 2199 #endif 2200 NET_EPOCH_ENTER(et); 2201 mld_v1_transmit_report(inm, MLD_LISTENER_DONE); 2202 NET_EPOCH_EXIT(et); 2203 inm->in6m_state = MLD_NOT_MEMBER; 2204 V_current_state_timers_running6 = 1; 2205 } else if (mli->mli_version == MLD_VERSION_2) { 2206 /* 2207 * Stop group timer and all pending reports. 2208 * Immediately enqueue a state-change report 2209 * TO_IN {} to be sent on the next fast timeout, 2210 * giving us an opportunity to merge reports. 2211 */ 2212 mbufq_drain(&inm->in6m_scq); 2213 inm->in6m_timer = 0; 2214 inm->in6m_scrv = mli->mli_rv; 2215 CTR4(KTR_MLD, "%s: Leaving %s/%s with %d " 2216 "pending retransmissions.", __func__, 2217 ip6_sprintf(ip6tbuf, &inm->in6m_addr), 2218 if_name(inm->in6m_ifp), inm->in6m_scrv); 2219 if (inm->in6m_scrv == 0) { 2220 inm->in6m_state = MLD_NOT_MEMBER; 2221 inm->in6m_sctimer = 0; 2222 } else { 2223 int retval __diagused; 2224 2225 in6m_acquire_locked(inm); 2226 2227 retval = mld_v2_enqueue_group_record( 2228 &inm->in6m_scq, inm, 1, 0, 0, 2229 (mli->mli_flags & MLIF_USEALLOW)); 2230 KASSERT(retval != 0, 2231 ("%s: enqueue record = %d", __func__, 2232 retval)); 2233 2234 inm->in6m_state = MLD_LEAVING_MEMBER; 2235 inm->in6m_sctimer = 1; 2236 V_state_change_timers_running6 = 1; 2237 syncstates = 0; 2238 } 2239 break; 2240 } 2241 break; 2242 case MLD_LAZY_MEMBER: 2243 case MLD_SLEEPING_MEMBER: 2244 case MLD_AWAKENING_MEMBER: 2245 /* Our reports are suppressed; do nothing. */ 2246 break; 2247 } 2248 2249 if (syncstates) { 2250 in6m_commit(inm); 2251 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__, 2252 ip6_sprintf(ip6tbuf, &inm->in6m_addr), 2253 if_name(inm->in6m_ifp)); 2254 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED; 2255 CTR3(KTR_MLD, "%s: T1 now MCAST_UNDEFINED for %p/%s", 2256 __func__, &inm->in6m_addr, if_name(inm->in6m_ifp)); 2257 } 2258 } 2259 2260 /* 2261 * Enqueue an MLDv2 group record to the given output queue. 2262 * 2263 * If is_state_change is zero, a current-state record is appended. 2264 * If is_state_change is non-zero, a state-change report is appended. 2265 * 2266 * If is_group_query is non-zero, an mbuf packet chain is allocated. 2267 * If is_group_query is zero, and if there is a packet with free space 2268 * at the tail of the queue, it will be appended to providing there 2269 * is enough free space. 2270 * Otherwise a new mbuf packet chain is allocated. 2271 * 2272 * If is_source_query is non-zero, each source is checked to see if 2273 * it was recorded for a Group-Source query, and will be omitted if 2274 * it is not both in-mode and recorded. 2275 * 2276 * If use_block_allow is non-zero, state change reports for initial join 2277 * and final leave, on an inclusive mode group with a source list, will be 2278 * rewritten to use the ALLOW_NEW and BLOCK_OLD record types, respectively. 2279 * 2280 * The function will attempt to allocate leading space in the packet 2281 * for the IPv6+ICMP headers to be prepended without fragmenting the chain. 2282 * 2283 * If successful the size of all data appended to the queue is returned, 2284 * otherwise an error code less than zero is returned, or zero if 2285 * no record(s) were appended. 2286 */ 2287 static int 2288 mld_v2_enqueue_group_record(struct mbufq *mq, struct in6_multi *inm, 2289 const int is_state_change, const int is_group_query, 2290 const int is_source_query, const int use_block_allow) 2291 { 2292 struct mldv2_record mr; 2293 struct mldv2_record *pmr; 2294 struct ifnet *ifp; 2295 struct ip6_msource *ims, *nims; 2296 struct mbuf *m0, *m, *md; 2297 int is_filter_list_change; 2298 int minrec0len, m0srcs, msrcs, nbytes, off; 2299 int record_has_sources; 2300 int now; 2301 int type; 2302 uint8_t mode; 2303 #ifdef KTR 2304 char ip6tbuf[INET6_ADDRSTRLEN]; 2305 #endif 2306 2307 IN6_MULTI_LIST_LOCK_ASSERT(); 2308 2309 ifp = inm->in6m_ifp; 2310 is_filter_list_change = 0; 2311 m = NULL; 2312 m0 = NULL; 2313 m0srcs = 0; 2314 msrcs = 0; 2315 nbytes = 0; 2316 nims = NULL; 2317 record_has_sources = 1; 2318 pmr = NULL; 2319 type = MLD_DO_NOTHING; 2320 mode = inm->in6m_st[1].iss_fmode; 2321 2322 /* 2323 * If we did not transition out of ASM mode during t0->t1, 2324 * and there are no source nodes to process, we can skip 2325 * the generation of source records. 2326 */ 2327 if (inm->in6m_st[0].iss_asm > 0 && inm->in6m_st[1].iss_asm > 0 && 2328 inm->in6m_nsrc == 0) 2329 record_has_sources = 0; 2330 2331 if (is_state_change) { 2332 /* 2333 * Queue a state change record. 2334 * If the mode did not change, and there are non-ASM 2335 * listeners or source filters present, 2336 * we potentially need to issue two records for the group. 2337 * If there are ASM listeners, and there was no filter 2338 * mode transition of any kind, do nothing. 2339 * 2340 * If we are transitioning to MCAST_UNDEFINED, we need 2341 * not send any sources. A transition to/from this state is 2342 * considered inclusive with some special treatment. 2343 * 2344 * If we are rewriting initial joins/leaves to use 2345 * ALLOW/BLOCK, and the group's membership is inclusive, 2346 * we need to send sources in all cases. 2347 */ 2348 if (mode != inm->in6m_st[0].iss_fmode) { 2349 if (mode == MCAST_EXCLUDE) { 2350 CTR1(KTR_MLD, "%s: change to EXCLUDE", 2351 __func__); 2352 type = MLD_CHANGE_TO_EXCLUDE_MODE; 2353 } else { 2354 CTR1(KTR_MLD, "%s: change to INCLUDE", 2355 __func__); 2356 if (use_block_allow) { 2357 /* 2358 * XXX 2359 * Here we're interested in state 2360 * edges either direction between 2361 * MCAST_UNDEFINED and MCAST_INCLUDE. 2362 * Perhaps we should just check 2363 * the group state, rather than 2364 * the filter mode. 2365 */ 2366 if (mode == MCAST_UNDEFINED) { 2367 type = MLD_BLOCK_OLD_SOURCES; 2368 } else { 2369 type = MLD_ALLOW_NEW_SOURCES; 2370 } 2371 } else { 2372 type = MLD_CHANGE_TO_INCLUDE_MODE; 2373 if (mode == MCAST_UNDEFINED) 2374 record_has_sources = 0; 2375 } 2376 } 2377 } else { 2378 if (record_has_sources) { 2379 is_filter_list_change = 1; 2380 } else { 2381 type = MLD_DO_NOTHING; 2382 } 2383 } 2384 } else { 2385 /* 2386 * Queue a current state record. 2387 */ 2388 if (mode == MCAST_EXCLUDE) { 2389 type = MLD_MODE_IS_EXCLUDE; 2390 } else if (mode == MCAST_INCLUDE) { 2391 type = MLD_MODE_IS_INCLUDE; 2392 KASSERT(inm->in6m_st[1].iss_asm == 0, 2393 ("%s: inm %p is INCLUDE but ASM count is %d", 2394 __func__, inm, inm->in6m_st[1].iss_asm)); 2395 } 2396 } 2397 2398 /* 2399 * Generate the filter list changes using a separate function. 2400 */ 2401 if (is_filter_list_change) 2402 return (mld_v2_enqueue_filter_change(mq, inm)); 2403 2404 if (type == MLD_DO_NOTHING) { 2405 CTR3(KTR_MLD, "%s: nothing to do for %s/%s", 2406 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr), 2407 if_name(inm->in6m_ifp)); 2408 return (0); 2409 } 2410 2411 /* 2412 * If any sources are present, we must be able to fit at least 2413 * one in the trailing space of the tail packet's mbuf, 2414 * ideally more. 2415 */ 2416 minrec0len = sizeof(struct mldv2_record); 2417 if (record_has_sources) 2418 minrec0len += sizeof(struct in6_addr); 2419 2420 CTR4(KTR_MLD, "%s: queueing %s for %s/%s", __func__, 2421 mld_rec_type_to_str(type), 2422 ip6_sprintf(ip6tbuf, &inm->in6m_addr), 2423 if_name(inm->in6m_ifp)); 2424 2425 /* 2426 * Check if we have a packet in the tail of the queue for this 2427 * group into which the first group record for this group will fit. 2428 * Otherwise allocate a new packet. 2429 * Always allocate leading space for IP6+RA+ICMPV6+REPORT. 2430 * Note: Group records for G/GSR query responses MUST be sent 2431 * in their own packet. 2432 */ 2433 m0 = mbufq_last(mq); 2434 if (!is_group_query && 2435 m0 != NULL && 2436 (m0->m_pkthdr.vt_nrecs + 1 <= MLD_V2_REPORT_MAXRECS) && 2437 (m0->m_pkthdr.len + minrec0len) < 2438 (ifp->if_mtu - MLD_MTUSPACE)) { 2439 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len - 2440 sizeof(struct mldv2_record)) / 2441 sizeof(struct in6_addr); 2442 m = m0; 2443 CTR1(KTR_MLD, "%s: use existing packet", __func__); 2444 } else { 2445 if (mbufq_full(mq)) { 2446 CTR1(KTR_MLD, "%s: outbound queue full", __func__); 2447 return (-ENOMEM); 2448 } 2449 m = NULL; 2450 m0srcs = (ifp->if_mtu - MLD_MTUSPACE - 2451 sizeof(struct mldv2_record)) / sizeof(struct in6_addr); 2452 if (!is_state_change && !is_group_query) 2453 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 2454 if (m == NULL) 2455 m = m_gethdr(M_NOWAIT, MT_DATA); 2456 if (m == NULL) 2457 return (-ENOMEM); 2458 2459 mld_save_context(m, ifp); 2460 2461 CTR1(KTR_MLD, "%s: allocated first packet", __func__); 2462 } 2463 2464 /* 2465 * Append group record. 2466 * If we have sources, we don't know how many yet. 2467 */ 2468 mr.mr_type = type; 2469 mr.mr_datalen = 0; 2470 mr.mr_numsrc = 0; 2471 mr.mr_addr = inm->in6m_addr; 2472 in6_clearscope(&mr.mr_addr); 2473 if (!m_append(m, sizeof(struct mldv2_record), (void *)&mr)) { 2474 if (m != m0) 2475 m_freem(m); 2476 CTR1(KTR_MLD, "%s: m_append() failed.", __func__); 2477 return (-ENOMEM); 2478 } 2479 nbytes += sizeof(struct mldv2_record); 2480 2481 /* 2482 * Append as many sources as will fit in the first packet. 2483 * If we are appending to a new packet, the chain allocation 2484 * may potentially use clusters; use m_getptr() in this case. 2485 * If we are appending to an existing packet, we need to obtain 2486 * a pointer to the group record after m_append(), in case a new 2487 * mbuf was allocated. 2488 * 2489 * Only append sources which are in-mode at t1. If we are 2490 * transitioning to MCAST_UNDEFINED state on the group, and 2491 * use_block_allow is zero, do not include source entries. 2492 * Otherwise, we need to include this source in the report. 2493 * 2494 * Only report recorded sources in our filter set when responding 2495 * to a group-source query. 2496 */ 2497 if (record_has_sources) { 2498 if (m == m0) { 2499 md = m_last(m); 2500 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) + 2501 md->m_len - nbytes); 2502 } else { 2503 md = m_getptr(m, 0, &off); 2504 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) + 2505 off); 2506 } 2507 msrcs = 0; 2508 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, 2509 nims) { 2510 CTR2(KTR_MLD, "%s: visit node %s", __func__, 2511 ip6_sprintf(ip6tbuf, &ims->im6s_addr)); 2512 now = im6s_get_mode(inm, ims, 1); 2513 CTR2(KTR_MLD, "%s: node is %d", __func__, now); 2514 if ((now != mode) || 2515 (now == mode && 2516 (!use_block_allow && mode == MCAST_UNDEFINED))) { 2517 CTR1(KTR_MLD, "%s: skip node", __func__); 2518 continue; 2519 } 2520 if (is_source_query && ims->im6s_stp == 0) { 2521 CTR1(KTR_MLD, "%s: skip unrecorded node", 2522 __func__); 2523 continue; 2524 } 2525 CTR1(KTR_MLD, "%s: append node", __func__); 2526 if (!m_append(m, sizeof(struct in6_addr), 2527 (void *)&ims->im6s_addr)) { 2528 if (m != m0) 2529 m_freem(m); 2530 CTR1(KTR_MLD, "%s: m_append() failed.", 2531 __func__); 2532 return (-ENOMEM); 2533 } 2534 nbytes += sizeof(struct in6_addr); 2535 ++msrcs; 2536 if (msrcs == m0srcs) 2537 break; 2538 } 2539 CTR2(KTR_MLD, "%s: msrcs is %d this packet", __func__, 2540 msrcs); 2541 pmr->mr_numsrc = htons(msrcs); 2542 nbytes += (msrcs * sizeof(struct in6_addr)); 2543 } 2544 2545 if (is_source_query && msrcs == 0) { 2546 CTR1(KTR_MLD, "%s: no recorded sources to report", __func__); 2547 if (m != m0) 2548 m_freem(m); 2549 return (0); 2550 } 2551 2552 /* 2553 * We are good to go with first packet. 2554 */ 2555 if (m != m0) { 2556 CTR1(KTR_MLD, "%s: enqueueing first packet", __func__); 2557 m->m_pkthdr.vt_nrecs = 1; 2558 mbufq_enqueue(mq, m); 2559 } else 2560 m->m_pkthdr.vt_nrecs++; 2561 2562 /* 2563 * No further work needed if no source list in packet(s). 2564 */ 2565 if (!record_has_sources) 2566 return (nbytes); 2567 2568 /* 2569 * Whilst sources remain to be announced, we need to allocate 2570 * a new packet and fill out as many sources as will fit. 2571 * Always try for a cluster first. 2572 */ 2573 while (nims != NULL) { 2574 if (mbufq_full(mq)) { 2575 CTR1(KTR_MLD, "%s: outbound queue full", __func__); 2576 return (-ENOMEM); 2577 } 2578 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 2579 if (m == NULL) 2580 m = m_gethdr(M_NOWAIT, MT_DATA); 2581 if (m == NULL) 2582 return (-ENOMEM); 2583 mld_save_context(m, ifp); 2584 md = m_getptr(m, 0, &off); 2585 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) + off); 2586 CTR1(KTR_MLD, "%s: allocated next packet", __func__); 2587 2588 if (!m_append(m, sizeof(struct mldv2_record), (void *)&mr)) { 2589 if (m != m0) 2590 m_freem(m); 2591 CTR1(KTR_MLD, "%s: m_append() failed.", __func__); 2592 return (-ENOMEM); 2593 } 2594 m->m_pkthdr.vt_nrecs = 1; 2595 nbytes += sizeof(struct mldv2_record); 2596 2597 m0srcs = (ifp->if_mtu - MLD_MTUSPACE - 2598 sizeof(struct mldv2_record)) / sizeof(struct in6_addr); 2599 2600 msrcs = 0; 2601 RB_FOREACH_FROM(ims, ip6_msource_tree, nims) { 2602 CTR2(KTR_MLD, "%s: visit node %s", 2603 __func__, ip6_sprintf(ip6tbuf, &ims->im6s_addr)); 2604 now = im6s_get_mode(inm, ims, 1); 2605 if ((now != mode) || 2606 (now == mode && 2607 (!use_block_allow && mode == MCAST_UNDEFINED))) { 2608 CTR1(KTR_MLD, "%s: skip node", __func__); 2609 continue; 2610 } 2611 if (is_source_query && ims->im6s_stp == 0) { 2612 CTR1(KTR_MLD, "%s: skip unrecorded node", 2613 __func__); 2614 continue; 2615 } 2616 CTR1(KTR_MLD, "%s: append node", __func__); 2617 if (!m_append(m, sizeof(struct in6_addr), 2618 (void *)&ims->im6s_addr)) { 2619 if (m != m0) 2620 m_freem(m); 2621 CTR1(KTR_MLD, "%s: m_append() failed.", 2622 __func__); 2623 return (-ENOMEM); 2624 } 2625 ++msrcs; 2626 if (msrcs == m0srcs) 2627 break; 2628 } 2629 pmr->mr_numsrc = htons(msrcs); 2630 nbytes += (msrcs * sizeof(struct in6_addr)); 2631 2632 CTR1(KTR_MLD, "%s: enqueueing next packet", __func__); 2633 mbufq_enqueue(mq, m); 2634 } 2635 2636 return (nbytes); 2637 } 2638 2639 /* 2640 * Type used to mark record pass completion. 2641 * We exploit the fact we can cast to this easily from the 2642 * current filter modes on each ip_msource node. 2643 */ 2644 typedef enum { 2645 REC_NONE = 0x00, /* MCAST_UNDEFINED */ 2646 REC_ALLOW = 0x01, /* MCAST_INCLUDE */ 2647 REC_BLOCK = 0x02, /* MCAST_EXCLUDE */ 2648 REC_FULL = REC_ALLOW | REC_BLOCK 2649 } rectype_t; 2650 2651 /* 2652 * Enqueue an MLDv2 filter list change to the given output queue. 2653 * 2654 * Source list filter state is held in an RB-tree. When the filter list 2655 * for a group is changed without changing its mode, we need to compute 2656 * the deltas between T0 and T1 for each source in the filter set, 2657 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records. 2658 * 2659 * As we may potentially queue two record types, and the entire R-B tree 2660 * needs to be walked at once, we break this out into its own function 2661 * so we can generate a tightly packed queue of packets. 2662 * 2663 * XXX This could be written to only use one tree walk, although that makes 2664 * serializing into the mbuf chains a bit harder. For now we do two walks 2665 * which makes things easier on us, and it may or may not be harder on 2666 * the L2 cache. 2667 * 2668 * If successful the size of all data appended to the queue is returned, 2669 * otherwise an error code less than zero is returned, or zero if 2670 * no record(s) were appended. 2671 */ 2672 static int 2673 mld_v2_enqueue_filter_change(struct mbufq *mq, struct in6_multi *inm) 2674 { 2675 static const int MINRECLEN = 2676 sizeof(struct mldv2_record) + sizeof(struct in6_addr); 2677 struct ifnet *ifp; 2678 struct mldv2_record mr; 2679 struct mldv2_record *pmr; 2680 struct ip6_msource *ims, *nims; 2681 struct mbuf *m, *m0, *md; 2682 int m0srcs, nbytes, npbytes, off, rsrcs, schanged; 2683 uint8_t mode, now, then; 2684 rectype_t crt, drt, nrt; 2685 #ifdef KTR 2686 int nallow, nblock; 2687 char ip6tbuf[INET6_ADDRSTRLEN]; 2688 #endif 2689 2690 IN6_MULTI_LIST_LOCK_ASSERT(); 2691 2692 if (inm->in6m_nsrc == 0 || 2693 (inm->in6m_st[0].iss_asm > 0 && inm->in6m_st[1].iss_asm > 0)) 2694 return (0); 2695 2696 ifp = inm->in6m_ifp; /* interface */ 2697 mode = inm->in6m_st[1].iss_fmode; /* filter mode at t1 */ 2698 crt = REC_NONE; /* current group record type */ 2699 drt = REC_NONE; /* mask of completed group record types */ 2700 nrt = REC_NONE; /* record type for current node */ 2701 m0srcs = 0; /* # source which will fit in current mbuf chain */ 2702 npbytes = 0; /* # of bytes appended this packet */ 2703 nbytes = 0; /* # of bytes appended to group's state-change queue */ 2704 rsrcs = 0; /* # sources encoded in current record */ 2705 schanged = 0; /* # nodes encoded in overall filter change */ 2706 #ifdef KTR 2707 nallow = 0; /* # of source entries in ALLOW_NEW */ 2708 nblock = 0; /* # of source entries in BLOCK_OLD */ 2709 #endif 2710 nims = NULL; /* next tree node pointer */ 2711 2712 /* 2713 * For each possible filter record mode. 2714 * The first kind of source we encounter tells us which 2715 * is the first kind of record we start appending. 2716 * If a node transitioned to UNDEFINED at t1, its mode is treated 2717 * as the inverse of the group's filter mode. 2718 */ 2719 while (drt != REC_FULL) { 2720 do { 2721 m0 = mbufq_last(mq); 2722 if (m0 != NULL && 2723 (m0->m_pkthdr.vt_nrecs + 1 <= 2724 MLD_V2_REPORT_MAXRECS) && 2725 (m0->m_pkthdr.len + MINRECLEN) < 2726 (ifp->if_mtu - MLD_MTUSPACE)) { 2727 m = m0; 2728 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len - 2729 sizeof(struct mldv2_record)) / 2730 sizeof(struct in6_addr); 2731 CTR1(KTR_MLD, 2732 "%s: use previous packet", __func__); 2733 } else { 2734 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 2735 if (m == NULL) 2736 m = m_gethdr(M_NOWAIT, MT_DATA); 2737 if (m == NULL) { 2738 CTR1(KTR_MLD, 2739 "%s: m_get*() failed", __func__); 2740 return (-ENOMEM); 2741 } 2742 m->m_pkthdr.vt_nrecs = 0; 2743 mld_save_context(m, ifp); 2744 m0srcs = (ifp->if_mtu - MLD_MTUSPACE - 2745 sizeof(struct mldv2_record)) / 2746 sizeof(struct in6_addr); 2747 npbytes = 0; 2748 CTR1(KTR_MLD, 2749 "%s: allocated new packet", __func__); 2750 } 2751 /* 2752 * Append the MLD group record header to the 2753 * current packet's data area. 2754 * Recalculate pointer to free space for next 2755 * group record, in case m_append() allocated 2756 * a new mbuf or cluster. 2757 */ 2758 memset(&mr, 0, sizeof(mr)); 2759 mr.mr_addr = inm->in6m_addr; 2760 in6_clearscope(&mr.mr_addr); 2761 if (!m_append(m, sizeof(mr), (void *)&mr)) { 2762 if (m != m0) 2763 m_freem(m); 2764 CTR1(KTR_MLD, 2765 "%s: m_append() failed", __func__); 2766 return (-ENOMEM); 2767 } 2768 npbytes += sizeof(struct mldv2_record); 2769 if (m != m0) { 2770 /* new packet; offset in chain */ 2771 md = m_getptr(m, npbytes - 2772 sizeof(struct mldv2_record), &off); 2773 pmr = (struct mldv2_record *)(mtod(md, 2774 uint8_t *) + off); 2775 } else { 2776 /* current packet; offset from last append */ 2777 md = m_last(m); 2778 pmr = (struct mldv2_record *)(mtod(md, 2779 uint8_t *) + md->m_len - 2780 sizeof(struct mldv2_record)); 2781 } 2782 /* 2783 * Begin walking the tree for this record type 2784 * pass, or continue from where we left off 2785 * previously if we had to allocate a new packet. 2786 * Only report deltas in-mode at t1. 2787 * We need not report included sources as allowed 2788 * if we are in inclusive mode on the group, 2789 * however the converse is not true. 2790 */ 2791 rsrcs = 0; 2792 if (nims == NULL) { 2793 nims = RB_MIN(ip6_msource_tree, 2794 &inm->in6m_srcs); 2795 } 2796 RB_FOREACH_FROM(ims, ip6_msource_tree, nims) { 2797 CTR2(KTR_MLD, "%s: visit node %s", __func__, 2798 ip6_sprintf(ip6tbuf, &ims->im6s_addr)); 2799 now = im6s_get_mode(inm, ims, 1); 2800 then = im6s_get_mode(inm, ims, 0); 2801 CTR3(KTR_MLD, "%s: mode: t0 %d, t1 %d", 2802 __func__, then, now); 2803 if (now == then) { 2804 CTR1(KTR_MLD, 2805 "%s: skip unchanged", __func__); 2806 continue; 2807 } 2808 if (mode == MCAST_EXCLUDE && 2809 now == MCAST_INCLUDE) { 2810 CTR1(KTR_MLD, 2811 "%s: skip IN src on EX group", 2812 __func__); 2813 continue; 2814 } 2815 nrt = (rectype_t)now; 2816 if (nrt == REC_NONE) 2817 nrt = (rectype_t)(~mode & REC_FULL); 2818 if (schanged++ == 0) { 2819 crt = nrt; 2820 } else if (crt != nrt) 2821 continue; 2822 if (!m_append(m, sizeof(struct in6_addr), 2823 (void *)&ims->im6s_addr)) { 2824 if (m != m0) 2825 m_freem(m); 2826 CTR1(KTR_MLD, 2827 "%s: m_append() failed", __func__); 2828 return (-ENOMEM); 2829 } 2830 #ifdef KTR 2831 nallow += !!(crt == REC_ALLOW); 2832 nblock += !!(crt == REC_BLOCK); 2833 #endif 2834 if (++rsrcs == m0srcs) 2835 break; 2836 } 2837 /* 2838 * If we did not append any tree nodes on this 2839 * pass, back out of allocations. 2840 */ 2841 if (rsrcs == 0) { 2842 npbytes -= sizeof(struct mldv2_record); 2843 if (m != m0) { 2844 CTR1(KTR_MLD, 2845 "%s: m_free(m)", __func__); 2846 m_freem(m); 2847 } else { 2848 CTR1(KTR_MLD, 2849 "%s: m_adj(m, -mr)", __func__); 2850 m_adj(m, -((int)sizeof( 2851 struct mldv2_record))); 2852 } 2853 continue; 2854 } 2855 npbytes += (rsrcs * sizeof(struct in6_addr)); 2856 if (crt == REC_ALLOW) 2857 pmr->mr_type = MLD_ALLOW_NEW_SOURCES; 2858 else if (crt == REC_BLOCK) 2859 pmr->mr_type = MLD_BLOCK_OLD_SOURCES; 2860 pmr->mr_numsrc = htons(rsrcs); 2861 /* 2862 * Count the new group record, and enqueue this 2863 * packet if it wasn't already queued. 2864 */ 2865 m->m_pkthdr.vt_nrecs++; 2866 if (m != m0) 2867 mbufq_enqueue(mq, m); 2868 nbytes += npbytes; 2869 } while (nims != NULL); 2870 drt |= crt; 2871 crt = (~crt & REC_FULL); 2872 } 2873 2874 CTR3(KTR_MLD, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__, 2875 nallow, nblock); 2876 2877 return (nbytes); 2878 } 2879 2880 static int 2881 mld_v2_merge_state_changes(struct in6_multi *inm, struct mbufq *scq) 2882 { 2883 struct mbufq *gq; 2884 struct mbuf *m; /* pending state-change */ 2885 struct mbuf *m0; /* copy of pending state-change */ 2886 struct mbuf *mt; /* last state-change in packet */ 2887 int docopy, domerge; 2888 u_int recslen; 2889 2890 docopy = 0; 2891 domerge = 0; 2892 recslen = 0; 2893 2894 IN6_MULTI_LIST_LOCK_ASSERT(); 2895 MLD_LOCK_ASSERT(); 2896 2897 /* 2898 * If there are further pending retransmissions, make a writable 2899 * copy of each queued state-change message before merging. 2900 */ 2901 if (inm->in6m_scrv > 0) 2902 docopy = 1; 2903 2904 gq = &inm->in6m_scq; 2905 #ifdef KTR 2906 if (mbufq_first(gq) == NULL) { 2907 CTR2(KTR_MLD, "%s: WARNING: queue for inm %p is empty", 2908 __func__, inm); 2909 } 2910 #endif 2911 2912 m = mbufq_first(gq); 2913 while (m != NULL) { 2914 /* 2915 * Only merge the report into the current packet if 2916 * there is sufficient space to do so; an MLDv2 report 2917 * packet may only contain 65,535 group records. 2918 * Always use a simple mbuf chain concatentation to do this, 2919 * as large state changes for single groups may have 2920 * allocated clusters. 2921 */ 2922 domerge = 0; 2923 mt = mbufq_last(scq); 2924 if (mt != NULL) { 2925 recslen = m_length(m, NULL); 2926 2927 if ((mt->m_pkthdr.vt_nrecs + 2928 m->m_pkthdr.vt_nrecs <= 2929 MLD_V2_REPORT_MAXRECS) && 2930 (mt->m_pkthdr.len + recslen <= 2931 (inm->in6m_ifp->if_mtu - MLD_MTUSPACE))) 2932 domerge = 1; 2933 } 2934 2935 if (!domerge && mbufq_full(gq)) { 2936 CTR2(KTR_MLD, 2937 "%s: outbound queue full, skipping whole packet %p", 2938 __func__, m); 2939 mt = m->m_nextpkt; 2940 if (!docopy) 2941 m_freem(m); 2942 m = mt; 2943 continue; 2944 } 2945 2946 if (!docopy) { 2947 CTR2(KTR_MLD, "%s: dequeueing %p", __func__, m); 2948 m0 = mbufq_dequeue(gq); 2949 m = m0->m_nextpkt; 2950 } else { 2951 CTR2(KTR_MLD, "%s: copying %p", __func__, m); 2952 m0 = m_dup(m, M_NOWAIT); 2953 if (m0 == NULL) 2954 return (ENOMEM); 2955 m0->m_nextpkt = NULL; 2956 m = m->m_nextpkt; 2957 } 2958 2959 if (!domerge) { 2960 CTR3(KTR_MLD, "%s: queueing %p to scq %p)", 2961 __func__, m0, scq); 2962 mbufq_enqueue(scq, m0); 2963 } else { 2964 struct mbuf *mtl; /* last mbuf of packet mt */ 2965 2966 CTR3(KTR_MLD, "%s: merging %p with ifscq tail %p)", 2967 __func__, m0, mt); 2968 2969 mtl = m_last(mt); 2970 m0->m_flags &= ~M_PKTHDR; 2971 mt->m_pkthdr.len += recslen; 2972 mt->m_pkthdr.vt_nrecs += 2973 m0->m_pkthdr.vt_nrecs; 2974 2975 mtl->m_next = m0; 2976 } 2977 } 2978 2979 return (0); 2980 } 2981 2982 /* 2983 * Respond to a pending MLDv2 General Query. 2984 */ 2985 static void 2986 mld_v2_dispatch_general_query(struct mld_ifsoftc *mli) 2987 { 2988 struct ifmultiaddr *ifma; 2989 struct ifnet *ifp; 2990 struct in6_multi *inm; 2991 int retval __unused; 2992 2993 NET_EPOCH_ASSERT(); 2994 IN6_MULTI_LIST_LOCK_ASSERT(); 2995 MLD_LOCK_ASSERT(); 2996 2997 KASSERT(mli->mli_version == MLD_VERSION_2, 2998 ("%s: called when version %d", __func__, mli->mli_version)); 2999 3000 /* 3001 * Check that there are some packets queued. If so, send them first. 3002 * For large number of groups the reply to general query can take 3003 * many packets, we should finish sending them before starting of 3004 * queuing the new reply. 3005 */ 3006 if (mbufq_len(&mli->mli_gq) != 0) 3007 goto send; 3008 3009 ifp = mli->mli_ifp; 3010 3011 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 3012 inm = in6m_ifmultiaddr_get_inm(ifma); 3013 if (inm == NULL) 3014 continue; 3015 KASSERT(ifp == inm->in6m_ifp, 3016 ("%s: inconsistent ifp", __func__)); 3017 3018 switch (inm->in6m_state) { 3019 case MLD_NOT_MEMBER: 3020 case MLD_SILENT_MEMBER: 3021 break; 3022 case MLD_REPORTING_MEMBER: 3023 case MLD_IDLE_MEMBER: 3024 case MLD_LAZY_MEMBER: 3025 case MLD_SLEEPING_MEMBER: 3026 case MLD_AWAKENING_MEMBER: 3027 inm->in6m_state = MLD_REPORTING_MEMBER; 3028 retval = mld_v2_enqueue_group_record(&mli->mli_gq, 3029 inm, 0, 0, 0, 0); 3030 CTR2(KTR_MLD, "%s: enqueue record = %d", 3031 __func__, retval); 3032 break; 3033 case MLD_G_QUERY_PENDING_MEMBER: 3034 case MLD_SG_QUERY_PENDING_MEMBER: 3035 case MLD_LEAVING_MEMBER: 3036 break; 3037 } 3038 } 3039 3040 send: 3041 mld_dispatch_queue(&mli->mli_gq, MLD_MAX_RESPONSE_BURST); 3042 3043 /* 3044 * Slew transmission of bursts over 500ms intervals. 3045 */ 3046 if (mbufq_first(&mli->mli_gq) != NULL) { 3047 mli->mli_v2_timer = 1 + MLD_RANDOM_DELAY( 3048 MLD_RESPONSE_BURST_INTERVAL); 3049 V_interface_timers_running6 = 1; 3050 } 3051 } 3052 3053 /* 3054 * Transmit the next pending message in the output queue. 3055 * 3056 * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis. 3057 * MRT: Nothing needs to be done, as MLD traffic is always local to 3058 * a link and uses a link-scope multicast address. 3059 */ 3060 static void 3061 mld_dispatch_packet(struct mbuf *m) 3062 { 3063 struct ip6_moptions im6o; 3064 struct ifnet *ifp; 3065 struct ifnet *oifp; 3066 struct mbuf *m0; 3067 struct mbuf *md; 3068 struct ip6_hdr *ip6; 3069 struct mld_hdr *mld; 3070 int error; 3071 int off; 3072 int type; 3073 uint32_t ifindex; 3074 3075 CTR2(KTR_MLD, "%s: transmit %p", __func__, m); 3076 NET_EPOCH_ASSERT(); 3077 3078 /* 3079 * Set VNET image pointer from enqueued mbuf chain 3080 * before doing anything else. Whilst we use interface 3081 * indexes to guard against interface detach, they are 3082 * unique to each VIMAGE and must be retrieved. 3083 */ 3084 ifindex = mld_restore_context(m); 3085 3086 /* 3087 * Check if the ifnet still exists. This limits the scope of 3088 * any race in the absence of a global ifp lock for low cost 3089 * (an array lookup). 3090 */ 3091 ifp = ifnet_byindex(ifindex); 3092 if (ifp == NULL) { 3093 CTR3(KTR_MLD, "%s: dropped %p as ifindex %u went away.", 3094 __func__, m, ifindex); 3095 m_freem(m); 3096 IP6STAT_INC(ip6s_noroute); 3097 goto out; 3098 } 3099 3100 im6o.im6o_multicast_hlim = 1; 3101 im6o.im6o_multicast_loop = (V_ip6_mrouter != NULL); 3102 im6o.im6o_multicast_ifp = ifp; 3103 3104 if (m->m_flags & M_MLDV1) { 3105 m0 = m; 3106 } else { 3107 m0 = mld_v2_encap_report(ifp, m); 3108 if (m0 == NULL) { 3109 CTR2(KTR_MLD, "%s: dropped %p", __func__, m); 3110 IP6STAT_INC(ip6s_odropped); 3111 goto out; 3112 } 3113 } 3114 3115 mld_scrub_context(m0); 3116 m_clrprotoflags(m); 3117 m0->m_pkthdr.rcvif = V_loif; 3118 3119 ip6 = mtod(m0, struct ip6_hdr *); 3120 #if 0 3121 (void)in6_setscope(&ip6->ip6_dst, ifp, NULL); /* XXX LOR */ 3122 #else 3123 /* 3124 * XXX XXX Break some KPI rules to prevent an LOR which would 3125 * occur if we called in6_setscope() at transmission. 3126 * See comments at top of file. 3127 */ 3128 MLD_EMBEDSCOPE(&ip6->ip6_dst, ifp->if_index); 3129 #endif 3130 3131 /* 3132 * Retrieve the ICMPv6 type before handoff to ip6_output(), 3133 * so we can bump the stats. 3134 */ 3135 md = m_getptr(m0, sizeof(struct ip6_hdr), &off); 3136 mld = (struct mld_hdr *)(mtod(md, uint8_t *) + off); 3137 type = mld->mld_type; 3138 3139 oifp = NULL; 3140 error = ip6_output(m0, &mld_po, NULL, IPV6_UNSPECSRC, &im6o, 3141 &oifp, NULL); 3142 if (error) { 3143 CTR3(KTR_MLD, "%s: ip6_output(%p) = %d", __func__, m0, error); 3144 goto out; 3145 } 3146 ICMP6STAT_INC(icp6s_outhist[type]); 3147 if (oifp != NULL) { 3148 icmp6_ifstat_inc(oifp, ifs6_out_msg); 3149 switch (type) { 3150 case MLD_LISTENER_REPORT: 3151 case MLDV2_LISTENER_REPORT: 3152 icmp6_ifstat_inc(oifp, ifs6_out_mldreport); 3153 break; 3154 case MLD_LISTENER_DONE: 3155 icmp6_ifstat_inc(oifp, ifs6_out_mlddone); 3156 break; 3157 } 3158 } 3159 out: 3160 return; 3161 } 3162 3163 /* 3164 * Encapsulate an MLDv2 report. 3165 * 3166 * KAME IPv6 requires that hop-by-hop options be passed separately, 3167 * and that the IPv6 header be prepended in a separate mbuf. 3168 * 3169 * Returns a pointer to the new mbuf chain head, or NULL if the 3170 * allocation failed. 3171 */ 3172 static struct mbuf * 3173 mld_v2_encap_report(struct ifnet *ifp, struct mbuf *m) 3174 { 3175 struct mbuf *mh; 3176 struct mldv2_report *mld; 3177 struct ip6_hdr *ip6; 3178 struct in6_ifaddr *ia; 3179 int mldreclen; 3180 3181 KASSERT(ifp != NULL, ("%s: null ifp", __func__)); 3182 KASSERT((m->m_flags & M_PKTHDR), 3183 ("%s: mbuf chain %p is !M_PKTHDR", __func__, m)); 3184 3185 /* 3186 * RFC3590: OK to send as :: or tentative during DAD. 3187 */ 3188 NET_EPOCH_ASSERT(); 3189 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST); 3190 if (ia == NULL) 3191 CTR1(KTR_MLD, "%s: warning: ia is NULL", __func__); 3192 3193 mh = m_gethdr(M_NOWAIT, MT_DATA); 3194 if (mh == NULL) { 3195 if (ia != NULL) 3196 ifa_free(&ia->ia_ifa); 3197 m_freem(m); 3198 return (NULL); 3199 } 3200 M_ALIGN(mh, sizeof(struct ip6_hdr) + sizeof(struct mldv2_report)); 3201 3202 mldreclen = m_length(m, NULL); 3203 CTR2(KTR_MLD, "%s: mldreclen is %d", __func__, mldreclen); 3204 3205 mh->m_len = sizeof(struct ip6_hdr) + sizeof(struct mldv2_report); 3206 mh->m_pkthdr.len = sizeof(struct ip6_hdr) + 3207 sizeof(struct mldv2_report) + mldreclen; 3208 3209 ip6 = mtod(mh, struct ip6_hdr *); 3210 ip6->ip6_flow = 0; 3211 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 3212 ip6->ip6_vfc |= IPV6_VERSION; 3213 ip6->ip6_nxt = IPPROTO_ICMPV6; 3214 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any; 3215 if (ia != NULL) 3216 ifa_free(&ia->ia_ifa); 3217 ip6->ip6_dst = in6addr_linklocal_allv2routers; 3218 /* scope ID will be set in netisr */ 3219 3220 mld = (struct mldv2_report *)(ip6 + 1); 3221 mld->mld_type = MLDV2_LISTENER_REPORT; 3222 mld->mld_code = 0; 3223 mld->mld_cksum = 0; 3224 mld->mld_v2_reserved = 0; 3225 mld->mld_v2_numrecs = htons(m->m_pkthdr.vt_nrecs); 3226 m->m_pkthdr.vt_nrecs = 0; 3227 3228 mh->m_next = m; 3229 mld->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6, 3230 sizeof(struct ip6_hdr), sizeof(struct mldv2_report) + mldreclen); 3231 return (mh); 3232 } 3233 3234 #ifdef KTR 3235 static char * 3236 mld_rec_type_to_str(const int type) 3237 { 3238 3239 switch (type) { 3240 case MLD_CHANGE_TO_EXCLUDE_MODE: 3241 return "TO_EX"; 3242 break; 3243 case MLD_CHANGE_TO_INCLUDE_MODE: 3244 return "TO_IN"; 3245 break; 3246 case MLD_MODE_IS_EXCLUDE: 3247 return "MODE_EX"; 3248 break; 3249 case MLD_MODE_IS_INCLUDE: 3250 return "MODE_IN"; 3251 break; 3252 case MLD_ALLOW_NEW_SOURCES: 3253 return "ALLOW_NEW"; 3254 break; 3255 case MLD_BLOCK_OLD_SOURCES: 3256 return "BLOCK_OLD"; 3257 break; 3258 default: 3259 break; 3260 } 3261 return "unknown"; 3262 } 3263 #endif 3264 3265 static void 3266 mld_init(void *unused __unused) 3267 { 3268 3269 CTR1(KTR_MLD, "%s: initializing", __func__); 3270 MLD_LOCK_INIT(); 3271 3272 ip6_initpktopts(&mld_po); 3273 mld_po.ip6po_hlim = 1; 3274 mld_po.ip6po_hbh = &mld_ra.hbh; 3275 mld_po.ip6po_prefer_tempaddr = IP6PO_TEMPADDR_NOTPREFER; 3276 mld_po.ip6po_flags = IP6PO_DONTFRAG; 3277 3278 callout_init(&mldslow_callout, 1); 3279 callout_reset(&mldslow_callout, hz / MLD_SLOWHZ, mld_slowtimo, NULL); 3280 callout_init(&mldfast_callout, 1); 3281 callout_reset(&mldfast_callout, hz / MLD_FASTHZ, mld_fasttimo, NULL); 3282 } 3283 SYSINIT(mld_init, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE, mld_init, NULL); 3284 3285 static void 3286 mld_uninit(void *unused __unused) 3287 { 3288 3289 CTR1(KTR_MLD, "%s: tearing down", __func__); 3290 callout_drain(&mldslow_callout); 3291 callout_drain(&mldfast_callout); 3292 MLD_LOCK_DESTROY(); 3293 } 3294 SYSUNINIT(mld_uninit, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE, mld_uninit, NULL); 3295 3296 static void 3297 vnet_mld_init(const void *unused __unused) 3298 { 3299 3300 CTR1(KTR_MLD, "%s: initializing", __func__); 3301 3302 LIST_INIT(&V_mli_head); 3303 } 3304 VNET_SYSINIT(vnet_mld_init, SI_SUB_PROTO_MC, SI_ORDER_ANY, vnet_mld_init, 3305 NULL); 3306 3307 static void 3308 vnet_mld_uninit(const void *unused __unused) 3309 { 3310 3311 /* This can happen if we shutdown the network stack. */ 3312 CTR1(KTR_MLD, "%s: tearing down", __func__); 3313 } 3314 VNET_SYSUNINIT(vnet_mld_uninit, SI_SUB_PROTO_MC, SI_ORDER_ANY, vnet_mld_uninit, 3315 NULL); 3316 3317 static int 3318 mld_modevent(module_t mod, int type, void *unused __unused) 3319 { 3320 3321 switch (type) { 3322 case MOD_LOAD: 3323 case MOD_UNLOAD: 3324 break; 3325 default: 3326 return (EOPNOTSUPP); 3327 } 3328 return (0); 3329 } 3330 3331 static moduledata_t mld_mod = { 3332 "mld", 3333 mld_modevent, 3334 0 3335 }; 3336 DECLARE_MODULE(mld, mld_mod, SI_SUB_PROTO_MC, SI_ORDER_ANY); 3337