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