]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_evpn.c
ospfd: Convert ospf_asbr.c to use error-codes subsystem
[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,
9df2b997 1567 struct bgp_info **ri, uint8_t flags,
f07e1c99 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) ||
9df2b997 1999 !IN6_IS_ADDR_LINKLOCAL(
2000 &evp->prefix.macip_addr.ip.ipaddr_v6)) &&
f07e1c99 2001 CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS) &&
2002 bgpevpn_get_l3vni(vpn))
2003 add_l3_ecomm = 1;
2004
2005 /* Set up extended community. */
2006 build_evpn_route_extcomm(vpn, &attr, add_l3_ecomm);
2007
2008 seq = mac_mobility_seqnum(tmp_ri->attr);
2009
2010 /* Update the route entry. */
2011 update_evpn_route_entry(bgp, vpn, afi, safi, rn,
2012 &attr, 0, &ri, 0, seq);
2013
d62a17ae 2014 /* Perform route selection; this is just to set the flags
f07e1c99 2015 * correctly as local route in the VNI always wins.
d62a17ae 2016 */
2017 evpn_route_select_install(bgp, vpn, rn);
2018
2019 attr_new = ri->attr;
2020
2021 /* Update route in global routing table. */
2022 rd_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
2023 (struct prefix *)evp, &vpn->prd);
2024 assert(rd_rn);
2025 update_evpn_route_entry(bgp, vpn, afi, safi, rd_rn, attr_new, 0,
f07e1c99 2026 &global_ri, 0,
2027 mac_mobility_seqnum(attr_new));
d62a17ae 2028
2029 /* Schedule for processing and unlock node. */
2030 bgp_process(bgp, rd_rn, afi, safi);
2031 bgp_unlock_node(rd_rn);
d62a17ae 2032
f07e1c99 2033 /* Unintern temporary. */
2034 aspath_unintern(&attr.aspath);
2035
2036 }
d62a17ae 2037
2038 return 0;
128ea8ab 2039}
2040
2041/*
2042 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2043 * global routing table. These are also scheduled for withdraw from peers.
2044 */
d62a17ae 2045static int delete_global_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2046{
d62a17ae 2047 afi_t afi;
2048 safi_t safi;
2049 struct bgp_node *rdrn, *rn;
2050 struct bgp_table *table;
2051 struct bgp_info *ri;
128ea8ab 2052
d62a17ae 2053 afi = AFI_L2VPN;
2054 safi = SAFI_EVPN;
128ea8ab 2055
d62a17ae 2056 rdrn = bgp_node_lookup(bgp->rib[afi][safi], (struct prefix *)&vpn->prd);
2057 if (rdrn && rdrn->info) {
2058 table = (struct bgp_table *)rdrn->info;
2059 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
2060 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
128ea8ab 2061
d62a17ae 2062 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2063 continue;
128ea8ab 2064
50f74cf1 2065 delete_evpn_route_entry(bgp, afi, safi, rn, &ri);
d62a17ae 2066 if (ri)
2067 bgp_process(bgp, rn, afi, safi);
2068 }
2069 }
128ea8ab 2070
d62a17ae 2071 /* Unlock RD node. */
2072 if (rdrn)
2073 bgp_unlock_node(rdrn);
128ea8ab 2074
d62a17ae 2075 return 0;
128ea8ab 2076}
2077
2078/*
2079 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2080 * table as well as the per-VNI route table.
2081 */
d62a17ae 2082static int delete_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2083{
d62a17ae 2084 afi_t afi;
2085 safi_t safi;
2086 struct bgp_node *rn;
2087 struct bgp_info *ri;
128ea8ab 2088
d62a17ae 2089 afi = AFI_L2VPN;
2090 safi = SAFI_EVPN;
128ea8ab 2091
d62a17ae 2092 /* First, walk the global route table for this VNI's type-2 local
2093 * routes.
2094 * EVPN routes are a 2-level table, first get the RD table.
2095 */
2096 delete_global_type2_routes(bgp, vpn);
128ea8ab 2097
d62a17ae 2098 /* Next, walk this VNI's route table and delete local type-2 routes. */
2099 for (rn = bgp_table_top(vpn->route_table); rn;
2100 rn = bgp_route_next(rn)) {
2101 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
128ea8ab 2102
d62a17ae 2103 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2104 continue;
128ea8ab 2105
50f74cf1 2106 delete_evpn_route_entry(bgp, afi, safi, rn, &ri);
128ea8ab 2107
d62a17ae 2108 /* Route entry in local table gets deleted immediately. */
2109 if (ri)
2110 bgp_info_reap(rn, ri);
2111 }
128ea8ab 2112
d62a17ae 2113 return 0;
128ea8ab 2114}
2115
50f74cf1 2116/*
2117 * Delete all routes in per ES route-table
2118 */
2119static int delete_all_es_routes(struct bgp *bgp, struct evpnes *es)
2120{
2121 struct bgp_node *rn;
2122 struct bgp_info *ri, *nextri;
2123
2124 /* Walk this ES's route table and delete all routes. */
2125 for (rn = bgp_table_top(es->route_table); rn;
2126 rn = bgp_route_next(rn)) {
2127 for (ri = rn->info; (ri != NULL) && (nextri = ri->next, 1);
2128 ri = nextri) {
2129 bgp_info_delete(rn, ri);
2130 bgp_info_reap(rn, ri);
2131 }
2132 }
2133
2134 return 0;
2135}
2136
128ea8ab 2137/*
2138 * Delete all routes in the per-VNI route table.
2139 */
d62a17ae 2140static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2141{
d62a17ae 2142 struct bgp_node *rn;
2143 struct bgp_info *ri, *nextri;
128ea8ab 2144
d62a17ae 2145 /* Walk this VNI's route table and delete all routes. */
2146 for (rn = bgp_table_top(vpn->route_table); rn;
2147 rn = bgp_route_next(rn)) {
2148 for (ri = rn->info; (ri != NULL) && (nextri = ri->next, 1);
2149 ri = nextri) {
2150 bgp_info_delete(rn, ri);
2151 bgp_info_reap(rn, ri);
2152 }
2153 }
128ea8ab 2154
d62a17ae 2155 return 0;
128ea8ab 2156}
2157
2158/*
2159 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2160 * export RT getting modified or change to tunnel IP. Note that these
2161 * situations need the route in the per-VNI table as well as the global
2162 * table to be updated (as attributes change).
2163 */
d62a17ae 2164static int update_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2165{
d62a17ae 2166 int ret;
2167 struct prefix_evpn p;
128ea8ab 2168
d62a17ae 2169 /* Update and advertise the type-3 route (only one) followed by the
2170 * locally learnt type-2 routes (MACIP) - for this VNI.
2171 */
2172 build_evpn_type3_prefix(&p, vpn->originator_ip);
f07e1c99 2173 ret = update_evpn_route(bgp, vpn, &p, 0, 0);
d62a17ae 2174 if (ret)
2175 return ret;
128ea8ab 2176
d62a17ae 2177 return update_all_type2_routes(bgp, vpn);
128ea8ab 2178}
2179
50f74cf1 2180/* Delete (and withdraw) local routes for specified ES from global and ES table.
2181 * Also remove all other routes from the per ES table.
2182 * Invoked when ES is deleted.
2183 */
2184static int delete_routes_for_es(struct bgp *bgp, struct evpnes *es)
2185{
2186 int ret;
2187 char buf[ESI_STR_LEN];
2188 struct prefix_evpn p;
2189
2190 /* Delete and withdraw locally learnt ES route */
2191 build_evpn_type4_prefix(&p, &es->esi, es->originator_ip.ipaddr_v4);
2192 ret = delete_evpn_type4_route(bgp, es, &p);
2193 if (ret) {
af4c2728 2194 flog_err(BGP_ERR_EVPN_ROUTE_DELETE,
14454c9f
DS
2195 "%u failed to delete type-4 route for ESI %s",
2196 bgp->vrf_id, esi_to_str(&es->esi, buf, sizeof(buf)));
50f74cf1 2197 }
2198
2199 /* Delete all routes from per ES table */
2200 return delete_all_es_routes(bgp, es);
2201}
2202
128ea8ab 2203/*
2204 * Delete (and withdraw) local routes for specified VNI from the global
2205 * table and per-VNI table. After this, remove all other routes from
2206 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2207 * (advertise-all-vni) being disabled.
2208 */
d62a17ae 2209static int delete_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2210{
d62a17ae 2211 int ret;
2212 struct prefix_evpn p;
128ea8ab 2213
d62a17ae 2214 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2215 * followed by type-3 routes (only one) - for this VNI.
2216 */
2217 ret = delete_all_type2_routes(bgp, vpn);
2218 if (ret)
2219 return ret;
128ea8ab 2220
d62a17ae 2221 build_evpn_type3_prefix(&p, vpn->originator_ip);
2222 ret = delete_evpn_route(bgp, vpn, &p);
2223 if (ret)
2224 return ret;
128ea8ab 2225
d62a17ae 2226 /* Delete all routes from the per-VNI table. */
2227 return delete_all_vni_routes(bgp, vpn);
128ea8ab 2228}
2229
2230/*
d1911c26 2231 * There is a tunnel endpoint IP address change for this VNI, delete
2232 * prior type-3 route (if needed) and update.
2233 * Note: Route re-advertisement happens elsewhere after other processing
2234 * other changes.
128ea8ab 2235 */
d62a17ae 2236static int handle_tunnel_ip_change(struct bgp *bgp, struct bgpevpn *vpn,
2237 struct in_addr originator_ip)
128ea8ab 2238{
d62a17ae 2239 struct prefix_evpn p;
128ea8ab 2240
ddd16ed5
MK
2241 /* If VNI is not live, we only need to update the originator ip */
2242 if (!is_vni_live(vpn)) {
2243 vpn->originator_ip = originator_ip;
2244 return 0;
2245 }
2246
db0e1937
MK
2247 /* Update the tunnel-ip hash */
2248 bgp_tip_del(bgp, &vpn->originator_ip);
2249 bgp_tip_add(bgp, &originator_ip);
2250
2251 /* filter routes as martian nexthop db has changed */
2252 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
2253
d62a17ae 2254 /* Need to withdraw type-3 route as the originator IP is part
2255 * of the key.
2256 */
2257 build_evpn_type3_prefix(&p, vpn->originator_ip);
2258 delete_evpn_route(bgp, vpn, &p);
128ea8ab 2259
d62a17ae 2260 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2261 vpn->originator_ip = originator_ip;
d1911c26 2262 return 0;
128ea8ab 2263}
2264
50f74cf1 2265/* Install EVPN route entry in ES */
2266static int install_evpn_route_entry_in_es(struct bgp *bgp,
2267 struct evpnes *es,
2268 struct prefix_evpn *p,
2269 struct bgp_info *parent_ri)
2270{
2271 int ret = 0;
2272 struct bgp_node *rn = NULL;
2273 struct bgp_info *ri = NULL;
2274 struct attr *attr_new = NULL;
2275
2276 /* Create (or fetch) route within the VNI.
2277 * NOTE: There is no RD here.
2278 */
2279 rn = bgp_node_get(es->route_table, (struct prefix *)p);
2280
2281 /* Check if route entry is already present. */
2282 for (ri = rn->info; ri; ri = ri->next)
2283 if (ri->extra &&
2284 (struct bgp_info *)ri->extra->parent == parent_ri)
2285 break;
2286
2287 if (!ri) {
2288 /* Add (or update) attribute to hash. */
2289 attr_new = bgp_attr_intern(parent_ri->attr);
2290
2291 /* Create new route with its attribute. */
2292 ri = info_make(parent_ri->type, BGP_ROUTE_IMPORTED, 0,
2293 parent_ri->peer, attr_new, rn);
2294 SET_FLAG(ri->flags, BGP_INFO_VALID);
2295 bgp_info_extra_get(ri);
2296 ri->extra->parent = parent_ri;
2297 bgp_info_add(rn, ri);
2298 } else {
2299 if (attrhash_cmp(ri->attr, parent_ri->attr)
2300 && !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) {
2301 bgp_unlock_node(rn);
2302 return 0;
2303 }
2304 /* The attribute has changed. */
2305 /* Add (or update) attribute to hash. */
2306 attr_new = bgp_attr_intern(parent_ri->attr);
2307
2308 /* Restore route, if needed. */
2309 if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
2310 bgp_info_restore(rn, ri);
2311
2312 /* Mark if nexthop has changed. */
2313 if (!IPV4_ADDR_SAME(&ri->attr->nexthop, &attr_new->nexthop))
2314 SET_FLAG(ri->flags, BGP_INFO_IGP_CHANGED);
2315
2316 /* Unintern existing, set to new. */
2317 bgp_attr_unintern(&ri->attr);
2318 ri->attr = attr_new;
2319 ri->uptime = bgp_clock();
2320 }
2321
2322 /* Perform route selection and update zebra, if required. */
2323 ret = evpn_es_route_select_install(bgp, es, rn);
2324 return ret;
2325}
2326
d3135ba3 2327/*
2328 * Install route entry into the VRF routing table and invoke route selection.
2329 */
2330static int install_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
2331 struct prefix_evpn *evp,
2332 struct bgp_info *parent_ri)
2333{
2334 struct bgp_node *rn;
2335 struct bgp_info *ri;
1ec31309 2336 struct attr attr;
d3135ba3 2337 struct attr *attr_new;
c4edf708 2338 int ret = 0;
d3135ba3 2339 struct prefix p;
2340 struct prefix *pp = &p;
2341 afi_t afi = 0;
2342 safi_t safi = 0;
1eb88002
MK
2343 char buf[PREFIX_STRLEN];
2344 char buf1[PREFIX_STRLEN];
d3135ba3 2345
2346 memset(pp, 0, sizeof(struct prefix));
3714a385 2347 ip_prefix_from_evpn_prefix(evp, pp);
d3135ba3 2348
1eb88002 2349 if (bgp_debug_zebra(NULL)) {
996c9314
LB
2350 zlog_debug(
2351 "installing evpn prefix %s as ip prefix %s in vrf %s",
2352 prefix2str(evp, buf, sizeof(buf)),
2353 prefix2str(pp, buf1, sizeof(buf)),
2354 vrf_id_to_name(bgp_vrf->vrf_id));
1eb88002
MK
2355 }
2356
d3135ba3 2357 /* Create (or fetch) route within the VRF. */
2358 /* NOTE: There is no RD here. */
3714a385 2359 if (is_evpn_prefix_ipaddr_v4(evp)) {
d3135ba3 2360 afi = AFI_IP;
2361 safi = SAFI_UNICAST;
2362 rn = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
3714a385 2363 } else if (is_evpn_prefix_ipaddr_v6(evp)) {
d3135ba3 2364 afi = AFI_IP6;
2365 safi = SAFI_UNICAST;
2366 rn = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
2367 } else
2368 return 0;
2369
1ec31309 2370 /* EVPN routes currently only support a IPv4 next hop which corresponds
2371 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
450e362d 2372 * or prefix route, we have to convert the next hop to an IPv4-mapped
2373 * address for the rest of the code to flow through. In the case of IPv4,
2374 * make sure to set the flag for next hop attribute.
1ec31309 2375 */
2376 bgp_attr_dup(&attr, parent_ri->attr);
2377 if (afi == AFI_IP6)
2378 evpn_convert_nexthop_to_ipv6(&attr);
450e362d 2379 else
2380 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP);
1ec31309 2381
d3135ba3 2382 /* Check if route entry is already present. */
2383 for (ri = rn->info; ri; ri = ri->next)
2384 if (ri->extra
2385 && (struct bgp_info *)ri->extra->parent == parent_ri)
2386 break;
2387
2388 if (!ri) {
2389 /* Add (or update) attribute to hash. */
1ec31309 2390 attr_new = bgp_attr_intern(&attr);
d3135ba3 2391
2392 /* Create new route with its attribute. */
90f4f482 2393 ri = info_make(parent_ri->type, BGP_ROUTE_IMPORTED, 0,
d3135ba3 2394 parent_ri->peer, attr_new, rn);
2395 SET_FLAG(ri->flags, BGP_INFO_VALID);
2396 bgp_info_extra_get(ri);
528cd74f 2397 ri->extra->parent = bgp_info_lock(parent_ri);
7c82b312 2398 bgp_lock_node((struct bgp_node *)parent_ri->net);
b57ba6d2 2399 if (parent_ri->extra) {
d3135ba3 2400 memcpy(&ri->extra->label, &parent_ri->extra->label,
b57ba6d2
MK
2401 sizeof(ri->extra->label));
2402 ri->extra->num_labels = parent_ri->extra->num_labels;
2403 }
d3135ba3 2404 bgp_info_add(rn, ri);
2405 } else {
1ec31309 2406 if (attrhash_cmp(ri->attr, &attr)
d3135ba3 2407 && !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) {
2408 bgp_unlock_node(rn);
2409 return 0;
2410 }
2411 /* The attribute has changed. */
2412 /* Add (or update) attribute to hash. */
1ec31309 2413 attr_new = bgp_attr_intern(&attr);
d3135ba3 2414
2415 /* Restore route, if needed. */
2416 if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
2417 bgp_info_restore(rn, ri);
2418
2419 /* Mark if nexthop has changed. */
1ec31309 2420 if ((afi == AFI_IP &&
2421 !IPV4_ADDR_SAME(&ri->attr->nexthop, &attr_new->nexthop)) ||
2422 (afi == AFI_IP6 &&
2423 !IPV6_ADDR_SAME(&ri->attr->mp_nexthop_global,
2424 &attr_new->mp_nexthop_global)))
d3135ba3 2425 SET_FLAG(ri->flags, BGP_INFO_IGP_CHANGED);
2426
2427 /* Unintern existing, set to new. */
2428 bgp_attr_unintern(&ri->attr);
2429 ri->attr = attr_new;
2430 ri->uptime = bgp_clock();
2431 }
2432
2433 /* Perform route selection and update zebra, if required. */
1eb88002 2434 bgp_process(bgp_vrf, rn, afi, safi);
d3135ba3 2435
2436 return ret;
2437}
2438
128ea8ab 2439/*
2440 * Install route entry into the VNI routing table and invoke route selection.
2441 */
d62a17ae 2442static int install_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
2443 struct prefix_evpn *p,
2444 struct bgp_info *parent_ri)
2445{
2446 struct bgp_node *rn;
2447 struct bgp_info *ri;
2448 struct attr *attr_new;
2449 int ret;
2450
2451 /* Create (or fetch) route within the VNI. */
2452 /* NOTE: There is no RD here. */
2453 rn = bgp_node_get(vpn->route_table, (struct prefix *)p);
2454
2455 /* Check if route entry is already present. */
2456 for (ri = rn->info; ri; ri = ri->next)
2457 if (ri->extra
2458 && (struct bgp_info *)ri->extra->parent == parent_ri)
2459 break;
2460
2461 if (!ri) {
2462 /* Add (or update) attribute to hash. */
2463 attr_new = bgp_attr_intern(parent_ri->attr);
2464
2465 /* Create new route with its attribute. */
90f4f482 2466 ri = info_make(parent_ri->type, BGP_ROUTE_IMPORTED, 0,
d62a17ae 2467 parent_ri->peer, attr_new, rn);
2468 SET_FLAG(ri->flags, BGP_INFO_VALID);
2469 bgp_info_extra_get(ri);
528cd74f 2470 ri->extra->parent = bgp_info_lock(parent_ri);
7c82b312 2471 bgp_lock_node((struct bgp_node *)parent_ri->net);
b57ba6d2 2472 if (parent_ri->extra) {
d62a17ae 2473 memcpy(&ri->extra->label, &parent_ri->extra->label,
b57ba6d2
MK
2474 sizeof(ri->extra->label));
2475 ri->extra->num_labels = parent_ri->extra->num_labels;
2476 }
d62a17ae 2477 bgp_info_add(rn, ri);
2478 } else {
2479 if (attrhash_cmp(ri->attr, parent_ri->attr)
2480 && !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) {
2481 bgp_unlock_node(rn);
2482 return 0;
2483 }
2484 /* The attribute has changed. */
2485 /* Add (or update) attribute to hash. */
2486 attr_new = bgp_attr_intern(parent_ri->attr);
2487
50f74cf1 2488 /* Restore route, if needed. */
2489 if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
2490 bgp_info_restore(rn, ri);
2491
2492 /* Mark if nexthop has changed. */
2493 if (!IPV4_ADDR_SAME(&ri->attr->nexthop, &attr_new->nexthop))
2494 SET_FLAG(ri->flags, BGP_INFO_IGP_CHANGED);
2495
2496 /* Unintern existing, set to new. */
2497 bgp_attr_unintern(&ri->attr);
2498 ri->attr = attr_new;
2499 ri->uptime = bgp_clock();
2500 }
2501
2502 /* Perform route selection and update zebra, if required. */
2503 ret = evpn_route_select_install(bgp, vpn, rn);
2504
2505 return ret;
2506}
2507
2508/* Uninstall EVPN route entry from ES route table */
2509static int uninstall_evpn_route_entry_in_es(struct bgp *bgp,
2510 struct evpnes *es,
2511 struct prefix_evpn *p,
2512 struct bgp_info *parent_ri)
2513{
2514 int ret;
2515 struct bgp_node *rn;
2516 struct bgp_info *ri;
2517
2518 if (!es->route_table)
2519 return 0;
2520
2521 /* Locate route within the ESI.
2522 * NOTE: There is no RD here.
2523 */
2524 rn = bgp_node_lookup(es->route_table, (struct prefix *)p);
2525 if (!rn)
2526 return 0;
2527
2528 /* Find matching route entry. */
2529 for (ri = rn->info; ri; ri = ri->next)
2530 if (ri->extra &&
2531 (struct bgp_info *)ri->extra->parent == parent_ri)
2532 break;
d62a17ae 2533
50f74cf1 2534 if (!ri)
2535 return 0;
d62a17ae 2536
50f74cf1 2537 /* Mark entry for deletion */
2538 bgp_info_delete(rn, ri);
d62a17ae 2539
2540 /* Perform route selection and update zebra, if required. */
50f74cf1 2541 ret = evpn_es_route_select_install(bgp, es, rn);
2542
2543 /* Unlock route node. */
2544 bgp_unlock_node(rn);
d62a17ae 2545
2546 return ret;
128ea8ab 2547}
2548
d3135ba3 2549/*
2550 * Uninstall route entry from the VRF routing table and send message
2551 * to zebra, if appropriate.
2552 */
2553static int uninstall_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
2554 struct prefix_evpn *evp,
2555 struct bgp_info *parent_ri)
2556{
2557 struct bgp_node *rn;
2558 struct bgp_info *ri;
c4edf708 2559 int ret = 0;
d3135ba3 2560 struct prefix p;
2561 struct prefix *pp = &p;
2562 afi_t afi = 0;
2563 safi_t safi = 0;
1eb88002
MK
2564 char buf[PREFIX_STRLEN];
2565 char buf1[PREFIX_STRLEN];
d3135ba3 2566
2567 memset(pp, 0, sizeof(struct prefix));
3714a385 2568 ip_prefix_from_evpn_prefix(evp, pp);
d3135ba3 2569
1eb88002 2570 if (bgp_debug_zebra(NULL)) {
996c9314
LB
2571 zlog_debug(
2572 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
2573 prefix2str(evp, buf, sizeof(buf)),
2574 prefix2str(pp, buf1, sizeof(buf)),
2575 vrf_id_to_name(bgp_vrf->vrf_id));
1eb88002
MK
2576 }
2577
d3135ba3 2578 /* Locate route within the VRF. */
2579 /* NOTE: There is no RD here. */
3714a385 2580 if (is_evpn_prefix_ipaddr_v4(evp)) {
d3135ba3 2581 afi = AFI_IP;
2582 safi = SAFI_UNICAST;
2583 rn = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
2584 } else {
2585 afi = AFI_IP6;
2586 safi = SAFI_UNICAST;
2587 rn = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
2588 }
2589
2590 if (!rn)
2591 return 0;
2592
2593 /* Find matching route entry. */
2594 for (ri = rn->info; ri; ri = ri->next)
2595 if (ri->extra
2596 && (struct bgp_info *)ri->extra->parent == parent_ri)
2597 break;
2598
2599 if (!ri)
2600 return 0;
2601
2602 /* Mark entry for deletion */
2603 bgp_info_delete(rn, ri);
2604
2605 /* Perform route selection and update zebra, if required. */
1eb88002 2606 bgp_process(bgp_vrf, rn, afi, safi);
d3135ba3 2607
2608 /* Unlock route node. */
2609 bgp_unlock_node(rn);
2610
2611 return ret;
2612}
2613
128ea8ab 2614/*
2615 * Uninstall route entry from the VNI routing table and send message
2616 * to zebra, if appropriate.
2617 */
d62a17ae 2618static int uninstall_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
2619 struct prefix_evpn *p,
2620 struct bgp_info *parent_ri)
128ea8ab 2621{
d62a17ae 2622 struct bgp_node *rn;
2623 struct bgp_info *ri;
2624 int ret;
128ea8ab 2625
d62a17ae 2626 /* Locate route within the VNI. */
2627 /* NOTE: There is no RD here. */
2628 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
2629 if (!rn)
2630 return 0;
128ea8ab 2631
d62a17ae 2632 /* Find matching route entry. */
2633 for (ri = rn->info; ri; ri = ri->next)
2634 if (ri->extra
2635 && (struct bgp_info *)ri->extra->parent == parent_ri)
2636 break;
128ea8ab 2637
d62a17ae 2638 if (!ri)
2639 return 0;
128ea8ab 2640
d62a17ae 2641 /* Mark entry for deletion */
2642 bgp_info_delete(rn, ri);
128ea8ab 2643
d62a17ae 2644 /* Perform route selection and update zebra, if required. */
2645 ret = evpn_route_select_install(bgp, vpn, rn);
128ea8ab 2646
d62a17ae 2647 /* Unlock route node. */
2648 bgp_unlock_node(rn);
128ea8ab 2649
d62a17ae 2650 return ret;
128ea8ab 2651}
2652
50f74cf1 2653/*
2654 * Given a prefix, see if it belongs to ES.
2655 */
2656static int is_prefix_matching_for_es(struct prefix_evpn *p,
2657 struct evpnes *es)
2658{
2659 /* if not an ES route return false */
2660 if (p->prefix.route_type != BGP_EVPN_ES_ROUTE)
2661 return 0;
2662
2663 if (memcmp(&p->prefix.es_addr.esi, &es->esi, sizeof(esi_t)) == 0)
2664 return 1;
2665
2666 return 0;
2667}
2668
5ba238b7
MK
2669/*
2670 * Given a route entry and a VRF, see if this route entry should be
2671 * imported into the VRF i.e., RTs match.
2672 */
996c9314 2673static int is_route_matching_for_vrf(struct bgp *bgp_vrf, struct bgp_info *ri)
5ba238b7
MK
2674{
2675 struct attr *attr = ri->attr;
2676 struct ecommunity *ecom;
2677 int i;
2678
2679 assert(attr);
2680 /* Route should have valid RT to be even considered. */
2681 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
2682 return 0;
2683
2684 ecom = attr->ecommunity;
2685 if (!ecom || !ecom->size)
2686 return 0;
2687
2688 /* For each extended community RT, see if it matches this VNI. If any RT
2689 * matches, we're done.
2690 */
2691 for (i = 0; i < ecom->size; i++) {
d7c0a89a
QY
2692 uint8_t *pnt;
2693 uint8_t type, sub_type;
5ba238b7
MK
2694 struct ecommunity_val *eval;
2695 struct ecommunity_val eval_tmp;
2696 struct vrf_irt_node *irt;
2697
2698 /* Only deal with RTs */
2699 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
2700 eval = (struct ecommunity_val *)(ecom->val
2701 + (i * ECOMMUNITY_SIZE));
2702 type = *pnt++;
2703 sub_type = *pnt++;
2704 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
2705 continue;
2706
2707 /* See if this RT matches specified VNIs import RTs */
2708 irt = lookup_vrf_import_rt(eval);
5d9cbca2 2709 if (irt)
5ba238b7
MK
2710 if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
2711 return 1;
2712
2713 /* Also check for non-exact match. In this, we mask out the AS
2714 * and
2715 * only check on the local-admin sub-field. This is to
2716 * facilitate using
2717 * VNI as the RT for EBGP peering too.
2718 */
2719 irt = NULL;
2720 if (type == ECOMMUNITY_ENCODE_AS
2721 || type == ECOMMUNITY_ENCODE_AS4
2722 || type == ECOMMUNITY_ENCODE_IP) {
2723 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
2724 mask_ecom_global_admin(&eval_tmp, eval);
2725 irt = lookup_vrf_import_rt(&eval_tmp);
2726 }
5d9cbca2 2727 if (irt)
5ba238b7
MK
2728 if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
2729 return 1;
2730 }
2731
2732 return 0;
2733}
2734
128ea8ab 2735/*
2736 * Given a route entry and a VNI, see if this route entry should be
2737 * imported into the VNI i.e., RTs match.
2738 */
d62a17ae 2739static int is_route_matching_for_vni(struct bgp *bgp, struct bgpevpn *vpn,
2740 struct bgp_info *ri)
2741{
2742 struct attr *attr = ri->attr;
2743 struct ecommunity *ecom;
2744 int i;
2745
2746 assert(attr);
2747 /* Route should have valid RT to be even considered. */
2748 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
2749 return 0;
2750
2751 ecom = attr->ecommunity;
2752 if (!ecom || !ecom->size)
2753 return 0;
2754
2755 /* For each extended community RT, see if it matches this VNI. If any RT
2756 * matches, we're done.
2757 */
2758 for (i = 0; i < ecom->size; i++) {
d7c0a89a
QY
2759 uint8_t *pnt;
2760 uint8_t type, sub_type;
d62a17ae 2761 struct ecommunity_val *eval;
2762 struct ecommunity_val eval_tmp;
2763 struct irt_node *irt;
2764
2765 /* Only deal with RTs */
2766 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
2767 eval = (struct ecommunity_val *)(ecom->val
2768 + (i * ECOMMUNITY_SIZE));
2769 type = *pnt++;
2770 sub_type = *pnt++;
2771 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
2772 continue;
2773
2774 /* See if this RT matches specified VNIs import RTs */
2775 irt = lookup_import_rt(bgp, eval);
b1ab0dfe 2776 if (irt)
d62a17ae 2777 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
2778 return 1;
2779
2780 /* Also check for non-exact match. In this, we mask out the AS
2781 * and
2782 * only check on the local-admin sub-field. This is to
2783 * facilitate using
2784 * VNI as the RT for EBGP peering too.
2785 */
2786 irt = NULL;
2787 if (type == ECOMMUNITY_ENCODE_AS
2788 || type == ECOMMUNITY_ENCODE_AS4
2789 || type == ECOMMUNITY_ENCODE_IP) {
2790 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
2791 mask_ecom_global_admin(&eval_tmp, eval);
2792 irt = lookup_import_rt(bgp, &eval_tmp);
2793 }
b1ab0dfe 2794 if (irt)
d62a17ae 2795 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
2796 return 1;
2797 }
2798
2799 return 0;
128ea8ab 2800}
2801
50f74cf1 2802static int install_uninstall_routes_for_es(struct bgp *bgp,
2803 struct evpnes *es,
2804 int install)
2805{
2806 int ret;
2807 afi_t afi;
2808 safi_t safi;
2809 char buf[PREFIX_STRLEN];
2810 char buf1[ESI_STR_LEN];
2811 struct bgp_node *rd_rn, *rn;
2812 struct bgp_table *table;
2813 struct bgp_info *ri;
2814
2815 afi = AFI_L2VPN;
2816 safi = SAFI_EVPN;
2817
2bb9eff4
DS
2818 /*
2819 * Walk entire global routing table and evaluate routes which could be
50f74cf1 2820 * imported into this VRF. Note that we need to loop through all global
2821 * routes to determine which route matches the import rt on vrf
2822 */
2823 for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
2824 rd_rn = bgp_route_next(rd_rn)) {
2825 table = (struct bgp_table *)(rd_rn->info);
2826 if (!table)
2827 continue;
2828
2829 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
2830 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
2831
2832 for (ri = rn->info; ri; ri = ri->next) {
2bb9eff4
DS
2833 /*
2834 * Consider "valid" remote routes applicable for
50f74cf1 2835 * this ES.
2836 */
2837 if (!(CHECK_FLAG(ri->flags, BGP_INFO_VALID)
2838 && ri->type == ZEBRA_ROUTE_BGP
2839 && ri->sub_type == BGP_ROUTE_NORMAL))
2840 continue;
2841
2bb9eff4
DS
2842 if (!is_prefix_matching_for_es(evp, es))
2843 continue;
50f74cf1 2844
2bb9eff4
DS
2845 if (install)
2846 ret = install_evpn_route_entry_in_es(
2847 bgp, es, evp, ri);
2848 else
2849 ret = uninstall_evpn_route_entry_in_es(
2850 bgp, es, evp, ri);
2851
2852 if (ret) {
af4c2728 2853 flog_err(
14454c9f 2854 BGP_ERR_EVPN_FAIL,
2bb9eff4
DS
2855 "Failed to %s EVPN %s route in ESI %s",
2856 install ? "install"
2857 : "uninstall",
2858 prefix2str(evp, buf,
2859 sizeof(buf)),
2860 esi_to_str(&es->esi, buf1,
2861 sizeof(buf1)));
2862 return ret;
50f74cf1 2863 }
2864 }
2865 }
2866 }
2867 return 0;
2868}
2869
5ba238b7
MK
2870/*
2871 * Install or uninstall mac-ip routes are appropriate for this
2872 * particular VRF.
2873 */
996c9314 2874static int install_uninstall_routes_for_vrf(struct bgp *bgp_vrf, int install)
5ba238b7
MK
2875{
2876 afi_t afi;
2877 safi_t safi;
2878 struct bgp_node *rd_rn, *rn;
2879 struct bgp_table *table;
2880 struct bgp_info *ri;
2881 int ret;
2882 char buf[PREFIX_STRLEN];
2883 struct bgp *bgp_def = NULL;
2884
2885 afi = AFI_L2VPN;
2886 safi = SAFI_EVPN;
2887 bgp_def = bgp_get_default();
2888 if (!bgp_def)
2889 return -1;
2890
2891 /* Walk entire global routing table and evaluate routes which could be
2892 * imported into this VRF. Note that we need to loop through all global
2893 * routes to determine which route matches the import rt on vrf
2894 */
2895 for (rd_rn = bgp_table_top(bgp_def->rib[afi][safi]); rd_rn;
2896 rd_rn = bgp_route_next(rd_rn)) {
2897 table = (struct bgp_table *)(rd_rn->info);
2898 if (!table)
2899 continue;
2900
2901 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
2902 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
2903
1eb88002 2904 /* if not mac-ip route skip this route */
996c9314
LB
2905 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
2906 || evp->prefix.route_type
2907 == BGP_EVPN_IP_PREFIX_ROUTE))
5ba238b7
MK
2908 continue;
2909
1eb88002 2910 /* if not a mac+ip route skip this route */
3714a385 2911 if (!(is_evpn_prefix_ipaddr_v4(evp)
2912 || is_evpn_prefix_ipaddr_v6(evp)))
1eb88002
MK
2913 continue;
2914
5ba238b7
MK
2915 for (ri = rn->info; ri; ri = ri->next) {
2916 /* Consider "valid" remote routes applicable for
523cafc4 2917 * this VRF.
2918 */
5ba238b7
MK
2919 if (!(CHECK_FLAG(ri->flags, BGP_INFO_VALID)
2920 && ri->type == ZEBRA_ROUTE_BGP
2921 && ri->sub_type == BGP_ROUTE_NORMAL))
2922 continue;
2923
2924 if (is_route_matching_for_vrf(bgp_vrf, ri)) {
2925 if (install)
996c9314 2926 ret = install_evpn_route_entry_in_vrf(
5ba238b7
MK
2927 bgp_vrf, evp, ri);
2928 else
996c9314 2929 ret = uninstall_evpn_route_entry_in_vrf(
5ba238b7
MK
2930 bgp_vrf, evp, ri);
2931
2932 if (ret) {
af4c2728 2933 flog_err(
14454c9f 2934 BGP_ERR_EVPN_FAIL,
5ba238b7
MK
2935 "Failed to %s EVPN %s route in VRF %s",
2936 install ? "install"
2937 : "uninstall",
2938 prefix2str(evp, buf,
2939 sizeof(buf)),
996c9314
LB
2940 vrf_id_to_name(
2941 bgp_vrf->vrf_id));
5ba238b7
MK
2942 return ret;
2943 }
2944 }
2945 }
2946 }
2947 }
2948
2949 return 0;
2950}
2951
128ea8ab 2952/*
2953 * Install or uninstall routes of specified type that are appropriate for this
2954 * particular VNI.
2955 */
d62a17ae 2956static int install_uninstall_routes_for_vni(struct bgp *bgp,
2957 struct bgpevpn *vpn,
2958 bgp_evpn_route_type rtype,
2959 int install)
2960{
0291c246
MK
2961 afi_t afi;
2962 safi_t safi;
2963 struct bgp_node *rd_rn, *rn;
2964 struct bgp_table *table;
2965 struct bgp_info *ri;
2966 int ret;
d62a17ae 2967
2968 afi = AFI_L2VPN;
2969 safi = SAFI_EVPN;
2970
2971 /* Walk entire global routing table and evaluate routes which could be
2972 * imported into this VPN. Note that we cannot just look at the routes
2973 * for
2974 * the VNI's RD - remote routes applicable for this VNI could have any
2975 * RD.
2976 */
2977 /* EVPN routes are a 2-level table. */
2978 for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
2979 rd_rn = bgp_route_next(rd_rn)) {
2980 table = (struct bgp_table *)(rd_rn->info);
2981 if (!table)
2982 continue;
2983
2984 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
2985 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
2986
2987 if (evp->prefix.route_type != rtype)
2988 continue;
2989
2990 for (ri = rn->info; ri; ri = ri->next) {
2991 /* Consider "valid" remote routes applicable for
2992 * this VNI. */
2993 if (!(CHECK_FLAG(ri->flags, BGP_INFO_VALID)
2994 && ri->type == ZEBRA_ROUTE_BGP
2995 && ri->sub_type == BGP_ROUTE_NORMAL))
2996 continue;
2997
2998 if (is_route_matching_for_vni(bgp, vpn, ri)) {
2999 if (install)
3000 ret = install_evpn_route_entry(
60466a63 3001 bgp, vpn, evp, ri);
d62a17ae 3002 else
3003 ret = uninstall_evpn_route_entry(
3004 bgp, vpn, evp, ri);
3005
3006 if (ret) {
af4c2728 3007 flog_err(
14454c9f 3008 BGP_ERR_EVPN_FAIL,
d62a17ae 3009 "%u: Failed to %s EVPN %s route in VNI %u",
3010 bgp->vrf_id,
3011 install ? "install"
3012 : "uninstall",
3013 rtype == BGP_EVPN_MAC_IP_ROUTE
3014 ? "MACIP"
3015 : "IMET",
3016 vpn->vni);
3017 return ret;
3018 }
3019 }
3020 }
3021 }
3022 }
3023
3024 return 0;
128ea8ab 3025}
3026
50f74cf1 3027/* Install any existing remote ES routes applicable for this ES into its routing
3028 * table. This is invoked when ES comes up.
3029 */
3030static int install_routes_for_es(struct bgp *bgp, struct evpnes *es)
3031{
3032 return install_uninstall_routes_for_es(bgp, es, 1);
3033}
3034
3035
5ba238b7 3036/* Install any existing remote routes applicable for this VRF into VRF RIB. This
523cafc4 3037 * is invoked upon l3vni-add or l3vni import rt change
3038 */
5ba238b7
MK
3039static int install_routes_for_vrf(struct bgp *bgp_vrf)
3040{
3041 install_uninstall_routes_for_vrf(bgp_vrf, 1);
3042 return 0;
3043}
3044
128ea8ab 3045/*
3046 * Install any existing remote routes applicable for this VNI into its
3047 * routing table. This is invoked when a VNI becomes "live" or its Import
3048 * RT is changed.
3049 */
d62a17ae 3050static int install_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3051{
d62a17ae 3052 int ret;
128ea8ab 3053
d62a17ae 3054 /* Install type-3 routes followed by type-2 routes - the ones applicable
3055 * for this VNI.
3056 */
3057 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
3058 1);
3059 if (ret)
3060 return ret;
128ea8ab 3061
d62a17ae 3062 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
3063 1);
128ea8ab 3064}
3065
5ba238b7
MK
3066/* uninstall routes from l3vni vrf. */
3067static int uninstall_routes_for_vrf(struct bgp *bgp_vrf)
3068{
3069 install_uninstall_routes_for_vrf(bgp_vrf, 0);
3070 return 0;
3071}
3072
90e60aa7 3073/*
3074 * Uninstall any existing remote routes for this VNI. One scenario in which
3075 * this is invoked is upon an import RT change.
3076 */
d62a17ae 3077static int uninstall_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 3078{
d62a17ae 3079 int ret;
90e60aa7 3080
d62a17ae 3081 /* Uninstall type-2 routes followed by type-3 routes - the ones
3082 * applicable
3083 * for this VNI.
3084 */
3085 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
3086 0);
3087 if (ret)
3088 return ret;
90e60aa7 3089
d62a17ae 3090 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
3091 0);
90e60aa7 3092}
3093
50f74cf1 3094/* Install or unistall route in ES */
3095static int install_uninstall_route_in_es(struct bgp *bgp, struct evpnes *es,
3096 afi_t afi, safi_t safi,
3097 struct prefix_evpn *evp,
3098 struct bgp_info *ri,
3099 int install)
3100{
3101 int ret = 0;
3102 char buf[ESI_STR_LEN];
3103
3104 if (install)
3105 ret = install_evpn_route_entry_in_es(bgp, es, evp, ri);
3106 else
3107 ret = uninstall_evpn_route_entry_in_es(bgp, es, evp, ri);
3108
3109 if (ret) {
af4c2728 3110 flog_err(
14454c9f
DS
3111 BGP_ERR_EVPN_FAIL,
3112 "%u: Failed to %s EVPN %s route in ESI %s", bgp->vrf_id,
3113 install ? "install" : "uninstall", "ES",
3114 esi_to_str(&evp->prefix.es_addr.esi, buf, sizeof(buf)));
50f74cf1 3115 return ret;
3116 }
3117 return 0;
3118}
3119
d3135ba3 3120/*
3121 * Install or uninstall route in matching VRFs (list).
3122 */
3123static int install_uninstall_route_in_vrfs(struct bgp *bgp_def, afi_t afi,
3124 safi_t safi, struct prefix_evpn *evp,
3125 struct bgp_info *ri,
3126 struct list *vrfs, int install)
3127{
3128 char buf[PREFIX2STR_BUFFER];
3129 struct bgp *bgp_vrf;
3130 struct listnode *node, *nnode;
3131
90264d64 3132 /* Only type-2/type-5 routes go into a VRF */
996c9314
LB
3133 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3134 || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
d3135ba3 3135 return 0;
3136
90264d64 3137 /* if it is type-2 route and not a mac+ip route skip this route */
996c9314 3138 if ((evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
3714a385 3139 && !(is_evpn_prefix_ipaddr_v4(evp)
3140 || is_evpn_prefix_ipaddr_v6(evp)))
30a30f57
MK
3141 return 0;
3142
d3135ba3 3143 for (ALL_LIST_ELEMENTS(vrfs, node, nnode, bgp_vrf)) {
3144 int ret;
3145
3146 if (install)
996c9314 3147 ret = install_evpn_route_entry_in_vrf(bgp_vrf, evp, ri);
d3135ba3 3148 else
996c9314
LB
3149 ret = uninstall_evpn_route_entry_in_vrf(bgp_vrf, evp,
3150 ri);
d3135ba3 3151
3152 if (ret) {
af4c2728 3153 flog_err(BGP_ERR_EVPN_FAIL,
14454c9f
DS
3154 "%u: Failed to %s prefix %s in VRF %s",
3155 bgp_def->vrf_id,
3156 install ? "install" : "uninstall",
3157 prefix2str(evp, buf, sizeof(buf)),
3158 vrf_id_to_name(bgp_vrf->vrf_id));
d3135ba3 3159 return ret;
3160 }
3161 }
3162
3163 return 0;
3164}
3165
128ea8ab 3166/*
3167 * Install or uninstall route in matching VNIs (list).
3168 */
d62a17ae 3169static int install_uninstall_route_in_vnis(struct bgp *bgp, afi_t afi,
3170 safi_t safi, struct prefix_evpn *evp,
3171 struct bgp_info *ri,
3172 struct list *vnis, int install)
128ea8ab 3173{
d62a17ae 3174 struct bgpevpn *vpn;
3175 struct listnode *node, *nnode;
128ea8ab 3176
d62a17ae 3177 for (ALL_LIST_ELEMENTS(vnis, node, nnode, vpn)) {
3178 int ret;
128ea8ab 3179
d62a17ae 3180 if (!is_vni_live(vpn))
3181 continue;
128ea8ab 3182
d62a17ae 3183 if (install)
3184 ret = install_evpn_route_entry(bgp, vpn, evp, ri);
3185 else
3186 ret = uninstall_evpn_route_entry(bgp, vpn, evp, ri);
128ea8ab 3187
d62a17ae 3188 if (ret) {
af4c2728 3189 flog_err(
14454c9f
DS
3190 BGP_ERR_EVPN_FAIL,
3191 "%u: Failed to %s EVPN %s route in VNI %u",
3192 bgp->vrf_id, install ? "install" : "uninstall",
3193 evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3194 ? "MACIP"
3195 : "IMET",
3196 vpn->vni);
d62a17ae 3197 return ret;
3198 }
3199 }
128ea8ab 3200
d62a17ae 3201 return 0;
128ea8ab 3202}
3203
3204/*
50f74cf1 3205 * Install or uninstall route for appropriate VNIs/ESIs.
128ea8ab 3206 */
d62a17ae 3207static int install_uninstall_evpn_route(struct bgp *bgp, afi_t afi, safi_t safi,
3208 struct prefix *p, struct bgp_info *ri,
3209 int import)
3210{
3211 struct prefix_evpn *evp = (struct prefix_evpn *)p;
3212 struct attr *attr = ri->attr;
3213 struct ecommunity *ecom;
3214 int i;
3215
3216 assert(attr);
3217
50f74cf1 3218 /* Only type-2, type-3, type-4 and type-5 are supported currently */
d62a17ae 3219 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
90264d64 3220 || evp->prefix.route_type == BGP_EVPN_IMET_ROUTE
50f74cf1 3221 || evp->prefix.route_type == BGP_EVPN_ES_ROUTE
90264d64 3222 || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
d62a17ae 3223 return 0;
3224
3225 /* If we don't have Route Target, nothing much to do. */
3226 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
3227 return 0;
3228
3229 ecom = attr->ecommunity;
3230 if (!ecom || !ecom->size)
3231 return -1;
3232
50f74cf1 3233 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3234 * attached to the route */
d62a17ae 3235 for (i = 0; i < ecom->size; i++) {
d7c0a89a
QY
3236 uint8_t *pnt;
3237 uint8_t type, sub_type;
d62a17ae 3238 struct ecommunity_val *eval;
3239 struct ecommunity_val eval_tmp;
996c9314 3240 struct irt_node *irt; /* import rt for l2vni */
d3135ba3 3241 struct vrf_irt_node *vrf_irt; /* import rt for l3vni */
50f74cf1 3242 struct evpnes *es;
d62a17ae 3243
3244 /* Only deal with RTs */
3245 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
3246 eval = (struct ecommunity_val *)(ecom->val
3247 + (i * ECOMMUNITY_SIZE));
3248 type = *pnt++;
3249 sub_type = *pnt++;
3250 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
3251 continue;
3252
50f74cf1 3253 /*
3254 * macip routes (type-2) are imported into VNI and VRF tables.
3255 * IMET route is imported into VNI table.
3256 * prefix routes are imported into VRF table.
523cafc4 3257 */
50f74cf1 3258 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE ||
3259 evp->prefix.route_type == BGP_EVPN_IMET_ROUTE ||
3260 evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE) {
d62a17ae 3261
50f74cf1 3262 irt = lookup_import_rt(bgp, eval);
3263 if (irt)
3264 install_uninstall_route_in_vnis(bgp, afi, safi,
3265 evp, ri,
3266 irt->vnis,
3267 import);
3268
3269 vrf_irt = lookup_vrf_import_rt(eval);
3270 if (vrf_irt)
3271 install_uninstall_route_in_vrfs(bgp, afi, safi,
3272 evp, ri,
3273 vrf_irt->vrfs,
3274 import);
3275
3276 /* Also check for non-exact match.
3277 * In this, we mask out the AS and
3278 * only check on the local-admin sub-field.
3279 * This is to facilitate using
3280 * VNI as the RT for EBGP peering too.
3281 */
3282 irt = NULL;
3283 vrf_irt = NULL;
3284 if (type == ECOMMUNITY_ENCODE_AS
3285 || type == ECOMMUNITY_ENCODE_AS4
3286 || type == ECOMMUNITY_ENCODE_IP) {
3287 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
3288 mask_ecom_global_admin(&eval_tmp, eval);
3289 irt = lookup_import_rt(bgp, &eval_tmp);
3290 vrf_irt = lookup_vrf_import_rt(&eval_tmp);
3291 }
3292
3293 if (irt)
3294 install_uninstall_route_in_vnis(bgp, afi, safi,
3295 evp, ri,
3296 irt->vnis,
3297 import);
3298 if (vrf_irt)
3299 install_uninstall_route_in_vrfs(bgp, afi, safi,
3300 evp, ri,
3301 vrf_irt->vrfs,
3302 import);
3303 }
3304
3305 /* es route is imported into the es table */
3306 if (evp->prefix.route_type == BGP_EVPN_ES_ROUTE) {
3307
3308 /* we will match based on the entire esi to avoid
3309 * imoort of an es route for esi2 into esi1
3310 */
3311 es = bgp_evpn_lookup_es(bgp, &evp->prefix.es_addr.esi);
3312 if (es && is_es_local(es))
3313 install_uninstall_route_in_es(bgp, es,
3314 afi, safi,
3315 evp, ri, import);
d62a17ae 3316 }
d62a17ae 3317 }
3318
3319 return 0;
128ea8ab 3320}
3321
2bb9eff4
DS
3322/*
3323 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3324 * routes
3325 */
80b140af
MK
3326static void delete_withdraw_vrf_routes(struct bgp *bgp_vrf)
3327{
3328 /* delete all ipv4 routes and withdraw from peers */
fdf19f06
MK
3329 if (advertise_type5_routes(bgp_vrf, AFI_IP))
3330 bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);
80b140af
MK
3331
3332 /* delete all ipv6 routes and withdraw from peers */
fdf19f06
MK
3333 if (advertise_type5_routes(bgp_vrf, AFI_IP6))
3334 bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
80b140af
MK
3335}
3336
2bb9eff4
DS
3337/*
3338 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3339 * routes
3340 */
80b140af
MK
3341static void update_advertise_vrf_routes(struct bgp *bgp_vrf)
3342{
3343 /* update all ipv4 routes */
fdf19f06
MK
3344 if (advertise_type5_routes(bgp_vrf, AFI_IP))
3345 bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);
80b140af
MK
3346
3347 /* update all ipv6 routes */
fdf19f06
MK
3348 if (advertise_type5_routes(bgp_vrf, AFI_IP6))
3349 bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
80b140af
MK
3350}
3351
676f83b9 3352/*
3353 * update and advertise local routes for a VRF as type-5 routes.
3354 * This is invoked upon RD change for a VRF. Note taht the processing is only
3355 * done in the global route table using the routes which already exist in the
3356 * VRF routing table
3357 */
80b140af 3358static void update_router_id_vrf(struct bgp *bgp_vrf)
676f83b9 3359{
80b140af
MK
3360 /* skip if the RD is configured */
3361 if (is_vrf_rd_configured(bgp_vrf))
3362 return;
3363
3364 /* derive the RD for the VRF based on new router-id */
3365 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
3366
3367 /* update advertise ipv4|ipv6 routes as type-5 routes */
3368 update_advertise_vrf_routes(bgp_vrf);
676f83b9 3369}
3370
3371/*
3372 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3373 * This is invoked upon VRF RD change. The processing is done only from global
3374 * table.
3375 */
80b140af 3376static void withdraw_router_id_vrf(struct bgp *bgp_vrf)
676f83b9 3377{
80b140af
MK
3378 /* skip if the RD is configured */
3379 if (is_vrf_rd_configured(bgp_vrf))
3380 return;
3381
3382 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3383 delete_withdraw_vrf_routes(bgp_vrf);
676f83b9 3384}
3385
90e60aa7 3386/*
3387 * Update and advertise local routes for a VNI. Invoked upon router-id
3388 * change. Note that the processing is done only on the global route table
3389 * using routes that already exist in the per-VNI table.
3390 */
d62a17ae 3391static int update_advertise_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
3392{
3393 struct prefix_evpn p;
3394 struct bgp_node *rn, *global_rn;
3395 struct bgp_info *ri, *global_ri;
3396 struct attr *attr;
3397 afi_t afi = AFI_L2VPN;
3398 safi_t safi = SAFI_EVPN;
3399
3400 /* Locate type-3 route for VNI in the per-VNI table and use its
3401 * attributes to create and advertise the type-3 route for this VNI
3402 * in the global table.
3403 */
3404 build_evpn_type3_prefix(&p, vpn->originator_ip);
3405 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)&p);
3406 if (!rn) /* unexpected */
3407 return 0;
3408 for (ri = rn->info; ri; ri = ri->next)
3409 if (ri->peer == bgp->peer_self && ri->type == ZEBRA_ROUTE_BGP
3410 && ri->sub_type == BGP_ROUTE_STATIC)
3411 break;
3412 if (!ri) /* unexpected */
3413 return 0;
3414 attr = ri->attr;
3415
3416 global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
3417 (struct prefix *)&p, &vpn->prd);
f07e1c99 3418 update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr, 1, &ri,
3419 0, mac_mobility_seqnum(attr));
d62a17ae 3420
3421 /* Schedule for processing and unlock node. */
3422 bgp_process(bgp, global_rn, afi, safi);
3423 bgp_unlock_node(global_rn);
3424
3425 /* Now, walk this VNI's route table and use the route and its attribute
3426 * to create and schedule route in global table.
3427 */
3428 for (rn = bgp_table_top(vpn->route_table); rn;
3429 rn = bgp_route_next(rn)) {
3430 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
3431
3432 /* Identify MAC-IP local routes. */
3433 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
3434 continue;
3435
3436 for (ri = rn->info; ri; ri = ri->next)
3437 if (ri->peer == bgp->peer_self
3438 && ri->type == ZEBRA_ROUTE_BGP
3439 && ri->sub_type == BGP_ROUTE_STATIC)
3440 break;
3441 if (!ri)
3442 continue;
3443
3444 /* Create route in global routing table using this route entry's
3445 * attribute.
3446 */
3447 attr = ri->attr;
3448 global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
3449 (struct prefix *)evp, &vpn->prd);
3450 assert(global_rn);
3451 update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr, 1,
f07e1c99 3452 &global_ri, 0,
3453 mac_mobility_seqnum(attr));
d62a17ae 3454
3455 /* Schedule for processing and unlock node. */
3456 bgp_process(bgp, global_rn, afi, safi);
3457 bgp_unlock_node(global_rn);
3458 }
3459
3460 return 0;
90e60aa7 3461}
3462
3463/*
3464 * Delete (and withdraw) local routes for a VNI - only from the global
3465 * table. Invoked upon router-id change.
3466 */
d62a17ae 3467static int delete_withdraw_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 3468{
d62a17ae 3469 int ret;
3470 struct prefix_evpn p;
3471 struct bgp_node *global_rn;
3472 struct bgp_info *ri;
3473 afi_t afi = AFI_L2VPN;
3474 safi_t safi = SAFI_EVPN;
90e60aa7 3475
d62a17ae 3476 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3477 * for this VNI - from the global table.
3478 */
3479 ret = delete_global_type2_routes(bgp, vpn);
3480 if (ret)
3481 return ret;
90e60aa7 3482
d62a17ae 3483 /* Remove type-3 route for this VNI from global table. */
3484 build_evpn_type3_prefix(&p, vpn->originator_ip);
3485 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
3486 (struct prefix *)&p, &vpn->prd);
3487 if (global_rn) {
3488 /* Delete route entry in the global EVPN table. */
50f74cf1 3489 delete_evpn_route_entry(bgp, afi, safi, global_rn, &ri);
90e60aa7 3490
d62a17ae 3491 /* Schedule for processing - withdraws to peers happen from
3492 * this table.
3493 */
3494 if (ri)
3495 bgp_process(bgp, global_rn, afi, safi);
3496 bgp_unlock_node(global_rn);
3497 }
90e60aa7 3498
d62a17ae 3499 return 0;
90e60aa7 3500}
3501
2d48ee25 3502/*
3503 * Handle router-id change. Update and advertise local routes corresponding
3504 * to this VNI from peers. Note that this is invoked after updating the
3505 * router-id. The routes in the per-VNI table are used to create routes in
3506 * the global table and schedule them.
3507 */
d62a17ae 3508static void update_router_id_vni(struct hash_backet *backet, struct bgp *bgp)
2d48ee25 3509{
0a1a07cb 3510 struct bgpevpn *vpn = (struct bgpevpn *)backet->data;
2d48ee25 3511
d62a17ae 3512 /* Skip VNIs with configured RD. */
3513 if (is_rd_configured(vpn))
3514 return;
2d48ee25 3515
d62a17ae 3516 bgp_evpn_derive_auto_rd(bgp, vpn);
3517 update_advertise_vni_routes(bgp, vpn);
2d48ee25 3518}
3519
3520/*
3521 * Handle router-id change. Delete and withdraw local routes corresponding
3522 * to this VNI from peers. Note that this is invoked prior to updating
3523 * the router-id and is done only on the global route table, the routes
3524 * are needed in the per-VNI table to re-advertise with new router id.
3525 */
d62a17ae 3526static void withdraw_router_id_vni(struct hash_backet *backet, struct bgp *bgp)
2d48ee25 3527{
0a1a07cb 3528 struct bgpevpn *vpn = (struct bgpevpn *)backet->data;
2d48ee25 3529
d62a17ae 3530 /* Skip VNIs with configured RD. */
3531 if (is_rd_configured(vpn))
3532 return;
2d48ee25 3533
d62a17ae 3534 delete_withdraw_vni_routes(bgp, vpn);
2d48ee25 3535}
3536
128ea8ab 3537/*
3538 * Process received EVPN type-2 route (advertise or withdraw).
3539 */
d62a17ae 3540static int process_type2_route(struct peer *peer, afi_t afi, safi_t safi,
d7c0a89a
QY
3541 struct attr *attr, uint8_t *pfx, int psize,
3542 uint32_t addpath_id)
d62a17ae 3543{
3544 struct prefix_rd prd;
3545 struct prefix_evpn p;
554cd77a 3546 struct bgp_route_evpn evpn;
d7c0a89a
QY
3547 uint8_t ipaddr_len;
3548 uint8_t macaddr_len;
b57ba6d2 3549 mpls_label_t label[BGP_MAX_LABELS]; /* holds the VNI(s) as in packet */
d7c0a89a 3550 uint32_t num_labels = 0;
554cd77a 3551 uint32_t eth_tag;
d62a17ae 3552 int ret;
3553
3554 /* Type-2 route should be either 33, 37 or 49 bytes or an
3555 * additional 3 bytes if there is a second label (VNI):
3556 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3557 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3558 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3559 */
3560 if (psize != 33 && psize != 37 && psize != 49 && psize != 36
3561 && psize != 40 && psize != 52) {
af4c2728 3562 flog_err(BGP_ERR_EVPN_ROUTE_INVALID,
14454c9f
DS
3563 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3564 peer->bgp->vrf_id, peer->host, psize);
d62a17ae 3565 return -1;
3566 }
3567
554cd77a
VB
3568 memset(&evpn, 0, sizeof(evpn));
3569
d62a17ae 3570 /* Make prefix_rd */
3571 prd.family = AF_UNSPEC;
3572 prd.prefixlen = 64;
3573 memcpy(&prd.val, pfx, 8);
3574 pfx += 8;
3575
3576 /* Make EVPN prefix. */
3577 memset(&p, 0, sizeof(struct prefix_evpn));
b03b8898 3578 p.family = AF_EVPN;
50f74cf1 3579 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
d62a17ae 3580 p.prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
3581
554cd77a
VB
3582 /* Copy Ethernet Seg Identifier */
3583 memcpy(&evpn.eth_s_id.val, pfx, ESI_LEN);
3584 pfx += ESI_LEN;
d62a17ae 3585
554cd77a
VB
3586 /* Copy Ethernet Tag */
3587 memcpy(&eth_tag, pfx, 4);
3714a385 3588 p.prefix.macip_addr.eth_tag = ntohl(eth_tag);
d62a17ae 3589 pfx += 4;
3590
3591 /* Get the MAC Addr len */
3592 macaddr_len = *pfx++;
3593
3594 /* Get the MAC Addr */
28328ea9 3595 if (macaddr_len == (ETH_ALEN * 8)) {
3714a385 3596 memcpy(&p.prefix.macip_addr.mac.octet, pfx, ETH_ALEN);
28328ea9 3597 pfx += ETH_ALEN;
d62a17ae 3598 } else {
af4c2728 3599 flog_err(
14454c9f 3600 BGP_ERR_EVPN_ROUTE_INVALID,
d62a17ae 3601 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3602 peer->bgp->vrf_id, peer->host, macaddr_len);
3603 return -1;
3604 }
3605
3606
3607 /* Get the IP. */
3608 ipaddr_len = *pfx++;
3609 if (ipaddr_len != 0 && ipaddr_len != IPV4_MAX_BITLEN
3610 && ipaddr_len != IPV6_MAX_BITLEN) {
af4c2728 3611 flog_err(
14454c9f 3612 BGP_ERR_EVPN_ROUTE_INVALID,
d62a17ae 3613 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3614 peer->bgp->vrf_id, peer->host, ipaddr_len);
3615 return -1;
3616 }
3617
3618 if (ipaddr_len) {
3619 ipaddr_len /= 8; /* Convert to bytes. */
3714a385 3620 p.prefix.macip_addr.ip.ipa_type = (ipaddr_len == IPV4_MAX_BYTELEN)
d62a17ae 3621 ? IPADDR_V4
3622 : IPADDR_V6;
3714a385 3623 memcpy(&p.prefix.macip_addr.ip.ip.addr, pfx, ipaddr_len);
d62a17ae 3624 }
3625 pfx += ipaddr_len;
3626
b57ba6d2
MK
3627 /* Get the VNI(s). Stored as bytes here. */
3628 num_labels++;
3629 memset(label, 0, sizeof(label));
3630 memcpy(&label[0], pfx, BGP_LABEL_BYTES);
1b817c78 3631 pfx += BGP_LABEL_BYTES;
b57ba6d2
MK
3632 psize -= (33 + ipaddr_len);
3633 /* Do we have a second VNI? */
3634 if (psize) {
3635 num_labels++;
3636 memcpy(&label[1], pfx, BGP_LABEL_BYTES);
6b11bd8d 3637 /*
3638 * If in future, we are required to access additional fields,
996c9314
LB
3639 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
3640 * the next field
6b11bd8d 3641 */
b57ba6d2 3642 }
d62a17ae 3643
3644 /* Process the route. */
3645 if (attr)
3646 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
3647 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
554cd77a 3648 &prd, &label[0], num_labels, 0, &evpn);
d62a17ae 3649 else
3650 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
3651 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
554cd77a 3652 &prd, &label[0], num_labels, &evpn);
d62a17ae 3653 return ret;
128ea8ab 3654}
3655
3656/*
3657 * Process received EVPN type-3 route (advertise or withdraw).
3658 */
d62a17ae 3659static int process_type3_route(struct peer *peer, afi_t afi, safi_t safi,
d7c0a89a
QY
3660 struct attr *attr, uint8_t *pfx, int psize,
3661 uint32_t addpath_id)
d62a17ae 3662{
3663 struct prefix_rd prd;
3664 struct prefix_evpn p;
d7c0a89a 3665 uint8_t ipaddr_len;
554cd77a 3666 uint32_t eth_tag;
d62a17ae 3667 int ret;
3668
3669 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3670 * IP len (1) and IP (4 or 16).
3671 */
3672 if (psize != 17 && psize != 29) {
af4c2728 3673 flog_err(BGP_ERR_EVPN_ROUTE_INVALID,
14454c9f
DS
3674 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3675 peer->bgp->vrf_id, peer->host, psize);
d62a17ae 3676 return -1;
3677 }
3678
7fd077aa 3679 /* If PMSI is present, log if it is anything other than IR.
3680 * Note: We just simply ignore the values as it is not clear if
3681 * doing anything else is better.
3682 */
3683 if (attr &&
3684 (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL))) {
3685 if (attr->pmsi_tnl_type != PMSI_TNLTYPE_INGR_REPL) {
28642513
DS
3686 flog_warn(BGP_WARN_EVPN_PMSI_PRESENT,
3687 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
7fd077aa 3688 peer->bgp->vrf_id, peer->host,
3689 attr->pmsi_tnl_type);
3690 }
3691 }
3692
d62a17ae 3693 /* Make prefix_rd */
3694 prd.family = AF_UNSPEC;
3695 prd.prefixlen = 64;
3696 memcpy(&prd.val, pfx, 8);
3697 pfx += 8;
3698
3699 /* Make EVPN prefix. */
3700 memset(&p, 0, sizeof(struct prefix_evpn));
b03b8898 3701 p.family = AF_EVPN;
50f74cf1 3702 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
d62a17ae 3703 p.prefix.route_type = BGP_EVPN_IMET_ROUTE;
3704
554cd77a
VB
3705 /* Copy Ethernet Tag */
3706 memcpy(&eth_tag, pfx, 4);
3714a385 3707 p.prefix.imet_addr.eth_tag = ntohl(eth_tag);
d62a17ae 3708 pfx += 4;
3709
3710 /* Get the IP. */
3711 ipaddr_len = *pfx++;
3712 if (ipaddr_len == IPV4_MAX_BITLEN) {
3714a385 3713 p.prefix.imet_addr.ip.ipa_type = IPADDR_V4;
3714 memcpy(&p.prefix.imet_addr.ip.ip.addr, pfx, IPV4_MAX_BYTELEN);
d62a17ae 3715 } else {
af4c2728 3716 flog_err(
14454c9f 3717 BGP_ERR_EVPN_ROUTE_INVALID,
d62a17ae 3718 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3719 peer->bgp->vrf_id, peer->host, ipaddr_len);
3720 return -1;
3721 }
3722
3723 /* Process the route. */
3724 if (attr)
3725 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
3726 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 3727 &prd, NULL, 0, 0, NULL);
d62a17ae 3728 else
3729 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
3730 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 3731 &prd, NULL, 0, NULL);
d62a17ae 3732 return ret;
128ea8ab 3733}
3734
50f74cf1 3735/*
3736 * Process received EVPN type-4 route (advertise or withdraw).
3737 */
3738static int process_type4_route(struct peer *peer, afi_t afi, safi_t safi,
3739 struct attr *attr, uint8_t *pfx, int psize,
3740 uint32_t addpath_id)
3741{
3742 int ret;
3743 esi_t esi;
3744 uint8_t ipaddr_len;
3745 struct in_addr vtep_ip;
3746 struct prefix_rd prd;
3747 struct prefix_evpn p;
3748
3749 /* Type-4 route should be either 23 or 35 bytes
3750 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
3751 */
3752 if (psize != 23 && psize != 35) {
af4c2728 3753 flog_err(BGP_ERR_EVPN_ROUTE_INVALID,
14454c9f
DS
3754 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
3755 peer->bgp->vrf_id, peer->host, psize);
50f74cf1 3756 return -1;
3757 }
3758
3759 /* Make prefix_rd */
3760 prd.family = AF_UNSPEC;
3761 prd.prefixlen = 64;
3762 memcpy(&prd.val, pfx, 8);
3763 pfx += 8;
3764
3765 /* get the ESI */
3766 memcpy(&esi, pfx, ESI_BYTES);
3767 pfx += ESI_BYTES;
3768
3769
3770 /* Get the IP. */
3771 ipaddr_len = *pfx++;
3772 if (ipaddr_len == IPV4_MAX_BITLEN) {
3773 memcpy(&vtep_ip, pfx, IPV4_MAX_BYTELEN);
3774 } else {
af4c2728 3775 flog_err(
14454c9f 3776 BGP_ERR_EVPN_ROUTE_INVALID,
50f74cf1 3777 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
3778 peer->bgp->vrf_id, peer->host, ipaddr_len);
3779 return -1;
3780 }
3781
3782 build_evpn_type4_prefix(&p, &esi, vtep_ip);
3783 /* Process the route. */
3784 if (attr) {
3785 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
3786 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
3787 &prd, NULL, 0, 0, NULL);
2bb9eff4 3788 } else {
50f74cf1 3789 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
3790 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
3791 &prd, NULL, 0, NULL);
3792 }
3793 return ret;
3794}
3795
3796
128ea8ab 3797/*
3798 * Process received EVPN type-5 route (advertise or withdraw).
3799 */
d62a17ae 3800static int process_type5_route(struct peer *peer, afi_t afi, safi_t safi,
d7c0a89a
QY
3801 struct attr *attr, uint8_t *pfx, int psize,
3802 uint32_t addpath_id, int withdraw)
d62a17ae 3803{
3804 struct prefix_rd prd;
3805 struct prefix_evpn p;
3806 struct bgp_route_evpn evpn;
d7c0a89a
QY
3807 uint8_t ippfx_len;
3808 uint32_t eth_tag;
b57ba6d2 3809 mpls_label_t label; /* holds the VNI as in the packet */
d62a17ae 3810 int ret;
3811
3812 /* Type-5 route should be 34 or 58 bytes:
3813 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3814 * GW (4 or 16) and VNI (3).
3815 * Note that the IP and GW should both be IPv4 or both IPv6.
3816 */
3817 if (psize != 34 && psize != 58) {
af4c2728 3818 flog_err(BGP_ERR_EVPN_ROUTE_INVALID,
14454c9f
DS
3819 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3820 peer->bgp->vrf_id, peer->host, psize);
d62a17ae 3821 return -1;
3822 }
3823
3824 /* Make prefix_rd */
3825 prd.family = AF_UNSPEC;
3826 prd.prefixlen = 64;
3827 memcpy(&prd.val, pfx, 8);
3828 pfx += 8;
3829
3830 /* Make EVPN prefix. */
3831 memset(&p, 0, sizeof(struct prefix_evpn));
b03b8898 3832 p.family = AF_EVPN;
50f74cf1 3833 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
d62a17ae 3834 p.prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
3835
3836 /* Additional information outside of prefix - ESI and GW IP */
3837 memset(&evpn, 0, sizeof(evpn));
3838
3839 /* Fetch ESI */
3840 memcpy(&evpn.eth_s_id.val, pfx, 10);
3841 pfx += 10;
3842
3843 /* Fetch Ethernet Tag. */
3844 memcpy(&eth_tag, pfx, 4);
3714a385 3845 p.prefix.prefix_addr.eth_tag = ntohl(eth_tag);
d62a17ae 3846 pfx += 4;
3847
3848 /* Fetch IP prefix length. */
3849 ippfx_len = *pfx++;
3850 if (ippfx_len > IPV6_MAX_BITLEN) {
af4c2728 3851 flog_err(
14454c9f 3852 BGP_ERR_EVPN_ROUTE_INVALID,
d62a17ae 3853 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3854 peer->bgp->vrf_id, peer->host, ippfx_len);
3855 return -1;
3856 }
3714a385 3857 p.prefix.prefix_addr.ip_prefix_length = ippfx_len;
d62a17ae 3858
3859 /* Determine IPv4 or IPv6 prefix */
3860 /* Since the address and GW are from the same family, this just becomes
3861 * a simple check on the total size.
3862 */
3863 if (psize == 34) {
3714a385 3864 SET_IPADDR_V4(&p.prefix.prefix_addr.ip);
3865 memcpy(&p.prefix.prefix_addr.ip.ipaddr_v4, pfx, 4);
d62a17ae 3866 pfx += 4;
3867 memcpy(&evpn.gw_ip.ipv4, pfx, 4);
3868 pfx += 4;
d62a17ae 3869 } else {
3714a385 3870 SET_IPADDR_V6(&p.prefix.prefix_addr.ip);
3871 memcpy(&p.prefix.prefix_addr.ip.ipaddr_v6, pfx, 16);
d62a17ae 3872 pfx += 16;
3873 memcpy(&evpn.gw_ip.ipv6, pfx, 16);
3874 pfx += 16;
d62a17ae 3875 }
3876
b57ba6d2
MK
3877 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3878 memset(&label, 0, sizeof(label));
3879 memcpy(&label, pfx, BGP_LABEL_BYTES);
6b11bd8d 3880
3881 /*
3882 * If in future, we are required to access additional fields,
996c9314
LB
3883 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
3884 * field
6b11bd8d 3885 */
d62a17ae 3886
3887 /* Process the route. */
3888 if (!withdraw)
3889 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
3890 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 3891 &prd, &label, 1, 0, &evpn);
d62a17ae 3892 else
3893 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
3894 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 3895 &prd, &label, 1, &evpn);
d62a17ae 3896
3897 return ret;
3898}
3899
3900static void evpn_mpattr_encode_type5(struct stream *s, struct prefix *p,
996c9314 3901 struct prefix_rd *prd, mpls_label_t *label,
d7c0a89a 3902 uint32_t num_labels, struct attr *attr)
d62a17ae 3903{
3904 int len;
3905 char temp[16];
3906 struct evpn_addr *p_evpn_p;
3907
3908 memset(&temp, 0, 16);
b03b8898 3909 if (p->family != AF_EVPN)
d62a17ae 3910 return;
3911 p_evpn_p = &(p->u.prefix_evpn);
3912
e9fc2840 3913 /* len denites the total len of IP and GW-IP in the route
523cafc4 3914 IP and GW-IP have to be both ipv4 or ipv6
3915 */
3714a385 3916 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
e9fc2840 3917 len = 8; /* IP and GWIP are both ipv4 */
d62a17ae 3918 else
e9fc2840 3919 len = 32; /* IP and GWIP are both ipv6 */
d62a17ae 3920 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3921 stream_putc(s, 8 + 10 + 4 + 1 + len + 3);
3922 stream_put(s, prd->val, 8);
0af35d90 3923 if (attr)
d62a17ae 3924 stream_put(s, &(attr->evpn_overlay.eth_s_id), 10);
3925 else
3926 stream_put(s, &temp, 10);
3714a385 3927 stream_putl(s, p_evpn_p->prefix_addr.eth_tag);
3928 stream_putc(s, p_evpn_p->prefix_addr.ip_prefix_length);
3929 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
3930 stream_put_ipv4(s, p_evpn_p->prefix_addr.ip.ipaddr_v4.s_addr);
d62a17ae 3931 else
3714a385 3932 stream_put(s, &p_evpn_p->prefix_addr.ip.ipaddr_v6, 16);
0af35d90 3933 if (attr) {
3714a385 3934 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
d62a17ae 3935 stream_put_ipv4(s,
3936 attr->evpn_overlay.gw_ip.ipv4.s_addr);
3937 else
3938 stream_put(s, &(attr->evpn_overlay.gw_ip.ipv6), 16);
3939 } else {
3714a385 3940 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
d62a17ae 3941 stream_put_ipv4(s, 0);
3942 else
3943 stream_put(s, &temp, 16);
3944 }
3945
b57ba6d2 3946 if (num_labels)
d62a17ae 3947 stream_put(s, label, 3);
3948 else
3949 stream_put3(s, 0);
128ea8ab 3950}
3951
3952/*
3953 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3954 */
d62a17ae 3955static void cleanup_vni_on_disable(struct hash_backet *backet, struct bgp *bgp)
128ea8ab 3956{
d62a17ae 3957 struct bgpevpn *vpn = (struct bgpevpn *)backet->data;
128ea8ab 3958
d62a17ae 3959 /* Remove EVPN routes and schedule for processing. */
3960 delete_routes_for_vni(bgp, vpn);
128ea8ab 3961
d62a17ae 3962 /* Clear "live" flag and see if hash needs to be freed. */
3963 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
3964 if (!is_vni_configured(vpn))
3965 bgp_evpn_free(bgp, vpn);
128ea8ab 3966}
3967
3968/*
3969 * Free a VNI entry; iterator function called during cleanup.
3970 */
d62a17ae 3971static void free_vni_entry(struct hash_backet *backet, struct bgp *bgp)
128ea8ab 3972{
0a1a07cb 3973 struct bgpevpn *vpn = (struct bgpevpn *)backet->data;
128ea8ab 3974
d62a17ae 3975 delete_all_vni_routes(bgp, vpn);
3976 bgp_evpn_free(bgp, vpn);
128ea8ab 3977}
3978
c581d8b0
MK
3979/*
3980 * Derive AUTO import RT for BGP VRF - L3VNI
3981 */
3982static void evpn_auto_rt_import_add_for_vrf(struct bgp *bgp_vrf)
3983{
10ebe1ab
MK
3984 struct bgp *bgp_def = NULL;
3985
c581d8b0 3986 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
10ebe1ab
MK
3987 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
3988
3989 /* Map RT to VRF */
3990 bgp_def = bgp_get_default();
3991 if (!bgp_def)
3992 return;
3993 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
c581d8b0
MK
3994}
3995
3996/*
3997 * Delete AUTO import RT from BGP VRF - L3VNI
3998 */
3999static void evpn_auto_rt_import_delete_for_vrf(struct bgp *bgp_vrf)
4000{
4001 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
4002}
4003
4004/*
4005 * Derive AUTO export RT for BGP VRF - L3VNI
4006 */
4007static void evpn_auto_rt_export_add_for_vrf(struct bgp *bgp_vrf)
4008{
4009 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
4010 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
4011}
4012
4013/*
4014 * Delete AUTO export RT from BGP VRF - L3VNI
4015 */
4016static void evpn_auto_rt_export_delete_for_vrf(struct bgp *bgp_vrf)
4017{
4018 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
4019}
128ea8ab 4020
f1f8b53c
MK
4021static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp *bgp_vrf)
4022{
4023 struct bgp *bgp_def = NULL;
4024 struct listnode *node = NULL;
4025 struct bgpevpn *vpn = NULL;
4026
4027 bgp_def = bgp_get_default();
4028 if (!bgp_def)
4029 return;
4030
4992b4ae
MK
4031 /* update all type-5 routes */
4032 update_advertise_vrf_routes(bgp_vrf);
4033
4034 /* update all type-2 routes */
f1f8b53c
MK
4035 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
4036 update_routes_for_vni(bgp_def, vpn);
4037}
4038
bf1061d8
VB
4039/*
4040 * Handle autort change for a given VNI.
4041 */
4042static void update_autort_vni(struct hash_backet *backet, struct bgp *bgp)
4043{
4044 struct bgpevpn *vpn = backet->data;
bf1061d8 4045
bf1061d8
VB
4046 if (!is_import_rt_configured(vpn)) {
4047 if (is_vni_live(vpn))
4048 bgp_evpn_uninstall_routes(bgp, vpn);
4049 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
bf1061d8
VB
4050 list_delete_all_node(vpn->import_rtl);
4051 bgp_evpn_derive_auto_rt_import(bgp, vpn);
4052 if (is_vni_live(vpn))
4053 bgp_evpn_install_routes(bgp, vpn);
4054 }
4055 if (!is_export_rt_configured(vpn)) {
bf1061d8
VB
4056 list_delete_all_node(vpn->export_rtl);
4057 bgp_evpn_derive_auto_rt_export(bgp, vpn);
4058 if (is_vni_live(vpn))
4059 bgp_evpn_handle_export_rt_change(bgp, vpn);
4060 }
4061}
4062
128ea8ab 4063/*
4064 * Public functions.
4065 */
4066
5424b7ba 4067/* withdraw type-5 route corresponding to ip prefix */
31310b25 4068void bgp_evpn_withdraw_type5_route(struct bgp *bgp_vrf, struct prefix *p,
5424b7ba
MK
4069 afi_t afi, safi_t safi)
4070{
4071 int ret = 0;
4072 struct prefix_evpn evp;
4073 char buf[PREFIX_STRLEN];
4074
31310b25 4075 build_type5_prefix_from_ip_prefix(&evp, p);
5424b7ba
MK
4076 ret = delete_evpn_type5_route(bgp_vrf, &evp);
4077 if (ret) {
af4c2728 4078 flog_err(
14454c9f 4079 BGP_ERR_EVPN_ROUTE_DELETE,
996c9314
LB
4080 "%u failed to delete type-5 route for prefix %s in vrf %s",
4081 bgp_vrf->vrf_id, prefix2str(p, buf, sizeof(buf)),
4082 vrf_id_to_name(bgp_vrf->vrf_id));
5424b7ba
MK
4083 }
4084}
4085
342dd0c6 4086/* withdraw all type-5 routes for an address family */
996c9314 4087void bgp_evpn_withdraw_type5_routes(struct bgp *bgp_vrf, afi_t afi, safi_t safi)
342dd0c6 4088{
4089 struct bgp_table *table = NULL;
4090 struct bgp_node *rn = NULL;
25f2ca53 4091 struct bgp_info *ri;
342dd0c6 4092
053905d2 4093 table = bgp_vrf->rib[afi][safi];
25f2ca53 4094 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
4095 /* Only care about "selected" routes - non-imported. */
4096 /* TODO: Support for AddPath for EVPN. */
4097 for (ri = rn->info; ri; ri = ri->next) {
996c9314
LB
4098 if (CHECK_FLAG(ri->flags, BGP_INFO_SELECTED)
4099 && (!ri->extra || !ri->extra->parent)) {
25f2ca53 4100 bgp_evpn_withdraw_type5_route(bgp_vrf, &rn->p,
4101 afi, safi);
4102 break;
4103 }
4104 }
4105 }
5424b7ba 4106}
342dd0c6 4107
2f69f6d3 4108/*
4109 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4110 * to this function correspond to those of the source IP prefix (best
4111 * path in the case of the attr. In the case of a local prefix (when we
4112 * are advertising local subnets), the src_attr will be NULL.
4113 */
31310b25 4114void bgp_evpn_advertise_type5_route(struct bgp *bgp_vrf, struct prefix *p,
996c9314
LB
4115 struct attr *src_attr, afi_t afi,
4116 safi_t safi)
5424b7ba
MK
4117{
4118 int ret = 0;
4119 struct prefix_evpn evp;
4120 char buf[PREFIX_STRLEN];
7c82b312 4121
31310b25 4122 build_type5_prefix_from_ip_prefix(&evp, p);
2f69f6d3 4123 ret = update_evpn_type5_route(bgp_vrf, &evp, src_attr);
4124 if (ret)
af4c2728 4125 flog_err(BGP_ERR_EVPN_ROUTE_CREATE,
14454c9f
DS
4126 "%u: Failed to create type-5 route for prefix %s",
4127 bgp_vrf->vrf_id, prefix2str(p, buf, sizeof(buf)));
342dd0c6 4128}
4129
2f69f6d3 4130/* Inject all prefixes of a particular address-family (currently, IPv4 or
4131 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4132 * advertisement is enabled.
4133 */
996c9314
LB
4134void bgp_evpn_advertise_type5_routes(struct bgp *bgp_vrf, afi_t afi,
4135 safi_t safi)
342dd0c6 4136{
4137 struct bgp_table *table = NULL;
4138 struct bgp_node *rn = NULL;
2f69f6d3 4139 struct bgp_info *ri;
342dd0c6 4140
053905d2 4141 table = bgp_vrf->rib[afi][safi];
31310b25 4142 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
2f69f6d3 4143 /* Need to identify the "selected" route entry to use its
25f2ca53 4144 * attribute. Also, we only consider "non-imported" routes.
2f69f6d3 4145 * TODO: Support for AddPath for EVPN.
4146 */
4147 for (ri = rn->info; ri; ri = ri->next) {
996c9314
LB
4148 if (CHECK_FLAG(ri->flags, BGP_INFO_SELECTED)
4149 && (!ri->extra || !ri->extra->parent)) {
53c84f78
MK
4150
4151 /* apply the route-map */
4152 if (bgp_vrf->adv_cmd_rmap[afi][safi].map) {
4153 int ret = 0;
4154
996c9314
LB
4155 ret = route_map_apply(
4156 bgp_vrf->adv_cmd_rmap[afi][safi]
4157 .map,
4158 &rn->p, RMAP_BGP, ri);
53c84f78
MK
4159 if (ret == RMAP_DENYMATCH)
4160 continue;
4161 }
996c9314
LB
4162 bgp_evpn_advertise_type5_route(
4163 bgp_vrf, &rn->p, ri->attr, afi, safi);
2f69f6d3 4164 break;
4165 }
4166 }
31310b25 4167 }
342dd0c6 4168}
4169
996c9314 4170void evpn_rt_delete_auto(struct bgp *bgp, vni_t vni, struct list *rtl)
c581d8b0
MK
4171{
4172 struct listnode *node, *nnode, *node_to_del;
4173 struct ecommunity *ecom, *ecom_auto;
4174 struct ecommunity_val eval;
4175
bf1061d8
VB
4176 if (bgp->advertise_autort_rfc8365)
4177 vni |= EVPN_AUTORT_VXLAN;
c581d8b0
MK
4178 encode_route_target_as((bgp->as & 0xFFFF), vni, &eval);
4179
4180 ecom_auto = ecommunity_new();
4181 ecommunity_add_val(ecom_auto, &eval);
4182 node_to_del = NULL;
4183
4184 for (ALL_LIST_ELEMENTS(rtl, node, nnode, ecom)) {
4185 if (ecommunity_match(ecom, ecom_auto)) {
4186 ecommunity_free(&ecom);
4187 node_to_del = node;
4188 }
4189 }
4190
4191 if (node_to_del)
4192 list_delete_node(rtl, node_to_del);
4193
4194 ecommunity_free(&ecom_auto);
4195}
4196
4197void bgp_evpn_configure_import_rt_for_vrf(struct bgp *bgp_vrf,
10ebe1ab 4198 struct ecommunity *ecomadd)
c581d8b0 4199{
5ba238b7
MK
4200 /* uninstall routes from vrf */
4201 uninstall_routes_for_vrf(bgp_vrf);
10ebe1ab
MK
4202
4203 /* Cleanup the RT to VRF mapping */
4204 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
4205
c581d8b0
MK
4206 /* Remove auto generated RT */
4207 evpn_auto_rt_import_delete_for_vrf(bgp_vrf);
4208
4209 /* Add the newly configured RT to RT list */
4210 listnode_add_sort(bgp_vrf->vrf_import_rtl, ecomadd);
4211 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
4212
10ebe1ab
MK
4213 /* map VRF to its RTs */
4214 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
4215
5ba238b7
MK
4216 /* install routes matching the new VRF */
4217 install_routes_for_vrf(bgp_vrf);
c581d8b0
MK
4218}
4219
4220void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf,
4221 struct ecommunity *ecomdel)
4222{
4223 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
4224 struct ecommunity *ecom = NULL;
4225
5ba238b7
MK
4226 /* uninstall routes from vrf */
4227 uninstall_routes_for_vrf(bgp_vrf);
10ebe1ab
MK
4228
4229 /* Cleanup the RT to VRF mapping */
4230 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
4231
c581d8b0
MK
4232 /* remove the RT from the RT list */
4233 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
4234 if (ecommunity_match(ecom, ecomdel)) {
4235 ecommunity_free(&ecom);
4236 node_to_del = node;
4237 break;
4238 }
4239 }
4240
4241 if (node_to_del)
4242 list_delete_node(bgp_vrf->vrf_import_rtl, node_to_del);
4243
b3a4db3d 4244 assert(bgp_vrf->vrf_import_rtl);
c581d8b0 4245 /* fallback to auto import rt, if this was the last RT */
1230a82d 4246 if (bgp_vrf->vrf_import_rtl && list_isempty(bgp_vrf->vrf_import_rtl)) {
c581d8b0
MK
4247 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
4248 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
4249 }
4250
10ebe1ab
MK
4251 /* map VRFs to its RTs */
4252 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
4253
5ba238b7
MK
4254 /* install routes matching this new RT */
4255 install_routes_for_vrf(bgp_vrf);
c581d8b0
MK
4256}
4257
4258void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf,
4259 struct ecommunity *ecomadd)
4260{
4261 /* remove auto-generated RT */
4262 evpn_auto_rt_export_delete_for_vrf(bgp_vrf);
4263
4264 /* Add the new RT to the RT list */
4265 listnode_add_sort(bgp_vrf->vrf_export_rtl, ecomadd);
4266 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
4267
f1f8b53c 4268 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
c581d8b0
MK
4269}
4270
4271void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf,
4272 struct ecommunity *ecomdel)
4273{
4274 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
4275 struct ecommunity *ecom = NULL;
4276
4277 /* Remove the RT from the RT list */
4278 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_export_rtl, node, nnode, ecom)) {
4279 if (ecommunity_match(ecom, ecomdel)) {
4280 ecommunity_free(&ecom);
4281 node_to_del = node;
4282 break;
4283 }
4284 }
4285
4286 if (node_to_del)
4287 list_delete_node(bgp_vrf->vrf_export_rtl, node_to_del);
4288
1525e99f
DS
4289 /*
4290 * Temporary assert to make SA happy.
4291 * The ALL_LIST_ELEMENTS macro above has a NULL check
4292 * which means that SA is going to complain about
4293 * the list_isempty call, which doesn't NULL check.
4294 * So until we get this situation cleaned up, here
4295 * we are.
4296 */
4297 assert(bgp_vrf->vrf_export_rtl);
4298
c581d8b0 4299 /* fall back to auto-generated RT if this was the last RT */
1525e99f 4300 if (list_isempty(bgp_vrf->vrf_export_rtl)) {
c581d8b0
MK
4301 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
4302 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
4303 }
4304
f1f8b53c 4305 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
c581d8b0
MK
4306}
4307
2d48ee25 4308/*
4309 * Handle change to BGP router id. This is invoked twice by the change
4310 * handler, first before the router id has been changed and then after
4311 * the router id has been changed. The first invocation will result in
676f83b9 4312 * local routes for all VNIs/VRF being deleted and withdrawn and the next
2d48ee25 4313 * will result in the routes being re-advertised.
4314 */
d62a17ae 4315void bgp_evpn_handle_router_id_update(struct bgp *bgp, int withdraw)
2d48ee25 4316{
676f83b9 4317 if (withdraw) {
4318
4319 /* delete and withdraw all the type-5 routes
523cafc4 4320 stored in the global table for this vrf
4321 */
80b140af 4322 withdraw_router_id_vrf(bgp);
676f83b9 4323
4324 /* delete all the VNI routes (type-2/type-3) routes for all the
523cafc4 4325 * L2-VNIs
4326 */
d62a17ae 4327 hash_iterate(bgp->vnihash,
4328 (void (*)(struct hash_backet *,
4329 void *))withdraw_router_id_vni,
4330 bgp);
676f83b9 4331 } else {
4332
4333 /* advertise all routes in the vrf as type-5 routes with the new
523cafc4 4334 * RD
4335 */
80b140af 4336 update_router_id_vrf(bgp);
676f83b9 4337
4338 /* advertise all the VNI routes (type-2/type-3) routes with the
523cafc4 4339 * new RD
4340 */
d62a17ae 4341 hash_iterate(bgp->vnihash,
4342 (void (*)(struct hash_backet *,
4343 void *))update_router_id_vni,
4344 bgp);
676f83b9 4345 }
2d48ee25 4346}
4347
bf1061d8
VB
4348/*
4349 * Handle change to auto-RT algorithm - update and advertise local routes.
4350 */
4351void bgp_evpn_handle_autort_change(struct bgp *bgp)
4352{
4353 hash_iterate(bgp->vnihash,
4354 (void (*)(struct hash_backet *,
4355 void*))update_autort_vni,
4356 bgp);
4357}
4358
90e60aa7 4359/*
4360 * Handle change to export RT - update and advertise local routes.
4361 */
d62a17ae 4362int bgp_evpn_handle_export_rt_change(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 4363{
d62a17ae 4364 return update_routes_for_vni(bgp, vpn);
90e60aa7 4365}
4366
996c9314 4367void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw)
676f83b9 4368{
4369 if (withdraw)
4370 delete_withdraw_vrf_routes(bgp_vrf);
4371 else
4372 update_advertise_vrf_routes(bgp_vrf);
4373}
4374
90e60aa7 4375/*
4376 * Handle change to RD. This is invoked twice by the change handler,
4377 * first before the RD has been changed and then after the RD has
4378 * been changed. The first invocation will result in local routes
4379 * of this VNI being deleted and withdrawn and the next will result
4380 * in the routes being re-advertised.
4381 */
d62a17ae 4382void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
4383 int withdraw)
90e60aa7 4384{
d62a17ae 4385 if (withdraw)
4386 delete_withdraw_vni_routes(bgp, vpn);
4387 else
4388 update_advertise_vni_routes(bgp, vpn);
90e60aa7 4389}
4390
4391/*
4392 * Install routes for this VNI. Invoked upon change to Import RT.
4393 */
d62a17ae 4394int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 4395{
d62a17ae 4396 return install_routes_for_vni(bgp, vpn);
90e60aa7 4397}
4398
4399/*
4400 * Uninstall all routes installed for this VNI. Invoked upon change
4401 * to Import RT.
4402 */
d62a17ae 4403int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 4404{
d62a17ae 4405 return uninstall_routes_for_vni(bgp, vpn);
90e60aa7 4406}
4407
b16031a2 4408/*
b57ba6d2 4409 * TODO: Hardcoded for a maximum of 2 VNIs right now
b16031a2 4410 */
d7c0a89a 4411char *bgp_evpn_label2str(mpls_label_t *label, uint32_t num_labels, char *buf,
996c9314 4412 int len)
b16031a2 4413{
b57ba6d2 4414 vni_t vni1, vni2;
b16031a2 4415
b57ba6d2
MK
4416 vni1 = label2vni(label);
4417 if (num_labels == 2) {
996c9314 4418 vni2 = label2vni(label + 1);
b57ba6d2
MK
4419 snprintf(buf, len, "%u/%u", vni1, vni2);
4420 } else
4421 snprintf(buf, len, "%u", vni1);
d62a17ae 4422 return buf;
b16031a2 4423}
4424
9c92b5f7
MK
4425/*
4426 * Function to convert evpn route to json format.
4427 * NOTE: We don't use prefix2str as the output here is a bit different.
4428 */
57f7feb6 4429void bgp_evpn_route2json(struct prefix_evpn *p, json_object *json)
9c92b5f7 4430{
b682f6de 4431 char buf1[ETHER_ADDR_STRLEN];
4432 char buf2[PREFIX2STR_BUFFER];
9c92b5f7 4433
b682f6de 4434 if (!json)
4435 return;
9c92b5f7 4436
dff8f48d 4437 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
b682f6de 4438 json_object_int_add(json, "routeType", p->prefix.route_type);
3714a385 4439 json_object_int_add(json, "ethTag",
4440 p->prefix.imet_addr.eth_tag);
57f7feb6 4441 json_object_int_add(json, "ipLen",
3714a385 4442 is_evpn_prefix_ipaddr_v4(p)
57f7feb6
MK
4443 ? IPV4_MAX_BITLEN
4444 : IPV6_MAX_BITLEN);
b682f6de 4445 json_object_string_add(json, "ip",
3714a385 4446 inet_ntoa(p->prefix.imet_addr.ip.ipaddr_v4));
57f7feb6 4447 } else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
3714a385 4448 if (is_evpn_prefix_ipaddr_none(p)) {
57f7feb6
MK
4449 json_object_int_add(json, "routeType",
4450 p->prefix.route_type);
3714a385 4451 json_object_int_add(json, "ethTag",
4452 p->prefix.macip_addr.eth_tag);
57f7feb6
MK
4453 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
4454 json_object_string_add(json, "mac",
3714a385 4455 prefix_mac2str(&p->prefix.macip_addr.mac,
57f7feb6
MK
4456 buf1,
4457 sizeof(buf1)));
dff8f48d 4458 } else {
d7c0a89a 4459 uint8_t family;
dff8f48d 4460
3714a385 4461 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET
57f7feb6 4462 : AF_INET6;
dff8f48d 4463
b682f6de 4464 json_object_int_add(json, "routeType",
57f7feb6 4465 p->prefix.route_type);
3714a385 4466 json_object_int_add(json, "ethTag",
4467 p->prefix.macip_addr.eth_tag);
57f7feb6 4468 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
b682f6de 4469 json_object_string_add(json, "mac",
3714a385 4470 prefix_mac2str(&p->prefix.macip_addr.mac,
57f7feb6
MK
4471 buf1,
4472 sizeof(buf1)));
b682f6de 4473 json_object_int_add(json, "ipLen",
3714a385 4474 is_evpn_prefix_ipaddr_v4(p)
57f7feb6
MK
4475 ? IPV4_MAX_BITLEN
4476 : IPV6_MAX_BITLEN);
4477 json_object_string_add(
4478 json, "ip",
3714a385 4479 inet_ntop(family,
4480 &p->prefix.macip_addr.ip.ip.addr,
4481 buf2,
57f7feb6 4482 PREFIX2STR_BUFFER));
dff8f48d
MK
4483 }
4484 } else {
4485 /* Currently, this is to cater to other AF_ETHERNET code. */
4486 }
9c92b5f7
MK
4487}
4488
520d5d76 4489/*
4490 * Function to convert evpn route to string.
4491 * NOTE: We don't use prefix2str as the output here is a bit different.
4492 */
d62a17ae 4493char *bgp_evpn_route2str(struct prefix_evpn *p, char *buf, int len)
4494{
4495 char buf1[ETHER_ADDR_STRLEN];
4496 char buf2[PREFIX2STR_BUFFER];
50f74cf1 4497 char buf3[ESI_STR_LEN];
d62a17ae 4498
4499 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
554cd77a 4500 snprintf(buf, len, "[%d]:[%d]:[%d]:[%s]", p->prefix.route_type,
3714a385 4501 p->prefix.imet_addr.eth_tag,
4502 is_evpn_prefix_ipaddr_v4(p) ? IPV4_MAX_BITLEN
d62a17ae 4503 : IPV6_MAX_BITLEN,
3714a385 4504 inet_ntoa(p->prefix.imet_addr.ip.ipaddr_v4));
d62a17ae 4505 } else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
3714a385 4506 if (is_evpn_prefix_ipaddr_none(p))
554cd77a
VB
4507 snprintf(buf, len, "[%d]:[%d]:[%d]:[%s]",
4508 p->prefix.route_type,
3714a385 4509 p->prefix.macip_addr.eth_tag,
554cd77a 4510 8 * ETH_ALEN,
3714a385 4511 prefix_mac2str(&p->prefix.macip_addr.mac, buf1,
d62a17ae 4512 sizeof(buf1)));
4513 else {
d7c0a89a 4514 uint8_t family;
d62a17ae 4515
3714a385 4516 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET
d62a17ae 4517 : AF_INET6;
554cd77a
VB
4518 snprintf(buf, len, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
4519 p->prefix.route_type,
3714a385 4520 p->prefix.macip_addr.eth_tag,
554cd77a 4521 8 * ETH_ALEN,
3714a385 4522 prefix_mac2str(&p->prefix.macip_addr.mac, buf1,
d62a17ae 4523 sizeof(buf1)),
4524 family == AF_INET ? IPV4_MAX_BITLEN
4525 : IPV6_MAX_BITLEN,
3714a385 4526 inet_ntop(family,
4527 &p->prefix.macip_addr.ip.ip.addr,
4528 buf2,
d62a17ae 4529 PREFIX2STR_BUFFER));
4530 }
342dd0c6 4531 } else if (p->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE) {
554cd77a
VB
4532 snprintf(buf, len, "[%d]:[%d]:[%d]:[%s]",
4533 p->prefix.route_type,
3714a385 4534 p->prefix.prefix_addr.eth_tag,
4535 p->prefix.prefix_addr.ip_prefix_length,
4536 is_evpn_prefix_ipaddr_v4(p)
4537 ? inet_ntoa(p->prefix.prefix_addr.ip.ipaddr_v4)
4538 : inet6_ntoa(p->prefix.prefix_addr.ip.ipaddr_v6));
50f74cf1 4539 } else if (p->prefix.route_type == BGP_EVPN_ES_ROUTE) {
4540 snprintf(buf, len, "[%d]:[%s]:[%d]:[%s]",
4541 p->prefix.route_type,
4542 esi_to_str(&p->prefix.es_addr.esi, buf3, sizeof(buf3)),
4543 is_evpn_prefix_ipaddr_v4(p) ? IPV4_MAX_BITLEN
4544 : IPV6_MAX_BITLEN,
4545 inet_ntoa(p->prefix.es_addr.ip.ipaddr_v4));
d62a17ae 4546 } else {
b03b8898 4547 /* For EVPN route types not supported yet. */
f9aa3e55
QY
4548 snprintf(buf, len, "(unsupported route type %d)",
4549 p->prefix.route_type);
d62a17ae 4550 }
4551
4552 return (buf);
520d5d76 4553}
4554
128ea8ab 4555/*
4556 * Encode EVPN prefix in Update (MP_REACH)
4557 */
d62a17ae 4558void bgp_evpn_encode_prefix(struct stream *s, struct prefix *p,
996c9314 4559 struct prefix_rd *prd, mpls_label_t *label,
d7c0a89a
QY
4560 uint32_t num_labels, struct attr *attr,
4561 int addpath_encode, uint32_t addpath_tx_id)
d62a17ae 4562{
4563 struct prefix_evpn *evp = (struct prefix_evpn *)p;
b57ba6d2 4564 int len, ipa_len = 0;
d62a17ae 4565
4566 if (addpath_encode)
4567 stream_putl(s, addpath_tx_id);
4568
4569 /* Route type */
4570 stream_putc(s, evp->prefix.route_type);
4571
4572 switch (evp->prefix.route_type) {
4573 case BGP_EVPN_MAC_IP_ROUTE:
3714a385 4574 if (is_evpn_prefix_ipaddr_v4(evp))
d62a17ae 4575 ipa_len = IPV4_MAX_BYTELEN;
3714a385 4576 else if (is_evpn_prefix_ipaddr_v6(evp))
d62a17ae 4577 ipa_len = IPV6_MAX_BYTELEN;
b57ba6d2
MK
4578 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4579 len = 8 + 10 + 4 + 1 + 6 + 1 + ipa_len + 3;
4580 if (ipa_len && num_labels > 1) /* There are 2 VNIs */
4581 len += 3;
4582 stream_putc(s, len);
996c9314 4583 stream_put(s, prd->val, 8); /* RD */
554cd77a
VB
4584 if (attr)
4585 stream_put(s, &attr->evpn_overlay.eth_s_id, ESI_LEN);
4586 else
4587 stream_put(s, 0, 10);
3714a385 4588 stream_putl(s, evp->prefix.macip_addr.eth_tag); /* Ethernet Tag ID */
28328ea9 4589 stream_putc(s, 8 * ETH_ALEN); /* Mac Addr Len - bits */
3714a385 4590 stream_put(s, evp->prefix.macip_addr.mac.octet, 6); /* Mac Addr */
4591 stream_putc(s, 8 * ipa_len); /* IP address Length */
4592 if (ipa_len) /* IP */
4593 stream_put(s, &evp->prefix.macip_addr.ip.ip.addr,
4594 ipa_len);
b57ba6d2
MK
4595 /* 1st label is the L2 VNI */
4596 stream_put(s, label, BGP_LABEL_BYTES);
4597 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4598 if (ipa_len && num_labels > 1)
996c9314 4599 stream_put(s, label + 1, BGP_LABEL_BYTES);
d62a17ae 4600 break;
4601
4602 case BGP_EVPN_IMET_ROUTE:
4603 stream_putc(s, 17); // TODO: length - assumes IPv4 address
4604 stream_put(s, prd->val, 8); /* RD */
3714a385 4605 stream_putl(s, evp->prefix.imet_addr.eth_tag); /* Ethernet Tag ID */
d62a17ae 4606 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
4607 /* Originating Router's IP Addr */
3714a385 4608 stream_put_in_addr(s, &evp->prefix.imet_addr.ip.ipaddr_v4);
d62a17ae 4609 break;
4610
50f74cf1 4611 case BGP_EVPN_ES_ROUTE:
4612 stream_putc(s, 23); /* TODO: length: assumes ipv4 VTEP */
4613 stream_put(s, prd->val, 8); /* RD */
4614 stream_put(s, evp->prefix.es_addr.esi.val, 10); /* ESI */
4615 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
2bb9eff4
DS
4616 /* VTEP IP */
4617 stream_put_in_addr(s, &evp->prefix.es_addr.ip.ipaddr_v4);
50f74cf1 4618 break;
4619
d62a17ae 4620 case BGP_EVPN_IP_PREFIX_ROUTE:
4621 /* TODO: AddPath support. */
b57ba6d2 4622 evpn_mpattr_encode_type5(s, p, prd, label, num_labels, attr);
d62a17ae 4623 break;
4624
4625 default:
4626 break;
4627 }
4628}
4629
4630int bgp_nlri_parse_evpn(struct peer *peer, struct attr *attr,
4631 struct bgp_nlri *packet, int withdraw)
4632{
d7c0a89a
QY
4633 uint8_t *pnt;
4634 uint8_t *lim;
d62a17ae 4635 afi_t afi;
4636 safi_t safi;
d7c0a89a 4637 uint32_t addpath_id;
d62a17ae 4638 int addpath_encoded;
4639 int psize = 0;
d7c0a89a 4640 uint8_t rtype;
d62a17ae 4641 struct prefix p;
4642
d62a17ae 4643 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4644 pnt = packet->nlri;
4645 lim = pnt + packet->length;
4646 afi = packet->afi;
4647 safi = packet->safi;
4648 addpath_id = 0;
4649
4650 addpath_encoded =
4651 (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV)
4652 && CHECK_FLAG(peer->af_cap[afi][safi],
4653 PEER_CAP_ADDPATH_AF_TX_RCV));
4654
4655 for (; pnt < lim; pnt += psize) {
4656 /* Clear prefix structure. */
4657 memset(&p, 0, sizeof(struct prefix));
4658
4659 /* Deal with path-id if AddPath is supported. */
4660 if (addpath_encoded) {
4661 /* When packet overflow occurs return immediately. */
4662 if (pnt + BGP_ADDPATH_ID_LEN > lim)
4663 return -1;
4664
4665 addpath_id = ntohl(*((uint32_t *)pnt));
4666 pnt += BGP_ADDPATH_ID_LEN;
4667 }
4668
4669 /* All EVPN NLRI types start with type and length. */
4670 if (pnt + 2 > lim)
4671 return -1;
4672
4673 rtype = *pnt++;
3f54c705 4674 psize = *pnt++;
d62a17ae 4675
4676 /* When packet overflow occur return immediately. */
4677 if (pnt + psize > lim)
4678 return -1;
4679
4680 switch (rtype) {
4681 case BGP_EVPN_MAC_IP_ROUTE:
4682 if (process_type2_route(peer, afi, safi,
4683 withdraw ? NULL : attr, pnt,
4684 psize, addpath_id)) {
af4c2728 4685 flog_err(
14454c9f 4686 BGP_ERR_EVPN_FAIL,
d62a17ae 4687 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4688 peer->bgp->vrf_id, peer->host, psize);
4689 return -1;
4690 }
4691 break;
4692
4693 case BGP_EVPN_IMET_ROUTE:
4694 if (process_type3_route(peer, afi, safi,
4695 withdraw ? NULL : attr, pnt,
4696 psize, addpath_id)) {
af4c2728 4697 flog_err(
14454c9f 4698 BGP_ERR_PKT_PROCESS,
d62a17ae 4699 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4700 peer->bgp->vrf_id, peer->host, psize);
4701 return -1;
4702 }
4703 break;
4704
50f74cf1 4705 case BGP_EVPN_ES_ROUTE:
4706 if (process_type4_route(peer, afi, safi,
4707 withdraw ? NULL : attr, pnt,
4708 psize, addpath_id)) {
af4c2728 4709 flog_err(
14454c9f 4710 BGP_ERR_PKT_PROCESS,
50f74cf1 4711 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4712 peer->bgp->vrf_id, peer->host, psize);
4713 return -1;
4714 }
4715 break;
4716
d62a17ae 4717 case BGP_EVPN_IP_PREFIX_ROUTE:
4718 if (process_type5_route(peer, afi, safi, attr, pnt,
4719 psize, addpath_id, withdraw)) {
af4c2728 4720 flog_err(
14454c9f 4721 BGP_ERR_PKT_PROCESS,
d62a17ae 4722 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4723 peer->bgp->vrf_id, peer->host, psize);
4724 return -1;
4725 }
4726 break;
4727
4728 default:
4729 break;
4730 }
4731 }
4732
4733 /* Packet length consistency check. */
4734 if (pnt != lim)
4735 return -1;
4736
4737 return 0;
128ea8ab 4738}
4739
10ebe1ab
MK
4740/*
4741 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4742 * The mapping will be used during route processing.
4743 * bgp_def: default bgp instance
4744 * bgp_vrf: specific bgp vrf instance on which RT is configured
4745 */
4746void bgp_evpn_map_vrf_to_its_rts(struct bgp *bgp_vrf)
4747{
4748 int i = 0;
4749 struct ecommunity_val *eval = NULL;
4750 struct listnode *node = NULL, *nnode = NULL;
4751 struct ecommunity *ecom = NULL;
4752
4753 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
4754 for (i = 0; i < ecom->size; i++) {
4755 eval = (struct ecommunity_val *)(ecom->val
4756 + (i
4757 * ECOMMUNITY_SIZE));
4758 map_vrf_to_rt(bgp_vrf, eval);
4759 }
4760 }
4761}
4762
4763/*
4764 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
4765 */
4766void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *bgp_vrf)
4767{
4768 int i;
4769 struct ecommunity_val *eval;
4770 struct listnode *node, *nnode;
4771 struct ecommunity *ecom;
4772
4773 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
4774 for (i = 0; i < ecom->size; i++) {
4775 struct vrf_irt_node *irt;
4776 struct ecommunity_val eval_tmp;
4777
4778 eval = (struct ecommunity_val *)(ecom->val
4779 + (i
4780 * ECOMMUNITY_SIZE));
4781 /* If using "automatic" RT, we only care about the
4782 * local-admin sub-field.
4783 * This is to facilitate using VNI as the RT for EBGP
4784 * peering too.
4785 */
4786 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
4787 if (!CHECK_FLAG(bgp_vrf->vrf_flags,
4788 BGP_VRF_IMPORT_RT_CFGD))
4789 mask_ecom_global_admin(&eval_tmp, eval);
4790
4791 irt = lookup_vrf_import_rt(&eval_tmp);
4792 if (irt)
4793 unmap_vrf_from_rt(bgp_vrf, irt);
4794 }
4795 }
4796}
4797
4798
128ea8ab 4799/*
4800 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
4801 * The mapping will be used during route processing.
4802 */
d62a17ae 4803void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 4804{
d62a17ae 4805 int i;
4806 struct ecommunity_val *eval;
4807 struct listnode *node, *nnode;
4808 struct ecommunity *ecom;
128ea8ab 4809
d62a17ae 4810 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
4811 for (i = 0; i < ecom->size; i++) {
4812 eval = (struct ecommunity_val *)(ecom->val
4813 + (i
4814 * ECOMMUNITY_SIZE));
4815 map_vni_to_rt(bgp, vpn, eval);
4816 }
4817 }
128ea8ab 4818}
4819
4820/*
4821 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
4822 */
d62a17ae 4823void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 4824{
d62a17ae 4825 int i;
4826 struct ecommunity_val *eval;
4827 struct listnode *node, *nnode;
4828 struct ecommunity *ecom;
128ea8ab 4829
d62a17ae 4830 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
4831 for (i = 0; i < ecom->size; i++) {
4832 struct irt_node *irt;
4833 struct ecommunity_val eval_tmp;
128ea8ab 4834
d62a17ae 4835 eval = (struct ecommunity_val *)(ecom->val
4836 + (i
4837 * ECOMMUNITY_SIZE));
4838 /* If using "automatic" RT, we only care about the
4839 * local-admin sub-field.
4840 * This is to facilitate using VNI as the RT for EBGP
4841 * peering too.
4842 */
4843 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
4844 if (!is_import_rt_configured(vpn))
4845 mask_ecom_global_admin(&eval_tmp, eval);
128ea8ab 4846
d62a17ae 4847 irt = lookup_import_rt(bgp, &eval_tmp);
4848 if (irt)
4849 unmap_vni_from_rt(bgp, vpn, irt);
4850 }
4851 }
128ea8ab 4852}
4853
4854/*
4855 * Derive Import RT automatically for VNI and map VNI to RT.
4856 * The mapping will be used during route processing.
4857 */
d62a17ae 4858void bgp_evpn_derive_auto_rt_import(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 4859{
c581d8b0 4860 form_auto_rt(bgp, vpn->vni, vpn->import_rtl);
d62a17ae 4861 UNSET_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD);
128ea8ab 4862
d62a17ae 4863 /* Map RT to VNI */
4864 bgp_evpn_map_vni_to_its_rts(bgp, vpn);
128ea8ab 4865}
4866
4867/*
4868 * Derive Export RT automatically for VNI.
4869 */
d62a17ae 4870void bgp_evpn_derive_auto_rt_export(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 4871{
c581d8b0 4872 form_auto_rt(bgp, vpn->vni, vpn->export_rtl);
d62a17ae 4873 UNSET_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD);
128ea8ab 4874}
4875
676f83b9 4876/*
4877 * Derive RD automatically for VNI using passed information - it
4878 * is of the form RouterId:unique-id-for-vni.
4879 */
4880void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp)
4881{
92708db6 4882 form_auto_rd(bgp->router_id, bgp->vrf_rd_id, &bgp->vrf_prd);
676f83b9 4883}
4884
128ea8ab 4885/*
4886 * Derive RD automatically for VNI using passed information - it
4887 * is of the form RouterId:unique-id-for-vni.
4888 */
d62a17ae 4889void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 4890{
d62a17ae 4891 char buf[100];
128ea8ab 4892
d62a17ae 4893 vpn->prd.family = AF_UNSPEC;
4894 vpn->prd.prefixlen = 64;
4895 sprintf(buf, "%s:%hu", inet_ntoa(bgp->router_id), vpn->rd_id);
cbb65f5e 4896 (void)str2prefix_rd(buf, &vpn->prd);
d62a17ae 4897 UNSET_FLAG(vpn->flags, VNI_FLAG_RD_CFGD);
128ea8ab 4898}
4899
7df407ed
CS
4900/*
4901 * Lookup L3-VNI
4902 */
4903bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni)
4904{
4905 struct list *inst = bm->bgp;
4906 struct listnode *node;
4907 struct bgp *bgp_vrf;
4908
4909 for (ALL_LIST_ELEMENTS_RO(inst, node, bgp_vrf)) {
4910 if (bgp_vrf->l3vni == vni)
4911 return true;
4912 }
4913
4914 return false;
4915}
4916
128ea8ab 4917/*
4918 * Lookup VNI.
4919 */
d62a17ae 4920struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni)
128ea8ab 4921{
d62a17ae 4922 struct bgpevpn *vpn;
4923 struct bgpevpn tmp;
128ea8ab 4924
d62a17ae 4925 memset(&tmp, 0, sizeof(struct bgpevpn));
4926 tmp.vni = vni;
4927 vpn = hash_lookup(bgp->vnihash, &tmp);
4928 return vpn;
128ea8ab 4929}
4930
4931/*
4932 * Create a new vpn - invoked upon configuration or zebra notification.
4933 */
d62a17ae 4934struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
29c53922
MK
4935 struct in_addr originator_ip,
4936 vrf_id_t tenant_vrf_id)
128ea8ab 4937{
d62a17ae 4938 struct bgpevpn *vpn;
128ea8ab 4939
d62a17ae 4940 if (!bgp)
4941 return NULL;
128ea8ab 4942
d62a17ae 4943 vpn = XCALLOC(MTYPE_BGP_EVPN, sizeof(struct bgpevpn));
4944 if (!vpn)
4945 return NULL;
128ea8ab 4946
d62a17ae 4947 /* Set values - RD and RT set to defaults. */
4948 vpn->vni = vni;
4949 vpn->originator_ip = originator_ip;
29c53922 4950 vpn->tenant_vrf_id = tenant_vrf_id;
128ea8ab 4951
d62a17ae 4952 /* Initialize route-target import and export lists */
4953 vpn->import_rtl = list_new();
4954 vpn->import_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
987d8198 4955 vpn->import_rtl->del = evpn_xxport_delete_ecomm;
d62a17ae 4956 vpn->export_rtl = list_new();
4957 vpn->export_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
987d8198 4958 vpn->export_rtl->del = evpn_xxport_delete_ecomm;
e9eb5f63 4959 bf_assign_index(bm->rd_idspace, vpn->rd_id);
d62a17ae 4960 derive_rd_rt_for_vni(bgp, vpn);
128ea8ab 4961
d62a17ae 4962 /* Initialize EVPN route table. */
960035b2 4963 vpn->route_table = bgp_table_init(bgp, AFI_L2VPN, SAFI_EVPN);
128ea8ab 4964
d62a17ae 4965 /* Add to hash */
4966 if (!hash_get(bgp->vnihash, vpn, hash_alloc_intern)) {
4967 XFREE(MTYPE_BGP_EVPN, vpn);
4968 return NULL;
4969 }
6a8657d0
MK
4970
4971 /* add to l2vni list on corresponding vrf */
4972 bgpevpn_link_to_l3vni(vpn);
4973
d62a17ae 4974 QOBJ_REG(vpn, bgpevpn);
4975 return vpn;
128ea8ab 4976}
4977
4978/*
4979 * Free a given VPN - called in multiple scenarios such as zebra
4980 * notification, configuration being deleted, advertise-all-vni disabled etc.
4981 * This just frees appropriate memory, caller should have taken other
4982 * needed actions.
4983 */
d62a17ae 4984void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 4985{
6a8657d0 4986 bgpevpn_unlink_from_l3vni(vpn);
d62a17ae 4987 bgp_table_unlock(vpn->route_table);
4988 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
affe9e99
DS
4989 list_delete_and_null(&vpn->import_rtl);
4990 list_delete_and_null(&vpn->export_rtl);
e9eb5f63 4991 bf_release_index(bm->rd_idspace, vpn->rd_id);
d62a17ae 4992 hash_release(bgp->vnihash, vpn);
4993 QOBJ_UNREG(vpn);
4994 XFREE(MTYPE_BGP_EVPN, vpn);
128ea8ab 4995}
4996
4997/*
50f74cf1 4998 * Lookup local ES.
4999 */
5000struct evpnes *bgp_evpn_lookup_es(struct bgp *bgp, esi_t *esi)
5001{
5002 struct evpnes *es;
5003 struct evpnes tmp;
5004
5005 memset(&tmp, 0, sizeof(struct evpnes));
5006 memcpy(&tmp.esi, esi, sizeof(esi_t));
5007 es = hash_lookup(bgp->esihash, &tmp);
5008 return es;
5009}
5010
5011/*
5012 * Create a new local es - invoked upon zebra notification.
5013 */
5014struct evpnes *bgp_evpn_es_new(struct bgp *bgp,
5015 esi_t *esi,
5016 struct ipaddr *originator_ip)
5017{
5018 char buf[100];
5019 struct evpnes *es;
5020
5021 if (!bgp)
5022 return NULL;
5023
5024 es = XCALLOC(MTYPE_BGP_EVPN_ES, sizeof(struct evpnes));
5025 if (!es)
5026 return NULL;
5027
5028 /* set the ESI and originator_ip */
5029 memcpy(&es->esi, esi, sizeof(esi_t));
5030 memcpy(&es->originator_ip, originator_ip, sizeof(struct ipaddr));
5031
5032 /* Initialise the VTEP list */
5033 es->vtep_list = list_new();
5034 es->vtep_list->cmp = (int (*)(void *, void *))evpn_vtep_ip_cmp;
5035
5036 /* auto derive RD for this es */
5037 bf_assign_index(bm->rd_idspace, es->rd_id);
5038 es->prd.family = AF_UNSPEC;
5039 es->prd.prefixlen = 64;
5040 sprintf(buf, "%s:%hu", inet_ntoa(bgp->router_id), es->rd_id);
5041 (void)str2prefix_rd(buf, &es->prd);
5042
5043 /* Initialize the ES route table */
5044 es->route_table = bgp_table_init(bgp, AFI_L2VPN, SAFI_EVPN);
5045
5046 /* Add to hash */
5047 if (!hash_get(bgp->esihash, es, hash_alloc_intern)) {
5048 XFREE(MTYPE_BGP_EVPN_ES, es);
5049 return NULL;
5050 }
5051
5052 QOBJ_REG(es, evpnes);
5053 return es;
5054}
5055
5056/*
5057 * Free a given ES -
5058 * This just frees appropriate memory, caller should have taken other
5059 * needed actions.
5060 */
5061void bgp_evpn_es_free(struct bgp *bgp, struct evpnes *es)
5062{
5063 list_delete_and_null(&es->vtep_list);
5064 bgp_table_unlock(es->route_table);
5065 bf_release_index(bm->rd_idspace, es->rd_id);
5066 hash_release(bgp->esihash, es);
5067 QOBJ_UNREG(es);
5068 XFREE(MTYPE_BGP_EVPN_ES, es);
5069}
5070
5071/*
5072 * Import evpn route from global table to VNI/VRF/ESI.
128ea8ab 5073 */
d62a17ae 5074int bgp_evpn_import_route(struct bgp *bgp, afi_t afi, safi_t safi,
5075 struct prefix *p, struct bgp_info *ri)
128ea8ab 5076{
d62a17ae 5077 return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 1);
128ea8ab 5078}
5079
5080/*
50f74cf1 5081 * Unimport evpn route from VNI/VRF/ESI.
128ea8ab 5082 */
d62a17ae 5083int bgp_evpn_unimport_route(struct bgp *bgp, afi_t afi, safi_t safi,
5084 struct prefix *p, struct bgp_info *ri)
128ea8ab 5085{
d62a17ae 5086 return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 0);
128ea8ab 5087}
5088
db0e1937
MK
5089/* filter routes which have martian next hops */
5090int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp *bgp)
5091{
0291c246
MK
5092 afi_t afi;
5093 safi_t safi;
5094 struct bgp_node *rd_rn, *rn;
5095 struct bgp_table *table;
5096 struct bgp_info *ri;
db0e1937
MK
5097
5098 afi = AFI_L2VPN;
5099 safi = SAFI_EVPN;
5100
5101 /* Walk entire global routing table and evaluate routes which could be
5102 * imported into this VPN. Note that we cannot just look at the routes
5103 * for the VNI's RD -
5104 * remote routes applicable for this VNI could have any RD.
5105 */
5106 /* EVPN routes are a 2-level table. */
5107 for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
5108 rd_rn = bgp_route_next(rd_rn)) {
5109 table = (struct bgp_table *)(rd_rn->info);
5110 if (!table)
5111 continue;
5112
5113 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
5114
5115 for (ri = rn->info; ri; ri = ri->next) {
5116
5117 /* Consider "valid" remote routes applicable for
5118 * this VNI. */
5119 if (!(ri->type == ZEBRA_ROUTE_BGP
5120 && ri->sub_type == BGP_ROUTE_NORMAL))
5121 continue;
5122
60466a63 5123 if (bgp_nexthop_self(bgp, ri->attr->nexthop)) {
db0e1937
MK
5124
5125 char attr_str[BUFSIZ];
5126 char pbuf[PREFIX_STRLEN];
5127
5128 bgp_dump_attr(ri->attr, attr_str,
5129 BUFSIZ);
5130
5131 if (bgp_debug_update(ri->peer, &rn->p,
5132 NULL, 1))
5133 zlog_debug(
b682f6de 5134 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
db0e1937
MK
5135 bgp->vrf_id,
5136 prefix2str(
60466a63 5137 &rn->p, pbuf,
db0e1937
MK
5138 sizeof(pbuf)),
5139 attr_str);
5140
5141 bgp_evpn_unimport_route(bgp, afi, safi,
5142 &rn->p, ri);
5143
60466a63
QY
5144 bgp_rib_remove(rn, ri, ri->peer, afi,
5145 safi);
db0e1937 5146 }
db0e1937
MK
5147 }
5148 }
5149 }
5150
5151 return 0;
5152}
5153
128ea8ab 5154/*
5155 * Handle del of a local MACIP.
5156 */
d62a17ae 5157int bgp_evpn_local_macip_del(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
5158 struct ipaddr *ip)
128ea8ab 5159{
d62a17ae 5160 struct bgpevpn *vpn;
5161 struct prefix_evpn p;
128ea8ab 5162
d62a17ae 5163 /* Lookup VNI hash - should exist. */
5164 vpn = bgp_evpn_lookup_vni(bgp, vni);
5165 if (!vpn || !is_vni_live(vpn)) {
28642513
DS
5166 flog_warn(BGP_WARN_EVPN_VPN_VNI,
5167 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
d62a17ae 5168 bgp->vrf_id, vni, vpn ? "not live" : "not found");
5169 return -1;
5170 }
128ea8ab 5171
d62a17ae 5172 /* Remove EVPN type-2 route and schedule for processing. */
5173 build_evpn_type2_prefix(&p, mac, ip);
5174 delete_evpn_route(bgp, vpn, &p);
128ea8ab 5175
d62a17ae 5176 return 0;
128ea8ab 5177}
5178
5179/*
5180 * Handle add of a local MACIP.
5181 */
d62a17ae 5182int bgp_evpn_local_macip_add(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
f07e1c99 5183 struct ipaddr *ip, uint8_t flags, uint32_t seq)
128ea8ab 5184{
d62a17ae 5185 struct bgpevpn *vpn;
5186 struct prefix_evpn p;
128ea8ab 5187
d62a17ae 5188 /* Lookup VNI hash - should exist. */
5189 vpn = bgp_evpn_lookup_vni(bgp, vni);
5190 if (!vpn || !is_vni_live(vpn)) {
28642513
DS
5191 flog_warn(BGP_WARN_EVPN_VPN_VNI,
5192 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
d62a17ae 5193 bgp->vrf_id, vni, vpn ? "not live" : "not found");
5194 return -1;
5195 }
128ea8ab 5196
d62a17ae 5197 /* Create EVPN type-2 route and schedule for processing. */
5198 build_evpn_type2_prefix(&p, mac, ip);
f07e1c99 5199 if (update_evpn_route(bgp, vpn, &p, flags, seq)) {
d62a17ae 5200 char buf[ETHER_ADDR_STRLEN];
5201 char buf2[INET6_ADDRSTRLEN];
128ea8ab 5202
af4c2728 5203 flog_err(
14454c9f 5204 BGP_ERR_EVPN_ROUTE_CREATE,
ead40654 5205 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
1a98c087 5206 bgp->vrf_id, vpn->vni,
996c9314
LB
5207 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY)
5208 ? "sticky gateway"
5209 : "",
d62a17ae 5210 prefix_mac2str(mac, buf, sizeof(buf)),
996c9314 5211 ipaddr2str(ip, buf2, sizeof(buf2)), flags);
d62a17ae 5212 return -1;
5213 }
128ea8ab 5214
d62a17ae 5215 return 0;
128ea8ab 5216}
5217
6a8657d0
MK
5218static void link_l2vni_hash_to_l3vni(struct hash_backet *backet,
5219 struct bgp *bgp_vrf)
5220{
0a1a07cb 5221 struct bgpevpn *vpn = (struct bgpevpn *)backet->data;
6a8657d0
MK
5222 struct bgp *bgp_def = NULL;
5223
5224 bgp_def = bgp_get_default();
5225 assert(bgp_def);
5226
6a8657d0
MK
5227 if (vpn->tenant_vrf_id == bgp_vrf->vrf_id)
5228 bgpevpn_link_to_l3vni(vpn);
5229}
5230
996c9314
LB
5231int bgp_evpn_local_l3vni_add(vni_t l3vni, vrf_id_t vrf_id, struct ethaddr *rmac,
5232 struct in_addr originator_ip, int filter)
fe1dc5a3
MK
5233{
5234 struct bgp *bgp_vrf = NULL; /* bgp VRF instance */
5235 struct bgp *bgp_def = NULL; /* default bgp instance */
f1f8b53c
MK
5236 struct listnode *node = NULL;
5237 struct bgpevpn *vpn = NULL;
fe1dc5a3
MK
5238 as_t as = 0;
5239
d846168d 5240 /* get the default instance - required to get the AS number for VRF
523cafc4 5241 * auto-creatio
5242 */
fe1dc5a3
MK
5243 bgp_def = bgp_get_default();
5244 if (!bgp_def) {
af4c2728 5245 flog_err(
14454c9f 5246 BGP_ERR_NO_DFLT,
996c9314
LB
5247 "Cannot process L3VNI %u ADD - default BGP instance not yet created",
5248 l3vni);
fe1dc5a3
MK
5249 return -1;
5250 }
5251 as = bgp_def->as;
5252
5253 /* if the BGP vrf instance doesnt exist - create one */
0b5131c9 5254 bgp_vrf = bgp_lookup_by_name(vrf_id_to_name(vrf_id));
fe1dc5a3
MK
5255 if (!bgp_vrf) {
5256
5257 int ret = 0;
5258
5259 ret = bgp_get(&bgp_vrf, &as, vrf_id_to_name(vrf_id),
5260 BGP_INSTANCE_TYPE_VRF);
5261 switch (ret) {
5262 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET:
af4c2728 5263 flog_err(BGP_ERR_MULTI_INSTANCE,
14454c9f 5264 "'bgp multiple-instance' not present\n");
fe1dc5a3
MK
5265 return -1;
5266 case BGP_ERR_AS_MISMATCH:
af4c2728 5267 flog_err(BGP_ERR_EVPN_AS_MISMATCH,
14454c9f 5268 "BGP is already running; AS is %u\n", as);
fe1dc5a3
MK
5269 return -1;
5270 case BGP_ERR_INSTANCE_MISMATCH:
af4c2728 5271 flog_err(BGP_ERR_EVPN_INSTANCE_MISMATCH,
14454c9f 5272 "BGP instance name and AS number mismatch\n");
fe1dc5a3
MK
5273 return -1;
5274 }
5275
5276 /* mark as auto created */
5277 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO);
5278 }
5279
5280 /* associate with l3vni */
5281 bgp_vrf->l3vni = l3vni;
5282
5283 /* set the router mac - to be used in mac-ip routes for this vrf */
5284 memcpy(&bgp_vrf->rmac, rmac, sizeof(struct ethaddr));
5285
b67a60d2 5286 /* set the originator ip */
5287 bgp_vrf->originator_ip = originator_ip;
5288
c48d9f5f
MK
5289 /* set the right filter - are we using l3vni only for prefix routes? */
5290 if (filter)
5291 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
5292
c581d8b0
MK
5293 /* auto derive RD/RT */
5294 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
5295 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
5296 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD))
5297 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
676f83b9 5298 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
fe1dc5a3 5299
6a8657d0
MK
5300 /* link all corresponding l2vnis */
5301 hash_iterate(bgp_def->vnihash,
996c9314
LB
5302 (void (*)(struct hash_backet *,
5303 void *))link_l2vni_hash_to_l3vni,
6a8657d0
MK
5304 bgp_vrf);
5305
c48d9f5f
MK
5306 /* Only update all corresponding type-2 routes if we are advertising two
5307 * labels along with type-2 routes
5308 */
5309 if (!filter)
5310 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
5311 update_routes_for_vni(bgp_def, vpn);
fe1dc5a3 5312
06d2e8f3
MK
5313 /* advertise type-5 routes if needed */
5314 update_advertise_vrf_routes(bgp_vrf);
5315
5ba238b7
MK
5316 /* install all remote routes belonging to this l3vni into correspondng
5317 * vrf */
5318 install_routes_for_vrf(bgp_vrf);
fe1dc5a3
MK
5319
5320 return 0;
5321}
5322
996c9314 5323int bgp_evpn_local_l3vni_del(vni_t l3vni, vrf_id_t vrf_id)
fe1dc5a3
MK
5324{
5325 struct bgp *bgp_vrf = NULL; /* bgp vrf instance */
f1f8b53c
MK
5326 struct bgp *bgp_def = NULL; /* default bgp instance */
5327 struct listnode *node = NULL;
18abc1eb 5328 struct listnode *next = NULL;
f1f8b53c 5329 struct bgpevpn *vpn = NULL;
fe1dc5a3
MK
5330
5331 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
5332 if (!bgp_vrf) {
af4c2728 5333 flog_err(
14454c9f 5334 BGP_ERR_NO_DFLT,
996c9314
LB
5335 "Cannot process L3VNI %u Del - Could not find BGP instance",
5336 l3vni);
fe1dc5a3
MK
5337 return -1;
5338 }
5339
f1f8b53c
MK
5340 bgp_def = bgp_get_default();
5341 if (!bgp_def) {
af4c2728 5342 flog_err(
14454c9f 5343 BGP_ERR_NO_DFLT,
996c9314
LB
5344 "Cannot process L3VNI %u Del - Could not find default BGP instance",
5345 l3vni);
f1f8b53c
MK
5346 return -1;
5347 }
5348
d846168d
CS
5349 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5350 * bgp_delete would not remove/decrement bgp_info of the ip_prefix
5351 * routes. This will uninstalling the routes from zebra and decremnt the
5352 * bgp info count.
523cafc4 5353 */
d846168d 5354 uninstall_routes_for_vrf(bgp_vrf);
5ba238b7 5355
06d2e8f3
MK
5356 /* delete/withdraw all type-5 routes */
5357 delete_withdraw_vrf_routes(bgp_vrf);
5358
fe1dc5a3
MK
5359 /* remove the l3vni from vrf instance */
5360 bgp_vrf->l3vni = 0;
5361
5362 /* remove the Rmac from the BGP vrf */
5363 memset(&bgp_vrf->rmac, 0, sizeof(struct ethaddr));
5364
c581d8b0 5365 /* delete RD/RT */
1525e99f 5366 if (!list_isempty(bgp_vrf->vrf_import_rtl)) {
10ebe1ab 5367 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
5ba238b7 5368 list_delete_all_node(bgp_vrf->vrf_import_rtl);
23a06e11 5369 }
1525e99f 5370 if (!list_isempty(bgp_vrf->vrf_export_rtl)) {
5ba238b7 5371 list_delete_all_node(bgp_vrf->vrf_export_rtl);
23a06e11 5372 }
fe1dc5a3 5373
f1f8b53c 5374 /* update all corresponding local mac-ip routes */
c48d9f5f
MK
5375 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY)) {
5376 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) {
5377 UNSET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
5378 update_routes_for_vni(bgp_def, vpn);
5379 }
5380 }
fe1dc5a3 5381
18abc1eb 5382 /* If any L2VNIs point to this instance, unlink them. */
5383 for (ALL_LIST_ELEMENTS(bgp_vrf->l2vnis, node, next, vpn))
5384 bgpevpn_unlink_from_l3vni(vpn);
5385
fe1dc5a3
MK
5386 /* Delete the instance if it was autocreated */
5387 if (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO))
5388 bgp_delete(bgp_vrf);
5389
5390 return 0;
5391}
5392
128ea8ab 5393/*
5394 * Handle del of a local VNI.
5395 */
d62a17ae 5396int bgp_evpn_local_vni_del(struct bgp *bgp, vni_t vni)
128ea8ab 5397{
d62a17ae 5398 struct bgpevpn *vpn;
128ea8ab 5399
d62a17ae 5400 /* Locate VNI hash */
5401 vpn = bgp_evpn_lookup_vni(bgp, vni);
5402 if (!vpn) {
1e00627b 5403 if (bgp_debug_zebra(NULL))
28642513
DS
5404 flog_warn(BGP_WARN_EVPN_VPN_VNI,
5405 "%u: VNI hash entry for VNI %u not found at DEL",
5406 bgp->vrf_id, vni);
d62a17ae 5407 return 0;
5408 }
128ea8ab 5409
d62a17ae 5410 /* Remove all local EVPN routes and schedule for processing (to
5411 * withdraw from peers).
5412 */
5413 delete_routes_for_vni(bgp, vpn);
128ea8ab 5414
db0e1937
MK
5415 /*
5416 * tunnel is no longer active, del tunnel ip address from tip_hash
5417 */
5418 bgp_tip_del(bgp, &vpn->originator_ip);
5419
d62a17ae 5420 /* Clear "live" flag and see if hash needs to be freed. */
5421 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
5422 if (!is_vni_configured(vpn))
5423 bgp_evpn_free(bgp, vpn);
128ea8ab 5424
d62a17ae 5425 return 0;
128ea8ab 5426}
5427
5428/*
d1911c26 5429 * Handle add (or update) of a local VNI. The VNI changes we care
5430 * about are for the local-tunnel-ip and the (tenant) VRF.
128ea8ab 5431 */
d62a17ae 5432int bgp_evpn_local_vni_add(struct bgp *bgp, vni_t vni,
996c9314 5433 struct in_addr originator_ip, vrf_id_t tenant_vrf_id)
d62a17ae 5434{
5435 struct bgpevpn *vpn;
5436 struct prefix_evpn p;
5437
d62a17ae 5438 /* Lookup VNI. If present and no change, exit. */
5439 vpn = bgp_evpn_lookup_vni(bgp, vni);
ddd16ed5 5440 if (vpn) {
29c53922 5441
d1911c26 5442 if (is_vni_live(vpn)
5443 && IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip)
5444 && vpn->tenant_vrf_id == tenant_vrf_id)
5445 /* Probably some other param has changed that we don't
5446 * care about. */
5447 return 0;
5448
5449 /* Update tenant_vrf_id if it has changed. */
6a8657d0
MK
5450 if (vpn->tenant_vrf_id != tenant_vrf_id) {
5451 bgpevpn_unlink_from_l3vni(vpn);
29c53922 5452 vpn->tenant_vrf_id = tenant_vrf_id;
6a8657d0
MK
5453 bgpevpn_link_to_l3vni(vpn);
5454 }
29c53922 5455
d1911c26 5456 /* If tunnel endpoint IP has changed, update (and delete prior
5457 * type-3 route, if needed.)
5458 */
5459 if (!IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip))
5460 handle_tunnel_ip_change(bgp, vpn, originator_ip);
d62a17ae 5461
d1911c26 5462 /* Update all routes with new endpoint IP and/or export RT
5463 * for VRFs
5464 */
5465 if (is_vni_live(vpn))
5466 update_routes_for_vni(bgp, vpn);
d62a17ae 5467 }
5468
5469 /* Create or update as appropriate. */
5470 if (!vpn) {
29c53922 5471 vpn = bgp_evpn_new(bgp, vni, originator_ip, tenant_vrf_id);
d62a17ae 5472 if (!vpn) {
af4c2728 5473 flog_err(
14454c9f 5474 BGP_ERR_VNI,
d62a17ae 5475 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5476 bgp->vrf_id, vni);
5477 return -1;
5478 }
5479 }
5480
db0e1937 5481 /* if the VNI is live already, there is nothing more to do */
ddd16ed5
MK
5482 if (is_vni_live(vpn))
5483 return 0;
5484
d62a17ae 5485 /* Mark as "live" */
5486 SET_FLAG(vpn->flags, VNI_FLAG_LIVE);
5487
db0e1937
MK
5488 /* tunnel is now active, add tunnel-ip to db */
5489 bgp_tip_add(bgp, &originator_ip);
5490
5491 /* filter routes as nexthop database has changed */
5492 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
5493
d62a17ae 5494 /* Create EVPN type-3 route and schedule for processing. */
5495 build_evpn_type3_prefix(&p, vpn->originator_ip);
f07e1c99 5496 if (update_evpn_route(bgp, vpn, &p, 0, 0)) {
af4c2728 5497 flog_err(BGP_ERR_EVPN_ROUTE_CREATE,
14454c9f
DS
5498 "%u: Type3 route creation failure for VNI %u",
5499 bgp->vrf_id, vni);
d62a17ae 5500 return -1;
5501 }
5502
5503 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5504 * VNI,
5505 * install them.
5506 */
5507 install_routes_for_vni(bgp, vpn);
5508
d7d97010
MK
5509 /* If we are advertising gateway mac-ip
5510 It needs to be conveyed again to zebra */
5511 bgp_zebra_advertise_gw_macip(bgp, vpn->advertise_gw_macip, vpn->vni);
5512
d62a17ae 5513 return 0;
b18825eb 5514}
14c1a7bf 5515
50f74cf1 5516/*
5517 * bgp_evpn_local_es_del
5518 */
5519int bgp_evpn_local_es_del(struct bgp *bgp,
5520 esi_t *esi,
5521 struct ipaddr *originator_ip)
5522{
5523 char buf[ESI_STR_LEN];
5524 struct evpnes *es = NULL;
5525
5526 if (!bgp->esihash) {
af4c2728 5527 flog_err(BGP_ERR_ES_CREATE, "%u: ESI hash not yet created",
14454c9f 5528 bgp->vrf_id);
50f74cf1 5529 return -1;
5530 }
5531
5532 /* Lookup ESI hash - should exist. */
5533 es = bgp_evpn_lookup_es(bgp, esi);
5534 if (!es) {
28642513
DS
5535 flog_warn(BGP_WARN_EVPN_ESI,
5536 "%u: ESI hash entry for ESI %s at Local ES DEL",
5537 bgp->vrf_id, esi_to_str(esi, buf, sizeof(buf)));
50f74cf1 5538 return -1;
5539 }
5540
5541 /* Delete all local EVPN ES routes from ESI table
2bb9eff4 5542 * and schedule for processing (to withdraw from peers))
50f74cf1 5543 */
5544 delete_routes_for_es(bgp, es);
5545
5546 /* free the hash entry */
5547 bgp_evpn_es_free(bgp, es);
5548
5549 return 0;
5550}
5551
5552/*
5553 * bgp_evpn_local_es_add
5554 */
5555int bgp_evpn_local_es_add(struct bgp *bgp,
5556 esi_t *esi,
5557 struct ipaddr *originator_ip)
5558{
5559 char buf[ESI_STR_LEN];
5560 struct evpnes *es = NULL;
5561 struct prefix_evpn p;
5562
5563 if (!bgp->esihash) {
af4c2728 5564 flog_err(BGP_ERR_ES_CREATE, "%u: ESI hash not yet created",
14454c9f 5565 bgp->vrf_id);
50f74cf1 5566 return -1;
5567 }
5568
5569 /* create the new es */
2bb9eff4 5570 es = bgp_evpn_lookup_es(bgp, esi);
50f74cf1 5571 if (!es) {
5572 es = bgp_evpn_es_new(bgp, esi, originator_ip);
5573 if (!es) {
af4c2728 5574 flog_err(
14454c9f 5575 BGP_ERR_ES_CREATE,
50f74cf1 5576 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
5577 bgp->vrf_id, esi_to_str(esi, buf, sizeof(buf)));
5578 return -1;
5579 }
5580 }
5581 UNSET_FLAG(es->flags, EVPNES_REMOTE);
5582 SET_FLAG(es->flags, EVPNES_LOCAL);
5583
5584 build_evpn_type4_prefix(&p, esi, originator_ip->ipaddr_v4);
5585 if (update_evpn_type4_route(bgp, es, &p)) {
af4c2728 5586 flog_err(BGP_ERR_EVPN_ROUTE_CREATE,
14454c9f
DS
5587 "%u: Type4 route creation failure for ESI %s",
5588 bgp->vrf_id, esi_to_str(esi, buf, sizeof(buf)));
50f74cf1 5589 return -1;
5590 }
5591
5592 /* import all remote ES routes in th ES table */
5593 install_routes_for_es(bgp, es);
5594
5595 return 0;
5596}
5597
7724c0a1 5598/*
5599 * Cleanup EVPN information on disable - Need to delete and withdraw
5600 * EVPN routes from peers.
5601 */
d62a17ae 5602void bgp_evpn_cleanup_on_disable(struct bgp *bgp)
7724c0a1 5603{
9d303b37
DL
5604 hash_iterate(bgp->vnihash, (void (*)(struct hash_backet *,
5605 void *))cleanup_vni_on_disable,
5606 bgp);
7724c0a1 5607}
5608
14c1a7bf 5609/*
5610 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5611 * BGP instance (default) is being freed.
5612 */
d62a17ae 5613void bgp_evpn_cleanup(struct bgp *bgp)
14c1a7bf 5614{
1525e99f
DS
5615 hash_iterate(bgp->vnihash,
5616 (void (*)(struct hash_backet *, void *))free_vni_entry,
5617 bgp);
5618
5619 hash_free(bgp->import_rt_hash);
d62a17ae 5620 bgp->import_rt_hash = NULL;
1525e99f
DS
5621
5622 hash_free(bgp->vrf_import_rt_hash);
10ebe1ab 5623 bgp->vrf_import_rt_hash = NULL;
1525e99f
DS
5624
5625 hash_free(bgp->vnihash);
d62a17ae 5626 bgp->vnihash = NULL;
50f74cf1 5627 if (bgp->esihash)
5628 hash_free(bgp->esihash);
5629 bgp->esihash = NULL;
1525e99f
DS
5630
5631 list_delete_and_null(&bgp->vrf_import_rtl);
5632 list_delete_and_null(&bgp->vrf_export_rtl);
5633 list_delete_and_null(&bgp->l2vnis);
14c1a7bf 5634}
5635
5636/*
5637 * Initialization for EVPN
5638 * Create
5639 * VNI hash table
5640 * hash for RT to VNI
14c1a7bf 5641 */
d62a17ae 5642void bgp_evpn_init(struct bgp *bgp)
5643{
5644 bgp->vnihash =
5645 hash_create(vni_hash_key_make, vni_hash_cmp, "BGP VNI Hash");
50f74cf1 5646 bgp->esihash =
5647 hash_create(esi_hash_keymake, esi_cmp,
5648 "BGP EVPN Local ESI Hash");
d62a17ae 5649 bgp->import_rt_hash =
5650 hash_create(import_rt_hash_key_make, import_rt_hash_cmp,
5651 "BGP Import RT Hash");
10ebe1ab
MK
5652 bgp->vrf_import_rt_hash =
5653 hash_create(vrf_import_rt_hash_key_make, vrf_import_rt_hash_cmp,
5654 "BGP VRF Import RT Hash");
c581d8b0
MK
5655 bgp->vrf_import_rtl = list_new();
5656 bgp->vrf_import_rtl->cmp =
5657 (int (*)(void *, void *))evpn_route_target_cmp;
987d8198 5658 bgp->vrf_import_rtl->del = evpn_xxport_delete_ecomm;
c581d8b0
MK
5659 bgp->vrf_export_rtl = list_new();
5660 bgp->vrf_export_rtl->cmp =
5661 (int (*)(void *, void *))evpn_route_target_cmp;
987d8198 5662 bgp->vrf_export_rtl->del = evpn_xxport_delete_ecomm;
6a8657d0 5663 bgp->l2vnis = list_new();
996c9314 5664 bgp->l2vnis->cmp = (int (*)(void *, void *))vni_hash_cmp;
14c1a7bf 5665}
10ebe1ab
MK
5666
5667void bgp_evpn_vrf_delete(struct bgp *bgp_vrf)
5668{
5669 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
5670}