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