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