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