Coverage Report

Created: 2025-10-08 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/frr/bgpd/bgp_evpn.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/* Ethernet-VPN Packet and vty Processing File
3
 * Copyright (C) 2016 6WIND
4
 * Copyright (C) 2017 Cumulus Networks, Inc.
5
 */
6
7
#include <zebra.h>
8
9
#include "command.h"
10
#include "filter.h"
11
#include "prefix.h"
12
#include "log.h"
13
#include "memory.h"
14
#include "stream.h"
15
#include "hash.h"
16
#include "jhash.h"
17
#include "zclient.h"
18
19
#include "lib/printfrr.h"
20
21
#include "bgpd/bgp_attr_evpn.h"
22
#include "bgpd/bgpd.h"
23
#include "bgpd/bgp_table.h"
24
#include "bgpd/bgp_route.h"
25
#include "bgpd/bgp_attr.h"
26
#include "bgpd/bgp_mplsvpn.h"
27
#include "bgpd/bgp_label.h"
28
#include "bgpd/bgp_evpn.h"
29
#include "bgpd/bgp_evpn_private.h"
30
#include "bgpd/bgp_evpn_mh.h"
31
#include "bgpd/bgp_ecommunity.h"
32
#include "bgpd/bgp_encap_types.h"
33
#include "bgpd/bgp_debug.h"
34
#include "bgpd/bgp_errors.h"
35
#include "bgpd/bgp_aspath.h"
36
#include "bgpd/bgp_zebra.h"
37
#include "bgpd/bgp_nexthop.h"
38
#include "bgpd/bgp_addpath.h"
39
#include "bgpd/bgp_mac.h"
40
#include "bgpd/bgp_vty.h"
41
#include "bgpd/bgp_nht.h"
42
#include "bgpd/bgp_trace.h"
43
#include "bgpd/bgp_mpath.h"
44
45
/*
46
 * Definitions and external declarations.
47
 */
48
DEFINE_QOBJ_TYPE(bgpevpn);
49
DEFINE_QOBJ_TYPE(bgp_evpn_es);
50
51
2
DEFINE_MTYPE_STATIC(BGPD, VRF_ROUTE_TARGET, "L3 Route Target");
52
2
53
2
/*
54
2
 * Static function declarations
55
2
 */
56
2
static void bgp_evpn_remote_ip_hash_init(struct bgpevpn *evpn);
57
2
static void bgp_evpn_remote_ip_hash_destroy(struct bgpevpn *evpn);
58
2
static void bgp_evpn_remote_ip_hash_add(struct bgpevpn *vpn,
59
2
          struct bgp_path_info *pi);
60
2
static void bgp_evpn_remote_ip_hash_del(struct bgpevpn *vpn,
61
2
          struct bgp_path_info *pi);
62
2
static void bgp_evpn_remote_ip_hash_iterate(struct bgpevpn *vpn,
63
2
              void (*func)(struct hash_bucket *,
64
2
               void *),
65
2
              void *arg);
66
2
static void bgp_evpn_link_to_vni_svi_hash(struct bgp *bgp, struct bgpevpn *vpn);
67
2
static void bgp_evpn_unlink_from_vni_svi_hash(struct bgp *bgp,
68
2
                struct bgpevpn *vpn);
69
2
static unsigned int vni_svi_hash_key_make(const void *p);
70
2
static bool vni_svi_hash_cmp(const void *p1, const void *p2);
71
2
static void bgp_evpn_remote_ip_process_nexthops(struct bgpevpn *vpn,
72
2
            struct ipaddr *addr,
73
2
            bool resolve);
74
2
static void bgp_evpn_remote_ip_hash_link_nexthop(struct hash_bucket *bucket,
75
2
             void *args);
76
2
static void bgp_evpn_remote_ip_hash_unlink_nexthop(struct hash_bucket *bucket,
77
2
               void *args);
78
2
static struct in_addr zero_vtep_ip;
79
2
80
2
/*
81
2
 * Private functions.
82
2
 */
83
2
84
2
/*
85
2
 * Make vni hash key.
86
2
 */
87
2
static unsigned int vni_hash_key_make(const void *p)
88
2
{
89
0
  const struct bgpevpn *vpn = p;
90
0
  return (jhash_1word(vpn->vni, 0));
91
0
}
92
93
/*
94
 * Comparison function for vni hash
95
 */
96
static bool vni_hash_cmp(const void *p1, const void *p2)
97
0
{
98
0
  const struct bgpevpn *vpn1 = p1;
99
0
  const struct bgpevpn *vpn2 = p2;
100
101
0
  return vpn1->vni == vpn2->vni;
102
0
}
103
104
int vni_list_cmp(void *p1, void *p2)
105
0
{
106
0
  const struct bgpevpn *vpn1 = p1;
107
0
  const struct bgpevpn *vpn2 = p2;
108
109
0
  return vpn1->vni - vpn2->vni;
110
0
}
111
112
/*
113
 * Make vrf import route target hash key.
114
 */
115
static unsigned int vrf_import_rt_hash_key_make(const void *p)
116
0
{
117
0
  const struct vrf_irt_node *irt = p;
118
0
  const char *pnt = irt->rt.val;
119
120
0
  return jhash(pnt, 8, 0x5abc1234);
121
0
}
122
123
/*
124
 * Comparison function for vrf import rt hash
125
 */
126
static bool vrf_import_rt_hash_cmp(const void *p1, const void *p2)
127
0
{
128
0
  const struct vrf_irt_node *irt1 = p1;
129
0
  const struct vrf_irt_node *irt2 = p2;
130
131
0
  return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0);
132
0
}
133
134
/*
135
 * Create a new vrf import_rt in evpn instance
136
 */
137
static struct vrf_irt_node *vrf_import_rt_new(struct ecommunity_val *rt)
138
0
{
139
0
  struct bgp *bgp_evpn = NULL;
140
0
  struct vrf_irt_node *irt;
141
142
0
  bgp_evpn = bgp_get_evpn();
143
0
  if (!bgp_evpn) {
144
0
    flog_err(EC_BGP_NO_DFLT,
145
0
       "vrf import rt new - evpn instance not created yet");
146
0
    return NULL;
147
0
  }
148
149
0
  irt = XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT,
150
0
          sizeof(struct vrf_irt_node));
151
152
0
  irt->rt = *rt;
153
0
  irt->vrfs = list_new();
154
155
  /* Add to hash */
156
0
  (void)hash_get(bgp_evpn->vrf_import_rt_hash, irt, hash_alloc_intern);
157
158
0
  return irt;
159
0
}
160
161
/*
162
 * Free the vrf import rt node
163
 */
164
static void vrf_import_rt_free(struct vrf_irt_node *irt)
165
0
{
166
0
  struct bgp *bgp_evpn = NULL;
167
168
0
  bgp_evpn = bgp_get_evpn();
169
0
  if (!bgp_evpn) {
170
0
    flog_err(EC_BGP_NO_DFLT,
171
0
       "vrf import rt free - evpn instance not created yet");
172
0
    return;
173
0
  }
174
175
0
  hash_release(bgp_evpn->vrf_import_rt_hash, irt);
176
0
  list_delete(&irt->vrfs);
177
0
  XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT, irt);
178
0
}
179
180
static void hash_vrf_import_rt_free(struct vrf_irt_node *irt)
181
0
{
182
0
  XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT, irt);
183
0
}
184
185
/*
186
 * Function to lookup Import RT node - used to map a RT to set of
187
 * VNIs importing routes with that RT.
188
 */
189
static struct vrf_irt_node *lookup_vrf_import_rt(struct ecommunity_val *rt)
190
0
{
191
0
  struct bgp *bgp_evpn = NULL;
192
0
  struct vrf_irt_node *irt;
193
0
  struct vrf_irt_node tmp;
194
195
0
  bgp_evpn = bgp_get_evpn();
196
0
  if (!bgp_evpn) {
197
0
    flog_err(
198
0
      EC_BGP_NO_DFLT,
199
0
      "vrf import rt lookup - evpn instance not created yet");
200
0
    return NULL;
201
0
  }
202
203
0
  memset(&tmp, 0, sizeof(tmp));
204
0
  memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE);
205
0
  irt = hash_lookup(bgp_evpn->vrf_import_rt_hash, &tmp);
206
0
  return irt;
207
0
}
208
209
/*
210
 * Is specified VRF present on the RT's list of "importing" VRFs?
211
 */
212
static int is_vrf_present_in_irt_vrfs(struct list *vrfs, struct bgp *bgp_vrf)
213
0
{
214
0
  struct listnode *node = NULL, *nnode = NULL;
215
0
  struct bgp *tmp_bgp_vrf = NULL;
216
217
0
  for (ALL_LIST_ELEMENTS(vrfs, node, nnode, tmp_bgp_vrf)) {
218
0
    if (tmp_bgp_vrf == bgp_vrf)
219
0
      return 1;
220
0
  }
221
0
  return 0;
222
0
}
223
224
/*
225
 * Make import route target hash key.
226
 */
227
static unsigned int import_rt_hash_key_make(const void *p)
228
0
{
229
0
  const struct irt_node *irt = p;
230
0
  const char *pnt = irt->rt.val;
231
232
0
  return jhash(pnt, 8, 0xdeadbeef);
233
0
}
234
235
/*
236
 * Comparison function for import rt hash
237
 */
238
static bool import_rt_hash_cmp(const void *p1, const void *p2)
239
0
{
240
0
  const struct irt_node *irt1 = p1;
241
0
  const struct irt_node *irt2 = p2;
242
243
0
  return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0);
244
0
}
245
246
/*
247
 * Create a new import_rt
248
 */
249
static struct irt_node *import_rt_new(struct bgp *bgp,
250
              struct ecommunity_val *rt)
251
0
{
252
0
  struct irt_node *irt;
253
254
0
  irt = XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT, sizeof(struct irt_node));
255
256
0
  irt->rt = *rt;
257
0
  irt->vnis = list_new();
258
259
  /* Add to hash */
260
0
  (void)hash_get(bgp->import_rt_hash, irt, hash_alloc_intern);
261
262
0
  return irt;
263
0
}
264
265
/*
266
 * Free the import rt node
267
 */
268
static void import_rt_free(struct bgp *bgp, struct irt_node *irt)
269
0
{
270
0
  hash_release(bgp->import_rt_hash, irt);
271
0
  list_delete(&irt->vnis);
272
0
  XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
273
0
}
274
275
static void hash_import_rt_free(struct irt_node *irt)
276
0
{
277
0
  XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
278
0
}
279
280
/*
281
 * Function to lookup Import RT node - used to map a RT to set of
282
 * VNIs importing routes with that RT.
283
 */
284
static struct irt_node *lookup_import_rt(struct bgp *bgp,
285
           struct ecommunity_val *rt)
286
0
{
287
0
  struct irt_node *irt;
288
0
  struct irt_node tmp;
289
290
0
  memset(&tmp, 0, sizeof(tmp));
291
0
  memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE);
292
0
  irt = hash_lookup(bgp->import_rt_hash, &tmp);
293
0
  return irt;
294
0
}
295
296
/*
297
 * Is specified VNI present on the RT's list of "importing" VNIs?
298
 */
299
static int is_vni_present_in_irt_vnis(struct list *vnis, struct bgpevpn *vpn)
300
0
{
301
0
  struct listnode *node, *nnode;
302
0
  struct bgpevpn *tmp_vpn;
303
304
0
  for (ALL_LIST_ELEMENTS(vnis, node, nnode, tmp_vpn)) {
305
0
    if (tmp_vpn == vpn)
306
0
      return 1;
307
0
  }
308
309
0
  return 0;
310
0
}
311
312
/*
313
 * Compare Route Targets.
314
 */
315
int bgp_evpn_route_target_cmp(struct ecommunity *ecom1,
316
            struct ecommunity *ecom2)
317
0
{
318
0
  if (ecom1 && !ecom2)
319
0
    return -1;
320
321
0
  if (!ecom1 && ecom2)
322
0
    return 1;
323
324
0
  if (!ecom1 && !ecom2)
325
0
    return 0;
326
327
0
  if (ecom1->str && !ecom2->str)
328
0
    return -1;
329
330
0
  if (!ecom1->str && ecom2->str)
331
0
    return 1;
332
333
0
  if (!ecom1->str && !ecom2->str)
334
0
    return 0;
335
336
0
  return strcmp(ecom1->str, ecom2->str);
337
0
}
338
339
/*
340
 * Compare L3 Route Targets.
341
 */
342
static int evpn_vrf_route_target_cmp(struct vrf_route_target *rt1,
343
             struct vrf_route_target *rt2)
344
0
{
345
0
  return bgp_evpn_route_target_cmp(rt1->ecom, rt2->ecom);
346
0
}
347
348
void bgp_evpn_xxport_delete_ecomm(void *val)
349
0
{
350
0
  struct ecommunity *ecomm = val;
351
0
  ecommunity_free(&ecomm);
352
0
}
353
354
/*
355
 * Delete l3 Route Target.
356
 */
357
static void evpn_vrf_rt_del(void *val)
358
0
{
359
0
  struct vrf_route_target *l3rt = val;
360
361
0
  ecommunity_free(&l3rt->ecom);
362
363
0
  XFREE(MTYPE_VRF_ROUTE_TARGET, l3rt);
364
0
}
365
366
/*
367
 * Allocate a new l3 Route Target.
368
 */
369
static struct vrf_route_target *evpn_vrf_rt_new(struct ecommunity *ecom)
370
0
{
371
0
  struct vrf_route_target *l3rt;
372
373
0
  l3rt = XCALLOC(MTYPE_VRF_ROUTE_TARGET, sizeof(struct vrf_route_target));
374
375
0
  l3rt->ecom = ecom;
376
377
0
  return l3rt;
378
0
}
379
380
/*
381
 * Mask off global-admin field of specified extended community (RT),
382
 * just retain the local-admin field.
383
 */
384
static inline void mask_ecom_global_admin(struct ecommunity_val *dst,
385
            const struct ecommunity_val *src)
386
0
{
387
0
  uint8_t type;
388
389
0
  type = src->val[0];
390
0
  dst->val[0] = 0;
391
0
  if (type == ECOMMUNITY_ENCODE_AS) {
392
0
    dst->val[2] = dst->val[3] = 0;
393
0
  } else if (type == ECOMMUNITY_ENCODE_AS4
394
0
       || type == ECOMMUNITY_ENCODE_IP) {
395
0
    dst->val[2] = dst->val[3] = 0;
396
0
    dst->val[4] = dst->val[5] = 0;
397
0
  }
398
0
}
399
400
/*
401
 * Converts the RT to Ecommunity Value and adjusts masking based
402
 * on flags set for RT.
403
 */
404
static void vrf_rt2ecom_val(struct ecommunity_val *to_eval,
405
          const struct vrf_route_target *l3rt, int iter)
406
0
{
407
0
  const struct ecommunity_val *eval;
408
409
0
  eval = (const struct ecommunity_val *)(l3rt->ecom->val +
410
0
                 (iter * ECOMMUNITY_SIZE));
411
  /* If using "automatic" or "wildcard *" RT,
412
   * we only care about the local-admin sub-field.
413
   * This is to facilitate using L3VNI(VRF-VNI)
414
   * as the RT for EBGP peering too and simplify
415
   * configurations by allowing any ASN via '*'.
416
   */
417
0
  memcpy(to_eval, eval, ECOMMUNITY_SIZE);
418
419
0
  if (CHECK_FLAG(l3rt->flags, BGP_VRF_RT_AUTO) ||
420
0
      CHECK_FLAG(l3rt->flags, BGP_VRF_RT_WILD))
421
0
    mask_ecom_global_admin(to_eval, eval);
422
0
}
423
424
/*
425
 * Map one RT to specified VRF.
426
 * bgp_vrf = BGP vrf instance
427
 */
428
static void map_vrf_to_rt(struct bgp *bgp_vrf, struct vrf_route_target *l3rt)
429
0
{
430
0
  uint32_t i = 0;
431
432
0
  for (i = 0; i < l3rt->ecom->size; i++) {
433
0
    struct vrf_irt_node *irt = NULL;
434
0
    struct ecommunity_val eval_tmp;
435
436
    /* Adjust masking for value */
437
0
    vrf_rt2ecom_val(&eval_tmp, l3rt, i);
438
439
0
    irt = lookup_vrf_import_rt(&eval_tmp);
440
441
0
    if (irt && is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
442
0
      return; /* Already mapped. */
443
444
0
    if (!irt)
445
0
      irt = vrf_import_rt_new(&eval_tmp);
446
447
    /* Add VRF to the list for this RT. */
448
0
    listnode_add(irt->vrfs, bgp_vrf);
449
0
  }
450
0
}
451
452
/*
453
 * Unmap specified VRF from specified RT. If there are no other
454
 * VRFs for this RT, then the RT hash is deleted.
455
 * bgp_vrf: BGP VRF specific instance
456
 */
457
static void unmap_vrf_from_rt(struct bgp *bgp_vrf,
458
            struct vrf_route_target *l3rt)
459
0
{
460
0
  uint32_t i;
461
462
0
  for (i = 0; i < l3rt->ecom->size; i++) {
463
0
    struct vrf_irt_node *irt;
464
0
    struct ecommunity_val eval_tmp;
465
466
    /* Adjust masking for value */
467
0
    vrf_rt2ecom_val(&eval_tmp, l3rt, i);
468
469
0
    irt = lookup_vrf_import_rt(&eval_tmp);
470
471
0
    if (!irt)
472
0
      return; /* Not mapped */
473
474
    /* Delete VRF from list for this RT. */
475
0
    listnode_delete(irt->vrfs, bgp_vrf);
476
477
0
    if (!listnode_head(irt->vrfs))
478
0
      vrf_import_rt_free(irt);
479
0
  }
480
0
}
481
482
/*
483
 * Map one RT to specified VNI.
484
 */
485
static void map_vni_to_rt(struct bgp *bgp, struct bgpevpn *vpn,
486
        struct ecommunity_val *eval)
487
0
{
488
0
  struct irt_node *irt;
489
0
  struct ecommunity_val eval_tmp;
490
491
  /* If using "automatic" RT, we only care about the local-admin
492
   * sub-field.
493
   * This is to facilitate using VNI as the RT for EBGP peering too.
494
   */
495
0
  memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
496
0
  if (!is_import_rt_configured(vpn))
497
0
    mask_ecom_global_admin(&eval_tmp, eval);
498
499
0
  irt = lookup_import_rt(bgp, &eval_tmp);
500
0
  if (irt)
501
0
    if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
502
      /* Already mapped. */
503
0
      return;
504
505
0
  if (!irt)
506
0
    irt = import_rt_new(bgp, &eval_tmp);
507
508
  /* Add VNI to the hash list for this RT. */
509
0
  listnode_add(irt->vnis, vpn);
510
0
}
511
512
/*
513
 * Unmap specified VNI from specified RT. If there are no other
514
 * VNIs for this RT, then the RT hash is deleted.
515
 */
516
static void unmap_vni_from_rt(struct bgp *bgp, struct bgpevpn *vpn,
517
            struct irt_node *irt)
518
0
{
519
  /* Delete VNI from hash list for this RT. */
520
0
  listnode_delete(irt->vnis, vpn);
521
0
  if (!listnode_head(irt->vnis)) {
522
0
    import_rt_free(bgp, irt);
523
0
  }
524
0
}
525
526
static void bgp_evpn_get_rmac_nexthop(struct bgpevpn *vpn,
527
              const struct prefix_evpn *p,
528
              struct attr *attr, uint8_t flags)
529
0
{
530
0
  struct bgp *bgp_vrf = vpn->bgp_vrf;
531
532
0
  memset(&attr->rmac, 0, sizeof(struct ethaddr));
533
0
  if (!bgp_vrf)
534
0
    return;
535
536
0
  if (p->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
537
0
    return;
538
539
  /* Copy sys (pip) RMAC and PIP IP as nexthop
540
   * in case of route is self MAC-IP,
541
   * advertise-pip and advertise-svi-ip features
542
   * are enabled.
543
   * Otherwise, for all host MAC-IP route's
544
   * copy anycast RMAC.
545
   */
546
0
  if (CHECK_FLAG(flags, BGP_EVPN_MACIP_TYPE_SVI_IP)
547
0
      && bgp_vrf->evpn_info->advertise_pip &&
548
0
      bgp_vrf->evpn_info->is_anycast_mac) {
549
    /* copy sys rmac */
550
0
    memcpy(&attr->rmac, &bgp_vrf->evpn_info->pip_rmac,
551
0
           ETH_ALEN);
552
0
    attr->nexthop = bgp_vrf->evpn_info->pip_ip;
553
0
    attr->mp_nexthop_global_in =
554
0
      bgp_vrf->evpn_info->pip_ip;
555
0
  } else
556
0
    memcpy(&attr->rmac, &bgp_vrf->rmac, ETH_ALEN);
557
0
}
558
559
/*
560
 * Create RT extended community automatically from passed information:
561
 * of the form AS:VNI.
562
 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
563
 * the need is to get a RT value that will be unique across different
564
 * VNIs but the same across routers (in the same AS) for a particular
565
 * VNI.
566
 */
567
static void form_auto_rt(struct bgp *bgp, vni_t vni, struct list *rtl,
568
       bool is_l3)
569
0
{
570
0
  struct ecommunity_val eval;
571
0
  struct ecommunity *ecomadd;
572
0
  struct ecommunity *ecom;
573
0
  struct vrf_route_target *l3rt;
574
0
  struct vrf_route_target *newrt;
575
0
  bool ecom_found = false;
576
0
  struct listnode *node;
577
578
0
  if (bgp->advertise_autort_rfc8365)
579
0
    vni |= EVPN_AUTORT_VXLAN;
580
0
  encode_route_target_as((bgp->as & 0xFFFF), vni, &eval, true);
581
582
0
  ecomadd = ecommunity_new();
583
0
  ecommunity_add_val(ecomadd, &eval, false, false);
584
585
0
  if (is_l3) {
586
0
    for (ALL_LIST_ELEMENTS_RO(rtl, node, l3rt))
587
0
      if (ecommunity_cmp(ecomadd, l3rt->ecom)) {
588
0
        ecom_found = true;
589
0
        break;
590
0
      }
591
0
  } else {
592
0
    for (ALL_LIST_ELEMENTS_RO(rtl, node, ecom))
593
0
      if (ecommunity_cmp(ecomadd, ecom)) {
594
0
        ecom_found = true;
595
0
        break;
596
0
      }
597
0
  }
598
599
0
  if (!ecom_found) {
600
0
    if (is_l3) {
601
0
      newrt = evpn_vrf_rt_new(ecomadd);
602
      /* Label it as autoderived */
603
0
      SET_FLAG(newrt->flags, BGP_VRF_RT_AUTO);
604
0
      listnode_add_sort(rtl, newrt);
605
0
    } else
606
0
      listnode_add_sort(rtl, ecomadd);
607
0
  } else
608
0
    ecommunity_free(&ecomadd);
609
0
}
610
611
/*
612
 * Derive RD and RT for a VNI automatically. Invoked at the time of
613
 * creation of a VNI.
614
 */
615
static void derive_rd_rt_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
616
0
{
617
0
  bgp_evpn_derive_auto_rd(bgp, vpn);
618
0
  bgp_evpn_derive_auto_rt_import(bgp, vpn);
619
0
  bgp_evpn_derive_auto_rt_export(bgp, vpn);
620
0
}
621
622
/*
623
 * Convert nexthop (remote VTEP IP) into an IPv6 address.
624
 */
625
static void evpn_convert_nexthop_to_ipv6(struct attr *attr)
626
0
{
627
0
  if (BGP_ATTR_NEXTHOP_AFI_IP6(attr))
628
0
    return;
629
0
  ipv4_to_ipv4_mapped_ipv6(&attr->mp_nexthop_global, attr->nexthop);
630
0
  attr->mp_nexthop_len = IPV6_MAX_BYTELEN;
631
0
}
632
633
/*
634
 * Wrapper for node get in global table.
635
 */
636
struct bgp_dest *bgp_evpn_global_node_get(struct bgp_table *table, afi_t afi,
637
            safi_t safi,
638
            const struct prefix_evpn *evp,
639
            struct prefix_rd *prd,
640
            const struct bgp_path_info *local_pi)
641
0
{
642
0
  struct prefix_evpn global_p;
643
644
0
  if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE) {
645
    /* prefix in the global table doesn't include the VTEP-IP so
646
     * we need to create a different copy of the prefix
647
     */
648
0
    evpn_type1_prefix_global_copy(&global_p, evp);
649
0
    evp = &global_p;
650
0
  } else if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE &&
651
0
       local_pi) {
652
    /*
653
     * prefix in the global table needs MAC/IP, ensure they are
654
     * present, using one's from local table's path_info.
655
     */
656
0
    if (is_evpn_prefix_ipaddr_none(evp)) {
657
      /* VNI MAC -> Global */
658
0
      evpn_type2_prefix_global_copy(
659
0
        &global_p, evp, NULL /* mac */,
660
0
        evpn_type2_path_info_get_ip(local_pi));
661
0
    } else {
662
      /* VNI IP -> Global */
663
0
      evpn_type2_prefix_global_copy(
664
0
        &global_p, evp,
665
0
        evpn_type2_path_info_get_mac(local_pi),
666
0
        NULL /* ip */);
667
0
    }
668
669
0
    evp = &global_p;
670
0
  }
671
0
  return bgp_afi_node_get(table, afi, safi, (struct prefix *)evp, prd);
672
0
}
673
674
/*
675
 * Wrapper for node lookup in global table.
676
 */
677
struct bgp_dest *bgp_evpn_global_node_lookup(
678
  struct bgp_table *table, safi_t safi, const struct prefix_evpn *evp,
679
  struct prefix_rd *prd, const struct bgp_path_info *local_pi)
680
0
{
681
0
  struct prefix_evpn global_p;
682
683
0
  if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE) {
684
    /* prefix in the global table doesn't include the VTEP-IP so
685
     * we need to create a different copy of the prefix
686
     */
687
0
    evpn_type1_prefix_global_copy(&global_p, evp);
688
0
    evp = &global_p;
689
0
  } else if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE &&
690
0
       local_pi) {
691
    /*
692
     * prefix in the global table needs MAC/IP, ensure they are
693
     * present, using one's from local table's path_info.
694
     */
695
0
    if (is_evpn_prefix_ipaddr_none(evp)) {
696
      /* VNI MAC -> Global */
697
0
      evpn_type2_prefix_global_copy(
698
0
        &global_p, evp, NULL /* mac */,
699
0
        evpn_type2_path_info_get_ip(local_pi));
700
0
    } else {
701
      /* VNI IP -> Global */
702
0
      evpn_type2_prefix_global_copy(
703
0
        &global_p, evp,
704
0
        evpn_type2_path_info_get_mac(local_pi),
705
0
        NULL /* ip */);
706
0
    }
707
708
0
    evp = &global_p;
709
0
  }
710
0
  return bgp_safi_node_lookup(table, safi, (struct prefix *)evp, prd);
711
0
}
712
713
/*
714
 * Wrapper for node get in VNI IP table.
715
 */
716
struct bgp_dest *bgp_evpn_vni_ip_node_get(struct bgp_table *const table,
717
            const struct prefix_evpn *evp,
718
            const struct bgp_path_info *parent_pi)
719
0
{
720
0
  struct prefix_evpn vni_p;
721
722
0
  if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE && parent_pi) {
723
    /* prefix in the global table doesn't include the VTEP-IP so
724
     * we need to create a different copy for the VNI
725
     */
726
0
    evpn_type1_prefix_vni_ip_copy(&vni_p, evp,
727
0
                parent_pi->attr->nexthop);
728
0
    evp = &vni_p;
729
0
  } else if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
730
    /* Only MAC-IP should go into this table, not mac-only */
731
0
    assert(is_evpn_prefix_ipaddr_none(evp) == false);
732
733
    /*
734
     * prefix in the vni IP table doesn't include MAC so
735
     * we need to create a different copy of the prefix.
736
     */
737
0
    evpn_type2_prefix_vni_ip_copy(&vni_p, evp);
738
0
    evp = &vni_p;
739
0
  }
740
0
  return bgp_node_get(table, (struct prefix *)evp);
741
0
}
742
743
/*
744
 * Wrapper for node lookup in VNI IP table.
745
 */
746
struct bgp_dest *
747
bgp_evpn_vni_ip_node_lookup(const struct bgp_table *const table,
748
          const struct prefix_evpn *evp,
749
          const struct bgp_path_info *parent_pi)
750
0
{
751
0
  struct prefix_evpn vni_p;
752
753
0
  if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE && parent_pi) {
754
    /* prefix in the global table doesn't include the VTEP-IP so
755
     * we need to create a different copy for the VNI
756
     */
757
0
    evpn_type1_prefix_vni_ip_copy(&vni_p, evp,
758
0
                parent_pi->attr->nexthop);
759
0
    evp = &vni_p;
760
0
  } else if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
761
    /* Only MAC-IP should go into this table, not mac-only */
762
0
    assert(is_evpn_prefix_ipaddr_none(evp) == false);
763
764
    /*
765
     * prefix in the vni IP table doesn't include MAC so
766
     * we need to create a different copy of the prefix.
767
     */
768
0
    evpn_type2_prefix_vni_ip_copy(&vni_p, evp);
769
0
    evp = &vni_p;
770
0
  }
771
0
  return bgp_node_lookup(table, (struct prefix *)evp);
772
0
}
773
774
/*
775
 * Wrapper for node get in VNI MAC table.
776
 */
777
struct bgp_dest *
778
bgp_evpn_vni_mac_node_get(struct bgp_table *const table,
779
        const struct prefix_evpn *evp,
780
        const struct bgp_path_info *parent_pi)
781
0
{
782
0
  struct prefix_evpn vni_p;
783
784
  /* Only type-2 should ever go into this table */
785
0
  assert(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE);
786
787
  /*
788
   * prefix in the vni MAC table doesn't include IP so
789
   * we need to create a different copy of the prefix.
790
   */
791
0
  evpn_type2_prefix_vni_mac_copy(&vni_p, evp);
792
0
  evp = &vni_p;
793
0
  return bgp_node_get(table, (struct prefix *)evp);
794
0
}
795
796
/*
797
 * Wrapper for node lookup in VNI MAC table.
798
 */
799
struct bgp_dest *
800
bgp_evpn_vni_mac_node_lookup(const struct bgp_table *const table,
801
           const struct prefix_evpn *evp,
802
           const struct bgp_path_info *parent_pi)
803
0
{
804
0
  struct prefix_evpn vni_p;
805
806
  /* Only type-2 should ever go into this table */
807
0
  assert(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE);
808
809
  /*
810
   * prefix in the vni MAC table doesn't include IP so
811
   * we need to create a different copy of the prefix.
812
   */
813
0
  evpn_type2_prefix_vni_mac_copy(&vni_p, evp);
814
0
  evp = &vni_p;
815
0
  return bgp_node_lookup(table, (struct prefix *)evp);
816
0
}
817
818
/*
819
 * Wrapper for node get in both VNI tables.
820
 */
821
struct bgp_dest *bgp_evpn_vni_node_get(struct bgpevpn *vpn,
822
               const struct prefix_evpn *p,
823
               const struct bgp_path_info *parent_pi)
824
0
{
825
0
  if ((p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) &&
826
0
      (is_evpn_prefix_ipaddr_none(p) == true))
827
0
    return bgp_evpn_vni_mac_node_get(vpn->mac_table, p, parent_pi);
828
829
0
  return bgp_evpn_vni_ip_node_get(vpn->ip_table, p, parent_pi);
830
0
}
831
832
/*
833
 * Wrapper for node lookup in both VNI tables.
834
 */
835
struct bgp_dest *bgp_evpn_vni_node_lookup(const struct bgpevpn *vpn,
836
            const struct prefix_evpn *p,
837
            const struct bgp_path_info *parent_pi)
838
0
{
839
0
  if ((p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) &&
840
0
      (is_evpn_prefix_ipaddr_none(p) == true))
841
0
    return bgp_evpn_vni_mac_node_lookup(vpn->mac_table, p,
842
0
                parent_pi);
843
844
0
  return bgp_evpn_vni_ip_node_lookup(vpn->ip_table, p, parent_pi);
845
0
}
846
847
/*
848
 * Add (update) or delete MACIP from zebra.
849
 */
850
static int bgp_zebra_send_remote_macip(struct bgp *bgp, struct bgpevpn *vpn,
851
               const struct prefix_evpn *p,
852
               const struct ethaddr *mac,
853
               struct in_addr remote_vtep_ip, int add,
854
               uint8_t flags, uint32_t seq, esi_t *esi)
855
0
{
856
0
  struct stream *s;
857
0
  uint16_t ipa_len;
858
0
  static struct in_addr zero_remote_vtep_ip;
859
0
  bool esi_valid;
860
861
  /* Check socket. */
862
0
  if (!zclient || zclient->sock < 0)
863
0
    return 0;
864
865
  /* Don't try to register if Zebra doesn't know of this instance. */
866
0
  if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
867
0
    if (BGP_DEBUG(zebra, ZEBRA))
868
0
      zlog_debug(
869
0
        "%s: No zebra instance to talk to, not installing remote macip",
870
0
        __func__);
871
0
    return 0;
872
0
  }
873
874
0
  if (!esi)
875
0
    esi = zero_esi;
876
0
  s = zclient->obuf;
877
0
  stream_reset(s);
878
879
0
  zclient_create_header(
880
0
    s, add ? ZEBRA_REMOTE_MACIP_ADD : ZEBRA_REMOTE_MACIP_DEL,
881
0
    bgp->vrf_id);
882
0
  stream_putl(s, vpn->vni);
883
884
0
  if (mac) /* Mac Addr */
885
0
    stream_put(s, &mac->octet, ETH_ALEN);
886
0
  else
887
0
    stream_put(s, &p->prefix.macip_addr.mac.octet, ETH_ALEN);
888
889
  /* IP address length and IP address, if any. */
890
0
  if (is_evpn_prefix_ipaddr_none(p))
891
0
    stream_putw(s, 0);
892
0
  else {
893
0
    ipa_len = is_evpn_prefix_ipaddr_v4(p) ? IPV4_MAX_BYTELEN
894
0
                  : IPV6_MAX_BYTELEN;
895
0
    stream_putw(s, ipa_len);
896
0
    stream_put(s, &p->prefix.macip_addr.ip.ip.addr, ipa_len);
897
0
  }
898
  /* If the ESI is valid that becomes the nexthop; tape out the
899
   * VTEP-IP for that case
900
   */
901
0
  if (bgp_evpn_is_esi_valid(esi)) {
902
0
    esi_valid = true;
903
0
    stream_put_in_addr(s, &zero_remote_vtep_ip);
904
0
  } else {
905
0
    esi_valid = false;
906
0
    stream_put_in_addr(s, &remote_vtep_ip);
907
0
  }
908
909
  /* TX flags - MAC sticky status and/or gateway mac */
910
  /* Also TX the sequence number of the best route. */
911
0
  if (add) {
912
0
    stream_putc(s, flags);
913
0
    stream_putl(s, seq);
914
0
    stream_put(s, esi, sizeof(esi_t));
915
0
  }
916
917
0
  stream_putw_at(s, 0, stream_get_endp(s));
918
919
0
  if (bgp_debug_zebra(NULL)) {
920
0
    char esi_buf[ESI_STR_LEN];
921
922
0
    if (esi_valid)
923
0
      esi_to_str(esi, esi_buf, sizeof(esi_buf));
924
0
    else
925
0
      snprintf(esi_buf, sizeof(esi_buf), "-");
926
0
    zlog_debug(
927
0
      "Tx %s MACIP, VNI %u MAC %pEA IP %pIA flags 0x%x seq %u remote VTEP %pI4 esi %s",
928
0
      add ? "ADD" : "DEL", vpn->vni,
929
0
      (mac ? mac : &p->prefix.macip_addr.mac),
930
0
      &p->prefix.macip_addr.ip, flags, seq, &remote_vtep_ip,
931
0
      esi_buf);
932
0
  }
933
934
0
  frrtrace(5, frr_bgp, evpn_mac_ip_zsend, add, vpn, p, remote_vtep_ip,
935
0
     esi);
936
937
0
  return zclient_send_message(zclient);
938
0
}
939
940
/*
941
 * Add (update) or delete remote VTEP from zebra.
942
 */
943
static int bgp_zebra_send_remote_vtep(struct bgp *bgp, struct bgpevpn *vpn,
944
              const struct prefix_evpn *p,
945
              int flood_control, int add)
946
0
{
947
0
  struct stream *s;
948
949
  /* Check socket. */
950
0
  if (!zclient || zclient->sock < 0)
951
0
    return 0;
952
953
  /* Don't try to register if Zebra doesn't know of this instance. */
954
0
  if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
955
0
    if (BGP_DEBUG(zebra, ZEBRA))
956
0
      zlog_debug(
957
0
        "%s: No zebra instance to talk to, not installing remote vtep",
958
0
        __func__);
959
0
    return 0;
960
0
  }
961
962
0
  s = zclient->obuf;
963
0
  stream_reset(s);
964
965
0
  zclient_create_header(
966
0
    s, add ? ZEBRA_REMOTE_VTEP_ADD : ZEBRA_REMOTE_VTEP_DEL,
967
0
    bgp->vrf_id);
968
0
  stream_putl(s, vpn->vni);
969
0
  if (is_evpn_prefix_ipaddr_v4(p))
970
0
    stream_put_in_addr(s, &p->prefix.imet_addr.ip.ipaddr_v4);
971
0
  else if (is_evpn_prefix_ipaddr_v6(p)) {
972
0
    flog_err(
973
0
      EC_BGP_VTEP_INVALID,
974
0
      "Bad remote IP when trying to %s remote VTEP for VNI %u",
975
0
      add ? "ADD" : "DEL", vpn->vni);
976
0
    return -1;
977
0
  }
978
0
  stream_putl(s, flood_control);
979
980
0
  stream_putw_at(s, 0, stream_get_endp(s));
981
982
0
  if (bgp_debug_zebra(NULL))
983
0
    zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %pI4",
984
0
         add ? "ADD" : "DEL", vpn->vni,
985
0
         &p->prefix.imet_addr.ip.ipaddr_v4);
986
987
0
  frrtrace(3, frr_bgp, evpn_bum_vtep_zsend, add, vpn, p);
988
989
0
  return zclient_send_message(zclient);
990
0
}
991
992
/*
993
 * Build extended communities for EVPN prefix route.
994
 */
995
static void build_evpn_type5_route_extcomm(struct bgp *bgp_vrf,
996
             struct attr *attr)
