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