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