xref: /freebsd/sys/netinet/igmp.c (revision 05c7a37afb48ddd5ee1bd921a5d46fe59cc70b15)
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  * $Id: igmp.c,v 1.16 1996/03/14 16:59:16 fenner Exp $
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/mbuf.h>
55 #include <sys/socket.h>
56 #include <sys/protosw.h>
57 #include <sys/kernel.h>
58 #include <sys/sysctl.h>
59 
60 #include <net/if.h>
61 #include <net/route.h>
62 
63 #include <netinet/in.h>
64 #include <netinet/in_var.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/igmp.h>
69 #include <netinet/igmp_var.h>
70 
71 static int	fill_rti __P((struct in_multi *inm));
72 static struct router_info *
73 		find_rti __P((struct ifnet *ifp));
74 
75 static struct igmpstat igmpstat;
76 
77 SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RD,
78 	&igmpstat, igmpstat, "");
79 
80 static int igmp_timers_are_running;
81 static u_long igmp_all_hosts_group;
82 static u_long igmp_all_rtrs_group;
83 static struct mbuf *router_alert;
84 static struct router_info *Head;
85 
86 static void igmp_sendpkt(struct in_multi *, int, unsigned long);
87 
88 void
89 igmp_init()
90 {
91 	struct ipoption *ra;
92 
93 	/*
94 	 * To avoid byte-swapping the same value over and over again.
95 	 */
96 	igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
97 	igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP);
98 
99 	igmp_timers_are_running = 0;
100 
101 	/*
102 	 * Construct a Router Alert option to use in outgoing packets
103 	 */
104 	MGET(router_alert, M_DONTWAIT, MT_DATA);
105 	ra = mtod(router_alert, struct ipoption *);
106 	ra->ipopt_dst.s_addr = 0;
107 	ra->ipopt_list[0] = IPOPT_RA;	/* Router Alert Option */
108 	ra->ipopt_list[1] = 0x04;	/* 4 bytes long */
109 	ra->ipopt_list[2] = 0x00;
110 	ra->ipopt_list[3] = 0x00;
111 	router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1];
112 
113 	Head = (struct router_info *) 0;
114 }
115 
116 static struct router_info *
117 find_rti(ifp)
118 	struct ifnet *ifp;
119 {
120         register struct router_info *rti = Head;
121 
122 #ifdef IGMP_DEBUG
123 	printf("[igmp.c, _find_rti] --> entering \n");
124 #endif
125         while (rti) {
126                 if (rti->rti_ifp == ifp) {
127 #ifdef IGMP_DEBUG
128 			printf("[igmp.c, _find_rti] --> found old entry \n");
129 #endif
130                         return rti;
131                 }
132                 rti = rti->rti_next;
133         }
134 	MALLOC(rti, struct router_info *, sizeof *rti, M_MRTABLE, M_NOWAIT);
135         rti->rti_ifp = ifp;
136         rti->rti_type = IGMP_V2_ROUTER;
137         rti->rti_time = 0;
138         rti->rti_next = Head;
139         Head = rti;
140 #ifdef IGMP_DEBUG
141 	printf("[igmp.c, _find_rti] --> created an entry \n");
142 #endif
143         return rti;
144 }
145 
146 void
147 igmp_input(m, iphlen)
148 	register struct mbuf *m;
149 	register int iphlen;
150 {
151 	register struct igmp *igmp;
152 	register struct ip *ip;
153 	register int igmplen;
154 	register struct ifnet *ifp = m->m_pkthdr.rcvif;
155 	register int minlen;
156 	register struct in_multi *inm;
157 	register struct in_ifaddr *ia;
158 	struct in_multistep step;
159 	struct router_info *rti;
160 
161 	int timer; /** timer value in the igmp query header **/
162 
163 	++igmpstat.igps_rcv_total;
164 
165 	ip = mtod(m, struct ip *);
166 	igmplen = ip->ip_len;
167 
168 	/*
169 	 * Validate lengths
170 	 */
171 	if (igmplen < IGMP_MINLEN) {
172 		++igmpstat.igps_rcv_tooshort;
173 		m_freem(m);
174 		return;
175 	}
176 	minlen = iphlen + IGMP_MINLEN;
177 	if ((m->m_flags & M_EXT || m->m_len < minlen) &&
178 	    (m = m_pullup(m, minlen)) == 0) {
179 		++igmpstat.igps_rcv_tooshort;
180 		return;
181 	}
182 
183 	/*
184 	 * Validate checksum
185 	 */
186 	m->m_data += iphlen;
187 	m->m_len -= iphlen;
188 	igmp = mtod(m, struct igmp *);
189 	if (in_cksum(m, igmplen)) {
190 		++igmpstat.igps_rcv_badsum;
191 		m_freem(m);
192 		return;
193 	}
194 	m->m_data -= iphlen;
195 	m->m_len += iphlen;
196 
197 	ip = mtod(m, struct ip *);
198 	timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
199 	rti = find_rti(ifp);
200 
201 	/*
202 	 * In the IGMPv2 specification, there are 3 states and a flag.
203 	 *
204 	 * In Non-Member state, we simply don't have a membership record.
205 	 * In Delaying Member state, our timer is running (inm->inm_timer)
206 	 * In Idle Member state, our timer is not running (inm->inm_timer==0)
207 	 *
208 	 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
209 	 * we have heard a report from another member, or IGMP_IREPORTEDLAST
210 	 * if I sent the last report.
211 	 */
212 	switch (igmp->igmp_type) {
213 
214 	case IGMP_MEMBERSHIP_QUERY:
215 		++igmpstat.igps_rcv_queries;
216 
217 		if (ifp->if_flags & IFF_LOOPBACK)
218 			break;
219 
220 		if (igmp->igmp_code == 0) {
221 			/*
222 			 * Old router.  Remember that the querier on this
223 			 * interface is old, and set the timer to the
224 			 * value in RFC 1112.
225 			 */
226 
227 			rti->rti_type = IGMP_V1_ROUTER;
228 			rti->rti_time = 0;
229 
230 			timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ;
231 
232 			if (ip->ip_dst.s_addr != igmp_all_hosts_group ||
233 			    igmp->igmp_group.s_addr != 0) {
234 				++igmpstat.igps_rcv_badqueries;
235 				m_freem(m);
236 				return;
237 			}
238 		} else {
239 			/*
240 			 * New router.  Simply do the new validity check.
241 			 */
242 
243 			if (igmp->igmp_group.s_addr != 0 &&
244 			    !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
245 				++igmpstat.igps_rcv_badqueries;
246 				m_freem(m);
247 				return;
248 			}
249 		}
250 
251 		/*
252 		 * - Start the timers in all of our membership records
253 		 *   that the query applies to for the interface on
254 		 *   which the query arrived excl. those that belong
255 		 *   to the "all-hosts" group (224.0.0.1).
256 		 * - Restart any timer that is already running but has
257 		 *   a value longer than the requested timeout.
258 		 * - Use the value specified in the query message as
259 		 *   the maximum timeout.
260 		 */
261 		IN_FIRST_MULTI(step, inm);
262 		while (inm != NULL) {
263 			if (inm->inm_ifp == ifp &&
264 			    inm->inm_addr.s_addr != igmp_all_hosts_group &&
265 			    (igmp->igmp_group.s_addr == 0 ||
266 			     igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) {
267 				if (inm->inm_timer == 0 ||
268 				    inm->inm_timer > timer) {
269 					inm->inm_timer =
270 						IGMP_RANDOM_DELAY(timer);
271 					igmp_timers_are_running = 1;
272 				}
273 			}
274 			IN_NEXT_MULTI(step, inm);
275 		}
276 
277 		break;
278 
279 	case IGMP_V1_MEMBERSHIP_REPORT:
280 	case IGMP_V2_MEMBERSHIP_REPORT:
281 		/*
282 		 * For fast leave to work, we have to know that we are the
283 		 * last person to send a report for this group.  Reports
284 		 * can potentially get looped back if we are a multicast
285 		 * router, so discard reports sourced by me.
286 		 */
287 		IFP_TO_IA(ifp, ia);
288 		if (ia && ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr)
289 			break;
290 
291 		++igmpstat.igps_rcv_reports;
292 
293 		if (ifp->if_flags & IFF_LOOPBACK)
294 			break;
295 
296 		if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
297 			++igmpstat.igps_rcv_badreports;
298 			m_freem(m);
299 			return;
300 		}
301 
302 		/*
303 		 * KLUDGE: if the IP source address of the report has an
304 		 * unspecified (i.e., zero) subnet number, as is allowed for
305 		 * a booting host, replace it with the correct subnet number
306 		 * so that a process-level multicast routing demon can
307 		 * determine which subnet it arrived from.  This is necessary
308 		 * to compensate for the lack of any way for a process to
309 		 * determine the arrival interface of an incoming packet.
310 		 */
311 		if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0)
312 			if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
313 
314 		/*
315 		 * If we belong to the group being reported, stop
316 		 * our timer for that group.
317 		 */
318 		IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
319 
320 		if (inm != NULL) {
321 			inm->inm_timer = 0;
322 			++igmpstat.igps_rcv_ourreports;
323 
324 			inm->inm_state = IGMP_OTHERMEMBER;
325 		}
326 
327 		break;
328 	}
329 
330 	/*
331 	 * Pass all valid IGMP packets up to any process(es) listening
332 	 * on a raw IGMP socket.
333 	 */
334 	rip_input(m, iphlen);
335 }
336 
337 void
338 igmp_joingroup(inm)
339 	struct in_multi *inm;
340 {
341 	int s = splnet();
342 
343 	if (inm->inm_addr.s_addr == igmp_all_hosts_group
344 	    || inm->inm_ifp->if_flags & IFF_LOOPBACK) {
345 		inm->inm_timer = 0;
346 		inm->inm_state = IGMP_OTHERMEMBER;
347 	} else {
348 		inm->inm_rti = find_rti(inm->inm_ifp);
349 		igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
350 		inm->inm_timer = IGMP_RANDOM_DELAY(
351 					IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ);
352 		inm->inm_state = IGMP_IREPORTEDLAST;
353 		igmp_timers_are_running = 1;
354 	}
355 	splx(s);
356 }
357 
358 void
359 igmp_leavegroup(inm)
360 	struct in_multi *inm;
361 {
362 	if (inm->inm_state == IGMP_IREPORTEDLAST &&
363 	    inm->inm_addr.s_addr != igmp_all_hosts_group &&
364 	    !(inm->inm_ifp->if_flags & IFF_LOOPBACK) &&
365 	    inm->inm_rti->rti_type != IGMP_V1_ROUTER)
366 		igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group);
367 }
368 
369 void
370 igmp_fasttimo()
371 {
372 	register struct in_multi *inm;
373 	struct in_multistep step;
374 	int s;
375 
376 	/*
377 	 * Quick check to see if any work needs to be done, in order
378 	 * to minimize the overhead of fasttimo processing.
379 	 */
380 
381 	if (!igmp_timers_are_running)
382 		return;
383 
384 	s = splnet();
385 	igmp_timers_are_running = 0;
386 	IN_FIRST_MULTI(step, inm);
387 	while (inm != NULL) {
388 		if (inm->inm_timer == 0) {
389 			/* do nothing */
390 		} else if (--inm->inm_timer == 0) {
391 			igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
392 			inm->inm_state = IGMP_IREPORTEDLAST;
393 		} else {
394 			igmp_timers_are_running = 1;
395 		}
396 		IN_NEXT_MULTI(step, inm);
397 	}
398 	splx(s);
399 }
400 
401 void
402 igmp_slowtimo()
403 {
404 	int s = splnet();
405 	register struct router_info *rti =  Head;
406 
407 #ifdef IGMP_DEBUG
408 	printf("[igmp.c,_slowtimo] -- > entering \n");
409 #endif
410 	while (rti) {
411 	    if (rti->rti_type == IGMP_V1_ROUTER) {
412 		rti->rti_time++;
413 		if (rti->rti_time >= IGMP_AGE_THRESHOLD) {
414 			rti->rti_type = IGMP_V2_ROUTER;
415 		}
416 	    }
417 	    rti = rti->rti_next;
418 	}
419 #ifdef IGMP_DEBUG
420 	printf("[igmp.c,_slowtimo] -- > exiting \n");
421 #endif
422 	splx(s);
423 }
424 
425 static void
426 igmp_sendpkt(inm, type, addr)
427 	struct in_multi *inm;
428 	int type;
429 	unsigned long addr;
430 {
431         struct mbuf *m;
432         struct igmp *igmp;
433         struct ip *ip;
434         struct ip_moptions *imo;
435 
436         MGETHDR(m, M_DONTWAIT, MT_HEADER);
437         if (m == NULL)
438                 return;
439 
440 	MALLOC(imo, struct ip_moptions *, sizeof *imo, M_IPMOPTS, M_DONTWAIT);
441 	if (!imo) {
442 		m_free(m);
443 		return;
444 	}
445 
446 	m->m_pkthdr.rcvif = loif;
447 	m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
448 	MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
449 	m->m_data += sizeof(struct ip);
450         m->m_len = IGMP_MINLEN;
451         igmp = mtod(m, struct igmp *);
452         igmp->igmp_type   = type;
453         igmp->igmp_code   = 0;
454         igmp->igmp_group  = inm->inm_addr;
455         igmp->igmp_cksum  = 0;
456         igmp->igmp_cksum  = in_cksum(m, IGMP_MINLEN);
457 
458         m->m_data -= sizeof(struct ip);
459         m->m_len += sizeof(struct ip);
460         ip = mtod(m, struct ip *);
461         ip->ip_tos        = 0;
462         ip->ip_len        = sizeof(struct ip) + IGMP_MINLEN;
463         ip->ip_off        = 0;
464         ip->ip_p          = IPPROTO_IGMP;
465         ip->ip_src.s_addr = INADDR_ANY;
466         ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr;
467 
468         imo->imo_multicast_ifp  = inm->inm_ifp;
469         imo->imo_multicast_ttl  = 1;
470 	imo->imo_multicast_vif  = -1;
471         /*
472          * Request loopback of the report if we are acting as a multicast
473          * router, so that the process-level routing demon can hear it.
474          */
475         imo->imo_multicast_loop = (ip_mrouter != NULL);
476 
477         ip_output(m, router_alert, (struct route *)0, 0, imo);
478 
479 	FREE(imo, M_IPMOPTS);
480         ++igmpstat.igps_snd_reports;
481 }
482