forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpim_bsm.c
2192 lines (1840 loc) · 56.2 KB
/
pim_bsm.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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* pim_bsm.c: PIM BSM handling routines
*
* Copyright (C) 2018-19 Vmware, Inc.
* Saravanan K
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <lib/network.h>
#include <lib/iana_afi.h>
#include <lib/sockunion.h>
#include <lib/sockopt.h>
#include "if.h"
#include "pimd.h"
#include "pim_iface.h"
#include "pim_instance.h"
#include "pim_neighbor.h"
#include "pim_rpf.h"
#include "pim_hello.h"
#include "pim_pim.h"
#include "pim_nht.h"
#include "pim_bsm.h"
#include "pim_time.h"
#include "pim_zebra.h"
#include "pim_util.h"
#include "pim_sock.h"
/* Functions forward declaration */
static void pim_bs_timer_start(struct bsm_scope *scope, int bs_timeout);
static void pim_g2rp_timer_start(struct bsm_rpinfo *bsrp, int hold_time);
static inline void pim_g2rp_timer_restart(struct bsm_rpinfo *bsrp,
int hold_time);
static void pim_bsm_accept_any(struct bsm_scope *scope);
static void pim_cand_bsr_trigger(struct bsm_scope *scope, bool verbose);
static void pim_cand_bsr_pending(struct bsm_scope *scope);
/* Memory Types */
DEFINE_MTYPE_STATIC(PIMD, PIM_BSGRP_NODE, "PIM BSR advertised grp info");
DEFINE_MTYPE_STATIC(PIMD, PIM_BSRP_INFO, "PIM BSR advertised RP info");
DEFINE_MTYPE(PIMD, PIM_BSM_FRAG, "PIM BSM fragment");
DEFINE_MTYPE_STATIC(PIMD, PIM_BSM_PKT_VAR_MEM, "PIM BSM Packet");
DEFINE_MTYPE_STATIC(PIMD, PIM_CAND_RP_GRP, "PIM Candidate RP group");
static int cand_rp_group_cmp(const struct cand_rp_group *a,
const struct cand_rp_group *b)
{
return prefix_cmp(&a->p, &b->p);
}
DECLARE_RBTREE_UNIQ(cand_rp_groups, struct cand_rp_group, item,
cand_rp_group_cmp);
/* All bsm packets forwarded shall be fit within ip mtu less iphdr(max) */
#define MAX_IP_HDR_LEN 24
/* pim_bsm_write_config - Write the interface pim bsm configuration.*/
void pim_bsm_write_config(struct vty *vty, struct interface *ifp)
{
struct pim_interface *pim_ifp = ifp->info;
if (pim_ifp) {
if (!pim_ifp->bsm_enable)
vty_out(vty, " no " PIM_AF_NAME " pim bsm\n");
if (!pim_ifp->ucast_bsm_accept)
vty_out(vty, " no " PIM_AF_NAME " pim unicast-bsm\n");
}
}
static void pim_bsm_rpinfo_free(struct bsm_rpinfo *bsrp_info)
{
EVENT_OFF(bsrp_info->g2rp_timer);
XFREE(MTYPE_PIM_BSRP_INFO, bsrp_info);
}
static void pim_bsm_rpinfos_free(struct bsm_rpinfos_head *head)
{
struct bsm_rpinfo *bsrp_info;
while ((bsrp_info = bsm_rpinfos_pop(head)))
pim_bsm_rpinfo_free(bsrp_info);
}
static void pim_free_bsgrp_data(struct bsgrp_node *bsgrp_node)
{
pim_bsm_rpinfos_free(bsgrp_node->bsrp_list);
pim_bsm_rpinfos_free(bsgrp_node->partial_bsrp_list);
XFREE(MTYPE_PIM_BSGRP_NODE, bsgrp_node);
}
static void pim_free_bsgrp_node(struct route_table *rt, struct prefix *grp)
{
struct route_node *rn;
rn = route_node_lookup(rt, grp);
if (rn) {
rn->info = NULL;
route_unlock_node(rn);
route_unlock_node(rn);
}
}
static void pim_bsm_frag_free(struct bsm_frag *bsfrag)
{
XFREE(MTYPE_PIM_BSM_FRAG, bsfrag);
}
void pim_bsm_frags_free(struct bsm_scope *scope)
{
struct bsm_frag *bsfrag;
while ((bsfrag = bsm_frags_pop(scope->bsm_frags)))
pim_bsm_frag_free(bsfrag);
}
int pim_bsm_rpinfo_cmp(const struct bsm_rpinfo *node1,
const struct bsm_rpinfo *node2)
{
/* RP election Algo :
* Step-1 : Loweset Rp priority will have higher precedance.
* Step-2 : If priority same then higher hash val will have
* higher precedance.
* Step-3 : If Hash val is same then highest rp address will
* become elected RP.
*/
if (node1->rp_prio < node2->rp_prio)
return -1;
if (node1->rp_prio > node2->rp_prio)
return 1;
if (node1->hash < node2->hash)
return 1;
if (node1->hash > node2->hash)
return -1;
return pim_addr_cmp(node2->rp_address, node1->rp_address);
}
static struct bsgrp_node *pim_bsm_new_bsgrp_node(struct route_table *rt,
struct prefix *grp)
{
struct route_node *rn;
struct bsgrp_node *bsgrp;
rn = route_node_get(rt, grp);
if (!rn) {
zlog_warn("%s: route node creation failed", __func__);
return NULL;
}
bsgrp = XCALLOC(MTYPE_PIM_BSGRP_NODE, sizeof(struct bsgrp_node));
rn->info = bsgrp;
bsm_rpinfos_init(bsgrp->bsrp_list);
bsm_rpinfos_init(bsgrp->partial_bsrp_list);
prefix_copy(&bsgrp->group, grp);
return bsgrp;
}
/* BS timer for NO_INFO, ACCEPT_ANY & ACCEPT_PREFERRED.
* Candidate BSR handling is separate further below
*/
static void pim_on_bs_timer(struct event *t)
{
struct bsm_scope *scope;
scope = EVENT_ARG(t);
EVENT_OFF(scope->bs_timer);
if (PIM_DEBUG_BSM)
zlog_debug("%s: Bootstrap Timer expired for scope: %d",
__func__, scope->sz_id);
assertf(scope->state <= ACCEPT_PREFERRED, "state=%d", scope->state);
pim_nht_bsr_del(scope->pim, scope->current_bsr);
pim_bsm_accept_any(scope);
}
static void pim_bsm_accept_any(struct bsm_scope *scope)
{
struct route_node *rn;
struct bsgrp_node *bsgrp_node;
struct bsm_rpinfo *bsrp;
EVENT_OFF(scope->t_ebsr_regen_bsm);
/* Reset scope zone data */
scope->state = ACCEPT_ANY;
scope->current_bsr = PIMADDR_ANY;
scope->current_bsr_prio = 0;
scope->current_bsr_first_ts = 0;
scope->current_bsr_last_ts = 0;
scope->bsm_frag_tag = 0;
pim_bsm_frags_free(scope);
for (rn = route_top(scope->bsrp_table); rn; rn = route_next(rn)) {
bsgrp_node = (struct bsgrp_node *)rn->info;
if (!bsgrp_node) {
if (PIM_DEBUG_BSM)
zlog_debug("%s: bsgrp_node is null", __func__);
continue;
}
/* Give grace time for rp to continue for another hold time */
bsrp = bsm_rpinfos_first(bsgrp_node->bsrp_list);
if (bsrp)
pim_g2rp_timer_restart(bsrp, bsrp->rp_holdtime);
/* clear pending list */
pim_bsm_rpinfos_free(bsgrp_node->partial_bsrp_list);
bsgrp_node->pend_rp_cnt = 0;
}
/* we're leaving ACCEPT_PREFERRED, which doubles as C-BSR if we're
* configured to be a Candidate BSR. See if we're P-BSR now.
*/
pim_cand_bsr_trigger(scope, false);
}
static void pim_bs_timer_stop(struct bsm_scope *scope)
{
if (PIM_DEBUG_BSM)
zlog_debug("%s : BS timer being stopped of sz: %d", __func__,
scope->sz_id);
EVENT_OFF(scope->bs_timer);
}
static void pim_bs_timer_start(struct bsm_scope *scope, int bs_timeout)
{
if (!scope) {
if (PIM_DEBUG_BSM)
zlog_debug("%s : Invalid scope(NULL).", __func__);
return;
}
EVENT_OFF(scope->bs_timer);
if (PIM_DEBUG_BSM)
zlog_debug(
"%s : starting bs timer for scope %d with timeout %d secs",
__func__, scope->sz_id, bs_timeout);
event_add_timer(router->master, pim_on_bs_timer, scope, bs_timeout,
&scope->bs_timer);
}
static inline void pim_bs_timer_restart(struct bsm_scope *scope, int bs_timeout)
{
pim_bs_timer_start(scope, bs_timeout);
}
static void bsm_unicast_sock_read(struct event *t)
{
struct bsm_scope *scope = EVENT_ARG(t);
pim_sock_read_helper(scope->unicast_sock, scope->pim, false);
event_add_read(router->master, bsm_unicast_sock_read, scope,
scope->unicast_sock, &scope->unicast_read);
}
void pim_bsm_proc_init(struct pim_instance *pim)
{
struct bsm_scope *scope = &pim->global_scope;
memset(scope, 0, sizeof(*scope));
scope->sz_id = PIM_GBL_SZ_ID;
scope->bsrp_table = route_table_init();
scope->accept_nofwd_bsm = true;
scope->state = NO_INFO;
scope->pim = pim;
bsm_frags_init(scope->bsm_frags);
pim_bs_timer_start(scope, PIM_BS_TIME);
scope->cand_rp_interval = PIM_CRP_ADV_INTERVAL;
cand_rp_groups_init(scope->cand_rp_groups);
scope->unicast_sock = pim_socket_raw(IPPROTO_PIM);
set_nonblocking(scope->unicast_sock);
sockopt_reuseaddr(scope->unicast_sock);
if (setsockopt_ifindex(PIM_AF, scope->unicast_sock, 1) == -1)
zlog_warn("%s: Without IP_PKTINFO, src interface can't be determined",
__func__);
pim_socket_ip_hdr(scope->unicast_sock);
frr_with_privs (&pimd_privs) {
vrf_bind(pim->vrf->vrf_id, scope->unicast_sock, NULL);
}
event_add_read(router->master, bsm_unicast_sock_read, scope,
scope->unicast_sock, &scope->unicast_read);
}
void pim_bsm_proc_free(struct pim_instance *pim)
{
struct bsm_scope *scope = &pim->global_scope;
struct route_node *rn;
struct bsgrp_node *bsgrp;
struct cand_rp_group *crpgrp;
EVENT_OFF(scope->unicast_read);
close(scope->unicast_sock);
pim_bs_timer_stop(scope);
pim_bsm_frags_free(scope);
for (rn = route_top(scope->bsrp_table); rn; rn = route_next(rn)) {
bsgrp = rn->info;
if (!bsgrp)
continue;
pim_free_bsgrp_data(bsgrp);
}
while ((crpgrp = cand_rp_groups_pop(scope->cand_rp_groups)))
XFREE(MTYPE_PIM_CAND_RP_GRP, crpgrp);
cand_rp_groups_fini(scope->cand_rp_groups);
route_table_finish(scope->bsrp_table);
}
static bool is_hold_time_elapsed(void *data)
{
struct bsm_rpinfo *bsrp;
bsrp = data;
if (bsrp->elapse_time < bsrp->rp_holdtime)
return false;
else
return true;
}
static void pim_on_g2rp_timer(struct event *t)
{
struct bsm_rpinfo *bsrp;
struct bsm_rpinfo *bsrp_node;
struct bsgrp_node *bsgrp_node;
struct pim_instance *pim;
struct rp_info *rp_info;
struct route_node *rn;
uint16_t elapse;
pim_addr bsrp_addr;
bsrp = EVENT_ARG(t);
EVENT_OFF(bsrp->g2rp_timer);
bsgrp_node = bsrp->bsgrp_node;
/* elapse time is the hold time of expired node */
elapse = bsrp->rp_holdtime;
bsrp_addr = bsrp->rp_address;
/* update elapse for all bsrp nodes */
frr_each_safe (bsm_rpinfos, bsgrp_node->bsrp_list, bsrp_node) {
bsrp_node->elapse_time += elapse;
if (is_hold_time_elapsed(bsrp_node)) {
bsm_rpinfos_del(bsgrp_node->bsrp_list, bsrp_node);
pim_bsm_rpinfo_free(bsrp_node);
}
}
/* Get the next elected rp node */
bsrp = bsm_rpinfos_first(bsgrp_node->bsrp_list);
pim = bsgrp_node->scope->pim;
rn = route_node_lookup(pim->rp_table, &bsgrp_node->group);
if (!rn) {
zlog_warn("%s: Route node doesn't exist", __func__);
return;
}
rp_info = (struct rp_info *)rn->info;
if (!rp_info) {
route_unlock_node(rn);
return;
}
if (rp_info->rp_src != RP_SRC_STATIC) {
/* If new rp available, change it else delete the existing */
if (bsrp) {
pim_g2rp_timer_start(
bsrp, (bsrp->rp_holdtime - bsrp->elapse_time));
pim_rp_change(pim, bsrp->rp_address, bsgrp_node->group,
RP_SRC_BSR);
} else {
pim_rp_del(pim, bsrp_addr, bsgrp_node->group, NULL,
RP_SRC_BSR);
}
}
if (!bsm_rpinfos_count(bsgrp_node->bsrp_list)
&& !bsm_rpinfos_count(bsgrp_node->partial_bsrp_list)) {
pim_free_bsgrp_node(pim->global_scope.bsrp_table,
&bsgrp_node->group);
pim_free_bsgrp_data(bsgrp_node);
}
}
static void pim_g2rp_timer_start(struct bsm_rpinfo *bsrp, int hold_time)
{
if (!bsrp) {
if (PIM_DEBUG_BSM)
zlog_debug("%s : Invalid brsp(NULL).", __func__);
return;
}
EVENT_OFF(bsrp->g2rp_timer);
if (PIM_DEBUG_BSM)
zlog_debug(
"%s : starting g2rp timer for grp: %pFX - rp: %pPAs with timeout %d secs(Actual Hold time : %d secs)",
__func__, &bsrp->bsgrp_node->group, &bsrp->rp_address,
hold_time, bsrp->rp_holdtime);
event_add_timer(router->master, pim_on_g2rp_timer, bsrp, hold_time,
&bsrp->g2rp_timer);
}
static inline void pim_g2rp_timer_restart(struct bsm_rpinfo *bsrp,
int hold_time)
{
pim_g2rp_timer_start(bsrp, hold_time);
}
static void pim_g2rp_timer_stop(struct bsm_rpinfo *bsrp)
{
if (!bsrp)
return;
if (PIM_DEBUG_BSM)
zlog_debug("%s : stopping g2rp timer for grp: %pFX - rp: %pPAs",
__func__, &bsrp->bsgrp_node->group,
&bsrp->rp_address);
EVENT_OFF(bsrp->g2rp_timer);
}
static bool is_hold_time_zero(void *data)
{
struct bsm_rpinfo *bsrp;
bsrp = data;
if (bsrp->rp_holdtime)
return false;
else
return true;
}
static void pim_instate_pend_list(struct bsgrp_node *bsgrp_node)
{
struct bsm_rpinfo *active;
struct bsm_rpinfo *pend;
struct rp_info *rp_info;
struct route_node *rn;
struct pim_instance *pim;
struct rp_info *rp_all;
struct prefix group_all;
bool had_rp_node = true;
pim = bsgrp_node->scope->pim;
active = bsm_rpinfos_first(bsgrp_node->bsrp_list);
/* Remove nodes with hold time 0 & check if list still has a head */
frr_each_safe (bsm_rpinfos, bsgrp_node->partial_bsrp_list, pend) {
if (is_hold_time_zero(pend)) {
bsm_rpinfos_del(bsgrp_node->partial_bsrp_list, pend);
pim_bsm_rpinfo_free(pend);
}
}
pend = bsm_rpinfos_first(bsgrp_node->partial_bsrp_list);
if (!pim_get_all_mcast_group(&group_all))
return;
rp_all = pim_rp_find_match_group(pim, &group_all);
rn = route_node_lookup(pim->rp_table, &bsgrp_node->group);
if (pend)
pim_g2rp_timer_start(pend, pend->rp_holdtime);
/* if rp node doesn't exist or exist but not configured(rp_all),
* install the rp from head(if exists) of partial list. List is
* is sorted such that head is the elected RP for the group.
*/
if (!rn || (prefix_same(&rp_all->group, &bsgrp_node->group) &&
pim_rpf_addr_is_inaddr_any(&rp_all->rp))) {
if (PIM_DEBUG_BSM)
zlog_debug("%s: Route node doesn't exist", __func__);
if (pend)
pim_rp_new(pim, pend->rp_address, bsgrp_node->group,
NULL, RP_SRC_BSR);
had_rp_node = false;
} else {
rp_info = (struct rp_info *)rn->info;
if (!rp_info) {
route_unlock_node(rn);
if (pend)
pim_rp_new(pim, pend->rp_address,
bsgrp_node->group, NULL, RP_SRC_BSR);
had_rp_node = false;
}
}
/* We didn't have rp node and pending list is empty(unlikely), cleanup*/
if ((!had_rp_node) && (!pend)) {
pim_free_bsgrp_node(bsgrp_node->scope->bsrp_table,
&bsgrp_node->group);
pim_free_bsgrp_data(bsgrp_node);
return;
}
if ((had_rp_node) && (rp_info->rp_src != RP_SRC_STATIC)) {
/* This means we searched and got rp node, needs unlock */
route_unlock_node(rn);
if (active && pend) {
if (pim_addr_cmp(active->rp_address, pend->rp_address))
pim_rp_change(pim, pend->rp_address,
bsgrp_node->group, RP_SRC_BSR);
}
/* Possible when the first BSM has group with 0 rp count */
if ((!active) && (!pend)) {
if (PIM_DEBUG_BSM) {
zlog_debug(
"%s: Both bsrp and partial list are empty",
__func__);
}
pim_free_bsgrp_node(bsgrp_node->scope->bsrp_table,
&bsgrp_node->group);
pim_free_bsgrp_data(bsgrp_node);
return;
}
/* Possible when a group with 0 rp count received in BSM */
if ((active) && (!pend)) {
pim_rp_del(pim, active->rp_address, bsgrp_node->group,
NULL, RP_SRC_BSR);
pim_free_bsgrp_node(bsgrp_node->scope->bsrp_table,
&bsgrp_node->group);
if (PIM_DEBUG_BSM) {
zlog_debug("%s:Pend List is null,del grp node",
__func__);
}
pim_free_bsgrp_data(bsgrp_node);
return;
}
}
if ((had_rp_node) && (rp_info->rp_src == RP_SRC_STATIC)) {
/* We need to unlock rn this case */
route_unlock_node(rn);
/* there is a chance that static rp exist and bsrp cleaned
* so clean bsgrp node if pending list empty
*/
if (!pend) {
if (PIM_DEBUG_BSM)
zlog_debug(
"%s: Partial list is empty, static rp exists",
__func__);
pim_free_bsgrp_node(bsgrp_node->scope->bsrp_table,
&bsgrp_node->group);
pim_free_bsgrp_data(bsgrp_node);
return;
}
}
/* swap the list & delete all nodes in partial list (old bsrp_list)
* before swap
* active is head of bsrp list
* pend is head of partial list
* After swap
* active is head of partial list
* pend is head of bsrp list
* So check appriate head after swap and clean the new partial list
*/
bsm_rpinfos_swap_all(bsgrp_node->bsrp_list,
bsgrp_node->partial_bsrp_list);
if (active)
pim_g2rp_timer_stop(active);
pim_bsm_rpinfos_free(bsgrp_node->partial_bsrp_list);
}
static bool is_preferred_bsr(struct pim_instance *pim, pim_addr bsr,
uint32_t bsr_prio)
{
if (bsr_prio > pim->global_scope.current_bsr_prio)
return true;
else if (bsr_prio == pim->global_scope.current_bsr_prio) {
if (pim_addr_cmp(bsr, pim->global_scope.current_bsr) >= 0)
return true;
else
return false;
} else if (!pim_addr_cmp(bsr, pim->global_scope.current_bsr)) {
/* BSR config changed, lower prio now. local BSR check
* is handled separately in pim_bsm_update()
*/
return true;
} else
return false;
}
static void pim_bsm_update(struct pim_instance *pim, pim_addr bsr,
uint32_t bsr_prio)
{
pim->global_scope.current_bsr_prio = bsr_prio;
pim->global_scope.current_bsr_last_ts = pim_time_monotonic_sec();
if (pim->global_scope.bsr_addrsel.run &&
pim->global_scope.cand_bsr_prio > bsr_prio &&
pim->global_scope.state < BSR_PENDING) {
/* current BSR is now less preferred than ourselves */
pim_cand_bsr_pending(&pim->global_scope);
return;
}
if (!pim_addr_cmp(bsr, pim->global_scope.current_bsr))
return;
switch (pim->global_scope.state) {
case BSR_PENDING:
if (PIM_DEBUG_BSM)
zlog_debug("Candidate BSR dropping out of BSR election, better BSR (%u, %pPA)",
bsr_prio, &bsr);
break;
case BSR_ELECTED:
if (PIM_DEBUG_BSM)
zlog_debug("Lost BSR status, better BSR (%u, %pPA)",
bsr_prio, &bsr);
break;
case NO_INFO:
case ACCEPT_ANY:
case ACCEPT_PREFERRED:
break;
}
EVENT_OFF(pim->global_scope.t_ebsr_regen_bsm);
if (pim->global_scope.state == BSR_ELECTED)
pim_crp_db_clear(&pim->global_scope);
else
pim_nht_bsr_del(pim, pim->global_scope.current_bsr);
pim_nht_bsr_add(pim, bsr);
pim->global_scope.current_bsr = bsr;
pim->global_scope.current_bsr_first_ts = pim_time_monotonic_sec();
pim->global_scope.state = ACCEPT_PREFERRED;
pim_cand_rp_trigger(&pim->global_scope);
}
void pim_bsm_clear(struct pim_instance *pim)
{
struct route_node *rn;
struct route_node *rpnode;
struct bsgrp_node *bsgrp;
pim_addr nht_p;
struct prefix g_all;
struct rp_info *rp_all;
struct pim_upstream *up;
struct rp_info *rp_info;
bool upstream_updated = false;
EVENT_OFF(pim->global_scope.t_ebsr_regen_bsm);
if (pim->global_scope.state == BSR_ELECTED)
pim_crp_db_clear(&pim->global_scope);
else
pim_nht_bsr_del(pim, pim->global_scope.current_bsr);
/* Reset scope zone data */
pim->global_scope.accept_nofwd_bsm = false;
pim->global_scope.state = ACCEPT_ANY;
pim->global_scope.current_bsr = PIMADDR_ANY;
pim->global_scope.current_bsr_prio = 0;
pim->global_scope.current_bsr_first_ts = 0;
pim->global_scope.current_bsr_last_ts = 0;
pim->global_scope.bsm_frag_tag = 0;
pim_bsm_frags_free(&pim->global_scope);
pim_bs_timer_stop(&pim->global_scope);
for (rn = route_top(pim->global_scope.bsrp_table); rn;
rn = route_next(rn)) {
bsgrp = rn->info;
if (!bsgrp)
continue;
rpnode = route_node_lookup(pim->rp_table, &bsgrp->group);
if (!rpnode) {
pim_free_bsgrp_node(bsgrp->scope->bsrp_table,
&bsgrp->group);
pim_free_bsgrp_data(bsgrp);
continue;
}
rp_info = (struct rp_info *)rpnode->info;
if ((!rp_info) || (rp_info->rp_src != RP_SRC_BSR)) {
pim_free_bsgrp_node(bsgrp->scope->bsrp_table,
&bsgrp->group);
pim_free_bsgrp_data(bsgrp);
continue;
}
/* Deregister addr with Zebra NHT */
nht_p = rp_info->rp.rpf_addr;
if (PIM_DEBUG_PIM_NHT_RP) {
zlog_debug("%s: Deregister RP addr %pPA with Zebra ",
__func__, &nht_p);
}
pim_delete_tracked_nexthop(pim, nht_p, NULL, rp_info);
if (!pim_get_all_mcast_group(&g_all))
return;
rp_all = pim_rp_find_match_group(pim, &g_all);
if (rp_all == rp_info) {
rp_all->rp.rpf_addr = PIMADDR_ANY;
rp_all->i_am_rp = 0;
} else {
/* Delete the rp_info from rp-list */
listnode_delete(pim->rp_list, rp_info);
/* Delete the rp node from rp_table */
rpnode->info = NULL;
route_unlock_node(rpnode);
route_unlock_node(rpnode);
XFREE(MTYPE_PIM_RP, rp_info);
}
pim_free_bsgrp_node(bsgrp->scope->bsrp_table, &bsgrp->group);
pim_free_bsgrp_data(bsgrp);
}
pim_rp_refresh_group_to_rp_mapping(pim);
frr_each (rb_pim_upstream, &pim->upstream_head, up) {
/* Find the upstream (*, G) whose upstream address is same as
* the RP
*/
if (!pim_addr_is_any(up->sg.src))
continue;
struct prefix grp;
struct rp_info *trp_info;
pim_addr_to_prefix(&grp, up->sg.grp);
trp_info = pim_rp_find_match_group(pim, &grp);
/* RP not found for the group grp */
if (pim_rpf_addr_is_inaddr_any(&trp_info->rp)) {
pim_upstream_rpf_clear(pim, up);
pim_rp_set_upstream_addr(pim, &up->upstream_addr,
up->sg.src, up->sg.grp);
} else {
/* RP found for the group grp */
pim_upstream_update(pim, up);
upstream_updated = true;
}
}
if (upstream_updated)
pim_zebra_update_all_interfaces(pim);
}
static bool pim_bsm_send_intf(uint8_t *buf, int len, struct interface *ifp,
pim_addr dst_addr)
{
struct pim_interface *pim_ifp;
pim_ifp = ifp->info;
if (!pim_ifp) {
if (PIM_DEBUG_BSM)
zlog_debug("%s: Pim interface not available for %s",
__func__, ifp->name);
return false;
}
if (pim_ifp->pim_sock_fd == -1) {
if (PIM_DEBUG_BSM)
zlog_debug("%s: Pim sock not available for %s",
__func__, ifp->name);
return false;
}
if (pim_msg_send(pim_ifp->pim_sock_fd, pim_ifp->primary_address,
dst_addr, buf, len, ifp)) {
zlog_warn("%s: Could not send BSM message on interface: %s",
__func__, ifp->name);
return false;
}
if (!pim_ifp->pim_passive_enable)
pim_ifp->pim_ifstat_bsm_tx++;
pim_ifp->pim->bsm_sent++;
return true;
}
static bool pim_bsm_frag_send(uint8_t *buf, uint32_t len, struct interface *ifp,
uint32_t pim_mtu, pim_addr dst_addr, bool no_fwd)
{
struct pim_interface *pim_ifp = ifp->info;
struct bsmmsg_grpinfo *grpinfo, *curgrp;
uint8_t *firstgrp_ptr;
uint8_t *pkt;
uint8_t *pak_start;
uint32_t parsed_len = 0;
uint32_t this_pkt_rem;
uint32_t copy_byte_count;
uint32_t this_pkt_len;
uint8_t total_rp_cnt;
uint8_t this_rp_cnt;
uint8_t frag_rp_cnt;
uint8_t rp_fit_cnt;
bool pak_pending = false;
/* MTU passed here is PIM MTU (IP MTU less IP Hdr) */
if (pim_mtu < (PIM_MIN_BSM_LEN)) {
zlog_warn(
"%s: mtu(pim mtu: %d) size less than minimum bootstrap len",
__func__, pim_mtu);
if (PIM_DEBUG_BSM)
zlog_debug(
"%s: mtu (pim mtu:%d) less than minimum bootstrap len",
__func__, pim_mtu);
return false;
}
pak_start = XCALLOC(MTYPE_PIM_BSM_PKT_VAR_MEM, pim_mtu);
pkt = pak_start;
/* Fill PIM header later before sending packet to calc checksum */
pkt += PIM_MSG_HEADER_LEN;
buf += PIM_MSG_HEADER_LEN;
/* copy bsm header to new packet at offset of pim hdr */
memcpy(pkt, buf, PIM_BSM_HDR_LEN);
pkt += PIM_BSM_HDR_LEN;
buf += PIM_BSM_HDR_LEN;
parsed_len += (PIM_MSG_HEADER_LEN + PIM_BSM_HDR_LEN);
/* Store the position of first grp ptr, which can be reused for
* next packet to start filling group. old bsm header and pim hdr
* remains. So need not be filled again for next packet onwards.
*/
firstgrp_ptr = pkt;
/* we received mtu excluding IP hdr len as param
* now this_pkt_rem is mtu excluding
* PIM_BSM_HDR_LEN + PIM_MSG_HEADER_LEN
*/
this_pkt_rem = pim_mtu - (PIM_BSM_HDR_LEN + PIM_MSG_HEADER_LEN);
/* For each group till the packet length parsed */
while (parsed_len < len) {
/* pkt ---> fragment's current pointer
* buf ---> input buffer's current pointer
* mtu ---> size of the pim packet - PIM header
* curgrp ---> current group on the fragment
* grpinfo ---> current group on the input buffer
* this_pkt_rem ---> bytes remaing on the current fragment
* rp_fit_cnt ---> num of rp for current grp that
* fits this frag
* total_rp_cnt ---> total rp present for the group in the buf
* frag_rp_cnt ---> no of rp for the group to be fit in
* the frag
* this_rp_cnt ---> how many rp have we parsed
*/
grpinfo = (struct bsmmsg_grpinfo *)buf;
memcpy(pkt, buf, PIM_BSM_GRP_LEN);
curgrp = (struct bsmmsg_grpinfo *)pkt;
parsed_len += PIM_BSM_GRP_LEN;
pkt += PIM_BSM_GRP_LEN;
buf += PIM_BSM_GRP_LEN;
this_pkt_rem -= PIM_BSM_GRP_LEN;
/* initialize rp count and total_rp_cnt before the rp loop */
this_rp_cnt = 0;
total_rp_cnt = grpinfo->frag_rp_count;
/* Loop till all RPs for the group parsed */
while (this_rp_cnt < total_rp_cnt) {
/* All RP from a group processed here.
* group is pointed by grpinfo.
* At this point make sure buf pointing to a RP
* within a group
*/
rp_fit_cnt = this_pkt_rem / PIM_BSM_RP_LEN;
/* calculate how many rp am i going to copy in
* this frag
*/
if (rp_fit_cnt > (total_rp_cnt - this_rp_cnt))
frag_rp_cnt = total_rp_cnt - this_rp_cnt;
else
frag_rp_cnt = rp_fit_cnt;
/* populate the frag rp count for the current grp */
curgrp->frag_rp_count = frag_rp_cnt;
copy_byte_count = frag_rp_cnt * PIM_BSM_RP_LEN;
/* copy all the rp that we are fitting in this
* frag for the grp
*/
memcpy(pkt, buf, copy_byte_count);
this_rp_cnt += frag_rp_cnt;
buf += copy_byte_count;
pkt += copy_byte_count;
parsed_len += copy_byte_count;
this_pkt_rem -= copy_byte_count;
/* Either we couldn't fit all rp for the group or the
* mtu reached
*/
if ((this_rp_cnt < total_rp_cnt)
|| (this_pkt_rem
< (PIM_BSM_GRP_LEN + PIM_BSM_RP_LEN))) {
/* No space to fit in more rp, send this pkt */
this_pkt_len = pim_mtu - this_pkt_rem;
pim_msg_build_header(
pim_ifp->primary_address, dst_addr,
pak_start, this_pkt_len,
PIM_MSG_TYPE_BOOTSTRAP, no_fwd);
pim_bsm_send_intf(pak_start, this_pkt_len, ifp,
dst_addr);
/* Construct next fragment. Reuse old packet */
pkt = firstgrp_ptr;
this_pkt_rem = pim_mtu - (PIM_BSM_HDR_LEN
+ PIM_MSG_HEADER_LEN);
/* If pkt can't accommodate next group + at
* least one rp, we must break out of this inner
* loop and process next RP
*/
if (total_rp_cnt == this_rp_cnt)
break;
/* If some more RPs for the same group pending,
* fill grp hdr
*/
memcpy(pkt, (uint8_t *)grpinfo,
PIM_BSM_GRP_LEN);
curgrp = (struct bsmmsg_grpinfo *)pkt;
pkt += PIM_BSM_GRP_LEN;
this_pkt_rem -= PIM_BSM_GRP_LEN;
pak_pending = false;
} else {
/* We filled something but not yet sent out */
pak_pending = true;
}
} /* while RP count */
} /*while parsed len */
/* Send if we have any unsent packet */
if (pak_pending) {
this_pkt_len = pim_mtu - this_pkt_rem;
pim_msg_build_header(pim_ifp->primary_address, dst_addr,
pak_start, this_pkt_len,
PIM_MSG_TYPE_BOOTSTRAP, no_fwd);
pim_bsm_send_intf(pak_start, (pim_mtu - this_pkt_rem), ifp,
dst_addr);
}
XFREE(MTYPE_PIM_BSM_PKT_VAR_MEM, pak_start);
return true;
}
static void pim_bsm_fwd_whole_sz(struct pim_instance *pim, uint8_t *buf,
uint32_t len, int sz)
{
struct interface *ifp;
struct pim_interface *pim_ifp;
pim_addr dst_addr;
uint32_t pim_mtu;
bool no_fwd = false;
bool ret = false;