]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_zebra.c
Merge pull request #10323 from opensourcerouting/ospf6-lsa-stats
[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 int 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 return 0;
1054 }
1055
1056 bool bgp_zebra_tm_chunk_obtained(void)
1057 {
1058 return bgp_tm_chunk_obtained;
1059 }
1060
1061 uint32_t bgp_zebra_tm_get_id(void)
1062 {
1063 static int table_id;
1064
1065 if (!bgp_tm_chunk_obtained)
1066 return ++table_id;
1067 return bgp_tm_min++;
1068 }
1069
1070 void bgp_zebra_init_tm_connect(struct bgp *bgp)
1071 {
1072 int delay = 1;
1073
1074 /* if already set, do nothing
1075 */
1076 if (bgp_tm_thread_connect != NULL)
1077 return;
1078 bgp_tm_status_connected = false;
1079 bgp_tm_chunk_obtained = false;
1080 bgp_tm_min = bgp_tm_max = 0;
1081 bgp_tm_chunk_size = BGP_FLOWSPEC_TABLE_CHUNK;
1082 bgp_tm_bgp = bgp;
1083 thread_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay,
1084 &bgp_tm_thread_connect);
1085 }
1086
1087 int bgp_zebra_get_table_range(uint32_t chunk_size,
1088 uint32_t *start, uint32_t *end)
1089 {
1090 int ret;
1091
1092 if (!bgp_tm_status_connected)
1093 return -1;
1094 ret = tm_get_table_chunk(zclient, chunk_size, start, end);
1095 if (ret < 0) {
1096 flog_err(EC_BGP_TABLE_CHUNK,
1097 "BGP: Error getting table chunk %u", chunk_size);
1098 return -1;
1099 }
1100 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1101 chunk_size, *start, *end);
1102 return 0;
1103 }
1104
1105 static bool update_ipv4nh_for_route_install(int nh_othervrf, struct bgp *nh_bgp,
1106 struct in_addr *nexthop,
1107 struct attr *attr, bool is_evpn,
1108 struct zapi_nexthop *api_nh)
1109 {
1110 api_nh->gate.ipv4 = *nexthop;
1111 api_nh->vrf_id = nh_bgp->vrf_id;
1112
1113 /* Need to set fields appropriately for EVPN routes imported into
1114 * a VRF (which are programmed as onlink on l3-vni SVI) as well as
1115 * connected routes leaked into a VRF.
1116 */
1117 if (attr->nh_type == NEXTHOP_TYPE_BLACKHOLE) {
1118 api_nh->type = attr->nh_type;
1119 api_nh->bh_type = attr->bh_type;
1120 } else if (is_evpn) {
1121 /*
1122 * If the nexthop is EVPN overlay index gateway IP,
1123 * treat the nexthop as NEXTHOP_TYPE_IPV4
1124 * Else, mark the nexthop as onlink.
1125 */
1126 if (attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP)
1127 api_nh->type = NEXTHOP_TYPE_IPV4;
1128 else {
1129 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1130 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK);
1131 api_nh->ifindex = nh_bgp->l3vni_svi_ifindex;
1132 }
1133 } else if (nh_othervrf && api_nh->gate.ipv4.s_addr == INADDR_ANY) {
1134 api_nh->type = NEXTHOP_TYPE_IFINDEX;
1135 api_nh->ifindex = attr->nh_ifindex;
1136 } else
1137 api_nh->type = NEXTHOP_TYPE_IPV4;
1138
1139 return true;
1140 }
1141
1142 static bool update_ipv6nh_for_route_install(int nh_othervrf, struct bgp *nh_bgp,
1143 struct in6_addr *nexthop,
1144 ifindex_t ifindex,
1145 struct bgp_path_info *pi,
1146 struct bgp_path_info *best_pi,
1147 bool is_evpn,
1148 struct zapi_nexthop *api_nh)
1149 {
1150 struct attr *attr;
1151
1152 attr = pi->attr;
1153 api_nh->vrf_id = nh_bgp->vrf_id;
1154
1155 if (attr->nh_type == NEXTHOP_TYPE_BLACKHOLE) {
1156 api_nh->type = attr->nh_type;
1157 api_nh->bh_type = attr->bh_type;
1158 } else if (is_evpn) {
1159 /*
1160 * If the nexthop is EVPN overlay index gateway IP,
1161 * treat the nexthop as NEXTHOP_TYPE_IPV4
1162 * Else, mark the nexthop as onlink.
1163 */
1164 if (attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP)
1165 api_nh->type = NEXTHOP_TYPE_IPV6;
1166 else {
1167 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1168 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK);
1169 api_nh->ifindex = nh_bgp->l3vni_svi_ifindex;
1170 }
1171 } else if (nh_othervrf) {
1172 if (IN6_IS_ADDR_UNSPECIFIED(nexthop)) {
1173 api_nh->type = NEXTHOP_TYPE_IFINDEX;
1174 api_nh->ifindex = attr->nh_ifindex;
1175 } else if (IN6_IS_ADDR_LINKLOCAL(nexthop)) {
1176 if (ifindex == 0)
1177 return false;
1178 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1179 api_nh->ifindex = ifindex;
1180 } else {
1181 api_nh->type = NEXTHOP_TYPE_IPV6;
1182 api_nh->ifindex = 0;
1183 }
1184 } else {
1185 if (IN6_IS_ADDR_LINKLOCAL(nexthop)) {
1186 if (pi == best_pi
1187 && attr->mp_nexthop_len
1188 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
1189 if (pi->peer->nexthop.ifp)
1190 ifindex =
1191 pi->peer->nexthop.ifp->ifindex;
1192 if (!ifindex) {
1193 if (pi->peer->conf_if)
1194 ifindex = pi->peer->ifp->ifindex;
1195 else if (pi->peer->ifname)
1196 ifindex = ifname2ifindex(
1197 pi->peer->ifname,
1198 pi->peer->bgp->vrf_id);
1199 else if (pi->peer->nexthop.ifp)
1200 ifindex =
1201 pi->peer->nexthop.ifp->ifindex;
1202 }
1203
1204 if (ifindex == 0)
1205 return false;
1206 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1207 api_nh->ifindex = ifindex;
1208 } else {
1209 api_nh->type = NEXTHOP_TYPE_IPV6;
1210 api_nh->ifindex = 0;
1211 }
1212 }
1213 /* api_nh structure has union of gate and bh_type */
1214 if (nexthop && api_nh->type != NEXTHOP_TYPE_BLACKHOLE)
1215 api_nh->gate.ipv6 = *nexthop;
1216
1217 return true;
1218 }
1219
1220 static bool bgp_zebra_use_nhop_weighted(struct bgp *bgp, struct attr *attr,
1221 uint64_t tot_bw, uint32_t *nh_weight)
1222 {
1223 uint32_t bw;
1224 uint64_t tmp;
1225
1226 bw = attr->link_bw;
1227 /* zero link-bandwidth and link-bandwidth not present are treated
1228 * as the same situation.
1229 */
1230 if (!bw) {
1231 /* the only situations should be if we're either told
1232 * to skip or use default weight.
1233 */
1234 if (bgp->lb_handling == BGP_LINK_BW_SKIP_MISSING)
1235 return false;
1236 *nh_weight = BGP_ZEBRA_DEFAULT_NHOP_WEIGHT;
1237 } else {
1238 tmp = (uint64_t)bw * 100;
1239 *nh_weight = ((uint32_t)(tmp / tot_bw));
1240 }
1241
1242 return true;
1243 }
1244
1245 void bgp_zebra_announce(struct bgp_dest *dest, const struct prefix *p,
1246 struct bgp_path_info *info, struct bgp *bgp, afi_t afi,
1247 safi_t safi)
1248 {
1249 struct zapi_route api = { 0 };
1250 struct zapi_nexthop *api_nh;
1251 int nh_family;
1252 unsigned int valid_nh_count = 0;
1253 bool allow_recursion = false;
1254 uint8_t distance;
1255 struct peer *peer;
1256 struct bgp_path_info *mpinfo;
1257 struct bgp *bgp_orig;
1258 uint32_t metric;
1259 struct attr local_attr;
1260 struct bgp_path_info local_info;
1261 struct bgp_path_info *mpinfo_cp = &local_info;
1262 route_tag_t tag;
1263 mpls_label_t label;
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 memcpy(&api_nh->seg6_segs, &mpinfo->extra->sid[0].sid,
1481 sizeof(api_nh->seg6_segs));
1482
1483 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6);
1484 }
1485
1486 valid_nh_count++;
1487 }
1488
1489 is_add = (valid_nh_count || nhg_id) ? true : false;
1490
1491 if (is_add && CHECK_FLAG(bm->flags, BM_FLAG_SEND_EXTRA_DATA_TO_ZEBRA)) {
1492 struct bgp_zebra_opaque bzo = {};
1493 const char *reason =
1494 bgp_path_selection_reason2str(dest->reason);
1495
1496 strlcpy(bzo.aspath, info->attr->aspath->str,
1497 sizeof(bzo.aspath));
1498
1499 if (info->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES))
1500 strlcpy(bzo.community, info->attr->community->str,
1501 sizeof(bzo.community));
1502
1503 if (info->attr->flag
1504 & ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES))
1505 strlcpy(bzo.lcommunity, info->attr->lcommunity->str,
1506 sizeof(bzo.lcommunity));
1507
1508 strlcpy(bzo.selection_reason, reason,
1509 sizeof(bzo.selection_reason));
1510
1511 SET_FLAG(api.message, ZAPI_MESSAGE_OPAQUE);
1512 api.opaque.length = MIN(sizeof(struct bgp_zebra_opaque),
1513 ZAPI_MESSAGE_OPAQUE_LENGTH);
1514 memcpy(api.opaque.data, &bzo, api.opaque.length);
1515 }
1516
1517 if (allow_recursion)
1518 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1519
1520 /*
1521 * When we create an aggregate route we must also
1522 * install a Null0 route in the RIB, so overwrite
1523 * what was written into api with a blackhole route
1524 */
1525 if (info->sub_type == BGP_ROUTE_AGGREGATE)
1526 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
1527 else
1528 api.nexthop_num = valid_nh_count;
1529
1530 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
1531 api.metric = metric;
1532
1533 if (tag) {
1534 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1535 api.tag = tag;
1536 }
1537
1538 distance = bgp_distance_apply(p, info, afi, safi, bgp);
1539 if (distance) {
1540 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
1541 api.distance = distance;
1542 }
1543
1544 if (bgp_debug_zebra(p)) {
1545 char nh_buf[INET6_ADDRSTRLEN];
1546 char eth_buf[ETHER_ADDR_STRLEN + 7] = {'\0'};
1547 char buf1[ETHER_ADDR_STRLEN];
1548 char label_buf[20];
1549 char sid_buf[20];
1550 char segs_buf[256];
1551 int i;
1552
1553 zlog_debug(
1554 "Tx route %s VRF %u %pFX metric %u tag %" ROUTE_TAG_PRI
1555 " count %d nhg %d",
1556 valid_nh_count ? "add" : "delete", bgp->vrf_id,
1557 &api.prefix, api.metric, api.tag, api.nexthop_num,
1558 nhg_id);
1559 for (i = 0; i < api.nexthop_num; i++) {
1560 api_nh = &api.nexthops[i];
1561
1562 switch (api_nh->type) {
1563 case NEXTHOP_TYPE_IFINDEX:
1564 nh_buf[0] = '\0';
1565 break;
1566 case NEXTHOP_TYPE_IPV4:
1567 case NEXTHOP_TYPE_IPV4_IFINDEX:
1568 nh_family = AF_INET;
1569 inet_ntop(nh_family, &api_nh->gate, nh_buf,
1570 sizeof(nh_buf));
1571 break;
1572 case NEXTHOP_TYPE_IPV6:
1573 case NEXTHOP_TYPE_IPV6_IFINDEX:
1574 nh_family = AF_INET6;
1575 inet_ntop(nh_family, &api_nh->gate, nh_buf,
1576 sizeof(nh_buf));
1577 break;
1578 case NEXTHOP_TYPE_BLACKHOLE:
1579 strlcpy(nh_buf, "blackhole", sizeof(nh_buf));
1580 break;
1581 default:
1582 /* Note: add new nexthop case */
1583 assert(0);
1584 break;
1585 }
1586
1587 label_buf[0] = '\0';
1588 eth_buf[0] = '\0';
1589 segs_buf[0] = '\0';
1590 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL)
1591 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE))
1592 snprintf(label_buf, sizeof(label_buf),
1593 "label %u", api_nh->labels[0]);
1594 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6)
1595 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1596 inet_ntop(AF_INET6, &api_nh->seg6_segs,
1597 sid_buf, sizeof(sid_buf));
1598 snprintf(segs_buf, sizeof(segs_buf), "segs %s",
1599 sid_buf);
1600 }
1601 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)
1602 && !is_zero_mac(&api_nh->rmac))
1603 snprintf(eth_buf, sizeof(eth_buf), " RMAC %s",
1604 prefix_mac2str(&api_nh->rmac,
1605 buf1, sizeof(buf1)));
1606 zlog_debug(" nhop [%d]: %s if %u VRF %u wt %u %s %s %s",
1607 i + 1, nh_buf, api_nh->ifindex,
1608 api_nh->vrf_id, api_nh->weight,
1609 label_buf, segs_buf, eth_buf);
1610 }
1611
1612 int recursion_flag = 0;
1613
1614 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION))
1615 recursion_flag = 1;
1616
1617 zlog_debug("%s: %pFX: announcing to zebra (recursion %sset)",
1618 __func__, p, (recursion_flag ? "" : "NOT "));
1619 }
1620 zclient_route_send(is_add ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE,
1621 zclient, &api);
1622 }
1623
1624 /* Announce all routes of a table to zebra */
1625 void bgp_zebra_announce_table(struct bgp *bgp, afi_t afi, safi_t safi)
1626 {
1627 struct bgp_dest *dest;
1628 struct bgp_table *table;
1629 struct bgp_path_info *pi;
1630
1631 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1632 * know of this instance.
1633 */
1634 if (!bgp_install_info_to_zebra(bgp))
1635 return;
1636
1637 table = bgp->rib[afi][safi];
1638 if (!table)
1639 return;
1640
1641 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest))
1642 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
1643 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED) &&
1644
1645 (pi->type == ZEBRA_ROUTE_BGP
1646 && (pi->sub_type == BGP_ROUTE_NORMAL
1647 || pi->sub_type == BGP_ROUTE_IMPORTED)))
1648
1649 bgp_zebra_announce(dest,
1650 bgp_dest_get_prefix(dest),
1651 pi, bgp, afi, safi);
1652 }
1653
1654 /* Announce routes of any bgp subtype of a table to zebra */
1655 void bgp_zebra_announce_table_all_subtypes(struct bgp *bgp, afi_t afi,
1656 safi_t safi)
1657 {
1658 struct bgp_dest *dest;
1659 struct bgp_table *table;
1660 struct bgp_path_info *pi;
1661
1662 if (!bgp_install_info_to_zebra(bgp))
1663 return;
1664
1665 table = bgp->rib[afi][safi];
1666 if (!table)
1667 return;
1668
1669 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest))
1670 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
1671 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED) &&
1672 pi->type == ZEBRA_ROUTE_BGP)
1673 bgp_zebra_announce(dest,
1674 bgp_dest_get_prefix(dest),
1675 pi, bgp, afi, safi);
1676 }
1677
1678 void bgp_zebra_withdraw(const struct prefix *p, struct bgp_path_info *info,
1679 struct bgp *bgp, safi_t safi)
1680 {
1681 struct zapi_route api;
1682 struct peer *peer;
1683
1684 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1685 * know of this instance.
1686 */
1687 if (!bgp_install_info_to_zebra(bgp))
1688 return;
1689
1690 if (safi == SAFI_FLOWSPEC) {
1691 peer = info->peer;
1692 bgp_pbr_update_entry(peer->bgp, p, info, AFI_IP, safi, false);
1693 return;
1694 }
1695
1696 memset(&api, 0, sizeof(api));
1697 api.vrf_id = bgp->vrf_id;
1698 api.type = ZEBRA_ROUTE_BGP;
1699 api.safi = safi;
1700 api.prefix = *p;
1701
1702 if (info->attr->rmap_table_id) {
1703 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
1704 api.tableid = info->attr->rmap_table_id;
1705 }
1706
1707 /* If the route's source is EVPN, flag as such. */
1708 if (is_route_parent_evpn(info))
1709 SET_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE);
1710
1711 if (bgp_debug_zebra(p))
1712 zlog_debug("Tx route delete VRF %u %pFX", bgp->vrf_id,
1713 &api.prefix);
1714
1715 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
1716 }
1717
1718 /* Withdraw all entries in a BGP instances RIB table from Zebra */
1719 void bgp_zebra_withdraw_table_all_subtypes(struct bgp *bgp, afi_t afi, safi_t safi)
1720 {
1721 struct bgp_dest *dest;
1722 struct bgp_table *table;
1723 struct bgp_path_info *pi;
1724
1725 if (!bgp_install_info_to_zebra(bgp))
1726 return;
1727
1728 table = bgp->rib[afi][safi];
1729 if (!table)
1730 return;
1731
1732 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
1733 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
1734 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
1735 && (pi->type == ZEBRA_ROUTE_BGP))
1736 bgp_zebra_withdraw(bgp_dest_get_prefix(dest),
1737 pi, bgp, safi);
1738 }
1739 }
1740 }
1741
1742 struct bgp_redist *bgp_redist_lookup(struct bgp *bgp, afi_t afi, uint8_t type,
1743 unsigned short instance)
1744 {
1745 struct list *red_list;
1746 struct listnode *node;
1747 struct bgp_redist *red;
1748
1749 red_list = bgp->redist[afi][type];
1750 if (!red_list)
1751 return (NULL);
1752
1753 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
1754 if (red->instance == instance)
1755 return red;
1756
1757 return NULL;
1758 }
1759
1760 struct bgp_redist *bgp_redist_add(struct bgp *bgp, afi_t afi, uint8_t type,
1761 unsigned short instance)
1762 {
1763 struct list *red_list;
1764 struct bgp_redist *red;
1765
1766 red = bgp_redist_lookup(bgp, afi, type, instance);
1767 if (red)
1768 return red;
1769
1770 if (!bgp->redist[afi][type])
1771 bgp->redist[afi][type] = list_new();
1772
1773 red_list = bgp->redist[afi][type];
1774 red = XCALLOC(MTYPE_BGP_REDIST, sizeof(struct bgp_redist));
1775 red->instance = instance;
1776
1777 listnode_add(red_list, red);
1778
1779 return red;
1780 }
1781
1782 static void bgp_redist_del(struct bgp *bgp, afi_t afi, uint8_t type,
1783 unsigned short instance)
1784 {
1785 struct bgp_redist *red;
1786
1787 red = bgp_redist_lookup(bgp, afi, type, instance);
1788
1789 if (red) {
1790 listnode_delete(bgp->redist[afi][type], red);
1791 XFREE(MTYPE_BGP_REDIST, red);
1792 if (!bgp->redist[afi][type]->count)
1793 list_delete(&bgp->redist[afi][type]);
1794 }
1795 }
1796
1797 /* Other routes redistribution into BGP. */
1798 int bgp_redistribute_set(struct bgp *bgp, afi_t afi, int type,
1799 unsigned short instance, bool changed)
1800 {
1801 /* If redistribute options are changed call
1802 * bgp_redistribute_unreg() to reset the option and withdraw
1803 * the routes
1804 */
1805 if (changed)
1806 bgp_redistribute_unreg(bgp, afi, type, instance);
1807
1808 /* Return if already redistribute flag is set. */
1809 if (instance) {
1810 if (redist_check_instance(&zclient->mi_redist[afi][type],
1811 instance))
1812 return CMD_WARNING;
1813
1814 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1815 } else {
1816 if (vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1817 return CMD_WARNING;
1818
1819 #ifdef ENABLE_BGP_VNC
1820 if (EVPN_ENABLED(bgp) && type == ZEBRA_ROUTE_VNC_DIRECT) {
1821 vnc_export_bgp_enable(
1822 bgp, afi); /* only enables if mode bits cfg'd */
1823 }
1824 #endif
1825
1826 vrf_bitmap_set(zclient->redist[afi][type], bgp->vrf_id);
1827 }
1828
1829 /*
1830 * Don't try to register if we're not connected to Zebra or Zebra
1831 * doesn't know of this instance.
1832 *
1833 * When we come up later well resend if needed.
1834 */
1835 if (!bgp_install_info_to_zebra(bgp))
1836 return CMD_SUCCESS;
1837
1838 if (BGP_DEBUG(zebra, ZEBRA))
1839 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1840 bgp->vrf_id, afi, zebra_route_string(type),
1841 instance);
1842
1843 /* Send distribute add message to zebra. */
1844 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1845 instance, bgp->vrf_id);
1846
1847 return CMD_SUCCESS;
1848 }
1849
1850 int bgp_redistribute_resend(struct bgp *bgp, afi_t afi, int type,
1851 unsigned short instance)
1852 {
1853 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1854 * know of this instance.
1855 */
1856 if (!bgp_install_info_to_zebra(bgp))
1857 return -1;
1858
1859 if (BGP_DEBUG(zebra, ZEBRA))
1860 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1861 bgp->vrf_id, afi, zebra_route_string(type),
1862 instance);
1863
1864 /* Send distribute add message to zebra. */
1865 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type,
1866 instance, bgp->vrf_id);
1867 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1868 instance, bgp->vrf_id);
1869
1870 return 0;
1871 }
1872
1873 /* Redistribute with route-map specification. */
1874 bool bgp_redistribute_rmap_set(struct bgp_redist *red, const char *name,
1875 struct route_map *route_map)
1876 {
1877 if (red->rmap.name && (strcmp(red->rmap.name, name) == 0))
1878 return false;
1879
1880 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1881 /* Decrement the count for existing routemap and
1882 * increment the count for new route map.
1883 */
1884 route_map_counter_decrement(red->rmap.map);
1885 red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
1886 red->rmap.map = route_map;
1887 route_map_counter_increment(red->rmap.map);
1888
1889 return true;
1890 }
1891
1892 /* Redistribute with metric specification. */
1893 bool bgp_redistribute_metric_set(struct bgp *bgp, struct bgp_redist *red,
1894 afi_t afi, int type, uint32_t metric)
1895 {
1896 struct bgp_dest *dest;
1897 struct bgp_path_info *pi;
1898
1899 if (red->redist_metric_flag && red->redist_metric == metric)
1900 return false;
1901
1902 red->redist_metric_flag = 1;
1903 red->redist_metric = metric;
1904
1905 for (dest = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); dest;
1906 dest = bgp_route_next(dest)) {
1907 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
1908 if (pi->sub_type == BGP_ROUTE_REDISTRIBUTE
1909 && pi->type == type
1910 && pi->instance == red->instance) {
1911 struct attr *old_attr;
1912 struct attr new_attr;
1913
1914 new_attr = *pi->attr;
1915 new_attr.med = red->redist_metric;
1916 old_attr = pi->attr;
1917 pi->attr = bgp_attr_intern(&new_attr);
1918 bgp_attr_unintern(&old_attr);
1919
1920 bgp_path_info_set_flag(dest, pi,
1921 BGP_PATH_ATTR_CHANGED);
1922 bgp_process(bgp, dest, afi, SAFI_UNICAST);
1923 }
1924 }
1925 }
1926
1927 return true;
1928 }
1929
1930 /* Unset redistribution. */
1931 int bgp_redistribute_unreg(struct bgp *bgp, afi_t afi, int type,
1932 unsigned short instance)
1933 {
1934 struct bgp_redist *red;
1935
1936 red = bgp_redist_lookup(bgp, afi, type, instance);
1937 if (!red)
1938 return CMD_SUCCESS;
1939
1940 /* Return if zebra connection is disabled. */
1941 if (instance) {
1942 if (!redist_check_instance(&zclient->mi_redist[afi][type],
1943 instance))
1944 return CMD_WARNING;
1945 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1946 } else {
1947 if (!vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1948 return CMD_WARNING;
1949 vrf_bitmap_unset(zclient->redist[afi][type], bgp->vrf_id);
1950 }
1951
1952 if (bgp_install_info_to_zebra(bgp)) {
1953 /* Send distribute delete message to zebra. */
1954 if (BGP_DEBUG(zebra, ZEBRA))
1955 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1956 bgp->vrf_id, afi, zebra_route_string(type),
1957 instance);
1958 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi,
1959 type, instance, bgp->vrf_id);
1960 }
1961
1962 /* Withdraw redistributed routes from current BGP's routing table. */
1963 bgp_redistribute_withdraw(bgp, afi, type, instance);
1964
1965 return CMD_SUCCESS;
1966 }
1967
1968 /* Unset redistribution. */
1969 int bgp_redistribute_unset(struct bgp *bgp, afi_t afi, int type,
1970 unsigned short instance)
1971 {
1972 struct bgp_redist *red;
1973
1974 /*
1975 * vnc and vpn->vrf checks must be before red check because
1976 * they operate within bgpd irrespective of zebra connection
1977 * status. red lookup fails if there is no zebra connection.
1978 */
1979 #ifdef ENABLE_BGP_VNC
1980 if (EVPN_ENABLED(bgp) && type == ZEBRA_ROUTE_VNC_DIRECT) {
1981 vnc_export_bgp_disable(bgp, afi);
1982 }
1983 #endif
1984
1985 red = bgp_redist_lookup(bgp, afi, type, instance);
1986 if (!red)
1987 return CMD_SUCCESS;
1988
1989 bgp_redistribute_unreg(bgp, afi, type, instance);
1990
1991 /* Unset route-map. */
1992 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1993 route_map_counter_decrement(red->rmap.map);
1994 red->rmap.map = NULL;
1995
1996 /* Unset metric. */
1997 red->redist_metric_flag = 0;
1998 red->redist_metric = 0;
1999
2000 bgp_redist_del(bgp, afi, type, instance);
2001
2002 return CMD_SUCCESS;
2003 }
2004
2005 void bgp_redistribute_redo(struct bgp *bgp)
2006 {
2007 afi_t afi;
2008 int i;
2009 struct list *red_list;
2010 struct listnode *node;
2011 struct bgp_redist *red;
2012
2013 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
2014 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
2015
2016 red_list = bgp->redist[afi][i];
2017 if (!red_list)
2018 continue;
2019
2020 for (ALL_LIST_ELEMENTS_RO(red_list, node, red)) {
2021 bgp_redistribute_resend(bgp, afi, i,
2022 red->instance);
2023 }
2024 }
2025 }
2026 }
2027
2028 void bgp_zclient_reset(void)
2029 {
2030 zclient_reset(zclient);
2031 }
2032
2033 /* Register this instance with Zebra. Invoked upon connect (for
2034 * default instance) and when other VRFs are learnt (or created and
2035 * already learnt).
2036 */
2037 void bgp_zebra_instance_register(struct bgp *bgp)
2038 {
2039 /* Don't try to register if we're not connected to Zebra */
2040 if (!zclient || zclient->sock < 0)
2041 return;
2042
2043 if (BGP_DEBUG(zebra, ZEBRA))
2044 zlog_debug("Registering VRF %u", bgp->vrf_id);
2045
2046 /* Register for router-id, interfaces, redistributed routes. */
2047 zclient_send_reg_requests(zclient, bgp->vrf_id);
2048
2049 /* For EVPN instance, register to learn about VNIs, if appropriate. */
2050 if (bgp->advertise_all_vni)
2051 bgp_zebra_advertise_all_vni(bgp, 1);
2052
2053 bgp_nht_register_nexthops(bgp);
2054 }
2055
2056 /* Deregister this instance with Zebra. Invoked upon the instance
2057 * being deleted (default or VRF) and it is already registered.
2058 */
2059 void bgp_zebra_instance_deregister(struct bgp *bgp)
2060 {
2061 /* Don't try to deregister if we're not connected to Zebra */
2062 if (zclient->sock < 0)
2063 return;
2064
2065 if (BGP_DEBUG(zebra, ZEBRA))
2066 zlog_debug("Deregistering VRF %u", bgp->vrf_id);
2067
2068 /* For EVPN instance, unregister learning about VNIs, if appropriate. */
2069 if (bgp->advertise_all_vni)
2070 bgp_zebra_advertise_all_vni(bgp, 0);
2071
2072 /* Deregister for router-id, interfaces, redistributed routes. */
2073 zclient_send_dereg_requests(zclient, bgp->vrf_id);
2074 }
2075
2076 void bgp_zebra_initiate_radv(struct bgp *bgp, struct peer *peer)
2077 {
2078 uint32_t ra_interval = BGP_UNNUM_DEFAULT_RA_INTERVAL;
2079
2080 /* Don't try to initiate if we're not connected to Zebra */
2081 if (zclient->sock < 0)
2082 return;
2083
2084 if (BGP_DEBUG(zebra, ZEBRA))
2085 zlog_debug("%u: Initiating RA for peer %s", bgp->vrf_id,
2086 peer->host);
2087
2088 /*
2089 * If unnumbered peer (peer->ifp) call thru zapi to start RAs.
2090 * If we don't have an ifp pointer, call function to find the
2091 * ifps for a numbered enhe peer to turn RAs on.
2092 */
2093 peer->ifp ? zclient_send_interface_radv_req(zclient, bgp->vrf_id,
2094 peer->ifp, 1, ra_interval)
2095 : bgp_nht_reg_enhe_cap_intfs(peer);
2096 }
2097
2098 void bgp_zebra_terminate_radv(struct bgp *bgp, struct peer *peer)
2099 {
2100 /* Don't try to terminate if we're not connected to Zebra */
2101 if (zclient->sock < 0)
2102 return;
2103
2104 if (BGP_DEBUG(zebra, ZEBRA))
2105 zlog_debug("%u: Terminating RA for peer %s", bgp->vrf_id,
2106 peer->host);
2107
2108 /*
2109 * If unnumbered peer (peer->ifp) call thru zapi to stop RAs.
2110 * If we don't have an ifp pointer, call function to find the
2111 * ifps for a numbered enhe peer to turn RAs off.
2112 */
2113 peer->ifp ? zclient_send_interface_radv_req(zclient, bgp->vrf_id,
2114 peer->ifp, 0, 0)
2115 : bgp_nht_dereg_enhe_cap_intfs(peer);
2116 }
2117
2118 int bgp_zebra_advertise_subnet(struct bgp *bgp, int advertise, vni_t vni)
2119 {
2120 struct stream *s = NULL;
2121
2122 /* Check socket. */
2123 if (!zclient || zclient->sock < 0)
2124 return 0;
2125
2126 /* Don't try to register if Zebra doesn't know of this instance. */
2127 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
2128 if (BGP_DEBUG(zebra, ZEBRA))
2129 zlog_debug(
2130 "%s: No zebra instance to talk to, cannot advertise subnet",
2131 __func__);
2132 return 0;
2133 }
2134
2135 s = zclient->obuf;
2136 stream_reset(s);
2137
2138 zclient_create_header(s, ZEBRA_ADVERTISE_SUBNET, bgp->vrf_id);
2139 stream_putc(s, advertise);
2140 stream_put3(s, vni);
2141 stream_putw_at(s, 0, stream_get_endp(s));
2142
2143 return zclient_send_message(zclient);
2144 }
2145
2146 int bgp_zebra_advertise_svi_macip(struct bgp *bgp, int advertise, vni_t vni)
2147 {
2148 struct stream *s = NULL;
2149
2150 /* Check socket. */
2151 if (!zclient || zclient->sock < 0)
2152 return 0;
2153
2154 /* Don't try to register if Zebra doesn't know of this instance. */
2155 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2156 return 0;
2157
2158 s = zclient->obuf;
2159 stream_reset(s);
2160
2161 zclient_create_header(s, ZEBRA_ADVERTISE_SVI_MACIP, bgp->vrf_id);
2162 stream_putc(s, advertise);
2163 stream_putl(s, vni);
2164 stream_putw_at(s, 0, stream_get_endp(s));
2165
2166 return zclient_send_message(zclient);
2167 }
2168
2169 int bgp_zebra_advertise_gw_macip(struct bgp *bgp, int advertise, vni_t vni)
2170 {
2171 struct stream *s = NULL;
2172
2173 /* Check socket. */
2174 if (!zclient || zclient->sock < 0)
2175 return 0;
2176
2177 /* Don't try to register if Zebra doesn't know of this instance. */
2178 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
2179 if (BGP_DEBUG(zebra, ZEBRA))
2180 zlog_debug(
2181 "%s: No zebra instance to talk to, not installing gw_macip",
2182 __func__);
2183 return 0;
2184 }
2185
2186 s = zclient->obuf;
2187 stream_reset(s);
2188
2189 zclient_create_header(s, ZEBRA_ADVERTISE_DEFAULT_GW, bgp->vrf_id);
2190 stream_putc(s, advertise);
2191 stream_putl(s, vni);
2192 stream_putw_at(s, 0, stream_get_endp(s));
2193
2194 return zclient_send_message(zclient);
2195 }
2196
2197 int bgp_zebra_vxlan_flood_control(struct bgp *bgp,
2198 enum vxlan_flood_control flood_ctrl)
2199 {
2200 struct stream *s;
2201
2202 /* Check socket. */
2203 if (!zclient || zclient->sock < 0)
2204 return 0;
2205
2206 /* Don't try to register if Zebra doesn't know of this instance. */
2207 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
2208 if (BGP_DEBUG(zebra, ZEBRA))
2209 zlog_debug(
2210 "%s: No zebra instance to talk to, not installing all vni",
2211 __func__);
2212 return 0;
2213 }
2214
2215 s = zclient->obuf;
2216 stream_reset(s);
2217
2218 zclient_create_header(s, ZEBRA_VXLAN_FLOOD_CONTROL, bgp->vrf_id);
2219 stream_putc(s, flood_ctrl);
2220 stream_putw_at(s, 0, stream_get_endp(s));
2221
2222 return zclient_send_message(zclient);
2223 }
2224
2225 int bgp_zebra_advertise_all_vni(struct bgp *bgp, int advertise)
2226 {
2227 struct stream *s;
2228
2229 /* Check socket. */
2230 if (!zclient || zclient->sock < 0)
2231 return 0;
2232
2233 /* Don't try to register if Zebra doesn't know of this instance. */
2234 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2235 return 0;
2236
2237 s = zclient->obuf;
2238 stream_reset(s);
2239
2240 zclient_create_header(s, ZEBRA_ADVERTISE_ALL_VNI, bgp->vrf_id);
2241 stream_putc(s, advertise);
2242 /* Also inform current BUM handling setting. This is really
2243 * relevant only when 'advertise' is set.
2244 */
2245 stream_putc(s, bgp->vxlan_flood_ctrl);
2246 stream_putw_at(s, 0, stream_get_endp(s));
2247
2248 return zclient_send_message(zclient);
2249 }
2250
2251 int bgp_zebra_dup_addr_detection(struct bgp *bgp)
2252 {
2253 struct stream *s;
2254
2255 /* Check socket. */
2256 if (!zclient || zclient->sock < 0)
2257 return 0;
2258
2259 /* Don't try to register if Zebra doesn't know of this instance. */
2260 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2261 return 0;
2262
2263 if (BGP_DEBUG(zebra, ZEBRA))
2264 zlog_debug("dup addr detect %s max_moves %u time %u freeze %s freeze_time %u",
2265 bgp->evpn_info->dup_addr_detect ?
2266 "enable" : "disable",
2267 bgp->evpn_info->dad_max_moves,
2268 bgp->evpn_info->dad_time,
2269 bgp->evpn_info->dad_freeze ?
2270 "enable" : "disable",
2271 bgp->evpn_info->dad_freeze_time);
2272
2273 s = zclient->obuf;
2274 stream_reset(s);
2275 zclient_create_header(s, ZEBRA_DUPLICATE_ADDR_DETECTION,
2276 bgp->vrf_id);
2277 stream_putl(s, bgp->evpn_info->dup_addr_detect);
2278 stream_putl(s, bgp->evpn_info->dad_time);
2279 stream_putl(s, bgp->evpn_info->dad_max_moves);
2280 stream_putl(s, bgp->evpn_info->dad_freeze);
2281 stream_putl(s, bgp->evpn_info->dad_freeze_time);
2282 stream_putw_at(s, 0, stream_get_endp(s));
2283
2284 return zclient_send_message(zclient);
2285 }
2286
2287 static int rule_notify_owner(ZAPI_CALLBACK_ARGS)
2288 {
2289 uint32_t seqno, priority, unique;
2290 enum zapi_rule_notify_owner note;
2291 struct bgp_pbr_action *bgp_pbra;
2292 struct bgp_pbr_rule *bgp_pbr = NULL;
2293 char ifname[INTERFACE_NAMSIZ + 1];
2294
2295 if (!zapi_rule_notify_decode(zclient->ibuf, &seqno, &priority, &unique,
2296 ifname, &note))
2297 return -1;
2298
2299 bgp_pbra = bgp_pbr_action_rule_lookup(vrf_id, unique);
2300 if (!bgp_pbra) {
2301 /* look in bgp pbr rule */
2302 bgp_pbr = bgp_pbr_rule_lookup(vrf_id, unique);
2303 if (!bgp_pbr && note != ZAPI_RULE_REMOVED) {
2304 if (BGP_DEBUG(zebra, ZEBRA))
2305 zlog_debug("%s: Fail to look BGP rule (%u)",
2306 __func__, unique);
2307 return 0;
2308 }
2309 }
2310
2311 switch (note) {
2312 case ZAPI_RULE_FAIL_INSTALL:
2313 if (BGP_DEBUG(zebra, ZEBRA))
2314 zlog_debug("%s: Received RULE_FAIL_INSTALL", __func__);
2315 if (bgp_pbra) {
2316 bgp_pbra->installed = false;
2317 bgp_pbra->install_in_progress = false;
2318 } else {
2319 bgp_pbr->installed = false;
2320 bgp_pbr->install_in_progress = false;
2321 }
2322 break;
2323 case ZAPI_RULE_INSTALLED:
2324 if (bgp_pbra) {
2325 bgp_pbra->installed = true;
2326 bgp_pbra->install_in_progress = false;
2327 } else {
2328 struct bgp_path_info *path;
2329 struct bgp_path_info_extra *extra;
2330
2331 bgp_pbr->installed = true;
2332 bgp_pbr->install_in_progress = false;
2333 bgp_pbr->action->refcnt++;
2334 /* link bgp_info to bgp_pbr */
2335 path = (struct bgp_path_info *)bgp_pbr->path;
2336 extra = bgp_path_info_extra_get(path);
2337 listnode_add_force(&extra->bgp_fs_iprule,
2338 bgp_pbr);
2339 }
2340 if (BGP_DEBUG(zebra, ZEBRA))
2341 zlog_debug("%s: Received RULE_INSTALLED", __func__);
2342 break;
2343 case ZAPI_RULE_FAIL_REMOVE:
2344 case ZAPI_RULE_REMOVED:
2345 if (BGP_DEBUG(zebra, ZEBRA))
2346 zlog_debug("%s: Received RULE REMOVED", __func__);
2347 break;
2348 }
2349
2350 return 0;
2351 }
2352
2353 static int ipset_notify_owner(ZAPI_CALLBACK_ARGS)
2354 {
2355 uint32_t unique;
2356 enum zapi_ipset_notify_owner note;
2357 struct bgp_pbr_match *bgp_pbim;
2358
2359 if (!zapi_ipset_notify_decode(zclient->ibuf,
2360 &unique,
2361 &note))
2362 return -1;
2363
2364 bgp_pbim = bgp_pbr_match_ipset_lookup(vrf_id, unique);
2365 if (!bgp_pbim) {
2366 if (BGP_DEBUG(zebra, ZEBRA))
2367 zlog_debug("%s: Fail to look BGP match ( %u, ID %u)",
2368 __func__, note, unique);
2369 return 0;
2370 }
2371
2372 switch (note) {
2373 case ZAPI_IPSET_FAIL_INSTALL:
2374 if (BGP_DEBUG(zebra, ZEBRA))
2375 zlog_debug("%s: Received IPSET_FAIL_INSTALL", __func__);
2376 bgp_pbim->installed = false;
2377 bgp_pbim->install_in_progress = false;
2378 break;
2379 case ZAPI_IPSET_INSTALLED:
2380 bgp_pbim->installed = true;
2381 bgp_pbim->install_in_progress = false;
2382 if (BGP_DEBUG(zebra, ZEBRA))
2383 zlog_debug("%s: Received IPSET_INSTALLED", __func__);
2384 break;
2385 case ZAPI_IPSET_FAIL_REMOVE:
2386 case ZAPI_IPSET_REMOVED:
2387 if (BGP_DEBUG(zebra, ZEBRA))
2388 zlog_debug("%s: Received IPSET REMOVED", __func__);
2389 break;
2390 }
2391
2392 return 0;
2393 }
2394
2395 static int ipset_entry_notify_owner(ZAPI_CALLBACK_ARGS)
2396 {
2397 uint32_t unique;
2398 char ipset_name[ZEBRA_IPSET_NAME_SIZE];
2399 enum zapi_ipset_entry_notify_owner note;
2400 struct bgp_pbr_match_entry *bgp_pbime;
2401
2402 if (!zapi_ipset_entry_notify_decode(
2403 zclient->ibuf,
2404 &unique,
2405 ipset_name,
2406 &note))
2407 return -1;
2408 bgp_pbime = bgp_pbr_match_ipset_entry_lookup(vrf_id,
2409 ipset_name,
2410 unique);
2411 if (!bgp_pbime) {
2412 if (BGP_DEBUG(zebra, ZEBRA))
2413 zlog_debug(
2414 "%s: Fail to look BGP match entry (%u, ID %u)",
2415 __func__, note, unique);
2416 return 0;
2417 }
2418
2419 switch (note) {
2420 case ZAPI_IPSET_ENTRY_FAIL_INSTALL:
2421 if (BGP_DEBUG(zebra, ZEBRA))
2422 zlog_debug("%s: Received IPSET_ENTRY_FAIL_INSTALL",
2423 __func__);
2424 bgp_pbime->installed = false;
2425 bgp_pbime->install_in_progress = false;
2426 break;
2427 case ZAPI_IPSET_ENTRY_INSTALLED:
2428 {
2429 struct bgp_path_info *path;
2430 struct bgp_path_info_extra *extra;
2431
2432 bgp_pbime->installed = true;
2433 bgp_pbime->install_in_progress = false;
2434 if (BGP_DEBUG(zebra, ZEBRA))
2435 zlog_debug("%s: Received IPSET_ENTRY_INSTALLED",
2436 __func__);
2437 /* link bgp_path_info to bpme */
2438 path = (struct bgp_path_info *)bgp_pbime->path;
2439 extra = bgp_path_info_extra_get(path);
2440 listnode_add_force(&extra->bgp_fs_pbr, bgp_pbime);
2441 }
2442 break;
2443 case ZAPI_IPSET_ENTRY_FAIL_REMOVE:
2444 case ZAPI_IPSET_ENTRY_REMOVED:
2445 if (BGP_DEBUG(zebra, ZEBRA))
2446 zlog_debug("%s: Received IPSET_ENTRY_REMOVED",
2447 __func__);
2448 break;
2449 }
2450 return 0;
2451 }
2452
2453 static int iptable_notify_owner(ZAPI_CALLBACK_ARGS)
2454 {
2455 uint32_t unique;
2456 enum zapi_iptable_notify_owner note;
2457 struct bgp_pbr_match *bgpm;
2458
2459 if (!zapi_iptable_notify_decode(
2460 zclient->ibuf,
2461 &unique,
2462 &note))
2463 return -1;
2464 bgpm = bgp_pbr_match_iptable_lookup(vrf_id, unique);
2465 if (!bgpm) {
2466 if (BGP_DEBUG(zebra, ZEBRA))
2467 zlog_debug("%s: Fail to look BGP iptable (%u %u)",
2468 __func__, note, unique);
2469 return 0;
2470 }
2471 switch (note) {
2472 case ZAPI_IPTABLE_FAIL_INSTALL:
2473 if (BGP_DEBUG(zebra, ZEBRA))
2474 zlog_debug("%s: Received IPTABLE_FAIL_INSTALL",
2475 __func__);
2476 bgpm->installed_in_iptable = false;
2477 bgpm->install_iptable_in_progress = false;
2478 break;
2479 case ZAPI_IPTABLE_INSTALLED:
2480 bgpm->installed_in_iptable = true;
2481 bgpm->install_iptable_in_progress = false;
2482 if (BGP_DEBUG(zebra, ZEBRA))
2483 zlog_debug("%s: Received IPTABLE_INSTALLED", __func__);
2484 bgpm->action->refcnt++;
2485 break;
2486 case ZAPI_IPTABLE_FAIL_REMOVE:
2487 case ZAPI_IPTABLE_REMOVED:
2488 if (BGP_DEBUG(zebra, ZEBRA))
2489 zlog_debug("%s: Received IPTABLE REMOVED", __func__);
2490 break;
2491 }
2492 return 0;
2493 }
2494
2495 /* Process route notification messages from RIB */
2496 static int bgp_zebra_route_notify_owner(int command, struct zclient *zclient,
2497 zebra_size_t length, vrf_id_t vrf_id)
2498 {
2499 struct prefix p;
2500 enum zapi_route_notify_owner note;
2501 uint32_t table_id;
2502 afi_t afi;
2503 safi_t safi;
2504 struct bgp_dest *dest;
2505 struct bgp *bgp;
2506 struct bgp_path_info *pi, *new_select;
2507
2508 if (!zapi_route_notify_decode(zclient->ibuf, &p, &table_id, &note,
2509 &afi, &safi)) {
2510 zlog_err("%s : error in msg decode", __func__);
2511 return -1;
2512 }
2513
2514 /* Get the bgp instance */
2515 bgp = bgp_lookup_by_vrf_id(vrf_id);
2516 if (!bgp) {
2517 flog_err(EC_BGP_INVALID_BGP_INSTANCE,
2518 "%s : bgp instance not found vrf %d", __func__,
2519 vrf_id);
2520 return -1;
2521 }
2522
2523 /* Find the bgp route node */
2524 dest = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi, &p,
2525 &bgp->vrf_prd);
2526 if (!dest)
2527 return -1;
2528
2529 switch (note) {
2530 case ZAPI_ROUTE_INSTALLED:
2531 new_select = NULL;
2532 /* Clear the flags so that route can be processed */
2533 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
2534 SET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2535 if (BGP_DEBUG(zebra, ZEBRA))
2536 zlog_debug("route %pRN : INSTALLED", dest);
2537 /* Find the best route */
2538 for (pi = dest->info; pi; pi = pi->next) {
2539 /* Process aggregate route */
2540 bgp_aggregate_increment(bgp, &p, pi, afi, safi);
2541 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2542 new_select = pi;
2543 }
2544 /* Advertise the route */
2545 if (new_select)
2546 group_announce_route(bgp, afi, safi, dest, new_select);
2547 else {
2548 flog_err(EC_BGP_INVALID_ROUTE,
2549 "selected route %pRN not found", dest);
2550
2551 bgp_dest_unlock_node(dest);
2552 return -1;
2553 }
2554 break;
2555 case ZAPI_ROUTE_REMOVED:
2556 /* Route deleted from dataplane, reset the installed flag
2557 * so that route can be reinstalled when client sends
2558 * route add later
2559 */
2560 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2561 if (BGP_DEBUG(zebra, ZEBRA))
2562 zlog_debug("route %pRN: Removed from Fib", dest);
2563 break;
2564 case ZAPI_ROUTE_FAIL_INSTALL:
2565 new_select = NULL;
2566 if (BGP_DEBUG(zebra, ZEBRA))
2567 zlog_debug("route: %pRN Failed to Install into Fib",
2568 dest);
2569 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
2570 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2571 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
2572 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2573 new_select = pi;
2574 }
2575 if (new_select)
2576 group_announce_route(bgp, afi, safi, dest, new_select);
2577 /* Error will be logged by zebra module */
2578 break;
2579 case ZAPI_ROUTE_BETTER_ADMIN_WON:
2580 if (BGP_DEBUG(zebra, ZEBRA))
2581 zlog_debug("route: %pRN removed due to better admin won",
2582 dest);
2583 new_select = NULL;
2584 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
2585 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2586 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
2587 bgp_aggregate_decrement(bgp, &p, pi, afi, safi);
2588 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2589 new_select = pi;
2590 }
2591 if (new_select)
2592 group_announce_route(bgp, afi, safi, dest, new_select);
2593 /* No action required */
2594 break;
2595 case ZAPI_ROUTE_REMOVE_FAIL:
2596 zlog_warn("%s: Route %pRN failure to remove",
2597 __func__, dest);
2598 break;
2599 }
2600
2601 bgp_dest_unlock_node(dest);
2602 return 0;
2603 }
2604
2605 /* this function is used to forge ip rule,
2606 * - either for iptable/ipset using fwmark id
2607 * - or for sample ip rule cmd
2608 */
2609 static void bgp_encode_pbr_rule_action(struct stream *s,
2610 struct bgp_pbr_action *pbra,
2611 struct bgp_pbr_rule *pbr)
2612 {
2613 struct prefix pfx;
2614 uint8_t fam = AF_INET;
2615 char ifname[INTERFACE_NAMSIZ];
2616
2617 if (pbra->nh.type == NEXTHOP_TYPE_IPV6)
2618 fam = AF_INET6;
2619 stream_putl(s, 0); /* seqno unused */
2620 if (pbr)
2621 stream_putl(s, pbr->priority);
2622 else
2623 stream_putl(s, 0);
2624 /* ruleno unused - priority change
2625 * ruleno permits distinguishing various FS PBR entries
2626 * - FS PBR entries based on ipset/iptables
2627 * - FS PBR entries based on iprule
2628 * the latter may contain default routing information injected by FS
2629 */
2630 if (pbr)
2631 stream_putl(s, pbr->unique);
2632 else
2633 stream_putl(s, pbra->unique);
2634 stream_putc(s, 0); /* ip protocol being used */
2635 if (pbr && pbr->flags & MATCH_IP_SRC_SET)
2636 memcpy(&pfx, &(pbr->src), sizeof(struct prefix));
2637 else {
2638 memset(&pfx, 0, sizeof(pfx));
2639 pfx.family = fam;
2640 }
2641 stream_putc(s, pfx.family);
2642 stream_putc(s, pfx.prefixlen);
2643 stream_put(s, &pfx.u.prefix, prefix_blen(&pfx));
2644
2645 stream_putw(s, 0); /* src port */
2646
2647 if (pbr && pbr->flags & MATCH_IP_DST_SET)
2648 memcpy(&pfx, &(pbr->dst), sizeof(struct prefix));
2649 else {
2650 memset(&pfx, 0, sizeof(pfx));
2651 pfx.family = fam;
2652 }
2653 stream_putc(s, pfx.family);
2654 stream_putc(s, pfx.prefixlen);
2655 stream_put(s, &pfx.u.prefix, prefix_blen(&pfx));
2656
2657 stream_putw(s, 0); /* dst port */
2658 stream_putc(s, 0); /* dsfield */
2659 /* if pbr present, fwmark is not used */
2660 if (pbr)
2661 stream_putl(s, 0);
2662 else
2663 stream_putl(s, pbra->fwmark); /* fwmark */
2664
2665 stream_putl(s, pbra->table_id);
2666
2667 memset(ifname, 0, sizeof(ifname));
2668 stream_put(s, ifname, INTERFACE_NAMSIZ); /* ifname unused */
2669 }
2670
2671 static void bgp_encode_pbr_ipset_match(struct stream *s,
2672 struct bgp_pbr_match *pbim)
2673 {
2674 stream_putl(s, pbim->unique);
2675 stream_putl(s, pbim->type);
2676 stream_putc(s, pbim->family);
2677 stream_put(s, pbim->ipset_name,
2678 ZEBRA_IPSET_NAME_SIZE);
2679 }
2680
2681 static void bgp_encode_pbr_ipset_entry_match(struct stream *s,
2682 struct bgp_pbr_match_entry *pbime)
2683 {
2684 stream_putl(s, pbime->unique);
2685 /* check that back pointer is not null */
2686 stream_put(s, pbime->backpointer->ipset_name,
2687 ZEBRA_IPSET_NAME_SIZE);
2688
2689 stream_putc(s, pbime->src.family);
2690 stream_putc(s, pbime->src.prefixlen);
2691 stream_put(s, &pbime->src.u.prefix, prefix_blen(&pbime->src));
2692
2693 stream_putc(s, pbime->dst.family);
2694 stream_putc(s, pbime->dst.prefixlen);
2695 stream_put(s, &pbime->dst.u.prefix, prefix_blen(&pbime->dst));
2696
2697 stream_putw(s, pbime->src_port_min);
2698 stream_putw(s, pbime->src_port_max);
2699 stream_putw(s, pbime->dst_port_min);
2700 stream_putw(s, pbime->dst_port_max);
2701 stream_putc(s, pbime->proto);
2702 }
2703
2704 static void bgp_encode_pbr_iptable_match(struct stream *s,
2705 struct bgp_pbr_action *bpa,
2706 struct bgp_pbr_match *pbm)
2707 {
2708 stream_putl(s, pbm->unique2);
2709
2710 stream_putl(s, pbm->type);
2711
2712 stream_putl(s, pbm->flags);
2713
2714 /* TODO: correlate with what is contained
2715 * into bgp_pbr_action.
2716 * currently only forward supported
2717 */
2718 if (bpa->nh.type == NEXTHOP_TYPE_BLACKHOLE)
2719 stream_putl(s, ZEBRA_IPTABLES_DROP);
2720 else
2721 stream_putl(s, ZEBRA_IPTABLES_FORWARD);
2722 stream_putl(s, bpa->fwmark);
2723 stream_put(s, pbm->ipset_name,
2724 ZEBRA_IPSET_NAME_SIZE);
2725 stream_putc(s, pbm->family);
2726 stream_putw(s, pbm->pkt_len_min);
2727 stream_putw(s, pbm->pkt_len_max);
2728 stream_putw(s, pbm->tcp_flags);
2729 stream_putw(s, pbm->tcp_mask_flags);
2730 stream_putc(s, pbm->dscp_value);
2731 stream_putc(s, pbm->fragment);
2732 stream_putc(s, pbm->protocol);
2733 stream_putw(s, pbm->flow_label);
2734 }
2735
2736 /* BGP has established connection with Zebra. */
2737 static void bgp_zebra_connected(struct zclient *zclient)
2738 {
2739 struct bgp *bgp;
2740
2741 zclient_num_connects++; /* increment even if not responding */
2742
2743 /* Send the client registration */
2744 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT);
2745
2746 /* At this point, we may or may not have BGP instances configured, but
2747 * we're only interested in the default VRF (others wouldn't have learnt
2748 * the VRF from Zebra yet.)
2749 */
2750 bgp = bgp_get_default();
2751 if (!bgp)
2752 return;
2753
2754 bgp_zebra_instance_register(bgp);
2755
2756 /* tell label pool that zebra is connected */
2757 bgp_lp_event_zebra_up();
2758
2759 /* TODO - What if we have peers and networks configured, do we have to
2760 * kick-start them?
2761 */
2762 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp, bgp->peer);
2763 }
2764
2765 static int bgp_zebra_process_local_es_add(ZAPI_CALLBACK_ARGS)
2766 {
2767 esi_t esi;
2768 struct bgp *bgp = NULL;
2769 struct stream *s = NULL;
2770 char buf[ESI_STR_LEN];
2771 struct in_addr originator_ip;
2772 uint8_t active;
2773 uint8_t bypass;
2774 uint16_t df_pref;
2775
2776 bgp = bgp_lookup_by_vrf_id(vrf_id);
2777 if (!bgp)
2778 return 0;
2779
2780 s = zclient->ibuf;
2781 stream_get(&esi, s, sizeof(esi_t));
2782 originator_ip.s_addr = stream_get_ipv4(s);
2783 active = stream_getc(s);
2784 df_pref = stream_getw(s);
2785 bypass = stream_getc(s);
2786
2787 if (BGP_DEBUG(zebra, ZEBRA))
2788 zlog_debug(
2789 "Rx add ESI %s originator-ip %pI4 active %u df_pref %u %s",
2790 esi_to_str(&esi, buf, sizeof(buf)), &originator_ip,
2791 active, df_pref, bypass ? "bypass" : "");
2792
2793 frrtrace(5, frr_bgp, evpn_mh_local_es_add_zrecv, &esi, originator_ip,
2794 active, bypass, df_pref);
2795
2796 bgp_evpn_local_es_add(bgp, &esi, originator_ip, active, df_pref,
2797 !!bypass);
2798
2799 return 0;
2800 }
2801
2802 static int bgp_zebra_process_local_es_del(ZAPI_CALLBACK_ARGS)
2803 {
2804 esi_t esi;
2805 struct bgp *bgp = NULL;
2806 struct stream *s = NULL;
2807 char buf[ESI_STR_LEN];
2808
2809 memset(&esi, 0, sizeof(esi_t));
2810 bgp = bgp_lookup_by_vrf_id(vrf_id);
2811 if (!bgp)
2812 return 0;
2813
2814 s = zclient->ibuf;
2815 stream_get(&esi, s, sizeof(esi_t));
2816
2817 if (BGP_DEBUG(zebra, ZEBRA))
2818 zlog_debug("Rx del ESI %s",
2819 esi_to_str(&esi, buf, sizeof(buf)));
2820
2821 frrtrace(1, frr_bgp, evpn_mh_local_es_del_zrecv, &esi);
2822
2823 bgp_evpn_local_es_del(bgp, &esi);
2824
2825 return 0;
2826 }
2827
2828 static int bgp_zebra_process_local_es_evi(ZAPI_CALLBACK_ARGS)
2829 {
2830 esi_t esi;
2831 vni_t vni;
2832 struct bgp *bgp;
2833 struct stream *s;
2834 char buf[ESI_STR_LEN];
2835
2836 bgp = bgp_lookup_by_vrf_id(vrf_id);
2837 if (!bgp)
2838 return 0;
2839
2840 s = zclient->ibuf;
2841 stream_get(&esi, s, sizeof(esi_t));
2842 vni = stream_getl(s);
2843
2844 if (BGP_DEBUG(zebra, ZEBRA))
2845 zlog_debug("Rx %s ESI %s VNI %u",
2846 (cmd == ZEBRA_VNI_ADD) ? "add" : "del",
2847 esi_to_str(&esi, buf, sizeof(buf)), vni);
2848
2849 if (cmd == ZEBRA_LOCAL_ES_EVI_ADD) {
2850 frrtrace(2, frr_bgp, evpn_mh_local_es_evi_add_zrecv, &esi, vni);
2851
2852 bgp_evpn_local_es_evi_add(bgp, &esi, vni);
2853 } else {
2854 frrtrace(2, frr_bgp, evpn_mh_local_es_evi_del_zrecv, &esi, vni);
2855
2856 bgp_evpn_local_es_evi_del(bgp, &esi, vni);
2857 }
2858
2859 return 0;
2860 }
2861
2862 static int bgp_zebra_process_local_l3vni(ZAPI_CALLBACK_ARGS)
2863 {
2864 int filter = 0;
2865 vni_t l3vni = 0;
2866 struct ethaddr svi_rmac, vrr_rmac = {.octet = {0} };
2867 struct in_addr originator_ip;
2868 struct stream *s;
2869 ifindex_t svi_ifindex;
2870 bool is_anycast_mac = false;
2871
2872 memset(&svi_rmac, 0, sizeof(struct ethaddr));
2873 memset(&originator_ip, 0, sizeof(struct in_addr));
2874 s = zclient->ibuf;
2875 l3vni = stream_getl(s);
2876 if (cmd == ZEBRA_L3VNI_ADD) {
2877 stream_get(&svi_rmac, s, sizeof(struct ethaddr));
2878 originator_ip.s_addr = stream_get_ipv4(s);
2879 stream_get(&filter, s, sizeof(int));
2880 svi_ifindex = stream_getl(s);
2881 stream_get(&vrr_rmac, s, sizeof(struct ethaddr));
2882 is_anycast_mac = stream_getl(s);
2883
2884 if (BGP_DEBUG(zebra, ZEBRA))
2885 zlog_debug(
2886 "Rx L3-VNI ADD VRF %s VNI %u RMAC svi-mac %pEA vrr-mac %pEA filter %s svi-if %u",
2887 vrf_id_to_name(vrf_id), l3vni, &svi_rmac,
2888 &vrr_rmac,
2889 filter ? "prefix-routes-only" : "none",
2890 svi_ifindex);
2891
2892 frrtrace(8, frr_bgp, evpn_local_l3vni_add_zrecv, l3vni, vrf_id,
2893 &svi_rmac, &vrr_rmac, filter, originator_ip,
2894 svi_ifindex, is_anycast_mac);
2895
2896 bgp_evpn_local_l3vni_add(l3vni, vrf_id, &svi_rmac, &vrr_rmac,
2897 originator_ip, filter, svi_ifindex,
2898 is_anycast_mac);
2899 } else {
2900 if (BGP_DEBUG(zebra, ZEBRA))
2901 zlog_debug("Rx L3-VNI DEL VRF %s VNI %u",
2902 vrf_id_to_name(vrf_id), l3vni);
2903
2904 frrtrace(2, frr_bgp, evpn_local_l3vni_del_zrecv, l3vni, vrf_id);
2905
2906 bgp_evpn_local_l3vni_del(l3vni, vrf_id);
2907 }
2908
2909 return 0;
2910 }
2911
2912 static int bgp_zebra_process_local_vni(ZAPI_CALLBACK_ARGS)
2913 {
2914 struct stream *s;
2915 vni_t vni;
2916 struct bgp *bgp;
2917 struct in_addr vtep_ip = {INADDR_ANY};
2918 vrf_id_t tenant_vrf_id = VRF_DEFAULT;
2919 struct in_addr mcast_grp = {INADDR_ANY};
2920 ifindex_t svi_ifindex = 0;
2921
2922 s = zclient->ibuf;
2923 vni = stream_getl(s);
2924 if (cmd == ZEBRA_VNI_ADD) {
2925 vtep_ip.s_addr = stream_get_ipv4(s);
2926 stream_get(&tenant_vrf_id, s, sizeof(vrf_id_t));
2927 mcast_grp.s_addr = stream_get_ipv4(s);
2928 stream_get(&svi_ifindex, s, sizeof(ifindex_t));
2929 }
2930
2931 bgp = bgp_lookup_by_vrf_id(vrf_id);
2932 if (!bgp)
2933 return 0;
2934
2935 if (BGP_DEBUG(zebra, ZEBRA))
2936 zlog_debug(
2937 "Rx VNI %s VRF %s VNI %u tenant-vrf %s SVI ifindex %u",
2938 (cmd == ZEBRA_VNI_ADD) ? "add" : "del",
2939 vrf_id_to_name(vrf_id), vni,
2940 vrf_id_to_name(tenant_vrf_id), svi_ifindex);
2941
2942 if (cmd == ZEBRA_VNI_ADD) {
2943 frrtrace(4, frr_bgp, evpn_local_vni_add_zrecv, vni, vtep_ip,
2944 tenant_vrf_id, mcast_grp);
2945
2946 return bgp_evpn_local_vni_add(
2947 bgp, vni,
2948 vtep_ip.s_addr != INADDR_ANY ? vtep_ip : bgp->router_id,
2949 tenant_vrf_id, mcast_grp, svi_ifindex);
2950 } else {
2951 frrtrace(1, frr_bgp, evpn_local_vni_del_zrecv, vni);
2952
2953 return bgp_evpn_local_vni_del(bgp, vni);
2954 }
2955 }
2956
2957 static int bgp_zebra_process_local_macip(ZAPI_CALLBACK_ARGS)
2958 {
2959 struct stream *s;
2960 vni_t vni;
2961 struct bgp *bgp;
2962 struct ethaddr mac;
2963 struct ipaddr ip;
2964 int ipa_len;
2965 uint8_t flags = 0;
2966 uint32_t seqnum = 0;
2967 int state = 0;
2968 char buf2[ESI_STR_LEN];
2969 esi_t esi;
2970
2971 memset(&ip, 0, sizeof(ip));
2972 s = zclient->ibuf;
2973 vni = stream_getl(s);
2974 stream_get(&mac.octet, s, ETH_ALEN);
2975 ipa_len = stream_getl(s);
2976 if (ipa_len != 0 && ipa_len != IPV4_MAX_BYTELEN
2977 && ipa_len != IPV6_MAX_BYTELEN) {
2978 flog_err(EC_BGP_MACIP_LEN,
2979 "%u:Recv MACIP %s with invalid IP addr length %d",
2980 vrf_id, (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del",
2981 ipa_len);
2982 return -1;
2983 }
2984
2985 if (ipa_len) {
2986 ip.ipa_type =
2987 (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4 : IPADDR_V6;
2988 stream_get(&ip.ip.addr, s, ipa_len);
2989 }
2990 if (cmd == ZEBRA_MACIP_ADD) {
2991 flags = stream_getc(s);
2992 seqnum = stream_getl(s);
2993 stream_get(&esi, s, sizeof(esi_t));
2994 } else {
2995 state = stream_getl(s);
2996 memset(&esi, 0, sizeof(esi_t));
2997 }
2998
2999 bgp = bgp_lookup_by_vrf_id(vrf_id);
3000 if (!bgp)
3001 return 0;
3002
3003 if (BGP_DEBUG(zebra, ZEBRA))
3004 zlog_debug(
3005 "%u:Recv MACIP %s f 0x%x MAC %pEA IP %pIA VNI %u seq %u state %d ESI %s",
3006 vrf_id, (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del", flags,
3007 &mac, &ip, vni, seqnum, state,
3008 esi_to_str(&esi, buf2, sizeof(buf2)));
3009
3010 if (cmd == ZEBRA_MACIP_ADD) {
3011 frrtrace(6, frr_bgp, evpn_local_macip_add_zrecv, vni, &mac, &ip,
3012 flags, seqnum, &esi);
3013
3014 return bgp_evpn_local_macip_add(bgp, vni, &mac, &ip,
3015 flags, seqnum, &esi);
3016 } else {
3017 frrtrace(4, frr_bgp, evpn_local_macip_del_zrecv, vni, &mac, &ip,
3018 state);
3019
3020 return bgp_evpn_local_macip_del(bgp, vni, &mac, &ip, state);
3021 }
3022 }
3023
3024 static int bgp_zebra_process_local_ip_prefix(ZAPI_CALLBACK_ARGS)
3025 {
3026 struct stream *s = NULL;
3027 struct bgp *bgp_vrf = NULL;
3028 struct prefix p;
3029
3030 memset(&p, 0, sizeof(struct prefix));
3031 s = zclient->ibuf;
3032 stream_get(&p, s, sizeof(struct prefix));
3033
3034 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
3035 if (!bgp_vrf)
3036 return 0;
3037
3038 if (BGP_DEBUG(zebra, ZEBRA))
3039 zlog_debug("Recv prefix %pFX %s on vrf %s", &p,
3040 (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) ? "ADD" : "DEL",
3041 vrf_id_to_name(vrf_id));
3042
3043 if (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) {
3044
3045 if (p.family == AF_INET)
3046 bgp_evpn_advertise_type5_route(bgp_vrf, &p, NULL,
3047 AFI_IP, SAFI_UNICAST);
3048 else
3049 bgp_evpn_advertise_type5_route(bgp_vrf, &p, NULL,
3050 AFI_IP6, SAFI_UNICAST);
3051
3052 } else {
3053 if (p.family == AF_INET)
3054 bgp_evpn_withdraw_type5_route(bgp_vrf, &p, AFI_IP,
3055 SAFI_UNICAST);
3056 else
3057 bgp_evpn_withdraw_type5_route(bgp_vrf, &p, AFI_IP6,
3058 SAFI_UNICAST);
3059 }
3060 return 0;
3061 }
3062
3063 static int bgp_zebra_process_label_chunk(ZAPI_CALLBACK_ARGS)
3064 {
3065 struct stream *s = NULL;
3066 uint8_t response_keep;
3067 uint32_t first;
3068 uint32_t last;
3069 uint8_t proto;
3070 unsigned short instance;
3071
3072 s = zclient->ibuf;
3073 STREAM_GETC(s, proto);
3074 STREAM_GETW(s, instance);
3075 STREAM_GETC(s, response_keep);
3076 STREAM_GETL(s, first);
3077 STREAM_GETL(s, last);
3078
3079 if (zclient->redist_default != proto) {
3080 flog_err(EC_BGP_LM_ERROR, "Got LM msg with wrong proto %u",
3081 proto);
3082 return 0;
3083 }
3084 if (zclient->instance != instance) {
3085 flog_err(EC_BGP_LM_ERROR, "Got LM msg with wrong instance %u",
3086 proto);
3087 return 0;
3088 }
3089
3090 if (first > last ||
3091 first < MPLS_LABEL_UNRESERVED_MIN ||
3092 last > MPLS_LABEL_UNRESERVED_MAX) {
3093
3094 flog_err(EC_BGP_LM_ERROR, "%s: Invalid Label chunk: %u - %u",
3095 __func__, first, last);
3096 return 0;
3097 }
3098 if (BGP_DEBUG(zebra, ZEBRA)) {
3099 zlog_debug("Label Chunk assign: %u - %u (%u) ",
3100 first, last, response_keep);
3101 }
3102
3103 bgp_lp_event_chunk(response_keep, first, last);
3104
3105 return 0;
3106
3107 stream_failure: /* for STREAM_GETX */
3108 return -1;
3109 }
3110
3111 extern struct zebra_privs_t bgpd_privs;
3112
3113 static int bgp_ifp_create(struct interface *ifp)
3114 {
3115 struct bgp *bgp;
3116
3117 if (BGP_DEBUG(zebra, ZEBRA))
3118 zlog_debug("Rx Intf add VRF %u IF %s", ifp->vrf->vrf_id,
3119 ifp->name);
3120
3121 bgp = ifp->vrf->info;
3122 if (!bgp)
3123 return 0;
3124
3125 bgp_mac_add_mac_entry(ifp);
3126
3127 bgp_update_interface_nbrs(bgp, ifp, ifp);
3128 hook_call(bgp_vrf_status_changed, bgp, ifp);
3129 return 0;
3130 }
3131
3132 static int bgp_zebra_process_srv6_locator_chunk(ZAPI_CALLBACK_ARGS)
3133 {
3134 struct stream *s = NULL;
3135 struct bgp *bgp = bgp_get_default();
3136 struct listnode *node;
3137 struct prefix_ipv6 *c;
3138 struct srv6_locator_chunk s6c = {};
3139 struct prefix_ipv6 *chunk = NULL;
3140
3141 s = zclient->ibuf;
3142 zapi_srv6_locator_chunk_decode(s, &s6c);
3143
3144 if (strcmp(bgp->srv6_locator_name, s6c.locator_name) != 0) {
3145 zlog_err("%s: Locator name unmatch %s:%s", __func__,
3146 bgp->srv6_locator_name, s6c.locator_name);
3147 return 0;
3148 }
3149
3150 for (ALL_LIST_ELEMENTS_RO(bgp->srv6_locator_chunks, node, c)) {
3151 if (!prefix_cmp(c, &s6c.prefix))
3152 return 0;
3153 }
3154
3155 chunk = prefix_ipv6_new();
3156 *chunk = s6c.prefix;
3157 listnode_add(bgp->srv6_locator_chunks, chunk);
3158 vpn_leak_postchange_all();
3159 return 0;
3160 }
3161
3162 static int bgp_zebra_process_srv6_locator_add(ZAPI_CALLBACK_ARGS)
3163 {
3164 struct srv6_locator loc = {};
3165 struct bgp *bgp = bgp_get_default();
3166 const char *loc_name = bgp->srv6_locator_name;
3167
3168 if (zapi_srv6_locator_decode(zclient->ibuf, &loc) < 0)
3169 return -1;
3170
3171 if (!bgp || !bgp->srv6_enabled)
3172 return 0;
3173
3174 if (bgp_zebra_srv6_manager_get_locator_chunk(loc_name) < 0)
3175 return -1;
3176
3177 return 0;
3178 }
3179
3180 static int bgp_zebra_process_srv6_locator_delete(ZAPI_CALLBACK_ARGS)
3181 {
3182 struct srv6_locator loc = {};
3183 struct bgp *bgp = bgp_get_default();
3184 struct listnode *node, *nnode;
3185 struct prefix_ipv6 *chunk;
3186 struct bgp_srv6_function *func;
3187 struct bgp *bgp_vrf;
3188 struct in6_addr *tovpn_sid;
3189 struct prefix_ipv6 tmp_prefi;
3190
3191 if (zapi_srv6_locator_decode(zclient->ibuf, &loc) < 0)
3192 return -1;
3193
3194 // refresh chunks
3195 for (ALL_LIST_ELEMENTS(bgp->srv6_locator_chunks, node, nnode, chunk))
3196 if (prefix_match((struct prefix *)&loc.prefix,
3197 (struct prefix *)chunk))
3198 listnode_delete(bgp->srv6_locator_chunks, chunk);
3199
3200 // refresh functions
3201 for (ALL_LIST_ELEMENTS(bgp->srv6_functions, node, nnode, func)) {
3202 tmp_prefi.family = AF_INET6;
3203 tmp_prefi.prefixlen = 128;
3204 tmp_prefi.prefix = func->sid;
3205 if (prefix_match((struct prefix *)&loc.prefix,
3206 (struct prefix *)&tmp_prefi))
3207 listnode_delete(bgp->srv6_functions, func);
3208 }
3209
3210 // refresh tovpn_sid
3211 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
3212 if (bgp_vrf->inst_type != BGP_INSTANCE_TYPE_VRF)
3213 continue;
3214
3215 // refresh vpnv4 tovpn_sid
3216 tovpn_sid = bgp_vrf->vpn_policy[AFI_IP].tovpn_sid;
3217 if (tovpn_sid) {
3218 tmp_prefi.family = AF_INET6;
3219 tmp_prefi.prefixlen = 128;
3220 tmp_prefi.prefix = *tovpn_sid;
3221 if (prefix_match((struct prefix *)&loc.prefix,
3222 (struct prefix *)&tmp_prefi))
3223 XFREE(MTYPE_BGP_SRV6_SID,
3224 bgp_vrf->vpn_policy[AFI_IP].tovpn_sid);
3225 }
3226
3227 // refresh vpnv6 tovpn_sid
3228 tovpn_sid = bgp_vrf->vpn_policy[AFI_IP6].tovpn_sid;
3229 if (tovpn_sid) {
3230 tmp_prefi.family = AF_INET6;
3231 tmp_prefi.prefixlen = 128;
3232 tmp_prefi.prefix = *tovpn_sid;
3233 if (prefix_match((struct prefix *)&loc.prefix,
3234 (struct prefix *)&tmp_prefi))
3235 XFREE(MTYPE_BGP_SRV6_SID,
3236 bgp_vrf->vpn_policy[AFI_IP6].tovpn_sid);
3237 }
3238 }
3239
3240 vpn_leak_postchange_all();
3241 return 0;
3242 }
3243
3244 static zclient_handler *const bgp_handlers[] = {
3245 [ZEBRA_ROUTER_ID_UPDATE] = bgp_router_id_update,
3246 [ZEBRA_INTERFACE_ADDRESS_ADD] = bgp_interface_address_add,
3247 [ZEBRA_INTERFACE_ADDRESS_DELETE] = bgp_interface_address_delete,
3248 [ZEBRA_INTERFACE_NBR_ADDRESS_ADD] = bgp_interface_nbr_address_add,
3249 [ZEBRA_INTERFACE_NBR_ADDRESS_DELETE] = bgp_interface_nbr_address_delete,
3250 [ZEBRA_INTERFACE_VRF_UPDATE] = bgp_interface_vrf_update,
3251 [ZEBRA_REDISTRIBUTE_ROUTE_ADD] = zebra_read_route,
3252 [ZEBRA_REDISTRIBUTE_ROUTE_DEL] = zebra_read_route,
3253 [ZEBRA_NEXTHOP_UPDATE] = bgp_read_nexthop_update,
3254 [ZEBRA_FEC_UPDATE] = bgp_read_fec_update,
3255 [ZEBRA_LOCAL_ES_ADD] = bgp_zebra_process_local_es_add,
3256 [ZEBRA_LOCAL_ES_DEL] = bgp_zebra_process_local_es_del,
3257 [ZEBRA_VNI_ADD] = bgp_zebra_process_local_vni,
3258 [ZEBRA_LOCAL_ES_EVI_ADD] = bgp_zebra_process_local_es_evi,
3259 [ZEBRA_LOCAL_ES_EVI_DEL] = bgp_zebra_process_local_es_evi,
3260 [ZEBRA_VNI_DEL] = bgp_zebra_process_local_vni,
3261 [ZEBRA_MACIP_ADD] = bgp_zebra_process_local_macip,
3262 [ZEBRA_MACIP_DEL] = bgp_zebra_process_local_macip,
3263 [ZEBRA_L3VNI_ADD] = bgp_zebra_process_local_l3vni,
3264 [ZEBRA_L3VNI_DEL] = bgp_zebra_process_local_l3vni,
3265 [ZEBRA_IP_PREFIX_ROUTE_ADD] = bgp_zebra_process_local_ip_prefix,
3266 [ZEBRA_IP_PREFIX_ROUTE_DEL] = bgp_zebra_process_local_ip_prefix,
3267 [ZEBRA_GET_LABEL_CHUNK] = bgp_zebra_process_label_chunk,
3268 [ZEBRA_RULE_NOTIFY_OWNER] = rule_notify_owner,
3269 [ZEBRA_IPSET_NOTIFY_OWNER] = ipset_notify_owner,
3270 [ZEBRA_IPSET_ENTRY_NOTIFY_OWNER] = ipset_entry_notify_owner,
3271 [ZEBRA_IPTABLE_NOTIFY_OWNER] = iptable_notify_owner,
3272 [ZEBRA_ROUTE_NOTIFY_OWNER] = bgp_zebra_route_notify_owner,
3273 [ZEBRA_SRV6_LOCATOR_ADD] = bgp_zebra_process_srv6_locator_add,
3274 [ZEBRA_SRV6_LOCATOR_DELETE] = bgp_zebra_process_srv6_locator_delete,
3275 [ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK] =
3276 bgp_zebra_process_srv6_locator_chunk,
3277 };
3278
3279 void bgp_zebra_init(struct thread_master *master, unsigned short instance)
3280 {
3281 zclient_num_connects = 0;
3282
3283 if_zapi_callbacks(bgp_ifp_create, bgp_ifp_up,
3284 bgp_ifp_down, bgp_ifp_destroy);
3285
3286 /* Set default values. */
3287 zclient = zclient_new(master, &zclient_options_default, bgp_handlers,
3288 array_size(bgp_handlers));
3289 zclient_init(zclient, ZEBRA_ROUTE_BGP, 0, &bgpd_privs);
3290 zclient->zebra_connected = bgp_zebra_connected;
3291 zclient->instance = instance;
3292 }
3293
3294 void bgp_zebra_destroy(void)
3295 {
3296 if (zclient == NULL)
3297 return;
3298 zclient_stop(zclient);
3299 zclient_free(zclient);
3300 zclient = NULL;
3301 }
3302
3303 int bgp_zebra_num_connects(void)
3304 {
3305 return zclient_num_connects;
3306 }
3307
3308 void bgp_send_pbr_rule_action(struct bgp_pbr_action *pbra,
3309 struct bgp_pbr_rule *pbr,
3310 bool install)
3311 {
3312 struct stream *s;
3313
3314 if (pbra->install_in_progress && !pbr)
3315 return;
3316 if (pbr && pbr->install_in_progress)
3317 return;
3318 if (BGP_DEBUG(zebra, ZEBRA)) {
3319 if (pbr)
3320 zlog_debug("%s: table %d (ip rule) %d", __func__,
3321 pbra->table_id, install);
3322 else
3323 zlog_debug("%s: table %d fwmark %d %d", __func__,
3324 pbra->table_id, pbra->fwmark, install);
3325 }
3326 s = zclient->obuf;
3327 stream_reset(s);
3328
3329 zclient_create_header(s,
3330 install ? ZEBRA_RULE_ADD : ZEBRA_RULE_DELETE,
3331 VRF_DEFAULT);
3332 stream_putl(s, 1); /* send one pbr action */
3333
3334 bgp_encode_pbr_rule_action(s, pbra, pbr);
3335
3336 stream_putw_at(s, 0, stream_get_endp(s));
3337 if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE)
3338 && install) {
3339 if (!pbr)
3340 pbra->install_in_progress = true;
3341 else
3342 pbr->install_in_progress = true;
3343 }
3344 }
3345
3346 void bgp_send_pbr_ipset_match(struct bgp_pbr_match *pbrim, bool install)
3347 {
3348 struct stream *s;
3349
3350 if (pbrim->install_in_progress)
3351 return;
3352 if (BGP_DEBUG(zebra, ZEBRA))
3353 zlog_debug("%s: name %s type %d %d, ID %u", __func__,
3354 pbrim->ipset_name, pbrim->type, install,
3355 pbrim->unique);
3356 s = zclient->obuf;
3357 stream_reset(s);
3358
3359 zclient_create_header(s,
3360 install ? ZEBRA_IPSET_CREATE :
3361 ZEBRA_IPSET_DESTROY,
3362 VRF_DEFAULT);
3363
3364 stream_putl(s, 1); /* send one pbr action */
3365
3366 bgp_encode_pbr_ipset_match(s, pbrim);
3367
3368 stream_putw_at(s, 0, stream_get_endp(s));
3369 if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE) && install)
3370 pbrim->install_in_progress = true;
3371 }
3372
3373 void bgp_send_pbr_ipset_entry_match(struct bgp_pbr_match_entry *pbrime,
3374 bool install)
3375 {
3376 struct stream *s;
3377
3378 if (pbrime->install_in_progress)
3379 return;
3380 if (BGP_DEBUG(zebra, ZEBRA))
3381 zlog_debug("%s: name %s %d %d, ID %u", __func__,
3382 pbrime->backpointer->ipset_name, pbrime->unique,
3383 install, pbrime->unique);
3384 s = zclient->obuf;
3385 stream_reset(s);
3386
3387 zclient_create_header(s,
3388 install ? ZEBRA_IPSET_ENTRY_ADD :
3389 ZEBRA_IPSET_ENTRY_DELETE,
3390 VRF_DEFAULT);
3391
3392 stream_putl(s, 1); /* send one pbr action */
3393
3394 bgp_encode_pbr_ipset_entry_match(s, pbrime);
3395
3396 stream_putw_at(s, 0, stream_get_endp(s));
3397 if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE) && install)
3398 pbrime->install_in_progress = true;
3399 }
3400
3401 static void bgp_encode_pbr_interface_list(struct bgp *bgp, struct stream *s,
3402 uint8_t family)
3403 {
3404 struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
3405 struct bgp_pbr_interface_head *head;
3406 struct bgp_pbr_interface *pbr_if;
3407 struct interface *ifp;
3408
3409 if (!bgp_pbr_cfg)
3410 return;
3411 if (family == AF_INET)
3412 head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
3413 else
3414 head = &(bgp_pbr_cfg->ifaces_by_name_ipv6);
3415 RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) {
3416 ifp = if_lookup_by_name(pbr_if->name, bgp->vrf_id);
3417 if (ifp)
3418 stream_putl(s, ifp->ifindex);
3419 }
3420 }
3421
3422 static int bgp_pbr_get_ifnumber(struct bgp *bgp, uint8_t family)
3423 {
3424 struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
3425 struct bgp_pbr_interface_head *head;
3426 struct bgp_pbr_interface *pbr_if;
3427 int cnt = 0;
3428
3429 if (!bgp_pbr_cfg)
3430 return 0;
3431 if (family == AF_INET)
3432 head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
3433 else
3434 head = &(bgp_pbr_cfg->ifaces_by_name_ipv6);
3435 RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) {
3436 if (if_lookup_by_name(pbr_if->name, bgp->vrf_id))
3437 cnt++;
3438 }
3439 return cnt;
3440 }
3441
3442 void bgp_send_pbr_iptable(struct bgp_pbr_action *pba,
3443 struct bgp_pbr_match *pbm,
3444 bool install)
3445 {
3446 struct stream *s;
3447 int ret = 0;
3448 int nb_interface;
3449
3450 if (pbm->install_iptable_in_progress)
3451 return;
3452 if (BGP_DEBUG(zebra, ZEBRA))
3453 zlog_debug("%s: name %s type %d mark %d %d, ID %u", __func__,
3454 pbm->ipset_name, pbm->type, pba->fwmark, install,
3455 pbm->unique2);
3456 s = zclient->obuf;
3457 stream_reset(s);
3458
3459 zclient_create_header(s,
3460 install ? ZEBRA_IPTABLE_ADD :
3461 ZEBRA_IPTABLE_DELETE,
3462 VRF_DEFAULT);
3463
3464 bgp_encode_pbr_iptable_match(s, pba, pbm);
3465 nb_interface = bgp_pbr_get_ifnumber(pba->bgp, pbm->family);
3466 stream_putl(s, nb_interface);
3467 if (nb_interface)
3468 bgp_encode_pbr_interface_list(pba->bgp, s, pbm->family);
3469 stream_putw_at(s, 0, stream_get_endp(s));
3470 ret = zclient_send_message(zclient);
3471 if (install) {
3472 if (ret != ZCLIENT_SEND_FAILURE)
3473 pba->refcnt++;
3474 else
3475 pbm->install_iptable_in_progress = true;
3476 }
3477 }
3478
3479 /* inject in table <table_id> a default route to:
3480 * - if nexthop IP is present : to this nexthop
3481 * - if vrf is different from local : to the matching VRF
3482 */
3483 void bgp_zebra_announce_default(struct bgp *bgp, struct nexthop *nh,
3484 afi_t afi, uint32_t table_id, bool announce)
3485 {
3486 struct zapi_nexthop *api_nh;
3487 struct zapi_route api;
3488 struct prefix p;
3489
3490 if (!nh || (nh->type != NEXTHOP_TYPE_IPV4
3491 && nh->type != NEXTHOP_TYPE_IPV6)
3492 || nh->vrf_id == VRF_UNKNOWN)
3493 return;
3494
3495 /* in vrf-lite, no default route has to be announced
3496 * the table id of vrf is directly used to divert traffic
3497 */
3498 if (!vrf_is_backend_netns() && bgp->vrf_id != nh->vrf_id)
3499 return;
3500
3501 memset(&p, 0, sizeof(struct prefix));
3502 if (afi != AFI_IP && afi != AFI_IP6)
3503 return;
3504 p.family = afi2family(afi);
3505 memset(&api, 0, sizeof(api));
3506 api.vrf_id = bgp->vrf_id;
3507 api.type = ZEBRA_ROUTE_BGP;
3508 api.safi = SAFI_UNICAST;
3509 api.prefix = p;
3510 api.tableid = table_id;
3511 api.nexthop_num = 1;
3512 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
3513 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
3514 api_nh = &api.nexthops[0];
3515
3516 api.distance = ZEBRA_EBGP_DISTANCE_DEFAULT;
3517 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
3518
3519 /* redirect IP */
3520 if (afi == AFI_IP && nh->gate.ipv4.s_addr != INADDR_ANY) {
3521 char buff[PREFIX_STRLEN];
3522
3523 api_nh->vrf_id = nh->vrf_id;
3524 api_nh->gate.ipv4 = nh->gate.ipv4;
3525 api_nh->type = NEXTHOP_TYPE_IPV4;
3526
3527 inet_ntop(AF_INET, &(nh->gate.ipv4), buff, INET_ADDRSTRLEN);
3528 if (BGP_DEBUG(zebra, ZEBRA))
3529 zlog_debug("BGP: %s default route to %s table %d (redirect IP)",
3530 announce ? "adding" : "withdrawing",
3531 buff, table_id);
3532 zclient_route_send(announce ? ZEBRA_ROUTE_ADD
3533 : ZEBRA_ROUTE_DELETE,
3534 zclient, &api);
3535 } else if (afi == AFI_IP6 &&
3536 memcmp(&nh->gate.ipv6,
3537 &in6addr_any, sizeof(struct in6_addr))) {
3538 char buff[PREFIX_STRLEN];
3539
3540 api_nh->vrf_id = nh->vrf_id;
3541 memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6,
3542 sizeof(struct in6_addr));
3543 api_nh->type = NEXTHOP_TYPE_IPV6;
3544
3545 inet_ntop(AF_INET6, &(nh->gate.ipv6), buff, INET_ADDRSTRLEN);
3546 if (BGP_DEBUG(zebra, ZEBRA))
3547 zlog_debug("BGP: %s default route to %s table %d (redirect IP)",
3548 announce ? "adding" : "withdrawing",
3549 buff, table_id);
3550 zclient_route_send(announce ? ZEBRA_ROUTE_ADD
3551 : ZEBRA_ROUTE_DELETE,
3552 zclient, &api);
3553 } else if (nh->vrf_id != bgp->vrf_id) {
3554 struct vrf *vrf;
3555 struct interface *ifp;
3556
3557 vrf = vrf_lookup_by_id(nh->vrf_id);
3558 if (!vrf)
3559 return;
3560 /* create default route with interface <VRF>
3561 * with nexthop-vrf <VRF>
3562 */
3563 ifp = if_lookup_by_name_vrf(vrf->name, vrf);
3564 if (!ifp)
3565 return;
3566 api_nh->vrf_id = nh->vrf_id;
3567 api_nh->type = NEXTHOP_TYPE_IFINDEX;
3568 api_nh->ifindex = ifp->ifindex;
3569 if (BGP_DEBUG(zebra, ZEBRA))
3570 zlog_info("BGP: %s default route to %s table %d (redirect VRF)",
3571 announce ? "adding" : "withdrawing",
3572 vrf->name, table_id);
3573 zclient_route_send(announce ? ZEBRA_ROUTE_ADD
3574 : ZEBRA_ROUTE_DELETE,
3575 zclient, &api);
3576 return;
3577 }
3578 }
3579
3580 /* Send capabilities to RIB */
3581 int bgp_zebra_send_capabilities(struct bgp *bgp, bool disable)
3582 {
3583 struct zapi_cap api;
3584 int ret = BGP_GR_SUCCESS;
3585
3586 if (zclient == NULL) {
3587 if (BGP_DEBUG(zebra, ZEBRA))
3588 zlog_debug("zclient invalid");
3589 return BGP_GR_FAILURE;
3590 }
3591
3592 /* Check if the client is connected */
3593 if ((zclient->sock < 0) || (zclient->t_connect)) {
3594 if (BGP_DEBUG(zebra, ZEBRA))
3595 zlog_debug("client not connected");
3596 return BGP_GR_FAILURE;
3597 }
3598
3599 /* Check if capability is already sent. If the flag force is set
3600 * send the capability since this can be initial bgp configuration
3601 */
3602 memset(&api, 0, sizeof(struct zapi_cap));
3603 if (disable) {
3604 api.cap = ZEBRA_CLIENT_GR_DISABLE;
3605 api.vrf_id = bgp->vrf_id;
3606 } else {
3607 api.cap = ZEBRA_CLIENT_GR_CAPABILITIES;
3608 api.stale_removal_time = bgp->rib_stale_time;
3609 api.vrf_id = bgp->vrf_id;
3610 }
3611
3612 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api)
3613 == ZCLIENT_SEND_FAILURE) {
3614 zlog_err("error sending capability");
3615 ret = BGP_GR_FAILURE;
3616 } else {
3617 if (disable)
3618 bgp->present_zebra_gr_state = ZEBRA_GR_DISABLE;
3619 else
3620 bgp->present_zebra_gr_state = ZEBRA_GR_ENABLE;
3621
3622 if (BGP_DEBUG(zebra, ZEBRA))
3623 zlog_debug("send capabilty success");
3624 ret = BGP_GR_SUCCESS;
3625 }
3626 return ret;
3627 }
3628
3629 /* Send route update pesding or completed status to RIB for the
3630 * specific AFI, SAFI
3631 */
3632 int bgp_zebra_update(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type)
3633 {
3634 struct zapi_cap api = {0};
3635
3636 if (zclient == NULL) {
3637 if (BGP_DEBUG(zebra, ZEBRA))
3638 zlog_debug("zclient == NULL, invalid");
3639 return BGP_GR_FAILURE;
3640 }
3641
3642 /* Check if the client is connected */
3643 if ((zclient->sock < 0) || (zclient->t_connect)) {
3644 if (BGP_DEBUG(zebra, ZEBRA))
3645 zlog_debug("client not connected");
3646 return BGP_GR_FAILURE;
3647 }
3648
3649 api.afi = afi;
3650 api.safi = safi;
3651 api.vrf_id = vrf_id;
3652 api.cap = type;
3653
3654 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api)
3655 == ZCLIENT_SEND_FAILURE) {
3656 if (BGP_DEBUG(zebra, ZEBRA))
3657 zlog_debug("error sending capability");
3658 return BGP_GR_FAILURE;
3659 }
3660 return BGP_GR_SUCCESS;
3661 }
3662
3663
3664 /* Send RIB stale timer update */
3665 int bgp_zebra_stale_timer_update(struct bgp *bgp)
3666 {
3667 struct zapi_cap api;
3668
3669 if (zclient == NULL) {
3670 if (BGP_DEBUG(zebra, ZEBRA))
3671 zlog_debug("zclient invalid");
3672 return BGP_GR_FAILURE;
3673 }
3674
3675 /* Check if the client is connected */
3676 if ((zclient->sock < 0) || (zclient->t_connect)) {
3677 if (BGP_DEBUG(zebra, ZEBRA))
3678 zlog_debug("client not connected");
3679 return BGP_GR_FAILURE;
3680 }
3681
3682 memset(&api, 0, sizeof(struct zapi_cap));
3683 api.cap = ZEBRA_CLIENT_RIB_STALE_TIME;
3684 api.stale_removal_time = bgp->rib_stale_time;
3685 api.vrf_id = bgp->vrf_id;
3686 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api)
3687 == ZCLIENT_SEND_FAILURE) {
3688 if (BGP_DEBUG(zebra, ZEBRA))
3689 zlog_debug("error sending capability");
3690 return BGP_GR_FAILURE;
3691 }
3692 if (BGP_DEBUG(zebra, ZEBRA))
3693 zlog_debug("send capabilty success");
3694 return BGP_GR_SUCCESS;
3695 }
3696
3697 int bgp_zebra_srv6_manager_get_locator_chunk(const char *name)
3698 {
3699 return srv6_manager_get_locator_chunk(zclient, name);
3700 }
3701
3702 int bgp_zebra_srv6_manager_release_locator_chunk(const char *name)
3703 {
3704 return srv6_manager_release_locator_chunk(zclient, name);
3705 }