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