]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_zebra.c
bgpd: new vpn-policy CLI
[mirror_frr.git] / bgpd / bgp_zebra.c
1 /* zebra client
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "command.h"
24 #include "stream.h"
25 #include "network.h"
26 #include "prefix.h"
27 #include "log.h"
28 #include "sockunion.h"
29 #include "zclient.h"
30 #include "routemap.h"
31 #include "thread.h"
32 #include "queue.h"
33 #include "memory.h"
34 #include "lib/json.h"
35 #include "lib/bfd.h"
36 #include "filter.h"
37 #include "mpls.h"
38 #include "vxlan.h"
39
40 #include "bgpd/bgpd.h"
41 #include "bgpd/bgp_route.h"
42 #include "bgpd/bgp_attr.h"
43 #include "bgpd/bgp_nexthop.h"
44 #include "bgpd/bgp_zebra.h"
45 #include "bgpd/bgp_fsm.h"
46 #include "bgpd/bgp_debug.h"
47 #include "bgpd/bgp_mpath.h"
48 #include "bgpd/bgp_nexthop.h"
49 #include "bgpd/bgp_nht.h"
50 #include "bgpd/bgp_bfd.h"
51 #include "bgpd/bgp_label.h"
52 #if ENABLE_BGP_VNC
53 #include "bgpd/rfapi/rfapi_backend.h"
54 #include "bgpd/rfapi/vnc_export_bgp.h"
55 #endif
56 #include "bgpd/bgp_evpn.h"
57 #include "bgpd/bgp_mplsvpn.h"
58
59 /* All information about zebra. */
60 struct zclient *zclient = NULL;
61
62 /* Can we install into zebra? */
63 static inline int bgp_install_info_to_zebra(struct bgp *bgp)
64 {
65 if (zclient->sock <= 0)
66 return 0;
67
68 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
69 return 0;
70
71 return 1;
72 }
73
74 int zclient_num_connects;
75
76 /* Router-id update message from zebra. */
77 static int bgp_router_id_update(int command, struct zclient *zclient,
78 zebra_size_t length, vrf_id_t vrf_id)
79 {
80 struct prefix router_id;
81
82 zebra_router_id_update_read(zclient->ibuf, &router_id);
83
84 if (BGP_DEBUG(zebra, ZEBRA)) {
85 char buf[PREFIX2STR_BUFFER];
86 prefix2str(&router_id, buf, sizeof(buf));
87 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id, buf);
88 }
89
90 bgp_router_id_zebra_bump(vrf_id, &router_id);
91 return 0;
92 }
93
94 /* Nexthop update message from zebra. */
95 static int bgp_read_nexthop_update(int command, struct zclient *zclient,
96 zebra_size_t length, vrf_id_t vrf_id)
97 {
98 bgp_parse_nexthop_update(command, vrf_id);
99 return 0;
100 }
101
102 static int bgp_read_import_check_update(int command, struct zclient *zclient,
103 zebra_size_t length, vrf_id_t vrf_id)
104 {
105 bgp_parse_nexthop_update(command, vrf_id);
106 return 0;
107 }
108
109 /* Set or clear interface on which unnumbered neighbor is configured. This
110 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
111 * interface.
112 */
113 static void bgp_update_interface_nbrs(struct bgp *bgp, struct interface *ifp,
114 struct interface *upd_ifp)
115 {
116 struct listnode *node, *nnode;
117 struct peer *peer;
118
119 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
120 if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)) {
121 if (upd_ifp) {
122 peer->ifp = upd_ifp;
123 bgp_zebra_initiate_radv(bgp, peer);
124 } else {
125 bgp_zebra_terminate_radv(bgp, peer);
126 peer->ifp = upd_ifp;
127 }
128 }
129 }
130 }
131
132 static int bgp_read_fec_update(int command, struct zclient *zclient,
133 zebra_size_t length)
134 {
135 bgp_parse_fec_update();
136 return 0;
137 }
138
139 static void bgp_start_interface_nbrs(struct bgp *bgp, struct interface *ifp)
140 {
141 struct listnode *node, *nnode;
142 struct peer *peer;
143
144 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
145 if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)
146 && peer->status != Established) {
147 if (peer_active(peer))
148 BGP_EVENT_ADD(peer, BGP_Stop);
149 BGP_EVENT_ADD(peer, BGP_Start);
150 }
151 }
152 }
153
154 static void bgp_nbr_connected_add(struct bgp *bgp, struct nbr_connected *ifc)
155 {
156 struct listnode *node;
157 struct connected *connected;
158 struct interface *ifp;
159 struct prefix *p;
160
161 /* Kick-off the FSM for any relevant peers only if there is a
162 * valid local address on the interface.
163 */
164 ifp = ifc->ifp;
165 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
166 p = connected->address;
167 if (p->family == AF_INET6
168 && IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
169 break;
170 }
171 if (!connected)
172 return;
173
174 bgp_start_interface_nbrs(bgp, ifp);
175 }
176
177 static void bgp_nbr_connected_delete(struct bgp *bgp, struct nbr_connected *ifc,
178 int del)
179 {
180 struct listnode *node, *nnode;
181 struct peer *peer;
182 struct interface *ifp;
183
184 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
185 if (peer->conf_if
186 && (strcmp(peer->conf_if, ifc->ifp->name) == 0)) {
187 peer->last_reset = PEER_DOWN_NBR_ADDR_DEL;
188 BGP_EVENT_ADD(peer, BGP_Stop);
189 }
190 }
191 /* Free neighbor also, if we're asked to. */
192 if (del) {
193 ifp = ifc->ifp;
194 listnode_delete(ifp->nbr_connected, ifc);
195 nbr_connected_free(ifc);
196 }
197 }
198
199 /* Inteface addition message from zebra. */
200 static int bgp_interface_add(int command, struct zclient *zclient,
201 zebra_size_t length, vrf_id_t vrf_id)
202 {
203 struct interface *ifp;
204 struct bgp *bgp;
205
206 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
207 if (!ifp) // unexpected
208 return 0;
209
210 if (BGP_DEBUG(zebra, ZEBRA) && ifp)
211 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id, ifp->name);
212
213 bgp = bgp_lookup_by_vrf_id(vrf_id);
214 if (!bgp)
215 return 0;
216
217 bgp_update_interface_nbrs(bgp, ifp, ifp);
218 return 0;
219 }
220
221 static int bgp_interface_delete(int command, struct zclient *zclient,
222 zebra_size_t length, vrf_id_t vrf_id)
223 {
224 struct stream *s;
225 struct interface *ifp;
226 struct bgp *bgp;
227
228 bgp = bgp_lookup_by_vrf_id(vrf_id);
229 if (!bgp)
230 return 0;
231
232 s = zclient->ibuf;
233 ifp = zebra_interface_state_read(s, vrf_id);
234 if (!ifp) /* This may happen if we've just unregistered for a VRF. */
235 return 0;
236
237 if (BGP_DEBUG(zebra, ZEBRA))
238 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id, ifp->name);
239
240 bgp_update_interface_nbrs(bgp, ifp, NULL);
241
242 if_set_index(ifp, IFINDEX_INTERNAL);
243 return 0;
244 }
245
246 static int bgp_interface_up(int command, struct zclient *zclient,
247 zebra_size_t length, vrf_id_t vrf_id)
248 {
249 struct stream *s;
250 struct interface *ifp;
251 struct connected *c;
252 struct nbr_connected *nc;
253 struct listnode *node, *nnode;
254 struct bgp *bgp;
255
256 bgp = bgp_lookup_by_vrf_id(vrf_id);
257 if (!bgp)
258 return 0;
259
260 s = zclient->ibuf;
261 ifp = zebra_interface_state_read(s, vrf_id);
262
263 if (!ifp)
264 return 0;
265
266 if (BGP_DEBUG(zebra, ZEBRA))
267 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id, ifp->name);
268
269 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
270 bgp_connected_add(bgp, c);
271
272 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
273 bgp_nbr_connected_add(bgp, nc);
274
275 return 0;
276 }
277
278 static int bgp_interface_down(int command, struct zclient *zclient,
279 zebra_size_t length, vrf_id_t vrf_id)
280 {
281 struct stream *s;
282 struct interface *ifp;
283 struct connected *c;
284 struct nbr_connected *nc;
285 struct listnode *node, *nnode;
286 struct bgp *bgp;
287
288 bgp = bgp_lookup_by_vrf_id(vrf_id);
289 if (!bgp)
290 return 0;
291
292 s = zclient->ibuf;
293 ifp = zebra_interface_state_read(s, vrf_id);
294 if (!ifp)
295 return 0;
296
297 if (BGP_DEBUG(zebra, ZEBRA))
298 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id, ifp->name);
299
300 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
301 bgp_connected_delete(bgp, c);
302
303 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
304 bgp_nbr_connected_delete(bgp, nc, 1);
305
306 /* Fast external-failover */
307 {
308 struct peer *peer;
309
310 if (CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
311 return 0;
312
313 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
314 #if defined(HAVE_CUMULUS)
315 /* Take down directly connected EBGP peers as well as
316 * 1-hop BFD
317 * tracked (directly connected) IBGP peers.
318 */
319 if ((peer->ttl != 1) && (peer->gtsm_hops != 1)
320 && (!peer->bfd_info
321 || bgp_bfd_is_peer_multihop(peer)))
322 #else
323 /* Take down directly connected EBGP peers */
324 if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
325 #endif
326 continue;
327
328 if (ifp == peer->nexthop.ifp) {
329 BGP_EVENT_ADD(peer, BGP_Stop);
330 peer->last_reset = PEER_DOWN_IF_DOWN;
331 }
332 }
333 }
334
335 return 0;
336 }
337
338 static int bgp_interface_address_add(int command, struct zclient *zclient,
339 zebra_size_t length, vrf_id_t vrf_id)
340 {
341 struct connected *ifc;
342 struct bgp *bgp;
343
344 bgp = bgp_lookup_by_vrf_id(vrf_id);
345 if (!bgp)
346 return 0;
347
348 ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
349
350 if (ifc == NULL)
351 return 0;
352
353 if (bgp_debug_zebra(ifc->address)) {
354 char buf[PREFIX2STR_BUFFER];
355 prefix2str(ifc->address, buf, sizeof(buf));
356 zlog_debug("Rx Intf address add VRF %u IF %s addr %s", vrf_id,
357 ifc->ifp->name, buf);
358 }
359
360 if (if_is_operative(ifc->ifp)) {
361 bgp_connected_add(bgp, ifc);
362
363 /* If we have learnt of any neighbors on this interface,
364 * check to kick off any BGP interface-based neighbors,
365 * but only if this is a link-local address.
366 */
367 if (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6)
368 && !list_isempty(ifc->ifp->nbr_connected))
369 bgp_start_interface_nbrs(bgp, ifc->ifp);
370 }
371
372 return 0;
373 }
374
375 static int bgp_interface_address_delete(int command, struct zclient *zclient,
376 zebra_size_t length, vrf_id_t vrf_id)
377 {
378 struct connected *ifc;
379 struct bgp *bgp;
380
381 bgp = bgp_lookup_by_vrf_id(vrf_id);
382 if (!bgp)
383 return 0;
384
385 ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
386
387 if (ifc == NULL)
388 return 0;
389
390 if (bgp_debug_zebra(ifc->address)) {
391 char buf[PREFIX2STR_BUFFER];
392 prefix2str(ifc->address, buf, sizeof(buf));
393 zlog_debug("Rx Intf address del VRF %u IF %s addr %s", vrf_id,
394 ifc->ifp->name, buf);
395 }
396
397 if (if_is_operative(ifc->ifp)) {
398 bgp_connected_delete(bgp, ifc);
399 }
400
401 connected_free(ifc);
402
403 return 0;
404 }
405
406 static int bgp_interface_nbr_address_add(int command, struct zclient *zclient,
407 zebra_size_t length, vrf_id_t vrf_id)
408 {
409 struct nbr_connected *ifc = NULL;
410 struct bgp *bgp;
411
412 ifc = zebra_interface_nbr_address_read(command, zclient->ibuf, vrf_id);
413
414 if (ifc == NULL)
415 return 0;
416
417 if (bgp_debug_zebra(ifc->address)) {
418 char buf[PREFIX2STR_BUFFER];
419 prefix2str(ifc->address, buf, sizeof(buf));
420 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s", vrf_id,
421 ifc->ifp->name, buf);
422 }
423
424 if (if_is_operative(ifc->ifp)) {
425 bgp = bgp_lookup_by_vrf_id(vrf_id);
426 if (bgp)
427 bgp_nbr_connected_add(bgp, ifc);
428 }
429
430 return 0;
431 }
432
433 static int bgp_interface_nbr_address_delete(int command,
434 struct zclient *zclient,
435 zebra_size_t length,
436 vrf_id_t vrf_id)
437 {
438 struct nbr_connected *ifc = NULL;
439 struct bgp *bgp;
440
441 ifc = zebra_interface_nbr_address_read(command, zclient->ibuf, vrf_id);
442
443 if (ifc == NULL)
444 return 0;
445
446 if (bgp_debug_zebra(ifc->address)) {
447 char buf[PREFIX2STR_BUFFER];
448 prefix2str(ifc->address, buf, sizeof(buf));
449 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s", vrf_id,
450 ifc->ifp->name, buf);
451 }
452
453 if (if_is_operative(ifc->ifp)) {
454 bgp = bgp_lookup_by_vrf_id(vrf_id);
455 if (bgp)
456 bgp_nbr_connected_delete(bgp, ifc, 0);
457 }
458
459 nbr_connected_free(ifc);
460
461 return 0;
462 }
463
464 /* VRF update for an interface. */
465 static int bgp_interface_vrf_update(int command, struct zclient *zclient,
466 zebra_size_t length, vrf_id_t vrf_id)
467 {
468 struct interface *ifp;
469 vrf_id_t new_vrf_id;
470 struct connected *c;
471 struct nbr_connected *nc;
472 struct listnode *node, *nnode;
473 struct bgp *bgp;
474
475 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
476 &new_vrf_id);
477 if (!ifp)
478 return 0;
479
480 if (BGP_DEBUG(zebra, ZEBRA) && ifp)
481 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id,
482 ifp->name, new_vrf_id);
483
484 bgp = bgp_lookup_by_vrf_id(vrf_id);
485 if (!bgp)
486 return 0;
487
488 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
489 bgp_connected_delete(bgp, c);
490
491 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
492 bgp_nbr_connected_delete(bgp, nc, 1);
493
494 /* Fast external-failover */
495 {
496 struct peer *peer;
497
498 if (CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
499 return 0;
500
501 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
502 if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
503 continue;
504
505 if (ifp == peer->nexthop.ifp)
506 BGP_EVENT_ADD(peer, BGP_Stop);
507 }
508 }
509
510 if_update_to_new_vrf(ifp, new_vrf_id);
511
512 bgp = bgp_lookup_by_vrf_id(new_vrf_id);
513 if (!bgp)
514 return 0;
515
516 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
517 bgp_connected_add(bgp, c);
518
519 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
520 bgp_nbr_connected_add(bgp, nc);
521 return 0;
522 }
523
524 /* Zebra route add and delete treatment. */
525 static int zebra_read_route(int command, struct zclient *zclient,
526 zebra_size_t length, vrf_id_t vrf_id)
527 {
528 enum nexthop_types_t nhtype;
529 struct zapi_route api;
530 union g_addr nexthop;
531 ifindex_t ifindex;
532 int add, i;
533 struct bgp *bgp;
534
535 bgp = bgp_lookup_by_vrf_id(vrf_id);
536 if (!bgp)
537 return 0;
538
539 if (zapi_route_decode(zclient->ibuf, &api) < 0)
540 return -1;
541
542 /* we completely ignore srcdest routes for now. */
543 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
544 return 0;
545
546 /* ignore link-local address. */
547 if (api.prefix.family == AF_INET6
548 && IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6))
549 return 0;
550
551 nexthop = api.nexthops[0].gate;
552 ifindex = api.nexthops[0].ifindex;
553 nhtype = api.nexthops[0].type;
554
555 add = (command == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
556 if (add) {
557 /*
558 * The ADD message is actually an UPDATE and there is no
559 * explicit DEL
560 * for a prior redistributed route, if any. So, perform an
561 * implicit
562 * DEL processing for the same redistributed route from any
563 * other
564 * source type.
565 */
566 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
567 if (i != api.type)
568 bgp_redistribute_delete(bgp, &api.prefix, i,
569 api.instance);
570 }
571
572 /* Now perform the add/update. */
573 bgp_redistribute_add(bgp, &api.prefix, &nexthop, ifindex,
574 nhtype, api.metric, api.type, api.instance,
575 api.tag);
576 } else {
577 bgp_redistribute_delete(bgp, &api.prefix, api.type,
578 api.instance);
579 }
580
581 if (bgp_debug_zebra(&api.prefix)) {
582 char buf[2][PREFIX_STRLEN];
583
584 prefix2str(&api.prefix, buf[0], sizeof(buf[0]));
585 inet_ntop(api.prefix.family, &nexthop, buf[1], sizeof(buf[1]));
586 zlog_debug(
587 "Rx route %s VRF %u %s[%d] %s "
588 "nexthop %s metric %u tag %" ROUTE_TAG_PRI,
589 (add) ? "add" : "delete", vrf_id,
590 zebra_route_string(api.type), api.instance, buf[0],
591 buf[1], api.metric, api.tag);
592 }
593
594 return 0;
595 }
596
597 struct interface *if_lookup_by_ipv4(struct in_addr *addr, vrf_id_t vrf_id)
598 {
599 struct vrf *vrf;
600 struct listnode *cnode;
601 struct interface *ifp;
602 struct connected *connected;
603 struct prefix_ipv4 p;
604 struct prefix *cp;
605
606 vrf = vrf_lookup_by_id(vrf_id);
607 if (!vrf)
608 return NULL;
609
610 p.family = AF_INET;
611 p.prefix = *addr;
612 p.prefixlen = IPV4_MAX_BITLEN;
613
614 FOR_ALL_INTERFACES (vrf, ifp) {
615 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
616 cp = connected->address;
617
618 if (cp->family == AF_INET)
619 if (prefix_match(cp, (struct prefix *)&p))
620 return ifp;
621 }
622 }
623 return NULL;
624 }
625
626 struct interface *if_lookup_by_ipv4_exact(struct in_addr *addr, vrf_id_t vrf_id)
627 {
628 struct vrf *vrf;
629 struct listnode *cnode;
630 struct interface *ifp;
631 struct connected *connected;
632 struct prefix *cp;
633
634 vrf = vrf_lookup_by_id(vrf_id);
635 if (!vrf)
636 return NULL;
637
638 FOR_ALL_INTERFACES (vrf, ifp) {
639 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
640 cp = connected->address;
641
642 if (cp->family == AF_INET)
643 if (IPV4_ADDR_SAME(&cp->u.prefix4, addr))
644 return ifp;
645 }
646 }
647 return NULL;
648 }
649
650 struct interface *if_lookup_by_ipv6(struct in6_addr *addr, ifindex_t ifindex,
651 vrf_id_t vrf_id)
652 {
653 struct vrf *vrf;
654 struct listnode *cnode;
655 struct interface *ifp;
656 struct connected *connected;
657 struct prefix_ipv6 p;
658 struct prefix *cp;
659
660 vrf = vrf_lookup_by_id(vrf_id);
661 if (!vrf)
662 return NULL;
663
664 p.family = AF_INET6;
665 p.prefix = *addr;
666 p.prefixlen = IPV6_MAX_BITLEN;
667
668 FOR_ALL_INTERFACES (vrf, ifp) {
669 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
670 cp = connected->address;
671
672 if (cp->family == AF_INET6)
673 if (prefix_match(cp, (struct prefix *)&p)) {
674 if (IN6_IS_ADDR_LINKLOCAL(
675 &cp->u.prefix6)) {
676 if (ifindex == ifp->ifindex)
677 return ifp;
678 } else
679 return ifp;
680 }
681 }
682 }
683 return NULL;
684 }
685
686 struct interface *if_lookup_by_ipv6_exact(struct in6_addr *addr,
687 ifindex_t ifindex, vrf_id_t vrf_id)
688 {
689 struct vrf *vrf;
690 struct listnode *cnode;
691 struct interface *ifp;
692 struct connected *connected;
693 struct prefix *cp;
694
695 vrf = vrf_lookup_by_id(vrf_id);
696 if (!vrf)
697 return NULL;
698
699 FOR_ALL_INTERFACES (vrf, ifp) {
700 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
701 cp = connected->address;
702
703 if (cp->family == AF_INET6)
704 if (IPV6_ADDR_SAME(&cp->u.prefix6, addr)) {
705 if (IN6_IS_ADDR_LINKLOCAL(
706 &cp->u.prefix6)) {
707 if (ifindex == ifp->ifindex)
708 return ifp;
709 } else
710 return ifp;
711 }
712 }
713 }
714 return NULL;
715 }
716
717 static int if_get_ipv6_global(struct interface *ifp, struct in6_addr *addr)
718 {
719 struct listnode *cnode;
720 struct connected *connected;
721 struct prefix *cp;
722
723 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
724 cp = connected->address;
725
726 if (cp->family == AF_INET6)
727 if (!IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
728 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
729 return 1;
730 }
731 }
732 return 0;
733 }
734
735 static int if_get_ipv6_local(struct interface *ifp, struct in6_addr *addr)
736 {
737 struct listnode *cnode;
738 struct connected *connected;
739 struct prefix *cp;
740
741 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
742 cp = connected->address;
743
744 if (cp->family == AF_INET6)
745 if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
746 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
747 return 1;
748 }
749 }
750 return 0;
751 }
752
753 static int if_get_ipv4_address(struct interface *ifp, struct in_addr *addr)
754 {
755 struct listnode *cnode;
756 struct connected *connected;
757 struct prefix *cp;
758
759 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
760 cp = connected->address;
761 if ((cp->family == AF_INET)
762 && !ipv4_martian(&(cp->u.prefix4))) {
763 *addr = cp->u.prefix4;
764 return 1;
765 }
766 }
767 return 0;
768 }
769
770 int bgp_nexthop_set(union sockunion *local, union sockunion *remote,
771 struct bgp_nexthop *nexthop, struct peer *peer)
772 {
773 int ret = 0;
774 struct interface *ifp = NULL;
775
776 memset(nexthop, 0, sizeof(struct bgp_nexthop));
777
778 if (!local)
779 return -1;
780 if (!remote)
781 return -1;
782
783 if (local->sa.sa_family == AF_INET) {
784 nexthop->v4 = local->sin.sin_addr;
785 if (peer->update_if)
786 ifp = if_lookup_by_name(peer->update_if,
787 peer->bgp->vrf_id);
788 else
789 ifp = if_lookup_by_ipv4_exact(&local->sin.sin_addr,
790 peer->bgp->vrf_id);
791 }
792 if (local->sa.sa_family == AF_INET6) {
793 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
794 if (peer->conf_if || peer->ifname)
795 ifp = if_lookup_by_name(peer->conf_if
796 ? peer->conf_if
797 : peer->ifname,
798 peer->bgp->vrf_id);
799 } else if (peer->update_if)
800 ifp = if_lookup_by_name(peer->update_if,
801 peer->bgp->vrf_id);
802 else
803 ifp = if_lookup_by_ipv6_exact(&local->sin6.sin6_addr,
804 local->sin6.sin6_scope_id,
805 peer->bgp->vrf_id);
806 }
807
808 if (!ifp)
809 return -1;
810
811 nexthop->ifp = ifp;
812
813 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
814 if (local->sa.sa_family == AF_INET) {
815 /* IPv6 nexthop*/
816 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
817
818 if (!ret) {
819 /* There is no global nexthop. Use link-local address as
820 * both the
821 * global and link-local nexthop. In this scenario, the
822 * expectation
823 * for interop is that the network admin would use a
824 * route-map to
825 * specify the global IPv6 nexthop.
826 */
827 if_get_ipv6_local(ifp, &nexthop->v6_global);
828 memcpy(&nexthop->v6_local, &nexthop->v6_global,
829 IPV6_MAX_BYTELEN);
830 } else
831 if_get_ipv6_local(ifp, &nexthop->v6_local);
832
833 if (if_lookup_by_ipv4(&remote->sin.sin_addr, peer->bgp->vrf_id))
834 peer->shared_network = 1;
835 else
836 peer->shared_network = 0;
837 }
838
839 /* IPv6 connection, fetch and store IPv4 local address if any. */
840 if (local->sa.sa_family == AF_INET6) {
841 struct interface *direct = NULL;
842
843 /* IPv4 nexthop. */
844 ret = if_get_ipv4_address(ifp, &nexthop->v4);
845 if (!ret && peer->local_id.s_addr)
846 nexthop->v4 = peer->local_id;
847
848 /* Global address*/
849 if (!IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
850 memcpy(&nexthop->v6_global, &local->sin6.sin6_addr,
851 IPV6_MAX_BYTELEN);
852
853 /* If directory connected set link-local address. */
854 direct = if_lookup_by_ipv6(&remote->sin6.sin6_addr,
855 remote->sin6.sin6_scope_id,
856 peer->bgp->vrf_id);
857 if (direct)
858 if_get_ipv6_local(ifp, &nexthop->v6_local);
859 } else
860 /* Link-local address. */
861 {
862 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
863
864 /* If there is no global address. Set link-local
865 address as
866 global. I know this break RFC specification... */
867 /* In this scenario, the expectation for interop is that
868 * the
869 * network admin would use a route-map to specify the
870 * global
871 * IPv6 nexthop.
872 */
873 if (!ret)
874 memcpy(&nexthop->v6_global,
875 &local->sin6.sin6_addr,
876 IPV6_MAX_BYTELEN);
877 /* Always set the link-local address */
878 memcpy(&nexthop->v6_local, &local->sin6.sin6_addr,
879 IPV6_MAX_BYTELEN);
880 }
881
882 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)
883 || if_lookup_by_ipv6(&remote->sin6.sin6_addr,
884 remote->sin6.sin6_scope_id,
885 peer->bgp->vrf_id))
886 peer->shared_network = 1;
887 else
888 peer->shared_network = 0;
889 }
890
891 /* KAME stack specific treatment. */
892 #ifdef KAME
893 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_global)
894 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_global)) {
895 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_global, 0);
896 }
897 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_local)
898 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_local)) {
899 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_local, 0);
900 }
901 #endif /* KAME */
902
903 /* If we have identified the local interface, there is no error for now.
904 */
905 return 0;
906 }
907
908 static struct in6_addr *bgp_info_to_ipv6_nexthop(struct bgp_info *info)
909 {
910 struct in6_addr *nexthop = NULL;
911
912 /* Only global address nexthop exists. */
913 if (info->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL)
914 nexthop = &info->attr->mp_nexthop_global;
915
916 /* If both global and link-local address present. */
917 if (info->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
918 /* Check if route-map is set to prefer global over link-local */
919 if (info->attr->mp_nexthop_prefer_global)
920 nexthop = &info->attr->mp_nexthop_global;
921 else {
922 /* Workaround for Cisco's nexthop bug. */
923 if (IN6_IS_ADDR_UNSPECIFIED(
924 &info->attr->mp_nexthop_global)
925 && info->peer->su_remote->sa.sa_family == AF_INET6)
926 nexthop =
927 &info->peer->su_remote->sin6.sin6_addr;
928 else
929 nexthop = &info->attr->mp_nexthop_local;
930 }
931 }
932
933 return nexthop;
934 }
935
936 static int bgp_table_map_apply(struct route_map *map, struct prefix *p,
937 struct bgp_info *info)
938 {
939 route_map_result_t ret;
940
941 ret = route_map_apply(map, p, RMAP_BGP, info);
942 bgp_attr_flush(info->attr);
943
944 if (ret != RMAP_DENYMATCH)
945 return 1;
946
947 if (bgp_debug_zebra(p)) {
948 if (p->family == AF_INET) {
949 char buf[2][INET_ADDRSTRLEN];
950 zlog_debug(
951 "Zebra rmap deny: IPv4 route %s/%d nexthop %s",
952 inet_ntop(AF_INET, &p->u.prefix4, buf[0],
953 sizeof(buf[0])),
954 p->prefixlen,
955 inet_ntop(AF_INET, &info->attr->nexthop, buf[1],
956 sizeof(buf[1])));
957 }
958 if (p->family == AF_INET6) {
959 char buf[2][INET6_ADDRSTRLEN];
960 zlog_debug(
961 "Zebra rmap deny: IPv6 route %s/%d nexthop %s",
962 inet_ntop(AF_INET6, &p->u.prefix6, buf[0],
963 sizeof(buf[0])),
964 p->prefixlen,
965 inet_ntop(AF_INET6,
966 bgp_info_to_ipv6_nexthop(info),
967 buf[1], sizeof(buf[1])));
968 }
969 }
970 return 0;
971 }
972
973 void bgp_zebra_announce(struct bgp_node *rn, struct prefix *p,
974 struct bgp_info *info, struct bgp *bgp, afi_t afi,
975 safi_t safi)
976 {
977 struct zapi_route api;
978 struct zapi_nexthop *api_nh;
979 int nh_family;
980 unsigned int valid_nh_count = 0;
981 int has_valid_label = 0;
982 u_char distance;
983 struct peer *peer;
984 struct bgp_info *mpinfo;
985 u_int32_t metric;
986 struct attr local_attr;
987 struct bgp_info local_info;
988 struct bgp_info *mpinfo_cp = &local_info;
989 route_tag_t tag;
990 mpls_label_t label;
991 int nh_othervrf = 0;
992
993 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
994 * know of this instance.
995 */
996 if (!bgp_install_info_to_zebra(bgp))
997 return;
998
999 if (bgp->main_zebra_update_hold)
1000 return;
1001
1002 /*
1003 * vrf leaking support (will have only one nexthop)
1004 */
1005 if (info->extra && info->extra->bgp_orig)
1006 nh_othervrf = 1;
1007
1008 /* Make Zebra API structure. */
1009 memset(&api, 0, sizeof(api));
1010 memcpy(&api.rmac, &(info->attr->rmac), sizeof(struct ethaddr));
1011 api.vrf_id = bgp->vrf_id;
1012 api.type = ZEBRA_ROUTE_BGP;
1013 api.safi = safi;
1014 api.prefix = *p;
1015 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
1016
1017 peer = info->peer;
1018
1019 if (info->type == ZEBRA_ROUTE_BGP
1020 && info->sub_type == BGP_ROUTE_IMPORTED) {
1021
1022 struct bgp_info *bi;
1023
1024 /*
1025 * Look at parent chain for peer sort
1026 */
1027 for (bi = info; bi->extra && bi->extra->parent;
1028 bi = bi->extra->parent) {
1029
1030 peer = ((struct bgp_info *)(bi->extra->parent))->peer;
1031 }
1032 }
1033
1034 tag = info->attr->tag;
1035
1036 /* When we create an aggregate route we must also install a Null0 route
1037 * in
1038 * the RIB */
1039 if (info->sub_type == BGP_ROUTE_AGGREGATE)
1040 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
1041
1042 /* If it is an EVPN route mark as such.
1043 * Currently presence of rmac in attr denotes
1044 * this is an EVPN type-2 route
1045 */
1046 if (!is_zero_mac(&(info->attr->rmac)))
1047 SET_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE);
1048
1049 if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED
1050 || info->sub_type == BGP_ROUTE_AGGREGATE) {
1051 SET_FLAG(api.flags, ZEBRA_FLAG_IBGP);
1052 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1053 }
1054
1055 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
1056 || CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1057 || bgp_flag_check(bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1058
1059 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1060
1061 /* Metric is currently based on the best-path only */
1062 metric = info->attr->med;
1063 for (mpinfo = info; mpinfo; mpinfo = bgp_info_mpath_next(mpinfo)) {
1064 if (valid_nh_count >= multipath_num)
1065 break;
1066
1067 *mpinfo_cp = *mpinfo;
1068
1069 /* Get nexthop address-family */
1070 if (p->family == AF_INET
1071 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp->attr))
1072 nh_family = AF_INET;
1073 else if (p->family == AF_INET6
1074 || (p->family == AF_INET
1075 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp->attr)))
1076 nh_family = AF_INET6;
1077 else
1078 continue;
1079
1080 api_nh = &api.nexthops[valid_nh_count];
1081 api_nh->vrf_id = bgp->vrf_id;
1082
1083 if (nh_family == AF_INET) {
1084 struct in_addr *nexthop;
1085
1086 if (bgp_debug_zebra(&api.prefix)) {
1087 char buf_prefix[PREFIX_STRLEN];
1088 prefix2str(&api.prefix, buf_prefix,
1089 sizeof(buf_prefix));
1090 if (mpinfo->extra) {
1091 zlog_debug(
1092 "%s: p=%s, bgp_is_valid_label: %d",
1093 __func__, buf_prefix,
1094 bgp_is_valid_label(
1095 &mpinfo->extra
1096 ->label[0]));
1097 } else {
1098 zlog_debug(
1099 "%s: p=%s, extra is NULL, no label",
1100 __func__, buf_prefix);
1101 }
1102 }
1103
1104 if (bgp->table_map[afi][safi].name || nh_othervrf) {
1105 /* Copy info and attributes, so the route-map
1106 apply doesn't modify the BGP route info. */
1107 local_attr = *mpinfo->attr;
1108 mpinfo_cp->attr = &local_attr;
1109 if (nh_othervrf) {
1110 /* allow route-map to modify */
1111 local_attr.nexthop =
1112 info->extra->nexthop_orig.u
1113 .prefix4;
1114 }
1115 }
1116
1117 if (bgp->table_map[afi][safi].name) {
1118 if (!bgp_table_map_apply(
1119 bgp->table_map[afi][safi].map, p,
1120 mpinfo_cp))
1121 continue;
1122
1123 /* metric/tag is only allowed to be
1124 * overridden on 1st nexthop */
1125 if (mpinfo == info) {
1126 metric = mpinfo_cp->attr->med;
1127 tag = mpinfo_cp->attr->tag;
1128 }
1129 }
1130
1131 nexthop = &mpinfo_cp->attr->nexthop;
1132
1133 api_nh->gate.ipv4 = *nexthop;
1134 api_nh->vrf_id = nh_othervrf
1135 ? info->extra->bgp_orig->vrf_id
1136 : bgp->vrf_id;
1137 /* EVPN type-2 routes are
1138 programmed as onlink on l3-vni SVI
1139 */
1140 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE))
1141 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1142 else
1143 api_nh->type = NEXTHOP_TYPE_IPV4;
1144 } else {
1145 ifindex_t ifindex;
1146 struct in6_addr *nexthop;
1147
1148 ifindex = 0;
1149
1150 if (bgp->table_map[afi][safi].name || nh_othervrf) {
1151 /* Copy info and attributes, so the route-map
1152 apply doesn't modify the BGP route info. */
1153 local_attr = *mpinfo->attr;
1154 mpinfo_cp->attr = &local_attr;
1155 if (nh_othervrf) {
1156 /* allow route-map to modify */
1157 local_attr.mp_nexthop_global =
1158 info->extra->nexthop_orig.u
1159 .prefix6;
1160 local_attr.mp_nexthop_len =
1161 BGP_ATTR_NHLEN_IPV6_GLOBAL;
1162 }
1163 }
1164
1165 if (bgp->table_map[afi][safi].name) {
1166 /* Copy info and attributes, so the route-map
1167 apply doesn't modify the BGP route info. */
1168 local_attr = *mpinfo->attr;
1169 mpinfo_cp->attr = &local_attr;
1170
1171 if (!bgp_table_map_apply(
1172 bgp->table_map[afi][safi].map, p,
1173 mpinfo_cp))
1174 continue;
1175
1176 /* metric/tag is only allowed to be
1177 * overridden on 1st nexthop */
1178 if (mpinfo == info) {
1179 metric = mpinfo_cp->attr->med;
1180 tag = mpinfo_cp->attr->tag;
1181 }
1182 }
1183 nexthop = bgp_info_to_ipv6_nexthop(mpinfo_cp);
1184
1185 if ((mpinfo == info)
1186 && mpinfo->attr->mp_nexthop_len
1187 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
1188 if (mpinfo->peer->nexthop.ifp)
1189 ifindex = mpinfo->peer->nexthop.ifp
1190 ->ifindex;
1191
1192 if (!ifindex) {
1193 if (mpinfo->peer->conf_if)
1194 ifindex = mpinfo->peer->ifp->ifindex;
1195 else if (mpinfo->peer->ifname)
1196 ifindex = ifname2ifindex(
1197 mpinfo->peer->ifname,
1198 bgp->vrf_id);
1199 else if (mpinfo->peer->nexthop.ifp)
1200 ifindex = mpinfo->peer->nexthop.ifp
1201 ->ifindex;
1202 }
1203 if (ifindex == 0)
1204 continue;
1205
1206 api_nh->gate.ipv6 = *nexthop;
1207 api_nh->ifindex = ifindex;
1208 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1209 /* api_nh->vrf_id is not set for normal case? */
1210 if (nh_othervrf)
1211 api_nh->vrf_id = info->extra->bgp_orig->vrf_id;
1212 }
1213
1214 if (mpinfo->extra
1215 && bgp_is_valid_label(&mpinfo->extra->label[0])
1216 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1217 has_valid_label = 1;
1218 label = label_pton(&mpinfo->extra->label[0]);
1219
1220 api_nh->label_num = 1;
1221 api_nh->labels[0] = label;
1222 }
1223 valid_nh_count++;
1224 }
1225
1226 /* if this is a evpn route we don't have to include the label */
1227 if (has_valid_label && !(CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)))
1228 SET_FLAG(api.message, ZAPI_MESSAGE_LABEL);
1229
1230 if (info->sub_type != BGP_ROUTE_AGGREGATE)
1231 api.nexthop_num = valid_nh_count;
1232
1233 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
1234 api.metric = metric;
1235
1236 if (tag) {
1237 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1238 api.tag = tag;
1239 }
1240
1241 distance = bgp_distance_apply(p, info, afi, safi, bgp);
1242 if (distance) {
1243 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
1244 api.distance = distance;
1245 }
1246
1247 if (bgp_debug_zebra(p)) {
1248 char prefix_buf[PREFIX_STRLEN];
1249 char nh_buf[INET6_ADDRSTRLEN];
1250 char label_buf[20];
1251 int i;
1252
1253 prefix2str(&api.prefix, prefix_buf, sizeof(prefix_buf));
1254 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1255 " count %d",
1256 valid_nh_count ? "add" : "delete", bgp->vrf_id,
1257 prefix_buf, api.metric, api.tag, api.nexthop_num);
1258 for (i = 0; i < api.nexthop_num; i++) {
1259 api_nh = &api.nexthops[i];
1260
1261 if (api_nh->type == NEXTHOP_TYPE_IPV4)
1262 nh_family = AF_INET;
1263 else
1264 nh_family = AF_INET6;
1265 inet_ntop(nh_family, &api_nh->gate, nh_buf,
1266 sizeof(nh_buf));
1267
1268 label_buf[0] = '\0';
1269 if (has_valid_label
1270 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE))
1271 sprintf(label_buf, "label %u",
1272 api_nh->labels[0]);
1273 zlog_debug(" nhop [%d]: %s %s", i + 1, nh_buf,
1274 label_buf);
1275 }
1276 }
1277
1278 zclient_route_send(valid_nh_count ? ZEBRA_ROUTE_ADD
1279 : ZEBRA_ROUTE_DELETE,
1280 zclient, &api);
1281 }
1282
1283 /* Announce all routes of a table to zebra */
1284 void bgp_zebra_announce_table(struct bgp *bgp, afi_t afi, safi_t safi)
1285 {
1286 struct bgp_node *rn;
1287 struct bgp_table *table;
1288 struct bgp_info *ri;
1289
1290 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1291 * know of this instance.
1292 */
1293 if (!bgp_install_info_to_zebra(bgp))
1294 return;
1295
1296 table = bgp->rib[afi][safi];
1297 if (!table)
1298 return;
1299
1300 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn))
1301 for (ri = rn->info; ri; ri = ri->next)
1302 if (CHECK_FLAG(ri->flags, BGP_INFO_SELECTED) &&
1303
1304 (ri->type == ZEBRA_ROUTE_BGP
1305 && (ri->sub_type == BGP_ROUTE_NORMAL
1306 || ri->sub_type == BGP_ROUTE_IMPORTED)))
1307
1308 bgp_zebra_announce(rn, &rn->p, ri, bgp, afi,
1309 safi);
1310 }
1311
1312 void bgp_zebra_withdraw(struct prefix *p, struct bgp_info *info, safi_t safi)
1313 {
1314 struct zapi_route api;
1315 struct peer *peer;
1316
1317 peer = info->peer;
1318 assert(peer);
1319
1320 if (info->type == ZEBRA_ROUTE_BGP
1321 && info->sub_type == BGP_ROUTE_IMPORTED) {
1322
1323 struct bgp_info *bi;
1324
1325 /*
1326 * Look at parent chain for peer sort
1327 */
1328 for (bi = info; bi->extra && bi->extra->parent;
1329 bi = bi->extra->parent) {
1330
1331 peer = ((struct bgp_info *)(bi->extra->parent))->peer;
1332 }
1333 }
1334
1335 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1336 * know of this instance.
1337 */
1338 if (!bgp_install_info_to_zebra(peer->bgp))
1339 return;
1340
1341 memset(&api, 0, sizeof(api));
1342 memcpy(&api.rmac, &(info->attr->rmac), sizeof(struct ethaddr));
1343 api.vrf_id = peer->bgp->vrf_id;
1344 api.type = ZEBRA_ROUTE_BGP;
1345 api.safi = safi;
1346 api.prefix = *p;
1347
1348 /* If it is an EVPN route mark as such.
1349 * Currently presence of rmac in attr denotes
1350 * this is an EVPN type-2 route
1351 */
1352 if (!is_zero_mac(&(info->attr->rmac)))
1353 SET_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE);
1354
1355 if (peer->sort == BGP_PEER_IBGP) {
1356 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1357 SET_FLAG(api.flags, ZEBRA_FLAG_IBGP);
1358 }
1359
1360 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
1361 || CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1362 || bgp_flag_check(peer->bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1363 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1364
1365 if (bgp_debug_zebra(p)) {
1366 char buf[PREFIX_STRLEN];
1367
1368 prefix2str(&api.prefix, buf, sizeof(buf));
1369 zlog_debug("Tx route delete VRF %u %s", peer->bgp->vrf_id, buf);
1370 }
1371
1372 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
1373 }
1374
1375 struct bgp_redist *bgp_redist_lookup(struct bgp *bgp, afi_t afi, u_char type,
1376 u_short instance)
1377 {
1378 struct list *red_list;
1379 struct listnode *node;
1380 struct bgp_redist *red;
1381
1382 red_list = bgp->redist[afi][type];
1383 if (!red_list)
1384 return (NULL);
1385
1386 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
1387 if (red->instance == instance)
1388 return red;
1389
1390 return NULL;
1391 }
1392
1393 struct bgp_redist *bgp_redist_add(struct bgp *bgp, afi_t afi, u_char type,
1394 u_short instance)
1395 {
1396 struct list *red_list;
1397 struct bgp_redist *red;
1398
1399 red = bgp_redist_lookup(bgp, afi, type, instance);
1400 if (red)
1401 return red;
1402
1403 if (!bgp->redist[afi][type])
1404 bgp->redist[afi][type] = list_new();
1405
1406 red_list = bgp->redist[afi][type];
1407 red = (struct bgp_redist *)XCALLOC(MTYPE_BGP_REDIST,
1408 sizeof(struct bgp_redist));
1409 red->instance = instance;
1410
1411 listnode_add(red_list, red);
1412
1413 return red;
1414 }
1415
1416 static void bgp_redist_del(struct bgp *bgp, afi_t afi, u_char type,
1417 u_short instance)
1418 {
1419 struct bgp_redist *red;
1420
1421 red = bgp_redist_lookup(bgp, afi, type, instance);
1422
1423 if (red) {
1424 listnode_delete(bgp->redist[afi][type], red);
1425 XFREE(MTYPE_BGP_REDIST, red);
1426 if (!bgp->redist[afi][type]->count)
1427 list_delete_and_null(&bgp->redist[afi][type]);
1428 }
1429 }
1430
1431 /* Other routes redistribution into BGP. */
1432 int bgp_redistribute_set(struct bgp *bgp, afi_t afi, int type, u_short instance)
1433 {
1434
1435 /* Return if already redistribute flag is set. */
1436 if (instance) {
1437 if (redist_check_instance(&zclient->mi_redist[afi][type],
1438 instance))
1439 return CMD_WARNING;
1440
1441 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1442 } else {
1443 if (vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1444 return CMD_WARNING;
1445
1446 #if ENABLE_BGP_VNC
1447 if (bgp->vrf_id == VRF_DEFAULT
1448 && type == ZEBRA_ROUTE_VNC_DIRECT) {
1449 vnc_export_bgp_enable(
1450 bgp, afi); /* only enables if mode bits cfg'd */
1451 }
1452 #endif
1453
1454 vrf_bitmap_set(zclient->redist[afi][type], bgp->vrf_id);
1455 }
1456
1457 /*
1458 * Don't try to register if we're not connected to Zebra or Zebra
1459 * doesn't know of this instance.
1460 *
1461 * When we come up later well resend if needed.
1462 */
1463 if (!bgp_install_info_to_zebra(bgp))
1464 return CMD_SUCCESS;
1465
1466 if (BGP_DEBUG(zebra, ZEBRA))
1467 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1468 bgp->vrf_id, afi, zebra_route_string(type),
1469 instance);
1470
1471 /* Send distribute add message to zebra. */
1472 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1473 instance, bgp->vrf_id);
1474
1475 return CMD_SUCCESS;
1476 }
1477
1478 int bgp_redistribute_resend(struct bgp *bgp, afi_t afi, int type,
1479 u_short instance)
1480 {
1481 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1482 * know of this instance.
1483 */
1484 if (!bgp_install_info_to_zebra(bgp))
1485 return -1;
1486
1487 if (BGP_DEBUG(zebra, ZEBRA))
1488 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1489 bgp->vrf_id, afi, zebra_route_string(type),
1490 instance);
1491
1492 /* Send distribute add message to zebra. */
1493 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type,
1494 instance, bgp->vrf_id);
1495 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1496 instance, bgp->vrf_id);
1497
1498 return 0;
1499 }
1500
1501 /* Redistribute with route-map specification. */
1502 int bgp_redistribute_rmap_set(struct bgp_redist *red, const char *name)
1503 {
1504 if (red->rmap.name && (strcmp(red->rmap.name, name) == 0))
1505 return 0;
1506
1507 if (red->rmap.name)
1508 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1509 red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
1510 red->rmap.map = route_map_lookup_by_name(name);
1511
1512 return 1;
1513 }
1514
1515 /* Redistribute with metric specification. */
1516 int bgp_redistribute_metric_set(struct bgp *bgp, struct bgp_redist *red,
1517 afi_t afi, int type, u_int32_t metric)
1518 {
1519 struct bgp_node *rn;
1520 struct bgp_info *ri;
1521
1522 if (red->redist_metric_flag && red->redist_metric == metric)
1523 return 0;
1524
1525 red->redist_metric_flag = 1;
1526 red->redist_metric = metric;
1527
1528 for (rn = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); rn;
1529 rn = bgp_route_next(rn)) {
1530 for (ri = rn->info; ri; ri = ri->next) {
1531 if (ri->sub_type == BGP_ROUTE_REDISTRIBUTE
1532 && ri->type == type
1533 && ri->instance == red->instance) {
1534 struct attr *old_attr;
1535 struct attr new_attr;
1536
1537 bgp_attr_dup(&new_attr, ri->attr);
1538 new_attr.med = red->redist_metric;
1539 old_attr = ri->attr;
1540 ri->attr = bgp_attr_intern(&new_attr);
1541 bgp_attr_unintern(&old_attr);
1542
1543 bgp_info_set_flag(rn, ri,
1544 BGP_INFO_ATTR_CHANGED);
1545 bgp_process(bgp, rn, afi, SAFI_UNICAST);
1546 }
1547 }
1548 }
1549
1550 return 1;
1551 }
1552
1553 /* Unset redistribution. */
1554 int bgp_redistribute_unreg(struct bgp *bgp, afi_t afi, int type,
1555 u_short instance)
1556 {
1557 struct bgp_redist *red;
1558
1559 red = bgp_redist_lookup(bgp, afi, type, instance);
1560 if (!red)
1561 return CMD_SUCCESS;
1562
1563 /* Return if zebra connection is disabled. */
1564 if (instance) {
1565 if (!redist_check_instance(&zclient->mi_redist[afi][type],
1566 instance))
1567 return CMD_WARNING;
1568 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1569 } else {
1570 if (!vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1571 return CMD_WARNING;
1572 vrf_bitmap_unset(zclient->redist[afi][type], bgp->vrf_id);
1573 }
1574
1575
1576 if (bgp_install_info_to_zebra(bgp)) {
1577 /* Send distribute delete message to zebra. */
1578 if (BGP_DEBUG(zebra, ZEBRA))
1579 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1580 bgp->vrf_id, afi, zebra_route_string(type),
1581 instance);
1582 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi,
1583 type, instance, bgp->vrf_id);
1584 }
1585
1586 /* Withdraw redistributed routes from current BGP's routing table. */
1587 bgp_redistribute_withdraw(bgp, afi, type, instance);
1588
1589 return CMD_SUCCESS;
1590 }
1591
1592 /* Unset redistribution. */
1593 int bgp_redistribute_unset(struct bgp *bgp, afi_t afi, int type,
1594 u_short instance)
1595 {
1596 struct bgp_redist *red;
1597
1598 /*
1599 * vnc and vpn->vrf checks must be before red check because
1600 * they operate within bgpd irrespective of zebra connection
1601 * status. red lookup fails if there is no zebra connection.
1602 */
1603 #if ENABLE_BGP_VNC
1604 if (bgp->vrf_id == VRF_DEFAULT && type == ZEBRA_ROUTE_VNC_DIRECT) {
1605 vnc_export_bgp_disable(bgp, afi);
1606 }
1607 #endif
1608
1609 red = bgp_redist_lookup(bgp, afi, type, instance);
1610 if (!red)
1611 return CMD_SUCCESS;
1612
1613 bgp_redistribute_unreg(bgp, afi, type, instance);
1614
1615 /* Unset route-map. */
1616 if (red->rmap.name)
1617 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1618 red->rmap.name = NULL;
1619 red->rmap.map = NULL;
1620
1621 /* Unset metric. */
1622 red->redist_metric_flag = 0;
1623 red->redist_metric = 0;
1624
1625 bgp_redist_del(bgp, afi, type, instance);
1626
1627 return CMD_SUCCESS;
1628 }
1629
1630 /* Update redistribute vrf bitmap during triggers like
1631 restart networking or delete/add VRFs */
1632 void bgp_update_redist_vrf_bitmaps(struct bgp *bgp, vrf_id_t old_vrf_id)
1633 {
1634 int i;
1635 afi_t afi;
1636
1637 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1638 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1639 if ((old_vrf_id == VRF_UNKNOWN)
1640 || vrf_bitmap_check(zclient->redist[afi][i],
1641 old_vrf_id)) {
1642 vrf_bitmap_unset(zclient->redist[afi][i],
1643 old_vrf_id);
1644 vrf_bitmap_set(zclient->redist[afi][i],
1645 bgp->vrf_id);
1646 }
1647 return;
1648 }
1649
1650 void bgp_zclient_reset(void)
1651 {
1652 zclient_reset(zclient);
1653 }
1654
1655 /* Register this instance with Zebra. Invoked upon connect (for
1656 * default instance) and when other VRFs are learnt (or created and
1657 * already learnt).
1658 */
1659 void bgp_zebra_instance_register(struct bgp *bgp)
1660 {
1661 /* Don't try to register if we're not connected to Zebra */
1662 if (!zclient || zclient->sock < 0)
1663 return;
1664
1665 if (BGP_DEBUG(zebra, ZEBRA))
1666 zlog_debug("Registering VRF %u", bgp->vrf_id);
1667
1668 /* Register for router-id, interfaces, redistributed routes. */
1669 zclient_send_reg_requests(zclient, bgp->vrf_id);
1670
1671 /* For default instance, register to learn about VNIs, if appropriate.
1672 */
1673 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT && is_evpn_enabled())
1674 bgp_zebra_advertise_all_vni(bgp, 1);
1675 }
1676
1677 /* Deregister this instance with Zebra. Invoked upon the instance
1678 * being deleted (default or VRF) and it is already registered.
1679 */
1680 void bgp_zebra_instance_deregister(struct bgp *bgp)
1681 {
1682 /* Don't try to deregister if we're not connected to Zebra */
1683 if (zclient->sock < 0)
1684 return;
1685
1686 if (BGP_DEBUG(zebra, ZEBRA))
1687 zlog_debug("Deregistering VRF %u", bgp->vrf_id);
1688
1689 /* For default instance, unregister learning about VNIs, if appropriate.
1690 */
1691 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT && is_evpn_enabled())
1692 bgp_zebra_advertise_all_vni(bgp, 0);
1693
1694 /* Deregister for router-id, interfaces, redistributed routes. */
1695 zclient_send_dereg_requests(zclient, bgp->vrf_id);
1696 }
1697
1698 void bgp_zebra_initiate_radv(struct bgp *bgp, struct peer *peer)
1699 {
1700 int ra_interval = BGP_UNNUM_DEFAULT_RA_INTERVAL;
1701
1702 /* Don't try to initiate if we're not connected to Zebra */
1703 if (zclient->sock < 0)
1704 return;
1705
1706 if (BGP_DEBUG(zebra, ZEBRA))
1707 zlog_debug("%u: Initiating RA for peer %s", bgp->vrf_id,
1708 peer->host);
1709
1710 zclient_send_interface_radv_req(zclient, bgp->vrf_id, peer->ifp, 1,
1711 ra_interval);
1712 }
1713
1714 void bgp_zebra_terminate_radv(struct bgp *bgp, struct peer *peer)
1715 {
1716 /* Don't try to terminate if we're not connected to Zebra */
1717 if (zclient->sock < 0)
1718 return;
1719
1720 if (BGP_DEBUG(zebra, ZEBRA))
1721 zlog_debug("%u: Terminating RA for peer %s", bgp->vrf_id,
1722 peer->host);
1723
1724 zclient_send_interface_radv_req(zclient, bgp->vrf_id, peer->ifp, 0, 0);
1725 }
1726
1727 int bgp_zebra_advertise_subnet(struct bgp *bgp, int advertise, vni_t vni)
1728 {
1729 struct stream *s = NULL;
1730
1731 /* Check socket. */
1732 if (!zclient || zclient->sock < 0)
1733 return 0;
1734
1735 /* Don't try to register if Zebra doesn't know of this instance. */
1736 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
1737 return 0;
1738
1739 s = zclient->obuf;
1740 stream_reset(s);
1741
1742 zclient_create_header(s, ZEBRA_ADVERTISE_SUBNET, bgp->vrf_id);
1743 stream_putc(s, advertise);
1744 stream_put3(s, vni);
1745 stream_putw_at(s, 0, stream_get_endp(s));
1746
1747 return zclient_send_message(zclient);
1748 }
1749
1750 int bgp_zebra_advertise_gw_macip(struct bgp *bgp, int advertise, vni_t vni)
1751 {
1752 struct stream *s = NULL;
1753
1754 /* Check socket. */
1755 if (!zclient || zclient->sock < 0)
1756 return 0;
1757
1758 /* Don't try to register if Zebra doesn't know of this instance. */
1759 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
1760 return 0;
1761
1762 s = zclient->obuf;
1763 stream_reset(s);
1764
1765 zclient_create_header(s, ZEBRA_ADVERTISE_DEFAULT_GW, bgp->vrf_id);
1766 stream_putc(s, advertise);
1767 stream_put3(s, vni);
1768 stream_putw_at(s, 0, stream_get_endp(s));
1769
1770 return zclient_send_message(zclient);
1771 }
1772
1773 int bgp_zebra_advertise_all_vni(struct bgp *bgp, int advertise)
1774 {
1775 struct stream *s;
1776
1777 /* Check socket. */
1778 if (!zclient || zclient->sock < 0)
1779 return 0;
1780
1781 /* Don't try to register if Zebra doesn't know of this instance. */
1782 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
1783 return 0;
1784
1785 s = zclient->obuf;
1786 stream_reset(s);
1787
1788 zclient_create_header(s, ZEBRA_ADVERTISE_ALL_VNI, bgp->vrf_id);
1789 stream_putc(s, advertise);
1790 stream_putw_at(s, 0, stream_get_endp(s));
1791
1792 return zclient_send_message(zclient);
1793 }
1794
1795 /* BGP has established connection with Zebra. */
1796 static void bgp_zebra_connected(struct zclient *zclient)
1797 {
1798 struct bgp *bgp;
1799
1800 zclient_num_connects++; /* increment even if not responding */
1801
1802 /* At this point, we may or may not have BGP instances configured, but
1803 * we're only interested in the default VRF (others wouldn't have learnt
1804 * the VRF from Zebra yet.)
1805 */
1806 bgp = bgp_get_default();
1807 if (!bgp)
1808 return;
1809
1810 bgp_zebra_instance_register(bgp);
1811
1812 /* Send the client registration */
1813 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
1814
1815 /* TODO - What if we have peers and networks configured, do we have to
1816 * kick-start them?
1817 */
1818 }
1819
1820 static int bgp_zebra_process_local_l3vni(int cmd, struct zclient *zclient,
1821 zebra_size_t length, vrf_id_t vrf_id)
1822 {
1823 int filter = 0;
1824 char buf[ETHER_ADDR_STRLEN];
1825 vni_t l3vni = 0;
1826 struct ethaddr rmac;
1827 struct in_addr originator_ip;
1828 struct stream *s;
1829
1830 memset(&rmac, 0, sizeof(struct ethaddr));
1831 memset(&originator_ip, 0, sizeof(struct in_addr));
1832 s = zclient->ibuf;
1833 l3vni = stream_getl(s);
1834 if (cmd == ZEBRA_L3VNI_ADD) {
1835 stream_get(&rmac, s, sizeof(struct ethaddr));
1836 originator_ip.s_addr = stream_get_ipv4(s);
1837 stream_get(&filter, s, sizeof(int));
1838 }
1839
1840 if (BGP_DEBUG(zebra, ZEBRA))
1841 zlog_debug("Rx L3-VNI %s VRF %s VNI %u RMAC %s filter %s",
1842 (cmd == ZEBRA_L3VNI_ADD) ? "add" : "del",
1843 vrf_id_to_name(vrf_id), l3vni,
1844 prefix_mac2str(&rmac, buf, sizeof(buf)),
1845 filter ? "prefix-routes-only" : "none");
1846
1847 if (cmd == ZEBRA_L3VNI_ADD)
1848 bgp_evpn_local_l3vni_add(l3vni, vrf_id, &rmac, originator_ip,
1849 filter);
1850 else
1851 bgp_evpn_local_l3vni_del(l3vni, vrf_id);
1852
1853 return 0;
1854 }
1855
1856 static int bgp_zebra_process_local_vni(int command, struct zclient *zclient,
1857 zebra_size_t length, vrf_id_t vrf_id)
1858 {
1859 struct stream *s;
1860 vni_t vni;
1861 struct bgp *bgp;
1862 struct in_addr vtep_ip = {INADDR_ANY};
1863 vrf_id_t tenant_vrf_id = VRF_DEFAULT;
1864
1865 s = zclient->ibuf;
1866 vni = stream_getl(s);
1867 if (command == ZEBRA_VNI_ADD) {
1868 vtep_ip.s_addr = stream_get_ipv4(s);
1869 stream_get(&tenant_vrf_id, s, sizeof(vrf_id_t));
1870 }
1871
1872 bgp = bgp_lookup_by_vrf_id(vrf_id);
1873 if (!bgp)
1874 return 0;
1875
1876 if (BGP_DEBUG(zebra, ZEBRA))
1877 zlog_debug("Rx VNI %s VRF %s VNI %u tenant-vrf %s",
1878 (command == ZEBRA_VNI_ADD) ? "add" : "del",
1879 vrf_id_to_name(vrf_id), vni,
1880 vrf_id_to_name(tenant_vrf_id));
1881
1882 if (command == ZEBRA_VNI_ADD)
1883 return bgp_evpn_local_vni_add(
1884 bgp, vni, vtep_ip.s_addr ? vtep_ip : bgp->router_id,
1885 tenant_vrf_id);
1886 else
1887 return bgp_evpn_local_vni_del(bgp, vni);
1888 }
1889
1890 static int bgp_zebra_process_local_macip(int command, struct zclient *zclient,
1891 zebra_size_t length, vrf_id_t vrf_id)
1892 {
1893 struct stream *s;
1894 vni_t vni;
1895 struct bgp *bgp;
1896 struct ethaddr mac;
1897 struct ipaddr ip;
1898 int ipa_len;
1899 char buf[ETHER_ADDR_STRLEN];
1900 char buf1[INET6_ADDRSTRLEN];
1901 u_char flags;
1902
1903 memset(&ip, 0, sizeof(ip));
1904 s = zclient->ibuf;
1905 vni = stream_getl(s);
1906 stream_get(&mac.octet, s, ETH_ALEN);
1907 ipa_len = stream_getl(s);
1908 if (ipa_len != 0 && ipa_len != IPV4_MAX_BYTELEN
1909 && ipa_len != IPV6_MAX_BYTELEN) {
1910 zlog_err("%u:Recv MACIP %s with invalid IP addr length %d",
1911 vrf_id, (command == ZEBRA_MACIP_ADD) ? "Add" : "Del",
1912 ipa_len);
1913 return -1;
1914 }
1915
1916 if (ipa_len) {
1917 ip.ipa_type =
1918 (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4 : IPADDR_V6;
1919 stream_get(&ip.ip.addr, s, ipa_len);
1920 }
1921 flags = stream_getc(s);
1922
1923 bgp = bgp_lookup_by_vrf_id(vrf_id);
1924 if (!bgp)
1925 return 0;
1926
1927 if (BGP_DEBUG(zebra, ZEBRA))
1928 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u",
1929 vrf_id, (command == ZEBRA_MACIP_ADD) ? "Add" : "Del",
1930 flags, prefix_mac2str(&mac, buf, sizeof(buf)),
1931 ipaddr2str(&ip, buf1, sizeof(buf1)), vni);
1932
1933 if (command == ZEBRA_MACIP_ADD)
1934 return bgp_evpn_local_macip_add(bgp, vni, &mac, &ip, flags);
1935 else
1936 return bgp_evpn_local_macip_del(bgp, vni, &mac, &ip);
1937 }
1938
1939 static void bgp_zebra_process_local_ip_prefix(int cmd, struct zclient *zclient,
1940 zebra_size_t length,
1941 vrf_id_t vrf_id)
1942 {
1943 struct stream *s = NULL;
1944 struct bgp *bgp_vrf = NULL;
1945 struct prefix p;
1946 char buf[PREFIX_STRLEN];
1947
1948 memset(&p, 0, sizeof(struct prefix));
1949 s = zclient->ibuf;
1950 stream_get(&p, s, sizeof(struct prefix));
1951
1952 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
1953 if (!bgp_vrf)
1954 return;
1955
1956 if (BGP_DEBUG(zebra, ZEBRA))
1957 zlog_debug("Recv prefix %s %s on vrf %s",
1958 prefix2str(&p, buf, sizeof(buf)),
1959 (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) ? "ADD" : "DEL",
1960 vrf_id_to_name(vrf_id));
1961
1962 if (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) {
1963
1964 if (p.family == AF_INET)
1965 return bgp_evpn_advertise_type5_route(
1966 bgp_vrf, &p, NULL, AFI_IP, SAFI_UNICAST);
1967 else
1968 return bgp_evpn_advertise_type5_route(
1969 bgp_vrf, &p, NULL, AFI_IP6, SAFI_UNICAST);
1970
1971 } else {
1972 if (p.family == AF_INET)
1973 return bgp_evpn_withdraw_type5_route(
1974 bgp_vrf, &p, AFI_IP, SAFI_UNICAST);
1975 else
1976 return bgp_evpn_withdraw_type5_route(
1977 bgp_vrf, &p, AFI_IP6, SAFI_UNICAST);
1978 }
1979 }
1980
1981 extern struct zebra_privs_t bgpd_privs;
1982
1983 void bgp_zebra_init(struct thread_master *master)
1984 {
1985 zclient_num_connects = 0;
1986
1987 /* Set default values. */
1988 zclient = zclient_new_notify(master, &zclient_options_default);
1989 zclient_init(zclient, ZEBRA_ROUTE_BGP, 0, &bgpd_privs);
1990 zclient->zebra_connected = bgp_zebra_connected;
1991 zclient->router_id_update = bgp_router_id_update;
1992 zclient->interface_add = bgp_interface_add;
1993 zclient->interface_delete = bgp_interface_delete;
1994 zclient->interface_address_add = bgp_interface_address_add;
1995 zclient->interface_address_delete = bgp_interface_address_delete;
1996 zclient->interface_nbr_address_add = bgp_interface_nbr_address_add;
1997 zclient->interface_nbr_address_delete =
1998 bgp_interface_nbr_address_delete;
1999 zclient->interface_vrf_update = bgp_interface_vrf_update;
2000 zclient->redistribute_route_add = zebra_read_route;
2001 zclient->redistribute_route_del = zebra_read_route;
2002 zclient->interface_up = bgp_interface_up;
2003 zclient->interface_down = bgp_interface_down;
2004 zclient->nexthop_update = bgp_read_nexthop_update;
2005 zclient->import_check_update = bgp_read_import_check_update;
2006 zclient->fec_update = bgp_read_fec_update;
2007 zclient->local_vni_add = bgp_zebra_process_local_vni;
2008 zclient->local_vni_del = bgp_zebra_process_local_vni;
2009 zclient->local_macip_add = bgp_zebra_process_local_macip;
2010 zclient->local_macip_del = bgp_zebra_process_local_macip;
2011 zclient->local_l3vni_add = bgp_zebra_process_local_l3vni;
2012 zclient->local_l3vni_del = bgp_zebra_process_local_l3vni;
2013 zclient->local_ip_prefix_add = bgp_zebra_process_local_ip_prefix;
2014 zclient->local_ip_prefix_del = bgp_zebra_process_local_ip_prefix;
2015 }
2016
2017 void bgp_zebra_destroy(void)
2018 {
2019 if (zclient == NULL)
2020 return;
2021 zclient_stop(zclient);
2022 zclient_free(zclient);
2023 zclient = NULL;
2024 }
2025
2026 int bgp_zebra_num_connects(void)
2027 {
2028 return zclient_num_connects;
2029 }