igmp.c revision 1.19 1 /* $NetBSD: igmp.c,v 1.19 1999/01/19 23:03:20 mycroft Exp $ */
2
3 /*
4 * Internet Group Management Protocol (IGMP) routines.
5 *
6 * Written by Steve Deering, Stanford, May 1988.
7 * Modified by Rosen Sharma, Stanford, Aug 1994.
8 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
9 *
10 * MULTICAST Revision: 1.3
11 */
12
13 #include "opt_mrouting.h"
14
15 #include <sys/param.h>
16 #include <sys/mbuf.h>
17 #include <sys/socket.h>
18 #include <sys/protosw.h>
19 #include <sys/systm.h>
20
21 #include <net/if.h>
22 #include <net/route.h>
23
24 #include <netinet/in.h>
25 #include <netinet/in_var.h>
26 #include <netinet/in_systm.h>
27 #include <netinet/ip.h>
28 #include <netinet/ip_var.h>
29 #include <netinet/igmp.h>
30 #include <netinet/igmp_var.h>
31
32 #include <machine/stdarg.h>
33
34 #define IP_MULTICASTOPTS 0
35
36 int igmp_timers_are_running;
37 static struct router_info *rti_head;
38
39 void igmp_sendpkt __P((struct in_multi *, int));
40 static int rti_fill __P((struct in_multi *));
41 static struct router_info * rti_find __P((struct ifnet *));
42
43 void
44 igmp_init()
45 {
46
47 /*
48 * To avoid byte-swapping the same value over and over again.
49 */
50 igmp_timers_are_running = 0;
51 rti_head = 0;
52 }
53
54 static int
55 rti_fill(inm)
56 struct in_multi *inm;
57 {
58 register struct router_info *rti;
59
60 for (rti = rti_head; rti != 0; rti = rti->rti_next) {
61 if (rti->rti_ifp == inm->inm_ifp) {
62 inm->inm_rti = rti;
63 if (rti->rti_type == IGMP_v1_ROUTER)
64 return (IGMP_v1_HOST_MEMBERSHIP_REPORT);
65 else
66 return (IGMP_v2_HOST_MEMBERSHIP_REPORT);
67 }
68 }
69
70 rti = (struct router_info *)malloc(sizeof(struct router_info),
71 M_MRTABLE, M_NOWAIT);
72 rti->rti_ifp = inm->inm_ifp;
73 rti->rti_type = IGMP_v2_ROUTER;
74 rti->rti_next = rti_head;
75 rti_head = rti;
76 inm->inm_rti = rti;
77 return (IGMP_v2_HOST_MEMBERSHIP_REPORT);
78 }
79
80 static struct router_info *
81 rti_find(ifp)
82 struct ifnet *ifp;
83 {
84 register struct router_info *rti;
85
86 for (rti = rti_head; rti != 0; rti = rti->rti_next) {
87 if (rti->rti_ifp == ifp)
88 return (rti);
89 }
90
91 rti = (struct router_info *)malloc(sizeof(struct router_info),
92 M_MRTABLE, M_NOWAIT);
93 rti->rti_ifp = ifp;
94 rti->rti_type = IGMP_v2_ROUTER;
95 rti->rti_next = rti_head;
96 rti_head = rti;
97 return (rti);
98 }
99
100 void
101 #if __STDC__
102 igmp_input(struct mbuf *m, ...)
103 #else
104 igmp_input(m, va_alist)
105 struct mbuf *m;
106 va_dcl
107 #endif
108 {
109 register int iphlen;
110 register struct ifnet *ifp = m->m_pkthdr.rcvif;
111 register struct ip *ip = mtod(m, struct ip *);
112 register struct igmp *igmp;
113 register int minlen;
114 struct in_multi *inm;
115 struct in_multistep step;
116 struct router_info *rti;
117 register struct in_ifaddr *ia;
118 int timer;
119 va_list ap;
120
121 va_start(ap, m);
122 iphlen = va_arg(ap, int);
123 va_end(ap);
124
125 ++igmpstat.igps_rcv_total;
126
127 /*
128 * Validate lengths
129 */
130 minlen = iphlen + IGMP_MINLEN;
131 if (ip->ip_len < minlen) {
132 ++igmpstat.igps_rcv_tooshort;
133 m_freem(m);
134 return;
135 }
136 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
137 (m = m_pullup(m, minlen)) == 0) {
138 ++igmpstat.igps_rcv_tooshort;
139 return;
140 }
141
142 /*
143 * Validate checksum
144 */
145 m->m_data += iphlen;
146 m->m_len -= iphlen;
147 igmp = mtod(m, struct igmp *);
148 if (in_cksum(m, ip->ip_len - iphlen)) {
149 ++igmpstat.igps_rcv_badsum;
150 m_freem(m);
151 return;
152 }
153 m->m_data -= iphlen;
154 m->m_len += iphlen;
155 ip = mtod(m, struct ip *);
156
157 switch (igmp->igmp_type) {
158
159 case IGMP_HOST_MEMBERSHIP_QUERY:
160 ++igmpstat.igps_rcv_queries;
161
162 if (ifp->if_flags & IFF_LOOPBACK)
163 break;
164
165 if (igmp->igmp_code == 0) {
166 rti = rti_find(ifp);
167 rti->rti_type = IGMP_v1_ROUTER;
168 rti->rti_age = 0;
169
170 if (ip->ip_dst.s_addr != INADDR_ALLHOSTS_GROUP) {
171 ++igmpstat.igps_rcv_badqueries;
172 m_freem(m);
173 return;
174 }
175
176 /*
177 * Start the timers in all of our membership records
178 * for the interface on which the query arrived,
179 * except those that are already running and those
180 * that belong to a "local" group (224.0.0.X).
181 */
182 IN_FIRST_MULTI(step, inm);
183 while (inm != NULL) {
184 if (inm->inm_ifp == ifp &&
185 inm->inm_timer == 0 &&
186 !IN_LOCAL_GROUP(inm->inm_addr.s_addr)) {
187 inm->inm_state = IGMP_DELAYING_MEMBER;
188 inm->inm_timer = IGMP_RANDOM_DELAY(
189 IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ);
190 igmp_timers_are_running = 1;
191 }
192 IN_NEXT_MULTI(step, inm);
193 }
194 } else {
195 if (!IN_MULTICAST(ip->ip_dst.s_addr)) {
196 ++igmpstat.igps_rcv_badqueries;
197 m_freem(m);
198 return;
199 }
200
201 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
202
203 /*
204 * Start the timers in all of our membership records
205 * for the interface on which the query arrived,
206 * except those that are already running and those
207 * that belong to a "local" group (224.0.0.X). For
208 * timers already running, check if they need to be
209 * reset.
210 */
211 IN_FIRST_MULTI(step, inm);
212 while (inm != NULL) {
213 if (inm->inm_ifp == ifp &&
214 !IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
215 (ip->ip_dst.s_addr == INADDR_ALLHOSTS_GROUP ||
216 in_hosteq(ip->ip_dst, inm->inm_addr))) {
217 switch (inm->inm_state) {
218 case IGMP_DELAYING_MEMBER:
219 if (inm->inm_timer <= timer)
220 break;
221 /* FALLTHROUGH */
222 case IGMP_IDLE_MEMBER:
223 case IGMP_LAZY_MEMBER:
224 case IGMP_AWAKENING_MEMBER:
225 inm->inm_state =
226 IGMP_DELAYING_MEMBER;
227 inm->inm_timer =
228 IGMP_RANDOM_DELAY(timer);
229 igmp_timers_are_running = 1;
230 break;
231 case IGMP_SLEEPING_MEMBER:
232 inm->inm_state =
233 IGMP_AWAKENING_MEMBER;
234 break;
235 }
236 }
237 IN_NEXT_MULTI(step, inm);
238 }
239 }
240
241 break;
242
243 case IGMP_v1_HOST_MEMBERSHIP_REPORT:
244 ++igmpstat.igps_rcv_reports;
245
246 if (ifp->if_flags & IFF_LOOPBACK)
247 break;
248
249 if (!IN_MULTICAST(igmp->igmp_group.s_addr) ||
250 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
251 ++igmpstat.igps_rcv_badreports;
252 m_freem(m);
253 return;
254 }
255
256 /*
257 * KLUDGE: if the IP source address of the report has an
258 * unspecified (i.e., zero) subnet number, as is allowed for
259 * a booting host, replace it with the correct subnet number
260 * so that a process-level multicast routing daemon can
261 * determine which subnet it arrived from. This is necessary
262 * to compensate for the lack of any way for a process to
263 * determine the arrival interface of an incoming packet.
264 */
265 if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) {
266 IFP_TO_IA(ifp, ia); /* XXX */
267 if (ia)
268 ip->ip_src.s_addr = ia->ia_subnet;
269 }
270
271 /*
272 * If we belong to the group being reported, stop
273 * our timer for that group.
274 */
275 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
276 if (inm != NULL) {
277 inm->inm_timer = 0;
278 ++igmpstat.igps_rcv_ourreports;
279
280 switch (inm->inm_state) {
281 case IGMP_IDLE_MEMBER:
282 case IGMP_LAZY_MEMBER:
283 case IGMP_AWAKENING_MEMBER:
284 case IGMP_SLEEPING_MEMBER:
285 inm->inm_state = IGMP_SLEEPING_MEMBER;
286 break;
287 case IGMP_DELAYING_MEMBER:
288 if (inm->inm_rti->rti_type == IGMP_v1_ROUTER)
289 inm->inm_state = IGMP_LAZY_MEMBER;
290 else
291 inm->inm_state = IGMP_SLEEPING_MEMBER;
292 break;
293 }
294 }
295
296 break;
297
298 case IGMP_v2_HOST_MEMBERSHIP_REPORT:
299 #ifdef MROUTING
300 /*
301 * Make sure we don't hear our own membership report. Fast
302 * leave requires knowing that we are the only member of a
303 * group.
304 */
305 IFP_TO_IA(ifp, ia); /* XXX */
306 if (ia && in_hosteq(ip->ip_src, ia->ia_addr.sin_addr))
307 break;
308 #endif
309
310 ++igmpstat.igps_rcv_reports;
311
312 if (ifp->if_flags & IFF_LOOPBACK)
313 break;
314
315 if (!IN_MULTICAST(igmp->igmp_group.s_addr) ||
316 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
317 ++igmpstat.igps_rcv_badreports;
318 m_freem(m);
319 return;
320 }
321
322 /*
323 * KLUDGE: if the IP source address of the report has an
324 * unspecified (i.e., zero) subnet number, as is allowed for
325 * a booting host, replace it with the correct subnet number
326 * so that a process-level multicast routing daemon can
327 * determine which subnet it arrived from. This is necessary
328 * to compensate for the lack of any way for a process to
329 * determine the arrival interface of an incoming packet.
330 */
331 if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) {
332 #ifndef MROUTING
333 IFP_TO_IA(ifp, ia); /* XXX */
334 #endif
335 if (ia)
336 ip->ip_src.s_addr = ia->ia_subnet;
337 }
338
339 /*
340 * If we belong to the group being reported, stop
341 * our timer for that group.
342 */
343 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
344 if (inm != NULL) {
345 inm->inm_timer = 0;
346 ++igmpstat.igps_rcv_ourreports;
347
348 switch (inm->inm_state) {
349 case IGMP_DELAYING_MEMBER:
350 case IGMP_IDLE_MEMBER:
351 case IGMP_AWAKENING_MEMBER:
352 inm->inm_state = IGMP_LAZY_MEMBER;
353 break;
354 case IGMP_LAZY_MEMBER:
355 case IGMP_SLEEPING_MEMBER:
356 break;
357 }
358 }
359
360 break;
361
362 }
363
364 /*
365 * Pass all valid IGMP packets up to any process(es) listening
366 * on a raw IGMP socket.
367 */
368 rip_input(m);
369 }
370
371 void
372 igmp_joingroup(inm)
373 struct in_multi *inm;
374 {
375 int s = splsoftnet();
376
377 inm->inm_state = IGMP_IDLE_MEMBER;
378
379 if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
380 (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0) {
381 igmp_sendpkt(inm, rti_fill(inm));
382 inm->inm_state = IGMP_DELAYING_MEMBER;
383 inm->inm_timer = IGMP_RANDOM_DELAY(
384 IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ);
385 igmp_timers_are_running = 1;
386 } else
387 inm->inm_timer = 0;
388 splx(s);
389 }
390
391 void
392 igmp_leavegroup(inm)
393 struct in_multi *inm;
394 {
395
396 switch (inm->inm_state) {
397 case IGMP_DELAYING_MEMBER:
398 case IGMP_IDLE_MEMBER:
399 if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
400 (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0)
401 if (inm->inm_rti->rti_type != IGMP_v1_ROUTER)
402 igmp_sendpkt(inm, IGMP_HOST_LEAVE_MESSAGE);
403 break;
404 case IGMP_LAZY_MEMBER:
405 case IGMP_AWAKENING_MEMBER:
406 case IGMP_SLEEPING_MEMBER:
407 break;
408 }
409 }
410
411 void
412 igmp_fasttimo()
413 {
414 register struct in_multi *inm;
415 struct in_multistep step;
416 int s;
417
418 /*
419 * Quick check to see if any work needs to be done, in order
420 * to minimize the overhead of fasttimo processing.
421 */
422 if (!igmp_timers_are_running)
423 return;
424
425 s = splsoftnet();
426 igmp_timers_are_running = 0;
427 IN_FIRST_MULTI(step, inm);
428 while (inm != NULL) {
429 if (inm->inm_timer == 0) {
430 /* do nothing */
431 } else if (--inm->inm_timer == 0) {
432 if (inm->inm_state == IGMP_DELAYING_MEMBER) {
433 if (inm->inm_rti->rti_type == IGMP_v1_ROUTER)
434 igmp_sendpkt(inm,
435 IGMP_v1_HOST_MEMBERSHIP_REPORT);
436 else
437 igmp_sendpkt(inm,
438 IGMP_v2_HOST_MEMBERSHIP_REPORT);
439 inm->inm_state = IGMP_IDLE_MEMBER;
440 }
441 } else {
442 igmp_timers_are_running = 1;
443 }
444 IN_NEXT_MULTI(step, inm);
445 }
446 splx(s);
447 }
448
449 void
450 igmp_slowtimo()
451 {
452 register struct router_info *rti;
453 int s;
454
455 s = splsoftnet();
456 for (rti = rti_head; rti != 0; rti = rti->rti_next) {
457 if (rti->rti_type == IGMP_v1_ROUTER &&
458 ++rti->rti_age >= IGMP_AGE_THRESHOLD) {
459 rti->rti_type = IGMP_v2_ROUTER;
460 }
461 }
462 splx(s);
463 }
464
465 void
466 igmp_sendpkt(inm, type)
467 struct in_multi *inm;
468 int type;
469 {
470 struct mbuf *m;
471 struct igmp *igmp;
472 struct ip *ip;
473 struct ip_moptions imo;
474 #ifdef MROUTING
475 extern struct socket *ip_mrouter;
476 #endif /* MROUTING */
477
478 MGETHDR(m, M_DONTWAIT, MT_HEADER);
479 if (m == NULL)
480 return;
481 /*
482 * Assume max_linkhdr + sizeof(struct ip) + IGMP_MINLEN
483 * is smaller than mbuf size returned by MGETHDR.
484 */
485 m->m_data += max_linkhdr;
486 m->m_len = sizeof(struct ip) + IGMP_MINLEN;
487 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
488
489 ip = mtod(m, struct ip *);
490 ip->ip_tos = 0;
491 ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
492 ip->ip_off = 0;
493 ip->ip_p = IPPROTO_IGMP;
494 ip->ip_src = zeroin_addr;
495 ip->ip_dst = inm->inm_addr;
496
497 m->m_data += sizeof(struct ip);
498 m->m_len -= sizeof(struct ip);
499 igmp = mtod(m, struct igmp *);
500 igmp->igmp_type = type;
501 igmp->igmp_code = 0;
502 igmp->igmp_group = inm->inm_addr;
503 igmp->igmp_cksum = 0;
504 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
505 m->m_data -= sizeof(struct ip);
506 m->m_len += sizeof(struct ip);
507
508 imo.imo_multicast_ifp = inm->inm_ifp;
509 imo.imo_multicast_ttl = 1;
510 #ifdef RSVP_ISI
511 imo.imo_multicast_vif = -1;
512 #endif
513 /*
514 * Request loopback of the report if we are acting as a multicast
515 * router, so that the process-level routing demon can hear it.
516 */
517 #ifdef MROUTING
518 imo.imo_multicast_loop = (ip_mrouter != NULL);
519 #else
520 imo.imo_multicast_loop = 0;
521 #endif /* MROUTING */
522
523 ip_output(m, (struct mbuf *)0, (struct route *)0, IP_MULTICASTOPTS,
524 &imo);
525
526 ++igmpstat.igps_snd_reports;
527 }
528