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