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"
58 /* All information about zebra. */
59 struct zclient
*zclient
= NULL
;
61 /* Can we install into zebra? */
62 static inline int bgp_install_info_to_zebra(struct bgp
*bgp
)
64 if (zclient
->sock
<= 0)
67 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
73 int zclient_num_connects
;
75 /* Router-id update message from zebra. */
76 static int bgp_router_id_update(int command
, struct zclient
*zclient
,
77 zebra_size_t length
, vrf_id_t vrf_id
)
79 struct prefix router_id
;
81 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
83 if (BGP_DEBUG(zebra
, ZEBRA
)) {
84 char buf
[PREFIX2STR_BUFFER
];
85 prefix2str(&router_id
, buf
, sizeof(buf
));
86 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id
, buf
);
89 bgp_router_id_zebra_bump(vrf_id
, &router_id
);
93 /* Nexthop update message from zebra. */
94 static int bgp_read_nexthop_update(int command
, struct zclient
*zclient
,
95 zebra_size_t length
, vrf_id_t vrf_id
)
97 bgp_parse_nexthop_update(command
, vrf_id
);
101 static int bgp_read_import_check_update(int command
, struct zclient
*zclient
,
102 zebra_size_t length
, vrf_id_t vrf_id
)
104 bgp_parse_nexthop_update(command
, vrf_id
);
108 /* Set or clear interface on which unnumbered neighbor is configured. This
109 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
112 static void bgp_update_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
,
113 struct interface
*upd_ifp
)
115 struct listnode
*node
, *nnode
;
118 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
119 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)) {
122 bgp_zebra_initiate_radv(bgp
, peer
);
124 bgp_zebra_terminate_radv(bgp
, peer
);
131 static int bgp_read_fec_update(int command
, struct zclient
*zclient
,
134 bgp_parse_fec_update();
138 static void bgp_start_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
)
140 struct listnode
*node
, *nnode
;
143 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
144 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)
145 && peer
->status
!= Established
) {
146 if (peer_active(peer
))
147 BGP_EVENT_ADD(peer
, BGP_Stop
);
148 BGP_EVENT_ADD(peer
, BGP_Start
);
153 static void bgp_nbr_connected_add(struct bgp
*bgp
, struct nbr_connected
*ifc
)
155 struct listnode
*node
;
156 struct connected
*connected
;
157 struct interface
*ifp
;
160 /* Kick-off the FSM for any relevant peers only if there is a
161 * valid local address on the interface.
164 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
165 p
= connected
->address
;
166 if (p
->family
== AF_INET6
167 && IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
173 bgp_start_interface_nbrs(bgp
, ifp
);
176 static void bgp_nbr_connected_delete(struct bgp
*bgp
, struct nbr_connected
*ifc
,
179 struct listnode
*node
, *nnode
;
181 struct interface
*ifp
;
183 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
185 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)) {
186 peer
->last_reset
= PEER_DOWN_NBR_ADDR_DEL
;
187 BGP_EVENT_ADD(peer
, BGP_Stop
);
190 /* Free neighbor also, if we're asked to. */
193 listnode_delete(ifp
->nbr_connected
, ifc
);
194 nbr_connected_free(ifc
);
198 /* Inteface addition message from zebra. */
199 static int bgp_interface_add(int command
, struct zclient
*zclient
,
200 zebra_size_t length
, vrf_id_t vrf_id
)
202 struct interface
*ifp
;
205 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
206 if (!ifp
) // unexpected
209 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
210 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id
, ifp
->name
);
212 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
216 bgp_update_interface_nbrs(bgp
, ifp
, ifp
);
220 static int bgp_interface_delete(int command
, struct zclient
*zclient
,
221 zebra_size_t length
, vrf_id_t vrf_id
)
224 struct interface
*ifp
;
227 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
232 ifp
= zebra_interface_state_read(s
, vrf_id
);
233 if (!ifp
) /* This may happen if we've just unregistered for a VRF. */
236 if (BGP_DEBUG(zebra
, ZEBRA
))
237 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id
, ifp
->name
);
239 bgp_update_interface_nbrs(bgp
, ifp
, NULL
);
241 if_set_index(ifp
, IFINDEX_INTERNAL
);
245 static int bgp_interface_up(int command
, struct zclient
*zclient
,
246 zebra_size_t length
, vrf_id_t vrf_id
)
249 struct interface
*ifp
;
251 struct nbr_connected
*nc
;
252 struct listnode
*node
, *nnode
;
255 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
260 ifp
= zebra_interface_state_read(s
, vrf_id
);
265 if (BGP_DEBUG(zebra
, ZEBRA
))
266 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id
, ifp
->name
);
268 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
269 bgp_connected_add(bgp
, c
);
271 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
272 bgp_nbr_connected_add(bgp
, nc
);
277 static int bgp_interface_down(int command
, struct zclient
*zclient
,
278 zebra_size_t length
, vrf_id_t vrf_id
)
281 struct interface
*ifp
;
283 struct nbr_connected
*nc
;
284 struct listnode
*node
, *nnode
;
287 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
292 ifp
= zebra_interface_state_read(s
, vrf_id
);
296 if (BGP_DEBUG(zebra
, ZEBRA
))
297 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id
, ifp
->name
);
299 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
300 bgp_connected_delete(bgp
, c
);
302 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
303 bgp_nbr_connected_delete(bgp
, nc
, 1);
305 /* Fast external-failover */
309 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
312 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
313 #if defined(HAVE_CUMULUS)
314 /* Take down directly connected EBGP peers as well as
316 * tracked (directly connected) IBGP peers.
318 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1)
320 || bgp_bfd_is_peer_multihop(peer
)))
322 /* Take down directly connected EBGP peers */
323 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
327 if (ifp
== peer
->nexthop
.ifp
) {
328 BGP_EVENT_ADD(peer
, BGP_Stop
);
329 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
337 static int bgp_interface_address_add(int command
, struct zclient
*zclient
,
338 zebra_size_t length
, vrf_id_t vrf_id
)
340 struct connected
*ifc
;
343 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
347 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
352 if (bgp_debug_zebra(ifc
->address
)) {
353 char buf
[PREFIX2STR_BUFFER
];
354 prefix2str(ifc
->address
, buf
, sizeof(buf
));
355 zlog_debug("Rx Intf address add VRF %u IF %s addr %s", vrf_id
,
356 ifc
->ifp
->name
, buf
);
359 if (if_is_operative(ifc
->ifp
)) {
360 bgp_connected_add(bgp
, ifc
);
362 /* If we have learnt of any neighbors on this interface,
363 * check to kick off any BGP interface-based neighbors,
364 * but only if this is a link-local address.
366 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
)
367 && !list_isempty(ifc
->ifp
->nbr_connected
))
368 bgp_start_interface_nbrs(bgp
, ifc
->ifp
);
374 static int bgp_interface_address_delete(int command
, struct zclient
*zclient
,
375 zebra_size_t length
, vrf_id_t vrf_id
)
377 struct connected
*ifc
;
380 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
384 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
389 if (bgp_debug_zebra(ifc
->address
)) {
390 char buf
[PREFIX2STR_BUFFER
];
391 prefix2str(ifc
->address
, buf
, sizeof(buf
));
392 zlog_debug("Rx Intf address del VRF %u IF %s addr %s", vrf_id
,
393 ifc
->ifp
->name
, buf
);
396 if (if_is_operative(ifc
->ifp
)) {
397 bgp_connected_delete(bgp
, ifc
);
405 static int bgp_interface_nbr_address_add(int command
, struct zclient
*zclient
,
406 zebra_size_t length
, vrf_id_t vrf_id
)
408 struct nbr_connected
*ifc
= NULL
;
411 ifc
= zebra_interface_nbr_address_read(command
, zclient
->ibuf
, vrf_id
);
416 if (bgp_debug_zebra(ifc
->address
)) {
417 char buf
[PREFIX2STR_BUFFER
];
418 prefix2str(ifc
->address
, buf
, sizeof(buf
));
419 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s", vrf_id
,
420 ifc
->ifp
->name
, buf
);
423 if (if_is_operative(ifc
->ifp
)) {
424 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
426 bgp_nbr_connected_add(bgp
, ifc
);
432 static int bgp_interface_nbr_address_delete(int command
,
433 struct zclient
*zclient
,
437 struct nbr_connected
*ifc
= NULL
;
440 ifc
= zebra_interface_nbr_address_read(command
, zclient
->ibuf
, vrf_id
);
445 if (bgp_debug_zebra(ifc
->address
)) {
446 char buf
[PREFIX2STR_BUFFER
];
447 prefix2str(ifc
->address
, buf
, sizeof(buf
));
448 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s", vrf_id
,
449 ifc
->ifp
->name
, buf
);
452 if (if_is_operative(ifc
->ifp
)) {
453 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
455 bgp_nbr_connected_delete(bgp
, ifc
, 0);
458 nbr_connected_free(ifc
);
463 /* VRF update for an interface. */
464 static int bgp_interface_vrf_update(int command
, struct zclient
*zclient
,
465 zebra_size_t length
, vrf_id_t vrf_id
)
467 struct interface
*ifp
;
470 struct nbr_connected
*nc
;
471 struct listnode
*node
, *nnode
;
474 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
479 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
480 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id
,
481 ifp
->name
, new_vrf_id
);
483 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
487 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
488 bgp_connected_delete(bgp
, c
);
490 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
491 bgp_nbr_connected_delete(bgp
, nc
, 1);
493 /* Fast external-failover */
497 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
500 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
501 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
504 if (ifp
== peer
->nexthop
.ifp
)
505 BGP_EVENT_ADD(peer
, BGP_Stop
);
509 if_update_to_new_vrf(ifp
, new_vrf_id
);
511 bgp
= bgp_lookup_by_vrf_id(new_vrf_id
);
515 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
516 bgp_connected_add(bgp
, c
);
518 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
519 bgp_nbr_connected_add(bgp
, nc
);
523 /* Zebra route add and delete treatment. */
524 static int zebra_read_route(int command
, struct zclient
*zclient
,
525 zebra_size_t length
, vrf_id_t vrf_id
)
527 struct zapi_route api
;
528 union g_addr nexthop
;
529 unsigned int ifindex
;
533 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
537 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
540 /* we completely ignore srcdest routes for now. */
541 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
544 /* ignore link-local address. */
545 if (api
.prefix
.family
== AF_INET6
546 && IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
549 nexthop
= api
.nexthops
[0].gate
;
550 ifindex
= api
.nexthops
[0].ifindex
;
552 add
= (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
555 * The ADD message is actually an UPDATE and there is no
557 * for a prior redistributed route, if any. So, perform an
559 * DEL processing for the same redistributed route from any
563 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
565 bgp_redistribute_delete(bgp
, &api
.prefix
, i
,
569 /* Now perform the add/update. */
570 bgp_redistribute_add(bgp
, &api
.prefix
, &nexthop
, ifindex
,
571 api
.metric
, api
.type
, api
.instance
,
574 bgp_redistribute_delete(bgp
, &api
.prefix
, api
.type
,
578 if (bgp_debug_zebra(&api
.prefix
)) {
579 char buf
[2][PREFIX_STRLEN
];
581 prefix2str(&api
.prefix
, buf
[0], sizeof(buf
[0]));
582 inet_ntop(api
.prefix
.family
, &nexthop
, buf
[1], sizeof(buf
[1]));
584 "Rx route %s VRF %u %s[%d] %s "
585 "nexthop %s metric %u tag %" ROUTE_TAG_PRI
,
586 (add
) ? "add" : "delete", vrf_id
,
587 zebra_route_string(api
.type
), api
.instance
, buf
[0],
588 buf
[1], api
.metric
, api
.tag
);
594 struct interface
*if_lookup_by_ipv4(struct in_addr
*addr
, vrf_id_t vrf_id
)
597 struct listnode
*cnode
;
598 struct interface
*ifp
;
599 struct connected
*connected
;
600 struct prefix_ipv4 p
;
603 vrf
= vrf_lookup_by_id(vrf_id
);
609 p
.prefixlen
= IPV4_MAX_BITLEN
;
611 FOR_ALL_INTERFACES (vrf
, ifp
) {
612 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
613 cp
= connected
->address
;
615 if (cp
->family
== AF_INET
)
616 if (prefix_match(cp
, (struct prefix
*)&p
))
623 struct interface
*if_lookup_by_ipv4_exact(struct in_addr
*addr
, vrf_id_t vrf_id
)
626 struct listnode
*cnode
;
627 struct interface
*ifp
;
628 struct connected
*connected
;
631 vrf
= vrf_lookup_by_id(vrf_id
);
635 FOR_ALL_INTERFACES (vrf
, ifp
) {
636 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
637 cp
= connected
->address
;
639 if (cp
->family
== AF_INET
)
640 if (IPV4_ADDR_SAME(&cp
->u
.prefix4
, addr
))
647 struct interface
*if_lookup_by_ipv6(struct in6_addr
*addr
, ifindex_t ifindex
,
651 struct listnode
*cnode
;
652 struct interface
*ifp
;
653 struct connected
*connected
;
654 struct prefix_ipv6 p
;
657 vrf
= vrf_lookup_by_id(vrf_id
);
663 p
.prefixlen
= IPV6_MAX_BITLEN
;
665 FOR_ALL_INTERFACES (vrf
, ifp
) {
666 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
667 cp
= connected
->address
;
669 if (cp
->family
== AF_INET6
)
670 if (prefix_match(cp
, (struct prefix
*)&p
)) {
671 if (IN6_IS_ADDR_LINKLOCAL(
673 if (ifindex
== ifp
->ifindex
)
683 struct interface
*if_lookup_by_ipv6_exact(struct in6_addr
*addr
,
684 ifindex_t ifindex
, vrf_id_t vrf_id
)
687 struct listnode
*cnode
;
688 struct interface
*ifp
;
689 struct connected
*connected
;
692 vrf
= vrf_lookup_by_id(vrf_id
);
696 FOR_ALL_INTERFACES (vrf
, ifp
) {
697 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
698 cp
= connected
->address
;
700 if (cp
->family
== AF_INET6
)
701 if (IPV6_ADDR_SAME(&cp
->u
.prefix6
, addr
)) {
702 if (IN6_IS_ADDR_LINKLOCAL(
704 if (ifindex
== ifp
->ifindex
)
714 static int if_get_ipv6_global(struct interface
*ifp
, struct in6_addr
*addr
)
716 struct listnode
*cnode
;
717 struct connected
*connected
;
720 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
721 cp
= connected
->address
;
723 if (cp
->family
== AF_INET6
)
724 if (!IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
725 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
732 static int if_get_ipv6_local(struct interface
*ifp
, struct in6_addr
*addr
)
734 struct listnode
*cnode
;
735 struct connected
*connected
;
738 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
739 cp
= connected
->address
;
741 if (cp
->family
== AF_INET6
)
742 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
743 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
750 static int if_get_ipv4_address(struct interface
*ifp
, struct in_addr
*addr
)
752 struct listnode
*cnode
;
753 struct connected
*connected
;
756 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
757 cp
= connected
->address
;
758 if ((cp
->family
== AF_INET
)
759 && !ipv4_martian(&(cp
->u
.prefix4
))) {
760 *addr
= cp
->u
.prefix4
;
767 int bgp_nexthop_set(union sockunion
*local
, union sockunion
*remote
,
768 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
771 struct interface
*ifp
= NULL
;
773 memset(nexthop
, 0, sizeof(struct bgp_nexthop
));
780 if (local
->sa
.sa_family
== AF_INET
) {
781 nexthop
->v4
= local
->sin
.sin_addr
;
783 ifp
= if_lookup_by_name(peer
->update_if
,
786 ifp
= if_lookup_by_ipv4_exact(&local
->sin
.sin_addr
,
789 if (local
->sa
.sa_family
== AF_INET6
) {
790 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
791 if (peer
->conf_if
|| peer
->ifname
)
792 ifp
= if_lookup_by_name(peer
->conf_if
796 } else if (peer
->update_if
)
797 ifp
= if_lookup_by_name(peer
->update_if
,
800 ifp
= if_lookup_by_ipv6_exact(&local
->sin6
.sin6_addr
,
801 local
->sin6
.sin6_scope_id
,
810 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
811 if (local
->sa
.sa_family
== AF_INET
) {
813 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
816 /* There is no global nexthop. Use link-local address as
818 * global and link-local nexthop. In this scenario, the
820 * for interop is that the network admin would use a
822 * specify the global IPv6 nexthop.
824 if_get_ipv6_local(ifp
, &nexthop
->v6_global
);
825 memcpy(&nexthop
->v6_local
, &nexthop
->v6_global
,
828 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
830 if (if_lookup_by_ipv4(&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
831 peer
->shared_network
= 1;
833 peer
->shared_network
= 0;
836 /* IPv6 connection, fetch and store IPv4 local address if any. */
837 if (local
->sa
.sa_family
== AF_INET6
) {
838 struct interface
*direct
= NULL
;
841 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
842 if (!ret
&& peer
->local_id
.s_addr
)
843 nexthop
->v4
= peer
->local_id
;
846 if (!IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
847 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
850 /* If directory connected set link-local address. */
851 direct
= if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
852 remote
->sin6
.sin6_scope_id
,
855 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
857 /* Link-local address. */
859 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
861 /* If there is no global address. Set link-local
863 global. I know this break RFC specification... */
864 /* In this scenario, the expectation for interop is that
866 * network admin would use a route-map to specify the
871 memcpy(&nexthop
->v6_global
,
872 &local
->sin6
.sin6_addr
,
874 /* Always set the link-local address */
875 memcpy(&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
879 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)
880 || if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
881 remote
->sin6
.sin6_scope_id
,
883 peer
->shared_network
= 1;
885 peer
->shared_network
= 0;
888 /* KAME stack specific treatment. */
890 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_global
)
891 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
)) {
892 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
, 0);
894 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_local
)
895 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
)) {
896 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
, 0);
900 /* If we have identified the local interface, there is no error for now.
905 static struct in6_addr
*bgp_info_to_ipv6_nexthop(struct bgp_info
*info
)
907 struct in6_addr
*nexthop
= NULL
;
909 /* Only global address nexthop exists. */
910 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
)
911 nexthop
= &info
->attr
->mp_nexthop_global
;
913 /* If both global and link-local address present. */
914 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
915 /* Check if route-map is set to prefer global over link-local */
916 if (info
->attr
->mp_nexthop_prefer_global
)
917 nexthop
= &info
->attr
->mp_nexthop_global
;
919 /* Workaround for Cisco's nexthop bug. */
920 if (IN6_IS_ADDR_UNSPECIFIED(
921 &info
->attr
->mp_nexthop_global
)
922 && info
->peer
->su_remote
->sa
.sa_family
== AF_INET6
)
924 &info
->peer
->su_remote
->sin6
.sin6_addr
;
926 nexthop
= &info
->attr
->mp_nexthop_local
;
933 static int bgp_table_map_apply(struct route_map
*map
, struct prefix
*p
,
934 struct bgp_info
*info
)
936 route_map_result_t ret
;
938 ret
= route_map_apply(map
, p
, RMAP_BGP
, info
);
939 bgp_attr_flush(info
->attr
);
941 if (ret
!= RMAP_DENYMATCH
)
944 if (bgp_debug_zebra(p
)) {
945 if (p
->family
== AF_INET
) {
946 char buf
[2][INET_ADDRSTRLEN
];
948 "Zebra rmap deny: IPv4 route %s/%d nexthop %s",
949 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0],
952 inet_ntop(AF_INET
, &info
->attr
->nexthop
, buf
[1],
955 if (p
->family
== AF_INET6
) {
956 char buf
[2][INET6_ADDRSTRLEN
];
958 "Zebra rmap deny: IPv6 route %s/%d nexthop %s",
959 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0],
963 bgp_info_to_ipv6_nexthop(info
),
964 buf
[1], sizeof(buf
[1])));
970 void bgp_zebra_announce(struct bgp_node
*rn
, struct prefix
*p
,
971 struct bgp_info
*info
, struct bgp
*bgp
, afi_t afi
,
974 struct zapi_route api
;
975 struct zapi_nexthop
*api_nh
;
977 unsigned int valid_nh_count
= 0;
978 int has_valid_label
= 0;
981 struct bgp_info
*mpinfo
;
983 struct attr local_attr
;
984 struct bgp_info local_info
;
985 struct bgp_info
*mpinfo_cp
= &local_info
;
989 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
990 * know of this instance.
992 if (!bgp_install_info_to_zebra(bgp
))
995 if (bgp
->main_zebra_update_hold
)
998 /* Make Zebra API structure. */
999 memset(&api
, 0, sizeof(api
));
1000 api
.vrf_id
= bgp
->vrf_id
;
1001 api
.type
= ZEBRA_ROUTE_BGP
;
1004 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1008 tag
= info
->attr
->tag
;
1010 /* When we create an aggregate route we must also install a Null0 route
1013 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1014 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
1016 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
1017 || info
->sub_type
== BGP_ROUTE_AGGREGATE
) {
1018 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1019 SET_FLAG(api
.flags
, ZEBRA_FLAG_INTERNAL
);
1022 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1023 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1024 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1026 SET_FLAG(api
.flags
, ZEBRA_FLAG_INTERNAL
);
1028 /* Metric is currently based on the best-path only */
1029 metric
= info
->attr
->med
;
1030 for (mpinfo
= info
; mpinfo
; mpinfo
= bgp_info_mpath_next(mpinfo
)) {
1031 if (valid_nh_count
>= multipath_num
)
1034 *mpinfo_cp
= *mpinfo
;
1036 /* Get nexthop address-family */
1037 if (p
->family
== AF_INET
1038 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
))
1039 nh_family
= AF_INET
;
1040 else if (p
->family
== AF_INET6
1041 || (p
->family
== AF_INET
1042 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
)))
1043 nh_family
= AF_INET6
;
1047 if (nh_family
== AF_INET
) {
1048 struct in_addr
*nexthop
;
1050 if (bgp
->table_map
[afi
][safi
].name
) {
1051 /* Copy info and attributes, so the route-map
1052 apply doesn't modify the BGP route info. */
1053 local_attr
= *mpinfo
->attr
;
1054 mpinfo_cp
->attr
= &local_attr
;
1056 if (!bgp_table_map_apply(
1057 bgp
->table_map
[afi
][safi
].map
, p
,
1061 /* metric/tag is only allowed to be
1062 * overridden on 1st nexthop */
1063 if (mpinfo
== info
) {
1064 metric
= mpinfo_cp
->attr
->med
;
1065 tag
= mpinfo_cp
->attr
->tag
;
1069 nexthop
= &mpinfo_cp
->attr
->nexthop
;
1071 api_nh
= &api
.nexthops
[valid_nh_count
];
1072 api_nh
->gate
.ipv4
= *nexthop
;
1073 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1076 struct in6_addr
*nexthop
;
1080 if (bgp
->table_map
[afi
][safi
].name
) {
1081 /* Copy info and attributes, so the route-map
1082 apply doesn't modify the BGP route info. */
1083 local_attr
= *mpinfo
->attr
;
1084 mpinfo_cp
->attr
= &local_attr
;
1086 if (!bgp_table_map_apply(
1087 bgp
->table_map
[afi
][safi
].map
, p
,
1091 /* metric/tag is only allowed to be
1092 * overridden on 1st nexthop */
1093 if (mpinfo
== info
) {
1094 metric
= mpinfo_cp
->attr
->med
;
1095 tag
= mpinfo_cp
->attr
->tag
;
1098 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo_cp
);
1100 if ((mpinfo
== info
)
1101 && mpinfo
->attr
->mp_nexthop_len
1102 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1103 if (mpinfo
->peer
->nexthop
.ifp
)
1104 ifindex
= mpinfo
->peer
->nexthop
.ifp
1108 if (mpinfo
->peer
->conf_if
1109 || mpinfo
->peer
->ifname
)
1110 ifindex
= ifname2ifindex(
1111 mpinfo
->peer
->conf_if
1112 ? mpinfo
->peer
->conf_if
1113 : mpinfo
->peer
->ifname
,
1115 else if (mpinfo
->peer
->nexthop
.ifp
)
1116 ifindex
= mpinfo
->peer
->nexthop
.ifp
1122 api_nh
= &api
.nexthops
[valid_nh_count
];
1123 api_nh
->gate
.ipv6
= *nexthop
;
1124 api_nh
->ifindex
= ifindex
;
1125 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1129 && bgp_is_valid_label(&mpinfo
->extra
->label
)) {
1130 has_valid_label
= 1;
1131 label
= label_pton(&mpinfo
->extra
->label
);
1133 api_nh
->label_num
= 1;
1134 api_nh
->labels
[0] = label
;
1139 if (has_valid_label
)
1140 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
1142 if (info
->sub_type
!= BGP_ROUTE_AGGREGATE
)
1143 api
.nexthop_num
= valid_nh_count
;
1145 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1146 api
.metric
= metric
;
1149 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1153 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1155 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1156 api
.distance
= distance
;
1159 if (bgp_debug_zebra(p
)) {
1160 char prefix_buf
[PREFIX_STRLEN
];
1161 char nh_buf
[INET6_ADDRSTRLEN
];
1165 prefix2str(&api
.prefix
, prefix_buf
, sizeof(prefix_buf
));
1166 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1168 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1169 prefix_buf
, api
.metric
, api
.tag
, api
.nexthop_num
);
1170 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1171 api_nh
= &api
.nexthops
[i
];
1173 if (api_nh
->type
== NEXTHOP_TYPE_IPV4
)
1174 nh_family
= AF_INET
;
1176 nh_family
= AF_INET6
;
1177 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1180 label_buf
[0] = '\0';
1181 if (has_valid_label
)
1182 sprintf(label_buf
, "label %u",
1184 zlog_debug(" nhop [%d]: %s %s", i
+ 1, nh_buf
,
1189 zclient_route_send(valid_nh_count
? ZEBRA_ROUTE_ADD
1190 : ZEBRA_ROUTE_DELETE
,
1194 /* Announce all routes of a table to zebra */
1195 void bgp_zebra_announce_table(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1197 struct bgp_node
*rn
;
1198 struct bgp_table
*table
;
1199 struct bgp_info
*ri
;
1201 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1202 * know of this instance.
1204 if (!bgp_install_info_to_zebra(bgp
))
1207 table
= bgp
->rib
[afi
][safi
];
1211 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
1212 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1213 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
1214 && ri
->type
== ZEBRA_ROUTE_BGP
1215 && ri
->sub_type
== BGP_ROUTE_NORMAL
)
1216 bgp_zebra_announce(rn
, &rn
->p
, ri
, bgp
, afi
,
1220 void bgp_zebra_withdraw(struct prefix
*p
, struct bgp_info
*info
, safi_t safi
)
1222 struct zapi_route api
;
1228 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1229 * know of this instance.
1231 if (!bgp_install_info_to_zebra(peer
->bgp
))
1234 memset(&api
, 0, sizeof(api
));
1235 api
.vrf_id
= peer
->bgp
->vrf_id
;
1236 api
.type
= ZEBRA_ROUTE_BGP
;
1240 if (peer
->sort
== BGP_PEER_IBGP
) {
1241 SET_FLAG(api
.flags
, ZEBRA_FLAG_INTERNAL
);
1242 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1245 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1246 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1247 || bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1248 SET_FLAG(api
.flags
, ZEBRA_FLAG_INTERNAL
);
1250 if (bgp_debug_zebra(p
)) {
1251 char buf
[PREFIX_STRLEN
];
1253 prefix2str(&api
.prefix
, buf
, sizeof(buf
));
1254 zlog_debug("Tx route delete VRF %u %s", peer
->bgp
->vrf_id
, buf
);
1257 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
1260 struct bgp_redist
*bgp_redist_lookup(struct bgp
*bgp
, afi_t afi
, u_char type
,
1263 struct list
*red_list
;
1264 struct listnode
*node
;
1265 struct bgp_redist
*red
;
1267 red_list
= bgp
->redist
[afi
][type
];
1271 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1272 if (red
->instance
== instance
)
1278 struct bgp_redist
*bgp_redist_add(struct bgp
*bgp
, afi_t afi
, u_char type
,
1281 struct list
*red_list
;
1282 struct bgp_redist
*red
;
1284 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1288 if (!bgp
->redist
[afi
][type
])
1289 bgp
->redist
[afi
][type
] = list_new();
1291 red_list
= bgp
->redist
[afi
][type
];
1292 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
,
1293 sizeof(struct bgp_redist
));
1294 red
->instance
= instance
;
1296 listnode_add(red_list
, red
);
1301 static void bgp_redist_del(struct bgp
*bgp
, afi_t afi
, u_char type
,
1304 struct bgp_redist
*red
;
1306 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1309 listnode_delete(bgp
->redist
[afi
][type
], red
);
1310 XFREE(MTYPE_BGP_REDIST
, red
);
1311 if (!bgp
->redist
[afi
][type
]->count
)
1312 list_delete_and_null(&bgp
->redist
[afi
][type
]);
1316 /* Other routes redistribution into BGP. */
1317 int bgp_redistribute_set(struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1320 /* Return if already redistribute flag is set. */
1322 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1326 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1328 if (vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1332 if (bgp
->vrf_id
== VRF_DEFAULT
1333 && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1334 vnc_export_bgp_enable(
1335 bgp
, afi
); /* only enables if mode bits cfg'd */
1339 vrf_bitmap_set(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1342 /* Don't try to register if we're not connected to Zebra or Zebra
1344 * know of this instance.
1346 if (!bgp_install_info_to_zebra(bgp
))
1347 return CMD_WARNING_CONFIG_FAILED
;
1349 if (BGP_DEBUG(zebra
, ZEBRA
))
1350 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1351 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1354 /* Send distribute add message to zebra. */
1355 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1356 instance
, bgp
->vrf_id
);
1361 int bgp_redistribute_resend(struct bgp
*bgp
, afi_t afi
, int type
,
1364 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1365 * know of this instance.
1367 if (!bgp_install_info_to_zebra(bgp
))
1370 if (BGP_DEBUG(zebra
, ZEBRA
))
1371 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1372 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1375 /* Send distribute add message to zebra. */
1376 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1377 instance
, bgp
->vrf_id
);
1378 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1379 instance
, bgp
->vrf_id
);
1384 /* Redistribute with route-map specification. */
1385 int bgp_redistribute_rmap_set(struct bgp_redist
*red
, const char *name
)
1387 if (red
->rmap
.name
&& (strcmp(red
->rmap
.name
, name
) == 0))
1391 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1392 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1393 red
->rmap
.map
= route_map_lookup_by_name(name
);
1398 /* Redistribute with metric specification. */
1399 int bgp_redistribute_metric_set(struct bgp
*bgp
, struct bgp_redist
*red
,
1400 afi_t afi
, int type
, u_int32_t metric
)
1402 struct bgp_node
*rn
;
1403 struct bgp_info
*ri
;
1405 if (red
->redist_metric_flag
&& red
->redist_metric
== metric
)
1408 red
->redist_metric_flag
= 1;
1409 red
->redist_metric
= metric
;
1411 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
1412 rn
= bgp_route_next(rn
)) {
1413 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
1414 if (ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
1416 && ri
->instance
== red
->instance
) {
1417 struct attr
*old_attr
;
1418 struct attr new_attr
;
1420 bgp_attr_dup(&new_attr
, ri
->attr
);
1421 new_attr
.med
= red
->redist_metric
;
1422 old_attr
= ri
->attr
;
1423 ri
->attr
= bgp_attr_intern(&new_attr
);
1424 bgp_attr_unintern(&old_attr
);
1426 bgp_info_set_flag(rn
, ri
,
1427 BGP_INFO_ATTR_CHANGED
);
1428 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1436 /* Unset redistribution. */
1437 int bgp_redistribute_unreg(struct bgp
*bgp
, afi_t afi
, int type
,
1440 struct bgp_redist
*red
;
1442 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1446 /* Return if zebra connection is disabled. */
1448 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1451 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1453 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1455 vrf_bitmap_unset(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1459 if (bgp
->vrf_id
== VRF_DEFAULT
&& type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1460 vnc_export_bgp_disable(bgp
, afi
);
1464 if (bgp_install_info_to_zebra(bgp
)) {
1465 /* Send distribute delete message to zebra. */
1466 if (BGP_DEBUG(zebra
, ZEBRA
))
1467 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1468 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1470 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
1471 type
, instance
, bgp
->vrf_id
);
1474 /* Withdraw redistributed routes from current BGP's routing table. */
1475 bgp_redistribute_withdraw(bgp
, afi
, type
, instance
);
1480 /* Unset redistribution. */
1481 int bgp_redistribute_unset(struct bgp
*bgp
, afi_t afi
, int type
,
1484 struct bgp_redist
*red
;
1486 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1490 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1492 /* Unset route-map. */
1494 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1495 red
->rmap
.name
= NULL
;
1496 red
->rmap
.map
= NULL
;
1499 red
->redist_metric_flag
= 0;
1500 red
->redist_metric
= 0;
1502 bgp_redist_del(bgp
, afi
, type
, instance
);
1507 /* Update redistribute vrf bitmap during triggers like
1508 restart networking or delete/add VRFs */
1509 void bgp_update_redist_vrf_bitmaps(struct bgp
*bgp
, vrf_id_t old_vrf_id
)
1514 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1515 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1516 if (vrf_bitmap_check(zclient
->redist
[afi
][i
],
1518 vrf_bitmap_unset(zclient
->redist
[afi
][i
],
1520 vrf_bitmap_set(zclient
->redist
[afi
][i
],
1526 void bgp_zclient_reset(void)
1528 zclient_reset(zclient
);
1531 /* Register this instance with Zebra. Invoked upon connect (for
1532 * default instance) and when other VRFs are learnt (or created and
1535 void bgp_zebra_instance_register(struct bgp
*bgp
)
1537 /* Don't try to register if we're not connected to Zebra */
1538 if (!zclient
|| zclient
->sock
< 0)
1541 if (BGP_DEBUG(zebra
, ZEBRA
))
1542 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
1544 /* Register for router-id, interfaces, redistributed routes. */
1545 zclient_send_reg_requests(zclient
, bgp
->vrf_id
);
1547 /* For default instance, register to learn about VNIs, if appropriate.
1549 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
1550 && bgp
->advertise_all_vni
)
1551 bgp_zebra_advertise_all_vni(bgp
, 1);
1554 /* Deregister this instance with Zebra. Invoked upon the instance
1555 * being deleted (default or VRF) and it is already registered.
1557 void bgp_zebra_instance_deregister(struct bgp
*bgp
)
1559 /* Don't try to deregister if we're not connected to Zebra */
1560 if (zclient
->sock
< 0)
1563 if (BGP_DEBUG(zebra
, ZEBRA
))
1564 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
1566 /* For default instance, unregister learning about VNIs, if appropriate.
1568 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
1569 && bgp
->advertise_all_vni
)
1570 bgp_zebra_advertise_all_vni(bgp
, 0);
1572 /* Deregister for router-id, interfaces, redistributed routes. */
1573 zclient_send_dereg_requests(zclient
, bgp
->vrf_id
);
1576 void bgp_zebra_initiate_radv(struct bgp
*bgp
, struct peer
*peer
)
1578 int ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
1580 /* Don't try to initiate if we're not connected to Zebra */
1581 if (zclient
->sock
< 0)
1584 if (BGP_DEBUG(zebra
, ZEBRA
))
1585 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
,
1588 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 1,
1592 void bgp_zebra_terminate_radv(struct bgp
*bgp
, struct peer
*peer
)
1594 /* Don't try to terminate if we're not connected to Zebra */
1595 if (zclient
->sock
< 0)
1598 if (BGP_DEBUG(zebra
, ZEBRA
))
1599 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
,
1602 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 0, 0);
1605 int bgp_zebra_advertise_gw_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
1607 struct stream
*s
= NULL
;
1610 if (!zclient
|| zclient
->sock
< 0)
1613 /* Don't try to register if Zebra doesn't know of this instance. */
1614 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1620 zclient_create_header(s
, ZEBRA_ADVERTISE_DEFAULT_GW
, bgp
->vrf_id
);
1621 stream_putc(s
, advertise
);
1622 stream_put3(s
, vni
);
1623 stream_putw_at(s
, 0, stream_get_endp(s
));
1625 return zclient_send_message(zclient
);
1628 int bgp_zebra_advertise_all_vni(struct bgp
*bgp
, int advertise
)
1633 if (!zclient
|| zclient
->sock
< 0)
1636 /* Don't try to register if Zebra doesn't know of this instance. */
1637 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1643 zclient_create_header(s
, ZEBRA_ADVERTISE_ALL_VNI
, bgp
->vrf_id
);
1644 stream_putc(s
, advertise
);
1645 stream_putw_at(s
, 0, stream_get_endp(s
));
1647 return zclient_send_message(zclient
);
1650 /* BGP has established connection with Zebra. */
1651 static void bgp_zebra_connected(struct zclient
*zclient
)
1655 zclient_num_connects
++; /* increment even if not responding */
1657 /* At this point, we may or may not have BGP instances configured, but
1658 * we're only interested in the default VRF (others wouldn't have learnt
1659 * the VRF from Zebra yet.)
1661 bgp
= bgp_get_default();
1665 bgp_zebra_instance_register(bgp
);
1667 /* Send the client registration */
1668 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
1670 /* TODO - What if we have peers and networks configured, do we have to
1675 static int bgp_zebra_process_local_vni(int command
, struct zclient
*zclient
,
1676 zebra_size_t length
, vrf_id_t vrf_id
)
1681 struct in_addr vtep_ip
;
1684 vni
= stream_getl(s
);
1685 if (command
== ZEBRA_VNI_ADD
)
1686 vtep_ip
.s_addr
= stream_get_ipv4(s
);
1687 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1691 if (BGP_DEBUG(zebra
, ZEBRA
))
1692 zlog_debug("Rx VNI %s VRF %u VNI %u",
1693 (command
== ZEBRA_VNI_ADD
) ? "add" : "del", vrf_id
,
1696 if (command
== ZEBRA_VNI_ADD
)
1697 return bgp_evpn_local_vni_add(
1698 bgp
, vni
, vtep_ip
.s_addr
? vtep_ip
: bgp
->router_id
);
1700 return bgp_evpn_local_vni_del(bgp
, vni
);
1703 static int bgp_zebra_process_local_macip(int command
, struct zclient
*zclient
,
1704 zebra_size_t length
, vrf_id_t vrf_id
)
1712 char buf
[ETHER_ADDR_STRLEN
];
1713 char buf1
[INET6_ADDRSTRLEN
];
1716 memset(&ip
, 0, sizeof(ip
));
1718 vni
= stream_getl(s
);
1719 stream_get(&mac
.octet
, s
, ETH_ALEN
);
1720 ipa_len
= stream_getl(s
);
1721 if (ipa_len
!= 0 && ipa_len
!= IPV4_MAX_BYTELEN
1722 && ipa_len
!= IPV6_MAX_BYTELEN
) {
1723 zlog_err("%u:Recv MACIP %s with invalid IP addr length %d",
1724 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
1731 (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
: IPADDR_V6
;
1732 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
1734 flags
= stream_getc(s
);
1736 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1740 if (BGP_DEBUG(zebra
, ZEBRA
))
1741 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u",
1742 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
1743 flags
, prefix_mac2str(&mac
, buf
, sizeof(buf
)),
1744 ipaddr2str(&ip
, buf1
, sizeof(buf1
)), vni
);
1746 if (command
== ZEBRA_MACIP_ADD
)
1747 return bgp_evpn_local_macip_add(bgp
, vni
, &mac
, &ip
, flags
);
1749 return bgp_evpn_local_macip_del(bgp
, vni
, &mac
, &ip
);
1752 void bgp_zebra_init(struct thread_master
*master
)
1754 zclient_num_connects
= 0;
1756 /* Set default values. */
1757 zclient
= zclient_new(master
);
1758 zclient_init(zclient
, ZEBRA_ROUTE_BGP
, 0);
1759 zclient
->zebra_connected
= bgp_zebra_connected
;
1760 zclient
->router_id_update
= bgp_router_id_update
;
1761 zclient
->interface_add
= bgp_interface_add
;
1762 zclient
->interface_delete
= bgp_interface_delete
;
1763 zclient
->interface_address_add
= bgp_interface_address_add
;
1764 zclient
->interface_address_delete
= bgp_interface_address_delete
;
1765 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
1766 zclient
->interface_nbr_address_delete
=
1767 bgp_interface_nbr_address_delete
;
1768 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
1769 zclient
->redistribute_route_add
= zebra_read_route
;
1770 zclient
->redistribute_route_del
= zebra_read_route
;
1771 zclient
->interface_up
= bgp_interface_up
;
1772 zclient
->interface_down
= bgp_interface_down
;
1773 zclient
->nexthop_update
= bgp_read_nexthop_update
;
1774 zclient
->import_check_update
= bgp_read_import_check_update
;
1775 zclient
->fec_update
= bgp_read_fec_update
;
1776 zclient
->local_vni_add
= bgp_zebra_process_local_vni
;
1777 zclient
->local_vni_del
= bgp_zebra_process_local_vni
;
1778 zclient
->local_macip_add
= bgp_zebra_process_local_macip
;
1779 zclient
->local_macip_del
= bgp_zebra_process_local_macip
;
1782 void bgp_zebra_destroy(void)
1784 if (zclient
== NULL
)
1786 zclient_stop(zclient
);
1787 zclient_free(zclient
);
1791 int bgp_zebra_num_connects(void)
1793 return zclient_num_connects
;