]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_evpn.c
*: conform with COMMUNITY.md formatting rules, via 'make indent'
[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"
32#include "bitfield.h"
128ea8ab 33#include "zclient.h"
7ef5a232
PG
34
35#include "bgpd/bgp_attr_evpn.h"
36#include "bgpd/bgpd.h"
37#include "bgpd/bgp_table.h"
38#include "bgpd/bgp_route.h"
39#include "bgpd/bgp_attr.h"
40#include "bgpd/bgp_mplsvpn.h"
9bedbb1e 41#include "bgpd/bgp_label.h"
7ef5a232 42#include "bgpd/bgp_evpn.h"
14c1a7bf 43#include "bgpd/bgp_evpn_private.h"
44#include "bgpd/bgp_ecommunity.h"
128ea8ab 45#include "bgpd/bgp_encap_types.h"
46#include "bgpd/bgp_debug.h"
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)
57
58
59/*
60 * Static function declarations
61 */
d62a17ae 62static void delete_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
63 afi_t afi, safi_t safi, struct bgp_node *rn,
64 struct bgp_info **ri);
65static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn);
14c1a7bf 66
67/*
68 * Private functions.
69 */
70
71/*
72 * Make vni hash key.
73 */
d62a17ae 74static unsigned int vni_hash_key_make(void *p)
14c1a7bf 75{
d62a17ae 76 struct bgpevpn *vpn = p;
77 return (jhash_1word(vpn->vni, 0));
14c1a7bf 78}
79
80/*
81 * Comparison function for vni hash
82 */
d62a17ae 83static int vni_hash_cmp(const void *p1, const void *p2)
14c1a7bf 84{
d62a17ae 85 const struct bgpevpn *vpn1 = p1;
86 const struct bgpevpn *vpn2 = p2;
14c1a7bf 87
d62a17ae 88 if (!vpn1 && !vpn2)
89 return 1;
90 if (!vpn1 || !vpn2)
91 return 0;
92 return (vpn1->vni == vpn2->vni);
14c1a7bf 93}
94
10ebe1ab
MK
95/*
96 * Make vrf import route target hash key.
97 */
98static unsigned int vrf_import_rt_hash_key_make(void *p)
99{
100 struct vrf_irt_node *irt = p;
101 char *pnt = irt->rt.val;
5a1b3fb5
DS
102
103 return jhash(pnt, 8, 0x5abc1234);
10ebe1ab
MK
104}
105
106/*
107 * Comparison function for vrf import rt hash
108 */
109static int vrf_import_rt_hash_cmp(const void *p1, const void *p2)
110{
111 const struct vrf_irt_node *irt1 = p1;
112 const struct vrf_irt_node *irt2 = p2;
113
114 if (irt1 == NULL && irt2 == NULL)
115 return 1;
116
117 if (irt1 == NULL || irt2 == NULL)
118 return 0;
119
120 return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0);
121}
122
123/*
124 * Create a new vrf import_rt in default instance
125 */
126static struct vrf_irt_node *vrf_import_rt_new(struct ecommunity_val *rt)
127{
128 struct bgp *bgp_def = NULL;
129 struct vrf_irt_node *irt;
130
131 bgp_def = bgp_get_default();
132 if (!bgp_def) {
133 zlog_err("vrf import rt new - def instance not created yet");
134 return NULL;
135 }
136
137 irt = XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT,
138 sizeof(struct vrf_irt_node));
139 if (!irt)
140 return NULL;
141
142 irt->rt = *rt;
143 irt->vrfs = list_new();
144
145 /* Add to hash */
146 if (!hash_get(bgp_def->vrf_import_rt_hash, irt, hash_alloc_intern)) {
147 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT, irt);
148 return NULL;
149 }
150
151 return irt;
152}
153
154/*
155 * Free the vrf import rt node
156 */
157static void vrf_import_rt_free(struct vrf_irt_node *irt)
158{
159 struct bgp *bgp_def = NULL;
160
161 bgp_def = bgp_get_default();
162 if (!bgp_def) {
163 zlog_err("vrf import rt free - def instance not created yet");
164 return;
165 }
166
167 hash_release(bgp_def->vrf_import_rt_hash, irt);
168 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT, irt);
169}
170
171/*
172 * Function to lookup Import RT node - used to map a RT to set of
173 * VNIs importing routes with that RT.
174 */
175static struct vrf_irt_node *lookup_vrf_import_rt(struct ecommunity_val *rt)
176{
177 struct bgp *bgp_def = NULL;
178 struct vrf_irt_node *irt;
179 struct vrf_irt_node tmp;
180
181 bgp_def = bgp_get_default();
182 if (!bgp_def) {
183 zlog_err("vrf import rt lookup - def instance not created yet");
184 return NULL;
185 }
186
187 memset(&tmp, 0, sizeof(struct vrf_irt_node));
188 memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE);
189 irt = hash_lookup(bgp_def->vrf_import_rt_hash, &tmp);
190 return irt;
191}
192
193/*
194 * Is specified VRF present on the RT's list of "importing" VRFs?
195 */
996c9314 196static int is_vrf_present_in_irt_vrfs(struct list *vrfs, struct bgp *bgp_vrf)
10ebe1ab
MK
197{
198 struct listnode *node = NULL, *nnode = NULL;
199 struct bgp *tmp_bgp_vrf = NULL;
200
201 for (ALL_LIST_ELEMENTS(vrfs, node, nnode, tmp_bgp_vrf)) {
202 if (tmp_bgp_vrf == bgp_vrf)
203 return 1;
204 }
205 return 0;
206}
207
14c1a7bf 208/*
209 * Make import route target hash key.
210 */
d62a17ae 211static unsigned int import_rt_hash_key_make(void *p)
14c1a7bf 212{
d62a17ae 213 struct irt_node *irt = p;
214 char *pnt = irt->rt.val;
5a1b3fb5
DS
215
216 return jhash(pnt, 8, 0xdeadbeef);
14c1a7bf 217}
218
219/*
220 * Comparison function for import rt hash
221 */
d62a17ae 222static int import_rt_hash_cmp(const void *p1, const void *p2)
14c1a7bf 223{
d62a17ae 224 const struct irt_node *irt1 = p1;
225 const struct irt_node *irt2 = p2;
14c1a7bf 226
d62a17ae 227 if (irt1 == NULL && irt2 == NULL)
228 return 1;
14c1a7bf 229
d62a17ae 230 if (irt1 == NULL || irt2 == NULL)
231 return 0;
14c1a7bf 232
d62a17ae 233 return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0);
14c1a7bf 234}
235
7724c0a1 236/*
128ea8ab 237 * Create a new import_rt
238 */
d62a17ae 239static struct irt_node *import_rt_new(struct bgp *bgp,
240 struct ecommunity_val *rt)
128ea8ab 241{
d62a17ae 242 struct irt_node *irt;
128ea8ab 243
d62a17ae 244 if (!bgp)
245 return NULL;
128ea8ab 246
d62a17ae 247 irt = XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT, sizeof(struct irt_node));
248 if (!irt)
249 return NULL;
128ea8ab 250
d62a17ae 251 irt->rt = *rt;
252 irt->vnis = list_new();
128ea8ab 253
d62a17ae 254 /* Add to hash */
255 if (!hash_get(bgp->import_rt_hash, irt, hash_alloc_intern)) {
256 XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
257 return NULL;
258 }
128ea8ab 259
d62a17ae 260 return irt;
128ea8ab 261}
262
263/*
264 * Free the import rt node
7724c0a1 265 */
d62a17ae 266static void import_rt_free(struct bgp *bgp, struct irt_node *irt)
7724c0a1 267{
d62a17ae 268 hash_release(bgp->import_rt_hash, irt);
269 XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
7724c0a1 270}
271
14c1a7bf 272/*
128ea8ab 273 * Function to lookup Import RT node - used to map a RT to set of
274 * VNIs importing routes with that RT.
275 */
d62a17ae 276static struct irt_node *lookup_import_rt(struct bgp *bgp,
277 struct ecommunity_val *rt)
128ea8ab 278{
d62a17ae 279 struct irt_node *irt;
280 struct irt_node tmp;
128ea8ab 281
d62a17ae 282 memset(&tmp, 0, sizeof(struct irt_node));
283 memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE);
284 irt = hash_lookup(bgp->import_rt_hash, &tmp);
285 return irt;
128ea8ab 286}
287
288/*
289 * Is specified VNI present on the RT's list of "importing" VNIs?
290 */
d62a17ae 291static int is_vni_present_in_irt_vnis(struct list *vnis, struct bgpevpn *vpn)
128ea8ab 292{
d62a17ae 293 struct listnode *node, *nnode;
294 struct bgpevpn *tmp_vpn;
128ea8ab 295
d62a17ae 296 for (ALL_LIST_ELEMENTS(vnis, node, nnode, tmp_vpn)) {
297 if (tmp_vpn == vpn)
298 return 1;
299 }
128ea8ab 300
d62a17ae 301 return 0;
128ea8ab 302}
303
304/*
305 * Compare Route Targets.
306 */
d62a17ae 307static int evpn_route_target_cmp(struct ecommunity *ecom1,
308 struct ecommunity *ecom2)
128ea8ab 309{
d62a17ae 310 if (ecom1 && !ecom2)
311 return -1;
128ea8ab 312
d62a17ae 313 if (!ecom1 && ecom2)
314 return 1;
128ea8ab 315
d62a17ae 316 if (!ecom1 && !ecom2)
317 return 0;
128ea8ab 318
d62a17ae 319 if (ecom1->str && !ecom2->str)
320 return -1;
128ea8ab 321
d62a17ae 322 if (!ecom1->str && ecom2->str)
323 return 1;
128ea8ab 324
d62a17ae 325 if (!ecom1->str && !ecom2->str)
326 return 0;
128ea8ab 327
d62a17ae 328 return strcmp(ecom1->str, ecom2->str);
128ea8ab 329}
330
331/*
332 * Mask off global-admin field of specified extended community (RT),
333 * just retain the local-admin field.
334 */
d62a17ae 335static inline void mask_ecom_global_admin(struct ecommunity_val *dst,
336 struct ecommunity_val *src)
128ea8ab 337{
d62a17ae 338 u_char type;
128ea8ab 339
d62a17ae 340 type = src->val[0];
341 dst->val[0] = 0;
342 if (type == ECOMMUNITY_ENCODE_AS) {
343 dst->val[2] = dst->val[3] = 0;
344 } else if (type == ECOMMUNITY_ENCODE_AS4
345 || type == ECOMMUNITY_ENCODE_IP) {
346 dst->val[2] = dst->val[3] = 0;
347 dst->val[4] = dst->val[5] = 0;
348 }
128ea8ab 349}
350
10ebe1ab
MK
351/*
352 * Map one RT to specified VRF.
353 * bgp_vrf = BGP vrf instance
354 */
996c9314 355static void map_vrf_to_rt(struct bgp *bgp_vrf, struct ecommunity_val *eval)
10ebe1ab
MK
356{
357 struct vrf_irt_node *irt = NULL;
358 struct ecommunity_val eval_tmp;
359
360 /* If using "automatic" RT,
361 * we only care about the local-admin sub-field.
362 * This is to facilitate using L3VNI(VRF-VNI)
363 * as the RT for EBGP peering too.
364 */
365 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
996c9314 366 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
10ebe1ab
MK
367 mask_ecom_global_admin(&eval_tmp, eval);
368
369 irt = lookup_vrf_import_rt(&eval_tmp);
370 if (irt && irt->vrfs)
371 if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
372 /* Already mapped. */
373 return;
374
375 if (!irt) {
376 irt = vrf_import_rt_new(&eval_tmp);
377 assert(irt);
378 }
379
380 /* Add VRF to the list for this RT. */
381 listnode_add(irt->vrfs, bgp_vrf);
382}
383
384/*
385 * Unmap specified VRF from specified RT. If there are no other
386 * VRFs for this RT, then the RT hash is deleted.
387 * bgp_vrf: BGP VRF specific instance
388 */
996c9314 389static void unmap_vrf_from_rt(struct bgp *bgp_vrf, struct vrf_irt_node *irt)
10ebe1ab
MK
390{
391 /* Delete VRF from list for this RT. */
392 listnode_delete(irt->vrfs, bgp_vrf);
393 if (!listnode_head(irt->vrfs)) {
bb7a24ab 394 list_delete_and_null(&irt->vrfs);
10ebe1ab
MK
395 vrf_import_rt_free(irt);
396 }
397}
398
128ea8ab 399/*
400 * Map one RT to specified VNI.
14c1a7bf 401 */
d62a17ae 402static void map_vni_to_rt(struct bgp *bgp, struct bgpevpn *vpn,
403 struct ecommunity_val *eval)
128ea8ab 404{
d62a17ae 405 struct irt_node *irt;
406 struct ecommunity_val eval_tmp;
128ea8ab 407
d62a17ae 408 /* If using "automatic" RT, we only care about the local-admin
409 * sub-field.
410 * This is to facilitate using VNI as the RT for EBGP peering too.
411 */
412 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
413 if (!is_import_rt_configured(vpn))
414 mask_ecom_global_admin(&eval_tmp, eval);
128ea8ab 415
d62a17ae 416 irt = lookup_import_rt(bgp, &eval_tmp);
417 if (irt && irt->vnis)
418 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
419 /* Already mapped. */
420 return;
128ea8ab 421
d62a17ae 422 if (!irt) {
423 irt = import_rt_new(bgp, &eval_tmp);
424 assert(irt);
425 }
128ea8ab 426
d62a17ae 427 /* Add VNI to the hash list for this RT. */
428 listnode_add(irt->vnis, vpn);
128ea8ab 429}
430
431/*
432 * Unmap specified VNI from specified RT. If there are no other
433 * VNIs for this RT, then the RT hash is deleted.
434 */
d62a17ae 435static void unmap_vni_from_rt(struct bgp *bgp, struct bgpevpn *vpn,
436 struct irt_node *irt)
14c1a7bf 437{
d62a17ae 438 /* Delete VNI from hash list for this RT. */
439 listnode_delete(irt->vnis, vpn);
440 if (!listnode_head(irt->vnis)) {
acdf5e25 441 list_delete_and_null(&irt->vnis);
d62a17ae 442 import_rt_free(bgp, irt);
443 }
14c1a7bf 444}
445
128ea8ab 446/*
447 * Create RT extended community automatically from passed information:
448 * of the form AS:VNI.
449 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
450 * the need is to get a RT value that will be unique across different
451 * VNIs but the same across routers (in the same AS) for a particular
452 * VNI.
453 */
c581d8b0 454static void form_auto_rt(struct bgp *bgp, vni_t vni, struct list *rtl)
128ea8ab 455{
d62a17ae 456 struct ecommunity_val eval;
457 struct ecommunity *ecomadd;
128ea8ab 458
c581d8b0 459 encode_route_target_as((bgp->as & 0xFFFF), vni, &eval);
128ea8ab 460
d62a17ae 461 ecomadd = ecommunity_new();
462 ecommunity_add_val(ecomadd, &eval);
463 listnode_add_sort(rtl, ecomadd);
128ea8ab 464}
14c1a7bf 465
466/*
128ea8ab 467 * Derive RD and RT for a VNI automatically. Invoked at the time of
468 * creation of a VNI.
469 */
d62a17ae 470static void derive_rd_rt_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 471{
d62a17ae 472 bgp_evpn_derive_auto_rd(bgp, vpn);
473 bgp_evpn_derive_auto_rt_import(bgp, vpn);
474 bgp_evpn_derive_auto_rt_export(bgp, vpn);
128ea8ab 475}
476
477/*
478 * Add (update) or delete MACIP from zebra.
14c1a7bf 479 */
d62a17ae 480static int bgp_zebra_send_remote_macip(struct bgp *bgp, struct bgpevpn *vpn,
481 struct prefix_evpn *p,
482 struct in_addr remote_vtep_ip, int add,
ead40654 483 u_char flags)
d62a17ae 484{
485 struct stream *s;
486 int ipa_len;
487 char buf1[ETHER_ADDR_STRLEN];
488 char buf2[INET6_ADDRSTRLEN];
489 char buf3[INET6_ADDRSTRLEN];
490
491 /* Check socket. */
492 if (!zclient || zclient->sock < 0)
493 return 0;
494
495 /* Don't try to register if Zebra doesn't know of this instance. */
496 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
497 return 0;
498
499 s = zclient->obuf;
500 stream_reset(s);
501
996c9314
LB
502 zclient_create_header(
503 s, add ? ZEBRA_REMOTE_MACIP_ADD : ZEBRA_REMOTE_MACIP_DEL,
504 bgp->vrf_id);
d62a17ae 505 stream_putl(s, vpn->vni);
28328ea9 506 stream_put(s, &p->prefix.mac.octet, ETH_ALEN); /* Mac Addr */
d62a17ae 507 /* IP address length and IP address, if any. */
508 if (IS_EVPN_PREFIX_IPADDR_NONE(p))
509 stream_putl(s, 0);
510 else {
511 ipa_len = IS_EVPN_PREFIX_IPADDR_V4(p) ? IPV4_MAX_BYTELEN
512 : IPV6_MAX_BYTELEN;
513 stream_putl(s, ipa_len);
514 stream_put(s, &p->prefix.ip.ip.addr, ipa_len);
515 }
516 stream_put_in_addr(s, &remote_vtep_ip);
517
ead40654 518 /* TX flags - MAC sticky status and/or gateway mac */
d62a17ae 519 if (add)
ead40654 520 stream_putc(s, flags);
d62a17ae 521
522 stream_putw_at(s, 0, stream_get_endp(s));
523
524 if (bgp_debug_zebra(NULL))
996c9314
LB
525 zlog_debug(
526 "Tx %s MACIP, VNI %u MAC %s IP %s (flags: 0x%x) remote VTEP %s",
527 add ? "ADD" : "DEL", vpn->vni,
528 prefix_mac2str(&p->prefix.mac, buf1, sizeof(buf1)),
529 ipaddr2str(&p->prefix.ip, buf3, sizeof(buf3)), flags,
530 inet_ntop(AF_INET, &remote_vtep_ip, buf2,
531 sizeof(buf2)));
d62a17ae 532
533 return zclient_send_message(zclient);
7ef5a232 534}
b18825eb 535
128ea8ab 536/*
537 * Add (update) or delete remote VTEP from zebra.
538 */
d62a17ae 539static int bgp_zebra_send_remote_vtep(struct bgp *bgp, struct bgpevpn *vpn,
540 struct prefix_evpn *p, int add)
128ea8ab 541{
d62a17ae 542 struct stream *s;
128ea8ab 543
d62a17ae 544 /* Check socket. */
545 if (!zclient || zclient->sock < 0)
546 return 0;
128ea8ab 547
d62a17ae 548 /* Don't try to register if Zebra doesn't know of this instance. */
549 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
550 return 0;
128ea8ab 551
d62a17ae 552 s = zclient->obuf;
553 stream_reset(s);
128ea8ab 554
996c9314
LB
555 zclient_create_header(
556 s, add ? ZEBRA_REMOTE_VTEP_ADD : ZEBRA_REMOTE_VTEP_DEL,
557 bgp->vrf_id);
d62a17ae 558 stream_putl(s, vpn->vni);
559 if (IS_EVPN_PREFIX_IPADDR_V4(p))
560 stream_put_in_addr(s, &p->prefix.ip.ipaddr_v4);
561 else if (IS_EVPN_PREFIX_IPADDR_V6(p)) {
562 zlog_err(
563 "Bad remote IP when trying to %s remote VTEP for VNI %u",
564 add ? "ADD" : "DEL", vpn->vni);
565 return -1;
566 }
128ea8ab 567
d62a17ae 568 stream_putw_at(s, 0, stream_get_endp(s));
128ea8ab 569
d62a17ae 570 if (bgp_debug_zebra(NULL))
571 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
572 add ? "ADD" : "DEL", vpn->vni,
573 inet_ntoa(p->prefix.ip.ipaddr_v4));
128ea8ab 574
d62a17ae 575 return zclient_send_message(zclient);
128ea8ab 576}
577
342dd0c6 578/*
579 * Build extended communities for EVPN prefix route.
580 */
581static void build_evpn_type5_route_extcomm(struct bgp *bgp_vrf,
582 struct attr *attr)
583{
584 struct ecommunity ecom_encap;
585 struct ecommunity ecom_rmac;
586 struct ecommunity_val eval;
587 struct ecommunity_val eval_rmac;
588 bgp_encap_types tnl_type;
589 struct listnode *node, *nnode;
590 struct ecommunity *ecom;
591 struct list *vrf_export_rtl = NULL;
592
593 /* Encap */
594 tnl_type = BGP_ENCAP_TYPE_VXLAN;
595 memset(&ecom_encap, 0, sizeof(ecom_encap));
596 encode_encap_extcomm(tnl_type, &eval);
597 ecom_encap.size = 1;
598 ecom_encap.val = (u_int8_t *)eval.val;
599
600 /* Add Encap */
601 attr->ecommunity = ecommunity_dup(&ecom_encap);
602
603 /* Add the export RTs for L3VNI/VRF */
604 vrf_export_rtl = bgp_vrf->vrf_export_rtl;
605 if (vrf_export_rtl && !list_isempty(vrf_export_rtl)) {
606 for (ALL_LIST_ELEMENTS(vrf_export_rtl, node, nnode, ecom))
996c9314
LB
607 attr->ecommunity =
608 ecommunity_merge(attr->ecommunity, ecom);
342dd0c6 609 }
610
611 /* add the router mac extended community */
612 if (!is_zero_mac(&attr->rmac)) {
613 memset(&ecom_rmac, 0, sizeof(ecom_rmac));
614 encode_rmac_extcomm(&eval_rmac, &attr->rmac);
615 ecom_rmac.size = 1;
616 ecom_rmac.val = (uint8_t *)eval_rmac.val;
996c9314
LB
617 attr->ecommunity =
618 ecommunity_merge(attr->ecommunity, &ecom_rmac);
342dd0c6 619 }
620
621 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
622}
623
128ea8ab 624/*
625 * Build extended communities for EVPN route. RT and ENCAP are
626 * applicable to all routes.
7ec156a9
MK
627 * TODO: currently kernel doesnt support ipv6 routes with ipv4 nexthops.
628 * This means that we can't do symmetric routing for ipv6 hosts routes
629 * in the same way as ipv4 host routes.
630 * We wont attach l3-vni related RTs for ipv6 routes.
631 * For now, We will only adevrtise ipv4 host routes
632 * with L3-VNI related ext-comm.
128ea8ab 633 */
7ec156a9
MK
634static void build_evpn_route_extcomm(struct bgpevpn *vpn, struct attr *attr,
635 afi_t afi)
128ea8ab 636{
d62a17ae 637 struct ecommunity ecom_encap;
638 struct ecommunity ecom_sticky;
ead40654 639 struct ecommunity ecom_default_gw;
bc59a672 640 struct ecommunity ecom_rmac;
d62a17ae 641 struct ecommunity_val eval;
642 struct ecommunity_val eval_sticky;
ead40654 643 struct ecommunity_val eval_default_gw;
bc59a672 644 struct ecommunity_val eval_rmac;
d62a17ae 645 bgp_encap_types tnl_type;
646 struct listnode *node, *nnode;
647 struct ecommunity *ecom;
648 u_int32_t seqnum;
7a3e76f1 649 struct list *vrf_export_rtl = NULL;
128ea8ab 650
d62a17ae 651 /* Encap */
652 tnl_type = BGP_ENCAP_TYPE_VXLAN;
653 memset(&ecom_encap, 0, sizeof(ecom_encap));
654 encode_encap_extcomm(tnl_type, &eval);
655 ecom_encap.size = 1;
656 ecom_encap.val = (u_int8_t *)eval.val;
128ea8ab 657
d62a17ae 658 /* Add Encap */
659 attr->ecommunity = ecommunity_dup(&ecom_encap);
128ea8ab 660
7a3e76f1 661 /* Add the export RTs for L2VNI */
d62a17ae 662 for (ALL_LIST_ELEMENTS(vpn->export_rtl, node, nnode, ecom))
663 attr->ecommunity = ecommunity_merge(attr->ecommunity, ecom);
128ea8ab 664
c48d9f5f
MK
665 /*
666 * only attach l3-vni export rts for ipv4 address family and if we are
667 * advertising both the labels in type-2 routes
523cafc4 668 */
c48d9f5f 669 if (afi == AFI_IP && CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)) {
7ec156a9
MK
670 vrf_export_rtl = bgpevpn_get_vrf_export_rtl(vpn);
671 if (vrf_export_rtl && !list_isempty(vrf_export_rtl)) {
672 for (ALL_LIST_ELEMENTS(vrf_export_rtl, node, nnode,
673 ecom))
996c9314
LB
674 attr->ecommunity = ecommunity_merge(
675 attr->ecommunity, ecom);
7ec156a9 676 }
f1f8b53c 677 }
7a3e76f1 678
d62a17ae 679 if (attr->sticky) {
680 seqnum = 0;
681 memset(&ecom_sticky, 0, sizeof(ecom_sticky));
682 encode_mac_mobility_extcomm(1, seqnum, &eval_sticky);
683 ecom_sticky.size = 1;
684 ecom_sticky.val = (u_int8_t *)eval_sticky.val;
685 attr->ecommunity =
686 ecommunity_merge(attr->ecommunity, &ecom_sticky);
687 }
c85c03c7 688
c48d9f5f
MK
689 /*
690 * only attach l3-vni rmac for ipv4 address family and if we are
691 * advertising both the labels in type-2 routes
692 */
996c9314
LB
693 if (afi == AFI_IP && !is_zero_mac(&attr->rmac)
694 && CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)) {
bc59a672
MK
695 memset(&ecom_rmac, 0, sizeof(ecom_rmac));
696 encode_rmac_extcomm(&eval_rmac, &attr->rmac);
697 ecom_rmac.size = 1;
698 ecom_rmac.val = (uint8_t *)eval_rmac.val;
996c9314
LB
699 attr->ecommunity =
700 ecommunity_merge(attr->ecommunity, &ecom_rmac);
bc59a672
MK
701 }
702
ead40654
MK
703 if (attr->default_gw) {
704 memset(&ecom_default_gw, 0, sizeof(ecom_default_gw));
705 encode_default_gw_extcomm(&eval_default_gw);
706 ecom_default_gw.size = 1;
707 ecom_default_gw.val = (uint8_t *)eval_default_gw.val;
996c9314
LB
708 attr->ecommunity =
709 ecommunity_merge(attr->ecommunity, &ecom_default_gw);
ead40654
MK
710 }
711
d62a17ae 712 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
128ea8ab 713}
714
715/*
716 * Add MAC mobility extended community to attribute.
717 */
d62a17ae 718static void add_mac_mobility_to_attr(u_int32_t seq_num, struct attr *attr)
719{
720 struct ecommunity ecom_tmp;
721 struct ecommunity_val eval;
421bb26a 722 u_int8_t *ecom_val_ptr;
d62a17ae 723 int i;
724 u_int8_t *pnt;
725 int type = 0;
726 int sub_type = 0;
727
728 /* Build MM */
729 encode_mac_mobility_extcomm(0, seq_num, &eval);
730
731 /* Find current MM ecommunity */
421bb26a 732 ecom_val_ptr = NULL;
d62a17ae 733
734 if (attr->ecommunity) {
735 for (i = 0; i < attr->ecommunity->size; i++) {
736 pnt = attr->ecommunity->val + (i * 8);
737 type = *pnt++;
738 sub_type = *pnt++;
739
740 if (type == ECOMMUNITY_ENCODE_EVPN
741 && sub_type
742 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY) {
421bb26a
MK
743 ecom_val_ptr =
744 (u_int8_t *)(attr->ecommunity->val
745 + (i * 8));
d62a17ae 746 break;
747 }
748 }
749 }
750
751 /* Update the existing MM ecommunity */
421bb26a
MK
752 if (ecom_val_ptr) {
753 memcpy(ecom_val_ptr, eval.val, sizeof(char) * ECOMMUNITY_SIZE);
d62a17ae 754 }
755 /* Add MM to existing */
756 else {
757 memset(&ecom_tmp, 0, sizeof(ecom_tmp));
758 ecom_tmp.size = 1;
759 ecom_tmp.val = (u_int8_t *)eval.val;
760
761 attr->ecommunity =
762 ecommunity_merge(attr->ecommunity, &ecom_tmp);
763 }
128ea8ab 764}
765
766/* Install EVPN route into zebra. */
d62a17ae 767static int evpn_zebra_install(struct bgp *bgp, struct bgpevpn *vpn,
768 struct prefix_evpn *p,
ead40654 769 struct in_addr remote_vtep_ip, u_char flags)
128ea8ab 770{
d62a17ae 771 int ret;
128ea8ab 772
d62a17ae 773 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
774 ret = bgp_zebra_send_remote_macip(bgp, vpn, p, remote_vtep_ip,
ead40654 775 1, flags);
d62a17ae 776 else
777 ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, 1);
128ea8ab 778
d62a17ae 779 return ret;
128ea8ab 780}
781
782/* Uninstall EVPN route from zebra. */
d62a17ae 783static int evpn_zebra_uninstall(struct bgp *bgp, struct bgpevpn *vpn,
784 struct prefix_evpn *p,
785 struct in_addr remote_vtep_ip)
128ea8ab 786{
d62a17ae 787 int ret;
128ea8ab 788
d62a17ae 789 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
790 ret = bgp_zebra_send_remote_macip(bgp, vpn, p, remote_vtep_ip,
791 0, 0);
792 else
793 ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, 0);
128ea8ab 794
d62a17ae 795 return ret;
128ea8ab 796}
797
798/*
799 * Due to MAC mobility, the prior "local" best route has been supplanted
800 * by a "remote" best route. The prior route has to be deleted and withdrawn
801 * from peers.
802 */
d62a17ae 803static void evpn_delete_old_local_route(struct bgp *bgp, struct bgpevpn *vpn,
804 struct bgp_node *rn,
805 struct bgp_info *old_local)
128ea8ab 806{
d62a17ae 807 struct bgp_node *global_rn;
808 struct bgp_info *ri;
809 afi_t afi = AFI_L2VPN;
810 safi_t safi = SAFI_EVPN;
128ea8ab 811
d62a17ae 812 /* Locate route node in the global EVPN routing table. Note that
813 * this table is a 2-level tree (RD-level + Prefix-level) similar to
814 * L3VPN routes.
815 */
816 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
817 (struct prefix *)&rn->p, &vpn->prd);
818 if (global_rn) {
819 /* Delete route entry in the global EVPN table. */
820 delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);
128ea8ab 821
d62a17ae 822 /* Schedule for processing - withdraws to peers happen from
823 * this table.
824 */
825 if (ri)
826 bgp_process(bgp, global_rn, afi, safi);
827 bgp_unlock_node(global_rn);
828 }
128ea8ab 829
d62a17ae 830 /* Delete route entry in the VNI route table, caller to remove. */
831 bgp_info_delete(rn, old_local);
128ea8ab 832}
833
834/*
835 * Calculate the best path for an EVPN route. Install/update best path in zebra,
836 * if appropriate.
837 */
d62a17ae 838static int evpn_route_select_install(struct bgp *bgp, struct bgpevpn *vpn,
839 struct bgp_node *rn)
840{
841 struct bgp_info *old_select, *new_select;
842 struct bgp_info_pair old_and_new;
843 afi_t afi = AFI_L2VPN;
844 safi_t safi = SAFI_EVPN;
845 int ret = 0;
996c9314 846 u_char flags = 0;
d62a17ae 847
848 /* Compute the best path. */
849 bgp_best_selection(bgp, rn, &bgp->maxpaths[afi][safi], &old_and_new,
850 afi, safi);
851 old_select = old_and_new.old;
852 new_select = old_and_new.new;
853
854 /* If the best path hasn't changed - see if there is still something to
855 * update
856 * to zebra RIB.
857 */
858 if (old_select && old_select == new_select
859 && old_select->type == ZEBRA_ROUTE_BGP
860 && old_select->sub_type == BGP_ROUTE_NORMAL
861 && !CHECK_FLAG(rn->flags, BGP_NODE_USER_CLEAR)
862 && !CHECK_FLAG(old_select->flags, BGP_INFO_ATTR_CHANGED)
863 && !bgp->addpath_tx_used[afi][safi]) {
ead40654
MK
864 if (bgp_zebra_has_route_changed(rn, old_select)) {
865 if (old_select->attr->sticky)
866 SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
867 if (old_select->attr->default_gw)
868 SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
996c9314
LB
869 ret = evpn_zebra_install(
870 bgp, vpn, (struct prefix_evpn *)&rn->p,
871 old_select->attr->nexthop, flags);
ead40654 872 }
d62a17ae 873 UNSET_FLAG(old_select->flags, BGP_INFO_MULTIPATH_CHG);
874 bgp_zebra_clear_route_change_flags(rn);
875 return ret;
876 }
877
878 /* If the user did a "clear" this flag will be set */
879 UNSET_FLAG(rn->flags, BGP_NODE_USER_CLEAR);
880
881 /* bestpath has changed; update relevant fields and install or uninstall
882 * into the zebra RIB.
883 */
884 if (old_select || new_select)
885 bgp_bump_version(rn);
886
887 if (old_select)
888 bgp_info_unset_flag(rn, old_select, BGP_INFO_SELECTED);
889 if (new_select) {
890 bgp_info_set_flag(rn, new_select, BGP_INFO_SELECTED);
891 bgp_info_unset_flag(rn, new_select, BGP_INFO_ATTR_CHANGED);
892 UNSET_FLAG(new_select->flags, BGP_INFO_MULTIPATH_CHG);
893 }
894
895 if (new_select && new_select->type == ZEBRA_ROUTE_BGP
896 && new_select->sub_type == BGP_ROUTE_NORMAL) {
ead40654
MK
897 flags = 0;
898 if (new_select->attr->sticky)
899 SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
900 if (new_select->attr->default_gw)
901 SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
d62a17ae 902 ret = evpn_zebra_install(bgp, vpn, (struct prefix_evpn *)&rn->p,
996c9314 903 new_select->attr->nexthop, flags);
d62a17ae 904 /* If an old best existed and it was a "local" route, the only
905 * reason
906 * it would be supplanted is due to MAC mobility procedures. So,
907 * we
908 * need to do an implicit delete and withdraw that route from
909 * peers.
910 */
911 if (old_select && old_select->peer == bgp->peer_self
912 && old_select->type == ZEBRA_ROUTE_BGP
913 && old_select->sub_type == BGP_ROUTE_STATIC)
914 evpn_delete_old_local_route(bgp, vpn, rn, old_select);
915 } else {
916 if (old_select && old_select->type == ZEBRA_ROUTE_BGP
917 && old_select->sub_type == BGP_ROUTE_NORMAL)
918 ret = evpn_zebra_uninstall(bgp, vpn,
919 (struct prefix_evpn *)&rn->p,
920 old_select->attr->nexthop);
921 }
922
923 /* Clear any route change flags. */
924 bgp_zebra_clear_route_change_flags(rn);
925
926 /* Reap old select bgp_info, if it has been removed */
927 if (old_select && CHECK_FLAG(old_select->flags, BGP_INFO_REMOVED))
928 bgp_info_reap(rn, old_select);
929
930 return ret;
128ea8ab 931}
932
ead40654
MK
933/*
934 * Return true if the local ri for this rn is of type gateway mac
935 */
936static int evpn_route_is_def_gw(struct bgp *bgp, struct bgp_node *rn)
937{
996c9314
LB
938 struct bgp_info *tmp_ri = NULL;
939 struct bgp_info *local_ri = NULL;
ead40654
MK
940
941 local_ri = NULL;
942 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
943 if (tmp_ri->peer == bgp->peer_self
944 && tmp_ri->type == ZEBRA_ROUTE_BGP
945 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
946 local_ri = tmp_ri;
947 }
948
949 if (!local_ri)
950 return 0;
951
952 return local_ri->attr->default_gw;
953}
954
c85c03c7 955
956/*
957 * Return true if the local ri for this rn has sticky set
958 */
d62a17ae 959static int evpn_route_is_sticky(struct bgp *bgp, struct bgp_node *rn)
c85c03c7 960{
d62a17ae 961 struct bgp_info *tmp_ri;
962 struct bgp_info *local_ri;
c85c03c7 963
d62a17ae 964 local_ri = NULL;
965 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
966 if (tmp_ri->peer == bgp->peer_self
967 && tmp_ri->type == ZEBRA_ROUTE_BGP
968 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
969 local_ri = tmp_ri;
970 }
c85c03c7 971
d62a17ae 972 if (!local_ri)
973 return 0;
c85c03c7 974
d62a17ae 975 return local_ri->attr->sticky;
c85c03c7 976}
977
342dd0c6 978static int update_evpn_type5_route_entry(struct bgp *bgp_def,
979 struct bgp *bgp_vrf, afi_t afi,
980 safi_t safi, struct bgp_node *rn,
5424b7ba 981 struct attr *attr, int *route_changed)
342dd0c6 982{
983 struct attr *attr_new = NULL;
984 struct bgp_info *ri = NULL;
985 mpls_label_t label = MPLS_INVALID_LABEL;
986 struct bgp_info *local_ri = NULL;
987 struct bgp_info *tmp_ri = NULL;
988
5424b7ba 989 *route_changed = 0;
342dd0c6 990 /* locate the local route entry if any */
991 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
992 if (tmp_ri->peer == bgp_def->peer_self
993 && tmp_ri->type == ZEBRA_ROUTE_BGP
994 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
995 local_ri = tmp_ri;
996 }
997
998 /* create a new route entry if one doesnt exist.
523cafc4 999 Otherwise see if route attr has changed
1000 */
342dd0c6 1001 if (!local_ri) {
1002
5424b7ba
MK
1003 /* route has changed as this is the first entry */
1004 *route_changed = 1;
1005
342dd0c6 1006 /* Add (or update) attribute to hash. */
1007 attr_new = bgp_attr_intern(attr);
1008
1009 /* create the route info from attribute */
1010 ri = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0,
1011 bgp_def->peer_self, attr_new, rn);
1012 SET_FLAG(ri->flags, BGP_INFO_VALID);
1013
b57ba6d2 1014 /* Type-5 routes advertise the L3-VNI */
342dd0c6 1015 bgp_info_extra_get(ri);
1016 vni2label(bgp_vrf->l3vni, &label);
b57ba6d2
MK
1017 memcpy(&ri->extra->label, &label, sizeof(label));
1018 ri->extra->num_labels = 1;
342dd0c6 1019
1020 /* add the route entry to route node*/
1021 bgp_info_add(rn, ri);
1022 } else {
1023
1024 tmp_ri = local_ri;
1025 if (!attrhash_cmp(tmp_ri->attr, attr)) {
5424b7ba
MK
1026
1027 /* attribute changed */
1028 *route_changed = 1;
1029
342dd0c6 1030 /* The attribute has changed. */
1031 /* Add (or update) attribute to hash. */
1032 attr_new = bgp_attr_intern(attr);
1033 bgp_info_set_flag(rn, tmp_ri, BGP_INFO_ATTR_CHANGED);
1034
1035 /* Restore route, if needed. */
1036 if (CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED))
1037 bgp_info_restore(rn, tmp_ri);
1038
1039 /* Unintern existing, set to new. */
1040 bgp_attr_unintern(&tmp_ri->attr);
1041 tmp_ri->attr = attr_new;
1042 tmp_ri->uptime = bgp_clock();
1043 }
1044 }
1045 return 0;
1046}
1047
1048/* update evpn type-5 route entry */
996c9314
LB
1049static int update_evpn_type5_route(struct bgp *bgp_vrf, struct prefix_evpn *evp,
1050 struct attr *src_attr)
342dd0c6 1051{
1052 afi_t afi = AFI_L2VPN;
1053 safi_t safi = SAFI_EVPN;
1054 struct attr attr;
1055 struct bgp_node *rn = NULL;
1056 struct bgp *bgp_def = NULL;
5424b7ba 1057 int route_changed = 0;
342dd0c6 1058
1059 bgp_def = bgp_get_default();
1060 if (!bgp_def)
faafdfa8 1061 return 0;
342dd0c6 1062
2f69f6d3 1063 /* Build path attribute for this route - use the source attr, if
1064 * present, else treat as locally originated.
1065 */
1066 if (src_attr)
1067 bgp_attr_dup(&attr, src_attr);
1068 else {
1069 memset(&attr, 0, sizeof(struct attr));
1070 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
1071 }
1072 /* Set nexthop to ourselves and fill in the Router MAC. */
342dd0c6 1073 attr.nexthop = bgp_vrf->originator_ip;
1074 attr.mp_nexthop_global_in = bgp_vrf->originator_ip;
1075 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1076 memcpy(&attr.rmac, &bgp_vrf->rmac, sizeof(struct ethaddr));
1077
1078 /* Setup RT and encap extended community */
1079 build_evpn_type5_route_extcomm(bgp_vrf, &attr);
1080
1081 /* get the route node in global table */
1082 rn = bgp_afi_node_get(bgp_def->rib[afi][safi], afi, safi,
996c9314 1083 (struct prefix *)evp, &bgp_vrf->vrf_prd);
342dd0c6 1084 assert(rn);
1085
1086 /* create or update the route entry within the route node */
996c9314
LB
1087 update_evpn_type5_route_entry(bgp_def, bgp_vrf, afi, safi, rn, &attr,
1088 &route_changed);
342dd0c6 1089
1090 /* schedule for processing and unlock node */
5424b7ba
MK
1091 if (route_changed) {
1092 bgp_process(bgp_def, rn, afi, safi);
1093 bgp_unlock_node(rn);
1094 }
342dd0c6 1095
1096 /* uninten temporary */
5ee65f6f 1097 if (!src_attr)
1098 aspath_unintern(&attr.aspath);
342dd0c6 1099 return 0;
1100}
1101
128ea8ab 1102/*
1103 * Create or update EVPN route entry. This could be in the VNI route table
1104 * or the global route table.
1105 */
d62a17ae 1106static int update_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
1107 afi_t afi, safi_t safi, struct bgp_node *rn,
1108 struct attr *attr, int add, int vni_table,
1a98c087 1109 struct bgp_info **ri, u_char flags)
d62a17ae 1110{
1111 struct bgp_info *tmp_ri;
1112 struct bgp_info *local_ri, *remote_ri;
1113 struct attr *attr_new;
b57ba6d2
MK
1114 mpls_label_t label[BGP_MAX_LABELS];
1115 u_int32_t num_labels = 1;
d62a17ae 1116 int route_change = 1;
1117 u_char sticky = 0;
b57ba6d2 1118 struct prefix_evpn *evp;
d62a17ae 1119
1120 *ri = NULL;
b57ba6d2
MK
1121 evp = (struct prefix_evpn *)&rn->p;
1122 memset(&label, 0, sizeof(label));
d62a17ae 1123
1124 /* See if this is an update of an existing route, or a new add. Also,
1125 * identify if already known from remote, and if so, the one with the
1126 * highest sequence number; this is only when adding to the VNI routing
1127 * table.
1128 */
1129 local_ri = remote_ri = NULL;
1130 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
1131 if (tmp_ri->peer == bgp->peer_self
1132 && tmp_ri->type == ZEBRA_ROUTE_BGP
1133 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
1134 local_ri = tmp_ri;
1135 if (vni_table) {
1136 if (tmp_ri->type == ZEBRA_ROUTE_BGP
1137 && tmp_ri->sub_type == BGP_ROUTE_NORMAL
1138 && CHECK_FLAG(tmp_ri->flags, BGP_INFO_VALID)) {
1139 if (!remote_ri)
1140 remote_ri = tmp_ri;
1141 else if (mac_mobility_seqnum(tmp_ri->attr)
1142 > mac_mobility_seqnum(remote_ri->attr))
1143 remote_ri = tmp_ri;
1144 }
1145 }
1146 }
1147
1148 /* If route doesn't exist already, create a new one, if told to.
1149 * Otherwise act based on whether the attributes of the route have
1150 * changed or not.
1151 */
1152 if (!local_ri && !add)
1153 return 0;
1154
1155 if (!local_ri) {
1156 /* When learnt locally for the first time but already known from
1157 * remote, we have to initiate appropriate MAC mobility steps.
1158 * This
1159 * is applicable when updating the VNI routing table.
1a98c087
MK
1160 * We need to skip mobility steps for g/w macs (local mac on g/w
1161 * SVI) advertised in EVPN.
1162 * This will ensure that local routes are preferred for g/w macs
d62a17ae 1163 */
ead40654 1164 if (remote_ri && !CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW)) {
d62a17ae 1165 u_int32_t cur_seqnum;
1166
1167 /* Add MM extended community to route. */
1168 cur_seqnum = mac_mobility_seqnum(remote_ri->attr);
1169 add_mac_mobility_to_attr(cur_seqnum + 1, attr);
1170 }
1171
1172 /* Add (or update) attribute to hash. */
1173 attr_new = bgp_attr_intern(attr);
1174
1175 /* Extract MAC mobility sequence number, if any. */
1176 attr_new->mm_seqnum =
1177 bgp_attr_mac_mobility_seqnum(attr_new, &sticky);
1178 attr_new->sticky = sticky;
1179
1180 /* Create new route with its attribute. */
1181 tmp_ri = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0,
1182 bgp->peer_self, attr_new, rn);
1183 SET_FLAG(tmp_ri->flags, BGP_INFO_VALID);
1184 bgp_info_extra_get(tmp_ri);
1185
1186 /* The VNI goes into the 'label' field of the route */
b57ba6d2 1187 vni2label(vpn->vni, &label[0]);
c48d9f5f
MK
1188
1189 /* Type-2 routes may carry a second VNI - the L3-VNI.
1190 * Only attach second label if we are advertising two labels for
1191 * type-2 routes.
1192 */
996c9314
LB
1193 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
1194 && CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)) {
b57ba6d2
MK
1195 vni_t l3vni;
1196
1197 l3vni = bgpevpn_get_l3vni(vpn);
1198 if (l3vni) {
1199 vni2label(l3vni, &label[1]);
1200 num_labels++;
1201 }
1202 }
d62a17ae 1203
b57ba6d2
MK
1204 memcpy(&tmp_ri->extra->label, label, sizeof(label));
1205 tmp_ri->extra->num_labels = num_labels;
d62a17ae 1206 bgp_info_add(rn, tmp_ri);
1207 } else {
1208 tmp_ri = local_ri;
1209 if (attrhash_cmp(tmp_ri->attr, attr)
1210 && !CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED))
1211 route_change = 0;
1212 else {
c48d9f5f
MK
1213 /*
1214 * The attributes have changed, type-2 routes needs to
1215 * be advertised with right labels.
1216 */
1217 vni2label(vpn->vni, &label[0]);
996c9314
LB
1218 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
1219 && CHECK_FLAG(vpn->flags,
1220 VNI_FLAG_USE_TWO_LABELS)) {
c48d9f5f
MK
1221 vni_t l3vni;
1222
1223 l3vni = bgpevpn_get_l3vni(vpn);
1224 if (l3vni) {
1225 vni2label(l3vni, &label[1]);
1226 num_labels++;
1227 }
1228 }
1229 memcpy(&tmp_ri->extra->label, label, sizeof(label));
1230 tmp_ri->extra->num_labels = num_labels;
1231
d62a17ae 1232 /* The attribute has changed. */
1233 /* Add (or update) attribute to hash. */
1234 attr_new = bgp_attr_intern(attr);
1235 bgp_info_set_flag(rn, tmp_ri, BGP_INFO_ATTR_CHANGED);
1236
1237 /* Restore route, if needed. */
1238 if (CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED))
1239 bgp_info_restore(rn, tmp_ri);
1240
1241 /* Unintern existing, set to new. */
1242 bgp_attr_unintern(&tmp_ri->attr);
1243 tmp_ri->attr = attr_new;
1244 tmp_ri->uptime = bgp_clock();
1245 }
1246 }
1247
1248 /* Return back the route entry. */
1249 *ri = tmp_ri;
1250 return route_change;
128ea8ab 1251}
1252
1253/*
1254 * Create or update EVPN route (of type based on prefix) for specified VNI
1255 * and schedule for processing.
1256 */
d62a17ae 1257static int update_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
1a98c087 1258 struct prefix_evpn *p, u_char flags)
128ea8ab 1259{
d62a17ae 1260 struct bgp_node *rn;
1261 struct attr attr;
1262 struct attr *attr_new;
1263 struct bgp_info *ri;
1264 afi_t afi = AFI_L2VPN;
1265 safi_t safi = SAFI_EVPN;
1266 int route_change;
128ea8ab 1267
d62a17ae 1268 memset(&attr, 0, sizeof(struct attr));
128ea8ab 1269
d62a17ae 1270 /* Build path-attribute for this route. */
1271 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
1272 attr.nexthop = vpn->originator_ip;
1273 attr.mp_nexthop_global_in = vpn->originator_ip;
1274 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
317f1fe0 1275 attr.sticky = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY) ? 1 : 0;
ead40654 1276 attr.default_gw = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW) ? 1 : 0;
be41eb68
MK
1277
1278 /* PMSI is only needed for type-3 routes */
1279 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE)
1280 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL);
1281
1282 /* router mac is only needed for type-2 and type-5 routes */
1283 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
1284 bgpevpn_get_rmac(vpn, &attr.rmac);
a21bd7a3 1285 vni2label(vpn->vni, &(attr.label));
128ea8ab 1286
d62a17ae 1287 /* Set up RT and ENCAP extended community. */
996c9314
LB
1288 build_evpn_route_extcomm(
1289 vpn, &attr, IS_EVPN_PREFIX_IPADDR_V4(p) ? AFI_IP : AFI_IP6);
128ea8ab 1290
d62a17ae 1291 /* First, create (or fetch) route node within the VNI. */
1292 /* NOTE: There is no RD here. */
1293 rn = bgp_node_get(vpn->route_table, (struct prefix *)p);
128ea8ab 1294
d62a17ae 1295 /* Create or update route entry. */
1296 route_change = update_evpn_route_entry(bgp, vpn, afi, safi, rn, &attr,
1a98c087 1297 1, 1, &ri, flags);
d62a17ae 1298 assert(ri);
1299 attr_new = ri->attr;
128ea8ab 1300
d62a17ae 1301 /* Perform route selection; this is just to set the flags correctly
1302 * as local route in the VNI always wins.
1303 */
1304 evpn_route_select_install(bgp, vpn, rn);
1305 bgp_unlock_node(rn);
128ea8ab 1306
d62a17ae 1307 /* If this is a new route or some attribute has changed, export the
1308 * route to the global table. The route will be advertised to peers
1309 * from there. Note that this table is a 2-level tree (RD-level +
1310 * Prefix-level) similar to L3VPN routes.
1311 */
1312 if (route_change) {
1313 struct bgp_info *global_ri;
128ea8ab 1314
d62a17ae 1315 rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
1316 (struct prefix *)p, &vpn->prd);
1317 update_evpn_route_entry(bgp, vpn, afi, safi, rn, attr_new, 1, 0,
1a98c087 1318 &global_ri, flags);
128ea8ab 1319
d62a17ae 1320 /* Schedule for processing and unlock node. */
1321 bgp_process(bgp, rn, afi, safi);
1322 bgp_unlock_node(rn);
1323 }
128ea8ab 1324
d62a17ae 1325 /* Unintern temporary. */
1326 aspath_unintern(&attr.aspath);
128ea8ab 1327
d62a17ae 1328 return 0;
128ea8ab 1329}
1330
342dd0c6 1331/* Delete EVPN type5 route entry from global table */
1332static void delete_evpn_type5_route_entry(struct bgp *bgp_def,
996c9314
LB
1333 struct bgp *bgp_vrf, afi_t afi,
1334 safi_t safi, struct bgp_node *rn,
342dd0c6 1335 struct bgp_info **ri)
1336{
1337 struct bgp_info *tmp_ri = NULL;
1338
1339 *ri = NULL;
1340
1341 /* find the matching route entry */
1342 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next)
1343 if (tmp_ri->peer == bgp_def->peer_self
1344 && tmp_ri->type == ZEBRA_ROUTE_BGP
1345 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
1346 break;
1347
1348 *ri = tmp_ri;
1349
1350 /* Mark route for delete. */
1351 if (tmp_ri)
1352 bgp_info_delete(rn, tmp_ri);
1353}
1354
1355/* Delete EVPN type5 route */
996c9314 1356static int delete_evpn_type5_route(struct bgp *bgp_vrf, struct prefix_evpn *evp)
342dd0c6 1357{
1358 afi_t afi = AFI_L2VPN;
1359 safi_t safi = SAFI_EVPN;
1360 struct bgp_node *rn = NULL;
1361 struct bgp_info *ri = NULL;
1362 struct bgp *bgp_def = NULL; /* default bgp instance */
1363
1364 bgp_def = bgp_get_default();
1365 if (!bgp_def)
faafdfa8 1366 return 0;
342dd0c6 1367
1368 /* locate the global route entry for this type-5 prefix */
1369 rn = bgp_afi_node_lookup(bgp_def->rib[afi][safi], afi, safi,
1370 (struct prefix *)evp, &bgp_vrf->vrf_prd);
1371 if (!rn)
1372 return 0;
1373
1374 delete_evpn_type5_route_entry(bgp_def, bgp_vrf, afi, safi, rn, &ri);
1375 if (ri)
1376 bgp_process(bgp_def, rn, afi, safi);
1377 bgp_unlock_node(rn);
1378 return 0;
1379}
1380
128ea8ab 1381/*
1382 * Delete EVPN route entry. This could be in the VNI route table
1383 * or the global route table.
1384 */
d62a17ae 1385static void delete_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
1386 afi_t afi, safi_t safi, struct bgp_node *rn,
1387 struct bgp_info **ri)
128ea8ab 1388{
d62a17ae 1389 struct bgp_info *tmp_ri;
128ea8ab 1390
d62a17ae 1391 *ri = NULL;
128ea8ab 1392
d62a17ae 1393 /* Now, find matching route. */
1394 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next)
1395 if (tmp_ri->peer == bgp->peer_self
1396 && tmp_ri->type == ZEBRA_ROUTE_BGP
1397 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
1398 break;
128ea8ab 1399
d62a17ae 1400 *ri = tmp_ri;
128ea8ab 1401
d62a17ae 1402 /* Mark route for delete. */
1403 if (tmp_ri)
1404 bgp_info_delete(rn, tmp_ri);
128ea8ab 1405}
1406
1407/*
1408 * Delete EVPN route (of type based on prefix) for specified VNI and
1409 * schedule for processing.
1410 */
d62a17ae 1411static int delete_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
1412 struct prefix_evpn *p)
1413{
1414 struct bgp_node *rn, *global_rn;
1415 struct bgp_info *ri;
1416 afi_t afi = AFI_L2VPN;
1417 safi_t safi = SAFI_EVPN;
1418
1419 /* First, locate the route node within the VNI. If it doesn't exist,
1420 * there
1421 * is nothing further to do.
1422 */
1423 /* NOTE: There is no RD here. */
1424 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
1425 if (!rn)
1426 return 0;
1427
1428 /* Next, locate route node in the global EVPN routing table. Note that
1429 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1430 * L3VPN routes.
1431 */
1432 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
1433 (struct prefix *)p, &vpn->prd);
1434 if (global_rn) {
1435 /* Delete route entry in the global EVPN table. */
1436 delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);
1437
1438 /* Schedule for processing - withdraws to peers happen from
1439 * this table.
1440 */
1441 if (ri)
1442 bgp_process(bgp, global_rn, afi, safi);
1443 bgp_unlock_node(global_rn);
1444 }
1445
1446 /* Delete route entry in the VNI route table. This can just be removed.
1447 */
1448 delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);
1449 if (ri)
1450 bgp_info_reap(rn, ri);
1451 bgp_unlock_node(rn);
1452
1453 return 0;
128ea8ab 1454}
1455
1456/*
1457 * Update all type-2 (MACIP) local routes for this VNI - these should also
1458 * be scheduled for advertise to peers.
1459 */
d62a17ae 1460static int update_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
1461{
1462 afi_t afi;
1463 safi_t safi;
1464 struct bgp_node *rn;
1465 struct bgp_info *ri;
1466 struct attr attr;
1467 struct attr attr_sticky;
ead40654 1468 struct attr attr_def_gw;
7ec156a9
MK
1469 struct attr attr_ip6;
1470 struct attr attr_sticky_ip6;
ead40654 1471 struct attr attr_def_gw_ip6;
d62a17ae 1472 struct attr *attr_new;
1473
1474 afi = AFI_L2VPN;
1475 safi = SAFI_EVPN;
1476 memset(&attr, 0, sizeof(struct attr));
1477 memset(&attr_sticky, 0, sizeof(struct attr));
ead40654 1478 memset(&attr_def_gw, 0, sizeof(struct attr));
7ec156a9
MK
1479 memset(&attr_ip6, 0, sizeof(struct attr));
1480 memset(&attr_sticky_ip6, 0, sizeof(struct attr));
ead40654 1481 memset(&attr_def_gw_ip6, 0, sizeof(struct attr));
d62a17ae 1482
1483 /* Build path-attribute - all type-2 routes for this VNI will share the
1484 * same path attribute.
1485 */
1486 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
1487 bgp_attr_default_set(&attr_sticky, BGP_ORIGIN_IGP);
ead40654 1488 bgp_attr_default_set(&attr_def_gw, BGP_ORIGIN_IGP);
d62a17ae 1489 attr.nexthop = vpn->originator_ip;
1490 attr.mp_nexthop_global_in = vpn->originator_ip;
1491 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
bc59a672 1492 bgpevpn_get_rmac(vpn, &attr.rmac);
d62a17ae 1493 attr_sticky.nexthop = vpn->originator_ip;
1494 attr_sticky.mp_nexthop_global_in = vpn->originator_ip;
1495 attr_sticky.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1496 attr_sticky.sticky = 1;
bc59a672 1497 bgpevpn_get_rmac(vpn, &attr_sticky.rmac);
ead40654
MK
1498 attr_def_gw.nexthop = vpn->originator_ip;
1499 attr_def_gw.mp_nexthop_global_in = vpn->originator_ip;
1500 attr_def_gw.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1501 attr_def_gw.default_gw = 1;
1502 bgpevpn_get_rmac(vpn, &attr_def_gw.rmac);
7ec156a9
MK
1503 bgp_attr_default_set(&attr_ip6, BGP_ORIGIN_IGP);
1504 bgp_attr_default_set(&attr_sticky_ip6, BGP_ORIGIN_IGP);
a6ad0a41 1505 bgp_attr_default_set(&attr_def_gw_ip6, BGP_ORIGIN_IGP);
7ec156a9
MK
1506 attr_ip6.nexthop = vpn->originator_ip;
1507 attr_ip6.mp_nexthop_global_in = vpn->originator_ip;
1508 attr_ip6.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1509 bgpevpn_get_rmac(vpn, &attr_ip6.rmac);
1510 attr_sticky_ip6.nexthop = vpn->originator_ip;
1511 attr_sticky_ip6.mp_nexthop_global_in = vpn->originator_ip;
1512 attr_sticky_ip6.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1513 attr_sticky_ip6.sticky = 1;
1514 bgpevpn_get_rmac(vpn, &attr_sticky_ip6.rmac);
ead40654
MK
1515 attr_def_gw_ip6.nexthop = vpn->originator_ip;
1516 attr_def_gw_ip6.mp_nexthop_global_in = vpn->originator_ip;
1517 attr_def_gw_ip6.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1518 attr_def_gw_ip6.default_gw = 1;
1519 bgpevpn_get_rmac(vpn, &attr_def_gw_ip6.rmac);
d62a17ae 1520
1521 /* Set up RT, ENCAP and sticky MAC extended community. */
7ec156a9
MK
1522 build_evpn_route_extcomm(vpn, &attr, AFI_IP);
1523 build_evpn_route_extcomm(vpn, &attr_sticky, AFI_IP);
ead40654 1524 build_evpn_route_extcomm(vpn, &attr_def_gw, AFI_IP);
7ec156a9
MK
1525 build_evpn_route_extcomm(vpn, &attr_ip6, AFI_IP6);
1526 build_evpn_route_extcomm(vpn, &attr_sticky_ip6, AFI_IP6);
ead40654 1527 build_evpn_route_extcomm(vpn, &attr_def_gw_ip6, AFI_IP);
d62a17ae 1528
1529 /* Walk this VNI's route table and update local type-2 routes. For any
1530 * routes updated, update corresponding entry in the global table too.
1531 */
1532 for (rn = bgp_table_top(vpn->route_table); rn;
1533 rn = bgp_route_next(rn)) {
1534 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
1535 struct bgp_node *rd_rn;
1536 struct bgp_info *global_ri;
1537
1538 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1539 continue;
1540
7ec156a9
MK
1541 if (IS_EVPN_PREFIX_IPADDR_V4(evp)) {
1542 if (evpn_route_is_sticky(bgp, rn))
1543 update_evpn_route_entry(bgp, vpn, afi, safi, rn,
996c9314
LB
1544 &attr_sticky, 0, 1, &ri,
1545 0);
ead40654
MK
1546 else if (evpn_route_is_def_gw(bgp, rn))
1547 update_evpn_route_entry(bgp, vpn, afi, safi, rn,
996c9314
LB
1548 &attr_def_gw, 0, 1, &ri,
1549 0);
7ec156a9
MK
1550 else
1551 update_evpn_route_entry(bgp, vpn, afi, safi, rn,
1552 &attr, 0, 1, &ri, 0);
1553 } else {
1554 if (evpn_route_is_sticky(bgp, rn))
1555 update_evpn_route_entry(bgp, vpn, afi, safi, rn,
1556 &attr_sticky_ip6, 0, 1,
1557 &ri, 0);
ead40654
MK
1558 else if (evpn_route_is_def_gw(bgp, rn))
1559 update_evpn_route_entry(bgp, vpn, afi, safi, rn,
1560 &attr_def_gw_ip6, 0, 1,
1561 &ri, 0);
7ec156a9
MK
1562 else
1563 update_evpn_route_entry(bgp, vpn, afi, safi, rn,
996c9314
LB
1564 &attr_ip6, 0, 1, &ri,
1565 0);
7ec156a9 1566 }
d62a17ae 1567
1568 /* If a local route exists for this prefix, we need to update
1569 * the global routing table too.
1570 */
1571 if (!ri)
1572 continue;
1573
1574 /* Perform route selection; this is just to set the flags
1575 * correctly
1576 * as local route in the VNI always wins.
1577 */
1578 evpn_route_select_install(bgp, vpn, rn);
1579
1580 attr_new = ri->attr;
1581
1582 /* Update route in global routing table. */
1583 rd_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
1584 (struct prefix *)evp, &vpn->prd);
1585 assert(rd_rn);
1586 update_evpn_route_entry(bgp, vpn, afi, safi, rd_rn, attr_new, 0,
1a98c087 1587 0, &global_ri, 0);
d62a17ae 1588
1589 /* Schedule for processing and unlock node. */
1590 bgp_process(bgp, rd_rn, afi, safi);
1591 bgp_unlock_node(rd_rn);
1592 }
1593
1594 /* Unintern temporary. */
1595 aspath_unintern(&attr.aspath);
ead40654 1596 aspath_unintern(&attr_ip6.aspath);
d62a17ae 1597 aspath_unintern(&attr_sticky.aspath);
ead40654
MK
1598 aspath_unintern(&attr_sticky_ip6.aspath);
1599 aspath_unintern(&attr_def_gw.aspath);
1600 aspath_unintern(&attr_def_gw_ip6.aspath);
d62a17ae 1601
1602 return 0;
128ea8ab 1603}
1604
1605/*
1606 * Delete all type-2 (MACIP) local routes for this VNI - only from the
1607 * global routing table. These are also scheduled for withdraw from peers.
1608 */
d62a17ae 1609static int delete_global_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1610{
d62a17ae 1611 afi_t afi;
1612 safi_t safi;
1613 struct bgp_node *rdrn, *rn;
1614 struct bgp_table *table;
1615 struct bgp_info *ri;
128ea8ab 1616
d62a17ae 1617 afi = AFI_L2VPN;
1618 safi = SAFI_EVPN;
128ea8ab 1619
d62a17ae 1620 rdrn = bgp_node_lookup(bgp->rib[afi][safi], (struct prefix *)&vpn->prd);
1621 if (rdrn && rdrn->info) {
1622 table = (struct bgp_table *)rdrn->info;
1623 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
1624 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
128ea8ab 1625
d62a17ae 1626 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1627 continue;
128ea8ab 1628
d62a17ae 1629 delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);
1630 if (ri)
1631 bgp_process(bgp, rn, afi, safi);
1632 }
1633 }
128ea8ab 1634
d62a17ae 1635 /* Unlock RD node. */
1636 if (rdrn)
1637 bgp_unlock_node(rdrn);
128ea8ab 1638
d62a17ae 1639 return 0;
128ea8ab 1640}
1641
1642/*
1643 * Delete all type-2 (MACIP) local routes for this VNI - from the global
1644 * table as well as the per-VNI route table.
1645 */
d62a17ae 1646static int delete_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1647{
d62a17ae 1648 afi_t afi;
1649 safi_t safi;
1650 struct bgp_node *rn;
1651 struct bgp_info *ri;
128ea8ab 1652
d62a17ae 1653 afi = AFI_L2VPN;
1654 safi = SAFI_EVPN;
128ea8ab 1655
d62a17ae 1656 /* First, walk the global route table for this VNI's type-2 local
1657 * routes.
1658 * EVPN routes are a 2-level table, first get the RD table.
1659 */
1660 delete_global_type2_routes(bgp, vpn);
128ea8ab 1661
d62a17ae 1662 /* Next, walk this VNI's route table and delete local type-2 routes. */
1663 for (rn = bgp_table_top(vpn->route_table); rn;
1664 rn = bgp_route_next(rn)) {
1665 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
128ea8ab 1666
d62a17ae 1667 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1668 continue;
128ea8ab 1669
d62a17ae 1670 delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);
128ea8ab 1671
d62a17ae 1672 /* Route entry in local table gets deleted immediately. */
1673 if (ri)
1674 bgp_info_reap(rn, ri);
1675 }
128ea8ab 1676
d62a17ae 1677 return 0;
128ea8ab 1678}
1679
1680/*
1681 * Delete all routes in the per-VNI route table.
1682 */
d62a17ae 1683static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1684{
d62a17ae 1685 struct bgp_node *rn;
1686 struct bgp_info *ri, *nextri;
128ea8ab 1687
d62a17ae 1688 /* Walk this VNI's route table and delete all routes. */
1689 for (rn = bgp_table_top(vpn->route_table); rn;
1690 rn = bgp_route_next(rn)) {
1691 for (ri = rn->info; (ri != NULL) && (nextri = ri->next, 1);
1692 ri = nextri) {
1693 bgp_info_delete(rn, ri);
1694 bgp_info_reap(rn, ri);
1695 }
1696 }
128ea8ab 1697
d62a17ae 1698 return 0;
128ea8ab 1699}
1700
1701/*
1702 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
1703 * export RT getting modified or change to tunnel IP. Note that these
1704 * situations need the route in the per-VNI table as well as the global
1705 * table to be updated (as attributes change).
1706 */
d62a17ae 1707static int update_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1708{
d62a17ae 1709 int ret;
1710 struct prefix_evpn p;
128ea8ab 1711
d62a17ae 1712 /* Update and advertise the type-3 route (only one) followed by the
1713 * locally learnt type-2 routes (MACIP) - for this VNI.
1714 */
1715 build_evpn_type3_prefix(&p, vpn->originator_ip);
1716 ret = update_evpn_route(bgp, vpn, &p, 0);
1717 if (ret)
1718 return ret;
128ea8ab 1719
d62a17ae 1720 return update_all_type2_routes(bgp, vpn);
128ea8ab 1721}
1722
1723/*
1724 * Delete (and withdraw) local routes for specified VNI from the global
1725 * table and per-VNI table. After this, remove all other routes from
1726 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
1727 * (advertise-all-vni) being disabled.
1728 */
d62a17ae 1729static int delete_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1730{
d62a17ae 1731 int ret;
1732 struct prefix_evpn p;
128ea8ab 1733
d62a17ae 1734 /* Delete and withdraw locally learnt type-2 routes (MACIP)
1735 * followed by type-3 routes (only one) - for this VNI.
1736 */
1737 ret = delete_all_type2_routes(bgp, vpn);
1738 if (ret)
1739 return ret;
128ea8ab 1740
d62a17ae 1741 build_evpn_type3_prefix(&p, vpn->originator_ip);
1742 ret = delete_evpn_route(bgp, vpn, &p);
1743 if (ret)
1744 return ret;
128ea8ab 1745
d62a17ae 1746 /* Delete all routes from the per-VNI table. */
1747 return delete_all_vni_routes(bgp, vpn);
128ea8ab 1748}
1749
1750/*
d1911c26 1751 * There is a tunnel endpoint IP address change for this VNI, delete
1752 * prior type-3 route (if needed) and update.
1753 * Note: Route re-advertisement happens elsewhere after other processing
1754 * other changes.
128ea8ab 1755 */
d62a17ae 1756static int handle_tunnel_ip_change(struct bgp *bgp, struct bgpevpn *vpn,
1757 struct in_addr originator_ip)
128ea8ab 1758{
d62a17ae 1759 struct prefix_evpn p;
128ea8ab 1760
ddd16ed5
MK
1761 /* If VNI is not live, we only need to update the originator ip */
1762 if (!is_vni_live(vpn)) {
1763 vpn->originator_ip = originator_ip;
1764 return 0;
1765 }
1766
db0e1937
MK
1767 /* Update the tunnel-ip hash */
1768 bgp_tip_del(bgp, &vpn->originator_ip);
1769 bgp_tip_add(bgp, &originator_ip);
1770
1771 /* filter routes as martian nexthop db has changed */
1772 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
1773
d62a17ae 1774 /* Need to withdraw type-3 route as the originator IP is part
1775 * of the key.
1776 */
1777 build_evpn_type3_prefix(&p, vpn->originator_ip);
1778 delete_evpn_route(bgp, vpn, &p);
128ea8ab 1779
d62a17ae 1780 /* Update the tunnel IP and re-advertise all routes for this VNI. */
1781 vpn->originator_ip = originator_ip;
d1911c26 1782 return 0;
128ea8ab 1783}
1784
d3135ba3 1785/*
1786 * Install route entry into the VRF routing table and invoke route selection.
1787 */
1788static int install_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
1789 struct prefix_evpn *evp,
1790 struct bgp_info *parent_ri)
1791{
1792 struct bgp_node *rn;
1793 struct bgp_info *ri;
1794 struct attr *attr_new;
c4edf708 1795 int ret = 0;
d3135ba3 1796 struct prefix p;
1797 struct prefix *pp = &p;
1798 afi_t afi = 0;
1799 safi_t safi = 0;
1eb88002
MK
1800 char buf[PREFIX_STRLEN];
1801 char buf1[PREFIX_STRLEN];
d3135ba3 1802
1803 memset(pp, 0, sizeof(struct prefix));
90264d64
MK
1804 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
1805 ip_prefix_from_type2_prefix(evp, pp);
523cafc4 1806 else if (evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)
90264d64 1807 ip_prefix_from_type5_prefix(evp, pp);
d3135ba3 1808
1eb88002 1809 if (bgp_debug_zebra(NULL)) {
996c9314
LB
1810 zlog_debug(
1811 "installing evpn prefix %s as ip prefix %s in vrf %s",
1812 prefix2str(evp, buf, sizeof(buf)),
1813 prefix2str(pp, buf1, sizeof(buf)),
1814 vrf_id_to_name(bgp_vrf->vrf_id));
1eb88002
MK
1815 }
1816
d3135ba3 1817 /* Create (or fetch) route within the VRF. */
1818 /* NOTE: There is no RD here. */
1819 if (IS_EVPN_PREFIX_IPADDR_V4(evp)) {
1820 afi = AFI_IP;
1821 safi = SAFI_UNICAST;
1822 rn = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
1823 } else if (IS_EVPN_PREFIX_IPADDR_V6(evp)) {
1824 afi = AFI_IP6;
1825 safi = SAFI_UNICAST;
1826 rn = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
1827 } else
1828 return 0;
1829
1830 /* Check if route entry is already present. */
1831 for (ri = rn->info; ri; ri = ri->next)
1832 if (ri->extra
1833 && (struct bgp_info *)ri->extra->parent == parent_ri)
1834 break;
1835
1836 if (!ri) {
1837 /* Add (or update) attribute to hash. */
1838 attr_new = bgp_attr_intern(parent_ri->attr);
1839
1840 /* Create new route with its attribute. */
1841 ri = info_make(parent_ri->type, parent_ri->sub_type, 0,
1842 parent_ri->peer, attr_new, rn);
1843 SET_FLAG(ri->flags, BGP_INFO_VALID);
1844 bgp_info_extra_get(ri);
1845 ri->extra->parent = parent_ri;
b57ba6d2 1846 if (parent_ri->extra) {
d3135ba3 1847 memcpy(&ri->extra->label, &parent_ri->extra->label,
b57ba6d2
MK
1848 sizeof(ri->extra->label));
1849 ri->extra->num_labels = parent_ri->extra->num_labels;
1850 }
d3135ba3 1851 bgp_info_add(rn, ri);
1852 } else {
1853 if (attrhash_cmp(ri->attr, parent_ri->attr)
1854 && !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) {
1855 bgp_unlock_node(rn);
1856 return 0;
1857 }
1858 /* The attribute has changed. */
1859 /* Add (or update) attribute to hash. */
1860 attr_new = bgp_attr_intern(parent_ri->attr);
1861
1862 /* Restore route, if needed. */
1863 if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
1864 bgp_info_restore(rn, ri);
1865
1866 /* Mark if nexthop has changed. */
1867 if (!IPV4_ADDR_SAME(&ri->attr->nexthop, &attr_new->nexthop))
1868 SET_FLAG(ri->flags, BGP_INFO_IGP_CHANGED);
1869
1870 /* Unintern existing, set to new. */
1871 bgp_attr_unintern(&ri->attr);
1872 ri->attr = attr_new;
1873 ri->uptime = bgp_clock();
1874 }
1875
1876 /* Perform route selection and update zebra, if required. */
1eb88002 1877 bgp_process(bgp_vrf, rn, afi, safi);
d3135ba3 1878
1879 return ret;
1880}
1881
128ea8ab 1882/*
1883 * Install route entry into the VNI routing table and invoke route selection.
1884 */
d62a17ae 1885static int install_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
1886 struct prefix_evpn *p,
1887 struct bgp_info *parent_ri)
1888{
1889 struct bgp_node *rn;
1890 struct bgp_info *ri;
1891 struct attr *attr_new;
1892 int ret;
1893
1894 /* Create (or fetch) route within the VNI. */
1895 /* NOTE: There is no RD here. */
1896 rn = bgp_node_get(vpn->route_table, (struct prefix *)p);
1897
1898 /* Check if route entry is already present. */
1899 for (ri = rn->info; ri; ri = ri->next)
1900 if (ri->extra
1901 && (struct bgp_info *)ri->extra->parent == parent_ri)
1902 break;
1903
1904 if (!ri) {
1905 /* Add (or update) attribute to hash. */
1906 attr_new = bgp_attr_intern(parent_ri->attr);
1907
1908 /* Create new route with its attribute. */
1909 ri = info_make(parent_ri->type, parent_ri->sub_type, 0,
1910 parent_ri->peer, attr_new, rn);
1911 SET_FLAG(ri->flags, BGP_INFO_VALID);
1912 bgp_info_extra_get(ri);
1913 ri->extra->parent = parent_ri;
b57ba6d2 1914 if (parent_ri->extra) {
d62a17ae 1915 memcpy(&ri->extra->label, &parent_ri->extra->label,
b57ba6d2
MK
1916 sizeof(ri->extra->label));
1917 ri->extra->num_labels = parent_ri->extra->num_labels;
1918 }
d62a17ae 1919 bgp_info_add(rn, ri);
1920 } else {
1921 if (attrhash_cmp(ri->attr, parent_ri->attr)
1922 && !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) {
1923 bgp_unlock_node(rn);
1924 return 0;
1925 }
1926 /* The attribute has changed. */
1927 /* Add (or update) attribute to hash. */
1928 attr_new = bgp_attr_intern(parent_ri->attr);
1929
1930 /* Restore route, if needed. */
1931 if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
1932 bgp_info_restore(rn, ri);
1933
1934 /* Mark if nexthop has changed. */
1935 if (!IPV4_ADDR_SAME(&ri->attr->nexthop, &attr_new->nexthop))
1936 SET_FLAG(ri->flags, BGP_INFO_IGP_CHANGED);
1937
1938 /* Unintern existing, set to new. */
1939 bgp_attr_unintern(&ri->attr);
1940 ri->attr = attr_new;
1941 ri->uptime = bgp_clock();
1942 }
1943
1944 /* Perform route selection and update zebra, if required. */
1945 ret = evpn_route_select_install(bgp, vpn, rn);
1946
1947 return ret;
128ea8ab 1948}
1949
d3135ba3 1950/*
1951 * Uninstall route entry from the VRF routing table and send message
1952 * to zebra, if appropriate.
1953 */
1954static int uninstall_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
1955 struct prefix_evpn *evp,
1956 struct bgp_info *parent_ri)
1957{
1958 struct bgp_node *rn;
1959 struct bgp_info *ri;
c4edf708 1960 int ret = 0;
d3135ba3 1961 struct prefix p;
1962 struct prefix *pp = &p;
1963 afi_t afi = 0;
1964 safi_t safi = 0;
1eb88002
MK
1965 char buf[PREFIX_STRLEN];
1966 char buf1[PREFIX_STRLEN];
d3135ba3 1967
1968 memset(pp, 0, sizeof(struct prefix));
42cb44f2
MK
1969 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
1970 ip_prefix_from_type2_prefix(evp, pp);
655b04d1 1971 else if (evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)
42cb44f2 1972 ip_prefix_from_type5_prefix(evp, pp);
d3135ba3 1973
1eb88002 1974 if (bgp_debug_zebra(NULL)) {
996c9314
LB
1975 zlog_debug(
1976 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
1977 prefix2str(evp, buf, sizeof(buf)),
1978 prefix2str(pp, buf1, sizeof(buf)),
1979 vrf_id_to_name(bgp_vrf->vrf_id));
1eb88002
MK
1980 }
1981
d3135ba3 1982 /* Locate route within the VRF. */
1983 /* NOTE: There is no RD here. */
1984 if (IS_EVPN_PREFIX_IPADDR_V4(evp)) {
1985 afi = AFI_IP;
1986 safi = SAFI_UNICAST;
1987 rn = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
1988 } else {
1989 afi = AFI_IP6;
1990 safi = SAFI_UNICAST;
1991 rn = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
1992 }
1993
1994 if (!rn)
1995 return 0;
1996
1997 /* Find matching route entry. */
1998 for (ri = rn->info; ri; ri = ri->next)
1999 if (ri->extra
2000 && (struct bgp_info *)ri->extra->parent == parent_ri)
2001 break;
2002
2003 if (!ri)
2004 return 0;
2005
2006 /* Mark entry for deletion */
2007 bgp_info_delete(rn, ri);
2008
2009 /* Perform route selection and update zebra, if required. */
1eb88002 2010 bgp_process(bgp_vrf, rn, afi, safi);
d3135ba3 2011
2012 /* Unlock route node. */
2013 bgp_unlock_node(rn);
2014
2015 return ret;
2016}
2017
128ea8ab 2018/*
2019 * Uninstall route entry from the VNI routing table and send message
2020 * to zebra, if appropriate.
2021 */
d62a17ae 2022static int uninstall_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
2023 struct prefix_evpn *p,
2024 struct bgp_info *parent_ri)
128ea8ab 2025{
d62a17ae 2026 struct bgp_node *rn;
2027 struct bgp_info *ri;
2028 int ret;
128ea8ab 2029
d62a17ae 2030 /* Locate route within the VNI. */
2031 /* NOTE: There is no RD here. */
2032 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
2033 if (!rn)
2034 return 0;
128ea8ab 2035
d62a17ae 2036 /* Find matching route entry. */
2037 for (ri = rn->info; ri; ri = ri->next)
2038 if (ri->extra
2039 && (struct bgp_info *)ri->extra->parent == parent_ri)
2040 break;
128ea8ab 2041
d62a17ae 2042 if (!ri)
2043 return 0;
128ea8ab 2044
d62a17ae 2045 /* Mark entry for deletion */
2046 bgp_info_delete(rn, ri);
128ea8ab 2047
d62a17ae 2048 /* Perform route selection and update zebra, if required. */
2049 ret = evpn_route_select_install(bgp, vpn, rn);
128ea8ab 2050
d62a17ae 2051 /* Unlock route node. */
2052 bgp_unlock_node(rn);
128ea8ab 2053
d62a17ae 2054 return ret;
128ea8ab 2055}
2056
5ba238b7
MK
2057/*
2058 * Given a route entry and a VRF, see if this route entry should be
2059 * imported into the VRF i.e., RTs match.
2060 */
996c9314 2061static int is_route_matching_for_vrf(struct bgp *bgp_vrf, struct bgp_info *ri)
5ba238b7
MK
2062{
2063 struct attr *attr = ri->attr;
2064 struct ecommunity *ecom;
2065 int i;
2066
2067 assert(attr);
2068 /* Route should have valid RT to be even considered. */
2069 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
2070 return 0;
2071
2072 ecom = attr->ecommunity;
2073 if (!ecom || !ecom->size)
2074 return 0;
2075
2076 /* For each extended community RT, see if it matches this VNI. If any RT
2077 * matches, we're done.
2078 */
2079 for (i = 0; i < ecom->size; i++) {
2080 u_char *pnt;
2081 u_char type, sub_type;
2082 struct ecommunity_val *eval;
2083 struct ecommunity_val eval_tmp;
2084 struct vrf_irt_node *irt;
2085
2086 /* Only deal with RTs */
2087 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
2088 eval = (struct ecommunity_val *)(ecom->val
2089 + (i * ECOMMUNITY_SIZE));
2090 type = *pnt++;
2091 sub_type = *pnt++;
2092 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
2093 continue;
2094
2095 /* See if this RT matches specified VNIs import RTs */
2096 irt = lookup_vrf_import_rt(eval);
2097 if (irt && irt->vrfs)
2098 if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
2099 return 1;
2100
2101 /* Also check for non-exact match. In this, we mask out the AS
2102 * and
2103 * only check on the local-admin sub-field. This is to
2104 * facilitate using
2105 * VNI as the RT for EBGP peering too.
2106 */
2107 irt = NULL;
2108 if (type == ECOMMUNITY_ENCODE_AS
2109 || type == ECOMMUNITY_ENCODE_AS4
2110 || type == ECOMMUNITY_ENCODE_IP) {
2111 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
2112 mask_ecom_global_admin(&eval_tmp, eval);
2113 irt = lookup_vrf_import_rt(&eval_tmp);
2114 }
2115 if (irt && irt->vrfs)
2116 if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
2117 return 1;
2118 }
2119
2120 return 0;
2121}
2122
128ea8ab 2123/*
2124 * Given a route entry and a VNI, see if this route entry should be
2125 * imported into the VNI i.e., RTs match.
2126 */
d62a17ae 2127static int is_route_matching_for_vni(struct bgp *bgp, struct bgpevpn *vpn,
2128 struct bgp_info *ri)
2129{
2130 struct attr *attr = ri->attr;
2131 struct ecommunity *ecom;
2132 int i;
2133
2134 assert(attr);
2135 /* Route should have valid RT to be even considered. */
2136 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
2137 return 0;
2138
2139 ecom = attr->ecommunity;
2140 if (!ecom || !ecom->size)
2141 return 0;
2142
2143 /* For each extended community RT, see if it matches this VNI. If any RT
2144 * matches, we're done.
2145 */
2146 for (i = 0; i < ecom->size; i++) {
2147 u_char *pnt;
2148 u_char type, sub_type;
2149 struct ecommunity_val *eval;
2150 struct ecommunity_val eval_tmp;
2151 struct irt_node *irt;
2152
2153 /* Only deal with RTs */
2154 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
2155 eval = (struct ecommunity_val *)(ecom->val
2156 + (i * ECOMMUNITY_SIZE));
2157 type = *pnt++;
2158 sub_type = *pnt++;
2159 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
2160 continue;
2161
2162 /* See if this RT matches specified VNIs import RTs */
2163 irt = lookup_import_rt(bgp, eval);
2164 if (irt && irt->vnis)
2165 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
2166 return 1;
2167
2168 /* Also check for non-exact match. In this, we mask out the AS
2169 * and
2170 * only check on the local-admin sub-field. This is to
2171 * facilitate using
2172 * VNI as the RT for EBGP peering too.
2173 */
2174 irt = NULL;
2175 if (type == ECOMMUNITY_ENCODE_AS
2176 || type == ECOMMUNITY_ENCODE_AS4
2177 || type == ECOMMUNITY_ENCODE_IP) {
2178 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
2179 mask_ecom_global_admin(&eval_tmp, eval);
2180 irt = lookup_import_rt(bgp, &eval_tmp);
2181 }
2182 if (irt && irt->vnis)
2183 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
2184 return 1;
2185 }
2186
2187 return 0;
128ea8ab 2188}
2189
5ba238b7
MK
2190/*
2191 * Install or uninstall mac-ip routes are appropriate for this
2192 * particular VRF.
2193 */
996c9314 2194static int install_uninstall_routes_for_vrf(struct bgp *bgp_vrf, int install)
5ba238b7
MK
2195{
2196 afi_t afi;
2197 safi_t safi;
2198 struct bgp_node *rd_rn, *rn;
2199 struct bgp_table *table;
2200 struct bgp_info *ri;
2201 int ret;
2202 char buf[PREFIX_STRLEN];
2203 struct bgp *bgp_def = NULL;
2204
2205 afi = AFI_L2VPN;
2206 safi = SAFI_EVPN;
2207 bgp_def = bgp_get_default();
2208 if (!bgp_def)
2209 return -1;
2210
2211 /* Walk entire global routing table and evaluate routes which could be
2212 * imported into this VRF. Note that we need to loop through all global
2213 * routes to determine which route matches the import rt on vrf
2214 */
2215 for (rd_rn = bgp_table_top(bgp_def->rib[afi][safi]); rd_rn;
2216 rd_rn = bgp_route_next(rd_rn)) {
2217 table = (struct bgp_table *)(rd_rn->info);
2218 if (!table)
2219 continue;
2220
2221 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
2222 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
2223
1eb88002 2224 /* if not mac-ip route skip this route */
996c9314
LB
2225 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
2226 || evp->prefix.route_type
2227 == BGP_EVPN_IP_PREFIX_ROUTE))
5ba238b7
MK
2228 continue;
2229
1eb88002 2230 /* if not a mac+ip route skip this route */
996c9314
LB
2231 if (!(IS_EVPN_PREFIX_IPADDR_V4(evp)
2232 || IS_EVPN_PREFIX_IPADDR_V6(evp)))
1eb88002
MK
2233 continue;
2234
5ba238b7
MK
2235 for (ri = rn->info; ri; ri = ri->next) {
2236 /* Consider "valid" remote routes applicable for
523cafc4 2237 * this VRF.
2238 */
5ba238b7
MK
2239 if (!(CHECK_FLAG(ri->flags, BGP_INFO_VALID)
2240 && ri->type == ZEBRA_ROUTE_BGP
2241 && ri->sub_type == BGP_ROUTE_NORMAL))
2242 continue;
2243
2244 if (is_route_matching_for_vrf(bgp_vrf, ri)) {
2245 if (install)
996c9314 2246 ret = install_evpn_route_entry_in_vrf(
5ba238b7
MK
2247 bgp_vrf, evp, ri);
2248 else
996c9314 2249 ret = uninstall_evpn_route_entry_in_vrf(
5ba238b7
MK
2250 bgp_vrf, evp, ri);
2251
2252 if (ret) {
2253 zlog_err(
2254 "Failed to %s EVPN %s route in VRF %s",
2255 install ? "install"
2256 : "uninstall",
2257 prefix2str(evp, buf,
2258 sizeof(buf)),
996c9314
LB
2259 vrf_id_to_name(
2260 bgp_vrf->vrf_id));
5ba238b7
MK
2261 return ret;
2262 }
2263 }
2264 }
2265 }
2266 }
2267
2268 return 0;
2269}
2270
128ea8ab 2271/*
2272 * Install or uninstall routes of specified type that are appropriate for this
2273 * particular VNI.
2274 */
d62a17ae 2275static int install_uninstall_routes_for_vni(struct bgp *bgp,
2276 struct bgpevpn *vpn,
2277 bgp_evpn_route_type rtype,
2278 int install)
2279{
0291c246
MK
2280 afi_t afi;
2281 safi_t safi;
2282 struct bgp_node *rd_rn, *rn;
2283 struct bgp_table *table;
2284 struct bgp_info *ri;
2285 int ret;
d62a17ae 2286
2287 afi = AFI_L2VPN;
2288 safi = SAFI_EVPN;
2289
2290 /* Walk entire global routing table and evaluate routes which could be
2291 * imported into this VPN. Note that we cannot just look at the routes
2292 * for
2293 * the VNI's RD - remote routes applicable for this VNI could have any
2294 * RD.
2295 */
2296 /* EVPN routes are a 2-level table. */
2297 for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
2298 rd_rn = bgp_route_next(rd_rn)) {
2299 table = (struct bgp_table *)(rd_rn->info);
2300 if (!table)
2301 continue;
2302
2303 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
2304 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
2305
2306 if (evp->prefix.route_type != rtype)
2307 continue;
2308
2309 for (ri = rn->info; ri; ri = ri->next) {
2310 /* Consider "valid" remote routes applicable for
2311 * this VNI. */
2312 if (!(CHECK_FLAG(ri->flags, BGP_INFO_VALID)
2313 && ri->type == ZEBRA_ROUTE_BGP
2314 && ri->sub_type == BGP_ROUTE_NORMAL))
2315 continue;
2316
2317 if (is_route_matching_for_vni(bgp, vpn, ri)) {
2318 if (install)
2319 ret = install_evpn_route_entry(
60466a63 2320 bgp, vpn, evp, ri);
d62a17ae 2321 else
2322 ret = uninstall_evpn_route_entry(
2323 bgp, vpn, evp, ri);
2324
2325 if (ret) {
2326 zlog_err(
2327 "%u: Failed to %s EVPN %s route in VNI %u",
2328 bgp->vrf_id,
2329 install ? "install"
2330 : "uninstall",
2331 rtype == BGP_EVPN_MAC_IP_ROUTE
2332 ? "MACIP"
2333 : "IMET",
2334 vpn->vni);
2335 return ret;
2336 }
2337 }
2338 }
2339 }
2340 }
2341
2342 return 0;
128ea8ab 2343}
2344
5ba238b7 2345/* Install any existing remote routes applicable for this VRF into VRF RIB. This
523cafc4 2346 * is invoked upon l3vni-add or l3vni import rt change
2347 */
5ba238b7
MK
2348static int install_routes_for_vrf(struct bgp *bgp_vrf)
2349{
2350 install_uninstall_routes_for_vrf(bgp_vrf, 1);
2351 return 0;
2352}
2353
128ea8ab 2354/*
2355 * Install any existing remote routes applicable for this VNI into its
2356 * routing table. This is invoked when a VNI becomes "live" or its Import
2357 * RT is changed.
2358 */
d62a17ae 2359static int install_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2360{
d62a17ae 2361 int ret;
128ea8ab 2362
d62a17ae 2363 /* Install type-3 routes followed by type-2 routes - the ones applicable
2364 * for this VNI.
2365 */
2366 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
2367 1);
2368 if (ret)
2369 return ret;
128ea8ab 2370
d62a17ae 2371 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
2372 1);
128ea8ab 2373}
2374
5ba238b7
MK
2375/* uninstall routes from l3vni vrf. */
2376static int uninstall_routes_for_vrf(struct bgp *bgp_vrf)
2377{
2378 install_uninstall_routes_for_vrf(bgp_vrf, 0);
2379 return 0;
2380}
2381
90e60aa7 2382/*
2383 * Uninstall any existing remote routes for this VNI. One scenario in which
2384 * this is invoked is upon an import RT change.
2385 */
d62a17ae 2386static int uninstall_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 2387{
d62a17ae 2388 int ret;
90e60aa7 2389
d62a17ae 2390 /* Uninstall type-2 routes followed by type-3 routes - the ones
2391 * applicable
2392 * for this VNI.
2393 */
2394 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
2395 0);
2396 if (ret)
2397 return ret;
90e60aa7 2398
d62a17ae 2399 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
2400 0);
90e60aa7 2401}
2402
d3135ba3 2403/*
2404 * Install or uninstall route in matching VRFs (list).
2405 */
2406static int install_uninstall_route_in_vrfs(struct bgp *bgp_def, afi_t afi,
2407 safi_t safi, struct prefix_evpn *evp,
2408 struct bgp_info *ri,
2409 struct list *vrfs, int install)
2410{
2411 char buf[PREFIX2STR_BUFFER];
2412 struct bgp *bgp_vrf;
2413 struct listnode *node, *nnode;
2414
90264d64 2415 /* Only type-2/type-5 routes go into a VRF */
996c9314
LB
2416 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
2417 || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
d3135ba3 2418 return 0;
2419
90264d64 2420 /* if it is type-2 route and not a mac+ip route skip this route */
996c9314
LB
2421 if ((evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
2422 && !(IS_EVPN_PREFIX_IPADDR_V4(evp)
2423 || IS_EVPN_PREFIX_IPADDR_V6(evp)))
30a30f57
MK
2424 return 0;
2425
d3135ba3 2426 for (ALL_LIST_ELEMENTS(vrfs, node, nnode, bgp_vrf)) {
2427 int ret;
2428
2429 if (install)
996c9314 2430 ret = install_evpn_route_entry_in_vrf(bgp_vrf, evp, ri);
d3135ba3 2431 else
996c9314
LB
2432 ret = uninstall_evpn_route_entry_in_vrf(bgp_vrf, evp,
2433 ri);
d3135ba3 2434
2435 if (ret) {
2436 zlog_err("%u: Failed to %s prefix %s in VRF %s",
2437 bgp_def->vrf_id,
2438 install ? "install" : "uninstall",
2439 prefix2str(evp, buf, sizeof(buf)),
2440 vrf_id_to_name(bgp_vrf->vrf_id));
2441 return ret;
2442 }
2443 }
2444
2445 return 0;
2446}
2447
128ea8ab 2448/*
2449 * Install or uninstall route in matching VNIs (list).
2450 */
d62a17ae 2451static int install_uninstall_route_in_vnis(struct bgp *bgp, afi_t afi,
2452 safi_t safi, struct prefix_evpn *evp,
2453 struct bgp_info *ri,
2454 struct list *vnis, int install)
128ea8ab 2455{
d62a17ae 2456 struct bgpevpn *vpn;
2457 struct listnode *node, *nnode;
128ea8ab 2458
d62a17ae 2459 for (ALL_LIST_ELEMENTS(vnis, node, nnode, vpn)) {
2460 int ret;
128ea8ab 2461
d62a17ae 2462 if (!is_vni_live(vpn))
2463 continue;
128ea8ab 2464
d62a17ae 2465 if (install)
2466 ret = install_evpn_route_entry(bgp, vpn, evp, ri);
2467 else
2468 ret = uninstall_evpn_route_entry(bgp, vpn, evp, ri);
128ea8ab 2469
d62a17ae 2470 if (ret) {
2471 zlog_err("%u: Failed to %s EVPN %s route in VNI %u",
2472 bgp->vrf_id, install ? "install" : "uninstall",
2473 evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
2474 ? "MACIP"
2475 : "IMET",
2476 vpn->vni);
2477 return ret;
2478 }
2479 }
128ea8ab 2480
d62a17ae 2481 return 0;
128ea8ab 2482}
2483
2484/*
2485 * Install or uninstall route for appropriate VNIs.
2486 */
d62a17ae 2487static int install_uninstall_evpn_route(struct bgp *bgp, afi_t afi, safi_t safi,
2488 struct prefix *p, struct bgp_info *ri,
2489 int import)
2490{
2491 struct prefix_evpn *evp = (struct prefix_evpn *)p;
2492 struct attr *attr = ri->attr;
2493 struct ecommunity *ecom;
2494 int i;
2495
2496 assert(attr);
2497
90264d64 2498 /* Only type-2 and type-3 and type-5 are supported currently */
d62a17ae 2499 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
90264d64
MK
2500 || evp->prefix.route_type == BGP_EVPN_IMET_ROUTE
2501 || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
d62a17ae 2502 return 0;
2503
2504 /* If we don't have Route Target, nothing much to do. */
2505 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
2506 return 0;
2507
2508 ecom = attr->ecommunity;
2509 if (!ecom || !ecom->size)
2510 return -1;
2511
90264d64
MK
2512 /* For each extended community RT, see which VNIs/VRFs match and import
2513 * the route into matching VNIs/VRFs.
d62a17ae 2514 */
2515 for (i = 0; i < ecom->size; i++) {
2516 u_char *pnt;
2517 u_char type, sub_type;
2518 struct ecommunity_val *eval;
2519 struct ecommunity_val eval_tmp;
996c9314 2520 struct irt_node *irt; /* import rt for l2vni */
d3135ba3 2521 struct vrf_irt_node *vrf_irt; /* import rt for l3vni */
d62a17ae 2522
2523 /* Only deal with RTs */
2524 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
2525 eval = (struct ecommunity_val *)(ecom->val
2526 + (i * ECOMMUNITY_SIZE));
2527 type = *pnt++;
2528 sub_type = *pnt++;
2529 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
2530 continue;
2531
90264d64 2532 /* Import route into matching l2-vnis (type-2/type-3 routes go
523cafc4 2533 * into l2vni table)
2534 */
d62a17ae 2535 irt = lookup_import_rt(bgp, eval);
2536 if (irt && irt->vnis)
2537 install_uninstall_route_in_vnis(bgp, afi, safi, evp, ri,
2538 irt->vnis, import);
2539
90264d64 2540 /* Import route into matching l3-vnis (type-2/type-5 routes go
523cafc4 2541 * into l3vni/vrf table)
2542 */
d3135ba3 2543 vrf_irt = lookup_vrf_import_rt(eval);
2544 if (vrf_irt && vrf_irt->vrfs)
2545 install_uninstall_route_in_vrfs(bgp, afi, safi, evp, ri,
2546 vrf_irt->vrfs, import);
2547
90264d64
MK
2548 /* Also check for non-exact match. In this,
2549 * we mask out the AS and
2550 * only check on the local-admin sub-field.
2551 * This is to facilitate using
d62a17ae 2552 * VNI as the RT for EBGP peering too.
2553 */
2554 irt = NULL;
d3135ba3 2555 vrf_irt = NULL;
d62a17ae 2556 if (type == ECOMMUNITY_ENCODE_AS
2557 || type == ECOMMUNITY_ENCODE_AS4
2558 || type == ECOMMUNITY_ENCODE_IP) {
2559 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
2560 mask_ecom_global_admin(&eval_tmp, eval);
2561 irt = lookup_import_rt(bgp, &eval_tmp);
d3135ba3 2562 vrf_irt = lookup_vrf_import_rt(&eval_tmp);
d62a17ae 2563 }
2564 if (irt && irt->vnis)
2565 install_uninstall_route_in_vnis(bgp, afi, safi, evp, ri,
2566 irt->vnis, import);
d3135ba3 2567 if (vrf_irt && vrf_irt->vrfs)
996c9314
LB
2568 install_uninstall_route_in_vrfs(bgp, afi, safi, evp, ri,
2569 vrf_irt->vrfs, import);
d62a17ae 2570 }
2571
2572 return 0;
128ea8ab 2573}
2574
80b140af
MK
2575/* delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
2576 * routes */
2577static void delete_withdraw_vrf_routes(struct bgp *bgp_vrf)
2578{
2579 /* delete all ipv4 routes and withdraw from peers */
053905d2 2580 bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);
80b140af
MK
2581
2582 /* delete all ipv6 routes and withdraw from peers */
053905d2 2583 bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
80b140af
MK
2584}
2585
2586/* update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
2587 * routes */
2588static void update_advertise_vrf_routes(struct bgp *bgp_vrf)
2589{
2590 /* update all ipv4 routes */
053905d2 2591 bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);
80b140af
MK
2592
2593 /* update all ipv6 routes */
053905d2 2594 bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
80b140af
MK
2595}
2596
676f83b9 2597/*
2598 * update and advertise local routes for a VRF as type-5 routes.
2599 * This is invoked upon RD change for a VRF. Note taht the processing is only
2600 * done in the global route table using the routes which already exist in the
2601 * VRF routing table
2602 */
80b140af 2603static void update_router_id_vrf(struct bgp *bgp_vrf)
676f83b9 2604{
80b140af
MK
2605 /* skip if the RD is configured */
2606 if (is_vrf_rd_configured(bgp_vrf))
2607 return;
2608
2609 /* derive the RD for the VRF based on new router-id */
2610 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
2611
2612 /* update advertise ipv4|ipv6 routes as type-5 routes */
2613 update_advertise_vrf_routes(bgp_vrf);
676f83b9 2614}
2615
2616/*
2617 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
2618 * This is invoked upon VRF RD change. The processing is done only from global
2619 * table.
2620 */
80b140af 2621static void withdraw_router_id_vrf(struct bgp *bgp_vrf)
676f83b9 2622{
80b140af
MK
2623 /* skip if the RD is configured */
2624 if (is_vrf_rd_configured(bgp_vrf))
2625 return;
2626
2627 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
2628 delete_withdraw_vrf_routes(bgp_vrf);
676f83b9 2629}
2630
90e60aa7 2631/*
2632 * Update and advertise local routes for a VNI. Invoked upon router-id
2633 * change. Note that the processing is done only on the global route table
2634 * using routes that already exist in the per-VNI table.
2635 */
d62a17ae 2636static int update_advertise_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
2637{
2638 struct prefix_evpn p;
2639 struct bgp_node *rn, *global_rn;
2640 struct bgp_info *ri, *global_ri;
2641 struct attr *attr;
2642 afi_t afi = AFI_L2VPN;
2643 safi_t safi = SAFI_EVPN;
2644
2645 /* Locate type-3 route for VNI in the per-VNI table and use its
2646 * attributes to create and advertise the type-3 route for this VNI
2647 * in the global table.
2648 */
2649 build_evpn_type3_prefix(&p, vpn->originator_ip);
2650 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)&p);
2651 if (!rn) /* unexpected */
2652 return 0;
2653 for (ri = rn->info; ri; ri = ri->next)
2654 if (ri->peer == bgp->peer_self && ri->type == ZEBRA_ROUTE_BGP
2655 && ri->sub_type == BGP_ROUTE_STATIC)
2656 break;
2657 if (!ri) /* unexpected */
2658 return 0;
2659 attr = ri->attr;
2660
2661 global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
2662 (struct prefix *)&p, &vpn->prd);
1a98c087
MK
2663 update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr, 1, 0, &ri,
2664 0);
d62a17ae 2665
2666 /* Schedule for processing and unlock node. */
2667 bgp_process(bgp, global_rn, afi, safi);
2668 bgp_unlock_node(global_rn);
2669
2670 /* Now, walk this VNI's route table and use the route and its attribute
2671 * to create and schedule route in global table.
2672 */
2673 for (rn = bgp_table_top(vpn->route_table); rn;
2674 rn = bgp_route_next(rn)) {
2675 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
2676
2677 /* Identify MAC-IP local routes. */
2678 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2679 continue;
2680
2681 for (ri = rn->info; ri; ri = ri->next)
2682 if (ri->peer == bgp->peer_self
2683 && ri->type == ZEBRA_ROUTE_BGP
2684 && ri->sub_type == BGP_ROUTE_STATIC)
2685 break;
2686 if (!ri)
2687 continue;
2688
2689 /* Create route in global routing table using this route entry's
2690 * attribute.
2691 */
2692 attr = ri->attr;
2693 global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
2694 (struct prefix *)evp, &vpn->prd);
2695 assert(global_rn);
2696 update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr, 1,
1a98c087 2697 0, &global_ri, 0);
d62a17ae 2698
2699 /* Schedule for processing and unlock node. */
2700 bgp_process(bgp, global_rn, afi, safi);
2701 bgp_unlock_node(global_rn);
2702 }
2703
2704 return 0;
90e60aa7 2705}
2706
2707/*
2708 * Delete (and withdraw) local routes for a VNI - only from the global
2709 * table. Invoked upon router-id change.
2710 */
d62a17ae 2711static int delete_withdraw_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 2712{
d62a17ae 2713 int ret;
2714 struct prefix_evpn p;
2715 struct bgp_node *global_rn;
2716 struct bgp_info *ri;
2717 afi_t afi = AFI_L2VPN;
2718 safi_t safi = SAFI_EVPN;
90e60aa7 2719
d62a17ae 2720 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2721 * for this VNI - from the global table.
2722 */
2723 ret = delete_global_type2_routes(bgp, vpn);
2724 if (ret)
2725 return ret;
90e60aa7 2726
d62a17ae 2727 /* Remove type-3 route for this VNI from global table. */
2728 build_evpn_type3_prefix(&p, vpn->originator_ip);
2729 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
2730 (struct prefix *)&p, &vpn->prd);
2731 if (global_rn) {
2732 /* Delete route entry in the global EVPN table. */
2733 delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);
90e60aa7 2734
d62a17ae 2735 /* Schedule for processing - withdraws to peers happen from
2736 * this table.
2737 */
2738 if (ri)
2739 bgp_process(bgp, global_rn, afi, safi);
2740 bgp_unlock_node(global_rn);
2741 }
90e60aa7 2742
d62a17ae 2743 return 0;
90e60aa7 2744}
2745
2d48ee25 2746/*
2747 * Handle router-id change. Update and advertise local routes corresponding
2748 * to this VNI from peers. Note that this is invoked after updating the
2749 * router-id. The routes in the per-VNI table are used to create routes in
2750 * the global table and schedule them.
2751 */
d62a17ae 2752static void update_router_id_vni(struct hash_backet *backet, struct bgp *bgp)
2d48ee25 2753{
d62a17ae 2754 struct bgpevpn *vpn;
2d48ee25 2755
d62a17ae 2756 vpn = (struct bgpevpn *)backet->data;
2d48ee25 2757
d62a17ae 2758 if (!vpn) {
2759 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__);
2760 return;
2761 }
2d48ee25 2762
d62a17ae 2763 /* Skip VNIs with configured RD. */
2764 if (is_rd_configured(vpn))
2765 return;
2d48ee25 2766
d62a17ae 2767 bgp_evpn_derive_auto_rd(bgp, vpn);
2768 update_advertise_vni_routes(bgp, vpn);
2d48ee25 2769}
2770
2771/*
2772 * Handle router-id change. Delete and withdraw local routes corresponding
2773 * to this VNI from peers. Note that this is invoked prior to updating
2774 * the router-id and is done only on the global route table, the routes
2775 * are needed in the per-VNI table to re-advertise with new router id.
2776 */
d62a17ae 2777static void withdraw_router_id_vni(struct hash_backet *backet, struct bgp *bgp)
2d48ee25 2778{
d62a17ae 2779 struct bgpevpn *vpn;
2d48ee25 2780
d62a17ae 2781 vpn = (struct bgpevpn *)backet->data;
2d48ee25 2782
d62a17ae 2783 if (!vpn) {
2784 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__);
2785 return;
2786 }
2d48ee25 2787
d62a17ae 2788 /* Skip VNIs with configured RD. */
2789 if (is_rd_configured(vpn))
2790 return;
2d48ee25 2791
d62a17ae 2792 delete_withdraw_vni_routes(bgp, vpn);
2d48ee25 2793}
2794
128ea8ab 2795/*
2796 * Process received EVPN type-2 route (advertise or withdraw).
2797 */
d62a17ae 2798static int process_type2_route(struct peer *peer, afi_t afi, safi_t safi,
2799 struct attr *attr, u_char *pfx, int psize,
2800 u_int32_t addpath_id)
2801{
2802 struct prefix_rd prd;
2803 struct prefix_evpn p;
2804 u_char ipaddr_len;
2805 u_char macaddr_len;
b57ba6d2
MK
2806 mpls_label_t label[BGP_MAX_LABELS]; /* holds the VNI(s) as in packet */
2807 u_int32_t num_labels = 0;
d62a17ae 2808 int ret;
2809
2810 /* Type-2 route should be either 33, 37 or 49 bytes or an
2811 * additional 3 bytes if there is a second label (VNI):
2812 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
2813 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
2814 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
2815 */
2816 if (psize != 33 && psize != 37 && psize != 49 && psize != 36
2817 && psize != 40 && psize != 52) {
2818 zlog_err("%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
2819 peer->bgp->vrf_id, peer->host, psize);
2820 return -1;
2821 }
2822
2823 /* Make prefix_rd */
2824 prd.family = AF_UNSPEC;
2825 prd.prefixlen = 64;
2826 memcpy(&prd.val, pfx, 8);
2827 pfx += 8;
2828
2829 /* Make EVPN prefix. */
2830 memset(&p, 0, sizeof(struct prefix_evpn));
b03b8898 2831 p.family = AF_EVPN;
d62a17ae 2832 p.prefixlen = EVPN_TYPE_2_ROUTE_PREFIXLEN;
2833 p.prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
2834
2835 /* Skip over Ethernet Seg Identifier for now. */
2836 pfx += 10;
2837
2838 /* Skip over Ethernet Tag for now. */
2839 pfx += 4;
2840
2841 /* Get the MAC Addr len */
2842 macaddr_len = *pfx++;
2843
2844 /* Get the MAC Addr */
28328ea9
DS
2845 if (macaddr_len == (ETH_ALEN * 8)) {
2846 memcpy(&p.prefix.mac.octet, pfx, ETH_ALEN);
2847 pfx += ETH_ALEN;
d62a17ae 2848 } else {
2849 zlog_err(
2850 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
2851 peer->bgp->vrf_id, peer->host, macaddr_len);
2852 return -1;
2853 }
2854
2855
2856 /* Get the IP. */
2857 ipaddr_len = *pfx++;
2858 if (ipaddr_len != 0 && ipaddr_len != IPV4_MAX_BITLEN
2859 && ipaddr_len != IPV6_MAX_BITLEN) {
2860 zlog_err(
2861 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
2862 peer->bgp->vrf_id, peer->host, ipaddr_len);
2863 return -1;
2864 }
2865
2866 if (ipaddr_len) {
2867 ipaddr_len /= 8; /* Convert to bytes. */
2868 p.prefix.ip.ipa_type = (ipaddr_len == IPV4_MAX_BYTELEN)
2869 ? IPADDR_V4
2870 : IPADDR_V6;
2871 memcpy(&p.prefix.ip.ip.addr, pfx, ipaddr_len);
2872 }
2873 pfx += ipaddr_len;
2874
b57ba6d2
MK
2875 /* Get the VNI(s). Stored as bytes here. */
2876 num_labels++;
2877 memset(label, 0, sizeof(label));
2878 memcpy(&label[0], pfx, BGP_LABEL_BYTES);
1b817c78 2879 pfx += BGP_LABEL_BYTES;
b57ba6d2
MK
2880 psize -= (33 + ipaddr_len);
2881 /* Do we have a second VNI? */
2882 if (psize) {
2883 num_labels++;
2884 memcpy(&label[1], pfx, BGP_LABEL_BYTES);
6b11bd8d 2885 /*
2886 * If in future, we are required to access additional fields,
996c9314
LB
2887 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
2888 * the next field
6b11bd8d 2889 */
b57ba6d2 2890 }
d62a17ae 2891
2892 /* Process the route. */
2893 if (attr)
2894 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
2895 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 2896 &prd, &label[0], num_labels, 0, NULL);
d62a17ae 2897 else
2898 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
2899 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 2900 &prd, &label[0], num_labels, NULL);
d62a17ae 2901 return ret;
128ea8ab 2902}
2903
2904/*
2905 * Process received EVPN type-3 route (advertise or withdraw).
2906 */
d62a17ae 2907static int process_type3_route(struct peer *peer, afi_t afi, safi_t safi,
2908 struct attr *attr, u_char *pfx, int psize,
2909 u_int32_t addpath_id)
2910{
2911 struct prefix_rd prd;
2912 struct prefix_evpn p;
2913 u_char ipaddr_len;
2914 int ret;
2915
2916 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
2917 * IP len (1) and IP (4 or 16).
2918 */
2919 if (psize != 17 && psize != 29) {
2920 zlog_err("%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
2921 peer->bgp->vrf_id, peer->host, psize);
2922 return -1;
2923 }
2924
2925 /* Make prefix_rd */
2926 prd.family = AF_UNSPEC;
2927 prd.prefixlen = 64;
2928 memcpy(&prd.val, pfx, 8);
2929 pfx += 8;
2930
2931 /* Make EVPN prefix. */
2932 memset(&p, 0, sizeof(struct prefix_evpn));
b03b8898 2933 p.family = AF_EVPN;
d62a17ae 2934 p.prefixlen = EVPN_TYPE_3_ROUTE_PREFIXLEN;
2935 p.prefix.route_type = BGP_EVPN_IMET_ROUTE;
2936
2937 /* Skip over Ethernet Tag for now. */
2938 pfx += 4;
2939
2940 /* Get the IP. */
2941 ipaddr_len = *pfx++;
2942 if (ipaddr_len == IPV4_MAX_BITLEN) {
2943 p.prefix.ip.ipa_type = IPADDR_V4;
2944 memcpy(&p.prefix.ip.ip.addr, pfx, IPV4_MAX_BYTELEN);
2945 } else {
2946 zlog_err(
2947 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
2948 peer->bgp->vrf_id, peer->host, ipaddr_len);
2949 return -1;
2950 }
2951
2952 /* Process the route. */
2953 if (attr)
2954 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
2955 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 2956 &prd, NULL, 0, 0, NULL);
d62a17ae 2957 else
2958 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
2959 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 2960 &prd, NULL, 0, NULL);
d62a17ae 2961 return ret;
128ea8ab 2962}
2963
2964/*
2965 * Process received EVPN type-5 route (advertise or withdraw).
2966 */
d62a17ae 2967static int process_type5_route(struct peer *peer, afi_t afi, safi_t safi,
2968 struct attr *attr, u_char *pfx, int psize,
2969 u_int32_t addpath_id, int withdraw)
2970{
2971 struct prefix_rd prd;
2972 struct prefix_evpn p;
2973 struct bgp_route_evpn evpn;
2974 u_char ippfx_len;
2975 u_int32_t eth_tag;
b57ba6d2 2976 mpls_label_t label; /* holds the VNI as in the packet */
d62a17ae 2977 int ret;
2978
2979 /* Type-5 route should be 34 or 58 bytes:
2980 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
2981 * GW (4 or 16) and VNI (3).
2982 * Note that the IP and GW should both be IPv4 or both IPv6.
2983 */
2984 if (psize != 34 && psize != 58) {
2985 zlog_err("%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
2986 peer->bgp->vrf_id, peer->host, psize);
2987 return -1;
2988 }
2989
2990 /* Make prefix_rd */
2991 prd.family = AF_UNSPEC;
2992 prd.prefixlen = 64;
2993 memcpy(&prd.val, pfx, 8);
2994 pfx += 8;
2995
2996 /* Make EVPN prefix. */
2997 memset(&p, 0, sizeof(struct prefix_evpn));
b03b8898 2998 p.family = AF_EVPN;
e9fc2840 2999 p.prefixlen = EVPN_TYPE_5_ROUTE_PREFIXLEN;
d62a17ae 3000 p.prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
3001
3002 /* Additional information outside of prefix - ESI and GW IP */
3003 memset(&evpn, 0, sizeof(evpn));
3004
3005 /* Fetch ESI */
3006 memcpy(&evpn.eth_s_id.val, pfx, 10);
3007 pfx += 10;
3008
3009 /* Fetch Ethernet Tag. */
3010 memcpy(&eth_tag, pfx, 4);
3011 p.prefix.eth_tag = ntohl(eth_tag);
3012 pfx += 4;
3013
3014 /* Fetch IP prefix length. */
3015 ippfx_len = *pfx++;
3016 if (ippfx_len > IPV6_MAX_BITLEN) {
3017 zlog_err(
3018 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3019 peer->bgp->vrf_id, peer->host, ippfx_len);
3020 return -1;
3021 }
3022 p.prefix.ip_prefix_length = ippfx_len;
3023
3024 /* Determine IPv4 or IPv6 prefix */
3025 /* Since the address and GW are from the same family, this just becomes
3026 * a simple check on the total size.
3027 */
3028 if (psize == 34) {
3029 SET_IPADDR_V4(&p.prefix.ip);
3030 memcpy(&p.prefix.ip.ipaddr_v4, pfx, 4);
3031 pfx += 4;
3032 memcpy(&evpn.gw_ip.ipv4, pfx, 4);
3033 pfx += 4;
d62a17ae 3034 } else {
3035 SET_IPADDR_V6(&p.prefix.ip);
3036 memcpy(&p.prefix.ip.ipaddr_v6, pfx, 16);
3037 pfx += 16;
3038 memcpy(&evpn.gw_ip.ipv6, pfx, 16);
3039 pfx += 16;
d62a17ae 3040 }
3041
b57ba6d2
MK
3042 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3043 memset(&label, 0, sizeof(label));
3044 memcpy(&label, pfx, BGP_LABEL_BYTES);
6b11bd8d 3045
3046 /*
3047 * If in future, we are required to access additional fields,
996c9314
LB
3048 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
3049 * field
6b11bd8d 3050 */
d62a17ae 3051
3052 /* Process the route. */
3053 if (!withdraw)
3054 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
3055 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 3056 &prd, &label, 1, 0, &evpn);
d62a17ae 3057 else
3058 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
3059 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
b57ba6d2 3060 &prd, &label, 1, &evpn);
d62a17ae 3061
3062 return ret;
3063}
3064
3065static void evpn_mpattr_encode_type5(struct stream *s, struct prefix *p,
996c9314
LB
3066 struct prefix_rd *prd, mpls_label_t *label,
3067 u_int32_t num_labels, struct attr *attr)
d62a17ae 3068{
3069 int len;
3070 char temp[16];
3071 struct evpn_addr *p_evpn_p;
3072
3073 memset(&temp, 0, 16);
b03b8898 3074 if (p->family != AF_EVPN)
d62a17ae 3075 return;
3076 p_evpn_p = &(p->u.prefix_evpn);
3077
e9fc2840 3078 /* len denites the total len of IP and GW-IP in the route
523cafc4 3079 IP and GW-IP have to be both ipv4 or ipv6
3080 */
d62a17ae 3081 if (IS_IPADDR_V4(&p_evpn_p->ip))
e9fc2840 3082 len = 8; /* IP and GWIP are both ipv4 */
d62a17ae 3083 else
e9fc2840 3084 len = 32; /* IP and GWIP are both ipv6 */
d62a17ae 3085 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3086 stream_putc(s, 8 + 10 + 4 + 1 + len + 3);
3087 stream_put(s, prd->val, 8);
0af35d90 3088 if (attr)
d62a17ae 3089 stream_put(s, &(attr->evpn_overlay.eth_s_id), 10);
3090 else
3091 stream_put(s, &temp, 10);
3092 stream_putl(s, p_evpn_p->eth_tag);
3093 stream_putc(s, p_evpn_p->ip_prefix_length);
3094 if (IS_IPADDR_V4(&p_evpn_p->ip))
3095 stream_put_ipv4(s, p_evpn_p->ip.ipaddr_v4.s_addr);
3096 else
3097 stream_put(s, &p_evpn_p->ip.ipaddr_v6, 16);
0af35d90 3098 if (attr) {
d62a17ae 3099 if (IS_IPADDR_V4(&p_evpn_p->ip))
3100 stream_put_ipv4(s,
3101 attr->evpn_overlay.gw_ip.ipv4.s_addr);
3102 else
3103 stream_put(s, &(attr->evpn_overlay.gw_ip.ipv6), 16);
3104 } else {
3105 if (IS_IPADDR_V4(&p_evpn_p->ip))
3106 stream_put_ipv4(s, 0);
3107 else
3108 stream_put(s, &temp, 16);
3109 }
3110
b57ba6d2 3111 if (num_labels)
d62a17ae 3112 stream_put(s, label, 3);
3113 else
3114 stream_put3(s, 0);
128ea8ab 3115}
3116
3117/*
3118 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3119 */
d62a17ae 3120static void cleanup_vni_on_disable(struct hash_backet *backet, struct bgp *bgp)
128ea8ab 3121{
d62a17ae 3122 struct bgpevpn *vpn = (struct bgpevpn *)backet->data;
128ea8ab 3123
d62a17ae 3124 /* Remove EVPN routes and schedule for processing. */
3125 delete_routes_for_vni(bgp, vpn);
128ea8ab 3126
d62a17ae 3127 /* Clear "live" flag and see if hash needs to be freed. */
3128 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
3129 if (!is_vni_configured(vpn))
3130 bgp_evpn_free(bgp, vpn);
128ea8ab 3131}
3132
3133/*
3134 * Free a VNI entry; iterator function called during cleanup.
3135 */
d62a17ae 3136static void free_vni_entry(struct hash_backet *backet, struct bgp *bgp)
128ea8ab 3137{
d62a17ae 3138 struct bgpevpn *vpn;
128ea8ab 3139
d62a17ae 3140 vpn = (struct bgpevpn *)backet->data;
3141 delete_all_vni_routes(bgp, vpn);
3142 bgp_evpn_free(bgp, vpn);
128ea8ab 3143}
3144
c581d8b0
MK
3145/*
3146 * Derive AUTO import RT for BGP VRF - L3VNI
3147 */
3148static void evpn_auto_rt_import_add_for_vrf(struct bgp *bgp_vrf)
3149{
10ebe1ab
MK
3150 struct bgp *bgp_def = NULL;
3151
c581d8b0 3152 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
10ebe1ab
MK
3153 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
3154
3155 /* Map RT to VRF */
3156 bgp_def = bgp_get_default();
3157 if (!bgp_def)
3158 return;
3159 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
c581d8b0
MK
3160}
3161
3162/*
3163 * Delete AUTO import RT from BGP VRF - L3VNI
3164 */
3165static void evpn_auto_rt_import_delete_for_vrf(struct bgp *bgp_vrf)
3166{
3167 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
3168}
3169
3170/*
3171 * Derive AUTO export RT for BGP VRF - L3VNI
3172 */
3173static void evpn_auto_rt_export_add_for_vrf(struct bgp *bgp_vrf)
3174{
3175 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
3176 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
3177}
3178
3179/*
3180 * Delete AUTO export RT from BGP VRF - L3VNI
3181 */
3182static void evpn_auto_rt_export_delete_for_vrf(struct bgp *bgp_vrf)
3183{
3184 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
3185}
128ea8ab 3186
f1f8b53c
MK
3187static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp *bgp_vrf)
3188{
3189 struct bgp *bgp_def = NULL;
3190 struct listnode *node = NULL;
3191 struct bgpevpn *vpn = NULL;
3192
3193 bgp_def = bgp_get_default();
3194 if (!bgp_def)
3195 return;
3196
4992b4ae
MK
3197 /* update all type-5 routes */
3198 update_advertise_vrf_routes(bgp_vrf);
3199
3200 /* update all type-2 routes */
f1f8b53c
MK
3201 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
3202 update_routes_for_vni(bgp_def, vpn);
3203}
3204
128ea8ab 3205/*
3206 * Public functions.
3207 */
3208
5424b7ba 3209/* withdraw type-5 route corresponding to ip prefix */
31310b25 3210void bgp_evpn_withdraw_type5_route(struct bgp *bgp_vrf, struct prefix *p,
5424b7ba
MK
3211 afi_t afi, safi_t safi)
3212{
3213 int ret = 0;
3214 struct prefix_evpn evp;
3215 char buf[PREFIX_STRLEN];
3216
faafdfa8 3217 /* NOTE: Check needed as this is called per-route also. */
3218 if (!advertise_type5_routes(bgp_vrf, afi))
3219 return;
3220
31310b25 3221 build_type5_prefix_from_ip_prefix(&evp, p);
5424b7ba
MK
3222 ret = delete_evpn_type5_route(bgp_vrf, &evp);
3223 if (ret) {
3224 zlog_err(
996c9314
LB
3225 "%u failed to delete type-5 route for prefix %s in vrf %s",
3226 bgp_vrf->vrf_id, prefix2str(p, buf, sizeof(buf)),
3227 vrf_id_to_name(bgp_vrf->vrf_id));
5424b7ba
MK
3228 }
3229}
3230
342dd0c6 3231/* withdraw all type-5 routes for an address family */
996c9314 3232void bgp_evpn_withdraw_type5_routes(struct bgp *bgp_vrf, afi_t afi, safi_t safi)
342dd0c6 3233{
3234 struct bgp_table *table = NULL;
3235 struct bgp_node *rn = NULL;
25f2ca53 3236 struct bgp_info *ri;
342dd0c6 3237
faafdfa8 3238 /* Bail out early if we don't have to advertise type-5 routes. */
06d2e8f3
MK
3239 if (!advertise_type5_routes(bgp_vrf, afi))
3240 return;
3241
053905d2 3242 table = bgp_vrf->rib[afi][safi];
25f2ca53 3243 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
3244 /* Only care about "selected" routes - non-imported. */
3245 /* TODO: Support for AddPath for EVPN. */
3246 for (ri = rn->info; ri; ri = ri->next) {
996c9314
LB
3247 if (CHECK_FLAG(ri->flags, BGP_INFO_SELECTED)
3248 && (!ri->extra || !ri->extra->parent)) {
25f2ca53 3249 bgp_evpn_withdraw_type5_route(bgp_vrf, &rn->p,
3250 afi, safi);
3251 break;
3252 }
3253 }
3254 }
5424b7ba 3255}
342dd0c6 3256
2f69f6d3 3257/*
3258 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
3259 * to this function correspond to those of the source IP prefix (best
3260 * path in the case of the attr. In the case of a local prefix (when we
3261 * are advertising local subnets), the src_attr will be NULL.
3262 */
31310b25 3263void bgp_evpn_advertise_type5_route(struct bgp *bgp_vrf, struct prefix *p,
996c9314
LB
3264 struct attr *src_attr, afi_t afi,
3265 safi_t safi)
5424b7ba
MK
3266{
3267 int ret = 0;
3268 struct prefix_evpn evp;
3269 char buf[PREFIX_STRLEN];
3270
faafdfa8 3271 /* NOTE: Check needed as this is called per-route also. */
5424b7ba
MK
3272 if (!advertise_type5_routes(bgp_vrf, afi))
3273 return;
3274
31310b25 3275 build_type5_prefix_from_ip_prefix(&evp, p);
2f69f6d3 3276 ret = update_evpn_type5_route(bgp_vrf, &evp, src_attr);
3277 if (ret)
996c9314
LB
3278 zlog_err("%u: Failed to create type-5 route for prefix %s",
3279 bgp_vrf->vrf_id, prefix2str(p, buf, sizeof(buf)));
342dd0c6 3280}
3281
2f69f6d3 3282/* Inject all prefixes of a particular address-family (currently, IPv4 or
3283 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
3284 * advertisement is enabled.
3285 */
996c9314
LB
3286void bgp_evpn_advertise_type5_routes(struct bgp *bgp_vrf, afi_t afi,
3287 safi_t safi)
342dd0c6 3288{
3289 struct bgp_table *table = NULL;
3290 struct bgp_node *rn = NULL;
2f69f6d3 3291 struct bgp_info *ri;
342dd0c6 3292
faafdfa8 3293 /* Bail out early if we don't have to advertise type-5 routes. */
3294 if (!advertise_type5_routes(bgp_vrf, afi))
3295 return;
342dd0c6 3296
053905d2 3297 table = bgp_vrf->rib[afi][safi];
31310b25 3298 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
2f69f6d3 3299 /* Need to identify the "selected" route entry to use its
25f2ca53 3300 * attribute. Also, we only consider "non-imported" routes.
2f69f6d3 3301 * TODO: Support for AddPath for EVPN.
3302 */
3303 for (ri = rn->info; ri; ri = ri->next) {
996c9314
LB
3304 if (CHECK_FLAG(ri->flags, BGP_INFO_SELECTED)
3305 && (!ri->extra || !ri->extra->parent)) {
53c84f78
MK
3306
3307 /* apply the route-map */
3308 if (bgp_vrf->adv_cmd_rmap[afi][safi].map) {
3309 int ret = 0;
3310
996c9314
LB
3311 ret = route_map_apply(
3312 bgp_vrf->adv_cmd_rmap[afi][safi]
3313 .map,
3314 &rn->p, RMAP_BGP, ri);
53c84f78
MK
3315 if (ret == RMAP_DENYMATCH)
3316 continue;
3317 }
996c9314
LB
3318 bgp_evpn_advertise_type5_route(
3319 bgp_vrf, &rn->p, ri->attr, afi, safi);
2f69f6d3 3320 break;
3321 }
3322 }
31310b25 3323 }
342dd0c6 3324}
3325
996c9314 3326void evpn_rt_delete_auto(struct bgp *bgp, vni_t vni, struct list *rtl)
c581d8b0
MK
3327{
3328 struct listnode *node, *nnode, *node_to_del;
3329 struct ecommunity *ecom, *ecom_auto;
3330 struct ecommunity_val eval;
3331
3332 encode_route_target_as((bgp->as & 0xFFFF), vni, &eval);
3333
3334 ecom_auto = ecommunity_new();
3335 ecommunity_add_val(ecom_auto, &eval);
3336 node_to_del = NULL;
3337
3338 for (ALL_LIST_ELEMENTS(rtl, node, nnode, ecom)) {
3339 if (ecommunity_match(ecom, ecom_auto)) {
3340 ecommunity_free(&ecom);
3341 node_to_del = node;
3342 }
3343 }
3344
3345 if (node_to_del)
3346 list_delete_node(rtl, node_to_del);
3347
3348 ecommunity_free(&ecom_auto);
3349}
3350
3351void bgp_evpn_configure_import_rt_for_vrf(struct bgp *bgp_vrf,
10ebe1ab 3352 struct ecommunity *ecomadd)
c581d8b0 3353{
5ba238b7
MK
3354 /* uninstall routes from vrf */
3355 uninstall_routes_for_vrf(bgp_vrf);
10ebe1ab
MK
3356
3357 /* Cleanup the RT to VRF mapping */
3358 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
3359
c581d8b0
MK
3360 /* Remove auto generated RT */
3361 evpn_auto_rt_import_delete_for_vrf(bgp_vrf);
3362
3363 /* Add the newly configured RT to RT list */
3364 listnode_add_sort(bgp_vrf->vrf_import_rtl, ecomadd);
3365 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
3366
10ebe1ab
MK
3367 /* map VRF to its RTs */
3368 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
3369
5ba238b7
MK
3370 /* install routes matching the new VRF */
3371 install_routes_for_vrf(bgp_vrf);
c581d8b0
MK
3372}
3373
3374void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf,
3375 struct ecommunity *ecomdel)
3376{
3377 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
3378 struct ecommunity *ecom = NULL;
3379
5ba238b7
MK
3380 /* uninstall routes from vrf */
3381 uninstall_routes_for_vrf(bgp_vrf);
10ebe1ab
MK
3382
3383 /* Cleanup the RT to VRF mapping */
3384 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
3385
c581d8b0
MK
3386 /* remove the RT from the RT list */
3387 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
3388 if (ecommunity_match(ecom, ecomdel)) {
3389 ecommunity_free(&ecom);
3390 node_to_del = node;
3391 break;
3392 }
3393 }
3394
3395 if (node_to_del)
3396 list_delete_node(bgp_vrf->vrf_import_rtl, node_to_del);
3397
3398 /* fallback to auto import rt, if this was the last RT */
3399 if (list_isempty(bgp_vrf->vrf_import_rtl)) {
3400 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
3401 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
3402 }
3403
10ebe1ab
MK
3404 /* map VRFs to its RTs */
3405 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
3406
5ba238b7
MK
3407 /* install routes matching this new RT */
3408 install_routes_for_vrf(bgp_vrf);
c581d8b0
MK
3409}
3410
3411void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf,
3412 struct ecommunity *ecomadd)
3413{
3414 /* remove auto-generated RT */
3415 evpn_auto_rt_export_delete_for_vrf(bgp_vrf);
3416
3417 /* Add the new RT to the RT list */
3418 listnode_add_sort(bgp_vrf->vrf_export_rtl, ecomadd);
3419 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
3420
f1f8b53c 3421 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
c581d8b0
MK
3422}
3423
3424void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf,
3425 struct ecommunity *ecomdel)
3426{
3427 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
3428 struct ecommunity *ecom = NULL;
3429
3430 /* Remove the RT from the RT list */
3431 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_export_rtl, node, nnode, ecom)) {
3432 if (ecommunity_match(ecom, ecomdel)) {
3433 ecommunity_free(&ecom);
3434 node_to_del = node;
3435 break;
3436 }
3437 }
3438
3439 if (node_to_del)
3440 list_delete_node(bgp_vrf->vrf_export_rtl, node_to_del);
3441
3442 /* fall back to auto-generated RT if this was the last RT */
877702e7 3443 if (bgp_vrf->vrf_export_rtl && list_isempty(bgp_vrf->vrf_export_rtl)) {
c581d8b0
MK
3444 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
3445 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
3446 }
3447
f1f8b53c 3448 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
c581d8b0
MK
3449}
3450
2d48ee25 3451/*
3452 * Handle change to BGP router id. This is invoked twice by the change
3453 * handler, first before the router id has been changed and then after
3454 * the router id has been changed. The first invocation will result in
676f83b9 3455 * local routes for all VNIs/VRF being deleted and withdrawn and the next
2d48ee25 3456 * will result in the routes being re-advertised.
3457 */
d62a17ae 3458void bgp_evpn_handle_router_id_update(struct bgp *bgp, int withdraw)
2d48ee25 3459{
676f83b9 3460 if (withdraw) {
3461
3462 /* delete and withdraw all the type-5 routes
523cafc4 3463 stored in the global table for this vrf
3464 */
80b140af 3465 withdraw_router_id_vrf(bgp);
676f83b9 3466
3467 /* delete all the VNI routes (type-2/type-3) routes for all the
523cafc4 3468 * L2-VNIs
3469 */
d62a17ae 3470 hash_iterate(bgp->vnihash,
3471 (void (*)(struct hash_backet *,
3472 void *))withdraw_router_id_vni,
3473 bgp);
676f83b9 3474 } else {
3475
3476 /* advertise all routes in the vrf as type-5 routes with the new
523cafc4 3477 * RD
3478 */
80b140af 3479 update_router_id_vrf(bgp);
676f83b9 3480
3481 /* advertise all the VNI routes (type-2/type-3) routes with the
523cafc4 3482 * new RD
3483 */
d62a17ae 3484 hash_iterate(bgp->vnihash,
3485 (void (*)(struct hash_backet *,
3486 void *))update_router_id_vni,
3487 bgp);
676f83b9 3488 }
2d48ee25 3489}
3490
90e60aa7 3491/*
3492 * Handle change to export RT - update and advertise local routes.
3493 */
d62a17ae 3494int bgp_evpn_handle_export_rt_change(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 3495{
d62a17ae 3496 return update_routes_for_vni(bgp, vpn);
90e60aa7 3497}
3498
996c9314 3499void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw)
676f83b9 3500{
3501 if (withdraw)
3502 delete_withdraw_vrf_routes(bgp_vrf);
3503 else
3504 update_advertise_vrf_routes(bgp_vrf);
3505}
3506
90e60aa7 3507/*
3508 * Handle change to RD. This is invoked twice by the change handler,
3509 * first before the RD has been changed and then after the RD has
3510 * been changed. The first invocation will result in local routes
3511 * of this VNI being deleted and withdrawn and the next will result
3512 * in the routes being re-advertised.
3513 */
d62a17ae 3514void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
3515 int withdraw)
90e60aa7 3516{
d62a17ae 3517 if (withdraw)
3518 delete_withdraw_vni_routes(bgp, vpn);
3519 else
3520 update_advertise_vni_routes(bgp, vpn);
90e60aa7 3521}
3522
3523/*
3524 * Install routes for this VNI. Invoked upon change to Import RT.
3525 */
d62a17ae 3526int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 3527{
d62a17ae 3528 return install_routes_for_vni(bgp, vpn);
90e60aa7 3529}
3530
3531/*
3532 * Uninstall all routes installed for this VNI. Invoked upon change
3533 * to Import RT.
3534 */
d62a17ae 3535int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 3536{
d62a17ae 3537 return uninstall_routes_for_vni(bgp, vpn);
90e60aa7 3538}
3539
b16031a2 3540/*
b57ba6d2 3541 * TODO: Hardcoded for a maximum of 2 VNIs right now
b16031a2 3542 */
996c9314
LB
3543char *bgp_evpn_label2str(mpls_label_t *label, u_int32_t num_labels, char *buf,
3544 int len)
b16031a2 3545{
b57ba6d2 3546 vni_t vni1, vni2;
b16031a2 3547
b57ba6d2
MK
3548 vni1 = label2vni(label);
3549 if (num_labels == 2) {
996c9314 3550 vni2 = label2vni(label + 1);
b57ba6d2
MK
3551 snprintf(buf, len, "%u/%u", vni1, vni2);
3552 } else
3553 snprintf(buf, len, "%u", vni1);
d62a17ae 3554 return buf;
b16031a2 3555}
3556
9c92b5f7
MK
3557/*
3558 * Function to convert evpn route to json format.
3559 * NOTE: We don't use prefix2str as the output here is a bit different.
3560 */
57f7feb6 3561void bgp_evpn_route2json(struct prefix_evpn *p, json_object *json)
9c92b5f7 3562{
b682f6de 3563 char buf1[ETHER_ADDR_STRLEN];
3564 char buf2[PREFIX2STR_BUFFER];
9c92b5f7 3565
b682f6de 3566 if (!json)
3567 return;
9c92b5f7 3568
dff8f48d 3569 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
b682f6de 3570 json_object_int_add(json, "routeType", p->prefix.route_type);
3571 json_object_int_add(json, "ethTag", 0);
57f7feb6
MK
3572 json_object_int_add(json, "ipLen",
3573 IS_EVPN_PREFIX_IPADDR_V4(p)
3574 ? IPV4_MAX_BITLEN
3575 : IPV6_MAX_BITLEN);
b682f6de 3576 json_object_string_add(json, "ip",
57f7feb6
MK
3577 inet_ntoa(p->prefix.ip.ipaddr_v4));
3578 } else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
dff8f48d 3579 if (IS_EVPN_PREFIX_IPADDR_NONE(p)) {
57f7feb6
MK
3580 json_object_int_add(json, "routeType",
3581 p->prefix.route_type);
3582 json_object_int_add(
3583 json, "esi",
3584 0); /* TODO: we don't support esi yet */
3585 json_object_int_add(json, "ethTag", 0);
3586 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
3587 json_object_string_add(json, "mac",
3588 prefix_mac2str(&p->prefix.mac,
3589 buf1,
3590 sizeof(buf1)));
dff8f48d
MK
3591 } else {
3592 u_char family;
3593
57f7feb6
MK
3594 family = IS_EVPN_PREFIX_IPADDR_V4(p) ? AF_INET
3595 : AF_INET6;
dff8f48d 3596
b682f6de 3597 json_object_int_add(json, "routeType",
57f7feb6
MK
3598 p->prefix.route_type);
3599 json_object_int_add(
3600 json, "esi",
3601 0); /* TODO: we don't support esi yet */
b682f6de 3602 json_object_int_add(json, "ethTag", 0);
57f7feb6 3603 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
b682f6de 3604 json_object_string_add(json, "mac",
57f7feb6
MK
3605 prefix_mac2str(&p->prefix.mac,
3606 buf1,
3607 sizeof(buf1)));
b682f6de 3608 json_object_int_add(json, "ipLen",
57f7feb6
MK
3609 IS_EVPN_PREFIX_IPADDR_V4(p)
3610 ? IPV4_MAX_BITLEN
3611 : IPV6_MAX_BITLEN);
3612 json_object_string_add(
3613 json, "ip",
3614 inet_ntop(family, &p->prefix.ip.ip.addr, buf2,
3615 PREFIX2STR_BUFFER));
dff8f48d
MK
3616 }
3617 } else {
3618 /* Currently, this is to cater to other AF_ETHERNET code. */
3619 }
9c92b5f7
MK
3620}
3621
520d5d76 3622/*
3623 * Function to convert evpn route to string.
3624 * NOTE: We don't use prefix2str as the output here is a bit different.
3625 */
d62a17ae 3626char *bgp_evpn_route2str(struct prefix_evpn *p, char *buf, int len)
3627{
3628 char buf1[ETHER_ADDR_STRLEN];
3629 char buf2[PREFIX2STR_BUFFER];
3630
3631 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
3632 snprintf(buf, len, "[%d]:[0]:[%d]:[%s]", p->prefix.route_type,
3633 IS_EVPN_PREFIX_IPADDR_V4(p) ? IPV4_MAX_BITLEN
3634 : IPV6_MAX_BITLEN,
3635 inet_ntoa(p->prefix.ip.ipaddr_v4));
3636 } else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
3637 if (IS_EVPN_PREFIX_IPADDR_NONE(p))
3638 snprintf(buf, len, "[%d]:[0]:[0]:[%d]:[%s]",
28328ea9 3639 p->prefix.route_type, 8 * ETH_ALEN,
d62a17ae 3640 prefix_mac2str(&p->prefix.mac, buf1,
3641 sizeof(buf1)));
3642 else {
3643 u_char family;
3644
3645 family = IS_EVPN_PREFIX_IPADDR_V4(p) ? AF_INET
3646 : AF_INET6;
3647 snprintf(buf, len, "[%d]:[0]:[0]:[%d]:[%s]:[%d]:[%s]",
28328ea9 3648 p->prefix.route_type, 8 * ETH_ALEN,
d62a17ae 3649 prefix_mac2str(&p->prefix.mac, buf1,
3650 sizeof(buf1)),
3651 family == AF_INET ? IPV4_MAX_BITLEN
3652 : IPV6_MAX_BITLEN,
3653 inet_ntop(family, &p->prefix.ip.ip.addr, buf2,
3654 PREFIX2STR_BUFFER));
3655 }
342dd0c6 3656 } else if (p->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE) {
b3628c70 3657 snprintf(buf, len, "[%d]:[0]:[0]:[%d]:[%s]",
996c9314
LB
3658 p->prefix.route_type, p->prefix.ip_prefix_length,
3659 IS_EVPN_PREFIX_IPADDR_V4(p)
3660 ? inet_ntoa(p->prefix.ip.ipaddr_v4)
3661 : inet6_ntoa(p->prefix.ip.ipaddr_v6));
d62a17ae 3662 } else {
b03b8898 3663 /* For EVPN route types not supported yet. */
f9aa3e55
QY
3664 snprintf(buf, len, "(unsupported route type %d)",
3665 p->prefix.route_type);
d62a17ae 3666 }
3667
3668 return (buf);
520d5d76 3669}
3670
128ea8ab 3671/*
3672 * Encode EVPN prefix in Update (MP_REACH)
3673 */
d62a17ae 3674void bgp_evpn_encode_prefix(struct stream *s, struct prefix *p,
996c9314
LB
3675 struct prefix_rd *prd, mpls_label_t *label,
3676 u_int32_t num_labels, struct attr *attr,
3677 int addpath_encode, u_int32_t addpath_tx_id)
d62a17ae 3678{
3679 struct prefix_evpn *evp = (struct prefix_evpn *)p;
b57ba6d2 3680 int len, ipa_len = 0;
d62a17ae 3681
3682 if (addpath_encode)
3683 stream_putl(s, addpath_tx_id);
3684
3685 /* Route type */
3686 stream_putc(s, evp->prefix.route_type);
3687
3688 switch (evp->prefix.route_type) {
3689 case BGP_EVPN_MAC_IP_ROUTE:
3690 if (IS_EVPN_PREFIX_IPADDR_V4(evp))
3691 ipa_len = IPV4_MAX_BYTELEN;
3692 else if (IS_EVPN_PREFIX_IPADDR_V6(evp))
3693 ipa_len = IPV6_MAX_BYTELEN;
b57ba6d2
MK
3694 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
3695 len = 8 + 10 + 4 + 1 + 6 + 1 + ipa_len + 3;
3696 if (ipa_len && num_labels > 1) /* There are 2 VNIs */
3697 len += 3;
3698 stream_putc(s, len);
996c9314
LB
3699 stream_put(s, prd->val, 8); /* RD */
3700 stream_put(s, 0, 10); /* ESI */
3701 stream_putl(s, 0); /* Ethernet Tag ID */
28328ea9 3702 stream_putc(s, 8 * ETH_ALEN); /* Mac Addr Len - bits */
d62a17ae 3703 stream_put(s, evp->prefix.mac.octet, 6); /* Mac Addr */
3704 stream_putc(s, 8 * ipa_len); /* IP address Length */
996c9314 3705 if (ipa_len) /* IP */
b57ba6d2
MK
3706 stream_put(s, &evp->prefix.ip.ip.addr, ipa_len);
3707 /* 1st label is the L2 VNI */
3708 stream_put(s, label, BGP_LABEL_BYTES);
3709 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
3710 if (ipa_len && num_labels > 1)
996c9314 3711 stream_put(s, label + 1, BGP_LABEL_BYTES);
d62a17ae 3712 break;
3713
3714 case BGP_EVPN_IMET_ROUTE:
3715 stream_putc(s, 17); // TODO: length - assumes IPv4 address
3716 stream_put(s, prd->val, 8); /* RD */
3717 stream_putl(s, 0); /* Ethernet Tag ID */
3718 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
3719 /* Originating Router's IP Addr */
3720 stream_put_in_addr(s, &evp->prefix.ip.ipaddr_v4);
3721 break;
3722
3723 case BGP_EVPN_IP_PREFIX_ROUTE:
3724 /* TODO: AddPath support. */
b57ba6d2 3725 evpn_mpattr_encode_type5(s, p, prd, label, num_labels, attr);
d62a17ae 3726 break;
3727
3728 default:
3729 break;
3730 }
3731}
3732
3733int bgp_nlri_parse_evpn(struct peer *peer, struct attr *attr,
3734 struct bgp_nlri *packet, int withdraw)
3735{
3736 u_char *pnt;
3737 u_char *lim;
3738 afi_t afi;
3739 safi_t safi;
3740 u_int32_t addpath_id;
3741 int addpath_encoded;
3742 int psize = 0;
3743 u_char rtype;
3744 u_char rlen;
3745 struct prefix p;
3746
3747 /* Check peer status. */
3748 if (peer->status != Established) {
3749 zlog_err("%u:%s - EVPN update received in state %d",
3750 peer->bgp->vrf_id, peer->host, peer->status);
3751 return -1;
3752 }
3753
3754 /* Start processing the NLRI - there may be multiple in the MP_REACH */
3755 pnt = packet->nlri;
3756 lim = pnt + packet->length;
3757 afi = packet->afi;
3758 safi = packet->safi;
3759 addpath_id = 0;
3760
3761 addpath_encoded =
3762 (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV)
3763 && CHECK_FLAG(peer->af_cap[afi][safi],
3764 PEER_CAP_ADDPATH_AF_TX_RCV));
3765
3766 for (; pnt < lim; pnt += psize) {
3767 /* Clear prefix structure. */
3768 memset(&p, 0, sizeof(struct prefix));
3769
3770 /* Deal with path-id if AddPath is supported. */
3771 if (addpath_encoded) {
3772 /* When packet overflow occurs return immediately. */
3773 if (pnt + BGP_ADDPATH_ID_LEN > lim)
3774 return -1;
3775
3776 addpath_id = ntohl(*((uint32_t *)pnt));
3777 pnt += BGP_ADDPATH_ID_LEN;
3778 }
3779
3780 /* All EVPN NLRI types start with type and length. */
3781 if (pnt + 2 > lim)
3782 return -1;
3783
3784 rtype = *pnt++;
3785 psize = rlen = *pnt++;
3786
3787 /* When packet overflow occur return immediately. */
3788 if (pnt + psize > lim)
3789 return -1;
3790
3791 switch (rtype) {
3792 case BGP_EVPN_MAC_IP_ROUTE:
3793 if (process_type2_route(peer, afi, safi,
3794 withdraw ? NULL : attr, pnt,
3795 psize, addpath_id)) {
3796 zlog_err(
3797 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
3798 peer->bgp->vrf_id, peer->host, psize);
3799 return -1;
3800 }
3801 break;
3802
3803 case BGP_EVPN_IMET_ROUTE:
3804 if (process_type3_route(peer, afi, safi,
3805 withdraw ? NULL : attr, pnt,
3806 psize, addpath_id)) {
3807 zlog_err(
3808 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
3809 peer->bgp->vrf_id, peer->host, psize);
3810 return -1;
3811 }
3812 break;
3813
3814 case BGP_EVPN_IP_PREFIX_ROUTE:
3815 if (process_type5_route(peer, afi, safi, attr, pnt,
3816 psize, addpath_id, withdraw)) {
3817 zlog_err(
3818 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
3819 peer->bgp->vrf_id, peer->host, psize);
3820 return -1;
3821 }
3822 break;
3823
3824 default:
3825 break;
3826 }
3827 }
3828
3829 /* Packet length consistency check. */
3830 if (pnt != lim)
3831 return -1;
3832
3833 return 0;
128ea8ab 3834}
3835
10ebe1ab
MK
3836/*
3837 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
3838 * The mapping will be used during route processing.
3839 * bgp_def: default bgp instance
3840 * bgp_vrf: specific bgp vrf instance on which RT is configured
3841 */
3842void bgp_evpn_map_vrf_to_its_rts(struct bgp *bgp_vrf)
3843{
3844 int i = 0;
3845 struct ecommunity_val *eval = NULL;
3846 struct listnode *node = NULL, *nnode = NULL;
3847 struct ecommunity *ecom = NULL;
3848
3849 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
3850 for (i = 0; i < ecom->size; i++) {
3851 eval = (struct ecommunity_val *)(ecom->val
3852 + (i
3853 * ECOMMUNITY_SIZE));
3854 map_vrf_to_rt(bgp_vrf, eval);
3855 }
3856 }
3857}
3858
3859/*
3860 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
3861 */
3862void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *bgp_vrf)
3863{
3864 int i;
3865 struct ecommunity_val *eval;
3866 struct listnode *node, *nnode;
3867 struct ecommunity *ecom;
3868
3869 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
3870 for (i = 0; i < ecom->size; i++) {
3871 struct vrf_irt_node *irt;
3872 struct ecommunity_val eval_tmp;
3873
3874 eval = (struct ecommunity_val *)(ecom->val
3875 + (i
3876 * ECOMMUNITY_SIZE));
3877 /* If using "automatic" RT, we only care about the
3878 * local-admin sub-field.
3879 * This is to facilitate using VNI as the RT for EBGP
3880 * peering too.
3881 */
3882 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
3883 if (!CHECK_FLAG(bgp_vrf->vrf_flags,
3884 BGP_VRF_IMPORT_RT_CFGD))
3885 mask_ecom_global_admin(&eval_tmp, eval);
3886
3887 irt = lookup_vrf_import_rt(&eval_tmp);
3888 if (irt)
3889 unmap_vrf_from_rt(bgp_vrf, irt);
3890 }
3891 }
3892}
3893
3894
128ea8ab 3895/*
3896 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
3897 * The mapping will be used during route processing.
3898 */
d62a17ae 3899void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3900{
d62a17ae 3901 int i;
3902 struct ecommunity_val *eval;
3903 struct listnode *node, *nnode;
3904 struct ecommunity *ecom;
128ea8ab 3905
d62a17ae 3906 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
3907 for (i = 0; i < ecom->size; i++) {
3908 eval = (struct ecommunity_val *)(ecom->val
3909 + (i
3910 * ECOMMUNITY_SIZE));
3911 map_vni_to_rt(bgp, vpn, eval);
3912 }
3913 }
128ea8ab 3914}
3915
3916/*
3917 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
3918 */
d62a17ae 3919void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3920{
d62a17ae 3921 int i;
3922 struct ecommunity_val *eval;
3923 struct listnode *node, *nnode;
3924 struct ecommunity *ecom;
128ea8ab 3925
d62a17ae 3926 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
3927 for (i = 0; i < ecom->size; i++) {
3928 struct irt_node *irt;
3929 struct ecommunity_val eval_tmp;
128ea8ab 3930
d62a17ae 3931 eval = (struct ecommunity_val *)(ecom->val
3932 + (i
3933 * ECOMMUNITY_SIZE));
3934 /* If using "automatic" RT, we only care about the
3935 * local-admin sub-field.
3936 * This is to facilitate using VNI as the RT for EBGP
3937 * peering too.
3938 */
3939 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
3940 if (!is_import_rt_configured(vpn))
3941 mask_ecom_global_admin(&eval_tmp, eval);
128ea8ab 3942
d62a17ae 3943 irt = lookup_import_rt(bgp, &eval_tmp);
3944 if (irt)
3945 unmap_vni_from_rt(bgp, vpn, irt);
3946 }
3947 }
128ea8ab 3948}
3949
3950/*
3951 * Derive Import RT automatically for VNI and map VNI to RT.
3952 * The mapping will be used during route processing.
3953 */
d62a17ae 3954void bgp_evpn_derive_auto_rt_import(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3955{
c581d8b0 3956 form_auto_rt(bgp, vpn->vni, vpn->import_rtl);
d62a17ae 3957 UNSET_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD);
128ea8ab 3958
d62a17ae 3959 /* Map RT to VNI */
3960 bgp_evpn_map_vni_to_its_rts(bgp, vpn);
128ea8ab 3961}
3962
3963/*
3964 * Derive Export RT automatically for VNI.
3965 */
d62a17ae 3966void bgp_evpn_derive_auto_rt_export(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3967{
c581d8b0 3968 form_auto_rt(bgp, vpn->vni, vpn->export_rtl);
d62a17ae 3969 UNSET_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD);
128ea8ab 3970}
3971
676f83b9 3972/*
3973 * Derive RD automatically for VNI using passed information - it
3974 * is of the form RouterId:unique-id-for-vni.
3975 */
3976void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp)
3977{
3978 char buf[100];
3979
3980 bgp->vrf_prd.family = AF_UNSPEC;
3981 bgp->vrf_prd.prefixlen = 64;
3982 sprintf(buf, "%s:%hu", inet_ntoa(bgp->router_id), bgp->vrf_rd_id);
3983 str2prefix_rd(buf, &bgp->vrf_prd);
3984}
3985
128ea8ab 3986/*
3987 * Derive RD automatically for VNI using passed information - it
3988 * is of the form RouterId:unique-id-for-vni.
3989 */
d62a17ae 3990void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3991{
d62a17ae 3992 char buf[100];
128ea8ab 3993
d62a17ae 3994 vpn->prd.family = AF_UNSPEC;
3995 vpn->prd.prefixlen = 64;
3996 sprintf(buf, "%s:%hu", inet_ntoa(bgp->router_id), vpn->rd_id);
cbb65f5e 3997 (void)str2prefix_rd(buf, &vpn->prd);
d62a17ae 3998 UNSET_FLAG(vpn->flags, VNI_FLAG_RD_CFGD);
128ea8ab 3999}
4000
4001/*
4002 * Lookup VNI.
4003 */
d62a17ae 4004struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni)
128ea8ab 4005{
d62a17ae 4006 struct bgpevpn *vpn;
4007 struct bgpevpn tmp;
128ea8ab 4008
d62a17ae 4009 memset(&tmp, 0, sizeof(struct bgpevpn));
4010 tmp.vni = vni;
4011 vpn = hash_lookup(bgp->vnihash, &tmp);
4012 return vpn;
128ea8ab 4013}
4014
4015/*
4016 * Create a new vpn - invoked upon configuration or zebra notification.
4017 */
d62a17ae 4018struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
29c53922
MK
4019 struct in_addr originator_ip,
4020 vrf_id_t tenant_vrf_id)
128ea8ab 4021{
d62a17ae 4022 struct bgpevpn *vpn;
128ea8ab 4023
d62a17ae 4024 if (!bgp)
4025 return NULL;
128ea8ab 4026
d62a17ae 4027 vpn = XCALLOC(MTYPE_BGP_EVPN, sizeof(struct bgpevpn));
4028 if (!vpn)
4029 return NULL;
128ea8ab 4030
d62a17ae 4031 /* Set values - RD and RT set to defaults. */
4032 vpn->vni = vni;
4033 vpn->originator_ip = originator_ip;
29c53922 4034 vpn->tenant_vrf_id = tenant_vrf_id;
128ea8ab 4035
d62a17ae 4036 /* Initialize route-target import and export lists */
4037 vpn->import_rtl = list_new();
4038 vpn->import_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
4039 vpn->export_rtl = list_new();
4040 vpn->export_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
e9eb5f63 4041 bf_assign_index(bm->rd_idspace, vpn->rd_id);
d62a17ae 4042 derive_rd_rt_for_vni(bgp, vpn);
128ea8ab 4043
d62a17ae 4044 /* Initialize EVPN route table. */
4045 vpn->route_table = bgp_table_init(AFI_L2VPN, SAFI_EVPN);
128ea8ab 4046
d62a17ae 4047 /* Add to hash */
4048 if (!hash_get(bgp->vnihash, vpn, hash_alloc_intern)) {
4049 XFREE(MTYPE_BGP_EVPN, vpn);
4050 return NULL;
4051 }
6a8657d0
MK
4052
4053 /* add to l2vni list on corresponding vrf */
4054 bgpevpn_link_to_l3vni(vpn);
4055
d62a17ae 4056 QOBJ_REG(vpn, bgpevpn);
4057 return vpn;
128ea8ab 4058}
4059
4060/*
4061 * Free a given VPN - called in multiple scenarios such as zebra
4062 * notification, configuration being deleted, advertise-all-vni disabled etc.
4063 * This just frees appropriate memory, caller should have taken other
4064 * needed actions.
4065 */
d62a17ae 4066void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 4067{
6a8657d0 4068 bgpevpn_unlink_from_l3vni(vpn);
d62a17ae 4069 bgp_table_unlock(vpn->route_table);
4070 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
affe9e99
DS
4071 list_delete_and_null(&vpn->import_rtl);
4072 list_delete_and_null(&vpn->export_rtl);
e9eb5f63 4073 bf_release_index(bm->rd_idspace, vpn->rd_id);
d62a17ae 4074 hash_release(bgp->vnihash, vpn);
4075 QOBJ_UNREG(vpn);
4076 XFREE(MTYPE_BGP_EVPN, vpn);
128ea8ab 4077}
4078
4079/*
4080 * Import route into matching VNI(s).
4081 */
d62a17ae 4082int bgp_evpn_import_route(struct bgp *bgp, afi_t afi, safi_t safi,
4083 struct prefix *p, struct bgp_info *ri)
128ea8ab 4084{
d62a17ae 4085 return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 1);
128ea8ab 4086}
4087
4088/*
4089 * Unimport route from matching VNI(s).
4090 */
d62a17ae 4091int bgp_evpn_unimport_route(struct bgp *bgp, afi_t afi, safi_t safi,
4092 struct prefix *p, struct bgp_info *ri)
128ea8ab 4093{
d62a17ae 4094 return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 0);
128ea8ab 4095}
4096
db0e1937
MK
4097/* filter routes which have martian next hops */
4098int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp *bgp)
4099{
0291c246
MK
4100 afi_t afi;
4101 safi_t safi;
4102 struct bgp_node *rd_rn, *rn;
4103 struct bgp_table *table;
4104 struct bgp_info *ri;
db0e1937
MK
4105
4106 afi = AFI_L2VPN;
4107 safi = SAFI_EVPN;
4108
4109 /* Walk entire global routing table and evaluate routes which could be
4110 * imported into this VPN. Note that we cannot just look at the routes
4111 * for the VNI's RD -
4112 * remote routes applicable for this VNI could have any RD.
4113 */
4114 /* EVPN routes are a 2-level table. */
4115 for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
4116 rd_rn = bgp_route_next(rd_rn)) {
4117 table = (struct bgp_table *)(rd_rn->info);
4118 if (!table)
4119 continue;
4120
4121 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
4122
4123 for (ri = rn->info; ri; ri = ri->next) {
4124
4125 /* Consider "valid" remote routes applicable for
4126 * this VNI. */
4127 if (!(ri->type == ZEBRA_ROUTE_BGP
4128 && ri->sub_type == BGP_ROUTE_NORMAL))
4129 continue;
4130
60466a63 4131 if (bgp_nexthop_self(bgp, ri->attr->nexthop)) {
db0e1937
MK
4132
4133 char attr_str[BUFSIZ];
4134 char pbuf[PREFIX_STRLEN];
4135
4136 bgp_dump_attr(ri->attr, attr_str,
4137 BUFSIZ);
4138
4139 if (bgp_debug_update(ri->peer, &rn->p,
4140 NULL, 1))
4141 zlog_debug(
b682f6de 4142 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
db0e1937
MK
4143 bgp->vrf_id,
4144 prefix2str(
60466a63 4145 &rn->p, pbuf,
db0e1937
MK
4146 sizeof(pbuf)),
4147 attr_str);
4148
4149 bgp_evpn_unimport_route(bgp, afi, safi,
4150 &rn->p, ri);
4151
60466a63
QY
4152 bgp_rib_remove(rn, ri, ri->peer, afi,
4153 safi);
db0e1937 4154 }
db0e1937
MK
4155 }
4156 }
4157 }
4158
4159 return 0;
4160}
4161
128ea8ab 4162/*
4163 * Handle del of a local MACIP.
4164 */
d62a17ae 4165int bgp_evpn_local_macip_del(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
4166 struct ipaddr *ip)
128ea8ab 4167{
d62a17ae 4168 struct bgpevpn *vpn;
4169 struct prefix_evpn p;
128ea8ab 4170
d62a17ae 4171 if (!bgp->vnihash) {
4172 zlog_err("%u: VNI hash not created", bgp->vrf_id);
4173 return -1;
4174 }
128ea8ab 4175
d62a17ae 4176 /* Lookup VNI hash - should exist. */
4177 vpn = bgp_evpn_lookup_vni(bgp, vni);
4178 if (!vpn || !is_vni_live(vpn)) {
4179 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL",
4180 bgp->vrf_id, vni, vpn ? "not live" : "not found");
4181 return -1;
4182 }
128ea8ab 4183
d62a17ae 4184 /* Remove EVPN type-2 route and schedule for processing. */
4185 build_evpn_type2_prefix(&p, mac, ip);
4186 delete_evpn_route(bgp, vpn, &p);
128ea8ab 4187
d62a17ae 4188 return 0;
128ea8ab 4189}
4190
4191/*
4192 * Handle add of a local MACIP.
4193 */
d62a17ae 4194int bgp_evpn_local_macip_add(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
1a98c087 4195 struct ipaddr *ip, u_char flags)
128ea8ab 4196{
d62a17ae 4197 struct bgpevpn *vpn;
4198 struct prefix_evpn p;
128ea8ab 4199
d62a17ae 4200 if (!bgp->vnihash) {
4201 zlog_err("%u: VNI hash not created", bgp->vrf_id);
4202 return -1;
4203 }
128ea8ab 4204
d62a17ae 4205 /* Lookup VNI hash - should exist. */
4206 vpn = bgp_evpn_lookup_vni(bgp, vni);
4207 if (!vpn || !is_vni_live(vpn)) {
4208 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD",
4209 bgp->vrf_id, vni, vpn ? "not live" : "not found");
4210 return -1;
4211 }
128ea8ab 4212
d62a17ae 4213 /* Create EVPN type-2 route and schedule for processing. */
4214 build_evpn_type2_prefix(&p, mac, ip);
1a98c087 4215 if (update_evpn_route(bgp, vpn, &p, flags)) {
d62a17ae 4216 char buf[ETHER_ADDR_STRLEN];
4217 char buf2[INET6_ADDRSTRLEN];
128ea8ab 4218
d62a17ae 4219 zlog_err(
ead40654 4220 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
1a98c087 4221 bgp->vrf_id, vpn->vni,
996c9314
LB
4222 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY)
4223 ? "sticky gateway"
4224 : "",
d62a17ae 4225 prefix_mac2str(mac, buf, sizeof(buf)),
996c9314 4226 ipaddr2str(ip, buf2, sizeof(buf2)), flags);
d62a17ae 4227 return -1;
4228 }
128ea8ab 4229
d62a17ae 4230 return 0;
128ea8ab 4231}
4232
6a8657d0
MK
4233static void link_l2vni_hash_to_l3vni(struct hash_backet *backet,
4234 struct bgp *bgp_vrf)
4235{
4236 struct bgpevpn *vpn = NULL;
4237 struct bgp *bgp_def = NULL;
4238
4239 bgp_def = bgp_get_default();
4240 assert(bgp_def);
4241
4242 vpn = (struct bgpevpn *)backet->data;
4243 if (vpn->tenant_vrf_id == bgp_vrf->vrf_id)
4244 bgpevpn_link_to_l3vni(vpn);
4245}
4246
996c9314
LB
4247int bgp_evpn_local_l3vni_add(vni_t l3vni, vrf_id_t vrf_id, struct ethaddr *rmac,
4248 struct in_addr originator_ip, int filter)
fe1dc5a3
MK
4249{
4250 struct bgp *bgp_vrf = NULL; /* bgp VRF instance */
4251 struct bgp *bgp_def = NULL; /* default bgp instance */
f1f8b53c
MK
4252 struct listnode *node = NULL;
4253 struct bgpevpn *vpn = NULL;
fe1dc5a3
MK
4254 as_t as = 0;
4255
4256 /* get the default instamce - required to get the AS number for VRF
523cafc4 4257 * auto-creatio
4258 */
fe1dc5a3
MK
4259 bgp_def = bgp_get_default();
4260 if (!bgp_def) {
996c9314
LB
4261 zlog_err(
4262 "Cannot process L3VNI %u ADD - default BGP instance not yet created",
4263 l3vni);
fe1dc5a3
MK
4264 return -1;
4265 }
4266 as = bgp_def->as;
4267
4268 /* if the BGP vrf instance doesnt exist - create one */
0b5131c9 4269 bgp_vrf = bgp_lookup_by_name(vrf_id_to_name(vrf_id));
fe1dc5a3
MK
4270 if (!bgp_vrf) {
4271
4272 int ret = 0;
4273
4274 ret = bgp_get(&bgp_vrf, &as, vrf_id_to_name(vrf_id),
4275 BGP_INSTANCE_TYPE_VRF);
4276 switch (ret) {
4277 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET:
4278 zlog_err("'bgp multiple-instance' not present\n");
4279 return -1;
4280 case BGP_ERR_AS_MISMATCH:
4281 zlog_err("BGP is already running; AS is %u\n", as);
4282 return -1;
4283 case BGP_ERR_INSTANCE_MISMATCH:
4284 zlog_err("BGP instance name and AS number mismatch\n");
4285 return -1;
4286 }
4287
4288 /* mark as auto created */
4289 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO);
4290 }
4291
4292 /* associate with l3vni */
4293 bgp_vrf->l3vni = l3vni;
4294
4295 /* set the router mac - to be used in mac-ip routes for this vrf */
4296 memcpy(&bgp_vrf->rmac, rmac, sizeof(struct ethaddr));
4297
b67a60d2 4298 /* set the originator ip */
4299 bgp_vrf->originator_ip = originator_ip;
4300
c48d9f5f
MK
4301 /* set the right filter - are we using l3vni only for prefix routes? */
4302 if (filter)
4303 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
4304
c581d8b0
MK
4305 /* auto derive RD/RT */
4306 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
4307 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
4308 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD))
4309 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
676f83b9 4310 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
fe1dc5a3 4311
6a8657d0
MK
4312 /* link all corresponding l2vnis */
4313 hash_iterate(bgp_def->vnihash,
996c9314
LB
4314 (void (*)(struct hash_backet *,
4315 void *))link_l2vni_hash_to_l3vni,
6a8657d0
MK
4316 bgp_vrf);
4317
c48d9f5f
MK
4318 /* Only update all corresponding type-2 routes if we are advertising two
4319 * labels along with type-2 routes
4320 */
4321 if (!filter)
4322 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
4323 update_routes_for_vni(bgp_def, vpn);
fe1dc5a3 4324
06d2e8f3
MK
4325 /* advertise type-5 routes if needed */
4326 update_advertise_vrf_routes(bgp_vrf);
4327
5ba238b7
MK
4328 /* install all remote routes belonging to this l3vni into correspondng
4329 * vrf */
4330 install_routes_for_vrf(bgp_vrf);
fe1dc5a3
MK
4331
4332 return 0;
4333}
4334
996c9314 4335int bgp_evpn_local_l3vni_del(vni_t l3vni, vrf_id_t vrf_id)
fe1dc5a3
MK
4336{
4337 struct bgp *bgp_vrf = NULL; /* bgp vrf instance */
f1f8b53c
MK
4338 struct bgp *bgp_def = NULL; /* default bgp instance */
4339 struct listnode *node = NULL;
4340 struct bgpevpn *vpn = NULL;
fe1dc5a3
MK
4341
4342 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
4343 if (!bgp_vrf) {
996c9314
LB
4344 zlog_err(
4345 "Cannot process L3VNI %u Del - Could not find BGP instance",
4346 l3vni);
fe1dc5a3
MK
4347 return -1;
4348 }
4349
f1f8b53c
MK
4350 bgp_def = bgp_get_default();
4351 if (!bgp_def) {
996c9314
LB
4352 zlog_err(
4353 "Cannot process L3VNI %u Del - Could not find default BGP instance",
4354 l3vni);
f1f8b53c
MK
4355 return -1;
4356 }
4357
1eb88002 4358 /* unimport remote routes from VRF, if it is AUTO vrf bgp_delete will
523cafc4 4359 * take care of uninstalling the routes from zebra
4360 */
1eb88002
MK
4361 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO))
4362 uninstall_routes_for_vrf(bgp_vrf);
5ba238b7 4363
06d2e8f3
MK
4364 /* delete/withdraw all type-5 routes */
4365 delete_withdraw_vrf_routes(bgp_vrf);
4366
fe1dc5a3
MK
4367 /* remove the l3vni from vrf instance */
4368 bgp_vrf->l3vni = 0;
4369
4370 /* remove the Rmac from the BGP vrf */
4371 memset(&bgp_vrf->rmac, 0, sizeof(struct ethaddr));
4372
c581d8b0 4373 /* delete RD/RT */
23a06e11 4374 if (bgp_vrf->vrf_import_rtl && !list_isempty(bgp_vrf->vrf_import_rtl)) {
10ebe1ab 4375 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
5ba238b7 4376 list_delete_all_node(bgp_vrf->vrf_import_rtl);
23a06e11
MK
4377 }
4378 if (bgp_vrf->vrf_export_rtl && !list_isempty(bgp_vrf->vrf_export_rtl)) {
5ba238b7 4379 list_delete_all_node(bgp_vrf->vrf_export_rtl);
23a06e11 4380 }
fe1dc5a3 4381
f1f8b53c 4382 /* update all corresponding local mac-ip routes */
c48d9f5f
MK
4383 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY)) {
4384 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) {
4385 UNSET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
4386 update_routes_for_vni(bgp_def, vpn);
4387 }
4388 }
fe1dc5a3
MK
4389
4390 /* Delete the instance if it was autocreated */
4391 if (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO))
4392 bgp_delete(bgp_vrf);
4393
4394 return 0;
4395}
4396
128ea8ab 4397/*
4398 * Handle del of a local VNI.
4399 */
d62a17ae 4400int bgp_evpn_local_vni_del(struct bgp *bgp, vni_t vni)
128ea8ab 4401{
d62a17ae 4402 struct bgpevpn *vpn;
128ea8ab 4403
d62a17ae 4404 if (!bgp->vnihash) {
4405 zlog_err("%u: VNI hash not created", bgp->vrf_id);
4406 return -1;
4407 }
128ea8ab 4408
d62a17ae 4409 /* Locate VNI hash */
4410 vpn = bgp_evpn_lookup_vni(bgp, vni);
4411 if (!vpn) {
4412 zlog_warn("%u: VNI hash entry for VNI %u not found at DEL",
4413 bgp->vrf_id, vni);
4414 return 0;
4415 }
128ea8ab 4416
d62a17ae 4417 /* Remove all local EVPN routes and schedule for processing (to
4418 * withdraw from peers).
4419 */
4420 delete_routes_for_vni(bgp, vpn);
128ea8ab 4421
db0e1937
MK
4422 /*
4423 * tunnel is no longer active, del tunnel ip address from tip_hash
4424 */
4425 bgp_tip_del(bgp, &vpn->originator_ip);
4426
d62a17ae 4427 /* Clear "live" flag and see if hash needs to be freed. */
4428 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
4429 if (!is_vni_configured(vpn))
4430 bgp_evpn_free(bgp, vpn);
128ea8ab 4431
d62a17ae 4432 return 0;
128ea8ab 4433}
4434
4435/*
d1911c26 4436 * Handle add (or update) of a local VNI. The VNI changes we care
4437 * about are for the local-tunnel-ip and the (tenant) VRF.
128ea8ab 4438 */
d62a17ae 4439int bgp_evpn_local_vni_add(struct bgp *bgp, vni_t vni,
996c9314 4440 struct in_addr originator_ip, vrf_id_t tenant_vrf_id)
d62a17ae 4441{
4442 struct bgpevpn *vpn;
4443 struct prefix_evpn p;
4444
4445 if (!bgp->vnihash) {
4446 zlog_err("%u: VNI hash not created", bgp->vrf_id);
4447 return -1;
4448 }
4449
4450 /* Lookup VNI. If present and no change, exit. */
4451 vpn = bgp_evpn_lookup_vni(bgp, vni);
ddd16ed5 4452 if (vpn) {
29c53922 4453
d1911c26 4454 if (is_vni_live(vpn)
4455 && IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip)
4456 && vpn->tenant_vrf_id == tenant_vrf_id)
4457 /* Probably some other param has changed that we don't
4458 * care about. */
4459 return 0;
4460
4461 /* Update tenant_vrf_id if it has changed. */
6a8657d0
MK
4462 if (vpn->tenant_vrf_id != tenant_vrf_id) {
4463 bgpevpn_unlink_from_l3vni(vpn);
29c53922 4464 vpn->tenant_vrf_id = tenant_vrf_id;
6a8657d0
MK
4465 bgpevpn_link_to_l3vni(vpn);
4466 }
29c53922 4467
d1911c26 4468 /* If tunnel endpoint IP has changed, update (and delete prior
4469 * type-3 route, if needed.)
4470 */
4471 if (!IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip))
4472 handle_tunnel_ip_change(bgp, vpn, originator_ip);
d62a17ae 4473
d1911c26 4474 /* Update all routes with new endpoint IP and/or export RT
4475 * for VRFs
4476 */
4477 if (is_vni_live(vpn))
4478 update_routes_for_vni(bgp, vpn);
d62a17ae 4479 }
4480
4481 /* Create or update as appropriate. */
4482 if (!vpn) {
29c53922 4483 vpn = bgp_evpn_new(bgp, vni, originator_ip, tenant_vrf_id);
d62a17ae 4484 if (!vpn) {
4485 zlog_err(
4486 "%u: Failed to allocate VNI entry for VNI %u - at Add",
4487 bgp->vrf_id, vni);
4488 return -1;
4489 }
4490 }
4491
db0e1937 4492 /* if the VNI is live already, there is nothing more to do */
ddd16ed5
MK
4493 if (is_vni_live(vpn))
4494 return 0;
4495
d62a17ae 4496 /* Mark as "live" */
4497 SET_FLAG(vpn->flags, VNI_FLAG_LIVE);
4498
db0e1937
MK
4499 /* tunnel is now active, add tunnel-ip to db */
4500 bgp_tip_add(bgp, &originator_ip);
4501
4502 /* filter routes as nexthop database has changed */
4503 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
4504
d62a17ae 4505 /* Create EVPN type-3 route and schedule for processing. */
4506 build_evpn_type3_prefix(&p, vpn->originator_ip);
4507 if (update_evpn_route(bgp, vpn, &p, 0)) {
4508 zlog_err("%u: Type3 route creation failure for VNI %u",
4509 bgp->vrf_id, vni);
4510 return -1;
4511 }
4512
4513 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
4514 * VNI,
4515 * install them.
4516 */
4517 install_routes_for_vni(bgp, vpn);
4518
d7d97010
MK
4519 /* If we are advertising gateway mac-ip
4520 It needs to be conveyed again to zebra */
4521 bgp_zebra_advertise_gw_macip(bgp, vpn->advertise_gw_macip, vpn->vni);
4522
d62a17ae 4523 return 0;
b18825eb 4524}
14c1a7bf 4525
7724c0a1 4526/*
4527 * Cleanup EVPN information on disable - Need to delete and withdraw
4528 * EVPN routes from peers.
4529 */
d62a17ae 4530void bgp_evpn_cleanup_on_disable(struct bgp *bgp)
7724c0a1 4531{
9d303b37
DL
4532 hash_iterate(bgp->vnihash, (void (*)(struct hash_backet *,
4533 void *))cleanup_vni_on_disable,
4534 bgp);
7724c0a1 4535}
4536
14c1a7bf 4537/*
4538 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
4539 * BGP instance (default) is being freed.
4540 */
d62a17ae 4541void bgp_evpn_cleanup(struct bgp *bgp)
14c1a7bf 4542{
d62a17ae 4543 if (bgp->vnihash)
9d303b37
DL
4544 hash_iterate(bgp->vnihash, (void (*)(struct hash_backet *,
4545 void *))free_vni_entry,
4546 bgp);
d62a17ae 4547 if (bgp->import_rt_hash)
4548 hash_free(bgp->import_rt_hash);
4549 bgp->import_rt_hash = NULL;
10ebe1ab
MK
4550 if (bgp->vrf_import_rt_hash)
4551 hash_free(bgp->vrf_import_rt_hash);
4552 bgp->vrf_import_rt_hash = NULL;
d62a17ae 4553 if (bgp->vnihash)
4554 hash_free(bgp->vnihash);
4555 bgp->vnihash = NULL;
c581d8b0 4556 if (bgp->vrf_import_rtl)
bb7a24ab 4557 list_delete_and_null(&bgp->vrf_import_rtl);
c581d8b0 4558 if (bgp->vrf_export_rtl)
bb7a24ab 4559 list_delete_and_null(&bgp->vrf_export_rtl);
6a8657d0 4560 if (bgp->l2vnis)
bb7a24ab 4561 list_delete_and_null(&bgp->l2vnis);
676f83b9 4562 bf_release_index(bm->rd_idspace, bgp->vrf_rd_id);
14c1a7bf 4563}
4564
4565/*
4566 * Initialization for EVPN
4567 * Create
4568 * VNI hash table
4569 * hash for RT to VNI
676f83b9 4570 * assign a unique rd id for auto derivation of vrf_prd
14c1a7bf 4571 */
d62a17ae 4572void bgp_evpn_init(struct bgp *bgp)
4573{
4574 bgp->vnihash =
4575 hash_create(vni_hash_key_make, vni_hash_cmp, "BGP VNI Hash");
4576 bgp->import_rt_hash =
4577 hash_create(import_rt_hash_key_make, import_rt_hash_cmp,
4578 "BGP Import RT Hash");
10ebe1ab
MK
4579 bgp->vrf_import_rt_hash =
4580 hash_create(vrf_import_rt_hash_key_make, vrf_import_rt_hash_cmp,
4581 "BGP VRF Import RT Hash");
c581d8b0
MK
4582 bgp->vrf_import_rtl = list_new();
4583 bgp->vrf_import_rtl->cmp =
4584 (int (*)(void *, void *))evpn_route_target_cmp;
4585
4586 bgp->vrf_export_rtl = list_new();
4587 bgp->vrf_export_rtl->cmp =
4588 (int (*)(void *, void *))evpn_route_target_cmp;
6a8657d0 4589 bgp->l2vnis = list_new();
996c9314 4590 bgp->l2vnis->cmp = (int (*)(void *, void *))vni_hash_cmp;
676f83b9 4591 bf_assign_index(bm->rd_idspace, bgp->vrf_rd_id);
14c1a7bf 4592}
10ebe1ab
MK
4593
4594void bgp_evpn_vrf_delete(struct bgp *bgp_vrf)
4595{
4596 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
4597}