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