igmp.c revision 144163
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
3450477Speter * $FreeBSD: head/sys/netinet/igmp.c 144163 2005-03-26 22:20:22Z sam $
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	}
15442776Sfenner	MALLOC(rti, struct router_info *, sizeof *rti, M_IGMP, M_NOWAIT);
155144163Ssam	if (rti == NULL) {
156144163Ssam		IGMP_PRINTF( "[igmp.c, _find_rti] --> no memory for entry\n");
157144163Ssam		return NULL;
158144163Ssam	}
159119181Srwatson	rti->rti_ifp = ifp;
160119181Srwatson	rti->rti_type = IGMP_V2_ROUTER;
161119181Srwatson	rti->rti_time = 0;
162119180Srwatson	SLIST_INSERT_HEAD(&router_info_head, rti, rti_list);
163119180Srwatson
164119180Srwatson	IGMP_PRINTF("[igmp.c, _find_rti] --> created an entry \n");
165119181Srwatson	return rti;
1662531Swollman}
1672531Swollman
1681541Srgrimesvoid
169119181Srwatsonigmp_input(register struct mbuf *m, int off)
1701541Srgrimes{
171107113Sluigi	register int iphlen = off;
172107113Sluigi	register struct igmp *igmp;
173107113Sluigi	register struct ip *ip;
174107113Sluigi	register int igmplen;
175107113Sluigi	register struct ifnet *ifp = m->m_pkthdr.rcvif;
176107113Sluigi	register int minlen;
177107113Sluigi	register struct in_multi *inm;
178107113Sluigi	register struct in_ifaddr *ia;
1791541Srgrimes	struct in_multistep step;
1802531Swollman	struct router_info *rti;
1818546Sdg	int timer; /** timer value in the igmp query header **/
1821541Srgrimes
1831541Srgrimes	++igmpstat.igps_rcv_total;
1841541Srgrimes
1851541Srgrimes	ip = mtod(m, struct ip *);
1861541Srgrimes	igmplen = ip->ip_len;
1871541Srgrimes
1881541Srgrimes	/*
1891541Srgrimes	 * Validate lengths
1901541Srgrimes	 */
1911541Srgrimes	if (igmplen < IGMP_MINLEN) {
1921541Srgrimes		++igmpstat.igps_rcv_tooshort;
1931541Srgrimes		m_freem(m);
1941541Srgrimes		return;
1951541Srgrimes	}
1961541Srgrimes	minlen = iphlen + IGMP_MINLEN;
1971541Srgrimes	if ((m->m_flags & M_EXT || m->m_len < minlen) &&
1981541Srgrimes	    (m = m_pullup(m, minlen)) == 0) {
1991541Srgrimes		++igmpstat.igps_rcv_tooshort;
2001541Srgrimes		return;
2011541Srgrimes	}
2021541Srgrimes
2031541Srgrimes	/*
2041541Srgrimes	 * Validate checksum
2051541Srgrimes	 */
2061541Srgrimes	m->m_data += iphlen;
2071541Srgrimes	m->m_len -= iphlen;
2081541Srgrimes	igmp = mtod(m, struct igmp *);
2091541Srgrimes	if (in_cksum(m, igmplen)) {
2101541Srgrimes		++igmpstat.igps_rcv_badsum;
2111541Srgrimes		m_freem(m);
2121541Srgrimes		return;
2131541Srgrimes	}
2141541Srgrimes	m->m_data -= iphlen;
2151541Srgrimes	m->m_len += iphlen;
2162531Swollman
2171541Srgrimes	ip = mtod(m, struct ip *);
2188546Sdg	timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
21941702Sdillon	if (timer == 0)
22041702Sdillon		timer = 1;
2211541Srgrimes
22214622Sfenner	/*
22314622Sfenner	 * In the IGMPv2 specification, there are 3 states and a flag.
22414622Sfenner	 *
22514622Sfenner	 * In Non-Member state, we simply don't have a membership record.
22614622Sfenner	 * In Delaying Member state, our timer is running (inm->inm_timer)
22714622Sfenner	 * In Idle Member state, our timer is not running (inm->inm_timer==0)
22814622Sfenner	 *
22914622Sfenner	 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
23014622Sfenner	 * we have heard a report from another member, or IGMP_IREPORTEDLAST
23114622Sfenner	 * if I sent the last report.
23214622Sfenner	 */
2331541Srgrimes	switch (igmp->igmp_type) {
23414622Sfenner	case IGMP_MEMBERSHIP_QUERY:
2351541Srgrimes		++igmpstat.igps_rcv_queries;
2361541Srgrimes
2378090Spst		if (ifp->if_flags & IFF_LOOPBACK)
2381541Srgrimes			break;
2391541Srgrimes
2402531Swollman		if (igmp->igmp_code == 0) {
24114622Sfenner			/*
24214622Sfenner			 * Old router.  Remember that the querier on this
24314622Sfenner			 * interface is old, and set the timer to the
24414622Sfenner			 * value in RFC 1112.
24514622Sfenner			 */
2464028Spst
247130333Srwatson			mtx_lock(&igmp_mtx);
248130333Srwatson			rti = find_rti(ifp);
249144163Ssam			if (rti == NULL) {
250144163Ssam				mtx_unlock(&igmp_mtx);
251144163Ssam				m_freem(m);
252144163Ssam				return;
253144163Ssam			}
25414622Sfenner			rti->rti_type = IGMP_V1_ROUTER;
25514622Sfenner			rti->rti_time = 0;
256130333Srwatson			mtx_unlock(&igmp_mtx);
2574028Spst
25814622Sfenner			timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ;
2594028Spst
26014622Sfenner			if (ip->ip_dst.s_addr != igmp_all_hosts_group ||
26114622Sfenner			    igmp->igmp_group.s_addr != 0) {
2622531Swollman				++igmpstat.igps_rcv_badqueries;
2632531Swollman				m_freem(m);
2642531Swollman				return;
2652531Swollman			}
26614622Sfenner		} else {
2672531Swollman			/*
26814622Sfenner			 * New router.  Simply do the new validity check.
2692531Swollman			 */
27014622Sfenner
27114622Sfenner			if (igmp->igmp_group.s_addr != 0 &&
27214622Sfenner			    !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
27314622Sfenner				++igmpstat.igps_rcv_badqueries;
27414622Sfenner				m_freem(m);
27514622Sfenner				return;
27614622Sfenner			}
27714622Sfenner		}
2782531Swollman
27914622Sfenner		/*
28014622Sfenner		 * - Start the timers in all of our membership records
28114622Sfenner		 *   that the query applies to for the interface on
28214622Sfenner		 *   which the query arrived excl. those that belong
28314622Sfenner		 *   to the "all-hosts" group (224.0.0.1).
28414622Sfenner		 * - Restart any timer that is already running but has
28514622Sfenner		 *   a value longer than the requested timeout.
28614622Sfenner		 * - Use the value specified in the query message as
28714622Sfenner		 *   the maximum timeout.
28814622Sfenner		 */
28914622Sfenner		IN_FIRST_MULTI(step, inm);
29014622Sfenner		while (inm != NULL) {
29114622Sfenner			if (inm->inm_ifp == ifp &&
29214622Sfenner			    inm->inm_addr.s_addr != igmp_all_hosts_group &&
29314622Sfenner			    (igmp->igmp_group.s_addr == 0 ||
29414622Sfenner			     igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) {
29514622Sfenner				if (inm->inm_timer == 0 ||
29614622Sfenner				    inm->inm_timer > timer) {
29714622Sfenner					inm->inm_timer =
29814622Sfenner						IGMP_RANDOM_DELAY(timer);
2992531Swollman					igmp_timers_are_running = 1;
3002531Swollman				}
3011541Srgrimes			}
3021541Srgrimes			IN_NEXT_MULTI(step, inm);
3031541Srgrimes		}
3049209Swollman
3051541Srgrimes		break;
3061541Srgrimes
30714622Sfenner	case IGMP_V1_MEMBERSHIP_REPORT:
30814622Sfenner	case IGMP_V2_MEMBERSHIP_REPORT:
3099209Swollman		/*
31014622Sfenner		 * For fast leave to work, we have to know that we are the
31114622Sfenner		 * last person to send a report for this group.  Reports
31214622Sfenner		 * can potentially get looped back if we are a multicast
31314622Sfenner		 * router, so discard reports sourced by me.
3149209Swollman		 */
31514622Sfenner		IFP_TO_IA(ifp, ia);
31614622Sfenner		if (ia && ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr)
31714622Sfenner			break;
31814622Sfenner
3191541Srgrimes		++igmpstat.igps_rcv_reports;
3201541Srgrimes
3218090Spst		if (ifp->if_flags & IFF_LOOPBACK)
3221541Srgrimes			break;
3231541Srgrimes
32414622Sfenner		if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
3251541Srgrimes			++igmpstat.igps_rcv_badreports;
3261541Srgrimes			m_freem(m);
3271541Srgrimes			return;
3281541Srgrimes		}
3291541Srgrimes
3301541Srgrimes		/*
3311541Srgrimes		 * KLUDGE: if the IP source address of the report has an
3321541Srgrimes		 * unspecified (i.e., zero) subnet number, as is allowed for
3331541Srgrimes		 * a booting host, replace it with the correct subnet number
33496432Sdd		 * so that a process-level multicast routing daemon can
3351541Srgrimes		 * determine which subnet it arrived from.  This is necessary
3361541Srgrimes		 * to compensate for the lack of any way for a process to
3371541Srgrimes		 * determine the arrival interface of an incoming packet.
3381541Srgrimes		 */
33914622Sfenner		if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0)
3401541Srgrimes			if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
3411541Srgrimes
3421541Srgrimes		/*
3431541Srgrimes		 * If we belong to the group being reported, stop
3441541Srgrimes		 * our timer for that group.
3451541Srgrimes		 */
3461541Srgrimes		IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
3471541Srgrimes
3482531Swollman		if (inm != NULL) {
34914622Sfenner			inm->inm_timer = 0;
35014622Sfenner			++igmpstat.igps_rcv_ourreports;
35114622Sfenner
35214622Sfenner			inm->inm_state = IGMP_OTHERMEMBER;
3532531Swollman		}
35414622Sfenner
3551541Srgrimes		break;
3561541Srgrimes	}
3571541Srgrimes
3581541Srgrimes	/*
3591541Srgrimes	 * Pass all valid IGMP packets up to any process(es) listening
3601541Srgrimes	 * on a raw IGMP socket.
3611541Srgrimes	 */
36282890Sjulian	rip_input(m, off);
3631541Srgrimes}
3641541Srgrimes
3651541Srgrimesvoid
366119181Srwatsonigmp_joingroup(struct in_multi *inm)
3671541Srgrimes{
3689209Swollman	int s = splnet();
3691541Srgrimes
37014622Sfenner	if (inm->inm_addr.s_addr == igmp_all_hosts_group
37114622Sfenner	    || inm->inm_ifp->if_flags & IFF_LOOPBACK) {
3721541Srgrimes		inm->inm_timer = 0;
37314622Sfenner		inm->inm_state = IGMP_OTHERMEMBER;
37414622Sfenner	} else {
375130333Srwatson		mtx_lock(&igmp_mtx);
37614622Sfenner		inm->inm_rti = find_rti(inm->inm_ifp);
377130333Srwatson		mtx_unlock(&igmp_mtx);
378144163Ssam		if (inm->inm_rti != NULL) {
379144163Ssam			igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
380144163Ssam			inm->inm_timer = IGMP_RANDOM_DELAY(
3812531Swollman					IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ);
382144163Ssam			inm->inm_state = IGMP_IREPORTEDLAST;
383144163Ssam			igmp_timers_are_running = 1;
384144163Ssam		}
385144163Ssam		/* XXX handling of failure case? */
3861541Srgrimes	}
3871541Srgrimes	splx(s);
3881541Srgrimes}
3891541Srgrimes
3901541Srgrimesvoid
391119181Srwatsonigmp_leavegroup(struct in_multi *inm)
3921541Srgrimes{
393119181Srwatson
39414622Sfenner	if (inm->inm_state == IGMP_IREPORTEDLAST &&
39514622Sfenner	    inm->inm_addr.s_addr != igmp_all_hosts_group &&
39614622Sfenner	    !(inm->inm_ifp->if_flags & IFF_LOOPBACK) &&
39714622Sfenner	    inm->inm_rti->rti_type != IGMP_V1_ROUTER)
39814622Sfenner		igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group);
3991541Srgrimes}
4001541Srgrimes
4011541Srgrimesvoid
402119181Srwatsonigmp_fasttimo(void)
4031541Srgrimes{
404107113Sluigi	register struct in_multi *inm;
4051541Srgrimes	struct in_multistep step;
4069209Swollman	int s;
4071541Srgrimes
4081541Srgrimes	/*
4091541Srgrimes	 * Quick check to see if any work needs to be done, in order
4101541Srgrimes	 * to minimize the overhead of fasttimo processing.
4111541Srgrimes	 */
4129209Swollman
4131541Srgrimes	if (!igmp_timers_are_running)
4141541Srgrimes		return;
4151541Srgrimes
4161541Srgrimes	s = splnet();
4171541Srgrimes	igmp_timers_are_running = 0;
4181541Srgrimes	IN_FIRST_MULTI(step, inm);
4191541Srgrimes	while (inm != NULL) {
4201541Srgrimes		if (inm->inm_timer == 0) {
4211541Srgrimes			/* do nothing */
4221541Srgrimes		} else if (--inm->inm_timer == 0) {
42314622Sfenner			igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
42414622Sfenner			inm->inm_state = IGMP_IREPORTEDLAST;
4251541Srgrimes		} else {
4261541Srgrimes			igmp_timers_are_running = 1;
4271541Srgrimes		}
4281541Srgrimes		IN_NEXT_MULTI(step, inm);
4291541Srgrimes	}
4301541Srgrimes	splx(s);
4311541Srgrimes}
4321541Srgrimes
4332531Swollmanvoid
434119181Srwatsonigmp_slowtimo(void)
4352531Swollman{
4362531Swollman	int s = splnet();
437119180Srwatson	struct router_info *rti;
4382531Swollman
439119180Srwatson	IGMP_PRINTF("[igmp.c,_slowtimo] -- > entering \n");
440130333Srwatson	mtx_lock(&igmp_mtx);
441119180Srwatson	SLIST_FOREACH(rti, &router_info_head, rti_list) {
442119181Srwatson		if (rti->rti_type == IGMP_V1_ROUTER) {
443119181Srwatson			rti->rti_time++;
444119181Srwatson			if (rti->rti_time >= IGMP_AGE_THRESHOLD)
445119181Srwatson				rti->rti_type = IGMP_V2_ROUTER;
4462531Swollman		}
4472531Swollman	}
448130333Srwatson	mtx_unlock(&igmp_mtx);
449119180Srwatson	IGMP_PRINTF("[igmp.c,_slowtimo] -- > exiting \n");
4502531Swollman	splx(s);
4512531Swollman}
4522531Swollman
4531541Srgrimesstatic void
454119181Srwatsonigmp_sendpkt(struct in_multi *inm, int type, unsigned long addr)
4551541Srgrimes{
456119181Srwatson	struct mbuf *m;
457119181Srwatson	struct igmp *igmp;
458119181Srwatson	struct ip *ip;
459119181Srwatson	struct ip_moptions imo;
4601541Srgrimes
461119181Srwatson	MGETHDR(m, M_DONTWAIT, MT_HEADER);
462119181Srwatson	if (m == NULL)
463119181Srwatson		return;
4642531Swollman
4658090Spst	m->m_pkthdr.rcvif = loif;
466101091Srwatson#ifdef MAC
467101091Srwatson	mac_create_mbuf_linklayer(inm->inm_ifp, m);
468101091Srwatson#endif
4691541Srgrimes	m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
4702531Swollman	MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
4712531Swollman	m->m_data += sizeof(struct ip);
472119181Srwatson	m->m_len = IGMP_MINLEN;
473119181Srwatson	igmp = mtod(m, struct igmp *);
474119181Srwatson	igmp->igmp_type = type;
475119181Srwatson	igmp->igmp_code = 0;
476119181Srwatson	igmp->igmp_group = inm->inm_addr;
477119181Srwatson	igmp->igmp_cksum = 0;
478119181Srwatson	igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
4791541Srgrimes
480119181Srwatson	m->m_data -= sizeof(struct ip);
481119181Srwatson	m->m_len += sizeof(struct ip);
482119181Srwatson	ip = mtod(m, struct ip *);
483119181Srwatson	ip->ip_tos = 0;
484119181Srwatson	ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
485119181Srwatson	ip->ip_off = 0;
486119181Srwatson	ip->ip_p = IPPROTO_IGMP;
487119181Srwatson	ip->ip_src.s_addr = INADDR_ANY;
488119181Srwatson	ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr;
4891541Srgrimes
490119181Srwatson	imo.imo_multicast_ifp  = inm->inm_ifp;
491119181Srwatson	imo.imo_multicast_ttl  = 1;
49215292Swollman	imo.imo_multicast_vif  = -1;
493119181Srwatson	/*
494119181Srwatson	 * Request loopback of the report if we are acting as a multicast
495119181Srwatson	 * router, so that the process-level routing daemon can hear it.
496119181Srwatson	 */
497119181Srwatson	imo.imo_multicast_loop = (ip_mrouter != NULL);
4981541Srgrimes
49915292Swollman	/*
50015292Swollman	 * XXX
50115292Swollman	 * Do we have to worry about reentrancy here?  Don't think so.
50215292Swollman	 */
503119181Srwatson	ip_output(m, router_alert, &igmprt, 0, &imo, NULL);
5042531Swollman
505119181Srwatson	++igmpstat.igps_snd_reports;
5061541Srgrimes}
507