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