/
vnc_import_bgp.c
3004 lines (2493 loc) · 73.5 KB
/
vnc_import_bgp.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
/*
*
* Copyright 2009-2016, LabN Consulting, L.L.C.
*
*
* This program 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
* of the License, or (at your option) any later version.
*
* This program 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
*/
/*
* File: vnc_import_bgp.c
* Purpose: Import routes from BGP unicast directly (not via zebra)
*/
#include "lib/zebra.h"
#include "lib/prefix.h"
#include "lib/table.h"
#include "lib/vty.h"
#include "lib/log.h"
#include "lib/memory.h"
#include "lib/linklist.h"
#include "lib/plist.h"
#include "lib/routemap.h"
#include "lib/lib_errors.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_ecommunity.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_mplsvpn.h" /* for RD_TYPE_IP */
#include "bgpd/rfapi/vnc_export_bgp.h"
#include "bgpd/rfapi/bgp_rfapi_cfg.h"
#include "bgpd/rfapi/rfapi.h"
#include "bgpd/rfapi/rfapi_import.h"
#include "bgpd/rfapi/rfapi_private.h"
#include "bgpd/rfapi/rfapi_monitor.h"
#include "bgpd/rfapi/rfapi_vty.h"
#include "bgpd/rfapi/vnc_import_bgp.h"
#include "bgpd/rfapi/vnc_import_bgp_p.h"
#include "bgpd/rfapi/vnc_debug.h"
#define ENABLE_VNC_RHNCK
#define DEBUG_RHN_LIST 0
static struct rfapi_descriptor vncHDBgpDirect; /* dummy nve descriptor */
static struct rfapi_descriptor vncHDResolveNve; /* dummy nve descriptor */
/*
* For routes from another AS:
*
* If MED is set,
* LOCAL_PREF = 255 - MIN(255, MED)
* else
* LOCAL_PREF = default_local_pref
*
* For routes from the same AS:
*
* LOCAL_PREF unchanged
*/
uint32_t calc_local_pref(struct attr *attr, struct peer *peer)
{
uint32_t local_pref = 0;
if (!attr) {
if (peer) {
return peer->bgp->default_local_pref;
}
return bgp_get_default()->default_local_pref;
}
if (peer && (peer->as != peer->bgp->as)) {
if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC)) {
if (attr->med > 255) {
local_pref = 0;
} else {
local_pref = 255 - attr->med;
}
} else {
local_pref = peer->bgp->default_local_pref;
}
} else {
if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF)) {
local_pref = attr->local_pref;
} else {
if (peer && peer->bgp) {
local_pref = peer->bgp->default_local_pref;
}
}
}
return local_pref;
}
static int is_host_prefix(struct prefix *p)
{
switch (p->family) {
case AF_INET:
return (p->prefixlen == 32);
case AF_INET6:
return (p->prefixlen == 128);
}
return 0;
}
/***********************************************************************
* RHN list
***********************************************************************/
struct prefix_bag {
struct prefix hpfx; /* ce address = unicast nexthop */
struct prefix upfx; /* unicast prefix */
struct bgp_info *ubi; /* unicast route */
};
static const uint8_t maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
0xf8, 0xfc, 0xfe, 0xff};
int vnc_prefix_cmp(void *pfx1, void *pfx2)
{
int offset;
int shift;
uint8_t mask;
struct prefix *p1 = pfx1;
struct prefix *p2 = pfx2;
if (p1->family < p2->family)
return -1;
if (p1->family > p2->family)
return 1;
if (p1->prefixlen < p2->prefixlen)
return -1;
if (p1->prefixlen > p2->prefixlen)
return 1;
offset = p1->prefixlen / 8;
shift = p1->prefixlen % 8;
if (shift == 0 && offset) { /* catch aligned case */
offset--;
shift = 8;
}
/* Set both prefix's head pointer. */
const uint8_t *pp1 = (const uint8_t *)&p1->u.prefix;
const uint8_t *pp2 = (const uint8_t *)&p2->u.prefix;
while (offset--) {
if (*pp1 < *pp2)
return -1;
if (*pp1 > *pp2)
return 1;
++pp1;
++pp2;
}
mask = maskbit[shift];
if ((*pp1 & mask) < (*pp2 & mask))
return -1;
if ((*pp1 & mask) > (*pp2 & mask))
return 1;
return 0;
}
static void prefix_bag_free(void *pb)
{
XFREE(MTYPE_RFAPI_PREFIX_BAG, pb);
}
#if DEBUG_RHN_LIST
static void print_rhn_list(const char *tag1, const char *tag2)
{
struct bgp *bgp;
struct skiplist *sl;
struct skiplistnode *p;
struct prefix_bag *pb;
int count = 0;
bgp = bgp_get_default();
if (!bgp)
return;
sl = bgp->frapi->resolve_nve_nexthop;
if (!sl) {
vnc_zlog_debug_verbose("%s: %s: RHN List is empty",
(tag1 ? tag1 : ""), (tag2 ? tag2 : ""));
return;
}
vnc_zlog_debug_verbose("%s: %s: RHN list:", (tag1 ? tag1 : ""),
(tag2 ? tag2 : ""));
/* XXX uses secret knowledge of skiplist structure */
for (p = sl->header->forward[0]; p; p = p->forward[0]) {
char kbuf[PREFIX_STRLEN];
char hbuf[PREFIX_STRLEN];
char ubuf[PREFIX_STRLEN];
pb = p->value;
prefix2str(p->key, kbuf, sizeof(kbuf));
prefix2str(&pb->hpfx, hbuf, sizeof(hbuf));
prefix2str(&pb->upfx, ubuf, sizeof(ubuf));
vnc_zlog_debug_verbose(
"RHN Entry %d (q=%p): kpfx=%s, upfx=%s, hpfx=%s, ubi=%p",
++count, p, kbuf, ubuf, hbuf, pb->ubi);
}
}
#endif
#ifdef ENABLE_VNC_RHNCK
static void vnc_rhnck(char *tag)
{
struct bgp *bgp;
struct skiplist *sl;
struct skiplistnode *p;
bgp = bgp_get_default();
if (!bgp)
return;
sl = bgp->rfapi->resolve_nve_nexthop;
if (!sl)
return;
/* XXX uses secret knowledge of skiplist structure */
for (p = sl->header->forward[0]; p; p = p->forward[0]) {
struct prefix_bag *pb;
struct prefix *pkey;
afi_t afi;
struct prefix pfx_orig_nexthop;
memset(&pfx_orig_nexthop, 0,
sizeof(struct prefix)); /* keep valgrind happy */
pkey = p->key;
pb = p->value;
afi = family2afi(pb->upfx.family);
rfapiUnicastNexthop2Prefix(afi, pb->ubi->attr,
&pfx_orig_nexthop);
/* pb->hpfx, pb->ubi nexthop, pkey should all reflect the same
* pfx */
assert(!vnc_prefix_cmp(&pb->hpfx, pkey));
if (vnc_prefix_cmp(&pb->hpfx, &pfx_orig_nexthop)) {
char str_onh[PREFIX_STRLEN];
char str_nve_pfx[PREFIX_STRLEN];
prefix2str(&pfx_orig_nexthop, str_onh, sizeof(str_onh));
prefix2str(&pb->hpfx, str_nve_pfx, sizeof(str_nve_pfx));
vnc_zlog_debug_verbose(
"%s: %s: FATAL: resolve_nve_nexthop list item bi nexthop %s != nve pfx %s",
__func__, tag, str_onh, str_nve_pfx);
assert(0);
}
}
vnc_zlog_debug_verbose("%s: vnc_rhnck OK", tag);
}
#define VNC_RHNCK(n) do {char buf[BUFSIZ];sprintf(buf,"%s: %s", __func__, #n);vnc_rhnck(buf);} while (0)
#else
#define VNC_RHNCK(n)
#endif
/***********************************************************************
* Add/Delete Unicast Route
***********************************************************************/
/*
* "Adding a Route" import process
*/
/*
* extract and package information from the BGP unicast route.
* Return code 0 means OK, non-0 means drop.
*
* If return code is 0, caller MUST release ecom
*/
static int process_unicast_route(struct bgp *bgp, /* in */
afi_t afi, /* in */
struct prefix *prefix, /* in */
struct bgp_info *info, /* in */
struct ecommunity **ecom, /* OUT */
struct prefix *unicast_nexthop) /* OUT */
{
struct rfapi_cfg *hc = bgp->rfapi_cfg;
struct peer *peer = info->peer;
struct attr *attr = info->attr;
struct attr hattr;
struct route_map *rmap = NULL;
struct prefix pfx_orig_nexthop;
memset(&pfx_orig_nexthop, 0,
sizeof(struct prefix)); /* keep valgrind happy */
/*
* prefix list check
*/
if (hc->plist_redist[ZEBRA_ROUTE_BGP_DIRECT][afi]) {
vnc_zlog_debug_verbose("%s: HC prefix list is set, checking",
__func__);
if (prefix_list_apply(
hc->plist_redist[ZEBRA_ROUTE_BGP_DIRECT][afi],
prefix)
== PREFIX_DENY) {
vnc_zlog_debug_verbose(
"%s: prefix list returns DENY, blocking route",
__func__);
return -1;
}
vnc_zlog_debug_verbose(
"%s: prefix list returns PASS, allowing route",
__func__);
}
/* apply routemap, if any, later */
rmap = hc->routemap_redist[ZEBRA_ROUTE_BGP_DIRECT];
/*
* Extract original nexthop, which we expect to be a NVE connected
* router
* Note that this is the nexthop before any possible application of
* policy
*/
/*
* Incoming prefix is unicast. If v6, it is in multiprotocol area,
* but if v4 it is in attr->nexthop
*/
rfapiUnicastNexthop2Prefix(afi, attr, &pfx_orig_nexthop);
/*
* route map handling
* This code is here because it allocates an interned attr which
* must be freed before we return. It's easier to put it after
* all of the possible returns above.
*/
memset(&hattr, 0, sizeof(struct attr));
bgp_attr_dup(&hattr, attr); /* hattr becomes a ghost attr */
if (rmap) {
struct bgp_info info;
route_map_result_t ret;
memset(&info, 0, sizeof(info));
info.peer = peer;
info.attr = &hattr;
ret = route_map_apply(rmap, prefix, RMAP_BGP, &info);
if (ret == RMAP_DENYMATCH) {
bgp_attr_flush(&hattr);
vnc_zlog_debug_verbose(
"%s: route map \"%s\" says DENY, returning",
__func__, rmap->name);
return -1;
}
}
/*
* Get the (possibly altered by policy) unicast nexthop
* for later lookup in the Import Table by caller
*/
rfapiUnicastNexthop2Prefix(afi, &hattr, unicast_nexthop);
if (hattr.ecommunity)
*ecom = ecommunity_dup(hattr.ecommunity);
else
*ecom = ecommunity_new();
/*
* Done with hattr, clean up
*/
bgp_attr_flush(&hattr);
/*
* Add EC that carries original NH of iBGP route (2 bytes = magic
* value indicating it came from an VNC gateway; default 5226, but
* must be user configurable). Note that this is the nexthop before
* any application of policy.
*/
{
struct ecommunity_val vnc_gateway_magic;
uint16_t localadmin;
/* Using route origin extended community type */
memset(&vnc_gateway_magic, 0, sizeof(vnc_gateway_magic));
vnc_gateway_magic.val[0] = 0x01;
vnc_gateway_magic.val[1] = 0x03;
/* Only works for IPv4 nexthops */
if (prefix->family == AF_INET) {
memcpy(vnc_gateway_magic.val + 2,
&unicast_nexthop->u.prefix4, 4);
}
localadmin = htons(hc->resolve_nve_roo_local_admin);
memcpy(vnc_gateway_magic.val + 6, (char *)&localadmin, 2);
ecommunity_add_val(*ecom, &vnc_gateway_magic);
}
return 0;
}
static void vnc_import_bgp_add_route_mode_resolve_nve_one_bi(
struct bgp *bgp, afi_t afi, struct bgp_info *bi, /* VPN bi */
struct prefix_rd *prd, /* RD */
struct prefix *prefix, /* unicast route prefix */
uint32_t *local_pref, /* NULL = no local_pref */
uint32_t *med, /* NULL = no med */
struct ecommunity *ecom) /* generated ecoms */
{
struct prefix un;
struct prefix nexthop;
struct rfapi_ip_addr nexthop_h;
uint32_t lifetime;
uint32_t *plifetime;
struct bgp_attr_encap_subtlv *encaptlvs;
uint32_t label = 0;
struct rfapi_un_option optary[3];
struct rfapi_un_option *opt = NULL;
int cur_opt = 0;
vnc_zlog_debug_verbose("%s: entry", __func__);
if (bi->type != ZEBRA_ROUTE_BGP && bi->type != ZEBRA_ROUTE_BGP_DIRECT) {
return;
}
if (bi->sub_type != BGP_ROUTE_NORMAL && bi->sub_type != BGP_ROUTE_STATIC
&& bi->sub_type != BGP_ROUTE_RFP) {
return;
}
if (CHECK_FLAG(bi->flags, BGP_INFO_REMOVED))
return;
vncHDResolveNve.peer = bi->peer;
if (!rfapiGetVncTunnelUnAddr(bi->attr, &un)) {
if (rfapiQprefix2Raddr(&un, &vncHDResolveNve.un_addr))
return;
} else {
memset(&vncHDResolveNve.un_addr, 0,
sizeof(vncHDResolveNve.un_addr));
}
/* Use nexthop of VPN route as nexthop of constructed route */
rfapiNexthop2Prefix(bi->attr, &nexthop);
rfapiQprefix2Raddr(&nexthop, &nexthop_h);
if (rfapiGetVncLifetime(bi->attr, &lifetime)) {
plifetime = NULL;
} else {
plifetime = &lifetime;
}
if (bi->attr) {
encaptlvs = bi->attr->vnc_subtlvs;
if (bi->attr->encap_tunneltype != BGP_ENCAP_TYPE_RESERVED
&& bi->attr->encap_tunneltype != BGP_ENCAP_TYPE_MPLS) {
if (opt != NULL)
opt->next = &optary[cur_opt];
opt = &optary[cur_opt++];
memset(opt, 0, sizeof(struct rfapi_un_option));
opt->type = RFAPI_UN_OPTION_TYPE_TUNNELTYPE;
opt->v.tunnel.type = bi->attr->encap_tunneltype;
/* TBD parse bi->attr->extra->encap_subtlvs */
}
} else {
encaptlvs = NULL;
}
struct ecommunity *new_ecom = ecommunity_dup(ecom);
if (bi->attr && bi->attr->ecommunity)
ecommunity_merge(new_ecom, bi->attr->ecommunity);
if (bi->extra)
label = decode_label(&bi->extra->label[0]);
add_vnc_route(&vncHDResolveNve, bgp, SAFI_MPLS_VPN,
prefix, /* unicast route prefix */
prd, &nexthop_h, /* new nexthop */
local_pref, plifetime,
(struct bgp_tea_options *)encaptlvs, /* RFP options */
opt, NULL, new_ecom, med, /* NULL => don't set med */
(label ? &label : NULL), /* NULL= default */
ZEBRA_ROUTE_BGP_DIRECT, BGP_ROUTE_REDISTRIBUTE,
RFAPI_AHR_RFPOPT_IS_VNCTLV); /* flags */
ecommunity_free(&new_ecom);
}
static void vnc_import_bgp_add_route_mode_resolve_nve_one_rd(
struct prefix_rd *prd, /* RD */
struct bgp_table *table_rd, /* per-rd VPN route table */
afi_t afi, struct bgp *bgp, struct prefix *prefix, /* unicast prefix */
struct ecommunity *ecom, /* generated ecoms */
uint32_t *local_pref, /* NULL = no local_pref */
uint32_t *med, /* NULL = no med */
struct prefix *ubi_nexthop) /* unicast nexthop */
{
struct bgp_node *bn;
struct bgp_info *bi;
if (!table_rd)
return;
{
char str_nh[PREFIX_STRLEN];
prefix2str(ubi_nexthop, str_nh, sizeof(str_nh));
vnc_zlog_debug_verbose("%s: ubi_nexthop=%s", __func__, str_nh);
}
/* exact match */
bn = bgp_node_lookup(table_rd, ubi_nexthop);
if (!bn) {
vnc_zlog_debug_verbose(
"%s: no match in RD's table for ubi_nexthop", __func__);
return;
}
/* Iterate over bgp_info items at this node */
for (bi = bn->info; bi; bi = bi->next) {
vnc_import_bgp_add_route_mode_resolve_nve_one_bi(
bgp, afi, bi, /* VPN bi */
prd, prefix, local_pref, med, ecom);
}
bgp_unlock_node(bn);
}
static void vnc_import_bgp_add_route_mode_resolve_nve(
struct bgp *bgp, struct prefix *prefix, /* unicast prefix */
struct bgp_info *info) /* unicast info */
{
afi_t afi = family2afi(prefix->family);
struct prefix pfx_unicast_nexthop = {0}; /* happy valgrind */
struct ecommunity *ecom = NULL;
uint32_t local_pref;
uint32_t *med = NULL;
struct prefix_bag *pb;
struct bgp_node *bnp; /* prd table node */
/*debugging */
if (VNC_DEBUG(VERBOSE)) {
char str_pfx[PREFIX_STRLEN];
char str_nh[PREFIX_STRLEN];
struct prefix nh;
prefix2str(prefix, str_pfx, sizeof(str_pfx));
nh.prefixlen = 0;
rfapiUnicastNexthop2Prefix(afi, info->attr, &nh);
if (nh.prefixlen) {
prefix2str(&nh, str_nh, sizeof(str_nh));
} else {
str_nh[0] = '?';
str_nh[1] = 0;
}
vnc_zlog_debug_verbose(
"%s(bgp=%p, unicast prefix=%s, unicast nh=%s)",
__func__, bgp, str_pfx, str_nh);
}
if (info->type != ZEBRA_ROUTE_BGP) {
vnc_zlog_debug_verbose(
"%s: unicast type %d=\"%s\" is not %d=%s, skipping",
__func__, info->type, zebra_route_string(info->type),
ZEBRA_ROUTE_BGP, "ZEBRA_ROUTE_BGP");
return;
}
/*
* Preliminary checks
*/
if (!afi) {
flog_err(LIB_ERR_DEVELOPMENT, "%s: can't get afi of prefix",
__func__);
return;
}
if (!(bgp->rfapi_cfg)) {
vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
__func__);
return;
}
/* check vnc redist flag for bgp direct routes */
if (!bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT]) {
vnc_zlog_debug_verbose(
"%s: bgp->rfapi_cfg->redist[afi=%d][type=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
__func__, afi);
return;
}
if (process_unicast_route(bgp, afi, prefix, info, &ecom,
&pfx_unicast_nexthop)) {
vnc_zlog_debug_verbose(
"%s: process_unicast_route error, skipping", __func__);
return;
}
local_pref = calc_local_pref(info->attr, info->peer);
if (info->attr
&& (info->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))) {
med = &info->attr->med;
}
/*
* At this point, we have allocated:
*
* ecom ecommunity ptr, union of unicast and ROO parts (no NVE part)
*
* And we have set:
*
* pfx_unicast_nexthop nexthop of uncast route
*/
if (!bgp->rfapi->resolve_nve_nexthop) {
bgp->rfapi->resolve_nve_nexthop =
skiplist_new(SKIPLIST_FLAG_ALLOW_DUPLICATES,
vnc_prefix_cmp, prefix_bag_free);
}
pb = XCALLOC(MTYPE_RFAPI_PREFIX_BAG, sizeof(struct prefix_bag));
pb->hpfx = pfx_unicast_nexthop;
pb->ubi = info;
pb->upfx = *prefix;
bgp_info_lock(info); /* skiplist refers to it */
skiplist_insert(bgp->rfapi->resolve_nve_nexthop, &pb->hpfx, pb);
/*
* Iterate over RDs in VPN RIB. For each RD, look up unicast nexthop
* (exact match, /32). If an exact match is found, call add_vnc_route.
*/
for (bnp = bgp_table_top(bgp->rib[afi][SAFI_MPLS_VPN]); bnp;
bnp = bgp_route_next(bnp)) {
struct bgp_table *table;
table = (struct bgp_table *)(bnp->info);
if (!table)
continue;
vnc_import_bgp_add_route_mode_resolve_nve_one_rd(
(struct prefix_rd *)&bnp->p, table, afi, bgp, prefix,
ecom, &local_pref, med, &pfx_unicast_nexthop);
}
if (ecom)
ecommunity_free(&ecom);
vnc_zlog_debug_verbose("%s: done", __func__);
}
static void vnc_import_bgp_add_route_mode_plain(struct bgp *bgp,
struct prefix *prefix,
struct bgp_info *info)
{
afi_t afi = family2afi(prefix->family);
struct peer *peer = info->peer;
struct attr *attr = info->attr;
struct attr hattr;
struct rfapi_cfg *hc = bgp->rfapi_cfg;
struct attr *iattr = NULL;
struct rfapi_ip_addr vnaddr;
struct prefix vn_pfx_space;
struct prefix *vn_pfx = NULL;
int ahr_flags = 0;
struct ecommunity *ecom = NULL;
struct prefix_rd prd;
struct route_map *rmap = NULL;
uint32_t local_pref;
uint32_t *med = NULL;
{
char buf[PREFIX_STRLEN];
prefix2str(prefix, buf, sizeof(buf));
vnc_zlog_debug_verbose("%s(prefix=%s) entry", __func__, buf);
}
if (!afi) {
flog_err(LIB_ERR_DEVELOPMENT, "%s: can't get afi of prefix",
__func__);
return;
}
if (!hc) {
vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
__func__);
return;
}
/* check vnc redist flag for bgp direct routes */
if (!bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT]) {
vnc_zlog_debug_verbose(
"%s: bgp->rfapi_cfg->redist[afi=%d][type=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
__func__, afi);
return;
}
/*
* mode "plain" specific code
*/
{
vnc_zlog_debug_verbose("%s: NOT using redist RFG", __func__);
/*
* prefix list check
*/
if (hc->plist_redist[ZEBRA_ROUTE_BGP_DIRECT][afi]) {
vnc_zlog_debug_verbose(
"%s: HC prefix list is set, checking",
__func__);
if (prefix_list_apply(
hc->plist_redist[ZEBRA_ROUTE_BGP_DIRECT]
[afi],
prefix)
== PREFIX_DENY) {
vnc_zlog_debug_verbose(
"%s: prefix list returns DENY, blocking route",
__func__);
return;
}
vnc_zlog_debug_verbose(
"%s: prefix list returns PASS, allowing route",
__func__);
}
/* apply routemap, if any, later */
rmap = hc->routemap_redist[ZEBRA_ROUTE_BGP_DIRECT];
/*
* Incoming prefix is unicast. If v6, it is in multiprotocol
* area,
* but if v4 it is in attr->nexthop
*/
rfapiUnicastNexthop2Prefix(afi, attr, &vn_pfx_space);
vn_pfx = &vn_pfx_space;
/* UN address */
ahr_flags |= RFAPI_AHR_NO_TUNNEL_SUBTLV;
}
if (VNC_DEBUG(IMPORT_BGP_ADD_ROUTE)) {
char buf[PREFIX_STRLEN];
prefix2str(vn_pfx, buf, sizeof(buf));
vnc_zlog_debug_any("%s vn_pfx=%s", __func__, buf);
}
/*
* Compute VN address
*/
if (rfapiQprefix2Raddr(vn_pfx, &vnaddr)) {
vnc_zlog_debug_verbose("%s: redist VN invalid, skipping",
__func__);
return;
}
/*
* route map handling
* This code is here because it allocates an interned attr which
* must be freed before we return. It's easier to put it after
* all of the possible returns above.
*/
memset(&hattr, 0, sizeof(struct attr));
bgp_attr_dup(&hattr, attr); /* hattr becomes a ghost attr */
if (rmap) {
struct bgp_info info;
route_map_result_t ret;
memset(&info, 0, sizeof(info));
info.peer = peer;
info.attr = &hattr;
ret = route_map_apply(rmap, prefix, RMAP_BGP, &info);
if (ret == RMAP_DENYMATCH) {
bgp_attr_flush(&hattr);
vnc_zlog_debug_verbose(
"%s: route map \"%s\" says DENY, returning",
__func__, rmap->name);
return;
}
}
iattr = bgp_attr_intern(&hattr);
bgp_attr_flush(&hattr);
/* Now iattr is an allocated interned attr */
/*
* Mode "plain" specific code
*
* Sets RD in dummy HD
* Allocates ecom
*/
{
if (vnaddr.addr_family != AF_INET) {
vnc_zlog_debug_verbose(
"%s: can't auto-assign RD, VN AF (%d) is not IPv4, skipping",
__func__, vnaddr.addr_family);
if (iattr) {
bgp_attr_unintern(&iattr);
}
return;
}
memset(&prd, 0, sizeof(prd));
rfapi_set_autord_from_vn(&prd, &vnaddr);
if (iattr && iattr->ecommunity)
ecom = ecommunity_dup(iattr->ecommunity);
}
local_pref = calc_local_pref(iattr, peer);
if (iattr && (iattr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))) {
med = &iattr->med;
}
if (VNC_DEBUG(IMPORT_BGP_ADD_ROUTE)) {
char buf[PREFIX_STRLEN];
rfapiRfapiIpAddr2Str(&vnaddr, buf, sizeof(buf));
vnc_zlog_debug_any("%s: setting vnaddr to %s", __func__, buf);
}
vncHDBgpDirect.peer = peer;
add_vnc_route(&vncHDBgpDirect, bgp, SAFI_MPLS_VPN, prefix, &prd,
&vnaddr, &local_pref, &(bgp->rfapi_cfg->redist_lifetime),
NULL, /* RFP options */
NULL, NULL, ecom, med, /* med */
NULL, /* label: default */
ZEBRA_ROUTE_BGP_DIRECT, BGP_ROUTE_REDISTRIBUTE,
ahr_flags);
vncHDBgpDirect.peer = NULL;
if (ecom)
ecommunity_free(&ecom);
}
static void
vnc_import_bgp_add_route_mode_nvegroup(struct bgp *bgp, struct prefix *prefix,
struct bgp_info *info,
struct rfapi_nve_group_cfg *rfg)
{
afi_t afi = family2afi(prefix->family);
struct peer *peer = info->peer;
struct attr *attr = info->attr;
struct attr hattr;
struct attr *iattr = NULL;
struct rfapi_ip_addr vnaddr;
struct prefix *vn_pfx = NULL;
int ahr_flags = 0;
struct ecommunity *ecom = NULL;
struct prefix_rd prd;
struct route_map *rmap = NULL;
uint32_t local_pref;
{
char buf[PREFIX_STRLEN];
prefix2str(prefix, buf, sizeof(buf));
vnc_zlog_debug_verbose("%s(prefix=%s) entry", __func__, buf);
}
assert(rfg);
if (!afi) {
flog_err(LIB_ERR_DEVELOPMENT, "%s: can't get afi of prefix",
__func__);
return;
}
if (!(bgp->rfapi_cfg)) {
vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
__func__);
return;
}
/* check vnc redist flag for bgp direct routes */
if (!bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT]) {
vnc_zlog_debug_verbose(
"%s: bgp->rfapi_cfg->redist[afi=%d][type=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
__func__, afi);
return;
}
/*
* RFG-specific code
*/
{
struct rfapi_ip_prefix pfx_un;
vnc_zlog_debug_verbose("%s: using redist RFG", __func__);
/*
* RFG prefix list check
*/
if (rfg->plist_redist[ZEBRA_ROUTE_BGP_DIRECT][afi]) {
vnc_zlog_debug_verbose(
"%s: RFG prefix list is set, checking",
__func__);
if (prefix_list_apply(
rfg->plist_redist[ZEBRA_ROUTE_BGP_DIRECT]
[afi],
prefix)
== PREFIX_DENY) {
vnc_zlog_debug_verbose(
"%s: prefix list returns DENY, blocking route",
__func__);
return;
}
vnc_zlog_debug_verbose(
"%s: prefix list returns PASS, allowing route",
__func__);
}
/* apply routemap, if any, later */
rmap = rfg->routemap_redist[ZEBRA_ROUTE_BGP_DIRECT];
/*
* export nve group's VN addr prefix must be a /32 which
* will yield the VN addr to use
*/
vn_pfx = &rfg->vn_prefix;
/*
* UN Address
*/
if (!is_host_prefix(&rfg->un_prefix)) {
/* NB prefixlen==0 means it has not been configured */
vnc_zlog_debug_verbose(
"%s: redist RFG UN pfx not host pfx (plen=%d), skipping",
__func__, rfg->un_prefix.prefixlen);
return;
}
rfapiQprefix2Rprefix(&rfg->un_prefix, &pfx_un);
vncHDBgpDirect.un_addr = pfx_un.prefix;
}
if (VNC_DEBUG(IMPORT_BGP_ADD_ROUTE)) {
char buf[PREFIX_STRLEN];
prefix2str(vn_pfx, buf, sizeof(buf));
vnc_zlog_debug_any("%s vn_pfx=%s", __func__, buf);
}
/*
* Compute VN address
*/
if (rfapiQprefix2Raddr(vn_pfx, &vnaddr)) {
vnc_zlog_debug_verbose("%s: redist VN invalid, skipping",
__func__);
return;