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