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