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