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"
59 /* All information about zebra. */
60 struct zclient
*zclient
= NULL
;
62 /* Can we install into zebra? */
63 static inline int bgp_install_info_to_zebra(struct bgp
*bgp
)
65 if (zclient
->sock
<= 0)
68 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
74 int zclient_num_connects
;
76 /* Router-id update message from zebra. */
77 static int bgp_router_id_update(int command
, struct zclient
*zclient
,
78 zebra_size_t length
, vrf_id_t vrf_id
)
80 struct prefix router_id
;
82 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
84 if (BGP_DEBUG(zebra
, ZEBRA
)) {
85 char buf
[PREFIX2STR_BUFFER
];
86 prefix2str(&router_id
, buf
, sizeof(buf
));
87 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id
, buf
);
90 bgp_router_id_zebra_bump(vrf_id
, &router_id
);
94 /* Nexthop update message from zebra. */
95 static int bgp_read_nexthop_update(int command
, struct zclient
*zclient
,
96 zebra_size_t length
, vrf_id_t vrf_id
)
98 bgp_parse_nexthop_update(command
, vrf_id
);
102 static int bgp_read_import_check_update(int command
, struct zclient
*zclient
,
103 zebra_size_t length
, vrf_id_t vrf_id
)
105 bgp_parse_nexthop_update(command
, vrf_id
);
109 /* Set or clear interface on which unnumbered neighbor is configured. This
110 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
113 static void bgp_update_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
,
114 struct interface
*upd_ifp
)
116 struct listnode
*node
, *nnode
;
119 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
120 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)) {
123 bgp_zebra_initiate_radv(bgp
, peer
);
125 bgp_zebra_terminate_radv(bgp
, peer
);
132 static int bgp_read_fec_update(int command
, struct zclient
*zclient
,
135 bgp_parse_fec_update();
139 static void bgp_start_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
)
141 struct listnode
*node
, *nnode
;
144 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
145 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)
146 && peer
->status
!= Established
) {
147 if (peer_active(peer
))
148 BGP_EVENT_ADD(peer
, BGP_Stop
);
149 BGP_EVENT_ADD(peer
, BGP_Start
);
154 static void bgp_nbr_connected_add(struct bgp
*bgp
, struct nbr_connected
*ifc
)
156 struct listnode
*node
;
157 struct connected
*connected
;
158 struct interface
*ifp
;
161 /* Kick-off the FSM for any relevant peers only if there is a
162 * valid local address on the interface.
165 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
166 p
= connected
->address
;
167 if (p
->family
== AF_INET6
168 && IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
174 bgp_start_interface_nbrs(bgp
, ifp
);
177 static void bgp_nbr_connected_delete(struct bgp
*bgp
, struct nbr_connected
*ifc
,
180 struct listnode
*node
, *nnode
;
182 struct interface
*ifp
;
184 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
186 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)) {
187 peer
->last_reset
= PEER_DOWN_NBR_ADDR_DEL
;
188 BGP_EVENT_ADD(peer
, BGP_Stop
);
191 /* Free neighbor also, if we're asked to. */
194 listnode_delete(ifp
->nbr_connected
, ifc
);
195 nbr_connected_free(ifc
);
199 /* Inteface addition message from zebra. */
200 static int bgp_interface_add(int command
, struct zclient
*zclient
,
201 zebra_size_t length
, vrf_id_t vrf_id
)
203 struct interface
*ifp
;
206 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
207 if (!ifp
) // unexpected
210 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
211 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id
, ifp
->name
);
213 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
217 bgp_update_interface_nbrs(bgp
, ifp
, ifp
);
221 static int bgp_interface_delete(int command
, struct zclient
*zclient
,
222 zebra_size_t length
, vrf_id_t vrf_id
)
225 struct interface
*ifp
;
228 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
233 ifp
= zebra_interface_state_read(s
, vrf_id
);
234 if (!ifp
) /* This may happen if we've just unregistered for a VRF. */
237 if (BGP_DEBUG(zebra
, ZEBRA
))
238 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id
, ifp
->name
);
240 bgp_update_interface_nbrs(bgp
, ifp
, NULL
);
242 if_set_index(ifp
, IFINDEX_INTERNAL
);
246 static int bgp_interface_up(int command
, struct zclient
*zclient
,
247 zebra_size_t length
, vrf_id_t vrf_id
)
250 struct interface
*ifp
;
252 struct nbr_connected
*nc
;
253 struct listnode
*node
, *nnode
;
256 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
261 ifp
= zebra_interface_state_read(s
, vrf_id
);
266 if (BGP_DEBUG(zebra
, ZEBRA
))
267 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id
, ifp
->name
);
269 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
270 bgp_connected_add(bgp
, c
);
272 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
273 bgp_nbr_connected_add(bgp
, nc
);
278 static int bgp_interface_down(int command
, struct zclient
*zclient
,
279 zebra_size_t length
, vrf_id_t vrf_id
)
282 struct interface
*ifp
;
284 struct nbr_connected
*nc
;
285 struct listnode
*node
, *nnode
;
288 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
293 ifp
= zebra_interface_state_read(s
, vrf_id
);
297 if (BGP_DEBUG(zebra
, ZEBRA
))
298 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id
, ifp
->name
);
300 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
301 bgp_connected_delete(bgp
, c
);
303 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
304 bgp_nbr_connected_delete(bgp
, nc
, 1);
306 /* Fast external-failover */
310 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
313 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
314 #if defined(HAVE_CUMULUS)
315 /* Take down directly connected EBGP peers as well as
317 * tracked (directly connected) IBGP peers.
319 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1)
321 || bgp_bfd_is_peer_multihop(peer
)))
323 /* Take down directly connected EBGP peers */
324 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
328 if (ifp
== peer
->nexthop
.ifp
) {
329 BGP_EVENT_ADD(peer
, BGP_Stop
);
330 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
338 static int bgp_interface_address_add(int command
, struct zclient
*zclient
,
339 zebra_size_t length
, vrf_id_t vrf_id
)
341 struct connected
*ifc
;
344 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
348 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
353 if (bgp_debug_zebra(ifc
->address
)) {
354 char buf
[PREFIX2STR_BUFFER
];
355 prefix2str(ifc
->address
, buf
, sizeof(buf
));
356 zlog_debug("Rx Intf address add VRF %u IF %s addr %s", vrf_id
,
357 ifc
->ifp
->name
, buf
);
360 if (if_is_operative(ifc
->ifp
)) {
361 bgp_connected_add(bgp
, ifc
);
363 /* If we have learnt of any neighbors on this interface,
364 * check to kick off any BGP interface-based neighbors,
365 * but only if this is a link-local address.
367 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
)
368 && !list_isempty(ifc
->ifp
->nbr_connected
))
369 bgp_start_interface_nbrs(bgp
, ifc
->ifp
);
375 static int bgp_interface_address_delete(int command
, struct zclient
*zclient
,
376 zebra_size_t length
, vrf_id_t vrf_id
)
378 struct connected
*ifc
;
381 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
385 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
390 if (bgp_debug_zebra(ifc
->address
)) {
391 char buf
[PREFIX2STR_BUFFER
];
392 prefix2str(ifc
->address
, buf
, sizeof(buf
));
393 zlog_debug("Rx Intf address del VRF %u IF %s addr %s", vrf_id
,
394 ifc
->ifp
->name
, buf
);
397 if (if_is_operative(ifc
->ifp
)) {
398 bgp_connected_delete(bgp
, ifc
);
406 static int bgp_interface_nbr_address_add(int command
, struct zclient
*zclient
,
407 zebra_size_t length
, vrf_id_t vrf_id
)
409 struct nbr_connected
*ifc
= NULL
;
412 ifc
= zebra_interface_nbr_address_read(command
, zclient
->ibuf
, vrf_id
);
417 if (bgp_debug_zebra(ifc
->address
)) {
418 char buf
[PREFIX2STR_BUFFER
];
419 prefix2str(ifc
->address
, buf
, sizeof(buf
));
420 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s", vrf_id
,
421 ifc
->ifp
->name
, buf
);
424 if (if_is_operative(ifc
->ifp
)) {
425 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
427 bgp_nbr_connected_add(bgp
, ifc
);
433 static int bgp_interface_nbr_address_delete(int command
,
434 struct zclient
*zclient
,
438 struct nbr_connected
*ifc
= NULL
;
441 ifc
= zebra_interface_nbr_address_read(command
, zclient
->ibuf
, vrf_id
);
446 if (bgp_debug_zebra(ifc
->address
)) {
447 char buf
[PREFIX2STR_BUFFER
];
448 prefix2str(ifc
->address
, buf
, sizeof(buf
));
449 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s", vrf_id
,
450 ifc
->ifp
->name
, buf
);
453 if (if_is_operative(ifc
->ifp
)) {
454 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
456 bgp_nbr_connected_delete(bgp
, ifc
, 0);
459 nbr_connected_free(ifc
);
464 /* VRF update for an interface. */
465 static int bgp_interface_vrf_update(int command
, struct zclient
*zclient
,
466 zebra_size_t length
, vrf_id_t vrf_id
)
468 struct interface
*ifp
;
471 struct nbr_connected
*nc
;
472 struct listnode
*node
, *nnode
;
475 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
480 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
481 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id
,
482 ifp
->name
, new_vrf_id
);
484 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
488 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
489 bgp_connected_delete(bgp
, c
);
491 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
492 bgp_nbr_connected_delete(bgp
, nc
, 1);
494 /* Fast external-failover */
498 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
501 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
502 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
505 if (ifp
== peer
->nexthop
.ifp
)
506 BGP_EVENT_ADD(peer
, BGP_Stop
);
510 if_update_to_new_vrf(ifp
, new_vrf_id
);
512 bgp
= bgp_lookup_by_vrf_id(new_vrf_id
);
516 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
517 bgp_connected_add(bgp
, c
);
519 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
520 bgp_nbr_connected_add(bgp
, nc
);
524 /* Zebra route add and delete treatment. */
525 static int zebra_read_route(int command
, struct zclient
*zclient
,
526 zebra_size_t length
, vrf_id_t vrf_id
)
528 enum nexthop_types_t nhtype
;
529 struct zapi_route api
;
530 union g_addr nexthop
;
535 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
539 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
542 /* we completely ignore srcdest routes for now. */
543 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
546 /* ignore link-local address. */
547 if (api
.prefix
.family
== AF_INET6
548 && IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
551 nexthop
= api
.nexthops
[0].gate
;
552 ifindex
= api
.nexthops
[0].ifindex
;
553 nhtype
= api
.nexthops
[0].type
;
555 add
= (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
558 * The ADD message is actually an UPDATE and there is no
560 * for a prior redistributed route, if any. So, perform an
562 * DEL processing for the same redistributed route from any
566 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
568 bgp_redistribute_delete(bgp
, &api
.prefix
, i
,
572 /* Now perform the add/update. */
573 bgp_redistribute_add(bgp
, &api
.prefix
, &nexthop
, ifindex
,
574 nhtype
, api
.metric
, api
.type
, api
.instance
,
577 bgp_redistribute_delete(bgp
, &api
.prefix
, api
.type
,
581 if (bgp_debug_zebra(&api
.prefix
)) {
582 char buf
[2][PREFIX_STRLEN
];
584 prefix2str(&api
.prefix
, buf
[0], sizeof(buf
[0]));
585 inet_ntop(api
.prefix
.family
, &nexthop
, buf
[1], sizeof(buf
[1]));
587 "Rx route %s VRF %u %s[%d] %s "
588 "nexthop %s metric %u tag %" ROUTE_TAG_PRI
,
589 (add
) ? "add" : "delete", vrf_id
,
590 zebra_route_string(api
.type
), api
.instance
, buf
[0],
591 buf
[1], api
.metric
, api
.tag
);
597 struct interface
*if_lookup_by_ipv4(struct in_addr
*addr
, vrf_id_t vrf_id
)
600 struct listnode
*cnode
;
601 struct interface
*ifp
;
602 struct connected
*connected
;
603 struct prefix_ipv4 p
;
606 vrf
= vrf_lookup_by_id(vrf_id
);
612 p
.prefixlen
= IPV4_MAX_BITLEN
;
614 FOR_ALL_INTERFACES (vrf
, ifp
) {
615 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
616 cp
= connected
->address
;
618 if (cp
->family
== AF_INET
)
619 if (prefix_match(cp
, (struct prefix
*)&p
))
626 struct interface
*if_lookup_by_ipv4_exact(struct in_addr
*addr
, vrf_id_t vrf_id
)
629 struct listnode
*cnode
;
630 struct interface
*ifp
;
631 struct connected
*connected
;
634 vrf
= vrf_lookup_by_id(vrf_id
);
638 FOR_ALL_INTERFACES (vrf
, ifp
) {
639 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
640 cp
= connected
->address
;
642 if (cp
->family
== AF_INET
)
643 if (IPV4_ADDR_SAME(&cp
->u
.prefix4
, addr
))
650 struct interface
*if_lookup_by_ipv6(struct in6_addr
*addr
, ifindex_t ifindex
,
654 struct listnode
*cnode
;
655 struct interface
*ifp
;
656 struct connected
*connected
;
657 struct prefix_ipv6 p
;
660 vrf
= vrf_lookup_by_id(vrf_id
);
666 p
.prefixlen
= IPV6_MAX_BITLEN
;
668 FOR_ALL_INTERFACES (vrf
, ifp
) {
669 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
670 cp
= connected
->address
;
672 if (cp
->family
== AF_INET6
)
673 if (prefix_match(cp
, (struct prefix
*)&p
)) {
674 if (IN6_IS_ADDR_LINKLOCAL(
676 if (ifindex
== ifp
->ifindex
)
686 struct interface
*if_lookup_by_ipv6_exact(struct in6_addr
*addr
,
687 ifindex_t ifindex
, vrf_id_t vrf_id
)
690 struct listnode
*cnode
;
691 struct interface
*ifp
;
692 struct connected
*connected
;
695 vrf
= vrf_lookup_by_id(vrf_id
);
699 FOR_ALL_INTERFACES (vrf
, ifp
) {
700 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
701 cp
= connected
->address
;
703 if (cp
->family
== AF_INET6
)
704 if (IPV6_ADDR_SAME(&cp
->u
.prefix6
, addr
)) {
705 if (IN6_IS_ADDR_LINKLOCAL(
707 if (ifindex
== ifp
->ifindex
)
717 static int if_get_ipv6_global(struct interface
*ifp
, struct in6_addr
*addr
)
719 struct listnode
*cnode
;
720 struct connected
*connected
;
723 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
724 cp
= connected
->address
;
726 if (cp
->family
== AF_INET6
)
727 if (!IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
728 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
735 static int if_get_ipv6_local(struct interface
*ifp
, struct in6_addr
*addr
)
737 struct listnode
*cnode
;
738 struct connected
*connected
;
741 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
742 cp
= connected
->address
;
744 if (cp
->family
== AF_INET6
)
745 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
746 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
753 static int if_get_ipv4_address(struct interface
*ifp
, struct in_addr
*addr
)
755 struct listnode
*cnode
;
756 struct connected
*connected
;
759 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
760 cp
= connected
->address
;
761 if ((cp
->family
== AF_INET
)
762 && !ipv4_martian(&(cp
->u
.prefix4
))) {
763 *addr
= cp
->u
.prefix4
;
770 int bgp_nexthop_set(union sockunion
*local
, union sockunion
*remote
,
771 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
774 struct interface
*ifp
= NULL
;
776 memset(nexthop
, 0, sizeof(struct bgp_nexthop
));
783 if (local
->sa
.sa_family
== AF_INET
) {
784 nexthop
->v4
= local
->sin
.sin_addr
;
786 ifp
= if_lookup_by_name(peer
->update_if
,
789 ifp
= if_lookup_by_ipv4_exact(&local
->sin
.sin_addr
,
792 if (local
->sa
.sa_family
== AF_INET6
) {
793 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
794 if (peer
->conf_if
|| peer
->ifname
)
795 ifp
= if_lookup_by_name(peer
->conf_if
799 } else if (peer
->update_if
)
800 ifp
= if_lookup_by_name(peer
->update_if
,
803 ifp
= if_lookup_by_ipv6_exact(&local
->sin6
.sin6_addr
,
804 local
->sin6
.sin6_scope_id
,
813 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
814 if (local
->sa
.sa_family
== AF_INET
) {
816 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
819 /* There is no global nexthop. Use link-local address as
821 * global and link-local nexthop. In this scenario, the
823 * for interop is that the network admin would use a
825 * specify the global IPv6 nexthop.
827 if_get_ipv6_local(ifp
, &nexthop
->v6_global
);
828 memcpy(&nexthop
->v6_local
, &nexthop
->v6_global
,
831 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
833 if (if_lookup_by_ipv4(&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
834 peer
->shared_network
= 1;
836 peer
->shared_network
= 0;
839 /* IPv6 connection, fetch and store IPv4 local address if any. */
840 if (local
->sa
.sa_family
== AF_INET6
) {
841 struct interface
*direct
= NULL
;
844 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
845 if (!ret
&& peer
->local_id
.s_addr
)
846 nexthop
->v4
= peer
->local_id
;
849 if (!IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
850 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
853 /* If directory connected set link-local address. */
854 direct
= if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
855 remote
->sin6
.sin6_scope_id
,
858 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
860 /* Link-local address. */
862 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
864 /* If there is no global address. Set link-local
866 global. I know this break RFC specification... */
867 /* In this scenario, the expectation for interop is that
869 * network admin would use a route-map to specify the
874 memcpy(&nexthop
->v6_global
,
875 &local
->sin6
.sin6_addr
,
877 /* Always set the link-local address */
878 memcpy(&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
882 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)
883 || if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
884 remote
->sin6
.sin6_scope_id
,
886 peer
->shared_network
= 1;
888 peer
->shared_network
= 0;
891 /* KAME stack specific treatment. */
893 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_global
)
894 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
)) {
895 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
, 0);
897 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_local
)
898 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
)) {
899 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
, 0);
903 /* If we have identified the local interface, there is no error for now.
908 static struct in6_addr
*bgp_info_to_ipv6_nexthop(struct bgp_info
*info
)
910 struct in6_addr
*nexthop
= NULL
;
912 /* Only global address nexthop exists. */
913 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
)
914 nexthop
= &info
->attr
->mp_nexthop_global
;
916 /* If both global and link-local address present. */
917 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
918 /* Check if route-map is set to prefer global over link-local */
919 if (info
->attr
->mp_nexthop_prefer_global
)
920 nexthop
= &info
->attr
->mp_nexthop_global
;
922 /* Workaround for Cisco's nexthop bug. */
923 if (IN6_IS_ADDR_UNSPECIFIED(
924 &info
->attr
->mp_nexthop_global
)
925 && info
->peer
->su_remote
->sa
.sa_family
== AF_INET6
)
927 &info
->peer
->su_remote
->sin6
.sin6_addr
;
929 nexthop
= &info
->attr
->mp_nexthop_local
;
936 static int bgp_table_map_apply(struct route_map
*map
, struct prefix
*p
,
937 struct bgp_info
*info
)
939 route_map_result_t ret
;
941 ret
= route_map_apply(map
, p
, RMAP_BGP
, info
);
942 bgp_attr_flush(info
->attr
);
944 if (ret
!= RMAP_DENYMATCH
)
947 if (bgp_debug_zebra(p
)) {
948 if (p
->family
== AF_INET
) {
949 char buf
[2][INET_ADDRSTRLEN
];
951 "Zebra rmap deny: IPv4 route %s/%d nexthop %s",
952 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0],
955 inet_ntop(AF_INET
, &info
->attr
->nexthop
, buf
[1],
958 if (p
->family
== AF_INET6
) {
959 char buf
[2][INET6_ADDRSTRLEN
];
961 "Zebra rmap deny: IPv6 route %s/%d nexthop %s",
962 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0],
966 bgp_info_to_ipv6_nexthop(info
),
967 buf
[1], sizeof(buf
[1])));
973 static struct thread
*bgp_tm_thread_connect
;
974 static bool bgp_tm_status_connected
;
976 static int bgp_zebra_tm_connect(struct thread
*t
)
978 struct zclient
*zclient
;
979 int delay
= 10, ret
= 0;
981 zclient
= THREAD_ARG(t
);
982 if (bgp_tm_status_connected
&& zclient
->sock
> 0)
985 bgp_tm_status_connected
= false;
986 ret
= tm_table_manager_connect(zclient
);
989 zlog_warn("Error connecting to table manager!");
990 bgp_tm_status_connected
= false;
992 if (!bgp_tm_status_connected
)
993 zlog_debug("Connecting to table manager. Success");
994 bgp_tm_status_connected
= true;
996 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
997 &bgp_tm_thread_connect
);
1001 void bgp_zebra_init_tm_connect(void)
1005 /* if already set, do nothing
1007 if (bgp_tm_thread_connect
!= NULL
)
1009 bgp_tm_status_connected
= false;
1010 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1011 &bgp_tm_thread_connect
);
1014 int bgp_zebra_get_table_range(uint32_t chunk_size
,
1015 uint32_t *start
, uint32_t *end
)
1019 if (!bgp_tm_status_connected
)
1021 ret
= tm_get_table_chunk(zclient
, chunk_size
, start
, end
);
1023 zlog_err("BGP: Error getting table chunk %u", chunk_size
);
1026 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1027 chunk_size
, *start
, *end
);
1031 void bgp_zebra_announce(struct bgp_node
*rn
, struct prefix
*p
,
1032 struct bgp_info
*info
, struct bgp
*bgp
, afi_t afi
,
1035 struct zapi_route api
;
1036 struct zapi_nexthop
*api_nh
;
1038 unsigned int valid_nh_count
= 0;
1039 int has_valid_label
= 0;
1042 struct bgp_info
*mpinfo
;
1044 struct attr local_attr
;
1045 struct bgp_info local_info
;
1046 struct bgp_info
*mpinfo_cp
= &local_info
;
1049 int nh_othervrf
= 0;
1051 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1052 * know of this instance.
1054 if (!bgp_install_info_to_zebra(bgp
))
1057 if (bgp
->main_zebra_update_hold
)
1061 * vrf leaking support (will have only one nexthop)
1063 if (info
->extra
&& info
->extra
->bgp_orig
)
1066 /* Make Zebra API structure. */
1067 memset(&api
, 0, sizeof(api
));
1068 memcpy(&api
.rmac
, &(info
->attr
->rmac
), sizeof(struct ethaddr
));
1069 api
.vrf_id
= bgp
->vrf_id
;
1070 api
.type
= ZEBRA_ROUTE_BGP
;
1073 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1077 if (info
->type
== ZEBRA_ROUTE_BGP
1078 && info
->sub_type
== BGP_ROUTE_IMPORTED
) {
1080 struct bgp_info
*bi
;
1083 * Look at parent chain for peer sort
1085 for (bi
= info
; bi
->extra
&& bi
->extra
->parent
;
1086 bi
= bi
->extra
->parent
) {
1088 peer
= ((struct bgp_info
*)(bi
->extra
->parent
))->peer
;
1092 tag
= info
->attr
->tag
;
1094 /* When we create an aggregate route we must also install a Null0 route
1097 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1098 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
1100 /* If the route's source is EVPN, flag as such. */
1101 if (is_route_parent_evpn(info
))
1102 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1104 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
1105 || info
->sub_type
== BGP_ROUTE_AGGREGATE
) {
1106 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1107 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1110 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1111 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1112 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1114 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1116 /* Metric is currently based on the best-path only */
1117 metric
= info
->attr
->med
;
1118 for (mpinfo
= info
; mpinfo
; mpinfo
= bgp_info_mpath_next(mpinfo
)) {
1119 if (valid_nh_count
>= multipath_num
)
1122 *mpinfo_cp
= *mpinfo
;
1124 /* Get nexthop address-family */
1125 if (p
->family
== AF_INET
1126 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
))
1127 nh_family
= AF_INET
;
1128 else if (p
->family
== AF_INET6
1129 || (p
->family
== AF_INET
1130 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
)))
1131 nh_family
= AF_INET6
;
1135 api_nh
= &api
.nexthops
[valid_nh_count
];
1136 api_nh
->vrf_id
= nh_othervrf
? info
->extra
->bgp_orig
->vrf_id
1138 if (nh_family
== AF_INET
) {
1139 struct in_addr
*nexthop
;
1141 if (bgp_debug_zebra(&api
.prefix
)) {
1142 char buf_prefix
[PREFIX_STRLEN
];
1143 prefix2str(&api
.prefix
, buf_prefix
,
1144 sizeof(buf_prefix
));
1145 if (mpinfo
->extra
) {
1147 "%s: p=%s, bgp_is_valid_label: %d",
1148 __func__
, buf_prefix
,
1154 "%s: p=%s, extra is NULL, no label",
1155 __func__
, buf_prefix
);
1159 if (bgp
->table_map
[afi
][safi
].name
|| nh_othervrf
) {
1160 /* Copy info and attributes, so the route-map
1161 apply doesn't modify the BGP route info. */
1162 local_attr
= *mpinfo
->attr
;
1163 mpinfo_cp
->attr
= &local_attr
;
1165 /* allow route-map to modify */
1166 local_attr
.nexthop
=
1167 info
->extra
->nexthop_orig
.u
1172 if (bgp
->table_map
[afi
][safi
].name
) {
1173 if (!bgp_table_map_apply(
1174 bgp
->table_map
[afi
][safi
].map
, p
,
1178 /* metric/tag is only allowed to be
1179 * overridden on 1st nexthop */
1180 if (mpinfo
== info
) {
1181 metric
= mpinfo_cp
->attr
->med
;
1182 tag
= mpinfo_cp
->attr
->tag
;
1186 nexthop
= &mpinfo_cp
->attr
->nexthop
;
1187 api_nh
->gate
.ipv4
= *nexthop
;
1189 /* EVPN type-2 routes are
1190 programmed as onlink on l3-vni SVI
1192 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1193 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1195 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1198 struct in6_addr
*nexthop
;
1202 if (bgp
->table_map
[afi
][safi
].name
|| nh_othervrf
) {
1203 /* Copy info and attributes, so the route-map
1204 apply doesn't modify the BGP route info. */
1205 local_attr
= *mpinfo
->attr
;
1206 mpinfo_cp
->attr
= &local_attr
;
1208 /* allow route-map to modify */
1209 local_attr
.mp_nexthop_global
=
1210 info
->extra
->nexthop_orig
.u
1212 local_attr
.mp_nexthop_len
=
1213 BGP_ATTR_NHLEN_IPV6_GLOBAL
;
1217 if (bgp
->table_map
[afi
][safi
].name
) {
1218 /* Copy info and attributes, so the route-map
1219 apply doesn't modify the BGP route info. */
1220 local_attr
= *mpinfo
->attr
;
1221 mpinfo_cp
->attr
= &local_attr
;
1223 if (!bgp_table_map_apply(
1224 bgp
->table_map
[afi
][safi
].map
, p
,
1228 /* metric/tag is only allowed to be
1229 * overridden on 1st nexthop */
1230 if (mpinfo
== info
) {
1231 metric
= mpinfo_cp
->attr
->med
;
1232 tag
= mpinfo_cp
->attr
->tag
;
1235 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo_cp
);
1237 if ((mpinfo
== info
)
1238 && mpinfo
->attr
->mp_nexthop_len
1239 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1240 if (mpinfo
->peer
->nexthop
.ifp
)
1241 ifindex
= mpinfo
->peer
->nexthop
.ifp
1245 if (mpinfo
->peer
->conf_if
)
1246 ifindex
= mpinfo
->peer
->ifp
->ifindex
;
1247 else if (mpinfo
->peer
->ifname
)
1248 ifindex
= ifname2ifindex(
1249 mpinfo
->peer
->ifname
,
1251 else if (mpinfo
->peer
->nexthop
.ifp
)
1252 ifindex
= mpinfo
->peer
->nexthop
.ifp
1256 if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1262 api_nh
->gate
.ipv6
= *nexthop
;
1263 api_nh
->ifindex
= ifindex
;
1264 api_nh
->type
= ifindex
? NEXTHOP_TYPE_IPV6_IFINDEX
1265 : NEXTHOP_TYPE_IPV6
;
1269 && bgp_is_valid_label(&mpinfo
->extra
->label
[0])
1270 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1271 has_valid_label
= 1;
1272 label
= label_pton(&mpinfo
->extra
->label
[0]);
1274 api_nh
->label_num
= 1;
1275 api_nh
->labels
[0] = label
;
1280 /* if this is a evpn route we don't have to include the label */
1281 if (has_valid_label
&& !(CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)))
1282 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
1284 if (info
->sub_type
!= BGP_ROUTE_AGGREGATE
)
1285 api
.nexthop_num
= valid_nh_count
;
1287 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1288 api
.metric
= metric
;
1291 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1295 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1297 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1298 api
.distance
= distance
;
1301 if (bgp_debug_zebra(p
)) {
1302 char prefix_buf
[PREFIX_STRLEN
];
1303 char nh_buf
[INET6_ADDRSTRLEN
];
1307 prefix2str(&api
.prefix
, prefix_buf
, sizeof(prefix_buf
));
1308 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1310 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1311 prefix_buf
, api
.metric
, api
.tag
, api
.nexthop_num
);
1312 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1313 api_nh
= &api
.nexthops
[i
];
1315 if (api_nh
->type
== NEXTHOP_TYPE_IPV4
)
1316 nh_family
= AF_INET
;
1318 nh_family
= AF_INET6
;
1319 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1322 label_buf
[0] = '\0';
1324 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1325 sprintf(label_buf
, "label %u",
1327 zlog_debug(" nhop [%d]: %s %s", i
+ 1, nh_buf
,
1332 zclient_route_send(valid_nh_count
? ZEBRA_ROUTE_ADD
1333 : ZEBRA_ROUTE_DELETE
,
1337 /* Announce all routes of a table to zebra */
1338 void bgp_zebra_announce_table(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1340 struct bgp_node
*rn
;
1341 struct bgp_table
*table
;
1342 struct bgp_info
*ri
;
1344 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1345 * know of this instance.
1347 if (!bgp_install_info_to_zebra(bgp
))
1350 table
= bgp
->rib
[afi
][safi
];
1354 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
1355 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1356 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
) &&
1358 (ri
->type
== ZEBRA_ROUTE_BGP
1359 && (ri
->sub_type
== BGP_ROUTE_NORMAL
1360 || ri
->sub_type
== BGP_ROUTE_IMPORTED
)))
1362 bgp_zebra_announce(rn
, &rn
->p
, ri
, bgp
, afi
,
1366 void bgp_zebra_withdraw(struct prefix
*p
, struct bgp_info
*info
,
1367 struct bgp
*bgp
, safi_t safi
)
1369 struct zapi_route api
;
1375 if (info
->type
== ZEBRA_ROUTE_BGP
1376 && info
->sub_type
== BGP_ROUTE_IMPORTED
) {
1378 struct bgp_info
*bi
;
1381 * Look at parent chain for peer sort
1383 for (bi
= info
; bi
->extra
&& bi
->extra
->parent
;
1384 bi
= bi
->extra
->parent
) {
1386 peer
= ((struct bgp_info
*)(bi
->extra
->parent
))->peer
;
1390 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1391 * know of this instance.
1393 if (!bgp_install_info_to_zebra(bgp
))
1396 memset(&api
, 0, sizeof(api
));
1397 memcpy(&api
.rmac
, &(info
->attr
->rmac
), sizeof(struct ethaddr
));
1398 api
.vrf_id
= bgp
->vrf_id
;
1399 api
.type
= ZEBRA_ROUTE_BGP
;
1403 /* If the route's source is EVPN, flag as such. */
1404 if (is_route_parent_evpn(info
))
1405 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1407 if (peer
->sort
== BGP_PEER_IBGP
) {
1408 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1409 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1412 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1413 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1414 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1415 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1417 if (bgp_debug_zebra(p
)) {
1418 char buf
[PREFIX_STRLEN
];
1420 prefix2str(&api
.prefix
, buf
, sizeof(buf
));
1421 zlog_debug("Tx route delete VRF %u %s", bgp
->vrf_id
, buf
);
1424 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
1427 struct bgp_redist
*bgp_redist_lookup(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1428 unsigned short instance
)
1430 struct list
*red_list
;
1431 struct listnode
*node
;
1432 struct bgp_redist
*red
;
1434 red_list
= bgp
->redist
[afi
][type
];
1438 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1439 if (red
->instance
== instance
)
1445 struct bgp_redist
*bgp_redist_add(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1446 unsigned short instance
)
1448 struct list
*red_list
;
1449 struct bgp_redist
*red
;
1451 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1455 if (!bgp
->redist
[afi
][type
])
1456 bgp
->redist
[afi
][type
] = list_new();
1458 red_list
= bgp
->redist
[afi
][type
];
1459 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
,
1460 sizeof(struct bgp_redist
));
1461 red
->instance
= instance
;
1463 listnode_add(red_list
, red
);
1468 static void bgp_redist_del(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1469 unsigned short instance
)
1471 struct bgp_redist
*red
;
1473 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1476 listnode_delete(bgp
->redist
[afi
][type
], red
);
1477 XFREE(MTYPE_BGP_REDIST
, red
);
1478 if (!bgp
->redist
[afi
][type
]->count
)
1479 list_delete_and_null(&bgp
->redist
[afi
][type
]);
1483 /* Other routes redistribution into BGP. */
1484 int bgp_redistribute_set(struct bgp
*bgp
, afi_t afi
, int type
,
1485 unsigned short instance
)
1488 /* Return if already redistribute flag is set. */
1490 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1494 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1496 if (vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1500 if (bgp
->vrf_id
== VRF_DEFAULT
1501 && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1502 vnc_export_bgp_enable(
1503 bgp
, afi
); /* only enables if mode bits cfg'd */
1507 vrf_bitmap_set(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1511 * Don't try to register if we're not connected to Zebra or Zebra
1512 * doesn't know of this instance.
1514 * When we come up later well resend if needed.
1516 if (!bgp_install_info_to_zebra(bgp
))
1519 if (BGP_DEBUG(zebra
, ZEBRA
))
1520 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1521 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1524 /* Send distribute add message to zebra. */
1525 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1526 instance
, bgp
->vrf_id
);
1531 int bgp_redistribute_resend(struct bgp
*bgp
, afi_t afi
, int type
,
1532 unsigned short instance
)
1534 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1535 * know of this instance.
1537 if (!bgp_install_info_to_zebra(bgp
))
1540 if (BGP_DEBUG(zebra
, ZEBRA
))
1541 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1542 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1545 /* Send distribute add message to zebra. */
1546 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1547 instance
, bgp
->vrf_id
);
1548 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1549 instance
, bgp
->vrf_id
);
1554 /* Redistribute with route-map specification. */
1555 int bgp_redistribute_rmap_set(struct bgp_redist
*red
, const char *name
)
1557 if (red
->rmap
.name
&& (strcmp(red
->rmap
.name
, name
) == 0))
1561 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1562 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1563 red
->rmap
.map
= route_map_lookup_by_name(name
);
1568 /* Redistribute with metric specification. */
1569 int bgp_redistribute_metric_set(struct bgp
*bgp
, struct bgp_redist
*red
,
1570 afi_t afi
, int type
, uint32_t metric
)
1572 struct bgp_node
*rn
;
1573 struct bgp_info
*ri
;
1575 if (red
->redist_metric_flag
&& red
->redist_metric
== metric
)
1578 red
->redist_metric_flag
= 1;
1579 red
->redist_metric
= metric
;
1581 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
1582 rn
= bgp_route_next(rn
)) {
1583 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
1584 if (ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
1586 && ri
->instance
== red
->instance
) {
1587 struct attr
*old_attr
;
1588 struct attr new_attr
;
1590 bgp_attr_dup(&new_attr
, ri
->attr
);
1591 new_attr
.med
= red
->redist_metric
;
1592 old_attr
= ri
->attr
;
1593 ri
->attr
= bgp_attr_intern(&new_attr
);
1594 bgp_attr_unintern(&old_attr
);
1596 bgp_info_set_flag(rn
, ri
,
1597 BGP_INFO_ATTR_CHANGED
);
1598 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1606 /* Unset redistribution. */
1607 int bgp_redistribute_unreg(struct bgp
*bgp
, afi_t afi
, int type
,
1608 unsigned short instance
)
1610 struct bgp_redist
*red
;
1612 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1616 /* Return if zebra connection is disabled. */
1618 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1621 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1623 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1625 vrf_bitmap_unset(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1629 if (bgp_install_info_to_zebra(bgp
)) {
1630 /* Send distribute delete message to zebra. */
1631 if (BGP_DEBUG(zebra
, ZEBRA
))
1632 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1633 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1635 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
1636 type
, instance
, bgp
->vrf_id
);
1639 /* Withdraw redistributed routes from current BGP's routing table. */
1640 bgp_redistribute_withdraw(bgp
, afi
, type
, instance
);
1645 /* Unset redistribution. */
1646 int bgp_redistribute_unset(struct bgp
*bgp
, afi_t afi
, int type
,
1647 unsigned short instance
)
1649 struct bgp_redist
*red
;
1652 * vnc and vpn->vrf checks must be before red check because
1653 * they operate within bgpd irrespective of zebra connection
1654 * status. red lookup fails if there is no zebra connection.
1657 if (bgp
->vrf_id
== VRF_DEFAULT
&& type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1658 vnc_export_bgp_disable(bgp
, afi
);
1662 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1666 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1668 /* Unset route-map. */
1670 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1671 red
->rmap
.name
= NULL
;
1672 red
->rmap
.map
= NULL
;
1675 red
->redist_metric_flag
= 0;
1676 red
->redist_metric
= 0;
1678 bgp_redist_del(bgp
, afi
, type
, instance
);
1683 /* Update redistribute vrf bitmap during triggers like
1684 restart networking or delete/add VRFs */
1685 void bgp_update_redist_vrf_bitmaps(struct bgp
*bgp
, vrf_id_t old_vrf_id
)
1690 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1691 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1692 if ((old_vrf_id
== VRF_UNKNOWN
)
1693 || vrf_bitmap_check(zclient
->redist
[afi
][i
],
1695 vrf_bitmap_unset(zclient
->redist
[afi
][i
],
1697 vrf_bitmap_set(zclient
->redist
[afi
][i
],
1703 void bgp_zclient_reset(void)
1705 zclient_reset(zclient
);
1708 /* Register this instance with Zebra. Invoked upon connect (for
1709 * default instance) and when other VRFs are learnt (or created and
1712 void bgp_zebra_instance_register(struct bgp
*bgp
)
1714 /* Don't try to register if we're not connected to Zebra */
1715 if (!zclient
|| zclient
->sock
< 0)
1718 if (BGP_DEBUG(zebra
, ZEBRA
))
1719 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
1721 /* Register for router-id, interfaces, redistributed routes. */
1722 zclient_send_reg_requests(zclient
, bgp
->vrf_id
);
1724 /* For default instance, register to learn about VNIs, if appropriate.
1726 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& is_evpn_enabled())
1727 bgp_zebra_advertise_all_vni(bgp
, 1);
1730 /* Deregister this instance with Zebra. Invoked upon the instance
1731 * being deleted (default or VRF) and it is already registered.
1733 void bgp_zebra_instance_deregister(struct bgp
*bgp
)
1735 /* Don't try to deregister if we're not connected to Zebra */
1736 if (zclient
->sock
< 0)
1739 if (BGP_DEBUG(zebra
, ZEBRA
))
1740 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
1742 /* For default instance, unregister learning about VNIs, if appropriate.
1744 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& is_evpn_enabled())
1745 bgp_zebra_advertise_all_vni(bgp
, 0);
1747 /* Deregister for router-id, interfaces, redistributed routes. */
1748 zclient_send_dereg_requests(zclient
, bgp
->vrf_id
);
1751 void bgp_zebra_initiate_radv(struct bgp
*bgp
, struct peer
*peer
)
1753 int ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
1755 /* Don't try to initiate if we're not connected to Zebra */
1756 if (zclient
->sock
< 0)
1759 if (BGP_DEBUG(zebra
, ZEBRA
))
1760 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
,
1763 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 1,
1767 void bgp_zebra_terminate_radv(struct bgp
*bgp
, struct peer
*peer
)
1769 /* Don't try to terminate if we're not connected to Zebra */
1770 if (zclient
->sock
< 0)
1773 if (BGP_DEBUG(zebra
, ZEBRA
))
1774 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
,
1777 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 0, 0);
1780 int bgp_zebra_advertise_subnet(struct bgp
*bgp
, int advertise
, vni_t vni
)
1782 struct stream
*s
= NULL
;
1785 if (!zclient
|| zclient
->sock
< 0)
1788 /* Don't try to register if Zebra doesn't know of this instance. */
1789 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1795 zclient_create_header(s
, ZEBRA_ADVERTISE_SUBNET
, bgp
->vrf_id
);
1796 stream_putc(s
, advertise
);
1797 stream_put3(s
, vni
);
1798 stream_putw_at(s
, 0, stream_get_endp(s
));
1800 return zclient_send_message(zclient
);
1803 int bgp_zebra_advertise_gw_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
1805 struct stream
*s
= NULL
;
1808 if (!zclient
|| zclient
->sock
< 0)
1811 /* Don't try to register if Zebra doesn't know of this instance. */
1812 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1818 zclient_create_header(s
, ZEBRA_ADVERTISE_DEFAULT_GW
, bgp
->vrf_id
);
1819 stream_putc(s
, advertise
);
1820 stream_put3(s
, vni
);
1821 stream_putw_at(s
, 0, stream_get_endp(s
));
1823 return zclient_send_message(zclient
);
1826 int bgp_zebra_advertise_all_vni(struct bgp
*bgp
, int advertise
)
1831 if (!zclient
|| zclient
->sock
< 0)
1834 /* Don't try to register if Zebra doesn't know of this instance. */
1835 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1841 zclient_create_header(s
, ZEBRA_ADVERTISE_ALL_VNI
, bgp
->vrf_id
);
1842 stream_putc(s
, advertise
);
1843 stream_putw_at(s
, 0, stream_get_endp(s
));
1845 return zclient_send_message(zclient
);
1848 /* BGP has established connection with Zebra. */
1849 static void bgp_zebra_connected(struct zclient
*zclient
)
1853 zclient_num_connects
++; /* increment even if not responding */
1855 /* At this point, we may or may not have BGP instances configured, but
1856 * we're only interested in the default VRF (others wouldn't have learnt
1857 * the VRF from Zebra yet.)
1859 bgp
= bgp_get_default();
1863 bgp_zebra_instance_register(bgp
);
1865 /* Send the client registration */
1866 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
1868 /* TODO - What if we have peers and networks configured, do we have to
1873 static int bgp_zebra_process_local_l3vni(int cmd
, struct zclient
*zclient
,
1874 zebra_size_t length
, vrf_id_t vrf_id
)
1877 char buf
[ETHER_ADDR_STRLEN
];
1879 struct ethaddr rmac
;
1880 struct in_addr originator_ip
;
1883 memset(&rmac
, 0, sizeof(struct ethaddr
));
1884 memset(&originator_ip
, 0, sizeof(struct in_addr
));
1886 l3vni
= stream_getl(s
);
1887 if (cmd
== ZEBRA_L3VNI_ADD
) {
1888 stream_get(&rmac
, s
, sizeof(struct ethaddr
));
1889 originator_ip
.s_addr
= stream_get_ipv4(s
);
1890 stream_get(&filter
, s
, sizeof(int));
1893 if (BGP_DEBUG(zebra
, ZEBRA
))
1894 zlog_debug("Rx L3-VNI %s VRF %s VNI %u RMAC %s filter %s",
1895 (cmd
== ZEBRA_L3VNI_ADD
) ? "add" : "del",
1896 vrf_id_to_name(vrf_id
), l3vni
,
1897 prefix_mac2str(&rmac
, buf
, sizeof(buf
)),
1898 filter
? "prefix-routes-only" : "none");
1900 if (cmd
== ZEBRA_L3VNI_ADD
)
1901 bgp_evpn_local_l3vni_add(l3vni
, vrf_id
, &rmac
, originator_ip
,
1904 bgp_evpn_local_l3vni_del(l3vni
, vrf_id
);
1909 static int bgp_zebra_process_local_vni(int command
, struct zclient
*zclient
,
1910 zebra_size_t length
, vrf_id_t vrf_id
)
1915 struct in_addr vtep_ip
= {INADDR_ANY
};
1916 vrf_id_t tenant_vrf_id
= VRF_DEFAULT
;
1919 vni
= stream_getl(s
);
1920 if (command
== ZEBRA_VNI_ADD
) {
1921 vtep_ip
.s_addr
= stream_get_ipv4(s
);
1922 stream_get(&tenant_vrf_id
, s
, sizeof(vrf_id_t
));
1925 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1929 if (BGP_DEBUG(zebra
, ZEBRA
))
1930 zlog_debug("Rx VNI %s VRF %s VNI %u tenant-vrf %s",
1931 (command
== ZEBRA_VNI_ADD
) ? "add" : "del",
1932 vrf_id_to_name(vrf_id
), vni
,
1933 vrf_id_to_name(tenant_vrf_id
));
1935 if (command
== ZEBRA_VNI_ADD
)
1936 return bgp_evpn_local_vni_add(
1937 bgp
, vni
, vtep_ip
.s_addr
? vtep_ip
: bgp
->router_id
,
1940 return bgp_evpn_local_vni_del(bgp
, vni
);
1943 static int bgp_zebra_process_local_macip(int command
, struct zclient
*zclient
,
1944 zebra_size_t length
, vrf_id_t vrf_id
)
1952 char buf
[ETHER_ADDR_STRLEN
];
1953 char buf1
[INET6_ADDRSTRLEN
];
1956 memset(&ip
, 0, sizeof(ip
));
1958 vni
= stream_getl(s
);
1959 stream_get(&mac
.octet
, s
, ETH_ALEN
);
1960 ipa_len
= stream_getl(s
);
1961 if (ipa_len
!= 0 && ipa_len
!= IPV4_MAX_BYTELEN
1962 && ipa_len
!= IPV6_MAX_BYTELEN
) {
1963 zlog_err("%u:Recv MACIP %s with invalid IP addr length %d",
1964 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
1971 (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
: IPADDR_V6
;
1972 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
1974 flags
= stream_getc(s
);
1976 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1980 if (BGP_DEBUG(zebra
, ZEBRA
))
1981 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u",
1982 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
1983 flags
, prefix_mac2str(&mac
, buf
, sizeof(buf
)),
1984 ipaddr2str(&ip
, buf1
, sizeof(buf1
)), vni
);
1986 if (command
== ZEBRA_MACIP_ADD
)
1987 return bgp_evpn_local_macip_add(bgp
, vni
, &mac
, &ip
, flags
);
1989 return bgp_evpn_local_macip_del(bgp
, vni
, &mac
, &ip
);
1992 static void bgp_zebra_process_local_ip_prefix(int cmd
, struct zclient
*zclient
,
1993 zebra_size_t length
,
1996 struct stream
*s
= NULL
;
1997 struct bgp
*bgp_vrf
= NULL
;
1999 char buf
[PREFIX_STRLEN
];
2001 memset(&p
, 0, sizeof(struct prefix
));
2003 stream_get(&p
, s
, sizeof(struct prefix
));
2005 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
2009 if (BGP_DEBUG(zebra
, ZEBRA
))
2010 zlog_debug("Recv prefix %s %s on vrf %s",
2011 prefix2str(&p
, buf
, sizeof(buf
)),
2012 (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) ? "ADD" : "DEL",
2013 vrf_id_to_name(vrf_id
));
2015 if (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) {
2017 if (p
.family
== AF_INET
)
2018 return bgp_evpn_advertise_type5_route(
2019 bgp_vrf
, &p
, NULL
, AFI_IP
, SAFI_UNICAST
);
2021 return bgp_evpn_advertise_type5_route(
2022 bgp_vrf
, &p
, NULL
, AFI_IP6
, SAFI_UNICAST
);
2025 if (p
.family
== AF_INET
)
2026 return bgp_evpn_withdraw_type5_route(
2027 bgp_vrf
, &p
, AFI_IP
, SAFI_UNICAST
);
2029 return bgp_evpn_withdraw_type5_route(
2030 bgp_vrf
, &p
, AFI_IP6
, SAFI_UNICAST
);
2034 extern struct zebra_privs_t bgpd_privs
;
2036 void bgp_zebra_init(struct thread_master
*master
)
2038 zclient_num_connects
= 0;
2040 /* Set default values. */
2041 zclient
= zclient_new_notify(master
, &zclient_options_default
);
2042 zclient_init(zclient
, ZEBRA_ROUTE_BGP
, 0, &bgpd_privs
);
2043 zclient
->zebra_connected
= bgp_zebra_connected
;
2044 zclient
->router_id_update
= bgp_router_id_update
;
2045 zclient
->interface_add
= bgp_interface_add
;
2046 zclient
->interface_delete
= bgp_interface_delete
;
2047 zclient
->interface_address_add
= bgp_interface_address_add
;
2048 zclient
->interface_address_delete
= bgp_interface_address_delete
;
2049 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
2050 zclient
->interface_nbr_address_delete
=
2051 bgp_interface_nbr_address_delete
;
2052 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
2053 zclient
->redistribute_route_add
= zebra_read_route
;
2054 zclient
->redistribute_route_del
= zebra_read_route
;
2055 zclient
->interface_up
= bgp_interface_up
;
2056 zclient
->interface_down
= bgp_interface_down
;
2057 zclient
->nexthop_update
= bgp_read_nexthop_update
;
2058 zclient
->import_check_update
= bgp_read_import_check_update
;
2059 zclient
->fec_update
= bgp_read_fec_update
;
2060 zclient
->local_vni_add
= bgp_zebra_process_local_vni
;
2061 zclient
->local_vni_del
= bgp_zebra_process_local_vni
;
2062 zclient
->local_macip_add
= bgp_zebra_process_local_macip
;
2063 zclient
->local_macip_del
= bgp_zebra_process_local_macip
;
2064 zclient
->local_l3vni_add
= bgp_zebra_process_local_l3vni
;
2065 zclient
->local_l3vni_del
= bgp_zebra_process_local_l3vni
;
2066 zclient
->local_ip_prefix_add
= bgp_zebra_process_local_ip_prefix
;
2067 zclient
->local_ip_prefix_del
= bgp_zebra_process_local_ip_prefix
;
2070 void bgp_zebra_destroy(void)
2072 if (zclient
== NULL
)
2074 zclient_stop(zclient
);
2075 zclient_free(zclient
);
2079 int bgp_zebra_num_connects(void)
2081 return zclient_num_connects
;