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 * 4. 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.
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
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 $
38 * Internet Group Management Protocol (IGMP) routines.
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.
45 * MULTICAST Revision: 3.5.1.4
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/malloc.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>
59 #include <machine/stdarg.h>
62 #include <net/route.h>
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>
72 static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
74 static struct router_info *
75 find_rti (struct ifnet *ifp);
77 static struct igmpstat igmpstat;
79 SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW,
80 &igmpstat, igmpstat, "IGMP statistics");
82 static int igmp_timers_are_running;
83 static u_long igmp_all_hosts_group;
84 static u_long igmp_all_rtrs_group;
85 static struct mbuf *router_alert;
86 static struct router_info *Head;
88 static void igmp_sendpkt (struct in_multi *, int, unsigned long);
96 * To avoid byte-swapping the same value over and over again.
98 igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
99 igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP);
101 igmp_timers_are_running = 0;
104 * Construct a Router Alert option to use in outgoing packets
106 MGET(router_alert, MB_DONTWAIT, MT_DATA);
107 ra = mtod(router_alert, struct ipoption *);
108 ra->ipopt_dst.s_addr = 0;
109 ra->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */
110 ra->ipopt_list[1] = 0x04; /* 4 bytes long */
111 ra->ipopt_list[2] = 0x00;
112 ra->ipopt_list[3] = 0x00;
113 router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1];
118 static struct router_info *
119 find_rti(struct ifnet *ifp)
121 struct router_info *rti = Head;
124 kprintf("[igmp.c, _find_rti] --> entering \n");
127 if (rti->rti_ifp == ifp) {
129 kprintf("[igmp.c, _find_rti] --> found old entry \n");
135 rti = kmalloc(sizeof *rti, M_IGMP, M_INTWAIT);
137 rti->rti_type = IGMP_V2_ROUTER;
139 rti->rti_next = Head;
142 kprintf("[igmp.c, _find_rti] --> created an entry \n");
148 igmp_input(struct mbuf **mp, int *offp, int proto)
150 struct mbuf *m = *mp;
155 struct ifnet *ifp = m->m_pkthdr.rcvif;
157 struct in_multi *inm;
158 struct in_ifaddr *ia;
159 struct in_multistep step;
160 struct router_info *rti;
161 int timer; /** timer value in the igmp query header **/
166 ++igmpstat.igps_rcv_total;
168 ip = mtod(m, struct ip *);
169 igmplen = ip->ip_len;
174 if (igmplen < IGMP_MINLEN) {
175 ++igmpstat.igps_rcv_tooshort;
177 return(IPPROTO_DONE);
179 minlen = iphlen + IGMP_MINLEN;
180 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
181 (m = m_pullup(m, minlen)) == NULL) {
182 ++igmpstat.igps_rcv_tooshort;
183 return(IPPROTO_DONE);
191 igmp = mtod(m, struct igmp *);
192 if (in_cksum(m, igmplen)) {
193 ++igmpstat.igps_rcv_badsum;
195 return(IPPROTO_DONE);
200 ip = mtod(m, struct ip *);
201 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
207 * In the IGMPv2 specification, there are 3 states and a flag.
209 * In Non-Member state, we simply don't have a membership record.
210 * In Delaying Member state, our timer is running (inm->inm_timer)
211 * In Idle Member state, our timer is not running (inm->inm_timer==0)
213 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
214 * we have heard a report from another member, or IGMP_IREPORTEDLAST
215 * if I sent the last report.
217 switch (igmp->igmp_type) {
219 case IGMP_MEMBERSHIP_QUERY:
220 ++igmpstat.igps_rcv_queries;
222 if (ifp->if_flags & IFF_LOOPBACK)
225 if (igmp->igmp_code == 0) {
227 * Old router. Remember that the querier on this
228 * interface is old, and set the timer to the
232 rti->rti_type = IGMP_V1_ROUTER;
235 timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ;
237 if (ip->ip_dst.s_addr != igmp_all_hosts_group ||
238 igmp->igmp_group.s_addr != 0) {
239 ++igmpstat.igps_rcv_badqueries;
241 return(IPPROTO_DONE);
245 * New router. Simply do the new validity check.
248 if (igmp->igmp_group.s_addr != 0 &&
249 !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
250 ++igmpstat.igps_rcv_badqueries;
252 return(IPPROTO_DONE);
257 * - Start the timers in all of our membership records
258 * that the query applies to for the interface on
259 * which the query arrived excl. those that belong
260 * to the "all-hosts" group (224.0.0.1).
261 * - Restart any timer that is already running but has
262 * a value longer than the requested timeout.
263 * - Use the value specified in the query message as
264 * the maximum timeout.
266 IN_FIRST_MULTI(step, inm);
267 while (inm != NULL) {
268 if (inm->inm_ifp == ifp &&
269 inm->inm_addr.s_addr != igmp_all_hosts_group &&
270 (igmp->igmp_group.s_addr == 0 ||
271 igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) {
272 if (inm->inm_timer == 0 ||
273 inm->inm_timer > timer) {
275 IGMP_RANDOM_DELAY(timer);
276 igmp_timers_are_running = 1;
279 IN_NEXT_MULTI(step, inm);
284 case IGMP_V1_MEMBERSHIP_REPORT:
285 case IGMP_V2_MEMBERSHIP_REPORT:
287 * For fast leave to work, we have to know that we are the
288 * last person to send a report for this group. Reports
289 * can potentially get looped back if we are a multicast
290 * router, so discard reports sourced by me.
293 if (ia && ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr)
296 ++igmpstat.igps_rcv_reports;
298 if (ifp->if_flags & IFF_LOOPBACK)
301 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
302 ++igmpstat.igps_rcv_badreports;
304 return(IPPROTO_DONE);
308 * KLUDGE: if the IP source address of the report has an
309 * unspecified (i.e., zero) subnet number, as is allowed for
310 * a booting host, replace it with the correct subnet number
311 * so that a process-level multicast routing demon can
312 * determine which subnet it arrived from. This is necessary
313 * to compensate for the lack of any way for a process to
314 * determine the arrival interface of an incoming packet.
316 if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0)
317 if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
320 * If we belong to the group being reported, stop
321 * our timer for that group.
323 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
327 ++igmpstat.igps_rcv_ourreports;
329 inm->inm_state = IGMP_OTHERMEMBER;
336 * Pass all valid IGMP packets up to any process(es) listening
337 * on a raw IGMP socket.
340 rip_input(mp, offp, proto);
341 return(IPPROTO_DONE);
345 igmp_joingroup(struct in_multi *inm)
348 if (inm->inm_addr.s_addr == igmp_all_hosts_group
349 || inm->inm_ifp->if_flags & IFF_LOOPBACK) {
351 inm->inm_state = IGMP_OTHERMEMBER;
353 inm->inm_rti = find_rti(inm->inm_ifp);
354 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
355 inm->inm_timer = IGMP_RANDOM_DELAY(
356 IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ);
357 inm->inm_state = IGMP_IREPORTEDLAST;
358 igmp_timers_are_running = 1;
364 igmp_leavegroup(struct in_multi *inm)
366 if (inm->inm_state == IGMP_IREPORTEDLAST &&
367 inm->inm_addr.s_addr != igmp_all_hosts_group &&
368 !(inm->inm_ifp->if_flags & IFF_LOOPBACK) &&
369 inm->inm_rti->rti_type != IGMP_V1_ROUTER)
370 igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group);
376 struct in_multi *inm;
377 struct in_multistep step;
380 * Quick check to see if any work needs to be done, in order
381 * to minimize the overhead of fasttimo processing.
384 if (!igmp_timers_are_running)
388 igmp_timers_are_running = 0;
389 IN_FIRST_MULTI(step, inm);
390 while (inm != NULL) {
391 if (inm->inm_timer == 0) {
393 } else if (--inm->inm_timer == 0) {
394 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
395 inm->inm_state = IGMP_IREPORTEDLAST;
397 igmp_timers_are_running = 1;
399 IN_NEXT_MULTI(step, inm);
407 struct router_info *rti = Head;
411 kprintf("[igmp.c,_slowtimo] -- > entering \n");
414 if (rti->rti_type == IGMP_V1_ROUTER) {
416 if (rti->rti_time >= IGMP_AGE_THRESHOLD) {
417 rti->rti_type = IGMP_V2_ROUTER;
423 kprintf("[igmp.c,_slowtimo] -- > exiting \n");
428 static struct route igmprt;
431 igmp_sendpkt(struct in_multi *inm, int type, unsigned long addr)
436 struct ip_moptions imo;
438 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
442 m->m_pkthdr.rcvif = loif;
443 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
444 MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
445 m->m_data += sizeof(struct ip);
446 m->m_len = IGMP_MINLEN;
447 igmp = mtod(m, struct igmp *);
448 igmp->igmp_type = type;
450 igmp->igmp_group = inm->inm_addr;
451 igmp->igmp_cksum = 0;
452 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
454 m->m_data -= sizeof(struct ip);
455 m->m_len += sizeof(struct ip);
456 ip = mtod(m, struct ip *);
458 ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
460 ip->ip_p = IPPROTO_IGMP;
461 ip->ip_src.s_addr = INADDR_ANY;
462 ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr;
464 imo.imo_multicast_ifp = inm->inm_ifp;
465 imo.imo_multicast_ttl = 1;
466 imo.imo_multicast_vif = -1;
468 * Request loopback of the report if we are acting as a multicast
469 * router, so that the process-level routing demon can hear it.
471 imo.imo_multicast_loop = (ip_mrouter != NULL);
475 * Do we have to worry about reentrancy here? Don't think so.
477 ip_output(m, router_alert, &igmprt, 0, &imo, NULL);
479 ++igmpstat.igps_snd_reports;