]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_evpn.c
Merge pull request #11284 from ordex/prefixlen_count
[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 if (bgp_debug_zebra(NULL)) {
1757 char buf3[ESI_STR_LEN];
1758
1759 zlog_debug(
1760 "VRF %s vni %u type-2 route evp %pFX RMAC %pEA nexthop %pI4 esi %s",
1761 vpn->bgp_vrf ? vrf_id_to_name(vpn->bgp_vrf->vrf_id)
1762 : " ",
1763 vpn->vni, p, &attr.rmac, &attr.mp_nexthop_global_in,
1764 esi_to_str(esi, buf3, sizeof(buf3)));
1765 }
1766 /* router mac is only needed for type-2 routes here. */
1767 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
1768 uint8_t af_flags = 0;
1769
1770 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_SVI_IP))
1771 SET_FLAG(af_flags, BGP_EVPN_MACIP_TYPE_SVI_IP);
1772
1773 bgp_evpn_get_rmac_nexthop(vpn, p, &attr, af_flags);
1774 }
1775
1776 vni2label(vpn->vni, &(attr.label));
1777
1778 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1779 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1780 * these routes.
1781 */
1782 add_l3_ecomm = bgp_evpn_route_add_l3_ecomm_ok(
1783 vpn, p, (attr.es_flags & ATTR_ES_IS_LOCAL) ? &attr.esi : NULL);
1784
1785 /* Set up extended community. */
1786 build_evpn_route_extcomm(vpn, &attr, add_l3_ecomm);
1787
1788 /* First, create (or fetch) route node within the VNI. */
1789 /* NOTE: There is no RD here. */
1790 dest = bgp_node_get(vpn->route_table, (struct prefix *)p);
1791
1792 /* Create or update route entry. */
1793 route_change = update_evpn_route_entry(bgp, vpn, afi, safi, dest, &attr,
1794 1, &pi, flags, seq,
1795 true /* setup_sync */, &old_is_sync);
1796 assert(pi);
1797 attr_new = pi->attr;
1798
1799 /* lock ri to prevent freeing in evpn_route_select_install */
1800 bgp_path_info_lock(pi);
1801
1802 /* Perform route selection. Normally, the local route in the
1803 * VNI is expected to win and be the best route. However, if
1804 * there is a race condition where a host moved from local to
1805 * remote and the remote route was received in BGP just prior
1806 * to the local MACIP notification from zebra, the remote
1807 * route would win, and we should evict the defunct local route
1808 * and (re)install the remote route into zebra.
1809 */
1810 evpn_route_select_install(bgp, vpn, dest);
1811 /*
1812 * If the new local route was not selected evict it and tell zebra
1813 * to re-add the best remote dest. BGP doesn't retain non-best local
1814 * routes.
1815 */
1816 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
1817 route_change = 0;
1818 } else {
1819 if (!CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
1820 route_change = 0;
1821 evpn_cleanup_local_non_best_route(bgp, vpn, dest, pi);
1822 } else {
1823 bool new_is_sync;
1824
1825 /* If the local path already existed and is still the
1826 * best path we need to also check if it transitioned
1827 * from being a sync path to a non-sync path. If it
1828 * it did we need to notify zebra that the sync-path
1829 * has been removed.
1830 */
1831 new_is_sync = bgp_evpn_attr_is_sync(pi->attr);
1832 if (!new_is_sync && old_is_sync)
1833 evpn_zebra_uninstall(bgp, vpn, p, zero_vtep_ip);
1834 }
1835 }
1836 bgp_path_info_unlock(pi);
1837
1838 bgp_dest_unlock_node(dest);
1839
1840 /* If this is a new route or some attribute has changed, export the
1841 * route to the global table. The route will be advertised to peers
1842 * from there. Note that this table is a 2-level tree (RD-level +
1843 * Prefix-level) similar to L3VPN routes.
1844 */
1845 if (route_change) {
1846 struct bgp_path_info *global_pi;
1847
1848 dest = bgp_global_evpn_node_get(bgp->rib[afi][safi], afi, safi,
1849 (const struct prefix_evpn *)p,
1850 &vpn->prd);
1851 update_evpn_route_entry(bgp, vpn, afi, safi, dest, attr_new, 1,
1852 &global_pi, flags, seq,
1853 false /* setup_sync */, NULL /* old_is_sync */);
1854
1855 /* Schedule for processing and unlock node. */
1856 bgp_process(bgp, dest, afi, safi);
1857 bgp_dest_unlock_node(dest);
1858 }
1859
1860 /* Unintern temporary. */
1861 aspath_unintern(&attr.aspath);
1862
1863 return 0;
1864 }
1865
1866 /*
1867 * Delete EVPN route entry.
1868 * The entry can be in ESI/VNI table or the global table.
1869 */
1870 void delete_evpn_route_entry(struct bgp *bgp, afi_t afi, safi_t safi,
1871 struct bgp_dest *dest,
1872 struct bgp_path_info **pi)
1873 {
1874 struct bgp_path_info *tmp_pi;
1875
1876 *pi = NULL;
1877
1878 /* Now, find matching route. */
1879 for (tmp_pi = bgp_dest_get_bgp_path_info(dest); tmp_pi;
1880 tmp_pi = tmp_pi->next)
1881 if (tmp_pi->peer == bgp->peer_self
1882 && tmp_pi->type == ZEBRA_ROUTE_BGP
1883 && tmp_pi->sub_type == BGP_ROUTE_STATIC)
1884 break;
1885
1886 *pi = tmp_pi;
1887
1888 /* Mark route for delete. */
1889 if (tmp_pi)
1890 bgp_path_info_delete(dest, tmp_pi);
1891 }
1892
1893 /* Delete EVPN type5 route */
1894 static int delete_evpn_type5_route(struct bgp *bgp_vrf, struct prefix_evpn *evp)
1895 {
1896 afi_t afi = AFI_L2VPN;
1897 safi_t safi = SAFI_EVPN;
1898 struct bgp_dest *dest = NULL;
1899 struct bgp_path_info *pi = NULL;
1900 struct bgp *bgp_evpn = NULL; /* evpn bgp instance */
1901
1902 bgp_evpn = bgp_get_evpn();
1903 if (!bgp_evpn)
1904 return 0;
1905
1906 /* locate the global route entry for this type-5 prefix */
1907 dest = bgp_global_evpn_node_lookup(bgp_evpn->rib[afi][safi], afi, safi,
1908 (const struct prefix_evpn *)evp, &bgp_vrf->vrf_prd);
1909 if (!dest)
1910 return 0;
1911
1912 delete_evpn_route_entry(bgp_evpn, afi, safi, dest, &pi);
1913 if (pi)
1914 bgp_process(bgp_evpn, dest, afi, safi);
1915 bgp_dest_unlock_node(dest);
1916 return 0;
1917 }
1918
1919 /*
1920 * Delete EVPN route (of type based on prefix) for specified VNI and
1921 * schedule for processing.
1922 */
1923 static int delete_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
1924 struct prefix_evpn *p)
1925 {
1926 struct bgp_dest *dest, *global_dest;
1927 struct bgp_path_info *pi;
1928 afi_t afi = AFI_L2VPN;
1929 safi_t safi = SAFI_EVPN;
1930
1931 /* First, locate the route node within the VNI. If it doesn't exist,
1932 * there
1933 * is nothing further to do.
1934 */
1935 /* NOTE: There is no RD here. */
1936 dest = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
1937 if (!dest)
1938 return 0;
1939
1940 /* Next, locate route node in the global EVPN routing table. Note that
1941 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1942 * L3VPN routes.
1943 */
1944 global_dest = bgp_global_evpn_node_lookup(bgp->rib[afi][safi], afi, safi,
1945 (const struct prefix_evpn *)p, &vpn->prd);
1946 if (global_dest) {
1947 /* Delete route entry in the global EVPN table. */
1948 delete_evpn_route_entry(bgp, afi, safi, global_dest, &pi);
1949
1950 /* Schedule for processing - withdraws to peers happen from
1951 * this table.
1952 */
1953 if (pi)
1954 bgp_process(bgp, global_dest, afi, safi);
1955 bgp_dest_unlock_node(global_dest);
1956 }
1957
1958 /* Delete route entry in the VNI route table. This can just be removed.
1959 */
1960 delete_evpn_route_entry(bgp, afi, safi, dest, &pi);
1961 if (pi) {
1962 bgp_path_info_reap(dest, pi);
1963 evpn_route_select_install(bgp, vpn, dest);
1964 }
1965 bgp_dest_unlock_node(dest);
1966
1967 return 0;
1968 }
1969
1970 void bgp_evpn_update_type2_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
1971 struct bgp_dest *dest,
1972 struct bgp_path_info *local_pi,
1973 const char *caller)
1974 {
1975 afi_t afi = AFI_L2VPN;
1976 safi_t safi = SAFI_EVPN;
1977 struct bgp_path_info *pi;
1978 struct attr attr;
1979 struct attr *attr_new;
1980 uint32_t seq;
1981 int add_l3_ecomm = 0;
1982 struct bgp_dest *global_dest;
1983 struct bgp_path_info *global_pi;
1984 struct prefix_evpn *evp =
1985 (struct prefix_evpn *)bgp_dest_get_prefix(dest);
1986 int route_change;
1987 bool old_is_sync = false;
1988
1989 if (CHECK_FLAG(local_pi->flags, BGP_PATH_REMOVED))
1990 return;
1991
1992 /*
1993 * Build attribute per local route as the MAC mobility and
1994 * some other values could differ for different routes. The
1995 * attributes will be shared in the hash table.
1996 */
1997 bgp_attr_default_set(&attr, bgp, BGP_ORIGIN_IGP);
1998 attr.nexthop = vpn->originator_ip;
1999 attr.mp_nexthop_global_in = vpn->originator_ip;
2000 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
2001 attr.sticky = (local_pi->attr->sticky) ? 1 : 0;
2002 attr.router_flag = (local_pi->attr->router_flag) ? 1 : 0;
2003 attr.es_flags = local_pi->attr->es_flags;
2004 if (local_pi->attr->default_gw) {
2005 attr.default_gw = 1;
2006 if (is_evpn_prefix_ipaddr_v6(evp))
2007 attr.router_flag = 1;
2008 }
2009 memcpy(&attr.esi, &local_pi->attr->esi, sizeof(esi_t));
2010 bgp_evpn_get_rmac_nexthop(vpn, evp, &attr,
2011 local_pi->extra->af_flags);
2012 vni2label(vpn->vni, &(attr.label));
2013 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
2014 * using L3 VNI for type-2 routes also.
2015 */
2016 add_l3_ecomm = bgp_evpn_route_add_l3_ecomm_ok(
2017 vpn, evp,
2018 (attr.es_flags & ATTR_ES_IS_LOCAL) ? &attr.esi : NULL);
2019
2020 /* Set up extended community. */
2021 build_evpn_route_extcomm(vpn, &attr, add_l3_ecomm);
2022 seq = mac_mobility_seqnum(local_pi->attr);
2023
2024 if (bgp_debug_zebra(NULL)) {
2025 char buf3[ESI_STR_LEN];
2026
2027 zlog_debug(
2028 "VRF %s vni %u evp %pFX RMAC %pEA nexthop %pI4 esi %s esf 0x%x from %s",
2029 vpn->bgp_vrf ? vrf_id_to_name(vpn->bgp_vrf->vrf_id)
2030 : " ",
2031 vpn->vni, evp, &attr.rmac, &attr.mp_nexthop_global_in,
2032 esi_to_str(&attr.esi, buf3, sizeof(buf3)),
2033 attr.es_flags, caller);
2034 }
2035
2036 /* Update the route entry. */
2037 route_change = update_evpn_route_entry(
2038 bgp, vpn, afi, safi, dest, &attr, 0, &pi, 0, seq,
2039 true /* setup_sync */, &old_is_sync);
2040
2041 assert(pi);
2042 attr_new = pi->attr;
2043 /* lock ri to prevent freeing in evpn_route_select_install */
2044 bgp_path_info_lock(pi);
2045
2046 /* Perform route selection. Normally, the local route in the
2047 * VNI is expected to win and be the best route. However,
2048 * under peculiar situations (e.g., tunnel (next hop) IP change
2049 * that causes best selection to be based on next hop), a
2050 * remote route could win. If the local route is the best,
2051 * ensure it is updated in the global EVPN route table and
2052 * advertised to peers; otherwise, ensure it is evicted and
2053 * (re)install the remote route into zebra.
2054 */
2055 evpn_route_select_install(bgp, vpn, dest);
2056
2057 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
2058 route_change = 0;
2059 } else {
2060 if (!CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
2061 route_change = 0;
2062 evpn_cleanup_local_non_best_route(bgp, vpn, dest, pi);
2063 } else {
2064 bool new_is_sync;
2065
2066 /* If the local path already existed and is still the
2067 * best path we need to also check if it transitioned
2068 * from being a sync path to a non-sync path. If it
2069 * it did we need to notify zebra that the sync-path
2070 * has been removed.
2071 */
2072 new_is_sync = bgp_evpn_attr_is_sync(pi->attr);
2073 if (!new_is_sync && old_is_sync)
2074 evpn_zebra_uninstall(bgp, vpn,
2075 evp, zero_vtep_ip);
2076 }
2077 }
2078
2079
2080 /* unlock pi */
2081 bgp_path_info_unlock(pi);
2082
2083 if (route_change) {
2084 /* Update route in global routing table. */
2085 global_dest = bgp_global_evpn_node_get(bgp->rib[afi][safi], afi,
2086 safi, evp, &vpn->prd);
2087 assert(global_dest);
2088 update_evpn_route_entry(
2089 bgp, vpn, afi, safi, global_dest, attr_new, 0,
2090 &global_pi, 0, mac_mobility_seqnum(attr_new),
2091 false /* setup_sync */, NULL /* old_is_sync */);
2092
2093 /* Schedule for processing and unlock node. */
2094 bgp_process(bgp, global_dest, afi, safi);
2095 bgp_dest_unlock_node(global_dest);
2096 }
2097
2098 /* Unintern temporary. */
2099 aspath_unintern(&attr.aspath);
2100 }
2101
2102 /*
2103 * Update all type-2 (MACIP) local routes for this VNI - these should also
2104 * be scheduled for advertise to peers.
2105 */
2106 static int update_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
2107 {
2108 struct bgp_dest *dest;
2109 struct bgp_path_info *tmp_pi;
2110
2111 /* Walk this VNI's route table and update local type-2 routes. For any
2112 * routes updated, update corresponding entry in the global table too.
2113 */
2114 for (dest = bgp_table_top(vpn->route_table); dest;
2115 dest = bgp_route_next(dest)) {
2116 const struct prefix_evpn *evp =
2117 (const struct prefix_evpn *)bgp_dest_get_prefix(dest);
2118
2119 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2120 continue;
2121
2122 /* Identify local route. */
2123 for (tmp_pi = bgp_dest_get_bgp_path_info(dest); tmp_pi;
2124 tmp_pi = tmp_pi->next) {
2125 if (tmp_pi->peer == bgp->peer_self
2126 && tmp_pi->type == ZEBRA_ROUTE_BGP
2127 && tmp_pi->sub_type == BGP_ROUTE_STATIC)
2128 break;
2129 }
2130
2131 if (!tmp_pi)
2132 continue;
2133
2134 bgp_evpn_update_type2_route_entry(bgp, vpn, dest, tmp_pi,
2135 __func__);
2136 }
2137
2138 return 0;
2139 }
2140
2141 /*
2142 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2143 * global routing table. These are also scheduled for withdraw from peers.
2144 */
2145 static void delete_global_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
2146 {
2147 afi_t afi;
2148 safi_t safi;
2149 struct bgp_dest *rddest, *dest;
2150 struct bgp_table *table;
2151 struct bgp_path_info *pi;
2152
2153 afi = AFI_L2VPN;
2154 safi = SAFI_EVPN;
2155
2156 rddest = bgp_node_lookup(bgp->rib[afi][safi],
2157 (struct prefix *)&vpn->prd);
2158 if (rddest) {
2159 table = bgp_dest_get_bgp_table_info(rddest);
2160 for (dest = bgp_table_top(table); dest;
2161 dest = bgp_route_next(dest)) {
2162 const struct prefix_evpn *evp =
2163 (const struct prefix_evpn *)bgp_dest_get_prefix(
2164 dest);
2165
2166 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2167 continue;
2168
2169 delete_evpn_route_entry(bgp, afi, safi, dest, &pi);
2170 if (pi)
2171 bgp_process(bgp, dest, afi, safi);
2172 }
2173
2174 /* Unlock RD node. */
2175 bgp_dest_unlock_node(rddest);
2176 }
2177 }
2178
2179 /*
2180 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2181 * table as well as the per-VNI route table.
2182 */
2183 static int delete_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
2184 {
2185 afi_t afi;
2186 safi_t safi;
2187 struct bgp_dest *dest;
2188 struct bgp_path_info *pi;
2189
2190 afi = AFI_L2VPN;
2191 safi = SAFI_EVPN;
2192
2193 /* First, walk the global route table for this VNI's type-2 local
2194 * routes.
2195 * EVPN routes are a 2-level table, first get the RD table.
2196 */
2197 delete_global_type2_routes(bgp, vpn);
2198
2199 /* Next, walk this VNI's route table and delete local type-2 routes. */
2200 for (dest = bgp_table_top(vpn->route_table); dest;
2201 dest = bgp_route_next(dest)) {
2202 const struct prefix_evpn *evp =
2203 (const struct prefix_evpn *)bgp_dest_get_prefix(dest);
2204
2205 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2206 continue;
2207
2208 delete_evpn_route_entry(bgp, afi, safi, dest, &pi);
2209
2210 /* Route entry in local table gets deleted immediately. */
2211 if (pi)
2212 bgp_path_info_reap(dest, pi);
2213 }
2214
2215 return 0;
2216 }
2217
2218 /*
2219 * Delete all routes in the per-VNI route table.
2220 */
2221 static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
2222 {
2223 struct bgp_dest *dest;
2224 struct bgp_path_info *pi, *nextpi;
2225
2226 /* Walk this VNI's route table and delete all routes. */
2227 for (dest = bgp_table_top(vpn->route_table); dest;
2228 dest = bgp_route_next(dest)) {
2229 for (pi = bgp_dest_get_bgp_path_info(dest);
2230 (pi != NULL) && (nextpi = pi->next, 1); pi = nextpi) {
2231 bgp_evpn_remote_ip_hash_del(vpn, pi);
2232 bgp_path_info_delete(dest, pi);
2233 bgp_path_info_reap(dest, pi);
2234 }
2235 }
2236
2237 return 0;
2238 }
2239
2240 /* BUM traffic flood mode per-l2-vni */
2241 static int bgp_evpn_vni_flood_mode_get(struct bgp *bgp,
2242 struct bgpevpn *vpn)
2243 {
2244 /* if flooding has been globally disabled per-vni mode is
2245 * not relevant
2246 */
2247 if (bgp->vxlan_flood_ctrl == VXLAN_FLOOD_DISABLED)
2248 return VXLAN_FLOOD_DISABLED;
2249
2250 /* if mcast group ip has been specified we use a PIM-SM MDT */
2251 if (vpn->mcast_grp.s_addr != INADDR_ANY)
2252 return VXLAN_FLOOD_PIM_SM;
2253
2254 /* default is ingress replication */
2255 return VXLAN_FLOOD_HEAD_END_REPL;
2256 }
2257
2258 /*
2259 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2260 * export RT getting modified or change to tunnel IP. Note that these
2261 * situations need the route in the per-VNI table as well as the global
2262 * table to be updated (as attributes change).
2263 */
2264 int update_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
2265 {
2266 int ret;
2267 struct prefix_evpn p;
2268
2269 update_type1_routes_for_evi(bgp, vpn);
2270
2271 /* Update and advertise the type-3 route (only one) followed by the
2272 * locally learnt type-2 routes (MACIP) - for this VNI.
2273 *
2274 * RT-3 only if doing head-end replication
2275 */
2276 if (bgp_evpn_vni_flood_mode_get(bgp, vpn)
2277 == VXLAN_FLOOD_HEAD_END_REPL) {
2278 build_evpn_type3_prefix(&p, vpn->originator_ip);
2279 ret = update_evpn_route(bgp, vpn, &p, 0, 0, NULL);
2280 if (ret)
2281 return ret;
2282 }
2283
2284 return update_all_type2_routes(bgp, vpn);
2285 }
2286
2287 /*
2288 * Delete (and withdraw) local routes for specified VNI from the global
2289 * table and per-VNI table. After this, remove all other routes from
2290 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2291 * (advertise-all-vni) being disabled.
2292 */
2293 static int delete_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
2294 {
2295 int ret;
2296 struct prefix_evpn p;
2297
2298 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2299 * followed by type-3 routes (only one) - for this VNI.
2300 */
2301 ret = delete_all_type2_routes(bgp, vpn);
2302 if (ret)
2303 return ret;
2304
2305 build_evpn_type3_prefix(&p, vpn->originator_ip);
2306 ret = delete_evpn_route(bgp, vpn, &p);
2307 if (ret)
2308 return ret;
2309
2310 /* Delete all routes from the per-VNI table. */
2311 return delete_all_vni_routes(bgp, vpn);
2312 }
2313
2314 /*
2315 * There is a flood mcast IP address change. Update the mcast-grp and
2316 * remove the type-3 route if any. A new type-3 route will be generated
2317 * post tunnel_ip update if the new flood mode is head-end-replication.
2318 */
2319 static int bgp_evpn_mcast_grp_change(struct bgp *bgp, struct bgpevpn *vpn,
2320 struct in_addr mcast_grp)
2321 {
2322 struct prefix_evpn p;
2323
2324 vpn->mcast_grp = mcast_grp;
2325
2326 if (is_vni_live(vpn)) {
2327 build_evpn_type3_prefix(&p, vpn->originator_ip);
2328 delete_evpn_route(bgp, vpn, &p);
2329 }
2330
2331 return 0;
2332 }
2333
2334 /*
2335 * There is a tunnel endpoint IP address change for this VNI, delete
2336 * prior type-3 route (if needed) and update.
2337 * Note: Route re-advertisement happens elsewhere after other processing
2338 * other changes.
2339 */
2340 static int handle_tunnel_ip_change(struct bgp *bgp, struct bgpevpn *vpn,
2341 struct in_addr originator_ip)
2342 {
2343 struct prefix_evpn p;
2344
2345 /* If VNI is not live, we only need to update the originator ip */
2346 if (!is_vni_live(vpn)) {
2347 vpn->originator_ip = originator_ip;
2348 return 0;
2349 }
2350
2351 /* Update the tunnel-ip hash */
2352 bgp_tip_del(bgp, &vpn->originator_ip);
2353 bgp_tip_add(bgp, &originator_ip);
2354
2355 /* filter routes as martian nexthop db has changed */
2356 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
2357
2358 /* Need to withdraw type-3 route as the originator IP is part
2359 * of the key.
2360 */
2361 build_evpn_type3_prefix(&p, vpn->originator_ip);
2362 delete_evpn_route(bgp, vpn, &p);
2363
2364 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2365 vpn->originator_ip = originator_ip;
2366 return 0;
2367 }
2368
2369 static struct bgp_path_info *
2370 bgp_create_evpn_bgp_path_info(struct bgp_path_info *parent_pi,
2371 struct bgp_dest *dest, struct attr *attr)
2372 {
2373 struct attr *attr_new;
2374 struct bgp_path_info *pi;
2375
2376 /* Add (or update) attribute to hash. */
2377 attr_new = bgp_attr_intern(attr);
2378
2379 /* Create new route with its attribute. */
2380 pi = info_make(parent_pi->type, BGP_ROUTE_IMPORTED, 0, parent_pi->peer,
2381 attr_new, dest);
2382 SET_FLAG(pi->flags, BGP_PATH_VALID);
2383 bgp_path_info_extra_get(pi);
2384 pi->extra->parent = bgp_path_info_lock(parent_pi);
2385 bgp_dest_lock_node((struct bgp_dest *)parent_pi->net);
2386 if (parent_pi->extra) {
2387 memcpy(&pi->extra->label, &parent_pi->extra->label,
2388 sizeof(pi->extra->label));
2389 pi->extra->num_labels = parent_pi->extra->num_labels;
2390 pi->extra->igpmetric = parent_pi->extra->igpmetric;
2391 }
2392 bgp_path_info_add(dest, pi);
2393
2394 return pi;
2395 }
2396
2397 /*
2398 * Install route entry into the VRF routing table and invoke route selection.
2399 */
2400 static int install_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
2401 const struct prefix_evpn *evp,
2402 struct bgp_path_info *parent_pi)
2403 {
2404 struct bgp_dest *dest;
2405 struct bgp_path_info *pi;
2406 struct attr attr;
2407 struct attr *attr_new;
2408 int ret = 0;
2409 struct prefix p;
2410 struct prefix *pp = &p;
2411 afi_t afi = 0;
2412 safi_t safi = 0;
2413 bool new_pi = false;
2414 bool use_l3nhg = false;
2415 bool is_l3nhg_active = false;
2416 char buf1[INET6_ADDRSTRLEN];
2417
2418 memset(pp, 0, sizeof(struct prefix));
2419 ip_prefix_from_evpn_prefix(evp, pp);
2420
2421 if (bgp_debug_zebra(NULL))
2422 zlog_debug(
2423 "vrf %s: import evpn prefix %pFX parent %p flags 0x%x",
2424 vrf_id_to_name(bgp_vrf->vrf_id), evp, parent_pi,
2425 parent_pi->flags);
2426
2427 /* Create (or fetch) route within the VRF. */
2428 /* NOTE: There is no RD here. */
2429 if (is_evpn_prefix_ipaddr_v4(evp)) {
2430 afi = AFI_IP;
2431 safi = SAFI_UNICAST;
2432 dest = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
2433 } else if (is_evpn_prefix_ipaddr_v6(evp)) {
2434 afi = AFI_IP6;
2435 safi = SAFI_UNICAST;
2436 dest = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
2437 } else
2438 return 0;
2439
2440 /* EVPN routes currently only support a IPv4 next hop which corresponds
2441 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
2442 * or prefix route, we have to convert the next hop to an IPv4-mapped
2443 * address for the rest of the code to flow through. In the case of IPv4,
2444 * make sure to set the flag for next hop attribute.
2445 */
2446 attr = *parent_pi->attr;
2447 if (attr.evpn_overlay.type != OVERLAY_INDEX_GATEWAY_IP) {
2448 if (afi == AFI_IP6)
2449 evpn_convert_nexthop_to_ipv6(&attr);
2450 else
2451 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP);
2452 } else {
2453
2454 /*
2455 * If gateway IP overlay index is specified in the NLRI of
2456 * EVPN RT-5, this gateway IP should be used as the nexthop
2457 * for the prefix in the VRF
2458 */
2459 if (bgp_debug_zebra(NULL)) {
2460 zlog_debug(
2461 "Install gateway IP %s as nexthop for prefix %pFX in vrf %s",
2462 inet_ntop(pp->family, &attr.evpn_overlay.gw_ip,
2463 buf1, sizeof(buf1)), pp,
2464 vrf_id_to_name(bgp_vrf->vrf_id));
2465 }
2466
2467 if (afi == AFI_IP6) {
2468 memcpy(&attr.mp_nexthop_global,
2469 &attr.evpn_overlay.gw_ip.ipaddr_v6,
2470 sizeof(struct in6_addr));
2471 attr.mp_nexthop_len = IPV6_MAX_BYTELEN;
2472 } else {
2473 attr.nexthop = attr.evpn_overlay.gw_ip.ipaddr_v4;
2474 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP);
2475 }
2476 }
2477
2478 bgp_evpn_es_vrf_use_nhg(bgp_vrf, &parent_pi->attr->esi, &use_l3nhg,
2479 &is_l3nhg_active, NULL);
2480 if (use_l3nhg)
2481 attr.es_flags |= ATTR_ES_L3_NHG_USE;
2482 if (is_l3nhg_active)
2483 attr.es_flags |= ATTR_ES_L3_NHG_ACTIVE;
2484
2485 /* Check if route entry is already present. */
2486 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
2487 if (pi->extra
2488 && (struct bgp_path_info *)pi->extra->parent == parent_pi)
2489 break;
2490
2491 if (!pi) {
2492 pi = bgp_create_evpn_bgp_path_info(parent_pi, dest, &attr);
2493 new_pi = true;
2494 } else {
2495 if (attrhash_cmp(pi->attr, &attr)
2496 && !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
2497 bgp_dest_unlock_node(dest);
2498 return 0;
2499 }
2500 /* The attribute has changed. */
2501 /* Add (or update) attribute to hash. */
2502 attr_new = bgp_attr_intern(&attr);
2503
2504 /* Restore route, if needed. */
2505 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
2506 bgp_path_info_restore(dest, pi);
2507
2508 /* Mark if nexthop has changed. */
2509 if ((afi == AFI_IP
2510 && !IPV4_ADDR_SAME(&pi->attr->nexthop, &attr_new->nexthop))
2511 || (afi == AFI_IP6
2512 && !IPV6_ADDR_SAME(&pi->attr->mp_nexthop_global,
2513 &attr_new->mp_nexthop_global)))
2514 SET_FLAG(pi->flags, BGP_PATH_IGP_CHANGED);
2515
2516 bgp_path_info_set_flag(dest, pi, BGP_PATH_ATTR_CHANGED);
2517 /* Unintern existing, set to new. */
2518 bgp_attr_unintern(&pi->attr);
2519 pi->attr = attr_new;
2520 pi->uptime = bgp_clock();
2521 }
2522
2523 /* Gateway IP nexthop should be resolved */
2524 if (attr.evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP) {
2525 if (bgp_find_or_add_nexthop(bgp_vrf, bgp_vrf, afi, safi, pi,
2526 NULL, 0, NULL))
2527 bgp_path_info_set_flag(dest, pi, BGP_PATH_VALID);
2528 else {
2529 if (BGP_DEBUG(nht, NHT)) {
2530 inet_ntop(pp->family,
2531 &attr.evpn_overlay.gw_ip,
2532 buf1, sizeof(buf1));
2533 zlog_debug("%s: gateway IP NH unresolved",
2534 buf1);
2535 }
2536 bgp_path_info_unset_flag(dest, pi, BGP_PATH_VALID);
2537 }
2538 } else {
2539
2540 /* as it is an importation, change nexthop */
2541 bgp_path_info_set_flag(dest, pi, BGP_PATH_ANNC_NH_SELF);
2542 }
2543
2544 /* Link path to evpn nexthop */
2545 bgp_evpn_path_nh_add(bgp_vrf, pi);
2546
2547 bgp_aggregate_increment(bgp_vrf, bgp_dest_get_prefix(dest), pi, afi,
2548 safi);
2549
2550 /* Perform route selection and update zebra, if required. */
2551 bgp_process(bgp_vrf, dest, afi, safi);
2552
2553 /* Process for route leaking. */
2554 vpn_leak_from_vrf_update(bgp_get_default(), bgp_vrf, pi);
2555
2556 bgp_dest_unlock_node(dest);
2557
2558 if (bgp_debug_zebra(NULL))
2559 zlog_debug("... %s pi dest %p (l %d) pi %p (l %d, f 0x%x)",
2560 new_pi ? "new" : "update", dest,
2561 bgp_dest_get_lock_count(dest), pi, pi->lock,
2562 pi->flags);
2563
2564 return ret;
2565 }
2566
2567 /*
2568 * Install route entry into the VNI routing table and invoke route selection.
2569 */
2570 static int install_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
2571 const struct prefix_evpn *p,
2572 struct bgp_path_info *parent_pi)
2573 {
2574 struct bgp_dest *dest;
2575 struct bgp_path_info *pi;
2576 struct bgp_path_info *local_pi;
2577 struct attr *attr_new;
2578 int ret;
2579 struct prefix_evpn ad_evp;
2580 bool old_local_es = false;
2581 bool new_local_es;
2582
2583 /* EAD prefix in the global table doesn't include the VTEP-IP so
2584 * we need to create a different copy for the VNI
2585 */
2586 if (p->prefix.route_type == BGP_EVPN_AD_ROUTE)
2587 p = evpn_type1_prefix_vni_copy(&ad_evp, p,
2588 parent_pi->attr->nexthop);
2589
2590 /* Create (or fetch) route within the VNI. */
2591 /* NOTE: There is no RD here. */
2592 dest = bgp_node_get(vpn->route_table, (struct prefix *)p);
2593
2594 /* Check if route entry is already present. */
2595 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
2596 if (pi->extra
2597 && (struct bgp_path_info *)pi->extra->parent == parent_pi)
2598 break;
2599
2600 if (!pi) {
2601 /* Create an info */
2602 pi = bgp_create_evpn_bgp_path_info(parent_pi, dest,
2603 parent_pi->attr);
2604 new_local_es = bgp_evpn_attr_is_local_es(pi->attr);
2605 } else {
2606 if (attrhash_cmp(pi->attr, parent_pi->attr)
2607 && !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
2608 bgp_dest_unlock_node(dest);
2609 return 0;
2610 }
2611 /* The attribute has changed. */
2612 /* Add (or update) attribute to hash. */
2613 attr_new = bgp_attr_intern(parent_pi->attr);
2614
2615 /* Restore route, if needed. */
2616 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
2617 bgp_path_info_restore(dest, pi);
2618
2619 /* Mark if nexthop has changed. */
2620 if (!IPV4_ADDR_SAME(&pi->attr->nexthop, &attr_new->nexthop))
2621 SET_FLAG(pi->flags, BGP_PATH_IGP_CHANGED);
2622
2623 old_local_es = bgp_evpn_attr_is_local_es(pi->attr);
2624 new_local_es = bgp_evpn_attr_is_local_es(attr_new);
2625 /* If ESI is different or if its type has changed we
2626 * need to reinstall the path in zebra
2627 */
2628 if ((old_local_es != new_local_es)
2629 || memcmp(&pi->attr->esi, &attr_new->esi,
2630 sizeof(attr_new->esi))) {
2631
2632 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
2633 zlog_debug("VNI %d path %pFX chg to %s es",
2634 vpn->vni, &pi->net->p,
2635 new_local_es ? "local"
2636 : "non-local");
2637 bgp_path_info_set_flag(dest, pi, BGP_PATH_ATTR_CHANGED);
2638 }
2639
2640 /* Unintern existing, set to new. */
2641 bgp_attr_unintern(&pi->attr);
2642 pi->attr = attr_new;
2643 pi->uptime = bgp_clock();
2644 }
2645
2646 /* Add this route to remote IP hashtable */
2647 bgp_evpn_remote_ip_hash_add(vpn, pi);
2648
2649 /* Perform route selection and update zebra, if required. */
2650 ret = evpn_route_select_install(bgp, vpn, dest);
2651
2652 /* if the best path is a local path with a non-zero ES
2653 * sync info against the local path may need to be updated
2654 * when a remote path is added/updated (including changes
2655 * from sync-path to remote-path)
2656 */
2657 local_pi = bgp_evpn_route_get_local_path(bgp, dest);
2658 if (local_pi && (old_local_es || new_local_es))
2659 bgp_evpn_update_type2_route_entry(bgp, vpn, dest, local_pi,
2660 __func__);
2661 bgp_dest_unlock_node(dest);
2662
2663 return ret;
2664 }
2665
2666 /*
2667 * Uninstall route entry from the VRF routing table and send message
2668 * to zebra, if appropriate.
2669 */
2670 static int uninstall_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
2671 const struct prefix_evpn *evp,
2672 struct bgp_path_info *parent_pi)
2673 {
2674 struct bgp_dest *dest;
2675 struct bgp_path_info *pi;
2676 int ret = 0;
2677 struct prefix p;
2678 struct prefix *pp = &p;
2679 afi_t afi = 0;
2680 safi_t safi = 0;
2681
2682 memset(pp, 0, sizeof(struct prefix));
2683 ip_prefix_from_evpn_prefix(evp, pp);
2684
2685 if (bgp_debug_zebra(NULL))
2686 zlog_debug(
2687 "vrf %s: unimport evpn prefix %pFX parent %p flags 0x%x",
2688 vrf_id_to_name(bgp_vrf->vrf_id), evp, parent_pi,
2689 parent_pi->flags);
2690
2691 /* Locate route within the VRF. */
2692 /* NOTE: There is no RD here. */
2693 if (is_evpn_prefix_ipaddr_v4(evp)) {
2694 afi = AFI_IP;
2695 safi = SAFI_UNICAST;
2696 dest = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
2697 } else {
2698 afi = AFI_IP6;
2699 safi = SAFI_UNICAST;
2700 dest = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
2701 }
2702
2703 if (!dest)
2704 return 0;
2705
2706 /* Find matching route entry. */
2707 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
2708 if (pi->extra
2709 && (struct bgp_path_info *)pi->extra->parent == parent_pi)
2710 break;
2711
2712 if (!pi) {
2713 bgp_dest_unlock_node(dest);
2714 return 0;
2715 }
2716
2717 if (bgp_debug_zebra(NULL))
2718 zlog_debug("... delete dest %p (l %d) pi %p (l %d, f 0x%x)",
2719 dest, bgp_dest_get_lock_count(dest), pi, pi->lock,
2720 pi->flags);
2721
2722 /* Process for route leaking. */
2723 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp_vrf, pi);
2724
2725 bgp_aggregate_decrement(bgp_vrf, bgp_dest_get_prefix(dest), pi, afi,
2726 safi);
2727
2728 /* Mark entry for deletion */
2729 bgp_path_info_delete(dest, pi);
2730
2731 /* Unlink path to evpn nexthop */
2732 bgp_evpn_path_nh_del(bgp_vrf, pi);
2733
2734 /* Perform route selection and update zebra, if required. */
2735 bgp_process(bgp_vrf, dest, afi, safi);
2736
2737 /* Unlock route node. */
2738 bgp_dest_unlock_node(dest);
2739
2740 return ret;
2741 }
2742
2743 /*
2744 * Uninstall route entry from the VNI routing table and send message
2745 * to zebra, if appropriate.
2746 */
2747 static int uninstall_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
2748 const struct prefix_evpn *p,
2749 struct bgp_path_info *parent_pi)
2750 {
2751 struct bgp_dest *dest;
2752 struct bgp_path_info *pi;
2753 struct bgp_path_info *local_pi;
2754 int ret;
2755 struct prefix_evpn ad_evp;
2756
2757 /* EAD prefix in the global table doesn't include the VTEP-IP so
2758 * we need to create a different copy for the VNI
2759 */
2760 if (p->prefix.route_type == BGP_EVPN_AD_ROUTE)
2761 p = evpn_type1_prefix_vni_copy(&ad_evp, p,
2762 parent_pi->attr->nexthop);
2763
2764 /* Locate route within the VNI. */
2765 /* NOTE: There is no RD here. */
2766 dest = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
2767 if (!dest)
2768 return 0;
2769
2770 /* Find matching route entry. */
2771 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
2772 if (pi->extra
2773 && (struct bgp_path_info *)pi->extra->parent == parent_pi)
2774 break;
2775
2776 if (!pi) {
2777 bgp_dest_unlock_node(dest);
2778 return 0;
2779 }
2780
2781 bgp_evpn_remote_ip_hash_del(vpn, pi);
2782
2783 /* Mark entry for deletion */
2784 bgp_path_info_delete(dest, pi);
2785
2786 /* Perform route selection and update zebra, if required. */
2787 ret = evpn_route_select_install(bgp, vpn, dest);
2788
2789 /* if the best path is a local path with a non-zero ES
2790 * sync info against the local path may need to be updated
2791 * when a remote path is deleted
2792 */
2793 local_pi = bgp_evpn_route_get_local_path(bgp, dest);
2794 if (local_pi && bgp_evpn_attr_is_local_es(local_pi->attr))
2795 bgp_evpn_update_type2_route_entry(bgp, vpn, dest, local_pi,
2796 __func__);
2797
2798 /* Unlock route node. */
2799 bgp_dest_unlock_node(dest);
2800
2801 return ret;
2802 }
2803
2804 /*
2805 * Given a route entry and a VRF, see if this route entry should be
2806 * imported into the VRF i.e., RTs match.
2807 */
2808 static int is_route_matching_for_vrf(struct bgp *bgp_vrf,
2809 struct bgp_path_info *pi)
2810 {
2811 struct attr *attr = pi->attr;
2812 struct ecommunity *ecom;
2813 uint32_t i;
2814
2815 assert(attr);
2816 /* Route should have valid RT to be even considered. */
2817 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
2818 return 0;
2819
2820 ecom = bgp_attr_get_ecommunity(attr);
2821 if (!ecom || !ecom->size)
2822 return 0;
2823
2824 /* For each extended community RT, see if it matches this VNI. If any RT
2825 * matches, we're done.
2826 */
2827 for (i = 0; i < ecom->size; i++) {
2828 uint8_t *pnt;
2829 uint8_t type, sub_type;
2830 struct ecommunity_val *eval;
2831 struct ecommunity_val eval_tmp;
2832 struct vrf_irt_node *irt;
2833
2834 /* Only deal with RTs */
2835 pnt = (ecom->val + (i * ecom->unit_size));
2836 eval = (struct ecommunity_val *)(ecom->val
2837 + (i * ecom->unit_size));
2838 type = *pnt++;
2839 sub_type = *pnt++;
2840 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
2841 continue;
2842
2843 /* See if this RT matches specified VNIs import RTs */
2844 irt = lookup_vrf_import_rt(eval);
2845 if (irt)
2846 if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
2847 return 1;
2848
2849 /* Also check for non-exact match. In this, we mask out the AS
2850 * and
2851 * only check on the local-admin sub-field. This is to
2852 * facilitate using
2853 * VNI as the RT for EBGP peering too.
2854 */
2855 irt = NULL;
2856 if (type == ECOMMUNITY_ENCODE_AS
2857 || type == ECOMMUNITY_ENCODE_AS4
2858 || type == ECOMMUNITY_ENCODE_IP) {
2859 memcpy(&eval_tmp, eval, ecom->unit_size);
2860 mask_ecom_global_admin(&eval_tmp, eval);
2861 irt = lookup_vrf_import_rt(&eval_tmp);
2862 }
2863 if (irt)
2864 if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
2865 return 1;
2866 }
2867
2868 return 0;
2869 }
2870
2871 /*
2872 * Given a route entry and a VNI, see if this route entry should be
2873 * imported into the VNI i.e., RTs match.
2874 */
2875 static int is_route_matching_for_vni(struct bgp *bgp, struct bgpevpn *vpn,
2876 struct bgp_path_info *pi)
2877 {
2878 struct attr *attr = pi->attr;
2879 struct ecommunity *ecom;
2880 uint32_t i;
2881
2882 assert(attr);
2883 /* Route should have valid RT to be even considered. */
2884 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
2885 return 0;
2886
2887 ecom = bgp_attr_get_ecommunity(attr);
2888 if (!ecom || !ecom->size)
2889 return 0;
2890
2891 /* For each extended community RT, see if it matches this VNI. If any RT
2892 * matches, we're done.
2893 */
2894 for (i = 0; i < ecom->size; i++) {
2895 uint8_t *pnt;
2896 uint8_t type, sub_type;
2897 struct ecommunity_val *eval;
2898 struct ecommunity_val eval_tmp;
2899 struct irt_node *irt;
2900
2901 /* Only deal with RTs */
2902 pnt = (ecom->val + (i * ecom->unit_size));
2903 eval = (struct ecommunity_val *)(ecom->val
2904 + (i * ecom->unit_size));
2905 type = *pnt++;
2906 sub_type = *pnt++;
2907 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
2908 continue;
2909
2910 /* See if this RT matches specified VNIs import RTs */
2911 irt = lookup_import_rt(bgp, eval);
2912 if (irt)
2913 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
2914 return 1;
2915
2916 /* Also check for non-exact match. In this, we mask out the AS
2917 * and
2918 * only check on the local-admin sub-field. This is to
2919 * facilitate using
2920 * VNI as the RT for EBGP peering too.
2921 */
2922 irt = NULL;
2923 if (type == ECOMMUNITY_ENCODE_AS
2924 || type == ECOMMUNITY_ENCODE_AS4
2925 || type == ECOMMUNITY_ENCODE_IP) {
2926 memcpy(&eval_tmp, eval, ecom->unit_size);
2927 mask_ecom_global_admin(&eval_tmp, eval);
2928 irt = lookup_import_rt(bgp, &eval_tmp);
2929 }
2930 if (irt)
2931 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
2932 return 1;
2933 }
2934
2935 return 0;
2936 }
2937
2938 /* This API will scan evpn routes for checking attribute's rmac
2939 * macthes with bgp instance router mac. It avoid installing
2940 * route into bgp vrf table and remote rmac in bridge table.
2941 */
2942 static int bgp_evpn_route_rmac_self_check(struct bgp *bgp_vrf,
2943 const struct prefix_evpn *evp,
2944 struct bgp_path_info *pi)
2945 {
2946 /* evpn route could have learnt prior to L3vni has come up,
2947 * perform rmac check before installing route and
2948 * remote router mac.
2949 * The route will be removed from global bgp table once
2950 * SVI comes up with MAC and stored in hash, triggers
2951 * bgp_mac_rescan_all_evpn_tables.
2952 */
2953 if (memcmp(&bgp_vrf->rmac, &pi->attr->rmac, ETH_ALEN) == 0) {
2954 if (bgp_debug_update(pi->peer, NULL, NULL, 1)) {
2955 char attr_str[BUFSIZ] = {0};
2956
2957 bgp_dump_attr(pi->attr, attr_str, sizeof(attr_str));
2958
2959 zlog_debug(
2960 "%s: bgp %u prefix %pFX with attr %s - DENIED due to self mac",
2961 __func__, bgp_vrf->vrf_id, evp, attr_str);
2962 }
2963
2964 return 1;
2965 }
2966
2967 return 0;
2968 }
2969
2970 /* don't import hosts that are locally attached */
2971 static inline bool
2972 bgp_evpn_skip_vrf_import_of_local_es(struct bgp *bgp_vrf,
2973 const struct prefix_evpn *evp,
2974 struct bgp_path_info *pi, int install)
2975 {
2976 esi_t *esi;
2977
2978 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
2979 esi = bgp_evpn_attr_get_esi(pi->attr);
2980
2981 /* Don't import routes that point to a local destination */
2982 if (bgp_evpn_attr_is_local_es(pi->attr)) {
2983 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT)) {
2984 char esi_buf[ESI_STR_LEN];
2985
2986 zlog_debug(
2987 "vrf %s of evpn prefix %pFX skipped, local es %s",
2988 install ? "import" : "unimport", evp,
2989 esi_to_str(esi, esi_buf,
2990 sizeof(esi_buf)));
2991 }
2992 return true;
2993 }
2994 }
2995 return false;
2996 }
2997
2998 /*
2999 * Install or uninstall a mac-ip route in the provided vrf if
3000 * there is a rt match
3001 */
3002 int bgp_evpn_route_entry_install_if_vrf_match(struct bgp *bgp_vrf,
3003 struct bgp_path_info *pi,
3004 int install)
3005 {
3006 int ret = 0;
3007 const struct prefix_evpn *evp =
3008 (const struct prefix_evpn *)bgp_dest_get_prefix(pi->net);
3009
3010 /* Consider "valid" remote routes applicable for
3011 * this VRF.
3012 */
3013 if (!(CHECK_FLAG(pi->flags, BGP_PATH_VALID)
3014 && pi->type == ZEBRA_ROUTE_BGP
3015 && pi->sub_type == BGP_ROUTE_NORMAL))
3016 return 0;
3017
3018 if (is_route_matching_for_vrf(bgp_vrf, pi)) {
3019 if (bgp_evpn_route_rmac_self_check(bgp_vrf, evp, pi))
3020 return 0;
3021
3022 /* don't import hosts that are locally attached */
3023 if (install && bgp_evpn_skip_vrf_import_of_local_es(
3024 bgp_vrf, evp, pi, install))
3025 return 0;
3026
3027 if (install)
3028 ret = install_evpn_route_entry_in_vrf(bgp_vrf, evp, pi);
3029 else
3030 ret = uninstall_evpn_route_entry_in_vrf(bgp_vrf, evp,
3031 pi);
3032
3033 if (ret)
3034 flog_err(EC_BGP_EVPN_FAIL,
3035 "Failed to %s EVPN %pFX route in VRF %s",
3036 install ? "install" : "uninstall", evp,
3037 vrf_id_to_name(bgp_vrf->vrf_id));
3038 }
3039
3040 return ret;
3041 }
3042
3043 /*
3044 * Install or uninstall mac-ip routes are appropriate for this
3045 * particular VRF.
3046 */
3047 static int install_uninstall_routes_for_vrf(struct bgp *bgp_vrf, int install)
3048 {
3049 afi_t afi;
3050 safi_t safi;
3051 struct bgp_dest *rd_dest, *dest;
3052 struct bgp_table *table;
3053 struct bgp_path_info *pi;
3054 int ret;
3055 struct bgp *bgp_evpn = NULL;
3056
3057 afi = AFI_L2VPN;
3058 safi = SAFI_EVPN;
3059 bgp_evpn = bgp_get_evpn();
3060 if (!bgp_evpn)
3061 return -1;
3062
3063 /* Walk entire global routing table and evaluate routes which could be
3064 * imported into this VRF. Note that we need to loop through all global
3065 * routes to determine which route matches the import rt on vrf
3066 */
3067 for (rd_dest = bgp_table_top(bgp_evpn->rib[afi][safi]); rd_dest;
3068 rd_dest = bgp_route_next(rd_dest)) {
3069 table = bgp_dest_get_bgp_table_info(rd_dest);
3070 if (!table)
3071 continue;
3072
3073 for (dest = bgp_table_top(table); dest;
3074 dest = bgp_route_next(dest)) {
3075 const struct prefix_evpn *evp =
3076 (const struct prefix_evpn *)bgp_dest_get_prefix(
3077 dest);
3078
3079 /* if not mac-ip route skip this route */
3080 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3081 || evp->prefix.route_type
3082 == BGP_EVPN_IP_PREFIX_ROUTE))
3083 continue;
3084
3085 /* if not a mac+ip route skip this route */
3086 if (!(is_evpn_prefix_ipaddr_v4(evp)
3087 || is_evpn_prefix_ipaddr_v6(evp)))
3088 continue;
3089
3090 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
3091 pi = pi->next) {
3092 ret = bgp_evpn_route_entry_install_if_vrf_match(
3093 bgp_vrf, pi, install);
3094 if (ret)
3095 return ret;
3096 }
3097 }
3098 }
3099
3100 return 0;
3101 }
3102
3103 /*
3104 * Install or uninstall routes of specified type that are appropriate for this
3105 * particular VNI.
3106 */
3107 static int install_uninstall_routes_for_vni(struct bgp *bgp,
3108 struct bgpevpn *vpn,
3109 bgp_evpn_route_type rtype,
3110 int install)
3111 {
3112 afi_t afi;
3113 safi_t safi;
3114 struct bgp_dest *rd_dest, *dest;
3115 struct bgp_table *table;
3116 struct bgp_path_info *pi;
3117 int ret;
3118
3119 afi = AFI_L2VPN;
3120 safi = SAFI_EVPN;
3121
3122 /* Walk entire global routing table and evaluate routes which could be
3123 * imported into this VPN. Note that we cannot just look at the routes
3124 * for
3125 * the VNI's RD - remote routes applicable for this VNI could have any
3126 * RD.
3127 */
3128 /* EVPN routes are a 2-level table. */
3129 for (rd_dest = bgp_table_top(bgp->rib[afi][safi]); rd_dest;
3130 rd_dest = bgp_route_next(rd_dest)) {
3131 table = bgp_dest_get_bgp_table_info(rd_dest);
3132 if (!table)
3133 continue;
3134
3135 for (dest = bgp_table_top(table); dest;
3136 dest = bgp_route_next(dest)) {
3137 const struct prefix_evpn *evp =
3138 (const struct prefix_evpn *)bgp_dest_get_prefix(
3139 dest);
3140
3141 if (evp->prefix.route_type != rtype)
3142 continue;
3143
3144 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
3145 pi = pi->next) {
3146 /* Consider "valid" remote routes applicable for
3147 * this VNI. */
3148 if (!(CHECK_FLAG(pi->flags, BGP_PATH_VALID)
3149 && pi->type == ZEBRA_ROUTE_BGP
3150 && pi->sub_type == BGP_ROUTE_NORMAL))
3151 continue;
3152
3153 if (is_route_matching_for_vni(bgp, vpn, pi)) {
3154 if (install)
3155 ret = install_evpn_route_entry(
3156 bgp, vpn, evp, pi);
3157 else
3158 ret = uninstall_evpn_route_entry(
3159 bgp, vpn, evp, pi);
3160
3161 if (ret) {
3162 flog_err(
3163 EC_BGP_EVPN_FAIL,
3164 "%u: Failed to %s EVPN %s route in VNI %u",
3165 bgp->vrf_id,
3166 install ? "install"
3167 : "uninstall",
3168 rtype == BGP_EVPN_MAC_IP_ROUTE
3169 ? "MACIP"
3170 : "IMET",
3171 vpn->vni);
3172
3173 bgp_dest_unlock_node(rd_dest);
3174 bgp_dest_unlock_node(dest);
3175 return ret;
3176 }
3177 }
3178 }
3179 }
3180 }
3181
3182 return 0;
3183 }
3184
3185 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3186 * is invoked upon l3vni-add or l3vni import rt change
3187 */
3188 static int install_routes_for_vrf(struct bgp *bgp_vrf)
3189 {
3190 install_uninstall_routes_for_vrf(bgp_vrf, 1);
3191 return 0;
3192 }
3193
3194 /*
3195 * Install any existing remote routes applicable for this VNI into its
3196 * routing table. This is invoked when a VNI becomes "live" or its Import
3197 * RT is changed.
3198 */
3199 static int install_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
3200 {
3201 int ret;
3202
3203 /* Install type-3 routes followed by type-2 routes - the ones applicable
3204 * for this VNI.
3205 */
3206 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
3207 1);
3208 if (ret)
3209 return ret;
3210
3211 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_AD_ROUTE,
3212 1);
3213 if (ret)
3214 return ret;
3215
3216 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
3217 1);
3218 }
3219
3220 /* uninstall routes from l3vni vrf. */
3221 static int uninstall_routes_for_vrf(struct bgp *bgp_vrf)
3222 {
3223 install_uninstall_routes_for_vrf(bgp_vrf, 0);
3224 return 0;
3225 }
3226
3227 /*
3228 * Uninstall any existing remote routes for this VNI. One scenario in which
3229 * this is invoked is upon an import RT change.
3230 */
3231 static int uninstall_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
3232 {
3233 int ret;
3234
3235 /* Uninstall type-2 routes followed by type-3 routes - the ones
3236 * applicable
3237 * for this VNI.
3238 */
3239 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
3240 0);
3241 if (ret)
3242 return ret;
3243
3244 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_AD_ROUTE,
3245 0);
3246 if (ret)
3247 return ret;
3248
3249
3250 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
3251 0);
3252 }
3253
3254 /*
3255 * Install or uninstall route in matching VRFs (list).
3256 */
3257 static int install_uninstall_route_in_vrfs(struct bgp *bgp_def, afi_t afi,
3258 safi_t safi, struct prefix_evpn *evp,
3259 struct bgp_path_info *pi,
3260 struct list *vrfs, int install)
3261 {
3262 struct bgp *bgp_vrf;
3263 struct listnode *node, *nnode;
3264
3265 /* Only type-2/type-5 routes go into a VRF */
3266 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3267 || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
3268 return 0;
3269
3270 /* if it is type-2 route and not a mac+ip route skip this route */
3271 if ((evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
3272 && !(is_evpn_prefix_ipaddr_v4(evp)
3273 || is_evpn_prefix_ipaddr_v6(evp)))
3274 return 0;
3275
3276 for (ALL_LIST_ELEMENTS(vrfs, node, nnode, bgp_vrf)) {
3277 int ret;
3278
3279 /* don't import hosts that are locally attached */
3280 if (install && bgp_evpn_skip_vrf_import_of_local_es(
3281 bgp_vrf, evp, pi, install))
3282 return 0;
3283
3284 if (install)
3285 ret = install_evpn_route_entry_in_vrf(bgp_vrf, evp, pi);
3286 else
3287 ret = uninstall_evpn_route_entry_in_vrf(bgp_vrf, evp,
3288 pi);
3289
3290 if (ret) {
3291 flog_err(EC_BGP_EVPN_FAIL,
3292 "%u: Failed to %s prefix %pFX in VRF %s",
3293 bgp_def->vrf_id,
3294 install ? "install" : "uninstall", evp,
3295 vrf_id_to_name(bgp_vrf->vrf_id));
3296 return ret;
3297 }
3298 }
3299
3300 return 0;
3301 }
3302
3303 /*
3304 * Install or uninstall route in matching VNIs (list).
3305 */
3306 static int install_uninstall_route_in_vnis(struct bgp *bgp, afi_t afi,
3307 safi_t safi, struct prefix_evpn *evp,
3308 struct bgp_path_info *pi,
3309 struct list *vnis, int install)
3310 {
3311 struct bgpevpn *vpn;
3312 struct listnode *node, *nnode;
3313
3314 for (ALL_LIST_ELEMENTS(vnis, node, nnode, vpn)) {
3315 int ret;
3316
3317 if (!is_vni_live(vpn))
3318 continue;
3319
3320 if (install)
3321 ret = install_evpn_route_entry(bgp, vpn, evp, pi);
3322 else
3323 ret = uninstall_evpn_route_entry(bgp, vpn, evp, pi);
3324
3325 if (ret) {
3326 flog_err(EC_BGP_EVPN_FAIL,
3327 "%u: Failed to %s EVPN %s route in VNI %u",
3328 bgp->vrf_id, install ? "install" : "uninstall",
3329 evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3330 ? "MACIP"
3331 : "IMET",
3332 vpn->vni);
3333 return ret;
3334 }
3335 }
3336
3337 return 0;
3338 }
3339
3340 /*
3341 * Install or uninstall route for appropriate VNIs/ESIs.
3342 */
3343 static int bgp_evpn_install_uninstall_table(struct bgp *bgp, afi_t afi,
3344 safi_t safi, const struct prefix *p,
3345 struct bgp_path_info *pi,
3346 int import, bool in_vni_rt,
3347 bool in_vrf_rt)
3348 {
3349 struct prefix_evpn *evp = (struct prefix_evpn *)p;
3350 struct attr *attr = pi->attr;
3351 struct ecommunity *ecom;
3352 uint32_t i;
3353 struct prefix_evpn ad_evp;
3354
3355 assert(attr);
3356
3357 /* Only type-1, type-2, type-3, type-4 and type-5
3358 * are supported currently
3359 */
3360 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3361 || evp->prefix.route_type == BGP_EVPN_IMET_ROUTE
3362 || evp->prefix.route_type == BGP_EVPN_ES_ROUTE
3363 || evp->prefix.route_type == BGP_EVPN_AD_ROUTE
3364 || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
3365 return 0;
3366
3367 /* If we don't have Route Target, nothing much to do. */
3368 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
3369 return 0;
3370
3371 /* EAD prefix in the global table doesn't include the VTEP-IP so
3372 * we need to create a different copy for the VNI
3373 */
3374 if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE)
3375 evp = evpn_type1_prefix_vni_copy(&ad_evp, evp, attr->nexthop);
3376
3377 ecom = bgp_attr_get_ecommunity(attr);
3378 if (!ecom || !ecom->size)
3379 return -1;
3380
3381 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3382 * attached to the route */
3383 for (i = 0; i < ecom->size; i++) {
3384 uint8_t *pnt;
3385 uint8_t type, sub_type;
3386 struct ecommunity_val *eval;
3387 struct ecommunity_val eval_tmp;
3388 struct irt_node *irt; /* import rt for l2vni */
3389 struct vrf_irt_node *vrf_irt; /* import rt for l3vni */
3390 struct bgp_evpn_es *es;
3391
3392 /* Only deal with RTs */
3393 pnt = (ecom->val + (i * ecom->unit_size));
3394 eval = (struct ecommunity_val *)(ecom->val
3395 + (i * ecom->unit_size));
3396 type = *pnt++;
3397 sub_type = *pnt++;
3398 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
3399 continue;
3400
3401 /* non-local MAC-IP routes in the global route table are linked
3402 * to the destination ES
3403 */
3404 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
3405 bgp_evpn_path_es_link(pi, 0,
3406 bgp_evpn_attr_get_esi(pi->attr));
3407
3408 /*
3409 * macip routes (type-2) are imported into VNI and VRF tables.
3410 * IMET route is imported into VNI table.
3411 * prefix routes are imported into VRF table.
3412 */
3413 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE ||
3414 evp->prefix.route_type == BGP_EVPN_IMET_ROUTE ||
3415 evp->prefix.route_type == BGP_EVPN_AD_ROUTE ||
3416 evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE) {
3417
3418 irt = in_vni_rt ? lookup_import_rt(bgp, eval) : NULL;
3419 if (irt)
3420 install_uninstall_route_in_vnis(
3421 bgp, afi, safi, evp, pi, irt->vnis,
3422 import);
3423
3424 vrf_irt = in_vrf_rt ? lookup_vrf_import_rt(eval) : NULL;
3425 if (vrf_irt)
3426 install_uninstall_route_in_vrfs(
3427 bgp, afi, safi, evp, pi, vrf_irt->vrfs,
3428 import);
3429
3430 /* Also check for non-exact match.
3431 * In this, we mask out the AS and
3432 * only check on the local-admin sub-field.
3433 * This is to facilitate using
3434 * VNI as the RT for EBGP peering too.
3435 */
3436 irt = NULL;
3437 vrf_irt = NULL;
3438 if (type == ECOMMUNITY_ENCODE_AS
3439 || type == ECOMMUNITY_ENCODE_AS4
3440 || type == ECOMMUNITY_ENCODE_IP) {
3441 memcpy(&eval_tmp, eval, ecom->unit_size);
3442 mask_ecom_global_admin(&eval_tmp, eval);
3443 if (in_vni_rt)
3444 irt = lookup_import_rt(bgp, &eval_tmp);
3445 if (in_vrf_rt)
3446 vrf_irt =
3447 lookup_vrf_import_rt(&eval_tmp);
3448 }
3449
3450 if (irt)
3451 install_uninstall_route_in_vnis(
3452 bgp, afi, safi, evp, pi, irt->vnis,
3453 import);
3454 if (vrf_irt)
3455 install_uninstall_route_in_vrfs(
3456 bgp, afi, safi, evp, pi, vrf_irt->vrfs,
3457 import);
3458 }
3459
3460 /* es route is imported into the es table */
3461 if (evp->prefix.route_type == BGP_EVPN_ES_ROUTE) {
3462
3463 /* we will match based on the entire esi to avoid
3464 * import of an es route for esi2 into esi1
3465 */
3466 es = bgp_evpn_es_find(&evp->prefix.es_addr.esi);
3467 if (es && bgp_evpn_is_es_local(es))
3468 bgp_evpn_es_route_install_uninstall(
3469 bgp, es, afi, safi, evp, pi, import);
3470 }
3471 }
3472
3473 return 0;
3474 }
3475
3476 /*
3477 * Install or uninstall route for appropriate VNIs/ESIs.
3478 */
3479 static int install_uninstall_evpn_route(struct bgp *bgp, afi_t afi, safi_t safi,
3480 const struct prefix *p,
3481 struct bgp_path_info *pi, int import)
3482 {
3483 return bgp_evpn_install_uninstall_table(bgp, afi, safi, p, pi, import,
3484 true, true);
3485 }
3486
3487 void bgp_evpn_import_type2_route(struct bgp_path_info *pi, int import)
3488 {
3489 struct bgp *bgp_evpn;
3490
3491 bgp_evpn = bgp_get_evpn();
3492 if (!bgp_evpn)
3493 return;
3494
3495 install_uninstall_evpn_route(bgp_evpn, AFI_L2VPN, SAFI_EVPN,
3496 &pi->net->p, pi, import);
3497 }
3498
3499 /*
3500 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3501 * routes
3502 */
3503 static void delete_withdraw_vrf_routes(struct bgp *bgp_vrf)
3504 {
3505 /* Delete ipv4 default route and withdraw from peers */
3506 if (evpn_default_originate_set(bgp_vrf, AFI_IP, SAFI_UNICAST))
3507 bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP,
3508 SAFI_UNICAST, false);
3509
3510 /* delete all ipv4 routes and withdraw from peers */
3511 if (advertise_type5_routes(bgp_vrf, AFI_IP))
3512 bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);
3513
3514 /* Delete ipv6 default route and withdraw from peers */
3515 if (evpn_default_originate_set(bgp_vrf, AFI_IP6, SAFI_UNICAST))
3516 bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP6,
3517 SAFI_UNICAST, false);
3518
3519 /* delete all ipv6 routes and withdraw from peers */
3520 if (advertise_type5_routes(bgp_vrf, AFI_IP6))
3521 bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
3522 }
3523
3524 /*
3525 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3526 * routes
3527 */
3528 void update_advertise_vrf_routes(struct bgp *bgp_vrf)
3529 {
3530 struct bgp *bgp_evpn = NULL; /* EVPN bgp instance */
3531
3532 bgp_evpn = bgp_get_evpn();
3533 if (!bgp_evpn)
3534 return;
3535
3536 /* update all ipv4 routes */
3537 if (advertise_type5_routes(bgp_vrf, AFI_IP))
3538 bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);
3539
3540 /* update ipv4 default route and withdraw from peers */
3541 if (evpn_default_originate_set(bgp_vrf, AFI_IP, SAFI_UNICAST))
3542 bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP,
3543 SAFI_UNICAST, true);
3544
3545 /* update all ipv6 routes */
3546 if (advertise_type5_routes(bgp_vrf, AFI_IP6))
3547 bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
3548
3549 /* update ipv6 default route and withdraw from peers */
3550 if (evpn_default_originate_set(bgp_vrf, AFI_IP6, SAFI_UNICAST))
3551 bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP6,
3552 SAFI_UNICAST, true);
3553
3554 }
3555
3556 /*
3557 * update and advertise local routes for a VRF as type-5 routes.
3558 * This is invoked upon RD change for a VRF. Note taht the processing is only
3559 * done in the global route table using the routes which already exist in the
3560 * VRF routing table
3561 */
3562 static void update_router_id_vrf(struct bgp *bgp_vrf)
3563 {
3564 /* skip if the RD is configured */
3565 if (is_vrf_rd_configured(bgp_vrf))
3566 return;
3567
3568 /* derive the RD for the VRF based on new router-id */
3569 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
3570
3571 /* update advertise ipv4|ipv6 routes as type-5 routes */
3572 update_advertise_vrf_routes(bgp_vrf);
3573 }
3574
3575 /*
3576 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3577 * This is invoked upon VRF RD change. The processing is done only from global
3578 * table.
3579 */
3580 static void withdraw_router_id_vrf(struct bgp *bgp_vrf)
3581 {
3582 /* skip if the RD is configured */
3583 if (is_vrf_rd_configured(bgp_vrf))
3584 return;
3585
3586 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3587 delete_withdraw_vrf_routes(bgp_vrf);
3588 }
3589
3590 /*
3591 * Update and advertise local routes for a VNI. Invoked upon router-id
3592 * change. Note that the processing is done only on the global route table
3593 * using routes that already exist in the per-VNI table.
3594 */
3595 static int update_advertise_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
3596 {
3597 struct prefix_evpn p;
3598 struct bgp_dest *dest, *global_dest;
3599 struct bgp_path_info *pi, *global_pi;
3600 struct attr *attr;
3601 afi_t afi = AFI_L2VPN;
3602 safi_t safi = SAFI_EVPN;
3603
3604 /* Locate type-3 route for VNI in the per-VNI table and use its
3605 * attributes to create and advertise the type-3 route for this VNI
3606 * in the global table.
3607 *
3608 * RT-3 only if doing head-end replication
3609 */
3610 if (bgp_evpn_vni_flood_mode_get(bgp, vpn)
3611 == VXLAN_FLOOD_HEAD_END_REPL) {
3612 build_evpn_type3_prefix(&p, vpn->originator_ip);
3613 dest = bgp_node_lookup(vpn->route_table, (struct prefix *)&p);
3614 if (!dest) /* unexpected */
3615 return 0;
3616 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
3617 if (pi->peer == bgp->peer_self &&
3618 pi->type == ZEBRA_ROUTE_BGP
3619 && pi->sub_type == BGP_ROUTE_STATIC)
3620 break;
3621 if (!pi) /* unexpected */
3622 return 0;
3623 attr = pi->attr;
3624
3625 global_dest = bgp_global_evpn_node_get(bgp->rib[afi][safi],
3626 afi, safi, &p, &vpn->prd);
3627 update_evpn_route_entry(bgp, vpn, afi, safi, global_dest, attr,
3628 1, &pi, 0, mac_mobility_seqnum(attr),
3629 false /* setup_sync */, NULL /* old_is_sync */);
3630
3631 /* Schedule for processing and unlock node. */
3632 bgp_process(bgp, global_dest, afi, safi);
3633 bgp_dest_unlock_node(global_dest);
3634 }
3635
3636 /* Now, walk this VNI's route table and use the route and its attribute
3637 * to create and schedule route in global table.
3638 */
3639 for (dest = bgp_table_top(vpn->route_table); dest;
3640 dest = bgp_route_next(dest)) {
3641 const struct prefix_evpn *evp =
3642 (const struct prefix_evpn *)bgp_dest_get_prefix(dest);
3643
3644 /*
3645 * We have already processed type-3 routes.
3646 * Process only type-1 and type-2 routes here.
3647 */
3648 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE
3649 && evp->prefix.route_type != BGP_EVPN_AD_ROUTE)
3650 continue;
3651
3652 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
3653 if (pi->peer == bgp->peer_self
3654 && pi->type == ZEBRA_ROUTE_BGP
3655 && pi->sub_type == BGP_ROUTE_STATIC)
3656 break;
3657 if (!pi)
3658 continue;
3659
3660 /* Create route in global routing table using this route entry's
3661 * attribute.
3662 */
3663 attr = pi->attr;
3664 global_dest = bgp_global_evpn_node_get(bgp->rib[afi][safi], afi, safi,
3665 evp, &vpn->prd);
3666 assert(global_dest);
3667
3668 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
3669 /* Type-2 route */
3670 update_evpn_route_entry(
3671 bgp, vpn, afi, safi, global_dest, attr, 1,
3672 &global_pi, 0, mac_mobility_seqnum(attr),
3673 false /* setup_sync */, NULL /* old_is_sync */);
3674 } else {
3675 /* Type-1 route */
3676 struct bgp_evpn_es *es;
3677 int route_changed = 0;
3678
3679 es = bgp_evpn_es_find(&evp->prefix.ead_addr.esi);
3680 bgp_evpn_mh_route_update(bgp, es, vpn, afi, safi,
3681 global_dest, attr, &global_pi,
3682 &route_changed);
3683 }
3684
3685 /* Schedule for processing and unlock node. */
3686 bgp_process(bgp, global_dest, afi, safi);
3687 bgp_dest_unlock_node(global_dest);
3688 }
3689
3690 return 0;
3691 }
3692
3693 /*
3694 * Delete (and withdraw) local routes for a VNI - only from the global
3695 * table. Invoked upon router-id change.
3696 */
3697 static int delete_withdraw_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
3698 {
3699 struct prefix_evpn p;
3700 struct bgp_dest *global_dest;
3701 struct bgp_path_info *pi;
3702 afi_t afi = AFI_L2VPN;
3703 safi_t safi = SAFI_EVPN;
3704
3705 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3706 * for this VNI - from the global table.
3707 */
3708 delete_global_type2_routes(bgp, vpn);
3709
3710 /* Remove type-3 route for this VNI from global table. */
3711 build_evpn_type3_prefix(&p, vpn->originator_ip);
3712 global_dest = bgp_global_evpn_node_lookup(bgp->rib[afi][safi], afi, safi,
3713 (const struct prefix_evpn *)&p, &vpn->prd);
3714 if (global_dest) {
3715 /* Delete route entry in the global EVPN table. */
3716 delete_evpn_route_entry(bgp, afi, safi, global_dest, &pi);
3717
3718 /* Schedule for processing - withdraws to peers happen from
3719 * this table.
3720 */
3721 if (pi)
3722 bgp_process(bgp, global_dest, afi, safi);
3723 bgp_dest_unlock_node(global_dest);
3724 }
3725
3726
3727 delete_global_ead_evi_routes(bgp, vpn);
3728 return 0;
3729 }
3730
3731 /*
3732 * Handle router-id change. Update and advertise local routes corresponding
3733 * to this VNI from peers. Note that this is invoked after updating the
3734 * router-id. The routes in the per-VNI table are used to create routes in
3735 * the global table and schedule them.
3736 */
3737 static void update_router_id_vni(struct hash_bucket *bucket, struct bgp *bgp)
3738 {
3739 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
3740
3741 /* Skip VNIs with configured RD. */
3742 if (is_rd_configured(vpn))
3743 return;
3744
3745 bgp_evpn_derive_auto_rd(bgp, vpn);
3746 update_advertise_vni_routes(bgp, vpn);
3747 }
3748
3749 /*
3750 * Handle router-id change. Delete and withdraw local routes corresponding
3751 * to this VNI from peers. Note that this is invoked prior to updating
3752 * the router-id and is done only on the global route table, the routes
3753 * are needed in the per-VNI table to re-advertise with new router id.
3754 */
3755 static void withdraw_router_id_vni(struct hash_bucket *bucket, struct bgp *bgp)
3756 {
3757 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
3758
3759 /* Skip VNIs with configured RD. */
3760 if (is_rd_configured(vpn))
3761 return;
3762
3763 delete_withdraw_vni_routes(bgp, vpn);
3764 }
3765
3766 /*
3767 * Create RT-3 for a VNI and schedule for processing and advertisement.
3768 * This is invoked upon flooding mode changing to head-end replication.
3769 */
3770 static void create_advertise_type3(struct hash_bucket *bucket, void *data)
3771 {
3772 struct bgpevpn *vpn = bucket->data;
3773 struct bgp *bgp = data;
3774 struct prefix_evpn p;
3775
3776 if (!vpn || !is_vni_live(vpn) ||
3777 bgp_evpn_vni_flood_mode_get(bgp, vpn)
3778 != VXLAN_FLOOD_HEAD_END_REPL)
3779 return;
3780
3781 build_evpn_type3_prefix(&p, vpn->originator_ip);
3782 if (update_evpn_route(bgp, vpn, &p, 0, 0, NULL))
3783 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
3784 "Type3 route creation failure for VNI %u", vpn->vni);
3785 }
3786
3787 /*
3788 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3789 * This is invoked upon flooding mode changing to drop BUM packets.
3790 */
3791 static void delete_withdraw_type3(struct hash_bucket *bucket, void *data)
3792 {
3793 struct bgpevpn *vpn = bucket->data;
3794 struct bgp *bgp = data;
3795 struct prefix_evpn p;
3796
3797 if (!vpn || !is_vni_live(vpn))
3798 return;
3799
3800 build_evpn_type3_prefix(&p, vpn->originator_ip);
3801 delete_evpn_route(bgp, vpn, &p);
3802 }
3803
3804 /*
3805 * Process received EVPN type-2 route (advertise or withdraw).
3806 */
3807 static int process_type2_route(struct peer *peer, afi_t afi, safi_t safi,
3808 struct attr *attr, uint8_t *pfx, int psize,
3809 uint32_t addpath_id)
3810 {
3811 struct prefix_rd prd;
3812 struct prefix_evpn p = {};
3813 struct bgp_route_evpn evpn = {};
3814 uint8_t ipaddr_len;
3815 uint8_t macaddr_len;
3816 /* holds the VNI(s) as in packet */
3817 mpls_label_t label[BGP_MAX_LABELS] = {};
3818 uint32_t num_labels = 0;
3819 uint32_t eth_tag;
3820 int ret;
3821
3822 /* Type-2 route should be either 33, 37 or 49 bytes or an
3823 * additional 3 bytes if there is a second label (VNI):
3824 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3825 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3826 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3827 */
3828 if (psize != 33 && psize != 37 && psize != 49 && psize != 36
3829 && psize != 40 && psize != 52) {
3830 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
3831 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3832 peer->bgp->vrf_id, peer->host, psize);
3833 return -1;
3834 }
3835
3836 struct stream *pkt = stream_new(psize);
3837 stream_put(pkt, pfx, psize);
3838
3839 /* Make prefix_rd */
3840 prd.family = AF_UNSPEC;
3841 prd.prefixlen = 64;
3842
3843 STREAM_GET(&prd.val, pkt, 8);
3844
3845 /* Make EVPN prefix. */
3846 p.family = AF_EVPN;
3847 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
3848 p.prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
3849
3850 /* Copy Ethernet Seg Identifier */
3851 if (attr) {
3852 STREAM_GET(&attr->esi, pkt, sizeof(esi_t));
3853
3854 if (bgp_evpn_is_esi_local_and_non_bypass(&attr->esi))
3855 attr->es_flags |= ATTR_ES_IS_LOCAL;
3856 else
3857 attr->es_flags &= ~ATTR_ES_IS_LOCAL;
3858 } else {
3859 STREAM_FORWARD_GETP(pkt, sizeof(esi_t));
3860 }
3861
3862 /* Copy Ethernet Tag */
3863 STREAM_GET(&eth_tag, pkt, 4);
3864 p.prefix.macip_addr.eth_tag = ntohl(eth_tag);
3865
3866 /* Get the MAC Addr len */
3867 STREAM_GETC(pkt, macaddr_len);
3868
3869 /* Get the MAC Addr */
3870 if (macaddr_len == (ETH_ALEN * 8)) {
3871 STREAM_GET(&p.prefix.macip_addr.mac.octet, pkt, ETH_ALEN);
3872 } else {
3873 flog_err(
3874 EC_BGP_EVPN_ROUTE_INVALID,
3875 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3876 peer->bgp->vrf_id, peer->host, macaddr_len);
3877 goto fail;
3878 }
3879
3880
3881 /* Get the IP. */
3882 STREAM_GETC(pkt, ipaddr_len);
3883
3884 if (ipaddr_len != 0 && ipaddr_len != IPV4_MAX_BITLEN
3885 && ipaddr_len != IPV6_MAX_BITLEN) {
3886 flog_err(
3887 EC_BGP_EVPN_ROUTE_INVALID,
3888 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3889 peer->bgp->vrf_id, peer->host, ipaddr_len);
3890 goto fail;
3891 }
3892
3893 if (ipaddr_len) {
3894 ipaddr_len /= 8; /* Convert to bytes. */
3895 p.prefix.macip_addr.ip.ipa_type = (ipaddr_len == IPV4_MAX_BYTELEN)
3896 ? IPADDR_V4
3897 : IPADDR_V6;
3898 STREAM_GET(&p.prefix.macip_addr.ip.ip.addr, pkt, ipaddr_len);
3899 }
3900
3901 /* Get the VNI(s). Stored as bytes here. */
3902 STREAM_GET(&label[0], pkt, BGP_LABEL_BYTES);
3903 num_labels++;
3904
3905 /* Do we have a second VNI? */
3906 if (STREAM_READABLE(pkt)) {
3907 num_labels++;
3908 STREAM_GET(&label[1], pkt, BGP_LABEL_BYTES);
3909 }
3910
3911 /* Process the route. */
3912 if (attr)
3913 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
3914 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
3915 &prd, &label[0], num_labels, 0, &evpn);
3916 else
3917 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
3918 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
3919 &prd, &label[0], num_labels, &evpn);
3920 goto done;
3921
3922 fail:
3923 stream_failure:
3924 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
3925 "%u:%s - Rx EVPN Type-2 NLRI - corrupt, discarding",
3926 peer->bgp->vrf_id, peer->host);
3927 ret = -1;
3928 done:
3929 stream_free(pkt);
3930 return ret;
3931 }
3932
3933 /*
3934 * Process received EVPN type-3 route (advertise or withdraw).
3935 */
3936 static int process_type3_route(struct peer *peer, afi_t afi, safi_t safi,
3937 struct attr *attr, uint8_t *pfx, int psize,
3938 uint32_t addpath_id)
3939 {
3940 struct prefix_rd prd;
3941 struct prefix_evpn p;
3942 uint8_t ipaddr_len;
3943 uint32_t eth_tag;
3944 int ret;
3945
3946 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3947 * IP len (1) and IP (4 or 16).
3948 */
3949 if (psize != 17 && psize != 29) {
3950 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
3951 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3952 peer->bgp->vrf_id, peer->host, psize);
3953 return -1;
3954 }
3955
3956 /* If PMSI is present, log if it is anything other than IR.
3957 * Note: We just simply ignore the values as it is not clear if
3958 * doing anything else is better.
3959 */
3960 if (attr &&
3961 (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL))) {
3962 enum pta_type pmsi_tnl_type = bgp_attr_get_pmsi_tnl_type(attr);
3963
3964 if (pmsi_tnl_type != PMSI_TNLTYPE_INGR_REPL
3965 && pmsi_tnl_type != PMSI_TNLTYPE_PIM_SM) {
3966 flog_warn(
3967 EC_BGP_EVPN_PMSI_PRESENT,
3968 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3969 peer->bgp->vrf_id, peer->host, pmsi_tnl_type);
3970 }
3971 }
3972
3973 /* Make prefix_rd */
3974 prd.family = AF_UNSPEC;
3975 prd.prefixlen = 64;
3976 memcpy(&prd.val, pfx, 8);
3977 pfx += 8;
3978
3979 /* Make EVPN prefix. */
3980 memset(&p, 0, sizeof(p));
3981 p.family = AF_EVPN;
3982 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
3983 p.prefix.route_type = BGP_EVPN_IMET_ROUTE;
3984
3985 /* Copy Ethernet Tag */
3986 memcpy(&eth_tag, pfx, 4);
3987 p.prefix.imet_addr.eth_tag = ntohl(eth_tag);
3988 pfx += 4;
3989
3990 /* Get the IP. */
3991 ipaddr_len = *pfx++;
3992 if (ipaddr_len == IPV4_MAX_BITLEN) {
3993 p.prefix.imet_addr.ip.ipa_type = IPADDR_V4;
3994 memcpy(&p.prefix.imet_addr.ip.ip.addr, pfx, IPV4_MAX_BYTELEN);
3995 } else {
3996 flog_err(
3997 EC_BGP_EVPN_ROUTE_INVALID,
3998 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3999 peer->bgp->vrf_id, peer->host, ipaddr_len);
4000 return -1;
4001 }
4002
4003 /* Process the route. */
4004 if (attr)
4005 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
4006 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
4007 &prd, NULL, 0, 0, NULL);
4008 else
4009 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
4010 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
4011 &prd, NULL, 0, NULL);
4012 return ret;
4013 }
4014
4015 /*
4016 * Process received EVPN type-5 route (advertise or withdraw).
4017 */
4018 static int process_type5_route(struct peer *peer, afi_t afi, safi_t safi,
4019 struct attr *attr, uint8_t *pfx, int psize,
4020 uint32_t addpath_id)
4021 {
4022 struct prefix_rd prd;
4023 struct prefix_evpn p;
4024 struct bgp_route_evpn evpn;
4025 uint8_t ippfx_len;
4026 uint32_t eth_tag;
4027 mpls_label_t label; /* holds the VNI as in the packet */
4028 int ret;
4029 bool is_valid_update = true;
4030
4031 /* Type-5 route should be 34 or 58 bytes:
4032 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
4033 * GW (4 or 16) and VNI (3).
4034 * Note that the IP and GW should both be IPv4 or both IPv6.
4035 */
4036 if (psize != 34 && psize != 58) {
4037 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
4038 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
4039 peer->bgp->vrf_id, peer->host, psize);
4040 return -1;
4041 }
4042
4043 /* Make prefix_rd */
4044 prd.family = AF_UNSPEC;
4045 prd.prefixlen = 64;
4046 memcpy(&prd.val, pfx, 8);
4047 pfx += 8;
4048
4049 /* Make EVPN prefix. */
4050 memset(&p, 0, sizeof(p));
4051 p.family = AF_EVPN;
4052 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
4053 p.prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
4054
4055 /* Additional information outside of prefix - ESI and GW IP */
4056 memset(&evpn, 0, sizeof(evpn));
4057
4058 /* Fetch ESI overlay index */
4059 if (attr)
4060 memcpy(&evpn.eth_s_id, pfx, sizeof(esi_t));
4061 pfx += ESI_BYTES;
4062
4063 /* Fetch Ethernet Tag. */
4064 memcpy(&eth_tag, pfx, 4);
4065 p.prefix.prefix_addr.eth_tag = ntohl(eth_tag);
4066 pfx += 4;
4067
4068 /* Fetch IP prefix length. */
4069 ippfx_len = *pfx++;
4070 if (ippfx_len > IPV6_MAX_BITLEN) {
4071 flog_err(
4072 EC_BGP_EVPN_ROUTE_INVALID,
4073 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
4074 peer->bgp->vrf_id, peer->host, ippfx_len);
4075 return -1;
4076 }
4077 p.prefix.prefix_addr.ip_prefix_length = ippfx_len;
4078
4079 /* Determine IPv4 or IPv6 prefix */
4080 /* Since the address and GW are from the same family, this just becomes
4081 * a simple check on the total size.
4082 */
4083 if (psize == 34) {
4084 SET_IPADDR_V4(&p.prefix.prefix_addr.ip);
4085 memcpy(&p.prefix.prefix_addr.ip.ipaddr_v4, pfx, 4);
4086 pfx += 4;
4087 SET_IPADDR_V4(&evpn.gw_ip);
4088 memcpy(&evpn.gw_ip.ipaddr_v4, pfx, 4);
4089 pfx += 4;
4090 } else {
4091 SET_IPADDR_V6(&p.prefix.prefix_addr.ip);
4092 memcpy(&p.prefix.prefix_addr.ip.ipaddr_v6, pfx,
4093 IPV6_MAX_BYTELEN);
4094 pfx += IPV6_MAX_BYTELEN;
4095 SET_IPADDR_V6(&evpn.gw_ip);
4096 memcpy(&evpn.gw_ip.ipaddr_v6, pfx, IPV6_MAX_BYTELEN);
4097 pfx += IPV6_MAX_BYTELEN;
4098 }
4099
4100 /* Get the VNI (in MPLS label field). Stored as bytes here. */
4101 memset(&label, 0, sizeof(label));
4102 memcpy(&label, pfx, BGP_LABEL_BYTES);
4103
4104 /*
4105 * If in future, we are required to access additional fields,
4106 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
4107 * field
4108 */
4109
4110 /*
4111 * An update containing a non-zero gateway IP and a non-zero ESI
4112 * at the same time is should be treated as withdraw
4113 */
4114 if (bgp_evpn_is_esi_valid(&evpn.eth_s_id) &&
4115 !ipaddr_is_zero(&evpn.gw_ip)) {
4116 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
4117 "%s - Rx EVPN Type-5 ESI and gateway-IP both non-zero.",
4118 peer->host);
4119 is_valid_update = false;
4120 } else if (bgp_evpn_is_esi_valid(&evpn.eth_s_id))
4121 evpn.type = OVERLAY_INDEX_ESI;
4122 else if (!ipaddr_is_zero(&evpn.gw_ip))
4123 evpn.type = OVERLAY_INDEX_GATEWAY_IP;
4124 if (attr) {
4125 if (is_zero_mac(&attr->rmac) &&
4126 !bgp_evpn_is_esi_valid(&evpn.eth_s_id) &&
4127 ipaddr_is_zero(&evpn.gw_ip) && label == 0) {
4128 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
4129 "%s - Rx EVPN Type-5 ESI, gateway-IP, RMAC and label all zero",
4130 peer->host);
4131 is_valid_update = false;
4132 }
4133
4134 if (is_mcast_mac(&attr->rmac) || is_bcast_mac(&attr->rmac))
4135 is_valid_update = false;
4136 }
4137
4138 /* Process the route. */
4139 if (attr && is_valid_update)
4140 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
4141 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
4142 &prd, &label, 1, 0, &evpn);
4143 else {
4144 if (!is_valid_update) {
4145 char attr_str[BUFSIZ] = {0};
4146
4147 bgp_dump_attr(attr, attr_str, BUFSIZ);
4148 zlog_warn(
4149 "Invalid update from peer %s vrf %u prefix %pFX attr %s - treat as withdraw",
4150 peer->hostname, peer->bgp->vrf_id, &p,
4151 attr_str);
4152 }
4153 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
4154 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
4155 &prd, &label, 1, &evpn);
4156 }
4157
4158 return ret;
4159 }
4160
4161 static void evpn_mpattr_encode_type5(struct stream *s, const struct prefix *p,
4162 const struct prefix_rd *prd,
4163 mpls_label_t *label, uint32_t num_labels,
4164 struct attr *attr)
4165 {
4166 int len;
4167 char temp[16];
4168 const struct evpn_addr *p_evpn_p;
4169
4170 memset(&temp, 0, sizeof(temp));
4171 if (p->family != AF_EVPN)
4172 return;
4173 p_evpn_p = &(p->u.prefix_evpn);
4174
4175 /* len denites the total len of IP and GW-IP in the route
4176 IP and GW-IP have to be both ipv4 or ipv6
4177 */
4178 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
4179 len = 8; /* IP and GWIP are both ipv4 */
4180 else
4181 len = 32; /* IP and GWIP are both ipv6 */
4182 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
4183 stream_putc(s, 8 + 10 + 4 + 1 + len + 3);
4184 stream_put(s, prd->val, 8);
4185 if (attr && attr->evpn_overlay.type == OVERLAY_INDEX_ESI)
4186 stream_put(s, &attr->esi, sizeof(esi_t));
4187 else
4188 stream_put(s, 0, sizeof(esi_t));
4189 stream_putl(s, p_evpn_p->prefix_addr.eth_tag);
4190 stream_putc(s, p_evpn_p->prefix_addr.ip_prefix_length);
4191 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
4192 stream_put_ipv4(s, p_evpn_p->prefix_addr.ip.ipaddr_v4.s_addr);
4193 else
4194 stream_put(s, &p_evpn_p->prefix_addr.ip.ipaddr_v6, 16);
4195 if (attr && attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP) {
4196 const struct bgp_route_evpn *evpn_overlay =
4197 bgp_attr_get_evpn_overlay(attr);
4198
4199 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
4200 stream_put_ipv4(s,
4201 evpn_overlay->gw_ip.ipaddr_v4.s_addr);
4202 else
4203 stream_put(s, &(evpn_overlay->gw_ip.ipaddr_v6), 16);
4204 } else {
4205 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
4206 stream_put_ipv4(s, 0);
4207 else
4208 stream_put(s, &temp, 16);
4209 }
4210
4211 if (num_labels)
4212 stream_put(s, label, 3);
4213 else
4214 stream_put3(s, 0);
4215 }
4216
4217 /*
4218 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
4219 */
4220 static void cleanup_vni_on_disable(struct hash_bucket *bucket, struct bgp *bgp)
4221 {
4222 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
4223
4224 /* Remove EVPN routes and schedule for processing. */
4225 delete_routes_for_vni(bgp, vpn);
4226
4227 /* Clear "live" flag and see if hash needs to be freed. */
4228 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
4229 if (!is_vni_configured(vpn))
4230 bgp_evpn_free(bgp, vpn);
4231 }
4232
4233 /*
4234 * Free a VNI entry; iterator function called during cleanup.
4235 */
4236 static void free_vni_entry(struct hash_bucket *bucket, struct bgp *bgp)
4237 {
4238 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
4239
4240 delete_all_vni_routes(bgp, vpn);
4241 bgp_evpn_free(bgp, vpn);
4242 }
4243
4244 /*
4245 * Derive AUTO import RT for BGP VRF - L3VNI
4246 */
4247 static void evpn_auto_rt_import_add_for_vrf(struct bgp *bgp_vrf)
4248 {
4249 struct bgp *bgp_evpn = NULL;
4250
4251 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
4252 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
4253
4254 /* Map RT to VRF */
4255 bgp_evpn = bgp_get_evpn();
4256 if (!bgp_evpn)
4257 return;
4258 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
4259 }
4260
4261 /*
4262 * Delete AUTO import RT from BGP VRF - L3VNI
4263 */
4264 static void evpn_auto_rt_import_delete_for_vrf(struct bgp *bgp_vrf)
4265 {
4266 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
4267 }
4268
4269 /*
4270 * Derive AUTO export RT for BGP VRF - L3VNI
4271 */
4272 static void evpn_auto_rt_export_add_for_vrf(struct bgp *bgp_vrf)
4273 {
4274 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
4275 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
4276 }
4277
4278 /*
4279 * Delete AUTO export RT from BGP VRF - L3VNI
4280 */
4281 static void evpn_auto_rt_export_delete_for_vrf(struct bgp *bgp_vrf)
4282 {
4283 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
4284 }
4285
4286 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp *bgp_vrf)
4287 {
4288 struct bgp *bgp_evpn = NULL;
4289 struct listnode *node = NULL;
4290 struct bgpevpn *vpn = NULL;
4291
4292 bgp_evpn = bgp_get_evpn();
4293 if (!bgp_evpn)
4294 return;
4295
4296 /* update all type-5 routes */
4297 update_advertise_vrf_routes(bgp_vrf);
4298
4299 /* update all type-2 routes */
4300 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
4301 update_routes_for_vni(bgp_evpn, vpn);
4302 }
4303
4304 /*
4305 * Handle autort change for a given VNI.
4306 */
4307 static void update_autort_vni(struct hash_bucket *bucket, struct bgp *bgp)
4308 {
4309 struct bgpevpn *vpn = bucket->data;
4310
4311 if (!is_import_rt_configured(vpn)) {
4312 if (is_vni_live(vpn))
4313 bgp_evpn_uninstall_routes(bgp, vpn);
4314 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
4315 list_delete_all_node(vpn->import_rtl);
4316 bgp_evpn_derive_auto_rt_import(bgp, vpn);
4317 if (is_vni_live(vpn))
4318 bgp_evpn_install_routes(bgp, vpn);
4319 }
4320 if (!is_export_rt_configured(vpn)) {
4321 list_delete_all_node(vpn->export_rtl);
4322 bgp_evpn_derive_auto_rt_export(bgp, vpn);
4323 if (is_vni_live(vpn))
4324 bgp_evpn_handle_export_rt_change(bgp, vpn);
4325 }
4326 }
4327
4328 /*
4329 * Handle autort change for L3VNI.
4330 */
4331 static void update_autort_l3vni(struct bgp *bgp)
4332 {
4333 if ((CHECK_FLAG(bgp->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
4334 && (CHECK_FLAG(bgp->vrf_flags, BGP_VRF_EXPORT_RT_CFGD)))
4335 return;
4336
4337 if (!CHECK_FLAG(bgp->vrf_flags, BGP_VRF_IMPORT_RT_CFGD)) {
4338 if (is_l3vni_live(bgp))
4339 uninstall_routes_for_vrf(bgp);
4340
4341 /* Cleanup the RT to VRF mapping */
4342 bgp_evpn_unmap_vrf_from_its_rts(bgp);
4343
4344 /* Remove auto generated RT */
4345 evpn_auto_rt_import_delete_for_vrf(bgp);
4346
4347 list_delete_all_node(bgp->vrf_import_rtl);
4348
4349 /* Map auto derive or configured RTs */
4350 evpn_auto_rt_import_add_for_vrf(bgp);
4351 }
4352
4353 if (!CHECK_FLAG(bgp->vrf_flags, BGP_VRF_EXPORT_RT_CFGD)) {
4354 list_delete_all_node(bgp->vrf_export_rtl);
4355
4356 evpn_auto_rt_export_delete_for_vrf(bgp);
4357
4358 evpn_auto_rt_export_add_for_vrf(bgp);
4359
4360 if (is_l3vni_live(bgp))
4361 bgp_evpn_map_vrf_to_its_rts(bgp);
4362 }
4363
4364 if (!is_l3vni_live(bgp))
4365 return;
4366
4367 /* advertise type-5 routes if needed */
4368 update_advertise_vrf_routes(bgp);
4369
4370 /* install all remote routes belonging to this l3vni
4371 * into corresponding vrf
4372 */
4373 install_routes_for_vrf(bgp);
4374 }
4375
4376 /*
4377 * Public functions.
4378 */
4379
4380 /* withdraw type-5 route corresponding to ip prefix */
4381 void bgp_evpn_withdraw_type5_route(struct bgp *bgp_vrf, const struct prefix *p,
4382 afi_t afi, safi_t safi)
4383 {
4384 int ret = 0;
4385 struct prefix_evpn evp;
4386
4387 build_type5_prefix_from_ip_prefix(&evp, p);
4388 ret = delete_evpn_type5_route(bgp_vrf, &evp);
4389 if (ret)
4390 flog_err(
4391 EC_BGP_EVPN_ROUTE_DELETE,
4392 "%u failed to delete type-5 route for prefix %pFX in vrf %s",
4393 bgp_vrf->vrf_id, p, vrf_id_to_name(bgp_vrf->vrf_id));
4394 }
4395
4396 /* withdraw all type-5 routes for an address family */
4397 void bgp_evpn_withdraw_type5_routes(struct bgp *bgp_vrf, afi_t afi, safi_t safi)
4398 {
4399 struct bgp_table *table = NULL;
4400 struct bgp_dest *dest = NULL;
4401 struct bgp_path_info *pi;
4402
4403 table = bgp_vrf->rib[afi][safi];
4404 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
4405 /* Only care about "selected" routes. Also ensure that
4406 * these are routes that are injectable into EVPN.
4407 */
4408 /* TODO: Support for AddPath for EVPN. */
4409 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
4410 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
4411 && is_route_injectable_into_evpn(pi)) {
4412 bgp_evpn_withdraw_type5_route(
4413 bgp_vrf, bgp_dest_get_prefix(dest), afi,
4414 safi);
4415 break;
4416 }
4417 }
4418 }
4419 }
4420
4421 /*
4422 * evpn - enable advertisement of default g/w
4423 */
4424 void bgp_evpn_install_uninstall_default_route(struct bgp *bgp_vrf, afi_t afi,
4425 safi_t safi, bool add)
4426 {
4427 struct prefix ip_prefix;
4428
4429 /* form the default prefix 0.0.0.0/0 */
4430 memset(&ip_prefix, 0, sizeof(ip_prefix));
4431 ip_prefix.family = afi2family(afi);
4432
4433 if (add) {
4434 bgp_evpn_advertise_type5_route(bgp_vrf, &ip_prefix,
4435 NULL, afi, safi);
4436 } else {
4437 bgp_evpn_withdraw_type5_route(bgp_vrf, &ip_prefix,
4438 afi, safi);
4439 }
4440 }
4441
4442
4443 /*
4444 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4445 * to this function correspond to those of the source IP prefix (best
4446 * path in the case of the attr. In the case of a local prefix (when we
4447 * are advertising local subnets), the src_attr will be NULL.
4448 */
4449 void bgp_evpn_advertise_type5_route(struct bgp *bgp_vrf, const struct prefix *p,
4450 struct attr *src_attr, afi_t afi,
4451 safi_t safi)
4452 {
4453 int ret = 0;
4454 struct prefix_evpn evp;
4455
4456 build_type5_prefix_from_ip_prefix(&evp, p);
4457 ret = update_evpn_type5_route(bgp_vrf, &evp, src_attr, afi, safi);
4458 if (ret)
4459 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
4460 "%u: Failed to create type-5 route for prefix %pFX",
4461 bgp_vrf->vrf_id, p);
4462 }
4463
4464 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4465 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4466 * advertisement is enabled.
4467 */
4468 void bgp_evpn_advertise_type5_routes(struct bgp *bgp_vrf, afi_t afi,
4469 safi_t safi)
4470 {
4471 struct bgp_table *table = NULL;
4472 struct bgp_dest *dest = NULL;
4473 struct bgp_path_info *pi;
4474
4475 table = bgp_vrf->rib[afi][safi];
4476 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
4477 /* Need to identify the "selected" route entry to use its
4478 * attribute. Also, ensure that the route is injectable
4479 * into EVPN.
4480 * TODO: Support for AddPath for EVPN.
4481 */
4482 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
4483 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
4484 && is_route_injectable_into_evpn(pi)) {
4485
4486 /* apply the route-map */
4487 if (bgp_vrf->adv_cmd_rmap[afi][safi].map) {
4488 route_map_result_t ret;
4489 struct bgp_path_info tmp_pi;
4490 struct bgp_path_info_extra tmp_pie;
4491 struct attr tmp_attr;
4492
4493 tmp_attr = *pi->attr;
4494
4495 /* Fill temp path_info */
4496 prep_for_rmap_apply(&tmp_pi, &tmp_pie,
4497 dest, pi, pi->peer,
4498 &tmp_attr);
4499
4500 RESET_FLAG(tmp_attr.rmap_change_flags);
4501
4502 ret = route_map_apply(
4503 bgp_vrf->adv_cmd_rmap[afi][safi]
4504 .map,
4505 bgp_dest_get_prefix(dest),
4506 &tmp_pi);
4507 if (ret == RMAP_DENYMATCH) {
4508 bgp_attr_flush(&tmp_attr);
4509 continue;
4510 }
4511 bgp_evpn_advertise_type5_route(
4512 bgp_vrf,
4513 bgp_dest_get_prefix(dest),
4514 &tmp_attr, afi, safi);
4515 } else
4516 bgp_evpn_advertise_type5_route(
4517 bgp_vrf,
4518 bgp_dest_get_prefix(dest),
4519 pi->attr, afi, safi);
4520 break;
4521 }
4522 }
4523 }
4524 }
4525
4526 void evpn_rt_delete_auto(struct bgp *bgp, vni_t vni, struct list *rtl)
4527 {
4528 struct listnode *node, *nnode, *node_to_del;
4529 struct ecommunity *ecom, *ecom_auto;
4530 struct ecommunity_val eval;
4531
4532 if (bgp->advertise_autort_rfc8365)
4533 vni |= EVPN_AUTORT_VXLAN;
4534 encode_route_target_as((bgp->as & 0xFFFF), vni, &eval);
4535
4536 ecom_auto = ecommunity_new();
4537 ecommunity_add_val(ecom_auto, &eval, false, false);
4538 node_to_del = NULL;
4539
4540 for (ALL_LIST_ELEMENTS(rtl, node, nnode, ecom)) {
4541 if (ecommunity_match(ecom, ecom_auto)) {
4542 ecommunity_free(&ecom);
4543 node_to_del = node;
4544 break;
4545 }
4546 }
4547
4548 if (node_to_del)
4549 list_delete_node(rtl, node_to_del);
4550
4551 ecommunity_free(&ecom_auto);
4552 }
4553
4554 void bgp_evpn_configure_import_rt_for_vrf(struct bgp *bgp_vrf,
4555 struct ecommunity *ecomadd)
4556 {
4557 /* uninstall routes from vrf */
4558 if (is_l3vni_live(bgp_vrf))
4559 uninstall_routes_for_vrf(bgp_vrf);
4560
4561 /* Cleanup the RT to VRF mapping */
4562 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
4563
4564 /* Remove auto generated RT */
4565 evpn_auto_rt_import_delete_for_vrf(bgp_vrf);
4566
4567 /* Add the newly configured RT to RT list */
4568 listnode_add_sort(bgp_vrf->vrf_import_rtl, ecomadd);
4569 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
4570
4571 /* map VRF to its RTs and install routes matching the new RTs */
4572 if (is_l3vni_live(bgp_vrf)) {
4573 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
4574 install_routes_for_vrf(bgp_vrf);
4575 }
4576 }
4577
4578 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf,
4579 struct ecommunity *ecomdel)
4580 {
4581 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
4582 struct ecommunity *ecom = NULL;
4583
4584 /* uninstall routes from vrf */
4585 if (is_l3vni_live(bgp_vrf))
4586 uninstall_routes_for_vrf(bgp_vrf);
4587
4588 /* Cleanup the RT to VRF mapping */
4589 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
4590
4591 /* remove the RT from the RT list */
4592 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
4593 if (ecommunity_match(ecom, ecomdel)) {
4594 ecommunity_free(&ecom);
4595 node_to_del = node;
4596 break;
4597 }
4598 }
4599
4600 if (node_to_del)
4601 list_delete_node(bgp_vrf->vrf_import_rtl, node_to_del);
4602
4603 assert(bgp_vrf->vrf_import_rtl);
4604 /* fallback to auto import rt, if this was the last RT */
4605 if (bgp_vrf->vrf_import_rtl && list_isempty(bgp_vrf->vrf_import_rtl)) {
4606 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
4607 if (is_l3vni_live(bgp_vrf))
4608 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
4609 }
4610
4611 /* map VRFs to its RTs and install routes matching this new RT */
4612 if (is_l3vni_live(bgp_vrf)) {
4613 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
4614 install_routes_for_vrf(bgp_vrf);
4615 }
4616 }
4617
4618 void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf,
4619 struct ecommunity *ecomadd)
4620 {
4621 /* remove auto-generated RT */
4622 evpn_auto_rt_export_delete_for_vrf(bgp_vrf);
4623
4624 /* Add the new RT to the RT list */
4625 listnode_add_sort(bgp_vrf->vrf_export_rtl, ecomadd);
4626 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
4627
4628 if (is_l3vni_live(bgp_vrf))
4629 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
4630 }
4631
4632 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf,
4633 struct ecommunity *ecomdel)
4634 {
4635 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
4636 struct ecommunity *ecom = NULL;
4637
4638 /* Remove the RT from the RT list */
4639 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_export_rtl, node, nnode, ecom)) {
4640 if (ecommunity_match(ecom, ecomdel)) {
4641 ecommunity_free(&ecom);
4642 node_to_del = node;
4643 break;
4644 }
4645 }
4646
4647 if (node_to_del)
4648 list_delete_node(bgp_vrf->vrf_export_rtl, node_to_del);
4649
4650 /*
4651 * Temporary assert to make SA happy.
4652 * The ALL_LIST_ELEMENTS macro above has a NULL check
4653 * which means that SA is going to complain about
4654 * the list_isempty call, which doesn't NULL check.
4655 * So until we get this situation cleaned up, here
4656 * we are.
4657 */
4658 assert(bgp_vrf->vrf_export_rtl);
4659
4660 /* fall back to auto-generated RT if this was the last RT */
4661 if (list_isempty(bgp_vrf->vrf_export_rtl)) {
4662 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
4663 if (is_l3vni_live(bgp_vrf))
4664 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
4665 }
4666
4667 if (is_l3vni_live(bgp_vrf))
4668 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
4669 }
4670
4671 /*
4672 * Handle change to BGP router id. This is invoked twice by the change
4673 * handler, first before the router id has been changed and then after
4674 * the router id has been changed. The first invocation will result in
4675 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4676 * will result in the routes being re-advertised.
4677 */
4678 void bgp_evpn_handle_router_id_update(struct bgp *bgp, int withdraw)
4679 {
4680 struct listnode *node;
4681 struct bgp *bgp_vrf;
4682
4683 if (withdraw) {
4684
4685 /* delete and withdraw all the type-5 routes
4686 stored in the global table for this vrf
4687 */
4688 withdraw_router_id_vrf(bgp);
4689
4690 /* delete all the VNI routes (type-2/type-3) routes for all the
4691 * L2-VNIs
4692 */
4693 hash_iterate(bgp->vnihash,
4694 (void (*)(struct hash_bucket *,
4695 void *))withdraw_router_id_vni,
4696 bgp);
4697
4698 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
4699 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
4700 if (bgp_vrf->evpn_info->advertise_pip &&
4701 (bgp_vrf->evpn_info->pip_ip_static.s_addr
4702 == INADDR_ANY))
4703 bgp_vrf->evpn_info->pip_ip.s_addr
4704 = INADDR_ANY;
4705 }
4706 }
4707 } else {
4708
4709 /* Assign new default instance router-id */
4710 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
4711 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
4712 if (bgp_vrf->evpn_info->advertise_pip &&
4713 (bgp_vrf->evpn_info->pip_ip_static.s_addr
4714 == INADDR_ANY)) {
4715 bgp_vrf->evpn_info->pip_ip =
4716 bgp->router_id;
4717 /* advertise type-5 routes with
4718 * new nexthop
4719 */
4720 update_advertise_vrf_routes(bgp_vrf);
4721 }
4722 }
4723 }
4724
4725 /* advertise all routes in the vrf as type-5 routes with the new
4726 * RD
4727 */
4728 update_router_id_vrf(bgp);
4729
4730 /* advertise all the VNI routes (type-2/type-3) routes with the
4731 * new RD
4732 */
4733 hash_iterate(bgp->vnihash,
4734 (void (*)(struct hash_bucket *,
4735 void *))update_router_id_vni,
4736 bgp);
4737 }
4738 }
4739
4740 /*
4741 * Handle change to auto-RT algorithm - update and advertise local routes.
4742 */
4743 void bgp_evpn_handle_autort_change(struct bgp *bgp)
4744 {
4745 hash_iterate(bgp->vnihash,
4746 (void (*)(struct hash_bucket *,
4747 void*))update_autort_vni,
4748 bgp);
4749 if (bgp->l3vni)
4750 update_autort_l3vni(bgp);
4751 }
4752
4753 /*
4754 * Handle change to export RT - update and advertise local routes.
4755 */
4756 int bgp_evpn_handle_export_rt_change(struct bgp *bgp, struct bgpevpn *vpn)
4757 {
4758 return update_routes_for_vni(bgp, vpn);
4759 }
4760
4761 void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw)
4762 {
4763 if (withdraw)
4764 delete_withdraw_vrf_routes(bgp_vrf);
4765 else
4766 update_advertise_vrf_routes(bgp_vrf);
4767 }
4768
4769 /*
4770 * Handle change to RD. This is invoked twice by the change handler,
4771 * first before the RD has been changed and then after the RD has
4772 * been changed. The first invocation will result in local routes
4773 * of this VNI being deleted and withdrawn and the next will result
4774 * in the routes being re-advertised.
4775 */
4776 void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
4777 int withdraw)
4778 {
4779 if (withdraw)
4780 delete_withdraw_vni_routes(bgp, vpn);
4781 else
4782 update_advertise_vni_routes(bgp, vpn);
4783 }
4784
4785 /*
4786 * Install routes for this VNI. Invoked upon change to Import RT.
4787 */
4788 int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn)
4789 {
4790 return install_routes_for_vni(bgp, vpn);
4791 }
4792
4793 /*
4794 * Uninstall all routes installed for this VNI. Invoked upon change
4795 * to Import RT.
4796 */
4797 int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn)
4798 {
4799 return uninstall_routes_for_vni(bgp, vpn);
4800 }
4801
4802 /*
4803 * TODO: Hardcoded for a maximum of 2 VNIs right now
4804 */
4805 char *bgp_evpn_label2str(mpls_label_t *label, uint32_t num_labels, char *buf,
4806 int len)
4807 {
4808 vni_t vni1, vni2;
4809
4810 vni1 = label2vni(label);
4811 if (num_labels == 2) {
4812 vni2 = label2vni(label + 1);
4813 snprintf(buf, len, "%u/%u", vni1, vni2);
4814 } else
4815 snprintf(buf, len, "%u", vni1);
4816 return buf;
4817 }
4818
4819 /*
4820 * Function to convert evpn route to json format.
4821 * NOTE: We don't use prefix2str as the output here is a bit different.
4822 */
4823 void bgp_evpn_route2json(const struct prefix_evpn *p, json_object *json)
4824 {
4825 char buf1[ETHER_ADDR_STRLEN];
4826 char buf2[PREFIX2STR_BUFFER];
4827 uint8_t family;
4828 uint8_t prefixlen;
4829
4830 if (!json)
4831 return;
4832
4833 json_object_int_add(json, "routeType", p->prefix.route_type);
4834
4835 switch (p->prefix.route_type) {
4836 case BGP_EVPN_MAC_IP_ROUTE:
4837 json_object_int_add(json, "ethTag",
4838 p->prefix.macip_addr.eth_tag);
4839 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
4840 json_object_string_add(json, "mac",
4841 prefix_mac2str(&p->prefix.macip_addr.mac, buf1,
4842 sizeof(buf1)));
4843
4844 if (!is_evpn_prefix_ipaddr_none(p)) {
4845 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET :
4846 AF_INET6;
4847 prefixlen = (family == AF_INET) ?
4848 IPV4_MAX_BITLEN : IPV6_MAX_BITLEN;
4849 inet_ntop(family, &p->prefix.macip_addr.ip.ip.addr,
4850 buf2, PREFIX2STR_BUFFER);
4851 json_object_int_add(json, "ipLen", prefixlen);
4852 json_object_string_add(json, "ip", buf2);
4853 }
4854 break;
4855
4856 case BGP_EVPN_IMET_ROUTE:
4857 json_object_int_add(json, "ethTag",
4858 p->prefix.imet_addr.eth_tag);
4859 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET : AF_INET6;
4860 prefixlen = (family == AF_INET) ? IPV4_MAX_BITLEN :
4861 IPV6_MAX_BITLEN;
4862 inet_ntop(family, &p->prefix.imet_addr.ip.ip.addr, buf2,
4863 PREFIX2STR_BUFFER);
4864 json_object_int_add(json, "ipLen", prefixlen);
4865 json_object_string_add(json, "ip", buf2);
4866 break;
4867
4868 case BGP_EVPN_IP_PREFIX_ROUTE:
4869 json_object_int_add(json, "ethTag",
4870 p->prefix.prefix_addr.eth_tag);
4871 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET : AF_INET6;
4872 inet_ntop(family, &p->prefix.prefix_addr.ip.ip.addr,
4873 buf2, sizeof(buf2));
4874 json_object_int_add(json, "ipLen",
4875 p->prefix.prefix_addr.ip_prefix_length);
4876 json_object_string_add(json, "ip", buf2);
4877 break;
4878
4879 default:
4880 break;
4881 }
4882 }
4883
4884 /*
4885 * Encode EVPN prefix in Update (MP_REACH)
4886 */
4887 void bgp_evpn_encode_prefix(struct stream *s, const struct prefix *p,
4888 const struct prefix_rd *prd, mpls_label_t *label,
4889 uint32_t num_labels, struct attr *attr,
4890 bool addpath_capable, uint32_t addpath_tx_id)
4891 {
4892 struct prefix_evpn *evp = (struct prefix_evpn *)p;
4893 int len, ipa_len = 0;
4894
4895 if (addpath_capable)
4896 stream_putl(s, addpath_tx_id);
4897
4898 /* Route type */
4899 stream_putc(s, evp->prefix.route_type);
4900
4901 switch (evp->prefix.route_type) {
4902 case BGP_EVPN_MAC_IP_ROUTE:
4903 if (is_evpn_prefix_ipaddr_v4(evp))
4904 ipa_len = IPV4_MAX_BYTELEN;
4905 else if (is_evpn_prefix_ipaddr_v6(evp))
4906 ipa_len = IPV6_MAX_BYTELEN;
4907 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4908 len = 8 + 10 + 4 + 1 + 6 + 1 + ipa_len + 3;
4909 if (ipa_len && num_labels > 1) /* There are 2 VNIs */
4910 len += 3;
4911 stream_putc(s, len);
4912 stream_put(s, prd->val, 8); /* RD */
4913 if (attr)
4914 stream_put(s, &attr->esi, ESI_BYTES);
4915 else
4916 stream_put(s, 0, 10);
4917 stream_putl(s, evp->prefix.macip_addr.eth_tag); /* Ethernet Tag ID */
4918 stream_putc(s, 8 * ETH_ALEN); /* Mac Addr Len - bits */
4919 stream_put(s, evp->prefix.macip_addr.mac.octet, 6); /* Mac Addr */
4920 stream_putc(s, 8 * ipa_len); /* IP address Length */
4921 if (ipa_len) /* IP */
4922 stream_put(s, &evp->prefix.macip_addr.ip.ip.addr,
4923 ipa_len);
4924 /* 1st label is the L2 VNI */
4925 stream_put(s, label, BGP_LABEL_BYTES);
4926 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4927 if (ipa_len && num_labels > 1)
4928 stream_put(s, label + 1, BGP_LABEL_BYTES);
4929 break;
4930
4931 case BGP_EVPN_IMET_ROUTE:
4932 stream_putc(s, 17); // TODO: length - assumes IPv4 address
4933 stream_put(s, prd->val, 8); /* RD */
4934 stream_putl(s, evp->prefix.imet_addr.eth_tag); /* Ethernet Tag ID */
4935 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
4936 /* Originating Router's IP Addr */
4937 stream_put_in_addr(s, &evp->prefix.imet_addr.ip.ipaddr_v4);
4938 break;
4939
4940 case BGP_EVPN_ES_ROUTE:
4941 stream_putc(s, 23); /* TODO: length: assumes ipv4 VTEP */
4942 stream_put(s, prd->val, 8); /* RD */
4943 stream_put(s, evp->prefix.es_addr.esi.val, 10); /* ESI */
4944 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
4945 /* VTEP IP */
4946 stream_put_in_addr(s, &evp->prefix.es_addr.ip.ipaddr_v4);
4947 break;
4948
4949 case BGP_EVPN_AD_ROUTE:
4950 /* RD, ESI, EthTag, 1 VNI */
4951 len = RD_BYTES + ESI_BYTES + EVPN_ETH_TAG_BYTES + BGP_LABEL_BYTES;
4952 stream_putc(s, len);
4953 stream_put(s, prd->val, RD_BYTES); /* RD */
4954 stream_put(s, evp->prefix.ead_addr.esi.val, ESI_BYTES); /* ESI */
4955 stream_putl(s, evp->prefix.ead_addr.eth_tag); /* Ethernet Tag */
4956 stream_put(s, label, BGP_LABEL_BYTES);
4957 break;
4958
4959 case BGP_EVPN_IP_PREFIX_ROUTE:
4960 /* TODO: AddPath support. */
4961 evpn_mpattr_encode_type5(s, p, prd, label, num_labels, attr);
4962 break;
4963
4964 default:
4965 break;
4966 }
4967 }
4968
4969 int bgp_nlri_parse_evpn(struct peer *peer, struct attr *attr,
4970 struct bgp_nlri *packet, int withdraw)
4971 {
4972 uint8_t *pnt;
4973 uint8_t *lim;
4974 afi_t afi;
4975 safi_t safi;
4976 uint32_t addpath_id;
4977 bool addpath_capable;
4978 int psize = 0;
4979 uint8_t rtype;
4980 struct prefix p;
4981
4982 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4983 pnt = packet->nlri;
4984 lim = pnt + packet->length;
4985 afi = packet->afi;
4986 safi = packet->safi;
4987 addpath_id = 0;
4988
4989 addpath_capable = bgp_addpath_encode_rx(peer, afi, safi);
4990
4991 for (; pnt < lim; pnt += psize) {
4992 /* Clear prefix structure. */
4993 memset(&p, 0, sizeof(p));
4994
4995 /* Deal with path-id if AddPath is supported. */
4996 if (addpath_capable) {
4997 /* When packet overflow occurs return immediately. */
4998 if (pnt + BGP_ADDPATH_ID_LEN > lim)
4999 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
5000
5001 memcpy(&addpath_id, pnt, BGP_ADDPATH_ID_LEN);
5002 addpath_id = ntohl(addpath_id);
5003 pnt += BGP_ADDPATH_ID_LEN;
5004 }
5005
5006 /* All EVPN NLRI types start with type and length. */
5007 if (pnt + 2 > lim)
5008 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE;
5009
5010 rtype = *pnt++;
5011 psize = *pnt++;
5012
5013 /* When packet overflow occur return immediately. */
5014 if (pnt + psize > lim)
5015 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
5016
5017 switch (rtype) {
5018 case BGP_EVPN_MAC_IP_ROUTE:
5019 if (process_type2_route(peer, afi, safi,
5020 withdraw ? NULL : attr, pnt,
5021 psize, addpath_id)) {
5022 flog_err(
5023 EC_BGP_EVPN_FAIL,
5024 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
5025 peer->bgp->vrf_id, peer->host, psize);
5026 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE;
5027 }
5028 break;
5029
5030 case BGP_EVPN_IMET_ROUTE:
5031 if (process_type3_route(peer, afi, safi,
5032 withdraw ? NULL : attr, pnt,
5033 psize, addpath_id)) {
5034 flog_err(
5035 EC_BGP_PKT_PROCESS,
5036 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
5037 peer->bgp->vrf_id, peer->host, psize);
5038 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE;
5039 }
5040 break;
5041
5042 case BGP_EVPN_ES_ROUTE:
5043 if (bgp_evpn_type4_route_process(peer, afi, safi,
5044 withdraw ? NULL : attr, pnt,
5045 psize, addpath_id)) {
5046 flog_err(
5047 EC_BGP_PKT_PROCESS,
5048 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
5049 peer->bgp->vrf_id, peer->host, psize);
5050 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE;
5051 }
5052 break;
5053
5054 case BGP_EVPN_AD_ROUTE:
5055 if (bgp_evpn_type1_route_process(peer, afi, safi,
5056 withdraw ? NULL : attr, pnt,
5057 psize, addpath_id)) {
5058 flog_err(
5059 EC_BGP_PKT_PROCESS,
5060 "%u:%s - Error in processing EVPN type-1 NLRI size %d",
5061 peer->bgp->vrf_id, peer->host, psize);
5062 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE1_SIZE;
5063 }
5064 break;
5065
5066 case BGP_EVPN_IP_PREFIX_ROUTE:
5067 if (process_type5_route(peer, afi, safi,
5068 withdraw ? NULL : attr, pnt,
5069 psize, addpath_id)) {
5070 flog_err(
5071 EC_BGP_PKT_PROCESS,
5072 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
5073 peer->bgp->vrf_id, peer->host, psize);
5074 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE;
5075 }
5076 break;
5077
5078 default:
5079 break;
5080 }
5081 }
5082
5083 /* Packet length consistency check. */
5084 if (pnt != lim)
5085 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH;
5086
5087 return BGP_NLRI_PARSE_OK;
5088 }
5089
5090 /*
5091 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
5092 * The mapping will be used during route processing.
5093 * bgp_vrf: specific bgp vrf instance on which RT is configured
5094 */
5095 void bgp_evpn_map_vrf_to_its_rts(struct bgp *bgp_vrf)
5096 {
5097 uint32_t i = 0;
5098 struct ecommunity_val *eval = NULL;
5099 struct listnode *node = NULL, *nnode = NULL;
5100 struct ecommunity *ecom = NULL;
5101
5102 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
5103 for (i = 0; i < ecom->size; i++) {
5104 eval = (struct ecommunity_val *)(ecom->val
5105 + (i
5106 * ECOMMUNITY_SIZE));
5107 map_vrf_to_rt(bgp_vrf, eval);
5108 }
5109 }
5110 }
5111
5112 /*
5113 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
5114 */
5115 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *bgp_vrf)
5116 {
5117 uint32_t i;
5118 struct ecommunity_val *eval;
5119 struct listnode *node, *nnode;
5120 struct ecommunity *ecom;
5121
5122 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
5123 for (i = 0; i < ecom->size; i++) {
5124 struct vrf_irt_node *irt;
5125 struct ecommunity_val eval_tmp;
5126
5127 eval = (struct ecommunity_val *)(ecom->val
5128 + (i
5129 * ECOMMUNITY_SIZE));
5130 /* If using "automatic" RT, we only care about the
5131 * local-admin sub-field.
5132 * This is to facilitate using VNI as the RT for EBGP
5133 * peering too.
5134 */
5135 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
5136 if (!CHECK_FLAG(bgp_vrf->vrf_flags,
5137 BGP_VRF_IMPORT_RT_CFGD))
5138 mask_ecom_global_admin(&eval_tmp, eval);
5139
5140 irt = lookup_vrf_import_rt(&eval_tmp);
5141 if (irt)
5142 unmap_vrf_from_rt(bgp_vrf, irt);
5143 }
5144 }
5145 }
5146
5147
5148 /*
5149 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
5150 * The mapping will be used during route processing.
5151 */
5152 void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
5153 {
5154 uint32_t i;
5155 struct ecommunity_val *eval;
5156 struct listnode *node, *nnode;
5157 struct ecommunity *ecom;
5158
5159 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
5160 for (i = 0; i < ecom->size; i++) {
5161 eval = (struct ecommunity_val *)(ecom->val
5162 + (i
5163 * ECOMMUNITY_SIZE));
5164 map_vni_to_rt(bgp, vpn, eval);
5165 }
5166 }
5167 }
5168
5169 /*
5170 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
5171 */
5172 void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
5173 {
5174 uint32_t i;
5175 struct ecommunity_val *eval;
5176 struct listnode *node, *nnode;
5177 struct ecommunity *ecom;
5178
5179 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
5180 for (i = 0; i < ecom->size; i++) {
5181 struct irt_node *irt;
5182 struct ecommunity_val eval_tmp;
5183
5184 eval = (struct ecommunity_val *)(ecom->val
5185 + (i
5186 * ECOMMUNITY_SIZE));
5187 /* If using "automatic" RT, we only care about the
5188 * local-admin sub-field.
5189 * This is to facilitate using VNI as the RT for EBGP
5190 * peering too.
5191 */
5192 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
5193 if (!is_import_rt_configured(vpn))
5194 mask_ecom_global_admin(&eval_tmp, eval);
5195
5196 irt = lookup_import_rt(bgp, &eval_tmp);
5197 if (irt)
5198 unmap_vni_from_rt(bgp, vpn, irt);
5199 }
5200 }
5201 }
5202
5203 /*
5204 * Derive Import RT automatically for VNI and map VNI to RT.
5205 * The mapping will be used during route processing.
5206 */
5207 void bgp_evpn_derive_auto_rt_import(struct bgp *bgp, struct bgpevpn *vpn)
5208 {
5209 form_auto_rt(bgp, vpn->vni, vpn->import_rtl);
5210 UNSET_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD);
5211
5212 /* Map RT to VNI */
5213 bgp_evpn_map_vni_to_its_rts(bgp, vpn);
5214 }
5215
5216 /*
5217 * Derive Export RT automatically for VNI.
5218 */
5219 void bgp_evpn_derive_auto_rt_export(struct bgp *bgp, struct bgpevpn *vpn)
5220 {
5221 form_auto_rt(bgp, vpn->vni, vpn->export_rtl);
5222 UNSET_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD);
5223 }
5224
5225 /*
5226 * Derive RD automatically for VNI using passed information - it
5227 * is of the form RouterId:unique-id-for-vni.
5228 */
5229 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp)
5230 {
5231 if (is_vrf_rd_configured(bgp))
5232 return;
5233
5234 form_auto_rd(bgp->router_id, bgp->vrf_rd_id, &bgp->vrf_prd);
5235 }
5236
5237 /*
5238 * Derive RD automatically for VNI using passed information - it
5239 * is of the form RouterId:unique-id-for-vni.
5240 */
5241 void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn)
5242 {
5243 char buf[BGP_EVPN_PREFIX_RD_LEN];
5244
5245 vpn->prd.family = AF_UNSPEC;
5246 vpn->prd.prefixlen = 64;
5247 snprintfrr(buf, sizeof(buf), "%pI4:%hu", &bgp->router_id, vpn->rd_id);
5248 (void)str2prefix_rd(buf, &vpn->prd);
5249 UNSET_FLAG(vpn->flags, VNI_FLAG_RD_CFGD);
5250 }
5251
5252 /*
5253 * Lookup L3-VNI
5254 */
5255 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni)
5256 {
5257 struct list *inst = bm->bgp;
5258 struct listnode *node;
5259 struct bgp *bgp_vrf;
5260
5261 for (ALL_LIST_ELEMENTS_RO(inst, node, bgp_vrf)) {
5262 if (bgp_vrf->l3vni == vni)
5263 return true;
5264 }
5265
5266 return false;
5267 }
5268
5269 /*
5270 * Lookup VNI.
5271 */
5272 struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni)
5273 {
5274 struct bgpevpn *vpn;
5275 struct bgpevpn tmp;
5276
5277 memset(&tmp, 0, sizeof(tmp));
5278 tmp.vni = vni;
5279 vpn = hash_lookup(bgp->vnihash, &tmp);
5280 return vpn;
5281 }
5282
5283 /*
5284 * Create a new vpn - invoked upon configuration or zebra notification.
5285 */
5286 struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
5287 struct in_addr originator_ip,
5288 vrf_id_t tenant_vrf_id,
5289 struct in_addr mcast_grp,
5290 ifindex_t svi_ifindex)
5291 {
5292 struct bgpevpn *vpn;
5293
5294 vpn = XCALLOC(MTYPE_BGP_EVPN, sizeof(struct bgpevpn));
5295
5296 /* Set values - RD and RT set to defaults. */
5297 vpn->vni = vni;
5298 vpn->originator_ip = originator_ip;
5299 vpn->tenant_vrf_id = tenant_vrf_id;
5300 vpn->mcast_grp = mcast_grp;
5301 vpn->svi_ifindex = svi_ifindex;
5302
5303 /* Initialize route-target import and export lists */
5304 vpn->import_rtl = list_new();
5305 vpn->import_rtl->cmp =
5306 (int (*)(void *, void *))bgp_evpn_route_target_cmp;
5307 vpn->import_rtl->del = bgp_evpn_xxport_delete_ecomm;
5308 vpn->export_rtl = list_new();
5309 vpn->export_rtl->cmp =
5310 (int (*)(void *, void *))bgp_evpn_route_target_cmp;
5311 vpn->export_rtl->del = bgp_evpn_xxport_delete_ecomm;
5312 bf_assign_index(bm->rd_idspace, vpn->rd_id);
5313 derive_rd_rt_for_vni(bgp, vpn);
5314
5315 /* Initialize EVPN route table. */
5316 vpn->route_table = bgp_table_init(bgp, AFI_L2VPN, SAFI_EVPN);
5317
5318 /* Add to hash */
5319 (void)hash_get(bgp->vnihash, vpn, hash_alloc_intern);
5320
5321 bgp_evpn_remote_ip_hash_init(vpn);
5322 bgp_evpn_link_to_vni_svi_hash(bgp, vpn);
5323
5324 /* add to l2vni list on corresponding vrf */
5325 bgpevpn_link_to_l3vni(vpn);
5326
5327 bgp_evpn_vni_es_init(vpn);
5328
5329 QOBJ_REG(vpn, bgpevpn);
5330 return vpn;
5331 }
5332
5333 /*
5334 * Free a given VPN - called in multiple scenarios such as zebra
5335 * notification, configuration being deleted, advertise-all-vni disabled etc.
5336 * This just frees appropriate memory, caller should have taken other
5337 * needed actions.
5338 */
5339 void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn)
5340 {
5341 bgp_evpn_remote_ip_hash_destroy(vpn);
5342 bgp_evpn_vni_es_cleanup(vpn);
5343 bgpevpn_unlink_from_l3vni(vpn);
5344 bgp_table_unlock(vpn->route_table);
5345 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
5346 list_delete(&vpn->import_rtl);
5347 list_delete(&vpn->export_rtl);
5348 bf_release_index(bm->rd_idspace, vpn->rd_id);
5349 hash_release(bgp->vni_svi_hash, vpn);
5350 hash_release(bgp->vnihash, vpn);
5351 QOBJ_UNREG(vpn);
5352 XFREE(MTYPE_BGP_EVPN, vpn);
5353 }
5354
5355 /*
5356 * Import evpn route from global table to VNI/VRF/ESI.
5357 */
5358 int bgp_evpn_import_route(struct bgp *bgp, afi_t afi, safi_t safi,
5359 const struct prefix *p, struct bgp_path_info *pi)
5360 {
5361 return install_uninstall_evpn_route(bgp, afi, safi, p, pi, 1);
5362 }
5363
5364 /*
5365 * Unimport evpn route from VNI/VRF/ESI.
5366 */
5367 int bgp_evpn_unimport_route(struct bgp *bgp, afi_t afi, safi_t safi,
5368 const struct prefix *p, struct bgp_path_info *pi)
5369 {
5370 return install_uninstall_evpn_route(bgp, afi, safi, p, pi, 0);
5371 }
5372
5373 /* filter routes which have martian next hops */
5374 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp *bgp)
5375 {
5376 afi_t afi;
5377 safi_t safi;
5378 struct bgp_dest *rd_dest, *dest;
5379 struct bgp_table *table;
5380 struct bgp_path_info *pi;
5381
5382 afi = AFI_L2VPN;
5383 safi = SAFI_EVPN;
5384
5385 /* Walk entire global routing table and evaluate routes which could be
5386 * imported into this VPN. Note that we cannot just look at the routes
5387 * for the VNI's RD -
5388 * remote routes applicable for this VNI could have any RD.
5389 */
5390 /* EVPN routes are a 2-level table. */
5391 for (rd_dest = bgp_table_top(bgp->rib[afi][safi]); rd_dest;
5392 rd_dest = bgp_route_next(rd_dest)) {
5393 table = bgp_dest_get_bgp_table_info(rd_dest);
5394 if (!table)
5395 continue;
5396
5397 for (dest = bgp_table_top(table); dest;
5398 dest = bgp_route_next(dest)) {
5399
5400 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
5401 pi = pi->next) {
5402
5403 /* Consider "valid" remote routes applicable for
5404 * this VNI. */
5405 if (!(pi->type == ZEBRA_ROUTE_BGP
5406 && pi->sub_type == BGP_ROUTE_NORMAL))
5407 continue;
5408 if (bgp_nexthop_self(bgp, afi, pi->type,
5409 pi->sub_type, pi->attr,
5410 dest)) {
5411 const struct prefix *p =
5412 bgp_dest_get_prefix(dest);
5413
5414 if (bgp_debug_update(pi->peer, p, NULL,
5415 1)) {
5416 char attr_str[BUFSIZ] = {0};
5417
5418 bgp_dump_attr(pi->attr,
5419 attr_str,
5420 sizeof(attr_str));
5421
5422 zlog_debug(
5423 "%u: prefix %pBD with attr %s - DENIED due to martian or self nexthop",
5424 bgp->vrf_id, dest,
5425 attr_str);
5426 }
5427 bgp_evpn_unimport_route(bgp, afi, safi,
5428 p, pi);
5429
5430 bgp_rib_remove(dest, pi, pi->peer, afi,
5431 safi);
5432 }
5433 }
5434 }
5435 }
5436
5437 return 0;
5438 }
5439
5440 /*
5441 * Handle del of a local MACIP.
5442 */
5443 int bgp_evpn_local_macip_del(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
5444 struct ipaddr *ip, int state)
5445 {
5446 struct bgpevpn *vpn;
5447 struct prefix_evpn p;
5448 struct bgp_dest *dest;
5449
5450 /* Lookup VNI hash - should exist. */
5451 vpn = bgp_evpn_lookup_vni(bgp, vni);
5452 if (!vpn || !is_vni_live(vpn)) {
5453 flog_warn(EC_BGP_EVPN_VPN_VNI,
5454 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5455 bgp->vrf_id, vni, vpn ? "not live" : "not found");
5456 return -1;
5457 }
5458
5459 build_evpn_type2_prefix(&p, mac, ip);
5460 if (state == ZEBRA_NEIGH_ACTIVE) {
5461 /* Remove EVPN type-2 route and schedule for processing. */
5462 delete_evpn_route(bgp, vpn, &p);
5463 } else {
5464 /* Re-instate the current remote best path if any */
5465 dest = bgp_node_lookup(vpn->route_table, (struct prefix *)&p);
5466 if (dest) {
5467 evpn_zebra_reinstall_best_route(bgp, vpn, dest);
5468 bgp_dest_unlock_node(dest);
5469 }
5470 }
5471
5472 return 0;
5473 }
5474
5475 /*
5476 * Handle add of a local MACIP.
5477 */
5478 int bgp_evpn_local_macip_add(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
5479 struct ipaddr *ip, uint8_t flags, uint32_t seq, esi_t *esi)
5480 {
5481 struct bgpevpn *vpn;
5482 struct prefix_evpn p;
5483
5484 /* Lookup VNI hash - should exist. */
5485 vpn = bgp_evpn_lookup_vni(bgp, vni);
5486 if (!vpn || !is_vni_live(vpn)) {
5487 flog_warn(EC_BGP_EVPN_VPN_VNI,
5488 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5489 bgp->vrf_id, vni, vpn ? "not live" : "not found");
5490 return -1;
5491 }
5492
5493 /* Create EVPN type-2 route and schedule for processing. */
5494 build_evpn_type2_prefix(&p, mac, ip);
5495 if (update_evpn_route(bgp, vpn, &p, flags, seq, esi)) {
5496 flog_err(
5497 EC_BGP_EVPN_ROUTE_CREATE,
5498 "%u:Failed to create Type-2 route, VNI %u %s MAC %pEA IP %pIA (flags: 0x%x)",
5499 bgp->vrf_id, vpn->vni,
5500 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY)
5501 ? "sticky gateway"
5502 : "",
5503 mac, ip, flags);
5504 return -1;
5505 }
5506
5507 return 0;
5508 }
5509
5510 static void link_l2vni_hash_to_l3vni(struct hash_bucket *bucket,
5511 struct bgp *bgp_vrf)
5512 {
5513 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
5514 struct bgp *bgp_evpn = NULL;
5515
5516 bgp_evpn = bgp_get_evpn();
5517 assert(bgp_evpn);
5518
5519 if (vpn->tenant_vrf_id == bgp_vrf->vrf_id)
5520 bgpevpn_link_to_l3vni(vpn);
5521 }
5522
5523 int bgp_evpn_local_l3vni_add(vni_t l3vni, vrf_id_t vrf_id,
5524 struct ethaddr *svi_rmac,
5525 struct ethaddr *vrr_rmac,
5526 struct in_addr originator_ip, int filter,
5527 ifindex_t svi_ifindex,
5528 bool is_anycast_mac)
5529 {
5530 struct bgp *bgp_vrf = NULL; /* bgp VRF instance */
5531 struct bgp *bgp_evpn = NULL; /* EVPN bgp instance */
5532 struct listnode *node = NULL;
5533 struct bgpevpn *vpn = NULL;
5534 as_t as = 0;
5535
5536 /* get the EVPN instance - required to get the AS number for VRF
5537 * auto-creatio
5538 */
5539 bgp_evpn = bgp_get_evpn();
5540 if (!bgp_evpn) {
5541 flog_err(
5542 EC_BGP_NO_DFLT,
5543 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5544 l3vni);
5545 return -1;
5546 }
5547 as = bgp_evpn->as;
5548
5549 /* if the BGP vrf instance doesn't exist - create one */
5550 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
5551 if (!bgp_vrf) {
5552
5553 int ret = 0;
5554
5555 ret = bgp_get_vty(&bgp_vrf, &as, vrf_id_to_name(vrf_id),
5556 vrf_id == VRF_DEFAULT
5557 ? BGP_INSTANCE_TYPE_DEFAULT
5558 : BGP_INSTANCE_TYPE_VRF);
5559 switch (ret) {
5560 case BGP_ERR_AS_MISMATCH:
5561 flog_err(EC_BGP_EVPN_AS_MISMATCH,
5562 "BGP instance is already running; AS is %u",
5563 as);
5564 return -1;
5565 case BGP_ERR_INSTANCE_MISMATCH:
5566 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH,
5567 "BGP instance type mismatch");
5568 return -1;
5569 }
5570
5571 /* mark as auto created */
5572 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO);
5573 }
5574
5575 /* associate the vrf with l3vni and related parameters */
5576 bgp_vrf->l3vni = l3vni;
5577 bgp_vrf->originator_ip = originator_ip;
5578 bgp_vrf->l3vni_svi_ifindex = svi_ifindex;
5579 bgp_vrf->evpn_info->is_anycast_mac = is_anycast_mac;
5580
5581 /* copy anycast MAC from VRR MAC */
5582 memcpy(&bgp_vrf->rmac, vrr_rmac, ETH_ALEN);
5583 /* copy sys RMAC from SVI MAC */
5584 memcpy(&bgp_vrf->evpn_info->pip_rmac_zebra, svi_rmac, ETH_ALEN);
5585 /* PIP user configured mac is not present use svi mac as sys mac */
5586 if (is_zero_mac(&bgp_vrf->evpn_info->pip_rmac_static))
5587 memcpy(&bgp_vrf->evpn_info->pip_rmac, svi_rmac, ETH_ALEN);
5588
5589 if (bgp_debug_zebra(NULL))
5590 zlog_debug(
5591 "VRF %s vni %u pip %s RMAC %pEA sys RMAC %pEA static RMAC %pEA is_anycast_mac %s",
5592 vrf_id_to_name(bgp_vrf->vrf_id), bgp_vrf->l3vni,
5593 bgp_vrf->evpn_info->advertise_pip ? "enable"
5594 : "disable",
5595 &bgp_vrf->rmac, &bgp_vrf->evpn_info->pip_rmac,
5596 &bgp_vrf->evpn_info->pip_rmac_static,
5597 is_anycast_mac ? "Enable" : "Disable");
5598
5599 /* set the right filter - are we using l3vni only for prefix routes? */
5600 if (filter) {
5601 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
5602
5603 /*
5604 * VNI_FLAG_USE_TWO_LABELS flag for linked L2VNIs should not be
5605 * set before linking vrf to L3VNI. Thus, no need to clear
5606 * that explicitly.
5607 */
5608 } else {
5609 UNSET_FLAG(bgp_vrf->vrf_flags,
5610 BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
5611
5612 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) {
5613 if (!CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)) {
5614
5615 /*
5616 * If we are flapping VNI_FLAG_USE_TWO_LABELS
5617 * flag, update all MACIP routes in this VNI
5618 */
5619 SET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
5620 update_all_type2_routes(bgp_evpn, vpn);
5621 }
5622 }
5623 }
5624
5625 /* Map auto derive or configured RTs */
5626 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
5627 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
5628 else
5629 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
5630
5631 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD))
5632 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
5633
5634 /* auto derive RD */
5635 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
5636
5637 /* link all corresponding l2vnis */
5638 hash_iterate(bgp_evpn->vnihash,
5639 (void (*)(struct hash_bucket *,
5640 void *))link_l2vni_hash_to_l3vni,
5641 bgp_vrf);
5642
5643 /* Only update all corresponding type-2 routes if we are advertising two
5644 * labels along with type-2 routes
5645 */
5646 if (!filter)
5647 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
5648 update_routes_for_vni(bgp_evpn, vpn);
5649
5650 /* advertise type-5 routes if needed */
5651 update_advertise_vrf_routes(bgp_vrf);
5652
5653 /* install all remote routes belonging to this l3vni into correspondng
5654 * vrf */
5655 install_routes_for_vrf(bgp_vrf);
5656
5657 return 0;
5658 }
5659
5660 int bgp_evpn_local_l3vni_del(vni_t l3vni, vrf_id_t vrf_id)
5661 {
5662 struct bgp *bgp_vrf = NULL; /* bgp vrf instance */
5663 struct bgp *bgp_evpn = NULL; /* EVPN bgp instance */
5664 struct listnode *node = NULL;
5665 struct listnode *next = NULL;
5666 struct bgpevpn *vpn = NULL;
5667
5668 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
5669 if (!bgp_vrf) {
5670 flog_err(
5671 EC_BGP_NO_DFLT,
5672 "Cannot process L3VNI %u Del - Could not find BGP instance",
5673 l3vni);
5674 return -1;
5675 }
5676
5677 bgp_evpn = bgp_get_evpn();
5678 if (!bgp_evpn) {
5679 flog_err(
5680 EC_BGP_NO_DFLT,
5681 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5682 l3vni);
5683 return -1;
5684 }
5685
5686 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5687 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5688 * routes. This will uninstalling the routes from zebra and decremnt the
5689 * bgp info count.
5690 */
5691 uninstall_routes_for_vrf(bgp_vrf);
5692
5693 /* delete/withdraw all type-5 routes */
5694 delete_withdraw_vrf_routes(bgp_vrf);
5695
5696 /* remove the l3vni from vrf instance */
5697 bgp_vrf->l3vni = 0;
5698
5699 /* remove the Rmac from the BGP vrf */
5700 memset(&bgp_vrf->rmac, 0, sizeof(struct ethaddr));
5701 memset(&bgp_vrf->evpn_info->pip_rmac_zebra, 0, ETH_ALEN);
5702 if (is_zero_mac(&bgp_vrf->evpn_info->pip_rmac_static) &&
5703 !is_zero_mac(&bgp_vrf->evpn_info->pip_rmac))
5704 memset(&bgp_vrf->evpn_info->pip_rmac, 0, ETH_ALEN);
5705
5706 /* remove default import RT or Unmap non-default import RT */
5707 if (!list_isempty(bgp_vrf->vrf_import_rtl)) {
5708 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
5709 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
5710 list_delete_all_node(bgp_vrf->vrf_import_rtl);
5711 }
5712
5713 /* remove default export RT */
5714 if (!list_isempty(bgp_vrf->vrf_export_rtl) &&
5715 !CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD)) {
5716 list_delete_all_node(bgp_vrf->vrf_export_rtl);
5717 }
5718
5719 /* update all corresponding local mac-ip routes */
5720 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY)) {
5721 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) {
5722 UNSET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
5723 update_routes_for_vni(bgp_evpn, vpn);
5724 }
5725 }
5726
5727 /* If any L2VNIs point to this instance, unlink them. */
5728 for (ALL_LIST_ELEMENTS(bgp_vrf->l2vnis, node, next, vpn))
5729 bgpevpn_unlink_from_l3vni(vpn);
5730
5731 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
5732
5733 /* Delete the instance if it was autocreated */
5734 if (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO))
5735 bgp_delete(bgp_vrf);
5736
5737 return 0;
5738 }
5739
5740 /*
5741 * Handle del of a local VNI.
5742 */
5743 int bgp_evpn_local_vni_del(struct bgp *bgp, vni_t vni)
5744 {
5745 struct bgpevpn *vpn;
5746
5747 /* Locate VNI hash */
5748 vpn = bgp_evpn_lookup_vni(bgp, vni);
5749 if (!vpn) {
5750 if (bgp_debug_zebra(NULL))
5751 flog_warn(
5752 EC_BGP_EVPN_VPN_VNI,
5753 "%u: VNI hash entry for VNI %u not found at DEL",
5754 bgp->vrf_id, vni);
5755 return 0;
5756 }
5757
5758 /* Remove all local EVPN routes and schedule for processing (to
5759 * withdraw from peers).
5760 */
5761 delete_routes_for_vni(bgp, vpn);
5762
5763 bgp_evpn_unlink_from_vni_svi_hash(bgp, vpn);
5764
5765 vpn->svi_ifindex = 0;
5766 /*
5767 * tunnel is no longer active, del tunnel ip address from tip_hash
5768 */
5769 bgp_tip_del(bgp, &vpn->originator_ip);
5770
5771 /* Clear "live" flag and see if hash needs to be freed. */
5772 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
5773 if (!is_vni_configured(vpn))
5774 bgp_evpn_free(bgp, vpn);
5775
5776 return 0;
5777 }
5778
5779 /*
5780 * Handle add (or update) of a local VNI. The VNI changes we care
5781 * about are for the local-tunnel-ip and the (tenant) VRF.
5782 */
5783 int bgp_evpn_local_vni_add(struct bgp *bgp, vni_t vni,
5784 struct in_addr originator_ip,
5785 vrf_id_t tenant_vrf_id,
5786 struct in_addr mcast_grp,
5787 ifindex_t svi_ifindex)
5788 {
5789 struct bgpevpn *vpn;
5790 struct prefix_evpn p;
5791
5792 /* Lookup VNI. If present and no change, exit. */
5793 vpn = bgp_evpn_lookup_vni(bgp, vni);
5794 if (vpn) {
5795
5796 if (is_vni_live(vpn)
5797 && IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip)
5798 && IPV4_ADDR_SAME(&vpn->mcast_grp, &mcast_grp)
5799 && vpn->tenant_vrf_id == tenant_vrf_id
5800 && vpn->svi_ifindex == svi_ifindex)
5801 /* Probably some other param has changed that we don't
5802 * care about. */
5803 return 0;
5804
5805 bgp_evpn_mcast_grp_change(bgp, vpn, mcast_grp);
5806
5807 if (vpn->svi_ifindex != svi_ifindex) {
5808
5809 /*
5810 * Unresolve all the gateway IP nexthops for this VNI
5811 * for old SVI
5812 */
5813 bgp_evpn_remote_ip_hash_iterate(
5814 vpn,
5815 (void (*)(struct hash_bucket *, void *))
5816 bgp_evpn_remote_ip_hash_unlink_nexthop,
5817 vpn);
5818 bgp_evpn_unlink_from_vni_svi_hash(bgp, vpn);
5819 vpn->svi_ifindex = svi_ifindex;
5820 bgp_evpn_link_to_vni_svi_hash(bgp, vpn);
5821
5822 /*
5823 * Resolve all the gateway IP nexthops for this VNI
5824 * for new SVI
5825 */
5826 bgp_evpn_remote_ip_hash_iterate(
5827 vpn,
5828 (void (*)(struct hash_bucket *, void *))
5829 bgp_evpn_remote_ip_hash_link_nexthop,
5830 vpn);
5831 }
5832
5833 /* Update tenant_vrf_id if it has changed. */
5834 if (vpn->tenant_vrf_id != tenant_vrf_id) {
5835
5836 /*
5837 * Unresolve all the gateway IP nexthops for this VNI
5838 * in old tenant vrf
5839 */
5840 bgp_evpn_remote_ip_hash_iterate(
5841 vpn,
5842 (void (*)(struct hash_bucket *, void *))
5843 bgp_evpn_remote_ip_hash_unlink_nexthop,
5844 vpn);
5845 bgpevpn_unlink_from_l3vni(vpn);
5846 vpn->tenant_vrf_id = tenant_vrf_id;
5847 bgpevpn_link_to_l3vni(vpn);
5848
5849 /*
5850 * Resolve all the gateway IP nexthops for this VNI
5851 * in new tenant vrf
5852 */
5853 bgp_evpn_remote_ip_hash_iterate(
5854 vpn,
5855 (void (*)(struct hash_bucket *, void *))
5856 bgp_evpn_remote_ip_hash_link_nexthop,
5857 vpn);
5858 }
5859
5860 /* If tunnel endpoint IP has changed, update (and delete prior
5861 * type-3 route, if needed.)
5862 */
5863 if (!IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip))
5864 handle_tunnel_ip_change(bgp, vpn, originator_ip);
5865
5866 /* Update all routes with new endpoint IP and/or export RT
5867 * for VRFs
5868 */
5869 if (is_vni_live(vpn))
5870 update_routes_for_vni(bgp, vpn);
5871 } else {
5872 /* Create or update as appropriate. */
5873 vpn = bgp_evpn_new(bgp, vni, originator_ip, tenant_vrf_id,
5874 mcast_grp, svi_ifindex);
5875 }
5876
5877 /* if the VNI is live already, there is nothing more to do */
5878 if (is_vni_live(vpn))
5879 return 0;
5880
5881 /* Mark as "live" */
5882 SET_FLAG(vpn->flags, VNI_FLAG_LIVE);
5883
5884 /* tunnel is now active, add tunnel-ip to db */
5885 bgp_tip_add(bgp, &originator_ip);
5886
5887 /* filter routes as nexthop database has changed */
5888 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
5889
5890 /*
5891 * Create EVPN type-3 route and schedule for processing.
5892 *
5893 * RT-3 only if doing head-end replication
5894 */
5895 if (bgp_evpn_vni_flood_mode_get(bgp, vpn)
5896 == VXLAN_FLOOD_HEAD_END_REPL) {
5897 build_evpn_type3_prefix(&p, vpn->originator_ip);
5898 if (update_evpn_route(bgp, vpn, &p, 0, 0, NULL)) {
5899 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
5900 "%u: Type3 route creation failure for VNI %u",
5901 bgp->vrf_id, vni);
5902 return -1;
5903 }
5904 }
5905
5906 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5907 * VNI,
5908 * install them.
5909 */
5910 install_routes_for_vni(bgp, vpn);
5911
5912 /* If we are advertising gateway mac-ip
5913 It needs to be conveyed again to zebra */
5914 bgp_zebra_advertise_gw_macip(bgp, vpn->advertise_gw_macip, vpn->vni);
5915
5916 /* advertise svi mac-ip knob to zebra */
5917 bgp_zebra_advertise_svi_macip(bgp, vpn->advertise_svi_macip, vpn->vni);
5918
5919 return 0;
5920 }
5921
5922 /*
5923 * Handle change in setting for BUM handling. The supported values
5924 * are head-end replication and dropping all BUM packets. Any change
5925 * should be registered with zebra. Also, if doing head-end replication,
5926 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5927 * to be dropped, the RT-3s must be withdrawn.
5928 */
5929 void bgp_evpn_flood_control_change(struct bgp *bgp)
5930 {
5931 zlog_info("L2VPN EVPN BUM handling is %s",
5932 bgp->vxlan_flood_ctrl == VXLAN_FLOOD_HEAD_END_REPL ?
5933 "Flooding" : "Flooding Disabled");
5934
5935 bgp_zebra_vxlan_flood_control(bgp, bgp->vxlan_flood_ctrl);
5936 if (bgp->vxlan_flood_ctrl == VXLAN_FLOOD_HEAD_END_REPL)
5937 hash_iterate(bgp->vnihash, create_advertise_type3, bgp);
5938 else if (bgp->vxlan_flood_ctrl == VXLAN_FLOOD_DISABLED)
5939 hash_iterate(bgp->vnihash, delete_withdraw_type3, bgp);
5940 }
5941
5942 /*
5943 * Cleanup EVPN information on disable - Need to delete and withdraw
5944 * EVPN routes from peers.
5945 */
5946 void bgp_evpn_cleanup_on_disable(struct bgp *bgp)
5947 {
5948 hash_iterate(bgp->vnihash, (void (*)(struct hash_bucket *,
5949 void *))cleanup_vni_on_disable,
5950 bgp);
5951 }
5952
5953 /*
5954 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5955 * BGP instance (default) is being freed.
5956 */
5957 void bgp_evpn_cleanup(struct bgp *bgp)
5958 {
5959 hash_iterate(bgp->vnihash,
5960 (void (*)(struct hash_bucket *, void *))free_vni_entry,
5961 bgp);
5962
5963 hash_free(bgp->import_rt_hash);
5964 bgp->import_rt_hash = NULL;
5965
5966 hash_free(bgp->vrf_import_rt_hash);
5967 bgp->vrf_import_rt_hash = NULL;
5968
5969 hash_free(bgp->vni_svi_hash);
5970 bgp->vni_svi_hash = NULL;
5971 hash_free(bgp->vnihash);
5972 bgp->vnihash = NULL;
5973
5974 list_delete(&bgp->vrf_import_rtl);
5975 list_delete(&bgp->vrf_export_rtl);
5976 list_delete(&bgp->l2vnis);
5977 }
5978
5979 /*
5980 * Initialization for EVPN
5981 * Create
5982 * VNI hash table
5983 * hash for RT to VNI
5984 */
5985 void bgp_evpn_init(struct bgp *bgp)
5986 {
5987 bgp->vnihash =
5988 hash_create(vni_hash_key_make, vni_hash_cmp, "BGP VNI Hash");
5989 bgp->vni_svi_hash =
5990 hash_create(vni_svi_hash_key_make, vni_svi_hash_cmp,
5991 "BGP VNI hash based on SVI ifindex");
5992 bgp->import_rt_hash =
5993 hash_create(import_rt_hash_key_make, import_rt_hash_cmp,
5994 "BGP Import RT Hash");
5995 bgp->vrf_import_rt_hash =
5996 hash_create(vrf_import_rt_hash_key_make, vrf_import_rt_hash_cmp,
5997 "BGP VRF Import RT Hash");
5998 bgp->vrf_import_rtl = list_new();
5999 bgp->vrf_import_rtl->cmp =
6000 (int (*)(void *, void *))bgp_evpn_route_target_cmp;
6001 bgp->vrf_import_rtl->del = bgp_evpn_xxport_delete_ecomm;
6002 bgp->vrf_export_rtl = list_new();
6003 bgp->vrf_export_rtl->cmp =
6004 (int (*)(void *, void *))bgp_evpn_route_target_cmp;
6005 bgp->vrf_export_rtl->del = bgp_evpn_xxport_delete_ecomm;
6006 bgp->l2vnis = list_new();
6007 bgp->l2vnis->cmp = vni_list_cmp;
6008 /* By default Duplicate Address Dection is enabled.
6009 * Max-moves (N) 5, detection time (M) 180
6010 * default action is warning-only
6011 * freeze action permanently freezes address,
6012 * and freeze time (auto-recovery) is disabled.
6013 */
6014 if (bgp->evpn_info) {
6015 bgp->evpn_info->dup_addr_detect = true;
6016 bgp->evpn_info->dad_time = EVPN_DAD_DEFAULT_TIME;
6017 bgp->evpn_info->dad_max_moves = EVPN_DAD_DEFAULT_MAX_MOVES;
6018 bgp->evpn_info->dad_freeze = false;
6019 bgp->evpn_info->dad_freeze_time = 0;
6020 /* Initialize zebra vxlan */
6021 bgp_zebra_dup_addr_detection(bgp);
6022 /* Enable PIP feature by default for bgp vrf instance */
6023 if (bgp->inst_type == BGP_INSTANCE_TYPE_VRF) {
6024 struct bgp *bgp_default;
6025
6026 bgp->evpn_info->advertise_pip = true;
6027 bgp_default = bgp_get_default();
6028 if (bgp_default)
6029 bgp->evpn_info->pip_ip = bgp_default->router_id;
6030 }
6031 }
6032
6033 /* Default BUM handling is to do head-end replication. */
6034 bgp->vxlan_flood_ctrl = VXLAN_FLOOD_HEAD_END_REPL;
6035
6036 bgp_evpn_nh_init(bgp);
6037 }
6038
6039 void bgp_evpn_vrf_delete(struct bgp *bgp_vrf)
6040 {
6041 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
6042 bgp_evpn_nh_finish(bgp_vrf);
6043 }
6044
6045 /*
6046 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
6047 */
6048 int bgp_evpn_get_type5_prefixlen(const struct prefix *pfx)
6049 {
6050 struct prefix_evpn *evp = (struct prefix_evpn *)pfx;
6051
6052 if (!pfx || pfx->family != AF_EVPN)
6053 return 0;
6054
6055 if (evp->prefix.route_type != BGP_EVPN_IP_PREFIX_ROUTE)
6056 return 0;
6057
6058 return evp->prefix.prefix_addr.ip_prefix_length;
6059 }
6060
6061 /*
6062 * Should we register nexthop for this EVPN prefix for nexthop tracking?
6063 */
6064 bool bgp_evpn_is_prefix_nht_supported(const struct prefix *pfx)
6065 {
6066 struct prefix_evpn *evp = (struct prefix_evpn *)pfx;
6067
6068 /*
6069 * EVPN routes should be marked as valid only if the nexthop is
6070 * reachable. Only if this happens, the route should be imported
6071 * (into VNI or VRF routing tables) and/or advertised.
6072 * Note: This is currently applied for EVPN type-1, type-2,
6073 * type-3, type-4 and type-5 routes.
6074 * It may be tweaked later on for other routes, or
6075 * even removed completely when all routes are handled.
6076 */
6077 if (pfx && pfx->family == AF_EVPN
6078 && (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
6079 || evp->prefix.route_type == BGP_EVPN_AD_ROUTE
6080 || evp->prefix.route_type == BGP_EVPN_ES_ROUTE
6081 || evp->prefix.route_type == BGP_EVPN_IMET_ROUTE
6082 || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
6083 return true;
6084
6085 return false;
6086 }
6087
6088 static void *bgp_evpn_remote_ip_hash_alloc(void *p)
6089 {
6090 const struct evpn_remote_ip *key = (const struct evpn_remote_ip *)p;
6091 struct evpn_remote_ip *ip;
6092
6093 ip = XMALLOC(MTYPE_EVPN_REMOTE_IP, sizeof(struct evpn_remote_ip));
6094 *ip = *key;
6095 ip->macip_path_list = list_new();
6096
6097 return ip;
6098 }
6099
6100 static unsigned int bgp_evpn_remote_ip_hash_key_make(const void *p)
6101 {
6102 const struct evpn_remote_ip *ip = p;
6103 const struct ipaddr *addr = &ip->addr;
6104
6105 if (IS_IPADDR_V4(addr))
6106 return jhash_1word(addr->ipaddr_v4.s_addr, 0);
6107
6108 return jhash2(addr->ipaddr_v6.s6_addr32,
6109 array_size(addr->ipaddr_v6.s6_addr32), 0);
6110 }
6111
6112 static bool bgp_evpn_remote_ip_hash_cmp(const void *p1, const void *p2)
6113 {
6114 const struct evpn_remote_ip *ip1 = p1;
6115 const struct evpn_remote_ip *ip2 = p2;
6116
6117 return !ipaddr_cmp(&ip1->addr, &ip2->addr);
6118 }
6119
6120 static void bgp_evpn_remote_ip_hash_init(struct bgpevpn *vpn)
6121 {
6122 if (!evpn_resolve_overlay_index())
6123 return;
6124
6125 vpn->remote_ip_hash = hash_create(bgp_evpn_remote_ip_hash_key_make,
6126 bgp_evpn_remote_ip_hash_cmp,
6127 "BGP EVPN remote IP hash");
6128 }
6129
6130 static void bgp_evpn_remote_ip_hash_free(struct hash_bucket *bucket, void *args)
6131 {
6132 struct evpn_remote_ip *ip = (struct evpn_remote_ip *)bucket->data;
6133 struct bgpevpn *vpn = (struct bgpevpn *)args;
6134
6135 bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, false);
6136
6137 list_delete(&ip->macip_path_list);
6138
6139 hash_release(vpn->remote_ip_hash, ip);
6140 XFREE(MTYPE_EVPN_REMOTE_IP, ip);
6141 }
6142
6143 static void bgp_evpn_remote_ip_hash_destroy(struct bgpevpn *vpn)
6144 {
6145 if (!evpn_resolve_overlay_index() || vpn->remote_ip_hash == NULL)
6146 return;
6147
6148 hash_iterate(vpn->remote_ip_hash,
6149 (void (*)(struct hash_bucket *, void *))bgp_evpn_remote_ip_hash_free,
6150 vpn);
6151
6152 hash_free(vpn->remote_ip_hash);
6153 vpn->remote_ip_hash = NULL;
6154 }
6155
6156 /* Add a remote MAC/IP route to hash table */
6157 static void bgp_evpn_remote_ip_hash_add(struct bgpevpn *vpn,
6158 struct bgp_path_info *pi)
6159 {
6160 struct evpn_remote_ip tmp;
6161 struct evpn_remote_ip *ip;
6162 struct prefix_evpn *evp;
6163
6164 if (!evpn_resolve_overlay_index())
6165 return;
6166
6167 if (pi->type != ZEBRA_ROUTE_BGP || pi->sub_type != BGP_ROUTE_IMPORTED
6168 || !CHECK_FLAG(pi->flags, BGP_PATH_VALID))
6169 return;
6170
6171 evp = (struct prefix_evpn *)&pi->net->p;
6172
6173 if (evp->family != AF_EVPN
6174 || evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE
6175 || is_evpn_prefix_ipaddr_none(evp))
6176 return;
6177
6178 tmp.addr = evp->prefix.macip_addr.ip;
6179 ip = hash_lookup(vpn->remote_ip_hash, &tmp);
6180 if (ip) {
6181 if (listnode_lookup(ip->macip_path_list, pi) != NULL)
6182 return;
6183 (void)listnode_add(ip->macip_path_list, pi);
6184 return;
6185 }
6186
6187 ip = hash_get(vpn->remote_ip_hash, &tmp, bgp_evpn_remote_ip_hash_alloc);
6188 (void)listnode_add(ip->macip_path_list, pi);
6189
6190 bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, true);
6191 }
6192
6193 /* Delete a remote MAC/IP route from hash table */
6194 static void bgp_evpn_remote_ip_hash_del(struct bgpevpn *vpn,
6195 struct bgp_path_info *pi)
6196 {
6197 struct evpn_remote_ip tmp;
6198 struct evpn_remote_ip *ip;
6199 struct prefix_evpn *evp;
6200
6201 if (!evpn_resolve_overlay_index())
6202 return;
6203
6204 evp = (struct prefix_evpn *)&pi->net->p;
6205
6206 if (evp->family != AF_EVPN
6207 || evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE
6208 || is_evpn_prefix_ipaddr_none(evp))
6209 return;
6210
6211 tmp.addr = evp->prefix.macip_addr.ip;
6212 ip = hash_lookup(vpn->remote_ip_hash, &tmp);
6213 if (ip == NULL)
6214 return;
6215
6216 listnode_delete(ip->macip_path_list, pi);
6217
6218 if (ip->macip_path_list->count == 0) {
6219 bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, false);
6220 hash_release(vpn->remote_ip_hash, ip);
6221 XFREE(MTYPE_EVPN_REMOTE_IP, ip);
6222 }
6223 }
6224
6225 static void bgp_evpn_remote_ip_hash_iterate(struct bgpevpn *vpn,
6226 void (*func)(struct hash_bucket *,
6227 void *),
6228 void *arg)
6229 {
6230 if (!evpn_resolve_overlay_index())
6231 return;
6232
6233 hash_iterate(vpn->remote_ip_hash, func, arg);
6234 }
6235
6236 static void show_remote_ip_entry(struct hash_bucket *bucket, void *args)
6237 {
6238 char buf[INET6_ADDRSTRLEN];
6239 char buf2[EVPN_ROUTE_STRLEN];
6240 struct prefix_evpn *evp;
6241
6242 struct listnode *node = NULL;
6243 struct bgp_path_info *pi = NULL;
6244 struct vty *vty = (struct vty *)args;
6245 struct evpn_remote_ip *ip = (struct evpn_remote_ip *)bucket->data;
6246
6247 vty_out(vty, " Remote IP: %s\n",
6248 ipaddr2str(&ip->addr, buf, sizeof(buf)));
6249 vty_out(vty, " Linked MAC/IP routes:\n");
6250 for (ALL_LIST_ELEMENTS_RO(ip->macip_path_list, node, pi)) {
6251 evp = (struct prefix_evpn *)&pi->net->p;
6252 prefix2str(evp, buf2, sizeof(buf2));
6253 vty_out(vty, " %s\n", buf2);
6254 }
6255 }
6256
6257 void bgp_evpn_show_remote_ip_hash(struct hash_bucket *bucket, void *args)
6258 {
6259 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
6260 struct vty *vty = (struct vty *)args;
6261
6262 vty_out(vty, "VNI: %u\n", vpn->vni);
6263 bgp_evpn_remote_ip_hash_iterate(
6264 vpn,
6265 (void (*)(struct hash_bucket *, void *))show_remote_ip_entry,
6266 vty);
6267 vty_out(vty, "\n");
6268 }
6269
6270 static void bgp_evpn_remote_ip_hash_link_nexthop(struct hash_bucket *bucket,
6271 void *args)
6272 {
6273 struct evpn_remote_ip *ip = (struct evpn_remote_ip *)bucket->data;
6274 struct bgpevpn *vpn = (struct bgpevpn *)args;
6275
6276 bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, true);
6277 }
6278
6279 static void bgp_evpn_remote_ip_hash_unlink_nexthop(struct hash_bucket *bucket,
6280 void *args)
6281 {
6282 struct evpn_remote_ip *ip = (struct evpn_remote_ip *)bucket->data;
6283 struct bgpevpn *vpn = (struct bgpevpn *)args;
6284
6285 bgp_evpn_remote_ip_process_nexthops(vpn, &ip->addr, false);
6286 }
6287
6288 static unsigned int vni_svi_hash_key_make(const void *p)
6289 {
6290 const struct bgpevpn *vpn = p;
6291
6292 return jhash_1word(vpn->svi_ifindex, 0);
6293 }
6294
6295 static bool vni_svi_hash_cmp(const void *p1, const void *p2)
6296 {
6297 const struct bgpevpn *vpn1 = p1;
6298 const struct bgpevpn *vpn2 = p2;
6299
6300 return (vpn1->svi_ifindex == vpn2->svi_ifindex);
6301 }
6302
6303 static struct bgpevpn *bgp_evpn_vni_svi_hash_lookup(struct bgp *bgp,
6304 ifindex_t svi)
6305 {
6306 struct bgpevpn *vpn;
6307 struct bgpevpn tmp;
6308
6309 memset(&tmp, 0, sizeof(tmp));
6310 tmp.svi_ifindex = svi;
6311 vpn = hash_lookup(bgp->vni_svi_hash, &tmp);
6312 return vpn;
6313 }
6314
6315 static void bgp_evpn_link_to_vni_svi_hash(struct bgp *bgp, struct bgpevpn *vpn)
6316 {
6317 if (vpn->svi_ifindex == 0)
6318 return;
6319
6320 (void)hash_get(bgp->vni_svi_hash, vpn, hash_alloc_intern);
6321 }
6322
6323 static void bgp_evpn_unlink_from_vni_svi_hash(struct bgp *bgp,
6324 struct bgpevpn *vpn)
6325 {
6326 if (vpn->svi_ifindex == 0)
6327 return;
6328
6329 hash_release(bgp->vni_svi_hash, vpn);
6330 }
6331
6332 void bgp_evpn_show_vni_svi_hash(struct hash_bucket *bucket, void *args)
6333 {
6334 struct bgpevpn *evpn = (struct bgpevpn *)bucket->data;
6335 struct vty *vty = (struct vty *)args;
6336
6337 vty_out(vty, "SVI: %u VNI: %u\n", evpn->svi_ifindex, evpn->vni);
6338 }
6339
6340 /*
6341 * This function is called for a bgp_nexthop_cache entry when the nexthop is
6342 * gateway IP overlay index.
6343 * This function returns true if there is a remote MAC/IP route for the gateway
6344 * IP in the EVI of the nexthop SVI.
6345 */
6346 bool bgp_evpn_is_gateway_ip_resolved(struct bgp_nexthop_cache *bnc)
6347 {
6348 struct bgp *bgp_evpn = NULL;
6349 struct bgpevpn *vpn = NULL;
6350 struct evpn_remote_ip tmp;
6351 struct prefix *p;
6352
6353 if (!evpn_resolve_overlay_index())
6354 return false;
6355
6356 if (!bnc->nexthop || bnc->nexthop->ifindex == 0)
6357 return false;
6358
6359 bgp_evpn = bgp_get_evpn();
6360 if (!bgp_evpn)
6361 return false;
6362
6363 /*
6364 * Gateway IP is resolved by nht over SVI interface.
6365 * Use this SVI to find corresponding EVI(L2 context)
6366 */
6367 vpn = bgp_evpn_vni_svi_hash_lookup(bgp_evpn, bnc->nexthop->ifindex);
6368 if (!vpn)
6369 return false;
6370
6371 if (vpn->bgp_vrf != bnc->bgp)
6372 return false;
6373
6374 /*
6375 * Check if the gateway IP is present in the EVI remote_ip_hash table
6376 * which stores all the remote IP addresses received via MAC/IP routes
6377 * in this EVI
6378 */
6379 memset(&tmp, 0, sizeof(tmp));
6380
6381 p = &bnc->prefix;
6382 if (p->family == AF_INET) {
6383 tmp.addr.ipa_type = IPADDR_V4;
6384 memcpy(&(tmp.addr.ipaddr_v4), &(p->u.prefix4),
6385 sizeof(struct in_addr));
6386 } else if (p->family == AF_INET6) {
6387 tmp.addr.ipa_type = IPADDR_V6;
6388 memcpy(&(tmp.addr.ipaddr_v6), &(p->u.prefix6),
6389 sizeof(struct in6_addr));
6390 } else
6391 return false;
6392
6393 if (hash_lookup(vpn->remote_ip_hash, &tmp) == NULL)
6394 return false;
6395
6396 return true;
6397 }
6398
6399 /* Resolve/Unresolve nexthops when a MAC/IP route is added/deleted */
6400 static void bgp_evpn_remote_ip_process_nexthops(struct bgpevpn *vpn,
6401 struct ipaddr *addr,
6402 bool resolve)
6403 {
6404 afi_t afi;
6405 struct prefix p;
6406 struct bgp_nexthop_cache *bnc;
6407 struct bgp_nexthop_cache_head *tree = NULL;
6408
6409 if (!vpn->bgp_vrf || vpn->svi_ifindex == 0)
6410 return;
6411
6412 memset(&p, 0, sizeof(p));
6413
6414 if (addr->ipa_type == IPADDR_V4) {
6415 afi = AFI_IP;
6416 p.family = AF_INET;
6417 memcpy(&(p.u.prefix4), &(addr->ipaddr_v4),
6418 sizeof(struct in_addr));
6419 p.prefixlen = IPV4_MAX_BITLEN;
6420 } else if (addr->ipa_type == IPADDR_V6) {
6421 afi = AFI_IP6;
6422 p.family = AF_INET6;
6423 memcpy(&(p.u.prefix6), &(addr->ipaddr_v6),
6424 sizeof(struct in6_addr));
6425 p.prefixlen = IPV6_MAX_BITLEN;
6426 } else
6427 return;
6428
6429 tree = &vpn->bgp_vrf->nexthop_cache_table[afi];
6430 bnc = bnc_find(tree, &p, 0);
6431
6432 if (!bnc || !bnc->is_evpn_gwip_nexthop)
6433 return;
6434
6435 if (!bnc->nexthop || bnc->nexthop->ifindex != vpn->svi_ifindex)
6436 return;
6437
6438 if (BGP_DEBUG(nht, NHT)) {
6439 char buf[PREFIX2STR_BUFFER];
6440
6441 prefix2str(&bnc->prefix, buf, sizeof(buf));
6442 zlog_debug("%s(%u): vni %u mac/ip %s for NH %s",
6443 vpn->bgp_vrf->name_pretty, vpn->tenant_vrf_id,
6444 vpn->vni, (resolve ? "add" : "delete"), buf);
6445 }
6446
6447 /*
6448 * MAC/IP route or SVI or tenant vrf being added to EVI.
6449 * Set nexthop as valid only if it is already L3 reachable
6450 */
6451 if (resolve && bnc->flags & BGP_NEXTHOP_EVPN_INCOMPLETE) {
6452 bnc->flags &= ~BGP_NEXTHOP_EVPN_INCOMPLETE;
6453 bnc->flags |= BGP_NEXTHOP_VALID;
6454 bnc->change_flags |= BGP_NEXTHOP_MACIP_CHANGED;
6455 evaluate_paths(bnc);
6456 }
6457
6458 /* MAC/IP route or SVI or tenant vrf being deleted from EVI */
6459 if (!resolve && bnc->flags & BGP_NEXTHOP_VALID) {
6460 bnc->flags &= ~BGP_NEXTHOP_VALID;
6461 bnc->flags |= BGP_NEXTHOP_EVPN_INCOMPLETE;
6462 bnc->change_flags |= BGP_NEXTHOP_MACIP_CHANGED;
6463 evaluate_paths(bnc);
6464 }
6465 }
6466
6467 void bgp_evpn_handle_resolve_overlay_index_set(struct hash_bucket *bucket,
6468 void *arg)
6469 {
6470 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
6471 struct bgp_dest *dest;
6472 struct bgp_path_info *pi;
6473
6474 bgp_evpn_remote_ip_hash_init(vpn);
6475
6476 for (dest = bgp_table_top(vpn->route_table); dest;
6477 dest = bgp_route_next(dest))
6478 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
6479 bgp_evpn_remote_ip_hash_add(vpn, pi);
6480 }
6481
6482 void bgp_evpn_handle_resolve_overlay_index_unset(struct hash_bucket *bucket,
6483 void *arg)
6484 {
6485 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
6486
6487 bgp_evpn_remote_ip_hash_destroy(vpn);
6488 }