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