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