997
0
{
998
0
  struct ecommunity ecom_encap;
999
0
  struct ecommunity_val eval;
1000
0
  struct ecommunity_val eval_rmac;
1001
0
  bgp_encap_types tnl_type;
1002
0
  struct listnode *node, *nnode;
1003
0
  struct vrf_route_target *l3rt;
1004
0
  struct ecommunity *old_ecom;
1005
0
  struct ecommunity *ecom;
1006
0
  struct list *vrf_export_rtl = NULL;
1007
1008
  /* Encap */
1009
0
  tnl_type = BGP_ENCAP_TYPE_VXLAN;
1010
0
  memset(&ecom_encap, 0, sizeof(ecom_encap));
1011
0
  encode_encap_extcomm(tnl_type, &eval);
1012
0
  ecom_encap.size = 1;
1013
0
  ecom_encap.unit_size = ECOMMUNITY_SIZE;
1014
0
  ecom_encap.val = (uint8_t *)eval.val;
1015
1016
  /* Add Encap */
1017
0
  if (bgp_attr_get_ecommunity(attr)) {
1018
0
    old_ecom = bgp_attr_get_ecommunity(attr);
1019
0
    ecom = ecommunity_merge(ecommunity_dup(old_ecom), &ecom_encap);
1020
0
    if (!old_ecom->refcnt)
1021
0
      ecommunity_free(&old_ecom);
1022
0
  } else
1023
0
    ecom = ecommunity_dup(&ecom_encap);
1024
0
  bgp_attr_set_ecommunity(attr, ecom);
1025
0
  attr->encap_tunneltype = tnl_type;
1026
1027
  /* Add the export RTs for L3VNI/VRF */
1028
0
  vrf_export_rtl = bgp_vrf->vrf_export_rtl;
1029
0
  for (ALL_LIST_ELEMENTS(vrf_export_rtl, node, nnode, l3rt))
1030
0
    bgp_attr_set_ecommunity(
1031
0
      attr, ecommunity_merge(bgp_attr_get_ecommunity(attr),
1032
0
                 l3rt->ecom));
1033
1034
  /* add the router mac extended community */
1035
0
  if (!is_zero_mac(&attr->rmac)) {
1036
0
    encode_rmac_extcomm(&eval_rmac, &attr->rmac);
1037
0
    ecommunity_add_val(bgp_attr_get_ecommunity(attr), &eval_rmac,
1038
0
           true, true);
1039
0
  }
1040
0
}
1041
1042
/*
1043
 * Build extended communities for EVPN route.
1044
 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
1045
 * and ENCAP extended communities are applicable for all routes.
1046
 * The default gateway extended community and MAC mobility (sticky) extended
1047
 * community are added as needed based on passed settings - only for type-2
1048
 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
1049
 * added, if present, based on passed settings - only for non-link-local
1050
 * type-2 routes.
1051
 */
1052
static void build_evpn_route_extcomm(struct bgpevpn *vpn, struct attr *attr,
1053
             int add_l3_ecomm)
1054
0
{
1055
0
  struct ecommunity ecom_encap;
1056
0
  struct ecommunity ecom_sticky;
1057
0
  struct ecommunity ecom_default_gw;
1058
0
  struct ecommunity ecom_na;
1059
0
  struct ecommunity_val eval;
1060
0
  struct ecommunity_val eval_sticky;
1061
0
  struct ecommunity_val eval_default_gw;
1062
0
  struct ecommunity_val eval_rmac;
1063
0
  struct ecommunity_val eval_na;
1064
0
  bool proxy;
1065
1066
0
  bgp_encap_types tnl_type;
1067
0
  struct listnode *node, *nnode;
1068
0
  struct ecommunity *ecom;
1069
0
  struct vrf_route_target *l3rt;
1070
0
  uint32_t seqnum;
1071
0
  struct list *vrf_export_rtl = NULL;
1072
1073
  /* Encap */
1074
0
  tnl_type = BGP_ENCAP_TYPE_VXLAN;
1075
0
  memset(&ecom_encap, 0, sizeof(ecom_encap));
1076
0
  encode_encap_extcomm(tnl_type, &eval);
1077
0
  ecom_encap.size = 1;
1078
0
  ecom_encap.unit_size = ECOMMUNITY_SIZE;
1079
0
  ecom_encap.val = (uint8_t *)eval.val;
1080
1081
  /* Add Encap */
1082
0
  bgp_attr_set_ecommunity(attr, ecommunity_dup(&ecom_encap));
1083
0
  attr->encap_tunneltype = tnl_type;
1084
1085
  /* Add the export RTs for L2VNI */
1086
0
  for (ALL_LIST_ELEMENTS(vpn->export_rtl, node, nnode, ecom))
1087
0
    bgp_attr_set_ecommunity(
1088
0
      attr,
1089
0
      ecommunity_merge(bgp_attr_get_ecommunity(attr), ecom));
1090
1091
  /* Add the export RTs for L3VNI if told to - caller determines
1092
   * when this should be done.
1093
   */
1094
0
  if (add_l3_ecomm) {
1095
0
    vrf_export_rtl = bgpevpn_get_vrf_export_rtl(vpn);
1096
0
    if (vrf_export_rtl && !list_isempty(vrf_export_rtl)) {
1097
0
      for (ALL_LIST_ELEMENTS(vrf_export_rtl, node, nnode,
1098
0
                 l3rt))
1099
0
        bgp_attr_set_ecommunity(
1100
0
          attr,
1101
0
          ecommunity_merge(
1102
0
            bgp_attr_get_ecommunity(attr),
1103
0
            l3rt->ecom));
1104
0
    }
1105
0
  }
1106
1107
  /* Add MAC mobility (sticky) if needed. */
1108
0
  if (attr->sticky) {
1109
0
    seqnum = 0;
1110
0
    memset(&ecom_sticky, 0, sizeof(ecom_sticky));
1111
0
    encode_mac_mobility_extcomm(1, seqnum, &eval_sticky);
1112
0
    ecom_sticky.size = 1;
1113
0
    ecom_sticky.unit_size = ECOMMUNITY_SIZE;
1114
0
    ecom_sticky.val = (uint8_t *)eval_sticky.val;
1115
0
    bgp_attr_set_ecommunity(
1116
0
      attr, ecommunity_merge(bgp_attr_get_ecommunity(attr),
1117
0
                 &ecom_sticky));
1118
0
  }
1119
1120
  /* Add RMAC, if told to. */
1121
0
  if (add_l3_ecomm) {
1122
0
    encode_rmac_extcomm(&eval_rmac, &attr->rmac);
1123
0
    ecommunity_add_val(bgp_attr_get_ecommunity(attr), &eval_rmac,
1124
0
           true, true);
1125
0
  }
1126
1127
  /* Add default gateway, if needed. */
1128
0
  if (attr->default_gw) {
1129
0
    memset(&ecom_default_gw, 0, sizeof(ecom_default_gw));
1130
0
    encode_default_gw_extcomm(&eval_default_gw);
1131
0
    ecom_default_gw.size = 1;
1132
0
    ecom_default_gw.unit_size = ECOMMUNITY_SIZE;
1133
0
    ecom_default_gw.val = (uint8_t *)eval_default_gw.val;
1134
0
    bgp_attr_set_ecommunity(
1135
0
      attr, ecommunity_merge(bgp_attr_get_ecommunity(attr),
1136
0
                 &ecom_default_gw));
1137
0
  }
1138
1139
0
  proxy = !!(attr->es_flags & ATTR_ES_PROXY_ADVERT);
1140
0
  if (attr->router_flag || proxy) {
1141
0
    memset(&ecom_na, 0, sizeof(ecom_na));
1142
0
    encode_na_flag_extcomm(&eval_na, attr->router_flag, proxy);
1143
0
    ecom_na.size = 1;
1144
0
    ecom_na.unit_size = ECOMMUNITY_SIZE;
1145
0
    ecom_na.val = (uint8_t *)eval_na.val;
1146
0
    bgp_attr_set_ecommunity(
1147
0
      attr, ecommunity_merge(bgp_attr_get_ecommunity(attr),
1148
0
                 &ecom_na));
1149
0
  }
1150
0
}
1151
1152
/*
1153
 * Add MAC mobility extended community to attribute.
1154
 */
1155
static void add_mac_mobility_to_attr(uint32_t seq_num, struct attr *attr)
1156
0
{
1157
0
  struct ecommunity ecom_tmp;
1158
0
  struct ecommunity_val eval;
1159
0
  uint8_t *ecom_val_ptr;
1160
0
  uint32_t i;
1161
0
  uint8_t *pnt;
1162
0
  int type = 0;
1163
0
  int sub_type = 0;
1164
0
  struct ecommunity *ecomm = bgp_attr_get_ecommunity(attr);
1165
1166
  /* Build MM */
1167
0
  encode_mac_mobility_extcomm(0, seq_num, &eval);
1168
1169
  /* Find current MM ecommunity */
1170
0
  ecom_val_ptr = NULL;
1171
1172
0
  if (ecomm) {
1173
0
    for (i = 0; i < ecomm->size; i++) {
1174
0
      pnt = ecomm->val + (i * ecomm->unit_size);
1175
0
      type = *pnt++;
1176
0
      sub_type = *pnt++;
1177
1178
0
      if (type == ECOMMUNITY_ENCODE_EVPN
1179
0
          && sub_type
1180
0
               == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY) {
1181
0
        ecom_val_ptr =
1182
0
          (ecomm->val + (i * ecomm->unit_size));
1183
0
        break;
1184
0
      }
1185
0
    }
1186
0
  }
1187
1188
  /* Update the existing MM ecommunity */
1189
0
  if (ecom_val_ptr) {
1190
0
    memcpy(ecom_val_ptr, eval.val, sizeof(char) * ecomm->unit_size);
1191
0
  }
1192
  /* Add MM to existing */
1193
0
  else {
1194
0
    memset(&ecom_tmp, 0, sizeof(ecom_tmp));
1195
0
    ecom_tmp.size = 1;
1196
0
    ecom_tmp.unit_size = ECOMMUNITY_SIZE;
1197
0
    ecom_tmp.val = (uint8_t *)eval.val;
1198
1199
0
    if (ecomm)
1200
0
      bgp_attr_set_ecommunity(
1201
0
        attr, ecommunity_merge(ecomm, &ecom_tmp));
1202
0
    else
1203
0
      bgp_attr_set_ecommunity(attr,
1204
0
            ecommunity_dup(&ecom_tmp));
1205
0
  }
1206
0
}
1207
1208
/* Install EVPN route into zebra. */
1209
static int evpn_zebra_install(struct bgp *bgp, struct bgpevpn *vpn,
1210
            const struct prefix_evpn *p,
1211
            struct bgp_path_info *pi)
1212
0
{
1213
0
  int ret;
1214
0
  uint8_t flags;
1215
0
  int flood_control = VXLAN_FLOOD_DISABLED;
1216
0
  uint32_t seq;
1217
1218
0
  if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
1219
0
    flags = 0;
1220
1221
0
    if (pi->sub_type == BGP_ROUTE_IMPORTED) {
1222
0
      if (pi->attr->sticky)
1223
0
        SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
1224
0
      if (pi->attr->default_gw)
1225
0
        SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
1226
0
      if (is_evpn_prefix_ipaddr_v6(p) &&
1227
0
          pi->attr->router_flag)
1228
0
        SET_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG);
1229
1230
0
      seq = mac_mobility_seqnum(pi->attr);
1231
      /* if local ES notify zebra that this is a sync path */
1232
0
      if (bgp_evpn_attr_is_local_es(pi->attr)) {
1233
0
        SET_FLAG(flags, ZEBRA_MACIP_TYPE_SYNC_PATH);
1234
0
        if (bgp_evpn_attr_is_proxy(pi->attr))
1235
0
          SET_FLAG(flags,
1236
0
            ZEBRA_MACIP_TYPE_PROXY_ADVERT);
1237
0
      }
1238
0
    } else {
1239
0
      if (!bgp_evpn_attr_is_sync(pi->attr))
1240
0
        return 0;
1241
1242
      /* if a local path is being turned around and sent
1243
       * to zebra it is because it is a sync path on
1244
       * a local ES
1245
       */
1246
0
      SET_FLAG(flags, ZEBRA_MACIP_TYPE_SYNC_PATH);
1247
      /* supply the highest peer seq number to zebra
1248
       * for MM seq syncing
1249
       */
1250
0
      seq = bgp_evpn_attr_get_sync_seq(pi->attr);
1251
      /* if any of the paths from the peer have the ROUTER
1252
       * flag set install the local entry as a router entry
1253
       */
1254
0
      if (is_evpn_prefix_ipaddr_v6(p) &&
1255
0
          (pi->attr->es_flags &
1256
0
           ATTR_ES_PEER_ROUTER))
1257
0
        SET_FLAG(flags,
1258
0
            ZEBRA_MACIP_TYPE_ROUTER_FLAG);
1259
1260
0
      if (!(pi->attr->es_flags & ATTR_ES_PEER_ACTIVE))
1261
0
        SET_FLAG(flags,
1262
0
            ZEBRA_MACIP_TYPE_PROXY_ADVERT);
1263
0
    }
1264
1265
0
    ret = bgp_zebra_send_remote_macip(
1266
0
      bgp, vpn, p,
1267
0
      (is_evpn_prefix_ipaddr_none(p)
1268
         ? NULL /* MAC update */
1269
0
         : evpn_type2_path_info_get_mac(
1270
0
             pi) /* MAC-IP update */),
1271
0
      pi->attr->nexthop, 1, flags, seq,
1272
0
      bgp_evpn_attr_get_esi(pi->attr));
1273
0
  } else if (p->prefix.route_type == BGP_EVPN_AD_ROUTE) {
1274
0
    ret = bgp_evpn_remote_es_evi_add(bgp, vpn, p);
1275
0
  } else {
1276
0
    switch (bgp_attr_get_pmsi_tnl_type(pi->attr)) {
1277
0
    case PMSI_TNLTYPE_INGR_REPL:
1278
0
      flood_control = VXLAN_FLOOD_HEAD_END_REPL;
1279
0
      break;
1280
1281
0
    case PMSI_TNLTYPE_PIM_SM:
1282
0
      flood_control = VXLAN_FLOOD_PIM_SM;
1283
0
      break;
1284
1285
0
    case PMSI_TNLTYPE_NO_INFO:
1286
0
    case PMSI_TNLTYPE_RSVP_TE_P2MP:
1287
0
    case PMSI_TNLTYPE_MLDP_P2MP:
1288
0
    case PMSI_TNLTYPE_PIM_SSM:
1289
0
    case PMSI_TNLTYPE_PIM_BIDIR:
1290
0
    case PMSI_TNLTYPE_MLDP_MP2MP:
1291
0
      flood_control = VXLAN_FLOOD_DISABLED;
1292
0
      break;
1293
0
    }
1294
0
    ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, flood_control, 1);
1295
0
  }
1296
1297
0
  return ret;
1298
0
}
1299
1300
/* Uninstall EVPN route from zebra. */
1301
static int evpn_zebra_uninstall(struct bgp *bgp, struct bgpevpn *vpn,
1302
        const struct prefix_evpn *p,
1303
        struct bgp_path_info *pi, bool is_sync)
1304
0
{
1305
0
  int ret;
1306
1307
0
  if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
1308
0
    ret = bgp_zebra_send_remote_macip(
1309
0
      bgp, vpn, p,
1310
0
      (is_evpn_prefix_ipaddr_none(p)
1311
         ? NULL /* MAC update */
1312
0
         : evpn_type2_path_info_get_mac(
1313
0
             pi) /* MAC-IP update */),
1314
0
      (is_sync ? zero_vtep_ip : pi->attr->nexthop), 0, 0, 0,
1315
0
      NULL);
1316
0
  else if (p->prefix.route_type == BGP_EVPN_AD_ROUTE)
1317
0
    ret = bgp_evpn_remote_es_evi_del(bgp, vpn, p);
1318
0
  else
1319
0
    ret = bgp_zebra_send_remote_vtep(bgp, vpn, p,
1320
0
          VXLAN_FLOOD_DISABLED, 0);
1321
1322
0
  return ret;
1323
0
}
1324
1325
/*
1326
 * Due to MAC mobility, the prior "local" best route has been supplanted
1327
 * by a "remote" best route. The prior route has to be deleted and withdrawn
1328
 * from peers.
1329
 */
1330
static void evpn_delete_old_local_route(struct bgp *bgp, struct bgpevpn *vpn,
1331
          struct bgp_dest *dest,
1332
          struct bgp_path_info *old_local,
1333
          struct bgp_path_info *new_select)
1334
0
{
1335
0
  struct bgp_dest *global_dest;
1336
0
  struct bgp_path_info *pi;
1337
0
  afi_t afi = AFI_L2VPN;
1338
0
  safi_t safi = SAFI_EVPN;
1339
1340
0
  if (BGP_DEBUG(evpn_mh, EVPN_MH_RT)) {
1341
0
    char esi_buf[ESI_STR_LEN];
1342
0
    char esi_buf2[ESI_STR_LEN];
1343
0
    struct prefix_evpn *evp =
1344
0
      (struct prefix_evpn *)bgp_dest_get_prefix(dest);
1345
1346
0
    zlog_debug("local path deleted %pFX es %s; new-path-es %s", evp,
1347
0
         esi_to_str(&old_local->attr->esi, esi_buf,
1348
0
              sizeof(esi_buf)),
1349
0
         new_select ? esi_to_str(&new_select->attr->esi,
1350
0
               esi_buf2, sizeof(esi_buf2))
1351
0
              : "");
1352
0
  }
1353
1354
  /* Locate route node in the global EVPN routing table. Note that
1355
   * this table is a 2-level tree (RD-level + Prefix-level) similar to
1356
   * L3VPN routes.
1357
   */
1358
0
  global_dest = bgp_evpn_global_node_lookup(
1359
0
    bgp->rib[afi][safi], safi,
1360
0
    (const struct prefix_evpn *)bgp_dest_get_prefix(dest),
1361
0
    &vpn->prd, old_local);
1362
0
  if (global_dest) {
1363
    /* Delete route entry in the global EVPN table. */
1364
0
    delete_evpn_route_entry(bgp, afi, safi, global_dest, &pi);
1365
1366
    /* Schedule for processing - withdraws to peers happen from
1367
     * this table.
1368
     */
1369
0
    if (pi)
1370
0
      bgp_process(bgp, global_dest, afi, safi);
1371
0
    bgp_dest_unlock_node(global_dest);
1372
0
  }
1373
1374
  /* Delete route entry in the VNI route table, caller to remove. */
1375
0
  bgp_path_info_delete(dest, old_local);
1376
0
}
1377
1378
/*
1379
 * Calculate the best path for an EVPN route. Install/update best path in zebra,
1380
 * if appropriate.
1381
 * Note: vpn is NULL for local EAD-ES routes.
1382
 */
1383
int evpn_route_select_install(struct bgp *bgp, struct bgpevpn *vpn,
1384
             struct bgp_dest *dest)
1385
0
{
1386
0
  struct bgp_path_info *old_select, *new_select;
1387
0
  struct bgp_path_info_pair old_and_new;
1388
0
  afi_t afi = AFI_L2VPN;
1389
0
  safi_t safi = SAFI_EVPN;
1390
0
  int ret = 0;
1391
1392
  /* Compute the best path. */
1393
0
  bgp_best_selection(bgp, dest, &bgp->maxpaths[afi][safi], &old_and_new,
1394
0
         afi, safi);
1395
0
  old_select = old_and_new.old;
1396
0
  new_select = old_and_new.new;
1397
1398
  /* If the best path hasn't changed - see if there is still something to
1399
   * update to zebra RIB.
1400
   * Remote routes and SYNC route (i.e. local routes with
1401
   * SYNCED_FROM_PEER flag) need to updated to zebra on any attr
1402
   * change.
1403
   */
1404
0
  if (old_select && old_select == new_select
1405
0
      && old_select->type == ZEBRA_ROUTE_BGP
1406
0
      && (old_select->sub_type == BGP_ROUTE_IMPORTED ||
1407
0
      bgp_evpn_attr_is_sync(old_select->attr))
1408
0
      && !CHECK_FLAG(dest->flags, BGP_NODE_USER_CLEAR)
1409
0
      && !CHECK_FLAG(old_select->flags, BGP_PATH_ATTR_CHANGED)
1410
0
      && !bgp_addpath_is_addpath_used(&bgp->tx_addpath, afi, safi)) {
1411
0
    if (bgp_zebra_has_route_changed(old_select))
1412
0
      ret = evpn_zebra_install(
1413
0
        bgp, vpn,
1414
0
        (const struct prefix_evpn *)bgp_dest_get_prefix(
1415
0
          dest),
1416
0
        old_select);
1417
0
    UNSET_FLAG(old_select->flags, BGP_PATH_MULTIPATH_CHG);
1418
0
    UNSET_FLAG(old_select->flags, BGP_PATH_LINK_BW_CHG);
1419
0
    bgp_zebra_clear_route_change_flags(dest);
1420
0
    return ret;
1421
0
  }
1422
1423
  /* If the user did a "clear" this flag will be set */
1424
0
  UNSET_FLAG(dest->flags, BGP_NODE_USER_CLEAR);
1425
1426
  /* bestpath has changed; update relevant fields and install or uninstall
1427
   * into the zebra RIB.
1428
   */
1429
0
  if (old_select || new_select)
1430
0
    bgp_bump_version(dest);
1431
1432
0
  if (old_select)
1433
0
    bgp_path_info_unset_flag(dest, old_select, BGP_PATH_SELECTED);
1434
0
  if (new_select) {
1435
0
    bgp_path_info_set_flag(dest, new_select, BGP_PATH_SELECTED);
1436
0
    bgp_path_info_unset_flag(dest, new_select,
1437
0
           BGP_PATH_ATTR_CHANGED);
1438
0
    UNSET_FLAG(new_select->flags, BGP_PATH_MULTIPATH_CHG);
1439
0
    UNSET_FLAG(new_select->flags, BGP_PATH_LINK_BW_CHG);
1440
0
  }
1441
1442
  /* a local entry with the SYNC flag also results in a MAC-IP update
1443
   * to zebra
1444
   */
1445
0
  if (new_select && new_select->type == ZEBRA_ROUTE_BGP
1446
0
      && (new_select->sub_type == BGP_ROUTE_IMPORTED ||
1447
0
      bgp_evpn_attr_is_sync(new_select->attr))) {
1448
0
    ret = evpn_zebra_install(
1449
0
      bgp, vpn,
1450
0
      (struct prefix_evpn *)bgp_dest_get_prefix(dest),
1451
0
      new_select);
1452
1453
    /* If an old best existed and it was a "local" route, the only
1454
     * reason
1455
     * it would be supplanted is due to MAC mobility procedures. So,
1456
     * we
1457
     * need to do an implicit delete and withdraw that route from
1458
     * peers.
1459
     */
1460
0
    if (new_select->sub_type == BGP_ROUTE_IMPORTED &&
1461
0
        old_select && old_select->peer == bgp->peer_self
1462
0
        && old_select->type == ZEBRA_ROUTE_BGP
1463
0
        && old_select->sub_type == BGP_ROUTE_STATIC
1464
0
        && vpn)
1465
0
      evpn_delete_old_local_route(bgp, vpn, dest,
1466
0
          old_select, new_select);
1467
0
  } else {
1468
0
    if (old_select && old_select->type == ZEBRA_ROUTE_BGP
1469
0
        && old_select->sub_type == BGP_ROUTE_IMPORTED)
1470
0
      ret = evpn_zebra_uninstall(
1471
0
        bgp, vpn,
1472
0
        (const struct prefix_evpn *)bgp_dest_get_prefix(
1473
0
          dest),
1474
0
        old_select, false);
1475
0
  }
1476
1477
  /* Clear any route change flags. */
1478
0
  bgp_zebra_clear_route_change_flags(dest);
1479
1480
  /* Reap old select bgp_path_info, if it has been removed */
1481
0
  if (old_select && CHECK_FLAG(old_select->flags, BGP_PATH_REMOVED))
1482
0
    bgp_path_info_reap(dest, old_select);
1483
1484
0
  return ret;
1485
0
}
1486
1487
static struct bgp_path_info *bgp_evpn_route_get_local_path(
1488
    struct bgp *bgp, struct bgp_dest *dest)
1489
0
{
1490
0
  struct bgp_path_info *tmp_pi;
1491
0
  struct bgp_path_info *local_pi = NULL;
1492
1493
0
  for (tmp_pi = bgp_dest_get_bgp_path_info(dest); tmp_pi;
1494
0
      tmp_pi = tmp_pi->next) {
1495
0
    if (bgp_evpn_is_path_local(bgp, tmp_pi)) {
1496
0
      local_pi = tmp_pi;
1497
0
      break;
1498
0
    }
1499
0
  }
1500
1501
0
  return local_pi;
1502
0
}
1503
1504
static int update_evpn_type5_route_entry(struct bgp *bgp_evpn,
1505
           struct bgp *bgp_vrf, afi_t afi,
1506
           safi_t safi, struct bgp_dest *dest,
1507
           struct attr *attr, int *route_changed)
1508
0
{
1509
0
  struct attr *attr_new = NULL;
1510
0
  struct bgp_path_info *pi = NULL;
1511
0
  mpls_label_t label = MPLS_INVALID_LABEL;
1512
0
  struct bgp_path_info *local_pi = NULL;
1513
0
  struct bgp_path_info *tmp_pi = NULL;
1514
1515
0
  *route_changed = 0;
1516
1517
  /* See if this is an update of an existing route, or a new add. */
1518
0
  local_pi = bgp_evpn_route_get_local_path(bgp_evpn, dest);
1519
1520
  /*
1521
   * create a new route entry if one doesn't exist.
1522
   * Otherwise see if route attr has changed
1523
   */
1524
0
  if (!local_pi) {
1525
1526
    /* route has changed as this is the first entry */
1527
0
    *route_changed = 1;
1528
1529
    /* Add (or update) attribute to hash. */
1530
0
    attr_new = bgp_attr_intern(attr);
1531
1532
    /* create the route info from attribute */
1533
0
    pi = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0,
1534
0
             bgp_evpn->peer_self, attr_new, dest);
1535
0
    SET_FLAG(pi->flags, BGP_PATH_VALID);
1536
1537
    /* Type-5 routes advertise the L3-VNI */
1538
0
    bgp_path_info_extra_get(pi);
1539
0
    vni2label(bgp_vrf->l3vni, &label);
1540
0
    memcpy(&pi->extra->label, &label, sizeof(label));
1541
0
    pi->extra->num_labels = 1;
1542
1543
    /* add the route entry to route node*/
1544
0
    bgp_path_info_add(dest, pi);
1545
0
  } else {
1546
1547
0
    tmp_pi = local_pi;
1548
0
    if (!attrhash_cmp(tmp_pi->attr, attr)) {
1549
1550
      /* attribute changed */
1551
0
      *route_changed = 1;
1552
1553
      /* The attribute has changed. */
1554
      /* Add (or update) attribute to hash. */
1555
0
      attr_new = bgp_attr_intern(attr);
1556
0
      bgp_path_info_set_flag(dest, tmp_pi,
1557
0
                 BGP_PATH_ATTR_CHANGED);
1558
1559
      /* Restore route, if needed. */
1560
0
      if (CHECK_FLAG(tmp_pi->flags, BGP_PATH_REMOVED))
1561
0
        bgp_path_info_restore(dest, tmp_pi);
1562
1563
      /* Unintern existing, set to new. */
1564
0
      bgp_attr_unintern(&tmp_pi->attr);
1565
0
      tmp_pi->attr = attr_new;
1566
0
      tmp_pi->uptime = monotime(NULL);
1567
0
    }
1568
0
  }
1569
0
  return 0;
1570
0
}
1571
1572
/* update evpn type-5 route entry */
1573
static int update_evpn_type5_route(struct bgp *bgp_vrf, struct prefix_evpn *evp,
1574
           struct attr *src_attr, afi_t src_afi,
1575
           safi_t src_safi)
1576
0
{
1577
0
  afi_t afi = AFI_L2VPN;
1578
0
  safi_t safi = SAFI_EVPN;
1579
0
  struct attr attr;
1580
0
  struct bgp_dest *dest = NULL;
1581
0
  struct bgp *bgp_evpn = NULL;
1582
0
  int route_changed = 0;
1583
1584
0
  bgp_evpn = bgp_get_evpn();
1585
0
  if (!bgp_evpn)
1586
0
    return 0;
1587
1588
  /* Build path attribute for this route - use the source attr, if
1589
   * present, else treat as locally originated.
1590
   */
1591
0
  if (src_attr)
1592
0
    attr = *src_attr;
1593
0
  else {
1594
0
    memset(&attr, 0, sizeof(attr));
1595
0
    bgp_attr_default_set(&attr, bgp_vrf, BGP_ORIGIN_IGP);
1596
0
  }
1597
1598
  /* Advertise Primary IP (PIP) is enabled, send individual
1599
   * IP (default instance router-id) as nexthop.
1600
   * PIP is disabled or vrr interface is not present
1601
   * use anycast-IP as nexthop and anycast RMAC.
1602
   */
1603
0
  if (!bgp_vrf->evpn_info->advertise_pip ||
1604
0
      (!bgp_vrf->evpn_info->is_anycast_mac)) {
1605
0
    attr.nexthop = bgp_vrf->originator_ip;
1606
0
    attr.mp_nexthop_global_in = bgp_vrf->originator_ip;
1607
0
    memcpy(&attr.rmac, &bgp_vrf->rmac, ETH_ALEN);
1608
0
  } else {
1609
    /* copy sys rmac */
1610
0
    memcpy(&attr.rmac, &bgp_vrf->evpn_info->pip_rmac, ETH_ALEN);
1611
0
    if (bgp_vrf->evpn_info->pip_ip.s_addr != INADDR_ANY) {
1612
0
      attr.nexthop = bgp_vrf->evpn_info->pip_ip;
1613
0
      attr.mp_nexthop_global_in = bgp_vrf->evpn_info->pip_ip;
1614
0
    } else if (bgp_vrf->evpn_info->pip_ip.s_addr == INADDR_ANY)
1615
0
      if (bgp_debug_zebra(NULL))
1616
0
        zlog_debug(
1617
0
          "VRF %s evp %pFX advertise-pip primary ip is not configured",
1618
0
          vrf_id_to_name(bgp_vrf->vrf_id), evp);
1619
0
  }
1620
1621
0
  if (bgp_debug_zebra(NULL))
1622
0
    zlog_debug(
1623
0
      "VRF %s type-5 route evp %pFX RMAC %pEA nexthop %pI4",
1624
0
      vrf_id_to_name(bgp_vrf->vrf_id), evp, &attr.rmac,
1625
0
      &attr.nexthop);
1626
1627
0
  attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1628
1629
0
  if (src_afi == AFI_IP6 &&
1630
0
      CHECK_FLAG(bgp_vrf->af_flags[AFI_L2VPN][SAFI_EVPN],
1631
0
           BGP_L2VPN_EVPN_ADV_IPV6_UNICAST_GW_IP)) {
1632
0
    if (src_attr &&
1633
0
        !IN6_IS_ADDR_UNSPECIFIED(&src_attr->mp_nexthop_global)) {
1634
0
      attr.evpn_overlay.type = OVERLAY_INDEX_GATEWAY_IP;
1635
0
      SET_IPADDR_V6(&attr.evpn_overlay.gw_ip);
1636
0
      memcpy(&attr.evpn_overlay.gw_ip.ipaddr_v6,
1637
0
             &src_attr->mp_nexthop_global,
1638
0
             sizeof(struct in6_addr));
1639
0
    }
1640
0
  } else if (src_afi == AFI_IP &&
1641
0
       CHECK_FLAG(bgp_vrf->af_flags[AFI_L2VPN][SAFI_EVPN],
1642
0
            BGP_L2VPN_EVPN_ADV_IPV4_UNICAST_GW_IP)) {
1643
0
    if (src_attr && src_attr->nexthop.s_addr != 0) {
1644
0
      attr.evpn_overlay.type = OVERLAY_INDEX_GATEWAY_IP;
1645
0
      SET_IPADDR_V4(&attr.evpn_overlay.gw_ip);
1646
0
      memcpy(&attr.evpn_overlay.gw_ip.ipaddr_v4,
1647
0
             &src_attr->nexthop, sizeof(struct in_addr));
1648
0
    }
1649
0
  }
1650
1651
  /* Setup RT and encap extended community */
1652
0
  build_evpn_type5_route_extcomm(bgp_vrf, &attr);
1653
1654
  /* get the route node in global table */
1655
0
  dest = bgp_evpn_global_node_get(bgp_evpn->rib[afi][safi], afi, safi,
1656
0
          evp, &bgp_vrf->vrf_prd, NULL);
1657
0
  assert(dest);
1658
1659
  /* create or update the route entry within the route node */
1660
0
  update_evpn_type5_route_entry(bgp_evpn, bgp_vrf, afi, safi, dest, &attr,
1661
0
              &route_changed);
1662
1663
  /* schedule for processing and unlock node */
1664
0
  if (route_changed) {
1665
0
    bgp_process(bgp_evpn, dest, afi, safi);
1666
0
    bgp_dest_unlock_node(dest);
1667
0
  }
1668
1669
  /* uninten temporary */
1670
0
  if (!src_attr)
1671
0
    aspath_unintern(&attr.aspath);
1672
0
  return 0;
1673
0
}
1674
1675
static void bgp_evpn_get_sync_info(struct bgp *bgp, esi_t *esi,
1676
           struct bgp_dest *dest, uint32_t loc_seq,
1677
           uint32_t *max_sync_seq, bool *active_on_peer,
1678
           bool *peer_router, bool *proxy_from_peer,
1679
           const struct ethaddr *mac)
1680
0
{
1681
0
  struct bgp_path_info *tmp_pi;
1682
0
  struct bgp_path_info *second_best_path = NULL;
1683
0
  uint32_t tmp_mm_seq = 0;
1684
0
  esi_t *tmp_esi;
1685
0
  int paths_eq;
1686
0
  struct ethaddr *tmp_mac;
1687
0
  bool mac_cmp = false;
1688
0
  struct prefix_evpn *evp = (struct prefix_evpn *)&dest->p;
1689
1690
1691
  /* mac comparison is not needed for MAC-only routes */
1692
0
  if (mac && !is_evpn_prefix_ipaddr_none(evp))
1693
0
    mac_cmp = true;
1694
1695
  /* find the best non-local path. a local path can only be present
1696
   * as best path
1697
   */
1698
0
  for (tmp_pi = bgp_dest_get_bgp_path_info(dest); tmp_pi;
1699
0
       tmp_pi = tmp_pi->next) {
1700
0
    if (tmp_pi->sub_type != BGP_ROUTE_IMPORTED ||
1701
0
      !CHECK_FLAG(tmp_pi->flags, BGP_PATH_VALID))
1702
0
      continue;
1703
1704
    /* ignore paths that have a different mac */
1705
0
    if (mac_cmp) {
1706
0
      tmp_mac = evpn_type2_path_info_get_mac(tmp_pi);
1707
0
      if (memcmp(mac, tmp_mac, sizeof(*mac)))
1708
0
        continue;
1709
0
    }
1710
1711
0
    if (bgp_evpn_path_info_cmp(bgp, tmp_pi,
1712
0
        second_best_path, &paths_eq))
1713
0
      second_best_path = tmp_pi;
1714
0
  }
1715
1716
0
  if (!second_best_path)
1717
0
    return;
1718
1719
0
  tmp_esi = bgp_evpn_attr_get_esi(second_best_path->attr);
1720
  /* if this has the same ES desination as the local path
1721
   * it is a sync path
1722
   */
1723
0
  if (!memcmp(esi, tmp_esi, sizeof(esi_t))) {
1724
0
    tmp_mm_seq = mac_mobility_seqnum(second_best_path->attr);
1725
0
    if (tmp_mm_seq < loc_seq)
1726
0
      return;
1727
1728
    /* we have a non-proxy path from the ES peer.  */
1729
0
    if (second_best_path->attr->es_flags &
1730
0
          ATTR_ES_PROXY_ADVERT) {
1731
0
      *proxy_from_peer = true;
1732
0
    } else {
1733
0
      *active_on_peer = true;
1734
0
    }
1735
1736
0
    if (second_best_path->attr->router_flag)
1737
0
      *peer_router = true;
1738
1739
    /* we use both proxy and non-proxy imports to
1740
     * determine the max sync sequence
1741
     */
1742
0
    if (tmp_mm_seq > *max_sync_seq)
1743
0
      *max_sync_seq = tmp_mm_seq;
1744
0
  }
1745
0
}
1746
1747
/* Bubble up sync-info from all paths (non-best) to the local-path.
1748
 * This is need for MM sequence number syncing and proxy advertisement.
1749
 * Note: The local path can only exist as a best path in the
1750
 * VPN route table. It will take precedence over all sync paths.
1751
 */
1752
static void update_evpn_route_entry_sync_info(struct bgp *bgp,
1753
                struct bgp_dest *dest,
1754
                struct attr *attr,
1755
                uint32_t loc_seq, bool setup_sync,
1756
                const struct ethaddr *mac)
1757
0
{
1758
0
  esi_t *esi;
1759
0
  struct prefix_evpn *evp =
1760
0
    (struct prefix_evpn *)bgp_dest_get_prefix(dest);
1761
1762
0
  if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1763
0
    return;
1764
1765
0
  esi = bgp_evpn_attr_get_esi(attr);
1766
0
  if (bgp_evpn_is_esi_valid(esi)) {
1767
0
    if (setup_sync) {
1768
0
      uint32_t max_sync_seq = 0;
1769
0
      bool active_on_peer = false;
1770
0
      bool peer_router = false;
1771
0
      bool proxy_from_peer = false;
1772
1773
0
      bgp_evpn_get_sync_info(bgp, esi, dest, loc_seq,
1774
0
                 &max_sync_seq, &active_on_peer,
1775
0
                 &peer_router, &proxy_from_peer,
1776
0
                 mac);
1777
0
      attr->mm_sync_seqnum = max_sync_seq;
1778
0
      if (active_on_peer)
1779
0
        attr->es_flags |= ATTR_ES_PEER_ACTIVE;
1780
0
      else
1781
0
        attr->es_flags &= ~ATTR_ES_PEER_ACTIVE;
1782
0
      if (proxy_from_peer)
1783
0
        attr->es_flags |= ATTR_ES_PEER_PROXY;
1784
0
      else
1785
0
        attr->es_flags &= ~ATTR_ES_PEER_PROXY;
1786
0
      if (peer_router)
1787
0
        attr->es_flags |= ATTR_ES_PEER_ROUTER;
1788
0
      else
1789
0
        attr->es_flags &= ~ATTR_ES_PEER_ROUTER;
1790
1791
0
      if (BGP_DEBUG(evpn_mh, EVPN_MH_RT)) {
1792
0
        char esi_buf[ESI_STR_LEN];
1793
1794
0
        zlog_debug(
1795
0
          "setup sync info for %pFX es %s max_seq %d %s%s%s",
1796
0
          evp,
1797
0
          esi_to_str(esi, esi_buf,
1798
0
               sizeof(esi_buf)),
1799
0
          max_sync_seq,
1800
0
          (attr->es_flags & ATTR_ES_PEER_ACTIVE)
1801
0
            ? "peer-active "
1802
0
            : "",
1803
0
          (attr->es_flags & ATTR_ES_PEER_PROXY)
1804
0
            ? "peer-proxy "
1805
0
            : "",
1806
0
          (attr->es_flags & ATTR_ES_PEER_ROUTER)
1807
0
            ? "peer-router "
1808
0
            : "");
1809
0
      }
1810
0
    }
1811
0
  } else {
1812
0
    attr->mm_sync_seqnum = 0;
1813
0
    attr->es_flags &= ~ATTR_ES_PEER_ACTIVE;
1814
0
    attr->es_flags &= ~ATTR_ES_PEER_PROXY;
1815
0
  }
1816
0
}
1817
1818
/*
1819
 * Create or update EVPN route entry. This could be in the VNI route tables
1820
 * or the global route table.
1821
 */
