Coverage Report

Created: 2026-01-01 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/frr/bgpd/rfapi/vnc_import_bgp.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 *
4
 * Copyright 2009-2016, LabN Consulting, L.L.C.
5
 *
6
 */
7
8
/*
9
 * File:  vnc_import_bgp.c
10
 * Purpose: Import routes from BGP unicast directly (not via zebra)
11
 */
12
13
#include "lib/zebra.h"
14
#include "lib/prefix.h"
15
#include "lib/agg_table.h"
16
#include "lib/vty.h"
17
#include "lib/log.h"
18
#include "lib/memory.h"
19
#include "lib/linklist.h"
20
#include "lib/plist.h"
21
#include "lib/routemap.h"
22
#include "lib/lib_errors.h"
23
24
#include "bgpd/bgpd.h"
25
#include "bgpd/bgp_ecommunity.h"
26
#include "bgpd/bgp_attr.h"
27
#include "bgpd/bgp_route.h"
28
#include "bgpd/bgp_mplsvpn.h" /* for RD_TYPE_IP */
29
30
#include "bgpd/rfapi/vnc_export_bgp.h"
31
#include "bgpd/rfapi/bgp_rfapi_cfg.h"
32
#include "bgpd/rfapi/rfapi.h"
33
#include "bgpd/rfapi/rfapi_import.h"
34
#include "bgpd/rfapi/rfapi_private.h"
35
#include "bgpd/rfapi/rfapi_monitor.h"
36
#include "bgpd/rfapi/rfapi_vty.h"
37
#include "bgpd/rfapi/vnc_import_bgp.h"
38
#include "bgpd/rfapi/vnc_import_bgp_p.h"
39
#include "bgpd/rfapi/vnc_debug.h"
40
41
#define ENABLE_VNC_RHNCK
42
43
#define DEBUG_RHN_LIST  0
44
45
static struct rfapi_descriptor vncHDBgpDirect;  /* dummy nve descriptor */
46
static struct rfapi_descriptor vncHDResolveNve; /* dummy nve descriptor */
47
48
/*
49
 * For routes from another AS:
50
 *
51
 * If MED is set,
52
 *  LOCAL_PREF = 255 - MIN(255, MED)
53
 * else
54
 *  LOCAL_PREF = default_local_pref
55
 *
56
 * For routes from the same AS:
57
 *
58
 *  LOCAL_PREF unchanged
59
 */
60
uint32_t calc_local_pref(struct attr *attr, struct peer *peer)
61
0
{
62
0
  uint32_t local_pref = 0;
63
64
0
  if (!attr) {
65
0
    if (peer) {
66
0
      return peer->bgp->default_local_pref;
67
0
    }
68
0
    return bgp_get_default()->default_local_pref;
69
0
  }
70
71
0
  if (peer && (peer->as != peer->bgp->as)) {
72
0
    if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC)) {
73
0
      if (attr->med > 255) {
74
0
        local_pref = 0;
75
0
      } else {
76
0
        local_pref = 255 - attr->med;
77
0
      }
78
0
    } else {
79
0
      local_pref = peer->bgp->default_local_pref;
80
0
    }
81
0
  } else {
82
0
    if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF)) {
83
0
      local_pref = attr->local_pref;
84
0
    } else {
85
0
      if (peer && peer->bgp) {
86
0
        local_pref = peer->bgp->default_local_pref;
87
0
      }
88
0
    }
89
0
  }
90
91
0
  return local_pref;
92
0
}
93
94
static int is_host_prefix(const struct prefix *p)
95
0
{
96
0
  switch (p->family) {
97
0
  case AF_INET:
98
0
    return (p->prefixlen == IPV4_MAX_BITLEN);
99
0
  case AF_INET6:
100
0
    return (p->prefixlen == IPV6_MAX_BITLEN);
101
0
  }
102
0
  return 0;
103
0
}
104
105
/***********************************************************************
106
 *        RHN list
107
 ***********************************************************************/
108
109
struct prefix_bag {
110
  struct prefix hpfx;   /* ce address = unicast nexthop */
111
  struct prefix upfx;   /* unicast prefix */
112
  struct bgp_path_info *ubpi; /* unicast route */
113
};
114
115
static const uint8_t maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
116
          0xf8, 0xfc, 0xfe, 0xff};
117
118
int vnc_prefix_cmp(const void *pfx1, const void *pfx2)
119
0
{
120
0
  int offset;
121
0
  int shift;
122
0
  uint8_t mask;
123
124
0
  const struct prefix *p1 = pfx1;
125
0
  const struct prefix *p2 = pfx2;
126
127
0
  if (p1->family < p2->family)
128
0
    return -1;
129
0
  if (p1->family > p2->family)
130
0
    return 1;
131
132
0
  if (p1->prefixlen < p2->prefixlen)
133
0
    return -1;
134
0
  if (p1->prefixlen > p2->prefixlen)
135
0
    return 1;
136
137
0
  offset = p1->prefixlen / 8;
138
0
  shift = p1->prefixlen % 8;
139
0
  if (shift == 0 && offset) { /* catch aligned case */
140
0
    offset--;
141
0
    shift = 8;
142
0
  }
143
144
  /* Set both prefix's head pointer. */
145
0
  const uint8_t *pp1 = (const uint8_t *)&p1->u.prefix;
146
0
  const uint8_t *pp2 = (const uint8_t *)&p2->u.prefix;
147
148
0
  while (offset--) {
149
0
    if (*pp1 < *pp2)
150
0
      return -1;
151
0
    if (*pp1 > *pp2)
152
0
      return 1;
153
0
    ++pp1;
154
0
    ++pp2;
155
0
  }
156
157
0
  mask = maskbit[shift];
158
0
  if ((*pp1 & mask) < (*pp2 & mask))
159
0
    return -1;
160
0
  if ((*pp1 & mask) > (*pp2 & mask))
161
0
    return 1;
162
163
0
  return 0;
164
0
}
165
166
static void prefix_bag_free(void *pb)
167
0
{
168
0
  XFREE(MTYPE_RFAPI_PREFIX_BAG, pb);
169
0
}
170
171
#if DEBUG_RHN_LIST
172
static void print_rhn_list(const char *tag1, const char *tag2)
173
{
174
  struct bgp *bgp;
175
  struct skiplist *sl;
176
  struct skiplistnode *p;
177
  struct prefix_bag *pb;
178
  int count = 0;
179
180
  bgp = bgp_get_default();
181
  if (!bgp)
182
    return;
183
184
  sl = bgp->frapi->resolve_nve_nexthop;
185
  if (!sl) {
186
    vnc_zlog_debug_verbose("%s: %s: RHN List is empty",
187
               (tag1 ? tag1 : ""), (tag2 ? tag2 : ""));
188
    return;
189
  }
190
191
  vnc_zlog_debug_verbose("%s: %s: RHN list:", (tag1 ? tag1 : ""),
192
             (tag2 ? tag2 : ""));
193
194
  /* XXX uses secret knowledge of skiplist structure */
195
  for (p = sl->header->forward[0]; p; p = p->forward[0]) {
196
    pb = p->value;
197
198
    vnc_zlog_debug_verbose(
199
      "RHN Entry %d (q=%p): kpfx=%pFX, upfx=%pFX, hpfx=%pFX, ubpi=%p",
200
      ++count, p, p->key, &pb->upfx, &pb->hpfx, pb->ubpi);
201
  }
202
}
203
#endif
204
205
#ifdef ENABLE_VNC_RHNCK
206
static void vnc_rhnck(char *tag)
207
0
{
208
0
  struct bgp *bgp;
209
0
  struct skiplist *sl;
210
0
  struct skiplistnode *p;
211
212
0
  bgp = bgp_get_default();
213
0
  if (!bgp)
214
0
    return;
215
0
  sl = bgp->rfapi->resolve_nve_nexthop;
216
217
0
  if (!sl)
218
0
    return;
219
220
  /* XXX uses secret knowledge of skiplist structure */
221
0
  for (p = sl->header->forward[0]; p; p = p->forward[0]) {
222
0
    struct prefix_bag *pb;
223
0
    struct prefix *pkey;
224
0
    afi_t afi;
225
0
    struct prefix pfx_orig_nexthop;
226
227
0
    memset(&pfx_orig_nexthop, 0,
228
0
           sizeof(pfx_orig_nexthop)); /* keep valgrind happy */
229
230
0
    pkey = p->key;
231
0
    pb = p->value;
232
233
0
    afi = family2afi(pb->upfx.family);
234
235
0
    rfapiUnicastNexthop2Prefix(afi, pb->ubpi->attr,
236
0
             &pfx_orig_nexthop);
237
238
    /* pb->hpfx, pb->ubpi nexthop, pkey should all reflect the same
239
     * pfx */
240
0
    assert(!vnc_prefix_cmp(&pb->hpfx, pkey));
241
0
    if (vnc_prefix_cmp(&pb->hpfx, &pfx_orig_nexthop)) {
242
0
      vnc_zlog_debug_verbose(
243
0
        "%s: %s: FATAL: resolve_nve_nexthop list item bpi nexthop %pFX != nve pfx %pFX",
244
0
        __func__, tag, &pfx_orig_nexthop, &pb->hpfx);
245
0
      assert(0);
246
0
    }
247
0
  }
248
0
  vnc_zlog_debug_verbose("%s: vnc_rhnck OK", tag);
249
0
}
250
251
#define VNC_RHNCK(n)                                                           \
252
0
  do {                                                                   \
253
0
    char buf[BUFSIZ];                                              \
254
0
    snprintf(buf, sizeof(buf), "%s: %s", __func__, #n);            \
255
0
    vnc_rhnck(buf);                                                \
256
0
  } while (0)
257
258
#else
259
260
#define VNC_RHNCK(n)
261
262
#endif
263
264
/***********************************************************************
265
 *      Add/Delete Unicast Route
266
 ***********************************************************************/
267
268
/*
269
 * "Adding a Route" import process
270
 */
271
272
/*
273
 * extract and package information from the BGP unicast route.
274
 * Return code 0 means OK, non-0 means drop.
275
 *
276
 * If return code is 0, caller MUST release ecom
277
 */
278
static int process_unicast_route(struct bgp *bgp,    /* in */
279
         afi_t afi,      /* in */
280
         const struct prefix *prefix,  /* in */
281
         struct bgp_path_info *info,   /* in */
282
         struct ecommunity **ecom,   /* OUT */
283
         struct prefix *unicast_nexthop) /* OUT */
284
0
{
285
0
  struct rfapi_cfg *hc = bgp->rfapi_cfg;
286
0
  struct peer *peer = info->peer;
287
0
  struct attr *attr = info->attr;
288
0
  struct attr hattr;
289
0
  struct route_map *rmap = NULL;
290
0
  struct prefix pfx_orig_nexthop;
291
292
0
  memset(&pfx_orig_nexthop, 0,
293
0
         sizeof(pfx_orig_nexthop)); /* keep valgrind happy */
294
295
  /*
296
   * prefix list check
297
   */
298
0
  if (hc->plist_redist[ZEBRA_ROUTE_BGP_DIRECT][afi]) {
299
0
    vnc_zlog_debug_verbose("%s: HC prefix list is set, checking",
300
0
               __func__);
301
0
    if (prefix_list_apply(
302
0
          hc->plist_redist[ZEBRA_ROUTE_BGP_DIRECT][afi],
303
0
          prefix)
304
0
        == PREFIX_DENY) {
305
0
      vnc_zlog_debug_verbose(
306
0
        "%s: prefix list returns DENY, blocking route",
307
0
        __func__);
308
0
      return -1;
309
0
    }
310
0
    vnc_zlog_debug_verbose(
311
0
      "%s: prefix list returns PASS, allowing route",
312
0
      __func__);
313
0
  }
314
315
  /* apply routemap, if any, later */
316
0
  rmap = hc->routemap_redist[ZEBRA_ROUTE_BGP_DIRECT];
317
318
  /*
319
   * Extract original nexthop, which we expect to be a NVE connected
320
   * router
321
   * Note that this is the nexthop before any possible application of
322
   * policy
323
   */
324
  /*
325
   * Incoming prefix is unicast. If v6, it is in multiprotocol area,
326
   * but if v4 it is in attr->nexthop
327
   */
328
0
  rfapiUnicastNexthop2Prefix(afi, attr, &pfx_orig_nexthop);
329
330
  /*
331
   * route map handling
332
   * This code is here because it allocates an interned attr which
333
   * must be freed before we return. It's easier to put it after
334
   * all of the possible returns above.
335
   */
336
0
  memset(&hattr, 0, sizeof(hattr));
337
  /* hattr becomes a ghost attr */
338
0
  hattr = *attr;
339
340
0
  if (rmap) {
341
0
    struct bgp_path_info info;
342
0
    route_map_result_t ret;
343
344
0
    memset(&info, 0, sizeof(info));
345
0
    info.peer = peer;
346
0
    info.attr = &hattr;
347
0
    ret = route_map_apply(rmap, prefix, &info);
348
0
    if (ret == RMAP_DENYMATCH) {
349
0
      bgp_attr_flush(&hattr);
350
0
      vnc_zlog_debug_verbose(
351
0
        "%s: route map \"%s\" says DENY, returning",
352
0
        __func__, rmap->name);
353
0
      return -1;
354
0
    }
355
0
  }
356
357
  /*
358
   * Get the (possibly altered by policy) unicast nexthop
359
   * for later lookup in the Import Table by caller
360
   */
361
0
  rfapiUnicastNexthop2Prefix(afi, &hattr, unicast_nexthop);
362
363
0
  if (bgp_attr_get_ecommunity(&hattr))
364
0
    *ecom = ecommunity_dup(bgp_attr_get_ecommunity(&hattr));
365
0
  else
366
0
    *ecom = ecommunity_new();
367
368
  /*
369
   * Done with hattr, clean up
370
   */
371
0
  bgp_attr_flush(&hattr);
372
373
  /*
374
   * Add EC that carries original NH of iBGP route (2 bytes = magic
375
   * value indicating it came from an VNC gateway; default 5226, but
376
   * must be user configurable). Note that this is the nexthop before
377
   * any application of policy.
378
   */
379
0
  {
380
0
    struct ecommunity_val vnc_gateway_magic;
381
0
    uint16_t localadmin;
382
383
    /* Using route origin extended community type */
384
0
    memset(&vnc_gateway_magic, 0, sizeof(vnc_gateway_magic));
385
0
    vnc_gateway_magic.val[0] = 0x01;
386
0
    vnc_gateway_magic.val[1] = 0x03;
387
388
    /* Only works for IPv4 nexthops */
389
0
    if (prefix->family == AF_INET) {
390
0
      memcpy(vnc_gateway_magic.val + 2,
391
0
             &unicast_nexthop->u.prefix4, 4);
392
0
    }
393
0
    localadmin = htons(hc->resolve_nve_roo_local_admin);
394
0
    memcpy(vnc_gateway_magic.val + 6, (char *)&localadmin, 2);
395
396
0
    ecommunity_add_val(*ecom, &vnc_gateway_magic, false, false);
397
0
  }
398
399
0
  return 0;
400
0
}
401
402
403
static void vnc_import_bgp_add_route_mode_resolve_nve_one_bi(
404
  struct bgp *bgp, afi_t afi, struct bgp_path_info *bpi, /* VPN bpi */
405
  struct prefix_rd *prd,               /* RD */
406
  const struct prefix *prefix, /* unicast route prefix */
407
  uint32_t *local_pref,      /* NULL = no local_pref */
408
  uint32_t *med,         /* NULL = no med */
409
  struct ecommunity *ecom)     /* generated ecoms */
410
0
{
411
0
  struct prefix un;
412
0
  struct prefix nexthop;
413
0
  struct rfapi_ip_addr nexthop_h;
414
0
  uint32_t lifetime;
415
0
  uint32_t *plifetime;
416
0
  struct bgp_attr_encap_subtlv *encaptlvs;
417
0
  uint32_t label = 0;
418
419
0
  struct rfapi_un_option optary[3];
420
0
  struct rfapi_un_option *opt = NULL;
421
0
  int cur_opt = 0;
422
423
0
  vnc_zlog_debug_verbose("%s: entry", __func__);
424
425
0
  if (bpi->type != ZEBRA_ROUTE_BGP
426
0
      && bpi->type != ZEBRA_ROUTE_BGP_DIRECT) {
427
428
0
    return;
429
0
  }
430
0
  if (bpi->sub_type != BGP_ROUTE_NORMAL
431
0
      && bpi->sub_type != BGP_ROUTE_STATIC
432
0
      && bpi->sub_type != BGP_ROUTE_RFP) {
433
434
0
    return;
435
0
  }
436
0
  if (CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED))
437
0
    return;
438
439
0
  vncHDResolveNve.peer = bpi->peer;
440
0
  if (!rfapiGetVncTunnelUnAddr(bpi->attr, &un)) {
441
0
    if (rfapiQprefix2Raddr(&un, &vncHDResolveNve.un_addr))
442
0
      return;
443
0
  } else {
444
0
    memset(&vncHDResolveNve.un_addr, 0,
445
0
           sizeof(vncHDResolveNve.un_addr));
446
0
  }
447
448
  /* Use nexthop of VPN route as nexthop of constructed route */
449
0
  rfapiNexthop2Prefix(bpi->attr, &nexthop);
450
0
  rfapiQprefix2Raddr(&nexthop, &nexthop_h);
451
452
0
  if (rfapiGetVncLifetime(bpi->attr, &lifetime)) {
453
0
    plifetime = NULL;
454
0
  } else {
455
0
    plifetime = &lifetime;
456
0
  }
457
458
0
  encaptlvs = bgp_attr_get_vnc_subtlvs(bpi->attr);
459
0
  if (bpi->attr->encap_tunneltype != BGP_ENCAP_TYPE_RESERVED
460
0
      && bpi->attr->encap_tunneltype != BGP_ENCAP_TYPE_MPLS) {
461
0
    opt = &optary[cur_opt++];
462
0
    memset(opt, 0, sizeof(struct rfapi_un_option));
463
0
    opt->type = RFAPI_UN_OPTION_TYPE_TUNNELTYPE;
464
0
    opt->v.tunnel.type = bpi->attr->encap_tunneltype;
465
    /* TBD parse bpi->attr->extra->encap_subtlvs */
466
0
  }
467
468
0
  struct ecommunity *new_ecom = ecommunity_dup(ecom);
469
470
0
  if (bgp_attr_get_ecommunity(bpi->attr))
471
0
    ecommunity_merge(new_ecom, bgp_attr_get_ecommunity(bpi->attr));
472
473
0
  if (bpi->extra)
474
0
    label = decode_label(&bpi->extra->label[0]);
475
476
0
  add_vnc_route(&vncHDResolveNve, bgp, SAFI_MPLS_VPN,
477
0
          prefix,   /* unicast route prefix */
478
0
          prd, &nexthop_h, /* new nexthop */
479
0
          local_pref, plifetime,
480
0
          (struct bgp_tea_options *)encaptlvs, /* RFP options */
481
0
          opt, NULL, new_ecom, med, /* NULL => don't set med */
482
0
          (label ? &label : NULL),  /* NULL= default */
483
0
          ZEBRA_ROUTE_BGP_DIRECT, BGP_ROUTE_REDISTRIBUTE,
484
0
          RFAPI_AHR_RFPOPT_IS_VNCTLV); /* flags */
485
486
0
  ecommunity_free(&new_ecom);
487
0
}
488
489
static void vnc_import_bgp_add_route_mode_resolve_nve_one_rd(
490
  struct prefix_rd *prd,      /* RD */
491
  struct bgp_table *table_rd, /* per-rd VPN route table */
492
  afi_t afi, struct bgp *bgp,
493
  const struct prefix *prefix, /* unicast prefix */
494
  struct ecommunity *ecom,     /* generated ecoms */
495
  uint32_t *local_pref,      /* NULL = no local_pref */
496
  uint32_t *med,         /* NULL = no med */
497
  struct prefix *ubpi_nexthop) /* unicast nexthop */
