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