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