498
0
{
499
0
  struct bgp_dest *bd;
500
0
  struct bgp_path_info *bpi;
501
502
0
  if (!table_rd)
503
0
    return;
504
505
0
  vnc_zlog_debug_verbose("%s: ubpi_nexthop=%pFX", __func__, ubpi_nexthop);
506
507
  /* exact match */
508
0
  bd = bgp_node_lookup(table_rd, ubpi_nexthop);
509
0
  if (!bd) {
510
0
    vnc_zlog_debug_verbose(
511
0
      "%s: no match in RD's table for ubpi_nexthop",
512
0
      __func__);
513
0
    return;
514
0
  }
515
516
  /* Iterate over bgp_info items at this node */
517
0
  for (bpi = bgp_dest_get_bgp_path_info(bd); bpi; bpi = bpi->next) {
518
519
0
    vnc_import_bgp_add_route_mode_resolve_nve_one_bi(
520
0
      bgp, afi, bpi, /* VPN bpi */
521
0
      prd, prefix, local_pref, med, ecom);
522
0
  }
523
524
0
  bgp_dest_unlock_node(bd);
525
0
}
526
527
static void vnc_import_bgp_add_route_mode_resolve_nve(
528
  struct bgp *bgp, const struct prefix *prefix, /* unicast prefix */
529
  struct bgp_path_info *info)         /* unicast info */
530
0
{
531
0
  afi_t afi = family2afi(prefix->family);
532
533
0
  struct prefix pfx_unicast_nexthop = {0}; /* happy valgrind */
534
535
0
  struct ecommunity *ecom = NULL;
536
0
  uint32_t local_pref;
537
0
  uint32_t *med = NULL;
538
539
0
  struct prefix_bag *pb;
540
0
  struct bgp_dest *bdp; /* prd table node */
541
542
  /*debugging */
543
0
  if (VNC_DEBUG(VERBOSE)) {
544
0
    char str_nh[PREFIX_STRLEN];
545
0
    struct prefix nh;
546
547
0
    nh.prefixlen = 0;
548
0
    rfapiUnicastNexthop2Prefix(afi, info->attr, &nh);
549
0
    if (nh.prefixlen) {
550
0
      prefix2str(&nh, str_nh, sizeof(str_nh));
551
0
    } else {
552
0
      str_nh[0] = '?';
553
0
      str_nh[1] = 0;
554
0
    }
555
556
0
    vnc_zlog_debug_verbose(
557
0
      "%s(bgp=%p, unicast prefix=%pFX, unicast nh=%s)",
558
0
      __func__, bgp, prefix, str_nh);
559
0
  }
560
561
0
  if (info->type != ZEBRA_ROUTE_BGP) {
562
0
    vnc_zlog_debug_verbose(
563
0
      "%s: unicast type %d=\"%s\" is not %d=%s, skipping",
564
0
      __func__, info->type, zebra_route_string(info->type),
565
0
      ZEBRA_ROUTE_BGP, "ZEBRA_ROUTE_BGP");
566
0
    return;
567
0
  }
568
569
  /*
570
   * Preliminary checks
571
   */
572
573
0
  if (!afi) {
574
0
    flog_err(EC_LIB_DEVELOPMENT, "%s: can't get afi of prefix",
575
0
       __func__);
576
0
    return;
577
0
  }
578
579
0
  if (!(bgp->rfapi_cfg)) {
580
0
    vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
581
0
               __func__);
582
0
    return;
583
0
  }
584
585
  /* check vnc redist flag for bgp direct routes */
586
0
  if (!bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT]) {
587
0
    vnc_zlog_debug_verbose(
588
0
      "%s: bgp->rfapi_cfg->redist[afi=%d][type=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
589
0
      __func__, afi);
590
0
    return;
591
0
  }
592
593
594
0
  if (process_unicast_route(bgp, afi, prefix, info, &ecom,
595
0
          &pfx_unicast_nexthop)) {
596
597
0
    vnc_zlog_debug_verbose(
598
0
      "%s: process_unicast_route error, skipping", __func__);
599
0
    return;
600
0
  }
601
602
0
  local_pref = calc_local_pref(info->attr, info->peer);
603
0
  if (info->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
604
0
    med = &info->attr->med;
605
606
  /*
607
   * At this point, we have allocated:
608
   *
609
   *  ecom    ecommunity ptr, union of unicast and ROO parts (no NVE part)
610
   *
611
   * And we have set:
612
   *
613
   *  pfx_unicast_nexthop     nexthop of uncast route
614
   */
615
616
0
  if (!bgp->rfapi->resolve_nve_nexthop) {
617
0
    bgp->rfapi->resolve_nve_nexthop =
618
0
      skiplist_new(SKIPLIST_FLAG_ALLOW_DUPLICATES,
619
0
             vnc_prefix_cmp, prefix_bag_free);
620
0
  }
621
622
0
  pb = XCALLOC(MTYPE_RFAPI_PREFIX_BAG, sizeof(struct prefix_bag));
623
0
  pb->hpfx = pfx_unicast_nexthop;
624
0
  pb->ubpi = info;
625
0
  pb->upfx = *prefix;
626
627
0
  bgp_path_info_lock(info); /* skiplist refers to it */
628
0
  skiplist_insert(bgp->rfapi->resolve_nve_nexthop, &pb->hpfx, pb);
629
630
  /*
631
   * Iterate over RDs in VPN RIB. For each RD, look up unicast nexthop
632
   * (exact match, /32). If an exact match is found, call add_vnc_route.
633
   */
634
635
0
  for (bdp = bgp_table_top(bgp->rib[afi][SAFI_MPLS_VPN]); bdp;
636
0
       bdp = bgp_route_next(bdp)) {
637
638
0
    struct bgp_table *table;
639
640
0
    table = bgp_dest_get_bgp_table_info(bdp);
641
642
0
    if (!table)
643
0
      continue;
644
645
0
    vnc_import_bgp_add_route_mode_resolve_nve_one_rd(
646
0
      (struct prefix_rd *)bgp_dest_get_prefix(bdp), table,
647
0
      afi, bgp, prefix, ecom, &local_pref, med,
648
0
      &pfx_unicast_nexthop);
649
0
  }
650
651
652
0
  if (ecom)
653
0
    ecommunity_free(&ecom);
654
655
0
  vnc_zlog_debug_verbose("%s: done", __func__);
656
0
}
657
658
659
static void vnc_import_bgp_add_route_mode_plain(struct bgp *bgp,
660
            const struct prefix *prefix,
661
            struct bgp_path_info *info)
662
0
{
663
0
  afi_t afi = family2afi(prefix->family);
664
0
  struct peer *peer = info->peer;
665
0
  struct attr *attr = info->attr;
666
0
  struct attr hattr;
667
0
  struct rfapi_cfg *hc = bgp->rfapi_cfg;
668
0
  struct attr *iattr = NULL;
669
670
0
  struct rfapi_ip_addr vnaddr;
671
0
  struct prefix vn_pfx_space;
672
0
  struct prefix *vn_pfx = NULL;
673
0
  int ahr_flags = 0;
674
0
  struct ecommunity *ecom = NULL;
675
0
  struct prefix_rd prd;
676
0
  struct route_map *rmap = NULL;
677
0
  uint32_t local_pref;
678
0
  uint32_t *med = NULL;
679
680
0
  vnc_zlog_debug_verbose("%s(prefix=%pFX) entry", __func__, prefix);
681
682
0
  if (!afi) {
683
0
    flog_err(EC_LIB_DEVELOPMENT, "%s: can't get afi of prefix",
684
0
       __func__);
685
0
    return;
686
0
  }
687
688
0
  if (!hc) {
689
0
    vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
690
0
               __func__);
691
0
    return;
692
0
  }
693
694
  /* check vnc redist flag for bgp direct routes */
695
0
  if (!bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT]) {
696
0
    vnc_zlog_debug_verbose(
697
0
      "%s: bgp->rfapi_cfg->redist[afi=%d][type=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
698
0
      __func__, afi);
699
0
    return;
700
0
  }
701
702
  /*
703
   * mode "plain" specific code
704
   */
705
0
  {
706
0
    vnc_zlog_debug_verbose("%s: NOT using redist RFG", __func__);
707
708
    /*
709
     * prefix list check
710
     */
711
0
    if (hc->plist_redist[ZEBRA_ROUTE_BGP_DIRECT][afi]) {
712
0
      vnc_zlog_debug_verbose(
713
0
        "%s: HC prefix list is set, checking",
714
0
        __func__);
715
0
      if (prefix_list_apply(
716
0
            hc->plist_redist[ZEBRA_ROUTE_BGP_DIRECT]
717
0
                [afi],
718
0
            prefix)
719
0
          == PREFIX_DENY) {
720
0
        vnc_zlog_debug_verbose(
721
0
          "%s: prefix list returns DENY, blocking route",
722
0
          __func__);
723
0
        return;
724
0
      }
725
0
      vnc_zlog_debug_verbose(
726
0
        "%s: prefix list returns PASS, allowing route",
727
0
        __func__);
728
0
    }
729
730
    /* apply routemap, if any, later */
731
0
    rmap = hc->routemap_redist[ZEBRA_ROUTE_BGP_DIRECT];
732
733
    /*
734
     * Incoming prefix is unicast. If v6, it is in multiprotocol
735
     * area,
736
     * but if v4 it is in attr->nexthop
737
     */
738
0
    rfapiUnicastNexthop2Prefix(afi, attr, &vn_pfx_space);
739
0
    vn_pfx = &vn_pfx_space;
740
741
    /* UN address */
742
0
    ahr_flags |= RFAPI_AHR_NO_TUNNEL_SUBTLV;
743
0
  }
744
745
0
  if (VNC_DEBUG(IMPORT_BGP_ADD_ROUTE))
746
0
    vnc_zlog_debug_any("%s vn_pfx=%pFX", __func__, vn_pfx);
747
748
  /*
749
   * Compute VN address
750
   */
751
0
  if (rfapiQprefix2Raddr(vn_pfx, &vnaddr)) {
752
0
    vnc_zlog_debug_verbose("%s: redist VN invalid, skipping",
753
0
               __func__);
754
0
    return;
755
0
  }
756
757
  /*
758
   * route map handling
759
   * This code is here because it allocates an interned attr which
760
   * must be freed before we return. It's easier to put it after
761
   * all of the possible returns above.
762
   */
