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
;
237 struct bgp
*bgp_default
= bgp_get_default();
240 bgp
= ifp
->vrf
->info
;
242 bgp_mac_add_mac_entry(ifp
);
244 if (BGP_DEBUG(zebra
, ZEBRA
))
245 zlog_debug("Rx Intf up VRF %u IF %s", ifp
->vrf
->vrf_id
,
251 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
252 bgp_connected_add(bgp
, c
);
254 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
255 bgp_nbr_connected_add(bgp
, nc
);
257 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
260 if (bgp_default
&& if_is_loopback(ifp
)) {
261 vpn_leak_zebra_vrf_label_update(bgp
, AFI_IP
);
262 vpn_leak_zebra_vrf_label_update(bgp
, AFI_IP6
);
263 vpn_leak_zebra_vrf_sid_update(bgp
, AFI_IP
);
264 vpn_leak_zebra_vrf_sid_update(bgp
, AFI_IP6
);
265 vpn_leak_postchange_all();
271 static int bgp_ifp_down(struct interface
*ifp
)
274 struct nbr_connected
*nc
;
275 struct listnode
*node
, *nnode
;
276 struct bgp
*bgp_default
= bgp_get_default();
280 bgp
= ifp
->vrf
->info
;
282 bgp_mac_del_mac_entry(ifp
);
284 if (BGP_DEBUG(zebra
, ZEBRA
))
285 zlog_debug("Rx Intf down VRF %u IF %s", ifp
->vrf
->vrf_id
,
291 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
292 bgp_connected_delete(bgp
, c
);
294 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
295 bgp_nbr_connected_delete(bgp
, nc
, 1);
297 /* Fast external-failover */
298 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
)) {
300 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
301 /* Take down directly connected peers. */
302 if ((peer
->ttl
!= BGP_DEFAULT_TTL
)
303 && (peer
->gtsm_hops
!= BGP_GTSM_HOPS_CONNECTED
))
306 if (ifp
== peer
->nexthop
.ifp
) {
307 BGP_EVENT_ADD(peer
, BGP_Stop
);
308 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
313 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
314 bgp_nht_ifp_down(ifp
);
316 if (bgp_default
&& if_is_loopback(ifp
)) {
317 vpn_leak_zebra_vrf_label_update(bgp
, AFI_IP
);
318 vpn_leak_zebra_vrf_label_update(bgp
, AFI_IP6
);
319 vpn_leak_zebra_vrf_sid_update(bgp
, AFI_IP
);
320 vpn_leak_zebra_vrf_sid_update(bgp
, AFI_IP6
);
321 vpn_leak_postchange_all();
327 static int bgp_interface_address_add(ZAPI_CALLBACK_ARGS
)
329 struct connected
*ifc
;
333 struct listnode
*node
, *nnode
;
337 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
339 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
344 if (bgp_debug_zebra(ifc
->address
))
345 zlog_debug("Rx Intf address add VRF %u IF %s addr %pFX", vrf_id
,
346 ifc
->ifp
->name
, ifc
->address
);
351 if (if_is_operative(ifc
->ifp
)) {
352 bgp_connected_add(bgp
, ifc
);
354 /* If we have learnt of any neighbors on this interface,
355 * check to kick off any BGP interface-based neighbors,
356 * but only if this is a link-local address.
358 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
)
359 && !list_isempty(ifc
->ifp
->nbr_connected
))
360 bgp_start_interface_nbrs(bgp
, ifc
->ifp
);
364 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
365 if (addr
->family
== AF_INET
)
369 * If the Peer's interface name matches the
370 * interface name for which BGP received the
371 * update and if the received interface address
372 * is a globalV6 and if the peer is currently
373 * using a v4-mapped-v6 addr or a link local
374 * address, then copy the Rxed global v6 addr
375 * into peer's v6_global and send updates out
376 * with new nexthop addr.
378 if ((peer
->conf_if
&&
379 (strcmp(peer
->conf_if
, ifc
->ifp
->name
) ==
381 !IN6_IS_ADDR_LINKLOCAL(&addr
->u
.prefix6
) &&
383 &peer
->nexthop
.v6_global
)) ||
384 IN6_IS_ADDR_LINKLOCAL(
385 &peer
->nexthop
.v6_global
))) {
387 if (bgp_debug_zebra(ifc
->address
)) {
389 "Update peer %pBP's current intf addr %pI6 and send updates",
394 memcpy(&peer
->nexthop
.v6_global
,
397 FOREACH_AFI_SAFI (afi
, safi
)
398 bgp_announce_route(peer
, afi
,
408 static int bgp_interface_address_delete(ZAPI_CALLBACK_ARGS
)
410 struct listnode
*node
, *nnode
;
411 struct connected
*ifc
;
416 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
418 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
423 if (bgp_debug_zebra(ifc
->address
))
424 zlog_debug("Rx Intf address del VRF %u IF %s addr %pFX", vrf_id
,
425 ifc
->ifp
->name
, ifc
->address
);
427 if (bgp
&& if_is_operative(ifc
->ifp
)) {
428 bgp_connected_delete(bgp
, ifc
);
435 * When we are using the v6 global as part of the peering
436 * nexthops and we are removing it, then we need to
437 * clear the peer data saved for that nexthop and
438 * cause a re-announcement of the route. Since
439 * we do not want the peering to bounce.
441 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
445 if (addr
->family
== AF_INET
)
448 if (!IN6_IS_ADDR_LINKLOCAL(&addr
->u
.prefix6
)
449 && memcmp(&peer
->nexthop
.v6_global
,
450 &addr
->u
.prefix6
, 16)
452 memset(&peer
->nexthop
.v6_global
, 0, 16);
453 FOREACH_AFI_SAFI (afi
, safi
)
454 bgp_announce_route(peer
, afi
, safi
,
460 connected_free(&ifc
);
465 static int bgp_interface_nbr_address_add(ZAPI_CALLBACK_ARGS
)
467 struct nbr_connected
*ifc
= NULL
;
470 ifc
= zebra_interface_nbr_address_read(cmd
, zclient
->ibuf
, vrf_id
);
475 if (bgp_debug_zebra(ifc
->address
))
476 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %pFX",
477 vrf_id
, ifc
->ifp
->name
, ifc
->address
);
479 if (if_is_operative(ifc
->ifp
)) {
480 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
482 bgp_nbr_connected_add(bgp
, ifc
);
488 static int bgp_interface_nbr_address_delete(ZAPI_CALLBACK_ARGS
)
490 struct nbr_connected
*ifc
= NULL
;
493 ifc
= zebra_interface_nbr_address_read(cmd
, zclient
->ibuf
, vrf_id
);
498 if (bgp_debug_zebra(ifc
->address
))
499 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %pFX",
500 vrf_id
, ifc
->ifp
->name
, ifc
->address
);
502 if (if_is_operative(ifc
->ifp
)) {
503 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
505 bgp_nbr_connected_delete(bgp
, ifc
, 0);
508 nbr_connected_free(ifc
);
513 /* VRF update for an interface. */
514 static int bgp_interface_vrf_update(ZAPI_CALLBACK_ARGS
)
516 struct interface
*ifp
;
519 struct nbr_connected
*nc
;
520 struct listnode
*node
, *nnode
;
524 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
529 if (BGP_DEBUG(zebra
, ZEBRA
))
530 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id
,
531 ifp
->name
, new_vrf_id
);
533 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
536 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
537 bgp_connected_delete(bgp
, c
);
539 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
540 bgp_nbr_connected_delete(bgp
, nc
, 1);
542 /* Fast external-failover */
543 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
)) {
544 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
545 if ((peer
->ttl
!= BGP_DEFAULT_TTL
)
547 != BGP_GTSM_HOPS_CONNECTED
))
550 if (ifp
== peer
->nexthop
.ifp
)
551 BGP_EVENT_ADD(peer
, BGP_Stop
);
556 if_update_to_new_vrf(ifp
, new_vrf_id
);
558 bgp
= bgp_lookup_by_vrf_id(new_vrf_id
);
562 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
563 bgp_connected_add(bgp
, c
);
565 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
566 bgp_nbr_connected_add(bgp
, nc
);
568 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
572 /* Zebra route add and delete treatment. */
573 static int zebra_read_route(ZAPI_CALLBACK_ARGS
)
575 enum nexthop_types_t nhtype
;
576 enum blackhole_type bhtype
= BLACKHOLE_UNSPEC
;
577 struct zapi_route api
;
578 union g_addr nexthop
= {};
583 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
587 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
590 /* we completely ignore srcdest routes for now. */
591 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
594 /* ignore link-local address. */
595 if (api
.prefix
.family
== AF_INET6
596 && IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
599 ifindex
= api
.nexthops
[0].ifindex
;
600 nhtype
= api
.nexthops
[0].type
;
602 /* api_nh structure has union of gate and bh_type */
603 if (nhtype
== NEXTHOP_TYPE_BLACKHOLE
) {
604 /* bh_type is only applicable if NEXTHOP_TYPE_BLACKHOLE*/
605 bhtype
= api
.nexthops
[0].bh_type
;
607 nexthop
= api
.nexthops
[0].gate
;
609 add
= (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
612 * The ADD message is actually an UPDATE and there is no
614 * for a prior redistributed route, if any. So, perform an
616 * DEL processing for the same redistributed route from any
620 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
622 bgp_redistribute_delete(bgp
, &api
.prefix
, i
,
626 /* Now perform the add/update. */
627 bgp_redistribute_add(bgp
, &api
.prefix
, &nexthop
, ifindex
,
628 nhtype
, bhtype
, api
.distance
, api
.metric
,
629 api
.type
, api
.instance
, api
.tag
);
631 bgp_redistribute_delete(bgp
, &api
.prefix
, api
.type
,
635 if (bgp_debug_zebra(&api
.prefix
)) {
636 char buf
[PREFIX_STRLEN
];
639 inet_ntop(api
.prefix
.family
, &nexthop
, buf
,
642 "Rx route ADD VRF %u %s[%d] %pFX nexthop %s (type %d if %u) metric %u distance %u tag %" ROUTE_TAG_PRI
,
643 vrf_id
, zebra_route_string(api
.type
),
644 api
.instance
, &api
.prefix
, buf
, nhtype
, ifindex
,
645 api
.metric
, api
.distance
, api
.tag
);
647 zlog_debug("Rx route DEL VRF %u %s[%d] %pFX", vrf_id
,
648 zebra_route_string(api
.type
), api
.instance
,
656 struct interface
*if_lookup_by_ipv4(struct in_addr
*addr
, vrf_id_t vrf_id
)
659 struct listnode
*cnode
;
660 struct interface
*ifp
;
661 struct connected
*connected
;
662 struct prefix_ipv4 p
;
665 vrf
= vrf_lookup_by_id(vrf_id
);
671 p
.prefixlen
= IPV4_MAX_BITLEN
;
673 FOR_ALL_INTERFACES (vrf
, ifp
) {
674 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
675 cp
= connected
->address
;
677 if (cp
->family
== AF_INET
)
678 if (prefix_match(cp
, (struct prefix
*)&p
))
685 struct interface
*if_lookup_by_ipv4_exact(struct in_addr
*addr
, vrf_id_t vrf_id
)
688 struct listnode
*cnode
;
689 struct interface
*ifp
;
690 struct connected
*connected
;
693 vrf
= vrf_lookup_by_id(vrf_id
);
697 FOR_ALL_INTERFACES (vrf
, ifp
) {
698 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
699 cp
= connected
->address
;
701 if (cp
->family
== AF_INET
)
702 if (IPV4_ADDR_SAME(&cp
->u
.prefix4
, addr
))
709 struct interface
*if_lookup_by_ipv6(struct in6_addr
*addr
, ifindex_t ifindex
,
713 struct listnode
*cnode
;
714 struct interface
*ifp
;
715 struct connected
*connected
;
716 struct prefix_ipv6 p
;
719 vrf
= vrf_lookup_by_id(vrf_id
);
725 p
.prefixlen
= IPV6_MAX_BITLEN
;
727 FOR_ALL_INTERFACES (vrf
, ifp
) {
728 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
729 cp
= connected
->address
;
731 if (cp
->family
== AF_INET6
)
732 if (prefix_match(cp
, (struct prefix
*)&p
)) {
733 if (IN6_IS_ADDR_LINKLOCAL(
735 if (ifindex
== ifp
->ifindex
)
745 struct interface
*if_lookup_by_ipv6_exact(struct in6_addr
*addr
,
746 ifindex_t ifindex
, vrf_id_t vrf_id
)
749 struct listnode
*cnode
;
750 struct interface
*ifp
;
751 struct connected
*connected
;
754 vrf
= vrf_lookup_by_id(vrf_id
);
758 FOR_ALL_INTERFACES (vrf
, ifp
) {
759 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
760 cp
= connected
->address
;
762 if (cp
->family
== AF_INET6
)
763 if (IPV6_ADDR_SAME(&cp
->u
.prefix6
, addr
)) {
764 if (IN6_IS_ADDR_LINKLOCAL(
766 if (ifindex
== ifp
->ifindex
)
776 static int if_get_ipv6_global(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 bool if_get_ipv6_local(struct interface
*ifp
, struct in6_addr
*addr
)
796 struct listnode
*cnode
;
797 struct connected
*connected
;
800 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
801 cp
= connected
->address
;
803 if (cp
->family
== AF_INET6
)
804 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
805 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
812 static int if_get_ipv4_address(struct interface
*ifp
, struct in_addr
*addr
)
814 struct listnode
*cnode
;
815 struct connected
*connected
;
818 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
819 cp
= connected
->address
;
820 if ((cp
->family
== AF_INET
)
821 && !ipv4_martian(&(cp
->u
.prefix4
))) {
822 *addr
= cp
->u
.prefix4
;
830 bool bgp_zebra_nexthop_set(union sockunion
*local
, union sockunion
*remote
,
831 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
834 struct interface
*ifp
= NULL
;
835 bool v6_ll_avail
= true;
837 memset(nexthop
, 0, sizeof(struct bgp_nexthop
));
844 if (local
->sa
.sa_family
== AF_INET
) {
845 nexthop
->v4
= local
->sin
.sin_addr
;
847 ifp
= if_lookup_by_name(peer
->update_if
,
850 ifp
= if_lookup_by_ipv4_exact(&local
->sin
.sin_addr
,
853 if (local
->sa
.sa_family
== AF_INET6
) {
854 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
, IPV6_MAX_BYTELEN
);
855 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
856 if (peer
->conf_if
|| peer
->ifname
)
857 ifp
= if_lookup_by_name(peer
->conf_if
861 else if (peer
->update_if
)
862 ifp
= if_lookup_by_name(peer
->update_if
,
864 } else if (peer
->update_if
)
865 ifp
= if_lookup_by_name(peer
->update_if
,
868 ifp
= if_lookup_by_ipv6_exact(&local
->sin6
.sin6_addr
,
869 local
->sin6
.sin6_scope_id
,
875 * BGP views do not currently get proper data
876 * from zebra( when attached ) to be able to
877 * properly resolve nexthops, so give this
878 * instance type a pass.
880 if (peer
->bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
883 * If we have no interface data but we have established
884 * some connection w/ zebra than something has gone
885 * terribly terribly wrong here, so say this failed
886 * If we do not any zebra connection then not
887 * having a ifp pointer is ok.
889 return zclient_num_connects
? false : true;
894 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
895 if (local
->sa
.sa_family
== AF_INET
) {
897 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
900 /* There is no global nexthop. Use link-local address as
902 * global and link-local nexthop. In this scenario, the
904 * for interop is that the network admin would use a
906 * specify the global IPv6 nexthop.
909 if_get_ipv6_local(ifp
, &nexthop
->v6_global
);
910 memcpy(&nexthop
->v6_local
, &nexthop
->v6_global
,
914 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
917 * If we are a v4 connection and we are not doing unnumbered
918 * not having a v6 LL address is ok
920 if (!v6_ll_avail
&& !peer
->conf_if
)
922 if (if_lookup_by_ipv4(&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
923 peer
->shared_network
= 1;
925 peer
->shared_network
= 0;
928 /* IPv6 connection, fetch and store IPv4 local address if any. */
929 if (local
->sa
.sa_family
== AF_INET6
) {
930 struct interface
*direct
= NULL
;
933 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
934 if (!ret
&& peer
->local_id
.s_addr
!= INADDR_ANY
)
935 nexthop
->v4
= peer
->local_id
;
938 if (!IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
939 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
942 /* If directly connected set link-local address. */
943 direct
= if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
944 remote
->sin6
.sin6_scope_id
,
947 v6_ll_avail
= if_get_ipv6_local(
948 ifp
, &nexthop
->v6_local
);
950 * It's fine to not have a v6 LL when using
951 * update-source loopback/vrf
953 if (!v6_ll_avail
&& if_is_loopback(ifp
))
955 else if (!v6_ll_avail
) {
957 EC_BGP_NO_LL_ADDRESS_AVAILABLE
,
958 "Interface: %s does not have a v6 LL address associated with it, waiting until one is created for it",
962 /* Link-local address. */
964 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
966 /* If there is no global address. Set link-local
968 global. I know this break RFC specification... */
969 /* In this scenario, the expectation for interop is that
971 * network admin would use a route-map to specify the
976 memcpy(&nexthop
->v6_global
,
977 &local
->sin6
.sin6_addr
,
979 /* Always set the link-local address */
980 memcpy(&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
984 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)
985 || if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
986 remote
->sin6
.sin6_scope_id
,
988 peer
->shared_network
= 1;
990 peer
->shared_network
= 0;
993 /* KAME stack specific treatment. */
995 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_global
)
996 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
)) {
997 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
, 0);
999 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_local
)
1000 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
)) {
1001 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
, 0);
1005 /* If we have identified the local interface, there is no error for now.
1010 static struct in6_addr
*
1011 bgp_path_info_to_ipv6_nexthop(struct bgp_path_info
*path
, ifindex_t
*ifindex
)
1013 struct in6_addr
*nexthop
= NULL
;
1015 /* Only global address nexthop exists. */
1016 if (path
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
1017 || path
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV6_GLOBAL
) {
1018 nexthop
= &path
->attr
->mp_nexthop_global
;
1019 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
1020 *ifindex
= path
->attr
->nh_ifindex
;
1023 /* If both global and link-local address present. */
1024 if (path
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
1025 || path
->attr
->mp_nexthop_len
1026 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
1027 /* Check if route-map is set to prefer global over link-local */
1028 if (path
->attr
->mp_nexthop_prefer_global
) {
1029 nexthop
= &path
->attr
->mp_nexthop_global
;
1030 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
1031 *ifindex
= path
->attr
->nh_ifindex
;
1033 /* Workaround for Cisco's nexthop bug. */
1034 if (IN6_IS_ADDR_UNSPECIFIED(
1035 &path
->attr
->mp_nexthop_global
)
1036 && path
->peer
->su_remote
1037 && path
->peer
->su_remote
->sa
.sa_family
1040 &path
->peer
->su_remote
->sin6
.sin6_addr
;
1041 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
1042 *ifindex
= path
->peer
->nexthop
.ifp
1045 nexthop
= &path
->attr
->mp_nexthop_local
;
1046 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
1047 *ifindex
= path
->attr
->nh_lla_ifindex
;
1055 static bool bgp_table_map_apply(struct route_map
*map
, const struct prefix
*p
,
1056 struct bgp_path_info
*path
)
1058 route_map_result_t ret
;
1060 ret
= route_map_apply(map
, p
, path
);
1061 bgp_attr_flush(path
->attr
);
1063 if (ret
!= RMAP_DENYMATCH
)
1066 if (bgp_debug_zebra(p
)) {
1067 if (p
->family
== AF_INET
) {
1069 "Zebra rmap deny: IPv4 route %pFX nexthop %pI4",
1070 p
, &path
->attr
->nexthop
);
1072 if (p
->family
== AF_INET6
) {
1074 struct in6_addr
*nexthop
;
1076 nexthop
= bgp_path_info_to_ipv6_nexthop(path
, &ifindex
);
1078 "Zebra rmap deny: IPv6 route %pFX nexthop %pI6",
1085 static struct thread
*bgp_tm_thread_connect
;
1086 static bool bgp_tm_status_connected
;
1087 static bool bgp_tm_chunk_obtained
;
1088 #define BGP_FLOWSPEC_TABLE_CHUNK 100000
1089 static uint32_t bgp_tm_min
, bgp_tm_max
, bgp_tm_chunk_size
;
1090 struct bgp
*bgp_tm_bgp
;
1092 static void bgp_zebra_tm_connect(struct thread
*t
)
1094 struct zclient
*zclient
;
1095 int delay
= 10, ret
= 0;
1097 zclient
= THREAD_ARG(t
);
1098 if (bgp_tm_status_connected
&& zclient
->sock
> 0)
1101 bgp_tm_status_connected
= false;
1102 ret
= tm_table_manager_connect(zclient
);
1105 zlog_info("Error connecting to table manager!");
1106 bgp_tm_status_connected
= false;
1108 if (!bgp_tm_status_connected
)
1109 zlog_debug("Connecting to table manager. Success");
1110 bgp_tm_status_connected
= true;
1111 if (!bgp_tm_chunk_obtained
) {
1112 if (bgp_zebra_get_table_range(bgp_tm_chunk_size
,
1114 &bgp_tm_max
) >= 0) {
1115 bgp_tm_chunk_obtained
= true;
1116 /* parse non installed entries */
1117 bgp_zebra_announce_table(bgp_tm_bgp
, AFI_IP
, SAFI_FLOWSPEC
);
1121 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1122 &bgp_tm_thread_connect
);
1125 bool bgp_zebra_tm_chunk_obtained(void)
1127 return bgp_tm_chunk_obtained
;
1130 uint32_t bgp_zebra_tm_get_id(void)
1132 static int table_id
;
1134 if (!bgp_tm_chunk_obtained
)
1136 return bgp_tm_min
++;
1139 void bgp_zebra_init_tm_connect(struct bgp
*bgp
)
1143 /* if already set, do nothing
1145 if (bgp_tm_thread_connect
!= NULL
)
1147 bgp_tm_status_connected
= false;
1148 bgp_tm_chunk_obtained
= false;
1149 bgp_tm_min
= bgp_tm_max
= 0;
1150 bgp_tm_chunk_size
= BGP_FLOWSPEC_TABLE_CHUNK
;
1152 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1153 &bgp_tm_thread_connect
);
1156 int bgp_zebra_get_table_range(uint32_t chunk_size
,
1157 uint32_t *start
, uint32_t *end
)
1161 if (!bgp_tm_status_connected
)
1163 ret
= tm_get_table_chunk(zclient
, chunk_size
, start
, end
);
1165 flog_err(EC_BGP_TABLE_CHUNK
,
1166 "BGP: Error getting table chunk %u", chunk_size
);
1169 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1170 chunk_size
, *start
, *end
);
1174 static bool update_ipv4nh_for_route_install(int nh_othervrf
, struct bgp
*nh_bgp
,
1175 struct in_addr
*nexthop
,
1176 struct attr
*attr
, bool is_evpn
,
1177 struct zapi_nexthop
*api_nh
)
1179 api_nh
->gate
.ipv4
= *nexthop
;
1180 api_nh
->vrf_id
= nh_bgp
->vrf_id
;
1182 /* Need to set fields appropriately for EVPN routes imported into
1183 * a VRF (which are programmed as onlink on l3-vni SVI) as well as
1184 * connected routes leaked into a VRF.
1186 if (attr
->nh_type
== NEXTHOP_TYPE_BLACKHOLE
) {
1187 api_nh
->type
= attr
->nh_type
;
1188 api_nh
->bh_type
= attr
->bh_type
;
1189 } else if (is_evpn
) {
1191 * If the nexthop is EVPN overlay index gateway IP,
1192 * treat the nexthop as NEXTHOP_TYPE_IPV4
1193 * Else, mark the nexthop as onlink.
1195 if (attr
->evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
)
1196 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1198 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1199 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
);
1200 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
);
1201 api_nh
->ifindex
= nh_bgp
->l3vni_svi_ifindex
;
1203 } else if (nh_othervrf
&& api_nh
->gate
.ipv4
.s_addr
== INADDR_ANY
) {
1204 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1205 api_nh
->ifindex
= attr
->nh_ifindex
;
1207 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1212 static bool update_ipv6nh_for_route_install(int nh_othervrf
, struct bgp
*nh_bgp
,
1213 struct in6_addr
*nexthop
,
1215 struct bgp_path_info
*pi
,
1216 struct bgp_path_info
*best_pi
,
1218 struct zapi_nexthop
*api_nh
)
1223 api_nh
->vrf_id
= nh_bgp
->vrf_id
;
1225 if (attr
->nh_type
== NEXTHOP_TYPE_BLACKHOLE
) {
1226 api_nh
->type
= attr
->nh_type
;
1227 api_nh
->bh_type
= attr
->bh_type
;
1228 } else if (is_evpn
) {
1230 * If the nexthop is EVPN overlay index gateway IP,
1231 * treat the nexthop as NEXTHOP_TYPE_IPV4
1232 * Else, mark the nexthop as onlink.
1234 if (attr
->evpn_overlay
.type
== OVERLAY_INDEX_GATEWAY_IP
)
1235 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1237 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1238 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
);
1239 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
);
1240 api_nh
->ifindex
= nh_bgp
->l3vni_svi_ifindex
;
1242 } else if (nh_othervrf
) {
1243 if (IN6_IS_ADDR_UNSPECIFIED(nexthop
)) {
1244 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1245 api_nh
->ifindex
= attr
->nh_ifindex
;
1246 } else if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1249 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1250 api_nh
->ifindex
= ifindex
;
1252 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1253 api_nh
->ifindex
= 0;
1256 if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1258 && attr
->mp_nexthop_len
1259 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1260 if (pi
->peer
->nexthop
.ifp
)
1262 pi
->peer
->nexthop
.ifp
->ifindex
;
1264 if (pi
->peer
->conf_if
)
1265 ifindex
= pi
->peer
->ifp
->ifindex
;
1266 else if (pi
->peer
->ifname
)
1267 ifindex
= ifname2ifindex(
1269 pi
->peer
->bgp
->vrf_id
);
1270 else if (pi
->peer
->nexthop
.ifp
)
1272 pi
->peer
->nexthop
.ifp
->ifindex
;
1277 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1278 api_nh
->ifindex
= ifindex
;
1280 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1281 api_nh
->ifindex
= 0;
1284 /* api_nh structure has union of gate and bh_type */
1285 if (nexthop
&& api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1286 api_nh
->gate
.ipv6
= *nexthop
;
1291 static bool bgp_zebra_use_nhop_weighted(struct bgp
*bgp
, struct attr
*attr
,
1292 uint64_t tot_bw
, uint32_t *nh_weight
)
1298 /* zero link-bandwidth and link-bandwidth not present are treated
1299 * as the same situation.
1302 /* the only situations should be if we're either told
1303 * to skip or use default weight.
1305 if (bgp
->lb_handling
== BGP_LINK_BW_SKIP_MISSING
)
1307 *nh_weight
= BGP_ZEBRA_DEFAULT_NHOP_WEIGHT
;
1309 tmp
= (uint64_t)bw
* 100;
1310 *nh_weight
= ((uint32_t)(tmp
/ tot_bw
));
1316 void bgp_zebra_announce(struct bgp_dest
*dest
, const struct prefix
*p
,
1317 struct bgp_path_info
*info
, struct bgp
*bgp
, afi_t afi
,
1320 struct zapi_route api
= { 0 };
1321 struct zapi_nexthop
*api_nh
;
1323 unsigned int valid_nh_count
= 0;
1324 bool allow_recursion
= false;
1327 struct bgp_path_info
*mpinfo
;
1328 struct bgp_path_info
*bpi_ultimate
;
1329 struct bgp
*bgp_orig
;
1331 struct attr local_attr
;
1332 struct bgp_path_info local_info
;
1333 struct bgp_path_info
*mpinfo_cp
= &local_info
;
1336 struct bgp_sid_info
*sid_info
;
1337 int nh_othervrf
= 0;
1338 bool nh_updated
= false;
1340 uint64_t cum_bw
= 0;
1341 uint32_t nhg_id
= 0;
1347 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1348 * know of this instance.
1350 if (!bgp_install_info_to_zebra(bgp
))
1353 if (bgp
->main_zebra_update_hold
)
1356 if (safi
== SAFI_FLOWSPEC
) {
1357 bgp_pbr_update_entry(bgp
, bgp_dest_get_prefix(dest
), info
, afi
,
1363 * vrf leaking support (will have only one nexthop)
1365 if (info
->extra
&& info
->extra
->bgp_orig
)
1368 /* Make Zebra API structure. */
1369 api
.vrf_id
= bgp
->vrf_id
;
1370 api
.type
= ZEBRA_ROUTE_BGP
;
1373 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1377 if (info
->type
== ZEBRA_ROUTE_BGP
) {
1378 bpi_ultimate
= bgp_get_imported_bpi_ultimate(info
);
1379 peer
= bpi_ultimate
->peer
;
1382 tag
= info
->attr
->tag
;
1384 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
1385 || info
->sub_type
== BGP_ROUTE_AGGREGATE
) {
1386 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1387 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1390 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= BGP_DEFAULT_TTL
)
1391 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1392 || CHECK_FLAG(bgp
->flags
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1394 allow_recursion
= true;
1396 if (info
->attr
->rmap_table_id
) {
1397 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
1398 api
.tableid
= info
->attr
->rmap_table_id
;
1401 if (CHECK_FLAG(info
->attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR
)))
1402 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRTE
);
1404 /* Metric is currently based on the best-path only */
1405 metric
= info
->attr
->med
;
1407 /* Determine if we're doing weighted ECMP or not */
1408 do_wt_ecmp
= bgp_path_info_mpath_chkwtd(bgp
, info
);
1410 cum_bw
= bgp_path_info_mpath_cumbw(info
);
1412 /* EVPN MAC-IP routes are installed with a L3 NHG id */
1413 if (bgp_evpn_path_es_use_nhg(bgp
, info
, &nhg_id
)) {
1417 SET_FLAG(api
.message
, ZAPI_MESSAGE_NHG
);
1422 for (; mpinfo
; mpinfo
= bgp_path_info_mpath_next(mpinfo
)) {
1426 if (valid_nh_count
>= multipath_num
)
1429 *mpinfo_cp
= *mpinfo
;
1432 /* Get nexthop address-family */
1433 if (p
->family
== AF_INET
&&
1434 !BGP_ATTR_MP_NEXTHOP_LEN_IP6(mpinfo_cp
->attr
))
1435 nh_family
= AF_INET
;
1436 else if (p
->family
== AF_INET6
||
1437 (p
->family
== AF_INET
&&
1438 BGP_ATTR_MP_NEXTHOP_LEN_IP6(mpinfo_cp
->attr
)))
1439 nh_family
= AF_INET6
;
1443 /* If processing for weighted ECMP, determine the next hop's
1444 * weight. Based on user setting, we may skip the next hop
1445 * in some situations.
1448 if (!bgp_zebra_use_nhop_weighted(bgp
, mpinfo
->attr
,
1449 cum_bw
, &nh_weight
))
1452 api_nh
= &api
.nexthops
[valid_nh_count
];
1454 if (CHECK_FLAG(info
->attr
->flag
,
1455 ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR
)))
1456 api_nh
->srte_color
= info
->attr
->srte_color
;
1458 if (bgp_debug_zebra(&api
.prefix
)) {
1459 if (mpinfo
->extra
) {
1460 zlog_debug("%s: p=%pFX, bgp_is_valid_label: %d",
1463 &mpinfo
->extra
->label
[0]));
1466 "%s: p=%pFX, extra is NULL, no label",
1471 if (bgp
->table_map
[afi
][safi
].name
) {
1472 /* Copy info and attributes, so the route-map
1473 apply doesn't modify the BGP route info. */
1474 local_attr
= *mpinfo
->attr
;
1475 mpinfo_cp
->attr
= &local_attr
;
1476 if (!bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
,
1480 /* metric/tag is only allowed to be
1481 * overridden on 1st nexthop */
1482 if (mpinfo
== info
) {
1483 metric
= mpinfo_cp
->attr
->med
;
1484 tag
= mpinfo_cp
->attr
->tag
;
1488 BGP_ORIGINAL_UPDATE(bgp_orig
, mpinfo
, bgp
);
1490 if (nh_family
== AF_INET
) {
1491 is_evpn
= is_route_parent_evpn(mpinfo
);
1493 nh_updated
= update_ipv4nh_for_route_install(
1494 nh_othervrf
, bgp_orig
,
1495 &mpinfo_cp
->attr
->nexthop
, mpinfo_cp
->attr
,
1498 ifindex_t ifindex
= IFINDEX_INTERNAL
;
1499 struct in6_addr
*nexthop
;
1501 nexthop
= bgp_path_info_to_ipv6_nexthop(mpinfo_cp
,
1504 is_evpn
= is_route_parent_evpn(mpinfo
);
1507 nh_updated
= update_ipv4nh_for_route_install(
1508 nh_othervrf
, bgp_orig
,
1509 &mpinfo_cp
->attr
->nexthop
,
1510 mpinfo_cp
->attr
, is_evpn
, api_nh
);
1512 nh_updated
= update_ipv6nh_for_route_install(
1513 nh_othervrf
, bgp_orig
, nexthop
, ifindex
,
1514 mpinfo
, info
, is_evpn
, api_nh
);
1517 /* Did we get proper nexthop info to update zebra? */
1521 /* Allow recursion if it is a multipath group with both
1522 * eBGP and iBGP paths.
1524 if (!allow_recursion
1525 && CHECK_FLAG(bgp
->flags
, BGP_FLAG_PEERTYPE_MULTIPATH_RELAX
)
1526 && (mpinfo
->peer
->sort
== BGP_PEER_IBGP
1527 || mpinfo
->peer
->sort
== BGP_PEER_CONFED
))
1528 allow_recursion
= true;
1530 if (mpinfo
->extra
&&
1531 bgp_is_valid_label(&mpinfo
->extra
->label
[0]) &&
1532 !CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
)) {
1533 mpls_lse_decode(mpinfo
->extra
->label
[0], &label
, &ttl
,
1536 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
);
1538 api_nh
->label_num
= 1;
1539 api_nh
->labels
[0] = label
;
1543 && mpinfo
->attr
->evpn_overlay
.type
1544 != OVERLAY_INDEX_GATEWAY_IP
)
1545 memcpy(&api_nh
->rmac
, &(mpinfo
->attr
->rmac
),
1546 sizeof(struct ethaddr
));
1548 api_nh
->weight
= nh_weight
;
1550 if (mpinfo
->extra
&& !sid_zero(&mpinfo
->extra
->sid
[0].sid
) &&
1551 !CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
)) {
1552 sid_info
= &mpinfo
->extra
->sid
[0];
1554 memcpy(&api_nh
->seg6_segs
, &sid_info
->sid
,
1555 sizeof(api_nh
->seg6_segs
));
1557 if (sid_info
->transposition_len
!= 0) {
1558 if (!bgp_is_valid_label(
1559 &mpinfo
->extra
->label
[0]))
1562 mpls_lse_decode(mpinfo
->extra
->label
[0], &label
,
1564 transpose_sid(&api_nh
->seg6_segs
, label
,
1565 sid_info
->transposition_offset
,
1566 sid_info
->transposition_len
);
1569 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6
);
1575 is_add
= (valid_nh_count
|| nhg_id
) ? true : false;
1577 if (is_add
&& CHECK_FLAG(bm
->flags
, BM_FLAG_SEND_EXTRA_DATA_TO_ZEBRA
)) {
1578 struct bgp_zebra_opaque bzo
= {};
1579 const char *reason
=
1580 bgp_path_selection_reason2str(dest
->reason
);
1582 strlcpy(bzo
.aspath
, info
->attr
->aspath
->str
,
1583 sizeof(bzo
.aspath
));
1585 if (info
->attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
))
1586 strlcpy(bzo
.community
,
1587 bgp_attr_get_community(info
->attr
)->str
,
1588 sizeof(bzo
.community
));
1590 if (info
->attr
->flag
1591 & ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
))
1592 strlcpy(bzo
.lcommunity
,
1593 bgp_attr_get_lcommunity(info
->attr
)->str
,
1594 sizeof(bzo
.lcommunity
));
1596 strlcpy(bzo
.selection_reason
, reason
,
1597 sizeof(bzo
.selection_reason
));
1599 SET_FLAG(api
.message
, ZAPI_MESSAGE_OPAQUE
);
1600 api
.opaque
.length
= MIN(sizeof(struct bgp_zebra_opaque
),
1601 ZAPI_MESSAGE_OPAQUE_LENGTH
);
1602 memcpy(api
.opaque
.data
, &bzo
, api
.opaque
.length
);
1605 if (allow_recursion
)
1606 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1609 * When we create an aggregate route we must also
1610 * install a Null0 route in the RIB, so overwrite
1611 * what was written into api with a blackhole route
1613 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1614 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
1616 api
.nexthop_num
= valid_nh_count
;
1618 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1619 api
.metric
= metric
;
1622 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1626 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1628 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1629 api
.distance
= distance
;
1632 if (bgp_debug_zebra(p
)) {
1633 char nh_buf
[INET6_ADDRSTRLEN
];
1634 char eth_buf
[ETHER_ADDR_STRLEN
+ 7] = {'\0'};
1635 char buf1
[ETHER_ADDR_STRLEN
];
1642 "Tx route %s VRF %u %pFX metric %u tag %" ROUTE_TAG_PRI
1644 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1645 &api
.prefix
, api
.metric
, api
.tag
, api
.nexthop_num
,
1647 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1648 api_nh
= &api
.nexthops
[i
];
1650 switch (api_nh
->type
) {
1651 case NEXTHOP_TYPE_IFINDEX
:
1654 case NEXTHOP_TYPE_IPV4
:
1655 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1656 nh_family
= AF_INET
;
1657 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1660 case NEXTHOP_TYPE_IPV6
:
1661 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1662 nh_family
= AF_INET6
;
1663 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1666 case NEXTHOP_TYPE_BLACKHOLE
:
1667 strlcpy(nh_buf
, "blackhole", sizeof(nh_buf
));
1670 /* Note: add new nexthop case */
1675 label_buf
[0] = '\0';
1678 if (CHECK_FLAG(api_nh
->flags
,
1679 ZAPI_NEXTHOP_FLAG_LABEL
) &&
1680 !CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
))
1681 snprintf(label_buf
, sizeof(label_buf
),
1682 "label %u", api_nh
->labels
[0]);
1683 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6
) &&
1684 !CHECK_FLAG(api_nh
->flags
,
1685 ZAPI_NEXTHOP_FLAG_EVPN
)) {
1686 inet_ntop(AF_INET6
, &api_nh
->seg6_segs
,
1687 sid_buf
, sizeof(sid_buf
));
1688 snprintf(segs_buf
, sizeof(segs_buf
), "segs %s",
1691 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
) &&
1692 !is_zero_mac(&api_nh
->rmac
))
1693 snprintf(eth_buf
, sizeof(eth_buf
), " RMAC %s",
1694 prefix_mac2str(&api_nh
->rmac
,
1695 buf1
, sizeof(buf1
)));
1696 zlog_debug(" nhop [%d]: %s if %u VRF %u wt %u %s %s %s",
1697 i
+ 1, nh_buf
, api_nh
->ifindex
,
1698 api_nh
->vrf_id
, api_nh
->weight
,
1699 label_buf
, segs_buf
, eth_buf
);
1702 int recursion_flag
= 0;
1704 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
))
1707 zlog_debug("%s: %pFX: announcing to zebra (recursion %sset)",
1708 __func__
, p
, (recursion_flag
? "" : "NOT "));
1710 zclient_route_send(is_add
? ZEBRA_ROUTE_ADD
: ZEBRA_ROUTE_DELETE
,
1714 /* Announce all routes of a table to zebra */
1715 void bgp_zebra_announce_table(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1717 struct bgp_dest
*dest
;
1718 struct bgp_table
*table
;
1719 struct bgp_path_info
*pi
;
1721 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1722 * know of this instance.
1724 if (!bgp_install_info_to_zebra(bgp
))
1727 table
= bgp
->rib
[afi
][safi
];
1731 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
))
1732 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
1733 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
) &&
1735 (pi
->type
== ZEBRA_ROUTE_BGP
1736 && (pi
->sub_type
== BGP_ROUTE_NORMAL
1737 || pi
->sub_type
== BGP_ROUTE_IMPORTED
)))
1739 bgp_zebra_announce(dest
,
1740 bgp_dest_get_prefix(dest
),
1741 pi
, bgp
, afi
, safi
);
1744 /* Announce routes of any bgp subtype of a table to zebra */
1745 void bgp_zebra_announce_table_all_subtypes(struct bgp
*bgp
, afi_t afi
,
1748 struct bgp_dest
*dest
;
1749 struct bgp_table
*table
;
1750 struct bgp_path_info
*pi
;
1752 if (!bgp_install_info_to_zebra(bgp
))
1755 table
= bgp
->rib
[afi
][safi
];
1759 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
))
1760 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
1761 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
) &&
1762 pi
->type
== ZEBRA_ROUTE_BGP
)
1763 bgp_zebra_announce(dest
,
1764 bgp_dest_get_prefix(dest
),
1765 pi
, bgp
, afi
, safi
);
1768 void bgp_zebra_withdraw(const struct prefix
*p
, struct bgp_path_info
*info
,
1769 struct bgp
*bgp
, safi_t safi
)
1771 struct zapi_route api
;
1774 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1775 * know of this instance.
1777 if (!bgp_install_info_to_zebra(bgp
))
1780 if (safi
== SAFI_FLOWSPEC
) {
1782 bgp_pbr_update_entry(peer
->bgp
, p
, info
, AFI_IP
, safi
, false);
1786 memset(&api
, 0, sizeof(api
));
1787 api
.vrf_id
= bgp
->vrf_id
;
1788 api
.type
= ZEBRA_ROUTE_BGP
;
1792 if (info
->attr
->rmap_table_id
) {
1793 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
1794 api
.tableid
= info
->attr
->rmap_table_id
;
1797 if (bgp_debug_zebra(p
))
1798 zlog_debug("Tx route delete VRF %u %pFX", bgp
->vrf_id
,
1801 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
1804 /* Withdraw all entries in a BGP instances RIB table from Zebra */
1805 void bgp_zebra_withdraw_table_all_subtypes(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1807 struct bgp_dest
*dest
;
1808 struct bgp_table
*table
;
1809 struct bgp_path_info
*pi
;
1811 if (!bgp_install_info_to_zebra(bgp
))
1814 table
= bgp
->rib
[afi
][safi
];
1818 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
1819 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
1820 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
1821 && (pi
->type
== ZEBRA_ROUTE_BGP
))
1822 bgp_zebra_withdraw(bgp_dest_get_prefix(dest
),
1828 struct bgp_redist
*bgp_redist_lookup(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1829 unsigned short instance
)
1831 struct list
*red_list
;
1832 struct listnode
*node
;
1833 struct bgp_redist
*red
;
1835 red_list
= bgp
->redist
[afi
][type
];
1839 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1840 if (red
->instance
== instance
)
1846 struct bgp_redist
*bgp_redist_add(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1847 unsigned short instance
)
1849 struct list
*red_list
;
1850 struct bgp_redist
*red
;
1852 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1856 if (!bgp
->redist
[afi
][type
])
1857 bgp
->redist
[afi
][type
] = list_new();
1859 red_list
= bgp
->redist
[afi
][type
];
1860 red
= XCALLOC(MTYPE_BGP_REDIST
, sizeof(struct bgp_redist
));
1861 red
->instance
= instance
;
1863 listnode_add(red_list
, red
);
1868 static void bgp_redist_del(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1869 unsigned short instance
)
1871 struct bgp_redist
*red
;
1873 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1876 listnode_delete(bgp
->redist
[afi
][type
], red
);
1877 XFREE(MTYPE_BGP_REDIST
, red
);
1878 if (!bgp
->redist
[afi
][type
]->count
)
1879 list_delete(&bgp
->redist
[afi
][type
]);
1883 /* Other routes redistribution into BGP. */
1884 int bgp_redistribute_set(struct bgp
*bgp
, afi_t afi
, int type
,
1885 unsigned short instance
, bool changed
)
1887 /* If redistribute options are changed call
1888 * bgp_redistribute_unreg() to reset the option and withdraw
1892 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1894 /* Return if already redistribute flag is set. */
1896 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1900 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1902 if (vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1905 #ifdef ENABLE_BGP_VNC
1906 if (EVPN_ENABLED(bgp
) && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1907 vnc_export_bgp_enable(
1908 bgp
, afi
); /* only enables if mode bits cfg'd */
1912 vrf_bitmap_set(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1916 * Don't try to register if we're not connected to Zebra or Zebra
1917 * doesn't know of this instance.
1919 * When we come up later well resend if needed.
1921 if (!bgp_install_info_to_zebra(bgp
))
1924 if (BGP_DEBUG(zebra
, ZEBRA
))
1925 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1926 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1929 /* Send distribute add message to zebra. */
1930 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1931 instance
, bgp
->vrf_id
);
1936 int bgp_redistribute_resend(struct bgp
*bgp
, afi_t afi
, int type
,
1937 unsigned short instance
)
1939 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1940 * know of this instance.
1942 if (!bgp_install_info_to_zebra(bgp
))
1945 if (BGP_DEBUG(zebra
, ZEBRA
))
1946 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1947 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1950 /* Send distribute add message to zebra. */
1951 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1952 instance
, bgp
->vrf_id
);
1953 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1954 instance
, bgp
->vrf_id
);
1959 /* Redistribute with route-map specification. */
1960 bool bgp_redistribute_rmap_set(struct bgp_redist
*red
, const char *name
,
1961 struct route_map
*route_map
)
1963 if (red
->rmap
.name
&& (strcmp(red
->rmap
.name
, name
) == 0))
1966 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1967 /* Decrement the count for existing routemap and
1968 * increment the count for new route map.
1970 route_map_counter_decrement(red
->rmap
.map
);
1971 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1972 red
->rmap
.map
= route_map
;
1973 route_map_counter_increment(red
->rmap
.map
);
1978 /* Redistribute with metric specification. */
1979 bool bgp_redistribute_metric_set(struct bgp
*bgp
, struct bgp_redist
*red
,
1980 afi_t afi
, int type
, uint32_t metric
)
1982 struct bgp_dest
*dest
;
1983 struct bgp_path_info
*pi
;
1985 if (red
->redist_metric_flag
&& red
->redist_metric
== metric
)
1988 red
->redist_metric_flag
= 1;
1989 red
->redist_metric
= metric
;
1991 for (dest
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); dest
;
1992 dest
= bgp_route_next(dest
)) {
1993 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
1994 if (pi
->sub_type
== BGP_ROUTE_REDISTRIBUTE
1996 && pi
->instance
== red
->instance
) {
1997 struct attr
*old_attr
;
1998 struct attr new_attr
;
2000 new_attr
= *pi
->attr
;
2001 new_attr
.med
= red
->redist_metric
;
2002 old_attr
= pi
->attr
;
2003 pi
->attr
= bgp_attr_intern(&new_attr
);
2004 bgp_attr_unintern(&old_attr
);
2006 bgp_path_info_set_flag(dest
, pi
,
2007 BGP_PATH_ATTR_CHANGED
);
2008 bgp_process(bgp
, dest
, afi
, SAFI_UNICAST
);
2016 /* Unset redistribution. */
2017 int bgp_redistribute_unreg(struct bgp
*bgp
, afi_t afi
, int type
,
2018 unsigned short instance
)
2020 struct bgp_redist
*red
;
2022 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
2026 /* Return if zebra connection is disabled. */
2028 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
],
2031 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
2033 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
2035 vrf_bitmap_unset(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
2038 if (bgp_install_info_to_zebra(bgp
)) {
2039 /* Send distribute delete message to zebra. */
2040 if (BGP_DEBUG(zebra
, ZEBRA
))
2041 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
2042 bgp
->vrf_id
, afi
, zebra_route_string(type
),
2044 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
2045 type
, instance
, bgp
->vrf_id
);
2048 /* Withdraw redistributed routes from current BGP's routing table. */
2049 bgp_redistribute_withdraw(bgp
, afi
, type
, instance
);
2054 /* Unset redistribution. */
2055 int bgp_redistribute_unset(struct bgp
*bgp
, afi_t afi
, int type
,
2056 unsigned short instance
)
2058 struct bgp_redist
*red
;
2061 * vnc and vpn->vrf checks must be before red check because
2062 * they operate within bgpd irrespective of zebra connection
2063 * status. red lookup fails if there is no zebra connection.
2065 #ifdef ENABLE_BGP_VNC
2066 if (EVPN_ENABLED(bgp
) && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
2067 vnc_export_bgp_disable(bgp
, afi
);
2071 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
2075 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
2077 /* Unset route-map. */
2078 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
2079 route_map_counter_decrement(red
->rmap
.map
);
2080 red
->rmap
.map
= NULL
;
2083 red
->redist_metric_flag
= 0;
2084 red
->redist_metric
= 0;
2086 bgp_redist_del(bgp
, afi
, type
, instance
);
2091 void bgp_redistribute_redo(struct bgp
*bgp
)
2095 struct list
*red_list
;
2096 struct listnode
*node
;
2097 struct bgp_redist
*red
;
2099 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2100 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
2102 red_list
= bgp
->redist
[afi
][i
];
2106 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
2107 bgp_redistribute_resend(bgp
, afi
, i
,
2114 void bgp_zclient_reset(void)
2116 zclient_reset(zclient
);
2119 /* Register this instance with Zebra. Invoked upon connect (for
2120 * default instance) and when other VRFs are learnt (or created and
2123 void bgp_zebra_instance_register(struct bgp
*bgp
)
2125 /* Don't try to register if we're not connected to Zebra */
2126 if (!zclient
|| zclient
->sock
< 0)
2129 if (BGP_DEBUG(zebra
, ZEBRA
))
2130 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
2132 /* Register for router-id, interfaces, redistributed routes. */
2133 zclient_send_reg_requests(zclient
, bgp
->vrf_id
);
2135 /* For EVPN instance, register to learn about VNIs, if appropriate. */
2136 if (bgp
->advertise_all_vni
)
2137 bgp_zebra_advertise_all_vni(bgp
, 1);
2139 bgp_nht_register_nexthops(bgp
);
2142 /* Deregister this instance with Zebra. Invoked upon the instance
2143 * being deleted (default or VRF) and it is already registered.
2145 void bgp_zebra_instance_deregister(struct bgp
*bgp
)
2147 /* Don't try to deregister if we're not connected to Zebra */
2148 if (zclient
->sock
< 0)
2151 if (BGP_DEBUG(zebra
, ZEBRA
))
2152 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
2154 /* For EVPN instance, unregister learning about VNIs, if appropriate. */
2155 if (bgp
->advertise_all_vni
)
2156 bgp_zebra_advertise_all_vni(bgp
, 0);
2158 /* Deregister for router-id, interfaces, redistributed routes. */
2159 zclient_send_dereg_requests(zclient
, bgp
->vrf_id
);
2162 void bgp_zebra_initiate_radv(struct bgp
*bgp
, struct peer
*peer
)
2164 uint32_t ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
2166 /* Don't try to initiate if we're not connected to Zebra */
2167 if (zclient
->sock
< 0)
2170 if (BGP_DEBUG(zebra
, ZEBRA
))
2171 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
,
2175 * If unnumbered peer (peer->ifp) call thru zapi to start RAs.
2176 * If we don't have an ifp pointer, call function to find the
2177 * ifps for a numbered enhe peer to turn RAs on.
2179 peer
->ifp
? zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
,
2180 peer
->ifp
, 1, ra_interval
)
2181 : bgp_nht_reg_enhe_cap_intfs(peer
);
2184 void bgp_zebra_terminate_radv(struct bgp
*bgp
, struct peer
*peer
)
2186 /* Don't try to terminate if we're not connected to Zebra */
2187 if (zclient
->sock
< 0)
2190 if (BGP_DEBUG(zebra
, ZEBRA
))
2191 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
,
2195 * If unnumbered peer (peer->ifp) call thru zapi to stop RAs.
2196 * If we don't have an ifp pointer, call function to find the
2197 * ifps for a numbered enhe peer to turn RAs off.
2199 peer
->ifp
? zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
,
2201 : bgp_nht_dereg_enhe_cap_intfs(peer
);
2204 int bgp_zebra_advertise_subnet(struct bgp
*bgp
, int advertise
, vni_t vni
)
2206 struct stream
*s
= NULL
;
2209 if (!zclient
|| zclient
->sock
< 0)
2212 /* Don't try to register if Zebra doesn't know of this instance. */
2213 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
2214 if (BGP_DEBUG(zebra
, ZEBRA
))
2216 "%s: No zebra instance to talk to, cannot advertise subnet",
2224 zclient_create_header(s
, ZEBRA_ADVERTISE_SUBNET
, bgp
->vrf_id
);
2225 stream_putc(s
, advertise
);
2226 stream_put3(s
, vni
);
2227 stream_putw_at(s
, 0, stream_get_endp(s
));
2229 return zclient_send_message(zclient
);
2232 int bgp_zebra_advertise_svi_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
2234 struct stream
*s
= NULL
;
2237 if (!zclient
|| zclient
->sock
< 0)
2240 /* Don't try to register if Zebra doesn't know of this instance. */
2241 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
2247 zclient_create_header(s
, ZEBRA_ADVERTISE_SVI_MACIP
, bgp
->vrf_id
);
2248 stream_putc(s
, advertise
);
2249 stream_putl(s
, vni
);
2250 stream_putw_at(s
, 0, stream_get_endp(s
));
2252 return zclient_send_message(zclient
);
2255 int bgp_zebra_advertise_gw_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
2257 struct stream
*s
= NULL
;
2260 if (!zclient
|| zclient
->sock
< 0)
2263 /* Don't try to register if Zebra doesn't know of this instance. */
2264 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
2265 if (BGP_DEBUG(zebra
, ZEBRA
))
2267 "%s: No zebra instance to talk to, not installing gw_macip",
2275 zclient_create_header(s
, ZEBRA_ADVERTISE_DEFAULT_GW
, bgp
->vrf_id
);
2276 stream_putc(s
, advertise
);
2277 stream_putl(s
, vni
);
2278 stream_putw_at(s
, 0, stream_get_endp(s
));
2280 return zclient_send_message(zclient
);
2283 int bgp_zebra_vxlan_flood_control(struct bgp
*bgp
,
2284 enum vxlan_flood_control flood_ctrl
)
2289 if (!zclient
|| zclient
->sock
< 0)
2292 /* Don't try to register if Zebra doesn't know of this instance. */
2293 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
2294 if (BGP_DEBUG(zebra
, ZEBRA
))
2296 "%s: No zebra instance to talk to, not installing all vni",
2304 zclient_create_header(s
, ZEBRA_VXLAN_FLOOD_CONTROL
, bgp
->vrf_id
);
2305 stream_putc(s
, flood_ctrl
);
2306 stream_putw_at(s
, 0, stream_get_endp(s
));
2308 return zclient_send_message(zclient
);
2311 int bgp_zebra_advertise_all_vni(struct bgp
*bgp
, int advertise
)
2316 if (!zclient
|| zclient
->sock
< 0)
2319 /* Don't try to register if Zebra doesn't know of this instance. */
2320 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
2326 zclient_create_header(s
, ZEBRA_ADVERTISE_ALL_VNI
, bgp
->vrf_id
);
2327 stream_putc(s
, advertise
);
2328 /* Also inform current BUM handling setting. This is really
2329 * relevant only when 'advertise' is set.
2331 stream_putc(s
, bgp
->vxlan_flood_ctrl
);
2332 stream_putw_at(s
, 0, stream_get_endp(s
));
2334 return zclient_send_message(zclient
);
2337 int bgp_zebra_dup_addr_detection(struct bgp
*bgp
)
2342 if (!zclient
|| zclient
->sock
< 0)
2345 /* Don't try to register if Zebra doesn't know of this instance. */
2346 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
2349 if (BGP_DEBUG(zebra
, ZEBRA
))
2350 zlog_debug("dup addr detect %s max_moves %u time %u freeze %s freeze_time %u",
2351 bgp
->evpn_info
->dup_addr_detect
?
2352 "enable" : "disable",
2353 bgp
->evpn_info
->dad_max_moves
,
2354 bgp
->evpn_info
->dad_time
,
2355 bgp
->evpn_info
->dad_freeze
?
2356 "enable" : "disable",
2357 bgp
->evpn_info
->dad_freeze_time
);
2361 zclient_create_header(s
, ZEBRA_DUPLICATE_ADDR_DETECTION
,
2363 stream_putl(s
, bgp
->evpn_info
->dup_addr_detect
);
2364 stream_putl(s
, bgp
->evpn_info
->dad_time
);
2365 stream_putl(s
, bgp
->evpn_info
->dad_max_moves
);
2366 stream_putl(s
, bgp
->evpn_info
->dad_freeze
);
2367 stream_putl(s
, bgp
->evpn_info
->dad_freeze_time
);
2368 stream_putw_at(s
, 0, stream_get_endp(s
));
2370 return zclient_send_message(zclient
);
2373 static int rule_notify_owner(ZAPI_CALLBACK_ARGS
)
2375 uint32_t seqno
, priority
, unique
;
2376 enum zapi_rule_notify_owner note
;
2377 struct bgp_pbr_action
*bgp_pbra
;
2378 struct bgp_pbr_rule
*bgp_pbr
= NULL
;
2379 char ifname
[INTERFACE_NAMSIZ
+ 1];
2381 if (!zapi_rule_notify_decode(zclient
->ibuf
, &seqno
, &priority
, &unique
,
2385 bgp_pbra
= bgp_pbr_action_rule_lookup(vrf_id
, unique
);
2387 /* look in bgp pbr rule */
2388 bgp_pbr
= bgp_pbr_rule_lookup(vrf_id
, unique
);
2389 if (!bgp_pbr
&& note
!= ZAPI_RULE_REMOVED
) {
2390 if (BGP_DEBUG(zebra
, ZEBRA
))
2391 zlog_debug("%s: Fail to look BGP rule (%u)",
2398 case ZAPI_RULE_FAIL_INSTALL
:
2399 if (BGP_DEBUG(zebra
, ZEBRA
))
2400 zlog_debug("%s: Received RULE_FAIL_INSTALL", __func__
);
2402 bgp_pbra
->installed
= false;
2403 bgp_pbra
->install_in_progress
= false;
2405 bgp_pbr
->installed
= false;
2406 bgp_pbr
->install_in_progress
= false;
2409 case ZAPI_RULE_INSTALLED
:
2411 bgp_pbra
->installed
= true;
2412 bgp_pbra
->install_in_progress
= false;
2414 struct bgp_path_info
*path
;
2415 struct bgp_path_info_extra
*extra
;
2417 bgp_pbr
->installed
= true;
2418 bgp_pbr
->install_in_progress
= false;
2419 bgp_pbr
->action
->refcnt
++;
2420 /* link bgp_info to bgp_pbr */
2421 path
= (struct bgp_path_info
*)bgp_pbr
->path
;
2422 extra
= bgp_path_info_extra_get(path
);
2423 listnode_add_force(&extra
->bgp_fs_iprule
,
2426 if (BGP_DEBUG(zebra
, ZEBRA
))
2427 zlog_debug("%s: Received RULE_INSTALLED", __func__
);
2429 case ZAPI_RULE_FAIL_REMOVE
:
2430 case ZAPI_RULE_REMOVED
:
2431 if (BGP_DEBUG(zebra
, ZEBRA
))
2432 zlog_debug("%s: Received RULE REMOVED", __func__
);
2439 static int ipset_notify_owner(ZAPI_CALLBACK_ARGS
)
2442 enum zapi_ipset_notify_owner note
;
2443 struct bgp_pbr_match
*bgp_pbim
;
2445 if (!zapi_ipset_notify_decode(zclient
->ibuf
,
2450 bgp_pbim
= bgp_pbr_match_ipset_lookup(vrf_id
, unique
);
2452 if (BGP_DEBUG(zebra
, ZEBRA
))
2453 zlog_debug("%s: Fail to look BGP match ( %u, ID %u)",
2454 __func__
, note
, unique
);
2459 case ZAPI_IPSET_FAIL_INSTALL
:
2460 if (BGP_DEBUG(zebra
, ZEBRA
))
2461 zlog_debug("%s: Received IPSET_FAIL_INSTALL", __func__
);
2462 bgp_pbim
->installed
= false;
2463 bgp_pbim
->install_in_progress
= false;
2465 case ZAPI_IPSET_INSTALLED
:
2466 bgp_pbim
->installed
= true;
2467 bgp_pbim
->install_in_progress
= false;
2468 if (BGP_DEBUG(zebra
, ZEBRA
))
2469 zlog_debug("%s: Received IPSET_INSTALLED", __func__
);
2471 case ZAPI_IPSET_FAIL_REMOVE
:
2472 case ZAPI_IPSET_REMOVED
:
2473 if (BGP_DEBUG(zebra
, ZEBRA
))
2474 zlog_debug("%s: Received IPSET REMOVED", __func__
);
2481 static int ipset_entry_notify_owner(ZAPI_CALLBACK_ARGS
)
2484 char ipset_name
[ZEBRA_IPSET_NAME_SIZE
];
2485 enum zapi_ipset_entry_notify_owner note
;
2486 struct bgp_pbr_match_entry
*bgp_pbime
;
2488 if (!zapi_ipset_entry_notify_decode(
2494 bgp_pbime
= bgp_pbr_match_ipset_entry_lookup(vrf_id
,
2498 if (BGP_DEBUG(zebra
, ZEBRA
))
2500 "%s: Fail to look BGP match entry (%u, ID %u)",
2501 __func__
, note
, unique
);
2506 case ZAPI_IPSET_ENTRY_FAIL_INSTALL
:
2507 if (BGP_DEBUG(zebra
, ZEBRA
))
2508 zlog_debug("%s: Received IPSET_ENTRY_FAIL_INSTALL",
2510 bgp_pbime
->installed
= false;
2511 bgp_pbime
->install_in_progress
= false;
2513 case ZAPI_IPSET_ENTRY_INSTALLED
:
2515 struct bgp_path_info
*path
;
2516 struct bgp_path_info_extra
*extra
;
2518 bgp_pbime
->installed
= true;
2519 bgp_pbime
->install_in_progress
= false;
2520 if (BGP_DEBUG(zebra
, ZEBRA
))
2521 zlog_debug("%s: Received IPSET_ENTRY_INSTALLED",
2523 /* link bgp_path_info to bpme */
2524 path
= (struct bgp_path_info
*)bgp_pbime
->path
;
2525 extra
= bgp_path_info_extra_get(path
);
2526 listnode_add_force(&extra
->bgp_fs_pbr
, bgp_pbime
);
2529 case ZAPI_IPSET_ENTRY_FAIL_REMOVE
:
2530 case ZAPI_IPSET_ENTRY_REMOVED
:
2531 if (BGP_DEBUG(zebra
, ZEBRA
))
2532 zlog_debug("%s: Received IPSET_ENTRY_REMOVED",
2539 static int iptable_notify_owner(ZAPI_CALLBACK_ARGS
)
2542 enum zapi_iptable_notify_owner note
;
2543 struct bgp_pbr_match
*bgpm
;
2545 if (!zapi_iptable_notify_decode(
2550 bgpm
= bgp_pbr_match_iptable_lookup(vrf_id
, unique
);
2552 if (BGP_DEBUG(zebra
, ZEBRA
))
2553 zlog_debug("%s: Fail to look BGP iptable (%u %u)",
2554 __func__
, note
, unique
);
2558 case ZAPI_IPTABLE_FAIL_INSTALL
:
2559 if (BGP_DEBUG(zebra
, ZEBRA
))
2560 zlog_debug("%s: Received IPTABLE_FAIL_INSTALL",
2562 bgpm
->installed_in_iptable
= false;
2563 bgpm
->install_iptable_in_progress
= false;
2565 case ZAPI_IPTABLE_INSTALLED
:
2566 bgpm
->installed_in_iptable
= true;
2567 bgpm
->install_iptable_in_progress
= false;
2568 if (BGP_DEBUG(zebra
, ZEBRA
))
2569 zlog_debug("%s: Received IPTABLE_INSTALLED", __func__
);
2570 bgpm
->action
->refcnt
++;
2572 case ZAPI_IPTABLE_FAIL_REMOVE
:
2573 case ZAPI_IPTABLE_REMOVED
:
2574 if (BGP_DEBUG(zebra
, ZEBRA
))
2575 zlog_debug("%s: Received IPTABLE REMOVED", __func__
);
2581 /* Process route notification messages from RIB */
2582 static int bgp_zebra_route_notify_owner(int command
, struct zclient
*zclient
,
2583 zebra_size_t length
, vrf_id_t vrf_id
)
2586 enum zapi_route_notify_owner note
;
2590 struct bgp_dest
*dest
;
2592 struct bgp_path_info
*pi
, *new_select
;
2594 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table_id
, ¬e
,
2596 zlog_err("%s : error in msg decode", __func__
);
2600 /* Get the bgp instance */
2601 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2603 flog_err(EC_BGP_INVALID_BGP_INSTANCE
,
2604 "%s : bgp instance not found vrf %d", __func__
,
2609 /* Find the bgp route node */
2610 dest
= bgp_afi_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
, &p
,
2616 case ZAPI_ROUTE_INSTALLED
:
2618 /* Clear the flags so that route can be processed */
2619 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALL_PENDING
);
2620 SET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALLED
);
2621 if (BGP_DEBUG(zebra
, ZEBRA
))
2622 zlog_debug("route %pRN : INSTALLED", dest
);
2623 /* Find the best route */
2624 for (pi
= dest
->info
; pi
; pi
= pi
->next
) {
2625 /* Process aggregate route */
2626 bgp_aggregate_increment(bgp
, &p
, pi
, afi
, safi
);
2627 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
))
2630 /* Advertise the route */
2632 group_announce_route(bgp
, afi
, safi
, dest
, new_select
);
2634 flog_err(EC_BGP_INVALID_ROUTE
,
2635 "selected route %pRN not found", dest
);
2637 bgp_dest_unlock_node(dest
);
2641 case ZAPI_ROUTE_REMOVED
:
2642 /* Route deleted from dataplane, reset the installed flag
2643 * so that route can be reinstalled when client sends
2646 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALLED
);
2647 if (BGP_DEBUG(zebra
, ZEBRA
))
2648 zlog_debug("route %pRN: Removed from Fib", dest
);
2650 case ZAPI_ROUTE_FAIL_INSTALL
:
2652 if (BGP_DEBUG(zebra
, ZEBRA
))
2653 zlog_debug("route: %pRN Failed to Install into Fib",
2655 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALL_PENDING
);
2656 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALLED
);
2657 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
2658 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
))
2662 group_announce_route(bgp
, afi
, safi
, dest
, new_select
);
2663 /* Error will be logged by zebra module */
2665 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
2666 if (BGP_DEBUG(zebra
, ZEBRA
))
2667 zlog_debug("route: %pRN removed due to better admin won",
2670 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALL_PENDING
);
2671 UNSET_FLAG(dest
->flags
, BGP_NODE_FIB_INSTALLED
);
2672 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
2673 bgp_aggregate_decrement(bgp
, &p
, pi
, afi
, safi
);
2674 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
))
2678 group_announce_route(bgp
, afi
, safi
, dest
, new_select
);
2679 /* No action required */
2681 case ZAPI_ROUTE_REMOVE_FAIL
:
2682 zlog_warn("%s: Route %pRN failure to remove",
2687 bgp_dest_unlock_node(dest
);
2691 /* this function is used to forge ip rule,
2692 * - either for iptable/ipset using fwmark id
2693 * - or for sample ip rule cmd
2695 static void bgp_encode_pbr_rule_action(struct stream
*s
,
2696 struct bgp_pbr_action
*pbra
,
2697 struct bgp_pbr_rule
*pbr
)
2700 uint8_t fam
= AF_INET
;
2701 char ifname
[INTERFACE_NAMSIZ
];
2703 if (pbra
->nh
.type
== NEXTHOP_TYPE_IPV6
)
2705 stream_putl(s
, 0); /* seqno unused */
2707 stream_putl(s
, pbr
->priority
);
2710 /* ruleno unused - priority change
2711 * ruleno permits distinguishing various FS PBR entries
2712 * - FS PBR entries based on ipset/iptables
2713 * - FS PBR entries based on iprule
2714 * the latter may contain default routing information injected by FS
2717 stream_putl(s
, pbr
->unique
);
2719 stream_putl(s
, pbra
->unique
);
2720 stream_putc(s
, 0); /* ip protocol being used */
2721 if (pbr
&& pbr
->flags
& MATCH_IP_SRC_SET
)
2722 memcpy(&pfx
, &(pbr
->src
), sizeof(struct prefix
));
2724 memset(&pfx
, 0, sizeof(pfx
));
2727 stream_putc(s
, pfx
.family
);
2728 stream_putc(s
, pfx
.prefixlen
);
2729 stream_put(s
, &pfx
.u
.prefix
, prefix_blen(&pfx
));
2731 stream_putw(s
, 0); /* src port */
2733 if (pbr
&& pbr
->flags
& MATCH_IP_DST_SET
)
2734 memcpy(&pfx
, &(pbr
->dst
), sizeof(struct prefix
));
2736 memset(&pfx
, 0, sizeof(pfx
));
2739 stream_putc(s
, pfx
.family
);
2740 stream_putc(s
, pfx
.prefixlen
);
2741 stream_put(s
, &pfx
.u
.prefix
, prefix_blen(&pfx
));
2743 stream_putw(s
, 0); /* dst port */
2744 stream_putc(s
, 0); /* dsfield */
2745 /* if pbr present, fwmark is not used */
2749 stream_putl(s
, pbra
->fwmark
); /* fwmark */
2751 stream_putl(s
, 0); /* queue id */
2752 stream_putw(s
, 0); /* vlan_id */
2753 stream_putw(s
, 0); /* vlan_flags */
2754 stream_putw(s
, 0); /* pcp */
2756 stream_putl(s
, pbra
->table_id
);
2758 memset(ifname
, 0, sizeof(ifname
));
2759 stream_put(s
, ifname
, INTERFACE_NAMSIZ
); /* ifname unused */
2762 static void bgp_encode_pbr_ipset_match(struct stream
*s
,
2763 struct bgp_pbr_match
*pbim
)
2765 stream_putl(s
, pbim
->unique
);
2766 stream_putl(s
, pbim
->type
);
2767 stream_putc(s
, pbim
->family
);
2768 stream_put(s
, pbim
->ipset_name
,
2769 ZEBRA_IPSET_NAME_SIZE
);
2772 static void bgp_encode_pbr_ipset_entry_match(struct stream
*s
,
2773 struct bgp_pbr_match_entry
*pbime
)
2775 stream_putl(s
, pbime
->unique
);
2776 /* check that back pointer is not null */
2777 stream_put(s
, pbime
->backpointer
->ipset_name
,
2778 ZEBRA_IPSET_NAME_SIZE
);
2780 stream_putc(s
, pbime
->src
.family
);
2781 stream_putc(s
, pbime
->src
.prefixlen
);
2782 stream_put(s
, &pbime
->src
.u
.prefix
, prefix_blen(&pbime
->src
));
2784 stream_putc(s
, pbime
->dst
.family
);
2785 stream_putc(s
, pbime
->dst
.prefixlen
);
2786 stream_put(s
, &pbime
->dst
.u
.prefix
, prefix_blen(&pbime
->dst
));
2788 stream_putw(s
, pbime
->src_port_min
);
2789 stream_putw(s
, pbime
->src_port_max
);
2790 stream_putw(s
, pbime
->dst_port_min
);
2791 stream_putw(s
, pbime
->dst_port_max
);
2792 stream_putc(s
, pbime
->proto
);
2795 static void bgp_encode_pbr_iptable_match(struct stream
*s
,
2796 struct bgp_pbr_action
*bpa
,
2797 struct bgp_pbr_match
*pbm
)
2799 stream_putl(s
, pbm
->unique2
);
2801 stream_putl(s
, pbm
->type
);
2803 stream_putl(s
, pbm
->flags
);
2805 /* TODO: correlate with what is contained
2806 * into bgp_pbr_action.
2807 * currently only forward supported
2809 if (bpa
->nh
.type
== NEXTHOP_TYPE_BLACKHOLE
)
2810 stream_putl(s
, ZEBRA_IPTABLES_DROP
);
2812 stream_putl(s
, ZEBRA_IPTABLES_FORWARD
);
2813 stream_putl(s
, bpa
->fwmark
);
2814 stream_put(s
, pbm
->ipset_name
,
2815 ZEBRA_IPSET_NAME_SIZE
);
2816 stream_putc(s
, pbm
->family
);
2817 stream_putw(s
, pbm
->pkt_len_min
);
2818 stream_putw(s
, pbm
->pkt_len_max
);
2819 stream_putw(s
, pbm
->tcp_flags
);
2820 stream_putw(s
, pbm
->tcp_mask_flags
);
2821 stream_putc(s
, pbm
->dscp_value
);
2822 stream_putc(s
, pbm
->fragment
);
2823 stream_putc(s
, pbm
->protocol
);
2824 stream_putw(s
, pbm
->flow_label
);
2827 /* BGP has established connection with Zebra. */
2828 static void bgp_zebra_connected(struct zclient
*zclient
)
2832 zclient_num_connects
++; /* increment even if not responding */
2834 /* Send the client registration */
2835 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
2837 /* At this point, we may or may not have BGP instances configured, but
2838 * we're only interested in the default VRF (others wouldn't have learnt
2839 * the VRF from Zebra yet.)
2841 bgp
= bgp_get_default();
2845 bgp_zebra_instance_register(bgp
);
2847 /* tell label pool that zebra is connected */
2848 bgp_lp_event_zebra_up();
2850 /* TODO - What if we have peers and networks configured, do we have to
2853 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp
, bgp
->peer
);
2856 static int bgp_zebra_process_local_es_add(ZAPI_CALLBACK_ARGS
)
2859 struct bgp
*bgp
= NULL
;
2860 struct stream
*s
= NULL
;
2861 char buf
[ESI_STR_LEN
];
2862 struct in_addr originator_ip
;
2867 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2872 stream_get(&esi
, s
, sizeof(esi_t
));
2873 originator_ip
.s_addr
= stream_get_ipv4(s
);
2874 active
= stream_getc(s
);
2875 df_pref
= stream_getw(s
);
2876 bypass
= stream_getc(s
);
2878 if (BGP_DEBUG(zebra
, ZEBRA
))
2880 "Rx add ESI %s originator-ip %pI4 active %u df_pref %u %s",
2881 esi_to_str(&esi
, buf
, sizeof(buf
)), &originator_ip
,
2882 active
, df_pref
, bypass
? "bypass" : "");
2884 frrtrace(5, frr_bgp
, evpn_mh_local_es_add_zrecv
, &esi
, originator_ip
,
2885 active
, bypass
, df_pref
);
2887 bgp_evpn_local_es_add(bgp
, &esi
, originator_ip
, active
, df_pref
,
2893 static int bgp_zebra_process_local_es_del(ZAPI_CALLBACK_ARGS
)
2896 struct bgp
*bgp
= NULL
;
2897 struct stream
*s
= NULL
;
2898 char buf
[ESI_STR_LEN
];
2900 memset(&esi
, 0, sizeof(esi_t
));
2901 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2906 stream_get(&esi
, s
, sizeof(esi_t
));
2908 if (BGP_DEBUG(zebra
, ZEBRA
))
2909 zlog_debug("Rx del ESI %s",
2910 esi_to_str(&esi
, buf
, sizeof(buf
)));
2912 frrtrace(1, frr_bgp
, evpn_mh_local_es_del_zrecv
, &esi
);
2914 bgp_evpn_local_es_del(bgp
, &esi
);
2919 static int bgp_zebra_process_local_es_evi(ZAPI_CALLBACK_ARGS
)
2925 char buf
[ESI_STR_LEN
];
2927 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2932 stream_get(&esi
, s
, sizeof(esi_t
));
2933 vni
= stream_getl(s
);
2935 if (BGP_DEBUG(zebra
, ZEBRA
))
2936 zlog_debug("Rx %s ESI %s VNI %u",
2937 (cmd
== ZEBRA_VNI_ADD
) ? "add" : "del",
2938 esi_to_str(&esi
, buf
, sizeof(buf
)), vni
);
2940 if (cmd
== ZEBRA_LOCAL_ES_EVI_ADD
) {
2941 frrtrace(2, frr_bgp
, evpn_mh_local_es_evi_add_zrecv
, &esi
, vni
);
2943 bgp_evpn_local_es_evi_add(bgp
, &esi
, vni
);
2945 frrtrace(2, frr_bgp
, evpn_mh_local_es_evi_del_zrecv
, &esi
, vni
);
2947 bgp_evpn_local_es_evi_del(bgp
, &esi
, vni
);
2953 static int bgp_zebra_process_local_l3vni(ZAPI_CALLBACK_ARGS
)
2957 struct ethaddr svi_rmac
, vrr_rmac
= {.octet
= {0} };
2958 struct in_addr originator_ip
;
2960 ifindex_t svi_ifindex
;
2961 bool is_anycast_mac
= false;
2963 memset(&svi_rmac
, 0, sizeof(svi_rmac
));
2964 memset(&originator_ip
, 0, sizeof(originator_ip
));
2966 l3vni
= stream_getl(s
);
2967 if (cmd
== ZEBRA_L3VNI_ADD
) {
2968 stream_get(&svi_rmac
, s
, sizeof(struct ethaddr
));
2969 originator_ip
.s_addr
= stream_get_ipv4(s
);
2970 stream_get(&filter
, s
, sizeof(int));
2971 svi_ifindex
= stream_getl(s
);
2972 stream_get(&vrr_rmac
, s
, sizeof(struct ethaddr
));
2973 is_anycast_mac
= stream_getl(s
);
2975 if (BGP_DEBUG(zebra
, ZEBRA
))
2977 "Rx L3-VNI ADD VRF %s VNI %u RMAC svi-mac %pEA vrr-mac %pEA filter %s svi-if %u",
2978 vrf_id_to_name(vrf_id
), l3vni
, &svi_rmac
,
2980 filter
? "prefix-routes-only" : "none",
2983 frrtrace(8, frr_bgp
, evpn_local_l3vni_add_zrecv
, l3vni
, vrf_id
,
2984 &svi_rmac
, &vrr_rmac
, filter
, originator_ip
,
2985 svi_ifindex
, is_anycast_mac
);
2987 bgp_evpn_local_l3vni_add(l3vni
, vrf_id
, &svi_rmac
, &vrr_rmac
,
2988 originator_ip
, filter
, svi_ifindex
,
2991 if (BGP_DEBUG(zebra
, ZEBRA
))
2992 zlog_debug("Rx L3-VNI DEL VRF %s VNI %u",
2993 vrf_id_to_name(vrf_id
), l3vni
);
2995 frrtrace(2, frr_bgp
, evpn_local_l3vni_del_zrecv
, l3vni
, vrf_id
);
2997 bgp_evpn_local_l3vni_del(l3vni
, vrf_id
);
3003 static int bgp_zebra_process_local_vni(ZAPI_CALLBACK_ARGS
)
3008 struct in_addr vtep_ip
= {INADDR_ANY
};
3009 vrf_id_t tenant_vrf_id
= VRF_DEFAULT
;
3010 struct in_addr mcast_grp
= {INADDR_ANY
};
3011 ifindex_t svi_ifindex
= 0;
3014 vni
= stream_getl(s
);
3015 if (cmd
== ZEBRA_VNI_ADD
) {
3016 vtep_ip
.s_addr
= stream_get_ipv4(s
);
3017 stream_get(&tenant_vrf_id
, s
, sizeof(vrf_id_t
));
3018 mcast_grp
.s_addr
= stream_get_ipv4(s
);
3019 stream_get(&svi_ifindex
, s
, sizeof(ifindex_t
));
3022 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
3026 if (BGP_DEBUG(zebra
, ZEBRA
))
3028 "Rx VNI %s VRF %s VNI %u tenant-vrf %s SVI ifindex %u",
3029 (cmd
== ZEBRA_VNI_ADD
) ? "add" : "del",
3030 vrf_id_to_name(vrf_id
), vni
,
3031 vrf_id_to_name(tenant_vrf_id
), svi_ifindex
);
3033 if (cmd
== ZEBRA_VNI_ADD
) {
3034 frrtrace(4, frr_bgp
, evpn_local_vni_add_zrecv
, vni
, vtep_ip
,
3035 tenant_vrf_id
, mcast_grp
);
3037 return bgp_evpn_local_vni_add(
3039 vtep_ip
.s_addr
!= INADDR_ANY
? vtep_ip
: bgp
->router_id
,
3040 tenant_vrf_id
, mcast_grp
, svi_ifindex
);
3042 frrtrace(1, frr_bgp
, evpn_local_vni_del_zrecv
, vni
);
3044 return bgp_evpn_local_vni_del(bgp
, vni
);
3048 static int bgp_zebra_process_local_macip(ZAPI_CALLBACK_ARGS
)
3057 uint32_t seqnum
= 0;
3059 char buf2
[ESI_STR_LEN
];
3062 memset(&ip
, 0, sizeof(ip
));
3064 vni
= stream_getl(s
);
3065 stream_get(&mac
.octet
, s
, ETH_ALEN
);
3066 ipa_len
= stream_getl(s
);
3067 if (ipa_len
!= 0 && ipa_len
!= IPV4_MAX_BYTELEN
3068 && ipa_len
!= IPV6_MAX_BYTELEN
) {
3069 flog_err(EC_BGP_MACIP_LEN
,
3070 "%u:Recv MACIP %s with invalid IP addr length %d",
3071 vrf_id
, (cmd
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
3078 (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
: IPADDR_V6
;
3079 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
3081 if (cmd
== ZEBRA_MACIP_ADD
) {
3082 flags
= stream_getc(s
);
3083 seqnum
= stream_getl(s
);
3084 stream_get(&esi
, s
, sizeof(esi_t
));
3086 state
= stream_getl(s
);
3087 memset(&esi
, 0, sizeof(esi_t
));
3090 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
3094 if (BGP_DEBUG(zebra
, ZEBRA
))
3096 "%u:Recv MACIP %s f 0x%x MAC %pEA IP %pIA VNI %u seq %u state %d ESI %s",
3097 vrf_id
, (cmd
== ZEBRA_MACIP_ADD
) ? "Add" : "Del", flags
,
3098 &mac
, &ip
, vni
, seqnum
, state
,
3099 esi_to_str(&esi
, buf2
, sizeof(buf2
)));
3101 if (cmd
== ZEBRA_MACIP_ADD
) {
3102 frrtrace(6, frr_bgp
, evpn_local_macip_add_zrecv
, vni
, &mac
, &ip
,
3103 flags
, seqnum
, &esi
);
3105 return bgp_evpn_local_macip_add(bgp
, vni
, &mac
, &ip
,
3106 flags
, seqnum
, &esi
);
3108 frrtrace(4, frr_bgp
, evpn_local_macip_del_zrecv
, vni
, &mac
, &ip
,
3111 return bgp_evpn_local_macip_del(bgp
, vni
, &mac
, &ip
, state
);
3115 static int bgp_zebra_process_local_ip_prefix(ZAPI_CALLBACK_ARGS
)
3117 struct stream
*s
= NULL
;
3118 struct bgp
*bgp_vrf
= NULL
;
3121 memset(&p
, 0, sizeof(p
));
3123 stream_get(&p
, s
, sizeof(struct prefix
));
3125 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
3129 if (BGP_DEBUG(zebra
, ZEBRA
))
3130 zlog_debug("Recv prefix %pFX %s on vrf %s", &p
,
3131 (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) ? "ADD" : "DEL",
3132 vrf_id_to_name(vrf_id
));
3134 if (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) {
3136 if (p
.family
== AF_INET
)
3137 bgp_evpn_advertise_type5_route(bgp_vrf
, &p
, NULL
,
3138 AFI_IP
, SAFI_UNICAST
);
3140 bgp_evpn_advertise_type5_route(bgp_vrf
, &p
, NULL
,
3141 AFI_IP6
, SAFI_UNICAST
);
3144 if (p
.family
== AF_INET
)
3145 bgp_evpn_withdraw_type5_route(bgp_vrf
, &p
, AFI_IP
,
3148 bgp_evpn_withdraw_type5_route(bgp_vrf
, &p
, AFI_IP6
,
3154 static int bgp_zebra_process_label_chunk(ZAPI_CALLBACK_ARGS
)
3156 struct stream
*s
= NULL
;
3157 uint8_t response_keep
;
3161 unsigned short instance
;
3164 STREAM_GETC(s
, proto
);
3165 STREAM_GETW(s
, instance
);
3166 STREAM_GETC(s
, response_keep
);
3167 STREAM_GETL(s
, first
);
3168 STREAM_GETL(s
, last
);
3170 if (zclient
->redist_default
!= proto
) {
3171 flog_err(EC_BGP_LM_ERROR
, "Got LM msg with wrong proto %u",
3175 if (zclient
->instance
!= instance
) {
3176 flog_err(EC_BGP_LM_ERROR
, "Got LM msg with wrong instance %u",
3182 first
< MPLS_LABEL_UNRESERVED_MIN
||
3183 last
> MPLS_LABEL_UNRESERVED_MAX
) {
3185 flog_err(EC_BGP_LM_ERROR
, "%s: Invalid Label chunk: %u - %u",
3186 __func__
, first
, last
);
3189 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3190 zlog_debug("Label Chunk assign: %u - %u (%u) ",
3191 first
, last
, response_keep
);
3194 bgp_lp_event_chunk(response_keep
, first
, last
);
3198 stream_failure
: /* for STREAM_GETX */
3202 extern struct zebra_privs_t bgpd_privs
;
3204 static int bgp_ifp_create(struct interface
*ifp
)
3206 struct bgp
*bgp_default
= bgp_get_default();
3209 if (BGP_DEBUG(zebra
, ZEBRA
))
3210 zlog_debug("Rx Intf add VRF %u IF %s", ifp
->vrf
->vrf_id
,
3213 bgp
= ifp
->vrf
->info
;
3217 bgp_mac_add_mac_entry(ifp
);
3219 bgp_update_interface_nbrs(bgp
, ifp
, ifp
);
3220 hook_call(bgp_vrf_status_changed
, bgp
, ifp
);
3223 (if_is_loopback_exact(ifp
) ||
3224 (if_is_vrf(ifp
) && ifp
->vrf
->vrf_id
!= VRF_DEFAULT
))) {
3225 vpn_leak_zebra_vrf_label_update(bgp
, AFI_IP
);
3226 vpn_leak_zebra_vrf_label_update(bgp
, AFI_IP6
);
3227 vpn_leak_zebra_vrf_sid_update(bgp
, AFI_IP
);
3228 vpn_leak_zebra_vrf_sid_update(bgp
, AFI_IP6
);
3229 vpn_leak_postchange_all();
3235 static int bgp_zebra_process_srv6_locator_chunk(ZAPI_CALLBACK_ARGS
)
3237 struct stream
*s
= NULL
;
3238 struct bgp
*bgp
= bgp_get_default();
3239 struct listnode
*node
;
3240 struct srv6_locator_chunk
*c
;
3241 struct srv6_locator_chunk
*chunk
= srv6_locator_chunk_alloc();
3244 zapi_srv6_locator_chunk_decode(s
, chunk
);
3246 if (strcmp(bgp
->srv6_locator_name
, chunk
->locator_name
) != 0) {
3247 zlog_err("%s: Locator name unmatch %s:%s", __func__
,
3248 bgp
->srv6_locator_name
, chunk
->locator_name
);
3249 srv6_locator_chunk_free(&chunk
);
3253 for (ALL_LIST_ELEMENTS_RO(bgp
->srv6_locator_chunks
, node
, c
)) {
3254 if (!prefix_cmp(&c
->prefix
, &chunk
->prefix
)) {
3255 srv6_locator_chunk_free(&chunk
);
3260 listnode_add(bgp
->srv6_locator_chunks
, chunk
);
3261 vpn_leak_postchange_all();
3265 static int bgp_zebra_process_srv6_locator_add(ZAPI_CALLBACK_ARGS
)
3267 struct srv6_locator loc
= {};
3268 struct bgp
*bgp
= bgp_get_default();
3269 const char *loc_name
= bgp
->srv6_locator_name
;
3271 if (zapi_srv6_locator_decode(zclient
->ibuf
, &loc
) < 0)
3274 if (!bgp
|| !bgp
->srv6_enabled
)
3277 if (bgp_zebra_srv6_manager_get_locator_chunk(loc_name
) < 0)
3283 static int bgp_zebra_process_srv6_locator_delete(ZAPI_CALLBACK_ARGS
)
3285 struct srv6_locator loc
= {};
3286 struct bgp
*bgp
= bgp_get_default();
3287 struct listnode
*node
, *nnode
;
3288 struct srv6_locator_chunk
*chunk
, *tovpn_sid_locator
;
3289 struct bgp_srv6_function
*func
;
3290 struct bgp
*bgp_vrf
;
3291 struct in6_addr
*tovpn_sid
;
3292 struct prefix_ipv6 tmp_prefi
;
3294 if (zapi_srv6_locator_decode(zclient
->ibuf
, &loc
) < 0)
3298 for (ALL_LIST_ELEMENTS(bgp
->srv6_locator_chunks
, node
, nnode
, chunk
))
3299 if (prefix_match((struct prefix
*)&loc
.prefix
,
3300 (struct prefix
*)&chunk
->prefix
)) {
3301 listnode_delete(bgp
->srv6_locator_chunks
, chunk
);
3302 srv6_locator_chunk_free(&chunk
);
3305 // refresh functions
3306 for (ALL_LIST_ELEMENTS(bgp
->srv6_functions
, node
, nnode
, func
)) {
3307 tmp_prefi
.family
= AF_INET6
;
3308 tmp_prefi
.prefixlen
= 128;
3309 tmp_prefi
.prefix
= func
->sid
;
3310 if (prefix_match((struct prefix
*)&loc
.prefix
,
3311 (struct prefix
*)&tmp_prefi
)) {
3312 listnode_delete(bgp
->srv6_functions
, func
);
3313 XFREE(MTYPE_BGP_SRV6_FUNCTION
, func
);
3317 // refresh tovpn_sid
3318 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
3319 if (bgp_vrf
->inst_type
!= BGP_INSTANCE_TYPE_VRF
)
3322 // refresh vpnv4 tovpn_sid
3323 tovpn_sid
= bgp_vrf
->vpn_policy
[AFI_IP
].tovpn_sid
;
3325 tmp_prefi
.family
= AF_INET6
;
3326 tmp_prefi
.prefixlen
= 128;
3327 tmp_prefi
.prefix
= *tovpn_sid
;
3328 if (prefix_match((struct prefix
*)&loc
.prefix
,
3329 (struct prefix
*)&tmp_prefi
))
3330 XFREE(MTYPE_BGP_SRV6_SID
,
3331 bgp_vrf
->vpn_policy
[AFI_IP
].tovpn_sid
);
3334 // refresh vpnv6 tovpn_sid
3335 tovpn_sid
= bgp_vrf
->vpn_policy
[AFI_IP6
].tovpn_sid
;
3337 tmp_prefi
.family
= AF_INET6
;
3338 tmp_prefi
.prefixlen
= 128;
3339 tmp_prefi
.prefix
= *tovpn_sid
;
3340 if (prefix_match((struct prefix
*)&loc
.prefix
,
3341 (struct prefix
*)&tmp_prefi
))
3342 XFREE(MTYPE_BGP_SRV6_SID
,
3343 bgp_vrf
->vpn_policy
[AFI_IP6
].tovpn_sid
);
3346 /* refresh per-vrf tovpn_sid */
3347 tovpn_sid
= bgp_vrf
->tovpn_sid
;
3349 tmp_prefi
.family
= AF_INET6
;
3350 tmp_prefi
.prefixlen
= IPV6_MAX_BITLEN
;
3351 tmp_prefi
.prefix
= *tovpn_sid
;
3352 if (prefix_match((struct prefix
*)&loc
.prefix
,
3353 (struct prefix
*)&tmp_prefi
))
3354 XFREE(MTYPE_BGP_SRV6_SID
, bgp_vrf
->tovpn_sid
);
3358 vpn_leak_postchange_all();
3360 /* refresh tovpn_sid_locator */
3361 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
3362 if (bgp_vrf
->inst_type
!= BGP_INSTANCE_TYPE_VRF
)
3365 /* refresh vpnv4 tovpn_sid_locator */
3367 bgp_vrf
->vpn_policy
[AFI_IP
].tovpn_sid_locator
;
3368 if (tovpn_sid_locator
) {
3369 tmp_prefi
.family
= AF_INET6
;
3370 tmp_prefi
.prefixlen
= IPV6_MAX_BITLEN
;
3371 tmp_prefi
.prefix
= tovpn_sid_locator
->prefix
.prefix
;
3372 if (prefix_match((struct prefix
*)&loc
.prefix
,
3373 (struct prefix
*)&tmp_prefi
))
3374 srv6_locator_chunk_free(
3375 &bgp_vrf
->vpn_policy
[AFI_IP
]
3376 .tovpn_sid_locator
);
3379 /* refresh vpnv6 tovpn_sid_locator */
3381 bgp_vrf
->vpn_policy
[AFI_IP6
].tovpn_sid_locator
;
3382 if (tovpn_sid_locator
) {
3383 tmp_prefi
.family
= AF_INET6
;
3384 tmp_prefi
.prefixlen
= IPV6_MAX_BITLEN
;
3385 tmp_prefi
.prefix
= tovpn_sid_locator
->prefix
.prefix
;
3386 if (prefix_match((struct prefix
*)&loc
.prefix
,
3387 (struct prefix
*)&tmp_prefi
))
3388 srv6_locator_chunk_free(
3389 &bgp_vrf
->vpn_policy
[AFI_IP6
]
3390 .tovpn_sid_locator
);
3393 /* refresh per-vrf tovpn_sid_locator */
3394 tovpn_sid_locator
= bgp_vrf
->tovpn_sid_locator
;
3395 if (tovpn_sid_locator
) {
3396 tmp_prefi
.family
= AF_INET6
;
3397 tmp_prefi
.prefixlen
= IPV6_MAX_BITLEN
;
3398 tmp_prefi
.prefix
= tovpn_sid_locator
->prefix
.prefix
;
3399 if (prefix_match((struct prefix
*)&loc
.prefix
,
3400 (struct prefix
*)&tmp_prefi
))
3401 srv6_locator_chunk_free(
3402 &bgp_vrf
->tovpn_sid_locator
);
3409 static zclient_handler
*const bgp_handlers
[] = {
3410 [ZEBRA_ROUTER_ID_UPDATE
] = bgp_router_id_update
,
3411 [ZEBRA_INTERFACE_ADDRESS_ADD
] = bgp_interface_address_add
,
3412 [ZEBRA_INTERFACE_ADDRESS_DELETE
] = bgp_interface_address_delete
,
3413 [ZEBRA_INTERFACE_NBR_ADDRESS_ADD
] = bgp_interface_nbr_address_add
,
3414 [ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
] = bgp_interface_nbr_address_delete
,
3415 [ZEBRA_INTERFACE_VRF_UPDATE
] = bgp_interface_vrf_update
,
3416 [ZEBRA_REDISTRIBUTE_ROUTE_ADD
] = zebra_read_route
,
3417 [ZEBRA_REDISTRIBUTE_ROUTE_DEL
] = zebra_read_route
,
3418 [ZEBRA_NEXTHOP_UPDATE
] = bgp_read_nexthop_update
,
3419 [ZEBRA_FEC_UPDATE
] = bgp_read_fec_update
,
3420 [ZEBRA_LOCAL_ES_ADD
] = bgp_zebra_process_local_es_add
,
3421 [ZEBRA_LOCAL_ES_DEL
] = bgp_zebra_process_local_es_del
,
3422 [ZEBRA_VNI_ADD
] = bgp_zebra_process_local_vni
,
3423 [ZEBRA_LOCAL_ES_EVI_ADD
] = bgp_zebra_process_local_es_evi
,
3424 [ZEBRA_LOCAL_ES_EVI_DEL
] = bgp_zebra_process_local_es_evi
,
3425 [ZEBRA_VNI_DEL
] = bgp_zebra_process_local_vni
,
3426 [ZEBRA_MACIP_ADD
] = bgp_zebra_process_local_macip
,
3427 [ZEBRA_MACIP_DEL
] = bgp_zebra_process_local_macip
,
3428 [ZEBRA_L3VNI_ADD
] = bgp_zebra_process_local_l3vni
,
3429 [ZEBRA_L3VNI_DEL
] = bgp_zebra_process_local_l3vni
,
3430 [ZEBRA_IP_PREFIX_ROUTE_ADD
] = bgp_zebra_process_local_ip_prefix
,
3431 [ZEBRA_IP_PREFIX_ROUTE_DEL
] = bgp_zebra_process_local_ip_prefix
,
3432 [ZEBRA_GET_LABEL_CHUNK
] = bgp_zebra_process_label_chunk
,
3433 [ZEBRA_RULE_NOTIFY_OWNER
] = rule_notify_owner
,
3434 [ZEBRA_IPSET_NOTIFY_OWNER
] = ipset_notify_owner
,
3435 [ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
] = ipset_entry_notify_owner
,
3436 [ZEBRA_IPTABLE_NOTIFY_OWNER
] = iptable_notify_owner
,
3437 [ZEBRA_ROUTE_NOTIFY_OWNER
] = bgp_zebra_route_notify_owner
,
3438 [ZEBRA_SRV6_LOCATOR_ADD
] = bgp_zebra_process_srv6_locator_add
,
3439 [ZEBRA_SRV6_LOCATOR_DELETE
] = bgp_zebra_process_srv6_locator_delete
,
3440 [ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK
] =
3441 bgp_zebra_process_srv6_locator_chunk
,
3442 [ZEBRA_OPAQUE_MESSAGE
] = bgp_opaque_msg_handler
,
3445 static int bgp_if_new_hook(struct interface
*ifp
)
3447 struct bgp_interface
*iifp
;
3451 iifp
= XCALLOC(MTYPE_BGP_IF_INFO
, sizeof(struct bgp_interface
));
3457 static int bgp_if_delete_hook(struct interface
*ifp
)
3459 XFREE(MTYPE_BGP_IF_INFO
, ifp
->info
);
3463 void bgp_if_init(void)
3465 /* Initialize Zebra interface data structure. */
3466 hook_register_prio(if_add
, 0, bgp_if_new_hook
);
3467 hook_register_prio(if_del
, 0, bgp_if_delete_hook
);
3470 void bgp_zebra_init(struct thread_master
*master
, unsigned short instance
)
3472 zclient_num_connects
= 0;
3474 if_zapi_callbacks(bgp_ifp_create
, bgp_ifp_up
,
3475 bgp_ifp_down
, bgp_ifp_destroy
);
3477 /* Set default values. */
3478 zclient
= zclient_new(master
, &zclient_options_default
, bgp_handlers
,
3479 array_size(bgp_handlers
));
3480 zclient_init(zclient
, ZEBRA_ROUTE_BGP
, 0, &bgpd_privs
);
3481 zclient
->zebra_connected
= bgp_zebra_connected
;
3482 zclient
->instance
= instance
;
3485 void bgp_zebra_destroy(void)
3487 if (zclient
== NULL
)
3489 zclient_stop(zclient
);
3490 zclient_free(zclient
);
3494 int bgp_zebra_num_connects(void)
3496 return zclient_num_connects
;
3499 void bgp_send_pbr_rule_action(struct bgp_pbr_action
*pbra
,
3500 struct bgp_pbr_rule
*pbr
,
3505 if (pbra
->install_in_progress
&& !pbr
)
3507 if (pbr
&& pbr
->install_in_progress
)
3509 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3511 zlog_debug("%s: table %d (ip rule) %d", __func__
,
3512 pbra
->table_id
, install
);
3514 zlog_debug("%s: table %d fwmark %d %d", __func__
,
3515 pbra
->table_id
, pbra
->fwmark
, install
);
3520 zclient_create_header(s
,
3521 install
? ZEBRA_RULE_ADD
: ZEBRA_RULE_DELETE
,
3523 stream_putl(s
, 1); /* send one pbr action */
3525 bgp_encode_pbr_rule_action(s
, pbra
, pbr
);
3527 stream_putw_at(s
, 0, stream_get_endp(s
));
3528 if ((zclient_send_message(zclient
) != ZCLIENT_SEND_FAILURE
)
3531 pbra
->install_in_progress
= true;
3533 pbr
->install_in_progress
= true;
3537 void bgp_send_pbr_ipset_match(struct bgp_pbr_match
*pbrim
, bool install
)
3541 if (pbrim
->install_in_progress
)
3543 if (BGP_DEBUG(zebra
, ZEBRA
))
3544 zlog_debug("%s: name %s type %d %d, ID %u", __func__
,
3545 pbrim
->ipset_name
, pbrim
->type
, install
,
3550 zclient_create_header(s
,
3551 install
? ZEBRA_IPSET_CREATE
:
3552 ZEBRA_IPSET_DESTROY
,
3555 stream_putl(s
, 1); /* send one pbr action */
3557 bgp_encode_pbr_ipset_match(s
, pbrim
);
3559 stream_putw_at(s
, 0, stream_get_endp(s
));
3560 if ((zclient_send_message(zclient
) != ZCLIENT_SEND_FAILURE
) && install
)
3561 pbrim
->install_in_progress
= true;
3564 void bgp_send_pbr_ipset_entry_match(struct bgp_pbr_match_entry
*pbrime
,
3569 if (pbrime
->install_in_progress
)
3571 if (BGP_DEBUG(zebra
, ZEBRA
))
3572 zlog_debug("%s: name %s %d %d, ID %u", __func__
,
3573 pbrime
->backpointer
->ipset_name
, pbrime
->unique
,
3574 install
, pbrime
->unique
);
3578 zclient_create_header(s
,
3579 install
? ZEBRA_IPSET_ENTRY_ADD
:
3580 ZEBRA_IPSET_ENTRY_DELETE
,
3583 stream_putl(s
, 1); /* send one pbr action */
3585 bgp_encode_pbr_ipset_entry_match(s
, pbrime
);
3587 stream_putw_at(s
, 0, stream_get_endp(s
));
3588 if ((zclient_send_message(zclient
) != ZCLIENT_SEND_FAILURE
) && install
)
3589 pbrime
->install_in_progress
= true;
3592 static void bgp_encode_pbr_interface_list(struct bgp
*bgp
, struct stream
*s
,
3595 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
3596 struct bgp_pbr_interface_head
*head
;
3597 struct bgp_pbr_interface
*pbr_if
;
3598 struct interface
*ifp
;
3602 if (family
== AF_INET
)
3603 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
3605 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv6
);
3606 RB_FOREACH (pbr_if
, bgp_pbr_interface_head
, head
) {
3607 ifp
= if_lookup_by_name(pbr_if
->name
, bgp
->vrf_id
);
3609 stream_putl(s
, ifp
->ifindex
);
3613 static int bgp_pbr_get_ifnumber(struct bgp
*bgp
, uint8_t family
)
3615 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
3616 struct bgp_pbr_interface_head
*head
;
3617 struct bgp_pbr_interface
*pbr_if
;
3622 if (family
== AF_INET
)
3623 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
3625 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv6
);
3626 RB_FOREACH (pbr_if
, bgp_pbr_interface_head
, head
) {
3627 if (if_lookup_by_name(pbr_if
->name
, bgp
->vrf_id
))
3633 void bgp_send_pbr_iptable(struct bgp_pbr_action
*pba
,
3634 struct bgp_pbr_match
*pbm
,
3641 if (pbm
->install_iptable_in_progress
)
3643 if (BGP_DEBUG(zebra
, ZEBRA
))
3644 zlog_debug("%s: name %s type %d mark %d %d, ID %u", __func__
,
3645 pbm
->ipset_name
, pbm
->type
, pba
->fwmark
, install
,
3650 zclient_create_header(s
,
3651 install
? ZEBRA_IPTABLE_ADD
:
3652 ZEBRA_IPTABLE_DELETE
,
3655 bgp_encode_pbr_iptable_match(s
, pba
, pbm
);
3656 nb_interface
= bgp_pbr_get_ifnumber(pba
->bgp
, pbm
->family
);
3657 stream_putl(s
, nb_interface
);
3659 bgp_encode_pbr_interface_list(pba
->bgp
, s
, pbm
->family
);
3660 stream_putw_at(s
, 0, stream_get_endp(s
));
3661 ret
= zclient_send_message(zclient
);
3663 if (ret
!= ZCLIENT_SEND_FAILURE
)
3666 pbm
->install_iptable_in_progress
= true;
3670 /* inject in table <table_id> a default route to:
3671 * - if nexthop IP is present : to this nexthop
3672 * - if vrf is different from local : to the matching VRF
3674 void bgp_zebra_announce_default(struct bgp
*bgp
, struct nexthop
*nh
,
3675 afi_t afi
, uint32_t table_id
, bool announce
)
3677 struct zapi_nexthop
*api_nh
;
3678 struct zapi_route api
;
3681 if (!nh
|| (nh
->type
!= NEXTHOP_TYPE_IPV4
3682 && nh
->type
!= NEXTHOP_TYPE_IPV6
)
3683 || nh
->vrf_id
== VRF_UNKNOWN
)
3686 /* in vrf-lite, no default route has to be announced
3687 * the table id of vrf is directly used to divert traffic
3689 if (!vrf_is_backend_netns() && bgp
->vrf_id
!= nh
->vrf_id
)
3692 memset(&p
, 0, sizeof(p
));
3693 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
)
3695 p
.family
= afi2family(afi
);
3696 memset(&api
, 0, sizeof(api
));
3697 api
.vrf_id
= bgp
->vrf_id
;
3698 api
.type
= ZEBRA_ROUTE_BGP
;
3699 api
.safi
= SAFI_UNICAST
;
3701 api
.tableid
= table_id
;
3702 api
.nexthop_num
= 1;
3703 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
3704 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
3705 api_nh
= &api
.nexthops
[0];
3707 api
.distance
= ZEBRA_EBGP_DISTANCE_DEFAULT
;
3708 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
3711 if (afi
== AFI_IP
&& nh
->gate
.ipv4
.s_addr
!= INADDR_ANY
) {
3712 api_nh
->vrf_id
= nh
->vrf_id
;
3713 api_nh
->gate
.ipv4
= nh
->gate
.ipv4
;
3714 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
3716 if (BGP_DEBUG(zebra
, ZEBRA
))
3718 "BGP: %s default route to %pI4 table %d (redirect IP)",
3719 announce
? "adding" : "withdrawing",
3720 &nh
->gate
.ipv4
, table_id
);
3722 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
3723 : ZEBRA_ROUTE_DELETE
,
3725 } else if (afi
== AFI_IP6
&&
3726 memcmp(&nh
->gate
.ipv6
,
3727 &in6addr_any
, sizeof(struct in6_addr
))) {
3728 api_nh
->vrf_id
= nh
->vrf_id
;
3729 memcpy(&api_nh
->gate
.ipv6
, &nh
->gate
.ipv6
,
3730 sizeof(struct in6_addr
));
3731 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
3733 if (BGP_DEBUG(zebra
, ZEBRA
))
3735 "BGP: %s default route to %pI6 table %d (redirect IP)",
3736 announce
? "adding" : "withdrawing",
3737 &nh
->gate
.ipv6
, table_id
);
3739 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
3740 : ZEBRA_ROUTE_DELETE
,
3742 } else if (nh
->vrf_id
!= bgp
->vrf_id
) {
3744 struct interface
*ifp
;
3746 vrf
= vrf_lookup_by_id(nh
->vrf_id
);
3749 /* create default route with interface <VRF>
3750 * with nexthop-vrf <VRF>
3752 ifp
= if_lookup_by_name_vrf(vrf
->name
, vrf
);
3755 api_nh
->vrf_id
= nh
->vrf_id
;
3756 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
3757 api_nh
->ifindex
= ifp
->ifindex
;
3758 if (BGP_DEBUG(zebra
, ZEBRA
))
3759 zlog_info("BGP: %s default route to %s table %d (redirect VRF)",
3760 announce
? "adding" : "withdrawing",
3761 vrf
->name
, table_id
);
3762 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
3763 : ZEBRA_ROUTE_DELETE
,
3769 /* Send capabilities to RIB */
3770 int bgp_zebra_send_capabilities(struct bgp
*bgp
, bool disable
)
3772 struct zapi_cap api
;
3773 int ret
= BGP_GR_SUCCESS
;
3775 if (zclient
== NULL
) {
3776 if (BGP_DEBUG(zebra
, ZEBRA
))
3777 zlog_debug("zclient invalid");
3778 return BGP_GR_FAILURE
;
3781 /* Check if the client is connected */
3782 if ((zclient
->sock
< 0) || (zclient
->t_connect
)) {
3783 if (BGP_DEBUG(zebra
, ZEBRA
))
3784 zlog_debug("client not connected");
3785 return BGP_GR_FAILURE
;
3788 /* Check if capability is already sent. If the flag force is set
3789 * send the capability since this can be initial bgp configuration
3791 memset(&api
, 0, sizeof(api
));
3793 api
.cap
= ZEBRA_CLIENT_GR_DISABLE
;
3794 api
.vrf_id
= bgp
->vrf_id
;
3796 api
.cap
= ZEBRA_CLIENT_GR_CAPABILITIES
;
3797 api
.stale_removal_time
= bgp
->rib_stale_time
;
3798 api
.vrf_id
= bgp
->vrf_id
;
3801 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
, &api
)
3802 == ZCLIENT_SEND_FAILURE
) {
3803 zlog_err("error sending capability");
3804 ret
= BGP_GR_FAILURE
;
3807 bgp
->present_zebra_gr_state
= ZEBRA_GR_DISABLE
;
3809 bgp
->present_zebra_gr_state
= ZEBRA_GR_ENABLE
;
3811 if (BGP_DEBUG(zebra
, ZEBRA
))
3812 zlog_debug("send capabilty success");
3813 ret
= BGP_GR_SUCCESS
;
3818 /* Send route update pesding or completed status to RIB for the
3819 * specific AFI, SAFI
3821 int bgp_zebra_update(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
)
3823 struct zapi_cap api
= {0};
3825 if (zclient
== NULL
) {
3826 if (BGP_DEBUG(zebra
, ZEBRA
))
3827 zlog_debug("zclient == NULL, invalid");
3828 return BGP_GR_FAILURE
;
3831 /* Check if the client is connected */
3832 if ((zclient
->sock
< 0) || (zclient
->t_connect
)) {
3833 if (BGP_DEBUG(zebra
, ZEBRA
))
3834 zlog_debug("client not connected");
3835 return BGP_GR_FAILURE
;
3840 api
.vrf_id
= vrf_id
;
3843 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
, &api
)
3844 == ZCLIENT_SEND_FAILURE
) {
3845 if (BGP_DEBUG(zebra
, ZEBRA
))
3846 zlog_debug("error sending capability");
3847 return BGP_GR_FAILURE
;
3849 return BGP_GR_SUCCESS
;
3853 /* Send RIB stale timer update */
3854 int bgp_zebra_stale_timer_update(struct bgp
*bgp
)
3856 struct zapi_cap api
;
3858 if (zclient
== NULL
) {
3859 if (BGP_DEBUG(zebra
, ZEBRA
))
3860 zlog_debug("zclient invalid");
3861 return BGP_GR_FAILURE
;
3864 /* Check if the client is connected */
3865 if ((zclient
->sock
< 0) || (zclient
->t_connect
)) {
3866 if (BGP_DEBUG(zebra
, ZEBRA
))
3867 zlog_debug("client not connected");
3868 return BGP_GR_FAILURE
;
3871 memset(&api
, 0, sizeof(api
));
3872 api
.cap
= ZEBRA_CLIENT_RIB_STALE_TIME
;
3873 api
.stale_removal_time
= bgp
->rib_stale_time
;
3874 api
.vrf_id
= bgp
->vrf_id
;
3875 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
, &api
)
3876 == ZCLIENT_SEND_FAILURE
) {
3877 if (BGP_DEBUG(zebra
, ZEBRA
))
3878 zlog_debug("error sending capability");
3879 return BGP_GR_FAILURE
;
3881 if (BGP_DEBUG(zebra
, ZEBRA
))
3882 zlog_debug("send capabilty success");
3883 return BGP_GR_SUCCESS
;
3886 int bgp_zebra_srv6_manager_get_locator_chunk(const char *name
)
3888 return srv6_manager_get_locator_chunk(zclient
, name
);
3891 int bgp_zebra_srv6_manager_release_locator_chunk(const char *name
)
3893 return srv6_manager_release_locator_chunk(zclient
, name
);
3897 * ORR messages between processes
3899 static int bgp_opaque_msg_handler(ZAPI_CALLBACK_ARGS
)
3902 struct zapi_opaque_msg info
;
3903 struct orr_igp_metric_info table
;
3908 if (zclient_opaque_decode(s
, &info
) != 0) {
3909 bgp_orr_debug("%s: opaque decode failed", __func__
);
3913 switch (info
.type
) {
3914 case ORR_IGP_METRIC_UPDATE
:
3915 STREAM_GET(&table
, s
, sizeof(table
));
3916 ret
= bgg_orr_message_process(BGP_ORR_IMSG_IGP_METRIC_UPDATE
,