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