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