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