FreeBSD/Linux Kernel Cross Reference
sys/netinet/igmp.c
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 * $FreeBSD$
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/malloc.h>
55 #include <sys/mbuf.h>
56 #include <sys/socket.h>
57 #include <sys/protosw.h>
58 #include <sys/kernel.h>
59 #include <sys/sysctl.h>
60
61 #include <net/if.h>
62 #include <net/route.h>
63
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>
71
72 static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
73
74 static struct router_info *
75 find_rti __P((struct ifnet *ifp));
76
77 static struct igmpstat igmpstat;
78
79 SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW,
80 &igmpstat, igmpstat, "");
81
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;
87
88 static void igmp_sendpkt __P((struct in_multi *, int, unsigned long));
89
90 void
91 igmp_init()
92 {
93 struct ipoption *ra;
94
95 /*
96 * To avoid byte-swapping the same value over and over again.
97 */
98 igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
99 igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP);
100
101 igmp_timers_are_running = 0;
102
103 /*
104 * Construct a Router Alert option to use in outgoing packets
105 */
106 MGET(router_alert, M_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];
114
115 Head = (struct router_info *) 0;
116 }
117
118 static struct router_info *
119 find_rti(ifp)
120 struct ifnet *ifp;
121 {
122 register struct router_info *rti = Head;
123
124 #ifdef IGMP_DEBUG
125 printf("[igmp.c, _find_rti] --> entering \n");
126 #endif
127 while (rti) {
128 if (rti->rti_ifp == ifp) {
129 #ifdef IGMP_DEBUG
130 printf("[igmp.c, _find_rti] --> found old entry \n");
131 #endif
132 return rti;
133 }
134 rti = rti->rti_next;
135 }
136 MALLOC(rti, struct router_info *, sizeof *rti, M_IGMP, M_NOWAIT);
137 rti->rti_ifp = ifp;
138 rti->rti_type = IGMP_V2_ROUTER;
139 rti->rti_time = 0;
140 rti->rti_next = Head;
141 Head = rti;
142 #ifdef IGMP_DEBUG
143 printf("[igmp.c, _find_rti] --> created an entry \n");
144 #endif
145 return rti;
146 }
147
148 void
149 igmp_input(m, off, proto)
150 register struct mbuf *m;
151 int off, proto;
152 {
153 register int iphlen = off;
154 register struct igmp *igmp;
155 register struct ip *ip;
156 register int igmplen;
157 register struct ifnet *ifp = m->m_pkthdr.rcvif;
158 register int minlen;
159 register struct in_multi *inm;
160 register struct in_ifaddr *ia;
161 struct in_multistep step;
162 struct router_info *rti;
163
164 int timer; /** timer value in the igmp query header **/
165
166 ++igmpstat.igps_rcv_total;
167
168 ip = mtod(m, struct ip *);
169 igmplen = ip->ip_len;
170
171 /*
172 * Validate lengths
173 */
174 if (igmplen < IGMP_MINLEN) {
175 ++igmpstat.igps_rcv_tooshort;
176 m_freem(m);
177 return;
178 }
179 minlen = iphlen + IGMP_MINLEN;
180 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
181 (m = m_pullup(m, minlen)) == 0) {
182 ++igmpstat.igps_rcv_tooshort;
183 return;
184 }
185
186 /*
187 * Validate checksum
188 */
189 m->m_data += iphlen;
190 m->m_len -= iphlen;
191 igmp = mtod(m, struct igmp *);
192 if (in_cksum(m, igmplen)) {
193 ++igmpstat.igps_rcv_badsum;
194 m_freem(m);
195 return;
196 }
197 m->m_data -= iphlen;
198 m->m_len += iphlen;
199
200 ip = mtod(m, struct ip *);
201 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
202 if (timer == 0)
203 timer = 1;
204 rti = find_rti(ifp);
205
206 /*
207 * In the IGMPv2 specification, there are 3 states and a flag.
208 *
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)
212 *
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.
216 */
217 switch (igmp->igmp_type) {
218
219 case IGMP_MEMBERSHIP_QUERY:
220 ++igmpstat.igps_rcv_queries;
221
222 if (ifp->if_flags & IFF_LOOPBACK)
223 break;
224
225 if (igmp->igmp_code == 0) {
226 /*
227 * Old router. Remember that the querier on this
228 * interface is old, and set the timer to the
229 * value in RFC 1112.
230 */
231
232 rti->rti_type = IGMP_V1_ROUTER;
233 rti->rti_time = 0;
234
235 timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ;
236
237 if (ip->ip_dst.s_addr != igmp_all_hosts_group ||
238 igmp->igmp_group.s_addr != 0) {
239 ++igmpstat.igps_rcv_badqueries;
240 m_freem(m);
241 return;
242 }
243 } else {
244 /*
245 * New router. Simply do the new validity check.
246 */
247
248 if (igmp->igmp_group.s_addr != 0 &&
249 !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
250 ++igmpstat.igps_rcv_badqueries;
251 m_freem(m);
252 return;
253 }
254 }
255
256 /*
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.
265 */
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) {
274 inm->inm_timer =
275 IGMP_RANDOM_DELAY(timer);
276 igmp_timers_are_running = 1;
277 }
278 }
279 IN_NEXT_MULTI(step, inm);
280 }
281
282 break;
283
284 case IGMP_V1_MEMBERSHIP_REPORT:
285 case IGMP_V2_MEMBERSHIP_REPORT:
286 /*
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.
291 */
292 IFP_TO_IA(ifp, ia);
293 if (ia && ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr)
294 break;
295
296 ++igmpstat.igps_rcv_reports;
297
298 if (ifp->if_flags & IFF_LOOPBACK)
299 break;
300
301 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
302 ++igmpstat.igps_rcv_badreports;
303 m_freem(m);
304 return;
305 }
306
307 /*
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.
315 */
316 if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0)
317 if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
318
319 /*
320 * If we belong to the group being reported, stop
321 * our timer for that group.
322 */
323 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
324
325 if (inm != NULL) {
326 inm->inm_timer = 0;
327 ++igmpstat.igps_rcv_ourreports;
328
329 inm->inm_state = IGMP_OTHERMEMBER;
330 }
331
332 break;
333 }
334
335 /*
336 * Pass all valid IGMP packets up to any process(es) listening
337 * on a raw IGMP socket.
338 */
339 rip_input(m, off, proto);
340 }
341
342 void
343 igmp_joingroup(inm)
344 struct in_multi *inm;
345 {
346 int s = splnet();
347
348 if (inm->inm_addr.s_addr == igmp_all_hosts_group
349 || inm->inm_ifp->if_flags & IFF_LOOPBACK) {
350 inm->inm_timer = 0;
351 inm->inm_state = IGMP_OTHERMEMBER;
352 } else {
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;
359 }
360 splx(s);
361 }
362
363 void
364 igmp_leavegroup(inm)
365 struct in_multi *inm;
366 {
367 if (inm->inm_state == IGMP_IREPORTEDLAST &&
368 inm->inm_addr.s_addr != igmp_all_hosts_group &&
369 !(inm->inm_ifp->if_flags & IFF_LOOPBACK) &&
370 inm->inm_rti->rti_type != IGMP_V1_ROUTER)
371 igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group);
372 }
373
374 void
375 igmp_fasttimo()
376 {
377 register struct in_multi *inm;
378 struct in_multistep step;
379 int s;
380
381 /*
382 * Quick check to see if any work needs to be done, in order
383 * to minimize the overhead of fasttimo processing.
384 */
385
386 if (!igmp_timers_are_running)
387 return;
388
389 s = splnet();
390 igmp_timers_are_running = 0;
391 IN_FIRST_MULTI(step, inm);
392 while (inm != NULL) {
393 if (inm->inm_timer == 0) {
394 /* do nothing */
395 } else if (--inm->inm_timer == 0) {
396 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
397 inm->inm_state = IGMP_IREPORTEDLAST;
398 } else {
399 igmp_timers_are_running = 1;
400 }
401 IN_NEXT_MULTI(step, inm);
402 }
403 splx(s);
404 }
405
406 void
407 igmp_slowtimo()
408 {
409 int s = splnet();
410 register struct router_info *rti = Head;
411
412 #ifdef IGMP_DEBUG
413 printf("[igmp.c,_slowtimo] -- > entering \n");
414 #endif
415 while (rti) {
416 if (rti->rti_type == IGMP_V1_ROUTER) {
417 rti->rti_time++;
418 if (rti->rti_time >= IGMP_AGE_THRESHOLD) {
419 rti->rti_type = IGMP_V2_ROUTER;
420 }
421 }
422 rti = rti->rti_next;
423 }
424 #ifdef IGMP_DEBUG
425 printf("[igmp.c,_slowtimo] -- > exiting \n");
426 #endif
427 splx(s);
428 }
429
430 static struct route igmprt;
431
432 static void
433 igmp_sendpkt(inm, type, addr)
434 struct in_multi *inm;
435 int type;
436 unsigned long addr;
437 {
438 struct mbuf *m;
439 struct igmp *igmp;
440 struct ip *ip;
441 struct ip_moptions imo;
442
443 MGETHDR(m, M_DONTWAIT, MT_HEADER);
444 if (m == NULL)
445 return;
446
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;
454 igmp->igmp_code = 0;
455 igmp->igmp_group = inm->inm_addr;
456 igmp->igmp_cksum = 0;
457 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
458
459 m->m_data -= sizeof(struct ip);
460 m->m_len += sizeof(struct ip);
461 ip = mtod(m, struct ip *);
462 ip->ip_tos = 0;
463 ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
464 ip->ip_off = 0;
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;
468
469 imo.imo_multicast_ifp = inm->inm_ifp;
470 imo.imo_multicast_ttl = 1;
471 imo.imo_multicast_vif = -1;
472 /*
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.
475 */
476 imo.imo_multicast_loop = (ip_mrouter != NULL);
477
478 /*
479 * XXX
480 * Do we have to worry about reentrancy here? Don't think so.
481 */
482 ip_output(m, router_alert, &igmprt, 0, &imo, NULL);
483
484 ++igmpstat.igps_snd_reports;
485 }
Cache object: dcfdcd639d720df7fddc05a2c3d4cedc
|