igmp.c revision 191548
1139749Simp/*- 279047Scg * Copyright (c) 2007-2009 Bruce Simpson. 379148Scg * Copyright (c) 1988 Stephen Deering. 479047Scg * Copyright (c) 1992, 1993 579047Scg * The Regents of the University of California. All rights reserved. 679047Scg * 779047Scg * This code is derived from software contributed to Berkeley by 879047Scg * Stephen Deering of Stanford University. 979047Scg * 1079047Scg * Redistribution and use in source and binary forms, with or without 1179047Scg * modification, are permitted provided that the following conditions 1279047Scg * are met: 1379047Scg * 1. Redistributions of source code must retain the above copyright 1479047Scg * notice, this list of conditions and the following disclaimer. 1579047Scg * 2. Redistributions in binary form must reproduce the above copyright 1679047Scg * notice, this list of conditions and the following disclaimer in the 1779047Scg * documentation and/or other materials provided with the distribution. 1879047Scg * 4. Neither the name of the University nor the names of its contributors 1979047Scg * may be used to endorse or promote products derived from this software 2079047Scg * without specific prior written permission. 2179047Scg * 2279047Scg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2379047Scg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2479047Scg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2579047Scg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2679047Scg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2779047Scg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28193640Sariff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29193640Sariff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30193640Sariff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31193640Sariff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3279047Scg * SUCH DAMAGE. 3379047Scg * 3479148Scg * @(#)igmp.c 8.1 (Berkeley) 7/19/93 3579047Scg */ 36119287Simp 37119287Simp/* 3879047Scg * Internet Group Management Protocol (IGMP) routines. 3982180Scg * [RFC1112, RFC2236, RFC3376] 4082180Scg * 4179047Scg * Written by Steve Deering, Stanford, May 1988. 4279047Scg * Modified by Rosen Sharma, Stanford, Aug 1994. 43164614Sariff * Modified by Bill Fenner, Xerox PARC, Feb 1995. 44164614Sariff * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995. 45164614Sariff * Significantly rewritten for IGMPv3, VIMAGE, and SMP by Bruce Simpson. 46164614Sariff * 47164614Sariff * MULTICAST Revision: 3.5.1.4 48164614Sariff */ 49164614Sariff 50164614Sariff#include <sys/cdefs.h> 51169278Sariff__FBSDID("$FreeBSD: head/sys/netinet/igmp.c 191548 2009-04-26 22:06:42Z zec $"); 5279047Scg 53152420Sariff#include "opt_mac.h" 54152420Sariff#include "opt_route.h" 55152420Sariff 56152420Sariff#include <sys/param.h> 5790880Salfred#include <sys/systm.h> 58152420Sariff#include <sys/module.h> 59152420Sariff#include <sys/malloc.h> 60152420Sariff#include <sys/mbuf.h> 61152420Sariff#include <sys/socket.h> 62152420Sariff#include <sys/protosw.h> 63152420Sariff#include <sys/kernel.h> 64152420Sariff#include <sys/sysctl.h> 65152420Sariff#include <sys/vimage.h> 66152420Sariff#include <sys/ktr.h> 67152420Sariff#include <sys/condvar.h> 68152420Sariff 69152420Sariff#include <net/if.h> 70152420Sariff#include <net/netisr.h> 71152420Sariff#include <net/route.h> 72152420Sariff#include <net/vnet.h> 73152420Sariff 74152420Sariff#include <netinet/in.h> 75157436Sariff#include <netinet/in_var.h> 76165304Sariff#include <netinet/in_systm.h> 77152420Sariff#include <netinet/ip.h> 78152420Sariff#include <netinet/ip_var.h> 79152420Sariff#include <netinet/ip_options.h> 80152420Sariff#include <netinet/igmp.h> 81152420Sariff#include <netinet/igmp_var.h> 82152420Sariff#include <netinet/vinet.h> 83152420Sariff 84164614Sariff#include <machine/in_cksum.h> 85164614Sariff 86164614Sariff#include <security/mac/mac_framework.h> 87193640Sariff 88164614Sariff#ifndef KTR_IGMPV3 89169278Sariff#define KTR_IGMPV3 KTR_SUBSYS 90164614Sariff#endif 91164614Sariff 92169278Sariffstatic struct igmp_ifinfo * 93169278Sariff igi_alloc_locked(struct ifnet *); 94169278Sariffstatic void igi_delete_locked(const struct ifnet *); 95169278Sariffstatic void igmp_dispatch_queue(struct ifqueue *, int, const int); 96169278Sariffstatic void igmp_fasttimo_vnet(void); 97169278Sariffstatic void igmp_final_leave(struct in_multi *, struct igmp_ifinfo *); 98169278Sariffstatic int igmp_handle_state_change(struct in_multi *, 99152420Sariff struct igmp_ifinfo *); 100152420Sariffstatic int igmp_initial_join(struct in_multi *, struct igmp_ifinfo *); 101152420Sariffstatic int igmp_input_v1_query(struct ifnet *, const struct ip *); 102152420Sariffstatic int igmp_input_v2_query(struct ifnet *, const struct ip *, 103152420Sariff const struct igmp *); 104152420Sariffstatic int igmp_input_v3_query(struct ifnet *, const struct ip *, 105152420Sariff /*const*/ struct igmpv3 *); 106152420Sariffstatic int igmp_input_v3_group_query(struct in_multi *, 107152420Sariff struct igmp_ifinfo *, int, /*const*/ struct igmpv3 *); 108152420Sariffstatic int igmp_input_v1_report(struct ifnet *, /*const*/ struct ip *, 109152420Sariff /*const*/ struct igmp *); 110152420Sariffstatic int igmp_input_v2_report(struct ifnet *, /*const*/ struct ip *, 111152420Sariff /*const*/ struct igmp *); 112152420Sariffstatic void igmp_intr(struct mbuf *); 113152420Sariffstatic int igmp_isgroupreported(const struct in_addr); 114152420Sariffstatic struct mbuf * 115152420Sariff igmp_ra_alloc(void); 116152420Sariff#ifdef KTR 117152420Sariffstatic char * igmp_rec_type_to_str(const int); 118152420Sariff#endif 119152420Sariffstatic void igmp_set_version(struct igmp_ifinfo *, const int); 120152420Sariffstatic void igmp_slowtimo_vnet(void); 121152420Sariffstatic void igmp_sysinit(void); 122152420Sariffstatic int igmp_v1v2_queue_report(struct in_multi *, const int); 123152420Sariffstatic void igmp_v1v2_process_group_timer(struct in_multi *, const int); 124152420Sariffstatic void igmp_v1v2_process_querier_timers(struct igmp_ifinfo *); 125152420Sariffstatic void igmp_v2_update_group(struct in_multi *, const int); 126152420Sariffstatic void igmp_v3_cancel_link_timers(struct igmp_ifinfo *); 127152420Sariffstatic void igmp_v3_dispatch_general_query(struct igmp_ifinfo *); 128152420Sariffstatic struct mbuf * 129152420Sariff igmp_v3_encap_report(struct ifnet *, struct mbuf *); 130152420Sariffstatic int igmp_v3_enqueue_group_record(struct ifqueue *, 131152420Sariff struct in_multi *, const int, const int, const int); 132152420Sariffstatic int igmp_v3_enqueue_filter_change(struct ifqueue *, 133152420Sariff struct in_multi *); 134152420Sariffstatic void igmp_v3_process_group_timers(struct igmp_ifinfo *, 135152420Sariff struct ifqueue *, struct ifqueue *, struct in_multi *, 136152420Sariff const int); 137152420Sariffstatic int igmp_v3_merge_state_changes(struct in_multi *, 138152420Sariff struct ifqueue *); 139152420Sariffstatic void igmp_v3_suppress_group_record(struct in_multi *); 140157436Sariffstatic int sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS); 141157436Sariffstatic int sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS); 142165304Sariffstatic int sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS); 143165304Sariff 144152420Sariffstatic vnet_attach_fn vnet_igmp_iattach; 145152420Sariffstatic vnet_detach_fn vnet_igmp_idetach; 146152420Sariff 147152420Sariff/* 148152420Sariff * System-wide globals. 149152420Sariff * 15079047Scg * Unlocked access to these is OK, except for the global IGMP output 15179047Scg * queue. The IGMP subsystem lock ends up being system-wide for the moment, 152164614Sariff * because all VIMAGEs have to share a global output queue, as netisrs 153164614Sariff * themselves are not virtualized. 15479047Scg * 15579047Scg * Locking: 15679047Scg * * The permitted lock order is: IN_MULTI_LOCK, IGMP_LOCK, IF_ADDR_LOCK. 15779047Scg * Any may be taken independently; if any are held at the same 15879047Scg * time, the above lock order must be followed. 15979047Scg * * All output is delegated to the netisr. 160164614Sariff * Now that Giant has been eliminated, the netisr may be inlined. 161164614Sariff * * IN_MULTI_LOCK covers in_multi. 162164614Sariff * * IGMP_LOCK covers igmp_ifinfo and any global variables in this file, 163164614Sariff * including the output queue. 164164614Sariff * * IF_ADDR_LOCK covers if_multiaddrs, which is used for a variety of 16579148Scg * per-link state iterators. 16679047Scg * * igmp_ifinfo is valid as long as PF_INET is attached to the interface, 16779047Scg * therefore it is not refcounted. 16879047Scg * We allow unlocked reads of igmp_ifinfo when accessed via in_multi. 16979148Scg * 17079148Scg * Reference counting 171111183Scognet * * IGMP acquires its own reference every time an in_multi is passed to 17279047Scg * it and the group is being joined for the first time. 17379047Scg * * IGMP releases its reference(s) on in_multi in a deferred way, 17479047Scg * because the operations which process the release run as part of 17579047Scg * a loop whose control variables are directly affected by the release 17679148Scg * (that, and not recursing on the IF_ADDR_LOCK). 17783617Scg * 178164614Sariff * VIMAGE: Each in_multi corresponds to an ifp, and each ifp corresponds 179169278Sariff * to a vnet in ifp->if_vnet. 18079047Scg * 18179148Scg * SMPng: XXX We may potentially race operations on ifma_protospec. 182118729Sorion * The problem is that we currently lack a clean way of taking the 18379047Scg * IF_ADDR_LOCK() between the ifnet and in layers w/o recursing, 18479047Scg * as anything which modifies ifma needs to be covered by that lock. 185169278Sariff * So check for ifma_protospec being NULL before proceeding. 18679148Scg */ 18779148Scgstruct mtx igmp_mtx; 18879047Scg 18979047Scgstruct mbuf *m_raopt; /* Router Alert option */ 19079148ScgMALLOC_DEFINE(M_IGMP, "igmp", "igmp state"); 191169278Sariff 19279148Scg/* 193169278Sariff * Global netisr output queue. 194111183Scognet */ 19598940Sscottlstruct ifqueue igmpoq; 196152420Sariff 197152420Sariff/* 198152644Syongari * VIMAGE-wide globals. 199152420Sariff * 20079047Scg * The IGMPv3 timers themselves need to run per-image, however, 20179047Scg * protosw timers run globally (see tcp). 20279047Scg * An ifnet can only be in one vimage at a time, and the loopback 20379047Scg * ifnet, loif, is itself virtualized. 204164614Sariff * It would otherwise be possible to seriously hose IGMP state, 205193640Sariff * and create inconsistencies in upstream multicast routing, if you have 20679047Scg * multiple VIMAGEs running on the same link joining different multicast 20779047Scg * groups, UNLESS the "primary IP address" is different. This is because 20879148Scg * IGMP for IPv4 does not force link-local addresses to be used for each 20979148Scg * node, unlike MLD for IPv6. 21079047Scg * Obviously the IGMPv3 per-interface state has per-vimage granularity 21179047Scg * also as a result. 21279047Scg * 213164614Sariff * FUTURE: Stop using IFP_TO_IA/INADDR_ANY, and use source address selection 21479047Scg * policy to control the address used by IGMP on the link. 21579047Scg */ 21679047Scg#ifdef VIMAGE_GLOBALS 21779047Scgint interface_timers_running; /* IGMPv3 general query response */ 218164614Sariffint state_change_timers_running; /* IGMPv3 state-change retransmit */ 21979047Scgint current_state_timers_running; /* IGMPv1/v2 host report; 220164614Sariff * IGMPv3 g/sg query response */ 22179047Scg 222164614SariffLIST_HEAD(, igmp_ifinfo) igi_head; 22379047Scgstruct igmpstat igmpstat; 224164614Sariffstruct timeval igmp_gsrdelay; 22579047Scg 22679047Scgint igmp_recvifkludge; 22779047Scgint igmp_sendra; 228152420Sariffint igmp_sendlocal; 229164614Sariffint igmp_v1enable; 23079047Scgint igmp_v2enable; 23179047Scgint igmp_legacysupp; 23279047Scgint igmp_default_version; 23379148Scg#endif /* VIMAGE_GLOBALS */ 23479047Scg 23579047Scg/* 23679148Scg * Virtualized sysctls. 23779047Scg */ 23879047ScgSYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_igmp, IGMPCTL_STATS, stats, 23979148Scg CTLFLAG_RW, igmpstat, igmpstat, ""); 24079047ScgSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, recvifkludge, 24179047Scg CTLFLAG_RW, igmp_recvifkludge, 0, 24279047Scg "Rewrite IGMPv1/v2 reports from 0.0.0.0 to contain subnet address"); 24379047ScgSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, sendra, 24479047Scg CTLFLAG_RW, igmp_sendra, 0, 24579047Scg "Send IP Router Alert option in IGMPv2/v3 messages"); 24679047ScgSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, sendlocal, 24779047Scg CTLFLAG_RW, igmp_sendlocal, 0, 248164614Sariff "Send IGMP membership reports for 224.0.0.0/24 groups"); 249164614SariffSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, v1enable, 25079047Scg CTLFLAG_RW, igmp_v1enable, 0, 25179148Scg "Enable backwards compatibility with IGMPv1"); 25279148ScgSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, v2enable, 25379148Scg CTLFLAG_RW, igmp_v2enable, 0, 25479047Scg "Enable backwards compatibility with IGMPv2"); 255164614SariffSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, legacysupp, 256152644Syongari CTLFLAG_RW, igmp_legacysupp, 0, 25779047Scg "Allow v1/v2 reports to suppress v3 group responses"); 258169278SariffSYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, default_version, 259152644Syongari CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, igmp_default_version, 0, 26079047Scg sysctl_igmp_default_version, "I", 261164614Sariff "Default version of IGMP to run on each interface"); 26279047ScgSYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, gsrdelay, 26379047Scg CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, igmp_gsrdelay.tv_sec, 0, 26479047Scg sysctl_igmp_gsr, "I", 26579047Scg "Rate limit for IGMPv3 Group-and-Source queries in seconds"); 26679047Scg 26779047Scg/* 26879148Scg * Non-virtualized sysctls. 26979047Scg */ 27079047ScgSYSCTL_NODE(_net_inet_igmp, OID_AUTO, ifinfo, CTLFLAG_RD | CTLFLAG_MPSAFE, 27179148Scg sysctl_igmp_ifinfo, "Per-interface IGMPv3 state"); 272164614Sariff 27379047Scgstatic __inline void 27479047Scgigmp_save_context(struct mbuf *m, struct ifnet *ifp) 27579047Scg{ 276193640Sariff 27779047Scg#ifdef VIMAGE 27879047Scg m->m_pkthdr.header = ifp->if_vnet; 27979148Scg#endif /* VIMAGE */ 28079047Scg m->m_pkthdr.flowid = ifp->if_index; 28179047Scg} 28279148Scg 28379148Scgstatic __inline void 284164614Sariffigmp_scrub_context(struct mbuf *m) 28579047Scg{ 28679047Scg 28779047Scg m->m_pkthdr.header = NULL; 28879047Scg m->m_pkthdr.flowid = 0; 28979047Scg} 290193640Sariff 29179047Scg#ifdef KTR 29279047Scgstatic __inline char * 29379047Scginet_ntoa_haddr(in_addr_t haddr) 29479047Scg{ 29579148Scg struct in_addr ia; 29679047Scg 29779047Scg ia.s_addr = htonl(haddr); 29879148Scg return (inet_ntoa(ia)); 29979047Scg} 300152420Sariff#endif 301164614Sariff 30282478Scg/* 30379047Scg * Restore context from a queued IGMP output chain. 304111183Scognet * Return saved ifindex. 305164614Sariff * 306164614Sariff * VIMAGE: The assertion is there to make sure that we 307164614Sariff * actually called CURVNET_SET() with what's in the mbuf chain. 308164614Sariff */ 309164614Sariffstatic __inline uint32_t 310164614Sariffigmp_restore_context(struct mbuf *m) 311164614Sariff{ 312152420Sariff 31379047Scg#ifdef notyet 31479148Scg#if defined(VIMAGE) && defined(INVARIANTS) 31582478Scg KASSERT(curvnet == (m->m_pkthdr.header), 31690880Salfred ("%s: called when curvnet was not restored", __func__)); 31790880Salfred#endif 31879047Scg#endif 31979047Scg return (m->m_pkthdr.flowid); 32079047Scg} 32179148Scg 32279148Scg/* 32379047Scg * Retrieve or set default IGMP version. 32479148Scg * 32579047Scg * VIMAGE: Assume curvnet set by caller. 32679148Scg * SMPng: NOTE: Serialized by IGMP lock. 32779148Scg */ 32879148Scgstatic int 32979148Scgsysctl_igmp_default_version(SYSCTL_HANDLER_ARGS) 33079148Scg{ 33179148Scg INIT_VNET_INET(curvnet); 33279148Scg int error; 333164614Sariff int new; 33479047Scg 33579148Scg error = sysctl_wire_old_buffer(req, sizeof(int)); 336150979Snetchild if (error) 337150979Snetchild return (error); 33879148Scg 339150979Snetchild IGMP_LOCK(); 340150979Snetchild 341150979Snetchild new = V_igmp_default_version; 34279148Scg 34379148Scg error = sysctl_handle_int(oidp, &new, 0, req); 34479148Scg if (error || !req->newptr) 34579148Scg goto out_locked; 346164614Sariff 347169278Sariff if (new < IGMP_VERSION_1 || new > IGMP_VERSION_3) { 34879148Scg error = EINVAL; 34979047Scg goto out_locked; 350169278Sariff } 351169278Sariff 352169278Sariff CTR2(KTR_IGMPV3, "change igmp_default_version from %d to %d", 353169278Sariff V_igmp_default_version, new); 354169278Sariff 355169278Sariff V_igmp_default_version = new; 356169278Sariff 357169278Sariffout_locked: 358169278Sariff IGMP_UNLOCK(); 359169278Sariff return (error); 36079148Scg} 361164614Sariff 36279047Scg/* 36379047Scg * Retrieve or set threshold between group-source queries in seconds. 36479148Scg * 36579148Scg * VIMAGE: Assume curvnet set by caller. 36679047Scg * SMPng: NOTE: Serialized by IGMP lock. 36779047Scg */ 36879148Scgstatic int 36979047Scgsysctl_igmp_gsr(SYSCTL_HANDLER_ARGS) 37079047Scg{ 37179047Scg INIT_VNET_INET(curvnet); 37283617Scg int error; 37379047Scg int i; 374152420Sariff 37579148Scg error = sysctl_wire_old_buffer(req, sizeof(int)); 37679148Scg if (error) 37779148Scg return (error); 37879047Scg 37979047Scg IGMP_LOCK(); 38079047Scg 38179047Scg i = V_igmp_gsrdelay.tv_sec; 38279148Scg 383169278Sariff error = sysctl_handle_int(oidp, &i, 0, req); 384169278Sariff if (error || !req->newptr) 385164614Sariff goto out_locked; 38683617Scg 38779047Scg if (i < -1 || i >= 60) { 38879148Scg error = EINVAL; 38979148Scg goto out_locked; 39079148Scg } 39179148Scg 392164614Sariff CTR2(KTR_IGMPV3, "change igmp_gsrdelay from %d to %d", 39388206Sorion V_igmp_gsrdelay.tv_sec, i); 39479148Scg V_igmp_gsrdelay.tv_sec = i; 39579047Scg 39682478Scgout_locked: 39779148Scg IGMP_UNLOCK(); 39882478Scg return (error); 399164614Sariff} 40088206Sorion 40179148Scg/* 40279047Scg * Expose struct igmp_ifinfo to userland, keyed by ifindex. 40382478Scg * For use by ifmcstat(8). 40479148Scg * 40582478Scg * SMPng: NOTE: Does an unlocked ifindex space read. 406164614Sariff * VIMAGE: Assume curvnet set by caller. The node handler itself 40788206Sorion * is not directly virtualized. 40879148Scg */ 40979047Scgstatic int 41079148Scgsysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS) 411164614Sariff{ 41279047Scg INIT_VNET_NET(curvnet); 41379047Scg INIT_VNET_INET(curvnet); 414169278Sariff int *name; 415164614Sariff int error; 416164614Sariff u_int namelen; 417152420Sariff struct ifnet *ifp; 418169278Sariff struct igmp_ifinfo *igi; 419169278Sariff 420169278Sariff name = (int *)arg1; 421164614Sariff namelen = arg2; 42279047Scg 423152420Sariff if (req->newptr != NULL) 424164614Sariff return (EPERM); 425152420Sariff 42679047Scg if (namelen != 1) 427164614Sariff return (EINVAL); 42879047Scg 42979047Scg error = sysctl_wire_old_buffer(req, sizeof(struct igmp_ifinfo)); 43079047Scg if (error) 431164614Sariff return (error); 43279047Scg 433164614Sariff IN_MULTI_LOCK(); 434164614Sariff IGMP_LOCK(); 435164614Sariff 436164614Sariff if (name[0] <= 0 || name[0] > V_if_index) { 437169278Sariff error = ENOENT; 438164614Sariff goto out_locked; 439164614Sariff } 440164614Sariff 441164614Sariff error = ENOENT; 442164614Sariff 443164614Sariff ifp = ifnet_byindex(name[0]); 44479047Scg if (ifp == NULL) 44579047Scg goto out_locked; 446193640Sariff 447164614Sariff LIST_FOREACH(igi, &V_igi_head, igi_link) { 44879047Scg if (ifp == igi->igi_ifp) { 44979047Scg error = SYSCTL_OUT(req, igi, 45079047Scg sizeof(struct igmp_ifinfo)); 45179047Scg break; 452164614Sariff } 453169278Sariff } 454164614Sariff 455164614Sariffout_locked: 456164614Sariff IGMP_UNLOCK(); 457164614Sariff IN_MULTI_UNLOCK(); 458164614Sariff return (error); 45988033Sorion} 460152420Sariff 461152420Sariff/* 462152420Sariff * Dispatch an entire queue of pending packet chains 46388033Sorion * using the netisr. 46488033Sorion * VIMAGE: Assumes the vnet pointer has been set. 465152420Sariff */ 466152420Sariffstatic void 467152420Sariffigmp_dispatch_queue(struct ifqueue *ifq, int limit, const int loop) 46894647Sjhay{ 469164614Sariff struct mbuf *m; 47094647Sjhay 47194647Sjhay for (;;) { 472164614Sariff _IF_DEQUEUE(ifq, m); 473152420Sariff if (m == NULL) 47488033Sorion break; 47588361Sorion CTR3(KTR_IGMPV3, "%s: dispatch %p from %p", __func__, ifq, m); 47688033Sorion if (loop) 477164614Sariff m->m_flags |= M_IGMP_LOOP; 47879047Scg netisr_dispatch(NETISR_IGMP, m); 47979047Scg if (--limit == 0) 480193640Sariff break; 481164614Sariff } 48279047Scg} 48379047Scg 48479148Scg/* 48579047Scg * Filter outgoing IGMP report state by group. 486164614Sariff * 487169278Sariff * Reports are ALWAYS suppressed for ALL-HOSTS (224.0.0.1). 488164614Sariff * If the net.inet.igmp.sendlocal sysctl is 0, then IGMP reports are 489164614Sariff * disabled for all groups in the 224.0.0.0/24 link-local scope. However, 490164614Sariff * this may break certain IGMP snooping switches which rely on the old 491164614Sariff * report behaviour. 492164614Sariff * 493169278Sariff * Return zero if the given group is one for which IGMP reports 494169278Sariff * should be suppressed, or non-zero if reports should be issued. 495169278Sariff */ 496169278Sariffstatic __inline int 497169278Sariffigmp_isgroupreported(const struct in_addr addr) 498169278Sariff{ 49979148Scg INIT_VNET_INET(curvnet); 50079148Scg 501152420Sariff if (in_allhosts(addr) || 50282478Scg ((!V_igmp_sendlocal && IN_LOCAL_GROUP(ntohl(addr.s_addr))))) 503152420Sariff return (0); 50479047Scg 505164614Sariff return (1); 50679047Scg} 50779047Scg 50879047Scg/* 50979148Scg * Construct a Router Alert option to use in outgoing packets. 51079047Scg */ 51179047Scgstatic struct mbuf * 51279047Scgigmp_ra_alloc(void) 51379047Scg{ 514164614Sariff struct mbuf *m; 515169278Sariff struct ipoption *p; 516164614Sariff 517164614Sariff MGET(m, M_DONTWAIT, MT_DATA); 518164614Sariff p = mtod(m, struct ipoption *); 519164614Sariff p->ipopt_dst.s_addr = INADDR_ANY; 520164614Sariff p->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */ 52179047Scg p->ipopt_list[1] = 0x04; /* 4 bytes long */ 52279047Scg p->ipopt_list[2] = IPOPT_EOL; /* End of IP option list */ 52379047Scg p->ipopt_list[3] = 0x00; /* pad byte */ 524152420Sariff m->m_len = sizeof(p->ipopt_dst) + p->ipopt_list[1]; 525164614Sariff 52689516Sorion return (m); 527152420Sariff} 52879047Scg 529170521Sariff/* 53079047Scg * Attach IGMP when PF_INET is attached to an interface. 531152420Sariff * 53279148Scg * VIMAGE: Currently we set the vnet pointer, although it is 533152420Sariff * likely that it was already set by our caller. 53479047Scg */ 53579047Scgstruct igmp_ifinfo * 536170521Sariffigmp_domifattach(struct ifnet *ifp) 537170521Sariff{ 53879047Scg struct igmp_ifinfo *igi; 539164614Sariff 54079047Scg CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)", 54179047Scg __func__, ifp, ifp->if_xname); 542193640Sariff 54379148Scg CURVNET_SET(ifp->if_vnet); 54479047Scg IGMP_LOCK(); 54579047Scg 54679047Scg igi = igi_alloc_locked(ifp); 547164614Sariff if (!(ifp->if_flags & IFF_MULTICAST)) 54879047Scg igi->igi_flags |= IGIF_SILENT; 549164614Sariff 550169278Sariff IGMP_UNLOCK(); 551164614Sariff CURVNET_RESTORE(); 552164614Sariff 553164614Sariff return (igi); 554164614Sariff} 555164614Sariff 556152420Sariff/* 55782478Scg * VIMAGE: assume curvnet set by caller. 558152420Sariff */ 55979148Scgstatic struct igmp_ifinfo * 56082478Scgigi_alloc_locked(/*const*/ struct ifnet *ifp) 56179148Scg{ 562164614Sariff INIT_VNET_INET(ifp->if_vnet); 56379047Scg struct igmp_ifinfo *igi; 56479047Scg 56579047Scg IGMP_LOCK_ASSERT(); 56679148Scg 56779047Scg igi = malloc(sizeof(struct igmp_ifinfo), M_IGMP, M_NOWAIT|M_ZERO); 56879148Scg if (igi == NULL) 56979148Scg goto out; 570164614Sariff 571164614Sariff igi->igi_ifp = ifp; 572164614Sariff igi->igi_version = V_igmp_default_version; 573169278Sariff igi->igi_flags = 0; 574164614Sariff igi->igi_rv = IGMP_RV_INIT; 575164614Sariff igi->igi_qi = IGMP_QI_INIT; 576164614Sariff igi->igi_qri = IGMP_QRI_INIT; 577164614Sariff igi->igi_uri = IGMP_URI_INIT; 578164614Sariff 579164614Sariff SLIST_INIT(&igi->igi_relinmhead); 58079047Scg 58179047Scg /* 58279148Scg * Responses to general queries are subject to bounds. 58379148Scg */ 58479148Scg IFQ_SET_MAXLEN(&igi->igi_gq, IGMP_MAX_RESPONSE_PACKETS); 58579148Scg 58679148Scg LIST_INSERT_HEAD(&V_igi_head, igi, igi_link); 58779148Scg 58879148Scg CTR2(KTR_IGMPV3, "allocate igmp_ifinfo for ifp %p(%s)", 58979148Scg ifp, ifp->if_xname); 590193640Sariff 59179047Scgout: 59279148Scg return (igi); 59379047Scg} 59479047Scg 59579047Scg/* 59679148Scg * Hook for ifdetach. 59779047Scg * 59879047Scg * NOTE: Some finalization tasks need to run before the protocol domain 59979047Scg * is detached, but also before the link layer does its cleanup. 60079047Scg * 60179047Scg * SMPNG: igmp_ifdetach() needs to take IF_ADDR_LOCK(). 602164614Sariff * XXX This is also bitten by unlocked ifma_protospec access. 60379148Scg * 60479047Scg * VIMAGE: curvnet should have been set by caller, but let's not assume 605152420Sariff * that for now. 606164614Sariff */ 607164614Sariffvoid 608169278Sariffigmp_ifdetach(struct ifnet *ifp) 609164614Sariff{ 610164614Sariff struct igmp_ifinfo *igi; 611164614Sariff struct ifmultiaddr *ifma; 612164614Sariff struct in_multi *inm, *tinm; 613164614Sariff 61488215Sorion CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)", __func__, ifp, 61588206Sorion ifp->if_xname); 61688206Sorion 61788206Sorion CURVNET_SET(ifp->if_vnet); 61888206Sorion 61988206Sorion IGMP_LOCK(); 62088206Sorion 62179148Scg igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp; 62279148Scg if (igi->igi_version == IGMP_VERSION_3) { 623117272Scg IF_ADDR_LOCK(ifp); 62488206Sorion TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 62588206Sorion if (ifma->ifma_addr->sa_family != AF_INET || 626117272Scg ifma->ifma_protospec == NULL) 627164614Sariff continue; 62890880Salfred#if 0 62979148Scg KASSERT(ifma->ifma_protospec != NULL, 63088206Sorion ("%s: ifma_protospec is NULL", __func__)); 63179148Scg#endif 632152420Sariff inm = (struct in_multi *)ifma->ifma_protospec; 633152420Sariff if (inm->inm_state == IGMP_LEAVING_MEMBER) { 63488206Sorion SLIST_INSERT_HEAD(&igi->igi_relinmhead, 635152420Sariff inm, inm_nrele); 636152420Sariff } 63788206Sorion inm_clear_recorded(inm); 63888206Sorion } 63988206Sorion IF_ADDR_UNLOCK(ifp); 64088206Sorion /* 64188206Sorion * Free the in_multi reference(s) for this IGMP lifecycle. 64288206Sorion */ 64388206Sorion SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, inm_nrele, 64488206Sorion tinm) { 64588206Sorion SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele); 64688206Sorion inm_release_locked(inm); 64788206Sorion } 64888206Sorion } 64988206Sorion 65088206Sorion IGMP_UNLOCK(); 65179047Scg 65288206Sorion CURVNET_RESTORE(); 653117272Scg} 654164614Sariff 65590880Salfred/* 65679047Scg * Hook for domifdetach. 657152420Sariff * 65888206Sorion * VIMAGE: curvnet should have been set by caller, but let's not assume 659117272Scg * that for now. 66088206Sorion */ 66188206Sorionvoid 66279047Scgigmp_domifdetach(struct ifnet *ifp) 66379047Scg{ 66488033Sorion struct igmp_ifinfo *igi; 665117272Scg 666117272Scg CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)", 667113056Sorion __func__, ifp, ifp->if_xname); 66888033Sorion 66988033Sorion CURVNET_SET(ifp->if_vnet); 67088033Sorion IGMP_LOCK(); 67188033Sorion 672159732Snetchild igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp; 673159732Snetchild igi_delete_locked(ifp); 674159732Snetchild 675164614Sariff IGMP_UNLOCK(); 676164614Sariff CURVNET_RESTORE(); 677117272Scg} 678117272Scg 67988033Sorionstatic void 680193640Sariffigi_delete_locked(const struct ifnet *ifp) 681164614Sariff{ 68288033Sorion INIT_VNET_INET(ifp->if_vnet); 68388033Sorion struct igmp_ifinfo *igi, *tigi; 684164614Sariff 685164614Sariff CTR3(KTR_IGMPV3, "%s: freeing igmp_ifinfo for ifp %p(%s)", 686164614Sariff __func__, ifp, ifp->if_xname); 687164614Sariff 688164614Sariff IGMP_LOCK_ASSERT(); 689164614Sariff 690164614Sariff LIST_FOREACH_SAFE(igi, &V_igi_head, igi_link, tigi) { 691164614Sariff if (igi->igi_ifp == ifp) { 692164614Sariff /* 693164614Sariff * Free deferred General Query responses. 694169278Sariff */ 695169278Sariff _IF_DRAIN(&igi->igi_gq); 696169278Sariff 697169278Sariff LIST_REMOVE(igi, igi_link); 698164614Sariff 699164614Sariff KASSERT(SLIST_EMPTY(&igi->igi_relinmhead), 700164614Sariff ("%s: there are dangling in_multi references", 70179047Scg __func__)); 702117272Scg 703113056Sorion free(igi, M_IGMP); 704113056Sorion return; 70588108Sorion } 706164614Sariff } 707164614Sariff 70888108Sorion#ifdef INVARIANTS 70998940Sscottl panic("%s: igmp_ifinfo not found for ifp %p\n", __func__, ifp); 71098940Sscottl#endif 71189516Sorion} 712164614Sariff 713164614Sariff/* 71489516Sorion * Process a received IGMPv1 query. 71598940Sscottl * Return non-zero if the message should be dropped. 716164614Sariff * 71798940Sscottl * VIMAGE: The curvnet pointer is derived from the input ifp. 71898940Sscottl */ 719169278Sariffstatic int 72098940Sscottligmp_input_v1_query(struct ifnet *ifp, const struct ip *ip) 721169278Sariff{ 722169278Sariff INIT_VNET_INET(ifp->if_vnet); 72398940Sscottl struct ifmultiaddr *ifma; 72489516Sorion struct igmp_ifinfo *igi; 72589516Sorion struct in_multi *inm; 72688108Sorion 72788108Sorion /* 72889516Sorion * IGMPv1 General Queries SHOULD always addressed to 224.0.0.1. 72989516Sorion * igmp_group is always ignored. Do not drop it as a userland 73089516Sorion * daemon may wish to see it. 73188108Sorion */ 73288108Sorion if (!in_allhosts(ip->ip_dst)) { 733164614Sariff IGMPSTAT_INC(igps_rcv_badqueries); 734164614Sariff return (0); 735169278Sariff } 736164614Sariff 737164614Sariff IGMPSTAT_INC(igps_rcv_gen_queries); 738164614Sariff 739169278Sariff /* 74088108Sorion * Switch to IGMPv1 host compatibility mode. 74189516Sorion */ 74289516Sorion IN_MULTI_LOCK(); 74389516Sorion IGMP_LOCK(); 74489516Sorion 74589516Sorion igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp; 74689516Sorion KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp)); 74789516Sorion 74889516Sorion if (igi->igi_flags & IGIF_LOOPBACK) { 74989516Sorion CTR2(KTR_IGMPV3, "ignore v1 query on IGIF_LOOPBACK ifp %p(%s)", 75089516Sorion ifp, ifp->if_xname); 75189516Sorion goto out_locked; 75288108Sorion } 75389516Sorion 75489516Sorion igmp_set_version(igi, IGMP_VERSION_1); 75589516Sorion 756164614Sariff CTR2(KTR_IGMPV3, "process v1 query on ifp %p(%s)", ifp, ifp->if_xname); 75788108Sorion 75889516Sorion /* 75989516Sorion * Start the timers in all of our group records 76089516Sorion * for the interface on which the query arrived, 76188108Sorion * except those which are already running. 76289516Sorion */ 763164614Sariff IF_ADDR_LOCK(ifp); 76489516Sorion TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 76589516Sorion if (ifma->ifma_addr->sa_family != AF_INET || 76689516Sorion ifma->ifma_protospec == NULL) 76789516Sorion continue; 768164614Sariff inm = (struct in_multi *)ifma->ifma_protospec; 76989516Sorion if (inm->inm_timer != 0) 77089516Sorion continue; 77189516Sorion switch (inm->inm_state) { 77289516Sorion case IGMP_NOT_MEMBER: 77389516Sorion case IGMP_SILENT_MEMBER: 77489516Sorion break; 77589516Sorion case IGMP_G_QUERY_PENDING_MEMBER: 776164614Sariff case IGMP_SG_QUERY_PENDING_MEMBER: 77789516Sorion case IGMP_REPORTING_MEMBER: 77889516Sorion case IGMP_IDLE_MEMBER: 77989516Sorion case IGMP_LAZY_MEMBER: 78089516Sorion case IGMP_SLEEPING_MEMBER: 78189516Sorion case IGMP_AWAKENING_MEMBER: 78289516Sorion inm->inm_state = IGMP_REPORTING_MEMBER; 783169278Sariff inm->inm_timer = IGMP_RANDOM_DELAY( 784164614Sariff IGMP_V1V2_MAX_RI * PR_FASTHZ); 785164614Sariff V_current_state_timers_running = 1; 78698940Sscottl break; 78789516Sorion case IGMP_LEAVING_MEMBER: 78889516Sorion break; 789171363Sariff } 790171363Sariff } 791171363Sariff IF_ADDR_UNLOCK(ifp); 792171363Sariff 793171363Sariffout_locked: 79489516Sorion IGMP_UNLOCK(); 79589516Sorion IN_MULTI_UNLOCK(); 79688108Sorion 79788108Sorion return (0); 79888108Sorion} 79988108Sorion 80088209Sorion/* 80189516Sorion * Process a received IGMPv2 general or group-specific query. 80289516Sorion */ 80389516Sorionstatic int 80489516Sorionigmp_input_v2_query(struct ifnet *ifp, const struct ip *ip, 80589516Sorion const struct igmp *igmp) 80689516Sorion{ 807169278Sariff INIT_VNET_INET(ifp->if_vnet); 808164614Sariff struct ifmultiaddr *ifma; 80988209Sorion struct igmp_ifinfo *igi; 810164614Sariff struct in_multi *inm; 811164614Sariff uint16_t timer; 81298940Sscottl 81388108Sorion /* 81488108Sorion * Perform lazy allocation of IGMP link info if required, 81588108Sorion * and switch to IGMPv2 host compatibility mode. 81679148Scg */ 81779047Scg IN_MULTI_LOCK(); 81879148Scg IGMP_LOCK(); 81979148Scg 82079148Scg igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp; 821111183Scognet KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp)); 822111183Scognet 82379148Scg if (igi->igi_flags & IGIF_LOOPBACK) { 82479148Scg CTR2(KTR_IGMPV3, "ignore v2 query on IGIF_LOOPBACK ifp %p(%s)", 82579047Scg ifp, ifp->if_xname); 82679047Scg goto out_locked; 82779047Scg } 82879047Scg 829164614Sariff igmp_set_version(igi, IGMP_VERSION_2); 83079047Scg 83179148Scg timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE; 83279047Scg if (timer == 0) 83379148Scg timer = 1; 83479148Scg 835102106Sorion if (!in_nullhost(igmp->igmp_group)) { 836117296Scg /* 837152420Sariff * IGMPv2 Group-Specific Query. 838152420Sariff * If this is a group-specific IGMPv2 query, we need only 839152420Sariff * look up the single group to process it. 840152420Sariff */ 841169278Sariff inm = inm_lookup(ifp, igmp->igmp_group); 842152644Syongari if (inm != NULL) { 843102106Sorion CTR3(KTR_IGMPV3, "process v2 query %s on ifp %p(%s)", 844102106Sorion inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname); 84579047Scg igmp_v2_update_group(inm, timer); 846157028Sariff } 84779148Scg IGMPSTAT_INC(igps_rcv_group_queries); 848157028Sariff } else { 849157028Sariff /* 850157028Sariff * IGMPv2 General Query. 85179047Scg * If this was not sent to the all-hosts group, ignore it. 85283617Scg */ 853164614Sariff if (in_allhosts(ip->ip_dst)) { 85483617Scg /* 855164614Sariff * For each reporting group joined on this 85679148Scg * interface, kick the report timer. 857164614Sariff */ 85879047Scg CTR2(KTR_IGMPV3, 85979047Scg "process v2 general query on ifp %p(%s)", 86079047Scg ifp, ifp->if_xname); 86179047Scg 86279047Scg IF_ADDR_LOCK(ifp); 863152420Sariff TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 864152420Sariff if (ifma->ifma_addr->sa_family != AF_INET || 86579047Scg ifma->ifma_protospec == NULL) 866152420Sariff continue; 867152420Sariff inm = (struct in_multi *)ifma->ifma_protospec; 868152420Sariff igmp_v2_update_group(inm, timer); 869152420Sariff } 870152420Sariff IF_ADDR_UNLOCK(ifp); 871152420Sariff } 872152420Sariff IGMPSTAT_INC(igps_rcv_gen_queries); 873152420Sariff } 874152420Sariff 875152420Sariffout_locked: 876152420Sariff IGMP_UNLOCK(); 87779148Scg IN_MULTI_UNLOCK(); 878152420Sariff 87979047Scg return (0); 88079047Scg} 88179047Scg 88279047Scg/* 88379047Scg * Update the report timer on a group in response to an IGMPv2 query. 884154168Sariff * 885164614Sariff * If we are becoming the reporting member for this group, start the timer. 886152420Sariff * If we already are the reporting member for this group, and timer is 88779047Scg * below the threshold, reset it. 888164614Sariff * 88979047Scg * We may be updating the group for the first time since we switched 890170873Sariff * to IGMPv3. If we are, then we must clear any recorded source lists, 891167608Sariff * and transition to REPORTING state; the group timer is overloaded 89279047Scg * for group and group-source query responses. 89379047Scg * 894152420Sariff * Unlike IGMPv3, the delay per group should be jittered 895152420Sariff * to avoid bursts of IGMPv2 reports. 896154168Sariff */ 89790880Salfredstatic void 89890880Salfredigmp_v2_update_group(struct in_multi *inm, const int timer) 89990880Salfred{ 90090880Salfred INIT_VNET_INET(curvnet); 901152420Sariff 90290880Salfred CTR4(KTR_IGMPV3, "%s: %s/%s timer=%d", __func__, 90390880Salfred inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname, timer); 90490880Salfred 90590880Salfred IN_MULTI_LOCK_ASSERT(); 90690880Salfred 90790880Salfred switch (inm->inm_state) { 90890880Salfred case IGMP_NOT_MEMBER: 909102106Sorion case IGMP_SILENT_MEMBER: 910169278Sariff break; 911169278Sariff case IGMP_REPORTING_MEMBER: 912169278Sariff if (inm->inm_timer != 0 && 913169278Sariff inm->inm_timer <= timer) { 914169278Sariff CTR1(KTR_IGMPV3, "%s: REPORTING and timer running, " 915169278Sariff "skipping.", __func__); 916169278Sariff break; 917169278Sariff } 918169278Sariff /* FALLTHROUGH */ 919152644Syongari case IGMP_SG_QUERY_PENDING_MEMBER: 920152644Syongari case IGMP_G_QUERY_PENDING_MEMBER: 921152644Syongari case IGMP_IDLE_MEMBER: 922152644Syongari case IGMP_LAZY_MEMBER: 923152644Syongari case IGMP_AWAKENING_MEMBER: 924152644Syongari CTR1(KTR_IGMPV3, "%s: ->REPORTING", __func__); 925127651Smatk inm->inm_state = IGMP_REPORTING_MEMBER; 926127651Smatk inm->inm_timer = IGMP_RANDOM_DELAY(timer); 927127651Smatk V_current_state_timers_running = 1; 928127651Smatk break; 929127651Smatk case IGMP_SLEEPING_MEMBER: 930127651Smatk CTR1(KTR_IGMPV3, "%s: ->AWAKENING", __func__); 931152644Syongari inm->inm_state = IGMP_AWAKENING_MEMBER; 932152644Syongari break; 933152644Syongari case IGMP_LEAVING_MEMBER: 934118729Sorion break; 935118729Sorion } 936118729Sorion} 937152644Syongari 938118729Sorion/* 939118729Sorion * Process a received IGMPv3 general, group-specific or 940118729Sorion * group-and-source-specific query. 941118729Sorion * Assumes m has already been pulled up to the full IGMP message length. 942118729Sorion * Return 0 if successful, otherwise an appropriate error code is returned. 94379148Scg */ 944127135Snjlstatic int 945127135Snjligmp_input_v3_query(struct ifnet *ifp, const struct ip *ip, 946127135Snjl /*const*/ struct igmpv3 *igmpv3) 947127135Snjl{ 948118729Sorion INIT_VNET_INET(ifp->if_vnet); 94979047Scg struct igmp_ifinfo *igi; 95079047Scg struct in_multi *inm; 95179047Scg uint32_t maxresp, nsrc, qqi; 95279047Scg uint16_t timer; 95379148Scg uint8_t qrv; 95479047Scg 95579047Scg CTR2(KTR_IGMPV3, "process v3 query on ifp %p(%s)", ifp, ifp->if_xname); 95679047Scg 95779047Scg maxresp = igmpv3->igmp_code; /* in 1/10ths of a second */ 95879047Scg if (maxresp >= 128) { 959164614Sariff maxresp = IGMP_MANT(igmpv3->igmp_code) << 960164614Sariff (IGMP_EXP(igmpv3->igmp_code) + 3); 961164614Sariff } 962169278Sariff 963169278Sariff /* 964164614Sariff * Robustness must never be less than 2 for on-wire IGMPv3. 965164614Sariff * FIXME: Check if ifp has IGIF_LOOPBACK set, as we make 966164614Sariff * an exception for interfaces whose IGMPv3 state changes 967164614Sariff * are redirected to loopback (e.g. MANET). 968164614Sariff */ 969164614Sariff qrv = IGMP_QRV(igmpv3->igmp_misc); 970164614Sariff if (qrv < 2) { 971164614Sariff CTR3(KTR_IGMPV3, "%s: clamping qrv %d to %d", __func__, 972164614Sariff qrv, IGMP_RV_INIT); 973164614Sariff qrv = IGMP_RV_INIT; 974164614Sariff } 975164614Sariff 976169278Sariff qqi = igmpv3->igmp_qqi; 977169278Sariff if (qqi >= 128) { 978169278Sariff qqi = IGMP_MANT(igmpv3->igmp_qqi) << 979169278Sariff (IGMP_EXP(igmpv3->igmp_qqi) + 3); 98079047Scg } 98179047Scg 982169278Sariff timer = maxresp * PR_FASTHZ / IGMP_TIMER_SCALE; 983169278Sariff if (timer == 0) 984169278Sariff timer = 1; 985169278Sariff 986174552Sariff nsrc = ntohs(igmpv3->igmp_numsrc); 987174552Sariff 988174552Sariff IN_MULTI_LOCK(); 989174552Sariff IGMP_LOCK(); 99089516Sorion 991127135Snjl igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp; 992169278Sariff KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp)); 993169278Sariff 994169278Sariff if (igi->igi_flags & IGIF_LOOPBACK) { 99589516Sorion CTR2(KTR_IGMPV3, "ignore v3 query on IGIF_LOOPBACK ifp %p(%s)", 99689516Sorion ifp, ifp->if_xname); 99789516Sorion goto out_locked; 99889516Sorion } 99979148Scg 100079047Scg igmp_set_version(igi, IGMP_VERSION_3); 100179047Scg 100279047Scg igi->igi_rv = qrv; 100379047Scg igi->igi_qi = qqi; 100479047Scg igi->igi_qri = maxresp; 100579047Scg 100679047Scg CTR4(KTR_IGMPV3, "%s: qrv %d qi %d qri %d", __func__, qrv, qqi, 1007154132Sariff maxresp); 1008154132Sariff 1009154168Sariff if (in_nullhost(igmpv3->igmp_group)) { 1010154168Sariff /* 1011154132Sariff * IGMPv3 General Query. 1012154168Sariff * Schedule a current-state report on this ifp for 1013154378Sariff * all groups, possibly containing source lists. 1014154168Sariff */ 1015191911Smav IGMPSTAT_INC(igps_rcv_gen_queries); 1016156376Sariff 1017154378Sariff if (!in_allhosts(ip->ip_dst) || nsrc > 0) { 1018158210Sariff /* 1019156308Sariff * General Queries SHOULD be directed to 224.0.0.1. 1020158210Sariff * A general query with a source list has undefined 1021161300Syongari * behaviour; discard it. 1022164783Sariff */ 1023164614Sariff IGMPSTAT_INC(igps_rcv_badqueries); 1024164614Sariff goto out_locked; 1025154132Sariff } 1026154168Sariff 1027154168Sariff CTR2(KTR_IGMPV3, "process v3 general query on ifp %p(%s)", 1028154168Sariff ifp, ifp->if_xname); 1029154168Sariff 1030154132Sariff /* 103179047Scg * If there is a pending General Query response 103279148Scg * scheduled earlier than the selected delay, do 103379047Scg * not schedule any other reports. 103486795Sorion * Otherwise, reset the interface timer. 103586708Sorion */ 103686708Sorion if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer) { 1037174552Sariff igi->igi_v3_timer = IGMP_RANDOM_DELAY(timer); 1038174552Sariff V_interface_timers_running = 1; 103994647Sjhay } 104079047Scg } else { 1041169278Sariff /* 1042169278Sariff * IGMPv3 Group-specific or Group-and-source-specific Query. 1043169278Sariff * 1044169278Sariff * Group-source-specific queries are throttled on 1045169278Sariff * a per-group basis to defeat denial-of-service attempts. 1046169278Sariff * Queries for groups we are not a member of on this 1047169278Sariff * link are simply ignored. 1048169278Sariff */ 1049169278Sariff inm = inm_lookup(ifp, igmpv3->igmp_group); 1050169278Sariff if (inm == NULL) 1051169278Sariff goto out_locked; 1052169278Sariff if (nsrc > 0) { 1053169278Sariff IGMPSTAT_INC(igps_rcv_gsr_queries); 1054169278Sariff if (!ratecheck(&inm->inm_lastgsrtv, 1055169278Sariff &V_igmp_gsrdelay)) { 1056169278Sariff CTR1(KTR_IGMPV3, "%s: GS query throttled.", 1057169278Sariff __func__); 1058169278Sariff IGMPSTAT_INC(igps_drop_gsr_queries); 1059169278Sariff goto out_locked; 1060169278Sariff } 1061169278Sariff } else { 1062169278Sariff IGMPSTAT_INC(igps_rcv_group_queries); 1063169278Sariff } 1064169278Sariff CTR3(KTR_IGMPV3, "process v3 %s query on ifp %p(%s)", 1065169278Sariff inet_ntoa(igmpv3->igmp_group), ifp, ifp->if_xname); 1066169278Sariff /* 1067169278Sariff * If there is a pending General Query response 1068169278Sariff * scheduled sooner than the selected delay, no 1069164614Sariff * further report need be scheduled. 107079148Scg * Otherwise, prepare to respond to the 107179047Scg * group-specific or group-and-source query. 107283617Scg */ 107383617Scg if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer) 107483617Scg igmp_input_v3_group_query(inm, igi, timer, igmpv3); 107583617Scg } 107679148Scg 1077169278Sariffout_locked: 1078169278Sariff IGMP_UNLOCK(); 1079169278Sariff IN_MULTI_UNLOCK(); 1080169278Sariff 1081164614Sariff return (0); 108279148Scg} 1083164614Sariff 1084164614Sariff/* 1085169278Sariff * Process a recieved IGMPv3 group-specific or group-and-source-specific 1086169278Sariff * query. 1087169278Sariff * Return <0 if any error occured. Currently this is ignored. 1088164614Sariff */ 1089164614Sariffstatic int 109098940Sscottligmp_input_v3_group_query(struct in_multi *inm, struct igmp_ifinfo *igi, 109198940Sscottl int timer, /*const*/ struct igmpv3 *igmpv3) 1092164614Sariff{ 109379047Scg INIT_VNET_INET(curvnet); 109479047Scg int retval; 109579047Scg uint16_t nsrc; 109679047Scg 109789516Sorion IN_MULTI_LOCK_ASSERT(); 109889516Sorion IGMP_LOCK_ASSERT(); 109989516Sorion 110089516Sorion retval = 0; 110179047Scg 1102118729Sorion switch (inm->inm_state) { 110379047Scg case IGMP_NOT_MEMBER: 110479047Scg case IGMP_SILENT_MEMBER: 1105118729Sorion case IGMP_SLEEPING_MEMBER: 110679047Scg case IGMP_LAZY_MEMBER: 1107155568Sariff case IGMP_AWAKENING_MEMBER: 1108155568Sariff case IGMP_IDLE_MEMBER: 1109167773Sariff case IGMP_LEAVING_MEMBER: 1110167773Sariff return (retval); 1111169278Sariff break; 1112169278Sariff case IGMP_REPORTING_MEMBER: 1113155568Sariff case IGMP_G_QUERY_PENDING_MEMBER: 1114155568Sariff case IGMP_SG_QUERY_PENDING_MEMBER: 1115152420Sariff break; 1116152420Sariff } 111779047Scg 1118164614Sariff nsrc = ntohs(igmpv3->igmp_numsrc); 111979047Scg 112079047Scg /* 112179047Scg * Deal with group-specific queries upfront. 112279047Scg * If any group query is already pending, purge any recorded 112379047Scg * source-list state if it exists, and schedule a query response 112479047Scg * for this group-specific query. 112579047Scg */ 112679047Scg if (nsrc == 0) { 112779047Scg if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER || 112879047Scg inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) { 1129164614Sariff inm_clear_recorded(inm); 113079047Scg timer = min(inm->inm_timer, timer); 113179047Scg } 113279148Scg inm->inm_state = IGMP_G_QUERY_PENDING_MEMBER; 113379148Scg inm->inm_timer = IGMP_RANDOM_DELAY(timer); 1134118729Sorion V_current_state_timers_running = 1; 1135118729Sorion return (retval); 1136155568Sariff } 1137167773Sariff 1138169278Sariff /* 113979047Scg * Deal with the case where a group-and-source-specific query has 1140152420Sariff * been received but a group-specific query is already pending. 114179047Scg */ 1142164614Sariff if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER) { 114379047Scg timer = min(inm->inm_timer, timer); 114479047Scg inm->inm_timer = IGMP_RANDOM_DELAY(timer); 1145120101Snjl V_current_state_timers_running = 1; 1146120101Snjl return (retval); 1147120101Snjl } 1148120101Snjl 1149120101Snjl /* 1150120101Snjl * Finally, deal with the case where a group-and-source-specific 1151120101Snjl * query has been received, where a response to a previous g-s-r 1152120101Snjl * query exists, or none exists. 1153120101Snjl * In this case, we need to parse the source-list which the Querier 1154120101Snjl * has provided us with and check if we have any source list filter 1155120101Snjl * entries at T1 for these sources. If we do not, there is no need 1156120101Snjl * schedule a report and the query may be dropped. 1157120101Snjl * If we do, we must record them and schedule a current-state 1158120101Snjl * report for those sources. 1159120101Snjl * FIXME: Handling source lists larger than 1 mbuf requires that 1160120101Snjl * we pass the mbuf chain pointer down to this function, and use 1161120101Snjl * m_getptr() to walk the chain. 1162120101Snjl */ 1163120101Snjl if (inm->inm_nsrc > 0) { 1164120101Snjl const struct in_addr *ap; 1165120101Snjl int i, nrecorded; 1166120101Snjl 116779047Scg ap = (const struct in_addr *)(igmpv3 + 1); 116888361Sorion nrecorded = 0; 116988361Sorion for (i = 0; i < nsrc; i++, ap++) { 117088361Sorion retval = inm_record_source(inm, ap->s_addr); 117188361Sorion if (retval < 0) 117288361Sorion break; 1173117296Scg nrecorded += retval; 1174152420Sariff } 117588361Sorion if (nrecorded > 0) { 117688361Sorion CTR1(KTR_IGMPV3, 117788361Sorion "%s: schedule response to SG query", __func__); 1178152420Sariff inm->inm_state = IGMP_SG_QUERY_PENDING_MEMBER; 117988361Sorion inm->inm_timer = IGMP_RANDOM_DELAY(timer); 1180152420Sariff V_current_state_timers_running = 1; 118188361Sorion } 118288361Sorion } 1183152420Sariff 1184164614Sariff return (retval); 118588361Sorion} 118688361Sorion 118788361Sorion/* 118879047Scg * Process a received IGMPv1 host membership report. 118979047Scg * 119079047Scg * NOTE: 0.0.0.0 workaround breaks const correctness. 119188361Sorion */ 119279047Scgstatic int 119379047Scgigmp_input_v1_report(struct ifnet *ifp, /*const*/ struct ip *ip, 119479047Scg /*const*/ struct igmp *igmp) 1195152420Sariff{ 119679047Scg INIT_VNET_INET(ifp->if_vnet); 119779047Scg struct in_ifaddr *ia; 119879047Scg struct in_multi *inm; 1199152420Sariff 1200164614Sariff IGMPSTAT_INC(igps_rcv_reports); 120179047Scg 120279047Scg if (ifp->if_flags & IFF_LOOPBACK) 1203120101Snjl return (0); 1204155568Sariff 1205120101Snjl if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr) || 120679047Scg !in_hosteq(igmp->igmp_group, ip->ip_dst))) { 120779047Scg IGMPSTAT_INC(igps_rcv_badreports); 1208164614Sariff return (EINVAL); 120979047Scg } 121088361Sorion 121188361Sorion /* 121288361Sorion * RFC 3376, Section 4.2.13, 9.2, 9.3: 121388361Sorion * Booting clients may use the source address 0.0.0.0. Some 121488361Sorion * IGMP daemons may not know how to use IP_RECVIF to determine 121588361Sorion * the interface upon which this message was received. 121688361Sorion * Replace 0.0.0.0 with the subnet address if told to do so. 121788361Sorion */ 121888361Sorion if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) { 1219164614Sariff IFP_TO_IA(ifp, ia); 122079047Scg if (ia != NULL) 122179047Scg ip->ip_src.s_addr = htonl(ia->ia_subnet); 122279047Scg } 122379047Scg 122479047Scg CTR3(KTR_IGMPV3, "process v1 report %s on ifp %p(%s)", 122579047Scg inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname); 122679047Scg 122788361Sorion /* 122879047Scg * IGMPv1 report suppression. 122979047Scg * If we are a member of this group, and our membership should be 123079047Scg * reported, stop our group timer and transition to the 'lazy' state. 123179047Scg */ 123279047Scg IN_MULTI_LOCK(); 123379047Scg inm = inm_lookup(ifp, igmp->igmp_group); 123479047Scg if (inm != NULL) { 123582180Scg struct igmp_ifinfo *igi; 123679047Scg 123779047Scg igi = inm->inm_igi; 123879047Scg if (igi == NULL) { 1239132236Stanimura KASSERT(igi != NULL, 124079047Scg ("%s: no igi for ifp %p", __func__, ifp)); 1241 goto out_locked; 1242 } 1243 1244 IGMPSTAT_INC(igps_rcv_ourreports); 1245 1246 /* 1247 * If we are in IGMPv3 host mode, do not allow the 1248 * other host's IGMPv1 report to suppress our reports 1249 * unless explicitly configured to do so. 1250 */ 1251 if (igi->igi_version == IGMP_VERSION_3) { 1252 if (V_igmp_legacysupp) 1253 igmp_v3_suppress_group_record(inm); 1254 goto out_locked; 1255 } 1256 1257 inm->inm_timer = 0; 1258 1259 switch (inm->inm_state) { 1260 case IGMP_NOT_MEMBER: 1261 case IGMP_SILENT_MEMBER: 1262 break; 1263 case IGMP_IDLE_MEMBER: 1264 case IGMP_LAZY_MEMBER: 1265 case IGMP_AWAKENING_MEMBER: 1266 CTR3(KTR_IGMPV3, 1267 "report suppressed for %s on ifp %p(%s)", 1268 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname); 1269 case IGMP_SLEEPING_MEMBER: 1270 inm->inm_state = IGMP_SLEEPING_MEMBER; 1271 break; 1272 case IGMP_REPORTING_MEMBER: 1273 CTR3(KTR_IGMPV3, 1274 "report suppressed for %s on ifp %p(%s)", 1275 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname); 1276 if (igi->igi_version == IGMP_VERSION_1) 1277 inm->inm_state = IGMP_LAZY_MEMBER; 1278 else if (igi->igi_version == IGMP_VERSION_2) 1279 inm->inm_state = IGMP_SLEEPING_MEMBER; 1280 break; 1281 case IGMP_G_QUERY_PENDING_MEMBER: 1282 case IGMP_SG_QUERY_PENDING_MEMBER: 1283 case IGMP_LEAVING_MEMBER: 1284 break; 1285 } 1286 } 1287 1288out_locked: 1289 IN_MULTI_UNLOCK(); 1290 1291 return (0); 1292} 1293 1294/* 1295 * Process a received IGMPv2 host membership report. 1296 * 1297 * NOTE: 0.0.0.0 workaround breaks const correctness. 1298 */ 1299static int 1300igmp_input_v2_report(struct ifnet *ifp, /*const*/ struct ip *ip, 1301 /*const*/ struct igmp *igmp) 1302{ 1303 INIT_VNET_INET(ifp->if_vnet); 1304 struct in_ifaddr *ia; 1305 struct in_multi *inm; 1306 1307 /* 1308 * Make sure we don't hear our own membership report. Fast 1309 * leave requires knowing that we are the only member of a 1310 * group. 1311 */ 1312 IFP_TO_IA(ifp, ia); 1313 if (ia != NULL && in_hosteq(ip->ip_src, IA_SIN(ia)->sin_addr)) 1314 return (0); 1315 1316 IGMPSTAT_INC(igps_rcv_reports); 1317 1318 if (ifp->if_flags & IFF_LOOPBACK) 1319 return (0); 1320 1321 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) || 1322 !in_hosteq(igmp->igmp_group, ip->ip_dst)) { 1323 IGMPSTAT_INC(igps_rcv_badreports); 1324 return (EINVAL); 1325 } 1326 1327 /* 1328 * RFC 3376, Section 4.2.13, 9.2, 9.3: 1329 * Booting clients may use the source address 0.0.0.0. Some 1330 * IGMP daemons may not know how to use IP_RECVIF to determine 1331 * the interface upon which this message was received. 1332 * Replace 0.0.0.0 with the subnet address if told to do so. 1333 */ 1334 if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) { 1335 if (ia != NULL) 1336 ip->ip_src.s_addr = htonl(ia->ia_subnet); 1337 } 1338 1339 CTR3(KTR_IGMPV3, "process v2 report %s on ifp %p(%s)", 1340 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname); 1341 1342 /* 1343 * IGMPv2 report suppression. 1344 * If we are a member of this group, and our membership should be 1345 * reported, and our group timer is pending or about to be reset, 1346 * stop our group timer by transitioning to the 'lazy' state. 1347 */ 1348 IN_MULTI_LOCK(); 1349 inm = inm_lookup(ifp, igmp->igmp_group); 1350 if (inm != NULL) { 1351 struct igmp_ifinfo *igi; 1352 1353 igi = inm->inm_igi; 1354 KASSERT(igi != NULL, ("%s: no igi for ifp %p", __func__, ifp)); 1355 1356 IGMPSTAT_INC(igps_rcv_ourreports); 1357 1358 /* 1359 * If we are in IGMPv3 host mode, do not allow the 1360 * other host's IGMPv1 report to suppress our reports 1361 * unless explicitly configured to do so. 1362 */ 1363 if (igi->igi_version == IGMP_VERSION_3) { 1364 if (V_igmp_legacysupp) 1365 igmp_v3_suppress_group_record(inm); 1366 goto out_locked; 1367 } 1368 1369 inm->inm_timer = 0; 1370 1371 switch (inm->inm_state) { 1372 case IGMP_NOT_MEMBER: 1373 case IGMP_SILENT_MEMBER: 1374 case IGMP_SLEEPING_MEMBER: 1375 break; 1376 case IGMP_REPORTING_MEMBER: 1377 case IGMP_IDLE_MEMBER: 1378 case IGMP_AWAKENING_MEMBER: 1379 CTR3(KTR_IGMPV3, 1380 "report suppressed for %s on ifp %p(%s)", 1381 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname); 1382 case IGMP_LAZY_MEMBER: 1383 inm->inm_state = IGMP_LAZY_MEMBER; 1384 break; 1385 case IGMP_G_QUERY_PENDING_MEMBER: 1386 case IGMP_SG_QUERY_PENDING_MEMBER: 1387 case IGMP_LEAVING_MEMBER: 1388 break; 1389 } 1390 } 1391 1392out_locked: 1393 IN_MULTI_UNLOCK(); 1394 1395 return (0); 1396} 1397 1398void 1399igmp_input(struct mbuf *m, int off) 1400{ 1401 int iphlen; 1402 struct ifnet *ifp; 1403 struct igmp *igmp; 1404 struct ip *ip; 1405 int igmplen; 1406 int minlen; 1407 int queryver; 1408 1409 CTR3(KTR_IGMPV3, "%s: called w/mbuf (%p,%d)", __func__, m, off); 1410 1411 ifp = m->m_pkthdr.rcvif; 1412 INIT_VNET_INET(ifp->if_vnet); 1413 1414 IGMPSTAT_INC(igps_rcv_total); 1415 1416 ip = mtod(m, struct ip *); 1417 iphlen = off; 1418 igmplen = ip->ip_len; 1419 1420 /* 1421 * Validate lengths. 1422 */ 1423 if (igmplen < IGMP_MINLEN) { 1424 IGMPSTAT_INC(igps_rcv_tooshort); 1425 m_freem(m); 1426 return; 1427 } 1428 1429 /* 1430 * Always pullup to the minimum size for v1/v2 or v3 1431 * to amortize calls to m_pullup(). 1432 */ 1433 minlen = iphlen; 1434 if (igmplen >= IGMP_V3_QUERY_MINLEN) 1435 minlen += IGMP_V3_QUERY_MINLEN; 1436 else 1437 minlen += IGMP_MINLEN; 1438 if ((m->m_flags & M_EXT || m->m_len < minlen) && 1439 (m = m_pullup(m, minlen)) == 0) { 1440 IGMPSTAT_INC(igps_rcv_tooshort); 1441 return; 1442 } 1443 ip = mtod(m, struct ip *); 1444 1445 if (ip->ip_ttl != 1) { 1446 IGMPSTAT_INC(igps_rcv_badttl); 1447 m_freem(m); 1448 return; 1449 } 1450 1451 /* 1452 * Validate checksum. 1453 */ 1454 m->m_data += iphlen; 1455 m->m_len -= iphlen; 1456 igmp = mtod(m, struct igmp *); 1457 if (in_cksum(m, igmplen)) { 1458 IGMPSTAT_INC(igps_rcv_badsum); 1459 m_freem(m); 1460 return; 1461 } 1462 m->m_data -= iphlen; 1463 m->m_len += iphlen; 1464 1465 switch (igmp->igmp_type) { 1466 case IGMP_HOST_MEMBERSHIP_QUERY: 1467 if (igmplen == IGMP_MINLEN) { 1468 if (igmp->igmp_code == 0) 1469 queryver = IGMP_VERSION_1; 1470 else 1471 queryver = IGMP_VERSION_2; 1472 } else if (igmplen >= IGMP_V3_QUERY_MINLEN) { 1473 queryver = IGMP_VERSION_3; 1474 } else { 1475 IGMPSTAT_INC(igps_rcv_tooshort); 1476 m_freem(m); 1477 return; 1478 } 1479 1480 switch (queryver) { 1481 case IGMP_VERSION_1: 1482 IGMPSTAT_INC(igps_rcv_v1v2_queries); 1483 if (!V_igmp_v1enable) 1484 break; 1485 if (igmp_input_v1_query(ifp, ip) != 0) { 1486 m_freem(m); 1487 return; 1488 } 1489 break; 1490 1491 case IGMP_VERSION_2: 1492 IGMPSTAT_INC(igps_rcv_v1v2_queries); 1493 if (!V_igmp_v2enable) 1494 break; 1495 if (igmp_input_v2_query(ifp, ip, igmp) != 0) { 1496 m_freem(m); 1497 return; 1498 } 1499 break; 1500 1501 case IGMP_VERSION_3: { 1502 struct igmpv3 *igmpv3; 1503 uint16_t igmpv3len; 1504 uint16_t srclen; 1505 int nsrc; 1506 1507 IGMPSTAT_INC(igps_rcv_v3_queries); 1508 igmpv3 = (struct igmpv3 *)igmp; 1509 /* 1510 * Validate length based on source count. 1511 */ 1512 nsrc = ntohs(igmpv3->igmp_numsrc); 1513 srclen = sizeof(struct in_addr) * nsrc; 1514 if (nsrc * sizeof(in_addr_t) > srclen) { 1515 IGMPSTAT_INC(igps_rcv_tooshort); 1516 return; 1517 } 1518 /* 1519 * m_pullup() may modify m, so pullup in 1520 * this scope. 1521 */ 1522 igmpv3len = iphlen + IGMP_V3_QUERY_MINLEN + 1523 srclen; 1524 if ((m->m_flags & M_EXT || 1525 m->m_len < igmpv3len) && 1526 (m = m_pullup(m, igmpv3len)) == NULL) { 1527 IGMPSTAT_INC(igps_rcv_tooshort); 1528 return; 1529 } 1530 igmpv3 = (struct igmpv3 *)(mtod(m, uint8_t *) 1531 + iphlen); 1532 if (igmp_input_v3_query(ifp, ip, igmpv3) != 0) { 1533 m_freem(m); 1534 return; 1535 } 1536 } 1537 break; 1538 } 1539 break; 1540 1541 case IGMP_v1_HOST_MEMBERSHIP_REPORT: 1542 if (!V_igmp_v1enable) 1543 break; 1544 if (igmp_input_v1_report(ifp, ip, igmp) != 0) { 1545 m_freem(m); 1546 return; 1547 } 1548 break; 1549 1550 case IGMP_v2_HOST_MEMBERSHIP_REPORT: 1551 if (!V_igmp_v2enable) 1552 break; 1553 if (!ip_checkrouteralert(m)) 1554 IGMPSTAT_INC(igps_rcv_nora); 1555 if (igmp_input_v2_report(ifp, ip, igmp) != 0) { 1556 m_freem(m); 1557 return; 1558 } 1559 break; 1560 1561 case IGMP_v3_HOST_MEMBERSHIP_REPORT: 1562 /* 1563 * Hosts do not need to process IGMPv3 membership reports, 1564 * as report suppression is no longer required. 1565 */ 1566 if (!ip_checkrouteralert(m)) 1567 IGMPSTAT_INC(igps_rcv_nora); 1568 break; 1569 1570 default: 1571 break; 1572 } 1573 1574 /* 1575 * Pass all valid IGMP packets up to any process(es) listening on a 1576 * raw IGMP socket. 1577 */ 1578 rip_input(m, off); 1579} 1580 1581 1582/* 1583 * Fast timeout handler (global). 1584 * VIMAGE: Timeout handlers are expected to service all vimages. 1585 */ 1586void 1587igmp_fasttimo(void) 1588{ 1589 VNET_ITERATOR_DECL(vnet_iter); 1590 1591 VNET_LIST_RLOCK(); 1592 VNET_FOREACH(vnet_iter) { 1593 CURVNET_SET(vnet_iter); 1594 igmp_fasttimo_vnet(); 1595 CURVNET_RESTORE(); 1596 } 1597 VNET_LIST_RUNLOCK(); 1598} 1599 1600/* 1601 * Fast timeout handler (per-vnet). 1602 * Sends are shuffled off to a netisr to deal with Giant. 1603 * 1604 * VIMAGE: Assume caller has set up our curvnet. 1605 */ 1606static void 1607igmp_fasttimo_vnet(void) 1608{ 1609 INIT_VNET_INET(curvnet); 1610 struct ifqueue scq; /* State-change packets */ 1611 struct ifqueue qrq; /* Query response packets */ 1612 struct ifnet *ifp; 1613 struct igmp_ifinfo *igi; 1614 struct ifmultiaddr *ifma, *tifma; 1615 struct in_multi *inm; 1616 int loop, uri_fasthz; 1617 1618 loop = 0; 1619 uri_fasthz = 0; 1620 1621 /* 1622 * Quick check to see if any work needs to be done, in order to 1623 * minimize the overhead of fasttimo processing. 1624 * SMPng: XXX Unlocked reads. 1625 */ 1626 if (!V_current_state_timers_running && 1627 !V_interface_timers_running && 1628 !V_state_change_timers_running) 1629 return; 1630 1631 IN_MULTI_LOCK(); 1632 IGMP_LOCK(); 1633 1634 /* 1635 * IGMPv3 General Query response timer processing. 1636 */ 1637 if (V_interface_timers_running) { 1638 CTR1(KTR_IGMPV3, "%s: interface timers running", __func__); 1639 1640 V_interface_timers_running = 0; 1641 LIST_FOREACH(igi, &V_igi_head, igi_link) { 1642 if (igi->igi_v3_timer == 0) { 1643 /* Do nothing. */ 1644 } else if (--igi->igi_v3_timer == 0) { 1645 igmp_v3_dispatch_general_query(igi); 1646 } else { 1647 V_interface_timers_running = 1; 1648 } 1649 } 1650 } 1651 1652 if (!V_current_state_timers_running && 1653 !V_state_change_timers_running) 1654 goto out_locked; 1655 1656 V_current_state_timers_running = 0; 1657 V_state_change_timers_running = 0; 1658 1659 CTR1(KTR_IGMPV3, "%s: state change timers running", __func__); 1660 1661 /* 1662 * IGMPv1/v2/v3 host report and state-change timer processing. 1663 * Note: Processing a v3 group timer may remove a node. 1664 */ 1665 LIST_FOREACH(igi, &V_igi_head, igi_link) { 1666 ifp = igi->igi_ifp; 1667 1668 if (igi->igi_version == IGMP_VERSION_3) { 1669 loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0; 1670 uri_fasthz = IGMP_RANDOM_DELAY(igi->igi_uri * 1671 PR_FASTHZ); 1672 1673 memset(&qrq, 0, sizeof(struct ifqueue)); 1674 IFQ_SET_MAXLEN(&qrq, IGMP_MAX_G_GS_PACKETS); 1675 1676 memset(&scq, 0, sizeof(struct ifqueue)); 1677 IFQ_SET_MAXLEN(&scq, IGMP_MAX_STATE_CHANGE_PACKETS); 1678 } 1679 1680 IF_ADDR_LOCK(ifp); 1681 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, 1682 tifma) { 1683 if (ifma->ifma_addr->sa_family != AF_INET || 1684 ifma->ifma_protospec == NULL) 1685 continue; 1686 inm = (struct in_multi *)ifma->ifma_protospec; 1687 switch (igi->igi_version) { 1688 case IGMP_VERSION_1: 1689 case IGMP_VERSION_2: 1690 igmp_v1v2_process_group_timer(inm, 1691 igi->igi_version); 1692 break; 1693 case IGMP_VERSION_3: 1694 igmp_v3_process_group_timers(igi, &qrq, 1695 &scq, inm, uri_fasthz); 1696 break; 1697 } 1698 } 1699 IF_ADDR_UNLOCK(ifp); 1700 1701 if (igi->igi_version == IGMP_VERSION_3) { 1702 struct in_multi *tinm; 1703 1704 igmp_dispatch_queue(&qrq, 0, loop); 1705 igmp_dispatch_queue(&scq, 0, loop); 1706 1707 /* 1708 * Free the in_multi reference(s) for this 1709 * IGMP lifecycle. 1710 */ 1711 SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, 1712 inm_nrele, tinm) { 1713 SLIST_REMOVE_HEAD(&igi->igi_relinmhead, 1714 inm_nrele); 1715 inm_release_locked(inm); 1716 } 1717 } 1718 } 1719 1720out_locked: 1721 IGMP_UNLOCK(); 1722 IN_MULTI_UNLOCK(); 1723} 1724 1725/* 1726 * Update host report group timer for IGMPv1/v2. 1727 * Will update the global pending timer flags. 1728 */ 1729static void 1730igmp_v1v2_process_group_timer(struct in_multi *inm, const int version) 1731{ 1732 INIT_VNET_INET(curvnet); 1733 int report_timer_expired; 1734 1735 IN_MULTI_LOCK_ASSERT(); 1736 IGMP_LOCK_ASSERT(); 1737 1738 if (inm->inm_timer == 0) { 1739 report_timer_expired = 0; 1740 } else if (--inm->inm_timer == 0) { 1741 report_timer_expired = 1; 1742 } else { 1743 V_current_state_timers_running = 1; 1744 return; 1745 } 1746 1747 switch (inm->inm_state) { 1748 case IGMP_NOT_MEMBER: 1749 case IGMP_SILENT_MEMBER: 1750 case IGMP_IDLE_MEMBER: 1751 case IGMP_LAZY_MEMBER: 1752 case IGMP_SLEEPING_MEMBER: 1753 case IGMP_AWAKENING_MEMBER: 1754 break; 1755 case IGMP_REPORTING_MEMBER: 1756 if (report_timer_expired) { 1757 inm->inm_state = IGMP_IDLE_MEMBER; 1758 (void)igmp_v1v2_queue_report(inm, 1759 (version == IGMP_VERSION_2) ? 1760 IGMP_v2_HOST_MEMBERSHIP_REPORT : 1761 IGMP_v1_HOST_MEMBERSHIP_REPORT); 1762 } 1763 break; 1764 case IGMP_G_QUERY_PENDING_MEMBER: 1765 case IGMP_SG_QUERY_PENDING_MEMBER: 1766 case IGMP_LEAVING_MEMBER: 1767 break; 1768 } 1769} 1770 1771/* 1772 * Update a group's timers for IGMPv3. 1773 * Will update the global pending timer flags. 1774 * Note: Unlocked read from igi. 1775 */ 1776static void 1777igmp_v3_process_group_timers(struct igmp_ifinfo *igi, 1778 struct ifqueue *qrq, struct ifqueue *scq, 1779 struct in_multi *inm, const int uri_fasthz) 1780{ 1781 INIT_VNET_INET(curvnet); 1782 int query_response_timer_expired; 1783 int state_change_retransmit_timer_expired; 1784 1785 IN_MULTI_LOCK_ASSERT(); 1786 IGMP_LOCK_ASSERT(); 1787 1788 query_response_timer_expired = 0; 1789 state_change_retransmit_timer_expired = 0; 1790 1791 /* 1792 * During a transition from v1/v2 compatibility mode back to v3, 1793 * a group record in REPORTING state may still have its group 1794 * timer active. This is a no-op in this function; it is easier 1795 * to deal with it here than to complicate the slow-timeout path. 1796 */ 1797 if (inm->inm_timer == 0) { 1798 query_response_timer_expired = 0; 1799 } else if (--inm->inm_timer == 0) { 1800 query_response_timer_expired = 1; 1801 } else { 1802 V_current_state_timers_running = 1; 1803 } 1804 1805 if (inm->inm_sctimer == 0) { 1806 state_change_retransmit_timer_expired = 0; 1807 } else if (--inm->inm_sctimer == 0) { 1808 state_change_retransmit_timer_expired = 1; 1809 } else { 1810 V_state_change_timers_running = 1; 1811 } 1812 1813 /* We are in fasttimo, so be quick about it. */ 1814 if (!state_change_retransmit_timer_expired && 1815 !query_response_timer_expired) 1816 return; 1817 1818 switch (inm->inm_state) { 1819 case IGMP_NOT_MEMBER: 1820 case IGMP_SILENT_MEMBER: 1821 case IGMP_SLEEPING_MEMBER: 1822 case IGMP_LAZY_MEMBER: 1823 case IGMP_AWAKENING_MEMBER: 1824 case IGMP_IDLE_MEMBER: 1825 break; 1826 case IGMP_G_QUERY_PENDING_MEMBER: 1827 case IGMP_SG_QUERY_PENDING_MEMBER: 1828 /* 1829 * Respond to a previously pending Group-Specific 1830 * or Group-and-Source-Specific query by enqueueing 1831 * the appropriate Current-State report for 1832 * immediate transmission. 1833 */ 1834 if (query_response_timer_expired) { 1835 int retval; 1836 1837 retval = igmp_v3_enqueue_group_record(qrq, inm, 0, 1, 1838 (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)); 1839 CTR2(KTR_IGMPV3, "%s: enqueue record = %d", 1840 __func__, retval); 1841 inm->inm_state = IGMP_REPORTING_MEMBER; 1842 /* XXX Clear recorded sources for next time. */ 1843 inm_clear_recorded(inm); 1844 } 1845 /* FALLTHROUGH */ 1846 case IGMP_REPORTING_MEMBER: 1847 case IGMP_LEAVING_MEMBER: 1848 if (state_change_retransmit_timer_expired) { 1849 /* 1850 * State-change retransmission timer fired. 1851 * If there are any further pending retransmissions, 1852 * set the global pending state-change flag, and 1853 * reset the timer. 1854 */ 1855 if (--inm->inm_scrv > 0) { 1856 inm->inm_sctimer = uri_fasthz; 1857 V_state_change_timers_running = 1; 1858 } 1859 /* 1860 * Retransmit the previously computed state-change 1861 * report. If there are no further pending 1862 * retransmissions, the mbuf queue will be consumed. 1863 * Update T0 state to T1 as we have now sent 1864 * a state-change. 1865 */ 1866 (void)igmp_v3_merge_state_changes(inm, scq); 1867 1868 inm_commit(inm); 1869 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__, 1870 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname); 1871 1872 /* 1873 * If we are leaving the group for good, make sure 1874 * we release IGMP's reference to it. 1875 * This release must be deferred using a SLIST, 1876 * as we are called from a loop which traverses 1877 * the in_ifmultiaddr TAILQ. 1878 */ 1879 if (inm->inm_state == IGMP_LEAVING_MEMBER && 1880 inm->inm_scrv == 0) { 1881 inm->inm_state = IGMP_NOT_MEMBER; 1882 SLIST_INSERT_HEAD(&igi->igi_relinmhead, 1883 inm, inm_nrele); 1884 } 1885 } 1886 break; 1887 } 1888} 1889 1890 1891/* 1892 * Suppress a group's pending response to a group or source/group query. 1893 * 1894 * Do NOT suppress state changes. This leads to IGMPv3 inconsistency. 1895 * Do NOT update ST1/ST0 as this operation merely suppresses 1896 * the currently pending group record. 1897 * Do NOT suppress the response to a general query. It is possible but 1898 * it would require adding another state or flag. 1899 */ 1900static void 1901igmp_v3_suppress_group_record(struct in_multi *inm) 1902{ 1903 1904 IN_MULTI_LOCK_ASSERT(); 1905 1906 KASSERT(inm->inm_igi->igi_version == IGMP_VERSION_3, 1907 ("%s: not IGMPv3 mode on link", __func__)); 1908 1909 if (inm->inm_state != IGMP_G_QUERY_PENDING_MEMBER || 1910 inm->inm_state != IGMP_SG_QUERY_PENDING_MEMBER) 1911 return; 1912 1913 if (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) 1914 inm_clear_recorded(inm); 1915 1916 inm->inm_timer = 0; 1917 inm->inm_state = IGMP_REPORTING_MEMBER; 1918} 1919 1920/* 1921 * Switch to a different IGMP version on the given interface, 1922 * as per Section 7.2.1. 1923 */ 1924static void 1925igmp_set_version(struct igmp_ifinfo *igi, const int version) 1926{ 1927 1928 IGMP_LOCK_ASSERT(); 1929 1930 CTR4(KTR_IGMPV3, "%s: switching to v%d on ifp %p(%s)", __func__, 1931 version, igi->igi_ifp, igi->igi_ifp->if_xname); 1932 1933 if (version == IGMP_VERSION_1 || version == IGMP_VERSION_2) { 1934 int old_version_timer; 1935 /* 1936 * Compute the "Older Version Querier Present" timer as per 1937 * Section 8.12. 1938 */ 1939 old_version_timer = igi->igi_rv * igi->igi_qi + igi->igi_qri; 1940 old_version_timer *= PR_SLOWHZ; 1941 1942 if (version == IGMP_VERSION_1) { 1943 igi->igi_v1_timer = old_version_timer; 1944 igi->igi_v2_timer = 0; 1945 } else if (version == IGMP_VERSION_2) { 1946 igi->igi_v1_timer = 0; 1947 igi->igi_v2_timer = old_version_timer; 1948 } 1949 } 1950 1951 if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) { 1952 if (igi->igi_version != IGMP_VERSION_2) { 1953 igi->igi_version = IGMP_VERSION_2; 1954 igmp_v3_cancel_link_timers(igi); 1955 } 1956 } else if (igi->igi_v1_timer > 0) { 1957 if (igi->igi_version != IGMP_VERSION_1) { 1958 igi->igi_version = IGMP_VERSION_1; 1959 igmp_v3_cancel_link_timers(igi); 1960 } 1961 } 1962} 1963 1964/* 1965 * Cancel pending IGMPv3 timers for the given link and all groups 1966 * joined on it; state-change, general-query, and group-query timers. 1967 */ 1968static void 1969igmp_v3_cancel_link_timers(struct igmp_ifinfo *igi) 1970{ 1971 INIT_VNET_INET(curvnet); 1972 struct ifmultiaddr *ifma; 1973 struct ifnet *ifp; 1974 struct in_multi *inm; 1975 1976 CTR3(KTR_IGMPV3, "%s: cancel v3 timers on ifp %p(%s)", __func__, 1977 igi->igi_ifp, igi->igi_ifp->if_xname); 1978 1979 IN_MULTI_LOCK_ASSERT(); 1980 IGMP_LOCK_ASSERT(); 1981 1982 /* 1983 * Fast-track this potentially expensive operation 1984 * by checking all the global 'timer pending' flags. 1985 */ 1986 if (!V_interface_timers_running && 1987 !V_state_change_timers_running && 1988 !V_current_state_timers_running) 1989 return; 1990 1991 igi->igi_v3_timer = 0; 1992 1993 ifp = igi->igi_ifp; 1994 1995 IF_ADDR_LOCK(ifp); 1996 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1997 if (ifma->ifma_addr->sa_family != AF_INET) 1998 continue; 1999 inm = (struct in_multi *)ifma->ifma_protospec; 2000 switch (inm->inm_state) { 2001 case IGMP_NOT_MEMBER: 2002 case IGMP_SILENT_MEMBER: 2003 case IGMP_IDLE_MEMBER: 2004 case IGMP_LAZY_MEMBER: 2005 case IGMP_SLEEPING_MEMBER: 2006 case IGMP_AWAKENING_MEMBER: 2007 break; 2008 case IGMP_LEAVING_MEMBER: 2009 /* 2010 * If we are leaving the group and switching 2011 * IGMP version, we need to release the final 2012 * reference held for issuing the INCLUDE {}. 2013 * 2014 * SMPNG: Must drop and re-acquire IF_ADDR_LOCK 2015 * around inm_release_locked(), as it is not 2016 * a recursive mutex. 2017 */ 2018 IF_ADDR_UNLOCK(ifp); 2019 inm_release_locked(inm); 2020 IF_ADDR_LOCK(ifp); 2021 /* FALLTHROUGH */ 2022 case IGMP_G_QUERY_PENDING_MEMBER: 2023 case IGMP_SG_QUERY_PENDING_MEMBER: 2024 inm_clear_recorded(inm); 2025 /* FALLTHROUGH */ 2026 case IGMP_REPORTING_MEMBER: 2027 inm->inm_sctimer = 0; 2028 inm->inm_timer = 0; 2029 inm->inm_state = IGMP_REPORTING_MEMBER; 2030 /* 2031 * Free any pending IGMPv3 state-change records. 2032 */ 2033 _IF_DRAIN(&inm->inm_scq); 2034 break; 2035 } 2036 } 2037 IF_ADDR_UNLOCK(ifp); 2038} 2039 2040/* 2041 * Update the Older Version Querier Present timers for a link. 2042 * See Section 7.2.1 of RFC 3376. 2043 */ 2044static void 2045igmp_v1v2_process_querier_timers(struct igmp_ifinfo *igi) 2046{ 2047 INIT_VNET_INET(curvnet); 2048 2049 IGMP_LOCK_ASSERT(); 2050 2051 if (igi->igi_v1_timer == 0 && igi->igi_v2_timer == 0) { 2052 /* 2053 * IGMPv1 and IGMPv2 Querier Present timers expired. 2054 * 2055 * Revert to IGMPv3. 2056 */ 2057 if (igi->igi_version != IGMP_VERSION_3) { 2058 CTR5(KTR_IGMPV3, 2059 "%s: transition from v%d -> v%d on %p(%s)", 2060 __func__, igi->igi_version, IGMP_VERSION_3, 2061 igi->igi_ifp, igi->igi_ifp->if_xname); 2062 igi->igi_version = IGMP_VERSION_3; 2063 } 2064 } else if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) { 2065 /* 2066 * IGMPv1 Querier Present timer expired, 2067 * IGMPv2 Querier Present timer running. 2068 * If IGMPv2 was disabled since last timeout, 2069 * revert to IGMPv3. 2070 * If IGMPv2 is enabled, revert to IGMPv2. 2071 */ 2072 if (!V_igmp_v2enable) { 2073 CTR5(KTR_IGMPV3, 2074 "%s: transition from v%d -> v%d on %p(%s)", 2075 __func__, igi->igi_version, IGMP_VERSION_3, 2076 igi->igi_ifp, igi->igi_ifp->if_xname); 2077 igi->igi_v2_timer = 0; 2078 igi->igi_version = IGMP_VERSION_3; 2079 } else { 2080 --igi->igi_v2_timer; 2081 if (igi->igi_version != IGMP_VERSION_2) { 2082 CTR5(KTR_IGMPV3, 2083 "%s: transition from v%d -> v%d on %p(%s)", 2084 __func__, igi->igi_version, IGMP_VERSION_2, 2085 igi->igi_ifp, igi->igi_ifp->if_xname); 2086 igi->igi_version = IGMP_VERSION_2; 2087 } 2088 } 2089 } else if (igi->igi_v1_timer > 0) { 2090 /* 2091 * IGMPv1 Querier Present timer running. 2092 * Stop IGMPv2 timer if running. 2093 * 2094 * If IGMPv1 was disabled since last timeout, 2095 * revert to IGMPv3. 2096 * If IGMPv1 is enabled, reset IGMPv2 timer if running. 2097 */ 2098 if (!V_igmp_v1enable) { 2099 CTR5(KTR_IGMPV3, 2100 "%s: transition from v%d -> v%d on %p(%s)", 2101 __func__, igi->igi_version, IGMP_VERSION_3, 2102 igi->igi_ifp, igi->igi_ifp->if_xname); 2103 igi->igi_v1_timer = 0; 2104 igi->igi_version = IGMP_VERSION_3; 2105 } else { 2106 --igi->igi_v1_timer; 2107 } 2108 if (igi->igi_v2_timer > 0) { 2109 CTR3(KTR_IGMPV3, 2110 "%s: cancel v2 timer on %p(%s)", 2111 __func__, igi->igi_ifp, igi->igi_ifp->if_xname); 2112 igi->igi_v2_timer = 0; 2113 } 2114 } 2115} 2116 2117/* 2118 * Global slowtimo handler. 2119 * VIMAGE: Timeout handlers are expected to service all vimages. 2120 */ 2121void 2122igmp_slowtimo(void) 2123{ 2124 VNET_ITERATOR_DECL(vnet_iter); 2125 2126 VNET_LIST_RLOCK(); 2127 VNET_FOREACH(vnet_iter) { 2128 CURVNET_SET(vnet_iter); 2129 igmp_slowtimo_vnet(); 2130 CURVNET_RESTORE(); 2131 } 2132 VNET_LIST_RUNLOCK(); 2133} 2134 2135/* 2136 * Per-vnet slowtimo handler. 2137 */ 2138static void 2139igmp_slowtimo_vnet(void) 2140{ 2141 INIT_VNET_INET(curvnet); 2142 struct igmp_ifinfo *igi; 2143 2144 IGMP_LOCK(); 2145 2146 LIST_FOREACH(igi, &V_igi_head, igi_link) { 2147 igmp_v1v2_process_querier_timers(igi); 2148 } 2149 2150 IGMP_UNLOCK(); 2151} 2152 2153/* 2154 * Dispatch an IGMPv1/v2 host report or leave message. 2155 * These are always small enough to fit inside a single mbuf. 2156 */ 2157static int 2158igmp_v1v2_queue_report(struct in_multi *inm, const int type) 2159{ 2160 struct ifnet *ifp; 2161 struct igmp *igmp; 2162 struct ip *ip; 2163 struct mbuf *m; 2164 2165 IN_MULTI_LOCK_ASSERT(); 2166 IGMP_LOCK_ASSERT(); 2167 2168 ifp = inm->inm_ifp; 2169 2170 MGETHDR(m, M_DONTWAIT, MT_DATA); 2171 if (m == NULL) 2172 return (ENOMEM); 2173 MH_ALIGN(m, sizeof(struct ip) + sizeof(struct igmp)); 2174 2175 m->m_pkthdr.len = sizeof(struct ip) + sizeof(struct igmp); 2176 2177 m->m_data += sizeof(struct ip); 2178 m->m_len = sizeof(struct igmp); 2179 2180 igmp = mtod(m, struct igmp *); 2181 igmp->igmp_type = type; 2182 igmp->igmp_code = 0; 2183 igmp->igmp_group = inm->inm_addr; 2184 igmp->igmp_cksum = 0; 2185 igmp->igmp_cksum = in_cksum(m, sizeof(struct igmp)); 2186 2187 m->m_data -= sizeof(struct ip); 2188 m->m_len += sizeof(struct ip); 2189 2190 ip = mtod(m, struct ip *); 2191 ip->ip_tos = 0; 2192 ip->ip_len = sizeof(struct ip) + sizeof(struct igmp); 2193 ip->ip_off = 0; 2194 ip->ip_p = IPPROTO_IGMP; 2195 ip->ip_src.s_addr = INADDR_ANY; 2196 2197 if (type == IGMP_HOST_LEAVE_MESSAGE) 2198 ip->ip_dst.s_addr = htonl(INADDR_ALLRTRS_GROUP); 2199 else 2200 ip->ip_dst = inm->inm_addr; 2201 2202 igmp_save_context(m, ifp); 2203 2204 m->m_flags |= M_IGMPV2; 2205 if (inm->inm_igi->igi_flags & IGIF_LOOPBACK) 2206 m->m_flags |= M_IGMP_LOOP; 2207 2208 CTR2(KTR_IGMPV3, "%s: netisr_dispatch(NETISR_IGMP, %p)", __func__, m); 2209 netisr_dispatch(NETISR_IGMP, m); 2210 2211 return (0); 2212} 2213 2214/* 2215 * Process a state change from the upper layer for the given IPv4 group. 2216 * 2217 * Each socket holds a reference on the in_multi in its own ip_moptions. 2218 * The socket layer will have made the necessary updates to.the group 2219 * state, it is now up to IGMP to issue a state change report if there 2220 * has been any change between T0 (when the last state-change was issued) 2221 * and T1 (now). 2222 * 2223 * We use the IGMPv3 state machine at group level. The IGMP module 2224 * however makes the decision as to which IGMP protocol version to speak. 2225 * A state change *from* INCLUDE {} always means an initial join. 2226 * A state change *to* INCLUDE {} always means a final leave. 2227 * 2228 * FUTURE: If IGIF_V3LITE is enabled for this interface, then we can 2229 * save ourselves a bunch of work; any exclusive mode groups need not 2230 * compute source filter lists. 2231 * 2232 * VIMAGE: curvnet should have been set by caller, as this routine 2233 * is called from the socket option handlers. 2234 */ 2235int 2236igmp_change_state(struct in_multi *inm) 2237{ 2238 struct igmp_ifinfo *igi; 2239 struct ifnet *ifp; 2240 int error; 2241 2242 IN_MULTI_LOCK_ASSERT(); 2243 2244 error = 0; 2245 2246 /* 2247 * Try to detect if the upper layer just asked us to change state 2248 * for an interface which has now gone away. 2249 */ 2250 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__)); 2251 ifp = inm->inm_ifma->ifma_ifp; 2252 if (ifp != NULL) { 2253 /* 2254 * Sanity check that netinet's notion of ifp is the 2255 * same as net's. 2256 */ 2257 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__)); 2258 } 2259 2260 IGMP_LOCK(); 2261 2262 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp; 2263 KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp)); 2264 2265 /* 2266 * If we detect a state transition to or from MCAST_UNDEFINED 2267 * for this group, then we are starting or finishing an IGMP 2268 * life cycle for this group. 2269 */ 2270 if (inm->inm_st[1].iss_fmode != inm->inm_st[0].iss_fmode) { 2271 CTR3(KTR_IGMPV3, "%s: inm transition %d -> %d", __func__, 2272 inm->inm_st[0].iss_fmode, inm->inm_st[1].iss_fmode); 2273 if (inm->inm_st[0].iss_fmode == MCAST_UNDEFINED) { 2274 CTR1(KTR_IGMPV3, "%s: initial join", __func__); 2275 error = igmp_initial_join(inm, igi); 2276 goto out_locked; 2277 } else if (inm->inm_st[1].iss_fmode == MCAST_UNDEFINED) { 2278 CTR1(KTR_IGMPV3, "%s: final leave", __func__); 2279 igmp_final_leave(inm, igi); 2280 goto out_locked; 2281 } 2282 } else { 2283 CTR1(KTR_IGMPV3, "%s: filter set change", __func__); 2284 } 2285 2286 error = igmp_handle_state_change(inm, igi); 2287 2288out_locked: 2289 IGMP_UNLOCK(); 2290 return (error); 2291} 2292 2293/* 2294 * Perform the initial join for an IGMP group. 2295 * 2296 * When joining a group: 2297 * If the group should have its IGMP traffic suppressed, do nothing. 2298 * IGMPv1 starts sending IGMPv1 host membership reports. 2299 * IGMPv2 starts sending IGMPv2 host membership reports. 2300 * IGMPv3 will schedule an IGMPv3 state-change report containing the 2301 * initial state of the membership. 2302 */ 2303static int 2304igmp_initial_join(struct in_multi *inm, struct igmp_ifinfo *igi) 2305{ 2306 INIT_VNET_INET(curvnet); 2307 struct ifnet *ifp; 2308 struct ifqueue *ifq; 2309 int error, retval, syncstates; 2310 2311 CTR4(KTR_IGMPV3, "%s: initial join %s on ifp %p(%s)", 2312 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp, 2313 inm->inm_ifp->if_xname); 2314 2315 error = 0; 2316 syncstates = 1; 2317 2318 ifp = inm->inm_ifp; 2319 2320 IN_MULTI_LOCK_ASSERT(); 2321 IGMP_LOCK_ASSERT(); 2322 2323 KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__)); 2324 2325 /* 2326 * Groups joined on loopback or marked as 'not reported', 2327 * e.g. 224.0.0.1, enter the IGMP_SILENT_MEMBER state and 2328 * are never reported in any IGMP protocol exchanges. 2329 * All other groups enter the appropriate IGMP state machine 2330 * for the version in use on this link. 2331 * A link marked as IGIF_SILENT causes IGMP to be completely 2332 * disabled for the link. 2333 */ 2334 if ((ifp->if_flags & IFF_LOOPBACK) || 2335 (igi->igi_flags & IGIF_SILENT) || 2336 !igmp_isgroupreported(inm->inm_addr)) { 2337 CTR1(KTR_IGMPV3, 2338"%s: not kicking state machine for silent group", __func__); 2339 inm->inm_state = IGMP_SILENT_MEMBER; 2340 inm->inm_timer = 0; 2341 } else { 2342 /* 2343 * Deal with overlapping in_multi lifecycle. 2344 * If this group was LEAVING, then make sure 2345 * we drop the reference we picked up to keep the 2346 * group around for the final INCLUDE {} enqueue. 2347 */ 2348 if (igi->igi_version == IGMP_VERSION_3 && 2349 inm->inm_state == IGMP_LEAVING_MEMBER) 2350 inm_release_locked(inm); 2351 2352 inm->inm_state = IGMP_REPORTING_MEMBER; 2353 2354 switch (igi->igi_version) { 2355 case IGMP_VERSION_1: 2356 case IGMP_VERSION_2: 2357 inm->inm_state = IGMP_IDLE_MEMBER; 2358 error = igmp_v1v2_queue_report(inm, 2359 (igi->igi_version == IGMP_VERSION_2) ? 2360 IGMP_v2_HOST_MEMBERSHIP_REPORT : 2361 IGMP_v1_HOST_MEMBERSHIP_REPORT); 2362 if (error == 0) { 2363 inm->inm_timer = IGMP_RANDOM_DELAY( 2364 IGMP_V1V2_MAX_RI * PR_FASTHZ); 2365 V_current_state_timers_running = 1; 2366 } 2367 break; 2368 2369 case IGMP_VERSION_3: 2370 /* 2371 * Defer update of T0 to T1, until the first copy 2372 * of the state change has been transmitted. 2373 */ 2374 syncstates = 0; 2375 2376 /* 2377 * Immediately enqueue a State-Change Report for 2378 * this interface, freeing any previous reports. 2379 * Don't kick the timers if there is nothing to do, 2380 * or if an error occurred. 2381 */ 2382 ifq = &inm->inm_scq; 2383 _IF_DRAIN(ifq); 2384 retval = igmp_v3_enqueue_group_record(ifq, inm, 1, 2385 0, 0); 2386 CTR2(KTR_IGMPV3, "%s: enqueue record = %d", 2387 __func__, retval); 2388 if (retval <= 0) { 2389 error = retval * -1; 2390 break; 2391 } 2392 2393 /* 2394 * Schedule transmission of pending state-change 2395 * report up to RV times for this link. The timer 2396 * will fire at the next igmp_fasttimo (~200ms), 2397 * giving us an opportunity to merge the reports. 2398 */ 2399 if (igi->igi_flags & IGIF_LOOPBACK) { 2400 inm->inm_scrv = 1; 2401 } else { 2402 KASSERT(igi->igi_rv > 1, 2403 ("%s: invalid robustness %d", __func__, 2404 igi->igi_rv)); 2405 inm->inm_scrv = igi->igi_rv; 2406 } 2407 inm->inm_sctimer = 1; 2408 V_state_change_timers_running = 1; 2409 2410 error = 0; 2411 break; 2412 } 2413 } 2414 2415 /* 2416 * Only update the T0 state if state change is atomic, 2417 * i.e. we don't need to wait for a timer to fire before we 2418 * can consider the state change to have been communicated. 2419 */ 2420 if (syncstates) { 2421 inm_commit(inm); 2422 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__, 2423 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname); 2424 } 2425 2426 return (error); 2427} 2428 2429/* 2430 * Issue an intermediate state change during the IGMP life-cycle. 2431 */ 2432static int 2433igmp_handle_state_change(struct in_multi *inm, struct igmp_ifinfo *igi) 2434{ 2435 INIT_VNET_INET(curvnet); 2436 struct ifnet *ifp; 2437 int retval; 2438 2439 CTR4(KTR_IGMPV3, "%s: state change for %s on ifp %p(%s)", 2440 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp, 2441 inm->inm_ifp->if_xname); 2442 2443 ifp = inm->inm_ifp; 2444 2445 IN_MULTI_LOCK_ASSERT(); 2446 IGMP_LOCK_ASSERT(); 2447 2448 KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__)); 2449 2450 if ((ifp->if_flags & IFF_LOOPBACK) || 2451 (igi->igi_flags & IGIF_SILENT) || 2452 !igmp_isgroupreported(inm->inm_addr) || 2453 (igi->igi_version != IGMP_VERSION_3)) { 2454 if (!igmp_isgroupreported(inm->inm_addr)) { 2455 CTR1(KTR_IGMPV3, 2456"%s: not kicking state machine for silent group", __func__); 2457 } 2458 CTR1(KTR_IGMPV3, "%s: nothing to do", __func__); 2459 inm_commit(inm); 2460 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__, 2461 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname); 2462 return (0); 2463 } 2464 2465 _IF_DRAIN(&inm->inm_scq); 2466 2467 retval = igmp_v3_enqueue_group_record(&inm->inm_scq, inm, 1, 0, 0); 2468 CTR2(KTR_IGMPV3, "%s: enqueue record = %d", __func__, retval); 2469 if (retval <= 0) 2470 return (-retval); 2471 2472 /* 2473 * If record(s) were enqueued, start the state-change 2474 * report timer for this group. 2475 */ 2476 inm->inm_scrv = ((igi->igi_flags & IGIF_LOOPBACK) ? 1 : igi->igi_rv); 2477 inm->inm_sctimer = 1; 2478 V_state_change_timers_running = 1; 2479 2480 return (0); 2481} 2482 2483/* 2484 * Perform the final leave for an IGMP group. 2485 * 2486 * When leaving a group: 2487 * IGMPv1 does nothing. 2488 * IGMPv2 sends a host leave message, if and only if we are the reporter. 2489 * IGMPv3 enqueues a state-change report containing a transition 2490 * to INCLUDE {} for immediate transmission. 2491 */ 2492static void 2493igmp_final_leave(struct in_multi *inm, struct igmp_ifinfo *igi) 2494{ 2495 INIT_VNET_INET(curvnet); 2496 int syncstates; 2497 2498 syncstates = 1; 2499 2500 CTR4(KTR_IGMPV3, "%s: final leave %s on ifp %p(%s)", 2501 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp, 2502 inm->inm_ifp->if_xname); 2503 2504 IN_MULTI_LOCK_ASSERT(); 2505 IGMP_LOCK_ASSERT(); 2506 2507 switch (inm->inm_state) { 2508 case IGMP_NOT_MEMBER: 2509 case IGMP_SILENT_MEMBER: 2510 case IGMP_LEAVING_MEMBER: 2511 /* Already leaving or left; do nothing. */ 2512 CTR1(KTR_IGMPV3, 2513"%s: not kicking state machine for silent group", __func__); 2514 break; 2515 case IGMP_REPORTING_MEMBER: 2516 case IGMP_IDLE_MEMBER: 2517 case IGMP_G_QUERY_PENDING_MEMBER: 2518 case IGMP_SG_QUERY_PENDING_MEMBER: 2519 if (igi->igi_version == IGMP_VERSION_2) { 2520#ifdef INVARIANTS 2521 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER || 2522 inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) 2523 panic("%s: IGMPv3 state reached, not IGMPv3 mode", 2524 __func__); 2525#endif 2526 igmp_v1v2_queue_report(inm, IGMP_HOST_LEAVE_MESSAGE); 2527 inm->inm_state = IGMP_NOT_MEMBER; 2528 } else if (igi->igi_version == IGMP_VERSION_3) { 2529 /* 2530 * Stop group timer and all pending reports. 2531 * Immediately enqueue a state-change report 2532 * TO_IN {} to be sent on the next fast timeout, 2533 * giving us an opportunity to merge reports. 2534 */ 2535 _IF_DRAIN(&inm->inm_scq); 2536 inm->inm_timer = 0; 2537 if (igi->igi_flags & IGIF_LOOPBACK) { 2538 inm->inm_scrv = 1; 2539 } else { 2540 inm->inm_scrv = igi->igi_rv; 2541 } 2542 CTR4(KTR_IGMPV3, "%s: Leaving %s/%s with %d " 2543 "pending retransmissions.", __func__, 2544 inet_ntoa(inm->inm_addr), 2545 inm->inm_ifp->if_xname, inm->inm_scrv); 2546 if (inm->inm_scrv == 0) { 2547 inm->inm_state = IGMP_NOT_MEMBER; 2548 inm->inm_sctimer = 0; 2549 } else { 2550 int retval; 2551 2552 inm_acquire_locked(inm); 2553 2554 retval = igmp_v3_enqueue_group_record( 2555 &inm->inm_scq, inm, 1, 0, 0); 2556 KASSERT(retval != 0, 2557 ("%s: enqueue record = %d", __func__, 2558 retval)); 2559 2560 inm->inm_state = IGMP_LEAVING_MEMBER; 2561 inm->inm_sctimer = 1; 2562 V_state_change_timers_running = 1; 2563 syncstates = 0; 2564 } 2565 break; 2566 } 2567 break; 2568 case IGMP_LAZY_MEMBER: 2569 case IGMP_SLEEPING_MEMBER: 2570 case IGMP_AWAKENING_MEMBER: 2571 /* Our reports are suppressed; do nothing. */ 2572 break; 2573 } 2574 2575 if (syncstates) { 2576 inm_commit(inm); 2577 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__, 2578 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname); 2579 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED; 2580 CTR3(KTR_IGMPV3, "%s: T1 now MCAST_UNDEFINED for %s/%s", 2581 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname); 2582 } 2583} 2584 2585/* 2586 * Enqueue an IGMPv3 group record to the given output queue. 2587 * 2588 * XXX This function could do with having the allocation code 2589 * split out, and the multiple-tree-walks coalesced into a single 2590 * routine as has been done in igmp_v3_enqueue_filter_change(). 2591 * 2592 * If is_state_change is zero, a current-state record is appended. 2593 * If is_state_change is non-zero, a state-change report is appended. 2594 * 2595 * If is_group_query is non-zero, an mbuf packet chain is allocated. 2596 * If is_group_query is zero, and if there is a packet with free space 2597 * at the tail of the queue, it will be appended to providing there 2598 * is enough free space. 2599 * Otherwise a new mbuf packet chain is allocated. 2600 * 2601 * If is_source_query is non-zero, each source is checked to see if 2602 * it was recorded for a Group-Source query, and will be omitted if 2603 * it is not both in-mode and recorded. 2604 * 2605 * The function will attempt to allocate leading space in the packet 2606 * for the IP/IGMP header to be prepended without fragmenting the chain. 2607 * 2608 * If successful the size of all data appended to the queue is returned, 2609 * otherwise an error code less than zero is returned, or zero if 2610 * no record(s) were appended. 2611 */ 2612static int 2613igmp_v3_enqueue_group_record(struct ifqueue *ifq, struct in_multi *inm, 2614 const int is_state_change, const int is_group_query, 2615 const int is_source_query) 2616{ 2617 struct igmp_grouprec ig; 2618 struct igmp_grouprec *pig; 2619 struct ifnet *ifp; 2620 struct ip_msource *ims, *nims; 2621 struct mbuf *m0, *m, *md; 2622 int error, is_filter_list_change; 2623 int minrec0len, m0srcs, msrcs, nbytes, off; 2624 int record_has_sources; 2625 int now; 2626 int type; 2627 in_addr_t naddr; 2628 uint8_t mode; 2629 2630 IN_MULTI_LOCK_ASSERT(); 2631 2632 error = 0; 2633 ifp = inm->inm_ifp; 2634 is_filter_list_change = 0; 2635 m = NULL; 2636 m0 = NULL; 2637 m0srcs = 0; 2638 msrcs = 0; 2639 nbytes = 0; 2640 nims = NULL; 2641 record_has_sources = 1; 2642 pig = NULL; 2643 type = IGMP_DO_NOTHING; 2644 mode = inm->inm_st[1].iss_fmode; 2645 2646 /* 2647 * If we did not transition out of ASM mode during t0->t1, 2648 * and there are no source nodes to process, we can skip 2649 * the generation of source records. 2650 */ 2651 if (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0 && 2652 inm->inm_nsrc == 0) 2653 record_has_sources = 0; 2654 2655 if (is_state_change) { 2656 /* 2657 * Queue a state change record. 2658 * If the mode did not change, and there are non-ASM 2659 * listeners or source filters present, 2660 * we potentially need to issue two records for the group. 2661 * If we are transitioning to MCAST_UNDEFINED, we need 2662 * not send any sources. 2663 * If there are ASM listeners, and there was no filter 2664 * mode transition of any kind, do nothing. 2665 */ 2666 if (mode != inm->inm_st[0].iss_fmode) { 2667 if (mode == MCAST_EXCLUDE) { 2668 CTR1(KTR_IGMPV3, "%s: change to EXCLUDE", 2669 __func__); 2670 type = IGMP_CHANGE_TO_EXCLUDE_MODE; 2671 } else { 2672 CTR1(KTR_IGMPV3, "%s: change to INCLUDE", 2673 __func__); 2674 type = IGMP_CHANGE_TO_INCLUDE_MODE; 2675 if (mode == MCAST_UNDEFINED) 2676 record_has_sources = 0; 2677 } 2678 } else { 2679 if (record_has_sources) { 2680 is_filter_list_change = 1; 2681 } else { 2682 type = IGMP_DO_NOTHING; 2683 } 2684 } 2685 } else { 2686 /* 2687 * Queue a current state record. 2688 */ 2689 if (mode == MCAST_EXCLUDE) { 2690 type = IGMP_MODE_IS_EXCLUDE; 2691 } else if (mode == MCAST_INCLUDE) { 2692 type = IGMP_MODE_IS_INCLUDE; 2693 KASSERT(inm->inm_st[1].iss_asm == 0, 2694 ("%s: inm %p is INCLUDE but ASM count is %d", 2695 __func__, inm, inm->inm_st[1].iss_asm)); 2696 } 2697 } 2698 2699 /* 2700 * Generate the filter list changes using a separate function. 2701 */ 2702 if (is_filter_list_change) 2703 return (igmp_v3_enqueue_filter_change(ifq, inm)); 2704 2705 if (type == IGMP_DO_NOTHING) { 2706 CTR3(KTR_IGMPV3, "%s: nothing to do for %s/%s", 2707 __func__, inet_ntoa(inm->inm_addr), 2708 inm->inm_ifp->if_xname); 2709 return (0); 2710 } 2711 2712 /* 2713 * If any sources are present, we must be able to fit at least 2714 * one in the trailing space of the tail packet's mbuf, 2715 * ideally more. 2716 */ 2717 minrec0len = sizeof(struct igmp_grouprec); 2718 if (record_has_sources) 2719 minrec0len += sizeof(in_addr_t); 2720 2721 CTR4(KTR_IGMPV3, "%s: queueing %s for %s/%s", __func__, 2722 igmp_rec_type_to_str(type), inet_ntoa(inm->inm_addr), 2723 inm->inm_ifp->if_xname); 2724 2725 /* 2726 * Check if we have a packet in the tail of the queue for this 2727 * group into which the first group record for this group will fit. 2728 * Otherwise allocate a new packet. 2729 * Always allocate leading space for IP+RA_OPT+IGMP+REPORT. 2730 * Note: Group records for G/GSR query responses MUST be sent 2731 * in their own packet. 2732 */ 2733 m0 = ifq->ifq_tail; 2734 if (!is_group_query && 2735 m0 != NULL && 2736 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= IGMP_V3_REPORT_MAXRECS) && 2737 (m0->m_pkthdr.len + minrec0len) < 2738 (ifp->if_mtu - IGMP_LEADINGSPACE)) { 2739 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len - 2740 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t); 2741 m = m0; 2742 CTR1(KTR_IGMPV3, "%s: use existing packet", __func__); 2743 } else { 2744 if (_IF_QFULL(ifq)) { 2745 CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__); 2746 return (-ENOMEM); 2747 } 2748 m = NULL; 2749 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE - 2750 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t); 2751 if (!is_state_change && !is_group_query) { 2752 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 2753 if (m) 2754 m->m_data += IGMP_LEADINGSPACE; 2755 } 2756 if (m == NULL) { 2757 m = m_gethdr(M_DONTWAIT, MT_DATA); 2758 if (m) 2759 MH_ALIGN(m, IGMP_LEADINGSPACE); 2760 } 2761 if (m == NULL) 2762 return (-ENOMEM); 2763 2764 igmp_save_context(m, ifp); 2765 2766 CTR1(KTR_IGMPV3, "%s: allocated first packet", __func__); 2767 } 2768 2769 /* 2770 * Append group record. 2771 * If we have sources, we don't know how many yet. 2772 */ 2773 ig.ig_type = type; 2774 ig.ig_datalen = 0; 2775 ig.ig_numsrc = 0; 2776 ig.ig_group = inm->inm_addr; 2777 if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) { 2778 if (m != m0) 2779 m_freem(m); 2780 CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__); 2781 return (-ENOMEM); 2782 } 2783 nbytes += sizeof(struct igmp_grouprec); 2784 2785 /* 2786 * Append as many sources as will fit in the first packet. 2787 * If we are appending to a new packet, the chain allocation 2788 * may potentially use clusters; use m_getptr() in this case. 2789 * If we are appending to an existing packet, we need to obtain 2790 * a pointer to the group record after m_append(), in case a new 2791 * mbuf was allocated. 2792 * Only append sources which are in-mode at t1. If we are 2793 * transitioning to MCAST_UNDEFINED state on the group, do not 2794 * include source entries. 2795 * Only report recorded sources in our filter set when responding 2796 * to a group-source query. 2797 */ 2798 if (record_has_sources) { 2799 if (m == m0) { 2800 md = m_last(m); 2801 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + 2802 md->m_len - nbytes); 2803 } else { 2804 md = m_getptr(m, 0, &off); 2805 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + 2806 off); 2807 } 2808 msrcs = 0; 2809 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, nims) { 2810 CTR2(KTR_IGMPV3, "%s: visit node %s", __func__, 2811 inet_ntoa_haddr(ims->ims_haddr)); 2812 now = ims_get_mode(inm, ims, 1); 2813 CTR2(KTR_IGMPV3, "%s: node is %d", __func__, now); 2814 if ((now != mode) || 2815 (now == mode && mode == MCAST_UNDEFINED)) { 2816 CTR1(KTR_IGMPV3, "%s: skip node", __func__); 2817 continue; 2818 } 2819 if (is_source_query && ims->ims_stp == 0) { 2820 CTR1(KTR_IGMPV3, "%s: skip unrecorded node", 2821 __func__); 2822 continue; 2823 } 2824 CTR1(KTR_IGMPV3, "%s: append node", __func__); 2825 naddr = htonl(ims->ims_haddr); 2826 if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) { 2827 if (m != m0) 2828 m_freem(m); 2829 CTR1(KTR_IGMPV3, "%s: m_append() failed.", 2830 __func__); 2831 return (-ENOMEM); 2832 } 2833 nbytes += sizeof(in_addr_t); 2834 ++msrcs; 2835 if (msrcs == m0srcs) 2836 break; 2837 } 2838 CTR2(KTR_IGMPV3, "%s: msrcs is %d this packet", __func__, 2839 msrcs); 2840 pig->ig_numsrc = htons(msrcs); 2841 nbytes += (msrcs * sizeof(in_addr_t)); 2842 } 2843 2844 if (is_source_query && msrcs == 0) { 2845 CTR1(KTR_IGMPV3, "%s: no recorded sources to report", __func__); 2846 if (m != m0) 2847 m_freem(m); 2848 return (0); 2849 } 2850 2851 /* 2852 * We are good to go with first packet. 2853 */ 2854 if (m != m0) { 2855 CTR1(KTR_IGMPV3, "%s: enqueueing first packet", __func__); 2856 m->m_pkthdr.PH_vt.vt_nrecs = 1; 2857 _IF_ENQUEUE(ifq, m); 2858 } else 2859 m->m_pkthdr.PH_vt.vt_nrecs++; 2860 2861 /* 2862 * No further work needed if no source list in packet(s). 2863 */ 2864 if (!record_has_sources) 2865 return (nbytes); 2866 2867 /* 2868 * Whilst sources remain to be announced, we need to allocate 2869 * a new packet and fill out as many sources as will fit. 2870 * Always try for a cluster first. 2871 */ 2872 while (nims != NULL) { 2873 if (_IF_QFULL(ifq)) { 2874 CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__); 2875 return (-ENOMEM); 2876 } 2877 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 2878 if (m) 2879 m->m_data += IGMP_LEADINGSPACE; 2880 if (m == NULL) { 2881 m = m_gethdr(M_DONTWAIT, MT_DATA); 2882 if (m) 2883 MH_ALIGN(m, IGMP_LEADINGSPACE); 2884 } 2885 if (m == NULL) 2886 return (-ENOMEM); 2887 igmp_save_context(m, ifp); 2888 md = m_getptr(m, 0, &off); 2889 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + off); 2890 CTR1(KTR_IGMPV3, "%s: allocated next packet", __func__); 2891 2892 if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) { 2893 if (m != m0) 2894 m_freem(m); 2895 CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__); 2896 return (-ENOMEM); 2897 } 2898 m->m_pkthdr.PH_vt.vt_nrecs = 1; 2899 nbytes += sizeof(struct igmp_grouprec); 2900 2901 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE - 2902 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t); 2903 2904 msrcs = 0; 2905 RB_FOREACH_FROM(ims, ip_msource_tree, nims) { 2906 CTR2(KTR_IGMPV3, "%s: visit node %s", __func__, 2907 inet_ntoa_haddr(ims->ims_haddr)); 2908 now = ims_get_mode(inm, ims, 1); 2909 if ((now != mode) || 2910 (now == mode && mode == MCAST_UNDEFINED)) { 2911 CTR1(KTR_IGMPV3, "%s: skip node", __func__); 2912 continue; 2913 } 2914 if (is_source_query && ims->ims_stp == 0) { 2915 CTR1(KTR_IGMPV3, "%s: skip unrecorded node", 2916 __func__); 2917 continue; 2918 } 2919 CTR1(KTR_IGMPV3, "%s: append node", __func__); 2920 naddr = htonl(ims->ims_haddr); 2921 if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) { 2922 if (m != m0) 2923 m_freem(m); 2924 CTR1(KTR_IGMPV3, "%s: m_append() failed.", 2925 __func__); 2926 return (-ENOMEM); 2927 } 2928 ++msrcs; 2929 if (msrcs == m0srcs) 2930 break; 2931 } 2932 pig->ig_numsrc = htons(msrcs); 2933 nbytes += (msrcs * sizeof(in_addr_t)); 2934 2935 CTR1(KTR_IGMPV3, "%s: enqueueing next packet", __func__); 2936 _IF_ENQUEUE(ifq, m); 2937 } 2938 2939 return (nbytes); 2940} 2941 2942/* 2943 * Type used to mark record pass completion. 2944 * We exploit the fact we can cast to this easily from the 2945 * current filter modes on each ip_msource node. 2946 */ 2947typedef enum { 2948 REC_NONE = 0x00, /* MCAST_UNDEFINED */ 2949 REC_ALLOW = 0x01, /* MCAST_INCLUDE */ 2950 REC_BLOCK = 0x02, /* MCAST_EXCLUDE */ 2951 REC_FULL = REC_ALLOW | REC_BLOCK 2952} rectype_t; 2953 2954/* 2955 * Enqueue an IGMPv3 filter list change to the given output queue. 2956 * 2957 * Source list filter state is held in an RB-tree. When the filter list 2958 * for a group is changed without changing its mode, we need to compute 2959 * the deltas between T0 and T1 for each source in the filter set, 2960 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records. 2961 * 2962 * As we may potentially queue two record types, and the entire R-B tree 2963 * needs to be walked at once, we break this out into its own function 2964 * so we can generate a tightly packed queue of packets. 2965 * 2966 * XXX This could be written to only use one tree walk, although that makes 2967 * serializing into the mbuf chains a bit harder. For now we do two walks 2968 * which makes things easier on us, and it may or may not be harder on 2969 * the L2 cache. 2970 * 2971 * If successful the size of all data appended to the queue is returned, 2972 * otherwise an error code less than zero is returned, or zero if 2973 * no record(s) were appended. 2974 */ 2975static int 2976igmp_v3_enqueue_filter_change(struct ifqueue *ifq, struct in_multi *inm) 2977{ 2978 static const int MINRECLEN = 2979 sizeof(struct igmp_grouprec) + sizeof(in_addr_t); 2980 struct ifnet *ifp; 2981 struct igmp_grouprec ig; 2982 struct igmp_grouprec *pig; 2983 struct ip_msource *ims, *nims; 2984 struct mbuf *m, *m0, *md; 2985 in_addr_t naddr; 2986 int m0srcs, nbytes, off, rsrcs, schanged; 2987 int nallow, nblock; 2988 uint8_t mode, now, then; 2989 rectype_t crt, drt, nrt; 2990 2991 IN_MULTI_LOCK_ASSERT(); 2992 2993 if (inm->inm_nsrc == 0 || 2994 (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0)) 2995 return (0); 2996 2997 ifp = inm->inm_ifp; /* interface */ 2998 mode = inm->inm_st[1].iss_fmode; /* filter mode at t1 */ 2999 crt = REC_NONE; /* current group record type */ 3000 drt = REC_NONE; /* mask of completed group record types */ 3001 nrt = REC_NONE; /* record type for current node */ 3002 m0srcs = 0; /* # source which will fit in current mbuf chain */ 3003 nbytes = 0; /* # of bytes appended to group's state-change queue */ 3004 rsrcs = 0; /* # sources encoded in current record */ 3005 schanged = 0; /* # nodes encoded in overall filter change */ 3006 nallow = 0; /* # of source entries in ALLOW_NEW */ 3007 nblock = 0; /* # of source entries in BLOCK_OLD */ 3008 nims = NULL; /* next tree node pointer */ 3009 3010 /* 3011 * For each possible filter record mode. 3012 * The first kind of source we encounter tells us which 3013 * is the first kind of record we start appending. 3014 * If a node transitioned to UNDEFINED at t1, its mode is treated 3015 * as the inverse of the group's filter mode. 3016 */ 3017 while (drt != REC_FULL) { 3018 do { 3019 m0 = ifq->ifq_tail; 3020 if (m0 != NULL && 3021 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= 3022 IGMP_V3_REPORT_MAXRECS) && 3023 (m0->m_pkthdr.len + MINRECLEN) < 3024 (ifp->if_mtu - IGMP_LEADINGSPACE)) { 3025 m = m0; 3026 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len - 3027 sizeof(struct igmp_grouprec)) / 3028 sizeof(in_addr_t); 3029 CTR1(KTR_IGMPV3, 3030 "%s: use previous packet", __func__); 3031 } else { 3032 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 3033 if (m) 3034 m->m_data += IGMP_LEADINGSPACE; 3035 if (m == NULL) { 3036 m = m_gethdr(M_DONTWAIT, MT_DATA); 3037 if (m) 3038 MH_ALIGN(m, IGMP_LEADINGSPACE); 3039 } 3040 if (m == NULL) { 3041 CTR1(KTR_IGMPV3, 3042 "%s: m_get*() failed", __func__); 3043 return (-ENOMEM); 3044 } 3045 m->m_pkthdr.PH_vt.vt_nrecs = 0; 3046 igmp_save_context(m, ifp); 3047 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE - 3048 sizeof(struct igmp_grouprec)) / 3049 sizeof(in_addr_t); 3050 CTR1(KTR_IGMPV3, 3051 "%s: allocated new packet", __func__); 3052 } 3053 /* 3054 * Append the IGMP group record header to the 3055 * current packet's data area. 3056 * Recalculate pointer to free space for next 3057 * group record, in case m_append() allocated 3058 * a new mbuf or cluster. 3059 */ 3060 memset(&ig, 0, sizeof(ig)); 3061 ig.ig_group = inm->inm_addr; 3062 if (!m_append(m, sizeof(ig), (void *)&ig)) { 3063 if (m != m0) 3064 m_freem(m); 3065 CTR1(KTR_IGMPV3, 3066 "%s: m_append() failed", __func__); 3067 return (-ENOMEM); 3068 } 3069 nbytes += sizeof(struct igmp_grouprec); 3070 if (m == m0) { 3071 md = m_last(m); 3072 pig = (struct igmp_grouprec *)(mtod(md, 3073 uint8_t *) + md->m_len - nbytes); 3074 } else { 3075 md = m_getptr(m, 0, &off); 3076 pig = (struct igmp_grouprec *)(mtod(md, 3077 uint8_t *) + off); 3078 } 3079 /* 3080 * Begin walking the tree for this record type 3081 * pass, or continue from where we left off 3082 * previously if we had to allocate a new packet. 3083 * Only report deltas in-mode at t1. 3084 * We need not report included sources as allowed 3085 * if we are in inclusive mode on the group, 3086 * however the converse is not true. 3087 */ 3088 rsrcs = 0; 3089 if (nims == NULL) 3090 nims = RB_MIN(ip_msource_tree, &inm->inm_srcs); 3091 RB_FOREACH_FROM(ims, ip_msource_tree, nims) { 3092 CTR2(KTR_IGMPV3, "%s: visit node %s", 3093 __func__, inet_ntoa_haddr(ims->ims_haddr)); 3094 now = ims_get_mode(inm, ims, 1); 3095 then = ims_get_mode(inm, ims, 0); 3096 CTR3(KTR_IGMPV3, "%s: mode: t0 %d, t1 %d", 3097 __func__, then, now); 3098 if (now == then) { 3099 CTR1(KTR_IGMPV3, 3100 "%s: skip unchanged", __func__); 3101 continue; 3102 } 3103 if (mode == MCAST_EXCLUDE && 3104 now == MCAST_INCLUDE) { 3105 CTR1(KTR_IGMPV3, 3106 "%s: skip IN src on EX group", 3107 __func__); 3108 continue; 3109 } 3110 nrt = (rectype_t)now; 3111 if (nrt == REC_NONE) 3112 nrt = (rectype_t)(~mode & REC_FULL); 3113 if (schanged++ == 0) { 3114 crt = nrt; 3115 } else if (crt != nrt) 3116 continue; 3117 naddr = htonl(ims->ims_haddr); 3118 if (!m_append(m, sizeof(in_addr_t), 3119 (void *)&naddr)) { 3120 if (m != m0) 3121 m_freem(m); 3122 CTR1(KTR_IGMPV3, 3123 "%s: m_append() failed", __func__); 3124 return (-ENOMEM); 3125 } 3126 nallow += !!(crt == REC_ALLOW); 3127 nblock += !!(crt == REC_BLOCK); 3128 if (++rsrcs == m0srcs) 3129 break; 3130 } 3131 /* 3132 * If we did not append any tree nodes on this 3133 * pass, back out of allocations. 3134 */ 3135 if (rsrcs == 0) { 3136 nbytes -= sizeof(struct igmp_grouprec); 3137 if (m != m0) { 3138 CTR1(KTR_IGMPV3, 3139 "%s: m_free(m)", __func__); 3140 m_freem(m); 3141 } else { 3142 CTR1(KTR_IGMPV3, 3143 "%s: m_adj(m, -ig)", __func__); 3144 m_adj(m, -((int)sizeof( 3145 struct igmp_grouprec))); 3146 } 3147 continue; 3148 } 3149 nbytes += (rsrcs * sizeof(in_addr_t)); 3150 if (crt == REC_ALLOW) 3151 pig->ig_type = IGMP_ALLOW_NEW_SOURCES; 3152 else if (crt == REC_BLOCK) 3153 pig->ig_type = IGMP_BLOCK_OLD_SOURCES; 3154 pig->ig_numsrc = htons(rsrcs); 3155 /* 3156 * Count the new group record, and enqueue this 3157 * packet if it wasn't already queued. 3158 */ 3159 m->m_pkthdr.PH_vt.vt_nrecs++; 3160 if (m != m0) 3161 _IF_ENQUEUE(ifq, m); 3162 } while (nims != NULL); 3163 drt |= crt; 3164 crt = (~crt & REC_FULL); 3165 } 3166 3167 CTR3(KTR_IGMPV3, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__, 3168 nallow, nblock); 3169 3170 return (nbytes); 3171} 3172 3173static int 3174igmp_v3_merge_state_changes(struct in_multi *inm, struct ifqueue *ifscq) 3175{ 3176 struct ifqueue *gq; 3177 struct mbuf *m; /* pending state-change */ 3178 struct mbuf *m0; /* copy of pending state-change */ 3179 struct mbuf *mt; /* last state-change in packet */ 3180 int docopy, domerge; 3181 u_int recslen; 3182 3183 docopy = 0; 3184 domerge = 0; 3185 recslen = 0; 3186 3187 IN_MULTI_LOCK_ASSERT(); 3188 IGMP_LOCK_ASSERT(); 3189 3190 /* 3191 * If there are further pending retransmissions, make a writable 3192 * copy of each queued state-change message before merging. 3193 */ 3194 if (inm->inm_scrv > 0) 3195 docopy = 1; 3196 3197 gq = &inm->inm_scq; 3198#ifdef KTR 3199 if (gq->ifq_head == NULL) { 3200 CTR2(KTR_IGMPV3, "%s: WARNING: queue for inm %p is empty", 3201 __func__, inm); 3202 } 3203#endif 3204 3205 m = gq->ifq_head; 3206 while (m != NULL) { 3207 /* 3208 * Only merge the report into the current packet if 3209 * there is sufficient space to do so; an IGMPv3 report 3210 * packet may only contain 65,535 group records. 3211 * Always use a simple mbuf chain concatentation to do this, 3212 * as large state changes for single groups may have 3213 * allocated clusters. 3214 */ 3215 domerge = 0; 3216 mt = ifscq->ifq_tail; 3217 if (mt != NULL) { 3218 recslen = m_length(m, NULL); 3219 3220 if ((mt->m_pkthdr.PH_vt.vt_nrecs + 3221 m->m_pkthdr.PH_vt.vt_nrecs <= 3222 IGMP_V3_REPORT_MAXRECS) && 3223 (mt->m_pkthdr.len + recslen <= 3224 (inm->inm_ifp->if_mtu - IGMP_LEADINGSPACE))) 3225 domerge = 1; 3226 } 3227 3228 if (!domerge && _IF_QFULL(gq)) { 3229 CTR2(KTR_IGMPV3, 3230 "%s: outbound queue full, skipping whole packet %p", 3231 __func__, m); 3232 mt = m->m_nextpkt; 3233 if (!docopy) 3234 m_freem(m); 3235 m = mt; 3236 continue; 3237 } 3238 3239 if (!docopy) { 3240 CTR2(KTR_IGMPV3, "%s: dequeueing %p", __func__, m); 3241 _IF_DEQUEUE(gq, m0); 3242 m = m0->m_nextpkt; 3243 } else { 3244 CTR2(KTR_IGMPV3, "%s: copying %p", __func__, m); 3245 m0 = m_dup(m, M_NOWAIT); 3246 if (m0 == NULL) 3247 return (ENOMEM); 3248 m0->m_nextpkt = NULL; 3249 m = m->m_nextpkt; 3250 } 3251 3252 if (!domerge) { 3253 CTR3(KTR_IGMPV3, "%s: queueing %p to ifscq %p)", 3254 __func__, m0, ifscq); 3255 _IF_ENQUEUE(ifscq, m0); 3256 } else { 3257 struct mbuf *mtl; /* last mbuf of packet mt */ 3258 3259 CTR3(KTR_IGMPV3, "%s: merging %p with ifscq tail %p)", 3260 __func__, m0, mt); 3261 3262 mtl = m_last(mt); 3263 m0->m_flags &= ~M_PKTHDR; 3264 mt->m_pkthdr.len += recslen; 3265 mt->m_pkthdr.PH_vt.vt_nrecs += 3266 m0->m_pkthdr.PH_vt.vt_nrecs; 3267 3268 mtl->m_next = m0; 3269 } 3270 } 3271 3272 return (0); 3273} 3274 3275/* 3276 * Respond to a pending IGMPv3 General Query. 3277 */ 3278static void 3279igmp_v3_dispatch_general_query(struct igmp_ifinfo *igi) 3280{ 3281 INIT_VNET_INET(curvnet); 3282 struct ifmultiaddr *ifma, *tifma; 3283 struct ifnet *ifp; 3284 struct in_multi *inm; 3285 int retval, loop; 3286 3287 IN_MULTI_LOCK_ASSERT(); 3288 IGMP_LOCK_ASSERT(); 3289 3290 KASSERT(igi->igi_version == IGMP_VERSION_3, 3291 ("%s: called when version %d", __func__, igi->igi_version)); 3292 3293 ifp = igi->igi_ifp; 3294 3295 IF_ADDR_LOCK(ifp); 3296 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, tifma) { 3297 if (ifma->ifma_addr->sa_family != AF_INET || 3298 ifma->ifma_protospec == NULL) 3299 continue; 3300 3301 inm = (struct in_multi *)ifma->ifma_protospec; 3302 KASSERT(ifp == inm->inm_ifp, 3303 ("%s: inconsistent ifp", __func__)); 3304 3305 switch (inm->inm_state) { 3306 case IGMP_NOT_MEMBER: 3307 case IGMP_SILENT_MEMBER: 3308 break; 3309 case IGMP_REPORTING_MEMBER: 3310 case IGMP_IDLE_MEMBER: 3311 case IGMP_LAZY_MEMBER: 3312 case IGMP_SLEEPING_MEMBER: 3313 case IGMP_AWAKENING_MEMBER: 3314 inm->inm_state = IGMP_REPORTING_MEMBER; 3315 retval = igmp_v3_enqueue_group_record(&igi->igi_gq, 3316 inm, 0, 0, 0); 3317 CTR2(KTR_IGMPV3, "%s: enqueue record = %d", 3318 __func__, retval); 3319 break; 3320 case IGMP_G_QUERY_PENDING_MEMBER: 3321 case IGMP_SG_QUERY_PENDING_MEMBER: 3322 case IGMP_LEAVING_MEMBER: 3323 break; 3324 } 3325 } 3326 IF_ADDR_UNLOCK(ifp); 3327 3328 loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0; 3329 igmp_dispatch_queue(&igi->igi_gq, IGMP_MAX_RESPONSE_BURST, loop); 3330 3331 /* 3332 * Slew transmission of bursts over 500ms intervals. 3333 */ 3334 if (igi->igi_gq.ifq_head != NULL) { 3335 igi->igi_v3_timer = 1 + IGMP_RANDOM_DELAY( 3336 IGMP_RESPONSE_BURST_INTERVAL); 3337 V_interface_timers_running = 1; 3338 } 3339} 3340 3341/* 3342 * Transmit the next pending IGMP message in the output queue. 3343 * 3344 * We get called from netisr_processqueue(). A mutex private to igmpoq 3345 * will be acquired and released around this routine. 3346 * 3347 * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis. 3348 * MRT: Nothing needs to be done, as IGMP traffic is always local to 3349 * a link and uses a link-scope multicast address. 3350 */ 3351static void 3352igmp_intr(struct mbuf *m) 3353{ 3354 struct ip_moptions imo; 3355 struct ifnet *ifp; 3356 struct mbuf *ipopts, *m0; 3357 int error; 3358 uint32_t ifindex; 3359 3360 CTR2(KTR_IGMPV3, "%s: transmit %p", __func__, m); 3361 3362 /* 3363 * Set VNET image pointer from enqueued mbuf chain 3364 * before doing anything else. Whilst we use interface 3365 * indexes to guard against interface detach, they are 3366 * unique to each VIMAGE and must be retrieved. 3367 */ 3368 CURVNET_SET(m->m_pkthdr.header); 3369 INIT_VNET_NET(curvnet); 3370 INIT_VNET_INET(curvnet); 3371 ifindex = igmp_restore_context(m); 3372 3373 /* 3374 * Check if the ifnet still exists. This limits the scope of 3375 * any race in the absence of a global ifp lock for low cost 3376 * (an array lookup). 3377 */ 3378 ifp = ifnet_byindex(ifindex); 3379 if (ifp == NULL) { 3380 CTR3(KTR_IGMPV3, "%s: dropped %p as ifindex %u went away.", 3381 __func__, m, ifindex); 3382 m_freem(m); 3383 IPSTAT_INC(ips_noroute); 3384 goto out; 3385 } 3386 3387 ipopts = V_igmp_sendra ? m_raopt : NULL; 3388 3389 imo.imo_multicast_ttl = 1; 3390 imo.imo_multicast_vif = -1; 3391 imo.imo_multicast_loop = (V_ip_mrouter != NULL); 3392 3393 /* 3394 * If the user requested that IGMP traffic be explicitly 3395 * redirected to the loopback interface (e.g. they are running a 3396 * MANET interface and the routing protocol needs to see the 3397 * updates), handle this now. 3398 */ 3399 if (m->m_flags & M_IGMP_LOOP) 3400 imo.imo_multicast_ifp = V_loif; 3401 else 3402 imo.imo_multicast_ifp = ifp; 3403 3404 if (m->m_flags & M_IGMPV2) { 3405 m0 = m; 3406 } else { 3407 m0 = igmp_v3_encap_report(ifp, m); 3408 if (m0 == NULL) { 3409 CTR2(KTR_IGMPV3, "%s: dropped %p", __func__, m); 3410 m_freem(m); 3411 IPSTAT_INC(ips_odropped); 3412 goto out; 3413 } 3414 } 3415 3416 igmp_scrub_context(m0); 3417 m->m_flags &= ~(M_PROTOFLAGS); 3418 m0->m_pkthdr.rcvif = V_loif; 3419#ifdef MAC 3420 mac_netinet_igmp_send(ifp, m0); 3421#endif 3422 error = ip_output(m0, ipopts, NULL, 0, &imo, NULL); 3423 if (error) { 3424 CTR3(KTR_IGMPV3, "%s: ip_output(%p) = %d", __func__, m0, error); 3425 goto out; 3426 } 3427 3428 IGMPSTAT_INC(igps_snd_reports); 3429 3430out: 3431 /* 3432 * We must restore the existing vnet pointer before 3433 * continuing as we are run from netisr context. 3434 */ 3435 CURVNET_RESTORE(); 3436} 3437 3438/* 3439 * Encapsulate an IGMPv3 report. 3440 * 3441 * The internal mbuf flag M_IGMPV3_HDR is used to indicate that the mbuf 3442 * chain has already had its IP/IGMPv3 header prepended. In this case 3443 * the function will not attempt to prepend; the lengths and checksums 3444 * will however be re-computed. 3445 * 3446 * Returns a pointer to the new mbuf chain head, or NULL if the 3447 * allocation failed. 3448 */ 3449static struct mbuf * 3450igmp_v3_encap_report(struct ifnet *ifp, struct mbuf *m) 3451{ 3452 INIT_VNET_INET(curvnet); 3453 struct igmp_report *igmp; 3454 struct ip *ip; 3455 int hdrlen, igmpreclen; 3456 3457 KASSERT((m->m_flags & M_PKTHDR), 3458 ("%s: mbuf chain %p is !M_PKTHDR", __func__, m)); 3459 3460 igmpreclen = m_length(m, NULL); 3461 hdrlen = sizeof(struct ip) + sizeof(struct igmp_report); 3462 3463 if (m->m_flags & M_IGMPV3_HDR) { 3464 igmpreclen -= hdrlen; 3465 } else { 3466 M_PREPEND(m, hdrlen, M_DONTWAIT); 3467 if (m == NULL) 3468 return (NULL); 3469 m->m_flags |= M_IGMPV3_HDR; 3470 } 3471 3472 CTR2(KTR_IGMPV3, "%s: igmpreclen is %d", __func__, igmpreclen); 3473 3474 m->m_data += sizeof(struct ip); 3475 m->m_len -= sizeof(struct ip); 3476 3477 igmp = mtod(m, struct igmp_report *); 3478 igmp->ir_type = IGMP_v3_HOST_MEMBERSHIP_REPORT; 3479 igmp->ir_rsv1 = 0; 3480 igmp->ir_rsv2 = 0; 3481 igmp->ir_numgrps = htons(m->m_pkthdr.PH_vt.vt_nrecs); 3482 igmp->ir_cksum = 0; 3483 igmp->ir_cksum = in_cksum(m, sizeof(struct igmp_report) + igmpreclen); 3484 m->m_pkthdr.PH_vt.vt_nrecs = 0; 3485 3486 m->m_data -= sizeof(struct ip); 3487 m->m_len += sizeof(struct ip); 3488 3489 ip = mtod(m, struct ip *); 3490 ip->ip_tos = IPTOS_PREC_INTERNETCONTROL; 3491 ip->ip_len = hdrlen + igmpreclen; 3492 ip->ip_off = IP_DF; 3493 ip->ip_p = IPPROTO_IGMP; 3494 ip->ip_sum = 0; 3495 3496 ip->ip_src.s_addr = INADDR_ANY; 3497 3498 if (m->m_flags & M_IGMP_LOOP) { 3499 struct in_ifaddr *ia; 3500 3501 IFP_TO_IA(ifp, ia); 3502 if (ia != NULL) 3503 ip->ip_src = ia->ia_addr.sin_addr; 3504 } 3505 3506 ip->ip_dst.s_addr = htonl(INADDR_ALLRPTS_GROUP); 3507 3508 return (m); 3509} 3510 3511#ifdef KTR 3512static char * 3513igmp_rec_type_to_str(const int type) 3514{ 3515 3516 switch (type) { 3517 case IGMP_CHANGE_TO_EXCLUDE_MODE: 3518 return "TO_EX"; 3519 break; 3520 case IGMP_CHANGE_TO_INCLUDE_MODE: 3521 return "TO_IN"; 3522 break; 3523 case IGMP_MODE_IS_EXCLUDE: 3524 return "MODE_EX"; 3525 break; 3526 case IGMP_MODE_IS_INCLUDE: 3527 return "MODE_IN"; 3528 break; 3529 case IGMP_ALLOW_NEW_SOURCES: 3530 return "ALLOW_NEW"; 3531 break; 3532 case IGMP_BLOCK_OLD_SOURCES: 3533 return "BLOCK_OLD"; 3534 break; 3535 default: 3536 break; 3537 } 3538 return "unknown"; 3539} 3540#endif 3541 3542static void 3543igmp_sysinit(void) 3544{ 3545 3546 CTR1(KTR_IGMPV3, "%s: initializing", __func__); 3547 3548 IGMP_LOCK_INIT(); 3549 3550 mtx_init(&igmpoq.ifq_mtx, "igmpoq_mtx", NULL, MTX_DEF); 3551 IFQ_SET_MAXLEN(&igmpoq, IFQ_MAXLEN); 3552 3553 m_raopt = igmp_ra_alloc(); 3554 3555 netisr_register(NETISR_IGMP, igmp_intr, &igmpoq, 0); 3556} 3557 3558static void 3559igmp_sysuninit(void) 3560{ 3561 3562 CTR1(KTR_IGMPV3, "%s: tearing down", __func__); 3563 3564 netisr_unregister(NETISR_IGMP); 3565 mtx_destroy(&igmpoq.ifq_mtx); 3566 3567 m_free(m_raopt); 3568 m_raopt = NULL; 3569 3570 IGMP_LOCK_DESTROY(); 3571} 3572 3573/* 3574 * Initialize an IGMPv3 instance. 3575 * VIMAGE: Assumes curvnet set by caller and called per vimage. 3576 */ 3577static int 3578vnet_igmp_iattach(const void *unused __unused) 3579{ 3580 INIT_VNET_INET(curvnet); 3581 3582 CTR1(KTR_IGMPV3, "%s: initializing", __func__); 3583 3584 LIST_INIT(&V_igi_head); 3585 3586 V_current_state_timers_running = 0; 3587 V_state_change_timers_running = 0; 3588 V_interface_timers_running = 0; 3589 3590 /* 3591 * Initialize sysctls to default values. 3592 */ 3593 V_igmp_recvifkludge = 1; 3594 V_igmp_sendra = 1; 3595 V_igmp_sendlocal = 1; 3596 V_igmp_v1enable = 1; 3597 V_igmp_v2enable = 1; 3598 V_igmp_legacysupp = 0; 3599 V_igmp_default_version = IGMP_VERSION_3; 3600 V_igmp_gsrdelay.tv_sec = 10; 3601 V_igmp_gsrdelay.tv_usec = 0; 3602 3603 memset(&V_igmpstat, 0, sizeof(struct igmpstat)); 3604 V_igmpstat.igps_version = IGPS_VERSION_3; 3605 V_igmpstat.igps_len = sizeof(struct igmpstat); 3606 3607 return (0); 3608} 3609 3610static int 3611vnet_igmp_idetach(const void *unused __unused) 3612{ 3613 INIT_VNET_INET(curvnet); 3614 3615 CTR1(KTR_IGMPV3, "%s: tearing down", __func__); 3616 3617 KASSERT(LIST_EMPTY(&V_igi_head), 3618 ("%s: igi list not empty; ifnets not detached?", __func__)); 3619 3620 return (0); 3621} 3622 3623#ifndef VIMAGE_GLOBALS 3624static vnet_modinfo_t vnet_igmp_modinfo = { 3625 .vmi_id = VNET_MOD_IGMP, 3626 .vmi_name = "igmp", 3627 .vmi_dependson = VNET_MOD_INET, 3628 .vmi_iattach = vnet_igmp_iattach, 3629 .vmi_idetach = vnet_igmp_idetach 3630}; 3631#endif 3632 3633static int 3634igmp_modevent(module_t mod, int type, void *unused __unused) 3635{ 3636 3637 switch (type) { 3638 case MOD_LOAD: 3639 igmp_sysinit(); 3640#ifndef VIMAGE_GLOBALS 3641 vnet_mod_register(&vnet_igmp_modinfo); 3642#else 3643 vnet_igmp_iattach(NULL); 3644#endif 3645 break; 3646 case MOD_UNLOAD: 3647#ifndef VIMAGE_GLOBALS 3648#ifdef NOTYET 3649 vnet_mod_deregister(&vnet_igmp_modinfo); 3650#endif 3651#else 3652 vnet_igmp_idetach(NULL); 3653#endif 3654 igmp_sysuninit(); 3655 break; 3656 default: 3657 return (EOPNOTSUPP); 3658 } 3659 return (0); 3660} 3661 3662static moduledata_t igmp_mod = { 3663 "igmp", 3664 igmp_modevent, 3665 0 3666}; 3667DECLARE_MODULE(igmp, igmp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 3668