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