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.14 2008/06/08 08:38:05 sephe 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>
61 #include <sys/in_cksum.h>
62 #include <sys/thread2.h>
64 #include <machine/stdarg.h>
67 #include <net/route.h>
69 #include <netinet/in.h>
70 #include <netinet/in_var.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip_var.h>
74 #include <netinet/igmp.h>
75 #include <netinet/igmp_var.h>
77 static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
79 static struct router_info *
80 find_rti (struct ifnet *ifp);
82 static struct igmpstat igmpstat;
84 SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW,
85 &igmpstat, igmpstat, "IGMP statistics");
87 static int igmp_timers_are_running;
88 static u_long igmp_all_hosts_group;
89 static u_long igmp_all_rtrs_group;
90 static struct mbuf *router_alert;
91 static struct router_info *Head;
93 static void igmp_sendpkt (struct in_multi *, int, unsigned long);
101 * To avoid byte-swapping the same value over and over again.
103 igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
104 igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP);
106 igmp_timers_are_running = 0;
109 * Construct a Router Alert option to use in outgoing packets
111 MGET(router_alert, MB_DONTWAIT, MT_DATA);
112 ra = mtod(router_alert, struct ipoption *);
113 ra->ipopt_dst.s_addr = 0;
114 ra->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */
115 ra->ipopt_list[1] = 0x04; /* 4 bytes long */
116 ra->ipopt_list[2] = 0x00;
117 ra->ipopt_list[3] = 0x00;
118 router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1];
123 static struct router_info *
124 find_rti(struct ifnet *ifp)
126 struct router_info *rti = Head;
129 kprintf("[igmp.c, _find_rti] --> entering \n");
132 if (rti->rti_ifp == ifp) {
134 kprintf("[igmp.c, _find_rti] --> found old entry \n");
140 MALLOC(rti, struct router_info *, sizeof *rti, M_IGMP, M_INTWAIT);
142 rti->rti_type = IGMP_V2_ROUTER;
144 rti->rti_next = Head;
147 kprintf("[igmp.c, _find_rti] --> created an entry \n");
153 igmp_input(struct mbuf **mp, int *offp, int proto)
155 struct mbuf *m = *mp;
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;
166 int timer; /** timer value in the igmp query header **/
171 ++igmpstat.igps_rcv_total;
173 ip = mtod(m, struct ip *);
174 igmplen = ip->ip_len;
179 if (igmplen < IGMP_MINLEN) {
180 ++igmpstat.igps_rcv_tooshort;
182 return(IPPROTO_DONE);
184 minlen = iphlen + IGMP_MINLEN;
185 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
186 (m = m_pullup(m, minlen)) == 0) {
187 ++igmpstat.igps_rcv_tooshort;
188 return(IPPROTO_DONE);
196 igmp = mtod(m, struct igmp *);
197 if (in_cksum(m, igmplen)) {
198 ++igmpstat.igps_rcv_badsum;
200 return(IPPROTO_DONE);
205 ip = mtod(m, struct ip *);
206 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
212 * In the IGMPv2 specification, there are 3 states and a flag.
214 * In Non-Member state, we simply don't have a membership record.
215 * In Delaying Member state, our timer is running (inm->inm_timer)
216 * In Idle Member state, our timer is not running (inm->inm_timer==0)
218 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
219 * we have heard a report from another member, or IGMP_IREPORTEDLAST
220 * if I sent the last report.
222 switch (igmp->igmp_type) {
224 case IGMP_MEMBERSHIP_QUERY:
225 ++igmpstat.igps_rcv_queries;
227 if (ifp->if_flags & IFF_LOOPBACK)
230 if (igmp->igmp_code == 0) {
232 * Old router. Remember that the querier on this
233 * interface is old, and set the timer to the
237 rti->rti_type = IGMP_V1_ROUTER;
240 timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ;
242 if (ip->ip_dst.s_addr != igmp_all_hosts_group ||
243 igmp->igmp_group.s_addr != 0) {
244 ++igmpstat.igps_rcv_badqueries;
246 return(IPPROTO_DONE);
250 * New router. Simply do the new validity check.
253 if (igmp->igmp_group.s_addr != 0 &&
254 !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
255 ++igmpstat.igps_rcv_badqueries;
257 return(IPPROTO_DONE);
262 * - Start the timers in all of our membership records
263 * that the query applies to for the interface on
264 * which the query arrived excl. those that belong
265 * to the "all-hosts" group (224.0.0.1).
266 * - Restart any timer that is already running but has
267 * a value longer than the requested timeout.
268 * - Use the value specified in the query message as
269 * the maximum timeout.
271 IN_FIRST_MULTI(step, inm);
272 while (inm != NULL) {
273 if (inm->inm_ifp == ifp &&
274 inm->inm_addr.s_addr != igmp_all_hosts_group &&
275 (igmp->igmp_group.s_addr == 0 ||
276 igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) {
277 if (inm->inm_timer == 0 ||
278 inm->inm_timer > timer) {
280 IGMP_RANDOM_DELAY(timer);
281 igmp_timers_are_running = 1;
284 IN_NEXT_MULTI(step, inm);
289 case IGMP_V1_MEMBERSHIP_REPORT:
290 case IGMP_V2_MEMBERSHIP_REPORT:
292 * For fast leave to work, we have to know that we are the
293 * last person to send a report for this group. Reports
294 * can potentially get looped back if we are a multicast
295 * router, so discard reports sourced by me.
298 if (ia && ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr)
301 ++igmpstat.igps_rcv_reports;
303 if (ifp->if_flags & IFF_LOOPBACK)
306 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
307 ++igmpstat.igps_rcv_badreports;
309 return(IPPROTO_DONE);
313 * KLUDGE: if the IP source address of the report has an
314 * unspecified (i.e., zero) subnet number, as is allowed for
315 * a booting host, replace it with the correct subnet number
316 * so that a process-level multicast routing demon can
317 * determine which subnet it arrived from. This is necessary
318 * to compensate for the lack of any way for a process to
319 * determine the arrival interface of an incoming packet.
321 if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0)
322 if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
325 * If we belong to the group being reported, stop
326 * our timer for that group.
328 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
332 ++igmpstat.igps_rcv_ourreports;
334 inm->inm_state = IGMP_OTHERMEMBER;
341 * Pass all valid IGMP packets up to any process(es) listening
342 * on a raw IGMP socket.
345 rip_input(mp, offp, proto);
346 return(IPPROTO_DONE);
350 igmp_joingroup(struct in_multi *inm)
353 if (inm->inm_addr.s_addr == igmp_all_hosts_group
354 || inm->inm_ifp->if_flags & IFF_LOOPBACK) {
356 inm->inm_state = IGMP_OTHERMEMBER;
358 inm->inm_rti = find_rti(inm->inm_ifp);
359 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
360 inm->inm_timer = IGMP_RANDOM_DELAY(
361 IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ);
362 inm->inm_state = IGMP_IREPORTEDLAST;
363 igmp_timers_are_running = 1;
369 igmp_leavegroup(struct in_multi *inm)
371 if (inm->inm_state == IGMP_IREPORTEDLAST &&
372 inm->inm_addr.s_addr != igmp_all_hosts_group &&
373 !(inm->inm_ifp->if_flags & IFF_LOOPBACK) &&
374 inm->inm_rti->rti_type != IGMP_V1_ROUTER)
375 igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group);
381 struct in_multi *inm;
382 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);
412 struct router_info *rti = Head;
416 kprintf("[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 kprintf("[igmp.c,_slowtimo] -- > exiting \n");
433 static struct route igmprt;
436 igmp_sendpkt(struct in_multi *inm, int type, unsigned long addr)
441 struct ip_moptions imo;
443 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
447 m->m_pkthdr.rcvif = loif;
448 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
449 MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
450 m->m_data += sizeof(struct ip);
451 m->m_len = IGMP_MINLEN;
452 igmp = mtod(m, struct igmp *);
453 igmp->igmp_type = type;
455 igmp->igmp_group = inm->inm_addr;
456 igmp->igmp_cksum = 0;
457 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
459 m->m_data -= sizeof(struct ip);
460 m->m_len += sizeof(struct ip);
461 ip = mtod(m, struct ip *);
463 ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
465 ip->ip_p = IPPROTO_IGMP;
466 ip->ip_src.s_addr = INADDR_ANY;
467 ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr;
469 imo.imo_multicast_ifp = inm->inm_ifp;
470 imo.imo_multicast_ttl = 1;
471 imo.imo_multicast_vif = -1;
473 * Request loopback of the report if we are acting as a multicast
474 * router, so that the process-level routing demon can hear it.
476 imo.imo_multicast_loop = (ip_mrouter != NULL);
480 * Do we have to worry about reentrancy here? Don't think so.
482 ip_output(m, router_alert, &igmprt, 0, &imo, NULL);
484 ++igmpstat.igps_snd_reports;