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
;
228 ifp
= zebra_interface_state_read(s
, vrf_id
);
229 if (!ifp
) /* This may happen if we've just unregistered for a VRF. */
232 if (BGP_DEBUG(zebra
, ZEBRA
))
233 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id
, ifp
->name
);
235 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
239 bgp_update_interface_nbrs(bgp
, ifp
, NULL
);
241 ifp
->ifindex
= IFINDEX_DELETED
;
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
;
256 ifp
= zebra_interface_state_read(s
, vrf_id
);
261 if (BGP_DEBUG(zebra
, ZEBRA
))
262 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id
, ifp
->name
);
264 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
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
;
288 ifp
= zebra_interface_state_read(s
, vrf_id
);
292 if (BGP_DEBUG(zebra
, ZEBRA
))
293 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id
, ifp
->name
);
295 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
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
;
342 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
347 if (bgp_debug_zebra(ifc
->address
)) {
348 char buf
[PREFIX2STR_BUFFER
];
349 prefix2str(ifc
->address
, buf
, sizeof(buf
));
350 zlog_debug("Rx Intf address add VRF %u IF %s addr %s", vrf_id
,
351 ifc
->ifp
->name
, buf
);
354 if (if_is_operative(ifc
->ifp
)) {
357 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
361 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 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
385 if (bgp_debug_zebra(ifc
->address
)) {
386 char buf
[PREFIX2STR_BUFFER
];
387 prefix2str(ifc
->address
, buf
, sizeof(buf
));
388 zlog_debug("Rx Intf address del VRF %u IF %s addr %s", vrf_id
,
389 ifc
->ifp
->name
, buf
);
392 if (if_is_operative(ifc
->ifp
)) {
393 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
395 bgp_connected_delete(bgp
, ifc
);
403 static int bgp_interface_nbr_address_add(int command
, struct zclient
*zclient
,
404 zebra_size_t length
, vrf_id_t vrf_id
)
406 struct nbr_connected
*ifc
= NULL
;
409 ifc
= zebra_interface_nbr_address_read(command
, zclient
->ibuf
, vrf_id
);
414 if (bgp_debug_zebra(ifc
->address
)) {
415 char buf
[PREFIX2STR_BUFFER
];
416 prefix2str(ifc
->address
, buf
, sizeof(buf
));
417 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s", vrf_id
,
418 ifc
->ifp
->name
, buf
);
421 if (if_is_operative(ifc
->ifp
)) {
422 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
424 bgp_nbr_connected_add(bgp
, ifc
);
430 static int bgp_interface_nbr_address_delete(int command
,
431 struct zclient
*zclient
,
435 struct nbr_connected
*ifc
= NULL
;
438 ifc
= zebra_interface_nbr_address_read(command
, zclient
->ibuf
, vrf_id
);
443 if (bgp_debug_zebra(ifc
->address
)) {
444 char buf
[PREFIX2STR_BUFFER
];
445 prefix2str(ifc
->address
, buf
, sizeof(buf
));
446 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s", vrf_id
,
447 ifc
->ifp
->name
, buf
);
450 if (if_is_operative(ifc
->ifp
)) {
451 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
453 bgp_nbr_connected_delete(bgp
, ifc
, 0);
456 nbr_connected_free(ifc
);
461 /* VRF update for an interface. */
462 static int bgp_interface_vrf_update(int command
, struct zclient
*zclient
,
463 zebra_size_t length
, vrf_id_t vrf_id
)
465 struct interface
*ifp
;
468 struct nbr_connected
*nc
;
469 struct listnode
*node
, *nnode
;
472 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
477 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
478 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id
,
479 ifp
->name
, new_vrf_id
);
481 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
485 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
486 bgp_connected_delete(bgp
, c
);
488 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
489 bgp_nbr_connected_delete(bgp
, nc
, 1);
491 /* Fast external-failover */
495 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
498 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
499 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
502 if (ifp
== peer
->nexthop
.ifp
)
503 BGP_EVENT_ADD(peer
, BGP_Stop
);
507 if_update_to_new_vrf(ifp
, new_vrf_id
);
509 bgp
= bgp_lookup_by_vrf_id(new_vrf_id
);
513 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
514 bgp_connected_add(bgp
, c
);
516 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
517 bgp_nbr_connected_add(bgp
, nc
);
521 /* Zebra route add and delete treatment. */
522 static int zebra_read_route(int command
, struct zclient
*zclient
,
523 zebra_size_t length
, vrf_id_t vrf_id
)
525 struct zapi_route api
;
526 union g_addr nexthop
;
527 unsigned int ifindex
;
531 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
535 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
538 /* we completely ignore srcdest routes for now. */
539 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
542 /* ignore link-local address. */
543 if (api
.prefix
.family
== AF_INET6
544 && IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
547 nexthop
= api
.nexthops
[0].gate
;
548 ifindex
= api
.nexthops
[0].ifindex
;
550 add
= (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
553 * The ADD message is actually an UPDATE and there is no
555 * for a prior redistributed route, if any. So, perform an
557 * DEL processing for the same redistributed route from any
561 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
563 bgp_redistribute_delete(bgp
, &api
.prefix
, i
,
567 /* Now perform the add/update. */
568 bgp_redistribute_add(bgp
, &api
.prefix
, &nexthop
, ifindex
,
569 api
.metric
, api
.type
, api
.instance
,
572 bgp_redistribute_delete(bgp
, &api
.prefix
, api
.type
,
576 if (bgp_debug_zebra(&api
.prefix
)) {
577 char buf
[2][PREFIX_STRLEN
];
579 prefix2str(&api
.prefix
, buf
[0], sizeof(buf
[0]));
580 inet_ntop(api
.prefix
.family
, &nexthop
, buf
[1], sizeof(buf
[1]));
582 "Rx route %s VRF %u %s[%d] %s "
583 "nexthop %s metric %u tag %" ROUTE_TAG_PRI
,
584 (add
) ? "add" : "delete", vrf_id
,
585 zebra_route_string(api
.type
), api
.instance
, buf
[0],
586 buf
[1], api
.metric
, api
.tag
);
592 struct interface
*if_lookup_by_ipv4(struct in_addr
*addr
, vrf_id_t vrf_id
)
594 struct listnode
*ifnode
;
595 struct listnode
*cnode
;
596 struct interface
*ifp
;
597 struct connected
*connected
;
598 struct prefix_ipv4 p
;
603 p
.prefixlen
= IPV4_MAX_BITLEN
;
605 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), ifnode
, ifp
)) {
606 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
607 cp
= connected
->address
;
609 if (cp
->family
== AF_INET
)
610 if (prefix_match(cp
, (struct prefix
*)&p
))
617 struct interface
*if_lookup_by_ipv4_exact(struct in_addr
*addr
, vrf_id_t vrf_id
)
619 struct listnode
*ifnode
;
620 struct listnode
*cnode
;
621 struct interface
*ifp
;
622 struct connected
*connected
;
625 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), ifnode
, ifp
)) {
626 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
627 cp
= connected
->address
;
629 if (cp
->family
== AF_INET
)
630 if (IPV4_ADDR_SAME(&cp
->u
.prefix4
, addr
))
637 struct interface
*if_lookup_by_ipv6(struct in6_addr
*addr
, ifindex_t ifindex
,
640 struct listnode
*ifnode
;
641 struct listnode
*cnode
;
642 struct interface
*ifp
;
643 struct connected
*connected
;
644 struct prefix_ipv6 p
;
649 p
.prefixlen
= IPV6_MAX_BITLEN
;
651 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), ifnode
, ifp
)) {
652 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
653 cp
= connected
->address
;
655 if (cp
->family
== AF_INET6
)
656 if (prefix_match(cp
, (struct prefix
*)&p
)) {
657 if (IN6_IS_ADDR_LINKLOCAL(
659 if (ifindex
== ifp
->ifindex
)
669 struct interface
*if_lookup_by_ipv6_exact(struct in6_addr
*addr
,
670 ifindex_t ifindex
, vrf_id_t vrf_id
)
672 struct listnode
*ifnode
;
673 struct listnode
*cnode
;
674 struct interface
*ifp
;
675 struct connected
*connected
;
678 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), ifnode
, ifp
)) {
679 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
680 cp
= connected
->address
;
682 if (cp
->family
== AF_INET6
)
683 if (IPV6_ADDR_SAME(&cp
->u
.prefix6
, addr
)) {
684 if (IN6_IS_ADDR_LINKLOCAL(
686 if (ifindex
== ifp
->ifindex
)
696 static int if_get_ipv6_global(struct interface
*ifp
, struct in6_addr
*addr
)
698 struct listnode
*cnode
;
699 struct connected
*connected
;
702 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
703 cp
= connected
->address
;
705 if (cp
->family
== AF_INET6
)
706 if (!IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
707 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
714 static int if_get_ipv6_local(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_ipv4_address(struct interface
*ifp
, struct in_addr
*addr
)
734 struct listnode
*cnode
;
735 struct connected
*connected
;
738 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
739 cp
= connected
->address
;
740 if ((cp
->family
== AF_INET
)
741 && !ipv4_martian(&(cp
->u
.prefix4
))) {
742 *addr
= cp
->u
.prefix4
;
749 int bgp_nexthop_set(union sockunion
*local
, union sockunion
*remote
,
750 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
753 struct interface
*ifp
= NULL
;
755 memset(nexthop
, 0, sizeof(struct bgp_nexthop
));
762 if (local
->sa
.sa_family
== AF_INET
) {
763 nexthop
->v4
= local
->sin
.sin_addr
;
765 ifp
= if_lookup_by_name(peer
->update_if
,
768 ifp
= if_lookup_by_ipv4_exact(&local
->sin
.sin_addr
,
771 if (local
->sa
.sa_family
== AF_INET6
) {
772 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
773 if (peer
->conf_if
|| peer
->ifname
)
774 ifp
= if_lookup_by_name(peer
->conf_if
778 } else if (peer
->update_if
)
779 ifp
= if_lookup_by_name(peer
->update_if
,
782 ifp
= if_lookup_by_ipv6_exact(&local
->sin6
.sin6_addr
,
783 local
->sin6
.sin6_scope_id
,
792 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
793 if (local
->sa
.sa_family
== AF_INET
) {
795 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
798 /* There is no global nexthop. Use link-local address as
800 * global and link-local nexthop. In this scenario, the
802 * for interop is that the network admin would use a
804 * specify the global IPv6 nexthop.
806 if_get_ipv6_local(ifp
, &nexthop
->v6_global
);
807 memcpy(&nexthop
->v6_local
, &nexthop
->v6_global
,
810 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
812 if (if_lookup_by_ipv4(&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
813 peer
->shared_network
= 1;
815 peer
->shared_network
= 0;
818 /* IPv6 connection, fetch and store IPv4 local address if any. */
819 if (local
->sa
.sa_family
== AF_INET6
) {
820 struct interface
*direct
= NULL
;
823 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
824 if (!ret
&& peer
->local_id
.s_addr
)
825 nexthop
->v4
= peer
->local_id
;
828 if (!IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
829 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
832 /* If directory connected set link-local address. */
833 direct
= if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
834 remote
->sin6
.sin6_scope_id
,
837 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
839 /* Link-local address. */
841 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
843 /* If there is no global address. Set link-local
845 global. I know this break RFC specification... */
846 /* In this scenario, the expectation for interop is that
848 * network admin would use a route-map to specify the
853 memcpy(&nexthop
->v6_global
,
854 &local
->sin6
.sin6_addr
,
856 /* Always set the link-local address */
857 memcpy(&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
861 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)
862 || if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
863 remote
->sin6
.sin6_scope_id
,
865 peer
->shared_network
= 1;
867 peer
->shared_network
= 0;
870 /* KAME stack specific treatment. */
872 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_global
)
873 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
)) {
874 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
, 0);
876 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_local
)
877 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
)) {
878 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
, 0);
882 /* If we have identified the local interface, there is no error for now.
887 static struct in6_addr
*bgp_info_to_ipv6_nexthop(struct bgp_info
*info
)
889 struct in6_addr
*nexthop
= NULL
;
891 /* Only global address nexthop exists. */
892 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
)
893 nexthop
= &info
->attr
->mp_nexthop_global
;
895 /* If both global and link-local address present. */
896 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
897 /* Check if route-map is set to prefer global over link-local */
898 if (info
->attr
->mp_nexthop_prefer_global
)
899 nexthop
= &info
->attr
->mp_nexthop_global
;
901 /* Workaround for Cisco's nexthop bug. */
902 if (IN6_IS_ADDR_UNSPECIFIED(
903 &info
->attr
->mp_nexthop_global
)
904 && info
->peer
->su_remote
->sa
.sa_family
== AF_INET6
)
906 &info
->peer
->su_remote
->sin6
.sin6_addr
;
908 nexthop
= &info
->attr
->mp_nexthop_local
;
915 static int bgp_table_map_apply(struct route_map
*map
, struct prefix
*p
,
916 struct bgp_info
*info
)
918 route_map_result_t ret
;
920 ret
= route_map_apply(map
, p
, RMAP_BGP
, info
);
921 bgp_attr_flush(info
->attr
);
923 if (ret
!= RMAP_DENYMATCH
)
926 if (bgp_debug_zebra(p
)) {
927 if (p
->family
== AF_INET
) {
928 char buf
[2][INET_ADDRSTRLEN
];
930 "Zebra rmap deny: IPv4 route %s/%d nexthop %s",
931 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0],
934 inet_ntop(AF_INET
, &info
->attr
->nexthop
, buf
[1],
937 if (p
->family
== AF_INET6
) {
938 char buf
[2][INET6_ADDRSTRLEN
];
940 "Zebra rmap deny: IPv6 route %s/%d nexthop %s",
941 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0],
945 bgp_info_to_ipv6_nexthop(info
),
946 buf
[1], sizeof(buf
[1])));
952 void bgp_zebra_announce(struct bgp_node
*rn
, struct prefix
*p
,
953 struct bgp_info
*info
, struct bgp
*bgp
, afi_t afi
,
956 struct zapi_route api
;
957 struct zapi_nexthop
*api_nh
;
959 unsigned int valid_nh_count
= 0;
960 int has_valid_label
= 0;
963 struct bgp_info
*mpinfo
;
965 struct attr local_attr
;
966 struct bgp_info local_info
;
967 struct bgp_info
*mpinfo_cp
= &local_info
;
971 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
972 * know of this instance.
974 if (!bgp_install_info_to_zebra(bgp
))
977 if (bgp
->main_zebra_update_hold
)
980 /* Make Zebra API structure. */
981 memset(&api
, 0, sizeof(api
));
982 api
.vrf_id
= bgp
->vrf_id
;
983 api
.type
= ZEBRA_ROUTE_BGP
;
986 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
990 tag
= info
->attr
->tag
;
992 /* When we create an aggregate route we must also install a Null0 route
995 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
996 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
998 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
999 || info
->sub_type
== BGP_ROUTE_AGGREGATE
) {
1000 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1001 SET_FLAG(api
.flags
, ZEBRA_FLAG_INTERNAL
);
1004 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1005 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1006 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1008 SET_FLAG(api
.flags
, ZEBRA_FLAG_INTERNAL
);
1010 /* Metric is currently based on the best-path only */
1011 metric
= info
->attr
->med
;
1012 for (mpinfo
= info
; mpinfo
; mpinfo
= bgp_info_mpath_next(mpinfo
)) {
1013 if (valid_nh_count
>= multipath_num
)
1016 *mpinfo_cp
= *mpinfo
;
1018 /* Get nexthop address-family */
1019 if (p
->family
== AF_INET
1020 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
))
1021 nh_family
= AF_INET
;
1022 else if (p
->family
== AF_INET6
1023 || (p
->family
== AF_INET
1024 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
)))
1025 nh_family
= AF_INET6
;
1029 if (nh_family
== AF_INET
) {
1030 struct in_addr
*nexthop
;
1032 if (bgp
->table_map
[afi
][safi
].name
) {
1033 /* Copy info and attributes, so the route-map
1034 apply doesn't modify the BGP route info. */
1035 local_attr
= *mpinfo
->attr
;
1036 mpinfo_cp
->attr
= &local_attr
;
1038 if (!bgp_table_map_apply(
1039 bgp
->table_map
[afi
][safi
].map
, p
,
1043 /* metric/tag is only allowed to be
1044 * overridden on 1st nexthop */
1045 if (mpinfo
== info
) {
1046 metric
= mpinfo_cp
->attr
->med
;
1047 tag
= mpinfo_cp
->attr
->tag
;
1051 nexthop
= &mpinfo_cp
->attr
->nexthop
;
1053 api_nh
= &api
.nexthops
[valid_nh_count
];
1054 api_nh
->gate
.ipv4
= *nexthop
;
1055 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1058 struct in6_addr
*nexthop
;
1062 if (bgp
->table_map
[afi
][safi
].name
) {
1063 /* Copy info and attributes, so the route-map
1064 apply doesn't modify the BGP route info. */
1065 local_attr
= *mpinfo
->attr
;
1066 mpinfo_cp
->attr
= &local_attr
;
1068 if (!bgp_table_map_apply(
1069 bgp
->table_map
[afi
][safi
].map
, p
,
1073 /* metric/tag is only allowed to be
1074 * overridden on 1st nexthop */
1075 if (mpinfo
== info
) {
1076 metric
= mpinfo_cp
->attr
->med
;
1077 tag
= mpinfo_cp
->attr
->tag
;
1080 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo_cp
);
1082 if ((mpinfo
== info
)
1083 && mpinfo
->attr
->mp_nexthop_len
1084 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1085 if (mpinfo
->peer
->nexthop
.ifp
)
1086 ifindex
= mpinfo
->peer
->nexthop
.ifp
1090 if (mpinfo
->peer
->conf_if
1091 || mpinfo
->peer
->ifname
)
1092 ifindex
= ifname2ifindex(
1093 mpinfo
->peer
->conf_if
1094 ? mpinfo
->peer
->conf_if
1095 : mpinfo
->peer
->ifname
,
1097 else if (mpinfo
->peer
->nexthop
.ifp
)
1098 ifindex
= mpinfo
->peer
->nexthop
.ifp
1104 api_nh
= &api
.nexthops
[valid_nh_count
];
1105 api_nh
->gate
.ipv6
= *nexthop
;
1106 api_nh
->ifindex
= ifindex
;
1107 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1111 && bgp_is_valid_label(&mpinfo
->extra
->label
)) {
1112 has_valid_label
= 1;
1113 label
= label_pton(&mpinfo
->extra
->label
);
1115 api_nh
->label_num
= 1;
1116 api_nh
->labels
[0] = label
;
1121 if (has_valid_label
)
1122 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
1124 if (info
->sub_type
!= BGP_ROUTE_AGGREGATE
)
1125 api
.nexthop_num
= valid_nh_count
;
1127 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1128 api
.metric
= metric
;
1131 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1135 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1137 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1138 api
.distance
= distance
;
1141 if (bgp_debug_zebra(p
)) {
1142 char prefix_buf
[PREFIX_STRLEN
];
1143 char nh_buf
[INET6_ADDRSTRLEN
];
1147 prefix2str(&api
.prefix
, prefix_buf
, sizeof(prefix_buf
));
1148 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1150 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1151 prefix_buf
, api
.metric
, api
.tag
, api
.nexthop_num
);
1152 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1153 api_nh
= &api
.nexthops
[i
];
1155 if (api_nh
->type
== NEXTHOP_TYPE_IPV4
)
1156 nh_family
= AF_INET
;
1158 nh_family
= AF_INET6
;
1159 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1162 label_buf
[0] = '\0';
1163 if (has_valid_label
)
1164 sprintf(label_buf
, "label %u",
1166 zlog_debug(" nhop [%d]: %s %s", i
+ 1, nh_buf
,
1171 zclient_route_send(valid_nh_count
? ZEBRA_ROUTE_ADD
1172 : ZEBRA_ROUTE_DELETE
,
1176 /* Announce all routes of a table to zebra */
1177 void bgp_zebra_announce_table(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1179 struct bgp_node
*rn
;
1180 struct bgp_table
*table
;
1181 struct bgp_info
*ri
;
1183 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1184 * know of this instance.
1186 if (!bgp_install_info_to_zebra(bgp
))
1189 table
= bgp
->rib
[afi
][safi
];
1193 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
1194 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1195 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
1196 && ri
->type
== ZEBRA_ROUTE_BGP
1197 && ri
->sub_type
== BGP_ROUTE_NORMAL
)
1198 bgp_zebra_announce(rn
, &rn
->p
, ri
, bgp
, afi
,
1202 void bgp_zebra_withdraw(struct prefix
*p
, struct bgp_info
*info
, safi_t safi
)
1204 struct zapi_route api
;
1210 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1211 * know of this instance.
1213 if (!bgp_install_info_to_zebra(peer
->bgp
))
1216 memset(&api
, 0, sizeof(api
));
1217 api
.vrf_id
= peer
->bgp
->vrf_id
;
1218 api
.type
= ZEBRA_ROUTE_BGP
;
1222 if (peer
->sort
== BGP_PEER_IBGP
) {
1223 SET_FLAG(api
.flags
, ZEBRA_FLAG_INTERNAL
);
1224 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1227 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1228 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1229 || bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1230 SET_FLAG(api
.flags
, ZEBRA_FLAG_INTERNAL
);
1232 if (bgp_debug_zebra(p
)) {
1233 char buf
[PREFIX_STRLEN
];
1235 prefix2str(&api
.prefix
, buf
, sizeof(buf
));
1236 zlog_debug("Tx route delete VRF %u %s", peer
->bgp
->vrf_id
, buf
);
1239 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
1242 struct bgp_redist
*bgp_redist_lookup(struct bgp
*bgp
, afi_t afi
, u_char type
,
1245 struct list
*red_list
;
1246 struct listnode
*node
;
1247 struct bgp_redist
*red
;
1249 red_list
= bgp
->redist
[afi
][type
];
1253 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1254 if (red
->instance
== instance
)
1260 struct bgp_redist
*bgp_redist_add(struct bgp
*bgp
, afi_t afi
, u_char type
,
1263 struct list
*red_list
;
1264 struct bgp_redist
*red
;
1266 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1270 if (!bgp
->redist
[afi
][type
])
1271 bgp
->redist
[afi
][type
] = list_new();
1273 red_list
= bgp
->redist
[afi
][type
];
1274 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
,
1275 sizeof(struct bgp_redist
));
1276 red
->instance
= instance
;
1278 listnode_add(red_list
, red
);
1283 static void bgp_redist_del(struct bgp
*bgp
, afi_t afi
, u_char type
,
1286 struct bgp_redist
*red
;
1288 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1291 listnode_delete(bgp
->redist
[afi
][type
], red
);
1292 XFREE(MTYPE_BGP_REDIST
, red
);
1293 if (!bgp
->redist
[afi
][type
]->count
)
1294 list_delete_and_null(&bgp
->redist
[afi
][type
]);
1298 /* Other routes redistribution into BGP. */
1299 int bgp_redistribute_set(struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1302 /* Return if already redistribute flag is set. */
1304 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1308 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1310 if (vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1314 if (bgp
->vrf_id
== VRF_DEFAULT
1315 && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1316 vnc_export_bgp_enable(
1317 bgp
, afi
); /* only enables if mode bits cfg'd */
1321 vrf_bitmap_set(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1324 /* Don't try to register if we're not connected to Zebra or Zebra
1326 * know of this instance.
1328 if (!bgp_install_info_to_zebra(bgp
))
1329 return CMD_WARNING_CONFIG_FAILED
;
1331 if (BGP_DEBUG(zebra
, ZEBRA
))
1332 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1333 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1336 /* Send distribute add message to zebra. */
1337 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1338 instance
, bgp
->vrf_id
);
1343 int bgp_redistribute_resend(struct bgp
*bgp
, afi_t afi
, int type
,
1346 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1347 * know of this instance.
1349 if (!bgp_install_info_to_zebra(bgp
))
1352 if (BGP_DEBUG(zebra
, ZEBRA
))
1353 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1354 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1357 /* Send distribute add message to zebra. */
1358 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1359 instance
, bgp
->vrf_id
);
1360 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1361 instance
, bgp
->vrf_id
);
1366 /* Redistribute with route-map specification. */
1367 int bgp_redistribute_rmap_set(struct bgp_redist
*red
, const char *name
)
1369 if (red
->rmap
.name
&& (strcmp(red
->rmap
.name
, name
) == 0))
1373 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1374 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1375 red
->rmap
.map
= route_map_lookup_by_name(name
);
1380 /* Redistribute with metric specification. */
1381 int bgp_redistribute_metric_set(struct bgp
*bgp
, struct bgp_redist
*red
,
1382 afi_t afi
, int type
, u_int32_t metric
)
1384 struct bgp_node
*rn
;
1385 struct bgp_info
*ri
;
1387 if (red
->redist_metric_flag
&& red
->redist_metric
== metric
)
1390 red
->redist_metric_flag
= 1;
1391 red
->redist_metric
= metric
;
1393 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
1394 rn
= bgp_route_next(rn
)) {
1395 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
1396 if (ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
1398 && ri
->instance
== red
->instance
) {
1399 struct attr
*old_attr
;
1400 struct attr new_attr
;
1402 bgp_attr_dup(&new_attr
, ri
->attr
);
1403 new_attr
.med
= red
->redist_metric
;
1404 old_attr
= ri
->attr
;
1405 ri
->attr
= bgp_attr_intern(&new_attr
);
1406 bgp_attr_unintern(&old_attr
);
1408 bgp_info_set_flag(rn
, ri
,
1409 BGP_INFO_ATTR_CHANGED
);
1410 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1418 /* Unset redistribution. */
1419 int bgp_redistribute_unreg(struct bgp
*bgp
, afi_t afi
, int type
,
1422 struct bgp_redist
*red
;
1424 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1428 /* Return if zebra connection is disabled. */
1430 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1433 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1435 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1437 vrf_bitmap_unset(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1441 if (bgp
->vrf_id
== VRF_DEFAULT
&& type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1442 vnc_export_bgp_disable(bgp
, afi
);
1446 if (bgp_install_info_to_zebra(bgp
)) {
1447 /* Send distribute delete message to zebra. */
1448 if (BGP_DEBUG(zebra
, ZEBRA
))
1449 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1450 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1452 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
1453 type
, instance
, bgp
->vrf_id
);
1456 /* Withdraw redistributed routes from current BGP's routing table. */
1457 bgp_redistribute_withdraw(bgp
, afi
, type
, instance
);
1462 /* Unset redistribution. */
1463 int bgp_redistribute_unset(struct bgp
*bgp
, afi_t afi
, int type
,
1466 struct bgp_redist
*red
;
1468 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1472 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1474 /* Unset route-map. */
1476 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1477 red
->rmap
.name
= NULL
;
1478 red
->rmap
.map
= NULL
;
1481 red
->redist_metric_flag
= 0;
1482 red
->redist_metric
= 0;
1484 bgp_redist_del(bgp
, afi
, type
, instance
);
1489 /* Update redistribute vrf bitmap during triggers like
1490 restart networking or delete/add VRFs */
1491 void bgp_update_redist_vrf_bitmaps(struct bgp
*bgp
, vrf_id_t old_vrf_id
)
1496 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1497 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1498 if (vrf_bitmap_check(zclient
->redist
[afi
][i
],
1500 vrf_bitmap_unset(zclient
->redist
[afi
][i
],
1502 vrf_bitmap_set(zclient
->redist
[afi
][i
],
1508 void bgp_zclient_reset(void)
1510 zclient_reset(zclient
);
1513 /* Register this instance with Zebra. Invoked upon connect (for
1514 * default instance) and when other VRFs are learnt (or created and
1517 void bgp_zebra_instance_register(struct bgp
*bgp
)
1519 /* Don't try to register if we're not connected to Zebra */
1520 if (!zclient
|| zclient
->sock
< 0)
1523 if (BGP_DEBUG(zebra
, ZEBRA
))
1524 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
1526 /* Register for router-id, interfaces, redistributed routes. */
1527 zclient_send_reg_requests(zclient
, bgp
->vrf_id
);
1529 /* For default instance, register to learn about VNIs, if appropriate.
1531 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
1532 && bgp
->advertise_all_vni
)
1533 bgp_zebra_advertise_all_vni(bgp
, 1);
1536 /* Deregister this instance with Zebra. Invoked upon the instance
1537 * being deleted (default or VRF) and it is already registered.
1539 void bgp_zebra_instance_deregister(struct bgp
*bgp
)
1541 /* Don't try to deregister if we're not connected to Zebra */
1542 if (zclient
->sock
< 0)
1545 if (BGP_DEBUG(zebra
, ZEBRA
))
1546 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
1548 /* For default instance, unregister learning about VNIs, if appropriate.
1550 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
1551 && bgp
->advertise_all_vni
)
1552 bgp_zebra_advertise_all_vni(bgp
, 0);
1554 /* Deregister for router-id, interfaces, redistributed routes. */
1555 zclient_send_dereg_requests(zclient
, bgp
->vrf_id
);
1558 void bgp_zebra_initiate_radv(struct bgp
*bgp
, struct peer
*peer
)
1560 int ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
1562 /* Don't try to initiate if we're not connected to Zebra */
1563 if (zclient
->sock
< 0)
1566 if (BGP_DEBUG(zebra
, ZEBRA
))
1567 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
,
1570 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 1,
1574 void bgp_zebra_terminate_radv(struct bgp
*bgp
, struct peer
*peer
)
1576 /* Don't try to terminate if we're not connected to Zebra */
1577 if (zclient
->sock
< 0)
1580 if (BGP_DEBUG(zebra
, ZEBRA
))
1581 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
,
1584 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 0, 0);
1587 int bgp_zebra_advertise_gw_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
1589 struct stream
*s
= NULL
;
1592 if (!zclient
|| zclient
->sock
< 0)
1595 /* Don't try to register if Zebra doesn't know of this instance. */
1596 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1602 zclient_create_header(s
, ZEBRA_ADVERTISE_DEFAULT_GW
, bgp
->vrf_id
);
1603 stream_putc(s
, advertise
);
1604 stream_put3(s
, vni
);
1605 stream_putw_at(s
, 0, stream_get_endp(s
));
1607 return zclient_send_message(zclient
);
1610 int bgp_zebra_advertise_all_vni(struct bgp
*bgp
, int advertise
)
1615 if (!zclient
|| zclient
->sock
< 0)
1618 /* Don't try to register if Zebra doesn't know of this instance. */
1619 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1625 zclient_create_header(s
, ZEBRA_ADVERTISE_ALL_VNI
, bgp
->vrf_id
);
1626 stream_putc(s
, advertise
);
1627 stream_putw_at(s
, 0, stream_get_endp(s
));
1629 return zclient_send_message(zclient
);
1632 /* BGP has established connection with Zebra. */
1633 static void bgp_zebra_connected(struct zclient
*zclient
)
1637 zclient_num_connects
++; /* increment even if not responding */
1639 /* At this point, we may or may not have BGP instances configured, but
1640 * we're only interested in the default VRF (others wouldn't have learnt
1641 * the VRF from Zebra yet.)
1643 bgp
= bgp_get_default();
1647 bgp_zebra_instance_register(bgp
);
1649 /* Send the client registration */
1650 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
1652 /* TODO - What if we have peers and networks configured, do we have to
1657 static int bgp_zebra_process_local_vni(int command
, struct zclient
*zclient
,
1658 zebra_size_t length
, vrf_id_t vrf_id
)
1663 struct in_addr vtep_ip
;
1666 vni
= stream_getl(s
);
1667 if (command
== ZEBRA_VNI_ADD
)
1668 vtep_ip
.s_addr
= stream_get_ipv4(s
);
1669 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1673 if (BGP_DEBUG(zebra
, ZEBRA
))
1674 zlog_debug("Rx VNI %s VRF %u VNI %u",
1675 (command
== ZEBRA_VNI_ADD
) ? "add" : "del", vrf_id
,
1678 if (command
== ZEBRA_VNI_ADD
)
1679 return bgp_evpn_local_vni_add(
1680 bgp
, vni
, vtep_ip
.s_addr
? vtep_ip
: bgp
->router_id
);
1682 return bgp_evpn_local_vni_del(bgp
, vni
);
1685 static int bgp_zebra_process_local_macip(int command
, struct zclient
*zclient
,
1686 zebra_size_t length
, vrf_id_t vrf_id
)
1694 char buf
[ETHER_ADDR_STRLEN
];
1695 char buf1
[INET6_ADDRSTRLEN
];
1698 memset(&ip
, 0, sizeof(ip
));
1700 vni
= stream_getl(s
);
1701 stream_get(&mac
.octet
, s
, ETH_ALEN
);
1702 ipa_len
= stream_getl(s
);
1703 if (ipa_len
!= 0 && ipa_len
!= IPV4_MAX_BYTELEN
1704 && ipa_len
!= IPV6_MAX_BYTELEN
) {
1705 zlog_err("%u:Recv MACIP %s with invalid IP addr length %d",
1706 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
1713 (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
: IPADDR_V6
;
1714 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
1716 flags
= stream_getc(s
);
1718 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1722 if (BGP_DEBUG(zebra
, ZEBRA
))
1723 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u",
1724 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
1725 flags
, prefix_mac2str(&mac
, buf
, sizeof(buf
)),
1726 ipaddr2str(&ip
, buf1
, sizeof(buf1
)), vni
);
1728 if (command
== ZEBRA_MACIP_ADD
)
1729 return bgp_evpn_local_macip_add(bgp
, vni
, &mac
, &ip
, flags
);
1731 return bgp_evpn_local_macip_del(bgp
, vni
, &mac
, &ip
);
1734 void bgp_zebra_init(struct thread_master
*master
)
1736 zclient_num_connects
= 0;
1738 /* Set default values. */
1739 zclient
= zclient_new(master
);
1740 zclient_init(zclient
, ZEBRA_ROUTE_BGP
, 0);
1741 zclient
->zebra_connected
= bgp_zebra_connected
;
1742 zclient
->router_id_update
= bgp_router_id_update
;
1743 zclient
->interface_add
= bgp_interface_add
;
1744 zclient
->interface_delete
= bgp_interface_delete
;
1745 zclient
->interface_address_add
= bgp_interface_address_add
;
1746 zclient
->interface_address_delete
= bgp_interface_address_delete
;
1747 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
1748 zclient
->interface_nbr_address_delete
=
1749 bgp_interface_nbr_address_delete
;
1750 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
1751 zclient
->redistribute_route_add
= zebra_read_route
;
1752 zclient
->redistribute_route_del
= zebra_read_route
;
1753 zclient
->interface_up
= bgp_interface_up
;
1754 zclient
->interface_down
= bgp_interface_down
;
1755 zclient
->nexthop_update
= bgp_read_nexthop_update
;
1756 zclient
->import_check_update
= bgp_read_import_check_update
;
1757 zclient
->fec_update
= bgp_read_fec_update
;
1758 zclient
->local_vni_add
= bgp_zebra_process_local_vni
;
1759 zclient
->local_vni_del
= bgp_zebra_process_local_vni
;
1760 zclient
->local_macip_add
= bgp_zebra_process_local_macip
;
1761 zclient
->local_macip_del
= bgp_zebra_process_local_macip
;
1764 void bgp_zebra_destroy(void)
1766 if (zclient
== NULL
)
1768 zclient_stop(zclient
);
1769 zclient_free(zclient
);
1773 int bgp_zebra_num_connects(void)
1775 return zclient_num_connects
;