1 /*- 2 * Copyright (c) 2007-2009 Bruce Simpson. 3 * Copyright (c) 1988 Stephen Deering. 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Stephen Deering of Stanford University. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)igmp.c 8.1 (Berkeley) 7/19/93 35 */ 36 37 /* 38 * Internet Group Management Protocol (IGMP) routines. 39 * [RFC1112, RFC2236, RFC3376] 40 * 41 * Written by Steve Deering, Stanford, May 1988. 42 * Modified by Rosen Sharma, Stanford, Aug 1994. 43 * Modified by Bill Fenner, Xerox PARC, Feb 1995. 44 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995. 45 * Significantly rewritten for IGMPv3, VIMAGE, and SMP by Bruce Simpson. 46 * 47 * MULTICAST Revision: 3.5.1.4 48 */ 49 50 #include <sys/cdefs.h> 51 __FBSDID("$FreeBSD$"); 52 53 #include "opt_mac.h" 54 #include "opt_route.h" 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/module.h> 59 #include <sys/malloc.h> 60 #include <sys/mbuf.h> 61 #include <sys/socket.h> 62 #include <sys/protosw.h> 63 #include <sys/kernel.h> 64 #include <sys/sysctl.h> 65 #include <sys/vimage.h> 66 #include <sys/ktr.h> 67 #include <sys/condvar.h> 68 69 #include <net/if.h> 70 #include <net/netisr.h> 71 #include <net/route.h> 72 #include <net/vnet.h> 73 74 #include <netinet/in.h> 75 #include <netinet/in_var.h> 76 #include <netinet/in_systm.h> 77 #include <netinet/ip.h> 78 #include <netinet/ip_var.h> 79 #include <netinet/ip_options.h> 80 #include <netinet/igmp.h> 81 #include <netinet/igmp_var.h> 82 #include <netinet/vinet.h> 83 84 #include <machine/in_cksum.h> 85 86 #include <security/mac/mac_framework.h> 87 88 #ifndef KTR_IGMPV3 89 #define KTR_IGMPV3 KTR_SUBSYS 90 #endif 91 92 static struct igmp_ifinfo * 93 igi_alloc_locked(struct ifnet *); 94 static void igi_delete_locked(const struct ifnet *); 95 static void igmp_dispatch_queue(struct ifqueue *, int, const int); 96 static void igmp_fasttimo_vnet(void); 97 static void igmp_final_leave(struct in_multi *, struct igmp_ifinfo *); 98 static int igmp_handle_state_change(struct in_multi *, 99 struct igmp_ifinfo *); 100 static int igmp_initial_join(struct in_multi *, struct igmp_ifinfo *); 101 static int igmp_input_v1_query(struct ifnet *, const struct ip *); 102 static int igmp_input_v2_query(struct ifnet *, const struct ip *, 103 const struct igmp *); 104 static int igmp_input_v3_query(struct ifnet *, const struct ip *, 105 /*const*/ struct igmpv3 *); 106 static int igmp_input_v3_group_query(struct in_multi *, 107 struct igmp_ifinfo *, int, /*const*/ struct igmpv3 *); 108 static int igmp_input_v1_report(struct ifnet *, /*const*/ struct ip *, 109 /*const*/ struct igmp *); 110 static int igmp_input_v2_report(struct ifnet *, /*const*/ struct ip *, 111 /*const*/ struct igmp *); 112 static void igmp_intr(struct mbuf *); 113 static int igmp_isgroupreported(const struct in_addr); 114 static struct mbuf * 115 igmp_ra_alloc(void); 116 #ifdef KTR 117 static char * igmp_rec_type_to_str(const int); 118 #endif 119 static void igmp_set_version(struct igmp_ifinfo *, const int); 120 static void igmp_slowtimo_vnet(void); 121 static void igmp_sysinit(void); 122 static int igmp_v1v2_queue_report(struct in_multi *, const int); 123 static void igmp_v1v2_process_group_timer(struct in_multi *, const int); 124 static void igmp_v1v2_process_querier_timers(struct igmp_ifinfo *); 125 static void igmp_v2_update_group(struct in_multi *, const int); 126 static void igmp_v3_cancel_link_timers(struct igmp_ifinfo *); 127 static void igmp_v3_dispatch_general_query(struct igmp_ifinfo *); 128 static struct mbuf * 129 igmp_v3_encap_report(struct ifnet *, struct mbuf *); 130 static int igmp_v3_enqueue_group_record(struct ifqueue *, 131 struct in_multi *, const int, const int, const int); 132 static int igmp_v3_enqueue_filter_change(struct ifqueue *, 133 struct in_multi *); 134 static void igmp_v3_process_group_timers(struct igmp_ifinfo *, 135 struct ifqueue *, struct ifqueue *, struct in_multi *, 136 const int); 137 static int igmp_v3_merge_state_changes(struct in_multi *, 138 struct ifqueue *); 139 static void igmp_v3_suppress_group_record(struct in_multi *); 140 static int sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS); 141 static int sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS); 142 static int sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS); 143 144 #ifdef VIMAGE 145 static vnet_attach_fn vnet_igmp_iattach; 146 static vnet_detach_fn vnet_igmp_idetach; 147 #else 148 static int vnet_igmp_iattach(const void *); 149 static int vnet_igmp_idetach(const void *); 150 #endif /* VIMAGE */ 151 152 /* 153 * System-wide globals. 154 * 155 * Unlocked access to these is OK, except for the global IGMP output 156 * queue. The IGMP subsystem lock ends up being system-wide for the moment, 157 * because all VIMAGEs have to share a global output queue, as netisrs 158 * themselves are not virtualized. 159 * 160 * Locking: 161 * * The permitted lock order is: IN_MULTI_LOCK, IGMP_LOCK, IF_ADDR_LOCK. 162 * Any may be taken independently; if any are held at the same 163 * time, the above lock order must be followed. 164 * * All output is delegated to the netisr. 165 * Now that Giant has been eliminated, the netisr may be inlined. 166 * * IN_MULTI_LOCK covers in_multi. 167 * * IGMP_LOCK covers igmp_ifinfo and any global variables in this file, 168 * including the output queue. 169 * * IF_ADDR_LOCK covers if_multiaddrs, which is used for a variety of 170 * per-link state iterators. 171 * * igmp_ifinfo is valid as long as PF_INET is attached to the interface, 172 * therefore it is not refcounted. 173 * We allow unlocked reads of igmp_ifinfo when accessed via in_multi. 174 * 175 * Reference counting 176 * * IGMP acquires its own reference every time an in_multi is passed to 177 * it and the group is being joined for the first time. 178 * * IGMP releases its reference(s) on in_multi in a deferred way, 179 * because the operations which process the release run as part of 180 * a loop whose control variables are directly affected by the release 181 * (that, and not recursing on the IF_ADDR_LOCK). 182 * 183 * VIMAGE: Each in_multi corresponds to an ifp, and each ifp corresponds 184 * to a vnet in ifp->if_vnet. 185 * 186 * SMPng: XXX We may potentially race operations on ifma_protospec. 187 * The problem is that we currently lack a clean way of taking the 188 * IF_ADDR_LOCK() between the ifnet and in layers w/o recursing, 189 * as anything which modifies ifma needs to be covered by that lock. 190 * So check for ifma_protospec being NULL before proceeding. 191 */ 192 struct mtx igmp_mtx; 193 194 struct mbuf *m_raopt; /* Router Alert option */ 195 MALLOC_DEFINE(M_IGMP, "igmp", "igmp state"); 196 197 /* 198 * Global netisr output queue. 199 */ 200 struct ifqueue igmpoq; 201 202 /* 203 * VIMAGE-wide globals. 204 * 205 * The IGMPv3 timers themselves need to run per-image, however, 206 * protosw timers run globally (see tcp). 207 * An ifnet can only be in one vimage at a time, and the loopback 208 * ifnet, loif, is itself virtualized. 209 * It would otherwise be possible to seriously hose IGMP state, 210 * and create inconsistencies in upstream multicast routing, if you have 211 * multiple VIMAGEs running on the same link joining different multicast 212 * groups, UNLESS the "primary IP address" is different. This is because 213 * IGMP for IPv4 does not force link-local addresses to be used for each 214 * node, unlike MLD for IPv6. 215 * Obviously the IGMPv3 per-interface state has per-vimage granularity 216 * also as a result. 217 * 218 * FUTURE: Stop using IFP_TO_IA/INADDR_ANY, and use source address selection 219 * policy to control the address used by IGMP on the link. 220 */ 221 #ifdef VIMAGE_GLOBALS 222 int interface_timers_running; /* IGMPv3 general query response */ 223 int state_change_timers_running; /* IGMPv3 state-change retransmit */ 224 int current_state_timers_running; /* IGMPv1/v2 host report; 225 * IGMPv3 g/sg query response */ 226 227 LIST_HEAD(, igmp_ifinfo) igi_head; 228 struct igmpstat igmpstat; 229 struct timeval igmp_gsrdelay; 230 231 int igmp_recvifkludge; 232 int igmp_sendra; 233 int igmp_sendlocal; 234 int igmp_v1enable; 235 int igmp_v2enable; 236 int igmp_legacysupp; 237 int igmp_default_version; 238 #endif /* VIMAGE_GLOBALS */ 239 240 /* 241 * Virtualized sysctls. 242 */ 243 SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_igmp, IGMPCTL_STATS, stats, 244 CTLFLAG_RW, igmpstat, igmpstat, ""); 245 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, recvifkludge, 246 CTLFLAG_RW, igmp_recvifkludge, 0, 247 "Rewrite IGMPv1/v2 reports from 0.0.0.0 to contain subnet address"); 248 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, sendra, 249 CTLFLAG_RW, igmp_sendra, 0, 250 "Send IP Router Alert option in IGMPv2/v3 messages"); 251 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, sendlocal, 252 CTLFLAG_RW, igmp_sendlocal, 0, 253 "Send IGMP membership reports for 224.0.0.0/24 groups"); 254 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, v1enable, 255 CTLFLAG_RW, igmp_v1enable, 0, 256 "Enable backwards compatibility with IGMPv1"); 257 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, v2enable, 258 CTLFLAG_RW, igmp_v2enable, 0, 259 "Enable backwards compatibility with IGMPv2"); 260 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, legacysupp, 261 CTLFLAG_RW, igmp_legacysupp, 0, 262 "Allow v1/v2 reports to suppress v3 group responses"); 263 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, default_version, 264 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, igmp_default_version, 0, 265 sysctl_igmp_default_version, "I", 266 "Default version of IGMP to run on each interface"); 267 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, gsrdelay, 268 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, igmp_gsrdelay.tv_sec, 0, 269 sysctl_igmp_gsr, "I", 270 "Rate limit for IGMPv3 Group-and-Source queries in seconds"); 271 272 /* 273 * Non-virtualized sysctls. 274 */ 275 SYSCTL_NODE(_net_inet_igmp, OID_AUTO, ifinfo, CTLFLAG_RD | CTLFLAG_MPSAFE, 276 sysctl_igmp_ifinfo, "Per-interface IGMPv3 state"); 277 278 static __inline void 279 igmp_save_context(struct mbuf *m, struct ifnet *ifp) 280 { 281 282 #ifdef VIMAGE 283 m->m_pkthdr.header = ifp->if_vnet; 284 #endif /* VIMAGE */ 285 m->m_pkthdr.flowid = ifp->if_index; 286 } 287 288 static __inline void 289 igmp_scrub_context(struct mbuf *m) 290 { 291 292 m->m_pkthdr.header = NULL; 293 m->m_pkthdr.flowid = 0; 294 } 295 296 #ifdef KTR 297 static __inline char * 298 inet_ntoa_haddr(in_addr_t haddr) 299 { 300 struct in_addr ia; 301 302 ia.s_addr = htonl(haddr); 303 return (inet_ntoa(ia)); 304 } 305 #endif 306 307 /* 308 * Restore context from a queued IGMP output chain. 309 * Return saved ifindex. 310 * 311 * VIMAGE: The assertion is there to make sure that we 312 * actually called CURVNET_SET() with what's in the mbuf chain. 313 */ 314 static __inline uint32_t 315 igmp_restore_context(struct mbuf *m) 316 { 317 318 #ifdef notyet 319 #if defined(VIMAGE) && defined(INVARIANTS) 320 KASSERT(curvnet == (m->m_pkthdr.header), 321 ("%s: called when curvnet was not restored", __func__)); 322 #endif 323 #endif 324 return (m->m_pkthdr.flowid); 325 } 326 327 /* 328 * Retrieve or set default IGMP version. 329 * 330 * VIMAGE: Assume curvnet set by caller. 331 * SMPng: NOTE: Serialized by IGMP lock. 332 */ 333 static int 334 sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS) 335 { 336 int error; 337 int new; 338 339 error = sysctl_wire_old_buffer(req, sizeof(int)); 340 if (error) 341 return (error); 342 343 IGMP_LOCK(); 344 345 new = V_igmp_default_version; 346 347 error = sysctl_handle_int(oidp, &new, 0, req); 348 if (error || !req->newptr) 349 goto out_locked; 350 351 if (new < IGMP_VERSION_1 || new > IGMP_VERSION_3) { 352 error = EINVAL; 353 goto out_locked; 354 } 355 356 CTR2(KTR_IGMPV3, "change igmp_default_version from %d to %d", 357 V_igmp_default_version, new); 358 359 V_igmp_default_version = new; 360 361 out_locked: 362 IGMP_UNLOCK(); 363 return (error); 364 } 365 366 /* 367 * Retrieve or set threshold between group-source queries in seconds. 368 * 369 * VIMAGE: Assume curvnet set by caller. 370 * SMPng: NOTE: Serialized by IGMP lock. 371 */ 372 static int 373 sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS) 374 { 375 int error; 376 int i; 377 378 error = sysctl_wire_old_buffer(req, sizeof(int)); 379 if (error) 380 return (error); 381 382 IGMP_LOCK(); 383 384 i = V_igmp_gsrdelay.tv_sec; 385 386 error = sysctl_handle_int(oidp, &i, 0, req); 387 if (error || !req->newptr) 388 goto out_locked; 389 390 if (i < -1 || i >= 60) { 391 error = EINVAL; 392 goto out_locked; 393 } 394 395 CTR2(KTR_IGMPV3, "change igmp_gsrdelay from %d to %d", 396 V_igmp_gsrdelay.tv_sec, i); 397 V_igmp_gsrdelay.tv_sec = i; 398 399 out_locked: 400 IGMP_UNLOCK(); 401 return (error); 402 } 403 404 /* 405 * Expose struct igmp_ifinfo to userland, keyed by ifindex. 406 * For use by ifmcstat(8). 407 * 408 * SMPng: NOTE: Does an unlocked ifindex space read. 409 * VIMAGE: Assume curvnet set by caller. The node handler itself 410 * is not directly virtualized. 411 */ 412 static int 413 sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS) 414 { 415 INIT_VNET_NET(curvnet); 416 int *name; 417 int error; 418 u_int namelen; 419 struct ifnet *ifp; 420 struct igmp_ifinfo *igi; 421 422 name = (int *)arg1; 423 namelen = arg2; 424 425 if (req->newptr != NULL) 426 return (EPERM); 427 428 if (namelen != 1) 429 return (EINVAL); 430 431 error = sysctl_wire_old_buffer(req, sizeof(struct igmp_ifinfo)); 432 if (error) 433 return (error); 434 435 IN_MULTI_LOCK(); 436 IGMP_LOCK(); 437 438 if (name[0] <= 0 || name[0] > V_if_index) { 439 error = ENOENT; 440 goto out_locked; 441 } 442 443 error = ENOENT; 444 445 ifp = ifnet_byindex(name[0]); 446 if (ifp == NULL) 447 goto out_locked; 448 449 LIST_FOREACH(igi, &V_igi_head, igi_link) { 450 if (ifp == igi->igi_ifp) { 451 error = SYSCTL_OUT(req, igi, 452 sizeof(struct igmp_ifinfo)); 453 break; 454 } 455 } 456 457 out_locked: 458 IGMP_UNLOCK(); 459 IN_MULTI_UNLOCK(); 460 return (error); 461 } 462 463 /* 464 * Dispatch an entire queue of pending packet chains 465 * using the netisr. 466 * VIMAGE: Assumes the vnet pointer has been set. 467 */ 468 static void 469 igmp_dispatch_queue(struct ifqueue *ifq, int limit, const int loop) 470 { 471 struct mbuf *m; 472 473 for (;;) { 474 _IF_DEQUEUE(ifq, m); 475 if (m == NULL) 476 break; 477 CTR3(KTR_IGMPV3, "%s: dispatch %p from %p", __func__, ifq, m); 478 if (loop) 479 m->m_flags |= M_IGMP_LOOP; 480 netisr_dispatch(NETISR_IGMP, m); 481 if (--limit == 0) 482 break; 483 } 484 } 485 486 /* 487 * Filter outgoing IGMP report state by group. 488 * 489 * Reports are ALWAYS suppressed for ALL-HOSTS (224.0.0.1). 490 * If the net.inet.igmp.sendlocal sysctl is 0, then IGMP reports are 491 * disabled for all groups in the 224.0.0.0/24 link-local scope. However, 492 * this may break certain IGMP snooping switches which rely on the old 493 * report behaviour. 494 * 495 * Return zero if the given group is one for which IGMP reports 496 * should be suppressed, or non-zero if reports should be issued. 497 */ 498 static __inline int 499 igmp_isgroupreported(const struct in_addr addr) 500 { 501 502 if (in_allhosts(addr) || 503 ((!V_igmp_sendlocal && IN_LOCAL_GROUP(ntohl(addr.s_addr))))) 504 return (0); 505 506 return (1); 507 } 508 509 /* 510 * Construct a Router Alert option to use in outgoing packets. 511 */ 512 static struct mbuf * 513 igmp_ra_alloc(void) 514 { 515 struct mbuf *m; 516 struct ipoption *p; 517 518 MGET(m, M_DONTWAIT, MT_DATA); 519 p = mtod(m, struct ipoption *); 520 p->ipopt_dst.s_addr = INADDR_ANY; 521 p->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */ 522 p->ipopt_list[1] = 0x04; /* 4 bytes long */ 523 p->ipopt_list[2] = IPOPT_EOL; /* End of IP option list */ 524 p->ipopt_list[3] = 0x00; /* pad byte */ 525 m->m_len = sizeof(p->ipopt_dst) + p->ipopt_list[1]; 526 527 return (m); 528 } 529 530 /* 531 * Attach IGMP when PF_INET is attached to an interface. 532 * 533 * VIMAGE: Currently we set the vnet pointer, although it is 534 * likely that it was already set by our caller. 535 */ 536 struct igmp_ifinfo * 537 igmp_domifattach(struct ifnet *ifp) 538 { 539 struct igmp_ifinfo *igi; 540 541 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)", 542 __func__, ifp, ifp->if_xname); 543 544 CURVNET_SET(ifp->if_vnet); 545 IGMP_LOCK(); 546 547 igi = igi_alloc_locked(ifp); 548 if (!(ifp->if_flags & IFF_MULTICAST)) 549 igi->igi_flags |= IGIF_SILENT; 550 551 IGMP_UNLOCK(); 552 CURVNET_RESTORE(); 553 554 return (igi); 555 } 556 557 /* 558 * VIMAGE: assume curvnet set by caller. 559 */ 560 static struct igmp_ifinfo * 561 igi_alloc_locked(/*const*/ struct ifnet *ifp) 562 { 563 struct igmp_ifinfo *igi; 564 565 IGMP_LOCK_ASSERT(); 566 567 igi = malloc(sizeof(struct igmp_ifinfo), M_IGMP, M_NOWAIT|M_ZERO); 568 if (igi == NULL) 569 goto out; 570 571 igi->igi_ifp = ifp; 572 igi->igi_version = V_igmp_default_version; 573 igi->igi_flags = 0; 574 igi->igi_rv = IGMP_RV_INIT; 575 igi->igi_qi = IGMP_QI_INIT; 576 igi->igi_qri = IGMP_QRI_INIT; 577 igi->igi_uri = IGMP_URI_INIT; 578 579 SLIST_INIT(&igi->igi_relinmhead); 580 581 /* 582 * Responses to general queries are subject to bounds. 583 */ 584 IFQ_SET_MAXLEN(&igi->igi_gq, IGMP_MAX_RESPONSE_PACKETS); 585 586 LIST_INSERT_HEAD(&V_igi_head, igi, igi_link); 587 588 CTR2(KTR_IGMPV3, "allocate igmp_ifinfo for ifp %p(%s)", 589 ifp, ifp->if_xname); 590 591 out: 592 return (igi); 593 } 594 595 /* 596 * Hook for ifdetach. 597 * 598 * NOTE: Some finalization tasks need to run before the protocol domain 599 * is detached, but also before the link layer does its cleanup. 600 * 601 * SMPNG: igmp_ifdetach() needs to take IF_ADDR_LOCK(). 602 * XXX This is also bitten by unlocked ifma_protospec access. 603 * 604 * VIMAGE: curvnet should have been set by caller, but let's not assume 605 * that for now. 606 */ 607 void 608 igmp_ifdetach(struct ifnet *ifp) 609 { 610 struct igmp_ifinfo *igi; 611 struct ifmultiaddr *ifma; 612 struct in_multi *inm, *tinm; 613 614 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)", __func__, ifp, 615 ifp->if_xname); 616 617 CURVNET_SET(ifp->if_vnet); 618 619 IGMP_LOCK(); 620 621 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp; 622 if (igi->igi_version == IGMP_VERSION_3) { 623 IF_ADDR_LOCK(ifp); 624 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 625 if (ifma->ifma_addr->sa_family != AF_INET || 626 ifma->ifma_protospec == NULL) 627 continue; 628 #if 0 629 KASSERT(ifma->ifma_protospec != NULL, 630 ("%s: ifma_protospec is NULL", __func__)); 631 #endif 632 inm = (struct in_multi *)ifma->ifma_protospec; 633 if (inm->inm_state == IGMP_LEAVING_MEMBER) { 634 SLIST_INSERT_HEAD(&igi->igi_relinmhead, 635 inm, inm_nrele); 636 } 637 inm_clear_recorded(inm); 638 } 639 IF_ADDR_UNLOCK(ifp); 640 /* 641 * Free the in_multi reference(s) for this IGMP lifecycle. 642 */ 643 SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, inm_nrele, 644 tinm) { 645 SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele); 646 inm_release_locked(inm); 647 } 648 } 649 650 IGMP_UNLOCK(); 651 652 CURVNET_RESTORE(); 653 } 654 655 /* 656 * Hook for domifdetach. 657 * 658 * VIMAGE: curvnet should have been set by caller, but let's not assume 659 * that for now. 660 */ 661 void 662 igmp_domifdetach(struct ifnet *ifp) 663 { 664 struct igmp_ifinfo *igi; 665 666 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)", 667 __func__, ifp, ifp->if_xname); 668 669 CURVNET_SET(ifp->if_vnet); 670 IGMP_LOCK(); 671 672 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp; 673 igi_delete_locked(ifp); 674 675 IGMP_UNLOCK(); 676 CURVNET_RESTORE(); 677 } 678 679 static void 680 igi_delete_locked(const struct ifnet *ifp) 681 { 682 struct igmp_ifinfo *igi, *tigi; 683 684 CTR3(KTR_IGMPV3, "%s: freeing igmp_ifinfo for ifp %p(%s)", 685 __func__, ifp, ifp->if_xname); 686 687 IGMP_LOCK_ASSERT(); 688 689 LIST_FOREACH_SAFE(igi, &V_igi_head, igi_link, tigi) { 690 if (igi->igi_ifp == ifp) { 691 /* 692 * Free deferred General Query responses. 693 */ 694 _IF_DRAIN(&igi->igi_gq); 695 696 LIST_REMOVE(igi, igi_link); 697 698 KASSERT(SLIST_EMPTY(&igi->igi_relinmhead), 699 ("%s: there are dangling in_multi references", 700 __func__)); 701 702 free(igi, M_IGMP); 703 return; 704 } 705 } 706 707 #ifdef INVARIANTS 708 panic("%s: igmp_ifinfo not found for ifp %p\n", __func__, ifp); 709 #endif 710 } 711 712 /* 713 * Process a received IGMPv1 query. 714 * Return non-zero if the message should be dropped. 715 * 716 * VIMAGE: The curvnet pointer is derived from the input ifp. 717 */ 718 static int 719 igmp_input_v1_query(struct ifnet *ifp, const struct ip *ip) 720 { 721 INIT_VNET_INET(ifp->if_vnet); 722 struct ifmultiaddr *ifma; 723 struct igmp_ifinfo *igi; 724 struct in_multi *inm; 725 726 /* 727 * IGMPv1 General Queries SHOULD always addressed to 224.0.0.1. 728 * igmp_group is always ignored. Do not drop it as a userland 729 * daemon may wish to see it. 730 */ 731 if (!in_allhosts(ip->ip_dst)) { 732 IGMPSTAT_INC(igps_rcv_badqueries); 733 return (0); 734 } 735 736 IGMPSTAT_INC(igps_rcv_gen_queries); 737 738 /* 739 * Switch to IGMPv1 host compatibility mode. 740 */ 741 IN_MULTI_LOCK(); 742 IGMP_LOCK(); 743 744 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp; 745 KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp)); 746 747 if (igi->igi_flags & IGIF_LOOPBACK) { 748 CTR2(KTR_IGMPV3, "ignore v1 query on IGIF_LOOPBACK ifp %p(%s)", 749 ifp, ifp->if_xname); 750 goto out_locked; 751 } 752 753 igmp_set_version(igi, IGMP_VERSION_1); 754 755 CTR2(KTR_IGMPV3, "process v1 query on ifp %p(%s)", ifp, ifp->if_xname); 756 757 /* 758 * Start the timers in all of our group records 759 * for the interface on which the query arrived, 760 * except those which are already running. 761 */ 762 IF_ADDR_LOCK(ifp); 763 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 764 if (ifma->ifma_addr->sa_family != AF_INET || 765 ifma->ifma_protospec == NULL) 766 continue; 767 inm = (struct in_multi *)ifma->ifma_protospec; 768 if (inm->inm_timer != 0) 769 continue; 770 switch (inm->inm_state) { 771 case IGMP_NOT_MEMBER: 772 case IGMP_SILENT_MEMBER: 773 break; 774 case IGMP_G_QUERY_PENDING_MEMBER: 775 case IGMP_SG_QUERY_PENDING_MEMBER: 776 case IGMP_REPORTING_MEMBER: 777 case IGMP_IDLE_MEMBER: 778 case IGMP_LAZY_MEMBER: 779 case IGMP_SLEEPING_MEMBER: 780 case IGMP_AWAKENING_MEMBER: 781 inm->inm_state = IGMP_REPORTING_MEMBER; 782 inm->inm_timer = IGMP_RANDOM_DELAY( 783 IGMP_V1V2_MAX_RI * PR_FASTHZ); 784 V_current_state_timers_running = 1; 785 break; 786 case IGMP_LEAVING_MEMBER: 787 break; 788 } 789 } 790 IF_ADDR_UNLOCK(ifp); 791 792 out_locked: 793 IGMP_UNLOCK(); 794 IN_MULTI_UNLOCK(); 795 796 return (0); 797 } 798 799 /* 800 * Process a received IGMPv2 general or group-specific query. 801 */ 802 static int 803 igmp_input_v2_query(struct ifnet *ifp, const struct ip *ip, 804 const struct igmp *igmp) 805 { 806 struct ifmultiaddr *ifma; 807 struct igmp_ifinfo *igi; 808 struct in_multi *inm; 809 uint16_t timer; 810 811 /* 812 * Perform lazy allocation of IGMP link info if required, 813 * and switch to IGMPv2 host compatibility mode. 814 */ 815 IN_MULTI_LOCK(); 816 IGMP_LOCK(); 817 818 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp; 819 KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp)); 820 821 if (igi->igi_flags & IGIF_LOOPBACK) { 822 CTR2(KTR_IGMPV3, "ignore v2 query on IGIF_LOOPBACK ifp %p(%s)", 823 ifp, ifp->if_xname); 824 goto out_locked; 825 } 826 827 igmp_set_version(igi, IGMP_VERSION_2); 828 829 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE; 830 if (timer == 0) 831 timer = 1; 832 833 if (!in_nullhost(igmp->igmp_group)) { 834 /* 835 * IGMPv2 Group-Specific Query. 836 * If this is a group-specific IGMPv2 query, we need only 837 * look up the single group to process it. 838 */ 839 inm = inm_lookup(ifp, igmp->igmp_group); 840 if (inm != NULL) { 841 CTR3(KTR_IGMPV3, "process v2 query %s on ifp %p(%s)", 842 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname); 843 igmp_v2_update_group(inm, timer); 844 } 845 IGMPSTAT_INC(igps_rcv_group_queries); 846 } else { 847 /* 848 * IGMPv2 General Query. 849 * If this was not sent to the all-hosts group, ignore it. 850 */ 851 if (in_allhosts(ip->ip_dst)) { 852 /* 853 * For each reporting group joined on this 854 * interface, kick the report timer. 855 */ 856 CTR2(KTR_IGMPV3, 857 "process v2 general query on ifp %p(%s)", 858 ifp, ifp->if_xname); 859 860 IF_ADDR_LOCK(ifp); 861 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 862 if (ifma->ifma_addr->sa_family != AF_INET || 863 ifma->ifma_protospec == NULL) 864 continue; 865 inm = (struct in_multi *)ifma->ifma_protospec; 866 igmp_v2_update_group(inm, timer); 867 } 868 IF_ADDR_UNLOCK(ifp); 869 } 870 IGMPSTAT_INC(igps_rcv_gen_queries); 871 } 872 873 out_locked: 874 IGMP_UNLOCK(); 875 IN_MULTI_UNLOCK(); 876 877 return (0); 878 } 879 880 /* 881 * Update the report timer on a group in response to an IGMPv2 query. 882 * 883 * If we are becoming the reporting member for this group, start the timer. 884 * If we already are the reporting member for this group, and timer is 885 * below the threshold, reset it. 886 * 887 * We may be updating the group for the first time since we switched 888 * to IGMPv3. If we are, then we must clear any recorded source lists, 889 * and transition to REPORTING state; the group timer is overloaded 890 * for group and group-source query responses. 891 * 892 * Unlike IGMPv3, the delay per group should be jittered 893 * to avoid bursts of IGMPv2 reports. 894 */ 895 static void 896 igmp_v2_update_group(struct in_multi *inm, const int timer) 897 { 898 899 CTR4(KTR_IGMPV3, "%s: %s/%s timer=%d", __func__, 900 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname, timer); 901 902 IN_MULTI_LOCK_ASSERT(); 903 904 switch (inm->inm_state) { 905 case IGMP_NOT_MEMBER: 906 case IGMP_SILENT_MEMBER: 907 break; 908 case IGMP_REPORTING_MEMBER: 909 if (inm->inm_timer != 0 && 910 inm->inm_timer <= timer) { 911 CTR1(KTR_IGMPV3, "%s: REPORTING and timer running, " 912 "skipping.", __func__); 913 break; 914 } 915 /* FALLTHROUGH */ 916 case IGMP_SG_QUERY_PENDING_MEMBER: 917 case IGMP_G_QUERY_PENDING_MEMBER: 918 case IGMP_IDLE_MEMBER: 919 case IGMP_LAZY_MEMBER: 920 case IGMP_AWAKENING_MEMBER: 921 CTR1(KTR_IGMPV3, "%s: ->REPORTING", __func__); 922 inm->inm_state = IGMP_REPORTING_MEMBER; 923 inm->inm_timer = IGMP_RANDOM_DELAY(timer); 924 V_current_state_timers_running = 1; 925 break; 926 case IGMP_SLEEPING_MEMBER: 927 CTR1(KTR_IGMPV3, "%s: ->AWAKENING", __func__); 928 inm->inm_state = IGMP_AWAKENING_MEMBER; 929 break; 930 case IGMP_LEAVING_MEMBER: 931 break; 932 } 933 } 934 935 /* 936 * Process a received IGMPv3 general, group-specific or 937 * group-and-source-specific query. 938 * Assumes m has already been pulled up to the full IGMP message length. 939 * Return 0 if successful, otherwise an appropriate error code is returned. 940 */ 941 static int 942 igmp_input_v3_query(struct ifnet *ifp, const struct ip *ip, 943 /*const*/ struct igmpv3 *igmpv3) 944 { 945 struct igmp_ifinfo *igi; 946 struct in_multi *inm; 947 uint32_t maxresp, nsrc, qqi; 948 uint16_t timer; 949 uint8_t qrv; 950 951 CTR2(KTR_IGMPV3, "process v3 query on ifp %p(%s)", ifp, ifp->if_xname); 952 953 maxresp = igmpv3->igmp_code; /* in 1/10ths of a second */ 954 if (maxresp >= 128) { 955 maxresp = IGMP_MANT(igmpv3->igmp_code) << 956 (IGMP_EXP(igmpv3->igmp_code) + 3); 957 } 958 959 /* 960 * Robustness must never be less than 2 for on-wire IGMPv3. 961 * FIXME: Check if ifp has IGIF_LOOPBACK set, as we make 962 * an exception for interfaces whose IGMPv3 state changes 963 * are redirected to loopback (e.g. MANET). 964 */ 965 qrv = IGMP_QRV(igmpv3->igmp_misc); 966 if (qrv < 2) { 967 CTR3(KTR_IGMPV3, "%s: clamping qrv %d to %d", __func__, 968 qrv, IGMP_RV_INIT); 969 qrv = IGMP_RV_INIT; 970 } 971 972 qqi = igmpv3->igmp_qqi; 973 if (qqi >= 128) { 974 qqi = IGMP_MANT(igmpv3->igmp_qqi) << 975 (IGMP_EXP(igmpv3->igmp_qqi) + 3); 976 } 977 978 timer = maxresp * PR_FASTHZ / IGMP_TIMER_SCALE; 979 if (timer == 0) 980 timer = 1; 981 982 nsrc = ntohs(igmpv3->igmp_numsrc); 983 984 IN_MULTI_LOCK(); 985 IGMP_LOCK(); 986 987 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp; 988 KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp)); 989 990 if (igi->igi_flags & IGIF_LOOPBACK) { 991 CTR2(KTR_IGMPV3, "ignore v3 query on IGIF_LOOPBACK ifp %p(%s)", 992 ifp, ifp->if_xname); 993 goto out_locked; 994 } 995 996 igmp_set_version(igi, IGMP_VERSION_3); 997 998 igi->igi_rv = qrv; 999 igi->igi_qi = qqi; 1000 igi->igi_qri = maxresp; 1001 1002 CTR4(KTR_IGMPV3, "%s: qrv %d qi %d qri %d", __func__, qrv, qqi, 1003 maxresp); 1004 1005 if (in_nullhost(igmpv3->igmp_group)) { 1006 /* 1007 * IGMPv3 General Query. 1008 * Schedule a current-state report on this ifp for 1009 * all groups, possibly containing source lists. 1010 */ 1011 IGMPSTAT_INC(igps_rcv_gen_queries); 1012 1013 if (!in_allhosts(ip->ip_dst) || nsrc > 0) { 1014 /* 1015 * General Queries SHOULD be directed to 224.0.0.1. 1016 * A general query with a source list has undefined 1017 * behaviour; discard it. 1018 */ 1019 IGMPSTAT_INC(igps_rcv_badqueries); 1020 goto out_locked; 1021 } 1022 1023 CTR2(KTR_IGMPV3, "process v3 general query on ifp %p(%s)", 1024 ifp, ifp->if_xname); 1025 1026 /* 1027 * If there is a pending General Query response 1028 * scheduled earlier than the selected delay, do 1029 * not schedule any other reports. 1030 * Otherwise, reset the interface timer. 1031 */ 1032 if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer) { 1033 igi->igi_v3_timer = IGMP_RANDOM_DELAY(timer); 1034 V_interface_timers_running = 1; 1035 } 1036 } else { 1037 /* 1038 * IGMPv3 Group-specific or Group-and-source-specific Query. 1039 * 1040 * Group-source-specific queries are throttled on 1041 * a per-group basis to defeat denial-of-service attempts. 1042 * Queries for groups we are not a member of on this 1043 * link are simply ignored. 1044 */ 1045 inm = inm_lookup(ifp, igmpv3->igmp_group); 1046 if (inm == NULL) 1047 goto out_locked; 1048 if (nsrc > 0) { 1049 IGMPSTAT_INC(igps_rcv_gsr_queries); 1050 if (!ratecheck(&inm->inm_lastgsrtv, 1051 &V_igmp_gsrdelay)) { 1052 CTR1(KTR_IGMPV3, "%s: GS query throttled.", 1053 __func__); 1054 IGMPSTAT_INC(igps_drop_gsr_queries); 1055 goto out_locked; 1056 } 1057 } else { 1058 IGMPSTAT_INC(igps_rcv_group_queries); 1059 } 1060 CTR3(KTR_IGMPV3, "process v3 %s query on ifp %p(%s)", 1061 inet_ntoa(igmpv3->igmp_group), ifp, ifp->if_xname); 1062 /* 1063 * If there is a pending General Query response 1064 * scheduled sooner than the selected delay, no 1065 * further report need be scheduled. 1066 * Otherwise, prepare to respond to the 1067 * group-specific or group-and-source query. 1068 */ 1069 if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer) 1070 igmp_input_v3_group_query(inm, igi, timer, igmpv3); 1071 } 1072 1073 out_locked: 1074 IGMP_UNLOCK(); 1075 IN_MULTI_UNLOCK(); 1076 1077 return (0); 1078 } 1079 1080 /* 1081 * Process a recieved IGMPv3 group-specific or group-and-source-specific 1082 * query. 1083 * Return <0 if any error occured. Currently this is ignored. 1084 */ 1085 static int 1086 igmp_input_v3_group_query(struct in_multi *inm, struct igmp_ifinfo *igi, 1087 int timer, /*const*/ struct igmpv3 *igmpv3) 1088 { 1089 int retval; 1090 uint16_t nsrc; 1091 1092 IN_MULTI_LOCK_ASSERT(); 1093 IGMP_LOCK_ASSERT(); 1094 1095 retval = 0; 1096 1097 switch (inm->inm_state) { 1098 case IGMP_NOT_MEMBER: 1099 case IGMP_SILENT_MEMBER: 1100 case IGMP_SLEEPING_MEMBER: 1101 case IGMP_LAZY_MEMBER: 1102 case IGMP_AWAKENING_MEMBER: 1103 case IGMP_IDLE_MEMBER: 1104 case IGMP_LEAVING_MEMBER: 1105 return (retval); 1106 break; 1107 case IGMP_REPORTING_MEMBER: 1108 case IGMP_G_QUERY_PENDING_MEMBER: 1109 case IGMP_SG_QUERY_PENDING_MEMBER: 1110 break; 1111 } 1112 1113 nsrc = ntohs(igmpv3->igmp_numsrc); 1114 1115 /* 1116 * Deal with group-specific queries upfront. 1117 * If any group query is already pending, purge any recorded 1118 * source-list state if it exists, and schedule a query response 1119 * for this group-specific query. 1120 */ 1121 if (nsrc == 0) { 1122 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER || 1123 inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) { 1124 inm_clear_recorded(inm); 1125 timer = min(inm->inm_timer, timer); 1126 } 1127 inm->inm_state = IGMP_G_QUERY_PENDING_MEMBER; 1128 inm->inm_timer = IGMP_RANDOM_DELAY(timer); 1129 V_current_state_timers_running = 1; 1130 return (retval); 1131 } 1132 1133 /* 1134 * Deal with the case where a group-and-source-specific query has 1135 * been received but a group-specific query is already pending. 1136 */ 1137 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER) { 1138 timer = min(inm->inm_timer, timer); 1139 inm->inm_timer = IGMP_RANDOM_DELAY(timer); 1140 V_current_state_timers_running = 1; 1141 return (retval); 1142 } 1143 1144 /* 1145 * Finally, deal with the case where a group-and-source-specific 1146 * query has been received, where a response to a previous g-s-r 1147 * query exists, or none exists. 1148 * In this case, we need to parse the source-list which the Querier 1149 * has provided us with and check if we have any source list filter 1150 * entries at T1 for these sources. If we do not, there is no need 1151 * schedule a report and the query may be dropped. 1152 * If we do, we must record them and schedule a current-state 1153 * report for those sources. 1154 * FIXME: Handling source lists larger than 1 mbuf requires that 1155 * we pass the mbuf chain pointer down to this function, and use 1156 * m_getptr() to walk the chain. 1157 */ 1158 if (inm->inm_nsrc > 0) { 1159 const struct in_addr *ap; 1160 int i, nrecorded; 1161 1162 ap = (const struct in_addr *)(igmpv3 + 1); 1163 nrecorded = 0; 1164 for (i = 0; i < nsrc; i++, ap++) { 1165 retval = inm_record_source(inm, ap->s_addr); 1166 if (retval < 0) 1167 break; 1168 nrecorded += retval; 1169 } 1170 if (nrecorded > 0) { 1171 CTR1(KTR_IGMPV3, 1172 "%s: schedule response to SG query", __func__); 1173 inm->inm_state = IGMP_SG_QUERY_PENDING_MEMBER; 1174 inm->inm_timer = IGMP_RANDOM_DELAY(timer); 1175 V_current_state_timers_running = 1; 1176 } 1177 } 1178 1179 return (retval); 1180 } 1181 1182 /* 1183 * Process a received IGMPv1 host membership report. 1184 * 1185 * NOTE: 0.0.0.0 workaround breaks const correctness. 1186 */ 1187 static int 1188 igmp_input_v1_report(struct ifnet *ifp, /*const*/ struct ip *ip, 1189 /*const*/ struct igmp *igmp) 1190 { 1191 struct in_ifaddr *ia; 1192 struct in_multi *inm; 1193 1194 IGMPSTAT_INC(igps_rcv_reports); 1195 1196 if (ifp->if_flags & IFF_LOOPBACK) 1197 return (0); 1198 1199 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr) || 1200 !in_hosteq(igmp->igmp_group, ip->ip_dst))) { 1201 IGMPSTAT_INC(igps_rcv_badreports); 1202 return (EINVAL); 1203 } 1204 1205 /* 1206 * RFC 3376, Section 4.2.13, 9.2, 9.3: 1207 * Booting clients may use the source address 0.0.0.0. Some 1208 * IGMP daemons may not know how to use IP_RECVIF to determine 1209 * the interface upon which this message was received. 1210 * Replace 0.0.0.0 with the subnet address if told to do so. 1211 */ 1212 if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) { 1213 IFP_TO_IA(ifp, ia); 1214 if (ia != NULL) 1215 ip->ip_src.s_addr = htonl(ia->ia_subnet); 1216 } 1217 1218 CTR3(KTR_IGMPV3, "process v1 report %s on ifp %p(%s)", 1219 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname); 1220 1221 /* 1222 * IGMPv1 report suppression. 1223 * If we are a member of this group, and our membership should be 1224 * reported, stop our group timer and transition to the 'lazy' state. 1225 */ 1226 IN_MULTI_LOCK(); 1227 inm = inm_lookup(ifp, igmp->igmp_group); 1228 if (inm != NULL) { 1229 struct igmp_ifinfo *igi; 1230 1231 igi = inm->inm_igi; 1232 if (igi == NULL) { 1233 KASSERT(igi != NULL, 1234 ("%s: no igi for ifp %p", __func__, ifp)); 1235 goto out_locked; 1236 } 1237 1238 IGMPSTAT_INC(igps_rcv_ourreports); 1239 1240 /* 1241 * If we are in IGMPv3 host mode, do not allow the 1242 * other host's IGMPv1 report to suppress our reports 1243 * unless explicitly configured to do so. 1244 */ 1245 if (igi->igi_version == IGMP_VERSION_3) { 1246 if (V_igmp_legacysupp) 1247 igmp_v3_suppress_group_record(inm); 1248 goto out_locked; 1249 } 1250 1251 inm->inm_timer = 0; 1252 1253 switch (inm->inm_state) { 1254 case IGMP_NOT_MEMBER: 1255 case IGMP_SILENT_MEMBER: 1256 break; 1257 case IGMP_IDLE_MEMBER: 1258 case IGMP_LAZY_MEMBER: 1259 case IGMP_AWAKENING_MEMBER: 1260 CTR3(KTR_IGMPV3, 1261 "report suppressed for %s on ifp %p(%s)", 1262 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname); 1263 case IGMP_SLEEPING_MEMBER: 1264 inm->inm_state = IGMP_SLEEPING_MEMBER; 1265 break; 1266 case IGMP_REPORTING_MEMBER: 1267 CTR3(KTR_IGMPV3, 1268 "report suppressed for %s on ifp %p(%s)", 1269 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname); 1270 if (igi->igi_version == IGMP_VERSION_1) 1271 inm->inm_state = IGMP_LAZY_MEMBER; 1272 else if (igi->igi_version == IGMP_VERSION_2) 1273 inm->inm_state = IGMP_SLEEPING_MEMBER; 1274 break; 1275 case IGMP_G_QUERY_PENDING_MEMBER: 1276 case IGMP_SG_QUERY_PENDING_MEMBER: 1277 case IGMP_LEAVING_MEMBER: 1278 break; 1279 } 1280 } 1281 1282 out_locked: 1283 IN_MULTI_UNLOCK(); 1284 1285 return (0); 1286 } 1287 1288 /* 1289 * Process a received IGMPv2 host membership report. 1290 * 1291 * NOTE: 0.0.0.0 workaround breaks const correctness. 1292 */ 1293 static int 1294 igmp_input_v2_report(struct ifnet *ifp, /*const*/ struct ip *ip, 1295 /*const*/ struct igmp *igmp) 1296 { 1297 struct in_ifaddr *ia; 1298 struct in_multi *inm; 1299 1300 /* 1301 * Make sure we don't hear our own membership report. Fast 1302 * leave requires knowing that we are the only member of a 1303 * group. 1304 */ 1305 IFP_TO_IA(ifp, ia); 1306 if (ia != NULL && in_hosteq(ip->ip_src, IA_SIN(ia)->sin_addr)) 1307 return (0); 1308 1309 IGMPSTAT_INC(igps_rcv_reports); 1310 1311 if (ifp->if_flags & IFF_LOOPBACK) 1312 return (0); 1313 1314 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) || 1315 !in_hosteq(igmp->igmp_group, ip->ip_dst)) { 1316 IGMPSTAT_INC(igps_rcv_badreports); 1317 return (EINVAL); 1318 } 1319 1320 /* 1321 * RFC 3376, Section 4.2.13, 9.2, 9.3: 1322 * Booting clients may use the source address 0.0.0.0. Some 1323 * IGMP daemons may not know how to use IP_RECVIF to determine 1324 * the interface upon which this message was received. 1325 * Replace 0.0.0.0 with the subnet address if told to do so. 1326 */ 1327 if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) { 1328 if (ia != NULL) 1329 ip->ip_src.s_addr = htonl(ia->ia_subnet); 1330 } 1331 1332 CTR3(KTR_IGMPV3, "process v2 report %s on ifp %p(%s)", 1333 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname); 1334 1335 /* 1336 * IGMPv2 report suppression. 1337 * If we are a member of this group, and our membership should be 1338 * reported, and our group timer is pending or about to be reset, 1339 * stop our group timer by transitioning to the 'lazy' state. 1340 */ 1341 IN_MULTI_LOCK(); 1342 inm = inm_lookup(ifp, igmp->igmp_group); 1343 if (inm != NULL) { 1344 struct igmp_ifinfo *igi; 1345 1346 igi = inm->inm_igi; 1347 KASSERT(igi != NULL, ("%s: no igi for ifp %p", __func__, ifp)); 1348 1349 IGMPSTAT_INC(igps_rcv_ourreports); 1350 1351 /* 1352 * If we are in IGMPv3 host mode, do not allow the 1353 * other host's IGMPv1 report to suppress our reports 1354 * unless explicitly configured to do so. 1355 */ 1356 if (igi->igi_version == IGMP_VERSION_3) { 1357 if (V_igmp_legacysupp) 1358 igmp_v3_suppress_group_record(inm); 1359 goto out_locked; 1360 } 1361 1362 inm->inm_timer = 0; 1363 1364 switch (inm->inm_state) { 1365 case IGMP_NOT_MEMBER: 1366 case IGMP_SILENT_MEMBER: 1367 case IGMP_SLEEPING_MEMBER: 1368 break; 1369 case IGMP_REPORTING_MEMBER: 1370 case IGMP_IDLE_MEMBER: 1371 case IGMP_AWAKENING_MEMBER: 1372 CTR3(KTR_IGMPV3, 1373 "report suppressed for %s on ifp %p(%s)", 1374 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname); 1375 case IGMP_LAZY_MEMBER: 1376 inm->inm_state = IGMP_LAZY_MEMBER; 1377 break; 1378 case IGMP_G_QUERY_PENDING_MEMBER: 1379 case IGMP_SG_QUERY_PENDING_MEMBER: 1380 case IGMP_LEAVING_MEMBER: 1381 break; 1382 } 1383 } 1384 1385 out_locked: 1386 IN_MULTI_UNLOCK(); 1387 1388 return (0); 1389 } 1390 1391 void 1392 igmp_input(struct mbuf *m, int off) 1393 { 1394 int iphlen; 1395 struct ifnet *ifp; 1396 struct igmp *igmp; 1397 struct ip *ip; 1398 int igmplen; 1399 int minlen; 1400 int queryver; 1401 1402 CTR3(KTR_IGMPV3, "%s: called w/mbuf (%p,%d)", __func__, m, off); 1403 1404 ifp = m->m_pkthdr.rcvif; 1405 INIT_VNET_INET(ifp->if_vnet); 1406 1407 IGMPSTAT_INC(igps_rcv_total); 1408 1409 ip = mtod(m, struct ip *); 1410 iphlen = off; 1411 igmplen = ip->ip_len; 1412 1413 /* 1414 * Validate lengths. 1415 */ 1416 if (igmplen < IGMP_MINLEN) { 1417 IGMPSTAT_INC(igps_rcv_tooshort); 1418 m_freem(m); 1419 return; 1420 } 1421 1422 /* 1423 * Always pullup to the minimum size for v1/v2 or v3 1424 * to amortize calls to m_pullup(). 1425 */ 1426 minlen = iphlen; 1427 if (igmplen >= IGMP_V3_QUERY_MINLEN) 1428 minlen += IGMP_V3_QUERY_MINLEN; 1429 else 1430 minlen += IGMP_MINLEN; 1431 if ((m->m_flags & M_EXT || m->m_len < minlen) && 1432 (m = m_pullup(m, minlen)) == 0) { 1433 IGMPSTAT_INC(igps_rcv_tooshort); 1434 return; 1435 } 1436 ip = mtod(m, struct ip *); 1437 1438 if (ip->ip_ttl != 1) { 1439 IGMPSTAT_INC(igps_rcv_badttl); 1440 m_freem(m); 1441 return; 1442 } 1443 1444 /* 1445 * Validate checksum. 1446 */ 1447 m->m_data += iphlen; 1448 m->m_len -= iphlen; 1449 igmp = mtod(m, struct igmp *); 1450 if (in_cksum(m, igmplen)) { 1451 IGMPSTAT_INC(igps_rcv_badsum); 1452 m_freem(m); 1453 return; 1454 } 1455 m->m_data -= iphlen; 1456 m->m_len += iphlen; 1457 1458 switch (igmp->igmp_type) { 1459 case IGMP_HOST_MEMBERSHIP_QUERY: 1460 if (igmplen == IGMP_MINLEN) { 1461 if (igmp->igmp_code == 0) 1462 queryver = IGMP_VERSION_1; 1463 else 1464 queryver = IGMP_VERSION_2; 1465 } else if (igmplen >= IGMP_V3_QUERY_MINLEN) { 1466 queryver = IGMP_VERSION_3; 1467 } else { 1468 IGMPSTAT_INC(igps_rcv_tooshort); 1469 m_freem(m); 1470 return; 1471 } 1472 1473 switch (queryver) { 1474 case IGMP_VERSION_1: 1475 IGMPSTAT_INC(igps_rcv_v1v2_queries); 1476 if (!V_igmp_v1enable) 1477 break; 1478 if (igmp_input_v1_query(ifp, ip) != 0) { 1479 m_freem(m); 1480 return; 1481 } 1482 break; 1483 1484 case IGMP_VERSION_2: 1485 IGMPSTAT_INC(igps_rcv_v1v2_queries); 1486 if (!V_igmp_v2enable) 1487 break; 1488 if (igmp_input_v2_query(ifp, ip, igmp) != 0) { 1489 m_freem(m); 1490 return; 1491 } 1492 break; 1493 1494 case IGMP_VERSION_3: { 1495 struct igmpv3 *igmpv3; 1496 uint16_t igmpv3len; 1497 uint16_t srclen; 1498 int nsrc; 1499 1500 IGMPSTAT_INC(igps_rcv_v3_queries); 1501 igmpv3 = (struct igmpv3 *)igmp; 1502 /* 1503 * Validate length based on source count. 1504 */ 1505 nsrc = ntohs(igmpv3->igmp_numsrc); 1506 srclen = sizeof(struct in_addr) * nsrc; 1507 if (nsrc * sizeof(in_addr_t) > srclen) { 1508 IGMPSTAT_INC(igps_rcv_tooshort); 1509 return; 1510 } 1511 /* 1512 * m_pullup() may modify m, so pullup in 1513 * this scope. 1514 */ 1515 igmpv3len = iphlen + IGMP_V3_QUERY_MINLEN + 1516 srclen; 1517 if ((m->m_flags & M_EXT || 1518 m->m_len < igmpv3len) && 1519 (m = m_pullup(m, igmpv3len)) == NULL) { 1520 IGMPSTAT_INC(igps_rcv_tooshort); 1521 return; 1522 } 1523 igmpv3 = (struct igmpv3 *)(mtod(m, uint8_t *) 1524 + iphlen); 1525 if (igmp_input_v3_query(ifp, ip, igmpv3) != 0) { 1526 m_freem(m); 1527 return; 1528 } 1529 } 1530 break; 1531 } 1532 break; 1533 1534 case IGMP_v1_HOST_MEMBERSHIP_REPORT: 1535 if (!V_igmp_v1enable) 1536 break; 1537 if (igmp_input_v1_report(ifp, ip, igmp) != 0) { 1538 m_freem(m); 1539 return; 1540 } 1541 break; 1542 1543 case IGMP_v2_HOST_MEMBERSHIP_REPORT: 1544 if (!V_igmp_v2enable) 1545 break; 1546 if (!ip_checkrouteralert(m)) 1547 IGMPSTAT_INC(igps_rcv_nora); 1548 if (igmp_input_v2_report(ifp, ip, igmp) != 0) { 1549 m_freem(m); 1550 return; 1551 } 1552 break; 1553 1554 case IGMP_v3_HOST_MEMBERSHIP_REPORT: 1555 /* 1556 * Hosts do not need to process IGMPv3 membership reports, 1557 * as report suppression is no longer required. 1558 */ 1559 if (!ip_checkrouteralert(m)) 1560 IGMPSTAT_INC(igps_rcv_nora); 1561 break; 1562 1563 default: 1564 break; 1565 } 1566 1567 /* 1568 * Pass all valid IGMP packets up to any process(es) listening on a 1569 * raw IGMP socket. 1570 */ 1571 rip_input(m, off); 1572 } 1573 1574 1575 /* 1576 * Fast timeout handler (global). 1577 * VIMAGE: Timeout handlers are expected to service all vimages. 1578 */ 1579 void 1580 igmp_fasttimo(void) 1581 { 1582 #ifdef VIMAGE 1583 VNET_ITERATOR_DECL(vnet_iter); 1584 1585 VNET_LIST_RLOCK(); 1586 VNET_FOREACH(vnet_iter) { 1587 CURVNET_SET(vnet_iter); 1588 INIT_VNET_INET(vnet_iter); 1589 igmp_fasttimo_vnet(); 1590 CURVNET_RESTORE(); 1591 } 1592 VNET_LIST_RUNLOCK(); 1593 #else /* !VIMAGE */ 1594 1595 igmp_fasttimo_vnet(); 1596 #endif /* VIMAGE */ 1597 } 1598 1599 /* 1600 * Fast timeout handler (per-vnet). 1601 * Sends are shuffled off to a netisr to deal with Giant. 1602 * 1603 * VIMAGE: Assume caller has set up our curvnet. 1604 */ 1605 static void 1606 igmp_fasttimo_vnet(void) 1607 { 1608 struct ifqueue scq; /* State-change packets */ 1609 struct ifqueue qrq; /* Query response packets */ 1610 struct ifnet *ifp; 1611 struct igmp_ifinfo *igi; 1612 struct ifmultiaddr *ifma, *tifma; 1613 struct in_multi *inm; 1614 int loop, uri_fasthz; 1615 1616 loop = 0; 1617 uri_fasthz = 0; 1618 1619 /* 1620 * Quick check to see if any work needs to be done, in order to 1621 * minimize the overhead of fasttimo processing. 1622 * SMPng: XXX Unlocked reads. 1623 */ 1624 if (!V_current_state_timers_running && 1625 !V_interface_timers_running && 1626 !V_state_change_timers_running) 1627 return; 1628 1629 IN_MULTI_LOCK(); 1630 IGMP_LOCK(); 1631 1632 /* 1633 * IGMPv3 General Query response timer processing. 1634 */ 1635 if (V_interface_timers_running) { 1636 CTR1(KTR_IGMPV3, "%s: interface timers running", __func__); 1637 1638 V_interface_timers_running = 0; 1639 LIST_FOREACH(igi, &V_igi_head, igi_link) { 1640 if (igi->igi_v3_timer == 0) { 1641 /* Do nothing. */ 1642 } else if (--igi->igi_v3_timer == 0) { 1643 igmp_v3_dispatch_general_query(igi); 1644 } else { 1645 V_interface_timers_running = 1; 1646 } 1647 } 1648 } 1649 1650 if (!V_current_state_timers_running && 1651 !V_state_change_timers_running) 1652 goto out_locked; 1653 1654 V_current_state_timers_running = 0; 1655 V_state_change_timers_running = 0; 1656 1657 CTR1(KTR_IGMPV3, "%s: state change timers running", __func__); 1658 1659 /* 1660 * IGMPv1/v2/v3 host report and state-change timer processing. 1661 * Note: Processing a v3 group timer may remove a node. 1662 */ 1663 LIST_FOREACH(igi, &V_igi_head, igi_link) { 1664 ifp = igi->igi_ifp; 1665 1666 if (igi->igi_version == IGMP_VERSION_3) { 1667 loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0; 1668 uri_fasthz = IGMP_RANDOM_DELAY(igi->igi_uri * 1669 PR_FASTHZ); 1670 1671 memset(&qrq, 0, sizeof(struct ifqueue)); 1672 IFQ_SET_MAXLEN(&qrq, IGMP_MAX_G_GS_PACKETS); 1673 1674 memset(&scq, 0, sizeof(struct ifqueue)); 1675 IFQ_SET_MAXLEN(&scq, IGMP_MAX_STATE_CHANGE_PACKETS); 1676 } 1677 1678 IF_ADDR_LOCK(ifp); 1679 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, 1680 tifma) { 1681 if (ifma->ifma_addr->sa_family != AF_INET || 1682 ifma->ifma_protospec == NULL) 1683 continue; 1684 inm = (struct in_multi *)ifma->ifma_protospec; 1685 switch (igi->igi_version) { 1686 case IGMP_VERSION_1: 1687 case IGMP_VERSION_2: 1688 igmp_v1v2_process_group_timer(inm, 1689 igi->igi_version); 1690 break; 1691 case IGMP_VERSION_3: 1692 igmp_v3_process_group_timers(igi, &qrq, 1693 &scq, inm, uri_fasthz); 1694 break; 1695 } 1696 } 1697 IF_ADDR_UNLOCK(ifp); 1698 1699 if (igi->igi_version == IGMP_VERSION_3) { 1700 struct in_multi *tinm; 1701 1702 igmp_dispatch_queue(&qrq, 0, loop); 1703 igmp_dispatch_queue(&scq, 0, loop); 1704 1705 /* 1706 * Free the in_multi reference(s) for this 1707 * IGMP lifecycle. 1708 */ 1709 SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, 1710 inm_nrele, tinm) { 1711 SLIST_REMOVE_HEAD(&igi->igi_relinmhead, 1712 inm_nrele); 1713 inm_release_locked(inm); 1714 } 1715 } 1716 } 1717 1718 out_locked: 1719 IGMP_UNLOCK(); 1720 IN_MULTI_UNLOCK(); 1721 } 1722 1723 /* 1724 * Update host report group timer for IGMPv1/v2. 1725 * Will update the global pending timer flags. 1726 */ 1727 static void 1728 igmp_v1v2_process_group_timer(struct in_multi *inm, const int version) 1729 { 1730 int report_timer_expired; 1731 1732 IN_MULTI_LOCK_ASSERT(); 1733 IGMP_LOCK_ASSERT(); 1734 1735 if (inm->inm_timer == 0) { 1736 report_timer_expired = 0; 1737 } else if (--inm->inm_timer == 0) { 1738 report_timer_expired = 1; 1739 } else { 1740 V_current_state_timers_running = 1; 1741 return; 1742 } 1743 1744 switch (inm->inm_state) { 1745 case IGMP_NOT_MEMBER: 1746 case IGMP_SILENT_MEMBER: 1747 case IGMP_IDLE_MEMBER: 1748 case IGMP_LAZY_MEMBER: 1749 case IGMP_SLEEPING_MEMBER: 1750 case IGMP_AWAKENING_MEMBER: 1751 break; 1752 case IGMP_REPORTING_MEMBER: 1753 if (report_timer_expired) { 1754 inm->inm_state = IGMP_IDLE_MEMBER; 1755 (void)igmp_v1v2_queue_report(inm, 1756 (version == IGMP_VERSION_2) ? 1757 IGMP_v2_HOST_MEMBERSHIP_REPORT : 1758 IGMP_v1_HOST_MEMBERSHIP_REPORT); 1759 } 1760 break; 1761 case IGMP_G_QUERY_PENDING_MEMBER: 1762 case IGMP_SG_QUERY_PENDING_MEMBER: 1763 case IGMP_LEAVING_MEMBER: 1764 break; 1765 } 1766 } 1767 1768 /* 1769 * Update a group's timers for IGMPv3. 1770 * Will update the global pending timer flags. 1771 * Note: Unlocked read from igi. 1772 */ 1773 static void 1774 igmp_v3_process_group_timers(struct igmp_ifinfo *igi, 1775 struct ifqueue *qrq, struct ifqueue *scq, 1776 struct in_multi *inm, const int uri_fasthz) 1777 { 1778 int query_response_timer_expired; 1779 int state_change_retransmit_timer_expired; 1780 1781 IN_MULTI_LOCK_ASSERT(); 1782 IGMP_LOCK_ASSERT(); 1783 1784 query_response_timer_expired = 0; 1785 state_change_retransmit_timer_expired = 0; 1786 1787 /* 1788 * During a transition from v1/v2 compatibility mode back to v3, 1789 * a group record in REPORTING state may still have its group 1790 * timer active. This is a no-op in this function; it is easier 1791 * to deal with it here than to complicate the slow-timeout path. 1792 */ 1793 if (inm->inm_timer == 0) { 1794 query_response_timer_expired = 0; 1795 } else if (--inm->inm_timer == 0) { 1796 query_response_timer_expired = 1; 1797 } else { 1798 V_current_state_timers_running = 1; 1799 } 1800 1801 if (inm->inm_sctimer == 0) { 1802 state_change_retransmit_timer_expired = 0; 1803 } else if (--inm->inm_sctimer == 0) { 1804 state_change_retransmit_timer_expired = 1; 1805 } else { 1806 V_state_change_timers_running = 1; 1807 } 1808 1809 /* We are in fasttimo, so be quick about it. */ 1810 if (!state_change_retransmit_timer_expired && 1811 !query_response_timer_expired) 1812 return; 1813 1814 switch (inm->inm_state) { 1815 case IGMP_NOT_MEMBER: 1816 case IGMP_SILENT_MEMBER: 1817 case IGMP_SLEEPING_MEMBER: 1818 case IGMP_LAZY_MEMBER: 1819 case IGMP_AWAKENING_MEMBER: 1820 case IGMP_IDLE_MEMBER: 1821 break; 1822 case IGMP_G_QUERY_PENDING_MEMBER: 1823 case IGMP_SG_QUERY_PENDING_MEMBER: 1824 /* 1825 * Respond to a previously pending Group-Specific 1826 * or Group-and-Source-Specific query by enqueueing 1827 * the appropriate Current-State report for 1828 * immediate transmission. 1829 */ 1830 if (query_response_timer_expired) { 1831 int retval; 1832 1833 retval = igmp_v3_enqueue_group_record(qrq, inm, 0, 1, 1834 (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)); 1835 CTR2(KTR_IGMPV3, "%s: enqueue record = %d", 1836 __func__, retval); 1837 inm->inm_state = IGMP_REPORTING_MEMBER; 1838 /* XXX Clear recorded sources for next time. */ 1839 inm_clear_recorded(inm); 1840 } 1841 /* FALLTHROUGH */ 1842 case IGMP_REPORTING_MEMBER: 1843 case IGMP_LEAVING_MEMBER: 1844 if (state_change_retransmit_timer_expired) { 1845 /* 1846 * State-change retransmission timer fired. 1847 * If there are any further pending retransmissions, 1848 * set the global pending state-change flag, and 1849 * reset the timer. 1850 */ 1851 if (--inm->inm_scrv > 0) { 1852 inm->inm_sctimer = uri_fasthz; 1853 V_state_change_timers_running = 1; 1854 } 1855 /* 1856 * Retransmit the previously computed state-change 1857 * report. If there are no further pending 1858 * retransmissions, the mbuf queue will be consumed. 1859 * Update T0 state to T1 as we have now sent 1860 * a state-change. 1861 */ 1862 (void)igmp_v3_merge_state_changes(inm, scq); 1863 1864 inm_commit(inm); 1865 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__, 1866 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname); 1867 1868 /* 1869 * If we are leaving the group for good, make sure 1870 * we release IGMP's reference to it. 1871 * This release must be deferred using a SLIST, 1872 * as we are called from a loop which traverses 1873 * the in_ifmultiaddr TAILQ. 1874 */ 1875 if (inm->inm_state == IGMP_LEAVING_MEMBER && 1876 inm->inm_scrv == 0) { 1877 inm->inm_state = IGMP_NOT_MEMBER; 1878 SLIST_INSERT_HEAD(&igi->igi_relinmhead, 1879 inm, inm_nrele); 1880 } 1881 } 1882 break; 1883 } 1884 } 1885 1886 1887 /* 1888 * Suppress a group's pending response to a group or source/group query. 1889 * 1890 * Do NOT suppress state changes. This leads to IGMPv3 inconsistency. 1891 * Do NOT update ST1/ST0 as this operation merely suppresses 1892 * the currently pending group record. 1893 * Do NOT suppress the response to a general query. It is possible but 1894 * it would require adding another state or flag. 1895 */ 1896 static void 1897 igmp_v3_suppress_group_record(struct in_multi *inm) 1898 { 1899 1900 IN_MULTI_LOCK_ASSERT(); 1901 1902 KASSERT(inm->inm_igi->igi_version == IGMP_VERSION_3, 1903 ("%s: not IGMPv3 mode on link", __func__)); 1904 1905 if (inm->inm_state != IGMP_G_QUERY_PENDING_MEMBER || 1906 inm->inm_state != IGMP_SG_QUERY_PENDING_MEMBER) 1907 return; 1908 1909 if (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) 1910 inm_clear_recorded(inm); 1911 1912 inm->inm_timer = 0; 1913 inm->inm_state = IGMP_REPORTING_MEMBER; 1914 } 1915 1916 /* 1917 * Switch to a different IGMP version on the given interface, 1918 * as per Section 7.2.1. 1919 */ 1920 static void 1921 igmp_set_version(struct igmp_ifinfo *igi, const int version) 1922 { 1923 1924 IGMP_LOCK_ASSERT(); 1925 1926 CTR4(KTR_IGMPV3, "%s: switching to v%d on ifp %p(%s)", __func__, 1927 version, igi->igi_ifp, igi->igi_ifp->if_xname); 1928 1929 if (version == IGMP_VERSION_1 || version == IGMP_VERSION_2) { 1930 int old_version_timer; 1931 /* 1932 * Compute the "Older Version Querier Present" timer as per 1933 * Section 8.12. 1934 */ 1935 old_version_timer = igi->igi_rv * igi->igi_qi + igi->igi_qri; 1936 old_version_timer *= PR_SLOWHZ; 1937 1938 if (version == IGMP_VERSION_1) { 1939 igi->igi_v1_timer = old_version_timer; 1940 igi->igi_v2_timer = 0; 1941 } else if (version == IGMP_VERSION_2) { 1942 igi->igi_v1_timer = 0; 1943 igi->igi_v2_timer = old_version_timer; 1944 } 1945 } 1946 1947 if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) { 1948 if (igi->igi_version != IGMP_VERSION_2) { 1949 igi->igi_version = IGMP_VERSION_2; 1950 igmp_v3_cancel_link_timers(igi); 1951 } 1952 } else if (igi->igi_v1_timer > 0) { 1953 if (igi->igi_version != IGMP_VERSION_1) { 1954 igi->igi_version = IGMP_VERSION_1; 1955 igmp_v3_cancel_link_timers(igi); 1956 } 1957 } 1958 } 1959 1960 /* 1961 * Cancel pending IGMPv3 timers for the given link and all groups 1962 * joined on it; state-change, general-query, and group-query timers. 1963 */ 1964 static void 1965 igmp_v3_cancel_link_timers(struct igmp_ifinfo *igi) 1966 { 1967 struct ifmultiaddr *ifma; 1968 struct ifnet *ifp; 1969 struct in_multi *inm; 1970 1971 CTR3(KTR_IGMPV3, "%s: cancel v3 timers on ifp %p(%s)", __func__, 1972 igi->igi_ifp, igi->igi_ifp->if_xname); 1973 1974 IN_MULTI_LOCK_ASSERT(); 1975 IGMP_LOCK_ASSERT(); 1976 1977 /* 1978 * Fast-track this potentially expensive operation 1979 * by checking all the global 'timer pending' flags. 1980 */ 1981 if (!V_interface_timers_running && 1982 !V_state_change_timers_running && 1983 !V_current_state_timers_running) 1984 return; 1985 1986 igi->igi_v3_timer = 0; 1987 1988 ifp = igi->igi_ifp; 1989 1990 IF_ADDR_LOCK(ifp); 1991 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1992 if (ifma->ifma_addr->sa_family != AF_INET) 1993 continue; 1994 inm = (struct in_multi *)ifma->ifma_protospec; 1995 switch (inm->inm_state) { 1996 case IGMP_NOT_MEMBER: 1997 case IGMP_SILENT_MEMBER: 1998 case IGMP_IDLE_MEMBER: 1999 case IGMP_LAZY_MEMBER: 2000 case IGMP_SLEEPING_MEMBER: 2001 case IGMP_AWAKENING_MEMBER: 2002 break; 2003 case IGMP_LEAVING_MEMBER: 2004 /* 2005 * If we are leaving the group and switching 2006 * IGMP version, we need to release the final 2007 * reference held for issuing the INCLUDE {}. 2008 * 2009 * SMPNG: Must drop and re-acquire IF_ADDR_LOCK 2010 * around inm_release_locked(), as it is not 2011 * a recursive mutex. 2012 */ 2013 IF_ADDR_UNLOCK(ifp); 2014 inm_release_locked(inm); 2015 IF_ADDR_LOCK(ifp); 2016 /* FALLTHROUGH */ 2017 case IGMP_G_QUERY_PENDING_MEMBER: 2018 case IGMP_SG_QUERY_PENDING_MEMBER: 2019 inm_clear_recorded(inm); 2020 /* FALLTHROUGH */ 2021 case IGMP_REPORTING_MEMBER: 2022 inm->inm_sctimer = 0; 2023 inm->inm_timer = 0; 2024 inm->inm_state = IGMP_REPORTING_MEMBER; 2025 /* 2026 * Free any pending IGMPv3 state-change records. 2027 */ 2028 _IF_DRAIN(&inm->inm_scq); 2029 break; 2030 } 2031 } 2032 IF_ADDR_UNLOCK(ifp); 2033 } 2034 2035 /* 2036 * Update the Older Version Querier Present timers for a link. 2037 * See Section 7.2.1 of RFC 3376. 2038 */ 2039 static void 2040 igmp_v1v2_process_querier_timers(struct igmp_ifinfo *igi) 2041 { 2042 2043 IGMP_LOCK_ASSERT(); 2044 2045 if (igi->igi_v1_timer == 0 && igi->igi_v2_timer == 0) { 2046 /* 2047 * IGMPv1 and IGMPv2 Querier Present timers expired. 2048 * 2049 * Revert to IGMPv3. 2050 */ 2051 if (igi->igi_version != IGMP_VERSION_3) { 2052 CTR5(KTR_IGMPV3, 2053 "%s: transition from v%d -> v%d on %p(%s)", 2054 __func__, igi->igi_version, IGMP_VERSION_3, 2055 igi->igi_ifp, igi->igi_ifp->if_xname); 2056 igi->igi_version = IGMP_VERSION_3; 2057 } 2058 } else if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) { 2059 /* 2060 * IGMPv1 Querier Present timer expired, 2061 * IGMPv2 Querier Present timer running. 2062 * If IGMPv2 was disabled since last timeout, 2063 * revert to IGMPv3. 2064 * If IGMPv2 is enabled, revert to IGMPv2. 2065 */ 2066 if (!V_igmp_v2enable) { 2067 CTR5(KTR_IGMPV3, 2068 "%s: transition from v%d -> v%d on %p(%s)", 2069 __func__, igi->igi_version, IGMP_VERSION_3, 2070 igi->igi_ifp, igi->igi_ifp->if_xname); 2071 igi->igi_v2_timer = 0; 2072 igi->igi_version = IGMP_VERSION_3; 2073 } else { 2074 --igi->igi_v2_timer; 2075 if (igi->igi_version != IGMP_VERSION_2) { 2076 CTR5(KTR_IGMPV3, 2077 "%s: transition from v%d -> v%d on %p(%s)", 2078 __func__, igi->igi_version, IGMP_VERSION_2, 2079 igi->igi_ifp, igi->igi_ifp->if_xname); 2080 igi->igi_version = IGMP_VERSION_2; 2081 } 2082 } 2083 } else if (igi->igi_v1_timer > 0) { 2084 /* 2085 * IGMPv1 Querier Present timer running. 2086 * Stop IGMPv2 timer if running. 2087 * 2088 * If IGMPv1 was disabled since last timeout, 2089 * revert to IGMPv3. 2090 * If IGMPv1 is enabled, reset IGMPv2 timer if running. 2091 */ 2092 if (!V_igmp_v1enable) { 2093 CTR5(KTR_IGMPV3, 2094 "%s: transition from v%d -> v%d on %p(%s)", 2095 __func__, igi->igi_version, IGMP_VERSION_3, 2096 igi->igi_ifp, igi->igi_ifp->if_xname); 2097 igi->igi_v1_timer = 0; 2098 igi->igi_version = IGMP_VERSION_3; 2099 } else { 2100 --igi->igi_v1_timer; 2101 } 2102 if (igi->igi_v2_timer > 0) { 2103 CTR3(KTR_IGMPV3, 2104 "%s: cancel v2 timer on %p(%s)", 2105 __func__, igi->igi_ifp, igi->igi_ifp->if_xname); 2106 igi->igi_v2_timer = 0; 2107 } 2108 } 2109 } 2110 2111 /* 2112 * Global slowtimo handler. 2113 * VIMAGE: Timeout handlers are expected to service all vimages. 2114 */ 2115 void 2116 igmp_slowtimo(void) 2117 { 2118 #ifdef VIMAGE 2119 VNET_ITERATOR_DECL(vnet_iter); 2120 2121 VNET_LIST_RLOCK(); 2122 VNET_FOREACH(vnet_iter) { 2123 CURVNET_SET(vnet_iter); 2124 INIT_VNET_INET(vnet_iter); 2125 igmp_slowtimo_vnet(); 2126 CURVNET_RESTORE(); 2127 } 2128 VNET_LIST_RUNLOCK(); 2129 #else /* !VIMAGE */ 2130 igmp_slowtimo_vnet(); 2131 #endif /* VIMAGE */ 2132 } 2133 2134 /* 2135 * Per-vnet slowtimo handler. 2136 */ 2137 static void 2138 igmp_slowtimo_vnet(void) 2139 { 2140 struct igmp_ifinfo *igi; 2141 2142 IGMP_LOCK(); 2143 2144 LIST_FOREACH(igi, &V_igi_head, igi_link) { 2145 igmp_v1v2_process_querier_timers(igi); 2146 } 2147 2148 IGMP_UNLOCK(); 2149 } 2150 2151 /* 2152 * Dispatch an IGMPv1/v2 host report or leave message. 2153 * These are always small enough to fit inside a single mbuf. 2154 */ 2155 static int 2156 igmp_v1v2_queue_report(struct in_multi *inm, const int type) 2157 { 2158 struct ifnet *ifp; 2159 struct igmp *igmp; 2160 struct ip *ip; 2161 struct mbuf *m; 2162 2163 IN_MULTI_LOCK_ASSERT(); 2164 IGMP_LOCK_ASSERT(); 2165 2166 ifp = inm->inm_ifp; 2167 /* XXX are these needed ? */ 2168 INIT_VNET_NET(ifp->if_vnet); 2169 INIT_VNET_INET(ifp->if_vnet); 2170 2171 MGETHDR(m, M_DONTWAIT, MT_DATA); 2172 if (m == NULL) 2173 return (ENOMEM); 2174 MH_ALIGN(m, sizeof(struct ip) + sizeof(struct igmp)); 2175 2176 m->m_pkthdr.len = sizeof(struct ip) + sizeof(struct igmp); 2177 2178 m->m_data += sizeof(struct ip); 2179 m->m_len = sizeof(struct igmp); 2180 2181 igmp = mtod(m, struct igmp *); 2182 igmp->igmp_type = type; 2183 igmp->igmp_code = 0; 2184 igmp->igmp_group = inm->inm_addr; 2185 igmp->igmp_cksum = 0; 2186 igmp->igmp_cksum = in_cksum(m, sizeof(struct igmp)); 2187 2188 m->m_data -= sizeof(struct ip); 2189 m->m_len += sizeof(struct ip); 2190 2191 ip = mtod(m, struct ip *); 2192 ip->ip_tos = 0; 2193 ip->ip_len = sizeof(struct ip) + sizeof(struct igmp); 2194 ip->ip_off = 0; 2195 ip->ip_p = IPPROTO_IGMP; 2196 ip->ip_src.s_addr = INADDR_ANY; 2197 2198 if (type == IGMP_HOST_LEAVE_MESSAGE) 2199 ip->ip_dst.s_addr = htonl(INADDR_ALLRTRS_GROUP); 2200 else 2201 ip->ip_dst = inm->inm_addr; 2202 2203 igmp_save_context(m, ifp); 2204 2205 m->m_flags |= M_IGMPV2; 2206 if (inm->inm_igi->igi_flags & IGIF_LOOPBACK) 2207 m->m_flags |= M_IGMP_LOOP; 2208 2209 CTR2(KTR_IGMPV3, "%s: netisr_dispatch(NETISR_IGMP, %p)", __func__, m); 2210 netisr_dispatch(NETISR_IGMP, m); 2211 2212 return (0); 2213 } 2214 2215 /* 2216 * Process a state change from the upper layer for the given IPv4 group. 2217 * 2218 * Each socket holds a reference on the in_multi in its own ip_moptions. 2219 * The socket layer will have made the necessary updates to.the group 2220 * state, it is now up to IGMP to issue a state change report if there 2221 * has been any change between T0 (when the last state-change was issued) 2222 * and T1 (now). 2223 * 2224 * We use the IGMPv3 state machine at group level. The IGMP module 2225 * however makes the decision as to which IGMP protocol version to speak. 2226 * A state change *from* INCLUDE {} always means an initial join. 2227 * A state change *to* INCLUDE {} always means a final leave. 2228 * 2229 * FUTURE: If IGIF_V3LITE is enabled for this interface, then we can 2230 * save ourselves a bunch of work; any exclusive mode groups need not 2231 * compute source filter lists. 2232 * 2233 * VIMAGE: curvnet should have been set by caller, as this routine 2234 * is called from the socket option handlers. 2235 */ 2236 int 2237 igmp_change_state(struct in_multi *inm) 2238 { 2239 struct igmp_ifinfo *igi; 2240 struct ifnet *ifp; 2241 int error; 2242 2243 IN_MULTI_LOCK_ASSERT(); 2244 2245 error = 0; 2246 2247 /* 2248 * Try to detect if the upper layer just asked us to change state 2249 * for an interface which has now gone away. 2250 */ 2251 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__)); 2252 ifp = inm->inm_ifma->ifma_ifp; 2253 if (ifp != NULL) { 2254 /* 2255 * Sanity check that netinet's notion of ifp is the 2256 * same as net's. 2257 */ 2258 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__)); 2259 } 2260 2261 IGMP_LOCK(); 2262 2263 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp; 2264 KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp)); 2265 2266 /* 2267 * If we detect a state transition to or from MCAST_UNDEFINED 2268 * for this group, then we are starting or finishing an IGMP 2269 * life cycle for this group. 2270 */ 2271 if (inm->inm_st[1].iss_fmode != inm->inm_st[0].iss_fmode) { 2272 CTR3(KTR_IGMPV3, "%s: inm transition %d -> %d", __func__, 2273 inm->inm_st[0].iss_fmode, inm->inm_st[1].iss_fmode); 2274 if (inm->inm_st[0].iss_fmode == MCAST_UNDEFINED) { 2275 CTR1(KTR_IGMPV3, "%s: initial join", __func__); 2276 error = igmp_initial_join(inm, igi); 2277 goto out_locked; 2278 } else if (inm->inm_st[1].iss_fmode == MCAST_UNDEFINED) { 2279 CTR1(KTR_IGMPV3, "%s: final leave", __func__); 2280 igmp_final_leave(inm, igi); 2281 goto out_locked; 2282 } 2283 } else { 2284 CTR1(KTR_IGMPV3, "%s: filter set change", __func__); 2285 } 2286 2287 error = igmp_handle_state_change(inm, igi); 2288 2289 out_locked: 2290 IGMP_UNLOCK(); 2291 return (error); 2292 } 2293 2294 /* 2295 * Perform the initial join for an IGMP group. 2296 * 2297 * When joining a group: 2298 * If the group should have its IGMP traffic suppressed, do nothing. 2299 * IGMPv1 starts sending IGMPv1 host membership reports. 2300 * IGMPv2 starts sending IGMPv2 host membership reports. 2301 * IGMPv3 will schedule an IGMPv3 state-change report containing the 2302 * initial state of the membership. 2303 */ 2304 static int 2305 igmp_initial_join(struct in_multi *inm, struct igmp_ifinfo *igi) 2306 { 2307 struct ifnet *ifp; 2308 struct ifqueue *ifq; 2309 int error, retval, syncstates; 2310 2311 CTR4(KTR_IGMPV3, "%s: initial join %s on ifp %p(%s)", 2312 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp, 2313 inm->inm_ifp->if_xname); 2314 2315 error = 0; 2316 syncstates = 1; 2317 2318 ifp = inm->inm_ifp; 2319 2320 IN_MULTI_LOCK_ASSERT(); 2321 IGMP_LOCK_ASSERT(); 2322 2323 KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__)); 2324 2325 /* 2326 * Groups joined on loopback or marked as 'not reported', 2327 * e.g. 224.0.0.1, enter the IGMP_SILENT_MEMBER state and 2328 * are never reported in any IGMP protocol exchanges. 2329 * All other groups enter the appropriate IGMP state machine 2330 * for the version in use on this link. 2331 * A link marked as IGIF_SILENT causes IGMP to be completely 2332 * disabled for the link. 2333 */ 2334 if ((ifp->if_flags & IFF_LOOPBACK) || 2335 (igi->igi_flags & IGIF_SILENT) || 2336 !igmp_isgroupreported(inm->inm_addr)) { 2337 CTR1(KTR_IGMPV3, 2338 "%s: not kicking state machine for silent group", __func__); 2339 inm->inm_state = IGMP_SILENT_MEMBER; 2340 inm->inm_timer = 0; 2341 } else { 2342 /* 2343 * Deal with overlapping in_multi lifecycle. 2344 * If this group was LEAVING, then make sure 2345 * we drop the reference we picked up to keep the 2346 * group around for the final INCLUDE {} enqueue. 2347 */ 2348 if (igi->igi_version == IGMP_VERSION_3 && 2349 inm->inm_state == IGMP_LEAVING_MEMBER) 2350 inm_release_locked(inm); 2351 2352 inm->inm_state = IGMP_REPORTING_MEMBER; 2353 2354 switch (igi->igi_version) { 2355 case IGMP_VERSION_1: 2356 case IGMP_VERSION_2: 2357 inm->inm_state = IGMP_IDLE_MEMBER; 2358 error = igmp_v1v2_queue_report(inm, 2359 (igi->igi_version == IGMP_VERSION_2) ? 2360 IGMP_v2_HOST_MEMBERSHIP_REPORT : 2361 IGMP_v1_HOST_MEMBERSHIP_REPORT); 2362 if (error == 0) { 2363 inm->inm_timer = IGMP_RANDOM_DELAY( 2364 IGMP_V1V2_MAX_RI * PR_FASTHZ); 2365 V_current_state_timers_running = 1; 2366 } 2367 break; 2368 2369 case IGMP_VERSION_3: 2370 /* 2371 * Defer update of T0 to T1, until the first copy 2372 * of the state change has been transmitted. 2373 */ 2374 syncstates = 0; 2375 2376 /* 2377 * Immediately enqueue a State-Change Report for 2378 * this interface, freeing any previous reports. 2379 * Don't kick the timers if there is nothing to do, 2380 * or if an error occurred. 2381 */ 2382 ifq = &inm->inm_scq; 2383 _IF_DRAIN(ifq); 2384 retval = igmp_v3_enqueue_group_record(ifq, inm, 1, 2385 0, 0); 2386 CTR2(KTR_IGMPV3, "%s: enqueue record = %d", 2387 __func__, retval); 2388 if (retval <= 0) { 2389 error = retval * -1; 2390 break; 2391 } 2392 2393 /* 2394 * Schedule transmission of pending state-change 2395 * report up to RV times for this link. The timer 2396 * will fire at the next igmp_fasttimo (~200ms), 2397 * giving us an opportunity to merge the reports. 2398 */ 2399 if (igi->igi_flags & IGIF_LOOPBACK) { 2400 inm->inm_scrv = 1; 2401 } else { 2402 KASSERT(igi->igi_rv > 1, 2403 ("%s: invalid robustness %d", __func__, 2404 igi->igi_rv)); 2405 inm->inm_scrv = igi->igi_rv; 2406 } 2407 inm->inm_sctimer = 1; 2408 V_state_change_timers_running = 1; 2409 2410 error = 0; 2411 break; 2412 } 2413 } 2414 2415 /* 2416 * Only update the T0 state if state change is atomic, 2417 * i.e. we don't need to wait for a timer to fire before we 2418 * can consider the state change to have been communicated. 2419 */ 2420 if (syncstates) { 2421 inm_commit(inm); 2422 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__, 2423 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname); 2424 } 2425 2426 return (error); 2427 } 2428 2429 /* 2430 * Issue an intermediate state change during the IGMP life-cycle. 2431 */ 2432 static int 2433 igmp_handle_state_change(struct in_multi *inm, struct igmp_ifinfo *igi) 2434 { 2435 struct ifnet *ifp; 2436 int retval; 2437 2438 CTR4(KTR_IGMPV3, "%s: state change for %s on ifp %p(%s)", 2439 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp, 2440 inm->inm_ifp->if_xname); 2441 2442 ifp = inm->inm_ifp; 2443 2444 IN_MULTI_LOCK_ASSERT(); 2445 IGMP_LOCK_ASSERT(); 2446 2447 KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__)); 2448 2449 if ((ifp->if_flags & IFF_LOOPBACK) || 2450 (igi->igi_flags & IGIF_SILENT) || 2451 !igmp_isgroupreported(inm->inm_addr) || 2452 (igi->igi_version != IGMP_VERSION_3)) { 2453 if (!igmp_isgroupreported(inm->inm_addr)) { 2454 CTR1(KTR_IGMPV3, 2455 "%s: not kicking state machine for silent group", __func__); 2456 } 2457 CTR1(KTR_IGMPV3, "%s: nothing to do", __func__); 2458 inm_commit(inm); 2459 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__, 2460 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname); 2461 return (0); 2462 } 2463 2464 _IF_DRAIN(&inm->inm_scq); 2465 2466 retval = igmp_v3_enqueue_group_record(&inm->inm_scq, inm, 1, 0, 0); 2467 CTR2(KTR_IGMPV3, "%s: enqueue record = %d", __func__, retval); 2468 if (retval <= 0) 2469 return (-retval); 2470 2471 /* 2472 * If record(s) were enqueued, start the state-change 2473 * report timer for this group. 2474 */ 2475 inm->inm_scrv = ((igi->igi_flags & IGIF_LOOPBACK) ? 1 : igi->igi_rv); 2476 inm->inm_sctimer = 1; 2477 V_state_change_timers_running = 1; 2478 2479 return (0); 2480 } 2481 2482 /* 2483 * Perform the final leave for an IGMP group. 2484 * 2485 * When leaving a group: 2486 * IGMPv1 does nothing. 2487 * IGMPv2 sends a host leave message, if and only if we are the reporter. 2488 * IGMPv3 enqueues a state-change report containing a transition 2489 * to INCLUDE {} for immediate transmission. 2490 */ 2491 static void 2492 igmp_final_leave(struct in_multi *inm, struct igmp_ifinfo *igi) 2493 { 2494 int syncstates; 2495 2496 syncstates = 1; 2497 2498 CTR4(KTR_IGMPV3, "%s: final leave %s on ifp %p(%s)", 2499 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp, 2500 inm->inm_ifp->if_xname); 2501 2502 IN_MULTI_LOCK_ASSERT(); 2503 IGMP_LOCK_ASSERT(); 2504 2505 switch (inm->inm_state) { 2506 case IGMP_NOT_MEMBER: 2507 case IGMP_SILENT_MEMBER: 2508 case IGMP_LEAVING_MEMBER: 2509 /* Already leaving or left; do nothing. */ 2510 CTR1(KTR_IGMPV3, 2511 "%s: not kicking state machine for silent group", __func__); 2512 break; 2513 case IGMP_REPORTING_MEMBER: 2514 case IGMP_IDLE_MEMBER: 2515 case IGMP_G_QUERY_PENDING_MEMBER: 2516 case IGMP_SG_QUERY_PENDING_MEMBER: 2517 if (igi->igi_version == IGMP_VERSION_2) { 2518 #ifdef INVARIANTS 2519 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER || 2520 inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) 2521 panic("%s: IGMPv3 state reached, not IGMPv3 mode", 2522 __func__); 2523 #endif 2524 igmp_v1v2_queue_report(inm, IGMP_HOST_LEAVE_MESSAGE); 2525 inm->inm_state = IGMP_NOT_MEMBER; 2526 } else if (igi->igi_version == IGMP_VERSION_3) { 2527 /* 2528 * Stop group timer and all pending reports. 2529 * Immediately enqueue a state-change report 2530 * TO_IN {} to be sent on the next fast timeout, 2531 * giving us an opportunity to merge reports. 2532 */ 2533 _IF_DRAIN(&inm->inm_scq); 2534 inm->inm_timer = 0; 2535 if (igi->igi_flags & IGIF_LOOPBACK) { 2536 inm->inm_scrv = 1; 2537 } else { 2538 inm->inm_scrv = igi->igi_rv; 2539 } 2540 CTR4(KTR_IGMPV3, "%s: Leaving %s/%s with %d " 2541 "pending retransmissions.", __func__, 2542 inet_ntoa(inm->inm_addr), 2543 inm->inm_ifp->if_xname, inm->inm_scrv); 2544 if (inm->inm_scrv == 0) { 2545 inm->inm_state = IGMP_NOT_MEMBER; 2546 inm->inm_sctimer = 0; 2547 } else { 2548 int retval; 2549 2550 inm_acquire_locked(inm); 2551 2552 retval = igmp_v3_enqueue_group_record( 2553 &inm->inm_scq, inm, 1, 0, 0); 2554 KASSERT(retval != 0, 2555 ("%s: enqueue record = %d", __func__, 2556 retval)); 2557 2558 inm->inm_state = IGMP_LEAVING_MEMBER; 2559 inm->inm_sctimer = 1; 2560 V_state_change_timers_running = 1; 2561 syncstates = 0; 2562 } 2563 break; 2564 } 2565 break; 2566 case IGMP_LAZY_MEMBER: 2567 case IGMP_SLEEPING_MEMBER: 2568 case IGMP_AWAKENING_MEMBER: 2569 /* Our reports are suppressed; do nothing. */ 2570 break; 2571 } 2572 2573 if (syncstates) { 2574 inm_commit(inm); 2575 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__, 2576 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname); 2577 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED; 2578 CTR3(KTR_IGMPV3, "%s: T1 now MCAST_UNDEFINED for %s/%s", 2579 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname); 2580 } 2581 } 2582 2583 /* 2584 * Enqueue an IGMPv3 group record to the given output queue. 2585 * 2586 * XXX This function could do with having the allocation code 2587 * split out, and the multiple-tree-walks coalesced into a single 2588 * routine as has been done in igmp_v3_enqueue_filter_change(). 2589 * 2590 * If is_state_change is zero, a current-state record is appended. 2591 * If is_state_change is non-zero, a state-change report is appended. 2592 * 2593 * If is_group_query is non-zero, an mbuf packet chain is allocated. 2594 * If is_group_query is zero, and if there is a packet with free space 2595 * at the tail of the queue, it will be appended to providing there 2596 * is enough free space. 2597 * Otherwise a new mbuf packet chain is allocated. 2598 * 2599 * If is_source_query is non-zero, each source is checked to see if 2600 * it was recorded for a Group-Source query, and will be omitted if 2601 * it is not both in-mode and recorded. 2602 * 2603 * The function will attempt to allocate leading space in the packet 2604 * for the IP/IGMP header to be prepended without fragmenting the chain. 2605 * 2606 * If successful the size of all data appended to the queue is returned, 2607 * otherwise an error code less than zero is returned, or zero if 2608 * no record(s) were appended. 2609 */ 2610 static int 2611 igmp_v3_enqueue_group_record(struct ifqueue *ifq, struct in_multi *inm, 2612 const int is_state_change, const int is_group_query, 2613 const int is_source_query) 2614 { 2615 struct igmp_grouprec ig; 2616 struct igmp_grouprec *pig; 2617 struct ifnet *ifp; 2618 struct ip_msource *ims, *nims; 2619 struct mbuf *m0, *m, *md; 2620 int error, is_filter_list_change; 2621 int minrec0len, m0srcs, msrcs, nbytes, off; 2622 int record_has_sources; 2623 int now; 2624 int type; 2625 in_addr_t naddr; 2626 uint8_t mode; 2627 2628 IN_MULTI_LOCK_ASSERT(); 2629 2630 error = 0; 2631 ifp = inm->inm_ifp; 2632 is_filter_list_change = 0; 2633 m = NULL; 2634 m0 = NULL; 2635 m0srcs = 0; 2636 msrcs = 0; 2637 nbytes = 0; 2638 nims = NULL; 2639 record_has_sources = 1; 2640 pig = NULL; 2641 type = IGMP_DO_NOTHING; 2642 mode = inm->inm_st[1].iss_fmode; 2643 2644 /* 2645 * If we did not transition out of ASM mode during t0->t1, 2646 * and there are no source nodes to process, we can skip 2647 * the generation of source records. 2648 */ 2649 if (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0 && 2650 inm->inm_nsrc == 0) 2651 record_has_sources = 0; 2652 2653 if (is_state_change) { 2654 /* 2655 * Queue a state change record. 2656 * If the mode did not change, and there are non-ASM 2657 * listeners or source filters present, 2658 * we potentially need to issue two records for the group. 2659 * If we are transitioning to MCAST_UNDEFINED, we need 2660 * not send any sources. 2661 * If there are ASM listeners, and there was no filter 2662 * mode transition of any kind, do nothing. 2663 */ 2664 if (mode != inm->inm_st[0].iss_fmode) { 2665 if (mode == MCAST_EXCLUDE) { 2666 CTR1(KTR_IGMPV3, "%s: change to EXCLUDE", 2667 __func__); 2668 type = IGMP_CHANGE_TO_EXCLUDE_MODE; 2669 } else { 2670 CTR1(KTR_IGMPV3, "%s: change to INCLUDE", 2671 __func__); 2672 type = IGMP_CHANGE_TO_INCLUDE_MODE; 2673 if (mode == MCAST_UNDEFINED) 2674 record_has_sources = 0; 2675 } 2676 } else { 2677 if (record_has_sources) { 2678 is_filter_list_change = 1; 2679 } else { 2680 type = IGMP_DO_NOTHING; 2681 } 2682 } 2683 } else { 2684 /* 2685 * Queue a current state record. 2686 */ 2687 if (mode == MCAST_EXCLUDE) { 2688 type = IGMP_MODE_IS_EXCLUDE; 2689 } else if (mode == MCAST_INCLUDE) { 2690 type = IGMP_MODE_IS_INCLUDE; 2691 KASSERT(inm->inm_st[1].iss_asm == 0, 2692 ("%s: inm %p is INCLUDE but ASM count is %d", 2693 __func__, inm, inm->inm_st[1].iss_asm)); 2694 } 2695 } 2696 2697 /* 2698 * Generate the filter list changes using a separate function. 2699 */ 2700 if (is_filter_list_change) 2701 return (igmp_v3_enqueue_filter_change(ifq, inm)); 2702 2703 if (type == IGMP_DO_NOTHING) { 2704 CTR3(KTR_IGMPV3, "%s: nothing to do for %s/%s", 2705 __func__, inet_ntoa(inm->inm_addr), 2706 inm->inm_ifp->if_xname); 2707 return (0); 2708 } 2709 2710 /* 2711 * If any sources are present, we must be able to fit at least 2712 * one in the trailing space of the tail packet's mbuf, 2713 * ideally more. 2714 */ 2715 minrec0len = sizeof(struct igmp_grouprec); 2716 if (record_has_sources) 2717 minrec0len += sizeof(in_addr_t); 2718 2719 CTR4(KTR_IGMPV3, "%s: queueing %s for %s/%s", __func__, 2720 igmp_rec_type_to_str(type), inet_ntoa(inm->inm_addr), 2721 inm->inm_ifp->if_xname); 2722 2723 /* 2724 * Check if we have a packet in the tail of the queue for this 2725 * group into which the first group record for this group will fit. 2726 * Otherwise allocate a new packet. 2727 * Always allocate leading space for IP+RA_OPT+IGMP+REPORT. 2728 * Note: Group records for G/GSR query responses MUST be sent 2729 * in their own packet. 2730 */ 2731 m0 = ifq->ifq_tail; 2732 if (!is_group_query && 2733 m0 != NULL && 2734 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= IGMP_V3_REPORT_MAXRECS) && 2735 (m0->m_pkthdr.len + minrec0len) < 2736 (ifp->if_mtu - IGMP_LEADINGSPACE)) { 2737 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len - 2738 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t); 2739 m = m0; 2740 CTR1(KTR_IGMPV3, "%s: use existing packet", __func__); 2741 } else { 2742 if (_IF_QFULL(ifq)) { 2743 CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__); 2744 return (-ENOMEM); 2745 } 2746 m = NULL; 2747 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE - 2748 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t); 2749 if (!is_state_change && !is_group_query) { 2750 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 2751 if (m) 2752 m->m_data += IGMP_LEADINGSPACE; 2753 } 2754 if (m == NULL) { 2755 m = m_gethdr(M_DONTWAIT, MT_DATA); 2756 if (m) 2757 MH_ALIGN(m, IGMP_LEADINGSPACE); 2758 } 2759 if (m == NULL) 2760 return (-ENOMEM); 2761 2762 igmp_save_context(m, ifp); 2763 2764 CTR1(KTR_IGMPV3, "%s: allocated first packet", __func__); 2765 } 2766 2767 /* 2768 * Append group record. 2769 * If we have sources, we don't know how many yet. 2770 */ 2771 ig.ig_type = type; 2772 ig.ig_datalen = 0; 2773 ig.ig_numsrc = 0; 2774 ig.ig_group = inm->inm_addr; 2775 if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) { 2776 if (m != m0) 2777 m_freem(m); 2778 CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__); 2779 return (-ENOMEM); 2780 } 2781 nbytes += sizeof(struct igmp_grouprec); 2782 2783 /* 2784 * Append as many sources as will fit in the first packet. 2785 * If we are appending to a new packet, the chain allocation 2786 * may potentially use clusters; use m_getptr() in this case. 2787 * If we are appending to an existing packet, we need to obtain 2788 * a pointer to the group record after m_append(), in case a new 2789 * mbuf was allocated. 2790 * Only append sources which are in-mode at t1. If we are 2791 * transitioning to MCAST_UNDEFINED state on the group, do not 2792 * include source entries. 2793 * Only report recorded sources in our filter set when responding 2794 * to a group-source query. 2795 */ 2796 if (record_has_sources) { 2797 if (m == m0) { 2798 md = m_last(m); 2799 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + 2800 md->m_len - nbytes); 2801 } else { 2802 md = m_getptr(m, 0, &off); 2803 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + 2804 off); 2805 } 2806 msrcs = 0; 2807 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, nims) { 2808 CTR2(KTR_IGMPV3, "%s: visit node %s", __func__, 2809 inet_ntoa_haddr(ims->ims_haddr)); 2810 now = ims_get_mode(inm, ims, 1); 2811 CTR2(KTR_IGMPV3, "%s: node is %d", __func__, now); 2812 if ((now != mode) || 2813 (now == mode && mode == MCAST_UNDEFINED)) { 2814 CTR1(KTR_IGMPV3, "%s: skip node", __func__); 2815 continue; 2816 } 2817 if (is_source_query && ims->ims_stp == 0) { 2818 CTR1(KTR_IGMPV3, "%s: skip unrecorded node", 2819 __func__); 2820 continue; 2821 } 2822 CTR1(KTR_IGMPV3, "%s: append node", __func__); 2823 naddr = htonl(ims->ims_haddr); 2824 if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) { 2825 if (m != m0) 2826 m_freem(m); 2827 CTR1(KTR_IGMPV3, "%s: m_append() failed.", 2828 __func__); 2829 return (-ENOMEM); 2830 } 2831 nbytes += sizeof(in_addr_t); 2832 ++msrcs; 2833 if (msrcs == m0srcs) 2834 break; 2835 } 2836 CTR2(KTR_IGMPV3, "%s: msrcs is %d this packet", __func__, 2837 msrcs); 2838 pig->ig_numsrc = htons(msrcs); 2839 nbytes += (msrcs * sizeof(in_addr_t)); 2840 } 2841 2842 if (is_source_query && msrcs == 0) { 2843 CTR1(KTR_IGMPV3, "%s: no recorded sources to report", __func__); 2844 if (m != m0) 2845 m_freem(m); 2846 return (0); 2847 } 2848 2849 /* 2850 * We are good to go with first packet. 2851 */ 2852 if (m != m0) { 2853 CTR1(KTR_IGMPV3, "%s: enqueueing first packet", __func__); 2854 m->m_pkthdr.PH_vt.vt_nrecs = 1; 2855 _IF_ENQUEUE(ifq, m); 2856 } else 2857 m->m_pkthdr.PH_vt.vt_nrecs++; 2858 2859 /* 2860 * No further work needed if no source list in packet(s). 2861 */ 2862 if (!record_has_sources) 2863 return (nbytes); 2864 2865 /* 2866 * Whilst sources remain to be announced, we need to allocate 2867 * a new packet and fill out as many sources as will fit. 2868 * Always try for a cluster first. 2869 */ 2870 while (nims != NULL) { 2871 if (_IF_QFULL(ifq)) { 2872 CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__); 2873 return (-ENOMEM); 2874 } 2875 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 2876 if (m) 2877 m->m_data += IGMP_LEADINGSPACE; 2878 if (m == NULL) { 2879 m = m_gethdr(M_DONTWAIT, MT_DATA); 2880 if (m) 2881 MH_ALIGN(m, IGMP_LEADINGSPACE); 2882 } 2883 if (m == NULL) 2884 return (-ENOMEM); 2885 igmp_save_context(m, ifp); 2886 md = m_getptr(m, 0, &off); 2887 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + off); 2888 CTR1(KTR_IGMPV3, "%s: allocated next packet", __func__); 2889 2890 if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) { 2891 if (m != m0) 2892 m_freem(m); 2893 CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__); 2894 return (-ENOMEM); 2895 } 2896 m->m_pkthdr.PH_vt.vt_nrecs = 1; 2897 nbytes += sizeof(struct igmp_grouprec); 2898 2899 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE - 2900 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t); 2901 2902 msrcs = 0; 2903 RB_FOREACH_FROM(ims, ip_msource_tree, nims) { 2904 CTR2(KTR_IGMPV3, "%s: visit node %s", __func__, 2905 inet_ntoa_haddr(ims->ims_haddr)); 2906 now = ims_get_mode(inm, ims, 1); 2907 if ((now != mode) || 2908 (now == mode && mode == MCAST_UNDEFINED)) { 2909 CTR1(KTR_IGMPV3, "%s: skip node", __func__); 2910 continue; 2911 } 2912 if (is_source_query && ims->ims_stp == 0) { 2913 CTR1(KTR_IGMPV3, "%s: skip unrecorded node", 2914 __func__); 2915 continue; 2916 } 2917 CTR1(KTR_IGMPV3, "%s: append node", __func__); 2918 naddr = htonl(ims->ims_haddr); 2919 if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) { 2920 if (m != m0) 2921 m_freem(m); 2922 CTR1(KTR_IGMPV3, "%s: m_append() failed.", 2923 __func__); 2924 return (-ENOMEM); 2925 } 2926 ++msrcs; 2927 if (msrcs == m0srcs) 2928 break; 2929 } 2930 pig->ig_numsrc = htons(msrcs); 2931 nbytes += (msrcs * sizeof(in_addr_t)); 2932 2933 CTR1(KTR_IGMPV3, "%s: enqueueing next packet", __func__); 2934 _IF_ENQUEUE(ifq, m); 2935 } 2936 2937 return (nbytes); 2938 } 2939 2940 /* 2941 * Type used to mark record pass completion. 2942 * We exploit the fact we can cast to this easily from the 2943 * current filter modes on each ip_msource node. 2944 */ 2945 typedef enum { 2946 REC_NONE = 0x00, /* MCAST_UNDEFINED */ 2947 REC_ALLOW = 0x01, /* MCAST_INCLUDE */ 2948 REC_BLOCK = 0x02, /* MCAST_EXCLUDE */ 2949 REC_FULL = REC_ALLOW | REC_BLOCK 2950 } rectype_t; 2951 2952 /* 2953 * Enqueue an IGMPv3 filter list change to the given output queue. 2954 * 2955 * Source list filter state is held in an RB-tree. When the filter list 2956 * for a group is changed without changing its mode, we need to compute 2957 * the deltas between T0 and T1 for each source in the filter set, 2958 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records. 2959 * 2960 * As we may potentially queue two record types, and the entire R-B tree 2961 * needs to be walked at once, we break this out into its own function 2962 * so we can generate a tightly packed queue of packets. 2963 * 2964 * XXX This could be written to only use one tree walk, although that makes 2965 * serializing into the mbuf chains a bit harder. For now we do two walks 2966 * which makes things easier on us, and it may or may not be harder on 2967 * the L2 cache. 2968 * 2969 * If successful the size of all data appended to the queue is returned, 2970 * otherwise an error code less than zero is returned, or zero if 2971 * no record(s) were appended. 2972 */ 2973 static int 2974 igmp_v3_enqueue_filter_change(struct ifqueue *ifq, struct in_multi *inm) 2975 { 2976 static const int MINRECLEN = 2977 sizeof(struct igmp_grouprec) + sizeof(in_addr_t); 2978 struct ifnet *ifp; 2979 struct igmp_grouprec ig; 2980 struct igmp_grouprec *pig; 2981 struct ip_msource *ims, *nims; 2982 struct mbuf *m, *m0, *md; 2983 in_addr_t naddr; 2984 int m0srcs, nbytes, off, rsrcs, schanged; 2985 int nallow, nblock; 2986 uint8_t mode, now, then; 2987 rectype_t crt, drt, nrt; 2988 2989 IN_MULTI_LOCK_ASSERT(); 2990 2991 if (inm->inm_nsrc == 0 || 2992 (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0)) 2993 return (0); 2994 2995 ifp = inm->inm_ifp; /* interface */ 2996 mode = inm->inm_st[1].iss_fmode; /* filter mode at t1 */ 2997 crt = REC_NONE; /* current group record type */ 2998 drt = REC_NONE; /* mask of completed group record types */ 2999 nrt = REC_NONE; /* record type for current node */ 3000 m0srcs = 0; /* # source which will fit in current mbuf chain */ 3001 nbytes = 0; /* # of bytes appended to group's state-change queue */ 3002 rsrcs = 0; /* # sources encoded in current record */ 3003 schanged = 0; /* # nodes encoded in overall filter change */ 3004 nallow = 0; /* # of source entries in ALLOW_NEW */ 3005 nblock = 0; /* # of source entries in BLOCK_OLD */ 3006 nims = NULL; /* next tree node pointer */ 3007 3008 /* 3009 * For each possible filter record mode. 3010 * The first kind of source we encounter tells us which 3011 * is the first kind of record we start appending. 3012 * If a node transitioned to UNDEFINED at t1, its mode is treated 3013 * as the inverse of the group's filter mode. 3014 */ 3015 while (drt != REC_FULL) { 3016 do { 3017 m0 = ifq->ifq_tail; 3018 if (m0 != NULL && 3019 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= 3020 IGMP_V3_REPORT_MAXRECS) && 3021 (m0->m_pkthdr.len + MINRECLEN) < 3022 (ifp->if_mtu - IGMP_LEADINGSPACE)) { 3023 m = m0; 3024 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len - 3025 sizeof(struct igmp_grouprec)) / 3026 sizeof(in_addr_t); 3027 CTR1(KTR_IGMPV3, 3028 "%s: use previous packet", __func__); 3029 } else { 3030 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 3031 if (m) 3032 m->m_data += IGMP_LEADINGSPACE; 3033 if (m == NULL) { 3034 m = m_gethdr(M_DONTWAIT, MT_DATA); 3035 if (m) 3036 MH_ALIGN(m, IGMP_LEADINGSPACE); 3037 } 3038 if (m == NULL) { 3039 CTR1(KTR_IGMPV3, 3040 "%s: m_get*() failed", __func__); 3041 return (-ENOMEM); 3042 } 3043 m->m_pkthdr.PH_vt.vt_nrecs = 0; 3044 igmp_save_context(m, ifp); 3045 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE - 3046 sizeof(struct igmp_grouprec)) / 3047 sizeof(in_addr_t); 3048 CTR1(KTR_IGMPV3, 3049 "%s: allocated new packet", __func__); 3050 } 3051 /* 3052 * Append the IGMP group record header to the 3053 * current packet's data area. 3054 * Recalculate pointer to free space for next 3055 * group record, in case m_append() allocated 3056 * a new mbuf or cluster. 3057 */ 3058 memset(&ig, 0, sizeof(ig)); 3059 ig.ig_group = inm->inm_addr; 3060 if (!m_append(m, sizeof(ig), (void *)&ig)) { 3061 if (m != m0) 3062 m_freem(m); 3063 CTR1(KTR_IGMPV3, 3064 "%s: m_append() failed", __func__); 3065 return (-ENOMEM); 3066 } 3067 nbytes += sizeof(struct igmp_grouprec); 3068 if (m == m0) { 3069 md = m_last(m); 3070 pig = (struct igmp_grouprec *)(mtod(md, 3071 uint8_t *) + md->m_len - nbytes); 3072 } else { 3073 md = m_getptr(m, 0, &off); 3074 pig = (struct igmp_grouprec *)(mtod(md, 3075 uint8_t *) + off); 3076 } 3077 /* 3078 * Begin walking the tree for this record type 3079 * pass, or continue from where we left off 3080 * previously if we had to allocate a new packet. 3081 * Only report deltas in-mode at t1. 3082 * We need not report included sources as allowed 3083 * if we are in inclusive mode on the group, 3084 * however the converse is not true. 3085 */ 3086 rsrcs = 0; 3087 if (nims == NULL) 3088 nims = RB_MIN(ip_msource_tree, &inm->inm_srcs); 3089 RB_FOREACH_FROM(ims, ip_msource_tree, nims) { 3090 CTR2(KTR_IGMPV3, "%s: visit node %s", 3091 __func__, inet_ntoa_haddr(ims->ims_haddr)); 3092 now = ims_get_mode(inm, ims, 1); 3093 then = ims_get_mode(inm, ims, 0); 3094 CTR3(KTR_IGMPV3, "%s: mode: t0 %d, t1 %d", 3095 __func__, then, now); 3096 if (now == then) { 3097 CTR1(KTR_IGMPV3, 3098 "%s: skip unchanged", __func__); 3099 continue; 3100 } 3101 if (mode == MCAST_EXCLUDE && 3102 now == MCAST_INCLUDE) { 3103 CTR1(KTR_IGMPV3, 3104 "%s: skip IN src on EX group", 3105 __func__); 3106 continue; 3107 } 3108 nrt = (rectype_t)now; 3109 if (nrt == REC_NONE) 3110 nrt = (rectype_t)(~mode & REC_FULL); 3111 if (schanged++ == 0) { 3112 crt = nrt; 3113 } else if (crt != nrt) 3114 continue; 3115 naddr = htonl(ims->ims_haddr); 3116 if (!m_append(m, sizeof(in_addr_t), 3117 (void *)&naddr)) { 3118 if (m != m0) 3119 m_freem(m); 3120 CTR1(KTR_IGMPV3, 3121 "%s: m_append() failed", __func__); 3122 return (-ENOMEM); 3123 } 3124 nallow += !!(crt == REC_ALLOW); 3125 nblock += !!(crt == REC_BLOCK); 3126 if (++rsrcs == m0srcs) 3127 break; 3128 } 3129 /* 3130 * If we did not append any tree nodes on this 3131 * pass, back out of allocations. 3132 */ 3133 if (rsrcs == 0) { 3134 nbytes -= sizeof(struct igmp_grouprec); 3135 if (m != m0) { 3136 CTR1(KTR_IGMPV3, 3137 "%s: m_free(m)", __func__); 3138 m_freem(m); 3139 } else { 3140 CTR1(KTR_IGMPV3, 3141 "%s: m_adj(m, -ig)", __func__); 3142 m_adj(m, -((int)sizeof( 3143 struct igmp_grouprec))); 3144 } 3145 continue; 3146 } 3147 nbytes += (rsrcs * sizeof(in_addr_t)); 3148 if (crt == REC_ALLOW) 3149 pig->ig_type = IGMP_ALLOW_NEW_SOURCES; 3150 else if (crt == REC_BLOCK) 3151 pig->ig_type = IGMP_BLOCK_OLD_SOURCES; 3152 pig->ig_numsrc = htons(rsrcs); 3153 /* 3154 * Count the new group record, and enqueue this 3155 * packet if it wasn't already queued. 3156 */ 3157 m->m_pkthdr.PH_vt.vt_nrecs++; 3158 if (m != m0) 3159 _IF_ENQUEUE(ifq, m); 3160 } while (nims != NULL); 3161 drt |= crt; 3162 crt = (~crt & REC_FULL); 3163 } 3164 3165 CTR3(KTR_IGMPV3, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__, 3166 nallow, nblock); 3167 3168 return (nbytes); 3169 } 3170 3171 static int 3172 igmp_v3_merge_state_changes(struct in_multi *inm, struct ifqueue *ifscq) 3173 { 3174 struct ifqueue *gq; 3175 struct mbuf *m; /* pending state-change */ 3176 struct mbuf *m0; /* copy of pending state-change */ 3177 struct mbuf *mt; /* last state-change in packet */ 3178 int docopy, domerge; 3179 u_int recslen; 3180 3181 docopy = 0; 3182 domerge = 0; 3183 recslen = 0; 3184 3185 IN_MULTI_LOCK_ASSERT(); 3186 IGMP_LOCK_ASSERT(); 3187 3188 /* 3189 * If there are further pending retransmissions, make a writable 3190 * copy of each queued state-change message before merging. 3191 */ 3192 if (inm->inm_scrv > 0) 3193 docopy = 1; 3194 3195 gq = &inm->inm_scq; 3196 #ifdef KTR 3197 if (gq->ifq_head == NULL) { 3198 CTR2(KTR_IGMPV3, "%s: WARNING: queue for inm %p is empty", 3199 __func__, inm); 3200 } 3201 #endif 3202 3203 m = gq->ifq_head; 3204 while (m != NULL) { 3205 /* 3206 * Only merge the report into the current packet if 3207 * there is sufficient space to do so; an IGMPv3 report 3208 * packet may only contain 65,535 group records. 3209 * Always use a simple mbuf chain concatentation to do this, 3210 * as large state changes for single groups may have 3211 * allocated clusters. 3212 */ 3213 domerge = 0; 3214 mt = ifscq->ifq_tail; 3215 if (mt != NULL) { 3216 recslen = m_length(m, NULL); 3217 3218 if ((mt->m_pkthdr.PH_vt.vt_nrecs + 3219 m->m_pkthdr.PH_vt.vt_nrecs <= 3220 IGMP_V3_REPORT_MAXRECS) && 3221 (mt->m_pkthdr.len + recslen <= 3222 (inm->inm_ifp->if_mtu - IGMP_LEADINGSPACE))) 3223 domerge = 1; 3224 } 3225 3226 if (!domerge && _IF_QFULL(gq)) { 3227 CTR2(KTR_IGMPV3, 3228 "%s: outbound queue full, skipping whole packet %p", 3229 __func__, m); 3230 mt = m->m_nextpkt; 3231 if (!docopy) 3232 m_freem(m); 3233 m = mt; 3234 continue; 3235 } 3236 3237 if (!docopy) { 3238 CTR2(KTR_IGMPV3, "%s: dequeueing %p", __func__, m); 3239 _IF_DEQUEUE(gq, m0); 3240 m = m0->m_nextpkt; 3241 } else { 3242 CTR2(KTR_IGMPV3, "%s: copying %p", __func__, m); 3243 m0 = m_dup(m, M_NOWAIT); 3244 if (m0 == NULL) 3245 return (ENOMEM); 3246 m0->m_nextpkt = NULL; 3247 m = m->m_nextpkt; 3248 } 3249 3250 if (!domerge) { 3251 CTR3(KTR_IGMPV3, "%s: queueing %p to ifscq %p)", 3252 __func__, m0, ifscq); 3253 _IF_ENQUEUE(ifscq, m0); 3254 } else { 3255 struct mbuf *mtl; /* last mbuf of packet mt */ 3256 3257 CTR3(KTR_IGMPV3, "%s: merging %p with ifscq tail %p)", 3258 __func__, m0, mt); 3259 3260 mtl = m_last(mt); 3261 m0->m_flags &= ~M_PKTHDR; 3262 mt->m_pkthdr.len += recslen; 3263 mt->m_pkthdr.PH_vt.vt_nrecs += 3264 m0->m_pkthdr.PH_vt.vt_nrecs; 3265 3266 mtl->m_next = m0; 3267 } 3268 } 3269 3270 return (0); 3271 } 3272 3273 /* 3274 * Respond to a pending IGMPv3 General Query. 3275 */ 3276 static void 3277 igmp_v3_dispatch_general_query(struct igmp_ifinfo *igi) 3278 { 3279 struct ifmultiaddr *ifma, *tifma; 3280 struct ifnet *ifp; 3281 struct in_multi *inm; 3282 int retval, loop; 3283 3284 IN_MULTI_LOCK_ASSERT(); 3285 IGMP_LOCK_ASSERT(); 3286 3287 KASSERT(igi->igi_version == IGMP_VERSION_3, 3288 ("%s: called when version %d", __func__, igi->igi_version)); 3289 3290 ifp = igi->igi_ifp; 3291 3292 IF_ADDR_LOCK(ifp); 3293 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, tifma) { 3294 if (ifma->ifma_addr->sa_family != AF_INET || 3295 ifma->ifma_protospec == NULL) 3296 continue; 3297 3298 inm = (struct in_multi *)ifma->ifma_protospec; 3299 KASSERT(ifp == inm->inm_ifp, 3300 ("%s: inconsistent ifp", __func__)); 3301 3302 switch (inm->inm_state) { 3303 case IGMP_NOT_MEMBER: 3304 case IGMP_SILENT_MEMBER: 3305 break; 3306 case IGMP_REPORTING_MEMBER: 3307 case IGMP_IDLE_MEMBER: 3308 case IGMP_LAZY_MEMBER: 3309 case IGMP_SLEEPING_MEMBER: 3310 case IGMP_AWAKENING_MEMBER: 3311 inm->inm_state = IGMP_REPORTING_MEMBER; 3312 retval = igmp_v3_enqueue_group_record(&igi->igi_gq, 3313 inm, 0, 0, 0); 3314 CTR2(KTR_IGMPV3, "%s: enqueue record = %d", 3315 __func__, retval); 3316 break; 3317 case IGMP_G_QUERY_PENDING_MEMBER: 3318 case IGMP_SG_QUERY_PENDING_MEMBER: 3319 case IGMP_LEAVING_MEMBER: 3320 break; 3321 } 3322 } 3323 IF_ADDR_UNLOCK(ifp); 3324 3325 loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0; 3326 igmp_dispatch_queue(&igi->igi_gq, IGMP_MAX_RESPONSE_BURST, loop); 3327 3328 /* 3329 * Slew transmission of bursts over 500ms intervals. 3330 */ 3331 if (igi->igi_gq.ifq_head != NULL) { 3332 igi->igi_v3_timer = 1 + IGMP_RANDOM_DELAY( 3333 IGMP_RESPONSE_BURST_INTERVAL); 3334 V_interface_timers_running = 1; 3335 } 3336 } 3337 3338 /* 3339 * Transmit the next pending IGMP message in the output queue. 3340 * 3341 * We get called from netisr_processqueue(). A mutex private to igmpoq 3342 * will be acquired and released around this routine. 3343 * 3344 * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis. 3345 * MRT: Nothing needs to be done, as IGMP traffic is always local to 3346 * a link and uses a link-scope multicast address. 3347 */ 3348 static void 3349 igmp_intr(struct mbuf *m) 3350 { 3351 struct ip_moptions imo; 3352 struct ifnet *ifp; 3353 struct mbuf *ipopts, *m0; 3354 int error; 3355 uint32_t ifindex; 3356 3357 CTR2(KTR_IGMPV3, "%s: transmit %p", __func__, m); 3358 3359 /* 3360 * Restore VNET image pointer from enqueued mbuf chain 3361 * before doing anything else. Whilst we use interface 3362 * indexes to guard against interface detach, they are 3363 * unique to each VIMAGE and must be retrieved. 3364 */ 3365 CURVNET_SET(m->m_pkthdr.header); 3366 ifindex = igmp_restore_context(m); 3367 3368 /* 3369 * Check if the ifnet still exists. This limits the scope of 3370 * any race in the absence of a global ifp lock for low cost 3371 * (an array lookup). 3372 */ 3373 ifp = ifnet_byindex(ifindex); 3374 if (ifp == NULL) { 3375 CTR3(KTR_IGMPV3, "%s: dropped %p as ifindex %u went away.", 3376 __func__, m, ifindex); 3377 m_freem(m); 3378 IPSTAT_INC(ips_noroute); 3379 goto out; 3380 } 3381 3382 ipopts = V_igmp_sendra ? m_raopt : NULL; 3383 3384 imo.imo_multicast_ttl = 1; 3385 imo.imo_multicast_vif = -1; 3386 imo.imo_multicast_loop = (V_ip_mrouter != NULL); 3387 3388 /* 3389 * If the user requested that IGMP traffic be explicitly 3390 * redirected to the loopback interface (e.g. they are running a 3391 * MANET interface and the routing protocol needs to see the 3392 * updates), handle this now. 3393 */ 3394 if (m->m_flags & M_IGMP_LOOP) 3395 imo.imo_multicast_ifp = V_loif; 3396 else 3397 imo.imo_multicast_ifp = ifp; 3398 3399 if (m->m_flags & M_IGMPV2) { 3400 m0 = m; 3401 } else { 3402 m0 = igmp_v3_encap_report(ifp, m); 3403 if (m0 == NULL) { 3404 CTR2(KTR_IGMPV3, "%s: dropped %p", __func__, m); 3405 m_freem(m); 3406 IPSTAT_INC(ips_odropped); 3407 goto out; 3408 } 3409 } 3410 3411 igmp_scrub_context(m0); 3412 m->m_flags &= ~(M_PROTOFLAGS); 3413 m0->m_pkthdr.rcvif = V_loif; 3414 #ifdef MAC 3415 mac_netinet_igmp_send(ifp, m0); 3416 #endif 3417 error = ip_output(m0, ipopts, NULL, 0, &imo, NULL); 3418 if (error) { 3419 CTR3(KTR_IGMPV3, "%s: ip_output(%p) = %d", __func__, m0, error); 3420 goto out; 3421 } 3422 3423 IGMPSTAT_INC(igps_snd_reports); 3424 3425 out: 3426 /* 3427 * We must restore the existing vnet pointer before 3428 * continuing as we are run from netisr context. 3429 */ 3430 CURVNET_RESTORE(); 3431 } 3432 3433 /* 3434 * Encapsulate an IGMPv3 report. 3435 * 3436 * The internal mbuf flag M_IGMPV3_HDR is used to indicate that the mbuf 3437 * chain has already had its IP/IGMPv3 header prepended. In this case 3438 * the function will not attempt to prepend; the lengths and checksums 3439 * will however be re-computed. 3440 * 3441 * Returns a pointer to the new mbuf chain head, or NULL if the 3442 * allocation failed. 3443 */ 3444 static struct mbuf * 3445 igmp_v3_encap_report(struct ifnet *ifp, struct mbuf *m) 3446 { 3447 INIT_VNET_NET(curvnet); 3448 INIT_VNET_INET(curvnet); 3449 struct igmp_report *igmp; 3450 struct ip *ip; 3451 int hdrlen, igmpreclen; 3452 3453 KASSERT((m->m_flags & M_PKTHDR), 3454 ("%s: mbuf chain %p is !M_PKTHDR", __func__, m)); 3455 3456 igmpreclen = m_length(m, NULL); 3457 hdrlen = sizeof(struct ip) + sizeof(struct igmp_report); 3458 3459 if (m->m_flags & M_IGMPV3_HDR) { 3460 igmpreclen -= hdrlen; 3461 } else { 3462 M_PREPEND(m, hdrlen, M_DONTWAIT); 3463 if (m == NULL) 3464 return (NULL); 3465 m->m_flags |= M_IGMPV3_HDR; 3466 } 3467 3468 CTR2(KTR_IGMPV3, "%s: igmpreclen is %d", __func__, igmpreclen); 3469 3470 m->m_data += sizeof(struct ip); 3471 m->m_len -= sizeof(struct ip); 3472 3473 igmp = mtod(m, struct igmp_report *); 3474 igmp->ir_type = IGMP_v3_HOST_MEMBERSHIP_REPORT; 3475 igmp->ir_rsv1 = 0; 3476 igmp->ir_rsv2 = 0; 3477 igmp->ir_numgrps = htons(m->m_pkthdr.PH_vt.vt_nrecs); 3478 igmp->ir_cksum = 0; 3479 igmp->ir_cksum = in_cksum(m, sizeof(struct igmp_report) + igmpreclen); 3480 m->m_pkthdr.PH_vt.vt_nrecs = 0; 3481 3482 m->m_data -= sizeof(struct ip); 3483 m->m_len += sizeof(struct ip); 3484 3485 ip = mtod(m, struct ip *); 3486 ip->ip_tos = IPTOS_PREC_INTERNETCONTROL; 3487 ip->ip_len = hdrlen + igmpreclen; 3488 ip->ip_off = IP_DF; 3489 ip->ip_p = IPPROTO_IGMP; 3490 ip->ip_sum = 0; 3491 3492 ip->ip_src.s_addr = INADDR_ANY; 3493 3494 if (m->m_flags & M_IGMP_LOOP) { 3495 struct in_ifaddr *ia; 3496 3497 IFP_TO_IA(ifp, ia); 3498 if (ia != NULL) 3499 ip->ip_src = ia->ia_addr.sin_addr; 3500 } 3501 3502 ip->ip_dst.s_addr = htonl(INADDR_ALLRPTS_GROUP); 3503 3504 return (m); 3505 } 3506 3507 #ifdef KTR 3508 static char * 3509 igmp_rec_type_to_str(const int type) 3510 { 3511 3512 switch (type) { 3513 case IGMP_CHANGE_TO_EXCLUDE_MODE: 3514 return "TO_EX"; 3515 break; 3516 case IGMP_CHANGE_TO_INCLUDE_MODE: 3517 return "TO_IN"; 3518 break; 3519 case IGMP_MODE_IS_EXCLUDE: 3520 return "MODE_EX"; 3521 break; 3522 case IGMP_MODE_IS_INCLUDE: 3523 return "MODE_IN"; 3524 break; 3525 case IGMP_ALLOW_NEW_SOURCES: 3526 return "ALLOW_NEW"; 3527 break; 3528 case IGMP_BLOCK_OLD_SOURCES: 3529 return "BLOCK_OLD"; 3530 break; 3531 default: 3532 break; 3533 } 3534 return "unknown"; 3535 } 3536 #endif 3537 3538 static void 3539 igmp_sysinit(void) 3540 { 3541 3542 CTR1(KTR_IGMPV3, "%s: initializing", __func__); 3543 3544 IGMP_LOCK_INIT(); 3545 3546 mtx_init(&igmpoq.ifq_mtx, "igmpoq_mtx", NULL, MTX_DEF); 3547 IFQ_SET_MAXLEN(&igmpoq, IFQ_MAXLEN); 3548 3549 m_raopt = igmp_ra_alloc(); 3550 3551 netisr_register(NETISR_IGMP, igmp_intr, &igmpoq, 0); 3552 } 3553 3554 static void 3555 igmp_sysuninit(void) 3556 { 3557 3558 CTR1(KTR_IGMPV3, "%s: tearing down", __func__); 3559 3560 netisr_unregister(NETISR_IGMP); 3561 mtx_destroy(&igmpoq.ifq_mtx); 3562 3563 m_free(m_raopt); 3564 m_raopt = NULL; 3565 3566 IGMP_LOCK_DESTROY(); 3567 } 3568 3569 /* 3570 * Initialize an IGMPv3 instance. 3571 * VIMAGE: Assumes curvnet set by caller and called per vimage. 3572 */ 3573 static int 3574 vnet_igmp_iattach(const void *unused __unused) 3575 { 3576 INIT_VNET_INET(curvnet); 3577 3578 CTR1(KTR_IGMPV3, "%s: initializing", __func__); 3579 3580 LIST_INIT(&V_igi_head); 3581 3582 V_current_state_timers_running = 0; 3583 V_state_change_timers_running = 0; 3584 V_interface_timers_running = 0; 3585 3586 /* 3587 * Initialize sysctls to default values. 3588 */ 3589 V_igmp_recvifkludge = 1; 3590 V_igmp_sendra = 1; 3591 V_igmp_sendlocal = 1; 3592 V_igmp_v1enable = 1; 3593 V_igmp_v2enable = 1; 3594 V_igmp_legacysupp = 0; 3595 V_igmp_default_version = IGMP_VERSION_3; 3596 V_igmp_gsrdelay.tv_sec = 10; 3597 V_igmp_gsrdelay.tv_usec = 0; 3598 3599 memset(&V_igmpstat, 0, sizeof(struct igmpstat)); 3600 V_igmpstat.igps_version = IGPS_VERSION_3; 3601 V_igmpstat.igps_len = sizeof(struct igmpstat); 3602 3603 return (0); 3604 } 3605 3606 static int 3607 vnet_igmp_idetach(const void *unused __unused) 3608 { 3609 INIT_VNET_INET(curvnet); 3610 3611 CTR1(KTR_IGMPV3, "%s: tearing down", __func__); 3612 3613 KASSERT(LIST_EMPTY(&V_igi_head), 3614 ("%s: igi list not empty; ifnets not detached?", __func__)); 3615 3616 return (0); 3617 } 3618 3619 #ifdef VIMAGE 3620 static struct vnet_symmap vnet_igmp_symmap[] = { 3621 VNET_SYMMAP(igmp, igi_head), 3622 VNET_SYMMAP(igmp, igmpstat), 3623 VNET_SYMMAP_END 3624 }; 3625 VNET_MOD_DECLARE(IGMP, igmp, vnet_igmp_iattach, vnet_igmp_idetach, 3626 vnet_igmp_symmap); 3627 #endif /* VIMAGE */ 3628 3629 static int 3630 igmp_modevent(module_t mod, int type, void *unused __unused) 3631 { 3632 3633 switch (type) { 3634 case MOD_LOAD: 3635 igmp_sysinit(); 3636 #ifdef VIMAGE 3637 vnet_mod_register(&vnet_igmp_modinfo); 3638 #else 3639 (void)vnet_igmp_iattach(NULL); 3640 #endif /* VIMAGE */ 3641 break; 3642 case MOD_UNLOAD: 3643 #ifdef VIMAGE 3644 /* 3645 * TODO: Allow module unload if any VIMAGE instances 3646 * are using this module. 3647 */ 3648 return (EBUSY); 3649 #else 3650 (void)vnet_igmp_idetach(NULL); 3651 #endif /* VIMAGE */ 3652 igmp_sysuninit(); 3653 break; 3654 default: 3655 return (EOPNOTSUPP); 3656 } 3657 return (0); 3658 } 3659 3660 static moduledata_t igmp_mod = { 3661 "igmp", 3662 igmp_modevent, 3663 0 3664 }; 3665 DECLARE_MODULE(igmp, igmp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 3666