763
0
  memset(&hattr, 0, sizeof(hattr));
764
  /* hattr becomes a ghost attr */
765
0
  hattr = *attr;
766
767
0
  if (rmap) {
768
0
    struct bgp_path_info info;
769
0
    route_map_result_t ret;
770
771
0
    memset(&info, 0, sizeof(info));
772
0
    info.peer = peer;
773
0
    info.attr = &hattr;
774
0
    ret = route_map_apply(rmap, prefix, &info);
775
0
    if (ret == RMAP_DENYMATCH) {
776
0
      bgp_attr_flush(&hattr);
777
0
      vnc_zlog_debug_verbose(
778
0
        "%s: route map \"%s\" says DENY, returning",
779
0
        __func__, rmap->name);
780
0
      return;
781
0
    }
782
0
  }
783
784
0
  iattr = bgp_attr_intern(&hattr);
785
0
  bgp_attr_flush(&hattr);
786
787
  /* Now iattr is an allocated interned attr */
788
789
  /*
790
   * Mode "plain" specific code
791
   *
792
   * Sets RD in dummy HD
793
   * Allocates ecom
794
   */
795
0
  {
796
0
    if (vnaddr.addr_family != AF_INET) {
797
0
      vnc_zlog_debug_verbose(
798
0
        "%s: can't auto-assign RD, VN AF (%d) is not IPv4, skipping",
799
0
        __func__, vnaddr.addr_family);
800
0
      if (iattr) {
801
0
        bgp_attr_unintern(&iattr);
802
0
      }
803
0
      return;
804
0
    }
805
0
    memset(&prd, 0, sizeof(prd));
806
0
    rfapi_set_autord_from_vn(&prd, &vnaddr);
807
808
0
    if (iattr && bgp_attr_get_ecommunity(iattr))
809
0
      ecom = ecommunity_dup(bgp_attr_get_ecommunity(iattr));
810
0
  }
811
812
0
  local_pref = calc_local_pref(iattr, peer);
813
814
0
  if (iattr && (iattr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))) {
815
0
    med = &iattr->med;
816
0
  }
817
818
0
  if (VNC_DEBUG(IMPORT_BGP_ADD_ROUTE)) {
819
0
    char buf[PREFIX_STRLEN];
820
821
0
    rfapiRfapiIpAddr2Str(&vnaddr, buf, sizeof(buf));
822
0
    vnc_zlog_debug_any("%s: setting vnaddr to %s", __func__, buf);
823
0
  }
824
825
0
  vncHDBgpDirect.peer = peer;
826
0
  add_vnc_route(&vncHDBgpDirect, bgp, SAFI_MPLS_VPN, prefix, &prd,
827
0
          &vnaddr, &local_pref, &(bgp->rfapi_cfg->redist_lifetime),
828
0
          NULL,        /* RFP options */
829
0
          NULL, NULL, ecom, med, /* med */
830
0
          NULL,        /* label: default */
831
0
          ZEBRA_ROUTE_BGP_DIRECT, BGP_ROUTE_REDISTRIBUTE,
832
0
          ahr_flags);
833
0
  vncHDBgpDirect.peer = NULL;
834
835
0
  if (ecom)
836
0
    ecommunity_free(&ecom);
837
0
  if (iattr)
838
0
    bgp_attr_unintern(&iattr);
839
0
}
840
841
static void vnc_import_bgp_add_route_mode_nvegroup(
842
  struct bgp *bgp, const struct prefix *prefix,
843
  struct bgp_path_info *info, struct rfapi_nve_group_cfg *rfg)
844
0
{
845
0
  afi_t afi = family2afi(prefix->family);
846
0
  struct peer *peer = info->peer;
847
0
  struct attr *attr = info->attr;
848
0
  struct attr hattr;
849
0
  struct attr *iattr = NULL;
850
851
0
  struct rfapi_ip_addr vnaddr;
852
0
  struct prefix *vn_pfx = NULL;
853
0
  int ahr_flags = 0;
854
0
  struct ecommunity *ecom = NULL;
855
0
  struct prefix_rd prd;
856
0
  struct route_map *rmap = NULL;
857
0
  uint32_t local_pref;
858
859
0
  vnc_zlog_debug_verbose("%s(prefix=%pFX) entry", __func__, prefix);
860
861
0
  assert(rfg);
862
863
0
  if (!afi) {
864
0
    flog_err(EC_LIB_DEVELOPMENT, "%s: can't get afi of prefix",
865
0
       __func__);
866
0
    return;
867
0
  }
868
869
0
  if (!(bgp->rfapi_cfg)) {
870
0
    vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
871
0
               __func__);
872
0
    return;
873
0
  }
874
875
  /* check vnc redist flag for bgp direct routes */
876
0
  if (!bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT]) {
877
0
    vnc_zlog_debug_verbose(
878
0
      "%s: bgp->rfapi_cfg->redist[afi=%d][type=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
879
0
      __func__, afi);
880
0
    return;
881
0
  }
882
883
884
  /*
885
   * RFG-specific code
886
   */
887
0
  {
888
889
0
    struct rfapi_ip_prefix pfx_un;
890
891
0
    vnc_zlog_debug_verbose("%s: using redist RFG", __func__);
892
893
    /*
894
     * RFG prefix list check
895
     */
896
0
    if (rfg->plist_redist[ZEBRA_ROUTE_BGP_DIRECT][afi]) {
897
0
      vnc_zlog_debug_verbose(
898
0
        "%s: RFG prefix list is set, checking",
899
0
        __func__);
900
0
      if (prefix_list_apply(
901
0
            rfg->plist_redist[ZEBRA_ROUTE_BGP_DIRECT]
902
0
                 [afi],
903
0
            prefix)
904
0
          == PREFIX_DENY) {
905
0
        vnc_zlog_debug_verbose(
906
0
          "%s: prefix list returns DENY, blocking route",
907
0
          __func__);
908
0
        return;
909
0
      }
910
0
      vnc_zlog_debug_verbose(
911
0
        "%s: prefix list returns PASS, allowing route",
912
0
        __func__);
913
0
    }
914
915
    /* apply routemap, if any, later */
916
0
    rmap = rfg->routemap_redist[ZEBRA_ROUTE_BGP_DIRECT];
917
918
    /*
919
     * export nve group's VN addr prefix must be a /32 which
920
     * will yield the VN addr to use
921
     */
922
0
    vn_pfx = &rfg->vn_prefix;
923
924
    /*
925
     * UN Address
926
     */
927
0
    if (!is_host_prefix(&rfg->un_prefix)) {
928
      /* NB prefixlen==0 means it has not been configured */
929
0
      vnc_zlog_debug_verbose(
930
0
        "%s: redist RFG UN pfx not host pfx (plen=%d), skipping",
931
0
        __func__, rfg->un_prefix.prefixlen);
932
0
      return;
933
0
    }
934
935
0
    rfapiQprefix2Rprefix(&rfg->un_prefix, &pfx_un);
936
937
0
    vncHDBgpDirect.un_addr = pfx_un.prefix;
938
0
  }
939
940
0
  if (VNC_DEBUG(IMPORT_BGP_ADD_ROUTE))
941
0
    vnc_zlog_debug_any("%s vn_pfx=%pFX", __func__, vn_pfx);
942
943
  /*
944
   * Compute VN address
945
   */
946
0
  if (rfapiQprefix2Raddr(vn_pfx, &vnaddr)) {
947
0
    vnc_zlog_debug_verbose("%s: redist VN invalid, skipping",
948
0
               __func__);
949
0
    return;
950
0
  }
951
952
  /*
953
   * route map handling
954
   * This code is here because it allocates an interned attr which
955
   * must be freed before we return. It's easier to put it after
956
   * all of the possible returns above.
957
   */
958
0
  memset(&hattr, 0, sizeof(hattr));
959
  /* hattr becomes a ghost attr */
960
0
  hattr = *attr;
961
962
0
  if (rmap) {
963
0
    struct bgp_path_info path;
964
0
    route_map_result_t ret;
965
966
0
    memset(&path, 0, sizeof(path));
967
0
    path.peer = peer;
968
0
    path.attr = &hattr;
969
0
    ret = route_map_apply(rmap, prefix, &path);
970
0
    if (ret == RMAP_DENYMATCH) {
971
0
      bgp_attr_flush(&hattr);
972
0
      vnc_zlog_debug_verbose(
973
0
        "%s: route map \"%s\" says DENY, returning",
974
0
        __func__, rmap->name);
975
0
      return;
976
0
    }
977
0
  }
978
979
0
  iattr = bgp_attr_intern(&hattr);
980
0
  bgp_attr_flush(&hattr);
981
982
  /* Now iattr is an allocated interned attr */
983
984
  /*
985
   * RFG-specific code
986
   *
987
   * Sets RD in dummy HD
988
   * Allocates ecom
989
   */
990
0
  {
991
992
0
    memset(&prd, 0, sizeof(prd));
993
0
    prd = rfg->rd;
994
0
    prd.family = AF_UNSPEC;
995
0
    prd.prefixlen = 64;
996
997
0
    if (rfg->rd.family == AF_UNIX) {
998
0
      rfapi_set_autord_from_vn(&prd, &vnaddr);
999
0
    }
1000
1001
0
    if (rfg->rt_export_list)
1002
0
      ecom = ecommunity_dup(
1003
0
        bgp->rfapi_cfg->rfg_redist->rt_export_list);
1004
0
    else
1005
0
      ecom = ecommunity_new();
1006
1007
0
    if (iattr && bgp_attr_get_ecommunity(iattr))
1008
0
      ecom = ecommunity_merge(ecom,
1009
0
            bgp_attr_get_ecommunity(iattr));
1010
0
  }
1011
1012
0
  local_pref = calc_local_pref(iattr, peer);
1013
1014
0
  if (VNC_DEBUG(IMPORT_BGP_ADD_ROUTE)) {
1015
0
    char buf[BUFSIZ];
1016
1017
0
    buf[0] = 0;
1018
0
    rfapiRfapiIpAddr2Str(&vnaddr, buf, BUFSIZ);
1019
0
    buf[BUFSIZ - 1] = 0;
1020
0
    vnc_zlog_debug_any("%s: setting vnaddr to %s", __func__, buf);
1021
0
  }
1022
1023
0
  vncHDBgpDirect.peer = peer;
1024
0
  add_vnc_route(&vncHDBgpDirect, bgp, SAFI_MPLS_VPN, prefix, &prd,
1025
0
          &vnaddr, &local_pref, &(bgp->rfapi_cfg->redist_lifetime),
1026
0
          NULL,         /* RFP options */
1027
0
          NULL, NULL, ecom, NULL, /* med */
1028
0
          NULL,         /* label: default */
1029
0
          ZEBRA_ROUTE_BGP_DIRECT, BGP_ROUTE_REDISTRIBUTE,
1030
0
          ahr_flags);
1031
0
  vncHDBgpDirect.peer = NULL;
1032
1033
0
  if (ecom)
1034
0
    ecommunity_free(&ecom);
1035
0
  if (iattr)
1036
0
    bgp_attr_unintern(&iattr);
1037
0
}
1038
1039
static void vnc_import_bgp_del_route_mode_plain(struct bgp *bgp,
1040
            const struct prefix *prefix,
1041
            struct bgp_path_info *info)
1042
0
{
1043
0
  struct prefix_rd prd;
1044
0
  afi_t afi = family2afi(prefix->family);
1045
0
  struct prefix *vn_pfx = NULL;
1046
0
  struct rfapi_ip_addr vnaddr;
1047
0
  struct prefix vn_pfx_space;
1048
1049
1050
0
  assert(afi);
1051
1052
  /*
1053
   * Compute VN address
1054
   */
1055
1056
0
  if (info) {
1057
0
    rfapiUnicastNexthop2Prefix(afi, info->attr, &vn_pfx_space);
1058
0
  } else {
1059
0
    vnc_zlog_debug_verbose("%s: no attr, can't delete route",
1060
0
               __func__);
1061
0
    return;
1062
0
  }
1063
0
  vn_pfx = &vn_pfx_space;
1064
1065
0
  vnaddr.addr_family = vn_pfx->family;
1066
0
  switch (vn_pfx->family) {
1067
0
  case AF_INET:
1068
0
    if (vn_pfx->prefixlen != IPV4_MAX_BITLEN) {
1069
0
      vnc_zlog_debug_verbose(
1070
0
        "%s: redist VN plen (%d) != 32, skipping",
1071
0
        __func__, vn_pfx->prefixlen);
1072
0
      return;
1073
0
    }
1074
0
    vnaddr.addr.v4 = vn_pfx->u.prefix4;
1075
0
    break;
1076
1077
0
  case AF_INET6:
1078
0
    if (vn_pfx->prefixlen != IPV6_MAX_BITLEN) {
1079
0
      vnc_zlog_debug_verbose(
1080
0
        "%s: redist VN plen (%d) != 128, skipping",
1081
0
        __func__, vn_pfx->prefixlen);
1082
0
      return;
1083
0
    }
1084
0
    vnaddr.addr.v6 = vn_pfx->u.prefix6;
1085
0
    break;
1086
1087
0
  default:
1088
0
    vnc_zlog_debug_verbose(
1089
0
      "%s: no redist RFG VN host pfx configured, skipping",
1090
0
      __func__);
1091
0
    return;
1092
0
  }
1093
1094
1095
0
  memset(&prd, 0, sizeof(prd));
1096
0
  if (rfapi_set_autord_from_vn(&prd, &vnaddr)) {
1097
0
    vnc_zlog_debug_verbose("%s: can't auto-assign RD, skipping",
1098
0
               __func__);
1099
0
    return;
1100
0
  }
1101
1102
0
  vncHDBgpDirect.peer = info->peer;
1103
0
  vnc_zlog_debug_verbose("%s: setting peer to %p", __func__,
1104
0
             vncHDBgpDirect.peer);
1105
0
  del_vnc_route(&vncHDBgpDirect, info->peer, bgp, SAFI_MPLS_VPN, prefix,
1106
0
          &prd, ZEBRA_ROUTE_BGP_DIRECT, BGP_ROUTE_REDISTRIBUTE,
1107
0
          NULL, 1);
1108
1109
0
  vncHDBgpDirect.peer = NULL;
1110
0
}
1111
1112
static void vnc_import_bgp_del_route_mode_nvegroup(struct bgp *bgp,
1113
               const struct prefix *prefix,
1114
               struct bgp_path_info *info)
