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