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