1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
|
/*
* Copyright (c) 1988 Stephen Deering.
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Stephen Deering of Stanford University.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)igmp.c 8.1 (Berkeley) 7/19/93
* $Id: igmp.c,v 1.6 1994/10/31 06:36:47 pst Exp $
*/
/*
* Internet Group Management Protocol (IGMP) routines.
*
* Written by Steve Deering, Stanford, May 1988.
* Modified by Rosen Sharma, Stanford, Aug 1994.
*
* MULTICAST 1.4
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/protosw.h>
#include <sys/proc.h> /* XXX needed for sysctl.h */
#include <vm/vm.h> /* XXX needed for sysctl.h */
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/igmp.h>
#include <netinet/igmp_var.h>
extern struct ifnet loif;
struct igmpstat igmpstat;
static int igmp_timers_are_running = 0;
static u_long igmp_all_hosts_group;
static struct router_info *Head = 0;
static void igmp_sendpkt(struct in_multi *, int);
static void igmp_sendleave(struct in_multi *);
void
igmp_init()
{
/*
* To avoid byte-swapping the same value over and over again.
*/
igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
Head = (struct router_info *) 0;
}
int
fill_rti(inm)
struct in_multi *inm;
{
register struct router_info *rti = Head;
#ifdef IGMP_DEBUG
printf("[igmp.c, _fill_rti] --> entering \n");
#endif
while (rti) {
if (rti->ifp == inm->inm_ifp){ /* ? is it ok to compare */
/* pointers */
inm->inm_rti = rti;
#ifdef IGMP_DEBUG
printf("[igmp.c, _fill_rti] --> found old entry \n");
#endif
if (rti->type == IGMP_OLD_ROUTER)
return IGMP_HOST_MEMBERSHIP_REPORT;
else
return IGMP_HOST_NEW_MEMBERSHIP_REPORT;
}
rti = rti->next;
}
MALLOC(rti, struct router_info *, sizeof *rti, M_MRTABLE, M_NOWAIT);
rti->ifp = inm->inm_ifp;
rti->type = IGMP_NEW_ROUTER;
rti->time = IGMP_AGE_THRESHOLD;
rti->next = Head;
Head = rti;
inm->inm_rti = rti;
#ifdef IGMP_DEBUG
printf("[igmp.c, _fill_rti] --> created new entry \n");
#endif
return IGMP_HOST_NEW_MEMBERSHIP_REPORT;
}
struct router_info *
find_rti(ifp)
struct ifnet *ifp;
{
register struct router_info *rti = Head;
#ifdef IGMP_DEBUG
printf("[igmp.c, _find_rti] --> entering \n");
#endif
while (rti) {
if (rti->ifp == ifp){ /* ? is it ok to compare pointers */
#ifdef IGMP_DEBUG
printf("[igmp.c, _find_rti] --> found old entry \n");
#endif
return rti;
}
rti = rti->next;
}
MALLOC(rti, struct router_info *, sizeof *rti, M_MRTABLE, M_NOWAIT);
rti->ifp = ifp;
rti->type = IGMP_NEW_ROUTER;
rti->time = IGMP_AGE_THRESHOLD;
rti->next = Head;
Head = rti;
#ifdef IGMP_DEBUG
printf("[igmp.c, _find_rti] --> created an entry \n");
#endif
return rti;
}
void
igmp_input(m, iphlen)
register struct mbuf *m;
register int iphlen;
{
register struct igmp *igmp;
register struct ip *ip;
register int igmplen;
register struct ifnet *ifp = m->m_pkthdr.rcvif;
register int minlen;
register struct in_multi *inm;
register struct in_ifaddr *ia;
struct in_multistep step;
struct router_info *rti;
static int timer; /** timer value in the igmp query header **/
++igmpstat.igps_rcv_total;
ip = mtod(m, struct ip *);
igmplen = ip->ip_len;
/*
* Validate lengths
*/
if (igmplen < IGMP_MINLEN) {
++igmpstat.igps_rcv_tooshort;
m_freem(m);
return;
}
minlen = iphlen + IGMP_MINLEN;
if ((m->m_flags & M_EXT || m->m_len < minlen) &&
(m = m_pullup(m, minlen)) == 0) {
++igmpstat.igps_rcv_tooshort;
return;
}
/*
* Validate checksum
*/
m->m_data += iphlen;
m->m_len -= iphlen;
igmp = mtod(m, struct igmp *);
if (in_cksum(m, igmplen)) {
++igmpstat.igps_rcv_badsum;
m_freem(m);
return;
}
m->m_data -= iphlen;
m->m_len += iphlen;
ip = mtod(m, struct ip *);
timer = ntohs(igmp->igmp_code);
rti = find_rti(ifp);
switch (igmp->igmp_type) {
case IGMP_HOST_MEMBERSHIP_QUERY:
++igmpstat.igps_rcv_queries;
if (ifp == &loif)
break;
if (igmp->igmp_code == 0) {
rti->type = IGMP_OLD_ROUTER; rti->time = 0;
/*
** Do exactly as RFC 1112 says
*/
if (ip->ip_dst.s_addr != igmp_all_hosts_group) {
++igmpstat.igps_rcv_badqueries;
m_freem(m);
return;
}
/*
* Start the timers in all of our membership records for
* the interface on which the query arrived, except those
* that are already running and those that belong to the
* "all-hosts" group.
*/
IN_FIRST_MULTI(step, inm);
while (inm != NULL) {
if (inm->inm_ifp == ifp
&& inm->inm_timer == 0
&& inm->inm_addr.s_addr
!= igmp_all_hosts_group) {
inm->inm_state = IGMP_DELAYING_MEMBER;
inm->inm_timer = IGMP_RANDOM_DELAY(
IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ );
igmp_timers_are_running = 1;
}
IN_NEXT_MULTI(step, inm);
}
} else {
/*
** New Router
*/
if (ip->ip_dst.s_addr != igmp_all_hosts_group) {
if (!(m->m_flags & M_MCAST)) {
++igmpstat.igps_rcv_badqueries;
m_freem(m);
return;
}
}
if (ip->ip_dst.s_addr == igmp_all_hosts_group) {
/*
* - Start the timers in all of our membership records
* for the interface on which the query arrived
* excl. those that belong to the "all-hosts" group.
* - For timers already running check if they need to
* be reset.
* - Use the igmp->igmp_code filed as the maximum
* delay possible
*/
IN_FIRST_MULTI(step, inm);
while (inm != NULL){
switch(inm->inm_state){
case IGMP_IDLE_MEMBER:
case IGMP_LAZY_MEMBER:
case IGMP_AWAKENING_MEMBER:
if (inm->inm_ifp == ifp &&
inm->inm_addr.s_addr !=
igmp_all_hosts_group) {
inm->inm_timer = IGMP_RANDOM_DELAY(timer);
igmp_timers_are_running = 1;
inm->inm_state = IGMP_DELAYING_MEMBER;
}
break;
case IGMP_DELAYING_MEMBER:
if (inm->inm_ifp == ifp &&
(inm->inm_timer >
timer * PR_FASTHZ / IGMP_TIMER_SCALE)
&&
inm->inm_addr.s_addr !=
igmp_all_hosts_group) {
inm->inm_timer = IGMP_RANDOM_DELAY(timer);
igmp_timers_are_running = 1;
inm->inm_state = IGMP_DELAYING_MEMBER;
}
break;
case IGMP_SLEEPING_MEMBER:
inm->inm_state = IGMP_AWAKENING_MEMBER;
break;
}
IN_NEXT_MULTI(step, inm);
}
} else {
/*
** group specific query
*/
IN_FIRST_MULTI(step, inm);
while (inm != NULL) {
if (inm->inm_addr.s_addr == ip->ip_dst.s_addr) {
switch(inm->inm_state ){
case IGMP_IDLE_MEMBER:
case IGMP_LAZY_MEMBER:
case IGMP_AWAKENING_MEMBER:
inm->inm_state = IGMP_DELAYING_MEMBER;
if (inm->inm_ifp == ifp ) {
inm->inm_timer = IGMP_RANDOM_DELAY(timer);
igmp_timers_are_running = 1;
inm->inm_state = IGMP_DELAYING_MEMBER;
}
break;
case IGMP_DELAYING_MEMBER:
inm->inm_state = IGMP_DELAYING_MEMBER;
if (inm->inm_ifp == ifp &&
(inm->inm_timer >
timer * PR_FASTHZ / IGMP_TIMER_SCALE) ) {
inm->inm_timer = IGMP_RANDOM_DELAY(timer);
igmp_timers_are_running = 1;
inm->inm_state = IGMP_DELAYING_MEMBER;
}
break;
case IGMP_SLEEPING_MEMBER:
inm->inm_state = IGMP_AWAKENING_MEMBER;
break;
}
}
IN_NEXT_MULTI(step, inm);
}
}
}
break;
case IGMP_HOST_MEMBERSHIP_REPORT:
++igmpstat.igps_rcv_reports;
if (ifp == &loif)
break;
if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
igmp->igmp_group.s_addr != ip->ip_dst.s_addr) {
++igmpstat.igps_rcv_badreports;
m_freem(m);
return;
}
/*
* KLUDGE: if the IP source address of the report has an
* unspecified (i.e., zero) subnet number, as is allowed for
* a booting host, replace it with the correct subnet number
* so that a process-level multicast routing demon can
* determine which subnet it arrived from. This is necessary
* to compensate for the lack of any way for a process to
* determine the arrival interface of an incoming packet.
*/
if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0) {
IFP_TO_IA(ifp, ia);
if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
}
/*
* If we belong to the group being reported, stop
* our timer for that group.
*/
IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
if (inm != NULL) {
inm->inm_timer = 0;
++igmpstat.igps_rcv_ourreports;
}
if (inm != NULL) {
inm->inm_timer = 0;
++igmpstat.igps_rcv_ourreports;
switch(inm->inm_state){
case IGMP_IDLE_MEMBER:
case IGMP_LAZY_MEMBER:
case IGMP_AWAKENING_MEMBER:
case IGMP_SLEEPING_MEMBER:
inm->inm_state = IGMP_SLEEPING_MEMBER;
break;
case IGMP_DELAYING_MEMBER:
/** check this out - this was if (oldrouter) **/
if (inm->inm_rti->type == IGMP_OLD_ROUTER)
inm->inm_state = IGMP_LAZY_MEMBER;
else inm->inm_state = IGMP_SLEEPING_MEMBER;
break;
}
}
break;
case IGMP_HOST_NEW_MEMBERSHIP_REPORT:
/*
* an new report
*/
++igmpstat.igps_rcv_reports;
if (ifp == &loif)
break;
if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
igmp->igmp_group.s_addr != ip->ip_dst.s_addr) {
++igmpstat.igps_rcv_badreports;
m_freem(m);
return;
}
/*
* KLUDGE: if the IP source address of the report has an
* unspecified (i.e., zero) subnet number, as is allowed for
* a booting host, replace it with the correct subnet number
* so that a process-level multicast routing demon can
* determine which subnet it arrived from. This is necessary
* to compensate for the lack of any way for a process to
* determine the arrival interface of an incoming packet.
*/
if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0) {
IFP_TO_IA(ifp, ia);
if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
}
/*
* If we belong to the group being reported, stop
* our timer for that group.
*/
IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
if (inm != NULL) {
inm->inm_timer = 0;
++igmpstat.igps_rcv_ourreports;
switch(inm->inm_state){
case IGMP_DELAYING_MEMBER:
case IGMP_IDLE_MEMBER:
inm->inm_state = IGMP_LAZY_MEMBER;
break;
case IGMP_AWAKENING_MEMBER:
inm->inm_state = IGMP_LAZY_MEMBER;
break;
case IGMP_LAZY_MEMBER:
case IGMP_SLEEPING_MEMBER:
break;
}
}
}
/*
* Pass all valid IGMP packets up to any process(es) listening
* on a raw IGMP socket.
*/
rip_input(m);
}
void
igmp_joingroup(inm)
struct in_multi *inm;
{
register int s = splnet();
inm->inm_state = IGMP_IDLE_MEMBER;
if (inm->inm_addr.s_addr == igmp_all_hosts_group ||
inm->inm_ifp == &loif)
inm->inm_timer = 0;
else {
igmp_sendpkt(inm,fill_rti(inm));
inm->inm_timer = IGMP_RANDOM_DELAY(
IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ);
inm->inm_state = IGMP_DELAYING_MEMBER;
igmp_timers_are_running = 1;
}
splx(s);
}
void
igmp_leavegroup(inm)
struct in_multi *inm;
{
/*
* No action required on leaving a group.
*/
switch(inm->inm_state){
case IGMP_DELAYING_MEMBER:
case IGMP_IDLE_MEMBER:
if (!(inm->inm_addr.s_addr == igmp_all_hosts_group ||
inm->inm_ifp == &loif))
if (inm->inm_rti->type != IGMP_OLD_ROUTER)
igmp_sendleave(inm);
break;
case IGMP_LAZY_MEMBER:
case IGMP_AWAKENING_MEMBER:
case IGMP_SLEEPING_MEMBER:
break;
}
}
void
igmp_fasttimo()
{
register struct in_multi *inm;
register int s;
struct in_multistep step;
/*
* Quick check to see if any work needs to be done, in order
* to minimize the overhead of fasttimo processing.
*/
if (!igmp_timers_are_running)
return;
s = splnet();
igmp_timers_are_running = 0;
IN_FIRST_MULTI(step, inm);
while (inm != NULL) {
if (inm->inm_timer == 0) {
/* do nothing */
} else if (--inm->inm_timer == 0) {
if (inm->inm_state == IGMP_DELAYING_MEMBER) {
if (inm->inm_rti->type == IGMP_OLD_ROUTER)
igmp_sendpkt(inm, IGMP_HOST_MEMBERSHIP_REPORT);
else
igmp_sendpkt(inm, IGMP_HOST_NEW_MEMBERSHIP_REPORT);
inm->inm_state = IGMP_IDLE_MEMBER;
}
} else {
igmp_timers_are_running = 1;
}
IN_NEXT_MULTI(step, inm);
}
splx(s);
}
void
igmp_slowtimo()
{
int s = splnet();
register struct router_info *rti = Head;
#ifdef IGMP_DEBUG
printf("[igmp.c,_slowtimo] -- > entering \n");
#endif
while (rti) {
rti->time ++;
if (rti->time >= IGMP_AGE_THRESHOLD){
rti->type = IGMP_NEW_ROUTER;
rti->time = IGMP_AGE_THRESHOLD;
}
rti = rti->next;
}
#ifdef IGMP_DEBUG
printf("[igmp.c,_slowtimo] -- > exiting \n");
#endif
splx(s);
}
static void
igmp_sendpkt(inm, type)
struct in_multi *inm;
int type;
{
struct mbuf *m;
struct igmp *igmp;
struct ip *ip;
struct ip_moptions *imo;
MGETHDR(m, M_DONTWAIT, MT_HEADER);
if (m == NULL)
return;
MALLOC(imo, struct ip_moptions *, sizeof *imo, M_IPMOPTS, M_DONTWAIT);
if (!imo) {
m_free(m);
return;
}
m->m_pkthdr.rcvif = &loif;
m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
m->m_data += sizeof(struct ip);
m->m_len = IGMP_MINLEN;
igmp = mtod(m, struct igmp *);
igmp->igmp_type = type;
igmp->igmp_code = 0;
igmp->igmp_group = inm->inm_addr;
igmp->igmp_cksum = 0;
igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
m->m_data -= sizeof(struct ip);
m->m_len += sizeof(struct ip);
ip = mtod(m, struct ip *);
ip->ip_tos = 0;
ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
ip->ip_off = 0;
ip->ip_p = IPPROTO_IGMP;
ip->ip_src.s_addr = INADDR_ANY;
ip->ip_dst = igmp->igmp_group;
imo->imo_multicast_ifp = inm->inm_ifp;
imo->imo_multicast_ttl = 1;
/*
* Request loopback of the report if we are acting as a multicast
* router, so that the process-level routing demon can hear it.
*/
imo->imo_multicast_loop = (ip_mrouter != NULL);
ip_output(m, (struct mbuf *)0, (struct route *)0, 0, imo);
FREE(imo, M_IPMOPTS);
++igmpstat.igps_snd_reports;
}
static void
igmp_sendleave(inm)
struct in_multi *inm;
{
igmp_sendpkt(inm, IGMP_HOST_LEAVE_MESSAGE);
}
int
igmp_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
void *newp, size_t newlen)
{
/* All sysctl names at this level are terminal. */
if (namelen != 1)
return ENOTDIR; /* XXX overloaded */
switch(name[0]) {
case IGMPCTL_STATS:
return sysctl_rdstruct(oldp, oldlenp, newp, &igmpstat,
sizeof igmpstat);
default:
return ENOPROTOOPT;
}
}
|