igmp.c revision 82890
1/*
2 * Copyright (c) 1988 Stephen Deering.
3 * Copyright (c) 1992, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Stephen Deering of Stanford University.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed by the University of
20 *	California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 *	@(#)igmp.c	8.1 (Berkeley) 7/19/93
38 * $FreeBSD: head/sys/netinet/igmp.c 82890 2001-09-03 20:40:35Z julian $
39 */
40
41/*
42 * Internet Group Management Protocol (IGMP) routines.
43 *
44 * Written by Steve Deering, Stanford, May 1988.
45 * Modified by Rosen Sharma, Stanford, Aug 1994.
46 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
47 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
48 *
49 * MULTICAST Revision: 3.5.1.4
50 */
51
52#include <sys/param.h>
53#include <sys/systm.h>
54#include <sys/malloc.h>
55#include <sys/mbuf.h>
56#include <sys/socket.h>
57#include <sys/protosw.h>
58#include <sys/kernel.h>
59#include <sys/sysctl.h>
60
61#include <net/if.h>
62#include <net/route.h>
63
64#include <netinet/in.h>
65#include <netinet/in_var.h>
66#include <netinet/in_systm.h>
67#include <netinet/ip.h>
68#include <netinet/ip_var.h>
69#include <netinet/igmp.h>
70#include <netinet/igmp_var.h>
71
72#include <machine/in_cksum.h>
73
74static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
75
76static struct router_info *
77		find_rti __P((struct ifnet *ifp));
78
79static struct igmpstat igmpstat;
80
81SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW,
82	&igmpstat, igmpstat, "");
83
84static int igmp_timers_are_running;
85static u_long igmp_all_hosts_group;
86static u_long igmp_all_rtrs_group;
87static struct mbuf *router_alert;
88static struct router_info *Head;
89
90static void igmp_sendpkt __P((struct in_multi *, int, unsigned long));
91
92void
93igmp_init()
94{
95	struct ipoption *ra;
96
97	/*
98	 * To avoid byte-swapping the same value over and over again.
99	 */
100	igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
101	igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP);
102
103	igmp_timers_are_running = 0;
104
105	/*
106	 * Construct a Router Alert option to use in outgoing packets
107	 */
108	MGET(router_alert, M_DONTWAIT, MT_DATA);
109	ra = mtod(router_alert, struct ipoption *);
110	ra->ipopt_dst.s_addr = 0;
111	ra->ipopt_list[0] = IPOPT_RA;	/* Router Alert Option */
112	ra->ipopt_list[1] = 0x04;	/* 4 bytes long */
113	ra->ipopt_list[2] = 0x00;
114	ra->ipopt_list[3] = 0x00;
115	router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1];
116
117	Head = (struct router_info *) 0;
118}
119
120static struct router_info *
121find_rti(ifp)
122	struct ifnet *ifp;
123{
124        register struct router_info *rti = Head;
125
126#ifdef IGMP_DEBUG
127	printf("[igmp.c, _find_rti] --> entering \n");
128#endif
129        while (rti) {
130                if (rti->rti_ifp == ifp) {
131#ifdef IGMP_DEBUG
132			printf("[igmp.c, _find_rti] --> found old entry \n");
133#endif
134                        return rti;
135                }
136                rti = rti->rti_next;
137        }
138	MALLOC(rti, struct router_info *, sizeof *rti, M_IGMP, M_NOWAIT);
139        rti->rti_ifp = ifp;
140        rti->rti_type = IGMP_V2_ROUTER;
141        rti->rti_time = 0;
142        rti->rti_next = Head;
143        Head = rti;
144#ifdef IGMP_DEBUG
145	printf("[igmp.c, _find_rti] --> created an entry \n");
146#endif
147        return rti;
148}
149
150void
151igmp_input(m, off)
152	register struct mbuf *m;
153	int off;
154{
155	register int iphlen = off;
156	register struct igmp *igmp;
157	register struct ip *ip;
158	register int igmplen;
159	register struct ifnet *ifp = m->m_pkthdr.rcvif;
160	register int minlen;
161	register struct in_multi *inm;
162	register struct in_ifaddr *ia;
163	struct in_multistep step;
164	struct router_info *rti;
165
166	int timer; /** timer value in the igmp query header **/
167
168	++igmpstat.igps_rcv_total;
169
170	ip = mtod(m, struct ip *);
171	igmplen = ip->ip_len;
172
173	/*
174	 * Validate lengths
175	 */
176	if (igmplen < IGMP_MINLEN) {
177		++igmpstat.igps_rcv_tooshort;
178		m_freem(m);
179		return;
180	}
181	minlen = iphlen + IGMP_MINLEN;
182	if ((m->m_flags & M_EXT || m->m_len < minlen) &&
183	    (m = m_pullup(m, minlen)) == 0) {
184		++igmpstat.igps_rcv_tooshort;
185		return;
186	}
187
188	/*
189	 * Validate checksum
190	 */
191	m->m_data += iphlen;
192	m->m_len -= iphlen;
193	igmp = mtod(m, struct igmp *);
194	if (in_cksum(m, igmplen)) {
195		++igmpstat.igps_rcv_badsum;
196		m_freem(m);
197		return;
198	}
199	m->m_data -= iphlen;
200	m->m_len += iphlen;
201
202	ip = mtod(m, struct ip *);
203	timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
204	if (timer == 0)
205		timer = 1;
206	rti = find_rti(ifp);
207
208	/*
209	 * In the IGMPv2 specification, there are 3 states and a flag.
210	 *
211	 * In Non-Member state, we simply don't have a membership record.
212	 * In Delaying Member state, our timer is running (inm->inm_timer)
213	 * In Idle Member state, our timer is not running (inm->inm_timer==0)
214	 *
215	 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
216	 * we have heard a report from another member, or IGMP_IREPORTEDLAST
217	 * if I sent the last report.
218	 */
219	switch (igmp->igmp_type) {
220
221	case IGMP_MEMBERSHIP_QUERY:
222		++igmpstat.igps_rcv_queries;
223
224		if (ifp->if_flags & IFF_LOOPBACK)
225			break;
226
227		if (igmp->igmp_code == 0) {
228			/*
229			 * Old router.  Remember that the querier on this
230			 * interface is old, and set the timer to the
231			 * value in RFC 1112.
232			 */
233
234			rti->rti_type = IGMP_V1_ROUTER;
235			rti->rti_time = 0;
236
237			timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ;
238
239			if (ip->ip_dst.s_addr != igmp_all_hosts_group ||
240			    igmp->igmp_group.s_addr != 0) {
241				++igmpstat.igps_rcv_badqueries;
242				m_freem(m);
243				return;
244			}
245		} else {
246			/*
247			 * New router.  Simply do the new validity check.
248			 */
249
250			if (igmp->igmp_group.s_addr != 0 &&
251			    !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
252				++igmpstat.igps_rcv_badqueries;
253				m_freem(m);
254				return;
255			}
256		}
257
258		/*
259		 * - Start the timers in all of our membership records
260		 *   that the query applies to for the interface on
261		 *   which the query arrived excl. those that belong
262		 *   to the "all-hosts" group (224.0.0.1).
263		 * - Restart any timer that is already running but has
264		 *   a value longer than the requested timeout.
265		 * - Use the value specified in the query message as
266		 *   the maximum timeout.
267		 */
268		IN_FIRST_MULTI(step, inm);
269		while (inm != NULL) {
270			if (inm->inm_ifp == ifp &&
271			    inm->inm_addr.s_addr != igmp_all_hosts_group &&
272			    (igmp->igmp_group.s_addr == 0 ||
273			     igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) {
274				if (inm->inm_timer == 0 ||
275				    inm->inm_timer > timer) {
276					inm->inm_timer =
277						IGMP_RANDOM_DELAY(timer);
278					igmp_timers_are_running = 1;
279				}
280			}
281			IN_NEXT_MULTI(step, inm);
282		}
283
284		break;
285
286	case IGMP_V1_MEMBERSHIP_REPORT:
287	case IGMP_V2_MEMBERSHIP_REPORT:
288		/*
289		 * For fast leave to work, we have to know that we are the
290		 * last person to send a report for this group.  Reports
291		 * can potentially get looped back if we are a multicast
292		 * router, so discard reports sourced by me.
293		 */
294		IFP_TO_IA(ifp, ia);
295		if (ia && ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr)
296			break;
297
298		++igmpstat.igps_rcv_reports;
299
300		if (ifp->if_flags & IFF_LOOPBACK)
301			break;
302
303		if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
304			++igmpstat.igps_rcv_badreports;
305			m_freem(m);
306			return;
307		}
308
309		/*
310		 * KLUDGE: if the IP source address of the report has an
311		 * unspecified (i.e., zero) subnet number, as is allowed for
312		 * a booting host, replace it with the correct subnet number
313		 * so that a process-level multicast routing demon can
314		 * determine which subnet it arrived from.  This is necessary
315		 * to compensate for the lack of any way for a process to
316		 * determine the arrival interface of an incoming packet.
317		 */
318		if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0)
319			if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
320
321		/*
322		 * If we belong to the group being reported, stop
323		 * our timer for that group.
324		 */
325		IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
326
327		if (inm != NULL) {
328			inm->inm_timer = 0;
329			++igmpstat.igps_rcv_ourreports;
330
331			inm->inm_state = IGMP_OTHERMEMBER;
332		}
333
334		break;
335	}
336
337	/*
338	 * Pass all valid IGMP packets up to any process(es) listening
339	 * on a raw IGMP socket.
340	 */
341	rip_input(m, off);
342}
343
344void
345igmp_joingroup(inm)
346	struct in_multi *inm;
347{
348	int s = splnet();
349
350	if (inm->inm_addr.s_addr == igmp_all_hosts_group
351	    || inm->inm_ifp->if_flags & IFF_LOOPBACK) {
352		inm->inm_timer = 0;
353		inm->inm_state = IGMP_OTHERMEMBER;
354	} else {
355		inm->inm_rti = find_rti(inm->inm_ifp);
356		igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
357		inm->inm_timer = IGMP_RANDOM_DELAY(
358					IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ);
359		inm->inm_state = IGMP_IREPORTEDLAST;
360		igmp_timers_are_running = 1;
361	}
362	splx(s);
363}
364
365void
366igmp_leavegroup(inm)
367	struct in_multi *inm;
368{
369	if (inm->inm_state == IGMP_IREPORTEDLAST &&
370	    inm->inm_addr.s_addr != igmp_all_hosts_group &&
371	    !(inm->inm_ifp->if_flags & IFF_LOOPBACK) &&
372	    inm->inm_rti->rti_type != IGMP_V1_ROUTER)
373		igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group);
374}
375
376void
377igmp_fasttimo()
378{
379	register struct in_multi *inm;
380	struct in_multistep step;
381	int s;
382
383	/*
384	 * Quick check to see if any work needs to be done, in order
385	 * to minimize the overhead of fasttimo processing.
386	 */
387
388	if (!igmp_timers_are_running)
389		return;
390
391	s = splnet();
392	igmp_timers_are_running = 0;
393	IN_FIRST_MULTI(step, inm);
394	while (inm != NULL) {
395		if (inm->inm_timer == 0) {
396			/* do nothing */
397		} else if (--inm->inm_timer == 0) {
398			igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
399			inm->inm_state = IGMP_IREPORTEDLAST;
400		} else {
401			igmp_timers_are_running = 1;
402		}
403		IN_NEXT_MULTI(step, inm);
404	}
405	splx(s);
406}
407
408void
409igmp_slowtimo()
410{
411	int s = splnet();
412	register struct router_info *rti =  Head;
413
414#ifdef IGMP_DEBUG
415	printf("[igmp.c,_slowtimo] -- > entering \n");
416#endif
417	while (rti) {
418	    if (rti->rti_type == IGMP_V1_ROUTER) {
419		rti->rti_time++;
420		if (rti->rti_time >= IGMP_AGE_THRESHOLD) {
421			rti->rti_type = IGMP_V2_ROUTER;
422		}
423	    }
424	    rti = rti->rti_next;
425	}
426#ifdef IGMP_DEBUG
427	printf("[igmp.c,_slowtimo] -- > exiting \n");
428#endif
429	splx(s);
430}
431
432static struct route igmprt;
433
434static void
435igmp_sendpkt(inm, type, addr)
436	struct in_multi *inm;
437	int type;
438	unsigned long addr;
439{
440        struct mbuf *m;
441        struct igmp *igmp;
442        struct ip *ip;
443        struct ip_moptions imo;
444
445        MGETHDR(m, M_DONTWAIT, MT_HEADER);
446        if (m == NULL)
447                return;
448
449	m->m_pkthdr.rcvif = loif;
450	m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
451	MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
452	m->m_data += sizeof(struct ip);
453        m->m_len = IGMP_MINLEN;
454        igmp = mtod(m, struct igmp *);
455        igmp->igmp_type   = type;
456        igmp->igmp_code   = 0;
457        igmp->igmp_group  = inm->inm_addr;
458        igmp->igmp_cksum  = 0;
459        igmp->igmp_cksum  = in_cksum(m, IGMP_MINLEN);
460
461        m->m_data -= sizeof(struct ip);
462        m->m_len += sizeof(struct ip);
463        ip = mtod(m, struct ip *);
464        ip->ip_tos        = 0;
465        ip->ip_len        = sizeof(struct ip) + IGMP_MINLEN;
466        ip->ip_off        = 0;
467        ip->ip_p          = IPPROTO_IGMP;
468        ip->ip_src.s_addr = INADDR_ANY;
469        ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr;
470
471        imo.imo_multicast_ifp  = inm->inm_ifp;
472        imo.imo_multicast_ttl  = 1;
473	imo.imo_multicast_vif  = -1;
474        /*
475         * Request loopback of the report if we are acting as a multicast
476         * router, so that the process-level routing demon can hear it.
477         */
478        imo.imo_multicast_loop = (ip_mrouter != NULL);
479
480	/*
481	 * XXX
482	 * Do we have to worry about reentrancy here?  Don't think so.
483	 */
484        ip_output(m, router_alert, &igmprt, 0, &imo);
485
486        ++igmpstat.igps_snd_reports;
487}
488