-
Notifications
You must be signed in to change notification settings - Fork 887
/
Copy pathprune.c
2019 lines (1782 loc) · 50.7 KB
/
prune.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
/* $NetBSD: prune.c,v 1.3 1995/12/10 10:07:09 mycroft Exp $ */
/*
* 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"
extern int cache_lifetime;
extern int max_prune_lifetime;
extern struct rtentry *routing_table;
extern int phys_vif;
/*
* dither cache lifetime to obtain a value between x and 2*x
*/
#define CACHE_LIFETIME(x) ((x) + (arc4random_uniform(x)))
struct gtable *kernel_table; /* ptr to list of kernel grp entries*/
static struct gtable *kernel_no_route; /* list of grp entries w/o routes */
struct gtable *gtp; /* pointer for kernel rt entries */
unsigned int kroutes; /* current number of cache entries */
/****************************************************************************
Functions that are local to prune.c
****************************************************************************/
static void prun_add_ttls(struct gtable *gt);
static int pruning_neighbor(vifi_t vifi, u_int32_t addr);
static int can_mtrace(vifi_t vifi, u_int32_t addr);
static struct ptable * find_prune_entry(u_int32_t vr, struct ptable *pt);
static void expire_prune(vifi_t vifi, struct gtable *gt);
static void send_prune(struct gtable *gt);
static void send_graft(struct gtable *gt);
static void send_graft_ack(u_int32_t src, u_int32_t dst,
u_int32_t origin, u_int32_t grp);
static void update_kernel(struct gtable *g);
static char * scaletime(time_t t);
/*
* Updates the ttl values for each vif.
*/
static void
prun_add_ttls(struct gtable *gt)
{
struct uvif *v;
vifi_t vifi;
for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
if (VIFM_ISSET(vifi, gt->gt_grpmems))
gt->gt_ttls[vifi] = v->uv_threshold;
else
gt->gt_ttls[vifi] = 0;
}
}
/*
* checks for scoped multicast addresses
*/
#define GET_SCOPE(gt) { \
vifi_t _i; \
if ((ntohl((gt)->gt_mcastgrp) & 0xff000000) == 0xef000000) \
for (_i = 0; _i < numvifs; _i++) \
if (scoped_addr(_i, (gt)->gt_mcastgrp)) \
VIFM_SET(_i, (gt)->gt_scope); \
}
int
scoped_addr(vifi_t vifi, u_int32_t addr)
{
struct vif_acl *acl;
for (acl = uvifs[vifi].uv_acl; acl; acl = acl->acl_next)
if ((addr & acl->acl_mask) == acl->acl_addr)
return 1;
return 0;
}
/*
* Determine if mcastgrp has a listener on vifi
*/
int
grplst_mem(vifi_t vifi, u_int32_t mcastgrp)
{
struct listaddr *g;
struct uvif *v;
v = &uvifs[vifi];
for (g = v->uv_groups; g != NULL; g = g->al_next)
if (mcastgrp == g->al_addr)
return 1;
return 0;
}
/*
* Finds the group entry with the specified source and netmask.
* If netmask is 0, it uses the route's netmask.
*
* Returns TRUE if found a match, and the global variable gtp is left
* pointing to entry before the found entry.
* Returns FALSE if no exact match found, gtp is left pointing to before
* the entry in question belongs, or is NULL if the it belongs at the
* head of the list.
*/
int
find_src_grp(u_int32_t src, u_int32_t mask, u_int32_t grp)
{
struct gtable *gt;
gtp = NULL;
gt = kernel_table;
while (gt != NULL) {
if (grp == gt->gt_mcastgrp &&
(mask ? (gt->gt_route->rt_origin == src &&
gt->gt_route->rt_originmask == mask) :
((src & gt->gt_route->rt_originmask) ==
gt->gt_route->rt_origin)))
return TRUE;
if (ntohl(grp) > ntohl(gt->gt_mcastgrp) ||
(grp == gt->gt_mcastgrp &&
(ntohl(mask) < ntohl(gt->gt_route->rt_originmask) ||
(mask == gt->gt_route->rt_originmask &&
(ntohl(src) > ntohl(gt->gt_route->rt_origin)))))) {
gtp = gt;
gt = gt->gt_gnext;
}
else break;
}
return FALSE;
}
/*
* Check if the neighbor supports pruning
*/
static int
pruning_neighbor(vifi_t vifi, u_int32_t addr)
{
struct listaddr *n = neighbor_info(vifi, addr);
int vers;
if (n == NULL)
return 0;
if (n->al_flags & NF_PRUNE)
return 1;
/*
* Versions from 3.0 to 3.4 relied on the version number to identify
* that they could handle pruning.
*/
vers = NBR_VERS(n);
return (vers >= 0x0300 && vers <= 0x0304);
}
/*
* Can the neighbor in question handle multicast traceroute?
*/
static int
can_mtrace(vifi_t vifi, u_int32_t addr)
{
struct listaddr *n = neighbor_info(vifi, addr);
int vers;
if (n == NULL)
return 0;
if (n->al_flags & NF_MTRACE)
return 1;
/*
* Versions 3.3 and 3.4 relied on the version number to identify
* that they could handle traceroute.
*/
vers = NBR_VERS(n);
return (vers >= 0x0303 && vers <= 0x0304);
}
/*
* Returns the prune entry of the router, or NULL if none exists
*/
static struct ptable *
find_prune_entry(u_int32_t vr, struct ptable *pt)
{
while (pt) {
if (pt->pt_router == vr)
return pt;
pt = pt->pt_next;
}
return NULL;
}
/*
* Send a prune message to the dominant router for
* this source.
*
* Record an entry that a prune was sent for this group
*/
static void
send_prune(struct gtable *gt)
{
struct ptable *pt;
char *p;
int i;
int datalen;
u_int32_t src;
u_int32_t dst;
u_int32_t tmp;
/* Don't process any prunes if router is not pruning */
if (pruning == 0)
return;
/* Can't process a prune if we don't have an associated route */
if (gt->gt_route == NULL)
return;
/* Don't send a prune to a non-pruning router */
if (!pruning_neighbor(gt->gt_route->rt_parent, gt->gt_route->rt_gateway))
return;
/*
* sends a prune message to the router upstream.
*/
src = uvifs[gt->gt_route->rt_parent].uv_lcl_addr;
dst = gt->gt_route->rt_gateway;
p = send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN;
datalen = 0;
/*
* determine prune lifetime
*/
gt->gt_prsent_timer = gt->gt_timer;
for (pt = gt->gt_pruntbl; pt; pt = pt->pt_next)
if (pt->pt_timer < gt->gt_prsent_timer)
gt->gt_prsent_timer = pt->pt_timer;
/*
* If we have a graft pending, cancel graft retransmission
*/
gt->gt_grftsnt = 0;
for (i = 0; i < 4; i++)
*p++ = ((char *)&(gt->gt_route->rt_origin))[i];
for (i = 0; i < 4; i++)
*p++ = ((char *)&(gt->gt_mcastgrp))[i];
tmp = htonl(gt->gt_prsent_timer);
for (i = 0; i < 4; i++)
*p++ = ((char *)&(tmp))[i];
datalen += 12;
send_igmp(src, dst, IGMP_DVMRP, DVMRP_PRUNE,
htonl(MROUTED_LEVEL), datalen);
logit(LOG_DEBUG, 0, "sent prune for (%s %s)/%d on vif %d to %s",
inet_fmts(gt->gt_route->rt_origin, gt->gt_route->rt_originmask, s1),
inet_fmt(gt->gt_mcastgrp, s2),
gt->gt_prsent_timer, gt->gt_route->rt_parent,
inet_fmt(gt->gt_route->rt_gateway, s3));
}
/*
* a prune was sent upstream
* so, a graft has to be sent to annul the prune
* set up a graft timer so that if an ack is not
* heard within that time, another graft request
* is sent out.
*/
static void
send_graft(struct gtable *gt)
{
char *p;
int i;
int datalen;
u_int32_t src;
u_int32_t dst;
/* Can't send a graft without an associated route */
if (gt->gt_route == NULL)
return;
src = uvifs[gt->gt_route->rt_parent].uv_lcl_addr;
dst = gt->gt_route->rt_gateway;
p = send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN;
datalen = 0;
for (i = 0; i < 4; i++)
*p++ = ((char *)&(gt->gt_route->rt_origin))[i];
for (i = 0; i < 4; i++)
*p++ = ((char *)&(gt->gt_mcastgrp))[i];
datalen += 8;
if (datalen != 0) {
send_igmp(src, dst, IGMP_DVMRP, DVMRP_GRAFT,
htonl(MROUTED_LEVEL), datalen);
}
logit(LOG_DEBUG, 0, "sent graft for (%s %s) to %s on vif %d",
inet_fmts(gt->gt_route->rt_origin, gt->gt_route->rt_originmask, s1),
inet_fmt(gt->gt_mcastgrp, s2),
inet_fmt(gt->gt_route->rt_gateway, s3), gt->gt_route->rt_parent);
}
/*
* Send an ack that a graft was received
*/
static void
send_graft_ack(u_int32_t src, u_int32_t dst, u_int32_t origin, u_int32_t grp)
{
char *p;
int i;
int datalen;
p = send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN;
datalen = 0;
for (i = 0; i < 4; i++)
*p++ = ((char *)&(origin))[i];
for (i = 0; i < 4; i++)
*p++ = ((char *)&(grp))[i];
datalen += 8;
send_igmp(src, dst, IGMP_DVMRP, DVMRP_GRAFT_ACK,
htonl(MROUTED_LEVEL), datalen);
logit(LOG_DEBUG, 0, "sent graft ack for (%s, %s) to %s",
inet_fmt(origin, s1), inet_fmt(grp, s2), inet_fmt(dst, s3));
}
/*
* Update the kernel cache with all the routes hanging off the group entry
*/
static void
update_kernel(struct gtable *g)
{
struct stable *st;
for (st = g->gt_srctbl; st; st = st->st_next)
k_add_rg(st->st_origin, g);
}
/****************************************************************************
Functions that are used externally
****************************************************************************/
/*
* Initialize the kernel table structure
*/
void
init_ktable(void)
{
kernel_table = NULL;
kernel_no_route = NULL;
kroutes = 0;
}
/*
* Add a new table entry for (origin, mcastgrp)
*/
void
add_table_entry(u_int32_t origin, u_int32_t mcastgrp)
{
struct rtentry *r;
struct gtable *gt,**gtnp,*prev_gt;
struct stable *st,**stnp;
vifi_t i;
#ifdef DEBUG_MFC
md_logit(MD_MISS, origin, mcastgrp);
#endif
r = determine_route(origin);
prev_gt = NULL;
if (r == NULL) {
/*
* Look for it on the no_route table; if it is found then
* it will be detected as a duplicate below.
*/
for (gt = kernel_no_route; gt; gt = gt->gt_next)
if (mcastgrp == gt->gt_mcastgrp &&
gt->gt_srctbl && gt->gt_srctbl->st_origin == origin)
break;
gtnp = &kernel_no_route;
} else {
gtnp = &r->rt_groups;
while ((gt = *gtnp) != NULL) {
if (gt->gt_mcastgrp >= mcastgrp)
break;
gtnp = >->gt_next;
prev_gt = gt;
}
}
if (gt == NULL || gt->gt_mcastgrp != mcastgrp) {
gt = malloc(sizeof(struct gtable));
if (gt == NULL)
logit(LOG_ERR, 0, "ran out of memory");
gt->gt_mcastgrp = mcastgrp;
gt->gt_timer = CACHE_LIFETIME(cache_lifetime);
time(>->gt_ctime);
gt->gt_grpmems = 0;
gt->gt_scope = 0;
gt->gt_prsent_timer = 0;
gt->gt_grftsnt = 0;
gt->gt_srctbl = NULL;
gt->gt_pruntbl = NULL;
gt->gt_route = r;
#ifdef RSRR
gt->gt_rsrr_cache = NULL;
#endif
if (r != NULL) {
/* obtain the multicast group membership list */
for (i = 0; i < numvifs; i++) {
if (VIFM_ISSET(i, r->rt_children) &&
!(VIFM_ISSET(i, r->rt_leaves)))
VIFM_SET(i, gt->gt_grpmems);
if (VIFM_ISSET(i, r->rt_leaves) && grplst_mem(i, mcastgrp))
VIFM_SET(i, gt->gt_grpmems);
}
GET_SCOPE(gt);
if (VIFM_ISSET(r->rt_parent, gt->gt_scope))
gt->gt_scope = -1;
gt->gt_grpmems &= ~gt->gt_scope;
} else {
gt->gt_scope = -1;
gt->gt_grpmems = 0;
}
/* update ttls */
prun_add_ttls(gt);
gt->gt_next = *gtnp;
*gtnp = gt;
if (gt->gt_next)
gt->gt_next->gt_prev = gt;
gt->gt_prev = prev_gt;
if (r) {
if (find_src_grp(r->rt_origin, r->rt_originmask, gt->gt_mcastgrp)) {
struct gtable *g;
g = gtp ? gtp->gt_gnext : kernel_table;
logit(LOG_WARNING, 0, "Entry for (%s %s) (rt:%x) exists (rt:%x)",
inet_fmts(r->rt_origin, r->rt_originmask, s1),
inet_fmt(g->gt_mcastgrp, s2),
r, g->gt_route);
} else {
if (gtp) {
gt->gt_gnext = gtp->gt_gnext;
gt->gt_gprev = gtp;
gtp->gt_gnext = gt;
} else {
gt->gt_gnext = kernel_table;
gt->gt_gprev = NULL;
kernel_table = gt;
}
if (gt->gt_gnext)
gt->gt_gnext->gt_gprev = gt;
}
} else {
gt->gt_gnext = gt->gt_gprev = NULL;
}
}
stnp = >->gt_srctbl;
while ((st = *stnp) != NULL) {
if (ntohl(st->st_origin) >= ntohl(origin))
break;
stnp = &st->st_next;
}
if (st == NULL || st->st_origin != origin) {
st = malloc(sizeof(struct stable));
if (st == NULL)
logit(LOG_ERR, 0, "ran out of memory");
st->st_origin = origin;
st->st_pktcnt = 0;
st->st_next = *stnp;
*stnp = st;
} else {
#ifdef DEBUG_MFC
md_logit(MD_DUPE, origin, mcastgrp);
#endif
logit(LOG_WARNING, 0, "kernel entry already exists for (%s %s)",
inet_fmt(origin, s1), inet_fmt(mcastgrp, s2));
/* XXX Doing this should cause no harm, and may ensure
* kernel<>mrouted synchronization */
k_add_rg(origin, gt);
return;
}
kroutes++;
k_add_rg(origin, gt);
logit(LOG_DEBUG, 0, "add cache entry (%s %s) gm:%x, parent-vif:%d",
inet_fmt(origin, s1),
inet_fmt(mcastgrp, s2),
gt->gt_grpmems, r ? r->rt_parent : -1);
/* If there are no leaf vifs
* which have this group, then
* mark this src-grp as a prune candidate.
*/
if (!gt->gt_prsent_timer && !gt->gt_grpmems && r && r->rt_gateway)
send_prune(gt);
}
/*
* An mrouter has gone down and come up on an interface
* Forward on that interface immediately
*/
void
reset_neighbor_state(vifi_t vifi, u_int32_t addr)
{
struct rtentry *r;
struct gtable *g;
struct ptable *pt, **ptnp;
struct stable *st;
for (g = kernel_table; g; g = g->gt_gnext) {
r = g->gt_route;
/*
* If neighbor was the parent, remove the prune sent state
* and all of the source cache info so that prunes get
* regenerated.
*/
if (vifi == r->rt_parent) {
if (addr == r->rt_gateway) {
logit(LOG_DEBUG, 0, "reset_neighbor_state parent reset (%s %s)",
inet_fmts(r->rt_origin, r->rt_originmask, s1),
inet_fmt(g->gt_mcastgrp, s2));
g->gt_prsent_timer = 0;
g->gt_grftsnt = 0;
while ((st = g->gt_srctbl)) {
g->gt_srctbl = st->st_next;
k_del_rg(st->st_origin, g);
kroutes--;
free(st);
}
}
} else {
/*
* Neighbor was not the parent, send grafts to join the groups
*/
if (g->gt_prsent_timer) {
g->gt_grftsnt = 1;
send_graft(g);
g->gt_prsent_timer = 0;
}
/*
* Remove any prunes that this router has sent us.
*/
ptnp = &g->gt_pruntbl;
while ((pt = *ptnp) != NULL) {
if (pt->pt_vifi == vifi && pt->pt_router == addr) {
*ptnp = pt->pt_next;
free(pt);
} else
ptnp = &pt->pt_next;
}
/*
* And see if we want to forward again.
*/
if (!VIFM_ISSET(vifi, g->gt_grpmems)) {
if (VIFM_ISSET(vifi, r->rt_children) &&
!(VIFM_ISSET(vifi, r->rt_leaves)))
VIFM_SET(vifi, g->gt_grpmems);
if (VIFM_ISSET(vifi, r->rt_leaves) &&
grplst_mem(vifi, g->gt_mcastgrp))
VIFM_SET(vifi, g->gt_grpmems);
g->gt_grpmems &= ~g->gt_scope;
prun_add_ttls(g);
/* Update kernel state */
update_kernel(g);
#ifdef RSRR
/* Send route change notification to reservation protocol. */
rsrr_cache_send(g,1);
#endif /* RSRR */
logit(LOG_DEBUG, 0, "reset member state (%s %s) gm:%x",
inet_fmts(r->rt_origin, r->rt_originmask, s1),
inet_fmt(g->gt_mcastgrp, s2), g->gt_grpmems);
}
}
}
}
/*
* Delete table entry from the kernel
* del_flag determines how many entries to delete
*/
void
del_table_entry(struct rtentry *r, u_int32_t mcastgrp, u_int del_flag)
{
struct gtable *g, *prev_g;
struct stable *st, *prev_st;
struct ptable *pt, *prev_pt;
if (del_flag == DEL_ALL_ROUTES) {
g = r->rt_groups;
while (g) {
logit(LOG_DEBUG, 0, "del_table_entry deleting (%s %s)",
inet_fmts(r->rt_origin, r->rt_originmask, s1),
inet_fmt(g->gt_mcastgrp, s2));
st = g->gt_srctbl;
while (st) {
if (k_del_rg(st->st_origin, g) < 0) {
logit(LOG_WARNING, errno,
"del_table_entry trying to delete (%s, %s)",
inet_fmt(st->st_origin, s1),
inet_fmt(g->gt_mcastgrp, s2));
}
kroutes--;
prev_st = st;
st = st->st_next;
free(prev_st);
}
g->gt_srctbl = NULL;
pt = g->gt_pruntbl;
while (pt) {
prev_pt = pt;
pt = pt->pt_next;
free(prev_pt);
}
g->gt_pruntbl = NULL;
if (g->gt_gnext)
g->gt_gnext->gt_gprev = g->gt_gprev;
if (g->gt_gprev)
g->gt_gprev->gt_gnext = g->gt_gnext;
else
kernel_table = g->gt_gnext;
#ifdef RSRR
/* Send route change notification to reservation protocol. */
rsrr_cache_send(g,0);
rsrr_cache_clean(g);
#endif /* RSRR */
prev_g = g;
g = g->gt_next;
free(prev_g);
}
r->rt_groups = NULL;
}
/*
* Dummy routine - someday this may be needed, so it is just there
*/
if (del_flag == DEL_RTE_GROUP) {
prev_g = (struct gtable *)&r->rt_groups;
for (g = r->rt_groups; g; g = g->gt_next) {
if (g->gt_mcastgrp == mcastgrp) {
logit(LOG_DEBUG, 0, "del_table_entry deleting (%s %s)",
inet_fmts(r->rt_origin, r->rt_originmask, s1),
inet_fmt(g->gt_mcastgrp, s2));
st = g->gt_srctbl;
while (st) {
if (k_del_rg(st->st_origin, g) < 0) {
logit(LOG_WARNING, errno,
"del_table_entry trying to delete (%s, %s)",
inet_fmt(st->st_origin, s1),
inet_fmt(g->gt_mcastgrp, s2));
}
kroutes--;
prev_st = st;
st = st->st_next;
free(prev_st);
}
g->gt_srctbl = NULL;
pt = g->gt_pruntbl;
while (pt) {
prev_pt = pt;
pt = pt->pt_next;
free(prev_pt);
}
g->gt_pruntbl = NULL;
if (g->gt_gnext)
g->gt_gnext->gt_gprev = g->gt_gprev;
if (g->gt_gprev)
g->gt_gprev->gt_gnext = g->gt_gnext;
else
kernel_table = g->gt_gnext;
if (prev_g != (struct gtable *)&r->rt_groups)
g->gt_next->gt_prev = prev_g;
else
g->gt_next->gt_prev = NULL;
prev_g->gt_next = g->gt_next;
#ifdef RSRR
/* Send route change notification to reservation protocol. */
rsrr_cache_send(g,0);
rsrr_cache_clean(g);
#endif /* RSRR */
free(g);
g = prev_g;
} else {
prev_g = g;
}
}
}
}
/*
* update kernel table entry when a route entry changes
*/
void
update_table_entry(struct rtentry *r)
{
struct gtable *g;
struct ptable *pt, *prev_pt;
vifi_t i;
for (g = r->rt_groups; g; g = g->gt_next) {
pt = g->gt_pruntbl;
while (pt) {
prev_pt = pt->pt_next;
free(pt);
pt = prev_pt;
}
g->gt_pruntbl = NULL;
g->gt_grpmems = 0;
/* obtain the multicast group membership list */
for (i = 0; i < numvifs; i++) {
if (VIFM_ISSET(i, r->rt_children) &&
!(VIFM_ISSET(i, r->rt_leaves)))
VIFM_SET(i, g->gt_grpmems);
if (VIFM_ISSET(i, r->rt_leaves) && grplst_mem(i, g->gt_mcastgrp))
VIFM_SET(i, g->gt_grpmems);
}
if (VIFM_ISSET(r->rt_parent, g->gt_scope))
g->gt_scope = -1;
g->gt_grpmems &= ~g->gt_scope;
logit(LOG_DEBUG, 0, "updating cache entries (%s %s) gm:%x",
inet_fmts(r->rt_origin, r->rt_originmask, s1),
inet_fmt(g->gt_mcastgrp, s2),
g->gt_grpmems);
if (g->gt_grpmems && g->gt_prsent_timer) {
g->gt_grftsnt = 1;
send_graft(g);
g->gt_prsent_timer = 0;
}
/* update ttls and add entry into kernel */
prun_add_ttls(g);
update_kernel(g);
#ifdef RSRR
/* Send route change notification to reservation protocol. */
rsrr_cache_send(g,1);
#endif /* RSRR */
/* Check if we want to prune this group */
if (!g->gt_prsent_timer && g->gt_grpmems == 0 && r->rt_gateway) {
g->gt_timer = CACHE_LIFETIME(cache_lifetime);
send_prune(g);
}
}
}
/*
* set the forwarding flag for all mcastgrps on this vifi
*/
void
update_lclgrp(vifi_t vifi, u_int32_t mcastgrp)
{
struct rtentry *r;
struct gtable *g;
logit(LOG_DEBUG, 0, "group %s joined on vif %d",
inet_fmt(mcastgrp, s1), vifi);
for (g = kernel_table; g; g = g->gt_gnext) {
if (ntohl(mcastgrp) < ntohl(g->gt_mcastgrp))
break;
r = g->gt_route;
if (g->gt_mcastgrp == mcastgrp &&
VIFM_ISSET(vifi, r->rt_children)) {
VIFM_SET(vifi, g->gt_grpmems);
g->gt_grpmems &= ~g->gt_scope;
if (g->gt_grpmems == 0)
continue;
prun_add_ttls(g);
logit(LOG_DEBUG, 0, "update lclgrp (%s %s) gm:%x",
inet_fmts(r->rt_origin, r->rt_originmask, s1),
inet_fmt(g->gt_mcastgrp, s2), g->gt_grpmems);
update_kernel(g);
#ifdef RSRR
/* Send route change notification to reservation protocol. */
rsrr_cache_send(g,1);
#endif /* RSRR */
}
}
}
/*
* reset forwarding flag for all mcastgrps on this vifi
*/
void
delete_lclgrp(vifi_t vifi, u_int32_t mcastgrp)
{
struct rtentry *r;
struct gtable *g;
logit(LOG_DEBUG, 0, "group %s left on vif %d",
inet_fmt(mcastgrp, s1), vifi);
for (g = kernel_table; g; g = g->gt_gnext) {
if (ntohl(mcastgrp) < ntohl(g->gt_mcastgrp))
break;
if (g->gt_mcastgrp == mcastgrp) {
int stop_sending = 1;
r = g->gt_route;
/*
* If this is not a leaf, then we have router neighbors on this
* vif. Only turn off forwarding if they have all pruned.
*/
if (!VIFM_ISSET(vifi, r->rt_leaves)) {
struct listaddr *vr;
for (vr = uvifs[vifi].uv_neighbors; vr; vr = vr->al_next)
if (find_prune_entry(vr->al_addr, g->gt_pruntbl) == NULL) {
stop_sending = 0;
break;
}
}
if (stop_sending) {
VIFM_CLR(vifi, g->gt_grpmems);
logit(LOG_DEBUG, 0, "delete lclgrp (%s %s) gm:%x",
inet_fmts(r->rt_origin, r->rt_originmask, s1),
inet_fmt(g->gt_mcastgrp, s2), g->gt_grpmems);
prun_add_ttls(g);
update_kernel(g);
#ifdef RSRR
/* Send route change notification to reservation protocol. */
rsrr_cache_send(g,1);
#endif /* RSRR */
/*
* If there are no more members of this particular group,
* send prune upstream
*/
if (!g->gt_prsent_timer && g->gt_grpmems == 0 && r->rt_gateway)
send_prune(g);
}
}
}
}
/*
* Takes the prune message received and then strips it to
* determine the (src, grp) pair to be pruned.
*
* Adds the router to the (src, grp) entry then.
*
* Determines if further packets have to be sent down that vif
*
* Determines if a corresponding prune message has to be generated
*/
void
accept_prune(u_int32_t src, u_int32_t dst, char *p, int datalen)
{
u_int32_t prun_src;
u_int32_t prun_grp;
u_int32_t prun_tmr;
vifi_t vifi;
int i;
int stop_sending;
struct rtentry *r;
struct gtable *g;
struct ptable *pt;
struct listaddr *vr;
/* Don't process any prunes if router is not pruning */
if (pruning == 0)
return;
if ((vifi = find_vif(src, dst)) == NO_VIF) {
logit(LOG_INFO, 0,
"ignoring prune report from non-neighbor %s",
inet_fmt(src, s1));
return;
}
/* Check if enough data is present */
if (datalen < 12)
{
logit(LOG_WARNING, 0,
"non-decipherable prune from %s",
inet_fmt(src, s1));
return;
}
for (i = 0; i< 4; i++)
((char *)&prun_src)[i] = *p++;
for (i = 0; i< 4; i++)
((char *)&prun_grp)[i] = *p++;
for (i = 0; i< 4; i++)
((char *)&prun_tmr)[i] = *p++;
prun_tmr = ntohl(prun_tmr);
logit(LOG_DEBUG, 0, "%s on vif %d prunes (%s %s)/%d",
inet_fmt(src, s1), vifi,
inet_fmt(prun_src, s2), inet_fmt(prun_grp, s3), prun_tmr);
/*
* Find the subnet for the prune
*/
if (find_src_grp(prun_src, 0, prun_grp)) {
g = gtp ? gtp->gt_gnext : kernel_table;
r = g->gt_route;
if (!VIFM_ISSET(vifi, r->rt_children)) {
logit(LOG_WARNING, 0, "prune received from non-child %s for (%s %s)",
inet_fmt(src, s1), inet_fmt(prun_src, s2),
inet_fmt(prun_grp, s3));
return;
}
if (VIFM_ISSET(vifi, g->gt_scope)) {
logit(LOG_WARNING, 0, "prune received from %s on scoped grp (%s %s)",
inet_fmt(src, s1), inet_fmt(prun_src, s2),
inet_fmt(prun_grp, s3));
return;
}
if ((pt = find_prune_entry(src, g->gt_pruntbl)) != NULL) {
/*
* If it's about to expire, then it's only still around because
* of timer granularity, so don't warn about it.
*/
if (pt->pt_timer > 10) {
logit(LOG_WARNING, 0, "%s %d from %s for (%s %s)/%d %s %d %s %x",
"duplicate prune received on vif",
vifi, inet_fmt(src, s1), inet_fmt(prun_src, s2),
inet_fmt(prun_grp, s3), prun_tmr,
"old timer:", pt->pt_timer, "cur gm:", g->gt_grpmems);
}
pt->pt_timer = prun_tmr;
} else {
/* allocate space for the prune structure */
pt = malloc(sizeof(struct ptable));
if (pt == NULL)
logit(LOG_ERR, 0, "pt: ran out of memory");
pt->pt_vifi = vifi;
pt->pt_router = src;
pt->pt_timer = prun_tmr;
pt->pt_next = g->gt_pruntbl;
g->gt_pruntbl = pt;
}
/* Refresh the group's lifetime */
g->gt_timer = CACHE_LIFETIME(cache_lifetime);
if (g->gt_timer < prun_tmr)
g->gt_timer = prun_tmr;
/*
* check if any more packets need to be sent on the
* vif which sent this message
*/
stop_sending = 1;
for (vr = uvifs[vifi].uv_neighbors; vr; vr = vr->al_next)
if (find_prune_entry(vr->al_addr, g->gt_pruntbl) == NULL) {
stop_sending = 0;