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