forked from troglobit/mrouted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vif.c
1812 lines (1580 loc) · 47.5 KB
/
vif.c
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* The mrouted program is covered by the license in the accompanying file
* named "LICENSE". Use of the mrouted program represents acceptance of
* the terms and conditions listed in that file.
*
* The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
* Leland Stanford Junior University.
*/
#include "defs.h"
#include <fcntl.h>
/*
* Exported variables.
*/
struct uvif uvifs[MAXVIFS]; /* array of virtual interfaces */
vifi_t numvifs; /* number of vifs in use */
int vifs_down; /* 1=>some interfaces are down */
int phys_vif; /* An enabled vif */
int udp_socket; /* Since the honkin' kernel doesn't support */
/* ioctls on raw IP sockets, we need a UDP */
/* socket as well as our IGMP (raw) socket. */
/* How dumb. */
int vifs_with_neighbors; /* == 1 if I am a leaf */
/*
* Private variables.
*/
struct listaddr *nbrs[MAXNBRS]; /* array of neighbors */
typedef struct {
vifi_t vifi;
struct listaddr *g;
int q_time;
} cbk_t;
/*
* Forward declarations.
*/
static void start_vif(vifi_t vifi);
static void start_vif2(vifi_t vifi);
static void stop_vif(vifi_t vifi);
static void age_old_hosts(void);
static void send_probe_on_vif(struct uvif *v);
static void send_query(struct uvif *v);
static int info_version(uint8_t *p, size_t plen);
static void DelVif(void *arg);
static int SetTimer(vifi_t vifi, struct listaddr *g);
static int DeleteTimer(int id);
static void SendQuery(void *arg);
static int SetQueryTimer(struct listaddr *g, vifi_t vifi, int to_expire, int q_time);
/*
* Initialize the virtual interfaces, but do not install
* them in the kernel. Start routing on all vifs that are
* not down or disabled.
*/
void init_vifs(void)
{
vifi_t vifi;
struct uvif *v;
int enabled_vifs, enabled_phyints;
extern char *configfilename;
numvifs = 0;
vifs_with_neighbors = 0;
vifs_down = FALSE;
/*
* Configure the vifs based on the interface configuration of the
* the kernel and the contents of the configuration file.
* (Open a UDP socket for ioctl use in the config procedures if
* the kernel can't handle IOCTL's on the IGMP socket.)
*/
#ifdef IOCTL_OK_ON_RAW_SOCKET
udp_socket = igmp_socket;
#else
if ((udp_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
logit(LOG_ERR, errno, "UDP socket");
#endif
logit(LOG_INFO,0,"Getting vifs from kernel interfaces");
config_vifs_from_kernel();
logit(LOG_INFO,0,"Getting vifs from %s",configfilename);
config_vifs_from_file();
/*
* Quit if there are fewer than two enabled vifs.
*/
enabled_vifs = 0;
enabled_phyints = 0;
phys_vif = -1;
for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
if (!(v->uv_flags & VIFF_DISABLED)) {
++enabled_vifs;
if (!(v->uv_flags & VIFF_TUNNEL)) {
if (phys_vif == -1)
phys_vif = vifi;
++enabled_phyints;
}
}
}
if (enabled_vifs < 2)
logit(LOG_ERR, 0, "Cannot forward: %s",
enabled_vifs == 0 ? "no enabled vifs" : "only one enabled vif");
if (enabled_phyints == 0)
logit(LOG_WARNING, 0, "No enabled interfaces, forwarding via tunnels only");
logit(LOG_INFO, 0, "Installing vifs in mrouted ...");
for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
if (!(v->uv_flags & VIFF_DISABLED)) {
if (!(v->uv_flags & VIFF_DOWN)) {
if (v->uv_flags & VIFF_TUNNEL)
logit(LOG_INFO, 0, "vif #%d, tunnel %s -> %s", vifi,
inet_fmt(v->uv_lcl_addr, s1, sizeof(s1)),
inet_fmt(v->uv_rmt_addr, s2, sizeof(s2)));
else
logit(LOG_INFO, 0, "vif #%d, phyint %s", vifi,
inet_fmt(v->uv_lcl_addr, s1, sizeof(s1)));
start_vif2(vifi);
} else {
logit(LOG_INFO, 0, "%s is not yet up; vif #%u not in service",
v->uv_name, vifi);
}
}
}
}
/*
* Initialize the passed vif with all appropriate default values.
* "t" is true if a tunnel, or false if a phyint.
*/
void zero_vif(struct uvif *v, int t)
{
v->uv_flags = 0;
v->uv_metric = DEFAULT_METRIC;
v->uv_admetric = 0;
v->uv_threshold = DEFAULT_THRESHOLD;
v->uv_rate_limit = t ? DEFAULT_TUN_RATE_LIMIT : DEFAULT_PHY_RATE_LIMIT;
v->uv_lcl_addr = 0;
v->uv_rmt_addr = 0;
v->uv_dst_addr = t ? 0 : dvmrp_group;
v->uv_subnet = 0;
v->uv_subnetmask = 0;
v->uv_subnetbcast = 0;
v->uv_name[0] = '\0';
v->uv_groups = NULL;
v->uv_neighbors = NULL;
NBRM_CLRALL(v->uv_nbrmap);
v->uv_querier = NULL;
v->uv_igmpv1_warn = 0;
v->uv_prune_lifetime = 0;
v->uv_leaf_timer = 0;
v->uv_acl = NULL;
v->uv_addrs = NULL;
v->uv_filter = NULL;
v->uv_blasterbuf = NULL;
v->uv_blastercur = NULL;
v->uv_blasterend = NULL;
v->uv_blasterlen = 0;
v->uv_blastertimer = 0;
v->uv_nbrup = 0;
v->uv_icmp_warn = 0;
v->uv_nroutes = 0;
}
/*
* Start routing on all virtual interfaces that are not down or
* administratively disabled.
*/
void init_installvifs(void)
{
vifi_t vifi;
struct uvif *v;
logit(LOG_INFO, 0, "Installing vifs in kernel...");
for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
if (!(v->uv_flags & VIFF_DISABLED)) {
if (!(v->uv_flags & VIFF_DOWN)) {
if (v->uv_flags & VIFF_TUNNEL) {
logit(LOG_INFO, 0, "vif #%d, tunnel %s -> %s", vifi,
inet_fmt(v->uv_lcl_addr, s1, sizeof(s1)),
inet_fmt(v->uv_rmt_addr, s2, sizeof(s2)));
} else {
logit(LOG_INFO, 0, "vif #%d, phyint %s", vifi,
inet_fmt(v->uv_lcl_addr, s1, sizeof(s1)));
}
k_add_vif(vifi, &uvifs[vifi]);
} else {
logit(LOG_INFO, 0, "%s is not yet up; vif #%u not in service",
v->uv_name, vifi);
}
}
}
}
/*
* See if any interfaces have changed from up state to down, or vice versa,
* including any non-multicast-capable interfaces that are in use as local
* tunnel end-points. Ignore interfaces that have been administratively
* disabled.
*/
void check_vif_state(void)
{
vifi_t vifi;
struct uvif *v;
struct ifreq ifr;
static int checking_vifs = 0;
/*
* If we get an error while checking, (e.g. two interfaces go down
* at once, and we decide to send a prune out one of the failed ones)
* then don't go into an infinite loop!
*/
if (checking_vifs)
return;
vifs_down = FALSE;
checking_vifs = 1;
for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
if (v->uv_flags & VIFF_DISABLED)
continue;
memcpy(ifr.ifr_name, v->uv_name, sizeof(ifr.ifr_name));
if (ioctl(udp_socket, SIOCGIFFLAGS, (char *)&ifr) < 0)
logit(LOG_ERR, errno, "Failed ioctl SIOCGIFFLAGS for %s", ifr.ifr_name);
if (v->uv_flags & VIFF_DOWN) {
if (ifr.ifr_flags & IFF_UP) {
logit(LOG_NOTICE, 0, "%s has come up; vif #%u now in service",
v->uv_name, vifi);
v->uv_flags &= ~VIFF_DOWN;
start_vif(vifi);
} else {
vifs_down = TRUE;
}
} else {
if (!(ifr.ifr_flags & IFF_UP)) {
logit(LOG_NOTICE, 0, "%s has gone down; vif #%u taken out of service",
v->uv_name, vifi);
stop_vif(vifi);
v->uv_flags |= VIFF_DOWN;
vifs_down = TRUE;
}
}
}
checking_vifs = 0;
}
/*
* Send a DVMRP message on the specified vif. If DVMRP messages are
* to be encapsulated and sent "inside" the tunnel, use the special
* encapsulator. If it's not a tunnel or DVMRP messages are to be
* sent "beside" the tunnel, as required by earlier versions of mrouted,
* then just send the message.
*/
void send_on_vif(struct uvif *v, uint32_t dst, int code, size_t datalen)
{
uint32_t group = htonl(MROUTED_LEVEL | ((v->uv_flags & VIFF_LEAF) ? 0 : LEAF_FLAGS));
/*
* The UNIX kernel will not decapsulate unicasts.
* Therefore, we don't send encapsulated unicasts.
*/
if ((v->uv_flags & (VIFF_TUNNEL|VIFF_OTUNNEL)) == VIFF_TUNNEL &&
((dst == 0) || IN_MULTICAST(ntohl(dst))))
send_ipip(v->uv_lcl_addr, dst ? dst : dvmrp_group, IGMP_DVMRP,
code, group, datalen, v);
else
send_igmp(v->uv_lcl_addr, dst ? dst : v->uv_dst_addr, IGMP_DVMRP,
code, group, datalen);
}
/*
* Send a probe message on vif v
*/
static void send_probe_on_vif(struct uvif *v)
{
char *p;
size_t datalen = 0;
struct listaddr *nbr;
int i;
if ((v->uv_flags & VIFF_PASSIVE && v->uv_neighbors == NULL) ||
(v->uv_flags & VIFF_FORCE_LEAF))
return;
p = send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN;
for (i = 0; i < 4; i++)
*p++ = ((char *)&(dvmrp_genid))[i];
datalen += 4;
/*
* add the neighbor list on the interface to the message
*/
nbr = v->uv_neighbors;
while (nbr) {
for (i = 0; i < 4; i++)
*p++ = ((char *)&nbr->al_addr)[i];
datalen +=4;
nbr = nbr->al_next;
}
send_on_vif(v, 0, DVMRP_PROBE, datalen);
}
static void send_query(struct uvif *v)
{
IF_DEBUG(DEBUG_IGMP) {
logit(LOG_DEBUG, 0, "Sending %squery on vif %d",
(v->uv_flags & VIFF_IGMPV1) ? "v1 " : "",
v - uvifs);
}
send_igmp(v->uv_lcl_addr, allhosts_group,
IGMP_MEMBERSHIP_QUERY,
(v->uv_flags & VIFF_IGMPV1) ? 0 :
IGMP_MAX_HOST_REPORT_DELAY * IGMP_TIMER_SCALE, 0, 0);
}
/*
* Add a vifi to the kernel and start routing on it.
*/
static void start_vif(vifi_t vifi)
{
/*
* Install the interface in the kernel's vif structure.
*/
k_add_vif(vifi, &uvifs[vifi]);
start_vif2(vifi);
}
/*
* Add a vifi to all the user-level data structures but don't add
* it to the kernel yet.
*/
static void start_vif2(vifi_t vifi)
{
struct uvif *v;
uint32_t src;
struct phaddr *p;
v = &uvifs[vifi];
src = v->uv_lcl_addr;
/*
* Update the existing route entries to take into account the new vif.
*/
add_vif_to_routes(vifi);
if (!(v->uv_flags & VIFF_TUNNEL)) {
/*
* Join the DVMRP multicast group on the interface.
* (This is not strictly necessary, since the kernel promiscuously
* receives IGMP packets addressed to ANY IP multicast group while
* multicast routing is enabled. However, joining the group allows
* this host to receive non-IGMP packets as well, such as 'pings'.)
*/
k_join(dvmrp_group, src);
/*
* Join the ALL-ROUTERS multicast group on the interface.
* This allows mtrace requests to loop back if they are run
* on the multicast router.
*/
k_join(allrtrs_group, src);
/*
* Install an entry in the routing table for the subnet to which
* the interface is connected.
*/
start_route_updates();
update_route(v->uv_subnet, v->uv_subnetmask, 0, 0, vifi, NULL);
for (p = v->uv_addrs; p; p = p->pa_next) {
start_route_updates();
update_route(p->pa_subnet, p->pa_subnetmask, 0, 0, vifi, NULL);
}
/*
* Until neighbors are discovered, assume responsibility for sending
* periodic group membership queries to the subnet. Send the first
* query.
*/
v->uv_flags |= VIFF_QUERIER;
IF_DEBUG(DEBUG_IGMP) {
logit(LOG_DEBUG, 0, "Assuming querier duties on vif %d", vifi);
}
send_query(v);
}
v->uv_leaf_timer = LEAF_CONFIRMATION_TIME;
/*
* Send a probe via the new vif to look for neighbors.
*/
send_probe_on_vif(v);
}
/*
* Stop routing on the specified virtual interface.
*/
static void stop_vif(vifi_t vifi)
{
struct uvif *v;
struct listaddr *a;
struct phaddr *p;
v = &uvifs[vifi];
if (!(v->uv_flags & VIFF_TUNNEL)) {
/*
* Depart from the DVMRP multicast group on the interface.
*/
k_leave(dvmrp_group, v->uv_lcl_addr);
/*
* Depart from the ALL-ROUTERS multicast group on the interface.
*/
k_leave(allrtrs_group, v->uv_lcl_addr);
/*
* Update the entry in the routing table for the subnet to which
* the interface is connected, to take into account the interface
* failure.
*/
start_route_updates();
update_route(v->uv_subnet, v->uv_subnetmask, UNREACHABLE, 0, vifi, NULL);
for (p = v->uv_addrs; p; p = p->pa_next) {
start_route_updates();
update_route(p->pa_subnet, p->pa_subnetmask, UNREACHABLE, 0, vifi, NULL);
}
/*
* Discard all group addresses. (No need to tell kernel;
* the k_del_vif() call, below, will clean up kernel state.)
*/
while (v->uv_groups != NULL) {
a = v->uv_groups;
v->uv_groups = a->al_next;
free((char *)a);
}
IF_DEBUG(DEBUG_IGMP) {
logit(LOG_DEBUG, 0, "Releasing querier duties on vif %d", vifi);
}
v->uv_flags &= ~VIFF_QUERIER;
}
/*
* Update the existing route entries to take into account the vif failure.
*/
delete_vif_from_routes(vifi);
/*
* Delete the interface from the kernel's vif structure.
*/
k_del_vif(vifi, v);
/*
* Discard all neighbor addresses.
*/
if (!NBRM_ISEMPTY(v->uv_nbrmap))
vifs_with_neighbors--;
while (v->uv_neighbors != NULL) {
a = v->uv_neighbors;
v->uv_neighbors = a->al_next;
nbrs[a->al_index] = NULL;
free((char *)a);
}
NBRM_CLRALL(v->uv_nbrmap);
}
/*
* stop routing on all vifs
*/
void stop_all_vifs(void)
{
vifi_t vifi;
struct uvif *v;
struct listaddr *a;
struct vif_acl *acl;
for (vifi = 0; vifi < numvifs; vifi++) {
v = &uvifs[vifi];
while (v->uv_groups != NULL) {
a = v->uv_groups;
v->uv_groups = a->al_next;
free((char *)a);
}
while (v->uv_neighbors != NULL) {
a = v->uv_neighbors;
v->uv_neighbors = a->al_next;
nbrs[a->al_index] = NULL;
free((char *)a);
}
while (v->uv_acl != NULL) {
acl = v->uv_acl;
v->uv_acl = acl->acl_next;
free((char *)acl);
}
}
}
/*
* Find the virtual interface from which an incoming packet arrived,
* based on the packet's source and destination IP addresses.
*/
vifi_t find_vif(uint32_t src, uint32_t dst)
{
vifi_t vifi;
struct uvif *v;
struct phaddr *p;
for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
if (!(v->uv_flags & (VIFF_DOWN|VIFF_DISABLED))) {
if (v->uv_flags & VIFF_TUNNEL) {
if (src == v->uv_rmt_addr && (dst == v->uv_lcl_addr || dst == dvmrp_group))
return vifi;
} else {
if ((src & v->uv_subnetmask) == v->uv_subnet &&
(v->uv_subnetmask == 0xffffffff || src != v->uv_subnetbcast))
return vifi;
for (p = v->uv_addrs; p; p = p->pa_next) {
if ((src & p->pa_subnetmask) == p->pa_subnet &&
(p->pa_subnetmask == 0xffffffff || src != p->pa_subnetbcast))
return vifi;
}
}
}
}
return NO_VIF;
}
static void age_old_hosts(void)
{
vifi_t vifi;
struct uvif *v;
struct listaddr *g;
/*
* Decrement the old-hosts-present timer for each
* active group on each vif.
*/
for (vifi = 0, v = uvifs; vifi < numvifs; vifi++, v++) {
for (g = v->uv_groups; g != NULL; g = g->al_next) {
if (g->al_old)
g->al_old--;
}
}
}
/*
* Send group membership queries on each interface for which I am querier.
* Note that technically, there should be a timer per interface, as the
* dynamics of querier election can cause the "right" time to send a
* query to be different on different interfaces. However, this simple
* implementation only ever sends queries sooner than the "right" time,
* so can not cause loss of membership (but can send more packets than
* necessary)
*/
void query_groups(void)
{
vifi_t vifi;
struct uvif *v;
for (vifi = 0, v = uvifs; vifi < numvifs; vifi++, v++) {
if (v->uv_flags & VIFF_QUERIER) {
send_query(v);
}
}
age_old_hosts();
}
/*
* Process an incoming host membership query. Warn about
* IGMP version mismatches, perform querier election, and
* handle group-specific queries when we're not the querier.
*/
void accept_membership_query(uint32_t src, uint32_t dst, uint32_t group, int tmo)
{
vifi_t vifi;
struct uvif *v;
vifi = find_vif(src, dst);
if (vifi == NO_VIF || (uvifs[vifi].uv_flags & VIFF_TUNNEL)) {
logit(LOG_INFO, 0, "Ignoring group membership query from non-adjacent host %s",
inet_fmt(src, s1, sizeof(s1)));
return;
}
v = &uvifs[vifi];
if ((tmo == 0 && !(v->uv_flags & VIFF_IGMPV1)) ||
(tmo != 0 && (v->uv_flags & VIFF_IGMPV1))) {
int i;
/*
* Exponentially back-off warning rate
*/
i = ++v->uv_igmpv1_warn;
while (i && !(i & 1))
i >>= 1;
if (i == 1) {
logit(LOG_WARNING, 0, "Received IGMP%s report from %s on vif %d, %s",
tmo == 0 ? "v1" : "v2", inet_fmt(src, s1, sizeof(s1)), vifi,
tmo == 0 ? "please configure vif for IGMPv1" : "but I am configured for IGMPv1");
}
}
if (v->uv_querier == NULL || v->uv_querier->al_addr != src) {
/*
* This might be:
* - A query from a new querier, with a lower source address
* than the current querier (who might be me)
* - A query from a new router that just started up and doesn't
* know who the querier is.
*/
if (ntohl(src) < (v->uv_querier ? ntohl(v->uv_querier->al_addr) : ntohl(v->uv_lcl_addr))) {
IF_DEBUG(DEBUG_IGMP) {
logit(LOG_DEBUG, 0, "New querier %s (was %s) on vif %d", inet_fmt(src, s1, sizeof(s1)),
v->uv_querier ? inet_fmt(v->uv_querier->al_addr, s2, sizeof(s2)) : "me", vifi);
}
if (!v->uv_querier) {
v->uv_querier = (struct listaddr *)malloc(sizeof(struct listaddr));
v->uv_flags &= ~VIFF_QUERIER;
}
if (v->uv_querier) {
time(&v->uv_querier->al_ctime);
v->uv_querier->al_addr = src;
}
} else {
IF_DEBUG(DEBUG_IGMP) {
logit(LOG_DEBUG, 0, "Ignoring query from %s; querier on vif %d is still %s",
inet_fmt(src, s1, sizeof(s1)), vifi,
v->uv_querier ? inet_fmt(v->uv_querier->al_addr, s2, sizeof(s2)) : "me");
}
return;
}
}
/*
* Reset the timer since we've received a query.
*/
if (v->uv_querier && src == v->uv_querier->al_addr)
v->uv_querier->al_timer = 0;
/*
* If this is a Group-Specific query which we did not source,
* we must set our membership timer to [Last Member Query Count] *
* the [Max Response Time] in the packet.
*/
if (!(v->uv_flags & (VIFF_IGMPV1|VIFF_QUERIER))
&& group != 0 && src != v->uv_lcl_addr) {
struct listaddr *g;
IF_DEBUG(DEBUG_IGMP) {
logit(LOG_DEBUG, 0, "Group-specific membership query for %s from %s on vif %d, timer %d",
inet_fmt(group, s2, sizeof(s2)),
inet_fmt(src, s1, sizeof(s1)), vifi, tmo);
}
for (g = v->uv_groups; g != NULL; g = g->al_next) {
if (group == g->al_addr && g->al_query == 0) {
/* setup a timeout to remove the group membership */
if (g->al_timerid)
g->al_timerid = DeleteTimer(g->al_timerid);
/* use al_query to record our presence in last-member state */
g->al_timer = IGMP_LAST_MEMBER_QUERY_COUNT * tmo / IGMP_TIMER_SCALE;
g->al_query = -1;
g->al_timerid = SetTimer(vifi, g);
IF_DEBUG(DEBUG_IGMP) {
logit(LOG_DEBUG, 0, "Timer for grp %s on vif %d set to %d",
inet_fmt(group, s2, sizeof(s2)), vifi, g->al_timer);
}
break;
}
}
}
}
/*
* Process an incoming group membership report.
*/
void accept_group_report(uint32_t src, uint32_t dst, uint32_t group, int r_type)
{
vifi_t vifi;
struct uvif *v;
struct listaddr *g;
vifi = find_vif(src, dst);
if (vifi == NO_VIF || (uvifs[vifi].uv_flags & VIFF_TUNNEL)) {
logit(LOG_INFO, 0, "Ignoring group membership report from non-adjacent host %s",
inet_fmt(src, s1, sizeof(s1)));
return;
}
v = &uvifs[vifi];
/*
* Look for the group in our group list; if found, reset its timer.
*/
for (g = v->uv_groups; g; g = g->al_next) {
if (group == g->al_addr) {
if (r_type == IGMP_V1_MEMBERSHIP_REPORT)
g->al_old = OLD_AGE_THRESHOLD;
g->al_reporter = src;
/** delete old timers, set a timer for expiration **/
g->al_timer = IGMP_GROUP_MEMBERSHIP_INTERVAL;
if (g->al_query)
g->al_query = DeleteTimer(g->al_query);
if (g->al_timerid)
g->al_timerid = DeleteTimer(g->al_timerid);
g->al_timerid = SetTimer(vifi, g);
break;
}
}
/*
* If not found, add it to the list and update kernel cache.
*/
if (!g) {
g = (struct listaddr *)malloc(sizeof(struct listaddr));
if (!g) {
logit(LOG_ERR, 0, "Malloc failed in vif.c:accept_group_report()\n"); /* FATAL! */
return; /* NOTREACHED */
}
g->al_addr = group;
if (r_type == IGMP_V1_MEMBERSHIP_REPORT)
g->al_old = OLD_AGE_THRESHOLD;
else
g->al_old = 0;
/** set a timer for expiration **/
g->al_query = 0;
g->al_timer = IGMP_GROUP_MEMBERSHIP_INTERVAL;
g->al_reporter = src;
g->al_timerid = SetTimer(vifi, g);
g->al_next = v->uv_groups;
v->uv_groups = g;
time(&g->al_ctime);
update_lclgrp(vifi, group);
}
/*
* Check if a graft is necessary for this group
*/
chkgrp_graft(vifi, group);
}
/*
* Process an incoming IGMPv2 Leave Group message.
*/
void accept_leave_message(uint32_t src, uint32_t dst, uint32_t group)
{
vifi_t vifi;
struct uvif *v;
struct listaddr *g;
vifi = find_vif(src, dst);
if (vifi == NO_VIF || (uvifs[vifi].uv_flags & VIFF_TUNNEL)) {
logit(LOG_INFO, 0, "Ignoring group leave report from non-adjacent host %s",
inet_fmt(src, s1, sizeof(s1)));
return;
}
v = &uvifs[vifi];
if (!(v->uv_flags & VIFF_QUERIER) || (v->uv_flags & VIFF_IGMPV1))
return;
/*
* Look for the group in our group list in order to set up a short-timeout
* query.
*/
for (g = v->uv_groups; g != NULL; g = g->al_next) {
if (group == g->al_addr) {
IF_DEBUG(DEBUG_IGMP) {
logit(LOG_DEBUG, 0, "[vif.c, _accept_leave_message] %d %d \n",
g->al_old, g->al_query);
}
/* Ignore the leave message if there are old hosts present */
if (g->al_old)
return;
/* still waiting for a reply to a query, ignore the leave */
if (g->al_query)
return;
/** delete old timer set a timer for expiration **/
if (g->al_timerid)
g->al_timerid = DeleteTimer(g->al_timerid);
#if IGMP_LAST_MEMBER_QUERY_COUNT != 2
This code needs to be updated to keep a counter of the number
of queries remaining.
#endif
/** send a group specific querry **/
g->al_timer = IGMP_LAST_MEMBER_QUERY_INTERVAL *
(IGMP_LAST_MEMBER_QUERY_COUNT + 1);
send_igmp(v->uv_lcl_addr, g->al_addr,
IGMP_MEMBERSHIP_QUERY,
IGMP_LAST_MEMBER_QUERY_INTERVAL * IGMP_TIMER_SCALE,
g->al_addr, 0);
g->al_query = SetQueryTimer(g, vifi,
IGMP_LAST_MEMBER_QUERY_INTERVAL,
IGMP_LAST_MEMBER_QUERY_INTERVAL * IGMP_TIMER_SCALE);
g->al_timerid = SetTimer(vifi, g);
break;
}
}
}
/*
* Send a periodic probe on all vifs.
* Useful to determine one-way interfaces.
* Detect neighbor loss faster.
*/
void probe_for_neighbors(void)
{
vifi_t vifi;
struct uvif *v;
for (vifi = 0, v = uvifs; vifi < numvifs; vifi++, v++) {
if (!(v->uv_flags & (VIFF_DOWN|VIFF_DISABLED))) {
send_probe_on_vif(v);
}
}
}
/*
* Send a list of all of our neighbors to the requestor, `src'.
*/
void accept_neighbor_request(uint32_t src, uint32_t UNUSED dst)
{
vifi_t vifi;
struct uvif *v;
uint8_t *p, *ncount;
struct listaddr *la;
int datalen;
uint32_t temp_addr, them = src;
#define PUT_ADDR(a) temp_addr = ntohl(a); \
*p++ = temp_addr >> 24; \
*p++ = (temp_addr >> 16) & 0xFF; \
*p++ = (temp_addr >> 8) & 0xFF; \
*p++ = temp_addr & 0xFF;
p = (uint8_t *) (send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
datalen = 0;
for (vifi = 0, v = uvifs; vifi < numvifs; vifi++, v++) {
if (v->uv_flags & VIFF_DISABLED)
continue;
ncount = 0;
for (la = v->uv_neighbors; la; la = la->al_next) {
/* Make sure that there's room for this neighbor... */
if (datalen + (ncount == 0 ? 4 + 3 + 4 : 4) > MAX_DVMRP_DATA_LEN) {
send_igmp(INADDR_ANY, them, IGMP_DVMRP, DVMRP_NEIGHBORS,
htonl(MROUTED_LEVEL), datalen);
p = (uint8_t *) (send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
datalen = 0;
ncount = 0;
}
/* Put out the header for this neighbor list... */
if (ncount == 0) {
PUT_ADDR(v->uv_lcl_addr);
*p++ = v->uv_metric;
*p++ = v->uv_threshold;
ncount = p;
*p++ = 0;
datalen += 4 + 3;
}
PUT_ADDR(la->al_addr);
datalen += 4;
(*ncount)++;
}
}
if (datalen != 0)
send_igmp(INADDR_ANY, them, IGMP_DVMRP, DVMRP_NEIGHBORS,
htonl(MROUTED_LEVEL), datalen);
}
/*
* Send a list of all of our neighbors to the requestor, `src'.
*/
void accept_neighbor_request2(uint32_t src, uint32_t UNUSED dst)
{
vifi_t vifi;
struct uvif *v;
uint8_t *p, *ncount;
struct listaddr *la;
int datalen;
uint32_t them = src;
p = (uint8_t *) (send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
datalen = 0;
for (vifi = 0, v = uvifs; vifi < numvifs; vifi++, v++) {
uint16_t vflags = v->uv_flags;
uint8_t rflags = 0;
if (vflags & VIFF_TUNNEL)
rflags |= DVMRP_NF_TUNNEL;
if (vflags & VIFF_SRCRT)
rflags |= DVMRP_NF_SRCRT;
if (vflags & VIFF_DOWN)
rflags |= DVMRP_NF_DOWN;
if (vflags & VIFF_DISABLED)
rflags |= DVMRP_NF_DISABLED;
if (vflags & VIFF_QUERIER)
rflags |= DVMRP_NF_QUERIER;
if (vflags & VIFF_LEAF)
rflags |= DVMRP_NF_LEAF;
ncount = 0;
la = v->uv_neighbors;
if (la == NULL) {
/*
* include down & disabled interfaces and interfaces on
* leaf nets.
*/
if (rflags & DVMRP_NF_TUNNEL)
rflags |= DVMRP_NF_DOWN;
if (datalen > MAX_DVMRP_DATA_LEN - 12) {
send_igmp(INADDR_ANY, them, IGMP_DVMRP, DVMRP_NEIGHBORS2,
htonl(MROUTED_LEVEL), datalen);
p = (uint8_t *) (send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
datalen = 0;
}
*(uint32_t*)p = v->uv_lcl_addr;
p += 4;
*p++ = v->uv_metric;
*p++ = v->uv_threshold;
*p++ = rflags;
*p++ = 1;
*(uint32_t*)p = v->uv_rmt_addr;
p += 4;
datalen += 12;
} else {
for ( ; la; la = la->al_next) {
/* Make sure that there's room for this neighbor... */
if (datalen + (ncount == 0 ? 4+4+4 : 4) > MAX_DVMRP_DATA_LEN) {
send_igmp(INADDR_ANY, them, IGMP_DVMRP, DVMRP_NEIGHBORS2,
htonl(MROUTED_LEVEL), datalen);
p = (uint8_t *) (send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
datalen = 0;
ncount = 0;
}
/* Put out the header for this neighbor list... */
if (ncount == 0) {
/* If it's a one-way tunnel, mark it down. */
if (rflags & DVMRP_NF_TUNNEL && la->al_flags & NBRF_ONEWAY)
rflags |= DVMRP_NF_DOWN;
*(uint32_t*)p = v->uv_lcl_addr;
p += 4;
*p++ = v->uv_metric;
*p++ = v->uv_threshold;
*p++ = rflags;
ncount = p;
*p++ = 0;
datalen += 4 + 4;
}