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