1115
0
{
1116
0
  struct prefix_rd prd;
1117
0
  afi_t afi = family2afi(prefix->family);
1118
0
  struct rfapi_nve_group_cfg *rfg = NULL;
1119
0
  struct prefix *vn_pfx = NULL;
1120
0
  struct rfapi_ip_addr vnaddr;
1121
1122
1123
0
  assert(afi);
1124
1125
0
  rfg = bgp->rfapi_cfg->rfg_redist;
1126
0
  assert(rfg);
1127
1128
  /*
1129
   * Compute VN address
1130
   */
1131
1132
  /*
1133
   * export nve group's VN addr prefix must be a /32 which
1134
   * will yield the VN addr to use
1135
   */
1136
0
  vn_pfx = &rfg->vn_prefix;
1137
1138
1139
0
  vnaddr.addr_family = vn_pfx->family;
1140
0
  switch (vn_pfx->family) {
1141
0
  case AF_INET:
1142
0
    if (vn_pfx->prefixlen != IPV4_MAX_BITLEN) {
1143
0
      vnc_zlog_debug_verbose(
1144
0
        "%s: redist VN plen (%d) != 32, skipping",
1145
0
        __func__, vn_pfx->prefixlen);
1146
0
      return;
1147
0
    }
1148
0
    vnaddr.addr.v4 = vn_pfx->u.prefix4;
1149
0
    break;
1150
1151
0
  case AF_INET6:
1152
0
    if (vn_pfx->prefixlen != IPV6_MAX_BITLEN) {
1153
0
      vnc_zlog_debug_verbose(
1154
0
        "%s: redist VN plen (%d) != 128, skipping",
1155
0
        __func__, vn_pfx->prefixlen);
1156
0
      return;
1157
0
    }
1158
0
    vnaddr.addr.v6 = vn_pfx->u.prefix6;
1159
0
    break;
1160
1161
0
  default:
1162
0
    vnc_zlog_debug_verbose(
1163
0
      "%s: no redist RFG VN host pfx configured, skipping",
1164
0
      __func__);
1165
0
    return;
1166
0
  }
1167
1168
0
  memset(&prd, 0, sizeof(prd));
1169
0
  prd = rfg->rd;
1170
0
  prd.family = AF_UNSPEC;
1171
0
  prd.prefixlen = 64;
1172
1173
0
  if (rfg->rd.family == AF_UNIX) {
1174
    /* means "auto" with VN addr */
1175
0
    if (rfapi_set_autord_from_vn(&prd, &vnaddr)) {
1176
0
      vnc_zlog_debug_verbose(
1177
0
        "%s: can't auto-assign RD, skipping", __func__);
1178
0
      return;
1179
0
    }
1180
0
  }
1181
1182
1183
0
  vncHDBgpDirect.peer = info->peer;
1184
0
  vnc_zlog_debug_verbose("%s: setting peer to %p", __func__,
1185
0
             vncHDBgpDirect.peer);
1186
0
  del_vnc_route(&vncHDBgpDirect, info->peer, bgp, SAFI_MPLS_VPN, prefix,
1187
0
          &prd, ZEBRA_ROUTE_BGP_DIRECT, BGP_ROUTE_REDISTRIBUTE,
1188
0
          NULL, 1);
1189
1190
0
  vncHDBgpDirect.peer = NULL;
1191
0
}
1192
1193
static void vnc_import_bgp_del_route_mode_resolve_nve_one_bi(
1194
  struct bgp *bgp, afi_t afi, struct bgp_path_info *bpi, /* VPN bpi */
1195
  struct prefix_rd *prd,               /* RD */
1196
  const struct prefix *prefix) /* unicast route prefix */
1197
0
{
1198
0
  struct prefix un;
1199
1200
0
  if (bpi->type != ZEBRA_ROUTE_BGP
1201
0
      && bpi->type != ZEBRA_ROUTE_BGP_DIRECT) {
1202
1203
0
    return;
1204
0
  }
1205
0
  if (bpi->sub_type != BGP_ROUTE_NORMAL
1206
0
      && bpi->sub_type != BGP_ROUTE_STATIC
1207
0
      && bpi->sub_type != BGP_ROUTE_RFP) {
1208
1209
0
    return;
1210
0
  }
1211
0
  if (CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED))
1212
0
    return;
1213
1214
0
  vncHDResolveNve.peer = bpi->peer;
1215
0
  if (!rfapiGetVncTunnelUnAddr(bpi->attr, &un)) {
1216
0
    if (rfapiQprefix2Raddr(&un, &vncHDResolveNve.un_addr))
1217
0
      return;
1218
0
  } else {
1219
0
    memset(&vncHDResolveNve.un_addr, 0,
1220
0
           sizeof(vncHDResolveNve.un_addr));
1221
0
  }
1222
1223
0
  del_vnc_route(&vncHDResolveNve, vncHDResolveNve.peer, bgp,
1224
0
          SAFI_MPLS_VPN, prefix, /* unicast route prefix */
1225
0
          prd, ZEBRA_ROUTE_BGP_DIRECT, BGP_ROUTE_REDISTRIBUTE, NULL,
1226
0
          0); /* flags */
1227
0
}
1228
1229
static void vnc_import_bgp_del_route_mode_resolve_nve_one_rd(
1230
  struct prefix_rd *prd,
1231
  struct bgp_table *table_rd, /* per-rd VPN route table */
1232
  afi_t afi, struct bgp *bgp,
1233
  const struct prefix *prefix,     /* unicast prefix */
1234
  const struct prefix *ubpi_nexthop) /* unicast bpi's nexthop */
1235
0
{
1236
0
  struct bgp_dest *bd;
1237
0
  struct bgp_path_info *bpi;
1238
1239
0
  if (!table_rd)
1240
0
    return;
1241
1242
0
  vnc_zlog_debug_verbose("%s: ubpi_nexthop=%pFX", __func__, ubpi_nexthop);
1243
1244
1245
  /* exact match */
1246
0
  bd = bgp_node_lookup(table_rd, ubpi_nexthop);
1247
0
  if (!bd) {
1248
0
    vnc_zlog_debug_verbose(
1249
0
      "%s: no match in RD's table for ubpi_nexthop",
1250
0
      __func__);
1251
0
    return;
1252
0
  }
1253
1254
  /* Iterate over bgp_info items at this node */
1255
0
  for (bpi = bgp_dest_get_bgp_path_info(bd); bpi; bpi = bpi->next) {
1256
1257
0
    vnc_import_bgp_del_route_mode_resolve_nve_one_bi(
1258
0
      bgp, afi, bpi, /* VPN bpi */
1259
0
      prd,     /* VPN RD */
1260
0
      prefix);       /* unicast route prefix */
1261
0
  }
1262
1263
0
  bgp_dest_unlock_node(bd);
1264
0
}
1265
1266
static void
1267
vnc_import_bgp_del_route_mode_resolve_nve(struct bgp *bgp, afi_t afi,
1268
            const struct prefix *prefix,
1269
            struct bgp_path_info *info)
1270
0
{
1271
0
  struct ecommunity *ecom = NULL;
1272
0
  struct prefix pfx_unicast_nexthop = {0}; /* happy valgrind */
1273
1274
  // struct listnode           *hnode;
1275
  // struct rfapi_descriptor   *rfd;
1276
0
  struct prefix_bag *pb;
1277
0
  void *cursor;
1278
0
  struct skiplist *sl = bgp->rfapi->resolve_nve_nexthop;
1279
0
  int rc;
1280
0
  struct bgp_dest *bdp; /* prd table node */
1281
1282
0
  if (!sl) {
1283
0
    vnc_zlog_debug_verbose("%s: no RHN entries, skipping",
1284
0
               __func__);
1285
0
    return;
1286
0
  }
1287
1288
0
  if (info->type != ZEBRA_ROUTE_BGP) {
1289
0
    vnc_zlog_debug_verbose(
1290
0
      "%s: unicast type %d=\"%s\" is not %d=%s, skipping",
1291
0
      __func__, info->type, zebra_route_string(info->type),
1292
0
      ZEBRA_ROUTE_BGP, "ZEBRA_ROUTE_BGP");
1293
0
    return;
1294
0
  }
1295
1296
0
  if (process_unicast_route(bgp, afi, prefix, info, &ecom,
1297
0
          &pfx_unicast_nexthop)) {
1298
1299
0
    vnc_zlog_debug_verbose(
1300
0
      "%s: process_unicast_route error, skipping", __func__);
1301
0
    return;
1302
0
  }
1303
1304
0
  rc = skiplist_first_value(sl, &pfx_unicast_nexthop, (void *)&pb,
1305
0
          &cursor);
1306
0
  while (!rc) {
1307
0
    if (pb->ubpi == info) {
1308
0
      skiplist_delete(sl, &pfx_unicast_nexthop, pb);
1309
0
      bgp_path_info_unlock(info);
1310
0
      break;
1311
0
    }
1312
0
    rc = skiplist_next_value(sl, &pfx_unicast_nexthop, (void *)&pb,
1313
0
           &cursor);
1314
0
  }
1315
1316
  /*
1317
   * Iterate over RDs in VPN RIB. For each RD, look up unicast nexthop
1318
   * (exact match, /32). If an exact match is found, call add_vnc_route.
1319
   */
1320
1321
0
  for (bdp = bgp_table_top(bgp->rib[afi][SAFI_MPLS_VPN]); bdp;
1322
0
       bdp = bgp_route_next(bdp)) {
1323
1324
0
    struct bgp_table *table;
1325
1326
0
    table = bgp_dest_get_bgp_table_info(bdp);
1327
1328
0
    if (!table)
1329
0
      continue;
1330
1331
0
    vnc_import_bgp_del_route_mode_resolve_nve_one_rd(
1332
0
      (struct prefix_rd *)bgp_dest_get_prefix(bdp), table,
1333
0
      afi, bgp, prefix, &pfx_unicast_nexthop);
1334
0
  }
1335
1336
0
  if (ecom)
1337
0
    ecommunity_free(&ecom);
1338
0
}
1339
1340
1341
/***********************************************************************
1342
 *      Add/Delete CE->NVE routes
1343
 ***********************************************************************/
1344
1345
/*
1346
 * Should be called whan a bpi is added to VPN RIB. This function
1347
 * will check if it is a host route and return immediately if not.
1348
 */
1349
void vnc_import_bgp_add_vnc_host_route_mode_resolve_nve(
1350
  struct bgp *bgp, struct prefix_rd *prd, /* RD */
1351
  struct bgp_table *table_rd,   /* per-rd VPN route table */
1352
  const struct prefix *prefix,    /* VPN prefix */
1353
  struct bgp_path_info *bpi)    /* new VPN host route */
1354
0
{
1355
0
  afi_t afi = family2afi(prefix->family);
1356
0
  struct skiplist *sl = NULL;
1357
0
  int rc;
1358
0
  struct prefix_bag *pb;
1359
0
  void *cursor;
1360
0
  struct rfapi_cfg *hc = NULL;
1361
1362
0
  vnc_zlog_debug_verbose("%s: entry", __func__);
1363
1364
0
  if (afi != AFI_IP && afi != AFI_IP6) {
1365
0
    vnc_zlog_debug_verbose("%s: bad afi %d, skipping", __func__,
1366
0
               afi);
1367
0
    return;
1368
0
  }
1369
1370
0
  if (!(hc = bgp->rfapi_cfg)) {
1371
0
    vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
1372
0
               __func__);
1373
0
    return;
1374
0
  }
1375
1376
  /* check vnc redist flag for bgp direct routes */
1377
0
  if (!hc->redist[afi][ZEBRA_ROUTE_BGP_DIRECT]) {
1378
0
    vnc_zlog_debug_verbose(
1379
0
      "%s: bgp->rfapi_cfg->redist[afi=%d][type=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
1380
0
      __func__, afi);
1381
0
    return;
1382
0
  }
1383
1384
0
  if (hc->redist_mode != VNC_REDIST_MODE_RESOLVE_NVE) {
1385
0
    vnc_zlog_debug_verbose("%s: not in resolve-nve mode, skipping",
1386
0
               __func__);
1387
0
    return;
1388
0
  }
1389
1390
0
  if (bgp->rfapi)
1391
0
    sl = bgp->rfapi->resolve_nve_nexthop;
1392
1393
0
  if (!sl) {
1394
0
    vnc_zlog_debug_verbose(
1395
0
      "%s: no resolve_nve_nexthop skiplist, skipping",
1396
0
      __func__);
1397
0
    return;
1398
0
  }
1399
1400
0
  if (!is_host_prefix(prefix)) {
1401
0
    vnc_zlog_debug_verbose("%s: not host prefix, skipping",
1402
0
               __func__);
1403
0
    return;
1404
0
  }
1405
1406
0
  rc = skiplist_first_value(sl, prefix, (void *)&pb, &cursor);
1407
0
  while (!rc) {
1408
0
    struct ecommunity *ecom;
1409
0
    struct prefix pfx_unicast_nexthop;
1410
0
    uint32_t *med = NULL;
1411
0
    uint32_t local_pref;
1412
1413
0
    memset(&pfx_unicast_nexthop, 0,
1414
0
           sizeof(pfx_unicast_nexthop)); /* keep valgrind happy */
1415
1416
0
    if (VNC_DEBUG(IMPORT_BGP_ADD_ROUTE))
1417
0
      vnc_zlog_debug_any(
1418
0
        "%s: examining RHN Entry (q=%p): upfx=%pFX, hpfx=%pFX, ubpi=%p",
1419
0
        __func__, cursor, &pb->upfx, &pb->hpfx,
1420
0
        pb->ubpi);
1421
1422
0
    if (process_unicast_route(bgp, afi, &pb->upfx, pb->ubpi, &ecom,
1423
0
            &pfx_unicast_nexthop)) {
1424
1425
0
      vnc_zlog_debug_verbose(
1426
0
        "%s: process_unicast_route error, skipping",
1427
0
        __func__);
1428
0
      continue;
1429
0
    }
1430
0
    local_pref = calc_local_pref(pb->ubpi->attr, pb->ubpi->peer);
1431
1432
0
    if (pb->ubpi->attr->flag
1433
0
        & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
1434
0
      med = &pb->ubpi->attr->med;
1435
1436
    /*
1437
     * Sanity check
1438
     */
1439
0
    if (vnc_prefix_cmp(&pfx_unicast_nexthop, prefix)) {
1440
0
      vnc_zlog_debug_verbose(
1441
0
        "%s: FATAL: resolve_nve_nexthop list item bpi nexthop %pFX != nve pfx %pFX",
1442
0
        __func__, &pfx_unicast_nexthop, prefix);
1443
0
      assert(0);
1444
0
    }
1445
1446
0
    vnc_import_bgp_add_route_mode_resolve_nve_one_bi(
1447
0
      bgp, afi, bpi,  /* VPN bpi */
1448
0
      prd, &pb->upfx, /* unicast prefix */
1449
0
      &local_pref, med, ecom);
1450
1451
0
    if (ecom)
1452
0
      ecommunity_free(&ecom);
1453
1454
#if DEBUG_RHN_LIST
1455
    /* debug */
1456
    {
1457
      vnc_zlog_debug_verbose(
1458
        "%s: advancing past RHN Entry (q=%p): with prefix %pFX",
1459
        __func__, cursor, prefix);
1460
      print_rhn_list(__func__, NULL); /* debug */
1461
    }
1462
#endif
1463
0
    rc = skiplist_next_value(sl, prefix, (void *)&pb, &cursor);
1464
0
  }
1465
0
  vnc_zlog_debug_verbose("%s: done", __func__);
1466
0
}
1467
1468
1469
void vnc_import_bgp_del_vnc_host_route_mode_resolve_nve(
1470
  struct bgp *bgp, struct prefix_rd *prd, /* RD */
1471
  struct bgp_table *table_rd,   /* per-rd VPN route table */
1472
  const struct prefix *prefix,    /* VPN prefix */
1473
  struct bgp_path_info *bpi)    /* old VPN host route */
