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