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"
41 #include "bgpd/bgpd.h"
42 #include "bgpd/bgp_route.h"
43 #include "bgpd/bgp_attr.h"
44 #include "bgpd/bgp_nexthop.h"
45 #include "bgpd/bgp_zebra.h"
46 #include "bgpd/bgp_fsm.h"
47 #include "bgpd/bgp_debug.h"
48 #include "bgpd/bgp_errors.h"
49 #include "bgpd/bgp_mpath.h"
50 #include "bgpd/bgp_nexthop.h"
51 #include "bgpd/bgp_nht.h"
52 #include "bgpd/bgp_bfd.h"
53 #include "bgpd/bgp_label.h"
55 #include "bgpd/rfapi/rfapi_backend.h"
56 #include "bgpd/rfapi/vnc_export_bgp.h"
58 #include "bgpd/bgp_evpn.h"
59 #include "bgpd/bgp_mplsvpn.h"
60 #include "bgpd/bgp_labelpool.h"
61 #include "bgpd/bgp_pbr.h"
62 #include "bgpd/bgp_evpn_private.h"
63 #include "bgpd/bgp_evpn_mh.h"
64 #include "bgpd/bgp_mac.h"
66 /* All information about zebra. */
67 struct zclient
*zclient
= NULL
;
69 /* Can we install into zebra? */
70 static inline bool bgp_install_info_to_zebra(struct bgp
*bgp
)
72 if (zclient
->sock
<= 0)
75 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
77 "%s: No zebra instance to talk to, not installing information",
85 int zclient_num_connects
;
87 /* Router-id update message from zebra. */
88 static int bgp_router_id_update(ZAPI_CALLBACK_ARGS
)
90 struct prefix router_id
;
92 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
94 if (BGP_DEBUG(zebra
, ZEBRA
)) {
95 char buf
[PREFIX2STR_BUFFER
];
96 prefix2str(&router_id
, buf
, sizeof(buf
));
97 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id
, buf
);
100 bgp_router_id_zebra_bump(vrf_id
, &router_id
);
104 /* Nexthop update message from zebra. */
105 static int bgp_read_nexthop_update(ZAPI_CALLBACK_ARGS
)
107 bgp_parse_nexthop_update(cmd
, vrf_id
);
111 static int bgp_read_import_check_update(ZAPI_CALLBACK_ARGS
)
113 bgp_parse_nexthop_update(cmd
, vrf_id
);
117 /* Set or clear interface on which unnumbered neighbor is configured. This
118 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
121 static void bgp_update_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
,
122 struct interface
*upd_ifp
)
124 struct listnode
*node
, *nnode
;
127 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
128 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)) {
131 bgp_zebra_initiate_radv(bgp
, peer
);
133 bgp_zebra_terminate_radv(bgp
, peer
);
140 static int bgp_read_fec_update(int command
, struct zclient
*zclient
,
143 bgp_parse_fec_update();
147 static void bgp_start_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
)
149 struct listnode
*node
, *nnode
;
152 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
153 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)
154 && peer
->status
!= Established
) {
155 if (peer_active(peer
))
156 BGP_EVENT_ADD(peer
, BGP_Stop
);
157 BGP_EVENT_ADD(peer
, BGP_Start
);
162 static void bgp_nbr_connected_add(struct bgp
*bgp
, struct nbr_connected
*ifc
)
164 struct listnode
*node
;
165 struct connected
*connected
;
166 struct interface
*ifp
;
169 /* Kick-off the FSM for any relevant peers only if there is a
170 * valid local address on the interface.
173 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
174 p
= connected
->address
;
175 if (p
->family
== AF_INET6
176 && IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
182 bgp_start_interface_nbrs(bgp
, ifp
);
185 static void bgp_nbr_connected_delete(struct bgp
*bgp
, struct nbr_connected
*ifc
,
188 struct listnode
*node
, *nnode
;
190 struct interface
*ifp
;
192 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
194 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)) {
195 peer
->last_reset
= PEER_DOWN_NBR_ADDR_DEL
;
196 BGP_EVENT_ADD(peer
, BGP_Stop
);
199 /* Free neighbor also, if we're asked to. */
202 listnode_delete(ifp
->nbr_connected
, ifc
);
203 nbr_connected_free(ifc
);
207 static int bgp_ifp_destroy(struct interface
*ifp
)
211 bgp
= bgp_lookup_by_vrf_id(ifp
->vrf_id
);
213 if (BGP_DEBUG(zebra
, ZEBRA
))
214 zlog_debug("Rx Intf del VRF %u IF %s", ifp
->vrf_id
, ifp
->name
);
217 bgp_update_interface_nbrs(bgp
, ifp
, NULL
);
219 bgp_mac_del_mac_entry(ifp
);
224 static int bgp_ifp_up(struct interface
*ifp
)
227 struct nbr_connected
*nc
;
228 struct listnode
*node
, *nnode
;
231 bgp
= bgp_lookup_by_vrf_id(ifp
->vrf_id
);
233 bgp_mac_add_mac_entry(ifp
);
235 if (BGP_DEBUG(zebra
, ZEBRA
))
236 zlog_debug("Rx Intf up VRF %u IF %s", ifp
->vrf_id
, ifp
->name
);
241 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
242 bgp_connected_add(bgp
, c
);
244 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
245 bgp_nbr_connected_add(bgp
, nc
);
250 static int bgp_ifp_down(struct interface
*ifp
)
253 struct nbr_connected
*nc
;
254 struct listnode
*node
, *nnode
;
258 bgp
= bgp_lookup_by_vrf_id(ifp
->vrf_id
);
260 bgp_mac_del_mac_entry(ifp
);
262 if (BGP_DEBUG(zebra
, ZEBRA
))
263 zlog_debug("Rx Intf down VRF %u IF %s", ifp
->vrf_id
, ifp
->name
);
268 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
269 bgp_connected_delete(bgp
, c
);
271 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
272 bgp_nbr_connected_delete(bgp
, nc
, 1);
274 /* Fast external-failover */
275 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
)) {
277 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
278 #if defined(HAVE_CUMULUS)
279 /* Take down directly connected EBGP peers as well as
281 * tracked (directly connected) IBGP peers.
283 if ((peer
->ttl
!= BGP_DEFAULT_TTL
)
284 && (peer
->gtsm_hops
!= BGP_GTSM_HOPS_CONNECTED
)
286 || bgp_bfd_is_peer_multihop(peer
)))
288 /* Take down directly connected EBGP peers */
289 if ((peer
->ttl
!= BGP_DEFAULT_TTL
)
290 && (peer
->gtsm_hops
!= BGP_GTSM_HOPS_CONNECTED
))
294 if (ifp
== peer
->nexthop
.ifp
) {
295 BGP_EVENT_ADD(peer
, BGP_Stop
);
296 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
304 static int bgp_interface_address_add(ZAPI_CALLBACK_ARGS
)
306 struct connected
*ifc
;
309 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
311 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
316 if (bgp_debug_zebra(ifc
->address
)) {
317 char buf
[PREFIX2STR_BUFFER
];
318 prefix2str(ifc
->address
, buf
, sizeof(buf
));
319 zlog_debug("Rx Intf address add VRF %u IF %s addr %s", vrf_id
,
320 ifc
->ifp
->name
, buf
);
326 if (if_is_operative(ifc
->ifp
)) {
327 bgp_connected_add(bgp
, ifc
);
329 /* If we have learnt of any neighbors on this interface,
330 * check to kick off any BGP interface-based neighbors,
331 * but only if this is a link-local address.
333 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
)
334 && !list_isempty(ifc
->ifp
->nbr_connected
))
335 bgp_start_interface_nbrs(bgp
, ifc
->ifp
);
341 static int bgp_interface_address_delete(ZAPI_CALLBACK_ARGS
)
343 struct connected
*ifc
;
346 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
348 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
353 if (bgp_debug_zebra(ifc
->address
)) {
354 char buf
[PREFIX2STR_BUFFER
];
355 prefix2str(ifc
->address
, buf
, sizeof(buf
));
356 zlog_debug("Rx Intf address del VRF %u IF %s addr %s", vrf_id
,
357 ifc
->ifp
->name
, buf
);
360 if (bgp
&& if_is_operative(ifc
->ifp
)) {
361 bgp_connected_delete(bgp
, ifc
);
364 connected_free(&ifc
);
369 static int bgp_interface_nbr_address_add(ZAPI_CALLBACK_ARGS
)
371 struct nbr_connected
*ifc
= NULL
;
374 ifc
= zebra_interface_nbr_address_read(cmd
, zclient
->ibuf
, vrf_id
);
379 if (bgp_debug_zebra(ifc
->address
)) {
380 char buf
[PREFIX2STR_BUFFER
];
381 prefix2str(ifc
->address
, buf
, sizeof(buf
));
382 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s", vrf_id
,
383 ifc
->ifp
->name
, buf
);
386 if (if_is_operative(ifc
->ifp
)) {
387 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
389 bgp_nbr_connected_add(bgp
, ifc
);
395 static int bgp_interface_nbr_address_delete(ZAPI_CALLBACK_ARGS
)
397 struct nbr_connected
*ifc
= NULL
;
400 ifc
= zebra_interface_nbr_address_read(cmd
, zclient
->ibuf
, vrf_id
);
405 if (bgp_debug_zebra(ifc
->address
)) {
406 char buf
[PREFIX2STR_BUFFER
];
407 prefix2str(ifc
->address
, buf
, sizeof(buf
));
408 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s", vrf_id
,
409 ifc
->ifp
->name
, buf
);
412 if (if_is_operative(ifc
->ifp
)) {
413 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
415 bgp_nbr_connected_delete(bgp
, ifc
, 0);
418 nbr_connected_free(ifc
);
423 /* VRF update for an interface. */
424 static int bgp_interface_vrf_update(ZAPI_CALLBACK_ARGS
)
426 struct interface
*ifp
;
429 struct nbr_connected
*nc
;
430 struct listnode
*node
, *nnode
;
434 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
439 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
440 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id
,
441 ifp
->name
, new_vrf_id
);
443 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
446 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
447 bgp_connected_delete(bgp
, c
);
449 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
450 bgp_nbr_connected_delete(bgp
, nc
, 1);
452 /* Fast external-failover */
453 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
)) {
454 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
455 if ((peer
->ttl
!= BGP_DEFAULT_TTL
)
457 != BGP_GTSM_HOPS_CONNECTED
))
460 if (ifp
== peer
->nexthop
.ifp
)
461 BGP_EVENT_ADD(peer
, BGP_Stop
);
466 if_update_to_new_vrf(ifp
, new_vrf_id
);
468 bgp
= bgp_lookup_by_vrf_id(new_vrf_id
);
472 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
473 bgp_connected_add(bgp
, c
);
475 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
476 bgp_nbr_connected_add(bgp
, nc
);
480 /* Zebra route add and delete treatment. */
481 static int zebra_read_route(ZAPI_CALLBACK_ARGS
)
483 enum nexthop_types_t nhtype
;
484 struct zapi_route api
;
485 union g_addr nexthop
;
490 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
494 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
497 /* we completely ignore srcdest routes for now. */
498 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
501 /* ignore link-local address. */
502 if (api
.prefix
.family
== AF_INET6
503 && IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
506 nexthop
= api
.nexthops
[0].gate
;
507 ifindex
= api
.nexthops
[0].ifindex
;
508 nhtype
= api
.nexthops
[0].type
;
510 add
= (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
513 * The ADD message is actually an UPDATE and there is no
515 * for a prior redistributed route, if any. So, perform an
517 * DEL processing for the same redistributed route from any
521 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
523 bgp_redistribute_delete(bgp
, &api
.prefix
, i
,
527 /* Now perform the add/update. */
528 bgp_redistribute_add(bgp
, &api
.prefix
, &nexthop
, ifindex
,
529 nhtype
, api
.metric
, api
.type
, api
.instance
,
532 bgp_redistribute_delete(bgp
, &api
.prefix
, api
.type
,
536 if (bgp_debug_zebra(&api
.prefix
)) {
537 char buf
[2][PREFIX_STRLEN
];
539 prefix2str(&api
.prefix
, buf
[0], sizeof(buf
[0]));
541 inet_ntop(api
.prefix
.family
, &nexthop
, buf
[1],
544 "Rx route ADD VRF %u %s[%d] %s nexthop %s (type %d if %u) metric %u tag %" ROUTE_TAG_PRI
,
545 vrf_id
, zebra_route_string(api
.type
),
546 api
.instance
, buf
[0], buf
[1], nhtype
,
547 ifindex
, api
.metric
, api
.tag
);
550 "Rx route DEL VRF %u %s[%d] %s",
551 vrf_id
, zebra_route_string(api
.type
),
552 api
.instance
, buf
[0]);
559 struct interface
*if_lookup_by_ipv4(struct in_addr
*addr
, vrf_id_t vrf_id
)
562 struct listnode
*cnode
;
563 struct interface
*ifp
;
564 struct connected
*connected
;
565 struct prefix_ipv4 p
;
568 vrf
= vrf_lookup_by_id(vrf_id
);
574 p
.prefixlen
= IPV4_MAX_BITLEN
;
576 FOR_ALL_INTERFACES (vrf
, ifp
) {
577 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
578 cp
= connected
->address
;
580 if (cp
->family
== AF_INET
)
581 if (prefix_match(cp
, (struct prefix
*)&p
))
588 struct interface
*if_lookup_by_ipv4_exact(struct in_addr
*addr
, vrf_id_t vrf_id
)
591 struct listnode
*cnode
;
592 struct interface
*ifp
;
593 struct connected
*connected
;
596 vrf
= vrf_lookup_by_id(vrf_id
);
600 FOR_ALL_INTERFACES (vrf
, ifp
) {
601 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
602 cp
= connected
->address
;
604 if (cp
->family
== AF_INET
)
605 if (IPV4_ADDR_SAME(&cp
->u
.prefix4
, addr
))
612 struct interface
*if_lookup_by_ipv6(struct in6_addr
*addr
, ifindex_t ifindex
,
616 struct listnode
*cnode
;
617 struct interface
*ifp
;
618 struct connected
*connected
;
619 struct prefix_ipv6 p
;
622 vrf
= vrf_lookup_by_id(vrf_id
);
628 p
.prefixlen
= IPV6_MAX_BITLEN
;
630 FOR_ALL_INTERFACES (vrf
, ifp
) {
631 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
632 cp
= connected
->address
;
634 if (cp
->family
== AF_INET6
)
635 if (prefix_match(cp
, (struct prefix
*)&p
)) {
636 if (IN6_IS_ADDR_LINKLOCAL(
638 if (ifindex
== ifp
->ifindex
)
648 struct interface
*if_lookup_by_ipv6_exact(struct in6_addr
*addr
,
649 ifindex_t ifindex
, vrf_id_t vrf_id
)
652 struct listnode
*cnode
;
653 struct interface
*ifp
;
654 struct connected
*connected
;
657 vrf
= vrf_lookup_by_id(vrf_id
);
661 FOR_ALL_INTERFACES (vrf
, ifp
) {
662 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
663 cp
= connected
->address
;
665 if (cp
->family
== AF_INET6
)
666 if (IPV6_ADDR_SAME(&cp
->u
.prefix6
, addr
)) {
667 if (IN6_IS_ADDR_LINKLOCAL(
669 if (ifindex
== ifp
->ifindex
)
679 static int if_get_ipv6_global(struct interface
*ifp
, struct in6_addr
*addr
)
681 struct listnode
*cnode
;
682 struct connected
*connected
;
685 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
686 cp
= connected
->address
;
688 if (cp
->family
== AF_INET6
)
689 if (!IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
690 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
697 static int if_get_ipv6_local(struct interface
*ifp
, struct in6_addr
*addr
)
699 struct listnode
*cnode
;
700 struct connected
*connected
;
703 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
704 cp
= connected
->address
;
706 if (cp
->family
== AF_INET6
)
707 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
708 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
715 static int if_get_ipv4_address(struct interface
*ifp
, struct in_addr
*addr
)
717 struct listnode
*cnode
;
718 struct connected
*connected
;
721 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
722 cp
= connected
->address
;
723 if ((cp
->family
== AF_INET
)
724 && !ipv4_martian(&(cp
->u
.prefix4
))) {
725 *addr
= cp
->u
.prefix4
;
733 bool bgp_zebra_nexthop_set(union sockunion
*local
, union sockunion
*remote
,
734 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
737 struct interface
*ifp
= NULL
;
739 memset(nexthop
, 0, sizeof(struct bgp_nexthop
));
746 if (local
->sa
.sa_family
== AF_INET
) {
747 nexthop
->v4
= local
->sin
.sin_addr
;
749 ifp
= if_lookup_by_name(peer
->update_if
,
752 ifp
= if_lookup_by_ipv4_exact(&local
->sin
.sin_addr
,
755 if (local
->sa
.sa_family
== AF_INET6
) {
756 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
, IPV6_MAX_BYTELEN
);
757 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
758 if (peer
->conf_if
|| peer
->ifname
)
759 ifp
= if_lookup_by_name(peer
->conf_if
763 } else if (peer
->update_if
)
764 ifp
= if_lookup_by_name(peer
->update_if
,
767 ifp
= if_lookup_by_ipv6_exact(&local
->sin6
.sin6_addr
,
768 local
->sin6
.sin6_scope_id
,
774 * BGP views do not currently get proper data
775 * from zebra( when attached ) to be able to
776 * properly resolve nexthops, so give this
777 * instance type a pass.
779 if (peer
->bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
782 * If we have no interface data but we have established
783 * some connection w/ zebra than something has gone
784 * terribly terribly wrong here, so say this failed
785 * If we do not any zebra connection then not
786 * having a ifp pointer is ok.
788 return zclient_num_connects
? false : true;
793 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
794 if (local
->sa
.sa_family
== AF_INET
) {
796 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
799 /* There is no global nexthop. Use link-local address as
801 * global and link-local nexthop. In this scenario, the
803 * for interop is that the network admin would use a
805 * specify the global IPv6 nexthop.
807 if_get_ipv6_local(ifp
, &nexthop
->v6_global
);
808 memcpy(&nexthop
->v6_local
, &nexthop
->v6_global
,
811 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
813 if (if_lookup_by_ipv4(&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
814 peer
->shared_network
= 1;
816 peer
->shared_network
= 0;
819 /* IPv6 connection, fetch and store IPv4 local address if any. */
820 if (local
->sa
.sa_family
== AF_INET6
) {
821 struct interface
*direct
= NULL
;
824 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
825 if (!ret
&& peer
->local_id
.s_addr
!= INADDR_ANY
)
826 nexthop
->v4
= peer
->local_id
;
829 if (!IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
830 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
833 /* If directory connected set link-local address. */
834 direct
= if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
835 remote
->sin6
.sin6_scope_id
,
838 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
840 /* Link-local address. */
842 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
844 /* If there is no global address. Set link-local
846 global. I know this break RFC specification... */
847 /* In this scenario, the expectation for interop is that
849 * network admin would use a route-map to specify the
854 memcpy(&nexthop
->v6_global
,
855 &local
->sin6
.sin6_addr
,
857 /* Always set the link-local address */
858 memcpy(&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
862 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)
863 || if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
864 remote
->sin6
.sin6_scope_id
,
866 peer
->shared_network
= 1;
868 peer
->shared_network
= 0;
871 /* KAME stack specific treatment. */
873 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_global
)
874 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
)) {
875 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
, 0);
877 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_local
)
878 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
)) {
879 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
, 0);
883 /* If we have identified the local interface, there is no error for now.
888 static struct in6_addr
*
889 bgp_path_info_to_ipv6_nexthop(struct bgp_path_info
*path
, ifindex_t
*ifindex
)
891 struct in6_addr
*nexthop
= NULL
;
893 /* Only global address nexthop exists. */
894 if (path
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
895 || path
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV6_GLOBAL
) {
896 nexthop
= &path
->attr
->mp_nexthop_global
;
897 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
898 *ifindex
= path
->attr
->nh_ifindex
;
901 /* If both global and link-local address present. */
902 if (path
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
903 || path
->attr
->mp_nexthop_len
904 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
905 /* Check if route-map is set to prefer global over link-local */
906 if (path
->attr
->mp_nexthop_prefer_global
) {
907 nexthop
= &path
->attr
->mp_nexthop_global
;
908 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
909 *ifindex
= path
->attr
->nh_ifindex
;
911 /* Workaround for Cisco's nexthop bug. */
912 if (IN6_IS_ADDR_UNSPECIFIED(
913 &path
->attr
->mp_nexthop_global
)
914 && path
->peer
->su_remote
->sa
.sa_family
917 &path
->peer
->su_remote
->sin6
.sin6_addr
;
918 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
919 *ifindex
= path
->peer
->nexthop
.ifp
922 nexthop
= &path
->attr
->mp_nexthop_local
;
923 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
924 *ifindex
= path
->attr
->nh_lla_ifindex
;
932 static bool bgp_table_map_apply(struct route_map
*map
, const struct prefix
*p
,
933 struct bgp_path_info
*path
)
935 route_map_result_t ret
;
937 ret
= route_map_apply(map
, p
, RMAP_BGP
, path
);
938 bgp_attr_flush(path
->attr
);
940 if (ret
!= RMAP_DENYMATCH
)
943 if (bgp_debug_zebra(p
)) {
944 if (p
->family
== AF_INET
) {
945 char buf
[2][INET_ADDRSTRLEN
];
947 "Zebra rmap deny: IPv4 route %pFX nexthop %s",
949 inet_ntop(AF_INET
, &path
->attr
->nexthop
, buf
[1],
952 if (p
->family
== AF_INET6
) {
953 char buf
[2][INET6_ADDRSTRLEN
];
955 struct in6_addr
*nexthop
;
957 nexthop
= bgp_path_info_to_ipv6_nexthop(path
, &ifindex
);
959 "Zebra rmap deny: IPv6 route %pFX nexthop %s",
961 inet_ntop(AF_INET6
, nexthop
, buf
[1],
968 static struct thread
*bgp_tm_thread_connect
;
969 static bool bgp_tm_status_connected
;
970 static bool bgp_tm_chunk_obtained
;
971 #define BGP_FLOWSPEC_TABLE_CHUNK 100000
972 static uint32_t bgp_tm_min
, bgp_tm_max
, bgp_tm_chunk_size
;
973 struct bgp
*bgp_tm_bgp
;
975 static int bgp_zebra_tm_connect(struct thread
*t
)
977 struct zclient
*zclient
;
978 int delay
= 10, ret
= 0;
980 zclient
= THREAD_ARG(t
);
981 if (bgp_tm_status_connected
&& zclient
->sock
> 0)
984 bgp_tm_status_connected
= false;
985 ret
= tm_table_manager_connect(zclient
);
988 zlog_info("Error connecting to table manager!");
989 bgp_tm_status_connected
= false;
991 if (!bgp_tm_status_connected
)
992 zlog_debug("Connecting to table manager. Success");
993 bgp_tm_status_connected
= true;
994 if (!bgp_tm_chunk_obtained
) {
995 if (bgp_zebra_get_table_range(bgp_tm_chunk_size
,
998 bgp_tm_chunk_obtained
= true;
999 /* parse non installed entries */
1000 bgp_zebra_announce_table(bgp_tm_bgp
, AFI_IP
, SAFI_FLOWSPEC
);
1004 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1005 &bgp_tm_thread_connect
);
1009 bool bgp_zebra_tm_chunk_obtained(void)
1011 return bgp_tm_chunk_obtained
;
1014 uint32_t bgp_zebra_tm_get_id(void)
1016 static int table_id
;
1018 if (!bgp_tm_chunk_obtained
)
1020 return bgp_tm_min
++;
1023 void bgp_zebra_init_tm_connect(struct bgp
*bgp
)
1027 /* if already set, do nothing
1029 if (bgp_tm_thread_connect
!= NULL
)
1031 bgp_tm_status_connected
= false;
1032 bgp_tm_chunk_obtained
= false;
1033 bgp_tm_min
= bgp_tm_max
= 0;
1034 bgp_tm_chunk_size
= BGP_FLOWSPEC_TABLE_CHUNK
;
1036 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1037 &bgp_tm_thread_connect
);
1040 int bgp_zebra_get_table_range(uint32_t chunk_size
,
1041 uint32_t *start
, uint32_t *end
)
1045 if (!bgp_tm_status_connected
)
1047 ret
= tm_get_table_chunk(zclient
, chunk_size
, start
, end
);
1049 flog_err(EC_BGP_TABLE_CHUNK
,
1050 "BGP: Error getting table chunk %u", chunk_size
);
1053 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1054 chunk_size
, *start
, *end
);
1058 static bool update_ipv4nh_for_route_install(int nh_othervrf
, struct bgp
*nh_bgp
,
1059 struct in_addr
*nexthop
,
1060 struct attr
*attr
, bool is_evpn
,
1061 struct zapi_nexthop
*api_nh
)
1063 api_nh
->gate
.ipv4
= *nexthop
;
1064 api_nh
->vrf_id
= nh_bgp
->vrf_id
;
1066 /* Need to set fields appropriately for EVPN routes imported into
1067 * a VRF (which are programmed as onlink on l3-vni SVI) as well as
1068 * connected routes leaked into a VRF.
1071 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1072 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
);
1073 api_nh
->ifindex
= nh_bgp
->l3vni_svi_ifindex
;
1074 } else if (nh_othervrf
&&
1075 api_nh
->gate
.ipv4
.s_addr
== INADDR_ANY
) {
1076 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1077 api_nh
->ifindex
= attr
->nh_ifindex
;
1079 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1084 static bool update_ipv6nh_for_route_install(int nh_othervrf
, struct bgp
*nh_bgp
,
1085 struct in6_addr
*nexthop
,
1087 struct bgp_path_info
*pi
,
1088 struct bgp_path_info
*best_pi
,
1090 struct zapi_nexthop
*api_nh
)
1095 api_nh
->vrf_id
= nh_bgp
->vrf_id
;
1098 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1099 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
);
1100 api_nh
->ifindex
= nh_bgp
->l3vni_svi_ifindex
;
1101 } else if (nh_othervrf
) {
1102 if (IN6_IS_ADDR_UNSPECIFIED(nexthop
)) {
1103 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1104 api_nh
->ifindex
= attr
->nh_ifindex
;
1105 } else if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1108 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1109 api_nh
->ifindex
= ifindex
;
1111 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1112 api_nh
->ifindex
= 0;
1115 if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1117 && attr
->mp_nexthop_len
1118 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1119 if (pi
->peer
->nexthop
.ifp
)
1121 pi
->peer
->nexthop
.ifp
->ifindex
;
1123 if (pi
->peer
->conf_if
)
1124 ifindex
= pi
->peer
->ifp
->ifindex
;
1125 else if (pi
->peer
->ifname
)
1126 ifindex
= ifname2ifindex(
1128 pi
->peer
->bgp
->vrf_id
);
1129 else if (pi
->peer
->nexthop
.ifp
)
1131 pi
->peer
->nexthop
.ifp
->ifindex
;
1136 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1137 api_nh
->ifindex
= ifindex
;
1139 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1140 api_nh
->ifindex
= 0;
1144 api_nh
->gate
.ipv6
= *nexthop
;
1149 static bool bgp_zebra_use_nhop_weighted(struct bgp
*bgp
, struct attr
*attr
,
1150 uint64_t tot_bw
, uint32_t *nh_weight
)
1156 /* zero link-bandwidth and link-bandwidth not present are treated
1157 * as the same situation.
1160 /* the only situations should be if we're either told
1161 * to skip or use default weight.
1163 if (bgp
->lb_handling
== BGP_LINK_BW_SKIP_MISSING
)
1165 *nh_weight
= BGP_ZEBRA_DEFAULT_NHOP_WEIGHT
;
1167 tmp
= (uint64_t)bw
* 100;
1168 *nh_weight
= ((uint32_t)(tmp
/ tot_bw
));
1174 void bgp_zebra_announce(struct bgp_dest
*dest
, const struct prefix
*p
,
1175 struct bgp_path_info
*info
, struct bgp
*bgp
, afi_t afi
,
1178 struct zapi_route api
= { 0 };
1179 struct zapi_nexthop
*api_nh
;
1181 unsigned int valid_nh_count
= 0;
1182 int has_valid_label
= 0;
1185 struct bgp_path_info
*mpinfo
;
1187 struct attr local_attr
;
1188 struct bgp_path_info local_info
;
1189 struct bgp_path_info
*mpinfo_cp
= &local_info
;
1192 int nh_othervrf
= 0;
1193 char buf_prefix
[PREFIX_STRLEN
]; /* filled in if we are debugging */
1197 uint64_t cum_bw
= 0;
1199 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1200 * know of this instance.
1202 if (!bgp_install_info_to_zebra(bgp
))
1205 if (bgp
->main_zebra_update_hold
)
1208 if (bgp_debug_zebra(p
))
1209 prefix2str(p
, buf_prefix
, sizeof(buf_prefix
));
1211 if (safi
== SAFI_FLOWSPEC
) {
1212 bgp_pbr_update_entry(bgp
, bgp_dest_get_prefix(dest
), info
, afi
,
1218 * vrf leaking support (will have only one nexthop)
1220 if (info
->extra
&& info
->extra
->bgp_orig
)
1223 /* Make Zebra API structure. */
1224 api
.vrf_id
= bgp
->vrf_id
;
1225 api
.type
= ZEBRA_ROUTE_BGP
;
1228 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1232 if (info
->type
== ZEBRA_ROUTE_BGP
1233 && info
->sub_type
== BGP_ROUTE_IMPORTED
) {
1235 /* Obtain peer from parent */
1236 if (info
->extra
&& info
->extra
->parent
)
1237 peer
= ((struct bgp_path_info
*)(info
->extra
->parent
))
1241 tag
= info
->attr
->tag
;
1243 /* If the route's source is EVPN, flag as such. */
1244 is_evpn
= is_route_parent_evpn(info
);
1246 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1248 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
1249 || info
->sub_type
== BGP_ROUTE_AGGREGATE
) {
1250 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1251 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1254 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= BGP_DEFAULT_TTL
)
1255 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1256 || CHECK_FLAG(bgp
->flags
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1258 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1260 if (info
->attr
->rmap_table_id
) {
1261 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
1262 api
.tableid
= info
->attr
->rmap_table_id
;
1265 if (CHECK_FLAG(info
->attr
->flag
, ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR
)))
1266 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRTE
);
1268 /* Metric is currently based on the best-path only */
1269 metric
= info
->attr
->med
;
1271 /* Determine if we're doing weighted ECMP or not */
1272 do_wt_ecmp
= bgp_path_info_mpath_chkwtd(bgp
, info
);
1274 cum_bw
= bgp_path_info_mpath_cumbw(info
);
1276 for (mpinfo
= info
; mpinfo
; mpinfo
= bgp_path_info_mpath_next(mpinfo
)) {
1279 if (valid_nh_count
>= multipath_num
)
1282 *mpinfo_cp
= *mpinfo
;
1285 /* Get nexthop address-family */
1286 if (p
->family
== AF_INET
1287 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
))
1288 nh_family
= AF_INET
;
1289 else if (p
->family
== AF_INET6
1290 || (p
->family
== AF_INET
1291 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
)))
1292 nh_family
= AF_INET6
;
1296 /* If processing for weighted ECMP, determine the next hop's
1297 * weight. Based on user setting, we may skip the next hop
1298 * in some situations.
1301 if (!bgp_zebra_use_nhop_weighted(bgp
, mpinfo
->attr
,
1302 cum_bw
, &nh_weight
))
1305 api_nh
= &api
.nexthops
[valid_nh_count
];
1307 if (CHECK_FLAG(info
->attr
->flag
,
1308 ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR
)))
1309 api_nh
->srte_color
= info
->attr
->srte_color
;
1311 if (nh_family
== AF_INET
) {
1312 if (bgp_debug_zebra(&api
.prefix
)) {
1313 if (mpinfo
->extra
) {
1315 "%s: p=%s, bgp_is_valid_label: %d",
1316 __func__
, buf_prefix
,
1322 "%s: p=%s, extra is NULL, no label",
1323 __func__
, buf_prefix
);
1327 if (bgp
->table_map
[afi
][safi
].name
) {
1328 /* Copy info and attributes, so the route-map
1329 apply doesn't modify the BGP route info. */
1330 local_attr
= *mpinfo
->attr
;
1331 mpinfo_cp
->attr
= &local_attr
;
1334 if (bgp
->table_map
[afi
][safi
].name
) {
1335 if (!bgp_table_map_apply(
1336 bgp
->table_map
[afi
][safi
].map
, p
,
1340 /* metric/tag is only allowed to be
1341 * overridden on 1st nexthop */
1342 if (mpinfo
== info
) {
1343 metric
= mpinfo_cp
->attr
->med
;
1344 tag
= mpinfo_cp
->attr
->tag
;
1348 nh_updated
= update_ipv4nh_for_route_install(
1351 info
->extra
->bgp_orig
: bgp
,
1352 &mpinfo_cp
->attr
->nexthop
,
1353 mpinfo_cp
->attr
, is_evpn
, api_nh
);
1355 ifindex_t ifindex
= IFINDEX_INTERNAL
;
1356 struct in6_addr
*nexthop
;
1358 if (bgp
->table_map
[afi
][safi
].name
) {
1359 /* Copy info and attributes, so the route-map
1360 apply doesn't modify the BGP route info. */
1361 local_attr
= *mpinfo
->attr
;
1362 mpinfo_cp
->attr
= &local_attr
;
1365 if (bgp
->table_map
[afi
][safi
].name
) {
1366 /* Copy info and attributes, so the route-map
1367 apply doesn't modify the BGP route info. */
1368 local_attr
= *mpinfo
->attr
;
1369 mpinfo_cp
->attr
= &local_attr
;
1371 if (!bgp_table_map_apply(
1372 bgp
->table_map
[afi
][safi
].map
, p
,
1376 /* metric/tag is only allowed to be
1377 * overridden on 1st nexthop */
1378 if (mpinfo
== info
) {
1379 metric
= mpinfo_cp
->attr
->med
;
1380 tag
= mpinfo_cp
->attr
->tag
;
1383 nexthop
= bgp_path_info_to_ipv6_nexthop(mpinfo_cp
,
1385 nh_updated
= update_ipv6nh_for_route_install(
1386 nh_othervrf
, nh_othervrf
?
1387 info
->extra
->bgp_orig
: bgp
,
1389 mpinfo
, info
, is_evpn
, api_nh
);
1392 /* Did we get proper nexthop info to update zebra? */
1397 && bgp_is_valid_label(&mpinfo
->extra
->label
[0])
1398 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1399 has_valid_label
= 1;
1400 label
= label_pton(&mpinfo
->extra
->label
[0]);
1402 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
);
1404 api_nh
->label_num
= 1;
1405 api_nh
->labels
[0] = label
;
1407 memcpy(&api_nh
->rmac
, &(mpinfo
->attr
->rmac
),
1408 sizeof(struct ethaddr
));
1409 api_nh
->weight
= nh_weight
;
1415 * When we create an aggregate route we must also
1416 * install a Null0 route in the RIB, so overwrite
1417 * what was written into api with a blackhole route
1419 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1420 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
1422 api
.nexthop_num
= valid_nh_count
;
1424 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1425 api
.metric
= metric
;
1428 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1432 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1434 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1435 api
.distance
= distance
;
1438 if (bgp_debug_zebra(p
)) {
1439 char prefix_buf
[PREFIX_STRLEN
];
1440 char nh_buf
[INET6_ADDRSTRLEN
];
1441 char eth_buf
[ETHER_ADDR_STRLEN
+ 7] = {'\0'};
1442 char buf1
[ETHER_ADDR_STRLEN
];
1446 prefix2str(&api
.prefix
, prefix_buf
, sizeof(prefix_buf
));
1447 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1449 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1450 prefix_buf
, api
.metric
, api
.tag
, api
.nexthop_num
);
1451 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1452 api_nh
= &api
.nexthops
[i
];
1454 switch (api_nh
->type
) {
1455 case NEXTHOP_TYPE_IFINDEX
:
1458 case NEXTHOP_TYPE_IPV4
:
1459 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1460 nh_family
= AF_INET
;
1461 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1464 case NEXTHOP_TYPE_IPV6
:
1465 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1466 nh_family
= AF_INET6
;
1467 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1470 case NEXTHOP_TYPE_BLACKHOLE
:
1471 strlcpy(nh_buf
, "blackhole", sizeof(nh_buf
));
1474 /* Note: add new nexthop case */
1479 label_buf
[0] = '\0';
1482 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1483 snprintf(label_buf
, sizeof(label_buf
),
1484 "label %u", api_nh
->labels
[0]);
1485 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)
1486 && !is_zero_mac(&api_nh
->rmac
))
1487 snprintf(eth_buf
, sizeof(eth_buf
), " RMAC %s",
1488 prefix_mac2str(&api_nh
->rmac
,
1489 buf1
, sizeof(buf1
)));
1490 zlog_debug(" nhop [%d]: %s if %u VRF %u wt %u %s %s",
1491 i
+ 1, nh_buf
, api_nh
->ifindex
,
1492 api_nh
->vrf_id
, api_nh
->weight
,
1493 label_buf
, eth_buf
);
1497 if (bgp_debug_zebra(p
)) {
1498 int recursion_flag
= 0;
1500 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
))
1503 zlog_debug("%s: %s: announcing to zebra (recursion %sset)",
1504 __func__
, buf_prefix
,
1505 (recursion_flag
? "" : "NOT "));
1507 zclient_route_send(valid_nh_count
? ZEBRA_ROUTE_ADD
1508 : ZEBRA_ROUTE_DELETE
,
1512 /* Announce all routes of a table to zebra */
1513 void bgp_zebra_announce_table(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1515 struct bgp_dest
*dest
;
1516 struct bgp_table
*table
;
1517 struct bgp_path_info
*pi
;
1519 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1520 * know of this instance.
1522 if (!bgp_install_info_to_zebra(bgp
))
1525 table
= bgp
->rib
[afi
][safi
];
1529 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
))
1530 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
1531 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
) &&
1533 (pi
->type
== ZEBRA_ROUTE_BGP
1534 && (pi
->sub_type
== BGP_ROUTE_NORMAL
1535 || pi
->sub_type
== BGP_ROUTE_IMPORTED
)))
1537 bgp_zebra_announce(dest
,
1538 bgp_dest_get_prefix(dest
),
1539 pi
, bgp
, afi
, safi
);
1542 /* Announce routes of any bgp subtype of a table to zebra */
1543 void bgp_zebra_announce_table_all_subtypes(struct bgp
*bgp
, afi_t afi
,
1546 struct bgp_dest
*dest
;
1547 struct bgp_table
*table
;
1548 struct bgp_path_info
*pi
;
1550 if (!bgp_install_info_to_zebra(bgp
))
1553 table
= bgp
->rib
[afi
][safi
];
1557 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
))
1558 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
1559 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
) &&
1560 pi
->type
== ZEBRA_ROUTE_BGP
)
1561 bgp_zebra_announce(dest
,
1562 bgp_dest_get_prefix(dest
),
1563 pi
, bgp
, afi
, safi
);
1566 void bgp_zebra_withdraw(const struct prefix
*p
, struct bgp_path_info
*info
,
1567 struct bgp
*bgp
, safi_t safi
)
1569 struct zapi_route api
;
1572 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1573 * know of this instance.
1575 if (!bgp_install_info_to_zebra(bgp
))
1578 if (safi
== SAFI_FLOWSPEC
) {
1580 bgp_pbr_update_entry(peer
->bgp
, p
, info
, AFI_IP
, safi
, false);
1584 memset(&api
, 0, sizeof(api
));
1585 api
.vrf_id
= bgp
->vrf_id
;
1586 api
.type
= ZEBRA_ROUTE_BGP
;
1590 if (info
->attr
->rmap_table_id
) {
1591 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
1592 api
.tableid
= info
->attr
->rmap_table_id
;
1595 /* If the route's source is EVPN, flag as such. */
1596 if (is_route_parent_evpn(info
))
1597 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1599 if (bgp_debug_zebra(p
)) {
1600 char buf
[PREFIX_STRLEN
];
1602 prefix2str(&api
.prefix
, buf
, sizeof(buf
));
1603 zlog_debug("Tx route delete VRF %u %s", bgp
->vrf_id
, buf
);
1606 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
1609 /* Withdraw all entries in a BGP instances RIB table from Zebra */
1610 void bgp_zebra_withdraw_table_all_subtypes(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1612 struct bgp_dest
*dest
;
1613 struct bgp_table
*table
;
1614 struct bgp_path_info
*pi
;
1616 if (!bgp_install_info_to_zebra(bgp
))
1619 table
= bgp
->rib
[afi
][safi
];
1623 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
1624 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
1625 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
1626 && (pi
->type
== ZEBRA_ROUTE_BGP
))
1627 bgp_zebra_withdraw(bgp_dest_get_prefix(dest
),
1633 struct bgp_redist
*bgp_redist_lookup(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1634 unsigned short instance
)
1636 struct list
*red_list
;
1637 struct listnode
*node
;
1638 struct bgp_redist
*red
;
1640 red_list
= bgp
->redist
[afi
][type
];
1644 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1645 if (red
->instance
== instance
)
1651 struct bgp_redist
*bgp_redist_add(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1652 unsigned short instance
)
1654 struct list
*red_list
;
1655 struct bgp_redist
*red
;
1657 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1661 if (!bgp
->redist
[afi
][type
])
1662 bgp
->redist
[afi
][type
] = list_new();
1664 red_list
= bgp
->redist
[afi
][type
];
1665 red
= XCALLOC(MTYPE_BGP_REDIST
, sizeof(struct bgp_redist
));
1666 red
->instance
= instance
;
1668 listnode_add(red_list
, red
);
1673 static void bgp_redist_del(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1674 unsigned short instance
)
1676 struct bgp_redist
*red
;
1678 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1681 listnode_delete(bgp
->redist
[afi
][type
], red
);
1682 XFREE(MTYPE_BGP_REDIST
, red
);
1683 if (!bgp
->redist
[afi
][type
]->count
)
1684 list_delete(&bgp
->redist
[afi
][type
]);
1688 /* Other routes redistribution into BGP. */
1689 int bgp_redistribute_set(struct bgp
*bgp
, afi_t afi
, int type
,
1690 unsigned short instance
, bool changed
)
1692 /* If redistribute options are changed call
1693 * bgp_redistribute_unreg() to reset the option and withdraw
1697 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1699 /* Return if already redistribute flag is set. */
1701 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1705 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1707 if (vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1710 #ifdef ENABLE_BGP_VNC
1711 if (EVPN_ENABLED(bgp
) && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1712 vnc_export_bgp_enable(
1713 bgp
, afi
); /* only enables if mode bits cfg'd */
1717 vrf_bitmap_set(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1721 * Don't try to register if we're not connected to Zebra or Zebra
1722 * doesn't know of this instance.
1724 * When we come up later well resend if needed.
1726 if (!bgp_install_info_to_zebra(bgp
))
1729 if (BGP_DEBUG(zebra
, ZEBRA
))
1730 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1731 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1734 /* Send distribute add message to zebra. */
1735 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1736 instance
, bgp
->vrf_id
);
1741 int bgp_redistribute_resend(struct bgp
*bgp
, afi_t afi
, int type
,
1742 unsigned short instance
)
1744 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1745 * know of this instance.
1747 if (!bgp_install_info_to_zebra(bgp
))
1750 if (BGP_DEBUG(zebra
, ZEBRA
))
1751 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1752 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1755 /* Send distribute add message to zebra. */
1756 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1757 instance
, bgp
->vrf_id
);
1758 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1759 instance
, bgp
->vrf_id
);
1764 /* Redistribute with route-map specification. */
1765 bool bgp_redistribute_rmap_set(struct bgp_redist
*red
, const char *name
,
1766 struct route_map
*route_map
)
1768 if (red
->rmap
.name
&& (strcmp(red
->rmap
.name
, name
) == 0))
1771 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1772 /* Decrement the count for existing routemap and
1773 * increment the count for new route map.
1775 route_map_counter_decrement(red
->rmap
.map
);
1776 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1777 red
->rmap
.map
= route_map
;
1778 route_map_counter_increment(red
->rmap
.map
);
1783 /* Redistribute with metric specification. */
1784 bool bgp_redistribute_metric_set(struct bgp
*bgp
, struct bgp_redist
*red
,
1785 afi_t afi
, int type
, uint32_t metric
)
1787 struct bgp_dest
*dest
;
1788 struct bgp_path_info
*pi
;
1790 if (red
->redist_metric_flag
&& red
->redist_metric
== metric
)
1793 red
->redist_metric_flag
= 1;
1794 red
->redist_metric
= metric
;
1796 for (dest
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); dest
;
1797 dest
= bgp_route_next(dest
)) {
1798 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
1799 if (pi
->sub_type
== BGP_ROUTE_REDISTRIBUTE
1801 && pi
->instance
== red
->instance
) {
1802 struct attr
*old_attr
;
1803 struct attr new_attr
;
1805 new_attr
= *pi
->attr
;
1806 new_attr
.med
= red
->redist_metric
;
1807 old_attr
= pi
->attr
;
1808 pi
->attr
= bgp_attr_intern(&new_attr
);
1809 bgp_attr_unintern(&old_attr
);
1811 bgp_path_info_set_flag(dest
, pi
,
1812 BGP_PATH_ATTR_CHANGED
);
1813 bgp_process(bgp
, dest
, afi
, SAFI_UNICAST
);
1821 /* Unset redistribution. */
1822 int bgp_redistribute_unreg(struct bgp
*bgp
, afi_t afi
, int type
,
1823 unsigned short instance
)
1825 struct bgp_redist
*red
;
1827 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1831 /* Return if zebra connection is disabled. */
1833 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1836 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1838 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1840 vrf_bitmap_unset(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1844 if (bgp_install_info_to_zebra(bgp
)) {
1845 /* Send distribute delete message to zebra. */
1846 if (BGP_DEBUG(zebra
, ZEBRA
))
1847 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1848 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1850 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
1851 type
, instance
, bgp
->vrf_id
);
1854 /* Withdraw redistributed routes from current BGP's routing table. */
1855 bgp_redistribute_withdraw(bgp
, afi
, type
, instance
);
1860 /* Unset redistribution. */
1861 int bgp_redistribute_unset(struct bgp
*bgp
, afi_t afi
, int type
,
1862 unsigned short instance
)
1864 struct bgp_redist
*red
;
1867 * vnc and vpn->vrf checks must be before red check because
1868 * they operate within bgpd irrespective of zebra connection
1869 * status. red lookup fails if there is no zebra connection.
1871 #ifdef ENABLE_BGP_VNC
1872 if (EVPN_ENABLED(bgp
) && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1873 vnc_export_bgp_disable(bgp
, afi
);
1877 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1881 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1883 /* Unset route-map. */
1884 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1885 route_map_counter_decrement(red
->rmap
.map
);
1886 red
->rmap
.map
= NULL
;
1889 red
->redist_metric_flag
= 0;
1890 red
->redist_metric
= 0;
1892 bgp_redist_del(bgp
, afi
, type
, instance
);
1897 void bgp_redistribute_redo(struct bgp
*bgp
)
1901 struct list
*red_list
;
1902 struct listnode
*node
;
1903 struct bgp_redist
*red
;
1905 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
1906 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
1908 red_list
= bgp
->redist
[afi
][i
];
1912 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
1913 bgp_redistribute_resend(bgp
, afi
, i
,
1920 /* Unset redistribute vrf bitmap during triggers like
1921 restart networking or delete VRFs */
1922 void bgp_unset_redist_vrf_bitmaps(struct bgp
*bgp
, vrf_id_t old_vrf_id
)
1927 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1928 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1929 if (vrf_bitmap_check(zclient
->redist
[afi
][i
],
1931 vrf_bitmap_unset(zclient
->redist
[afi
][i
],
1936 void bgp_zclient_reset(void)
1938 zclient_reset(zclient
);
1941 /* Register this instance with Zebra. Invoked upon connect (for
1942 * default instance) and when other VRFs are learnt (or created and
1945 void bgp_zebra_instance_register(struct bgp
*bgp
)
1947 /* Don't try to register if we're not connected to Zebra */
1948 if (!zclient
|| zclient
->sock
< 0)
1951 if (BGP_DEBUG(zebra
, ZEBRA
))
1952 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
1954 /* Register for router-id, interfaces, redistributed routes. */
1955 zclient_send_reg_requests(zclient
, bgp
->vrf_id
);
1957 /* For EVPN instance, register to learn about VNIs, if appropriate. */
1958 if (bgp
->advertise_all_vni
)
1959 bgp_zebra_advertise_all_vni(bgp
, 1);
1961 bgp_nht_register_nexthops(bgp
);
1964 /* Deregister this instance with Zebra. Invoked upon the instance
1965 * being deleted (default or VRF) and it is already registered.
1967 void bgp_zebra_instance_deregister(struct bgp
*bgp
)
1969 /* Don't try to deregister if we're not connected to Zebra */
1970 if (zclient
->sock
< 0)
1973 if (BGP_DEBUG(zebra
, ZEBRA
))
1974 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
1976 /* For EVPN instance, unregister learning about VNIs, if appropriate. */
1977 if (bgp
->advertise_all_vni
)
1978 bgp_zebra_advertise_all_vni(bgp
, 0);
1980 /* Deregister for router-id, interfaces, redistributed routes. */
1981 zclient_send_dereg_requests(zclient
, bgp
->vrf_id
);
1984 void bgp_zebra_initiate_radv(struct bgp
*bgp
, struct peer
*peer
)
1986 int ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
1988 /* Don't try to initiate if we're not connected to Zebra */
1989 if (zclient
->sock
< 0)
1992 if (BGP_DEBUG(zebra
, ZEBRA
))
1993 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
,
1997 * If unnumbered peer (peer->ifp) call thru zapi to start RAs.
1998 * If we don't have an ifp pointer, call function to find the
1999 * ifps for a numbered enhe peer to turn RAs on.
2001 peer
->ifp
? zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
,
2002 peer
->ifp
, 1, ra_interval
)
2003 : bgp_nht_reg_enhe_cap_intfs(peer
);
2006 void bgp_zebra_terminate_radv(struct bgp
*bgp
, struct peer
*peer
)
2008 /* Don't try to terminate if we're not connected to Zebra */
2009 if (zclient
->sock
< 0)
2012 if (BGP_DEBUG(zebra
, ZEBRA
))
2013 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
,
2017 * If unnumbered peer (peer->ifp) call thru zapi to stop RAs.
2018 * If we don't have an ifp pointer, call function to find the
2019 * ifps for a numbered enhe peer to turn RAs off.
2021 peer
->ifp
? zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
,
2023 : bgp_nht_dereg_enhe_cap_intfs(peer
);
2026 int bgp_zebra_advertise_subnet(struct bgp
*bgp
, int advertise
, vni_t vni
)
2028 struct stream
*s
= NULL
;
2031 if (!zclient
|| zclient
->sock
< 0)
2034 /* Don't try to register if Zebra doesn't know of this instance. */
2035 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
2036 if (BGP_DEBUG(zebra
, ZEBRA
))
2038 "%s: No zebra instance to talk to, cannot advertise subnet",
2046 zclient_create_header(s
, ZEBRA_ADVERTISE_SUBNET
, bgp
->vrf_id
);
2047 stream_putc(s
, advertise
);
2048 stream_put3(s
, vni
);
2049 stream_putw_at(s
, 0, stream_get_endp(s
));
2051 return zclient_send_message(zclient
);
2054 int bgp_zebra_advertise_svi_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
2056 struct stream
*s
= NULL
;
2059 if (!zclient
|| zclient
->sock
< 0)
2062 /* Don't try to register if Zebra doesn't know of this instance. */
2063 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
2069 zclient_create_header(s
, ZEBRA_ADVERTISE_SVI_MACIP
, bgp
->vrf_id
);
2070 stream_putc(s
, advertise
);
2071 stream_putl(s
, vni
);
2072 stream_putw_at(s
, 0, stream_get_endp(s
));
2074 return zclient_send_message(zclient
);
2077 int bgp_zebra_advertise_gw_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
2079 struct stream
*s
= NULL
;
2082 if (!zclient
|| zclient
->sock
< 0)
2085 /* Don't try to register if Zebra doesn't know of this instance. */
2086 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
2087 if (BGP_DEBUG(zebra
, ZEBRA
))
2089 "%s: No zebra instance to talk to, not installing gw_macip",
2097 zclient_create_header(s
, ZEBRA_ADVERTISE_DEFAULT_GW
, bgp
->vrf_id
);
2098 stream_putc(s
, advertise
);
2099 stream_putl(s
, vni
);
2100 stream_putw_at(s
, 0, stream_get_endp(s
));
2102 return zclient_send_message(zclient
);
2105 int bgp_zebra_vxlan_flood_control(struct bgp
*bgp
,
2106 enum vxlan_flood_control flood_ctrl
)
2111 if (!zclient
|| zclient
->sock
< 0)
2114 /* Don't try to register if Zebra doesn't know of this instance. */
2115 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
2116 if (BGP_DEBUG(zebra
, ZEBRA
))
2118 "%s: No zebra instance to talk to, not installing all vni",
2126 zclient_create_header(s
, ZEBRA_VXLAN_FLOOD_CONTROL
, bgp
->vrf_id
);
2127 stream_putc(s
, flood_ctrl
);
2128 stream_putw_at(s
, 0, stream_get_endp(s
));
2130 return zclient_send_message(zclient
);
2133 int bgp_zebra_advertise_all_vni(struct bgp
*bgp
, int advertise
)
2138 if (!zclient
|| zclient
->sock
< 0)
2141 /* Don't try to register if Zebra doesn't know of this instance. */
2142 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
2148 zclient_create_header(s
, ZEBRA_ADVERTISE_ALL_VNI
, bgp
->vrf_id
);
2149 stream_putc(s
, advertise
);
2150 /* Also inform current BUM handling setting. This is really
2151 * relevant only when 'advertise' is set.
2153 stream_putc(s
, bgp
->vxlan_flood_ctrl
);
2154 stream_putw_at(s
, 0, stream_get_endp(s
));
2156 return zclient_send_message(zclient
);
2159 int bgp_zebra_dup_addr_detection(struct bgp
*bgp
)
2164 if (!zclient
|| zclient
->sock
< 0)
2167 /* Don't try to register if Zebra doesn't know of this instance. */
2168 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
2171 if (BGP_DEBUG(zebra
, ZEBRA
))
2172 zlog_debug("dup addr detect %s max_moves %u time %u freeze %s freeze_time %u",
2173 bgp
->evpn_info
->dup_addr_detect
?
2174 "enable" : "disable",
2175 bgp
->evpn_info
->dad_max_moves
,
2176 bgp
->evpn_info
->dad_time
,
2177 bgp
->evpn_info
->dad_freeze
?
2178 "enable" : "disable",
2179 bgp
->evpn_info
->dad_freeze_time
);
2183 zclient_create_header(s
, ZEBRA_DUPLICATE_ADDR_DETECTION
,
2185 stream_putl(s
, bgp
->evpn_info
->dup_addr_detect
);
2186 stream_putl(s
, bgp
->evpn_info
->dad_time
);
2187 stream_putl(s
, bgp
->evpn_info
->dad_max_moves
);
2188 stream_putl(s
, bgp
->evpn_info
->dad_freeze
);
2189 stream_putl(s
, bgp
->evpn_info
->dad_freeze_time
);
2190 stream_putw_at(s
, 0, stream_get_endp(s
));
2192 return zclient_send_message(zclient
);
2195 static int rule_notify_owner(ZAPI_CALLBACK_ARGS
)
2197 uint32_t seqno
, priority
, unique
;
2198 enum zapi_rule_notify_owner note
;
2199 struct bgp_pbr_action
*bgp_pbra
;
2200 struct bgp_pbr_rule
*bgp_pbr
= NULL
;
2201 char ifname
[INTERFACE_NAMSIZ
+ 1];
2203 if (!zapi_rule_notify_decode(zclient
->ibuf
, &seqno
, &priority
, &unique
,
2207 bgp_pbra
= bgp_pbr_action_rule_lookup(vrf_id
, unique
);
2209 /* look in bgp pbr rule */
2210 bgp_pbr
= bgp_pbr_rule_lookup(vrf_id
, unique
);
2211 if (!bgp_pbr
&& note
!= ZAPI_RULE_REMOVED
) {
2212 if (BGP_DEBUG(zebra
, ZEBRA
))
2213 zlog_debug("%s: Fail to look BGP rule (%u)",
2220 case ZAPI_RULE_FAIL_INSTALL
:
2221 if (BGP_DEBUG(zebra
, ZEBRA
))
2222 zlog_debug("%s: Received RULE_FAIL_INSTALL", __func__
);
2224 bgp_pbra
->installed
= false;
2225 bgp_pbra
->install_in_progress
= false;
2227 bgp_pbr
->installed
= false;
2228 bgp_pbr
->install_in_progress
= false;
2231 case ZAPI_RULE_INSTALLED
:
2233 bgp_pbra
->installed
= true;
2234 bgp_pbra
->install_in_progress
= false;
2236 struct bgp_path_info
*path
;
2237 struct bgp_path_info_extra
*extra
;
2239 bgp_pbr
->installed
= true;
2240 bgp_pbr
->install_in_progress
= false;
2241 bgp_pbr
->action
->refcnt
++;
2242 /* link bgp_info to bgp_pbr */
2243 path
= (struct bgp_path_info
*)bgp_pbr
->path
;
2244 extra
= bgp_path_info_extra_get(path
);
2245 listnode_add_force(&extra
->bgp_fs_iprule
,
2248 if (BGP_DEBUG(zebra
, ZEBRA
))
2249 zlog_debug("%s: Received RULE_INSTALLED", __func__
);
2251 case ZAPI_RULE_FAIL_REMOVE
:
2252 case ZAPI_RULE_REMOVED
:
2253 if (BGP_DEBUG(zebra
, ZEBRA
))
2254 zlog_debug("%s: Received RULE REMOVED", __func__
);
2261 static int ipset_notify_owner(ZAPI_CALLBACK_ARGS
)
2264 enum zapi_ipset_notify_owner note
;
2265 struct bgp_pbr_match
*bgp_pbim
;
2267 if (!zapi_ipset_notify_decode(zclient
->ibuf
,
2272 bgp_pbim
= bgp_pbr_match_ipset_lookup(vrf_id
, unique
);
2274 if (BGP_DEBUG(zebra
, ZEBRA
))
2275 zlog_debug("%s: Fail to look BGP match ( %u, ID %u)",
2276 __func__
, note
, unique
);
2281 case ZAPI_IPSET_FAIL_INSTALL
:
2282 if (BGP_DEBUG(zebra
, ZEBRA
))
2283 zlog_debug("%s: Received IPSET_FAIL_INSTALL", __func__
);
2284 bgp_pbim
->installed
= false;
2285 bgp_pbim
->install_in_progress
= false;
2287 case ZAPI_IPSET_INSTALLED
:
2288 bgp_pbim
->installed
= true;
2289 bgp_pbim
->install_in_progress
= false;
2290 if (BGP_DEBUG(zebra
, ZEBRA
))
2291 zlog_debug("%s: Received IPSET_INSTALLED", __func__
);
2293 case ZAPI_IPSET_FAIL_REMOVE
:
2294 case ZAPI_IPSET_REMOVED
:
2295 if (BGP_DEBUG(zebra
, ZEBRA
))
2296 zlog_debug("%s: Received IPSET REMOVED", __func__
);
2303 static int ipset_entry_notify_owner(ZAPI_CALLBACK_ARGS
)
2306 char ipset_name
[ZEBRA_IPSET_NAME_SIZE
];
2307 enum zapi_ipset_entry_notify_owner note
;
2308 struct bgp_pbr_match_entry
*bgp_pbime
;
2310 if (!zapi_ipset_entry_notify_decode(
2316 bgp_pbime
= bgp_pbr_match_ipset_entry_lookup(vrf_id
,
2320 if (BGP_DEBUG(zebra
, ZEBRA
))
2322 "%s: Fail to look BGP match entry (%u, ID %u)",
2323 __func__
, note
, unique
);
2328 case ZAPI_IPSET_ENTRY_FAIL_INSTALL
:
2329 if (BGP_DEBUG(zebra
, ZEBRA
))
2330 zlog_debug("%s: Received IPSET_ENTRY_FAIL_INSTALL",
2332 bgp_pbime
->installed
= false;
2333 bgp_pbime
->install_in_progress
= false;
2335 case ZAPI_IPSET_ENTRY_INSTALLED
:
2337 struct bgp_path_info
*path
;
2338 struct bgp_path_info_extra
*extra
;
2340 bgp_pbime
->installed
= true;
2341 bgp_pbime
->install_in_progress
= false;
2342 if (BGP_DEBUG(zebra
, ZEBRA
))
2343 zlog_debug("%s: Received IPSET_ENTRY_INSTALLED",
2345 /* link bgp_path_info to bpme */
2346 path
= (struct bgp_path_info
*)bgp_pbime
->path
;
2347 extra
= bgp_path_info_extra_get(path
);
2348 listnode_add_force(&extra
->bgp_fs_pbr
, bgp_pbime
);
2351 case ZAPI_IPSET_ENTRY_FAIL_REMOVE
:
2352 case ZAPI_IPSET_ENTRY_REMOVED
:
2353 if (BGP_DEBUG(zebra
, ZEBRA
))
2354 zlog_debug("%s: Received IPSET_ENTRY_REMOVED",
2361 static int iptable_notify_owner(ZAPI_CALLBACK_ARGS
)
2364 enum zapi_iptable_notify_owner note
;
2365 struct bgp_pbr_match
*bgpm
;
2367 if (!zapi_iptable_notify_decode(
2372 bgpm
= bgp_pbr_match_iptable_lookup(vrf_id
, unique
);
2374 if (BGP_DEBUG(zebra
, ZEBRA
))
2375 zlog_debug("%s: Fail to look BGP iptable (%u %u)",
2376 __func__
, note
, unique
);
2380 case ZAPI_IPTABLE_FAIL_INSTALL
:
2381 if (BGP_DEBUG(zebra
, ZEBRA
))
2382 zlog_debug("%s: Received IPTABLE_FAIL_INSTALL",
2384 bgpm
->installed_in_iptable
= false;
2385 bgpm
->install_iptable_in_progress
= false;
2387 case ZAPI_IPTABLE_INSTALLED
:
2388 bgpm
->installed_in_iptable
= true;
2389 bgpm
->install_iptable_in_progress
= false;
2390 if (BGP_DEBUG(zebra
, ZEBRA
))
2391 zlog_debug("%s: Received IPTABLE_INSTALLED", __func__
);
2392 bgpm
->action
->refcnt
++;
2394 case ZAPI_IPTABLE_FAIL_REMOVE
:
2395 case ZAPI_IPTABLE_REMOVED
:
2396 if (BGP_DEBUG(zebra
, ZEBRA
))
2397 zlog_debug("%s: Received IPTABLE REMOVED", __func__
);
2403 /* this function is used to forge ip rule,
2404 * - either for iptable/ipset using fwmark id
2405 * - or for sample ip rule cmd
2407 static void bgp_encode_pbr_rule_action(struct stream
*s
,
2408 struct bgp_pbr_action
*pbra
,
2409 struct bgp_pbr_rule
*pbr
)
2412 uint8_t fam
= AF_INET
;
2414 if (pbra
->nh
.type
== NEXTHOP_TYPE_IPV6
)
2416 stream_putl(s
, 0); /* seqno unused */
2418 stream_putl(s
, pbr
->priority
);
2421 /* ruleno unused - priority change
2422 * ruleno permits distinguishing various FS PBR entries
2423 * - FS PBR entries based on ipset/iptables
2424 * - FS PBR entries based on iprule
2425 * the latter may contain default routing information injected by FS
2428 stream_putl(s
, pbr
->unique
);
2430 stream_putl(s
, pbra
->unique
);
2431 if (pbr
&& pbr
->flags
& MATCH_IP_SRC_SET
)
2432 memcpy(&pfx
, &(pbr
->src
), sizeof(struct prefix
));
2434 memset(&pfx
, 0, sizeof(pfx
));
2437 stream_putc(s
, pfx
.family
);
2438 stream_putc(s
, pfx
.prefixlen
);
2439 stream_put(s
, &pfx
.u
.prefix
, prefix_blen(&pfx
));
2441 stream_putw(s
, 0); /* src port */
2443 if (pbr
&& pbr
->flags
& MATCH_IP_DST_SET
)
2444 memcpy(&pfx
, &(pbr
->dst
), sizeof(struct prefix
));
2446 memset(&pfx
, 0, sizeof(pfx
));
2449 stream_putc(s
, pfx
.family
);
2450 stream_putc(s
, pfx
.prefixlen
);
2451 stream_put(s
, &pfx
.u
.prefix
, prefix_blen(&pfx
));
2453 stream_putw(s
, 0); /* dst port */
2455 /* if pbr present, fwmark is not used */
2459 stream_putl(s
, pbra
->fwmark
); /* fwmark */
2461 stream_putl(s
, pbra
->table_id
);
2463 stream_putl(s
, 0); /* ifindex unused */
2466 static void bgp_encode_pbr_ipset_match(struct stream
*s
,
2467 struct bgp_pbr_match
*pbim
)
2469 stream_putl(s
, pbim
->unique
);
2470 stream_putl(s
, pbim
->type
);
2471 stream_putc(s
, pbim
->family
);
2472 stream_put(s
, pbim
->ipset_name
,
2473 ZEBRA_IPSET_NAME_SIZE
);
2476 static void bgp_encode_pbr_ipset_entry_match(struct stream
*s
,
2477 struct bgp_pbr_match_entry
*pbime
)
2479 stream_putl(s
, pbime
->unique
);
2480 /* check that back pointer is not null */
2481 stream_put(s
, pbime
->backpointer
->ipset_name
,
2482 ZEBRA_IPSET_NAME_SIZE
);
2484 stream_putc(s
, pbime
->src
.family
);
2485 stream_putc(s
, pbime
->src
.prefixlen
);
2486 stream_put(s
, &pbime
->src
.u
.prefix
, prefix_blen(&pbime
->src
));
2488 stream_putc(s
, pbime
->dst
.family
);
2489 stream_putc(s
, pbime
->dst
.prefixlen
);
2490 stream_put(s
, &pbime
->dst
.u
.prefix
, prefix_blen(&pbime
->dst
));
2492 stream_putw(s
, pbime
->src_port_min
);
2493 stream_putw(s
, pbime
->src_port_max
);
2494 stream_putw(s
, pbime
->dst_port_min
);
2495 stream_putw(s
, pbime
->dst_port_max
);
2496 stream_putc(s
, pbime
->proto
);
2499 static void bgp_encode_pbr_iptable_match(struct stream
*s
,
2500 struct bgp_pbr_action
*bpa
,
2501 struct bgp_pbr_match
*pbm
)
2503 stream_putl(s
, pbm
->unique2
);
2505 stream_putl(s
, pbm
->type
);
2507 stream_putl(s
, pbm
->flags
);
2509 /* TODO: correlate with what is contained
2510 * into bgp_pbr_action.
2511 * currently only forward supported
2513 if (bpa
->nh
.type
== NEXTHOP_TYPE_BLACKHOLE
)
2514 stream_putl(s
, ZEBRA_IPTABLES_DROP
);
2516 stream_putl(s
, ZEBRA_IPTABLES_FORWARD
);
2517 stream_putl(s
, bpa
->fwmark
);
2518 stream_put(s
, pbm
->ipset_name
,
2519 ZEBRA_IPSET_NAME_SIZE
);
2520 stream_putc(s
, pbm
->family
);
2521 stream_putw(s
, pbm
->pkt_len_min
);
2522 stream_putw(s
, pbm
->pkt_len_max
);
2523 stream_putw(s
, pbm
->tcp_flags
);
2524 stream_putw(s
, pbm
->tcp_mask_flags
);
2525 stream_putc(s
, pbm
->dscp_value
);
2526 stream_putc(s
, pbm
->fragment
);
2527 stream_putc(s
, pbm
->protocol
);
2528 stream_putw(s
, pbm
->flow_label
);
2531 /* BGP has established connection with Zebra. */
2532 static void bgp_zebra_connected(struct zclient
*zclient
)
2536 zclient_num_connects
++; /* increment even if not responding */
2538 /* At this point, we may or may not have BGP instances configured, but
2539 * we're only interested in the default VRF (others wouldn't have learnt
2540 * the VRF from Zebra yet.)
2542 bgp
= bgp_get_default();
2546 bgp_zebra_instance_register(bgp
);
2548 /* Send the client registration */
2549 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, bgp
->vrf_id
);
2551 /* tell label pool that zebra is connected */
2552 bgp_lp_event_zebra_up();
2554 /* TODO - What if we have peers and networks configured, do we have to
2557 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp
, bgp
->peer
);
2560 static int bgp_zebra_process_local_es_add(ZAPI_CALLBACK_ARGS
)
2563 struct bgp
*bgp
= NULL
;
2564 struct stream
*s
= NULL
;
2565 char buf
[ESI_STR_LEN
];
2566 struct in_addr originator_ip
;
2569 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2574 stream_get(&esi
, s
, sizeof(esi_t
));
2575 originator_ip
.s_addr
= stream_get_ipv4(s
);
2576 active
= stream_getc(s
);
2578 if (BGP_DEBUG(zebra
, ZEBRA
))
2579 zlog_debug("Rx add ESI %s originator-ip %s active %u",
2580 esi_to_str(&esi
, buf
, sizeof(buf
)),
2581 inet_ntoa(originator_ip
),
2584 bgp_evpn_local_es_add(bgp
, &esi
, originator_ip
, active
);
2589 static int bgp_zebra_process_local_es_del(ZAPI_CALLBACK_ARGS
)
2592 struct bgp
*bgp
= NULL
;
2593 struct stream
*s
= NULL
;
2594 char buf
[ESI_STR_LEN
];
2596 memset(&esi
, 0, sizeof(esi_t
));
2597 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2602 stream_get(&esi
, s
, sizeof(esi_t
));
2604 if (BGP_DEBUG(zebra
, ZEBRA
))
2605 zlog_debug("Rx del ESI %s",
2606 esi_to_str(&esi
, buf
, sizeof(buf
)));
2608 bgp_evpn_local_es_del(bgp
, &esi
);
2613 static int bgp_zebra_process_local_es_evi(ZAPI_CALLBACK_ARGS
)
2619 char buf
[ESI_STR_LEN
];
2621 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2626 stream_get(&esi
, s
, sizeof(esi_t
));
2627 vni
= stream_getl(s
);
2629 if (BGP_DEBUG(zebra
, ZEBRA
))
2630 zlog_debug("Rx %s ESI %s VNI %u",
2631 ZEBRA_VNI_ADD
? "add" : "del",
2632 esi_to_str(&esi
, buf
, sizeof(buf
)), vni
);
2634 if (cmd
== ZEBRA_LOCAL_ES_EVI_ADD
)
2635 bgp_evpn_local_es_evi_add(bgp
, &esi
, vni
);
2637 bgp_evpn_local_es_evi_del(bgp
, &esi
, vni
);
2642 static int bgp_zebra_process_local_l3vni(ZAPI_CALLBACK_ARGS
)
2645 char buf
[ETHER_ADDR_STRLEN
];
2647 struct ethaddr svi_rmac
, vrr_rmac
= {.octet
= {0} };
2648 struct in_addr originator_ip
;
2650 ifindex_t svi_ifindex
;
2651 bool is_anycast_mac
= false;
2652 char buf1
[ETHER_ADDR_STRLEN
];
2654 memset(&svi_rmac
, 0, sizeof(struct ethaddr
));
2655 memset(&originator_ip
, 0, sizeof(struct in_addr
));
2657 l3vni
= stream_getl(s
);
2658 if (cmd
== ZEBRA_L3VNI_ADD
) {
2659 stream_get(&svi_rmac
, s
, sizeof(struct ethaddr
));
2660 originator_ip
.s_addr
= stream_get_ipv4(s
);
2661 stream_get(&filter
, s
, sizeof(int));
2662 svi_ifindex
= stream_getl(s
);
2663 stream_get(&vrr_rmac
, s
, sizeof(struct ethaddr
));
2664 is_anycast_mac
= stream_getl(s
);
2666 if (BGP_DEBUG(zebra
, ZEBRA
))
2667 zlog_debug("Rx L3-VNI ADD VRF %s VNI %u RMAC svi-mac %s vrr-mac %s filter %s svi-if %u",
2668 vrf_id_to_name(vrf_id
), l3vni
,
2669 prefix_mac2str(&svi_rmac
, buf
, sizeof(buf
)),
2670 prefix_mac2str(&vrr_rmac
, buf1
,
2672 filter
? "prefix-routes-only" : "none",
2675 bgp_evpn_local_l3vni_add(l3vni
, vrf_id
, &svi_rmac
, &vrr_rmac
,
2676 originator_ip
, filter
, svi_ifindex
,
2679 if (BGP_DEBUG(zebra
, ZEBRA
))
2680 zlog_debug("Rx L3-VNI DEL VRF %s VNI %u",
2681 vrf_id_to_name(vrf_id
), l3vni
);
2683 bgp_evpn_local_l3vni_del(l3vni
, vrf_id
);
2689 static int bgp_zebra_process_local_vni(ZAPI_CALLBACK_ARGS
)
2694 struct in_addr vtep_ip
= {INADDR_ANY
};
2695 vrf_id_t tenant_vrf_id
= VRF_DEFAULT
;
2696 struct in_addr mcast_grp
= {INADDR_ANY
};
2699 vni
= stream_getl(s
);
2700 if (cmd
== ZEBRA_VNI_ADD
) {
2701 vtep_ip
.s_addr
= stream_get_ipv4(s
);
2702 stream_get(&tenant_vrf_id
, s
, sizeof(vrf_id_t
));
2703 mcast_grp
.s_addr
= stream_get_ipv4(s
);
2706 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2710 if (BGP_DEBUG(zebra
, ZEBRA
))
2711 zlog_debug("Rx VNI %s VRF %s VNI %u tenant-vrf %s",
2712 (cmd
== ZEBRA_VNI_ADD
) ? "add" : "del",
2713 vrf_id_to_name(vrf_id
), vni
,
2714 vrf_id_to_name(tenant_vrf_id
));
2716 if (cmd
== ZEBRA_VNI_ADD
)
2717 return bgp_evpn_local_vni_add(
2719 vtep_ip
.s_addr
!= INADDR_ANY
? vtep_ip
: bgp
->router_id
,
2720 tenant_vrf_id
, mcast_grp
);
2722 return bgp_evpn_local_vni_del(bgp
, vni
);
2725 static int bgp_zebra_process_local_macip(ZAPI_CALLBACK_ARGS
)
2733 char buf
[ETHER_ADDR_STRLEN
];
2734 char buf1
[INET6_ADDRSTRLEN
];
2736 uint32_t seqnum
= 0;
2738 char buf2
[ESI_STR_LEN
];
2741 memset(&ip
, 0, sizeof(ip
));
2743 vni
= stream_getl(s
);
2744 stream_get(&mac
.octet
, s
, ETH_ALEN
);
2745 ipa_len
= stream_getl(s
);
2746 if (ipa_len
!= 0 && ipa_len
!= IPV4_MAX_BYTELEN
2747 && ipa_len
!= IPV6_MAX_BYTELEN
) {
2748 flog_err(EC_BGP_MACIP_LEN
,
2749 "%u:Recv MACIP %s with invalid IP addr length %d",
2750 vrf_id
, (cmd
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
2757 (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
: IPADDR_V6
;
2758 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
2760 if (cmd
== ZEBRA_MACIP_ADD
) {
2761 flags
= stream_getc(s
);
2762 seqnum
= stream_getl(s
);
2763 stream_get(&esi
, s
, sizeof(esi_t
));
2765 state
= stream_getl(s
);
2768 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2772 if (BGP_DEBUG(zebra
, ZEBRA
))
2773 zlog_debug("%u:Recv MACIP %s f 0x%x MAC %s IP %s VNI %u seq %u state %d ESI %s",
2774 vrf_id
, (cmd
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
2775 flags
, prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2776 ipaddr2str(&ip
, buf1
, sizeof(buf1
)), vni
, seqnum
,
2777 state
, esi_to_str(&esi
, buf2
, sizeof(buf2
)));
2779 if (cmd
== ZEBRA_MACIP_ADD
)
2780 return bgp_evpn_local_macip_add(bgp
, vni
, &mac
, &ip
,
2781 flags
, seqnum
, &esi
);
2783 return bgp_evpn_local_macip_del(bgp
, vni
, &mac
, &ip
, state
);
2786 static void bgp_zebra_process_local_ip_prefix(ZAPI_CALLBACK_ARGS
)
2788 struct stream
*s
= NULL
;
2789 struct bgp
*bgp_vrf
= NULL
;
2791 char buf
[PREFIX_STRLEN
];
2793 memset(&p
, 0, sizeof(struct prefix
));
2795 stream_get(&p
, s
, sizeof(struct prefix
));
2797 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
2801 if (BGP_DEBUG(zebra
, ZEBRA
))
2802 zlog_debug("Recv prefix %s %s on vrf %s",
2803 prefix2str(&p
, buf
, sizeof(buf
)),
2804 (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) ? "ADD" : "DEL",
2805 vrf_id_to_name(vrf_id
));
2807 if (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) {
2809 if (p
.family
== AF_INET
)
2810 bgp_evpn_advertise_type5_route(bgp_vrf
, &p
, NULL
,
2811 AFI_IP
, SAFI_UNICAST
);
2813 bgp_evpn_advertise_type5_route(bgp_vrf
, &p
, NULL
,
2814 AFI_IP6
, SAFI_UNICAST
);
2817 if (p
.family
== AF_INET
)
2818 bgp_evpn_withdraw_type5_route(bgp_vrf
, &p
, AFI_IP
,
2821 bgp_evpn_withdraw_type5_route(bgp_vrf
, &p
, AFI_IP6
,
2826 static void bgp_zebra_process_label_chunk(ZAPI_CALLBACK_ARGS
)
2828 struct stream
*s
= NULL
;
2829 uint8_t response_keep
;
2833 unsigned short instance
;
2836 STREAM_GETC(s
, proto
);
2837 STREAM_GETW(s
, instance
);
2838 STREAM_GETC(s
, response_keep
);
2839 STREAM_GETL(s
, first
);
2840 STREAM_GETL(s
, last
);
2842 if (zclient
->redist_default
!= proto
) {
2843 flog_err(EC_BGP_LM_ERROR
, "Got LM msg with wrong proto %u",
2847 if (zclient
->instance
!= instance
) {
2848 flog_err(EC_BGP_LM_ERROR
, "Got LM msg with wrong instance %u",
2854 first
< MPLS_LABEL_UNRESERVED_MIN
||
2855 last
> MPLS_LABEL_UNRESERVED_MAX
) {
2857 flog_err(EC_BGP_LM_ERROR
, "%s: Invalid Label chunk: %u - %u",
2858 __func__
, first
, last
);
2861 if (BGP_DEBUG(zebra
, ZEBRA
)) {
2862 zlog_debug("Label Chunk assign: %u - %u (%u) ",
2863 first
, last
, response_keep
);
2866 bgp_lp_event_chunk(response_keep
, first
, last
);
2868 stream_failure
: /* for STREAM_GETX */
2872 extern struct zebra_privs_t bgpd_privs
;
2874 static int bgp_ifp_create(struct interface
*ifp
)
2878 if (BGP_DEBUG(zebra
, ZEBRA
))
2879 zlog_debug("Rx Intf add VRF %u IF %s", ifp
->vrf_id
, ifp
->name
);
2881 bgp
= bgp_lookup_by_vrf_id(ifp
->vrf_id
);
2885 bgp_mac_add_mac_entry(ifp
);
2887 bgp_update_interface_nbrs(bgp
, ifp
, ifp
);
2891 void bgp_zebra_init(struct thread_master
*master
, unsigned short instance
)
2893 zclient_num_connects
= 0;
2895 if_zapi_callbacks(bgp_ifp_create
, bgp_ifp_up
,
2896 bgp_ifp_down
, bgp_ifp_destroy
);
2898 /* Set default values. */
2899 zclient
= zclient_new(master
, &zclient_options_default
);
2900 zclient_init(zclient
, ZEBRA_ROUTE_BGP
, 0, &bgpd_privs
);
2901 zclient
->zebra_connected
= bgp_zebra_connected
;
2902 zclient
->router_id_update
= bgp_router_id_update
;
2903 zclient
->interface_address_add
= bgp_interface_address_add
;
2904 zclient
->interface_address_delete
= bgp_interface_address_delete
;
2905 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
2906 zclient
->interface_nbr_address_delete
=
2907 bgp_interface_nbr_address_delete
;
2908 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
2909 zclient
->redistribute_route_add
= zebra_read_route
;
2910 zclient
->redistribute_route_del
= zebra_read_route
;
2911 zclient
->nexthop_update
= bgp_read_nexthop_update
;
2912 zclient
->import_check_update
= bgp_read_import_check_update
;
2913 zclient
->fec_update
= bgp_read_fec_update
;
2914 zclient
->local_es_add
= bgp_zebra_process_local_es_add
;
2915 zclient
->local_es_del
= bgp_zebra_process_local_es_del
;
2916 zclient
->local_vni_add
= bgp_zebra_process_local_vni
;
2917 zclient
->local_es_evi_add
= bgp_zebra_process_local_es_evi
;
2918 zclient
->local_es_evi_del
= bgp_zebra_process_local_es_evi
;
2919 zclient
->local_vni_del
= bgp_zebra_process_local_vni
;
2920 zclient
->local_macip_add
= bgp_zebra_process_local_macip
;
2921 zclient
->local_macip_del
= bgp_zebra_process_local_macip
;
2922 zclient
->local_l3vni_add
= bgp_zebra_process_local_l3vni
;
2923 zclient
->local_l3vni_del
= bgp_zebra_process_local_l3vni
;
2924 zclient
->local_ip_prefix_add
= bgp_zebra_process_local_ip_prefix
;
2925 zclient
->local_ip_prefix_del
= bgp_zebra_process_local_ip_prefix
;
2926 zclient
->label_chunk
= bgp_zebra_process_label_chunk
;
2927 zclient
->rule_notify_owner
= rule_notify_owner
;
2928 zclient
->ipset_notify_owner
= ipset_notify_owner
;
2929 zclient
->ipset_entry_notify_owner
= ipset_entry_notify_owner
;
2930 zclient
->iptable_notify_owner
= iptable_notify_owner
;
2931 zclient
->instance
= instance
;
2934 void bgp_zebra_destroy(void)
2936 if (zclient
== NULL
)
2938 zclient_stop(zclient
);
2939 zclient_free(zclient
);
2943 int bgp_zebra_num_connects(void)
2945 return zclient_num_connects
;
2948 void bgp_send_pbr_rule_action(struct bgp_pbr_action
*pbra
,
2949 struct bgp_pbr_rule
*pbr
,
2954 if (pbra
->install_in_progress
&& !pbr
)
2956 if (pbr
&& pbr
->install_in_progress
)
2958 if (BGP_DEBUG(zebra
, ZEBRA
)) {
2960 zlog_debug("%s: table %d (ip rule) %d", __func__
,
2961 pbra
->table_id
, install
);
2963 zlog_debug("%s: table %d fwmark %d %d", __func__
,
2964 pbra
->table_id
, pbra
->fwmark
, install
);
2969 zclient_create_header(s
,
2970 install
? ZEBRA_RULE_ADD
: ZEBRA_RULE_DELETE
,
2972 stream_putl(s
, 1); /* send one pbr action */
2974 bgp_encode_pbr_rule_action(s
, pbra
, pbr
);
2976 stream_putw_at(s
, 0, stream_get_endp(s
));
2977 if (!zclient_send_message(zclient
) && install
) {
2979 pbra
->install_in_progress
= true;
2981 pbr
->install_in_progress
= true;
2985 void bgp_send_pbr_ipset_match(struct bgp_pbr_match
*pbrim
, bool install
)
2989 if (pbrim
->install_in_progress
)
2991 if (BGP_DEBUG(zebra
, ZEBRA
))
2992 zlog_debug("%s: name %s type %d %d, ID %u", __func__
,
2993 pbrim
->ipset_name
, pbrim
->type
, install
,
2998 zclient_create_header(s
,
2999 install
? ZEBRA_IPSET_CREATE
:
3000 ZEBRA_IPSET_DESTROY
,
3003 stream_putl(s
, 1); /* send one pbr action */
3005 bgp_encode_pbr_ipset_match(s
, pbrim
);
3007 stream_putw_at(s
, 0, stream_get_endp(s
));
3008 if (!zclient_send_message(zclient
) && install
)
3009 pbrim
->install_in_progress
= true;
3012 void bgp_send_pbr_ipset_entry_match(struct bgp_pbr_match_entry
*pbrime
,
3017 if (pbrime
->install_in_progress
)
3019 if (BGP_DEBUG(zebra
, ZEBRA
))
3020 zlog_debug("%s: name %s %d %d, ID %u", __func__
,
3021 pbrime
->backpointer
->ipset_name
, pbrime
->unique
,
3022 install
, pbrime
->unique
);
3026 zclient_create_header(s
,
3027 install
? ZEBRA_IPSET_ENTRY_ADD
:
3028 ZEBRA_IPSET_ENTRY_DELETE
,
3031 stream_putl(s
, 1); /* send one pbr action */
3033 bgp_encode_pbr_ipset_entry_match(s
, pbrime
);
3035 stream_putw_at(s
, 0, stream_get_endp(s
));
3036 if (!zclient_send_message(zclient
) && install
)
3037 pbrime
->install_in_progress
= true;
3040 static void bgp_encode_pbr_interface_list(struct bgp
*bgp
, struct stream
*s
,
3043 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
3044 struct bgp_pbr_interface_head
*head
;
3045 struct bgp_pbr_interface
*pbr_if
;
3046 struct interface
*ifp
;
3050 if (family
== AF_INET
)
3051 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
3053 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv6
);
3054 RB_FOREACH (pbr_if
, bgp_pbr_interface_head
, head
) {
3055 ifp
= if_lookup_by_name(pbr_if
->name
, bgp
->vrf_id
);
3057 stream_putl(s
, ifp
->ifindex
);
3061 static int bgp_pbr_get_ifnumber(struct bgp
*bgp
, uint8_t family
)
3063 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
3064 struct bgp_pbr_interface_head
*head
;
3065 struct bgp_pbr_interface
*pbr_if
;
3070 if (family
== AF_INET
)
3071 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
3073 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv6
);
3074 RB_FOREACH (pbr_if
, bgp_pbr_interface_head
, head
) {
3075 if (if_lookup_by_name(pbr_if
->name
, bgp
->vrf_id
))
3081 void bgp_send_pbr_iptable(struct bgp_pbr_action
*pba
,
3082 struct bgp_pbr_match
*pbm
,
3089 if (pbm
->install_iptable_in_progress
)
3091 if (BGP_DEBUG(zebra
, ZEBRA
))
3092 zlog_debug("%s: name %s type %d mark %d %d, ID %u", __func__
,
3093 pbm
->ipset_name
, pbm
->type
, pba
->fwmark
, install
,
3098 zclient_create_header(s
,
3099 install
? ZEBRA_IPTABLE_ADD
:
3100 ZEBRA_IPTABLE_DELETE
,
3103 bgp_encode_pbr_iptable_match(s
, pba
, pbm
);
3104 nb_interface
= bgp_pbr_get_ifnumber(pba
->bgp
, pbm
->family
);
3105 stream_putl(s
, nb_interface
);
3107 bgp_encode_pbr_interface_list(pba
->bgp
, s
, pbm
->family
);
3108 stream_putw_at(s
, 0, stream_get_endp(s
));
3109 ret
= zclient_send_message(zclient
);
3114 pbm
->install_iptable_in_progress
= true;
3118 /* inject in table <table_id> a default route to:
3119 * - if nexthop IP is present : to this nexthop
3120 * - if vrf is different from local : to the matching VRF
3122 void bgp_zebra_announce_default(struct bgp
*bgp
, struct nexthop
*nh
,
3123 afi_t afi
, uint32_t table_id
, bool announce
)
3125 struct zapi_nexthop
*api_nh
;
3126 struct zapi_route api
;
3129 if (!nh
|| (nh
->type
!= NEXTHOP_TYPE_IPV4
3130 && nh
->type
!= NEXTHOP_TYPE_IPV6
)
3131 || nh
->vrf_id
== VRF_UNKNOWN
)
3133 memset(&p
, 0, sizeof(struct prefix
));
3134 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
)
3136 p
.family
= afi2family(afi
);
3137 memset(&api
, 0, sizeof(api
));
3138 api
.vrf_id
= bgp
->vrf_id
;
3139 api
.type
= ZEBRA_ROUTE_BGP
;
3140 api
.safi
= SAFI_UNICAST
;
3142 api
.tableid
= table_id
;
3143 api
.nexthop_num
= 1;
3144 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
3145 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
3146 api_nh
= &api
.nexthops
[0];
3148 api
.distance
= ZEBRA_EBGP_DISTANCE_DEFAULT
;
3149 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
3152 if (afi
== AFI_IP
&& nh
->gate
.ipv4
.s_addr
!= INADDR_ANY
) {
3153 char buff
[PREFIX_STRLEN
];
3155 api_nh
->vrf_id
= nh
->vrf_id
;
3156 api_nh
->gate
.ipv4
= nh
->gate
.ipv4
;
3157 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
3159 inet_ntop(AF_INET
, &(nh
->gate
.ipv4
), buff
, INET_ADDRSTRLEN
);
3160 if (BGP_DEBUG(zebra
, ZEBRA
))
3161 zlog_debug("BGP: %s default route to %s table %d (redirect IP)",
3162 announce
? "adding" : "withdrawing",
3164 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
3165 : ZEBRA_ROUTE_DELETE
,
3167 } else if (afi
== AFI_IP6
&&
3168 memcmp(&nh
->gate
.ipv6
,
3169 &in6addr_any
, sizeof(struct in6_addr
))) {
3170 char buff
[PREFIX_STRLEN
];
3172 api_nh
->vrf_id
= nh
->vrf_id
;
3173 memcpy(&api_nh
->gate
.ipv6
, &nh
->gate
.ipv6
,
3174 sizeof(struct in6_addr
));
3175 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
3177 inet_ntop(AF_INET6
, &(nh
->gate
.ipv6
), buff
, INET_ADDRSTRLEN
);
3178 if (BGP_DEBUG(zebra
, ZEBRA
))
3179 zlog_debug("BGP: %s default route to %s table %d (redirect IP)",
3180 announce
? "adding" : "withdrawing",
3182 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
3183 : ZEBRA_ROUTE_DELETE
,
3185 } else if (nh
->vrf_id
!= bgp
->vrf_id
) {
3187 struct interface
*ifp
;
3189 vrf
= vrf_lookup_by_id(nh
->vrf_id
);
3192 /* create default route with interface <VRF>
3193 * with nexthop-vrf <VRF>
3195 ifp
= if_lookup_by_name_all_vrf(vrf
->name
);
3198 api_nh
->vrf_id
= nh
->vrf_id
;
3199 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
3200 api_nh
->ifindex
= ifp
->ifindex
;
3201 if (BGP_DEBUG(zebra
, ZEBRA
))
3202 zlog_info("BGP: %s default route to %s table %d (redirect VRF)",
3203 announce
? "adding" : "withdrawing",
3204 vrf
->name
, table_id
);
3205 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
3206 : ZEBRA_ROUTE_DELETE
,
3212 /* Send capabilities to RIB */
3213 int bgp_zebra_send_capabilities(struct bgp
*bgp
, bool disable
)
3215 struct zapi_cap api
;
3216 int ret
= BGP_GR_SUCCESS
;
3218 if (zclient
== NULL
) {
3219 if (BGP_DEBUG(zebra
, ZEBRA
))
3220 zlog_debug("zclient invalid");
3221 return BGP_GR_FAILURE
;
3224 /* Check if the client is connected */
3225 if ((zclient
->sock
< 0) || (zclient
->t_connect
)) {
3226 if (BGP_DEBUG(zebra
, ZEBRA
))
3227 zlog_debug("client not connected");
3228 return BGP_GR_FAILURE
;
3231 /* Check if capability is already sent. If the flag force is set
3232 * send the capability since this can be initial bgp configuration
3234 memset(&api
, 0, sizeof(struct zapi_cap
));
3236 api
.cap
= ZEBRA_CLIENT_GR_DISABLE
;
3237 api
.vrf_id
= bgp
->vrf_id
;
3239 api
.cap
= ZEBRA_CLIENT_GR_CAPABILITIES
;
3240 api
.stale_removal_time
= bgp
->rib_stale_time
;
3241 api
.vrf_id
= bgp
->vrf_id
;
3244 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
, &api
)
3246 zlog_err("error sending capability");
3247 ret
= BGP_GR_FAILURE
;
3250 bgp
->present_zebra_gr_state
= ZEBRA_GR_DISABLE
;
3252 bgp
->present_zebra_gr_state
= ZEBRA_GR_ENABLE
;
3254 if (BGP_DEBUG(zebra
, ZEBRA
))
3255 zlog_debug("send capabilty success");
3256 ret
= BGP_GR_SUCCESS
;
3261 /* Send route update pesding or completed status to RIB for the
3262 * specific AFI, SAFI
3264 int bgp_zebra_update(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
)
3266 struct zapi_cap api
= {0};
3268 if (zclient
== NULL
) {
3269 if (BGP_DEBUG(zebra
, ZEBRA
))
3270 zlog_debug("zclient == NULL, invalid");
3271 return BGP_GR_FAILURE
;
3274 /* Check if the client is connected */
3275 if ((zclient
->sock
< 0) || (zclient
->t_connect
)) {
3276 if (BGP_DEBUG(zebra
, ZEBRA
))
3277 zlog_debug("client not connected");
3278 return BGP_GR_FAILURE
;
3283 api
.vrf_id
= vrf_id
;
3286 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
, &api
)
3288 if (BGP_DEBUG(zebra
, ZEBRA
))
3289 zlog_debug("error sending capability");
3290 return BGP_GR_FAILURE
;
3292 return BGP_GR_SUCCESS
;
3296 /* Send RIB stale timer update */
3297 int bgp_zebra_stale_timer_update(struct bgp
*bgp
)
3299 struct zapi_cap api
;
3301 if (zclient
== NULL
) {
3302 if (BGP_DEBUG(zebra
, ZEBRA
))
3303 zlog_debug("zclient invalid");
3304 return BGP_GR_FAILURE
;
3307 /* Check if the client is connected */
3308 if ((zclient
->sock
< 0) || (zclient
->t_connect
)) {
3309 if (BGP_DEBUG(zebra
, ZEBRA
))
3310 zlog_debug("client not connected");
3311 return BGP_GR_FAILURE
;
3314 memset(&api
, 0, sizeof(struct zapi_cap
));
3315 api
.cap
= ZEBRA_CLIENT_RIB_STALE_TIME
;
3316 api
.stale_removal_time
= bgp
->rib_stale_time
;
3317 api
.vrf_id
= bgp
->vrf_id
;
3318 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES
, zclient
, &api
)
3320 if (BGP_DEBUG(zebra
, ZEBRA
))
3321 zlog_debug("error sending capability");
3322 return BGP_GR_FAILURE
;
3324 if (BGP_DEBUG(zebra
, ZEBRA
))
3325 zlog_debug("send capabilty success");
3326 return BGP_GR_SUCCESS
;