1822
static int update_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
1823
           afi_t afi, safi_t safi,
1824
           struct bgp_dest *dest, struct attr *attr,
1825
           const struct ethaddr *mac,
1826
           const struct ipaddr *ip, int add,
1827
           struct bgp_path_info **pi, uint8_t flags,
1828
           uint32_t seq, bool vpn_rt, bool *old_is_sync)
1829
0
{
1830
0
  struct bgp_path_info *tmp_pi;
1831
0
  struct bgp_path_info *local_pi;
1832
0
  struct attr *attr_new;
1833
0
  struct attr local_attr;
1834
0
  mpls_label_t label[BGP_MAX_LABELS];
1835
0
  uint32_t num_labels = 1;
1836
0
  int route_change = 1;
1837
0
  uint8_t sticky = 0;
1838
0
  const struct prefix_evpn *evp;
1839
1840
0
  *pi = NULL;
1841
0
  evp = (const struct prefix_evpn *)bgp_dest_get_prefix(dest);
1842
0
  memset(&label, 0, sizeof(label));
1843
1844
  /* See if this is an update of an existing route, or a new add. */
1845
0
  local_pi = bgp_evpn_route_get_local_path(bgp, dest);
1846
1847
  /* If route doesn't exist already, create a new one, if told to.
1848
   * Otherwise act based on whether the attributes of the route have
1849
   * changed or not.
1850
   */
1851
0
  if (!local_pi && !add)
1852
0
    return 0;
1853
1854
0
  if (old_is_sync && local_pi)
1855
0
    *old_is_sync = bgp_evpn_attr_is_sync(local_pi->attr);
1856
1857
  /* if a local path is being added with a non-zero esi look
1858
   * for SYNC paths from ES peers and bubble up the sync-info
1859
   */
1860
0
  update_evpn_route_entry_sync_info(bgp, dest, attr, seq, vpn_rt, mac);
1861
1862
  /* For non-GW MACs, update MAC mobility seq number, if needed. */
1863
0
  if (seq && !CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW))
1864
0
    add_mac_mobility_to_attr(seq, attr);
1865
1866
0
  if (!local_pi) {
1867
0
    local_attr = *attr;
1868
1869
    /* Extract MAC mobility sequence number, if any. */
1870
0
    local_attr.mm_seqnum =
1871
0
      bgp_attr_mac_mobility_seqnum(&local_attr, &sticky);
1872
0
    local_attr.sticky = sticky;
1873
1874
    /* Add (or update) attribute to hash. */
1875
0
    attr_new = bgp_attr_intern(&local_attr);
1876
1877
    /* Create new route with its attribute. */
1878
0
    tmp_pi = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0,
1879
0
           bgp->peer_self, attr_new, dest);
1880
0
    SET_FLAG(tmp_pi->flags, BGP_PATH_VALID);
1881
0
    bgp_path_info_extra_get(tmp_pi);
1882
1883
    /* The VNI goes into the 'label' field of the route */
1884
0
    vni2label(vpn->vni, &label[0]);
1885
1886
    /* Type-2 routes may carry a second VNI - the L3-VNI.
1887
     * Only attach second label if we are advertising two labels for
1888
     * type-2 routes.
1889
     */
1890
0
    if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
1891
0
        && CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)) {
1892
0
      vni_t l3vni;
1893
1894
0
      l3vni = bgpevpn_get_l3vni(vpn);
1895
0
      if (l3vni) {
1896
0
        vni2label(l3vni, &label[1]);
1897
0
        num_labels++;
1898
0
      }
1899
0
    }
1900
1901
0
    memcpy(&tmp_pi->extra->label, label, sizeof(label));
1902
0
    tmp_pi->extra->num_labels = num_labels;
1903
1904
0
    if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
1905
0
      if (mac)
1906
0
        evpn_type2_path_info_set_mac(tmp_pi, *mac);
1907
0
      else if (ip)
1908
0
        evpn_type2_path_info_set_ip(tmp_pi, *ip);
1909
0
    }
1910
1911
    /* Mark route as self type-2 route */
1912
0
    if (flags && CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_SVI_IP))
1913
0
      tmp_pi->extra->af_flags = BGP_EVPN_MACIP_TYPE_SVI_IP;
1914
0
    bgp_path_info_add(dest, tmp_pi);
1915
0
  } else {
1916
0
    tmp_pi = local_pi;
1917
0
    if (attrhash_cmp(tmp_pi->attr, attr)
1918
0
        && !CHECK_FLAG(tmp_pi->flags, BGP_PATH_REMOVED))
1919
0
      route_change = 0;
1920
0
    else {
1921
      /*
1922
       * The attributes have changed, type-2 routes needs to
1923
       * be advertised with right labels.
1924
       */
1925
0
      vni2label(vpn->vni, &label[0]);
1926
0
      if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
1927
0
          && CHECK_FLAG(vpn->flags,
1928
0
            VNI_FLAG_USE_TWO_LABELS)) {
1929
0
        vni_t l3vni;
1930
1931
0
        l3vni = bgpevpn_get_l3vni(vpn);
1932
0
        if (l3vni) {
1933
0
          vni2label(l3vni, &label[1]);
1934
0
          num_labels++;
1935
0
        }
1936
0
      }
1937
0
      memcpy(&tmp_pi->extra->label, label, sizeof(label));
1938
0
      tmp_pi->extra->num_labels = num_labels;
1939
1940
0
      if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
1941
0
        if (mac)
1942
0
          evpn_type2_path_info_set_mac(tmp_pi,
1943
0
                     *mac);
1944
0
        else if (ip)
1945
0
          evpn_type2_path_info_set_ip(tmp_pi,
1946
0
                    *ip);
1947
0
      }
1948
1949
      /* The attribute has changed. */
1950
      /* Add (or update) attribute to hash. */
1951
0
      local_attr = *attr;
1952
0
      bgp_path_info_set_flag(dest, tmp_pi,
1953
0
                 BGP_PATH_ATTR_CHANGED);
1954
1955
      /* Extract MAC mobility sequence number, if any. */
1956
0
      local_attr.mm_seqnum = bgp_attr_mac_mobility_seqnum(
1957
0
        &local_attr, &sticky);
1958
0
      local_attr.sticky = sticky;
1959
1960
0
      attr_new = bgp_attr_intern(&local_attr);
1961
1962
      /* Restore route, if needed. */
1963
0
      if (CHECK_FLAG(tmp_pi->flags, BGP_PATH_REMOVED))
1964
0
        bgp_path_info_restore(dest, tmp_pi);
1965
1966
      /* Unintern existing, set to new. */
1967
0
      bgp_attr_unintern(&tmp_pi->attr);
1968
0
      tmp_pi->attr = attr_new;
1969
0
      tmp_pi->uptime = monotime(NULL);
1970
0
    }
1971
0
  }
1972
1973
  /* local MAC-IP routes in the VNI table are linked to
1974
   * the destination ES
1975
   */
1976
0
  if (route_change && vpn_rt
1977
0
      && (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE))
1978
0
    bgp_evpn_path_es_link(tmp_pi, vpn->vni,
1979
0
              bgp_evpn_attr_get_esi(tmp_pi->attr));
1980
1981
  /* Return back the route entry. */
1982
0
  *pi = tmp_pi;
1983
0
  return route_change;
1984
0
}
1985
1986
static void evpn_zebra_reinstall_best_route(struct bgp *bgp,
1987
              struct bgpevpn *vpn,
1988
              struct bgp_dest *dest)
1989
0
{
1990
0
  struct bgp_path_info *tmp_ri;
1991
0
  struct bgp_path_info *curr_select = NULL;
1992
1993
0
  for (tmp_ri = bgp_dest_get_bgp_path_info(dest); tmp_ri;
1994
0
       tmp_ri = tmp_ri->next) {
1995
0
    if (CHECK_FLAG(tmp_ri->flags, BGP_PATH_SELECTED)) {
1996
0
      curr_select = tmp_ri;
1997
0
      break;
1998
0
    }
1999
0
  }
2000
2001
0
  if (curr_select && curr_select->type == ZEBRA_ROUTE_BGP
2002
0
      && (curr_select->sub_type == BGP_ROUTE_IMPORTED ||
2003
0
        bgp_evpn_attr_is_sync(curr_select->attr)))
2004
0
    evpn_zebra_install(bgp, vpn,
2005
0
       (const struct prefix_evpn *)bgp_dest_get_prefix(dest),
2006
0
       curr_select);
2007
0
}
2008
2009
/*
2010
 * If the local route was not selected evict it and tell zebra to re-add
2011
 * the best remote dest.
2012
 *
2013
 * Typically a local path added by zebra is expected to be selected as
2014
 * best. In which case when a remote path wins as best (later)
2015
 * evpn_route_select_install itself evicts the older-local-best path.
2016
 *
2017
 * However if bgp's add and zebra's add cross paths (race condition) it
2018
 * is possible that the local path is no longer the "older" best path.
2019
 * It is a path that was never designated as best and hence requires
2020
 * additional handling to prevent bgp from injecting and holding on to a
2021
 * non-best local path.
2022
 */
2023
static void evpn_cleanup_local_non_best_route(struct bgp *bgp,
2024
                struct bgpevpn *vpn,
2025
                struct bgp_dest *dest,
2026
                struct bgp_path_info *local_pi)
2027
0
{
2028
  /* local path was not picked as the winner; kick it out */
2029
0
  if (bgp_debug_zebra(NULL))
2030
0
    zlog_debug("evicting local evpn prefix %pBD as remote won",
2031
0
         dest);
2032
2033
0
  evpn_delete_old_local_route(bgp, vpn, dest, local_pi, NULL);
2034
0
  bgp_path_info_reap(dest, local_pi);
2035
2036
  /* tell zebra to re-add the best remote path */
2037
0
  evpn_zebra_reinstall_best_route(bgp, vpn, dest);
2038
0
}
2039
2040
static inline bool bgp_evpn_route_add_l3_ecomm_ok(struct bgpevpn *vpn,
2041
              const struct prefix_evpn *p,
2042
              esi_t *esi)
2043
0
{
2044
0
  return p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
2045
0
         && (is_evpn_prefix_ipaddr_v4(p)
2046
0
       || (is_evpn_prefix_ipaddr_v6(p)
2047
0
           && !IN6_IS_ADDR_LINKLOCAL(
2048
0
             &p->prefix.macip_addr.ip.ipaddr_v6)))
2049
0
         && CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)
2050
0
         && bgpevpn_get_l3vni(vpn) && bgp_evpn_es_add_l3_ecomm_ok(esi);
2051
0
}
2052
2053
/*
2054
 * Create or update EVPN route (of type based on prefix) for specified VNI
2055
 * and schedule for processing.
2056
 */
2057
static int update_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
2058
           struct prefix_evpn *p, uint8_t flags,
2059
           uint32_t seq, esi_t *esi)
2060
0
{
2061
0
  struct bgp_dest *dest;
2062
0
  struct attr attr;
2063
0
  struct attr *attr_new;
2064
0
  int add_l3_ecomm = 0;
2065
0
  struct bgp_path_info *pi;
2066
0
  afi_t afi = AFI_L2VPN;
2067
0
  safi_t safi = SAFI_EVPN;
2068
0
  int route_change;
2069
0
  bool old_is_sync = false;
2070
0
  bool mac_only = false;
2071
2072
0
  memset(&attr, 0, sizeof(attr));
2073
2074
  /* Build path-attribute for this route. */
2075
0
  bgp_attr_default_set(&attr, bgp, BGP_ORIGIN_IGP);
2076
0
  attr.nexthop = vpn->originator_ip;
2077
0
  attr.mp_nexthop_global_in = vpn->originator_ip;
2078
0
  attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
2079
0
  attr.sticky = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY) ? 1 : 0;
2080
0
  attr.default_gw = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW) ? 1 : 0;
2081
0
  attr.router_flag = CHECK_FLAG(flags,
2082
0
              ZEBRA_MACIP_TYPE_ROUTER_FLAG) ? 1 : 0;
2083
0
  if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT))
2084
0
    attr.es_flags |= ATTR_ES_PROXY_ADVERT;
2085
2086
0
  if (esi && bgp_evpn_is_esi_valid(esi)) {
2087
0
    memcpy(&attr.esi, esi, sizeof(esi_t));
2088
0
    attr.es_flags |= ATTR_ES_IS_LOCAL;
2089
0
  }
2090
2091
  /* PMSI is only needed for type-3 routes */
2092
0
  if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
2093
0
    attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL);
2094
0
    bgp_attr_set_pmsi_tnl_type(&attr, PMSI_TNLTYPE_INGR_REPL);
2095
0
  }
2096
2097
  /* router mac is only needed for type-2 routes here. */
2098
0
  if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
2099
0
    uint8_t af_flags = 0;
2100
2101
0
    if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_SVI_IP))
2102
0
      SET_FLAG(af_flags, BGP_EVPN_MACIP_TYPE_SVI_IP);
2103
2104
0
    bgp_evpn_get_rmac_nexthop(vpn, p, &attr, af_flags);
2105
0
  }
2106
2107
0
  if (bgp_debug_zebra(NULL)) {
2108
0
    char buf3[ESI_STR_LEN];
2109
2110
0
    zlog_debug(
2111
0
      "VRF %s vni %u type-%u route evp %pFX RMAC %pEA nexthop %pI4 esi %s",
2112
0
      vpn->bgp_vrf ? vrf_id_to_name(vpn->bgp_vrf->vrf_id)
2113
0
             : "None",
2114
0
      vpn->vni, p->prefix.route_type, p, &attr.rmac,
2115
0
      &attr.mp_nexthop_global_in,
2116
0
      esi_to_str(esi, buf3, sizeof(buf3)));
2117
0
  }
2118
2119
0
  vni2label(vpn->vni, &(attr.label));
2120
2121
  /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
2122
   * IPv4 or IPv6 global addresses and we're advertising L3VNI with
2123
   * these routes.
2124
   */
2125
0
  add_l3_ecomm = bgp_evpn_route_add_l3_ecomm_ok(
2126
0
    vpn, p, (attr.es_flags & ATTR_ES_IS_LOCAL) ? &attr.esi : NULL);
2127
2128
  /* Set up extended community. */
2129
0
  build_evpn_route_extcomm(vpn, &attr, add_l3_ecomm);
2130
2131
  /* First, create (or fetch) route node within the VNI.
2132
   * NOTE: There is no RD here.
2133
   */
2134
0
  dest = bgp_evpn_vni_node_get(vpn, p, NULL);
2135
2136
0
  if ((p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) &&
2137
0
      (is_evpn_prefix_ipaddr_none(p) == true))
2138
0
    mac_only = true;
2139
2140
  /* Create or update route entry. */
2141
0
  route_change = update_evpn_route_entry(
2142
0
    bgp, vpn, afi, safi, dest, &attr,
2143
0
    (mac_only ? NULL : &p->prefix.macip_addr.mac), NULL /* ip */, 1,
2144
0
    &pi, flags, seq, true /* setup_sync */, &old_is_sync);
2145
0
  assert(pi);
2146
0
  attr_new = pi->attr;
2147
2148
  /* lock ri to prevent freeing in evpn_route_select_install */
2149
0
  bgp_path_info_lock(pi);
2150
2151
       /* Perform route selection. Normally, the local route in the
2152
        * VNI is expected to win and be the best route. However, if
2153
        * there is a race condition where a host moved from local to
2154
        * remote and the remote route was received in BGP just prior
2155
        * to the local MACIP notification from zebra, the remote
2156
        * route would win, and we should evict the defunct local route
2157
        * and (re)install the remote route into zebra.
2158
  */
2159
0
  evpn_route_select_install(bgp, vpn, dest);
2160
  /*
2161
   * If the new local route was not selected evict it and tell zebra
2162
   * to re-add the best remote dest. BGP doesn't retain non-best local
2163
   * routes.
2164
   */
2165
0
  if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
2166
0
    route_change = 0;
2167
0
  } else {
2168
0
    if (!CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
2169
0
      route_change = 0;
2170
0
      evpn_cleanup_local_non_best_route(bgp, vpn, dest, pi);
2171
0
    } else {
2172
0
      bool new_is_sync;
2173
2174
      /* If the local path already existed and is still the
2175
       * best path we need to also check if it transitioned
2176
       * from being a sync path to a non-sync path. If it
2177
       * it did we need to notify zebra that the sync-path
2178
       * has been removed.
2179
       */
2180
0
      new_is_sync = bgp_evpn_attr_is_sync(pi->attr);
2181
0
      if (!new_is_sync && old_is_sync)
2182
0
        evpn_zebra_uninstall(bgp, vpn, p, pi, true);
2183
0
    }
2184
0
  }
2185
0
  bgp_path_info_unlock(pi);
2186
2187
0
  bgp_dest_unlock_node(dest);
2188
2189
  /* If this is a new route or some attribute has changed, export the
2190
   * route to the global table. The route will be advertised to peers
2191
   * from there. Note that this table is a 2-level tree (RD-level +
2192
   * Prefix-level) similar to L3VPN routes.
2193
   */
2194
0
  if (route_change) {
2195
0
    struct bgp_path_info *global_pi;
2196
2197
0
    dest = bgp_evpn_global_node_get(bgp->rib[afi][safi], afi, safi,
2198
0
            p, &vpn->prd, NULL);
2199
0
    update_evpn_route_entry(
2200
0
      bgp, vpn, afi, safi, dest, attr_new, NULL /* mac */,
2201
0
      NULL /* ip */, 1, &global_pi, flags, seq,
2202
0
      false /* setup_sync */, NULL /* old_is_sync */);
2203
2204
    /* Schedule for processing and unlock node. */
2205
0
    bgp_process(bgp, dest, afi, safi);
2206
0
    bgp_dest_unlock_node(dest);
2207
0
  }
2208
2209
  /* Unintern temporary. */
2210
0
  aspath_unintern(&attr.aspath);
2211
2212
0
  return 0;
2213
0
}
2214
2215
/*
2216
 * Delete EVPN route entry.
2217
 * The entry can be in ESI/VNI table or the global table.
2218
 */
2219
void delete_evpn_route_entry(struct bgp *bgp, afi_t afi, safi_t safi,
2220
            struct bgp_dest *dest,
2221
            struct bgp_path_info **pi)
2222
0
{
2223
0
  struct bgp_path_info *tmp_pi;
2224
2225
0
  *pi = NULL;
2226
2227
  /* Now, find matching route. */
2228
0
  for (tmp_pi = bgp_dest_get_bgp_path_info(dest); tmp_pi;
2229
0
       tmp_pi = tmp_pi->next)
2230
0
    if (tmp_pi->peer == bgp->peer_self
2231
0
        && tmp_pi->type == ZEBRA_ROUTE_BGP
2232
0
        && tmp_pi->sub_type == BGP_ROUTE_STATIC)
2233
0
      break;
2234
2235
0
  *pi = tmp_pi;
2236
2237
  /* Mark route for delete. */
2238
0
  if (tmp_pi)
2239
0
    bgp_path_info_delete(dest, tmp_pi);
2240
0
}
2241
2242
/* Delete EVPN type5 route */
2243
static int delete_evpn_type5_route(struct bgp *bgp_vrf, struct prefix_evpn *evp)
2244
0
{
2245
0
  afi_t afi = AFI_L2VPN;
2246
0
  safi_t safi = SAFI_EVPN;
2247
0
  struct bgp_dest *dest = NULL;
2248
0
  struct bgp_path_info *pi = NULL;
2249
0
  struct bgp *bgp_evpn = NULL; /* evpn bgp instance */
2250
2251
0
  bgp_evpn = bgp_get_evpn();
2252
0
  if (!bgp_evpn)
2253
0
    return 0;
2254
2255
  /* locate the global route entry for this type-5 prefix */
2256
0
  dest = bgp_evpn_global_node_lookup(bgp_evpn->rib[afi][safi], safi, evp,
2257
0
             &bgp_vrf->vrf_prd, NULL);
2258
0
  if (!dest)
2259
0
    return 0;
2260
2261
0
  delete_evpn_route_entry(bgp_evpn, afi, safi, dest, &pi);
2262
0
  if (pi)
2263
0
    bgp_process(bgp_evpn, dest, afi, safi);
2264
0
  bgp_dest_unlock_node(dest);
2265
0
  return 0;
2266
0
}
2267
2268
/*
2269
 * Delete EVPN route (of type based on prefix) for specified VNI and
2270
 * schedule for processing.
2271
 */
2272
static int delete_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
2273
           struct prefix_evpn *p)
2274
0
{
2275
0
  struct bgp_dest *dest, *global_dest;
2276
0
  struct bgp_path_info *pi;
2277
0
  afi_t afi = AFI_L2VPN;
2278
0
  safi_t safi = SAFI_EVPN;
2279
2280
  /* First, locate the route node within the VNI. If it doesn't exist,
2281
   * there
2282
   * is nothing further to do.
2283
   * NOTE: There is no RD here.
2284
   */
2285
0
  dest = bgp_evpn_vni_node_lookup(vpn, p, NULL);
2286
0
  if (!dest)
2287
0
    return 0;
2288
2289
  /* Next, locate route node in the global EVPN routing table. Note that
2290
   * this table is a 2-level tree (RD-level + Prefix-level) similar to
2291
   * L3VPN routes.
2292
   */
2293
0
  global_dest = bgp_evpn_global_node_lookup(bgp->rib[afi][safi], safi, p,
2294
0
              &vpn->prd, NULL);
2295
0
  if (global_dest) {
2296
    /* Delete route entry in the global EVPN table. */
2297
0
    delete_evpn_route_entry(bgp, afi, safi, global_dest, &pi);
2298
2299
    /* Schedule for processing - withdraws to peers happen from
2300
     * this table.
2301
     */
2302
0
    if (pi)
2303
0
      bgp_process(bgp, global_dest, afi, safi);
2304
0
    bgp_dest_unlock_node(global_dest);
2305
0
  }
2306
2307
  /* Delete route entry in the VNI route table. This can just be removed.
2308
   */
2309
0
  delete_evpn_route_entry(bgp, afi, safi, dest, &pi);
2310
0
  if (pi) {
2311
0
    bgp_path_info_reap(dest, pi);
2312
0
    evpn_route_select_install(bgp, vpn, dest);
2313
0
  }
2314
0
  bgp_dest_unlock_node(dest);
2315
2316
0
  return 0;
2317
0
}
2318
2319
void bgp_evpn_update_type2_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
2320
               struct bgp_dest *dest,
2321
               struct bgp_path_info *local_pi,
2322
               const char *caller)
2323
0
{
2324
0
  afi_t afi = AFI_L2VPN;
2325
0
  safi_t safi = SAFI_EVPN;
2326
0
  struct bgp_path_info *pi;
2327
0
  struct attr attr;
2328
0
  struct attr *attr_new;
2329
0
  uint32_t seq;
2330
0
  int add_l3_ecomm = 0;
2331
0
  struct bgp_dest *global_dest;
2332
0
  struct bgp_path_info *global_pi;
2333
0
  struct prefix_evpn evp;
2334
0
  int route_change;
2335
0
  bool old_is_sync = false;
2336
2337
0
  if (CHECK_FLAG(local_pi->flags, BGP_PATH_REMOVED))
2338
0
    return;
2339
2340
  /*
2341
   * VNI table MAC-IP prefixes don't have MAC so make sure it's set from
2342
   * path info here.
2343
   */
2344
0
  if (is_evpn_prefix_ipaddr_none((struct prefix_evpn *)&dest->p)) {
2345
    /* VNI MAC -> Global */
2346
0
    evpn_type2_prefix_global_copy(
2347
0
      &evp, (struct prefix_evpn *)&dest->p, NULL /* mac */,
2348
0
      evpn_type2_path_info_get_ip(local_pi));
2349
0
  } else {
2350
    /* VNI IP -> Global */
2351
0
    evpn_type2_prefix_global_copy(
2352
0
      &evp, (struct prefix_evpn *)&dest->p,
2353
0
      evpn_type2_path_info_get_mac(local_pi), NULL /* ip */);
2354
0
  }
2355
2356
  /*
2357
   * Build attribute per local route as the MAC mobility and
2358
   * some other values could differ for different routes. The
2359
   * attributes will be shared in the hash table.
2360
   */
2361
0
  bgp_attr_default_set(&attr, bgp, BGP_ORIGIN_IGP);
2362
0
  attr.nexthop = vpn->originator_ip;
2363
0
  attr.mp_nexthop_global_in = vpn->originator_ip;
2364
0
  attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
2365
0
  attr.sticky = (local_pi->attr->sticky) ? 1 : 0;
2366
0
  attr.router_flag = (local_pi->attr->router_flag) ? 1 : 0;
2367
0
  attr.es_flags = local_pi->attr->es_flags;
2368
0
  if (local_pi->attr->default_gw) {
2369
0
    attr.default_gw = 1;
2370
0
    if (is_evpn_prefix_ipaddr_v6(&evp))
2371
0
      attr.router_flag = 1;
2372
0
  }
2373
0
  memcpy(&attr.esi, &local_pi->attr->esi, sizeof(esi_t));
2374
0
  bgp_evpn_get_rmac_nexthop(vpn, &evp, &attr, local_pi->extra->af_flags);
2375
0
  vni2label(vpn->vni, &(attr.label));
2376
  /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2377
   * using L3 VNI for type-2 routes also.
2378
   */
2379
0
  add_l3_ecomm = bgp_evpn_route_add_l3_ecomm_ok(
2380
0
    vpn, &evp,
2381
0
    (attr.es_flags & ATTR_ES_IS_LOCAL) ? &attr.esi : NULL);
2382
2383
  /* Set up extended community. */
2384
0
  build_evpn_route_extcomm(vpn, &attr, add_l3_ecomm);
2385
0
  seq = mac_mobility_seqnum(local_pi->attr);
2386
2387
0
  if (bgp_debug_zebra(NULL)) {
2388
0
    char buf3[ESI_STR_LEN];
2389
2390
0
    zlog_debug(
2391
0
      "VRF %s vni %u evp %pFX RMAC %pEA nexthop %pI4 esi %s esf 0x%x from %s",
2392
0
      vpn->bgp_vrf ? vrf_id_to_name(vpn->bgp_vrf->vrf_id)
2393
0
             : " ",
2394
0
      vpn->vni, &evp, &attr.rmac, &attr.mp_nexthop_global_in,
2395
0
      esi_to_str(&attr.esi, buf3, sizeof(buf3)),
2396
0
      attr.es_flags, caller);
2397
0
  }
2398
2399
  /* Update the route entry. */
2400
0
  route_change = update_evpn_route_entry(
2401
0
    bgp, vpn, afi, safi, dest, &attr, NULL /* mac */, NULL /* ip */,
2402
0
    0, &pi, 0, seq, true /* setup_sync */, &old_is_sync);
2403
2404
0
  assert(pi);
2405
0
  attr_new = pi->attr;
2406
  /* lock ri to prevent freeing in evpn_route_select_install */
2407
0
  bgp_path_info_lock(pi);
2408
2409
  /* Perform route selection. Normally, the local route in the
2410
   * VNI is expected to win and be the best route. However,
2411
   * under peculiar situations (e.g., tunnel (next hop) IP change
2412
   * that causes best selection to be based on next hop), a
2413
   * remote route could win. If the local route is the best,
2414
   * ensure it is updated in the global EVPN route table and
2415
   * advertised to peers; otherwise, ensure it is evicted and
2416
   * (re)install the remote route into zebra.
2417
   */
2418
0
  evpn_route_select_install(bgp, vpn, dest);
2419
2420
0
  if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
2421
0
    route_change = 0;
2422
0
  } else {
2423
0
    if (!CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
2424
0
      route_change = 0;
2425
0
      evpn_cleanup_local_non_best_route(bgp, vpn, dest, pi);
2426
0
    } else {
2427
0
      bool new_is_sync;
2428
2429
      /* If the local path already existed and is still the
2430
       * best path we need to also check if it transitioned
2431
       * from being a sync path to a non-sync path. If it
2432
       * it did we need to notify zebra that the sync-path
2433
       * has been removed.
2434
       */
2435
0
      new_is_sync = bgp_evpn_attr_is_sync(pi->attr);
2436
0
      if (!new_is_sync && old_is_sync)
2437
0
        evpn_zebra_uninstall(bgp, vpn, &evp, pi, true);
2438
0
    }
2439
0
  }
2440
2441
2442
  /* unlock pi */
2443
0
  bgp_path_info_unlock(pi);
2444
2445
0
  if (route_change) {
2446
    /* Update route in global routing table. */
2447
0
    global_dest = bgp_evpn_global_node_get(
2448
0
      bgp->rib[afi][safi], afi, safi, &evp, &vpn->prd, NULL);
2449
0
    assert(global_dest);
2450
0
    update_evpn_route_entry(
2451
0
      bgp, vpn, afi, safi, global_dest, attr_new,
2452
0
      NULL /* mac */, NULL /* ip */, 0, &global_pi, 0,
2453
0
      mac_mobility_seqnum(attr_new), false /* setup_sync */,
2454
0
      NULL /* old_is_sync */);
2455
2456
    /* Schedule for processing and unlock node. */
2457
0
    bgp_process(bgp, global_dest, afi, safi);
2458
0
    bgp_dest_unlock_node(global_dest);
2459
0
  }
2460
2461
  /* Unintern temporary. */
2462
0
  aspath_unintern(&attr.aspath);
2463
0
}
2464
2465
static void update_type2_route(struct bgp *bgp, struct bgpevpn *vpn,
2466
             struct bgp_dest *dest)
2467
0
{
2468
0
  struct bgp_path_info *tmp_pi;
2469
2470
0
  const struct prefix_evpn *evp =
2471
0
    (const struct prefix_evpn *)bgp_dest_get_prefix(dest);
2472
2473
0
  if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2474
0
    return;
2475
2476
  /* Identify local route. */
2477
0
  for (tmp_pi = bgp_dest_get_bgp_path_info(dest); tmp_pi;
2478
0
       tmp_pi = tmp_pi->next) {
2479
0
    if (tmp_pi->peer == bgp->peer_self &&
2480
0
        tmp_pi->type == ZEBRA_ROUTE_BGP &&
2481
0
        tmp_pi->sub_type == BGP_ROUTE_STATIC)
2482
0
      break;
2483
0
  }
2484
2485
0
  if (!tmp_pi)
2486
0
    return;
2487
2488
0
  bgp_evpn_update_type2_route_entry(bgp, vpn, dest, tmp_pi, __func__);
2489
0
}
2490
2491
/*
2492
 * Update all type-2 (MACIP) local routes for this VNI - these should also
2493
 * be scheduled for advertise to peers.
2494
 */
2495
static void update_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
2496
0
{
2497
0
  struct bgp_dest *dest;
2498
2499
  /* Walk this VNI's route MAC & IP table and update local type-2
2500
   * routes. For any routes updated, update corresponding entry in the
2501
   * global table too.
2502
   */
2503
0
  for (dest = bgp_table_top(vpn->mac_table); dest;
2504
0
       dest = bgp_route_next(dest))
2505
0
    update_type2_route(bgp, vpn, dest);
2506
2507
0
  for (dest = bgp_table_top(vpn->ip_table); dest;
2508
0
       dest = bgp_route_next(dest))
2509
0
    update_type2_route(bgp, vpn, dest);
2510
0
}
2511
2512
/*
2513
 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2514
 * global routing table. These are also scheduled for withdraw from peers.
2515
 */
2516
static void delete_global_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
2517
0
{
2518
0
  afi_t afi;
2519
0
  safi_t safi;
2520
0
  struct bgp_dest *rddest, *dest;
2521
0
  struct bgp_table *table;
2522
0
  struct bgp_path_info *pi;
2523
2524
0
  afi = AFI_L2VPN;
2525
0
  safi = SAFI_EVPN;
2526
2527
0
  rddest = bgp_node_lookup(bgp->rib[afi][safi],
2528
0
         (struct prefix *)&vpn->prd);
2529
0
  if (rddest) {
2530
0
    table = bgp_dest_get_bgp_table_info(rddest);
2531
0
    for (dest = bgp_table_top(table); dest;
2532
0
         dest = bgp_route_next(dest)) {
2533
0
      const struct prefix_evpn *evp =
2534
0
        (const struct prefix_evpn *)bgp_dest_get_prefix(
2535
0
          dest);
2536
2537
0
      if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2538
0
        continue;
2539
2540
0
      delete_evpn_route_entry(bgp, afi, safi, dest, &pi);
2541
0
      if (pi)
2542
0
        bgp_process(bgp, dest, afi, safi);
2543
0
    }
2544
2545
    /* Unlock RD node. */
2546
0
    bgp_dest_unlock_node(rddest);
2547
0
  }
2548
0
}
2549
2550
static void delete_vni_type2_route(struct bgp *bgp, struct bgp_dest *dest)
2551
0
{
2552
0
  struct bgp_path_info *pi;
2553
0
  afi_t afi = AFI_L2VPN;
2554
0
  safi_t safi = SAFI_EVPN;
2555
2556
0
  const struct prefix_evpn *evp =
2557
0
    (const struct prefix_evpn *)bgp_dest_get_prefix(dest);
2558
2559
0
  if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2560
0
    return;
2561
2562
0
  delete_evpn_route_entry(bgp, afi, safi, dest, &pi);
2563
2564
  /* Route entry in local table gets deleted immediately. */
2565
0
  if (pi)
2566
0
    bgp_path_info_reap(dest, pi);
2567
0
}
2568
2569
static void delete_vni_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
2570
0
{
2571
0
  struct bgp_dest *dest;
2572
2573
  /* Next, walk this VNI's MAC & IP route table and delete local type-2
2574
   * routes.
2575
   */
2576
0
  for (dest = bgp_table_top(vpn->mac_table); dest;
2577
0
       dest = bgp_route_next(dest))
2578
0
    delete_vni_type2_route(bgp, dest);
2579
2580
0
  for (dest = bgp_table_top(vpn->ip_table); dest;
2581
0
       dest = bgp_route_next(dest))
2582
0
    delete_vni_type2_route(bgp, dest);
2583
0
}
2584
2585
/*
2586
 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2587
 * table as well as the per-VNI route table.
2588
 */
2589
static void delete_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
2590
0
{
2591
  /* First, walk the global route table for this VNI's type-2 local
2592
   * routes.
2593
   * EVPN routes are a 2-level table, first get the RD table.
2594
   */
2595
0
  delete_global_type2_routes(bgp, vpn);
2596
0
  delete_vni_type2_routes(bgp, vpn);
2597
0
}
2598
2599
/*
2600
 * Delete all routes in the per-VNI route table.
2601
 */
2602
static void delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
2603
0
{
2604
0
  struct bgp_dest *dest;
2605
0
  struct bgp_path_info *pi, *nextpi;
2606
2607
  /* Walk this VNI's MAC & IP route table and delete all routes. */
2608
0
  for (dest = bgp_table_top(vpn->mac_table); dest;
2609
0
       dest = bgp_route_next(dest)) {
2610
0
    for (pi = bgp_dest_get_bgp_path_info(dest);
2611
0
         (pi != NULL) && (nextpi = pi->next, 1); pi = nextpi) {
2612
0
      bgp_evpn_remote_ip_hash_del(vpn, pi);
2613
0
      bgp_path_info_delete(dest, pi);
2614
0
      bgp_path_info_reap(dest, pi);
2615
0
    }
2616
0
  }
2617
2618
0
  for (dest = bgp_table_top(vpn->ip_table); dest;
2619
0
       dest = bgp_route_next(dest)) {
2620
0
    for (pi = bgp_dest_get_bgp_path_info(dest);
2621
0
         (pi != NULL) && (nextpi = pi->next, 1); pi = nextpi) {
2622
0
      bgp_path_info_delete(dest, pi);
2623
0
      bgp_path_info_reap(dest, pi);
2624
0
    }
2625
0
  }
2626
0
}
2627
2628
/* BUM traffic flood mode per-l2-vni */
2629
static int bgp_evpn_vni_flood_mode_get(struct bgp *bgp,
2630
          struct bgpevpn *vpn)
2631
0
{
2632
  /* if flooding has been globally disabled per-vni mode is
2633
   * not relevant
2634
   */
2635
0
  if (bgp->vxlan_flood_ctrl == VXLAN_FLOOD_DISABLED)
2636
0
    return VXLAN_FLOOD_DISABLED;
2637
2638
  /* if mcast group ip has been specified we use a PIM-SM MDT */
2639
0
  if (vpn->mcast_grp.s_addr != INADDR_ANY)
2640
0
    return VXLAN_FLOOD_PIM_SM;
2641
2642
  /* default is ingress replication */
2643
0
  return VXLAN_FLOOD_HEAD_END_REPL;
2644
0
}
2645
2646
/*
2647
 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2648
 * export RT getting modified or change to tunnel IP. Note that these
2649
 * situations need the route in the per-VNI table as well as the global
2650
 * table to be updated (as attributes change).
2651
 */
2652
int update_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
2653
0
{
2654
0
  int ret;
2655
0
  struct prefix_evpn p;
2656
2657
0
  update_type1_routes_for_evi(bgp, vpn);
2658
2659
  /* Update and advertise the type-3 route (only one) followed by the
2660
   * locally learnt type-2 routes (MACIP) - for this VNI.
2661
   *
2662
   * RT-3 only if doing head-end replication
2663
   */
2664
0
  if (bgp_evpn_vni_flood_mode_get(bgp, vpn)
2665
0
        == VXLAN_FLOOD_HEAD_END_REPL) {
2666
0
    build_evpn_type3_prefix(&p, vpn->originator_ip);
2667
0
    ret = update_evpn_route(bgp, vpn, &p, 0, 0, NULL);
2668
0
    if (ret)
2669
0
      return ret;
2670
0
  }
2671
2672
0
  update_all_type2_routes(bgp, vpn);
2673
0
  return 0;
2674
0
}
2675
2676
/*
2677
 * Delete (and withdraw) local routes for specified VNI from the global
2678
 * table and per-VNI table. After this, remove all other routes from
2679
 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2680
 * (advertise-all-vni) being disabled.
2681
 */
