]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_evpn.c
Merge pull request #1737 from mkanjari/build-failure
[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)) {
3278 bgp_evpn_advertise_type5_route(bgp_vrf, &rn->p,
3279 ri->attr,
3280 afi, safi);
3281 break;
3282 }
3283 }
31310b25 3284 }
342dd0c6 3285}
3286
c581d8b0
MK
3287void evpn_rt_delete_auto(struct bgp *bgp, vni_t vni,
3288 struct list *rtl)
3289{
3290 struct listnode *node, *nnode, *node_to_del;
3291 struct ecommunity *ecom, *ecom_auto;
3292 struct ecommunity_val eval;
3293
3294 encode_route_target_as((bgp->as & 0xFFFF), vni, &eval);
3295
3296 ecom_auto = ecommunity_new();
3297 ecommunity_add_val(ecom_auto, &eval);
3298 node_to_del = NULL;
3299
3300 for (ALL_LIST_ELEMENTS(rtl, node, nnode, ecom)) {
3301 if (ecommunity_match(ecom, ecom_auto)) {
3302 ecommunity_free(&ecom);
3303 node_to_del = node;
3304 }
3305 }
3306
3307 if (node_to_del)
3308 list_delete_node(rtl, node_to_del);
3309
3310 ecommunity_free(&ecom_auto);
3311}
3312
3313void bgp_evpn_configure_import_rt_for_vrf(struct bgp *bgp_vrf,
10ebe1ab 3314 struct ecommunity *ecomadd)
c581d8b0 3315{
5ba238b7
MK
3316 /* uninstall routes from vrf */
3317 uninstall_routes_for_vrf(bgp_vrf);
10ebe1ab
MK
3318
3319 /* Cleanup the RT to VRF mapping */
3320 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
3321
c581d8b0
MK
3322 /* Remove auto generated RT */
3323 evpn_auto_rt_import_delete_for_vrf(bgp_vrf);
3324
3325 /* Add the newly configured RT to RT list */
3326 listnode_add_sort(bgp_vrf->vrf_import_rtl, ecomadd);
3327 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
3328
10ebe1ab
MK
3329 /* map VRF to its RTs */
3330 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
3331
5ba238b7
MK
3332 /* install routes matching the new VRF */
3333 install_routes_for_vrf(bgp_vrf);
c581d8b0
MK
3334}
3335
3336void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf,
3337 struct ecommunity *ecomdel)
3338{
3339 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
3340 struct ecommunity *ecom = NULL;
3341
5ba238b7
MK
3342 /* uninstall routes from vrf */
3343 uninstall_routes_for_vrf(bgp_vrf);
10ebe1ab
MK
3344
3345 /* Cleanup the RT to VRF mapping */
3346 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
3347
c581d8b0
MK
3348 /* remove the RT from the RT list */
3349 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
3350 if (ecommunity_match(ecom, ecomdel)) {
3351 ecommunity_free(&ecom);
3352 node_to_del = node;
3353 break;
3354 }
3355 }
3356
3357 if (node_to_del)
3358 list_delete_node(bgp_vrf->vrf_import_rtl, node_to_del);
3359
3360 /* fallback to auto import rt, if this was the last RT */
3361 if (list_isempty(bgp_vrf->vrf_import_rtl)) {
3362 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
3363 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
3364 }
3365
10ebe1ab
MK
3366 /* map VRFs to its RTs */
3367 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
3368
5ba238b7
MK
3369 /* install routes matching this new RT */
3370 install_routes_for_vrf(bgp_vrf);
c581d8b0
MK
3371}
3372
3373void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf,
3374 struct ecommunity *ecomadd)
3375{
3376 /* remove auto-generated RT */
3377 evpn_auto_rt_export_delete_for_vrf(bgp_vrf);
3378
3379 /* Add the new RT to the RT list */
3380 listnode_add_sort(bgp_vrf->vrf_export_rtl, ecomadd);
3381 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
3382
f1f8b53c
MK
3383 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
3384
c581d8b0
MK
3385}
3386
3387void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf,
3388 struct ecommunity *ecomdel)
3389{
3390 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
3391 struct ecommunity *ecom = NULL;
3392
3393 /* Remove the RT from the RT list */
3394 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_export_rtl, node, nnode, ecom)) {
3395 if (ecommunity_match(ecom, ecomdel)) {
3396 ecommunity_free(&ecom);
3397 node_to_del = node;
3398 break;
3399 }
3400 }
3401
3402 if (node_to_del)
3403 list_delete_node(bgp_vrf->vrf_export_rtl, node_to_del);
3404
3405 /* fall back to auto-generated RT if this was the last RT */
877702e7 3406 if (bgp_vrf->vrf_export_rtl && list_isempty(bgp_vrf->vrf_export_rtl)) {
c581d8b0
MK
3407 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
3408 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
3409 }
3410
f1f8b53c 3411 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
c581d8b0
MK
3412}
3413
2d48ee25 3414/*
3415 * Handle change to BGP router id. This is invoked twice by the change
3416 * handler, first before the router id has been changed and then after
3417 * the router id has been changed. The first invocation will result in
676f83b9 3418 * local routes for all VNIs/VRF being deleted and withdrawn and the next
2d48ee25 3419 * will result in the routes being re-advertised.
3420 */
d62a17ae 3421void bgp_evpn_handle_router_id_update(struct bgp *bgp, int withdraw)
2d48ee25 3422{
676f83b9 3423 if (withdraw) {
3424
3425 /* delete and withdraw all the type-5 routes
523cafc4 3426 stored in the global table for this vrf
3427 */
80b140af 3428 withdraw_router_id_vrf(bgp);
676f83b9 3429
3430 /* delete all the VNI routes (type-2/type-3) routes for all the
523cafc4 3431 * L2-VNIs
3432 */
d62a17ae 3433 hash_iterate(bgp->vnihash,
3434 (void (*)(struct hash_backet *,
3435 void *))withdraw_router_id_vni,
3436 bgp);
676f83b9 3437 } else {
3438
3439 /* advertise all routes in the vrf as type-5 routes with the new
523cafc4 3440 * RD
3441 */
80b140af 3442 update_router_id_vrf(bgp);
676f83b9 3443
3444 /* advertise all the VNI routes (type-2/type-3) routes with the
523cafc4 3445 * new RD
3446 */
d62a17ae 3447 hash_iterate(bgp->vnihash,
3448 (void (*)(struct hash_backet *,
3449 void *))update_router_id_vni,
3450 bgp);
676f83b9 3451 }
2d48ee25 3452}
3453
90e60aa7 3454/*
3455 * Handle change to export RT - update and advertise local routes.
3456 */
d62a17ae 3457int bgp_evpn_handle_export_rt_change(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 3458{
d62a17ae 3459 return update_routes_for_vni(bgp, vpn);
90e60aa7 3460}
3461
676f83b9 3462void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf,
3463 int withdraw)
3464{
3465 if (withdraw)
3466 delete_withdraw_vrf_routes(bgp_vrf);
3467 else
3468 update_advertise_vrf_routes(bgp_vrf);
3469}
3470
90e60aa7 3471/*
3472 * Handle change to RD. This is invoked twice by the change handler,
3473 * first before the RD has been changed and then after the RD has
3474 * been changed. The first invocation will result in local routes
3475 * of this VNI being deleted and withdrawn and the next will result
3476 * in the routes being re-advertised.
3477 */
d62a17ae 3478void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
3479 int withdraw)
90e60aa7 3480{
d62a17ae 3481 if (withdraw)
3482 delete_withdraw_vni_routes(bgp, vpn);
3483 else
3484 update_advertise_vni_routes(bgp, vpn);
90e60aa7 3485}
3486
3487/*
3488 * Install routes for this VNI. Invoked upon change to Import RT.
3489 */
d62a17ae 3490int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 3491{
d62a17ae 3492 return install_routes_for_vni(bgp, vpn);
90e60aa7 3493}
3494
3495/*
3496 * Uninstall all routes installed for this VNI. Invoked upon change
3497 * to Import RT.
3498 */
d62a17ae 3499int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 3500{
d62a17ae 3501 return uninstall_routes_for_vni(bgp, vpn);
90e60aa7 3502}
3503
b16031a2 3504/*
b57ba6d2 3505 * TODO: Hardcoded for a maximum of 2 VNIs right now
b16031a2 3506 */
b57ba6d2
MK
3507char *bgp_evpn_label2str(mpls_label_t *label, u_int32_t num_labels,
3508 char *buf, int len)
b16031a2 3509{
b57ba6d2 3510 vni_t vni1, vni2;
b16031a2 3511
b57ba6d2
MK
3512 vni1 = label2vni(label);
3513 if (num_labels == 2) {
3514 vni2 = label2vni(label+1);
3515 snprintf(buf, len, "%u/%u", vni1, vni2);
3516 } else
3517 snprintf(buf, len, "%u", vni1);
d62a17ae 3518 return buf;
b16031a2 3519}
3520
9c92b5f7
MK
3521/*
3522 * Function to convert evpn route to json format.
3523 * NOTE: We don't use prefix2str as the output here is a bit different.
3524 */
57f7feb6 3525void bgp_evpn_route2json(struct prefix_evpn *p, json_object *json)
9c92b5f7 3526{
b682f6de 3527 char buf1[ETHER_ADDR_STRLEN];
3528 char buf2[PREFIX2STR_BUFFER];
9c92b5f7 3529
b682f6de 3530 if (!json)
3531 return;
9c92b5f7 3532
dff8f48d 3533 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
b682f6de 3534 json_object_int_add(json, "routeType", p->prefix.route_type);
3535 json_object_int_add(json, "ethTag", 0);
57f7feb6
MK
3536 json_object_int_add(json, "ipLen",
3537 IS_EVPN_PREFIX_IPADDR_V4(p)
3538 ? IPV4_MAX_BITLEN
3539 : IPV6_MAX_BITLEN);
b682f6de 3540 json_object_string_add(json, "ip",
57f7feb6
MK
3541 inet_ntoa(p->prefix.ip.ipaddr_v4));
3542 } else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
dff8f48d 3543 if (IS_EVPN_PREFIX_IPADDR_NONE(p)) {
57f7feb6
MK
3544 json_object_int_add(json, "routeType",
3545 p->prefix.route_type);
3546 json_object_int_add(
3547 json, "esi",
3548 0); /* TODO: we don't support esi yet */
3549 json_object_int_add(json, "ethTag", 0);
3550 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
3551 json_object_string_add(json, "mac",
3552 prefix_mac2str(&p->prefix.mac,
3553 buf1,
3554 sizeof(buf1)));
dff8f48d
MK
3555 } else {
3556 u_char family;
3557
57f7feb6
MK
3558 family = IS_EVPN_PREFIX_IPADDR_V4(p) ? AF_INET
3559 : AF_INET6;
dff8f48d 3560
b682f6de 3561 json_object_int_add(json, "routeType",
57f7feb6
MK
3562 p->prefix.route_type);
3563 json_object_int_add(
3564 json, "esi",
3565 0); /* TODO: we don't support esi yet */
b682f6de 3566 json_object_int_add(json, "ethTag", 0);
57f7feb6 3567 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
b682f6de 3568 json_object_string_add(json, "mac",
57f7feb6
MK
3569 prefix_mac2str(&p->prefix.mac,
3570 buf1,
3571 sizeof(buf1)));
b682f6de 3572 json_object_int_add(json, "ipLen",
57f7feb6
MK
3573 IS_EVPN_PREFIX_IPADDR_V4(p)
3574 ? IPV4_MAX_BITLEN
3575 : IPV6_MAX_BITLEN);
3576 json_object_string_add(
3577 json, "ip",
3578 inet_ntop(family, &p->prefix.ip.ip.addr, buf2,
3579 PREFIX2STR_BUFFER));
dff8f48d
MK
3580 }
3581 } else {
3582 /* Currently, this is to cater to other AF_ETHERNET code. */
3583 }
9c92b5f7
MK
3584}
3585
520d5d76 3586/*
3587 * Function to convert evpn route to string.
3588 * NOTE: We don't use prefix2str as the output here is a bit different.
3589 */
d62a17ae 3590char *bgp_evpn_route2str(struct prefix_evpn *p, char *buf, int len)
3591{
3592 char buf1[ETHER_ADDR_STRLEN];
3593 char buf2[PREFIX2STR_BUFFER];
3594
3595 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
3596 snprintf(buf, len, "[%d]:[0]:[%d]:[%s]", p->prefix.route_type,
3597 IS_EVPN_PREFIX_IPADDR_V4(p) ? IPV4_MAX_BITLEN
3598 : IPV6_MAX_BITLEN,
3599 inet_ntoa(p->prefix.ip.ipaddr_v4));
3600 } else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
3601 if (IS_EVPN_PREFIX_IPADDR_NONE(p))
3602 snprintf(buf, len, "[%d]:[0]:[0]:[%d]:[%s]",
28328ea9 3603 p->prefix.route_type, 8 * ETH_ALEN,
d62a17ae 3604 prefix_mac2str(&p->prefix.mac, buf1,
3605 sizeof(buf1)));
3606 else {
3607 u_char family;
3608
3609 family = IS_EVPN_PREFIX_IPADDR_V4(p) ? AF_INET
3610 : AF_INET6;
3611 snprintf(buf, len, "[%d]:[0]:[0]:[%d]:[%s]:[%d]:[%s]",
28328ea9 3612 p->prefix.route_type, 8 * ETH_ALEN,
d62a17ae 3613 prefix_mac2str(&p->prefix.mac, buf1,
3614 sizeof(buf1)),
3615 family == AF_INET ? IPV4_MAX_BITLEN
3616 : IPV6_MAX_BITLEN,
3617 inet_ntop(family, &p->prefix.ip.ip.addr, buf2,
3618 PREFIX2STR_BUFFER));
3619 }
342dd0c6 3620 } else if (p->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE) {
b3628c70 3621 snprintf(buf, len, "[%d]:[0]:[0]:[%d]:[%s]",
342dd0c6 3622 p->prefix.route_type,
3623 p->prefix.ip_prefix_length,
3624 IS_EVPN_PREFIX_IPADDR_V4(p) ?
3625 inet_ntoa(p->prefix.ip.ipaddr_v4) :
3626 inet6_ntoa(p->prefix.ip.ipaddr_v6));
d62a17ae 3627 } else {
b03b8898 3628 /* For EVPN route types not supported yet. */
f9aa3e55
QY
3629 snprintf(buf, len, "(unsupported route type %d)",
3630 p->prefix.route_type);
d62a17ae 3631 }
3632
3633 return (buf);
520d5d76 3634}
3635
128ea8ab 3636/*
3637 * Encode EVPN prefix in Update (MP_REACH)
3638 */
d62a17ae 3639void bgp_evpn_encode_prefix(struct stream *s, struct prefix *p,
b57ba6d2
MK
3640 struct prefix_rd *prd,
3641 mpls_label_t *label, u_int32_t num_labels,
d62a17ae 3642 struct attr *attr, int addpath_encode,
3643 u_int32_t addpath_tx_id)
3644{
3645 struct prefix_evpn *evp = (struct prefix_evpn *)p;
b57ba6d2 3646 int len, ipa_len = 0;
d62a17ae 3647
3648 if (addpath_encode)
3649 stream_putl(s, addpath_tx_id);
3650
3651 /* Route type */
3652 stream_putc(s, evp->prefix.route_type);
3653
3654 switch (evp->prefix.route_type) {
3655 case BGP_EVPN_MAC_IP_ROUTE:
3656 if (IS_EVPN_PREFIX_IPADDR_V4(evp))
3657 ipa_len = IPV4_MAX_BYTELEN;
3658 else if (IS_EVPN_PREFIX_IPADDR_V6(evp))
3659 ipa_len = IPV6_MAX_BYTELEN;
b57ba6d2
MK
3660 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
3661 len = 8 + 10 + 4 + 1 + 6 + 1 + ipa_len + 3;
3662 if (ipa_len && num_labels > 1) /* There are 2 VNIs */
3663 len += 3;
3664 stream_putc(s, len);
d62a17ae 3665 stream_put(s, prd->val, 8); /* RD */
3666 stream_put(s, 0, 10); /* ESI */
3667 stream_putl(s, 0); /* Ethernet Tag ID */
28328ea9 3668 stream_putc(s, 8 * ETH_ALEN); /* Mac Addr Len - bits */
d62a17ae 3669 stream_put(s, evp->prefix.mac.octet, 6); /* Mac Addr */
3670 stream_putc(s, 8 * ipa_len); /* IP address Length */
b57ba6d2
MK
3671 if (ipa_len) /* IP */
3672 stream_put(s, &evp->prefix.ip.ip.addr, ipa_len);
3673 /* 1st label is the L2 VNI */
3674 stream_put(s, label, BGP_LABEL_BYTES);
3675 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
3676 if (ipa_len && num_labels > 1)
3677 stream_put(s, label+1, BGP_LABEL_BYTES);
d62a17ae 3678 break;
3679
3680 case BGP_EVPN_IMET_ROUTE:
3681 stream_putc(s, 17); // TODO: length - assumes IPv4 address
3682 stream_put(s, prd->val, 8); /* RD */
3683 stream_putl(s, 0); /* Ethernet Tag ID */
3684 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
3685 /* Originating Router's IP Addr */
3686 stream_put_in_addr(s, &evp->prefix.ip.ipaddr_v4);
3687 break;
3688
3689 case BGP_EVPN_IP_PREFIX_ROUTE:
3690 /* TODO: AddPath support. */
b57ba6d2 3691 evpn_mpattr_encode_type5(s, p, prd, label, num_labels, attr);
d62a17ae 3692 break;
3693
3694 default:
3695 break;
3696 }
3697}
3698
3699int bgp_nlri_parse_evpn(struct peer *peer, struct attr *attr,
3700 struct bgp_nlri *packet, int withdraw)
3701{
3702 u_char *pnt;
3703 u_char *lim;
3704 afi_t afi;
3705 safi_t safi;
3706 u_int32_t addpath_id;
3707 int addpath_encoded;
3708 int psize = 0;
3709 u_char rtype;
3710 u_char rlen;
3711 struct prefix p;
3712
3713 /* Check peer status. */
3714 if (peer->status != Established) {
3715 zlog_err("%u:%s - EVPN update received in state %d",
3716 peer->bgp->vrf_id, peer->host, peer->status);
3717 return -1;
3718 }
3719
3720 /* Start processing the NLRI - there may be multiple in the MP_REACH */
3721 pnt = packet->nlri;
3722 lim = pnt + packet->length;
3723 afi = packet->afi;
3724 safi = packet->safi;
3725 addpath_id = 0;
3726
3727 addpath_encoded =
3728 (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV)
3729 && CHECK_FLAG(peer->af_cap[afi][safi],
3730 PEER_CAP_ADDPATH_AF_TX_RCV));
3731
3732 for (; pnt < lim; pnt += psize) {
3733 /* Clear prefix structure. */
3734 memset(&p, 0, sizeof(struct prefix));
3735
3736 /* Deal with path-id if AddPath is supported. */
3737 if (addpath_encoded) {
3738 /* When packet overflow occurs return immediately. */
3739 if (pnt + BGP_ADDPATH_ID_LEN > lim)
3740 return -1;
3741
3742 addpath_id = ntohl(*((uint32_t *)pnt));
3743 pnt += BGP_ADDPATH_ID_LEN;
3744 }
3745
3746 /* All EVPN NLRI types start with type and length. */
3747 if (pnt + 2 > lim)
3748 return -1;
3749
3750 rtype = *pnt++;
3751 psize = rlen = *pnt++;
3752
3753 /* When packet overflow occur return immediately. */
3754 if (pnt + psize > lim)
3755 return -1;
3756
3757 switch (rtype) {
3758 case BGP_EVPN_MAC_IP_ROUTE:
3759 if (process_type2_route(peer, afi, safi,
3760 withdraw ? NULL : attr, pnt,
3761 psize, addpath_id)) {
3762 zlog_err(
3763 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
3764 peer->bgp->vrf_id, peer->host, psize);
3765 return -1;
3766 }
3767 break;
3768
3769 case BGP_EVPN_IMET_ROUTE:
3770 if (process_type3_route(peer, afi, safi,
3771 withdraw ? NULL : attr, pnt,
3772 psize, addpath_id)) {
3773 zlog_err(
3774 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
3775 peer->bgp->vrf_id, peer->host, psize);
3776 return -1;
3777 }
3778 break;
3779
3780 case BGP_EVPN_IP_PREFIX_ROUTE:
3781 if (process_type5_route(peer, afi, safi, attr, pnt,
3782 psize, addpath_id, withdraw)) {
3783 zlog_err(
3784 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
3785 peer->bgp->vrf_id, peer->host, psize);
3786 return -1;
3787 }
3788 break;
3789
3790 default:
3791 break;
3792 }
3793 }
3794
3795 /* Packet length consistency check. */
3796 if (pnt != lim)
3797 return -1;
3798
3799 return 0;
128ea8ab 3800}
3801
10ebe1ab
MK
3802/*
3803 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
3804 * The mapping will be used during route processing.
3805 * bgp_def: default bgp instance
3806 * bgp_vrf: specific bgp vrf instance on which RT is configured
3807 */
3808void bgp_evpn_map_vrf_to_its_rts(struct bgp *bgp_vrf)
3809{
3810 int i = 0;
3811 struct ecommunity_val *eval = NULL;
3812 struct listnode *node = NULL, *nnode = NULL;
3813 struct ecommunity *ecom = NULL;
3814
3815 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
3816 for (i = 0; i < ecom->size; i++) {
3817 eval = (struct ecommunity_val *)(ecom->val
3818 + (i
3819 * ECOMMUNITY_SIZE));
3820 map_vrf_to_rt(bgp_vrf, eval);
3821 }
3822 }
3823}
3824
3825/*
3826 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
3827 */
3828void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *bgp_vrf)
3829{
3830 int i;
3831 struct ecommunity_val *eval;
3832 struct listnode *node, *nnode;
3833 struct ecommunity *ecom;
3834
3835 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
3836 for (i = 0; i < ecom->size; i++) {
3837 struct vrf_irt_node *irt;
3838 struct ecommunity_val eval_tmp;
3839
3840 eval = (struct ecommunity_val *)(ecom->val
3841 + (i
3842 * ECOMMUNITY_SIZE));
3843 /* If using "automatic" RT, we only care about the
3844 * local-admin sub-field.
3845 * This is to facilitate using VNI as the RT for EBGP
3846 * peering too.
3847 */
3848 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
3849 if (!CHECK_FLAG(bgp_vrf->vrf_flags,
3850 BGP_VRF_IMPORT_RT_CFGD))
3851 mask_ecom_global_admin(&eval_tmp, eval);
3852
3853 irt = lookup_vrf_import_rt(&eval_tmp);
3854 if (irt)
3855 unmap_vrf_from_rt(bgp_vrf, irt);
3856 }
3857 }
3858}
3859
3860
128ea8ab 3861
3862/*
3863 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
3864 * The mapping will be used during route processing.
3865 */
d62a17ae 3866void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3867{
d62a17ae 3868 int i;
3869 struct ecommunity_val *eval;
3870 struct listnode *node, *nnode;
3871 struct ecommunity *ecom;
128ea8ab 3872
d62a17ae 3873 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
3874 for (i = 0; i < ecom->size; i++) {
3875 eval = (struct ecommunity_val *)(ecom->val
3876 + (i
3877 * ECOMMUNITY_SIZE));
3878 map_vni_to_rt(bgp, vpn, eval);
3879 }
3880 }
128ea8ab 3881}
3882
3883/*
3884 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
3885 */
d62a17ae 3886void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3887{
d62a17ae 3888 int i;
3889 struct ecommunity_val *eval;
3890 struct listnode *node, *nnode;
3891 struct ecommunity *ecom;
128ea8ab 3892
d62a17ae 3893 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
3894 for (i = 0; i < ecom->size; i++) {
3895 struct irt_node *irt;
3896 struct ecommunity_val eval_tmp;
128ea8ab 3897
d62a17ae 3898 eval = (struct ecommunity_val *)(ecom->val
3899 + (i
3900 * ECOMMUNITY_SIZE));
3901 /* If using "automatic" RT, we only care about the
3902 * local-admin sub-field.
3903 * This is to facilitate using VNI as the RT for EBGP
3904 * peering too.
3905 */
3906 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
3907 if (!is_import_rt_configured(vpn))
3908 mask_ecom_global_admin(&eval_tmp, eval);
128ea8ab 3909
d62a17ae 3910 irt = lookup_import_rt(bgp, &eval_tmp);
3911 if (irt)
3912 unmap_vni_from_rt(bgp, vpn, irt);
3913 }
3914 }
128ea8ab 3915}
3916
3917/*
3918 * Derive Import RT automatically for VNI and map VNI to RT.
3919 * The mapping will be used during route processing.
3920 */
d62a17ae 3921void bgp_evpn_derive_auto_rt_import(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3922{
c581d8b0 3923 form_auto_rt(bgp, vpn->vni, vpn->import_rtl);
d62a17ae 3924 UNSET_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD);
128ea8ab 3925
d62a17ae 3926 /* Map RT to VNI */
3927 bgp_evpn_map_vni_to_its_rts(bgp, vpn);
128ea8ab 3928}
3929
3930/*
3931 * Derive Export RT automatically for VNI.
3932 */
d62a17ae 3933void bgp_evpn_derive_auto_rt_export(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3934{
c581d8b0 3935 form_auto_rt(bgp, vpn->vni, vpn->export_rtl);
d62a17ae 3936 UNSET_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD);
128ea8ab 3937}
3938
676f83b9 3939/*
3940 * Derive RD automatically for VNI using passed information - it
3941 * is of the form RouterId:unique-id-for-vni.
3942 */
3943void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp)
3944{
3945 char buf[100];
3946
3947 bgp->vrf_prd.family = AF_UNSPEC;
3948 bgp->vrf_prd.prefixlen = 64;
3949 sprintf(buf, "%s:%hu", inet_ntoa(bgp->router_id), bgp->vrf_rd_id);
3950 str2prefix_rd(buf, &bgp->vrf_prd);
3951}
3952
128ea8ab 3953/*
3954 * Derive RD automatically for VNI using passed information - it
3955 * is of the form RouterId:unique-id-for-vni.
3956 */
d62a17ae 3957void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3958{
d62a17ae 3959 char buf[100];
128ea8ab 3960
d62a17ae 3961 vpn->prd.family = AF_UNSPEC;
3962 vpn->prd.prefixlen = 64;
3963 sprintf(buf, "%s:%hu", inet_ntoa(bgp->router_id), vpn->rd_id);
cbb65f5e 3964 (void)str2prefix_rd(buf, &vpn->prd);
d62a17ae 3965 UNSET_FLAG(vpn->flags, VNI_FLAG_RD_CFGD);
128ea8ab 3966}
3967
3968/*
3969 * Lookup VNI.
3970 */
d62a17ae 3971struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni)
128ea8ab 3972{
d62a17ae 3973 struct bgpevpn *vpn;
3974 struct bgpevpn tmp;
128ea8ab 3975
d62a17ae 3976 memset(&tmp, 0, sizeof(struct bgpevpn));
3977 tmp.vni = vni;
3978 vpn = hash_lookup(bgp->vnihash, &tmp);
3979 return vpn;
128ea8ab 3980}
3981
3982/*
3983 * Create a new vpn - invoked upon configuration or zebra notification.
3984 */
d62a17ae 3985struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
29c53922
MK
3986 struct in_addr originator_ip,
3987 vrf_id_t tenant_vrf_id)
128ea8ab 3988{
d62a17ae 3989 struct bgpevpn *vpn;
128ea8ab 3990
d62a17ae 3991 if (!bgp)
3992 return NULL;
128ea8ab 3993
d62a17ae 3994 vpn = XCALLOC(MTYPE_BGP_EVPN, sizeof(struct bgpevpn));
3995 if (!vpn)
3996 return NULL;
128ea8ab 3997
d62a17ae 3998 /* Set values - RD and RT set to defaults. */
3999 vpn->vni = vni;
4000 vpn->originator_ip = originator_ip;
29c53922 4001 vpn->tenant_vrf_id = tenant_vrf_id;
128ea8ab 4002
d62a17ae 4003 /* Initialize route-target import and export lists */
4004 vpn->import_rtl = list_new();
4005 vpn->import_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
4006 vpn->export_rtl = list_new();
4007 vpn->export_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
e9eb5f63 4008 bf_assign_index(bm->rd_idspace, vpn->rd_id);
d62a17ae 4009 derive_rd_rt_for_vni(bgp, vpn);
128ea8ab 4010
d62a17ae 4011 /* Initialize EVPN route table. */
4012 vpn->route_table = bgp_table_init(AFI_L2VPN, SAFI_EVPN);
128ea8ab 4013
d62a17ae 4014 /* Add to hash */
4015 if (!hash_get(bgp->vnihash, vpn, hash_alloc_intern)) {
4016 XFREE(MTYPE_BGP_EVPN, vpn);
4017 return NULL;
4018 }
6a8657d0
MK
4019
4020 /* add to l2vni list on corresponding vrf */
4021 bgpevpn_link_to_l3vni(vpn);
4022
d62a17ae 4023 QOBJ_REG(vpn, bgpevpn);
4024 return vpn;
128ea8ab 4025}
4026
4027/*
4028 * Free a given VPN - called in multiple scenarios such as zebra
4029 * notification, configuration being deleted, advertise-all-vni disabled etc.
4030 * This just frees appropriate memory, caller should have taken other
4031 * needed actions.
4032 */
d62a17ae 4033void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 4034{
6a8657d0 4035 bgpevpn_unlink_from_l3vni(vpn);
d62a17ae 4036 bgp_table_unlock(vpn->route_table);
4037 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
affe9e99
DS
4038 list_delete_and_null(&vpn->import_rtl);
4039 list_delete_and_null(&vpn->export_rtl);
e9eb5f63 4040 bf_release_index(bm->rd_idspace, vpn->rd_id);
d62a17ae 4041 hash_release(bgp->vnihash, vpn);
4042 QOBJ_UNREG(vpn);
4043 XFREE(MTYPE_BGP_EVPN, vpn);
128ea8ab 4044}
4045
4046/*
4047 * Import route into matching VNI(s).
4048 */
d62a17ae 4049int bgp_evpn_import_route(struct bgp *bgp, afi_t afi, safi_t safi,
4050 struct prefix *p, struct bgp_info *ri)
128ea8ab 4051{
d62a17ae 4052 return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 1);
128ea8ab 4053}
4054
4055/*
4056 * Unimport route from matching VNI(s).
4057 */
d62a17ae 4058int bgp_evpn_unimport_route(struct bgp *bgp, afi_t afi, safi_t safi,
4059 struct prefix *p, struct bgp_info *ri)
128ea8ab 4060{
d62a17ae 4061 return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 0);
128ea8ab 4062}
4063
db0e1937
MK
4064/* filter routes which have martian next hops */
4065int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp *bgp)
4066{
0291c246
MK
4067 afi_t afi;
4068 safi_t safi;
4069 struct bgp_node *rd_rn, *rn;
4070 struct bgp_table *table;
4071 struct bgp_info *ri;
db0e1937
MK
4072
4073 afi = AFI_L2VPN;
4074 safi = SAFI_EVPN;
4075
4076 /* Walk entire global routing table and evaluate routes which could be
4077 * imported into this VPN. Note that we cannot just look at the routes
4078 * for the VNI's RD -
4079 * remote routes applicable for this VNI could have any RD.
4080 */
4081 /* EVPN routes are a 2-level table. */
4082 for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
4083 rd_rn = bgp_route_next(rd_rn)) {
4084 table = (struct bgp_table *)(rd_rn->info);
4085 if (!table)
4086 continue;
4087
4088 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
4089
4090 for (ri = rn->info; ri; ri = ri->next) {
4091
4092 /* Consider "valid" remote routes applicable for
4093 * this VNI. */
4094 if (!(ri->type == ZEBRA_ROUTE_BGP
4095 && ri->sub_type == BGP_ROUTE_NORMAL))
4096 continue;
4097
60466a63 4098 if (bgp_nexthop_self(bgp, ri->attr->nexthop)) {
db0e1937
MK
4099
4100 char attr_str[BUFSIZ];
4101 char pbuf[PREFIX_STRLEN];
4102
4103 bgp_dump_attr(ri->attr, attr_str,
4104 BUFSIZ);
4105
4106 if (bgp_debug_update(ri->peer, &rn->p,
4107 NULL, 1))
4108 zlog_debug(
b682f6de 4109 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
db0e1937
MK
4110 bgp->vrf_id,
4111 prefix2str(
60466a63 4112 &rn->p, pbuf,
db0e1937
MK
4113 sizeof(pbuf)),
4114 attr_str);
4115
4116 bgp_evpn_unimport_route(bgp, afi, safi,
4117 &rn->p, ri);
4118
60466a63
QY
4119 bgp_rib_remove(rn, ri, ri->peer, afi,
4120 safi);
db0e1937 4121 }
db0e1937
MK
4122 }
4123 }
4124 }
4125
4126 return 0;
4127}
4128
128ea8ab 4129/*
4130 * Handle del of a local MACIP.
4131 */
d62a17ae 4132int bgp_evpn_local_macip_del(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
4133 struct ipaddr *ip)
128ea8ab 4134{
d62a17ae 4135 struct bgpevpn *vpn;
4136 struct prefix_evpn p;
128ea8ab 4137
d62a17ae 4138 if (!bgp->vnihash) {
4139 zlog_err("%u: VNI hash not created", bgp->vrf_id);
4140 return -1;
4141 }
128ea8ab 4142
d62a17ae 4143 /* Lookup VNI hash - should exist. */
4144 vpn = bgp_evpn_lookup_vni(bgp, vni);
4145 if (!vpn || !is_vni_live(vpn)) {
4146 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL",
4147 bgp->vrf_id, vni, vpn ? "not live" : "not found");
4148 return -1;
4149 }
128ea8ab 4150
d62a17ae 4151 /* Remove EVPN type-2 route and schedule for processing. */
4152 build_evpn_type2_prefix(&p, mac, ip);
4153 delete_evpn_route(bgp, vpn, &p);
128ea8ab 4154
d62a17ae 4155 return 0;
128ea8ab 4156}
4157
4158/*
4159 * Handle add of a local MACIP.
4160 */
d62a17ae 4161int bgp_evpn_local_macip_add(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
1a98c087 4162 struct ipaddr *ip, u_char flags)
128ea8ab 4163{
d62a17ae 4164 struct bgpevpn *vpn;
4165 struct prefix_evpn p;
128ea8ab 4166
d62a17ae 4167 if (!bgp->vnihash) {
4168 zlog_err("%u: VNI hash not created", bgp->vrf_id);
4169 return -1;
4170 }
128ea8ab 4171
d62a17ae 4172 /* Lookup VNI hash - should exist. */
4173 vpn = bgp_evpn_lookup_vni(bgp, vni);
4174 if (!vpn || !is_vni_live(vpn)) {
4175 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD",
4176 bgp->vrf_id, vni, vpn ? "not live" : "not found");
4177 return -1;
4178 }
128ea8ab 4179
d62a17ae 4180 /* Create EVPN type-2 route and schedule for processing. */
4181 build_evpn_type2_prefix(&p, mac, ip);
1a98c087 4182 if (update_evpn_route(bgp, vpn, &p, flags)) {
d62a17ae 4183 char buf[ETHER_ADDR_STRLEN];
4184 char buf2[INET6_ADDRSTRLEN];
128ea8ab 4185
d62a17ae 4186 zlog_err(
ead40654 4187 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
1a98c087 4188 bgp->vrf_id, vpn->vni,
317f1fe0 4189 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY) ? "sticky gateway"
1a98c087 4190 : "",
d62a17ae 4191 prefix_mac2str(mac, buf, sizeof(buf)),
ead40654
MK
4192 ipaddr2str(ip, buf2, sizeof(buf2)),
4193 flags);
d62a17ae 4194 return -1;
4195 }
128ea8ab 4196
d62a17ae 4197 return 0;
128ea8ab 4198}
4199
6a8657d0
MK
4200static void link_l2vni_hash_to_l3vni(struct hash_backet *backet,
4201 struct bgp *bgp_vrf)
4202{
4203 struct bgpevpn *vpn = NULL;
4204 struct bgp *bgp_def = NULL;
4205
4206 bgp_def = bgp_get_default();
4207 assert(bgp_def);
4208
4209 vpn = (struct bgpevpn *)backet->data;
4210 if (vpn->tenant_vrf_id == bgp_vrf->vrf_id)
4211 bgpevpn_link_to_l3vni(vpn);
4212}
4213
fe1dc5a3
MK
4214int bgp_evpn_local_l3vni_add(vni_t l3vni,
4215 vrf_id_t vrf_id,
b67a60d2 4216 struct ethaddr *rmac,
4217 struct in_addr originator_ip)
fe1dc5a3
MK
4218{
4219 struct bgp *bgp_vrf = NULL; /* bgp VRF instance */
4220 struct bgp *bgp_def = NULL; /* default bgp instance */
f1f8b53c
MK
4221 struct listnode *node = NULL;
4222 struct bgpevpn *vpn = NULL;
fe1dc5a3
MK
4223 as_t as = 0;
4224
4225 /* get the default instamce - required to get the AS number for VRF
523cafc4 4226 * auto-creatio
4227 */
fe1dc5a3
MK
4228 bgp_def = bgp_get_default();
4229 if (!bgp_def) {
4230 zlog_err("Cannot process L3VNI %u ADD - default BGP instance not yet created",
4231 l3vni);
4232 return -1;
4233 }
4234 as = bgp_def->as;
4235
4236 /* if the BGP vrf instance doesnt exist - create one */
0b5131c9 4237 bgp_vrf = bgp_lookup_by_name(vrf_id_to_name(vrf_id));
fe1dc5a3
MK
4238 if (!bgp_vrf) {
4239
4240 int ret = 0;
4241
4242 ret = bgp_get(&bgp_vrf, &as, vrf_id_to_name(vrf_id),
4243 BGP_INSTANCE_TYPE_VRF);
4244 switch (ret) {
4245 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET:
4246 zlog_err("'bgp multiple-instance' not present\n");
4247 return -1;
4248 case BGP_ERR_AS_MISMATCH:
4249 zlog_err("BGP is already running; AS is %u\n", as);
4250 return -1;
4251 case BGP_ERR_INSTANCE_MISMATCH:
4252 zlog_err("BGP instance name and AS number mismatch\n");
4253 return -1;
4254 }
4255
4256 /* mark as auto created */
4257 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO);
4258 }
4259
4260 /* associate with l3vni */
4261 bgp_vrf->l3vni = l3vni;
4262
4263 /* set the router mac - to be used in mac-ip routes for this vrf */
4264 memcpy(&bgp_vrf->rmac, rmac, sizeof(struct ethaddr));
4265
b67a60d2 4266 /* set the originator ip */
4267 bgp_vrf->originator_ip = originator_ip;
4268
c581d8b0
MK
4269 /* auto derive RD/RT */
4270 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
4271 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
4272 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD))
4273 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
676f83b9 4274 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
fe1dc5a3 4275
6a8657d0
MK
4276 /* link all corresponding l2vnis */
4277 hash_iterate(bgp_def->vnihash,
4278 (void (*)(struct hash_backet *, void *))
4279 link_l2vni_hash_to_l3vni,
4280 bgp_vrf);
4281
f1f8b53c
MK
4282 /* updates all corresponding local mac-ip routes */
4283 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
4284 update_routes_for_vni(bgp_def, vpn);
fe1dc5a3 4285
06d2e8f3
MK
4286 /* advertise type-5 routes if needed */
4287 update_advertise_vrf_routes(bgp_vrf);
4288
5ba238b7
MK
4289 /* install all remote routes belonging to this l3vni into correspondng
4290 * vrf */
4291 install_routes_for_vrf(bgp_vrf);
fe1dc5a3
MK
4292
4293 return 0;
4294}
4295
4296int bgp_evpn_local_l3vni_del(vni_t l3vni,
4297 vrf_id_t vrf_id)
4298{
4299 struct bgp *bgp_vrf = NULL; /* bgp vrf instance */
f1f8b53c
MK
4300 struct bgp *bgp_def = NULL; /* default bgp instance */
4301 struct listnode *node = NULL;
4302 struct bgpevpn *vpn = NULL;
fe1dc5a3
MK
4303
4304 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
4305 if (!bgp_vrf) {
4306 zlog_err("Cannot process L3VNI %u Del - Could not find BGP instance",
4307 l3vni);
4308 return -1;
4309 }
4310
f1f8b53c
MK
4311 bgp_def = bgp_get_default();
4312 if (!bgp_def) {
4313 zlog_err("Cannot process L3VNI %u Del - Could not find default BGP instance",
4314 l3vni);
4315 return -1;
4316 }
4317
1eb88002 4318 /* unimport remote routes from VRF, if it is AUTO vrf bgp_delete will
523cafc4 4319 * take care of uninstalling the routes from zebra
4320 */
1eb88002
MK
4321 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO))
4322 uninstall_routes_for_vrf(bgp_vrf);
5ba238b7 4323
06d2e8f3
MK
4324 /* delete/withdraw all type-5 routes */
4325 delete_withdraw_vrf_routes(bgp_vrf);
4326
fe1dc5a3
MK
4327 /* remove the l3vni from vrf instance */
4328 bgp_vrf->l3vni = 0;
4329
4330 /* remove the Rmac from the BGP vrf */
4331 memset(&bgp_vrf->rmac, 0, sizeof(struct ethaddr));
4332
c581d8b0 4333 /* delete RD/RT */
23a06e11 4334 if (bgp_vrf->vrf_import_rtl && !list_isempty(bgp_vrf->vrf_import_rtl)) {
10ebe1ab 4335 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
5ba238b7 4336 list_delete_all_node(bgp_vrf->vrf_import_rtl);
23a06e11
MK
4337 }
4338 if (bgp_vrf->vrf_export_rtl && !list_isempty(bgp_vrf->vrf_export_rtl)) {
5ba238b7 4339 list_delete_all_node(bgp_vrf->vrf_export_rtl);
23a06e11 4340 }
fe1dc5a3 4341
f1f8b53c
MK
4342 /* update all corresponding local mac-ip routes */
4343 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
4344 update_routes_for_vni(bgp_def, vpn);
fe1dc5a3 4345
fe1dc5a3
MK
4346
4347 /* Delete the instance if it was autocreated */
4348 if (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO))
4349 bgp_delete(bgp_vrf);
4350
4351 return 0;
4352}
4353
128ea8ab 4354/*
4355 * Handle del of a local VNI.
4356 */
d62a17ae 4357int bgp_evpn_local_vni_del(struct bgp *bgp, vni_t vni)
128ea8ab 4358{
d62a17ae 4359 struct bgpevpn *vpn;
128ea8ab 4360
d62a17ae 4361 if (!bgp->vnihash) {
4362 zlog_err("%u: VNI hash not created", bgp->vrf_id);
4363 return -1;
4364 }
128ea8ab 4365
d62a17ae 4366 /* Locate VNI hash */
4367 vpn = bgp_evpn_lookup_vni(bgp, vni);
4368 if (!vpn) {
4369 zlog_warn("%u: VNI hash entry for VNI %u not found at DEL",
4370 bgp->vrf_id, vni);
4371 return 0;
4372 }
128ea8ab 4373
d62a17ae 4374 /* Remove all local EVPN routes and schedule for processing (to
4375 * withdraw from peers).
4376 */
4377 delete_routes_for_vni(bgp, vpn);
128ea8ab 4378
db0e1937
MK
4379 /*
4380 * tunnel is no longer active, del tunnel ip address from tip_hash
4381 */
4382 bgp_tip_del(bgp, &vpn->originator_ip);
4383
d62a17ae 4384 /* Clear "live" flag and see if hash needs to be freed. */
4385 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
4386 if (!is_vni_configured(vpn))
4387 bgp_evpn_free(bgp, vpn);
128ea8ab 4388
d62a17ae 4389 return 0;
128ea8ab 4390}
4391
4392/*
4393 * Handle add (or update) of a local VNI. The only VNI change we care
4394 * about is change to local-tunnel-ip.
4395 */
d62a17ae 4396int bgp_evpn_local_vni_add(struct bgp *bgp, vni_t vni,
29c53922
MK
4397 struct in_addr originator_ip,
4398 vrf_id_t tenant_vrf_id)
d62a17ae 4399{
4400 struct bgpevpn *vpn;
4401 struct prefix_evpn p;
4402
4403 if (!bgp->vnihash) {
4404 zlog_err("%u: VNI hash not created", bgp->vrf_id);
4405 return -1;
4406 }
4407
4408 /* Lookup VNI. If present and no change, exit. */
4409 vpn = bgp_evpn_lookup_vni(bgp, vni);
ddd16ed5 4410 if (vpn) {
29c53922
MK
4411
4412 /* update tenant_vrf_id if required */
6a8657d0
MK
4413 if (vpn->tenant_vrf_id != tenant_vrf_id) {
4414 bgpevpn_unlink_from_l3vni(vpn);
29c53922 4415 vpn->tenant_vrf_id = tenant_vrf_id;
6a8657d0 4416 bgpevpn_link_to_l3vni(vpn);
e92bd2a2
MK
4417
4418 /* update all routes with new export RT for VRFs */
4419 update_routes_for_vni(bgp, vpn);
6a8657d0 4420 }
29c53922 4421
2f1ac16a
MK
4422 if (is_vni_live(vpn)
4423 && IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip))
d62a17ae 4424 /* Probably some other param has changed that we don't
4425 * care about. */
4426 return 0;
4427
4428 /* Local tunnel endpoint IP address has changed */
ddd16ed5 4429 handle_tunnel_ip_change(bgp, vpn, originator_ip);
d62a17ae 4430 }
4431
4432 /* Create or update as appropriate. */
4433 if (!vpn) {
29c53922 4434 vpn = bgp_evpn_new(bgp, vni, originator_ip, tenant_vrf_id);
d62a17ae 4435 if (!vpn) {
4436 zlog_err(
4437 "%u: Failed to allocate VNI entry for VNI %u - at Add",
4438 bgp->vrf_id, vni);
4439 return -1;
4440 }
4441 }
4442
db0e1937 4443 /* if the VNI is live already, there is nothing more to do */
ddd16ed5
MK
4444 if (is_vni_live(vpn))
4445 return 0;
4446
d62a17ae 4447 /* Mark as "live" */
4448 SET_FLAG(vpn->flags, VNI_FLAG_LIVE);
4449
db0e1937
MK
4450 /* tunnel is now active, add tunnel-ip to db */
4451 bgp_tip_add(bgp, &originator_ip);
4452
4453 /* filter routes as nexthop database has changed */
4454 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
4455
d62a17ae 4456 /* Create EVPN type-3 route and schedule for processing. */
4457 build_evpn_type3_prefix(&p, vpn->originator_ip);
4458 if (update_evpn_route(bgp, vpn, &p, 0)) {
4459 zlog_err("%u: Type3 route creation failure for VNI %u",
4460 bgp->vrf_id, vni);
4461 return -1;
4462 }
4463
4464 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
4465 * VNI,
4466 * install them.
4467 */
4468 install_routes_for_vni(bgp, vpn);
4469
d7d97010
MK
4470 /* If we are advertising gateway mac-ip
4471 It needs to be conveyed again to zebra */
4472 bgp_zebra_advertise_gw_macip(bgp, vpn->advertise_gw_macip, vpn->vni);
4473
d62a17ae 4474 return 0;
b18825eb 4475}
14c1a7bf 4476
7724c0a1 4477/*
4478 * Cleanup EVPN information on disable - Need to delete and withdraw
4479 * EVPN routes from peers.
4480 */
d62a17ae 4481void bgp_evpn_cleanup_on_disable(struct bgp *bgp)
7724c0a1 4482{
9d303b37
DL
4483 hash_iterate(bgp->vnihash, (void (*)(struct hash_backet *,
4484 void *))cleanup_vni_on_disable,
4485 bgp);
7724c0a1 4486}
4487
14c1a7bf 4488/*
4489 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
4490 * BGP instance (default) is being freed.
4491 */
d62a17ae 4492void bgp_evpn_cleanup(struct bgp *bgp)
14c1a7bf 4493{
d62a17ae 4494 if (bgp->vnihash)
9d303b37
DL
4495 hash_iterate(bgp->vnihash, (void (*)(struct hash_backet *,
4496 void *))free_vni_entry,
4497 bgp);
d62a17ae 4498 if (bgp->import_rt_hash)
4499 hash_free(bgp->import_rt_hash);
4500 bgp->import_rt_hash = NULL;
10ebe1ab
MK
4501 if (bgp->vrf_import_rt_hash)
4502 hash_free(bgp->vrf_import_rt_hash);
4503 bgp->vrf_import_rt_hash = NULL;
d62a17ae 4504 if (bgp->vnihash)
4505 hash_free(bgp->vnihash);
4506 bgp->vnihash = NULL;
c581d8b0 4507 if (bgp->vrf_import_rtl)
bb7a24ab 4508 list_delete_and_null(&bgp->vrf_import_rtl);
c581d8b0 4509 if (bgp->vrf_export_rtl)
bb7a24ab 4510 list_delete_and_null(&bgp->vrf_export_rtl);
6a8657d0 4511 if (bgp->l2vnis)
bb7a24ab 4512 list_delete_and_null(&bgp->l2vnis);
676f83b9 4513 bf_release_index(bm->rd_idspace, bgp->vrf_rd_id);
14c1a7bf 4514}
4515
4516/*
4517 * Initialization for EVPN
4518 * Create
4519 * VNI hash table
4520 * hash for RT to VNI
676f83b9 4521 * assign a unique rd id for auto derivation of vrf_prd
14c1a7bf 4522 */
d62a17ae 4523void bgp_evpn_init(struct bgp *bgp)
4524{
4525 bgp->vnihash =
4526 hash_create(vni_hash_key_make, vni_hash_cmp, "BGP VNI Hash");
4527 bgp->import_rt_hash =
4528 hash_create(import_rt_hash_key_make, import_rt_hash_cmp,
4529 "BGP Import RT Hash");
10ebe1ab
MK
4530 bgp->vrf_import_rt_hash =
4531 hash_create(vrf_import_rt_hash_key_make, vrf_import_rt_hash_cmp,
4532 "BGP VRF Import RT Hash");
c581d8b0
MK
4533 bgp->vrf_import_rtl = list_new();
4534 bgp->vrf_import_rtl->cmp =
4535 (int (*)(void *, void *))evpn_route_target_cmp;
4536
4537 bgp->vrf_export_rtl = list_new();
4538 bgp->vrf_export_rtl->cmp =
4539 (int (*)(void *, void *))evpn_route_target_cmp;
6a8657d0
MK
4540 bgp->l2vnis = list_new();
4541 bgp->l2vnis->cmp =
4542 (int (*)(void *, void *))vni_hash_cmp;
676f83b9 4543 bf_assign_index(bm->rd_idspace, bgp->vrf_rd_id);
4544
14c1a7bf 4545}
10ebe1ab
MK
4546
4547void bgp_evpn_vrf_delete(struct bgp *bgp_vrf)
4548{
4549 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
4550}