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 void bgp_zebra_announce(struct bgp_node
*rn
, struct prefix
*p
,
974 struct bgp_info
*info
, struct bgp
*bgp
, afi_t afi
,
977 struct zapi_route api
;
978 struct zapi_nexthop
*api_nh
;
980 unsigned int valid_nh_count
= 0;
981 int has_valid_label
= 0;
984 struct bgp_info
*mpinfo
;
986 struct attr local_attr
;
987 struct bgp_info local_info
;
988 struct bgp_info
*mpinfo_cp
= &local_info
;
993 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
994 * know of this instance.
996 if (!bgp_install_info_to_zebra(bgp
))
999 if (bgp
->main_zebra_update_hold
)
1003 * vrf leaking support (will have only one nexthop)
1005 if (info
->extra
&& info
->extra
->bgp_orig
)
1008 /* Make Zebra API structure. */
1009 memset(&api
, 0, sizeof(api
));
1010 memcpy(&api
.rmac
, &(info
->attr
->rmac
), sizeof(struct ethaddr
));
1011 api
.vrf_id
= bgp
->vrf_id
;
1012 api
.type
= ZEBRA_ROUTE_BGP
;
1015 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1019 if (info
->type
== ZEBRA_ROUTE_BGP
1020 && info
->sub_type
== BGP_ROUTE_IMPORTED
) {
1022 struct bgp_info
*bi
;
1025 * Look at parent chain for peer sort
1027 for (bi
= info
; bi
->extra
&& bi
->extra
->parent
;
1028 bi
= bi
->extra
->parent
) {
1030 peer
= ((struct bgp_info
*)(bi
->extra
->parent
))->peer
;
1034 tag
= info
->attr
->tag
;
1036 /* When we create an aggregate route we must also install a Null0 route
1039 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1040 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
1042 /* If it is an EVPN route mark as such.
1043 * Currently presence of rmac in attr denotes
1044 * this is an EVPN type-2 route
1046 if (!is_zero_mac(&(info
->attr
->rmac
)))
1047 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1049 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
1050 || info
->sub_type
== BGP_ROUTE_AGGREGATE
) {
1051 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1052 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1055 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1056 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1057 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1059 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1061 /* Metric is currently based on the best-path only */
1062 metric
= info
->attr
->med
;
1063 for (mpinfo
= info
; mpinfo
; mpinfo
= bgp_info_mpath_next(mpinfo
)) {
1064 if (valid_nh_count
>= multipath_num
)
1067 *mpinfo_cp
= *mpinfo
;
1069 /* Get nexthop address-family */
1070 if (p
->family
== AF_INET
1071 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
))
1072 nh_family
= AF_INET
;
1073 else if (p
->family
== AF_INET6
1074 || (p
->family
== AF_INET
1075 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
)))
1076 nh_family
= AF_INET6
;
1080 api_nh
= &api
.nexthops
[valid_nh_count
];
1081 api_nh
->vrf_id
= bgp
->vrf_id
;
1083 if (nh_family
== AF_INET
) {
1084 struct in_addr
*nexthop
;
1086 if (bgp_debug_zebra(&api
.prefix
)) {
1087 char buf_prefix
[PREFIX_STRLEN
];
1088 prefix2str(&api
.prefix
, buf_prefix
,
1089 sizeof(buf_prefix
));
1090 if (mpinfo
->extra
) {
1092 "%s: p=%s, bgp_is_valid_label: %d",
1093 __func__
, buf_prefix
,
1099 "%s: p=%s, extra is NULL, no label",
1100 __func__
, buf_prefix
);
1104 if (bgp
->table_map
[afi
][safi
].name
|| nh_othervrf
) {
1105 /* Copy info and attributes, so the route-map
1106 apply doesn't modify the BGP route info. */
1107 local_attr
= *mpinfo
->attr
;
1108 mpinfo_cp
->attr
= &local_attr
;
1110 /* allow route-map to modify */
1111 local_attr
.nexthop
=
1112 info
->extra
->nexthop_orig
.u
1117 if (bgp
->table_map
[afi
][safi
].name
) {
1118 if (!bgp_table_map_apply(
1119 bgp
->table_map
[afi
][safi
].map
, p
,
1123 /* metric/tag is only allowed to be
1124 * overridden on 1st nexthop */
1125 if (mpinfo
== info
) {
1126 metric
= mpinfo_cp
->attr
->med
;
1127 tag
= mpinfo_cp
->attr
->tag
;
1131 nexthop
= &mpinfo_cp
->attr
->nexthop
;
1133 api_nh
->gate
.ipv4
= *nexthop
;
1134 api_nh
->vrf_id
= nh_othervrf
1135 ? info
->extra
->bgp_orig
->vrf_id
1137 /* EVPN type-2 routes are
1138 programmed as onlink on l3-vni SVI
1140 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1141 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1143 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1146 struct in6_addr
*nexthop
;
1150 if (bgp
->table_map
[afi
][safi
].name
|| nh_othervrf
) {
1151 /* Copy info and attributes, so the route-map
1152 apply doesn't modify the BGP route info. */
1153 local_attr
= *mpinfo
->attr
;
1154 mpinfo_cp
->attr
= &local_attr
;
1156 /* allow route-map to modify */
1157 local_attr
.mp_nexthop_global
=
1158 info
->extra
->nexthop_orig
.u
1160 local_attr
.mp_nexthop_len
=
1161 BGP_ATTR_NHLEN_IPV6_GLOBAL
;
1165 if (bgp
->table_map
[afi
][safi
].name
) {
1166 /* Copy info and attributes, so the route-map
1167 apply doesn't modify the BGP route info. */
1168 local_attr
= *mpinfo
->attr
;
1169 mpinfo_cp
->attr
= &local_attr
;
1171 if (!bgp_table_map_apply(
1172 bgp
->table_map
[afi
][safi
].map
, p
,
1176 /* metric/tag is only allowed to be
1177 * overridden on 1st nexthop */
1178 if (mpinfo
== info
) {
1179 metric
= mpinfo_cp
->attr
->med
;
1180 tag
= mpinfo_cp
->attr
->tag
;
1183 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo_cp
);
1185 if ((mpinfo
== info
)
1186 && mpinfo
->attr
->mp_nexthop_len
1187 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1188 if (mpinfo
->peer
->nexthop
.ifp
)
1189 ifindex
= mpinfo
->peer
->nexthop
.ifp
1193 if (mpinfo
->peer
->conf_if
)
1194 ifindex
= mpinfo
->peer
->ifp
->ifindex
;
1195 else if (mpinfo
->peer
->ifname
)
1196 ifindex
= ifname2ifindex(
1197 mpinfo
->peer
->ifname
,
1199 else if (mpinfo
->peer
->nexthop
.ifp
)
1200 ifindex
= mpinfo
->peer
->nexthop
.ifp
1206 api_nh
->gate
.ipv6
= *nexthop
;
1207 api_nh
->ifindex
= ifindex
;
1208 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1209 /* api_nh->vrf_id is not set for normal case? */
1211 api_nh
->vrf_id
= info
->extra
->bgp_orig
->vrf_id
;
1215 && bgp_is_valid_label(&mpinfo
->extra
->label
[0])
1216 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1217 has_valid_label
= 1;
1218 label
= label_pton(&mpinfo
->extra
->label
[0]);
1220 api_nh
->label_num
= 1;
1221 api_nh
->labels
[0] = label
;
1226 /* if this is a evpn route we don't have to include the label */
1227 if (has_valid_label
&& !(CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)))
1228 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
1230 if (info
->sub_type
!= BGP_ROUTE_AGGREGATE
)
1231 api
.nexthop_num
= valid_nh_count
;
1233 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1234 api
.metric
= metric
;
1237 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1241 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1243 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1244 api
.distance
= distance
;
1247 if (bgp_debug_zebra(p
)) {
1248 char prefix_buf
[PREFIX_STRLEN
];
1249 char nh_buf
[INET6_ADDRSTRLEN
];
1253 prefix2str(&api
.prefix
, prefix_buf
, sizeof(prefix_buf
));
1254 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1256 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1257 prefix_buf
, api
.metric
, api
.tag
, api
.nexthop_num
);
1258 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1259 api_nh
= &api
.nexthops
[i
];
1261 if (api_nh
->type
== NEXTHOP_TYPE_IPV4
)
1262 nh_family
= AF_INET
;
1264 nh_family
= AF_INET6
;
1265 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1268 label_buf
[0] = '\0';
1270 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1271 sprintf(label_buf
, "label %u",
1273 zlog_debug(" nhop [%d]: %s %s", i
+ 1, nh_buf
,
1278 zclient_route_send(valid_nh_count
? ZEBRA_ROUTE_ADD
1279 : ZEBRA_ROUTE_DELETE
,
1283 /* Announce all routes of a table to zebra */
1284 void bgp_zebra_announce_table(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1286 struct bgp_node
*rn
;
1287 struct bgp_table
*table
;
1288 struct bgp_info
*ri
;
1290 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1291 * know of this instance.
1293 if (!bgp_install_info_to_zebra(bgp
))
1296 table
= bgp
->rib
[afi
][safi
];
1300 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
1301 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1302 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
) &&
1304 (ri
->type
== ZEBRA_ROUTE_BGP
1305 && (ri
->sub_type
== BGP_ROUTE_NORMAL
1306 || ri
->sub_type
== BGP_ROUTE_IMPORTED
)))
1308 bgp_zebra_announce(rn
, &rn
->p
, ri
, bgp
, afi
,
1312 void bgp_zebra_withdraw(struct prefix
*p
, struct bgp_info
*info
, safi_t safi
)
1314 struct zapi_route api
;
1320 if (info
->type
== ZEBRA_ROUTE_BGP
1321 && info
->sub_type
== BGP_ROUTE_IMPORTED
) {
1323 struct bgp_info
*bi
;
1326 * Look at parent chain for peer sort
1328 for (bi
= info
; bi
->extra
&& bi
->extra
->parent
;
1329 bi
= bi
->extra
->parent
) {
1331 peer
= ((struct bgp_info
*)(bi
->extra
->parent
))->peer
;
1335 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1336 * know of this instance.
1338 if (!bgp_install_info_to_zebra(peer
->bgp
))
1341 memset(&api
, 0, sizeof(api
));
1342 memcpy(&api
.rmac
, &(info
->attr
->rmac
), sizeof(struct ethaddr
));
1343 api
.vrf_id
= peer
->bgp
->vrf_id
;
1344 api
.type
= ZEBRA_ROUTE_BGP
;
1348 /* If it is an EVPN route mark as such.
1349 * Currently presence of rmac in attr denotes
1350 * this is an EVPN type-2 route
1352 if (!is_zero_mac(&(info
->attr
->rmac
)))
1353 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1355 if (peer
->sort
== BGP_PEER_IBGP
) {
1356 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1357 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1360 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1361 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1362 || bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1363 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1365 if (bgp_debug_zebra(p
)) {
1366 char buf
[PREFIX_STRLEN
];
1368 prefix2str(&api
.prefix
, buf
, sizeof(buf
));
1369 zlog_debug("Tx route delete VRF %u %s", peer
->bgp
->vrf_id
, buf
);
1372 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
1375 struct bgp_redist
*bgp_redist_lookup(struct bgp
*bgp
, afi_t afi
, u_char type
,
1378 struct list
*red_list
;
1379 struct listnode
*node
;
1380 struct bgp_redist
*red
;
1382 red_list
= bgp
->redist
[afi
][type
];
1386 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1387 if (red
->instance
== instance
)
1393 struct bgp_redist
*bgp_redist_add(struct bgp
*bgp
, afi_t afi
, u_char type
,
1396 struct list
*red_list
;
1397 struct bgp_redist
*red
;
1399 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1403 if (!bgp
->redist
[afi
][type
])
1404 bgp
->redist
[afi
][type
] = list_new();
1406 red_list
= bgp
->redist
[afi
][type
];
1407 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
,
1408 sizeof(struct bgp_redist
));
1409 red
->instance
= instance
;
1411 listnode_add(red_list
, red
);
1416 static void bgp_redist_del(struct bgp
*bgp
, afi_t afi
, u_char type
,
1419 struct bgp_redist
*red
;
1421 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1424 listnode_delete(bgp
->redist
[afi
][type
], red
);
1425 XFREE(MTYPE_BGP_REDIST
, red
);
1426 if (!bgp
->redist
[afi
][type
]->count
)
1427 list_delete_and_null(&bgp
->redist
[afi
][type
]);
1431 /* Other routes redistribution into BGP. */
1432 int bgp_redistribute_set(struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1435 /* Return if already redistribute flag is set. */
1437 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1441 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1443 if (vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1447 if (bgp
->vrf_id
== VRF_DEFAULT
1448 && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1449 vnc_export_bgp_enable(
1450 bgp
, afi
); /* only enables if mode bits cfg'd */
1454 vrf_bitmap_set(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1458 * Don't try to register if we're not connected to Zebra or Zebra
1459 * doesn't know of this instance.
1461 * When we come up later well resend if needed.
1463 if (!bgp_install_info_to_zebra(bgp
))
1466 if (BGP_DEBUG(zebra
, ZEBRA
))
1467 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1468 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1471 /* Send distribute add message to zebra. */
1472 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1473 instance
, bgp
->vrf_id
);
1478 int bgp_redistribute_resend(struct bgp
*bgp
, afi_t afi
, int type
,
1481 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1482 * know of this instance.
1484 if (!bgp_install_info_to_zebra(bgp
))
1487 if (BGP_DEBUG(zebra
, ZEBRA
))
1488 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1489 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1492 /* Send distribute add message to zebra. */
1493 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1494 instance
, bgp
->vrf_id
);
1495 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1496 instance
, bgp
->vrf_id
);
1501 /* Redistribute with route-map specification. */
1502 int bgp_redistribute_rmap_set(struct bgp_redist
*red
, const char *name
)
1504 if (red
->rmap
.name
&& (strcmp(red
->rmap
.name
, name
) == 0))
1508 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1509 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1510 red
->rmap
.map
= route_map_lookup_by_name(name
);
1515 /* Redistribute with metric specification. */
1516 int bgp_redistribute_metric_set(struct bgp
*bgp
, struct bgp_redist
*red
,
1517 afi_t afi
, int type
, u_int32_t metric
)
1519 struct bgp_node
*rn
;
1520 struct bgp_info
*ri
;
1522 if (red
->redist_metric_flag
&& red
->redist_metric
== metric
)
1525 red
->redist_metric_flag
= 1;
1526 red
->redist_metric
= metric
;
1528 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
1529 rn
= bgp_route_next(rn
)) {
1530 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
1531 if (ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
1533 && ri
->instance
== red
->instance
) {
1534 struct attr
*old_attr
;
1535 struct attr new_attr
;
1537 bgp_attr_dup(&new_attr
, ri
->attr
);
1538 new_attr
.med
= red
->redist_metric
;
1539 old_attr
= ri
->attr
;
1540 ri
->attr
= bgp_attr_intern(&new_attr
);
1541 bgp_attr_unintern(&old_attr
);
1543 bgp_info_set_flag(rn
, ri
,
1544 BGP_INFO_ATTR_CHANGED
);
1545 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1553 /* Unset redistribution. */
1554 int bgp_redistribute_unreg(struct bgp
*bgp
, afi_t afi
, int type
,
1557 struct bgp_redist
*red
;
1559 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1563 /* Return if zebra connection is disabled. */
1565 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1568 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1570 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1572 vrf_bitmap_unset(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1576 if (bgp_install_info_to_zebra(bgp
)) {
1577 /* Send distribute delete message to zebra. */
1578 if (BGP_DEBUG(zebra
, ZEBRA
))
1579 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1580 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1582 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
1583 type
, instance
, bgp
->vrf_id
);
1586 /* Withdraw redistributed routes from current BGP's routing table. */
1587 bgp_redistribute_withdraw(bgp
, afi
, type
, instance
);
1592 /* Unset redistribution. */
1593 int bgp_redistribute_unset(struct bgp
*bgp
, afi_t afi
, int type
,
1596 struct bgp_redist
*red
;
1599 * vnc and vpn->vrf checks must be before red check because
1600 * they operate within bgpd irrespective of zebra connection
1601 * status. red lookup fails if there is no zebra connection.
1604 if (bgp
->vrf_id
== VRF_DEFAULT
&& type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1605 vnc_export_bgp_disable(bgp
, afi
);
1609 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1613 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1615 /* Unset route-map. */
1617 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1618 red
->rmap
.name
= NULL
;
1619 red
->rmap
.map
= NULL
;
1622 red
->redist_metric_flag
= 0;
1623 red
->redist_metric
= 0;
1625 bgp_redist_del(bgp
, afi
, type
, instance
);
1630 /* Update redistribute vrf bitmap during triggers like
1631 restart networking or delete/add VRFs */
1632 void bgp_update_redist_vrf_bitmaps(struct bgp
*bgp
, vrf_id_t old_vrf_id
)
1637 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1638 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1639 if ((old_vrf_id
== VRF_UNKNOWN
)
1640 || vrf_bitmap_check(zclient
->redist
[afi
][i
],
1642 vrf_bitmap_unset(zclient
->redist
[afi
][i
],
1644 vrf_bitmap_set(zclient
->redist
[afi
][i
],
1650 void bgp_zclient_reset(void)
1652 zclient_reset(zclient
);
1655 /* Register this instance with Zebra. Invoked upon connect (for
1656 * default instance) and when other VRFs are learnt (or created and
1659 void bgp_zebra_instance_register(struct bgp
*bgp
)
1661 /* Don't try to register if we're not connected to Zebra */
1662 if (!zclient
|| zclient
->sock
< 0)
1665 if (BGP_DEBUG(zebra
, ZEBRA
))
1666 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
1668 /* Register for router-id, interfaces, redistributed routes. */
1669 zclient_send_reg_requests(zclient
, bgp
->vrf_id
);
1671 /* For default instance, register to learn about VNIs, if appropriate.
1673 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& is_evpn_enabled())
1674 bgp_zebra_advertise_all_vni(bgp
, 1);
1677 /* Deregister this instance with Zebra. Invoked upon the instance
1678 * being deleted (default or VRF) and it is already registered.
1680 void bgp_zebra_instance_deregister(struct bgp
*bgp
)
1682 /* Don't try to deregister if we're not connected to Zebra */
1683 if (zclient
->sock
< 0)
1686 if (BGP_DEBUG(zebra
, ZEBRA
))
1687 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
1689 /* For default instance, unregister learning about VNIs, if appropriate.
1691 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& is_evpn_enabled())
1692 bgp_zebra_advertise_all_vni(bgp
, 0);
1694 /* Deregister for router-id, interfaces, redistributed routes. */
1695 zclient_send_dereg_requests(zclient
, bgp
->vrf_id
);
1698 void bgp_zebra_initiate_radv(struct bgp
*bgp
, struct peer
*peer
)
1700 int ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
1702 /* Don't try to initiate if we're not connected to Zebra */
1703 if (zclient
->sock
< 0)
1706 if (BGP_DEBUG(zebra
, ZEBRA
))
1707 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
,
1710 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 1,
1714 void bgp_zebra_terminate_radv(struct bgp
*bgp
, struct peer
*peer
)
1716 /* Don't try to terminate if we're not connected to Zebra */
1717 if (zclient
->sock
< 0)
1720 if (BGP_DEBUG(zebra
, ZEBRA
))
1721 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
,
1724 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 0, 0);
1727 int bgp_zebra_advertise_subnet(struct bgp
*bgp
, int advertise
, vni_t vni
)
1729 struct stream
*s
= NULL
;
1732 if (!zclient
|| zclient
->sock
< 0)
1735 /* Don't try to register if Zebra doesn't know of this instance. */
1736 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1742 zclient_create_header(s
, ZEBRA_ADVERTISE_SUBNET
, bgp
->vrf_id
);
1743 stream_putc(s
, advertise
);
1744 stream_put3(s
, vni
);
1745 stream_putw_at(s
, 0, stream_get_endp(s
));
1747 return zclient_send_message(zclient
);
1750 int bgp_zebra_advertise_gw_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
1752 struct stream
*s
= NULL
;
1755 if (!zclient
|| zclient
->sock
< 0)
1758 /* Don't try to register if Zebra doesn't know of this instance. */
1759 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1765 zclient_create_header(s
, ZEBRA_ADVERTISE_DEFAULT_GW
, bgp
->vrf_id
);
1766 stream_putc(s
, advertise
);
1767 stream_put3(s
, vni
);
1768 stream_putw_at(s
, 0, stream_get_endp(s
));
1770 return zclient_send_message(zclient
);
1773 int bgp_zebra_advertise_all_vni(struct bgp
*bgp
, int advertise
)
1778 if (!zclient
|| zclient
->sock
< 0)
1781 /* Don't try to register if Zebra doesn't know of this instance. */
1782 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1788 zclient_create_header(s
, ZEBRA_ADVERTISE_ALL_VNI
, bgp
->vrf_id
);
1789 stream_putc(s
, advertise
);
1790 stream_putw_at(s
, 0, stream_get_endp(s
));
1792 return zclient_send_message(zclient
);
1795 /* BGP has established connection with Zebra. */
1796 static void bgp_zebra_connected(struct zclient
*zclient
)
1800 zclient_num_connects
++; /* increment even if not responding */
1802 /* At this point, we may or may not have BGP instances configured, but
1803 * we're only interested in the default VRF (others wouldn't have learnt
1804 * the VRF from Zebra yet.)
1806 bgp
= bgp_get_default();
1810 bgp_zebra_instance_register(bgp
);
1812 /* Send the client registration */
1813 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
1815 /* TODO - What if we have peers and networks configured, do we have to
1820 static int bgp_zebra_process_local_l3vni(int cmd
, struct zclient
*zclient
,
1821 zebra_size_t length
, vrf_id_t vrf_id
)
1824 char buf
[ETHER_ADDR_STRLEN
];
1826 struct ethaddr rmac
;
1827 struct in_addr originator_ip
;
1830 memset(&rmac
, 0, sizeof(struct ethaddr
));
1831 memset(&originator_ip
, 0, sizeof(struct in_addr
));
1833 l3vni
= stream_getl(s
);
1834 if (cmd
== ZEBRA_L3VNI_ADD
) {
1835 stream_get(&rmac
, s
, sizeof(struct ethaddr
));
1836 originator_ip
.s_addr
= stream_get_ipv4(s
);
1837 stream_get(&filter
, s
, sizeof(int));
1840 if (BGP_DEBUG(zebra
, ZEBRA
))
1841 zlog_debug("Rx L3-VNI %s VRF %s VNI %u RMAC %s filter %s",
1842 (cmd
== ZEBRA_L3VNI_ADD
) ? "add" : "del",
1843 vrf_id_to_name(vrf_id
), l3vni
,
1844 prefix_mac2str(&rmac
, buf
, sizeof(buf
)),
1845 filter
? "prefix-routes-only" : "none");
1847 if (cmd
== ZEBRA_L3VNI_ADD
)
1848 bgp_evpn_local_l3vni_add(l3vni
, vrf_id
, &rmac
, originator_ip
,
1851 bgp_evpn_local_l3vni_del(l3vni
, vrf_id
);
1856 static int bgp_zebra_process_local_vni(int command
, struct zclient
*zclient
,
1857 zebra_size_t length
, vrf_id_t vrf_id
)
1862 struct in_addr vtep_ip
= {INADDR_ANY
};
1863 vrf_id_t tenant_vrf_id
= VRF_DEFAULT
;
1866 vni
= stream_getl(s
);
1867 if (command
== ZEBRA_VNI_ADD
) {
1868 vtep_ip
.s_addr
= stream_get_ipv4(s
);
1869 stream_get(&tenant_vrf_id
, s
, sizeof(vrf_id_t
));
1872 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1876 if (BGP_DEBUG(zebra
, ZEBRA
))
1877 zlog_debug("Rx VNI %s VRF %s VNI %u tenant-vrf %s",
1878 (command
== ZEBRA_VNI_ADD
) ? "add" : "del",
1879 vrf_id_to_name(vrf_id
), vni
,
1880 vrf_id_to_name(tenant_vrf_id
));
1882 if (command
== ZEBRA_VNI_ADD
)
1883 return bgp_evpn_local_vni_add(
1884 bgp
, vni
, vtep_ip
.s_addr
? vtep_ip
: bgp
->router_id
,
1887 return bgp_evpn_local_vni_del(bgp
, vni
);
1890 static int bgp_zebra_process_local_macip(int command
, struct zclient
*zclient
,
1891 zebra_size_t length
, vrf_id_t vrf_id
)
1899 char buf
[ETHER_ADDR_STRLEN
];
1900 char buf1
[INET6_ADDRSTRLEN
];
1903 memset(&ip
, 0, sizeof(ip
));
1905 vni
= stream_getl(s
);
1906 stream_get(&mac
.octet
, s
, ETH_ALEN
);
1907 ipa_len
= stream_getl(s
);
1908 if (ipa_len
!= 0 && ipa_len
!= IPV4_MAX_BYTELEN
1909 && ipa_len
!= IPV6_MAX_BYTELEN
) {
1910 zlog_err("%u:Recv MACIP %s with invalid IP addr length %d",
1911 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
1918 (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
: IPADDR_V6
;
1919 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
1921 flags
= stream_getc(s
);
1923 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1927 if (BGP_DEBUG(zebra
, ZEBRA
))
1928 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u",
1929 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
1930 flags
, prefix_mac2str(&mac
, buf
, sizeof(buf
)),
1931 ipaddr2str(&ip
, buf1
, sizeof(buf1
)), vni
);
1933 if (command
== ZEBRA_MACIP_ADD
)
1934 return bgp_evpn_local_macip_add(bgp
, vni
, &mac
, &ip
, flags
);
1936 return bgp_evpn_local_macip_del(bgp
, vni
, &mac
, &ip
);
1939 static void bgp_zebra_process_local_ip_prefix(int cmd
, struct zclient
*zclient
,
1940 zebra_size_t length
,
1943 struct stream
*s
= NULL
;
1944 struct bgp
*bgp_vrf
= NULL
;
1946 char buf
[PREFIX_STRLEN
];
1948 memset(&p
, 0, sizeof(struct prefix
));
1950 stream_get(&p
, s
, sizeof(struct prefix
));
1952 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
1956 if (BGP_DEBUG(zebra
, ZEBRA
))
1957 zlog_debug("Recv prefix %s %s on vrf %s",
1958 prefix2str(&p
, buf
, sizeof(buf
)),
1959 (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) ? "ADD" : "DEL",
1960 vrf_id_to_name(vrf_id
));
1962 if (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) {
1964 if (p
.family
== AF_INET
)
1965 return bgp_evpn_advertise_type5_route(
1966 bgp_vrf
, &p
, NULL
, AFI_IP
, SAFI_UNICAST
);
1968 return bgp_evpn_advertise_type5_route(
1969 bgp_vrf
, &p
, NULL
, AFI_IP6
, SAFI_UNICAST
);
1972 if (p
.family
== AF_INET
)
1973 return bgp_evpn_withdraw_type5_route(
1974 bgp_vrf
, &p
, AFI_IP
, SAFI_UNICAST
);
1976 return bgp_evpn_withdraw_type5_route(
1977 bgp_vrf
, &p
, AFI_IP6
, SAFI_UNICAST
);
1981 extern struct zebra_privs_t bgpd_privs
;
1983 void bgp_zebra_init(struct thread_master
*master
)
1985 zclient_num_connects
= 0;
1987 /* Set default values. */
1988 zclient
= zclient_new_notify(master
, &zclient_options_default
);
1989 zclient_init(zclient
, ZEBRA_ROUTE_BGP
, 0, &bgpd_privs
);
1990 zclient
->zebra_connected
= bgp_zebra_connected
;
1991 zclient
->router_id_update
= bgp_router_id_update
;
1992 zclient
->interface_add
= bgp_interface_add
;
1993 zclient
->interface_delete
= bgp_interface_delete
;
1994 zclient
->interface_address_add
= bgp_interface_address_add
;
1995 zclient
->interface_address_delete
= bgp_interface_address_delete
;
1996 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
1997 zclient
->interface_nbr_address_delete
=
1998 bgp_interface_nbr_address_delete
;
1999 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
2000 zclient
->redistribute_route_add
= zebra_read_route
;
2001 zclient
->redistribute_route_del
= zebra_read_route
;
2002 zclient
->interface_up
= bgp_interface_up
;
2003 zclient
->interface_down
= bgp_interface_down
;
2004 zclient
->nexthop_update
= bgp_read_nexthop_update
;
2005 zclient
->import_check_update
= bgp_read_import_check_update
;
2006 zclient
->fec_update
= bgp_read_fec_update
;
2007 zclient
->local_vni_add
= bgp_zebra_process_local_vni
;
2008 zclient
->local_vni_del
= bgp_zebra_process_local_vni
;
2009 zclient
->local_macip_add
= bgp_zebra_process_local_macip
;
2010 zclient
->local_macip_del
= bgp_zebra_process_local_macip
;
2011 zclient
->local_l3vni_add
= bgp_zebra_process_local_l3vni
;
2012 zclient
->local_l3vni_del
= bgp_zebra_process_local_l3vni
;
2013 zclient
->local_ip_prefix_add
= bgp_zebra_process_local_ip_prefix
;
2014 zclient
->local_ip_prefix_del
= bgp_zebra_process_local_ip_prefix
;
2017 void bgp_zebra_destroy(void)
2019 if (zclient
== NULL
)
2021 zclient_stop(zclient
);
2022 zclient_free(zclient
);
2026 int bgp_zebra_num_connects(void)
2028 return zclient_num_connects
;