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