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]));
586 inet_ntop(api
.prefix
.family
, &nexthop
, buf
[1], sizeof(buf
[1]));
588 "Rx route %s VRF %u %s[%d] %s "
589 "nexthop %s metric %u tag %" ROUTE_TAG_PRI
,
590 (add
) ? "add" : "delete", vrf_id
,
591 zebra_route_string(api
.type
), api
.instance
, buf
[0],
592 buf
[1], api
.metric
, api
.tag
);
598 struct interface
*if_lookup_by_ipv4(struct in_addr
*addr
, vrf_id_t vrf_id
)
601 struct listnode
*cnode
;
602 struct interface
*ifp
;
603 struct connected
*connected
;
604 struct prefix_ipv4 p
;
607 vrf
= vrf_lookup_by_id(vrf_id
);
613 p
.prefixlen
= IPV4_MAX_BITLEN
;
615 FOR_ALL_INTERFACES (vrf
, ifp
) {
616 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
617 cp
= connected
->address
;
619 if (cp
->family
== AF_INET
)
620 if (prefix_match(cp
, (struct prefix
*)&p
))
627 struct interface
*if_lookup_by_ipv4_exact(struct in_addr
*addr
, vrf_id_t vrf_id
)
630 struct listnode
*cnode
;
631 struct interface
*ifp
;
632 struct connected
*connected
;
635 vrf
= vrf_lookup_by_id(vrf_id
);
639 FOR_ALL_INTERFACES (vrf
, ifp
) {
640 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
641 cp
= connected
->address
;
643 if (cp
->family
== AF_INET
)
644 if (IPV4_ADDR_SAME(&cp
->u
.prefix4
, addr
))
651 struct interface
*if_lookup_by_ipv6(struct in6_addr
*addr
, ifindex_t ifindex
,
655 struct listnode
*cnode
;
656 struct interface
*ifp
;
657 struct connected
*connected
;
658 struct prefix_ipv6 p
;
661 vrf
= vrf_lookup_by_id(vrf_id
);
667 p
.prefixlen
= IPV6_MAX_BITLEN
;
669 FOR_ALL_INTERFACES (vrf
, ifp
) {
670 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
671 cp
= connected
->address
;
673 if (cp
->family
== AF_INET6
)
674 if (prefix_match(cp
, (struct prefix
*)&p
)) {
675 if (IN6_IS_ADDR_LINKLOCAL(
677 if (ifindex
== ifp
->ifindex
)
687 struct interface
*if_lookup_by_ipv6_exact(struct in6_addr
*addr
,
688 ifindex_t ifindex
, vrf_id_t vrf_id
)
691 struct listnode
*cnode
;
692 struct interface
*ifp
;
693 struct connected
*connected
;
696 vrf
= vrf_lookup_by_id(vrf_id
);
700 FOR_ALL_INTERFACES (vrf
, ifp
) {
701 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
702 cp
= connected
->address
;
704 if (cp
->family
== AF_INET6
)
705 if (IPV6_ADDR_SAME(&cp
->u
.prefix6
, addr
)) {
706 if (IN6_IS_ADDR_LINKLOCAL(
708 if (ifindex
== ifp
->ifindex
)
718 static int if_get_ipv6_global(struct interface
*ifp
, struct in6_addr
*addr
)
720 struct listnode
*cnode
;
721 struct connected
*connected
;
724 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
725 cp
= connected
->address
;
727 if (cp
->family
== AF_INET6
)
728 if (!IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
729 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
736 static int if_get_ipv6_local(struct interface
*ifp
, struct in6_addr
*addr
)
738 struct listnode
*cnode
;
739 struct connected
*connected
;
742 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
743 cp
= connected
->address
;
745 if (cp
->family
== AF_INET6
)
746 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
747 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
754 static int if_get_ipv4_address(struct interface
*ifp
, struct in_addr
*addr
)
756 struct listnode
*cnode
;
757 struct connected
*connected
;
760 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
761 cp
= connected
->address
;
762 if ((cp
->family
== AF_INET
)
763 && !ipv4_martian(&(cp
->u
.prefix4
))) {
764 *addr
= cp
->u
.prefix4
;
771 int bgp_nexthop_set(union sockunion
*local
, union sockunion
*remote
,
772 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
775 struct interface
*ifp
= NULL
;
777 memset(nexthop
, 0, sizeof(struct bgp_nexthop
));
784 if (local
->sa
.sa_family
== AF_INET
) {
785 nexthop
->v4
= local
->sin
.sin_addr
;
787 ifp
= if_lookup_by_name(peer
->update_if
,
790 ifp
= if_lookup_by_ipv4_exact(&local
->sin
.sin_addr
,
793 if (local
->sa
.sa_family
== AF_INET6
) {
794 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
795 if (peer
->conf_if
|| peer
->ifname
)
796 ifp
= if_lookup_by_name(peer
->conf_if
800 } else if (peer
->update_if
)
801 ifp
= if_lookup_by_name(peer
->update_if
,
804 ifp
= if_lookup_by_ipv6_exact(&local
->sin6
.sin6_addr
,
805 local
->sin6
.sin6_scope_id
,
814 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
815 if (local
->sa
.sa_family
== AF_INET
) {
817 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
820 /* There is no global nexthop. Use link-local address as
822 * global and link-local nexthop. In this scenario, the
824 * for interop is that the network admin would use a
826 * specify the global IPv6 nexthop.
828 if_get_ipv6_local(ifp
, &nexthop
->v6_global
);
829 memcpy(&nexthop
->v6_local
, &nexthop
->v6_global
,
832 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
834 if (if_lookup_by_ipv4(&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
835 peer
->shared_network
= 1;
837 peer
->shared_network
= 0;
840 /* IPv6 connection, fetch and store IPv4 local address if any. */
841 if (local
->sa
.sa_family
== AF_INET6
) {
842 struct interface
*direct
= NULL
;
845 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
846 if (!ret
&& peer
->local_id
.s_addr
)
847 nexthop
->v4
= peer
->local_id
;
850 if (!IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
851 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
854 /* If directory connected set link-local address. */
855 direct
= if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
856 remote
->sin6
.sin6_scope_id
,
859 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
861 /* Link-local address. */
863 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
865 /* If there is no global address. Set link-local
867 global. I know this break RFC specification... */
868 /* In this scenario, the expectation for interop is that
870 * network admin would use a route-map to specify the
875 memcpy(&nexthop
->v6_global
,
876 &local
->sin6
.sin6_addr
,
878 /* Always set the link-local address */
879 memcpy(&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
883 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)
884 || if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
885 remote
->sin6
.sin6_scope_id
,
887 peer
->shared_network
= 1;
889 peer
->shared_network
= 0;
892 /* KAME stack specific treatment. */
894 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_global
)
895 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
)) {
896 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
, 0);
898 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_local
)
899 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
)) {
900 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
, 0);
904 /* If we have identified the local interface, there is no error for now.
909 static struct in6_addr
*bgp_info_to_ipv6_nexthop(struct bgp_info
*info
)
911 struct in6_addr
*nexthop
= NULL
;
913 /* Only global address nexthop exists. */
914 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
)
915 nexthop
= &info
->attr
->mp_nexthop_global
;
917 /* If both global and link-local address present. */
918 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
919 /* Check if route-map is set to prefer global over link-local */
920 if (info
->attr
->mp_nexthop_prefer_global
)
921 nexthop
= &info
->attr
->mp_nexthop_global
;
923 /* Workaround for Cisco's nexthop bug. */
924 if (IN6_IS_ADDR_UNSPECIFIED(
925 &info
->attr
->mp_nexthop_global
)
926 && info
->peer
->su_remote
->sa
.sa_family
== AF_INET6
)
928 &info
->peer
->su_remote
->sin6
.sin6_addr
;
930 nexthop
= &info
->attr
->mp_nexthop_local
;
937 static int bgp_table_map_apply(struct route_map
*map
, struct prefix
*p
,
938 struct bgp_info
*info
)
940 route_map_result_t ret
;
942 ret
= route_map_apply(map
, p
, RMAP_BGP
, info
);
943 bgp_attr_flush(info
->attr
);
945 if (ret
!= RMAP_DENYMATCH
)
948 if (bgp_debug_zebra(p
)) {
949 if (p
->family
== AF_INET
) {
950 char buf
[2][INET_ADDRSTRLEN
];
952 "Zebra rmap deny: IPv4 route %s/%d nexthop %s",
953 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0],
956 inet_ntop(AF_INET
, &info
->attr
->nexthop
, buf
[1],
959 if (p
->family
== AF_INET6
) {
960 char buf
[2][INET6_ADDRSTRLEN
];
962 "Zebra rmap deny: IPv6 route %s/%d nexthop %s",
963 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0],
967 bgp_info_to_ipv6_nexthop(info
),
968 buf
[1], sizeof(buf
[1])));
974 static struct thread
*bgp_tm_thread_connect
;
975 static bool bgp_tm_status_connected
;
977 static int bgp_zebra_tm_connect(struct thread
*t
)
979 struct zclient
*zclient
;
980 int delay
= 10, ret
= 0;
982 zclient
= THREAD_ARG(t
);
983 if (bgp_tm_status_connected
&& zclient
->sock
> 0)
986 bgp_tm_status_connected
= false;
987 ret
= tm_table_manager_connect(zclient
);
990 zlog_warn("Error connecting to table manager!");
991 bgp_tm_status_connected
= false;
993 if (!bgp_tm_status_connected
)
994 zlog_debug("Connecting to table manager. Success");
995 bgp_tm_status_connected
= true;
997 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
998 &bgp_tm_thread_connect
);
1002 void bgp_zebra_init_tm_connect(void)
1006 /* if already set, do nothing
1008 if (bgp_tm_thread_connect
!= NULL
)
1010 bgp_tm_status_connected
= false;
1011 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1012 &bgp_tm_thread_connect
);
1015 int bgp_zebra_get_table_range(uint32_t chunk_size
,
1016 uint32_t *start
, uint32_t *end
)
1020 if (!bgp_tm_status_connected
)
1022 ret
= tm_get_table_chunk(zclient
, chunk_size
, start
, end
);
1024 zlog_err("BGP: Error getting table chunk %u", chunk_size
);
1027 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1028 chunk_size
, *start
, *end
);
1032 void bgp_zebra_announce(struct bgp_node
*rn
, struct prefix
*p
,
1033 struct bgp_info
*info
, struct bgp
*bgp
, afi_t afi
,
1036 struct zapi_route api
;
1037 struct zapi_nexthop
*api_nh
;
1039 unsigned int valid_nh_count
= 0;
1040 int has_valid_label
= 0;
1043 struct bgp_info
*mpinfo
;
1045 struct attr local_attr
;
1046 struct bgp_info local_info
;
1047 struct bgp_info
*mpinfo_cp
= &local_info
;
1050 int nh_othervrf
= 0;
1051 char buf_prefix
[PREFIX_STRLEN
]; /* filled in if we are debugging */
1053 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1054 * know of this instance.
1056 if (!bgp_install_info_to_zebra(bgp
))
1059 if (bgp
->main_zebra_update_hold
)
1062 if (bgp_debug_zebra(p
))
1063 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1066 * vrf leaking support (will have only one nexthop)
1068 if (info
->extra
&& info
->extra
->bgp_orig
)
1071 /* Make Zebra API structure. */
1072 memset(&api
, 0, sizeof(api
));
1073 memcpy(&api
.rmac
, &(info
->attr
->rmac
), sizeof(struct ethaddr
));
1074 api
.vrf_id
= bgp
->vrf_id
;
1075 api
.type
= ZEBRA_ROUTE_BGP
;
1078 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1082 if (info
->type
== ZEBRA_ROUTE_BGP
1083 && info
->sub_type
== BGP_ROUTE_IMPORTED
) {
1085 struct bgp_info
*bi
;
1088 * Look at parent chain for peer sort
1090 for (bi
= info
; bi
->extra
&& bi
->extra
->parent
;
1091 bi
= bi
->extra
->parent
) {
1093 peer
= ((struct bgp_info
*)(bi
->extra
->parent
))->peer
;
1097 tag
= info
->attr
->tag
;
1099 /* When we create an aggregate route we must also install a Null0 route
1102 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1103 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
1105 /* If the route's source is EVPN, flag as such. */
1106 if (is_route_parent_evpn(info
))
1107 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1109 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
1110 || info
->sub_type
== BGP_ROUTE_AGGREGATE
) {
1111 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1112 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1115 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1116 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1117 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1119 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1121 /* Metric is currently based on the best-path only */
1122 metric
= info
->attr
->med
;
1123 for (mpinfo
= info
; mpinfo
; mpinfo
= bgp_info_mpath_next(mpinfo
)) {
1124 if (valid_nh_count
>= multipath_num
)
1127 *mpinfo_cp
= *mpinfo
;
1129 /* Get nexthop address-family */
1130 if (p
->family
== AF_INET
1131 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
))
1132 nh_family
= AF_INET
;
1133 else if (p
->family
== AF_INET6
1134 || (p
->family
== AF_INET
1135 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
)))
1136 nh_family
= AF_INET6
;
1140 api_nh
= &api
.nexthops
[valid_nh_count
];
1141 api_nh
->vrf_id
= nh_othervrf
? info
->extra
->bgp_orig
->vrf_id
1143 if (nh_family
== AF_INET
) {
1144 struct in_addr
*nexthop
;
1146 if (bgp_debug_zebra(&api
.prefix
)) {
1147 if (mpinfo
->extra
) {
1149 "%s: p=%s, bgp_is_valid_label: %d",
1150 __func__
, buf_prefix
,
1156 "%s: p=%s, extra is NULL, no label",
1157 __func__
, buf_prefix
);
1161 if (bgp
->table_map
[afi
][safi
].name
|| nh_othervrf
) {
1162 /* Copy info and attributes, so the route-map
1163 apply doesn't modify the BGP route info. */
1164 local_attr
= *mpinfo
->attr
;
1165 mpinfo_cp
->attr
= &local_attr
;
1167 /* allow route-map to modify */
1168 local_attr
.nexthop
=
1169 info
->extra
->nexthop_orig
.u
1174 if (bgp
->table_map
[afi
][safi
].name
) {
1175 if (!bgp_table_map_apply(
1176 bgp
->table_map
[afi
][safi
].map
, p
,
1180 /* metric/tag is only allowed to be
1181 * overridden on 1st nexthop */
1182 if (mpinfo
== info
) {
1183 metric
= mpinfo_cp
->attr
->med
;
1184 tag
= mpinfo_cp
->attr
->tag
;
1188 nexthop
= &mpinfo_cp
->attr
->nexthop
;
1189 api_nh
->gate
.ipv4
= *nexthop
;
1191 /* EVPN type-2 routes are
1192 programmed as onlink on l3-vni SVI
1194 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1195 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1197 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1200 struct in6_addr
*nexthop
;
1204 if (bgp
->table_map
[afi
][safi
].name
|| nh_othervrf
) {
1205 /* Copy info and attributes, so the route-map
1206 apply doesn't modify the BGP route info. */
1207 local_attr
= *mpinfo
->attr
;
1208 mpinfo_cp
->attr
= &local_attr
;
1210 /* allow route-map to modify */
1211 local_attr
.mp_nexthop_global
=
1212 info
->extra
->nexthop_orig
.u
1214 local_attr
.mp_nexthop_len
=
1215 BGP_ATTR_NHLEN_IPV6_GLOBAL
;
1219 if (bgp
->table_map
[afi
][safi
].name
) {
1220 /* Copy info and attributes, so the route-map
1221 apply doesn't modify the BGP route info. */
1222 local_attr
= *mpinfo
->attr
;
1223 mpinfo_cp
->attr
= &local_attr
;
1225 if (!bgp_table_map_apply(
1226 bgp
->table_map
[afi
][safi
].map
, p
,
1230 /* metric/tag is only allowed to be
1231 * overridden on 1st nexthop */
1232 if (mpinfo
== info
) {
1233 metric
= mpinfo_cp
->attr
->med
;
1234 tag
= mpinfo_cp
->attr
->tag
;
1237 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo_cp
);
1239 if ((mpinfo
== info
)
1240 && mpinfo
->attr
->mp_nexthop_len
1241 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1242 if (mpinfo
->peer
->nexthop
.ifp
)
1243 ifindex
= mpinfo
->peer
->nexthop
.ifp
1247 if (mpinfo
->peer
->conf_if
)
1248 ifindex
= mpinfo
->peer
->ifp
->ifindex
;
1249 else if (mpinfo
->peer
->ifname
)
1250 ifindex
= ifname2ifindex(
1251 mpinfo
->peer
->ifname
,
1253 else if (mpinfo
->peer
->nexthop
.ifp
)
1254 ifindex
= mpinfo
->peer
->nexthop
.ifp
1258 if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1264 api_nh
->gate
.ipv6
= *nexthop
;
1265 api_nh
->ifindex
= ifindex
;
1266 api_nh
->type
= ifindex
? NEXTHOP_TYPE_IPV6_IFINDEX
1267 : NEXTHOP_TYPE_IPV6
;
1271 && bgp_is_valid_label(&mpinfo
->extra
->label
[0])
1272 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1273 has_valid_label
= 1;
1274 label
= label_pton(&mpinfo
->extra
->label
[0]);
1276 api_nh
->label_num
= 1;
1277 api_nh
->labels
[0] = label
;
1282 /* if this is a evpn route we don't have to include the label */
1283 if (has_valid_label
&& !(CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)))
1284 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
1286 if (info
->sub_type
!= BGP_ROUTE_AGGREGATE
)
1287 api
.nexthop_num
= valid_nh_count
;
1289 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1290 api
.metric
= metric
;
1293 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1297 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1299 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1300 api
.distance
= distance
;
1303 if (bgp_debug_zebra(p
)) {
1304 char prefix_buf
[PREFIX_STRLEN
];
1305 char nh_buf
[INET6_ADDRSTRLEN
];
1309 prefix2str(&api
.prefix
, prefix_buf
, sizeof(prefix_buf
));
1310 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1312 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1313 prefix_buf
, api
.metric
, api
.tag
, api
.nexthop_num
);
1314 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1315 api_nh
= &api
.nexthops
[i
];
1317 if (api_nh
->type
== NEXTHOP_TYPE_IPV4
)
1318 nh_family
= AF_INET
;
1320 nh_family
= AF_INET6
;
1321 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1324 label_buf
[0] = '\0';
1326 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1327 sprintf(label_buf
, "label %u",
1329 zlog_debug(" nhop [%d]: %s %s", i
+ 1, nh_buf
,
1334 if (bgp_debug_zebra(p
)) {
1335 int recursion_flag
= 0;
1337 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
))
1340 zlog_debug("%s: %s: announcing to zebra (recursion %sset)",
1341 __func__
, buf_prefix
,
1342 (recursion_flag
? "" : "NOT "));
1344 zclient_route_send(valid_nh_count
? ZEBRA_ROUTE_ADD
1345 : ZEBRA_ROUTE_DELETE
,
1349 /* Announce all routes of a table to zebra */
1350 void bgp_zebra_announce_table(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1352 struct bgp_node
*rn
;
1353 struct bgp_table
*table
;
1354 struct bgp_info
*ri
;
1356 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1357 * know of this instance.
1359 if (!bgp_install_info_to_zebra(bgp
))
1362 table
= bgp
->rib
[afi
][safi
];
1366 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
1367 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1368 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
) &&
1370 (ri
->type
== ZEBRA_ROUTE_BGP
1371 && (ri
->sub_type
== BGP_ROUTE_NORMAL
1372 || ri
->sub_type
== BGP_ROUTE_IMPORTED
)))
1374 bgp_zebra_announce(rn
, &rn
->p
, ri
, bgp
, afi
,
1378 void bgp_zebra_withdraw(struct prefix
*p
, struct bgp_info
*info
,
1379 struct bgp
*bgp
, safi_t safi
)
1381 struct zapi_route api
;
1383 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1384 * know of this instance.
1386 if (!bgp_install_info_to_zebra(bgp
))
1389 memset(&api
, 0, sizeof(api
));
1390 memcpy(&api
.rmac
, &(info
->attr
->rmac
), sizeof(struct ethaddr
));
1391 api
.vrf_id
= bgp
->vrf_id
;
1392 api
.type
= ZEBRA_ROUTE_BGP
;
1396 /* If the route's source is EVPN, flag as such. */
1397 if (is_route_parent_evpn(info
))
1398 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1400 if (bgp_debug_zebra(p
)) {
1401 char buf
[PREFIX_STRLEN
];
1403 prefix2str(&api
.prefix
, buf
, sizeof(buf
));
1404 zlog_debug("Tx route delete VRF %u %s", bgp
->vrf_id
, buf
);
1407 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
1410 struct bgp_redist
*bgp_redist_lookup(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1411 unsigned short instance
)
1413 struct list
*red_list
;
1414 struct listnode
*node
;
1415 struct bgp_redist
*red
;
1417 red_list
= bgp
->redist
[afi
][type
];
1421 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1422 if (red
->instance
== instance
)
1428 struct bgp_redist
*bgp_redist_add(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1429 unsigned short instance
)
1431 struct list
*red_list
;
1432 struct bgp_redist
*red
;
1434 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1438 if (!bgp
->redist
[afi
][type
])
1439 bgp
->redist
[afi
][type
] = list_new();
1441 red_list
= bgp
->redist
[afi
][type
];
1442 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
,
1443 sizeof(struct bgp_redist
));
1444 red
->instance
= instance
;
1446 listnode_add(red_list
, red
);
1451 static void bgp_redist_del(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1452 unsigned short instance
)
1454 struct bgp_redist
*red
;
1456 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1459 listnode_delete(bgp
->redist
[afi
][type
], red
);
1460 XFREE(MTYPE_BGP_REDIST
, red
);
1461 if (!bgp
->redist
[afi
][type
]->count
)
1462 list_delete_and_null(&bgp
->redist
[afi
][type
]);
1466 /* Other routes redistribution into BGP. */
1467 int bgp_redistribute_set(struct bgp
*bgp
, afi_t afi
, int type
,
1468 unsigned short instance
)
1471 /* Return if already redistribute flag is set. */
1473 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1477 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1479 if (vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1483 if (bgp
->vrf_id
== VRF_DEFAULT
1484 && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1485 vnc_export_bgp_enable(
1486 bgp
, afi
); /* only enables if mode bits cfg'd */
1490 vrf_bitmap_set(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1494 * Don't try to register if we're not connected to Zebra or Zebra
1495 * doesn't know of this instance.
1497 * When we come up later well resend if needed.
1499 if (!bgp_install_info_to_zebra(bgp
))
1502 if (BGP_DEBUG(zebra
, ZEBRA
))
1503 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1504 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1507 /* Send distribute add message to zebra. */
1508 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1509 instance
, bgp
->vrf_id
);
1514 int bgp_redistribute_resend(struct bgp
*bgp
, afi_t afi
, int type
,
1515 unsigned short instance
)
1517 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1518 * know of this instance.
1520 if (!bgp_install_info_to_zebra(bgp
))
1523 if (BGP_DEBUG(zebra
, ZEBRA
))
1524 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1525 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1528 /* Send distribute add message to zebra. */
1529 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1530 instance
, bgp
->vrf_id
);
1531 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1532 instance
, bgp
->vrf_id
);
1537 /* Redistribute with route-map specification. */
1538 int bgp_redistribute_rmap_set(struct bgp_redist
*red
, const char *name
)
1540 if (red
->rmap
.name
&& (strcmp(red
->rmap
.name
, name
) == 0))
1544 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1545 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1546 red
->rmap
.map
= route_map_lookup_by_name(name
);
1551 /* Redistribute with metric specification. */
1552 int bgp_redistribute_metric_set(struct bgp
*bgp
, struct bgp_redist
*red
,
1553 afi_t afi
, int type
, uint32_t metric
)
1555 struct bgp_node
*rn
;
1556 struct bgp_info
*ri
;
1558 if (red
->redist_metric_flag
&& red
->redist_metric
== metric
)
1561 red
->redist_metric_flag
= 1;
1562 red
->redist_metric
= metric
;
1564 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
1565 rn
= bgp_route_next(rn
)) {
1566 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
1567 if (ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
1569 && ri
->instance
== red
->instance
) {
1570 struct attr
*old_attr
;
1571 struct attr new_attr
;
1573 bgp_attr_dup(&new_attr
, ri
->attr
);
1574 new_attr
.med
= red
->redist_metric
;
1575 old_attr
= ri
->attr
;
1576 ri
->attr
= bgp_attr_intern(&new_attr
);
1577 bgp_attr_unintern(&old_attr
);
1579 bgp_info_set_flag(rn
, ri
,
1580 BGP_INFO_ATTR_CHANGED
);
1581 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1589 /* Unset redistribution. */
1590 int bgp_redistribute_unreg(struct bgp
*bgp
, afi_t afi
, int type
,
1591 unsigned short instance
)
1593 struct bgp_redist
*red
;
1595 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1599 /* Return if zebra connection is disabled. */
1601 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1604 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1606 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1608 vrf_bitmap_unset(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1612 if (bgp_install_info_to_zebra(bgp
)) {
1613 /* Send distribute delete message to zebra. */
1614 if (BGP_DEBUG(zebra
, ZEBRA
))
1615 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1616 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1618 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
1619 type
, instance
, bgp
->vrf_id
);
1622 /* Withdraw redistributed routes from current BGP's routing table. */
1623 bgp_redistribute_withdraw(bgp
, afi
, type
, instance
);
1628 /* Unset redistribution. */
1629 int bgp_redistribute_unset(struct bgp
*bgp
, afi_t afi
, int type
,
1630 unsigned short instance
)
1632 struct bgp_redist
*red
;
1635 * vnc and vpn->vrf checks must be before red check because
1636 * they operate within bgpd irrespective of zebra connection
1637 * status. red lookup fails if there is no zebra connection.
1640 if (bgp
->vrf_id
== VRF_DEFAULT
&& type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1641 vnc_export_bgp_disable(bgp
, afi
);
1645 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1649 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1651 /* Unset route-map. */
1653 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1654 red
->rmap
.name
= NULL
;
1655 red
->rmap
.map
= NULL
;
1658 red
->redist_metric_flag
= 0;
1659 red
->redist_metric
= 0;
1661 bgp_redist_del(bgp
, afi
, type
, instance
);
1666 /* Update redistribute vrf bitmap during triggers like
1667 restart networking or delete/add VRFs */
1668 void bgp_update_redist_vrf_bitmaps(struct bgp
*bgp
, vrf_id_t old_vrf_id
)
1673 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1674 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1675 if ((old_vrf_id
== VRF_UNKNOWN
)
1676 || vrf_bitmap_check(zclient
->redist
[afi
][i
],
1678 vrf_bitmap_unset(zclient
->redist
[afi
][i
],
1680 vrf_bitmap_set(zclient
->redist
[afi
][i
],
1686 void bgp_zclient_reset(void)
1688 zclient_reset(zclient
);
1691 /* Register this instance with Zebra. Invoked upon connect (for
1692 * default instance) and when other VRFs are learnt (or created and
1695 void bgp_zebra_instance_register(struct bgp
*bgp
)
1697 /* Don't try to register if we're not connected to Zebra */
1698 if (!zclient
|| zclient
->sock
< 0)
1701 if (BGP_DEBUG(zebra
, ZEBRA
))
1702 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
1704 /* Register for router-id, interfaces, redistributed routes. */
1705 zclient_send_reg_requests(zclient
, bgp
->vrf_id
);
1707 /* For default instance, register to learn about VNIs, if appropriate.
1709 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& is_evpn_enabled())
1710 bgp_zebra_advertise_all_vni(bgp
, 1);
1713 /* Deregister this instance with Zebra. Invoked upon the instance
1714 * being deleted (default or VRF) and it is already registered.
1716 void bgp_zebra_instance_deregister(struct bgp
*bgp
)
1718 /* Don't try to deregister if we're not connected to Zebra */
1719 if (zclient
->sock
< 0)
1722 if (BGP_DEBUG(zebra
, ZEBRA
))
1723 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
1725 /* For default instance, unregister learning about VNIs, if appropriate.
1727 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& is_evpn_enabled())
1728 bgp_zebra_advertise_all_vni(bgp
, 0);
1730 /* Deregister for router-id, interfaces, redistributed routes. */
1731 zclient_send_dereg_requests(zclient
, bgp
->vrf_id
);
1734 void bgp_zebra_initiate_radv(struct bgp
*bgp
, struct peer
*peer
)
1736 int ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
1738 /* Don't try to initiate if we're not connected to Zebra */
1739 if (zclient
->sock
< 0)
1742 if (BGP_DEBUG(zebra
, ZEBRA
))
1743 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
,
1746 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 1,
1750 void bgp_zebra_terminate_radv(struct bgp
*bgp
, struct peer
*peer
)
1752 /* Don't try to terminate if we're not connected to Zebra */
1753 if (zclient
->sock
< 0)
1756 if (BGP_DEBUG(zebra
, ZEBRA
))
1757 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
,
1760 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 0, 0);
1763 int bgp_zebra_advertise_subnet(struct bgp
*bgp
, int advertise
, vni_t vni
)
1765 struct stream
*s
= NULL
;
1768 if (!zclient
|| zclient
->sock
< 0)
1771 /* Don't try to register if Zebra doesn't know of this instance. */
1772 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1778 zclient_create_header(s
, ZEBRA_ADVERTISE_SUBNET
, bgp
->vrf_id
);
1779 stream_putc(s
, advertise
);
1780 stream_put3(s
, vni
);
1781 stream_putw_at(s
, 0, stream_get_endp(s
));
1783 return zclient_send_message(zclient
);
1786 int bgp_zebra_advertise_gw_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
1788 struct stream
*s
= NULL
;
1791 if (!zclient
|| zclient
->sock
< 0)
1794 /* Don't try to register if Zebra doesn't know of this instance. */
1795 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1801 zclient_create_header(s
, ZEBRA_ADVERTISE_DEFAULT_GW
, bgp
->vrf_id
);
1802 stream_putc(s
, advertise
);
1803 stream_put3(s
, vni
);
1804 stream_putw_at(s
, 0, stream_get_endp(s
));
1806 return zclient_send_message(zclient
);
1809 int bgp_zebra_advertise_all_vni(struct bgp
*bgp
, int advertise
)
1814 if (!zclient
|| zclient
->sock
< 0)
1817 /* Don't try to register if Zebra doesn't know of this instance. */
1818 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1824 zclient_create_header(s
, ZEBRA_ADVERTISE_ALL_VNI
, bgp
->vrf_id
);
1825 stream_putc(s
, advertise
);
1826 stream_putw_at(s
, 0, stream_get_endp(s
));
1828 return zclient_send_message(zclient
);
1831 /* BGP has established connection with Zebra. */
1832 static void bgp_zebra_connected(struct zclient
*zclient
)
1836 zclient_num_connects
++; /* increment even if not responding */
1838 /* At this point, we may or may not have BGP instances configured, but
1839 * we're only interested in the default VRF (others wouldn't have learnt
1840 * the VRF from Zebra yet.)
1842 bgp
= bgp_get_default();
1846 bgp_zebra_instance_register(bgp
);
1848 /* Send the client registration */
1849 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
1851 /* tell label pool that zebra is connected */
1852 bgp_lp_event_zebra_up();
1854 /* TODO - What if we have peers and networks configured, do we have to
1859 static int bgp_zebra_process_local_l3vni(int cmd
, struct zclient
*zclient
,
1860 zebra_size_t length
, vrf_id_t vrf_id
)
1863 char buf
[ETHER_ADDR_STRLEN
];
1865 struct ethaddr rmac
;
1866 struct in_addr originator_ip
;
1869 memset(&rmac
, 0, sizeof(struct ethaddr
));
1870 memset(&originator_ip
, 0, sizeof(struct in_addr
));
1872 l3vni
= stream_getl(s
);
1873 if (cmd
== ZEBRA_L3VNI_ADD
) {
1874 stream_get(&rmac
, s
, sizeof(struct ethaddr
));
1875 originator_ip
.s_addr
= stream_get_ipv4(s
);
1876 stream_get(&filter
, s
, sizeof(int));
1879 if (BGP_DEBUG(zebra
, ZEBRA
))
1880 zlog_debug("Rx L3-VNI %s VRF %s VNI %u RMAC %s filter %s",
1881 (cmd
== ZEBRA_L3VNI_ADD
) ? "add" : "del",
1882 vrf_id_to_name(vrf_id
), l3vni
,
1883 prefix_mac2str(&rmac
, buf
, sizeof(buf
)),
1884 filter
? "prefix-routes-only" : "none");
1886 if (cmd
== ZEBRA_L3VNI_ADD
)
1887 bgp_evpn_local_l3vni_add(l3vni
, vrf_id
, &rmac
, originator_ip
,
1890 bgp_evpn_local_l3vni_del(l3vni
, vrf_id
);
1895 static int bgp_zebra_process_local_vni(int command
, struct zclient
*zclient
,
1896 zebra_size_t length
, vrf_id_t vrf_id
)
1901 struct in_addr vtep_ip
= {INADDR_ANY
};
1902 vrf_id_t tenant_vrf_id
= VRF_DEFAULT
;
1905 vni
= stream_getl(s
);
1906 if (command
== ZEBRA_VNI_ADD
) {
1907 vtep_ip
.s_addr
= stream_get_ipv4(s
);
1908 stream_get(&tenant_vrf_id
, s
, sizeof(vrf_id_t
));
1911 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1915 if (BGP_DEBUG(zebra
, ZEBRA
))
1916 zlog_debug("Rx VNI %s VRF %s VNI %u tenant-vrf %s",
1917 (command
== ZEBRA_VNI_ADD
) ? "add" : "del",
1918 vrf_id_to_name(vrf_id
), vni
,
1919 vrf_id_to_name(tenant_vrf_id
));
1921 if (command
== ZEBRA_VNI_ADD
)
1922 return bgp_evpn_local_vni_add(
1923 bgp
, vni
, vtep_ip
.s_addr
? vtep_ip
: bgp
->router_id
,
1926 return bgp_evpn_local_vni_del(bgp
, vni
);
1929 static int bgp_zebra_process_local_macip(int command
, struct zclient
*zclient
,
1930 zebra_size_t length
, vrf_id_t vrf_id
)
1938 char buf
[ETHER_ADDR_STRLEN
];
1939 char buf1
[INET6_ADDRSTRLEN
];
1942 memset(&ip
, 0, sizeof(ip
));
1944 vni
= stream_getl(s
);
1945 stream_get(&mac
.octet
, s
, ETH_ALEN
);
1946 ipa_len
= stream_getl(s
);
1947 if (ipa_len
!= 0 && ipa_len
!= IPV4_MAX_BYTELEN
1948 && ipa_len
!= IPV6_MAX_BYTELEN
) {
1949 zlog_err("%u:Recv MACIP %s with invalid IP addr length %d",
1950 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
1957 (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
: IPADDR_V6
;
1958 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
1960 flags
= stream_getc(s
);
1962 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1966 if (BGP_DEBUG(zebra
, ZEBRA
))
1967 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u",
1968 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
1969 flags
, prefix_mac2str(&mac
, buf
, sizeof(buf
)),
1970 ipaddr2str(&ip
, buf1
, sizeof(buf1
)), vni
);
1972 if (command
== ZEBRA_MACIP_ADD
)
1973 return bgp_evpn_local_macip_add(bgp
, vni
, &mac
, &ip
, flags
);
1975 return bgp_evpn_local_macip_del(bgp
, vni
, &mac
, &ip
);
1978 static void bgp_zebra_process_local_ip_prefix(int cmd
, struct zclient
*zclient
,
1979 zebra_size_t length
,
1982 struct stream
*s
= NULL
;
1983 struct bgp
*bgp_vrf
= NULL
;
1985 char buf
[PREFIX_STRLEN
];
1987 memset(&p
, 0, sizeof(struct prefix
));
1989 stream_get(&p
, s
, sizeof(struct prefix
));
1991 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
1995 if (BGP_DEBUG(zebra
, ZEBRA
))
1996 zlog_debug("Recv prefix %s %s on vrf %s",
1997 prefix2str(&p
, buf
, sizeof(buf
)),
1998 (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) ? "ADD" : "DEL",
1999 vrf_id_to_name(vrf_id
));
2001 if (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) {
2003 if (p
.family
== AF_INET
)
2004 return bgp_evpn_advertise_type5_route(
2005 bgp_vrf
, &p
, NULL
, AFI_IP
, SAFI_UNICAST
);
2007 return bgp_evpn_advertise_type5_route(
2008 bgp_vrf
, &p
, NULL
, AFI_IP6
, SAFI_UNICAST
);
2011 if (p
.family
== AF_INET
)
2012 return bgp_evpn_withdraw_type5_route(
2013 bgp_vrf
, &p
, AFI_IP
, SAFI_UNICAST
);
2015 return bgp_evpn_withdraw_type5_route(
2016 bgp_vrf
, &p
, AFI_IP6
, SAFI_UNICAST
);
2020 static void bgp_zebra_process_label_chunk(
2022 struct zclient
*zclient
,
2023 zebra_size_t length
,
2026 struct stream
*s
= NULL
;
2027 uint8_t response_keep
;
2032 STREAM_GETC(s
, response_keep
);
2033 STREAM_GETL(s
, first
);
2034 STREAM_GETL(s
, last
);
2037 first
< MPLS_LABEL_UNRESERVED_MIN
||
2038 last
> MPLS_LABEL_UNRESERVED_MAX
) {
2040 zlog_err("%s: Invalid Label chunk: %u - %u",
2041 __func__
, first
, last
);
2044 if (BGP_DEBUG(zebra
, ZEBRA
)) {
2045 zlog_debug("Label Chunk assign: %u - %u (%u) ",
2046 first
, last
, response_keep
);
2049 bgp_lp_event_chunk(response_keep
, first
, last
);
2051 stream_failure
: /* for STREAM_GETX */
2055 extern struct zebra_privs_t bgpd_privs
;
2057 void bgp_zebra_init(struct thread_master
*master
)
2059 zclient_num_connects
= 0;
2061 /* Set default values. */
2062 zclient
= zclient_new_notify(master
, &zclient_options_default
);
2063 zclient_init(zclient
, ZEBRA_ROUTE_BGP
, 0, &bgpd_privs
);
2064 zclient
->zebra_connected
= bgp_zebra_connected
;
2065 zclient
->router_id_update
= bgp_router_id_update
;
2066 zclient
->interface_add
= bgp_interface_add
;
2067 zclient
->interface_delete
= bgp_interface_delete
;
2068 zclient
->interface_address_add
= bgp_interface_address_add
;
2069 zclient
->interface_address_delete
= bgp_interface_address_delete
;
2070 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
2071 zclient
->interface_nbr_address_delete
=
2072 bgp_interface_nbr_address_delete
;
2073 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
2074 zclient
->redistribute_route_add
= zebra_read_route
;
2075 zclient
->redistribute_route_del
= zebra_read_route
;
2076 zclient
->interface_up
= bgp_interface_up
;
2077 zclient
->interface_down
= bgp_interface_down
;
2078 zclient
->nexthop_update
= bgp_read_nexthop_update
;
2079 zclient
->import_check_update
= bgp_read_import_check_update
;
2080 zclient
->fec_update
= bgp_read_fec_update
;
2081 zclient
->local_vni_add
= bgp_zebra_process_local_vni
;
2082 zclient
->local_vni_del
= bgp_zebra_process_local_vni
;
2083 zclient
->local_macip_add
= bgp_zebra_process_local_macip
;
2084 zclient
->local_macip_del
= bgp_zebra_process_local_macip
;
2085 zclient
->local_l3vni_add
= bgp_zebra_process_local_l3vni
;
2086 zclient
->local_l3vni_del
= bgp_zebra_process_local_l3vni
;
2087 zclient
->local_ip_prefix_add
= bgp_zebra_process_local_ip_prefix
;
2088 zclient
->local_ip_prefix_del
= bgp_zebra_process_local_ip_prefix
;
2089 zclient
->label_chunk
= bgp_zebra_process_label_chunk
;
2092 void bgp_zebra_destroy(void)
2094 if (zclient
== NULL
)
2096 zclient_stop(zclient
);
2097 zclient_free(zclient
);
2101 int bgp_zebra_num_connects(void)
2103 return zclient_num_connects
;