-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
bgp_nht.c
1600 lines (1407 loc) · 44.2 KB
/
bgp_nht.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
/* BGP Nexthop tracking
* Copyright (C) 2013 Cumulus Networks, Inc.
*/
#include <zebra.h>
#include "command.h"
#include "frrevent.h"
#include "prefix.h"
#include "zclient.h"
#include "stream.h"
#include "network.h"
#include "log.h"
#include "memory.h"
#include "nexthop.h"
#include "vrf.h"
#include "filter.h"
#include "nexthop_group.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_table.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_nexthop.h"
#include "bgpd/bgp_debug.h"
#include "bgpd/bgp_errors.h"
#include "bgpd/bgp_nht.h"
#include "bgpd/bgp_fsm.h"
#include "bgpd/bgp_zebra.h"
#include "bgpd/bgp_flowspec_util.h"
#include "bgpd/bgp_evpn.h"
#include "bgpd/bgp_rd.h"
#include "bgpd/bgp_mplsvpn.h"
#include "bgpd/bgp_ecommunity.h"
extern struct zclient *zclient;
static void register_zebra_rnh(struct bgp_nexthop_cache *bnc);
static void unregister_zebra_rnh(struct bgp_nexthop_cache *bnc);
static int make_prefix(int afi, struct bgp_path_info *pi, struct prefix *p);
static void bgp_nht_ifp_initial(struct event *thread);
static int bgp_isvalid_nexthop(struct bgp_nexthop_cache *bnc)
{
return (bgp_zebra_num_connects() == 0
|| (bnc && CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)
&& bnc->nexthop_num > 0));
}
static int bgp_isvalid_nexthop_for_ebgp(struct bgp_nexthop_cache *bnc,
struct bgp_path_info *path)
{
struct interface *ifp = NULL;
struct nexthop *nexthop;
struct bgp_interface *iifp;
struct peer *peer;
if (!path->extra || !path->extra->vrfleak ||
!path->extra->vrfleak->peer_orig)
return false;
peer = path->extra->vrfleak->peer_orig;
/* only connected ebgp peers are valid */
if (peer->sort != BGP_PEER_EBGP || peer->ttl != BGP_DEFAULT_TTL ||
CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK) ||
CHECK_FLAG(peer->bgp->flags, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
return false;
for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next) {
if (nexthop->type == NEXTHOP_TYPE_IFINDEX ||
nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX ||
nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
ifp = if_lookup_by_index(bnc->ifindex_ipv6_ll
? bnc->ifindex_ipv6_ll
: nexthop->ifindex,
bnc->bgp->vrf_id);
}
if (!ifp)
continue;
iifp = ifp->info;
if (CHECK_FLAG(iifp->flags, BGP_INTERFACE_MPLS_BGP_FORWARDING))
return true;
}
return false;
}
static int bgp_isvalid_nexthop_for_mplsovergre(struct bgp_nexthop_cache *bnc,
struct bgp_path_info *path)
{
struct interface *ifp = NULL;
struct nexthop *nexthop;
for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next) {
if (nexthop->type != NEXTHOP_TYPE_BLACKHOLE) {
ifp = if_lookup_by_index(bnc->ifindex_ipv6_ll
? bnc->ifindex_ipv6_ll
: nexthop->ifindex,
bnc->bgp->vrf_id);
if (ifp && (ifp->ll_type == ZEBRA_LLT_IPGRE ||
ifp->ll_type == ZEBRA_LLT_IP6GRE))
break;
}
}
if (!ifp)
return false;
if (CHECK_FLAG(path->attr->rmap_change_flags,
BATTR_RMAP_L3VPN_ACCEPT_GRE))
return true;
return false;
}
static int bgp_isvalid_nexthop_for_mpls(struct bgp_nexthop_cache *bnc,
struct bgp_path_info *path)
{
return (bnc && (bnc->nexthop_num > 0 &&
(CHECK_FLAG(path->flags, BGP_PATH_ACCEPT_OWN) ||
CHECK_FLAG(bnc->flags, BGP_NEXTHOP_LABELED_VALID) ||
bgp_isvalid_nexthop_for_ebgp(bnc, path) ||
bgp_isvalid_nexthop_for_mplsovergre(bnc, path))));
}
static bool bgp_isvalid_nexthop_for_l3vpn(struct bgp_nexthop_cache *bnc,
struct bgp_path_info *path)
{
if (bgp_zebra_num_connects() == 0)
return 1;
if (path->attr->srv6_l3vpn || path->attr->srv6_vpn) {
/* In the case of SRv6-VPN, we need to track the reachability to the
* SID (in other words, IPv6 address). We check that the SID is
* available in the BGP update; then if it is available, we check
* for the nexthop reachability.
*/
if (bnc && (bnc->nexthop_num > 0 && bgp_isvalid_nexthop(bnc)))
return 1;
return 0;
}
/*
* In the case of MPLS-VPN, the label is learned from LDP or other
* protocols, and nexthop tracking is enabled for the label.
* The value is recorded as BGP_NEXTHOP_LABELED_VALID.
* - Otherwise check for mpls-gre acceptance
*/
return bgp_isvalid_nexthop_for_mpls(bnc, path);
}
static void bgp_unlink_nexthop_check(struct bgp_nexthop_cache *bnc)
{
if (LIST_EMPTY(&(bnc->paths)) && !bnc->nht_info) {
if (BGP_DEBUG(nht, NHT))
zlog_debug("%s: freeing bnc %pFX(%d)(%u)(%s)", __func__,
&bnc->prefix, bnc->ifindex_ipv6_ll,
bnc->srte_color, bnc->bgp->name_pretty);
/* only unregister if this is the last nh for this prefix*/
if (!bnc_existing_for_prefix(bnc))
unregister_zebra_rnh(bnc);
bnc_free(bnc);
}
}
void bgp_unlink_nexthop(struct bgp_path_info *path)
{
struct bgp_nexthop_cache *bnc = path->nexthop;
bgp_mplsvpn_path_nh_label_unlink(path);
bgp_mplsvpn_path_nh_label_bind_unlink(path);
if (!bnc)
return;
path_nh_map(path, NULL, false);
bgp_unlink_nexthop_check(bnc);
}
void bgp_replace_nexthop_by_peer(struct peer *from, struct peer *to)
{
struct prefix pp;
struct prefix pt;
struct bgp_nexthop_cache *bncp, *bnct;
afi_t afi;
ifindex_t ifindex = 0;
if (!sockunion2hostprefix(&from->connection->su, &pp))
return;
/*
* Gather the ifindex for if up/down events to be
* tagged into this fun
*/
if (from->conf_if &&
IN6_IS_ADDR_LINKLOCAL(&from->connection->su.sin6.sin6_addr))
ifindex = from->connection->su.sin6.sin6_scope_id;
afi = family2afi(pp.family);
bncp = bnc_find(&from->bgp->nexthop_cache_table[afi], &pp, 0, ifindex);
if (!sockunion2hostprefix(&to->connection->su, &pt))
return;
/*
* Gather the ifindex for if up/down events to be
* tagged into this fun
*/
ifindex = 0;
if (to->conf_if &&
IN6_IS_ADDR_LINKLOCAL(&to->connection->su.sin6.sin6_addr))
ifindex = to->connection->su.sin6.sin6_scope_id;
bnct = bnc_find(&to->bgp->nexthop_cache_table[afi], &pt, 0, ifindex);
if (bnct != bncp)
return;
if (bnct)
bnct->nht_info = to;
}
/*
* Returns the bnc whose bnc->nht_info matches the LL peer by
* looping through the IPv6 nexthop table
*/
static struct bgp_nexthop_cache *
bgp_find_ipv6_nexthop_matching_peer(struct peer *peer)
{
struct bgp_nexthop_cache *bnc;
frr_each (bgp_nexthop_cache, &peer->bgp->nexthop_cache_table[AFI_IP6],
bnc) {
if (bnc->nht_info == peer) {
if (BGP_DEBUG(nht, NHT)) {
zlog_debug(
"Found bnc: %pFX(%u)(%u)(%p) for peer: %s(%s) %p",
&bnc->prefix, bnc->ifindex_ipv6_ll,
bnc->srte_color, bnc, peer->host,
peer->bgp->name_pretty, peer);
}
return bnc;
}
}
if (BGP_DEBUG(nht, NHT))
zlog_debug(
"Could not find bnc for peer %s(%s) %p in v6 nexthop table",
peer->host, peer->bgp->name_pretty, peer);
return NULL;
}
void bgp_unlink_nexthop_by_peer(struct peer *peer)
{
struct prefix p;
struct bgp_nexthop_cache *bnc;
afi_t afi = family2afi(peer->connection->su.sa.sa_family);
ifindex_t ifindex = 0;
if (!sockunion2hostprefix(&peer->connection->su, &p)) {
/*
* In scenarios where unnumbered BGP session is brought
* down by shutting down the interface before unconfiguring
* the BGP neighbor, neighbor information in peer->su.sa
* will be cleared when the interface is shutdown. So
* during the deletion of unnumbered bgp peer, above check
* will return true. Therefore, in this case,BGP needs to
* find the bnc whose bnc->nht_info matches the
* peer being deleted and free it.
*/
bnc = bgp_find_ipv6_nexthop_matching_peer(peer);
} else {
/*
* Gather the ifindex for if up/down events to be
* tagged into this fun
*/
if (afi == AFI_IP6 &&
IN6_IS_ADDR_LINKLOCAL(&peer->connection->su.sin6.sin6_addr))
ifindex = peer->connection->su.sin6.sin6_scope_id;
bnc = bnc_find(&peer->bgp->nexthop_cache_table[afi], &p, 0,
ifindex);
}
if (!bnc)
return;
/* cleanup the peer reference */
bnc->nht_info = NULL;
bgp_unlink_nexthop_check(bnc);
}
/*
* A route and its nexthop might belong to different VRFs. Therefore,
* we need both the bgp_route and bgp_nexthop pointers.
*/
int bgp_find_or_add_nexthop(struct bgp *bgp_route, struct bgp *bgp_nexthop,
afi_t afi, safi_t safi, struct bgp_path_info *pi,
struct peer *peer, int connected,
const struct prefix *orig_prefix)
{
struct bgp_nexthop_cache_head *tree = NULL;
struct bgp_nexthop_cache *bnc;
struct bgp_path_info *bpi_ultimate;
struct prefix p;
uint32_t srte_color = 0;
int is_bgp_static_route = 0;
ifindex_t ifindex = 0;
if (pi) {
is_bgp_static_route = ((pi->type == ZEBRA_ROUTE_BGP)
&& (pi->sub_type == BGP_ROUTE_STATIC))
? 1
: 0;
/* Since Extended Next-hop Encoding (RFC5549) support, we want
to derive
address-family from the next-hop. */
if (!is_bgp_static_route)
afi = BGP_ATTR_MP_NEXTHOP_LEN_IP6(pi->attr) ? AFI_IP6
: AFI_IP;
/* This will return true if the global IPv6 NH is a link local
* addr */
if (make_prefix(afi, pi, &p) < 0)
return 1;
/*
* If it's a V6 nexthop, path is learnt from a v6 LL peer,
* and if the NH prefix matches peer's LL address then
* set the ifindex to peer's interface index so that
* correct nexthop can be found in nexthop tree.
*
* NH could be set to different v6 LL address (compared to
* peer's LL) using route-map. In such a scenario, do not set
* the ifindex.
*/
if (afi == AFI_IP6 &&
IN6_IS_ADDR_LINKLOCAL(
&pi->peer->connection->su.sin6.sin6_addr) &&
IPV6_ADDR_SAME(&pi->peer->connection->su.sin6.sin6_addr,
&p.u.prefix6))
ifindex = pi->peer->connection->su.sin6.sin6_scope_id;
if (!is_bgp_static_route && orig_prefix
&& prefix_same(&p, orig_prefix)) {
if (BGP_DEBUG(nht, NHT)) {
zlog_debug(
"%s(%pFX): prefix loops through itself",
__func__, &p);
}
return 0;
}
srte_color = bgp_attr_get_color(pi->attr);
} else if (peer) {
/*
* Gather the ifindex for if up/down events to be
* tagged into this fun
*/
if (afi == AFI_IP6 && peer->conf_if &&
IN6_IS_ADDR_LINKLOCAL(&peer->connection->su.sin6.sin6_addr)) {
ifindex = peer->connection->su.sin6.sin6_scope_id;
if (ifindex == 0) {
if (BGP_DEBUG(nht, NHT)) {
zlog_debug(
"%s: Unable to locate ifindex, waiting till we have one",
peer->conf_if);
}
return 0;
}
}
if (!sockunion2hostprefix(&peer->connection->su, &p)) {
if (BGP_DEBUG(nht, NHT)) {
zlog_debug(
"%s: Attempting to register with unknown AFI %d (not %d or %d)",
__func__, afi, AFI_IP, AFI_IP6);
}
return 0;
}
} else
return 0;
if (is_bgp_static_route)
tree = &bgp_nexthop->import_check_table[afi];
else
tree = &bgp_nexthop->nexthop_cache_table[afi];
bnc = bnc_find(tree, &p, srte_color, ifindex);
if (!bnc) {
bnc = bnc_new(tree, &p, srte_color, ifindex);
bnc->afi = afi;
bnc->bgp = bgp_nexthop;
if (BGP_DEBUG(nht, NHT))
zlog_debug("Allocated bnc %pFX(%d)(%u)(%s) peer %p",
&bnc->prefix, bnc->ifindex_ipv6_ll,
bnc->srte_color, bnc->bgp->name_pretty,
peer);
} else {
if (BGP_DEBUG(nht, NHT))
zlog_debug(
"Found existing bnc %pFX(%d)(%s) flags 0x%x ifindex %d #paths %d peer %p",
&bnc->prefix, bnc->ifindex_ipv6_ll,
bnc->bgp->name_pretty, bnc->flags,
bnc->ifindex_ipv6_ll, bnc->path_count,
bnc->nht_info);
}
if (pi && is_route_parent_evpn(pi))
bnc->is_evpn_gwip_nexthop = true;
if (is_bgp_static_route && !CHECK_FLAG(bnc->flags, BGP_STATIC_ROUTE)) {
SET_FLAG(bnc->flags, BGP_STATIC_ROUTE);
/* If we're toggling the type, re-register */
if ((CHECK_FLAG(bgp_route->flags, BGP_FLAG_IMPORT_CHECK))
&& !CHECK_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH)) {
SET_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
} else if ((!CHECK_FLAG(bgp_route->flags,
BGP_FLAG_IMPORT_CHECK))
&& CHECK_FLAG(bnc->flags,
BGP_STATIC_ROUTE_EXACT_MATCH)) {
UNSET_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
}
}
/* When nexthop is already known, but now requires 'connected'
* resolution,
* re-register it. The reverse scenario where the nexthop currently
* requires
* 'connected' resolution does not need a re-register (i.e., we treat
* 'connected-required' as an override) except in the scenario where
* this
* is actually a case of tracking a peer for connectivity (e.g., after
* disable connected-check).
* NOTE: We don't track the number of paths separately for 'connected-
* required' vs 'connected-not-required' as this change is not a common
* scenario.
*/
else if (connected && !CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED)) {
SET_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
} else if (peer && !connected
&& CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED)) {
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
}
if (peer && (bnc->ifindex_ipv6_ll != ifindex)) {
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
bnc->ifindex_ipv6_ll = ifindex;
}
if (bgp_route->inst_type == BGP_INSTANCE_TYPE_VIEW) {
SET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
SET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
} else if (!CHECK_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED)
&& !is_default_host_route(&bnc->prefix))
register_zebra_rnh(bnc);
if (pi && pi->nexthop != bnc) {
/* Unlink from existing nexthop cache, if any. This will also
* free
* the nexthop cache entry, if appropriate.
*/
bgp_unlink_nexthop(pi);
/* updates NHT pi list reference */
path_nh_map(pi, bnc, true);
bpi_ultimate = bgp_get_imported_bpi_ultimate(pi);
if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID) && bnc->metric)
(bgp_path_info_extra_get(bpi_ultimate))->igpmetric =
bnc->metric;
else if (bpi_ultimate->extra)
bpi_ultimate->extra->igpmetric = 0;
} else if (peer) {
/*
* Let's not accidentally save the peer data for a peer
* we are going to throw away in a second or so.
* When we come back around we'll fix up this
* data properly in replace_nexthop_by_peer
*/
if (CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE))
bnc->nht_info = (void *)peer; /* NHT peer reference */
}
/*
* We are cheating here. Views have no associated underlying
* ability to detect nexthops. So when we have a view
* just tell everyone the nexthop is valid
*/
if (bgp_route->inst_type == BGP_INSTANCE_TYPE_VIEW)
return 1;
else if (safi == SAFI_UNICAST && pi &&
pi->sub_type == BGP_ROUTE_IMPORTED &&
bgp_path_info_num_labels(pi) && !bnc->is_evpn_gwip_nexthop)
return bgp_isvalid_nexthop_for_l3vpn(bnc, pi);
else if (safi == SAFI_MPLS_VPN && pi &&
pi->sub_type != BGP_ROUTE_IMPORTED)
/* avoid not redistributing mpls vpn routes */
return 1;
else
/* mpls-vpn routes with BGP_ROUTE_IMPORTED subtype */
return (bgp_isvalid_nexthop(bnc));
}
void bgp_delete_connected_nexthop(afi_t afi, struct peer *peer)
{
struct bgp_nexthop_cache *bnc;
struct prefix p;
ifindex_t ifindex = 0;
if (!peer)
return;
/*
* In case the below check evaluates true and if
* the bnc has not been freed at this point, then
* we might have to do something similar to what's
* done in bgp_unlink_nexthop_by_peer(). Since
* bgp_unlink_nexthop_by_peer() loops through the
* nodes of V6 nexthop cache to find the bnc, it is
* currently not being called here.
*/
if (!sockunion2hostprefix(&peer->connection->su, &p))
return;
/*
* Gather the ifindex for if up/down events to be
* tagged into this fun
*/
if (afi == AFI_IP6 &&
IN6_IS_ADDR_LINKLOCAL(&peer->connection->su.sin6.sin6_addr))
ifindex = peer->connection->su.sin6.sin6_scope_id;
bnc = bnc_find(&peer->bgp->nexthop_cache_table[family2afi(p.family)],
&p, 0, ifindex);
if (!bnc) {
if (BGP_DEBUG(nht, NHT))
zlog_debug(
"Cannot find connected NHT node for peer %s(%s)",
peer->host, peer->bgp->name_pretty);
return;
}
if (bnc->nht_info != peer) {
if (BGP_DEBUG(nht, NHT))
zlog_debug(
"Connected NHT %p node for peer %s(%s) points to %p",
bnc, peer->host, bnc->bgp->name_pretty,
bnc->nht_info);
return;
}
bnc->nht_info = NULL;
if (LIST_EMPTY(&(bnc->paths))) {
if (BGP_DEBUG(nht, NHT))
zlog_debug(
"Freeing connected NHT node %p for peer %s(%s)",
bnc, peer->host, bnc->bgp->name_pretty);
unregister_zebra_rnh(bnc);
bnc_free(bnc);
}
}
static void bgp_process_nexthop_update(struct bgp_nexthop_cache *bnc,
struct zapi_route *nhr,
bool import_check)
{
struct nexthop *nexthop;
struct nexthop *oldnh;
struct nexthop *nhlist_head = NULL;
struct nexthop *nhlist_tail = NULL;
int i;
bool evpn_resolved = false;
bnc->last_update = monotime(NULL);
bnc->change_flags = 0;
/* debug print the input */
if (BGP_DEBUG(nht, NHT)) {
char bnc_buf[BNC_FLAG_DUMP_SIZE];
zlog_debug(
"%s(%u): Rcvd NH update %pFX(%u)(%u) - metric %d/%d #nhops %d/%d flags %s",
bnc->bgp->name_pretty, bnc->bgp->vrf_id, &nhr->prefix,
bnc->ifindex_ipv6_ll, bnc->srte_color, nhr->metric,
bnc->metric, nhr->nexthop_num, bnc->nexthop_num,
bgp_nexthop_dump_bnc_flags(bnc, bnc_buf,
sizeof(bnc_buf)));
}
if (nhr->metric != bnc->metric)
bnc->change_flags |= BGP_NEXTHOP_METRIC_CHANGED;
if (nhr->nexthop_num != bnc->nexthop_num)
bnc->change_flags |= BGP_NEXTHOP_CHANGED;
if (import_check && (nhr->type == ZEBRA_ROUTE_BGP ||
!prefix_same(&bnc->prefix, &nhr->prefix))) {
SET_FLAG(bnc->change_flags, BGP_NEXTHOP_CHANGED);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_LABELED_VALID);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_EVPN_INCOMPLETE);
bnc_nexthop_free(bnc);
bnc->nexthop = NULL;
if (BGP_DEBUG(nht, NHT))
zlog_debug(
"%s: Import Check does not resolve to the same prefix for %pFX received %pFX or matching route is BGP",
__func__, &bnc->prefix, &nhr->prefix);
} else if (nhr->nexthop_num) {
struct peer *peer = bnc->nht_info;
prefix_copy(&bnc->resolved_prefix, &nhr->prefix);
/* notify bgp fsm if nbr ip goes from invalid->valid */
if (!bnc->nexthop_num)
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
if (!bnc->is_evpn_gwip_nexthop)
bnc->flags |= BGP_NEXTHOP_VALID;
bnc->metric = nhr->metric;
bnc->nexthop_num = nhr->nexthop_num;
bnc->flags &= ~BGP_NEXTHOP_LABELED_VALID; /* check below */
for (i = 0; i < nhr->nexthop_num; i++) {
int num_labels = 0;
nexthop = nexthop_from_zapi_nexthop(&nhr->nexthops[i]);
/*
* Turn on RA for the v6 nexthops
* we receive from bgp. This is to allow us
* to work with v4 routing over v6 nexthops
*/
if (peer && !peer->ifp
&& CHECK_FLAG(peer->flags,
PEER_FLAG_CAPABILITY_ENHE)
&& nhr->prefix.family == AF_INET6
&& nexthop->type != NEXTHOP_TYPE_BLACKHOLE) {
struct interface *ifp;
ifp = if_lookup_by_index(nexthop->ifindex,
nexthop->vrf_id);
if (ifp)
zclient_send_interface_radv_req(
zclient, nexthop->vrf_id, ifp,
true,
BGP_UNNUM_DEFAULT_RA_INTERVAL);
}
/* There is at least one label-switched path */
if (nexthop->nh_label &&
nexthop->nh_label->num_labels) {
bnc->flags |= BGP_NEXTHOP_LABELED_VALID;
num_labels = nexthop->nh_label->num_labels;
}
if (BGP_DEBUG(nht, NHT)) {
char buf[NEXTHOP_STRLEN];
zlog_debug(
" nhop via %s (%d labels)",
nexthop2str(nexthop, buf, sizeof(buf)),
num_labels);
}
if (nhlist_tail) {
nhlist_tail->next = nexthop;
nhlist_tail = nexthop;
} else {
nhlist_tail = nexthop;
nhlist_head = nexthop;
}
/* No need to evaluate the nexthop if we have already
* determined
* that there has been a change.
*/
if (bnc->change_flags & BGP_NEXTHOP_CHANGED)
continue;
for (oldnh = bnc->nexthop; oldnh; oldnh = oldnh->next)
if (nexthop_same(oldnh, nexthop))
break;
if (!oldnh)
bnc->change_flags |= BGP_NEXTHOP_CHANGED;
}
bnc_nexthop_free(bnc);
bnc->nexthop = nhlist_head;
/*
* Gateway IP nexthop is L3 reachable. Mark it as
* BGP_NEXTHOP_VALID only if it is recursively resolved with a
* remote EVPN RT-2.
* Else, mark it as BGP_NEXTHOP_EVPN_INCOMPLETE.
* When its mapping with EVPN RT-2 is established, unset
* BGP_NEXTHOP_EVPN_INCOMPLETE and set BGP_NEXTHOP_VALID.
*/
if (bnc->is_evpn_gwip_nexthop) {
evpn_resolved = bgp_evpn_is_gateway_ip_resolved(bnc);
if (BGP_DEBUG(nht, NHT))
zlog_debug(
"EVPN gateway IP %pFX recursive MAC/IP lookup %s",
&bnc->prefix,
(evpn_resolved ? "successful"
: "failed"));
if (evpn_resolved) {
bnc->flags |= BGP_NEXTHOP_VALID;
bnc->flags &= ~BGP_NEXTHOP_EVPN_INCOMPLETE;
bnc->change_flags |= BGP_NEXTHOP_MACIP_CHANGED;
} else {
bnc->flags |= BGP_NEXTHOP_EVPN_INCOMPLETE;
bnc->flags &= ~BGP_NEXTHOP_VALID;
}
}
} else {
memset(&bnc->resolved_prefix, 0, sizeof(bnc->resolved_prefix));
bnc->flags &= ~BGP_NEXTHOP_EVPN_INCOMPLETE;
bnc->flags &= ~BGP_NEXTHOP_VALID;
bnc->flags &= ~BGP_NEXTHOP_LABELED_VALID;
bnc->nexthop_num = nhr->nexthop_num;
/* notify bgp fsm if nbr ip goes from valid->invalid */
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
bnc_nexthop_free(bnc);
bnc->nexthop = NULL;
}
evaluate_paths(bnc);
}
static void bgp_nht_ifp_table_handle(struct bgp *bgp,
struct bgp_nexthop_cache_head *table,
struct interface *ifp, bool up)
{
struct bgp_nexthop_cache *bnc;
struct nexthop *nhop;
uint8_t other_nh_count;
bool nhop_ll_found = false;
bool nhop_found = false;
if (ifp->ifindex == IFINDEX_INTERNAL) {
zlog_warn("%s: The interface %s ignored", __func__, ifp->name);
return;
}
frr_each (bgp_nexthop_cache, table, bnc) {
other_nh_count = 0;
nhop_ll_found = bnc->ifindex_ipv6_ll == ifp->ifindex;
for (nhop = bnc->nexthop; nhop; nhop = nhop->next) {
if (nhop->ifindex == bnc->ifindex_ipv6_ll)
continue;
if (nhop->ifindex != ifp->ifindex) {
other_nh_count++;
continue;
}
if (nhop->vrf_id != ifp->vrf->vrf_id) {
other_nh_count++;
continue;
}
nhop_found = true;
}
if (!nhop_found && !nhop_ll_found)
/* The event interface does not match the nexthop cache
* entry */
continue;
if (!up && other_nh_count > 0)
/* Down event ignored in case of multiple next-hop
* interfaces. The other might interfaces might be still
* up. The cases where all interfaces are down or a bnc
* is invalid are processed by a separate zebra rnh
* messages.
*/
continue;
if (!nhop_ll_found) {
evaluate_paths(bnc);
continue;
}
bnc->last_update = monotime(NULL);
bnc->change_flags = 0;
/*
* For interface based routes ( ala the v6 LL routes
* that this was written for ) the metric received
* for the connected route is 0 not 1.
*/
bnc->metric = 0;
if (up) {
SET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
SET_FLAG(bnc->change_flags, BGP_NEXTHOP_CHANGED);
/* change nexthop number only for ll */
bnc->nexthop_num = 1;
} else {
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
SET_FLAG(bnc->change_flags, BGP_NEXTHOP_CHANGED);
bnc->nexthop_num = 0;
}
evaluate_paths(bnc);
}
}
static void bgp_nht_ifp_handle(struct interface *ifp, bool up)
{
struct bgp *bgp;
bgp = ifp->vrf->info;
if (!bgp)
return;
bgp_nht_ifp_table_handle(bgp, &bgp->nexthop_cache_table[AFI_IP], ifp,
up);
bgp_nht_ifp_table_handle(bgp, &bgp->import_check_table[AFI_IP], ifp,
up);
bgp_nht_ifp_table_handle(bgp, &bgp->nexthop_cache_table[AFI_IP6], ifp,
up);
bgp_nht_ifp_table_handle(bgp, &bgp->import_check_table[AFI_IP6], ifp,
up);
}
void bgp_nht_ifp_up(struct interface *ifp)
{
bgp_nht_ifp_handle(ifp, true);
}
void bgp_nht_ifp_down(struct interface *ifp)
{
bgp_nht_ifp_handle(ifp, false);
}
static void bgp_nht_ifp_initial(struct event *thread)
{
ifindex_t ifindex = EVENT_VAL(thread);
struct bgp *bgp = EVENT_ARG(thread);
struct interface *ifp = if_lookup_by_index(ifindex, bgp->vrf_id);
if (!ifp)
return;
if (BGP_DEBUG(nht, NHT))
zlog_debug(
"Handle NHT initial update for Intf %s(%d) status %s",
ifp->name, ifp->ifindex, if_is_up(ifp) ? "up" : "down");
if (if_is_up(ifp))
bgp_nht_ifp_up(ifp);
else
bgp_nht_ifp_down(ifp);
}
/*
* So the bnc code has the ability to handle interface up/down
* events to properly handle v6 LL peering.
* What is happening here:
* The event system for peering expects the nht code to
* report on the tracking events after we move to active
* So let's give the system a chance to report on that event
* in a manner that is expected.
*/
void bgp_nht_interface_events(struct peer *peer)
{
struct bgp *bgp = peer->bgp;
struct bgp_nexthop_cache_head *table;
struct bgp_nexthop_cache *bnc;
struct prefix p;
ifindex_t ifindex = 0;
if (!IN6_IS_ADDR_LINKLOCAL(&peer->connection->su.sin6.sin6_addr))
return;
if (!sockunion2hostprefix(&peer->connection->su, &p))
return;
/*
* Gather the ifindex for if up/down events to be
* tagged into this fun
*/
if (peer->conf_if &&
IN6_IS_ADDR_LINKLOCAL(&peer->connection->su.sin6.sin6_addr))
ifindex = peer->connection->su.sin6.sin6_scope_id;
table = &bgp->nexthop_cache_table[AFI_IP6];
bnc = bnc_find(table, &p, 0, ifindex);
if (!bnc)
return;
if (bnc->ifindex_ipv6_ll)
event_add_event(bm->master, bgp_nht_ifp_initial, bnc->bgp,
bnc->ifindex_ipv6_ll, NULL);
}
void bgp_nexthop_update(struct vrf *vrf, struct prefix *match,
struct zapi_route *nhr)
{
struct bgp_nexthop_cache_head *tree = NULL;
struct bgp_nexthop_cache *bnc_nhc, *bnc_import;
struct bgp *bgp, *bgp_default;
struct bgp_path_info *pi;
struct bgp_dest *dest;
safi_t safi;
afi_t afi;
if (!vrf->info) {
flog_err(EC_BGP_NH_UPD,
"parse nexthop update: instance not found for vrf_id %u",
vrf->vrf_id);
return;
}
bgp = (struct bgp *)vrf->info;
afi = family2afi(match->family);
tree = &bgp->nexthop_cache_table[afi];
bnc_nhc = bnc_find(tree, match, nhr->srte_color, 0);
if (bnc_nhc)
bgp_process_nexthop_update(bnc_nhc, nhr, false);
else if (BGP_DEBUG(nht, NHT))
zlog_debug("parse nexthop update %pFX(%u)(%s): bnc info not found for nexthop cache",
&nhr->prefix, nhr->srte_color,
bgp->name_pretty);
tree = &bgp->import_check_table[afi];
bnc_import = bnc_find(tree, match, nhr->srte_color, 0);
if (bnc_import) {
bgp_process_nexthop_update(bnc_import, nhr, true);
bgp_default = bgp_get_default();
safi = nhr->safi;
if (bgp != bgp_default && bgp->rib[afi][safi]) {
dest = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
match, NULL);
for (pi = bgp_dest_get_bgp_path_info(dest); pi;
pi = pi->next)
if (pi->peer == bgp->peer_self &&
pi->type == ZEBRA_ROUTE_BGP &&
pi->sub_type == BGP_ROUTE_STATIC)
vpn_leak_from_vrf_update(bgp_default,
bgp, pi);
}
} else if (BGP_DEBUG(nht, NHT))
zlog_debug("parse nexthop update %pFX(%u)(%s): bnc info not found for import check",
&nhr->prefix, nhr->srte_color,
bgp->name_pretty);
/*
* HACK: if any BGP route is dependant on an SR-policy that doesn't
* exist, zebra will never send NH updates relative to that policy. In
* that case, whenever we receive an update about a colorless NH, update
* the corresponding colorful NHs that share the same endpoint but that
* are inactive. This ugly hack should work around the problem at the
* cost of a performance pernalty. Long term, what should be done is to
* make zebra's RNH subsystem aware of SR-TE colors (like bgpd is),
* which should provide a better infrastructure to solve this issue in
* a more efficient and elegant way.
*/
if (nhr->srte_color == 0) {
struct bgp_nexthop_cache *bnc_iter;
frr_each (bgp_nexthop_cache, &bgp->nexthop_cache_table[afi],
bnc_iter) {
if (!prefix_same(match, &bnc_iter->prefix) ||
bnc_iter->srte_color == 0)
continue;
bgp_process_nexthop_update(bnc_iter, nhr, false);
}
}
}
/*
* Cleanup nexthop registration and status information for BGP nexthops
* pertaining to this VRF. This is invoked upon VRF deletion.
*/
void bgp_cleanup_nexthops(struct bgp *bgp)
{
for (afi_t afi = AFI_IP; afi < AFI_MAX; afi++) {
struct bgp_nexthop_cache *bnc;
frr_each (bgp_nexthop_cache, &bgp->nexthop_cache_table[afi],
bnc) {
/* Clear relevant flags. */