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