]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_zebra.c
Merge pull request #10658 from proelbtn/fix/srv6-l3vpn-transposition
[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 "lib/route_opaque.h"
37 #include "filter.h"
38 #include "mpls.h"
39 #include "vxlan.h"
40 #include "pbr.h"
41
42 #include "bgpd/bgpd.h"
43 #include "bgpd/bgp_route.h"
44 #include "bgpd/bgp_attr.h"
45 #include "bgpd/bgp_aspath.h"
46 #include "bgpd/bgp_nexthop.h"
47 #include "bgpd/bgp_zebra.h"
48 #include "bgpd/bgp_fsm.h"
49 #include "bgpd/bgp_debug.h"
50 #include "bgpd/bgp_errors.h"
51 #include "bgpd/bgp_mpath.h"
52 #include "bgpd/bgp_nexthop.h"
53 #include "bgpd/bgp_nht.h"
54 #include "bgpd/bgp_bfd.h"
55 #include "bgpd/bgp_label.h"
56 #ifdef ENABLE_BGP_VNC
57 #include "bgpd/rfapi/rfapi_backend.h"
58 #include "bgpd/rfapi/vnc_export_bgp.h"
59 #endif
60 #include "bgpd/bgp_evpn.h"
61 #include "bgpd/bgp_mplsvpn.h"
62 #include "bgpd/bgp_labelpool.h"
63 #include "bgpd/bgp_pbr.h"
64 #include "bgpd/bgp_evpn_private.h"
65 #include "bgpd/bgp_evpn_mh.h"
66 #include "bgpd/bgp_mac.h"
67 #include "bgpd/bgp_trace.h"
68 #include "bgpd/bgp_community.h"
69 #include "bgpd/bgp_lcommunity.h"
70
71 /* All information about zebra. */
72 struct zclient *zclient = NULL;
73
74 /* hook to indicate vrf status change for SNMP */
75 DEFINE_HOOK(bgp_vrf_status_changed, (struct bgp *bgp, struct interface *ifp),
76 (bgp, ifp));
77
78 /* Can we install into zebra? */
79 static inline bool bgp_install_info_to_zebra(struct bgp *bgp)
80 {
81 if (zclient->sock <= 0)
82 return false;
83
84 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
85 zlog_debug(
86 "%s: No zebra instance to talk to, not installing information",
87 __func__);
88 return false;
89 }
90
91 return true;
92 }
93
94 int zclient_num_connects;
95
96 /* Router-id update message from zebra. */
97 static int bgp_router_id_update(ZAPI_CALLBACK_ARGS)
98 {
99 struct prefix router_id;
100
101 zebra_router_id_update_read(zclient->ibuf, &router_id);
102
103 if (BGP_DEBUG(zebra, ZEBRA))
104 zlog_debug("Rx Router Id update VRF %u Id %pFX", vrf_id,
105 &router_id);
106
107 bgp_router_id_zebra_bump(vrf_id, &router_id);
108 return 0;
109 }
110
111 /* Nexthop update message from zebra. */
112 static int bgp_read_nexthop_update(ZAPI_CALLBACK_ARGS)
113 {
114 bgp_parse_nexthop_update(cmd, vrf_id);
115 return 0;
116 }
117
118 /* Set or clear interface on which unnumbered neighbor is configured. This
119 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
120 * interface.
121 */
122 static void bgp_update_interface_nbrs(struct bgp *bgp, struct interface *ifp,
123 struct interface *upd_ifp)
124 {
125 struct listnode *node, *nnode;
126 struct peer *peer;
127
128 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
129 if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)) {
130 if (upd_ifp) {
131 peer->ifp = upd_ifp;
132 bgp_zebra_initiate_radv(bgp, peer);
133 } else {
134 bgp_zebra_terminate_radv(bgp, peer);
135 peer->ifp = upd_ifp;
136 }
137 }
138 }
139 }
140
141 static int bgp_read_fec_update(ZAPI_CALLBACK_ARGS)
142 {
143 bgp_parse_fec_update();
144 return 0;
145 }
146
147 static void bgp_start_interface_nbrs(struct bgp *bgp, struct interface *ifp)
148 {
149 struct listnode *node, *nnode;
150 struct peer *peer;
151
152 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
153 if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)
154 && !peer_established(peer)) {
155 if (peer_active(peer))
156 BGP_EVENT_ADD(peer, BGP_Stop);
157 BGP_EVENT_ADD(peer, BGP_Start);
158 }
159 }
160 }
161
162 static void bgp_nbr_connected_add(struct bgp *bgp, struct nbr_connected *ifc)
163 {
164 struct listnode *node;
165 struct connected *connected;
166 struct interface *ifp;
167 struct prefix *p;
168
169 /* Kick-off the FSM for any relevant peers only if there is a
170 * valid local address on the interface.
171 */
172 ifp = ifc->ifp;
173 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
174 p = connected->address;
175 if (p->family == AF_INET6
176 && IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
177 break;
178 }
179 if (!connected)
180 return;
181
182 bgp_start_interface_nbrs(bgp, ifp);
183 }
184
185 static void bgp_nbr_connected_delete(struct bgp *bgp, struct nbr_connected *ifc,
186 int del)
187 {
188 struct listnode *node, *nnode;
189 struct peer *peer;
190 struct interface *ifp;
191
192 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
193 if (peer->conf_if
194 && (strcmp(peer->conf_if, ifc->ifp->name) == 0)) {
195 peer->last_reset = PEER_DOWN_NBR_ADDR_DEL;
196 BGP_EVENT_ADD(peer, BGP_Stop);
197 }
198 }
199 /* Free neighbor also, if we're asked to. */
200 if (del) {
201 ifp = ifc->ifp;
202 listnode_delete(ifp->nbr_connected, ifc);
203 nbr_connected_free(ifc);
204 }
205 }
206
207 static int bgp_ifp_destroy(struct interface *ifp)
208 {
209 struct bgp *bgp;
210
211 bgp = ifp->vrf->info;
212
213 if (BGP_DEBUG(zebra, ZEBRA))
214 zlog_debug("Rx Intf del VRF %u IF %s", ifp->vrf->vrf_id,
215 ifp->name);
216
217 if (bgp) {
218 bgp_update_interface_nbrs(bgp, ifp, NULL);
219 hook_call(bgp_vrf_status_changed, bgp, ifp);
220 }
221
222 bgp_mac_del_mac_entry(ifp);
223
224 return 0;
225 }
226
227 static int bgp_ifp_up(struct interface *ifp)
228 {
229 struct connected *c;
230 struct nbr_connected *nc;
231 struct listnode *node, *nnode;
232 struct bgp *bgp;
233
234 bgp = ifp->vrf->info;
235
236 bgp_mac_add_mac_entry(ifp);
237
238 if (BGP_DEBUG(zebra, ZEBRA))
239 zlog_debug("Rx Intf up VRF %u IF %s", ifp->vrf->vrf_id,
240 ifp->name);
241
242 if (!bgp)
243 return 0;
244
245 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
246 bgp_connected_add(bgp, c);
247
248 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
249 bgp_nbr_connected_add(bgp, nc);
250
251 hook_call(bgp_vrf_status_changed, bgp, ifp);
252 bgp_nht_ifp_up(ifp);
253
254 return 0;
255 }
256
257 static int bgp_ifp_down(struct interface *ifp)
258 {
259 struct connected *c;
260 struct nbr_connected *nc;
261 struct listnode *node, *nnode;
262 struct bgp *bgp;
263 struct peer *peer;
264
265 bgp = ifp->vrf->info;
266
267 bgp_mac_del_mac_entry(ifp);
268
269 if (BGP_DEBUG(zebra, ZEBRA))
270 zlog_debug("Rx Intf down VRF %u IF %s", ifp->vrf->vrf_id,
271 ifp->name);
272
273 if (!bgp)
274 return 0;
275
276 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
277 bgp_connected_delete(bgp, c);
278
279 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
280 bgp_nbr_connected_delete(bgp, nc, 1);
281
282 /* Fast external-failover */
283 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) {
284
285 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
286 /* Take down directly connected peers. */
287 if ((peer->ttl != BGP_DEFAULT_TTL)
288 && (peer->gtsm_hops != BGP_GTSM_HOPS_CONNECTED))
289 continue;
290
291 if (ifp == peer->nexthop.ifp) {
292 BGP_EVENT_ADD(peer, BGP_Stop);
293 peer->last_reset = PEER_DOWN_IF_DOWN;
294 }
295 }
296 }
297
298 hook_call(bgp_vrf_status_changed, bgp, ifp);
299 bgp_nht_ifp_down(ifp);
300
301 return 0;
302 }
303
304 static int bgp_interface_address_add(ZAPI_CALLBACK_ARGS)
305 {
306 struct connected *ifc;
307 struct bgp *bgp;
308
309 bgp = bgp_lookup_by_vrf_id(vrf_id);
310
311 ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
312
313 if (ifc == NULL)
314 return 0;
315
316 if (bgp_debug_zebra(ifc->address))
317 zlog_debug("Rx Intf address add VRF %u IF %s addr %pFX", vrf_id,
318 ifc->ifp->name, ifc->address);
319
320 if (!bgp)
321 return 0;
322
323 if (if_is_operative(ifc->ifp)) {
324 bgp_connected_add(bgp, ifc);
325
326 /* If we have learnt of any neighbors on this interface,
327 * check to kick off any BGP interface-based neighbors,
328 * but only if this is a link-local address.
329 */
330 if (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6)
331 && !list_isempty(ifc->ifp->nbr_connected))
332 bgp_start_interface_nbrs(bgp, ifc->ifp);
333 }
334
335 return 0;
336 }
337
338 static int bgp_interface_address_delete(ZAPI_CALLBACK_ARGS)
339 {
340 struct listnode *node, *nnode;
341 struct connected *ifc;
342 struct peer *peer;
343 struct bgp *bgp;
344 struct prefix *addr;
345
346 bgp = bgp_lookup_by_vrf_id(vrf_id);
347
348 ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
349
350 if (ifc == NULL)
351 return 0;
352
353 if (bgp_debug_zebra(ifc->address))
354 zlog_debug("Rx Intf address del VRF %u IF %s addr %pFX", vrf_id,
355 ifc->ifp->name, ifc->address);
356
357 if (bgp && if_is_operative(ifc->ifp)) {
358 bgp_connected_delete(bgp, ifc);
359 }
360
361 addr = ifc->address;
362
363 if (bgp) {
364 /*
365 * When we are using the v6 global as part of the peering
366 * nexthops and we are removing it, then we need to
367 * clear the peer data saved for that nexthop and
368 * cause a re-announcement of the route. Since
369 * we do not want the peering to bounce.
370 */
371 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
372 afi_t afi;
373 safi_t safi;
374
375 if (addr->family == AF_INET)
376 continue;
377
378 if (!IN6_IS_ADDR_LINKLOCAL(&addr->u.prefix6)
379 && memcmp(&peer->nexthop.v6_global,
380 &addr->u.prefix6, 16)
381 == 0) {
382 memset(&peer->nexthop.v6_global, 0, 16);
383 FOREACH_AFI_SAFI (afi, safi)
384 bgp_announce_route(peer, afi, safi,
385 true);
386 }
387 }
388 }
389
390 connected_free(&ifc);
391
392 return 0;
393 }
394
395 static int bgp_interface_nbr_address_add(ZAPI_CALLBACK_ARGS)
396 {
397 struct nbr_connected *ifc = NULL;
398 struct bgp *bgp;
399
400 ifc = zebra_interface_nbr_address_read(cmd, zclient->ibuf, vrf_id);
401
402 if (ifc == NULL)
403 return 0;
404
405 if (bgp_debug_zebra(ifc->address))
406 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %pFX",
407 vrf_id, ifc->ifp->name, ifc->address);
408
409 if (if_is_operative(ifc->ifp)) {
410 bgp = bgp_lookup_by_vrf_id(vrf_id);
411 if (bgp)
412 bgp_nbr_connected_add(bgp, ifc);
413 }
414
415 return 0;
416 }
417
418 static int bgp_interface_nbr_address_delete(ZAPI_CALLBACK_ARGS)
419 {
420 struct nbr_connected *ifc = NULL;
421 struct bgp *bgp;
422
423 ifc = zebra_interface_nbr_address_read(cmd, zclient->ibuf, vrf_id);
424
425 if (ifc == NULL)
426 return 0;
427
428 if (bgp_debug_zebra(ifc->address))
429 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %pFX",
430 vrf_id, ifc->ifp->name, ifc->address);
431
432 if (if_is_operative(ifc->ifp)) {
433 bgp = bgp_lookup_by_vrf_id(vrf_id);
434 if (bgp)
435 bgp_nbr_connected_delete(bgp, ifc, 0);
436 }
437
438 nbr_connected_free(ifc);
439
440 return 0;
441 }
442
443 /* VRF update for an interface. */
444 static int bgp_interface_vrf_update(ZAPI_CALLBACK_ARGS)
445 {
446 struct interface *ifp;
447 vrf_id_t new_vrf_id;
448 struct connected *c;
449 struct nbr_connected *nc;
450 struct listnode *node, *nnode;
451 struct bgp *bgp;
452 struct peer *peer;
453
454 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
455 &new_vrf_id);
456 if (!ifp)
457 return 0;
458
459 if (BGP_DEBUG(zebra, ZEBRA) && ifp)
460 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id,
461 ifp->name, new_vrf_id);
462
463 bgp = bgp_lookup_by_vrf_id(vrf_id);
464
465 if (bgp) {
466 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
467 bgp_connected_delete(bgp, c);
468
469 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
470 bgp_nbr_connected_delete(bgp, nc, 1);
471
472 /* Fast external-failover */
473 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) {
474 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
475 if ((peer->ttl != BGP_DEFAULT_TTL)
476 && (peer->gtsm_hops
477 != BGP_GTSM_HOPS_CONNECTED))
478 continue;
479
480 if (ifp == peer->nexthop.ifp)
481 BGP_EVENT_ADD(peer, BGP_Stop);
482 }
483 }
484 }
485
486 if_update_to_new_vrf(ifp, new_vrf_id);
487
488 bgp = bgp_lookup_by_vrf_id(new_vrf_id);
489 if (!bgp)
490 return 0;
491
492 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
493 bgp_connected_add(bgp, c);
494
495 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
496 bgp_nbr_connected_add(bgp, nc);
497
498 hook_call(bgp_vrf_status_changed, bgp, ifp);
499 return 0;
500 }
501
502 /* Zebra route add and delete treatment. */
503 static int zebra_read_route(ZAPI_CALLBACK_ARGS)
504 {
505 enum nexthop_types_t nhtype;
506 enum blackhole_type bhtype = BLACKHOLE_UNSPEC;
507 struct zapi_route api;
508 union g_addr nexthop = {};
509 ifindex_t ifindex;
510 int add, i;
511 struct bgp *bgp;
512
513 bgp = bgp_lookup_by_vrf_id(vrf_id);
514 if (!bgp)
515 return 0;
516
517 if (zapi_route_decode(zclient->ibuf, &api) < 0)
518 return -1;
519
520 /* we completely ignore srcdest routes for now. */
521 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
522 return 0;
523
524 /* ignore link-local address. */
525 if (api.prefix.family == AF_INET6
526 && IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6))
527 return 0;
528
529 ifindex = api.nexthops[0].ifindex;
530 nhtype = api.nexthops[0].type;
531
532 /* api_nh structure has union of gate and bh_type */
533 if (nhtype == NEXTHOP_TYPE_BLACKHOLE) {
534 /* bh_type is only applicable if NEXTHOP_TYPE_BLACKHOLE*/
535 bhtype = api.nexthops[0].bh_type;
536 } else
537 nexthop = api.nexthops[0].gate;
538
539 add = (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
540 if (add) {
541 /*
542 * The ADD message is actually an UPDATE and there is no
543 * explicit DEL
544 * for a prior redistributed route, if any. So, perform an
545 * implicit
546 * DEL processing for the same redistributed route from any
547 * other
548 * source type.
549 */
550 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
551 if (i != api.type)
552 bgp_redistribute_delete(bgp, &api.prefix, i,
553 api.instance);
554 }
555
556 /* Now perform the add/update. */
557 bgp_redistribute_add(bgp, &api.prefix, &nexthop, ifindex,
558 nhtype, bhtype, api.distance, api.metric,
559 api.type, api.instance, api.tag);
560 } else {
561 bgp_redistribute_delete(bgp, &api.prefix, api.type,
562 api.instance);
563 }
564
565 if (bgp_debug_zebra(&api.prefix)) {
566 char buf[PREFIX_STRLEN];
567
568 if (add) {
569 inet_ntop(api.prefix.family, &nexthop, buf,
570 sizeof(buf));
571 zlog_debug(
572 "Rx route ADD VRF %u %s[%d] %pFX nexthop %s (type %d if %u) metric %u distance %u tag %" ROUTE_TAG_PRI,
573 vrf_id, zebra_route_string(api.type),
574 api.instance, &api.prefix, buf, nhtype, ifindex,
575 api.metric, api.distance, api.tag);
576 } else {
577 zlog_debug("Rx route DEL VRF %u %s[%d] %pFX", vrf_id,
578 zebra_route_string(api.type), api.instance,
579 &api.prefix);
580 }
581 }
582
583 return 0;
584 }
585
586 struct interface *if_lookup_by_ipv4(struct in_addr *addr, vrf_id_t vrf_id)
587 {
588 struct vrf *vrf;
589 struct listnode *cnode;
590 struct interface *ifp;
591 struct connected *connected;
592 struct prefix_ipv4 p;
593 struct prefix *cp;
594
595 vrf = vrf_lookup_by_id(vrf_id);
596 if (!vrf)
597 return NULL;
598
599 p.family = AF_INET;
600 p.prefix = *addr;
601 p.prefixlen = IPV4_MAX_BITLEN;
602
603 FOR_ALL_INTERFACES (vrf, ifp) {
604 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
605 cp = connected->address;
606
607 if (cp->family == AF_INET)
608 if (prefix_match(cp, (struct prefix *)&p))
609 return ifp;
610 }
611 }
612 return NULL;
613 }
614
615 struct interface *if_lookup_by_ipv4_exact(struct in_addr *addr, vrf_id_t vrf_id)
616 {
617 struct vrf *vrf;
618 struct listnode *cnode;
619 struct interface *ifp;
620 struct connected *connected;
621 struct prefix *cp;
622
623 vrf = vrf_lookup_by_id(vrf_id);
624 if (!vrf)
625 return NULL;
626
627 FOR_ALL_INTERFACES (vrf, ifp) {
628 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
629 cp = connected->address;
630
631 if (cp->family == AF_INET)
632 if (IPV4_ADDR_SAME(&cp->u.prefix4, addr))
633 return ifp;
634 }
635 }
636 return NULL;
637 }
638
639 struct interface *if_lookup_by_ipv6(struct in6_addr *addr, ifindex_t ifindex,
640 vrf_id_t vrf_id)
641 {
642 struct vrf *vrf;
643 struct listnode *cnode;
644 struct interface *ifp;
645 struct connected *connected;
646 struct prefix_ipv6 p;
647 struct prefix *cp;
648
649 vrf = vrf_lookup_by_id(vrf_id);
650 if (!vrf)
651 return NULL;
652
653 p.family = AF_INET6;
654 p.prefix = *addr;
655 p.prefixlen = IPV6_MAX_BITLEN;
656
657 FOR_ALL_INTERFACES (vrf, ifp) {
658 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
659 cp = connected->address;
660
661 if (cp->family == AF_INET6)
662 if (prefix_match(cp, (struct prefix *)&p)) {
663 if (IN6_IS_ADDR_LINKLOCAL(
664 &cp->u.prefix6)) {
665 if (ifindex == ifp->ifindex)
666 return ifp;
667 } else
668 return ifp;
669 }
670 }
671 }
672 return NULL;
673 }
674
675 struct interface *if_lookup_by_ipv6_exact(struct in6_addr *addr,
676 ifindex_t ifindex, vrf_id_t vrf_id)
677 {
678 struct vrf *vrf;
679 struct listnode *cnode;
680 struct interface *ifp;
681 struct connected *connected;
682 struct prefix *cp;
683
684 vrf = vrf_lookup_by_id(vrf_id);
685 if (!vrf)
686 return NULL;
687
688 FOR_ALL_INTERFACES (vrf, ifp) {
689 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
690 cp = connected->address;
691
692 if (cp->family == AF_INET6)
693 if (IPV6_ADDR_SAME(&cp->u.prefix6, addr)) {
694 if (IN6_IS_ADDR_LINKLOCAL(
695 &cp->u.prefix6)) {
696 if (ifindex == ifp->ifindex)
697 return ifp;
698 } else
699 return ifp;
700 }
701 }
702 }
703 return NULL;
704 }
705
706 static int if_get_ipv6_global(struct interface *ifp, struct in6_addr *addr)
707 {
708 struct listnode *cnode;
709 struct connected *connected;
710 struct prefix *cp;
711
712 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
713 cp = connected->address;
714
715 if (cp->family == AF_INET6)
716 if (!IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
717 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
718 return 1;
719 }
720 }
721 return 0;
722 }
723
724 static bool if_get_ipv6_local(struct interface *ifp, struct in6_addr *addr)
725 {
726 struct listnode *cnode;
727 struct connected *connected;
728 struct prefix *cp;
729
730 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
731 cp = connected->address;
732
733 if (cp->family == AF_INET6)
734 if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
735 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
736 return true;
737 }
738 }
739 return false;
740 }
741
742 static int if_get_ipv4_address(struct interface *ifp, struct in_addr *addr)
743 {
744 struct listnode *cnode;
745 struct connected *connected;
746 struct prefix *cp;
747
748 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
749 cp = connected->address;
750 if ((cp->family == AF_INET)
751 && !ipv4_martian(&(cp->u.prefix4))) {
752 *addr = cp->u.prefix4;
753 return 1;
754 }
755 }
756 return 0;
757 }
758
759
760 bool bgp_zebra_nexthop_set(union sockunion *local, union sockunion *remote,
761 struct bgp_nexthop *nexthop, struct peer *peer)
762 {
763 int ret = 0;
764 struct interface *ifp = NULL;
765 bool v6_ll_avail = true;
766
767 memset(nexthop, 0, sizeof(struct bgp_nexthop));
768
769 if (!local)
770 return false;
771 if (!remote)
772 return false;
773
774 if (local->sa.sa_family == AF_INET) {
775 nexthop->v4 = local->sin.sin_addr;
776 if (peer->update_if)
777 ifp = if_lookup_by_name(peer->update_if,
778 peer->bgp->vrf_id);
779 else
780 ifp = if_lookup_by_ipv4_exact(&local->sin.sin_addr,
781 peer->bgp->vrf_id);
782 }
783 if (local->sa.sa_family == AF_INET6) {
784 memcpy(&nexthop->v6_global, &local->sin6.sin6_addr, IPV6_MAX_BYTELEN);
785 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
786 if (peer->conf_if || peer->ifname)
787 ifp = if_lookup_by_name(peer->conf_if
788 ? peer->conf_if
789 : peer->ifname,
790 peer->bgp->vrf_id);
791 else if (peer->update_if)
792 ifp = if_lookup_by_name(peer->update_if,
793 peer->bgp->vrf_id);
794 } else if (peer->update_if)
795 ifp = if_lookup_by_name(peer->update_if,
796 peer->bgp->vrf_id);
797 else
798 ifp = if_lookup_by_ipv6_exact(&local->sin6.sin6_addr,
799 local->sin6.sin6_scope_id,
800 peer->bgp->vrf_id);
801 }
802
803 if (!ifp) {
804 /*
805 * BGP views do not currently get proper data
806 * from zebra( when attached ) to be able to
807 * properly resolve nexthops, so give this
808 * instance type a pass.
809 */
810 if (peer->bgp->inst_type == BGP_INSTANCE_TYPE_VIEW)
811 return true;
812 /*
813 * If we have no interface data but we have established
814 * some connection w/ zebra than something has gone
815 * terribly terribly wrong here, so say this failed
816 * If we do not any zebra connection then not
817 * having a ifp pointer is ok.
818 */
819 return zclient_num_connects ? false : true;
820 }
821
822 nexthop->ifp = ifp;
823
824 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
825 if (local->sa.sa_family == AF_INET) {
826 /* IPv6 nexthop*/
827 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
828
829 if (!ret) {
830 /* There is no global nexthop. Use link-local address as
831 * both the
832 * global and link-local nexthop. In this scenario, the
833 * expectation
834 * for interop is that the network admin would use a
835 * route-map to
836 * specify the global IPv6 nexthop.
837 */
838 v6_ll_avail =
839 if_get_ipv6_local(ifp, &nexthop->v6_global);
840 memcpy(&nexthop->v6_local, &nexthop->v6_global,
841 IPV6_MAX_BYTELEN);
842 } else
843 v6_ll_avail =
844 if_get_ipv6_local(ifp, &nexthop->v6_local);
845
846 /*
847 * If we are a v4 connection and we are not doing unnumbered
848 * not having a v6 LL address is ok
849 */
850 if (!v6_ll_avail && !peer->conf_if)
851 v6_ll_avail = true;
852 if (if_lookup_by_ipv4(&remote->sin.sin_addr, peer->bgp->vrf_id))
853 peer->shared_network = 1;
854 else
855 peer->shared_network = 0;
856 }
857
858 /* IPv6 connection, fetch and store IPv4 local address if any. */
859 if (local->sa.sa_family == AF_INET6) {
860 struct interface *direct = NULL;
861
862 /* IPv4 nexthop. */
863 ret = if_get_ipv4_address(ifp, &nexthop->v4);
864 if (!ret && peer->local_id.s_addr != INADDR_ANY)
865 nexthop->v4 = peer->local_id;
866
867 /* Global address*/
868 if (!IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
869 memcpy(&nexthop->v6_global, &local->sin6.sin6_addr,
870 IPV6_MAX_BYTELEN);
871
872 /* If directory connected set link-local address. */
873 direct = if_lookup_by_ipv6(&remote->sin6.sin6_addr,
874 remote->sin6.sin6_scope_id,
875 peer->bgp->vrf_id);
876 if (direct)
877 v6_ll_avail = if_get_ipv6_local(
878 ifp, &nexthop->v6_local);
879 /*
880 * It's fine to not have a v6 LL when using
881 * update-source loopback/vrf
882 */
883 if (!v6_ll_avail && if_is_loopback(ifp))
884 v6_ll_avail = true;
885 } else
886 /* Link-local address. */
887 {
888 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
889
890 /* If there is no global address. Set link-local
891 address as
892 global. I know this break RFC specification... */
893 /* In this scenario, the expectation for interop is that
894 * the
895 * network admin would use a route-map to specify the
896 * global
897 * IPv6 nexthop.
898 */
899 if (!ret)
900 memcpy(&nexthop->v6_global,
901 &local->sin6.sin6_addr,
902 IPV6_MAX_BYTELEN);
903 /* Always set the link-local address */
904 memcpy(&nexthop->v6_local, &local->sin6.sin6_addr,
905 IPV6_MAX_BYTELEN);
906 }
907
908 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)
909 || if_lookup_by_ipv6(&remote->sin6.sin6_addr,
910 remote->sin6.sin6_scope_id,
911 peer->bgp->vrf_id))
912 peer->shared_network = 1;
913 else
914 peer->shared_network = 0;
915 }
916
917 /* KAME stack specific treatment. */
918 #ifdef KAME
919 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_global)
920 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_global)) {
921 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_global, 0);
922 }
923 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_local)
924 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_local)) {
925 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_local, 0);
926 }
927 #endif /* KAME */
928
929 /* If we have identified the local interface, there is no error for now.
930 */
931 return v6_ll_avail;
932 }
933
934 static struct in6_addr *
935 bgp_path_info_to_ipv6_nexthop(struct bgp_path_info *path, ifindex_t *ifindex)
936 {
937 struct in6_addr *nexthop = NULL;
938
939 /* Only global address nexthop exists. */
940 if (path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL
941 || path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_VPNV6_GLOBAL) {
942 nexthop = &path->attr->mp_nexthop_global;
943 if (IN6_IS_ADDR_LINKLOCAL(nexthop))
944 *ifindex = path->attr->nh_ifindex;
945 }
946
947 /* If both global and link-local address present. */
948 if (path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
949 || path->attr->mp_nexthop_len
950 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL) {
951 /* Check if route-map is set to prefer global over link-local */
952 if (path->attr->mp_nexthop_prefer_global) {
953 nexthop = &path->attr->mp_nexthop_global;
954 if (IN6_IS_ADDR_LINKLOCAL(nexthop))
955 *ifindex = path->attr->nh_ifindex;
956 } else {
957 /* Workaround for Cisco's nexthop bug. */
958 if (IN6_IS_ADDR_UNSPECIFIED(
959 &path->attr->mp_nexthop_global)
960 && path->peer->su_remote
961 && path->peer->su_remote->sa.sa_family
962 == AF_INET6) {
963 nexthop =
964 &path->peer->su_remote->sin6.sin6_addr;
965 if (IN6_IS_ADDR_LINKLOCAL(nexthop))
966 *ifindex = path->peer->nexthop.ifp
967 ->ifindex;
968 } else {
969 nexthop = &path->attr->mp_nexthop_local;
970 if (IN6_IS_ADDR_LINKLOCAL(nexthop))
971 *ifindex = path->attr->nh_lla_ifindex;
972 }
973 }
974 }
975
976 return nexthop;
977 }
978
979 static bool bgp_table_map_apply(struct route_map *map, const struct prefix *p,
980 struct bgp_path_info *path)
981 {
982 route_map_result_t ret;
983
984 ret = route_map_apply(map, p, path);
985 bgp_attr_flush(path->attr);
986
987 if (ret != RMAP_DENYMATCH)
988 return true;
989
990 if (bgp_debug_zebra(p)) {
991 if (p->family == AF_INET) {
992 zlog_debug(
993 "Zebra rmap deny: IPv4 route %pFX nexthop %pI4",
994 p, &path->attr->nexthop);
995 }
996 if (p->family == AF_INET6) {
997 char buf[2][INET6_ADDRSTRLEN];
998 ifindex_t ifindex;
999 struct in6_addr *nexthop;
1000
1001 nexthop = bgp_path_info_to_ipv6_nexthop(path, &ifindex);
1002 zlog_debug(
1003 "Zebra rmap deny: IPv6 route %pFX nexthop %s",
1004 p,
1005 nexthop ? inet_ntop(AF_INET6, nexthop, buf[1],
1006 sizeof(buf[1]))
1007 : inet_ntop(AF_INET,
1008 &path->attr->nexthop,
1009 buf[1], sizeof(buf[1])));
1010 }
1011 }
1012 return false;
1013 }
1014
1015 static struct thread *bgp_tm_thread_connect;
1016 static bool bgp_tm_status_connected;
1017 static bool bgp_tm_chunk_obtained;
1018 #define BGP_FLOWSPEC_TABLE_CHUNK 100000
1019 static uint32_t bgp_tm_min, bgp_tm_max, bgp_tm_chunk_size;
1020 struct bgp *bgp_tm_bgp;
1021
1022 static void bgp_zebra_tm_connect(struct thread *t)
1023 {
1024 struct zclient *zclient;
1025 int delay = 10, ret = 0;
1026
1027 zclient = THREAD_ARG(t);
1028 if (bgp_tm_status_connected && zclient->sock > 0)
1029 delay = 60;
1030 else {
1031 bgp_tm_status_connected = false;
1032 ret = tm_table_manager_connect(zclient);
1033 }
1034 if (ret < 0) {
1035 zlog_info("Error connecting to table manager!");
1036 bgp_tm_status_connected = false;
1037 } else {
1038 if (!bgp_tm_status_connected)
1039 zlog_debug("Connecting to table manager. Success");
1040 bgp_tm_status_connected = true;
1041 if (!bgp_tm_chunk_obtained) {
1042 if (bgp_zebra_get_table_range(bgp_tm_chunk_size,
1043 &bgp_tm_min,
1044 &bgp_tm_max) >= 0) {
1045 bgp_tm_chunk_obtained = true;
1046 /* parse non installed entries */
1047 bgp_zebra_announce_table(bgp_tm_bgp, AFI_IP, SAFI_FLOWSPEC);
1048 }
1049 }
1050 }
1051 thread_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay,
1052 &bgp_tm_thread_connect);
1053 }
1054
1055 bool bgp_zebra_tm_chunk_obtained(void)
1056 {
1057 return bgp_tm_chunk_obtained;
1058 }
1059
1060 uint32_t bgp_zebra_tm_get_id(void)
1061 {
1062 static int table_id;
1063
1064 if (!bgp_tm_chunk_obtained)
1065 return ++table_id;
1066 return bgp_tm_min++;
1067 }
1068
1069 void bgp_zebra_init_tm_connect(struct bgp *bgp)
1070 {
1071 int delay = 1;
1072
1073 /* if already set, do nothing
1074 */
1075 if (bgp_tm_thread_connect != NULL)
1076 return;
1077 bgp_tm_status_connected = false;
1078 bgp_tm_chunk_obtained = false;
1079 bgp_tm_min = bgp_tm_max = 0;
1080 bgp_tm_chunk_size = BGP_FLOWSPEC_TABLE_CHUNK;
1081 bgp_tm_bgp = bgp;
1082 thread_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay,
1083 &bgp_tm_thread_connect);
1084 }
1085
1086 int bgp_zebra_get_table_range(uint32_t chunk_size,
1087 uint32_t *start, uint32_t *end)
1088 {
1089 int ret;
1090
1091 if (!bgp_tm_status_connected)
1092 return -1;
1093 ret = tm_get_table_chunk(zclient, chunk_size, start, end);
1094 if (ret < 0) {
1095 flog_err(EC_BGP_TABLE_CHUNK,
1096 "BGP: Error getting table chunk %u", chunk_size);
1097 return -1;
1098 }
1099 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1100 chunk_size, *start, *end);
1101 return 0;
1102 }
1103
1104 static bool update_ipv4nh_for_route_install(int nh_othervrf, struct bgp *nh_bgp,
1105 struct in_addr *nexthop,
1106 struct attr *attr, bool is_evpn,
1107 struct zapi_nexthop *api_nh)
1108 {
1109 api_nh->gate.ipv4 = *nexthop;
1110 api_nh->vrf_id = nh_bgp->vrf_id;
1111
1112 /* Need to set fields appropriately for EVPN routes imported into
1113 * a VRF (which are programmed as onlink on l3-vni SVI) as well as
1114 * connected routes leaked into a VRF.
1115 */
1116 if (attr->nh_type == NEXTHOP_TYPE_BLACKHOLE) {
1117 api_nh->type = attr->nh_type;
1118 api_nh->bh_type = attr->bh_type;
1119 } else if (is_evpn) {
1120 /*
1121 * If the nexthop is EVPN overlay index gateway IP,
1122 * treat the nexthop as NEXTHOP_TYPE_IPV4
1123 * Else, mark the nexthop as onlink.
1124 */
1125 if (attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP)
1126 api_nh->type = NEXTHOP_TYPE_IPV4;
1127 else {
1128 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1129 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK);
1130 api_nh->ifindex = nh_bgp->l3vni_svi_ifindex;
1131 }
1132 } else if (nh_othervrf && api_nh->gate.ipv4.s_addr == INADDR_ANY) {
1133 api_nh->type = NEXTHOP_TYPE_IFINDEX;
1134 api_nh->ifindex = attr->nh_ifindex;
1135 } else
1136 api_nh->type = NEXTHOP_TYPE_IPV4;
1137
1138 return true;
1139 }
1140
1141 static bool update_ipv6nh_for_route_install(int nh_othervrf, struct bgp *nh_bgp,
1142 struct in6_addr *nexthop,
1143 ifindex_t ifindex,
1144 struct bgp_path_info *pi,
1145 struct bgp_path_info *best_pi,
1146 bool is_evpn,
1147 struct zapi_nexthop *api_nh)
1148 {
1149 struct attr *attr;
1150
1151 attr = pi->attr;
1152 api_nh->vrf_id = nh_bgp->vrf_id;
1153
1154 if (attr->nh_type == NEXTHOP_TYPE_BLACKHOLE) {
1155 api_nh->type = attr->nh_type;
1156 api_nh->bh_type = attr->bh_type;
1157 } else if (is_evpn) {
1158 /*
1159 * If the nexthop is EVPN overlay index gateway IP,
1160 * treat the nexthop as NEXTHOP_TYPE_IPV4
1161 * Else, mark the nexthop as onlink.
1162 */
1163 if (attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP)
1164 api_nh->type = NEXTHOP_TYPE_IPV6;
1165 else {
1166 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1167 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK);
1168 api_nh->ifindex = nh_bgp->l3vni_svi_ifindex;
1169 }
1170 } else if (nh_othervrf) {
1171 if (IN6_IS_ADDR_UNSPECIFIED(nexthop)) {
1172 api_nh->type = NEXTHOP_TYPE_IFINDEX;
1173 api_nh->ifindex = attr->nh_ifindex;
1174 } else if (IN6_IS_ADDR_LINKLOCAL(nexthop)) {
1175 if (ifindex == 0)
1176 return false;
1177 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1178 api_nh->ifindex = ifindex;
1179 } else {
1180 api_nh->type = NEXTHOP_TYPE_IPV6;
1181 api_nh->ifindex = 0;
1182 }
1183 } else {
1184 if (IN6_IS_ADDR_LINKLOCAL(nexthop)) {
1185 if (pi == best_pi
1186 && attr->mp_nexthop_len
1187 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
1188 if (pi->peer->nexthop.ifp)
1189 ifindex =
1190 pi->peer->nexthop.ifp->ifindex;
1191 if (!ifindex) {
1192 if (pi->peer->conf_if)
1193 ifindex = pi->peer->ifp->ifindex;
1194 else if (pi->peer->ifname)
1195 ifindex = ifname2ifindex(
1196 pi->peer->ifname,
1197 pi->peer->bgp->vrf_id);
1198 else if (pi->peer->nexthop.ifp)
1199 ifindex =
1200 pi->peer->nexthop.ifp->ifindex;
1201 }
1202
1203 if (ifindex == 0)
1204 return false;
1205 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1206 api_nh->ifindex = ifindex;
1207 } else {
1208 api_nh->type = NEXTHOP_TYPE_IPV6;
1209 api_nh->ifindex = 0;
1210 }
1211 }
1212 /* api_nh structure has union of gate and bh_type */
1213 if (nexthop && api_nh->type != NEXTHOP_TYPE_BLACKHOLE)
1214 api_nh->gate.ipv6 = *nexthop;
1215
1216 return true;
1217 }
1218
1219 static bool bgp_zebra_use_nhop_weighted(struct bgp *bgp, struct attr *attr,
1220 uint64_t tot_bw, uint32_t *nh_weight)
1221 {
1222 uint32_t bw;
1223 uint64_t tmp;
1224
1225 bw = attr->link_bw;
1226 /* zero link-bandwidth and link-bandwidth not present are treated
1227 * as the same situation.
1228 */
1229 if (!bw) {
1230 /* the only situations should be if we're either told
1231 * to skip or use default weight.
1232 */
1233 if (bgp->lb_handling == BGP_LINK_BW_SKIP_MISSING)
1234 return false;
1235 *nh_weight = BGP_ZEBRA_DEFAULT_NHOP_WEIGHT;
1236 } else {
1237 tmp = (uint64_t)bw * 100;
1238 *nh_weight = ((uint32_t)(tmp / tot_bw));
1239 }
1240
1241 return true;
1242 }
1243
1244 void bgp_zebra_announce(struct bgp_dest *dest, const struct prefix *p,
1245 struct bgp_path_info *info, struct bgp *bgp, afi_t afi,
1246 safi_t safi)
1247 {
1248 struct zapi_route api = { 0 };
1249 struct zapi_nexthop *api_nh;
1250 int nh_family;
1251 unsigned int valid_nh_count = 0;
1252 bool allow_recursion = false;
1253 uint8_t distance;
1254 struct peer *peer;
1255 struct bgp_path_info *mpinfo;
1256 struct bgp *bgp_orig;
1257 uint32_t metric;
1258 struct attr local_attr;
1259 struct bgp_path_info local_info;
1260 struct bgp_path_info *mpinfo_cp = &local_info;
1261 route_tag_t tag;
1262 mpls_label_t label;
1263 struct bgp_sid_info *sid_info;
1264 int nh_othervrf = 0;
1265 bool is_evpn;
1266 bool nh_updated = false;
1267 bool do_wt_ecmp;
1268 uint64_t cum_bw = 0;
1269 uint32_t nhg_id = 0;
1270 bool is_add;
1271
1272 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1273 * know of this instance.
1274 */
1275 if (!bgp_install_info_to_zebra(bgp))
1276 return;
1277
1278 if (bgp->main_zebra_update_hold)
1279 return;
1280
1281 if (safi == SAFI_FLOWSPEC) {
1282 bgp_pbr_update_entry(bgp, bgp_dest_get_prefix(dest), info, afi,
1283 safi, true);
1284 return;
1285 }
1286
1287 /*
1288 * vrf leaking support (will have only one nexthop)
1289 */
1290 if (info->extra && info->extra->bgp_orig)
1291 nh_othervrf = 1;
1292
1293 /* Make Zebra API structure. */
1294 api.vrf_id = bgp->vrf_id;
1295 api.type = ZEBRA_ROUTE_BGP;
1296 api.safi = safi;
1297 api.prefix = *p;
1298 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
1299
1300 peer = info->peer;
1301
1302 if (info->type == ZEBRA_ROUTE_BGP
1303 && info->sub_type == BGP_ROUTE_IMPORTED) {
1304
1305 /* Obtain peer from parent */
1306 if (info->extra && info->extra->parent)
1307 peer = ((struct bgp_path_info *)(info->extra->parent))
1308 ->peer;
1309 }
1310
1311 tag = info->attr->tag;
1312
1313 /* If the route's source is EVPN, flag as such. */
1314 is_evpn = is_route_parent_evpn(info);
1315 if (is_evpn)
1316 SET_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE);
1317
1318 if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED
1319 || info->sub_type == BGP_ROUTE_AGGREGATE) {
1320 SET_FLAG(api.flags, ZEBRA_FLAG_IBGP);
1321 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1322 }
1323
1324 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != BGP_DEFAULT_TTL)
1325 || CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1326 || CHECK_FLAG(bgp->flags, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1327
1328 allow_recursion = true;
1329
1330 if (info->attr->rmap_table_id) {
1331 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
1332 api.tableid = info->attr->rmap_table_id;
1333 }
1334
1335 if (CHECK_FLAG(info->attr->flag, ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR)))
1336 SET_FLAG(api.message, ZAPI_MESSAGE_SRTE);
1337
1338 /* Metric is currently based on the best-path only */
1339 metric = info->attr->med;
1340
1341 /* Determine if we're doing weighted ECMP or not */
1342 do_wt_ecmp = bgp_path_info_mpath_chkwtd(bgp, info);
1343 if (do_wt_ecmp)
1344 cum_bw = bgp_path_info_mpath_cumbw(info);
1345
1346 /* EVPN MAC-IP routes are installed with a L3 NHG id */
1347 if (bgp_evpn_path_es_use_nhg(bgp, info, &nhg_id)) {
1348 mpinfo = NULL;
1349 api.nhgid = nhg_id;
1350 if (nhg_id)
1351 SET_FLAG(api.message, ZAPI_MESSAGE_NHG);
1352 } else {
1353 mpinfo = info;
1354 }
1355
1356 for (; mpinfo; mpinfo = bgp_path_info_mpath_next(mpinfo)) {
1357 uint32_t nh_weight;
1358
1359 if (valid_nh_count >= multipath_num)
1360 break;
1361
1362 *mpinfo_cp = *mpinfo;
1363 nh_weight = 0;
1364
1365 /* Get nexthop address-family */
1366 if (p->family == AF_INET
1367 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp->attr))
1368 nh_family = AF_INET;
1369 else if (p->family == AF_INET6
1370 || (p->family == AF_INET
1371 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp->attr)))
1372 nh_family = AF_INET6;
1373 else
1374 continue;
1375
1376 /* If processing for weighted ECMP, determine the next hop's
1377 * weight. Based on user setting, we may skip the next hop
1378 * in some situations.
1379 */
1380 if (do_wt_ecmp) {
1381 if (!bgp_zebra_use_nhop_weighted(bgp, mpinfo->attr,
1382 cum_bw, &nh_weight))
1383 continue;
1384 }
1385 api_nh = &api.nexthops[valid_nh_count];
1386
1387 if (CHECK_FLAG(info->attr->flag,
1388 ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR)))
1389 api_nh->srte_color = info->attr->srte_color;
1390
1391 if (bgp_debug_zebra(&api.prefix)) {
1392 if (mpinfo->extra) {
1393 zlog_debug("%s: p=%pFX, bgp_is_valid_label: %d",
1394 __func__, p,
1395 bgp_is_valid_label(
1396 &mpinfo->extra->label[0]));
1397 } else {
1398 zlog_debug(
1399 "%s: p=%pFX, extra is NULL, no label",
1400 __func__, p);
1401 }
1402 }
1403
1404 if (bgp->table_map[afi][safi].name) {
1405 /* Copy info and attributes, so the route-map
1406 apply doesn't modify the BGP route info. */
1407 local_attr = *mpinfo->attr;
1408 mpinfo_cp->attr = &local_attr;
1409 if (!bgp_table_map_apply(bgp->table_map[afi][safi].map,
1410 p, mpinfo_cp))
1411 continue;
1412
1413 /* metric/tag is only allowed to be
1414 * overridden on 1st nexthop */
1415 if (mpinfo == info) {
1416 metric = mpinfo_cp->attr->med;
1417 tag = mpinfo_cp->attr->tag;
1418 }
1419 }
1420
1421 BGP_ORIGINAL_UPDATE(bgp_orig, mpinfo, bgp);
1422
1423 if (nh_family == AF_INET) {
1424 nh_updated = update_ipv4nh_for_route_install(
1425 nh_othervrf, bgp_orig,
1426 &mpinfo_cp->attr->nexthop, mpinfo_cp->attr,
1427 is_evpn, api_nh);
1428 } else {
1429 ifindex_t ifindex = IFINDEX_INTERNAL;
1430 struct in6_addr *nexthop;
1431
1432 nexthop = bgp_path_info_to_ipv6_nexthop(mpinfo_cp,
1433 &ifindex);
1434
1435 if (!nexthop)
1436 nh_updated = update_ipv4nh_for_route_install(
1437 nh_othervrf, bgp_orig,
1438 &mpinfo_cp->attr->nexthop,
1439 mpinfo_cp->attr, is_evpn, api_nh);
1440 else
1441 nh_updated = update_ipv6nh_for_route_install(
1442 nh_othervrf, bgp_orig, nexthop, ifindex,
1443 mpinfo, info, is_evpn, api_nh);
1444 }
1445
1446 /* Did we get proper nexthop info to update zebra? */
1447 if (!nh_updated)
1448 continue;
1449
1450 /* Allow recursion if it is a multipath group with both
1451 * eBGP and iBGP paths.
1452 */
1453 if (!allow_recursion
1454 && CHECK_FLAG(bgp->flags, BGP_FLAG_PEERTYPE_MULTIPATH_RELAX)
1455 && (mpinfo->peer->sort == BGP_PEER_IBGP
1456 || mpinfo->peer->sort == BGP_PEER_CONFED))
1457 allow_recursion = true;
1458
1459 if (mpinfo->extra
1460 && bgp_is_valid_label(&mpinfo->extra->label[0])
1461 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1462 label = label_pton(&mpinfo->extra->label[0]);
1463
1464 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL);
1465
1466 api_nh->label_num = 1;
1467 api_nh->labels[0] = label;
1468 }
1469
1470 if (is_evpn
1471 && mpinfo->attr->evpn_overlay.type
1472 != OVERLAY_INDEX_GATEWAY_IP)
1473 memcpy(&api_nh->rmac, &(mpinfo->attr->rmac),
1474 sizeof(struct ethaddr));
1475
1476 api_nh->weight = nh_weight;
1477
1478 if (mpinfo->extra && !sid_zero(&mpinfo->extra->sid[0].sid)
1479 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1480 sid_info = &mpinfo->extra->sid[0];
1481
1482 memcpy(&api_nh->seg6_segs, &sid_info->sid,
1483 sizeof(api_nh->seg6_segs));
1484
1485 if (sid_info->transposition_len != 0) {
1486 if (!bgp_is_valid_label(
1487 &mpinfo->extra->label[0]))
1488 continue;
1489
1490 label = label_pton(&mpinfo->extra->label[0]);
1491 transpose_sid(&api_nh->seg6_segs, label,
1492 sid_info->transposition_offset,
1493 sid_info->transposition_len);
1494 }
1495
1496 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6);
1497 }
1498
1499 valid_nh_count++;
1500 }
1501
1502 is_add = (valid_nh_count || nhg_id) ? true : false;
1503
1504 if (is_add && CHECK_FLAG(bm->flags, BM_FLAG_SEND_EXTRA_DATA_TO_ZEBRA)) {
1505 struct bgp_zebra_opaque bzo = {};
1506 const char *reason =
1507 bgp_path_selection_reason2str(dest->reason);
1508
1509 strlcpy(bzo.aspath, info->attr->aspath->str,
1510 sizeof(bzo.aspath));
1511
1512 if (info->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES))
1513 strlcpy(bzo.community, info->attr->community->str,
1514 sizeof(bzo.community));
1515
1516 if (info->attr->flag
1517 & ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES))
1518 strlcpy(bzo.lcommunity,
1519 bgp_attr_get_lcommunity(info->attr)->str,
1520 sizeof(bzo.lcommunity));
1521
1522 strlcpy(bzo.selection_reason, reason,
1523 sizeof(bzo.selection_reason));
1524
1525 SET_FLAG(api.message, ZAPI_MESSAGE_OPAQUE);
1526 api.opaque.length = MIN(sizeof(struct bgp_zebra_opaque),
1527 ZAPI_MESSAGE_OPAQUE_LENGTH);
1528 memcpy(api.opaque.data, &bzo, api.opaque.length);
1529 }
1530
1531 if (allow_recursion)
1532 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1533
1534 /*
1535 * When we create an aggregate route we must also
1536 * install a Null0 route in the RIB, so overwrite
1537 * what was written into api with a blackhole route
1538 */
1539 if (info->sub_type == BGP_ROUTE_AGGREGATE)
1540 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
1541 else
1542 api.nexthop_num = valid_nh_count;
1543
1544 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
1545 api.metric = metric;
1546
1547 if (tag) {
1548 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1549 api.tag = tag;
1550 }
1551
1552 distance = bgp_distance_apply(p, info, afi, safi, bgp);
1553 if (distance) {
1554 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
1555 api.distance = distance;
1556 }
1557
1558 if (bgp_debug_zebra(p)) {
1559 char nh_buf[INET6_ADDRSTRLEN];
1560 char eth_buf[ETHER_ADDR_STRLEN + 7] = {'\0'};
1561 char buf1[ETHER_ADDR_STRLEN];
1562 char label_buf[20];
1563 char sid_buf[20];
1564 char segs_buf[256];
1565 int i;
1566
1567 zlog_debug(
1568 "Tx route %s VRF %u %pFX metric %u tag %" ROUTE_TAG_PRI
1569 " count %d nhg %d",
1570 valid_nh_count ? "add" : "delete", bgp->vrf_id,
1571 &api.prefix, api.metric, api.tag, api.nexthop_num,
1572 nhg_id);
1573 for (i = 0; i < api.nexthop_num; i++) {
1574 api_nh = &api.nexthops[i];
1575
1576 switch (api_nh->type) {
1577 case NEXTHOP_TYPE_IFINDEX:
1578 nh_buf[0] = '\0';
1579 break;
1580 case NEXTHOP_TYPE_IPV4:
1581 case NEXTHOP_TYPE_IPV4_IFINDEX:
1582 nh_family = AF_INET;
1583 inet_ntop(nh_family, &api_nh->gate, nh_buf,
1584 sizeof(nh_buf));
1585 break;
1586 case NEXTHOP_TYPE_IPV6:
1587 case NEXTHOP_TYPE_IPV6_IFINDEX:
1588 nh_family = AF_INET6;
1589 inet_ntop(nh_family, &api_nh->gate, nh_buf,
1590 sizeof(nh_buf));
1591 break;
1592 case NEXTHOP_TYPE_BLACKHOLE:
1593 strlcpy(nh_buf, "blackhole", sizeof(nh_buf));
1594 break;
1595 default:
1596 /* Note: add new nexthop case */
1597 assert(0);
1598 break;
1599 }
1600
1601 label_buf[0] = '\0';
1602 eth_buf[0] = '\0';
1603 segs_buf[0] = '\0';
1604 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL)
1605 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE))
1606 snprintf(label_buf, sizeof(label_buf),
1607 "label %u", api_nh->labels[0]);
1608 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6)
1609 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1610 inet_ntop(AF_INET6, &api_nh->seg6_segs,
1611 sid_buf, sizeof(sid_buf));
1612 snprintf(segs_buf, sizeof(segs_buf), "segs %s",
1613 sid_buf);
1614 }
1615 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)
1616 && !is_zero_mac(&api_nh->rmac))
1617 snprintf(eth_buf, sizeof(eth_buf), " RMAC %s",
1618 prefix_mac2str(&api_nh->rmac,
1619 buf1, sizeof(buf1)));
1620 zlog_debug(" nhop [%d]: %s if %u VRF %u wt %u %s %s %s",
1621 i + 1, nh_buf, api_nh->ifindex,
1622 api_nh->vrf_id, api_nh->weight,
1623 label_buf, segs_buf, eth_buf);
1624 }
1625
1626 int recursion_flag = 0;
1627
1628 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION))
1629 recursion_flag = 1;
1630
1631 zlog_debug("%s: %pFX: announcing to zebra (recursion %sset)",
1632 __func__, p, (recursion_flag ? "" : "NOT "));
1633 }
1634 zclient_route_send(is_add ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE,
1635 zclient, &api);
1636 }
1637
1638 /* Announce all routes of a table to zebra */
1639 void bgp_zebra_announce_table(struct bgp *bgp, afi_t afi, safi_t safi)
1640 {
1641 struct bgp_dest *dest;
1642 struct bgp_table *table;
1643 struct bgp_path_info *pi;
1644
1645 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1646 * know of this instance.
1647 */
1648 if (!bgp_install_info_to_zebra(bgp))
1649 return;
1650
1651 table = bgp->rib[afi][safi];
1652 if (!table)
1653 return;
1654
1655 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest))
1656 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
1657 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED) &&
1658
1659 (pi->type == ZEBRA_ROUTE_BGP
1660 && (pi->sub_type == BGP_ROUTE_NORMAL
1661 || pi->sub_type == BGP_ROUTE_IMPORTED)))
1662
1663 bgp_zebra_announce(dest,
1664 bgp_dest_get_prefix(dest),
1665 pi, bgp, afi, safi);
1666 }
1667
1668 /* Announce routes of any bgp subtype of a table to zebra */
1669 void bgp_zebra_announce_table_all_subtypes(struct bgp *bgp, afi_t afi,
1670 safi_t safi)
1671 {
1672 struct bgp_dest *dest;
1673 struct bgp_table *table;
1674 struct bgp_path_info *pi;
1675
1676 if (!bgp_install_info_to_zebra(bgp))
1677 return;
1678
1679 table = bgp->rib[afi][safi];
1680 if (!table)
1681 return;
1682
1683 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest))
1684 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
1685 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED) &&
1686 pi->type == ZEBRA_ROUTE_BGP)
1687 bgp_zebra_announce(dest,
1688 bgp_dest_get_prefix(dest),
1689 pi, bgp, afi, safi);
1690 }
1691
1692 void bgp_zebra_withdraw(const struct prefix *p, struct bgp_path_info *info,
1693 struct bgp *bgp, safi_t safi)
1694 {
1695 struct zapi_route api;
1696 struct peer *peer;
1697
1698 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1699 * know of this instance.
1700 */
1701 if (!bgp_install_info_to_zebra(bgp))
1702 return;
1703
1704 if (safi == SAFI_FLOWSPEC) {
1705 peer = info->peer;
1706 bgp_pbr_update_entry(peer->bgp, p, info, AFI_IP, safi, false);
1707 return;
1708 }
1709
1710 memset(&api, 0, sizeof(api));
1711 api.vrf_id = bgp->vrf_id;
1712 api.type = ZEBRA_ROUTE_BGP;
1713 api.safi = safi;
1714 api.prefix = *p;
1715
1716 if (info->attr->rmap_table_id) {
1717 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
1718 api.tableid = info->attr->rmap_table_id;
1719 }
1720
1721 /* If the route's source is EVPN, flag as such. */
1722 if (is_route_parent_evpn(info))
1723 SET_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE);
1724
1725 if (bgp_debug_zebra(p))
1726 zlog_debug("Tx route delete VRF %u %pFX", bgp->vrf_id,
1727 &api.prefix);
1728
1729 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
1730 }
1731
1732 /* Withdraw all entries in a BGP instances RIB table from Zebra */
1733 void bgp_zebra_withdraw_table_all_subtypes(struct bgp *bgp, afi_t afi, safi_t safi)
1734 {
1735 struct bgp_dest *dest;
1736 struct bgp_table *table;
1737 struct bgp_path_info *pi;
1738
1739 if (!bgp_install_info_to_zebra(bgp))
1740 return;
1741
1742 table = bgp->rib[afi][safi];
1743 if (!table)
1744 return;
1745
1746 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
1747 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
1748 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
1749 && (pi->type == ZEBRA_ROUTE_BGP))
1750 bgp_zebra_withdraw(bgp_dest_get_prefix(dest),
1751 pi, bgp, safi);
1752 }
1753 }
1754 }
1755
1756 struct bgp_redist *bgp_redist_lookup(struct bgp *bgp, afi_t afi, uint8_t type,
1757 unsigned short instance)
1758 {
1759 struct list *red_list;
1760 struct listnode *node;
1761 struct bgp_redist *red;
1762
1763 red_list = bgp->redist[afi][type];
1764 if (!red_list)
1765 return (NULL);
1766
1767 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
1768 if (red->instance == instance)
1769 return red;
1770
1771 return NULL;
1772 }
1773
1774 struct bgp_redist *bgp_redist_add(struct bgp *bgp, afi_t afi, uint8_t type,
1775 unsigned short instance)
1776 {
1777 struct list *red_list;
1778 struct bgp_redist *red;
1779
1780 red = bgp_redist_lookup(bgp, afi, type, instance);
1781 if (red)
1782 return red;
1783
1784 if (!bgp->redist[afi][type])
1785 bgp->redist[afi][type] = list_new();
1786
1787 red_list = bgp->redist[afi][type];
1788 red = XCALLOC(MTYPE_BGP_REDIST, sizeof(struct bgp_redist));
1789 red->instance = instance;
1790
1791 listnode_add(red_list, red);
1792
1793 return red;
1794 }
1795
1796 static void bgp_redist_del(struct bgp *bgp, afi_t afi, uint8_t type,
1797 unsigned short instance)
1798 {
1799 struct bgp_redist *red;
1800
1801 red = bgp_redist_lookup(bgp, afi, type, instance);
1802
1803 if (red) {
1804 listnode_delete(bgp->redist[afi][type], red);
1805 XFREE(MTYPE_BGP_REDIST, red);
1806 if (!bgp->redist[afi][type]->count)
1807 list_delete(&bgp->redist[afi][type]);
1808 }
1809 }
1810
1811 /* Other routes redistribution into BGP. */
1812 int bgp_redistribute_set(struct bgp *bgp, afi_t afi, int type,
1813 unsigned short instance, bool changed)
1814 {
1815 /* If redistribute options are changed call
1816 * bgp_redistribute_unreg() to reset the option and withdraw
1817 * the routes
1818 */
1819 if (changed)
1820 bgp_redistribute_unreg(bgp, afi, type, instance);
1821
1822 /* Return if already redistribute flag is set. */
1823 if (instance) {
1824 if (redist_check_instance(&zclient->mi_redist[afi][type],
1825 instance))
1826 return CMD_WARNING;
1827
1828 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1829 } else {
1830 if (vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1831 return CMD_WARNING;
1832
1833 #ifdef ENABLE_BGP_VNC
1834 if (EVPN_ENABLED(bgp) && type == ZEBRA_ROUTE_VNC_DIRECT) {
1835 vnc_export_bgp_enable(
1836 bgp, afi); /* only enables if mode bits cfg'd */
1837 }
1838 #endif
1839
1840 vrf_bitmap_set(zclient->redist[afi][type], bgp->vrf_id);
1841 }
1842
1843 /*
1844 * Don't try to register if we're not connected to Zebra or Zebra
1845 * doesn't know of this instance.
1846 *
1847 * When we come up later well resend if needed.
1848 */
1849 if (!bgp_install_info_to_zebra(bgp))
1850 return CMD_SUCCESS;
1851
1852 if (BGP_DEBUG(zebra, ZEBRA))
1853 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1854 bgp->vrf_id, afi, zebra_route_string(type),
1855 instance);
1856
1857 /* Send distribute add message to zebra. */
1858 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1859 instance, bgp->vrf_id);
1860
1861 return CMD_SUCCESS;
1862 }
1863
1864 int bgp_redistribute_resend(struct bgp *bgp, afi_t afi, int type,
1865 unsigned short instance)
1866 {
1867 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1868 * know of this instance.
1869 */
1870 if (!bgp_install_info_to_zebra(bgp))
1871 return -1;
1872
1873 if (BGP_DEBUG(zebra, ZEBRA))
1874 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1875 bgp->vrf_id, afi, zebra_route_string(type),
1876 instance);
1877
1878 /* Send distribute add message to zebra. */
1879 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type,
1880 instance, bgp->vrf_id);
1881 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1882 instance, bgp->vrf_id);
1883
1884 return 0;
1885 }
1886
1887 /* Redistribute with route-map specification. */
1888 bool bgp_redistribute_rmap_set(struct bgp_redist *red, const char *name,
1889 struct route_map *route_map)
1890 {
1891 if (red->rmap.name && (strcmp(red->rmap.name, name) == 0))
1892 return false;
1893
1894 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1895 /* Decrement the count for existing routemap and
1896 * increment the count for new route map.
1897 */
1898 route_map_counter_decrement(red->rmap.map);
1899 red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
1900 red->rmap.map = route_map;
1901 route_map_counter_increment(red->rmap.map);
1902
1903 return true;
1904 }
1905
1906 /* Redistribute with metric specification. */
1907 bool bgp_redistribute_metric_set(struct bgp *bgp, struct bgp_redist *red,
1908 afi_t afi, int type, uint32_t metric)
1909 {
1910 struct bgp_dest *dest;
1911 struct bgp_path_info *pi;
1912
1913 if (red->redist_metric_flag && red->redist_metric == metric)
1914 return false;
1915
1916 red->redist_metric_flag = 1;
1917 red->redist_metric = metric;
1918
1919 for (dest = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); dest;
1920 dest = bgp_route_next(dest)) {
1921 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
1922 if (pi->sub_type == BGP_ROUTE_REDISTRIBUTE
1923 && pi->type == type
1924 && pi->instance == red->instance) {
1925 struct attr *old_attr;
1926 struct attr new_attr;
1927
1928 new_attr = *pi->attr;
1929 new_attr.med = red->redist_metric;
1930 old_attr = pi->attr;
1931 pi->attr = bgp_attr_intern(&new_attr);
1932 bgp_attr_unintern(&old_attr);
1933
1934 bgp_path_info_set_flag(dest, pi,
1935 BGP_PATH_ATTR_CHANGED);
1936 bgp_process(bgp, dest, afi, SAFI_UNICAST);
1937 }
1938 }
1939 }
1940
1941 return true;
1942 }
1943
1944 /* Unset redistribution. */
1945 int bgp_redistribute_unreg(struct bgp *bgp, afi_t afi, int type,
1946 unsigned short instance)
1947 {
1948 struct bgp_redist *red;
1949
1950 red = bgp_redist_lookup(bgp, afi, type, instance);
1951 if (!red)
1952 return CMD_SUCCESS;
1953
1954 /* Return if zebra connection is disabled. */
1955 if (instance) {
1956 if (!redist_check_instance(&zclient->mi_redist[afi][type],
1957 instance))
1958 return CMD_WARNING;
1959 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1960 } else {
1961 if (!vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1962 return CMD_WARNING;
1963 vrf_bitmap_unset(zclient->redist[afi][type], bgp->vrf_id);
1964 }
1965
1966 if (bgp_install_info_to_zebra(bgp)) {
1967 /* Send distribute delete message to zebra. */
1968 if (BGP_DEBUG(zebra, ZEBRA))
1969 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1970 bgp->vrf_id, afi, zebra_route_string(type),
1971 instance);
1972 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi,
1973 type, instance, bgp->vrf_id);
1974 }
1975
1976 /* Withdraw redistributed routes from current BGP's routing table. */
1977 bgp_redistribute_withdraw(bgp, afi, type, instance);
1978
1979 return CMD_SUCCESS;
1980 }
1981
1982 /* Unset redistribution. */
1983 int bgp_redistribute_unset(struct bgp *bgp, afi_t afi, int type,
1984 unsigned short instance)
1985 {
1986 struct bgp_redist *red;
1987
1988 /*
1989 * vnc and vpn->vrf checks must be before red check because
1990 * they operate within bgpd irrespective of zebra connection
1991 * status. red lookup fails if there is no zebra connection.
1992 */
1993 #ifdef ENABLE_BGP_VNC
1994 if (EVPN_ENABLED(bgp) && type == ZEBRA_ROUTE_VNC_DIRECT) {
1995 vnc_export_bgp_disable(bgp, afi);
1996 }
1997 #endif
1998
1999 red = bgp_redist_lookup(bgp, afi, type, instance);
2000 if (!red)
2001 return CMD_SUCCESS;
2002
2003 bgp_redistribute_unreg(bgp, afi, type, instance);
2004
2005 /* Unset route-map. */
2006 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
2007 route_map_counter_decrement(red->rmap.map);
2008 red->rmap.map = NULL;
2009
2010 /* Unset metric. */
2011 red->redist_metric_flag = 0;
2012 red->redist_metric = 0;
2013
2014 bgp_redist_del(bgp, afi, type, instance);
2015
2016 return CMD_SUCCESS;
2017 }
2018
2019 void bgp_redistribute_redo(struct bgp *bgp)
2020 {
2021 afi_t afi;
2022 int i;
2023 struct list *red_list;
2024 struct listnode *node;
2025 struct bgp_redist *red;
2026
2027 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
2028 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
2029
2030 red_list = bgp->redist[afi][i];
2031 if (!red_list)
2032 continue;
2033
2034 for (ALL_LIST_ELEMENTS_RO(red_list, node, red)) {
2035 bgp_redistribute_resend(bgp, afi, i,
2036 red->instance);
2037 }
2038 }
2039 }
2040 }
2041
2042 void bgp_zclient_reset(void)
2043 {
2044 zclient_reset(zclient);
2045 }
2046
2047 /* Register this instance with Zebra. Invoked upon connect (for
2048 * default instance) and when other VRFs are learnt (or created and
2049 * already learnt).
2050 */
2051 void bgp_zebra_instance_register(struct bgp *bgp)
2052 {
2053 /* Don't try to register if we're not connected to Zebra */
2054 if (!zclient || zclient->sock < 0)
2055 return;
2056
2057 if (BGP_DEBUG(zebra, ZEBRA))
2058 zlog_debug("Registering VRF %u", bgp->vrf_id);
2059
2060 /* Register for router-id, interfaces, redistributed routes. */
2061 zclient_send_reg_requests(zclient, bgp->vrf_id);
2062
2063 /* For EVPN instance, register to learn about VNIs, if appropriate. */
2064 if (bgp->advertise_all_vni)
2065 bgp_zebra_advertise_all_vni(bgp, 1);
2066
2067 bgp_nht_register_nexthops(bgp);
2068 }
2069
2070 /* Deregister this instance with Zebra. Invoked upon the instance
2071 * being deleted (default or VRF) and it is already registered.
2072 */
2073 void bgp_zebra_instance_deregister(struct bgp *bgp)
2074 {
2075 /* Don't try to deregister if we're not connected to Zebra */
2076 if (zclient->sock < 0)
2077 return;
2078
2079 if (BGP_DEBUG(zebra, ZEBRA))
2080 zlog_debug("Deregistering VRF %u", bgp->vrf_id);
2081
2082 /* For EVPN instance, unregister learning about VNIs, if appropriate. */
2083 if (bgp->advertise_all_vni)
2084 bgp_zebra_advertise_all_vni(bgp, 0);
2085
2086 /* Deregister for router-id, interfaces, redistributed routes. */
2087 zclient_send_dereg_requests(zclient, bgp->vrf_id);
2088 }
2089
2090 void bgp_zebra_initiate_radv(struct bgp *bgp, struct peer *peer)
2091 {
2092 uint32_t ra_interval = BGP_UNNUM_DEFAULT_RA_INTERVAL;
2093
2094 /* Don't try to initiate if we're not connected to Zebra */
2095 if (zclient->sock < 0)
2096 return;
2097
2098 if (BGP_DEBUG(zebra, ZEBRA))
2099 zlog_debug("%u: Initiating RA for peer %s", bgp->vrf_id,
2100 peer->host);
2101
2102 /*
2103 * If unnumbered peer (peer->ifp) call thru zapi to start RAs.
2104 * If we don't have an ifp pointer, call function to find the
2105 * ifps for a numbered enhe peer to turn RAs on.
2106 */
2107 peer->ifp ? zclient_send_interface_radv_req(zclient, bgp->vrf_id,
2108 peer->ifp, 1, ra_interval)
2109 : bgp_nht_reg_enhe_cap_intfs(peer);
2110 }
2111
2112 void bgp_zebra_terminate_radv(struct bgp *bgp, struct peer *peer)
2113 {
2114 /* Don't try to terminate if we're not connected to Zebra */
2115 if (zclient->sock < 0)
2116 return;
2117
2118 if (BGP_DEBUG(zebra, ZEBRA))
2119 zlog_debug("%u: Terminating RA for peer %s", bgp->vrf_id,
2120 peer->host);
2121
2122 /*
2123 * If unnumbered peer (peer->ifp) call thru zapi to stop RAs.
2124 * If we don't have an ifp pointer, call function to find the
2125 * ifps for a numbered enhe peer to turn RAs off.
2126 */
2127 peer->ifp ? zclient_send_interface_radv_req(zclient, bgp->vrf_id,
2128 peer->ifp, 0, 0)
2129 : bgp_nht_dereg_enhe_cap_intfs(peer);
2130 }
2131
2132 int bgp_zebra_advertise_subnet(struct bgp *bgp, int advertise, vni_t vni)
2133 {
2134 struct stream *s = NULL;
2135
2136 /* Check socket. */
2137 if (!zclient || zclient->sock < 0)
2138 return 0;
2139
2140 /* Don't try to register if Zebra doesn't know of this instance. */
2141 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
2142 if (BGP_DEBUG(zebra, ZEBRA))
2143 zlog_debug(
2144 "%s: No zebra instance to talk to, cannot advertise subnet",
2145 __func__);
2146 return 0;
2147 }
2148
2149 s = zclient->obuf;
2150 stream_reset(s);
2151
2152 zclient_create_header(s, ZEBRA_ADVERTISE_SUBNET, bgp->vrf_id);
2153 stream_putc(s, advertise);
2154 stream_put3(s, vni);
2155 stream_putw_at(s, 0, stream_get_endp(s));
2156
2157 return zclient_send_message(zclient);
2158 }
2159
2160 int bgp_zebra_advertise_svi_macip(struct bgp *bgp, int advertise, vni_t vni)
2161 {
2162 struct stream *s = NULL;
2163
2164 /* Check socket. */
2165 if (!zclient || zclient->sock < 0)
2166 return 0;
2167
2168 /* Don't try to register if Zebra doesn't know of this instance. */
2169 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2170 return 0;
2171
2172 s = zclient->obuf;
2173 stream_reset(s);
2174
2175 zclient_create_header(s, ZEBRA_ADVERTISE_SVI_MACIP, bgp->vrf_id);
2176 stream_putc(s, advertise);
2177 stream_putl(s, vni);
2178 stream_putw_at(s, 0, stream_get_endp(s));
2179
2180 return zclient_send_message(zclient);
2181 }
2182
2183 int bgp_zebra_advertise_gw_macip(struct bgp *bgp, int advertise, vni_t vni)
2184 {
2185 struct stream *s = NULL;
2186
2187 /* Check socket. */
2188 if (!zclient || zclient->sock < 0)
2189 return 0;
2190
2191 /* Don't try to register if Zebra doesn't know of this instance. */
2192 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
2193 if (BGP_DEBUG(zebra, ZEBRA))
2194 zlog_debug(
2195 "%s: No zebra instance to talk to, not installing gw_macip",
2196 __func__);
2197 return 0;
2198 }
2199
2200 s = zclient->obuf;
2201 stream_reset(s);
2202
2203 zclient_create_header(s, ZEBRA_ADVERTISE_DEFAULT_GW, bgp->vrf_id);
2204 stream_putc(s, advertise);
2205 stream_putl(s, vni);
2206 stream_putw_at(s, 0, stream_get_endp(s));
2207
2208 return zclient_send_message(zclient);
2209 }
2210
2211 int bgp_zebra_vxlan_flood_control(struct bgp *bgp,
2212 enum vxlan_flood_control flood_ctrl)
2213 {
2214 struct stream *s;
2215
2216 /* Check socket. */
2217 if (!zclient || zclient->sock < 0)
2218 return 0;
2219
2220 /* Don't try to register if Zebra doesn't know of this instance. */
2221 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
2222 if (BGP_DEBUG(zebra, ZEBRA))
2223 zlog_debug(
2224 "%s: No zebra instance to talk to, not installing all vni",
2225 __func__);
2226 return 0;
2227 }
2228
2229 s = zclient->obuf;
2230 stream_reset(s);
2231
2232 zclient_create_header(s, ZEBRA_VXLAN_FLOOD_CONTROL, bgp->vrf_id);
2233 stream_putc(s, flood_ctrl);
2234 stream_putw_at(s, 0, stream_get_endp(s));
2235
2236 return zclient_send_message(zclient);
2237 }
2238
2239 int bgp_zebra_advertise_all_vni(struct bgp *bgp, int advertise)
2240 {
2241 struct stream *s;
2242
2243 /* Check socket. */
2244 if (!zclient || zclient->sock < 0)
2245 return 0;
2246
2247 /* Don't try to register if Zebra doesn't know of this instance. */
2248 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2249 return 0;
2250
2251 s = zclient->obuf;
2252 stream_reset(s);
2253
2254 zclient_create_header(s, ZEBRA_ADVERTISE_ALL_VNI, bgp->vrf_id);
2255 stream_putc(s, advertise);
2256 /* Also inform current BUM handling setting. This is really
2257 * relevant only when 'advertise' is set.
2258 */
2259 stream_putc(s, bgp->vxlan_flood_ctrl);
2260 stream_putw_at(s, 0, stream_get_endp(s));
2261
2262 return zclient_send_message(zclient);
2263 }
2264
2265 int bgp_zebra_dup_addr_detection(struct bgp *bgp)
2266 {
2267 struct stream *s;
2268
2269 /* Check socket. */
2270 if (!zclient || zclient->sock < 0)
2271 return 0;
2272
2273 /* Don't try to register if Zebra doesn't know of this instance. */
2274 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2275 return 0;
2276
2277 if (BGP_DEBUG(zebra, ZEBRA))
2278 zlog_debug("dup addr detect %s max_moves %u time %u freeze %s freeze_time %u",
2279 bgp->evpn_info->dup_addr_detect ?
2280 "enable" : "disable",
2281 bgp->evpn_info->dad_max_moves,
2282 bgp->evpn_info->dad_time,
2283 bgp->evpn_info->dad_freeze ?
2284 "enable" : "disable",
2285 bgp->evpn_info->dad_freeze_time);
2286
2287 s = zclient->obuf;
2288 stream_reset(s);
2289 zclient_create_header(s, ZEBRA_DUPLICATE_ADDR_DETECTION,
2290 bgp->vrf_id);
2291 stream_putl(s, bgp->evpn_info->dup_addr_detect);
2292 stream_putl(s, bgp->evpn_info->dad_time);
2293 stream_putl(s, bgp->evpn_info->dad_max_moves);
2294 stream_putl(s, bgp->evpn_info->dad_freeze);
2295 stream_putl(s, bgp->evpn_info->dad_freeze_time);
2296 stream_putw_at(s, 0, stream_get_endp(s));
2297
2298 return zclient_send_message(zclient);
2299 }
2300
2301 static int rule_notify_owner(ZAPI_CALLBACK_ARGS)
2302 {
2303 uint32_t seqno, priority, unique;
2304 enum zapi_rule_notify_owner note;
2305 struct bgp_pbr_action *bgp_pbra;
2306 struct bgp_pbr_rule *bgp_pbr = NULL;
2307 char ifname[INTERFACE_NAMSIZ + 1];
2308
2309 if (!zapi_rule_notify_decode(zclient->ibuf, &seqno, &priority, &unique,
2310 ifname, &note))
2311 return -1;
2312
2313 bgp_pbra = bgp_pbr_action_rule_lookup(vrf_id, unique);
2314 if (!bgp_pbra) {
2315 /* look in bgp pbr rule */
2316 bgp_pbr = bgp_pbr_rule_lookup(vrf_id, unique);
2317 if (!bgp_pbr && note != ZAPI_RULE_REMOVED) {
2318 if (BGP_DEBUG(zebra, ZEBRA))
2319 zlog_debug("%s: Fail to look BGP rule (%u)",
2320 __func__, unique);
2321 return 0;
2322 }
2323 }
2324
2325 switch (note) {
2326 case ZAPI_RULE_FAIL_INSTALL:
2327 if (BGP_DEBUG(zebra, ZEBRA))
2328 zlog_debug("%s: Received RULE_FAIL_INSTALL", __func__);
2329 if (bgp_pbra) {
2330 bgp_pbra->installed = false;
2331 bgp_pbra->install_in_progress = false;
2332 } else {
2333 bgp_pbr->installed = false;
2334 bgp_pbr->install_in_progress = false;
2335 }
2336 break;
2337 case ZAPI_RULE_INSTALLED:
2338 if (bgp_pbra) {
2339 bgp_pbra->installed = true;
2340 bgp_pbra->install_in_progress = false;
2341 } else {
2342 struct bgp_path_info *path;
2343 struct bgp_path_info_extra *extra;
2344
2345 bgp_pbr->installed = true;
2346 bgp_pbr->install_in_progress = false;
2347 bgp_pbr->action->refcnt++;
2348 /* link bgp_info to bgp_pbr */
2349 path = (struct bgp_path_info *)bgp_pbr->path;
2350 extra = bgp_path_info_extra_get(path);
2351 listnode_add_force(&extra->bgp_fs_iprule,
2352 bgp_pbr);
2353 }
2354 if (BGP_DEBUG(zebra, ZEBRA))
2355 zlog_debug("%s: Received RULE_INSTALLED", __func__);
2356 break;
2357 case ZAPI_RULE_FAIL_REMOVE:
2358 case ZAPI_RULE_REMOVED:
2359 if (BGP_DEBUG(zebra, ZEBRA))
2360 zlog_debug("%s: Received RULE REMOVED", __func__);
2361 break;
2362 }
2363
2364 return 0;
2365 }
2366
2367 static int ipset_notify_owner(ZAPI_CALLBACK_ARGS)
2368 {
2369 uint32_t unique;
2370 enum zapi_ipset_notify_owner note;
2371 struct bgp_pbr_match *bgp_pbim;
2372
2373 if (!zapi_ipset_notify_decode(zclient->ibuf,
2374 &unique,
2375 &note))
2376 return -1;
2377
2378 bgp_pbim = bgp_pbr_match_ipset_lookup(vrf_id, unique);
2379 if (!bgp_pbim) {
2380 if (BGP_DEBUG(zebra, ZEBRA))
2381 zlog_debug("%s: Fail to look BGP match ( %u, ID %u)",
2382 __func__, note, unique);
2383 return 0;
2384 }
2385
2386 switch (note) {
2387 case ZAPI_IPSET_FAIL_INSTALL:
2388 if (BGP_DEBUG(zebra, ZEBRA))
2389 zlog_debug("%s: Received IPSET_FAIL_INSTALL", __func__);
2390 bgp_pbim->installed = false;
2391 bgp_pbim->install_in_progress = false;
2392 break;
2393 case ZAPI_IPSET_INSTALLED:
2394 bgp_pbim->installed = true;
2395 bgp_pbim->install_in_progress = false;
2396 if (BGP_DEBUG(zebra, ZEBRA))
2397 zlog_debug("%s: Received IPSET_INSTALLED", __func__);
2398 break;
2399 case ZAPI_IPSET_FAIL_REMOVE:
2400 case ZAPI_IPSET_REMOVED:
2401 if (BGP_DEBUG(zebra, ZEBRA))
2402 zlog_debug("%s: Received IPSET REMOVED", __func__);
2403 break;
2404 }
2405
2406 return 0;
2407 }
2408
2409 static int ipset_entry_notify_owner(ZAPI_CALLBACK_ARGS)
2410 {
2411 uint32_t unique;
2412 char ipset_name[ZEBRA_IPSET_NAME_SIZE];
2413 enum zapi_ipset_entry_notify_owner note;
2414 struct bgp_pbr_match_entry *bgp_pbime;
2415
2416 if (!zapi_ipset_entry_notify_decode(
2417 zclient->ibuf,
2418 &unique,
2419 ipset_name,
2420 &note))
2421 return -1;
2422 bgp_pbime = bgp_pbr_match_ipset_entry_lookup(vrf_id,
2423 ipset_name,
2424 unique);
2425 if (!bgp_pbime) {
2426 if (BGP_DEBUG(zebra, ZEBRA))
2427 zlog_debug(
2428 "%s: Fail to look BGP match entry (%u, ID %u)",
2429 __func__, note, unique);
2430 return 0;
2431 }
2432
2433 switch (note) {
2434 case ZAPI_IPSET_ENTRY_FAIL_INSTALL:
2435 if (BGP_DEBUG(zebra, ZEBRA))
2436 zlog_debug("%s: Received IPSET_ENTRY_FAIL_INSTALL",
2437 __func__);
2438 bgp_pbime->installed = false;
2439 bgp_pbime->install_in_progress = false;
2440 break;
2441 case ZAPI_IPSET_ENTRY_INSTALLED:
2442 {
2443 struct bgp_path_info *path;
2444 struct bgp_path_info_extra *extra;
2445
2446 bgp_pbime->installed = true;
2447 bgp_pbime->install_in_progress = false;
2448 if (BGP_DEBUG(zebra, ZEBRA))
2449 zlog_debug("%s: Received IPSET_ENTRY_INSTALLED",
2450 __func__);
2451 /* link bgp_path_info to bpme */
2452 path = (struct bgp_path_info *)bgp_pbime->path;
2453 extra = bgp_path_info_extra_get(path);
2454 listnode_add_force(&extra->bgp_fs_pbr, bgp_pbime);
2455 }
2456 break;
2457 case ZAPI_IPSET_ENTRY_FAIL_REMOVE:
2458 case ZAPI_IPSET_ENTRY_REMOVED:
2459 if (BGP_DEBUG(zebra, ZEBRA))
2460 zlog_debug("%s: Received IPSET_ENTRY_REMOVED",
2461 __func__);
2462 break;
2463 }
2464 return 0;
2465 }
2466
2467 static int iptable_notify_owner(ZAPI_CALLBACK_ARGS)
2468 {
2469 uint32_t unique;
2470 enum zapi_iptable_notify_owner note;
2471 struct bgp_pbr_match *bgpm;
2472
2473 if (!zapi_iptable_notify_decode(
2474 zclient->ibuf,
2475 &unique,
2476 &note))
2477 return -1;
2478 bgpm = bgp_pbr_match_iptable_lookup(vrf_id, unique);
2479 if (!bgpm) {
2480 if (BGP_DEBUG(zebra, ZEBRA))
2481 zlog_debug("%s: Fail to look BGP iptable (%u %u)",
2482 __func__, note, unique);
2483 return 0;
2484 }
2485 switch (note) {
2486 case ZAPI_IPTABLE_FAIL_INSTALL:
2487 if (BGP_DEBUG(zebra, ZEBRA))
2488 zlog_debug("%s: Received IPTABLE_FAIL_INSTALL",
2489 __func__);
2490 bgpm->installed_in_iptable = false;
2491 bgpm->install_iptable_in_progress = false;
2492 break;
2493 case ZAPI_IPTABLE_INSTALLED:
2494 bgpm->installed_in_iptable = true;
2495 bgpm->install_iptable_in_progress = false;
2496 if (BGP_DEBUG(zebra, ZEBRA))
2497 zlog_debug("%s: Received IPTABLE_INSTALLED", __func__);
2498 bgpm->action->refcnt++;
2499 break;
2500 case ZAPI_IPTABLE_FAIL_REMOVE:
2501 case ZAPI_IPTABLE_REMOVED:
2502 if (BGP_DEBUG(zebra, ZEBRA))
2503 zlog_debug("%s: Received IPTABLE REMOVED", __func__);
2504 break;
2505 }
2506 return 0;
2507 }
2508
2509 /* Process route notification messages from RIB */
2510 static int bgp_zebra_route_notify_owner(int command, struct zclient *zclient,
2511 zebra_size_t length, vrf_id_t vrf_id)
2512 {
2513 struct prefix p;
2514 enum zapi_route_notify_owner note;
2515 uint32_t table_id;
2516 afi_t afi;
2517 safi_t safi;
2518 struct bgp_dest *dest;
2519 struct bgp *bgp;
2520 struct bgp_path_info *pi, *new_select;
2521
2522 if (!zapi_route_notify_decode(zclient->ibuf, &p, &table_id, &note,
2523 &afi, &safi)) {
2524 zlog_err("%s : error in msg decode", __func__);
2525 return -1;
2526 }
2527
2528 /* Get the bgp instance */
2529 bgp = bgp_lookup_by_vrf_id(vrf_id);
2530 if (!bgp) {
2531 flog_err(EC_BGP_INVALID_BGP_INSTANCE,
2532 "%s : bgp instance not found vrf %d", __func__,
2533 vrf_id);
2534 return -1;
2535 }
2536
2537 /* Find the bgp route node */
2538 dest = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi, &p,
2539 &bgp->vrf_prd);
2540 if (!dest)
2541 return -1;
2542
2543 switch (note) {
2544 case ZAPI_ROUTE_INSTALLED:
2545 new_select = NULL;
2546 /* Clear the flags so that route can be processed */
2547 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
2548 SET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2549 if (BGP_DEBUG(zebra, ZEBRA))
2550 zlog_debug("route %pRN : INSTALLED", dest);
2551 /* Find the best route */
2552 for (pi = dest->info; pi; pi = pi->next) {
2553 /* Process aggregate route */
2554 bgp_aggregate_increment(bgp, &p, pi, afi, safi);
2555 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2556 new_select = pi;
2557 }
2558 /* Advertise the route */
2559 if (new_select)
2560 group_announce_route(bgp, afi, safi, dest, new_select);
2561 else {
2562 flog_err(EC_BGP_INVALID_ROUTE,
2563 "selected route %pRN not found", dest);
2564
2565 bgp_dest_unlock_node(dest);
2566 return -1;
2567 }
2568 break;
2569 case ZAPI_ROUTE_REMOVED:
2570 /* Route deleted from dataplane, reset the installed flag
2571 * so that route can be reinstalled when client sends
2572 * route add later
2573 */
2574 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2575 if (BGP_DEBUG(zebra, ZEBRA))
2576 zlog_debug("route %pRN: Removed from Fib", dest);
2577 break;
2578 case ZAPI_ROUTE_FAIL_INSTALL:
2579 new_select = NULL;
2580 if (BGP_DEBUG(zebra, ZEBRA))
2581 zlog_debug("route: %pRN Failed to Install into Fib",
2582 dest);
2583 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
2584 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2585 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
2586 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2587 new_select = pi;
2588 }
2589 if (new_select)
2590 group_announce_route(bgp, afi, safi, dest, new_select);
2591 /* Error will be logged by zebra module */
2592 break;
2593 case ZAPI_ROUTE_BETTER_ADMIN_WON:
2594 if (BGP_DEBUG(zebra, ZEBRA))
2595 zlog_debug("route: %pRN removed due to better admin won",
2596 dest);
2597 new_select = NULL;
2598 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
2599 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2600 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
2601 bgp_aggregate_decrement(bgp, &p, pi, afi, safi);
2602 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2603 new_select = pi;
2604 }
2605 if (new_select)
2606 group_announce_route(bgp, afi, safi, dest, new_select);
2607 /* No action required */
2608 break;
2609 case ZAPI_ROUTE_REMOVE_FAIL:
2610 zlog_warn("%s: Route %pRN failure to remove",
2611 __func__, dest);
2612 break;
2613 }
2614
2615 bgp_dest_unlock_node(dest);
2616 return 0;
2617 }
2618
2619 /* this function is used to forge ip rule,
2620 * - either for iptable/ipset using fwmark id
2621 * - or for sample ip rule cmd
2622 */
2623 static void bgp_encode_pbr_rule_action(struct stream *s,
2624 struct bgp_pbr_action *pbra,
2625 struct bgp_pbr_rule *pbr)
2626 {
2627 struct prefix pfx;
2628 uint8_t fam = AF_INET;
2629 char ifname[INTERFACE_NAMSIZ];
2630
2631 if (pbra->nh.type == NEXTHOP_TYPE_IPV6)
2632 fam = AF_INET6;
2633 stream_putl(s, 0); /* seqno unused */
2634 if (pbr)
2635 stream_putl(s, pbr->priority);
2636 else
2637 stream_putl(s, 0);
2638 /* ruleno unused - priority change
2639 * ruleno permits distinguishing various FS PBR entries
2640 * - FS PBR entries based on ipset/iptables
2641 * - FS PBR entries based on iprule
2642 * the latter may contain default routing information injected by FS
2643 */
2644 if (pbr)
2645 stream_putl(s, pbr->unique);
2646 else
2647 stream_putl(s, pbra->unique);
2648 stream_putc(s, 0); /* ip protocol being used */
2649 if (pbr && pbr->flags & MATCH_IP_SRC_SET)
2650 memcpy(&pfx, &(pbr->src), sizeof(struct prefix));
2651 else {
2652 memset(&pfx, 0, sizeof(pfx));
2653 pfx.family = fam;
2654 }
2655 stream_putc(s, pfx.family);
2656 stream_putc(s, pfx.prefixlen);
2657 stream_put(s, &pfx.u.prefix, prefix_blen(&pfx));
2658
2659 stream_putw(s, 0); /* src port */
2660
2661 if (pbr && pbr->flags & MATCH_IP_DST_SET)
2662 memcpy(&pfx, &(pbr->dst), sizeof(struct prefix));
2663 else {
2664 memset(&pfx, 0, sizeof(pfx));
2665 pfx.family = fam;
2666 }
2667 stream_putc(s, pfx.family);
2668 stream_putc(s, pfx.prefixlen);
2669 stream_put(s, &pfx.u.prefix, prefix_blen(&pfx));
2670
2671 stream_putw(s, 0); /* dst port */
2672 stream_putc(s, 0); /* dsfield */
2673 /* if pbr present, fwmark is not used */
2674 if (pbr)
2675 stream_putl(s, 0);
2676 else
2677 stream_putl(s, pbra->fwmark); /* fwmark */
2678
2679 stream_putl(s, pbra->table_id);
2680
2681 memset(ifname, 0, sizeof(ifname));
2682 stream_put(s, ifname, INTERFACE_NAMSIZ); /* ifname unused */
2683 }
2684
2685 static void bgp_encode_pbr_ipset_match(struct stream *s,
2686 struct bgp_pbr_match *pbim)
2687 {
2688 stream_putl(s, pbim->unique);
2689 stream_putl(s, pbim->type);
2690 stream_putc(s, pbim->family);
2691 stream_put(s, pbim->ipset_name,
2692 ZEBRA_IPSET_NAME_SIZE);
2693 }
2694
2695 static void bgp_encode_pbr_ipset_entry_match(struct stream *s,
2696 struct bgp_pbr_match_entry *pbime)
2697 {
2698 stream_putl(s, pbime->unique);
2699 /* check that back pointer is not null */
2700 stream_put(s, pbime->backpointer->ipset_name,
2701 ZEBRA_IPSET_NAME_SIZE);
2702
2703 stream_putc(s, pbime->src.family);
2704 stream_putc(s, pbime->src.prefixlen);
2705 stream_put(s, &pbime->src.u.prefix, prefix_blen(&pbime->src));
2706
2707 stream_putc(s, pbime->dst.family);
2708 stream_putc(s, pbime->dst.prefixlen);
2709 stream_put(s, &pbime->dst.u.prefix, prefix_blen(&pbime->dst));
2710
2711 stream_putw(s, pbime->src_port_min);
2712 stream_putw(s, pbime->src_port_max);
2713 stream_putw(s, pbime->dst_port_min);
2714 stream_putw(s, pbime->dst_port_max);
2715 stream_putc(s, pbime->proto);
2716 }
2717
2718 static void bgp_encode_pbr_iptable_match(struct stream *s,
2719 struct bgp_pbr_action *bpa,
2720 struct bgp_pbr_match *pbm)
2721 {
2722 stream_putl(s, pbm->unique2);
2723
2724 stream_putl(s, pbm->type);
2725
2726 stream_putl(s, pbm->flags);
2727
2728 /* TODO: correlate with what is contained
2729 * into bgp_pbr_action.
2730 * currently only forward supported
2731 */
2732 if (bpa->nh.type == NEXTHOP_TYPE_BLACKHOLE)
2733 stream_putl(s, ZEBRA_IPTABLES_DROP);
2734 else
2735 stream_putl(s, ZEBRA_IPTABLES_FORWARD);
2736 stream_putl(s, bpa->fwmark);
2737 stream_put(s, pbm->ipset_name,
2738 ZEBRA_IPSET_NAME_SIZE);
2739 stream_putc(s, pbm->family);
2740 stream_putw(s, pbm->pkt_len_min);
2741 stream_putw(s, pbm->pkt_len_max);
2742 stream_putw(s, pbm->tcp_flags);
2743 stream_putw(s, pbm->tcp_mask_flags);
2744 stream_putc(s, pbm->dscp_value);
2745 stream_putc(s, pbm->fragment);
2746 stream_putc(s, pbm->protocol);
2747 stream_putw(s, pbm->flow_label);
2748 }
2749
2750 /* BGP has established connection with Zebra. */
2751 static void bgp_zebra_connected(struct zclient *zclient)
2752 {
2753 struct bgp *bgp;
2754
2755 zclient_num_connects++; /* increment even if not responding */
2756
2757 /* Send the client registration */
2758 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT);
2759
2760 /* At this point, we may or may not have BGP instances configured, but
2761 * we're only interested in the default VRF (others wouldn't have learnt
2762 * the VRF from Zebra yet.)
2763 */
2764 bgp = bgp_get_default();
2765 if (!bgp)
2766 return;
2767
2768 bgp_zebra_instance_register(bgp);
2769
2770 /* tell label pool that zebra is connected */
2771 bgp_lp_event_zebra_up();
2772
2773 /* TODO - What if we have peers and networks configured, do we have to
2774 * kick-start them?
2775 */
2776 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp, bgp->peer);
2777 }
2778
2779 static int bgp_zebra_process_local_es_add(ZAPI_CALLBACK_ARGS)
2780 {
2781 esi_t esi;
2782 struct bgp *bgp = NULL;
2783 struct stream *s = NULL;
2784 char buf[ESI_STR_LEN];
2785 struct in_addr originator_ip;
2786 uint8_t active;
2787 uint8_t bypass;
2788 uint16_t df_pref;
2789
2790 bgp = bgp_lookup_by_vrf_id(vrf_id);
2791 if (!bgp)
2792 return 0;
2793
2794 s = zclient->ibuf;
2795 stream_get(&esi, s, sizeof(esi_t));
2796 originator_ip.s_addr = stream_get_ipv4(s);
2797 active = stream_getc(s);
2798 df_pref = stream_getw(s);
2799 bypass = stream_getc(s);
2800
2801 if (BGP_DEBUG(zebra, ZEBRA))
2802 zlog_debug(
2803 "Rx add ESI %s originator-ip %pI4 active %u df_pref %u %s",
2804 esi_to_str(&esi, buf, sizeof(buf)), &originator_ip,
2805 active, df_pref, bypass ? "bypass" : "");
2806
2807 frrtrace(5, frr_bgp, evpn_mh_local_es_add_zrecv, &esi, originator_ip,
2808 active, bypass, df_pref);
2809
2810 bgp_evpn_local_es_add(bgp, &esi, originator_ip, active, df_pref,
2811 !!bypass);
2812
2813 return 0;
2814 }
2815
2816 static int bgp_zebra_process_local_es_del(ZAPI_CALLBACK_ARGS)
2817 {
2818 esi_t esi;
2819 struct bgp *bgp = NULL;
2820 struct stream *s = NULL;
2821 char buf[ESI_STR_LEN];
2822
2823 memset(&esi, 0, sizeof(esi_t));
2824 bgp = bgp_lookup_by_vrf_id(vrf_id);
2825 if (!bgp)
2826 return 0;
2827
2828 s = zclient->ibuf;
2829 stream_get(&esi, s, sizeof(esi_t));
2830
2831 if (BGP_DEBUG(zebra, ZEBRA))
2832 zlog_debug("Rx del ESI %s",
2833 esi_to_str(&esi, buf, sizeof(buf)));
2834
2835 frrtrace(1, frr_bgp, evpn_mh_local_es_del_zrecv, &esi);
2836
2837 bgp_evpn_local_es_del(bgp, &esi);
2838
2839 return 0;
2840 }
2841
2842 static int bgp_zebra_process_local_es_evi(ZAPI_CALLBACK_ARGS)
2843 {
2844 esi_t esi;
2845 vni_t vni;
2846 struct bgp *bgp;
2847 struct stream *s;
2848 char buf[ESI_STR_LEN];
2849
2850 bgp = bgp_lookup_by_vrf_id(vrf_id);
2851 if (!bgp)
2852 return 0;
2853
2854 s = zclient->ibuf;
2855 stream_get(&esi, s, sizeof(esi_t));
2856 vni = stream_getl(s);
2857
2858 if (BGP_DEBUG(zebra, ZEBRA))
2859 zlog_debug("Rx %s ESI %s VNI %u",
2860 (cmd == ZEBRA_VNI_ADD) ? "add" : "del",
2861 esi_to_str(&esi, buf, sizeof(buf)), vni);
2862
2863 if (cmd == ZEBRA_LOCAL_ES_EVI_ADD) {
2864 frrtrace(2, frr_bgp, evpn_mh_local_es_evi_add_zrecv, &esi, vni);
2865
2866 bgp_evpn_local_es_evi_add(bgp, &esi, vni);
2867 } else {
2868 frrtrace(2, frr_bgp, evpn_mh_local_es_evi_del_zrecv, &esi, vni);
2869
2870 bgp_evpn_local_es_evi_del(bgp, &esi, vni);
2871 }
2872
2873 return 0;
2874 }
2875
2876 static int bgp_zebra_process_local_l3vni(ZAPI_CALLBACK_ARGS)
2877 {
2878 int filter = 0;
2879 vni_t l3vni = 0;
2880 struct ethaddr svi_rmac, vrr_rmac = {.octet = {0} };
2881 struct in_addr originator_ip;
2882 struct stream *s;
2883 ifindex_t svi_ifindex;
2884 bool is_anycast_mac = false;
2885
2886 memset(&svi_rmac, 0, sizeof(struct ethaddr));
2887 memset(&originator_ip, 0, sizeof(struct in_addr));
2888 s = zclient->ibuf;
2889 l3vni = stream_getl(s);
2890 if (cmd == ZEBRA_L3VNI_ADD) {
2891 stream_get(&svi_rmac, s, sizeof(struct ethaddr));
2892 originator_ip.s_addr = stream_get_ipv4(s);
2893 stream_get(&filter, s, sizeof(int));
2894 svi_ifindex = stream_getl(s);
2895 stream_get(&vrr_rmac, s, sizeof(struct ethaddr));
2896 is_anycast_mac = stream_getl(s);
2897
2898 if (BGP_DEBUG(zebra, ZEBRA))
2899 zlog_debug(
2900 "Rx L3-VNI ADD VRF %s VNI %u RMAC svi-mac %pEA vrr-mac %pEA filter %s svi-if %u",
2901 vrf_id_to_name(vrf_id), l3vni, &svi_rmac,
2902 &vrr_rmac,
2903 filter ? "prefix-routes-only" : "none",
2904 svi_ifindex);
2905
2906 frrtrace(8, frr_bgp, evpn_local_l3vni_add_zrecv, l3vni, vrf_id,
2907 &svi_rmac, &vrr_rmac, filter, originator_ip,
2908 svi_ifindex, is_anycast_mac);
2909
2910 bgp_evpn_local_l3vni_add(l3vni, vrf_id, &svi_rmac, &vrr_rmac,
2911 originator_ip, filter, svi_ifindex,
2912 is_anycast_mac);
2913 } else {
2914 if (BGP_DEBUG(zebra, ZEBRA))
2915 zlog_debug("Rx L3-VNI DEL VRF %s VNI %u",
2916 vrf_id_to_name(vrf_id), l3vni);
2917
2918 frrtrace(2, frr_bgp, evpn_local_l3vni_del_zrecv, l3vni, vrf_id);
2919
2920 bgp_evpn_local_l3vni_del(l3vni, vrf_id);
2921 }
2922
2923 return 0;
2924 }
2925
2926 static int bgp_zebra_process_local_vni(ZAPI_CALLBACK_ARGS)
2927 {
2928 struct stream *s;
2929 vni_t vni;
2930 struct bgp *bgp;
2931 struct in_addr vtep_ip = {INADDR_ANY};
2932 vrf_id_t tenant_vrf_id = VRF_DEFAULT;
2933 struct in_addr mcast_grp = {INADDR_ANY};
2934 ifindex_t svi_ifindex = 0;
2935
2936 s = zclient->ibuf;
2937 vni = stream_getl(s);
2938 if (cmd == ZEBRA_VNI_ADD) {
2939 vtep_ip.s_addr = stream_get_ipv4(s);
2940 stream_get(&tenant_vrf_id, s, sizeof(vrf_id_t));
2941 mcast_grp.s_addr = stream_get_ipv4(s);
2942 stream_get(&svi_ifindex, s, sizeof(ifindex_t));
2943 }
2944
2945 bgp = bgp_lookup_by_vrf_id(vrf_id);
2946 if (!bgp)
2947 return 0;
2948
2949 if (BGP_DEBUG(zebra, ZEBRA))
2950 zlog_debug(
2951 "Rx VNI %s VRF %s VNI %u tenant-vrf %s SVI ifindex %u",
2952 (cmd == ZEBRA_VNI_ADD) ? "add" : "del",
2953 vrf_id_to_name(vrf_id), vni,
2954 vrf_id_to_name(tenant_vrf_id), svi_ifindex);
2955
2956 if (cmd == ZEBRA_VNI_ADD) {
2957 frrtrace(4, frr_bgp, evpn_local_vni_add_zrecv, vni, vtep_ip,
2958 tenant_vrf_id, mcast_grp);
2959
2960 return bgp_evpn_local_vni_add(
2961 bgp, vni,
2962 vtep_ip.s_addr != INADDR_ANY ? vtep_ip : bgp->router_id,
2963 tenant_vrf_id, mcast_grp, svi_ifindex);
2964 } else {
2965 frrtrace(1, frr_bgp, evpn_local_vni_del_zrecv, vni);
2966
2967 return bgp_evpn_local_vni_del(bgp, vni);
2968 }
2969 }
2970
2971 static int bgp_zebra_process_local_macip(ZAPI_CALLBACK_ARGS)
2972 {
2973 struct stream *s;
2974 vni_t vni;
2975 struct bgp *bgp;
2976 struct ethaddr mac;
2977 struct ipaddr ip;
2978 int ipa_len;
2979 uint8_t flags = 0;
2980 uint32_t seqnum = 0;
2981 int state = 0;
2982 char buf2[ESI_STR_LEN];
2983 esi_t esi;
2984
2985 memset(&ip, 0, sizeof(ip));
2986 s = zclient->ibuf;
2987 vni = stream_getl(s);
2988 stream_get(&mac.octet, s, ETH_ALEN);
2989 ipa_len = stream_getl(s);
2990 if (ipa_len != 0 && ipa_len != IPV4_MAX_BYTELEN
2991 && ipa_len != IPV6_MAX_BYTELEN) {
2992 flog_err(EC_BGP_MACIP_LEN,
2993 "%u:Recv MACIP %s with invalid IP addr length %d",
2994 vrf_id, (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del",
2995 ipa_len);
2996 return -1;
2997 }
2998
2999 if (ipa_len) {
3000 ip.ipa_type =
3001 (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4 : IPADDR_V6;
3002 stream_get(&ip.ip.addr, s, ipa_len);
3003 }
3004 if (cmd == ZEBRA_MACIP_ADD) {
3005 flags = stream_getc(s);
3006 seqnum = stream_getl(s);
3007 stream_get(&esi, s, sizeof(esi_t));
3008 } else {
3009 state = stream_getl(s);
3010 memset(&esi, 0, sizeof(esi_t));
3011 }
3012
3013 bgp = bgp_lookup_by_vrf_id(vrf_id);
3014 if (!bgp)
3015 return 0;
3016
3017 if (BGP_DEBUG(zebra, ZEBRA))
3018 zlog_debug(
3019 "%u:Recv MACIP %s f 0x%x MAC %pEA IP %pIA VNI %u seq %u state %d ESI %s",
3020 vrf_id, (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del", flags,
3021 &mac, &ip, vni, seqnum, state,
3022 esi_to_str(&esi, buf2, sizeof(buf2)));
3023
3024 if (cmd == ZEBRA_MACIP_ADD) {
3025 frrtrace(6, frr_bgp, evpn_local_macip_add_zrecv, vni, &mac, &ip,
3026 flags, seqnum, &esi);
3027
3028 return bgp_evpn_local_macip_add(bgp, vni, &mac, &ip,
3029 flags, seqnum, &esi);
3030 } else {
3031 frrtrace(4, frr_bgp, evpn_local_macip_del_zrecv, vni, &mac, &ip,
3032 state);
3033
3034 return bgp_evpn_local_macip_del(bgp, vni, &mac, &ip, state);
3035 }
3036 }
3037
3038 static int bgp_zebra_process_local_ip_prefix(ZAPI_CALLBACK_ARGS)
3039 {
3040 struct stream *s = NULL;
3041 struct bgp *bgp_vrf = NULL;
3042 struct prefix p;
3043
3044 memset(&p, 0, sizeof(struct prefix));
3045 s = zclient->ibuf;
3046 stream_get(&p, s, sizeof(struct prefix));
3047
3048 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
3049 if (!bgp_vrf)
3050 return 0;
3051
3052 if (BGP_DEBUG(zebra, ZEBRA))
3053 zlog_debug("Recv prefix %pFX %s on vrf %s", &p,
3054 (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) ? "ADD" : "DEL",
3055 vrf_id_to_name(vrf_id));
3056
3057 if (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) {
3058
3059 if (p.family == AF_INET)
3060 bgp_evpn_advertise_type5_route(bgp_vrf, &p, NULL,
3061 AFI_IP, SAFI_UNICAST);
3062 else
3063 bgp_evpn_advertise_type5_route(bgp_vrf, &p, NULL,
3064 AFI_IP6, SAFI_UNICAST);
3065
3066 } else {
3067 if (p.family == AF_INET)
3068 bgp_evpn_withdraw_type5_route(bgp_vrf, &p, AFI_IP,
3069 SAFI_UNICAST);
3070 else
3071 bgp_evpn_withdraw_type5_route(bgp_vrf, &p, AFI_IP6,
3072 SAFI_UNICAST);
3073 }
3074 return 0;
3075 }
3076
3077 static int bgp_zebra_process_label_chunk(ZAPI_CALLBACK_ARGS)
3078 {
3079 struct stream *s = NULL;
3080 uint8_t response_keep;
3081 uint32_t first;
3082 uint32_t last;
3083 uint8_t proto;
3084 unsigned short instance;
3085
3086 s = zclient->ibuf;
3087 STREAM_GETC(s, proto);
3088 STREAM_GETW(s, instance);
3089 STREAM_GETC(s, response_keep);
3090 STREAM_GETL(s, first);
3091 STREAM_GETL(s, last);
3092
3093 if (zclient->redist_default != proto) {
3094 flog_err(EC_BGP_LM_ERROR, "Got LM msg with wrong proto %u",
3095 proto);
3096 return 0;
3097 }
3098 if (zclient->instance != instance) {
3099 flog_err(EC_BGP_LM_ERROR, "Got LM msg with wrong instance %u",
3100 proto);
3101 return 0;
3102 }
3103
3104 if (first > last ||
3105 first < MPLS_LABEL_UNRESERVED_MIN ||
3106 last > MPLS_LABEL_UNRESERVED_MAX) {
3107
3108 flog_err(EC_BGP_LM_ERROR, "%s: Invalid Label chunk: %u - %u",
3109 __func__, first, last);
3110 return 0;
3111 }
3112 if (BGP_DEBUG(zebra, ZEBRA)) {
3113 zlog_debug("Label Chunk assign: %u - %u (%u) ",
3114 first, last, response_keep);
3115 }
3116
3117 bgp_lp_event_chunk(response_keep, first, last);
3118
3119 return 0;
3120
3121 stream_failure: /* for STREAM_GETX */
3122 return -1;
3123 }
3124
3125 extern struct zebra_privs_t bgpd_privs;
3126
3127 static int bgp_ifp_create(struct interface *ifp)
3128 {
3129 struct bgp *bgp;
3130
3131 if (BGP_DEBUG(zebra, ZEBRA))
3132 zlog_debug("Rx Intf add VRF %u IF %s", ifp->vrf->vrf_id,
3133 ifp->name);
3134
3135 bgp = ifp->vrf->info;
3136 if (!bgp)
3137 return 0;
3138
3139 bgp_mac_add_mac_entry(ifp);
3140
3141 bgp_update_interface_nbrs(bgp, ifp, ifp);
3142 hook_call(bgp_vrf_status_changed, bgp, ifp);
3143 return 0;
3144 }
3145
3146 static int bgp_zebra_process_srv6_locator_chunk(ZAPI_CALLBACK_ARGS)
3147 {
3148 struct stream *s = NULL;
3149 struct bgp *bgp = bgp_get_default();
3150 struct listnode *node;
3151 struct prefix_ipv6 *c;
3152 struct srv6_locator_chunk s6c = {};
3153 struct prefix_ipv6 *chunk = NULL;
3154
3155 s = zclient->ibuf;
3156 zapi_srv6_locator_chunk_decode(s, &s6c);
3157
3158 if (strcmp(bgp->srv6_locator_name, s6c.locator_name) != 0) {
3159 zlog_err("%s: Locator name unmatch %s:%s", __func__,
3160 bgp->srv6_locator_name, s6c.locator_name);
3161 return 0;
3162 }
3163
3164 for (ALL_LIST_ELEMENTS_RO(bgp->srv6_locator_chunks, node, c)) {
3165 if (!prefix_cmp(c, &s6c.prefix))
3166 return 0;
3167 }
3168
3169 chunk = prefix_ipv6_new();
3170 *chunk = s6c.prefix;
3171 listnode_add(bgp->srv6_locator_chunks, chunk);
3172 vpn_leak_postchange_all();
3173 return 0;
3174 }
3175
3176 static int bgp_zebra_process_srv6_locator_add(ZAPI_CALLBACK_ARGS)
3177 {
3178 struct srv6_locator loc = {};
3179 struct bgp *bgp = bgp_get_default();
3180 const char *loc_name = bgp->srv6_locator_name;
3181
3182 if (zapi_srv6_locator_decode(zclient->ibuf, &loc) < 0)
3183 return -1;
3184
3185 if (!bgp || !bgp->srv6_enabled)
3186 return 0;
3187
3188 if (bgp_zebra_srv6_manager_get_locator_chunk(loc_name) < 0)
3189 return -1;
3190
3191 return 0;
3192 }
3193
3194 static int bgp_zebra_process_srv6_locator_delete(ZAPI_CALLBACK_ARGS)
3195 {
3196 struct srv6_locator loc = {};
3197 struct bgp *bgp = bgp_get_default();
3198 struct listnode *node, *nnode;
3199 struct prefix_ipv6 *chunk;
3200 struct bgp_srv6_function *func;
3201 struct bgp *bgp_vrf;
3202 struct in6_addr *tovpn_sid;
3203 struct prefix_ipv6 tmp_prefi;
3204
3205 if (zapi_srv6_locator_decode(zclient->ibuf, &loc) < 0)
3206 return -1;
3207
3208 // refresh chunks
3209 for (ALL_LIST_ELEMENTS(bgp->srv6_locator_chunks, node, nnode, chunk))
3210 if (prefix_match((struct prefix *)&loc.prefix,
3211 (struct prefix *)chunk))
3212 listnode_delete(bgp->srv6_locator_chunks, chunk);
3213
3214 // refresh functions
3215 for (ALL_LIST_ELEMENTS(bgp->srv6_functions, node, nnode, func)) {
3216 tmp_prefi.family = AF_INET6;
3217 tmp_prefi.prefixlen = 128;
3218 tmp_prefi.prefix = func->sid;
3219 if (prefix_match((struct prefix *)&loc.prefix,
3220 (struct prefix *)&tmp_prefi))
3221 listnode_delete(bgp->srv6_functions, func);
3222 }
3223
3224 // refresh tovpn_sid
3225 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
3226 if (bgp_vrf->inst_type != BGP_INSTANCE_TYPE_VRF)
3227 continue;
3228
3229 // refresh vpnv4 tovpn_sid
3230 tovpn_sid = bgp_vrf->vpn_policy[AFI_IP].tovpn_sid;
3231 if (tovpn_sid) {
3232 tmp_prefi.family = AF_INET6;
3233 tmp_prefi.prefixlen = 128;
3234 tmp_prefi.prefix = *tovpn_sid;
3235 if (prefix_match((struct prefix *)&loc.prefix,
3236 (struct prefix *)&tmp_prefi))
3237 XFREE(MTYPE_BGP_SRV6_SID,
3238 bgp_vrf->vpn_policy[AFI_IP].tovpn_sid);
3239 }
3240
3241 // refresh vpnv6 tovpn_sid
3242 tovpn_sid = bgp_vrf->vpn_policy[AFI_IP6].tovpn_sid;
3243 if (tovpn_sid) {
3244 tmp_prefi.family = AF_INET6;
3245 tmp_prefi.prefixlen = 128;
3246 tmp_prefi.prefix = *tovpn_sid;
3247 if (prefix_match((struct prefix *)&loc.prefix,
3248 (struct prefix *)&tmp_prefi))
3249 XFREE(MTYPE_BGP_SRV6_SID,
3250 bgp_vrf->vpn_policy[AFI_IP6].tovpn_sid);
3251 }
3252 }
3253
3254 vpn_leak_postchange_all();
3255 return 0;
3256 }
3257
3258 static zclient_handler *const bgp_handlers[] = {
3259 [ZEBRA_ROUTER_ID_UPDATE] = bgp_router_id_update,
3260 [ZEBRA_INTERFACE_ADDRESS_ADD] = bgp_interface_address_add,
3261 [ZEBRA_INTERFACE_ADDRESS_DELETE] = bgp_interface_address_delete,
3262 [ZEBRA_INTERFACE_NBR_ADDRESS_ADD] = bgp_interface_nbr_address_add,
3263 [ZEBRA_INTERFACE_NBR_ADDRESS_DELETE] = bgp_interface_nbr_address_delete,
3264 [ZEBRA_INTERFACE_VRF_UPDATE] = bgp_interface_vrf_update,
3265 [ZEBRA_REDISTRIBUTE_ROUTE_ADD] = zebra_read_route,
3266 [ZEBRA_REDISTRIBUTE_ROUTE_DEL] = zebra_read_route,
3267 [ZEBRA_NEXTHOP_UPDATE] = bgp_read_nexthop_update,
3268 [ZEBRA_FEC_UPDATE] = bgp_read_fec_update,
3269 [ZEBRA_LOCAL_ES_ADD] = bgp_zebra_process_local_es_add,
3270 [ZEBRA_LOCAL_ES_DEL] = bgp_zebra_process_local_es_del,
3271 [ZEBRA_VNI_ADD] = bgp_zebra_process_local_vni,
3272 [ZEBRA_LOCAL_ES_EVI_ADD] = bgp_zebra_process_local_es_evi,
3273 [ZEBRA_LOCAL_ES_EVI_DEL] = bgp_zebra_process_local_es_evi,
3274 [ZEBRA_VNI_DEL] = bgp_zebra_process_local_vni,
3275 [ZEBRA_MACIP_ADD] = bgp_zebra_process_local_macip,
3276 [ZEBRA_MACIP_DEL] = bgp_zebra_process_local_macip,
3277 [ZEBRA_L3VNI_ADD] = bgp_zebra_process_local_l3vni,
3278 [ZEBRA_L3VNI_DEL] = bgp_zebra_process_local_l3vni,
3279 [ZEBRA_IP_PREFIX_ROUTE_ADD] = bgp_zebra_process_local_ip_prefix,
3280 [ZEBRA_IP_PREFIX_ROUTE_DEL] = bgp_zebra_process_local_ip_prefix,
3281 [ZEBRA_GET_LABEL_CHUNK] = bgp_zebra_process_label_chunk,
3282 [ZEBRA_RULE_NOTIFY_OWNER] = rule_notify_owner,
3283 [ZEBRA_IPSET_NOTIFY_OWNER] = ipset_notify_owner,
3284 [ZEBRA_IPSET_ENTRY_NOTIFY_OWNER] = ipset_entry_notify_owner,
3285 [ZEBRA_IPTABLE_NOTIFY_OWNER] = iptable_notify_owner,
3286 [ZEBRA_ROUTE_NOTIFY_OWNER] = bgp_zebra_route_notify_owner,
3287 [ZEBRA_SRV6_LOCATOR_ADD] = bgp_zebra_process_srv6_locator_add,
3288 [ZEBRA_SRV6_LOCATOR_DELETE] = bgp_zebra_process_srv6_locator_delete,
3289 [ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK] =
3290 bgp_zebra_process_srv6_locator_chunk,
3291 };
3292
3293 void bgp_zebra_init(struct thread_master *master, unsigned short instance)
3294 {
3295 zclient_num_connects = 0;
3296
3297 if_zapi_callbacks(bgp_ifp_create, bgp_ifp_up,
3298 bgp_ifp_down, bgp_ifp_destroy);
3299
3300 /* Set default values. */
3301 zclient = zclient_new(master, &zclient_options_default, bgp_handlers,
3302 array_size(bgp_handlers));
3303 zclient_init(zclient, ZEBRA_ROUTE_BGP, 0, &bgpd_privs);
3304 zclient->zebra_connected = bgp_zebra_connected;
3305 zclient->instance = instance;
3306 }
3307
3308 void bgp_zebra_destroy(void)
3309 {
3310 if (zclient == NULL)
3311 return;
3312 zclient_stop(zclient);
3313 zclient_free(zclient);
3314 zclient = NULL;
3315 }
3316
3317 int bgp_zebra_num_connects(void)
3318 {
3319 return zclient_num_connects;
3320 }
3321
3322 void bgp_send_pbr_rule_action(struct bgp_pbr_action *pbra,
3323 struct bgp_pbr_rule *pbr,
3324 bool install)
3325 {
3326 struct stream *s;
3327
3328 if (pbra->install_in_progress && !pbr)
3329 return;
3330 if (pbr && pbr->install_in_progress)
3331 return;
3332 if (BGP_DEBUG(zebra, ZEBRA)) {
3333 if (pbr)
3334 zlog_debug("%s: table %d (ip rule) %d", __func__,
3335 pbra->table_id, install);
3336 else
3337 zlog_debug("%s: table %d fwmark %d %d", __func__,
3338 pbra->table_id, pbra->fwmark, install);
3339 }
3340 s = zclient->obuf;
3341 stream_reset(s);
3342
3343 zclient_create_header(s,
3344 install ? ZEBRA_RULE_ADD : ZEBRA_RULE_DELETE,
3345 VRF_DEFAULT);
3346 stream_putl(s, 1); /* send one pbr action */
3347
3348 bgp_encode_pbr_rule_action(s, pbra, pbr);
3349
3350 stream_putw_at(s, 0, stream_get_endp(s));
3351 if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE)
3352 && install) {
3353 if (!pbr)
3354 pbra->install_in_progress = true;
3355 else
3356 pbr->install_in_progress = true;
3357 }
3358 }
3359
3360 void bgp_send_pbr_ipset_match(struct bgp_pbr_match *pbrim, bool install)
3361 {
3362 struct stream *s;
3363
3364 if (pbrim->install_in_progress)
3365 return;
3366 if (BGP_DEBUG(zebra, ZEBRA))
3367 zlog_debug("%s: name %s type %d %d, ID %u", __func__,
3368 pbrim->ipset_name, pbrim->type, install,
3369 pbrim->unique);
3370 s = zclient->obuf;
3371 stream_reset(s);
3372
3373 zclient_create_header(s,
3374 install ? ZEBRA_IPSET_CREATE :
3375 ZEBRA_IPSET_DESTROY,
3376 VRF_DEFAULT);
3377
3378 stream_putl(s, 1); /* send one pbr action */
3379
3380 bgp_encode_pbr_ipset_match(s, pbrim);
3381
3382 stream_putw_at(s, 0, stream_get_endp(s));
3383 if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE) && install)
3384 pbrim->install_in_progress = true;
3385 }
3386
3387 void bgp_send_pbr_ipset_entry_match(struct bgp_pbr_match_entry *pbrime,
3388 bool install)
3389 {
3390 struct stream *s;
3391
3392 if (pbrime->install_in_progress)
3393 return;
3394 if (BGP_DEBUG(zebra, ZEBRA))
3395 zlog_debug("%s: name %s %d %d, ID %u", __func__,
3396 pbrime->backpointer->ipset_name, pbrime->unique,
3397 install, pbrime->unique);
3398 s = zclient->obuf;
3399 stream_reset(s);
3400
3401 zclient_create_header(s,
3402 install ? ZEBRA_IPSET_ENTRY_ADD :
3403 ZEBRA_IPSET_ENTRY_DELETE,
3404 VRF_DEFAULT);
3405
3406 stream_putl(s, 1); /* send one pbr action */
3407
3408 bgp_encode_pbr_ipset_entry_match(s, pbrime);
3409
3410 stream_putw_at(s, 0, stream_get_endp(s));
3411 if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE) && install)
3412 pbrime->install_in_progress = true;
3413 }
3414
3415 static void bgp_encode_pbr_interface_list(struct bgp *bgp, struct stream *s,
3416 uint8_t family)
3417 {
3418 struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
3419 struct bgp_pbr_interface_head *head;
3420 struct bgp_pbr_interface *pbr_if;
3421 struct interface *ifp;
3422
3423 if (!bgp_pbr_cfg)
3424 return;
3425 if (family == AF_INET)
3426 head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
3427 else
3428 head = &(bgp_pbr_cfg->ifaces_by_name_ipv6);
3429 RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) {
3430 ifp = if_lookup_by_name(pbr_if->name, bgp->vrf_id);
3431 if (ifp)
3432 stream_putl(s, ifp->ifindex);
3433 }
3434 }
3435
3436 static int bgp_pbr_get_ifnumber(struct bgp *bgp, uint8_t family)
3437 {
3438 struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
3439 struct bgp_pbr_interface_head *head;
3440 struct bgp_pbr_interface *pbr_if;
3441 int cnt = 0;
3442
3443 if (!bgp_pbr_cfg)
3444 return 0;
3445 if (family == AF_INET)
3446 head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
3447 else
3448 head = &(bgp_pbr_cfg->ifaces_by_name_ipv6);
3449 RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) {
3450 if (if_lookup_by_name(pbr_if->name, bgp->vrf_id))
3451 cnt++;
3452 }
3453 return cnt;
3454 }
3455
3456 void bgp_send_pbr_iptable(struct bgp_pbr_action *pba,
3457 struct bgp_pbr_match *pbm,
3458 bool install)
3459 {
3460 struct stream *s;
3461 int ret = 0;
3462 int nb_interface;
3463
3464 if (pbm->install_iptable_in_progress)
3465 return;
3466 if (BGP_DEBUG(zebra, ZEBRA))
3467 zlog_debug("%s: name %s type %d mark %d %d, ID %u", __func__,
3468 pbm->ipset_name, pbm->type, pba->fwmark, install,
3469 pbm->unique2);
3470 s = zclient->obuf;
3471 stream_reset(s);
3472
3473 zclient_create_header(s,
3474 install ? ZEBRA_IPTABLE_ADD :
3475 ZEBRA_IPTABLE_DELETE,
3476 VRF_DEFAULT);
3477
3478 bgp_encode_pbr_iptable_match(s, pba, pbm);
3479 nb_interface = bgp_pbr_get_ifnumber(pba->bgp, pbm->family);
3480 stream_putl(s, nb_interface);
3481 if (nb_interface)
3482 bgp_encode_pbr_interface_list(pba->bgp, s, pbm->family);
3483 stream_putw_at(s, 0, stream_get_endp(s));
3484 ret = zclient_send_message(zclient);
3485 if (install) {
3486 if (ret != ZCLIENT_SEND_FAILURE)
3487 pba->refcnt++;
3488 else
3489 pbm->install_iptable_in_progress = true;
3490 }
3491 }
3492
3493 /* inject in table <table_id> a default route to:
3494 * - if nexthop IP is present : to this nexthop
3495 * - if vrf is different from local : to the matching VRF
3496 */
3497 void bgp_zebra_announce_default(struct bgp *bgp, struct nexthop *nh,
3498 afi_t afi, uint32_t table_id, bool announce)
3499 {
3500 struct zapi_nexthop *api_nh;
3501 struct zapi_route api;
3502 struct prefix p;
3503
3504 if (!nh || (nh->type != NEXTHOP_TYPE_IPV4
3505 && nh->type != NEXTHOP_TYPE_IPV6)
3506 || nh->vrf_id == VRF_UNKNOWN)
3507 return;
3508
3509 /* in vrf-lite, no default route has to be announced
3510 * the table id of vrf is directly used to divert traffic
3511 */
3512 if (!vrf_is_backend_netns() && bgp->vrf_id != nh->vrf_id)
3513 return;
3514
3515 memset(&p, 0, sizeof(struct prefix));
3516 if (afi != AFI_IP && afi != AFI_IP6)
3517 return;
3518 p.family = afi2family(afi);
3519 memset(&api, 0, sizeof(api));
3520 api.vrf_id = bgp->vrf_id;
3521 api.type = ZEBRA_ROUTE_BGP;
3522 api.safi = SAFI_UNICAST;
3523 api.prefix = p;
3524 api.tableid = table_id;
3525 api.nexthop_num = 1;
3526 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
3527 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
3528 api_nh = &api.nexthops[0];
3529
3530 api.distance = ZEBRA_EBGP_DISTANCE_DEFAULT;
3531 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
3532
3533 /* redirect IP */
3534 if (afi == AFI_IP && nh->gate.ipv4.s_addr != INADDR_ANY) {
3535 char buff[PREFIX_STRLEN];
3536
3537 api_nh->vrf_id = nh->vrf_id;
3538 api_nh->gate.ipv4 = nh->gate.ipv4;
3539 api_nh->type = NEXTHOP_TYPE_IPV4;
3540
3541 inet_ntop(AF_INET, &(nh->gate.ipv4), buff, INET_ADDRSTRLEN);
3542 if (BGP_DEBUG(zebra, ZEBRA))
3543 zlog_debug("BGP: %s default route to %s table %d (redirect IP)",
3544 announce ? "adding" : "withdrawing",
3545 buff, table_id);
3546 zclient_route_send(announce ? ZEBRA_ROUTE_ADD
3547 : ZEBRA_ROUTE_DELETE,
3548 zclient, &api);
3549 } else if (afi == AFI_IP6 &&
3550 memcmp(&nh->gate.ipv6,
3551 &in6addr_any, sizeof(struct in6_addr))) {
3552 char buff[PREFIX_STRLEN];
3553
3554 api_nh->vrf_id = nh->vrf_id;
3555 memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6,
3556 sizeof(struct in6_addr));
3557 api_nh->type = NEXTHOP_TYPE_IPV6;
3558
3559 inet_ntop(AF_INET6, &(nh->gate.ipv6), buff, INET_ADDRSTRLEN);
3560 if (BGP_DEBUG(zebra, ZEBRA))
3561 zlog_debug("BGP: %s default route to %s table %d (redirect IP)",
3562 announce ? "adding" : "withdrawing",
3563 buff, table_id);
3564 zclient_route_send(announce ? ZEBRA_ROUTE_ADD
3565 : ZEBRA_ROUTE_DELETE,
3566 zclient, &api);
3567 } else if (nh->vrf_id != bgp->vrf_id) {
3568 struct vrf *vrf;
3569 struct interface *ifp;
3570
3571 vrf = vrf_lookup_by_id(nh->vrf_id);
3572 if (!vrf)
3573 return;
3574 /* create default route with interface <VRF>
3575 * with nexthop-vrf <VRF>
3576 */
3577 ifp = if_lookup_by_name_vrf(vrf->name, vrf);
3578 if (!ifp)
3579 return;
3580 api_nh->vrf_id = nh->vrf_id;
3581 api_nh->type = NEXTHOP_TYPE_IFINDEX;
3582 api_nh->ifindex = ifp->ifindex;
3583 if (BGP_DEBUG(zebra, ZEBRA))
3584 zlog_info("BGP: %s default route to %s table %d (redirect VRF)",
3585 announce ? "adding" : "withdrawing",
3586 vrf->name, table_id);
3587 zclient_route_send(announce ? ZEBRA_ROUTE_ADD
3588 : ZEBRA_ROUTE_DELETE,
3589 zclient, &api);
3590 return;
3591 }
3592 }
3593
3594 /* Send capabilities to RIB */
3595 int bgp_zebra_send_capabilities(struct bgp *bgp, bool disable)
3596 {
3597 struct zapi_cap api;
3598 int ret = BGP_GR_SUCCESS;
3599
3600 if (zclient == NULL) {
3601 if (BGP_DEBUG(zebra, ZEBRA))
3602 zlog_debug("zclient invalid");
3603 return BGP_GR_FAILURE;
3604 }
3605
3606 /* Check if the client is connected */
3607 if ((zclient->sock < 0) || (zclient->t_connect)) {
3608 if (BGP_DEBUG(zebra, ZEBRA))
3609 zlog_debug("client not connected");
3610 return BGP_GR_FAILURE;
3611 }
3612
3613 /* Check if capability is already sent. If the flag force is set
3614 * send the capability since this can be initial bgp configuration
3615 */
3616 memset(&api, 0, sizeof(struct zapi_cap));
3617 if (disable) {
3618 api.cap = ZEBRA_CLIENT_GR_DISABLE;
3619 api.vrf_id = bgp->vrf_id;
3620 } else {
3621 api.cap = ZEBRA_CLIENT_GR_CAPABILITIES;
3622 api.stale_removal_time = bgp->rib_stale_time;
3623 api.vrf_id = bgp->vrf_id;
3624 }
3625
3626 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api)
3627 == ZCLIENT_SEND_FAILURE) {
3628 zlog_err("error sending capability");
3629 ret = BGP_GR_FAILURE;
3630 } else {
3631 if (disable)
3632 bgp->present_zebra_gr_state = ZEBRA_GR_DISABLE;
3633 else
3634 bgp->present_zebra_gr_state = ZEBRA_GR_ENABLE;
3635
3636 if (BGP_DEBUG(zebra, ZEBRA))
3637 zlog_debug("send capabilty success");
3638 ret = BGP_GR_SUCCESS;
3639 }
3640 return ret;
3641 }
3642
3643 /* Send route update pesding or completed status to RIB for the
3644 * specific AFI, SAFI
3645 */
3646 int bgp_zebra_update(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type)
3647 {
3648 struct zapi_cap api = {0};
3649
3650 if (zclient == NULL) {
3651 if (BGP_DEBUG(zebra, ZEBRA))
3652 zlog_debug("zclient == NULL, invalid");
3653 return BGP_GR_FAILURE;
3654 }
3655
3656 /* Check if the client is connected */
3657 if ((zclient->sock < 0) || (zclient->t_connect)) {
3658 if (BGP_DEBUG(zebra, ZEBRA))
3659 zlog_debug("client not connected");
3660 return BGP_GR_FAILURE;
3661 }
3662
3663 api.afi = afi;
3664 api.safi = safi;
3665 api.vrf_id = vrf_id;
3666 api.cap = type;
3667
3668 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api)
3669 == ZCLIENT_SEND_FAILURE) {
3670 if (BGP_DEBUG(zebra, ZEBRA))
3671 zlog_debug("error sending capability");
3672 return BGP_GR_FAILURE;
3673 }
3674 return BGP_GR_SUCCESS;
3675 }
3676
3677
3678 /* Send RIB stale timer update */
3679 int bgp_zebra_stale_timer_update(struct bgp *bgp)
3680 {
3681 struct zapi_cap api;
3682
3683 if (zclient == NULL) {
3684 if (BGP_DEBUG(zebra, ZEBRA))
3685 zlog_debug("zclient invalid");
3686 return BGP_GR_FAILURE;
3687 }
3688
3689 /* Check if the client is connected */
3690 if ((zclient->sock < 0) || (zclient->t_connect)) {
3691 if (BGP_DEBUG(zebra, ZEBRA))
3692 zlog_debug("client not connected");
3693 return BGP_GR_FAILURE;
3694 }
3695
3696 memset(&api, 0, sizeof(struct zapi_cap));
3697 api.cap = ZEBRA_CLIENT_RIB_STALE_TIME;
3698 api.stale_removal_time = bgp->rib_stale_time;
3699 api.vrf_id = bgp->vrf_id;
3700 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api)
3701 == ZCLIENT_SEND_FAILURE) {
3702 if (BGP_DEBUG(zebra, ZEBRA))
3703 zlog_debug("error sending capability");
3704 return BGP_GR_FAILURE;
3705 }
3706 if (BGP_DEBUG(zebra, ZEBRA))
3707 zlog_debug("send capabilty success");
3708 return BGP_GR_SUCCESS;
3709 }
3710
3711 int bgp_zebra_srv6_manager_get_locator_chunk(const char *name)
3712 {
3713 return srv6_manager_get_locator_chunk(zclient, name);
3714 }
3715
3716 int bgp_zebra_srv6_manager_release_locator_chunk(const char *name)
3717 {
3718 return srv6_manager_release_locator_chunk(zclient, name);
3719 }