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