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