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