1474
0
{
1475
0
  afi_t afi = family2afi(prefix->family);
1476
0
  struct skiplist *sl = NULL;
1477
0
  struct prefix_bag *pb;
1478
0
  void *cursor;
1479
0
  struct rfapi_cfg *hc = NULL;
1480
0
  int rc;
1481
1482
0
  vnc_zlog_debug_verbose("%s(bgp=%p, nve prefix=%pFX)", __func__, bgp,
1483
0
             prefix);
1484
1485
0
  if (afi != AFI_IP && afi != AFI_IP6)
1486
0
    return;
1487
1488
0
  if (!(hc = bgp->rfapi_cfg)) {
1489
0
    vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
1490
0
               __func__);
1491
0
    return;
1492
0
  }
1493
1494
  /* check vnc redist flag for bgp direct routes */
1495
0
  if (!hc->redist[afi][ZEBRA_ROUTE_BGP_DIRECT]) {
1496
0
    vnc_zlog_debug_verbose(
1497
0
      "%s: bgp->rfapi_cfg->redist[afi=%d][type=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
1498
0
      __func__, afi);
1499
0
    return;
1500
0
  }
1501
1502
0
  if (hc->redist_mode != VNC_REDIST_MODE_RESOLVE_NVE) {
1503
0
    vnc_zlog_debug_verbose("%s: not in resolve-nve mode, skipping",
1504
0
               __func__);
1505
0
    return;
1506
0
  }
1507
1508
0
  if (bgp->rfapi)
1509
0
    sl = bgp->rfapi->resolve_nve_nexthop;
1510
1511
0
  if (!sl) {
1512
0
    vnc_zlog_debug_verbose("%s: no RHN entries, skipping",
1513
0
               __func__);
1514
0
    return;
1515
0
  }
1516
1517
0
  if (!is_host_prefix(prefix)) {
1518
0
    vnc_zlog_debug_verbose("%s: not host route, skip", __func__);
1519
0
    return;
1520
0
  }
1521
1522
  /*
1523
   * Find all entries with key == CE in the RHN list
1524
   */
1525
0
  rc = skiplist_first_value(sl, prefix, (void *)&pb, &cursor);
1526
0
  while (!rc) {
1527
1528
0
    struct ecommunity *ecom;
1529
0
    struct prefix pfx_unicast_nexthop;
1530
1531
0
    memset(&pfx_unicast_nexthop, 0,
1532
0
           sizeof(pfx_unicast_nexthop)); /* keep valgrind happy */
1533
1534
0
    if (process_unicast_route(bgp, afi, &pb->upfx, pb->ubpi, &ecom,
1535
0
            &pfx_unicast_nexthop)) {
1536
1537
0
      vnc_zlog_debug_verbose(
1538
0
        "%s: process_unicast_route error, skipping",
1539
0
        __func__);
1540
0
      continue;
1541
0
    }
1542
1543
    /*
1544
     * Sanity check
1545
     */
1546
0
    if (vnc_prefix_cmp(&pfx_unicast_nexthop, prefix)) {
1547
0
      vnc_zlog_debug_verbose(
1548
0
        "%s: FATAL: resolve_nve_nexthop list item bpi nexthop %pFX != nve pfx %pFX",
1549
0
        __func__, &pfx_unicast_nexthop, prefix);
1550
0
      assert(0);
1551
0
    }
1552
1553
0
    vnc_import_bgp_del_route_mode_resolve_nve_one_bi(
1554
0
      bgp, afi, bpi, prd, &pb->upfx);
1555
1556
0
    if (ecom)
1557
0
      ecommunity_free(&ecom);
1558
1559
0
    rc = skiplist_next_value(sl, prefix, (void *)&pb, &cursor);
1560
0
  }
1561
0
}
1562
1563
1564
/***********************************************************************
1565
 *      Exterior Routes
1566
 ***********************************************************************/
1567
1568
#define DEBUG_IS_USABLE_INTERIOR 1
1569
1570
static int is_usable_interior_route(struct bgp_path_info *bpi_interior)
1571
0
{
1572
0
  if (!VALID_INTERIOR_TYPE(bpi_interior->type)) {
1573
0
#if DEBUG_IS_USABLE_INTERIOR
1574
0
    vnc_zlog_debug_verbose(
1575
0
      "%s: NO: type %d is not valid interior type", __func__,
1576
0
      bpi_interior->type);
1577
0
#endif
1578
0
    return 0;
1579
0
  }
1580
0
  if (!CHECK_FLAG(bpi_interior->flags, BGP_PATH_VALID)) {
1581
0
#if DEBUG_IS_USABLE_INTERIOR
1582
0
    vnc_zlog_debug_verbose("%s: NO: BGP_PATH_VALID not set",
1583
0
               __func__);
1584
0
#endif
1585
0
    return 0;
1586
0
  }
1587
0
  return 1;
1588
0
}
1589
1590
/*
1591
 * There should be only one of these per prefix at a time.
1592
 * This should be called as a result of selection operation
1593
 *
1594
 * NB should be called espacially for bgp instances that are named,
1595
 * because the exterior routes will always come from one of those.
1596
 * We filter here on the instance name to make sure we get only the
1597
 * right routes.
1598
 */
1599
static void vnc_import_bgp_exterior_add_route_it(
1600
  struct bgp *bgp,        /* exterior instance, we hope */
1601
  const struct prefix *prefix,      /* unicast prefix */
1602
  struct bgp_path_info *info,     /* unicast info */
1603
  struct rfapi_import_table *it_only) /* NULL, or limit to this IT */
1604
0
{
1605
0
  struct rfapi *h;
1606
0
  struct rfapi_cfg *hc;
1607
0
  struct prefix pfx_orig_nexthop;
1608
0
  struct rfapi_import_table *it;
1609
0
  struct bgp *bgp_default = bgp_get_default();
1610
0
  afi_t afi = family2afi(prefix->family);
1611
1612
0
  if (!bgp_default)
1613
0
    return;
1614
1615
0
  h = bgp_default->rfapi;
1616
0
  hc = bgp_default->rfapi_cfg;
1617
1618
0
  vnc_zlog_debug_verbose("%s: entry with it=%p", __func__, it_only);
1619
1620
0
  if (!h || !hc) {
1621
0
    vnc_zlog_debug_verbose(
1622
0
      "%s: rfapi or rfapi_cfg not instantiated, skipping",
1623
0
      __func__);
1624
0
    return;
1625
0
  }
1626
0
  if (!hc->redist_bgp_exterior_view) {
1627
0
    vnc_zlog_debug_verbose("%s: exterior view not set, skipping",
1628
0
               __func__);
1629
0
    return;
1630
0
  }
1631
0
  if (bgp != hc->redist_bgp_exterior_view) {
1632
0
    vnc_zlog_debug_verbose(
1633
0
      "%s: bgp %p != hc->redist_bgp_exterior_view %p, skipping",
1634
0
      __func__, bgp, hc->redist_bgp_exterior_view);
1635
0
    return;
1636
0
  }
1637
1638
0
  if (!hc->redist[afi][ZEBRA_ROUTE_BGP_DIRECT_EXT]) {
1639
0
    vnc_zlog_debug_verbose(
1640
0
      "%s: redist of exterior routes not enabled, skipping",
1641
0
      __func__);
1642
0
    return;
1643
0
  }
1644
1645
  /*
1646
   * Extract nexthop from exterior route
1647
   *
1648
   * Incoming prefix is unicast. If v6, it is in multiprotocol area,
1649
   * but if v4 it is in attr->nexthop
1650
   */
1651
0
  rfapiUnicastNexthop2Prefix(afi, info->attr, &pfx_orig_nexthop);
1652
1653
0
  for (it = h->imports; it; it = it->next) {
1654
0
    struct agg_table *table;
1655
0
    struct agg_node *rn;
1656
0
    struct agg_node *par;
1657
0
    struct bgp_path_info *bpi_interior;
1658
0
    int have_usable_route;
1659
1660
0
    vnc_zlog_debug_verbose("%s: doing it %p", __func__, it);
1661
1662
0
    if (it_only && (it_only != it)) {
1663
0
      vnc_zlog_debug_verbose("%s: doesn't match it_only %p",
1664
0
                 __func__, it_only);
1665
0
      continue;
1666
0
    }
1667
1668
0
    table = it->imported_vpn[afi];
1669
1670
0
    for (rn = agg_node_match(table, &pfx_orig_nexthop),
1671
0
        have_usable_route = 0;
1672
0
         (!have_usable_route) && rn;) {
1673
1674
0
      vnc_zlog_debug_verbose("%s: it %p trying rn %p",
1675
0
                 __func__, it, rn);
1676
1677
0
      for (bpi_interior = rn->info; bpi_interior;
1678
0
           bpi_interior = bpi_interior->next) {
1679
0
        struct prefix_rd *prd;
1680
0
        struct attr new_attr;
1681
0
        uint32_t label = 0;
1682
1683
0
        if (!is_usable_interior_route(bpi_interior))
1684
0
          continue;
1685
1686
0
        vnc_zlog_debug_verbose(
1687
0
          "%s: usable: bpi_interior %p", __func__,
1688
0
          bpi_interior);
1689
1690
        /*
1691
         * have a legitimate route to exterior's nexthop
1692
         * via NVE.
1693
         *
1694
         * Import unicast route to the import table
1695
         */
1696
0
        have_usable_route = 1;
1697
1698
0
        if (bpi_interior->extra) {
1699
0
          prd = &bpi_interior->extra->vnc.import
1700
0
                   .rd;
1701
0
          label = decode_label(
1702
0
            &bpi_interior->extra->label[0]);
1703
0
        } else
1704
0
          prd = NULL;
1705
1706
        /* use local_pref from unicast route */
1707
0
        memset(&new_attr, 0, sizeof(new_attr));
1708
0
        new_attr = *bpi_interior->attr;
1709
0
        if (info->attr->flag
1710
0
            & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF)) {
1711
0
          new_attr.local_pref =
1712
0
            info->attr->local_pref;
1713
0
          new_attr.flag |= ATTR_FLAG_BIT(
1714
0
            BGP_ATTR_LOCAL_PREF);
1715
0
        }
1716
1717
0
        rfapiBgpInfoFilteredImportVPN(
1718
0
          it, FIF_ACTION_UPDATE,
1719
0
          bpi_interior->peer, NULL, /* rfd */
1720
0
          prefix, NULL, afi, prd, &new_attr,
1721
0
          ZEBRA_ROUTE_BGP_DIRECT_EXT,
1722
0
          BGP_ROUTE_REDISTRIBUTE, &label);
1723
0
      }
1724
1725
0
      if (have_usable_route) {
1726
        /*
1727
         * Make monitor
1728
         *
1729
         * TBD factor this out into its own function
1730
         */
1731
0
        struct prefix *pfx_mon = prefix_new();
1732
0
        if (!RFAPI_MONITOR_EXTERIOR(rn)->source) {
1733
0
          RFAPI_MONITOR_EXTERIOR(rn)->source =
1734
0
            skiplist_new(
1735
0
              0, NULL,
1736
0
              prefix_free_lists);
1737
0
          agg_lock_node(rn); /* for skiplist */
1738
0
        }
1739
0
        agg_lock_node(rn); /* for skiplist entry */
1740
0
        prefix_copy(pfx_mon, prefix);
1741
0
        if (!skiplist_insert(
1742
0
              RFAPI_MONITOR_EXTERIOR(rn)->source,
1743
0
              info, pfx_mon)) {
1744
1745
0
          bgp_path_info_lock(info);
1746
0
        }
1747
0
      }
1748
0
      par = agg_node_parent(rn);
1749
0
      if (par)
1750
0
        agg_lock_node(par);
1751
0
      agg_unlock_node(rn);
1752
0
      rn = par;
1753
0
    }
1754
0
    if (rn)
1755
0
      agg_unlock_node(rn);
1756
1757
0
    if (!have_usable_route) {
1758
0
      struct prefix *pfx_mon = prefix_new();
1759
0
      prefix_copy(pfx_mon, prefix);
1760
0
      if (!skiplist_insert(it->monitor_exterior_orphans, info,
1761
0
               pfx_mon)) {
1762
1763
0
        bgp_path_info_lock(info);
1764
0
      }
1765
0
    }
1766
0
  }
1767
0
}
1768
1769
void vnc_import_bgp_exterior_add_route(
1770
  struct bgp *bgp,       /* exterior instance, we hope */
1771
  const struct prefix *prefix, /* unicast prefix */
1772
  struct bgp_path_info *info)  /* unicast info */
1773
0
{
1774
0
  vnc_import_bgp_exterior_add_route_it(bgp, prefix, info, NULL);
1775
0
}
1776
1777
/*
1778
 * There should be only one of these per prefix at a time.
1779
 * This should probably be called as a result of selection operation.
1780
 *
1781
 * NB should be called espacially for bgp instances that are named,
1782
 * because the exterior routes will always come from one of those.
1783
 * We filter here on the instance name to make sure we get only the
1784
 * right routes.
1785
 */
1786
void vnc_import_bgp_exterior_del_route(
1787
  struct bgp *bgp, const struct prefix *prefix, /* unicast prefix */
1788
  struct bgp_path_info *info)         /* unicast info */
