]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_evpn.c
bgpd: Free vni list on actual deletion
[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_aspath.h"
47 #include "bgpd/bgp_zebra.h"
48 #include "bgpd/bgp_nexthop.h"
49
50 /*
51 * Definitions and external declarations.
52 */
53 extern struct zclient *zclient;
54
55 DEFINE_QOBJ_TYPE(bgpevpn)
56
57
58 /*
59 * Static function declarations
60 */
61 static void delete_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
62 afi_t afi, safi_t safi, struct bgp_node *rn,
63 struct bgp_info **ri);
64 static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn);
65
66 /*
67 * Private functions.
68 */
69
70 /*
71 * Make vni hash key.
72 */
73 static unsigned int vni_hash_key_make(void *p)
74 {
75 struct bgpevpn *vpn = p;
76 return (jhash_1word(vpn->vni, 0));
77 }
78
79 /*
80 * Comparison function for vni hash
81 */
82 static int vni_hash_cmp(const void *p1, const void *p2)
83 {
84 const struct bgpevpn *vpn1 = p1;
85 const struct bgpevpn *vpn2 = p2;
86
87 if (!vpn1 && !vpn2)
88 return 1;
89 if (!vpn1 || !vpn2)
90 return 0;
91 return (vpn1->vni == vpn2->vni);
92 }
93
94 /*
95 * Make vrf import route target hash key.
96 */
97 static unsigned int vrf_import_rt_hash_key_make(void *p)
98 {
99 struct vrf_irt_node *irt = p;
100 char *pnt = irt->rt.val;
101
102 return jhash(pnt, 8, 0x5abc1234);
103 }
104
105 /*
106 * Comparison function for vrf import rt hash
107 */
108 static int vrf_import_rt_hash_cmp(const void *p1, const void *p2)
109 {
110 const struct vrf_irt_node *irt1 = p1;
111 const struct vrf_irt_node *irt2 = p2;
112
113 if (irt1 == NULL && irt2 == NULL)
114 return 1;
115
116 if (irt1 == NULL || irt2 == NULL)
117 return 0;
118
119 return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0);
120 }
121
122 /*
123 * Create a new vrf import_rt in default instance
124 */
125 static struct vrf_irt_node *vrf_import_rt_new(struct ecommunity_val *rt)
126 {
127 struct bgp *bgp_def = NULL;
128 struct vrf_irt_node *irt;
129
130 bgp_def = bgp_get_default();
131 if (!bgp_def) {
132 zlog_err("vrf import rt new - def instance not created yet");
133 return NULL;
134 }
135
136 irt = XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT,
137 sizeof(struct vrf_irt_node));
138 if (!irt)
139 return NULL;
140
141 irt->rt = *rt;
142 irt->vrfs = list_new();
143
144 /* Add to hash */
145 if (!hash_get(bgp_def->vrf_import_rt_hash, irt, hash_alloc_intern)) {
146 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT, irt);
147 return NULL;
148 }
149
150 return irt;
151 }
152
153 /*
154 * Free the vrf import rt node
155 */
156 static void vrf_import_rt_free(struct vrf_irt_node *irt)
157 {
158 struct bgp *bgp_def = NULL;
159
160 bgp_def = bgp_get_default();
161 if (!bgp_def) {
162 zlog_err("vrf import rt free - def instance not created yet");
163 return;
164 }
165
166 hash_release(bgp_def->vrf_import_rt_hash, irt);
167 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT, irt);
168 }
169
170 /*
171 * Function to lookup Import RT node - used to map a RT to set of
172 * VNIs importing routes with that RT.
173 */
174 static struct vrf_irt_node *lookup_vrf_import_rt(struct ecommunity_val *rt)
175 {
176 struct bgp *bgp_def = NULL;
177 struct vrf_irt_node *irt;
178 struct vrf_irt_node tmp;
179
180 bgp_def = bgp_get_default();
181 if (!bgp_def) {
182 zlog_err("vrf import rt lookup - def instance not created yet");
183 return NULL;
184 }
185
186 memset(&tmp, 0, sizeof(struct vrf_irt_node));
187 memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE);
188 irt = hash_lookup(bgp_def->vrf_import_rt_hash, &tmp);
189 return irt;
190 }
191
192 /*
193 * Is specified VRF present on the RT's list of "importing" VRFs?
194 */
195 static int is_vrf_present_in_irt_vrfs(struct list *vrfs, struct bgp *bgp_vrf)
196 {
197 struct listnode *node = NULL, *nnode = NULL;
198 struct bgp *tmp_bgp_vrf = NULL;
199
200 for (ALL_LIST_ELEMENTS(vrfs, node, nnode, tmp_bgp_vrf)) {
201 if (tmp_bgp_vrf == bgp_vrf)
202 return 1;
203 }
204 return 0;
205 }
206
207 /*
208 * Make import route target hash key.
209 */
210 static unsigned int import_rt_hash_key_make(void *p)
211 {
212 struct irt_node *irt = p;
213 char *pnt = irt->rt.val;
214
215 return jhash(pnt, 8, 0xdeadbeef);
216 }
217
218 /*
219 * Comparison function for import rt hash
220 */
221 static int import_rt_hash_cmp(const void *p1, const void *p2)
222 {
223 const struct irt_node *irt1 = p1;
224 const struct irt_node *irt2 = p2;
225
226 if (irt1 == NULL && irt2 == NULL)
227 return 1;
228
229 if (irt1 == NULL || irt2 == NULL)
230 return 0;
231
232 return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0);
233 }
234
235 /*
236 * Create a new import_rt
237 */
238 static struct irt_node *import_rt_new(struct bgp *bgp,
239 struct ecommunity_val *rt)
240 {
241 struct irt_node *irt;
242
243 if (!bgp)
244 return NULL;
245
246 irt = XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT, sizeof(struct irt_node));
247 if (!irt)
248 return NULL;
249
250 irt->rt = *rt;
251 irt->vnis = list_new();
252
253 /* Add to hash */
254 if (!hash_get(bgp->import_rt_hash, irt, hash_alloc_intern)) {
255 XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
256 return NULL;
257 }
258
259 return irt;
260 }
261
262 /*
263 * Free the import rt node
264 */
265 static void import_rt_free(struct bgp *bgp, struct irt_node *irt)
266 {
267 hash_release(bgp->import_rt_hash, irt);
268 list_delete_and_null(&irt->vnis);
269 XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
270 }
271
272 /*
273 * Function to lookup Import RT node - used to map a RT to set of
274 * VNIs importing routes with that RT.
275 */
276 static struct irt_node *lookup_import_rt(struct bgp *bgp,
277 struct ecommunity_val *rt)
278 {
279 struct irt_node *irt;
280 struct irt_node tmp;
281
282 memset(&tmp, 0, sizeof(struct irt_node));
283 memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE);
284 irt = hash_lookup(bgp->import_rt_hash, &tmp);
285 return irt;
286 }
287
288 /*
289 * Is specified VNI present on the RT's list of "importing" VNIs?
290 */
291 static int is_vni_present_in_irt_vnis(struct list *vnis, struct bgpevpn *vpn)
292 {
293 struct listnode *node, *nnode;
294 struct bgpevpn *tmp_vpn;
295
296 for (ALL_LIST_ELEMENTS(vnis, node, nnode, tmp_vpn)) {
297 if (tmp_vpn == vpn)
298 return 1;
299 }
300
301 return 0;
302 }
303
304 /*
305 * Compare Route Targets.
306 */
307 static int evpn_route_target_cmp(struct ecommunity *ecom1,
308 struct ecommunity *ecom2)
309 {
310 if (ecom1 && !ecom2)
311 return -1;
312
313 if (!ecom1 && ecom2)
314 return 1;
315
316 if (!ecom1 && !ecom2)
317 return 0;
318
319 if (ecom1->str && !ecom2->str)
320 return -1;
321
322 if (!ecom1->str && ecom2->str)
323 return 1;
324
325 if (!ecom1->str && !ecom2->str)
326 return 0;
327
328 return strcmp(ecom1->str, ecom2->str);
329 }
330
331 static void evpn_xxport_delete_ecomm(void *val)
332 {
333 struct ecommunity *ecomm = val;
334 ecommunity_free(&ecomm);
335 }
336
337 /*
338 * Mask off global-admin field of specified extended community (RT),
339 * just retain the local-admin field.
340 */
341 static inline void mask_ecom_global_admin(struct ecommunity_val *dst,
342 struct ecommunity_val *src)
343 {
344 uint8_t type;
345
346 type = src->val[0];
347 dst->val[0] = 0;
348 if (type == ECOMMUNITY_ENCODE_AS) {
349 dst->val[2] = dst->val[3] = 0;
350 } else if (type == ECOMMUNITY_ENCODE_AS4
351 || type == ECOMMUNITY_ENCODE_IP) {
352 dst->val[2] = dst->val[3] = 0;
353 dst->val[4] = dst->val[5] = 0;
354 }
355 }
356
357 /*
358 * Map one RT to specified VRF.
359 * bgp_vrf = BGP vrf instance
360 */
361 static void map_vrf_to_rt(struct bgp *bgp_vrf, struct ecommunity_val *eval)
362 {
363 struct vrf_irt_node *irt = NULL;
364 struct ecommunity_val eval_tmp;
365
366 /* If using "automatic" RT,
367 * we only care about the local-admin sub-field.
368 * This is to facilitate using L3VNI(VRF-VNI)
369 * as the RT for EBGP peering too.
370 */
371 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
372 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
373 mask_ecom_global_admin(&eval_tmp, eval);
374
375 irt = lookup_vrf_import_rt(&eval_tmp);
376 if (irt && is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
377 /* Already mapped. */
378 return;
379
380 if (!irt)
381 irt = vrf_import_rt_new(&eval_tmp);
382
383 /* Add VRF to the list for this RT. */
384 listnode_add(irt->vrfs, bgp_vrf);
385 }
386
387 /*
388 * Unmap specified VRF from specified RT. If there are no other
389 * VRFs for this RT, then the RT hash is deleted.
390 * bgp_vrf: BGP VRF specific instance
391 */
392 static void unmap_vrf_from_rt(struct bgp *bgp_vrf, struct vrf_irt_node *irt)
393 {
394 /* Delete VRF from list for this RT. */
395 listnode_delete(irt->vrfs, bgp_vrf);
396 if (!listnode_head(irt->vrfs)) {
397 list_delete_and_null(&irt->vrfs);
398 vrf_import_rt_free(irt);
399 }
400 }
401
402 /*
403 * Map one RT to specified VNI.
404 */
405 static void map_vni_to_rt(struct bgp *bgp, struct bgpevpn *vpn,
406 struct ecommunity_val *eval)
407 {
408 struct irt_node *irt;
409 struct ecommunity_val eval_tmp;
410
411 /* If using "automatic" RT, we only care about the local-admin
412 * sub-field.
413 * This is to facilitate using VNI as the RT for EBGP peering too.
414 */
415 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
416 if (!is_import_rt_configured(vpn))
417 mask_ecom_global_admin(&eval_tmp, eval);
418
419 irt = lookup_import_rt(bgp, &eval_tmp);
420 if (irt)
421 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
422 /* Already mapped. */
423 return;
424
425 if (!irt) {
426 irt = import_rt_new(bgp, &eval_tmp);
427 assert(irt);
428 }
429
430 /* Add VNI to the hash list for this RT. */
431 listnode_add(irt->vnis, vpn);
432 }
433
434 /*
435 * Unmap specified VNI from specified RT. If there are no other
436 * VNIs for this RT, then the RT hash is deleted.
437 */
438 static void unmap_vni_from_rt(struct bgp *bgp, struct bgpevpn *vpn,
439 struct irt_node *irt)
440 {
441 /* Delete VNI from hash list for this RT. */
442 listnode_delete(irt->vnis, vpn);
443 if (!listnode_head(irt->vnis)) {
444 import_rt_free(bgp, irt);
445 }
446 }
447
448 /*
449 * Create RT extended community automatically from passed information:
450 * of the form AS:VNI.
451 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
452 * the need is to get a RT value that will be unique across different
453 * VNIs but the same across routers (in the same AS) for a particular
454 * VNI.
455 */
456 static void form_auto_rt(struct bgp *bgp, vni_t vni, struct list *rtl)
457 {
458 struct ecommunity_val eval;
459 struct ecommunity *ecomadd;
460
461 if (bgp->advertise_autort_rfc8365)
462 vni |= EVPN_AUTORT_VXLAN;
463 encode_route_target_as((bgp->as & 0xFFFF), vni, &eval);
464
465 ecomadd = ecommunity_new();
466 ecommunity_add_val(ecomadd, &eval);
467 listnode_add_sort(rtl, ecomadd);
468 }
469
470 /*
471 * Derive RD and RT for a VNI automatically. Invoked at the time of
472 * creation of a VNI.
473 */
474 static void derive_rd_rt_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
475 {
476 bgp_evpn_derive_auto_rd(bgp, vpn);
477 bgp_evpn_derive_auto_rt_import(bgp, vpn);
478 bgp_evpn_derive_auto_rt_export(bgp, vpn);
479 }
480
481 /*
482 * Convert nexthop (remote VTEP IP) into an IPv6 address.
483 */
484 static void evpn_convert_nexthop_to_ipv6(struct attr *attr)
485 {
486 if (BGP_ATTR_NEXTHOP_AFI_IP6(attr))
487 return;
488 ipv4_to_ipv4_mapped_ipv6(&attr->mp_nexthop_global, attr->nexthop);
489 attr->mp_nexthop_len = IPV6_MAX_BYTELEN;
490 }
491
492 /*
493 * Add (update) or delete MACIP from zebra.
494 */
495 static int bgp_zebra_send_remote_macip(struct bgp *bgp, struct bgpevpn *vpn,
496 struct prefix_evpn *p,
497 struct in_addr remote_vtep_ip, int add,
498 uint8_t flags)
499 {
500 struct stream *s;
501 int ipa_len;
502 char buf1[ETHER_ADDR_STRLEN];
503 char buf2[INET6_ADDRSTRLEN];
504 char buf3[INET6_ADDRSTRLEN];
505
506 /* Check socket. */
507 if (!zclient || zclient->sock < 0)
508 return 0;
509
510 /* Don't try to register if Zebra doesn't know of this instance. */
511 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
512 return 0;
513
514 s = zclient->obuf;
515 stream_reset(s);
516
517 zclient_create_header(
518 s, add ? ZEBRA_REMOTE_MACIP_ADD : ZEBRA_REMOTE_MACIP_DEL,
519 bgp->vrf_id);
520 stream_putl(s, vpn->vni);
521 stream_put(s, &p->prefix.macip_addr.mac.octet, ETH_ALEN); /* Mac Addr */
522 /* IP address length and IP address, if any. */
523 if (is_evpn_prefix_ipaddr_none(p))
524 stream_putl(s, 0);
525 else {
526 ipa_len = is_evpn_prefix_ipaddr_v4(p) ? IPV4_MAX_BYTELEN
527 : IPV6_MAX_BYTELEN;
528 stream_putl(s, ipa_len);
529 stream_put(s, &p->prefix.macip_addr.ip.ip.addr, ipa_len);
530 }
531 stream_put_in_addr(s, &remote_vtep_ip);
532
533 /* TX flags - MAC sticky status and/or gateway mac */
534 if (add)
535 stream_putc(s, flags);
536
537 stream_putw_at(s, 0, stream_get_endp(s));
538
539 if (bgp_debug_zebra(NULL))
540 zlog_debug(
541 "Tx %s MACIP, VNI %u MAC %s IP %s (flags: 0x%x) remote VTEP %s",
542 add ? "ADD" : "DEL", vpn->vni,
543 prefix_mac2str(&p->prefix.macip_addr.mac,
544 buf1, sizeof(buf1)),
545 ipaddr2str(&p->prefix.macip_addr.ip,
546 buf3, sizeof(buf3)), flags,
547 inet_ntop(AF_INET, &remote_vtep_ip, buf2,
548 sizeof(buf2)));
549
550 return zclient_send_message(zclient);
551 }
552
553 /*
554 * Add (update) or delete remote VTEP from zebra.
555 */
556 static int bgp_zebra_send_remote_vtep(struct bgp *bgp, struct bgpevpn *vpn,
557 struct prefix_evpn *p, int add)
558 {
559 struct stream *s;
560
561 /* Check socket. */
562 if (!zclient || zclient->sock < 0)
563 return 0;
564
565 /* Don't try to register if Zebra doesn't know of this instance. */
566 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
567 return 0;
568
569 s = zclient->obuf;
570 stream_reset(s);
571
572 zclient_create_header(
573 s, add ? ZEBRA_REMOTE_VTEP_ADD : ZEBRA_REMOTE_VTEP_DEL,
574 bgp->vrf_id);
575 stream_putl(s, vpn->vni);
576 if (is_evpn_prefix_ipaddr_v4(p))
577 stream_put_in_addr(s, &p->prefix.imet_addr.ip.ipaddr_v4);
578 else if (is_evpn_prefix_ipaddr_v6(p)) {
579 zlog_err(
580 "Bad remote IP when trying to %s remote VTEP for VNI %u",
581 add ? "ADD" : "DEL", vpn->vni);
582 return -1;
583 }
584
585 stream_putw_at(s, 0, stream_get_endp(s));
586
587 if (bgp_debug_zebra(NULL))
588 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
589 add ? "ADD" : "DEL", vpn->vni,
590 inet_ntoa(p->prefix.imet_addr.ip.ipaddr_v4));
591
592 return zclient_send_message(zclient);
593 }
594
595 /*
596 * Build extended communities for EVPN prefix route.
597 */
598 static void build_evpn_type5_route_extcomm(struct bgp *bgp_vrf,
599 struct attr *attr)
600 {
601 struct ecommunity ecom_encap;
602 struct ecommunity ecom_rmac;
603 struct ecommunity_val eval;
604 struct ecommunity_val eval_rmac;
605 bgp_encap_types tnl_type;
606 struct listnode *node, *nnode;
607 struct ecommunity *ecom;
608 struct list *vrf_export_rtl = NULL;
609
610 /* Encap */
611 tnl_type = BGP_ENCAP_TYPE_VXLAN;
612 memset(&ecom_encap, 0, sizeof(ecom_encap));
613 encode_encap_extcomm(tnl_type, &eval);
614 ecom_encap.size = 1;
615 ecom_encap.val = (uint8_t *)eval.val;
616
617 /* Add Encap */
618 attr->ecommunity = ecommunity_dup(&ecom_encap);
619
620 /* Add the export RTs for L3VNI/VRF */
621 vrf_export_rtl = bgp_vrf->vrf_export_rtl;
622 if (vrf_export_rtl && !list_isempty(vrf_export_rtl)) {
623 for (ALL_LIST_ELEMENTS(vrf_export_rtl, node, nnode, ecom))
624 attr->ecommunity =
625 ecommunity_merge(attr->ecommunity, ecom);
626 }
627
628 /* add the router mac extended community */
629 if (!is_zero_mac(&attr->rmac)) {
630 memset(&ecom_rmac, 0, sizeof(ecom_rmac));
631 encode_rmac_extcomm(&eval_rmac, &attr->rmac);
632 ecom_rmac.size = 1;
633 ecom_rmac.val = (uint8_t *)eval_rmac.val;
634 attr->ecommunity =
635 ecommunity_merge(attr->ecommunity, &ecom_rmac);
636 }
637
638 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
639 }
640
641 /*
642 * Build extended communities for EVPN route.
643 * This function is applicable for type-2 and type-3 routes. The layer-2 RT
644 * and ENCAP extended communities are applicable for all routes.
645 * The default gateway extended community and MAC mobility (sticky) extended
646 * community are added as needed based on passed settings - only for type-2
647 * routes. Likewise, the layer-3 RT and Router MAC extended communities are
648 * added, if present, based on passed settings - only for non-link-local
649 * type-2 routes.
650 */
651 static void build_evpn_route_extcomm(struct bgpevpn *vpn, struct attr *attr,
652 int add_l3_ecomm)
653 {
654 struct ecommunity ecom_encap;
655 struct ecommunity ecom_sticky;
656 struct ecommunity ecom_default_gw;
657 struct ecommunity ecom_rmac;
658 struct ecommunity_val eval;
659 struct ecommunity_val eval_sticky;
660 struct ecommunity_val eval_default_gw;
661 struct ecommunity_val eval_rmac;
662 bgp_encap_types tnl_type;
663 struct listnode *node, *nnode;
664 struct ecommunity *ecom;
665 uint32_t seqnum;
666 struct list *vrf_export_rtl = NULL;
667
668 /* Encap */
669 tnl_type = BGP_ENCAP_TYPE_VXLAN;
670 memset(&ecom_encap, 0, sizeof(ecom_encap));
671 encode_encap_extcomm(tnl_type, &eval);
672 ecom_encap.size = 1;
673 ecom_encap.val = (uint8_t *)eval.val;
674
675 /* Add Encap */
676 attr->ecommunity = ecommunity_dup(&ecom_encap);
677
678 /* Add the export RTs for L2VNI */
679 for (ALL_LIST_ELEMENTS(vpn->export_rtl, node, nnode, ecom))
680 attr->ecommunity = ecommunity_merge(attr->ecommunity, ecom);
681
682 /* Add the export RTs for L3VNI if told to - caller determines
683 * when this should be done.
684 */
685 if (add_l3_ecomm) {
686 vrf_export_rtl = bgpevpn_get_vrf_export_rtl(vpn);
687 if (vrf_export_rtl && !list_isempty(vrf_export_rtl)) {
688 for (ALL_LIST_ELEMENTS(vrf_export_rtl, node, nnode,
689 ecom))
690 attr->ecommunity = ecommunity_merge(
691 attr->ecommunity, ecom);
692 }
693 }
694
695 /* Add MAC mobility (sticky) if needed. */
696 if (attr->sticky) {
697 seqnum = 0;
698 memset(&ecom_sticky, 0, sizeof(ecom_sticky));
699 encode_mac_mobility_extcomm(1, seqnum, &eval_sticky);
700 ecom_sticky.size = 1;
701 ecom_sticky.val = (uint8_t *)eval_sticky.val;
702 attr->ecommunity =
703 ecommunity_merge(attr->ecommunity, &ecom_sticky);
704 }
705
706 /* Add RMAC, if told to. */
707 if (add_l3_ecomm) {
708 memset(&ecom_rmac, 0, sizeof(ecom_rmac));
709 encode_rmac_extcomm(&eval_rmac, &attr->rmac);
710 ecom_rmac.size = 1;
711 ecom_rmac.val = (uint8_t *)eval_rmac.val;
712 attr->ecommunity =
713 ecommunity_merge(attr->ecommunity, &ecom_rmac);
714 }
715
716 /* Add default gateway, if needed. */
717 if (attr->default_gw) {
718 memset(&ecom_default_gw, 0, sizeof(ecom_default_gw));
719 encode_default_gw_extcomm(&eval_default_gw);
720 ecom_default_gw.size = 1;
721 ecom_default_gw.val = (uint8_t *)eval_default_gw.val;
722 attr->ecommunity =
723 ecommunity_merge(attr->ecommunity, &ecom_default_gw);
724 }
725
726 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
727 }
728
729 /*
730 * Add MAC mobility extended community to attribute.
731 */
732 static void add_mac_mobility_to_attr(uint32_t seq_num, struct attr *attr)
733 {
734 struct ecommunity ecom_tmp;
735 struct ecommunity_val eval;
736 uint8_t *ecom_val_ptr;
737 int i;
738 uint8_t *pnt;
739 int type = 0;
740 int sub_type = 0;
741
742 /* Build MM */
743 encode_mac_mobility_extcomm(0, seq_num, &eval);
744
745 /* Find current MM ecommunity */
746 ecom_val_ptr = NULL;
747
748 if (attr->ecommunity) {
749 for (i = 0; i < attr->ecommunity->size; i++) {
750 pnt = attr->ecommunity->val + (i * 8);
751 type = *pnt++;
752 sub_type = *pnt++;
753
754 if (type == ECOMMUNITY_ENCODE_EVPN
755 && sub_type
756 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY) {
757 ecom_val_ptr = (uint8_t *)(attr->ecommunity->val
758 + (i * 8));
759 break;
760 }
761 }
762 }
763
764 /* Update the existing MM ecommunity */
765 if (ecom_val_ptr) {
766 memcpy(ecom_val_ptr, eval.val, sizeof(char) * ECOMMUNITY_SIZE);
767 }
768 /* Add MM to existing */
769 else {
770 memset(&ecom_tmp, 0, sizeof(ecom_tmp));
771 ecom_tmp.size = 1;
772 ecom_tmp.val = (uint8_t *)eval.val;
773
774 if (attr->ecommunity)
775 attr->ecommunity =
776 ecommunity_merge(attr->ecommunity, &ecom_tmp);
777 else
778 attr->ecommunity = ecommunity_dup(&ecom_tmp);
779 }
780 }
781
782 /* Install EVPN route into zebra. */
783 static int evpn_zebra_install(struct bgp *bgp, struct bgpevpn *vpn,
784 struct prefix_evpn *p,
785 struct in_addr remote_vtep_ip, uint8_t flags)
786 {
787 int ret;
788
789 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
790 ret = bgp_zebra_send_remote_macip(bgp, vpn, p, remote_vtep_ip,
791 1, flags);
792 else
793 ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, 1);
794
795 return ret;
796 }
797
798 /* Uninstall EVPN route from zebra. */
799 static int evpn_zebra_uninstall(struct bgp *bgp, struct bgpevpn *vpn,
800 struct prefix_evpn *p,
801 struct in_addr remote_vtep_ip)
802 {
803 int ret;
804
805 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
806 ret = bgp_zebra_send_remote_macip(bgp, vpn, p, remote_vtep_ip,
807 0, 0);
808 else
809 ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, 0);
810
811 return ret;
812 }
813
814 /*
815 * Due to MAC mobility, the prior "local" best route has been supplanted
816 * by a "remote" best route. The prior route has to be deleted and withdrawn
817 * from peers.
818 */
819 static void evpn_delete_old_local_route(struct bgp *bgp, struct bgpevpn *vpn,
820 struct bgp_node *rn,
821 struct bgp_info *old_local)
822 {
823 struct bgp_node *global_rn;
824 struct bgp_info *ri;
825 afi_t afi = AFI_L2VPN;
826 safi_t safi = SAFI_EVPN;
827
828 /* Locate route node in the global EVPN routing table. Note that
829 * this table is a 2-level tree (RD-level + Prefix-level) similar to
830 * L3VPN routes.
831 */
832 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
833 (struct prefix *)&rn->p, &vpn->prd);
834 if (global_rn) {
835 /* Delete route entry in the global EVPN table. */
836 delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);
837
838 /* Schedule for processing - withdraws to peers happen from
839 * this table.
840 */
841 if (ri)
842 bgp_process(bgp, global_rn, afi, safi);
843 bgp_unlock_node(global_rn);
844 }
845
846 /* Delete route entry in the VNI route table, caller to remove. */
847 bgp_info_delete(rn, old_local);
848 }
849
850 /*
851 * Calculate the best path for an EVPN route. Install/update best path in zebra,
852 * if appropriate.
853 */
854 static int evpn_route_select_install(struct bgp *bgp, struct bgpevpn *vpn,
855 struct bgp_node *rn)
856 {
857 struct bgp_info *old_select, *new_select;
858 struct bgp_info_pair old_and_new;
859 afi_t afi = AFI_L2VPN;
860 safi_t safi = SAFI_EVPN;
861 int ret = 0;
862 uint8_t flags = 0;
863
864 /* Compute the best path. */
865 bgp_best_selection(bgp, rn, &bgp->maxpaths[afi][safi], &old_and_new,
866 afi, safi);
867 old_select = old_and_new.old;
868 new_select = old_and_new.new;
869
870 /* If the best path hasn't changed - see if there is still something to
871 * update
872 * to zebra RIB.
873 */
874 if (old_select && old_select == new_select
875 && old_select->type == ZEBRA_ROUTE_BGP
876 && old_select->sub_type == BGP_ROUTE_IMPORTED
877 && !CHECK_FLAG(rn->flags, BGP_NODE_USER_CLEAR)
878 && !CHECK_FLAG(old_select->flags, BGP_INFO_ATTR_CHANGED)
879 && !bgp->addpath_tx_used[afi][safi]) {
880 if (bgp_zebra_has_route_changed(rn, old_select)) {
881 if (old_select->attr->sticky)
882 SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
883 if (old_select->attr->default_gw)
884 SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
885 ret = evpn_zebra_install(
886 bgp, vpn, (struct prefix_evpn *)&rn->p,
887 old_select->attr->nexthop, flags);
888 }
889 UNSET_FLAG(old_select->flags, BGP_INFO_MULTIPATH_CHG);
890 bgp_zebra_clear_route_change_flags(rn);
891 return ret;
892 }
893
894 /* If the user did a "clear" this flag will be set */
895 UNSET_FLAG(rn->flags, BGP_NODE_USER_CLEAR);
896
897 /* bestpath has changed; update relevant fields and install or uninstall
898 * into the zebra RIB.
899 */
900 if (old_select || new_select)
901 bgp_bump_version(rn);
902
903 if (old_select)
904 bgp_info_unset_flag(rn, old_select, BGP_INFO_SELECTED);
905 if (new_select) {
906 bgp_info_set_flag(rn, new_select, BGP_INFO_SELECTED);
907 bgp_info_unset_flag(rn, new_select, BGP_INFO_ATTR_CHANGED);
908 UNSET_FLAG(new_select->flags, BGP_INFO_MULTIPATH_CHG);
909 }
910
911 if (new_select && new_select->type == ZEBRA_ROUTE_BGP
912 && new_select->sub_type == BGP_ROUTE_IMPORTED) {
913 flags = 0;
914 if (new_select->attr->sticky)
915 SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
916 if (new_select->attr->default_gw)
917 SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
918 ret = evpn_zebra_install(bgp, vpn, (struct prefix_evpn *)&rn->p,
919 new_select->attr->nexthop, flags);
920 /* If an old best existed and it was a "local" route, the only
921 * reason
922 * it would be supplanted is due to MAC mobility procedures. So,
923 * we
924 * need to do an implicit delete and withdraw that route from
925 * peers.
926 */
927 if (old_select && old_select->peer == bgp->peer_self
928 && old_select->type == ZEBRA_ROUTE_BGP
929 && old_select->sub_type == BGP_ROUTE_STATIC)
930 evpn_delete_old_local_route(bgp, vpn, rn, old_select);
931 } else {
932 if (old_select && old_select->type == ZEBRA_ROUTE_BGP
933 && old_select->sub_type == BGP_ROUTE_IMPORTED)
934 ret = evpn_zebra_uninstall(bgp, vpn,
935 (struct prefix_evpn *)&rn->p,
936 old_select->attr->nexthop);
937 }
938
939 /* Clear any route change flags. */
940 bgp_zebra_clear_route_change_flags(rn);
941
942 /* Reap old select bgp_info, if it has been removed */
943 if (old_select && CHECK_FLAG(old_select->flags, BGP_INFO_REMOVED))
944 bgp_info_reap(rn, old_select);
945
946 return ret;
947 }
948
949 /*
950 * Return true if the local ri for this rn is of type gateway mac
951 */
952 static int evpn_route_is_def_gw(struct bgp *bgp, struct bgp_node *rn)
953 {
954 struct bgp_info *tmp_ri = NULL;
955 struct bgp_info *local_ri = NULL;
956
957 local_ri = NULL;
958 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
959 if (tmp_ri->peer == bgp->peer_self
960 && tmp_ri->type == ZEBRA_ROUTE_BGP
961 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
962 local_ri = tmp_ri;
963 }
964
965 if (!local_ri)
966 return 0;
967
968 return local_ri->attr->default_gw;
969 }
970
971
972 /*
973 * Return true if the local ri for this rn has sticky set
974 */
975 static int evpn_route_is_sticky(struct bgp *bgp, struct bgp_node *rn)
976 {
977 struct bgp_info *tmp_ri;
978 struct bgp_info *local_ri;
979
980 local_ri = NULL;
981 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
982 if (tmp_ri->peer == bgp->peer_self
983 && tmp_ri->type == ZEBRA_ROUTE_BGP
984 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
985 local_ri = tmp_ri;
986 }
987
988 if (!local_ri)
989 return 0;
990
991 return local_ri->attr->sticky;
992 }
993
994 static int update_evpn_type5_route_entry(struct bgp *bgp_def,
995 struct bgp *bgp_vrf, afi_t afi,
996 safi_t safi, struct bgp_node *rn,
997 struct attr *attr, int *route_changed)
998 {
999 struct attr *attr_new = NULL;
1000 struct bgp_info *ri = NULL;
1001 mpls_label_t label = MPLS_INVALID_LABEL;
1002 struct bgp_info *local_ri = NULL;
1003 struct bgp_info *tmp_ri = NULL;
1004
1005 *route_changed = 0;
1006 /* locate the local route entry if any */
1007 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
1008 if (tmp_ri->peer == bgp_def->peer_self
1009 && tmp_ri->type == ZEBRA_ROUTE_BGP
1010 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
1011 local_ri = tmp_ri;
1012 }
1013
1014 /* create a new route entry if one doesnt exist.
1015 Otherwise see if route attr has changed
1016 */
1017 if (!local_ri) {
1018
1019 /* route has changed as this is the first entry */
1020 *route_changed = 1;
1021
1022 /* Add (or update) attribute to hash. */
1023 attr_new = bgp_attr_intern(attr);
1024
1025 /* create the route info from attribute */
1026 ri = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0,
1027 bgp_def->peer_self, attr_new, rn);
1028 SET_FLAG(ri->flags, BGP_INFO_VALID);
1029
1030 /* Type-5 routes advertise the L3-VNI */
1031 bgp_info_extra_get(ri);
1032 vni2label(bgp_vrf->l3vni, &label);
1033 memcpy(&ri->extra->label, &label, sizeof(label));
1034 ri->extra->num_labels = 1;
1035
1036 /* add the route entry to route node*/
1037 bgp_info_add(rn, ri);
1038 } else {
1039
1040 tmp_ri = local_ri;
1041 if (!attrhash_cmp(tmp_ri->attr, attr)) {
1042
1043 /* attribute changed */
1044 *route_changed = 1;
1045
1046 /* The attribute has changed. */
1047 /* Add (or update) attribute to hash. */
1048 attr_new = bgp_attr_intern(attr);
1049 bgp_info_set_flag(rn, tmp_ri, BGP_INFO_ATTR_CHANGED);
1050
1051 /* Restore route, if needed. */
1052 if (CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED))
1053 bgp_info_restore(rn, tmp_ri);
1054
1055 /* Unintern existing, set to new. */
1056 bgp_attr_unintern(&tmp_ri->attr);
1057 tmp_ri->attr = attr_new;
1058 tmp_ri->uptime = bgp_clock();
1059 }
1060 }
1061 return 0;
1062 }
1063
1064 /* update evpn type-5 route entry */
1065 static int update_evpn_type5_route(struct bgp *bgp_vrf, struct prefix_evpn *evp,
1066 struct attr *src_attr)
1067 {
1068 afi_t afi = AFI_L2VPN;
1069 safi_t safi = SAFI_EVPN;
1070 struct attr attr;
1071 struct bgp_node *rn = NULL;
1072 struct bgp *bgp_def = NULL;
1073 int route_changed = 0;
1074
1075 bgp_def = bgp_get_default();
1076 if (!bgp_def)
1077 return 0;
1078
1079 /* Build path attribute for this route - use the source attr, if
1080 * present, else treat as locally originated.
1081 */
1082 if (src_attr)
1083 bgp_attr_dup(&attr, src_attr);
1084 else {
1085 memset(&attr, 0, sizeof(struct attr));
1086 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
1087 }
1088 /* Set nexthop to ourselves and fill in the Router MAC. */
1089 attr.nexthop = bgp_vrf->originator_ip;
1090 attr.mp_nexthop_global_in = bgp_vrf->originator_ip;
1091 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1092 memcpy(&attr.rmac, &bgp_vrf->rmac, sizeof(struct ethaddr));
1093
1094 /* Setup RT and encap extended community */
1095 build_evpn_type5_route_extcomm(bgp_vrf, &attr);
1096
1097 /* get the route node in global table */
1098 rn = bgp_afi_node_get(bgp_def->rib[afi][safi], afi, safi,
1099 (struct prefix *)evp, &bgp_vrf->vrf_prd);
1100 assert(rn);
1101
1102 /* create or update the route entry within the route node */
1103 update_evpn_type5_route_entry(bgp_def, bgp_vrf, afi, safi, rn, &attr,
1104 &route_changed);
1105
1106 /* schedule for processing and unlock node */
1107 if (route_changed) {
1108 bgp_process(bgp_def, rn, afi, safi);
1109 bgp_unlock_node(rn);
1110 }
1111
1112 /* uninten temporary */
1113 if (!src_attr)
1114 aspath_unintern(&attr.aspath);
1115 return 0;
1116 }
1117
1118 /*
1119 * Create or update EVPN route entry. This could be in the VNI route table
1120 * or the global route table.
1121 */
1122 static int update_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
1123 afi_t afi, safi_t safi, struct bgp_node *rn,
1124 struct attr *attr, int add, int vni_table,
1125 struct bgp_info **ri, uint8_t flags)
1126 {
1127 struct bgp_info *tmp_ri;
1128 struct bgp_info *local_ri, *remote_ri;
1129 struct attr *attr_new;
1130 mpls_label_t label[BGP_MAX_LABELS];
1131 uint32_t num_labels = 1;
1132 int route_change = 1;
1133 uint8_t sticky = 0;
1134 struct prefix_evpn *evp;
1135
1136 *ri = NULL;
1137 evp = (struct prefix_evpn *)&rn->p;
1138 memset(&label, 0, sizeof(label));
1139
1140 /* See if this is an update of an existing route, or a new add. Also,
1141 * identify if already known from remote, and if so, the one with the
1142 * highest sequence number; this is only when adding to the VNI routing
1143 * table.
1144 */
1145 local_ri = remote_ri = NULL;
1146 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
1147 if (tmp_ri->peer == bgp->peer_self
1148 && tmp_ri->type == ZEBRA_ROUTE_BGP
1149 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
1150 local_ri = tmp_ri;
1151 if (vni_table) {
1152 if (tmp_ri->type == ZEBRA_ROUTE_BGP
1153 && tmp_ri->sub_type == BGP_ROUTE_IMPORTED
1154 && CHECK_FLAG(tmp_ri->flags, BGP_INFO_VALID)) {
1155 if (!remote_ri)
1156 remote_ri = tmp_ri;
1157 else if (mac_mobility_seqnum(tmp_ri->attr)
1158 > mac_mobility_seqnum(remote_ri->attr))
1159 remote_ri = tmp_ri;
1160 }
1161 }
1162 }
1163
1164 /* If route doesn't exist already, create a new one, if told to.
1165 * Otherwise act based on whether the attributes of the route have
1166 * changed or not.
1167 */
1168 if (!local_ri && !add)
1169 return 0;
1170
1171 if (!local_ri) {
1172 /* When learnt locally for the first time but already known from
1173 * remote, we have to initiate appropriate MAC mobility steps.
1174 * This
1175 * is applicable when updating the VNI routing table.
1176 * We need to skip mobility steps for g/w macs (local mac on g/w
1177 * SVI) advertised in EVPN.
1178 * This will ensure that local routes are preferred for g/w macs
1179 */
1180 if (remote_ri && !CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW)) {
1181 uint32_t cur_seqnum;
1182
1183 /* Add MM extended community to route. */
1184 cur_seqnum = mac_mobility_seqnum(remote_ri->attr);
1185 add_mac_mobility_to_attr(cur_seqnum + 1, attr);
1186 }
1187
1188 /* Add (or update) attribute to hash. */
1189 attr_new = bgp_attr_intern(attr);
1190
1191 /* Extract MAC mobility sequence number, if any. */
1192 attr_new->mm_seqnum =
1193 bgp_attr_mac_mobility_seqnum(attr_new, &sticky);
1194 attr_new->sticky = sticky;
1195
1196 /* Create new route with its attribute. */
1197 tmp_ri = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0,
1198 bgp->peer_self, attr_new, rn);
1199 SET_FLAG(tmp_ri->flags, BGP_INFO_VALID);
1200 bgp_info_extra_get(tmp_ri);
1201
1202 /* The VNI goes into the 'label' field of the route */
1203 vni2label(vpn->vni, &label[0]);
1204
1205 /* Type-2 routes may carry a second VNI - the L3-VNI.
1206 * Only attach second label if we are advertising two labels for
1207 * type-2 routes.
1208 */
1209 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
1210 && CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)) {
1211 vni_t l3vni;
1212
1213 l3vni = bgpevpn_get_l3vni(vpn);
1214 if (l3vni) {
1215 vni2label(l3vni, &label[1]);
1216 num_labels++;
1217 }
1218 }
1219
1220 memcpy(&tmp_ri->extra->label, label, sizeof(label));
1221 tmp_ri->extra->num_labels = num_labels;
1222 bgp_info_add(rn, tmp_ri);
1223 } else {
1224 tmp_ri = local_ri;
1225 if (attrhash_cmp(tmp_ri->attr, attr)
1226 && !CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED))
1227 route_change = 0;
1228 else {
1229 /*
1230 * The attributes have changed, type-2 routes needs to
1231 * be advertised with right labels.
1232 */
1233 vni2label(vpn->vni, &label[0]);
1234 if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
1235 && CHECK_FLAG(vpn->flags,
1236 VNI_FLAG_USE_TWO_LABELS)) {
1237 vni_t l3vni;
1238
1239 l3vni = bgpevpn_get_l3vni(vpn);
1240 if (l3vni) {
1241 vni2label(l3vni, &label[1]);
1242 num_labels++;
1243 }
1244 }
1245 memcpy(&tmp_ri->extra->label, label, sizeof(label));
1246 tmp_ri->extra->num_labels = num_labels;
1247
1248 /* The attribute has changed. */
1249 /* Add (or update) attribute to hash. */
1250 attr_new = bgp_attr_intern(attr);
1251 bgp_info_set_flag(rn, tmp_ri, BGP_INFO_ATTR_CHANGED);
1252
1253 /* Restore route, if needed. */
1254 if (CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED))
1255 bgp_info_restore(rn, tmp_ri);
1256
1257 /* Unintern existing, set to new. */
1258 bgp_attr_unintern(&tmp_ri->attr);
1259 tmp_ri->attr = attr_new;
1260 tmp_ri->uptime = bgp_clock();
1261 }
1262 }
1263
1264 /* Return back the route entry. */
1265 *ri = tmp_ri;
1266 return route_change;
1267 }
1268
1269 /*
1270 * Create or update EVPN route (of type based on prefix) for specified VNI
1271 * and schedule for processing.
1272 */
1273 static int update_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
1274 struct prefix_evpn *p, uint8_t flags)
1275 {
1276 struct bgp_node *rn;
1277 struct attr attr;
1278 struct attr *attr_new;
1279 int add_l3_ecomm = 0;
1280 struct bgp_info *ri;
1281 afi_t afi = AFI_L2VPN;
1282 safi_t safi = SAFI_EVPN;
1283 int route_change;
1284
1285 memset(&attr, 0, sizeof(struct attr));
1286
1287 /* Build path-attribute for this route. */
1288 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
1289 attr.nexthop = vpn->originator_ip;
1290 attr.mp_nexthop_global_in = vpn->originator_ip;
1291 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1292 attr.sticky = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY) ? 1 : 0;
1293 attr.default_gw = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW) ? 1 : 0;
1294
1295 /* PMSI is only needed for type-3 routes */
1296 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE)
1297 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL);
1298
1299 /* router mac is only needed for type-2 routes here. */
1300 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
1301 bgpevpn_get_rmac(vpn, &attr.rmac);
1302 vni2label(vpn->vni, &(attr.label));
1303
1304 /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're
1305 * IPv4 or IPv6 global addresses and we're advertising L3VNI with
1306 * these routes.
1307 */
1308 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE &&
1309 (is_evpn_prefix_ipaddr_v4(p) ||
1310 !IN6_IS_ADDR_LINKLOCAL(&p->prefix.macip_addr.ip.ipaddr_v6)) &&
1311 CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS))
1312 add_l3_ecomm = 1;
1313
1314 /* Set up extended community. */
1315 build_evpn_route_extcomm(vpn, &attr, add_l3_ecomm);
1316
1317 /* First, create (or fetch) route node within the VNI. */
1318 /* NOTE: There is no RD here. */
1319 rn = bgp_node_get(vpn->route_table, (struct prefix *)p);
1320
1321 /* Create or update route entry. */
1322 route_change = update_evpn_route_entry(bgp, vpn, afi, safi, rn, &attr,
1323 1, 1, &ri, flags);
1324 assert(ri);
1325 attr_new = ri->attr;
1326
1327 /* Perform route selection; this is just to set the flags correctly
1328 * as local route in the VNI always wins.
1329 */
1330 evpn_route_select_install(bgp, vpn, rn);
1331 bgp_unlock_node(rn);
1332
1333 /* If this is a new route or some attribute has changed, export the
1334 * route to the global table. The route will be advertised to peers
1335 * from there. Note that this table is a 2-level tree (RD-level +
1336 * Prefix-level) similar to L3VPN routes.
1337 */
1338 if (route_change) {
1339 struct bgp_info *global_ri;
1340
1341 rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
1342 (struct prefix *)p, &vpn->prd);
1343 update_evpn_route_entry(bgp, vpn, afi, safi, rn, attr_new, 1, 0,
1344 &global_ri, flags);
1345
1346 /* Schedule for processing and unlock node. */
1347 bgp_process(bgp, rn, afi, safi);
1348 bgp_unlock_node(rn);
1349 }
1350
1351 /* Unintern temporary. */
1352 aspath_unintern(&attr.aspath);
1353
1354 return 0;
1355 }
1356
1357 /* Delete EVPN type5 route entry from global table */
1358 static void delete_evpn_type5_route_entry(struct bgp *bgp_def,
1359 struct bgp *bgp_vrf, afi_t afi,
1360 safi_t safi, struct bgp_node *rn,
1361 struct bgp_info **ri)
1362 {
1363 struct bgp_info *tmp_ri = NULL;
1364
1365 *ri = NULL;
1366
1367 /* find the matching route entry */
1368 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next)
1369 if (tmp_ri->peer == bgp_def->peer_self
1370 && tmp_ri->type == ZEBRA_ROUTE_BGP
1371 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
1372 break;
1373
1374 *ri = tmp_ri;
1375
1376 /* Mark route for delete. */
1377 if (tmp_ri)
1378 bgp_info_delete(rn, tmp_ri);
1379 }
1380
1381 /* Delete EVPN type5 route */
1382 static int delete_evpn_type5_route(struct bgp *bgp_vrf, struct prefix_evpn *evp)
1383 {
1384 afi_t afi = AFI_L2VPN;
1385 safi_t safi = SAFI_EVPN;
1386 struct bgp_node *rn = NULL;
1387 struct bgp_info *ri = NULL;
1388 struct bgp *bgp_def = NULL; /* default bgp instance */
1389
1390 bgp_def = bgp_get_default();
1391 if (!bgp_def)
1392 return 0;
1393
1394 /* locate the global route entry for this type-5 prefix */
1395 rn = bgp_afi_node_lookup(bgp_def->rib[afi][safi], afi, safi,
1396 (struct prefix *)evp, &bgp_vrf->vrf_prd);
1397 if (!rn)
1398 return 0;
1399
1400 delete_evpn_type5_route_entry(bgp_def, bgp_vrf, afi, safi, rn, &ri);
1401 if (ri)
1402 bgp_process(bgp_def, rn, afi, safi);
1403 bgp_unlock_node(rn);
1404 return 0;
1405 }
1406
1407 /*
1408 * Delete EVPN route entry. This could be in the VNI route table
1409 * or the global route table.
1410 */
1411 static void delete_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
1412 afi_t afi, safi_t safi, struct bgp_node *rn,
1413 struct bgp_info **ri)
1414 {
1415 struct bgp_info *tmp_ri;
1416
1417 *ri = NULL;
1418
1419 /* Now, find matching route. */
1420 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next)
1421 if (tmp_ri->peer == bgp->peer_self
1422 && tmp_ri->type == ZEBRA_ROUTE_BGP
1423 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
1424 break;
1425
1426 *ri = tmp_ri;
1427
1428 /* Mark route for delete. */
1429 if (tmp_ri)
1430 bgp_info_delete(rn, tmp_ri);
1431 }
1432
1433 /*
1434 * Delete EVPN route (of type based on prefix) for specified VNI and
1435 * schedule for processing.
1436 */
1437 static int delete_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
1438 struct prefix_evpn *p)
1439 {
1440 struct bgp_node *rn, *global_rn;
1441 struct bgp_info *ri;
1442 afi_t afi = AFI_L2VPN;
1443 safi_t safi = SAFI_EVPN;
1444
1445 /* First, locate the route node within the VNI. If it doesn't exist,
1446 * there
1447 * is nothing further to do.
1448 */
1449 /* NOTE: There is no RD here. */
1450 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
1451 if (!rn)
1452 return 0;
1453
1454 /* Next, locate route node in the global EVPN routing table. Note that
1455 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1456 * L3VPN routes.
1457 */
1458 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
1459 (struct prefix *)p, &vpn->prd);
1460 if (global_rn) {
1461 /* Delete route entry in the global EVPN table. */
1462 delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);
1463
1464 /* Schedule for processing - withdraws to peers happen from
1465 * this table.
1466 */
1467 if (ri)
1468 bgp_process(bgp, global_rn, afi, safi);
1469 bgp_unlock_node(global_rn);
1470 }
1471
1472 /* Delete route entry in the VNI route table. This can just be removed.
1473 */
1474 delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);
1475 if (ri)
1476 bgp_info_reap(rn, ri);
1477 bgp_unlock_node(rn);
1478
1479 return 0;
1480 }
1481
1482 /*
1483 * Update all type-2 (MACIP) local routes for this VNI - these should also
1484 * be scheduled for advertise to peers.
1485 */
1486 static int update_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
1487 {
1488 afi_t afi;
1489 safi_t safi;
1490 struct bgp_node *rn;
1491 struct bgp_info *ri;
1492 struct attr attr;
1493 struct attr attr_sticky;
1494 struct attr attr_def_gw;
1495 struct attr attr_ip6_ll;
1496 struct attr *attr_new;
1497 int add_l3_ecomm = 0;
1498
1499 afi = AFI_L2VPN;
1500 safi = SAFI_EVPN;
1501 memset(&attr, 0, sizeof(struct attr));
1502 memset(&attr_sticky, 0, sizeof(struct attr));
1503 memset(&attr_def_gw, 0, sizeof(struct attr));
1504 memset(&attr_ip6_ll, 0, sizeof(struct attr));
1505
1506 /* Build path-attribute - multiple type-2 routes for this VNI will share
1507 * the same path attribute, but we need separate structures for sticky
1508 * MACs, default gateway and IPv6 link-local addresses (no L3 RT/RMAC).
1509 */
1510 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
1511 bgp_attr_default_set(&attr_sticky, BGP_ORIGIN_IGP);
1512 bgp_attr_default_set(&attr_def_gw, BGP_ORIGIN_IGP);
1513 attr.nexthop = vpn->originator_ip;
1514 attr.mp_nexthop_global_in = vpn->originator_ip;
1515 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1516 bgpevpn_get_rmac(vpn, &attr.rmac);
1517 attr_sticky.nexthop = vpn->originator_ip;
1518 attr_sticky.mp_nexthop_global_in = vpn->originator_ip;
1519 attr_sticky.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1520 attr_sticky.sticky = 1;
1521 bgpevpn_get_rmac(vpn, &attr_sticky.rmac);
1522 attr_def_gw.nexthop = vpn->originator_ip;
1523 attr_def_gw.mp_nexthop_global_in = vpn->originator_ip;
1524 attr_def_gw.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1525 attr_def_gw.default_gw = 1;
1526 bgpevpn_get_rmac(vpn, &attr_def_gw.rmac);
1527 bgp_attr_default_set(&attr_ip6_ll, BGP_ORIGIN_IGP);
1528 attr_ip6_ll.nexthop = vpn->originator_ip;
1529 attr_ip6_ll.mp_nexthop_global_in = vpn->originator_ip;
1530 attr_ip6_ll.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1531
1532 /* Add L3 VNI RTs and RMAC for non IPv6 link-local attributes if
1533 * using L3 VNI for type-2 routes also.
1534 */
1535 if (CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS))
1536 add_l3_ecomm = 1;
1537
1538 build_evpn_route_extcomm(vpn, &attr, add_l3_ecomm);
1539 build_evpn_route_extcomm(vpn, &attr_sticky, add_l3_ecomm);
1540 build_evpn_route_extcomm(vpn, &attr_def_gw, add_l3_ecomm);
1541 build_evpn_route_extcomm(vpn, &attr_ip6_ll, 0);
1542
1543 /* Walk this VNI's route table and update local type-2 routes. For any
1544 * routes updated, update corresponding entry in the global table too.
1545 */
1546 for (rn = bgp_table_top(vpn->route_table); rn;
1547 rn = bgp_route_next(rn)) {
1548 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
1549 struct bgp_node *rd_rn;
1550 struct bgp_info *global_ri;
1551
1552 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1553 continue;
1554
1555 if (is_evpn_prefix_ipaddr_v6(evp) &&
1556 IN6_IS_ADDR_LINKLOCAL(&evp->prefix.macip_addr.ip.ipaddr_v6))
1557 update_evpn_route_entry(bgp, vpn, afi, safi, rn,
1558 &attr_ip6_ll, 0, 1, &ri, 0);
1559 else {
1560 if (evpn_route_is_sticky(bgp, rn))
1561 update_evpn_route_entry(bgp, vpn, afi, safi, rn,
1562 &attr_sticky, 0, 1, &ri,
1563 0);
1564 else if (evpn_route_is_def_gw(bgp, rn))
1565 update_evpn_route_entry(bgp, vpn, afi, safi, rn,
1566 &attr_def_gw, 0, 1, &ri,
1567 0);
1568 else
1569 update_evpn_route_entry(bgp, vpn, afi, safi, rn,
1570 &attr, 0, 1, &ri, 0);
1571 }
1572
1573 /* If a local route exists for this prefix, we need to update
1574 * the global routing table too.
1575 */
1576 if (!ri)
1577 continue;
1578
1579 /* Perform route selection; this is just to set the flags
1580 * correctly
1581 * as local route in the VNI always wins.
1582 */
1583 evpn_route_select_install(bgp, vpn, rn);
1584
1585 attr_new = ri->attr;
1586
1587 /* Update route in global routing table. */
1588 rd_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
1589 (struct prefix *)evp, &vpn->prd);
1590 assert(rd_rn);
1591 update_evpn_route_entry(bgp, vpn, afi, safi, rd_rn, attr_new, 0,
1592 0, &global_ri, 0);
1593
1594 /* Schedule for processing and unlock node. */
1595 bgp_process(bgp, rd_rn, afi, safi);
1596 bgp_unlock_node(rd_rn);
1597 }
1598
1599 /* Unintern temporary. */
1600 aspath_unintern(&attr.aspath);
1601 aspath_unintern(&attr_sticky.aspath);
1602 aspath_unintern(&attr_def_gw.aspath);
1603 aspath_unintern(&attr_ip6_ll.aspath);
1604
1605 return 0;
1606 }
1607
1608 /*
1609 * Delete all type-2 (MACIP) local routes for this VNI - only from the
1610 * global routing table. These are also scheduled for withdraw from peers.
1611 */
1612 static int delete_global_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
1613 {
1614 afi_t afi;
1615 safi_t safi;
1616 struct bgp_node *rdrn, *rn;
1617 struct bgp_table *table;
1618 struct bgp_info *ri;
1619
1620 afi = AFI_L2VPN;
1621 safi = SAFI_EVPN;
1622
1623 rdrn = bgp_node_lookup(bgp->rib[afi][safi], (struct prefix *)&vpn->prd);
1624 if (rdrn && rdrn->info) {
1625 table = (struct bgp_table *)rdrn->info;
1626 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
1627 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
1628
1629 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1630 continue;
1631
1632 delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);
1633 if (ri)
1634 bgp_process(bgp, rn, afi, safi);
1635 }
1636 }
1637
1638 /* Unlock RD node. */
1639 if (rdrn)
1640 bgp_unlock_node(rdrn);
1641
1642 return 0;
1643 }
1644
1645 /*
1646 * Delete all type-2 (MACIP) local routes for this VNI - from the global
1647 * table as well as the per-VNI route table.
1648 */
1649 static int delete_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
1650 {
1651 afi_t afi;
1652 safi_t safi;
1653 struct bgp_node *rn;
1654 struct bgp_info *ri;
1655
1656 afi = AFI_L2VPN;
1657 safi = SAFI_EVPN;
1658
1659 /* First, walk the global route table for this VNI's type-2 local
1660 * routes.
1661 * EVPN routes are a 2-level table, first get the RD table.
1662 */
1663 delete_global_type2_routes(bgp, vpn);
1664
1665 /* Next, walk this VNI's route table and delete local type-2 routes. */
1666 for (rn = bgp_table_top(vpn->route_table); rn;
1667 rn = bgp_route_next(rn)) {
1668 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
1669
1670 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1671 continue;
1672
1673 delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);
1674
1675 /* Route entry in local table gets deleted immediately. */
1676 if (ri)
1677 bgp_info_reap(rn, ri);
1678 }
1679
1680 return 0;
1681 }
1682
1683 /*
1684 * Delete all routes in the per-VNI route table.
1685 */
1686 static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
1687 {
1688 struct bgp_node *rn;
1689 struct bgp_info *ri, *nextri;
1690
1691 /* Walk this VNI's route table and delete all routes. */
1692 for (rn = bgp_table_top(vpn->route_table); rn;
1693 rn = bgp_route_next(rn)) {
1694 for (ri = rn->info; (ri != NULL) && (nextri = ri->next, 1);
1695 ri = nextri) {
1696 bgp_info_delete(rn, ri);
1697 bgp_info_reap(rn, ri);
1698 }
1699 }
1700
1701 return 0;
1702 }
1703
1704 /*
1705 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
1706 * export RT getting modified or change to tunnel IP. Note that these
1707 * situations need the route in the per-VNI table as well as the global
1708 * table to be updated (as attributes change).
1709 */
1710 static int update_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
1711 {
1712 int ret;
1713 struct prefix_evpn p;
1714
1715 /* Update and advertise the type-3 route (only one) followed by the
1716 * locally learnt type-2 routes (MACIP) - for this VNI.
1717 */
1718 build_evpn_type3_prefix(&p, vpn->originator_ip);
1719 ret = update_evpn_route(bgp, vpn, &p, 0);
1720 if (ret)
1721 return ret;
1722
1723 return update_all_type2_routes(bgp, vpn);
1724 }
1725
1726 /*
1727 * Delete (and withdraw) local routes for specified VNI from the global
1728 * table and per-VNI table. After this, remove all other routes from
1729 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
1730 * (advertise-all-vni) being disabled.
1731 */
1732 static int delete_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
1733 {
1734 int ret;
1735 struct prefix_evpn p;
1736
1737 /* Delete and withdraw locally learnt type-2 routes (MACIP)
1738 * followed by type-3 routes (only one) - for this VNI.
1739 */
1740 ret = delete_all_type2_routes(bgp, vpn);
1741 if (ret)
1742 return ret;
1743
1744 build_evpn_type3_prefix(&p, vpn->originator_ip);
1745 ret = delete_evpn_route(bgp, vpn, &p);
1746 if (ret)
1747 return ret;
1748
1749 /* Delete all routes from the per-VNI table. */
1750 return delete_all_vni_routes(bgp, vpn);
1751 }
1752
1753 /*
1754 * There is a tunnel endpoint IP address change for this VNI, delete
1755 * prior type-3 route (if needed) and update.
1756 * Note: Route re-advertisement happens elsewhere after other processing
1757 * other changes.
1758 */
1759 static int handle_tunnel_ip_change(struct bgp *bgp, struct bgpevpn *vpn,
1760 struct in_addr originator_ip)
1761 {
1762 struct prefix_evpn p;
1763
1764 /* If VNI is not live, we only need to update the originator ip */
1765 if (!is_vni_live(vpn)) {
1766 vpn->originator_ip = originator_ip;
1767 return 0;
1768 }
1769
1770 /* Update the tunnel-ip hash */
1771 bgp_tip_del(bgp, &vpn->originator_ip);
1772 bgp_tip_add(bgp, &originator_ip);
1773
1774 /* filter routes as martian nexthop db has changed */
1775 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
1776
1777 /* Need to withdraw type-3 route as the originator IP is part
1778 * of the key.
1779 */
1780 build_evpn_type3_prefix(&p, vpn->originator_ip);
1781 delete_evpn_route(bgp, vpn, &p);
1782
1783 /* Update the tunnel IP and re-advertise all routes for this VNI. */
1784 vpn->originator_ip = originator_ip;
1785 return 0;
1786 }
1787
1788 /*
1789 * Install route entry into the VRF routing table and invoke route selection.
1790 */
1791 static int install_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
1792 struct prefix_evpn *evp,
1793 struct bgp_info *parent_ri)
1794 {
1795 struct bgp_node *rn;
1796 struct bgp_info *ri;
1797 struct attr attr;
1798 struct attr *attr_new;
1799 int ret = 0;
1800 struct prefix p;
1801 struct prefix *pp = &p;
1802 afi_t afi = 0;
1803 safi_t safi = 0;
1804 char buf[PREFIX_STRLEN];
1805 char buf1[PREFIX_STRLEN];
1806
1807 memset(pp, 0, sizeof(struct prefix));
1808 ip_prefix_from_evpn_prefix(evp, pp);
1809
1810 if (bgp_debug_zebra(NULL)) {
1811 zlog_debug(
1812 "installing evpn prefix %s as ip prefix %s in vrf %s",
1813 prefix2str(evp, buf, sizeof(buf)),
1814 prefix2str(pp, buf1, sizeof(buf)),
1815 vrf_id_to_name(bgp_vrf->vrf_id));
1816 }
1817
1818 /* Create (or fetch) route within the VRF. */
1819 /* NOTE: There is no RD here. */
1820 if (is_evpn_prefix_ipaddr_v4(evp)) {
1821 afi = AFI_IP;
1822 safi = SAFI_UNICAST;
1823 rn = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
1824 } else if (is_evpn_prefix_ipaddr_v6(evp)) {
1825 afi = AFI_IP6;
1826 safi = SAFI_UNICAST;
1827 rn = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
1828 } else
1829 return 0;
1830
1831 /* EVPN routes currently only support a IPv4 next hop which corresponds
1832 * to the remote VTEP. When importing into a VRF, if it is IPv6 host
1833 * or prefix route, we have to convert the next hop to an IPv4-mapped
1834 * address for the rest of the code to flow through. In the case of IPv4,
1835 * make sure to set the flag for next hop attribute.
1836 */
1837 bgp_attr_dup(&attr, parent_ri->attr);
1838 if (afi == AFI_IP6)
1839 evpn_convert_nexthop_to_ipv6(&attr);
1840 else
1841 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP);
1842
1843 /* Check if route entry is already present. */
1844 for (ri = rn->info; ri; ri = ri->next)
1845 if (ri->extra
1846 && (struct bgp_info *)ri->extra->parent == parent_ri)
1847 break;
1848
1849 if (!ri) {
1850 /* Add (or update) attribute to hash. */
1851 attr_new = bgp_attr_intern(&attr);
1852
1853 /* Create new route with its attribute. */
1854 ri = info_make(parent_ri->type, BGP_ROUTE_IMPORTED, 0,
1855 parent_ri->peer, attr_new, rn);
1856 SET_FLAG(ri->flags, BGP_INFO_VALID);
1857 bgp_info_extra_get(ri);
1858 ri->extra->parent = bgp_info_lock(parent_ri);
1859 if (parent_ri->extra) {
1860 memcpy(&ri->extra->label, &parent_ri->extra->label,
1861 sizeof(ri->extra->label));
1862 ri->extra->num_labels = parent_ri->extra->num_labels;
1863 }
1864 bgp_info_add(rn, ri);
1865 } else {
1866 if (attrhash_cmp(ri->attr, &attr)
1867 && !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) {
1868 bgp_unlock_node(rn);
1869 return 0;
1870 }
1871 /* The attribute has changed. */
1872 /* Add (or update) attribute to hash. */
1873 attr_new = bgp_attr_intern(&attr);
1874
1875 /* Restore route, if needed. */
1876 if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
1877 bgp_info_restore(rn, ri);
1878
1879 /* Mark if nexthop has changed. */
1880 if ((afi == AFI_IP &&
1881 !IPV4_ADDR_SAME(&ri->attr->nexthop, &attr_new->nexthop)) ||
1882 (afi == AFI_IP6 &&
1883 !IPV6_ADDR_SAME(&ri->attr->mp_nexthop_global,
1884 &attr_new->mp_nexthop_global)))
1885 SET_FLAG(ri->flags, BGP_INFO_IGP_CHANGED);
1886
1887 /* Unintern existing, set to new. */
1888 bgp_attr_unintern(&ri->attr);
1889 ri->attr = attr_new;
1890 ri->uptime = bgp_clock();
1891 }
1892
1893 /* Perform route selection and update zebra, if required. */
1894 bgp_process(bgp_vrf, rn, afi, safi);
1895
1896 return ret;
1897 }
1898
1899 /*
1900 * Install route entry into the VNI routing table and invoke route selection.
1901 */
1902 static int install_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
1903 struct prefix_evpn *p,
1904 struct bgp_info *parent_ri)
1905 {
1906 struct bgp_node *rn;
1907 struct bgp_info *ri;
1908 struct attr *attr_new;
1909 int ret;
1910
1911 /* Create (or fetch) route within the VNI. */
1912 /* NOTE: There is no RD here. */
1913 rn = bgp_node_get(vpn->route_table, (struct prefix *)p);
1914
1915 /* Check if route entry is already present. */
1916 for (ri = rn->info; ri; ri = ri->next)
1917 if (ri->extra
1918 && (struct bgp_info *)ri->extra->parent == parent_ri)
1919 break;
1920
1921 if (!ri) {
1922 /* Add (or update) attribute to hash. */
1923 attr_new = bgp_attr_intern(parent_ri->attr);
1924
1925 /* Create new route with its attribute. */
1926 ri = info_make(parent_ri->type, BGP_ROUTE_IMPORTED, 0,
1927 parent_ri->peer, attr_new, rn);
1928 SET_FLAG(ri->flags, BGP_INFO_VALID);
1929 bgp_info_extra_get(ri);
1930 ri->extra->parent = bgp_info_lock(parent_ri);
1931 if (parent_ri->extra) {
1932 memcpy(&ri->extra->label, &parent_ri->extra->label,
1933 sizeof(ri->extra->label));
1934 ri->extra->num_labels = parent_ri->extra->num_labels;
1935 }
1936 bgp_info_add(rn, ri);
1937 } else {
1938 if (attrhash_cmp(ri->attr, parent_ri->attr)
1939 && !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) {
1940 bgp_unlock_node(rn);
1941 return 0;
1942 }
1943 /* The attribute has changed. */
1944 /* Add (or update) attribute to hash. */
1945 attr_new = bgp_attr_intern(parent_ri->attr);
1946
1947 /* Restore route, if needed. */
1948 if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
1949 bgp_info_restore(rn, ri);
1950
1951 /* Mark if nexthop has changed. */
1952 if (!IPV4_ADDR_SAME(&ri->attr->nexthop, &attr_new->nexthop))
1953 SET_FLAG(ri->flags, BGP_INFO_IGP_CHANGED);
1954
1955 /* Unintern existing, set to new. */
1956 bgp_attr_unintern(&ri->attr);
1957 ri->attr = attr_new;
1958 ri->uptime = bgp_clock();
1959 }
1960
1961 /* Perform route selection and update zebra, if required. */
1962 ret = evpn_route_select_install(bgp, vpn, rn);
1963
1964 return ret;
1965 }
1966
1967 /*
1968 * Uninstall route entry from the VRF routing table and send message
1969 * to zebra, if appropriate.
1970 */
1971 static int uninstall_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
1972 struct prefix_evpn *evp,
1973 struct bgp_info *parent_ri)
1974 {
1975 struct bgp_node *rn;
1976 struct bgp_info *ri;
1977 int ret = 0;
1978 struct prefix p;
1979 struct prefix *pp = &p;
1980 afi_t afi = 0;
1981 safi_t safi = 0;
1982 char buf[PREFIX_STRLEN];
1983 char buf1[PREFIX_STRLEN];
1984
1985 memset(pp, 0, sizeof(struct prefix));
1986 ip_prefix_from_evpn_prefix(evp, pp);
1987
1988 if (bgp_debug_zebra(NULL)) {
1989 zlog_debug(
1990 "uninstalling evpn prefix %s as ip prefix %s in vrf %s",
1991 prefix2str(evp, buf, sizeof(buf)),
1992 prefix2str(pp, buf1, sizeof(buf)),
1993 vrf_id_to_name(bgp_vrf->vrf_id));
1994 }
1995
1996 /* Locate route within the VRF. */
1997 /* NOTE: There is no RD here. */
1998 if (is_evpn_prefix_ipaddr_v4(evp)) {
1999 afi = AFI_IP;
2000 safi = SAFI_UNICAST;
2001 rn = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
2002 } else {
2003 afi = AFI_IP6;
2004 safi = SAFI_UNICAST;
2005 rn = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
2006 }
2007
2008 if (!rn)
2009 return 0;
2010
2011 /* Find matching route entry. */
2012 for (ri = rn->info; ri; ri = ri->next)
2013 if (ri->extra
2014 && (struct bgp_info *)ri->extra->parent == parent_ri)
2015 break;
2016
2017 if (!ri)
2018 return 0;
2019
2020 /* Mark entry for deletion */
2021 bgp_info_delete(rn, ri);
2022
2023 /* Perform route selection and update zebra, if required. */
2024 bgp_process(bgp_vrf, rn, afi, safi);
2025
2026 /* Unlock route node. */
2027 bgp_unlock_node(rn);
2028
2029 return ret;
2030 }
2031
2032 /*
2033 * Uninstall route entry from the VNI routing table and send message
2034 * to zebra, if appropriate.
2035 */
2036 static int uninstall_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
2037 struct prefix_evpn *p,
2038 struct bgp_info *parent_ri)
2039 {
2040 struct bgp_node *rn;
2041 struct bgp_info *ri;
2042 int ret;
2043
2044 /* Locate route within the VNI. */
2045 /* NOTE: There is no RD here. */
2046 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
2047 if (!rn)
2048 return 0;
2049
2050 /* Find matching route entry. */
2051 for (ri = rn->info; ri; ri = ri->next)
2052 if (ri->extra
2053 && (struct bgp_info *)ri->extra->parent == parent_ri)
2054 break;
2055
2056 if (!ri)
2057 return 0;
2058
2059 /* Mark entry for deletion */
2060 bgp_info_delete(rn, ri);
2061
2062 /* Perform route selection and update zebra, if required. */
2063 ret = evpn_route_select_install(bgp, vpn, rn);
2064
2065 /* Unlock route node. */
2066 bgp_unlock_node(rn);
2067
2068 return ret;
2069 }
2070
2071 /*
2072 * Given a route entry and a VRF, see if this route entry should be
2073 * imported into the VRF i.e., RTs match.
2074 */
2075 static int is_route_matching_for_vrf(struct bgp *bgp_vrf, struct bgp_info *ri)
2076 {
2077 struct attr *attr = ri->attr;
2078 struct ecommunity *ecom;
2079 int i;
2080
2081 assert(attr);
2082 /* Route should have valid RT to be even considered. */
2083 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
2084 return 0;
2085
2086 ecom = attr->ecommunity;
2087 if (!ecom || !ecom->size)
2088 return 0;
2089
2090 /* For each extended community RT, see if it matches this VNI. If any RT
2091 * matches, we're done.
2092 */
2093 for (i = 0; i < ecom->size; i++) {
2094 uint8_t *pnt;
2095 uint8_t type, sub_type;
2096 struct ecommunity_val *eval;
2097 struct ecommunity_val eval_tmp;
2098 struct vrf_irt_node *irt;
2099
2100 /* Only deal with RTs */
2101 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
2102 eval = (struct ecommunity_val *)(ecom->val
2103 + (i * ECOMMUNITY_SIZE));
2104 type = *pnt++;
2105 sub_type = *pnt++;
2106 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
2107 continue;
2108
2109 /* See if this RT matches specified VNIs import RTs */
2110 irt = lookup_vrf_import_rt(eval);
2111 if (irt && irt->vrfs)
2112 if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
2113 return 1;
2114
2115 /* Also check for non-exact match. In this, we mask out the AS
2116 * and
2117 * only check on the local-admin sub-field. This is to
2118 * facilitate using
2119 * VNI as the RT for EBGP peering too.
2120 */
2121 irt = NULL;
2122 if (type == ECOMMUNITY_ENCODE_AS
2123 || type == ECOMMUNITY_ENCODE_AS4
2124 || type == ECOMMUNITY_ENCODE_IP) {
2125 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
2126 mask_ecom_global_admin(&eval_tmp, eval);
2127 irt = lookup_vrf_import_rt(&eval_tmp);
2128 }
2129 if (irt && irt->vrfs)
2130 if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
2131 return 1;
2132 }
2133
2134 return 0;
2135 }
2136
2137 /*
2138 * Given a route entry and a VNI, see if this route entry should be
2139 * imported into the VNI i.e., RTs match.
2140 */
2141 static int is_route_matching_for_vni(struct bgp *bgp, struct bgpevpn *vpn,
2142 struct bgp_info *ri)
2143 {
2144 struct attr *attr = ri->attr;
2145 struct ecommunity *ecom;
2146 int i;
2147
2148 assert(attr);
2149 /* Route should have valid RT to be even considered. */
2150 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
2151 return 0;
2152
2153 ecom = attr->ecommunity;
2154 if (!ecom || !ecom->size)
2155 return 0;
2156
2157 /* For each extended community RT, see if it matches this VNI. If any RT
2158 * matches, we're done.
2159 */
2160 for (i = 0; i < ecom->size; i++) {
2161 uint8_t *pnt;
2162 uint8_t type, sub_type;
2163 struct ecommunity_val *eval;
2164 struct ecommunity_val eval_tmp;
2165 struct irt_node *irt;
2166
2167 /* Only deal with RTs */
2168 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
2169 eval = (struct ecommunity_val *)(ecom->val
2170 + (i * ECOMMUNITY_SIZE));
2171 type = *pnt++;
2172 sub_type = *pnt++;
2173 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
2174 continue;
2175
2176 /* See if this RT matches specified VNIs import RTs */
2177 irt = lookup_import_rt(bgp, eval);
2178 if (irt)
2179 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
2180 return 1;
2181
2182 /* Also check for non-exact match. In this, we mask out the AS
2183 * and
2184 * only check on the local-admin sub-field. This is to
2185 * facilitate using
2186 * VNI as the RT for EBGP peering too.
2187 */
2188 irt = NULL;
2189 if (type == ECOMMUNITY_ENCODE_AS
2190 || type == ECOMMUNITY_ENCODE_AS4
2191 || type == ECOMMUNITY_ENCODE_IP) {
2192 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
2193 mask_ecom_global_admin(&eval_tmp, eval);
2194 irt = lookup_import_rt(bgp, &eval_tmp);
2195 }
2196 if (irt)
2197 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
2198 return 1;
2199 }
2200
2201 return 0;
2202 }
2203
2204 /*
2205 * Install or uninstall mac-ip routes are appropriate for this
2206 * particular VRF.
2207 */
2208 static int install_uninstall_routes_for_vrf(struct bgp *bgp_vrf, int install)
2209 {
2210 afi_t afi;
2211 safi_t safi;
2212 struct bgp_node *rd_rn, *rn;
2213 struct bgp_table *table;
2214 struct bgp_info *ri;
2215 int ret;
2216 char buf[PREFIX_STRLEN];
2217 struct bgp *bgp_def = NULL;
2218
2219 afi = AFI_L2VPN;
2220 safi = SAFI_EVPN;
2221 bgp_def = bgp_get_default();
2222 if (!bgp_def)
2223 return -1;
2224
2225 /* Walk entire global routing table and evaluate routes which could be
2226 * imported into this VRF. Note that we need to loop through all global
2227 * routes to determine which route matches the import rt on vrf
2228 */
2229 for (rd_rn = bgp_table_top(bgp_def->rib[afi][safi]); rd_rn;
2230 rd_rn = bgp_route_next(rd_rn)) {
2231 table = (struct bgp_table *)(rd_rn->info);
2232 if (!table)
2233 continue;
2234
2235 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
2236 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
2237
2238 /* if not mac-ip route skip this route */
2239 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
2240 || evp->prefix.route_type
2241 == BGP_EVPN_IP_PREFIX_ROUTE))
2242 continue;
2243
2244 /* if not a mac+ip route skip this route */
2245 if (!(is_evpn_prefix_ipaddr_v4(evp)
2246 || is_evpn_prefix_ipaddr_v6(evp)))
2247 continue;
2248
2249 for (ri = rn->info; ri; ri = ri->next) {
2250 /* Consider "valid" remote routes applicable for
2251 * this VRF.
2252 */
2253 if (!(CHECK_FLAG(ri->flags, BGP_INFO_VALID)
2254 && ri->type == ZEBRA_ROUTE_BGP
2255 && ri->sub_type == BGP_ROUTE_NORMAL))
2256 continue;
2257
2258 if (is_route_matching_for_vrf(bgp_vrf, ri)) {
2259 if (install)
2260 ret = install_evpn_route_entry_in_vrf(
2261 bgp_vrf, evp, ri);
2262 else
2263 ret = uninstall_evpn_route_entry_in_vrf(
2264 bgp_vrf, evp, ri);
2265
2266 if (ret) {
2267 zlog_err(
2268 "Failed to %s EVPN %s route in VRF %s",
2269 install ? "install"
2270 : "uninstall",
2271 prefix2str(evp, buf,
2272 sizeof(buf)),
2273 vrf_id_to_name(
2274 bgp_vrf->vrf_id));
2275 return ret;
2276 }
2277 }
2278 }
2279 }
2280 }
2281
2282 return 0;
2283 }
2284
2285 /*
2286 * Install or uninstall routes of specified type that are appropriate for this
2287 * particular VNI.
2288 */
2289 static int install_uninstall_routes_for_vni(struct bgp *bgp,
2290 struct bgpevpn *vpn,
2291 bgp_evpn_route_type rtype,
2292 int install)
2293 {
2294 afi_t afi;
2295 safi_t safi;
2296 struct bgp_node *rd_rn, *rn;
2297 struct bgp_table *table;
2298 struct bgp_info *ri;
2299 int ret;
2300
2301 afi = AFI_L2VPN;
2302 safi = SAFI_EVPN;
2303
2304 /* Walk entire global routing table and evaluate routes which could be
2305 * imported into this VPN. Note that we cannot just look at the routes
2306 * for
2307 * the VNI's RD - remote routes applicable for this VNI could have any
2308 * RD.
2309 */
2310 /* EVPN routes are a 2-level table. */
2311 for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
2312 rd_rn = bgp_route_next(rd_rn)) {
2313 table = (struct bgp_table *)(rd_rn->info);
2314 if (!table)
2315 continue;
2316
2317 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
2318 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
2319
2320 if (evp->prefix.route_type != rtype)
2321 continue;
2322
2323 for (ri = rn->info; ri; ri = ri->next) {
2324 /* Consider "valid" remote routes applicable for
2325 * this VNI. */
2326 if (!(CHECK_FLAG(ri->flags, BGP_INFO_VALID)
2327 && ri->type == ZEBRA_ROUTE_BGP
2328 && ri->sub_type == BGP_ROUTE_NORMAL))
2329 continue;
2330
2331 if (is_route_matching_for_vni(bgp, vpn, ri)) {
2332 if (install)
2333 ret = install_evpn_route_entry(
2334 bgp, vpn, evp, ri);
2335 else
2336 ret = uninstall_evpn_route_entry(
2337 bgp, vpn, evp, ri);
2338
2339 if (ret) {
2340 zlog_err(
2341 "%u: Failed to %s EVPN %s route in VNI %u",
2342 bgp->vrf_id,
2343 install ? "install"
2344 : "uninstall",
2345 rtype == BGP_EVPN_MAC_IP_ROUTE
2346 ? "MACIP"
2347 : "IMET",
2348 vpn->vni);
2349 return ret;
2350 }
2351 }
2352 }
2353 }
2354 }
2355
2356 return 0;
2357 }
2358
2359 /* Install any existing remote routes applicable for this VRF into VRF RIB. This
2360 * is invoked upon l3vni-add or l3vni import rt change
2361 */
2362 static int install_routes_for_vrf(struct bgp *bgp_vrf)
2363 {
2364 install_uninstall_routes_for_vrf(bgp_vrf, 1);
2365 return 0;
2366 }
2367
2368 /*
2369 * Install any existing remote routes applicable for this VNI into its
2370 * routing table. This is invoked when a VNI becomes "live" or its Import
2371 * RT is changed.
2372 */
2373 static int install_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
2374 {
2375 int ret;
2376
2377 /* Install type-3 routes followed by type-2 routes - the ones applicable
2378 * for this VNI.
2379 */
2380 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
2381 1);
2382 if (ret)
2383 return ret;
2384
2385 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
2386 1);
2387 }
2388
2389 /* uninstall routes from l3vni vrf. */
2390 static int uninstall_routes_for_vrf(struct bgp *bgp_vrf)
2391 {
2392 install_uninstall_routes_for_vrf(bgp_vrf, 0);
2393 return 0;
2394 }
2395
2396 /*
2397 * Uninstall any existing remote routes for this VNI. One scenario in which
2398 * this is invoked is upon an import RT change.
2399 */
2400 static int uninstall_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
2401 {
2402 int ret;
2403
2404 /* Uninstall type-2 routes followed by type-3 routes - the ones
2405 * applicable
2406 * for this VNI.
2407 */
2408 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
2409 0);
2410 if (ret)
2411 return ret;
2412
2413 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
2414 0);
2415 }
2416
2417 /*
2418 * Install or uninstall route in matching VRFs (list).
2419 */
2420 static int install_uninstall_route_in_vrfs(struct bgp *bgp_def, afi_t afi,
2421 safi_t safi, struct prefix_evpn *evp,
2422 struct bgp_info *ri,
2423 struct list *vrfs, int install)
2424 {
2425 char buf[PREFIX2STR_BUFFER];
2426 struct bgp *bgp_vrf;
2427 struct listnode *node, *nnode;
2428
2429 /* Only type-2/type-5 routes go into a VRF */
2430 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
2431 || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
2432 return 0;
2433
2434 /* if it is type-2 route and not a mac+ip route skip this route */
2435 if ((evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
2436 && !(is_evpn_prefix_ipaddr_v4(evp)
2437 || is_evpn_prefix_ipaddr_v6(evp)))
2438 return 0;
2439
2440 for (ALL_LIST_ELEMENTS(vrfs, node, nnode, bgp_vrf)) {
2441 int ret;
2442
2443 if (install)
2444 ret = install_evpn_route_entry_in_vrf(bgp_vrf, evp, ri);
2445 else
2446 ret = uninstall_evpn_route_entry_in_vrf(bgp_vrf, evp,
2447 ri);
2448
2449 if (ret) {
2450 zlog_err("%u: Failed to %s prefix %s in VRF %s",
2451 bgp_def->vrf_id,
2452 install ? "install" : "uninstall",
2453 prefix2str(evp, buf, sizeof(buf)),
2454 vrf_id_to_name(bgp_vrf->vrf_id));
2455 return ret;
2456 }
2457 }
2458
2459 return 0;
2460 }
2461
2462 /*
2463 * Install or uninstall route in matching VNIs (list).
2464 */
2465 static int install_uninstall_route_in_vnis(struct bgp *bgp, afi_t afi,
2466 safi_t safi, struct prefix_evpn *evp,
2467 struct bgp_info *ri,
2468 struct list *vnis, int install)
2469 {
2470 struct bgpevpn *vpn;
2471 struct listnode *node, *nnode;
2472
2473 for (ALL_LIST_ELEMENTS(vnis, node, nnode, vpn)) {
2474 int ret;
2475
2476 if (!is_vni_live(vpn))
2477 continue;
2478
2479 if (install)
2480 ret = install_evpn_route_entry(bgp, vpn, evp, ri);
2481 else
2482 ret = uninstall_evpn_route_entry(bgp, vpn, evp, ri);
2483
2484 if (ret) {
2485 zlog_err("%u: Failed to %s EVPN %s route in VNI %u",
2486 bgp->vrf_id, install ? "install" : "uninstall",
2487 evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
2488 ? "MACIP"
2489 : "IMET",
2490 vpn->vni);
2491 return ret;
2492 }
2493 }
2494
2495 return 0;
2496 }
2497
2498 /*
2499 * Install or uninstall route for appropriate VNIs.
2500 */
2501 static int install_uninstall_evpn_route(struct bgp *bgp, afi_t afi, safi_t safi,
2502 struct prefix *p, struct bgp_info *ri,
2503 int import)
2504 {
2505 struct prefix_evpn *evp = (struct prefix_evpn *)p;
2506 struct attr *attr = ri->attr;
2507 struct ecommunity *ecom;
2508 int i;
2509
2510 assert(attr);
2511
2512 /* Only type-2 and type-3 and type-5 are supported currently */
2513 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
2514 || evp->prefix.route_type == BGP_EVPN_IMET_ROUTE
2515 || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE))
2516 return 0;
2517
2518 /* If we don't have Route Target, nothing much to do. */
2519 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
2520 return 0;
2521
2522 ecom = attr->ecommunity;
2523 if (!ecom || !ecom->size)
2524 return -1;
2525
2526 /* For each extended community RT, see which VNIs/VRFs match and import
2527 * the route into matching VNIs/VRFs.
2528 */
2529 for (i = 0; i < ecom->size; i++) {
2530 uint8_t *pnt;
2531 uint8_t type, sub_type;
2532 struct ecommunity_val *eval;
2533 struct ecommunity_val eval_tmp;
2534 struct irt_node *irt; /* import rt for l2vni */
2535 struct vrf_irt_node *vrf_irt; /* import rt for l3vni */
2536
2537 /* Only deal with RTs */
2538 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
2539 eval = (struct ecommunity_val *)(ecom->val
2540 + (i * ECOMMUNITY_SIZE));
2541 type = *pnt++;
2542 sub_type = *pnt++;
2543 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
2544 continue;
2545
2546 /* Import route into matching l2-vnis (type-2/type-3 routes go
2547 * into l2vni table)
2548 */
2549 irt = lookup_import_rt(bgp, eval);
2550 if (irt)
2551 install_uninstall_route_in_vnis(bgp, afi, safi, evp, ri,
2552 irt->vnis, import);
2553
2554 /* Import route into matching l3-vnis (type-2/type-5 routes go
2555 * into l3vni/vrf table)
2556 */
2557 vrf_irt = lookup_vrf_import_rt(eval);
2558 if (vrf_irt && vrf_irt->vrfs)
2559 install_uninstall_route_in_vrfs(bgp, afi, safi, evp, ri,
2560 vrf_irt->vrfs, import);
2561
2562 /* Also check for non-exact match. In this,
2563 * we mask out the AS and
2564 * only check on the local-admin sub-field.
2565 * This is to facilitate using
2566 * VNI as the RT for EBGP peering too.
2567 */
2568 irt = NULL;
2569 vrf_irt = NULL;
2570 if (type == ECOMMUNITY_ENCODE_AS
2571 || type == ECOMMUNITY_ENCODE_AS4
2572 || type == ECOMMUNITY_ENCODE_IP) {
2573 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
2574 mask_ecom_global_admin(&eval_tmp, eval);
2575 irt = lookup_import_rt(bgp, &eval_tmp);
2576 vrf_irt = lookup_vrf_import_rt(&eval_tmp);
2577 }
2578 if (irt)
2579 install_uninstall_route_in_vnis(bgp, afi, safi, evp, ri,
2580 irt->vnis, import);
2581 if (vrf_irt && vrf_irt->vrfs)
2582 install_uninstall_route_in_vrfs(bgp, afi, safi, evp, ri,
2583 vrf_irt->vrfs, import);
2584 }
2585
2586 return 0;
2587 }
2588
2589 /* delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5
2590 * routes */
2591 static void delete_withdraw_vrf_routes(struct bgp *bgp_vrf)
2592 {
2593 /* delete all ipv4 routes and withdraw from peers */
2594 if (advertise_type5_routes(bgp_vrf, AFI_IP))
2595 bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);
2596
2597 /* delete all ipv6 routes and withdraw from peers */
2598 if (advertise_type5_routes(bgp_vrf, AFI_IP6))
2599 bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
2600 }
2601
2602 /* update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5
2603 * routes */
2604 static void update_advertise_vrf_routes(struct bgp *bgp_vrf)
2605 {
2606 /* update all ipv4 routes */
2607 if (advertise_type5_routes(bgp_vrf, AFI_IP))
2608 bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST);
2609
2610 /* update all ipv6 routes */
2611 if (advertise_type5_routes(bgp_vrf, AFI_IP6))
2612 bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST);
2613 }
2614
2615 /*
2616 * update and advertise local routes for a VRF as type-5 routes.
2617 * This is invoked upon RD change for a VRF. Note taht the processing is only
2618 * done in the global route table using the routes which already exist in the
2619 * VRF routing table
2620 */
2621 static void update_router_id_vrf(struct bgp *bgp_vrf)
2622 {
2623 /* skip if the RD is configured */
2624 if (is_vrf_rd_configured(bgp_vrf))
2625 return;
2626
2627 /* derive the RD for the VRF based on new router-id */
2628 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
2629
2630 /* update advertise ipv4|ipv6 routes as type-5 routes */
2631 update_advertise_vrf_routes(bgp_vrf);
2632 }
2633
2634 /*
2635 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
2636 * This is invoked upon VRF RD change. The processing is done only from global
2637 * table.
2638 */
2639 static void withdraw_router_id_vrf(struct bgp *bgp_vrf)
2640 {
2641 /* skip if the RD is configured */
2642 if (is_vrf_rd_configured(bgp_vrf))
2643 return;
2644
2645 /* delete/withdraw ipv4|ipv6 routes as type-5 routes */
2646 delete_withdraw_vrf_routes(bgp_vrf);
2647 }
2648
2649 /*
2650 * Update and advertise local routes for a VNI. Invoked upon router-id
2651 * change. Note that the processing is done only on the global route table
2652 * using routes that already exist in the per-VNI table.
2653 */
2654 static int update_advertise_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
2655 {
2656 struct prefix_evpn p;
2657 struct bgp_node *rn, *global_rn;
2658 struct bgp_info *ri, *global_ri;
2659 struct attr *attr;
2660 afi_t afi = AFI_L2VPN;
2661 safi_t safi = SAFI_EVPN;
2662
2663 /* Locate type-3 route for VNI in the per-VNI table and use its
2664 * attributes to create and advertise the type-3 route for this VNI
2665 * in the global table.
2666 */
2667 build_evpn_type3_prefix(&p, vpn->originator_ip);
2668 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)&p);
2669 if (!rn) /* unexpected */
2670 return 0;
2671 for (ri = rn->info; ri; ri = ri->next)
2672 if (ri->peer == bgp->peer_self && ri->type == ZEBRA_ROUTE_BGP
2673 && ri->sub_type == BGP_ROUTE_STATIC)
2674 break;
2675 if (!ri) /* unexpected */
2676 return 0;
2677 attr = ri->attr;
2678
2679 global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
2680 (struct prefix *)&p, &vpn->prd);
2681 update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr, 1, 0, &ri,
2682 0);
2683
2684 /* Schedule for processing and unlock node. */
2685 bgp_process(bgp, global_rn, afi, safi);
2686 bgp_unlock_node(global_rn);
2687
2688 /* Now, walk this VNI's route table and use the route and its attribute
2689 * to create and schedule route in global table.
2690 */
2691 for (rn = bgp_table_top(vpn->route_table); rn;
2692 rn = bgp_route_next(rn)) {
2693 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
2694
2695 /* Identify MAC-IP local routes. */
2696 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2697 continue;
2698
2699 for (ri = rn->info; ri; ri = ri->next)
2700 if (ri->peer == bgp->peer_self
2701 && ri->type == ZEBRA_ROUTE_BGP
2702 && ri->sub_type == BGP_ROUTE_STATIC)
2703 break;
2704 if (!ri)
2705 continue;
2706
2707 /* Create route in global routing table using this route entry's
2708 * attribute.
2709 */
2710 attr = ri->attr;
2711 global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
2712 (struct prefix *)evp, &vpn->prd);
2713 assert(global_rn);
2714 update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr, 1,
2715 0, &global_ri, 0);
2716
2717 /* Schedule for processing and unlock node. */
2718 bgp_process(bgp, global_rn, afi, safi);
2719 bgp_unlock_node(global_rn);
2720 }
2721
2722 return 0;
2723 }
2724
2725 /*
2726 * Delete (and withdraw) local routes for a VNI - only from the global
2727 * table. Invoked upon router-id change.
2728 */
2729 static int delete_withdraw_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
2730 {
2731 int ret;
2732 struct prefix_evpn p;
2733 struct bgp_node *global_rn;
2734 struct bgp_info *ri;
2735 afi_t afi = AFI_L2VPN;
2736 safi_t safi = SAFI_EVPN;
2737
2738 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2739 * for this VNI - from the global table.
2740 */
2741 ret = delete_global_type2_routes(bgp, vpn);
2742 if (ret)
2743 return ret;
2744
2745 /* Remove type-3 route for this VNI from global table. */
2746 build_evpn_type3_prefix(&p, vpn->originator_ip);
2747 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
2748 (struct prefix *)&p, &vpn->prd);
2749 if (global_rn) {
2750 /* Delete route entry in the global EVPN table. */
2751 delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);
2752
2753 /* Schedule for processing - withdraws to peers happen from
2754 * this table.
2755 */
2756 if (ri)
2757 bgp_process(bgp, global_rn, afi, safi);
2758 bgp_unlock_node(global_rn);
2759 }
2760
2761 return 0;
2762 }
2763
2764 /*
2765 * Handle router-id change. Update and advertise local routes corresponding
2766 * to this VNI from peers. Note that this is invoked after updating the
2767 * router-id. The routes in the per-VNI table are used to create routes in
2768 * the global table and schedule them.
2769 */
2770 static void update_router_id_vni(struct hash_backet *backet, struct bgp *bgp)
2771 {
2772 struct bgpevpn *vpn;
2773
2774 vpn = (struct bgpevpn *)backet->data;
2775
2776 if (!vpn) {
2777 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__);
2778 return;
2779 }
2780
2781 /* Skip VNIs with configured RD. */
2782 if (is_rd_configured(vpn))
2783 return;
2784
2785 bgp_evpn_derive_auto_rd(bgp, vpn);
2786 update_advertise_vni_routes(bgp, vpn);
2787 }
2788
2789 /*
2790 * Handle router-id change. Delete and withdraw local routes corresponding
2791 * to this VNI from peers. Note that this is invoked prior to updating
2792 * the router-id and is done only on the global route table, the routes
2793 * are needed in the per-VNI table to re-advertise with new router id.
2794 */
2795 static void withdraw_router_id_vni(struct hash_backet *backet, struct bgp *bgp)
2796 {
2797 struct bgpevpn *vpn;
2798
2799 vpn = (struct bgpevpn *)backet->data;
2800
2801 if (!vpn) {
2802 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__);
2803 return;
2804 }
2805
2806 /* Skip VNIs with configured RD. */
2807 if (is_rd_configured(vpn))
2808 return;
2809
2810 delete_withdraw_vni_routes(bgp, vpn);
2811 }
2812
2813 /*
2814 * Process received EVPN type-2 route (advertise or withdraw).
2815 */
2816 static int process_type2_route(struct peer *peer, afi_t afi, safi_t safi,
2817 struct attr *attr, uint8_t *pfx, int psize,
2818 uint32_t addpath_id)
2819 {
2820 struct prefix_rd prd;
2821 struct prefix_evpn p;
2822 struct bgp_route_evpn evpn;
2823 uint8_t ipaddr_len;
2824 uint8_t macaddr_len;
2825 mpls_label_t label[BGP_MAX_LABELS]; /* holds the VNI(s) as in packet */
2826 uint32_t num_labels = 0;
2827 uint32_t eth_tag;
2828 int ret;
2829
2830 /* Type-2 route should be either 33, 37 or 49 bytes or an
2831 * additional 3 bytes if there is a second label (VNI):
2832 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
2833 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
2834 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
2835 */
2836 if (psize != 33 && psize != 37 && psize != 49 && psize != 36
2837 && psize != 40 && psize != 52) {
2838 zlog_err("%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
2839 peer->bgp->vrf_id, peer->host, psize);
2840 return -1;
2841 }
2842
2843 memset(&evpn, 0, sizeof(evpn));
2844
2845 /* Make prefix_rd */
2846 prd.family = AF_UNSPEC;
2847 prd.prefixlen = 64;
2848 memcpy(&prd.val, pfx, 8);
2849 pfx += 8;
2850
2851 /* Make EVPN prefix. */
2852 memset(&p, 0, sizeof(struct prefix_evpn));
2853 p.family = AF_EVPN;
2854 p.prefixlen = EVPN_TYPE_2_ROUTE_PREFIXLEN;
2855 p.prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
2856
2857 /* Copy Ethernet Seg Identifier */
2858 memcpy(&evpn.eth_s_id.val, pfx, ESI_LEN);
2859 pfx += ESI_LEN;
2860
2861 /* Copy Ethernet Tag */
2862 memcpy(&eth_tag, pfx, 4);
2863 p.prefix.macip_addr.eth_tag = ntohl(eth_tag);
2864 pfx += 4;
2865
2866 /* Get the MAC Addr len */
2867 macaddr_len = *pfx++;
2868
2869 /* Get the MAC Addr */
2870 if (macaddr_len == (ETH_ALEN * 8)) {
2871 memcpy(&p.prefix.macip_addr.mac.octet, pfx, ETH_ALEN);
2872 pfx += ETH_ALEN;
2873 } else {
2874 zlog_err(
2875 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
2876 peer->bgp->vrf_id, peer->host, macaddr_len);
2877 return -1;
2878 }
2879
2880
2881 /* Get the IP. */
2882 ipaddr_len = *pfx++;
2883 if (ipaddr_len != 0 && ipaddr_len != IPV4_MAX_BITLEN
2884 && ipaddr_len != IPV6_MAX_BITLEN) {
2885 zlog_err(
2886 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
2887 peer->bgp->vrf_id, peer->host, ipaddr_len);
2888 return -1;
2889 }
2890
2891 if (ipaddr_len) {
2892 ipaddr_len /= 8; /* Convert to bytes. */
2893 p.prefix.macip_addr.ip.ipa_type = (ipaddr_len == IPV4_MAX_BYTELEN)
2894 ? IPADDR_V4
2895 : IPADDR_V6;
2896 memcpy(&p.prefix.macip_addr.ip.ip.addr, pfx, ipaddr_len);
2897 }
2898 pfx += ipaddr_len;
2899
2900 /* Get the VNI(s). Stored as bytes here. */
2901 num_labels++;
2902 memset(label, 0, sizeof(label));
2903 memcpy(&label[0], pfx, BGP_LABEL_BYTES);
2904 pfx += BGP_LABEL_BYTES;
2905 psize -= (33 + ipaddr_len);
2906 /* Do we have a second VNI? */
2907 if (psize) {
2908 num_labels++;
2909 memcpy(&label[1], pfx, BGP_LABEL_BYTES);
2910 /*
2911 * If in future, we are required to access additional fields,
2912 * we MUST increment pfx by BGP_LABEL_BYTES in before reading
2913 * the next field
2914 */
2915 }
2916
2917 /* Process the route. */
2918 if (attr)
2919 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
2920 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
2921 &prd, &label[0], num_labels, 0, &evpn);
2922 else
2923 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
2924 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
2925 &prd, &label[0], num_labels, &evpn);
2926 return ret;
2927 }
2928
2929 /*
2930 * Process received EVPN type-3 route (advertise or withdraw).
2931 */
2932 static int process_type3_route(struct peer *peer, afi_t afi, safi_t safi,
2933 struct attr *attr, uint8_t *pfx, int psize,
2934 uint32_t addpath_id)
2935 {
2936 struct prefix_rd prd;
2937 struct prefix_evpn p;
2938 uint8_t ipaddr_len;
2939 uint32_t eth_tag;
2940 int ret;
2941
2942 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
2943 * IP len (1) and IP (4 or 16).
2944 */
2945 if (psize != 17 && psize != 29) {
2946 zlog_err("%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
2947 peer->bgp->vrf_id, peer->host, psize);
2948 return -1;
2949 }
2950
2951 /* If PMSI is present, log if it is anything other than IR.
2952 * Note: We just simply ignore the values as it is not clear if
2953 * doing anything else is better.
2954 */
2955 if (attr &&
2956 (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL))) {
2957 if (attr->pmsi_tnl_type != PMSI_TNLTYPE_INGR_REPL) {
2958 zlog_warn("%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d",
2959 peer->bgp->vrf_id, peer->host,
2960 attr->pmsi_tnl_type);
2961 }
2962 }
2963
2964 /* Make prefix_rd */
2965 prd.family = AF_UNSPEC;
2966 prd.prefixlen = 64;
2967 memcpy(&prd.val, pfx, 8);
2968 pfx += 8;
2969
2970 /* Make EVPN prefix. */
2971 memset(&p, 0, sizeof(struct prefix_evpn));
2972 p.family = AF_EVPN;
2973 p.prefixlen = EVPN_TYPE_3_ROUTE_PREFIXLEN;
2974 p.prefix.route_type = BGP_EVPN_IMET_ROUTE;
2975
2976 /* Copy Ethernet Tag */
2977 memcpy(&eth_tag, pfx, 4);
2978 p.prefix.imet_addr.eth_tag = ntohl(eth_tag);
2979 pfx += 4;
2980
2981 /* Get the IP. */
2982 ipaddr_len = *pfx++;
2983 if (ipaddr_len == IPV4_MAX_BITLEN) {
2984 p.prefix.imet_addr.ip.ipa_type = IPADDR_V4;
2985 memcpy(&p.prefix.imet_addr.ip.ip.addr, pfx, IPV4_MAX_BYTELEN);
2986 } else {
2987 zlog_err(
2988 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
2989 peer->bgp->vrf_id, peer->host, ipaddr_len);
2990 return -1;
2991 }
2992
2993 /* Process the route. */
2994 if (attr)
2995 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
2996 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
2997 &prd, NULL, 0, 0, NULL);
2998 else
2999 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
3000 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
3001 &prd, NULL, 0, NULL);
3002 return ret;
3003 }
3004
3005 /*
3006 * Process received EVPN type-5 route (advertise or withdraw).
3007 */
3008 static int process_type5_route(struct peer *peer, afi_t afi, safi_t safi,
3009 struct attr *attr, uint8_t *pfx, int psize,
3010 uint32_t addpath_id, int withdraw)
3011 {
3012 struct prefix_rd prd;
3013 struct prefix_evpn p;
3014 struct bgp_route_evpn evpn;
3015 uint8_t ippfx_len;
3016 uint32_t eth_tag;
3017 mpls_label_t label; /* holds the VNI as in the packet */
3018 int ret;
3019
3020 /* Type-5 route should be 34 or 58 bytes:
3021 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
3022 * GW (4 or 16) and VNI (3).
3023 * Note that the IP and GW should both be IPv4 or both IPv6.
3024 */
3025 if (psize != 34 && psize != 58) {
3026 zlog_err("%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
3027 peer->bgp->vrf_id, peer->host, psize);
3028 return -1;
3029 }
3030
3031 /* Make prefix_rd */
3032 prd.family = AF_UNSPEC;
3033 prd.prefixlen = 64;
3034 memcpy(&prd.val, pfx, 8);
3035 pfx += 8;
3036
3037 /* Make EVPN prefix. */
3038 memset(&p, 0, sizeof(struct prefix_evpn));
3039 p.family = AF_EVPN;
3040 p.prefixlen = EVPN_TYPE_5_ROUTE_PREFIXLEN;
3041 p.prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
3042
3043 /* Additional information outside of prefix - ESI and GW IP */
3044 memset(&evpn, 0, sizeof(evpn));
3045
3046 /* Fetch ESI */
3047 memcpy(&evpn.eth_s_id.val, pfx, 10);
3048 pfx += 10;
3049
3050 /* Fetch Ethernet Tag. */
3051 memcpy(&eth_tag, pfx, 4);
3052 p.prefix.prefix_addr.eth_tag = ntohl(eth_tag);
3053 pfx += 4;
3054
3055 /* Fetch IP prefix length. */
3056 ippfx_len = *pfx++;
3057 if (ippfx_len > IPV6_MAX_BITLEN) {
3058 zlog_err(
3059 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
3060 peer->bgp->vrf_id, peer->host, ippfx_len);
3061 return -1;
3062 }
3063 p.prefix.prefix_addr.ip_prefix_length = ippfx_len;
3064
3065 /* Determine IPv4 or IPv6 prefix */
3066 /* Since the address and GW are from the same family, this just becomes
3067 * a simple check on the total size.
3068 */
3069 if (psize == 34) {
3070 SET_IPADDR_V4(&p.prefix.prefix_addr.ip);
3071 memcpy(&p.prefix.prefix_addr.ip.ipaddr_v4, pfx, 4);
3072 pfx += 4;
3073 memcpy(&evpn.gw_ip.ipv4, pfx, 4);
3074 pfx += 4;
3075 } else {
3076 SET_IPADDR_V6(&p.prefix.prefix_addr.ip);
3077 memcpy(&p.prefix.prefix_addr.ip.ipaddr_v6, pfx, 16);
3078 pfx += 16;
3079 memcpy(&evpn.gw_ip.ipv6, pfx, 16);
3080 pfx += 16;
3081 }
3082
3083 /* Get the VNI (in MPLS label field). Stored as bytes here. */
3084 memset(&label, 0, sizeof(label));
3085 memcpy(&label, pfx, BGP_LABEL_BYTES);
3086
3087 /*
3088 * If in future, we are required to access additional fields,
3089 * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next
3090 * field
3091 */
3092
3093 /* Process the route. */
3094 if (!withdraw)
3095 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
3096 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
3097 &prd, &label, 1, 0, &evpn);
3098 else
3099 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
3100 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
3101 &prd, &label, 1, &evpn);
3102
3103 return ret;
3104 }
3105
3106 static void evpn_mpattr_encode_type5(struct stream *s, struct prefix *p,
3107 struct prefix_rd *prd, mpls_label_t *label,
3108 uint32_t num_labels, struct attr *attr)
3109 {
3110 int len;
3111 char temp[16];
3112 struct evpn_addr *p_evpn_p;
3113
3114 memset(&temp, 0, 16);
3115 if (p->family != AF_EVPN)
3116 return;
3117 p_evpn_p = &(p->u.prefix_evpn);
3118
3119 /* len denites the total len of IP and GW-IP in the route
3120 IP and GW-IP have to be both ipv4 or ipv6
3121 */
3122 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
3123 len = 8; /* IP and GWIP are both ipv4 */
3124 else
3125 len = 32; /* IP and GWIP are both ipv6 */
3126 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
3127 stream_putc(s, 8 + 10 + 4 + 1 + len + 3);
3128 stream_put(s, prd->val, 8);
3129 if (attr)
3130 stream_put(s, &(attr->evpn_overlay.eth_s_id), 10);
3131 else
3132 stream_put(s, &temp, 10);
3133 stream_putl(s, p_evpn_p->prefix_addr.eth_tag);
3134 stream_putc(s, p_evpn_p->prefix_addr.ip_prefix_length);
3135 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
3136 stream_put_ipv4(s, p_evpn_p->prefix_addr.ip.ipaddr_v4.s_addr);
3137 else
3138 stream_put(s, &p_evpn_p->prefix_addr.ip.ipaddr_v6, 16);
3139 if (attr) {
3140 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
3141 stream_put_ipv4(s,
3142 attr->evpn_overlay.gw_ip.ipv4.s_addr);
3143 else
3144 stream_put(s, &(attr->evpn_overlay.gw_ip.ipv6), 16);
3145 } else {
3146 if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip))
3147 stream_put_ipv4(s, 0);
3148 else
3149 stream_put(s, &temp, 16);
3150 }
3151
3152 if (num_labels)
3153 stream_put(s, label, 3);
3154 else
3155 stream_put3(s, 0);
3156 }
3157
3158 /*
3159 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
3160 */
3161 static void cleanup_vni_on_disable(struct hash_backet *backet, struct bgp *bgp)
3162 {
3163 struct bgpevpn *vpn = (struct bgpevpn *)backet->data;
3164
3165 /* Remove EVPN routes and schedule for processing. */
3166 delete_routes_for_vni(bgp, vpn);
3167
3168 /* Clear "live" flag and see if hash needs to be freed. */
3169 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
3170 if (!is_vni_configured(vpn))
3171 bgp_evpn_free(bgp, vpn);
3172 }
3173
3174 /*
3175 * Free a VNI entry; iterator function called during cleanup.
3176 */
3177 static void free_vni_entry(struct hash_backet *backet, struct bgp *bgp)
3178 {
3179 struct bgpevpn *vpn;
3180
3181 vpn = (struct bgpevpn *)backet->data;
3182 delete_all_vni_routes(bgp, vpn);
3183 bgp_evpn_free(bgp, vpn);
3184 }
3185
3186 /*
3187 * Derive AUTO import RT for BGP VRF - L3VNI
3188 */
3189 static void evpn_auto_rt_import_add_for_vrf(struct bgp *bgp_vrf)
3190 {
3191 struct bgp *bgp_def = NULL;
3192
3193 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
3194 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
3195
3196 /* Map RT to VRF */
3197 bgp_def = bgp_get_default();
3198 if (!bgp_def)
3199 return;
3200 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
3201 }
3202
3203 /*
3204 * Delete AUTO import RT from BGP VRF - L3VNI
3205 */
3206 static void evpn_auto_rt_import_delete_for_vrf(struct bgp *bgp_vrf)
3207 {
3208 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
3209 }
3210
3211 /*
3212 * Derive AUTO export RT for BGP VRF - L3VNI
3213 */
3214 static void evpn_auto_rt_export_add_for_vrf(struct bgp *bgp_vrf)
3215 {
3216 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
3217 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
3218 }
3219
3220 /*
3221 * Delete AUTO export RT from BGP VRF - L3VNI
3222 */
3223 static void evpn_auto_rt_export_delete_for_vrf(struct bgp *bgp_vrf)
3224 {
3225 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
3226 }
3227
3228 static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp *bgp_vrf)
3229 {
3230 struct bgp *bgp_def = NULL;
3231 struct listnode *node = NULL;
3232 struct bgpevpn *vpn = NULL;
3233
3234 bgp_def = bgp_get_default();
3235 if (!bgp_def)
3236 return;
3237
3238 /* update all type-5 routes */
3239 update_advertise_vrf_routes(bgp_vrf);
3240
3241 /* update all type-2 routes */
3242 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
3243 update_routes_for_vni(bgp_def, vpn);
3244 }
3245
3246 /*
3247 * Handle autort change for a given VNI.
3248 */
3249 static void update_autort_vni(struct hash_backet *backet, struct bgp *bgp)
3250 {
3251 struct bgpevpn *vpn = backet->data;
3252
3253 if (!vpn) {
3254 zlog_warn("%s: VNI hash entry for VNI not found", __PRETTY_FUNCTION__);
3255 return;
3256 }
3257
3258 if (!is_import_rt_configured(vpn)) {
3259 if (is_vni_live(vpn))
3260 bgp_evpn_uninstall_routes(bgp, vpn);
3261 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
3262 list_delete_all_node(vpn->import_rtl);
3263 bgp_evpn_derive_auto_rt_import(bgp, vpn);
3264 if (is_vni_live(vpn))
3265 bgp_evpn_install_routes(bgp, vpn);
3266 }
3267 if (!is_export_rt_configured(vpn)) {
3268 list_delete_all_node(vpn->export_rtl);
3269 bgp_evpn_derive_auto_rt_export(bgp, vpn);
3270 if (is_vni_live(vpn))
3271 bgp_evpn_handle_export_rt_change(bgp, vpn);
3272 }
3273 }
3274
3275 /*
3276 * Public functions.
3277 */
3278
3279 /* withdraw type-5 route corresponding to ip prefix */
3280 void bgp_evpn_withdraw_type5_route(struct bgp *bgp_vrf, struct prefix *p,
3281 afi_t afi, safi_t safi)
3282 {
3283 int ret = 0;
3284 struct prefix_evpn evp;
3285 char buf[PREFIX_STRLEN];
3286
3287 build_type5_prefix_from_ip_prefix(&evp, p);
3288 ret = delete_evpn_type5_route(bgp_vrf, &evp);
3289 if (ret) {
3290 zlog_err(
3291 "%u failed to delete type-5 route for prefix %s in vrf %s",
3292 bgp_vrf->vrf_id, prefix2str(p, buf, sizeof(buf)),
3293 vrf_id_to_name(bgp_vrf->vrf_id));
3294 }
3295 }
3296
3297 /* withdraw all type-5 routes for an address family */
3298 void bgp_evpn_withdraw_type5_routes(struct bgp *bgp_vrf, afi_t afi, safi_t safi)
3299 {
3300 struct bgp_table *table = NULL;
3301 struct bgp_node *rn = NULL;
3302 struct bgp_info *ri;
3303
3304 table = bgp_vrf->rib[afi][safi];
3305 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
3306 /* Only care about "selected" routes - non-imported. */
3307 /* TODO: Support for AddPath for EVPN. */
3308 for (ri = rn->info; ri; ri = ri->next) {
3309 if (CHECK_FLAG(ri->flags, BGP_INFO_SELECTED)
3310 && (!ri->extra || !ri->extra->parent)) {
3311 bgp_evpn_withdraw_type5_route(bgp_vrf, &rn->p,
3312 afi, safi);
3313 break;
3314 }
3315 }
3316 }
3317 }
3318
3319 /*
3320 * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed
3321 * to this function correspond to those of the source IP prefix (best
3322 * path in the case of the attr. In the case of a local prefix (when we
3323 * are advertising local subnets), the src_attr will be NULL.
3324 */
3325 void bgp_evpn_advertise_type5_route(struct bgp *bgp_vrf, struct prefix *p,
3326 struct attr *src_attr, afi_t afi,
3327 safi_t safi)
3328 {
3329 int ret = 0;
3330 struct prefix_evpn evp;
3331 char buf[PREFIX_STRLEN];
3332
3333 build_type5_prefix_from_ip_prefix(&evp, p);
3334 ret = update_evpn_type5_route(bgp_vrf, &evp, src_attr);
3335 if (ret)
3336 zlog_err("%u: Failed to create type-5 route for prefix %s",
3337 bgp_vrf->vrf_id, prefix2str(p, buf, sizeof(buf)));
3338 }
3339
3340 /* Inject all prefixes of a particular address-family (currently, IPv4 or
3341 * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the
3342 * advertisement is enabled.
3343 */
3344 void bgp_evpn_advertise_type5_routes(struct bgp *bgp_vrf, afi_t afi,
3345 safi_t safi)
3346 {
3347 struct bgp_table *table = NULL;
3348 struct bgp_node *rn = NULL;
3349 struct bgp_info *ri;
3350
3351 table = bgp_vrf->rib[afi][safi];
3352 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
3353 /* Need to identify the "selected" route entry to use its
3354 * attribute. Also, we only consider "non-imported" routes.
3355 * TODO: Support for AddPath for EVPN.
3356 */
3357 for (ri = rn->info; ri; ri = ri->next) {
3358 if (CHECK_FLAG(ri->flags, BGP_INFO_SELECTED)
3359 && (!ri->extra || !ri->extra->parent)) {
3360
3361 /* apply the route-map */
3362 if (bgp_vrf->adv_cmd_rmap[afi][safi].map) {
3363 int ret = 0;
3364
3365 ret = route_map_apply(
3366 bgp_vrf->adv_cmd_rmap[afi][safi]
3367 .map,
3368 &rn->p, RMAP_BGP, ri);
3369 if (ret == RMAP_DENYMATCH)
3370 continue;
3371 }
3372 bgp_evpn_advertise_type5_route(
3373 bgp_vrf, &rn->p, ri->attr, afi, safi);
3374 break;
3375 }
3376 }
3377 }
3378 }
3379
3380 void evpn_rt_delete_auto(struct bgp *bgp, vni_t vni, struct list *rtl)
3381 {
3382 struct listnode *node, *nnode, *node_to_del;
3383 struct ecommunity *ecom, *ecom_auto;
3384 struct ecommunity_val eval;
3385
3386 if (bgp->advertise_autort_rfc8365)
3387 vni |= EVPN_AUTORT_VXLAN;
3388 encode_route_target_as((bgp->as & 0xFFFF), vni, &eval);
3389
3390 ecom_auto = ecommunity_new();
3391 ecommunity_add_val(ecom_auto, &eval);
3392 node_to_del = NULL;
3393
3394 for (ALL_LIST_ELEMENTS(rtl, node, nnode, ecom)) {
3395 if (ecommunity_match(ecom, ecom_auto)) {
3396 ecommunity_free(&ecom);
3397 node_to_del = node;
3398 }
3399 }
3400
3401 if (node_to_del)
3402 list_delete_node(rtl, node_to_del);
3403
3404 ecommunity_free(&ecom_auto);
3405 }
3406
3407 void bgp_evpn_configure_import_rt_for_vrf(struct bgp *bgp_vrf,
3408 struct ecommunity *ecomadd)
3409 {
3410 /* uninstall routes from vrf */
3411 uninstall_routes_for_vrf(bgp_vrf);
3412
3413 /* Cleanup the RT to VRF mapping */
3414 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
3415
3416 /* Remove auto generated RT */
3417 evpn_auto_rt_import_delete_for_vrf(bgp_vrf);
3418
3419 /* Add the newly configured RT to RT list */
3420 listnode_add_sort(bgp_vrf->vrf_import_rtl, ecomadd);
3421 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
3422
3423 /* map VRF to its RTs */
3424 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
3425
3426 /* install routes matching the new VRF */
3427 install_routes_for_vrf(bgp_vrf);
3428 }
3429
3430 void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf,
3431 struct ecommunity *ecomdel)
3432 {
3433 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
3434 struct ecommunity *ecom = NULL;
3435
3436 /* uninstall routes from vrf */
3437 uninstall_routes_for_vrf(bgp_vrf);
3438
3439 /* Cleanup the RT to VRF mapping */
3440 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
3441
3442 /* remove the RT from the RT list */
3443 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
3444 if (ecommunity_match(ecom, ecomdel)) {
3445 ecommunity_free(&ecom);
3446 node_to_del = node;
3447 break;
3448 }
3449 }
3450
3451 if (node_to_del)
3452 list_delete_node(bgp_vrf->vrf_import_rtl, node_to_del);
3453
3454 /* fallback to auto import rt, if this was the last RT */
3455 if (list_isempty(bgp_vrf->vrf_import_rtl)) {
3456 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
3457 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
3458 }
3459
3460 /* map VRFs to its RTs */
3461 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
3462
3463 /* install routes matching this new RT */
3464 install_routes_for_vrf(bgp_vrf);
3465 }
3466
3467 void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf,
3468 struct ecommunity *ecomadd)
3469 {
3470 /* remove auto-generated RT */
3471 evpn_auto_rt_export_delete_for_vrf(bgp_vrf);
3472
3473 /* Add the new RT to the RT list */
3474 listnode_add_sort(bgp_vrf->vrf_export_rtl, ecomadd);
3475 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
3476
3477 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
3478 }
3479
3480 void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf,
3481 struct ecommunity *ecomdel)
3482 {
3483 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
3484 struct ecommunity *ecom = NULL;
3485
3486 /* Remove the RT from the RT list */
3487 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_export_rtl, node, nnode, ecom)) {
3488 if (ecommunity_match(ecom, ecomdel)) {
3489 ecommunity_free(&ecom);
3490 node_to_del = node;
3491 break;
3492 }
3493 }
3494
3495 if (node_to_del)
3496 list_delete_node(bgp_vrf->vrf_export_rtl, node_to_del);
3497
3498 /* fall back to auto-generated RT if this was the last RT */
3499 if (bgp_vrf->vrf_export_rtl && list_isempty(bgp_vrf->vrf_export_rtl)) {
3500 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
3501 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
3502 }
3503
3504 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
3505 }
3506
3507 /*
3508 * Handle change to BGP router id. This is invoked twice by the change
3509 * handler, first before the router id has been changed and then after
3510 * the router id has been changed. The first invocation will result in
3511 * local routes for all VNIs/VRF being deleted and withdrawn and the next
3512 * will result in the routes being re-advertised.
3513 */
3514 void bgp_evpn_handle_router_id_update(struct bgp *bgp, int withdraw)
3515 {
3516 if (withdraw) {
3517
3518 /* delete and withdraw all the type-5 routes
3519 stored in the global table for this vrf
3520 */
3521 withdraw_router_id_vrf(bgp);
3522
3523 /* delete all the VNI routes (type-2/type-3) routes for all the
3524 * L2-VNIs
3525 */
3526 hash_iterate(bgp->vnihash,
3527 (void (*)(struct hash_backet *,
3528 void *))withdraw_router_id_vni,
3529 bgp);
3530 } else {
3531
3532 /* advertise all routes in the vrf as type-5 routes with the new
3533 * RD
3534 */
3535 update_router_id_vrf(bgp);
3536
3537 /* advertise all the VNI routes (type-2/type-3) routes with the
3538 * new RD
3539 */
3540 hash_iterate(bgp->vnihash,
3541 (void (*)(struct hash_backet *,
3542 void *))update_router_id_vni,
3543 bgp);
3544 }
3545 }
3546
3547 /*
3548 * Handle change to auto-RT algorithm - update and advertise local routes.
3549 */
3550 void bgp_evpn_handle_autort_change(struct bgp *bgp)
3551 {
3552 hash_iterate(bgp->vnihash,
3553 (void (*)(struct hash_backet *,
3554 void*))update_autort_vni,
3555 bgp);
3556 }
3557
3558 /*
3559 * Handle change to export RT - update and advertise local routes.
3560 */
3561 int bgp_evpn_handle_export_rt_change(struct bgp *bgp, struct bgpevpn *vpn)
3562 {
3563 return update_routes_for_vni(bgp, vpn);
3564 }
3565
3566 void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw)
3567 {
3568 if (withdraw)
3569 delete_withdraw_vrf_routes(bgp_vrf);
3570 else
3571 update_advertise_vrf_routes(bgp_vrf);
3572 }
3573
3574 /*
3575 * Handle change to RD. This is invoked twice by the change handler,
3576 * first before the RD has been changed and then after the RD has
3577 * been changed. The first invocation will result in local routes
3578 * of this VNI being deleted and withdrawn and the next will result
3579 * in the routes being re-advertised.
3580 */
3581 void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
3582 int withdraw)
3583 {
3584 if (withdraw)
3585 delete_withdraw_vni_routes(bgp, vpn);
3586 else
3587 update_advertise_vni_routes(bgp, vpn);
3588 }
3589
3590 /*
3591 * Install routes for this VNI. Invoked upon change to Import RT.
3592 */
3593 int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn)
3594 {
3595 return install_routes_for_vni(bgp, vpn);
3596 }
3597
3598 /*
3599 * Uninstall all routes installed for this VNI. Invoked upon change
3600 * to Import RT.
3601 */
3602 int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn)
3603 {
3604 return uninstall_routes_for_vni(bgp, vpn);
3605 }
3606
3607 /*
3608 * TODO: Hardcoded for a maximum of 2 VNIs right now
3609 */
3610 char *bgp_evpn_label2str(mpls_label_t *label, uint32_t num_labels, char *buf,
3611 int len)
3612 {
3613 vni_t vni1, vni2;
3614
3615 vni1 = label2vni(label);
3616 if (num_labels == 2) {
3617 vni2 = label2vni(label + 1);
3618 snprintf(buf, len, "%u/%u", vni1, vni2);
3619 } else
3620 snprintf(buf, len, "%u", vni1);
3621 return buf;
3622 }
3623
3624 /*
3625 * Function to convert evpn route to json format.
3626 * NOTE: We don't use prefix2str as the output here is a bit different.
3627 */
3628 void bgp_evpn_route2json(struct prefix_evpn *p, json_object *json)
3629 {
3630 char buf1[ETHER_ADDR_STRLEN];
3631 char buf2[PREFIX2STR_BUFFER];
3632
3633 if (!json)
3634 return;
3635
3636 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
3637 json_object_int_add(json, "routeType", p->prefix.route_type);
3638 json_object_int_add(json, "ethTag",
3639 p->prefix.imet_addr.eth_tag);
3640 json_object_int_add(json, "ipLen",
3641 is_evpn_prefix_ipaddr_v4(p)
3642 ? IPV4_MAX_BITLEN
3643 : IPV6_MAX_BITLEN);
3644 json_object_string_add(json, "ip",
3645 inet_ntoa(p->prefix.imet_addr.ip.ipaddr_v4));
3646 } else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
3647 if (is_evpn_prefix_ipaddr_none(p)) {
3648 json_object_int_add(json, "routeType",
3649 p->prefix.route_type);
3650 json_object_int_add(json, "ethTag",
3651 p->prefix.macip_addr.eth_tag);
3652 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
3653 json_object_string_add(json, "mac",
3654 prefix_mac2str(&p->prefix.macip_addr.mac,
3655 buf1,
3656 sizeof(buf1)));
3657 } else {
3658 uint8_t family;
3659
3660 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET
3661 : AF_INET6;
3662
3663 json_object_int_add(json, "routeType",
3664 p->prefix.route_type);
3665 json_object_int_add(json, "ethTag",
3666 p->prefix.macip_addr.eth_tag);
3667 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
3668 json_object_string_add(json, "mac",
3669 prefix_mac2str(&p->prefix.macip_addr.mac,
3670 buf1,
3671 sizeof(buf1)));
3672 json_object_int_add(json, "ipLen",
3673 is_evpn_prefix_ipaddr_v4(p)
3674 ? IPV4_MAX_BITLEN
3675 : IPV6_MAX_BITLEN);
3676 json_object_string_add(
3677 json, "ip",
3678 inet_ntop(family,
3679 &p->prefix.macip_addr.ip.ip.addr,
3680 buf2,
3681 PREFIX2STR_BUFFER));
3682 }
3683 } else {
3684 /* Currently, this is to cater to other AF_ETHERNET code. */
3685 }
3686 }
3687
3688 /*
3689 * Function to convert evpn route to string.
3690 * NOTE: We don't use prefix2str as the output here is a bit different.
3691 */
3692 char *bgp_evpn_route2str(struct prefix_evpn *p, char *buf, int len)
3693 {
3694 char buf1[ETHER_ADDR_STRLEN];
3695 char buf2[PREFIX2STR_BUFFER];
3696
3697 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
3698 snprintf(buf, len, "[%d]:[%d]:[%d]:[%s]", p->prefix.route_type,
3699 p->prefix.imet_addr.eth_tag,
3700 is_evpn_prefix_ipaddr_v4(p) ? IPV4_MAX_BITLEN
3701 : IPV6_MAX_BITLEN,
3702 inet_ntoa(p->prefix.imet_addr.ip.ipaddr_v4));
3703 } else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
3704 if (is_evpn_prefix_ipaddr_none(p))
3705 snprintf(buf, len, "[%d]:[%d]:[%d]:[%s]",
3706 p->prefix.route_type,
3707 p->prefix.macip_addr.eth_tag,
3708 8 * ETH_ALEN,
3709 prefix_mac2str(&p->prefix.macip_addr.mac, buf1,
3710 sizeof(buf1)));
3711 else {
3712 uint8_t family;
3713
3714 family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET
3715 : AF_INET6;
3716 snprintf(buf, len, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]",
3717 p->prefix.route_type,
3718 p->prefix.macip_addr.eth_tag,
3719 8 * ETH_ALEN,
3720 prefix_mac2str(&p->prefix.macip_addr.mac, buf1,
3721 sizeof(buf1)),
3722 family == AF_INET ? IPV4_MAX_BITLEN
3723 : IPV6_MAX_BITLEN,
3724 inet_ntop(family,
3725 &p->prefix.macip_addr.ip.ip.addr,
3726 buf2,
3727 PREFIX2STR_BUFFER));
3728 }
3729 } else if (p->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE) {
3730 snprintf(buf, len, "[%d]:[%d]:[%d]:[%s]",
3731 p->prefix.route_type,
3732 p->prefix.prefix_addr.eth_tag,
3733 p->prefix.prefix_addr.ip_prefix_length,
3734 is_evpn_prefix_ipaddr_v4(p)
3735 ? inet_ntoa(p->prefix.prefix_addr.ip.ipaddr_v4)
3736 : inet6_ntoa(p->prefix.prefix_addr.ip.ipaddr_v6));
3737 } else {
3738 /* For EVPN route types not supported yet. */
3739 snprintf(buf, len, "(unsupported route type %d)",
3740 p->prefix.route_type);
3741 }
3742
3743 return (buf);
3744 }
3745
3746 /*
3747 * Encode EVPN prefix in Update (MP_REACH)
3748 */
3749 void bgp_evpn_encode_prefix(struct stream *s, struct prefix *p,
3750 struct prefix_rd *prd, mpls_label_t *label,
3751 uint32_t num_labels, struct attr *attr,
3752 int addpath_encode, uint32_t addpath_tx_id)
3753 {
3754 struct prefix_evpn *evp = (struct prefix_evpn *)p;
3755 int len, ipa_len = 0;
3756
3757 if (addpath_encode)
3758 stream_putl(s, addpath_tx_id);
3759
3760 /* Route type */
3761 stream_putc(s, evp->prefix.route_type);
3762
3763 switch (evp->prefix.route_type) {
3764 case BGP_EVPN_MAC_IP_ROUTE:
3765 if (is_evpn_prefix_ipaddr_v4(evp))
3766 ipa_len = IPV4_MAX_BYTELEN;
3767 else if (is_evpn_prefix_ipaddr_v6(evp))
3768 ipa_len = IPV6_MAX_BYTELEN;
3769 /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */
3770 len = 8 + 10 + 4 + 1 + 6 + 1 + ipa_len + 3;
3771 if (ipa_len && num_labels > 1) /* There are 2 VNIs */
3772 len += 3;
3773 stream_putc(s, len);
3774 stream_put(s, prd->val, 8); /* RD */
3775 if (attr)
3776 stream_put(s, &attr->evpn_overlay.eth_s_id, ESI_LEN);
3777 else
3778 stream_put(s, 0, 10);
3779 stream_putl(s, evp->prefix.macip_addr.eth_tag); /* Ethernet Tag ID */
3780 stream_putc(s, 8 * ETH_ALEN); /* Mac Addr Len - bits */
3781 stream_put(s, evp->prefix.macip_addr.mac.octet, 6); /* Mac Addr */
3782 stream_putc(s, 8 * ipa_len); /* IP address Length */
3783 if (ipa_len) /* IP */
3784 stream_put(s, &evp->prefix.macip_addr.ip.ip.addr,
3785 ipa_len);
3786 /* 1st label is the L2 VNI */
3787 stream_put(s, label, BGP_LABEL_BYTES);
3788 /* Include 2nd label (L3 VNI) if advertising MAC+IP */
3789 if (ipa_len && num_labels > 1)
3790 stream_put(s, label + 1, BGP_LABEL_BYTES);
3791 break;
3792
3793 case BGP_EVPN_IMET_ROUTE:
3794 stream_putc(s, 17); // TODO: length - assumes IPv4 address
3795 stream_put(s, prd->val, 8); /* RD */
3796 stream_putl(s, evp->prefix.imet_addr.eth_tag); /* Ethernet Tag ID */
3797 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
3798 /* Originating Router's IP Addr */
3799 stream_put_in_addr(s, &evp->prefix.imet_addr.ip.ipaddr_v4);
3800 break;
3801
3802 case BGP_EVPN_IP_PREFIX_ROUTE:
3803 /* TODO: AddPath support. */
3804 evpn_mpattr_encode_type5(s, p, prd, label, num_labels, attr);
3805 break;
3806
3807 default:
3808 break;
3809 }
3810 }
3811
3812 int bgp_nlri_parse_evpn(struct peer *peer, struct attr *attr,
3813 struct bgp_nlri *packet, int withdraw)
3814 {
3815 uint8_t *pnt;
3816 uint8_t *lim;
3817 afi_t afi;
3818 safi_t safi;
3819 uint32_t addpath_id;
3820 int addpath_encoded;
3821 int psize = 0;
3822 uint8_t rtype;
3823 uint8_t rlen;
3824 struct prefix p;
3825
3826 /* Start processing the NLRI - there may be multiple in the MP_REACH */
3827 pnt = packet->nlri;
3828 lim = pnt + packet->length;
3829 afi = packet->afi;
3830 safi = packet->safi;
3831 addpath_id = 0;
3832
3833 addpath_encoded =
3834 (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV)
3835 && CHECK_FLAG(peer->af_cap[afi][safi],
3836 PEER_CAP_ADDPATH_AF_TX_RCV));
3837
3838 for (; pnt < lim; pnt += psize) {
3839 /* Clear prefix structure. */
3840 memset(&p, 0, sizeof(struct prefix));
3841
3842 /* Deal with path-id if AddPath is supported. */
3843 if (addpath_encoded) {
3844 /* When packet overflow occurs return immediately. */
3845 if (pnt + BGP_ADDPATH_ID_LEN > lim)
3846 return -1;
3847
3848 addpath_id = ntohl(*((uint32_t *)pnt));
3849 pnt += BGP_ADDPATH_ID_LEN;
3850 }
3851
3852 /* All EVPN NLRI types start with type and length. */
3853 if (pnt + 2 > lim)
3854 return -1;
3855
3856 rtype = *pnt++;
3857 psize = rlen = *pnt++;
3858
3859 /* When packet overflow occur return immediately. */
3860 if (pnt + psize > lim)
3861 return -1;
3862
3863 switch (rtype) {
3864 case BGP_EVPN_MAC_IP_ROUTE:
3865 if (process_type2_route(peer, afi, safi,
3866 withdraw ? NULL : attr, pnt,
3867 psize, addpath_id)) {
3868 zlog_err(
3869 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
3870 peer->bgp->vrf_id, peer->host, psize);
3871 return -1;
3872 }
3873 break;
3874
3875 case BGP_EVPN_IMET_ROUTE:
3876 if (process_type3_route(peer, afi, safi,
3877 withdraw ? NULL : attr, pnt,
3878 psize, addpath_id)) {
3879 zlog_err(
3880 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
3881 peer->bgp->vrf_id, peer->host, psize);
3882 return -1;
3883 }
3884 break;
3885
3886 case BGP_EVPN_IP_PREFIX_ROUTE:
3887 if (process_type5_route(peer, afi, safi, attr, pnt,
3888 psize, addpath_id, withdraw)) {
3889 zlog_err(
3890 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
3891 peer->bgp->vrf_id, peer->host, psize);
3892 return -1;
3893 }
3894 break;
3895
3896 default:
3897 break;
3898 }
3899 }
3900
3901 /* Packet length consistency check. */
3902 if (pnt != lim)
3903 return -1;
3904
3905 return 0;
3906 }
3907
3908 /*
3909 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
3910 * The mapping will be used during route processing.
3911 * bgp_def: default bgp instance
3912 * bgp_vrf: specific bgp vrf instance on which RT is configured
3913 */
3914 void bgp_evpn_map_vrf_to_its_rts(struct bgp *bgp_vrf)
3915 {
3916 int i = 0;
3917 struct ecommunity_val *eval = NULL;
3918 struct listnode *node = NULL, *nnode = NULL;
3919 struct ecommunity *ecom = NULL;
3920
3921 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
3922 for (i = 0; i < ecom->size; i++) {
3923 eval = (struct ecommunity_val *)(ecom->val
3924 + (i
3925 * ECOMMUNITY_SIZE));
3926 map_vrf_to_rt(bgp_vrf, eval);
3927 }
3928 }
3929 }
3930
3931 /*
3932 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
3933 */
3934 void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *bgp_vrf)
3935 {
3936 int i;
3937 struct ecommunity_val *eval;
3938 struct listnode *node, *nnode;
3939 struct ecommunity *ecom;
3940
3941 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
3942 for (i = 0; i < ecom->size; i++) {
3943 struct vrf_irt_node *irt;
3944 struct ecommunity_val eval_tmp;
3945
3946 eval = (struct ecommunity_val *)(ecom->val
3947 + (i
3948 * ECOMMUNITY_SIZE));
3949 /* If using "automatic" RT, we only care about the
3950 * local-admin sub-field.
3951 * This is to facilitate using VNI as the RT for EBGP
3952 * peering too.
3953 */
3954 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
3955 if (!CHECK_FLAG(bgp_vrf->vrf_flags,
3956 BGP_VRF_IMPORT_RT_CFGD))
3957 mask_ecom_global_admin(&eval_tmp, eval);
3958
3959 irt = lookup_vrf_import_rt(&eval_tmp);
3960 if (irt)
3961 unmap_vrf_from_rt(bgp_vrf, irt);
3962 }
3963 }
3964 }
3965
3966
3967 /*
3968 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
3969 * The mapping will be used during route processing.
3970 */
3971 void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
3972 {
3973 int i;
3974 struct ecommunity_val *eval;
3975 struct listnode *node, *nnode;
3976 struct ecommunity *ecom;
3977
3978 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
3979 for (i = 0; i < ecom->size; i++) {
3980 eval = (struct ecommunity_val *)(ecom->val
3981 + (i
3982 * ECOMMUNITY_SIZE));
3983 map_vni_to_rt(bgp, vpn, eval);
3984 }
3985 }
3986 }
3987
3988 /*
3989 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
3990 */
3991 void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
3992 {
3993 int i;
3994 struct ecommunity_val *eval;
3995 struct listnode *node, *nnode;
3996 struct ecommunity *ecom;
3997
3998 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
3999 for (i = 0; i < ecom->size; i++) {
4000 struct irt_node *irt;
4001 struct ecommunity_val eval_tmp;
4002
4003 eval = (struct ecommunity_val *)(ecom->val
4004 + (i
4005 * ECOMMUNITY_SIZE));
4006 /* If using "automatic" RT, we only care about the
4007 * local-admin sub-field.
4008 * This is to facilitate using VNI as the RT for EBGP
4009 * peering too.
4010 */
4011 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
4012 if (!is_import_rt_configured(vpn))
4013 mask_ecom_global_admin(&eval_tmp, eval);
4014
4015 irt = lookup_import_rt(bgp, &eval_tmp);
4016 if (irt)
4017 unmap_vni_from_rt(bgp, vpn, irt);
4018 }
4019 }
4020 }
4021
4022 /*
4023 * Derive Import RT automatically for VNI and map VNI to RT.
4024 * The mapping will be used during route processing.
4025 */
4026 void bgp_evpn_derive_auto_rt_import(struct bgp *bgp, struct bgpevpn *vpn)
4027 {
4028 form_auto_rt(bgp, vpn->vni, vpn->import_rtl);
4029 UNSET_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD);
4030
4031 /* Map RT to VNI */
4032 bgp_evpn_map_vni_to_its_rts(bgp, vpn);
4033 }
4034
4035 /*
4036 * Derive Export RT automatically for VNI.
4037 */
4038 void bgp_evpn_derive_auto_rt_export(struct bgp *bgp, struct bgpevpn *vpn)
4039 {
4040 form_auto_rt(bgp, vpn->vni, vpn->export_rtl);
4041 UNSET_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD);
4042 }
4043
4044 /*
4045 * Derive RD automatically for VNI using passed information - it
4046 * is of the form RouterId:unique-id-for-vni.
4047 */
4048 void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp)
4049 {
4050 form_auto_rd(bgp->router_id, bgp->vrf_rd_id, &bgp->vrf_prd);
4051 }
4052
4053 /*
4054 * Derive RD automatically for VNI using passed information - it
4055 * is of the form RouterId:unique-id-for-vni.
4056 */
4057 void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn)
4058 {
4059 char buf[100];
4060
4061 vpn->prd.family = AF_UNSPEC;
4062 vpn->prd.prefixlen = 64;
4063 sprintf(buf, "%s:%hu", inet_ntoa(bgp->router_id), vpn->rd_id);
4064 (void)str2prefix_rd(buf, &vpn->prd);
4065 UNSET_FLAG(vpn->flags, VNI_FLAG_RD_CFGD);
4066 }
4067
4068 /*
4069 * Lookup VNI.
4070 */
4071 struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni)
4072 {
4073 struct bgpevpn *vpn;
4074 struct bgpevpn tmp;
4075
4076 memset(&tmp, 0, sizeof(struct bgpevpn));
4077 tmp.vni = vni;
4078 vpn = hash_lookup(bgp->vnihash, &tmp);
4079 return vpn;
4080 }
4081
4082 /*
4083 * Create a new vpn - invoked upon configuration or zebra notification.
4084 */
4085 struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
4086 struct in_addr originator_ip,
4087 vrf_id_t tenant_vrf_id)
4088 {
4089 struct bgpevpn *vpn;
4090
4091 if (!bgp)
4092 return NULL;
4093
4094 vpn = XCALLOC(MTYPE_BGP_EVPN, sizeof(struct bgpevpn));
4095 if (!vpn)
4096 return NULL;
4097
4098 /* Set values - RD and RT set to defaults. */
4099 vpn->vni = vni;
4100 vpn->originator_ip = originator_ip;
4101 vpn->tenant_vrf_id = tenant_vrf_id;
4102
4103 /* Initialize route-target import and export lists */
4104 vpn->import_rtl = list_new();
4105 vpn->import_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
4106 vpn->import_rtl->del = evpn_xxport_delete_ecomm;
4107 vpn->export_rtl = list_new();
4108 vpn->export_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
4109 vpn->export_rtl->del = evpn_xxport_delete_ecomm;
4110 bf_assign_index(bm->rd_idspace, vpn->rd_id);
4111 derive_rd_rt_for_vni(bgp, vpn);
4112
4113 /* Initialize EVPN route table. */
4114 vpn->route_table = bgp_table_init(bgp, AFI_L2VPN, SAFI_EVPN);
4115
4116 /* Add to hash */
4117 if (!hash_get(bgp->vnihash, vpn, hash_alloc_intern)) {
4118 XFREE(MTYPE_BGP_EVPN, vpn);
4119 return NULL;
4120 }
4121
4122 /* add to l2vni list on corresponding vrf */
4123 bgpevpn_link_to_l3vni(vpn);
4124
4125 QOBJ_REG(vpn, bgpevpn);
4126 return vpn;
4127 }
4128
4129 /*
4130 * Free a given VPN - called in multiple scenarios such as zebra
4131 * notification, configuration being deleted, advertise-all-vni disabled etc.
4132 * This just frees appropriate memory, caller should have taken other
4133 * needed actions.
4134 */
4135 void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn)
4136 {
4137 bgpevpn_unlink_from_l3vni(vpn);
4138 bgp_table_unlock(vpn->route_table);
4139 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
4140 list_delete_and_null(&vpn->import_rtl);
4141 list_delete_and_null(&vpn->export_rtl);
4142 bf_release_index(bm->rd_idspace, vpn->rd_id);
4143 hash_release(bgp->vnihash, vpn);
4144 QOBJ_UNREG(vpn);
4145 XFREE(MTYPE_BGP_EVPN, vpn);
4146 }
4147
4148 /*
4149 * Import route into matching VNI(s).
4150 */
4151 int bgp_evpn_import_route(struct bgp *bgp, afi_t afi, safi_t safi,
4152 struct prefix *p, struct bgp_info *ri)
4153 {
4154 return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 1);
4155 }
4156
4157 /*
4158 * Unimport route from matching VNI(s).
4159 */
4160 int bgp_evpn_unimport_route(struct bgp *bgp, afi_t afi, safi_t safi,
4161 struct prefix *p, struct bgp_info *ri)
4162 {
4163 return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 0);
4164 }
4165
4166 /* filter routes which have martian next hops */
4167 int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp *bgp)
4168 {
4169 afi_t afi;
4170 safi_t safi;
4171 struct bgp_node *rd_rn, *rn;
4172 struct bgp_table *table;
4173 struct bgp_info *ri;
4174
4175 afi = AFI_L2VPN;
4176 safi = SAFI_EVPN;
4177
4178 /* Walk entire global routing table and evaluate routes which could be
4179 * imported into this VPN. Note that we cannot just look at the routes
4180 * for the VNI's RD -
4181 * remote routes applicable for this VNI could have any RD.
4182 */
4183 /* EVPN routes are a 2-level table. */
4184 for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
4185 rd_rn = bgp_route_next(rd_rn)) {
4186 table = (struct bgp_table *)(rd_rn->info);
4187 if (!table)
4188 continue;
4189
4190 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
4191
4192 for (ri = rn->info; ri; ri = ri->next) {
4193
4194 /* Consider "valid" remote routes applicable for
4195 * this VNI. */
4196 if (!(ri->type == ZEBRA_ROUTE_BGP
4197 && ri->sub_type == BGP_ROUTE_NORMAL))
4198 continue;
4199
4200 if (bgp_nexthop_self(bgp, ri->attr->nexthop)) {
4201
4202 char attr_str[BUFSIZ];
4203 char pbuf[PREFIX_STRLEN];
4204
4205 bgp_dump_attr(ri->attr, attr_str,
4206 BUFSIZ);
4207
4208 if (bgp_debug_update(ri->peer, &rn->p,
4209 NULL, 1))
4210 zlog_debug(
4211 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
4212 bgp->vrf_id,
4213 prefix2str(
4214 &rn->p, pbuf,
4215 sizeof(pbuf)),
4216 attr_str);
4217
4218 bgp_evpn_unimport_route(bgp, afi, safi,
4219 &rn->p, ri);
4220
4221 bgp_rib_remove(rn, ri, ri->peer, afi,
4222 safi);
4223 }
4224 }
4225 }
4226 }
4227
4228 return 0;
4229 }
4230
4231 /*
4232 * Handle del of a local MACIP.
4233 */
4234 int bgp_evpn_local_macip_del(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
4235 struct ipaddr *ip)
4236 {
4237 struct bgpevpn *vpn;
4238 struct prefix_evpn p;
4239
4240 if (!bgp->vnihash) {
4241 zlog_err("%u: VNI hash not created", bgp->vrf_id);
4242 return -1;
4243 }
4244
4245 /* Lookup VNI hash - should exist. */
4246 vpn = bgp_evpn_lookup_vni(bgp, vni);
4247 if (!vpn || !is_vni_live(vpn)) {
4248 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL",
4249 bgp->vrf_id, vni, vpn ? "not live" : "not found");
4250 return -1;
4251 }
4252
4253 /* Remove EVPN type-2 route and schedule for processing. */
4254 build_evpn_type2_prefix(&p, mac, ip);
4255 delete_evpn_route(bgp, vpn, &p);
4256
4257 return 0;
4258 }
4259
4260 /*
4261 * Handle add of a local MACIP.
4262 */
4263 int bgp_evpn_local_macip_add(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
4264 struct ipaddr *ip, uint8_t flags)
4265 {
4266 struct bgpevpn *vpn;
4267 struct prefix_evpn p;
4268
4269 if (!bgp->vnihash) {
4270 zlog_err("%u: VNI hash not created", bgp->vrf_id);
4271 return -1;
4272 }
4273
4274 /* Lookup VNI hash - should exist. */
4275 vpn = bgp_evpn_lookup_vni(bgp, vni);
4276 if (!vpn || !is_vni_live(vpn)) {
4277 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD",
4278 bgp->vrf_id, vni, vpn ? "not live" : "not found");
4279 return -1;
4280 }
4281
4282 /* Create EVPN type-2 route and schedule for processing. */
4283 build_evpn_type2_prefix(&p, mac, ip);
4284 if (update_evpn_route(bgp, vpn, &p, flags)) {
4285 char buf[ETHER_ADDR_STRLEN];
4286 char buf2[INET6_ADDRSTRLEN];
4287
4288 zlog_err(
4289 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)",
4290 bgp->vrf_id, vpn->vni,
4291 CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY)
4292 ? "sticky gateway"
4293 : "",
4294 prefix_mac2str(mac, buf, sizeof(buf)),
4295 ipaddr2str(ip, buf2, sizeof(buf2)), flags);
4296 return -1;
4297 }
4298
4299 return 0;
4300 }
4301
4302 static void link_l2vni_hash_to_l3vni(struct hash_backet *backet,
4303 struct bgp *bgp_vrf)
4304 {
4305 struct bgpevpn *vpn = NULL;
4306 struct bgp *bgp_def = NULL;
4307
4308 bgp_def = bgp_get_default();
4309 assert(bgp_def);
4310
4311 vpn = (struct bgpevpn *)backet->data;
4312 if (vpn->tenant_vrf_id == bgp_vrf->vrf_id)
4313 bgpevpn_link_to_l3vni(vpn);
4314 }
4315
4316 int bgp_evpn_local_l3vni_add(vni_t l3vni, vrf_id_t vrf_id, struct ethaddr *rmac,
4317 struct in_addr originator_ip, int filter)
4318 {
4319 struct bgp *bgp_vrf = NULL; /* bgp VRF instance */
4320 struct bgp *bgp_def = NULL; /* default bgp instance */
4321 struct listnode *node = NULL;
4322 struct bgpevpn *vpn = NULL;
4323 as_t as = 0;
4324
4325 /* get the default instamce - required to get the AS number for VRF
4326 * auto-creatio
4327 */
4328 bgp_def = bgp_get_default();
4329 if (!bgp_def) {
4330 zlog_err(
4331 "Cannot process L3VNI %u ADD - default BGP instance not yet created",
4332 l3vni);
4333 return -1;
4334 }
4335 as = bgp_def->as;
4336
4337 /* if the BGP vrf instance doesnt exist - create one */
4338 bgp_vrf = bgp_lookup_by_name(vrf_id_to_name(vrf_id));
4339 if (!bgp_vrf) {
4340
4341 int ret = 0;
4342
4343 ret = bgp_get(&bgp_vrf, &as, vrf_id_to_name(vrf_id),
4344 BGP_INSTANCE_TYPE_VRF);
4345 switch (ret) {
4346 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET:
4347 zlog_err("'bgp multiple-instance' not present\n");
4348 return -1;
4349 case BGP_ERR_AS_MISMATCH:
4350 zlog_err("BGP is already running; AS is %u\n", as);
4351 return -1;
4352 case BGP_ERR_INSTANCE_MISMATCH:
4353 zlog_err("BGP instance name and AS number mismatch\n");
4354 return -1;
4355 }
4356
4357 /* mark as auto created */
4358 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO);
4359 }
4360
4361 /* associate with l3vni */
4362 bgp_vrf->l3vni = l3vni;
4363
4364 /* set the router mac - to be used in mac-ip routes for this vrf */
4365 memcpy(&bgp_vrf->rmac, rmac, sizeof(struct ethaddr));
4366
4367 /* set the originator ip */
4368 bgp_vrf->originator_ip = originator_ip;
4369
4370 /* set the right filter - are we using l3vni only for prefix routes? */
4371 if (filter)
4372 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY);
4373
4374 /* auto derive RD/RT */
4375 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
4376 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
4377 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD))
4378 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
4379 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
4380
4381 /* link all corresponding l2vnis */
4382 hash_iterate(bgp_def->vnihash,
4383 (void (*)(struct hash_backet *,
4384 void *))link_l2vni_hash_to_l3vni,
4385 bgp_vrf);
4386
4387 /* Only update all corresponding type-2 routes if we are advertising two
4388 * labels along with type-2 routes
4389 */
4390 if (!filter)
4391 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
4392 update_routes_for_vni(bgp_def, vpn);
4393
4394 /* advertise type-5 routes if needed */
4395 update_advertise_vrf_routes(bgp_vrf);
4396
4397 /* install all remote routes belonging to this l3vni into correspondng
4398 * vrf */
4399 install_routes_for_vrf(bgp_vrf);
4400
4401 return 0;
4402 }
4403
4404 int bgp_evpn_local_l3vni_del(vni_t l3vni, vrf_id_t vrf_id)
4405 {
4406 struct bgp *bgp_vrf = NULL; /* bgp vrf instance */
4407 struct bgp *bgp_def = NULL; /* default bgp instance */
4408 struct listnode *node = NULL;
4409 struct listnode *next = NULL;
4410 struct bgpevpn *vpn = NULL;
4411
4412 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
4413 if (!bgp_vrf) {
4414 zlog_err(
4415 "Cannot process L3VNI %u Del - Could not find BGP instance",
4416 l3vni);
4417 return -1;
4418 }
4419
4420 bgp_def = bgp_get_default();
4421 if (!bgp_def) {
4422 zlog_err(
4423 "Cannot process L3VNI %u Del - Could not find default BGP instance",
4424 l3vni);
4425 return -1;
4426 }
4427
4428 /* unimport remote routes from VRF, if it is AUTO vrf bgp_delete will
4429 * take care of uninstalling the routes from zebra
4430 */
4431 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO))
4432 uninstall_routes_for_vrf(bgp_vrf);
4433
4434 /* delete/withdraw all type-5 routes */
4435 delete_withdraw_vrf_routes(bgp_vrf);
4436
4437 /* remove the l3vni from vrf instance */
4438 bgp_vrf->l3vni = 0;
4439
4440 /* remove the Rmac from the BGP vrf */
4441 memset(&bgp_vrf->rmac, 0, sizeof(struct ethaddr));
4442
4443 /* delete RD/RT */
4444 if (bgp_vrf->vrf_import_rtl && !list_isempty(bgp_vrf->vrf_import_rtl)) {
4445 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
4446 list_delete_all_node(bgp_vrf->vrf_import_rtl);
4447 }
4448 if (bgp_vrf->vrf_export_rtl && !list_isempty(bgp_vrf->vrf_export_rtl)) {
4449 list_delete_all_node(bgp_vrf->vrf_export_rtl);
4450 }
4451
4452 /* update all corresponding local mac-ip routes */
4453 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY)) {
4454 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) {
4455 UNSET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS);
4456 update_routes_for_vni(bgp_def, vpn);
4457 }
4458 }
4459
4460 /* If any L2VNIs point to this instance, unlink them. */
4461 for (ALL_LIST_ELEMENTS(bgp_vrf->l2vnis, node, next, vpn))
4462 bgpevpn_unlink_from_l3vni(vpn);
4463
4464 /* Delete the instance if it was autocreated */
4465 if (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO))
4466 bgp_delete(bgp_vrf);
4467
4468 return 0;
4469 }
4470
4471 /*
4472 * Handle del of a local VNI.
4473 */
4474 int bgp_evpn_local_vni_del(struct bgp *bgp, vni_t vni)
4475 {
4476 struct bgpevpn *vpn;
4477
4478 if (!bgp->vnihash) {
4479 zlog_err("%u: VNI hash not created", bgp->vrf_id);
4480 return -1;
4481 }
4482
4483 /* Locate VNI hash */
4484 vpn = bgp_evpn_lookup_vni(bgp, vni);
4485 if (!vpn) {
4486 if (bgp_debug_zebra(NULL))
4487 zlog_warn(
4488 "%u: VNI hash entry for VNI %u not found at DEL",
4489 bgp->vrf_id, vni);
4490 return 0;
4491 }
4492
4493 /* Remove all local EVPN routes and schedule for processing (to
4494 * withdraw from peers).
4495 */
4496 delete_routes_for_vni(bgp, vpn);
4497
4498 /*
4499 * tunnel is no longer active, del tunnel ip address from tip_hash
4500 */
4501 bgp_tip_del(bgp, &vpn->originator_ip);
4502
4503 /* Clear "live" flag and see if hash needs to be freed. */
4504 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
4505 if (!is_vni_configured(vpn))
4506 bgp_evpn_free(bgp, vpn);
4507
4508 return 0;
4509 }
4510
4511 /*
4512 * Handle add (or update) of a local VNI. The VNI changes we care
4513 * about are for the local-tunnel-ip and the (tenant) VRF.
4514 */
4515 int bgp_evpn_local_vni_add(struct bgp *bgp, vni_t vni,
4516 struct in_addr originator_ip, vrf_id_t tenant_vrf_id)
4517 {
4518 struct bgpevpn *vpn;
4519 struct prefix_evpn p;
4520
4521 if (!bgp->vnihash) {
4522 zlog_err("%u: VNI hash not created", bgp->vrf_id);
4523 return -1;
4524 }
4525
4526 /* Lookup VNI. If present and no change, exit. */
4527 vpn = bgp_evpn_lookup_vni(bgp, vni);
4528 if (vpn) {
4529
4530 if (is_vni_live(vpn)
4531 && IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip)
4532 && vpn->tenant_vrf_id == tenant_vrf_id)
4533 /* Probably some other param has changed that we don't
4534 * care about. */
4535 return 0;
4536
4537 /* Update tenant_vrf_id if it has changed. */
4538 if (vpn->tenant_vrf_id != tenant_vrf_id) {
4539 bgpevpn_unlink_from_l3vni(vpn);
4540 vpn->tenant_vrf_id = tenant_vrf_id;
4541 bgpevpn_link_to_l3vni(vpn);
4542 }
4543
4544 /* If tunnel endpoint IP has changed, update (and delete prior
4545 * type-3 route, if needed.)
4546 */
4547 if (!IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip))
4548 handle_tunnel_ip_change(bgp, vpn, originator_ip);
4549
4550 /* Update all routes with new endpoint IP and/or export RT
4551 * for VRFs
4552 */
4553 if (is_vni_live(vpn))
4554 update_routes_for_vni(bgp, vpn);
4555 }
4556
4557 /* Create or update as appropriate. */
4558 if (!vpn) {
4559 vpn = bgp_evpn_new(bgp, vni, originator_ip, tenant_vrf_id);
4560 if (!vpn) {
4561 zlog_err(
4562 "%u: Failed to allocate VNI entry for VNI %u - at Add",
4563 bgp->vrf_id, vni);
4564 return -1;
4565 }
4566 }
4567
4568 /* if the VNI is live already, there is nothing more to do */
4569 if (is_vni_live(vpn))
4570 return 0;
4571
4572 /* Mark as "live" */
4573 SET_FLAG(vpn->flags, VNI_FLAG_LIVE);
4574
4575 /* tunnel is now active, add tunnel-ip to db */
4576 bgp_tip_add(bgp, &originator_ip);
4577
4578 /* filter routes as nexthop database has changed */
4579 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
4580
4581 /* Create EVPN type-3 route and schedule for processing. */
4582 build_evpn_type3_prefix(&p, vpn->originator_ip);
4583 if (update_evpn_route(bgp, vpn, &p, 0)) {
4584 zlog_err("%u: Type3 route creation failure for VNI %u",
4585 bgp->vrf_id, vni);
4586 return -1;
4587 }
4588
4589 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
4590 * VNI,
4591 * install them.
4592 */
4593 install_routes_for_vni(bgp, vpn);
4594
4595 /* If we are advertising gateway mac-ip
4596 It needs to be conveyed again to zebra */
4597 bgp_zebra_advertise_gw_macip(bgp, vpn->advertise_gw_macip, vpn->vni);
4598
4599 return 0;
4600 }
4601
4602 /*
4603 * Cleanup EVPN information on disable - Need to delete and withdraw
4604 * EVPN routes from peers.
4605 */
4606 void bgp_evpn_cleanup_on_disable(struct bgp *bgp)
4607 {
4608 hash_iterate(bgp->vnihash, (void (*)(struct hash_backet *,
4609 void *))cleanup_vni_on_disable,
4610 bgp);
4611 }
4612
4613 /*
4614 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
4615 * BGP instance (default) is being freed.
4616 */
4617 void bgp_evpn_cleanup(struct bgp *bgp)
4618 {
4619 if (bgp->vnihash)
4620 hash_iterate(bgp->vnihash, (void (*)(struct hash_backet *,
4621 void *))free_vni_entry,
4622 bgp);
4623 if (bgp->import_rt_hash)
4624 hash_free(bgp->import_rt_hash);
4625 bgp->import_rt_hash = NULL;
4626 if (bgp->vrf_import_rt_hash)
4627 hash_free(bgp->vrf_import_rt_hash);
4628 bgp->vrf_import_rt_hash = NULL;
4629 if (bgp->vnihash)
4630 hash_free(bgp->vnihash);
4631 bgp->vnihash = NULL;
4632 if (bgp->vrf_import_rtl)
4633 list_delete_and_null(&bgp->vrf_import_rtl);
4634 if (bgp->vrf_export_rtl)
4635 list_delete_and_null(&bgp->vrf_export_rtl);
4636 if (bgp->l2vnis)
4637 list_delete_and_null(&bgp->l2vnis);
4638 }
4639
4640 /*
4641 * Initialization for EVPN
4642 * Create
4643 * VNI hash table
4644 * hash for RT to VNI
4645 */
4646 void bgp_evpn_init(struct bgp *bgp)
4647 {
4648 bgp->vnihash =
4649 hash_create(vni_hash_key_make, vni_hash_cmp, "BGP VNI Hash");
4650 bgp->import_rt_hash =
4651 hash_create(import_rt_hash_key_make, import_rt_hash_cmp,
4652 "BGP Import RT Hash");
4653 bgp->vrf_import_rt_hash =
4654 hash_create(vrf_import_rt_hash_key_make, vrf_import_rt_hash_cmp,
4655 "BGP VRF Import RT Hash");
4656 bgp->vrf_import_rtl = list_new();
4657 bgp->vrf_import_rtl->cmp =
4658 (int (*)(void *, void *))evpn_route_target_cmp;
4659 bgp->vrf_import_rtl->del = evpn_xxport_delete_ecomm;
4660 bgp->vrf_export_rtl = list_new();
4661 bgp->vrf_export_rtl->cmp =
4662 (int (*)(void *, void *))evpn_route_target_cmp;
4663 bgp->vrf_export_rtl->del = evpn_xxport_delete_ecomm;
4664 bgp->l2vnis = list_new();
4665 bgp->l2vnis->cmp = (int (*)(void *, void *))vni_hash_cmp;
4666 }
4667
4668 void bgp_evpn_vrf_delete(struct bgp *bgp_vrf)
4669 {
4670 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
4671 }