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