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