1789
0
{
1790
0
  struct rfapi *h;
1791
0
  struct rfapi_cfg *hc;
1792
0
  struct rfapi_import_table *it;
1793
0
  struct prefix pfx_orig_nexthop;
1794
0
  afi_t afi = family2afi(prefix->family);
1795
0
  struct bgp *bgp_default = bgp_get_default();
1796
1797
0
  if (!bgp_default)
1798
0
    return;
1799
1800
0
  memset(&pfx_orig_nexthop, 0,
1801
0
         sizeof(pfx_orig_nexthop)); /* keep valgrind happy */
1802
1803
0
  h = bgp_default->rfapi;
1804
0
  hc = bgp_default->rfapi_cfg;
1805
1806
0
  if (!h || !hc) {
1807
0
    vnc_zlog_debug_verbose(
1808
0
      "%s: rfapi or rfapi_cfg not instantiated, skipping",
1809
0
      __func__);
1810
0
    return;
1811
0
  }
1812
0
  if (!hc->redist_bgp_exterior_view) {
1813
0
    vnc_zlog_debug_verbose("%s: exterior view not set, skipping",
1814
0
               __func__);
1815
0
    return;
1816
0
  }
1817
0
  if (bgp != hc->redist_bgp_exterior_view) {
1818
0
    vnc_zlog_debug_verbose(
1819
0
      "%s: bgp %p != hc->redist_bgp_exterior_view %p, skipping",
1820
0
      __func__, bgp, hc->redist_bgp_exterior_view);
1821
0
    return;
1822
0
  }
1823
0
  if (!hc->redist[afi][ZEBRA_ROUTE_BGP_DIRECT_EXT]) {
1824
0
    vnc_zlog_debug_verbose(
1825
0
      "%s: redist of exterior routes no enabled, skipping",
1826
0
      __func__);
1827
0
    return;
1828
0
  }
1829
1830
  /*
1831
   * Extract nexthop from exterior route
1832
   *
1833
   * Incoming prefix is unicast. If v6, it is in multiprotocol area,
1834
   * but if v4 it is in attr->nexthop
1835
   */
1836
0
  rfapiUnicastNexthop2Prefix(afi, info->attr, &pfx_orig_nexthop);
1837
1838
0
  for (it = h->imports; it; it = it->next) {
1839
0
    struct agg_table *table;
1840
0
    struct agg_node *rn;
1841
0
    struct agg_node *par;
1842
0
    struct bgp_path_info *bpi_interior;
1843
0
    int have_usable_route;
1844
1845
0
    table = it->imported_vpn[afi];
1846
1847
0
    for (rn = agg_node_match(table, &pfx_orig_nexthop),
1848
0
        have_usable_route = 0;
1849
0
         (!have_usable_route) && rn;) {
1850
1851
0
      for (bpi_interior = rn->info; bpi_interior;
1852
0
           bpi_interior = bpi_interior->next) {
1853
0
        struct prefix_rd *prd;
1854
0
        uint32_t label = 0;
1855
1856
0
        if (!is_usable_interior_route(bpi_interior))
1857
0
          continue;
1858
1859
        /*
1860
         * have a legitimate route to exterior's nexthop
1861
         * via NVE.
1862
         *
1863
         * Import unicast route to the import table
1864
         */
1865
0
        have_usable_route = 1;
1866
1867
0
        if (bpi_interior->extra) {
1868
0
          prd = &bpi_interior->extra->vnc.import
1869
0
                   .rd;
1870
0
          label = decode_label(
1871
0
            &bpi_interior->extra->label[0]);
1872
0
        } else
1873
0
          prd = NULL;
1874
1875
0
        rfapiBgpInfoFilteredImportVPN(
1876
0
          it, FIF_ACTION_KILL, bpi_interior->peer,
1877
0
          NULL, /* rfd */
1878
0
          prefix, NULL, afi, prd,
1879
0
          bpi_interior->attr,
1880
0
          ZEBRA_ROUTE_BGP_DIRECT_EXT,
1881
0
          BGP_ROUTE_REDISTRIBUTE, &label);
1882
1883
        /*
1884
         * Delete monitor
1885
         *
1886
         * TBD factor this out into its own function
1887
         */
1888
0
        {
1889
0
          if (RFAPI_MONITOR_EXTERIOR(rn)
1890
0
                ->source) {
1891
0
            if (!skiplist_delete(
1892
0
                  RFAPI_MONITOR_EXTERIOR(
1893
0
                    rn)
1894
0
                    ->source,
1895
0
                  info, NULL)) {
1896
1897
0
              bgp_path_info_unlock(
1898
0
                info);
1899
0
              agg_unlock_node(
1900
0
                rn); /* sl entry
1901
                      */
1902
0
            }
1903
0
            if (skiplist_empty(
1904
0
                  RFAPI_MONITOR_EXTERIOR(
1905
0
                    rn)
1906
0
                    ->source)) {
1907
0
              skiplist_free(
1908
0
                RFAPI_MONITOR_EXTERIOR(
1909
0
                  rn)
1910
0
                  ->source);
1911
0
              RFAPI_MONITOR_EXTERIOR(
1912
0
                rn)
1913
0
                ->source = NULL;
1914
0
              agg_unlock_node(
1915
0
                rn); /* skiplist
1916
                  itself
1917
                  */
1918
0
            }
1919
0
          }
1920
0
        }
1921
0
      }
1922
0
      par = agg_node_parent(rn);
1923
0
      if (par)
1924
0
        agg_lock_node(par);
1925
0
      agg_unlock_node(rn);
1926
0
      rn = par;
1927
0
    }
1928
0
    if (rn)
1929
0
      agg_unlock_node(rn);
1930
1931
0
    if (!have_usable_route) {
1932
0
      if (!skiplist_delete(it->monitor_exterior_orphans, info,
1933
0
               NULL)) {
1934
1935
0
        bgp_path_info_unlock(info);
1936
0
      }
1937
0
    }
1938
0
  }
1939
0
}
1940
1941
/*
1942
 * This function should be called after a new interior VPN route
1943
 * has been added to an import_table.
1944
 *
1945
 * NB should also be called whenever an existing vpn interior route
1946
 * becomes valid (e.g., valid_interior_count is inremented)
1947
 */
1948
void vnc_import_bgp_exterior_add_route_interior(
1949
  struct bgp *bgp, struct rfapi_import_table *it,
1950
  struct agg_node *rn_interior,       /* VPN IT node */
1951
  struct bgp_path_info *bpi_interior) /* VPN IT route */
1952
0
{
1953
0
  const struct prefix *p = agg_node_get_prefix(rn_interior);
1954
0
  afi_t afi = family2afi(p->family);
1955
0
  struct agg_node *par;
1956
0
  struct bgp_path_info *bpi_exterior;
1957
0
  struct prefix *pfx_exterior; /* exterior pfx */
1958
0
  void *cursor;
1959
0
  int rc;
1960
0
  struct list *list_adopted;
1961
1962
0
  vnc_zlog_debug_verbose("%s: entry", __func__);
1963
1964
0
  if (!is_usable_interior_route(bpi_interior)) {
1965
0
    vnc_zlog_debug_verbose(
1966
0
      "%s: not usable interior route, skipping", __func__);
1967
0
    return;
1968
0
  }
1969
1970
0
  if (!bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT_EXT]) {
1971
0
    vnc_zlog_debug_verbose(
1972
0
      "%s: redist of exterior routes no enabled, skipping",
1973
0
      __func__);
1974
0
    return;
1975
0
  }
1976
1977
0
  if (it == bgp->rfapi->it_ce) {
1978
0
    vnc_zlog_debug_verbose("%s: import table is it_ce, skipping",
1979
0
               __func__);
1980
0
    return;
1981
0
  }
1982
1983
  /*debugging */
1984
0
  vnc_zlog_debug_verbose("%s: interior prefix=%pRN, bpi type=%d",
1985
0
             __func__, rn_interior, bpi_interior->type);
1986
1987
0
  if (RFAPI_HAS_MONITOR_EXTERIOR(rn_interior)) {
1988
1989
0
    vnc_zlog_debug_verbose(
1990
0
      "%s: has exterior monitor; ext src: %p", __func__,
1991
0
      RFAPI_MONITOR_EXTERIOR(rn_interior)->source);
1992
1993
    /*
1994
     * There is a monitor here already. Therefore, we do not need
1995
     * to do any pulldown. Just construct exterior routes based
1996
     * on the new interior route.
1997
     */
1998
0
    cursor = NULL;
1999
0
    for (rc = skiplist_next(
2000
0
           RFAPI_MONITOR_EXTERIOR(rn_interior)->source,
2001
0
           (void **)&bpi_exterior, (void **)&pfx_exterior,
2002
0
           &cursor);
2003
0
         !rc; rc = skiplist_next(
2004
0
          RFAPI_MONITOR_EXTERIOR(rn_interior)->source,
2005
0
          (void **)&bpi_exterior,
2006
0
          (void **)&pfx_exterior, &cursor)) {
2007
2008
0
      struct prefix_rd *prd;
2009
0
      struct attr new_attr;
2010
0
      uint32_t label = 0;
2011
2012
0
      assert(bpi_exterior);
2013
0
      assert(pfx_exterior);
2014
2015
0
      if (bpi_interior->extra) {
2016
0
        prd = &bpi_interior->extra->vnc.import.rd;
2017
0
        label = decode_label(
2018
0
          &bpi_interior->extra->label[0]);
2019
0
      } else
2020
0
        prd = NULL;
2021
2022
      /* use local_pref from unicast route */
2023
0
      memset(&new_attr, 0, sizeof(struct attr));
2024
0
      new_attr = *bpi_interior->attr;
2025
0
      if (bpi_exterior
2026
0
          && (bpi_exterior->attr->flag
2027
0
        & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))) {
2028
0
        new_attr.local_pref =
2029
0
          bpi_exterior->attr->local_pref;
2030
0
        new_attr.flag |=
2031
0
          ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
2032
0
      }
2033
2034
0
      rfapiBgpInfoFilteredImportVPN(
2035
0
        it, FIF_ACTION_UPDATE, bpi_interior->peer,
2036
0
        NULL, /* rfd */
2037
0
        pfx_exterior, NULL, afi, prd, &new_attr,
2038
0
        ZEBRA_ROUTE_BGP_DIRECT_EXT,
2039
0
        BGP_ROUTE_REDISTRIBUTE, &label);
2040
0
    }
2041
0
    vnc_zlog_debug_verbose(
2042
0
      "%s: finished constructing exteriors based on existing monitors",
2043
0
      __func__);
2044
0
    return;
2045
0
  }
2046
2047
0
  vnc_zlog_debug_verbose("%s: no exterior monitor", __func__);
2048
2049
  /*
2050
   * No monitor at this node. Is this the first valid interior
2051
   * route at this node?
2052
   */
2053
0
  if (RFAPI_MONITOR_EXTERIOR(rn_interior)->valid_interior_count > 1) {
2054
0
    vnc_zlog_debug_verbose(
2055
0
      "%s: new interior route not first valid one, skipping pulldown",
2056
0
      __func__);
2057
0
    return;
2058
0
  }
2059
2060
  /*
2061
   * Look up the tree for possible pulldown candidates.
2062
   * Find nearest parent with an exterior route monitor
2063
   */
2064
0
  for (par = agg_node_parent(rn_interior); par;
2065
0
       par = agg_node_parent(par)) {
2066
0
    if (RFAPI_HAS_MONITOR_EXTERIOR(par))
2067
0
      break;
2068
0
  }
2069
2070
0
  if (par) {
2071
2072
0
    vnc_zlog_debug_verbose(
2073
0
      "%s: checking parent %p for possible pulldowns",
2074
0
      __func__, par);
2075
2076
    /* check monitors at par for possible pulldown */
2077
0
    cursor = NULL;
2078
0
    for (rc = skiplist_next(RFAPI_MONITOR_EXTERIOR(par)->source,
2079
0
          (void **)&bpi_exterior,
2080
0
          (void **)&pfx_exterior, &cursor);
2081
0
         !rc;
2082
0
         rc = skiplist_next(RFAPI_MONITOR_EXTERIOR(par)->source,
2083
0
          (void **)&bpi_exterior,
2084
0
          (void **)&pfx_exterior, &cursor)) {
2085
2086
0
      struct prefix pfx_nexthop;
2087
2088
0
      memset(&pfx_nexthop, 0,
2089
0
             sizeof(struct prefix)); /* keep valgrind happy */
2090
2091
      /* check original nexthop for prefix match */
2092
0
      rfapiUnicastNexthop2Prefix(afi, bpi_exterior->attr,
2093
0
               &pfx_nexthop);
2094
2095
0
      if (prefix_match(p, &pfx_nexthop)) {
2096
2097
0
        struct bgp_path_info *bpi;
2098
0
        struct prefix_rd *prd;
2099
0
        struct attr new_attr;
2100
0
        uint32_t label = 0;
2101
2102
        /* do pull-down */
2103
2104
        /*
2105
         * add monitor to longer prefix
2106
         */
2107
0
        struct prefix *pfx_mon = prefix_new();
2108
0
        prefix_copy(pfx_mon, pfx_exterior);
2109
0
        if (!RFAPI_MONITOR_EXTERIOR(rn_interior)
2110
0
               ->source) {
2111
0
          RFAPI_MONITOR_EXTERIOR(rn_interior)
2112
0
            ->source = skiplist_new(
2113
0
            0, NULL, prefix_free_lists);
2114
0
          agg_lock_node(rn_interior);
2115
0
        }
2116
0
        skiplist_insert(
2117
0
          RFAPI_MONITOR_EXTERIOR(rn_interior)
2118
0
            ->source,
2119
0
          bpi_exterior, pfx_mon);
2120
0
        agg_lock_node(rn_interior);
2121
2122
        /*
2123
         * Delete constructed exterior routes based on
2124
         * parent routes.
2125
         */
2126
0
        for (bpi = par->info; bpi; bpi = bpi->next) {
2127
2128
0
          if (bpi->extra) {
2129
0
            prd = &bpi->extra->vnc.import
2130
0
                     .rd;
2131
0
            label = decode_label(
2132
0
              &bpi->extra->label[0]);
2133
0
          } else
2134
0
            prd = NULL;
2135
2136
0
          rfapiBgpInfoFilteredImportVPN(
2137
0
            it, FIF_ACTION_KILL, bpi->peer,
2138
0
            NULL, /* rfd */
2139
0
            pfx_exterior, NULL, afi, prd,
2140
0
            bpi->attr,
2141
0
            ZEBRA_ROUTE_BGP_DIRECT_EXT,
2142
0
            BGP_ROUTE_REDISTRIBUTE, &label);
2143
0
        }
2144
2145
2146
        /*
2147
         * Add constructed exterior routes based on
2148
         * the new interior route at longer prefix.
2149
         */
2150
0
        if (bpi_interior->extra) {
2151
0
          prd = &bpi_interior->extra->vnc.import
2152
0
                   .rd;
2153
0
          label = decode_label(
2154
0
            &bpi_interior->extra->label[0]);
2155
0
        } else
2156
0
          prd = NULL;
2157
2158
        /* use local_pref from unicast route */
2159
0
        memset(&new_attr, 0, sizeof(struct attr));
2160
0
        new_attr = *bpi_interior->attr;
2161
0
        if (bpi_exterior
2162
0
            && (bpi_exterior->attr->flag
2163
0
          & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))) {
2164
0
          new_attr.local_pref =
2165
0
            bpi_exterior->attr->local_pref;
2166
0
          new_attr.flag |= ATTR_FLAG_BIT(
2167
0
            BGP_ATTR_LOCAL_PREF);
2168
0
        }
2169
2170
0
        rfapiBgpInfoFilteredImportVPN(
2171
0
          it, FIF_ACTION_UPDATE,
2172
0
          bpi_interior->peer, NULL, /* rfd */
2173
0
          pfx_exterior, NULL, afi, prd, &new_attr,
2174
0
          ZEBRA_ROUTE_BGP_DIRECT_EXT,
2175
0
          BGP_ROUTE_REDISTRIBUTE, &label);
2176
0
      }
2177
0
    }
2178
2179
    /*
2180
     * The only monitors at rn_interior are the ones we added just
2181
     * above, so we can use the rn_interior list to identify which
2182
     * monitors to delete from the parent.
2183
     */
2184
0
    cursor = NULL;
2185
0
    for (rc = skiplist_next(
2186
0
           RFAPI_MONITOR_EXTERIOR(rn_interior)->source,
2187
0
           (void **)&bpi_exterior, NULL, &cursor);
2188
0
         !rc; rc = skiplist_next(
2189
0
          RFAPI_MONITOR_EXTERIOR(rn_interior)->source,
2190
0
          (void **)&bpi_exterior, NULL, &cursor)) {
2191
2192
2193
0
      skiplist_delete(RFAPI_MONITOR_EXTERIOR(par)->source,
2194
0
          bpi_exterior, NULL);
2195
0
      agg_unlock_node(par); /* sl entry */
2196
0
    }