2682
static int delete_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
2683
0
{
2684
0
  int ret;
2685
0
  struct prefix_evpn p;
2686
2687
  /* Delete and withdraw locally learnt type-2 routes (MACIP)
2688
   * followed by type-3 routes (only one) - for this VNI.
2689
   */
2690
0
  delete_all_type2_routes(bgp, vpn);
2691
2692
0
  build_evpn_type3_prefix(&p, vpn->originator_ip);
2693
0
  ret = delete_evpn_route(bgp, vpn, &p);
2694
0
  if (ret)
2695
0
    return ret;
2696
2697
  /* Delete all routes from the per-VNI table. */
2698
0
  delete_all_vni_routes(bgp, vpn);
2699
0
  return 0;
2700
0
}
2701
2702
/*
2703
 * There is a flood mcast IP address change. Update the mcast-grp and
2704
 * remove the type-3 route if any. A new type-3 route will be generated
2705
 * post tunnel_ip update if the new flood mode is head-end-replication.
2706
 */
2707
static int bgp_evpn_mcast_grp_change(struct bgp *bgp, struct bgpevpn *vpn,
2708
    struct in_addr mcast_grp)
2709
0
{
2710
0
  struct prefix_evpn p;
2711
2712
0
  vpn->mcast_grp = mcast_grp;
2713
2714
0
  if (is_vni_live(vpn)) {
2715
0
    build_evpn_type3_prefix(&p, vpn->originator_ip);
2716
0
    delete_evpn_route(bgp, vpn, &p);
2717
0
  }
2718
2719
0
  return 0;
2720
0
}
2721
2722
/*
2723
 * There is a tunnel endpoint IP address change for this VNI, delete
2724
 * prior type-3 route (if needed) and update.
2725
 * Note: Route re-advertisement happens elsewhere after other processing
2726
 * other changes.
2727
 */
2728
static void handle_tunnel_ip_change(struct bgp *bgp, struct bgpevpn *vpn,
2729
            struct in_addr originator_ip)
2730
0
{
2731
0
  struct prefix_evpn p;
2732
2733
0
  if (IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip))
2734
0
    return;
2735
2736
  /* If VNI is not live, we only need to update the originator ip */
2737
0
  if (!is_vni_live(vpn)) {
2738
0
    vpn->originator_ip = originator_ip;
2739
0
    return;
2740
0
  }
2741
2742
  /* Update the tunnel-ip hash */
2743
0
  bgp_tip_del(bgp, &vpn->originator_ip);
2744
0
  if (bgp_tip_add(bgp, &originator_ip))
2745
    /* The originator_ip was not already present in the
2746
     * bgp martian next-hop table as a tunnel-ip, so we
2747
     * need to go back and filter routes matching the new
2748
     * martian next-hop.
2749
     */
2750
0
    bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
2751
2752
  /* Need to withdraw type-3 route as the originator IP is part
2753
   * of the key.
2754
   */
2755
0
  build_evpn_type3_prefix(&p, vpn->originator_ip);
2756
0
  delete_evpn_route(bgp, vpn, &p);
2757
2758
  /* Update the tunnel IP and re-advertise all routes for this VNI. */
2759
0
  vpn->originator_ip = originator_ip;
2760
0
  return;
2761
0
}
2762
2763
static struct bgp_path_info *
2764
bgp_create_evpn_bgp_path_info(struct bgp_path_info *parent_pi,
2765
            struct bgp_dest *dest, struct attr *attr)
2766
0
{
2767
0
  struct attr *attr_new;
2768
0
  struct bgp_path_info *pi;
2769
2770
  /* Add (or update) attribute to hash. */
2771
0
  attr_new = bgp_attr_intern(attr);
2772
2773
  /* Create new route with its attribute. */
2774
0
  pi = info_make(parent_pi->type, BGP_ROUTE_IMPORTED, 0, parent_pi->peer,
2775
0
           attr_new, dest);
2776
0
  SET_FLAG(pi->flags, BGP_PATH_VALID);
2777
0
  bgp_path_info_extra_get(pi);
2778
0
  pi->extra->parent = bgp_path_info_lock(parent_pi);
2779
0
  bgp_dest_lock_node((struct bgp_dest *)parent_pi->net);
2780
0
  if (parent_pi->extra) {
2781
0
    memcpy(&pi->extra->label, &parent_pi->extra->label,
2782
0
           sizeof(pi->extra->label));
2783
0
    pi->extra->num_labels = parent_pi->extra->num_labels;
2784
0
    pi->extra->igpmetric = parent_pi->extra->igpmetric;
2785
0
  }
2786
2787
0
  bgp_path_info_add(dest, pi);
2788
2789
0
  return pi;
2790
0
}
2791
2792
/*
2793
 * Install route entry into the VRF routing table and invoke route selection.
2794
 */
2795
static int install_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
2796
             const struct prefix_evpn *evp,
2797
             struct bgp_path_info *parent_pi)
2798
0
{
2799
0
  struct bgp_dest *dest;
2800
0
  struct bgp_path_info *pi;
2801
0
  struct attr attr;
2802
0
  struct attr *attr_new;
2803
0
  int ret = 0;
2804
0
  struct prefix p;
2805
0
  struct prefix *pp = &p;
2806
0
  afi_t afi = 0;
2807
0
  safi_t safi = 0;
2808
0
  bool new_pi = false;
2809
0
  bool use_l3nhg = false;
2810
0
  bool is_l3nhg_active = false;
2811
0
  char buf1[INET6_ADDRSTRLEN];
2812
2813
0
  memset(pp, 0, sizeof(struct prefix));
2814
0
  ip_prefix_from_evpn_prefix(evp, pp);
2815
2816
0
  if (bgp_debug_zebra(NULL))
2817
0
    zlog_debug(
2818
0
      "vrf %s: import evpn prefix %pFX parent %p flags 0x%x",
2819
0
      vrf_id_to_name(bgp_vrf->vrf_id), evp, parent_pi,
2820
0
      parent_pi->flags);
2821
2822
  /* Create (or fetch) route within the VRF. */
2823
  /* NOTE: There is no RD here. */
2824
0
  if (is_evpn_prefix_ipaddr_v4(evp)) {
2825
0
    afi = AFI_IP;
2826
0
    safi = SAFI_UNICAST;
2827
0
    dest = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
2828
0
  } else if (is_evpn_prefix_ipaddr_v6(evp)) {
2829
0
    afi = AFI_IP6;
2830
0
    safi = SAFI_UNICAST;
2831
0
    dest = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
2832
0
  } else
2833
0
    return 0;
2834
2835
  /* EVPN routes currently only support a IPv4 next hop which corresponds
2836
   * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2837
   * or prefix route, we have to convert the next hop to an IPv4-mapped
2838
   * address for the rest of the code to flow through. In the case of IPv4,
2839
   * make sure to set the flag for next hop attribute.
2840
   */
2841
0
  attr = *parent_pi->attr;
2842
0
  if (attr.evpn_overlay.type != OVERLAY_INDEX_GATEWAY_IP) {
2843
0
    if (afi == AFI_IP6)
2844
0
      evpn_convert_nexthop_to_ipv6(&attr);
2845
0
    else {
2846
0
      attr.nexthop = attr.mp_nexthop_global_in;
2847
0
      attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP);
2848
0
    }
2849
0
  } else {
2850
2851
    /*
2852
     * If gateway IP overlay index is specified in the NLRI of
2853
     * EVPN RT-5, this gateway IP should be used as the nexthop
2854
     * for the prefix in the VRF
2855
     */
2856
0
    if (bgp_debug_zebra(NULL)) {
2857
0
      zlog_debug(
2858
0
        "Install gateway IP %s as nexthop for prefix %pFX in vrf %s",
2859
0
        inet_ntop(pp->family, &attr.evpn_overlay.gw_ip,
2860
0
            buf1, sizeof(buf1)), pp,
2861
0
            vrf_id_to_name(bgp_vrf->vrf_id));
2862
0
    }
2863
2864
0
    if (afi == AFI_IP6) {
2865
0
      memcpy(&attr.mp_nexthop_global,
2866
0
             &attr.evpn_overlay.gw_ip.ipaddr_v6,
2867
0
             sizeof(struct in6_addr));
2868
0
      attr.mp_nexthop_len = IPV6_MAX_BYTELEN;
2869
0
    } else {
2870
0
      attr.nexthop = attr.evpn_overlay.gw_ip.ipaddr_v4;
2871
0
      attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP);
2872
0
    }
2873
0
  }
2874
2875
0
  bgp_evpn_es_vrf_use_nhg(bgp_vrf, &parent_pi->attr->esi, &use_l3nhg,
2876
0
        &is_l3nhg_active, NULL);
2877
0
  if (use_l3nhg)
2878
0
    attr.es_flags |= ATTR_ES_L3_NHG_USE;
2879
0
  if (is_l3nhg_active)
2880
0
    attr.es_flags |= ATTR_ES_L3_NHG_ACTIVE;
2881
2882
  /* Check if route entry is already present. */
2883
0
  for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
2884
0
    if (pi->extra
2885
0
        && (struct bgp_path_info *)pi->extra->parent == parent_pi)
2886
0
      break;
2887
2888
0
  if (!pi) {
2889
0
    pi = bgp_create_evpn_bgp_path_info(parent_pi, dest, &attr);
2890
0
    new_pi = true;
2891
0
  } else {
2892
0
    if (attrhash_cmp(pi->attr, &attr)
2893
0
        && !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
2894
0
      bgp_dest_unlock_node(dest);
2895
0
      return 0;
2896
0
    }
2897
    /* The attribute has changed. */
2898
    /* Add (or update) attribute to hash. */
2899
0
    attr_new = bgp_attr_intern(&attr);
2900
2901
    /* Restore route, if needed. */
2902
0
    if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
2903
0
      bgp_path_info_restore(dest, pi);
2904
2905
    /* Mark if nexthop has changed. */
2906
0
    if ((afi == AFI_IP
2907
0
         && !IPV4_ADDR_SAME(&pi->attr->nexthop, &attr_new->nexthop))
2908
0
        || (afi == AFI_IP6
2909
0
      && !IPV6_ADDR_SAME(&pi->attr->mp_nexthop_global,
2910
0
             &attr_new->mp_nexthop_global)))
2911
0
      SET_FLAG(pi->flags, BGP_PATH_IGP_CHANGED);
2912
2913
0
    bgp_path_info_set_flag(dest, pi, BGP_PATH_ATTR_CHANGED);
2914
    /* Unintern existing, set to new. */
2915
0
    bgp_attr_unintern(&pi->attr);
2916
0
    pi->attr = attr_new;
2917
0
    pi->uptime = monotime(NULL);
2918
0
  }
2919
2920
  /* Gateway IP nexthop should be resolved */
2921
0
  if (attr.evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP) {
2922
0
    if (bgp_find_or_add_nexthop(bgp_vrf, bgp_vrf, afi, safi, pi,
2923
0
              NULL, 0, NULL))
2924
0
      bgp_path_info_set_flag(dest, pi, BGP_PATH_VALID);
2925
0
    else {
2926
0
      if (BGP_DEBUG(nht, NHT)) {
2927
0
        inet_ntop(pp->family,
2928
0
            &attr.evpn_overlay.gw_ip,
2929
0
            buf1, sizeof(buf1));
2930
0
        zlog_debug("%s: gateway IP NH unresolved",
2931
0
             buf1);
2932
0
      }
2933
0
      bgp_path_info_unset_flag(dest, pi, BGP_PATH_VALID);
2934
0
    }
2935
0
  } else {
2936
2937
    /* as it is an importation, change nexthop */
2938
0
    bgp_path_info_set_flag(dest, pi, BGP_PATH_ANNC_NH_SELF);
2939
0
  }
2940
2941
  /* Link path to evpn nexthop */
2942
0
  bgp_evpn_path_nh_add(bgp_vrf, pi);
2943
2944
0
  bgp_aggregate_increment(bgp_vrf, bgp_dest_get_prefix(dest), pi, afi,
2945
0
        safi);
2946
2947
  /* Perform route selection and update zebra, if required. */
2948
0
  bgp_process(bgp_vrf, dest, afi, safi);
2949
2950
  /* Process for route leaking. */
2951
0
  vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf, pi);
2952
2953
0
  bgp_dest_unlock_node(dest);
2954
2955
0
  if (bgp_debug_zebra(NULL))
2956
0
    zlog_debug("... %s pi dest %p (l %d) pi %p (l %d, f 0x%x)",
2957
0
         new_pi ? "new" : "update", dest,
2958
0
         bgp_dest_get_lock_count(dest), pi, pi->lock,
2959
0
         pi->flags);
2960
2961
0
  return ret;
2962
0
}
2963
2964
/*
2965
 * Common handling for vni route tables install/selection.
2966
 */
2967
static int install_evpn_route_entry_in_vni_common(
2968
  struct bgp *bgp, struct bgpevpn *vpn, const struct prefix_evpn *p,
2969
  struct bgp_dest *dest, struct bgp_path_info *parent_pi)
2970
0
{
2971
0
  struct bgp_path_info *pi;
2972
0
  struct bgp_path_info *local_pi;
2973
0
  struct attr *attr_new;
2974
0
  int ret;
2975
0
  bool old_local_es = false;
2976
0
  bool new_local_es;
2977
2978
  /* Check if route entry is already present. */
2979
0
  for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
2980
0
    if (pi->extra
2981
0
        && (struct bgp_path_info *)pi->extra->parent == parent_pi)
2982
0
      break;
2983
2984
0
  if (!pi) {
2985
    /* Create an info */
2986
0
    pi = bgp_create_evpn_bgp_path_info(parent_pi, dest,
2987
0
                parent_pi->attr);
2988
2989
0
    if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
2990
0
      if (is_evpn_type2_dest_ipaddr_none(dest))
2991
0
        evpn_type2_path_info_set_ip(
2992
0
          pi, p->prefix.macip_addr.ip);
2993
0
      else
2994
0
        evpn_type2_path_info_set_mac(
2995
0
          pi, p->prefix.macip_addr.mac);
2996
0
    }
2997
2998
0
    new_local_es = bgp_evpn_attr_is_local_es(pi->attr);
2999
0
  } else {
3000
    /* Return early if attributes haven't changed
3001
     * and dest isn't flagged for removal.
3002
     * dest will be unlocked by either
3003
     * install_evpn_route_entry_in_vni_mac() or
3004
     * install_evpn_route_entry_in_vni_ip()
3005
     */
3006
0
    if (attrhash_cmp(pi->attr, parent_pi->attr) &&
3007
0
        !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
3008
0
      return 0;
3009
    /* The attribute has changed. */
3010
    /* Add (or update) attribute to hash. */
3011
0
    attr_new = bgp_attr_intern(parent_pi->attr);
3012
3013
    /* Restore route, if needed. */
3014
0
    if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
3015
0
      bgp_path_info_restore(dest, pi);
3016
3017
    /* Mark if nexthop has changed. */
3018
0
    if (!IPV4_ADDR_SAME(&pi->attr->nexthop, &attr_new->nexthop))
3019
0
      SET_FLAG(pi->flags, BGP_PATH_IGP_CHANGED);
3020
3021
0
    old_local_es = bgp_evpn_attr_is_local_es(pi->attr);
3022
0
    new_local_es = bgp_evpn_attr_is_local_es(attr_new);
3023
    /* If ESI is different or if its type has changed we
3024
     * need to reinstall the path in zebra
3025
     */
3026
0
    if ((old_local_es != new_local_es)
3027
0
        || memcmp(&pi->attr->esi, &attr_new->esi,
3028
0
            sizeof(attr_new->esi))) {
3029
3030
0
      if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
3031
0
        zlog_debug("VNI %d path %pFX chg to %s es",
3032
0
             vpn->vni, &pi->net->p,
3033
0
             new_local_es ? "local"
3034
0
              : "non-local");
3035
0
      bgp_path_info_set_flag(dest, pi, BGP_PATH_ATTR_CHANGED);
3036
0
    }
3037
3038
    /* Unintern existing, set to new. */
3039
0
    bgp_attr_unintern(&pi->attr);
3040
0
    pi->attr = attr_new;
3041
0
    pi->uptime = monotime(NULL);
3042
0
  }
3043
3044
  /* Add this route to remote IP hashtable */
3045
0
  bgp_evpn_remote_ip_hash_add(vpn, pi);
3046
3047
  /* Perform route selection and update zebra, if required. */
3048
0
  ret = evpn_route_select_install(bgp, vpn, dest);
3049
3050
  /* if the best path is a local path with a non-zero ES
3051
   * sync info against the local path may need to be updated
3052
   * when a remote path is added/updated (including changes
3053
   * from sync-path to remote-path)
3054
   */
3055
0
  local_pi = bgp_evpn_route_get_local_path(bgp, dest);
3056
0
  if (local_pi && (old_local_es || new_local_es))
3057
0
    bgp_evpn_update_type2_route_entry(bgp, vpn, dest, local_pi,
3058
0
              __func__);
3059
3060
0
  return ret;
3061
0
}
3062
3063
/*
3064
 * Common handling for vni route tables uninstall/selection.
3065
 */
3066
static int uninstall_evpn_route_entry_in_vni_common(
3067
  struct bgp *bgp, struct bgpevpn *vpn, const struct prefix_evpn *p,
3068
  struct bgp_dest *dest, struct bgp_path_info *parent_pi)
3069
0
{
3070
0
  struct bgp_path_info *pi;
3071
0
  struct bgp_path_info *local_pi;
3072
0
  int ret;
3073
3074
  /* Find matching route entry. */
3075
0
  for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
3076
0
    if (pi->extra &&
3077
0
        (struct bgp_path_info *)pi->extra->parent == parent_pi)
3078
0
      break;
3079
3080
0
  if (!pi)
3081
0
    return 0;
3082
3083
0
  bgp_evpn_remote_ip_hash_del(vpn, pi);
3084
3085
  /* Mark entry for deletion */
3086
0
  bgp_path_info_delete(dest, pi);
3087
3088
  /* Perform route selection and update zebra, if required. */
3089
0
  ret = evpn_route_select_install(bgp, vpn, dest);
3090
3091
  /* if the best path is a local path with a non-zero ES
3092
   * sync info against the local path may need to be updated
3093
   * when a remote path is deleted
3094
   */
3095
0
  local_pi = bgp_evpn_route_get_local_path(bgp, dest);
3096
0
  if (local_pi && bgp_evpn_attr_is_local_es(local_pi->attr))
3097
0
    bgp_evpn_update_type2_route_entry(bgp, vpn, dest, local_pi,
3098
0
              __func__);
3099
3100
0
  return ret;
3101
0
}
3102
3103
/*
3104
 * Install route entry into VNI IP table and invoke route selection.
3105
 */
3106
static int install_evpn_route_entry_in_vni_ip(struct bgp *bgp,
3107
                struct bgpevpn *vpn,
3108
                const struct prefix_evpn *p,
3109
                struct bgp_path_info *parent_pi)
3110
0
{
3111
0
  int ret;
3112
0
  struct bgp_dest *dest;
3113
3114
  /* Ignore MAC Only Type-2 */
3115
0
  if ((p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) &&
3116
0
      (is_evpn_prefix_ipaddr_none(p) == true))
3117
0
    return 0;
3118
3119
  /* Create (or fetch) route within the VNI IP table. */
3120
0
  dest = bgp_evpn_vni_ip_node_get(vpn->ip_table, p, parent_pi);
3121
3122
0
  ret = install_evpn_route_entry_in_vni_common(bgp, vpn, p, dest,
3123
0
                 parent_pi);
3124
3125
0
  bgp_dest_unlock_node(dest);
3126
3127
0
  return ret;
3128
0
}
3129
3130
/*
3131
 * Install route entry into VNI MAC table and invoke route selection.
3132
 */
3133
static int install_evpn_route_entry_in_vni_mac(struct bgp *bgp,
3134
                 struct bgpevpn *vpn,
3135
                 const struct prefix_evpn *p,
3136
                 struct bgp_path_info *parent_pi)
3137
0
{
3138
0
  int ret;
3139
0
  struct bgp_dest *dest;
3140
3141
  /* Only type-2 routes go into this table */
3142
0
  if (p->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
3143
0
    return 0;
3144
3145
  /* Create (or fetch) route within the VNI MAC table. */
3146
0
  dest = bgp_evpn_vni_mac_node_get(vpn->mac_table, p, parent_pi);
3147
3148
0
  ret = install_evpn_route_entry_in_vni_common(bgp, vpn, p, dest,
3149
0
                 parent_pi);
3150
3151
0
  bgp_dest_unlock_node(dest);
3152
3153
0
  return ret;
3154
0
}
3155
3156
/*
3157
 * Uninstall route entry from VNI IP table and invoke route selection.
3158
 */
3159
static int uninstall_evpn_route_entry_in_vni_ip(struct bgp *bgp,
3160
            struct bgpevpn *vpn,
3161
            const struct prefix_evpn *p,
3162
            struct bgp_path_info *parent_pi)
3163
0
{
3164
0
  int ret;
3165
0
  struct bgp_dest *dest;
3166
3167
  /* Ignore MAC Only Type-2 */
3168
0
  if ((p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) &&
3169
0
      (is_evpn_prefix_ipaddr_none(p) == true))
3170
0
    return 0;
3171
3172
  /* Locate route within the VNI IP table. */
3173
0
  dest = bgp_evpn_vni_ip_node_lookup(vpn->ip_table, p, parent_pi);
3174
0
  if (!dest)
3175
0
    return 0;
3176
3177
0
  ret = uninstall_evpn_route_entry_in_vni_common(bgp, vpn, p, dest,
3178
0
                   parent_pi);
3179
3180
0
  bgp_dest_unlock_node(dest);
3181
3182
0
  return ret;
3183
0
}
3184
3185
/*
3186
 * Uninstall route entry from VNI IP table and invoke route selection.
3187
 */
3188
static int
3189
uninstall_evpn_route_entry_in_vni_mac(struct bgp *bgp, struct bgpevpn *vpn,
3190
              const struct prefix_evpn *p,
3191
              struct bgp_path_info *parent_pi)
3192
0
{
3193
0
  int ret;
3194
0
  struct bgp_dest *dest;
3195
3196
  /* Only type-2 routes go into this table */
3197
0
  if (p->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
3198
0
    return 0;
3199
3200
  /* Locate route within the VNI MAC table. */
3201
0
  dest = bgp_evpn_vni_mac_node_lookup(vpn->mac_table, p, parent_pi);
3202
0
  if (!dest)
3203
0
    return 0;
3204
3205
0
  ret = uninstall_evpn_route_entry_in_vni_common(bgp, vpn, p, dest,
3206
0
                   parent_pi);
3207
3208
0
  bgp_dest_unlock_node(dest);
3209
3210
0
  return ret;
3211
0
}
3212
/*
3213
 * Uninstall route entry from the VRF routing table and send message
3214
 * to zebra, if appropriate.
3215
 */
3216
static int uninstall_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
3217
               const struct prefix_evpn *evp,
3218
               struct bgp_path_info *parent_pi)
3219
0
{
3220
0
  struct bgp_dest *dest;
3221
0
  struct bgp_path_info *pi;
3222
0
  int ret = 0;
3223
0
  struct prefix p;
3224
0
  struct prefix *pp = &p;
3225
0
  afi_t afi = 0;
3226
0
  safi_t safi = 0;
3227
3228
0
  memset(pp, 0, sizeof(struct prefix));
3229
0
  ip_prefix_from_evpn_prefix(evp, pp);
3230
3231
0
  if (bgp_debug_zebra(NULL))
3232
0
    zlog_debug(
3233
0
      "vrf %s: unimport evpn prefix %pFX parent %p flags 0x%x",
3234
0
      vrf_id_to_name(bgp_vrf->vrf_id), evp, parent_pi,
3235
0
      parent_pi->flags);
3236
3237
  /* Locate route within the VRF. */
3238
  /* NOTE: There is no RD here. */
3239
0
  if (is_evpn_prefix_ipaddr_v4(evp)) {
3240
0
    afi = AFI_IP;
3241
0
    safi = SAFI_UNICAST;
3242
0
    dest = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
3243
0
  } else {
3244
0
    afi = AFI_IP6;
3245
0
    safi = SAFI_UNICAST;
3246
0
    dest = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
3247
0
  }
3248
3249
0
  if (!dest)
3250
0
    return 0;
3251
3252
  /* Find matching route entry. */
3253
0
  for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
3254
0
    if (pi->extra
3255
0
        && (struct bgp_path_info *)pi->extra->parent == parent_pi)
3256
0
      break;
3257
3258
0
  if (!pi) {
3259
0
    bgp_dest_unlock_node(dest);
3260
0
    return 0;
3261
0
  }
3262
3263
0
  if (bgp_debug_zebra(NULL))
3264
0
    zlog_debug("... delete dest %p (l %d) pi %p (l %d, f 0x%x)",
3265
0
         dest, bgp_dest_get_lock_count(dest), pi, pi->lock,
3266
0
         pi->flags);
3267
3268
  /* Process for route leaking. */
3269
0
  vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf, pi);
3270
3271
0
  bgp_aggregate_decrement(bgp_vrf, bgp_dest_get_prefix(dest), pi, afi,
3272
0
        safi);
3273
3274
  /* Mark entry for deletion */
3275
0
  bgp_path_info_delete(dest, pi);
3276
3277
  /* Unlink path to evpn nexthop */
3278
0
  bgp_evpn_path_nh_del(bgp_vrf, pi);
3279
3280
  /* Perform route selection and update zebra, if required. */
3281
0
  bgp_process(bgp_vrf, dest, afi, safi);
3282
3283
  /* Unlock route node. */
3284
0
  bgp_dest_unlock_node(dest);
3285
3286
0
  return ret;
3287
0
}
3288
3289
/*
3290
 * Install route entry into the VNI routing tables.
3291
 */
3292
static int install_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
3293
            const struct prefix_evpn *p,
3294
            struct bgp_path_info *parent_pi)
3295
0
{
3296
0
  int ret = 0;
3297
3298
0
  if (bgp_debug_update(parent_pi->peer, NULL, NULL, 1))
3299
0
    zlog_debug(
3300
0
      "%s (%u): Installing EVPN %pFX route in VNI %u IP/MAC table",
3301
0
      vrf_id_to_name(bgp->vrf_id), bgp->vrf_id, p, vpn->vni);
3302
3303
0
  ret = install_evpn_route_entry_in_vni_mac(bgp, vpn, p, parent_pi);
3304
3305
0
  if (ret) {
3306
0
    flog_err(
3307
0
      EC_BGP_EVPN_FAIL,
3308
0
      "%s (%u): Failed to install EVPN %pFX route in VNI %u MAC table",
3309
0
      vrf_id_to_name(bgp->vrf_id), bgp->vrf_id, p, vpn->vni);
3310
3311
0
    return ret;
3312
0
  }
3313
3314
0
  ret = install_evpn_route_entry_in_vni_ip(bgp, vpn, p, parent_pi);
3315
3316
0
  if (ret) {
3317
0
    flog_err(
3318
0
      EC_BGP_EVPN_FAIL,
3319
0
      "%s (%u): Failed to install EVPN %pFX route in VNI %u IP table",
3320
0
      vrf_id_to_name(bgp->vrf_id), bgp->vrf_id, p, vpn->vni);
3321
3322
0
    return ret;
3323
0
  }
3324
3325
0
  return ret;
3326
0
}
3327
3328
/*
3329
 * Uninstall route entry from the VNI routing tables.
3330
 */
3331
static int uninstall_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
3332
              const struct prefix_evpn *p,
3333
              struct bgp_path_info *parent_pi)
3334
0
{
3335
0
  int ret = 0;
3336
3337
0
  if (bgp_debug_update(parent_pi->peer, NULL, NULL, 1))
3338
0
    zlog_debug(
3339
0
      "%s (%u): Uninstalling EVPN %pFX route from VNI %u IP/MAC table",
3340
0
      vrf_id_to_name(bgp->vrf_id), bgp->vrf_id, p, vpn->vni);
3341
3342
0
  ret = uninstall_evpn_route_entry_in_vni_ip(bgp, vpn, p, parent_pi);
3343
3344
0
  if (ret) {
3345
0
    flog_err(
3346
0
      EC_BGP_EVPN_FAIL,
3347
0
      "%s (%u): Failed to uninstall EVPN %pFX route from VNI %u IP table",
3348
0
      vrf_id_to_name(bgp->vrf_id), bgp->vrf_id, p, vpn->vni);
3349
3350
0
    return ret;
3351
0
  }
3352
3353
0
  ret = uninstall_evpn_route_entry_in_vni_mac(bgp, vpn, p, parent_pi);
3354
3355
0
  if (ret) {
3356
0
    flog_err(
3357
0
      EC_BGP_EVPN_FAIL,
3358
0
      "%s (%u): Failed to uninstall EVPN %pFX route from VNI %u MAC table",
3359
0
      vrf_id_to_name(bgp->vrf_id), bgp->vrf_id, p, vpn->vni);
3360
3361
0
    return ret;
3362
0
  }
3363
3364
0
  return ret;
3365
0
}
3366
3367
/*
3368
 * Given a route entry and a VRF, see if this route entry should be
3369
 * imported into the VRF i.e., RTs match.
3370
 */
3371
static int is_route_matching_for_vrf(struct bgp *bgp_vrf,
3372
             struct bgp_path_info *pi)
3373
0
{
3374
0
  struct attr *attr = pi->attr;
3375
0
  struct ecommunity *ecom;
3376
0
  uint32_t i;
3377
3378
0
  assert(attr);
3379
  /* Route should have valid RT to be even considered. */
3380
0
  if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
3381
0
    return 0;
3382
3383
0
  ecom = bgp_attr_get_ecommunity(attr);
3384
0
  if (!ecom || !ecom->size)
3385
0
    return 0;
3386
3387
  /* For each extended community RT, see if it matches this VNI. If any RT
3388
   * matches, we're done.
3389
   */
3390
0
  for (i = 0; i < ecom->size; i++) {
3391
0
    uint8_t *pnt;
3392
0
    uint8_t type, sub_type;
3393
0
    struct ecommunity_val *eval;
3394
0
    struct ecommunity_val eval_tmp;
3395
0
    struct vrf_irt_node *irt;
3396
3397
    /* Only deal with RTs */
3398
0
    pnt = (ecom->val + (i * ecom->unit_size));
3399
0
    eval = (struct ecommunity_val *)(ecom->val
3400
0
             + (i * ecom->unit_size));
3401
0
    type = *pnt++;
3402
0
    sub_type = *pnt++;
3403
0
    if (sub_type != ECOMMUNITY_ROUTE_TARGET)
3404
0
      continue;
3405
3406
    /* See if this RT matches specified VNIs import RTs */
3407
0
    irt = lookup_vrf_import_rt(eval);
3408
0
    if (irt)
3409
0
      if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
3410
0
        return 1;
3411
3412
    /* Also check for non-exact match. In this, we mask out the AS
3413
     * and
3414
     * only check on the local-admin sub-field. This is to
3415
     * facilitate using
3416
     * VNI as the RT for EBGP peering too.
3417
     */
3418
0
    irt = NULL;
3419
0
    if (type == ECOMMUNITY_ENCODE_AS
3420
0
        || type == ECOMMUNITY_ENCODE_AS4
3421
0
        || type == ECOMMUNITY_ENCODE_IP) {
3422
0
      memcpy(&eval_tmp, eval, ecom->unit_size);
3423
0
      mask_ecom_global_admin(&eval_tmp, eval);
3424
0
      irt = lookup_vrf_import_rt(&eval_tmp);
3425
0
    }
3426
0
    if (irt)
3427
0
      if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
3428
0
        return 1;
3429
0
  }
3430
3431
0
  return 0;
3432
0
}
3433
3434
/*
3435
 * Given a route entry and a VNI, see if this route entry should be
3436
 * imported into the VNI i.e., RTs match.
3437
 */
3438
static int is_route_matching_for_vni(struct bgp *bgp, struct bgpevpn *vpn,
3439
             struct bgp_path_info *pi)
3440
0
{
3441
0
  struct attr *attr = pi->attr;
3442
0
  struct ecommunity *ecom;
3443
0
  uint32_t i;
3444
3445
0
  assert(attr);
3446
  /* Route should have valid RT to be even considered. */
3447
0
  if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
3448
0
    return 0;
3449
3450
0
  ecom = bgp_attr_get_ecommunity(attr);
3451
0
  if (!ecom || !ecom->size)
3452
0
    return 0;
3453
3454
  /* For each extended community RT, see if it matches this VNI. If any RT
3455
   * matches, we're done.
3456
   */
3457
0
  for (i = 0; i < ecom->size; i++) {
3458
0
    uint8_t *pnt;
3459
0
    uint8_t type, sub_type;
3460
0
    struct ecommunity_val *eval;
3461
0
    struct ecommunity_val eval_tmp;
3462
0
    struct irt_node *irt;
3463
3464
    /* Only deal with RTs */
3465
0
    pnt = (ecom->val + (i * ecom->unit_size));
3466
0
    eval = (struct ecommunity_val *)(ecom->val
3467
0
             + (i * ecom->unit_size));
3468
0
    type = *pnt++;
3469
0
    sub_type = *pnt++;
3470
0
    if (sub_type != ECOMMUNITY_ROUTE_TARGET)
3471
0
      continue;
3472
3473
    /* See if this RT matches specified VNIs import RTs */
3474
0
    irt = lookup_import_rt(bgp, eval);
3475
0
    if (irt)
3476
0
      if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
3477
0
        return 1;
3478
3479
    /* Also check for non-exact match. In this, we mask out the AS
3480
     * and
3481
     * only check on the local-admin sub-field. This is to
3482
     * facilitate using
3483
     * VNI as the RT for EBGP peering too.
3484
     */
3485
0
    irt = NULL;
3486
0
    if (type == ECOMMUNITY_ENCODE_AS
3487
0
        || type == ECOMMUNITY_ENCODE_AS4
3488
0
        || type == ECOMMUNITY_ENCODE_IP) {
3489
0
      memcpy(&eval_tmp, eval, ecom->unit_size);
3490
0
      mask_ecom_global_admin(&eval_tmp, eval);
3491
0
      irt = lookup_import_rt(bgp, &eval_tmp);
3492
0
    }
3493
0
    if (irt)
3494
0
      if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
3495
0
        return 1;
3496
0
  }
3497
3498
0
  return 0;
3499
0
}
3500
3501
/* This API will scan evpn routes for checking attribute's rmac
3502
 * macthes with bgp instance router mac. It avoid installing
3503
 * route into bgp vrf table and remote rmac in bridge table.
3504
 */
3505
static int bgp_evpn_route_rmac_self_check(struct bgp *bgp_vrf,
3506
            const struct prefix_evpn *evp,
3507
            struct bgp_path_info *pi)
3508
0
{
3509
  /* evpn route could have learnt prior to L3vni has come up,
3510
   * perform rmac check before installing route and
3511
   * remote router mac.
3512
   * The route will be removed from global bgp table once
3513
   * SVI comes up with MAC and stored in hash, triggers
3514
   * bgp_mac_rescan_all_evpn_tables.
3515
   */
3516
0
  if (memcmp(&bgp_vrf->rmac, &pi->attr->rmac, ETH_ALEN) == 0) {
3517
0
    if (bgp_debug_update(pi->peer, NULL, NULL, 1)) {
3518
0
      char attr_str[BUFSIZ] = {0};
3519
3520
0
      bgp_dump_attr(pi->attr, attr_str, sizeof(attr_str));
3521
3522
0
      zlog_debug(
3523
0
        "%s: bgp %u prefix %pFX with attr %s - DENIED due to self mac",
3524
0
        __func__, bgp_vrf->vrf_id, evp, attr_str);
3525
0
    }
3526
3527
0
    return 1;
3528
0
  }
3529
3530
0
  return 0;
3531
0
}
3532
3533
/* don't import hosts that are locally attached */
3534
static inline bool
3535
bgp_evpn_skip_vrf_import_of_local_es(struct bgp *bgp_vrf,
3536
             const struct prefix_evpn *evp,
3537
             struct bgp_path_info *pi, int install)
3538
0
{
3539
0
  esi_t *esi;
3540
3541
0
  if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
3542
0
    esi = bgp_evpn_attr_get_esi(pi->attr);
3543
3544
    /* Don't import routes that point to a local destination */
3545
0
    if (bgp_evpn_attr_is_local_es(pi->attr)) {
3546
0
      if (BGP_DEBUG(evpn_mh, EVPN_MH_RT)) {
3547
0
        char esi_buf[ESI_STR_LEN];
3548
3549
0
        zlog_debug(
3550
0
          "vrf %s of evpn prefix %pFX skipped, local es %s",
3551
0
          install ? "import" : "unimport", evp,
3552
0
          esi_to_str(esi, esi_buf,
3553
0
               sizeof(esi_buf)));
3554
0
      }
3555
0
      return true;
3556
0
    }
3557
0
  }
3558
0
  return false;
3559
0
}
3560
3561
/*
3562
 * Install or uninstall a mac-ip route in the provided vrf if
3563
 * there is a rt match
3564
 */
3565
int bgp_evpn_route_entry_install_if_vrf_match(struct bgp *bgp_vrf,
3566
                struct bgp_path_info *pi,
3567
                int install)
3568
0
{
3569
0
  int ret = 0;
3570
0
  const struct prefix_evpn *evp =
3571
0
    (const struct prefix_evpn *)bgp_dest_get_prefix(pi->net);
3572
3573
  /* Consider "valid" remote routes applicable for
3574
   * this VRF.
3575
   */
3576
0
  if (!(CHECK_FLAG(pi->flags, BGP_PATH_VALID)
3577
0
        && pi->type == ZEBRA_ROUTE_BGP
3578
0
        && pi->sub_type == BGP_ROUTE_NORMAL))
3579
0
    return 0;
3580
3581
0
  if (is_route_matching_for_vrf(bgp_vrf, pi)) {
3582
0
    if (bgp_evpn_route_rmac_self_check(bgp_vrf, evp, pi))
3583
0
      return 0;
3584
3585
    /* don't import hosts that are locally attached */
3586
0
    if (install && bgp_evpn_skip_vrf_import_of_local_es(
3587
0
               bgp_vrf, evp, pi, install))
3588
0
      return 0;
3589
3590
0
    if (install)
3591
0
      ret = install_evpn_route_entry_in_vrf(bgp_vrf, evp, pi);
3592
0
    else
3593
0
      ret = uninstall_evpn_route_entry_in_vrf(bgp_vrf, evp,
3594
0
                pi);
3595
3596
0
    if (ret)
3597
0
      flog_err(EC_BGP_EVPN_FAIL,
3598
0
         "Failed to %s EVPN %pFX route in VRF %s",
3599
0
         install ? "install" : "uninstall", evp,
3600
0
         vrf_id_to_name(bgp_vrf->vrf_id));
3601
0
  }
3602
3603
0
  return ret;
3604
0
}
3605
3606
/*
3607
 * Install or uninstall mac-ip routes are appropriate for this
3608
 * particular VRF.
3609
 */
