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