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