3610
static int install_uninstall_routes_for_vrf(struct bgp *bgp_vrf, int install)
3611
0
{
3612
0
  afi_t afi;
3613
0
  safi_t safi;
3614
0
  struct bgp_dest *rd_dest, *dest;
3615
0
  struct bgp_table *table;
3616
0
  struct bgp_path_info *pi;
3617
0
  int ret;
3618
0
  struct bgp *bgp_evpn = NULL;
3619
3620
0
  afi = AFI_L2VPN;
3621
0
  safi = SAFI_EVPN;
3622
0
  bgp_evpn = bgp_get_evpn();
3623
0
  if (!bgp_evpn)
3624
0
    return -1;
3625
3626
  /* Walk entire global routing table and evaluate routes which could be
3627
   * imported into this VRF. Note that we need to loop through all global
3628
   * routes to determine which route matches the import rt on vrf
3629
   */
3630
0
  for (rd_dest = bgp_table_top(bgp_evpn->rib[afi][safi]); rd_dest;
3631
0
       rd_dest = bgp_route_next(rd_dest)) {
3632
0
    table = bgp_dest_get_bgp_table_info(rd_dest);
3633
0
    if (!table)
3634
0
      continue;
3635
3636
0
    for (dest = bgp_table_top(table); dest;
3637
0
         dest = bgp_route_next(dest)) {
3638
0
      const struct prefix_evpn *evp =
3639
0
        (const struct prefix_evpn *)bgp_dest_get_prefix(
3640
0
          dest);
3641
3642
      /* if not mac-ip route skip this route */
3643
0
      if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3644
0
            || evp->prefix.route_type
3645
0
           == BGP_EVPN_IP_PREFIX_ROUTE))
3646
0
        continue;
3647
3648
      /* if not a mac+ip route skip this route */
3649
0
      if (!(is_evpn_prefix_ipaddr_v4(evp)
3650
0
            || is_evpn_prefix_ipaddr_v6(evp)))
3651
0
        continue;
3652
3653
0
      for (pi = bgp_dest_get_bgp_path_info(dest); pi;
3654
0
           pi = pi->next) {
3655
0
        ret = bgp_evpn_route_entry_install_if_vrf_match(
3656
0
          bgp_vrf, pi, install);
3657
0
        if (ret) {
3658
0
          bgp_dest_unlock_node(rd_dest);
3659
0
          bgp_dest_unlock_node(dest);
3660
0
          return ret;
3661
0
        }
3662
0
      }
3663
0
    }
3664
0
  }
3665
3666
0
  return 0;
3667
0
}
3668
3669
/*
3670
 * Install or uninstall routes of specified type that are appropriate for this
3671
 * particular VNI.
3672
 */
3673
static int install_uninstall_routes_for_vni(struct bgp *bgp,
3674
              struct bgpevpn *vpn,
3675
              bgp_evpn_route_type rtype,
3676
              int install)
3677
0
{
3678
0
  afi_t afi;
3679
0
  safi_t safi;
3680
0
  struct bgp_dest *rd_dest, *dest;
3681
0
  struct bgp_table *table;
3682
0
  struct bgp_path_info *pi;
3683
0
  int ret;
3684
3685
0
  afi = AFI_L2VPN;
3686
0
  safi = SAFI_EVPN;
3687
3688
  /* Walk entire global routing table and evaluate routes which could be
3689
   * imported into this VPN. Note that we cannot just look at the routes
3690
   * for
3691
   * the VNI's RD - remote routes applicable for this VNI could have any
3692
   * RD.
3693
   */
3694
  /* EVPN routes are a 2-level table. */
3695
0
  for (rd_dest = bgp_table_top(bgp->rib[afi][safi]); rd_dest;
3696
0
       rd_dest = bgp_route_next(rd_dest)) {
3697
0
    table = bgp_dest_get_bgp_table_info(rd_dest);
3698
0
    if (!table)
3699
0
      continue;
3700
3701
0
    for (dest = bgp_table_top(table); dest;
3702
0
         dest = bgp_route_next(dest)) {
3703
0
      const struct prefix_evpn *evp =
3704
0
        (const struct prefix_evpn *)bgp_dest_get_prefix(
3705
0
          dest);
3706
3707
0
      if (evp->prefix.route_type != rtype)
3708
0
        continue;
3709
3710
0
      for (pi = bgp_dest_get_bgp_path_info(dest); pi;
3711
0
           pi = pi->next) {
3712
        /* Consider "valid" remote routes applicable for
3713
         * this VNI. */
3714
0
        if (!(CHECK_FLAG(pi->flags, BGP_PATH_VALID)
3715
0
              && pi->type == ZEBRA_ROUTE_BGP
3716
0
              && pi->sub_type == BGP_ROUTE_NORMAL))
3717
0
          continue;
3718
3719
0
        if (is_route_matching_for_vni(bgp, vpn, pi)) {
3720
0
          if (install)
3721
0
            ret = install_evpn_route_entry(
3722
0
              bgp, vpn, evp, pi);
3723
0
          else
3724
0
            ret = uninstall_evpn_route_entry(
3725
0
              bgp, vpn, evp, pi);
3726
3727
0
          if (ret) {
3728
0
            flog_err(
3729
0
              EC_BGP_EVPN_FAIL,
3730
0
              "%u: Failed to %s EVPN %s route in VNI %u",
3731
0
              bgp->vrf_id,
3732
0
              install ? "install"
3733
0
                : "uninstall",
3734
0
              rtype == BGP_EVPN_MAC_IP_ROUTE
3735
0
                ? "MACIP"
3736
0
                : "IMET",
3737
0
              vpn->vni);
3738
3739
0
            bgp_dest_unlock_node(rd_dest);
3740
0
            bgp_dest_unlock_node(dest);
3741
0
            return ret;
3742
0
          }
3743
0
        }
3744
0
      }
3745
0
    }
3746
0
  }
3747
3748
0
  return 0;
3749
0
}
3750
3751
/* Install any existing remote routes applicable for this VRF into VRF RIB. This
3752
 * is invoked upon l3vni-add or l3vni import rt change
3753
 */
3754
static int install_routes_for_vrf(struct bgp *bgp_vrf)
3755
0
{
3756
0
  install_uninstall_routes_for_vrf(bgp_vrf, 1);
3757
0
  return 0;
3758
0
}
3759
3760
/*
3761
 * Install any existing remote routes applicable for this VNI into its
3762
 * routing table. This is invoked when a VNI becomes "live" or its Import
3763
 * RT is changed.
3764
 */
3765
static int install_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
3766
0
{
3767
0
  int ret;
3768
3769
  /* Install type-3 routes followed by type-2 routes - the ones applicable
3770
   * for this VNI.
3771
   */
3772
0
  ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
3773
0
                 1);
3774
0
  if (ret)
3775
0
    return ret;
3776
3777
0
  ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_AD_ROUTE,
3778
0
                 1);
3779
0
  if (ret)
3780
0
    return ret;
3781
3782
0
  return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
3783
0
            1);
3784
0
}
3785
3786
/* uninstall routes from l3vni vrf. */
3787
static int uninstall_routes_for_vrf(struct bgp *bgp_vrf)
3788
0
{
3789
0
  install_uninstall_routes_for_vrf(bgp_vrf, 0);
3790
0
  return 0;
3791
0
}
3792
3793
/*
3794
 * Uninstall any existing remote routes for this VNI. One scenario in which
3795
 * this is invoked is upon an import RT change.
3796
 */
3797
static int uninstall_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
3798
0
{
3799
0
  int ret;
3800
3801
  /* Uninstall type-2 routes followed by type-3 routes - the ones
3802
   * applicable
3803
   * for this VNI.
3804
   */
3805
0
  ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
3806
0
                 0);
3807
0
  if (ret)
3808
0
    return ret;
3809
3810
0
  ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_AD_ROUTE,
3811
0
                 0);
3812
0
  if (ret)
3813
0
    return ret;
3814
3815
3816
0
  return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
3817
0
            0);
3818
0
}
3819
3820
/*
3821
 * Install or uninstall route in matching VRFs (list).
3822
 */
3823
static int install_uninstall_route_in_vrfs(struct bgp *bgp_def, afi_t afi,
3824
             safi_t safi, struct prefix_evpn *evp,
3825
             struct bgp_path_info *pi,
3826
             struct list *vrfs, int install)
3827
0
{
3828
0
  struct bgp *bgp_vrf;
3829
0
  struct listnode *node, *nnode;
3830
3831
  /* Only type-2/type-5 routes go into a VRF */
3832
0
  if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3833
0
        || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
3834
0
    return 0;
3835
3836
  /* if it is type-2 route and not a mac+ip route skip this route */
3837
0
  if ((evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
3838
0
      && !(is_evpn_prefix_ipaddr_v4(evp)
3839
0
     || is_evpn_prefix_ipaddr_v6(evp)))
3840
0
    return 0;
3841
3842
0
  for (ALL_LIST_ELEMENTS(vrfs, node, nnode, bgp_vrf)) {
3843
0
    int ret;
3844
3845
    /* don't import hosts that are locally attached */
3846
0
    if (install && bgp_evpn_skip_vrf_import_of_local_es(
3847
0
               bgp_vrf, evp, pi, install))
3848
0
      return 0;
3849
3850
0
    if (install)
3851
0
      ret = install_evpn_route_entry_in_vrf(bgp_vrf, evp, pi);
3852
0
    else
3853
0
      ret = uninstall_evpn_route_entry_in_vrf(bgp_vrf, evp,
3854
0
                pi);
3855
3856
0
    if (ret) {
3857
0
      flog_err(EC_BGP_EVPN_FAIL,
3858
0
         "%u: Failed to %s prefix %pFX in VRF %s",
3859
0
         bgp_def->vrf_id,
3860
0
         install ? "install" : "uninstall", evp,
3861
0
         vrf_id_to_name(bgp_vrf->vrf_id));
3862
0
      return ret;
3863
0
    }
3864
0
  }
3865
3866
0
  return 0;
3867
0
}
3868
3869
/*
3870
 * Install or uninstall route in matching VNIs (list).
3871
 */
3872
static int install_uninstall_route_in_vnis(struct bgp *bgp, afi_t afi,
3873
             safi_t safi, struct prefix_evpn *evp,
3874
             struct bgp_path_info *pi,
3875
             struct list *vnis, int install)
3876
0
{
3877
0
  struct bgpevpn *vpn;
3878
0
  struct listnode *node, *nnode;
3879
3880
0
  for (ALL_LIST_ELEMENTS(vnis, node, nnode, vpn)) {
3881
0
    int ret;
3882
3883
0
    if (!is_vni_live(vpn))
3884
0
      continue;
3885
3886
0
    if (install)
3887
0
      ret = install_evpn_route_entry(bgp, vpn, evp, pi);
3888
0
    else
3889
0
      ret = uninstall_evpn_route_entry(bgp, vpn, evp, pi);
3890
3891
0
    if (ret) {
3892
0
      flog_err(EC_BGP_EVPN_FAIL,
3893
0
         "%u: Failed to %s EVPN %s route in VNI %u",
3894
0
         bgp->vrf_id, install ? "install" : "uninstall",
3895
0
         evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3896
0
           ? "MACIP"
3897
0
           : "IMET",
3898
0
         vpn->vni);
3899
0
      return ret;
3900
0
    }
3901
0
  }
3902
3903
0
  return 0;
3904
0
}
3905
3906
/*
3907
 * Install or uninstall route for appropriate VNIs/ESIs.
3908
 */
3909
static int bgp_evpn_install_uninstall_table(struct bgp *bgp, afi_t afi,
3910
              safi_t safi, const struct prefix *p,
3911
              struct bgp_path_info *pi,
3912
              int import, bool in_vni_rt,
3913
              bool in_vrf_rt)
3914
0
{
3915
0
  struct prefix_evpn *evp = (struct prefix_evpn *)p;
3916
0
  struct attr *attr = pi->attr;
3917
0
  struct ecommunity *ecom;
3918
0
  uint32_t i;
3919
0
  struct prefix_evpn ad_evp;
3920
3921
0
  assert(attr);
3922
3923
  /* Only type-1, type-2, type-3, type-4 and type-5
3924
   * are supported currently
3925
   */
3926
0
  if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3927
0
        || evp->prefix.route_type == BGP_EVPN_IMET_ROUTE
3928
0
        || evp->prefix.route_type == BGP_EVPN_ES_ROUTE
3929
0
        || evp->prefix.route_type == BGP_EVPN_AD_ROUTE
3930
0
        || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
3931
0
    return 0;
3932
3933
  /* If we don't have Route Target, nothing much to do. */
3934
0
  if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
3935
0
    return 0;
3936
3937
  /* EAD prefix in the global table doesn't include the VTEP-IP so
3938
   * we need to create a different copy for the VNI
3939
   */
3940
0
  if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE)
3941
0
    evp = evpn_type1_prefix_vni_ip_copy(&ad_evp, evp,
3942
0
                attr->nexthop);
3943
3944
0
  ecom = bgp_attr_get_ecommunity(attr);
3945
0
  if (!ecom || !ecom->size)
3946
0
    return -1;
3947
3948
  /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3949
   * attached to the route */
3950
0
  for (i = 0; i < ecom->size; i++) {
3951
0
    uint8_t *pnt;
3952
0
    uint8_t type, sub_type;
3953
0
    struct ecommunity_val *eval;
3954
0
    struct ecommunity_val eval_tmp;
3955
0
    struct irt_node *irt;  /* import rt for l2vni */
3956
0
    struct vrf_irt_node *vrf_irt; /* import rt for l3vni */
3957
0
    struct bgp_evpn_es *es;
3958
3959
    /* Only deal with RTs */
3960
0
    pnt = (ecom->val + (i * ecom->unit_size));
3961
0
    eval = (struct ecommunity_val *)(ecom->val
3962
0
             + (i * ecom->unit_size));
3963
0
    type = *pnt++;
3964
0
    sub_type = *pnt++;
3965
0
    if (sub_type != ECOMMUNITY_ROUTE_TARGET)
3966
0
      continue;
3967
3968
    /* non-local MAC-IP routes in the global route table are linked
3969
     * to the destination ES
3970
     */
3971
0
    if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
3972
0
      bgp_evpn_path_es_link(pi, 0,
3973
0
                bgp_evpn_attr_get_esi(pi->attr));
3974
3975
    /*
3976
     * macip routes (type-2) are imported into VNI and VRF tables.
3977
     * IMET route is imported into VNI table.
3978
     * prefix routes are imported into VRF table.
3979
     */
3980
0
    if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE ||
3981
0
        evp->prefix.route_type == BGP_EVPN_IMET_ROUTE ||
3982
0
        evp->prefix.route_type == BGP_EVPN_AD_ROUTE ||
3983
0
        evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE) {
3984
3985
0
      irt = in_vni_rt ? lookup_import_rt(bgp, eval) : NULL;
3986
0
      if (irt)
3987
0
        install_uninstall_route_in_vnis(
3988
0
          bgp, afi, safi, evp, pi, irt->vnis,
3989
0
          import);
3990
3991
0
      vrf_irt = in_vrf_rt ? lookup_vrf_import_rt(eval) : NULL;
3992
0
      if (vrf_irt)
3993
0
        install_uninstall_route_in_vrfs(
3994
0
          bgp, afi, safi, evp, pi, vrf_irt->vrfs,
3995
0
          import);
3996
3997
      /* Also check for non-exact match.
3998
       * In this, we mask out the AS and
3999
       * only check on the local-admin sub-field.
4000
       * This is to facilitate using
4001
       * VNI as the RT for EBGP peering too.
4002
       */
4003
0
      irt = NULL;
4004
0
      vrf_irt = NULL;
4005
0
      if (type == ECOMMUNITY_ENCODE_AS
4006
0
          || type == ECOMMUNITY_ENCODE_AS4
4007
0
          || type == ECOMMUNITY_ENCODE_IP) {
4008
0
        memcpy(&eval_tmp, eval, ecom->unit_size);
4009
0
        mask_ecom_global_admin(&eval_tmp, eval);
4010
0
        if (in_vni_rt)
4011
0
          irt = lookup_import_rt(bgp, &eval_tmp);
4012
0
        if (in_vrf_rt)
4013
0
          vrf_irt =
4014
0
            lookup_vrf_import_rt(&eval_tmp);
4015
0
      }
4016
4017
0
      if (irt)
4018
0
        install_uninstall_route_in_vnis(
4019
0
          bgp, afi, safi, evp, pi, irt->vnis,
4020
0
          import);
4021
0
      if (vrf_irt)
4022
0
        install_uninstall_route_in_vrfs(
4023
0
          bgp, afi, safi, evp, pi, vrf_irt->vrfs,
4024
0
          import);
4025
0
    }
4026
4027
    /* es route is imported into the es table */
4028
0
    if (evp->prefix.route_type == BGP_EVPN_ES_ROUTE) {
4029
4030
      /* we will match based on the entire esi to avoid
4031
       * import of an es route for esi2 into esi1
4032
       */
4033
0
      es = bgp_evpn_es_find(&evp->prefix.es_addr.esi);
4034
0
      if (es && bgp_evpn_is_es_local(es))
4035
0
        bgp_evpn_es_route_install_uninstall(
4036
0
          bgp, es, afi, safi, evp, pi, import);
4037
0
    }
4038
0
  }
4039
4040
0
  return 0;
4041
0
}
4042
4043
/*
4044
 * Install or uninstall route for appropriate VNIs/ESIs.
4045
 */
4046
static int install_uninstall_evpn_route(struct bgp *bgp, afi_t afi, safi_t safi,
4047
          const struct prefix *p,
4048
          struct bgp_path_info *pi, int import)
4049
0
{
4050
0
  return bgp_evpn_install_uninstall_table(bgp, afi, safi, p, pi, import,
4051
0
            true, true);
4052
0
}
4053
4054
void bgp_evpn_import_type2_route(struct bgp_path_info *pi, int import)
4055
0
{
4056
0
  struct bgp *bgp_evpn;
4057
4058
0
  bgp_evpn = bgp_get_evpn();
4059
0
  if (!bgp_evpn)
4060
0
    return;
4061
4062
0
  install_uninstall_evpn_route(bgp_evpn, AFI_L2VPN, SAFI_EVPN,
4063
0
             &pi->net->p, pi, import);
4064
0
}
4065
4066
/*
4067
 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
4068
 * routes
4069
 */
4070
static void delete_withdraw_vrf_routes(struct bgp *bgp_vrf)
4071
0
{
4072
  /* Delete ipv4 default route and withdraw from peers */
4073
0
  if (evpn_default_originate_set(bgp_vrf, AFI_IP, SAFI_UNICAST))
4074
0
    bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP,
4075
0
               SAFI_UNICAST, false);
4076
4077
  /* delete all ipv4 routes and withdraw from peers */
4078
0
  if (advertise_type5_routes(bgp_vrf, AFI_IP))
4079
0
    bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);
4080
4081
  /* Delete ipv6 default route and withdraw from peers */
4082
0
  if (evpn_default_originate_set(bgp_vrf, AFI_IP6, SAFI_UNICAST))
4083
0
    bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP6,
4084
0
               SAFI_UNICAST, false);
4085
4086
  /* delete all ipv6 routes and withdraw from peers */
4087
0
  if (advertise_type5_routes(bgp_vrf, AFI_IP6))
4088
0
    bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
4089
0
}
4090
4091
/*
4092
 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
4093
 * routes
4094
 */
4095
void update_advertise_vrf_routes(struct bgp *bgp_vrf)
4096
0
{
4097
0
  struct bgp *bgp_evpn = NULL; /* EVPN bgp instance */
4098
4099
0
  bgp_evpn = bgp_get_evpn();
4100
0
  if (!bgp_evpn)
4101
0
    return;
4102
4103
  /* update all ipv4 routes */
4104
0
  if (advertise_type5_routes(bgp_vrf, AFI_IP))
4105
0
    bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);
4106
4107
  /* update ipv4 default route and withdraw from peers */
4108
0
  if (evpn_default_originate_set(bgp_vrf, AFI_IP, SAFI_UNICAST))
4109
0
    bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP,
4110
0
               SAFI_UNICAST, true);
4111
4112
  /* update all ipv6 routes */
4113
0
  if (advertise_type5_routes(bgp_vrf, AFI_IP6))
4114
0
    bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
4115
4116
  /* update ipv6 default route and withdraw from peers */
4117
0
  if (evpn_default_originate_set(bgp_vrf, AFI_IP6, SAFI_UNICAST))
4118
0
    bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP6,
4119
0
               SAFI_UNICAST, true);
4120
4121
0
}
4122
4123
/*
4124
 * update and advertise local routes for a VRF as type-5 routes.
4125
 * This is invoked upon RD change for a VRF. Note taht the processing is only
4126
 * done in the global route table using the routes which already exist in the
4127
 * VRF routing table
4128
 */
4129
static void update_router_id_vrf(struct bgp *bgp_vrf)
4130
0
{
4131
  /* skip if the RD is configured */
4132
0
  if (is_vrf_rd_configured(bgp_vrf))
4133
0
    return;
4134
4135
  /* derive the RD for the VRF based on new router-id */
4136
0
  bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
4137
4138
  /* update advertise ipv4|ipv6 routes as type-5 routes */
4139
0
  update_advertise_vrf_routes(bgp_vrf);
4140
0
}
4141
4142
/*
4143
 * Delete and withdraw all type-5 routes  for the RD corresponding to VRF.
4144
 * This is invoked upon VRF RD change. The processing is done only from global
4145
 * table.
4146
 */
4147
static void withdraw_router_id_vrf(struct bgp *bgp_vrf)
4148
0
{
4149
  /* skip if the RD is configured */
4150
0
  if (is_vrf_rd_configured(bgp_vrf))
4151
0
    return;
4152
4153
  /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
4154
0
  delete_withdraw_vrf_routes(bgp_vrf);
4155
0
}
4156
4157
static void update_advertise_vni_route(struct bgp *bgp, struct bgpevpn *vpn,
4158
               struct bgp_dest *dest)
4159
0
{
4160
0
  struct bgp_dest *global_dest;
4161
0
  struct bgp_path_info *pi, *global_pi;
4162
0
  struct attr *attr;
4163
0
  afi_t afi = AFI_L2VPN;
4164
0
  safi_t safi = SAFI_EVPN;
4165
4166
0
  struct prefix_evpn tmp_evp;
4167
0
  const struct prefix_evpn *evp =
4168
0
    (const struct prefix_evpn *)bgp_dest_get_prefix(dest);
4169
4170
  /*
4171
   * We have already processed type-3 routes.
4172
   * Process only type-1 and type-2 routes here.
4173
   */
4174
0
  if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE &&
4175
0
      evp->prefix.route_type != BGP_EVPN_AD_ROUTE)
4176
0
    return;
4177
4178
0
  for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
4179
0
    if (pi->peer == bgp->peer_self && pi->type == ZEBRA_ROUTE_BGP &&
4180
0
        pi->sub_type == BGP_ROUTE_STATIC)
4181
0
      break;
4182
0
  if (!pi)
4183
0
    return;
4184
4185
  /*
4186
   * VNI table MAC-IP prefixes don't have MAC so make sure it's
4187
   * set from path info here.
4188
   */
4189
0
  if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
4190
0
    if (is_evpn_prefix_ipaddr_none(evp)) {
4191
      /* VNI MAC -> Global */
4192
0
      evpn_type2_prefix_global_copy(
4193
0
        &tmp_evp, evp, NULL /* mac */,
4194
0
        evpn_type2_path_info_get_ip(pi));
4195
0
    } else {
4196
      /* VNI IP -> Global */
4197
0
      evpn_type2_prefix_global_copy(
4198
0
        &tmp_evp, evp, evpn_type2_path_info_get_mac(pi),
4199
0
        NULL /* ip */);
4200
0
    }
4201
0
  } else {
4202
0
    memcpy(&tmp_evp, evp, sizeof(tmp_evp));
4203
0
  }
4204
4205
  /* Create route in global routing table using this route entry's
4206
   * attribute.
4207
   */
4208
0
  attr = pi->attr;
4209
0
  global_dest = bgp_evpn_global_node_get(bgp->rib[afi][safi], afi, safi,
4210
0
                 &tmp_evp, &vpn->prd, NULL);
4211
0
  assert(global_dest);
4212
4213
0
  if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
4214
    /* Type-2 route */
4215
0
    update_evpn_route_entry(
4216
0
      bgp, vpn, afi, safi, global_dest, attr, NULL /* mac */,
4217
0
      NULL /* ip */, 1, &global_pi, 0,
4218
0
      mac_mobility_seqnum(attr), false /* setup_sync */,
4219
0
      NULL /* old_is_sync */);
4220
0
  } else {
4221
    /* Type-1 route */
4222
0
    struct bgp_evpn_es *es;
4223
0
    int route_changed = 0;
4224
4225
0
    es = bgp_evpn_es_find(&evp->prefix.ead_addr.esi);
4226
0
    bgp_evpn_mh_route_update(bgp, es, vpn, afi, safi, global_dest,
4227
0
           attr, &global_pi, &route_changed);
4228
0
  }
4229
4230
  /* Schedule for processing and unlock node. */
4231
0
  bgp_process(bgp, global_dest, afi, safi);
4232
0
  bgp_dest_unlock_node(global_dest);
4233
0
}
4234
4235
/*
4236
 * Update and advertise local routes for a VNI. Invoked upon router-id
4237
 * change. Note that the processing is done only on the global route table
4238
 * using routes that already exist in the per-VNI table.
4239
 */
4240
static void update_advertise_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
4241
0
{
4242
0
  struct prefix_evpn p;
4243
0
  struct bgp_dest *dest, *global_dest;
4244
0
  struct bgp_path_info *pi;
4245
0
  struct attr *attr;
4246
0
  afi_t afi = AFI_L2VPN;
4247
0
  safi_t safi = SAFI_EVPN;
4248
4249
  /* Locate type-3 route for VNI in the per-VNI table and use its
4250
   * attributes to create and advertise the type-3 route for this VNI
4251
   * in the global table.
4252
   *
4253
   * RT-3 only if doing head-end replication
4254
   */
4255
0
  if (bgp_evpn_vni_flood_mode_get(bgp, vpn)
4256
0
        == VXLAN_FLOOD_HEAD_END_REPL) {
4257
0
    build_evpn_type3_prefix(&p, vpn->originator_ip);
4258
0
    dest = bgp_evpn_vni_node_lookup(vpn, &p, NULL);
4259
0
    if (!dest) /* unexpected */
4260
0
      return;
4261
0
    for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
4262
0
      if (pi->peer == bgp->peer_self &&
4263
0
          pi->type == ZEBRA_ROUTE_BGP
4264
0
          && pi->sub_type == BGP_ROUTE_STATIC)
4265
0
        break;
4266
0
    if (!pi) {
4267
0
      bgp_dest_unlock_node(dest);
4268
0
      return;
4269
0
    }
4270
4271
0
    attr = pi->attr;
4272
4273
0
    global_dest = bgp_evpn_global_node_get(
4274
0
      bgp->rib[afi][safi], afi, safi, &p, &vpn->prd, NULL);
4275
0
    update_evpn_route_entry(
4276
0
      bgp, vpn, afi, safi, global_dest, attr, NULL /* mac */,
4277
0
      NULL /* ip */, 1, &pi, 0, mac_mobility_seqnum(attr),
4278
0
      false /* setup_sync */, NULL /* old_is_sync */);
4279
4280
    /* Schedule for processing and unlock node. */
4281
0
    bgp_process(bgp, global_dest, afi, safi);
4282
0
    bgp_dest_unlock_node(global_dest);
4283
0
  }
4284
4285
  /* Now, walk this VNI's MAC & IP route table and use the route and its
4286
   * attribute to create and schedule route in global table.
4287
   */
4288
0
  for (dest = bgp_table_top(vpn->mac_table); dest;
4289
0
       dest = bgp_route_next(dest))
4290
0
    update_advertise_vni_route(bgp, vpn, dest);
4291
4292
0
  for (dest = bgp_table_top(vpn->ip_table); dest;
4293
0
       dest = bgp_route_next(dest))
4294
0
    update_advertise_vni_route(bgp, vpn, dest);
4295
0
}
4296
4297
/*
4298
 * Delete (and withdraw) local routes for a VNI - only from the global
4299
 * table. Invoked upon router-id change.
4300
 */
4301
static int delete_withdraw_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
4302
0
{
4303
0
  struct prefix_evpn p;
4304
0
  struct bgp_dest *global_dest;
4305
0
  struct bgp_path_info *pi;
4306
0
  afi_t afi = AFI_L2VPN;
4307
0
  safi_t safi = SAFI_EVPN;
4308
4309
  /* Delete and withdraw locally learnt type-2 routes (MACIP)
4310
   * for this VNI - from the global table.
4311
   */
4312
0
  delete_global_type2_routes(bgp, vpn);
4313
4314
  /* Remove type-3 route for this VNI from global table. */
4315
0
  build_evpn_type3_prefix(&p, vpn->originator_ip);
4316
0
  global_dest = bgp_evpn_global_node_lookup(bgp->rib[afi][safi], safi, &p,
4317
0
              &vpn->prd, NULL);
4318
0
  if (global_dest) {
4319
    /* Delete route entry in the global EVPN table. */
4320
0
    delete_evpn_route_entry(bgp, afi, safi, global_dest, &pi);
4321
4322
    /* Schedule for processing - withdraws to peers happen from
4323
     * this table.
4324
     */
4325
0
    if (pi)
4326
0
      bgp_process(bgp, global_dest, afi, safi);
4327
0
    bgp_dest_unlock_node(global_dest);
4328
0
  }
4329
4330
4331
0
  delete_global_ead_evi_routes(bgp, vpn);
4332
0
  return 0;
4333
0
}
4334
4335
/*
4336
 * Handle router-id change. Update and advertise local routes corresponding
4337
 * to this VNI from peers. Note that this is invoked after updating the
4338
 * router-id. The routes in the per-VNI table are used to create routes in
4339
 * the global table and schedule them.
4340
 */
4341
static void update_router_id_vni(struct hash_bucket *bucket, struct bgp *bgp)
4342
0
{
4343
0
  struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
4344
4345
  /* Skip VNIs with configured RD. */
4346
0
  if (is_rd_configured(vpn))
4347
0
    return;
4348
4349
0
  bgp_evpn_derive_auto_rd(bgp, vpn);
4350
0
  update_advertise_vni_routes(bgp, vpn);
4351
0
}
4352
4353
/*
4354
 * Handle router-id change. Delete and withdraw local routes corresponding
4355
 * to this VNI from peers. Note that this is invoked prior to updating
4356
 * the router-id and is done only on the global route table, the routes
4357
 * are needed in the per-VNI table to re-advertise with new router id.
4358
 */
4359
static void withdraw_router_id_vni(struct hash_bucket *bucket, struct bgp *bgp)
4360
0
{
4361
0
  struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
4362
4363
  /* Skip VNIs with configured RD. */
4364
0
  if (is_rd_configured(vpn))
4365
0
    return;
4366
4367
0
  delete_withdraw_vni_routes(bgp, vpn);
4368
0
}
4369
4370
/*
4371
 * Create RT-3 for a VNI and schedule for processing and advertisement.
4372
 * This is invoked upon flooding mode changing to head-end replication.
4373
 */
4374
static void create_advertise_type3(struct hash_bucket *bucket, void *data)
4375
0
{
4376
0
  struct bgpevpn *vpn = bucket->data;
4377
0
  struct bgp *bgp = data;
4378
0
  struct prefix_evpn p;
4379
4380
0
  if (!vpn || !is_vni_live(vpn) ||
4381
0
    bgp_evpn_vni_flood_mode_get(bgp, vpn)
4382
0
          != VXLAN_FLOOD_HEAD_END_REPL)
4383
0
    return;
4384
4385
0
  build_evpn_type3_prefix(&p, vpn->originator_ip);
4386
0
  if (update_evpn_route(bgp, vpn, &p, 0, 0, NULL))
4387
0
    flog_err(EC_BGP_EVPN_ROUTE_CREATE,
4388
0
       "Type3 route creation failure for VNI %u", vpn->vni);
4389
0
}
4390
4391
/*
4392
 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
4393
 * This is invoked upon flooding mode changing to drop BUM packets.
4394
 */
4395
static void delete_withdraw_type3(struct hash_bucket *bucket, void *data)
4396
0
{
4397
0
  struct bgpevpn *vpn = bucket->data;
4398
0
  struct bgp *bgp = data;
4399
0
  struct prefix_evpn p;
4400
4401
0
  if (!vpn || !is_vni_live(vpn))
4402
0
    return;
4403
4404
0
  build_evpn_type3_prefix(&p, vpn->originator_ip);
4405
0
  delete_evpn_route(bgp, vpn, &p);
4406
0
}
4407
4408
/*
4409
 * Process received EVPN type-2 route (advertise or withdraw).
4410
 */
4411
static int process_type2_route(struct peer *peer, afi_t afi, safi_t safi,
4412
             struct attr *attr, uint8_t *pfx, int psize,
4413
             uint32_t addpath_id)
4414
10
{
4415
10
  struct prefix_rd prd;
4416
10
  struct prefix_evpn p = {};
4417
10
  struct bgp_route_evpn evpn = {};
4418
10
  uint8_t ipaddr_len;
4419
10
  uint8_t macaddr_len;
4420
  /* holds the VNI(s) as in packet */
4421
10
  mpls_label_t label[BGP_MAX_LABELS] = {};
4422
10
  uint32_t num_labels = 0;
4423
10
  uint32_t eth_tag;
4424
10
  int ret = 0;
4425
4426
  /* Type-2 route should be either 33, 37 or 49 bytes or an
4427
   * additional 3 bytes if there is a second label (VNI):
4428
   * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
4429
   * MAC Addr (6), IP len (1), IP (0, 4 or 16),
4430
   * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
4431
   */
4432
10
  if (psize != 33 && psize != 37 && psize != 49 && psize != 36
4433
1
      && psize != 40 && psize != 52) {
4434
1
    flog_err(EC_BGP_EVPN_ROUTE_INVALID,
4435
1
       "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
4436
1
       peer->bgp->vrf_id, peer->host, psize);
4437
1
    return -1;
4438
1
  }
4439
4440
9
  struct stream *pkt = stream_new(psize);
4441
9
  stream_put(pkt, pfx, psize);
4442
4443
  /* Make prefix_rd */
4444
9
  prd.family = AF_UNSPEC;
4445
9
  prd.prefixlen = 64;
4446
4447
9
  STREAM_GET(&prd.val, pkt, 8);
4448
4449
  /* Make EVPN prefix. */
4450
9
  p.family = AF_EVPN;
4451
9
  p.prefixlen = EVPN_ROUTE_PREFIXLEN;
4452
9
  p.prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
4453
4454
  /* Copy Ethernet Seg Identifier */
4455
9
  if (attr) {
4456
2
    STREAM_GET(&attr->esi, pkt, sizeof(esi_t));
4457
4458
2
    if (bgp_evpn_is_esi_local_and_non_bypass(&attr->esi))
4459
0
      attr->es_flags |= ATTR_ES_IS_LOCAL;
4460
2
    else
4461
2
      attr->es_flags &= ~ATTR_ES_IS_LOCAL;
4462
7
  } else {
4463
7
    STREAM_FORWARD_GETP(pkt, sizeof(esi_t));
4464
7
  }
4465
4466
  /* Copy Ethernet Tag */
4467
9
  STREAM_GET(&eth_tag, pkt, 4);
4468
9
  p.prefix.macip_addr.eth_tag = ntohl(eth_tag);
4469
4470
  /* Get the MAC Addr len */
4471
9
  STREAM_GETC(pkt, macaddr_len);
4472
4473
  /* Get the MAC Addr */
4474
9
  if (macaddr_len == (ETH_ALEN * 8)) {
4475
7
    STREAM_GET(&p.prefix.macip_addr.mac.octet, pkt, ETH_ALEN);
4476
7
  } else {
4477
2
    flog_err(
4478
2
      EC_BGP_EVPN_ROUTE_INVALID,
4479
2
      "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
4480
2
      peer->bgp->vrf_id, peer->host, macaddr_len);
4481
2
    goto fail;
4482
2
  }
4483
4484
4485
  /* Get the IP. */
4486
7
  STREAM_GETC(pkt, ipaddr_len);
4487
4488
7
  if (ipaddr_len != 0 && ipaddr_len != IPV4_MAX_BITLEN
4489
0
      && ipaddr_len != IPV6_MAX_BITLEN) {
4490
0
    flog_err(
4491
0
      EC_BGP_EVPN_ROUTE_INVALID,
4492
0
      "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
4493
0
      peer->bgp->vrf_id, peer->host, ipaddr_len);
4494
0
    goto fail;
4495
0
  }
4496
4497
7
  if (ipaddr_len) {
4498
0
    ipaddr_len /= 8; /* Convert to bytes. */
4499
0
    p.prefix.macip_addr.ip.ipa_type = (ipaddr_len == IPV4_MAX_BYTELEN)
4500
0
                 ? IPADDR_V4
4501
0
                 : IPADDR_V6;
4502
0
    STREAM_GET(&p.prefix.macip_addr.ip.ip.addr, pkt, ipaddr_len);
4503
0
  }
4504
4505
  /* Get the VNI(s). Stored as bytes here. */
4506
7
  STREAM_GET(&label[0], pkt, BGP_LABEL_BYTES);
4507
7
  num_labels++;
4508
4509
  /* Do we have a second VNI? */
4510
7
  if (STREAM_READABLE(pkt)) {
4511
0
    num_labels++;
4512
0
    STREAM_GET(&label[1], pkt, BGP_LABEL_BYTES);
4513
0
  }
4514
4515
  /* Process the route. */
4516
7
  if (attr)
4517
2
    bgp_update(peer, (struct prefix *)&p, addpath_id, attr, afi,
4518
2
         safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd,
4519
2
         &label[0], num_labels, 0, &evpn);
4520
5
  else
4521
5
    bgp_withdraw(peer, (struct prefix *)&p, addpath_id, afi, safi,
4522
5
           ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd, &label[0],
4523
5
           num_labels, &evpn);
4524
7
  goto done;
4525
4526
2
fail:
4527
2
stream_failure:
4528
2
  flog_err(EC_BGP_EVPN_ROUTE_INVALID,
4529
2
     "%u:%s - Rx EVPN Type-2 NLRI - corrupt, discarding",
4530
2
     peer->bgp->vrf_id, peer->host);
4531
2
  ret = -1;
4532
9
done:
4533
9
  stream_free(pkt);
4534
9
  return ret;
4535
2
}
4536
4537
/*
4538
 * Process received EVPN type-3 route (advertise or withdraw).
4539
 */
4540
static int process_type3_route(struct peer *peer, afi_t afi, safi_t safi,
4541
             struct attr *attr, uint8_t *pfx, int psize,
4542
             uint32_t addpath_id)
