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