2 * Copyright (c) 1988 Stephen Deering.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Stephen Deering of Stanford University.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
37 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
38 * $FreeBSD: src/sys/netinet/igmp.c,v 1.29.2.2 2003/01/23 21:06:44 sam Exp $
39 * $DragonFly: src/sys/netinet/igmp.c,v 1.5 2004/02/14 02:09:28 dillon Exp $
43 * Internet Group Management Protocol (IGMP) routines.
45 * Written by Steve Deering, Stanford, May 1988.
46 * Modified by Rosen Sharma, Stanford, Aug 1994.
47 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
48 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
50 * MULTICAST Revision: 3.5.1.4
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/malloc.h>
57 #include <sys/socket.h>
58 #include <sys/protosw.h>
59 #include <sys/kernel.h>
60 #include <sys/sysctl.h>
63 #include <net/route.h>
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/ip.h>
69 #include <netinet/ip_var.h>
70 #include <netinet/igmp.h>
71 #include <netinet/igmp_var.h>
73 #include <machine/in_cksum.h>
75 static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
77 static struct router_info *
78 find_rti (struct ifnet *ifp);
80 static struct igmpstat igmpstat;
82 SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW,
83 &igmpstat, igmpstat, "");
85 static int igmp_timers_are_running;
86 static u_long igmp_all_hosts_group;
87 static u_long igmp_all_rtrs_group;
88 static struct mbuf *router_alert;
89 static struct router_info *Head;
91 static void igmp_sendpkt (struct in_multi *, int, unsigned long);
99 * To avoid byte-swapping the same value over and over again.
101 igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
102 igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP);
104 igmp_timers_are_running = 0;
107 * Construct a Router Alert option to use in outgoing packets
109 MGET(router_alert, M_DONTWAIT, MT_DATA);
110 ra = mtod(router_alert, struct ipoption *);
111 ra->ipopt_dst.s_addr = 0;
112 ra->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */
113 ra->ipopt_list[1] = 0x04; /* 4 bytes long */
114 ra->ipopt_list[2] = 0x00;
115 ra->ipopt_list[3] = 0x00;
116 router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1];
118 Head = (struct router_info *) 0;
121 static struct router_info *
125 struct router_info *rti = Head;
128 printf("[igmp.c, _find_rti] --> entering \n");
131 if (rti->rti_ifp == ifp) {
133 printf("[igmp.c, _find_rti] --> found old entry \n");
139 MALLOC(rti, struct router_info *, sizeof *rti, M_IGMP, M_NOWAIT);
141 rti->rti_type = IGMP_V2_ROUTER;
143 rti->rti_next = Head;
146 printf("[igmp.c, _find_rti] --> created an entry \n");
152 igmp_input(m, off, proto)
160 struct ifnet *ifp = m->m_pkthdr.rcvif;
162 struct in_multi *inm;
163 struct in_ifaddr *ia;
164 struct in_multistep step;
165 struct router_info *rti;
167 int timer; /** timer value in the igmp query header **/
169 ++igmpstat.igps_rcv_total;
171 ip = mtod(m, struct ip *);
172 igmplen = ip->ip_len;
177 if (igmplen < IGMP_MINLEN) {
178 ++igmpstat.igps_rcv_tooshort;
182 minlen = iphlen + IGMP_MINLEN;
183 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
184 (m = m_pullup(m, minlen)) == 0) {
185 ++igmpstat.igps_rcv_tooshort;
194 igmp = mtod(m, struct igmp *);
195 if (in_cksum(m, igmplen)) {
196 ++igmpstat.igps_rcv_badsum;
203 ip = mtod(m, struct ip *);
204 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
210 * In the IGMPv2 specification, there are 3 states and a flag.
212 * In Non-Member state, we simply don't have a membership record.
213 * In Delaying Member state, our timer is running (inm->inm_timer)
214 * In Idle Member state, our timer is not running (inm->inm_timer==0)
216 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
217 * we have heard a report from another member, or IGMP_IREPORTEDLAST
218 * if I sent the last report.
220 switch (igmp->igmp_type) {
222 case IGMP_MEMBERSHIP_QUERY:
223 ++igmpstat.igps_rcv_queries;
225 if (ifp->if_flags & IFF_LOOPBACK)
228 if (igmp->igmp_code == 0) {
230 * Old router. Remember that the querier on this
231 * interface is old, and set the timer to the
235 rti->rti_type = IGMP_V1_ROUTER;
238 timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ;
240 if (ip->ip_dst.s_addr != igmp_all_hosts_group ||
241 igmp->igmp_group.s_addr != 0) {
242 ++igmpstat.igps_rcv_badqueries;
248 * New router. Simply do the new validity check.
251 if (igmp->igmp_group.s_addr != 0 &&
252 !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
253 ++igmpstat.igps_rcv_badqueries;
260 * - Start the timers in all of our membership records
261 * that the query applies to for the interface on
262 * which the query arrived excl. those that belong
263 * to the "all-hosts" group (224.0.0.1).
264 * - Restart any timer that is already running but has
265 * a value longer than the requested timeout.
266 * - Use the value specified in the query message as
267 * the maximum timeout.
269 IN_FIRST_MULTI(step, inm);
270 while (inm != NULL) {
271 if (inm->inm_ifp == ifp &&
272 inm->inm_addr.s_addr != igmp_all_hosts_group &&
273 (igmp->igmp_group.s_addr == 0 ||
274 igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) {
275 if (inm->inm_timer == 0 ||
276 inm->inm_timer > timer) {
278 IGMP_RANDOM_DELAY(timer);
279 igmp_timers_are_running = 1;
282 IN_NEXT_MULTI(step, inm);
287 case IGMP_V1_MEMBERSHIP_REPORT:
288 case IGMP_V2_MEMBERSHIP_REPORT:
290 * For fast leave to work, we have to know that we are the
291 * last person to send a report for this group. Reports
292 * can potentially get looped back if we are a multicast
293 * router, so discard reports sourced by me.
296 if (ia && ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr)
299 ++igmpstat.igps_rcv_reports;
301 if (ifp->if_flags & IFF_LOOPBACK)
304 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
305 ++igmpstat.igps_rcv_badreports;
311 * KLUDGE: if the IP source address of the report has an
312 * unspecified (i.e., zero) subnet number, as is allowed for
313 * a booting host, replace it with the correct subnet number
314 * so that a process-level multicast routing demon can
315 * determine which subnet it arrived from. This is necessary
316 * to compensate for the lack of any way for a process to
317 * determine the arrival interface of an incoming packet.
319 if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0)
320 if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
323 * If we belong to the group being reported, stop
324 * our timer for that group.
326 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
330 ++igmpstat.igps_rcv_ourreports;
332 inm->inm_state = IGMP_OTHERMEMBER;
339 * Pass all valid IGMP packets up to any process(es) listening
340 * on a raw IGMP socket.
342 rip_input(m, off, proto);
347 struct in_multi *inm;
351 if (inm->inm_addr.s_addr == igmp_all_hosts_group
352 || inm->inm_ifp->if_flags & IFF_LOOPBACK) {
354 inm->inm_state = IGMP_OTHERMEMBER;
356 inm->inm_rti = find_rti(inm->inm_ifp);
357 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
358 inm->inm_timer = IGMP_RANDOM_DELAY(
359 IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ);
360 inm->inm_state = IGMP_IREPORTEDLAST;
361 igmp_timers_are_running = 1;
368 struct in_multi *inm;
370 if (inm->inm_state == IGMP_IREPORTEDLAST &&
371 inm->inm_addr.s_addr != igmp_all_hosts_group &&
372 !(inm->inm_ifp->if_flags & IFF_LOOPBACK) &&
373 inm->inm_rti->rti_type != IGMP_V1_ROUTER)
374 igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group);
380 struct in_multi *inm;
381 struct in_multistep step;
385 * Quick check to see if any work needs to be done, in order
386 * to minimize the overhead of fasttimo processing.
389 if (!igmp_timers_are_running)
393 igmp_timers_are_running = 0;
394 IN_FIRST_MULTI(step, inm);
395 while (inm != NULL) {
396 if (inm->inm_timer == 0) {
398 } else if (--inm->inm_timer == 0) {
399 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
400 inm->inm_state = IGMP_IREPORTEDLAST;
402 igmp_timers_are_running = 1;
404 IN_NEXT_MULTI(step, inm);
413 struct router_info *rti = Head;
416 printf("[igmp.c,_slowtimo] -- > entering \n");
419 if (rti->rti_type == IGMP_V1_ROUTER) {
421 if (rti->rti_time >= IGMP_AGE_THRESHOLD) {
422 rti->rti_type = IGMP_V2_ROUTER;
428 printf("[igmp.c,_slowtimo] -- > exiting \n");
433 static struct route igmprt;
436 igmp_sendpkt(inm, type, addr)
437 struct in_multi *inm;
444 struct ip_moptions imo;
446 MGETHDR(m, M_DONTWAIT, MT_HEADER);
450 m->m_pkthdr.rcvif = loif;
451 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
452 MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
453 m->m_data += sizeof(struct ip);
454 m->m_len = IGMP_MINLEN;
455 igmp = mtod(m, struct igmp *);
456 igmp->igmp_type = type;
458 igmp->igmp_group = inm->inm_addr;
459 igmp->igmp_cksum = 0;
460 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
462 m->m_data -= sizeof(struct ip);
463 m->m_len += sizeof(struct ip);
464 ip = mtod(m, struct ip *);
466 ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
468 ip->ip_p = IPPROTO_IGMP;
469 ip->ip_src.s_addr = INADDR_ANY;
470 ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr;
472 imo.imo_multicast_ifp = inm->inm_ifp;
473 imo.imo_multicast_ttl = 1;
474 imo.imo_multicast_vif = -1;
476 * Request loopback of the report if we are acting as a multicast
477 * router, so that the process-level routing demon can hear it.
479 imo.imo_multicast_loop = (ip_mrouter != NULL);
483 * Do we have to worry about reentrancy here? Don't think so.
485 ip_output(m, router_alert, &igmprt, 0, &imo, NULL);
487 ++igmpstat.igps_snd_reports;