igmp.c revision 181803
1139823Simp/*- 21541Srgrimes * Copyright (c) 1988 Stephen Deering. 31541Srgrimes * Copyright (c) 1992, 1993 41541Srgrimes * The Regents of the University of California. All rights reserved. 51541Srgrimes * 61541Srgrimes * This code is derived from software contributed to Berkeley by 71541Srgrimes * Stephen Deering of Stanford University. 81541Srgrimes * 91541Srgrimes * Redistribution and use in source and binary forms, with or without 101541Srgrimes * modification, are permitted provided that the following conditions 111541Srgrimes * are met: 121541Srgrimes * 1. Redistributions of source code must retain the above copyright 131541Srgrimes * notice, this list of conditions and the following disclaimer. 141541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 151541Srgrimes * notice, this list of conditions and the following disclaimer in the 161541Srgrimes * documentation and/or other materials provided with the distribution. 171541Srgrimes * 4. Neither the name of the University nor the names of its contributors 181541Srgrimes * may be used to endorse or promote products derived from this software 191541Srgrimes * without specific prior written permission. 201541Srgrimes * 211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311541Srgrimes * SUCH DAMAGE. 321541Srgrimes * 331541Srgrimes * @(#)igmp.c 8.1 (Berkeley) 7/19/93 341541Srgrimes */ 351541Srgrimes 362531Swollman/* 372531Swollman * Internet Group Management Protocol (IGMP) routines. 382531Swollman * 392531Swollman * Written by Steve Deering, Stanford, May 1988. 402531Swollman * Modified by Rosen Sharma, Stanford, Aug 1994. 419209Swollman * Modified by Bill Fenner, Xerox PARC, Feb 1995. 4214622Sfenner * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995. 432531Swollman * 4414622Sfenner * MULTICAST Revision: 3.5.1.4 452531Swollman */ 461541Srgrimes 47172467Ssilby#include <sys/cdefs.h> 48172467Ssilby__FBSDID("$FreeBSD: head/sys/netinet/igmp.c 181803 2008-08-17 23:27:27Z bz $"); 49172467Ssilby 50101091Srwatson#include "opt_mac.h" 51101091Srwatson 521541Srgrimes#include <sys/param.h> 531549Srgrimes#include <sys/systm.h> 5429024Sbde#include <sys/malloc.h> 551541Srgrimes#include <sys/mbuf.h> 561541Srgrimes#include <sys/socket.h> 571541Srgrimes#include <sys/protosw.h> 5812296Sphk#include <sys/kernel.h> 596472Swollman#include <sys/sysctl.h> 60181803Sbz#include <sys/vimage.h> 611541Srgrimes 621541Srgrimes#include <net/if.h> 631541Srgrimes#include <net/route.h> 641541Srgrimes 651541Srgrimes#include <netinet/in.h> 661541Srgrimes#include <netinet/in_var.h> 671541Srgrimes#include <netinet/in_systm.h> 681541Srgrimes#include <netinet/ip.h> 691541Srgrimes#include <netinet/ip_var.h> 70152592Sandre#include <netinet/ip_options.h> 711541Srgrimes#include <netinet/igmp.h> 721541Srgrimes#include <netinet/igmp_var.h> 731541Srgrimes 7460105Sjlemon#include <machine/in_cksum.h> 7560105Sjlemon 76163606Srwatson#include <security/mac/mac_framework.h> 77163606Srwatson 7842776Sfennerstatic MALLOC_DEFINE(M_IGMP, "igmp", "igmp state"); 7930309Sphk 80119181Srwatsonstatic struct router_info *find_rti(struct ifnet *ifp); 81119181Srwatsonstatic void igmp_sendpkt(struct in_multi *, int, unsigned long); 8212579Sbde 8312704Sphkstatic struct igmpstat igmpstat; 842531Swollman 85119181SrwatsonSYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW, &igmpstat, 86119181Srwatson igmpstat, ""); 8712296Sphk 88130333Srwatson/* 89164863Srwatson * igmp_mtx protects all mutable global variables in igmp.c, as well as the 90164863Srwatson * data fields in struct router_info. In general, a router_info structure 91164863Srwatson * will be valid as long as the referencing struct in_multi is valid, so no 92164863Srwatson * reference counting is used. We allow unlocked reads of router_info data 93164863Srwatson * when accessed via an in_multi read-only. 94130333Srwatson */ 95130333Srwatsonstatic struct mtx igmp_mtx; 96119181Srwatsonstatic SLIST_HEAD(, router_info) router_info_head; 979209Swollmanstatic int igmp_timers_are_running; 98130333Srwatson 99130333Srwatson/* 100130333Srwatson * XXXRW: can we define these such that these can be made const? In any 101130333Srwatson * case, these shouldn't be changed after igmp_init() and therefore don't 102130333Srwatson * need locking. 103130333Srwatson */ 1041541Srgrimesstatic u_long igmp_all_hosts_group; 10514622Sfennerstatic u_long igmp_all_rtrs_group; 106130333Srwatson 10714622Sfennerstatic struct mbuf *router_alert; 108119181Srwatsonstatic struct route igmprt; 1091541Srgrimes 110119180Srwatson#ifdef IGMP_DEBUG 111119180Srwatson#define IGMP_PRINTF(x) printf(x) 112119180Srwatson#else 113119180Srwatson#define IGMP_PRINTF(x) 114119180Srwatson#endif 115119180Srwatson 1161541Srgrimesvoid 117119181Srwatsonigmp_init(void) 1181541Srgrimes{ 11914622Sfenner struct ipoption *ra; 12014622Sfenner 1211541Srgrimes /* 1221541Srgrimes * To avoid byte-swapping the same value over and over again. 1231541Srgrimes */ 1241541Srgrimes igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP); 12514622Sfenner igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP); 1269209Swollman 1279209Swollman igmp_timers_are_running = 0; 1289209Swollman 12914622Sfenner /* 130164863Srwatson * Construct a Router Alert option to use in outgoing packets. 13114622Sfenner */ 132111119Simp MGET(router_alert, M_DONTWAIT, MT_DATA); 13314622Sfenner ra = mtod(router_alert, struct ipoption *); 13414622Sfenner ra->ipopt_dst.s_addr = 0; 13514622Sfenner ra->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */ 13614622Sfenner ra->ipopt_list[1] = 0x04; /* 4 bytes long */ 13714622Sfenner ra->ipopt_list[2] = 0x00; 13814622Sfenner ra->ipopt_list[3] = 0x00; 13914622Sfenner router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1]; 14014622Sfenner 141130333Srwatson mtx_init(&igmp_mtx, "igmp_mtx", NULL, MTX_DEF); 142181803Sbz SLIST_INIT(&V_router_info_head); 1431541Srgrimes} 1441541Srgrimes 14512704Sphkstatic struct router_info * 146119181Srwatsonfind_rti(struct ifnet *ifp) 1472531Swollman{ 148119180Srwatson struct router_info *rti; 1492531Swollman 150130333Srwatson mtx_assert(&igmp_mtx, MA_OWNED); 151119180Srwatson IGMP_PRINTF("[igmp.c, _find_rti] --> entering \n"); 152181803Sbz SLIST_FOREACH(rti, &V_router_info_head, rti_list) { 153119180Srwatson if (rti->rti_ifp == ifp) { 154119180Srwatson IGMP_PRINTF( 155119180Srwatson "[igmp.c, _find_rti] --> found old entry \n"); 156164863Srwatson return (rti); 157119181Srwatson } 158119181Srwatson } 15942776Sfenner MALLOC(rti, struct router_info *, sizeof *rti, M_IGMP, M_NOWAIT); 160144163Ssam if (rti == NULL) { 161164863Srwatson IGMP_PRINTF("[igmp.c, _find_rti] --> no memory for entry\n"); 162164863Srwatson return (NULL); 163144163Ssam } 164119181Srwatson rti->rti_ifp = ifp; 165119181Srwatson rti->rti_type = IGMP_V2_ROUTER; 166119181Srwatson rti->rti_time = 0; 167181803Sbz SLIST_INSERT_HEAD(&V_router_info_head, rti, rti_list); 168119180Srwatson IGMP_PRINTF("[igmp.c, _find_rti] --> created an entry \n"); 169164863Srwatson return (rti); 1702531Swollman} 1712531Swollman 1721541Srgrimesvoid 173119181Srwatsonigmp_input(register struct mbuf *m, int off) 1741541Srgrimes{ 175107113Sluigi register int iphlen = off; 176107113Sluigi register struct igmp *igmp; 177107113Sluigi register struct ip *ip; 178107113Sluigi register int igmplen; 179107113Sluigi register struct ifnet *ifp = m->m_pkthdr.rcvif; 180107113Sluigi register int minlen; 181107113Sluigi register struct in_multi *inm; 182107113Sluigi register struct in_ifaddr *ia; 1831541Srgrimes struct in_multistep step; 1842531Swollman struct router_info *rti; 1858546Sdg int timer; /** timer value in the igmp query header **/ 1861541Srgrimes 187181803Sbz ++V_igmpstat.igps_rcv_total; 1881541Srgrimes 1891541Srgrimes ip = mtod(m, struct ip *); 1901541Srgrimes igmplen = ip->ip_len; 1911541Srgrimes 1921541Srgrimes /* 193164863Srwatson * Validate lengths. 1941541Srgrimes */ 1951541Srgrimes if (igmplen < IGMP_MINLEN) { 196181803Sbz ++V_igmpstat.igps_rcv_tooshort; 1971541Srgrimes m_freem(m); 1981541Srgrimes return; 1991541Srgrimes } 2001541Srgrimes minlen = iphlen + IGMP_MINLEN; 2011541Srgrimes if ((m->m_flags & M_EXT || m->m_len < minlen) && 2021541Srgrimes (m = m_pullup(m, minlen)) == 0) { 203181803Sbz ++V_igmpstat.igps_rcv_tooshort; 2041541Srgrimes return; 2051541Srgrimes } 2061541Srgrimes 2071541Srgrimes /* 208164863Srwatson * Validate checksum. 2091541Srgrimes */ 2101541Srgrimes m->m_data += iphlen; 2111541Srgrimes m->m_len -= iphlen; 2121541Srgrimes igmp = mtod(m, struct igmp *); 2131541Srgrimes if (in_cksum(m, igmplen)) { 214181803Sbz ++V_igmpstat.igps_rcv_badsum; 2151541Srgrimes m_freem(m); 2161541Srgrimes return; 2171541Srgrimes } 2181541Srgrimes m->m_data -= iphlen; 2191541Srgrimes m->m_len += iphlen; 2202531Swollman 2211541Srgrimes ip = mtod(m, struct ip *); 2228546Sdg timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE; 22341702Sdillon if (timer == 0) 22441702Sdillon timer = 1; 2251541Srgrimes 22614622Sfenner /* 22714622Sfenner * In the IGMPv2 specification, there are 3 states and a flag. 22814622Sfenner * 22914622Sfenner * In Non-Member state, we simply don't have a membership record. 230164863Srwatson * In Delaying Member state, our timer is running (inm->inm_timer). 231164863Srwatson * In Idle Member state, our timer is not running (inm->inm_timer==0). 23214622Sfenner * 233164863Srwatson * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if we 234164863Srwatson * have heard a report from another member, or IGMP_IREPORTEDLAST if 235164863Srwatson * I sent the last report. 23614622Sfenner */ 2371541Srgrimes switch (igmp->igmp_type) { 23814622Sfenner case IGMP_MEMBERSHIP_QUERY: 239181803Sbz ++V_igmpstat.igps_rcv_queries; 2401541Srgrimes 2418090Spst if (ifp->if_flags & IFF_LOOPBACK) 2421541Srgrimes break; 2431541Srgrimes 2442531Swollman if (igmp->igmp_code == 0) { 24514622Sfenner /* 24614622Sfenner * Old router. Remember that the querier on this 247164863Srwatson * interface is old, and set the timer to the value 248164863Srwatson * in RFC 1112. 24914622Sfenner */ 2504028Spst 251130333Srwatson mtx_lock(&igmp_mtx); 252130333Srwatson rti = find_rti(ifp); 253144163Ssam if (rti == NULL) { 254144163Ssam mtx_unlock(&igmp_mtx); 255144163Ssam m_freem(m); 256144163Ssam return; 257144163Ssam } 25814622Sfenner rti->rti_type = IGMP_V1_ROUTER; 25914622Sfenner rti->rti_time = 0; 260130333Srwatson mtx_unlock(&igmp_mtx); 2614028Spst 26214622Sfenner timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ; 2634028Spst 26414622Sfenner if (ip->ip_dst.s_addr != igmp_all_hosts_group || 26514622Sfenner igmp->igmp_group.s_addr != 0) { 266181803Sbz ++V_igmpstat.igps_rcv_badqueries; 2672531Swollman m_freem(m); 2682531Swollman return; 2692531Swollman } 27014622Sfenner } else { 2712531Swollman /* 27214622Sfenner * New router. Simply do the new validity check. 2732531Swollman */ 27414622Sfenner 27514622Sfenner if (igmp->igmp_group.s_addr != 0 && 27614622Sfenner !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) { 277181803Sbz ++V_igmpstat.igps_rcv_badqueries; 27814622Sfenner m_freem(m); 27914622Sfenner return; 28014622Sfenner } 28114622Sfenner } 2822531Swollman 28314622Sfenner /* 284164863Srwatson * - Start the timers in all of our membership records that 285164863Srwatson * the query applies to for the interface on which the 286164863Srwatson * query arrived excl. those that belong to the "all-hosts" 287164863Srwatson * group (224.0.0.1). 288164863Srwatson * - Restart any timer that is already running but has a 289164863Srwatson * value longer than the requested timeout. 290164863Srwatson * - Use the value specified in the query message as the 291164863Srwatson * maximum timeout. 29214622Sfenner */ 293148682Srwatson IN_MULTI_LOCK(); 29414622Sfenner IN_FIRST_MULTI(step, inm); 29514622Sfenner while (inm != NULL) { 29614622Sfenner if (inm->inm_ifp == ifp && 29714622Sfenner inm->inm_addr.s_addr != igmp_all_hosts_group && 29814622Sfenner (igmp->igmp_group.s_addr == 0 || 29914622Sfenner igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) { 30014622Sfenner if (inm->inm_timer == 0 || 30114622Sfenner inm->inm_timer > timer) { 30214622Sfenner inm->inm_timer = 30314622Sfenner IGMP_RANDOM_DELAY(timer); 3042531Swollman igmp_timers_are_running = 1; 3052531Swollman } 3061541Srgrimes } 3071541Srgrimes IN_NEXT_MULTI(step, inm); 3081541Srgrimes } 309148682Srwatson IN_MULTI_UNLOCK(); 3101541Srgrimes break; 3111541Srgrimes 31214622Sfenner case IGMP_V1_MEMBERSHIP_REPORT: 31314622Sfenner case IGMP_V2_MEMBERSHIP_REPORT: 3149209Swollman /* 31514622Sfenner * For fast leave to work, we have to know that we are the 316164863Srwatson * last person to send a report for this group. Reports can 317164863Srwatson * potentially get looped back if we are a multicast router, 318164863Srwatson * so discard reports sourced by me. 3199209Swollman */ 32014622Sfenner IFP_TO_IA(ifp, ia); 321164863Srwatson if (ia != NULL && 322164863Srwatson ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr) 32314622Sfenner break; 32414622Sfenner 325181803Sbz ++V_igmpstat.igps_rcv_reports; 3261541Srgrimes 3278090Spst if (ifp->if_flags & IFF_LOOPBACK) 3281541Srgrimes break; 3291541Srgrimes 33014622Sfenner if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) { 331181803Sbz ++V_igmpstat.igps_rcv_badreports; 3321541Srgrimes m_freem(m); 3331541Srgrimes return; 3341541Srgrimes } 3351541Srgrimes 3361541Srgrimes /* 3371541Srgrimes * KLUDGE: if the IP source address of the report has an 3381541Srgrimes * unspecified (i.e., zero) subnet number, as is allowed for 3391541Srgrimes * a booting host, replace it with the correct subnet number 34096432Sdd * so that a process-level multicast routing daemon can 3411541Srgrimes * determine which subnet it arrived from. This is necessary 3421541Srgrimes * to compensate for the lack of any way for a process to 3431541Srgrimes * determine the arrival interface of an incoming packet. 3441541Srgrimes */ 345164863Srwatson if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0) { 346164863Srwatson if (ia != NULL) 347164863Srwatson ip->ip_src.s_addr = htonl(ia->ia_subnet); 348164863Srwatson } 3491541Srgrimes 3501541Srgrimes /* 351164863Srwatson * If we belong to the group being reported, stop our timer 352164863Srwatson * for that group. 3531541Srgrimes */ 354148682Srwatson IN_MULTI_LOCK(); 3551541Srgrimes IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm); 3562531Swollman if (inm != NULL) { 35714622Sfenner inm->inm_timer = 0; 358181803Sbz ++V_igmpstat.igps_rcv_ourreports; 35914622Sfenner inm->inm_state = IGMP_OTHERMEMBER; 3602531Swollman } 361148682Srwatson IN_MULTI_UNLOCK(); 3621541Srgrimes break; 3631541Srgrimes } 3641541Srgrimes 3651541Srgrimes /* 366164863Srwatson * Pass all valid IGMP packets up to any process(es) listening on a 367164863Srwatson * raw IGMP socket. 3681541Srgrimes */ 36982890Sjulian rip_input(m, off); 3701541Srgrimes} 3711541Srgrimes 3721541Srgrimesvoid 373119181Srwatsonigmp_joingroup(struct in_multi *inm) 3741541Srgrimes{ 3751541Srgrimes 376148682Srwatson IN_MULTI_LOCK_ASSERT(); 377148682Srwatson 37814622Sfenner if (inm->inm_addr.s_addr == igmp_all_hosts_group 37914622Sfenner || inm->inm_ifp->if_flags & IFF_LOOPBACK) { 3801541Srgrimes inm->inm_timer = 0; 38114622Sfenner inm->inm_state = IGMP_OTHERMEMBER; 38214622Sfenner } else { 383130333Srwatson mtx_lock(&igmp_mtx); 38414622Sfenner inm->inm_rti = find_rti(inm->inm_ifp); 385130333Srwatson mtx_unlock(&igmp_mtx); 386144163Ssam if (inm->inm_rti != NULL) { 387144163Ssam igmp_sendpkt(inm, inm->inm_rti->rti_type, 0); 388144163Ssam inm->inm_timer = IGMP_RANDOM_DELAY( 3892531Swollman IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ); 390144163Ssam inm->inm_state = IGMP_IREPORTEDLAST; 391144163Ssam igmp_timers_are_running = 1; 392144163Ssam } 393144163Ssam /* XXX handling of failure case? */ 3941541Srgrimes } 3951541Srgrimes} 3961541Srgrimes 3971541Srgrimesvoid 398119181Srwatsonigmp_leavegroup(struct in_multi *inm) 3991541Srgrimes{ 400119181Srwatson 401148682Srwatson IN_MULTI_LOCK_ASSERT(); 402148682Srwatson 40314622Sfenner if (inm->inm_state == IGMP_IREPORTEDLAST && 40414622Sfenner inm->inm_addr.s_addr != igmp_all_hosts_group && 40514622Sfenner !(inm->inm_ifp->if_flags & IFF_LOOPBACK) && 40614622Sfenner inm->inm_rti->rti_type != IGMP_V1_ROUTER) 40714622Sfenner igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group); 4081541Srgrimes} 4091541Srgrimes 4101541Srgrimesvoid 411119181Srwatsonigmp_fasttimo(void) 4121541Srgrimes{ 413107113Sluigi register struct in_multi *inm; 4141541Srgrimes struct in_multistep step; 4151541Srgrimes 4161541Srgrimes /* 417164863Srwatson * Quick check to see if any work needs to be done, in order to 418164863Srwatson * minimize the overhead of fasttimo processing. 4191541Srgrimes */ 4209209Swollman 4211541Srgrimes if (!igmp_timers_are_running) 4221541Srgrimes return; 4231541Srgrimes 424148682Srwatson IN_MULTI_LOCK(); 4251541Srgrimes igmp_timers_are_running = 0; 4261541Srgrimes IN_FIRST_MULTI(step, inm); 4271541Srgrimes while (inm != NULL) { 4281541Srgrimes if (inm->inm_timer == 0) { 4291541Srgrimes /* do nothing */ 4301541Srgrimes } else if (--inm->inm_timer == 0) { 43114622Sfenner igmp_sendpkt(inm, inm->inm_rti->rti_type, 0); 43214622Sfenner inm->inm_state = IGMP_IREPORTEDLAST; 4331541Srgrimes } else { 4341541Srgrimes igmp_timers_are_running = 1; 4351541Srgrimes } 4361541Srgrimes IN_NEXT_MULTI(step, inm); 4371541Srgrimes } 438148682Srwatson IN_MULTI_UNLOCK(); 4391541Srgrimes} 4401541Srgrimes 4412531Swollmanvoid 442119181Srwatsonigmp_slowtimo(void) 4432531Swollman{ 444119180Srwatson struct router_info *rti; 4452531Swollman 446119180Srwatson IGMP_PRINTF("[igmp.c,_slowtimo] -- > entering \n"); 447130333Srwatson mtx_lock(&igmp_mtx); 448181803Sbz SLIST_FOREACH(rti, &V_router_info_head, rti_list) { 449119181Srwatson if (rti->rti_type == IGMP_V1_ROUTER) { 450119181Srwatson rti->rti_time++; 451119181Srwatson if (rti->rti_time >= IGMP_AGE_THRESHOLD) 452119181Srwatson rti->rti_type = IGMP_V2_ROUTER; 4532531Swollman } 4542531Swollman } 455130333Srwatson mtx_unlock(&igmp_mtx); 456119180Srwatson IGMP_PRINTF("[igmp.c,_slowtimo] -- > exiting \n"); 4572531Swollman} 4582531Swollman 4591541Srgrimesstatic void 460119181Srwatsonigmp_sendpkt(struct in_multi *inm, int type, unsigned long addr) 4611541Srgrimes{ 462119181Srwatson struct mbuf *m; 463119181Srwatson struct igmp *igmp; 464119181Srwatson struct ip *ip; 465119181Srwatson struct ip_moptions imo; 4661541Srgrimes 467148682Srwatson IN_MULTI_LOCK_ASSERT(); 468148682Srwatson 469151967Sandre MGETHDR(m, M_DONTWAIT, MT_DATA); 470119181Srwatson if (m == NULL) 471119181Srwatson return; 4722531Swollman 473181803Sbz m->m_pkthdr.rcvif = V_loif; 474101091Srwatson#ifdef MAC 475173095Srwatson mac_netinet_igmp_send(inm->inm_ifp, m); 476101091Srwatson#endif 4771541Srgrimes m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN; 4782531Swollman MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip)); 4792531Swollman m->m_data += sizeof(struct ip); 480119181Srwatson m->m_len = IGMP_MINLEN; 481119181Srwatson igmp = mtod(m, struct igmp *); 482119181Srwatson igmp->igmp_type = type; 483119181Srwatson igmp->igmp_code = 0; 484119181Srwatson igmp->igmp_group = inm->inm_addr; 485119181Srwatson igmp->igmp_cksum = 0; 486119181Srwatson igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN); 4871541Srgrimes 488119181Srwatson m->m_data -= sizeof(struct ip); 489119181Srwatson m->m_len += sizeof(struct ip); 490119181Srwatson ip = mtod(m, struct ip *); 491119181Srwatson ip->ip_tos = 0; 492119181Srwatson ip->ip_len = sizeof(struct ip) + IGMP_MINLEN; 493119181Srwatson ip->ip_off = 0; 494119181Srwatson ip->ip_p = IPPROTO_IGMP; 495119181Srwatson ip->ip_src.s_addr = INADDR_ANY; 496119181Srwatson ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr; 4971541Srgrimes 498119181Srwatson imo.imo_multicast_ifp = inm->inm_ifp; 499119181Srwatson imo.imo_multicast_ttl = 1; 50015292Swollman imo.imo_multicast_vif = -1; 501119181Srwatson /* 502119181Srwatson * Request loopback of the report if we are acting as a multicast 503119181Srwatson * router, so that the process-level routing daemon can hear it. 504119181Srwatson */ 505181803Sbz imo.imo_multicast_loop = (V_ip_mrouter != NULL); 5061541Srgrimes 50715292Swollman /* 508164863Srwatson * XXX: Do we have to worry about reentrancy here? Don't think so. 50915292Swollman */ 510119181Srwatson ip_output(m, router_alert, &igmprt, 0, &imo, NULL); 5112531Swollman 512181803Sbz ++V_igmpstat.igps_snd_reports; 5131541Srgrimes} 514