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