/
bgp_zebra.c
3033 lines (2566 loc) · 76.9 KB
/
bgp_zebra.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
/* zebra client
* Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#include "command.h"
#include "stream.h"
#include "network.h"
#include "prefix.h"
#include "log.h"
#include "sockunion.h"
#include "zclient.h"
#include "routemap.h"
#include "thread.h"
#include "queue.h"
#include "memory.h"
#include "lib/json.h"
#include "lib/bfd.h"
#include "filter.h"
#include "mpls.h"
#include "vxlan.h"
#include "pbr.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_nexthop.h"
#include "bgpd/bgp_zebra.h"
#include "bgpd/bgp_fsm.h"
#include "bgpd/bgp_debug.h"
#include "bgpd/bgp_errors.h"
#include "bgpd/bgp_mpath.h"
#include "bgpd/bgp_nexthop.h"
#include "bgpd/bgp_nht.h"
#include "bgpd/bgp_bfd.h"
#include "bgpd/bgp_label.h"
#if ENABLE_BGP_VNC
#include "bgpd/rfapi/rfapi_backend.h"
#include "bgpd/rfapi/vnc_export_bgp.h"
#endif
#include "bgpd/bgp_evpn.h"
#include "bgpd/bgp_mplsvpn.h"
#include "bgpd/bgp_labelpool.h"
#include "bgpd/bgp_pbr.h"
#include "bgpd/bgp_evpn_private.h"
#include "bgpd/bgp_mac.h"
/* All information about zebra. */
struct zclient *zclient = NULL;
/* Can we install into zebra? */
static inline int bgp_install_info_to_zebra(struct bgp *bgp)
{
if (zclient->sock <= 0)
return 0;
if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
zlog_debug("%s: No zebra instance to talk to, not installing information",
__PRETTY_FUNCTION__);
return 0;
}
return 1;
}
int zclient_num_connects;
/* Router-id update message from zebra. */
static int bgp_router_id_update(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct prefix router_id;
zebra_router_id_update_read(zclient->ibuf, &router_id);
if (BGP_DEBUG(zebra, ZEBRA)) {
char buf[PREFIX2STR_BUFFER];
prefix2str(&router_id, buf, sizeof(buf));
zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id, buf);
}
bgp_router_id_zebra_bump(vrf_id, &router_id);
return 0;
}
/* Nexthop update message from zebra. */
static int bgp_read_nexthop_update(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
bgp_parse_nexthop_update(command, vrf_id);
return 0;
}
static int bgp_read_import_check_update(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
bgp_parse_nexthop_update(command, vrf_id);
return 0;
}
/* Set or clear interface on which unnumbered neighbor is configured. This
* would in turn cause BGP to initiate or turn off IPv6 RAs on this
* interface.
*/
static void bgp_update_interface_nbrs(struct bgp *bgp, struct interface *ifp,
struct interface *upd_ifp)
{
struct listnode *node, *nnode;
struct peer *peer;
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)) {
if (upd_ifp) {
peer->ifp = upd_ifp;
bgp_zebra_initiate_radv(bgp, peer);
} else {
bgp_zebra_terminate_radv(bgp, peer);
peer->ifp = upd_ifp;
}
}
}
}
static int bgp_read_fec_update(int command, struct zclient *zclient,
zebra_size_t length)
{
bgp_parse_fec_update();
return 0;
}
static void bgp_start_interface_nbrs(struct bgp *bgp, struct interface *ifp)
{
struct listnode *node, *nnode;
struct peer *peer;
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)
&& peer->status != Established) {
if (peer_active(peer))
BGP_EVENT_ADD(peer, BGP_Stop);
BGP_EVENT_ADD(peer, BGP_Start);
}
}
}
static void bgp_nbr_connected_add(struct bgp *bgp, struct nbr_connected *ifc)
{
struct listnode *node;
struct connected *connected;
struct interface *ifp;
struct prefix *p;
/* Kick-off the FSM for any relevant peers only if there is a
* valid local address on the interface.
*/
ifp = ifc->ifp;
for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
p = connected->address;
if (p->family == AF_INET6
&& IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
break;
}
if (!connected)
return;
bgp_start_interface_nbrs(bgp, ifp);
}
static void bgp_nbr_connected_delete(struct bgp *bgp, struct nbr_connected *ifc,
int del)
{
struct listnode *node, *nnode;
struct peer *peer;
struct interface *ifp;
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
if (peer->conf_if
&& (strcmp(peer->conf_if, ifc->ifp->name) == 0)) {
peer->last_reset = PEER_DOWN_NBR_ADDR_DEL;
BGP_EVENT_ADD(peer, BGP_Stop);
}
}
/* Free neighbor also, if we're asked to. */
if (del) {
ifp = ifc->ifp;
listnode_delete(ifp->nbr_connected, ifc);
nbr_connected_free(ifc);
}
}
/* Inteface addition message from zebra. */
static int bgp_interface_add(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct interface *ifp;
struct bgp *bgp;
ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
if (!ifp) // unexpected
return 0;
if (BGP_DEBUG(zebra, ZEBRA) && ifp)
zlog_debug("Rx Intf add VRF %u IF %s", vrf_id, ifp->name);
bgp = bgp_lookup_by_vrf_id(vrf_id);
if (!bgp)
return 0;
bgp_mac_add_mac_entry(ifp);
bgp_update_interface_nbrs(bgp, ifp, ifp);
return 0;
}
static int bgp_interface_delete(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct stream *s;
struct interface *ifp;
struct bgp *bgp;
bgp = bgp_lookup_by_vrf_id(vrf_id);
s = zclient->ibuf;
ifp = zebra_interface_state_read(s, vrf_id);
if (!ifp) /* This may happen if we've just unregistered for a VRF. */
return 0;
if (BGP_DEBUG(zebra, ZEBRA))
zlog_debug("Rx Intf del VRF %u IF %s", vrf_id, ifp->name);
if (bgp)
bgp_update_interface_nbrs(bgp, ifp, NULL);
bgp_mac_del_mac_entry(ifp);
if_set_index(ifp, IFINDEX_INTERNAL);
return 0;
}
static int bgp_interface_up(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct stream *s;
struct interface *ifp;
struct connected *c;
struct nbr_connected *nc;
struct listnode *node, *nnode;
struct bgp *bgp;
bgp = bgp_lookup_by_vrf_id(vrf_id);
s = zclient->ibuf;
ifp = zebra_interface_state_read(s, vrf_id);
if (!ifp)
return 0;
bgp_mac_add_mac_entry(ifp);
if (BGP_DEBUG(zebra, ZEBRA))
zlog_debug("Rx Intf up VRF %u IF %s", vrf_id, ifp->name);
if (!bgp)
return 0;
for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
bgp_connected_add(bgp, c);
for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
bgp_nbr_connected_add(bgp, nc);
return 0;
}
static int bgp_interface_down(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct stream *s;
struct interface *ifp;
struct connected *c;
struct nbr_connected *nc;
struct listnode *node, *nnode;
struct bgp *bgp;
struct peer *peer;
bgp = bgp_lookup_by_vrf_id(vrf_id);
s = zclient->ibuf;
ifp = zebra_interface_state_read(s, vrf_id);
if (!ifp)
return 0;
bgp_mac_del_mac_entry(ifp);
if (BGP_DEBUG(zebra, ZEBRA))
zlog_debug("Rx Intf down VRF %u IF %s", vrf_id, ifp->name);
if (!bgp)
return 0;
for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
bgp_connected_delete(bgp, c);
for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
bgp_nbr_connected_delete(bgp, nc, 1);
/* Fast external-failover */
if (!CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) {
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
#if defined(HAVE_CUMULUS)
/* Take down directly connected EBGP peers as well as
* 1-hop BFD
* tracked (directly connected) IBGP peers.
*/
if ((peer->ttl != 1) && (peer->gtsm_hops != 1)
&& (!peer->bfd_info
|| bgp_bfd_is_peer_multihop(peer)))
#else
/* Take down directly connected EBGP peers */
if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
#endif
continue;
if (ifp == peer->nexthop.ifp) {
BGP_EVENT_ADD(peer, BGP_Stop);
peer->last_reset = PEER_DOWN_IF_DOWN;
}
}
}
return 0;
}
static int bgp_interface_address_add(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct connected *ifc;
struct bgp *bgp;
bgp = bgp_lookup_by_vrf_id(vrf_id);
ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
if (ifc == NULL)
return 0;
if (bgp_debug_zebra(ifc->address)) {
char buf[PREFIX2STR_BUFFER];
prefix2str(ifc->address, buf, sizeof(buf));
zlog_debug("Rx Intf address add VRF %u IF %s addr %s", vrf_id,
ifc->ifp->name, buf);
}
if (!bgp)
return 0;
if (if_is_operative(ifc->ifp)) {
bgp_connected_add(bgp, ifc);
/* If we have learnt of any neighbors on this interface,
* check to kick off any BGP interface-based neighbors,
* but only if this is a link-local address.
*/
if (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6)
&& !list_isempty(ifc->ifp->nbr_connected))
bgp_start_interface_nbrs(bgp, ifc->ifp);
}
return 0;
}
static int bgp_interface_address_delete(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct connected *ifc;
struct bgp *bgp;
bgp = bgp_lookup_by_vrf_id(vrf_id);
ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
if (ifc == NULL)
return 0;
if (bgp_debug_zebra(ifc->address)) {
char buf[PREFIX2STR_BUFFER];
prefix2str(ifc->address, buf, sizeof(buf));
zlog_debug("Rx Intf address del VRF %u IF %s addr %s", vrf_id,
ifc->ifp->name, buf);
}
if (bgp && if_is_operative(ifc->ifp)) {
bgp_connected_delete(bgp, ifc);
}
connected_free(ifc);
return 0;
}
static int bgp_interface_nbr_address_add(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct nbr_connected *ifc = NULL;
struct bgp *bgp;
ifc = zebra_interface_nbr_address_read(command, zclient->ibuf, vrf_id);
if (ifc == NULL)
return 0;
if (bgp_debug_zebra(ifc->address)) {
char buf[PREFIX2STR_BUFFER];
prefix2str(ifc->address, buf, sizeof(buf));
zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s", vrf_id,
ifc->ifp->name, buf);
}
if (if_is_operative(ifc->ifp)) {
bgp = bgp_lookup_by_vrf_id(vrf_id);
if (bgp)
bgp_nbr_connected_add(bgp, ifc);
}
return 0;
}
static int bgp_interface_nbr_address_delete(int command,
struct zclient *zclient,
zebra_size_t length,
vrf_id_t vrf_id)
{
struct nbr_connected *ifc = NULL;
struct bgp *bgp;
ifc = zebra_interface_nbr_address_read(command, zclient->ibuf, vrf_id);
if (ifc == NULL)
return 0;
if (bgp_debug_zebra(ifc->address)) {
char buf[PREFIX2STR_BUFFER];
prefix2str(ifc->address, buf, sizeof(buf));
zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s", vrf_id,
ifc->ifp->name, buf);
}
if (if_is_operative(ifc->ifp)) {
bgp = bgp_lookup_by_vrf_id(vrf_id);
if (bgp)
bgp_nbr_connected_delete(bgp, ifc, 0);
}
nbr_connected_free(ifc);
return 0;
}
/* VRF update for an interface. */
static int bgp_interface_vrf_update(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct interface *ifp;
vrf_id_t new_vrf_id;
struct connected *c;
struct nbr_connected *nc;
struct listnode *node, *nnode;
struct bgp *bgp;
struct peer *peer;
ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
&new_vrf_id);
if (!ifp)
return 0;
if (BGP_DEBUG(zebra, ZEBRA) && ifp)
zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id,
ifp->name, new_vrf_id);
bgp = bgp_lookup_by_vrf_id(vrf_id);
if (bgp) {
for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
bgp_connected_delete(bgp, c);
for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
bgp_nbr_connected_delete(bgp, nc, 1);
/* Fast external-failover */
if (!CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) {
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
continue;
if (ifp == peer->nexthop.ifp)
BGP_EVENT_ADD(peer, BGP_Stop);
}
}
}
if_update_to_new_vrf(ifp, new_vrf_id);
bgp = bgp_lookup_by_vrf_id(new_vrf_id);
if (!bgp)
return 0;
for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
bgp_connected_add(bgp, c);
for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
bgp_nbr_connected_add(bgp, nc);
return 0;
}
/* Zebra route add and delete treatment. */
static int zebra_read_route(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
enum nexthop_types_t nhtype;
struct zapi_route api;
union g_addr nexthop;
ifindex_t ifindex;
int add, i;
struct bgp *bgp;
bgp = bgp_lookup_by_vrf_id(vrf_id);
if (!bgp)
return 0;
if (zapi_route_decode(zclient->ibuf, &api) < 0)
return -1;
/* we completely ignore srcdest routes for now. */
if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
return 0;
/* ignore link-local address. */
if (api.prefix.family == AF_INET6
&& IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6))
return 0;
nexthop = api.nexthops[0].gate;
ifindex = api.nexthops[0].ifindex;
nhtype = api.nexthops[0].type;
add = (command == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
if (add) {
/*
* The ADD message is actually an UPDATE and there is no
* explicit DEL
* for a prior redistributed route, if any. So, perform an
* implicit
* DEL processing for the same redistributed route from any
* other
* source type.
*/
for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
if (i != api.type)
bgp_redistribute_delete(bgp, &api.prefix, i,
api.instance);
}
/* Now perform the add/update. */
bgp_redistribute_add(bgp, &api.prefix, &nexthop, ifindex,
nhtype, api.metric, api.type, api.instance,
api.tag);
} else {
bgp_redistribute_delete(bgp, &api.prefix, api.type,
api.instance);
}
if (bgp_debug_zebra(&api.prefix)) {
char buf[2][PREFIX_STRLEN];
prefix2str(&api.prefix, buf[0], sizeof(buf[0]));
if (add) {
inet_ntop(api.prefix.family, &nexthop, buf[1],
sizeof(buf[1]));
zlog_debug(
"Rx route ADD VRF %u %s[%d] %s nexthop %s (type %d if %u) metric %u tag %" ROUTE_TAG_PRI,
vrf_id, zebra_route_string(api.type),
api.instance, buf[0], buf[1], nhtype,
ifindex, api.metric, api.tag);
} else {
zlog_debug(
"Rx route DEL VRF %u %s[%d] %s",
vrf_id, zebra_route_string(api.type),
api.instance, buf[0]);
}
}
return 0;
}
struct interface *if_lookup_by_ipv4(struct in_addr *addr, vrf_id_t vrf_id)
{
struct vrf *vrf;
struct listnode *cnode;
struct interface *ifp;
struct connected *connected;
struct prefix_ipv4 p;
struct prefix *cp;
vrf = vrf_lookup_by_id(vrf_id);
if (!vrf)
return NULL;
p.family = AF_INET;
p.prefix = *addr;
p.prefixlen = IPV4_MAX_BITLEN;
FOR_ALL_INTERFACES (vrf, ifp) {
for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
cp = connected->address;
if (cp->family == AF_INET)
if (prefix_match(cp, (struct prefix *)&p))
return ifp;
}
}
return NULL;
}
struct interface *if_lookup_by_ipv4_exact(struct in_addr *addr, vrf_id_t vrf_id)
{
struct vrf *vrf;
struct listnode *cnode;
struct interface *ifp;
struct connected *connected;
struct prefix *cp;
vrf = vrf_lookup_by_id(vrf_id);
if (!vrf)
return NULL;
FOR_ALL_INTERFACES (vrf, ifp) {
for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
cp = connected->address;
if (cp->family == AF_INET)
if (IPV4_ADDR_SAME(&cp->u.prefix4, addr))
return ifp;
}
}
return NULL;
}
struct interface *if_lookup_by_ipv6(struct in6_addr *addr, ifindex_t ifindex,
vrf_id_t vrf_id)
{
struct vrf *vrf;
struct listnode *cnode;
struct interface *ifp;
struct connected *connected;
struct prefix_ipv6 p;
struct prefix *cp;
vrf = vrf_lookup_by_id(vrf_id);
if (!vrf)
return NULL;
p.family = AF_INET6;
p.prefix = *addr;
p.prefixlen = IPV6_MAX_BITLEN;
FOR_ALL_INTERFACES (vrf, ifp) {
for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
cp = connected->address;
if (cp->family == AF_INET6)
if (prefix_match(cp, (struct prefix *)&p)) {
if (IN6_IS_ADDR_LINKLOCAL(
&cp->u.prefix6)) {
if (ifindex == ifp->ifindex)
return ifp;
} else
return ifp;
}
}
}
return NULL;
}
struct interface *if_lookup_by_ipv6_exact(struct in6_addr *addr,
ifindex_t ifindex, vrf_id_t vrf_id)
{
struct vrf *vrf;
struct listnode *cnode;
struct interface *ifp;
struct connected *connected;
struct prefix *cp;
vrf = vrf_lookup_by_id(vrf_id);
if (!vrf)
return NULL;
FOR_ALL_INTERFACES (vrf, ifp) {
for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
cp = connected->address;
if (cp->family == AF_INET6)
if (IPV6_ADDR_SAME(&cp->u.prefix6, addr)) {
if (IN6_IS_ADDR_LINKLOCAL(
&cp->u.prefix6)) {
if (ifindex == ifp->ifindex)
return ifp;
} else
return ifp;
}
}
}
return NULL;
}
static int if_get_ipv6_global(struct interface *ifp, struct in6_addr *addr)
{
struct listnode *cnode;
struct connected *connected;
struct prefix *cp;
for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
cp = connected->address;
if (cp->family == AF_INET6)
if (!IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
return 1;
}
}
return 0;
}
static int if_get_ipv6_local(struct interface *ifp, struct in6_addr *addr)
{
struct listnode *cnode;
struct connected *connected;
struct prefix *cp;
for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
cp = connected->address;
if (cp->family == AF_INET6)
if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
return 1;
}
}
return 0;
}
static int if_get_ipv4_address(struct interface *ifp, struct in_addr *addr)
{
struct listnode *cnode;
struct connected *connected;
struct prefix *cp;
for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
cp = connected->address;
if ((cp->family == AF_INET)
&& !ipv4_martian(&(cp->u.prefix4))) {
*addr = cp->u.prefix4;
return 1;
}
}
return 0;
}
bool bgp_zebra_nexthop_set(union sockunion *local, union sockunion *remote,
struct bgp_nexthop *nexthop, struct peer *peer)
{
int ret = 0;
struct interface *ifp = NULL;
memset(nexthop, 0, sizeof(struct bgp_nexthop));
if (!local)
return false;
if (!remote)
return false;
if (local->sa.sa_family == AF_INET) {
nexthop->v4 = local->sin.sin_addr;
if (peer->update_if)
ifp = if_lookup_by_name(peer->update_if,
peer->bgp->vrf_id);
else
ifp = if_lookup_by_ipv4_exact(&local->sin.sin_addr,
peer->bgp->vrf_id);
}
if (local->sa.sa_family == AF_INET6) {
if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
if (peer->conf_if || peer->ifname)
ifp = if_lookup_by_name(peer->conf_if
? peer->conf_if
: peer->ifname,
peer->bgp->vrf_id);
} else if (peer->update_if)
ifp = if_lookup_by_name(peer->update_if,
peer->bgp->vrf_id);
else
ifp = if_lookup_by_ipv6_exact(&local->sin6.sin6_addr,
local->sin6.sin6_scope_id,
peer->bgp->vrf_id);
}
if (!ifp) {
/*
* BGP views do not currently get proper data
* from zebra( when attached ) to be able to
* properly resolve nexthops, so give this
* instance type a pass.
*/
if (peer->bgp->inst_type == BGP_INSTANCE_TYPE_VIEW)
return true;
/*
* If we have no interface data but we have established
* some connection w/ zebra than something has gone
* terribly terribly wrong here, so say this failed
* If we do not any zebra connection then not
* having a ifp pointer is ok.
*/
return zclient_num_connects ? false : true;
}
nexthop->ifp = ifp;
/* IPv4 connection, fetch and store IPv6 local address(es) if any. */
if (local->sa.sa_family == AF_INET) {
/* IPv6 nexthop*/
ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
if (!ret) {
/* There is no global nexthop. Use link-local address as
* both the
* global and link-local nexthop. In this scenario, the
* expectation
* for interop is that the network admin would use a
* route-map to
* specify the global IPv6 nexthop.
*/
if_get_ipv6_local(ifp, &nexthop->v6_global);
memcpy(&nexthop->v6_local, &nexthop->v6_global,
IPV6_MAX_BYTELEN);
} else
if_get_ipv6_local(ifp, &nexthop->v6_local);
if (if_lookup_by_ipv4(&remote->sin.sin_addr, peer->bgp->vrf_id))
peer->shared_network = 1;
else
peer->shared_network = 0;
}
/* IPv6 connection, fetch and store IPv4 local address if any. */
if (local->sa.sa_family == AF_INET6) {
struct interface *direct = NULL;
/* IPv4 nexthop. */
ret = if_get_ipv4_address(ifp, &nexthop->v4);
if (!ret && peer->local_id.s_addr)
nexthop->v4 = peer->local_id;
/* Global address*/
if (!IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
memcpy(&nexthop->v6_global, &local->sin6.sin6_addr,
IPV6_MAX_BYTELEN);
/* If directory connected set link-local address. */
direct = if_lookup_by_ipv6(&remote->sin6.sin6_addr,
remote->sin6.sin6_scope_id,
peer->bgp->vrf_id);
if (direct)
if_get_ipv6_local(ifp, &nexthop->v6_local);
} else
/* Link-local address. */
{
ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
/* If there is no global address. Set link-local
address as
global. I know this break RFC specification... */
/* In this scenario, the expectation for interop is that
* the
* network admin would use a route-map to specify the
* global
* IPv6 nexthop.
*/
if (!ret)
memcpy(&nexthop->v6_global,
&local->sin6.sin6_addr,
IPV6_MAX_BYTELEN);
/* Always set the link-local address */
memcpy(&nexthop->v6_local, &local->sin6.sin6_addr,
IPV6_MAX_BYTELEN);
}
if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)
|| if_lookup_by_ipv6(&remote->sin6.sin6_addr,
remote->sin6.sin6_scope_id,
peer->bgp->vrf_id))
peer->shared_network = 1;
else
peer->shared_network = 0;
}
/* KAME stack specific treatment. */
#ifdef KAME
if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_global)
&& IN6_LINKLOCAL_IFINDEX(nexthop->v6_global)) {
SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_global, 0);
}
if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_local)
&& IN6_LINKLOCAL_IFINDEX(nexthop->v6_local)) {
SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_local, 0);
}
#endif /* KAME */
/* If we have identified the local interface, there is no error for now.
*/
return true;
}
static struct in6_addr *
bgp_path_info_to_ipv6_nexthop(struct bgp_path_info *path, ifindex_t *ifindex)
{
struct in6_addr *nexthop = NULL;
/* Only global address nexthop exists. */
if (path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL
|| path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_VPNV6_GLOBAL) {
nexthop = &path->attr->mp_nexthop_global;
if (IN6_IS_ADDR_LINKLOCAL(nexthop))
*ifindex = path->attr->nh_ifindex;
}
/* If both global and link-local address present. */
if (path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
|| path->attr->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL) {
/* Check if route-map is set to prefer global over link-local */
if (path->attr->mp_nexthop_prefer_global) {
nexthop = &path->attr->mp_nexthop_global;
if (IN6_IS_ADDR_LINKLOCAL(nexthop))
*ifindex = path->attr->nh_ifindex;
} else {
/* Workaround for Cisco's nexthop bug. */
if (IN6_IS_ADDR_UNSPECIFIED(
&path->attr->mp_nexthop_global)
&& path->peer->su_remote->sa.sa_family
== AF_INET6) {
nexthop =
&path->peer->su_remote->sin6.sin6_addr;
if (IN6_IS_ADDR_LINKLOCAL(nexthop))
*ifindex = path->peer->nexthop.ifp
->ifindex;
} else {
nexthop = &path->attr->mp_nexthop_local;
if (IN6_IS_ADDR_LINKLOCAL(nexthop))
*ifindex = path->attr->nh_lla_ifindex;
}
}
}
return nexthop;
}
static int bgp_table_map_apply(struct route_map *map, struct prefix *p,
struct bgp_path_info *path)
{
route_map_result_t ret;
ret = route_map_apply(map, p, RMAP_BGP, path);
bgp_attr_flush(path->attr);
if (ret != RMAP_DENYMATCH)
return 1;
if (bgp_debug_zebra(p)) {
if (p->family == AF_INET) {
char buf[2][INET_ADDRSTRLEN];
zlog_debug(