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