4543
2
{
4544
2
  struct prefix_rd prd;
4545
2
  struct prefix_evpn p;
4546
2
  uint8_t ipaddr_len;
4547
2
  uint32_t eth_tag;
4548
4549
  /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
4550
   * IP len (1) and IP (4 or 16).
4551
   */
4552
2
  if (psize != 17 && psize != 29) {
4553
2
    flog_err(EC_BGP_EVPN_ROUTE_INVALID,
4554
2
       "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
4555
2
       peer->bgp->vrf_id, peer->host, psize);
4556
2
    return -1;
4557
2
  }
4558
4559
  /* If PMSI is present, log if it is anything other than IR.
4560
   * Note: We just simply ignore the values as it is not clear if
4561
   * doing anything else is better.
4562
   */
4563
0
  if (attr &&
4564
0
      (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL))) {
4565
0
    enum pta_type pmsi_tnl_type = bgp_attr_get_pmsi_tnl_type(attr);
4566
4567
0
    if (pmsi_tnl_type != PMSI_TNLTYPE_INGR_REPL
4568
0
        && pmsi_tnl_type != PMSI_TNLTYPE_PIM_SM) {
4569
0
      flog_warn(
4570
0
        EC_BGP_EVPN_PMSI_PRESENT,
4571
0
        "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
4572
0
        peer->bgp->vrf_id, peer->host, pmsi_tnl_type);
4573
0
    }
4574
0
  }
4575
4576
  /* Make prefix_rd */
4577
0
  prd.family = AF_UNSPEC;
4578
0
  prd.prefixlen = 64;
4579
0
  memcpy(&prd.val, pfx, 8);
4580
0
  pfx += 8;
4581
4582
  /* Make EVPN prefix. */
4583
0
  memset(&p, 0, sizeof(p));
4584
0
  p.family = AF_EVPN;
4585
0
  p.prefixlen = EVPN_ROUTE_PREFIXLEN;
4586
0
  p.prefix.route_type = BGP_EVPN_IMET_ROUTE;
4587
4588
  /* Copy Ethernet Tag */
4589
0
  memcpy(&eth_tag, pfx, 4);
4590
0
  p.prefix.imet_addr.eth_tag = ntohl(eth_tag);
4591
0
  pfx += 4;
4592
4593
  /* Get the IP. */
4594
0
  ipaddr_len = *pfx++;
4595
0
  if (ipaddr_len == IPV4_MAX_BITLEN) {
4596
0
    p.prefix.imet_addr.ip.ipa_type = IPADDR_V4;
4597
0
    memcpy(&p.prefix.imet_addr.ip.ip.addr, pfx, IPV4_MAX_BYTELEN);
4598
0
  } else {
4599
0
    flog_err(
4600
0
      EC_BGP_EVPN_ROUTE_INVALID,
4601
0
      "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
4602
0
      peer->bgp->vrf_id, peer->host, ipaddr_len);
4603
0
    return -1;
4604
0
  }
4605
4606
  /* Process the route. */
4607
0
  if (attr)
4608
0
    bgp_update(peer, (struct prefix *)&p, addpath_id, attr, afi,
4609
0
         safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd, NULL,
4610
0
         0, 0, NULL);
4611
0
  else
4612
0
    bgp_withdraw(peer, (struct prefix *)&p, addpath_id, afi, safi,
4613
0
           ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd, NULL, 0,
4614
0
           NULL);
4615
0
  return 0;
4616
0
}
4617
4618
/*
4619
 * Process received EVPN type-5 route (advertise or withdraw).
4620
 */
4621
static int process_type5_route(struct peer *peer, afi_t afi, safi_t safi,
4622
             struct attr *attr, uint8_t *pfx, int psize,
4623
             uint32_t addpath_id)
4624
84
{
4625
84
  struct prefix_rd prd;
4626
84
  struct prefix_evpn p;
4627
84
  struct bgp_route_evpn evpn;
4628
84
  uint8_t ippfx_len;
4629
84
  uint32_t eth_tag;
4630
84
  mpls_label_t label; /* holds the VNI as in the packet */
4631
84
  bool is_valid_update = true;
4632
4633
  /* Type-5 route should be 34 or 58 bytes:
4634
   * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4635
   * GW (4 or 16) and VNI (3).
4636
   * Note that the IP and GW should both be IPv4 or both IPv6.
4637
   */
4638
84
  if (psize != 34 && psize != 58) {
4639
3
    flog_err(EC_BGP_EVPN_ROUTE_INVALID,
4640
3
       "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4641
3
       peer->bgp->vrf_id, peer->host, psize);
4642
3
    return -1;
4643
3
  }
4644
4645
  /* Make prefix_rd */
4646
81
  prd.family = AF_UNSPEC;
4647
81
  prd.prefixlen = 64;
4648
81
  memcpy(&prd.val, pfx, 8);
4649
81
  pfx += 8;
4650
4651
  /* Make EVPN prefix. */
4652
81
  memset(&p, 0, sizeof(p));
4653
81
  p.family = AF_EVPN;
4654
81
  p.prefixlen = EVPN_ROUTE_PREFIXLEN;
4655
81
  p.prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
4656
4657
  /* Additional information outside of prefix - ESI and GW IP */
4658
81
  memset(&evpn, 0, sizeof(evpn));
4659
4660
  /* Fetch ESI overlay index */
4661
81
  if (attr)
4662
78
    memcpy(&evpn.eth_s_id, pfx, sizeof(esi_t));
4663
81
  pfx += ESI_BYTES;
4664
4665
  /* Fetch Ethernet Tag. */
4666
81
  memcpy(&eth_tag, pfx, 4);
4667
81
  p.prefix.prefix_addr.eth_tag = ntohl(eth_tag);
4668
81
  pfx += 4;
4669
4670
  /* Fetch IP prefix length. */
4671
81
  ippfx_len = *pfx++;
4672
81
  if (ippfx_len > IPV6_MAX_BITLEN) {
4673
2
    flog_err(
4674
2
      EC_BGP_EVPN_ROUTE_INVALID,
4675
2
      "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4676
2
      peer->bgp->vrf_id, peer->host, ippfx_len);
4677
2
    return -1;
4678
2
  }
4679
79
  p.prefix.prefix_addr.ip_prefix_length = ippfx_len;
4680
4681
  /* Determine IPv4 or IPv6 prefix */
4682
  /* Since the address and GW are from the same family, this just becomes
4683
   * a simple check on the total size.
4684
   */
4685
79
  if (psize == 34) {
4686
51
    SET_IPADDR_V4(&p.prefix.prefix_addr.ip);
4687
51
    memcpy(&p.prefix.prefix_addr.ip.ipaddr_v4, pfx, 4);
4688
51
    pfx += 4;
4689
51
    SET_IPADDR_V4(&evpn.gw_ip);
4690
51
    memcpy(&evpn.gw_ip.ipaddr_v4, pfx, 4);
4691
51
    pfx += 4;
4692
51
  } else {
4693
28
    SET_IPADDR_V6(&p.prefix.prefix_addr.ip);
4694
28
    memcpy(&p.prefix.prefix_addr.ip.ipaddr_v6, pfx,
4695
28
           IPV6_MAX_BYTELEN);
4696
28
    pfx += IPV6_MAX_BYTELEN;
4697
28
    SET_IPADDR_V6(&evpn.gw_ip);
4698
28
    memcpy(&evpn.gw_ip.ipaddr_v6, pfx, IPV6_MAX_BYTELEN);
4699
28
    pfx += IPV6_MAX_BYTELEN;
4700
28
  }
4701
4702
  /* Get the VNI (in MPLS label field). Stored as bytes here. */
4703
79
  memset(&label, 0, sizeof(label));
4704
79
  memcpy(&label, pfx, BGP_LABEL_BYTES);
4705
4706
  /*
4707
   * If in future, we are required to access additional fields,
4708
   * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4709
   * field
4710
   */
4711
4712
  /*
4713
   * An update containing a non-zero gateway IP and a non-zero ESI
4714
   * at the same time is should be treated as withdraw
4715
   */
4716
79
  if (bgp_evpn_is_esi_valid(&evpn.eth_s_id) &&
4717
48
      !ipaddr_is_zero(&evpn.gw_ip)) {
4718
47
    flog_err(EC_BGP_EVPN_ROUTE_INVALID,
4719
47
       "%s - Rx EVPN Type-5 ESI and gateway-IP both non-zero.",
4720
47
       peer->host);
4721
47
    is_valid_update = false;
4722
47
  } else if (bgp_evpn_is_esi_valid(&evpn.eth_s_id))
4723
1
    evpn.type = OVERLAY_INDEX_ESI;
4724
31
  else if (!ipaddr_is_zero(&evpn.gw_ip))
4725
22
    evpn.type = OVERLAY_INDEX_GATEWAY_IP;
4726
79
  if (attr) {
4727
76
    if (is_zero_mac(&attr->rmac) &&
4728
75
        !bgp_evpn_is_esi_valid(&evpn.eth_s_id) &&
4729
28
        ipaddr_is_zero(&evpn.gw_ip) && label == 0) {
4730
0
      flog_err(EC_BGP_EVPN_ROUTE_INVALID,
4731
0
         "%s - Rx EVPN Type-5 ESI, gateway-IP, RMAC and label all zero",
4732
0
         peer->host);
4733
0
      is_valid_update = false;
4734
0
    }
4735
4736
76
    if (is_mcast_mac(&attr->rmac) || is_bcast_mac(&attr->rmac))
4737
0
      is_valid_update = false;
4738
76
  }
4739
4740
  /* Process the route. */
4741
79
  if (attr && is_valid_update)
4742
29
    bgp_update(peer, (struct prefix *)&p, addpath_id, attr, afi,
4743
29
         safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd,
4744
29
         &label, 1, 0, &evpn);
4745
50
  else {
4746
50
    if (!is_valid_update) {
4747
47
      char attr_str[BUFSIZ] = {0};
4748
4749
47
      bgp_dump_attr(attr, attr_str, BUFSIZ);
4750
47
      zlog_warn(
4751
47
        "Invalid update from peer %s vrf %u prefix %pFX attr %s - treat as withdraw",
4752
47
        peer->hostname, peer->bgp->vrf_id, &p,
4753
47
        attr_str);
4754
47
    }
4755
50
    bgp_withdraw(peer, (struct prefix *)&p, addpath_id, afi, safi,
4756
50
           ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd, &label, 1,
4757
50
           &evpn);
4758
50
  }
4759
4760
79
  return 0;
4761
81
}
4762
4763
static void evpn_mpattr_encode_type5(struct stream *s, const struct prefix *p,
4764
             const struct prefix_rd *prd,
4765
             mpls_label_t *label, uint32_t num_labels,
4766
             struct attr *attr)
4767
0
{
4768
0
  int len;
4769
0
  char temp[16];
4770
0
  const struct evpn_addr *p_evpn_p;
4771
4772
0
  memset(&temp, 0, sizeof(temp));
4773
0
  if (p->family != AF_EVPN)
4774
0
    return;
4775
0
  p_evpn_p = &(p->u.prefix_evpn);
4776
4777
  /* len denites the total len of IP and GW-IP in the route
4778
     IP and GW-IP have to be both ipv4 or ipv6
4779
   */
4780
0
  if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
4781
0
    len = 8; /* IP and GWIP are both ipv4 */
4782
0
  else
4783
0
    len = 32; /* IP and GWIP are both ipv6 */
4784
  /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4785
0
  stream_putc(s, 8 + 10 + 4 + 1 + len + 3);
4786
0
  stream_put(s, prd->val, 8);
4787
0
  if (attr && attr->evpn_overlay.type == OVERLAY_INDEX_ESI)
4788
0
    stream_put(s, &attr->esi, sizeof(esi_t));
4789
0
  else
4790
0
    stream_put(s, 0, sizeof(esi_t));
4791
0
  stream_putl(s, p_evpn_p->prefix_addr.eth_tag);
4792
0
  stream_putc(s, p_evpn_p->prefix_addr.ip_prefix_length);
4793
0
  if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
4794
0
    stream_put_ipv4(s, p_evpn_p->prefix_addr.ip.ipaddr_v4.s_addr);
4795
0
  else
4796
0
    stream_put(s, &p_evpn_p->prefix_addr.ip.ipaddr_v6, 16);
4797
0
  if (attr && attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP) {
4798
0
    const struct bgp_route_evpn *evpn_overlay =
4799
0
      bgp_attr_get_evpn_overlay(attr);
4800
4801
0
    if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
4802
0
      stream_put_ipv4(s,
4803
0
          evpn_overlay->gw_ip.ipaddr_v4.s_addr);
4804
0
    else
4805
0
      stream_put(s, &(evpn_overlay->gw_ip.ipaddr_v6), 16);
4806
0
  } else {
4807
0
    if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
4808
0
      stream_put_ipv4(s, 0);
4809
0
    else
4810
0
      stream_put(s, &temp, 16);
4811
0
  }
4812
4813
0
  if (num_labels)
4814
0
    stream_put(s, label, 3);
4815
0
  else
4816
0
    stream_put3(s, 0);
4817
0
}
4818
4819
/*
4820
 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4821
 */
4822
static void cleanup_vni_on_disable(struct hash_bucket *bucket, struct bgp *bgp)
4823
0
{
4824
0
  struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
4825
4826
  /* Remove EVPN routes and schedule for processing. */
4827
0
  delete_routes_for_vni(bgp, vpn);
4828
4829
  /* Clear "live" flag and see if hash needs to be freed. */
4830
0
  UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
4831
0
  if (!is_vni_configured(vpn))
4832
0
    bgp_evpn_free(bgp, vpn);
4833
0
}
4834
4835
/*
4836
 * Free a VNI entry; iterator function called during cleanup.
4837
 */
4838
static void free_vni_entry(struct hash_bucket *bucket, struct bgp *bgp)
4839
0
{
4840
0
  struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
4841
4842
0
  delete_all_vni_routes(bgp, vpn);
4843
0
  bgp_evpn_free(bgp, vpn);
4844
0
}
4845
4846
/*
4847
 * Derive AUTO import RT for BGP VRF - L3VNI
4848
 */
4849
static void evpn_auto_rt_import_add_for_vrf(struct bgp *bgp_vrf)
4850
0
{
4851
0
  struct bgp *bgp_evpn = NULL;
4852
4853
0
  form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl, true);
4854
4855
  /* Map RT to VRF */
4856
0
  bgp_evpn = bgp_get_evpn();
4857
4858
0
  if (!bgp_evpn)
4859
0
    return;
4860
4861
0
  bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
4862
0
}
4863
4864
/*
4865
 * Delete AUTO import RT from BGP VRF - L3VNI
4866
 */
4867
static void evpn_auto_rt_import_delete_for_vrf(struct bgp *bgp_vrf)
4868
0
{
4869
0
  evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl,
4870
0
          true);
4871
0
}
4872
4873
/*
4874
 * Derive AUTO export RT for BGP VRF - L3VNI
4875
 */
4876
static void evpn_auto_rt_export_add_for_vrf(struct bgp *bgp_vrf)
4877
0
{
4878
0
  form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl, true);
4879
0
}
4880
4881
/*
4882
 * Delete AUTO export RT from BGP VRF - L3VNI
4883
 */
4884
static void evpn_auto_rt_export_delete_for_vrf(struct bgp *bgp_vrf)
4885
0
{
4886
0
  evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl,
4887
0
          true);
4888
0
}
4889
4890
static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp *bgp_vrf)
4891
0
{
4892
0
  struct bgp *bgp_evpn = NULL;
4893
0
  struct listnode *node = NULL;
4894
0
  struct bgpevpn *vpn = NULL;
4895
4896
0
  bgp_evpn = bgp_get_evpn();
4897
0
  if (!bgp_evpn)
4898
0
    return;
4899
4900
  /* update all type-5 routes */
4901
0
  update_advertise_vrf_routes(bgp_vrf);
4902
4903
  /* update all type-2 routes */
4904
0
  for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
4905
0
    update_routes_for_vni(bgp_evpn, vpn);
4906
0
}
4907
4908
/*
4909
 * Handle autort change for a given VNI.
4910
 */
4911
static void update_autort_vni(struct hash_bucket *bucket, struct bgp *bgp)
4912
0
{
4913
0
  struct bgpevpn *vpn = bucket->data;
4914
4915
0
  if (!is_import_rt_configured(vpn)) {
4916
0
    if (is_vni_live(vpn))
4917
0
      bgp_evpn_uninstall_routes(bgp, vpn);
4918
0
    bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
4919
0
    list_delete_all_node(vpn->import_rtl);
4920
0
    bgp_evpn_derive_auto_rt_import(bgp, vpn);
4921
0
    if (is_vni_live(vpn))
4922
0
      bgp_evpn_install_routes(bgp, vpn);
4923
0
  }
4924
0
  if (!is_export_rt_configured(vpn)) {
4925
0
    list_delete_all_node(vpn->export_rtl);
4926
0
    bgp_evpn_derive_auto_rt_export(bgp, vpn);
4927
0
    if (is_vni_live(vpn))
4928
0
      bgp_evpn_handle_export_rt_change(bgp, vpn);
4929
0
  }
4930
0
}
4931
4932
/*
4933
 * Handle autort change for L3VNI.
4934
 */
4935
static void update_autort_l3vni(struct bgp *bgp)
4936
0
{
4937
0
  if ((CHECK_FLAG(bgp->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
4938
0
      && (CHECK_FLAG(bgp->vrf_flags, BGP_VRF_EXPORT_RT_CFGD)))
4939
0
    return;
4940
4941
0
  if (!CHECK_FLAG(bgp->vrf_flags, BGP_VRF_IMPORT_RT_CFGD)) {
4942
0
    if (is_l3vni_live(bgp))
4943
0
      uninstall_routes_for_vrf(bgp);
4944
4945
    /* Cleanup the RT to VRF mapping */
4946
0
    bgp_evpn_unmap_vrf_from_its_rts(bgp);
4947
4948
    /* Remove auto generated RT */
4949
0
    evpn_auto_rt_import_delete_for_vrf(bgp);
4950
4951
0
    list_delete_all_node(bgp->vrf_import_rtl);
4952
4953
    /* Map auto derive or configured RTs */
4954
0
    evpn_auto_rt_import_add_for_vrf(bgp);
4955
0
  }
4956
4957
0
  if (!CHECK_FLAG(bgp->vrf_flags, BGP_VRF_EXPORT_RT_CFGD)) {
4958
0
    list_delete_all_node(bgp->vrf_export_rtl);
4959
4960
0
    evpn_auto_rt_export_delete_for_vrf(bgp);
4961
4962
0
    evpn_auto_rt_export_add_for_vrf(bgp);
4963
4964
0
    if (is_l3vni_live(bgp))
4965
0
      bgp_evpn_map_vrf_to_its_rts(bgp);
4966
0
  }
4967
4968
0
  if (!is_l3vni_live(bgp))
4969
0
    return;
4970
4971
  /* advertise type-5 routes if needed */
4972
0
  update_advertise_vrf_routes(bgp);
4973
4974
  /* install all remote routes belonging to this l3vni
4975
   * into corresponding vrf
4976
   */
4977
0
  install_routes_for_vrf(bgp);
4978
0
}
4979
4980
/*
4981
 * Public functions.
4982
 */
4983
4984
/* withdraw type-5 route corresponding to ip prefix */
4985
void bgp_evpn_withdraw_type5_route(struct bgp *bgp_vrf, const struct prefix *p,
4986
           afi_t afi, safi_t safi)
4987
0
{
4988
0
  int ret = 0;
4989
0
  struct prefix_evpn evp;
4990
4991
0
  build_type5_prefix_from_ip_prefix(&evp, p);
4992
0
  ret = delete_evpn_type5_route(bgp_vrf, &evp);
4993
0
  if (ret)
4994
0
    flog_err(
4995
0
      EC_BGP_EVPN_ROUTE_DELETE,
4996
0
      "%u failed to delete type-5 route for prefix %pFX in vrf %s",
4997
0
      bgp_vrf->vrf_id, p, vrf_id_to_name(bgp_vrf->vrf_id));
4998
0
}
4999
5000
/* withdraw all type-5 routes for an address family */
5001
void bgp_evpn_withdraw_type5_routes(struct bgp *bgp_vrf, afi_t afi, safi_t safi)
5002
0
{
5003
0
  struct bgp_table *table = NULL;
5004
0
  struct bgp_dest *dest = NULL;
5005
0
  struct bgp_path_info *pi;
5006
5007
0
  table = bgp_vrf->rib[afi][safi];
5008
0
  for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
5009
    /* Only care about "selected" routes. Also ensure that
5010
     * these are routes that are injectable into EVPN.
5011
     */
5012
    /* TODO: Support for AddPath for EVPN. */
5013
0
    for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
5014
0
      if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
5015
0
          && is_route_injectable_into_evpn(pi)) {
5016
0
        bgp_evpn_withdraw_type5_route(
5017
0
          bgp_vrf, bgp_dest_get_prefix(dest), afi,
5018
0
          safi);
5019
0
        break;
5020
0
      }
5021
0
    }
5022
0
  }
5023
0
}
5024
5025
/*
5026
 * evpn - enable advertisement of default g/w
5027
 */
5028
void bgp_evpn_install_uninstall_default_route(struct bgp *bgp_vrf, afi_t afi,
5029
                safi_t safi, bool add)
5030
0
{
5031
0
  struct prefix ip_prefix;
5032
5033
  /* form the default prefix 0.0.0.0/0 */
5034
0
  memset(&ip_prefix, 0, sizeof(ip_prefix));
5035
0
  ip_prefix.family = afi2family(afi);
5036
5037
0
  if (add) {
5038
0
    bgp_evpn_advertise_type5_route(bgp_vrf, &ip_prefix,
5039
0
                 NULL, afi, safi);
5040
0
  } else {
5041
0
    bgp_evpn_withdraw_type5_route(bgp_vrf, &ip_prefix,
5042
0
                afi, safi);
5043
0
  }
5044
0
}
5045
5046
5047
/*
5048
 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
5049
 * to this function correspond to those of the source IP prefix (best
5050
 * path in the case of the attr. In the case of a local prefix (when we
5051
 * are advertising local subnets), the src_attr will be NULL.
5052
 */
5053
void bgp_evpn_advertise_type5_route(struct bgp *bgp_vrf, const struct prefix *p,
5054
            struct attr *src_attr, afi_t afi,
5055
            safi_t safi)
5056
0
{
5057
0
  int ret = 0;
5058
0
  struct prefix_evpn evp;
5059
5060
0
  build_type5_prefix_from_ip_prefix(&evp, p);
5061
0
  ret = update_evpn_type5_route(bgp_vrf, &evp, src_attr, afi, safi);
5062
0
  if (ret)
5063
0
    flog_err(EC_BGP_EVPN_ROUTE_CREATE,
5064
0
       "%u: Failed to create type-5 route for prefix %pFX",
5065
0
       bgp_vrf->vrf_id, p);
5066
0
}
5067
5068
/* Inject all prefixes of a particular address-family (currently, IPv4 or
5069
 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
5070
 * advertisement is enabled.
5071
 */
5072
void bgp_evpn_advertise_type5_routes(struct bgp *bgp_vrf, afi_t afi,
5073
             safi_t safi)
5074
0
{
5075
0
  struct bgp_table *table = NULL;
5076
0
  struct bgp_dest *dest = NULL;
5077
0
  struct bgp_path_info *pi;
5078
5079
0
  table = bgp_vrf->rib[afi][safi];
5080
0
  for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
5081
    /* Need to identify the "selected" route entry to use its
5082
     * attribute. Also, ensure that the route is injectable
5083
     * into EVPN.
5084
     * TODO: Support for AddPath for EVPN.
5085
     */
5086
0
    for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
5087
0
      if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
5088
0
          && is_route_injectable_into_evpn(pi)) {
5089
5090
        /* apply the route-map */
5091
0
        if (bgp_vrf->adv_cmd_rmap[afi][safi].map) {
5092
0
          route_map_result_t ret;
5093
0
          struct bgp_path_info tmp_pi;
5094
0
          struct bgp_path_info_extra tmp_pie;
5095
0
          struct attr tmp_attr;
5096
5097
0
          tmp_attr = *pi->attr;
5098
5099
          /* Fill temp path_info */
5100
0
          prep_for_rmap_apply(&tmp_pi, &tmp_pie,
5101
0
                  dest, pi, pi->peer,
5102
0
                  &tmp_attr);
5103
5104
0
          RESET_FLAG(tmp_attr.rmap_change_flags);
5105
5106
0
          ret = route_map_apply(
5107
0
            bgp_vrf->adv_cmd_rmap[afi][safi]
5108
0
              .map,
5109
0
            bgp_dest_get_prefix(dest),
5110
0
            &tmp_pi);
5111
0
          if (ret == RMAP_DENYMATCH) {
5112
0
            bgp_attr_flush(&tmp_attr);
5113
0
            continue;
5114
0
          }
5115
0
          bgp_evpn_advertise_type5_route(
5116
0
            bgp_vrf,
5117
0
            bgp_dest_get_prefix(dest),
5118
0
            &tmp_attr, afi, safi);
5119
0
        } else
5120
0
          bgp_evpn_advertise_type5_route(
5121
0
            bgp_vrf,
5122
0
            bgp_dest_get_prefix(dest),
5123
0
            pi->attr, afi, safi);
5124
0
        break;
5125
0
      }
5126
0
    }
5127
0
  }
5128
0
}
5129
5130
static void rt_list_remove_node(struct list *rt_list,
5131
        struct ecommunity *ecomdel, bool is_l3)
5132
0
{
5133
0
  struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
5134
0
  struct vrf_route_target *l3rt = NULL;
5135
0
  struct ecommunity *ecom = NULL;
5136
5137
0
  if (is_l3) {
5138
0
    for (ALL_LIST_ELEMENTS(rt_list, node, nnode, l3rt)) {
5139
0
      if (ecommunity_match(l3rt->ecom, ecomdel)) {
5140
0
        evpn_vrf_rt_del(l3rt);
5141
0
        node_to_del = node;
5142
0
        break;
5143
0
      }
5144
0
    }
5145
0
  } else {
5146
0
    for (ALL_LIST_ELEMENTS(rt_list, node, nnode, ecom)) {
5147
0
      if (ecommunity_match(ecom, ecomdel)) {
5148
0
        ecommunity_free(&ecom);
5149
0
        node_to_del = node;
5150
0
        break;
5151
0
      }
5152
0
    }
5153
0
  }
5154
5155
5156
0
  if (node_to_del)
5157
0
    list_delete_node(rt_list, node_to_del);
5158
0
}
5159
5160
void evpn_rt_delete_auto(struct bgp *bgp, vni_t vni, struct list *rtl,
5161
       bool is_l3)
5162
0
{
5163
0
  struct ecommunity *ecom_auto;
5164
0
  struct ecommunity_val eval;
5165
5166
0
  if (bgp->advertise_autort_rfc8365)
5167
0
    vni |= EVPN_AUTORT_VXLAN;
5168
5169
0
  encode_route_target_as((bgp->as & 0xFFFF), vni, &eval, true);
5170
5171
0
  ecom_auto = ecommunity_new();
5172
0
  ecommunity_add_val(ecom_auto, &eval, false, false);
5173
5174
0
  rt_list_remove_node(rtl, ecom_auto, is_l3);
5175
5176
0
  ecommunity_free(&ecom_auto);
5177
0
}
5178
5179
static void evpn_vrf_rt_routes_map(struct bgp *bgp_vrf)
5180
0
{
5181
  /* map VRFs to its RTs and install routes matching this new RT */
5182
0
  if (is_l3vni_live(bgp_vrf)) {
5183
0
    bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
5184
0
    install_routes_for_vrf(bgp_vrf);
5185
0
  }
5186
0
}
5187
5188
static void evpn_vrf_rt_routes_unmap(struct bgp *bgp_vrf)
5189
0
{
5190
  /* uninstall routes from vrf */
5191
0
  if (is_l3vni_live(bgp_vrf))
5192
0
    uninstall_routes_for_vrf(bgp_vrf);
5193
5194
  /* Cleanup the RT to VRF mapping */
5195
0
  bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
5196
0
}
5197
5198
static bool rt_list_has_cfgd_rt(struct list *rt_list)
5199
0
{
5200
0
  struct listnode *node = NULL, *nnode = NULL;
5201
0
  struct vrf_route_target *l3rt = NULL;
5202
5203
0
  for (ALL_LIST_ELEMENTS(rt_list, node, nnode, l3rt)) {
5204
0
    if (!CHECK_FLAG(l3rt->flags, BGP_VRF_RT_AUTO))
5205
0
      return true;
5206
0
  }
5207
5208
0
  return false;
5209
0
}
5210
5211
static void unconfigure_import_rt_for_vrf_fini(struct bgp *bgp_vrf)
5212
0
{
5213
0
  if (!bgp_vrf->vrf_import_rtl)
5214
0
    return; /* this should never fail */
5215
5216
0
  if (!is_l3vni_live(bgp_vrf))
5217
0
    return; /* Nothing to do if no vni */
5218
5219
  /* fall back to auto-generated RT if this was the last RT */
5220
0
  if (list_isempty(bgp_vrf->vrf_import_rtl))
5221
0
    evpn_auto_rt_import_add_for_vrf(bgp_vrf);
5222
0
}
5223
5224
static void unconfigure_export_rt_for_vrf_fini(struct bgp *bgp_vrf)
5225
0
{
5226
5227
0
  if (!bgp_vrf->vrf_export_rtl)
5228
0
    return; /* this should never fail */
5229
5230
0
  if (!is_l3vni_live(bgp_vrf))
5231
0
    return; /* Nothing to do if no vni */
5232
5233
  /* fall back to auto-generated RT if this was the last RT */
5234
0
  if (list_isempty(bgp_vrf->vrf_export_rtl))
5235
0
    evpn_auto_rt_export_add_for_vrf(bgp_vrf);
5236
5237
0
  bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
5238
0
}
5239
5240
void bgp_evpn_configure_import_rt_for_vrf(struct bgp *bgp_vrf,
5241
            struct ecommunity *ecomadd,
5242
            bool is_wildcard)
5243
0
{
5244
0
  struct vrf_route_target *newrt;
5245
5246
0
  newrt = evpn_vrf_rt_new(ecomadd);
5247
5248
0
  if (is_wildcard)
5249
0
    SET_FLAG(newrt->flags, BGP_VRF_RT_WILD);
5250
5251
0
  evpn_vrf_rt_routes_unmap(bgp_vrf);
5252
5253
  /* Remove auto generated RT if not configured */
5254
0
  if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_AUTO_RT_CFGD))
5255
0
    evpn_auto_rt_import_delete_for_vrf(bgp_vrf);
5256
5257
  /* Add the newly configured RT to RT list */
5258
0
  listnode_add_sort(bgp_vrf->vrf_import_rtl, newrt);
5259
5260
0
  SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
5261
5262
0
  evpn_vrf_rt_routes_map(bgp_vrf);
5263
0
}
5264
5265
void bgp_evpn_configure_import_auto_rt_for_vrf(struct bgp *bgp_vrf)
5266
0
{
5267
0
  if (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_AUTO_RT_CFGD))
5268
0
    return; /* Already configured */
5269
5270
0
  SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_AUTO_RT_CFGD);
5271
5272
0
  if (!is_l3vni_live(bgp_vrf))
5273
0
    return; /* Wait for VNI before adding rts */
5274
5275
0
  evpn_vrf_rt_routes_unmap(bgp_vrf);
5276
5277
0
  evpn_auto_rt_import_add_for_vrf(bgp_vrf);
5278
5279
0
  evpn_vrf_rt_routes_map(bgp_vrf);
5280
0
}
5281
5282
void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf,
5283
              struct ecommunity *ecomdel)
5284
0
{
5285
0
  if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
5286
0
    return; /* Already un-configured */
5287
5288
0
  evpn_vrf_rt_routes_unmap(bgp_vrf);
5289
5290
  /* Remove rt */
5291
0
  rt_list_remove_node(bgp_vrf->vrf_import_rtl, ecomdel, true);
5292
5293
0
  if (!rt_list_has_cfgd_rt(bgp_vrf->vrf_import_rtl))
5294
0
    UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
5295
5296
0
  unconfigure_import_rt_for_vrf_fini(bgp_vrf);
5297
5298
0
  evpn_vrf_rt_routes_map(bgp_vrf);
5299
0
}
5300
5301
void bgp_evpn_unconfigure_import_auto_rt_for_vrf(struct bgp *bgp_vrf)
5302
0
{
5303
0
  if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_AUTO_RT_CFGD))
5304
0
    return; /* Already un-configured */
5305
5306
0
  evpn_vrf_rt_routes_unmap(bgp_vrf);
5307
5308
  /* remove auto-generated RT */
5309
0
  evpn_auto_rt_import_delete_for_vrf(bgp_vrf);
5310
5311
0
  UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_AUTO_RT_CFGD);
5312
5313
0
  unconfigure_import_rt_for_vrf_fini(bgp_vrf);
5314
5315
0
  evpn_vrf_rt_routes_map(bgp_vrf);
5316
0
}
5317
5318
void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf,
5319
            struct ecommunity *ecomadd)
5320
0
{
5321
0
  struct vrf_route_target *newrt;
5322
5323
0
  newrt = evpn_vrf_rt_new(ecomadd);
5324
5325
  /* Remove auto generated RT if not configured */
5326
0
  if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_AUTO_RT_CFGD))
5327
0
    evpn_auto_rt_export_delete_for_vrf(bgp_vrf);
5328
5329
  /* Add the new RT to the RT list */
5330
0
  listnode_add_sort(bgp_vrf->vrf_export_rtl, newrt);
5331
5332
0
  SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
5333
5334
0
  if (is_l3vni_live(bgp_vrf))
5335
0
    bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
5336
0
}
5337
5338
void bgp_evpn_configure_export_auto_rt_for_vrf(struct bgp *bgp_vrf)
5339
0
{
5340
0
  if (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_AUTO_RT_CFGD))
5341
0
    return; /* Already configured */
5342
5343
0
  SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_AUTO_RT_CFGD);
5344
5345
0
  if (!is_l3vni_live(bgp_vrf))
5346
0
    return; /* Wait for VNI before adding rts */
5347
5348
0
  evpn_auto_rt_export_add_for_vrf(bgp_vrf);
5349
5350
0
  bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
5351
0
}
5352
5353
void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf,
5354
              struct ecommunity *ecomdel)
5355
0
{
5356
0
  if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD))
5357
0
    return; /* Already un-configured */
5358
5359
  /* Remove rt */
5360
0
  rt_list_remove_node(bgp_vrf->vrf_export_rtl, ecomdel, true);
5361
5362
0
  if (!rt_list_has_cfgd_rt(bgp_vrf->vrf_export_rtl))
5363
0
    UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
5364
5365
0
  unconfigure_export_rt_for_vrf_fini(bgp_vrf);
5366
0
}
5367
5368
void bgp_evpn_unconfigure_export_auto_rt_for_vrf(struct bgp *bgp_vrf)
5369
0
{
5370
0
  if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_AUTO_RT_CFGD))
5371
0
    return; /* Already un-configured */
5372
5373
  /* remove auto-generated RT */
5374
0
  evpn_auto_rt_export_delete_for_vrf(bgp_vrf);
5375
5376
0
  UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_AUTO_RT_CFGD);
5377
5378
0
  unconfigure_export_rt_for_vrf_fini(bgp_vrf);
5379
0
}
5380
5381
/*
5382
 * Handle change to BGP router id. This is invoked twice by the change
5383
 * handler, first before the router id has been changed and then after
5384
 * the router id has been changed. The first invocation will result in
5385
 * local routes for all VNIs/VRF being deleted and withdrawn and the next
5386
 * will result in the routes being re-advertised.
5387
 */
5388
void bgp_evpn_handle_router_id_update(struct bgp *bgp, int withdraw)
5389
0
{
5390
0
  struct listnode *node;
5391
0
  struct bgp *bgp_vrf;
5392
5393
0
  if (withdraw) {
5394
5395
    /* delete and withdraw all the type-5 routes
5396
       stored in the global table for this vrf
5397
     */
5398
0
    withdraw_router_id_vrf(bgp);
5399
5400
    /* delete all the VNI routes (type-2/type-3) routes for all the
5401
     * L2-VNIs
5402
     */
5403
0
    hash_iterate(bgp->vnihash,
5404
0
           (void (*)(struct hash_bucket *,
5405
0
               void *))withdraw_router_id_vni,
5406
0
           bgp);
5407
5408
0
    if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
5409
0
      for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
5410
0
        if (bgp_vrf->evpn_info->advertise_pip &&
5411
0
            (bgp_vrf->evpn_info->pip_ip_static.s_addr
5412
0
             == INADDR_ANY))
5413
0
          bgp_vrf->evpn_info->pip_ip.s_addr
5414
0
            = INADDR_ANY;
5415
0
      }
5416
0
    }
5417
0
  } else {
5418
5419
    /* Assign new default instance router-id */
5420
0
    if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
5421
0
      for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
5422
0
        if (bgp_vrf->evpn_info->advertise_pip &&
5423
0
            (bgp_vrf->evpn_info->pip_ip_static.s_addr
5424
0
             == INADDR_ANY)) {
5425
0
          bgp_vrf->evpn_info->pip_ip =
5426
0
            bgp->router_id;
5427
          /* advertise type-5 routes with
5428
           * new nexthop
5429
           */
5430
0
          update_advertise_vrf_routes(bgp_vrf);
5431
0
        }
5432
0
      }
5433
0
    }
5434
5435
    /* advertise all routes in the vrf as type-5 routes with the new
5436
     * RD
5437
     */
5438
0
    update_router_id_vrf(bgp);
5439
5440
    /* advertise all the VNI routes (type-2/type-3) routes with the
5441
     * new RD
5442
     */
5443
0
    hash_iterate(bgp->vnihash,
5444
0
           (void (*)(struct hash_bucket *,
5445
0
               void *))update_router_id_vni,
5446
0
           bgp);
5447
0
  }
5448
0
}
5449
5450
/*
5451
 * Handle change to auto-RT algorithm - update and advertise local routes.
5452
 */
5453
void bgp_evpn_handle_autort_change(struct bgp *bgp)
5454
0
{
5455
0
  hash_iterate(bgp->vnihash,
5456
0
         (void (*)(struct hash_bucket *,
5457
0
             void*))update_autort_vni,
5458
0
         bgp);
5459
0
  if (bgp->l3vni)
5460
0
    update_autort_l3vni(bgp);
5461
0
}
5462
5463
/*
5464
 * Handle change to export RT - update and advertise local routes.
5465
 */
5466
int bgp_evpn_handle_export_rt_change(struct bgp *bgp, struct bgpevpn *vpn)
5467
0
{
5468
0
  return update_routes_for_vni(bgp, vpn);
5469
0
}
5470
5471
void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw)
5472
0
{
5473
0
  if (withdraw)
5474
0
    delete_withdraw_vrf_routes(bgp_vrf);
5475
0
  else
5476
0
    update_advertise_vrf_routes(bgp_vrf);
5477
0
}
5478
5479
/*
5480
 * Handle change to RD. This is invoked twice by the change handler,
5481
 * first before the RD has been changed and then after the RD has
5482
 * been changed. The first invocation will result in local routes
5483
 * of this VNI being deleted and withdrawn and the next will result
5484
 * in the routes being re-advertised.
5485
 */
