2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "sockunion.h"
36 #include "lib/route_opaque.h"
42 #include "bgpd/bgpd.h"
43 #include "bgpd/bgp_route.h"
44 #include "bgpd/bgp_attr.h"
45 #include "bgpd/bgp_aspath.h"
46 #include "bgpd/bgp_nexthop.h"
47 #include "bgpd/bgp_zebra.h"
48 #include "bgpd/bgp_fsm.h"
49 #include "bgpd/bgp_debug.h"
50 #include "bgpd/bgp_errors.h"
51 #include "bgpd/bgp_mpath.h"
52 #include "bgpd/bgp_nexthop.h"
53 #include "bgpd/bgp_nht.h"
54 #include "bgpd/bgp_bfd.h"
55 #include "bgpd/bgp_label.h"
57 #include "bgpd/rfapi/rfapi_backend.h"
58 #include "bgpd/rfapi/vnc_export_bgp.h"
60 #include "bgpd/bgp_evpn.h"
61 #include "bgpd/bgp_mplsvpn.h"
62 #include "bgpd/bgp_labelpool.h"
63 #include "bgpd/bgp_pbr.h"
64 #include "bgpd/bgp_evpn_private.h"
65 #include "bgpd/bgp_evpn_mh.h"
66 #include "bgpd/bgp_mac.h"
67 #include "bgpd/bgp_trace.h"
68 #include "bgpd/bgp_community.h"
69 #include "bgpd/bgp_lcommunity.h"
70 #include "bgpd/bgp_orr.h"
72 /* All information about zebra. */
73 struct zclient
*zclient
= NULL
;
75 static int bgp_opaque_msg_handler(ZAPI_CALLBACK_ARGS
);
77 /* hook to indicate vrf status change for SNMP */
78 DEFINE_HOOK(bgp_vrf_status_changed
, (struct bgp
*bgp
, struct interface
*ifp
),
81 DEFINE_MTYPE_STATIC(BGPD
, BGP_IF_INFO
, "BGP interface context");
83 /* Can we install into zebra? */
84 static inline bool bgp_install_info_to_zebra(struct bgp
*bgp
)
86 if (zclient
->sock
<= 0)
89 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
91 "%s: No zebra instance to talk to, not installing information",
99 int zclient_num_connects
;
101 /* Router-id update message from zebra. */
102 static int bgp_router_id_update(ZAPI_CALLBACK_ARGS
)
104 struct prefix router_id
;
106 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
108 if (BGP_DEBUG(zebra
, ZEBRA
))
109 zlog_debug("Rx Router Id update VRF %u Id %pFX", vrf_id
,
112 bgp_router_id_zebra_bump(vrf_id
, &router_id
);
116 /* Nexthop update message from zebra. */
117 static int bgp_read_nexthop_update(ZAPI_CALLBACK_ARGS
)
119 bgp_parse_nexthop_update(cmd
, vrf_id
);
123 /* Set or clear interface on which unnumbered neighbor is configured. This
124 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
127 static void bgp_update_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
,
128 struct interface
*upd_ifp
)
130 struct listnode
*node
, *nnode
;
133 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
134 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)) {
137 bgp_zebra_initiate_radv(bgp
, peer
);
139 bgp_zebra_terminate_radv(bgp
, peer
);
146 static int bgp_read_fec_update(ZAPI_CALLBACK_ARGS
)
148 bgp_parse_fec_update();
152 static void bgp_start_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
)
154 struct listnode
*node
, *nnode
;
157 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
158 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)
159 && !peer_established(peer
)) {
160 if (peer_active(peer
))
161 BGP_EVENT_ADD(peer
, BGP_Stop
);
162 BGP_EVENT_ADD(peer
, BGP_Start
);
167 static void bgp_nbr_connected_add(struct bgp
*bgp
, struct nbr_connected
*ifc
)
169 struct listnode
*node
;
170 struct connected
*connected
;
171 struct interface
*ifp
;
174 /* Kick-off the FSM for any relevant peers only if there is a
175 * valid local address on the interface.
178 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
179 p
= connected
->address
;
180 if (p
->family
== AF_INET6
181 && IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
187 bgp_start_interface_nbrs(bgp
, ifp
);
190 static void bgp_nbr_connected_delete(struct bgp
*bgp
, struct nbr_connected
*ifc
,
193 struct listnode
*node
, *nnode
;
195 struct interface
*ifp
;
197 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
199 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)) {
200 peer
->last_reset
= PEER_DOWN_NBR_ADDR_DEL
;
201 BGP_EVENT_ADD(peer
, BGP_Stop
);
204 /* Free neighbor also, if we're asked to. */
207 listnode_delete(ifp
->nbr_connected
, ifc
);
208 nbr_connected_free(ifc
);
212 static int bgp_ifp_destroy(struct interface
*ifp
)
216 bgp
= ifp
->vrf
->info
;
218 if (BGP_DEBUG(zebra
, ZEBRA
))
219 zlog_debug("Rx Intf del VRF %u IF %s", ifp
->vrf
->vrf_id
,
223 bgp_update_interface_nbrs(bgp
, ifp
, NULL
);
224 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
227 bgp_mac_del_mac_entry(ifp
);
232 static int bgp_ifp_up(struct interface
*ifp
)
235 struct nbr_connected
*nc
;
236 struct listnode
*node
, *nnode
;
239 bgp
= ifp
->vrf
->info
;
241 bgp_mac_add_mac_entry(ifp
);
243 if (BGP_DEBUG(zebra
, ZEBRA
))
244 zlog_debug("Rx Intf up VRF %u IF %s", ifp
->vrf
->vrf_id
,
250 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
251 bgp_connected_add(bgp
, c
);
253 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
254 bgp_nbr_connected_add(bgp
, nc
);
256 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
262 static int bgp_ifp_down(struct interface
*ifp
)
265 struct nbr_connected
*nc
;
266 struct listnode
*node
, *nnode
;
270 bgp
= ifp
->vrf
->info
;
272 bgp_mac_del_mac_entry(ifp
);
274 if (BGP_DEBUG(zebra
, ZEBRA
))
275 zlog_debug("Rx Intf down VRF %u IF %s", ifp
->vrf
->vrf_id
,
281 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
282 bgp_connected_delete(bgp
, c
);
284 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
285 bgp_nbr_connected_delete(bgp
, nc
, 1);
287 /* Fast external-failover */
288 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
)) {
290 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
291 /* Take down directly connected peers. */
292 if ((peer
->ttl
!= BGP_DEFAULT_TTL
)
293 && (peer
->gtsm_hops
!= BGP_GTSM_HOPS_CONNECTED
))
296 if (ifp
== peer
->nexthop
.ifp
) {
297 BGP_EVENT_ADD(peer
, BGP_Stop
);
298 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
303 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
304 bgp_nht_ifp_down(ifp
);
309 static int bgp_interface_address_add(ZAPI_CALLBACK_ARGS
)
311 struct connected
*ifc
;
315 struct listnode
*node
, *nnode
;
319 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
321 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
326 if (bgp_debug_zebra(ifc
->address
))
327 zlog_debug("Rx Intf address add VRF %u IF %s addr %pFX", vrf_id
,
328 ifc
->ifp
->name
, ifc
->address
);
333 if (if_is_operative(ifc
->ifp
)) {
334 bgp_connected_add(bgp
, ifc
);
336 /* If we have learnt of any neighbors on this interface,
337 * check to kick off any BGP interface-based neighbors,
338 * but only if this is a link-local address.
340 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
)
341 && !list_isempty(ifc
->ifp
->nbr_connected
))
342 bgp_start_interface_nbrs(bgp
, ifc
->ifp
);
346 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
347 if (addr
->family
== AF_INET
)
351 * If the Peer's interface name matches the
352 * interface name for which BGP received the
353 * update and if the received interface address
354 * is a globalV6 and if the peer is currently
355 * using a v4-mapped-v6 addr or a link local
356 * address, then copy the Rxed global v6 addr
357 * into peer's v6_global and send updates out
358 * with new nexthop addr.
360 if ((peer
->conf_if
&&
361 (strcmp(peer
->conf_if
, ifc
->ifp
->name
) ==
363 !IN6_IS_ADDR_LINKLOCAL(&addr
->u
.prefix6
) &&
365 &peer
->nexthop
.v6_global
)) ||
366 IN6_IS_ADDR_LINKLOCAL(
367 &peer
->nexthop
.v6_global
))) {
369 if (bgp_debug_zebra(ifc
->address
)) {
371 "Update peer %pBP's current intf addr %pI6 and send updates",
376 memcpy(&peer
->nexthop
.v6_global
,
379 FOREACH_AFI_SAFI (afi
, safi
)
380 bgp_announce_route(peer
, afi
,
390 static int bgp_interface_address_delete(ZAPI_CALLBACK_ARGS
)
392 struct listnode
*node
, *nnode
;
393 struct connected
*ifc
;
398 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
400 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
405 if (bgp_debug_zebra(ifc
->address
))
406 zlog_debug("Rx Intf address del VRF %u IF %s addr %pFX", vrf_id
,
407 ifc
->ifp
->name
, ifc
->address
);
409 if (bgp
&& if_is_operative(ifc
->ifp
)) {
410 bgp_connected_delete(bgp
, ifc
);
417 * When we are using the v6 global as part of the peering
418 * nexthops and we are removing it, then we need to
419 * clear the peer data saved for that nexthop and
420 * cause a re-announcement of the route. Since
421 * we do not want the peering to bounce.
423 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
427 if (addr
->family
== AF_INET
)
430 if (!IN6_IS_ADDR_LINKLOCAL(&addr
->u
.prefix6
)
431 && memcmp(&peer
->nexthop
.v6_global
,
432 &addr
->u
.prefix6
, 16)
434 memset(&peer
->nexthop
.v6_global
, 0, 16);
435 FOREACH_AFI_SAFI (afi
, safi
)
436 bgp_announce_route(peer
, afi
, safi
,
442 connected_free(&ifc
);
447 static int bgp_interface_nbr_address_add(ZAPI_CALLBACK_ARGS
)
449 struct nbr_connected
*ifc
= NULL
;
452 ifc
= zebra_interface_nbr_address_read(cmd
, zclient
->ibuf
, vrf_id
);
457 if (bgp_debug_zebra(ifc
->address
))
458 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %pFX",
459 vrf_id
, ifc
->ifp
->name
, ifc
->address
);
461 if (if_is_operative(ifc
->ifp
)) {
462 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
464 bgp_nbr_connected_add(bgp
, ifc
);
470 static int bgp_interface_nbr_address_delete(ZAPI_CALLBACK_ARGS
)
472 struct nbr_connected
*ifc
= NULL
;
475 ifc
= zebra_interface_nbr_address_read(cmd
, zclient
->ibuf
, vrf_id
);
480 if (bgp_debug_zebra(ifc
->address
))
481 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %pFX",
482 vrf_id
, ifc
->ifp
->name
, ifc
->address
);
484 if (if_is_operative(ifc
->ifp
)) {
485 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
487 bgp_nbr_connected_delete(bgp
, ifc
, 0);
490 nbr_connected_free(ifc
);
495 /* VRF update for an interface. */
496 static int bgp_interface_vrf_update(ZAPI_CALLBACK_ARGS
)
498 struct interface
*ifp
;
501 struct nbr_connected
*nc
;
502 struct listnode
*node
, *nnode
;
506 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
511 if (BGP_DEBUG(zebra
, ZEBRA
))
512 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id
,
513 ifp
->name
, new_vrf_id
);
515 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
518 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
519 bgp_connected_delete(bgp
, c
);
521 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
522 bgp_nbr_connected_delete(bgp
, nc
, 1);
524 /* Fast external-failover */
525 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
)) {
526 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
527 if ((peer
->ttl
!= BGP_DEFAULT_TTL
)
529 != BGP_GTSM_HOPS_CONNECTED
))
532 if (ifp
== peer
->nexthop
.ifp
)
533 BGP_EVENT_ADD(peer
, BGP_Stop
);
538 if_update_to_new_vrf(ifp
, new_vrf_id
);
540 bgp
= bgp_lookup_by_vrf_id(new_vrf_id
);
544 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
545 bgp_connected_add(bgp
, c
);
547 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
548 bgp_nbr_connected_add(bgp
, nc
);
550 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
554 /* Zebra route add and delete treatment. */
555 static int zebra_read_route(ZAPI_CALLBACK_ARGS
)
557 enum nexthop_types_t nhtype
;
558 enum blackhole_type bhtype
= BLACKHOLE_UNSPEC
;
559 struct zapi_route api
;
560 union g_addr nexthop
= {};
565 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
569 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
572 /* we completely ignore srcdest routes for now. */
573 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
576 /* ignore link-local address. */
577 if (api
.prefix
.family
== AF_INET6
578 && IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
581 ifindex
= api
.nexthops
[0].ifindex
;
582 nhtype
= api
.nexthops
[0].type
;
584 /* api_nh structure has union of gate and bh_type */
585 if (nhtype
== NEXTHOP_TYPE_BLACKHOLE
) {
586 /* bh_type is only applicable if NEXTHOP_TYPE_BLACKHOLE*/
587 bhtype
= api
.nexthops
[0].bh_type
;
589 nexthop
= api
.nexthops
[0].gate
;
591 add
= (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
594 * The ADD message is actually an UPDATE and there is no
596 * for a prior redistributed route, if any. So, perform an
598 * DEL processing for the same redistributed route from any
602 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
604 bgp_redistribute_delete(bgp
, &api
.prefix
, i
,
608 /* Now perform the add/update. */
609 bgp_redistribute_add(bgp
, &api
.prefix
, &nexthop
, ifindex
,
610 nhtype
, bhtype
, api
.distance
, api
.metric
,
611 api
.type
, api
.instance
, api
.tag
);
613 bgp_redistribute_delete(bgp
, &api
.prefix
, api
.type
,
617 if (bgp_debug_zebra(&api
.prefix
)) {
618 char buf
[PREFIX_STRLEN
];
621 inet_ntop(api
.prefix
.family
, &nexthop
, buf
,
624 "Rx route ADD VRF %u %s[%d] %pFX nexthop %s (type %d if %u) metric %u distance %u tag %" ROUTE_TAG_PRI
,
625 vrf_id
, zebra_route_string(api
.type
),
626 api
.instance
, &api
.prefix
, buf
, nhtype
, ifindex
,
627 api
.metric
, api
.distance
, api
.tag
);
629 zlog_debug("Rx route DEL VRF %u %s[%d] %pFX", vrf_id
,
630 zebra_route_string(api
.type
), api
.instance
,
638 struct interface
*if_lookup_by_ipv4(struct in_addr
*addr
, vrf_id_t vrf_id
)
641 struct listnode
*cnode
;
642 struct interface
*ifp
;
643 struct connected
*connected
;
644 struct prefix_ipv4 p
;
647 vrf
= vrf_lookup_by_id(vrf_id
);
653 p
.prefixlen
= IPV4_MAX_BITLEN
;
655 FOR_ALL_INTERFACES (vrf
, ifp
) {
656 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
657 cp
= connected
->address
;
659 if (cp
->family
== AF_INET
)
660 if (prefix_match(cp
, (struct prefix
*)&p
))
667 struct interface
*if_lookup_by_ipv4_exact(struct in_addr
*addr
, vrf_id_t vrf_id
)
670 struct listnode
*cnode
;
671 struct interface
*ifp
;
672 struct connected
*connected
;
675 vrf
= vrf_lookup_by_id(vrf_id
);
679 FOR_ALL_INTERFACES (vrf
, ifp
) {
680 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
681 cp
= connected
->address
;
683 if (cp
->family
== AF_INET
)
684 if (IPV4_ADDR_SAME(&cp
->u
.prefix4
, addr
))
691 struct interface
*if_lookup_by_ipv6(struct in6_addr
*addr
, ifindex_t ifindex
,
695 struct listnode
*cnode
;
696 struct interface
*ifp
;
697 struct connected
*connected
;
698 struct prefix_ipv6 p
;
701 vrf
= vrf_lookup_by_id(vrf_id
);
707 p
.prefixlen
= IPV6_MAX_BITLEN
;
709 FOR_ALL_INTERFACES (vrf
, ifp
) {
710 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
711 cp
= connected
->address
;
713 if (cp
->family
== AF_INET6
)
714 if (prefix_match(cp
, (struct prefix
*)&p
)) {
715 if (IN6_IS_ADDR_LINKLOCAL(
717 if (ifindex
== ifp
->ifindex
)
727 struct interface
*if_lookup_by_ipv6_exact(struct in6_addr
*addr
,
728 ifindex_t ifindex
, vrf_id_t vrf_id
)
731 struct listnode
*cnode
;
732 struct interface
*ifp
;
733 struct connected
*connected
;
736 vrf
= vrf_lookup_by_id(vrf_id
);
740 FOR_ALL_INTERFACES (vrf
, ifp
) {
741 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
742 cp
= connected
->address
;
744 if (cp
->family
== AF_INET6
)
745 if (IPV6_ADDR_SAME(&cp
->u
.prefix6
, addr
)) {
746 if (IN6_IS_ADDR_LINKLOCAL(
748 if (ifindex
== ifp
->ifindex
)
758 static int if_get_ipv6_global(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 bool if_get_ipv6_local(struct interface
*ifp
, struct in6_addr
*addr
)
778 struct listnode
*cnode
;
779 struct connected
*connected
;
782 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
783 cp
= connected
->address
;
785 if (cp
->family
== AF_INET6
)
786 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
787 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
794 static int if_get_ipv4_address(struct interface
*ifp
, struct in_addr
*addr
)
796 struct listnode
*cnode
;
797 struct connected
*connected
;
800 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
801 cp
= connected
->address
;
802 if ((cp
->family
== AF_INET
)
803 && !ipv4_martian(&(cp
->u
.prefix4
))) {
804 *addr
= cp
->u
.prefix4
;
812 bool bgp_zebra_nexthop_set(union sockunion
*local
, union sockunion
*remote
,
813 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
816 struct interface
*ifp
= NULL
;
817 bool v6_ll_avail
= true;
819 memset(nexthop
, 0, sizeof(struct bgp_nexthop
));
826 if (local
->sa
.sa_family
== AF_INET
) {
827 nexthop
->v4
= local
->sin
.sin_addr
;
829 ifp
= if_lookup_by_name(peer
->update_if
,
832 ifp
= if_lookup_by_ipv4_exact(&local
->sin
.sin_addr
,
835 if (local
->sa
.sa_family
== AF_INET6
) {
836 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
, IPV6_MAX_BYTELEN
);
837 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
838 if (peer
->conf_if
|| peer
->ifname
)
839 ifp
= if_lookup_by_name(peer
->conf_if
843 else if (peer
->update_if
)
844 ifp
= if_lookup_by_name(peer
->update_if
,
846 } else if (peer
->update_if
)
847 ifp
= if_lookup_by_name(peer
->update_if
,
850 ifp
= if_lookup_by_ipv6_exact(&local
->sin6
.sin6_addr
,
851 local
->sin6
.sin6_scope_id
,
857 * BGP views do not currently get proper data
858 * from zebra( when attached ) to be able to
859 * properly resolve nexthops, so give this
860 * instance type a pass.
862 if (peer
->bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
865 * If we have no interface data but we have established
866 * some connection w/ zebra than something has gone
867 * terribly terribly wrong here, so say this failed
868 * If we do not any zebra connection then not
869 * having a ifp pointer is ok.
871 return zclient_num_connects
? false : true;
876 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
877 if (local
->sa
.sa_family
== AF_INET
) {
879 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
882 /* There is no global nexthop. Use link-local address as
884 * global and link-local nexthop. In this scenario, the
886 * for interop is that the network admin would use a
888 * specify the global IPv6 nexthop.
891 if_get_ipv6_local(ifp
, &nexthop
->v6_global
);
892 memcpy(&nexthop
->v6_local
, &nexthop
->v6_global
,
896 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
899 * If we are a v4 connection and we are not doing unnumbered
900 * not having a v6 LL address is ok
902 if (!v6_ll_avail
&& !peer
->conf_if
)
904 if (if_lookup_by_ipv4(&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
905 peer
->shared_network
= 1;
907 peer
->shared_network
= 0;
910 /* IPv6 connection, fetch and store IPv4 local address if any. */
911 if (local
->sa
.sa_family
== AF_INET6
) {
912 struct interface
*direct
= NULL
;
915 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
916 if (!ret
&& peer
->local_id
.s_addr
!= INADDR_ANY
)
917 nexthop
->v4
= peer
->local_id
;
920 if (!IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
921 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
924 /* If directly connected set link-local address. */
925 direct
= if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
926 remote
->sin6
.sin6_scope_id
,
929 v6_ll_avail
= if_get_ipv6_local(
930 ifp
, &nexthop
->v6_local
);
932 * It's fine to not have a v6 LL when using
933 * update-source loopback/vrf
935 if (!v6_ll_avail
&& if_is_loopback(ifp
))
937 else if (!v6_ll_avail
) {
939 EC_BGP_NO_LL_ADDRESS_AVAILABLE
,
940 "Interface: %s does not have a v6 LL address associated with it, waiting until one is created for it",
944 /* Link-local address. */
946 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
948 /* If there is no global address. Set link-local
950 global. I know this break RFC specification... */
951 /* In this scenario, the expectation for interop is that
953 * network admin would use a route-map to specify the
958 memcpy(&nexthop
->v6_global
,
959 &local
->sin6
.sin6_addr
,
961 /* Always set the link-local address */
962 memcpy(&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
966 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)
967 || if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
968 remote
->sin6
.sin6_scope_id
,
970 peer
->shared_network
= 1;
972 peer
->shared_network
= 0;
975 /* KAME stack specific treatment. */
977 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_global
)
978 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
)) {
979 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
, 0);
981 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_local
)
982 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
)) {
983 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
, 0);
987 /* If we have identified the local interface, there is no error for now.
992 static struct in6_addr
*
993 bgp_path_info_to_ipv6_nexthop(struct bgp_path_info
*path
, ifindex_t
*ifindex
)
995 struct in6_addr
*nexthop
= NULL
;
997 /* Only global address nexthop exists. */
998 if (path
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
999 || path
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV6_GLOBAL
) {
1000 nexthop
= &path
->attr
->mp_nexthop_global
;
1001 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
1002 *ifindex
= path
->attr
->nh_ifindex
;
1005 /* If both global and link-local address present. */
1006 if (path
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
1007 || path
->attr
->mp_nexthop_len
1008 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
1009 /* Check if route-map is set to prefer global over link-local */
1010 if (path
->attr
->mp_nexthop_prefer_global
) {
1011 nexthop
= &path
->attr
->mp_nexthop_global
;
1012 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
1013 *ifindex
= path
->attr
->nh_ifindex
;
1015 /* Workaround for Cisco's nexthop bug. */
1016 if (IN6_IS_ADDR_UNSPECIFIED(
1017 &path
->attr
->mp_nexthop_global
)
1018 && path
->peer
->su_remote
1019 && path
->peer
->su_remote
->sa
.sa_family
1022 &path
->peer
->su_remote
->sin6
.sin6_addr
;
1023 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
1024 *ifindex
= path
->peer
->nexthop
.ifp
1027 nexthop
= &path
->attr
->mp_nexthop_local
;
1028 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
1029 *ifindex
= path
->attr
->nh_lla_ifindex
;
1037 static bool bgp_table_map_apply(struct route_map
*map
, const struct prefix
*p
,
1038 struct bgp_path_info
*path
)
1040 route_map_result_t ret
;
1042 ret
= route_map_apply(map
, p
, path
);
1043 bgp_attr_flush(path
->attr
);
1045 if (ret
!= RMAP_DENYMATCH
)
1048 if (bgp_debug_zebra(p
)) {
1049 if (p
->family
== AF_INET
) {
1051 "Zebra rmap deny: IPv4 route %pFX nexthop %pI4",
1052 p
, &path
->attr
->nexthop
);
1054 if (p
->family
== AF_INET6
) {
1056 struct in6_addr
*nexthop
;
1058 nexthop
= bgp_path_info_to_ipv6_nexthop(path
, &ifindex
);
1060 "Zebra rmap deny: IPv6 route %pFX nexthop %pI6",
1067 static struct thread
*bgp_tm_thread_connect
;
1068 static bool bgp_tm_status_connected
;
1069 static bool bgp_tm_chunk_obtained
;
1070 #define BGP_FLOWSPEC_TABLE_CHUNK 100000
1071 static uint32_t bgp_tm_min
, bgp_tm_max
, bgp_tm_chunk_size
;
1072 struct bgp
*bgp_tm_bgp
;
1074 static void bgp_zebra_tm_connect(struct thread
*t
)
1076 struct zclient
*zclient
;
1077 int delay
= 10, ret
= 0;
1079 zclient
= THREAD_ARG(t
);
1080 if (bgp_tm_status_connected
&& zclient
->sock
> 0)
1083 bgp_tm_status_connected
= false;
1084 ret
= tm_table_manager_connect(zclient
);
1087 zlog_info("Error connecting to table manager!");
1088 bgp_tm_status_connected
= false;
1090 if (!bgp_tm_status_connected
)
1091 zlog_debug("Connecting to table manager. Success");
1092 bgp_tm_status_connected
= true;
1093 if (!bgp_tm_chunk_obtained
) {
1094 if (bgp_zebra_get_table_range(bgp_tm_chunk_size
,
1096 &bgp_tm_max
) >= 0) {
1097 bgp_tm_chunk_obtained
= true;
1098 /* parse non installed entries */
1099 bgp_zebra_announce_table(bgp_tm_bgp
, AFI_IP
, SAFI_FLOWSPEC
);
1103 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1104 &bgp_tm_thread_connect
);
1107 bool bgp_zebra_tm_chunk_obtained(void)
1109 return bgp_tm_chunk_obtained
;
1112 uint32_t bgp_zebra_tm_get_id(void)
1114 static int table_id
;
1116 if (!bgp_tm_chunk_obtained
)
1118 return bgp_tm_min
++;
1121 void bgp_zebra_init_tm_connect(struct bgp
*bgp
)
1125 /* if already set, do nothing
1127 if (bgp_tm_thread_connect
!= NULL
)
1129 bgp_tm_status_connected
= false;
1130 bgp_tm_chunk_obtained
= false;
1131 bgp_tm_min
= bgp_tm_max
= 0;
1132 bgp_tm_chunk_size
= BGP_FLOWSPEC_TABLE_CHUNK
;
1134 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1135 &bgp_tm_thread_connect
);
1138 int bgp_zebra_get_table_range(uint32_t chunk_size
,
1139 uint32_t *start
, uint32_t *end
)
1143 if (!bgp_tm_status_connected
)
1145 ret
= tm_get_table_chunk(zclient
, chunk_size
, start
, end
);
1147 flog_err(EC_BGP_TABLE_CHUNK
,
1148 "BGP: Error getting table chunk %u", chunk_size
);
1151 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1152 chunk_size
, *start
, *end
);
1156 static bool update_ipv4nh_for_route_install(int nh_othervrf
, struct bgp
*nh_bgp
,
1157 struct in_addr
*nexthop
,
1158 struct attr
*attr
, bool is_evpn
,
1159 struct zapi_nexthop
*api_nh
)
1161 api_nh
->gate
.ipv4
= *nexthop
;
1162 api_nh
->vrf_id
= nh_bgp
->vrf_id
;
1164 /* Need to set fields appropriately for EVPN routes imported into
1165 * a VRF (which are programmed as onlink on l3-vni SVI) as well as
1166 * connected routes leaked into a VRF.
1168 if (attr
->nh_type
== NEXTHOP_TYPE_BLACKHOLE
) {
1169 api_nh
->type
= attr
->nh_type
;
1170 api_nh
->bh_type
= attr
->bh_type
;
1171 } else if (is_evpn
) {
1173 * If the nexthop is EVPN overlay index gateway IP,
1174 * treat the nexthop as NEXTHOP_TYPE_IPV4
1175 * Else, mark the nexthop as onlink.
1177 if (attr
->evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
)
1178 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1180 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1181 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
);
1182 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
);
1183 api_nh
->ifindex
= nh_bgp
->l3vni_svi_ifindex
;
1185 } else if (nh_othervrf
&& api_nh
->gate
.ipv4
.s_addr
== INADDR_ANY
) {
1186 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1187 api_nh
->ifindex
= attr
->nh_ifindex
;
1189 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1194 static bool update_ipv6nh_for_route_install(int nh_othervrf
, struct bgp
*nh_bgp
,
1195 struct in6_addr
*nexthop
,
1197 struct bgp_path_info
*pi
,
1198 struct bgp_path_info
*best_pi
,
1200 struct zapi_nexthop
*api_nh
)
1205 api_nh
->vrf_id
= nh_bgp
->vrf_id
;
1207 if (attr
->nh_type
== NEXTHOP_TYPE_BLACKHOLE
) {
1208 api_nh
->type
= attr
->nh_type
;
1209 api_nh
->bh_type
= attr
->bh_type
;
1210 } else if (is_evpn
) {
1212 * If the nexthop is EVPN overlay index gateway IP,
1213 * treat the nexthop as NEXTHOP_TYPE_IPV4
1214 * Else, mark the nexthop as onlink.
1216 if (attr
->evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
)
1217 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1219 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1220 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
);
1221 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
);
1222 api_nh
->ifindex
= nh_bgp
->l3vni_svi_ifindex
;
1224 } else if (nh_othervrf
) {
1225 if (IN6_IS_ADDR_UNSPECIFIED(nexthop
)) {
1226 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1227 api_nh
->ifindex
= attr
->nh_ifindex
;
1228 } else if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1231 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1232 api_nh
->ifindex
= ifindex
;
1234 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1235 api_nh
->ifindex
= 0;
1238 if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1240 && attr
->mp_nexthop_len
1241 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1242 if (pi
->peer
->nexthop
.ifp
)
1244 pi
->peer
->nexthop
.ifp
->ifindex
;
1246 if (pi
->peer
->conf_if
)
1247 ifindex
= pi
->peer
->ifp
->ifindex
;
1248 else if (pi
->peer
->ifname
)
1249 ifindex
= ifname2ifindex(
1251 pi
->peer
->bgp
->vrf_id
);
1252 else if (pi
->peer
->nexthop
.ifp
)
1254 pi
->peer
->nexthop
.ifp
->ifindex
;
1259 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1260 api_nh
->ifindex
= ifindex
;
1262 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1263 api_nh
->ifindex
= 0;
1266 /* api_nh structure has union of gate and bh_type */
1267 if (nexthop
&& api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1268 api_nh
->gate
.ipv6
= *nexthop
;
1273 static bool bgp_zebra_use_nhop_weighted(struct bgp
*bgp
, struct attr
*attr
,
1274 uint64_t tot_bw
, uint32_t *nh_weight
)
1280 /* zero link-bandwidth and link-bandwidth not present are treated
1281 * as the same situation.
1284 /* the only situations should be if we're either told
1285 * to skip or use default weight.
1287 if (bgp
->lb_handling
== BGP_LINK_BW_SKIP_MISSING
)
1289 *nh_weight
= BGP_ZEBRA_DEFAULT_NHOP_WEIGHT
;
1291 tmp
= (uint64_t)bw
* 100;
1292 *nh_weight
= ((uint32_t)(tmp
/ tot_bw
));
1298 void bgp_zebra_announce(struct bgp_dest
*dest
, const struct prefix
*p
,
1299 struct bgp_path_info
*info
, struct bgp
*bgp
, afi_t afi
,
1302 struct zapi_route api
= { 0 };
1303 struct zapi_nexthop
*api_nh
;
1305 unsigned int valid_nh_count
= 0;
1306 bool allow_recursion
= false;
1309 struct bgp_path_info
*mpinfo
;
1310 struct bgp
*bgp_orig
;
1312 struct attr local_attr
;
1313 struct bgp_path_info local_info
;
1314 struct bgp_path_info
*mpinfo_cp
= &local_info
;
1317 struct bgp_sid_info
*sid_info
;
1318 int nh_othervrf
= 0;
1319 bool nh_updated
= false;
1321 uint64_t cum_bw
= 0;
1322 uint32_t nhg_id
= 0;
1328 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1329 * know of this instance.
1331 if (!bgp_install_info_to_zebra(bgp
))
1334 if (bgp
->main_zebra_update_hold
)
1337 if (safi
== SAFI_FLOWSPEC
) {
1338 bgp_pbr_update_entry(bgp
, bgp_dest_get_prefix(dest
), info
, afi
,
1344 * vrf leaking support (will have only one nexthop)
1346 if (info
->extra
&& info
->extra
->bgp_orig
)
1349 /* Make Zebra API structure. */
1350 api
.vrf_id
= bgp
->vrf_id
;
1351 api
.type
= ZEBRA_ROUTE_BGP
;
1354 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1358 if (info
->type
== ZEBRA_ROUTE_BGP
1359 && info
->sub_type
== BGP_ROUTE_IMPORTED
) {
1361 /* Obtain peer from parent */
1362 if (info
->extra
&& info
->extra
->parent
)
1363 peer
= ((struct bgp_path_info
*)(info
->extra
->parent
))
1367 tag
= info
->attr
->tag
;
1369 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
1370 || info
->sub_type
== BGP_ROUTE_AGGREGATE
) {
1371 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1372 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1375 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= BGP_DEFAULT_TTL
)
1376 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1377 || CHECK_FLAG(bgp
->flags
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1379 allow_recursion
= true;
1381 if (info
->attr
->rmap_table_id
) {
1382 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
1383 api
.tableid
= info
->attr
->rmap_table_id
;
1386 if (CHECK_FLAG(info
->attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR
)))
1387 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRTE
);
1389 /* Metric is currently based on the best-path only */
1390 metric
= info
->attr
->med
;
1392 /* Determine if we're doing weighted ECMP or not */
1393 do_wt_ecmp
= bgp_path_info_mpath_chkwtd(bgp
, info
);
1395 cum_bw
= bgp_path_info_mpath_cumbw(info
);
1397 /* EVPN MAC-IP routes are installed with a L3 NHG id */
1398 if (bgp_evpn_path_es_use_nhg(bgp
, info
, &nhg_id
)) {
1402 SET_FLAG(api
.message
, ZAPI_MESSAGE_NHG
);
1407 for (; mpinfo
; mpinfo
= bgp_path_info_mpath_next(mpinfo
)) {
1411 if (valid_nh_count
>= multipath_num
)
1414 *mpinfo_cp
= *mpinfo
;
1417 /* Get nexthop address-family */
1418 if (p
->family
== AF_INET
&&
1419 !BGP_ATTR_MP_NEXTHOP_LEN_IP6(mpinfo_cp
->attr
))
1420 nh_family
= AF_INET
;
1421 else if (p
->family
== AF_INET6
||
1422 (p
->family
== AF_INET
&&
1423 BGP_ATTR_MP_NEXTHOP_LEN_IP6(mpinfo_cp
->attr
)))
1424 nh_family
= AF_INET6
;
1428 /* If processing for weighted ECMP, determine the next hop's
1429 * weight. Based on user setting, we may skip the next hop
1430 * in some situations.
1433 if (!bgp_zebra_use_nhop_weighted(bgp
, mpinfo
->attr
,
1434 cum_bw
, &nh_weight
))
1437 api_nh
= &api
.nexthops
[valid_nh_count
];
1439 if (CHECK_FLAG(info
->attr
->flag
,
1440 ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR
)))
1441 api_nh
->srte_color
= info
->attr
->srte_color
;
1443 if (bgp_debug_zebra(&api
.prefix
)) {
1444 if (mpinfo
->extra
) {
1445 zlog_debug("%s: p=%pFX, bgp_is_valid_label: %d",
1448 &mpinfo
->extra
->label
[0]));
1451 "%s: p=%pFX, extra is NULL, no label",
1456 if (bgp
->table_map
[afi
][safi
].name
) {
1457 /* Copy info and attributes, so the route-map
1458 apply doesn't modify the BGP route info. */
1459 local_attr
= *mpinfo
->attr
;
1460 mpinfo_cp
->attr
= &local_attr
;
1461 if (!bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
,
1465 /* metric/tag is only allowed to be
1466 * overridden on 1st nexthop */
1467 if (mpinfo
== info
) {
1468 metric
= mpinfo_cp
->attr
->med
;
1469 tag
= mpinfo_cp
->attr
->tag
;
1473 BGP_ORIGINAL_UPDATE(bgp_orig
, mpinfo
, bgp
);
1475 if (nh_family
== AF_INET
) {
1476 is_evpn
= is_route_parent_evpn(mpinfo
);
1478 nh_updated
= update_ipv4nh_for_route_install(
1479 nh_othervrf
, bgp_orig
,
1480 &mpinfo_cp
->attr
->nexthop
, mpinfo_cp
->attr
,
1483 ifindex_t ifindex
= IFINDEX_INTERNAL
;
1484 struct in6_addr
*nexthop
;
1486 nexthop
= bgp_path_info_to_ipv6_nexthop(mpinfo_cp
,
1489 is_evpn
= is_route_parent_evpn(mpinfo
);
1492 nh_updated
= update_ipv4nh_for_route_install(
1493 nh_othervrf
, bgp_orig
,
1494 &mpinfo_cp
->attr
->nexthop
,
1495 mpinfo_cp
->attr
, is_evpn
, api_nh
);
1497 nh_updated
= update_ipv6nh_for_route_install(
1498 nh_othervrf
, bgp_orig
, nexthop
, ifindex
,
1499 mpinfo
, info
, is_evpn
, api_nh
);
1502 /* Did we get proper nexthop info to update zebra? */
1506 /* Allow recursion if it is a multipath group with both
1507 * eBGP and iBGP paths.
1509 if (!allow_recursion
1510 && CHECK_FLAG(bgp
->flags
, BGP_FLAG_PEERTYPE_MULTIPATH_RELAX
)
1511 && (mpinfo
->peer
->sort
== BGP_PEER_IBGP
1512 || mpinfo
->peer
->sort
== BGP_PEER_CONFED
))
1513 allow_recursion
= true;
1515 if (mpinfo
->extra
&&
1516 bgp_is_valid_label(&mpinfo
->extra
->label
[0]) &&
1517 !CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
)) {
1518 mpls_lse_decode(mpinfo
->extra
->label
[0], &label
, &ttl
,
1521 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
);
1523 api_nh
->label_num
= 1;
1524 api_nh
->labels
[0] = 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
&&
1536 bgp_is_valid_label(&mpinfo
->extra
->label
[0]) &&
1537 !sid_zero(&mpinfo
->extra
->sid
[0].sid
) &&
1538 !CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
)) {
1539 sid_info
= &mpinfo
->extra
->sid
[0];
1541 memcpy(&api_nh
->seg6_segs
, &sid_info
->sid
,
1542 sizeof(api_nh
->seg6_segs
));
1544 if (sid_info
->transposition_len
!= 0) {
1545 mpls_lse_decode(mpinfo
->extra
->label
[0], &label
,
1548 if (label
< MPLS_LABEL_UNRESERVED_MIN
) {
1549 if (bgp_debug_zebra(&api
.prefix
))
1551 "skip invalid SRv6 routes: transposition scheme is used, but label is too small");
1555 transpose_sid(&api_nh
->seg6_segs
, label
,
1556 sid_info
->transposition_offset
,
1557 sid_info
->transposition_len
);
1560 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6
);
1566 is_add
= (valid_nh_count
|| nhg_id
) ? true : false;
1568 if (is_add
&& CHECK_FLAG(bm
->flags
, BM_FLAG_SEND_EXTRA_DATA_TO_ZEBRA
)) {
1569 struct bgp_zebra_opaque bzo
= {};
1570 const char *reason
=
1571 bgp_path_selection_reason2str(dest
->reason
);
1573 strlcpy(bzo
.aspath
, info
->attr
->aspath
->str
,
1574 sizeof(bzo
.aspath
));
1576 if (info
->attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
))
1577 strlcpy(bzo
.community
,
1578 bgp_attr_get_community(info
->attr
)->str
,
1579 sizeof(bzo
.community
));
1581 if (info
->attr
->flag
1582 & ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
))
1583 strlcpy(bzo
.lcommunity
,
1584 bgp_attr_get_lcommunity(info
->attr
)->str
,
1585 sizeof(bzo
.lcommunity
));
1587 strlcpy(bzo
.selection_reason
, reason
,
1588 sizeof(bzo
.selection_reason
));
1590 SET_FLAG(api
.message
, ZAPI_MESSAGE_OPAQUE
);
1591 api
.opaque
.length
= MIN(sizeof(struct bgp_zebra_opaque
),
1592 ZAPI_MESSAGE_OPAQUE_LENGTH
);
1593 memcpy(api
.opaque
.data
, &bzo
, api
.opaque
.length
);
1596 if (allow_recursion
)
1597 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1600 * When we create an aggregate route we must also
1601 * install a Null0 route in the RIB, so overwrite
1602 * what was written into api with a blackhole route
1604 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1605 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
1607 api
.nexthop_num
= valid_nh_count
;
1609 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1610 api
.metric
= metric
;
1613 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1617 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1619 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1620 api
.distance
= distance
;
1623 if (bgp_debug_zebra(p
)) {
1624 char nh_buf
[INET6_ADDRSTRLEN
];
1625 char eth_buf
[ETHER_ADDR_STRLEN
+ 7] = {'\0'};
1626 char buf1
[ETHER_ADDR_STRLEN
];
1633 "Tx route %s VRF %u %pFX metric %u tag %" ROUTE_TAG_PRI
1635 is_add
? "add" : "delete", bgp
->vrf_id
, &api
.prefix
,
1636 api
.metric
, api
.tag
, api
.nexthop_num
, nhg_id
);
1637 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1638 api_nh
= &api
.nexthops
[i
];
1640 switch (api_nh
->type
) {
1641 case NEXTHOP_TYPE_IFINDEX
:
1644 case NEXTHOP_TYPE_IPV4
:
1645 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1646 nh_family
= AF_INET
;
1647 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1650 case NEXTHOP_TYPE_IPV6
:
1651 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1652 nh_family
= AF_INET6
;
1653 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1656 case NEXTHOP_TYPE_BLACKHOLE
:
1657 strlcpy(nh_buf
, "blackhole", sizeof(nh_buf
));
1660 /* Note: add new nexthop case */
1665 label_buf
[0] = '\0';
1668 if (CHECK_FLAG(api_nh
->flags
,
1669 ZAPI_NEXTHOP_FLAG_LABEL
) &&
1670 !CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
))
1671 snprintf(label_buf
, sizeof(label_buf
),
1672 "label %u", api_nh
->labels
[0]);
1673 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6
) &&
1674 !CHECK_FLAG(api_nh
->flags
,
1675 ZAPI_NEXTHOP_FLAG_EVPN
)) {
1676 inet_ntop(AF_INET6
, &api_nh
->seg6_segs
,
1677 sid_buf
, sizeof(sid_buf
));
1678 snprintf(segs_buf
, sizeof(segs_buf
), "segs %s",
1681 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
) &&
1682 !is_zero_mac(&api_nh
->rmac
))
1683 snprintf(eth_buf
, sizeof(eth_buf
), " RMAC %s",
1684 prefix_mac2str(&api_nh
->rmac
,
1685 buf1
, sizeof(buf1
)));
1686 zlog_debug(" nhop [%d]: %s if %u VRF %u wt %u %s %s %s",
1687 i
+ 1, nh_buf
, api_nh
->ifindex
,
1688 api_nh
->vrf_id
, api_nh
->weight
,
1689 label_buf
, segs_buf
, eth_buf
);
1692 int recursion_flag
= 0;
1694 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
))
1697 zlog_debug("%s: %pFX: announcing to zebra (recursion %sset)",
1698 __func__
, p
, (recursion_flag
? "" : "NOT "));
1700 zclient_route_send(is_add
? ZEBRA_ROUTE_ADD
: ZEBRA_ROUTE_DELETE
,
1704 /* Announce all routes of a table to zebra */
1705 void bgp_zebra_announce_table(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1707 struct bgp_dest
*dest
;
1708 struct bgp_table
*table
;
1709 struct bgp_path_info
*pi
;
1711 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1712 * know of this instance.
1714 if (!bgp_install_info_to_zebra(bgp
))
1717 table
= bgp
->rib
[afi
][safi
];
1721 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
))
1722 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
1723 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
) &&
1725 (pi
->type
== ZEBRA_ROUTE_BGP
1726 && (pi
->sub_type
== BGP_ROUTE_NORMAL
1727 || pi
->sub_type
== BGP_ROUTE_IMPORTED
)))
1729 bgp_zebra_announce(dest
,
1730 bgp_dest_get_prefix(dest
),
1731 pi
, bgp
, afi
, safi
);
1734 /* Announce routes of any bgp subtype of a table to zebra */
1735 void bgp_zebra_announce_table_all_subtypes(struct bgp
*bgp
, afi_t afi
,
1738 struct bgp_dest
*dest
;
1739 struct bgp_table
*table
;
1740 struct bgp_path_info
*pi
;
1742 if (!bgp_install_info_to_zebra(bgp
))
1745 table
= bgp
->rib
[afi
][safi
];
1749 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
))
1750 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
1751 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
) &&
1752 pi
->type
== ZEBRA_ROUTE_BGP
)
1753 bgp_zebra_announce(dest
,
1754 bgp_dest_get_prefix(dest
),
1755 pi
, bgp
, afi
, safi
);
1758 void bgp_zebra_withdraw(const struct prefix
*p
, struct bgp_path_info
*info
,
1759 struct bgp
*bgp
, safi_t safi
)
1761 struct zapi_route api
;
1764 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1765 * know of this instance.
1767 if (!bgp_install_info_to_zebra(bgp
))
1770 if (safi
== SAFI_FLOWSPEC
) {
1772 bgp_pbr_update_entry(peer
->bgp
, p
, info
, AFI_IP
, safi
, false);
1776 memset(&api
, 0, sizeof(api
));
1777 api
.vrf_id
= bgp
->vrf_id
;
1778 api
.type
= ZEBRA_ROUTE_BGP
;
1782 if (info
->attr
->rmap_table_id
) {
1783 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
1784 api
.tableid
= info
->attr
->rmap_table_id
;
1787 if (bgp_debug_zebra(p
))
1788 zlog_debug("Tx route delete VRF %u %pFX", bgp
->vrf_id
,
1791 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
1794 /* Withdraw all entries in a BGP instances RIB table from Zebra */
1795 void bgp_zebra_withdraw_table_all_subtypes(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1797 struct bgp_dest
*dest
;
1798 struct bgp_table
*table
;
1799 struct bgp_path_info
*pi
;
1801 if (!bgp_install_info_to_zebra(bgp
))
1804 table
= bgp
->rib
[afi
][safi
];
1808 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
1809 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
1810 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
1811 && (pi
->type
== ZEBRA_ROUTE_BGP
))
1812 bgp_zebra_withdraw(bgp_dest_get_prefix(dest
),
1818 struct bgp_redist
*bgp_redist_lookup(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1819 unsigned short instance
)
1821 struct list
*red_list
;
1822 struct listnode
*node
;
1823 struct bgp_redist
*red
;
1825 red_list
= bgp
->redist
[afi
][type
];
1829 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1830 if (red
->instance
== instance
)
1836 struct bgp_redist
*bgp_redist_add(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1837 unsigned short instance
)
1839 struct list
*red_list
;
1840 struct bgp_redist
*red
;
1842 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1846 if (!bgp
->redist
[afi
][type
])
1847 bgp
->redist
[afi
][type
] = list_new();
1849 red_list
= bgp
->redist
[afi
][type
];
1850 red
= XCALLOC(MTYPE_BGP_REDIST
, sizeof(struct bgp_redist
));
1851 red
->instance
= instance
;
1853 listnode_add(red_list
, red
);
1858 static void bgp_redist_del(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1859 unsigned short instance
)
1861 struct bgp_redist
*red
;
1863 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1866 listnode_delete(bgp
->redist
[afi
][type
], red
);
1867 XFREE(MTYPE_BGP_REDIST
, red
);
1868 if (!bgp
->redist
[afi
][type
]->count
)
1869 list_delete(&bgp
->redist
[afi
][type
]);
1873 /* Other routes redistribution into BGP. */
1874 int bgp_redistribute_set(struct bgp
*bgp
, afi_t afi
, int type
,
1875 unsigned short instance
, bool changed
)
1877 /* If redistribute options are changed call
1878 * bgp_redistribute_unreg() to reset the option and withdraw
1882 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1884 /* Return if already redistribute flag is set. */
1886 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1890 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1892 if (vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1895 #ifdef ENABLE_BGP_VNC
1896 if (EVPN_ENABLED(bgp
) && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1897 vnc_export_bgp_enable(
1898 bgp
, afi
); /* only enables if mode bits cfg'd */
1902 vrf_bitmap_set(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1906 * Don't try to register if we're not connected to Zebra or Zebra
1907 * doesn't know of this instance.
1909 * When we come up later well resend if needed.
1911 if (!bgp_install_info_to_zebra(bgp
))
1914 if (BGP_DEBUG(zebra
, ZEBRA
))
1915 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1916 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1919 /* Send distribute add message to zebra. */
1920 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1921 instance
, bgp
->vrf_id
);
1926 int bgp_redistribute_resend(struct bgp
*bgp
, afi_t afi
, int type
,
1927 unsigned short instance
)
1929 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1930 * know of this instance.
1932 if (!bgp_install_info_to_zebra(bgp
))
1935 if (BGP_DEBUG(zebra
, ZEBRA
))
1936 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1937 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1940 /* Send distribute add message to zebra. */
1941 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1942 instance
, bgp
->vrf_id
);
1943 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1944 instance
, bgp
->vrf_id
);
1949 /* Redistribute with route-map specification. */
1950 bool bgp_redistribute_rmap_set(struct bgp_redist
*red
, const char *name
,
1951 struct route_map
*route_map
)
1953 if (red
->rmap
.name
&& (strcmp(red
->rmap
.name
, name
) == 0))
1956 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1957 /* Decrement the count for existing routemap and
1958 * increment the count for new route map.
1960 route_map_counter_decrement(red
->rmap
.map
);
1961 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1962 red
->rmap
.map
= route_map
;
1963 route_map_counter_increment(red
->rmap
.map
);
1968 /* Redistribute with metric specification. */
1969 bool bgp_redistribute_metric_set(struct bgp
*bgp
, struct bgp_redist
*red
,
1970 afi_t afi
, int type
, uint32_t metric
)
1972 struct bgp_dest
*dest
;
1973 struct bgp_path_info
*pi
;
1975 if (red
->redist_metric_flag
&& red
->redist_metric
== metric
)
1978 red
->redist_metric_flag
= 1;
1979 red
->redist_metric
= metric
;
1981 for (dest
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); dest
;
1982 dest
= bgp_route_next(dest
)) {
1983 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
1984 if (pi
->sub_type
== BGP_ROUTE_REDISTRIBUTE
1986 && pi
->instance
== red
->instance
) {
1987 struct attr
*old_attr
;
1988 struct attr new_attr
;
1990 new_attr
= *pi
->attr
;
1991 new_attr
.med
= red
->redist_metric
;
1992 old_attr
= pi
->attr
;
1993 pi
->attr
= bgp_attr_intern(&new_attr
);
1994 bgp_attr_unintern(&old_attr
);
1996 bgp_path_info_set_flag(dest
, pi
,
1997 BGP_PATH_ATTR_CHANGED
);
1998 bgp_process(bgp
, dest
, afi
, SAFI_UNICAST
);
2006 /* Unset redistribution. */
2007 int bgp_redistribute_unreg(struct bgp
*bgp
, afi_t afi
, int type
,
2008 unsigned short instance
)
2010 struct bgp_redist
*red
;
2012 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
2016 /* Return if zebra connection is disabled. */
2018 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
],
2021 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
2023 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
2025 vrf_bitmap_unset(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
2028 if (bgp_install_info_to_zebra(bgp
)) {
2029 /* Send distribute delete message to zebra. */
2030 if (BGP_DEBUG(zebra
, ZEBRA
))
2031 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
2032 bgp
->vrf_id
, afi
, zebra_route_string(type
),
2034 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
2035 type
, instance
, bgp
->vrf_id
);
2038 /* Withdraw redistributed routes from current BGP's routing table. */
2039 bgp_redistribute_withdraw(bgp
, afi
, type
, instance
);
2044 /* Unset redistribution. */
2045 int bgp_redistribute_unset(struct bgp
*bgp
, afi_t afi
, int type
,
2046 unsigned short instance
)
2048 struct bgp_redist
*red
;
2051 * vnc and vpn->vrf checks must be before red check because
2052 * they operate within bgpd irrespective of zebra connection
2053 * status. red lookup fails if there is no zebra connection.
2055 #ifdef ENABLE_BGP_VNC
2056 if (EVPN_ENABLED(bgp
) && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
2057 vnc_export_bgp_disable(bgp
, afi
);
2061 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
2065 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
2067 /* Unset route-map. */
2068 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
2069 route_map_counter_decrement(red
->rmap
.map
);
2070 red
->rmap
.map
= NULL
;
2073 red
->redist_metric_flag
= 0;
2074 red
->redist_metric
= 0;
2076 bgp_redist_del(bgp
, afi
, type
, instance
);
2081 void bgp_redistribute_redo(struct bgp
*bgp
)
2085 struct list
*red_list
;
2086 struct listnode
*node
;
2087 struct bgp_redist
*red
;
2089 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2090 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
2092 red_list
= bgp
->redist
[afi
][i
];
2096 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
2097 bgp_redistribute_resend(bgp
, afi
, i
,
2104 void bgp_zclient_reset(void)
2106 zclient_reset(zclient
);
2109 /* Register this instance with Zebra. Invoked upon connect (for
2110 * default instance) and when other VRFs are learnt (or created and
2113 void bgp_zebra_instance_register(struct bgp
*bgp
)
2115 /* Don't try to register if we're not connected to Zebra */
2116 if (!zclient
|| zclient
->sock
< 0)
2119 if (BGP_DEBUG(zebra
, ZEBRA
))
2120 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
2122 /* Register for router-id, interfaces, redistributed routes. */
2123 zclient_send_reg_requests(zclient
, bgp
->vrf_id
);
2125 /* For EVPN instance, register to learn about VNIs, if appropriate. */
2126 if (bgp
->advertise_all_vni
)
2127 bgp_zebra_advertise_all_vni(bgp
, 1);
2129 bgp_nht_register_nexthops(bgp
);
2132 /* Deregister this instance with Zebra. Invoked upon the instance
2133 * being deleted (default or VRF) and it is already registered.
2135 void bgp_zebra_instance_deregister(struct bgp
*bgp
)
2137 /* Don't try to deregister if we're not connected to Zebra */
2138 if (zclient
->sock
< 0)
2141 if (BGP_DEBUG(zebra
, ZEBRA
))
2142 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
2144 /* For EVPN instance, unregister learning about VNIs, if appropriate. */
2145 if (bgp
->advertise_all_vni
)
2146 bgp_zebra_advertise_all_vni(bgp
, 0);
2148 /* Deregister for router-id, interfaces, redistributed routes. */
2149 zclient_send_dereg_requests(zclient
, bgp
->vrf_id
);
2152 void bgp_zebra_initiate_radv(struct bgp
*bgp
, struct peer
*peer
)
2154 uint32_t ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
2156 /* Don't try to initiate if we're not connected to Zebra */
2157 if (zclient
->sock
< 0)
2160 if (BGP_DEBUG(zebra
, ZEBRA
))
2161 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
,
2165 * If unnumbered peer (peer->ifp) call thru zapi to start RAs.
2166 * If we don't have an ifp pointer, call function to find the
2167 * ifps for a numbered enhe peer to turn RAs on.
2169 peer
->ifp
? zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
,
2170 peer
->ifp
, 1, ra_interval
)
2171 : bgp_nht_reg_enhe_cap_intfs(peer
);
2174 void bgp_zebra_terminate_radv(struct bgp
*bgp
, struct peer
*peer
)
2176 /* Don't try to terminate if we're not connected to Zebra */
2177 if (zclient
->sock
< 0)
2180 if (BGP_DEBUG(zebra
, ZEBRA
))
2181 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
,
2185 * If unnumbered peer (peer->ifp) call thru zapi to stop RAs.
2186 * If we don't have an ifp pointer, call function to find the
2187 * ifps for a numbered enhe peer to turn RAs off.
2189 peer
->ifp
? zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
,
2191 : bgp_nht_dereg_enhe_cap_intfs(peer
);
2194 int bgp_zebra_advertise_subnet(struct bgp
*bgp
, int advertise
, vni_t vni
)
2196 struct stream
*s
= NULL
;
2199 if (!zclient
|| zclient
->sock
< 0)
2202 /* Don't try to register if Zebra doesn't know of this instance. */
2203 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
2204 if (BGP_DEBUG(zebra
, ZEBRA
))
2206 "%s: No zebra instance to talk to, cannot advertise subnet",
2214 zclient_create_header(s
, ZEBRA_ADVERTISE_SUBNET
, bgp
->vrf_id
);
2215 stream_putc(s
, advertise
);
2216 stream_put3(s
, vni
);
2217 stream_putw_at(s
, 0, stream_get_endp(s
));
2219 return zclient_send_message(zclient
);
2222 int bgp_zebra_advertise_svi_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
2224 struct stream
*s
= NULL
;
2227 if (!zclient
|| zclient
->sock
< 0)
2230 /* Don't try to register if Zebra doesn't know of this instance. */
2231 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
2237 zclient_create_header(s
, ZEBRA_ADVERTISE_SVI_MACIP
, bgp
->vrf_id
);
2238 stream_putc(s
, advertise
);
2239 stream_putl(s
, vni
);
2240 stream_putw_at(s
, 0, stream_get_endp(s
));
2242 return zclient_send_message(zclient
);
2245 int bgp_zebra_advertise_gw_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
2247 struct stream
*s
= NULL
;
2250 if (!zclient
|| zclient
->sock
< 0)
2253 /* Don't try to register if Zebra doesn't know of this instance. */
2254 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
2255 if (BGP_DEBUG(zebra
, ZEBRA
))
2257 "%s: No zebra instance to talk to, not installing gw_macip",
2265 zclient_create_header(s
, ZEBRA_ADVERTISE_DEFAULT_GW
, bgp
->vrf_id
);
2266 stream_putc(s
, advertise
);
2267 stream_putl(s
, vni
);
2268 stream_putw_at(s
, 0, stream_get_endp(s
));
2270 return zclient_send_message(zclient
);
2273 int bgp_zebra_vxlan_flood_control(struct bgp
*bgp
,
2274 enum vxlan_flood_control flood_ctrl
)
2279 if (!zclient
|| zclient
->sock
< 0)
2282 /* Don't try to register if Zebra doesn't know of this instance. */
2283 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
2284 if (BGP_DEBUG(zebra
, ZEBRA
))
2286 "%s: No zebra instance to talk to, not installing all vni",
2294 zclient_create_header(s
, ZEBRA_VXLAN_FLOOD_CONTROL
, bgp
->vrf_id
);
2295 stream_putc(s
, flood_ctrl
);
2296 stream_putw_at(s
, 0, stream_get_endp(s
));
2298 return zclient_send_message(zclient
);
2301 int bgp_zebra_advertise_all_vni(struct bgp
*bgp
, int advertise
)
2306 if (!zclient
|| zclient
->sock
< 0)
2309 /* Don't try to register if Zebra doesn't know of this instance. */
2310 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
2316 zclient_create_header(s
, ZEBRA_ADVERTISE_ALL_VNI
, bgp
->vrf_id
);
2317 stream_putc(s
, advertise
);
2318 /* Also inform current BUM handling setting. This is really
2319 * relevant only when 'advertise' is set.
2321 stream_putc(s
, bgp
->vxlan_flood_ctrl
);
2322 stream_putw_at(s
, 0, stream_get_endp(s
));
2324 return zclient_send_message(zclient
);
2327 int bgp_zebra_dup_addr_detection(struct bgp
*bgp
)
2332 if (!zclient
|| zclient
->sock
< 0)
2335 /* Don't try to register if Zebra doesn't know of this instance. */
2336 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
2339 if (BGP_DEBUG(zebra
, ZEBRA
))
2340 zlog_debug("dup addr detect %s max_moves %u time %u freeze %s freeze_time %u",
2341 bgp
->evpn_info
->dup_addr_detect
?
2342 "enable" : "disable",
2343 bgp
->evpn_info
->dad_max_moves
,
2344 bgp
->evpn_info
->dad_time
,
2345 bgp
->evpn_info
->dad_freeze
?
2346 "enable" : "disable",
2347 bgp
->evpn_info
->dad_freeze_time
);
2351 zclient_create_header(s
, ZEBRA_DUPLICATE_ADDR_DETECTION
,
2353 stream_putl(s
, bgp
->evpn_info
->dup_addr_detect
);
2354 stream_putl(s
, bgp
->evpn_info
->dad_time
);
2355 stream_putl(s
, bgp
->evpn_info
->dad_max_moves
);
2356 stream_putl(s
, bgp
->evpn_info
->dad_freeze
);
2357 stream_putl(s
, bgp
->evpn_info
->dad_freeze_time
);
2358 stream_putw_at(s
, 0, stream_get_endp(s
));
2360 return zclient_send_message(zclient
);
2363 static int rule_notify_owner(ZAPI_CALLBACK_ARGS
)
2365 uint32_t seqno
, priority
, unique
;
2366 enum zapi_rule_notify_owner note
;
2367 struct bgp_pbr_action
*bgp_pbra
;
2368 struct bgp_pbr_rule
*bgp_pbr
= NULL
;
2369 char ifname
[INTERFACE_NAMSIZ
+ 1];
2371 if (!zapi_rule_notify_decode(zclient
->ibuf
, &seqno
, &priority
, &unique
,
2375 bgp_pbra
= bgp_pbr_action_rule_lookup(vrf_id
, unique
);
2377 /* look in bgp pbr rule */
2378 bgp_pbr
= bgp_pbr_rule_lookup(vrf_id
, unique
);
2379 if (!bgp_pbr
&& note
!= ZAPI_RULE_REMOVED
) {
2380 if (BGP_DEBUG(zebra
, ZEBRA
))
2381 zlog_debug("%s: Fail to look BGP rule (%u)",
2388 case ZAPI_RULE_FAIL_INSTALL
:
2389 if (BGP_DEBUG(zebra
, ZEBRA
))
2390 zlog_debug("%s: Received RULE_FAIL_INSTALL", __func__
);
2392 bgp_pbra
->installed
= false;
2393 bgp_pbra
->install_in_progress
= false;
2395 bgp_pbr
->installed
= false;
2396 bgp_pbr
->install_in_progress
= false;
2399 case ZAPI_RULE_INSTALLED
:
2401 bgp_pbra
->installed
= true;
2402 bgp_pbra
->install_in_progress
= false;
2404 struct bgp_path_info
*path
;
2405 struct bgp_path_info_extra
*extra
;
2407 bgp_pbr
->installed
= true;
2408 bgp_pbr
->install_in_progress
= false;
2409 bgp_pbr
->action
->refcnt
++;
2410 /* link bgp_info to bgp_pbr */
2411 path
= (struct bgp_path_info
*)bgp_pbr
->path
;
2412 extra
= bgp_path_info_extra_get(path
);
2413 listnode_add_force(&extra
->bgp_fs_iprule
,
2416 if (BGP_DEBUG(zebra
, ZEBRA
))
2417 zlog_debug("%s: Received RULE_INSTALLED", __func__
);
2419 case ZAPI_RULE_FAIL_REMOVE
:
2420 case ZAPI_RULE_REMOVED
:
2421 if (BGP_DEBUG(zebra
, ZEBRA
))
2422 zlog_debug("%s: Received RULE REMOVED", __func__
);
2429 static int ipset_notify_owner(ZAPI_CALLBACK_ARGS
)
2432 enum zapi_ipset_notify_owner note
;
2433 struct bgp_pbr_match
*bgp_pbim
;
2435 if (!zapi_ipset_notify_decode(zclient
->ibuf
,
2440 bgp_pbim
= bgp_pbr_match_ipset_lookup(vrf_id
, unique
);
2442 if (BGP_DEBUG(zebra
, ZEBRA
))
2443 zlog_debug("%s: Fail to look BGP match ( %u, ID %u)",
2444 __func__
, note
, unique
);
2449 case ZAPI_IPSET_FAIL_INSTALL
:
2450 if (BGP_DEBUG(zebra
, ZEBRA
))
2451 zlog_debug("%s: Received IPSET_FAIL_INSTALL", __func__
);
2452 bgp_pbim
->installed
= false;
2453 bgp_pbim
->install_in_progress
= false;
2455 case ZAPI_IPSET_INSTALLED
:
2456 bgp_pbim
->installed
= true;
2457 bgp_pbim
->install_in_progress
= false;
2458 if (BGP_DEBUG(zebra
, ZEBRA
))
2459 zlog_debug("%s: Received IPSET_INSTALLED", __func__
);
2461 case ZAPI_IPSET_FAIL_REMOVE
:
2462 case ZAPI_IPSET_REMOVED
:
2463 if (BGP_DEBUG(zebra
, ZEBRA
))
2464 zlog_debug("%s: Received IPSET REMOVED", __func__
);
2471 static int ipset_entry_notify_owner(ZAPI_CALLBACK_ARGS
)
2474 char ipset_name
[ZEBRA_IPSET_NAME_SIZE
];
2475 enum zapi_ipset_entry_notify_owner note
;
2476 struct bgp_pbr_match_entry
*bgp_pbime
;
2478 if (!zapi_ipset_entry_notify_decode(
2484 bgp_pbime
= bgp_pbr_match_ipset_entry_lookup(vrf_id
,
2488 if (BGP_DEBUG(zebra
, ZEBRA
))
2490 "%s: Fail to look BGP match entry (%u, ID %u)",
2491 __func__
, note
, unique
);
2496 case ZAPI_IPSET_ENTRY_FAIL_INSTALL
:
2497 if (BGP_DEBUG(zebra
, ZEBRA
))
2498 zlog_debug("%s: Received IPSET_ENTRY_FAIL_INSTALL",
2500 bgp_pbime
->installed
= false;
2501 bgp_pbime
->install_in_progress
= false;
2503 case ZAPI_IPSET_ENTRY_INSTALLED
:
2505 struct bgp_path_info
*path
;
2506 struct bgp_path_info_extra
*extra
;
2508 bgp_pbime
->installed
= true;
2509 bgp_pbime
->install_in_progress
= false;
2510 if (BGP_DEBUG(zebra
, ZEBRA
))
2511 zlog_debug("%s: Received IPSET_ENTRY_INSTALLED",
2513 /* link bgp_path_info to bpme */
2514 path
= (struct bgp_path_info
*)bgp_pbime
->path
;
2515 extra
= bgp_path_info_extra_get(path
);
2516 listnode_add_force(&extra
->bgp_fs_pbr
, bgp_pbime
);
2519 case ZAPI_IPSET_ENTRY_FAIL_REMOVE
:
2520 case ZAPI_IPSET_ENTRY_REMOVED
:
2521 if (BGP_DEBUG(zebra
, ZEBRA
))
2522 zlog_debug("%s: Received IPSET_ENTRY_REMOVED",
2529 static int iptable_notify_owner(ZAPI_CALLBACK_ARGS
)
2532 enum zapi_iptable_notify_owner note
;
2533 struct bgp_pbr_match
*bgpm
;
2535 if (!zapi_iptable_notify_decode(
2540 bgpm
= bgp_pbr_match_iptable_lookup(vrf_id
, unique
);
2542 if (BGP_DEBUG(zebra
, ZEBRA
))
2543 zlog_debug("%s: Fail to look BGP iptable (%u %u)",
2544 __func__
, note
, unique
);
2548 case ZAPI_IPTABLE_FAIL_INSTALL
:
2549 if (BGP_DEBUG(zebra
, ZEBRA
))
2550 zlog_debug("%s: Received IPTABLE_FAIL_INSTALL",
2552 bgpm
->installed_in_iptable
= false;
2553 bgpm
->install_iptable_in_progress
= false;
2555 case ZAPI_IPTABLE_INSTALLED
:
2556 bgpm
->installed_in_iptable
= true;
2557 bgpm
->install_iptable_in_progress
= false;
2558 if (BGP_DEBUG(zebra
, ZEBRA
))
2559 zlog_debug("%s: Received IPTABLE_INSTALLED", __func__
);
2560 bgpm
->action
->refcnt
++;
2562 case ZAPI_IPTABLE_FAIL_REMOVE
:
2563 case ZAPI_IPTABLE_REMOVED
:
2564 if (BGP_DEBUG(zebra
, ZEBRA
))
2565 zlog_debug("%s: Received IPTABLE REMOVED", __func__
);
2571 /* Process route notification messages from RIB */
2572 static int bgp_zebra_route_notify_owner(int command
, struct zclient
*zclient
,
2573 zebra_size_t length
, vrf_id_t vrf_id
)
2576 enum zapi_route_notify_owner note
;
2580 struct bgp_dest
*dest
;
2582 struct bgp_path_info
*pi
, *new_select
;
2584 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table_id
, ¬e
,
2586 zlog_err("%s : error in msg decode", __func__
);
2590 /* Get the bgp instance */
2591 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2593 flog_err(EC_BGP_INVALID_BGP_INSTANCE
,
2594 "%s : bgp instance not found vrf %d", __func__
,
2599 /* Find the bgp route node */
2600 dest
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
, &p
,
2606 case ZAPI_ROUTE_INSTALLED
:
2608 /* Clear the flags so that route can be processed */
2609 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALL_PENDING
);
2610 SET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALLED
);
2611 if (BGP_DEBUG(zebra
, ZEBRA
))
2612 zlog_debug("route %pRN : INSTALLED", dest
);
2613 /* Find the best route */
2614 for (pi
= dest
->info
; pi
; pi
= pi
->next
) {
2615 /* Process aggregate route */
2616 bgp_aggregate_increment(bgp
, &p
, pi
, afi
, safi
);
2617 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
))
2620 /* Advertise the route */
2622 group_announce_route(bgp
, afi
, safi
, dest
, new_select
);
2624 flog_err(EC_BGP_INVALID_ROUTE
,
2625 "selected route %pRN not found", dest
);
2627 bgp_dest_unlock_node(dest
);
2631 case ZAPI_ROUTE_REMOVED
:
2632 /* Route deleted from dataplane, reset the installed flag
2633 * so that route can be reinstalled when client sends
2636 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALLED
);
2637 if (BGP_DEBUG(zebra
, ZEBRA
))
2638 zlog_debug("route %pRN: Removed from Fib", dest
);
2640 case ZAPI_ROUTE_FAIL_INSTALL
:
2642 if (BGP_DEBUG(zebra
, ZEBRA
))
2643 zlog_debug("route: %pRN Failed to Install into Fib",
2645 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALL_PENDING
);
2646 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALLED
);
2647 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
2648 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
))
2652 group_announce_route(bgp
, afi
, safi
, dest
, new_select
);
2653 /* Error will be logged by zebra module */
2655 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
2656 if (BGP_DEBUG(zebra
, ZEBRA
))
2657 zlog_debug("route: %pRN removed due to better admin won",
2660 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALL_PENDING
);
2661 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALLED
);
2662 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
2663 bgp_aggregate_decrement(bgp
, &p
, pi
, afi
, safi
);
2664 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
))
2668 group_announce_route(bgp
, afi
, safi
, dest
, new_select
);
2669 /* No action required */
2671 case ZAPI_ROUTE_REMOVE_FAIL
:
2672 zlog_warn("%s: Route %pRN failure to remove",
2677 bgp_dest_unlock_node(dest
);
2681 /* this function is used to forge ip rule,
2682 * - either for iptable/ipset using fwmark id
2683 * - or for sample ip rule cmd
2685 static void bgp_encode_pbr_rule_action(struct stream
*s
,
2686 struct bgp_pbr_action
*pbra
,
2687 struct bgp_pbr_rule
*pbr
)
2690 uint8_t fam
= AF_INET
;
2691 char ifname
[INTERFACE_NAMSIZ
];
2693 if (pbra
->nh
.type
== NEXTHOP_TYPE_IPV6
)
2695 stream_putl(s
, 0); /* seqno unused */
2697 stream_putl(s
, pbr
->priority
);
2700 /* ruleno unused - priority change
2701 * ruleno permits distinguishing various FS PBR entries
2702 * - FS PBR entries based on ipset/iptables
2703 * - FS PBR entries based on iprule
2704 * the latter may contain default routing information injected by FS
2707 stream_putl(s
, pbr
->unique
);
2709 stream_putl(s
, pbra
->unique
);
2710 stream_putc(s
, 0); /* ip protocol being used */
2711 if (pbr
&& pbr
->flags
& MATCH_IP_SRC_SET
)
2712 memcpy(&pfx
, &(pbr
->src
), sizeof(struct prefix
));
2714 memset(&pfx
, 0, sizeof(pfx
));
2717 stream_putc(s
, pfx
.family
);
2718 stream_putc(s
, pfx
.prefixlen
);
2719 stream_put(s
, &pfx
.u
.prefix
, prefix_blen(&pfx
));
2721 stream_putw(s
, 0); /* src port */
2723 if (pbr
&& pbr
->flags
& MATCH_IP_DST_SET
)
2724 memcpy(&pfx
, &(pbr
->dst
), sizeof(struct prefix
));
2726 memset(&pfx
, 0, sizeof(pfx
));
2729 stream_putc(s
, pfx
.family
);
2730 stream_putc(s
, pfx
.prefixlen
);
2731 stream_put(s
, &pfx
.u
.prefix
, prefix_blen(&pfx
));
2733 stream_putw(s
, 0); /* dst port */
2734 stream_putc(s
, 0); /* dsfield */
2735 /* if pbr present, fwmark is not used */
2739 stream_putl(s
, pbra
->fwmark
); /* fwmark */
2741 stream_putl(s
, 0); /* queue id */
2742 stream_putw(s
, 0); /* vlan_id */
2743 stream_putw(s
, 0); /* vlan_flags */
2744 stream_putw(s
, 0); /* pcp */
2746 stream_putl(s
, pbra
->table_id
);
2748 memset(ifname
, 0, sizeof(ifname
));
2749 stream_put(s
, ifname
, INTERFACE_NAMSIZ
); /* ifname unused */
2752 static void bgp_encode_pbr_ipset_match(struct stream
*s
,
2753 struct bgp_pbr_match
*pbim
)
2755 stream_putl(s
, pbim
->unique
);
2756 stream_putl(s
, pbim
->type
);
2757 stream_putc(s
, pbim
->family
);
2758 stream_put(s
, pbim
->ipset_name
,
2759 ZEBRA_IPSET_NAME_SIZE
);
2762 static void bgp_encode_pbr_ipset_entry_match(struct stream
*s
,
2763 struct bgp_pbr_match_entry
*pbime
)
2765 stream_putl(s
, pbime
->unique
);
2766 /* check that back pointer is not null */
2767 stream_put(s
, pbime
->backpointer
->ipset_name
,
2768 ZEBRA_IPSET_NAME_SIZE
);
2770 stream_putc(s
, pbime
->src
.family
);
2771 stream_putc(s
, pbime
->src
.prefixlen
);
2772 stream_put(s
, &pbime
->src
.u
.prefix
, prefix_blen(&pbime
->src
));
2774 stream_putc(s
, pbime
->dst
.family
);
2775 stream_putc(s
, pbime
->dst
.prefixlen
);
2776 stream_put(s
, &pbime
->dst
.u
.prefix
, prefix_blen(&pbime
->dst
));
2778 stream_putw(s
, pbime
->src_port_min
);
2779 stream_putw(s
, pbime
->src_port_max
);
2780 stream_putw(s
, pbime
->dst_port_min
);
2781 stream_putw(s
, pbime
->dst_port_max
);
2782 stream_putc(s
, pbime
->proto
);
2785 static void bgp_encode_pbr_iptable_match(struct stream
*s
,
2786 struct bgp_pbr_action
*bpa
,
2787 struct bgp_pbr_match
*pbm
)
2789 stream_putl(s
, pbm
->unique2
);
2791 stream_putl(s
, pbm
->type
);
2793 stream_putl(s
, pbm
->flags
);
2795 /* TODO: correlate with what is contained
2796 * into bgp_pbr_action.
2797 * currently only forward supported
2799 if (bpa
->nh
.type
== NEXTHOP_TYPE_BLACKHOLE
)
2800 stream_putl(s
, ZEBRA_IPTABLES_DROP
);
2802 stream_putl(s
, ZEBRA_IPTABLES_FORWARD
);
2803 stream_putl(s
, bpa
->fwmark
);
2804 stream_put(s
, pbm
->ipset_name
,
2805 ZEBRA_IPSET_NAME_SIZE
);
2806 stream_putc(s
, pbm
->family
);
2807 stream_putw(s
, pbm
->pkt_len_min
);
2808 stream_putw(s
, pbm
->pkt_len_max
);
2809 stream_putw(s
, pbm
->tcp_flags
);
2810 stream_putw(s
, pbm
->tcp_mask_flags
);
2811 stream_putc(s
, pbm
->dscp_value
);
2812 stream_putc(s
, pbm
->fragment
);
2813 stream_putc(s
, pbm
->protocol
);
2814 stream_putw(s
, pbm
->flow_label
);
2817 /* BGP has established connection with Zebra. */
2818 static void bgp_zebra_connected(struct zclient
*zclient
)
2822 zclient_num_connects
++; /* increment even if not responding */
2824 /* Send the client registration */
2825 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
2827 /* At this point, we may or may not have BGP instances configured, but
2828 * we're only interested in the default VRF (others wouldn't have learnt
2829 * the VRF from Zebra yet.)
2831 bgp
= bgp_get_default();
2835 bgp_zebra_instance_register(bgp
);
2837 /* tell label pool that zebra is connected */
2838 bgp_lp_event_zebra_up();
2840 /* TODO - What if we have peers and networks configured, do we have to
2843 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp
, bgp
->peer
);
2846 static int bgp_zebra_process_local_es_add(ZAPI_CALLBACK_ARGS
)
2849 struct bgp
*bgp
= NULL
;
2850 struct stream
*s
= NULL
;
2851 char buf
[ESI_STR_LEN
];
2852 struct in_addr originator_ip
;
2857 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2862 stream_get(&esi
, s
, sizeof(esi_t
));
2863 originator_ip
.s_addr
= stream_get_ipv4(s
);
2864 active
= stream_getc(s
);
2865 df_pref
= stream_getw(s
);
2866 bypass
= stream_getc(s
);
2868 if (BGP_DEBUG(zebra
, ZEBRA
))
2870 "Rx add ESI %s originator-ip %pI4 active %u df_pref %u %s",
2871 esi_to_str(&esi
, buf
, sizeof(buf
)), &originator_ip
,
2872 active
, df_pref
, bypass
? "bypass" : "");
2874 frrtrace(5, frr_bgp
, evpn_mh_local_es_add_zrecv
, &esi
, originator_ip
,
2875 active
, bypass
, df_pref
);
2877 bgp_evpn_local_es_add(bgp
, &esi
, originator_ip
, active
, df_pref
,
2883 static int bgp_zebra_process_local_es_del(ZAPI_CALLBACK_ARGS
)
2886 struct bgp
*bgp
= NULL
;
2887 struct stream
*s
= NULL
;
2888 char buf
[ESI_STR_LEN
];
2890 memset(&esi
, 0, sizeof(esi_t
));
2891 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2896 stream_get(&esi
, s
, sizeof(esi_t
));
2898 if (BGP_DEBUG(zebra
, ZEBRA
))
2899 zlog_debug("Rx del ESI %s",
2900 esi_to_str(&esi
, buf
, sizeof(buf
)));
2902 frrtrace(1, frr_bgp
, evpn_mh_local_es_del_zrecv
, &esi
);
2904 bgp_evpn_local_es_del(bgp
, &esi
);
2909 static int bgp_zebra_process_local_es_evi(ZAPI_CALLBACK_ARGS
)
2915 char buf
[ESI_STR_LEN
];
2917 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2922 stream_get(&esi
, s
, sizeof(esi_t
));
2923 vni
= stream_getl(s
);
2925 if (BGP_DEBUG(zebra
, ZEBRA
))
2926 zlog_debug("Rx %s ESI %s VNI %u",
2927 (cmd
== ZEBRA_VNI_ADD
) ? "add" : "del",
2928 esi_to_str(&esi
, buf
, sizeof(buf
)), vni
);
2930 if (cmd
== ZEBRA_LOCAL_ES_EVI_ADD
) {
2931 frrtrace(2, frr_bgp
, evpn_mh_local_es_evi_add_zrecv
, &esi
, vni
);
2933 bgp_evpn_local_es_evi_add(bgp
, &esi
, vni
);
2935 frrtrace(2, frr_bgp
, evpn_mh_local_es_evi_del_zrecv
, &esi
, vni
);
2937 bgp_evpn_local_es_evi_del(bgp
, &esi
, vni
);
2943 static int bgp_zebra_process_local_l3vni(ZAPI_CALLBACK_ARGS
)
2947 struct ethaddr svi_rmac
, vrr_rmac
= {.octet
= {0} };
2948 struct in_addr originator_ip
;
2950 ifindex_t svi_ifindex
;
2951 bool is_anycast_mac
= false;
2953 memset(&svi_rmac
, 0, sizeof(svi_rmac
));
2954 memset(&originator_ip
, 0, sizeof(originator_ip
));
2956 l3vni
= stream_getl(s
);
2957 if (cmd
== ZEBRA_L3VNI_ADD
) {
2958 stream_get(&svi_rmac
, s
, sizeof(struct ethaddr
));
2959 originator_ip
.s_addr
= stream_get_ipv4(s
);
2960 stream_get(&filter
, s
, sizeof(int));
2961 svi_ifindex
= stream_getl(s
);
2962 stream_get(&vrr_rmac
, s
, sizeof(struct ethaddr
));
2963 is_anycast_mac
= stream_getl(s
);
2965 if (BGP_DEBUG(zebra
, ZEBRA
))
2967 "Rx L3-VNI ADD VRF %s VNI %u RMAC svi-mac %pEA vrr-mac %pEA filter %s svi-if %u",
2968 vrf_id_to_name(vrf_id
), l3vni
, &svi_rmac
,
2970 filter
? "prefix-routes-only" : "none",
2973 frrtrace(8, frr_bgp
, evpn_local_l3vni_add_zrecv
, l3vni
, vrf_id
,
2974 &svi_rmac
, &vrr_rmac
, filter
, originator_ip
,
2975 svi_ifindex
, is_anycast_mac
);
2977 bgp_evpn_local_l3vni_add(l3vni
, vrf_id
, &svi_rmac
, &vrr_rmac
,
2978 originator_ip
, filter
, svi_ifindex
,
2981 if (BGP_DEBUG(zebra
, ZEBRA
))
2982 zlog_debug("Rx L3-VNI DEL VRF %s VNI %u",
2983 vrf_id_to_name(vrf_id
), l3vni
);
2985 frrtrace(2, frr_bgp
, evpn_local_l3vni_del_zrecv
, l3vni
, vrf_id
);
2987 bgp_evpn_local_l3vni_del(l3vni
, vrf_id
);
2993 static int bgp_zebra_process_local_vni(ZAPI_CALLBACK_ARGS
)
2998 struct in_addr vtep_ip
= {INADDR_ANY
};
2999 vrf_id_t tenant_vrf_id
= VRF_DEFAULT
;
3000 struct in_addr mcast_grp
= {INADDR_ANY
};
3001 ifindex_t svi_ifindex
= 0;
3004 vni
= stream_getl(s
);
3005 if (cmd
== ZEBRA_VNI_ADD
) {
3006 vtep_ip
.s_addr
= stream_get_ipv4(s
);
3007 stream_get(&tenant_vrf_id
, s
, sizeof(vrf_id_t
));
3008 mcast_grp
.s_addr
= stream_get_ipv4(s
);
3009 stream_get(&svi_ifindex
, s
, sizeof(ifindex_t
));
3012 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
3016 if (BGP_DEBUG(zebra
, ZEBRA
))
3018 "Rx VNI %s VRF %s VNI %u tenant-vrf %s SVI ifindex %u",
3019 (cmd
== ZEBRA_VNI_ADD
) ? "add" : "del",
3020 vrf_id_to_name(vrf_id
), vni
,
3021 vrf_id_to_name(tenant_vrf_id
), svi_ifindex
);
3023 if (cmd
== ZEBRA_VNI_ADD
) {
3024 frrtrace(4, frr_bgp
, evpn_local_vni_add_zrecv
, vni
, vtep_ip
,
3025 tenant_vrf_id
, mcast_grp
);
3027 return bgp_evpn_local_vni_add(
3029 vtep_ip
.s_addr
!= INADDR_ANY
? vtep_ip
: bgp
->router_id
,
3030 tenant_vrf_id
, mcast_grp
, svi_ifindex
);
3032 frrtrace(1, frr_bgp
, evpn_local_vni_del_zrecv
, vni
);
3034 return bgp_evpn_local_vni_del(bgp
, vni
);
3038 static int bgp_zebra_process_local_macip(ZAPI_CALLBACK_ARGS
)
3047 uint32_t seqnum
= 0;
3049 char buf2
[ESI_STR_LEN
];
3052 memset(&ip
, 0, sizeof(ip
));
3054 vni
= stream_getl(s
);
3055 stream_get(&mac
.octet
, s
, ETH_ALEN
);
3056 ipa_len
= stream_getl(s
);
3057 if (ipa_len
!= 0 && ipa_len
!= IPV4_MAX_BYTELEN
3058 && ipa_len
!= IPV6_MAX_BYTELEN
) {
3059 flog_err(EC_BGP_MACIP_LEN
,
3060 "%u:Recv MACIP %s with invalid IP addr length %d",
3061 vrf_id
, (cmd
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
3068 (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
: IPADDR_V6
;
3069 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
3071 if (cmd
== ZEBRA_MACIP_ADD
) {
3072 flags
= stream_getc(s
);
3073 seqnum
= stream_getl(s
);
3074 stream_get(&esi
, s
, sizeof(esi_t
));
3076 state
= stream_getl(s
);
3077 memset(&esi
, 0, sizeof(esi_t
));
3080 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
3084 if (BGP_DEBUG(zebra
, ZEBRA
))
3086 "%u:Recv MACIP %s f 0x%x MAC %pEA IP %pIA VNI %u seq %u state %d ESI %s",
3087 vrf_id
, (cmd
== ZEBRA_MACIP_ADD
) ? "Add" : "Del", flags
,
3088 &mac
, &ip
, vni
, seqnum
, state
,
3089 esi_to_str(&esi
, buf2
, sizeof(buf2
)));
3091 if (cmd
== ZEBRA_MACIP_ADD
) {
3092 frrtrace(6, frr_bgp
, evpn_local_macip_add_zrecv
, vni
, &mac
, &ip
,
3093 flags
, seqnum
, &esi
);
3095 return bgp_evpn_local_macip_add(bgp
, vni
, &mac
, &ip
,
3096 flags
, seqnum
, &esi
);
3098 frrtrace(4, frr_bgp
, evpn_local_macip_del_zrecv
, vni
, &mac
, &ip
,
3101 return bgp_evpn_local_macip_del(bgp
, vni
, &mac
, &ip
, state
);
3105 static int bgp_zebra_process_local_ip_prefix(ZAPI_CALLBACK_ARGS
)
3107 struct stream
*s
= NULL
;
3108 struct bgp
*bgp_vrf
= NULL
;
3111 memset(&p
, 0, sizeof(p
));
3113 stream_get(&p
, s
, sizeof(struct prefix
));
3115 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
3119 if (BGP_DEBUG(zebra
, ZEBRA
))
3120 zlog_debug("Recv prefix %pFX %s on vrf %s", &p
,
3121 (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) ? "ADD" : "DEL",
3122 vrf_id_to_name(vrf_id
));
3124 if (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) {
3126 if (p
.family
== AF_INET
)
3127 bgp_evpn_advertise_type5_route(bgp_vrf
, &p
, NULL
,
3128 AFI_IP
, SAFI_UNICAST
);
3130 bgp_evpn_advertise_type5_route(bgp_vrf
, &p
, NULL
,
3131 AFI_IP6
, SAFI_UNICAST
);
3134 if (p
.family
== AF_INET
)
3135 bgp_evpn_withdraw_type5_route(bgp_vrf
, &p
, AFI_IP
,
3138 bgp_evpn_withdraw_type5_route(bgp_vrf
, &p
, AFI_IP6
,
3144 static int bgp_zebra_process_label_chunk(ZAPI_CALLBACK_ARGS
)
3146 struct stream
*s
= NULL
;
3147 uint8_t response_keep
;
3151 unsigned short instance
;
3154 STREAM_GETC(s
, proto
);
3155 STREAM_GETW(s
, instance
);
3156 STREAM_GETC(s
, response_keep
);
3157 STREAM_GETL(s
, first
);
3158 STREAM_GETL(s
, last
);
3160 if (zclient
->redist_default
!= proto
) {
3161 flog_err(EC_BGP_LM_ERROR
, "Got LM msg with wrong proto %u",
3165 if (zclient
->instance
!= instance
) {
3166 flog_err(EC_BGP_LM_ERROR
, "Got LM msg with wrong instance %u",
3172 first
< MPLS_LABEL_UNRESERVED_MIN
||
3173 last
> MPLS_LABEL_UNRESERVED_MAX
) {
3175 flog_err(EC_BGP_LM_ERROR
, "%s: Invalid Label chunk: %u - %u",
3176 __func__
, first
, last
);
3179 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3180 zlog_debug("Label Chunk assign: %u - %u (%u) ",
3181 first
, last
, response_keep
);
3184 bgp_lp_event_chunk(response_keep
, first
, last
);
3188 stream_failure
: /* for STREAM_GETX */
3192 extern struct zebra_privs_t bgpd_privs
;
3194 static int bgp_ifp_create(struct interface
*ifp
)
3198 if (BGP_DEBUG(zebra
, ZEBRA
))
3199 zlog_debug("Rx Intf add VRF %u IF %s", ifp
->vrf
->vrf_id
,
3202 bgp
= ifp
->vrf
->info
;
3206 bgp_mac_add_mac_entry(ifp
);
3208 bgp_update_interface_nbrs(bgp
, ifp
, ifp
);
3209 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
3213 static int bgp_zebra_process_srv6_locator_chunk(ZAPI_CALLBACK_ARGS
)
3215 struct stream
*s
= NULL
;
3216 struct bgp
*bgp
= bgp_get_default();
3217 struct listnode
*node
;
3218 struct srv6_locator_chunk
*c
;
3219 struct srv6_locator_chunk
*chunk
= srv6_locator_chunk_alloc();
3222 zapi_srv6_locator_chunk_decode(s
, chunk
);
3224 if (strcmp(bgp
->srv6_locator_name
, chunk
->locator_name
) != 0) {
3225 zlog_err("%s: Locator name unmatch %s:%s", __func__
,
3226 bgp
->srv6_locator_name
, chunk
->locator_name
);
3227 srv6_locator_chunk_free(&chunk
);
3231 for (ALL_LIST_ELEMENTS_RO(bgp
->srv6_locator_chunks
, node
, c
)) {
3232 if (!prefix_cmp(&c
->prefix
, &chunk
->prefix
)) {
3233 srv6_locator_chunk_free(&chunk
);
3238 listnode_add(bgp
->srv6_locator_chunks
, chunk
);
3239 vpn_leak_postchange_all();
3243 static int bgp_zebra_process_srv6_locator_add(ZAPI_CALLBACK_ARGS
)
3245 struct srv6_locator loc
= {};
3246 struct bgp
*bgp
= bgp_get_default();
3247 const char *loc_name
= bgp
->srv6_locator_name
;
3249 if (zapi_srv6_locator_decode(zclient
->ibuf
, &loc
) < 0)
3252 if (!bgp
|| !bgp
->srv6_enabled
)
3255 if (bgp_zebra_srv6_manager_get_locator_chunk(loc_name
) < 0)
3261 static int bgp_zebra_process_srv6_locator_delete(ZAPI_CALLBACK_ARGS
)
3263 struct srv6_locator loc
= {};
3264 struct bgp
*bgp
= bgp_get_default();
3265 struct listnode
*node
, *nnode
;
3266 struct srv6_locator_chunk
*chunk
, *tovpn_sid_locator
;
3267 struct bgp_srv6_function
*func
;
3268 struct bgp
*bgp_vrf
;
3269 struct in6_addr
*tovpn_sid
;
3270 struct prefix_ipv6 tmp_prefi
;
3272 if (zapi_srv6_locator_decode(zclient
->ibuf
, &loc
) < 0)
3276 for (ALL_LIST_ELEMENTS(bgp
->srv6_locator_chunks
, node
, nnode
, chunk
))
3277 if (prefix_match((struct prefix
*)&loc
.prefix
,
3278 (struct prefix
*)&chunk
->prefix
)) {
3279 listnode_delete(bgp
->srv6_locator_chunks
, chunk
);
3280 srv6_locator_chunk_free(&chunk
);
3283 // refresh functions
3284 for (ALL_LIST_ELEMENTS(bgp
->srv6_functions
, node
, nnode
, func
)) {
3285 tmp_prefi
.family
= AF_INET6
;
3286 tmp_prefi
.prefixlen
= 128;
3287 tmp_prefi
.prefix
= func
->sid
;
3288 if (prefix_match((struct prefix
*)&loc
.prefix
,
3289 (struct prefix
*)&tmp_prefi
)) {
3290 listnode_delete(bgp
->srv6_functions
, func
);
3291 XFREE(MTYPE_BGP_SRV6_FUNCTION
, func
);
3295 // refresh tovpn_sid
3296 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
3297 if (bgp_vrf
->inst_type
!= BGP_INSTANCE_TYPE_VRF
)
3300 // refresh vpnv4 tovpn_sid
3301 tovpn_sid
= bgp_vrf
->vpn_policy
[AFI_IP
].tovpn_sid
;
3303 tmp_prefi
.family
= AF_INET6
;
3304 tmp_prefi
.prefixlen
= 128;
3305 tmp_prefi
.prefix
= *tovpn_sid
;
3306 if (prefix_match((struct prefix
*)&loc
.prefix
,
3307 (struct prefix
*)&tmp_prefi
))
3308 XFREE(MTYPE_BGP_SRV6_SID
,
3309 bgp_vrf
->vpn_policy
[AFI_IP
].tovpn_sid
);
3312 // refresh vpnv6 tovpn_sid
3313 tovpn_sid
= bgp_vrf
->vpn_policy
[AFI_IP6
].tovpn_sid
;
3315 tmp_prefi
.family
= AF_INET6
;
3316 tmp_prefi
.prefixlen
= 128;
3317 tmp_prefi
.prefix
= *tovpn_sid
;
3318 if (prefix_match((struct prefix
*)&loc
.prefix
,
3319 (struct prefix
*)&tmp_prefi
))
3320 XFREE(MTYPE_BGP_SRV6_SID
,
3321 bgp_vrf
->vpn_policy
[AFI_IP6
].tovpn_sid
);
3324 /* refresh per-vrf tovpn_sid */
3325 tovpn_sid
= bgp_vrf
->tovpn_sid
;
3327 tmp_prefi
.family
= AF_INET6
;
3328 tmp_prefi
.prefixlen
= IPV6_MAX_BITLEN
;
3329 tmp_prefi
.prefix
= *tovpn_sid
;
3330 if (prefix_match((struct prefix
*)&loc
.prefix
,
3331 (struct prefix
*)&tmp_prefi
))
3332 XFREE(MTYPE_BGP_SRV6_SID
, bgp_vrf
->tovpn_sid
);
3336 vpn_leak_postchange_all();
3338 /* refresh tovpn_sid_locator */
3339 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
3340 if (bgp_vrf
->inst_type
!= BGP_INSTANCE_TYPE_VRF
)
3343 /* refresh vpnv4 tovpn_sid_locator */
3345 bgp_vrf
->vpn_policy
[AFI_IP
].tovpn_sid_locator
;
3346 if (tovpn_sid_locator
) {
3347 tmp_prefi
.family
= AF_INET6
;
3348 tmp_prefi
.prefixlen
= IPV6_MAX_BITLEN
;
3349 tmp_prefi
.prefix
= tovpn_sid_locator
->prefix
.prefix
;
3350 if (prefix_match((struct prefix
*)&loc
.prefix
,
3351 (struct prefix
*)&tmp_prefi
))
3352 srv6_locator_chunk_free(
3353 &bgp_vrf
->vpn_policy
[AFI_IP
]
3354 .tovpn_sid_locator
);
3357 /* refresh vpnv6 tovpn_sid_locator */
3359 bgp_vrf
->vpn_policy
[AFI_IP6
].tovpn_sid_locator
;
3360 if (tovpn_sid_locator
) {
3361 tmp_prefi
.family
= AF_INET6
;
3362 tmp_prefi
.prefixlen
= IPV6_MAX_BITLEN
;
3363 tmp_prefi
.prefix
= tovpn_sid_locator
->prefix
.prefix
;
3364 if (prefix_match((struct prefix
*)&loc
.prefix
,
3365 (struct prefix
*)&tmp_prefi
))
3366 srv6_locator_chunk_free(
3367 &bgp_vrf
->vpn_policy
[AFI_IP6
]
3368 .tovpn_sid_locator
);
3371 /* refresh per-vrf tovpn_sid_locator */
3372 tovpn_sid_locator
= bgp_vrf
->tovpn_sid_locator
;
3373 if (tovpn_sid_locator
) {
3374 tmp_prefi
.family
= AF_INET6
;
3375 tmp_prefi
.prefixlen
= IPV6_MAX_BITLEN
;
3376 tmp_prefi
.prefix
= tovpn_sid_locator
->prefix
.prefix
;
3377 if (prefix_match((struct prefix
*)&loc
.prefix
,
3378 (struct prefix
*)&tmp_prefi
))
3379 srv6_locator_chunk_free(
3380 &bgp_vrf
->tovpn_sid_locator
);
3387 static zclient_handler
*const bgp_handlers
[] = {
3388 [ZEBRA_ROUTER_ID_UPDATE
] = bgp_router_id_update
,
3389 [ZEBRA_INTERFACE_ADDRESS_ADD
] = bgp_interface_address_add
,
3390 [ZEBRA_INTERFACE_ADDRESS_DELETE
] = bgp_interface_address_delete
,
3391 [ZEBRA_INTERFACE_NBR_ADDRESS_ADD
] = bgp_interface_nbr_address_add
,
3392 [ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
] = bgp_interface_nbr_address_delete
,
3393 [ZEBRA_INTERFACE_VRF_UPDATE
] = bgp_interface_vrf_update
,
3394 [ZEBRA_REDISTRIBUTE_ROUTE_ADD
] = zebra_read_route
,
3395 [ZEBRA_REDISTRIBUTE_ROUTE_DEL
] = zebra_read_route
,
3396 [ZEBRA_NEXTHOP_UPDATE
] = bgp_read_nexthop_update
,
3397 [ZEBRA_FEC_UPDATE
] = bgp_read_fec_update
,
3398 [ZEBRA_LOCAL_ES_ADD
] = bgp_zebra_process_local_es_add
,
3399 [ZEBRA_LOCAL_ES_DEL
] = bgp_zebra_process_local_es_del
,
3400 [ZEBRA_VNI_ADD
] = bgp_zebra_process_local_vni
,
3401 [ZEBRA_LOCAL_ES_EVI_ADD
] = bgp_zebra_process_local_es_evi
,
3402 [ZEBRA_LOCAL_ES_EVI_DEL
] = bgp_zebra_process_local_es_evi
,
3403 [ZEBRA_VNI_DEL
] = bgp_zebra_process_local_vni
,
3404 [ZEBRA_MACIP_ADD
] = bgp_zebra_process_local_macip
,
3405 [ZEBRA_MACIP_DEL
] = bgp_zebra_process_local_macip
,
3406 [ZEBRA_L3VNI_ADD
] = bgp_zebra_process_local_l3vni
,
3407 [ZEBRA_L3VNI_DEL
] = bgp_zebra_process_local_l3vni
,
3408 [ZEBRA_IP_PREFIX_ROUTE_ADD
] = bgp_zebra_process_local_ip_prefix
,
3409 [ZEBRA_IP_PREFIX_ROUTE_DEL
] = bgp_zebra_process_local_ip_prefix
,
3410 [ZEBRA_GET_LABEL_CHUNK
] = bgp_zebra_process_label_chunk
,
3411 [ZEBRA_RULE_NOTIFY_OWNER
] = rule_notify_owner
,
3412 [ZEBRA_IPSET_NOTIFY_OWNER
] = ipset_notify_owner
,
3413 [ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
] = ipset_entry_notify_owner
,
3414 [ZEBRA_IPTABLE_NOTIFY_OWNER
] = iptable_notify_owner
,
3415 [ZEBRA_ROUTE_NOTIFY_OWNER
] = bgp_zebra_route_notify_owner
,
3416 [ZEBRA_SRV6_LOCATOR_ADD
] = bgp_zebra_process_srv6_locator_add
,
3417 [ZEBRA_SRV6_LOCATOR_DELETE
] = bgp_zebra_process_srv6_locator_delete
,
3418 [ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK
] =
3419 bgp_zebra_process_srv6_locator_chunk
,
3420 [ZEBRA_OPAQUE_MESSAGE
] = bgp_opaque_msg_handler
,
3423 static int bgp_if_new_hook(struct interface
*ifp
)
3425 struct bgp_interface
*iifp
;
3429 iifp
= XCALLOC(MTYPE_BGP_IF_INFO
, sizeof(struct bgp_interface
));
3435 static int bgp_if_delete_hook(struct interface
*ifp
)
3437 XFREE(MTYPE_BGP_IF_INFO
, ifp
->info
);
3441 void bgp_if_init(void)
3443 /* Initialize Zebra interface data structure. */
3444 hook_register_prio(if_add
, 0, bgp_if_new_hook
);
3445 hook_register_prio(if_del
, 0, bgp_if_delete_hook
);
3448 void bgp_zebra_init(struct thread_master
*master
, unsigned short instance
)
3450 zclient_num_connects
= 0;
3452 if_zapi_callbacks(bgp_ifp_create
, bgp_ifp_up
,
3453 bgp_ifp_down
, bgp_ifp_destroy
);
3455 /* Set default values. */
3456 zclient
= zclient_new(master
, &zclient_options_default
, bgp_handlers
,
3457 array_size(bgp_handlers
));
3458 zclient_init(zclient
, ZEBRA_ROUTE_BGP
, 0, &bgpd_privs
);
3459 zclient
->zebra_connected
= bgp_zebra_connected
;
3460 zclient
->instance
= instance
;
3463 void bgp_zebra_destroy(void)
3465 if (zclient
== NULL
)
3467 zclient_stop(zclient
);
3468 zclient_free(zclient
);
3472 int bgp_zebra_num_connects(void)
3474 return zclient_num_connects
;
3477 void bgp_send_pbr_rule_action(struct bgp_pbr_action
*pbra
,
3478 struct bgp_pbr_rule
*pbr
,
3483 if (pbra
->install_in_progress
&& !pbr
)
3485 if (pbr
&& pbr
->install_in_progress
)
3487 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3489 zlog_debug("%s: table %d (ip rule) %d", __func__
,
3490 pbra
->table_id
, install
);
3492 zlog_debug("%s: table %d fwmark %d %d", __func__
,
3493 pbra
->table_id
, pbra
->fwmark
, install
);
3498 zclient_create_header(s
,
3499 install
? ZEBRA_RULE_ADD
: ZEBRA_RULE_DELETE
,
3501 stream_putl(s
, 1); /* send one pbr action */
3503 bgp_encode_pbr_rule_action(s
, pbra
, pbr
);
3505 stream_putw_at(s
, 0, stream_get_endp(s
));
3506 if ((zclient_send_message(zclient
) != ZCLIENT_SEND_FAILURE
)
3509 pbra
->install_in_progress
= true;
3511 pbr
->install_in_progress
= true;
3515 void bgp_send_pbr_ipset_match(struct bgp_pbr_match
*pbrim
, bool install
)
3519 if (pbrim
->install_in_progress
)
3521 if (BGP_DEBUG(zebra
, ZEBRA
))
3522 zlog_debug("%s: name %s type %d %d, ID %u", __func__
,
3523 pbrim
->ipset_name
, pbrim
->type
, install
,
3528 zclient_create_header(s
,
3529 install
? ZEBRA_IPSET_CREATE
:
3530 ZEBRA_IPSET_DESTROY
,
3533 stream_putl(s
, 1); /* send one pbr action */
3535 bgp_encode_pbr_ipset_match(s
, pbrim
);
3537 stream_putw_at(s
, 0, stream_get_endp(s
));
3538 if ((zclient_send_message(zclient
) != ZCLIENT_SEND_FAILURE
) && install
)
3539 pbrim
->install_in_progress
= true;
3542 void bgp_send_pbr_ipset_entry_match(struct bgp_pbr_match_entry
*pbrime
,
3547 if (pbrime
->install_in_progress
)
3549 if (BGP_DEBUG(zebra
, ZEBRA
))
3550 zlog_debug("%s: name %s %d %d, ID %u", __func__
,
3551 pbrime
->backpointer
->ipset_name
, pbrime
->unique
,
3552 install
, pbrime
->unique
);
3556 zclient_create_header(s
,
3557 install
? ZEBRA_IPSET_ENTRY_ADD
:
3558 ZEBRA_IPSET_ENTRY_DELETE
,
3561 stream_putl(s
, 1); /* send one pbr action */
3563 bgp_encode_pbr_ipset_entry_match(s
, pbrime
);
3565 stream_putw_at(s
, 0, stream_get_endp(s
));
3566 if ((zclient_send_message(zclient
) != ZCLIENT_SEND_FAILURE
) && install
)
3567 pbrime
->install_in_progress
= true;
3570 static void bgp_encode_pbr_interface_list(struct bgp
*bgp
, struct stream
*s
,
3573 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
3574 struct bgp_pbr_interface_head
*head
;
3575 struct bgp_pbr_interface
*pbr_if
;
3576 struct interface
*ifp
;
3580 if (family
== AF_INET
)
3581 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
3583 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv6
);
3584 RB_FOREACH (pbr_if
, bgp_pbr_interface_head
, head
) {
3585 ifp
= if_lookup_by_name(pbr_if
->name
, bgp
->vrf_id
);
3587 stream_putl(s
, ifp
->ifindex
);
3591 static int bgp_pbr_get_ifnumber(struct bgp
*bgp
, uint8_t family
)
3593 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
3594 struct bgp_pbr_interface_head
*head
;
3595 struct bgp_pbr_interface
*pbr_if
;
3600 if (family
== AF_INET
)
3601 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
3603 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv6
);
3604 RB_FOREACH (pbr_if
, bgp_pbr_interface_head
, head
) {
3605 if (if_lookup_by_name(pbr_if
->name
, bgp
->vrf_id
))
3611 void bgp_send_pbr_iptable(struct bgp_pbr_action
*pba
,
3612 struct bgp_pbr_match
*pbm
,
3619 if (pbm
->install_iptable_in_progress
)
3621 if (BGP_DEBUG(zebra
, ZEBRA
))
3622 zlog_debug("%s: name %s type %d mark %d %d, ID %u", __func__
,
3623 pbm
->ipset_name
, pbm
->type
, pba
->fwmark
, install
,
3628 zclient_create_header(s
,
3629 install
? ZEBRA_IPTABLE_ADD
:
3630 ZEBRA_IPTABLE_DELETE
,
3633 bgp_encode_pbr_iptable_match(s
, pba
, pbm
);
3634 nb_interface
= bgp_pbr_get_ifnumber(pba
->bgp
, pbm
->family
);
3635 stream_putl(s
, nb_interface
);
3637 bgp_encode_pbr_interface_list(pba
->bgp
, s
, pbm
->family
);
3638 stream_putw_at(s
, 0, stream_get_endp(s
));
3639 ret
= zclient_send_message(zclient
);
3641 if (ret
!= ZCLIENT_SEND_FAILURE
)
3644 pbm
->install_iptable_in_progress
= true;
3648 /* inject in table <table_id> a default route to:
3649 * - if nexthop IP is present : to this nexthop
3650 * - if vrf is different from local : to the matching VRF
3652 void bgp_zebra_announce_default(struct bgp
*bgp
, struct nexthop
*nh
,
3653 afi_t afi
, uint32_t table_id
, bool announce
)
3655 struct zapi_nexthop
*api_nh
;
3656 struct zapi_route api
;
3659 if (!nh
|| (nh
->type
!= NEXTHOP_TYPE_IPV4
3660 && nh
->type
!= NEXTHOP_TYPE_IPV6
)
3661 || nh
->vrf_id
== VRF_UNKNOWN
)
3664 /* in vrf-lite, no default route has to be announced
3665 * the table id of vrf is directly used to divert traffic
3667 if (!vrf_is_backend_netns() && bgp
->vrf_id
!= nh
->vrf_id
)
3670 memset(&p
, 0, sizeof(p
));
3671 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
)
3673 p
.family
= afi2family(afi
);
3674 memset(&api
, 0, sizeof(api
));
3675 api
.vrf_id
= bgp
->vrf_id
;
3676 api
.type
= ZEBRA_ROUTE_BGP
;
3677 api
.safi
= SAFI_UNICAST
;
3679 api
.tableid
= table_id
;
3680 api
.nexthop_num
= 1;
3681 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
3682 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
3683 api_nh
= &api
.nexthops
[0];
3685 api
.distance
= ZEBRA_EBGP_DISTANCE_DEFAULT
;
3686 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
3689 if (afi
== AFI_IP
&& nh
->gate
.ipv4
.s_addr
!= INADDR_ANY
) {
3690 api_nh
->vrf_id
= nh
->vrf_id
;
3691 api_nh
->gate
.ipv4
= nh
->gate
.ipv4
;
3692 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
3694 if (BGP_DEBUG(zebra
, ZEBRA
))
3696 "BGP: %s default route to %pI4 table %d (redirect IP)",
3697 announce
? "adding" : "withdrawing",
3698 &nh
->gate
.ipv4
, table_id
);
3700 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
3701 : ZEBRA_ROUTE_DELETE
,
3703 } else if (afi
== AFI_IP6
&&
3704 memcmp(&nh
->gate
.ipv6
,
3705 &in6addr_any
, sizeof(struct in6_addr
))) {
3706 api_nh
->vrf_id
= nh
->vrf_id
;
3707 memcpy(&api_nh
->gate
.ipv6
, &nh
->gate
.ipv6
,
3708 sizeof(struct in6_addr
));
3709 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
3711 if (BGP_DEBUG(zebra
, ZEBRA
))
3713 "BGP: %s default route to %pI6 table %d (redirect IP)",
3714 announce
? "adding" : "withdrawing",
3715 &nh
->gate
.ipv6
, table_id
);
3717 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
3718 : ZEBRA_ROUTE_DELETE
,
3720 } else if (nh
->vrf_id
!= bgp
->vrf_id
) {
3722 struct interface
*ifp
;
3724 vrf
= vrf_lookup_by_id(nh
->vrf_id
);
3727 /* create default route with interface <VRF>
3728 * with nexthop-vrf <VRF>
3730 ifp
= if_lookup_by_name_vrf(vrf
->name
, vrf
);
3733 api_nh
->vrf_id
= nh
->vrf_id
;
3734 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
3735 api_nh
->ifindex
= ifp
->ifindex
;
3736 if (BGP_DEBUG(zebra
, ZEBRA
))
3737 zlog_info("BGP: %s default route to %s table %d (redirect VRF)",
3738 announce
? "adding" : "withdrawing",
3739 vrf
->name
, table_id
);
3740 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
3741 : ZEBRA_ROUTE_DELETE
,
3747 /* Send capabilities to RIB */
3748 int bgp_zebra_send_capabilities(struct bgp
*bgp
, bool disable
)
3750 struct zapi_cap api
;
3751 int ret
= BGP_GR_SUCCESS
;
3753 if (zclient
== NULL
) {
3754 if (BGP_DEBUG(zebra
, ZEBRA
))
3755 zlog_debug("zclient invalid");
3756 return BGP_GR_FAILURE
;
3759 /* Check if the client is connected */
3760 if ((zclient
->sock
< 0) || (zclient
->t_connect
)) {
3761 if (BGP_DEBUG(zebra
, ZEBRA
))
3762 zlog_debug("client not connected");
3763 return BGP_GR_FAILURE
;
3766 /* Check if capability is already sent. If the flag force is set
3767 * send the capability since this can be initial bgp configuration
3769 memset(&api
, 0, sizeof(api
));
3771 api
.cap
= ZEBRA_CLIENT_GR_DISABLE
;
3772 api
.vrf_id
= bgp
->vrf_id
;
3774 api
.cap
= ZEBRA_CLIENT_GR_CAPABILITIES
;
3775 api
.stale_removal_time
= bgp
->rib_stale_time
;
3776 api
.vrf_id
= bgp
->vrf_id
;
3779 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
, &api
)
3780 == ZCLIENT_SEND_FAILURE
) {
3781 zlog_err("error sending capability");
3782 ret
= BGP_GR_FAILURE
;
3785 bgp
->present_zebra_gr_state
= ZEBRA_GR_DISABLE
;
3787 bgp
->present_zebra_gr_state
= ZEBRA_GR_ENABLE
;
3789 if (BGP_DEBUG(zebra
, ZEBRA
))
3790 zlog_debug("send capabilty success");
3791 ret
= BGP_GR_SUCCESS
;
3796 /* Send route update pesding or completed status to RIB for the
3797 * specific AFI, SAFI
3799 int bgp_zebra_update(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
)
3801 struct zapi_cap api
= {0};
3803 if (zclient
== NULL
) {
3804 if (BGP_DEBUG(zebra
, ZEBRA
))
3805 zlog_debug("zclient == NULL, invalid");
3806 return BGP_GR_FAILURE
;
3809 /* Check if the client is connected */
3810 if ((zclient
->sock
< 0) || (zclient
->t_connect
)) {
3811 if (BGP_DEBUG(zebra
, ZEBRA
))
3812 zlog_debug("client not connected");
3813 return BGP_GR_FAILURE
;
3818 api
.vrf_id
= vrf_id
;
3821 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
, &api
)
3822 == ZCLIENT_SEND_FAILURE
) {
3823 if (BGP_DEBUG(zebra
, ZEBRA
))
3824 zlog_debug("error sending capability");
3825 return BGP_GR_FAILURE
;
3827 return BGP_GR_SUCCESS
;
3831 /* Send RIB stale timer update */
3832 int bgp_zebra_stale_timer_update(struct bgp
*bgp
)
3834 struct zapi_cap api
;
3836 if (zclient
== NULL
) {
3837 if (BGP_DEBUG(zebra
, ZEBRA
))
3838 zlog_debug("zclient invalid");
3839 return BGP_GR_FAILURE
;
3842 /* Check if the client is connected */
3843 if ((zclient
->sock
< 0) || (zclient
->t_connect
)) {
3844 if (BGP_DEBUG(zebra
, ZEBRA
))
3845 zlog_debug("client not connected");
3846 return BGP_GR_FAILURE
;
3849 memset(&api
, 0, sizeof(api
));
3850 api
.cap
= ZEBRA_CLIENT_RIB_STALE_TIME
;
3851 api
.stale_removal_time
= bgp
->rib_stale_time
;
3852 api
.vrf_id
= bgp
->vrf_id
;
3853 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
, &api
)
3854 == ZCLIENT_SEND_FAILURE
) {
3855 if (BGP_DEBUG(zebra
, ZEBRA
))
3856 zlog_debug("error sending capability");
3857 return BGP_GR_FAILURE
;
3859 if (BGP_DEBUG(zebra
, ZEBRA
))
3860 zlog_debug("send capabilty success");
3861 return BGP_GR_SUCCESS
;
3864 int bgp_zebra_srv6_manager_get_locator_chunk(const char *name
)
3866 return srv6_manager_get_locator_chunk(zclient
, name
);
3869 int bgp_zebra_srv6_manager_release_locator_chunk(const char *name
)
3871 return srv6_manager_release_locator_chunk(zclient
, name
);
3875 * ORR messages between processes
3877 static int bgp_opaque_msg_handler(ZAPI_CALLBACK_ARGS
)
3880 struct zapi_opaque_msg info
;
3881 struct orr_igp_metric_info table
;
3886 if (zclient_opaque_decode(s
, &info
) != 0) {
3887 bgp_orr_debug("%s: opaque decode failed", __func__
);
3891 switch (info
.type
) {
3892 case ORR_IGP_METRIC_UPDATE
:
3893 STREAM_GET(&table
, s
, sizeof(table
));
3894 ret
= bgg_orr_message_process(BGP_ORR_IMSG_IGP_METRIC_UPDATE
,