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