5486
void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
5487
             int withdraw)
5488
0
{
5489
0
  if (withdraw)
5490
0
    delete_withdraw_vni_routes(bgp, vpn);
5491
0
  else
5492
0
    update_advertise_vni_routes(bgp, vpn);
5493
0
}
5494
5495
/*
5496
 * Install routes for this VNI. Invoked upon change to Import RT.
5497
 */
5498
int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn)
5499
0
{
5500
0
  return install_routes_for_vni(bgp, vpn);
5501
0
}
5502
5503
/*
5504
 * Uninstall all routes installed for this VNI. Invoked upon change
5505
 * to Import RT.
5506
 */
5507
int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn)
5508
0
{
5509
0
  return uninstall_routes_for_vni(bgp, vpn);
5510
0
}
5511
5512
/*
5513
 * TODO: Hardcoded for a maximum of 2 VNIs right now
5514
 */
5515
char *bgp_evpn_label2str(mpls_label_t *label, uint32_t num_labels, char *buf,
5516
       int len)
5517
0
{
5518
0
  vni_t vni1, vni2;
5519
5520
0
  vni1 = label2vni(label);
5521
0
  if (num_labels == 2) {
5522
0
    vni2 = label2vni(label + 1);
5523
0
    snprintf(buf, len, "%u/%u", vni1, vni2);
5524
0
  } else
5525
0
    snprintf(buf, len, "%u", vni1);
5526
0
  return buf;
5527
0
}
5528
5529
/*
5530
 * Function to convert evpn route to json format.
5531
 * NOTE: We don't use prefix2str as the output here is a bit different.
5532
 */
5533
void bgp_evpn_route2json(const struct prefix_evpn *p, json_object *json)
5534
0
{
5535
0
  char buf1[ETHER_ADDR_STRLEN];
5536
0
  char buf2[PREFIX2STR_BUFFER];
5537
0
  uint8_t family;
5538
0
  uint8_t prefixlen;
5539
5540
0
  if (!json)
5541
0
    return;
5542
5543
0
  json_object_int_add(json, "routeType", p->prefix.route_type);
5544
5545
0
  switch (p->prefix.route_type) {
5546
0
  case BGP_EVPN_MAC_IP_ROUTE:
5547
0
    json_object_int_add(json, "ethTag",
5548
0
      p->prefix.macip_addr.eth_tag);
5549
0
    json_object_int_add(json, "macLen", 8 * ETH_ALEN);
5550
0
    json_object_string_add(json, "mac",
5551
0
      prefix_mac2str(&p->prefix.macip_addr.mac, buf1,
5552
0
      sizeof(buf1)));
5553
5554
0
    if (!is_evpn_prefix_ipaddr_none(p)) {
5555
0
      family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET :
5556
0
        AF_INET6;
5557
0
      prefixlen = (family == AF_INET) ?
5558
0
        IPV4_MAX_BITLEN : IPV6_MAX_BITLEN;
5559
0
      inet_ntop(family, &p->prefix.macip_addr.ip.ip.addr,
5560
0
        buf2, PREFIX2STR_BUFFER);
5561
0
      json_object_int_add(json, "ipLen", prefixlen);
5562
0
      json_object_string_add(json, "ip", buf2);
5563
0
    }
5564
0
  break;
5565
5566
0
  case BGP_EVPN_IMET_ROUTE:
5567
0
    json_object_int_add(json, "ethTag",
5568
0
      p->prefix.imet_addr.eth_tag);
5569
0
    family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET : AF_INET6;
5570
0
    prefixlen = (family == AF_INET) ?  IPV4_MAX_BITLEN :
5571
0
      IPV6_MAX_BITLEN;
5572
0
    inet_ntop(family, &p->prefix.imet_addr.ip.ip.addr, buf2,
5573
0
      PREFIX2STR_BUFFER);
5574
0
    json_object_int_add(json, "ipLen", prefixlen);
5575
0
    json_object_string_add(json, "ip", buf2);
5576
0
  break;
5577
5578
0
  case BGP_EVPN_IP_PREFIX_ROUTE:
5579
0
    json_object_int_add(json, "ethTag",
5580
0
      p->prefix.prefix_addr.eth_tag);
5581
0
    family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET : AF_INET6;
5582
0
    inet_ntop(family, &p->prefix.prefix_addr.ip.ip.addr,
5583
0
        buf2, sizeof(buf2));
5584
0
    json_object_int_add(json, "ipLen",
5585
0
            p->prefix.prefix_addr.ip_prefix_length);
5586
0
    json_object_string_add(json, "ip", buf2);
5587
0
  break;
5588
5589
0
  default:
5590
0
  break;
5591
0
  }
5592
0
}
5593
5594
/*
5595
 * Encode EVPN prefix in Update (MP_REACH)
5596
 */
5597
void bgp_evpn_encode_prefix(struct stream *s, const struct prefix *p,
5598
          const struct prefix_rd *prd, mpls_label_t *label,
5599
          uint32_t num_labels, struct attr *attr,
5600
          bool addpath_capable, uint32_t addpath_tx_id)
5601
0
{
5602
0
  struct prefix_evpn *evp = (struct prefix_evpn *)p;
5603
0
  int len, ipa_len = 0;
5604
5605
0
  if (addpath_capable)
5606
0
    stream_putl(s, addpath_tx_id);
5607
5608
  /* Route type */
5609
0
  stream_putc(s, evp->prefix.route_type);
5610
5611
0
  switch (evp->prefix.route_type) {
5612
0
  case BGP_EVPN_MAC_IP_ROUTE:
5613
0
    if (is_evpn_prefix_ipaddr_v4(evp))
5614
0
      ipa_len = IPV4_MAX_BYTELEN;
5615
0
    else if (is_evpn_prefix_ipaddr_v6(evp))
5616
0
      ipa_len = IPV6_MAX_BYTELEN;
5617
    /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
5618
0
    len = 8 + 10 + 4 + 1 + 6 + 1 + ipa_len + 3;
5619
0
    if (ipa_len && num_labels > 1) /* There are 2 VNIs */
5620
0
      len += 3;
5621
0
    stream_putc(s, len);
5622
0
    stream_put(s, prd->val, 8);   /* RD */
5623
0
    if (attr)
5624
0
      stream_put(s, &attr->esi, ESI_BYTES);
5625
0
    else
5626
0
      stream_put(s, 0, 10);
5627
0
    stream_putl(s, evp->prefix.macip_addr.eth_tag);  /* Ethernet Tag ID */
5628
0
    stream_putc(s, 8 * ETH_ALEN); /* Mac Addr Len - bits */
5629
0
    stream_put(s, evp->prefix.macip_addr.mac.octet, 6); /* Mac Addr */
5630
0
    stream_putc(s, 8 * ipa_len); /* IP address Length */
5631
0
    if (ipa_len) /* IP */
5632
0
      stream_put(s, &evp->prefix.macip_addr.ip.ip.addr,
5633
0
           ipa_len);
5634
    /* 1st label is the L2 VNI */
5635
0
    stream_put(s, label, BGP_LABEL_BYTES);
5636
    /* Include 2nd label (L3 VNI) if advertising MAC+IP */
5637
0
    if (ipa_len && num_labels > 1)
5638
0
      stream_put(s, label + 1, BGP_LABEL_BYTES);
5639
0
    break;
5640
5641
0
  case BGP_EVPN_IMET_ROUTE:
5642
0
    stream_putc(s, 17); // TODO: length - assumes IPv4 address
5643
0
    stream_put(s, prd->val, 8);      /* RD */
5644
0
    stream_putl(s, evp->prefix.imet_addr.eth_tag); /* Ethernet Tag ID */
5645
0
    stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
5646
    /* Originating Router's IP Addr */
5647
0
    stream_put_in_addr(s, &evp->prefix.imet_addr.ip.ipaddr_v4);
5648
0
    break;
5649
5650
0
  case BGP_EVPN_ES_ROUTE:
5651
0
    stream_putc(s, 23); /* TODO: length: assumes ipv4 VTEP */
5652
0
    stream_put(s, prd->val, 8); /* RD */
5653
0
    stream_put(s, evp->prefix.es_addr.esi.val, 10); /* ESI */
5654
0
    stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
5655
    /* VTEP IP */
5656
0
    stream_put_in_addr(s, &evp->prefix.es_addr.ip.ipaddr_v4);
5657
0
    break;
5658
5659
0
  case BGP_EVPN_AD_ROUTE:
5660
    /* RD, ESI, EthTag, 1 VNI */
5661
0
    len = RD_BYTES + ESI_BYTES + EVPN_ETH_TAG_BYTES + BGP_LABEL_BYTES;
5662
0
    stream_putc(s, len);
5663
0
    stream_put(s, prd->val, RD_BYTES); /* RD */
5664
0
    stream_put(s, evp->prefix.ead_addr.esi.val, ESI_BYTES); /* ESI */
5665
0
    stream_putl(s, evp->prefix.ead_addr.eth_tag); /* Ethernet Tag */
5666
0
    stream_put(s, label, BGP_LABEL_BYTES);
5667
0
    break;
5668
5669
0
  case BGP_EVPN_IP_PREFIX_ROUTE:
5670
    /* TODO: AddPath support. */
5671
0
    evpn_mpattr_encode_type5(s, p, prd, label, num_labels, attr);
5672
0
    break;
5673
5674
0
  default:
5675
0
    break;
5676
0
  }
5677
0
}
5678
5679
int bgp_nlri_parse_evpn(struct peer *peer, struct attr *attr,
5680
      struct bgp_nlri *packet, bool withdraw)
5681
39
{
5682
39
  uint8_t *pnt;
5683
39
  uint8_t *lim;
5684
39
  afi_t afi;
5685
39
  safi_t safi;
5686
39
  uint32_t addpath_id;
5687
39
  bool addpath_capable;
5688
39
  int psize = 0;
5689
39
  uint8_t rtype;
5690
39
  struct prefix p;
5691
5692
  /* Start processing the NLRI - there may be multiple in the MP_REACH */
5693
39
  pnt = packet->nlri;
5694
39
  lim = pnt + packet->length;
5695
39
  afi = packet->afi;
5696
39
  safi = packet->safi;
5697
39
  addpath_id = 0;
5698
5699
39
  addpath_capable = bgp_addpath_encode_rx(peer, afi, safi);
5700
5701
220
  for (; pnt < lim; pnt += psize) {
5702
    /* Clear prefix structure. */
5703
220
    memset(&p, 0, sizeof(p));
5704
5705
    /* Deal with path-id if AddPath is supported. */
5706
220
    if (addpath_capable) {
5707
      /* When packet overflow occurs return immediately. */
5708
220
      if (pnt + BGP_ADDPATH_ID_LEN > lim)
5709
2
        return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
5710
5711
218
      memcpy(&addpath_id, pnt, BGP_ADDPATH_ID_LEN);
5712
218
      addpath_id = ntohl(addpath_id);
5713
218
      pnt += BGP_ADDPATH_ID_LEN;
5714
218
    }
5715
5716
    /* All EVPN NLRI types start with type and length. */
5717
218
    if (pnt + 2 > lim)
5718
2
      return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE;
5719
5720
216
    rtype = *pnt++;
5721
216
    psize = *pnt++;
5722
5723
    /* When packet overflow occur return immediately. */
5724
216
    if (pnt + psize > lim)
5725
22
      return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
5726
5727
194
    switch (rtype) {
5728
10
    case BGP_EVPN_MAC_IP_ROUTE:
5729
10
      if (process_type2_route(peer, afi, safi,
5730
10
            withdraw ? NULL : attr, pnt,
5731
10
            psize, addpath_id)) {
5732
3
        flog_err(
5733
3
          EC_BGP_EVPN_FAIL,
5734
3
          "%u:%s - Error in processing EVPN type-2 NLRI size %d",
5735
3
          peer->bgp->vrf_id, peer->host, psize);
5736
3
        return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE;
5737
3
      }
5738
7
      break;
5739
5740
7
    case BGP_EVPN_IMET_ROUTE:
5741
2
      if (process_type3_route(peer, afi, safi,
5742
2
            withdraw ? NULL : attr, pnt,
5743
2
            psize, addpath_id)) {
5744
2
        flog_err(
5745
2
          EC_BGP_PKT_PROCESS,
5746
2
          "%u:%s - Error in processing EVPN type-3 NLRI size %d",
5747
2
          peer->bgp->vrf_id, peer->host, psize);
5748
2
        return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE;
5749
2
      }
5750
0
      break;
5751
5752
1
    case BGP_EVPN_ES_ROUTE:
5753
1
      if (bgp_evpn_type4_route_process(peer, afi, safi,
5754
1
            withdraw ? NULL : attr, pnt,
5755
1
            psize, addpath_id)) {
5756
1
        flog_err(
5757
1
          EC_BGP_PKT_PROCESS,
5758
1
          "%u:%s - Error in processing EVPN type-4 NLRI size %d",
5759
1
          peer->bgp->vrf_id, peer->host, psize);
5760
1
        return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE;
5761
1
      }
5762
0
      break;
5763
5764
2
    case BGP_EVPN_AD_ROUTE:
5765
2
      if (bgp_evpn_type1_route_process(peer, afi, safi,
5766
2
            withdraw ? NULL : attr, pnt,
5767
2
            psize, addpath_id)) {
5768
2
        flog_err(
5769
2
          EC_BGP_PKT_PROCESS,
5770
2
          "%u:%s - Error in processing EVPN type-1 NLRI size %d",
5771
2
          peer->bgp->vrf_id, peer->host, psize);
5772
2
        return BGP_NLRI_PARSE_ERROR_EVPN_TYPE1_SIZE;
5773
2
      }
5774
0
      break;
5775
5776
84
    case BGP_EVPN_IP_PREFIX_ROUTE:
5777
84
      if (process_type5_route(peer, afi, safi,
5778
84
            withdraw ? NULL : attr, pnt,
5779
84
            psize, addpath_id)) {
5780
5
        flog_err(
5781
5
          EC_BGP_PKT_PROCESS,
5782
5
          "%u:%s - Error in processing EVPN type-5 NLRI size %d",
5783
5
          peer->bgp->vrf_id, peer->host, psize);
5784
5
        return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE;
5785
5
      }
5786
79
      break;
5787
5788
95
    default:
5789
95
      break;
5790
194
    }
5791
194
  }
5792
5793
  /* Packet length consistency check. */
5794
0
  if (pnt != lim)
5795
0
    return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH;
5796
5797
0
  return BGP_NLRI_PARSE_OK;
5798
0
}
5799
5800
/*
5801
 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5802
 * The mapping will be used during route processing.
5803
 * bgp_vrf: specific bgp vrf instance on which RT is configured
5804
 */
5805
void bgp_evpn_map_vrf_to_its_rts(struct bgp *bgp_vrf)
5806
0
{
5807
0
  struct listnode *node, *nnode;
5808
0
  struct vrf_route_target *l3rt;
5809
5810
0
  for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, l3rt))
5811
0
    map_vrf_to_rt(bgp_vrf, l3rt);
5812
0
}
5813
5814
/*
5815
 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5816
 */
5817
void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *bgp_vrf)
5818
0
{
5819
0
  struct listnode *node, *nnode;
5820
0
  struct vrf_route_target *l3rt;
5821
5822
0
  for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, l3rt))
5823
0
    unmap_vrf_from_rt(bgp_vrf, l3rt);
5824
0
}
5825
5826
/*
5827
 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5828
 * The mapping will be used during route processing.
5829
 */
5830
void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
5831
0
{
5832
0
  uint32_t i;
5833
0
  struct ecommunity_val *eval;
5834
0
  struct listnode *node, *nnode;
5835
0
  struct ecommunity *ecom;
5836
5837
0
  for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
5838
0
    for (i = 0; i < ecom->size; i++) {
5839
0
      eval = (struct ecommunity_val *)(ecom->val
5840
0
               + (i
5841
0
                  * ECOMMUNITY_SIZE));
5842
0
      map_vni_to_rt(bgp, vpn, eval);
5843
0
    }
5844
0
  }
5845
0
}
5846
5847
/*
5848
 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5849
 */
5850
void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
5851
0
{
5852
0
  uint32_t i;
5853
0
  struct ecommunity_val *eval;
5854
0
  struct listnode *node, *nnode;
5855
0
  struct ecommunity *ecom;
5856
5857
0
  for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
5858
0
    for (i = 0; i < ecom->size; i++) {
5859
0
      struct irt_node *irt;
5860
0
      struct ecommunity_val eval_tmp;
5861
5862
0
      eval = (struct ecommunity_val *)(ecom->val
5863
0
               + (i
5864
0
                  * ECOMMUNITY_SIZE));
5865
      /* If using "automatic" RT, we only care about the
5866
       * local-admin sub-field.
5867
       * This is to facilitate using VNI as the RT for EBGP
5868
       * peering too.
5869
       */
5870
0
      memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
5871
0
      if (!is_import_rt_configured(vpn))
5872
0
        mask_ecom_global_admin(&eval_tmp, eval);
5873
5874
0
      irt = lookup_import_rt(bgp, &eval_tmp);
5875
0
      if (irt)
5876
0
        unmap_vni_from_rt(bgp, vpn, irt);
5877
0
    }
5878
0
  }
5879
0
}
5880
5881
/*
5882
 * Derive Import RT automatically for VNI and map VNI to RT.
5883
 * The mapping will be used during route processing.
5884
 */
5885
void bgp_evpn_derive_auto_rt_import(struct bgp *bgp, struct bgpevpn *vpn)
5886
0
{
5887
0
  form_auto_rt(bgp, vpn->vni, vpn->import_rtl, false);
5888
0
  UNSET_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD);
5889
5890
  /* Map RT to VNI */
5891
0
  bgp_evpn_map_vni_to_its_rts(bgp, vpn);
5892
0
}
5893
5894
/*
5895
 * Derive Export RT automatically for VNI.
5896
 */
5897
void bgp_evpn_derive_auto_rt_export(struct bgp *bgp, struct bgpevpn *vpn)
5898
0
{
5899
0
  form_auto_rt(bgp, vpn->vni, vpn->export_rtl, false);
5900
0
  UNSET_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD);
5901
0
}
5902
5903
/*
5904
 * Derive RD automatically for VNI using passed information - it
5905
 * is of the form RouterId:unique-id-for-vni.
5906
 */
5907
void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp)
5908
0
{
5909
0
  if (is_vrf_rd_configured(bgp))
5910
0
    return;
5911
5912
0
  form_auto_rd(bgp->router_id, bgp->vrf_rd_id, &bgp->vrf_prd);
5913
0
}
5914
5915
/*
5916
 * Derive RD automatically for VNI using passed information - it
5917
 * is of the form RouterId:unique-id-for-vni.
5918
 */
5919
void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn)
5920
0
{
5921
0
  char buf[BGP_EVPN_PREFIX_RD_LEN];
5922
5923
0
  vpn->prd.family = AF_UNSPEC;
5924
0
  vpn->prd.prefixlen = 64;
5925
0
  snprintfrr(buf, sizeof(buf), "%pI4:%hu", &bgp->router_id, vpn->rd_id);
5926
0
  (void)str2prefix_rd(buf, &vpn->prd);
5927
0
  if (vpn->prd_pretty)
5928
0
    XFREE(MTYPE_BGP, vpn->prd_pretty);
5929
0
  UNSET_FLAG(vpn->flags, VNI_FLAG_RD_CFGD);
5930
0
}
5931
5932
/*
5933
 * Lookup L3-VNI
5934
 */
5935
bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni)
5936
0
{
5937
0
  struct list *inst = bm->bgp;
5938
0
  struct listnode *node;
5939
0
  struct bgp *bgp_vrf;
5940
5941
0
  for (ALL_LIST_ELEMENTS_RO(inst, node, bgp_vrf)) {
5942
0
    if (bgp_vrf->l3vni == vni)
5943
0
      return true;
5944
0
  }
5945
5946
0
  return false;
5947
0
}
5948
5949
/*
5950
 * Lookup VNI.
5951
 */
5952
struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni)
5953
0
{
5954
0
  struct bgpevpn *vpn;
5955
0
  struct bgpevpn tmp;
5956
5957
0
  memset(&tmp, 0, sizeof(tmp));
5958
0
  tmp.vni = vni;
5959
0
  vpn = hash_lookup(bgp->vnihash, &tmp);
5960
0
  return vpn;
5961
0
}
5962
5963
/*
5964
 * Create a new vpn - invoked upon configuration or zebra notification.
5965
 */
5966
struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
5967
    struct in_addr originator_ip,
5968
    vrf_id_t tenant_vrf_id,
5969
    struct in_addr mcast_grp,
5970
    ifindex_t svi_ifindex)
5971
0
{
5972
0
  struct bgpevpn *vpn;
5973
5974
0
  vpn = XCALLOC(MTYPE_BGP_EVPN, sizeof(struct bgpevpn));
5975
5976
  /* Set values - RD and RT set to defaults. */
5977
0
  vpn->vni = vni;
5978
0
  vpn->originator_ip = originator_ip;
5979
0
  vpn->tenant_vrf_id = tenant_vrf_id;
5980
0
  vpn->mcast_grp = mcast_grp;
5981
0
  vpn->svi_ifindex = svi_ifindex;
5982
5983
  /* Initialize route-target import and export lists */
5984
0
  vpn->import_rtl = list_new();
5985
0
  vpn->import_rtl->cmp =
5986
0
    (int (*)(void *, void *))bgp_evpn_route_target_cmp;
5987
0
  vpn->import_rtl->del = bgp_evpn_xxport_delete_ecomm;
5988
0
  vpn->export_rtl = list_new();
5989
0
  vpn->export_rtl->cmp =
5990
0
    (int (*)(void *, void *))bgp_evpn_route_target_cmp;
5991
0
  vpn->export_rtl->del = bgp_evpn_xxport_delete_ecomm;
5992
0
  bf_assign_index(bm->rd_idspace, vpn->rd_id);
5993
0
  derive_rd_rt_for_vni(bgp, vpn);
5994
5995
  /* Initialize EVPN route tables. */
5996
0
  vpn->ip_table = bgp_table_init(bgp, AFI_L2VPN, SAFI_EVPN);
5997
0
  vpn->mac_table = bgp_table_init(bgp, AFI_L2VPN, SAFI_EVPN);
5998
5999
  /* Add to hash */
6000
0
  (void)hash_get(bgp->vnihash, vpn, hash_alloc_intern);
6001
6002
0
  bgp_evpn_remote_ip_hash_init(vpn);
6003
0
  bgp_evpn_link_to_vni_svi_hash(bgp, vpn);
6004
6005
  /* add to l2vni list on corresponding vrf */
6006
0
  bgpevpn_link_to_l3vni(vpn);
6007
6008
0
  bgp_evpn_vni_es_init(vpn);
6009
6010
0
  QOBJ_REG(vpn, bgpevpn);
6011
0
  return vpn;
6012
0
}
6013
6014
/*
6015
 * Free a given VPN - called in multiple scenarios such as zebra
6016
 * notification, configuration being deleted, advertise-all-vni disabled etc.
6017
 * This just frees appropriate memory, caller should have taken other
6018
 * needed actions.
6019
 */
6020
void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn)
6021
0
{
6022
0
  bgp_evpn_remote_ip_hash_destroy(vpn);
6023
0
  bgp_evpn_vni_es_cleanup(vpn);
6024
0
  bgpevpn_unlink_from_l3vni(vpn);
6025
0
  bgp_table_unlock(vpn->ip_table);
6026
0
  bgp_table_unlock(vpn->mac_table);
6027
0
  bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
6028
0
  list_delete(&vpn->import_rtl);
6029
0
  list_delete(&vpn->export_rtl);
6030
0
  bf_release_index(bm->rd_idspace, vpn->rd_id);
6031
0
  hash_release(bgp->vni_svi_hash, vpn);
6032
0
  hash_release(bgp->vnihash, vpn);
6033
0
  if (vpn->prd_pretty)
6034
0
    XFREE(MTYPE_BGP, vpn->prd_pretty);
6035
0
  QOBJ_UNREG(vpn);
6036
0
  XFREE(MTYPE_BGP_EVPN, vpn);
6037
0
}
6038
6039
static void hash_evpn_free(struct bgpevpn *vpn)
6040
0
{
6041
0
  XFREE(MTYPE_BGP_EVPN, vpn);
6042
0
}
6043
6044
/*
6045
 * Import evpn route from global table to VNI/VRF/ESI.
6046
 */
6047
int bgp_evpn_import_route(struct bgp *bgp, afi_t afi, safi_t safi,
6048
        const struct prefix *p, struct bgp_path_info *pi)
6049
0
{
6050
0
  return install_uninstall_evpn_route(bgp, afi, safi, p, pi, 1);
6051
0
}
6052
6053
/*
6054
 * Unimport evpn route from VNI/VRF/ESI.
6055
 */
6056
int bgp_evpn_unimport_route(struct bgp *bgp, afi_t afi, safi_t safi,
6057
          const struct prefix *p, struct bgp_path_info *pi)
6058
0
{
6059
0
  return install_uninstall_evpn_route(bgp, afi, safi, p, pi, 0);
6060
0
}
6061
6062
/* filter routes which have martian next hops */
6063
int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp *bgp)
6064
0
{
6065
0
  afi_t afi;
6066
0
  safi_t safi;
6067
0
  struct bgp_dest *rd_dest, *dest;
6068
0
  struct bgp_table *table;
6069
0
  struct bgp_path_info *pi;
6070
6071
0
  afi = AFI_L2VPN;
6072
0
  safi = SAFI_EVPN;
6073
6074
  /* Walk entire global routing table and evaluate routes which could be
6075
   * imported into this VPN. Note that we cannot just look at the routes
6076
   * for the VNI's RD -
6077
   * remote routes applicable for this VNI could have any RD.
6078
   */
6079
  /* EVPN routes are a 2-level table. */
6080
0
  for (rd_dest = bgp_table_top(bgp->rib[afi][safi]); rd_dest;
6081
0
       rd_dest = bgp_route_next(rd_dest)) {
6082
0
    table = bgp_dest_get_bgp_table_info(rd_dest);
6083
0
    if (!table)
6084
0
      continue;
6085
6086
0
    for (dest = bgp_table_top(table); dest;
6087
0
         dest = bgp_route_next(dest)) {
6088
6089
0
      for (pi = bgp_dest_get_bgp_path_info(dest); pi;
6090
0
           pi = pi->next) {
6091
6092
        /* Consider "valid" remote routes applicable for
6093
         * this VNI. */
6094
0
        if (!(pi->type == ZEBRA_ROUTE_BGP
6095
0
              && pi->sub_type == BGP_ROUTE_NORMAL))
6096
0
          continue;
6097
0
        if (bgp_nexthop_self(bgp, afi, pi->type,
6098
0
                 pi->sub_type, pi->attr,
6099
0
                 dest)) {
6100
0
          const struct prefix *p =
6101
0
            bgp_dest_get_prefix(dest);
6102
6103
0
          if (bgp_debug_update(pi->peer, p, NULL,
6104
0
                   1)) {
6105
0
            char attr_str[BUFSIZ] = {0};
6106
6107
0
            bgp_dump_attr(pi->attr,
6108
0
                    attr_str,
6109
0
                    sizeof(attr_str));
6110
6111
0
            zlog_debug(
6112
0
              "%u: prefix %pBD with attr %s - DENIED due to martian or self nexthop",
6113
0
              bgp->vrf_id, dest,
6114
0
              attr_str);
6115
0
          }
6116
0
          bgp_evpn_unimport_route(bgp, afi, safi,
6117
0
                p, pi);
6118
6119
0
          bgp_rib_remove(dest, pi, pi->peer, afi,
6120
0
                   safi);
6121
0
        }
6122
0
      }
6123
0
    }
6124
0
  }
6125
6126
0
  return 0;
6127
0
}
6128
6129
/*
6130
 * Handle del of a local MACIP.
6131
 */
6132
int bgp_evpn_local_macip_del(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
6133
           struct ipaddr *ip, int state)
6134
0
{
6135
0
  struct bgpevpn *vpn;
6136
0
  struct prefix_evpn p;
6137
0
  struct bgp_dest *dest;
6138
6139
  /* Lookup VNI hash - should exist. */
6140
0
  vpn = bgp_evpn_lookup_vni(bgp, vni);
6141
0
  if (!vpn || !is_vni_live(vpn)) {
6142
0
    flog_warn(EC_BGP_EVPN_VPN_VNI,
6143
0
        "%u: VNI hash entry for VNI %u %s at MACIP DEL",
6144
0
        bgp->vrf_id, vni, vpn ? "not live" : "not found");
6145
0
    return -1;
6146
0
  }
6147
6148
0
  build_evpn_type2_prefix(&p, mac, ip);
6149
0
  if (state == ZEBRA_NEIGH_ACTIVE) {
6150
    /* Remove EVPN type-2 route and schedule for processing. */
6151
0
    delete_evpn_route(bgp, vpn, &p);
6152
0
  } else {
6153
    /* Re-instate the current remote best path if any */
6154
0
    dest = bgp_evpn_vni_node_lookup(vpn, &p, NULL);
6155
0
    if (dest) {
6156
0
      evpn_zebra_reinstall_best_route(bgp, vpn, dest);
6157
0
      bgp_dest_unlock_node(dest);
6158
0
    }
6159
0
  }
6160
6161
0
  return 0;
6162
0
}
6163
6164
/*
6165
 * Handle add of a local MACIP.
6166
 */
6167
int bgp_evpn_local_macip_add(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
6168
    struct ipaddr *ip, uint8_t flags, uint32_t seq, esi_t *esi)
6169
0
{
6170
0
  struct bgpevpn *vpn;
6171
0
  struct prefix_evpn p;
6172
6173
  /* Lookup VNI hash - should exist. */
6174
0
  vpn = bgp_evpn_lookup_vni(bgp, vni);
6175
0
  if (!vpn || !is_vni_live(vpn)) {
6176
0
    flog_warn(EC_BGP_EVPN_VPN_VNI,
6177
0
        "%u: VNI hash entry for VNI %u %s at MACIP ADD",
6178
0
        bgp->vrf_id, vni, vpn ? "not live" : "not found");
6179
0
    return -1;
6180
0
  }
6181
6182
  /* Create EVPN type-2 route and schedule for processing. */
6183
0
  build_evpn_type2_prefix(&p, mac, ip);
6184
0
  if (update_evpn_route(bgp, vpn, &p, flags, seq, esi)) {
6185
0
    flog_err(
6186
0
      EC_BGP_EVPN_ROUTE_CREATE,
6187
0
      "%u:Failed to create Type-2 route, VNI %u %s MAC %pEA IP %pIA (flags: 0x%x)",
6188
0
      bgp->vrf_id, vpn->vni,
6189
0
      CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY)
6190
0
        ? "sticky gateway"
6191
0
        : "",
6192
0
      mac, ip, flags);
6193
0
    return -1;
6194
0
  }
6195
6196
0
  return 0;
6197
0
}
6198
6199
static void link_l2vni_hash_to_l3vni(struct hash_bucket *bucket,
6200
             struct bgp *bgp_vrf)
6201
0
{
6202
0
  struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
6203
0
  struct bgp *bgp_evpn = NULL;
6204
6205
0
  bgp_evpn = bgp_get_evpn();
6206
0
  assert(bgp_evpn);
6207
6208
0
  if (vpn->tenant_vrf_id == bgp_vrf->vrf_id)
6209
0
    bgpevpn_link_to_l3vni(vpn);
6210
0
}
6211
6212
int bgp_evpn_local_l3vni_add(vni_t l3vni, vrf_id_t vrf_id,
6213
           struct ethaddr *svi_rmac,
6214
           struct ethaddr *vrr_rmac,
6215
           struct in_addr originator_ip, int filter,
6216
           ifindex_t svi_ifindex,
6217
           bool is_anycast_mac)
6218
0
{
6219
0
  struct bgp *bgp_vrf = NULL; /* bgp VRF instance */
6220
0
  struct bgp *bgp_evpn = NULL; /* EVPN bgp instance */
6221
0
  struct listnode *node = NULL;
6222
0
  struct bgpevpn *vpn = NULL;
6223
0
  as_t as = 0;
6224
6225
  /* get the EVPN instance - required to get the AS number for VRF
6226
   * auto-creatio
6227
   */
6228
0
  bgp_evpn = bgp_get_evpn();
6229
0
  if (!bgp_evpn) {
6230
0
    flog_err(
6231
0
      EC_BGP_NO_DFLT,
6232
0
      "Cannot process L3VNI  %u ADD - EVPN BGP instance not yet created",
6233
0
      l3vni);
6234
0
    return -1;
6235
0
  }
6236
6237
0
  if (CHECK_FLAG(bgp_evpn->flags, BGP_FLAG_DELETE_IN_PROGRESS)) {
6238
0
    flog_err(EC_BGP_NO_DFLT,
6239
0
        "Cannot process L3VNI %u ADD - EVPN BGP instance is shutting down",
6240
0
        l3vni);
6241
0
    return -1;
6242
0
  }
6243
6244
0
  as = bgp_evpn->as;
6245
6246
  /* if the BGP vrf instance doesn't exist - create one */
6247
0
  bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
6248
0
  if (!bgp_vrf) {
6249
6250
0
    int ret = 0;
6251
6252
0
    ret = bgp_get_vty(&bgp_vrf, &as, vrf_id_to_name(vrf_id),
6253
0
          vrf_id == VRF_DEFAULT
6254
0
            ? BGP_INSTANCE_TYPE_DEFAULT
6255
0
            : BGP_INSTANCE_TYPE_VRF,
6256
0
          NULL, ASNOTATION_UNDEFINED);
6257
0
    switch (ret) {
6258
0
    case BGP_ERR_AS_MISMATCH:
6259
0
      flog_err(EC_BGP_EVPN_AS_MISMATCH,
6260
0
         "BGP instance is already running; AS is %s",
6261
0
         bgp_vrf->as_pretty);
6262
0
      return -1;
6263
0
    case BGP_ERR_INSTANCE_MISMATCH:
6264
0
      flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH,
6265
0
         "BGP instance type mismatch");
6266
0
      return -1;
6267
0
    }
6268
6269
    /* mark as auto created */
6270
0
    SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO);
6271
0
  }
6272
6273
  /* associate the vrf with l3vni and related parameters */
6274
0
  bgp_vrf->l3vni = l3vni;
6275
0
  bgp_vrf->originator_ip = originator_ip;
6276
0
  bgp_vrf->l3vni_svi_ifindex = svi_ifindex;
6277
0
  bgp_vrf->evpn_info->is_anycast_mac = is_anycast_mac;
6278
6279
  /* copy anycast MAC from VRR MAC */
6280
0
  memcpy(&bgp_vrf->rmac, vrr_rmac, ETH_ALEN);
6281
  /* copy sys RMAC from SVI MAC */
6282
0
  memcpy(&bgp_vrf->evpn_info->pip_rmac_zebra, svi_rmac, ETH_ALEN);
6283
  /* PIP user configured mac is not present use svi mac as sys mac */
6284
0
  if (is_zero_mac(&bgp_vrf->evpn_info->pip_rmac_static))
6285
0
    memcpy(&bgp_vrf->evpn_info->pip_rmac, svi_rmac, ETH_ALEN);
6286
6287
0
  if (bgp_debug_zebra(NULL))
6288
0
    zlog_debug(
6289
0
      "VRF %s vni %u pip %s RMAC %pEA sys RMAC %pEA static RMAC %pEA is_anycast_mac %s",
6290
0
      vrf_id_to_name(bgp_vrf->vrf_id), bgp_vrf->l3vni,
6291
0
      bgp_vrf->evpn_info->advertise_pip ? "enable"
6292
0
                : "disable",
6293
0
      &bgp_vrf->rmac, &bgp_vrf->evpn_info->pip_rmac,
6294
0
      &bgp_vrf->evpn_info->pip_rmac_static,
6295
0
      is_anycast_mac ? "Enable" : "Disable");
6296
6297
  /* set the right filter - are we using l3vni only for prefix routes? */
6298
0
  if (filter) {
6299
0
    SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
6300
6301
    /*
6302
     * VNI_FLAG_USE_TWO_LABELS flag for linked L2VNIs should not be
6303
     * set before linking vrf to L3VNI. Thus, no need to clear
6304
     * that explicitly.
6305
     */
6306
0
  } else {
6307
0
    UNSET_FLAG(bgp_vrf->vrf_flags,
6308
0
         BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
6309
6310
0
    for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) {
6311
0
      if (!CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)) {
6312
6313
        /*
6314
         * If we are flapping VNI_FLAG_USE_TWO_LABELS
6315
         * flag, update all MACIP routes in this VNI
6316
         */
6317
0
        SET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
6318
0
        update_all_type2_routes(bgp_evpn, vpn);
6319
0
      }
6320
0
    }
6321
0
  }
6322
6323
  /* Map auto derive or configured RTs */
6324
0
  if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD) ||
6325
0
      CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_AUTO_RT_CFGD))
6326
0
    evpn_auto_rt_import_add_for_vrf(bgp_vrf);
6327
0
  else
6328
0
    bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
6329
6330
0
  if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD) ||
6331
0
      CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_AUTO_RT_CFGD))
6332
0
    evpn_auto_rt_export_add_for_vrf(bgp_vrf);
6333
6334
  /* auto derive RD */
6335
0
  bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
6336
6337
  /* link all corresponding l2vnis */
6338
0
  hash_iterate(bgp_evpn->vnihash,
6339
0
         (void (*)(struct hash_bucket *,
6340
0
             void *))link_l2vni_hash_to_l3vni,
6341
0
         bgp_vrf);
6342
6343
  /* Only update all corresponding type-2 routes if we are advertising two
6344
   * labels along with type-2 routes
6345
   */
6346
0
  if (!filter)
6347
0
    for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
6348
0
      update_routes_for_vni(bgp_evpn, vpn);
6349
6350
  /* advertise type-5 routes if needed */
6351
0
  update_advertise_vrf_routes(bgp_vrf);
6352
6353
  /* install all remote routes belonging to this l3vni into correspondng
6354
   * vrf */
6355
0
  install_routes_for_vrf(bgp_vrf);
6356
6357
0
  return 0;
