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