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