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"
40 #include "bgpd/bgpd.h"
41 #include "bgpd/bgp_route.h"
42 #include "bgpd/bgp_attr.h"
43 #include "bgpd/bgp_nexthop.h"
44 #include "bgpd/bgp_zebra.h"
45 #include "bgpd/bgp_fsm.h"
46 #include "bgpd/bgp_debug.h"
47 #include "bgpd/bgp_mpath.h"
48 #include "bgpd/bgp_nexthop.h"
49 #include "bgpd/bgp_nht.h"
50 #include "bgpd/bgp_bfd.h"
51 #include "bgpd/bgp_label.h"
53 #include "bgpd/rfapi/rfapi_backend.h"
54 #include "bgpd/rfapi/vnc_export_bgp.h"
56 #include "bgpd/bgp_evpn.h"
57 #include "bgpd/bgp_mplsvpn.h"
58 #include "bgpd/bgp_labelpool.h"
60 /* All information about zebra. */
61 struct zclient
*zclient
= NULL
;
63 /* Can we install into zebra? */
64 static inline int bgp_install_info_to_zebra(struct bgp
*bgp
)
66 if (zclient
->sock
<= 0)
69 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
75 int zclient_num_connects
;
77 /* Router-id update message from zebra. */
78 static int bgp_router_id_update(int command
, struct zclient
*zclient
,
79 zebra_size_t length
, vrf_id_t vrf_id
)
81 struct prefix router_id
;
83 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
85 if (BGP_DEBUG(zebra
, ZEBRA
)) {
86 char buf
[PREFIX2STR_BUFFER
];
87 prefix2str(&router_id
, buf
, sizeof(buf
));
88 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id
, buf
);
91 bgp_router_id_zebra_bump(vrf_id
, &router_id
);
95 /* Nexthop update message from zebra. */
96 static int bgp_read_nexthop_update(int command
, struct zclient
*zclient
,
97 zebra_size_t length
, vrf_id_t vrf_id
)
99 bgp_parse_nexthop_update(command
, vrf_id
);
103 static int bgp_read_import_check_update(int command
, struct zclient
*zclient
,
104 zebra_size_t length
, vrf_id_t vrf_id
)
106 bgp_parse_nexthop_update(command
, vrf_id
);
110 /* Set or clear interface on which unnumbered neighbor is configured. This
111 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
114 static void bgp_update_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
,
115 struct interface
*upd_ifp
)
117 struct listnode
*node
, *nnode
;
120 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
121 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)) {
124 bgp_zebra_initiate_radv(bgp
, peer
);
126 bgp_zebra_terminate_radv(bgp
, peer
);
133 static int bgp_read_fec_update(int command
, struct zclient
*zclient
,
136 bgp_parse_fec_update();
140 static void bgp_start_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
)
142 struct listnode
*node
, *nnode
;
145 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
146 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)
147 && peer
->status
!= Established
) {
148 if (peer_active(peer
))
149 BGP_EVENT_ADD(peer
, BGP_Stop
);
150 BGP_EVENT_ADD(peer
, BGP_Start
);
155 static void bgp_nbr_connected_add(struct bgp
*bgp
, struct nbr_connected
*ifc
)
157 struct listnode
*node
;
158 struct connected
*connected
;
159 struct interface
*ifp
;
162 /* Kick-off the FSM for any relevant peers only if there is a
163 * valid local address on the interface.
166 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
167 p
= connected
->address
;
168 if (p
->family
== AF_INET6
169 && IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
175 bgp_start_interface_nbrs(bgp
, ifp
);
178 static void bgp_nbr_connected_delete(struct bgp
*bgp
, struct nbr_connected
*ifc
,
181 struct listnode
*node
, *nnode
;
183 struct interface
*ifp
;
185 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
187 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)) {
188 peer
->last_reset
= PEER_DOWN_NBR_ADDR_DEL
;
189 BGP_EVENT_ADD(peer
, BGP_Stop
);
192 /* Free neighbor also, if we're asked to. */
195 listnode_delete(ifp
->nbr_connected
, ifc
);
196 nbr_connected_free(ifc
);
200 /* Inteface addition message from zebra. */
201 static int bgp_interface_add(int command
, struct zclient
*zclient
,
202 zebra_size_t length
, vrf_id_t vrf_id
)
204 struct interface
*ifp
;
207 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
208 if (!ifp
) // unexpected
211 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
212 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id
, ifp
->name
);
214 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
218 bgp_update_interface_nbrs(bgp
, ifp
, ifp
);
222 static int bgp_interface_delete(int command
, struct zclient
*zclient
,
223 zebra_size_t length
, vrf_id_t vrf_id
)
226 struct interface
*ifp
;
229 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
234 ifp
= zebra_interface_state_read(s
, vrf_id
);
235 if (!ifp
) /* This may happen if we've just unregistered for a VRF. */
238 if (BGP_DEBUG(zebra
, ZEBRA
))
239 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id
, ifp
->name
);
241 bgp_update_interface_nbrs(bgp
, ifp
, NULL
);
243 if_set_index(ifp
, IFINDEX_INTERNAL
);
247 static int bgp_interface_up(int command
, struct zclient
*zclient
,
248 zebra_size_t length
, vrf_id_t vrf_id
)
251 struct interface
*ifp
;
253 struct nbr_connected
*nc
;
254 struct listnode
*node
, *nnode
;
257 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
262 ifp
= zebra_interface_state_read(s
, vrf_id
);
267 if (BGP_DEBUG(zebra
, ZEBRA
))
268 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id
, ifp
->name
);
270 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
271 bgp_connected_add(bgp
, c
);
273 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
274 bgp_nbr_connected_add(bgp
, nc
);
279 static int bgp_interface_down(int command
, struct zclient
*zclient
,
280 zebra_size_t length
, vrf_id_t vrf_id
)
283 struct interface
*ifp
;
285 struct nbr_connected
*nc
;
286 struct listnode
*node
, *nnode
;
289 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
294 ifp
= zebra_interface_state_read(s
, vrf_id
);
298 if (BGP_DEBUG(zebra
, ZEBRA
))
299 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id
, ifp
->name
);
301 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
302 bgp_connected_delete(bgp
, c
);
304 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
305 bgp_nbr_connected_delete(bgp
, nc
, 1);
307 /* Fast external-failover */
311 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
314 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
315 #if defined(HAVE_CUMULUS)
316 /* Take down directly connected EBGP peers as well as
318 * tracked (directly connected) IBGP peers.
320 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1)
322 || bgp_bfd_is_peer_multihop(peer
)))
324 /* Take down directly connected EBGP peers */
325 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
329 if (ifp
== peer
->nexthop
.ifp
) {
330 BGP_EVENT_ADD(peer
, BGP_Stop
);
331 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
339 static int bgp_interface_address_add(int command
, struct zclient
*zclient
,
340 zebra_size_t length
, vrf_id_t vrf_id
)
342 struct connected
*ifc
;
345 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
349 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
354 if (bgp_debug_zebra(ifc
->address
)) {
355 char buf
[PREFIX2STR_BUFFER
];
356 prefix2str(ifc
->address
, buf
, sizeof(buf
));
357 zlog_debug("Rx Intf address add VRF %u IF %s addr %s", vrf_id
,
358 ifc
->ifp
->name
, buf
);
361 if (if_is_operative(ifc
->ifp
)) {
362 bgp_connected_add(bgp
, ifc
);
364 /* If we have learnt of any neighbors on this interface,
365 * check to kick off any BGP interface-based neighbors,
366 * but only if this is a link-local address.
368 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
)
369 && !list_isempty(ifc
->ifp
->nbr_connected
))
370 bgp_start_interface_nbrs(bgp
, ifc
->ifp
);
376 static int bgp_interface_address_delete(int command
, struct zclient
*zclient
,
377 zebra_size_t length
, vrf_id_t vrf_id
)
379 struct connected
*ifc
;
382 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
386 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
391 if (bgp_debug_zebra(ifc
->address
)) {
392 char buf
[PREFIX2STR_BUFFER
];
393 prefix2str(ifc
->address
, buf
, sizeof(buf
));
394 zlog_debug("Rx Intf address del VRF %u IF %s addr %s", vrf_id
,
395 ifc
->ifp
->name
, buf
);
398 if (if_is_operative(ifc
->ifp
)) {
399 bgp_connected_delete(bgp
, ifc
);
407 static int bgp_interface_nbr_address_add(int command
, struct zclient
*zclient
,
408 zebra_size_t length
, vrf_id_t vrf_id
)
410 struct nbr_connected
*ifc
= NULL
;
413 ifc
= zebra_interface_nbr_address_read(command
, zclient
->ibuf
, vrf_id
);
418 if (bgp_debug_zebra(ifc
->address
)) {
419 char buf
[PREFIX2STR_BUFFER
];
420 prefix2str(ifc
->address
, buf
, sizeof(buf
));
421 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s", vrf_id
,
422 ifc
->ifp
->name
, buf
);
425 if (if_is_operative(ifc
->ifp
)) {
426 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
428 bgp_nbr_connected_add(bgp
, ifc
);
434 static int bgp_interface_nbr_address_delete(int command
,
435 struct zclient
*zclient
,
439 struct nbr_connected
*ifc
= NULL
;
442 ifc
= zebra_interface_nbr_address_read(command
, zclient
->ibuf
, vrf_id
);
447 if (bgp_debug_zebra(ifc
->address
)) {
448 char buf
[PREFIX2STR_BUFFER
];
449 prefix2str(ifc
->address
, buf
, sizeof(buf
));
450 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s", vrf_id
,
451 ifc
->ifp
->name
, buf
);
454 if (if_is_operative(ifc
->ifp
)) {
455 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
457 bgp_nbr_connected_delete(bgp
, ifc
, 0);
460 nbr_connected_free(ifc
);
465 /* VRF update for an interface. */
466 static int bgp_interface_vrf_update(int command
, struct zclient
*zclient
,
467 zebra_size_t length
, vrf_id_t vrf_id
)
469 struct interface
*ifp
;
472 struct nbr_connected
*nc
;
473 struct listnode
*node
, *nnode
;
476 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
481 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
482 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id
,
483 ifp
->name
, new_vrf_id
);
485 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
489 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
490 bgp_connected_delete(bgp
, c
);
492 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
493 bgp_nbr_connected_delete(bgp
, nc
, 1);
495 /* Fast external-failover */
499 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
502 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
503 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
506 if (ifp
== peer
->nexthop
.ifp
)
507 BGP_EVENT_ADD(peer
, BGP_Stop
);
511 if_update_to_new_vrf(ifp
, new_vrf_id
);
513 bgp
= bgp_lookup_by_vrf_id(new_vrf_id
);
517 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
518 bgp_connected_add(bgp
, c
);
520 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
521 bgp_nbr_connected_add(bgp
, nc
);
525 /* Zebra route add and delete treatment. */
526 static int zebra_read_route(int command
, struct zclient
*zclient
,
527 zebra_size_t length
, vrf_id_t vrf_id
)
529 enum nexthop_types_t nhtype
;
530 struct zapi_route api
;
531 union g_addr nexthop
;
536 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
540 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
543 /* we completely ignore srcdest routes for now. */
544 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
547 /* ignore link-local address. */
548 if (api
.prefix
.family
== AF_INET6
549 && IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
552 nexthop
= api
.nexthops
[0].gate
;
553 ifindex
= api
.nexthops
[0].ifindex
;
554 nhtype
= api
.nexthops
[0].type
;
556 add
= (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
559 * The ADD message is actually an UPDATE and there is no
561 * for a prior redistributed route, if any. So, perform an
563 * DEL processing for the same redistributed route from any
567 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
569 bgp_redistribute_delete(bgp
, &api
.prefix
, i
,
573 /* Now perform the add/update. */
574 bgp_redistribute_add(bgp
, &api
.prefix
, &nexthop
, ifindex
,
575 nhtype
, api
.metric
, api
.type
, api
.instance
,
578 bgp_redistribute_delete(bgp
, &api
.prefix
, api
.type
,
582 if (bgp_debug_zebra(&api
.prefix
)) {
583 char buf
[2][PREFIX_STRLEN
];
585 prefix2str(&api
.prefix
, buf
[0], sizeof(buf
[0]));
587 inet_ntop(api
.prefix
.family
, &nexthop
, buf
[1],
590 "Rx route ADD VRF %u %s[%d] %s nexthop %s (type %d if %u) metric %u tag %" ROUTE_TAG_PRI
,
591 vrf_id
, zebra_route_string(api
.type
),
592 api
.instance
, buf
[0], buf
[1], nhtype
,
593 ifindex
, api
.metric
, api
.tag
);
596 "Rx route DEL VRF %u %s[%d] %s",
597 vrf_id
, zebra_route_string(api
.type
),
598 api
.instance
, buf
[0]);
605 struct interface
*if_lookup_by_ipv4(struct in_addr
*addr
, vrf_id_t vrf_id
)
608 struct listnode
*cnode
;
609 struct interface
*ifp
;
610 struct connected
*connected
;
611 struct prefix_ipv4 p
;
614 vrf
= vrf_lookup_by_id(vrf_id
);
620 p
.prefixlen
= IPV4_MAX_BITLEN
;
622 FOR_ALL_INTERFACES (vrf
, ifp
) {
623 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
624 cp
= connected
->address
;
626 if (cp
->family
== AF_INET
)
627 if (prefix_match(cp
, (struct prefix
*)&p
))
634 struct interface
*if_lookup_by_ipv4_exact(struct in_addr
*addr
, vrf_id_t vrf_id
)
637 struct listnode
*cnode
;
638 struct interface
*ifp
;
639 struct connected
*connected
;
642 vrf
= vrf_lookup_by_id(vrf_id
);
646 FOR_ALL_INTERFACES (vrf
, ifp
) {
647 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
648 cp
= connected
->address
;
650 if (cp
->family
== AF_INET
)
651 if (IPV4_ADDR_SAME(&cp
->u
.prefix4
, addr
))
658 struct interface
*if_lookup_by_ipv6(struct in6_addr
*addr
, ifindex_t ifindex
,
662 struct listnode
*cnode
;
663 struct interface
*ifp
;
664 struct connected
*connected
;
665 struct prefix_ipv6 p
;
668 vrf
= vrf_lookup_by_id(vrf_id
);
674 p
.prefixlen
= IPV6_MAX_BITLEN
;
676 FOR_ALL_INTERFACES (vrf
, ifp
) {
677 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
678 cp
= connected
->address
;
680 if (cp
->family
== AF_INET6
)
681 if (prefix_match(cp
, (struct prefix
*)&p
)) {
682 if (IN6_IS_ADDR_LINKLOCAL(
684 if (ifindex
== ifp
->ifindex
)
694 struct interface
*if_lookup_by_ipv6_exact(struct in6_addr
*addr
,
695 ifindex_t ifindex
, vrf_id_t vrf_id
)
698 struct listnode
*cnode
;
699 struct interface
*ifp
;
700 struct connected
*connected
;
703 vrf
= vrf_lookup_by_id(vrf_id
);
707 FOR_ALL_INTERFACES (vrf
, ifp
) {
708 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
709 cp
= connected
->address
;
711 if (cp
->family
== AF_INET6
)
712 if (IPV6_ADDR_SAME(&cp
->u
.prefix6
, addr
)) {
713 if (IN6_IS_ADDR_LINKLOCAL(
715 if (ifindex
== ifp
->ifindex
)
725 static int if_get_ipv6_global(struct interface
*ifp
, struct in6_addr
*addr
)
727 struct listnode
*cnode
;
728 struct connected
*connected
;
731 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
732 cp
= connected
->address
;
734 if (cp
->family
== AF_INET6
)
735 if (!IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
736 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
743 static int if_get_ipv6_local(struct interface
*ifp
, struct in6_addr
*addr
)
745 struct listnode
*cnode
;
746 struct connected
*connected
;
749 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
750 cp
= connected
->address
;
752 if (cp
->family
== AF_INET6
)
753 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
754 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
761 static int if_get_ipv4_address(struct interface
*ifp
, struct in_addr
*addr
)
763 struct listnode
*cnode
;
764 struct connected
*connected
;
767 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
768 cp
= connected
->address
;
769 if ((cp
->family
== AF_INET
)
770 && !ipv4_martian(&(cp
->u
.prefix4
))) {
771 *addr
= cp
->u
.prefix4
;
778 int bgp_nexthop_set(union sockunion
*local
, union sockunion
*remote
,
779 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
782 struct interface
*ifp
= NULL
;
784 memset(nexthop
, 0, sizeof(struct bgp_nexthop
));
791 if (local
->sa
.sa_family
== AF_INET
) {
792 nexthop
->v4
= local
->sin
.sin_addr
;
794 ifp
= if_lookup_by_name(peer
->update_if
,
797 ifp
= if_lookup_by_ipv4_exact(&local
->sin
.sin_addr
,
800 if (local
->sa
.sa_family
== AF_INET6
) {
801 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
802 if (peer
->conf_if
|| peer
->ifname
)
803 ifp
= if_lookup_by_name(peer
->conf_if
807 } else if (peer
->update_if
)
808 ifp
= if_lookup_by_name(peer
->update_if
,
811 ifp
= if_lookup_by_ipv6_exact(&local
->sin6
.sin6_addr
,
812 local
->sin6
.sin6_scope_id
,
821 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
822 if (local
->sa
.sa_family
== AF_INET
) {
824 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
827 /* There is no global nexthop. Use link-local address as
829 * global and link-local nexthop. In this scenario, the
831 * for interop is that the network admin would use a
833 * specify the global IPv6 nexthop.
835 if_get_ipv6_local(ifp
, &nexthop
->v6_global
);
836 memcpy(&nexthop
->v6_local
, &nexthop
->v6_global
,
839 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
841 if (if_lookup_by_ipv4(&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
842 peer
->shared_network
= 1;
844 peer
->shared_network
= 0;
847 /* IPv6 connection, fetch and store IPv4 local address if any. */
848 if (local
->sa
.sa_family
== AF_INET6
) {
849 struct interface
*direct
= NULL
;
852 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
853 if (!ret
&& peer
->local_id
.s_addr
)
854 nexthop
->v4
= peer
->local_id
;
857 if (!IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
858 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
861 /* If directory connected set link-local address. */
862 direct
= if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
863 remote
->sin6
.sin6_scope_id
,
866 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
868 /* Link-local address. */
870 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
872 /* If there is no global address. Set link-local
874 global. I know this break RFC specification... */
875 /* In this scenario, the expectation for interop is that
877 * network admin would use a route-map to specify the
882 memcpy(&nexthop
->v6_global
,
883 &local
->sin6
.sin6_addr
,
885 /* Always set the link-local address */
886 memcpy(&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
890 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)
891 || if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
892 remote
->sin6
.sin6_scope_id
,
894 peer
->shared_network
= 1;
896 peer
->shared_network
= 0;
899 /* KAME stack specific treatment. */
901 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_global
)
902 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
)) {
903 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
, 0);
905 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_local
)
906 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
)) {
907 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
, 0);
911 /* If we have identified the local interface, there is no error for now.
916 static struct in6_addr
*bgp_info_to_ipv6_nexthop(struct bgp_info
*info
,
919 struct in6_addr
*nexthop
= NULL
;
921 /* Only global address nexthop exists. */
922 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
) {
923 nexthop
= &info
->attr
->mp_nexthop_global
;
924 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
925 *ifindex
= info
->attr
->nh_ifindex
;
929 /* If both global and link-local address present. */
930 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
931 /* Check if route-map is set to prefer global over link-local */
932 if (info
->attr
->mp_nexthop_prefer_global
) {
933 nexthop
= &info
->attr
->mp_nexthop_global
;
934 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
935 *ifindex
= info
->attr
->nh_ifindex
;
937 /* Workaround for Cisco's nexthop bug. */
938 if (IN6_IS_ADDR_UNSPECIFIED(
939 &info
->attr
->mp_nexthop_global
)
940 && info
->peer
->su_remote
->sa
.sa_family
943 &info
->peer
->su_remote
->sin6
.sin6_addr
;
944 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
945 *ifindex
= info
->peer
->nexthop
.ifp
948 nexthop
= &info
->attr
->mp_nexthop_local
;
949 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
950 *ifindex
= info
->attr
->nh_lla_ifindex
;
958 static int bgp_table_map_apply(struct route_map
*map
, struct prefix
*p
,
959 struct bgp_info
*info
)
961 route_map_result_t ret
;
963 ret
= route_map_apply(map
, p
, RMAP_BGP
, info
);
964 bgp_attr_flush(info
->attr
);
966 if (ret
!= RMAP_DENYMATCH
)
969 if (bgp_debug_zebra(p
)) {
970 if (p
->family
== AF_INET
) {
971 char buf
[2][INET_ADDRSTRLEN
];
973 "Zebra rmap deny: IPv4 route %s/%d nexthop %s",
974 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0],
977 inet_ntop(AF_INET
, &info
->attr
->nexthop
, buf
[1],
980 if (p
->family
== AF_INET6
) {
981 char buf
[2][INET6_ADDRSTRLEN
];
983 struct in6_addr
*nexthop
;
985 nexthop
= bgp_info_to_ipv6_nexthop(info
, &ifindex
);
987 "Zebra rmap deny: IPv6 route %s/%d nexthop %s",
988 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0],
991 inet_ntop(AF_INET6
, nexthop
,
992 buf
[1], sizeof(buf
[1])));
998 static struct thread
*bgp_tm_thread_connect
;
999 static bool bgp_tm_status_connected
;
1001 static int bgp_zebra_tm_connect(struct thread
*t
)
1003 struct zclient
*zclient
;
1004 int delay
= 10, ret
= 0;
1006 zclient
= THREAD_ARG(t
);
1007 if (bgp_tm_status_connected
&& zclient
->sock
> 0)
1010 bgp_tm_status_connected
= false;
1011 ret
= tm_table_manager_connect(zclient
);
1014 zlog_warn("Error connecting to table manager!");
1015 bgp_tm_status_connected
= false;
1017 if (!bgp_tm_status_connected
)
1018 zlog_debug("Connecting to table manager. Success");
1019 bgp_tm_status_connected
= true;
1021 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1022 &bgp_tm_thread_connect
);
1026 void bgp_zebra_init_tm_connect(void)
1030 /* if already set, do nothing
1032 if (bgp_tm_thread_connect
!= NULL
)
1034 bgp_tm_status_connected
= false;
1035 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1036 &bgp_tm_thread_connect
);
1039 int bgp_zebra_get_table_range(uint32_t chunk_size
,
1040 uint32_t *start
, uint32_t *end
)
1044 if (!bgp_tm_status_connected
)
1046 ret
= tm_get_table_chunk(zclient
, chunk_size
, start
, end
);
1048 zlog_err("BGP: Error getting table chunk %u", chunk_size
);
1051 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1052 chunk_size
, *start
, *end
);
1056 static int update_ipv4nh_for_route_install(int nh_othervrf
,
1057 struct in_addr
*nexthop
,
1060 struct zapi_nexthop
*api_nh
)
1062 api_nh
->gate
.ipv4
= *nexthop
;
1064 /* Need to set fields appropriately for EVPN routes imported into
1065 * a VRF (which are programmed as onlink on l3-vni SVI) as well as
1066 * connected routes leaked into a VRF.
1069 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1070 else if (nh_othervrf
&&
1071 api_nh
->gate
.ipv4
.s_addr
== INADDR_ANY
) {
1072 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1073 api_nh
->ifindex
= attr
->nh_ifindex
;
1075 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1080 static int update_ipv6nh_for_route_install(int nh_othervrf
,
1081 struct in6_addr
*nexthop
,
1083 struct bgp_info
*ri
,
1084 struct bgp_info
*best_ri
,
1086 struct zapi_nexthop
*api_nh
)
1093 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1094 else if (nh_othervrf
) {
1095 if (IN6_IS_ADDR_UNSPECIFIED(nexthop
)) {
1096 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1097 api_nh
->ifindex
= attr
->nh_ifindex
;
1098 } else if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1101 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1102 api_nh
->ifindex
= ifindex
;
1104 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1105 api_nh
->ifindex
= 0;
1108 if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1109 if (ri
== best_ri
&&
1110 attr
->mp_nexthop_len
1111 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1112 if (ri
->peer
->nexthop
.ifp
)
1113 ifindex
= ri
->peer
->nexthop
.ifp
1116 if (ri
->peer
->conf_if
)
1117 ifindex
= ri
->peer
->ifp
->ifindex
;
1118 else if (ri
->peer
->ifname
)
1119 ifindex
= ifname2ifindex(
1121 ri
->peer
->bgp
->vrf_id
);
1122 else if (ri
->peer
->nexthop
.ifp
)
1123 ifindex
= ri
->peer
->nexthop
.ifp
1129 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1130 api_nh
->ifindex
= ifindex
;
1132 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1133 api_nh
->ifindex
= 0;
1136 api_nh
->gate
.ipv6
= *nexthop
;
1141 void bgp_zebra_announce(struct bgp_node
*rn
, struct prefix
*p
,
1142 struct bgp_info
*info
, struct bgp
*bgp
, afi_t afi
,
1145 struct zapi_route api
;
1146 struct zapi_nexthop
*api_nh
;
1148 unsigned int valid_nh_count
= 0;
1149 int has_valid_label
= 0;
1152 struct bgp_info
*mpinfo
;
1154 struct attr local_attr
;
1155 struct bgp_info local_info
;
1156 struct bgp_info
*mpinfo_cp
= &local_info
;
1159 int nh_othervrf
= 0;
1160 char buf_prefix
[PREFIX_STRLEN
]; /* filled in if we are debugging */
1161 bool is_evpn
= false;
1164 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1165 * know of this instance.
1167 if (!bgp_install_info_to_zebra(bgp
))
1170 if (bgp
->main_zebra_update_hold
)
1173 if (bgp_debug_zebra(p
))
1174 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1177 * vrf leaking support (will have only one nexthop)
1179 if (info
->extra
&& info
->extra
->bgp_orig
)
1182 /* Make Zebra API structure. */
1183 memset(&api
, 0, sizeof(api
));
1184 memcpy(&api
.rmac
, &(info
->attr
->rmac
), sizeof(struct ethaddr
));
1185 api
.vrf_id
= bgp
->vrf_id
;
1186 api
.type
= ZEBRA_ROUTE_BGP
;
1189 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1193 if (info
->type
== ZEBRA_ROUTE_BGP
1194 && info
->sub_type
== BGP_ROUTE_IMPORTED
) {
1196 /* Obtain peer from parent */
1197 if (info
->extra
&& info
->extra
->parent
)
1198 peer
= ((struct bgp_info
*)(info
->extra
->parent
))->peer
;
1201 tag
= info
->attr
->tag
;
1204 * When we create an aggregate route we must also install a
1205 * Null0 route in the RIB
1207 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1208 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
1210 /* If the route's source is EVPN, flag as such. */
1211 is_evpn
= is_route_parent_evpn(info
);
1213 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1215 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
1216 || info
->sub_type
== BGP_ROUTE_AGGREGATE
) {
1217 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1218 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1221 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1222 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1223 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1225 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1227 /* Metric is currently based on the best-path only */
1228 metric
= info
->attr
->med
;
1229 for (mpinfo
= info
; mpinfo
; mpinfo
= bgp_info_mpath_next(mpinfo
)) {
1230 if (valid_nh_count
>= multipath_num
)
1233 *mpinfo_cp
= *mpinfo
;
1235 /* Get nexthop address-family */
1236 if (p
->family
== AF_INET
1237 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
))
1238 nh_family
= AF_INET
;
1239 else if (p
->family
== AF_INET6
1240 || (p
->family
== AF_INET
1241 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
)))
1242 nh_family
= AF_INET6
;
1246 api_nh
= &api
.nexthops
[valid_nh_count
];
1247 api_nh
->vrf_id
= nh_othervrf
? info
->extra
->bgp_orig
->vrf_id
1249 if (nh_family
== AF_INET
) {
1250 if (bgp_debug_zebra(&api
.prefix
)) {
1251 if (mpinfo
->extra
) {
1253 "%s: p=%s, bgp_is_valid_label: %d",
1254 __func__
, buf_prefix
,
1260 "%s: p=%s, extra is NULL, no label",
1261 __func__
, buf_prefix
);
1265 if (bgp
->table_map
[afi
][safi
].name
|| nh_othervrf
) {
1266 /* Copy info and attributes, so the route-map
1267 apply doesn't modify the BGP route info. */
1268 local_attr
= *mpinfo
->attr
;
1269 mpinfo_cp
->attr
= &local_attr
;
1271 /* allow route-map to modify */
1272 local_attr
.nexthop
=
1273 info
->extra
->nexthop_orig
.u
1278 if (bgp
->table_map
[afi
][safi
].name
) {
1279 if (!bgp_table_map_apply(
1280 bgp
->table_map
[afi
][safi
].map
, p
,
1284 /* metric/tag is only allowed to be
1285 * overridden on 1st nexthop */
1286 if (mpinfo
== info
) {
1287 metric
= mpinfo_cp
->attr
->med
;
1288 tag
= mpinfo_cp
->attr
->tag
;
1292 nh_updated
= update_ipv4nh_for_route_install(
1294 &mpinfo_cp
->attr
->nexthop
,
1295 mpinfo_cp
->attr
, is_evpn
, api_nh
);
1298 struct in6_addr
*nexthop
;
1300 if (bgp
->table_map
[afi
][safi
].name
|| nh_othervrf
) {
1301 /* Copy info and attributes, so the route-map
1302 apply doesn't modify the BGP route info. */
1303 local_attr
= *mpinfo
->attr
;
1304 mpinfo_cp
->attr
= &local_attr
;
1306 /* allow route-map to modify */
1307 local_attr
.mp_nexthop_global
=
1308 info
->extra
->nexthop_orig
.u
1310 local_attr
.mp_nexthop_len
=
1311 BGP_ATTR_NHLEN_IPV6_GLOBAL
;
1315 if (bgp
->table_map
[afi
][safi
].name
) {
1316 /* Copy info and attributes, so the route-map
1317 apply doesn't modify the BGP route info. */
1318 local_attr
= *mpinfo
->attr
;
1319 mpinfo_cp
->attr
= &local_attr
;
1321 if (!bgp_table_map_apply(
1322 bgp
->table_map
[afi
][safi
].map
, p
,
1326 /* metric/tag is only allowed to be
1327 * overridden on 1st nexthop */
1328 if (mpinfo
== info
) {
1329 metric
= mpinfo_cp
->attr
->med
;
1330 tag
= mpinfo_cp
->attr
->tag
;
1333 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo_cp
,
1335 nh_updated
= update_ipv6nh_for_route_install(
1336 nh_othervrf
, nexthop
, ifindex
,
1337 mpinfo
, info
, is_evpn
, api_nh
);
1340 /* Did we get proper nexthop info to update zebra? */
1345 && bgp_is_valid_label(&mpinfo
->extra
->label
[0])
1346 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1347 has_valid_label
= 1;
1348 label
= label_pton(&mpinfo
->extra
->label
[0]);
1350 api_nh
->label_num
= 1;
1351 api_nh
->labels
[0] = label
;
1357 /* if this is a evpn route we don't have to include the label */
1358 if (has_valid_label
&& !(CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)))
1359 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
1361 if (info
->sub_type
!= BGP_ROUTE_AGGREGATE
)
1362 api
.nexthop_num
= valid_nh_count
;
1364 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1365 api
.metric
= metric
;
1368 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1372 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1374 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1375 api
.distance
= distance
;
1378 if (bgp_debug_zebra(p
)) {
1379 char prefix_buf
[PREFIX_STRLEN
];
1380 char nh_buf
[INET6_ADDRSTRLEN
];
1384 prefix2str(&api
.prefix
, prefix_buf
, sizeof(prefix_buf
));
1385 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1387 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1388 prefix_buf
, api
.metric
, api
.tag
, api
.nexthop_num
);
1389 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1390 api_nh
= &api
.nexthops
[i
];
1392 if (api_nh
->type
== NEXTHOP_TYPE_IFINDEX
)
1395 if (api_nh
->type
== NEXTHOP_TYPE_IPV4
)
1396 nh_family
= AF_INET
;
1398 nh_family
= AF_INET6
;
1399 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1403 label_buf
[0] = '\0';
1405 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1406 sprintf(label_buf
, "label %u",
1408 zlog_debug(" nhop [%d]: %s if %u VRF %u %s",
1409 i
+ 1, nh_buf
, api_nh
->ifindex
,
1410 api_nh
->vrf_id
, label_buf
);
1414 if (bgp_debug_zebra(p
)) {
1415 int recursion_flag
= 0;
1417 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
))
1420 zlog_debug("%s: %s: announcing to zebra (recursion %sset)",
1421 __func__
, buf_prefix
,
1422 (recursion_flag
? "" : "NOT "));
1424 zclient_route_send(valid_nh_count
? ZEBRA_ROUTE_ADD
1425 : ZEBRA_ROUTE_DELETE
,
1429 /* Announce all routes of a table to zebra */
1430 void bgp_zebra_announce_table(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1432 struct bgp_node
*rn
;
1433 struct bgp_table
*table
;
1434 struct bgp_info
*ri
;
1436 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1437 * know of this instance.
1439 if (!bgp_install_info_to_zebra(bgp
))
1442 table
= bgp
->rib
[afi
][safi
];
1446 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
1447 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1448 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
) &&
1450 (ri
->type
== ZEBRA_ROUTE_BGP
1451 && (ri
->sub_type
== BGP_ROUTE_NORMAL
1452 || ri
->sub_type
== BGP_ROUTE_IMPORTED
)))
1454 bgp_zebra_announce(rn
, &rn
->p
, ri
, bgp
, afi
,
1458 void bgp_zebra_withdraw(struct prefix
*p
, struct bgp_info
*info
,
1459 struct bgp
*bgp
, safi_t safi
)
1461 struct zapi_route api
;
1463 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1464 * know of this instance.
1466 if (!bgp_install_info_to_zebra(bgp
))
1469 memset(&api
, 0, sizeof(api
));
1470 memcpy(&api
.rmac
, &(info
->attr
->rmac
), sizeof(struct ethaddr
));
1471 api
.vrf_id
= bgp
->vrf_id
;
1472 api
.type
= ZEBRA_ROUTE_BGP
;
1476 /* If the route's source is EVPN, flag as such. */
1477 if (is_route_parent_evpn(info
))
1478 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1480 if (bgp_debug_zebra(p
)) {
1481 char buf
[PREFIX_STRLEN
];
1483 prefix2str(&api
.prefix
, buf
, sizeof(buf
));
1484 zlog_debug("Tx route delete VRF %u %s", bgp
->vrf_id
, buf
);
1487 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
1490 struct bgp_redist
*bgp_redist_lookup(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1491 unsigned short instance
)
1493 struct list
*red_list
;
1494 struct listnode
*node
;
1495 struct bgp_redist
*red
;
1497 red_list
= bgp
->redist
[afi
][type
];
1501 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1502 if (red
->instance
== instance
)
1508 struct bgp_redist
*bgp_redist_add(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1509 unsigned short instance
)
1511 struct list
*red_list
;
1512 struct bgp_redist
*red
;
1514 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1518 if (!bgp
->redist
[afi
][type
])
1519 bgp
->redist
[afi
][type
] = list_new();
1521 red_list
= bgp
->redist
[afi
][type
];
1522 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
,
1523 sizeof(struct bgp_redist
));
1524 red
->instance
= instance
;
1526 listnode_add(red_list
, red
);
1531 static void bgp_redist_del(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1532 unsigned short instance
)
1534 struct bgp_redist
*red
;
1536 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1539 listnode_delete(bgp
->redist
[afi
][type
], red
);
1540 XFREE(MTYPE_BGP_REDIST
, red
);
1541 if (!bgp
->redist
[afi
][type
]->count
)
1542 list_delete_and_null(&bgp
->redist
[afi
][type
]);
1546 /* Other routes redistribution into BGP. */
1547 int bgp_redistribute_set(struct bgp
*bgp
, afi_t afi
, int type
,
1548 unsigned short instance
)
1551 /* Return if already redistribute flag is set. */
1553 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1557 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1559 if (vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1563 if (bgp
->vrf_id
== VRF_DEFAULT
1564 && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1565 vnc_export_bgp_enable(
1566 bgp
, afi
); /* only enables if mode bits cfg'd */
1570 vrf_bitmap_set(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1574 * Don't try to register if we're not connected to Zebra or Zebra
1575 * doesn't know of this instance.
1577 * When we come up later well resend if needed.
1579 if (!bgp_install_info_to_zebra(bgp
))
1582 if (BGP_DEBUG(zebra
, ZEBRA
))
1583 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1584 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1587 /* Send distribute add message to zebra. */
1588 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1589 instance
, bgp
->vrf_id
);
1594 int bgp_redistribute_resend(struct bgp
*bgp
, afi_t afi
, int type
,
1595 unsigned short instance
)
1597 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1598 * know of this instance.
1600 if (!bgp_install_info_to_zebra(bgp
))
1603 if (BGP_DEBUG(zebra
, ZEBRA
))
1604 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1605 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1608 /* Send distribute add message to zebra. */
1609 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1610 instance
, bgp
->vrf_id
);
1611 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1612 instance
, bgp
->vrf_id
);
1617 /* Redistribute with route-map specification. */
1618 int bgp_redistribute_rmap_set(struct bgp_redist
*red
, const char *name
)
1620 if (red
->rmap
.name
&& (strcmp(red
->rmap
.name
, name
) == 0))
1624 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1625 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1626 red
->rmap
.map
= route_map_lookup_by_name(name
);
1631 /* Redistribute with metric specification. */
1632 int bgp_redistribute_metric_set(struct bgp
*bgp
, struct bgp_redist
*red
,
1633 afi_t afi
, int type
, uint32_t metric
)
1635 struct bgp_node
*rn
;
1636 struct bgp_info
*ri
;
1638 if (red
->redist_metric_flag
&& red
->redist_metric
== metric
)
1641 red
->redist_metric_flag
= 1;
1642 red
->redist_metric
= metric
;
1644 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
1645 rn
= bgp_route_next(rn
)) {
1646 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
1647 if (ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
1649 && ri
->instance
== red
->instance
) {
1650 struct attr
*old_attr
;
1651 struct attr new_attr
;
1653 bgp_attr_dup(&new_attr
, ri
->attr
);
1654 new_attr
.med
= red
->redist_metric
;
1655 old_attr
= ri
->attr
;
1656 ri
->attr
= bgp_attr_intern(&new_attr
);
1657 bgp_attr_unintern(&old_attr
);
1659 bgp_info_set_flag(rn
, ri
,
1660 BGP_INFO_ATTR_CHANGED
);
1661 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1669 /* Unset redistribution. */
1670 int bgp_redistribute_unreg(struct bgp
*bgp
, afi_t afi
, int type
,
1671 unsigned short instance
)
1673 struct bgp_redist
*red
;
1675 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1679 /* Return if zebra connection is disabled. */
1681 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1684 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1686 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1688 vrf_bitmap_unset(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1692 if (bgp_install_info_to_zebra(bgp
)) {
1693 /* Send distribute delete message to zebra. */
1694 if (BGP_DEBUG(zebra
, ZEBRA
))
1695 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1696 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1698 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
1699 type
, instance
, bgp
->vrf_id
);
1702 /* Withdraw redistributed routes from current BGP's routing table. */
1703 bgp_redistribute_withdraw(bgp
, afi
, type
, instance
);
1708 /* Unset redistribution. */
1709 int bgp_redistribute_unset(struct bgp
*bgp
, afi_t afi
, int type
,
1710 unsigned short instance
)
1712 struct bgp_redist
*red
;
1715 * vnc and vpn->vrf checks must be before red check because
1716 * they operate within bgpd irrespective of zebra connection
1717 * status. red lookup fails if there is no zebra connection.
1720 if (bgp
->vrf_id
== VRF_DEFAULT
&& type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1721 vnc_export_bgp_disable(bgp
, afi
);
1725 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1729 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1731 /* Unset route-map. */
1733 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1734 red
->rmap
.name
= NULL
;
1735 red
->rmap
.map
= NULL
;
1738 red
->redist_metric_flag
= 0;
1739 red
->redist_metric
= 0;
1741 bgp_redist_del(bgp
, afi
, type
, instance
);
1746 /* Update redistribute vrf bitmap during triggers like
1747 restart networking or delete/add VRFs */
1748 void bgp_update_redist_vrf_bitmaps(struct bgp
*bgp
, vrf_id_t old_vrf_id
)
1753 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1754 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1755 if ((old_vrf_id
== VRF_UNKNOWN
)
1756 || vrf_bitmap_check(zclient
->redist
[afi
][i
],
1758 vrf_bitmap_unset(zclient
->redist
[afi
][i
],
1760 vrf_bitmap_set(zclient
->redist
[afi
][i
],
1766 void bgp_zclient_reset(void)
1768 zclient_reset(zclient
);
1771 /* Register this instance with Zebra. Invoked upon connect (for
1772 * default instance) and when other VRFs are learnt (or created and
1775 void bgp_zebra_instance_register(struct bgp
*bgp
)
1777 /* Don't try to register if we're not connected to Zebra */
1778 if (!zclient
|| zclient
->sock
< 0)
1781 if (BGP_DEBUG(zebra
, ZEBRA
))
1782 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
1784 /* Register for router-id, interfaces, redistributed routes. */
1785 zclient_send_reg_requests(zclient
, bgp
->vrf_id
);
1787 /* For default instance, register to learn about VNIs, if appropriate.
1789 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& is_evpn_enabled())
1790 bgp_zebra_advertise_all_vni(bgp
, 1);
1793 /* Deregister this instance with Zebra. Invoked upon the instance
1794 * being deleted (default or VRF) and it is already registered.
1796 void bgp_zebra_instance_deregister(struct bgp
*bgp
)
1798 /* Don't try to deregister if we're not connected to Zebra */
1799 if (zclient
->sock
< 0)
1802 if (BGP_DEBUG(zebra
, ZEBRA
))
1803 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
1805 /* For default instance, unregister learning about VNIs, if appropriate.
1807 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& is_evpn_enabled())
1808 bgp_zebra_advertise_all_vni(bgp
, 0);
1810 /* Deregister for router-id, interfaces, redistributed routes. */
1811 zclient_send_dereg_requests(zclient
, bgp
->vrf_id
);
1814 void bgp_zebra_initiate_radv(struct bgp
*bgp
, struct peer
*peer
)
1816 int ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
1818 /* Don't try to initiate if we're not connected to Zebra */
1819 if (zclient
->sock
< 0)
1822 if (BGP_DEBUG(zebra
, ZEBRA
))
1823 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
,
1826 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 1,
1830 void bgp_zebra_terminate_radv(struct bgp
*bgp
, struct peer
*peer
)
1832 /* Don't try to terminate if we're not connected to Zebra */
1833 if (zclient
->sock
< 0)
1836 if (BGP_DEBUG(zebra
, ZEBRA
))
1837 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
,
1840 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 0, 0);
1843 int bgp_zebra_advertise_subnet(struct bgp
*bgp
, int advertise
, vni_t vni
)
1845 struct stream
*s
= NULL
;
1848 if (!zclient
|| zclient
->sock
< 0)
1851 /* Don't try to register if Zebra doesn't know of this instance. */
1852 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1858 zclient_create_header(s
, ZEBRA_ADVERTISE_SUBNET
, bgp
->vrf_id
);
1859 stream_putc(s
, advertise
);
1860 stream_put3(s
, vni
);
1861 stream_putw_at(s
, 0, stream_get_endp(s
));
1863 return zclient_send_message(zclient
);
1866 int bgp_zebra_advertise_gw_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
1868 struct stream
*s
= NULL
;
1871 if (!zclient
|| zclient
->sock
< 0)
1874 /* Don't try to register if Zebra doesn't know of this instance. */
1875 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1881 zclient_create_header(s
, ZEBRA_ADVERTISE_DEFAULT_GW
, bgp
->vrf_id
);
1882 stream_putc(s
, advertise
);
1883 stream_put3(s
, vni
);
1884 stream_putw_at(s
, 0, stream_get_endp(s
));
1886 return zclient_send_message(zclient
);
1889 int bgp_zebra_advertise_all_vni(struct bgp
*bgp
, int advertise
)
1894 if (!zclient
|| zclient
->sock
< 0)
1897 /* Don't try to register if Zebra doesn't know of this instance. */
1898 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1904 zclient_create_header(s
, ZEBRA_ADVERTISE_ALL_VNI
, bgp
->vrf_id
);
1905 stream_putc(s
, advertise
);
1906 stream_putw_at(s
, 0, stream_get_endp(s
));
1908 return zclient_send_message(zclient
);
1911 /* BGP has established connection with Zebra. */
1912 static void bgp_zebra_connected(struct zclient
*zclient
)
1916 zclient_num_connects
++; /* increment even if not responding */
1918 /* At this point, we may or may not have BGP instances configured, but
1919 * we're only interested in the default VRF (others wouldn't have learnt
1920 * the VRF from Zebra yet.)
1922 bgp
= bgp_get_default();
1926 bgp_zebra_instance_register(bgp
);
1928 /* Send the client registration */
1929 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
1931 /* tell label pool that zebra is connected */
1932 bgp_lp_event_zebra_up();
1934 /* TODO - What if we have peers and networks configured, do we have to
1939 static int bgp_zebra_process_local_l3vni(int cmd
, struct zclient
*zclient
,
1940 zebra_size_t length
, vrf_id_t vrf_id
)
1943 char buf
[ETHER_ADDR_STRLEN
];
1945 struct ethaddr rmac
;
1946 struct in_addr originator_ip
;
1949 memset(&rmac
, 0, sizeof(struct ethaddr
));
1950 memset(&originator_ip
, 0, sizeof(struct in_addr
));
1952 l3vni
= stream_getl(s
);
1953 if (cmd
== ZEBRA_L3VNI_ADD
) {
1954 stream_get(&rmac
, s
, sizeof(struct ethaddr
));
1955 originator_ip
.s_addr
= stream_get_ipv4(s
);
1956 stream_get(&filter
, s
, sizeof(int));
1959 if (BGP_DEBUG(zebra
, ZEBRA
))
1960 zlog_debug("Rx L3-VNI %s VRF %s VNI %u RMAC %s filter %s",
1961 (cmd
== ZEBRA_L3VNI_ADD
) ? "add" : "del",
1962 vrf_id_to_name(vrf_id
), l3vni
,
1963 prefix_mac2str(&rmac
, buf
, sizeof(buf
)),
1964 filter
? "prefix-routes-only" : "none");
1966 if (cmd
== ZEBRA_L3VNI_ADD
)
1967 bgp_evpn_local_l3vni_add(l3vni
, vrf_id
, &rmac
, originator_ip
,
1970 bgp_evpn_local_l3vni_del(l3vni
, vrf_id
);
1975 static int bgp_zebra_process_local_vni(int command
, struct zclient
*zclient
,
1976 zebra_size_t length
, vrf_id_t vrf_id
)
1981 struct in_addr vtep_ip
= {INADDR_ANY
};
1982 vrf_id_t tenant_vrf_id
= VRF_DEFAULT
;
1985 vni
= stream_getl(s
);
1986 if (command
== ZEBRA_VNI_ADD
) {
1987 vtep_ip
.s_addr
= stream_get_ipv4(s
);
1988 stream_get(&tenant_vrf_id
, s
, sizeof(vrf_id_t
));
1991 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1995 if (BGP_DEBUG(zebra
, ZEBRA
))
1996 zlog_debug("Rx VNI %s VRF %s VNI %u tenant-vrf %s",
1997 (command
== ZEBRA_VNI_ADD
) ? "add" : "del",
1998 vrf_id_to_name(vrf_id
), vni
,
1999 vrf_id_to_name(tenant_vrf_id
));
2001 if (command
== ZEBRA_VNI_ADD
)
2002 return bgp_evpn_local_vni_add(
2003 bgp
, vni
, vtep_ip
.s_addr
? vtep_ip
: bgp
->router_id
,
2006 return bgp_evpn_local_vni_del(bgp
, vni
);
2009 static int bgp_zebra_process_local_macip(int command
, struct zclient
*zclient
,
2010 zebra_size_t length
, vrf_id_t vrf_id
)
2018 char buf
[ETHER_ADDR_STRLEN
];
2019 char buf1
[INET6_ADDRSTRLEN
];
2022 memset(&ip
, 0, sizeof(ip
));
2024 vni
= stream_getl(s
);
2025 stream_get(&mac
.octet
, s
, ETH_ALEN
);
2026 ipa_len
= stream_getl(s
);
2027 if (ipa_len
!= 0 && ipa_len
!= IPV4_MAX_BYTELEN
2028 && ipa_len
!= IPV6_MAX_BYTELEN
) {
2029 zlog_err("%u:Recv MACIP %s with invalid IP addr length %d",
2030 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
2037 (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
: IPADDR_V6
;
2038 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
2040 flags
= stream_getc(s
);
2042 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2046 if (BGP_DEBUG(zebra
, ZEBRA
))
2047 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u",
2048 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
2049 flags
, prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2050 ipaddr2str(&ip
, buf1
, sizeof(buf1
)), vni
);
2052 if (command
== ZEBRA_MACIP_ADD
)
2053 return bgp_evpn_local_macip_add(bgp
, vni
, &mac
, &ip
, flags
);
2055 return bgp_evpn_local_macip_del(bgp
, vni
, &mac
, &ip
);
2058 static void bgp_zebra_process_local_ip_prefix(int cmd
, struct zclient
*zclient
,
2059 zebra_size_t length
,
2062 struct stream
*s
= NULL
;
2063 struct bgp
*bgp_vrf
= NULL
;
2065 char buf
[PREFIX_STRLEN
];
2067 memset(&p
, 0, sizeof(struct prefix
));
2069 stream_get(&p
, s
, sizeof(struct prefix
));
2071 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
2075 if (BGP_DEBUG(zebra
, ZEBRA
))
2076 zlog_debug("Recv prefix %s %s on vrf %s",
2077 prefix2str(&p
, buf
, sizeof(buf
)),
2078 (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) ? "ADD" : "DEL",
2079 vrf_id_to_name(vrf_id
));
2081 if (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) {
2083 if (p
.family
== AF_INET
)
2084 return bgp_evpn_advertise_type5_route(
2085 bgp_vrf
, &p
, NULL
, AFI_IP
, SAFI_UNICAST
);
2087 return bgp_evpn_advertise_type5_route(
2088 bgp_vrf
, &p
, NULL
, AFI_IP6
, SAFI_UNICAST
);
2091 if (p
.family
== AF_INET
)
2092 return bgp_evpn_withdraw_type5_route(
2093 bgp_vrf
, &p
, AFI_IP
, SAFI_UNICAST
);
2095 return bgp_evpn_withdraw_type5_route(
2096 bgp_vrf
, &p
, AFI_IP6
, SAFI_UNICAST
);
2100 static void bgp_zebra_process_label_chunk(
2102 struct zclient
*zclient
,
2103 zebra_size_t length
,
2106 struct stream
*s
= NULL
;
2107 uint8_t response_keep
;
2112 STREAM_GETC(s
, response_keep
);
2113 STREAM_GETL(s
, first
);
2114 STREAM_GETL(s
, last
);
2117 first
< MPLS_LABEL_UNRESERVED_MIN
||
2118 last
> MPLS_LABEL_UNRESERVED_MAX
) {
2120 zlog_err("%s: Invalid Label chunk: %u - %u",
2121 __func__
, first
, last
);
2124 if (BGP_DEBUG(zebra
, ZEBRA
)) {
2125 zlog_debug("Label Chunk assign: %u - %u (%u) ",
2126 first
, last
, response_keep
);
2129 bgp_lp_event_chunk(response_keep
, first
, last
);
2131 stream_failure
: /* for STREAM_GETX */
2135 extern struct zebra_privs_t bgpd_privs
;
2137 void bgp_zebra_init(struct thread_master
*master
)
2139 zclient_num_connects
= 0;
2141 /* Set default values. */
2142 zclient
= zclient_new_notify(master
, &zclient_options_default
);
2143 zclient_init(zclient
, ZEBRA_ROUTE_BGP
, 0, &bgpd_privs
);
2144 zclient
->zebra_connected
= bgp_zebra_connected
;
2145 zclient
->router_id_update
= bgp_router_id_update
;
2146 zclient
->interface_add
= bgp_interface_add
;
2147 zclient
->interface_delete
= bgp_interface_delete
;
2148 zclient
->interface_address_add
= bgp_interface_address_add
;
2149 zclient
->interface_address_delete
= bgp_interface_address_delete
;
2150 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
2151 zclient
->interface_nbr_address_delete
=
2152 bgp_interface_nbr_address_delete
;
2153 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
2154 zclient
->redistribute_route_add
= zebra_read_route
;
2155 zclient
->redistribute_route_del
= zebra_read_route
;
2156 zclient
->interface_up
= bgp_interface_up
;
2157 zclient
->interface_down
= bgp_interface_down
;
2158 zclient
->nexthop_update
= bgp_read_nexthop_update
;
2159 zclient
->import_check_update
= bgp_read_import_check_update
;
2160 zclient
->fec_update
= bgp_read_fec_update
;
2161 zclient
->local_vni_add
= bgp_zebra_process_local_vni
;
2162 zclient
->local_vni_del
= bgp_zebra_process_local_vni
;
2163 zclient
->local_macip_add
= bgp_zebra_process_local_macip
;
2164 zclient
->local_macip_del
= bgp_zebra_process_local_macip
;
2165 zclient
->local_l3vni_add
= bgp_zebra_process_local_l3vni
;
2166 zclient
->local_l3vni_del
= bgp_zebra_process_local_l3vni
;
2167 zclient
->local_ip_prefix_add
= bgp_zebra_process_local_ip_prefix
;
2168 zclient
->local_ip_prefix_del
= bgp_zebra_process_local_ip_prefix
;
2169 zclient
->label_chunk
= bgp_zebra_process_label_chunk
;
2172 void bgp_zebra_destroy(void)
2174 if (zclient
== NULL
)
2176 zclient_stop(zclient
);
2177 zclient_free(zclient
);
2181 int bgp_zebra_num_connects(void)
2183 return zclient_num_connects
;