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