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