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