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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
|
// SPDX-License-Identifier: GPL-2.0-or-later
/* BGP nexthop scan
* Copyright (C) 2000 Kunihiro Ishiguro
*/
#include <zebra.h>
#include "command.h"
#include "frrevent.h"
#include "prefix.h"
#include "lib/json.h"
#include "zclient.h"
#include "stream.h"
#include "network.h"
#include "log.h"
#include "memory.h"
#include "hash.h"
#include "jhash.h"
#include "nexthop.h"
#include "queue.h"
#include "filter.h"
#include "printfrr.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_nexthop.h"
#include "bgpd/bgp_nht.h"
#include "bgpd/bgp_debug.h"
#include "bgpd/bgp_damp.h"
#include "bgpd/bgp_fsm.h"
#include "bgpd/bgp_vty.h"
#include "bgpd/bgp_rd.h"
#include "bgpd/bgp_mplsvpn.h"
DEFINE_MTYPE_STATIC(BGPD, MARTIAN_STRING, "BGP Martian Addr Intf String");
int bgp_nexthop_cache_compare(const struct bgp_nexthop_cache *a,
const struct bgp_nexthop_cache *b)
{
if (a->srte_color < b->srte_color)
return -1;
if (a->srte_color > b->srte_color)
return 1;
if (a->ifindex_ipv6_ll < b->ifindex_ipv6_ll)
return -1;
if (a->ifindex_ipv6_ll > b->ifindex_ipv6_ll)
return 1;
return prefix_cmp(&a->prefix, &b->prefix);
}
void bnc_nexthop_free(struct bgp_nexthop_cache *bnc)
{
nexthops_free(bnc->nexthop);
}
struct bgp_nexthop_cache *bnc_new(struct bgp_nexthop_cache_head *tree,
struct prefix *prefix, uint32_t srte_color,
ifindex_t ifindex)
{
struct bgp_nexthop_cache *bnc;
bnc = XCALLOC(MTYPE_BGP_NEXTHOP_CACHE,
sizeof(struct bgp_nexthop_cache));
bnc->prefix = *prefix;
bnc->ifindex_ipv6_ll = ifindex;
bnc->srte_color = srte_color;
bnc->tree = tree;
LIST_INIT(&(bnc->paths));
bgp_nexthop_cache_add(tree, bnc);
return bnc;
}
bool bnc_existing_for_prefix(struct bgp_nexthop_cache *bnc)
{
struct bgp_nexthop_cache *bnc_tmp;
frr_each (bgp_nexthop_cache, bnc->tree, bnc_tmp) {
if (bnc_tmp == bnc)
continue;
if (prefix_cmp(&bnc->prefix, &bnc_tmp->prefix) == 0)
return true;
}
return false;
}
void bnc_free(struct bgp_nexthop_cache *bnc)
{
bnc_nexthop_free(bnc);
bgp_nexthop_cache_del(bnc->tree, bnc);
XFREE(MTYPE_BGP_NEXTHOP_CACHE, bnc);
}
struct bgp_nexthop_cache *bnc_find(struct bgp_nexthop_cache_head *tree,
struct prefix *prefix, uint32_t srte_color,
ifindex_t ifindex)
{
struct bgp_nexthop_cache bnc = {};
if (!tree)
return NULL;
bnc.prefix = *prefix;
bnc.srte_color = srte_color;
bnc.ifindex_ipv6_ll = ifindex;
return bgp_nexthop_cache_find(tree, &bnc);
}
/* Reset and free all BGP nexthop cache. */
static void bgp_nexthop_cache_reset(struct bgp_nexthop_cache_head *tree)
{
struct bgp_nexthop_cache *bnc;
while (bgp_nexthop_cache_count(tree) > 0) {
bnc = bgp_nexthop_cache_first(tree);
while (!LIST_EMPTY(&(bnc->paths))) {
struct bgp_path_info *path = LIST_FIRST(&(bnc->paths));
bgp_mplsvpn_path_nh_label_unlink(path);
bgp_mplsvpn_path_nh_label_bind_unlink(path);
path_nh_map(path, bnc, false);
}
bnc_free(bnc);
}
}
static void *bgp_tip_hash_alloc(void *p)
{
const struct in_addr *val = (const struct in_addr *)p;
struct tip_addr *addr;
addr = XMALLOC(MTYPE_TIP_ADDR, sizeof(struct tip_addr));
addr->refcnt = 0;
addr->addr.s_addr = val->s_addr;
return addr;
}
static void bgp_tip_hash_free(void *addr)
{
XFREE(MTYPE_TIP_ADDR, addr);
}
static unsigned int bgp_tip_hash_key_make(const void *p)
{
const struct tip_addr *addr = p;
return jhash_1word(addr->addr.s_addr, 0);
}
static bool bgp_tip_hash_cmp(const void *p1, const void *p2)
{
const struct tip_addr *addr1 = p1;
const struct tip_addr *addr2 = p2;
return addr1->addr.s_addr == addr2->addr.s_addr;
}
void bgp_tip_hash_init(struct bgp *bgp)
{
bgp->tip_hash = hash_create(bgp_tip_hash_key_make, bgp_tip_hash_cmp,
"BGP TIP hash");
}
void bgp_tip_hash_destroy(struct bgp *bgp)
{
hash_clean_and_free(&bgp->tip_hash, bgp_tip_hash_free);
}
/* Add/Update Tunnel-IP entry of bgp martian next-hop table.
*
* Returns true only if we add a _new_ TIP so the caller knows that an
* actionable change has occurred. If we find an existing TIP then we
* only need to update the refcnt, since the collection of known TIPs
* has not changed.
*/
bool bgp_tip_add(struct bgp *bgp, struct in_addr *tip)
{
struct tip_addr tmp;
struct tip_addr *addr;
bool tip_added = false;
tmp.addr = *tip;
addr = hash_lookup(bgp->tip_hash, &tmp);
if (!addr) {
addr = hash_get(bgp->tip_hash, &tmp, bgp_tip_hash_alloc);
tip_added = true;
}
addr->refcnt++;
return tip_added;
}
void bgp_tip_del(struct bgp *bgp, struct in_addr *tip)
{
struct tip_addr tmp;
struct tip_addr *addr;
tmp.addr = *tip;
addr = hash_lookup(bgp->tip_hash, &tmp);
/* may have been deleted earlier by bgp_interface_down() */
if (addr == NULL)
return;
addr->refcnt--;
if (addr->refcnt == 0) {
hash_release(bgp->tip_hash, addr);
XFREE(MTYPE_TIP_ADDR, addr);
}
}
/* BGP own address structure */
struct bgp_addr {
struct prefix p;
struct list *ifp_name_list;
};
static void show_address_entry(struct hash_bucket *bucket, void *args)
{
struct vty *vty = (struct vty *)args;
struct bgp_addr *addr = (struct bgp_addr *)bucket->data;
char *name;
struct listnode *node;
char str[INET6_ADDRSTRLEN] = {0};
vty_out(vty, "addr: %s, count: %d : ",
inet_ntop(addr->p.family, &(addr->p.u.prefix),
str, INET6_ADDRSTRLEN),
addr->ifp_name_list->count);
for (ALL_LIST_ELEMENTS_RO(addr->ifp_name_list, node, name)) {
vty_out(vty, " %s,", name);
}
vty_out(vty, "\n");
}
void bgp_nexthop_show_address_hash(struct vty *vty, struct bgp *bgp)
{
hash_iterate(bgp->address_hash,
(void (*)(struct hash_bucket *, void *))show_address_entry,
vty);
}
static void bgp_address_hash_string_del(void *val)
{
char *data = val;
XFREE(MTYPE_MARTIAN_STRING, data);
}
static void *bgp_address_hash_alloc(void *p)
{
struct bgp_addr *copy_addr = p;
struct bgp_addr *addr = NULL;
addr = XMALLOC(MTYPE_BGP_ADDR, sizeof(struct bgp_addr));
prefix_copy(&addr->p, ©_addr->p);
addr->ifp_name_list = list_new();
addr->ifp_name_list->del = bgp_address_hash_string_del;
return addr;
}
static void bgp_address_hash_free(void *data)
{
struct bgp_addr *addr = data;
list_delete(&addr->ifp_name_list);
XFREE(MTYPE_BGP_ADDR, addr);
}
static unsigned int bgp_address_hash_key_make(const void *p)
{
const struct bgp_addr *addr = p;
return prefix_hash_key(&addr->p);
}
static bool bgp_address_hash_cmp(const void *p1, const void *p2)
{
const struct bgp_addr *addr1 = p1;
const struct bgp_addr *addr2 = p2;
return prefix_same(&addr1->p, &addr2->p);
}
void bgp_address_init(struct bgp *bgp)
{
bgp->address_hash =
hash_create(bgp_address_hash_key_make, bgp_address_hash_cmp,
"BGP Connected Address Hash");
}
void bgp_address_destroy(struct bgp *bgp)
{
hash_clean_and_free(&bgp->address_hash, bgp_address_hash_free);
}
static void bgp_address_add(struct bgp *bgp, struct connected *ifc,
struct prefix *p)
{
struct bgp_addr tmp;
struct bgp_addr *addr;
struct listnode *node;
char *name;
tmp.p = *p;
if (tmp.p.family == AF_INET)
tmp.p.prefixlen = IPV4_MAX_BITLEN;
else if (tmp.p.family == AF_INET6)
tmp.p.prefixlen = IPV6_MAX_BITLEN;
addr = hash_get(bgp->address_hash, &tmp, bgp_address_hash_alloc);
for (ALL_LIST_ELEMENTS_RO(addr->ifp_name_list, node, name)) {
if (strcmp(ifc->ifp->name, name) == 0)
break;
}
if (!node) {
name = XSTRDUP(MTYPE_MARTIAN_STRING, ifc->ifp->name);
listnode_add(addr->ifp_name_list, name);
}
}
static void bgp_address_del(struct bgp *bgp, struct connected *ifc,
struct prefix *p)
{
struct bgp_addr tmp;
struct bgp_addr *addr;
struct listnode *node;
char *name;
tmp.p = *p;
if (tmp.p.family == AF_INET)
tmp.p.prefixlen = IPV4_MAX_BITLEN;
else if (tmp.p.family == AF_INET6)
tmp.p.prefixlen = IPV6_MAX_BITLEN;
addr = hash_lookup(bgp->address_hash, &tmp);
/* may have been deleted earlier by bgp_interface_down() */
if (addr == NULL)
return;
for (ALL_LIST_ELEMENTS_RO(addr->ifp_name_list, node, name)) {
if (strcmp(ifc->ifp->name, name) == 0)
break;
}
if (node) {
list_delete_node(addr->ifp_name_list, node);
XFREE(MTYPE_MARTIAN_STRING, name);
}
if (addr->ifp_name_list->count == 0) {
hash_release(bgp->address_hash, addr);
list_delete(&addr->ifp_name_list);
XFREE(MTYPE_BGP_ADDR, addr);
}
}
struct bgp_connected_ref {
unsigned int refcnt;
};
void bgp_connected_add(struct bgp *bgp, struct connected *ifc)
{
struct prefix p;
struct prefix *addr;
struct bgp_dest *dest;
struct bgp_connected_ref *bc;
struct listnode *node, *nnode;
struct peer *peer;
struct peer_connection *connection;
addr = ifc->address;
p = *(CONNECTED_PREFIX(ifc));
if (addr->family == AF_INET) {
apply_mask_ipv4((struct prefix_ipv4 *)&p);
if (prefix_ipv4_any((struct prefix_ipv4 *)&p))
return;
bgp_address_add(bgp, ifc, addr);
dest = bgp_node_get(bgp->connected_table[AFI_IP], &p);
bc = bgp_dest_get_bgp_connected_ref_info(dest);
if (bc)
bc->refcnt++;
else {
bc = XCALLOC(MTYPE_BGP_CONN,
sizeof(struct bgp_connected_ref));
bc->refcnt = 1;
bgp_dest_set_bgp_connected_ref_info(dest, bc);
}
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
if (peer->conf_if &&
(strcmp(peer->conf_if, ifc->ifp->name) == 0) &&
!peer_established(peer->connection) &&
!CHECK_FLAG(peer->flags, PEER_FLAG_IFPEER_V6ONLY)) {
connection = peer->connection;
if (peer_active(peer))
BGP_EVENT_ADD(connection, BGP_Stop);
BGP_EVENT_ADD(connection, BGP_Start);
}
}
} else if (addr->family == AF_INET6) {
apply_mask_ipv6((struct prefix_ipv6 *)&p);
if (IN6_IS_ADDR_UNSPECIFIED(&p.u.prefix6))
return;
if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
return;
bgp_address_add(bgp, ifc, addr);
dest = bgp_node_get(bgp->connected_table[AFI_IP6], &p);
bc = bgp_dest_get_bgp_connected_ref_info(dest);
if (bc)
bc->refcnt++;
else {
bc = XCALLOC(MTYPE_BGP_CONN,
sizeof(struct bgp_connected_ref));
bc->refcnt = 1;
bgp_dest_set_bgp_connected_ref_info(dest, bc);
}
}
}
void bgp_connected_delete(struct bgp *bgp, struct connected *ifc)
{
struct prefix p;
struct prefix *addr;
struct bgp_dest *dest = NULL;
struct bgp_connected_ref *bc;
addr = ifc->address;
p = *(CONNECTED_PREFIX(ifc));
apply_mask(&p);
if (addr->family == AF_INET) {
if (prefix_ipv4_any((struct prefix_ipv4 *)&p))
return;
bgp_address_del(bgp, ifc, addr);
dest = bgp_node_lookup(bgp->connected_table[AFI_IP], &p);
} else if (addr->family == AF_INET6) {
if (IN6_IS_ADDR_UNSPECIFIED(&p.u.prefix6))
return;
if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
return;
bgp_address_del(bgp, ifc, addr);
dest = bgp_node_lookup(bgp->connected_table[AFI_IP6], &p);
}
if (!dest)
return;
bc = bgp_dest_get_bgp_connected_ref_info(dest);
bc->refcnt--;
if (bc->refcnt == 0) {
XFREE(MTYPE_BGP_CONN, bc);
bgp_dest_set_bgp_connected_ref_info(dest, NULL);
}
dest = bgp_dest_unlock_node(dest);
assert(dest);
bgp_dest_unlock_node(dest);
}
static void bgp_connected_cleanup(struct route_table *table,
struct route_node *rn)
{
struct bgp_connected_ref *bc;
struct bgp_dest *bn = bgp_dest_from_rnode(rn);
bc = bgp_dest_get_bgp_connected_ref_info(bn);
if (!bc)
return;
XFREE(MTYPE_BGP_CONN, bc);
bgp_dest_set_bgp_connected_ref_info(bn, NULL);
}
bool bgp_nexthop_self(struct bgp *bgp, afi_t afi, uint8_t type,
uint8_t sub_type, struct attr *attr,
struct bgp_dest *dest)
{
uint8_t new_afi = afi == AFI_IP ? AF_INET : AF_INET6;
struct bgp_addr tmp_addr = {{0}}, *addr = NULL;
struct tip_addr tmp_tip, *tip = NULL;
const struct prefix *p = bgp_dest_get_prefix(dest);
bool is_bgp_static_route =
((type == ZEBRA_ROUTE_BGP) && (sub_type == BGP_ROUTE_STATIC))
? true
: false;
if (!is_bgp_static_route)
new_afi = BGP_ATTR_NEXTHOP_AFI_IP6(attr) ? AF_INET6 : AF_INET;
tmp_addr.p.family = new_afi;
switch (new_afi) {
case AF_INET:
if (is_bgp_static_route) {
tmp_addr.p.u.prefix4 = p->u.prefix4;
tmp_addr.p.prefixlen = p->prefixlen;
} else {
/* Here we need to find out which nexthop to be used*/
if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP)) {
tmp_addr.p.u.prefix4 = attr->nexthop;
tmp_addr.p.prefixlen = IPV4_MAX_BITLEN;
} else if ((attr->mp_nexthop_len)
&& ((attr->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV4)
|| (attr->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV4))) {
tmp_addr.p.u.prefix4 =
attr->mp_nexthop_global_in;
tmp_addr.p.prefixlen = IPV4_MAX_BITLEN;
} else
return false;
}
break;
case AF_INET6:
if (is_bgp_static_route) {
tmp_addr.p.u.prefix6 = p->u.prefix6;
tmp_addr.p.prefixlen = p->prefixlen;
} else {
tmp_addr.p.u.prefix6 = attr->mp_nexthop_global;
tmp_addr.p.prefixlen = IPV6_MAX_BITLEN;
}
break;
default:
break;
}
addr = hash_lookup(bgp->address_hash, &tmp_addr);
if (addr)
return true;
if (new_afi == AF_INET && hashcount(bgp->tip_hash)) {
memset(&tmp_tip, 0, sizeof(tmp_tip));
tmp_tip.addr = attr->nexthop;
if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP)) {
tmp_tip.addr = attr->nexthop;
} else if ((attr->mp_nexthop_len) &&
((attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV4)
|| (attr->mp_nexthop_len == BGP_ATTR_NHLEN_VPNV4))) {
tmp_tip.addr = attr->mp_nexthop_global_in;
}
tip = hash_lookup(bgp->tip_hash, &tmp_tip);
if (tip)
return true;
}
return false;
}
bool bgp_multiaccess_check_v4(struct in_addr nexthop, struct peer *peer)
{
struct bgp_dest *dest1;
struct bgp_dest *dest2;
struct prefix p;
int ret;
p.family = AF_INET;
p.prefixlen = IPV4_MAX_BITLEN;
p.u.prefix4 = nexthop;
dest1 = bgp_node_match(peer->bgp->connected_table[AFI_IP], &p);
if (!dest1)
return false;
p.family = AF_INET;
p.prefixlen = IPV4_MAX_BITLEN;
p.u.prefix4 = peer->connection->su.sin.sin_addr;
dest2 = bgp_node_match(peer->bgp->connected_table[AFI_IP], &p);
if (!dest2) {
bgp_dest_unlock_node(dest1);
return false;
}
ret = (dest1 == dest2);
bgp_dest_unlock_node(dest1);
bgp_dest_unlock_node(dest2);
return ret;
}
bool bgp_multiaccess_check_v6(struct in6_addr nexthop, struct peer *peer)
{
struct bgp_dest *dest1;
struct bgp_dest *dest2;
struct prefix p;
int ret;
p.family = AF_INET6;
p.prefixlen = IPV6_MAX_BITLEN;
p.u.prefix6 = nexthop;
dest1 = bgp_node_match(peer->bgp->connected_table[AFI_IP6], &p);
if (!dest1)
return false;
p.family = AF_INET6;
p.prefixlen = IPV6_MAX_BITLEN;
p.u.prefix6 = peer->connection->su.sin6.sin6_addr;
dest2 = bgp_node_match(peer->bgp->connected_table[AFI_IP6], &p);
if (!dest2) {
bgp_dest_unlock_node(dest1);
return false;
}
ret = (dest1 == dest2);
bgp_dest_unlock_node(dest1);
bgp_dest_unlock_node(dest2);
return ret;
}
bool bgp_subgrp_multiaccess_check_v6(struct in6_addr nexthop,
struct update_subgroup *subgrp,
struct peer *exclude)
{
struct bgp_dest *dest1 = NULL, *dest2 = NULL;
struct peer_af *paf = NULL;
struct prefix p = {0}, np = {0};
struct bgp *bgp = NULL;
np.family = AF_INET6;
np.prefixlen = IPV6_MAX_BITLEN;
np.u.prefix6 = nexthop;
p.family = AF_INET;
p.prefixlen = IPV6_MAX_BITLEN;
bgp = SUBGRP_INST(subgrp);
dest1 = bgp_node_match(bgp->connected_table[AFI_IP6], &np);
if (!dest1)
return false;
SUBGRP_FOREACH_PEER (subgrp, paf) {
/* Skip peer we're told to exclude - e.g., source of route. */
if (paf->peer == exclude)
continue;
p.u.prefix6 = paf->peer->connection->su.sin6.sin6_addr;
dest2 = bgp_node_match(bgp->connected_table[AFI_IP6], &p);
if (dest1 == dest2) {
bgp_dest_unlock_node(dest1);
bgp_dest_unlock_node(dest2);
return true;
}
if (dest2)
bgp_dest_unlock_node(dest2);
}
bgp_dest_unlock_node(dest1);
return false;
}
bool bgp_subgrp_multiaccess_check_v4(struct in_addr nexthop,
struct update_subgroup *subgrp,
struct peer *exclude)
{
struct bgp_dest *dest1, *dest2;
struct peer_af *paf;
struct prefix p, np;
struct bgp *bgp;
np.family = AF_INET;
np.prefixlen = IPV4_MAX_BITLEN;
np.u.prefix4 = nexthop;
p.family = AF_INET;
p.prefixlen = IPV4_MAX_BITLEN;
bgp = SUBGRP_INST(subgrp);
dest1 = bgp_node_match(bgp->connected_table[AFI_IP], &np);
if (!dest1)
return false;
SUBGRP_FOREACH_PEER (subgrp, paf) {
/* Skip peer we're told to exclude - e.g., source of route. */
if (paf->peer == exclude)
continue;
p.u.prefix4 = paf->peer->connection->su.sin.sin_addr;
dest2 = bgp_node_match(bgp->connected_table[AFI_IP], &p);
if (dest1 == dest2) {
bgp_dest_unlock_node(dest1);
bgp_dest_unlock_node(dest2);
return true;
}
if (dest2)
bgp_dest_unlock_node(dest2);
}
bgp_dest_unlock_node(dest1);
return false;
}
static void bgp_show_bgp_path_info_flags(uint32_t flags, json_object *json)
{
json_object *json_flags = NULL;
if (!json)
return;
json_flags = json_object_new_object();
json_object_boolean_add(json_flags, "igpChanged",
CHECK_FLAG(flags, BGP_PATH_IGP_CHANGED));
json_object_boolean_add(json_flags, "damped",
CHECK_FLAG(flags, BGP_PATH_DAMPED));
json_object_boolean_add(json_flags, "history",
CHECK_FLAG(flags, BGP_PATH_HISTORY));
json_object_boolean_add(json_flags, "bestpath",
CHECK_FLAG(flags, BGP_PATH_SELECTED));
json_object_boolean_add(json_flags, "valid",
CHECK_FLAG(flags, BGP_PATH_VALID));
json_object_boolean_add(json_flags, "attrChanged",
CHECK_FLAG(flags, BGP_PATH_ATTR_CHANGED));
json_object_boolean_add(json_flags, "deterministicMedCheck",
CHECK_FLAG(flags, BGP_PATH_DMED_CHECK));
json_object_boolean_add(json_flags, "deterministicMedSelected",
CHECK_FLAG(flags, BGP_PATH_DMED_SELECTED));
json_object_boolean_add(json_flags, "stale",
CHECK_FLAG(flags, BGP_PATH_STALE));
json_object_boolean_add(json_flags, "removed",
CHECK_FLAG(flags, BGP_PATH_REMOVED));
json_object_boolean_add(json_flags, "counted",
CHECK_FLAG(flags, BGP_PATH_COUNTED));
json_object_boolean_add(json_flags, "multipath",
CHECK_FLAG(flags, BGP_PATH_MULTIPATH));
json_object_boolean_add(json_flags, "multipathChanged",
CHECK_FLAG(flags, BGP_PATH_MULTIPATH_CHG));
json_object_boolean_add(json_flags, "ribAttributeChanged",
CHECK_FLAG(flags, BGP_PATH_RIB_ATTR_CHG));
json_object_boolean_add(json_flags, "nexthopSelf",
CHECK_FLAG(flags, BGP_PATH_ANNC_NH_SELF));
json_object_boolean_add(json_flags, "linkBandwidthChanged",
CHECK_FLAG(flags, BGP_PATH_LINK_BW_CHG));
json_object_boolean_add(json_flags, "acceptOwn",
CHECK_FLAG(flags, BGP_PATH_ACCEPT_OWN));
json_object_object_add(json, "flags", json_flags);
}
static void bgp_show_nexthop_paths(struct vty *vty, struct bgp *bgp,
struct bgp_nexthop_cache *bnc,
json_object *json)
{
struct bgp_dest *dest;
struct bgp_path_info *path;
afi_t afi;
safi_t safi;
struct bgp_table *table;
struct bgp *bgp_path;
json_object *paths = NULL;
json_object *json_path = NULL;
if (json)
paths = json_object_new_array();
else
vty_out(vty, " Paths:\n");
LIST_FOREACH (path, &(bnc->paths), nh_thread) {
dest = path->net;
assert(dest && bgp_dest_table(dest));
afi = family2afi(bgp_dest_get_prefix(dest)->family);
table = bgp_dest_table(dest);
safi = table->safi;
bgp_path = table->bgp;
if (json) {
json_path = json_object_new_object();
json_object_string_add(json_path, "afi", afi2str(afi));
json_object_string_add(json_path, "safi",
safi2str(safi));
json_object_string_addf(json_path, "prefix", "%pBD",
dest);
if (dest->pdest)
json_object_string_addf(
json_path, "rd",
BGP_RD_AS_FORMAT(bgp->asnotation),
(struct prefix_rd *)bgp_dest_get_prefix(
dest->pdest));
json_object_string_add(
json_path, "vrf",
vrf_id_to_name(bgp_path->vrf_id));
bgp_show_bgp_path_info_flags(path->flags, json_path);
json_object_array_add(paths, json_path);
continue;
}
if (dest->pdest) {
vty_out(vty, " %d/%d %pBD RD ", afi, safi, dest);
vty_out(vty, BGP_RD_AS_FORMAT(bgp->asnotation),
(struct prefix_rd *)bgp_dest_get_prefix(
dest->pdest));
vty_out(vty, " %s flags 0x%x\n", bgp_path->name_pretty,
path->flags);
} else
vty_out(vty, " %d/%d %pBD %s flags 0x%x\n",
afi, safi, dest, bgp_path->name_pretty, path->flags);
}
if (json)
json_object_object_add(json, "paths", paths);
}
static void bgp_show_nexthops_detail(struct vty *vty, struct bgp *bgp,
struct bgp_nexthop_cache *bnc,
json_object *json)
{
struct nexthop *nexthop;
json_object *json_gates = NULL;
json_object *json_gate = NULL;
if (json)
json_gates = json_object_new_array();
for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next) {
if (json) {
json_gate = json_object_new_object();
switch (nexthop->type) {
case NEXTHOP_TYPE_IPV6:
json_object_string_addf(json_gate, "ip", "%pI6",
&nexthop->gate.ipv6);
break;
case NEXTHOP_TYPE_IPV6_IFINDEX:
json_object_string_addf(json_gate, "ip", "%pI6",
&nexthop->gate.ipv6);
json_object_string_add(
json_gate, "interfaceName",
ifindex2ifname(
bnc->ifindex_ipv6_ll
? bnc->ifindex_ipv6_ll
: nexthop->ifindex,
bgp->vrf_id));
break;
case NEXTHOP_TYPE_IPV4:
json_object_string_addf(json_gate, "ip", "%pI4",
&nexthop->gate.ipv4);
break;
case NEXTHOP_TYPE_IFINDEX:
json_object_string_add(
json_gate, "interfaceName",
ifindex2ifname(
bnc->ifindex_ipv6_ll
? bnc->ifindex_ipv6_ll
: nexthop->ifindex,
bgp->vrf_id));
break;
case NEXTHOP_TYPE_IPV4_IFINDEX:
json_object_string_addf(json_gate, "ip", "%pI4",
&nexthop->gate.ipv4);
json_object_string_add(
json_gate, "interfaceName",
ifindex2ifname(
bnc->ifindex_ipv6_ll
? bnc->ifindex_ipv6_ll
: nexthop->ifindex,
bgp->vrf_id));
break;
case NEXTHOP_TYPE_BLACKHOLE:
json_object_boolean_true_add(json_gate,
"unreachable");
switch (nexthop->bh_type) {
case BLACKHOLE_REJECT:
json_object_boolean_true_add(json_gate,
"reject");
break;
case BLACKHOLE_ADMINPROHIB:
json_object_boolean_true_add(
json_gate, "adminProhibited");
break;
case BLACKHOLE_NULL:
json_object_boolean_true_add(
json_gate, "blackhole");
break;
case BLACKHOLE_UNSPEC:
break;
}
break;
default:
break;
}
json_object_array_add(json_gates, json_gate);
continue;
}
switch (nexthop->type) {
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
vty_out(vty, " gate %pI6", &nexthop->gate.ipv6);
if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX &&
bnc->ifindex_ipv6_ll)
vty_out(vty, ", if %s\n",
ifindex2ifname(bnc->ifindex_ipv6_ll,
bgp->vrf_id));
else if (nexthop->ifindex)
vty_out(vty, ", if %s\n",
ifindex2ifname(nexthop->ifindex,
bgp->vrf_id));
else
vty_out(vty, "\n");
break;
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
vty_out(vty, " gate %pI4", &nexthop->gate.ipv4);
if (nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX &&
bnc->ifindex_ipv6_ll)
vty_out(vty, ", if %s\n",
ifindex2ifname(bnc->ifindex_ipv6_ll,
bgp->vrf_id));
else if (nexthop->ifindex)
vty_out(vty, ", if %s\n",
ifindex2ifname(nexthop->ifindex,
bgp->vrf_id));
else
vty_out(vty, "\n");
break;
case NEXTHOP_TYPE_IFINDEX:
vty_out(vty, " if %s\n",
ifindex2ifname(bnc->ifindex_ipv6_ll
? bnc->ifindex_ipv6_ll
: nexthop->ifindex,
bgp->vrf_id));
break;
case NEXTHOP_TYPE_BLACKHOLE:
vty_out(vty, " blackhole\n");
break;
default:
vty_out(vty, " invalid nexthop type %u\n",
nexthop->type);
}
}
if (json)
json_object_object_add(json, "nexthops", json_gates);
}
static void bgp_show_nexthop(struct vty *vty, struct bgp *bgp,
struct bgp_nexthop_cache *bnc, bool specific,
json_object *json)
{
char buf[PREFIX2STR_BUFFER];
time_t tbuf;
char timebuf[32];
struct peer *peer;
json_object *json_last_update = NULL;
json_object *json_nexthop = NULL;
peer = (struct peer *)bnc->nht_info;
if (json)
json_nexthop = json_object_new_object();
if (bnc->srte_color) {
if (json)
json_object_int_add(json_nexthop, "srteColor",
bnc->srte_color);
else
vty_out(vty, " SR-TE color %u -", bnc->srte_color);
}
inet_ntop(bnc->prefix.family, &bnc->prefix.u.prefix, buf, sizeof(buf));
if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)) {
if (json) {
json_object_boolean_true_add(json_nexthop, "valid");
json_object_boolean_true_add(json_nexthop, "complete");
json_object_int_add(json_nexthop, "igpMetric",
bnc->metric);
json_object_int_add(json_nexthop, "pathCount",
bnc->path_count);
if (peer)
json_object_string_add(json_nexthop, "peer",
peer->host);
if (bnc->is_evpn_gwip_nexthop)
json_object_boolean_true_add(json_nexthop,
"isEvpnGatewayIp");
} else {
vty_out(vty, " %s valid [IGP metric %d], #paths %d",
buf, bnc->metric, bnc->path_count);
if (peer)
vty_out(vty, ", peer %s", peer->host);
if (bnc->is_evpn_gwip_nexthop)
vty_out(vty, " EVPN Gateway IP");
vty_out(vty, "\n");
}
bgp_show_nexthops_detail(vty, bgp, bnc, json_nexthop);
} else if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_EVPN_INCOMPLETE)) {
if (json) {
json_object_boolean_true_add(json_nexthop, "valid");
json_object_boolean_false_add(json_nexthop, "complete");
json_object_int_add(json_nexthop, "igpMetric",
bnc->metric);
json_object_int_add(json_nexthop, "pathCount",
bnc->path_count);
if (bnc->is_evpn_gwip_nexthop)
json_object_boolean_true_add(json_nexthop,
"isEvpnGatewayIp");
} else {
vty_out(vty,
" %s overlay index unresolved [IGP metric %d], #paths %d",
buf, bnc->metric, bnc->path_count);
if (bnc->is_evpn_gwip_nexthop)
vty_out(vty, " EVPN Gateway IP");
vty_out(vty, "\n");
}
bgp_show_nexthops_detail(vty, bgp, bnc, json_nexthop);
} else {
if (json) {
json_object_boolean_false_add(json_nexthop, "valid");
json_object_boolean_false_add(json_nexthop, "complete");
json_object_int_add(json_nexthop, "pathCount",
bnc->path_count);
if (peer)
json_object_string_add(json_nexthop, "peer",
peer->host);
if (bnc->is_evpn_gwip_nexthop)
json_object_boolean_true_add(json_nexthop,
"isEvpnGatewayIp");
if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED))
json_object_boolean_false_add(json_nexthop,
"isConnected");
if (!CHECK_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED))
json_object_boolean_false_add(json_nexthop,
"isRegistered");
} else {
vty_out(vty, " %s invalid, #paths %d", buf,
bnc->path_count);
if (peer)
vty_out(vty, ", peer %s", peer->host);
if (bnc->is_evpn_gwip_nexthop)
vty_out(vty, " EVPN Gateway IP");
vty_out(vty, "\n");
if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED))
vty_out(vty, " Must be Connected\n");
if (!CHECK_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED))
vty_out(vty, " Is not Registered\n");
}
}
tbuf = time(NULL) - (monotime(NULL) - bnc->last_update);
if (json) {
if (!specific) {
json_last_update = json_object_new_object();
json_object_int_add(json_last_update, "epoch", tbuf);
json_object_string_add(json_last_update, "string",
ctime_r(&tbuf, timebuf));
json_object_object_add(json_nexthop, "lastUpdate",
json_last_update);
} else {
json_object_int_add(json_nexthop, "lastUpdate", tbuf);
}
} else {
vty_out(vty, " Last update: %s", ctime_r(&tbuf, timebuf));
}
/* show paths dependent on nexthop, if needed. */
if (specific)
bgp_show_nexthop_paths(vty, bgp, bnc, json_nexthop);
if (json)
json_object_object_add(json, buf, json_nexthop);
}
static void bgp_show_nexthops(struct vty *vty, struct bgp *bgp,
bool import_table, json_object *json, afi_t afi,
bool detail)
{
struct bgp_nexthop_cache *bnc;
struct bgp_nexthop_cache_head(*tree)[AFI_MAX];
json_object *json_afi = NULL;
bool found = false;
if (!json) {
if (import_table)
vty_out(vty, "Current BGP import check cache:\n");
else
vty_out(vty, "Current BGP nexthop cache:\n");
}
if (import_table)
tree = &bgp->import_check_table;
else
tree = &bgp->nexthop_cache_table;
if (afi == AFI_IP || afi == AFI_IP6) {
if (json)
json_afi = json_object_new_object();
frr_each (bgp_nexthop_cache, &(*tree)[afi], bnc) {
bgp_show_nexthop(vty, bgp, bnc, detail, json_afi);
found = true;
}
if (found && json)
json_object_object_add(
json, (afi == AFI_IP) ? "ipv4" : "ipv6",
json_afi);
return;
}
for (afi = AFI_IP; afi < AFI_MAX; afi++) {
if (json && (afi == AFI_IP || afi == AFI_IP6))
json_afi = json_object_new_object();
frr_each (bgp_nexthop_cache, &(*tree)[afi], bnc)
bgp_show_nexthop(vty, bgp, bnc, detail, json_afi);
if (json && (afi == AFI_IP || afi == AFI_IP6))
json_object_object_add(
json, (afi == AFI_IP) ? "ipv4" : "ipv6",
json_afi);
}
}
static int show_ip_bgp_nexthop_table(struct vty *vty, const char *name,
const char *nhopip_str, bool import_table,
json_object *json, afi_t afi, bool detail)
{
struct bgp *bgp;
if (name && !strmatch(name, VRF_DEFAULT_NAME))
bgp = bgp_lookup_by_name(name);
else
bgp = bgp_get_default();
if (!bgp) {
if (!json)
vty_out(vty, "%% No such BGP instance exist\n");
return CMD_WARNING;
}
if (nhopip_str) {
struct prefix nhop;
struct bgp_nexthop_cache_head (*tree)[AFI_MAX];
struct bgp_nexthop_cache *bnc;
bool found = false;
json_object *json_afi = NULL;
if (!str2prefix(nhopip_str, &nhop)) {
if (!json)
vty_out(vty, "nexthop address is malformed\n");
return CMD_WARNING;
}
tree = import_table ? &bgp->import_check_table
: &bgp->nexthop_cache_table;
if (json)
json_afi = json_object_new_object();
frr_each (bgp_nexthop_cache, &(*tree)[family2afi(nhop.family)],
bnc) {
if (prefix_cmp(&bnc->prefix, &nhop))
continue;
bgp_show_nexthop(vty, bgp, bnc, true, json_afi);
found = true;
}
if (json)
json_object_object_add(
json,
(family2afi(nhop.family) == AFI_IP) ? "ipv4"
: "ipv6",
json_afi);
if (!found && !json)
vty_out(vty, "nexthop %s does not have entry\n",
nhopip_str);
} else
bgp_show_nexthops(vty, bgp, import_table, json, afi, detail);
return CMD_SUCCESS;
}
static void bgp_show_all_instances_nexthops_vty(struct vty *vty,
json_object *json, afi_t afi,
bool detail)
{
struct listnode *node, *nnode;
struct bgp *bgp;
const char *inst_name;
json_object *json_instance = NULL;
for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
inst_name = (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
? VRF_DEFAULT_NAME
: bgp->name;
if (json)
json_instance = json_object_new_object();
else
vty_out(vty, "\nInstance %s:\n", inst_name);
bgp_show_nexthops(vty, bgp, false, json_instance, afi, detail);
if (json)
json_object_object_add(json, inst_name, json_instance);
}
}
#include "bgpd/bgp_nexthop_clippy.c"
DEFPY (show_ip_bgp_nexthop,
show_ip_bgp_nexthop_cmd,
"show [ip] bgp [<view|vrf> VIEWVRFNAME$vrf] nexthop [<A.B.C.D|X:X::X:X>$nhop] [<ipv4$afi [A.B.C.D$nhop]|ipv6$afi [X:X::X:X$nhop]>] [detail$detail] [json$uj]",
SHOW_STR
IP_STR
BGP_STR
BGP_INSTANCE_HELP_STR
"BGP nexthop table\n"
"IPv4 nexthop address\n"
"IPv6 nexthop address\n"
"BGP nexthop IPv4 table\n"
"IPv4 nexthop address\n"
"BGP nexthop IPv6 table\n"
"IPv6 nexthop address\n"
"Show detailed information\n"
JSON_STR)
{
int rc = 0;
json_object *json = NULL;
afi_t afiz = AFI_UNSPEC;
if (uj)
json = json_object_new_object();
if (afi)
afiz = bgp_vty_afi_from_str(afi);
rc = show_ip_bgp_nexthop_table(vty, vrf, nhop_str, false, json, afiz,
detail);
if (uj)
vty_json(vty, json);
return rc;
}
DEFPY (show_ip_bgp_import_check,
show_ip_bgp_import_check_cmd,
"show [ip] bgp [<view|vrf> VIEWVRFNAME$vrf] import-check-table [detail$detail] [json$uj]",
SHOW_STR
IP_STR
BGP_STR
BGP_INSTANCE_HELP_STR
"BGP import check table\n"
"Show detailed information\n"
JSON_STR)
{
int rc = 0;
json_object *json = NULL;
if (uj)
json = json_object_new_object();
rc = show_ip_bgp_nexthop_table(vty, vrf, NULL, true, json, AFI_UNSPEC,
detail);
if (uj)
vty_json(vty, json);
return rc;
}
DEFPY (show_ip_bgp_instance_all_nexthop,
show_ip_bgp_instance_all_nexthop_cmd,
"show [ip] bgp <view|vrf> all nexthop [<ipv4|ipv6>$afi] [detail$detail] [json$uj]",
SHOW_STR
IP_STR
BGP_STR
BGP_INSTANCE_ALL_HELP_STR
"BGP nexthop table\n"
"BGP IPv4 nexthop table\n"
"BGP IPv6 nexthop table\n"
"Show detailed information\n"
JSON_STR)
{
json_object *json = NULL;
afi_t afiz = AFI_UNSPEC;
if (uj)
json = json_object_new_object();
if (afi)
afiz = bgp_vty_afi_from_str(afi);
bgp_show_all_instances_nexthops_vty(vty, json, afiz, detail);
if (uj)
vty_json(vty, json);
return CMD_SUCCESS;
}
void bgp_scan_init(struct bgp *bgp)
{
afi_t afi;
for (afi = AFI_IP; afi < AFI_MAX; afi++) {
bgp_nexthop_cache_init(&bgp->nexthop_cache_table[afi]);
bgp_nexthop_cache_init(&bgp->import_check_table[afi]);
bgp->connected_table[afi] = bgp_table_init(bgp, afi,
SAFI_UNICAST);
}
}
void bgp_scan_vty_init(void)
{
install_element(VIEW_NODE, &show_ip_bgp_nexthop_cmd);
install_element(VIEW_NODE, &show_ip_bgp_import_check_cmd);
install_element(VIEW_NODE, &show_ip_bgp_instance_all_nexthop_cmd);
}
void bgp_scan_finish(struct bgp *bgp)
{
afi_t afi;
for (afi = AFI_IP; afi < AFI_MAX; afi++) {
/* Only the current one needs to be reset. */
bgp_nexthop_cache_reset(&bgp->nexthop_cache_table[afi]);
bgp_nexthop_cache_reset(&bgp->import_check_table[afi]);
bgp->connected_table[afi]->route_table->cleanup =
bgp_connected_cleanup;
bgp_table_unlock(bgp->connected_table[afi]);
bgp->connected_table[afi] = NULL;
}
}
char *bgp_nexthop_dump_bnc_flags(struct bgp_nexthop_cache *bnc, char *buf,
size_t len)
{
if (bnc->flags == 0) {
snprintfrr(buf, len, "None ");
return buf;
}
snprintfrr(buf, len, "%s%s%s%s%s%s%s",
CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID) ? "Valid " : "",
CHECK_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED) ? "Reg " : "",
CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED) ? "Conn " : "",
CHECK_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED) ? "Notify "
: "",
CHECK_FLAG(bnc->flags, BGP_STATIC_ROUTE) ? "Static " : "",
CHECK_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH)
? "Static Exact "
: "",
CHECK_FLAG(bnc->flags, BGP_NEXTHOP_LABELED_VALID)
? "Label Valid "
: "");
return buf;
}
char *bgp_nexthop_dump_bnc_change_flags(struct bgp_nexthop_cache *bnc,
char *buf, size_t len)
{
if (bnc->flags == 0) {
snprintfrr(buf, len, "None ");
return buf;
}
snprintfrr(buf, len, "%s%s%s",
CHECK_FLAG(bnc->change_flags, BGP_NEXTHOP_CHANGED)
? "Changed "
: "",
CHECK_FLAG(bnc->change_flags, BGP_NEXTHOP_METRIC_CHANGED)
? "Metric "
: "",
CHECK_FLAG(bnc->change_flags, BGP_NEXTHOP_CONNECTED_CHANGED)
? "Connected "
: "");
return buf;
}
|