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