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