2197
0
    if (skiplist_empty(RFAPI_MONITOR_EXTERIOR(par)->source)) {
2198
0
      skiplist_free(RFAPI_MONITOR_EXTERIOR(par)->source);
2199
0
      RFAPI_MONITOR_EXTERIOR(par)->source = NULL;
2200
0
      agg_unlock_node(par); /* sl itself */
2201
0
    }
2202
0
  }
2203
2204
0
  vnc_zlog_debug_verbose("%s: checking orphans", __func__);
2205
2206
  /*
2207
   * See if any orphans can be pulled down to the current node
2208
   */
2209
0
  cursor = NULL;
2210
0
  list_adopted = NULL;
2211
0
  for (rc = skiplist_next(it->monitor_exterior_orphans,
2212
0
        (void **)&bpi_exterior, (void **)&pfx_exterior,
2213
0
        &cursor);
2214
0
       !rc; rc = skiplist_next(it->monitor_exterior_orphans,
2215
0
             (void **)&bpi_exterior,
2216
0
             (void **)&pfx_exterior, &cursor)) {
2217
2218
0
    struct prefix pfx_nexthop;
2219
0
    afi_t afi_exterior = family2afi(pfx_exterior->family);
2220
2221
0
    vnc_zlog_debug_verbose(
2222
0
      "%s: checking exterior orphan at prefix %pFX", __func__,
2223
0
      pfx_exterior);
2224
2225
0
    if (afi_exterior != afi) {
2226
0
      vnc_zlog_debug_verbose(
2227
0
        "%s: exterior orphan afi %d != interior afi %d, skip",
2228
0
        __func__, afi_exterior, afi);
2229
0
      continue;
2230
0
    }
2231
2232
    /* check original nexthop for prefix match */
2233
0
    rfapiUnicastNexthop2Prefix(afi, bpi_exterior->attr,
2234
0
             &pfx_nexthop);
2235
2236
0
    if (prefix_match(p, &pfx_nexthop)) {
2237
2238
0
      struct prefix_rd *prd;
2239
0
      struct attr new_attr;
2240
0
      uint32_t label = 0;
2241
2242
      /* do pull-down */
2243
2244
      /*
2245
       * add monitor to longer prefix
2246
       */
2247
2248
0
      struct prefix *pfx_mon = prefix_new();
2249
0
      prefix_copy(pfx_mon, pfx_exterior);
2250
0
      if (!RFAPI_MONITOR_EXTERIOR(rn_interior)->source) {
2251
0
        RFAPI_MONITOR_EXTERIOR(rn_interior)->source =
2252
0
          skiplist_new(
2253
0
            0, NULL, prefix_free_lists);
2254
0
        agg_lock_node(rn_interior); /* sl */
2255
0
      }
2256
0
      skiplist_insert(
2257
0
        RFAPI_MONITOR_EXTERIOR(rn_interior)->source,
2258
0
        bpi_exterior, pfx_mon);
2259
0
      agg_lock_node(rn_interior); /* sl entry */
2260
0
      if (!list_adopted) {
2261
0
        list_adopted = list_new();
2262
0
      }
2263
0
      listnode_add(list_adopted, bpi_exterior);
2264
2265
      /*
2266
       * Add constructed exterior routes based on the
2267
       * new interior route at the longer prefix.
2268
       */
2269
0
      if (bpi_interior->extra) {
2270
0
        prd = &bpi_interior->extra->vnc.import.rd;
2271
0
        label = decode_label(
2272
0
          &bpi_interior->extra->label[0]);
2273
0
      } else
2274
0
        prd = NULL;
2275
2276
      /* use local_pref from unicast route */
2277
0
      memset(&new_attr, 0, sizeof(struct attr));
2278
0
      new_attr = *bpi_interior->attr;
2279
0
      if (bpi_exterior
2280
0
          && (bpi_exterior->attr->flag
2281
0
        & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))) {
2282
0
        new_attr.local_pref =
2283
0
          bpi_exterior->attr->local_pref;
2284
0
        new_attr.flag |=
2285
0
          ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
2286
0
      }
2287
2288
0
      rfapiBgpInfoFilteredImportVPN(
2289
0
        it, FIF_ACTION_UPDATE, bpi_interior->peer,
2290
0
        NULL, /* rfd */
2291
0
        pfx_exterior, NULL, afi, prd, &new_attr,
2292
0
        ZEBRA_ROUTE_BGP_DIRECT_EXT,
2293
0
        BGP_ROUTE_REDISTRIBUTE, &label);
2294
0
    }
2295
0
  }
2296
0
  if (list_adopted) {
2297
0
    struct listnode *node;
2298
0
    struct agg_node *an_bpi_exterior;
2299
2300
0
    for (ALL_LIST_ELEMENTS_RO(list_adopted, node,
2301
0
            an_bpi_exterior)) {
2302
0
      skiplist_delete(it->monitor_exterior_orphans,
2303
0
          an_bpi_exterior, NULL);
2304
0
    }
2305
0
    list_delete(&list_adopted);
2306
0
  }
2307
0
}
2308
2309
/*
2310
 * This function should be called after an interior VPN route
2311
 * has been deleted from an import_table.
2312
 * bpi_interior must still be valid, but it must already be detached
2313
 * from its route node and the route node's valid_interior_count
2314
 * must already be decremented.
2315
 *
2316
 * NB should also be called whenever an existing vpn interior route
2317
 * becomes invalid (e.g., valid_interior_count is decremented)
2318
 */
2319
void vnc_import_bgp_exterior_del_route_interior(
2320
  struct bgp *bgp, struct rfapi_import_table *it,
2321
  struct agg_node *rn_interior,       /* VPN IT node */
2322
  struct bgp_path_info *bpi_interior) /* VPN IT route */
2323
0
{
2324
0
  const struct prefix *p = agg_node_get_prefix(rn_interior);
2325
0
  afi_t afi = family2afi(p->family);
2326
0
  struct agg_node *par;
2327
0
  struct bgp_path_info *bpi_exterior;
2328
0
  struct prefix *pfx_exterior; /* exterior pfx */
2329
0
  void *cursor;
2330
0
  int rc;
2331
2332
0
  if (!VALID_INTERIOR_TYPE(bpi_interior->type)) {
2333
0
    vnc_zlog_debug_verbose(
2334
0
      "%s: type %d not valid interior type, skipping",
2335
0
      __func__, bpi_interior->type);
2336
0
    return;
2337
0
  }
2338
2339
0
  if (!bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT_EXT]) {
2340
0
    vnc_zlog_debug_verbose(
2341
0
      "%s: redist of exterior routes no enabled, skipping",
2342
0
      __func__);
2343
0
    return;
2344
0
  }
2345
2346
0
  if (it == bgp->rfapi->it_ce) {
2347
0
    vnc_zlog_debug_verbose("%s: it is it_ce, skipping", __func__);
2348
0
    return;
2349
0
  }
2350
2351
  /* If no exterior routes depend on this prefix, nothing to do */
2352
0
  if (!RFAPI_HAS_MONITOR_EXTERIOR(rn_interior)) {
2353
0
    vnc_zlog_debug_verbose("%s: no exterior monitor, skipping",
2354
0
               __func__);
2355
0
    return;
2356
0
  }
2357
2358
  /*debugging */
2359
0
  vnc_zlog_debug_verbose("%s: interior prefix=%pRN, bpi type=%d",
2360
0
             __func__, rn_interior, bpi_interior->type);
2361
2362
  /*
2363
   * Remove constructed routes based on the deleted interior route
2364
   */
2365
0
  cursor = NULL;
2366
0
  for (rc = skiplist_next(RFAPI_MONITOR_EXTERIOR(rn_interior)->source,
2367
0
        (void **)&bpi_exterior, (void **)&pfx_exterior,
2368
0
        &cursor);
2369
0
       !rc;
2370
0
       rc = skiplist_next(RFAPI_MONITOR_EXTERIOR(rn_interior)->source,
2371
0
        (void **)&bpi_exterior, (void **)&pfx_exterior,
2372
0
        &cursor)) {
2373
2374
0
    struct prefix_rd *prd;
2375
0
    uint32_t label = 0;
2376
2377
0
    if (bpi_interior->extra) {
2378
0
      prd = &bpi_interior->extra->vnc.import.rd;
2379
0
      label = decode_label(&bpi_interior->extra->label[0]);
2380
0
    } else
2381
0
      prd = NULL;
2382
2383
0
    rfapiBgpInfoFilteredImportVPN(
2384
0
      it, FIF_ACTION_KILL, bpi_interior->peer, NULL, /* rfd */
2385
0
      pfx_exterior, NULL, afi, prd, bpi_interior->attr,
2386
0
      ZEBRA_ROUTE_BGP_DIRECT_EXT, BGP_ROUTE_REDISTRIBUTE,
2387
0
      &label);
2388
0
  }
2389
2390
  /*
2391
   * If there are no remaining valid interior routes at this prefix,
2392
   * we need to look up the tree for a possible node to move monitors to
2393
   */
2394
0
  if (RFAPI_MONITOR_EXTERIOR(rn_interior)->valid_interior_count) {
2395
0
    vnc_zlog_debug_verbose(
2396
0
      "%s: interior routes still present, skipping",
2397
0
      __func__);
2398
0
    return;
2399
0
  }
2400
2401
  /*
2402
   * Find nearest parent with at least one valid interior route
2403
   * If none is found, par will end up NULL, and we will move
2404
   * the monitors to the orphan list for this import table
2405
   */
2406
0
  for (par = agg_node_parent(rn_interior); par;
2407
0
       par = agg_node_parent(par)) {
2408
0
    if (RFAPI_MONITOR_EXTERIOR(par)->valid_interior_count)
2409
0
      break;
2410
0
  }
2411
2412
0
  vnc_zlog_debug_verbose("%s: par=%p, ext src: %p", __func__, par,
2413
0
             RFAPI_MONITOR_EXTERIOR(rn_interior)->source);
2414
2415
  /* move all monitors */
2416
  /*
2417
   * We will use and delete every element of the source skiplist
2418
   */
2419
0
  while (!skiplist_first(RFAPI_MONITOR_EXTERIOR(rn_interior)->source,
2420
0
             (void **)&bpi_exterior,
2421
0
             (void **)&pfx_exterior)) {
2422
2423
0
    struct prefix *pfx_mon = prefix_new();
2424
2425
0
    prefix_copy(pfx_mon, pfx_exterior);
2426
2427
0
    if (par) {
2428
2429
0
      struct bgp_path_info *bpi;
2430
2431
      /*
2432
       * Add monitor to parent node
2433
       */
2434
0
      if (!RFAPI_MONITOR_EXTERIOR(par)->source) {
2435
0
        RFAPI_MONITOR_EXTERIOR(par)->source =
2436
0
          skiplist_new(
2437
0
            0, NULL, prefix_free_lists);
2438
0
        agg_lock_node(par); /* sl */
2439
0
      }
2440
0
      skiplist_insert(RFAPI_MONITOR_EXTERIOR(par)->source,
2441
0
          bpi_exterior, pfx_mon);
2442
0
      agg_lock_node(par); /* sl entry */
2443
2444
      /* Add constructed exterior routes based on parent */
2445
0
      for (bpi = par->info; bpi; bpi = bpi->next) {
2446
2447
0
        struct prefix_rd *prd;
2448
0
        struct attr new_attr;
2449
0
        uint32_t label = 0;
2450
2451
0
        if (bpi->type == ZEBRA_ROUTE_BGP_DIRECT_EXT)
2452
0
          continue;
2453
2454
0
        if (bpi->extra) {
2455
0
          prd = &bpi->extra->vnc.import.rd;
2456
0
          label = decode_label(
2457
0
            &bpi->extra->label[0]);
2458
0
        } else
2459
0
          prd = NULL;
2460
2461
        /* use local_pref from unicast route */
2462
0
        memset(&new_attr, 0, sizeof(new_attr));
2463
0
        new_attr = *bpi->attr;
2464
0
        if (bpi_exterior
2465
0
            && (bpi_exterior->attr->flag
2466
0
          & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))) {
2467
0
          new_attr.local_pref =
2468
0
            bpi_exterior->attr->local_pref;
2469
0
          new_attr.flag |= ATTR_FLAG_BIT(
2470
0
            BGP_ATTR_LOCAL_PREF);
2471
0
        }
2472
2473
0
        rfapiBgpInfoFilteredImportVPN(
2474
0
          it, FIF_ACTION_UPDATE, bpi->peer,
2475
0
          NULL, /* rfd */
2476
0
          pfx_exterior, NULL, afi, prd, &new_attr,
2477
0
          ZEBRA_ROUTE_BGP_DIRECT_EXT,
2478
0
          BGP_ROUTE_REDISTRIBUTE, &label);
2479
0
      }
2480
2481
0
    } else {
2482
2483
      /*
2484
       * No interior route for exterior's nexthop. Save
2485
       * monitor
2486
       * in orphan list to await future route.
2487
       */
2488
0
      skiplist_insert(it->monitor_exterior_orphans,
2489
0
          bpi_exterior, pfx_mon);
2490
0
    }
2491
2492
0
    skiplist_delete_first(
2493
0
      RFAPI_MONITOR_EXTERIOR(rn_interior)->source);
2494
0
    agg_unlock_node(rn_interior); /* sl entry */
2495
0
  }
2496
0
  if (skiplist_empty(RFAPI_MONITOR_EXTERIOR(rn_interior)->source)) {
2497
0
    skiplist_free(RFAPI_MONITOR_EXTERIOR(rn_interior)->source);
2498
0
    RFAPI_MONITOR_EXTERIOR(rn_interior)->source = NULL;
2499
0
    agg_unlock_node(rn_interior); /* sl itself */
2500
0
  }
2501
0
}
2502
2503
/***********************************************************************
2504
 *      Generic add/delete unicast routes
2505
 ***********************************************************************/
2506
2507
void vnc_import_bgp_add_route(struct bgp *bgp, const struct prefix *prefix,
2508
            struct bgp_path_info *info)
2509
0
{
2510
0
  afi_t afi = family2afi(prefix->family);
2511
2512
0
  if (VNC_DEBUG(VERBOSE)) {
2513
0
    struct prefix pfx_nexthop;
2514
2515
0
    rfapiUnicastNexthop2Prefix(afi, info->attr, &pfx_nexthop);
2516
0
    vnc_zlog_debug_verbose("%s: pfx %pFX, nh %pFX", __func__,
2517
0
               prefix, &pfx_nexthop);
2518
0
  }
2519
#if DEBUG_RHN_LIST
2520
  print_rhn_list(__func__, "ENTER ");
2521
#endif
2522
0
  VNC_RHNCK(enter);
2523
2524
0
  if (!afi) {
2525
0
    flog_err(EC_LIB_DEVELOPMENT, "%s: can't get afi of prefix",
2526
0
       __func__);
2527
0
    return;
2528
0
  }
2529
2530
0
  if (!bgp->rfapi_cfg) {
2531
0
    vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
2532
0
               __func__);
2533
0
    return;
2534
0
  }
2535
2536
  /* check vnc redist flag for bgp direct routes */
