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