]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_evpn.c
lib, bgpd: Address Review comments.
[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"
48
49/*
50 * Definitions and external declarations.
51 */
52extern struct zclient *zclient;
53
54DEFINE_QOBJ_TYPE(bgpevpn)
55
56
57/*
58 * Static function declarations
59 */
d62a17ae 60static void delete_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
61 afi_t afi, safi_t safi, struct bgp_node *rn,
62 struct bgp_info **ri);
63static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn);
14c1a7bf 64
65/*
66 * Private functions.
67 */
68
69/*
70 * Make vni hash key.
71 */
d62a17ae 72static unsigned int vni_hash_key_make(void *p)
14c1a7bf 73{
d62a17ae 74 struct bgpevpn *vpn = p;
75 return (jhash_1word(vpn->vni, 0));
14c1a7bf 76}
77
78/*
79 * Comparison function for vni hash
80 */
d62a17ae 81static int vni_hash_cmp(const void *p1, const void *p2)
14c1a7bf 82{
d62a17ae 83 const struct bgpevpn *vpn1 = p1;
84 const struct bgpevpn *vpn2 = p2;
14c1a7bf 85
d62a17ae 86 if (!vpn1 && !vpn2)
87 return 1;
88 if (!vpn1 || !vpn2)
89 return 0;
90 return (vpn1->vni == vpn2->vni);
14c1a7bf 91}
92
93/*
94 * Make import route target hash key.
95 */
d62a17ae 96static unsigned int import_rt_hash_key_make(void *p)
14c1a7bf 97{
d62a17ae 98 struct irt_node *irt = p;
99 char *pnt = irt->rt.val;
100 unsigned int key = 0;
101 int c = 0;
14c1a7bf 102
d62a17ae 103 key += pnt[c];
104 key += pnt[c + 1];
105 key += pnt[c + 2];
106 key += pnt[c + 3];
107 key += pnt[c + 4];
108 key += pnt[c + 5];
109 key += pnt[c + 6];
110 key += pnt[c + 7];
14c1a7bf 111
d62a17ae 112 return (key);
14c1a7bf 113}
114
115/*
116 * Comparison function for import rt hash
117 */
d62a17ae 118static int import_rt_hash_cmp(const void *p1, const void *p2)
14c1a7bf 119{
d62a17ae 120 const struct irt_node *irt1 = p1;
121 const struct irt_node *irt2 = p2;
14c1a7bf 122
d62a17ae 123 if (irt1 == NULL && irt2 == NULL)
124 return 1;
14c1a7bf 125
d62a17ae 126 if (irt1 == NULL || irt2 == NULL)
127 return 0;
14c1a7bf 128
d62a17ae 129 return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0);
14c1a7bf 130}
131
7724c0a1 132/*
128ea8ab 133 * Create a new import_rt
134 */
d62a17ae 135static struct irt_node *import_rt_new(struct bgp *bgp,
136 struct ecommunity_val *rt)
128ea8ab 137{
d62a17ae 138 struct irt_node *irt;
128ea8ab 139
d62a17ae 140 if (!bgp)
141 return NULL;
128ea8ab 142
d62a17ae 143 irt = XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT, sizeof(struct irt_node));
144 if (!irt)
145 return NULL;
128ea8ab 146
d62a17ae 147 irt->rt = *rt;
148 irt->vnis = list_new();
128ea8ab 149
d62a17ae 150 /* Add to hash */
151 if (!hash_get(bgp->import_rt_hash, irt, hash_alloc_intern)) {
152 XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
153 return NULL;
154 }
128ea8ab 155
d62a17ae 156 return irt;
128ea8ab 157}
158
159/*
160 * Free the import rt node
7724c0a1 161 */
d62a17ae 162static void import_rt_free(struct bgp *bgp, struct irt_node *irt)
7724c0a1 163{
d62a17ae 164 hash_release(bgp->import_rt_hash, irt);
165 XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
7724c0a1 166}
167
14c1a7bf 168/*
128ea8ab 169 * Function to lookup Import RT node - used to map a RT to set of
170 * VNIs importing routes with that RT.
171 */
d62a17ae 172static struct irt_node *lookup_import_rt(struct bgp *bgp,
173 struct ecommunity_val *rt)
128ea8ab 174{
d62a17ae 175 struct irt_node *irt;
176 struct irt_node tmp;
128ea8ab 177
d62a17ae 178 memset(&tmp, 0, sizeof(struct irt_node));
179 memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE);
180 irt = hash_lookup(bgp->import_rt_hash, &tmp);
181 return irt;
128ea8ab 182}
183
184/*
185 * Is specified VNI present on the RT's list of "importing" VNIs?
186 */
d62a17ae 187static int is_vni_present_in_irt_vnis(struct list *vnis, struct bgpevpn *vpn)
128ea8ab 188{
d62a17ae 189 struct listnode *node, *nnode;
190 struct bgpevpn *tmp_vpn;
128ea8ab 191
d62a17ae 192 for (ALL_LIST_ELEMENTS(vnis, node, nnode, tmp_vpn)) {
193 if (tmp_vpn == vpn)
194 return 1;
195 }
128ea8ab 196
d62a17ae 197 return 0;
128ea8ab 198}
199
200/*
201 * Compare Route Targets.
202 */
d62a17ae 203static int evpn_route_target_cmp(struct ecommunity *ecom1,
204 struct ecommunity *ecom2)
128ea8ab 205{
d62a17ae 206 if (ecom1 && !ecom2)
207 return -1;
128ea8ab 208
d62a17ae 209 if (!ecom1 && ecom2)
210 return 1;
128ea8ab 211
d62a17ae 212 if (!ecom1 && !ecom2)
213 return 0;
128ea8ab 214
d62a17ae 215 if (ecom1->str && !ecom2->str)
216 return -1;
128ea8ab 217
d62a17ae 218 if (!ecom1->str && ecom2->str)
219 return 1;
128ea8ab 220
d62a17ae 221 if (!ecom1->str && !ecom2->str)
222 return 0;
128ea8ab 223
d62a17ae 224 return strcmp(ecom1->str, ecom2->str);
128ea8ab 225}
226
227/*
228 * Mask off global-admin field of specified extended community (RT),
229 * just retain the local-admin field.
230 */
d62a17ae 231static inline void mask_ecom_global_admin(struct ecommunity_val *dst,
232 struct ecommunity_val *src)
128ea8ab 233{
d62a17ae 234 u_char type;
128ea8ab 235
d62a17ae 236 type = src->val[0];
237 dst->val[0] = 0;
238 if (type == ECOMMUNITY_ENCODE_AS) {
239 dst->val[2] = dst->val[3] = 0;
240 } else if (type == ECOMMUNITY_ENCODE_AS4
241 || type == ECOMMUNITY_ENCODE_IP) {
242 dst->val[2] = dst->val[3] = 0;
243 dst->val[4] = dst->val[5] = 0;
244 }
128ea8ab 245}
246
247/*
248 * Map one RT to specified VNI.
14c1a7bf 249 */
d62a17ae 250static void map_vni_to_rt(struct bgp *bgp, struct bgpevpn *vpn,
251 struct ecommunity_val *eval)
128ea8ab 252{
d62a17ae 253 struct irt_node *irt;
254 struct ecommunity_val eval_tmp;
128ea8ab 255
d62a17ae 256 /* If using "automatic" RT, we only care about the local-admin
257 * sub-field.
258 * This is to facilitate using VNI as the RT for EBGP peering too.
259 */
260 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
261 if (!is_import_rt_configured(vpn))
262 mask_ecom_global_admin(&eval_tmp, eval);
128ea8ab 263
d62a17ae 264 irt = lookup_import_rt(bgp, &eval_tmp);
265 if (irt && irt->vnis)
266 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
267 /* Already mapped. */
268 return;
128ea8ab 269
d62a17ae 270 if (!irt) {
271 irt = import_rt_new(bgp, &eval_tmp);
272 assert(irt);
273 }
128ea8ab 274
d62a17ae 275 /* Add VNI to the hash list for this RT. */
276 listnode_add(irt->vnis, vpn);
128ea8ab 277}
278
279/*
280 * Unmap specified VNI from specified RT. If there are no other
281 * VNIs for this RT, then the RT hash is deleted.
282 */
d62a17ae 283static void unmap_vni_from_rt(struct bgp *bgp, struct bgpevpn *vpn,
284 struct irt_node *irt)
14c1a7bf 285{
d62a17ae 286 /* Delete VNI from hash list for this RT. */
287 listnode_delete(irt->vnis, vpn);
288 if (!listnode_head(irt->vnis)) {
289 list_free(irt->vnis);
290 import_rt_free(bgp, irt);
291 }
14c1a7bf 292}
293
128ea8ab 294/*
295 * Create RT extended community automatically from passed information:
296 * of the form AS:VNI.
297 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
298 * the need is to get a RT value that will be unique across different
299 * VNIs but the same across routers (in the same AS) for a particular
300 * VNI.
301 */
d62a17ae 302static void form_auto_rt(struct bgp *bgp, struct bgpevpn *vpn, struct list *rtl)
128ea8ab 303{
d62a17ae 304 struct ecommunity_val eval;
305 struct ecommunity *ecomadd;
128ea8ab 306
d62a17ae 307 encode_route_target_as((bgp->as & 0xFFFF), vpn->vni, &eval);
128ea8ab 308
d62a17ae 309 ecomadd = ecommunity_new();
310 ecommunity_add_val(ecomadd, &eval);
311 listnode_add_sort(rtl, ecomadd);
128ea8ab 312}
14c1a7bf 313
314/*
128ea8ab 315 * Derive RD and RT for a VNI automatically. Invoked at the time of
316 * creation of a VNI.
317 */
d62a17ae 318static void derive_rd_rt_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 319{
d62a17ae 320 bgp_evpn_derive_auto_rd(bgp, vpn);
321 bgp_evpn_derive_auto_rt_import(bgp, vpn);
322 bgp_evpn_derive_auto_rt_export(bgp, vpn);
128ea8ab 323}
324
325/*
326 * Add (update) or delete MACIP from zebra.
14c1a7bf 327 */
d62a17ae 328static int bgp_zebra_send_remote_macip(struct bgp *bgp, struct bgpevpn *vpn,
329 struct prefix_evpn *p,
330 struct in_addr remote_vtep_ip, int add,
331 u_char sticky)
332{
333 struct stream *s;
334 int ipa_len;
335 char buf1[ETHER_ADDR_STRLEN];
336 char buf2[INET6_ADDRSTRLEN];
337 char buf3[INET6_ADDRSTRLEN];
338
339 /* Check socket. */
340 if (!zclient || zclient->sock < 0)
341 return 0;
342
343 /* Don't try to register if Zebra doesn't know of this instance. */
344 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
345 return 0;
346
347 s = zclient->obuf;
348 stream_reset(s);
349
421bb26a
MK
350 zclient_create_header(s, add ? ZEBRA_REMOTE_MACIP_ADD
351 : ZEBRA_REMOTE_MACIP_DEL,
352 bgp->vrf_id);
d62a17ae 353 stream_putl(s, vpn->vni);
28328ea9 354 stream_put(s, &p->prefix.mac.octet, ETH_ALEN); /* Mac Addr */
d62a17ae 355 /* IP address length and IP address, if any. */
356 if (IS_EVPN_PREFIX_IPADDR_NONE(p))
357 stream_putl(s, 0);
358 else {
359 ipa_len = IS_EVPN_PREFIX_IPADDR_V4(p) ? IPV4_MAX_BYTELEN
360 : IPV6_MAX_BYTELEN;
361 stream_putl(s, ipa_len);
362 stream_put(s, &p->prefix.ip.ip.addr, ipa_len);
363 }
364 stream_put_in_addr(s, &remote_vtep_ip);
365
366 /* TX MAC sticky status */
367 if (add)
368 stream_putc(s, sticky);
369
370 stream_putw_at(s, 0, stream_get_endp(s));
371
372 if (bgp_debug_zebra(NULL))
373 zlog_debug("Tx %s MACIP, VNI %u %sMAC %s IP %s remote VTEP %s",
374 add ? "ADD" : "DEL", vpn->vni,
375 sticky ? "sticky " : "",
376 prefix_mac2str(&p->prefix.mac, buf1, sizeof(buf1)),
377 ipaddr2str(&p->prefix.ip, buf3, sizeof(buf3)),
378 inet_ntop(AF_INET, &remote_vtep_ip, buf2,
379 sizeof(buf2)));
380
381 return zclient_send_message(zclient);
7ef5a232 382}
b18825eb 383
128ea8ab 384/*
385 * Add (update) or delete remote VTEP from zebra.
386 */
d62a17ae 387static int bgp_zebra_send_remote_vtep(struct bgp *bgp, struct bgpevpn *vpn,
388 struct prefix_evpn *p, int add)
128ea8ab 389{
d62a17ae 390 struct stream *s;
128ea8ab 391
d62a17ae 392 /* Check socket. */
393 if (!zclient || zclient->sock < 0)
394 return 0;
128ea8ab 395
d62a17ae 396 /* Don't try to register if Zebra doesn't know of this instance. */
397 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
398 return 0;
128ea8ab 399
d62a17ae 400 s = zclient->obuf;
401 stream_reset(s);
128ea8ab 402
421bb26a
MK
403 zclient_create_header(s, add ? ZEBRA_REMOTE_VTEP_ADD
404 : ZEBRA_REMOTE_VTEP_DEL,
405 bgp->vrf_id);
d62a17ae 406 stream_putl(s, vpn->vni);
407 if (IS_EVPN_PREFIX_IPADDR_V4(p))
408 stream_put_in_addr(s, &p->prefix.ip.ipaddr_v4);
409 else if (IS_EVPN_PREFIX_IPADDR_V6(p)) {
410 zlog_err(
411 "Bad remote IP when trying to %s remote VTEP for VNI %u",
412 add ? "ADD" : "DEL", vpn->vni);
413 return -1;
414 }
128ea8ab 415
d62a17ae 416 stream_putw_at(s, 0, stream_get_endp(s));
128ea8ab 417
d62a17ae 418 if (bgp_debug_zebra(NULL))
419 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
420 add ? "ADD" : "DEL", vpn->vni,
421 inet_ntoa(p->prefix.ip.ipaddr_v4));
128ea8ab 422
d62a17ae 423 return zclient_send_message(zclient);
128ea8ab 424}
425
426/*
427 * Build extended communities for EVPN route. RT and ENCAP are
428 * applicable to all routes.
429 */
d62a17ae 430static void build_evpn_route_extcomm(struct bgpevpn *vpn, struct attr *attr)
128ea8ab 431{
d62a17ae 432 struct ecommunity ecom_encap;
433 struct ecommunity ecom_sticky;
434 struct ecommunity_val eval;
435 struct ecommunity_val eval_sticky;
436 bgp_encap_types tnl_type;
437 struct listnode *node, *nnode;
438 struct ecommunity *ecom;
439 u_int32_t seqnum;
128ea8ab 440
d62a17ae 441 /* Encap */
442 tnl_type = BGP_ENCAP_TYPE_VXLAN;
443 memset(&ecom_encap, 0, sizeof(ecom_encap));
444 encode_encap_extcomm(tnl_type, &eval);
445 ecom_encap.size = 1;
446 ecom_encap.val = (u_int8_t *)eval.val;
128ea8ab 447
d62a17ae 448 /* Add Encap */
449 attr->ecommunity = ecommunity_dup(&ecom_encap);
128ea8ab 450
d62a17ae 451 /* Add the export RTs */
452 for (ALL_LIST_ELEMENTS(vpn->export_rtl, node, nnode, ecom))
453 attr->ecommunity = ecommunity_merge(attr->ecommunity, ecom);
128ea8ab 454
d62a17ae 455 if (attr->sticky) {
456 seqnum = 0;
457 memset(&ecom_sticky, 0, sizeof(ecom_sticky));
458 encode_mac_mobility_extcomm(1, seqnum, &eval_sticky);
459 ecom_sticky.size = 1;
460 ecom_sticky.val = (u_int8_t *)eval_sticky.val;
461 attr->ecommunity =
462 ecommunity_merge(attr->ecommunity, &ecom_sticky);
463 }
c85c03c7 464
d62a17ae 465 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
128ea8ab 466}
467
468/*
469 * Add MAC mobility extended community to attribute.
470 */
d62a17ae 471static void add_mac_mobility_to_attr(u_int32_t seq_num, struct attr *attr)
472{
473 struct ecommunity ecom_tmp;
474 struct ecommunity_val eval;
421bb26a 475 u_int8_t *ecom_val_ptr;
d62a17ae 476 int i;
477 u_int8_t *pnt;
478 int type = 0;
479 int sub_type = 0;
480
481 /* Build MM */
482 encode_mac_mobility_extcomm(0, seq_num, &eval);
483
484 /* Find current MM ecommunity */
421bb26a 485 ecom_val_ptr = NULL;
d62a17ae 486
487 if (attr->ecommunity) {
488 for (i = 0; i < attr->ecommunity->size; i++) {
489 pnt = attr->ecommunity->val + (i * 8);
490 type = *pnt++;
491 sub_type = *pnt++;
492
493 if (type == ECOMMUNITY_ENCODE_EVPN
494 && sub_type
495 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY) {
421bb26a
MK
496 ecom_val_ptr =
497 (u_int8_t *)(attr->ecommunity->val
498 + (i * 8));
d62a17ae 499 break;
500 }
501 }
502 }
503
504 /* Update the existing MM ecommunity */
421bb26a
MK
505 if (ecom_val_ptr) {
506 memcpy(ecom_val_ptr, eval.val, sizeof(char) * ECOMMUNITY_SIZE);
d62a17ae 507 }
508 /* Add MM to existing */
509 else {
510 memset(&ecom_tmp, 0, sizeof(ecom_tmp));
511 ecom_tmp.size = 1;
512 ecom_tmp.val = (u_int8_t *)eval.val;
513
514 attr->ecommunity =
515 ecommunity_merge(attr->ecommunity, &ecom_tmp);
516 }
128ea8ab 517}
518
519/* Install EVPN route into zebra. */
d62a17ae 520static int evpn_zebra_install(struct bgp *bgp, struct bgpevpn *vpn,
521 struct prefix_evpn *p,
522 struct in_addr remote_vtep_ip, u_char sticky)
128ea8ab 523{
d62a17ae 524 int ret;
128ea8ab 525
d62a17ae 526 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
527 ret = bgp_zebra_send_remote_macip(bgp, vpn, p, remote_vtep_ip,
528 1, sticky);
529 else
530 ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, 1);
128ea8ab 531
d62a17ae 532 return ret;
128ea8ab 533}
534
535/* Uninstall EVPN route from zebra. */
d62a17ae 536static int evpn_zebra_uninstall(struct bgp *bgp, struct bgpevpn *vpn,
537 struct prefix_evpn *p,
538 struct in_addr remote_vtep_ip)
128ea8ab 539{
d62a17ae 540 int ret;
128ea8ab 541
d62a17ae 542 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
543 ret = bgp_zebra_send_remote_macip(bgp, vpn, p, remote_vtep_ip,
544 0, 0);
545 else
546 ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, 0);
128ea8ab 547
d62a17ae 548 return ret;
128ea8ab 549}
550
551/*
552 * Due to MAC mobility, the prior "local" best route has been supplanted
553 * by a "remote" best route. The prior route has to be deleted and withdrawn
554 * from peers.
555 */
d62a17ae 556static void evpn_delete_old_local_route(struct bgp *bgp, struct bgpevpn *vpn,
557 struct bgp_node *rn,
558 struct bgp_info *old_local)
128ea8ab 559{
d62a17ae 560 struct bgp_node *global_rn;
561 struct bgp_info *ri;
562 afi_t afi = AFI_L2VPN;
563 safi_t safi = SAFI_EVPN;
128ea8ab 564
d62a17ae 565 /* Locate route node in the global EVPN routing table. Note that
566 * this table is a 2-level tree (RD-level + Prefix-level) similar to
567 * L3VPN routes.
568 */
569 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
570 (struct prefix *)&rn->p, &vpn->prd);
571 if (global_rn) {
572 /* Delete route entry in the global EVPN table. */
573 delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);
128ea8ab 574
d62a17ae 575 /* Schedule for processing - withdraws to peers happen from
576 * this table.
577 */
578 if (ri)
579 bgp_process(bgp, global_rn, afi, safi);
580 bgp_unlock_node(global_rn);
581 }
128ea8ab 582
d62a17ae 583 /* Delete route entry in the VNI route table, caller to remove. */
584 bgp_info_delete(rn, old_local);
128ea8ab 585}
586
587/*
588 * Calculate the best path for an EVPN route. Install/update best path in zebra,
589 * if appropriate.
590 */
d62a17ae 591static int evpn_route_select_install(struct bgp *bgp, struct bgpevpn *vpn,
592 struct bgp_node *rn)
593{
594 struct bgp_info *old_select, *new_select;
595 struct bgp_info_pair old_and_new;
596 afi_t afi = AFI_L2VPN;
597 safi_t safi = SAFI_EVPN;
598 int ret = 0;
599
600 /* Compute the best path. */
601 bgp_best_selection(bgp, rn, &bgp->maxpaths[afi][safi], &old_and_new,
602 afi, safi);
603 old_select = old_and_new.old;
604 new_select = old_and_new.new;
605
606 /* If the best path hasn't changed - see if there is still something to
607 * update
608 * to zebra RIB.
609 */
610 if (old_select && old_select == new_select
611 && old_select->type == ZEBRA_ROUTE_BGP
612 && old_select->sub_type == BGP_ROUTE_NORMAL
613 && !CHECK_FLAG(rn->flags, BGP_NODE_USER_CLEAR)
614 && !CHECK_FLAG(old_select->flags, BGP_INFO_ATTR_CHANGED)
615 && !bgp->addpath_tx_used[afi][safi]) {
616 if (bgp_zebra_has_route_changed(rn, old_select))
617 ret = evpn_zebra_install(bgp, vpn,
618 (struct prefix_evpn *)&rn->p,
619 old_select->attr->nexthop,
620 old_select->attr->sticky);
621 UNSET_FLAG(old_select->flags, BGP_INFO_MULTIPATH_CHG);
622 bgp_zebra_clear_route_change_flags(rn);
623 return ret;
624 }
625
626 /* If the user did a "clear" this flag will be set */
627 UNSET_FLAG(rn->flags, BGP_NODE_USER_CLEAR);
628
629 /* bestpath has changed; update relevant fields and install or uninstall
630 * into the zebra RIB.
631 */
632 if (old_select || new_select)
633 bgp_bump_version(rn);
634
635 if (old_select)
636 bgp_info_unset_flag(rn, old_select, BGP_INFO_SELECTED);
637 if (new_select) {
638 bgp_info_set_flag(rn, new_select, BGP_INFO_SELECTED);
639 bgp_info_unset_flag(rn, new_select, BGP_INFO_ATTR_CHANGED);
640 UNSET_FLAG(new_select->flags, BGP_INFO_MULTIPATH_CHG);
641 }
642
643 if (new_select && new_select->type == ZEBRA_ROUTE_BGP
644 && new_select->sub_type == BGP_ROUTE_NORMAL) {
645 ret = evpn_zebra_install(bgp, vpn, (struct prefix_evpn *)&rn->p,
646 new_select->attr->nexthop,
647 new_select->attr->sticky);
648 /* If an old best existed and it was a "local" route, the only
649 * reason
650 * it would be supplanted is due to MAC mobility procedures. So,
651 * we
652 * need to do an implicit delete and withdraw that route from
653 * peers.
654 */
655 if (old_select && old_select->peer == bgp->peer_self
656 && old_select->type == ZEBRA_ROUTE_BGP
657 && old_select->sub_type == BGP_ROUTE_STATIC)
658 evpn_delete_old_local_route(bgp, vpn, rn, old_select);
659 } else {
660 if (old_select && old_select->type == ZEBRA_ROUTE_BGP
661 && old_select->sub_type == BGP_ROUTE_NORMAL)
662 ret = evpn_zebra_uninstall(bgp, vpn,
663 (struct prefix_evpn *)&rn->p,
664 old_select->attr->nexthop);
665 }
666
667 /* Clear any route change flags. */
668 bgp_zebra_clear_route_change_flags(rn);
669
670 /* Reap old select bgp_info, if it has been removed */
671 if (old_select && CHECK_FLAG(old_select->flags, BGP_INFO_REMOVED))
672 bgp_info_reap(rn, old_select);
673
674 return ret;
128ea8ab 675}
676
c85c03c7 677
678/*
679 * Return true if the local ri for this rn has sticky set
680 */
d62a17ae 681static int evpn_route_is_sticky(struct bgp *bgp, struct bgp_node *rn)
c85c03c7 682{
d62a17ae 683 struct bgp_info *tmp_ri;
684 struct bgp_info *local_ri;
c85c03c7 685
d62a17ae 686 local_ri = NULL;
687 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
688 if (tmp_ri->peer == bgp->peer_self
689 && tmp_ri->type == ZEBRA_ROUTE_BGP
690 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
691 local_ri = tmp_ri;
692 }
c85c03c7 693
d62a17ae 694 if (!local_ri)
695 return 0;
c85c03c7 696
d62a17ae 697 return local_ri->attr->sticky;
c85c03c7 698}
699
128ea8ab 700/*
701 * Create or update EVPN route entry. This could be in the VNI route table
702 * or the global route table.
703 */
d62a17ae 704static int update_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
705 afi_t afi, safi_t safi, struct bgp_node *rn,
706 struct attr *attr, int add, int vni_table,
1a98c087 707 struct bgp_info **ri, u_char flags)
d62a17ae 708{
709 struct bgp_info *tmp_ri;
710 struct bgp_info *local_ri, *remote_ri;
711 struct attr *attr_new;
712 mpls_label_t label = MPLS_INVALID_LABEL;
713 int route_change = 1;
714 u_char sticky = 0;
715
716 *ri = NULL;
717
718 /* See if this is an update of an existing route, or a new add. Also,
719 * identify if already known from remote, and if so, the one with the
720 * highest sequence number; this is only when adding to the VNI routing
721 * table.
722 */
723 local_ri = remote_ri = NULL;
724 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
725 if (tmp_ri->peer == bgp->peer_self
726 && tmp_ri->type == ZEBRA_ROUTE_BGP
727 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
728 local_ri = tmp_ri;
729 if (vni_table) {
730 if (tmp_ri->type == ZEBRA_ROUTE_BGP
731 && tmp_ri->sub_type == BGP_ROUTE_NORMAL
732 && CHECK_FLAG(tmp_ri->flags, BGP_INFO_VALID)) {
733 if (!remote_ri)
734 remote_ri = tmp_ri;
735 else if (mac_mobility_seqnum(tmp_ri->attr)
736 > mac_mobility_seqnum(remote_ri->attr))
737 remote_ri = tmp_ri;
738 }
739 }
740 }
741
742 /* If route doesn't exist already, create a new one, if told to.
743 * Otherwise act based on whether the attributes of the route have
744 * changed or not.
745 */
746 if (!local_ri && !add)
747 return 0;
748
749 if (!local_ri) {
750 /* When learnt locally for the first time but already known from
751 * remote, we have to initiate appropriate MAC mobility steps.
752 * This
753 * is applicable when updating the VNI routing table.
1a98c087
MK
754 * We need to skip mobility steps for g/w macs (local mac on g/w
755 * SVI) advertised in EVPN.
756 * This will ensure that local routes are preferred for g/w macs
d62a17ae 757 */
1a98c087 758 if (remote_ri && !CHECK_FLAG(flags, ZEBRA_MAC_TYPE_GW)) {
d62a17ae 759 u_int32_t cur_seqnum;
760
761 /* Add MM extended community to route. */
762 cur_seqnum = mac_mobility_seqnum(remote_ri->attr);
763 add_mac_mobility_to_attr(cur_seqnum + 1, attr);
764 }
765
766 /* Add (or update) attribute to hash. */
767 attr_new = bgp_attr_intern(attr);
768
769 /* Extract MAC mobility sequence number, if any. */
770 attr_new->mm_seqnum =
771 bgp_attr_mac_mobility_seqnum(attr_new, &sticky);
772 attr_new->sticky = sticky;
773
774 /* Create new route with its attribute. */
775 tmp_ri = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0,
776 bgp->peer_self, attr_new, rn);
777 SET_FLAG(tmp_ri->flags, BGP_INFO_VALID);
778 bgp_info_extra_get(tmp_ri);
779
780 /* The VNI goes into the 'label' field of the route */
781 vni2label(vpn->vni, &label);
782
783 memcpy(&tmp_ri->extra->label, &label, BGP_LABEL_BYTES);
784 bgp_info_add(rn, tmp_ri);
785 } else {
786 tmp_ri = local_ri;
787 if (attrhash_cmp(tmp_ri->attr, attr)
788 && !CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED))
789 route_change = 0;
790 else {
791 /* The attribute has changed. */
792 /* Add (or update) attribute to hash. */
793 attr_new = bgp_attr_intern(attr);
794 bgp_info_set_flag(rn, tmp_ri, BGP_INFO_ATTR_CHANGED);
795
796 /* Restore route, if needed. */
797 if (CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED))
798 bgp_info_restore(rn, tmp_ri);
799
800 /* Unintern existing, set to new. */
801 bgp_attr_unintern(&tmp_ri->attr);
802 tmp_ri->attr = attr_new;
803 tmp_ri->uptime = bgp_clock();
804 }
805 }
806
807 /* Return back the route entry. */
808 *ri = tmp_ri;
809 return route_change;
128ea8ab 810}
811
812/*
813 * Create or update EVPN route (of type based on prefix) for specified VNI
814 * and schedule for processing.
815 */
d62a17ae 816static int update_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
1a98c087 817 struct prefix_evpn *p, u_char flags)
128ea8ab 818{
d62a17ae 819 struct bgp_node *rn;
820 struct attr attr;
821 struct attr *attr_new;
822 struct bgp_info *ri;
823 afi_t afi = AFI_L2VPN;
824 safi_t safi = SAFI_EVPN;
825 int route_change;
128ea8ab 826
d62a17ae 827 memset(&attr, 0, sizeof(struct attr));
128ea8ab 828
d62a17ae 829 /* Build path-attribute for this route. */
830 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
831 attr.nexthop = vpn->originator_ip;
832 attr.mp_nexthop_global_in = vpn->originator_ip;
833 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1a98c087 834 attr.sticky = CHECK_FLAG(flags, ZEBRA_MAC_TYPE_STICKY) ? 1 : 0;
128ea8ab 835
d62a17ae 836 /* Set up RT and ENCAP extended community. */
837 build_evpn_route_extcomm(vpn, &attr);
128ea8ab 838
d62a17ae 839 /* First, create (or fetch) route node within the VNI. */
840 /* NOTE: There is no RD here. */
841 rn = bgp_node_get(vpn->route_table, (struct prefix *)p);
128ea8ab 842
d62a17ae 843 /* Create or update route entry. */
844 route_change = update_evpn_route_entry(bgp, vpn, afi, safi, rn, &attr,
1a98c087 845 1, 1, &ri, flags);
d62a17ae 846 assert(ri);
847 attr_new = ri->attr;
128ea8ab 848
d62a17ae 849 /* Perform route selection; this is just to set the flags correctly
850 * as local route in the VNI always wins.
851 */
852 evpn_route_select_install(bgp, vpn, rn);
853 bgp_unlock_node(rn);
128ea8ab 854
d62a17ae 855 /* If this is a new route or some attribute has changed, export the
856 * route to the global table. The route will be advertised to peers
857 * from there. Note that this table is a 2-level tree (RD-level +
858 * Prefix-level) similar to L3VPN routes.
859 */
860 if (route_change) {
861 struct bgp_info *global_ri;
128ea8ab 862
d62a17ae 863 rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
864 (struct prefix *)p, &vpn->prd);
865 update_evpn_route_entry(bgp, vpn, afi, safi, rn, attr_new, 1, 0,
1a98c087 866 &global_ri, flags);
128ea8ab 867
d62a17ae 868 /* Schedule for processing and unlock node. */
869 bgp_process(bgp, rn, afi, safi);
870 bgp_unlock_node(rn);
871 }
128ea8ab 872
d62a17ae 873 /* Unintern temporary. */
874 aspath_unintern(&attr.aspath);
128ea8ab 875
d62a17ae 876 return 0;
128ea8ab 877}
878
879/*
880 * Delete EVPN route entry. This could be in the VNI route table
881 * or the global route table.
882 */
d62a17ae 883static void delete_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
884 afi_t afi, safi_t safi, struct bgp_node *rn,
885 struct bgp_info **ri)
128ea8ab 886{
d62a17ae 887 struct bgp_info *tmp_ri;
128ea8ab 888
d62a17ae 889 *ri = NULL;
128ea8ab 890
d62a17ae 891 /* Now, find matching route. */
892 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next)
893 if (tmp_ri->peer == bgp->peer_self
894 && tmp_ri->type == ZEBRA_ROUTE_BGP
895 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
896 break;
128ea8ab 897
d62a17ae 898 *ri = tmp_ri;
128ea8ab 899
d62a17ae 900 /* Mark route for delete. */
901 if (tmp_ri)
902 bgp_info_delete(rn, tmp_ri);
128ea8ab 903}
904
905/*
906 * Delete EVPN route (of type based on prefix) for specified VNI and
907 * schedule for processing.
908 */
d62a17ae 909static int delete_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
910 struct prefix_evpn *p)
911{
912 struct bgp_node *rn, *global_rn;
913 struct bgp_info *ri;
914 afi_t afi = AFI_L2VPN;
915 safi_t safi = SAFI_EVPN;
916
917 /* First, locate the route node within the VNI. If it doesn't exist,
918 * there
919 * is nothing further to do.
920 */
921 /* NOTE: There is no RD here. */
922 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
923 if (!rn)
924 return 0;
925
926 /* Next, locate route node in the global EVPN routing table. Note that
927 * this table is a 2-level tree (RD-level + Prefix-level) similar to
928 * L3VPN routes.
929 */
930 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
931 (struct prefix *)p, &vpn->prd);
932 if (global_rn) {
933 /* Delete route entry in the global EVPN table. */
934 delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);
935
936 /* Schedule for processing - withdraws to peers happen from
937 * this table.
938 */
939 if (ri)
940 bgp_process(bgp, global_rn, afi, safi);
941 bgp_unlock_node(global_rn);
942 }
943
944 /* Delete route entry in the VNI route table. This can just be removed.
945 */
946 delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);
947 if (ri)
948 bgp_info_reap(rn, ri);
949 bgp_unlock_node(rn);
950
951 return 0;
128ea8ab 952}
953
954/*
955 * Update all type-2 (MACIP) local routes for this VNI - these should also
956 * be scheduled for advertise to peers.
957 */
d62a17ae 958static int update_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
959{
960 afi_t afi;
961 safi_t safi;
962 struct bgp_node *rn;
963 struct bgp_info *ri;
964 struct attr attr;
965 struct attr attr_sticky;
966 struct attr *attr_new;
967
968 afi = AFI_L2VPN;
969 safi = SAFI_EVPN;
970 memset(&attr, 0, sizeof(struct attr));
971 memset(&attr_sticky, 0, sizeof(struct attr));
972
973 /* Build path-attribute - all type-2 routes for this VNI will share the
974 * same path attribute.
975 */
976 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
977 bgp_attr_default_set(&attr_sticky, BGP_ORIGIN_IGP);
978 attr.nexthop = vpn->originator_ip;
979 attr.mp_nexthop_global_in = vpn->originator_ip;
980 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
981 attr_sticky.nexthop = vpn->originator_ip;
982 attr_sticky.mp_nexthop_global_in = vpn->originator_ip;
983 attr_sticky.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
984 attr_sticky.sticky = 1;
985
986 /* Set up RT, ENCAP and sticky MAC extended community. */
987 build_evpn_route_extcomm(vpn, &attr);
988 build_evpn_route_extcomm(vpn, &attr_sticky);
989
990 /* Walk this VNI's route table and update local type-2 routes. For any
991 * routes updated, update corresponding entry in the global table too.
992 */
993 for (rn = bgp_table_top(vpn->route_table); rn;
994 rn = bgp_route_next(rn)) {
995 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
996 struct bgp_node *rd_rn;
997 struct bgp_info *global_ri;
998
999 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1000 continue;
1001
1002 if (evpn_route_is_sticky(bgp, rn))
1003 update_evpn_route_entry(bgp, vpn, afi, safi, rn,
1a98c087 1004 &attr_sticky, 0, 1, &ri, 0);
d62a17ae 1005 else
1006 update_evpn_route_entry(bgp, vpn, afi, safi, rn, &attr,
1a98c087 1007 0, 1, &ri, 0);
d62a17ae 1008
1009 /* If a local route exists for this prefix, we need to update
1010 * the global routing table too.
1011 */
1012 if (!ri)
1013 continue;
1014
1015 /* Perform route selection; this is just to set the flags
1016 * correctly
1017 * as local route in the VNI always wins.
1018 */
1019 evpn_route_select_install(bgp, vpn, rn);
1020
1021 attr_new = ri->attr;
1022
1023 /* Update route in global routing table. */
1024 rd_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
1025 (struct prefix *)evp, &vpn->prd);
1026 assert(rd_rn);
1027 update_evpn_route_entry(bgp, vpn, afi, safi, rd_rn, attr_new, 0,
1a98c087 1028 0, &global_ri, 0);
d62a17ae 1029
1030 /* Schedule for processing and unlock node. */
1031 bgp_process(bgp, rd_rn, afi, safi);
1032 bgp_unlock_node(rd_rn);
1033 }
1034
1035 /* Unintern temporary. */
1036 aspath_unintern(&attr.aspath);
1037 aspath_unintern(&attr_sticky.aspath);
1038
1039 return 0;
128ea8ab 1040}
1041
1042/*
1043 * Delete all type-2 (MACIP) local routes for this VNI - only from the
1044 * global routing table. These are also scheduled for withdraw from peers.
1045 */
d62a17ae 1046static int delete_global_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1047{
d62a17ae 1048 afi_t afi;
1049 safi_t safi;
1050 struct bgp_node *rdrn, *rn;
1051 struct bgp_table *table;
1052 struct bgp_info *ri;
128ea8ab 1053
d62a17ae 1054 afi = AFI_L2VPN;
1055 safi = SAFI_EVPN;
128ea8ab 1056
d62a17ae 1057 rdrn = bgp_node_lookup(bgp->rib[afi][safi], (struct prefix *)&vpn->prd);
1058 if (rdrn && rdrn->info) {
1059 table = (struct bgp_table *)rdrn->info;
1060 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
1061 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
128ea8ab 1062
d62a17ae 1063 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1064 continue;
128ea8ab 1065
d62a17ae 1066 delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);
1067 if (ri)
1068 bgp_process(bgp, rn, afi, safi);
1069 }
1070 }
128ea8ab 1071
d62a17ae 1072 /* Unlock RD node. */
1073 if (rdrn)
1074 bgp_unlock_node(rdrn);
128ea8ab 1075
d62a17ae 1076 return 0;
128ea8ab 1077}
1078
1079/*
1080 * Delete all type-2 (MACIP) local routes for this VNI - from the global
1081 * table as well as the per-VNI route table.
1082 */
d62a17ae 1083static int delete_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1084{
d62a17ae 1085 afi_t afi;
1086 safi_t safi;
1087 struct bgp_node *rn;
1088 struct bgp_info *ri;
128ea8ab 1089
d62a17ae 1090 afi = AFI_L2VPN;
1091 safi = SAFI_EVPN;
128ea8ab 1092
d62a17ae 1093 /* First, walk the global route table for this VNI's type-2 local
1094 * routes.
1095 * EVPN routes are a 2-level table, first get the RD table.
1096 */
1097 delete_global_type2_routes(bgp, vpn);
128ea8ab 1098
d62a17ae 1099 /* Next, walk this VNI's route table and delete local type-2 routes. */
1100 for (rn = bgp_table_top(vpn->route_table); rn;
1101 rn = bgp_route_next(rn)) {
1102 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
128ea8ab 1103
d62a17ae 1104 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1105 continue;
128ea8ab 1106
d62a17ae 1107 delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);
128ea8ab 1108
d62a17ae 1109 /* Route entry in local table gets deleted immediately. */
1110 if (ri)
1111 bgp_info_reap(rn, ri);
1112 }
128ea8ab 1113
d62a17ae 1114 return 0;
128ea8ab 1115}
1116
1117/*
1118 * Delete all routes in the per-VNI route table.
1119 */
d62a17ae 1120static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1121{
d62a17ae 1122 struct bgp_node *rn;
1123 struct bgp_info *ri, *nextri;
128ea8ab 1124
d62a17ae 1125 /* Walk this VNI's route table and delete all routes. */
1126 for (rn = bgp_table_top(vpn->route_table); rn;
1127 rn = bgp_route_next(rn)) {
1128 for (ri = rn->info; (ri != NULL) && (nextri = ri->next, 1);
1129 ri = nextri) {
1130 bgp_info_delete(rn, ri);
1131 bgp_info_reap(rn, ri);
1132 }
1133 }
128ea8ab 1134
d62a17ae 1135 return 0;
128ea8ab 1136}
1137
1138/*
1139 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
1140 * export RT getting modified or change to tunnel IP. Note that these
1141 * situations need the route in the per-VNI table as well as the global
1142 * table to be updated (as attributes change).
1143 */
d62a17ae 1144static int update_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1145{
d62a17ae 1146 int ret;
1147 struct prefix_evpn p;
128ea8ab 1148
d62a17ae 1149 /* Update and advertise the type-3 route (only one) followed by the
1150 * locally learnt type-2 routes (MACIP) - for this VNI.
1151 */
1152 build_evpn_type3_prefix(&p, vpn->originator_ip);
1153 ret = update_evpn_route(bgp, vpn, &p, 0);
1154 if (ret)
1155 return ret;
128ea8ab 1156
d62a17ae 1157 return update_all_type2_routes(bgp, vpn);
128ea8ab 1158}
1159
1160/*
1161 * Delete (and withdraw) local routes for specified VNI from the global
1162 * table and per-VNI table. After this, remove all other routes from
1163 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
1164 * (advertise-all-vni) being disabled.
1165 */
d62a17ae 1166static int delete_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1167{
d62a17ae 1168 int ret;
1169 struct prefix_evpn p;
128ea8ab 1170
d62a17ae 1171 /* Delete and withdraw locally learnt type-2 routes (MACIP)
1172 * followed by type-3 routes (only one) - for this VNI.
1173 */
1174 ret = delete_all_type2_routes(bgp, vpn);
1175 if (ret)
1176 return ret;
128ea8ab 1177
d62a17ae 1178 build_evpn_type3_prefix(&p, vpn->originator_ip);
1179 ret = delete_evpn_route(bgp, vpn, &p);
1180 if (ret)
1181 return ret;
128ea8ab 1182
d62a17ae 1183 /* Delete all routes from the per-VNI table. */
1184 return delete_all_vni_routes(bgp, vpn);
128ea8ab 1185}
1186
1187/*
1188 * There is a tunnel endpoint IP address change for this VNI,
1189 * need to re-advertise routes with the new nexthop.
1190 */
d62a17ae 1191static int handle_tunnel_ip_change(struct bgp *bgp, struct bgpevpn *vpn,
1192 struct in_addr originator_ip)
128ea8ab 1193{
d62a17ae 1194 struct prefix_evpn p;
128ea8ab 1195
ddd16ed5
MK
1196 /* If VNI is not live, we only need to update the originator ip */
1197 if (!is_vni_live(vpn)) {
1198 vpn->originator_ip = originator_ip;
1199 return 0;
1200 }
1201
d62a17ae 1202 /* Need to withdraw type-3 route as the originator IP is part
1203 * of the key.
1204 */
1205 build_evpn_type3_prefix(&p, vpn->originator_ip);
1206 delete_evpn_route(bgp, vpn, &p);
128ea8ab 1207
d62a17ae 1208 /* Update the tunnel IP and re-advertise all routes for this VNI. */
1209 vpn->originator_ip = originator_ip;
1210 return update_routes_for_vni(bgp, vpn);
128ea8ab 1211}
1212
1213/*
1214 * Install route entry into the VNI routing table and invoke route selection.
1215 */
d62a17ae 1216static int install_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
1217 struct prefix_evpn *p,
1218 struct bgp_info *parent_ri)
1219{
1220 struct bgp_node *rn;
1221 struct bgp_info *ri;
1222 struct attr *attr_new;
1223 int ret;
1224
1225 /* Create (or fetch) route within the VNI. */
1226 /* NOTE: There is no RD here. */
1227 rn = bgp_node_get(vpn->route_table, (struct prefix *)p);
1228
1229 /* Check if route entry is already present. */
1230 for (ri = rn->info; ri; ri = ri->next)
1231 if (ri->extra
1232 && (struct bgp_info *)ri->extra->parent == parent_ri)
1233 break;
1234
1235 if (!ri) {
1236 /* Add (or update) attribute to hash. */
1237 attr_new = bgp_attr_intern(parent_ri->attr);
1238
1239 /* Create new route with its attribute. */
1240 ri = info_make(parent_ri->type, parent_ri->sub_type, 0,
1241 parent_ri->peer, attr_new, rn);
1242 SET_FLAG(ri->flags, BGP_INFO_VALID);
1243 bgp_info_extra_get(ri);
1244 ri->extra->parent = parent_ri;
1245 if (parent_ri->extra)
1246 memcpy(&ri->extra->label, &parent_ri->extra->label,
1247 BGP_LABEL_BYTES);
1248 bgp_info_add(rn, ri);
1249 } else {
1250 if (attrhash_cmp(ri->attr, parent_ri->attr)
1251 && !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) {
1252 bgp_unlock_node(rn);
1253 return 0;
1254 }
1255 /* The attribute has changed. */
1256 /* Add (or update) attribute to hash. */
1257 attr_new = bgp_attr_intern(parent_ri->attr);
1258
1259 /* Restore route, if needed. */
1260 if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
1261 bgp_info_restore(rn, ri);
1262
1263 /* Mark if nexthop has changed. */
1264 if (!IPV4_ADDR_SAME(&ri->attr->nexthop, &attr_new->nexthop))
1265 SET_FLAG(ri->flags, BGP_INFO_IGP_CHANGED);
1266
1267 /* Unintern existing, set to new. */
1268 bgp_attr_unintern(&ri->attr);
1269 ri->attr = attr_new;
1270 ri->uptime = bgp_clock();
1271 }
1272
1273 /* Perform route selection and update zebra, if required. */
1274 ret = evpn_route_select_install(bgp, vpn, rn);
1275
1276 return ret;
128ea8ab 1277}
1278
1279/*
1280 * Uninstall route entry from the VNI routing table and send message
1281 * to zebra, if appropriate.
1282 */
d62a17ae 1283static int uninstall_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
1284 struct prefix_evpn *p,
1285 struct bgp_info *parent_ri)
128ea8ab 1286{
d62a17ae 1287 struct bgp_node *rn;
1288 struct bgp_info *ri;
1289 int ret;
128ea8ab 1290
d62a17ae 1291 /* Locate route within the VNI. */
1292 /* NOTE: There is no RD here. */
1293 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
1294 if (!rn)
1295 return 0;
128ea8ab 1296
d62a17ae 1297 /* Find matching route entry. */
1298 for (ri = rn->info; ri; ri = ri->next)
1299 if (ri->extra
1300 && (struct bgp_info *)ri->extra->parent == parent_ri)
1301 break;
128ea8ab 1302
d62a17ae 1303 if (!ri)
1304 return 0;
128ea8ab 1305
d62a17ae 1306 /* Mark entry for deletion */
1307 bgp_info_delete(rn, ri);
128ea8ab 1308
d62a17ae 1309 /* Perform route selection and update zebra, if required. */
1310 ret = evpn_route_select_install(bgp, vpn, rn);
128ea8ab 1311
d62a17ae 1312 /* Unlock route node. */
1313 bgp_unlock_node(rn);
128ea8ab 1314
d62a17ae 1315 return ret;
128ea8ab 1316}
1317
1318/*
1319 * Given a route entry and a VNI, see if this route entry should be
1320 * imported into the VNI i.e., RTs match.
1321 */
d62a17ae 1322static int is_route_matching_for_vni(struct bgp *bgp, struct bgpevpn *vpn,
1323 struct bgp_info *ri)
1324{
1325 struct attr *attr = ri->attr;
1326 struct ecommunity *ecom;
1327 int i;
1328
1329 assert(attr);
1330 /* Route should have valid RT to be even considered. */
1331 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
1332 return 0;
1333
1334 ecom = attr->ecommunity;
1335 if (!ecom || !ecom->size)
1336 return 0;
1337
1338 /* For each extended community RT, see if it matches this VNI. If any RT
1339 * matches, we're done.
1340 */
1341 for (i = 0; i < ecom->size; i++) {
1342 u_char *pnt;
1343 u_char type, sub_type;
1344 struct ecommunity_val *eval;
1345 struct ecommunity_val eval_tmp;
1346 struct irt_node *irt;
1347
1348 /* Only deal with RTs */
1349 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
1350 eval = (struct ecommunity_val *)(ecom->val
1351 + (i * ECOMMUNITY_SIZE));
1352 type = *pnt++;
1353 sub_type = *pnt++;
1354 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
1355 continue;
1356
1357 /* See if this RT matches specified VNIs import RTs */
1358 irt = lookup_import_rt(bgp, eval);
1359 if (irt && irt->vnis)
1360 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
1361 return 1;
1362
1363 /* Also check for non-exact match. In this, we mask out the AS
1364 * and
1365 * only check on the local-admin sub-field. This is to
1366 * facilitate using
1367 * VNI as the RT for EBGP peering too.
1368 */
1369 irt = NULL;
1370 if (type == ECOMMUNITY_ENCODE_AS
1371 || type == ECOMMUNITY_ENCODE_AS4
1372 || type == ECOMMUNITY_ENCODE_IP) {
1373 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
1374 mask_ecom_global_admin(&eval_tmp, eval);
1375 irt = lookup_import_rt(bgp, &eval_tmp);
1376 }
1377 if (irt && irt->vnis)
1378 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
1379 return 1;
1380 }
1381
1382 return 0;
128ea8ab 1383}
1384
1385/*
1386 * Install or uninstall routes of specified type that are appropriate for this
1387 * particular VNI.
1388 */
d62a17ae 1389static int install_uninstall_routes_for_vni(struct bgp *bgp,
1390 struct bgpevpn *vpn,
1391 bgp_evpn_route_type rtype,
1392 int install)
1393{
1394 afi_t afi;
1395 safi_t safi;
1396 struct bgp_node *rd_rn, *rn;
1397 struct bgp_table *table;
1398 struct bgp_info *ri;
1399 int ret;
1400
1401 afi = AFI_L2VPN;
1402 safi = SAFI_EVPN;
1403
1404 /* Walk entire global routing table and evaluate routes which could be
1405 * imported into this VPN. Note that we cannot just look at the routes
1406 * for
1407 * the VNI's RD - remote routes applicable for this VNI could have any
1408 * RD.
1409 */
1410 /* EVPN routes are a 2-level table. */
1411 for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
1412 rd_rn = bgp_route_next(rd_rn)) {
1413 table = (struct bgp_table *)(rd_rn->info);
1414 if (!table)
1415 continue;
1416
1417 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
1418 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
1419
1420 if (evp->prefix.route_type != rtype)
1421 continue;
1422
1423 for (ri = rn->info; ri; ri = ri->next) {
1424 /* Consider "valid" remote routes applicable for
1425 * this VNI. */
1426 if (!(CHECK_FLAG(ri->flags, BGP_INFO_VALID)
1427 && ri->type == ZEBRA_ROUTE_BGP
1428 && ri->sub_type == BGP_ROUTE_NORMAL))
1429 continue;
1430
1431 if (is_route_matching_for_vni(bgp, vpn, ri)) {
1432 if (install)
1433 ret = install_evpn_route_entry(
1434 bgp, vpn, evp, ri);
1435 else
1436 ret = uninstall_evpn_route_entry(
1437 bgp, vpn, evp, ri);
1438
1439 if (ret) {
1440 zlog_err(
1441 "%u: Failed to %s EVPN %s route in VNI %u",
1442 bgp->vrf_id,
1443 install ? "install"
1444 : "uninstall",
1445 rtype == BGP_EVPN_MAC_IP_ROUTE
1446 ? "MACIP"
1447 : "IMET",
1448 vpn->vni);
1449 return ret;
1450 }
1451 }
1452 }
1453 }
1454 }
1455
1456 return 0;
128ea8ab 1457}
1458
1459/*
1460 * Install any existing remote routes applicable for this VNI into its
1461 * routing table. This is invoked when a VNI becomes "live" or its Import
1462 * RT is changed.
1463 */
d62a17ae 1464static int install_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1465{
d62a17ae 1466 int ret;
128ea8ab 1467
d62a17ae 1468 /* Install type-3 routes followed by type-2 routes - the ones applicable
1469 * for this VNI.
1470 */
1471 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
1472 1);
1473 if (ret)
1474 return ret;
128ea8ab 1475
d62a17ae 1476 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
1477 1);
128ea8ab 1478}
1479
90e60aa7 1480/*
1481 * Uninstall any existing remote routes for this VNI. One scenario in which
1482 * this is invoked is upon an import RT change.
1483 */
d62a17ae 1484static int uninstall_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 1485{
d62a17ae 1486 int ret;
90e60aa7 1487
d62a17ae 1488 /* Uninstall type-2 routes followed by type-3 routes - the ones
1489 * applicable
1490 * for this VNI.
1491 */
1492 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
1493 0);
1494 if (ret)
1495 return ret;
90e60aa7 1496
d62a17ae 1497 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
1498 0);
90e60aa7 1499}
1500
128ea8ab 1501/*
1502 * Install or uninstall route in matching VNIs (list).
1503 */
d62a17ae 1504static int install_uninstall_route_in_vnis(struct bgp *bgp, afi_t afi,
1505 safi_t safi, struct prefix_evpn *evp,
1506 struct bgp_info *ri,
1507 struct list *vnis, int install)
128ea8ab 1508{
d62a17ae 1509 struct bgpevpn *vpn;
1510 struct listnode *node, *nnode;
128ea8ab 1511
d62a17ae 1512 for (ALL_LIST_ELEMENTS(vnis, node, nnode, vpn)) {
1513 int ret;
128ea8ab 1514
d62a17ae 1515 if (!is_vni_live(vpn))
1516 continue;
128ea8ab 1517
d62a17ae 1518 if (install)
1519 ret = install_evpn_route_entry(bgp, vpn, evp, ri);
1520 else
1521 ret = uninstall_evpn_route_entry(bgp, vpn, evp, ri);
128ea8ab 1522
d62a17ae 1523 if (ret) {
1524 zlog_err("%u: Failed to %s EVPN %s route in VNI %u",
1525 bgp->vrf_id, install ? "install" : "uninstall",
1526 evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
1527 ? "MACIP"
1528 : "IMET",
1529 vpn->vni);
1530 return ret;
1531 }
1532 }
128ea8ab 1533
d62a17ae 1534 return 0;
128ea8ab 1535}
1536
1537/*
1538 * Install or uninstall route for appropriate VNIs.
1539 */
d62a17ae 1540static int install_uninstall_evpn_route(struct bgp *bgp, afi_t afi, safi_t safi,
1541 struct prefix *p, struct bgp_info *ri,
1542 int import)
1543{
1544 struct prefix_evpn *evp = (struct prefix_evpn *)p;
1545 struct attr *attr = ri->attr;
1546 struct ecommunity *ecom;
1547 int i;
1548
1549 assert(attr);
1550
1551 /* Only type-2 and type-3 routes go into a L2 VNI. */
1552 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
1553 || evp->prefix.route_type == BGP_EVPN_IMET_ROUTE))
1554 return 0;
1555
1556 /* If we don't have Route Target, nothing much to do. */
1557 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
1558 return 0;
1559
1560 ecom = attr->ecommunity;
1561 if (!ecom || !ecom->size)
1562 return -1;
1563
1564 /* For each extended community RT, see which VNIs match and import
1565 * the route into matching VNIs.
1566 */
1567 for (i = 0; i < ecom->size; i++) {
1568 u_char *pnt;
1569 u_char type, sub_type;
1570 struct ecommunity_val *eval;
1571 struct ecommunity_val eval_tmp;
1572 struct irt_node *irt;
1573
1574 /* Only deal with RTs */
1575 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
1576 eval = (struct ecommunity_val *)(ecom->val
1577 + (i * ECOMMUNITY_SIZE));
1578 type = *pnt++;
1579 sub_type = *pnt++;
1580 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
1581 continue;
1582
1583 /* Are we interested in this RT? */
1584 irt = lookup_import_rt(bgp, eval);
1585 if (irt && irt->vnis)
1586 install_uninstall_route_in_vnis(bgp, afi, safi, evp, ri,
1587 irt->vnis, import);
1588
1589 /* Also check for non-exact match. In this, we mask out the AS
1590 * and
1591 * only check on the local-admin sub-field. This is to
1592 * facilitate using
1593 * VNI as the RT for EBGP peering too.
1594 */
1595 irt = NULL;
1596 if (type == ECOMMUNITY_ENCODE_AS
1597 || type == ECOMMUNITY_ENCODE_AS4
1598 || type == ECOMMUNITY_ENCODE_IP) {
1599 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
1600 mask_ecom_global_admin(&eval_tmp, eval);
1601 irt = lookup_import_rt(bgp, &eval_tmp);
1602 }
1603 if (irt && irt->vnis)
1604 install_uninstall_route_in_vnis(bgp, afi, safi, evp, ri,
1605 irt->vnis, import);
1606 }
1607
1608 return 0;
128ea8ab 1609}
1610
90e60aa7 1611/*
1612 * Update and advertise local routes for a VNI. Invoked upon router-id
1613 * change. Note that the processing is done only on the global route table
1614 * using routes that already exist in the per-VNI table.
1615 */
d62a17ae 1616static int update_advertise_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
1617{
1618 struct prefix_evpn p;
1619 struct bgp_node *rn, *global_rn;
1620 struct bgp_info *ri, *global_ri;
1621 struct attr *attr;
1622 afi_t afi = AFI_L2VPN;
1623 safi_t safi = SAFI_EVPN;
1624
1625 /* Locate type-3 route for VNI in the per-VNI table and use its
1626 * attributes to create and advertise the type-3 route for this VNI
1627 * in the global table.
1628 */
1629 build_evpn_type3_prefix(&p, vpn->originator_ip);
1630 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)&p);
1631 if (!rn) /* unexpected */
1632 return 0;
1633 for (ri = rn->info; ri; ri = ri->next)
1634 if (ri->peer == bgp->peer_self && ri->type == ZEBRA_ROUTE_BGP
1635 && ri->sub_type == BGP_ROUTE_STATIC)
1636 break;
1637 if (!ri) /* unexpected */
1638 return 0;
1639 attr = ri->attr;
1640
1641 global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
1642 (struct prefix *)&p, &vpn->prd);
1a98c087
MK
1643 update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr, 1, 0, &ri,
1644 0);
d62a17ae 1645
1646 /* Schedule for processing and unlock node. */
1647 bgp_process(bgp, global_rn, afi, safi);
1648 bgp_unlock_node(global_rn);
1649
1650 /* Now, walk this VNI's route table and use the route and its attribute
1651 * to create and schedule route in global table.
1652 */
1653 for (rn = bgp_table_top(vpn->route_table); rn;
1654 rn = bgp_route_next(rn)) {
1655 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
1656
1657 /* Identify MAC-IP local routes. */
1658 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1659 continue;
1660
1661 for (ri = rn->info; ri; ri = ri->next)
1662 if (ri->peer == bgp->peer_self
1663 && ri->type == ZEBRA_ROUTE_BGP
1664 && ri->sub_type == BGP_ROUTE_STATIC)
1665 break;
1666 if (!ri)
1667 continue;
1668
1669 /* Create route in global routing table using this route entry's
1670 * attribute.
1671 */
1672 attr = ri->attr;
1673 global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
1674 (struct prefix *)evp, &vpn->prd);
1675 assert(global_rn);
1676 update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr, 1,
1a98c087 1677 0, &global_ri, 0);
d62a17ae 1678
1679 /* Schedule for processing and unlock node. */
1680 bgp_process(bgp, global_rn, afi, safi);
1681 bgp_unlock_node(global_rn);
1682 }
1683
1684 return 0;
90e60aa7 1685}
1686
1687/*
1688 * Delete (and withdraw) local routes for a VNI - only from the global
1689 * table. Invoked upon router-id change.
1690 */
d62a17ae 1691static int delete_withdraw_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 1692{
d62a17ae 1693 int ret;
1694 struct prefix_evpn p;
1695 struct bgp_node *global_rn;
1696 struct bgp_info *ri;
1697 afi_t afi = AFI_L2VPN;
1698 safi_t safi = SAFI_EVPN;
90e60aa7 1699
d62a17ae 1700 /* Delete and withdraw locally learnt type-2 routes (MACIP)
1701 * for this VNI - from the global table.
1702 */
1703 ret = delete_global_type2_routes(bgp, vpn);
1704 if (ret)
1705 return ret;
90e60aa7 1706
d62a17ae 1707 /* Remove type-3 route for this VNI from global table. */
1708 build_evpn_type3_prefix(&p, vpn->originator_ip);
1709 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
1710 (struct prefix *)&p, &vpn->prd);
1711 if (global_rn) {
1712 /* Delete route entry in the global EVPN table. */
1713 delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);
90e60aa7 1714
d62a17ae 1715 /* Schedule for processing - withdraws to peers happen from
1716 * this table.
1717 */
1718 if (ri)
1719 bgp_process(bgp, global_rn, afi, safi);
1720 bgp_unlock_node(global_rn);
1721 }
90e60aa7 1722
d62a17ae 1723 return 0;
90e60aa7 1724}
1725
2d48ee25 1726/*
1727 * Handle router-id change. Update and advertise local routes corresponding
1728 * to this VNI from peers. Note that this is invoked after updating the
1729 * router-id. The routes in the per-VNI table are used to create routes in
1730 * the global table and schedule them.
1731 */
d62a17ae 1732static void update_router_id_vni(struct hash_backet *backet, struct bgp *bgp)
2d48ee25 1733{
d62a17ae 1734 struct bgpevpn *vpn;
2d48ee25 1735
d62a17ae 1736 vpn = (struct bgpevpn *)backet->data;
2d48ee25 1737
d62a17ae 1738 if (!vpn) {
1739 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__);
1740 return;
1741 }
2d48ee25 1742
d62a17ae 1743 /* Skip VNIs with configured RD. */
1744 if (is_rd_configured(vpn))
1745 return;
2d48ee25 1746
d62a17ae 1747 bgp_evpn_derive_auto_rd(bgp, vpn);
1748 update_advertise_vni_routes(bgp, vpn);
2d48ee25 1749}
1750
1751/*
1752 * Handle router-id change. Delete and withdraw local routes corresponding
1753 * to this VNI from peers. Note that this is invoked prior to updating
1754 * the router-id and is done only on the global route table, the routes
1755 * are needed in the per-VNI table to re-advertise with new router id.
1756 */
d62a17ae 1757static void withdraw_router_id_vni(struct hash_backet *backet, struct bgp *bgp)
2d48ee25 1758{
d62a17ae 1759 struct bgpevpn *vpn;
2d48ee25 1760
d62a17ae 1761 vpn = (struct bgpevpn *)backet->data;
2d48ee25 1762
d62a17ae 1763 if (!vpn) {
1764 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__);
1765 return;
1766 }
2d48ee25 1767
d62a17ae 1768 /* Skip VNIs with configured RD. */
1769 if (is_rd_configured(vpn))
1770 return;
2d48ee25 1771
d62a17ae 1772 delete_withdraw_vni_routes(bgp, vpn);
2d48ee25 1773}
1774
128ea8ab 1775/*
1776 * Process received EVPN type-2 route (advertise or withdraw).
1777 */
d62a17ae 1778static int process_type2_route(struct peer *peer, afi_t afi, safi_t safi,
1779 struct attr *attr, u_char *pfx, int psize,
1780 u_int32_t addpath_id)
1781{
1782 struct prefix_rd prd;
1783 struct prefix_evpn p;
1784 u_char ipaddr_len;
1785 u_char macaddr_len;
1786 mpls_label_t *label_pnt;
1787 int ret;
1788
1789 /* Type-2 route should be either 33, 37 or 49 bytes or an
1790 * additional 3 bytes if there is a second label (VNI):
1791 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
1792 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
1793 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
1794 */
1795 if (psize != 33 && psize != 37 && psize != 49 && psize != 36
1796 && psize != 40 && psize != 52) {
1797 zlog_err("%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
1798 peer->bgp->vrf_id, peer->host, psize);
1799 return -1;
1800 }
1801
1802 /* Make prefix_rd */
1803 prd.family = AF_UNSPEC;
1804 prd.prefixlen = 64;
1805 memcpy(&prd.val, pfx, 8);
1806 pfx += 8;
1807
1808 /* Make EVPN prefix. */
1809 memset(&p, 0, sizeof(struct prefix_evpn));
1810 p.family = AF_ETHERNET;
1811 p.prefixlen = EVPN_TYPE_2_ROUTE_PREFIXLEN;
1812 p.prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
1813
1814 /* Skip over Ethernet Seg Identifier for now. */
1815 pfx += 10;
1816
1817 /* Skip over Ethernet Tag for now. */
1818 pfx += 4;
1819
1820 /* Get the MAC Addr len */
1821 macaddr_len = *pfx++;
1822
1823 /* Get the MAC Addr */
28328ea9
DS
1824 if (macaddr_len == (ETH_ALEN * 8)) {
1825 memcpy(&p.prefix.mac.octet, pfx, ETH_ALEN);
1826 pfx += ETH_ALEN;
d62a17ae 1827 } else {
1828 zlog_err(
1829 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
1830 peer->bgp->vrf_id, peer->host, macaddr_len);
1831 return -1;
1832 }
1833
1834
1835 /* Get the IP. */
1836 ipaddr_len = *pfx++;
1837 if (ipaddr_len != 0 && ipaddr_len != IPV4_MAX_BITLEN
1838 && ipaddr_len != IPV6_MAX_BITLEN) {
1839 zlog_err(
1840 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
1841 peer->bgp->vrf_id, peer->host, ipaddr_len);
1842 return -1;
1843 }
1844
1845 if (ipaddr_len) {
1846 ipaddr_len /= 8; /* Convert to bytes. */
1847 p.prefix.ip.ipa_type = (ipaddr_len == IPV4_MAX_BYTELEN)
1848 ? IPADDR_V4
1849 : IPADDR_V6;
1850 memcpy(&p.prefix.ip.ip.addr, pfx, ipaddr_len);
1851 }
1852 pfx += ipaddr_len;
1853
1854 /* Get the VNI (in MPLS label field). */
1855 /* Note: We ignore the second VNI, if any. */
1856 label_pnt = (mpls_label_t *)pfx;
1857
1858 /* Process the route. */
1859 if (attr)
1860 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
1861 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
1862 &prd, label_pnt, 0, NULL);
1863 else
1864 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
1865 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
1866 &prd, label_pnt, NULL);
1867 return ret;
128ea8ab 1868}
1869
1870/*
1871 * Process received EVPN type-3 route (advertise or withdraw).
1872 */
d62a17ae 1873static int process_type3_route(struct peer *peer, afi_t afi, safi_t safi,
1874 struct attr *attr, u_char *pfx, int psize,
1875 u_int32_t addpath_id)
1876{
1877 struct prefix_rd prd;
1878 struct prefix_evpn p;
1879 u_char ipaddr_len;
1880 int ret;
1881
1882 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
1883 * IP len (1) and IP (4 or 16).
1884 */
1885 if (psize != 17 && psize != 29) {
1886 zlog_err("%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
1887 peer->bgp->vrf_id, peer->host, psize);
1888 return -1;
1889 }
1890
1891 /* Make prefix_rd */
1892 prd.family = AF_UNSPEC;
1893 prd.prefixlen = 64;
1894 memcpy(&prd.val, pfx, 8);
1895 pfx += 8;
1896
1897 /* Make EVPN prefix. */
1898 memset(&p, 0, sizeof(struct prefix_evpn));
1899 p.family = AF_ETHERNET;
1900 p.prefixlen = EVPN_TYPE_3_ROUTE_PREFIXLEN;
1901 p.prefix.route_type = BGP_EVPN_IMET_ROUTE;
1902
1903 /* Skip over Ethernet Tag for now. */
1904 pfx += 4;
1905
1906 /* Get the IP. */
1907 ipaddr_len = *pfx++;
1908 if (ipaddr_len == IPV4_MAX_BITLEN) {
1909 p.prefix.ip.ipa_type = IPADDR_V4;
1910 memcpy(&p.prefix.ip.ip.addr, pfx, IPV4_MAX_BYTELEN);
1911 } else {
1912 zlog_err(
1913 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
1914 peer->bgp->vrf_id, peer->host, ipaddr_len);
1915 return -1;
1916 }
1917
1918 /* Process the route. */
1919 if (attr)
1920 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
1921 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
1922 &prd, NULL, 0, NULL);
1923 else
1924 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
1925 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
1926 &prd, NULL, NULL);
1927 return ret;
128ea8ab 1928}
1929
1930/*
1931 * Process received EVPN type-5 route (advertise or withdraw).
1932 */
d62a17ae 1933static int process_type5_route(struct peer *peer, afi_t afi, safi_t safi,
1934 struct attr *attr, u_char *pfx, int psize,
1935 u_int32_t addpath_id, int withdraw)
1936{
1937 struct prefix_rd prd;
1938 struct prefix_evpn p;
1939 struct bgp_route_evpn evpn;
1940 u_char ippfx_len;
1941 u_int32_t eth_tag;
1942 mpls_label_t *label_pnt;
1943 int ret;
1944
1945 /* Type-5 route should be 34 or 58 bytes:
1946 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
1947 * GW (4 or 16) and VNI (3).
1948 * Note that the IP and GW should both be IPv4 or both IPv6.
1949 */
1950 if (psize != 34 && psize != 58) {
1951 zlog_err("%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
1952 peer->bgp->vrf_id, peer->host, psize);
1953 return -1;
1954 }
1955
1956 /* Make prefix_rd */
1957 prd.family = AF_UNSPEC;
1958 prd.prefixlen = 64;
1959 memcpy(&prd.val, pfx, 8);
1960 pfx += 8;
1961
1962 /* Make EVPN prefix. */
1963 memset(&p, 0, sizeof(struct prefix_evpn));
1964 p.family = AF_ETHERNET;
1965 p.prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
1966
1967 /* Additional information outside of prefix - ESI and GW IP */
1968 memset(&evpn, 0, sizeof(evpn));
1969
1970 /* Fetch ESI */
1971 memcpy(&evpn.eth_s_id.val, pfx, 10);
1972 pfx += 10;
1973
1974 /* Fetch Ethernet Tag. */
1975 memcpy(&eth_tag, pfx, 4);
1976 p.prefix.eth_tag = ntohl(eth_tag);
1977 pfx += 4;
1978
1979 /* Fetch IP prefix length. */
1980 ippfx_len = *pfx++;
1981 if (ippfx_len > IPV6_MAX_BITLEN) {
1982 zlog_err(
1983 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
1984 peer->bgp->vrf_id, peer->host, ippfx_len);
1985 return -1;
1986 }
1987 p.prefix.ip_prefix_length = ippfx_len;
1988
1989 /* Determine IPv4 or IPv6 prefix */
1990 /* Since the address and GW are from the same family, this just becomes
1991 * a simple check on the total size.
1992 */
1993 if (psize == 34) {
1994 SET_IPADDR_V4(&p.prefix.ip);
1995 memcpy(&p.prefix.ip.ipaddr_v4, pfx, 4);
1996 pfx += 4;
1997 memcpy(&evpn.gw_ip.ipv4, pfx, 4);
1998 pfx += 4;
1999 p.prefixlen = PREFIX_LEN_ROUTE_TYPE_5_IPV4;
2000 } else {
2001 SET_IPADDR_V6(&p.prefix.ip);
2002 memcpy(&p.prefix.ip.ipaddr_v6, pfx, 16);
2003 pfx += 16;
2004 memcpy(&evpn.gw_ip.ipv6, pfx, 16);
2005 pfx += 16;
2006 p.prefixlen = PREFIX_LEN_ROUTE_TYPE_5_IPV6;
2007 }
2008
2009 label_pnt = (mpls_label_t *)pfx;
2010
2011 /* Process the route. */
2012 if (!withdraw)
2013 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
2014 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
2015 &prd, label_pnt, 0, &evpn);
2016 else
2017 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
2018 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
2019 &prd, label_pnt, &evpn);
2020
2021 return ret;
2022}
2023
2024static void evpn_mpattr_encode_type5(struct stream *s, struct prefix *p,
2025 struct prefix_rd *prd, mpls_label_t *label,
2026 struct attr *attr)
2027{
2028 int len;
2029 char temp[16];
2030 struct evpn_addr *p_evpn_p;
2031
2032 memset(&temp, 0, 16);
2033 if (p->family != AF_ETHERNET)
2034 return;
2035 p_evpn_p = &(p->u.prefix_evpn);
2036
2037 if (IS_IPADDR_V4(&p_evpn_p->ip))
2038 len = 8; /* ipv4 */
2039 else
2040 len = 32; /* ipv6 */
2041 stream_putc(s, BGP_EVPN_IP_PREFIX_ROUTE);
2042 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
2043 stream_putc(s, 8 + 10 + 4 + 1 + len + 3);
2044 stream_put(s, prd->val, 8);
2045 if (attr && attr)
2046 stream_put(s, &(attr->evpn_overlay.eth_s_id), 10);
2047 else
2048 stream_put(s, &temp, 10);
2049 stream_putl(s, p_evpn_p->eth_tag);
2050 stream_putc(s, p_evpn_p->ip_prefix_length);
2051 if (IS_IPADDR_V4(&p_evpn_p->ip))
2052 stream_put_ipv4(s, p_evpn_p->ip.ipaddr_v4.s_addr);
2053 else
2054 stream_put(s, &p_evpn_p->ip.ipaddr_v6, 16);
2055 if (attr && attr) {
2056 if (IS_IPADDR_V4(&p_evpn_p->ip))
2057 stream_put_ipv4(s,
2058 attr->evpn_overlay.gw_ip.ipv4.s_addr);
2059 else
2060 stream_put(s, &(attr->evpn_overlay.gw_ip.ipv6), 16);
2061 } else {
2062 if (IS_IPADDR_V4(&p_evpn_p->ip))
2063 stream_put_ipv4(s, 0);
2064 else
2065 stream_put(s, &temp, 16);
2066 }
2067
2068 if (label)
2069 stream_put(s, label, 3);
2070 else
2071 stream_put3(s, 0);
128ea8ab 2072}
2073
2074/*
2075 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
2076 */
d62a17ae 2077static void cleanup_vni_on_disable(struct hash_backet *backet, struct bgp *bgp)
128ea8ab 2078{
d62a17ae 2079 struct bgpevpn *vpn = (struct bgpevpn *)backet->data;
128ea8ab 2080
d62a17ae 2081 /* Remove EVPN routes and schedule for processing. */
2082 delete_routes_for_vni(bgp, vpn);
128ea8ab 2083
d62a17ae 2084 /* Clear "live" flag and see if hash needs to be freed. */
2085 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
2086 if (!is_vni_configured(vpn))
2087 bgp_evpn_free(bgp, vpn);
128ea8ab 2088}
2089
2090/*
2091 * Free a VNI entry; iterator function called during cleanup.
2092 */
d62a17ae 2093static void free_vni_entry(struct hash_backet *backet, struct bgp *bgp)
128ea8ab 2094{
d62a17ae 2095 struct bgpevpn *vpn;
128ea8ab 2096
d62a17ae 2097 vpn = (struct bgpevpn *)backet->data;
2098 delete_all_vni_routes(bgp, vpn);
2099 bgp_evpn_free(bgp, vpn);
128ea8ab 2100}
2101
2102
2103/*
2104 * Public functions.
2105 */
2106
2d48ee25 2107/*
2108 * Handle change to BGP router id. This is invoked twice by the change
2109 * handler, first before the router id has been changed and then after
2110 * the router id has been changed. The first invocation will result in
2111 * local routes for all VNIs being deleted and withdrawn and the next
2112 * will result in the routes being re-advertised.
2113 */
d62a17ae 2114void bgp_evpn_handle_router_id_update(struct bgp *bgp, int withdraw)
2d48ee25 2115{
d62a17ae 2116 if (withdraw)
2117 hash_iterate(bgp->vnihash,
2118 (void (*)(struct hash_backet *,
2119 void *))withdraw_router_id_vni,
2120 bgp);
2121 else
2122 hash_iterate(bgp->vnihash,
2123 (void (*)(struct hash_backet *,
2124 void *))update_router_id_vni,
2125 bgp);
2d48ee25 2126}
2127
90e60aa7 2128/*
2129 * Handle change to export RT - update and advertise local routes.
2130 */
d62a17ae 2131int bgp_evpn_handle_export_rt_change(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 2132{
d62a17ae 2133 return update_routes_for_vni(bgp, vpn);
90e60aa7 2134}
2135
2136/*
2137 * Handle change to RD. This is invoked twice by the change handler,
2138 * first before the RD has been changed and then after the RD has
2139 * been changed. The first invocation will result in local routes
2140 * of this VNI being deleted and withdrawn and the next will result
2141 * in the routes being re-advertised.
2142 */
d62a17ae 2143void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
2144 int withdraw)
90e60aa7 2145{
d62a17ae 2146 if (withdraw)
2147 delete_withdraw_vni_routes(bgp, vpn);
2148 else
2149 update_advertise_vni_routes(bgp, vpn);
90e60aa7 2150}
2151
2152/*
2153 * Install routes for this VNI. Invoked upon change to Import RT.
2154 */
d62a17ae 2155int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 2156{
d62a17ae 2157 return install_routes_for_vni(bgp, vpn);
90e60aa7 2158}
2159
2160/*
2161 * Uninstall all routes installed for this VNI. Invoked upon change
2162 * to Import RT.
2163 */
d62a17ae 2164int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 2165{
d62a17ae 2166 return uninstall_routes_for_vni(bgp, vpn);
90e60aa7 2167}
2168
b16031a2 2169/*
2170 * Function to display "tag" in route as a VNI.
2171 */
d62a17ae 2172char *bgp_evpn_label2str(mpls_label_t *label, char *buf, int len)
b16031a2 2173{
d62a17ae 2174 vni_t vni;
b16031a2 2175
d62a17ae 2176 vni = label2vni(label);
2177 snprintf(buf, len, "%u", vni);
2178 return buf;
b16031a2 2179}
2180
520d5d76 2181/*
2182 * Function to convert evpn route to string.
2183 * NOTE: We don't use prefix2str as the output here is a bit different.
2184 */
d62a17ae 2185char *bgp_evpn_route2str(struct prefix_evpn *p, char *buf, int len)
2186{
2187 char buf1[ETHER_ADDR_STRLEN];
2188 char buf2[PREFIX2STR_BUFFER];
2189
2190 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
2191 snprintf(buf, len, "[%d]:[0]:[%d]:[%s]", p->prefix.route_type,
2192 IS_EVPN_PREFIX_IPADDR_V4(p) ? IPV4_MAX_BITLEN
2193 : IPV6_MAX_BITLEN,
2194 inet_ntoa(p->prefix.ip.ipaddr_v4));
2195 } else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
2196 if (IS_EVPN_PREFIX_IPADDR_NONE(p))
2197 snprintf(buf, len, "[%d]:[0]:[0]:[%d]:[%s]",
28328ea9 2198 p->prefix.route_type, 8 * ETH_ALEN,
d62a17ae 2199 prefix_mac2str(&p->prefix.mac, buf1,
2200 sizeof(buf1)));
2201 else {
2202 u_char family;
2203
2204 family = IS_EVPN_PREFIX_IPADDR_V4(p) ? AF_INET
2205 : AF_INET6;
2206 snprintf(buf, len, "[%d]:[0]:[0]:[%d]:[%s]:[%d]:[%s]",
28328ea9 2207 p->prefix.route_type, 8 * ETH_ALEN,
d62a17ae 2208 prefix_mac2str(&p->prefix.mac, buf1,
2209 sizeof(buf1)),
2210 family == AF_INET ? IPV4_MAX_BITLEN
2211 : IPV6_MAX_BITLEN,
2212 inet_ntop(family, &p->prefix.ip.ip.addr, buf2,
2213 PREFIX2STR_BUFFER));
2214 }
2215 } else {
2216 /* Currently, this is to cater to other AF_ETHERNET code. */
2217 }
2218
2219 return (buf);
520d5d76 2220}
2221
128ea8ab 2222/*
2223 * Encode EVPN prefix in Update (MP_REACH)
2224 */
d62a17ae 2225void bgp_evpn_encode_prefix(struct stream *s, struct prefix *p,
2226 struct prefix_rd *prd, mpls_label_t *label,
2227 struct attr *attr, int addpath_encode,
2228 u_int32_t addpath_tx_id)
2229{
2230 struct prefix_evpn *evp = (struct prefix_evpn *)p;
2231 int ipa_len = 0;
2232
2233 if (addpath_encode)
2234 stream_putl(s, addpath_tx_id);
2235
2236 /* Route type */
2237 stream_putc(s, evp->prefix.route_type);
2238
2239 switch (evp->prefix.route_type) {
2240 case BGP_EVPN_MAC_IP_ROUTE:
2241 if (IS_EVPN_PREFIX_IPADDR_V4(evp))
2242 ipa_len = IPV4_MAX_BYTELEN;
2243 else if (IS_EVPN_PREFIX_IPADDR_V6(evp))
2244 ipa_len = IPV6_MAX_BYTELEN;
2245 stream_putc(s, 33 + ipa_len); // 1 VNI
2246 stream_put(s, prd->val, 8); /* RD */
2247 stream_put(s, 0, 10); /* ESI */
2248 stream_putl(s, 0); /* Ethernet Tag ID */
28328ea9 2249 stream_putc(s, 8 * ETH_ALEN); /* Mac Addr Len - bits */
d62a17ae 2250 stream_put(s, evp->prefix.mac.octet, 6); /* Mac Addr */
2251 stream_putc(s, 8 * ipa_len); /* IP address Length */
2252 if (ipa_len)
2253 stream_put(s, &evp->prefix.ip.ip.addr,
2254 ipa_len); /* IP */
2255 stream_put(s, label,
2256 BGP_LABEL_BYTES); /* VNI is contained in 'tag' */
2257 break;
2258
2259 case BGP_EVPN_IMET_ROUTE:
2260 stream_putc(s, 17); // TODO: length - assumes IPv4 address
2261 stream_put(s, prd->val, 8); /* RD */
2262 stream_putl(s, 0); /* Ethernet Tag ID */
2263 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
2264 /* Originating Router's IP Addr */
2265 stream_put_in_addr(s, &evp->prefix.ip.ipaddr_v4);
2266 break;
2267
2268 case BGP_EVPN_IP_PREFIX_ROUTE:
2269 /* TODO: AddPath support. */
2270 evpn_mpattr_encode_type5(s, p, prd, label, attr);
2271 break;
2272
2273 default:
2274 break;
2275 }
2276}
2277
2278int bgp_nlri_parse_evpn(struct peer *peer, struct attr *attr,
2279 struct bgp_nlri *packet, int withdraw)
2280{
2281 u_char *pnt;
2282 u_char *lim;
2283 afi_t afi;
2284 safi_t safi;
2285 u_int32_t addpath_id;
2286 int addpath_encoded;
2287 int psize = 0;
2288 u_char rtype;
2289 u_char rlen;
2290 struct prefix p;
2291
2292 /* Check peer status. */
2293 if (peer->status != Established) {
2294 zlog_err("%u:%s - EVPN update received in state %d",
2295 peer->bgp->vrf_id, peer->host, peer->status);
2296 return -1;
2297 }
2298
2299 /* Start processing the NLRI - there may be multiple in the MP_REACH */
2300 pnt = packet->nlri;
2301 lim = pnt + packet->length;
2302 afi = packet->afi;
2303 safi = packet->safi;
2304 addpath_id = 0;
2305
2306 addpath_encoded =
2307 (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV)
2308 && CHECK_FLAG(peer->af_cap[afi][safi],
2309 PEER_CAP_ADDPATH_AF_TX_RCV));
2310
2311 for (; pnt < lim; pnt += psize) {
2312 /* Clear prefix structure. */
2313 memset(&p, 0, sizeof(struct prefix));
2314
2315 /* Deal with path-id if AddPath is supported. */
2316 if (addpath_encoded) {
2317 /* When packet overflow occurs return immediately. */
2318 if (pnt + BGP_ADDPATH_ID_LEN > lim)
2319 return -1;
2320
2321 addpath_id = ntohl(*((uint32_t *)pnt));
2322 pnt += BGP_ADDPATH_ID_LEN;
2323 }
2324
2325 /* All EVPN NLRI types start with type and length. */
2326 if (pnt + 2 > lim)
2327 return -1;
2328
2329 rtype = *pnt++;
2330 psize = rlen = *pnt++;
2331
2332 /* When packet overflow occur return immediately. */
2333 if (pnt + psize > lim)
2334 return -1;
2335
2336 switch (rtype) {
2337 case BGP_EVPN_MAC_IP_ROUTE:
2338 if (process_type2_route(peer, afi, safi,
2339 withdraw ? NULL : attr, pnt,
2340 psize, addpath_id)) {
2341 zlog_err(
2342 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
2343 peer->bgp->vrf_id, peer->host, psize);
2344 return -1;
2345 }
2346 break;
2347
2348 case BGP_EVPN_IMET_ROUTE:
2349 if (process_type3_route(peer, afi, safi,
2350 withdraw ? NULL : attr, pnt,
2351 psize, addpath_id)) {
2352 zlog_err(
2353 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
2354 peer->bgp->vrf_id, peer->host, psize);
2355 return -1;
2356 }
2357 break;
2358
2359 case BGP_EVPN_IP_PREFIX_ROUTE:
2360 if (process_type5_route(peer, afi, safi, attr, pnt,
2361 psize, addpath_id, withdraw)) {
2362 zlog_err(
2363 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
2364 peer->bgp->vrf_id, peer->host, psize);
2365 return -1;
2366 }
2367 break;
2368
2369 default:
2370 break;
2371 }
2372 }
2373
2374 /* Packet length consistency check. */
2375 if (pnt != lim)
2376 return -1;
2377
2378 return 0;
128ea8ab 2379}
2380
2381
2382/*
2383 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
2384 * The mapping will be used during route processing.
2385 */
d62a17ae 2386void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2387{
d62a17ae 2388 int i;
2389 struct ecommunity_val *eval;
2390 struct listnode *node, *nnode;
2391 struct ecommunity *ecom;
128ea8ab 2392
d62a17ae 2393 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
2394 for (i = 0; i < ecom->size; i++) {
2395 eval = (struct ecommunity_val *)(ecom->val
2396 + (i
2397 * ECOMMUNITY_SIZE));
2398 map_vni_to_rt(bgp, vpn, eval);
2399 }
2400 }
128ea8ab 2401}
2402
2403/*
2404 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
2405 */
d62a17ae 2406void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2407{
d62a17ae 2408 int i;
2409 struct ecommunity_val *eval;
2410 struct listnode *node, *nnode;
2411 struct ecommunity *ecom;
128ea8ab 2412
d62a17ae 2413 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
2414 for (i = 0; i < ecom->size; i++) {
2415 struct irt_node *irt;
2416 struct ecommunity_val eval_tmp;
128ea8ab 2417
d62a17ae 2418 eval = (struct ecommunity_val *)(ecom->val
2419 + (i
2420 * ECOMMUNITY_SIZE));
2421 /* If using "automatic" RT, we only care about the
2422 * local-admin sub-field.
2423 * This is to facilitate using VNI as the RT for EBGP
2424 * peering too.
2425 */
2426 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
2427 if (!is_import_rt_configured(vpn))
2428 mask_ecom_global_admin(&eval_tmp, eval);
128ea8ab 2429
d62a17ae 2430 irt = lookup_import_rt(bgp, &eval_tmp);
2431 if (irt)
2432 unmap_vni_from_rt(bgp, vpn, irt);
2433 }
2434 }
128ea8ab 2435}
2436
2437/*
2438 * Derive Import RT automatically for VNI and map VNI to RT.
2439 * The mapping will be used during route processing.
2440 */
d62a17ae 2441void bgp_evpn_derive_auto_rt_import(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2442{
d62a17ae 2443 form_auto_rt(bgp, vpn, vpn->import_rtl);
2444 UNSET_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD);
128ea8ab 2445
d62a17ae 2446 /* Map RT to VNI */
2447 bgp_evpn_map_vni_to_its_rts(bgp, vpn);
128ea8ab 2448}
2449
2450/*
2451 * Derive Export RT automatically for VNI.
2452 */
d62a17ae 2453void bgp_evpn_derive_auto_rt_export(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2454{
d62a17ae 2455 form_auto_rt(bgp, vpn, vpn->export_rtl);
2456 UNSET_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD);
128ea8ab 2457}
2458
2459/*
2460 * Derive RD automatically for VNI using passed information - it
2461 * is of the form RouterId:unique-id-for-vni.
2462 */
d62a17ae 2463void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2464{
d62a17ae 2465 char buf[100];
128ea8ab 2466
d62a17ae 2467 vpn->prd.family = AF_UNSPEC;
2468 vpn->prd.prefixlen = 64;
2469 sprintf(buf, "%s:%hu", inet_ntoa(bgp->router_id), vpn->rd_id);
2470 str2prefix_rd(buf, &vpn->prd);
2471 UNSET_FLAG(vpn->flags, VNI_FLAG_RD_CFGD);
128ea8ab 2472}
2473
2474/*
2475 * Lookup VNI.
2476 */
d62a17ae 2477struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni)
128ea8ab 2478{
d62a17ae 2479 struct bgpevpn *vpn;
2480 struct bgpevpn tmp;
128ea8ab 2481
d62a17ae 2482 memset(&tmp, 0, sizeof(struct bgpevpn));
2483 tmp.vni = vni;
2484 vpn = hash_lookup(bgp->vnihash, &tmp);
2485 return vpn;
128ea8ab 2486}
2487
2488/*
2489 * Create a new vpn - invoked upon configuration or zebra notification.
2490 */
d62a17ae 2491struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
2492 struct in_addr originator_ip)
128ea8ab 2493{
d62a17ae 2494 struct bgpevpn *vpn;
128ea8ab 2495
d62a17ae 2496 if (!bgp)
2497 return NULL;
128ea8ab 2498
d62a17ae 2499 vpn = XCALLOC(MTYPE_BGP_EVPN, sizeof(struct bgpevpn));
2500 if (!vpn)
2501 return NULL;
128ea8ab 2502
d62a17ae 2503 /* Set values - RD and RT set to defaults. */
2504 vpn->vni = vni;
2505 vpn->originator_ip = originator_ip;
128ea8ab 2506
d62a17ae 2507 /* Initialize route-target import and export lists */
2508 vpn->import_rtl = list_new();
2509 vpn->import_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
2510 vpn->export_rtl = list_new();
2511 vpn->export_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
2512 bf_assign_index(bgp->rd_idspace, vpn->rd_id);
2513 derive_rd_rt_for_vni(bgp, vpn);
128ea8ab 2514
d62a17ae 2515 /* Initialize EVPN route table. */
2516 vpn->route_table = bgp_table_init(AFI_L2VPN, SAFI_EVPN);
128ea8ab 2517
d62a17ae 2518 /* Add to hash */
2519 if (!hash_get(bgp->vnihash, vpn, hash_alloc_intern)) {
2520 XFREE(MTYPE_BGP_EVPN, vpn);
2521 return NULL;
2522 }
2523 QOBJ_REG(vpn, bgpevpn);
2524 return vpn;
128ea8ab 2525}
2526
2527/*
2528 * Free a given VPN - called in multiple scenarios such as zebra
2529 * notification, configuration being deleted, advertise-all-vni disabled etc.
2530 * This just frees appropriate memory, caller should have taken other
2531 * needed actions.
2532 */
d62a17ae 2533void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2534{
d62a17ae 2535 bgp_table_unlock(vpn->route_table);
2536 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
2537 list_delete(vpn->import_rtl);
2538 list_delete(vpn->export_rtl);
2539 vpn->import_rtl = NULL;
2540 vpn->export_rtl = NULL;
2541 bf_release_index(bgp->rd_idspace, vpn->rd_id);
2542 hash_release(bgp->vnihash, vpn);
2543 QOBJ_UNREG(vpn);
2544 XFREE(MTYPE_BGP_EVPN, vpn);
128ea8ab 2545}
2546
2547/*
2548 * Import route into matching VNI(s).
2549 */
d62a17ae 2550int bgp_evpn_import_route(struct bgp *bgp, afi_t afi, safi_t safi,
2551 struct prefix *p, struct bgp_info *ri)
128ea8ab 2552{
d62a17ae 2553 return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 1);
128ea8ab 2554}
2555
2556/*
2557 * Unimport route from matching VNI(s).
2558 */
d62a17ae 2559int bgp_evpn_unimport_route(struct bgp *bgp, afi_t afi, safi_t safi,
2560 struct prefix *p, struct bgp_info *ri)
128ea8ab 2561{
d62a17ae 2562 return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 0);
128ea8ab 2563}
2564
2565/*
2566 * Handle del of a local MACIP.
2567 */
d62a17ae 2568int bgp_evpn_local_macip_del(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
2569 struct ipaddr *ip)
128ea8ab 2570{
d62a17ae 2571 struct bgpevpn *vpn;
2572 struct prefix_evpn p;
128ea8ab 2573
d62a17ae 2574 if (!bgp->vnihash) {
2575 zlog_err("%u: VNI hash not created", bgp->vrf_id);
2576 return -1;
2577 }
128ea8ab 2578
d62a17ae 2579 /* Lookup VNI hash - should exist. */
2580 vpn = bgp_evpn_lookup_vni(bgp, vni);
2581 if (!vpn || !is_vni_live(vpn)) {
2582 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL",
2583 bgp->vrf_id, vni, vpn ? "not live" : "not found");
2584 return -1;
2585 }
128ea8ab 2586
d62a17ae 2587 /* Remove EVPN type-2 route and schedule for processing. */
2588 build_evpn_type2_prefix(&p, mac, ip);
2589 delete_evpn_route(bgp, vpn, &p);
128ea8ab 2590
d62a17ae 2591 return 0;
128ea8ab 2592}
2593
2594/*
2595 * Handle add of a local MACIP.
2596 */
d62a17ae 2597int bgp_evpn_local_macip_add(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
1a98c087 2598 struct ipaddr *ip, u_char flags)
128ea8ab 2599{
d62a17ae 2600 struct bgpevpn *vpn;
2601 struct prefix_evpn p;
128ea8ab 2602
d62a17ae 2603 if (!bgp->vnihash) {
2604 zlog_err("%u: VNI hash not created", bgp->vrf_id);
2605 return -1;
2606 }
128ea8ab 2607
d62a17ae 2608 /* Lookup VNI hash - should exist. */
2609 vpn = bgp_evpn_lookup_vni(bgp, vni);
2610 if (!vpn || !is_vni_live(vpn)) {
2611 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD",
2612 bgp->vrf_id, vni, vpn ? "not live" : "not found");
2613 return -1;
2614 }
128ea8ab 2615
d62a17ae 2616 /* Create EVPN type-2 route and schedule for processing. */
2617 build_evpn_type2_prefix(&p, mac, ip);
1a98c087 2618 if (update_evpn_route(bgp, vpn, &p, flags)) {
d62a17ae 2619 char buf[ETHER_ADDR_STRLEN];
2620 char buf2[INET6_ADDRSTRLEN];
128ea8ab 2621
d62a17ae 2622 zlog_err(
b34fd35d 2623 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s",
1a98c087 2624 bgp->vrf_id, vpn->vni,
b34fd35d 2625 CHECK_FLAG(flags, ZEBRA_MAC_TYPE_STICKY) ? "sticky gateway"
1a98c087 2626 : "",
d62a17ae 2627 prefix_mac2str(mac, buf, sizeof(buf)),
2628 ipaddr2str(ip, buf2, sizeof(buf2)));
2629 return -1;
2630 }
128ea8ab 2631
d62a17ae 2632 return 0;
128ea8ab 2633}
2634
2635/*
2636 * Handle del of a local VNI.
2637 */
d62a17ae 2638int bgp_evpn_local_vni_del(struct bgp *bgp, vni_t vni)
128ea8ab 2639{
d62a17ae 2640 struct bgpevpn *vpn;
128ea8ab 2641
d62a17ae 2642 if (!bgp->vnihash) {
2643 zlog_err("%u: VNI hash not created", bgp->vrf_id);
2644 return -1;
2645 }
128ea8ab 2646
d62a17ae 2647 /* Locate VNI hash */
2648 vpn = bgp_evpn_lookup_vni(bgp, vni);
2649 if (!vpn) {
2650 zlog_warn("%u: VNI hash entry for VNI %u not found at DEL",
2651 bgp->vrf_id, vni);
2652 return 0;
2653 }
128ea8ab 2654
d62a17ae 2655 /* Remove all local EVPN routes and schedule for processing (to
2656 * withdraw from peers).
2657 */
2658 delete_routes_for_vni(bgp, vpn);
128ea8ab 2659
d62a17ae 2660 /* Clear "live" flag and see if hash needs to be freed. */
2661 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
2662 if (!is_vni_configured(vpn))
2663 bgp_evpn_free(bgp, vpn);
128ea8ab 2664
d62a17ae 2665 return 0;
128ea8ab 2666}
2667
2668/*
2669 * Handle add (or update) of a local VNI. The only VNI change we care
2670 * about is change to local-tunnel-ip.
2671 */
d62a17ae 2672int bgp_evpn_local_vni_add(struct bgp *bgp, vni_t vni,
2673 struct in_addr originator_ip)
2674{
2675 struct bgpevpn *vpn;
2676 struct prefix_evpn p;
2677
2678 if (!bgp->vnihash) {
2679 zlog_err("%u: VNI hash not created", bgp->vrf_id);
2680 return -1;
2681 }
2682
2683 /* Lookup VNI. If present and no change, exit. */
2684 vpn = bgp_evpn_lookup_vni(bgp, vni);
ddd16ed5 2685 if (vpn) {
2f1ac16a
MK
2686 if (is_vni_live(vpn)
2687 && IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip))
d62a17ae 2688 /* Probably some other param has changed that we don't
2689 * care about. */
2690 return 0;
2691
2692 /* Local tunnel endpoint IP address has changed */
ddd16ed5 2693 handle_tunnel_ip_change(bgp, vpn, originator_ip);
d62a17ae 2694 }
2695
2696 /* Create or update as appropriate. */
2697 if (!vpn) {
2698 vpn = bgp_evpn_new(bgp, vni, originator_ip);
2699 if (!vpn) {
2700 zlog_err(
2701 "%u: Failed to allocate VNI entry for VNI %u - at Add",
2702 bgp->vrf_id, vni);
2703 return -1;
2704 }
2705 }
2706
ddd16ed5
MK
2707 /* if the VNI is live already, there is nothibng more to do */
2708 if (is_vni_live(vpn))
2709 return 0;
2710
d62a17ae 2711 /* Mark as "live" */
2712 SET_FLAG(vpn->flags, VNI_FLAG_LIVE);
2713
2714 /* Create EVPN type-3 route and schedule for processing. */
2715 build_evpn_type3_prefix(&p, vpn->originator_ip);
2716 if (update_evpn_route(bgp, vpn, &p, 0)) {
2717 zlog_err("%u: Type3 route creation failure for VNI %u",
2718 bgp->vrf_id, vni);
2719 return -1;
2720 }
2721
2722 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
2723 * VNI,
2724 * install them.
2725 */
2726 install_routes_for_vni(bgp, vpn);
2727
2728 return 0;
b18825eb 2729}
14c1a7bf 2730
7724c0a1 2731/*
2732 * Cleanup EVPN information on disable - Need to delete and withdraw
2733 * EVPN routes from peers.
2734 */
d62a17ae 2735void bgp_evpn_cleanup_on_disable(struct bgp *bgp)
7724c0a1 2736{
9d303b37
DL
2737 hash_iterate(bgp->vnihash, (void (*)(struct hash_backet *,
2738 void *))cleanup_vni_on_disable,
2739 bgp);
7724c0a1 2740}
2741
14c1a7bf 2742/*
2743 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
2744 * BGP instance (default) is being freed.
2745 */
d62a17ae 2746void bgp_evpn_cleanup(struct bgp *bgp)
14c1a7bf 2747{
d62a17ae 2748 if (bgp->vnihash)
9d303b37
DL
2749 hash_iterate(bgp->vnihash, (void (*)(struct hash_backet *,
2750 void *))free_vni_entry,
2751 bgp);
d62a17ae 2752 if (bgp->import_rt_hash)
2753 hash_free(bgp->import_rt_hash);
2754 bgp->import_rt_hash = NULL;
2755 if (bgp->vnihash)
2756 hash_free(bgp->vnihash);
2757 bgp->vnihash = NULL;
2758 bf_free(bgp->rd_idspace);
14c1a7bf 2759}
2760
2761/*
2762 * Initialization for EVPN
2763 * Create
2764 * VNI hash table
2765 * hash for RT to VNI
2766 * unique rd id space for auto derivation of RD for VNIs
2767 */
d62a17ae 2768void bgp_evpn_init(struct bgp *bgp)
2769{
2770 bgp->vnihash =
2771 hash_create(vni_hash_key_make, vni_hash_cmp, "BGP VNI Hash");
2772 bgp->import_rt_hash =
2773 hash_create(import_rt_hash_key_make, import_rt_hash_cmp,
2774 "BGP Import RT Hash");
2775 bf_init(bgp->rd_idspace, UINT16_MAX);
2776 /*assign 0th index in the bitfield, so that we start with id 1*/
2777 bf_assign_zero_index(bgp->rd_idspace);
14c1a7bf 2778}