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