1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
13 #include "sockunion.h"
21 #include "lib/route_opaque.h"
27 #include "bgpd/bgpd.h"
28 #include "bgpd/bgp_route.h"
29 #include "bgpd/bgp_attr.h"
30 #include "bgpd/bgp_aspath.h"
31 #include "bgpd/bgp_nexthop.h"
32 #include "bgpd/bgp_zebra.h"
33 #include "bgpd/bgp_fsm.h"
34 #include "bgpd/bgp_debug.h"
35 #include "bgpd/bgp_errors.h"
36 #include "bgpd/bgp_mpath.h"
37 #include "bgpd/bgp_nexthop.h"
38 #include "bgpd/bgp_nht.h"
39 #include "bgpd/bgp_bfd.h"
40 #include "bgpd/bgp_label.h"
42 #include "bgpd/rfapi/rfapi_backend.h"
43 #include "bgpd/rfapi/vnc_export_bgp.h"
45 #include "bgpd/bgp_evpn.h"
46 #include "bgpd/bgp_mplsvpn.h"
47 #include "bgpd/bgp_labelpool.h"
48 #include "bgpd/bgp_pbr.h"
49 #include "bgpd/bgp_evpn_private.h"
50 #include "bgpd/bgp_evpn_mh.h"
51 #include "bgpd/bgp_mac.h"
52 #include "bgpd/bgp_trace.h"
53 #include "bgpd/bgp_community.h"
54 #include "bgpd/bgp_lcommunity.h"
56 /* All information about zebra. */
57 struct zclient
*zclient
= NULL
;
59 /* hook to indicate vrf status change for SNMP */
60 DEFINE_HOOK(bgp_vrf_status_changed
, (struct bgp
*bgp
, struct interface
*ifp
),
63 DEFINE_MTYPE_STATIC(BGPD
, BGP_IF_INFO
, "BGP interface context");
65 /* Can we install into zebra? */
66 static inline bool bgp_install_info_to_zebra(struct bgp
*bgp
)
68 if (zclient
->sock
<= 0)
71 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
73 "%s: No zebra instance to talk to, not installing information",
81 int zclient_num_connects
;
83 /* Router-id update message from zebra. */
84 static int bgp_router_id_update(ZAPI_CALLBACK_ARGS
)
86 struct prefix router_id
;
88 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
90 if (BGP_DEBUG(zebra
, ZEBRA
))
91 zlog_debug("Rx Router Id update VRF %u Id %pFX", vrf_id
,
94 bgp_router_id_zebra_bump(vrf_id
, &router_id
);
98 /* Nexthop update message from zebra. */
99 static int bgp_read_nexthop_update(ZAPI_CALLBACK_ARGS
)
101 bgp_parse_nexthop_update(cmd
, vrf_id
);
105 /* Set or clear interface on which unnumbered neighbor is configured. This
106 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
109 static void bgp_update_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
,
110 struct interface
*upd_ifp
)
112 struct listnode
*node
, *nnode
;
115 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
116 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)) {
119 bgp_zebra_initiate_radv(bgp
, peer
);
121 bgp_zebra_terminate_radv(bgp
, peer
);
128 static int bgp_read_fec_update(ZAPI_CALLBACK_ARGS
)
130 bgp_parse_fec_update();
134 static void bgp_start_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
)
136 struct listnode
*node
, *nnode
;
139 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
140 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)
141 && !peer_established(peer
)) {
142 if (peer_active(peer
))
143 BGP_EVENT_ADD(peer
, BGP_Stop
);
144 BGP_EVENT_ADD(peer
, BGP_Start
);
149 static void bgp_nbr_connected_add(struct bgp
*bgp
, struct nbr_connected
*ifc
)
151 struct listnode
*node
;
152 struct connected
*connected
;
153 struct interface
*ifp
;
156 /* Kick-off the FSM for any relevant peers only if there is a
157 * valid local address on the interface.
160 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
161 p
= connected
->address
;
162 if (p
->family
== AF_INET6
163 && IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
169 bgp_start_interface_nbrs(bgp
, ifp
);
172 static void bgp_nbr_connected_delete(struct bgp
*bgp
, struct nbr_connected
*ifc
,
175 struct listnode
*node
, *nnode
;
177 struct interface
*ifp
;
179 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
181 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)) {
182 peer
->last_reset
= PEER_DOWN_NBR_ADDR_DEL
;
183 BGP_EVENT_ADD(peer
, BGP_Stop
);
186 /* Free neighbor also, if we're asked to. */
189 listnode_delete(ifp
->nbr_connected
, ifc
);
190 nbr_connected_free(ifc
);
194 static int bgp_ifp_destroy(struct interface
*ifp
)
198 bgp
= ifp
->vrf
->info
;
200 if (BGP_DEBUG(zebra
, ZEBRA
))
201 zlog_debug("Rx Intf del VRF %u IF %s", ifp
->vrf
->vrf_id
,
205 bgp_update_interface_nbrs(bgp
, ifp
, NULL
);
206 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
209 bgp_mac_del_mac_entry(ifp
);
214 static int bgp_ifp_up(struct interface
*ifp
)
217 struct nbr_connected
*nc
;
218 struct listnode
*node
, *nnode
;
221 bgp
= ifp
->vrf
->info
;
223 bgp_mac_add_mac_entry(ifp
);
225 if (BGP_DEBUG(zebra
, ZEBRA
))
226 zlog_debug("Rx Intf up VRF %u IF %s", ifp
->vrf
->vrf_id
,
232 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
233 bgp_connected_add(bgp
, c
);
235 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
236 bgp_nbr_connected_add(bgp
, nc
);
238 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
244 static int bgp_ifp_down(struct interface
*ifp
)
247 struct nbr_connected
*nc
;
248 struct listnode
*node
, *nnode
;
252 bgp
= ifp
->vrf
->info
;
254 bgp_mac_del_mac_entry(ifp
);
256 if (BGP_DEBUG(zebra
, ZEBRA
))
257 zlog_debug("Rx Intf down VRF %u IF %s", ifp
->vrf
->vrf_id
,
263 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
264 bgp_connected_delete(bgp
, c
);
266 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
267 bgp_nbr_connected_delete(bgp
, nc
, 1);
269 /* Fast external-failover */
270 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
)) {
272 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
273 /* Take down directly connected peers. */
274 if ((peer
->ttl
!= BGP_DEFAULT_TTL
)
275 && (peer
->gtsm_hops
!= BGP_GTSM_HOPS_CONNECTED
))
278 if (ifp
== peer
->nexthop
.ifp
) {
279 BGP_EVENT_ADD(peer
, BGP_Stop
);
280 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
285 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
286 bgp_nht_ifp_down(ifp
);
291 static int bgp_interface_address_add(ZAPI_CALLBACK_ARGS
)
293 struct connected
*ifc
;
297 struct listnode
*node
, *nnode
;
301 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
303 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
308 if (bgp_debug_zebra(ifc
->address
))
309 zlog_debug("Rx Intf address add VRF %u IF %s addr %pFX", vrf_id
,
310 ifc
->ifp
->name
, ifc
->address
);
315 if (if_is_operative(ifc
->ifp
)) {
316 bgp_connected_add(bgp
, ifc
);
318 /* If we have learnt of any neighbors on this interface,
319 * check to kick off any BGP interface-based neighbors,
320 * but only if this is a link-local address.
322 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
)
323 && !list_isempty(ifc
->ifp
->nbr_connected
))
324 bgp_start_interface_nbrs(bgp
, ifc
->ifp
);
328 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
329 if (addr
->family
== AF_INET
)
333 * If the Peer's interface name matches the
334 * interface name for which BGP received the
335 * update and if the received interface address
336 * is a globalV6 and if the peer is currently
337 * using a v4-mapped-v6 addr or a link local
338 * address, then copy the Rxed global v6 addr
339 * into peer's v6_global and send updates out
340 * with new nexthop addr.
342 if ((peer
->conf_if
&&
343 (strcmp(peer
->conf_if
, ifc
->ifp
->name
) ==
345 !IN6_IS_ADDR_LINKLOCAL(&addr
->u
.prefix6
) &&
347 &peer
->nexthop
.v6_global
)) ||
348 IN6_IS_ADDR_LINKLOCAL(
349 &peer
->nexthop
.v6_global
))) {
351 if (bgp_debug_zebra(ifc
->address
)) {
353 "Update peer %pBP's current intf addr %pI6 and send updates",
358 memcpy(&peer
->nexthop
.v6_global
,
361 FOREACH_AFI_SAFI (afi
, safi
)
362 bgp_announce_route(peer
, afi
,
372 static int bgp_interface_address_delete(ZAPI_CALLBACK_ARGS
)
374 struct listnode
*node
, *nnode
;
375 struct connected
*ifc
;
380 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
382 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
387 if (bgp_debug_zebra(ifc
->address
))
388 zlog_debug("Rx Intf address del VRF %u IF %s addr %pFX", vrf_id
,
389 ifc
->ifp
->name
, ifc
->address
);
391 if (bgp
&& if_is_operative(ifc
->ifp
)) {
392 bgp_connected_delete(bgp
, ifc
);
399 * When we are using the v6 global as part of the peering
400 * nexthops and we are removing it, then we need to
401 * clear the peer data saved for that nexthop and
402 * cause a re-announcement of the route. Since
403 * we do not want the peering to bounce.
405 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
409 if (addr
->family
== AF_INET
)
412 if (!IN6_IS_ADDR_LINKLOCAL(&addr
->u
.prefix6
)
413 && memcmp(&peer
->nexthop
.v6_global
,
414 &addr
->u
.prefix6
, 16)
416 memset(&peer
->nexthop
.v6_global
, 0, 16);
417 FOREACH_AFI_SAFI (afi
, safi
)
418 bgp_announce_route(peer
, afi
, safi
,
424 connected_free(&ifc
);
429 static int bgp_interface_nbr_address_add(ZAPI_CALLBACK_ARGS
)
431 struct nbr_connected
*ifc
= NULL
;
434 ifc
= zebra_interface_nbr_address_read(cmd
, zclient
->ibuf
, vrf_id
);
439 if (bgp_debug_zebra(ifc
->address
))
440 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %pFX",
441 vrf_id
, ifc
->ifp
->name
, ifc
->address
);
443 if (if_is_operative(ifc
->ifp
)) {
444 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
446 bgp_nbr_connected_add(bgp
, ifc
);
452 static int bgp_interface_nbr_address_delete(ZAPI_CALLBACK_ARGS
)
454 struct nbr_connected
*ifc
= NULL
;
457 ifc
= zebra_interface_nbr_address_read(cmd
, zclient
->ibuf
, vrf_id
);
462 if (bgp_debug_zebra(ifc
->address
))
463 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %pFX",
464 vrf_id
, ifc
->ifp
->name
, ifc
->address
);
466 if (if_is_operative(ifc
->ifp
)) {
467 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
469 bgp_nbr_connected_delete(bgp
, ifc
, 0);
472 nbr_connected_free(ifc
);
477 /* VRF update for an interface. */
478 static int bgp_interface_vrf_update(ZAPI_CALLBACK_ARGS
)
480 struct interface
*ifp
;
483 struct nbr_connected
*nc
;
484 struct listnode
*node
, *nnode
;
488 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
493 if (BGP_DEBUG(zebra
, ZEBRA
))
494 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id
,
495 ifp
->name
, new_vrf_id
);
497 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
500 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
501 bgp_connected_delete(bgp
, c
);
503 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
504 bgp_nbr_connected_delete(bgp
, nc
, 1);
506 /* Fast external-failover */
507 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
)) {
508 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
509 if ((peer
->ttl
!= BGP_DEFAULT_TTL
)
511 != BGP_GTSM_HOPS_CONNECTED
))
514 if (ifp
== peer
->nexthop
.ifp
)
515 BGP_EVENT_ADD(peer
, BGP_Stop
);
520 if_update_to_new_vrf(ifp
, new_vrf_id
);
522 bgp
= bgp_lookup_by_vrf_id(new_vrf_id
);
526 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
527 bgp_connected_add(bgp
, c
);
529 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
530 bgp_nbr_connected_add(bgp
, nc
);
532 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
536 /* Zebra route add and delete treatment. */
537 static int zebra_read_route(ZAPI_CALLBACK_ARGS
)
539 enum nexthop_types_t nhtype
;
540 enum blackhole_type bhtype
= BLACKHOLE_UNSPEC
;
541 struct zapi_route api
;
542 union g_addr nexthop
= {};
547 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
551 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
554 /* we completely ignore srcdest routes for now. */
555 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
558 /* ignore link-local address. */
559 if (api
.prefix
.family
== AF_INET6
560 && IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
563 ifindex
= api
.nexthops
[0].ifindex
;
564 nhtype
= api
.nexthops
[0].type
;
566 /* api_nh structure has union of gate and bh_type */
567 if (nhtype
== NEXTHOP_TYPE_BLACKHOLE
) {
568 /* bh_type is only applicable if NEXTHOP_TYPE_BLACKHOLE*/
569 bhtype
= api
.nexthops
[0].bh_type
;
571 nexthop
= api
.nexthops
[0].gate
;
573 add
= (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
576 * The ADD message is actually an UPDATE and there is no
578 * for a prior redistributed route, if any. So, perform an
580 * DEL processing for the same redistributed route from any
584 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
586 bgp_redistribute_delete(bgp
, &api
.prefix
, i
,
590 /* Now perform the add/update. */
591 bgp_redistribute_add(bgp
, &api
.prefix
, &nexthop
, ifindex
,
592 nhtype
, bhtype
, api
.distance
, api
.metric
,
593 api
.type
, api
.instance
, api
.tag
);
595 bgp_redistribute_delete(bgp
, &api
.prefix
, api
.type
,
599 if (bgp_debug_zebra(&api
.prefix
)) {
600 char buf
[PREFIX_STRLEN
];
603 inet_ntop(api
.prefix
.family
, &nexthop
, buf
,
606 "Rx route ADD VRF %u %s[%d] %pFX nexthop %s (type %d if %u) metric %u distance %u tag %" ROUTE_TAG_PRI
,
607 vrf_id
, zebra_route_string(api
.type
),
608 api
.instance
, &api
.prefix
, buf
, nhtype
, ifindex
,
609 api
.metric
, api
.distance
, api
.tag
);
611 zlog_debug("Rx route DEL VRF %u %s[%d] %pFX", vrf_id
,
612 zebra_route_string(api
.type
), api
.instance
,
620 struct interface
*if_lookup_by_ipv4(struct in_addr
*addr
, vrf_id_t vrf_id
)
623 struct listnode
*cnode
;
624 struct interface
*ifp
;
625 struct connected
*connected
;
626 struct prefix_ipv4 p
;
629 vrf
= vrf_lookup_by_id(vrf_id
);
635 p
.prefixlen
= IPV4_MAX_BITLEN
;
637 FOR_ALL_INTERFACES (vrf
, ifp
) {
638 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
639 cp
= connected
->address
;
641 if (cp
->family
== AF_INET
)
642 if (prefix_match(cp
, (struct prefix
*)&p
))
649 struct interface
*if_lookup_by_ipv4_exact(struct in_addr
*addr
, vrf_id_t vrf_id
)
652 struct listnode
*cnode
;
653 struct interface
*ifp
;
654 struct connected
*connected
;
657 vrf
= vrf_lookup_by_id(vrf_id
);
661 FOR_ALL_INTERFACES (vrf
, ifp
) {
662 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
663 cp
= connected
->address
;
665 if (cp
->family
== AF_INET
)
666 if (IPV4_ADDR_SAME(&cp
->u
.prefix4
, addr
))
673 struct interface
*if_lookup_by_ipv6(struct in6_addr
*addr
, ifindex_t ifindex
,
677 struct listnode
*cnode
;
678 struct interface
*ifp
;
679 struct connected
*connected
;
680 struct prefix_ipv6 p
;
683 vrf
= vrf_lookup_by_id(vrf_id
);
689 p
.prefixlen
= IPV6_MAX_BITLEN
;
691 FOR_ALL_INTERFACES (vrf
, ifp
) {
692 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
693 cp
= connected
->address
;
695 if (cp
->family
== AF_INET6
)
696 if (prefix_match(cp
, (struct prefix
*)&p
)) {
697 if (IN6_IS_ADDR_LINKLOCAL(
699 if (ifindex
== ifp
->ifindex
)
709 struct interface
*if_lookup_by_ipv6_exact(struct in6_addr
*addr
,
710 ifindex_t ifindex
, vrf_id_t vrf_id
)
713 struct listnode
*cnode
;
714 struct interface
*ifp
;
715 struct connected
*connected
;
718 vrf
= vrf_lookup_by_id(vrf_id
);
722 FOR_ALL_INTERFACES (vrf
, ifp
) {
723 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
724 cp
= connected
->address
;
726 if (cp
->family
== AF_INET6
)
727 if (IPV6_ADDR_SAME(&cp
->u
.prefix6
, addr
)) {
728 if (IN6_IS_ADDR_LINKLOCAL(
730 if (ifindex
== ifp
->ifindex
)
740 static int if_get_ipv6_global(struct interface
*ifp
, struct in6_addr
*addr
)
742 struct listnode
*cnode
;
743 struct connected
*connected
;
746 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
747 cp
= connected
->address
;
749 if (cp
->family
== AF_INET6
)
750 if (!IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
751 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
758 static bool if_get_ipv6_local(struct interface
*ifp
, struct in6_addr
*addr
)
760 struct listnode
*cnode
;
761 struct connected
*connected
;
764 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
765 cp
= connected
->address
;
767 if (cp
->family
== AF_INET6
)
768 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
769 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
776 static int if_get_ipv4_address(struct interface
*ifp
, struct in_addr
*addr
)
778 struct listnode
*cnode
;
779 struct connected
*connected
;
782 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
783 cp
= connected
->address
;
784 if ((cp
->family
== AF_INET
)
785 && !ipv4_martian(&(cp
->u
.prefix4
))) {
786 *addr
= cp
->u
.prefix4
;
794 bool bgp_zebra_nexthop_set(union sockunion
*local
, union sockunion
*remote
,
795 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
798 struct interface
*ifp
= NULL
;
799 bool v6_ll_avail
= true;
801 memset(nexthop
, 0, sizeof(struct bgp_nexthop
));
808 if (local
->sa
.sa_family
== AF_INET
) {
809 nexthop
->v4
= local
->sin
.sin_addr
;
811 ifp
= if_lookup_by_name(peer
->update_if
,
814 ifp
= if_lookup_by_ipv4_exact(&local
->sin
.sin_addr
,
817 if (local
->sa
.sa_family
== AF_INET6
) {
818 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
, IPV6_MAX_BYTELEN
);
819 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
820 if (peer
->conf_if
|| peer
->ifname
)
821 ifp
= if_lookup_by_name(peer
->conf_if
825 else if (peer
->update_if
)
826 ifp
= if_lookup_by_name(peer
->update_if
,
828 } else if (peer
->update_if
)
829 ifp
= if_lookup_by_name(peer
->update_if
,
832 ifp
= if_lookup_by_ipv6_exact(&local
->sin6
.sin6_addr
,
833 local
->sin6
.sin6_scope_id
,
839 * BGP views do not currently get proper data
840 * from zebra( when attached ) to be able to
841 * properly resolve nexthops, so give this
842 * instance type a pass.
844 if (peer
->bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
847 * If we have no interface data but we have established
848 * some connection w/ zebra than something has gone
849 * terribly terribly wrong here, so say this failed
850 * If we do not any zebra connection then not
851 * having a ifp pointer is ok.
853 return zclient_num_connects
? false : true;
858 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
859 if (local
->sa
.sa_family
== AF_INET
) {
861 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
864 /* There is no global nexthop. Use link-local address as
866 * global and link-local nexthop. In this scenario, the
868 * for interop is that the network admin would use a
870 * specify the global IPv6 nexthop.
873 if_get_ipv6_local(ifp
, &nexthop
->v6_global
);
874 memcpy(&nexthop
->v6_local
, &nexthop
->v6_global
,
878 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
881 * If we are a v4 connection and we are not doing unnumbered
882 * not having a v6 LL address is ok
884 if (!v6_ll_avail
&& !peer
->conf_if
)
886 if (if_lookup_by_ipv4(&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
887 peer
->shared_network
= 1;
889 peer
->shared_network
= 0;
892 /* IPv6 connection, fetch and store IPv4 local address if any. */
893 if (local
->sa
.sa_family
== AF_INET6
) {
894 struct interface
*direct
= NULL
;
897 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
898 if (!ret
&& peer
->local_id
.s_addr
!= INADDR_ANY
)
899 nexthop
->v4
= peer
->local_id
;
902 if (!IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
903 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
906 /* If directly connected set link-local address. */
907 direct
= if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
908 remote
->sin6
.sin6_scope_id
,
911 v6_ll_avail
= if_get_ipv6_local(
912 ifp
, &nexthop
->v6_local
);
914 * It's fine to not have a v6 LL when using
915 * update-source loopback/vrf
917 if (!v6_ll_avail
&& if_is_loopback(ifp
))
919 else if (!v6_ll_avail
) {
921 EC_BGP_NO_LL_ADDRESS_AVAILABLE
,
922 "Interface: %s does not have a v6 LL address associated with it, waiting until one is created for it",
926 /* Link-local address. */
928 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
930 /* If there is no global address. Set link-local
932 global. I know this break RFC specification... */
933 /* In this scenario, the expectation for interop is that
935 * network admin would use a route-map to specify the
940 memcpy(&nexthop
->v6_global
,
941 &local
->sin6
.sin6_addr
,
943 /* Always set the link-local address */
944 memcpy(&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
948 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)
949 || if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
950 remote
->sin6
.sin6_scope_id
,
952 peer
->shared_network
= 1;
954 peer
->shared_network
= 0;
957 /* KAME stack specific treatment. */
959 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_global
)
960 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
)) {
961 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
, 0);
963 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_local
)
964 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
)) {
965 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
, 0);
969 /* If we have identified the local interface, there is no error for now.
974 static struct in6_addr
*
975 bgp_path_info_to_ipv6_nexthop(struct bgp_path_info
*path
, ifindex_t
*ifindex
)
977 struct in6_addr
*nexthop
= NULL
;
979 /* Only global address nexthop exists. */
980 if (path
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
981 || path
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV6_GLOBAL
) {
982 nexthop
= &path
->attr
->mp_nexthop_global
;
983 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
984 *ifindex
= path
->attr
->nh_ifindex
;
987 /* If both global and link-local address present. */
988 if (path
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
989 || path
->attr
->mp_nexthop_len
990 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
991 /* Check if route-map is set to prefer global over link-local */
992 if (path
->attr
->mp_nexthop_prefer_global
) {
993 nexthop
= &path
->attr
->mp_nexthop_global
;
994 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
995 *ifindex
= path
->attr
->nh_ifindex
;
997 /* Workaround for Cisco's nexthop bug. */
998 if (IN6_IS_ADDR_UNSPECIFIED(
999 &path
->attr
->mp_nexthop_global
)
1000 && path
->peer
->su_remote
1001 && path
->peer
->su_remote
->sa
.sa_family
1004 &path
->peer
->su_remote
->sin6
.sin6_addr
;
1005 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
1006 *ifindex
= path
->peer
->nexthop
.ifp
1009 nexthop
= &path
->attr
->mp_nexthop_local
;
1010 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
1011 *ifindex
= path
->attr
->nh_lla_ifindex
;
1019 static bool bgp_table_map_apply(struct route_map
*map
, const struct prefix
*p
,
1020 struct bgp_path_info
*path
)
1022 route_map_result_t ret
;
1024 ret
= route_map_apply(map
, p
, path
);
1025 bgp_attr_flush(path
->attr
);
1027 if (ret
!= RMAP_DENYMATCH
)
1030 if (bgp_debug_zebra(p
)) {
1031 if (p
->family
== AF_INET
) {
1033 "Zebra rmap deny: IPv4 route %pFX nexthop %pI4",
1034 p
, &path
->attr
->nexthop
);
1036 if (p
->family
== AF_INET6
) {
1038 struct in6_addr
*nexthop
;
1040 nexthop
= bgp_path_info_to_ipv6_nexthop(path
, &ifindex
);
1042 "Zebra rmap deny: IPv6 route %pFX nexthop %pI6",
1049 static struct thread
*bgp_tm_thread_connect
;
1050 static bool bgp_tm_status_connected
;
1051 static bool bgp_tm_chunk_obtained
;
1052 #define BGP_FLOWSPEC_TABLE_CHUNK 100000
1053 static uint32_t bgp_tm_min
, bgp_tm_max
, bgp_tm_chunk_size
;
1054 struct bgp
*bgp_tm_bgp
;
1056 static void bgp_zebra_tm_connect(struct thread
*t
)
1058 struct zclient
*zclient
;
1059 int delay
= 10, ret
= 0;
1061 zclient
= THREAD_ARG(t
);
1062 if (bgp_tm_status_connected
&& zclient
->sock
> 0)
1065 bgp_tm_status_connected
= false;
1066 ret
= tm_table_manager_connect(zclient
);
1069 zlog_info("Error connecting to table manager!");
1070 bgp_tm_status_connected
= false;
1072 if (!bgp_tm_status_connected
)
1073 zlog_debug("Connecting to table manager. Success");
1074 bgp_tm_status_connected
= true;
1075 if (!bgp_tm_chunk_obtained
) {
1076 if (bgp_zebra_get_table_range(bgp_tm_chunk_size
,
1078 &bgp_tm_max
) >= 0) {
1079 bgp_tm_chunk_obtained
= true;
1080 /* parse non installed entries */
1081 bgp_zebra_announce_table(bgp_tm_bgp
, AFI_IP
, SAFI_FLOWSPEC
);
1085 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1086 &bgp_tm_thread_connect
);
1089 bool bgp_zebra_tm_chunk_obtained(void)
1091 return bgp_tm_chunk_obtained
;
1094 uint32_t bgp_zebra_tm_get_id(void)
1096 static int table_id
;
1098 if (!bgp_tm_chunk_obtained
)
1100 return bgp_tm_min
++;
1103 void bgp_zebra_init_tm_connect(struct bgp
*bgp
)
1107 /* if already set, do nothing
1109 if (bgp_tm_thread_connect
!= NULL
)
1111 bgp_tm_status_connected
= false;
1112 bgp_tm_chunk_obtained
= false;
1113 bgp_tm_min
= bgp_tm_max
= 0;
1114 bgp_tm_chunk_size
= BGP_FLOWSPEC_TABLE_CHUNK
;
1116 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1117 &bgp_tm_thread_connect
);
1120 int bgp_zebra_get_table_range(uint32_t chunk_size
,
1121 uint32_t *start
, uint32_t *end
)
1125 if (!bgp_tm_status_connected
)
1127 ret
= tm_get_table_chunk(zclient
, chunk_size
, start
, end
);
1129 flog_err(EC_BGP_TABLE_CHUNK
,
1130 "BGP: Error getting table chunk %u", chunk_size
);
1133 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1134 chunk_size
, *start
, *end
);
1138 static bool update_ipv4nh_for_route_install(int nh_othervrf
, struct bgp
*nh_bgp
,
1139 struct in_addr
*nexthop
,
1140 struct attr
*attr
, bool is_evpn
,
1141 struct zapi_nexthop
*api_nh
)
1143 api_nh
->gate
.ipv4
= *nexthop
;
1144 api_nh
->vrf_id
= nh_bgp
->vrf_id
;
1146 /* Need to set fields appropriately for EVPN routes imported into
1147 * a VRF (which are programmed as onlink on l3-vni SVI) as well as
1148 * connected routes leaked into a VRF.
1150 if (attr
->nh_type
== NEXTHOP_TYPE_BLACKHOLE
) {
1151 api_nh
->type
= attr
->nh_type
;
1152 api_nh
->bh_type
= attr
->bh_type
;
1153 } else if (is_evpn
) {
1155 * If the nexthop is EVPN overlay index gateway IP,
1156 * treat the nexthop as NEXTHOP_TYPE_IPV4
1157 * Else, mark the nexthop as onlink.
1159 if (attr
->evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
)
1160 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1162 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1163 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
);
1164 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
);
1165 api_nh
->ifindex
= nh_bgp
->l3vni_svi_ifindex
;
1167 } else if (nh_othervrf
&& api_nh
->gate
.ipv4
.s_addr
== INADDR_ANY
) {
1168 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1169 api_nh
->ifindex
= attr
->nh_ifindex
;
1171 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1176 static bool update_ipv6nh_for_route_install(int nh_othervrf
, struct bgp
*nh_bgp
,
1177 struct in6_addr
*nexthop
,
1179 struct bgp_path_info
*pi
,
1180 struct bgp_path_info
*best_pi
,
1182 struct zapi_nexthop
*api_nh
)
1187 api_nh
->vrf_id
= nh_bgp
->vrf_id
;
1189 if (attr
->nh_type
== NEXTHOP_TYPE_BLACKHOLE
) {
1190 api_nh
->type
= attr
->nh_type
;
1191 api_nh
->bh_type
= attr
->bh_type
;
1192 } else if (is_evpn
) {
1194 * If the nexthop is EVPN overlay index gateway IP,
1195 * treat the nexthop as NEXTHOP_TYPE_IPV4
1196 * Else, mark the nexthop as onlink.
1198 if (attr
->evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
)
1199 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1201 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1202 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
);
1203 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
);
1204 api_nh
->ifindex
= nh_bgp
->l3vni_svi_ifindex
;
1206 } else if (nh_othervrf
) {
1207 if (IN6_IS_ADDR_UNSPECIFIED(nexthop
)) {
1208 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1209 api_nh
->ifindex
= attr
->nh_ifindex
;
1210 } else if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1213 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1214 api_nh
->ifindex
= ifindex
;
1216 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1217 api_nh
->ifindex
= 0;
1220 if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1222 && attr
->mp_nexthop_len
1223 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1224 if (pi
->peer
->nexthop
.ifp
)
1226 pi
->peer
->nexthop
.ifp
->ifindex
;
1228 if (pi
->peer
->conf_if
)
1229 ifindex
= pi
->peer
->ifp
->ifindex
;
1230 else if (pi
->peer
->ifname
)
1231 ifindex
= ifname2ifindex(
1233 pi
->peer
->bgp
->vrf_id
);
1234 else if (pi
->peer
->nexthop
.ifp
)
1236 pi
->peer
->nexthop
.ifp
->ifindex
;
1241 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1242 api_nh
->ifindex
= ifindex
;
1244 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1245 api_nh
->ifindex
= 0;
1248 /* api_nh structure has union of gate and bh_type */
1249 if (nexthop
&& api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1250 api_nh
->gate
.ipv6
= *nexthop
;
1255 static bool bgp_zebra_use_nhop_weighted(struct bgp
*bgp
, struct attr
*attr
,
1256 uint64_t tot_bw
, uint32_t *nh_weight
)
1262 /* zero link-bandwidth and link-bandwidth not present are treated
1263 * as the same situation.
1266 /* the only situations should be if we're either told
1267 * to skip or use default weight.
1269 if (bgp
->lb_handling
== BGP_LINK_BW_SKIP_MISSING
)
1271 *nh_weight
= BGP_ZEBRA_DEFAULT_NHOP_WEIGHT
;
1273 tmp
= (uint64_t)bw
* 100;
1274 *nh_weight
= ((uint32_t)(tmp
/ tot_bw
));
1280 void bgp_zebra_announce(struct bgp_dest
*dest
, const struct prefix
*p
,
1281 struct bgp_path_info
*info
, struct bgp
*bgp
, afi_t afi
,
1284 struct zapi_route api
= { 0 };
1285 struct zapi_nexthop
*api_nh
;
1287 unsigned int valid_nh_count
= 0;
1288 bool allow_recursion
= false;
1291 struct bgp_path_info
*mpinfo
;
1292 struct bgp
*bgp_orig
;
1294 struct attr local_attr
;
1295 struct bgp_path_info local_info
;
1296 struct bgp_path_info
*mpinfo_cp
= &local_info
;
1298 struct bgp_sid_info
*sid_info
;
1299 mpls_label_t
*labels
;
1300 uint32_t num_labels
= 0;
1301 mpls_label_t nh_label
;
1302 int nh_othervrf
= 0;
1303 bool nh_updated
= false;
1305 uint64_t cum_bw
= 0;
1306 uint32_t nhg_id
= 0;
1312 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1313 * know of this instance.
1315 if (!bgp_install_info_to_zebra(bgp
))
1318 if (bgp
->main_zebra_update_hold
)
1321 if (safi
== SAFI_FLOWSPEC
) {
1322 bgp_pbr_update_entry(bgp
, bgp_dest_get_prefix(dest
), info
, afi
,
1328 * vrf leaking support (will have only one nexthop)
1330 if (info
->extra
&& info
->extra
->bgp_orig
)
1333 /* Make Zebra API structure. */
1334 api
.vrf_id
= bgp
->vrf_id
;
1335 api
.type
= ZEBRA_ROUTE_BGP
;
1338 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1342 if (info
->type
== ZEBRA_ROUTE_BGP
1343 && info
->sub_type
== BGP_ROUTE_IMPORTED
) {
1345 /* Obtain peer from parent */
1346 if (info
->extra
&& info
->extra
->parent
)
1347 peer
= ((struct bgp_path_info
*)(info
->extra
->parent
))
1351 tag
= info
->attr
->tag
;
1353 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
1354 || info
->sub_type
== BGP_ROUTE_AGGREGATE
) {
1355 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1356 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1359 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= BGP_DEFAULT_TTL
)
1360 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1361 || CHECK_FLAG(bgp
->flags
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1363 allow_recursion
= true;
1365 if (info
->attr
->rmap_table_id
) {
1366 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
1367 api
.tableid
= info
->attr
->rmap_table_id
;
1370 if (CHECK_FLAG(info
->attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR
)))
1371 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRTE
);
1373 /* Metric is currently based on the best-path only */
1374 metric
= info
->attr
->med
;
1376 /* Determine if we're doing weighted ECMP or not */
1377 do_wt_ecmp
= bgp_path_info_mpath_chkwtd(bgp
, info
);
1379 cum_bw
= bgp_path_info_mpath_cumbw(info
);
1381 /* EVPN MAC-IP routes are installed with a L3 NHG id */
1382 if (bgp_evpn_path_es_use_nhg(bgp
, info
, &nhg_id
)) {
1386 SET_FLAG(api
.message
, ZAPI_MESSAGE_NHG
);
1391 for (; mpinfo
; mpinfo
= bgp_path_info_mpath_next(mpinfo
)) {
1396 bool is_parent_evpn
;
1398 if (valid_nh_count
>= multipath_num
)
1401 *mpinfo_cp
= *mpinfo
;
1404 /* Get nexthop address-family */
1405 if (p
->family
== AF_INET
&&
1406 !BGP_ATTR_MP_NEXTHOP_LEN_IP6(mpinfo_cp
->attr
))
1407 nh_family
= AF_INET
;
1408 else if (p
->family
== AF_INET6
||
1409 (p
->family
== AF_INET
&&
1410 BGP_ATTR_MP_NEXTHOP_LEN_IP6(mpinfo_cp
->attr
)))
1411 nh_family
= AF_INET6
;
1415 /* If processing for weighted ECMP, determine the next hop's
1416 * weight. Based on user setting, we may skip the next hop
1417 * in some situations.
1420 if (!bgp_zebra_use_nhop_weighted(bgp
, mpinfo
->attr
,
1421 cum_bw
, &nh_weight
))
1424 api_nh
= &api
.nexthops
[valid_nh_count
];
1426 if (CHECK_FLAG(info
->attr
->flag
,
1427 ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR
)))
1428 api_nh
->srte_color
= info
->attr
->srte_color
;
1430 if (bgp_debug_zebra(&api
.prefix
)) {
1431 if (mpinfo
->extra
) {
1432 zlog_debug("%s: p=%pFX, bgp_is_valid_label: %d",
1435 &mpinfo
->extra
->label
[0]));
1438 "%s: p=%pFX, extra is NULL, no label",
1443 if (bgp
->table_map
[afi
][safi
].name
) {
1444 /* Copy info and attributes, so the route-map
1445 apply doesn't modify the BGP route info. */
1446 local_attr
= *mpinfo
->attr
;
1447 mpinfo_cp
->attr
= &local_attr
;
1448 if (!bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
,
1452 /* metric/tag is only allowed to be
1453 * overridden on 1st nexthop */
1454 if (mpinfo
== info
) {
1455 metric
= mpinfo_cp
->attr
->med
;
1456 tag
= mpinfo_cp
->attr
->tag
;
1460 BGP_ORIGINAL_UPDATE(bgp_orig
, mpinfo
, bgp
);
1462 is_parent_evpn
= is_route_parent_evpn(mpinfo
);
1464 if (nh_family
== AF_INET
) {
1465 nh_updated
= update_ipv4nh_for_route_install(
1466 nh_othervrf
, bgp_orig
,
1467 &mpinfo_cp
->attr
->nexthop
, mpinfo_cp
->attr
,
1468 is_parent_evpn
, api_nh
);
1470 ifindex_t ifindex
= IFINDEX_INTERNAL
;
1471 struct in6_addr
*nexthop
;
1473 nexthop
= bgp_path_info_to_ipv6_nexthop(mpinfo_cp
,
1477 nh_updated
= update_ipv4nh_for_route_install(
1478 nh_othervrf
, bgp_orig
,
1479 &mpinfo_cp
->attr
->nexthop
,
1480 mpinfo_cp
->attr
, is_parent_evpn
,
1483 nh_updated
= update_ipv6nh_for_route_install(
1484 nh_othervrf
, bgp_orig
, nexthop
, ifindex
,
1485 mpinfo
, info
, is_parent_evpn
, api_nh
);
1488 is_evpn
= !!CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
);
1490 /* Did we get proper nexthop info to update zebra? */
1494 /* Allow recursion if it is a multipath group with both
1495 * eBGP and iBGP paths.
1497 if (!allow_recursion
1498 && CHECK_FLAG(bgp
->flags
, BGP_FLAG_PEERTYPE_MULTIPATH_RELAX
)
1499 && (mpinfo
->peer
->sort
== BGP_PEER_IBGP
1500 || mpinfo
->peer
->sort
== BGP_PEER_CONFED
))
1501 allow_recursion
= true;
1503 if (mpinfo
->extra
) {
1504 labels
= mpinfo
->extra
->label
;
1505 num_labels
= mpinfo
->extra
->num_labels
;
1508 if (labels
&& (num_labels
> 0) &&
1509 (is_evpn
|| bgp_is_valid_label(&labels
[0]))) {
1510 enum lsp_types_t nh_label_type
= ZEBRA_LSP_NONE
;
1513 nh_label
= *bgp_evpn_path_info_labels_get_l3vni(
1514 labels
, num_labels
);
1515 nh_label_type
= ZEBRA_LSP_EVPN
;
1517 mpls_lse_decode(labels
[0], &nh_label
, &ttl
,
1521 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
);
1522 api_nh
->label_num
= 1;
1523 api_nh
->label_type
= nh_label_type
;
1524 api_nh
->labels
[0] = nh_label
;
1528 && mpinfo
->attr
->evpn_overlay
.type
1529 != OVERLAY_INDEX_GATEWAY_IP
)
1530 memcpy(&api_nh
->rmac
, &(mpinfo
->attr
->rmac
),
1531 sizeof(struct ethaddr
));
1533 api_nh
->weight
= nh_weight
;
1535 if (mpinfo
->extra
&& !is_evpn
&&
1536 bgp_is_valid_label(&labels
[0]) &&
1537 !sid_zero(&mpinfo
->extra
->sid
[0].sid
)) {
1538 sid_info
= &mpinfo
->extra
->sid
[0];
1540 memcpy(&api_nh
->seg6_segs
, &sid_info
->sid
,
1541 sizeof(api_nh
->seg6_segs
));
1543 if (sid_info
->transposition_len
!= 0) {
1544 mpls_lse_decode(labels
[0], &nh_label
, &ttl
,
1547 if (nh_label
< MPLS_LABEL_UNRESERVED_MIN
) {
1548 if (bgp_debug_zebra(&api
.prefix
))
1550 "skip invalid SRv6 routes: transposition scheme is used, but label is too small");
1554 transpose_sid(&api_nh
->seg6_segs
, nh_label
,
1555 sid_info
->transposition_offset
,
1556 sid_info
->transposition_len
);
1559 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6
);
1565 is_add
= (valid_nh_count
|| nhg_id
) ? true : false;
1567 if (is_add
&& CHECK_FLAG(bm
->flags
, BM_FLAG_SEND_EXTRA_DATA_TO_ZEBRA
)) {
1568 struct bgp_zebra_opaque bzo
= {};
1569 const char *reason
=
1570 bgp_path_selection_reason2str(dest
->reason
);
1572 strlcpy(bzo
.aspath
, info
->attr
->aspath
->str
,
1573 sizeof(bzo
.aspath
));
1575 if (info
->attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
))
1576 strlcpy(bzo
.community
,
1577 bgp_attr_get_community(info
->attr
)->str
,
1578 sizeof(bzo
.community
));
1580 if (info
->attr
->flag
1581 & ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
))
1582 strlcpy(bzo
.lcommunity
,
1583 bgp_attr_get_lcommunity(info
->attr
)->str
,
1584 sizeof(bzo
.lcommunity
));
1586 strlcpy(bzo
.selection_reason
, reason
,
1587 sizeof(bzo
.selection_reason
));
1589 SET_FLAG(api
.message
, ZAPI_MESSAGE_OPAQUE
);
1590 api
.opaque
.length
= MIN(sizeof(struct bgp_zebra_opaque
),
1591 ZAPI_MESSAGE_OPAQUE_LENGTH
);
1592 memcpy(api
.opaque
.data
, &bzo
, api
.opaque
.length
);
1595 if (allow_recursion
)
1596 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1599 * When we create an aggregate route we must also
1600 * install a Null0 route in the RIB, so overwrite
1601 * what was written into api with a blackhole route
1603 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1604 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
1606 api
.nexthop_num
= valid_nh_count
;
1608 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1609 api
.metric
= metric
;
1612 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1616 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1618 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1619 api
.distance
= distance
;
1622 if (bgp_debug_zebra(p
)) {
1623 char nh_buf
[INET6_ADDRSTRLEN
];
1624 char eth_buf
[ETHER_ADDR_STRLEN
+ 7] = {'\0'};
1625 char buf1
[ETHER_ADDR_STRLEN
];
1632 "Tx route %s VRF %u %pFX metric %u tag %" ROUTE_TAG_PRI
1634 is_add
? "add" : "delete", bgp
->vrf_id
, &api
.prefix
,
1635 api
.metric
, api
.tag
, api
.nexthop_num
, nhg_id
);
1636 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1637 api_nh
= &api
.nexthops
[i
];
1639 switch (api_nh
->type
) {
1640 case NEXTHOP_TYPE_IFINDEX
:
1643 case NEXTHOP_TYPE_IPV4
:
1644 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1645 nh_family
= AF_INET
;
1646 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1649 case NEXTHOP_TYPE_IPV6
:
1650 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1651 nh_family
= AF_INET6
;
1652 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1655 case NEXTHOP_TYPE_BLACKHOLE
:
1656 strlcpy(nh_buf
, "blackhole", sizeof(nh_buf
));
1659 /* Note: add new nexthop case */
1664 label_buf
[0] = '\0';
1667 if (CHECK_FLAG(api_nh
->flags
,
1668 ZAPI_NEXTHOP_FLAG_LABEL
) &&
1669 !CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
))
1670 snprintf(label_buf
, sizeof(label_buf
),
1671 "label %u", api_nh
->labels
[0]);
1672 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6
) &&
1673 !CHECK_FLAG(api_nh
->flags
,
1674 ZAPI_NEXTHOP_FLAG_EVPN
)) {
1675 inet_ntop(AF_INET6
, &api_nh
->seg6_segs
,
1676 sid_buf
, sizeof(sid_buf
));
1677 snprintf(segs_buf
, sizeof(segs_buf
), "segs %s",
1680 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
) &&
1681 !is_zero_mac(&api_nh
->rmac
))
1682 snprintf(eth_buf
, sizeof(eth_buf
), " RMAC %s",
1683 prefix_mac2str(&api_nh
->rmac
,
1684 buf1
, sizeof(buf1
)));
1685 zlog_debug(" nhop [%d]: %s if %u VRF %u wt %u %s %s %s",
1686 i
+ 1, nh_buf
, api_nh
->ifindex
,
1687 api_nh
->vrf_id
, api_nh
->weight
,
1688 label_buf
, segs_buf
, eth_buf
);
1691 int recursion_flag
= 0;
1693 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
))
1696 zlog_debug("%s: %pFX: announcing to zebra (recursion %sset)",
1697 __func__
, p
, (recursion_flag
? "" : "NOT "));
1699 zclient_route_send(is_add
? ZEBRA_ROUTE_ADD
: ZEBRA_ROUTE_DELETE
,
1703 /* Announce all routes of a table to zebra */
1704 void bgp_zebra_announce_table(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1706 struct bgp_dest
*dest
;
1707 struct bgp_table
*table
;
1708 struct bgp_path_info
*pi
;
1710 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1711 * know of this instance.
1713 if (!bgp_install_info_to_zebra(bgp
))
1716 table
= bgp
->rib
[afi
][safi
];
1720 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
))
1721 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
1722 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
) &&
1724 (pi
->type
== ZEBRA_ROUTE_BGP
1725 && (pi
->sub_type
== BGP_ROUTE_NORMAL
1726 || pi
->sub_type
== BGP_ROUTE_IMPORTED
)))
1728 bgp_zebra_announce(dest
,
1729 bgp_dest_get_prefix(dest
),
1730 pi
, bgp
, afi
, safi
);
1733 /* Announce routes of any bgp subtype of a table to zebra */
1734 void bgp_zebra_announce_table_all_subtypes(struct bgp
*bgp
, afi_t afi
,
1737 struct bgp_dest
*dest
;
1738 struct bgp_table
*table
;
1739 struct bgp_path_info
*pi
;
1741 if (!bgp_install_info_to_zebra(bgp
))
1744 table
= bgp
->rib
[afi
][safi
];
1748 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
))
1749 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
1750 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
) &&
1751 pi
->type
== ZEBRA_ROUTE_BGP
)
1752 bgp_zebra_announce(dest
,
1753 bgp_dest_get_prefix(dest
),
1754 pi
, bgp
, afi
, safi
);
1757 void bgp_zebra_withdraw(const struct prefix
*p
, struct bgp_path_info
*info
,
1758 struct bgp
*bgp
, safi_t safi
)
1760 struct zapi_route api
;
1763 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1764 * know of this instance.
1766 if (!bgp_install_info_to_zebra(bgp
))
1769 if (safi
== SAFI_FLOWSPEC
) {
1771 bgp_pbr_update_entry(peer
->bgp
, p
, info
, AFI_IP
, safi
, false);
1775 memset(&api
, 0, sizeof(api
));
1776 api
.vrf_id
= bgp
->vrf_id
;
1777 api
.type
= ZEBRA_ROUTE_BGP
;
1781 if (info
->attr
->rmap_table_id
) {
1782 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
1783 api
.tableid
= info
->attr
->rmap_table_id
;
1786 if (bgp_debug_zebra(p
))
1787 zlog_debug("Tx route delete VRF %u %pFX", bgp
->vrf_id
,
1790 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
1793 /* Withdraw all entries in a BGP instances RIB table from Zebra */
1794 void bgp_zebra_withdraw_table_all_subtypes(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1796 struct bgp_dest
*dest
;
1797 struct bgp_table
*table
;
1798 struct bgp_path_info
*pi
;
1800 if (!bgp_install_info_to_zebra(bgp
))
1803 table
= bgp
->rib
[afi
][safi
];
1807 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
1808 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
1809 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
1810 && (pi
->type
== ZEBRA_ROUTE_BGP
))
1811 bgp_zebra_withdraw(bgp_dest_get_prefix(dest
),
1817 struct bgp_redist
*bgp_redist_lookup(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1818 unsigned short instance
)
1820 struct list
*red_list
;
1821 struct listnode
*node
;
1822 struct bgp_redist
*red
;
1824 red_list
= bgp
->redist
[afi
][type
];
1828 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1829 if (red
->instance
== instance
)
1835 struct bgp_redist
*bgp_redist_add(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1836 unsigned short instance
)
1838 struct list
*red_list
;
1839 struct bgp_redist
*red
;
1841 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1845 if (!bgp
->redist
[afi
][type
])
1846 bgp
->redist
[afi
][type
] = list_new();
1848 red_list
= bgp
->redist
[afi
][type
];
1849 red
= XCALLOC(MTYPE_BGP_REDIST
, sizeof(struct bgp_redist
));
1850 red
->instance
= instance
;
1852 listnode_add(red_list
, red
);
1857 static void bgp_redist_del(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1858 unsigned short instance
)
1860 struct bgp_redist
*red
;
1862 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1865 listnode_delete(bgp
->redist
[afi
][type
], red
);
1866 XFREE(MTYPE_BGP_REDIST
, red
);
1867 if (!bgp
->redist
[afi
][type
]->count
)
1868 list_delete(&bgp
->redist
[afi
][type
]);
1872 /* Other routes redistribution into BGP. */
1873 int bgp_redistribute_set(struct bgp
*bgp
, afi_t afi
, int type
,
1874 unsigned short instance
, bool changed
)
1876 /* If redistribute options are changed call
1877 * bgp_redistribute_unreg() to reset the option and withdraw
1881 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1883 /* Return if already redistribute flag is set. */
1885 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1889 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1891 if (vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1894 #ifdef ENABLE_BGP_VNC
1895 if (EVPN_ENABLED(bgp
) && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1896 vnc_export_bgp_enable(
1897 bgp
, afi
); /* only enables if mode bits cfg'd */
1901 vrf_bitmap_set(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1905 * Don't try to register if we're not connected to Zebra or Zebra
1906 * doesn't know of this instance.
1908 * When we come up later well resend if needed.
1910 if (!bgp_install_info_to_zebra(bgp
))
1913 if (BGP_DEBUG(zebra
, ZEBRA
))
1914 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1915 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1918 /* Send distribute add message to zebra. */
1919 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1920 instance
, bgp
->vrf_id
);
1925 int bgp_redistribute_resend(struct bgp
*bgp
, afi_t afi
, int type
,
1926 unsigned short instance
)
1928 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1929 * know of this instance.
1931 if (!bgp_install_info_to_zebra(bgp
))
1934 if (BGP_DEBUG(zebra
, ZEBRA
))
1935 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1936 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1939 /* Send distribute add message to zebra. */
1940 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1941 instance
, bgp
->vrf_id
);
1942 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1943 instance
, bgp
->vrf_id
);
1948 /* Redistribute with route-map specification. */
1949 bool bgp_redistribute_rmap_set(struct bgp_redist
*red
, const char *name
,
1950 struct route_map
*route_map
)
1952 if (red
->rmap
.name
&& (strcmp(red
->rmap
.name
, name
) == 0))
1955 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1956 /* Decrement the count for existing routemap and
1957 * increment the count for new route map.
1959 route_map_counter_decrement(red
->rmap
.map
);
1960 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1961 red
->rmap
.map
= route_map
;
1962 route_map_counter_increment(red
->rmap
.map
);
1967 /* Redistribute with metric specification. */
1968 bool bgp_redistribute_metric_set(struct bgp
*bgp
, struct bgp_redist
*red
,
1969 afi_t afi
, int type
, uint32_t metric
)
1971 struct bgp_dest
*dest
;
1972 struct bgp_path_info
*pi
;
1974 if (red
->redist_metric_flag
&& red
->redist_metric
== metric
)
1977 red
->redist_metric_flag
= 1;
1978 red
->redist_metric
= metric
;
1980 for (dest
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); dest
;
1981 dest
= bgp_route_next(dest
)) {
1982 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
1983 if (pi
->sub_type
== BGP_ROUTE_REDISTRIBUTE
1985 && pi
->instance
== red
->instance
) {
1986 struct attr
*old_attr
;
1987 struct attr new_attr
;
1989 new_attr
= *pi
->attr
;
1990 new_attr
.med
= red
->redist_metric
;
1991 old_attr
= pi
->attr
;
1992 pi
->attr
= bgp_attr_intern(&new_attr
);
1993 bgp_attr_unintern(&old_attr
);
1995 bgp_path_info_set_flag(dest
, pi
,
1996 BGP_PATH_ATTR_CHANGED
);
1997 bgp_process(bgp
, dest
, afi
, SAFI_UNICAST
);
2005 /* Unset redistribution. */
2006 int bgp_redistribute_unreg(struct bgp
*bgp
, afi_t afi
, int type
,
2007 unsigned short instance
)
2009 struct bgp_redist
*red
;
2011 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
2015 /* Return if zebra connection is disabled. */
2017 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
],
2020 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
2022 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
2024 vrf_bitmap_unset(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
2027 if (bgp_install_info_to_zebra(bgp
)) {
2028 /* Send distribute delete message to zebra. */
2029 if (BGP_DEBUG(zebra
, ZEBRA
))
2030 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
2031 bgp
->vrf_id
, afi
, zebra_route_string(type
),
2033 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
2034 type
, instance
, bgp
->vrf_id
);
2037 /* Withdraw redistributed routes from current BGP's routing table. */
2038 bgp_redistribute_withdraw(bgp
, afi
, type
, instance
);
2043 /* Unset redistribution. */
2044 int bgp_redistribute_unset(struct bgp
*bgp
, afi_t afi
, int type
,
2045 unsigned short instance
)
2047 struct bgp_redist
*red
;
2050 * vnc and vpn->vrf checks must be before red check because
2051 * they operate within bgpd irrespective of zebra connection
2052 * status. red lookup fails if there is no zebra connection.
2054 #ifdef ENABLE_BGP_VNC
2055 if (EVPN_ENABLED(bgp
) && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
2056 vnc_export_bgp_disable(bgp
, afi
);
2060 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
2064 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
2066 /* Unset route-map. */
2067 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
2068 route_map_counter_decrement(red
->rmap
.map
);
2069 red
->rmap
.map
= NULL
;
2072 red
->redist_metric_flag
= 0;
2073 red
->redist_metric
= 0;
2075 bgp_redist_del(bgp
, afi
, type
, instance
);
2080 void bgp_redistribute_redo(struct bgp
*bgp
)
2084 struct list
*red_list
;
2085 struct listnode
*node
;
2086 struct bgp_redist
*red
;
2088 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2089 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
2091 red_list
= bgp
->redist
[afi
][i
];
2095 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
2096 bgp_redistribute_resend(bgp
, afi
, i
,
2103 void bgp_zclient_reset(void)
2105 zclient_reset(zclient
);
2108 /* Register this instance with Zebra. Invoked upon connect (for
2109 * default instance) and when other VRFs are learnt (or created and
2112 void bgp_zebra_instance_register(struct bgp
*bgp
)
2114 /* Don't try to register if we're not connected to Zebra */
2115 if (!zclient
|| zclient
->sock
< 0)
2118 if (BGP_DEBUG(zebra
, ZEBRA
))
2119 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
2121 /* Register for router-id, interfaces, redistributed routes. */
2122 zclient_send_reg_requests(zclient
, bgp
->vrf_id
);
2124 /* For EVPN instance, register to learn about VNIs, if appropriate. */
2125 if (bgp
->advertise_all_vni
)
2126 bgp_zebra_advertise_all_vni(bgp
, 1);
2128 bgp_nht_register_nexthops(bgp
);
2131 /* Deregister this instance with Zebra. Invoked upon the instance
2132 * being deleted (default or VRF) and it is already registered.
2134 void bgp_zebra_instance_deregister(struct bgp
*bgp
)
2136 /* Don't try to deregister if we're not connected to Zebra */
2137 if (zclient
->sock
< 0)
2140 if (BGP_DEBUG(zebra
, ZEBRA
))
2141 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
2143 /* For EVPN instance, unregister learning about VNIs, if appropriate. */
2144 if (bgp
->advertise_all_vni
)
2145 bgp_zebra_advertise_all_vni(bgp
, 0);
2147 /* Deregister for router-id, interfaces, redistributed routes. */
2148 zclient_send_dereg_requests(zclient
, bgp
->vrf_id
);
2151 void bgp_zebra_initiate_radv(struct bgp
*bgp
, struct peer
*peer
)
2153 uint32_t ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
2155 /* Don't try to initiate if we're not connected to Zebra */
2156 if (zclient
->sock
< 0)
2159 if (BGP_DEBUG(zebra
, ZEBRA
))
2160 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
,
2164 * If unnumbered peer (peer->ifp) call thru zapi to start RAs.
2165 * If we don't have an ifp pointer, call function to find the
2166 * ifps for a numbered enhe peer to turn RAs on.
2168 peer
->ifp
? zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
,
2169 peer
->ifp
, 1, ra_interval
)
2170 : bgp_nht_reg_enhe_cap_intfs(peer
);
2173 void bgp_zebra_terminate_radv(struct bgp
*bgp
, struct peer
*peer
)
2175 /* Don't try to terminate if we're not connected to Zebra */
2176 if (zclient
->sock
< 0)
2179 if (BGP_DEBUG(zebra
, ZEBRA
))
2180 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
,
2184 * If unnumbered peer (peer->ifp) call thru zapi to stop RAs.
2185 * If we don't have an ifp pointer, call function to find the
2186 * ifps for a numbered enhe peer to turn RAs off.
2188 peer
->ifp
? zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
,
2190 : bgp_nht_dereg_enhe_cap_intfs(peer
);
2193 int bgp_zebra_advertise_subnet(struct bgp
*bgp
, int advertise
, vni_t vni
)
2195 struct stream
*s
= NULL
;
2198 if (!zclient
|| zclient
->sock
< 0)
2201 /* Don't try to register if Zebra doesn't know of this instance. */
2202 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
2203 if (BGP_DEBUG(zebra
, ZEBRA
))
2205 "%s: No zebra instance to talk to, cannot advertise subnet",
2213 zclient_create_header(s
, ZEBRA_ADVERTISE_SUBNET
, bgp
->vrf_id
);
2214 stream_putc(s
, advertise
);
2215 stream_put3(s
, vni
);
2216 stream_putw_at(s
, 0, stream_get_endp(s
));
2218 return zclient_send_message(zclient
);
2221 int bgp_zebra_advertise_svi_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
2223 struct stream
*s
= NULL
;
2226 if (!zclient
|| zclient
->sock
< 0)
2229 /* Don't try to register if Zebra doesn't know of this instance. */
2230 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
2236 zclient_create_header(s
, ZEBRA_ADVERTISE_SVI_MACIP
, bgp
->vrf_id
);
2237 stream_putc(s
, advertise
);
2238 stream_putl(s
, vni
);
2239 stream_putw_at(s
, 0, stream_get_endp(s
));
2241 return zclient_send_message(zclient
);
2244 int bgp_zebra_advertise_gw_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
2246 struct stream
*s
= NULL
;
2249 if (!zclient
|| zclient
->sock
< 0)
2252 /* Don't try to register if Zebra doesn't know of this instance. */
2253 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
2254 if (BGP_DEBUG(zebra
, ZEBRA
))
2256 "%s: No zebra instance to talk to, not installing gw_macip",
2264 zclient_create_header(s
, ZEBRA_ADVERTISE_DEFAULT_GW
, bgp
->vrf_id
);
2265 stream_putc(s
, advertise
);
2266 stream_putl(s
, vni
);
2267 stream_putw_at(s
, 0, stream_get_endp(s
));
2269 return zclient_send_message(zclient
);
2272 int bgp_zebra_vxlan_flood_control(struct bgp
*bgp
,
2273 enum vxlan_flood_control flood_ctrl
)
2278 if (!zclient
|| zclient
->sock
< 0)
2281 /* Don't try to register if Zebra doesn't know of this instance. */
2282 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
2283 if (BGP_DEBUG(zebra
, ZEBRA
))
2285 "%s: No zebra instance to talk to, not installing all vni",
2293 zclient_create_header(s
, ZEBRA_VXLAN_FLOOD_CONTROL
, bgp
->vrf_id
);
2294 stream_putc(s
, flood_ctrl
);
2295 stream_putw_at(s
, 0, stream_get_endp(s
));
2297 return zclient_send_message(zclient
);
2300 int bgp_zebra_advertise_all_vni(struct bgp
*bgp
, int advertise
)
2305 if (!zclient
|| zclient
->sock
< 0)
2308 /* Don't try to register if Zebra doesn't know of this instance. */
2309 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
2315 zclient_create_header(s
, ZEBRA_ADVERTISE_ALL_VNI
, bgp
->vrf_id
);
2316 stream_putc(s
, advertise
);
2317 /* Also inform current BUM handling setting. This is really
2318 * relevant only when 'advertise' is set.
2320 stream_putc(s
, bgp
->vxlan_flood_ctrl
);
2321 stream_putw_at(s
, 0, stream_get_endp(s
));
2323 return zclient_send_message(zclient
);
2326 int bgp_zebra_dup_addr_detection(struct bgp
*bgp
)
2331 if (!zclient
|| zclient
->sock
< 0)
2334 /* Don't try to register if Zebra doesn't know of this instance. */
2335 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
2338 if (BGP_DEBUG(zebra
, ZEBRA
))
2339 zlog_debug("dup addr detect %s max_moves %u time %u freeze %s freeze_time %u",
2340 bgp
->evpn_info
->dup_addr_detect
?
2341 "enable" : "disable",
2342 bgp
->evpn_info
->dad_max_moves
,
2343 bgp
->evpn_info
->dad_time
,
2344 bgp
->evpn_info
->dad_freeze
?
2345 "enable" : "disable",
2346 bgp
->evpn_info
->dad_freeze_time
);
2350 zclient_create_header(s
, ZEBRA_DUPLICATE_ADDR_DETECTION
,
2352 stream_putl(s
, bgp
->evpn_info
->dup_addr_detect
);
2353 stream_putl(s
, bgp
->evpn_info
->dad_time
);
2354 stream_putl(s
, bgp
->evpn_info
->dad_max_moves
);
2355 stream_putl(s
, bgp
->evpn_info
->dad_freeze
);
2356 stream_putl(s
, bgp
->evpn_info
->dad_freeze_time
);
2357 stream_putw_at(s
, 0, stream_get_endp(s
));
2359 return zclient_send_message(zclient
);
2362 static int rule_notify_owner(ZAPI_CALLBACK_ARGS
)
2364 uint32_t seqno
, priority
, unique
;
2365 enum zapi_rule_notify_owner note
;
2366 struct bgp_pbr_action
*bgp_pbra
;
2367 struct bgp_pbr_rule
*bgp_pbr
= NULL
;
2368 char ifname
[INTERFACE_NAMSIZ
+ 1];
2370 if (!zapi_rule_notify_decode(zclient
->ibuf
, &seqno
, &priority
, &unique
,
2374 bgp_pbra
= bgp_pbr_action_rule_lookup(vrf_id
, unique
);
2376 /* look in bgp pbr rule */
2377 bgp_pbr
= bgp_pbr_rule_lookup(vrf_id
, unique
);
2378 if (!bgp_pbr
&& note
!= ZAPI_RULE_REMOVED
) {
2379 if (BGP_DEBUG(zebra
, ZEBRA
))
2380 zlog_debug("%s: Fail to look BGP rule (%u)",
2387 case ZAPI_RULE_FAIL_INSTALL
:
2388 if (BGP_DEBUG(zebra
, ZEBRA
))
2389 zlog_debug("%s: Received RULE_FAIL_INSTALL", __func__
);
2391 bgp_pbra
->installed
= false;
2392 bgp_pbra
->install_in_progress
= false;
2394 bgp_pbr
->installed
= false;
2395 bgp_pbr
->install_in_progress
= false;
2398 case ZAPI_RULE_INSTALLED
:
2400 bgp_pbra
->installed
= true;
2401 bgp_pbra
->install_in_progress
= false;
2403 struct bgp_path_info
*path
;
2404 struct bgp_path_info_extra
*extra
;
2406 bgp_pbr
->installed
= true;
2407 bgp_pbr
->install_in_progress
= false;
2408 bgp_pbr
->action
->refcnt
++;
2409 /* link bgp_info to bgp_pbr */
2410 path
= (struct bgp_path_info
*)bgp_pbr
->path
;
2411 extra
= bgp_path_info_extra_get(path
);
2412 listnode_add_force(&extra
->bgp_fs_iprule
,
2415 if (BGP_DEBUG(zebra
, ZEBRA
))
2416 zlog_debug("%s: Received RULE_INSTALLED", __func__
);
2418 case ZAPI_RULE_FAIL_REMOVE
:
2419 case ZAPI_RULE_REMOVED
:
2420 if (BGP_DEBUG(zebra
, ZEBRA
))
2421 zlog_debug("%s: Received RULE REMOVED", __func__
);
2428 static int ipset_notify_owner(ZAPI_CALLBACK_ARGS
)
2431 enum zapi_ipset_notify_owner note
;
2432 struct bgp_pbr_match
*bgp_pbim
;
2434 if (!zapi_ipset_notify_decode(zclient
->ibuf
,
2439 bgp_pbim
= bgp_pbr_match_ipset_lookup(vrf_id
, unique
);
2441 if (BGP_DEBUG(zebra
, ZEBRA
))
2442 zlog_debug("%s: Fail to look BGP match ( %u, ID %u)",
2443 __func__
, note
, unique
);
2448 case ZAPI_IPSET_FAIL_INSTALL
:
2449 if (BGP_DEBUG(zebra
, ZEBRA
))
2450 zlog_debug("%s: Received IPSET_FAIL_INSTALL", __func__
);
2451 bgp_pbim
->installed
= false;
2452 bgp_pbim
->install_in_progress
= false;
2454 case ZAPI_IPSET_INSTALLED
:
2455 bgp_pbim
->installed
= true;
2456 bgp_pbim
->install_in_progress
= false;
2457 if (BGP_DEBUG(zebra
, ZEBRA
))
2458 zlog_debug("%s: Received IPSET_INSTALLED", __func__
);
2460 case ZAPI_IPSET_FAIL_REMOVE
:
2461 case ZAPI_IPSET_REMOVED
:
2462 if (BGP_DEBUG(zebra
, ZEBRA
))
2463 zlog_debug("%s: Received IPSET REMOVED", __func__
);
2470 static int ipset_entry_notify_owner(ZAPI_CALLBACK_ARGS
)
2473 char ipset_name
[ZEBRA_IPSET_NAME_SIZE
];
2474 enum zapi_ipset_entry_notify_owner note
;
2475 struct bgp_pbr_match_entry
*bgp_pbime
;
2477 if (!zapi_ipset_entry_notify_decode(
2483 bgp_pbime
= bgp_pbr_match_ipset_entry_lookup(vrf_id
,
2487 if (BGP_DEBUG(zebra
, ZEBRA
))
2489 "%s: Fail to look BGP match entry (%u, ID %u)",
2490 __func__
, note
, unique
);
2495 case ZAPI_IPSET_ENTRY_FAIL_INSTALL
:
2496 if (BGP_DEBUG(zebra
, ZEBRA
))
2497 zlog_debug("%s: Received IPSET_ENTRY_FAIL_INSTALL",
2499 bgp_pbime
->installed
= false;
2500 bgp_pbime
->install_in_progress
= false;
2502 case ZAPI_IPSET_ENTRY_INSTALLED
:
2504 struct bgp_path_info
*path
;
2505 struct bgp_path_info_extra
*extra
;
2507 bgp_pbime
->installed
= true;
2508 bgp_pbime
->install_in_progress
= false;
2509 if (BGP_DEBUG(zebra
, ZEBRA
))
2510 zlog_debug("%s: Received IPSET_ENTRY_INSTALLED",
2512 /* link bgp_path_info to bpme */
2513 path
= (struct bgp_path_info
*)bgp_pbime
->path
;
2514 extra
= bgp_path_info_extra_get(path
);
2515 listnode_add_force(&extra
->bgp_fs_pbr
, bgp_pbime
);
2518 case ZAPI_IPSET_ENTRY_FAIL_REMOVE
:
2519 case ZAPI_IPSET_ENTRY_REMOVED
:
2520 if (BGP_DEBUG(zebra
, ZEBRA
))
2521 zlog_debug("%s: Received IPSET_ENTRY_REMOVED",
2528 static int iptable_notify_owner(ZAPI_CALLBACK_ARGS
)
2531 enum zapi_iptable_notify_owner note
;
2532 struct bgp_pbr_match
*bgpm
;
2534 if (!zapi_iptable_notify_decode(
2539 bgpm
= bgp_pbr_match_iptable_lookup(vrf_id
, unique
);
2541 if (BGP_DEBUG(zebra
, ZEBRA
))
2542 zlog_debug("%s: Fail to look BGP iptable (%u %u)",
2543 __func__
, note
, unique
);
2547 case ZAPI_IPTABLE_FAIL_INSTALL
:
2548 if (BGP_DEBUG(zebra
, ZEBRA
))
2549 zlog_debug("%s: Received IPTABLE_FAIL_INSTALL",
2551 bgpm
->installed_in_iptable
= false;
2552 bgpm
->install_iptable_in_progress
= false;
2554 case ZAPI_IPTABLE_INSTALLED
:
2555 bgpm
->installed_in_iptable
= true;
2556 bgpm
->install_iptable_in_progress
= false;
2557 if (BGP_DEBUG(zebra
, ZEBRA
))
2558 zlog_debug("%s: Received IPTABLE_INSTALLED", __func__
);
2559 bgpm
->action
->refcnt
++;
2561 case ZAPI_IPTABLE_FAIL_REMOVE
:
2562 case ZAPI_IPTABLE_REMOVED
:
2563 if (BGP_DEBUG(zebra
, ZEBRA
))
2564 zlog_debug("%s: Received IPTABLE REMOVED", __func__
);
2570 /* Process route notification messages from RIB */
2571 static int bgp_zebra_route_notify_owner(int command
, struct zclient
*zclient
,
2572 zebra_size_t length
, vrf_id_t vrf_id
)
2575 enum zapi_route_notify_owner note
;
2579 struct bgp_dest
*dest
;
2581 struct bgp_path_info
*pi
, *new_select
;
2583 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table_id
, ¬e
,
2585 zlog_err("%s : error in msg decode", __func__
);
2589 /* Get the bgp instance */
2590 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2592 flog_err(EC_BGP_INVALID_BGP_INSTANCE
,
2593 "%s : bgp instance not found vrf %d", __func__
,
2598 /* Find the bgp route node */
2599 dest
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
, &p
,
2605 case ZAPI_ROUTE_INSTALLED
:
2607 /* Clear the flags so that route can be processed */
2608 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALL_PENDING
);
2609 SET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALLED
);
2610 if (BGP_DEBUG(zebra
, ZEBRA
))
2611 zlog_debug("route %pRN : INSTALLED", dest
);
2612 /* Find the best route */
2613 for (pi
= dest
->info
; pi
; pi
= pi
->next
) {
2614 /* Process aggregate route */
2615 bgp_aggregate_increment(bgp
, &p
, pi
, afi
, safi
);
2616 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
))
2619 /* Advertise the route */
2621 group_announce_route(bgp
, afi
, safi
, dest
, new_select
);
2623 flog_err(EC_BGP_INVALID_ROUTE
,
2624 "selected route %pRN not found", dest
);
2626 bgp_dest_unlock_node(dest
);
2630 case ZAPI_ROUTE_REMOVED
:
2631 /* Route deleted from dataplane, reset the installed flag
2632 * so that route can be reinstalled when client sends
2635 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALLED
);
2636 if (BGP_DEBUG(zebra
, ZEBRA
))
2637 zlog_debug("route %pRN: Removed from Fib", dest
);
2639 case ZAPI_ROUTE_FAIL_INSTALL
:
2641 if (BGP_DEBUG(zebra
, ZEBRA
))
2642 zlog_debug("route: %pRN Failed to Install into Fib",
2644 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALL_PENDING
);
2645 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALLED
);
2646 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
2647 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
))
2651 group_announce_route(bgp
, afi
, safi
, dest
, new_select
);
2652 /* Error will be logged by zebra module */
2654 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
2655 if (BGP_DEBUG(zebra
, ZEBRA
))
2656 zlog_debug("route: %pRN removed due to better admin won",
2659 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALL_PENDING
);
2660 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALLED
);
2661 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
2662 bgp_aggregate_decrement(bgp
, &p
, pi
, afi
, safi
);
2663 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
))
2667 group_announce_route(bgp
, afi
, safi
, dest
, new_select
);
2668 /* No action required */
2670 case ZAPI_ROUTE_REMOVE_FAIL
:
2671 zlog_warn("%s: Route %pRN failure to remove",
2676 bgp_dest_unlock_node(dest
);
2680 /* this function is used to forge ip rule,
2681 * - either for iptable/ipset using fwmark id
2682 * - or for sample ip rule cmd
2684 static void bgp_encode_pbr_rule_action(struct stream
*s
,
2685 struct bgp_pbr_action
*pbra
,
2686 struct bgp_pbr_rule
*pbr
)
2689 uint8_t fam
= AF_INET
;
2690 char ifname
[INTERFACE_NAMSIZ
];
2692 if (pbra
->nh
.type
== NEXTHOP_TYPE_IPV6
)
2694 stream_putl(s
, 0); /* seqno unused */
2696 stream_putl(s
, pbr
->priority
);
2699 /* ruleno unused - priority change
2700 * ruleno permits distinguishing various FS PBR entries
2701 * - FS PBR entries based on ipset/iptables
2702 * - FS PBR entries based on iprule
2703 * the latter may contain default routing information injected by FS
2706 stream_putl(s
, pbr
->unique
);
2708 stream_putl(s
, pbra
->unique
);
2709 stream_putc(s
, 0); /* ip protocol being used */
2710 if (pbr
&& pbr
->flags
& MATCH_IP_SRC_SET
)
2711 memcpy(&pfx
, &(pbr
->src
), sizeof(struct prefix
));
2713 memset(&pfx
, 0, sizeof(pfx
));
2716 stream_putc(s
, pfx
.family
);
2717 stream_putc(s
, pfx
.prefixlen
);
2718 stream_put(s
, &pfx
.u
.prefix
, prefix_blen(&pfx
));
2720 stream_putw(s
, 0); /* src port */
2722 if (pbr
&& pbr
->flags
& MATCH_IP_DST_SET
)
2723 memcpy(&pfx
, &(pbr
->dst
), sizeof(struct prefix
));
2725 memset(&pfx
, 0, sizeof(pfx
));
2728 stream_putc(s
, pfx
.family
);
2729 stream_putc(s
, pfx
.prefixlen
);
2730 stream_put(s
, &pfx
.u
.prefix
, prefix_blen(&pfx
));
2732 stream_putw(s
, 0); /* dst port */
2733 stream_putc(s
, 0); /* dsfield */
2734 /* if pbr present, fwmark is not used */
2738 stream_putl(s
, pbra
->fwmark
); /* fwmark */
2740 stream_putl(s
, 0); /* queue id */
2741 stream_putw(s
, 0); /* vlan_id */
2742 stream_putw(s
, 0); /* vlan_flags */
2743 stream_putw(s
, 0); /* pcp */
2745 stream_putl(s
, pbra
->table_id
);
2747 memset(ifname
, 0, sizeof(ifname
));
2748 stream_put(s
, ifname
, INTERFACE_NAMSIZ
); /* ifname unused */
2751 static void bgp_encode_pbr_ipset_match(struct stream
*s
,
2752 struct bgp_pbr_match
*pbim
)
2754 stream_putl(s
, pbim
->unique
);
2755 stream_putl(s
, pbim
->type
);
2756 stream_putc(s
, pbim
->family
);
2757 stream_put(s
, pbim
->ipset_name
,
2758 ZEBRA_IPSET_NAME_SIZE
);
2761 static void bgp_encode_pbr_ipset_entry_match(struct stream
*s
,
2762 struct bgp_pbr_match_entry
*pbime
)
2764 stream_putl(s
, pbime
->unique
);
2765 /* check that back pointer is not null */
2766 stream_put(s
, pbime
->backpointer
->ipset_name
,
2767 ZEBRA_IPSET_NAME_SIZE
);
2769 stream_putc(s
, pbime
->src
.family
);
2770 stream_putc(s
, pbime
->src
.prefixlen
);
2771 stream_put(s
, &pbime
->src
.u
.prefix
, prefix_blen(&pbime
->src
));
2773 stream_putc(s
, pbime
->dst
.family
);
2774 stream_putc(s
, pbime
->dst
.prefixlen
);
2775 stream_put(s
, &pbime
->dst
.u
.prefix
, prefix_blen(&pbime
->dst
));
2777 stream_putw(s
, pbime
->src_port_min
);
2778 stream_putw(s
, pbime
->src_port_max
);
2779 stream_putw(s
, pbime
->dst_port_min
);
2780 stream_putw(s
, pbime
->dst_port_max
);
2781 stream_putc(s
, pbime
->proto
);
2784 static void bgp_encode_pbr_iptable_match(struct stream
*s
,
2785 struct bgp_pbr_action
*bpa
,
2786 struct bgp_pbr_match
*pbm
)
2788 stream_putl(s
, pbm
->unique2
);
2790 stream_putl(s
, pbm
->type
);
2792 stream_putl(s
, pbm
->flags
);
2794 /* TODO: correlate with what is contained
2795 * into bgp_pbr_action.
2796 * currently only forward supported
2798 if (bpa
->nh
.type
== NEXTHOP_TYPE_BLACKHOLE
)
2799 stream_putl(s
, ZEBRA_IPTABLES_DROP
);
2801 stream_putl(s
, ZEBRA_IPTABLES_FORWARD
);
2802 stream_putl(s
, bpa
->fwmark
);
2803 stream_put(s
, pbm
->ipset_name
,
2804 ZEBRA_IPSET_NAME_SIZE
);
2805 stream_putc(s
, pbm
->family
);
2806 stream_putw(s
, pbm
->pkt_len_min
);
2807 stream_putw(s
, pbm
->pkt_len_max
);
2808 stream_putw(s
, pbm
->tcp_flags
);
2809 stream_putw(s
, pbm
->tcp_mask_flags
);
2810 stream_putc(s
, pbm
->dscp_value
);
2811 stream_putc(s
, pbm
->fragment
);
2812 stream_putc(s
, pbm
->protocol
);
2813 stream_putw(s
, pbm
->flow_label
);
2816 /* BGP has established connection with Zebra. */
2817 static void bgp_zebra_connected(struct zclient
*zclient
)
2821 zclient_num_connects
++; /* increment even if not responding */
2823 /* Send the client registration */
2824 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
2826 /* At this point, we may or may not have BGP instances configured, but
2827 * we're only interested in the default VRF (others wouldn't have learnt
2828 * the VRF from Zebra yet.)
2830 bgp
= bgp_get_default();
2834 bgp_zebra_instance_register(bgp
);
2836 /* tell label pool that zebra is connected */
2837 bgp_lp_event_zebra_up();
2839 /* TODO - What if we have peers and networks configured, do we have to
2842 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp
, bgp
->peer
);
2845 static int bgp_zebra_process_local_es_add(ZAPI_CALLBACK_ARGS
)
2848 struct bgp
*bgp
= NULL
;
2849 struct stream
*s
= NULL
;
2850 char buf
[ESI_STR_LEN
];
2851 struct in_addr originator_ip
;
2856 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2861 stream_get(&esi
, s
, sizeof(esi_t
));
2862 originator_ip
.s_addr
= stream_get_ipv4(s
);
2863 active
= stream_getc(s
);
2864 df_pref
= stream_getw(s
);
2865 bypass
= stream_getc(s
);
2867 if (BGP_DEBUG(zebra
, ZEBRA
))
2869 "Rx add ESI %s originator-ip %pI4 active %u df_pref %u %s",
2870 esi_to_str(&esi
, buf
, sizeof(buf
)), &originator_ip
,
2871 active
, df_pref
, bypass
? "bypass" : "");
2873 frrtrace(5, frr_bgp
, evpn_mh_local_es_add_zrecv
, &esi
, originator_ip
,
2874 active
, bypass
, df_pref
);
2876 bgp_evpn_local_es_add(bgp
, &esi
, originator_ip
, active
, df_pref
,
2882 static int bgp_zebra_process_local_es_del(ZAPI_CALLBACK_ARGS
)
2885 struct bgp
*bgp
= NULL
;
2886 struct stream
*s
= NULL
;
2887 char buf
[ESI_STR_LEN
];
2889 memset(&esi
, 0, sizeof(esi_t
));
2890 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2895 stream_get(&esi
, s
, sizeof(esi_t
));
2897 if (BGP_DEBUG(zebra
, ZEBRA
))
2898 zlog_debug("Rx del ESI %s",
2899 esi_to_str(&esi
, buf
, sizeof(buf
)));
2901 frrtrace(1, frr_bgp
, evpn_mh_local_es_del_zrecv
, &esi
);
2903 bgp_evpn_local_es_del(bgp
, &esi
);
2908 static int bgp_zebra_process_local_es_evi(ZAPI_CALLBACK_ARGS
)
2914 char buf
[ESI_STR_LEN
];
2916 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2921 stream_get(&esi
, s
, sizeof(esi_t
));
2922 vni
= stream_getl(s
);
2924 if (BGP_DEBUG(zebra
, ZEBRA
))
2925 zlog_debug("Rx %s ESI %s VNI %u",
2926 (cmd
== ZEBRA_VNI_ADD
) ? "add" : "del",
2927 esi_to_str(&esi
, buf
, sizeof(buf
)), vni
);
2929 if (cmd
== ZEBRA_LOCAL_ES_EVI_ADD
) {
2930 frrtrace(2, frr_bgp
, evpn_mh_local_es_evi_add_zrecv
, &esi
, vni
);
2932 bgp_evpn_local_es_evi_add(bgp
, &esi
, vni
);
2934 frrtrace(2, frr_bgp
, evpn_mh_local_es_evi_del_zrecv
, &esi
, vni
);
2936 bgp_evpn_local_es_evi_del(bgp
, &esi
, vni
);
2942 static int bgp_zebra_process_local_l3vni(ZAPI_CALLBACK_ARGS
)
2946 struct ethaddr svi_rmac
, vrr_rmac
= {.octet
= {0} };
2947 struct in_addr originator_ip
;
2949 ifindex_t svi_ifindex
;
2950 bool is_anycast_mac
= false;
2952 memset(&svi_rmac
, 0, sizeof(svi_rmac
));
2953 memset(&originator_ip
, 0, sizeof(originator_ip
));
2955 l3vni
= stream_getl(s
);
2956 if (cmd
== ZEBRA_L3VNI_ADD
) {
2957 stream_get(&svi_rmac
, s
, sizeof(struct ethaddr
));
2958 originator_ip
.s_addr
= stream_get_ipv4(s
);
2959 stream_get(&filter
, s
, sizeof(int));
2960 svi_ifindex
= stream_getl(s
);
2961 stream_get(&vrr_rmac
, s
, sizeof(struct ethaddr
));
2962 is_anycast_mac
= stream_getl(s
);
2964 if (BGP_DEBUG(zebra
, ZEBRA
))
2966 "Rx L3-VNI ADD VRF %s VNI %u RMAC svi-mac %pEA vrr-mac %pEA filter %s svi-if %u",
2967 vrf_id_to_name(vrf_id
), l3vni
, &svi_rmac
,
2969 filter
? "prefix-routes-only" : "none",
2972 frrtrace(8, frr_bgp
, evpn_local_l3vni_add_zrecv
, l3vni
, vrf_id
,
2973 &svi_rmac
, &vrr_rmac
, filter
, originator_ip
,
2974 svi_ifindex
, is_anycast_mac
);
2976 bgp_evpn_local_l3vni_add(l3vni
, vrf_id
, &svi_rmac
, &vrr_rmac
,
2977 originator_ip
, filter
, svi_ifindex
,
2980 if (BGP_DEBUG(zebra
, ZEBRA
))
2981 zlog_debug("Rx L3-VNI DEL VRF %s VNI %u",
2982 vrf_id_to_name(vrf_id
), l3vni
);
2984 frrtrace(2, frr_bgp
, evpn_local_l3vni_del_zrecv
, l3vni
, vrf_id
);
2986 bgp_evpn_local_l3vni_del(l3vni
, vrf_id
);
2992 static int bgp_zebra_process_local_vni(ZAPI_CALLBACK_ARGS
)
2997 struct in_addr vtep_ip
= {INADDR_ANY
};
2998 vrf_id_t tenant_vrf_id
= VRF_DEFAULT
;
2999 struct in_addr mcast_grp
= {INADDR_ANY
};
3000 ifindex_t svi_ifindex
= 0;
3003 vni
= stream_getl(s
);
3004 if (cmd
== ZEBRA_VNI_ADD
) {
3005 vtep_ip
.s_addr
= stream_get_ipv4(s
);
3006 stream_get(&tenant_vrf_id
, s
, sizeof(vrf_id_t
));
3007 mcast_grp
.s_addr
= stream_get_ipv4(s
);
3008 stream_get(&svi_ifindex
, s
, sizeof(ifindex_t
));
3011 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
3015 if (BGP_DEBUG(zebra
, ZEBRA
))
3017 "Rx VNI %s VRF %s VNI %u tenant-vrf %s SVI ifindex %u",
3018 (cmd
== ZEBRA_VNI_ADD
) ? "add" : "del",
3019 vrf_id_to_name(vrf_id
), vni
,
3020 vrf_id_to_name(tenant_vrf_id
), svi_ifindex
);
3022 if (cmd
== ZEBRA_VNI_ADD
) {
3023 frrtrace(4, frr_bgp
, evpn_local_vni_add_zrecv
, vni
, vtep_ip
,
3024 tenant_vrf_id
, mcast_grp
);
3026 return bgp_evpn_local_vni_add(
3028 vtep_ip
.s_addr
!= INADDR_ANY
? vtep_ip
: bgp
->router_id
,
3029 tenant_vrf_id
, mcast_grp
, svi_ifindex
);
3031 frrtrace(1, frr_bgp
, evpn_local_vni_del_zrecv
, vni
);
3033 return bgp_evpn_local_vni_del(bgp
, vni
);
3037 static int bgp_zebra_process_local_macip(ZAPI_CALLBACK_ARGS
)
3046 uint32_t seqnum
= 0;
3048 char buf2
[ESI_STR_LEN
];
3051 memset(&ip
, 0, sizeof(ip
));
3053 vni
= stream_getl(s
);
3054 stream_get(&mac
.octet
, s
, ETH_ALEN
);
3055 ipa_len
= stream_getl(s
);
3056 if (ipa_len
!= 0 && ipa_len
!= IPV4_MAX_BYTELEN
3057 && ipa_len
!= IPV6_MAX_BYTELEN
) {
3058 flog_err(EC_BGP_MACIP_LEN
,
3059 "%u:Recv MACIP %s with invalid IP addr length %d",
3060 vrf_id
, (cmd
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
3067 (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
: IPADDR_V6
;
3068 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
3070 if (cmd
== ZEBRA_MACIP_ADD
) {
3071 flags
= stream_getc(s
);
3072 seqnum
= stream_getl(s
);
3073 stream_get(&esi
, s
, sizeof(esi_t
));
3075 state
= stream_getl(s
);
3076 memset(&esi
, 0, sizeof(esi_t
));
3079 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
3083 if (BGP_DEBUG(zebra
, ZEBRA
))
3085 "%u:Recv MACIP %s f 0x%x MAC %pEA IP %pIA VNI %u seq %u state %d ESI %s",
3086 vrf_id
, (cmd
== ZEBRA_MACIP_ADD
) ? "Add" : "Del", flags
,
3087 &mac
, &ip
, vni
, seqnum
, state
,
3088 esi_to_str(&esi
, buf2
, sizeof(buf2
)));
3090 if (cmd
== ZEBRA_MACIP_ADD
) {
3091 frrtrace(6, frr_bgp
, evpn_local_macip_add_zrecv
, vni
, &mac
, &ip
,
3092 flags
, seqnum
, &esi
);
3094 return bgp_evpn_local_macip_add(bgp
, vni
, &mac
, &ip
,
3095 flags
, seqnum
, &esi
);
3097 frrtrace(4, frr_bgp
, evpn_local_macip_del_zrecv
, vni
, &mac
, &ip
,
3100 return bgp_evpn_local_macip_del(bgp
, vni
, &mac
, &ip
, state
);
3104 static int bgp_zebra_process_local_ip_prefix(ZAPI_CALLBACK_ARGS
)
3106 struct stream
*s
= NULL
;
3107 struct bgp
*bgp_vrf
= NULL
;
3110 memset(&p
, 0, sizeof(p
));
3112 stream_get(&p
, s
, sizeof(struct prefix
));
3114 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
3118 if (BGP_DEBUG(zebra
, ZEBRA
))
3119 zlog_debug("Recv prefix %pFX %s on vrf %s", &p
,
3120 (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) ? "ADD" : "DEL",
3121 vrf_id_to_name(vrf_id
));
3123 if (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) {
3125 if (p
.family
== AF_INET
)
3126 bgp_evpn_advertise_type5_route(bgp_vrf
, &p
, NULL
,
3127 AFI_IP
, SAFI_UNICAST
);
3129 bgp_evpn_advertise_type5_route(bgp_vrf
, &p
, NULL
,
3130 AFI_IP6
, SAFI_UNICAST
);
3133 if (p
.family
== AF_INET
)
3134 bgp_evpn_withdraw_type5_route(bgp_vrf
, &p
, AFI_IP
,
3137 bgp_evpn_withdraw_type5_route(bgp_vrf
, &p
, AFI_IP6
,
3143 static int bgp_zebra_process_label_chunk(ZAPI_CALLBACK_ARGS
)
3145 struct stream
*s
= NULL
;
3146 uint8_t response_keep
;
3150 unsigned short instance
;
3153 STREAM_GETC(s
, proto
);
3154 STREAM_GETW(s
, instance
);
3155 STREAM_GETC(s
, response_keep
);
3156 STREAM_GETL(s
, first
);
3157 STREAM_GETL(s
, last
);
3159 if (zclient
->redist_default
!= proto
) {
3160 flog_err(EC_BGP_LM_ERROR
, "Got LM msg with wrong proto %u",
3164 if (zclient
->instance
!= instance
) {
3165 flog_err(EC_BGP_LM_ERROR
, "Got LM msg with wrong instance %u",
3171 first
< MPLS_LABEL_UNRESERVED_MIN
||
3172 last
> MPLS_LABEL_UNRESERVED_MAX
) {
3174 flog_err(EC_BGP_LM_ERROR
, "%s: Invalid Label chunk: %u - %u",
3175 __func__
, first
, last
);
3178 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3179 zlog_debug("Label Chunk assign: %u - %u (%u) ",
3180 first
, last
, response_keep
);
3183 bgp_lp_event_chunk(response_keep
, first
, last
);
3187 stream_failure
: /* for STREAM_GETX */
3191 extern struct zebra_privs_t bgpd_privs
;
3193 static int bgp_ifp_create(struct interface
*ifp
)
3197 if (BGP_DEBUG(zebra
, ZEBRA
))
3198 zlog_debug("Rx Intf add VRF %u IF %s", ifp
->vrf
->vrf_id
,
3201 bgp
= ifp
->vrf
->info
;
3205 bgp_mac_add_mac_entry(ifp
);
3207 bgp_update_interface_nbrs(bgp
, ifp
, ifp
);
3208 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
3212 static int bgp_zebra_process_srv6_locator_chunk(ZAPI_CALLBACK_ARGS
)
3214 struct stream
*s
= NULL
;
3215 struct bgp
*bgp
= bgp_get_default();
3216 struct listnode
*node
;
3217 struct srv6_locator_chunk
*c
;
3218 struct srv6_locator_chunk
*chunk
= srv6_locator_chunk_alloc();
3221 zapi_srv6_locator_chunk_decode(s
, chunk
);
3223 if (strcmp(bgp
->srv6_locator_name
, chunk
->locator_name
) != 0) {
3224 zlog_err("%s: Locator name unmatch %s:%s", __func__
,
3225 bgp
->srv6_locator_name
, chunk
->locator_name
);
3226 srv6_locator_chunk_free(&chunk
);
3230 for (ALL_LIST_ELEMENTS_RO(bgp
->srv6_locator_chunks
, node
, c
)) {
3231 if (!prefix_cmp(&c
->prefix
, &chunk
->prefix
)) {
3232 srv6_locator_chunk_free(&chunk
);
3237 listnode_add(bgp
->srv6_locator_chunks
, chunk
);
3238 vpn_leak_postchange_all();
3242 static int bgp_zebra_process_srv6_locator_add(ZAPI_CALLBACK_ARGS
)
3244 struct srv6_locator loc
= {};
3245 struct bgp
*bgp
= bgp_get_default();
3246 const char *loc_name
= bgp
->srv6_locator_name
;
3248 if (zapi_srv6_locator_decode(zclient
->ibuf
, &loc
) < 0)
3251 if (!bgp
|| !bgp
->srv6_enabled
)
3254 if (bgp_zebra_srv6_manager_get_locator_chunk(loc_name
) < 0)
3260 static int bgp_zebra_process_srv6_locator_delete(ZAPI_CALLBACK_ARGS
)
3262 struct srv6_locator loc
= {};
3263 struct bgp
*bgp
= bgp_get_default();
3264 struct listnode
*node
, *nnode
;
3265 struct srv6_locator_chunk
*chunk
, *tovpn_sid_locator
;
3266 struct bgp_srv6_function
*func
;
3267 struct bgp
*bgp_vrf
;
3268 struct in6_addr
*tovpn_sid
;
3269 struct prefix_ipv6 tmp_prefi
;
3271 if (zapi_srv6_locator_decode(zclient
->ibuf
, &loc
) < 0)
3275 for (ALL_LIST_ELEMENTS(bgp
->srv6_locator_chunks
, node
, nnode
, chunk
))
3276 if (prefix_match((struct prefix
*)&loc
.prefix
,
3277 (struct prefix
*)&chunk
->prefix
)) {
3278 listnode_delete(bgp
->srv6_locator_chunks
, chunk
);
3279 srv6_locator_chunk_free(&chunk
);
3282 // refresh functions
3283 for (ALL_LIST_ELEMENTS(bgp
->srv6_functions
, node
, nnode
, func
)) {
3284 tmp_prefi
.family
= AF_INET6
;
3285 tmp_prefi
.prefixlen
= 128;
3286 tmp_prefi
.prefix
= func
->sid
;
3287 if (prefix_match((struct prefix
*)&loc
.prefix
,
3288 (struct prefix
*)&tmp_prefi
)) {
3289 listnode_delete(bgp
->srv6_functions
, func
);
3290 XFREE(MTYPE_BGP_SRV6_FUNCTION
, func
);
3294 // refresh tovpn_sid
3295 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
3296 if (bgp_vrf
->inst_type
!= BGP_INSTANCE_TYPE_VRF
)
3299 // refresh vpnv4 tovpn_sid
3300 tovpn_sid
= bgp_vrf
->vpn_policy
[AFI_IP
].tovpn_sid
;
3302 tmp_prefi
.family
= AF_INET6
;
3303 tmp_prefi
.prefixlen
= 128;
3304 tmp_prefi
.prefix
= *tovpn_sid
;
3305 if (prefix_match((struct prefix
*)&loc
.prefix
,
3306 (struct prefix
*)&tmp_prefi
))
3307 XFREE(MTYPE_BGP_SRV6_SID
,
3308 bgp_vrf
->vpn_policy
[AFI_IP
].tovpn_sid
);
3311 // refresh vpnv6 tovpn_sid
3312 tovpn_sid
= bgp_vrf
->vpn_policy
[AFI_IP6
].tovpn_sid
;
3314 tmp_prefi
.family
= AF_INET6
;
3315 tmp_prefi
.prefixlen
= 128;
3316 tmp_prefi
.prefix
= *tovpn_sid
;
3317 if (prefix_match((struct prefix
*)&loc
.prefix
,
3318 (struct prefix
*)&tmp_prefi
))
3319 XFREE(MTYPE_BGP_SRV6_SID
,
3320 bgp_vrf
->vpn_policy
[AFI_IP6
].tovpn_sid
);
3323 /* refresh per-vrf tovpn_sid */
3324 tovpn_sid
= bgp_vrf
->tovpn_sid
;
3326 tmp_prefi
.family
= AF_INET6
;
3327 tmp_prefi
.prefixlen
= IPV6_MAX_BITLEN
;
3328 tmp_prefi
.prefix
= *tovpn_sid
;
3329 if (prefix_match((struct prefix
*)&loc
.prefix
,
3330 (struct prefix
*)&tmp_prefi
))
3331 XFREE(MTYPE_BGP_SRV6_SID
, bgp_vrf
->tovpn_sid
);
3335 vpn_leak_postchange_all();
3337 /* refresh tovpn_sid_locator */
3338 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
3339 if (bgp_vrf
->inst_type
!= BGP_INSTANCE_TYPE_VRF
)
3342 /* refresh vpnv4 tovpn_sid_locator */
3344 bgp_vrf
->vpn_policy
[AFI_IP
].tovpn_sid_locator
;
3345 if (tovpn_sid_locator
) {
3346 tmp_prefi
.family
= AF_INET6
;
3347 tmp_prefi
.prefixlen
= IPV6_MAX_BITLEN
;
3348 tmp_prefi
.prefix
= tovpn_sid_locator
->prefix
.prefix
;
3349 if (prefix_match((struct prefix
*)&loc
.prefix
,
3350 (struct prefix
*)&tmp_prefi
))
3351 srv6_locator_chunk_free(
3352 &bgp_vrf
->vpn_policy
[AFI_IP
]
3353 .tovpn_sid_locator
);
3356 /* refresh vpnv6 tovpn_sid_locator */
3358 bgp_vrf
->vpn_policy
[AFI_IP6
].tovpn_sid_locator
;
3359 if (tovpn_sid_locator
) {
3360 tmp_prefi
.family
= AF_INET6
;
3361 tmp_prefi
.prefixlen
= IPV6_MAX_BITLEN
;
3362 tmp_prefi
.prefix
= tovpn_sid_locator
->prefix
.prefix
;
3363 if (prefix_match((struct prefix
*)&loc
.prefix
,
3364 (struct prefix
*)&tmp_prefi
))
3365 srv6_locator_chunk_free(
3366 &bgp_vrf
->vpn_policy
[AFI_IP6
]
3367 .tovpn_sid_locator
);
3370 /* refresh per-vrf tovpn_sid_locator */
3371 tovpn_sid_locator
= bgp_vrf
->tovpn_sid_locator
;
3372 if (tovpn_sid_locator
) {
3373 tmp_prefi
.family
= AF_INET6
;
3374 tmp_prefi
.prefixlen
= IPV6_MAX_BITLEN
;
3375 tmp_prefi
.prefix
= tovpn_sid_locator
->prefix
.prefix
;
3376 if (prefix_match((struct prefix
*)&loc
.prefix
,
3377 (struct prefix
*)&tmp_prefi
))
3378 srv6_locator_chunk_free(
3379 &bgp_vrf
->tovpn_sid_locator
);
3386 static zclient_handler
*const bgp_handlers
[] = {
3387 [ZEBRA_ROUTER_ID_UPDATE
] = bgp_router_id_update
,
3388 [ZEBRA_INTERFACE_ADDRESS_ADD
] = bgp_interface_address_add
,
3389 [ZEBRA_INTERFACE_ADDRESS_DELETE
] = bgp_interface_address_delete
,
3390 [ZEBRA_INTERFACE_NBR_ADDRESS_ADD
] = bgp_interface_nbr_address_add
,
3391 [ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
] = bgp_interface_nbr_address_delete
,
3392 [ZEBRA_INTERFACE_VRF_UPDATE
] = bgp_interface_vrf_update
,
3393 [ZEBRA_REDISTRIBUTE_ROUTE_ADD
] = zebra_read_route
,
3394 [ZEBRA_REDISTRIBUTE_ROUTE_DEL
] = zebra_read_route
,
3395 [ZEBRA_NEXTHOP_UPDATE
] = bgp_read_nexthop_update
,
3396 [ZEBRA_FEC_UPDATE
] = bgp_read_fec_update
,
3397 [ZEBRA_LOCAL_ES_ADD
] = bgp_zebra_process_local_es_add
,
3398 [ZEBRA_LOCAL_ES_DEL
] = bgp_zebra_process_local_es_del
,
3399 [ZEBRA_VNI_ADD
] = bgp_zebra_process_local_vni
,
3400 [ZEBRA_LOCAL_ES_EVI_ADD
] = bgp_zebra_process_local_es_evi
,
3401 [ZEBRA_LOCAL_ES_EVI_DEL
] = bgp_zebra_process_local_es_evi
,
3402 [ZEBRA_VNI_DEL
] = bgp_zebra_process_local_vni
,
3403 [ZEBRA_MACIP_ADD
] = bgp_zebra_process_local_macip
,
3404 [ZEBRA_MACIP_DEL
] = bgp_zebra_process_local_macip
,
3405 [ZEBRA_L3VNI_ADD
] = bgp_zebra_process_local_l3vni
,
3406 [ZEBRA_L3VNI_DEL
] = bgp_zebra_process_local_l3vni
,
3407 [ZEBRA_IP_PREFIX_ROUTE_ADD
] = bgp_zebra_process_local_ip_prefix
,
3408 [ZEBRA_IP_PREFIX_ROUTE_DEL
] = bgp_zebra_process_local_ip_prefix
,
3409 [ZEBRA_GET_LABEL_CHUNK
] = bgp_zebra_process_label_chunk
,
3410 [ZEBRA_RULE_NOTIFY_OWNER
] = rule_notify_owner
,
3411 [ZEBRA_IPSET_NOTIFY_OWNER
] = ipset_notify_owner
,
3412 [ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
] = ipset_entry_notify_owner
,
3413 [ZEBRA_IPTABLE_NOTIFY_OWNER
] = iptable_notify_owner
,
3414 [ZEBRA_ROUTE_NOTIFY_OWNER
] = bgp_zebra_route_notify_owner
,
3415 [ZEBRA_SRV6_LOCATOR_ADD
] = bgp_zebra_process_srv6_locator_add
,
3416 [ZEBRA_SRV6_LOCATOR_DELETE
] = bgp_zebra_process_srv6_locator_delete
,
3417 [ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK
] =
3418 bgp_zebra_process_srv6_locator_chunk
,
3421 static int bgp_if_new_hook(struct interface
*ifp
)
3423 struct bgp_interface
*iifp
;
3427 iifp
= XCALLOC(MTYPE_BGP_IF_INFO
, sizeof(struct bgp_interface
));
3433 static int bgp_if_delete_hook(struct interface
*ifp
)
3435 XFREE(MTYPE_BGP_IF_INFO
, ifp
->info
);
3439 void bgp_if_init(void)
3441 /* Initialize Zebra interface data structure. */
3442 hook_register_prio(if_add
, 0, bgp_if_new_hook
);
3443 hook_register_prio(if_del
, 0, bgp_if_delete_hook
);
3446 void bgp_zebra_init(struct thread_master
*master
, unsigned short instance
)
3448 zclient_num_connects
= 0;
3450 if_zapi_callbacks(bgp_ifp_create
, bgp_ifp_up
,
3451 bgp_ifp_down
, bgp_ifp_destroy
);
3453 /* Set default values. */
3454 zclient
= zclient_new(master
, &zclient_options_default
, bgp_handlers
,
3455 array_size(bgp_handlers
));
3456 zclient_init(zclient
, ZEBRA_ROUTE_BGP
, 0, &bgpd_privs
);
3457 zclient
->zebra_connected
= bgp_zebra_connected
;
3458 zclient
->instance
= instance
;
3461 void bgp_zebra_destroy(void)
3463 if (zclient
== NULL
)
3465 zclient_stop(zclient
);
3466 zclient_free(zclient
);
3470 int bgp_zebra_num_connects(void)
3472 return zclient_num_connects
;
3475 void bgp_send_pbr_rule_action(struct bgp_pbr_action
*pbra
,
3476 struct bgp_pbr_rule
*pbr
,
3481 if (pbra
->install_in_progress
&& !pbr
)
3483 if (pbr
&& pbr
->install_in_progress
)
3485 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3487 zlog_debug("%s: table %d (ip rule) %d", __func__
,
3488 pbra
->table_id
, install
);
3490 zlog_debug("%s: table %d fwmark %d %d", __func__
,
3491 pbra
->table_id
, pbra
->fwmark
, install
);
3496 zclient_create_header(s
,
3497 install
? ZEBRA_RULE_ADD
: ZEBRA_RULE_DELETE
,
3499 stream_putl(s
, 1); /* send one pbr action */
3501 bgp_encode_pbr_rule_action(s
, pbra
, pbr
);
3503 stream_putw_at(s
, 0, stream_get_endp(s
));
3504 if ((zclient_send_message(zclient
) != ZCLIENT_SEND_FAILURE
)
3507 pbra
->install_in_progress
= true;
3509 pbr
->install_in_progress
= true;
3513 void bgp_send_pbr_ipset_match(struct bgp_pbr_match
*pbrim
, bool install
)
3517 if (pbrim
->install_in_progress
)
3519 if (BGP_DEBUG(zebra
, ZEBRA
))
3520 zlog_debug("%s: name %s type %d %d, ID %u", __func__
,
3521 pbrim
->ipset_name
, pbrim
->type
, install
,
3526 zclient_create_header(s
,
3527 install
? ZEBRA_IPSET_CREATE
:
3528 ZEBRA_IPSET_DESTROY
,
3531 stream_putl(s
, 1); /* send one pbr action */
3533 bgp_encode_pbr_ipset_match(s
, pbrim
);
3535 stream_putw_at(s
, 0, stream_get_endp(s
));
3536 if ((zclient_send_message(zclient
) != ZCLIENT_SEND_FAILURE
) && install
)
3537 pbrim
->install_in_progress
= true;
3540 void bgp_send_pbr_ipset_entry_match(struct bgp_pbr_match_entry
*pbrime
,
3545 if (pbrime
->install_in_progress
)
3547 if (BGP_DEBUG(zebra
, ZEBRA
))
3548 zlog_debug("%s: name %s %d %d, ID %u", __func__
,
3549 pbrime
->backpointer
->ipset_name
, pbrime
->unique
,
3550 install
, pbrime
->unique
);
3554 zclient_create_header(s
,
3555 install
? ZEBRA_IPSET_ENTRY_ADD
:
3556 ZEBRA_IPSET_ENTRY_DELETE
,
3559 stream_putl(s
, 1); /* send one pbr action */
3561 bgp_encode_pbr_ipset_entry_match(s
, pbrime
);
3563 stream_putw_at(s
, 0, stream_get_endp(s
));
3564 if ((zclient_send_message(zclient
) != ZCLIENT_SEND_FAILURE
) && install
)
3565 pbrime
->install_in_progress
= true;
3568 static void bgp_encode_pbr_interface_list(struct bgp
*bgp
, struct stream
*s
,
3571 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
3572 struct bgp_pbr_interface_head
*head
;
3573 struct bgp_pbr_interface
*pbr_if
;
3574 struct interface
*ifp
;
3578 if (family
== AF_INET
)
3579 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
3581 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv6
);
3582 RB_FOREACH (pbr_if
, bgp_pbr_interface_head
, head
) {
3583 ifp
= if_lookup_by_name(pbr_if
->name
, bgp
->vrf_id
);
3585 stream_putl(s
, ifp
->ifindex
);
3589 static int bgp_pbr_get_ifnumber(struct bgp
*bgp
, uint8_t family
)
3591 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
3592 struct bgp_pbr_interface_head
*head
;
3593 struct bgp_pbr_interface
*pbr_if
;
3598 if (family
== AF_INET
)
3599 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
3601 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv6
);
3602 RB_FOREACH (pbr_if
, bgp_pbr_interface_head
, head
) {
3603 if (if_lookup_by_name(pbr_if
->name
, bgp
->vrf_id
))
3609 void bgp_send_pbr_iptable(struct bgp_pbr_action
*pba
,
3610 struct bgp_pbr_match
*pbm
,
3617 if (pbm
->install_iptable_in_progress
)
3619 if (BGP_DEBUG(zebra
, ZEBRA
))
3620 zlog_debug("%s: name %s type %d mark %d %d, ID %u", __func__
,
3621 pbm
->ipset_name
, pbm
->type
, pba
->fwmark
, install
,
3626 zclient_create_header(s
,
3627 install
? ZEBRA_IPTABLE_ADD
:
3628 ZEBRA_IPTABLE_DELETE
,
3631 bgp_encode_pbr_iptable_match(s
, pba
, pbm
);
3632 nb_interface
= bgp_pbr_get_ifnumber(pba
->bgp
, pbm
->family
);
3633 stream_putl(s
, nb_interface
);
3635 bgp_encode_pbr_interface_list(pba
->bgp
, s
, pbm
->family
);
3636 stream_putw_at(s
, 0, stream_get_endp(s
));
3637 ret
= zclient_send_message(zclient
);
3639 if (ret
!= ZCLIENT_SEND_FAILURE
)
3642 pbm
->install_iptable_in_progress
= true;
3646 /* inject in table <table_id> a default route to:
3647 * - if nexthop IP is present : to this nexthop
3648 * - if vrf is different from local : to the matching VRF
3650 void bgp_zebra_announce_default(struct bgp
*bgp
, struct nexthop
*nh
,
3651 afi_t afi
, uint32_t table_id
, bool announce
)
3653 struct zapi_nexthop
*api_nh
;
3654 struct zapi_route api
;
3657 if (!nh
|| (nh
->type
!= NEXTHOP_TYPE_IPV4
3658 && nh
->type
!= NEXTHOP_TYPE_IPV6
)
3659 || nh
->vrf_id
== VRF_UNKNOWN
)
3662 /* in vrf-lite, no default route has to be announced
3663 * the table id of vrf is directly used to divert traffic
3665 if (!vrf_is_backend_netns() && bgp
->vrf_id
!= nh
->vrf_id
)
3668 memset(&p
, 0, sizeof(p
));
3669 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
)
3671 p
.family
= afi2family(afi
);
3672 memset(&api
, 0, sizeof(api
));
3673 api
.vrf_id
= bgp
->vrf_id
;
3674 api
.type
= ZEBRA_ROUTE_BGP
;
3675 api
.safi
= SAFI_UNICAST
;
3677 api
.tableid
= table_id
;
3678 api
.nexthop_num
= 1;
3679 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
3680 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
3681 api_nh
= &api
.nexthops
[0];
3683 api
.distance
= ZEBRA_EBGP_DISTANCE_DEFAULT
;
3684 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
3687 if (afi
== AFI_IP
&& nh
->gate
.ipv4
.s_addr
!= INADDR_ANY
) {
3688 api_nh
->vrf_id
= nh
->vrf_id
;
3689 api_nh
->gate
.ipv4
= nh
->gate
.ipv4
;
3690 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
3692 if (BGP_DEBUG(zebra
, ZEBRA
))
3694 "BGP: %s default route to %pI4 table %d (redirect IP)",
3695 announce
? "adding" : "withdrawing",
3696 &nh
->gate
.ipv4
, table_id
);
3698 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
3699 : ZEBRA_ROUTE_DELETE
,
3701 } else if (afi
== AFI_IP6
&&
3702 memcmp(&nh
->gate
.ipv6
,
3703 &in6addr_any
, sizeof(struct in6_addr
))) {
3704 api_nh
->vrf_id
= nh
->vrf_id
;
3705 memcpy(&api_nh
->gate
.ipv6
, &nh
->gate
.ipv6
,
3706 sizeof(struct in6_addr
));
3707 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
3709 if (BGP_DEBUG(zebra
, ZEBRA
))
3711 "BGP: %s default route to %pI6 table %d (redirect IP)",
3712 announce
? "adding" : "withdrawing",
3713 &nh
->gate
.ipv6
, table_id
);
3715 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
3716 : ZEBRA_ROUTE_DELETE
,
3718 } else if (nh
->vrf_id
!= bgp
->vrf_id
) {
3720 struct interface
*ifp
;
3722 vrf
= vrf_lookup_by_id(nh
->vrf_id
);
3725 /* create default route with interface <VRF>
3726 * with nexthop-vrf <VRF>
3728 ifp
= if_lookup_by_name_vrf(vrf
->name
, vrf
);
3731 api_nh
->vrf_id
= nh
->vrf_id
;
3732 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
3733 api_nh
->ifindex
= ifp
->ifindex
;
3734 if (BGP_DEBUG(zebra
, ZEBRA
))
3735 zlog_info("BGP: %s default route to %s table %d (redirect VRF)",
3736 announce
? "adding" : "withdrawing",
3737 vrf
->name
, table_id
);
3738 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
3739 : ZEBRA_ROUTE_DELETE
,
3745 /* Send capabilities to RIB */
3746 int bgp_zebra_send_capabilities(struct bgp
*bgp
, bool disable
)
3748 struct zapi_cap api
;
3749 int ret
= BGP_GR_SUCCESS
;
3751 if (zclient
== NULL
) {
3752 if (BGP_DEBUG(zebra
, ZEBRA
))
3753 zlog_debug("zclient invalid");
3754 return BGP_GR_FAILURE
;
3757 /* Check if the client is connected */
3758 if ((zclient
->sock
< 0) || (zclient
->t_connect
)) {
3759 if (BGP_DEBUG(zebra
, ZEBRA
))
3760 zlog_debug("client not connected");
3761 return BGP_GR_FAILURE
;
3764 /* Check if capability is already sent. If the flag force is set
3765 * send the capability since this can be initial bgp configuration
3767 memset(&api
, 0, sizeof(api
));
3769 api
.cap
= ZEBRA_CLIENT_GR_DISABLE
;
3770 api
.vrf_id
= bgp
->vrf_id
;
3772 api
.cap
= ZEBRA_CLIENT_GR_CAPABILITIES
;
3773 api
.stale_removal_time
= bgp
->rib_stale_time
;
3774 api
.vrf_id
= bgp
->vrf_id
;
3777 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
, &api
)
3778 == ZCLIENT_SEND_FAILURE
) {
3779 zlog_err("error sending capability");
3780 ret
= BGP_GR_FAILURE
;
3783 bgp
->present_zebra_gr_state
= ZEBRA_GR_DISABLE
;
3785 bgp
->present_zebra_gr_state
= ZEBRA_GR_ENABLE
;
3787 if (BGP_DEBUG(zebra
, ZEBRA
))
3788 zlog_debug("send capabilty success");
3789 ret
= BGP_GR_SUCCESS
;
3794 /* Send route update pesding or completed status to RIB for the
3795 * specific AFI, SAFI
3797 int bgp_zebra_update(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
)
3799 struct zapi_cap api
= {0};
3801 if (zclient
== NULL
) {
3802 if (BGP_DEBUG(zebra
, ZEBRA
))
3803 zlog_debug("zclient == NULL, invalid");
3804 return BGP_GR_FAILURE
;
3807 /* Check if the client is connected */
3808 if ((zclient
->sock
< 0) || (zclient
->t_connect
)) {
3809 if (BGP_DEBUG(zebra
, ZEBRA
))
3810 zlog_debug("client not connected");
3811 return BGP_GR_FAILURE
;
3816 api
.vrf_id
= vrf_id
;
3819 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
, &api
)
3820 == ZCLIENT_SEND_FAILURE
) {
3821 if (BGP_DEBUG(zebra
, ZEBRA
))
3822 zlog_debug("error sending capability");
3823 return BGP_GR_FAILURE
;
3825 return BGP_GR_SUCCESS
;
3829 /* Send RIB stale timer update */
3830 int bgp_zebra_stale_timer_update(struct bgp
*bgp
)
3832 struct zapi_cap api
;
3834 if (zclient
== NULL
) {
3835 if (BGP_DEBUG(zebra
, ZEBRA
))
3836 zlog_debug("zclient invalid");
3837 return BGP_GR_FAILURE
;
3840 /* Check if the client is connected */
3841 if ((zclient
->sock
< 0) || (zclient
->t_connect
)) {
3842 if (BGP_DEBUG(zebra
, ZEBRA
))
3843 zlog_debug("client not connected");
3844 return BGP_GR_FAILURE
;
3847 memset(&api
, 0, sizeof(api
));
3848 api
.cap
= ZEBRA_CLIENT_RIB_STALE_TIME
;
3849 api
.stale_removal_time
= bgp
->rib_stale_time
;
3850 api
.vrf_id
= bgp
->vrf_id
;
3851 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
, &api
)
3852 == ZCLIENT_SEND_FAILURE
) {
3853 if (BGP_DEBUG(zebra
, ZEBRA
))
3854 zlog_debug("error sending capability");
3855 return BGP_GR_FAILURE
;
3857 if (BGP_DEBUG(zebra
, ZEBRA
))
3858 zlog_debug("send capabilty success");
3859 return BGP_GR_SUCCESS
;
3862 int bgp_zebra_srv6_manager_get_locator_chunk(const char *name
)
3864 return srv6_manager_get_locator_chunk(zclient
, name
);
3867 int bgp_zebra_srv6_manager_release_locator_chunk(const char *name
)
3869 return srv6_manager_release_locator_chunk(zclient
, name
);