]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_evpn.c
Merge pull request #11997 from sri-mohan1/sri-zebra-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) {
3635 bgp_dest_unlock_node(dest);
3636 return 0;
3637 }
3638 attr = pi->attr;
3639
3640 global_dest = bgp_global_evpn_node_get(bgp->rib[afi][safi],
3641 afi, safi, &p, &vpn->prd);
3642 update_evpn_route_entry(bgp, vpn, afi, safi, global_dest, attr,
3643 1, &pi, 0, mac_mobility_seqnum(attr),
3644 false /* setup_sync */, NULL /* old_is_sync */);
3645
3646 /* Schedule for processing and unlock node. */
3647 bgp_process(bgp, global_dest, afi, safi);
3648 bgp_dest_unlock_node(global_dest);
3649 }
3650
3651 /* Now, walk this VNI's route table and use the route and its attribute
3652 * to create and schedule route in global table.
3653 */
3654 for (dest = bgp_table_top(vpn->route_table); dest;
3655 dest = bgp_route_next(dest)) {
3656 const struct prefix_evpn *evp =
3657 (const struct prefix_evpn *)bgp_dest_get_prefix(dest);
3658
3659 /*
3660 * We have already processed type-3 routes.
3661 * Process only type-1 and type-2 routes here.
3662 */
3663 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE
3664 && evp->prefix.route_type != BGP_EVPN_AD_ROUTE)
3665 continue;
3666
3667 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
3668 if (pi->peer == bgp->peer_self
3669 && pi->type == ZEBRA_ROUTE_BGP
3670 && pi->sub_type == BGP_ROUTE_STATIC)
3671 break;
3672 if (!pi)
3673 continue;
3674
3675 /* Create route in global routing table using this route entry's
3676 * attribute.
3677 */
3678 attr = pi->attr;
3679 global_dest = bgp_global_evpn_node_get(bgp->rib[afi][safi], afi, safi,
3680 evp, &vpn->prd);
3681 assert(global_dest);
3682
3683 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
3684 /* Type-2 route */
3685 update_evpn_route_entry(
3686 bgp, vpn, afi, safi, global_dest, attr, 1,
3687 &global_pi, 0, mac_mobility_seqnum(attr),
3688 false /* setup_sync */, NULL /* old_is_sync */);
3689 } else {
3690 /* Type-1 route */
3691 struct bgp_evpn_es *es;
3692 int route_changed = 0;
3693
3694 es = bgp_evpn_es_find(&evp->prefix.ead_addr.esi);
3695 bgp_evpn_mh_route_update(bgp, es, vpn, afi, safi,
3696 global_dest, attr, &global_pi,
3697 &route_changed);
3698 }
3699
3700 /* Schedule for processing and unlock node. */
3701 bgp_process(bgp, global_dest, afi, safi);
3702 bgp_dest_unlock_node(global_dest);
3703 }
3704
3705 return 0;
3706 }
3707
3708 /*
3709 * Delete (and withdraw) local routes for a VNI - only from the global
3710 * table. Invoked upon router-id change.
3711 */
3712 static int delete_withdraw_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
3713 {
3714 struct prefix_evpn p;
3715 struct bgp_dest *global_dest;
3716 struct bgp_path_info *pi;
3717 afi_t afi = AFI_L2VPN;
3718 safi_t safi = SAFI_EVPN;
3719
3720 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3721 * for this VNI - from the global table.
3722 */
3723 delete_global_type2_routes(bgp, vpn);
3724
3725 /* Remove type-3 route for this VNI from global table. */
3726 build_evpn_type3_prefix(&p, vpn->originator_ip);
3727 global_dest = bgp_global_evpn_node_lookup(bgp->rib[afi][safi], afi, safi,
3728 (const struct prefix_evpn *)&p, &vpn->prd);
3729 if (global_dest) {
3730 /* Delete route entry in the global EVPN table. */
3731 delete_evpn_route_entry(bgp, afi, safi, global_dest, &pi);
3732
3733 /* Schedule for processing - withdraws to peers happen from
3734 * this table.
3735 */
3736 if (pi)
3737 bgp_process(bgp, global_dest, afi, safi);
3738 bgp_dest_unlock_node(global_dest);
3739 }
3740
3741
3742 delete_global_ead_evi_routes(bgp, vpn);
3743 return 0;
3744 }
3745
3746 /*
3747 * Handle router-id change. Update and advertise local routes corresponding
3748 * to this VNI from peers. Note that this is invoked after updating the
3749 * router-id. The routes in the per-VNI table are used to create routes in
3750 * the global table and schedule them.
3751 */
3752 static void update_router_id_vni(struct hash_bucket *bucket, struct bgp *bgp)
3753 {
3754 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
3755
3756 /* Skip VNIs with configured RD. */
3757 if (is_rd_configured(vpn))
3758 return;
3759
3760 bgp_evpn_derive_auto_rd(bgp, vpn);
3761 update_advertise_vni_routes(bgp, vpn);
3762 }
3763
3764 /*
3765 * Handle router-id change. Delete and withdraw local routes corresponding
3766 * to this VNI from peers. Note that this is invoked prior to updating
3767 * the router-id and is done only on the global route table, the routes
3768 * are needed in the per-VNI table to re-advertise with new router id.
3769 */
3770 static void withdraw_router_id_vni(struct hash_bucket *bucket, struct bgp *bgp)
3771 {
3772 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
3773
3774 /* Skip VNIs with configured RD. */
3775 if (is_rd_configured(vpn))
3776 return;
3777
3778 delete_withdraw_vni_routes(bgp, vpn);
3779 }
3780
3781 /*
3782 * Create RT-3 for a VNI and schedule for processing and advertisement.
3783 * This is invoked upon flooding mode changing to head-end replication.
3784 */
3785 static void create_advertise_type3(struct hash_bucket *bucket, void *data)
3786 {
3787 struct bgpevpn *vpn = bucket->data;
3788 struct bgp *bgp = data;
3789 struct prefix_evpn p;
3790
3791 if (!vpn || !is_vni_live(vpn) ||
3792 bgp_evpn_vni_flood_mode_get(bgp, vpn)
3793 != VXLAN_FLOOD_HEAD_END_REPL)
3794 return;
3795
3796 build_evpn_type3_prefix(&p, vpn->originator_ip);
3797 if (update_evpn_route(bgp, vpn, &p, 0, 0, NULL))
3798 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
3799 "Type3 route creation failure for VNI %u", vpn->vni);
3800 }
3801
3802 /*
3803 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3804 * This is invoked upon flooding mode changing to drop BUM packets.
3805 */
3806 static void delete_withdraw_type3(struct hash_bucket *bucket, void *data)
3807 {
3808 struct bgpevpn *vpn = bucket->data;
3809 struct bgp *bgp = data;
3810 struct prefix_evpn p;
3811
3812 if (!vpn || !is_vni_live(vpn))
3813 return;
3814
3815 build_evpn_type3_prefix(&p, vpn->originator_ip);
3816 delete_evpn_route(bgp, vpn, &p);
3817 }
3818
3819 /*
3820 * Process received EVPN type-2 route (advertise or withdraw).
3821 */
3822 static int process_type2_route(struct peer *peer, afi_t afi, safi_t safi,
3823 struct attr *attr, uint8_t *pfx, int psize,
3824 uint32_t addpath_id)
3825 {
3826 struct prefix_rd prd;
3827 struct prefix_evpn p = {};
3828 struct bgp_route_evpn evpn = {};
3829 uint8_t ipaddr_len;
3830 uint8_t macaddr_len;
3831 /* holds the VNI(s) as in packet */
3832 mpls_label_t label[BGP_MAX_LABELS] = {};
3833 uint32_t num_labels = 0;
3834 uint32_t eth_tag;
3835 int ret;
3836
3837 /* Type-2 route should be either 33, 37 or 49 bytes or an
3838 * additional 3 bytes if there is a second label (VNI):
3839 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3840 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3841 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3842 */
3843 if (psize != 33 && psize != 37 && psize != 49 && psize != 36
3844 && psize != 40 && psize != 52) {
3845 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
3846 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3847 peer->bgp->vrf_id, peer->host, psize);
3848 return -1;
3849 }
3850
3851 struct stream *pkt = stream_new(psize);
3852 stream_put(pkt, pfx, psize);
3853
3854 /* Make prefix_rd */
3855 prd.family = AF_UNSPEC;
3856 prd.prefixlen = 64;
3857
3858 STREAM_GET(&prd.val, pkt, 8);
3859
3860 /* Make EVPN prefix. */
3861 p.family = AF_EVPN;
3862 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
3863 p.prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
3864
3865 /* Copy Ethernet Seg Identifier */
3866 if (attr) {
3867 STREAM_GET(&attr->esi, pkt, sizeof(esi_t));
3868
3869 if (bgp_evpn_is_esi_local_and_non_bypass(&attr->esi))
3870 attr->es_flags |= ATTR_ES_IS_LOCAL;
3871 else
3872 attr->es_flags &= ~ATTR_ES_IS_LOCAL;
3873 } else {
3874 STREAM_FORWARD_GETP(pkt, sizeof(esi_t));
3875 }
3876
3877 /* Copy Ethernet Tag */
3878 STREAM_GET(&eth_tag, pkt, 4);
3879 p.prefix.macip_addr.eth_tag = ntohl(eth_tag);
3880
3881 /* Get the MAC Addr len */
3882 STREAM_GETC(pkt, macaddr_len);
3883
3884 /* Get the MAC Addr */
3885 if (macaddr_len == (ETH_ALEN * 8)) {
3886 STREAM_GET(&p.prefix.macip_addr.mac.octet, pkt, ETH_ALEN);
3887 } else {
3888 flog_err(
3889 EC_BGP_EVPN_ROUTE_INVALID,
3890 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3891 peer->bgp->vrf_id, peer->host, macaddr_len);
3892 goto fail;
3893 }
3894
3895
3896 /* Get the IP. */
3897 STREAM_GETC(pkt, ipaddr_len);
3898
3899 if (ipaddr_len != 0 && ipaddr_len != IPV4_MAX_BITLEN
3900 && ipaddr_len != IPV6_MAX_BITLEN) {
3901 flog_err(
3902 EC_BGP_EVPN_ROUTE_INVALID,
3903 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3904 peer->bgp->vrf_id, peer->host, ipaddr_len);
3905 goto fail;
3906 }
3907
3908 if (ipaddr_len) {
3909 ipaddr_len /= 8; /* Convert to bytes. */
3910 p.prefix.macip_addr.ip.ipa_type = (ipaddr_len == IPV4_MAX_BYTELEN)
3911 ? IPADDR_V4
3912 : IPADDR_V6;
3913 STREAM_GET(&p.prefix.macip_addr.ip.ip.addr, pkt, ipaddr_len);
3914 }
3915
3916 /* Get the VNI(s). Stored as bytes here. */
3917 STREAM_GET(&label[0], pkt, BGP_LABEL_BYTES);
3918 num_labels++;
3919
3920 /* Do we have a second VNI? */
3921 if (STREAM_READABLE(pkt)) {
3922 num_labels++;
3923 STREAM_GET(&label[1], pkt, BGP_LABEL_BYTES);
3924 }
3925
3926 /* Process the route. */
3927 if (attr)
3928 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
3929 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
3930 &prd, &label[0], num_labels, 0, &evpn);
3931 else
3932 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
3933 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
3934 &prd, &label[0], num_labels, &evpn);
3935 goto done;
3936
3937 fail:
3938 stream_failure:
3939 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
3940 "%u:%s - Rx EVPN Type-2 NLRI - corrupt, discarding",
3941 peer->bgp->vrf_id, peer->host);
3942 ret = -1;
3943 done:
3944 stream_free(pkt);
3945 return ret;
3946 }
3947
3948 /*
3949 * Process received EVPN type-3 route (advertise or withdraw).
3950 */
3951 static int process_type3_route(struct peer *peer, afi_t afi, safi_t safi,
3952 struct attr *attr, uint8_t *pfx, int psize,
3953 uint32_t addpath_id)
3954 {
3955 struct prefix_rd prd;
3956 struct prefix_evpn p;
3957 uint8_t ipaddr_len;
3958 uint32_t eth_tag;
3959 int ret;
3960
3961 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3962 * IP len (1) and IP (4 or 16).
3963 */
3964 if (psize != 17 && psize != 29) {
3965 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
3966 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3967 peer->bgp->vrf_id, peer->host, psize);
3968 return -1;
3969 }
3970
3971 /* If PMSI is present, log if it is anything other than IR.
3972 * Note: We just simply ignore the values as it is not clear if
3973 * doing anything else is better.
3974 */
3975 if (attr &&
3976 (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL))) {
3977 enum pta_type pmsi_tnl_type = bgp_attr_get_pmsi_tnl_type(attr);
3978
3979 if (pmsi_tnl_type != PMSI_TNLTYPE_INGR_REPL
3980 && pmsi_tnl_type != PMSI_TNLTYPE_PIM_SM) {
3981 flog_warn(
3982 EC_BGP_EVPN_PMSI_PRESENT,
3983 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3984 peer->bgp->vrf_id, peer->host, pmsi_tnl_type);
3985 }
3986 }
3987
3988 /* Make prefix_rd */
3989 prd.family = AF_UNSPEC;
3990 prd.prefixlen = 64;
3991 memcpy(&prd.val, pfx, 8);
3992 pfx += 8;
3993
3994 /* Make EVPN prefix. */
3995 memset(&p, 0, sizeof(p));
3996 p.family = AF_EVPN;
3997 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
3998 p.prefix.route_type = BGP_EVPN_IMET_ROUTE;
3999
4000 /* Copy Ethernet Tag */
4001 memcpy(&eth_tag, pfx, 4);
4002 p.prefix.imet_addr.eth_tag = ntohl(eth_tag);
4003 pfx += 4;
4004
4005 /* Get the IP. */
4006 ipaddr_len = *pfx++;
4007 if (ipaddr_len == IPV4_MAX_BITLEN) {
4008 p.prefix.imet_addr.ip.ipa_type = IPADDR_V4;
4009 memcpy(&p.prefix.imet_addr.ip.ip.addr, pfx, IPV4_MAX_BYTELEN);
4010 } else {
4011 flog_err(
4012 EC_BGP_EVPN_ROUTE_INVALID,
4013 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
4014 peer->bgp->vrf_id, peer->host, ipaddr_len);
4015 return -1;
4016 }
4017
4018 /* Process the route. */
4019 if (attr)
4020 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
4021 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
4022 &prd, NULL, 0, 0, NULL);
4023 else
4024 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
4025 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
4026 &prd, NULL, 0, NULL);
4027 return ret;
4028 }
4029
4030 /*
4031 * Process received EVPN type-5 route (advertise or withdraw).
4032 */
4033 static int process_type5_route(struct peer *peer, afi_t afi, safi_t safi,
4034 struct attr *attr, uint8_t *pfx, int psize,
4035 uint32_t addpath_id)
4036 {
4037 struct prefix_rd prd;
4038 struct prefix_evpn p;
4039 struct bgp_route_evpn evpn;
4040 uint8_t ippfx_len;
4041 uint32_t eth_tag;
4042 mpls_label_t label; /* holds the VNI as in the packet */
4043 int ret;
4044 bool is_valid_update = true;
4045
4046 /* Type-5 route should be 34 or 58 bytes:
4047 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4048 * GW (4 or 16) and VNI (3).
4049 * Note that the IP and GW should both be IPv4 or both IPv6.
4050 */
4051 if (psize != 34 && psize != 58) {
4052 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
4053 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4054 peer->bgp->vrf_id, peer->host, psize);
4055 return -1;
4056 }
4057
4058 /* Make prefix_rd */
4059 prd.family = AF_UNSPEC;
4060 prd.prefixlen = 64;
4061 memcpy(&prd.val, pfx, 8);
4062 pfx += 8;
4063
4064 /* Make EVPN prefix. */
4065 memset(&p, 0, sizeof(p));
4066 p.family = AF_EVPN;
4067 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
4068 p.prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
4069
4070 /* Additional information outside of prefix - ESI and GW IP */
4071 memset(&evpn, 0, sizeof(evpn));
4072
4073 /* Fetch ESI overlay index */
4074 if (attr)
4075 memcpy(&evpn.eth_s_id, pfx, sizeof(esi_t));
4076 pfx += ESI_BYTES;
4077
4078 /* Fetch Ethernet Tag. */
4079 memcpy(&eth_tag, pfx, 4);
4080 p.prefix.prefix_addr.eth_tag = ntohl(eth_tag);
4081 pfx += 4;
4082
4083 /* Fetch IP prefix length. */
4084 ippfx_len = *pfx++;
4085 if (ippfx_len > IPV6_MAX_BITLEN) {
4086 flog_err(
4087 EC_BGP_EVPN_ROUTE_INVALID,
4088 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4089 peer->bgp->vrf_id, peer->host, ippfx_len);
4090 return -1;
4091 }
4092 p.prefix.prefix_addr.ip_prefix_length = ippfx_len;
4093
4094 /* Determine IPv4 or IPv6 prefix */
4095 /* Since the address and GW are from the same family, this just becomes
4096 * a simple check on the total size.
4097 */
4098 if (psize == 34) {
4099 SET_IPADDR_V4(&p.prefix.prefix_addr.ip);
4100 memcpy(&p.prefix.prefix_addr.ip.ipaddr_v4, pfx, 4);
4101 pfx += 4;
4102 SET_IPADDR_V4(&evpn.gw_ip);
4103 memcpy(&evpn.gw_ip.ipaddr_v4, pfx, 4);
4104 pfx += 4;
4105 } else {
4106 SET_IPADDR_V6(&p.prefix.prefix_addr.ip);
4107 memcpy(&p.prefix.prefix_addr.ip.ipaddr_v6, pfx,
4108 IPV6_MAX_BYTELEN);
4109 pfx += IPV6_MAX_BYTELEN;
4110 SET_IPADDR_V6(&evpn.gw_ip);
4111 memcpy(&evpn.gw_ip.ipaddr_v6, pfx, IPV6_MAX_BYTELEN);
4112 pfx += IPV6_MAX_BYTELEN;
4113 }
4114
4115 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4116 memset(&label, 0, sizeof(label));
4117 memcpy(&label, pfx, BGP_LABEL_BYTES);
4118
4119 /*
4120 * If in future, we are required to access additional fields,
4121 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4122 * field
4123 */
4124
4125 /*
4126 * An update containing a non-zero gateway IP and a non-zero ESI
4127 * at the same time is should be treated as withdraw
4128 */
4129 if (bgp_evpn_is_esi_valid(&evpn.eth_s_id) &&
4130 !ipaddr_is_zero(&evpn.gw_ip)) {
4131 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
4132 "%s - Rx EVPN Type-5 ESI and gateway-IP both non-zero.",
4133 peer->host);
4134 is_valid_update = false;
4135 } else if (bgp_evpn_is_esi_valid(&evpn.eth_s_id))
4136 evpn.type = OVERLAY_INDEX_ESI;
4137 else if (!ipaddr_is_zero(&evpn.gw_ip))
4138 evpn.type = OVERLAY_INDEX_GATEWAY_IP;
4139 if (attr) {
4140 if (is_zero_mac(&attr->rmac) &&
4141 !bgp_evpn_is_esi_valid(&evpn.eth_s_id) &&
4142 ipaddr_is_zero(&evpn.gw_ip) && label == 0) {
4143 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
4144 "%s - Rx EVPN Type-5 ESI, gateway-IP, RMAC and label all zero",
4145 peer->host);
4146 is_valid_update = false;
4147 }
4148
4149 if (is_mcast_mac(&attr->rmac) || is_bcast_mac(&attr->rmac))
4150 is_valid_update = false;
4151 }
4152
4153 /* Process the route. */
4154 if (attr && is_valid_update)
4155 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
4156 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
4157 &prd, &label, 1, 0, &evpn);
4158 else {
4159 if (!is_valid_update) {
4160 char attr_str[BUFSIZ] = {0};
4161
4162 bgp_dump_attr(attr, attr_str, BUFSIZ);
4163 zlog_warn(
4164 "Invalid update from peer %s vrf %u prefix %pFX attr %s - treat as withdraw",
4165 peer->hostname, peer->bgp->vrf_id, &p,
4166 attr_str);
4167 }
4168 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
4169 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
4170 &prd, &label, 1, &evpn);
4171 }
4172
4173 return ret;
4174 }
4175
4176 static void evpn_mpattr_encode_type5(struct stream *s, const struct prefix *p,
4177 const struct prefix_rd *prd,
4178 mpls_label_t *label, uint32_t num_labels,
4179 struct attr *attr)
4180 {
4181 int len;
4182 char temp[16];
4183 const struct evpn_addr *p_evpn_p;
4184
4185 memset(&temp, 0, sizeof(temp));
4186 if (p->family != AF_EVPN)
4187 return;
4188 p_evpn_p = &(p->u.prefix_evpn);
4189
4190 /* len denites the total len of IP and GW-IP in the route
4191 IP and GW-IP have to be both ipv4 or ipv6
4192 */
4193 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
4194 len = 8; /* IP and GWIP are both ipv4 */
4195 else
4196 len = 32; /* IP and GWIP are both ipv6 */
4197 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4198 stream_putc(s, 8 + 10 + 4 + 1 + len + 3);
4199 stream_put(s, prd->val, 8);
4200 if (attr && attr->evpn_overlay.type == OVERLAY_INDEX_ESI)
4201 stream_put(s, &attr->esi, sizeof(esi_t));
4202 else
4203 stream_put(s, 0, sizeof(esi_t));
4204 stream_putl(s, p_evpn_p->prefix_addr.eth_tag);
4205 stream_putc(s, p_evpn_p->prefix_addr.ip_prefix_length);
4206 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
4207 stream_put_ipv4(s, p_evpn_p->prefix_addr.ip.ipaddr_v4.s_addr);
4208 else
4209 stream_put(s, &p_evpn_p->prefix_addr.ip.ipaddr_v6, 16);
4210 if (attr && attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP) {
4211 const struct bgp_route_evpn *evpn_overlay =
4212 bgp_attr_get_evpn_overlay(attr);
4213
4214 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
4215 stream_put_ipv4(s,
4216 evpn_overlay->gw_ip.ipaddr_v4.s_addr);
4217 else
4218 stream_put(s, &(evpn_overlay->gw_ip.ipaddr_v6), 16);
4219 } else {
4220 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
4221 stream_put_ipv4(s, 0);
4222 else
4223 stream_put(s, &temp, 16);
4224 }
4225
4226 if (num_labels)
4227 stream_put(s, label, 3);
4228 else
4229 stream_put3(s, 0);
4230 }
4231
4232 /*
4233 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4234 */
4235 static void cleanup_vni_on_disable(struct hash_bucket *bucket, struct bgp *bgp)
4236 {
4237 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
4238
4239 /* Remove EVPN routes and schedule for processing. */
4240 delete_routes_for_vni(bgp, vpn);
4241
4242 /* Clear "live" flag and see if hash needs to be freed. */
4243 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
4244 if (!is_vni_configured(vpn))
4245 bgp_evpn_free(bgp, vpn);
4246 }
4247
4248 /*
4249 * Free a VNI entry; iterator function called during cleanup.
4250 */
4251 static void free_vni_entry(struct hash_bucket *bucket, struct bgp *bgp)
4252 {
4253 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
4254
4255 delete_all_vni_routes(bgp, vpn);
4256 bgp_evpn_free(bgp, vpn);
4257 }
4258
4259 /*
4260 * Derive AUTO import RT for BGP VRF - L3VNI
4261 */
4262 static void evpn_auto_rt_import_add_for_vrf(struct bgp *bgp_vrf)
4263 {
4264 struct bgp *bgp_evpn = NULL;
4265
4266 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
4267 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
4268
4269 /* Map RT to VRF */
4270 bgp_evpn = bgp_get_evpn();
4271 if (!bgp_evpn)
4272 return;
4273 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
4274 }
4275
4276 /*
4277 * Delete AUTO import RT from BGP VRF - L3VNI
4278 */
4279 static void evpn_auto_rt_import_delete_for_vrf(struct bgp *bgp_vrf)
4280 {
4281 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
4282 }
4283
4284 /*
4285 * Derive AUTO export RT for BGP VRF - L3VNI
4286 */
4287 static void evpn_auto_rt_export_add_for_vrf(struct bgp *bgp_vrf)
4288 {
4289 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
4290 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
4291 }
4292
4293 /*
4294 * Delete AUTO export RT from BGP VRF - L3VNI
4295 */
4296 static void evpn_auto_rt_export_delete_for_vrf(struct bgp *bgp_vrf)
4297 {
4298 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
4299 }
4300
4301 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp *bgp_vrf)
4302 {
4303 struct bgp *bgp_evpn = NULL;
4304 struct listnode *node = NULL;
4305 struct bgpevpn *vpn = NULL;
4306
4307 bgp_evpn = bgp_get_evpn();
4308 if (!bgp_evpn)
4309 return;
4310
4311 /* update all type-5 routes */
4312 update_advertise_vrf_routes(bgp_vrf);
4313
4314 /* update all type-2 routes */
4315 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
4316 update_routes_for_vni(bgp_evpn, vpn);
4317 }
4318
4319 /*
4320 * Handle autort change for a given VNI.
4321 */
4322 static void update_autort_vni(struct hash_bucket *bucket, struct bgp *bgp)
4323 {
4324 struct bgpevpn *vpn = bucket->data;
4325
4326 if (!is_import_rt_configured(vpn)) {
4327 if (is_vni_live(vpn))
4328 bgp_evpn_uninstall_routes(bgp, vpn);
4329 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
4330 list_delete_all_node(vpn->import_rtl);
4331 bgp_evpn_derive_auto_rt_import(bgp, vpn);
4332 if (is_vni_live(vpn))
4333 bgp_evpn_install_routes(bgp, vpn);
4334 }
4335 if (!is_export_rt_configured(vpn)) {
4336 list_delete_all_node(vpn->export_rtl);
4337 bgp_evpn_derive_auto_rt_export(bgp, vpn);
4338 if (is_vni_live(vpn))
4339 bgp_evpn_handle_export_rt_change(bgp, vpn);
4340 }
4341 }
4342
4343 /*
4344 * Handle autort change for L3VNI.
4345 */
4346 static void update_autort_l3vni(struct bgp *bgp)
4347 {
4348 if ((CHECK_FLAG(bgp->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
4349 && (CHECK_FLAG(bgp->vrf_flags, BGP_VRF_EXPORT_RT_CFGD)))
4350 return;
4351
4352 if (!CHECK_FLAG(bgp->vrf_flags, BGP_VRF_IMPORT_RT_CFGD)) {
4353 if (is_l3vni_live(bgp))
4354 uninstall_routes_for_vrf(bgp);
4355
4356 /* Cleanup the RT to VRF mapping */
4357 bgp_evpn_unmap_vrf_from_its_rts(bgp);
4358
4359 /* Remove auto generated RT */
4360 evpn_auto_rt_import_delete_for_vrf(bgp);
4361
4362 list_delete_all_node(bgp->vrf_import_rtl);
4363
4364 /* Map auto derive or configured RTs */
4365 evpn_auto_rt_import_add_for_vrf(bgp);
4366 }
4367
4368 if (!CHECK_FLAG(bgp->vrf_flags, BGP_VRF_EXPORT_RT_CFGD)) {
4369 list_delete_all_node(bgp->vrf_export_rtl);
4370
4371 evpn_auto_rt_export_delete_for_vrf(bgp);
4372
4373 evpn_auto_rt_export_add_for_vrf(bgp);
4374
4375 if (is_l3vni_live(bgp))
4376 bgp_evpn_map_vrf_to_its_rts(bgp);
4377 }
4378
4379 if (!is_l3vni_live(bgp))
4380 return;
4381
4382 /* advertise type-5 routes if needed */
4383 update_advertise_vrf_routes(bgp);
4384
4385 /* install all remote routes belonging to this l3vni
4386 * into corresponding vrf
4387 */
4388 install_routes_for_vrf(bgp);
4389 }
4390
4391 /*
4392 * Public functions.
4393 */
4394
4395 /* withdraw type-5 route corresponding to ip prefix */
4396 void bgp_evpn_withdraw_type5_route(struct bgp *bgp_vrf, const struct prefix *p,
4397 afi_t afi, safi_t safi)
4398 {
4399 int ret = 0;
4400 struct prefix_evpn evp;
4401
4402 build_type5_prefix_from_ip_prefix(&evp, p);
4403 ret = delete_evpn_type5_route(bgp_vrf, &evp);
4404 if (ret)
4405 flog_err(
4406 EC_BGP_EVPN_ROUTE_DELETE,
4407 "%u failed to delete type-5 route for prefix %pFX in vrf %s",
4408 bgp_vrf->vrf_id, p, vrf_id_to_name(bgp_vrf->vrf_id));
4409 }
4410
4411 /* withdraw all type-5 routes for an address family */
4412 void bgp_evpn_withdraw_type5_routes(struct bgp *bgp_vrf, afi_t afi, safi_t safi)
4413 {
4414 struct bgp_table *table = NULL;
4415 struct bgp_dest *dest = NULL;
4416 struct bgp_path_info *pi;
4417
4418 table = bgp_vrf->rib[afi][safi];
4419 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
4420 /* Only care about "selected" routes. Also ensure that
4421 * these are routes that are injectable into EVPN.
4422 */
4423 /* TODO: Support for AddPath for EVPN. */
4424 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
4425 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
4426 && is_route_injectable_into_evpn(pi)) {
4427 bgp_evpn_withdraw_type5_route(
4428 bgp_vrf, bgp_dest_get_prefix(dest), afi,
4429 safi);
4430 break;
4431 }
4432 }
4433 }
4434 }
4435
4436 /*
4437 * evpn - enable advertisement of default g/w
4438 */
4439 void bgp_evpn_install_uninstall_default_route(struct bgp *bgp_vrf, afi_t afi,
4440 safi_t safi, bool add)
4441 {
4442 struct prefix ip_prefix;
4443
4444 /* form the default prefix 0.0.0.0/0 */
4445 memset(&ip_prefix, 0, sizeof(ip_prefix));
4446 ip_prefix.family = afi2family(afi);
4447
4448 if (add) {
4449 bgp_evpn_advertise_type5_route(bgp_vrf, &ip_prefix,
4450 NULL, afi, safi);
4451 } else {
4452 bgp_evpn_withdraw_type5_route(bgp_vrf, &ip_prefix,
4453 afi, safi);
4454 }
4455 }
4456
4457
4458 /*
4459 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4460 * to this function correspond to those of the source IP prefix (best
4461 * path in the case of the attr. In the case of a local prefix (when we
4462 * are advertising local subnets), the src_attr will be NULL.
4463 */
4464 void bgp_evpn_advertise_type5_route(struct bgp *bgp_vrf, const struct prefix *p,
4465 struct attr *src_attr, afi_t afi,
4466 safi_t safi)
4467 {
4468 int ret = 0;
4469 struct prefix_evpn evp;
4470
4471 build_type5_prefix_from_ip_prefix(&evp, p);
4472 ret = update_evpn_type5_route(bgp_vrf, &evp, src_attr, afi, safi);
4473 if (ret)
4474 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
4475 "%u: Failed to create type-5 route for prefix %pFX",
4476 bgp_vrf->vrf_id, p);
4477 }
4478
4479 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4480 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4481 * advertisement is enabled.
4482 */
4483 void bgp_evpn_advertise_type5_routes(struct bgp *bgp_vrf, afi_t afi,
4484 safi_t safi)
4485 {
4486 struct bgp_table *table = NULL;
4487 struct bgp_dest *dest = NULL;
4488 struct bgp_path_info *pi;
4489
4490 table = bgp_vrf->rib[afi][safi];
4491 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
4492 /* Need to identify the "selected" route entry to use its
4493 * attribute. Also, ensure that the route is injectable
4494 * into EVPN.
4495 * TODO: Support for AddPath for EVPN.
4496 */
4497 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
4498 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
4499 && is_route_injectable_into_evpn(pi)) {
4500
4501 /* apply the route-map */
4502 if (bgp_vrf->adv_cmd_rmap[afi][safi].map) {
4503 route_map_result_t ret;
4504 struct bgp_path_info tmp_pi;
4505 struct bgp_path_info_extra tmp_pie;
4506 struct attr tmp_attr;
4507
4508 tmp_attr = *pi->attr;
4509
4510 /* Fill temp path_info */
4511 prep_for_rmap_apply(&tmp_pi, &tmp_pie,
4512 dest, pi, pi->peer,
4513 &tmp_attr);
4514
4515 RESET_FLAG(tmp_attr.rmap_change_flags);
4516
4517 ret = route_map_apply(
4518 bgp_vrf->adv_cmd_rmap[afi][safi]
4519 .map,
4520 bgp_dest_get_prefix(dest),
4521 &tmp_pi);
4522 if (ret == RMAP_DENYMATCH) {
4523 bgp_attr_flush(&tmp_attr);
4524 continue;
4525 }
4526 bgp_evpn_advertise_type5_route(
4527 bgp_vrf,
4528 bgp_dest_get_prefix(dest),
4529 &tmp_attr, afi, safi);
4530 } else
4531 bgp_evpn_advertise_type5_route(
4532 bgp_vrf,
4533 bgp_dest_get_prefix(dest),
4534 pi->attr, afi, safi);
4535 break;
4536 }
4537 }
4538 }
4539 }
4540
4541 void evpn_rt_delete_auto(struct bgp *bgp, vni_t vni, struct list *rtl)
4542 {
4543 struct listnode *node, *nnode, *node_to_del;
4544 struct ecommunity *ecom, *ecom_auto;
4545 struct ecommunity_val eval;
4546
4547 if (bgp->advertise_autort_rfc8365)
4548 vni |= EVPN_AUTORT_VXLAN;
4549 encode_route_target_as((bgp->as & 0xFFFF), vni, &eval);
4550
4551 ecom_auto = ecommunity_new();
4552 ecommunity_add_val(ecom_auto, &eval, false, false);
4553 node_to_del = NULL;
4554
4555 for (ALL_LIST_ELEMENTS(rtl, node, nnode, ecom)) {
4556 if (ecommunity_match(ecom, ecom_auto)) {
4557 ecommunity_free(&ecom);
4558 node_to_del = node;
4559 break;
4560 }
4561 }
4562
4563 if (node_to_del)
4564 list_delete_node(rtl, node_to_del);
4565
4566 ecommunity_free(&ecom_auto);
4567 }
4568
4569 void bgp_evpn_configure_import_rt_for_vrf(struct bgp *bgp_vrf,
4570 struct ecommunity *ecomadd)
4571 {
4572 /* uninstall routes from vrf */
4573 if (is_l3vni_live(bgp_vrf))
4574 uninstall_routes_for_vrf(bgp_vrf);
4575
4576 /* Cleanup the RT to VRF mapping */
4577 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
4578
4579 /* Remove auto generated RT */
4580 evpn_auto_rt_import_delete_for_vrf(bgp_vrf);
4581
4582 /* Add the newly configured RT to RT list */
4583 listnode_add_sort(bgp_vrf->vrf_import_rtl, ecomadd);
4584 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
4585
4586 /* map VRF to its RTs and install routes matching the new RTs */
4587 if (is_l3vni_live(bgp_vrf)) {
4588 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
4589 install_routes_for_vrf(bgp_vrf);
4590 }
4591 }
4592
4593 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf,
4594 struct ecommunity *ecomdel)
4595 {
4596 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
4597 struct ecommunity *ecom = NULL;
4598
4599 /* uninstall routes from vrf */
4600 if (is_l3vni_live(bgp_vrf))
4601 uninstall_routes_for_vrf(bgp_vrf);
4602
4603 /* Cleanup the RT to VRF mapping */
4604 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
4605
4606 /* remove the RT from the RT list */
4607 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
4608 if (ecommunity_match(ecom, ecomdel)) {
4609 ecommunity_free(&ecom);
4610 node_to_del = node;
4611 break;
4612 }
4613 }
4614
4615 if (node_to_del)
4616 list_delete_node(bgp_vrf->vrf_import_rtl, node_to_del);
4617
4618 assert(bgp_vrf->vrf_import_rtl);
4619 /* fallback to auto import rt, if this was the last RT */
4620 if (bgp_vrf->vrf_import_rtl && list_isempty(bgp_vrf->vrf_import_rtl)) {
4621 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
4622 if (is_l3vni_live(bgp_vrf))
4623 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
4624 }
4625
4626 /* map VRFs to its RTs and install routes matching this new RT */
4627 if (is_l3vni_live(bgp_vrf)) {
4628 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
4629 install_routes_for_vrf(bgp_vrf);
4630 }
4631 }
4632
4633 void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf,
4634 struct ecommunity *ecomadd)
4635 {
4636 /* remove auto-generated RT */
4637 evpn_auto_rt_export_delete_for_vrf(bgp_vrf);
4638
4639 /* Add the new RT to the RT list */
4640 listnode_add_sort(bgp_vrf->vrf_export_rtl, ecomadd);
4641 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
4642
4643 if (is_l3vni_live(bgp_vrf))
4644 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
4645 }
4646
4647 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf,
4648 struct ecommunity *ecomdel)
4649 {
4650 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
4651 struct ecommunity *ecom = NULL;
4652
4653 /* Remove the RT from the RT list */
4654 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_export_rtl, node, nnode, ecom)) {
4655 if (ecommunity_match(ecom, ecomdel)) {
4656 ecommunity_free(&ecom);
4657 node_to_del = node;
4658 break;
4659 }
4660 }
4661
4662 if (node_to_del)
4663 list_delete_node(bgp_vrf->vrf_export_rtl, node_to_del);
4664
4665 /*
4666 * Temporary assert to make SA happy.
4667 * The ALL_LIST_ELEMENTS macro above has a NULL check
4668 * which means that SA is going to complain about
4669 * the list_isempty call, which doesn't NULL check.
4670 * So until we get this situation cleaned up, here
4671 * we are.
4672 */
4673 assert(bgp_vrf->vrf_export_rtl);
4674
4675 /* fall back to auto-generated RT if this was the last RT */
4676 if (list_isempty(bgp_vrf->vrf_export_rtl)) {
4677 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
4678 if (is_l3vni_live(bgp_vrf))
4679 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
4680 }
4681
4682 if (is_l3vni_live(bgp_vrf))
4683 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
4684 }
4685
4686 /*
4687 * Handle change to BGP router id. This is invoked twice by the change
4688 * handler, first before the router id has been changed and then after
4689 * the router id has been changed. The first invocation will result in
4690 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4691 * will result in the routes being re-advertised.
4692 */
4693 void bgp_evpn_handle_router_id_update(struct bgp *bgp, int withdraw)
4694 {
4695 struct listnode *node;
4696 struct bgp *bgp_vrf;
4697
4698 if (withdraw) {
4699
4700 /* delete and withdraw all the type-5 routes
4701 stored in the global table for this vrf
4702 */
4703 withdraw_router_id_vrf(bgp);
4704
4705 /* delete all the VNI routes (type-2/type-3) routes for all the
4706 * L2-VNIs
4707 */
4708 hash_iterate(bgp->vnihash,
4709 (void (*)(struct hash_bucket *,
4710 void *))withdraw_router_id_vni,
4711 bgp);
4712
4713 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
4714 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
4715 if (bgp_vrf->evpn_info->advertise_pip &&
4716 (bgp_vrf->evpn_info->pip_ip_static.s_addr
4717 == INADDR_ANY))
4718 bgp_vrf->evpn_info->pip_ip.s_addr
4719 = INADDR_ANY;
4720 }
4721 }
4722 } else {
4723
4724 /* Assign new default instance router-id */
4725 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
4726 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
4727 if (bgp_vrf->evpn_info->advertise_pip &&
4728 (bgp_vrf->evpn_info->pip_ip_static.s_addr
4729 == INADDR_ANY)) {
4730 bgp_vrf->evpn_info->pip_ip =
4731 bgp->router_id;
4732 /* advertise type-5 routes with
4733 * new nexthop
4734 */
4735 update_advertise_vrf_routes(bgp_vrf);
4736 }
4737 }
4738 }
4739
4740 /* advertise all routes in the vrf as type-5 routes with the new
4741 * RD
4742 */
4743 update_router_id_vrf(bgp);
4744
4745 /* advertise all the VNI routes (type-2/type-3) routes with the
4746 * new RD
4747 */
4748 hash_iterate(bgp->vnihash,
4749 (void (*)(struct hash_bucket *,
4750 void *))update_router_id_vni,
4751 bgp);
4752 }
4753 }
4754
4755 /*
4756 * Handle change to auto-RT algorithm - update and advertise local routes.
4757 */
4758 void bgp_evpn_handle_autort_change(struct bgp *bgp)
4759 {
4760 hash_iterate(bgp->vnihash,
4761 (void (*)(struct hash_bucket *,
4762 void*))update_autort_vni,
4763 bgp);
4764 if (bgp->l3vni)
4765 update_autort_l3vni(bgp);
4766 }
4767
4768 /*
4769 * Handle change to export RT - update and advertise local routes.
4770 */
4771 int bgp_evpn_handle_export_rt_change(struct bgp *bgp, struct bgpevpn *vpn)
4772 {
4773 return update_routes_for_vni(bgp, vpn);
4774 }
4775
4776 void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw)
4777 {
4778 if (withdraw)
4779 delete_withdraw_vrf_routes(bgp_vrf);
4780 else
4781 update_advertise_vrf_routes(bgp_vrf);
4782 }
4783
4784 /*
4785 * Handle change to RD. This is invoked twice by the change handler,
4786 * first before the RD has been changed and then after the RD has
4787 * been changed. The first invocation will result in local routes
4788 * of this VNI being deleted and withdrawn and the next will result
4789 * in the routes being re-advertised.
4790 */
4791 void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
4792 int withdraw)
4793 {
4794 if (withdraw)
4795 delete_withdraw_vni_routes(bgp, vpn);
4796 else
4797 update_advertise_vni_routes(bgp, vpn);
4798 }
4799
4800 /*
4801 * Install routes for this VNI. Invoked upon change to Import RT.
4802 */
4803 int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn)
4804 {
4805 return install_routes_for_vni(bgp, vpn);
4806 }
4807
4808 /*
4809 * Uninstall all routes installed for this VNI. Invoked upon change
4810 * to Import RT.
4811 */
4812 int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn)
4813 {
4814 return uninstall_routes_for_vni(bgp, vpn);
4815 }
4816
4817 /*
4818 * TODO: Hardcoded for a maximum of 2 VNIs right now
4819 */
4820 char *bgp_evpn_label2str(mpls_label_t *label, uint32_t num_labels, char *buf,
4821 int len)
4822 {
4823 vni_t vni1, vni2;
4824
4825 vni1 = label2vni(label);
4826 if (num_labels == 2) {
4827 vni2 = label2vni(label + 1);
4828 snprintf(buf, len, "%u/%u", vni1, vni2);
4829 } else
4830 snprintf(buf, len, "%u", vni1);
4831 return buf;
4832 }
4833
4834 /*
4835 * Function to convert evpn route to json format.
4836 * NOTE: We don't use prefix2str as the output here is a bit different.
4837 */
4838 void bgp_evpn_route2json(const struct prefix_evpn *p, json_object *json)
4839 {
4840 char buf1[ETHER_ADDR_STRLEN];
4841 char buf2[PREFIX2STR_BUFFER];
4842 uint8_t family;
4843 uint8_t prefixlen;
4844
4845 if (!json)
4846 return;
4847
4848 json_object_int_add(json, "routeType", p->prefix.route_type);
4849
4850 switch (p->prefix.route_type) {
4851 case BGP_EVPN_MAC_IP_ROUTE:
4852 json_object_int_add(json, "ethTag",
4853 p->prefix.macip_addr.eth_tag);
4854 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
4855 json_object_string_add(json, "mac",
4856 prefix_mac2str(&p->prefix.macip_addr.mac, buf1,
4857 sizeof(buf1)));
4858
4859 if (!is_evpn_prefix_ipaddr_none(p)) {
4860 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET :
4861 AF_INET6;
4862 prefixlen = (family == AF_INET) ?
4863 IPV4_MAX_BITLEN : IPV6_MAX_BITLEN;
4864 inet_ntop(family, &p->prefix.macip_addr.ip.ip.addr,
4865 buf2, PREFIX2STR_BUFFER);
4866 json_object_int_add(json, "ipLen", prefixlen);
4867 json_object_string_add(json, "ip", buf2);
4868 }
4869 break;
4870
4871 case BGP_EVPN_IMET_ROUTE:
4872 json_object_int_add(json, "ethTag",
4873 p->prefix.imet_addr.eth_tag);
4874 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET : AF_INET6;
4875 prefixlen = (family == AF_INET) ? IPV4_MAX_BITLEN :
4876 IPV6_MAX_BITLEN;
4877 inet_ntop(family, &p->prefix.imet_addr.ip.ip.addr, buf2,
4878 PREFIX2STR_BUFFER);
4879 json_object_int_add(json, "ipLen", prefixlen);
4880 json_object_string_add(json, "ip", buf2);
4881 break;
4882
4883 case BGP_EVPN_IP_PREFIX_ROUTE:
4884 json_object_int_add(json, "ethTag",
4885 p->prefix.prefix_addr.eth_tag);
4886 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET : AF_INET6;
4887 inet_ntop(family, &p->prefix.prefix_addr.ip.ip.addr,
4888 buf2, sizeof(buf2));
4889 json_object_int_add(json, "ipLen",
4890 p->prefix.prefix_addr.ip_prefix_length);
4891 json_object_string_add(json, "ip", buf2);
4892 break;
4893
4894 default:
4895 break;
4896 }
4897 }
4898
4899 /*
4900 * Encode EVPN prefix in Update (MP_REACH)
4901 */
4902 void bgp_evpn_encode_prefix(struct stream *s, const struct prefix *p,
4903 const struct prefix_rd *prd, mpls_label_t *label,
4904 uint32_t num_labels, struct attr *attr,
4905 bool addpath_capable, uint32_t addpath_tx_id)
4906 {
4907 struct prefix_evpn *evp = (struct prefix_evpn *)p;
4908 int len, ipa_len = 0;
4909
4910 if (addpath_capable)
4911 stream_putl(s, addpath_tx_id);
4912
4913 /* Route type */
4914 stream_putc(s, evp->prefix.route_type);
4915
4916 switch (evp->prefix.route_type) {
4917 case BGP_EVPN_MAC_IP_ROUTE:
4918 if (is_evpn_prefix_ipaddr_v4(evp))
4919 ipa_len = IPV4_MAX_BYTELEN;
4920 else if (is_evpn_prefix_ipaddr_v6(evp))
4921 ipa_len = IPV6_MAX_BYTELEN;
4922 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4923 len = 8 + 10 + 4 + 1 + 6 + 1 + ipa_len + 3;
4924 if (ipa_len && num_labels > 1) /* There are 2 VNIs */
4925 len += 3;
4926 stream_putc(s, len);
4927 stream_put(s, prd->val, 8); /* RD */
4928 if (attr)
4929 stream_put(s, &attr->esi, ESI_BYTES);
4930 else
4931 stream_put(s, 0, 10);
4932 stream_putl(s, evp->prefix.macip_addr.eth_tag); /* Ethernet Tag ID */
4933 stream_putc(s, 8 * ETH_ALEN); /* Mac Addr Len - bits */
4934 stream_put(s, evp->prefix.macip_addr.mac.octet, 6); /* Mac Addr */
4935 stream_putc(s, 8 * ipa_len); /* IP address Length */
4936 if (ipa_len) /* IP */
4937 stream_put(s, &evp->prefix.macip_addr.ip.ip.addr,
4938 ipa_len);
4939 /* 1st label is the L2 VNI */
4940 stream_put(s, label, BGP_LABEL_BYTES);
4941 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4942 if (ipa_len && num_labels > 1)
4943 stream_put(s, label + 1, BGP_LABEL_BYTES);
4944 break;
4945
4946 case BGP_EVPN_IMET_ROUTE:
4947 stream_putc(s, 17); // TODO: length - assumes IPv4 address
4948 stream_put(s, prd->val, 8); /* RD */
4949 stream_putl(s, evp->prefix.imet_addr.eth_tag); /* Ethernet Tag ID */
4950 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
4951 /* Originating Router's IP Addr */
4952 stream_put_in_addr(s, &evp->prefix.imet_addr.ip.ipaddr_v4);
4953 break;
4954
4955 case BGP_EVPN_ES_ROUTE:
4956 stream_putc(s, 23); /* TODO: length: assumes ipv4 VTEP */
4957 stream_put(s, prd->val, 8); /* RD */
4958 stream_put(s, evp->prefix.es_addr.esi.val, 10); /* ESI */
4959 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
4960 /* VTEP IP */
4961 stream_put_in_addr(s, &evp->prefix.es_addr.ip.ipaddr_v4);
4962 break;
4963
4964 case BGP_EVPN_AD_ROUTE:
4965 /* RD, ESI, EthTag, 1 VNI */
4966 len = RD_BYTES + ESI_BYTES + EVPN_ETH_TAG_BYTES + BGP_LABEL_BYTES;
4967 stream_putc(s, len);
4968 stream_put(s, prd->val, RD_BYTES); /* RD */
4969 stream_put(s, evp->prefix.ead_addr.esi.val, ESI_BYTES); /* ESI */
4970 stream_putl(s, evp->prefix.ead_addr.eth_tag); /* Ethernet Tag */
4971 stream_put(s, label, BGP_LABEL_BYTES);
4972 break;
4973
4974 case BGP_EVPN_IP_PREFIX_ROUTE:
4975 /* TODO: AddPath support. */
4976 evpn_mpattr_encode_type5(s, p, prd, label, num_labels, attr);
4977 break;
4978
4979 default:
4980 break;
4981 }
4982 }
4983
4984 int bgp_nlri_parse_evpn(struct peer *peer, struct attr *attr,
4985 struct bgp_nlri *packet, int withdraw)
4986 {
4987 uint8_t *pnt;
4988 uint8_t *lim;
4989 afi_t afi;
4990 safi_t safi;
4991 uint32_t addpath_id;
4992 bool addpath_capable;
4993 int psize = 0;
4994 uint8_t rtype;
4995 struct prefix p;
4996
4997 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4998 pnt = packet->nlri;
4999 lim = pnt + packet->length;
5000 afi = packet->afi;
5001 safi = packet->safi;
5002 addpath_id = 0;
5003
5004 addpath_capable = bgp_addpath_encode_rx(peer, afi, safi);
5005
5006 for (; pnt < lim; pnt += psize) {
5007 /* Clear prefix structure. */
5008 memset(&p, 0, sizeof(p));
5009
5010 /* Deal with path-id if AddPath is supported. */
5011 if (addpath_capable) {
5012 /* When packet overflow occurs return immediately. */
5013 if (pnt + BGP_ADDPATH_ID_LEN > lim)
5014 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
5015
5016 memcpy(&addpath_id, pnt, BGP_ADDPATH_ID_LEN);
5017 addpath_id = ntohl(addpath_id);
5018 pnt += BGP_ADDPATH_ID_LEN;
5019 }
5020
5021 /* All EVPN NLRI types start with type and length. */
5022 if (pnt + 2 > lim)
5023 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE;
5024
5025 rtype = *pnt++;
5026 psize = *pnt++;
5027
5028 /* When packet overflow occur return immediately. */
5029 if (pnt + psize > lim)
5030 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
5031
5032 switch (rtype) {
5033 case BGP_EVPN_MAC_IP_ROUTE:
5034 if (process_type2_route(peer, afi, safi,
5035 withdraw ? NULL : attr, pnt,
5036 psize, addpath_id)) {
5037 flog_err(
5038 EC_BGP_EVPN_FAIL,
5039 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
5040 peer->bgp->vrf_id, peer->host, psize);
5041 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE;
5042 }
5043 break;
5044
5045 case BGP_EVPN_IMET_ROUTE:
5046 if (process_type3_route(peer, afi, safi,
5047 withdraw ? NULL : attr, pnt,
5048 psize, addpath_id)) {
5049 flog_err(
5050 EC_BGP_PKT_PROCESS,
5051 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
5052 peer->bgp->vrf_id, peer->host, psize);
5053 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE;
5054 }
5055 break;
5056
5057 case BGP_EVPN_ES_ROUTE:
5058 if (bgp_evpn_type4_route_process(peer, afi, safi,
5059 withdraw ? NULL : attr, pnt,
5060 psize, addpath_id)) {
5061 flog_err(
5062 EC_BGP_PKT_PROCESS,
5063 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
5064 peer->bgp->vrf_id, peer->host, psize);
5065 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE;
5066 }
5067 break;
5068
5069 case BGP_EVPN_AD_ROUTE:
5070 if (bgp_evpn_type1_route_process(peer, afi, safi,
5071 withdraw ? NULL : attr, pnt,
5072 psize, addpath_id)) {
5073 flog_err(
5074 EC_BGP_PKT_PROCESS,
5075 "%u:%s - Error in processing EVPN type-1 NLRI size %d",
5076 peer->bgp->vrf_id, peer->host, psize);
5077 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE1_SIZE;
5078 }
5079 break;
5080
5081 case BGP_EVPN_IP_PREFIX_ROUTE:
5082 if (process_type5_route(peer, afi, safi,
5083 withdraw ? NULL : attr, pnt,
5084 psize, addpath_id)) {
5085 flog_err(
5086 EC_BGP_PKT_PROCESS,
5087 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
5088 peer->bgp->vrf_id, peer->host, psize);
5089 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE;
5090 }
5091 break;
5092
5093 default:
5094 break;
5095 }
5096 }
5097
5098 /* Packet length consistency check. */
5099 if (pnt != lim)
5100 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH;
5101
5102 return BGP_NLRI_PARSE_OK;
5103 }
5104
5105 /*
5106 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5107 * The mapping will be used during route processing.
5108 * bgp_vrf: specific bgp vrf instance on which RT is configured
5109 */
5110 void bgp_evpn_map_vrf_to_its_rts(struct bgp *bgp_vrf)
5111 {
5112 uint32_t i = 0;
5113 struct ecommunity_val *eval = NULL;
5114 struct listnode *node = NULL, *nnode = NULL;
5115 struct ecommunity *ecom = NULL;
5116
5117 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
5118 for (i = 0; i < ecom->size; i++) {
5119 eval = (struct ecommunity_val *)(ecom->val
5120 + (i
5121 * ECOMMUNITY_SIZE));
5122 map_vrf_to_rt(bgp_vrf, eval);
5123 }
5124 }
5125 }
5126
5127 /*
5128 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5129 */
5130 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *bgp_vrf)
5131 {
5132 uint32_t i;
5133 struct ecommunity_val *eval;
5134 struct listnode *node, *nnode;
5135 struct ecommunity *ecom;
5136
5137 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
5138 for (i = 0; i < ecom->size; i++) {
5139 struct vrf_irt_node *irt;
5140 struct ecommunity_val eval_tmp;
5141
5142 eval = (struct ecommunity_val *)(ecom->val
5143 + (i
5144 * ECOMMUNITY_SIZE));
5145 /* If using "automatic" RT, we only care about the
5146 * local-admin sub-field.
5147 * This is to facilitate using VNI as the RT for EBGP
5148 * peering too.
5149 */
5150 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
5151 if (!CHECK_FLAG(bgp_vrf->vrf_flags,
5152 BGP_VRF_IMPORT_RT_CFGD))
5153 mask_ecom_global_admin(&eval_tmp, eval);
5154
5155 irt = lookup_vrf_import_rt(&eval_tmp);
5156 if (irt)
5157 unmap_vrf_from_rt(bgp_vrf, irt);
5158 }
5159 }
5160 }
5161
5162
5163 /*
5164 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5165 * The mapping will be used during route processing.
5166 */
5167 void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
5168 {
5169 uint32_t i;
5170 struct ecommunity_val *eval;
5171 struct listnode *node, *nnode;
5172 struct ecommunity *ecom;
5173
5174 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
5175 for (i = 0; i < ecom->size; i++) {
5176 eval = (struct ecommunity_val *)(ecom->val
5177 + (i
5178 * ECOMMUNITY_SIZE));
5179 map_vni_to_rt(bgp, vpn, eval);
5180 }
5181 }
5182 }
5183
5184 /*
5185 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5186 */
5187 void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
5188 {
5189 uint32_t i;
5190 struct ecommunity_val *eval;
5191 struct listnode *node, *nnode;
5192 struct ecommunity *ecom;
5193
5194 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
5195 for (i = 0; i < ecom->size; i++) {
5196 struct irt_node *irt;
5197 struct ecommunity_val eval_tmp;
5198
5199 eval = (struct ecommunity_val *)(ecom->val
5200 + (i
5201 * ECOMMUNITY_SIZE));
5202 /* If using "automatic" RT, we only care about the
5203 * local-admin sub-field.
5204 * This is to facilitate using VNI as the RT for EBGP
5205 * peering too.
5206 */
5207 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
5208 if (!is_import_rt_configured(vpn))
5209 mask_ecom_global_admin(&eval_tmp, eval);
5210
5211 irt = lookup_import_rt(bgp, &eval_tmp);
5212 if (irt)
5213 unmap_vni_from_rt(bgp, vpn, irt);
5214 }
5215 }
5216 }
5217
5218 /*
5219 * Derive Import RT automatically for VNI and map VNI to RT.
5220 * The mapping will be used during route processing.
5221 */
5222 void bgp_evpn_derive_auto_rt_import(struct bgp *bgp, struct bgpevpn *vpn)
5223 {
5224 form_auto_rt(bgp, vpn->vni, vpn->import_rtl);
5225 UNSET_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD);
5226
5227 /* Map RT to VNI */
5228 bgp_evpn_map_vni_to_its_rts(bgp, vpn);
5229 }
5230
5231 /*
5232 * Derive Export RT automatically for VNI.
5233 */
5234 void bgp_evpn_derive_auto_rt_export(struct bgp *bgp, struct bgpevpn *vpn)
5235 {
5236 form_auto_rt(bgp, vpn->vni, vpn->export_rtl);
5237 UNSET_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD);
5238 }
5239
5240 /*
5241 * Derive RD automatically for VNI using passed information - it
5242 * is of the form RouterId:unique-id-for-vni.
5243 */
5244 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp)
5245 {
5246 if (is_vrf_rd_configured(bgp))
5247 return;
5248
5249 form_auto_rd(bgp->router_id, bgp->vrf_rd_id, &bgp->vrf_prd);
5250 }
5251
5252 /*
5253 * Derive RD automatically for VNI using passed information - it
5254 * is of the form RouterId:unique-id-for-vni.
5255 */
5256 void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn)
5257 {
5258 char buf[BGP_EVPN_PREFIX_RD_LEN];
5259
5260 vpn->prd.family = AF_UNSPEC;
5261 vpn->prd.prefixlen = 64;
5262 snprintfrr(buf, sizeof(buf), "%pI4:%hu", &bgp->router_id, vpn->rd_id);
5263 (void)str2prefix_rd(buf, &vpn->prd);
5264 UNSET_FLAG(vpn->flags, VNI_FLAG_RD_CFGD);
5265 }
5266
5267 /*
5268 * Lookup L3-VNI
5269 */
5270 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni)
5271 {
5272 struct list *inst = bm->bgp;
5273 struct listnode *node;
5274 struct bgp *bgp_vrf;
5275
5276 for (ALL_LIST_ELEMENTS_RO(inst, node, bgp_vrf)) {
5277 if (bgp_vrf->l3vni == vni)
5278 return true;
5279 }
5280
5281 return false;
5282 }
5283
5284 /*
5285 * Lookup VNI.
5286 */
5287 struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni)
5288 {
5289 struct bgpevpn *vpn;
5290 struct bgpevpn tmp;
5291
5292 memset(&tmp, 0, sizeof(tmp));
5293 tmp.vni = vni;
5294 vpn = hash_lookup(bgp->vnihash, &tmp);
5295 return vpn;
5296 }
5297
5298 /*
5299 * Create a new vpn - invoked upon configuration or zebra notification.
5300 */
5301 struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
5302 struct in_addr originator_ip,
5303 vrf_id_t tenant_vrf_id,
5304 struct in_addr mcast_grp,
5305 ifindex_t svi_ifindex)
5306 {
5307 struct bgpevpn *vpn;
5308
5309 vpn = XCALLOC(MTYPE_BGP_EVPN, sizeof(struct bgpevpn));
5310
5311 /* Set values - RD and RT set to defaults. */
5312 vpn->vni = vni;
5313 vpn->originator_ip = originator_ip;
5314 vpn->tenant_vrf_id = tenant_vrf_id;
5315 vpn->mcast_grp = mcast_grp;
5316 vpn->svi_ifindex = svi_ifindex;
5317
5318 /* Initialize route-target import and export lists */
5319 vpn->import_rtl = list_new();
5320 vpn->import_rtl->cmp =
5321 (int (*)(void *, void *))bgp_evpn_route_target_cmp;
5322 vpn->import_rtl->del = bgp_evpn_xxport_delete_ecomm;
5323 vpn->export_rtl = list_new();
5324 vpn->export_rtl->cmp =
5325 (int (*)(void *, void *))bgp_evpn_route_target_cmp;
5326 vpn->export_rtl->del = bgp_evpn_xxport_delete_ecomm;
5327 bf_assign_index(bm->rd_idspace, vpn->rd_id);
5328 derive_rd_rt_for_vni(bgp, vpn);
5329
5330 /* Initialize EVPN route table. */
5331 vpn->route_table = bgp_table_init(bgp, AFI_L2VPN, SAFI_EVPN);
5332
5333 /* Add to hash */
5334 (void)hash_get(bgp->vnihash, vpn, hash_alloc_intern);
5335
5336 bgp_evpn_remote_ip_hash_init(vpn);
5337 bgp_evpn_link_to_vni_svi_hash(bgp, vpn);
5338
5339 /* add to l2vni list on corresponding vrf */
5340 bgpevpn_link_to_l3vni(vpn);
5341
5342 bgp_evpn_vni_es_init(vpn);
5343
5344 QOBJ_REG(vpn, bgpevpn);
5345 return vpn;
5346 }
5347
5348 /*
5349 * Free a given VPN - called in multiple scenarios such as zebra
5350 * notification, configuration being deleted, advertise-all-vni disabled etc.
5351 * This just frees appropriate memory, caller should have taken other
5352 * needed actions.
5353 */
5354 void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn)
5355 {
5356 bgp_evpn_remote_ip_hash_destroy(vpn);
5357 bgp_evpn_vni_es_cleanup(vpn);
5358 bgpevpn_unlink_from_l3vni(vpn);
5359 bgp_table_unlock(vpn->route_table);
5360 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
5361 list_delete(&vpn->import_rtl);
5362 list_delete(&vpn->export_rtl);
5363 bf_release_index(bm->rd_idspace, vpn->rd_id);
5364 hash_release(bgp->vni_svi_hash, vpn);
5365 hash_release(bgp->vnihash, vpn);
5366 QOBJ_UNREG(vpn);
5367 XFREE(MTYPE_BGP_EVPN, vpn);
5368 }
5369
5370 static void hash_evpn_free(struct bgpevpn *vpn)
5371 {
5372 XFREE(MTYPE_BGP_EVPN, vpn);
5373 }
5374
5375 /*
5376 * Import evpn route from global table to VNI/VRF/ESI.
5377 */
5378 int bgp_evpn_import_route(struct bgp *bgp, afi_t afi, safi_t safi,
5379 const struct prefix *p, struct bgp_path_info *pi)
5380 {
5381 return install_uninstall_evpn_route(bgp, afi, safi, p, pi, 1);
5382 }
5383
5384 /*
5385 * Unimport evpn route from VNI/VRF/ESI.
5386 */
5387 int bgp_evpn_unimport_route(struct bgp *bgp, afi_t afi, safi_t safi,
5388 const struct prefix *p, struct bgp_path_info *pi)
5389 {
5390 return install_uninstall_evpn_route(bgp, afi, safi, p, pi, 0);
5391 }
5392
5393 /* filter routes which have martian next hops */
5394 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp *bgp)
5395 {
5396 afi_t afi;
5397 safi_t safi;
5398 struct bgp_dest *rd_dest, *dest;
5399 struct bgp_table *table;
5400 struct bgp_path_info *pi;
5401
5402 afi = AFI_L2VPN;
5403 safi = SAFI_EVPN;
5404
5405 /* Walk entire global routing table and evaluate routes which could be
5406 * imported into this VPN. Note that we cannot just look at the routes
5407 * for the VNI's RD -
5408 * remote routes applicable for this VNI could have any RD.
5409 */
5410 /* EVPN routes are a 2-level table. */
5411 for (rd_dest = bgp_table_top(bgp->rib[afi][safi]); rd_dest;
5412 rd_dest = bgp_route_next(rd_dest)) {
5413 table = bgp_dest_get_bgp_table_info(rd_dest);
5414 if (!table)
5415 continue;
5416
5417 for (dest = bgp_table_top(table); dest;
5418 dest = bgp_route_next(dest)) {
5419
5420 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
5421 pi = pi->next) {
5422
5423 /* Consider "valid" remote routes applicable for
5424 * this VNI. */
5425 if (!(pi->type == ZEBRA_ROUTE_BGP
5426 && pi->sub_type == BGP_ROUTE_NORMAL))
5427 continue;
5428 if (bgp_nexthop_self(bgp, afi, pi->type,
5429 pi->sub_type, pi->attr,
5430 dest)) {
5431 const struct prefix *p =
5432 bgp_dest_get_prefix(dest);
5433
5434 if (bgp_debug_update(pi->peer, p, NULL,
5435 1)) {
5436 char attr_str[BUFSIZ] = {0};
5437
5438 bgp_dump_attr(pi->attr,
5439 attr_str,
5440 sizeof(attr_str));
5441
5442 zlog_debug(
5443 "%u: prefix %pBD with attr %s - DENIED due to martian or self nexthop",
5444 bgp->vrf_id, dest,
5445 attr_str);
5446 }
5447 bgp_evpn_unimport_route(bgp, afi, safi,
5448 p, pi);
5449
5450 bgp_rib_remove(dest, pi, pi->peer, afi,
5451 safi);
5452 }
5453 }
5454 }
5455 }
5456
5457 return 0;
5458 }
5459
5460 /*
5461 * Handle del of a local MACIP.
5462 */
5463 int bgp_evpn_local_macip_del(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
5464 struct ipaddr *ip, int state)
5465 {
5466 struct bgpevpn *vpn;
5467 struct prefix_evpn p;
5468 struct bgp_dest *dest;
5469
5470 /* Lookup VNI hash - should exist. */
5471 vpn = bgp_evpn_lookup_vni(bgp, vni);
5472 if (!vpn || !is_vni_live(vpn)) {
5473 flog_warn(EC_BGP_EVPN_VPN_VNI,
5474 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5475 bgp->vrf_id, vni, vpn ? "not live" : "not found");
5476 return -1;
5477 }
5478
5479 build_evpn_type2_prefix(&p, mac, ip);
5480 if (state == ZEBRA_NEIGH_ACTIVE) {
5481 /* Remove EVPN type-2 route and schedule for processing. */
5482 delete_evpn_route(bgp, vpn, &p);
5483 } else {
5484 /* Re-instate the current remote best path if any */
5485 dest = bgp_node_lookup(vpn->route_table, (struct prefix *)&p);
5486 if (dest) {
5487 evpn_zebra_reinstall_best_route(bgp, vpn, dest);
5488 bgp_dest_unlock_node(dest);
5489 }
5490 }
5491
5492 return 0;
5493 }
5494
5495 /*
5496 * Handle add of a local MACIP.
5497 */
5498 int bgp_evpn_local_macip_add(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
5499 struct ipaddr *ip, uint8_t flags, uint32_t seq, esi_t *esi)
5500 {
5501 struct bgpevpn *vpn;
5502 struct prefix_evpn p;
5503
5504 /* Lookup VNI hash - should exist. */
5505 vpn = bgp_evpn_lookup_vni(bgp, vni);
5506 if (!vpn || !is_vni_live(vpn)) {
5507 flog_warn(EC_BGP_EVPN_VPN_VNI,
5508 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5509 bgp->vrf_id, vni, vpn ? "not live" : "not found");
5510 return -1;
5511 }
5512
5513 /* Create EVPN type-2 route and schedule for processing. */
5514 build_evpn_type2_prefix(&p, mac, ip);
5515 if (update_evpn_route(bgp, vpn, &p, flags, seq, esi)) {
5516 flog_err(
5517 EC_BGP_EVPN_ROUTE_CREATE,
5518 "%u:Failed to create Type-2 route, VNI %u %s MAC %pEA IP %pIA (flags: 0x%x)",
5519 bgp->vrf_id, vpn->vni,
5520 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY)
5521 ? "sticky gateway"
5522 : "",
5523 mac, ip, flags);
5524 return -1;
5525 }
5526
5527 return 0;
5528 }
5529
5530 static void link_l2vni_hash_to_l3vni(struct hash_bucket *bucket,
5531 struct bgp *bgp_vrf)
5532 {
5533 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
5534 struct bgp *bgp_evpn = NULL;
5535
5536 bgp_evpn = bgp_get_evpn();
5537 assert(bgp_evpn);
5538
5539 if (vpn->tenant_vrf_id == bgp_vrf->vrf_id)
5540 bgpevpn_link_to_l3vni(vpn);
5541 }
5542
5543 int bgp_evpn_local_l3vni_add(vni_t l3vni, vrf_id_t vrf_id,
5544 struct ethaddr *svi_rmac,
5545 struct ethaddr *vrr_rmac,
5546 struct in_addr originator_ip, int filter,
5547 ifindex_t svi_ifindex,
5548 bool is_anycast_mac)
5549 {
5550 struct bgp *bgp_vrf = NULL; /* bgp VRF instance */
5551 struct bgp *bgp_evpn = NULL; /* EVPN bgp instance */
5552 struct listnode *node = NULL;
5553 struct bgpevpn *vpn = NULL;
5554 as_t as = 0;
5555
5556 /* get the EVPN instance - required to get the AS number for VRF
5557 * auto-creatio
5558 */
5559 bgp_evpn = bgp_get_evpn();
5560 if (!bgp_evpn) {
5561 flog_err(
5562 EC_BGP_NO_DFLT,
5563 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5564 l3vni);
5565 return -1;
5566 }
5567 as = bgp_evpn->as;
5568
5569 /* if the BGP vrf instance doesn't exist - create one */
5570 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
5571 if (!bgp_vrf) {
5572
5573 int ret = 0;
5574
5575 ret = bgp_get_vty(&bgp_vrf, &as, vrf_id_to_name(vrf_id),
5576 vrf_id == VRF_DEFAULT
5577 ? BGP_INSTANCE_TYPE_DEFAULT
5578 : BGP_INSTANCE_TYPE_VRF);
5579 switch (ret) {
5580 case BGP_ERR_AS_MISMATCH:
5581 flog_err(EC_BGP_EVPN_AS_MISMATCH,
5582 "BGP instance is already running; AS is %u",
5583 as);
5584 return -1;
5585 case BGP_ERR_INSTANCE_MISMATCH:
5586 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH,
5587 "BGP instance type mismatch");
5588 return -1;
5589 }
5590
5591 /* mark as auto created */
5592 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO);
5593 }
5594
5595 /* associate the vrf with l3vni and related parameters */
5596 bgp_vrf->l3vni = l3vni;
5597 bgp_vrf->originator_ip = originator_ip;
5598 bgp_vrf->l3vni_svi_ifindex = svi_ifindex;
5599 bgp_vrf->evpn_info->is_anycast_mac = is_anycast_mac;
5600
5601 /* copy anycast MAC from VRR MAC */
5602 memcpy(&bgp_vrf->rmac, vrr_rmac, ETH_ALEN);
5603 /* copy sys RMAC from SVI MAC */
5604 memcpy(&bgp_vrf->evpn_info->pip_rmac_zebra, svi_rmac, ETH_ALEN);
5605 /* PIP user configured mac is not present use svi mac as sys mac */
5606 if (is_zero_mac(&bgp_vrf->evpn_info->pip_rmac_static))
5607 memcpy(&bgp_vrf->evpn_info->pip_rmac, svi_rmac, ETH_ALEN);
5608
5609 if (bgp_debug_zebra(NULL))
5610 zlog_debug(
5611 "VRF %s vni %u pip %s RMAC %pEA sys RMAC %pEA static RMAC %pEA is_anycast_mac %s",
5612 vrf_id_to_name(bgp_vrf->vrf_id), bgp_vrf->l3vni,
5613 bgp_vrf->evpn_info->advertise_pip ? "enable"
5614 : "disable",
5615 &bgp_vrf->rmac, &bgp_vrf->evpn_info->pip_rmac,
5616 &bgp_vrf->evpn_info->pip_rmac_static,
5617 is_anycast_mac ? "Enable" : "Disable");
5618
5619 /* set the right filter - are we using l3vni only for prefix routes? */
5620 if (filter) {
5621 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
5622
5623 /*
5624 * VNI_FLAG_USE_TWO_LABELS flag for linked L2VNIs should not be
5625 * set before linking vrf to L3VNI. Thus, no need to clear
5626 * that explicitly.
5627 */
5628 } else {
5629 UNSET_FLAG(bgp_vrf->vrf_flags,
5630 BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
5631
5632 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) {
5633 if (!CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)) {
5634
5635 /*
5636 * If we are flapping VNI_FLAG_USE_TWO_LABELS
5637 * flag, update all MACIP routes in this VNI
5638 */
5639 SET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
5640 update_all_type2_routes(bgp_evpn, vpn);
5641 }
5642 }
5643 }
5644
5645 /* Map auto derive or configured RTs */
5646 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
5647 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
5648 else
5649 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
5650
5651 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD))
5652 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
5653
5654 /* auto derive RD */
5655 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
5656
5657 /* link all corresponding l2vnis */
5658 hash_iterate(bgp_evpn->vnihash,
5659 (void (*)(struct hash_bucket *,
5660 void *))link_l2vni_hash_to_l3vni,
5661 bgp_vrf);
5662
5663 /* Only update all corresponding type-2 routes if we are advertising two
5664 * labels along with type-2 routes
5665 */
5666 if (!filter)
5667 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
5668 update_routes_for_vni(bgp_evpn, vpn);
5669
5670 /* advertise type-5 routes if needed */
5671 update_advertise_vrf_routes(bgp_vrf);
5672
5673 /* install all remote routes belonging to this l3vni into correspondng
5674 * vrf */
5675 install_routes_for_vrf(bgp_vrf);
5676
5677 return 0;
5678 }
5679
5680 int bgp_evpn_local_l3vni_del(vni_t l3vni, vrf_id_t vrf_id)
5681 {
5682 struct bgp *bgp_vrf = NULL; /* bgp vrf instance */
5683 struct bgp *bgp_evpn = NULL; /* EVPN bgp instance */
5684 struct listnode *node = NULL;
5685 struct listnode *next = NULL;
5686 struct bgpevpn *vpn = NULL;
5687
5688 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
5689 if (!bgp_vrf) {
5690 flog_err(
5691 EC_BGP_NO_DFLT,
5692 "Cannot process L3VNI %u Del - Could not find BGP instance",
5693 l3vni);
5694 return -1;
5695 }
5696
5697 bgp_evpn = bgp_get_evpn();
5698 if (!bgp_evpn) {
5699 flog_err(
5700 EC_BGP_NO_DFLT,
5701 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5702 l3vni);
5703 return -1;
5704 }
5705
5706 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5707 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5708 * routes. This will uninstalling the routes from zebra and decremnt the
5709 * bgp info count.
5710 */
5711 uninstall_routes_for_vrf(bgp_vrf);
5712
5713 /* delete/withdraw all type-5 routes */
5714 delete_withdraw_vrf_routes(bgp_vrf);
5715
5716 /* remove the l3vni from vrf instance */
5717 bgp_vrf->l3vni = 0;
5718
5719 /* remove the Rmac from the BGP vrf */
5720 memset(&bgp_vrf->rmac, 0, sizeof(struct ethaddr));
5721 memset(&bgp_vrf->evpn_info->pip_rmac_zebra, 0, ETH_ALEN);
5722 if (is_zero_mac(&bgp_vrf->evpn_info->pip_rmac_static) &&
5723 !is_zero_mac(&bgp_vrf->evpn_info->pip_rmac))
5724 memset(&bgp_vrf->evpn_info->pip_rmac, 0, ETH_ALEN);
5725
5726 /* remove default import RT or Unmap non-default import RT */
5727 if (!list_isempty(bgp_vrf->vrf_import_rtl)) {
5728 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
5729 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
5730 list_delete_all_node(bgp_vrf->vrf_import_rtl);
5731 }
5732
5733 /* remove default export RT */
5734 if (!list_isempty(bgp_vrf->vrf_export_rtl) &&
5735 !CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD)) {
5736 list_delete_all_node(bgp_vrf->vrf_export_rtl);
5737 }
5738
5739 /* update all corresponding local mac-ip routes */
5740 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY)) {
5741 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) {
5742 UNSET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
5743 update_routes_for_vni(bgp_evpn, vpn);
5744 }
5745 }
5746
5747 /* If any L2VNIs point to this instance, unlink them. */
5748 for (ALL_LIST_ELEMENTS(bgp_vrf->l2vnis, node, next, vpn))
5749 bgpevpn_unlink_from_l3vni(vpn);
5750
5751 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
5752
5753 /* Delete the instance if it was autocreated */
5754 if (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO))
5755 bgp_delete(bgp_vrf);
5756
5757 return 0;
5758 }
5759
5760 /*
5761 * Handle del of a local VNI.
5762 */
5763 int bgp_evpn_local_vni_del(struct bgp *bgp, vni_t vni)
5764 {
5765 struct bgpevpn *vpn;
5766
5767 /* Locate VNI hash */
5768 vpn = bgp_evpn_lookup_vni(bgp, vni);
5769 if (!vpn) {
5770 if (bgp_debug_zebra(NULL))
5771 flog_warn(
5772 EC_BGP_EVPN_VPN_VNI,
5773 "%u: VNI hash entry for VNI %u not found at DEL",
5774 bgp->vrf_id, vni);
5775 return 0;
5776 }
5777
5778 /* Remove all local EVPN routes and schedule for processing (to
5779 * withdraw from peers).
5780 */
5781 delete_routes_for_vni(bgp, vpn);
5782
5783 bgp_evpn_unlink_from_vni_svi_hash(bgp, vpn);
5784
5785 vpn->svi_ifindex = 0;
5786 /*
5787 * tunnel is no longer active, del tunnel ip address from tip_hash
5788 */
5789 bgp_tip_del(bgp, &vpn->originator_ip);
5790
5791 /* Clear "live" flag and see if hash needs to be freed. */
5792 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
5793 if (!is_vni_configured(vpn))
5794 bgp_evpn_free(bgp, vpn);
5795
5796 return 0;
5797 }
5798
5799 /*
5800 * Handle add (or update) of a local VNI. The VNI changes we care
5801 * about are for the local-tunnel-ip and the (tenant) VRF.
5802 */
5803 int bgp_evpn_local_vni_add(struct bgp *bgp, vni_t vni,
5804 struct in_addr originator_ip,
5805 vrf_id_t tenant_vrf_id,
5806 struct in_addr mcast_grp,
5807 ifindex_t svi_ifindex)
5808 {
5809 struct bgpevpn *vpn;
5810 struct prefix_evpn p;
5811
5812 /* Lookup VNI. If present and no change, exit. */
5813 vpn = bgp_evpn_lookup_vni(bgp, vni);
5814 if (vpn) {
5815
5816 if (is_vni_live(vpn)
5817 && IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip)
5818 && IPV4_ADDR_SAME(&vpn->mcast_grp, &mcast_grp)
5819 && vpn->tenant_vrf_id == tenant_vrf_id
5820 && vpn->svi_ifindex == svi_ifindex)
5821 /* Probably some other param has changed that we don't
5822 * care about. */
5823 return 0;
5824
5825 bgp_evpn_mcast_grp_change(bgp, vpn, mcast_grp);
5826
5827 if (vpn->svi_ifindex != svi_ifindex) {
5828
5829 /*
5830 * Unresolve all the gateway IP nexthops for this VNI
5831 * for old SVI
5832 */
5833 bgp_evpn_remote_ip_hash_iterate(
5834 vpn,
5835 (void (*)(struct hash_bucket *, void *))
5836 bgp_evpn_remote_ip_hash_unlink_nexthop,
5837 vpn);
5838 bgp_evpn_unlink_from_vni_svi_hash(bgp, vpn);
5839 vpn->svi_ifindex = svi_ifindex;
5840 bgp_evpn_link_to_vni_svi_hash(bgp, vpn);
5841
5842 /*
5843 * Resolve all the gateway IP nexthops for this VNI
5844 * for new SVI
5845 */
5846 bgp_evpn_remote_ip_hash_iterate(
5847 vpn,
5848 (void (*)(struct hash_bucket *, void *))
5849 bgp_evpn_remote_ip_hash_link_nexthop,
5850 vpn);
5851 }
5852
5853 /* Update tenant_vrf_id if it has changed. */
5854 if (vpn->tenant_vrf_id != tenant_vrf_id) {
5855
5856 /*
5857 * Unresolve all the gateway IP nexthops for this VNI
5858 * in old tenant vrf
5859 */
5860 bgp_evpn_remote_ip_hash_iterate(
5861 vpn,
5862 (void (*)(struct hash_bucket *, void *))
5863 bgp_evpn_remote_ip_hash_unlink_nexthop,
5864 vpn);
5865 bgpevpn_unlink_from_l3vni(vpn);
5866 vpn->tenant_vrf_id = tenant_vrf_id;
5867 bgpevpn_link_to_l3vni(vpn);
5868
5869 /*
5870 * Resolve all the gateway IP nexthops for this VNI
5871 * in new tenant vrf
5872 */
5873 bgp_evpn_remote_ip_hash_iterate(
5874 vpn,
5875 (void (*)(struct hash_bucket *, void *))
5876 bgp_evpn_remote_ip_hash_link_nexthop,
5877 vpn);
5878 }
5879
5880 /* If tunnel endpoint IP has changed, update (and delete prior
5881 * type-3 route, if needed.)
5882 */
5883 if (!IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip))
5884 handle_tunnel_ip_change(bgp, vpn, originator_ip);
5885
5886 /* Update all routes with new endpoint IP and/or export RT
5887 * for VRFs
5888 */
5889 if (is_vni_live(vpn))
5890 update_routes_for_vni(bgp, vpn);
5891 } else {
5892 /* Create or update as appropriate. */
5893 vpn = bgp_evpn_new(bgp, vni, originator_ip, tenant_vrf_id,
5894 mcast_grp, svi_ifindex);
5895 }
5896
5897 /* if the VNI is live already, there is nothing more to do */
5898 if (is_vni_live(vpn))
5899 return 0;
5900
5901 /* Mark as "live" */
5902 SET_FLAG(vpn->flags, VNI_FLAG_LIVE);
5903
5904 /* tunnel is now active, add tunnel-ip to db */
5905 bgp_tip_add(bgp, &originator_ip);
5906
5907 /* filter routes as nexthop database has changed */
5908 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
5909
5910 /*
5911 * Create EVPN type-3 route and schedule for processing.
5912 *
5913 * RT-3 only if doing head-end replication
5914 */
5915 if (bgp_evpn_vni_flood_mode_get(bgp, vpn)
5916 == VXLAN_FLOOD_HEAD_END_REPL) {
5917 build_evpn_type3_prefix(&p, vpn->originator_ip);
5918 if (update_evpn_route(bgp, vpn, &p, 0, 0, NULL)) {
5919 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
5920 "%u: Type3 route creation failure for VNI %u",
5921 bgp->vrf_id, vni);
5922 return -1;
5923 }
5924 }
5925
5926 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5927 * VNI,
5928 * install them.
5929 */
5930 install_routes_for_vni(bgp, vpn);
5931
5932 /* If we are advertising gateway mac-ip
5933 It needs to be conveyed again to zebra */
5934 bgp_zebra_advertise_gw_macip(bgp, vpn->advertise_gw_macip, vpn->vni);
5935
5936 /* advertise svi mac-ip knob to zebra */
5937 bgp_zebra_advertise_svi_macip(bgp, vpn->advertise_svi_macip, vpn->vni);
5938
5939 return 0;
5940 }
5941
5942 /*
5943 * Handle change in setting for BUM handling. The supported values
5944 * are head-end replication and dropping all BUM packets. Any change
5945 * should be registered with zebra. Also, if doing head-end replication,
5946 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5947 * to be dropped, the RT-3s must be withdrawn.
5948 */
5949 void bgp_evpn_flood_control_change(struct bgp *bgp)
5950 {
5951 zlog_info("L2VPN EVPN BUM handling is %s",
5952 bgp->vxlan_flood_ctrl == VXLAN_FLOOD_HEAD_END_REPL ?
5953 "Flooding" : "Flooding Disabled");
5954
5955 bgp_zebra_vxlan_flood_control(bgp, bgp->vxlan_flood_ctrl);
5956 if (bgp->vxlan_flood_ctrl == VXLAN_FLOOD_HEAD_END_REPL)
5957 hash_iterate(bgp->vnihash, create_advertise_type3, bgp);
5958 else if (bgp->vxlan_flood_ctrl == VXLAN_FLOOD_DISABLED)
5959 hash_iterate(bgp->vnihash, delete_withdraw_type3, bgp);
5960 }
5961
5962 /*
5963 * Cleanup EVPN information on disable - Need to delete and withdraw
5964 * EVPN routes from peers.
5965 */
5966 void bgp_evpn_cleanup_on_disable(struct bgp *bgp)
5967 {
5968 hash_iterate(bgp->vnihash, (void (*)(struct hash_bucket *,
5969 void *))cleanup_vni_on_disable,
5970 bgp);
5971 }
5972
5973 /*
5974 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5975 * BGP instance (default) is being freed.
5976 */
5977 void bgp_evpn_cleanup(struct bgp *bgp)
5978 {
5979 hash_iterate(bgp->vnihash,
5980 (void (*)(struct hash_bucket *, void *))free_vni_entry,
5981 bgp);
5982
5983 hash_clean(bgp->import_rt_hash, (void (*)(void *))hash_import_rt_free);
5984 hash_free(bgp->import_rt_hash);
5985 bgp->import_rt_hash = NULL;
5986
5987 hash_clean(bgp->vrf_import_rt_hash,
5988 (void (*)(void *))hash_vrf_import_rt_free);
5989 hash_free(bgp->vrf_import_rt_hash);
5990 bgp->vrf_import_rt_hash = NULL;
5991
5992 hash_clean(bgp->vni_svi_hash, (void (*)(void *))hash_evpn_free);
5993 hash_free(bgp->vni_svi_hash);
5994 bgp->vni_svi_hash = NULL;
5995 hash_free(bgp->vnihash);
5996 bgp->vnihash = NULL;
5997
5998 list_delete(&bgp->vrf_import_rtl);
5999 list_delete(&bgp->vrf_export_rtl);
6000 list_delete(&bgp->l2vnis);
6001 }
6002
6003 /*
6004 * Initialization for EVPN
6005 * Create
6006 * VNI hash table
6007 * hash for RT to VNI
6008 */
6009 void bgp_evpn_init(struct bgp *bgp)
6010 {
6011 bgp->vnihash =
6012 hash_create(vni_hash_key_make, vni_hash_cmp, "BGP VNI Hash");
6013 bgp->vni_svi_hash =
6014 hash_create(vni_svi_hash_key_make, vni_svi_hash_cmp,
6015 "BGP VNI hash based on SVI ifindex");
6016 bgp->import_rt_hash =
6017 hash_create(import_rt_hash_key_make, import_rt_hash_cmp,
6018 "BGP Import RT Hash");
6019 bgp->vrf_import_rt_hash =
6020 hash_create(vrf_import_rt_hash_key_make, vrf_import_rt_hash_cmp,
6021 "BGP VRF Import RT Hash");
6022 bgp->vrf_import_rtl = list_new();
6023 bgp->vrf_import_rtl->cmp =
6024 (int (*)(void *, void *))bgp_evpn_route_target_cmp;
6025 bgp->vrf_import_rtl->del = bgp_evpn_xxport_delete_ecomm;
6026 bgp->vrf_export_rtl = list_new();
6027 bgp->vrf_export_rtl->cmp =
6028 (int (*)(void *, void *))bgp_evpn_route_target_cmp;
6029 bgp->vrf_export_rtl->del = bgp_evpn_xxport_delete_ecomm;
6030 bgp->l2vnis = list_new();
6031 bgp->l2vnis->cmp = vni_list_cmp;
6032 /* By default Duplicate Address Dection is enabled.
6033 * Max-moves (N) 5, detection time (M) 180
6034 * default action is warning-only
6035 * freeze action permanently freezes address,
6036 * and freeze time (auto-recovery) is disabled.
6037 */
6038 if (bgp->evpn_info) {
6039 bgp->evpn_info->dup_addr_detect = true;
6040 bgp->evpn_info->dad_time = EVPN_DAD_DEFAULT_TIME;
6041 bgp->evpn_info->dad_max_moves = EVPN_DAD_DEFAULT_MAX_MOVES;
6042 bgp->evpn_info->dad_freeze = false;
6043 bgp->evpn_info->dad_freeze_time = 0;
6044 /* Initialize zebra vxlan */
6045 bgp_zebra_dup_addr_detection(bgp);
6046 /* Enable PIP feature by default for bgp vrf instance */
6047 if (bgp->inst_type == BGP_INSTANCE_TYPE_VRF) {
6048 struct bgp *bgp_default;
6049
6050 bgp->evpn_info->advertise_pip = true;
6051 bgp_default = bgp_get_default();
6052 if (bgp_default)
6053 bgp->evpn_info->pip_ip = bgp_default->router_id;
6054 }
6055 }
6056
6057 /* Default BUM handling is to do head-end replication. */
6058 bgp->vxlan_flood_ctrl = VXLAN_FLOOD_HEAD_END_REPL;
6059
6060 bgp_evpn_nh_init(bgp);
6061 }
6062
6063 void bgp_evpn_vrf_delete(struct bgp *bgp_vrf)
6064 {
6065 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
6066 bgp_evpn_nh_finish(bgp_vrf);
6067 }
6068
6069 /*
6070 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
6071 */
6072 int bgp_evpn_get_type5_prefixlen(const struct prefix *pfx)
6073 {
6074 struct prefix_evpn *evp = (struct prefix_evpn *)pfx;
6075
6076 if (!pfx || pfx->family != AF_EVPN)
6077 return 0;
6078
6079 if (evp->prefix.route_type != BGP_EVPN_IP_PREFIX_ROUTE)
6080 return 0;
6081
6082 return evp->prefix.prefix_addr.ip_prefix_length;
6083 }
6084
6085 /*
6086 * Should we register nexthop for this EVPN prefix for nexthop tracking?
6087 */
6088 bool bgp_evpn_is_prefix_nht_supported(const struct prefix *pfx)
6089 {
6090 struct prefix_evpn *evp = (struct prefix_evpn *)pfx;
6091
6092 /*
6093 * EVPN routes should be marked as valid only if the nexthop is
6094 * reachable. Only if this happens, the route should be imported
6095 * (into VNI or VRF routing tables) and/or advertised.
6096 * Note: This is currently applied for EVPN type-1, type-2,
6097 * type-3, type-4 and type-5 routes.
6098 * It may be tweaked later on for other routes, or
6099 * even removed completely when all routes are handled.
6100 */
6101 if (pfx && pfx->family == AF_EVPN
6102 && (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
6103 || evp->prefix.route_type == BGP_EVPN_AD_ROUTE
6104 || evp->prefix.route_type == BGP_EVPN_ES_ROUTE
6105 || evp->prefix.route_type == BGP_EVPN_IMET_ROUTE
6106 || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
6107 return true;
6108
6109 return false;
6110 }
6111
6112 static void *bgp_evpn_remote_ip_hash_alloc(void *p)
6113 {
6114 const struct evpn_remote_ip *key = (const struct evpn_remote_ip *)p;
6115 struct evpn_remote_ip *ip;
6116
6117 ip = XMALLOC(MTYPE_EVPN_REMOTE_IP, sizeof(struct evpn_remote_ip));
6118 *ip = *key;
6119 ip->macip_path_list = list_new();
6120
6121 return ip;
6122 }
6123
6124 static unsigned int bgp_evpn_remote_ip_hash_key_make(const void *p)
6125 {
6126 const struct evpn_remote_ip *ip = p;
6127 const struct ipaddr *addr = &ip->addr;
6128
6129 if (IS_IPADDR_V4(addr))
6130 return jhash_1word(addr->ipaddr_v4.s_addr, 0);
6131
6132 return jhash2(addr->ipaddr_v6.s6_addr32,
6133 array_size(addr->ipaddr_v6.s6_addr32), 0);
6134 }
6135
6136 static bool bgp_evpn_remote_ip_hash_cmp(const void *p1, const void *p2)
6137 {
6138 const struct evpn_remote_ip *ip1 = p1;
6139 const struct evpn_remote_ip *ip2 = p2;
6140
6141 return !ipaddr_cmp(&ip1->addr, &ip2->addr);
6142 }
6143
6144 static void bgp_evpn_remote_ip_hash_init(struct bgpevpn *vpn)
6145 {
6146 if (!evpn_resolve_overlay_index())
6147 return;
6148
6149 vpn->remote_ip_hash = hash_create(bgp_evpn_remote_ip_hash_key_make,
6150 bgp_evpn_remote_ip_hash_cmp,
6151 "BGP EVPN remote IP hash");
6152 }
6153
6154 static void bgp_evpn_remote_ip_hash_free(struct hash_bucket *bucket, void *args)
6155 {
6156 struct evpn_remote_ip *ip = (struct evpn_remote_ip *)bucket->data;
6157 struct bgpevpn *vpn = (struct bgpevpn *)args;
6158
6159 bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, false);
6160
6161 list_delete(&ip->macip_path_list);
6162
6163 hash_release(vpn->remote_ip_hash, ip);
6164 XFREE(MTYPE_EVPN_REMOTE_IP, ip);
6165 }
6166
6167 static void bgp_evpn_remote_ip_hash_destroy(struct bgpevpn *vpn)
6168 {
6169 if (!evpn_resolve_overlay_index() || vpn->remote_ip_hash == NULL)
6170 return;
6171
6172 hash_iterate(vpn->remote_ip_hash,
6173 (void (*)(struct hash_bucket *, void *))bgp_evpn_remote_ip_hash_free,
6174 vpn);
6175
6176 hash_free(vpn->remote_ip_hash);
6177 vpn->remote_ip_hash = NULL;
6178 }
6179
6180 /* Add a remote MAC/IP route to hash table */
6181 static void bgp_evpn_remote_ip_hash_add(struct bgpevpn *vpn,
6182 struct bgp_path_info *pi)
6183 {
6184 struct evpn_remote_ip tmp;
6185 struct evpn_remote_ip *ip;
6186 struct prefix_evpn *evp;
6187
6188 if (!evpn_resolve_overlay_index())
6189 return;
6190
6191 if (pi->type != ZEBRA_ROUTE_BGP || pi->sub_type != BGP_ROUTE_IMPORTED
6192 || !CHECK_FLAG(pi->flags, BGP_PATH_VALID))
6193 return;
6194
6195 evp = (struct prefix_evpn *)&pi->net->p;
6196
6197 if (evp->family != AF_EVPN
6198 || evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE
6199 || is_evpn_prefix_ipaddr_none(evp))
6200 return;
6201
6202 tmp.addr = evp->prefix.macip_addr.ip;
6203 ip = hash_lookup(vpn->remote_ip_hash, &tmp);
6204 if (ip) {
6205 if (listnode_lookup(ip->macip_path_list, pi) != NULL)
6206 return;
6207 (void)listnode_add(ip->macip_path_list, pi);
6208 return;
6209 }
6210
6211 ip = hash_get(vpn->remote_ip_hash, &tmp, bgp_evpn_remote_ip_hash_alloc);
6212 (void)listnode_add(ip->macip_path_list, pi);
6213
6214 bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, true);
6215 }
6216
6217 /* Delete a remote MAC/IP route from hash table */
6218 static void bgp_evpn_remote_ip_hash_del(struct bgpevpn *vpn,
6219 struct bgp_path_info *pi)
6220 {
6221 struct evpn_remote_ip tmp;
6222 struct evpn_remote_ip *ip;
6223 struct prefix_evpn *evp;
6224
6225 if (!evpn_resolve_overlay_index())
6226 return;
6227
6228 evp = (struct prefix_evpn *)&pi->net->p;
6229
6230 if (evp->family != AF_EVPN
6231 || evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE
6232 || is_evpn_prefix_ipaddr_none(evp))
6233 return;
6234
6235 tmp.addr = evp->prefix.macip_addr.ip;
6236 ip = hash_lookup(vpn->remote_ip_hash, &tmp);
6237 if (ip == NULL)
6238 return;
6239
6240 listnode_delete(ip->macip_path_list, pi);
6241
6242 if (ip->macip_path_list->count == 0) {
6243 bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, false);
6244 hash_release(vpn->remote_ip_hash, ip);
6245 XFREE(MTYPE_EVPN_REMOTE_IP, ip);
6246 }
6247 }
6248
6249 static void bgp_evpn_remote_ip_hash_iterate(struct bgpevpn *vpn,
6250 void (*func)(struct hash_bucket *,
6251 void *),
6252 void *arg)
6253 {
6254 if (!evpn_resolve_overlay_index())
6255 return;
6256
6257 hash_iterate(vpn->remote_ip_hash, func, arg);
6258 }
6259
6260 static void show_remote_ip_entry(struct hash_bucket *bucket, void *args)
6261 {
6262 char buf[INET6_ADDRSTRLEN];
6263 struct listnode *node = NULL;
6264 struct bgp_path_info *pi = NULL;
6265 struct vty *vty = (struct vty *)args;
6266 struct evpn_remote_ip *ip = (struct evpn_remote_ip *)bucket->data;
6267
6268 vty_out(vty, " Remote IP: %s\n",
6269 ipaddr2str(&ip->addr, buf, sizeof(buf)));
6270 vty_out(vty, " Linked MAC/IP routes:\n");
6271 for (ALL_LIST_ELEMENTS_RO(ip->macip_path_list, node, pi))
6272 vty_out(vty, " %pFX\n", &pi->net->p);
6273 }
6274
6275 void bgp_evpn_show_remote_ip_hash(struct hash_bucket *bucket, void *args)
6276 {
6277 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
6278 struct vty *vty = (struct vty *)args;
6279
6280 vty_out(vty, "VNI: %u\n", vpn->vni);
6281 bgp_evpn_remote_ip_hash_iterate(
6282 vpn,
6283 (void (*)(struct hash_bucket *, void *))show_remote_ip_entry,
6284 vty);
6285 vty_out(vty, "\n");
6286 }
6287
6288 static void bgp_evpn_remote_ip_hash_link_nexthop(struct hash_bucket *bucket,
6289 void *args)
6290 {
6291 struct evpn_remote_ip *ip = (struct evpn_remote_ip *)bucket->data;
6292 struct bgpevpn *vpn = (struct bgpevpn *)args;
6293
6294 bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, true);
6295 }
6296
6297 static void bgp_evpn_remote_ip_hash_unlink_nexthop(struct hash_bucket *bucket,
6298 void *args)
6299 {
6300 struct evpn_remote_ip *ip = (struct evpn_remote_ip *)bucket->data;
6301 struct bgpevpn *vpn = (struct bgpevpn *)args;
6302
6303 bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, false);
6304 }
6305
6306 static unsigned int vni_svi_hash_key_make(const void *p)
6307 {
6308 const struct bgpevpn *vpn = p;
6309
6310 return jhash_1word(vpn->svi_ifindex, 0);
6311 }
6312
6313 static bool vni_svi_hash_cmp(const void *p1, const void *p2)
6314 {
6315 const struct bgpevpn *vpn1 = p1;
6316 const struct bgpevpn *vpn2 = p2;
6317
6318 return (vpn1->svi_ifindex == vpn2->svi_ifindex);
6319 }
6320
6321 static struct bgpevpn *bgp_evpn_vni_svi_hash_lookup(struct bgp *bgp,
6322 ifindex_t svi)
6323 {
6324 struct bgpevpn *vpn;
6325 struct bgpevpn tmp;
6326
6327 memset(&tmp, 0, sizeof(tmp));
6328 tmp.svi_ifindex = svi;
6329 vpn = hash_lookup(bgp->vni_svi_hash, &tmp);
6330 return vpn;
6331 }
6332
6333 static void bgp_evpn_link_to_vni_svi_hash(struct bgp *bgp, struct bgpevpn *vpn)
6334 {
6335 if (vpn->svi_ifindex == 0)
6336 return;
6337
6338 (void)hash_get(bgp->vni_svi_hash, vpn, hash_alloc_intern);
6339 }
6340
6341 static void bgp_evpn_unlink_from_vni_svi_hash(struct bgp *bgp,
6342 struct bgpevpn *vpn)
6343 {
6344 if (vpn->svi_ifindex == 0)
6345 return;
6346
6347 hash_release(bgp->vni_svi_hash, vpn);
6348 }
6349
6350 void bgp_evpn_show_vni_svi_hash(struct hash_bucket *bucket, void *args)
6351 {
6352 struct bgpevpn *evpn = (struct bgpevpn *)bucket->data;
6353 struct vty *vty = (struct vty *)args;
6354
6355 vty_out(vty, "SVI: %u VNI: %u\n", evpn->svi_ifindex, evpn->vni);
6356 }
6357
6358 /*
6359 * This function is called for a bgp_nexthop_cache entry when the nexthop is
6360 * gateway IP overlay index.
6361 * This function returns true if there is a remote MAC/IP route for the gateway
6362 * IP in the EVI of the nexthop SVI.
6363 */
6364 bool bgp_evpn_is_gateway_ip_resolved(struct bgp_nexthop_cache *bnc)
6365 {
6366 struct bgp *bgp_evpn = NULL;
6367 struct bgpevpn *vpn = NULL;
6368 struct evpn_remote_ip tmp;
6369 struct prefix *p;
6370
6371 if (!evpn_resolve_overlay_index())
6372 return false;
6373
6374 if (!bnc->nexthop || bnc->nexthop->ifindex == 0)
6375 return false;
6376
6377 bgp_evpn = bgp_get_evpn();
6378 if (!bgp_evpn)
6379 return false;
6380
6381 /*
6382 * Gateway IP is resolved by nht over SVI interface.
6383 * Use this SVI to find corresponding EVI(L2 context)
6384 */
6385 vpn = bgp_evpn_vni_svi_hash_lookup(bgp_evpn, bnc->nexthop->ifindex);
6386 if (!vpn)
6387 return false;
6388
6389 if (vpn->bgp_vrf != bnc->bgp)
6390 return false;
6391
6392 /*
6393 * Check if the gateway IP is present in the EVI remote_ip_hash table
6394 * which stores all the remote IP addresses received via MAC/IP routes
6395 * in this EVI
6396 */
6397 memset(&tmp, 0, sizeof(tmp));
6398
6399 p = &bnc->prefix;
6400 if (p->family == AF_INET) {
6401 tmp.addr.ipa_type = IPADDR_V4;
6402 memcpy(&(tmp.addr.ipaddr_v4), &(p->u.prefix4),
6403 sizeof(struct in_addr));
6404 } else if (p->family == AF_INET6) {
6405 tmp.addr.ipa_type = IPADDR_V6;
6406 memcpy(&(tmp.addr.ipaddr_v6), &(p->u.prefix6),
6407 sizeof(struct in6_addr));
6408 } else
6409 return false;
6410
6411 if (hash_lookup(vpn->remote_ip_hash, &tmp) == NULL)
6412 return false;
6413
6414 return true;
6415 }
6416
6417 /* Resolve/Unresolve nexthops when a MAC/IP route is added/deleted */
6418 static void bgp_evpn_remote_ip_process_nexthops(struct bgpevpn *vpn,
6419 struct ipaddr *addr,
6420 bool resolve)
6421 {
6422 afi_t afi;
6423 struct prefix p;
6424 struct bgp_nexthop_cache *bnc;
6425 struct bgp_nexthop_cache_head *tree = NULL;
6426
6427 if (!vpn->bgp_vrf || vpn->svi_ifindex == 0)
6428 return;
6429
6430 memset(&p, 0, sizeof(p));
6431
6432 if (addr->ipa_type == IPADDR_V4) {
6433 afi = AFI_IP;
6434 p.family = AF_INET;
6435 memcpy(&(p.u.prefix4), &(addr->ipaddr_v4),
6436 sizeof(struct in_addr));
6437 p.prefixlen = IPV4_MAX_BITLEN;
6438 } else if (addr->ipa_type == IPADDR_V6) {
6439 afi = AFI_IP6;
6440 p.family = AF_INET6;
6441 memcpy(&(p.u.prefix6), &(addr->ipaddr_v6),
6442 sizeof(struct in6_addr));
6443 p.prefixlen = IPV6_MAX_BITLEN;
6444 } else
6445 return;
6446
6447 tree = &vpn->bgp_vrf->nexthop_cache_table[afi];
6448 bnc = bnc_find(tree, &p, 0, 0);
6449
6450 if (!bnc || !bnc->is_evpn_gwip_nexthop)
6451 return;
6452
6453 if (!bnc->nexthop || bnc->nexthop->ifindex != vpn->svi_ifindex)
6454 return;
6455
6456 if (BGP_DEBUG(nht, NHT))
6457 zlog_debug("%s(%u): vni %u mac/ip %s for NH %pFX",
6458 vpn->bgp_vrf->name_pretty, vpn->tenant_vrf_id,
6459 vpn->vni, (resolve ? "add" : "delete"),
6460 &bnc->prefix);
6461
6462 /*
6463 * MAC/IP route or SVI or tenant vrf being added to EVI.
6464 * Set nexthop as valid only if it is already L3 reachable
6465 */
6466 if (resolve && bnc->flags & BGP_NEXTHOP_EVPN_INCOMPLETE) {
6467 bnc->flags &= ~BGP_NEXTHOP_EVPN_INCOMPLETE;
6468 bnc->flags |= BGP_NEXTHOP_VALID;
6469 bnc->change_flags |= BGP_NEXTHOP_MACIP_CHANGED;
6470 evaluate_paths(bnc);
6471 }
6472
6473 /* MAC/IP route or SVI or tenant vrf being deleted from EVI */
6474 if (!resolve && bnc->flags & BGP_NEXTHOP_VALID) {
6475 bnc->flags &= ~BGP_NEXTHOP_VALID;
6476 bnc->flags |= BGP_NEXTHOP_EVPN_INCOMPLETE;
6477 bnc->change_flags |= BGP_NEXTHOP_MACIP_CHANGED;
6478 evaluate_paths(bnc);
6479 }
6480 }
6481
6482 void bgp_evpn_handle_resolve_overlay_index_set(struct hash_bucket *bucket,
6483 void *arg)
6484 {
6485 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
6486 struct bgp_dest *dest;
6487 struct bgp_path_info *pi;
6488
6489 bgp_evpn_remote_ip_hash_init(vpn);
6490
6491 for (dest = bgp_table_top(vpn->route_table); dest;
6492 dest = bgp_route_next(dest))
6493 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
6494 bgp_evpn_remote_ip_hash_add(vpn, pi);
6495 }
6496
6497 void bgp_evpn_handle_resolve_overlay_index_unset(struct hash_bucket *bucket,
6498 void *arg)
6499 {
6500 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
6501
6502 bgp_evpn_remote_ip_hash_destroy(vpn);
6503 }