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