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