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