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