igmp.c revision 130333
11541Srgrimes/*
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
3450477Speter * $FreeBSD: head/sys/netinet/igmp.c 130333 2004-06-11 03:42:37Z rwatson $
351541Srgrimes */
361541Srgrimes
372531Swollman/*
382531Swollman * Internet Group Management Protocol (IGMP) routines.
392531Swollman *
402531Swollman * Written by Steve Deering, Stanford, May 1988.
412531Swollman * Modified by Rosen Sharma, Stanford, Aug 1994.
429209Swollman * Modified by Bill Fenner, Xerox PARC, Feb 1995.
4314622Sfenner * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
442531Swollman *
4514622Sfenner * MULTICAST Revision: 3.5.1.4
462531Swollman */
471541Srgrimes
48101091Srwatson#include "opt_mac.h"
49101091Srwatson
501541Srgrimes#include <sys/param.h>
511549Srgrimes#include <sys/systm.h>
52101091Srwatson#include <sys/mac.h>
5329024Sbde#include <sys/malloc.h>
541541Srgrimes#include <sys/mbuf.h>
551541Srgrimes#include <sys/socket.h>
561541Srgrimes#include <sys/protosw.h>
5712296Sphk#include <sys/kernel.h>
586472Swollman#include <sys/sysctl.h>
591541Srgrimes
601541Srgrimes#include <net/if.h>
611541Srgrimes#include <net/route.h>
621541Srgrimes
631541Srgrimes#include <netinet/in.h>
641541Srgrimes#include <netinet/in_var.h>
651541Srgrimes#include <netinet/in_systm.h>
661541Srgrimes#include <netinet/ip.h>
671541Srgrimes#include <netinet/ip_var.h>
681541Srgrimes#include <netinet/igmp.h>
691541Srgrimes#include <netinet/igmp_var.h>
701541Srgrimes
7160105Sjlemon#include <machine/in_cksum.h>
7260105Sjlemon
7342776Sfennerstatic MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
7430309Sphk
75119181Srwatsonstatic struct router_info	*find_rti(struct ifnet *ifp);
76119181Srwatsonstatic void	igmp_sendpkt(struct in_multi *, int, unsigned long);
7712579Sbde
7812704Sphkstatic struct igmpstat igmpstat;
792531Swollman
80119181SrwatsonSYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW, &igmpstat,
81119181Srwatson    igmpstat, "");
8212296Sphk
83130333Srwatson/*
84130333Srwatson * igmp_mtx protects all mutable global variables in igmp.c, as well as
85130333Srwatson * the data fields in struct router_info.  In general, a router_info
86130333Srwatson * structure will be valid as long as the referencing struct in_multi is
87130333Srwatson * valid, so no reference counting is used.  We allow unlocked reads of
88130333Srwatson * router_info data when accessed via an in_multi read-only.
89130333Srwatson */
90130333Srwatsonstatic struct mtx igmp_mtx;
91119181Srwatsonstatic SLIST_HEAD(, router_info) router_info_head;
929209Swollmanstatic int igmp_timers_are_running;
93130333Srwatson
94130333Srwatson/*
95130333Srwatson * XXXRW: can we define these such that these can be made const?  In any
96130333Srwatson * case, these shouldn't be changed after igmp_init() and therefore don't
97130333Srwatson * need locking.
98130333Srwatson */
991541Srgrimesstatic u_long igmp_all_hosts_group;
10014622Sfennerstatic u_long igmp_all_rtrs_group;
101130333Srwatson
10214622Sfennerstatic struct mbuf *router_alert;
103119181Srwatsonstatic struct route igmprt;
1041541Srgrimes
105119180Srwatson#ifdef IGMP_DEBUG
106119180Srwatson#define	IGMP_PRINTF(x)	printf(x)
107119180Srwatson#else
108119180Srwatson#define	IGMP_PRINTF(x)
109119180Srwatson#endif
110119180Srwatson
1111541Srgrimesvoid
112119181Srwatsonigmp_init(void)
1131541Srgrimes{
11414622Sfenner	struct ipoption *ra;
11514622Sfenner
1161541Srgrimes	/*
1171541Srgrimes	 * To avoid byte-swapping the same value over and over again.
1181541Srgrimes	 */
1191541Srgrimes	igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
12014622Sfenner	igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP);
1219209Swollman
1229209Swollman	igmp_timers_are_running = 0;
1239209Swollman
12414622Sfenner	/*
12514622Sfenner	 * Construct a Router Alert option to use in outgoing packets
12614622Sfenner	 */
127111119Simp	MGET(router_alert, M_DONTWAIT, MT_DATA);
12814622Sfenner	ra = mtod(router_alert, struct ipoption *);
12914622Sfenner	ra->ipopt_dst.s_addr = 0;
13014622Sfenner	ra->ipopt_list[0] = IPOPT_RA;	/* Router Alert Option */
13114622Sfenner	ra->ipopt_list[1] = 0x04;	/* 4 bytes long */
13214622Sfenner	ra->ipopt_list[2] = 0x00;
13314622Sfenner	ra->ipopt_list[3] = 0x00;
13414622Sfenner	router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1];
13514622Sfenner
136130333Srwatson	mtx_init(&igmp_mtx, "igmp_mtx", NULL, MTX_DEF);
137119180Srwatson	SLIST_INIT(&router_info_head);
1381541Srgrimes}
1391541Srgrimes
14012704Sphkstatic struct router_info *
141119181Srwatsonfind_rti(struct ifnet *ifp)
1422531Swollman{
143119180Srwatson	struct router_info *rti;
1442531Swollman
145130333Srwatson	mtx_assert(&igmp_mtx, MA_OWNED);
146119180Srwatson	IGMP_PRINTF("[igmp.c, _find_rti] --> entering \n");
147119180Srwatson	SLIST_FOREACH(rti, &router_info_head, rti_list) {
148119180Srwatson		if (rti->rti_ifp == ifp) {
149119180Srwatson			IGMP_PRINTF(
150119180Srwatson			    "[igmp.c, _find_rti] --> found old entry \n");
151119181Srwatson			return rti;
152119181Srwatson		}
153119181Srwatson	}
154130333Srwatson	/*
155130333Srwatson	 * XXXRW: return value of malloc not checked, despite M_NOWAIT.
156130333Srwatson	 */
15742776Sfenner	MALLOC(rti, struct router_info *, sizeof *rti, M_IGMP, M_NOWAIT);
158119181Srwatson	rti->rti_ifp = ifp;
159119181Srwatson	rti->rti_type = IGMP_V2_ROUTER;
160119181Srwatson	rti->rti_time = 0;
161119180Srwatson	SLIST_INSERT_HEAD(&router_info_head, rti, rti_list);
162119180Srwatson
163119180Srwatson	IGMP_PRINTF("[igmp.c, _find_rti] --> created an entry \n");
164119181Srwatson	return rti;
1652531Swollman}
1662531Swollman
1671541Srgrimesvoid
168119181Srwatsonigmp_input(register struct mbuf *m, int off)
1691541Srgrimes{
170107113Sluigi	register int iphlen = off;
171107113Sluigi	register struct igmp *igmp;
172107113Sluigi	register struct ip *ip;
173107113Sluigi	register int igmplen;
174107113Sluigi	register struct ifnet *ifp = m->m_pkthdr.rcvif;
175107113Sluigi	register int minlen;
176107113Sluigi	register struct in_multi *inm;
177107113Sluigi	register struct in_ifaddr *ia;
1781541Srgrimes	struct in_multistep step;
1792531Swollman	struct router_info *rti;
1808546Sdg	int timer; /** timer value in the igmp query header **/
1811541Srgrimes
1821541Srgrimes	++igmpstat.igps_rcv_total;
1831541Srgrimes
1841541Srgrimes	ip = mtod(m, struct ip *);
1851541Srgrimes	igmplen = ip->ip_len;
1861541Srgrimes
1871541Srgrimes	/*
1881541Srgrimes	 * Validate lengths
1891541Srgrimes	 */
1901541Srgrimes	if (igmplen < IGMP_MINLEN) {
1911541Srgrimes		++igmpstat.igps_rcv_tooshort;
1921541Srgrimes		m_freem(m);
1931541Srgrimes		return;
1941541Srgrimes	}
1951541Srgrimes	minlen = iphlen + IGMP_MINLEN;
1961541Srgrimes	if ((m->m_flags & M_EXT || m->m_len < minlen) &&
1971541Srgrimes	    (m = m_pullup(m, minlen)) == 0) {
1981541Srgrimes		++igmpstat.igps_rcv_tooshort;
1991541Srgrimes		return;
2001541Srgrimes	}
2011541Srgrimes
2021541Srgrimes	/*
2031541Srgrimes	 * Validate checksum
2041541Srgrimes	 */
2051541Srgrimes	m->m_data += iphlen;
2061541Srgrimes	m->m_len -= iphlen;
2071541Srgrimes	igmp = mtod(m, struct igmp *);
2081541Srgrimes	if (in_cksum(m, igmplen)) {
2091541Srgrimes		++igmpstat.igps_rcv_badsum;
2101541Srgrimes		m_freem(m);
2111541Srgrimes		return;
2121541Srgrimes	}
2131541Srgrimes	m->m_data -= iphlen;
2141541Srgrimes	m->m_len += iphlen;
2152531Swollman
2161541Srgrimes	ip = mtod(m, struct ip *);
2178546Sdg	timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
21841702Sdillon	if (timer == 0)
21941702Sdillon		timer = 1;
2201541Srgrimes
22114622Sfenner	/*
22214622Sfenner	 * In the IGMPv2 specification, there are 3 states and a flag.
22314622Sfenner	 *
22414622Sfenner	 * In Non-Member state, we simply don't have a membership record.
22514622Sfenner	 * In Delaying Member state, our timer is running (inm->inm_timer)
22614622Sfenner	 * In Idle Member state, our timer is not running (inm->inm_timer==0)
22714622Sfenner	 *
22814622Sfenner	 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
22914622Sfenner	 * we have heard a report from another member, or IGMP_IREPORTEDLAST
23014622Sfenner	 * if I sent the last report.
23114622Sfenner	 */
2321541Srgrimes	switch (igmp->igmp_type) {
23314622Sfenner	case IGMP_MEMBERSHIP_QUERY:
2341541Srgrimes		++igmpstat.igps_rcv_queries;
2351541Srgrimes
2368090Spst		if (ifp->if_flags & IFF_LOOPBACK)
2371541Srgrimes			break;
2381541Srgrimes
2392531Swollman		if (igmp->igmp_code == 0) {
24014622Sfenner			/*
24114622Sfenner			 * Old router.  Remember that the querier on this
24214622Sfenner			 * interface is old, and set the timer to the
24314622Sfenner			 * value in RFC 1112.
24414622Sfenner			 */
2454028Spst
246130333Srwatson			mtx_lock(&igmp_mtx);
247130333Srwatson			rti = find_rti(ifp);
24814622Sfenner			rti->rti_type = IGMP_V1_ROUTER;
24914622Sfenner			rti->rti_time = 0;
250130333Srwatson			mtx_unlock(&igmp_mtx);
2514028Spst
25214622Sfenner			timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ;
2534028Spst
25414622Sfenner			if (ip->ip_dst.s_addr != igmp_all_hosts_group ||
25514622Sfenner			    igmp->igmp_group.s_addr != 0) {
2562531Swollman				++igmpstat.igps_rcv_badqueries;
2572531Swollman				m_freem(m);
2582531Swollman				return;
2592531Swollman			}
26014622Sfenner		} else {
2612531Swollman			/*
26214622Sfenner			 * New router.  Simply do the new validity check.
2632531Swollman			 */
26414622Sfenner
26514622Sfenner			if (igmp->igmp_group.s_addr != 0 &&
26614622Sfenner			    !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
26714622Sfenner				++igmpstat.igps_rcv_badqueries;
26814622Sfenner				m_freem(m);
26914622Sfenner				return;
27014622Sfenner			}
27114622Sfenner		}
2722531Swollman
27314622Sfenner		/*
27414622Sfenner		 * - Start the timers in all of our membership records
27514622Sfenner		 *   that the query applies to for the interface on
27614622Sfenner		 *   which the query arrived excl. those that belong
27714622Sfenner		 *   to the "all-hosts" group (224.0.0.1).
27814622Sfenner		 * - Restart any timer that is already running but has
27914622Sfenner		 *   a value longer than the requested timeout.
28014622Sfenner		 * - Use the value specified in the query message as
28114622Sfenner		 *   the maximum timeout.
28214622Sfenner		 */
28314622Sfenner		IN_FIRST_MULTI(step, inm);
28414622Sfenner		while (inm != NULL) {
28514622Sfenner			if (inm->inm_ifp == ifp &&
28614622Sfenner			    inm->inm_addr.s_addr != igmp_all_hosts_group &&
28714622Sfenner			    (igmp->igmp_group.s_addr == 0 ||
28814622Sfenner			     igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) {
28914622Sfenner				if (inm->inm_timer == 0 ||
29014622Sfenner				    inm->inm_timer > timer) {
29114622Sfenner					inm->inm_timer =
29214622Sfenner						IGMP_RANDOM_DELAY(timer);
2932531Swollman					igmp_timers_are_running = 1;
2942531Swollman				}
2951541Srgrimes			}
2961541Srgrimes			IN_NEXT_MULTI(step, inm);
2971541Srgrimes		}
2989209Swollman
2991541Srgrimes		break;
3001541Srgrimes
30114622Sfenner	case IGMP_V1_MEMBERSHIP_REPORT:
30214622Sfenner	case IGMP_V2_MEMBERSHIP_REPORT:
3039209Swollman		/*
30414622Sfenner		 * For fast leave to work, we have to know that we are the
30514622Sfenner		 * last person to send a report for this group.  Reports
30614622Sfenner		 * can potentially get looped back if we are a multicast
30714622Sfenner		 * router, so discard reports sourced by me.
3089209Swollman		 */
30914622Sfenner		IFP_TO_IA(ifp, ia);
31014622Sfenner		if (ia && ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr)
31114622Sfenner			break;
31214622Sfenner
3131541Srgrimes		++igmpstat.igps_rcv_reports;
3141541Srgrimes
3158090Spst		if (ifp->if_flags & IFF_LOOPBACK)
3161541Srgrimes			break;
3171541Srgrimes
31814622Sfenner		if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
3191541Srgrimes			++igmpstat.igps_rcv_badreports;
3201541Srgrimes			m_freem(m);
3211541Srgrimes			return;
3221541Srgrimes		}
3231541Srgrimes
3241541Srgrimes		/*
3251541Srgrimes		 * KLUDGE: if the IP source address of the report has an
3261541Srgrimes		 * unspecified (i.e., zero) subnet number, as is allowed for
3271541Srgrimes		 * a booting host, replace it with the correct subnet number
32896432Sdd		 * so that a process-level multicast routing daemon can
3291541Srgrimes		 * determine which subnet it arrived from.  This is necessary
3301541Srgrimes		 * to compensate for the lack of any way for a process to
3311541Srgrimes		 * determine the arrival interface of an incoming packet.
3321541Srgrimes		 */
33314622Sfenner		if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0)
3341541Srgrimes			if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
3351541Srgrimes
3361541Srgrimes		/*
3371541Srgrimes		 * If we belong to the group being reported, stop
3381541Srgrimes		 * our timer for that group.
3391541Srgrimes		 */
3401541Srgrimes		IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
3411541Srgrimes
3422531Swollman		if (inm != NULL) {
34314622Sfenner			inm->inm_timer = 0;
34414622Sfenner			++igmpstat.igps_rcv_ourreports;
34514622Sfenner
34614622Sfenner			inm->inm_state = IGMP_OTHERMEMBER;
3472531Swollman		}
34814622Sfenner
3491541Srgrimes		break;
3501541Srgrimes	}
3511541Srgrimes
3521541Srgrimes	/*
3531541Srgrimes	 * Pass all valid IGMP packets up to any process(es) listening
3541541Srgrimes	 * on a raw IGMP socket.
3551541Srgrimes	 */
35682890Sjulian	rip_input(m, off);
3571541Srgrimes}
3581541Srgrimes
3591541Srgrimesvoid
360119181Srwatsonigmp_joingroup(struct in_multi *inm)
3611541Srgrimes{
3629209Swollman	int s = splnet();
3631541Srgrimes
36414622Sfenner	if (inm->inm_addr.s_addr == igmp_all_hosts_group
36514622Sfenner	    || inm->inm_ifp->if_flags & IFF_LOOPBACK) {
3661541Srgrimes		inm->inm_timer = 0;
36714622Sfenner		inm->inm_state = IGMP_OTHERMEMBER;
36814622Sfenner	} else {
369130333Srwatson		mtx_lock(&igmp_mtx);
37014622Sfenner		inm->inm_rti = find_rti(inm->inm_ifp);
371130333Srwatson		mtx_unlock(&igmp_mtx);
37214622Sfenner		igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
3732531Swollman		inm->inm_timer = IGMP_RANDOM_DELAY(
3742531Swollman					IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ);
37514622Sfenner		inm->inm_state = IGMP_IREPORTEDLAST;
3761541Srgrimes		igmp_timers_are_running = 1;
3771541Srgrimes	}
3781541Srgrimes	splx(s);
3791541Srgrimes}
3801541Srgrimes
3811541Srgrimesvoid
382119181Srwatsonigmp_leavegroup(struct in_multi *inm)
3831541Srgrimes{
384119181Srwatson
38514622Sfenner	if (inm->inm_state == IGMP_IREPORTEDLAST &&
38614622Sfenner	    inm->inm_addr.s_addr != igmp_all_hosts_group &&
38714622Sfenner	    !(inm->inm_ifp->if_flags & IFF_LOOPBACK) &&
38814622Sfenner	    inm->inm_rti->rti_type != IGMP_V1_ROUTER)
38914622Sfenner		igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group);
3901541Srgrimes}
3911541Srgrimes
3921541Srgrimesvoid
393119181Srwatsonigmp_fasttimo(void)
3941541Srgrimes{
395107113Sluigi	register struct in_multi *inm;
3961541Srgrimes	struct in_multistep step;
3979209Swollman	int s;
3981541Srgrimes
3991541Srgrimes	/*
4001541Srgrimes	 * Quick check to see if any work needs to be done, in order
4011541Srgrimes	 * to minimize the overhead of fasttimo processing.
4021541Srgrimes	 */
4039209Swollman
4041541Srgrimes	if (!igmp_timers_are_running)
4051541Srgrimes		return;
4061541Srgrimes
4071541Srgrimes	s = splnet();
4081541Srgrimes	igmp_timers_are_running = 0;
4091541Srgrimes	IN_FIRST_MULTI(step, inm);
4101541Srgrimes	while (inm != NULL) {
4111541Srgrimes		if (inm->inm_timer == 0) {
4121541Srgrimes			/* do nothing */
4131541Srgrimes		} else if (--inm->inm_timer == 0) {
41414622Sfenner			igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
41514622Sfenner			inm->inm_state = IGMP_IREPORTEDLAST;
4161541Srgrimes		} else {
4171541Srgrimes			igmp_timers_are_running = 1;
4181541Srgrimes		}
4191541Srgrimes		IN_NEXT_MULTI(step, inm);
4201541Srgrimes	}
4211541Srgrimes	splx(s);
4221541Srgrimes}
4231541Srgrimes
4242531Swollmanvoid
425119181Srwatsonigmp_slowtimo(void)
4262531Swollman{
4272531Swollman	int s = splnet();
428119180Srwatson	struct router_info *rti;
4292531Swollman
430119180Srwatson	IGMP_PRINTF("[igmp.c,_slowtimo] -- > entering \n");
431130333Srwatson	mtx_lock(&igmp_mtx);
432119180Srwatson	SLIST_FOREACH(rti, &router_info_head, rti_list) {
433119181Srwatson		if (rti->rti_type == IGMP_V1_ROUTER) {
434119181Srwatson			rti->rti_time++;
435119181Srwatson			if (rti->rti_time >= IGMP_AGE_THRESHOLD)
436119181Srwatson				rti->rti_type = IGMP_V2_ROUTER;
4372531Swollman		}
4382531Swollman	}
439130333Srwatson	mtx_unlock(&igmp_mtx);
440119180Srwatson	IGMP_PRINTF("[igmp.c,_slowtimo] -- > exiting \n");
4412531Swollman	splx(s);
4422531Swollman}
4432531Swollman
4441541Srgrimesstatic void
445119181Srwatsonigmp_sendpkt(struct in_multi *inm, int type, unsigned long addr)
4461541Srgrimes{
447119181Srwatson	struct mbuf *m;
448119181Srwatson	struct igmp *igmp;
449119181Srwatson	struct ip *ip;
450119181Srwatson	struct ip_moptions imo;
4511541Srgrimes
452119181Srwatson	MGETHDR(m, M_DONTWAIT, MT_HEADER);
453119181Srwatson	if (m == NULL)
454119181Srwatson		return;
4552531Swollman
4568090Spst	m->m_pkthdr.rcvif = loif;
457101091Srwatson#ifdef MAC
458101091Srwatson	mac_create_mbuf_linklayer(inm->inm_ifp, m);
459101091Srwatson#endif
4601541Srgrimes	m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
4612531Swollman	MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
4622531Swollman	m->m_data += sizeof(struct ip);
463119181Srwatson	m->m_len = IGMP_MINLEN;
464119181Srwatson	igmp = mtod(m, struct igmp *);
465119181Srwatson	igmp->igmp_type = type;
466119181Srwatson	igmp->igmp_code = 0;
467119181Srwatson	igmp->igmp_group = inm->inm_addr;
468119181Srwatson	igmp->igmp_cksum = 0;
469119181Srwatson	igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
4701541Srgrimes
471119181Srwatson	m->m_data -= sizeof(struct ip);
472119181Srwatson	m->m_len += sizeof(struct ip);
473119181Srwatson	ip = mtod(m, struct ip *);
474119181Srwatson	ip->ip_tos = 0;
475119181Srwatson	ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
476119181Srwatson	ip->ip_off = 0;
477119181Srwatson	ip->ip_p = IPPROTO_IGMP;
478119181Srwatson	ip->ip_src.s_addr = INADDR_ANY;
479119181Srwatson	ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr;
4801541Srgrimes
481119181Srwatson	imo.imo_multicast_ifp  = inm->inm_ifp;
482119181Srwatson	imo.imo_multicast_ttl  = 1;
48315292Swollman	imo.imo_multicast_vif  = -1;
484119181Srwatson	/*
485119181Srwatson	 * Request loopback of the report if we are acting as a multicast
486119181Srwatson	 * router, so that the process-level routing daemon can hear it.
487119181Srwatson	 */
488119181Srwatson	imo.imo_multicast_loop = (ip_mrouter != NULL);
4891541Srgrimes
49015292Swollman	/*
49115292Swollman	 * XXX
49215292Swollman	 * Do we have to worry about reentrancy here?  Don't think so.
49315292Swollman	 */
494119181Srwatson	ip_output(m, router_alert, &igmprt, 0, &imo, NULL);
4952531Swollman
496119181Srwatson	++igmpstat.igps_snd_reports;
4971541Srgrimes}
498