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"
68 /* All information about zebra. */
69 struct zclient
*zclient
= NULL
;
71 /* hook to indicate vrf status change for SNMP */
72 DEFINE_HOOK(bgp_vrf_status_changed
, (struct bgp
*bgp
, struct interface
*ifp
),
75 /* Can we install into zebra? */
76 static inline bool bgp_install_info_to_zebra(struct bgp
*bgp
)
78 if (zclient
->sock
<= 0)
81 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
83 "%s: No zebra instance to talk to, not installing information",
91 int zclient_num_connects
;
93 /* Router-id update message from zebra. */
94 static int bgp_router_id_update(ZAPI_CALLBACK_ARGS
)
96 struct prefix router_id
;
98 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
100 if (BGP_DEBUG(zebra
, ZEBRA
))
101 zlog_debug("Rx Router Id update VRF %u Id %pFX", vrf_id
,
104 bgp_router_id_zebra_bump(vrf_id
, &router_id
);
108 /* Nexthop update message from zebra. */
109 static int bgp_read_nexthop_update(ZAPI_CALLBACK_ARGS
)
111 bgp_parse_nexthop_update(cmd
, vrf_id
);
115 static int bgp_read_import_check_update(ZAPI_CALLBACK_ARGS
)
117 bgp_parse_nexthop_update(cmd
, vrf_id
);
121 /* Set or clear interface on which unnumbered neighbor is configured. This
122 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
125 static void bgp_update_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
,
126 struct interface
*upd_ifp
)
128 struct listnode
*node
, *nnode
;
131 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
132 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)) {
135 bgp_zebra_initiate_radv(bgp
, peer
);
137 bgp_zebra_terminate_radv(bgp
, peer
);
144 static int bgp_read_fec_update(int command
, struct zclient
*zclient
,
147 bgp_parse_fec_update();
151 static void bgp_start_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
)
153 struct listnode
*node
, *nnode
;
156 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
157 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)
158 && peer
->status
!= Established
) {
159 if (peer_active(peer
))
160 BGP_EVENT_ADD(peer
, BGP_Stop
);
161 BGP_EVENT_ADD(peer
, BGP_Start
);
166 static void bgp_nbr_connected_add(struct bgp
*bgp
, struct nbr_connected
*ifc
)
168 struct listnode
*node
;
169 struct connected
*connected
;
170 struct interface
*ifp
;
173 /* Kick-off the FSM for any relevant peers only if there is a
174 * valid local address on the interface.
177 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
178 p
= connected
->address
;
179 if (p
->family
== AF_INET6
180 && IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
186 bgp_start_interface_nbrs(bgp
, ifp
);
189 static void bgp_nbr_connected_delete(struct bgp
*bgp
, struct nbr_connected
*ifc
,
192 struct listnode
*node
, *nnode
;
194 struct interface
*ifp
;
196 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
198 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)) {
199 peer
->last_reset
= PEER_DOWN_NBR_ADDR_DEL
;
200 BGP_EVENT_ADD(peer
, BGP_Stop
);
203 /* Free neighbor also, if we're asked to. */
206 listnode_delete(ifp
->nbr_connected
, ifc
);
207 nbr_connected_free(ifc
);
211 static int bgp_ifp_destroy(struct interface
*ifp
)
215 bgp
= bgp_lookup_by_vrf_id(ifp
->vrf_id
);
217 if (BGP_DEBUG(zebra
, ZEBRA
))
218 zlog_debug("Rx Intf del VRF %u IF %s", ifp
->vrf_id
, ifp
->name
);
221 bgp_update_interface_nbrs(bgp
, ifp
, NULL
);
222 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
225 bgp_mac_del_mac_entry(ifp
);
230 static int bgp_ifp_up(struct interface
*ifp
)
233 struct nbr_connected
*nc
;
234 struct listnode
*node
, *nnode
;
237 bgp
= bgp_lookup_by_vrf_id(ifp
->vrf_id
);
239 bgp_mac_add_mac_entry(ifp
);
241 if (BGP_DEBUG(zebra
, ZEBRA
))
242 zlog_debug("Rx Intf up VRF %u IF %s", ifp
->vrf_id
, ifp
->name
);
247 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
248 bgp_connected_add(bgp
, c
);
250 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
251 bgp_nbr_connected_add(bgp
, nc
);
253 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
259 static int bgp_ifp_down(struct interface
*ifp
)
262 struct nbr_connected
*nc
;
263 struct listnode
*node
, *nnode
;
267 bgp
= bgp_lookup_by_vrf_id(ifp
->vrf_id
);
269 bgp_mac_del_mac_entry(ifp
);
271 if (BGP_DEBUG(zebra
, ZEBRA
))
272 zlog_debug("Rx Intf down VRF %u IF %s", ifp
->vrf_id
, ifp
->name
);
277 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
278 bgp_connected_delete(bgp
, c
);
280 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
281 bgp_nbr_connected_delete(bgp
, nc
, 1);
283 /* Fast external-failover */
284 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
)) {
286 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
287 /* Take down directly connected peers. */
288 if ((peer
->ttl
!= BGP_DEFAULT_TTL
)
289 && (peer
->gtsm_hops
!= BGP_GTSM_HOPS_CONNECTED
))
292 if (ifp
== peer
->nexthop
.ifp
) {
293 BGP_EVENT_ADD(peer
, BGP_Stop
);
294 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
299 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
300 bgp_nht_ifp_down(ifp
);
305 static int bgp_interface_address_add(ZAPI_CALLBACK_ARGS
)
307 struct connected
*ifc
;
310 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
312 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
317 if (bgp_debug_zebra(ifc
->address
))
318 zlog_debug("Rx Intf address add VRF %u IF %s addr %pFX", vrf_id
,
319 ifc
->ifp
->name
, ifc
->address
);
324 if (if_is_operative(ifc
->ifp
)) {
325 bgp_connected_add(bgp
, ifc
);
327 /* If we have learnt of any neighbors on this interface,
328 * check to kick off any BGP interface-based neighbors,
329 * but only if this is a link-local address.
331 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
)
332 && !list_isempty(ifc
->ifp
->nbr_connected
))
333 bgp_start_interface_nbrs(bgp
, ifc
->ifp
);
339 static int bgp_interface_address_delete(ZAPI_CALLBACK_ARGS
)
341 struct connected
*ifc
;
344 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
346 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
351 if (bgp_debug_zebra(ifc
->address
))
352 zlog_debug("Rx Intf address del VRF %u IF %s addr %pFX", vrf_id
,
353 ifc
->ifp
->name
, ifc
->address
);
355 if (bgp
&& if_is_operative(ifc
->ifp
)) {
356 bgp_connected_delete(bgp
, ifc
);
359 connected_free(&ifc
);
364 static int bgp_interface_nbr_address_add(ZAPI_CALLBACK_ARGS
)
366 struct nbr_connected
*ifc
= NULL
;
369 ifc
= zebra_interface_nbr_address_read(cmd
, zclient
->ibuf
, vrf_id
);
374 if (bgp_debug_zebra(ifc
->address
))
375 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %pFX",
376 vrf_id
, ifc
->ifp
->name
, ifc
->address
);
378 if (if_is_operative(ifc
->ifp
)) {
379 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
381 bgp_nbr_connected_add(bgp
, ifc
);
387 static int bgp_interface_nbr_address_delete(ZAPI_CALLBACK_ARGS
)
389 struct nbr_connected
*ifc
= NULL
;
392 ifc
= zebra_interface_nbr_address_read(cmd
, zclient
->ibuf
, vrf_id
);
397 if (bgp_debug_zebra(ifc
->address
))
398 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %pFX",
399 vrf_id
, ifc
->ifp
->name
, ifc
->address
);
401 if (if_is_operative(ifc
->ifp
)) {
402 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
404 bgp_nbr_connected_delete(bgp
, ifc
, 0);
407 nbr_connected_free(ifc
);
412 /* VRF update for an interface. */
413 static int bgp_interface_vrf_update(ZAPI_CALLBACK_ARGS
)
415 struct interface
*ifp
;
418 struct nbr_connected
*nc
;
419 struct listnode
*node
, *nnode
;
423 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
428 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
429 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id
,
430 ifp
->name
, new_vrf_id
);
432 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
435 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
436 bgp_connected_delete(bgp
, c
);
438 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
439 bgp_nbr_connected_delete(bgp
, nc
, 1);
441 /* Fast external-failover */
442 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
)) {
443 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
444 if ((peer
->ttl
!= BGP_DEFAULT_TTL
)
446 != BGP_GTSM_HOPS_CONNECTED
))
449 if (ifp
== peer
->nexthop
.ifp
)
450 BGP_EVENT_ADD(peer
, BGP_Stop
);
455 if_update_to_new_vrf(ifp
, new_vrf_id
);
457 bgp
= bgp_lookup_by_vrf_id(new_vrf_id
);
461 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
462 bgp_connected_add(bgp
, c
);
464 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
465 bgp_nbr_connected_add(bgp
, nc
);
467 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
471 /* Zebra route add and delete treatment. */
472 static int zebra_read_route(ZAPI_CALLBACK_ARGS
)
474 enum nexthop_types_t nhtype
;
475 struct zapi_route api
;
476 union g_addr nexthop
;
481 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
485 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
488 /* we completely ignore srcdest routes for now. */
489 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
492 /* ignore link-local address. */
493 if (api
.prefix
.family
== AF_INET6
494 && IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
497 nexthop
= api
.nexthops
[0].gate
;
498 ifindex
= api
.nexthops
[0].ifindex
;
499 nhtype
= api
.nexthops
[0].type
;
501 add
= (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
504 * The ADD message is actually an UPDATE and there is no
506 * for a prior redistributed route, if any. So, perform an
508 * DEL processing for the same redistributed route from any
512 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
514 bgp_redistribute_delete(bgp
, &api
.prefix
, i
,
518 /* Now perform the add/update. */
519 bgp_redistribute_add(bgp
, &api
.prefix
, &nexthop
, ifindex
,
520 nhtype
, api
.metric
, api
.type
, api
.instance
,
523 bgp_redistribute_delete(bgp
, &api
.prefix
, api
.type
,
527 if (bgp_debug_zebra(&api
.prefix
)) {
528 char buf
[PREFIX_STRLEN
];
531 inet_ntop(api
.prefix
.family
, &nexthop
, buf
,
534 "Rx route ADD VRF %u %s[%d] %pFX nexthop %s (type %d if %u) metric %u distance %u tag %" ROUTE_TAG_PRI
,
535 vrf_id
, zebra_route_string(api
.type
),
536 api
.instance
, &api
.prefix
, buf
, nhtype
, ifindex
,
537 api
.metric
, api
.distance
, api
.tag
);
539 zlog_debug("Rx route DEL VRF %u %s[%d] %pFX", vrf_id
,
540 zebra_route_string(api
.type
), api
.instance
,
548 struct interface
*if_lookup_by_ipv4(struct in_addr
*addr
, vrf_id_t vrf_id
)
551 struct listnode
*cnode
;
552 struct interface
*ifp
;
553 struct connected
*connected
;
554 struct prefix_ipv4 p
;
557 vrf
= vrf_lookup_by_id(vrf_id
);
563 p
.prefixlen
= IPV4_MAX_BITLEN
;
565 FOR_ALL_INTERFACES (vrf
, ifp
) {
566 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
567 cp
= connected
->address
;
569 if (cp
->family
== AF_INET
)
570 if (prefix_match(cp
, (struct prefix
*)&p
))
577 struct interface
*if_lookup_by_ipv4_exact(struct in_addr
*addr
, vrf_id_t vrf_id
)
580 struct listnode
*cnode
;
581 struct interface
*ifp
;
582 struct connected
*connected
;
585 vrf
= vrf_lookup_by_id(vrf_id
);
589 FOR_ALL_INTERFACES (vrf
, ifp
) {
590 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
591 cp
= connected
->address
;
593 if (cp
->family
== AF_INET
)
594 if (IPV4_ADDR_SAME(&cp
->u
.prefix4
, addr
))
601 struct interface
*if_lookup_by_ipv6(struct in6_addr
*addr
, ifindex_t ifindex
,
605 struct listnode
*cnode
;
606 struct interface
*ifp
;
607 struct connected
*connected
;
608 struct prefix_ipv6 p
;
611 vrf
= vrf_lookup_by_id(vrf_id
);
617 p
.prefixlen
= IPV6_MAX_BITLEN
;
619 FOR_ALL_INTERFACES (vrf
, ifp
) {
620 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
621 cp
= connected
->address
;
623 if (cp
->family
== AF_INET6
)
624 if (prefix_match(cp
, (struct prefix
*)&p
)) {
625 if (IN6_IS_ADDR_LINKLOCAL(
627 if (ifindex
== ifp
->ifindex
)
637 struct interface
*if_lookup_by_ipv6_exact(struct in6_addr
*addr
,
638 ifindex_t ifindex
, vrf_id_t vrf_id
)
641 struct listnode
*cnode
;
642 struct interface
*ifp
;
643 struct connected
*connected
;
646 vrf
= vrf_lookup_by_id(vrf_id
);
650 FOR_ALL_INTERFACES (vrf
, ifp
) {
651 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
652 cp
= connected
->address
;
654 if (cp
->family
== AF_INET6
)
655 if (IPV6_ADDR_SAME(&cp
->u
.prefix6
, addr
)) {
656 if (IN6_IS_ADDR_LINKLOCAL(
658 if (ifindex
== ifp
->ifindex
)
668 static int if_get_ipv6_global(struct interface
*ifp
, struct in6_addr
*addr
)
670 struct listnode
*cnode
;
671 struct connected
*connected
;
674 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
675 cp
= connected
->address
;
677 if (cp
->family
== AF_INET6
)
678 if (!IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
679 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
686 static int if_get_ipv6_local(struct interface
*ifp
, struct in6_addr
*addr
)
688 struct listnode
*cnode
;
689 struct connected
*connected
;
692 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
693 cp
= connected
->address
;
695 if (cp
->family
== AF_INET6
)
696 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
697 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
704 static int if_get_ipv4_address(struct interface
*ifp
, struct in_addr
*addr
)
706 struct listnode
*cnode
;
707 struct connected
*connected
;
710 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
711 cp
= connected
->address
;
712 if ((cp
->family
== AF_INET
)
713 && !ipv4_martian(&(cp
->u
.prefix4
))) {
714 *addr
= cp
->u
.prefix4
;
722 bool bgp_zebra_nexthop_set(union sockunion
*local
, union sockunion
*remote
,
723 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
726 struct interface
*ifp
= NULL
;
728 memset(nexthop
, 0, sizeof(struct bgp_nexthop
));
735 if (local
->sa
.sa_family
== AF_INET
) {
736 nexthop
->v4
= local
->sin
.sin_addr
;
738 ifp
= if_lookup_by_name(peer
->update_if
,
741 ifp
= if_lookup_by_ipv4_exact(&local
->sin
.sin_addr
,
744 if (local
->sa
.sa_family
== AF_INET6
) {
745 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
, IPV6_MAX_BYTELEN
);
746 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
747 if (peer
->conf_if
|| peer
->ifname
)
748 ifp
= if_lookup_by_name(peer
->conf_if
752 } else if (peer
->update_if
)
753 ifp
= if_lookup_by_name(peer
->update_if
,
756 ifp
= if_lookup_by_ipv6_exact(&local
->sin6
.sin6_addr
,
757 local
->sin6
.sin6_scope_id
,
763 * BGP views do not currently get proper data
764 * from zebra( when attached ) to be able to
765 * properly resolve nexthops, so give this
766 * instance type a pass.
768 if (peer
->bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
771 * If we have no interface data but we have established
772 * some connection w/ zebra than something has gone
773 * terribly terribly wrong here, so say this failed
774 * If we do not any zebra connection then not
775 * having a ifp pointer is ok.
777 return zclient_num_connects
? false : true;
782 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
783 if (local
->sa
.sa_family
== AF_INET
) {
785 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
788 /* There is no global nexthop. Use link-local address as
790 * global and link-local nexthop. In this scenario, the
792 * for interop is that the network admin would use a
794 * specify the global IPv6 nexthop.
796 if_get_ipv6_local(ifp
, &nexthop
->v6_global
);
797 memcpy(&nexthop
->v6_local
, &nexthop
->v6_global
,
800 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
802 if (if_lookup_by_ipv4(&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
803 peer
->shared_network
= 1;
805 peer
->shared_network
= 0;
808 /* IPv6 connection, fetch and store IPv4 local address if any. */
809 if (local
->sa
.sa_family
== AF_INET6
) {
810 struct interface
*direct
= NULL
;
813 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
814 if (!ret
&& peer
->local_id
.s_addr
!= INADDR_ANY
)
815 nexthop
->v4
= peer
->local_id
;
818 if (!IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
819 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
822 /* If directory connected set link-local address. */
823 direct
= if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
824 remote
->sin6
.sin6_scope_id
,
827 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
829 /* Link-local address. */
831 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
833 /* If there is no global address. Set link-local
835 global. I know this break RFC specification... */
836 /* In this scenario, the expectation for interop is that
838 * network admin would use a route-map to specify the
843 memcpy(&nexthop
->v6_global
,
844 &local
->sin6
.sin6_addr
,
846 /* Always set the link-local address */
847 memcpy(&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
851 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)
852 || if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
853 remote
->sin6
.sin6_scope_id
,
855 peer
->shared_network
= 1;
857 peer
->shared_network
= 0;
860 /* KAME stack specific treatment. */
862 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_global
)
863 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
)) {
864 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
, 0);
866 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_local
)
867 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
)) {
868 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
, 0);
872 /* If we have identified the local interface, there is no error for now.
877 static struct in6_addr
*
878 bgp_path_info_to_ipv6_nexthop(struct bgp_path_info
*path
, ifindex_t
*ifindex
)
880 struct in6_addr
*nexthop
= NULL
;
882 /* Only global address nexthop exists. */
883 if (path
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
884 || path
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV6_GLOBAL
) {
885 nexthop
= &path
->attr
->mp_nexthop_global
;
886 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
887 *ifindex
= path
->attr
->nh_ifindex
;
890 /* If both global and link-local address present. */
891 if (path
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
892 || path
->attr
->mp_nexthop_len
893 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
894 /* Check if route-map is set to prefer global over link-local */
895 if (path
->attr
->mp_nexthop_prefer_global
) {
896 nexthop
= &path
->attr
->mp_nexthop_global
;
897 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
898 *ifindex
= path
->attr
->nh_ifindex
;
900 /* Workaround for Cisco's nexthop bug. */
901 if (IN6_IS_ADDR_UNSPECIFIED(
902 &path
->attr
->mp_nexthop_global
)
903 && path
->peer
->su_remote
904 && path
->peer
->su_remote
->sa
.sa_family
907 &path
->peer
->su_remote
->sin6
.sin6_addr
;
908 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
909 *ifindex
= path
->peer
->nexthop
.ifp
912 nexthop
= &path
->attr
->mp_nexthop_local
;
913 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
914 *ifindex
= path
->attr
->nh_lla_ifindex
;
922 static bool bgp_table_map_apply(struct route_map
*map
, const struct prefix
*p
,
923 struct bgp_path_info
*path
)
925 route_map_result_t ret
;
927 ret
= route_map_apply(map
, p
, path
);
928 bgp_attr_flush(path
->attr
);
930 if (ret
!= RMAP_DENYMATCH
)
933 if (bgp_debug_zebra(p
)) {
934 if (p
->family
== AF_INET
) {
936 "Zebra rmap deny: IPv4 route %pFX nexthop %pI4",
937 p
, &path
->attr
->nexthop
);
939 if (p
->family
== AF_INET6
) {
940 char buf
[2][INET6_ADDRSTRLEN
];
942 struct in6_addr
*nexthop
;
944 nexthop
= bgp_path_info_to_ipv6_nexthop(path
, &ifindex
);
946 "Zebra rmap deny: IPv6 route %pFX nexthop %s",
948 nexthop
? inet_ntop(AF_INET6
, nexthop
, buf
[1],
951 &path
->attr
->nexthop
,
952 buf
[1], sizeof(buf
[1])));
958 static struct thread
*bgp_tm_thread_connect
;
959 static bool bgp_tm_status_connected
;
960 static bool bgp_tm_chunk_obtained
;
961 #define BGP_FLOWSPEC_TABLE_CHUNK 100000
962 static uint32_t bgp_tm_min
, bgp_tm_max
, bgp_tm_chunk_size
;
963 struct bgp
*bgp_tm_bgp
;
965 static int bgp_zebra_tm_connect(struct thread
*t
)
967 struct zclient
*zclient
;
968 int delay
= 10, ret
= 0;
970 zclient
= THREAD_ARG(t
);
971 if (bgp_tm_status_connected
&& zclient
->sock
> 0)
974 bgp_tm_status_connected
= false;
975 ret
= tm_table_manager_connect(zclient
);
978 zlog_info("Error connecting to table manager!");
979 bgp_tm_status_connected
= false;
981 if (!bgp_tm_status_connected
)
982 zlog_debug("Connecting to table manager. Success");
983 bgp_tm_status_connected
= true;
984 if (!bgp_tm_chunk_obtained
) {
985 if (bgp_zebra_get_table_range(bgp_tm_chunk_size
,
988 bgp_tm_chunk_obtained
= true;
989 /* parse non installed entries */
990 bgp_zebra_announce_table(bgp_tm_bgp
, AFI_IP
, SAFI_FLOWSPEC
);
994 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
995 &bgp_tm_thread_connect
);
999 bool bgp_zebra_tm_chunk_obtained(void)
1001 return bgp_tm_chunk_obtained
;
1004 uint32_t bgp_zebra_tm_get_id(void)
1006 static int table_id
;
1008 if (!bgp_tm_chunk_obtained
)
1010 return bgp_tm_min
++;
1013 void bgp_zebra_init_tm_connect(struct bgp
*bgp
)
1017 /* if already set, do nothing
1019 if (bgp_tm_thread_connect
!= NULL
)
1021 bgp_tm_status_connected
= false;
1022 bgp_tm_chunk_obtained
= false;
1023 bgp_tm_min
= bgp_tm_max
= 0;
1024 bgp_tm_chunk_size
= BGP_FLOWSPEC_TABLE_CHUNK
;
1026 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1027 &bgp_tm_thread_connect
);
1030 int bgp_zebra_get_table_range(uint32_t chunk_size
,
1031 uint32_t *start
, uint32_t *end
)
1035 if (!bgp_tm_status_connected
)
1037 ret
= tm_get_table_chunk(zclient
, chunk_size
, start
, end
);
1039 flog_err(EC_BGP_TABLE_CHUNK
,
1040 "BGP: Error getting table chunk %u", chunk_size
);
1043 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1044 chunk_size
, *start
, *end
);
1048 static bool update_ipv4nh_for_route_install(int nh_othervrf
, struct bgp
*nh_bgp
,
1049 struct in_addr
*nexthop
,
1050 struct attr
*attr
, bool is_evpn
,
1051 struct zapi_nexthop
*api_nh
)
1053 api_nh
->gate
.ipv4
= *nexthop
;
1054 api_nh
->vrf_id
= nh_bgp
->vrf_id
;
1056 /* Need to set fields appropriately for EVPN routes imported into
1057 * a VRF (which are programmed as onlink on l3-vni SVI) as well as
1058 * connected routes leaked into a VRF.
1061 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1062 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
);
1063 api_nh
->ifindex
= nh_bgp
->l3vni_svi_ifindex
;
1064 } else if (nh_othervrf
&&
1065 api_nh
->gate
.ipv4
.s_addr
== INADDR_ANY
) {
1066 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1067 api_nh
->ifindex
= attr
->nh_ifindex
;
1069 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1074 static bool update_ipv6nh_for_route_install(int nh_othervrf
, struct bgp
*nh_bgp
,
1075 struct in6_addr
*nexthop
,
1077 struct bgp_path_info
*pi
,
1078 struct bgp_path_info
*best_pi
,
1080 struct zapi_nexthop
*api_nh
)
1085 api_nh
->vrf_id
= nh_bgp
->vrf_id
;
1088 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1089 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
);
1090 api_nh
->ifindex
= nh_bgp
->l3vni_svi_ifindex
;
1091 } else if (nh_othervrf
) {
1092 if (IN6_IS_ADDR_UNSPECIFIED(nexthop
)) {
1093 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1094 api_nh
->ifindex
= attr
->nh_ifindex
;
1095 } else if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1098 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1099 api_nh
->ifindex
= ifindex
;
1101 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1102 api_nh
->ifindex
= 0;
1105 if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1107 && attr
->mp_nexthop_len
1108 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1109 if (pi
->peer
->nexthop
.ifp
)
1111 pi
->peer
->nexthop
.ifp
->ifindex
;
1113 if (pi
->peer
->conf_if
)
1114 ifindex
= pi
->peer
->ifp
->ifindex
;
1115 else if (pi
->peer
->ifname
)
1116 ifindex
= ifname2ifindex(
1118 pi
->peer
->bgp
->vrf_id
);
1119 else if (pi
->peer
->nexthop
.ifp
)
1121 pi
->peer
->nexthop
.ifp
->ifindex
;
1126 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1127 api_nh
->ifindex
= ifindex
;
1129 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1130 api_nh
->ifindex
= 0;
1134 api_nh
->gate
.ipv6
= *nexthop
;
1139 static bool bgp_zebra_use_nhop_weighted(struct bgp
*bgp
, struct attr
*attr
,
1140 uint64_t tot_bw
, uint32_t *nh_weight
)
1146 /* zero link-bandwidth and link-bandwidth not present are treated
1147 * as the same situation.
1150 /* the only situations should be if we're either told
1151 * to skip or use default weight.
1153 if (bgp
->lb_handling
== BGP_LINK_BW_SKIP_MISSING
)
1155 *nh_weight
= BGP_ZEBRA_DEFAULT_NHOP_WEIGHT
;
1157 tmp
= (uint64_t)bw
* 100;
1158 *nh_weight
= ((uint32_t)(tmp
/ tot_bw
));
1164 void bgp_zebra_announce(struct bgp_dest
*dest
, const struct prefix
*p
,
1165 struct bgp_path_info
*info
, struct bgp
*bgp
, afi_t afi
,
1168 struct zapi_route api
= { 0 };
1169 struct zapi_nexthop
*api_nh
;
1171 unsigned int valid_nh_count
= 0;
1172 int has_valid_label
= 0;
1173 bool allow_recursion
= false;
1176 struct bgp_path_info
*mpinfo
;
1178 struct attr local_attr
;
1179 struct bgp_path_info local_info
;
1180 struct bgp_path_info
*mpinfo_cp
= &local_info
;
1183 int nh_othervrf
= 0;
1185 bool nh_updated
= false;
1187 uint64_t cum_bw
= 0;
1188 uint32_t nhg_id
= 0;
1191 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1192 * know of this instance.
1194 if (!bgp_install_info_to_zebra(bgp
))
1197 if (bgp
->main_zebra_update_hold
)
1200 if (safi
== SAFI_FLOWSPEC
) {
1201 bgp_pbr_update_entry(bgp
, bgp_dest_get_prefix(dest
), info
, afi
,
1207 * vrf leaking support (will have only one nexthop)
1209 if (info
->extra
&& info
->extra
->bgp_orig
)
1212 /* Make Zebra API structure. */
1213 api
.vrf_id
= bgp
->vrf_id
;
1214 api
.type
= ZEBRA_ROUTE_BGP
;
1217 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1221 if (info
->type
== ZEBRA_ROUTE_BGP
1222 && info
->sub_type
== BGP_ROUTE_IMPORTED
) {
1224 /* Obtain peer from parent */
1225 if (info
->extra
&& info
->extra
->parent
)
1226 peer
= ((struct bgp_path_info
*)(info
->extra
->parent
))
1230 tag
= info
->attr
->tag
;
1232 /* If the route's source is EVPN, flag as such. */
1233 is_evpn
= is_route_parent_evpn(info
);
1235 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1237 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
1238 || info
->sub_type
== BGP_ROUTE_AGGREGATE
) {
1239 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1240 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1243 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= BGP_DEFAULT_TTL
)
1244 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1245 || CHECK_FLAG(bgp
->flags
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1247 allow_recursion
= true;
1249 if (info
->attr
->rmap_table_id
) {
1250 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
1251 api
.tableid
= info
->attr
->rmap_table_id
;
1254 if (CHECK_FLAG(info
->attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR
)))
1255 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRTE
);
1257 /* Metric is currently based on the best-path only */
1258 metric
= info
->attr
->med
;
1260 /* Determine if we're doing weighted ECMP or not */
1261 do_wt_ecmp
= bgp_path_info_mpath_chkwtd(bgp
, info
);
1263 cum_bw
= bgp_path_info_mpath_cumbw(info
);
1265 /* EVPN MAC-IP routes are installed with a L3 NHG id */
1266 if (bgp_evpn_path_es_use_nhg(bgp
, info
, &nhg_id
)) {
1270 SET_FLAG(api
.message
, ZAPI_MESSAGE_NHG
);
1275 for (; mpinfo
; mpinfo
= bgp_path_info_mpath_next(mpinfo
)) {
1278 if (valid_nh_count
>= multipath_num
)
1281 *mpinfo_cp
= *mpinfo
;
1284 /* Get nexthop address-family */
1285 if (p
->family
== AF_INET
1286 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
))
1287 nh_family
= AF_INET
;
1288 else if (p
->family
== AF_INET6
1289 || (p
->family
== AF_INET
1290 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
)))
1291 nh_family
= AF_INET6
;
1295 /* If processing for weighted ECMP, determine the next hop's
1296 * weight. Based on user setting, we may skip the next hop
1297 * in some situations.
1300 if (!bgp_zebra_use_nhop_weighted(bgp
, mpinfo
->attr
,
1301 cum_bw
, &nh_weight
))
1304 api_nh
= &api
.nexthops
[valid_nh_count
];
1306 if (CHECK_FLAG(info
->attr
->flag
,
1307 ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR
)))
1308 api_nh
->srte_color
= info
->attr
->srte_color
;
1310 if (bgp_debug_zebra(&api
.prefix
)) {
1311 if (mpinfo
->extra
) {
1312 zlog_debug("%s: p=%pFX, bgp_is_valid_label: %d",
1315 &mpinfo
->extra
->label
[0]));
1318 "%s: p=%pFX, extra is NULL, no label",
1323 if (bgp
->table_map
[afi
][safi
].name
) {
1324 /* Copy info and attributes, so the route-map
1325 apply doesn't modify the BGP route info. */
1326 local_attr
= *mpinfo
->attr
;
1327 mpinfo_cp
->attr
= &local_attr
;
1328 if (!bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
,
1332 /* metric/tag is only allowed to be
1333 * overridden on 1st nexthop */
1334 if (mpinfo
== info
) {
1335 metric
= mpinfo_cp
->attr
->med
;
1336 tag
= mpinfo_cp
->attr
->tag
;
1340 if (nh_family
== AF_INET
) {
1341 nh_updated
= update_ipv4nh_for_route_install(
1344 info
->extra
->bgp_orig
: bgp
,
1345 &mpinfo_cp
->attr
->nexthop
,
1346 mpinfo_cp
->attr
, is_evpn
, api_nh
);
1348 ifindex_t ifindex
= IFINDEX_INTERNAL
;
1349 struct in6_addr
*nexthop
;
1351 nexthop
= bgp_path_info_to_ipv6_nexthop(mpinfo_cp
,
1355 nh_updated
= update_ipv4nh_for_route_install(
1357 nh_othervrf
? info
->extra
->bgp_orig
1359 &mpinfo_cp
->attr
->nexthop
,
1360 mpinfo_cp
->attr
, is_evpn
, api_nh
);
1362 nh_updated
= update_ipv6nh_for_route_install(
1364 nh_othervrf
? info
->extra
->bgp_orig
1366 nexthop
, ifindex
, mpinfo
, info
, is_evpn
,
1370 /* Did we get proper nexthop info to update zebra? */
1374 /* Allow recursion if it is a multipath group with both
1375 * eBGP and iBGP paths.
1377 if (!allow_recursion
1378 && CHECK_FLAG(bgp
->flags
, BGP_FLAG_PEERTYPE_MULTIPATH_RELAX
)
1379 && (mpinfo
->peer
->sort
== BGP_PEER_IBGP
1380 || mpinfo
->peer
->sort
== BGP_PEER_CONFED
))
1381 allow_recursion
= true;
1384 && bgp_is_valid_label(&mpinfo
->extra
->label
[0])
1385 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1386 has_valid_label
= 1;
1387 label
= label_pton(&mpinfo
->extra
->label
[0]);
1389 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
);
1391 api_nh
->label_num
= 1;
1392 api_nh
->labels
[0] = label
;
1394 memcpy(&api_nh
->rmac
, &(mpinfo
->attr
->rmac
),
1395 sizeof(struct ethaddr
));
1396 api_nh
->weight
= nh_weight
;
1401 is_add
= (valid_nh_count
|| nhg_id
) ? true : false;
1403 if (is_add
&& CHECK_FLAG(bm
->flags
, BM_FLAG_SEND_EXTRA_DATA_TO_ZEBRA
)) {
1404 struct bgp_zebra_opaque bzo
= {};
1406 strlcpy(bzo
.aspath
, info
->attr
->aspath
->str
,
1407 sizeof(bzo
.aspath
));
1409 if (info
->attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
))
1410 strlcpy(bzo
.community
, info
->attr
->community
->str
,
1411 sizeof(bzo
.community
));
1413 if (info
->attr
->flag
1414 & ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
))
1415 strlcpy(bzo
.lcommunity
, info
->attr
->lcommunity
->str
,
1416 sizeof(bzo
.lcommunity
));
1418 SET_FLAG(api
.message
, ZAPI_MESSAGE_OPAQUE
);
1419 api
.opaque
.length
= MIN(sizeof(struct bgp_zebra_opaque
),
1420 ZAPI_MESSAGE_OPAQUE_LENGTH
);
1421 memcpy(api
.opaque
.data
, &bzo
, api
.opaque
.length
);
1424 if (allow_recursion
)
1425 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1428 * When we create an aggregate route we must also
1429 * install a Null0 route in the RIB, so overwrite
1430 * what was written into api with a blackhole route
1432 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1433 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
1435 api
.nexthop_num
= valid_nh_count
;
1437 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1438 api
.metric
= metric
;
1441 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1445 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1447 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1448 api
.distance
= distance
;
1451 if (bgp_debug_zebra(p
)) {
1452 char nh_buf
[INET6_ADDRSTRLEN
];
1453 char eth_buf
[ETHER_ADDR_STRLEN
+ 7] = {'\0'};
1454 char buf1
[ETHER_ADDR_STRLEN
];
1459 "Tx route %s VRF %u %pFX metric %u tag %" ROUTE_TAG_PRI
1461 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1462 &api
.prefix
, api
.metric
, api
.tag
, api
.nexthop_num
,
1464 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1465 api_nh
= &api
.nexthops
[i
];
1467 switch (api_nh
->type
) {
1468 case NEXTHOP_TYPE_IFINDEX
:
1471 case NEXTHOP_TYPE_IPV4
:
1472 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1473 nh_family
= AF_INET
;
1474 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1477 case NEXTHOP_TYPE_IPV6
:
1478 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1479 nh_family
= AF_INET6
;
1480 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1483 case NEXTHOP_TYPE_BLACKHOLE
:
1484 strlcpy(nh_buf
, "blackhole", sizeof(nh_buf
));
1487 /* Note: add new nexthop case */
1492 label_buf
[0] = '\0';
1495 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1496 snprintf(label_buf
, sizeof(label_buf
),
1497 "label %u", api_nh
->labels
[0]);
1498 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)
1499 && !is_zero_mac(&api_nh
->rmac
))
1500 snprintf(eth_buf
, sizeof(eth_buf
), " RMAC %s",
1501 prefix_mac2str(&api_nh
->rmac
,
1502 buf1
, sizeof(buf1
)));
1503 zlog_debug(" nhop [%d]: %s if %u VRF %u wt %u %s %s",
1504 i
+ 1, nh_buf
, api_nh
->ifindex
,
1505 api_nh
->vrf_id
, api_nh
->weight
,
1506 label_buf
, eth_buf
);
1509 int recursion_flag
= 0;
1511 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
))
1514 zlog_debug("%s: %pFX: announcing to zebra (recursion %sset)",
1515 __func__
, p
, (recursion_flag
? "" : "NOT "));
1517 zclient_route_send(is_add
? ZEBRA_ROUTE_ADD
: ZEBRA_ROUTE_DELETE
,
1521 /* Announce all routes of a table to zebra */
1522 void bgp_zebra_announce_table(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1524 struct bgp_dest
*dest
;
1525 struct bgp_table
*table
;
1526 struct bgp_path_info
*pi
;
1528 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1529 * know of this instance.
1531 if (!bgp_install_info_to_zebra(bgp
))
1534 table
= bgp
->rib
[afi
][safi
];
1538 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
))
1539 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
1540 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
) &&
1542 (pi
->type
== ZEBRA_ROUTE_BGP
1543 && (pi
->sub_type
== BGP_ROUTE_NORMAL
1544 || pi
->sub_type
== BGP_ROUTE_IMPORTED
)))
1546 bgp_zebra_announce(dest
,
1547 bgp_dest_get_prefix(dest
),
1548 pi
, bgp
, afi
, safi
);
1551 /* Announce routes of any bgp subtype of a table to zebra */
1552 void bgp_zebra_announce_table_all_subtypes(struct bgp
*bgp
, afi_t afi
,
1555 struct bgp_dest
*dest
;
1556 struct bgp_table
*table
;
1557 struct bgp_path_info
*pi
;
1559 if (!bgp_install_info_to_zebra(bgp
))
1562 table
= bgp
->rib
[afi
][safi
];
1566 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
))
1567 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
1568 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
) &&
1569 pi
->type
== ZEBRA_ROUTE_BGP
)
1570 bgp_zebra_announce(dest
,
1571 bgp_dest_get_prefix(dest
),
1572 pi
, bgp
, afi
, safi
);
1575 void bgp_zebra_withdraw(const struct prefix
*p
, struct bgp_path_info
*info
,
1576 struct bgp
*bgp
, safi_t safi
)
1578 struct zapi_route api
;
1581 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1582 * know of this instance.
1584 if (!bgp_install_info_to_zebra(bgp
))
1587 if (safi
== SAFI_FLOWSPEC
) {
1589 bgp_pbr_update_entry(peer
->bgp
, p
, info
, AFI_IP
, safi
, false);
1593 memset(&api
, 0, sizeof(api
));
1594 api
.vrf_id
= bgp
->vrf_id
;
1595 api
.type
= ZEBRA_ROUTE_BGP
;
1599 if (info
->attr
->rmap_table_id
) {
1600 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
1601 api
.tableid
= info
->attr
->rmap_table_id
;
1604 /* If the route's source is EVPN, flag as such. */
1605 if (is_route_parent_evpn(info
))
1606 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1608 if (bgp_debug_zebra(p
))
1609 zlog_debug("Tx route delete VRF %u %pFX", bgp
->vrf_id
,
1612 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
1615 /* Withdraw all entries in a BGP instances RIB table from Zebra */
1616 void bgp_zebra_withdraw_table_all_subtypes(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1618 struct bgp_dest
*dest
;
1619 struct bgp_table
*table
;
1620 struct bgp_path_info
*pi
;
1622 if (!bgp_install_info_to_zebra(bgp
))
1625 table
= bgp
->rib
[afi
][safi
];
1629 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
1630 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
1631 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
1632 && (pi
->type
== ZEBRA_ROUTE_BGP
))
1633 bgp_zebra_withdraw(bgp_dest_get_prefix(dest
),
1639 struct bgp_redist
*bgp_redist_lookup(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1640 unsigned short instance
)
1642 struct list
*red_list
;
1643 struct listnode
*node
;
1644 struct bgp_redist
*red
;
1646 red_list
= bgp
->redist
[afi
][type
];
1650 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1651 if (red
->instance
== instance
)
1657 struct bgp_redist
*bgp_redist_add(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1658 unsigned short instance
)
1660 struct list
*red_list
;
1661 struct bgp_redist
*red
;
1663 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1667 if (!bgp
->redist
[afi
][type
])
1668 bgp
->redist
[afi
][type
] = list_new();
1670 red_list
= bgp
->redist
[afi
][type
];
1671 red
= XCALLOC(MTYPE_BGP_REDIST
, sizeof(struct bgp_redist
));
1672 red
->instance
= instance
;
1674 listnode_add(red_list
, red
);
1679 static void bgp_redist_del(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1680 unsigned short instance
)
1682 struct bgp_redist
*red
;
1684 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1687 listnode_delete(bgp
->redist
[afi
][type
], red
);
1688 XFREE(MTYPE_BGP_REDIST
, red
);
1689 if (!bgp
->redist
[afi
][type
]->count
)
1690 list_delete(&bgp
->redist
[afi
][type
]);
1694 /* Other routes redistribution into BGP. */
1695 int bgp_redistribute_set(struct bgp
*bgp
, afi_t afi
, int type
,
1696 unsigned short instance
, bool changed
)
1698 /* If redistribute options are changed call
1699 * bgp_redistribute_unreg() to reset the option and withdraw
1703 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1705 /* Return if already redistribute flag is set. */
1707 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1711 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1713 if (vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1716 #ifdef ENABLE_BGP_VNC
1717 if (EVPN_ENABLED(bgp
) && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1718 vnc_export_bgp_enable(
1719 bgp
, afi
); /* only enables if mode bits cfg'd */
1723 vrf_bitmap_set(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1727 * Don't try to register if we're not connected to Zebra or Zebra
1728 * doesn't know of this instance.
1730 * When we come up later well resend if needed.
1732 if (!bgp_install_info_to_zebra(bgp
))
1735 if (BGP_DEBUG(zebra
, ZEBRA
))
1736 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1737 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1740 /* Send distribute add message to zebra. */
1741 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1742 instance
, bgp
->vrf_id
);
1747 int bgp_redistribute_resend(struct bgp
*bgp
, afi_t afi
, int type
,
1748 unsigned short instance
)
1750 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1751 * know of this instance.
1753 if (!bgp_install_info_to_zebra(bgp
))
1756 if (BGP_DEBUG(zebra
, ZEBRA
))
1757 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1758 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1761 /* Send distribute add message to zebra. */
1762 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1763 instance
, bgp
->vrf_id
);
1764 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1765 instance
, bgp
->vrf_id
);
1770 /* Redistribute with route-map specification. */
1771 bool bgp_redistribute_rmap_set(struct bgp_redist
*red
, const char *name
,
1772 struct route_map
*route_map
)
1774 if (red
->rmap
.name
&& (strcmp(red
->rmap
.name
, name
) == 0))
1777 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1778 /* Decrement the count for existing routemap and
1779 * increment the count for new route map.
1781 route_map_counter_decrement(red
->rmap
.map
);
1782 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1783 red
->rmap
.map
= route_map
;
1784 route_map_counter_increment(red
->rmap
.map
);
1789 /* Redistribute with metric specification. */
1790 bool bgp_redistribute_metric_set(struct bgp
*bgp
, struct bgp_redist
*red
,
1791 afi_t afi
, int type
, uint32_t metric
)
1793 struct bgp_dest
*dest
;
1794 struct bgp_path_info
*pi
;
1796 if (red
->redist_metric_flag
&& red
->redist_metric
== metric
)
1799 red
->redist_metric_flag
= 1;
1800 red
->redist_metric
= metric
;
1802 for (dest
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); dest
;
1803 dest
= bgp_route_next(dest
)) {
1804 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
1805 if (pi
->sub_type
== BGP_ROUTE_REDISTRIBUTE
1807 && pi
->instance
== red
->instance
) {
1808 struct attr
*old_attr
;
1809 struct attr new_attr
;
1811 new_attr
= *pi
->attr
;
1812 new_attr
.med
= red
->redist_metric
;
1813 old_attr
= pi
->attr
;
1814 pi
->attr
= bgp_attr_intern(&new_attr
);
1815 bgp_attr_unintern(&old_attr
);
1817 bgp_path_info_set_flag(dest
, pi
,
1818 BGP_PATH_ATTR_CHANGED
);
1819 bgp_process(bgp
, dest
, afi
, SAFI_UNICAST
);
1827 /* Unset redistribution. */
1828 int bgp_redistribute_unreg(struct bgp
*bgp
, afi_t afi
, int type
,
1829 unsigned short instance
)
1831 struct bgp_redist
*red
;
1833 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1837 /* Return if zebra connection is disabled. */
1839 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1842 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1844 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1846 vrf_bitmap_unset(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1849 if (bgp_install_info_to_zebra(bgp
)) {
1850 /* Send distribute delete message to zebra. */
1851 if (BGP_DEBUG(zebra
, ZEBRA
))
1852 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1853 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1855 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
1856 type
, instance
, bgp
->vrf_id
);
1859 /* Withdraw redistributed routes from current BGP's routing table. */
1860 bgp_redistribute_withdraw(bgp
, afi
, type
, instance
);
1865 /* Unset redistribution. */
1866 int bgp_redistribute_unset(struct bgp
*bgp
, afi_t afi
, int type
,
1867 unsigned short instance
)
1869 struct bgp_redist
*red
;
1872 * vnc and vpn->vrf checks must be before red check because
1873 * they operate within bgpd irrespective of zebra connection
1874 * status. red lookup fails if there is no zebra connection.
1876 #ifdef ENABLE_BGP_VNC
1877 if (EVPN_ENABLED(bgp
) && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1878 vnc_export_bgp_disable(bgp
, afi
);
1882 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1886 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1888 /* Unset route-map. */
1889 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1890 route_map_counter_decrement(red
->rmap
.map
);
1891 red
->rmap
.map
= NULL
;
1894 red
->redist_metric_flag
= 0;
1895 red
->redist_metric
= 0;
1897 bgp_redist_del(bgp
, afi
, type
, instance
);
1902 void bgp_redistribute_redo(struct bgp
*bgp
)
1906 struct list
*red_list
;
1907 struct listnode
*node
;
1908 struct bgp_redist
*red
;
1910 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
1911 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
1913 red_list
= bgp
->redist
[afi
][i
];
1917 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
1918 bgp_redistribute_resend(bgp
, afi
, i
,
1925 void bgp_zclient_reset(void)
1927 zclient_reset(zclient
);
1930 /* Register this instance with Zebra. Invoked upon connect (for
1931 * default instance) and when other VRFs are learnt (or created and
1934 void bgp_zebra_instance_register(struct bgp
*bgp
)
1936 /* Don't try to register if we're not connected to Zebra */
1937 if (!zclient
|| zclient
->sock
< 0)
1940 if (BGP_DEBUG(zebra
, ZEBRA
))
1941 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
1943 /* Register for router-id, interfaces, redistributed routes. */
1944 zclient_send_reg_requests(zclient
, bgp
->vrf_id
);
1946 /* For EVPN instance, register to learn about VNIs, if appropriate. */
1947 if (bgp
->advertise_all_vni
)
1948 bgp_zebra_advertise_all_vni(bgp
, 1);
1950 bgp_nht_register_nexthops(bgp
);
1953 /* Deregister this instance with Zebra. Invoked upon the instance
1954 * being deleted (default or VRF) and it is already registered.
1956 void bgp_zebra_instance_deregister(struct bgp
*bgp
)
1958 /* Don't try to deregister if we're not connected to Zebra */
1959 if (zclient
->sock
< 0)
1962 if (BGP_DEBUG(zebra
, ZEBRA
))
1963 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
1965 /* For EVPN instance, unregister learning about VNIs, if appropriate. */
1966 if (bgp
->advertise_all_vni
)
1967 bgp_zebra_advertise_all_vni(bgp
, 0);
1969 /* Deregister for router-id, interfaces, redistributed routes. */
1970 zclient_send_dereg_requests(zclient
, bgp
->vrf_id
);
1973 void bgp_zebra_initiate_radv(struct bgp
*bgp
, struct peer
*peer
)
1975 uint32_t ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
1977 /* Don't try to initiate if we're not connected to Zebra */
1978 if (zclient
->sock
< 0)
1981 if (BGP_DEBUG(zebra
, ZEBRA
))
1982 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
,
1986 * If unnumbered peer (peer->ifp) call thru zapi to start RAs.
1987 * If we don't have an ifp pointer, call function to find the
1988 * ifps for a numbered enhe peer to turn RAs on.
1990 peer
->ifp
? zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
,
1991 peer
->ifp
, 1, ra_interval
)
1992 : bgp_nht_reg_enhe_cap_intfs(peer
);
1995 void bgp_zebra_terminate_radv(struct bgp
*bgp
, struct peer
*peer
)
1997 /* Don't try to terminate if we're not connected to Zebra */
1998 if (zclient
->sock
< 0)
2001 if (BGP_DEBUG(zebra
, ZEBRA
))
2002 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
,
2006 * If unnumbered peer (peer->ifp) call thru zapi to stop RAs.
2007 * If we don't have an ifp pointer, call function to find the
2008 * ifps for a numbered enhe peer to turn RAs off.
2010 peer
->ifp
? zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
,
2012 : bgp_nht_dereg_enhe_cap_intfs(peer
);
2015 int bgp_zebra_advertise_subnet(struct bgp
*bgp
, int advertise
, vni_t vni
)
2017 struct stream
*s
= NULL
;
2020 if (!zclient
|| zclient
->sock
< 0)
2023 /* Don't try to register if Zebra doesn't know of this instance. */
2024 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
2025 if (BGP_DEBUG(zebra
, ZEBRA
))
2027 "%s: No zebra instance to talk to, cannot advertise subnet",
2035 zclient_create_header(s
, ZEBRA_ADVERTISE_SUBNET
, bgp
->vrf_id
);
2036 stream_putc(s
, advertise
);
2037 stream_put3(s
, vni
);
2038 stream_putw_at(s
, 0, stream_get_endp(s
));
2040 return zclient_send_message(zclient
);
2043 int bgp_zebra_advertise_svi_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
2045 struct stream
*s
= NULL
;
2048 if (!zclient
|| zclient
->sock
< 0)
2051 /* Don't try to register if Zebra doesn't know of this instance. */
2052 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
2058 zclient_create_header(s
, ZEBRA_ADVERTISE_SVI_MACIP
, bgp
->vrf_id
);
2059 stream_putc(s
, advertise
);
2060 stream_putl(s
, vni
);
2061 stream_putw_at(s
, 0, stream_get_endp(s
));
2063 return zclient_send_message(zclient
);
2066 int bgp_zebra_advertise_gw_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
2068 struct stream
*s
= NULL
;
2071 if (!zclient
|| zclient
->sock
< 0)
2074 /* Don't try to register if Zebra doesn't know of this instance. */
2075 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
2076 if (BGP_DEBUG(zebra
, ZEBRA
))
2078 "%s: No zebra instance to talk to, not installing gw_macip",
2086 zclient_create_header(s
, ZEBRA_ADVERTISE_DEFAULT_GW
, bgp
->vrf_id
);
2087 stream_putc(s
, advertise
);
2088 stream_putl(s
, vni
);
2089 stream_putw_at(s
, 0, stream_get_endp(s
));
2091 return zclient_send_message(zclient
);
2094 int bgp_zebra_vxlan_flood_control(struct bgp
*bgp
,
2095 enum vxlan_flood_control flood_ctrl
)
2100 if (!zclient
|| zclient
->sock
< 0)
2103 /* Don't try to register if Zebra doesn't know of this instance. */
2104 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
2105 if (BGP_DEBUG(zebra
, ZEBRA
))
2107 "%s: No zebra instance to talk to, not installing all vni",
2115 zclient_create_header(s
, ZEBRA_VXLAN_FLOOD_CONTROL
, bgp
->vrf_id
);
2116 stream_putc(s
, flood_ctrl
);
2117 stream_putw_at(s
, 0, stream_get_endp(s
));
2119 return zclient_send_message(zclient
);
2122 int bgp_zebra_advertise_all_vni(struct bgp
*bgp
, int advertise
)
2127 if (!zclient
|| zclient
->sock
< 0)
2130 /* Don't try to register if Zebra doesn't know of this instance. */
2131 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
2137 zclient_create_header(s
, ZEBRA_ADVERTISE_ALL_VNI
, bgp
->vrf_id
);
2138 stream_putc(s
, advertise
);
2139 /* Also inform current BUM handling setting. This is really
2140 * relevant only when 'advertise' is set.
2142 stream_putc(s
, bgp
->vxlan_flood_ctrl
);
2143 stream_putw_at(s
, 0, stream_get_endp(s
));
2145 return zclient_send_message(zclient
);
2148 int bgp_zebra_dup_addr_detection(struct bgp
*bgp
)
2153 if (!zclient
|| zclient
->sock
< 0)
2156 /* Don't try to register if Zebra doesn't know of this instance. */
2157 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
2160 if (BGP_DEBUG(zebra
, ZEBRA
))
2161 zlog_debug("dup addr detect %s max_moves %u time %u freeze %s freeze_time %u",
2162 bgp
->evpn_info
->dup_addr_detect
?
2163 "enable" : "disable",
2164 bgp
->evpn_info
->dad_max_moves
,
2165 bgp
->evpn_info
->dad_time
,
2166 bgp
->evpn_info
->dad_freeze
?
2167 "enable" : "disable",
2168 bgp
->evpn_info
->dad_freeze_time
);
2172 zclient_create_header(s
, ZEBRA_DUPLICATE_ADDR_DETECTION
,
2174 stream_putl(s
, bgp
->evpn_info
->dup_addr_detect
);
2175 stream_putl(s
, bgp
->evpn_info
->dad_time
);
2176 stream_putl(s
, bgp
->evpn_info
->dad_max_moves
);
2177 stream_putl(s
, bgp
->evpn_info
->dad_freeze
);
2178 stream_putl(s
, bgp
->evpn_info
->dad_freeze_time
);
2179 stream_putw_at(s
, 0, stream_get_endp(s
));
2181 return zclient_send_message(zclient
);
2184 static int rule_notify_owner(ZAPI_CALLBACK_ARGS
)
2186 uint32_t seqno
, priority
, unique
;
2187 enum zapi_rule_notify_owner note
;
2188 struct bgp_pbr_action
*bgp_pbra
;
2189 struct bgp_pbr_rule
*bgp_pbr
= NULL
;
2190 char ifname
[INTERFACE_NAMSIZ
+ 1];
2192 if (!zapi_rule_notify_decode(zclient
->ibuf
, &seqno
, &priority
, &unique
,
2196 bgp_pbra
= bgp_pbr_action_rule_lookup(vrf_id
, unique
);
2198 /* look in bgp pbr rule */
2199 bgp_pbr
= bgp_pbr_rule_lookup(vrf_id
, unique
);
2200 if (!bgp_pbr
&& note
!= ZAPI_RULE_REMOVED
) {
2201 if (BGP_DEBUG(zebra
, ZEBRA
))
2202 zlog_debug("%s: Fail to look BGP rule (%u)",
2209 case ZAPI_RULE_FAIL_INSTALL
:
2210 if (BGP_DEBUG(zebra
, ZEBRA
))
2211 zlog_debug("%s: Received RULE_FAIL_INSTALL", __func__
);
2213 bgp_pbra
->installed
= false;
2214 bgp_pbra
->install_in_progress
= false;
2216 bgp_pbr
->installed
= false;
2217 bgp_pbr
->install_in_progress
= false;
2220 case ZAPI_RULE_INSTALLED
:
2222 bgp_pbra
->installed
= true;
2223 bgp_pbra
->install_in_progress
= false;
2225 struct bgp_path_info
*path
;
2226 struct bgp_path_info_extra
*extra
;
2228 bgp_pbr
->installed
= true;
2229 bgp_pbr
->install_in_progress
= false;
2230 bgp_pbr
->action
->refcnt
++;
2231 /* link bgp_info to bgp_pbr */
2232 path
= (struct bgp_path_info
*)bgp_pbr
->path
;
2233 extra
= bgp_path_info_extra_get(path
);
2234 listnode_add_force(&extra
->bgp_fs_iprule
,
2237 if (BGP_DEBUG(zebra
, ZEBRA
))
2238 zlog_debug("%s: Received RULE_INSTALLED", __func__
);
2240 case ZAPI_RULE_FAIL_REMOVE
:
2241 case ZAPI_RULE_REMOVED
:
2242 if (BGP_DEBUG(zebra
, ZEBRA
))
2243 zlog_debug("%s: Received RULE REMOVED", __func__
);
2250 static int ipset_notify_owner(ZAPI_CALLBACK_ARGS
)
2253 enum zapi_ipset_notify_owner note
;
2254 struct bgp_pbr_match
*bgp_pbim
;
2256 if (!zapi_ipset_notify_decode(zclient
->ibuf
,
2261 bgp_pbim
= bgp_pbr_match_ipset_lookup(vrf_id
, unique
);
2263 if (BGP_DEBUG(zebra
, ZEBRA
))
2264 zlog_debug("%s: Fail to look BGP match ( %u, ID %u)",
2265 __func__
, note
, unique
);
2270 case ZAPI_IPSET_FAIL_INSTALL
:
2271 if (BGP_DEBUG(zebra
, ZEBRA
))
2272 zlog_debug("%s: Received IPSET_FAIL_INSTALL", __func__
);
2273 bgp_pbim
->installed
= false;
2274 bgp_pbim
->install_in_progress
= false;
2276 case ZAPI_IPSET_INSTALLED
:
2277 bgp_pbim
->installed
= true;
2278 bgp_pbim
->install_in_progress
= false;
2279 if (BGP_DEBUG(zebra
, ZEBRA
))
2280 zlog_debug("%s: Received IPSET_INSTALLED", __func__
);
2282 case ZAPI_IPSET_FAIL_REMOVE
:
2283 case ZAPI_IPSET_REMOVED
:
2284 if (BGP_DEBUG(zebra
, ZEBRA
))
2285 zlog_debug("%s: Received IPSET REMOVED", __func__
);
2292 static int ipset_entry_notify_owner(ZAPI_CALLBACK_ARGS
)
2295 char ipset_name
[ZEBRA_IPSET_NAME_SIZE
];
2296 enum zapi_ipset_entry_notify_owner note
;
2297 struct bgp_pbr_match_entry
*bgp_pbime
;
2299 if (!zapi_ipset_entry_notify_decode(
2305 bgp_pbime
= bgp_pbr_match_ipset_entry_lookup(vrf_id
,
2309 if (BGP_DEBUG(zebra
, ZEBRA
))
2311 "%s: Fail to look BGP match entry (%u, ID %u)",
2312 __func__
, note
, unique
);
2317 case ZAPI_IPSET_ENTRY_FAIL_INSTALL
:
2318 if (BGP_DEBUG(zebra
, ZEBRA
))
2319 zlog_debug("%s: Received IPSET_ENTRY_FAIL_INSTALL",
2321 bgp_pbime
->installed
= false;
2322 bgp_pbime
->install_in_progress
= false;
2324 case ZAPI_IPSET_ENTRY_INSTALLED
:
2326 struct bgp_path_info
*path
;
2327 struct bgp_path_info_extra
*extra
;
2329 bgp_pbime
->installed
= true;
2330 bgp_pbime
->install_in_progress
= false;
2331 if (BGP_DEBUG(zebra
, ZEBRA
))
2332 zlog_debug("%s: Received IPSET_ENTRY_INSTALLED",
2334 /* link bgp_path_info to bpme */
2335 path
= (struct bgp_path_info
*)bgp_pbime
->path
;
2336 extra
= bgp_path_info_extra_get(path
);
2337 listnode_add_force(&extra
->bgp_fs_pbr
, bgp_pbime
);
2340 case ZAPI_IPSET_ENTRY_FAIL_REMOVE
:
2341 case ZAPI_IPSET_ENTRY_REMOVED
:
2342 if (BGP_DEBUG(zebra
, ZEBRA
))
2343 zlog_debug("%s: Received IPSET_ENTRY_REMOVED",
2350 static int iptable_notify_owner(ZAPI_CALLBACK_ARGS
)
2353 enum zapi_iptable_notify_owner note
;
2354 struct bgp_pbr_match
*bgpm
;
2356 if (!zapi_iptable_notify_decode(
2361 bgpm
= bgp_pbr_match_iptable_lookup(vrf_id
, unique
);
2363 if (BGP_DEBUG(zebra
, ZEBRA
))
2364 zlog_debug("%s: Fail to look BGP iptable (%u %u)",
2365 __func__
, note
, unique
);
2369 case ZAPI_IPTABLE_FAIL_INSTALL
:
2370 if (BGP_DEBUG(zebra
, ZEBRA
))
2371 zlog_debug("%s: Received IPTABLE_FAIL_INSTALL",
2373 bgpm
->installed_in_iptable
= false;
2374 bgpm
->install_iptable_in_progress
= false;
2376 case ZAPI_IPTABLE_INSTALLED
:
2377 bgpm
->installed_in_iptable
= true;
2378 bgpm
->install_iptable_in_progress
= false;
2379 if (BGP_DEBUG(zebra
, ZEBRA
))
2380 zlog_debug("%s: Received IPTABLE_INSTALLED", __func__
);
2381 bgpm
->action
->refcnt
++;
2383 case ZAPI_IPTABLE_FAIL_REMOVE
:
2384 case ZAPI_IPTABLE_REMOVED
:
2385 if (BGP_DEBUG(zebra
, ZEBRA
))
2386 zlog_debug("%s: Received IPTABLE REMOVED", __func__
);
2392 /* Process route notification messages from RIB */
2393 static int bgp_zebra_route_notify_owner(int command
, struct zclient
*zclient
,
2394 zebra_size_t length
, vrf_id_t vrf_id
)
2397 enum zapi_route_notify_owner note
;
2401 struct bgp_dest
*dest
;
2403 struct bgp_path_info
*pi
, *new_select
;
2405 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table_id
, ¬e
,
2407 zlog_err("%s : error in msg decode", __func__
);
2411 /* Get the bgp instance */
2412 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2414 flog_err(EC_BGP_INVALID_BGP_INSTANCE
,
2415 "%s : bgp instance not found vrf %d", __func__
,
2420 /* Find the bgp route node */
2421 dest
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
, &p
,
2426 bgp_dest_unlock_node(dest
);
2429 case ZAPI_ROUTE_INSTALLED
:
2431 /* Clear the flags so that route can be processed */
2432 if (CHECK_FLAG(dest
->flags
,
2433 BGP_NODE_FIB_INSTALL_PENDING
)) {
2434 UNSET_FLAG(dest
->flags
,
2435 BGP_NODE_FIB_INSTALL_PENDING
);
2436 SET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALLED
);
2437 if (BGP_DEBUG(zebra
, ZEBRA
))
2438 zlog_debug("route %pRN : INSTALLED", dest
);
2439 /* Find the best route */
2440 for (pi
= dest
->info
; pi
; pi
= pi
->next
) {
2441 /* Process aggregate route */
2442 bgp_aggregate_increment(bgp
, &p
, pi
,
2444 if (CHECK_FLAG(pi
->flags
,
2448 /* Advertise the route */
2450 group_announce_route(bgp
, afi
, safi
,
2453 flog_err(EC_BGP_INVALID_ROUTE
,
2454 "selected route %pRN not found",
2460 case ZAPI_ROUTE_REMOVED
:
2461 /* Route deleted from dataplane, reset the installed flag
2462 * so that route can be reinstalled when client sends
2465 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALLED
);
2466 if (BGP_DEBUG(zebra
, ZEBRA
))
2467 zlog_debug("route %pRN: Removed from Fib", dest
);
2469 case ZAPI_ROUTE_FAIL_INSTALL
:
2470 if (BGP_DEBUG(zebra
, ZEBRA
))
2471 zlog_debug("route: %pRN Failed to Install into Fib",
2473 /* Error will be logged by zebra module */
2475 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
2476 if (BGP_DEBUG(zebra
, ZEBRA
))
2477 zlog_debug("route: %pRN removed due to better admin won",
2479 /* No action required */
2481 case ZAPI_ROUTE_REMOVE_FAIL
:
2482 zlog_warn("%s: Route %pRN failure to remove",
2489 /* this function is used to forge ip rule,
2490 * - either for iptable/ipset using fwmark id
2491 * - or for sample ip rule cmd
2493 static void bgp_encode_pbr_rule_action(struct stream
*s
,
2494 struct bgp_pbr_action
*pbra
,
2495 struct bgp_pbr_rule
*pbr
)
2498 uint8_t fam
= AF_INET
;
2499 char ifname
[INTERFACE_NAMSIZ
];
2501 if (pbra
->nh
.type
== NEXTHOP_TYPE_IPV6
)
2503 stream_putl(s
, 0); /* seqno unused */
2505 stream_putl(s
, pbr
->priority
);
2508 /* ruleno unused - priority change
2509 * ruleno permits distinguishing various FS PBR entries
2510 * - FS PBR entries based on ipset/iptables
2511 * - FS PBR entries based on iprule
2512 * the latter may contain default routing information injected by FS
2515 stream_putl(s
, pbr
->unique
);
2517 stream_putl(s
, pbra
->unique
);
2518 if (pbr
&& pbr
->flags
& MATCH_IP_SRC_SET
)
2519 memcpy(&pfx
, &(pbr
->src
), sizeof(struct prefix
));
2521 memset(&pfx
, 0, sizeof(pfx
));
2524 stream_putc(s
, pfx
.family
);
2525 stream_putc(s
, pfx
.prefixlen
);
2526 stream_put(s
, &pfx
.u
.prefix
, prefix_blen(&pfx
));
2528 stream_putw(s
, 0); /* src port */
2530 if (pbr
&& pbr
->flags
& MATCH_IP_DST_SET
)
2531 memcpy(&pfx
, &(pbr
->dst
), sizeof(struct prefix
));
2533 memset(&pfx
, 0, sizeof(pfx
));
2536 stream_putc(s
, pfx
.family
);
2537 stream_putc(s
, pfx
.prefixlen
);
2538 stream_put(s
, &pfx
.u
.prefix
, prefix_blen(&pfx
));
2540 stream_putw(s
, 0); /* dst port */
2541 stream_putc(s
, 0); /* dsfield */
2542 /* if pbr present, fwmark is not used */
2546 stream_putl(s
, pbra
->fwmark
); /* fwmark */
2548 stream_putl(s
, pbra
->table_id
);
2550 memset(ifname
, 0, sizeof(ifname
));
2551 stream_put(s
, ifname
, INTERFACE_NAMSIZ
); /* ifname unused */
2554 static void bgp_encode_pbr_ipset_match(struct stream
*s
,
2555 struct bgp_pbr_match
*pbim
)
2557 stream_putl(s
, pbim
->unique
);
2558 stream_putl(s
, pbim
->type
);
2559 stream_putc(s
, pbim
->family
);
2560 stream_put(s
, pbim
->ipset_name
,
2561 ZEBRA_IPSET_NAME_SIZE
);
2564 static void bgp_encode_pbr_ipset_entry_match(struct stream
*s
,
2565 struct bgp_pbr_match_entry
*pbime
)
2567 stream_putl(s
, pbime
->unique
);
2568 /* check that back pointer is not null */
2569 stream_put(s
, pbime
->backpointer
->ipset_name
,
2570 ZEBRA_IPSET_NAME_SIZE
);
2572 stream_putc(s
, pbime
->src
.family
);
2573 stream_putc(s
, pbime
->src
.prefixlen
);
2574 stream_put(s
, &pbime
->src
.u
.prefix
, prefix_blen(&pbime
->src
));
2576 stream_putc(s
, pbime
->dst
.family
);
2577 stream_putc(s
, pbime
->dst
.prefixlen
);
2578 stream_put(s
, &pbime
->dst
.u
.prefix
, prefix_blen(&pbime
->dst
));
2580 stream_putw(s
, pbime
->src_port_min
);
2581 stream_putw(s
, pbime
->src_port_max
);
2582 stream_putw(s
, pbime
->dst_port_min
);
2583 stream_putw(s
, pbime
->dst_port_max
);
2584 stream_putc(s
, pbime
->proto
);
2587 static void bgp_encode_pbr_iptable_match(struct stream
*s
,
2588 struct bgp_pbr_action
*bpa
,
2589 struct bgp_pbr_match
*pbm
)
2591 stream_putl(s
, pbm
->unique2
);
2593 stream_putl(s
, pbm
->type
);
2595 stream_putl(s
, pbm
->flags
);
2597 /* TODO: correlate with what is contained
2598 * into bgp_pbr_action.
2599 * currently only forward supported
2601 if (bpa
->nh
.type
== NEXTHOP_TYPE_BLACKHOLE
)
2602 stream_putl(s
, ZEBRA_IPTABLES_DROP
);
2604 stream_putl(s
, ZEBRA_IPTABLES_FORWARD
);
2605 stream_putl(s
, bpa
->fwmark
);
2606 stream_put(s
, pbm
->ipset_name
,
2607 ZEBRA_IPSET_NAME_SIZE
);
2608 stream_putc(s
, pbm
->family
);
2609 stream_putw(s
, pbm
->pkt_len_min
);
2610 stream_putw(s
, pbm
->pkt_len_max
);
2611 stream_putw(s
, pbm
->tcp_flags
);
2612 stream_putw(s
, pbm
->tcp_mask_flags
);
2613 stream_putc(s
, pbm
->dscp_value
);
2614 stream_putc(s
, pbm
->fragment
);
2615 stream_putc(s
, pbm
->protocol
);
2616 stream_putw(s
, pbm
->flow_label
);
2619 /* BGP has established connection with Zebra. */
2620 static void bgp_zebra_connected(struct zclient
*zclient
)
2624 zclient_num_connects
++; /* increment even if not responding */
2626 /* Send the client registration */
2627 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
2629 /* At this point, we may or may not have BGP instances configured, but
2630 * we're only interested in the default VRF (others wouldn't have learnt
2631 * the VRF from Zebra yet.)
2633 bgp
= bgp_get_default();
2637 bgp_zebra_instance_register(bgp
);
2639 /* tell label pool that zebra is connected */
2640 bgp_lp_event_zebra_up();
2642 /* TODO - What if we have peers and networks configured, do we have to
2645 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp
, bgp
->peer
);
2648 static int bgp_zebra_process_local_es_add(ZAPI_CALLBACK_ARGS
)
2651 struct bgp
*bgp
= NULL
;
2652 struct stream
*s
= NULL
;
2653 char buf
[ESI_STR_LEN
];
2654 struct in_addr originator_ip
;
2659 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2664 stream_get(&esi
, s
, sizeof(esi_t
));
2665 originator_ip
.s_addr
= stream_get_ipv4(s
);
2666 active
= stream_getc(s
);
2667 df_pref
= stream_getw(s
);
2668 bypass
= stream_getc(s
);
2670 if (BGP_DEBUG(zebra
, ZEBRA
))
2672 "Rx add ESI %s originator-ip %pI4 active %u df_pref %u %s",
2673 esi_to_str(&esi
, buf
, sizeof(buf
)), &originator_ip
,
2674 active
, df_pref
, bypass
? "bypass" : "");
2676 bgp_evpn_local_es_add(bgp
, &esi
, originator_ip
, active
, df_pref
,
2682 static int bgp_zebra_process_local_es_del(ZAPI_CALLBACK_ARGS
)
2685 struct bgp
*bgp
= NULL
;
2686 struct stream
*s
= NULL
;
2687 char buf
[ESI_STR_LEN
];
2689 memset(&esi
, 0, sizeof(esi_t
));
2690 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2695 stream_get(&esi
, s
, sizeof(esi_t
));
2697 if (BGP_DEBUG(zebra
, ZEBRA
))
2698 zlog_debug("Rx del ESI %s",
2699 esi_to_str(&esi
, buf
, sizeof(buf
)));
2701 bgp_evpn_local_es_del(bgp
, &esi
);
2706 static int bgp_zebra_process_local_es_evi(ZAPI_CALLBACK_ARGS
)
2712 char buf
[ESI_STR_LEN
];
2714 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2719 stream_get(&esi
, s
, sizeof(esi_t
));
2720 vni
= stream_getl(s
);
2722 if (BGP_DEBUG(zebra
, ZEBRA
))
2723 zlog_debug("Rx %s ESI %s VNI %u",
2724 ZEBRA_VNI_ADD
? "add" : "del",
2725 esi_to_str(&esi
, buf
, sizeof(buf
)), vni
);
2727 if (cmd
== ZEBRA_LOCAL_ES_EVI_ADD
)
2728 bgp_evpn_local_es_evi_add(bgp
, &esi
, vni
);
2730 bgp_evpn_local_es_evi_del(bgp
, &esi
, vni
);
2735 static int bgp_zebra_process_local_l3vni(ZAPI_CALLBACK_ARGS
)
2739 struct ethaddr svi_rmac
, vrr_rmac
= {.octet
= {0} };
2740 struct in_addr originator_ip
;
2742 ifindex_t svi_ifindex
;
2743 bool is_anycast_mac
= false;
2745 memset(&svi_rmac
, 0, sizeof(struct ethaddr
));
2746 memset(&originator_ip
, 0, sizeof(struct in_addr
));
2748 l3vni
= stream_getl(s
);
2749 if (cmd
== ZEBRA_L3VNI_ADD
) {
2750 stream_get(&svi_rmac
, s
, sizeof(struct ethaddr
));
2751 originator_ip
.s_addr
= stream_get_ipv4(s
);
2752 stream_get(&filter
, s
, sizeof(int));
2753 svi_ifindex
= stream_getl(s
);
2754 stream_get(&vrr_rmac
, s
, sizeof(struct ethaddr
));
2755 is_anycast_mac
= stream_getl(s
);
2757 if (BGP_DEBUG(zebra
, ZEBRA
))
2759 "Rx L3-VNI ADD VRF %s VNI %u RMAC svi-mac %pEA vrr-mac %pEA filter %s svi-if %u",
2760 vrf_id_to_name(vrf_id
), l3vni
, &svi_rmac
,
2762 filter
? "prefix-routes-only" : "none",
2765 bgp_evpn_local_l3vni_add(l3vni
, vrf_id
, &svi_rmac
, &vrr_rmac
,
2766 originator_ip
, filter
, svi_ifindex
,
2769 if (BGP_DEBUG(zebra
, ZEBRA
))
2770 zlog_debug("Rx L3-VNI DEL VRF %s VNI %u",
2771 vrf_id_to_name(vrf_id
), l3vni
);
2773 bgp_evpn_local_l3vni_del(l3vni
, vrf_id
);
2779 static int bgp_zebra_process_local_vni(ZAPI_CALLBACK_ARGS
)
2784 struct in_addr vtep_ip
= {INADDR_ANY
};
2785 vrf_id_t tenant_vrf_id
= VRF_DEFAULT
;
2786 struct in_addr mcast_grp
= {INADDR_ANY
};
2789 vni
= stream_getl(s
);
2790 if (cmd
== ZEBRA_VNI_ADD
) {
2791 vtep_ip
.s_addr
= stream_get_ipv4(s
);
2792 stream_get(&tenant_vrf_id
, s
, sizeof(vrf_id_t
));
2793 mcast_grp
.s_addr
= stream_get_ipv4(s
);
2796 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2800 if (BGP_DEBUG(zebra
, ZEBRA
))
2801 zlog_debug("Rx VNI %s VRF %s VNI %u tenant-vrf %s",
2802 (cmd
== ZEBRA_VNI_ADD
) ? "add" : "del",
2803 vrf_id_to_name(vrf_id
), vni
,
2804 vrf_id_to_name(tenant_vrf_id
));
2806 if (cmd
== ZEBRA_VNI_ADD
)
2807 return bgp_evpn_local_vni_add(
2809 vtep_ip
.s_addr
!= INADDR_ANY
? vtep_ip
: bgp
->router_id
,
2810 tenant_vrf_id
, mcast_grp
);
2812 return bgp_evpn_local_vni_del(bgp
, vni
);
2815 static int bgp_zebra_process_local_macip(ZAPI_CALLBACK_ARGS
)
2824 uint32_t seqnum
= 0;
2826 char buf2
[ESI_STR_LEN
];
2829 memset(&ip
, 0, sizeof(ip
));
2831 vni
= stream_getl(s
);
2832 stream_get(&mac
.octet
, s
, ETH_ALEN
);
2833 ipa_len
= stream_getl(s
);
2834 if (ipa_len
!= 0 && ipa_len
!= IPV4_MAX_BYTELEN
2835 && ipa_len
!= IPV6_MAX_BYTELEN
) {
2836 flog_err(EC_BGP_MACIP_LEN
,
2837 "%u:Recv MACIP %s with invalid IP addr length %d",
2838 vrf_id
, (cmd
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
2845 (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
: IPADDR_V6
;
2846 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
2848 if (cmd
== ZEBRA_MACIP_ADD
) {
2849 flags
= stream_getc(s
);
2850 seqnum
= stream_getl(s
);
2851 stream_get(&esi
, s
, sizeof(esi_t
));
2853 state
= stream_getl(s
);
2854 memset(&esi
, 0, sizeof(esi_t
));
2857 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2861 if (BGP_DEBUG(zebra
, ZEBRA
))
2863 "%u:Recv MACIP %s f 0x%x MAC %pEA IP %pIA VNI %u seq %u state %d ESI %s",
2864 vrf_id
, (cmd
== ZEBRA_MACIP_ADD
) ? "Add" : "Del", flags
,
2865 &mac
, &ip
, vni
, seqnum
, state
,
2866 esi_to_str(&esi
, buf2
, sizeof(buf2
)));
2868 if (cmd
== ZEBRA_MACIP_ADD
)
2869 return bgp_evpn_local_macip_add(bgp
, vni
, &mac
, &ip
,
2870 flags
, seqnum
, &esi
);
2872 return bgp_evpn_local_macip_del(bgp
, vni
, &mac
, &ip
, state
);
2875 static void bgp_zebra_process_local_ip_prefix(ZAPI_CALLBACK_ARGS
)
2877 struct stream
*s
= NULL
;
2878 struct bgp
*bgp_vrf
= NULL
;
2881 memset(&p
, 0, sizeof(struct prefix
));
2883 stream_get(&p
, s
, sizeof(struct prefix
));
2885 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
2889 if (BGP_DEBUG(zebra
, ZEBRA
))
2890 zlog_debug("Recv prefix %pFX %s on vrf %s", &p
,
2891 (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) ? "ADD" : "DEL",
2892 vrf_id_to_name(vrf_id
));
2894 if (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) {
2896 if (p
.family
== AF_INET
)
2897 bgp_evpn_advertise_type5_route(bgp_vrf
, &p
, NULL
,
2898 AFI_IP
, SAFI_UNICAST
);
2900 bgp_evpn_advertise_type5_route(bgp_vrf
, &p
, NULL
,
2901 AFI_IP6
, SAFI_UNICAST
);
2904 if (p
.family
== AF_INET
)
2905 bgp_evpn_withdraw_type5_route(bgp_vrf
, &p
, AFI_IP
,
2908 bgp_evpn_withdraw_type5_route(bgp_vrf
, &p
, AFI_IP6
,
2913 static void bgp_zebra_process_label_chunk(ZAPI_CALLBACK_ARGS
)
2915 struct stream
*s
= NULL
;
2916 uint8_t response_keep
;
2920 unsigned short instance
;
2923 STREAM_GETC(s
, proto
);
2924 STREAM_GETW(s
, instance
);
2925 STREAM_GETC(s
, response_keep
);
2926 STREAM_GETL(s
, first
);
2927 STREAM_GETL(s
, last
);
2929 if (zclient
->redist_default
!= proto
) {
2930 flog_err(EC_BGP_LM_ERROR
, "Got LM msg with wrong proto %u",
2934 if (zclient
->instance
!= instance
) {
2935 flog_err(EC_BGP_LM_ERROR
, "Got LM msg with wrong instance %u",
2941 first
< MPLS_LABEL_UNRESERVED_MIN
||
2942 last
> MPLS_LABEL_UNRESERVED_MAX
) {
2944 flog_err(EC_BGP_LM_ERROR
, "%s: Invalid Label chunk: %u - %u",
2945 __func__
, first
, last
);
2948 if (BGP_DEBUG(zebra
, ZEBRA
)) {
2949 zlog_debug("Label Chunk assign: %u - %u (%u) ",
2950 first
, last
, response_keep
);
2953 bgp_lp_event_chunk(response_keep
, first
, last
);
2955 stream_failure
: /* for STREAM_GETX */
2959 extern struct zebra_privs_t bgpd_privs
;
2961 static int bgp_ifp_create(struct interface
*ifp
)
2965 if (BGP_DEBUG(zebra
, ZEBRA
))
2966 zlog_debug("Rx Intf add VRF %u IF %s", ifp
->vrf_id
, ifp
->name
);
2968 bgp
= bgp_lookup_by_vrf_id(ifp
->vrf_id
);
2972 bgp_mac_add_mac_entry(ifp
);
2974 bgp_update_interface_nbrs(bgp
, ifp
, ifp
);
2975 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
2979 void bgp_zebra_init(struct thread_master
*master
, unsigned short instance
)
2981 zclient_num_connects
= 0;
2983 if_zapi_callbacks(bgp_ifp_create
, bgp_ifp_up
,
2984 bgp_ifp_down
, bgp_ifp_destroy
);
2986 /* Set default values. */
2987 zclient
= zclient_new(master
, &zclient_options_default
);
2988 zclient_init(zclient
, ZEBRA_ROUTE_BGP
, 0, &bgpd_privs
);
2989 zclient
->zebra_connected
= bgp_zebra_connected
;
2990 zclient
->router_id_update
= bgp_router_id_update
;
2991 zclient
->interface_address_add
= bgp_interface_address_add
;
2992 zclient
->interface_address_delete
= bgp_interface_address_delete
;
2993 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
2994 zclient
->interface_nbr_address_delete
=
2995 bgp_interface_nbr_address_delete
;
2996 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
2997 zclient
->redistribute_route_add
= zebra_read_route
;
2998 zclient
->redistribute_route_del
= zebra_read_route
;
2999 zclient
->nexthop_update
= bgp_read_nexthop_update
;
3000 zclient
->import_check_update
= bgp_read_import_check_update
;
3001 zclient
->fec_update
= bgp_read_fec_update
;
3002 zclient
->local_es_add
= bgp_zebra_process_local_es_add
;
3003 zclient
->local_es_del
= bgp_zebra_process_local_es_del
;
3004 zclient
->local_vni_add
= bgp_zebra_process_local_vni
;
3005 zclient
->local_es_evi_add
= bgp_zebra_process_local_es_evi
;
3006 zclient
->local_es_evi_del
= bgp_zebra_process_local_es_evi
;
3007 zclient
->local_vni_del
= bgp_zebra_process_local_vni
;
3008 zclient
->local_macip_add
= bgp_zebra_process_local_macip
;
3009 zclient
->local_macip_del
= bgp_zebra_process_local_macip
;
3010 zclient
->local_l3vni_add
= bgp_zebra_process_local_l3vni
;
3011 zclient
->local_l3vni_del
= bgp_zebra_process_local_l3vni
;
3012 zclient
->local_ip_prefix_add
= bgp_zebra_process_local_ip_prefix
;
3013 zclient
->local_ip_prefix_del
= bgp_zebra_process_local_ip_prefix
;
3014 zclient
->label_chunk
= bgp_zebra_process_label_chunk
;
3015 zclient
->rule_notify_owner
= rule_notify_owner
;
3016 zclient
->ipset_notify_owner
= ipset_notify_owner
;
3017 zclient
->ipset_entry_notify_owner
= ipset_entry_notify_owner
;
3018 zclient
->iptable_notify_owner
= iptable_notify_owner
;
3019 zclient
->route_notify_owner
= bgp_zebra_route_notify_owner
;
3020 zclient
->instance
= instance
;
3023 void bgp_zebra_destroy(void)
3025 if (zclient
== NULL
)
3027 zclient_stop(zclient
);
3028 zclient_free(zclient
);
3032 int bgp_zebra_num_connects(void)
3034 return zclient_num_connects
;
3037 void bgp_send_pbr_rule_action(struct bgp_pbr_action
*pbra
,
3038 struct bgp_pbr_rule
*pbr
,
3043 if (pbra
->install_in_progress
&& !pbr
)
3045 if (pbr
&& pbr
->install_in_progress
)
3047 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3049 zlog_debug("%s: table %d (ip rule) %d", __func__
,
3050 pbra
->table_id
, install
);
3052 zlog_debug("%s: table %d fwmark %d %d", __func__
,
3053 pbra
->table_id
, pbra
->fwmark
, install
);
3058 zclient_create_header(s
,
3059 install
? ZEBRA_RULE_ADD
: ZEBRA_RULE_DELETE
,
3061 stream_putl(s
, 1); /* send one pbr action */
3063 bgp_encode_pbr_rule_action(s
, pbra
, pbr
);
3065 stream_putw_at(s
, 0, stream_get_endp(s
));
3066 if ((zclient_send_message(zclient
) != ZCLIENT_SEND_FAILURE
)
3069 pbra
->install_in_progress
= true;
3071 pbr
->install_in_progress
= true;
3075 void bgp_send_pbr_ipset_match(struct bgp_pbr_match
*pbrim
, bool install
)
3079 if (pbrim
->install_in_progress
)
3081 if (BGP_DEBUG(zebra
, ZEBRA
))
3082 zlog_debug("%s: name %s type %d %d, ID %u", __func__
,
3083 pbrim
->ipset_name
, pbrim
->type
, install
,
3088 zclient_create_header(s
,
3089 install
? ZEBRA_IPSET_CREATE
:
3090 ZEBRA_IPSET_DESTROY
,
3093 stream_putl(s
, 1); /* send one pbr action */
3095 bgp_encode_pbr_ipset_match(s
, pbrim
);
3097 stream_putw_at(s
, 0, stream_get_endp(s
));
3098 if ((zclient_send_message(zclient
) != ZCLIENT_SEND_FAILURE
) && install
)
3099 pbrim
->install_in_progress
= true;
3102 void bgp_send_pbr_ipset_entry_match(struct bgp_pbr_match_entry
*pbrime
,
3107 if (pbrime
->install_in_progress
)
3109 if (BGP_DEBUG(zebra
, ZEBRA
))
3110 zlog_debug("%s: name %s %d %d, ID %u", __func__
,
3111 pbrime
->backpointer
->ipset_name
, pbrime
->unique
,
3112 install
, pbrime
->unique
);
3116 zclient_create_header(s
,
3117 install
? ZEBRA_IPSET_ENTRY_ADD
:
3118 ZEBRA_IPSET_ENTRY_DELETE
,
3121 stream_putl(s
, 1); /* send one pbr action */
3123 bgp_encode_pbr_ipset_entry_match(s
, pbrime
);
3125 stream_putw_at(s
, 0, stream_get_endp(s
));
3126 if ((zclient_send_message(zclient
) != ZCLIENT_SEND_FAILURE
) && install
)
3127 pbrime
->install_in_progress
= true;
3130 static void bgp_encode_pbr_interface_list(struct bgp
*bgp
, struct stream
*s
,
3133 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
3134 struct bgp_pbr_interface_head
*head
;
3135 struct bgp_pbr_interface
*pbr_if
;
3136 struct interface
*ifp
;
3140 if (family
== AF_INET
)
3141 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
3143 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv6
);
3144 RB_FOREACH (pbr_if
, bgp_pbr_interface_head
, head
) {
3145 ifp
= if_lookup_by_name(pbr_if
->name
, bgp
->vrf_id
);
3147 stream_putl(s
, ifp
->ifindex
);
3151 static int bgp_pbr_get_ifnumber(struct bgp
*bgp
, uint8_t family
)
3153 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
3154 struct bgp_pbr_interface_head
*head
;
3155 struct bgp_pbr_interface
*pbr_if
;
3160 if (family
== AF_INET
)
3161 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
3163 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv6
);
3164 RB_FOREACH (pbr_if
, bgp_pbr_interface_head
, head
) {
3165 if (if_lookup_by_name(pbr_if
->name
, bgp
->vrf_id
))
3171 void bgp_send_pbr_iptable(struct bgp_pbr_action
*pba
,
3172 struct bgp_pbr_match
*pbm
,
3179 if (pbm
->install_iptable_in_progress
)
3181 if (BGP_DEBUG(zebra
, ZEBRA
))
3182 zlog_debug("%s: name %s type %d mark %d %d, ID %u", __func__
,
3183 pbm
->ipset_name
, pbm
->type
, pba
->fwmark
, install
,
3188 zclient_create_header(s
,
3189 install
? ZEBRA_IPTABLE_ADD
:
3190 ZEBRA_IPTABLE_DELETE
,
3193 bgp_encode_pbr_iptable_match(s
, pba
, pbm
);
3194 nb_interface
= bgp_pbr_get_ifnumber(pba
->bgp
, pbm
->family
);
3195 stream_putl(s
, nb_interface
);
3197 bgp_encode_pbr_interface_list(pba
->bgp
, s
, pbm
->family
);
3198 stream_putw_at(s
, 0, stream_get_endp(s
));
3199 ret
= zclient_send_message(zclient
);
3201 if (ret
!= ZCLIENT_SEND_FAILURE
)
3204 pbm
->install_iptable_in_progress
= true;
3208 /* inject in table <table_id> a default route to:
3209 * - if nexthop IP is present : to this nexthop
3210 * - if vrf is different from local : to the matching VRF
3212 void bgp_zebra_announce_default(struct bgp
*bgp
, struct nexthop
*nh
,
3213 afi_t afi
, uint32_t table_id
, bool announce
)
3215 struct zapi_nexthop
*api_nh
;
3216 struct zapi_route api
;
3219 if (!nh
|| (nh
->type
!= NEXTHOP_TYPE_IPV4
3220 && nh
->type
!= NEXTHOP_TYPE_IPV6
)
3221 || nh
->vrf_id
== VRF_UNKNOWN
)
3223 memset(&p
, 0, sizeof(struct prefix
));
3224 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
)
3226 p
.family
= afi2family(afi
);
3227 memset(&api
, 0, sizeof(api
));
3228 api
.vrf_id
= bgp
->vrf_id
;
3229 api
.type
= ZEBRA_ROUTE_BGP
;
3230 api
.safi
= SAFI_UNICAST
;
3232 api
.tableid
= table_id
;
3233 api
.nexthop_num
= 1;
3234 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
3235 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
3236 api_nh
= &api
.nexthops
[0];
3238 api
.distance
= ZEBRA_EBGP_DISTANCE_DEFAULT
;
3239 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
3242 if (afi
== AFI_IP
&& nh
->gate
.ipv4
.s_addr
!= INADDR_ANY
) {
3243 char buff
[PREFIX_STRLEN
];
3245 api_nh
->vrf_id
= nh
->vrf_id
;
3246 api_nh
->gate
.ipv4
= nh
->gate
.ipv4
;
3247 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
3249 inet_ntop(AF_INET
, &(nh
->gate
.ipv4
), buff
, INET_ADDRSTRLEN
);
3250 if (BGP_DEBUG(zebra
, ZEBRA
))
3251 zlog_debug("BGP: %s default route to %s table %d (redirect IP)",
3252 announce
? "adding" : "withdrawing",
3254 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
3255 : ZEBRA_ROUTE_DELETE
,
3257 } else if (afi
== AFI_IP6
&&
3258 memcmp(&nh
->gate
.ipv6
,
3259 &in6addr_any
, sizeof(struct in6_addr
))) {
3260 char buff
[PREFIX_STRLEN
];
3262 api_nh
->vrf_id
= nh
->vrf_id
;
3263 memcpy(&api_nh
->gate
.ipv6
, &nh
->gate
.ipv6
,
3264 sizeof(struct in6_addr
));
3265 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
3267 inet_ntop(AF_INET6
, &(nh
->gate
.ipv6
), buff
, INET_ADDRSTRLEN
);
3268 if (BGP_DEBUG(zebra
, ZEBRA
))
3269 zlog_debug("BGP: %s default route to %s table %d (redirect IP)",
3270 announce
? "adding" : "withdrawing",
3272 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
3273 : ZEBRA_ROUTE_DELETE
,
3275 } else if (nh
->vrf_id
!= bgp
->vrf_id
) {
3277 struct interface
*ifp
;
3279 vrf
= vrf_lookup_by_id(nh
->vrf_id
);
3282 /* create default route with interface <VRF>
3283 * with nexthop-vrf <VRF>
3285 ifp
= if_lookup_by_name_all_vrf(vrf
->name
);
3288 api_nh
->vrf_id
= nh
->vrf_id
;
3289 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
3290 api_nh
->ifindex
= ifp
->ifindex
;
3291 if (BGP_DEBUG(zebra
, ZEBRA
))
3292 zlog_info("BGP: %s default route to %s table %d (redirect VRF)",
3293 announce
? "adding" : "withdrawing",
3294 vrf
->name
, table_id
);
3295 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
3296 : ZEBRA_ROUTE_DELETE
,
3302 /* Send capabilities to RIB */
3303 int bgp_zebra_send_capabilities(struct bgp
*bgp
, bool disable
)
3305 struct zapi_cap api
;
3306 int ret
= BGP_GR_SUCCESS
;
3308 if (zclient
== NULL
) {
3309 if (BGP_DEBUG(zebra
, ZEBRA
))
3310 zlog_debug("zclient invalid");
3311 return BGP_GR_FAILURE
;
3314 /* Check if the client is connected */
3315 if ((zclient
->sock
< 0) || (zclient
->t_connect
)) {
3316 if (BGP_DEBUG(zebra
, ZEBRA
))
3317 zlog_debug("client not connected");
3318 return BGP_GR_FAILURE
;
3321 /* Check if capability is already sent. If the flag force is set
3322 * send the capability since this can be initial bgp configuration
3324 memset(&api
, 0, sizeof(struct zapi_cap
));
3326 api
.cap
= ZEBRA_CLIENT_GR_DISABLE
;
3327 api
.vrf_id
= bgp
->vrf_id
;
3329 api
.cap
= ZEBRA_CLIENT_GR_CAPABILITIES
;
3330 api
.stale_removal_time
= bgp
->rib_stale_time
;
3331 api
.vrf_id
= bgp
->vrf_id
;
3334 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
, &api
)
3335 == ZCLIENT_SEND_FAILURE
) {
3336 zlog_err("error sending capability");
3337 ret
= BGP_GR_FAILURE
;
3340 bgp
->present_zebra_gr_state
= ZEBRA_GR_DISABLE
;
3342 bgp
->present_zebra_gr_state
= ZEBRA_GR_ENABLE
;
3344 if (BGP_DEBUG(zebra
, ZEBRA
))
3345 zlog_debug("send capabilty success");
3346 ret
= BGP_GR_SUCCESS
;
3351 /* Send route update pesding or completed status to RIB for the
3352 * specific AFI, SAFI
3354 int bgp_zebra_update(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
)
3356 struct zapi_cap api
= {0};
3358 if (zclient
== NULL
) {
3359 if (BGP_DEBUG(zebra
, ZEBRA
))
3360 zlog_debug("zclient == NULL, invalid");
3361 return BGP_GR_FAILURE
;
3364 /* Check if the client is connected */
3365 if ((zclient
->sock
< 0) || (zclient
->t_connect
)) {
3366 if (BGP_DEBUG(zebra
, ZEBRA
))
3367 zlog_debug("client not connected");
3368 return BGP_GR_FAILURE
;
3373 api
.vrf_id
= vrf_id
;
3376 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
, &api
)
3377 == ZCLIENT_SEND_FAILURE
) {
3378 if (BGP_DEBUG(zebra
, ZEBRA
))
3379 zlog_debug("error sending capability");
3380 return BGP_GR_FAILURE
;
3382 return BGP_GR_SUCCESS
;
3386 /* Send RIB stale timer update */
3387 int bgp_zebra_stale_timer_update(struct bgp
*bgp
)
3389 struct zapi_cap api
;
3391 if (zclient
== NULL
) {
3392 if (BGP_DEBUG(zebra
, ZEBRA
))
3393 zlog_debug("zclient invalid");
3394 return BGP_GR_FAILURE
;
3397 /* Check if the client is connected */
3398 if ((zclient
->sock
< 0) || (zclient
->t_connect
)) {
3399 if (BGP_DEBUG(zebra
, ZEBRA
))
3400 zlog_debug("client not connected");
3401 return BGP_GR_FAILURE
;
3404 memset(&api
, 0, sizeof(struct zapi_cap
));
3405 api
.cap
= ZEBRA_CLIENT_RIB_STALE_TIME
;
3406 api
.stale_removal_time
= bgp
->rib_stale_time
;
3407 api
.vrf_id
= bgp
->vrf_id
;
3408 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
, &api
)
3409 == ZCLIENT_SEND_FAILURE
) {
3410 if (BGP_DEBUG(zebra
, ZEBRA
))
3411 zlog_debug("error sending capability");
3412 return BGP_GR_FAILURE
;
3414 if (BGP_DEBUG(zebra
, ZEBRA
))
3415 zlog_debug("send capabilty success");
3416 return BGP_GR_SUCCESS
;