2537
0
  if (!bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT]) {
2538
0
    vnc_zlog_debug_verbose(
2539
0
      "%s: bgp->rfapi_cfg->redist[afi=%d][type=%d=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
2540
0
      __func__, afi, ZEBRA_ROUTE_BGP_DIRECT);
2541
0
    return;
2542
0
  }
2543
2544
0
  switch (bgp->rfapi_cfg->redist_mode) {
2545
0
  case VNC_REDIST_MODE_PLAIN:
2546
0
    vnc_import_bgp_add_route_mode_plain(bgp, prefix, info);
2547
0
    break;
2548
2549
0
  case VNC_REDIST_MODE_RFG:
2550
0
    if (bgp->rfapi_cfg->rfg_redist)
2551
0
      vnc_import_bgp_add_route_mode_nvegroup(
2552
0
        bgp, prefix, info, bgp->rfapi_cfg->rfg_redist);
2553
0
    else
2554
0
      vnc_zlog_debug_verbose("%s: mode RFG but no redist RFG",
2555
0
                 __func__);
2556
0
    break;
2557
2558
0
  case VNC_REDIST_MODE_RESOLVE_NVE:
2559
0
    vnc_import_bgp_add_route_mode_resolve_nve(bgp, prefix, info);
2560
0
    break;
2561
0
  }
2562
#if DEBUG_RHN_LIST
2563
  print_rhn_list(__func__, "LEAVE ");
2564
#endif
2565
0
  VNC_RHNCK(leave);
2566
0
}
2567
2568
/*
2569
 * "Withdrawing a Route" import process
2570
 */
2571
void vnc_import_bgp_del_route(struct bgp *bgp, const struct prefix *prefix,
2572
            struct bgp_path_info *info) /* unicast info */
2573
0
{
2574
0
  afi_t afi = family2afi(prefix->family);
2575
2576
0
  assert(afi);
2577
2578
0
  {
2579
0
    struct prefix pfx_nexthop;
2580
2581
0
    rfapiUnicastNexthop2Prefix(afi, info->attr, &pfx_nexthop);
2582
0
    vnc_zlog_debug_verbose("%s: pfx %pFX, nh %pFX", __func__,
2583
0
               prefix, &pfx_nexthop);
2584
0
  }
2585
#if DEBUG_RHN_LIST
2586
  print_rhn_list(__func__, "ENTER ");
2587
#endif
2588
0
  VNC_RHNCK(enter);
2589
2590
0
  if (!bgp->rfapi_cfg) {
2591
0
    vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
2592
0
               __func__);
2593
0
    return;
2594
0
  }
2595
2596
  /* check bgp redist flag for vnc direct ("vpn") routes */
2597
0
  if (!bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT]) {
2598
0
    vnc_zlog_debug_verbose(
2599
0
      "%s: bgp redistribution of afi=%d VNC direct routes is off",
2600
0
      __func__, afi);
2601
0
    return;
2602
0
  }
2603
2604
0
  switch (bgp->rfapi_cfg->redist_mode) {
2605
0
  case VNC_REDIST_MODE_PLAIN:
2606
0
    vnc_import_bgp_del_route_mode_plain(bgp, prefix, info);
2607
0
    break;
2608
2609
0
  case VNC_REDIST_MODE_RFG:
2610
0
    if (bgp->rfapi_cfg->rfg_redist)
2611
0
      vnc_import_bgp_del_route_mode_nvegroup(bgp, prefix,
2612
0
                     info);
2613
0
    else
2614
0
      vnc_zlog_debug_verbose("%s: mode RFG but no redist RFG",
2615
0
                 __func__);
2616
0
    break;
2617
2618
0
  case VNC_REDIST_MODE_RESOLVE_NVE:
2619
0
    vnc_import_bgp_del_route_mode_resolve_nve(bgp, afi, prefix,
2620
0
                info);
2621
0
    break;
2622
0
  }
2623
#if DEBUG_RHN_LIST
2624
  print_rhn_list(__func__, "LEAVE ");
2625
#endif
2626
0
  VNC_RHNCK(leave);
2627
0
}
2628
2629
2630
/***********************************************************************
2631
 *      Enable/Disable
2632
 ***********************************************************************/
2633
2634
void vnc_import_bgp_redist_enable(struct bgp *bgp, afi_t afi)
2635
0
{
2636
  /* iterate over bgp unicast v4 and v6 routes, call
2637
   * vnc_import_bgp_add_route */
2638
2639
0
  struct bgp_dest *dest;
2640
2641
0
  vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__, afi);
2642
2643
0
  if (bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT]) {
2644
0
    vnc_zlog_debug_verbose(
2645
0
      "%s: already enabled for afi %d, skipping", __func__,
2646
0
      afi);
2647
0
    return;
2648
0
  }
2649
0
  bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT] = 1;
2650
2651
0
  for (dest = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); dest;
2652
0
       dest = bgp_route_next(dest)) {
2653
2654
0
    struct bgp_path_info *bpi;
2655
2656
0
    for (bpi = bgp_dest_get_bgp_path_info(dest); bpi;
2657
0
         bpi = bpi->next) {
2658
2659
0
      if (CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED))
2660
0
        continue;
2661
2662
0
      vnc_import_bgp_add_route(bgp, bgp_dest_get_prefix(dest),
2663
0
             bpi);
2664
0
    }
2665
0
  }
2666
0
  vnc_zlog_debug_verbose(
2667
0
    "%s: set redist[afi=%d][type=%d=ZEBRA_ROUTE_BGP_DIRECT] return",
2668
0
    __func__, afi, ZEBRA_ROUTE_BGP_DIRECT);
2669
0
}
2670
2671
void vnc_import_bgp_exterior_redist_enable(struct bgp *bgp, afi_t afi)
2672
0
{
2673
0
  struct bgp *bgp_exterior;
2674
0
  struct bgp_dest *dest;
2675
2676
0
  bgp_exterior = bgp->rfapi_cfg->redist_bgp_exterior_view;
2677
2678
0
  if (bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT_EXT]) {
2679
0
    vnc_zlog_debug_verbose(
2680
0
      "%s: already enabled for afi %d, skipping", __func__,
2681
0
      afi);
2682
0
    return;
2683
0
  }
2684
0
  bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT_EXT] = 1;
2685
2686
0
  if (!bgp_exterior) {
2687
0
    vnc_zlog_debug_verbose(
2688
0
      "%s: no exterior view set yet, no routes to import yet",
2689
0
      __func__);
2690
0
    return;
2691
0
  }
2692
2693
0
  for (dest = bgp_table_top(bgp_exterior->rib[afi][SAFI_UNICAST]); dest;
2694
0
       dest = bgp_route_next(dest)) {
2695
2696
0
    struct bgp_path_info *bpi;
2697
2698
0
    for (bpi = bgp_dest_get_bgp_path_info(dest); bpi;
2699
0
         bpi = bpi->next) {
2700
2701
0
      if (CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED))
2702
0
        continue;
2703
2704
0
      vnc_import_bgp_exterior_add_route(
2705
0
        bgp_exterior, bgp_dest_get_prefix(dest), bpi);
2706
0
    }
2707
0
  }
2708
0
  vnc_zlog_debug_verbose(
2709
0
    "%s: set redist[afi=%d][type=%d=ZEBRA_ROUTE_BGP_DIRECT] return",
2710
0
    __func__, afi, ZEBRA_ROUTE_BGP_DIRECT);
2711
0
}
2712
2713
/*
2714
 * This function is for populating a newly-created Import Table
2715
 */
2716
void vnc_import_bgp_exterior_redist_enable_it(
2717
  struct bgp *bgp, afi_t afi, struct rfapi_import_table *it_only)
2718
0
{
2719
0
  struct bgp *bgp_exterior;
2720
0
  struct bgp_dest *dest;
2721
2722
0
  vnc_zlog_debug_verbose("%s: entry", __func__);
2723
2724
0
  bgp_exterior = bgp->rfapi_cfg->redist_bgp_exterior_view;
2725
2726
0
  if (!bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT_EXT]) {
2727
0
    vnc_zlog_debug_verbose("%s: not enabled for afi %d, skipping",
2728
0
               __func__, afi);
2729
0
    return;
2730
0
  }
2731
2732
0
  if (!bgp_exterior) {
2733
0
    vnc_zlog_debug_verbose(
2734
0
      "%s: no exterior view set yet, no routes to import yet",
2735
0
      __func__);
2736
0
    return;
2737
0
  }
2738
2739
0
  for (dest = bgp_table_top(bgp_exterior->rib[afi][SAFI_UNICAST]); dest;
2740
0
       dest = bgp_route_next(dest)) {
2741
2742
0
    struct bgp_path_info *bpi;
2743
2744
0
    for (bpi = bgp_dest_get_bgp_path_info(dest); bpi;
2745
0
         bpi = bpi->next) {
2746
2747
0
      if (CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED))
2748
0
        continue;
2749
2750
0
      vnc_import_bgp_exterior_add_route_it(
2751
0
        bgp_exterior, bgp_dest_get_prefix(dest), bpi,
2752
0
        it_only);
2753
0
    }
2754
0
  }
2755
0
}
2756
2757
2758
void vnc_import_bgp_redist_disable(struct bgp *bgp, afi_t afi)
2759
0
{
2760
  /*
2761
   * iterate over vpn routes, find routes of type ZEBRA_ROUTE_BGP_DIRECT,
2762
   * delete (call timer expire immediately)
2763
   */
2764
0
  struct bgp_dest *dest1;
2765
0
  struct bgp_dest *dest2;
2766
2767
0
  vnc_zlog_debug_verbose("%s: entry", __func__);
2768
2769
0
  if (!bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT]) {
2770
0
    vnc_zlog_debug_verbose(
2771
0
      "%s: already disabled for afi %d, skipping", __func__,
2772
0
      afi);
2773
0
    return;
2774
0
  }
2775
2776
  /*
2777
   * Two-level table for SAFI_MPLS_VPN
2778
   * Be careful when changing the things we iterate over
2779
   */
2780
0
  for (dest1 = bgp_table_top(bgp->rib[afi][SAFI_MPLS_VPN]); dest1;
2781
0
       dest1 = bgp_route_next(dest1)) {
2782
0
    const struct prefix *dest1_p;
2783
2784
0
    if (!bgp_dest_has_bgp_path_info_data(dest1))
2785
0
      continue;
2786
2787
0
    dest1_p = bgp_dest_get_prefix(dest1);
2788
0
    for (dest2 = bgp_table_top(bgp_dest_get_bgp_table_info(dest1));
2789
0
         dest2; dest2 = bgp_route_next(dest2)) {
2790
0
      const struct prefix *dest2_p =
2791
0
        bgp_dest_get_prefix(dest2);
2792
0
      struct bgp_path_info *bpi;
2793
0
      struct bgp_path_info *nextbpi;
2794
2795
0
      for (bpi = bgp_dest_get_bgp_path_info(dest2); bpi;
2796
0
           bpi = nextbpi) {
2797
2798
0
        nextbpi = bpi->next;
2799
2800
0
        if (bpi->type != ZEBRA_ROUTE_BGP_DIRECT)
2801
0
          continue;
2802
2803
0
        struct rfapi_descriptor *rfd;
2804
0
        vncHDBgpDirect.peer = bpi->peer;
2805
2806
0
        assert(bpi->extra);
2807
2808
0
        rfd = bpi->extra->vnc.export.rfapi_handle;
2809
2810
0
        vnc_zlog_debug_verbose(
2811
0
          "%s: deleting bpi=%p, bpi->peer=%p, bpi->type=%d, bpi->sub_type=%d, bpi->extra->vnc.export.rfapi_handle=%p [passing rfd=%p]",
2812
0
          __func__, bpi, bpi->peer, bpi->type,
2813
0
          bpi->sub_type,
2814
0
          (bpi->extra ? bpi->extra->vnc.export
2815
0
                    .rfapi_handle
2816
0
                : NULL),
2817
0
          rfd);
2818
2819
0
        del_vnc_route(rfd, bpi->peer, bgp,
2820
0
                SAFI_MPLS_VPN, dest2_p,
2821
0
                (struct prefix_rd *)dest1_p,
2822
0
                bpi->type, bpi->sub_type, NULL,
2823
0
                1); /* kill */
2824
2825
0
        vncHDBgpDirect.peer = NULL;
2826
0
      }
2827
0
    }
2828
0
  }
2829
  /* Clear RHN list */
2830
0
  if (bgp->rfapi->resolve_nve_nexthop) {
2831
0
    struct prefix_bag *pb;
2832
0
    struct bgp_path_info *info;
2833
0
    while (!skiplist_first(bgp->rfapi->resolve_nve_nexthop, NULL,
2834
0
               (void *)&pb)) {
2835
0
      info = pb->ubpi;
2836
0
      skiplist_delete_first(bgp->rfapi->resolve_nve_nexthop);
2837
0
      bgp_path_info_unlock(info);
2838
0
    }
2839
0
  }
2840
2841
0
  bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT] = 0;
2842
0
  vnc_zlog_debug_verbose("%s: return", __func__);
2843
0
}
2844
2845
2846
void vnc_import_bgp_exterior_redist_disable(struct bgp *bgp, afi_t afi)
2847
0
{
2848
0
  struct rfapi_cfg *hc = bgp->rfapi_cfg;
2849
0
  struct bgp *bgp_exterior = hc->redist_bgp_exterior_view;
2850
2851
0
  vnc_zlog_debug_verbose("%s: entry", __func__);
2852
2853
0
  if (!hc->redist[afi][ZEBRA_ROUTE_BGP_DIRECT_EXT]) {
2854
0
    vnc_zlog_debug_verbose(
2855
0
      "%s: already disabled for afi %d, skipping", __func__,
2856
0
      afi);
2857
0
    return;
2858
0
  }
2859
2860
0
  if (!bgp_exterior) {
2861
0
    vnc_zlog_debug_verbose(
2862
0
      "%s: bgp exterior view not defined, skipping",
2863
0
      __func__);
2864
0
    return;
2865
0
  }
2866
2867
2868
0
  {
2869
0
    struct bgp_dest *dest;
2870
0
    for (dest = bgp_table_top(bgp_exterior->rib[afi][SAFI_UNICAST]);
2871
0
         dest; dest = bgp_route_next(dest)) {
2872
2873
0
      struct bgp_path_info *bpi;
2874
2875
0
      for (bpi = bgp_dest_get_bgp_path_info(dest); bpi;
2876
0
           bpi = bpi->next) {
2877
2878
0
        if (CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED))
2879
0
          continue;
2880
2881
0
        vnc_import_bgp_exterior_del_route(
2882
0
          bgp_exterior, bgp_dest_get_prefix(dest),
2883
0
          bpi);
2884
0
      }
2885
0
    }
2886
#if DEBUG_RHN_LIST
2887
    print_rhn_list(__func__, NULL);
2888
#endif
2889
0
  }
2890
2891
0
  bgp->rfapi_cfg->redist[afi][ZEBRA_ROUTE_BGP_DIRECT_EXT] = 0;
2892
0
  vnc_zlog_debug_verbose("%s: return", __func__);
2893
0
}