6358
0
}
6359
6360
int bgp_evpn_local_l3vni_del(vni_t l3vni, vrf_id_t vrf_id)
6361
0
{
6362
0
  struct bgp *bgp_vrf = NULL; /* bgp vrf instance */
6363
0
  struct bgp *bgp_evpn = NULL; /* EVPN bgp instance */
6364
0
  struct listnode *node = NULL;
6365
0
  struct listnode *next = NULL;
6366
0
  struct bgpevpn *vpn = NULL;
6367
6368
0
  bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
6369
0
  if (!bgp_vrf) {
6370
0
    flog_err(
6371
0
      EC_BGP_NO_DFLT,
6372
0
      "Cannot process L3VNI %u Del - Could not find BGP instance",
6373
0
      l3vni);
6374
0
    return -1;
6375
0
  }
6376
6377
0
  bgp_evpn = bgp_get_evpn();
6378
0
  if (!bgp_evpn) {
6379
0
    flog_err(
6380
0
      EC_BGP_NO_DFLT,
6381
0
      "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
6382
0
      l3vni);
6383
0
    return -1;
6384
0
  }
6385
6386
0
  if (CHECK_FLAG(bgp_evpn->flags, BGP_FLAG_DELETE_IN_PROGRESS)) {
6387
0
    flog_err(EC_BGP_NO_DFLT,
6388
0
        "Cannot process L3VNI %u ADD - EVPN BGP instance is shutting down",
6389
0
        l3vni);
6390
0
    return -1;
6391
0
  }
6392
6393
  /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
6394
   * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
6395
   * routes. This will uninstalling the routes from zebra and decremnt the
6396
   * bgp info count.
6397
   */
6398
0
  uninstall_routes_for_vrf(bgp_vrf);
6399
6400
  /* delete/withdraw all type-5 routes */
6401
0
  delete_withdraw_vrf_routes(bgp_vrf);
6402
6403
  /* remove the l3vni from vrf instance */
6404
0
  bgp_vrf->l3vni = 0;
6405
6406
  /* remove the Rmac from the BGP vrf */
6407
0
  memset(&bgp_vrf->rmac, 0, sizeof(struct ethaddr));
6408
0
  memset(&bgp_vrf->evpn_info->pip_rmac_zebra, 0, ETH_ALEN);
6409
0
  if (is_zero_mac(&bgp_vrf->evpn_info->pip_rmac_static) &&
6410
0
      !is_zero_mac(&bgp_vrf->evpn_info->pip_rmac))
6411
0
    memset(&bgp_vrf->evpn_info->pip_rmac, 0, ETH_ALEN);
6412
6413
  /* remove default import RT or Unmap non-default import RT */
6414
0
  if (!list_isempty(bgp_vrf->vrf_import_rtl)) {
6415
0
    bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
6416
0
    if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
6417
0
      list_delete_all_node(bgp_vrf->vrf_import_rtl);
6418
0
  }
6419
6420
  /* remove default export RT */
6421
0
  if (!list_isempty(bgp_vrf->vrf_export_rtl) &&
6422
0
      !CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD)) {
6423
0
    list_delete_all_node(bgp_vrf->vrf_export_rtl);
6424
0
  }
6425
6426
  /* update all corresponding local mac-ip routes */
6427
0
  if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY)) {
6428
0
    for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) {
6429
0
      UNSET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
6430
0
      update_routes_for_vni(bgp_evpn, vpn);
6431
0
    }
6432
0
  }
6433
6434
  /* If any L2VNIs point to this instance, unlink them. */
6435
0
  for (ALL_LIST_ELEMENTS(bgp_vrf->l2vnis, node, next, vpn))
6436
0
    bgpevpn_unlink_from_l3vni(vpn);
6437
6438
0
  UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
6439
6440
  /* Delete the instance if it was autocreated */
6441
0
  if (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO))
6442
0
    bgp_delete(bgp_vrf);
6443
6444
0
  return 0;
6445
0
}
6446
6447
/*
6448
 * Handle del of a local VNI.
6449
 */
6450
int bgp_evpn_local_vni_del(struct bgp *bgp, vni_t vni)
6451
0
{
6452
0
  struct bgpevpn *vpn;
6453
6454
  /* Locate VNI hash */
6455
0
  vpn = bgp_evpn_lookup_vni(bgp, vni);
6456
0
  if (!vpn)
6457
0
    return 0;
6458
6459
  /* Remove all local EVPN routes and schedule for processing (to
6460
   * withdraw from peers).
6461
   */
6462
0
  delete_routes_for_vni(bgp, vpn);
6463
6464
0
  bgp_evpn_unlink_from_vni_svi_hash(bgp, vpn);
6465
6466
0
  vpn->svi_ifindex = 0;
6467
  /*
6468
   * tunnel is no longer active, del tunnel ip address from tip_hash
6469
   */
6470
0
  bgp_tip_del(bgp, &vpn->originator_ip);
6471
6472
  /* Clear "live" flag and see if hash needs to be freed. */
6473
0
  UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
6474
0
  if (!is_vni_configured(vpn))
6475
0
    bgp_evpn_free(bgp, vpn);
6476
6477
0
  return 0;
6478
0
}
6479
6480
/*
6481
 * Handle add (or update) of a local VNI. The VNI changes we care
6482
 * about are for the local-tunnel-ip and the (tenant) VRF.
6483
 */
6484
int bgp_evpn_local_vni_add(struct bgp *bgp, vni_t vni,
6485
         struct in_addr originator_ip,
6486
         vrf_id_t tenant_vrf_id,
6487
         struct in_addr mcast_grp,
6488
         ifindex_t svi_ifindex)
6489
0
{
6490
0
  struct bgpevpn *vpn;
6491
0
  struct prefix_evpn p;
6492
6493
  /* Lookup VNI. If present and no change, exit. */
6494
0
  vpn = bgp_evpn_lookup_vni(bgp, vni);
6495
0
  if (vpn) {
6496
6497
0
    if (is_vni_live(vpn)
6498
0
        && IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip)
6499
0
        && IPV4_ADDR_SAME(&vpn->mcast_grp, &mcast_grp)
6500
0
        && vpn->tenant_vrf_id == tenant_vrf_id
6501
0
        && vpn->svi_ifindex == svi_ifindex)
6502
      /* Probably some other param has changed that we don't
6503
       * care about. */
6504
0
      return 0;
6505
6506
0
    bgp_evpn_mcast_grp_change(bgp, vpn, mcast_grp);
6507
6508
0
    if (vpn->svi_ifindex != svi_ifindex) {
6509
6510
      /*
6511
       * Unresolve all the gateway IP nexthops for this VNI
6512
       * for old SVI
6513
       */
6514
0
      bgp_evpn_remote_ip_hash_iterate(
6515
0
        vpn,
6516
0
        (void (*)(struct hash_bucket *, void *))
6517
0
          bgp_evpn_remote_ip_hash_unlink_nexthop,
6518
0
        vpn);
6519
0
      bgp_evpn_unlink_from_vni_svi_hash(bgp, vpn);
6520
0
      vpn->svi_ifindex = svi_ifindex;
6521
0
      bgp_evpn_link_to_vni_svi_hash(bgp, vpn);
6522
6523
      /*
6524
       * Resolve all the gateway IP nexthops for this VNI
6525
       * for new SVI
6526
       */
6527
0
      bgp_evpn_remote_ip_hash_iterate(
6528
0
        vpn,
6529
0
        (void (*)(struct hash_bucket *, void *))
6530
0
          bgp_evpn_remote_ip_hash_link_nexthop,
6531
0
        vpn);
6532
0
    }
6533
6534
    /* Update tenant_vrf_id if it has changed. */
6535
0
    if (vpn->tenant_vrf_id != tenant_vrf_id) {
6536
6537
      /*
6538
       * Unresolve all the gateway IP nexthops for this VNI
6539
       * in old tenant vrf
6540
       */
6541
0
      bgp_evpn_remote_ip_hash_iterate(
6542
0
        vpn,
6543
0
        (void (*)(struct hash_bucket *, void *))
6544
0
          bgp_evpn_remote_ip_hash_unlink_nexthop,
6545
0
        vpn);
6546
0
      bgpevpn_unlink_from_l3vni(vpn);
6547
0
      vpn->tenant_vrf_id = tenant_vrf_id;
6548
0
      bgpevpn_link_to_l3vni(vpn);
6549
6550
      /*
6551
       * Resolve all the gateway IP nexthops for this VNI
6552
       * in new tenant vrf
6553
       */
6554
0
      bgp_evpn_remote_ip_hash_iterate(
6555
0
        vpn,
6556
0
        (void (*)(struct hash_bucket *, void *))
6557
0
          bgp_evpn_remote_ip_hash_link_nexthop,
6558
0
        vpn);
6559
0
    }
6560
6561
    /* If tunnel endpoint IP has changed, update (and delete prior
6562
     * type-3 route, if needed.)
6563
     */
6564
0
    handle_tunnel_ip_change(bgp, vpn, originator_ip);
6565
6566
    /* Update all routes with new endpoint IP and/or export RT
6567
     * for VRFs
6568
     */
6569
0
    if (is_vni_live(vpn))
6570
0
      update_routes_for_vni(bgp, vpn);
6571
0
  } else {
6572
    /* Create or update as appropriate. */
6573
0
    vpn = bgp_evpn_new(bgp, vni, originator_ip, tenant_vrf_id,
6574
0
           mcast_grp, svi_ifindex);
6575
0
  }
6576
6577
  /* if the VNI is live already, there is nothing more to do */
6578
0
  if (is_vni_live(vpn))
6579
0
    return 0;
6580
6581
  /* Mark as "live" */
6582
0
  SET_FLAG(vpn->flags, VNI_FLAG_LIVE);
6583
6584
  /* tunnel is now active, add tunnel-ip to db */
6585
0
  if (bgp_tip_add(bgp, &originator_ip))
6586
    /* The originator_ip was not already present in the
6587
     * bgp martian next-hop table as a tunnel-ip, so we
6588
     * need to go back and filter routes matching the new
6589
     * martian next-hop.
6590
     */
6591
0
    bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
6592
6593
  /*
6594
   * Create EVPN type-3 route and schedule for processing.
6595
   *
6596
   * RT-3 only if doing head-end replication
6597
   */
6598
0
  if (bgp_evpn_vni_flood_mode_get(bgp, vpn)
6599
0
      == VXLAN_FLOOD_HEAD_END_REPL) {
6600
0
    build_evpn_type3_prefix(&p, vpn->originator_ip);
6601
0
    if (update_evpn_route(bgp, vpn, &p, 0, 0, NULL)) {
6602
0
      flog_err(EC_BGP_EVPN_ROUTE_CREATE,
6603
0
         "%u: Type3 route creation failure for VNI %u",
6604
0
         bgp->vrf_id, vni);
6605
0
      return -1;
6606
0
    }
6607
0
  }
6608
6609
  /* If we have learnt and retained remote routes (VTEPs, MACs) for this
6610
   * VNI,
6611
   * install them.
6612
   */
6613
0
  install_routes_for_vni(bgp, vpn);
6614
6615
  /* If we are advertising gateway mac-ip
6616
     It needs to be conveyed again to zebra */
6617
0
  bgp_zebra_advertise_gw_macip(bgp, vpn->advertise_gw_macip, vpn->vni);
6618
6619
  /* advertise svi mac-ip knob to zebra */
6620
0
  bgp_zebra_advertise_svi_macip(bgp, vpn->advertise_svi_macip, vpn->vni);
6621
6622
0
  return 0;
6623
0
}
6624
6625
/*
6626
 * Handle change in setting for BUM handling. The supported values
6627
 * are head-end replication and dropping all BUM packets. Any change
6628
 * should be registered with zebra. Also, if doing head-end replication,
6629
 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
6630
 * to be dropped, the RT-3s must be withdrawn.
6631
 */
6632
void bgp_evpn_flood_control_change(struct bgp *bgp)
6633
0
{
6634
0
  zlog_info("L2VPN EVPN BUM handling is %s",
6635
0
      bgp->vxlan_flood_ctrl == VXLAN_FLOOD_HEAD_END_REPL ?
6636
0
      "Flooding" : "Flooding Disabled");
6637
6638
0
  bgp_zebra_vxlan_flood_control(bgp, bgp->vxlan_flood_ctrl);
6639
0
  if (bgp->vxlan_flood_ctrl == VXLAN_FLOOD_HEAD_END_REPL)
6640
0
    hash_iterate(bgp->vnihash, create_advertise_type3, bgp);
6641
0
  else if (bgp->vxlan_flood_ctrl == VXLAN_FLOOD_DISABLED)
6642
0
    hash_iterate(bgp->vnihash, delete_withdraw_type3, bgp);
6643
0
}
6644
6645
/*
6646
 * Cleanup EVPN information on disable - Need to delete and withdraw
6647
 * EVPN routes from peers.
6648
 */
6649
void bgp_evpn_cleanup_on_disable(struct bgp *bgp)
6650
0
{
6651
0
  hash_iterate(bgp->vnihash, (void (*)(struct hash_bucket *,
6652
0
               void *))cleanup_vni_on_disable,
6653
0
         bgp);
6654
0
}
6655
6656
/*
6657
 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
6658
 * BGP instance (default) is being freed.
6659
 */
6660
void bgp_evpn_cleanup(struct bgp *bgp)
6661
0
{
6662
0
  hash_iterate(bgp->vnihash,
6663
0
         (void (*)(struct hash_bucket *, void *))free_vni_entry,
6664
0
         bgp);
6665
6666
0
  hash_clean_and_free(&bgp->import_rt_hash,
6667
0
          (void (*)(void *))hash_import_rt_free);
6668
6669
0
  hash_clean_and_free(&bgp->vrf_import_rt_hash,
6670
0
          (void (*)(void *))hash_vrf_import_rt_free);
6671
6672
0
  hash_clean_and_free(&bgp->vni_svi_hash,
6673
0
          (void (*)(void *))hash_evpn_free);
6674
6675
  /*
6676
   * Why is the vnihash freed at the top of this function and
6677
   * then deleted here?
6678
   */
6679
0
  hash_clean_and_free(&bgp->vnihash, NULL);
6680
6681
0
  list_delete(&bgp->vrf_import_rtl);
6682
0
  list_delete(&bgp->vrf_export_rtl);
6683
0
  list_delete(&bgp->l2vnis);
6684
6685
0
  if (bgp->vrf_prd_pretty)
6686
0
    XFREE(MTYPE_BGP, bgp->vrf_prd_pretty);
6687
0
}
6688
6689
/*
6690
 * Initialization for EVPN
6691
 * Create
6692
 *  VNI hash table
6693
 *  hash for RT to VNI
6694
 */
6695
void bgp_evpn_init(struct bgp *bgp)
6696
1
{
6697
1
  bgp->vnihash =
6698
1
    hash_create(vni_hash_key_make, vni_hash_cmp, "BGP VNI Hash");
6699
1
  bgp->vni_svi_hash =
6700
1
    hash_create(vni_svi_hash_key_make, vni_svi_hash_cmp,
6701
1
          "BGP VNI hash based on SVI ifindex");
6702
1
  bgp->import_rt_hash =
6703
1
    hash_create(import_rt_hash_key_make, import_rt_hash_cmp,
6704
1
          "BGP Import RT Hash");
6705
1
  bgp->vrf_import_rt_hash =
6706
1
    hash_create(vrf_import_rt_hash_key_make, vrf_import_rt_hash_cmp,
6707
1
          "BGP VRF Import RT Hash");
6708
1
  bgp->vrf_import_rtl = list_new();
6709
1
  bgp->vrf_import_rtl->cmp =
6710
1
    (int (*)(void *, void *))evpn_vrf_route_target_cmp;
6711
1
  bgp->vrf_import_rtl->del = evpn_vrf_rt_del;
6712
1
  bgp->vrf_export_rtl = list_new();
6713
1
  bgp->vrf_export_rtl->cmp =
6714
1
    (int (*)(void *, void *))evpn_vrf_route_target_cmp;
6715
1
  bgp->vrf_export_rtl->del = evpn_vrf_rt_del;
6716
1
  bgp->l2vnis = list_new();
6717
1
  bgp->l2vnis->cmp = vni_list_cmp;
6718
  /* By default Duplicate Address Dection is enabled.
6719
   * Max-moves (N) 5, detection time (M) 180
6720
   * default action is warning-only
6721
   * freeze action permanently freezes address,
6722
   * and freeze time (auto-recovery) is disabled.
6723
   */
6724
1
  if (bgp->evpn_info) {
6725
1
    bgp->evpn_info->dup_addr_detect = true;
6726
1
    bgp->evpn_info->dad_time = EVPN_DAD_DEFAULT_TIME;
6727
1
    bgp->evpn_info->dad_max_moves = EVPN_DAD_DEFAULT_MAX_MOVES;
6728
1
    bgp->evpn_info->dad_freeze = false;
6729
1
    bgp->evpn_info->dad_freeze_time = 0;
6730
    /* Initialize zebra vxlan */
6731
1
    bgp_zebra_dup_addr_detection(bgp);
6732
    /* Enable PIP feature by default for bgp vrf instance */
6733
1
    if (bgp->inst_type == BGP_INSTANCE_TYPE_VRF) {
6734
0
      struct bgp *bgp_default;
6735
6736
0
      bgp->evpn_info->advertise_pip = true;
6737
0
      bgp_default = bgp_get_default();
6738
0
      if (bgp_default)
6739
0
        bgp->evpn_info->pip_ip = bgp_default->router_id;
6740
0
    }
6741
1
  }
6742
6743
  /* Default BUM handling is to do head-end replication. */
6744
1
  bgp->vxlan_flood_ctrl = VXLAN_FLOOD_HEAD_END_REPL;
6745
6746
1
  bgp_evpn_nh_init(bgp);
6747
1
}
6748
6749
void bgp_evpn_vrf_delete(struct bgp *bgp_vrf)
6750
0
{
6751
0
  bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
6752
0
  bgp_evpn_nh_finish(bgp_vrf);
6753
0
}
6754
6755
/*
6756
 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
6757
 */
6758
int bgp_evpn_get_type5_prefixlen(const struct prefix *pfx)
6759
0
{
6760
0
  struct prefix_evpn *evp = (struct prefix_evpn *)pfx;
6761
6762
0
  if (!pfx || pfx->family != AF_EVPN)
6763
0
    return 0;
6764
6765
0
  if (evp->prefix.route_type != BGP_EVPN_IP_PREFIX_ROUTE)
6766
0
    return 0;
6767
6768
0
  return evp->prefix.prefix_addr.ip_prefix_length;
6769
0
}
6770
6771
/*
6772
 * Should we register nexthop for this EVPN prefix for nexthop tracking?
6773
 */
6774
bool bgp_evpn_is_prefix_nht_supported(const struct prefix *pfx)
6775
0
{
6776
0
  struct prefix_evpn *evp = (struct prefix_evpn *)pfx;
6777
6778
  /*
6779
   * EVPN routes should be marked as valid only if the nexthop is
6780
   * reachable. Only if this happens, the route should be imported
6781
   * (into VNI or VRF routing tables) and/or advertised.
6782
   * Note: This is currently applied for EVPN type-1, type-2,
6783
   * type-3, type-4 and type-5 routes.
6784
   * It may be tweaked later on for other routes, or
6785
   * even removed completely when all routes are handled.
6786
   */
6787
0
  if (pfx && pfx->family == AF_EVPN
6788
0
      && (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
6789
0
    || evp->prefix.route_type == BGP_EVPN_AD_ROUTE
6790
0
    || evp->prefix.route_type == BGP_EVPN_ES_ROUTE
6791
0
    || evp->prefix.route_type == BGP_EVPN_IMET_ROUTE
6792
0
    || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
6793
0
    return true;
6794
6795
0
  return false;
6796
0
}
6797
6798
static void *bgp_evpn_remote_ip_hash_alloc(void *p)
6799
0
{
6800
0
  const struct evpn_remote_ip *key = (const struct evpn_remote_ip *)p;
6801
0
  struct evpn_remote_ip *ip;
6802
6803
0
  ip = XMALLOC(MTYPE_EVPN_REMOTE_IP, sizeof(struct evpn_remote_ip));
6804
0
  *ip = *key;
6805
0
  ip->macip_path_list = list_new();
6806
6807
0
  return ip;
6808
0
}
6809
6810
static unsigned int bgp_evpn_remote_ip_hash_key_make(const void *p)
6811
0
{
6812
0
  const struct evpn_remote_ip *ip = p;
6813
0
  const struct ipaddr *addr = &ip->addr;
6814
6815
0
  if (IS_IPADDR_V4(addr))
6816
0
    return jhash_1word(addr->ipaddr_v4.s_addr, 0);
6817
6818
0
  return jhash2(addr->ipaddr_v6.s6_addr32,
6819
0
          array_size(addr->ipaddr_v6.s6_addr32), 0);
6820
0
}
6821
6822
static bool bgp_evpn_remote_ip_hash_cmp(const void *p1, const void *p2)
6823
0
{
6824
0
  const struct evpn_remote_ip *ip1 = p1;
6825
0
  const struct evpn_remote_ip *ip2 = p2;
6826
6827
0
  return !ipaddr_cmp(&ip1->addr, &ip2->addr);
6828
0
}
6829
6830
static void bgp_evpn_remote_ip_hash_init(struct bgpevpn *vpn)
6831
0
{
6832
0
  if (!evpn_resolve_overlay_index())
6833
0
    return;
6834
6835
0
  vpn->remote_ip_hash = hash_create(bgp_evpn_remote_ip_hash_key_make,
6836
0
            bgp_evpn_remote_ip_hash_cmp,
6837
0
            "BGP EVPN remote IP hash");
6838
0
}
6839
6840
static void bgp_evpn_remote_ip_hash_free(struct hash_bucket *bucket, void *args)
6841
0
{
6842
0
  struct evpn_remote_ip *ip = (struct evpn_remote_ip *)bucket->data;
6843
0
  struct bgpevpn *vpn = (struct bgpevpn *)args;
6844
6845
0
  bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, false);
6846
6847
0
  list_delete(&ip->macip_path_list);
6848
6849
0
  hash_release(vpn->remote_ip_hash, ip);
6850
0
  XFREE(MTYPE_EVPN_REMOTE_IP, ip);
6851
0
}
6852
6853
static void bgp_evpn_remote_ip_hash_destroy(struct bgpevpn *vpn)
6854
0
{
6855
0
  if (!evpn_resolve_overlay_index() || vpn->remote_ip_hash == NULL)
6856
0
    return;
6857
6858
0
  hash_iterate(vpn->remote_ip_hash,
6859
0
  (void (*)(struct hash_bucket *, void *))bgp_evpn_remote_ip_hash_free,
6860
0
  vpn);
6861
6862
0
  hash_free(vpn->remote_ip_hash);
6863
0
  vpn->remote_ip_hash = NULL;
6864
0
}
6865
6866
/* Add a remote MAC/IP route to hash table */
6867
static void bgp_evpn_remote_ip_hash_add(struct bgpevpn *vpn,
6868
          struct bgp_path_info *pi)
6869
0
{
6870
0
  struct evpn_remote_ip tmp;
6871
0
  struct evpn_remote_ip *ip;
6872
0
  struct prefix_evpn *evp;
6873
6874
0
  if (!evpn_resolve_overlay_index())
6875
0
    return;
6876
6877
0
  if (pi->type != ZEBRA_ROUTE_BGP || pi->sub_type != BGP_ROUTE_IMPORTED
6878
0
      || !CHECK_FLAG(pi->flags, BGP_PATH_VALID))
6879
0
    return;
6880
6881
0
  evp = (struct prefix_evpn *)&pi->net->p;
6882
6883
0
  if (evp->family != AF_EVPN
6884
0
      || evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE
6885
0
      || is_evpn_prefix_ipaddr_none(evp))
6886
0
    return;
6887
6888
0
  tmp.addr = evp->prefix.macip_addr.ip;
6889
0
  ip = hash_lookup(vpn->remote_ip_hash, &tmp);
6890
0
  if (ip) {
6891
0
    if (listnode_lookup(ip->macip_path_list, pi) != NULL)
6892
0
      return;
6893
0
    (void)listnode_add(ip->macip_path_list, pi);
6894
0
    return;
6895
0
  }
6896
6897
0
  ip = hash_get(vpn->remote_ip_hash, &tmp, bgp_evpn_remote_ip_hash_alloc);
6898
0
  (void)listnode_add(ip->macip_path_list, pi);
6899
6900
0
  bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, true);
6901
0
}
6902
6903
/* Delete a remote MAC/IP route from hash table */
6904
static void bgp_evpn_remote_ip_hash_del(struct bgpevpn *vpn,
6905
          struct bgp_path_info *pi)
6906
0
{
6907
0
  struct evpn_remote_ip tmp;
6908
0
  struct evpn_remote_ip *ip;
6909
0
  struct prefix_evpn *evp;
6910
6911
0
  if (!evpn_resolve_overlay_index())
6912
0
    return;
6913
6914
0
  evp = (struct prefix_evpn *)&pi->net->p;
6915
6916
0
  if (evp->family != AF_EVPN
6917
0
      || evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE
6918
0
      || is_evpn_prefix_ipaddr_none(evp))
6919
0
    return;
6920
6921
0
  tmp.addr = evp->prefix.macip_addr.ip;
6922
0
  ip = hash_lookup(vpn->remote_ip_hash, &tmp);
6923
0
  if (ip == NULL)
6924
0
    return;
6925
6926
0
  listnode_delete(ip->macip_path_list, pi);
6927
6928
0
  if (ip->macip_path_list->count == 0) {
6929
0
    bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, false);
6930
0
    hash_release(vpn->remote_ip_hash, ip);
6931
0
    list_delete(&ip->macip_path_list);
6932
0
    XFREE(MTYPE_EVPN_REMOTE_IP, ip);
6933
0
  }
6934
0
}
6935
6936
static void bgp_evpn_remote_ip_hash_iterate(struct bgpevpn *vpn,
6937
              void (*func)(struct hash_bucket *,
6938
               void *),
6939
              void *arg)
6940
0
{
6941
0
  if (!evpn_resolve_overlay_index())
6942
0
    return;
6943
6944
0
  hash_iterate(vpn->remote_ip_hash, func, arg);
6945
0
}
6946
6947
static void show_remote_ip_entry(struct hash_bucket *bucket, void *args)
6948
0
{
6949
0
  char buf[INET6_ADDRSTRLEN];
6950
0
  struct listnode *node = NULL;
6951
0
  struct bgp_path_info *pi = NULL;
6952
0
  struct vty *vty = (struct vty *)args;
6953
0
  struct evpn_remote_ip *ip = (struct evpn_remote_ip *)bucket->data;
6954
6955
0
  vty_out(vty, "  Remote IP: %s\n",
6956
0
    ipaddr2str(&ip->addr, buf, sizeof(buf)));
6957
0
  vty_out(vty, "      Linked MAC/IP routes:\n");
6958
0
  for (ALL_LIST_ELEMENTS_RO(ip->macip_path_list, node, pi))
6959
0
    vty_out(vty, "        %pFX\n", &pi->net->p);
6960
0
}
6961
6962
void bgp_evpn_show_remote_ip_hash(struct hash_bucket *bucket, void *args)
6963
0
{
6964
0
  struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
6965
0
  struct vty *vty = (struct vty *)args;
6966
6967
0
  vty_out(vty, "VNI: %u\n", vpn->vni);
6968
0
  bgp_evpn_remote_ip_hash_iterate(
6969
0
    vpn,
6970
0
    (void (*)(struct hash_bucket *, void *))show_remote_ip_entry,
6971
0
    vty);
6972
0
  vty_out(vty, "\n");
6973
0
}
6974
6975
static void bgp_evpn_remote_ip_hash_link_nexthop(struct hash_bucket *bucket,
6976
             void *args)
6977
0
{
6978
0
  struct evpn_remote_ip *ip = (struct evpn_remote_ip *)bucket->data;
6979
0
  struct bgpevpn *vpn = (struct bgpevpn *)args;
6980
6981
0
  bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, true);
6982
0
}
6983
6984
static void bgp_evpn_remote_ip_hash_unlink_nexthop(struct hash_bucket *bucket,
6985
               void *args)
6986
0
{
6987
0
  struct evpn_remote_ip *ip = (struct evpn_remote_ip *)bucket->data;
6988
0
  struct bgpevpn *vpn = (struct bgpevpn *)args;
6989
6990
0
  bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, false);
6991
0
}
6992
6993
static unsigned int vni_svi_hash_key_make(const void *p)
6994
0
{
6995
0
  const struct bgpevpn *vpn = p;
6996
6997
0
  return jhash_1word(vpn->svi_ifindex, 0);
6998
0
}
6999
7000
static bool vni_svi_hash_cmp(const void *p1, const void *p2)
7001
0
{
7002
0
  const struct bgpevpn *vpn1 = p1;
7003
0
  const struct bgpevpn *vpn2 = p2;
7004
7005
0
  return (vpn1->svi_ifindex == vpn2->svi_ifindex);
7006
0
}
7007
7008
static struct bgpevpn *bgp_evpn_vni_svi_hash_lookup(struct bgp *bgp,
7009
                ifindex_t svi)
7010
0
{
7011
0
  struct bgpevpn *vpn;
7012
0
  struct bgpevpn tmp;
7013
7014
0
  memset(&tmp, 0, sizeof(tmp));
7015
0
  tmp.svi_ifindex = svi;
7016
0
  vpn = hash_lookup(bgp->vni_svi_hash, &tmp);
7017
0
  return vpn;
7018
0
}
7019
7020
static void bgp_evpn_link_to_vni_svi_hash(struct bgp *bgp, struct bgpevpn *vpn)
7021
0
{
7022
0
  if (vpn->svi_ifindex == 0)
7023
0
    return;
7024
7025
0
  (void)hash_get(bgp->vni_svi_hash, vpn, hash_alloc_intern);
7026
0
}
7027
7028
static void bgp_evpn_unlink_from_vni_svi_hash(struct bgp *bgp,
7029
                struct bgpevpn *vpn)
7030
0
{
7031
0
  if (vpn->svi_ifindex == 0)
7032
0
    return;
7033
7034
0
  hash_release(bgp->vni_svi_hash, vpn);
7035
0
}
7036
7037
void bgp_evpn_show_vni_svi_hash(struct hash_bucket *bucket, void *args)
7038
0
{
7039
0
  struct bgpevpn *evpn = (struct bgpevpn *)bucket->data;
7040
0
  struct vty *vty = (struct vty *)args;
7041
7042
0
  vty_out(vty, "SVI: %u VNI: %u\n", evpn->svi_ifindex, evpn->vni);
7043
0
}
7044
7045
/*
7046
 * This function is called for a bgp_nexthop_cache entry when the nexthop is
7047
 * gateway IP overlay index.
7048
 * This function returns true if there is a remote MAC/IP route for the gateway
7049
 * IP in the EVI of the nexthop SVI.
7050
 */
7051
bool bgp_evpn_is_gateway_ip_resolved(struct bgp_nexthop_cache *bnc)
7052
0
{
7053
0
  struct bgp *bgp_evpn = NULL;
7054
0
  struct bgpevpn *vpn = NULL;
7055
0
  struct evpn_remote_ip tmp;
7056
0
  struct prefix *p;
7057
7058
0
  if (!evpn_resolve_overlay_index())
7059
0
    return false;
7060
7061
0
  if (!bnc->nexthop || bnc->nexthop->ifindex == 0)
7062
0
    return false;
7063
7064
0
  bgp_evpn = bgp_get_evpn();
7065
0
  if (!bgp_evpn)
7066
0
    return false;
7067
7068
  /*
7069
   * Gateway IP is resolved by nht over SVI interface.
7070
   * Use this SVI to find corresponding EVI(L2 context)
7071
   */
7072
0
  vpn = bgp_evpn_vni_svi_hash_lookup(bgp_evpn, bnc->nexthop->ifindex);
7073
0
  if (!vpn)
7074
0
    return false;
7075
7076
0
  if (vpn->bgp_vrf != bnc->bgp)
7077
0
    return false;
7078
7079
  /*
7080
   * Check if the gateway IP is present in the EVI remote_ip_hash table
7081
   * which stores all the remote IP addresses received via MAC/IP routes
7082
   * in this EVI
7083
   */
7084
0
  memset(&tmp, 0, sizeof(tmp));
7085
7086
0
  p = &bnc->prefix;
7087
0
  if (p->family == AF_INET) {
7088
0
    tmp.addr.ipa_type = IPADDR_V4;
7089
0
    memcpy(&(tmp.addr.ipaddr_v4), &(p->u.prefix4),
7090
0
           sizeof(struct in_addr));
7091
0
  } else if (p->family == AF_INET6) {
7092
0
    tmp.addr.ipa_type = IPADDR_V6;
7093
0
    memcpy(&(tmp.addr.ipaddr_v6), &(p->u.prefix6),
7094
0
           sizeof(struct in6_addr));
7095
0
  } else
7096
0
    return false;
7097
7098
0
  if (hash_lookup(vpn->remote_ip_hash, &tmp) == NULL)
7099
0
    return false;
7100
7101
0
  return true;
7102
0
}
7103
7104
/* Resolve/Unresolve nexthops when a MAC/IP route is added/deleted */
7105
static void bgp_evpn_remote_ip_process_nexthops(struct bgpevpn *vpn,
7106
            struct ipaddr *addr,
7107
            bool resolve)
7108
0
{
7109
0
  afi_t afi;
7110
0
  struct prefix p;
7111
0
  struct bgp_nexthop_cache *bnc;
7112
0
  struct bgp_nexthop_cache_head *tree = NULL;
7113
7114
0
  if (!vpn->bgp_vrf || vpn->svi_ifindex == 0)
7115
0
    return;
7116
7117
0
  memset(&p, 0, sizeof(p));
7118
7119
0
  if (addr->ipa_type == IPADDR_V4) {
7120
0
    afi = AFI_IP;
7121
0
    p.family = AF_INET;
7122
0
    memcpy(&(p.u.prefix4), &(addr->ipaddr_v4),
7123
0
           sizeof(struct in_addr));
7124
0
    p.prefixlen = IPV4_MAX_BITLEN;
7125
0
  } else if (addr->ipa_type == IPADDR_V6) {
7126
0
    afi = AFI_IP6;
7127
0
    p.family = AF_INET6;
7128
0
    memcpy(&(p.u.prefix6), &(addr->ipaddr_v6),
7129
0
           sizeof(struct in6_addr));
7130
0
    p.prefixlen = IPV6_MAX_BITLEN;
7131
0
  } else
7132
0
    return;
7133
7134
0
  tree = &vpn->bgp_vrf->nexthop_cache_table[afi];
7135
0
  bnc = bnc_find(tree, &p, 0, 0);
7136
7137
0
  if (!bnc || !bnc->is_evpn_gwip_nexthop)
7138
0
    return;
7139
7140
0
  if (!bnc->nexthop || bnc->nexthop->ifindex != vpn->svi_ifindex)
7141
0
    return;
7142
7143
0
  if (BGP_DEBUG(nht, NHT))
7144
0
    zlog_debug("%s(%u): vni %u mac/ip %s for NH %pFX",
7145
0
         vpn->bgp_vrf->name_pretty, vpn->tenant_vrf_id,
7146
0
         vpn->vni, (resolve ? "add" : "delete"),
7147
0
         &bnc->prefix);
7148
7149
  /*
7150
   * MAC/IP route or SVI or tenant vrf being added to EVI.
7151
   * Set nexthop as valid only if it is already L3 reachable
7152
   */
7153
0
  if (resolve && bnc->flags & BGP_NEXTHOP_EVPN_INCOMPLETE) {
7154
0
    bnc->flags &= ~BGP_NEXTHOP_EVPN_INCOMPLETE;
7155
0
    bnc->flags |= BGP_NEXTHOP_VALID;
7156
0
    bnc->change_flags |= BGP_NEXTHOP_MACIP_CHANGED;
7157
0
    evaluate_paths(bnc);
7158
0
  }
7159
7160
   /* MAC/IP route or SVI or tenant vrf being deleted from EVI */
7161
0
  if (!resolve &&  bnc->flags & BGP_NEXTHOP_VALID) {
7162
0
    bnc->flags &= ~BGP_NEXTHOP_VALID;
7163
0
    bnc->flags |= BGP_NEXTHOP_EVPN_INCOMPLETE;
7164
0
    bnc->change_flags |= BGP_NEXTHOP_MACIP_CHANGED;
7165
0
    evaluate_paths(bnc);
7166
0
  }
7167
0
}
7168
7169
void bgp_evpn_handle_resolve_overlay_index_set(struct hash_bucket *bucket,
7170
                 void *arg)
7171
0
{
7172
0
  struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
7173
0
  struct bgp_dest *dest;
7174
0
  struct bgp_path_info *pi;
7175
7176
0
  bgp_evpn_remote_ip_hash_init(vpn);
7177
7178
0
  for (dest = bgp_table_top(vpn->ip_table); dest;
7179
0
       dest = bgp_route_next(dest))
7180
0
    for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
7181
0
      bgp_evpn_remote_ip_hash_add(vpn, pi);
7182
0
}
7183
7184
void bgp_evpn_handle_resolve_overlay_index_unset(struct hash_bucket *bucket,
7185
             void *arg)
7186
0
{
7187
0
  struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
7188
7189
0
  bgp_evpn_remote_ip_hash_destroy(vpn);
7190
0
}
7191
7192
/*
7193
 * Helper function for getting the correct label index for l3vni.
7194
 *
7195
 * Returns the label with the l3vni of the path's label stack.
7196
 *
7197
 * L3vni is always last label. Type5 will only
7198
 * have one label, Type2 will have two.
7199
 *
7200
 */
7201
mpls_label_t *bgp_evpn_path_info_labels_get_l3vni(mpls_label_t *labels,
7202
              uint32_t num_labels)
7203
0
{
7204
0
  if (!labels)
7205
0
    return NULL;
7206
7207
0
  if (!num_labels)
7208
0
    return NULL;
7209
7210
0
  return &labels[num_labels - 1];
7211
0
}
7212
7213
/*
7214
 * Returns the l3vni of the path converted from the label stack.
7215
 */
7216
vni_t bgp_evpn_path_info_get_l3vni(const struct bgp_path_info *pi)
7217
0
{
7218
0
  if (!pi->extra)
7219
0
    return 0;
7220
7221
0
  return label2vni(bgp_evpn_path_info_labels_get_l3vni(
7222
0
    pi->extra->label, pi->extra->num_labels));
7223
0
}
7224
7225
/*
7226
 * Returns true if the l3vni of any of this path doesn't match vrf's l3vni.
7227
 */
7228
static bool bgp_evpn_path_is_dvni(const struct bgp *bgp_vrf,
7229
          const struct bgp_path_info *pi)
7230
0
{
7231
0
  vni_t vni = 0;
7232
7233
0
  vni = bgp_evpn_path_info_get_l3vni(pi);
7234
7235
0
  if ((vni > 0) && (vni != bgp_vrf->l3vni))
7236
0
    return true;
7237
7238
0
  return false;
7239
0
}
7240
7241
/*
7242
 * Returns true if the l3vni of any of the mpath's doesn't match vrf's l3vni.
7243
 */
7244
bool bgp_evpn_mpath_has_dvni(const struct bgp *bgp_vrf,
7245
           struct bgp_path_info *mpinfo)
7246
0
{
7247
0
  for (; mpinfo; mpinfo = bgp_path_info_mpath_next(mpinfo)) {
7248
0
    if (bgp_evpn_path_is_dvni(bgp_vrf, mpinfo))
7249
0
      return true;
7250
0
  }
7251
7252
0
  return false;
7253
0
}