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