]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_evpn.c
bgpd: Convert inet_ntoa to %pI4/inet_ntop
[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
747 /* Add the export RTs for L3VNI/VRF */
748 vrf_export_rtl = bgp_vrf->vrf_export_rtl;
749 for (ALL_LIST_ELEMENTS(vrf_export_rtl, node, nnode, ecom))
750 attr->ecommunity =
751 ecommunity_merge(attr->ecommunity, ecom);
752
753 /* add the router mac extended community */
754 if (!is_zero_mac(&attr->rmac)) {
755 encode_rmac_extcomm(&eval_rmac, &attr->rmac);
756 ecommunity_add_val(attr->ecommunity, &eval_rmac, true, true);
757 }
758
759 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
760 }
761
762 /*
763 * Build extended communities for EVPN route.
764 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
765 * and ENCAP extended communities are applicable for all routes.
766 * The default gateway extended community and MAC mobility (sticky) extended
767 * community are added as needed based on passed settings - only for type-2
768 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
769 * added, if present, based on passed settings - only for non-link-local
770 * type-2 routes.
771 */
772 static void build_evpn_route_extcomm(struct bgpevpn *vpn, struct attr *attr,
773 int add_l3_ecomm)
774 {
775 struct ecommunity ecom_encap;
776 struct ecommunity ecom_sticky;
777 struct ecommunity ecom_default_gw;
778 struct ecommunity ecom_na;
779 struct ecommunity_val eval;
780 struct ecommunity_val eval_sticky;
781 struct ecommunity_val eval_default_gw;
782 struct ecommunity_val eval_rmac;
783 struct ecommunity_val eval_na;
784 bool proxy;
785
786 bgp_encap_types tnl_type;
787 struct listnode *node, *nnode;
788 struct ecommunity *ecom;
789 uint32_t seqnum;
790 struct list *vrf_export_rtl = NULL;
791
792 /* Encap */
793 tnl_type = BGP_ENCAP_TYPE_VXLAN;
794 memset(&ecom_encap, 0, sizeof(ecom_encap));
795 encode_encap_extcomm(tnl_type, &eval);
796 ecom_encap.size = 1;
797 ecom_encap.unit_size = ECOMMUNITY_SIZE;
798 ecom_encap.val = (uint8_t *)eval.val;
799
800 /* Add Encap */
801 attr->ecommunity = ecommunity_dup(&ecom_encap);
802
803 /* Add the export RTs for L2VNI */
804 for (ALL_LIST_ELEMENTS(vpn->export_rtl, node, nnode, ecom))
805 attr->ecommunity = ecommunity_merge(attr->ecommunity, ecom);
806
807 /* Add the export RTs for L3VNI if told to - caller determines
808 * when this should be done.
809 */
810 if (add_l3_ecomm) {
811 vrf_export_rtl = bgpevpn_get_vrf_export_rtl(vpn);
812 if (vrf_export_rtl && !list_isempty(vrf_export_rtl)) {
813 for (ALL_LIST_ELEMENTS(vrf_export_rtl, node, nnode,
814 ecom))
815 attr->ecommunity = ecommunity_merge(
816 attr->ecommunity, ecom);
817 }
818 }
819
820 /* Add MAC mobility (sticky) if needed. */
821 if (attr->sticky) {
822 seqnum = 0;
823 memset(&ecom_sticky, 0, sizeof(ecom_sticky));
824 encode_mac_mobility_extcomm(1, seqnum, &eval_sticky);
825 ecom_sticky.size = 1;
826 ecom_sticky.unit_size = ECOMMUNITY_SIZE;
827 ecom_sticky.val = (uint8_t *)eval_sticky.val;
828 attr->ecommunity =
829 ecommunity_merge(attr->ecommunity, &ecom_sticky);
830 }
831
832 /* Add RMAC, if told to. */
833 if (add_l3_ecomm) {
834 encode_rmac_extcomm(&eval_rmac, &attr->rmac);
835 ecommunity_add_val(attr->ecommunity, &eval_rmac, true, true);
836 }
837
838 /* Add default gateway, if needed. */
839 if (attr->default_gw) {
840 memset(&ecom_default_gw, 0, sizeof(ecom_default_gw));
841 encode_default_gw_extcomm(&eval_default_gw);
842 ecom_default_gw.size = 1;
843 ecom_default_gw.unit_size = ECOMMUNITY_SIZE;
844 ecom_default_gw.val = (uint8_t *)eval_default_gw.val;
845 attr->ecommunity =
846 ecommunity_merge(attr->ecommunity, &ecom_default_gw);
847 }
848
849 proxy = !!(attr->es_flags & ATTR_ES_PROXY_ADVERT);
850 if (attr->router_flag || proxy) {
851 memset(&ecom_na, 0, sizeof(ecom_na));
852 encode_na_flag_extcomm(&eval_na, attr->router_flag, proxy);
853 ecom_na.size = 1;
854 ecom_na.unit_size = ECOMMUNITY_SIZE;
855 ecom_na.val = (uint8_t *)eval_na.val;
856 attr->ecommunity = ecommunity_merge(attr->ecommunity,
857 &ecom_na);
858 }
859
860 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
861 }
862
863 /*
864 * Add MAC mobility extended community to attribute.
865 */
866 static void add_mac_mobility_to_attr(uint32_t seq_num, struct attr *attr)
867 {
868 struct ecommunity ecom_tmp;
869 struct ecommunity_val eval;
870 uint8_t *ecom_val_ptr;
871 int i;
872 uint8_t *pnt;
873 int type = 0;
874 int sub_type = 0;
875
876 /* Build MM */
877 encode_mac_mobility_extcomm(0, seq_num, &eval);
878
879 /* Find current MM ecommunity */
880 ecom_val_ptr = NULL;
881
882 if (attr->ecommunity) {
883 for (i = 0; i < attr->ecommunity->size; i++) {
884 pnt = attr->ecommunity->val +
885 (i * attr->ecommunity->unit_size);
886 type = *pnt++;
887 sub_type = *pnt++;
888
889 if (type == ECOMMUNITY_ENCODE_EVPN
890 && sub_type
891 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY) {
892 ecom_val_ptr =
893 (attr->ecommunity->val +
894 (i * attr->ecommunity->unit_size));
895 break;
896 }
897 }
898 }
899
900 /* Update the existing MM ecommunity */
901 if (ecom_val_ptr) {
902 memcpy(ecom_val_ptr, eval.val, sizeof(char)
903 * attr->ecommunity->unit_size);
904 }
905 /* Add MM to existing */
906 else {
907 memset(&ecom_tmp, 0, sizeof(ecom_tmp));
908 ecom_tmp.size = 1;
909 ecom_tmp.unit_size = ECOMMUNITY_SIZE;
910 ecom_tmp.val = (uint8_t *)eval.val;
911
912 if (attr->ecommunity)
913 attr->ecommunity =
914 ecommunity_merge(attr->ecommunity, &ecom_tmp);
915 else
916 attr->ecommunity = ecommunity_dup(&ecom_tmp);
917 }
918 }
919
920 /* Install EVPN route into zebra. */
921 static int evpn_zebra_install(struct bgp *bgp, struct bgpevpn *vpn,
922 const struct prefix_evpn *p,
923 struct bgp_path_info *pi)
924 {
925 int ret;
926 uint8_t flags;
927 int flood_control;
928 uint32_t seq;
929
930 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
931 flags = 0;
932
933 if (pi->sub_type == BGP_ROUTE_IMPORTED) {
934 if (pi->attr->sticky)
935 SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
936 if (pi->attr->default_gw)
937 SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
938 if (is_evpn_prefix_ipaddr_v6(p) &&
939 pi->attr->router_flag)
940 SET_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG);
941
942 seq = mac_mobility_seqnum(pi->attr);
943 /* if local ES notify zebra that this is a sync path */
944 if (bgp_evpn_attr_is_local_es(pi->attr)) {
945 SET_FLAG(flags, ZEBRA_MACIP_TYPE_SYNC_PATH);
946 if (bgp_evpn_attr_is_proxy(pi->attr))
947 SET_FLAG(flags,
948 ZEBRA_MACIP_TYPE_PROXY_ADVERT);
949 }
950 } else {
951 if (!bgp_evpn_attr_is_sync(pi->attr))
952 return 0;
953
954 /* if a local path is being turned around and sent
955 * to zebra it is because it is a sync path on
956 * a local ES
957 */
958 SET_FLAG(flags, ZEBRA_MACIP_TYPE_SYNC_PATH);
959 /* supply the highest peer seq number to zebra
960 * for MM seq syncing
961 */
962 seq = bgp_evpn_attr_get_sync_seq(pi->attr);
963 /* if any of the paths from the peer have the ROUTER
964 * flag set install the local entry as a router entry
965 */
966 if (is_evpn_prefix_ipaddr_v6(p) &&
967 (pi->attr->es_flags &
968 ATTR_ES_PEER_ROUTER))
969 SET_FLAG(flags,
970 ZEBRA_MACIP_TYPE_ROUTER_FLAG);
971
972 if (!(pi->attr->es_flags & ATTR_ES_PEER_ACTIVE))
973 SET_FLAG(flags,
974 ZEBRA_MACIP_TYPE_PROXY_ADVERT);
975 }
976
977 ret = bgp_zebra_send_remote_macip(
978 bgp, vpn, p, pi->attr->nexthop, 1, flags,
979 seq, bgp_evpn_attr_get_esi(pi->attr));
980 } else if (p->prefix.route_type == BGP_EVPN_AD_ROUTE) {
981 ret = bgp_evpn_remote_es_evi_add(bgp, vpn, p);
982 } else {
983 switch (pi->attr->pmsi_tnl_type) {
984 case PMSI_TNLTYPE_INGR_REPL:
985 flood_control = VXLAN_FLOOD_HEAD_END_REPL;
986 break;
987
988 case PMSI_TNLTYPE_PIM_SM:
989 flood_control = VXLAN_FLOOD_PIM_SM;
990 break;
991
992 default:
993 flood_control = VXLAN_FLOOD_DISABLED;
994 break;
995 }
996 ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, flood_control, 1);
997 }
998
999 return ret;
1000 }
1001
1002 /* Uninstall EVPN route from zebra. */
1003 static int evpn_zebra_uninstall(struct bgp *bgp, struct bgpevpn *vpn,
1004 const struct prefix_evpn *p,
1005 struct in_addr remote_vtep_ip)
1006 {
1007 int ret;
1008
1009 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
1010 ret = bgp_zebra_send_remote_macip(bgp, vpn, p, remote_vtep_ip,
1011 0, 0, 0, NULL);
1012 else if (p->prefix.route_type == BGP_EVPN_AD_ROUTE)
1013 ret = bgp_evpn_remote_es_evi_del(bgp, vpn, p);
1014 else
1015 ret = bgp_zebra_send_remote_vtep(bgp, vpn, p,
1016 VXLAN_FLOOD_DISABLED, 0);
1017
1018 return ret;
1019 }
1020
1021 /*
1022 * Due to MAC mobility, the prior "local" best route has been supplanted
1023 * by a "remote" best route. The prior route has to be deleted and withdrawn
1024 * from peers.
1025 */
1026 static void evpn_delete_old_local_route(struct bgp *bgp, struct bgpevpn *vpn,
1027 struct bgp_dest *dest,
1028 struct bgp_path_info *old_local,
1029 struct bgp_path_info *new_select)
1030 {
1031 struct bgp_dest *global_dest;
1032 struct bgp_path_info *pi;
1033 afi_t afi = AFI_L2VPN;
1034 safi_t safi = SAFI_EVPN;
1035
1036 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT)) {
1037 char prefix_buf[PREFIX_STRLEN];
1038 char esi_buf[ESI_STR_LEN];
1039 char esi_buf2[ESI_STR_LEN];
1040 struct prefix_evpn *evp =
1041 (struct prefix_evpn *)bgp_dest_get_prefix(dest);
1042
1043 zlog_debug("local path deleted %s es %s; new-path-es %s",
1044 prefix2str(evp,
1045 prefix_buf, sizeof(prefix_buf)),
1046 esi_to_str(&old_local->attr->esi,
1047 esi_buf, sizeof(esi_buf)),
1048 new_select ? esi_to_str(&new_select->attr->esi,
1049 esi_buf2, sizeof(esi_buf2)) : "");
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 char buf1[PREFIX_STRLEN];
1318
1319 zlog_debug("VRF %s evp %s advertise-pip primary ip is not configured",
1320 vrf_id_to_name(bgp_vrf->vrf_id),
1321 prefix2str(evp, buf1, sizeof(buf1)));
1322 }
1323 }
1324
1325 if (bgp_debug_zebra(NULL)) {
1326 char buf[ETHER_ADDR_STRLEN];
1327 char buf1[PREFIX_STRLEN];
1328 char buf2[INET6_ADDRSTRLEN];
1329
1330 zlog_debug("VRF %s type-5 route evp %s RMAC %s nexthop %s",
1331 vrf_id_to_name(bgp_vrf->vrf_id),
1332 prefix2str(evp, buf1, sizeof(buf1)),
1333 prefix_mac2str(&attr.rmac, buf, sizeof(buf)),
1334 inet_ntop(AF_INET, &attr.nexthop, buf2,
1335 INET_ADDRSTRLEN));
1336 }
1337
1338 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1339
1340 /* Setup RT and encap extended community */
1341 build_evpn_type5_route_extcomm(bgp_vrf, &attr);
1342
1343 /* get the route node in global table */
1344 dest = bgp_global_evpn_node_get(bgp_evpn->rib[afi][safi], afi, safi,
1345 (const struct prefix_evpn *)evp,
1346 &bgp_vrf->vrf_prd);
1347 assert(dest);
1348
1349 /* create or update the route entry within the route node */
1350 update_evpn_type5_route_entry(bgp_evpn, bgp_vrf, afi, safi, dest, &attr,
1351 &route_changed);
1352
1353 /* schedule for processing and unlock node */
1354 if (route_changed) {
1355 bgp_process(bgp_evpn, dest, afi, safi);
1356 bgp_dest_unlock_node(dest);
1357 }
1358
1359 /* uninten temporary */
1360 if (!src_attr)
1361 aspath_unintern(&attr.aspath);
1362 return 0;
1363 }
1364
1365 static void bgp_evpn_get_sync_info(struct bgp *bgp, esi_t *esi,
1366 struct bgp_dest *dest, uint32_t loc_seq,
1367 uint32_t *max_sync_seq, bool *active_on_peer,
1368 bool *peer_router, bool *proxy_from_peer)
1369 {
1370 struct bgp_path_info *tmp_pi;
1371 struct bgp_path_info *second_best_path = NULL;
1372 uint32_t tmp_mm_seq = 0;
1373 esi_t *tmp_esi;
1374 int paths_eq;
1375
1376 /* find the best non-local path. a local path can only be present
1377 * as best path
1378 */
1379 for (tmp_pi = bgp_dest_get_bgp_path_info(dest); tmp_pi;
1380 tmp_pi = tmp_pi->next) {
1381 if (tmp_pi->sub_type != BGP_ROUTE_IMPORTED ||
1382 !CHECK_FLAG(tmp_pi->flags, BGP_PATH_VALID))
1383 continue;
1384
1385 if (bgp_evpn_path_info_cmp(bgp, tmp_pi,
1386 second_best_path, &paths_eq))
1387 second_best_path = tmp_pi;
1388 }
1389
1390 if (!second_best_path)
1391 return;
1392
1393 tmp_esi = bgp_evpn_attr_get_esi(second_best_path->attr);
1394 /* if this has the same ES desination as the local path
1395 * it is a sync path
1396 */
1397 if (!memcmp(esi, tmp_esi, sizeof(esi_t))) {
1398 tmp_mm_seq = mac_mobility_seqnum(second_best_path->attr);
1399 if (tmp_mm_seq < loc_seq)
1400 return;
1401
1402 /* we have a non-proxy path from the ES peer. */
1403 if (second_best_path->attr->es_flags &
1404 ATTR_ES_PROXY_ADVERT) {
1405 *proxy_from_peer = true;
1406 } else {
1407 *active_on_peer = true;
1408 }
1409
1410 if (second_best_path->attr->router_flag)
1411 *peer_router = true;
1412
1413 /* we use both proxy and non-proxy imports to
1414 * determine the max sync sequence
1415 */
1416 if (tmp_mm_seq > *max_sync_seq)
1417 *max_sync_seq = tmp_mm_seq;
1418 }
1419 }
1420
1421 /* Bubble up sync-info from all paths (non-best) to the local-path.
1422 * This is need for MM sequence number syncing and proxy advertisement.
1423 * Note: The local path can only exist as a best path in the
1424 * VPN route table. It will take precedence over all sync paths.
1425 */
1426 static void update_evpn_route_entry_sync_info(struct bgp *bgp,
1427 struct bgp_dest *dest,
1428 struct attr *attr,
1429 uint32_t loc_seq, bool setup_sync)
1430 {
1431 esi_t *esi;
1432 struct prefix_evpn *evp =
1433 (struct prefix_evpn *)bgp_dest_get_prefix(dest);
1434
1435 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1436 return;
1437
1438 esi = bgp_evpn_attr_get_esi(attr);
1439 if (bgp_evpn_is_esi_valid(esi)) {
1440 if (setup_sync) {
1441 uint32_t max_sync_seq = 0;
1442 bool active_on_peer = false;
1443 bool peer_router = false;
1444 bool proxy_from_peer = false;
1445
1446 bgp_evpn_get_sync_info(bgp, esi, dest, loc_seq,
1447 &max_sync_seq, &active_on_peer,
1448 &peer_router, &proxy_from_peer);
1449 attr->mm_sync_seqnum = max_sync_seq;
1450 if (active_on_peer)
1451 attr->es_flags |= ATTR_ES_PEER_ACTIVE;
1452 else
1453 attr->es_flags &= ~ATTR_ES_PEER_ACTIVE;
1454 if (proxy_from_peer)
1455 attr->es_flags |= ATTR_ES_PEER_PROXY;
1456 else
1457 attr->es_flags &= ~ATTR_ES_PEER_PROXY;
1458 if (peer_router)
1459 attr->es_flags |= ATTR_ES_PEER_ROUTER;
1460 else
1461 attr->es_flags &= ~ATTR_ES_PEER_ROUTER;
1462
1463 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT)) {
1464 char prefix_buf[PREFIX_STRLEN];
1465 char esi_buf[ESI_STR_LEN];
1466
1467 zlog_debug("setup sync info for %s es %s max_seq %d %s%s%s",
1468 prefix2str(evp, prefix_buf,
1469 sizeof(prefix_buf)),
1470 esi_to_str(esi, esi_buf,
1471 sizeof(esi_buf)),
1472 max_sync_seq,
1473 (attr->es_flags & ATTR_ES_PEER_ACTIVE) ?
1474 "peer-active " : "",
1475 (attr->es_flags & ATTR_ES_PEER_PROXY) ?
1476 "peer-proxy " : "",
1477 (attr->es_flags & ATTR_ES_PEER_ROUTER) ?
1478 "peer-router " : "");
1479 }
1480 }
1481 } else {
1482 attr->mm_sync_seqnum = 0;
1483 attr->es_flags &= ~ATTR_ES_PEER_ACTIVE;
1484 attr->es_flags &= ~ATTR_ES_PEER_PROXY;
1485 }
1486 }
1487
1488 /*
1489 * Create or update EVPN route entry. This could be in the VNI route table
1490 * or the global route table.
1491 */
1492 static int update_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
1493 afi_t afi, safi_t safi, struct bgp_dest *dest,
1494 struct attr *attr, int add,
1495 struct bgp_path_info **pi, uint8_t flags,
1496 uint32_t seq, bool setup_sync,
1497 bool *old_is_sync)
1498 {
1499 struct bgp_path_info *tmp_pi;
1500 struct bgp_path_info *local_pi;
1501 struct attr *attr_new;
1502 mpls_label_t label[BGP_MAX_LABELS];
1503 uint32_t num_labels = 1;
1504 int route_change = 1;
1505 uint8_t sticky = 0;
1506 const struct prefix_evpn *evp;
1507
1508 *pi = NULL;
1509 evp = (const struct prefix_evpn *)bgp_dest_get_prefix(dest);
1510 memset(&label, 0, sizeof(label));
1511
1512 /* See if this is an update of an existing route, or a new add. */
1513 local_pi = bgp_evpn_route_get_local_path(bgp, dest);
1514
1515 /* If route doesn't exist already, create a new one, if told to.
1516 * Otherwise act based on whether the attributes of the route have
1517 * changed or not.
1518 */
1519 if (!local_pi && !add)
1520 return 0;
1521
1522 if (old_is_sync && local_pi)
1523 *old_is_sync = bgp_evpn_attr_is_sync(local_pi->attr);
1524
1525 /* if a local path is being added with a non-zero esi look
1526 * for SYNC paths from ES peers and bubble up the sync-info
1527 */
1528 update_evpn_route_entry_sync_info(bgp, dest, attr, seq, setup_sync);
1529
1530 /* For non-GW MACs, update MAC mobility seq number, if needed. */
1531 if (seq && !CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW))
1532 add_mac_mobility_to_attr(seq, attr);
1533
1534 if (!local_pi) {
1535 /* Add (or update) attribute to hash. */
1536 attr_new = bgp_attr_intern(attr);
1537
1538 /* Extract MAC mobility sequence number, if any. */
1539 attr_new->mm_seqnum =
1540 bgp_attr_mac_mobility_seqnum(attr_new, &sticky);
1541 attr_new->sticky = sticky;
1542
1543 /* Create new route with its attribute. */
1544 tmp_pi = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0,
1545 bgp->peer_self, attr_new, dest);
1546 SET_FLAG(tmp_pi->flags, BGP_PATH_VALID);
1547 bgp_path_info_extra_get(tmp_pi);
1548
1549 /* The VNI goes into the 'label' field of the route */
1550 vni2label(vpn->vni, &label[0]);
1551
1552 /* Type-2 routes may carry a second VNI - the L3-VNI.
1553 * Only attach second label if we are advertising two labels for
1554 * type-2 routes.
1555 */
1556 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
1557 && CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)) {
1558 vni_t l3vni;
1559
1560 l3vni = bgpevpn_get_l3vni(vpn);
1561 if (l3vni) {
1562 vni2label(l3vni, &label[1]);
1563 num_labels++;
1564 }
1565 }
1566
1567 memcpy(&tmp_pi->extra->label, label, sizeof(label));
1568 tmp_pi->extra->num_labels = num_labels;
1569 /* Mark route as self type-2 route */
1570 if (flags && CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_SVI_IP))
1571 tmp_pi->extra->af_flags = BGP_EVPN_MACIP_TYPE_SVI_IP;
1572 bgp_path_info_add(dest, tmp_pi);
1573 } else {
1574 tmp_pi = local_pi;
1575 if (attrhash_cmp(tmp_pi->attr, attr)
1576 && !CHECK_FLAG(tmp_pi->flags, BGP_PATH_REMOVED))
1577 route_change = 0;
1578 else {
1579 /*
1580 * The attributes have changed, type-2 routes needs to
1581 * be advertised with right labels.
1582 */
1583 vni2label(vpn->vni, &label[0]);
1584 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
1585 && CHECK_FLAG(vpn->flags,
1586 VNI_FLAG_USE_TWO_LABELS)) {
1587 vni_t l3vni;
1588
1589 l3vni = bgpevpn_get_l3vni(vpn);
1590 if (l3vni) {
1591 vni2label(l3vni, &label[1]);
1592 num_labels++;
1593 }
1594 }
1595 memcpy(&tmp_pi->extra->label, label, sizeof(label));
1596 tmp_pi->extra->num_labels = num_labels;
1597
1598 /* The attribute has changed. */
1599 /* Add (or update) attribute to hash. */
1600 attr_new = bgp_attr_intern(attr);
1601 bgp_path_info_set_flag(dest, tmp_pi,
1602 BGP_PATH_ATTR_CHANGED);
1603
1604 /* Extract MAC mobility sequence number, if any. */
1605 attr_new->mm_seqnum =
1606 bgp_attr_mac_mobility_seqnum(attr_new, &sticky);
1607 attr_new->sticky = sticky;
1608
1609 /* Restore route, if needed. */
1610 if (CHECK_FLAG(tmp_pi->flags, BGP_PATH_REMOVED))
1611 bgp_path_info_restore(dest, tmp_pi);
1612
1613 /* Unintern existing, set to new. */
1614 bgp_attr_unintern(&tmp_pi->attr);
1615 tmp_pi->attr = attr_new;
1616 tmp_pi->uptime = bgp_clock();
1617 }
1618 }
1619
1620 /* Return back the route entry. */
1621 *pi = tmp_pi;
1622 return route_change;
1623 }
1624
1625 static void evpn_zebra_reinstall_best_route(struct bgp *bgp,
1626 struct bgpevpn *vpn,
1627 struct bgp_dest *dest)
1628 {
1629 struct bgp_path_info *tmp_ri;
1630 struct bgp_path_info *curr_select = NULL;
1631
1632 for (tmp_ri = bgp_dest_get_bgp_path_info(dest); tmp_ri;
1633 tmp_ri = tmp_ri->next) {
1634 if (CHECK_FLAG(tmp_ri->flags, BGP_PATH_SELECTED)) {
1635 curr_select = tmp_ri;
1636 break;
1637 }
1638 }
1639
1640 if (curr_select && curr_select->type == ZEBRA_ROUTE_BGP
1641 && (curr_select->sub_type == BGP_ROUTE_IMPORTED ||
1642 bgp_evpn_attr_is_sync(curr_select->attr)))
1643 evpn_zebra_install(bgp, vpn,
1644 (const struct prefix_evpn *)bgp_dest_get_prefix(dest),
1645 curr_select);
1646 }
1647
1648 /*
1649 * If the local route was not selected evict it and tell zebra to re-add
1650 * the best remote dest.
1651 *
1652 * Typically a local path added by zebra is expected to be selected as
1653 * best. In which case when a remote path wins as best (later)
1654 * evpn_route_select_install itself evicts the older-local-best path.
1655 *
1656 * However if bgp's add and zebra's add cross paths (race condition) it
1657 * is possible that the local path is no longer the "older" best path.
1658 * It is a path that was never designated as best and hence requires
1659 * additional handling to prevent bgp from injecting and holding on to a
1660 * non-best local path.
1661 */
1662 static void evpn_cleanup_local_non_best_route(struct bgp *bgp,
1663 struct bgpevpn *vpn,
1664 struct bgp_dest *dest,
1665 struct bgp_path_info *local_pi)
1666 {
1667 /* local path was not picked as the winner; kick it out */
1668 if (bgp_debug_zebra(NULL))
1669 zlog_debug("evicting local evpn prefix %pBD as remote won",
1670 dest);
1671
1672 evpn_delete_old_local_route(bgp, vpn, dest, local_pi, NULL);
1673 bgp_path_info_reap(dest, local_pi);
1674
1675 /* tell zebra to re-add the best remote path */
1676 evpn_zebra_reinstall_best_route(bgp, vpn, dest);
1677 }
1678
1679 /*
1680 * Create or update EVPN route (of type based on prefix) for specified VNI
1681 * and schedule for processing.
1682 */
1683 static int update_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
1684 struct prefix_evpn *p, uint8_t flags,
1685 uint32_t seq, esi_t *esi)
1686 {
1687 struct bgp_dest *dest;
1688 struct attr attr;
1689 struct attr *attr_new;
1690 int add_l3_ecomm = 0;
1691 struct bgp_path_info *pi;
1692 afi_t afi = AFI_L2VPN;
1693 safi_t safi = SAFI_EVPN;
1694 int route_change;
1695 bool old_is_sync = false;
1696
1697 memset(&attr, 0, sizeof(struct attr));
1698
1699 /* Build path-attribute for this route. */
1700 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
1701 attr.nexthop = vpn->originator_ip;
1702 attr.mp_nexthop_global_in = vpn->originator_ip;
1703 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1704 attr.sticky = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY) ? 1 : 0;
1705 attr.default_gw = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW) ? 1 : 0;
1706 attr.router_flag = CHECK_FLAG(flags,
1707 ZEBRA_MACIP_TYPE_ROUTER_FLAG) ? 1 : 0;
1708 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT))
1709 attr.es_flags |= ATTR_ES_PROXY_ADVERT;
1710
1711 if (esi && bgp_evpn_is_esi_valid(esi)) {
1712 memcpy(&attr.esi, esi, sizeof(esi_t));
1713 attr.es_flags |= ATTR_ES_IS_LOCAL;
1714 }
1715
1716 /* PMSI is only needed for type-3 routes */
1717 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
1718 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL);
1719 attr.pmsi_tnl_type = PMSI_TNLTYPE_INGR_REPL;
1720 }
1721
1722 if (bgp_debug_zebra(NULL)) {
1723 char buf[ETHER_ADDR_STRLEN];
1724 char buf1[PREFIX_STRLEN];
1725 char buf3[ESI_STR_LEN];
1726
1727 zlog_debug(
1728 "VRF %s vni %u type-2 route evp %s RMAC %s nexthop %pI4 esi %s",
1729 vpn->bgp_vrf ? vrf_id_to_name(vpn->bgp_vrf->vrf_id)
1730 : " ",
1731 vpn->vni, prefix2str(p, buf1, sizeof(buf1)),
1732 prefix_mac2str(&attr.rmac, buf, sizeof(buf)),
1733 &attr.mp_nexthop_global_in,
1734 esi_to_str(esi, buf3, sizeof(buf3)));
1735 }
1736 /* router mac is only needed for type-2 routes here. */
1737 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
1738 uint8_t af_flags = 0;
1739
1740 if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_SVI_IP))
1741 SET_FLAG(af_flags, BGP_EVPN_MACIP_TYPE_SVI_IP);
1742
1743 bgp_evpn_get_rmac_nexthop(vpn, p, &attr, af_flags);
1744 }
1745
1746 vni2label(vpn->vni, &(attr.label));
1747
1748 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1749 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1750 * these routes.
1751 */
1752 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE &&
1753 (is_evpn_prefix_ipaddr_v4(p) ||
1754 !IN6_IS_ADDR_LINKLOCAL(&p->prefix.macip_addr.ip.ipaddr_v6)) &&
1755 CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS) &&
1756 bgpevpn_get_l3vni(vpn))
1757 add_l3_ecomm = 1;
1758
1759 /* Set up extended community. */
1760 build_evpn_route_extcomm(vpn, &attr, add_l3_ecomm);
1761
1762 /* First, create (or fetch) route node within the VNI. */
1763 /* NOTE: There is no RD here. */
1764 dest = bgp_node_get(vpn->route_table, (struct prefix *)p);
1765
1766 /* Create or update route entry. */
1767 route_change = update_evpn_route_entry(bgp, vpn, afi, safi, dest, &attr,
1768 1, &pi, flags, seq,
1769 true /* setup_sync */, &old_is_sync);
1770 assert(pi);
1771 attr_new = pi->attr;
1772
1773 /* lock ri to prevent freeing in evpn_route_select_install */
1774 bgp_path_info_lock(pi);
1775
1776 /* Perform route selection. Normally, the local route in the
1777 * VNI is expected to win and be the best route. However, if
1778 * there is a race condition where a host moved from local to
1779 * remote and the remote route was received in BGP just prior
1780 * to the local MACIP notification from zebra, the remote
1781 * route would win, and we should evict the defunct local route
1782 * and (re)install the remote route into zebra.
1783 */
1784 evpn_route_select_install(bgp, vpn, dest);
1785 /*
1786 * If the new local route was not selected evict it and tell zebra
1787 * to re-add the best remote dest. BGP doesn't retain non-best local
1788 * routes.
1789 */
1790 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
1791 route_change = 0;
1792 } else {
1793 if (!CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
1794 route_change = 0;
1795 evpn_cleanup_local_non_best_route(bgp, vpn, dest, pi);
1796 } else {
1797 bool new_is_sync;
1798
1799 /* If the local path already existed and is still the
1800 * best path we need to also check if it transitioned
1801 * from being a sync path to a non-sync path. If it
1802 * it did we need to notify zebra that the sync-path
1803 * has been removed.
1804 */
1805 new_is_sync = bgp_evpn_attr_is_sync(pi->attr);
1806 if (!new_is_sync && old_is_sync)
1807 evpn_zebra_uninstall(bgp, vpn, p, zero_vtep_ip);
1808 }
1809 }
1810 bgp_path_info_unlock(pi);
1811
1812 bgp_dest_unlock_node(dest);
1813
1814 /* If this is a new route or some attribute has changed, export the
1815 * route to the global table. The route will be advertised to peers
1816 * from there. Note that this table is a 2-level tree (RD-level +
1817 * Prefix-level) similar to L3VPN routes.
1818 */
1819 if (route_change) {
1820 struct bgp_path_info *global_pi;
1821
1822 dest = bgp_global_evpn_node_get(bgp->rib[afi][safi], afi, safi,
1823 (const struct prefix_evpn *)p,
1824 &vpn->prd);
1825 update_evpn_route_entry(bgp, vpn, afi, safi, dest, attr_new, 1,
1826 &global_pi, flags, seq,
1827 false /* setup_sync */, NULL /* old_is_sync */);
1828
1829 /* Schedule for processing and unlock node. */
1830 bgp_process(bgp, dest, afi, safi);
1831 bgp_dest_unlock_node(dest);
1832 }
1833
1834 /* Unintern temporary. */
1835 aspath_unintern(&attr.aspath);
1836
1837 return 0;
1838 }
1839
1840 /*
1841 * Delete EVPN route entry.
1842 * The entry can be in ESI/VNI table or the global table.
1843 */
1844 void delete_evpn_route_entry(struct bgp *bgp, afi_t afi, safi_t safi,
1845 struct bgp_dest *dest,
1846 struct bgp_path_info **pi)
1847 {
1848 struct bgp_path_info *tmp_pi;
1849
1850 *pi = NULL;
1851
1852 /* Now, find matching route. */
1853 for (tmp_pi = bgp_dest_get_bgp_path_info(dest); tmp_pi;
1854 tmp_pi = tmp_pi->next)
1855 if (tmp_pi->peer == bgp->peer_self
1856 && tmp_pi->type == ZEBRA_ROUTE_BGP
1857 && tmp_pi->sub_type == BGP_ROUTE_STATIC)
1858 break;
1859
1860 *pi = tmp_pi;
1861
1862 /* Mark route for delete. */
1863 if (tmp_pi)
1864 bgp_path_info_delete(dest, tmp_pi);
1865 }
1866
1867 /* Delete EVPN type5 route */
1868 static int delete_evpn_type5_route(struct bgp *bgp_vrf, struct prefix_evpn *evp)
1869 {
1870 afi_t afi = AFI_L2VPN;
1871 safi_t safi = SAFI_EVPN;
1872 struct bgp_dest *dest = NULL;
1873 struct bgp_path_info *pi = NULL;
1874 struct bgp *bgp_evpn = NULL; /* evpn bgp instance */
1875
1876 bgp_evpn = bgp_get_evpn();
1877 if (!bgp_evpn)
1878 return 0;
1879
1880 /* locate the global route entry for this type-5 prefix */
1881 dest = bgp_global_evpn_node_lookup(bgp_evpn->rib[afi][safi], afi, safi,
1882 (const struct prefix_evpn *)evp, &bgp_vrf->vrf_prd);
1883 if (!dest)
1884 return 0;
1885
1886 delete_evpn_route_entry(bgp_evpn, afi, safi, dest, &pi);
1887 if (pi)
1888 bgp_process(bgp_evpn, dest, afi, safi);
1889 bgp_dest_unlock_node(dest);
1890 return 0;
1891 }
1892
1893 /*
1894 * Delete EVPN route (of type based on prefix) for specified VNI and
1895 * schedule for processing.
1896 */
1897 static int delete_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
1898 struct prefix_evpn *p)
1899 {
1900 struct bgp_dest *dest, *global_dest;
1901 struct bgp_path_info *pi;
1902 afi_t afi = AFI_L2VPN;
1903 safi_t safi = SAFI_EVPN;
1904
1905 /* First, locate the route node within the VNI. If it doesn't exist,
1906 * there
1907 * is nothing further to do.
1908 */
1909 /* NOTE: There is no RD here. */
1910 dest = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
1911 if (!dest)
1912 return 0;
1913
1914 /* Next, locate route node in the global EVPN routing table. Note that
1915 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1916 * L3VPN routes.
1917 */
1918 global_dest = bgp_global_evpn_node_lookup(bgp->rib[afi][safi], afi, safi,
1919 (const struct prefix_evpn *)p, &vpn->prd);
1920 if (global_dest) {
1921 /* Delete route entry in the global EVPN table. */
1922 delete_evpn_route_entry(bgp, afi, safi, global_dest, &pi);
1923
1924 /* Schedule for processing - withdraws to peers happen from
1925 * this table.
1926 */
1927 if (pi)
1928 bgp_process(bgp, global_dest, afi, safi);
1929 bgp_dest_unlock_node(global_dest);
1930 }
1931
1932 /* Delete route entry in the VNI route table. This can just be removed.
1933 */
1934 delete_evpn_route_entry(bgp, afi, safi, dest, &pi);
1935 if (pi) {
1936 bgp_path_info_reap(dest, pi);
1937 evpn_route_select_install(bgp, vpn, dest);
1938 }
1939 bgp_dest_unlock_node(dest);
1940
1941 return 0;
1942 }
1943
1944 static void bgp_evpn_update_type2_route_entry(struct bgp *bgp,
1945 struct bgpevpn *vpn,
1946 struct bgp_dest *dest,
1947 struct bgp_path_info *local_pi,
1948 const char *caller)
1949 {
1950 afi_t afi = AFI_L2VPN;
1951 safi_t safi = SAFI_EVPN;
1952 struct bgp_path_info *pi;
1953 struct attr attr;
1954 struct attr *attr_new;
1955 uint32_t seq;
1956 int add_l3_ecomm = 0;
1957 struct bgp_dest *global_dest;
1958 struct bgp_path_info *global_pi;
1959 struct prefix_evpn *evp =
1960 (struct prefix_evpn *)bgp_dest_get_prefix(dest);
1961 int route_change;
1962 bool old_is_sync = false;
1963
1964 if (CHECK_FLAG(local_pi->flags, BGP_PATH_REMOVED))
1965 return;
1966
1967 /*
1968 * Build attribute per local route as the MAC mobility and
1969 * some other values could differ for different routes. The
1970 * attributes will be shared in the hash table.
1971 */
1972 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
1973 attr.nexthop = vpn->originator_ip;
1974 attr.mp_nexthop_global_in = vpn->originator_ip;
1975 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1976 attr.sticky = (local_pi->attr->sticky) ? 1 : 0;
1977 attr.router_flag = (local_pi->attr->router_flag) ? 1 : 0;
1978 attr.es_flags = local_pi->attr->es_flags;
1979 if (local_pi->attr->default_gw) {
1980 attr.default_gw = 1;
1981 if (is_evpn_prefix_ipaddr_v6(evp))
1982 attr.router_flag = 1;
1983 }
1984 memcpy(&attr.esi, &local_pi->attr->esi, sizeof(esi_t));
1985 bgp_evpn_get_rmac_nexthop(vpn, evp, &attr,
1986 local_pi->extra->af_flags);
1987 vni2label(vpn->vni, &(attr.label));
1988 /* Add L3 VNI RTs and RMAC for non IPv6 link-local if
1989 * using L3 VNI for type-2 routes also.
1990 */
1991 if ((is_evpn_prefix_ipaddr_v4(evp) ||
1992 !IN6_IS_ADDR_LINKLOCAL(
1993 &evp->prefix.macip_addr.ip.ipaddr_v6)) &&
1994 CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS) &&
1995 bgpevpn_get_l3vni(vpn))
1996 add_l3_ecomm = 1;
1997
1998 /* Set up extended community. */
1999 build_evpn_route_extcomm(vpn, &attr, add_l3_ecomm);
2000 seq = mac_mobility_seqnum(local_pi->attr);
2001
2002 if (bgp_debug_zebra(NULL)) {
2003 char buf[ETHER_ADDR_STRLEN];
2004 char buf1[PREFIX_STRLEN];
2005 char buf3[ESI_STR_LEN];
2006
2007 zlog_debug(
2008 "VRF %s vni %u evp %s RMAC %s nexthop %pI4 esi %s esf 0x%x from %s",
2009 vpn->bgp_vrf ? vrf_id_to_name(vpn->bgp_vrf->vrf_id)
2010 : " ",
2011 vpn->vni, prefix2str(evp, buf1, sizeof(buf1)),
2012 prefix_mac2str(&attr.rmac, buf, sizeof(buf)),
2013 &attr.mp_nexthop_global_in,
2014 esi_to_str(&attr.esi, buf3, sizeof(buf3)),
2015 attr.es_flags, caller);
2016 }
2017
2018 /* Update the route entry. */
2019 route_change = update_evpn_route_entry(
2020 bgp, vpn, afi, safi, dest, &attr, 0, &pi, 0, seq,
2021 true /* setup_sync */, &old_is_sync);
2022
2023 assert(pi);
2024 attr_new = pi->attr;
2025 /* lock ri to prevent freeing in evpn_route_select_install */
2026 bgp_path_info_lock(pi);
2027
2028 /* Perform route selection. Normally, the local route in the
2029 * VNI is expected to win and be the best route. However,
2030 * under peculiar situations (e.g., tunnel (next hop) IP change
2031 * that causes best selection to be based on next hop), a
2032 * remote route could win. If the local route is the best,
2033 * ensure it is updated in the global EVPN route table and
2034 * advertised to peers; otherwise, ensure it is evicted and
2035 * (re)install the remote route into zebra.
2036 */
2037 evpn_route_select_install(bgp, vpn, dest);
2038
2039 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
2040 route_change = 0;
2041 } else {
2042 if (!CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
2043 route_change = 0;
2044 evpn_cleanup_local_non_best_route(bgp, vpn, dest, pi);
2045 } else {
2046 bool new_is_sync;
2047
2048 /* If the local path already existed and is still the
2049 * best path we need to also check if it transitioned
2050 * from being a sync path to a non-sync path. If it
2051 * it did we need to notify zebra that the sync-path
2052 * has been removed.
2053 */
2054 new_is_sync = bgp_evpn_attr_is_sync(pi->attr);
2055 if (!new_is_sync && old_is_sync)
2056 evpn_zebra_uninstall(bgp, vpn,
2057 evp, zero_vtep_ip);
2058 }
2059 }
2060
2061
2062 /* unlock pi */
2063 bgp_path_info_unlock(pi);
2064
2065 if (route_change) {
2066 /* Update route in global routing table. */
2067 global_dest = bgp_global_evpn_node_get(bgp->rib[afi][safi], afi,
2068 safi, evp, &vpn->prd);
2069 assert(global_dest);
2070 update_evpn_route_entry(
2071 bgp, vpn, afi, safi, global_dest, attr_new, 0,
2072 &global_pi, 0, mac_mobility_seqnum(attr_new),
2073 false /* setup_sync */, NULL /* old_is_sync */);
2074
2075 /* Schedule for processing and unlock node. */
2076 bgp_process(bgp, global_dest, afi, safi);
2077 bgp_dest_unlock_node(global_dest);
2078 }
2079
2080 /* Unintern temporary. */
2081 aspath_unintern(&attr.aspath);
2082 }
2083
2084 /*
2085 * Update all type-2 (MACIP) local routes for this VNI - these should also
2086 * be scheduled for advertise to peers.
2087 */
2088 static int update_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
2089 {
2090 struct bgp_dest *dest;
2091 struct bgp_path_info *tmp_pi;
2092
2093 /* Walk this VNI's route table and update local type-2 routes. For any
2094 * routes updated, update corresponding entry in the global table too.
2095 */
2096 for (dest = bgp_table_top(vpn->route_table); dest;
2097 dest = bgp_route_next(dest)) {
2098 const struct prefix_evpn *evp =
2099 (const struct prefix_evpn *)bgp_dest_get_prefix(dest);
2100
2101 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2102 continue;
2103
2104 /* Identify local route. */
2105 for (tmp_pi = bgp_dest_get_bgp_path_info(dest); tmp_pi;
2106 tmp_pi = tmp_pi->next) {
2107 if (tmp_pi->peer == bgp->peer_self
2108 && tmp_pi->type == ZEBRA_ROUTE_BGP
2109 && tmp_pi->sub_type == BGP_ROUTE_STATIC)
2110 break;
2111 }
2112
2113 if (!tmp_pi)
2114 continue;
2115
2116 bgp_evpn_update_type2_route_entry(bgp, vpn, dest, tmp_pi,
2117 __func__);
2118 }
2119
2120 return 0;
2121 }
2122
2123 /*
2124 * Delete all type-2 (MACIP) local routes for this VNI - only from the
2125 * global routing table. These are also scheduled for withdraw from peers.
2126 */
2127 static int delete_global_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
2128 {
2129 afi_t afi;
2130 safi_t safi;
2131 struct bgp_dest *rddest, *dest;
2132 struct bgp_table *table;
2133 struct bgp_path_info *pi;
2134
2135 afi = AFI_L2VPN;
2136 safi = SAFI_EVPN;
2137
2138 rddest = bgp_node_lookup(bgp->rib[afi][safi],
2139 (struct prefix *)&vpn->prd);
2140 if (rddest && bgp_dest_has_bgp_path_info_data(rddest)) {
2141 table = bgp_dest_get_bgp_table_info(rddest);
2142 for (dest = bgp_table_top(table); dest;
2143 dest = bgp_route_next(dest)) {
2144 const struct prefix_evpn *evp =
2145 (const struct prefix_evpn *)bgp_dest_get_prefix(
2146 dest);
2147
2148 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2149 continue;
2150
2151 delete_evpn_route_entry(bgp, afi, safi, dest, &pi);
2152 if (pi)
2153 bgp_process(bgp, dest, afi, safi);
2154 }
2155 }
2156
2157 /* Unlock RD node. */
2158 if (rddest)
2159 bgp_dest_unlock_node(rddest);
2160
2161 return 0;
2162 }
2163
2164 /*
2165 * Delete all type-2 (MACIP) local routes for this VNI - from the global
2166 * table as well as the per-VNI route table.
2167 */
2168 static int delete_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
2169 {
2170 afi_t afi;
2171 safi_t safi;
2172 struct bgp_dest *dest;
2173 struct bgp_path_info *pi;
2174
2175 afi = AFI_L2VPN;
2176 safi = SAFI_EVPN;
2177
2178 /* First, walk the global route table for this VNI's type-2 local
2179 * routes.
2180 * EVPN routes are a 2-level table, first get the RD table.
2181 */
2182 delete_global_type2_routes(bgp, vpn);
2183
2184 /* Next, walk this VNI's route table and delete local type-2 routes. */
2185 for (dest = bgp_table_top(vpn->route_table); dest;
2186 dest = bgp_route_next(dest)) {
2187 const struct prefix_evpn *evp =
2188 (const struct prefix_evpn *)bgp_dest_get_prefix(dest);
2189
2190 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2191 continue;
2192
2193 delete_evpn_route_entry(bgp, afi, safi, dest, &pi);
2194
2195 /* Route entry in local table gets deleted immediately. */
2196 if (pi)
2197 bgp_path_info_reap(dest, pi);
2198 }
2199
2200 return 0;
2201 }
2202
2203 /*
2204 * Delete all routes in the per-VNI route table.
2205 */
2206 static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
2207 {
2208 struct bgp_dest *dest;
2209 struct bgp_path_info *pi, *nextpi;
2210
2211 /* Walk this VNI's route table and delete all routes. */
2212 for (dest = bgp_table_top(vpn->route_table); dest;
2213 dest = bgp_route_next(dest)) {
2214 for (pi = bgp_dest_get_bgp_path_info(dest);
2215 (pi != NULL) && (nextpi = pi->next, 1); pi = nextpi) {
2216 bgp_path_info_delete(dest, pi);
2217 bgp_path_info_reap(dest, pi);
2218 }
2219 }
2220
2221 return 0;
2222 }
2223
2224 /* BUM traffic flood mode per-l2-vni */
2225 static int bgp_evpn_vni_flood_mode_get(struct bgp *bgp,
2226 struct bgpevpn *vpn)
2227 {
2228 /* if flooding has been globally disabled per-vni mode is
2229 * not relevant
2230 */
2231 if (bgp->vxlan_flood_ctrl == VXLAN_FLOOD_DISABLED)
2232 return VXLAN_FLOOD_DISABLED;
2233
2234 /* if mcast group ip has been specified we use a PIM-SM MDT */
2235 if (vpn->mcast_grp.s_addr != INADDR_ANY)
2236 return VXLAN_FLOOD_PIM_SM;
2237
2238 /* default is ingress replication */
2239 return VXLAN_FLOOD_HEAD_END_REPL;
2240 }
2241
2242 /*
2243 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
2244 * export RT getting modified or change to tunnel IP. Note that these
2245 * situations need the route in the per-VNI table as well as the global
2246 * table to be updated (as attributes change).
2247 */
2248 int update_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
2249 {
2250 int ret;
2251 struct prefix_evpn p;
2252
2253 /* Update and advertise the type-3 route (only one) followed by the
2254 * locally learnt type-2 routes (MACIP) - for this VNI.
2255 *
2256 * RT-3 only if doing head-end replication
2257 */
2258 if (bgp_evpn_vni_flood_mode_get(bgp, vpn)
2259 == VXLAN_FLOOD_HEAD_END_REPL) {
2260 build_evpn_type3_prefix(&p, vpn->originator_ip);
2261 ret = update_evpn_route(bgp, vpn, &p, 0, 0, NULL);
2262 if (ret)
2263 return ret;
2264 }
2265
2266 return update_all_type2_routes(bgp, vpn);
2267 }
2268
2269 /*
2270 * Delete (and withdraw) local routes for specified VNI from the global
2271 * table and per-VNI table. After this, remove all other routes from
2272 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
2273 * (advertise-all-vni) being disabled.
2274 */
2275 static int delete_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
2276 {
2277 int ret;
2278 struct prefix_evpn p;
2279
2280 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2281 * followed by type-3 routes (only one) - for this VNI.
2282 */
2283 ret = delete_all_type2_routes(bgp, vpn);
2284 if (ret)
2285 return ret;
2286
2287 build_evpn_type3_prefix(&p, vpn->originator_ip);
2288 ret = delete_evpn_route(bgp, vpn, &p);
2289 if (ret)
2290 return ret;
2291
2292 /* Delete all routes from the per-VNI table. */
2293 return delete_all_vni_routes(bgp, vpn);
2294 }
2295
2296 /*
2297 * There is a flood mcast IP address change. Update the mcast-grp and
2298 * remove the type-3 route if any. A new type-3 route will be generated
2299 * post tunnel_ip update if the new flood mode is head-end-replication.
2300 */
2301 static int bgp_evpn_mcast_grp_change(struct bgp *bgp, struct bgpevpn *vpn,
2302 struct in_addr mcast_grp)
2303 {
2304 struct prefix_evpn p;
2305
2306 vpn->mcast_grp = mcast_grp;
2307
2308 if (is_vni_live(vpn)) {
2309 build_evpn_type3_prefix(&p, vpn->originator_ip);
2310 delete_evpn_route(bgp, vpn, &p);
2311 }
2312
2313 return 0;
2314 }
2315
2316 /*
2317 * There is a tunnel endpoint IP address change for this VNI, delete
2318 * prior type-3 route (if needed) and update.
2319 * Note: Route re-advertisement happens elsewhere after other processing
2320 * other changes.
2321 */
2322 static int handle_tunnel_ip_change(struct bgp *bgp, struct bgpevpn *vpn,
2323 struct in_addr originator_ip)
2324 {
2325 struct prefix_evpn p;
2326
2327 /* If VNI is not live, we only need to update the originator ip */
2328 if (!is_vni_live(vpn)) {
2329 vpn->originator_ip = originator_ip;
2330 return 0;
2331 }
2332
2333 /* Update the tunnel-ip hash */
2334 bgp_tip_del(bgp, &vpn->originator_ip);
2335 bgp_tip_add(bgp, &originator_ip);
2336
2337 /* filter routes as martian nexthop db has changed */
2338 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
2339
2340 /* Need to withdraw type-3 route as the originator IP is part
2341 * of the key.
2342 */
2343 build_evpn_type3_prefix(&p, vpn->originator_ip);
2344 delete_evpn_route(bgp, vpn, &p);
2345
2346 /* Update the tunnel IP and re-advertise all routes for this VNI. */
2347 vpn->originator_ip = originator_ip;
2348 return 0;
2349 }
2350
2351 static struct bgp_path_info *
2352 bgp_create_evpn_bgp_path_info(struct bgp_path_info *parent_pi,
2353 struct bgp_dest *dest, struct attr *attr)
2354 {
2355 struct attr *attr_new;
2356 struct bgp_path_info *pi;
2357
2358 /* Add (or update) attribute to hash. */
2359 attr_new = bgp_attr_intern(attr);
2360
2361 /* Create new route with its attribute. */
2362 pi = info_make(parent_pi->type, BGP_ROUTE_IMPORTED, 0, parent_pi->peer,
2363 attr_new, dest);
2364 SET_FLAG(pi->flags, BGP_PATH_VALID);
2365 bgp_path_info_extra_get(pi);
2366 pi->extra->parent = bgp_path_info_lock(parent_pi);
2367 bgp_dest_lock_node((struct bgp_dest *)parent_pi->net);
2368 if (parent_pi->extra) {
2369 memcpy(&pi->extra->label, &parent_pi->extra->label,
2370 sizeof(pi->extra->label));
2371 pi->extra->num_labels = parent_pi->extra->num_labels;
2372 }
2373 bgp_path_info_add(dest, pi);
2374
2375 return pi;
2376 }
2377
2378 /*
2379 * Install route entry into the VRF routing table and invoke route selection.
2380 */
2381 static int install_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
2382 const struct prefix_evpn *evp,
2383 struct bgp_path_info *parent_pi)
2384 {
2385 struct bgp_dest *dest;
2386 struct bgp_path_info *pi;
2387 struct attr attr;
2388 struct attr *attr_new;
2389 int ret = 0;
2390 struct prefix p;
2391 struct prefix *pp = &p;
2392 afi_t afi = 0;
2393 safi_t safi = 0;
2394 char buf[PREFIX_STRLEN];
2395 bool new_pi = false;
2396
2397 memset(pp, 0, sizeof(struct prefix));
2398 ip_prefix_from_evpn_prefix(evp, pp);
2399
2400 if (bgp_debug_zebra(NULL)) {
2401 zlog_debug(
2402 "vrf %s: import evpn prefix %s parent %p flags 0x%x",
2403 vrf_id_to_name(bgp_vrf->vrf_id),
2404 prefix2str(evp, buf, sizeof(buf)),
2405 parent_pi, parent_pi->flags);
2406 }
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(
2486 "... %s pi dest %p (l %d) pi %p (l %d, f 0x%x)",
2487 new_pi ? "new" : "update",
2488 dest, bgp_dest_to_rnode(dest)->lock,
2489 pi, pi->lock, pi->flags);
2490
2491 return ret;
2492 }
2493
2494 /*
2495 * Install route entry into the VNI routing table and invoke route selection.
2496 */
2497 static int install_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
2498 const struct prefix_evpn *p,
2499 struct bgp_path_info *parent_pi)
2500 {
2501 struct bgp_dest *dest;
2502 struct bgp_path_info *pi;
2503 struct bgp_path_info *local_pi;
2504 struct attr *attr_new;
2505 int ret;
2506 struct prefix_evpn ad_evp;
2507
2508 /* EAD prefix in the global table doesn't include the VTEP-IP so
2509 * we need to create a different copy for the VNI
2510 */
2511 if (p->prefix.route_type == BGP_EVPN_AD_ROUTE)
2512 p = evpn_type1_prefix_vni_copy(&ad_evp, p,
2513 parent_pi->attr->nexthop);
2514
2515 /* Create (or fetch) route within the VNI. */
2516 /* NOTE: There is no RD here. */
2517 dest = bgp_node_get(vpn->route_table, (struct prefix *)p);
2518
2519 /* Check if route entry is already present. */
2520 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
2521 if (pi->extra
2522 && (struct bgp_path_info *)pi->extra->parent == parent_pi)
2523 break;
2524
2525 if (!pi) {
2526 /* Create an info */
2527 (void)bgp_create_evpn_bgp_path_info(parent_pi, dest,
2528 parent_pi->attr);
2529 } else {
2530 if (attrhash_cmp(pi->attr, parent_pi->attr)
2531 && !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
2532 bgp_dest_unlock_node(dest);
2533 return 0;
2534 }
2535 /* The attribute has changed. */
2536 /* Add (or update) attribute to hash. */
2537 attr_new = bgp_attr_intern(parent_pi->attr);
2538
2539 /* Restore route, if needed. */
2540 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
2541 bgp_path_info_restore(dest, pi);
2542
2543 /* Mark if nexthop has changed. */
2544 if (!IPV4_ADDR_SAME(&pi->attr->nexthop, &attr_new->nexthop))
2545 SET_FLAG(pi->flags, BGP_PATH_IGP_CHANGED);
2546
2547 /* Unintern existing, set to new. */
2548 bgp_attr_unintern(&pi->attr);
2549 pi->attr = attr_new;
2550 pi->uptime = bgp_clock();
2551 }
2552
2553 /* Perform route selection and update zebra, if required. */
2554 ret = evpn_route_select_install(bgp, vpn, dest);
2555
2556 /* if the best path is a local path with a non-zero ES
2557 * sync info against the local path may need to be updated
2558 * when a remote path is added/updated (including changes
2559 * from sync-path to remote-path)
2560 */
2561 local_pi = bgp_evpn_route_get_local_path(bgp, dest);
2562 if (local_pi && bgp_evpn_attr_is_local_es(local_pi->attr))
2563 bgp_evpn_update_type2_route_entry(bgp, vpn, dest, local_pi,
2564 __func__);
2565
2566 bgp_dest_unlock_node(dest);
2567
2568 return ret;
2569 }
2570
2571 /*
2572 * Uninstall route entry from the VRF routing table and send message
2573 * to zebra, if appropriate.
2574 */
2575 static int uninstall_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
2576 const struct prefix_evpn *evp,
2577 struct bgp_path_info *parent_pi)
2578 {
2579 struct bgp_dest *dest;
2580 struct bgp_path_info *pi;
2581 int ret = 0;
2582 struct prefix p;
2583 struct prefix *pp = &p;
2584 afi_t afi = 0;
2585 safi_t safi = 0;
2586 char buf[PREFIX_STRLEN];
2587
2588 memset(pp, 0, sizeof(struct prefix));
2589 ip_prefix_from_evpn_prefix(evp, pp);
2590
2591 if (bgp_debug_zebra(NULL)) {
2592 zlog_debug(
2593 "vrf %s: unimport evpn prefix %s parent %p flags 0x%x",
2594 vrf_id_to_name(bgp_vrf->vrf_id),
2595 prefix2str(evp, buf, sizeof(buf)),
2596 parent_pi, parent_pi->flags);
2597 }
2598
2599 /* Locate route within the VRF. */
2600 /* NOTE: There is no RD here. */
2601 if (is_evpn_prefix_ipaddr_v4(evp)) {
2602 afi = AFI_IP;
2603 safi = SAFI_UNICAST;
2604 dest = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
2605 } else {
2606 afi = AFI_IP6;
2607 safi = SAFI_UNICAST;
2608 dest = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
2609 }
2610
2611 if (!dest)
2612 return 0;
2613
2614 /* Find matching route entry. */
2615 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
2616 if (pi->extra
2617 && (struct bgp_path_info *)pi->extra->parent == parent_pi)
2618 break;
2619
2620 if (!pi)
2621 return 0;
2622
2623 if (bgp_debug_zebra(NULL))
2624 zlog_debug(
2625 "... delete dest %p (l %d) pi %p (l %d, f 0x%x)",
2626 dest, bgp_dest_to_rnode(dest)->lock,
2627 pi, pi->lock, 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 int 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 int 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 buf1[PREFIX_STRLEN];
2856 char attr_str[BUFSIZ] = {0};
2857
2858 bgp_dump_attr(pi->attr, attr_str, sizeof(attr_str));
2859
2860 zlog_debug("%s: bgp %u prefix %s with attr %s - DENIED due to self mac",
2861 __func__, bgp_vrf->vrf_id,
2862 prefix2str(evp, buf1, sizeof(buf1)),
2863 attr_str);
2864 }
2865
2866 return 1;
2867 }
2868
2869 return 0;
2870 }
2871
2872 /*
2873 * Install or uninstall mac-ip routes are appropriate for this
2874 * particular VRF.
2875 */
2876 static int install_uninstall_routes_for_vrf(struct bgp *bgp_vrf, int install)
2877 {
2878 afi_t afi;
2879 safi_t safi;
2880 struct bgp_dest *rd_dest, *dest;
2881 struct bgp_table *table;
2882 struct bgp_path_info *pi;
2883 int ret;
2884 char buf[PREFIX_STRLEN];
2885 struct bgp *bgp_evpn = NULL;
2886
2887 afi = AFI_L2VPN;
2888 safi = SAFI_EVPN;
2889 bgp_evpn = bgp_get_evpn();
2890 if (!bgp_evpn)
2891 return -1;
2892
2893 /* Walk entire global routing table and evaluate routes which could be
2894 * imported into this VRF. Note that we need to loop through all global
2895 * routes to determine which route matches the import rt on vrf
2896 */
2897 for (rd_dest = bgp_table_top(bgp_evpn->rib[afi][safi]); rd_dest;
2898 rd_dest = bgp_route_next(rd_dest)) {
2899 table = bgp_dest_get_bgp_table_info(rd_dest);
2900 if (!table)
2901 continue;
2902
2903 for (dest = bgp_table_top(table); dest;
2904 dest = bgp_route_next(dest)) {
2905 const struct prefix_evpn *evp =
2906 (const struct prefix_evpn *)bgp_dest_get_prefix(
2907 dest);
2908
2909 /* if not mac-ip route skip this route */
2910 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
2911 || evp->prefix.route_type
2912 == BGP_EVPN_IP_PREFIX_ROUTE))
2913 continue;
2914
2915 /* if not a mac+ip route skip this route */
2916 if (!(is_evpn_prefix_ipaddr_v4(evp)
2917 || is_evpn_prefix_ipaddr_v6(evp)))
2918 continue;
2919
2920 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
2921 pi = pi->next) {
2922 /* Consider "valid" remote routes applicable for
2923 * this VRF.
2924 */
2925 if (!(CHECK_FLAG(pi->flags, BGP_PATH_VALID)
2926 && pi->type == ZEBRA_ROUTE_BGP
2927 && pi->sub_type == BGP_ROUTE_NORMAL))
2928 continue;
2929
2930 if (is_route_matching_for_vrf(bgp_vrf, pi)) {
2931 if (bgp_evpn_route_rmac_self_check(
2932 bgp_vrf, evp, pi))
2933 continue;
2934
2935 if (install)
2936 ret = install_evpn_route_entry_in_vrf(
2937 bgp_vrf, evp, pi);
2938 else
2939 ret = uninstall_evpn_route_entry_in_vrf(
2940 bgp_vrf, evp, pi);
2941
2942 if (ret) {
2943 flog_err(
2944 EC_BGP_EVPN_FAIL,
2945 "Failed to %s EVPN %s route in VRF %s",
2946 install ? "install"
2947 : "uninstall",
2948 prefix2str(evp, buf,
2949 sizeof(buf)),
2950 vrf_id_to_name(
2951 bgp_vrf->vrf_id));
2952 return ret;
2953 }
2954 }
2955 }
2956 }
2957 }
2958
2959 return 0;
2960 }
2961
2962 /*
2963 * Install or uninstall routes of specified type that are appropriate for this
2964 * particular VNI.
2965 */
2966 static int install_uninstall_routes_for_vni(struct bgp *bgp,
2967 struct bgpevpn *vpn,
2968 bgp_evpn_route_type rtype,
2969 int install)
2970 {
2971 afi_t afi;
2972 safi_t safi;
2973 struct bgp_dest *rd_dest, *dest;
2974 struct bgp_table *table;
2975 struct bgp_path_info *pi;
2976 int ret;
2977
2978 afi = AFI_L2VPN;
2979 safi = SAFI_EVPN;
2980
2981 /* Walk entire global routing table and evaluate routes which could be
2982 * imported into this VPN. Note that we cannot just look at the routes
2983 * for
2984 * the VNI's RD - remote routes applicable for this VNI could have any
2985 * RD.
2986 */
2987 /* EVPN routes are a 2-level table. */
2988 for (rd_dest = bgp_table_top(bgp->rib[afi][safi]); rd_dest;
2989 rd_dest = bgp_route_next(rd_dest)) {
2990 table = bgp_dest_get_bgp_table_info(rd_dest);
2991 if (!table)
2992 continue;
2993
2994 for (dest = bgp_table_top(table); dest;
2995 dest = bgp_route_next(dest)) {
2996 const struct prefix_evpn *evp =
2997 (const struct prefix_evpn *)bgp_dest_get_prefix(
2998 dest);
2999
3000 if (evp->prefix.route_type != rtype)
3001 continue;
3002
3003 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
3004 pi = pi->next) {
3005 /* Consider "valid" remote routes applicable for
3006 * this VNI. */
3007 if (!(CHECK_FLAG(pi->flags, BGP_PATH_VALID)
3008 && pi->type == ZEBRA_ROUTE_BGP
3009 && pi->sub_type == BGP_ROUTE_NORMAL))
3010 continue;
3011
3012 if (is_route_matching_for_vni(bgp, vpn, pi)) {
3013 if (install)
3014 ret = install_evpn_route_entry(
3015 bgp, vpn, evp, pi);
3016 else
3017 ret = uninstall_evpn_route_entry(
3018 bgp, vpn, evp, pi);
3019
3020 if (ret) {
3021 flog_err(
3022 EC_BGP_EVPN_FAIL,
3023 "%u: Failed to %s EVPN %s route in VNI %u",
3024 bgp->vrf_id,
3025 install ? "install"
3026 : "uninstall",
3027 rtype == BGP_EVPN_MAC_IP_ROUTE
3028 ? "MACIP"
3029 : "IMET",
3030 vpn->vni);
3031 return ret;
3032 }
3033 }
3034 }
3035 }
3036 }
3037
3038 return 0;
3039 }
3040
3041 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
3042 * is invoked upon l3vni-add or l3vni import rt change
3043 */
3044 static int install_routes_for_vrf(struct bgp *bgp_vrf)
3045 {
3046 install_uninstall_routes_for_vrf(bgp_vrf, 1);
3047 return 0;
3048 }
3049
3050 /*
3051 * Install any existing remote routes applicable for this VNI into its
3052 * routing table. This is invoked when a VNI becomes "live" or its Import
3053 * RT is changed.
3054 */
3055 static int install_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
3056 {
3057 int ret;
3058
3059 /* Install type-3 routes followed by type-2 routes - the ones applicable
3060 * for this VNI.
3061 */
3062 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
3063 1);
3064 if (ret)
3065 return ret;
3066
3067 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_AD_ROUTE,
3068 1);
3069 if (ret)
3070 return ret;
3071
3072 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
3073 1);
3074 }
3075
3076 /* uninstall routes from l3vni vrf. */
3077 static int uninstall_routes_for_vrf(struct bgp *bgp_vrf)
3078 {
3079 install_uninstall_routes_for_vrf(bgp_vrf, 0);
3080 return 0;
3081 }
3082
3083 /*
3084 * Uninstall any existing remote routes for this VNI. One scenario in which
3085 * this is invoked is upon an import RT change.
3086 */
3087 static int uninstall_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
3088 {
3089 int ret;
3090
3091 /* Uninstall type-2 routes followed by type-3 routes - the ones
3092 * applicable
3093 * for this VNI.
3094 */
3095 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
3096 0);
3097 if (ret)
3098 return ret;
3099
3100 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_AD_ROUTE,
3101 1);
3102 if (ret)
3103 return ret;
3104
3105
3106 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
3107 0);
3108 }
3109
3110 /*
3111 * Install or uninstall route in matching VRFs (list).
3112 */
3113 static int install_uninstall_route_in_vrfs(struct bgp *bgp_def, afi_t afi,
3114 safi_t safi, struct prefix_evpn *evp,
3115 struct bgp_path_info *pi,
3116 struct list *vrfs, int install)
3117 {
3118 char buf[PREFIX2STR_BUFFER];
3119 struct bgp *bgp_vrf;
3120 struct listnode *node, *nnode;
3121
3122 /* Only type-2/type-5 routes go into a VRF */
3123 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3124 || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
3125 return 0;
3126
3127 /* if it is type-2 route and not a mac+ip route skip this route */
3128 if ((evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
3129 && !(is_evpn_prefix_ipaddr_v4(evp)
3130 || is_evpn_prefix_ipaddr_v6(evp)))
3131 return 0;
3132
3133 for (ALL_LIST_ELEMENTS(vrfs, node, nnode, bgp_vrf)) {
3134 int ret;
3135
3136 if (install)
3137 ret = install_evpn_route_entry_in_vrf(bgp_vrf, evp, pi);
3138 else
3139 ret = uninstall_evpn_route_entry_in_vrf(bgp_vrf, evp,
3140 pi);
3141
3142 if (ret) {
3143 flog_err(EC_BGP_EVPN_FAIL,
3144 "%u: Failed to %s prefix %s in VRF %s",
3145 bgp_def->vrf_id,
3146 install ? "install" : "uninstall",
3147 prefix2str(evp, buf, sizeof(buf)),
3148 vrf_id_to_name(bgp_vrf->vrf_id));
3149 return ret;
3150 }
3151 }
3152
3153 return 0;
3154 }
3155
3156 /*
3157 * Install or uninstall route in matching VNIs (list).
3158 */
3159 static int install_uninstall_route_in_vnis(struct bgp *bgp, afi_t afi,
3160 safi_t safi, struct prefix_evpn *evp,
3161 struct bgp_path_info *pi,
3162 struct list *vnis, int install)
3163 {
3164 struct bgpevpn *vpn;
3165 struct listnode *node, *nnode;
3166
3167 for (ALL_LIST_ELEMENTS(vnis, node, nnode, vpn)) {
3168 int ret;
3169
3170 if (!is_vni_live(vpn))
3171 continue;
3172
3173 if (install)
3174 ret = install_evpn_route_entry(bgp, vpn, evp, pi);
3175 else
3176 ret = uninstall_evpn_route_entry(bgp, vpn, evp, pi);
3177
3178 if (ret) {
3179 flog_err(EC_BGP_EVPN_FAIL,
3180 "%u: Failed to %s EVPN %s route in VNI %u",
3181 bgp->vrf_id, install ? "install" : "uninstall",
3182 evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3183 ? "MACIP"
3184 : "IMET",
3185 vpn->vni);
3186 return ret;
3187 }
3188 }
3189
3190 return 0;
3191 }
3192
3193 /*
3194 * Install or uninstall route for appropriate VNIs/ESIs.
3195 */
3196 static int install_uninstall_evpn_route(struct bgp *bgp, afi_t afi, safi_t safi,
3197 const struct prefix *p,
3198 struct bgp_path_info *pi, int import)
3199 {
3200 struct prefix_evpn *evp = (struct prefix_evpn *)p;
3201 struct attr *attr = pi->attr;
3202 struct ecommunity *ecom;
3203 int i;
3204 struct prefix_evpn ad_evp;
3205
3206 assert(attr);
3207
3208 /* Only type-2, type-3, type-4 and type-5 are supported currently */
3209 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
3210 || evp->prefix.route_type == BGP_EVPN_IMET_ROUTE
3211 || evp->prefix.route_type == BGP_EVPN_ES_ROUTE
3212 || evp->prefix.route_type == BGP_EVPN_AD_ROUTE
3213 || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
3214 return 0;
3215
3216 /* If we don't have Route Target, nothing much to do. */
3217 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
3218 return 0;
3219
3220 /* EAD prefix in the global table doesn't include the VTEP-IP so
3221 * we need to create a different copy for the VNI
3222 */
3223 if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE)
3224 evp = evpn_type1_prefix_vni_copy(&ad_evp, evp, attr->nexthop);
3225
3226 ecom = attr->ecommunity;
3227 if (!ecom || !ecom->size)
3228 return -1;
3229
3230 /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs
3231 * attached to the route */
3232 for (i = 0; i < ecom->size; i++) {
3233 uint8_t *pnt;
3234 uint8_t type, sub_type;
3235 struct ecommunity_val *eval;
3236 struct ecommunity_val eval_tmp;
3237 struct irt_node *irt; /* import rt for l2vni */
3238 struct vrf_irt_node *vrf_irt; /* import rt for l3vni */
3239 struct bgp_evpn_es *es;
3240
3241 /* Only deal with RTs */
3242 pnt = (ecom->val + (i * ecom->unit_size));
3243 eval = (struct ecommunity_val *)(ecom->val
3244 + (i * ecom->unit_size));
3245 type = *pnt++;
3246 sub_type = *pnt++;
3247 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
3248 continue;
3249
3250 /*
3251 * macip routes (type-2) are imported into VNI and VRF tables.
3252 * IMET route is imported into VNI table.
3253 * prefix routes are imported into VRF table.
3254 */
3255 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE ||
3256 evp->prefix.route_type == BGP_EVPN_IMET_ROUTE ||
3257 evp->prefix.route_type == BGP_EVPN_AD_ROUTE ||
3258 evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE) {
3259
3260 irt = lookup_import_rt(bgp, eval);
3261 if (irt)
3262 install_uninstall_route_in_vnis(
3263 bgp, afi, safi, evp, pi, irt->vnis,
3264 import);
3265
3266 vrf_irt = lookup_vrf_import_rt(eval);
3267 if (vrf_irt)
3268 install_uninstall_route_in_vrfs(
3269 bgp, afi, safi, evp, pi, vrf_irt->vrfs,
3270 import);
3271
3272 /* Also check for non-exact match.
3273 * In this, we mask out the AS and
3274 * only check on the local-admin sub-field.
3275 * This is to facilitate using
3276 * VNI as the RT for EBGP peering too.
3277 */
3278 irt = NULL;
3279 vrf_irt = NULL;
3280 if (type == ECOMMUNITY_ENCODE_AS
3281 || type == ECOMMUNITY_ENCODE_AS4
3282 || type == ECOMMUNITY_ENCODE_IP) {
3283 memcpy(&eval_tmp, eval, ecom->unit_size);
3284 mask_ecom_global_admin(&eval_tmp, eval);
3285 irt = lookup_import_rt(bgp, &eval_tmp);
3286 vrf_irt = lookup_vrf_import_rt(&eval_tmp);
3287 }
3288
3289 if (irt)
3290 install_uninstall_route_in_vnis(
3291 bgp, afi, safi, evp, pi, irt->vnis,
3292 import);
3293 if (vrf_irt)
3294 install_uninstall_route_in_vrfs(
3295 bgp, afi, safi, evp, pi, vrf_irt->vrfs,
3296 import);
3297 }
3298
3299 /* es route is imported into the es table */
3300 if (evp->prefix.route_type == BGP_EVPN_ES_ROUTE) {
3301
3302 /* we will match based on the entire esi to avoid
3303 * imoort of an es route for esi2 into esi1
3304 */
3305 es = bgp_evpn_es_find(&evp->prefix.es_addr.esi);
3306 if (es && bgp_evpn_is_es_local(es))
3307 bgp_evpn_es_route_install_uninstall(
3308 bgp, es, afi, safi, evp, pi, import);
3309 }
3310 }
3311
3312 return 0;
3313 }
3314
3315 /*
3316 * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
3317 * routes
3318 */
3319 static void delete_withdraw_vrf_routes(struct bgp *bgp_vrf)
3320 {
3321 /* Delete ipv4 default route and withdraw from peers */
3322 if (evpn_default_originate_set(bgp_vrf, AFI_IP, SAFI_UNICAST))
3323 bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP,
3324 SAFI_UNICAST, false);
3325
3326 /* delete all ipv4 routes and withdraw from peers */
3327 if (advertise_type5_routes(bgp_vrf, AFI_IP))
3328 bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);
3329
3330 /* Delete ipv6 default route and withdraw from peers */
3331 if (evpn_default_originate_set(bgp_vrf, AFI_IP6, SAFI_UNICAST))
3332 bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP6,
3333 SAFI_UNICAST, false);
3334
3335 /* delete all ipv6 routes and withdraw from peers */
3336 if (advertise_type5_routes(bgp_vrf, AFI_IP6))
3337 bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
3338 }
3339
3340 /*
3341 * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
3342 * routes
3343 */
3344 void update_advertise_vrf_routes(struct bgp *bgp_vrf)
3345 {
3346 struct bgp *bgp_evpn = NULL; /* EVPN bgp instance */
3347
3348 bgp_evpn = bgp_get_evpn();
3349 if (!bgp_evpn)
3350 return;
3351
3352 /* update all ipv4 routes */
3353 if (advertise_type5_routes(bgp_vrf, AFI_IP))
3354 bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);
3355
3356 /* update ipv4 default route and withdraw from peers */
3357 if (evpn_default_originate_set(bgp_vrf, AFI_IP, SAFI_UNICAST))
3358 bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP,
3359 SAFI_UNICAST, true);
3360
3361 /* update all ipv6 routes */
3362 if (advertise_type5_routes(bgp_vrf, AFI_IP6))
3363 bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
3364
3365 /* update ipv6 default route and withdraw from peers */
3366 if (evpn_default_originate_set(bgp_vrf, AFI_IP6, SAFI_UNICAST))
3367 bgp_evpn_install_uninstall_default_route(bgp_vrf, AFI_IP6,
3368 SAFI_UNICAST, true);
3369
3370 }
3371
3372 /*
3373 * update and advertise local routes for a VRF as type-5 routes.
3374 * This is invoked upon RD change for a VRF. Note taht the processing is only
3375 * done in the global route table using the routes which already exist in the
3376 * VRF routing table
3377 */
3378 static void update_router_id_vrf(struct bgp *bgp_vrf)
3379 {
3380 /* skip if the RD is configured */
3381 if (is_vrf_rd_configured(bgp_vrf))
3382 return;
3383
3384 /* derive the RD for the VRF based on new router-id */
3385 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
3386
3387 /* update advertise ipv4|ipv6 routes as type-5 routes */
3388 update_advertise_vrf_routes(bgp_vrf);
3389 }
3390
3391 /*
3392 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
3393 * This is invoked upon VRF RD change. The processing is done only from global
3394 * table.
3395 */
3396 static void withdraw_router_id_vrf(struct bgp *bgp_vrf)
3397 {
3398 /* skip if the RD is configured */
3399 if (is_vrf_rd_configured(bgp_vrf))
3400 return;
3401
3402 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
3403 delete_withdraw_vrf_routes(bgp_vrf);
3404 }
3405
3406 /*
3407 * Update and advertise local routes for a VNI. Invoked upon router-id
3408 * change. Note that the processing is done only on the global route table
3409 * using routes that already exist in the per-VNI table.
3410 */
3411 static int update_advertise_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
3412 {
3413 struct prefix_evpn p;
3414 struct bgp_dest *dest, *global_dest;
3415 struct bgp_path_info *pi, *global_pi;
3416 struct attr *attr;
3417 afi_t afi = AFI_L2VPN;
3418 safi_t safi = SAFI_EVPN;
3419
3420 /* Locate type-3 route for VNI in the per-VNI table and use its
3421 * attributes to create and advertise the type-3 route for this VNI
3422 * in the global table.
3423 *
3424 * RT-3 only if doing head-end replication
3425 */
3426 if (bgp_evpn_vni_flood_mode_get(bgp, vpn)
3427 == VXLAN_FLOOD_HEAD_END_REPL) {
3428 build_evpn_type3_prefix(&p, vpn->originator_ip);
3429 dest = bgp_node_lookup(vpn->route_table, (struct prefix *)&p);
3430 if (!dest) /* unexpected */
3431 return 0;
3432 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
3433 if (pi->peer == bgp->peer_self &&
3434 pi->type == ZEBRA_ROUTE_BGP
3435 && pi->sub_type == BGP_ROUTE_STATIC)
3436 break;
3437 if (!pi) /* unexpected */
3438 return 0;
3439 attr = pi->attr;
3440
3441 global_dest = bgp_global_evpn_node_get(bgp->rib[afi][safi],
3442 afi, safi, &p, &vpn->prd);
3443 update_evpn_route_entry(bgp, vpn, afi, safi, global_dest, attr,
3444 1, &pi, 0, mac_mobility_seqnum(attr),
3445 false /* setup_sync */, NULL /* old_is_sync */);
3446
3447 /* Schedule for processing and unlock node. */
3448 bgp_process(bgp, global_dest, afi, safi);
3449 bgp_dest_unlock_node(global_dest);
3450 }
3451
3452 /* Now, walk this VNI's route table and use the route and its attribute
3453 * to create and schedule route in global table.
3454 */
3455 for (dest = bgp_table_top(vpn->route_table); dest;
3456 dest = bgp_route_next(dest)) {
3457 const struct prefix_evpn *evp =
3458 (const struct prefix_evpn *)bgp_dest_get_prefix(dest);
3459
3460 /* Identify MAC-IP local routes. */
3461 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
3462 continue;
3463
3464 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
3465 if (pi->peer == bgp->peer_self
3466 && pi->type == ZEBRA_ROUTE_BGP
3467 && pi->sub_type == BGP_ROUTE_STATIC)
3468 break;
3469 if (!pi)
3470 continue;
3471
3472 /* Create route in global routing table using this route entry's
3473 * attribute.
3474 */
3475 attr = pi->attr;
3476 global_dest = bgp_global_evpn_node_get(bgp->rib[afi][safi], afi, safi,
3477 evp, &vpn->prd);
3478 assert(global_dest);
3479 update_evpn_route_entry(bgp, vpn, afi, safi, global_dest, attr, 1,
3480 &global_pi, 0,
3481 mac_mobility_seqnum(attr),
3482 false /* setup_sync */, NULL /* old_is_sync */);
3483
3484 /* Schedule for processing and unlock node. */
3485 bgp_process(bgp, global_dest, afi, safi);
3486 bgp_dest_unlock_node(global_dest);
3487 }
3488
3489 return 0;
3490 }
3491
3492 /*
3493 * Delete (and withdraw) local routes for a VNI - only from the global
3494 * table. Invoked upon router-id change.
3495 */
3496 static int delete_withdraw_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
3497 {
3498 int ret;
3499 struct prefix_evpn p;
3500 struct bgp_dest *global_dest;
3501 struct bgp_path_info *pi;
3502 afi_t afi = AFI_L2VPN;
3503 safi_t safi = SAFI_EVPN;
3504
3505 /* Delete and withdraw locally learnt type-2 routes (MACIP)
3506 * for this VNI - from the global table.
3507 */
3508 ret = delete_global_type2_routes(bgp, vpn);
3509 if (ret)
3510 return ret;
3511
3512 /* Remove type-3 route for this VNI from global table. */
3513 build_evpn_type3_prefix(&p, vpn->originator_ip);
3514 global_dest = bgp_global_evpn_node_lookup(bgp->rib[afi][safi], afi, safi,
3515 (const struct prefix_evpn *)&p, &vpn->prd);
3516 if (global_dest) {
3517 /* Delete route entry in the global EVPN table. */
3518 delete_evpn_route_entry(bgp, afi, safi, global_dest, &pi);
3519
3520 /* Schedule for processing - withdraws to peers happen from
3521 * this table.
3522 */
3523 if (pi)
3524 bgp_process(bgp, global_dest, afi, safi);
3525 bgp_dest_unlock_node(global_dest);
3526 }
3527
3528 return 0;
3529 }
3530
3531 /*
3532 * Handle router-id change. Update and advertise local routes corresponding
3533 * to this VNI from peers. Note that this is invoked after updating the
3534 * router-id. The routes in the per-VNI table are used to create routes in
3535 * the global table and schedule them.
3536 */
3537 static void update_router_id_vni(struct hash_bucket *bucket, struct bgp *bgp)
3538 {
3539 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
3540
3541 /* Skip VNIs with configured RD. */
3542 if (is_rd_configured(vpn))
3543 return;
3544
3545 bgp_evpn_derive_auto_rd(bgp, vpn);
3546 update_advertise_vni_routes(bgp, vpn);
3547 }
3548
3549 /*
3550 * Handle router-id change. Delete and withdraw local routes corresponding
3551 * to this VNI from peers. Note that this is invoked prior to updating
3552 * the router-id and is done only on the global route table, the routes
3553 * are needed in the per-VNI table to re-advertise with new router id.
3554 */
3555 static void withdraw_router_id_vni(struct hash_bucket *bucket, struct bgp *bgp)
3556 {
3557 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
3558
3559 /* Skip VNIs with configured RD. */
3560 if (is_rd_configured(vpn))
3561 return;
3562
3563 delete_withdraw_vni_routes(bgp, vpn);
3564 }
3565
3566 /*
3567 * Create RT-3 for a VNI and schedule for processing and advertisement.
3568 * This is invoked upon flooding mode changing to head-end replication.
3569 */
3570 static void create_advertise_type3(struct hash_bucket *bucket, void *data)
3571 {
3572 struct bgpevpn *vpn = bucket->data;
3573 struct bgp *bgp = data;
3574 struct prefix_evpn p;
3575
3576 if (!vpn || !is_vni_live(vpn) ||
3577 bgp_evpn_vni_flood_mode_get(bgp, vpn)
3578 != VXLAN_FLOOD_HEAD_END_REPL)
3579 return;
3580
3581 build_evpn_type3_prefix(&p, vpn->originator_ip);
3582 if (update_evpn_route(bgp, vpn, &p, 0, 0, NULL))
3583 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
3584 "Type3 route creation failure for VNI %u", vpn->vni);
3585 }
3586
3587 /*
3588 * Delete RT-3 for a VNI and schedule for processing and withdrawal.
3589 * This is invoked upon flooding mode changing to drop BUM packets.
3590 */
3591 static void delete_withdraw_type3(struct hash_bucket *bucket, void *data)
3592 {
3593 struct bgpevpn *vpn = bucket->data;
3594 struct bgp *bgp = data;
3595 struct prefix_evpn p;
3596
3597 if (!vpn || !is_vni_live(vpn))
3598 return;
3599
3600 build_evpn_type3_prefix(&p, vpn->originator_ip);
3601 delete_evpn_route(bgp, vpn, &p);
3602 }
3603
3604 /*
3605 * Process received EVPN type-2 route (advertise or withdraw).
3606 */
3607 static int process_type2_route(struct peer *peer, afi_t afi, safi_t safi,
3608 struct attr *attr, uint8_t *pfx, int psize,
3609 uint32_t addpath_id)
3610 {
3611 struct prefix_rd prd;
3612 struct prefix_evpn p = {};
3613 struct bgp_route_evpn evpn = {};
3614 uint8_t ipaddr_len;
3615 uint8_t macaddr_len;
3616 /* holds the VNI(s) as in packet */
3617 mpls_label_t label[BGP_MAX_LABELS] = {};
3618 uint32_t num_labels = 0;
3619 uint32_t eth_tag;
3620 int ret;
3621
3622 /* Type-2 route should be either 33, 37 or 49 bytes or an
3623 * additional 3 bytes if there is a second label (VNI):
3624 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
3625 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
3626 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
3627 */
3628 if (psize != 33 && psize != 37 && psize != 49 && psize != 36
3629 && psize != 40 && psize != 52) {
3630 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
3631 "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
3632 peer->bgp->vrf_id, peer->host, psize);
3633 return -1;
3634 }
3635
3636 struct stream *pkt = stream_new(psize);
3637 stream_put(pkt, pfx, psize);
3638
3639 /* Make prefix_rd */
3640 prd.family = AF_UNSPEC;
3641 prd.prefixlen = 64;
3642
3643 STREAM_GET(&prd.val, pkt, 8);
3644
3645 /* Make EVPN prefix. */
3646 p.family = AF_EVPN;
3647 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
3648 p.prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
3649
3650 /* Copy Ethernet Seg Identifier */
3651 if (attr) {
3652 STREAM_GET(&attr->esi, pkt, sizeof(esi_t));
3653
3654 if (bgp_evpn_is_esi_local(&attr->esi))
3655 attr->es_flags |= ATTR_ES_IS_LOCAL;
3656 else
3657 attr->es_flags &= ~ATTR_ES_IS_LOCAL;
3658 } else {
3659 STREAM_FORWARD_GETP(pkt, sizeof(esi_t));
3660 }
3661
3662 /* Copy Ethernet Tag */
3663 STREAM_GET(&eth_tag, pkt, 4);
3664 p.prefix.macip_addr.eth_tag = ntohl(eth_tag);
3665
3666 /* Get the MAC Addr len */
3667 STREAM_GETC(pkt, macaddr_len);
3668
3669 /* Get the MAC Addr */
3670 if (macaddr_len == (ETH_ALEN * 8)) {
3671 STREAM_GET(&p.prefix.macip_addr.mac.octet, pkt, ETH_ALEN);
3672 } else {
3673 flog_err(
3674 EC_BGP_EVPN_ROUTE_INVALID,
3675 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
3676 peer->bgp->vrf_id, peer->host, macaddr_len);
3677 goto fail;
3678 }
3679
3680
3681 /* Get the IP. */
3682 STREAM_GETC(pkt, ipaddr_len);
3683
3684 if (ipaddr_len != 0 && ipaddr_len != IPV4_MAX_BITLEN
3685 && ipaddr_len != IPV6_MAX_BITLEN) {
3686 flog_err(
3687 EC_BGP_EVPN_ROUTE_INVALID,
3688 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
3689 peer->bgp->vrf_id, peer->host, ipaddr_len);
3690 goto fail;
3691 }
3692
3693 if (ipaddr_len) {
3694 ipaddr_len /= 8; /* Convert to bytes. */
3695 p.prefix.macip_addr.ip.ipa_type = (ipaddr_len == IPV4_MAX_BYTELEN)
3696 ? IPADDR_V4
3697 : IPADDR_V6;
3698 STREAM_GET(&p.prefix.macip_addr.ip.ip.addr, pkt, ipaddr_len);
3699 }
3700
3701 /* Get the VNI(s). Stored as bytes here. */
3702 STREAM_GET(&label[0], pkt, BGP_LABEL_BYTES);
3703 num_labels++;
3704
3705 /* Do we have a second VNI? */
3706 if (STREAM_READABLE(pkt)) {
3707 num_labels++;
3708 STREAM_GET(&label[1], pkt, BGP_LABEL_BYTES);
3709 }
3710
3711 /* Process the route. */
3712 if (attr)
3713 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
3714 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
3715 &prd, &label[0], num_labels, 0, &evpn);
3716 else
3717 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
3718 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
3719 &prd, &label[0], num_labels, &evpn);
3720 goto done;
3721
3722 fail:
3723 stream_failure:
3724 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
3725 "%u:%s - Rx EVPN Type-2 NLRI - corrupt, discarding",
3726 peer->bgp->vrf_id, peer->host);
3727 ret = -1;
3728 done:
3729 stream_free(pkt);
3730 return ret;
3731 }
3732
3733 /*
3734 * Process received EVPN type-3 route (advertise or withdraw).
3735 */
3736 static int process_type3_route(struct peer *peer, afi_t afi, safi_t safi,
3737 struct attr *attr, uint8_t *pfx, int psize,
3738 uint32_t addpath_id)
3739 {
3740 struct prefix_rd prd;
3741 struct prefix_evpn p;
3742 uint8_t ipaddr_len;
3743 uint32_t eth_tag;
3744 int ret;
3745
3746 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
3747 * IP len (1) and IP (4 or 16).
3748 */
3749 if (psize != 17 && psize != 29) {
3750 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
3751 "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
3752 peer->bgp->vrf_id, peer->host, psize);
3753 return -1;
3754 }
3755
3756 /* If PMSI is present, log if it is anything other than IR.
3757 * Note: We just simply ignore the values as it is not clear if
3758 * doing anything else is better.
3759 */
3760 if (attr &&
3761 (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL))) {
3762 if (attr->pmsi_tnl_type != PMSI_TNLTYPE_INGR_REPL &&
3763 attr->pmsi_tnl_type != PMSI_TNLTYPE_PIM_SM) {
3764 flog_warn(EC_BGP_EVPN_PMSI_PRESENT,
3765 "%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
3766 peer->bgp->vrf_id, peer->host,
3767 attr->pmsi_tnl_type);
3768 }
3769 }
3770
3771 /* Make prefix_rd */
3772 prd.family = AF_UNSPEC;
3773 prd.prefixlen = 64;
3774 memcpy(&prd.val, pfx, 8);
3775 pfx += 8;
3776
3777 /* Make EVPN prefix. */
3778 memset(&p, 0, sizeof(struct prefix_evpn));
3779 p.family = AF_EVPN;
3780 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
3781 p.prefix.route_type = BGP_EVPN_IMET_ROUTE;
3782
3783 /* Copy Ethernet Tag */
3784 memcpy(&eth_tag, pfx, 4);
3785 p.prefix.imet_addr.eth_tag = ntohl(eth_tag);
3786 pfx += 4;
3787
3788 /* Get the IP. */
3789 ipaddr_len = *pfx++;
3790 if (ipaddr_len == IPV4_MAX_BITLEN) {
3791 p.prefix.imet_addr.ip.ipa_type = IPADDR_V4;
3792 memcpy(&p.prefix.imet_addr.ip.ip.addr, pfx, IPV4_MAX_BYTELEN);
3793 } else {
3794 flog_err(
3795 EC_BGP_EVPN_ROUTE_INVALID,
3796 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
3797 peer->bgp->vrf_id, peer->host, ipaddr_len);
3798 return -1;
3799 }
3800
3801 /* Process the route. */
3802 if (attr)
3803 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
3804 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
3805 &prd, NULL, 0, 0, NULL);
3806 else
3807 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
3808 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
3809 &prd, NULL, 0, NULL);
3810 return ret;
3811 }
3812
3813 /*
3814 * Process received EVPN type-5 route (advertise or withdraw).
3815 */
3816 static int process_type5_route(struct peer *peer, afi_t afi, safi_t safi,
3817 struct attr *attr, uint8_t *pfx, int psize,
3818 uint32_t addpath_id)
3819 {
3820 struct prefix_rd prd;
3821 struct prefix_evpn p;
3822 struct bgp_route_evpn evpn;
3823 uint8_t ippfx_len;
3824 uint32_t eth_tag;
3825 mpls_label_t label; /* holds the VNI as in the packet */
3826 int ret;
3827 afi_t gw_afi;
3828 bool is_valid_update = false;
3829
3830 /* Type-5 route should be 34 or 58 bytes:
3831 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3832 * GW (4 or 16) and VNI (3).
3833 * Note that the IP and GW should both be IPv4 or both IPv6.
3834 */
3835 if (psize != 34 && psize != 58) {
3836 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
3837 "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3838 peer->bgp->vrf_id, peer->host, psize);
3839 return -1;
3840 }
3841
3842 /* Make prefix_rd */
3843 prd.family = AF_UNSPEC;
3844 prd.prefixlen = 64;
3845 memcpy(&prd.val, pfx, 8);
3846 pfx += 8;
3847
3848 /* Make EVPN prefix. */
3849 memset(&p, 0, sizeof(struct prefix_evpn));
3850 p.family = AF_EVPN;
3851 p.prefixlen = EVPN_ROUTE_PREFIXLEN;
3852 p.prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
3853
3854 /* Additional information outside of prefix - ESI and GW IP */
3855 memset(&evpn, 0, sizeof(evpn));
3856
3857 /* Fetch ESI */
3858 if (attr)
3859 memcpy(&attr->esi, pfx, sizeof(esi_t));
3860 pfx += ESI_BYTES;
3861
3862 /* Fetch Ethernet Tag. */
3863 memcpy(&eth_tag, pfx, 4);
3864 p.prefix.prefix_addr.eth_tag = ntohl(eth_tag);
3865 pfx += 4;
3866
3867 /* Fetch IP prefix length. */
3868 ippfx_len = *pfx++;
3869 if (ippfx_len > IPV6_MAX_BITLEN) {
3870 flog_err(
3871 EC_BGP_EVPN_ROUTE_INVALID,
3872 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3873 peer->bgp->vrf_id, peer->host, ippfx_len);
3874 return -1;
3875 }
3876 p.prefix.prefix_addr.ip_prefix_length = ippfx_len;
3877
3878 /* Determine IPv4 or IPv6 prefix */
3879 /* Since the address and GW are from the same family, this just becomes
3880 * a simple check on the total size.
3881 */
3882 if (psize == 34) {
3883 SET_IPADDR_V4(&p.prefix.prefix_addr.ip);
3884 memcpy(&p.prefix.prefix_addr.ip.ipaddr_v4, pfx, 4);
3885 pfx += 4;
3886 memcpy(&evpn.gw_ip.ipv4, pfx, 4);
3887 pfx += 4;
3888 gw_afi = AF_INET;
3889 } else {
3890 SET_IPADDR_V6(&p.prefix.prefix_addr.ip);
3891 memcpy(&p.prefix.prefix_addr.ip.ipaddr_v6, pfx, 16);
3892 pfx += 16;
3893 memcpy(&evpn.gw_ip.ipv6, pfx, 16);
3894 pfx += 16;
3895 gw_afi = AF_INET6;
3896 }
3897
3898 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3899 memset(&label, 0, sizeof(label));
3900 memcpy(&label, pfx, BGP_LABEL_BYTES);
3901
3902 /*
3903 * If in future, we are required to access additional fields,
3904 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
3905 * field
3906 */
3907
3908 if (attr) {
3909 is_valid_update = true;
3910 if (is_zero_mac(&attr->rmac) &&
3911 is_zero_gw_ip(&evpn.gw_ip, gw_afi))
3912 is_valid_update = false;
3913
3914 if (is_mcast_mac(&attr->rmac) || is_bcast_mac(&attr->rmac))
3915 is_valid_update = false;
3916 }
3917
3918 /* Process the route. */
3919 if (is_valid_update)
3920 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
3921 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
3922 &prd, &label, 1, 0, &evpn);
3923 else
3924 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
3925 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
3926 &prd, &label, 1, &evpn);
3927
3928 return ret;
3929 }
3930
3931 static void evpn_mpattr_encode_type5(struct stream *s, const struct prefix *p,
3932 const struct prefix_rd *prd,
3933 mpls_label_t *label, uint32_t num_labels,
3934 struct attr *attr)
3935 {
3936 int len;
3937 char temp[16];
3938 const struct evpn_addr *p_evpn_p;
3939
3940 memset(&temp, 0, 16);
3941 if (p->family != AF_EVPN)
3942 return;
3943 p_evpn_p = &(p->u.prefix_evpn);
3944
3945 /* len denites the total len of IP and GW-IP in the route
3946 IP and GW-IP have to be both ipv4 or ipv6
3947 */
3948 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
3949 len = 8; /* IP and GWIP are both ipv4 */
3950 else
3951 len = 32; /* IP and GWIP are both ipv6 */
3952 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3953 stream_putc(s, 8 + 10 + 4 + 1 + len + 3);
3954 stream_put(s, prd->val, 8);
3955 if (attr)
3956 stream_put(s, &attr->esi, sizeof(esi_t));
3957 else
3958 stream_put(s, 0, sizeof(esi_t));
3959 stream_putl(s, p_evpn_p->prefix_addr.eth_tag);
3960 stream_putc(s, p_evpn_p->prefix_addr.ip_prefix_length);
3961 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
3962 stream_put_ipv4(s, p_evpn_p->prefix_addr.ip.ipaddr_v4.s_addr);
3963 else
3964 stream_put(s, &p_evpn_p->prefix_addr.ip.ipaddr_v6, 16);
3965 if (attr) {
3966 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
3967 stream_put_ipv4(s,
3968 attr->evpn_overlay.gw_ip.ipv4.s_addr);
3969 else
3970 stream_put(s, &(attr->evpn_overlay.gw_ip.ipv6), 16);
3971 } else {
3972 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
3973 stream_put_ipv4(s, 0);
3974 else
3975 stream_put(s, &temp, 16);
3976 }
3977
3978 if (num_labels)
3979 stream_put(s, label, 3);
3980 else
3981 stream_put3(s, 0);
3982 }
3983
3984 /*
3985 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3986 */
3987 static void cleanup_vni_on_disable(struct hash_bucket *bucket, struct bgp *bgp)
3988 {
3989 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
3990
3991 /* Remove EVPN routes and schedule for processing. */
3992 delete_routes_for_vni(bgp, vpn);
3993
3994 /* Clear "live" flag and see if hash needs to be freed. */
3995 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
3996 if (!is_vni_configured(vpn))
3997 bgp_evpn_free(bgp, vpn);
3998 }
3999
4000 /*
4001 * Free a VNI entry; iterator function called during cleanup.
4002 */
4003 static void free_vni_entry(struct hash_bucket *bucket, struct bgp *bgp)
4004 {
4005 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
4006
4007 delete_all_vni_routes(bgp, vpn);
4008 bgp_evpn_free(bgp, vpn);
4009 }
4010
4011 /*
4012 * Derive AUTO import RT for BGP VRF - L3VNI
4013 */
4014 static void evpn_auto_rt_import_add_for_vrf(struct bgp *bgp_vrf)
4015 {
4016 struct bgp *bgp_evpn = NULL;
4017
4018 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
4019 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
4020
4021 /* Map RT to VRF */
4022 bgp_evpn = bgp_get_evpn();
4023 if (!bgp_evpn)
4024 return;
4025 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
4026 }
4027
4028 /*
4029 * Delete AUTO import RT from BGP VRF - L3VNI
4030 */
4031 static void evpn_auto_rt_import_delete_for_vrf(struct bgp *bgp_vrf)
4032 {
4033 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
4034 }
4035
4036 /*
4037 * Derive AUTO export RT for BGP VRF - L3VNI
4038 */
4039 static void evpn_auto_rt_export_add_for_vrf(struct bgp *bgp_vrf)
4040 {
4041 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
4042 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
4043 }
4044
4045 /*
4046 * Delete AUTO export RT from BGP VRF - L3VNI
4047 */
4048 static void evpn_auto_rt_export_delete_for_vrf(struct bgp *bgp_vrf)
4049 {
4050 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
4051 }
4052
4053 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp *bgp_vrf)
4054 {
4055 struct bgp *bgp_evpn = NULL;
4056 struct listnode *node = NULL;
4057 struct bgpevpn *vpn = NULL;
4058
4059 bgp_evpn = bgp_get_evpn();
4060 if (!bgp_evpn)
4061 return;
4062
4063 /* update all type-5 routes */
4064 update_advertise_vrf_routes(bgp_vrf);
4065
4066 /* update all type-2 routes */
4067 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
4068 update_routes_for_vni(bgp_evpn, vpn);
4069 }
4070
4071 /*
4072 * Handle autort change for a given VNI.
4073 */
4074 static void update_autort_vni(struct hash_bucket *bucket, struct bgp *bgp)
4075 {
4076 struct bgpevpn *vpn = bucket->data;
4077
4078 if (!is_import_rt_configured(vpn)) {
4079 if (is_vni_live(vpn))
4080 bgp_evpn_uninstall_routes(bgp, vpn);
4081 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
4082 list_delete_all_node(vpn->import_rtl);
4083 bgp_evpn_derive_auto_rt_import(bgp, vpn);
4084 if (is_vni_live(vpn))
4085 bgp_evpn_install_routes(bgp, vpn);
4086 }
4087 if (!is_export_rt_configured(vpn)) {
4088 list_delete_all_node(vpn->export_rtl);
4089 bgp_evpn_derive_auto_rt_export(bgp, vpn);
4090 if (is_vni_live(vpn))
4091 bgp_evpn_handle_export_rt_change(bgp, vpn);
4092 }
4093 }
4094
4095 /*
4096 * Public functions.
4097 */
4098
4099 /* withdraw type-5 route corresponding to ip prefix */
4100 void bgp_evpn_withdraw_type5_route(struct bgp *bgp_vrf, const struct prefix *p,
4101 afi_t afi, safi_t safi)
4102 {
4103 int ret = 0;
4104 struct prefix_evpn evp;
4105 char buf[PREFIX_STRLEN];
4106
4107 build_type5_prefix_from_ip_prefix(&evp, p);
4108 ret = delete_evpn_type5_route(bgp_vrf, &evp);
4109 if (ret) {
4110 flog_err(
4111 EC_BGP_EVPN_ROUTE_DELETE,
4112 "%u failed to delete type-5 route for prefix %s in vrf %s",
4113 bgp_vrf->vrf_id, prefix2str(p, buf, sizeof(buf)),
4114 vrf_id_to_name(bgp_vrf->vrf_id));
4115 }
4116 }
4117
4118 /* withdraw all type-5 routes for an address family */
4119 void bgp_evpn_withdraw_type5_routes(struct bgp *bgp_vrf, afi_t afi, safi_t safi)
4120 {
4121 struct bgp_table *table = NULL;
4122 struct bgp_dest *dest = NULL;
4123 struct bgp_path_info *pi;
4124
4125 table = bgp_vrf->rib[afi][safi];
4126 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
4127 /* Only care about "selected" routes. Also ensure that
4128 * these are routes that are injectable into EVPN.
4129 */
4130 /* TODO: Support for AddPath for EVPN. */
4131 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
4132 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
4133 && is_route_injectable_into_evpn(pi)) {
4134 bgp_evpn_withdraw_type5_route(
4135 bgp_vrf, bgp_dest_get_prefix(dest), afi,
4136 safi);
4137 break;
4138 }
4139 }
4140 }
4141 }
4142
4143 /*
4144 * evpn - enable advertisement of default g/w
4145 */
4146 void bgp_evpn_install_uninstall_default_route(struct bgp *bgp_vrf, afi_t afi,
4147 safi_t safi, bool add)
4148 {
4149 struct prefix ip_prefix;
4150
4151 /* form the default prefix 0.0.0.0/0 */
4152 memset(&ip_prefix, 0, sizeof(struct prefix));
4153 ip_prefix.family = afi2family(afi);
4154
4155 if (add) {
4156 bgp_evpn_advertise_type5_route(bgp_vrf, &ip_prefix,
4157 NULL, afi, safi);
4158 } else {
4159 bgp_evpn_withdraw_type5_route(bgp_vrf, &ip_prefix,
4160 afi, safi);
4161 }
4162 }
4163
4164
4165 /*
4166 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
4167 * to this function correspond to those of the source IP prefix (best
4168 * path in the case of the attr. In the case of a local prefix (when we
4169 * are advertising local subnets), the src_attr will be NULL.
4170 */
4171 void bgp_evpn_advertise_type5_route(struct bgp *bgp_vrf, const struct prefix *p,
4172 struct attr *src_attr, afi_t afi,
4173 safi_t safi)
4174 {
4175 int ret = 0;
4176 struct prefix_evpn evp;
4177 char buf[PREFIX_STRLEN];
4178
4179 build_type5_prefix_from_ip_prefix(&evp, p);
4180 ret = update_evpn_type5_route(bgp_vrf, &evp, src_attr);
4181 if (ret)
4182 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
4183 "%u: Failed to create type-5 route for prefix %s",
4184 bgp_vrf->vrf_id, prefix2str(p, buf, sizeof(buf)));
4185 }
4186
4187 /* Inject all prefixes of a particular address-family (currently, IPv4 or
4188 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
4189 * advertisement is enabled.
4190 */
4191 void bgp_evpn_advertise_type5_routes(struct bgp *bgp_vrf, afi_t afi,
4192 safi_t safi)
4193 {
4194 struct bgp_table *table = NULL;
4195 struct bgp_dest *dest = NULL;
4196 struct bgp_path_info *pi;
4197
4198 table = bgp_vrf->rib[afi][safi];
4199 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
4200 /* Need to identify the "selected" route entry to use its
4201 * attribute. Also, ensure that the route is injectable
4202 * into EVPN.
4203 * TODO: Support for AddPath for EVPN.
4204 */
4205 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
4206 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
4207 && is_route_injectable_into_evpn(pi)) {
4208
4209 /* apply the route-map */
4210 if (bgp_vrf->adv_cmd_rmap[afi][safi].map) {
4211 route_map_result_t ret;
4212 struct bgp_path_info tmp_pi;
4213 struct bgp_path_info_extra tmp_pie;
4214 struct attr tmp_attr;
4215
4216 tmp_attr = *pi->attr;
4217
4218 /* Fill temp path_info */
4219 prep_for_rmap_apply(&tmp_pi, &tmp_pie,
4220 dest, pi, pi->peer,
4221 &tmp_attr);
4222
4223 RESET_FLAG(tmp_attr.rmap_change_flags);
4224
4225 ret = route_map_apply(
4226 bgp_vrf->adv_cmd_rmap[afi][safi]
4227 .map,
4228 bgp_dest_get_prefix(dest),
4229 RMAP_BGP, &tmp_pi);
4230 if (ret == RMAP_DENYMATCH) {
4231 bgp_attr_flush(&tmp_attr);
4232 continue;
4233 }
4234 bgp_evpn_advertise_type5_route(
4235 bgp_vrf,
4236 bgp_dest_get_prefix(dest),
4237 &tmp_attr, afi, safi);
4238 } else
4239 bgp_evpn_advertise_type5_route(
4240 bgp_vrf,
4241 bgp_dest_get_prefix(dest),
4242 pi->attr, afi, safi);
4243 break;
4244 }
4245 }
4246 }
4247 }
4248
4249 void evpn_rt_delete_auto(struct bgp *bgp, vni_t vni, struct list *rtl)
4250 {
4251 struct listnode *node, *nnode, *node_to_del;
4252 struct ecommunity *ecom, *ecom_auto;
4253 struct ecommunity_val eval;
4254
4255 if (bgp->advertise_autort_rfc8365)
4256 vni |= EVPN_AUTORT_VXLAN;
4257 encode_route_target_as((bgp->as & 0xFFFF), vni, &eval);
4258
4259 ecom_auto = ecommunity_new();
4260 ecommunity_add_val(ecom_auto, &eval, false, false);
4261 node_to_del = NULL;
4262
4263 for (ALL_LIST_ELEMENTS(rtl, node, nnode, ecom)) {
4264 if (ecommunity_match(ecom, ecom_auto)) {
4265 ecommunity_free(&ecom);
4266 node_to_del = node;
4267 }
4268 }
4269
4270 if (node_to_del)
4271 list_delete_node(rtl, node_to_del);
4272
4273 ecommunity_free(&ecom_auto);
4274 }
4275
4276 void bgp_evpn_configure_import_rt_for_vrf(struct bgp *bgp_vrf,
4277 struct ecommunity *ecomadd)
4278 {
4279 /* uninstall routes from vrf */
4280 if (is_l3vni_live(bgp_vrf))
4281 uninstall_routes_for_vrf(bgp_vrf);
4282
4283 /* Cleanup the RT to VRF mapping */
4284 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
4285
4286 /* Remove auto generated RT */
4287 evpn_auto_rt_import_delete_for_vrf(bgp_vrf);
4288
4289 /* Add the newly configured RT to RT list */
4290 listnode_add_sort(bgp_vrf->vrf_import_rtl, ecomadd);
4291 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
4292
4293 /* map VRF to its RTs and install routes matching the new RTs */
4294 if (is_l3vni_live(bgp_vrf)) {
4295 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
4296 install_routes_for_vrf(bgp_vrf);
4297 }
4298 }
4299
4300 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf,
4301 struct ecommunity *ecomdel)
4302 {
4303 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
4304 struct ecommunity *ecom = NULL;
4305
4306 /* uninstall routes from vrf */
4307 if (is_l3vni_live(bgp_vrf))
4308 uninstall_routes_for_vrf(bgp_vrf);
4309
4310 /* Cleanup the RT to VRF mapping */
4311 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
4312
4313 /* remove the RT from the RT list */
4314 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
4315 if (ecommunity_match(ecom, ecomdel)) {
4316 ecommunity_free(&ecom);
4317 node_to_del = node;
4318 break;
4319 }
4320 }
4321
4322 if (node_to_del)
4323 list_delete_node(bgp_vrf->vrf_import_rtl, node_to_del);
4324
4325 assert(bgp_vrf->vrf_import_rtl);
4326 /* fallback to auto import rt, if this was the last RT */
4327 if (bgp_vrf->vrf_import_rtl && list_isempty(bgp_vrf->vrf_import_rtl)) {
4328 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
4329 if (is_l3vni_live(bgp_vrf))
4330 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
4331 }
4332
4333 /* map VRFs to its RTs and install routes matching this new RT */
4334 if (is_l3vni_live(bgp_vrf)) {
4335 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
4336 install_routes_for_vrf(bgp_vrf);
4337 }
4338 }
4339
4340 void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf,
4341 struct ecommunity *ecomadd)
4342 {
4343 /* remove auto-generated RT */
4344 evpn_auto_rt_export_delete_for_vrf(bgp_vrf);
4345
4346 /* Add the new RT to the RT list */
4347 listnode_add_sort(bgp_vrf->vrf_export_rtl, ecomadd);
4348 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
4349
4350 if (is_l3vni_live(bgp_vrf))
4351 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
4352 }
4353
4354 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf,
4355 struct ecommunity *ecomdel)
4356 {
4357 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
4358 struct ecommunity *ecom = NULL;
4359
4360 /* Remove the RT from the RT list */
4361 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_export_rtl, node, nnode, ecom)) {
4362 if (ecommunity_match(ecom, ecomdel)) {
4363 ecommunity_free(&ecom);
4364 node_to_del = node;
4365 break;
4366 }
4367 }
4368
4369 if (node_to_del)
4370 list_delete_node(bgp_vrf->vrf_export_rtl, node_to_del);
4371
4372 /*
4373 * Temporary assert to make SA happy.
4374 * The ALL_LIST_ELEMENTS macro above has a NULL check
4375 * which means that SA is going to complain about
4376 * the list_isempty call, which doesn't NULL check.
4377 * So until we get this situation cleaned up, here
4378 * we are.
4379 */
4380 assert(bgp_vrf->vrf_export_rtl);
4381
4382 /* fall back to auto-generated RT if this was the last RT */
4383 if (list_isempty(bgp_vrf->vrf_export_rtl)) {
4384 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
4385 if (is_l3vni_live(bgp_vrf))
4386 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
4387 }
4388
4389 if (is_l3vni_live(bgp_vrf))
4390 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
4391 }
4392
4393 /*
4394 * Handle change to BGP router id. This is invoked twice by the change
4395 * handler, first before the router id has been changed and then after
4396 * the router id has been changed. The first invocation will result in
4397 * local routes for all VNIs/VRF being deleted and withdrawn and the next
4398 * will result in the routes being re-advertised.
4399 */
4400 void bgp_evpn_handle_router_id_update(struct bgp *bgp, int withdraw)
4401 {
4402 struct listnode *node;
4403 struct bgp *bgp_vrf;
4404
4405 if (withdraw) {
4406
4407 /* delete and withdraw all the type-5 routes
4408 stored in the global table for this vrf
4409 */
4410 withdraw_router_id_vrf(bgp);
4411
4412 /* delete all the VNI routes (type-2/type-3) routes for all the
4413 * L2-VNIs
4414 */
4415 hash_iterate(bgp->vnihash,
4416 (void (*)(struct hash_bucket *,
4417 void *))withdraw_router_id_vni,
4418 bgp);
4419
4420 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
4421 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
4422 if (bgp_vrf->evpn_info->advertise_pip &&
4423 (bgp_vrf->evpn_info->pip_ip_static.s_addr
4424 == INADDR_ANY))
4425 bgp_vrf->evpn_info->pip_ip.s_addr
4426 = INADDR_ANY;
4427 }
4428 }
4429 } else {
4430
4431 /* Assign new default instance router-id */
4432 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
4433 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
4434 if (bgp_vrf->evpn_info->advertise_pip &&
4435 (bgp_vrf->evpn_info->pip_ip_static.s_addr
4436 == INADDR_ANY)) {
4437 bgp_vrf->evpn_info->pip_ip =
4438 bgp->router_id;
4439 /* advertise type-5 routes with
4440 * new nexthop
4441 */
4442 update_advertise_vrf_routes(bgp_vrf);
4443 }
4444 }
4445 }
4446
4447 /* advertise all routes in the vrf as type-5 routes with the new
4448 * RD
4449 */
4450 update_router_id_vrf(bgp);
4451
4452 /* advertise all the VNI routes (type-2/type-3) routes with the
4453 * new RD
4454 */
4455 hash_iterate(bgp->vnihash,
4456 (void (*)(struct hash_bucket *,
4457 void *))update_router_id_vni,
4458 bgp);
4459 }
4460 }
4461
4462 /*
4463 * Handle change to auto-RT algorithm - update and advertise local routes.
4464 */
4465 void bgp_evpn_handle_autort_change(struct bgp *bgp)
4466 {
4467 hash_iterate(bgp->vnihash,
4468 (void (*)(struct hash_bucket *,
4469 void*))update_autort_vni,
4470 bgp);
4471 }
4472
4473 /*
4474 * Handle change to export RT - update and advertise local routes.
4475 */
4476 int bgp_evpn_handle_export_rt_change(struct bgp *bgp, struct bgpevpn *vpn)
4477 {
4478 return update_routes_for_vni(bgp, vpn);
4479 }
4480
4481 void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw)
4482 {
4483 if (withdraw)
4484 delete_withdraw_vrf_routes(bgp_vrf);
4485 else
4486 update_advertise_vrf_routes(bgp_vrf);
4487 }
4488
4489 /*
4490 * Handle change to RD. This is invoked twice by the change handler,
4491 * first before the RD has been changed and then after the RD has
4492 * been changed. The first invocation will result in local routes
4493 * of this VNI being deleted and withdrawn and the next will result
4494 * in the routes being re-advertised.
4495 */
4496 void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
4497 int withdraw)
4498 {
4499 if (withdraw)
4500 delete_withdraw_vni_routes(bgp, vpn);
4501 else
4502 update_advertise_vni_routes(bgp, vpn);
4503 }
4504
4505 /*
4506 * Install routes for this VNI. Invoked upon change to Import RT.
4507 */
4508 int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn)
4509 {
4510 return install_routes_for_vni(bgp, vpn);
4511 }
4512
4513 /*
4514 * Uninstall all routes installed for this VNI. Invoked upon change
4515 * to Import RT.
4516 */
4517 int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn)
4518 {
4519 return uninstall_routes_for_vni(bgp, vpn);
4520 }
4521
4522 /*
4523 * TODO: Hardcoded for a maximum of 2 VNIs right now
4524 */
4525 char *bgp_evpn_label2str(mpls_label_t *label, uint32_t num_labels, char *buf,
4526 int len)
4527 {
4528 vni_t vni1, vni2;
4529
4530 vni1 = label2vni(label);
4531 if (num_labels == 2) {
4532 vni2 = label2vni(label + 1);
4533 snprintf(buf, len, "%u/%u", vni1, vni2);
4534 } else
4535 snprintf(buf, len, "%u", vni1);
4536 return buf;
4537 }
4538
4539 /*
4540 * Function to convert evpn route to json format.
4541 * NOTE: We don't use prefix2str as the output here is a bit different.
4542 */
4543 void bgp_evpn_route2json(const struct prefix_evpn *p, json_object *json)
4544 {
4545 char buf1[ETHER_ADDR_STRLEN];
4546 char buf2[PREFIX2STR_BUFFER];
4547 uint8_t family;
4548 uint8_t prefixlen;
4549
4550 if (!json)
4551 return;
4552
4553 json_object_int_add(json, "routeType", p->prefix.route_type);
4554
4555 switch (p->prefix.route_type) {
4556 case BGP_EVPN_MAC_IP_ROUTE:
4557 json_object_int_add(json, "ethTag",
4558 p->prefix.macip_addr.eth_tag);
4559 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
4560 json_object_string_add(json, "mac",
4561 prefix_mac2str(&p->prefix.macip_addr.mac, buf1,
4562 sizeof(buf1)));
4563
4564 if (!is_evpn_prefix_ipaddr_none(p)) {
4565 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET :
4566 AF_INET6;
4567 prefixlen = (family == AF_INET) ?
4568 IPV4_MAX_BITLEN : IPV6_MAX_BITLEN;
4569 inet_ntop(family, &p->prefix.macip_addr.ip.ip.addr,
4570 buf2, PREFIX2STR_BUFFER);
4571 json_object_int_add(json, "ipLen", prefixlen);
4572 json_object_string_add(json, "ip", buf2);
4573 }
4574 break;
4575
4576 case BGP_EVPN_IMET_ROUTE:
4577 json_object_int_add(json, "ethTag",
4578 p->prefix.imet_addr.eth_tag);
4579 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET : AF_INET6;
4580 prefixlen = (family == AF_INET) ? IPV4_MAX_BITLEN :
4581 IPV6_MAX_BITLEN;
4582 inet_ntop(family, &p->prefix.imet_addr.ip.ip.addr, buf2,
4583 PREFIX2STR_BUFFER);
4584 json_object_int_add(json, "ipLen", prefixlen);
4585 json_object_string_add(json, "ip", buf2);
4586 break;
4587
4588 case BGP_EVPN_IP_PREFIX_ROUTE:
4589 json_object_int_add(json, "ethTag",
4590 p->prefix.prefix_addr.eth_tag);
4591 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET : AF_INET6;
4592 inet_ntop(family, &p->prefix.prefix_addr.ip.ip.addr,
4593 buf2, sizeof(buf2));
4594 json_object_int_add(json, "ipLen",
4595 p->prefix.prefix_addr.ip_prefix_length);
4596 json_object_string_add(json, "ip", buf2);
4597 break;
4598
4599 default:
4600 break;
4601 }
4602 }
4603
4604 /*
4605 * Encode EVPN prefix in Update (MP_REACH)
4606 */
4607 void bgp_evpn_encode_prefix(struct stream *s, const struct prefix *p,
4608 const struct prefix_rd *prd, mpls_label_t *label,
4609 uint32_t num_labels, struct attr *attr,
4610 int addpath_encode, uint32_t addpath_tx_id)
4611 {
4612 struct prefix_evpn *evp = (struct prefix_evpn *)p;
4613 int len, ipa_len = 0;
4614
4615 if (addpath_encode)
4616 stream_putl(s, addpath_tx_id);
4617
4618 /* Route type */
4619 stream_putc(s, evp->prefix.route_type);
4620
4621 switch (evp->prefix.route_type) {
4622 case BGP_EVPN_MAC_IP_ROUTE:
4623 if (is_evpn_prefix_ipaddr_v4(evp))
4624 ipa_len = IPV4_MAX_BYTELEN;
4625 else if (is_evpn_prefix_ipaddr_v6(evp))
4626 ipa_len = IPV6_MAX_BYTELEN;
4627 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
4628 len = 8 + 10 + 4 + 1 + 6 + 1 + ipa_len + 3;
4629 if (ipa_len && num_labels > 1) /* There are 2 VNIs */
4630 len += 3;
4631 stream_putc(s, len);
4632 stream_put(s, prd->val, 8); /* RD */
4633 if (attr)
4634 stream_put(s, &attr->esi, ESI_BYTES);
4635 else
4636 stream_put(s, 0, 10);
4637 stream_putl(s, evp->prefix.macip_addr.eth_tag); /* Ethernet Tag ID */
4638 stream_putc(s, 8 * ETH_ALEN); /* Mac Addr Len - bits */
4639 stream_put(s, evp->prefix.macip_addr.mac.octet, 6); /* Mac Addr */
4640 stream_putc(s, 8 * ipa_len); /* IP address Length */
4641 if (ipa_len) /* IP */
4642 stream_put(s, &evp->prefix.macip_addr.ip.ip.addr,
4643 ipa_len);
4644 /* 1st label is the L2 VNI */
4645 stream_put(s, label, BGP_LABEL_BYTES);
4646 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
4647 if (ipa_len && num_labels > 1)
4648 stream_put(s, label + 1, BGP_LABEL_BYTES);
4649 break;
4650
4651 case BGP_EVPN_IMET_ROUTE:
4652 stream_putc(s, 17); // TODO: length - assumes IPv4 address
4653 stream_put(s, prd->val, 8); /* RD */
4654 stream_putl(s, evp->prefix.imet_addr.eth_tag); /* Ethernet Tag ID */
4655 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
4656 /* Originating Router's IP Addr */
4657 stream_put_in_addr(s, &evp->prefix.imet_addr.ip.ipaddr_v4);
4658 break;
4659
4660 case BGP_EVPN_ES_ROUTE:
4661 stream_putc(s, 23); /* TODO: length: assumes ipv4 VTEP */
4662 stream_put(s, prd->val, 8); /* RD */
4663 stream_put(s, evp->prefix.es_addr.esi.val, 10); /* ESI */
4664 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
4665 /* VTEP IP */
4666 stream_put_in_addr(s, &evp->prefix.es_addr.ip.ipaddr_v4);
4667 break;
4668
4669 case BGP_EVPN_AD_ROUTE:
4670 /* RD, ESI, EthTag, 1 VNI */
4671 len = RD_BYTES + ESI_BYTES + EVPN_ETH_TAG_BYTES + BGP_LABEL_BYTES;
4672 stream_putc(s, len);
4673 stream_put(s, prd->val, RD_BYTES); /* RD */
4674 stream_put(s, evp->prefix.ead_addr.esi.val, ESI_BYTES); /* ESI */
4675 stream_putl(s, evp->prefix.ead_addr.eth_tag); /* Ethernet Tag */
4676 stream_put(s, label, BGP_LABEL_BYTES);
4677 break;
4678
4679 case BGP_EVPN_IP_PREFIX_ROUTE:
4680 /* TODO: AddPath support. */
4681 evpn_mpattr_encode_type5(s, p, prd, label, num_labels, attr);
4682 break;
4683
4684 default:
4685 break;
4686 }
4687 }
4688
4689 int bgp_nlri_parse_evpn(struct peer *peer, struct attr *attr,
4690 struct bgp_nlri *packet, int withdraw)
4691 {
4692 uint8_t *pnt;
4693 uint8_t *lim;
4694 afi_t afi;
4695 safi_t safi;
4696 uint32_t addpath_id;
4697 int addpath_encoded;
4698 int psize = 0;
4699 uint8_t rtype;
4700 struct prefix p;
4701
4702 /* Start processing the NLRI - there may be multiple in the MP_REACH */
4703 pnt = packet->nlri;
4704 lim = pnt + packet->length;
4705 afi = packet->afi;
4706 safi = packet->safi;
4707 addpath_id = 0;
4708
4709 addpath_encoded =
4710 (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV)
4711 && CHECK_FLAG(peer->af_cap[afi][safi],
4712 PEER_CAP_ADDPATH_AF_TX_RCV));
4713
4714 for (; pnt < lim; pnt += psize) {
4715 /* Clear prefix structure. */
4716 memset(&p, 0, sizeof(struct prefix));
4717
4718 /* Deal with path-id if AddPath is supported. */
4719 if (addpath_encoded) {
4720 /* When packet overflow occurs return immediately. */
4721 if (pnt + BGP_ADDPATH_ID_LEN > lim)
4722 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
4723
4724 memcpy(&addpath_id, pnt, BGP_ADDPATH_ID_LEN);
4725 addpath_id = ntohl(addpath_id);
4726 pnt += BGP_ADDPATH_ID_LEN;
4727 }
4728
4729 /* All EVPN NLRI types start with type and length. */
4730 if (pnt + 2 > lim)
4731 return BGP_NLRI_PARSE_ERROR_EVPN_MISSING_TYPE;
4732
4733 rtype = *pnt++;
4734 psize = *pnt++;
4735
4736 /* When packet overflow occur return immediately. */
4737 if (pnt + psize > lim)
4738 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
4739
4740 switch (rtype) {
4741 case BGP_EVPN_MAC_IP_ROUTE:
4742 if (process_type2_route(peer, afi, safi,
4743 withdraw ? NULL : attr, pnt,
4744 psize, addpath_id)) {
4745 flog_err(
4746 EC_BGP_EVPN_FAIL,
4747 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
4748 peer->bgp->vrf_id, peer->host, psize);
4749 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE2_SIZE;
4750 }
4751 break;
4752
4753 case BGP_EVPN_IMET_ROUTE:
4754 if (process_type3_route(peer, afi, safi,
4755 withdraw ? NULL : attr, pnt,
4756 psize, addpath_id)) {
4757 flog_err(
4758 EC_BGP_PKT_PROCESS,
4759 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
4760 peer->bgp->vrf_id, peer->host, psize);
4761 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE3_SIZE;
4762 }
4763 break;
4764
4765 case BGP_EVPN_ES_ROUTE:
4766 if (bgp_evpn_type4_route_process(peer, afi, safi,
4767 withdraw ? NULL : attr, pnt,
4768 psize, addpath_id)) {
4769 flog_err(
4770 EC_BGP_PKT_PROCESS,
4771 "%u:%s - Error in processing EVPN type-4 NLRI size %d",
4772 peer->bgp->vrf_id, peer->host, psize);
4773 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE4_SIZE;
4774 }
4775 break;
4776
4777 case BGP_EVPN_AD_ROUTE:
4778 if (bgp_evpn_type1_route_process(peer, afi, safi,
4779 withdraw ? NULL : attr, pnt,
4780 psize, addpath_id)) {
4781 flog_err(
4782 EC_BGP_PKT_PROCESS,
4783 "%u:%s - Error in processing EVPN type-1 NLRI size %d",
4784 peer->bgp->vrf_id, peer->host, psize);
4785 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE1_SIZE;
4786 }
4787 break;
4788
4789 case BGP_EVPN_IP_PREFIX_ROUTE:
4790 if (process_type5_route(peer, afi, safi,
4791 withdraw ? NULL : attr, pnt,
4792 psize, addpath_id)) {
4793 flog_err(
4794 EC_BGP_PKT_PROCESS,
4795 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
4796 peer->bgp->vrf_id, peer->host, psize);
4797 return BGP_NLRI_PARSE_ERROR_EVPN_TYPE5_SIZE;
4798 }
4799 break;
4800
4801 default:
4802 break;
4803 }
4804 }
4805
4806 /* Packet length consistency check. */
4807 if (pnt != lim)
4808 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH;
4809
4810 return BGP_NLRI_PARSE_OK;
4811 }
4812
4813 /*
4814 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
4815 * The mapping will be used during route processing.
4816 * bgp_def: default bgp instance
4817 * bgp_vrf: specific bgp vrf instance on which RT is configured
4818 */
4819 void bgp_evpn_map_vrf_to_its_rts(struct bgp *bgp_vrf)
4820 {
4821 int i = 0;
4822 struct ecommunity_val *eval = NULL;
4823 struct listnode *node = NULL, *nnode = NULL;
4824 struct ecommunity *ecom = NULL;
4825
4826 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
4827 for (i = 0; i < ecom->size; i++) {
4828 eval = (struct ecommunity_val *)(ecom->val
4829 + (i
4830 * ECOMMUNITY_SIZE));
4831 map_vrf_to_rt(bgp_vrf, eval);
4832 }
4833 }
4834 }
4835
4836 /*
4837 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
4838 */
4839 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *bgp_vrf)
4840 {
4841 int i;
4842 struct ecommunity_val *eval;
4843 struct listnode *node, *nnode;
4844 struct ecommunity *ecom;
4845
4846 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
4847 for (i = 0; i < ecom->size; i++) {
4848 struct vrf_irt_node *irt;
4849 struct ecommunity_val eval_tmp;
4850
4851 eval = (struct ecommunity_val *)(ecom->val
4852 + (i
4853 * ECOMMUNITY_SIZE));
4854 /* If using "automatic" RT, we only care about the
4855 * local-admin sub-field.
4856 * This is to facilitate using VNI as the RT for EBGP
4857 * peering too.
4858 */
4859 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
4860 if (!CHECK_FLAG(bgp_vrf->vrf_flags,
4861 BGP_VRF_IMPORT_RT_CFGD))
4862 mask_ecom_global_admin(&eval_tmp, eval);
4863
4864 irt = lookup_vrf_import_rt(&eval_tmp);
4865 if (irt)
4866 unmap_vrf_from_rt(bgp_vrf, irt);
4867 }
4868 }
4869 }
4870
4871
4872 /*
4873 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
4874 * The mapping will be used during route processing.
4875 */
4876 void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
4877 {
4878 int i;
4879 struct ecommunity_val *eval;
4880 struct listnode *node, *nnode;
4881 struct ecommunity *ecom;
4882
4883 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
4884 for (i = 0; i < ecom->size; i++) {
4885 eval = (struct ecommunity_val *)(ecom->val
4886 + (i
4887 * ECOMMUNITY_SIZE));
4888 map_vni_to_rt(bgp, vpn, eval);
4889 }
4890 }
4891 }
4892
4893 /*
4894 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
4895 */
4896 void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
4897 {
4898 int i;
4899 struct ecommunity_val *eval;
4900 struct listnode *node, *nnode;
4901 struct ecommunity *ecom;
4902
4903 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
4904 for (i = 0; i < ecom->size; i++) {
4905 struct irt_node *irt;
4906 struct ecommunity_val eval_tmp;
4907
4908 eval = (struct ecommunity_val *)(ecom->val
4909 + (i
4910 * ECOMMUNITY_SIZE));
4911 /* If using "automatic" RT, we only care about the
4912 * local-admin sub-field.
4913 * This is to facilitate using VNI as the RT for EBGP
4914 * peering too.
4915 */
4916 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
4917 if (!is_import_rt_configured(vpn))
4918 mask_ecom_global_admin(&eval_tmp, eval);
4919
4920 irt = lookup_import_rt(bgp, &eval_tmp);
4921 if (irt)
4922 unmap_vni_from_rt(bgp, vpn, irt);
4923 }
4924 }
4925 }
4926
4927 /*
4928 * Derive Import RT automatically for VNI and map VNI to RT.
4929 * The mapping will be used during route processing.
4930 */
4931 void bgp_evpn_derive_auto_rt_import(struct bgp *bgp, struct bgpevpn *vpn)
4932 {
4933 form_auto_rt(bgp, vpn->vni, vpn->import_rtl);
4934 UNSET_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD);
4935
4936 /* Map RT to VNI */
4937 bgp_evpn_map_vni_to_its_rts(bgp, vpn);
4938 }
4939
4940 /*
4941 * Derive Export RT automatically for VNI.
4942 */
4943 void bgp_evpn_derive_auto_rt_export(struct bgp *bgp, struct bgpevpn *vpn)
4944 {
4945 form_auto_rt(bgp, vpn->vni, vpn->export_rtl);
4946 UNSET_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD);
4947 }
4948
4949 /*
4950 * Derive RD automatically for VNI using passed information - it
4951 * is of the form RouterId:unique-id-for-vni.
4952 */
4953 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp)
4954 {
4955 if (is_vrf_rd_configured(bgp))
4956 return;
4957
4958 form_auto_rd(bgp->router_id, bgp->vrf_rd_id, &bgp->vrf_prd);
4959 }
4960
4961 /*
4962 * Derive RD automatically for VNI using passed information - it
4963 * is of the form RouterId:unique-id-for-vni.
4964 */
4965 void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn)
4966 {
4967 char buf[BGP_EVPN_PREFIX_RD_LEN];
4968
4969 vpn->prd.family = AF_UNSPEC;
4970 vpn->prd.prefixlen = 64;
4971 snprintfrr(buf, sizeof(buf), "%pI4:%hu", &bgp->router_id, vpn->rd_id);
4972 (void)str2prefix_rd(buf, &vpn->prd);
4973 UNSET_FLAG(vpn->flags, VNI_FLAG_RD_CFGD);
4974 }
4975
4976 /*
4977 * Lookup L3-VNI
4978 */
4979 bool bgp_evpn_lookup_l3vni_l2vni_table(vni_t vni)
4980 {
4981 struct list *inst = bm->bgp;
4982 struct listnode *node;
4983 struct bgp *bgp_vrf;
4984
4985 for (ALL_LIST_ELEMENTS_RO(inst, node, bgp_vrf)) {
4986 if (bgp_vrf->l3vni == vni)
4987 return true;
4988 }
4989
4990 return false;
4991 }
4992
4993 /*
4994 * Lookup VNI.
4995 */
4996 struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni)
4997 {
4998 struct bgpevpn *vpn;
4999 struct bgpevpn tmp;
5000
5001 memset(&tmp, 0, sizeof(struct bgpevpn));
5002 tmp.vni = vni;
5003 vpn = hash_lookup(bgp->vnihash, &tmp);
5004 return vpn;
5005 }
5006
5007 /*
5008 * Create a new vpn - invoked upon configuration or zebra notification.
5009 */
5010 struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
5011 struct in_addr originator_ip,
5012 vrf_id_t tenant_vrf_id,
5013 struct in_addr mcast_grp)
5014 {
5015 struct bgpevpn *vpn;
5016
5017 if (!bgp)
5018 return NULL;
5019
5020 vpn = XCALLOC(MTYPE_BGP_EVPN, sizeof(struct bgpevpn));
5021
5022 /* Set values - RD and RT set to defaults. */
5023 vpn->vni = vni;
5024 vpn->originator_ip = originator_ip;
5025 vpn->tenant_vrf_id = tenant_vrf_id;
5026 vpn->mcast_grp = mcast_grp;
5027
5028 /* Initialize route-target import and export lists */
5029 vpn->import_rtl = list_new();
5030 vpn->import_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
5031 vpn->import_rtl->del = evpn_xxport_delete_ecomm;
5032 vpn->export_rtl = list_new();
5033 vpn->export_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
5034 vpn->export_rtl->del = evpn_xxport_delete_ecomm;
5035 bf_assign_index(bm->rd_idspace, vpn->rd_id);
5036 derive_rd_rt_for_vni(bgp, vpn);
5037
5038 /* Initialize EVPN route table. */
5039 vpn->route_table = bgp_table_init(bgp, AFI_L2VPN, SAFI_EVPN);
5040
5041 /* Add to hash */
5042 if (!hash_get(bgp->vnihash, vpn, hash_alloc_intern)) {
5043 XFREE(MTYPE_BGP_EVPN, vpn);
5044 return NULL;
5045 }
5046
5047 /* add to l2vni list on corresponding vrf */
5048 bgpevpn_link_to_l3vni(vpn);
5049
5050 bgp_evpn_vni_es_init(vpn);
5051
5052 QOBJ_REG(vpn, bgpevpn);
5053 return vpn;
5054 }
5055
5056 /*
5057 * Free a given VPN - called in multiple scenarios such as zebra
5058 * notification, configuration being deleted, advertise-all-vni disabled etc.
5059 * This just frees appropriate memory, caller should have taken other
5060 * needed actions.
5061 */
5062 void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn)
5063 {
5064 bgp_evpn_vni_es_cleanup(vpn);
5065 bgpevpn_unlink_from_l3vni(vpn);
5066 bgp_table_unlock(vpn->route_table);
5067 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
5068 list_delete(&vpn->import_rtl);
5069 list_delete(&vpn->export_rtl);
5070 bf_release_index(bm->rd_idspace, vpn->rd_id);
5071 hash_release(bgp->vnihash, vpn);
5072 QOBJ_UNREG(vpn);
5073 XFREE(MTYPE_BGP_EVPN, vpn);
5074 }
5075
5076 /*
5077 * Import evpn route from global table to VNI/VRF/ESI.
5078 */
5079 int bgp_evpn_import_route(struct bgp *bgp, afi_t afi, safi_t safi,
5080 const struct prefix *p, struct bgp_path_info *pi)
5081 {
5082 return install_uninstall_evpn_route(bgp, afi, safi, p, pi, 1);
5083 }
5084
5085 /*
5086 * Unimport evpn route from VNI/VRF/ESI.
5087 */
5088 int bgp_evpn_unimport_route(struct bgp *bgp, afi_t afi, safi_t safi,
5089 const struct prefix *p, struct bgp_path_info *pi)
5090 {
5091 return install_uninstall_evpn_route(bgp, afi, safi, p, pi, 0);
5092 }
5093
5094 /* filter routes which have martian next hops */
5095 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp *bgp)
5096 {
5097 afi_t afi;
5098 safi_t safi;
5099 struct bgp_dest *rd_dest, *dest;
5100 struct bgp_table *table;
5101 struct bgp_path_info *pi;
5102
5103 afi = AFI_L2VPN;
5104 safi = SAFI_EVPN;
5105
5106 /* Walk entire global routing table and evaluate routes which could be
5107 * imported into this VPN. Note that we cannot just look at the routes
5108 * for the VNI's RD -
5109 * remote routes applicable for this VNI could have any RD.
5110 */
5111 /* EVPN routes are a 2-level table. */
5112 for (rd_dest = bgp_table_top(bgp->rib[afi][safi]); rd_dest;
5113 rd_dest = bgp_route_next(rd_dest)) {
5114 table = bgp_dest_get_bgp_table_info(rd_dest);
5115 if (!table)
5116 continue;
5117
5118 for (dest = bgp_table_top(table); dest;
5119 dest = bgp_route_next(dest)) {
5120
5121 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
5122 pi = pi->next) {
5123
5124 /* Consider "valid" remote routes applicable for
5125 * this VNI. */
5126 if (!(pi->type == ZEBRA_ROUTE_BGP
5127 && pi->sub_type == BGP_ROUTE_NORMAL))
5128 continue;
5129 if (bgp_nexthop_self(bgp, afi, pi->type,
5130 pi->sub_type, pi->attr,
5131 dest)) {
5132 const struct prefix *p =
5133 bgp_dest_get_prefix(dest);
5134
5135 if (bgp_debug_update(pi->peer, p, NULL,
5136 1)) {
5137 char attr_str[BUFSIZ] = {0};
5138
5139 bgp_dump_attr(pi->attr,
5140 attr_str,
5141 sizeof(attr_str));
5142
5143 zlog_debug(
5144 "%u: prefix %pBD with attr %s - DENIED due to martian or self nexthop",
5145 bgp->vrf_id, dest,
5146 attr_str);
5147 }
5148 bgp_evpn_unimport_route(bgp, afi, safi,
5149 p, pi);
5150
5151 bgp_rib_remove(dest, pi, pi->peer, afi,
5152 safi);
5153 }
5154 }
5155 }
5156 }
5157
5158 return 0;
5159 }
5160
5161 /*
5162 * Handle del of a local MACIP.
5163 */
5164 int bgp_evpn_local_macip_del(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
5165 struct ipaddr *ip, int state)
5166 {
5167 struct bgpevpn *vpn;
5168 struct prefix_evpn p;
5169 struct bgp_dest *dest;
5170
5171 /* Lookup VNI hash - should exist. */
5172 vpn = bgp_evpn_lookup_vni(bgp, vni);
5173 if (!vpn || !is_vni_live(vpn)) {
5174 flog_warn(EC_BGP_EVPN_VPN_VNI,
5175 "%u: VNI hash entry for VNI %u %s at MACIP DEL",
5176 bgp->vrf_id, vni, vpn ? "not live" : "not found");
5177 return -1;
5178 }
5179
5180 build_evpn_type2_prefix(&p, mac, ip);
5181 if (state == ZEBRA_NEIGH_ACTIVE) {
5182 /* Remove EVPN type-2 route and schedule for processing. */
5183 delete_evpn_route(bgp, vpn, &p);
5184 } else {
5185 /* Re-instate the current remote best path if any */
5186 dest = bgp_node_lookup(vpn->route_table, (struct prefix *)&p);
5187 if (dest)
5188 evpn_zebra_reinstall_best_route(bgp, vpn, dest);
5189 }
5190
5191 return 0;
5192 }
5193
5194 /*
5195 * Handle add of a local MACIP.
5196 */
5197 int bgp_evpn_local_macip_add(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
5198 struct ipaddr *ip, uint8_t flags, uint32_t seq, esi_t *esi)
5199 {
5200 struct bgpevpn *vpn;
5201 struct prefix_evpn p;
5202
5203 /* Lookup VNI hash - should exist. */
5204 vpn = bgp_evpn_lookup_vni(bgp, vni);
5205 if (!vpn || !is_vni_live(vpn)) {
5206 flog_warn(EC_BGP_EVPN_VPN_VNI,
5207 "%u: VNI hash entry for VNI %u %s at MACIP ADD",
5208 bgp->vrf_id, vni, vpn ? "not live" : "not found");
5209 return -1;
5210 }
5211
5212 /* Create EVPN type-2 route and schedule for processing. */
5213 build_evpn_type2_prefix(&p, mac, ip);
5214 if (update_evpn_route(bgp, vpn, &p, flags, seq, esi)) {
5215 char buf[ETHER_ADDR_STRLEN];
5216 char buf2[INET6_ADDRSTRLEN];
5217
5218 flog_err(
5219 EC_BGP_EVPN_ROUTE_CREATE,
5220 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
5221 bgp->vrf_id, vpn->vni,
5222 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY)
5223 ? "sticky gateway"
5224 : "",
5225 prefix_mac2str(mac, buf, sizeof(buf)),
5226 ipaddr2str(ip, buf2, sizeof(buf2)), flags);
5227 return -1;
5228 }
5229
5230 return 0;
5231 }
5232
5233 static void link_l2vni_hash_to_l3vni(struct hash_bucket *bucket,
5234 struct bgp *bgp_vrf)
5235 {
5236 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
5237 struct bgp *bgp_evpn = NULL;
5238
5239 bgp_evpn = bgp_get_evpn();
5240 assert(bgp_evpn);
5241
5242 if (vpn->tenant_vrf_id == bgp_vrf->vrf_id)
5243 bgpevpn_link_to_l3vni(vpn);
5244 }
5245
5246 int bgp_evpn_local_l3vni_add(vni_t l3vni, vrf_id_t vrf_id,
5247 struct ethaddr *svi_rmac,
5248 struct ethaddr *vrr_rmac,
5249 struct in_addr originator_ip, int filter,
5250 ifindex_t svi_ifindex,
5251 bool is_anycast_mac)
5252 {
5253 struct bgp *bgp_vrf = NULL; /* bgp VRF instance */
5254 struct bgp *bgp_evpn = NULL; /* EVPN bgp instance */
5255 struct listnode *node = NULL;
5256 struct bgpevpn *vpn = NULL;
5257 as_t as = 0;
5258
5259 /* get the EVPN instance - required to get the AS number for VRF
5260 * auto-creatio
5261 */
5262 bgp_evpn = bgp_get_evpn();
5263 if (!bgp_evpn) {
5264 flog_err(
5265 EC_BGP_NO_DFLT,
5266 "Cannot process L3VNI %u ADD - EVPN BGP instance not yet created",
5267 l3vni);
5268 return -1;
5269 }
5270 as = bgp_evpn->as;
5271
5272 /* if the BGP vrf instance doesn't exist - create one */
5273 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
5274 if (!bgp_vrf) {
5275
5276 int ret = 0;
5277
5278 ret = bgp_get_vty(&bgp_vrf, &as, vrf_id_to_name(vrf_id),
5279 vrf_id == VRF_DEFAULT
5280 ? BGP_INSTANCE_TYPE_DEFAULT
5281 : BGP_INSTANCE_TYPE_VRF);
5282 switch (ret) {
5283 case BGP_ERR_AS_MISMATCH:
5284 flog_err(EC_BGP_EVPN_AS_MISMATCH,
5285 "BGP is already running; AS is %u\n", as);
5286 return -1;
5287 case BGP_ERR_INSTANCE_MISMATCH:
5288 flog_err(EC_BGP_EVPN_INSTANCE_MISMATCH,
5289 "BGP instance name and AS number mismatch\n");
5290 return -1;
5291 }
5292
5293 /* mark as auto created */
5294 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO);
5295 }
5296
5297 /* associate the vrf with l3vni and related parameters */
5298 bgp_vrf->l3vni = l3vni;
5299 bgp_vrf->originator_ip = originator_ip;
5300 bgp_vrf->l3vni_svi_ifindex = svi_ifindex;
5301 bgp_vrf->evpn_info->is_anycast_mac = is_anycast_mac;
5302
5303 /* copy anycast MAC from VRR MAC */
5304 memcpy(&bgp_vrf->rmac, vrr_rmac, ETH_ALEN);
5305 /* copy sys RMAC from SVI MAC */
5306 memcpy(&bgp_vrf->evpn_info->pip_rmac_zebra, svi_rmac, ETH_ALEN);
5307 /* PIP user configured mac is not present use svi mac as sys mac */
5308 if (is_zero_mac(&bgp_vrf->evpn_info->pip_rmac_static))
5309 memcpy(&bgp_vrf->evpn_info->pip_rmac, svi_rmac, ETH_ALEN);
5310
5311 if (bgp_debug_zebra(NULL)) {
5312 char buf[ETHER_ADDR_STRLEN];
5313 char buf1[ETHER_ADDR_STRLEN];
5314 char buf2[ETHER_ADDR_STRLEN];
5315
5316 zlog_debug("VRF %s vni %u pip %s RMAC %s sys RMAC %s static RMAC %s is_anycast_mac %s",
5317 vrf_id_to_name(bgp_vrf->vrf_id),
5318 bgp_vrf->l3vni,
5319 bgp_vrf->evpn_info->advertise_pip ? "enable"
5320 : "disable",
5321 prefix_mac2str(&bgp_vrf->rmac, buf, sizeof(buf)),
5322 prefix_mac2str(&bgp_vrf->evpn_info->pip_rmac,
5323 buf1, sizeof(buf1)),
5324 prefix_mac2str(&bgp_vrf->evpn_info->pip_rmac_static,
5325 buf2, sizeof(buf2)),
5326 is_anycast_mac ? "Enable" : "Disable");
5327 }
5328 /* set the right filter - are we using l3vni only for prefix routes? */
5329 if (filter) {
5330 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
5331
5332 /*
5333 * VNI_FLAG_USE_TWO_LABELS flag for linked L2VNIs should not be
5334 * set before linking vrf to L3VNI. Thus, no need to clear
5335 * that explicitly.
5336 */
5337 } else {
5338 UNSET_FLAG(bgp_vrf->vrf_flags,
5339 BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
5340
5341 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) {
5342 if (!CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)) {
5343
5344 /*
5345 * If we are flapping VNI_FLAG_USE_TWO_LABELS
5346 * flag, update all MACIP routes in this VNI
5347 */
5348 SET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
5349 update_all_type2_routes(bgp_evpn, vpn);
5350 }
5351 }
5352 }
5353
5354 /* Map auto derive or configured RTs */
5355 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
5356 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
5357 else
5358 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
5359
5360 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD))
5361 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
5362
5363 /* auto derive RD */
5364 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
5365
5366 /* link all corresponding l2vnis */
5367 hash_iterate(bgp_evpn->vnihash,
5368 (void (*)(struct hash_bucket *,
5369 void *))link_l2vni_hash_to_l3vni,
5370 bgp_vrf);
5371
5372 /* Only update all corresponding type-2 routes if we are advertising two
5373 * labels along with type-2 routes
5374 */
5375 if (!filter)
5376 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
5377 update_routes_for_vni(bgp_evpn, vpn);
5378
5379 /* advertise type-5 routes if needed */
5380 update_advertise_vrf_routes(bgp_vrf);
5381
5382 /* install all remote routes belonging to this l3vni into correspondng
5383 * vrf */
5384 install_routes_for_vrf(bgp_vrf);
5385
5386 return 0;
5387 }
5388
5389 int bgp_evpn_local_l3vni_del(vni_t l3vni, vrf_id_t vrf_id)
5390 {
5391 struct bgp *bgp_vrf = NULL; /* bgp vrf instance */
5392 struct bgp *bgp_evpn = NULL; /* EVPN bgp instance */
5393 struct listnode *node = NULL;
5394 struct listnode *next = NULL;
5395 struct bgpevpn *vpn = NULL;
5396
5397 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
5398 if (!bgp_vrf) {
5399 flog_err(
5400 EC_BGP_NO_DFLT,
5401 "Cannot process L3VNI %u Del - Could not find BGP instance",
5402 l3vni);
5403 return -1;
5404 }
5405
5406 bgp_evpn = bgp_get_evpn();
5407 if (!bgp_evpn) {
5408 flog_err(
5409 EC_BGP_NO_DFLT,
5410 "Cannot process L3VNI %u Del - Could not find EVPN BGP instance",
5411 l3vni);
5412 return -1;
5413 }
5414
5415 /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured,
5416 * bgp_delete would not remove/decrement bgp_path_info of the ip_prefix
5417 * routes. This will uninstalling the routes from zebra and decremnt the
5418 * bgp info count.
5419 */
5420 uninstall_routes_for_vrf(bgp_vrf);
5421
5422 /* delete/withdraw all type-5 routes */
5423 delete_withdraw_vrf_routes(bgp_vrf);
5424
5425 /* remove the l3vni from vrf instance */
5426 bgp_vrf->l3vni = 0;
5427
5428 /* remove the Rmac from the BGP vrf */
5429 memset(&bgp_vrf->rmac, 0, sizeof(struct ethaddr));
5430 memset(&bgp_vrf->evpn_info->pip_rmac_zebra, 0, ETH_ALEN);
5431 if (is_zero_mac(&bgp_vrf->evpn_info->pip_rmac_static) &&
5432 !is_zero_mac(&bgp_vrf->evpn_info->pip_rmac))
5433 memset(&bgp_vrf->evpn_info->pip_rmac, 0, ETH_ALEN);
5434
5435 /* remove default import RT or Unmap non-default import RT */
5436 if (!list_isempty(bgp_vrf->vrf_import_rtl)) {
5437 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
5438 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
5439 list_delete_all_node(bgp_vrf->vrf_import_rtl);
5440 }
5441
5442 /* remove default export RT */
5443 if (!list_isempty(bgp_vrf->vrf_export_rtl) &&
5444 !CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD)) {
5445 list_delete_all_node(bgp_vrf->vrf_export_rtl);
5446 }
5447
5448 /* update all corresponding local mac-ip routes */
5449 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY)) {
5450 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) {
5451 UNSET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
5452 update_routes_for_vni(bgp_evpn, vpn);
5453 }
5454 }
5455
5456 /* If any L2VNIs point to this instance, unlink them. */
5457 for (ALL_LIST_ELEMENTS(bgp_vrf->l2vnis, node, next, vpn))
5458 bgpevpn_unlink_from_l3vni(vpn);
5459
5460 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
5461
5462 /* Delete the instance if it was autocreated */
5463 if (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO))
5464 bgp_delete(bgp_vrf);
5465
5466 return 0;
5467 }
5468
5469 /*
5470 * Handle del of a local VNI.
5471 */
5472 int bgp_evpn_local_vni_del(struct bgp *bgp, vni_t vni)
5473 {
5474 struct bgpevpn *vpn;
5475
5476 /* Locate VNI hash */
5477 vpn = bgp_evpn_lookup_vni(bgp, vni);
5478 if (!vpn) {
5479 if (bgp_debug_zebra(NULL))
5480 flog_warn(
5481 EC_BGP_EVPN_VPN_VNI,
5482 "%u: VNI hash entry for VNI %u not found at DEL",
5483 bgp->vrf_id, vni);
5484 return 0;
5485 }
5486
5487 /* Remove all local EVPN routes and schedule for processing (to
5488 * withdraw from peers).
5489 */
5490 delete_routes_for_vni(bgp, vpn);
5491
5492 /*
5493 * tunnel is no longer active, del tunnel ip address from tip_hash
5494 */
5495 bgp_tip_del(bgp, &vpn->originator_ip);
5496
5497 /* Clear "live" flag and see if hash needs to be freed. */
5498 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
5499 if (!is_vni_configured(vpn))
5500 bgp_evpn_free(bgp, vpn);
5501
5502 return 0;
5503 }
5504
5505 /*
5506 * Handle add (or update) of a local VNI. The VNI changes we care
5507 * about are for the local-tunnel-ip and the (tenant) VRF.
5508 */
5509 int bgp_evpn_local_vni_add(struct bgp *bgp, vni_t vni,
5510 struct in_addr originator_ip,
5511 vrf_id_t tenant_vrf_id,
5512 struct in_addr mcast_grp)
5513
5514 {
5515 struct bgpevpn *vpn;
5516 struct prefix_evpn p;
5517
5518 /* Lookup VNI. If present and no change, exit. */
5519 vpn = bgp_evpn_lookup_vni(bgp, vni);
5520 if (vpn) {
5521
5522 if (is_vni_live(vpn)
5523 && IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip)
5524 && IPV4_ADDR_SAME(&vpn->mcast_grp, &mcast_grp)
5525 && vpn->tenant_vrf_id == tenant_vrf_id)
5526 /* Probably some other param has changed that we don't
5527 * care about. */
5528 return 0;
5529
5530 bgp_evpn_mcast_grp_change(bgp, vpn, mcast_grp);
5531
5532 /* Update tenant_vrf_id if it has changed. */
5533 if (vpn->tenant_vrf_id != tenant_vrf_id) {
5534 bgpevpn_unlink_from_l3vni(vpn);
5535 vpn->tenant_vrf_id = tenant_vrf_id;
5536 bgpevpn_link_to_l3vni(vpn);
5537 }
5538
5539 /* If tunnel endpoint IP has changed, update (and delete prior
5540 * type-3 route, if needed.)
5541 */
5542 if (!IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip))
5543 handle_tunnel_ip_change(bgp, vpn, originator_ip);
5544
5545 /* Update all routes with new endpoint IP and/or export RT
5546 * for VRFs
5547 */
5548 if (is_vni_live(vpn))
5549 update_routes_for_vni(bgp, vpn);
5550 }
5551
5552 /* Create or update as appropriate. */
5553 if (!vpn) {
5554 vpn = bgp_evpn_new(bgp, vni, originator_ip, tenant_vrf_id,
5555 mcast_grp);
5556 if (!vpn) {
5557 flog_err(
5558 EC_BGP_VNI,
5559 "%u: Failed to allocate VNI entry for VNI %u - at Add",
5560 bgp->vrf_id, vni);
5561 return -1;
5562 }
5563 }
5564
5565 /* if the VNI is live already, there is nothing more to do */
5566 if (is_vni_live(vpn))
5567 return 0;
5568
5569 /* Mark as "live" */
5570 SET_FLAG(vpn->flags, VNI_FLAG_LIVE);
5571
5572 /* tunnel is now active, add tunnel-ip to db */
5573 bgp_tip_add(bgp, &originator_ip);
5574
5575 /* filter routes as nexthop database has changed */
5576 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
5577
5578 /*
5579 * Create EVPN type-3 route and schedule for processing.
5580 *
5581 * RT-3 only if doing head-end replication
5582 */
5583 if (bgp_evpn_vni_flood_mode_get(bgp, vpn)
5584 == VXLAN_FLOOD_HEAD_END_REPL) {
5585 build_evpn_type3_prefix(&p, vpn->originator_ip);
5586 if (update_evpn_route(bgp, vpn, &p, 0, 0, NULL)) {
5587 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
5588 "%u: Type3 route creation failure for VNI %u",
5589 bgp->vrf_id, vni);
5590 return -1;
5591 }
5592 }
5593
5594 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
5595 * VNI,
5596 * install them.
5597 */
5598 install_routes_for_vni(bgp, vpn);
5599
5600 /* If we are advertising gateway mac-ip
5601 It needs to be conveyed again to zebra */
5602 bgp_zebra_advertise_gw_macip(bgp, vpn->advertise_gw_macip, vpn->vni);
5603
5604 /* advertise svi mac-ip knob to zebra */
5605 bgp_zebra_advertise_svi_macip(bgp, vpn->advertise_svi_macip, vpn->vni);
5606
5607 return 0;
5608 }
5609
5610 /*
5611 * Handle change in setting for BUM handling. The supported values
5612 * are head-end replication and dropping all BUM packets. Any change
5613 * should be registered with zebra. Also, if doing head-end replication,
5614 * need to advertise local VNIs as EVPN RT-3 wheras, if BUM packets are
5615 * to be dropped, the RT-3s must be withdrawn.
5616 */
5617 void bgp_evpn_flood_control_change(struct bgp *bgp)
5618 {
5619 zlog_info("L2VPN EVPN BUM handling is %s",
5620 bgp->vxlan_flood_ctrl == VXLAN_FLOOD_HEAD_END_REPL ?
5621 "Flooding" : "Flooding Disabled");
5622
5623 bgp_zebra_vxlan_flood_control(bgp, bgp->vxlan_flood_ctrl);
5624 if (bgp->vxlan_flood_ctrl == VXLAN_FLOOD_HEAD_END_REPL)
5625 hash_iterate(bgp->vnihash, create_advertise_type3, bgp);
5626 else if (bgp->vxlan_flood_ctrl == VXLAN_FLOOD_DISABLED)
5627 hash_iterate(bgp->vnihash, delete_withdraw_type3, bgp);
5628 }
5629
5630 /*
5631 * Cleanup EVPN information on disable - Need to delete and withdraw
5632 * EVPN routes from peers.
5633 */
5634 void bgp_evpn_cleanup_on_disable(struct bgp *bgp)
5635 {
5636 hash_iterate(bgp->vnihash, (void (*)(struct hash_bucket *,
5637 void *))cleanup_vni_on_disable,
5638 bgp);
5639 }
5640
5641 /*
5642 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
5643 * BGP instance (default) is being freed.
5644 */
5645 void bgp_evpn_cleanup(struct bgp *bgp)
5646 {
5647 hash_iterate(bgp->vnihash,
5648 (void (*)(struct hash_bucket *, void *))free_vni_entry,
5649 bgp);
5650
5651 hash_free(bgp->import_rt_hash);
5652 bgp->import_rt_hash = NULL;
5653
5654 hash_free(bgp->vrf_import_rt_hash);
5655 bgp->vrf_import_rt_hash = NULL;
5656
5657 hash_free(bgp->vnihash);
5658 bgp->vnihash = NULL;
5659
5660 list_delete(&bgp->vrf_import_rtl);
5661 list_delete(&bgp->vrf_export_rtl);
5662 list_delete(&bgp->l2vnis);
5663 }
5664
5665 /*
5666 * Initialization for EVPN
5667 * Create
5668 * VNI hash table
5669 * hash for RT to VNI
5670 */
5671 void bgp_evpn_init(struct bgp *bgp)
5672 {
5673 bgp->vnihash =
5674 hash_create(vni_hash_key_make, vni_hash_cmp, "BGP VNI Hash");
5675 bgp->import_rt_hash =
5676 hash_create(import_rt_hash_key_make, import_rt_hash_cmp,
5677 "BGP Import RT Hash");
5678 bgp->vrf_import_rt_hash =
5679 hash_create(vrf_import_rt_hash_key_make, vrf_import_rt_hash_cmp,
5680 "BGP VRF Import RT Hash");
5681 bgp->vrf_import_rtl = list_new();
5682 bgp->vrf_import_rtl->cmp =
5683 (int (*)(void *, void *))evpn_route_target_cmp;
5684 bgp->vrf_import_rtl->del = evpn_xxport_delete_ecomm;
5685 bgp->vrf_export_rtl = list_new();
5686 bgp->vrf_export_rtl->cmp =
5687 (int (*)(void *, void *))evpn_route_target_cmp;
5688 bgp->vrf_export_rtl->del = evpn_xxport_delete_ecomm;
5689 bgp->l2vnis = list_new();
5690 bgp->l2vnis->cmp = vni_list_cmp;
5691 /* By default Duplicate Address Dection is enabled.
5692 * Max-moves (N) 5, detection time (M) 180
5693 * default action is warning-only
5694 * freeze action permanently freezes address,
5695 * and freeze time (auto-recovery) is disabled.
5696 */
5697 if (bgp->evpn_info) {
5698 bgp->evpn_info->dup_addr_detect = true;
5699 bgp->evpn_info->dad_time = EVPN_DAD_DEFAULT_TIME;
5700 bgp->evpn_info->dad_max_moves = EVPN_DAD_DEFAULT_MAX_MOVES;
5701 bgp->evpn_info->dad_freeze = false;
5702 bgp->evpn_info->dad_freeze_time = 0;
5703 /* Initialize zebra vxlan */
5704 bgp_zebra_dup_addr_detection(bgp);
5705 /* Enable PIP feature by default for bgp vrf instance */
5706 if (bgp->inst_type == BGP_INSTANCE_TYPE_VRF) {
5707 struct bgp *bgp_default;
5708
5709 bgp->evpn_info->advertise_pip = true;
5710 bgp_default = bgp_get_default();
5711 if (bgp_default)
5712 bgp->evpn_info->pip_ip = bgp_default->router_id;
5713 }
5714 }
5715
5716 /* Default BUM handling is to do head-end replication. */
5717 bgp->vxlan_flood_ctrl = VXLAN_FLOOD_HEAD_END_REPL;
5718 }
5719
5720 void bgp_evpn_vrf_delete(struct bgp *bgp_vrf)
5721 {
5722 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
5723 }
5724
5725 /*
5726 * Get the prefixlen of the ip prefix carried within the type5 evpn route.
5727 */
5728 int bgp_evpn_get_type5_prefixlen(const struct prefix *pfx)
5729 {
5730 struct prefix_evpn *evp = (struct prefix_evpn *)pfx;
5731
5732 if (!pfx || pfx->family != AF_EVPN)
5733 return 0;
5734
5735 if (evp->prefix.route_type != BGP_EVPN_IP_PREFIX_ROUTE)
5736 return 0;
5737
5738 return evp->prefix.prefix_addr.ip_prefix_length;
5739 }
5740
5741 /*
5742 * Should we register nexthop for this EVPN prefix for nexthop tracking?
5743 */
5744 bool bgp_evpn_is_prefix_nht_supported(const struct prefix *pfx)
5745 {
5746 struct prefix_evpn *evp = (struct prefix_evpn *)pfx;
5747
5748 /*
5749 * EVPN routes should be marked as valid only if the nexthop is
5750 * reachable. Only if this happens, the route should be imported
5751 * (into VNI or VRF routing tables) and/or advertised.
5752 * Note: This is currently applied for EVPN type-2, type-3 and
5753 * type-5 routes. It may be tweaked later on for other routes, or
5754 * even removed completely when all routes are handled.
5755 */
5756 if (pfx && pfx->family == AF_EVPN &&
5757 (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE ||
5758 evp->prefix.route_type == BGP_EVPN_IMET_ROUTE ||
5759 evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
5760 return true;
5761
5762 return false;
5763 }