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