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 /* These array buffers are used in making a copy of the attributes for
62 route-map apply. Arrays are being used here to minimize mallocs and
63 frees for the temporary copy of the attributes.
64 Given the zapi api expects the nexthop buffer to contain pointer to
65 pointers for nexthops, we couldnt have used a single nexthop variable
66 on the stack, hence we had two options:
67 1. maintain a linked-list and free it after zapi_*_route call
68 2. use an array to avoid number of mallocs.
69 Number of supported next-hops are finite, use of arrays should be ok. */
70 struct attr attr_cp
[MULTIPATH_NUM
];
71 unsigned int attr_index
= 0;
73 /* Once per address-family initialization of the attribute array */
74 #define BGP_INFO_ATTR_BUF_INIT() \
76 memset(attr_cp, 0, MULTIPATH_NUM * sizeof(struct attr)); \
80 #define BGP_INFO_ATTR_BUF_COPY(info_src, info_dst) \
82 *info_dst = *info_src; \
83 assert(attr_index != multipath_num); \
84 bgp_attr_dup(&attr_cp[attr_index], info_src->attr); \
85 bgp_attr_deep_dup(&attr_cp[attr_index], info_src->attr); \
86 info_dst->attr = &attr_cp[attr_index]; \
90 #define BGP_INFO_ATTR_BUF_FREE(info) \
92 bgp_attr_deep_free(info->attr); \
96 /* Can we install into zebra? */
97 static inline int bgp_install_info_to_zebra(struct bgp
*bgp
)
99 if (zclient
->sock
<= 0)
102 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
108 int zclient_num_connects
;
110 /* Router-id update message from zebra. */
111 static int bgp_router_id_update(int command
, struct zclient
*zclient
,
112 zebra_size_t length
, vrf_id_t vrf_id
)
114 struct prefix router_id
;
116 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
118 if (BGP_DEBUG(zebra
, ZEBRA
)) {
119 char buf
[PREFIX2STR_BUFFER
];
120 prefix2str(&router_id
, buf
, sizeof(buf
));
121 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id
, buf
);
124 bgp_router_id_zebra_bump(vrf_id
, &router_id
);
128 /* Nexthop update message from zebra. */
129 static int bgp_read_nexthop_update(int command
, struct zclient
*zclient
,
130 zebra_size_t length
, vrf_id_t vrf_id
)
132 bgp_parse_nexthop_update(command
, vrf_id
);
136 static int bgp_read_import_check_update(int command
, struct zclient
*zclient
,
137 zebra_size_t length
, vrf_id_t vrf_id
)
139 bgp_parse_nexthop_update(command
, vrf_id
);
143 /* Set or clear interface on which unnumbered neighbor is configured. This
144 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
147 static void bgp_update_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
,
148 struct interface
*upd_ifp
)
150 struct listnode
*node
, *nnode
;
153 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
154 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)) {
157 bgp_zebra_initiate_radv(bgp
, peer
);
159 bgp_zebra_terminate_radv(bgp
, peer
);
166 static int bgp_read_fec_update(int command
, struct zclient
*zclient
,
169 bgp_parse_fec_update();
173 static void bgp_start_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
)
175 struct listnode
*node
, *nnode
;
178 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
179 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)
180 && peer
->status
!= Established
) {
181 if (peer_active(peer
))
182 BGP_EVENT_ADD(peer
, BGP_Stop
);
183 BGP_EVENT_ADD(peer
, BGP_Start
);
188 static void bgp_nbr_connected_add(struct bgp
*bgp
, struct nbr_connected
*ifc
)
190 struct listnode
*node
;
191 struct connected
*connected
;
192 struct interface
*ifp
;
195 /* Kick-off the FSM for any relevant peers only if there is a
196 * valid local address on the interface.
199 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
200 p
= connected
->address
;
201 if (p
->family
== AF_INET6
202 && IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
208 bgp_start_interface_nbrs(bgp
, ifp
);
211 static void bgp_nbr_connected_delete(struct bgp
*bgp
, struct nbr_connected
*ifc
,
214 struct listnode
*node
, *nnode
;
216 struct interface
*ifp
;
218 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
220 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)) {
221 peer
->last_reset
= PEER_DOWN_NBR_ADDR_DEL
;
222 BGP_EVENT_ADD(peer
, BGP_Stop
);
225 /* Free neighbor also, if we're asked to. */
228 listnode_delete(ifp
->nbr_connected
, ifc
);
229 nbr_connected_free(ifc
);
233 /* Inteface addition message from zebra. */
234 static int bgp_interface_add(int command
, struct zclient
*zclient
,
235 zebra_size_t length
, vrf_id_t vrf_id
)
237 struct interface
*ifp
;
240 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
241 if (!ifp
) // unexpected
244 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
245 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id
, ifp
->name
);
247 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
251 bgp_update_interface_nbrs(bgp
, ifp
, ifp
);
255 static int bgp_interface_delete(int command
, struct zclient
*zclient
,
256 zebra_size_t length
, vrf_id_t vrf_id
)
259 struct interface
*ifp
;
263 ifp
= zebra_interface_state_read(s
, vrf_id
);
264 if (!ifp
) /* This may happen if we've just unregistered for a VRF. */
267 if (BGP_DEBUG(zebra
, ZEBRA
))
268 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id
, ifp
->name
);
270 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
274 bgp_update_interface_nbrs(bgp
, ifp
, NULL
);
276 ifp
->ifindex
= IFINDEX_DELETED
;
280 static int bgp_interface_up(int command
, struct zclient
*zclient
,
281 zebra_size_t length
, vrf_id_t vrf_id
)
284 struct interface
*ifp
;
286 struct nbr_connected
*nc
;
287 struct listnode
*node
, *nnode
;
291 ifp
= zebra_interface_state_read(s
, vrf_id
);
296 if (BGP_DEBUG(zebra
, ZEBRA
))
297 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id
, ifp
->name
);
299 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
303 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
304 bgp_connected_add(bgp
, c
);
306 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
307 bgp_nbr_connected_add(bgp
, nc
);
312 static int bgp_interface_down(int command
, struct zclient
*zclient
,
313 zebra_size_t length
, vrf_id_t vrf_id
)
316 struct interface
*ifp
;
318 struct nbr_connected
*nc
;
319 struct listnode
*node
, *nnode
;
323 ifp
= zebra_interface_state_read(s
, vrf_id
);
327 if (BGP_DEBUG(zebra
, ZEBRA
))
328 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id
, ifp
->name
);
330 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
334 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
335 bgp_connected_delete(bgp
, c
);
337 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
338 bgp_nbr_connected_delete(bgp
, nc
, 1);
340 /* Fast external-failover */
344 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
347 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
348 #if defined(HAVE_CUMULUS)
349 /* Take down directly connected EBGP peers as well as
351 * tracked (directly connected) IBGP peers.
353 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1)
355 || bgp_bfd_is_peer_multihop(peer
)))
357 /* Take down directly connected EBGP peers */
358 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
362 if (ifp
== peer
->nexthop
.ifp
) {
363 BGP_EVENT_ADD(peer
, BGP_Stop
);
364 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
372 static int bgp_interface_address_add(int command
, struct zclient
*zclient
,
373 zebra_size_t length
, vrf_id_t vrf_id
)
375 struct connected
*ifc
;
377 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
382 if (bgp_debug_zebra(ifc
->address
)) {
383 char buf
[PREFIX2STR_BUFFER
];
384 prefix2str(ifc
->address
, buf
, sizeof(buf
));
385 zlog_debug("Rx Intf address add VRF %u IF %s addr %s", vrf_id
,
386 ifc
->ifp
->name
, buf
);
389 if (if_is_operative(ifc
->ifp
)) {
392 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
396 bgp_connected_add(bgp
, ifc
);
397 /* If we have learnt of any neighbors on this interface,
398 * check to kick off any BGP interface-based neighbors,
399 * but only if this is a link-local address.
401 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
)
402 && !list_isempty(ifc
->ifp
->nbr_connected
))
403 bgp_start_interface_nbrs(bgp
, ifc
->ifp
);
409 static int bgp_interface_address_delete(int command
, struct zclient
*zclient
,
410 zebra_size_t length
, vrf_id_t vrf_id
)
412 struct connected
*ifc
;
415 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
420 if (bgp_debug_zebra(ifc
->address
)) {
421 char buf
[PREFIX2STR_BUFFER
];
422 prefix2str(ifc
->address
, buf
, sizeof(buf
));
423 zlog_debug("Rx Intf address del VRF %u IF %s addr %s", vrf_id
,
424 ifc
->ifp
->name
, buf
);
427 if (if_is_operative(ifc
->ifp
)) {
428 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
430 bgp_connected_delete(bgp
, ifc
);
438 static int bgp_interface_nbr_address_add(int command
, struct zclient
*zclient
,
439 zebra_size_t length
, vrf_id_t vrf_id
)
441 struct nbr_connected
*ifc
= NULL
;
444 ifc
= zebra_interface_nbr_address_read(command
, zclient
->ibuf
, vrf_id
);
449 if (bgp_debug_zebra(ifc
->address
)) {
450 char buf
[PREFIX2STR_BUFFER
];
451 prefix2str(ifc
->address
, buf
, sizeof(buf
));
452 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s", vrf_id
,
453 ifc
->ifp
->name
, buf
);
456 if (if_is_operative(ifc
->ifp
)) {
457 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
459 bgp_nbr_connected_add(bgp
, ifc
);
465 static int bgp_interface_nbr_address_delete(int command
,
466 struct zclient
*zclient
,
470 struct nbr_connected
*ifc
= NULL
;
473 ifc
= zebra_interface_nbr_address_read(command
, zclient
->ibuf
, vrf_id
);
478 if (bgp_debug_zebra(ifc
->address
)) {
479 char buf
[PREFIX2STR_BUFFER
];
480 prefix2str(ifc
->address
, buf
, sizeof(buf
));
481 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s", vrf_id
,
482 ifc
->ifp
->name
, buf
);
485 if (if_is_operative(ifc
->ifp
)) {
486 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
488 bgp_nbr_connected_delete(bgp
, ifc
, 0);
491 nbr_connected_free(ifc
);
496 /* VRF update for an interface. */
497 static int bgp_interface_vrf_update(int command
, struct zclient
*zclient
,
498 zebra_size_t length
, vrf_id_t vrf_id
)
500 struct interface
*ifp
;
503 struct nbr_connected
*nc
;
504 struct listnode
*node
, *nnode
;
507 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
512 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
513 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id
,
514 ifp
->name
, new_vrf_id
);
516 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
520 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
521 bgp_connected_delete(bgp
, c
);
523 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
524 bgp_nbr_connected_delete(bgp
, nc
, 1);
526 /* Fast external-failover */
530 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
533 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
534 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
537 if (ifp
== peer
->nexthop
.ifp
)
538 BGP_EVENT_ADD(peer
, BGP_Stop
);
542 if_update_to_new_vrf(ifp
, new_vrf_id
);
544 bgp
= bgp_lookup_by_vrf_id(new_vrf_id
);
548 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
549 bgp_connected_add(bgp
, c
);
551 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
552 bgp_nbr_connected_add(bgp
, nc
);
556 /* Zebra route add and delete treatment. */
557 static int zebra_read_route(int command
, struct zclient
*zclient
,
558 zebra_size_t length
, vrf_id_t vrf_id
)
560 struct zapi_route api
;
561 union g_addr nexthop
;
562 unsigned int ifindex
;
566 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
570 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
573 /* we completely ignore srcdest routes for now. */
574 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
577 /* ignore link-local address. */
578 if (api
.prefix
.family
== AF_INET6
579 && IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
582 nexthop
= api
.nexthops
[0].gate
;
583 ifindex
= api
.nexthops
[0].ifindex
;
585 add
= (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
588 * The ADD message is actually an UPDATE and there is no
590 * for a prior redistributed route, if any. So, perform an
592 * DEL processing for the same redistributed route from any
596 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
598 bgp_redistribute_delete(bgp
, &api
.prefix
, i
,
602 /* Now perform the add/update. */
603 bgp_redistribute_add(bgp
, &api
.prefix
, &nexthop
, ifindex
,
604 api
.metric
, api
.type
, api
.instance
,
607 bgp_redistribute_delete(bgp
, &api
.prefix
, api
.type
,
611 if (bgp_debug_zebra(&api
.prefix
)) {
612 char buf
[2][PREFIX_STRLEN
];
614 prefix2str(&api
.prefix
, buf
[0], sizeof(buf
[0]));
615 inet_ntop(api
.prefix
.family
, &nexthop
, buf
[1], sizeof(buf
[1]));
617 "Rx route %s VRF %u %s[%d] %s "
618 "nexthop %s metric %u tag %" ROUTE_TAG_PRI
,
619 (add
) ? "add" : "delete", vrf_id
,
620 zebra_route_string(api
.type
), api
.instance
, buf
[0],
621 buf
[1], api
.metric
, api
.tag
);
627 struct interface
*if_lookup_by_ipv4(struct in_addr
*addr
, vrf_id_t vrf_id
)
629 struct listnode
*ifnode
;
630 struct listnode
*cnode
;
631 struct interface
*ifp
;
632 struct connected
*connected
;
633 struct prefix_ipv4 p
;
638 p
.prefixlen
= IPV4_MAX_BITLEN
;
640 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), ifnode
, ifp
)) {
641 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
642 cp
= connected
->address
;
644 if (cp
->family
== AF_INET
)
645 if (prefix_match(cp
, (struct prefix
*)&p
))
652 struct interface
*if_lookup_by_ipv4_exact(struct in_addr
*addr
, vrf_id_t vrf_id
)
654 struct listnode
*ifnode
;
655 struct listnode
*cnode
;
656 struct interface
*ifp
;
657 struct connected
*connected
;
660 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), ifnode
, ifp
)) {
661 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
662 cp
= connected
->address
;
664 if (cp
->family
== AF_INET
)
665 if (IPV4_ADDR_SAME(&cp
->u
.prefix4
, addr
))
672 struct interface
*if_lookup_by_ipv6(struct in6_addr
*addr
, ifindex_t ifindex
,
675 struct listnode
*ifnode
;
676 struct listnode
*cnode
;
677 struct interface
*ifp
;
678 struct connected
*connected
;
679 struct prefix_ipv6 p
;
684 p
.prefixlen
= IPV6_MAX_BITLEN
;
686 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), ifnode
, ifp
)) {
687 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
688 cp
= connected
->address
;
690 if (cp
->family
== AF_INET6
)
691 if (prefix_match(cp
, (struct prefix
*)&p
)) {
692 if (IN6_IS_ADDR_LINKLOCAL(
694 if (ifindex
== ifp
->ifindex
)
704 struct interface
*if_lookup_by_ipv6_exact(struct in6_addr
*addr
,
705 ifindex_t ifindex
, vrf_id_t vrf_id
)
707 struct listnode
*ifnode
;
708 struct listnode
*cnode
;
709 struct interface
*ifp
;
710 struct connected
*connected
;
713 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), ifnode
, ifp
)) {
714 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
715 cp
= connected
->address
;
717 if (cp
->family
== AF_INET6
)
718 if (IPV6_ADDR_SAME(&cp
->u
.prefix6
, addr
)) {
719 if (IN6_IS_ADDR_LINKLOCAL(
721 if (ifindex
== ifp
->ifindex
)
731 static int if_get_ipv6_global(struct interface
*ifp
, struct in6_addr
*addr
)
733 struct listnode
*cnode
;
734 struct connected
*connected
;
737 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
738 cp
= connected
->address
;
740 if (cp
->family
== AF_INET6
)
741 if (!IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
742 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
749 static int if_get_ipv6_local(struct interface
*ifp
, struct in6_addr
*addr
)
751 struct listnode
*cnode
;
752 struct connected
*connected
;
755 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
756 cp
= connected
->address
;
758 if (cp
->family
== AF_INET6
)
759 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
760 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
767 static int if_get_ipv4_address(struct interface
*ifp
, struct in_addr
*addr
)
769 struct listnode
*cnode
;
770 struct connected
*connected
;
773 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
774 cp
= connected
->address
;
775 if ((cp
->family
== AF_INET
)
776 && !ipv4_martian(&(cp
->u
.prefix4
))) {
777 *addr
= cp
->u
.prefix4
;
784 int bgp_nexthop_set(union sockunion
*local
, union sockunion
*remote
,
785 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
788 struct interface
*ifp
= NULL
;
790 memset(nexthop
, 0, sizeof(struct bgp_nexthop
));
797 if (local
->sa
.sa_family
== AF_INET
) {
798 nexthop
->v4
= local
->sin
.sin_addr
;
800 ifp
= if_lookup_by_name(peer
->update_if
,
803 ifp
= if_lookup_by_ipv4_exact(&local
->sin
.sin_addr
,
806 if (local
->sa
.sa_family
== AF_INET6
) {
807 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
808 if (peer
->conf_if
|| peer
->ifname
)
809 ifp
= if_lookup_by_name(peer
->conf_if
813 } else if (peer
->update_if
)
814 ifp
= if_lookup_by_name(peer
->update_if
,
817 ifp
= if_lookup_by_ipv6_exact(&local
->sin6
.sin6_addr
,
818 local
->sin6
.sin6_scope_id
,
827 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
828 if (local
->sa
.sa_family
== AF_INET
) {
830 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
833 /* There is no global nexthop. Use link-local address as
835 * global and link-local nexthop. In this scenario, the
837 * for interop is that the network admin would use a
839 * specify the global IPv6 nexthop.
841 if_get_ipv6_local(ifp
, &nexthop
->v6_global
);
842 memcpy(&nexthop
->v6_local
, &nexthop
->v6_global
,
845 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
847 if (if_lookup_by_ipv4(&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
848 peer
->shared_network
= 1;
850 peer
->shared_network
= 0;
853 /* IPv6 connection, fetch and store IPv4 local address if any. */
854 if (local
->sa
.sa_family
== AF_INET6
) {
855 struct interface
*direct
= NULL
;
858 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
859 if (!ret
&& peer
->local_id
.s_addr
)
860 nexthop
->v4
= peer
->local_id
;
863 if (!IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
864 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
867 /* If directory connected set link-local address. */
868 direct
= if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
869 remote
->sin6
.sin6_scope_id
,
872 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
874 /* Link-local address. */
876 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
878 /* If there is no global address. Set link-local
880 global. I know this break RFC specification... */
881 /* In this scenario, the expectation for interop is that
883 * network admin would use a route-map to specify the
888 memcpy(&nexthop
->v6_global
,
889 &local
->sin6
.sin6_addr
,
891 /* Always set the link-local address */
892 memcpy(&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
896 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)
897 || if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
898 remote
->sin6
.sin6_scope_id
,
900 peer
->shared_network
= 1;
902 peer
->shared_network
= 0;
905 /* KAME stack specific treatment. */
907 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_global
)
908 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
)) {
909 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
, 0);
911 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_local
)
912 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
)) {
913 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
, 0);
917 /* If we have identified the local interface, there is no error for now.
922 static struct in6_addr
*bgp_info_to_ipv6_nexthop(struct bgp_info
*info
)
924 struct in6_addr
*nexthop
= NULL
;
926 /* Only global address nexthop exists. */
927 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
)
928 nexthop
= &info
->attr
->mp_nexthop_global
;
930 /* If both global and link-local address present. */
931 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
932 /* Check if route-map is set to prefer global over link-local */
933 if (info
->attr
->mp_nexthop_prefer_global
)
934 nexthop
= &info
->attr
->mp_nexthop_global
;
936 /* Workaround for Cisco's nexthop bug. */
937 if (IN6_IS_ADDR_UNSPECIFIED(
938 &info
->attr
->mp_nexthop_global
)
939 && info
->peer
->su_remote
->sa
.sa_family
== AF_INET6
)
941 &info
->peer
->su_remote
->sin6
.sin6_addr
;
943 nexthop
= &info
->attr
->mp_nexthop_local
;
950 static int bgp_table_map_apply(struct route_map
*map
, struct prefix
*p
,
951 struct bgp_info
*info
)
953 if (route_map_apply(map
, p
, RMAP_BGP
, info
) != RMAP_DENYMATCH
)
956 if (bgp_debug_zebra(p
)) {
957 if (p
->family
== AF_INET
) {
958 char buf
[2][INET_ADDRSTRLEN
];
960 "Zebra rmap deny: IPv4 route %s/%d nexthop %s",
961 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0],
964 inet_ntop(AF_INET
, &info
->attr
->nexthop
, buf
[1],
967 if (p
->family
== AF_INET6
) {
968 char buf
[2][INET6_ADDRSTRLEN
];
970 "Zebra rmap deny: IPv6 route %s/%d nexthop %s",
971 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0],
975 bgp_info_to_ipv6_nexthop(info
),
976 buf
[1], sizeof(buf
[1])));
982 void bgp_zebra_announce(struct bgp_node
*rn
, struct prefix
*p
,
983 struct bgp_info
*info
, struct bgp
*bgp
, afi_t afi
,
986 struct zapi_route api
;
987 struct zapi_nexthop
*api_nh
;
989 int valid_nh_count
= 0;
990 int has_valid_label
= 0;
993 struct bgp_info
*mpinfo
;
995 struct bgp_info local_info
;
996 struct bgp_info
*info_cp
= &local_info
;
1000 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1001 * know of this instance.
1003 if (!bgp_install_info_to_zebra(bgp
))
1006 if (bgp
->main_zebra_update_hold
)
1009 /* Make Zebra API structure. */
1010 memset(&api
, 0, sizeof(api
));
1011 api
.vrf_id
= bgp
->vrf_id
;
1012 api
.type
= ZEBRA_ROUTE_BGP
;
1015 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1019 tag
= info
->attr
->tag
;
1021 /* When we create an aggregate route we must also install a Null0 route
1024 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1025 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
1027 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
1028 || info
->sub_type
== BGP_ROUTE_AGGREGATE
) {
1029 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1030 SET_FLAG(api
.flags
, ZEBRA_FLAG_INTERNAL
);
1033 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1034 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1035 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1037 SET_FLAG(api
.flags
, ZEBRA_FLAG_INTERNAL
);
1039 /* Get nexthop address-family */
1040 if (p
->family
== AF_INET
&& !BGP_ATTR_NEXTHOP_AFI_IP6(info
->attr
))
1041 nh_family
= AF_INET
;
1042 else if (p
->family
== AF_INET6
1043 || (p
->family
== AF_INET
1044 && BGP_ATTR_NEXTHOP_AFI_IP6(info
->attr
)))
1045 nh_family
= AF_INET6
;
1049 if (bgp
->table_map
[afi
][safi
].name
)
1050 BGP_INFO_ATTR_BUF_INIT();
1052 /* Metric is currently based on the best-path only */
1053 metric
= info
->attr
->med
;
1054 for (mpinfo
= info
; mpinfo
; mpinfo
= bgp_info_mpath_next(mpinfo
)) {
1055 if (nh_family
== AF_INET
) {
1056 struct in_addr
*nexthop
;
1060 if (bgp
->table_map
[afi
][safi
].name
) {
1061 /* Copy info and attributes, so the route-map
1062 apply doesn't modify the
1064 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
1065 if (bgp_table_map_apply(
1066 bgp
->table_map
[afi
][safi
].map
, p
,
1068 if (mpinfo
== info
) {
1069 metric
= info_cp
->attr
->med
;
1070 tag
= info_cp
->attr
->tag
;
1072 nexthop
= &info_cp
->attr
->nexthop
;
1074 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1076 nexthop
= &mpinfo
->attr
->nexthop
;
1078 if (nexthop
== NULL
)
1081 api_nh
= &api
.nexthops
[valid_nh_count
];
1082 api_nh
->gate
.ipv4
= *nexthop
;
1083 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1086 struct in6_addr
*nexthop
;
1091 if (bgp
->table_map
[afi
][safi
].name
) {
1092 /* Copy info and attributes, so the route-map
1093 apply doesn't modify the
1095 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
1096 if (bgp_table_map_apply(
1097 bgp
->table_map
[afi
][safi
].map
, p
,
1099 if (mpinfo
== info
) {
1100 metric
= info_cp
->attr
->med
;
1101 tag
= info_cp
->attr
->tag
;
1103 nexthop
= bgp_info_to_ipv6_nexthop(
1106 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1108 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo
);
1110 if (nexthop
== NULL
)
1113 if ((mpinfo
== info
)
1114 && mpinfo
->attr
->mp_nexthop_len
1115 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1116 if (mpinfo
->peer
->nexthop
.ifp
)
1117 ifindex
= mpinfo
->peer
->nexthop
.ifp
1121 if (mpinfo
->peer
->conf_if
1122 || mpinfo
->peer
->ifname
)
1123 ifindex
= ifname2ifindex(
1124 mpinfo
->peer
->conf_if
1125 ? mpinfo
->peer
->conf_if
1126 : mpinfo
->peer
->ifname
,
1128 else if (mpinfo
->peer
->nexthop
.ifp
)
1129 ifindex
= mpinfo
->peer
->nexthop
.ifp
1135 api_nh
= &api
.nexthops
[valid_nh_count
];
1136 api_nh
->gate
.ipv6
= *nexthop
;
1137 api_nh
->ifindex
= ifindex
;
1138 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1142 && bgp_is_valid_label(&mpinfo
->extra
->label
)) {
1143 has_valid_label
= 1;
1144 label
= label_pton(&mpinfo
->extra
->label
);
1146 api_nh
->label_num
= 1;
1147 api_nh
->labels
[0] = label
;
1152 if (has_valid_label
)
1153 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
1155 if (info
->sub_type
!= BGP_ROUTE_AGGREGATE
)
1156 api
.nexthop_num
= valid_nh_count
;
1158 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1159 api
.metric
= metric
;
1162 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1166 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1168 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1169 api
.distance
= distance
;
1172 if (bgp_debug_zebra(p
)) {
1173 char prefix_buf
[PREFIX_STRLEN
];
1174 char nh_buf
[INET6_ADDRSTRLEN
];
1178 prefix2str(&api
.prefix
, prefix_buf
, sizeof(prefix_buf
));
1179 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1181 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1182 prefix_buf
, api
.metric
, api
.tag
, api
.nexthop_num
);
1183 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1184 api_nh
= &api
.nexthops
[i
];
1186 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1189 label_buf
[0] = '\0';
1190 if (has_valid_label
)
1191 sprintf(label_buf
, "label %u",
1193 zlog_debug(" nhop [%d]: %s %s", i
+ 1, nh_buf
,
1198 zclient_route_send(valid_nh_count
? ZEBRA_ROUTE_ADD
1199 : ZEBRA_ROUTE_DELETE
,
1203 /* Announce all routes of a table to zebra */
1204 void bgp_zebra_announce_table(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1206 struct bgp_node
*rn
;
1207 struct bgp_table
*table
;
1208 struct bgp_info
*ri
;
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(bgp
))
1216 table
= bgp
->rib
[afi
][safi
];
1220 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
1221 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1222 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
1223 && ri
->type
== ZEBRA_ROUTE_BGP
1224 && ri
->sub_type
== BGP_ROUTE_NORMAL
)
1225 bgp_zebra_announce(rn
, &rn
->p
, ri
, bgp
, afi
,
1229 void bgp_zebra_withdraw(struct prefix
*p
, struct bgp_info
*info
, safi_t safi
)
1231 struct zapi_route api
;
1237 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1238 * know of this instance.
1240 if (!bgp_install_info_to_zebra(peer
->bgp
))
1243 memset(&api
, 0, sizeof(api
));
1244 api
.vrf_id
= peer
->bgp
->vrf_id
;
1245 api
.type
= ZEBRA_ROUTE_BGP
;
1249 if (peer
->sort
== BGP_PEER_IBGP
) {
1250 SET_FLAG(api
.flags
, ZEBRA_FLAG_INTERNAL
);
1251 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1254 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1255 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1256 || bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1257 SET_FLAG(api
.flags
, ZEBRA_FLAG_INTERNAL
);
1259 if (bgp_debug_zebra(p
)) {
1260 char buf
[PREFIX_STRLEN
];
1262 prefix2str(&api
.prefix
, buf
, sizeof(buf
));
1263 zlog_debug("Tx route delete VRF %u %s", peer
->bgp
->vrf_id
, buf
);
1266 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
1269 struct bgp_redist
*bgp_redist_lookup(struct bgp
*bgp
, afi_t afi
, u_char type
,
1272 struct list
*red_list
;
1273 struct listnode
*node
;
1274 struct bgp_redist
*red
;
1276 red_list
= bgp
->redist
[afi
][type
];
1280 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1281 if (red
->instance
== instance
)
1287 struct bgp_redist
*bgp_redist_add(struct bgp
*bgp
, afi_t afi
, u_char type
,
1290 struct list
*red_list
;
1291 struct bgp_redist
*red
;
1293 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1297 if (!bgp
->redist
[afi
][type
])
1298 bgp
->redist
[afi
][type
] = list_new();
1300 red_list
= bgp
->redist
[afi
][type
];
1301 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
,
1302 sizeof(struct bgp_redist
));
1303 red
->instance
= instance
;
1305 listnode_add(red_list
, red
);
1310 static void bgp_redist_del(struct bgp
*bgp
, afi_t afi
, u_char type
,
1313 struct bgp_redist
*red
;
1315 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1318 listnode_delete(bgp
->redist
[afi
][type
], red
);
1319 XFREE(MTYPE_BGP_REDIST
, red
);
1320 if (!bgp
->redist
[afi
][type
]->count
) {
1321 list_free(bgp
->redist
[afi
][type
]);
1322 bgp
->redist
[afi
][type
] = NULL
;
1327 /* Other routes redistribution into BGP. */
1328 int bgp_redistribute_set(struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1331 /* Return if already redistribute flag is set. */
1333 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1337 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1339 if (vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1343 if (bgp
->vrf_id
== VRF_DEFAULT
1344 && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1345 vnc_export_bgp_enable(
1346 bgp
, afi
); /* only enables if mode bits cfg'd */
1350 vrf_bitmap_set(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1353 /* Don't try to register if we're not connected to Zebra or Zebra
1355 * know of this instance.
1357 if (!bgp_install_info_to_zebra(bgp
))
1358 return CMD_WARNING_CONFIG_FAILED
;
1360 if (BGP_DEBUG(zebra
, ZEBRA
))
1361 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1362 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1365 /* Send distribute add message to zebra. */
1366 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1367 instance
, bgp
->vrf_id
);
1372 int bgp_redistribute_resend(struct bgp
*bgp
, afi_t afi
, int type
,
1375 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1376 * know of this instance.
1378 if (!bgp_install_info_to_zebra(bgp
))
1381 if (BGP_DEBUG(zebra
, ZEBRA
))
1382 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1383 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1386 /* Send distribute add message to zebra. */
1387 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1388 instance
, bgp
->vrf_id
);
1389 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1390 instance
, bgp
->vrf_id
);
1395 /* Redistribute with route-map specification. */
1396 int bgp_redistribute_rmap_set(struct bgp_redist
*red
, const char *name
)
1398 if (red
->rmap
.name
&& (strcmp(red
->rmap
.name
, name
) == 0))
1402 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1403 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1404 red
->rmap
.map
= route_map_lookup_by_name(name
);
1409 /* Redistribute with metric specification. */
1410 int bgp_redistribute_metric_set(struct bgp
*bgp
, struct bgp_redist
*red
,
1411 afi_t afi
, int type
, u_int32_t metric
)
1413 struct bgp_node
*rn
;
1414 struct bgp_info
*ri
;
1416 if (red
->redist_metric_flag
&& red
->redist_metric
== metric
)
1419 red
->redist_metric_flag
= 1;
1420 red
->redist_metric
= metric
;
1422 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
1423 rn
= bgp_route_next(rn
)) {
1424 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
1425 if (ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
1427 && ri
->instance
== red
->instance
) {
1428 struct attr
*old_attr
;
1429 struct attr new_attr
;
1431 bgp_attr_dup(&new_attr
, ri
->attr
);
1432 new_attr
.med
= red
->redist_metric
;
1433 old_attr
= ri
->attr
;
1434 ri
->attr
= bgp_attr_intern(&new_attr
);
1435 bgp_attr_unintern(&old_attr
);
1437 bgp_info_set_flag(rn
, ri
,
1438 BGP_INFO_ATTR_CHANGED
);
1439 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1447 /* Unset redistribution. */
1448 int bgp_redistribute_unreg(struct bgp
*bgp
, afi_t afi
, int type
,
1451 struct bgp_redist
*red
;
1453 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1457 /* Return if zebra connection is disabled. */
1459 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1462 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1464 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1466 vrf_bitmap_unset(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1470 if (bgp
->vrf_id
== VRF_DEFAULT
&& type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1471 vnc_export_bgp_disable(bgp
, afi
);
1475 if (bgp_install_info_to_zebra(bgp
)) {
1476 /* Send distribute delete message to zebra. */
1477 if (BGP_DEBUG(zebra
, ZEBRA
))
1478 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1479 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1481 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
1482 type
, instance
, bgp
->vrf_id
);
1485 /* Withdraw redistributed routes from current BGP's routing table. */
1486 bgp_redistribute_withdraw(bgp
, afi
, type
, instance
);
1491 /* Unset redistribution. */
1492 int bgp_redistribute_unset(struct bgp
*bgp
, afi_t afi
, int type
,
1495 struct bgp_redist
*red
;
1497 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1501 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1503 /* Unset route-map. */
1505 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1506 red
->rmap
.name
= NULL
;
1507 red
->rmap
.map
= NULL
;
1510 red
->redist_metric_flag
= 0;
1511 red
->redist_metric
= 0;
1513 bgp_redist_del(bgp
, afi
, type
, instance
);
1518 /* Update redistribute vrf bitmap during triggers like
1519 restart networking or delete/add VRFs */
1520 void bgp_update_redist_vrf_bitmaps(struct bgp
*bgp
, vrf_id_t old_vrf_id
)
1525 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1526 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1527 if (vrf_bitmap_check(zclient
->redist
[afi
][i
],
1529 vrf_bitmap_unset(zclient
->redist
[afi
][i
],
1531 vrf_bitmap_set(zclient
->redist
[afi
][i
],
1537 void bgp_zclient_reset(void)
1539 zclient_reset(zclient
);
1542 /* Register this instance with Zebra. Invoked upon connect (for
1543 * default instance) and when other VRFs are learnt (or created and
1546 void bgp_zebra_instance_register(struct bgp
*bgp
)
1548 /* Don't try to register if we're not connected to Zebra */
1549 if (!zclient
|| zclient
->sock
< 0)
1552 if (BGP_DEBUG(zebra
, ZEBRA
))
1553 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
1555 /* Register for router-id, interfaces, redistributed routes. */
1556 zclient_send_reg_requests(zclient
, bgp
->vrf_id
);
1558 /* For default instance, register to learn about VNIs, if appropriate.
1560 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
1561 && bgp
->advertise_all_vni
)
1562 bgp_zebra_advertise_all_vni(bgp
, 1);
1565 /* Deregister this instance with Zebra. Invoked upon the instance
1566 * being deleted (default or VRF) and it is already registered.
1568 void bgp_zebra_instance_deregister(struct bgp
*bgp
)
1570 /* Don't try to deregister if we're not connected to Zebra */
1571 if (zclient
->sock
< 0)
1574 if (BGP_DEBUG(zebra
, ZEBRA
))
1575 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
1577 /* For default instance, unregister learning about VNIs, if appropriate.
1579 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
1580 && bgp
->advertise_all_vni
)
1581 bgp_zebra_advertise_all_vni(bgp
, 0);
1583 /* Deregister for router-id, interfaces, redistributed routes. */
1584 zclient_send_dereg_requests(zclient
, bgp
->vrf_id
);
1587 void bgp_zebra_initiate_radv(struct bgp
*bgp
, struct peer
*peer
)
1589 int ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
1591 /* Don't try to initiate if we're not connected to Zebra */
1592 if (zclient
->sock
< 0)
1595 if (BGP_DEBUG(zebra
, ZEBRA
))
1596 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
,
1599 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 1,
1603 void bgp_zebra_terminate_radv(struct bgp
*bgp
, struct peer
*peer
)
1605 /* Don't try to terminate if we're not connected to Zebra */
1606 if (zclient
->sock
< 0)
1609 if (BGP_DEBUG(zebra
, ZEBRA
))
1610 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
,
1613 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 0, 0);
1616 int bgp_zebra_advertise_gw_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
1618 struct stream
*s
= NULL
;
1621 if (!zclient
|| zclient
->sock
< 0)
1624 /* Don't try to register if Zebra doesn't know of this instance. */
1625 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1631 zclient_create_header(s
, ZEBRA_ADVERTISE_DEFAULT_GW
, bgp
->vrf_id
);
1632 stream_putc(s
, advertise
);
1633 stream_put3(s
, vni
);
1634 stream_putw_at(s
, 0, stream_get_endp(s
));
1636 return zclient_send_message(zclient
);
1639 int bgp_zebra_advertise_all_vni(struct bgp
*bgp
, int advertise
)
1644 if (!zclient
|| zclient
->sock
< 0)
1647 /* Don't try to register if Zebra doesn't know of this instance. */
1648 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1654 zclient_create_header(s
, ZEBRA_ADVERTISE_ALL_VNI
, bgp
->vrf_id
);
1655 stream_putc(s
, advertise
);
1656 stream_putw_at(s
, 0, stream_get_endp(s
));
1658 return zclient_send_message(zclient
);
1661 /* BGP has established connection with Zebra. */
1662 static void bgp_zebra_connected(struct zclient
*zclient
)
1666 zclient_num_connects
++; /* increment even if not responding */
1668 /* At this point, we may or may not have BGP instances configured, but
1669 * we're only interested in the default VRF (others wouldn't have learnt
1670 * the VRF from Zebra yet.)
1672 bgp
= bgp_get_default();
1676 bgp_zebra_instance_register(bgp
);
1678 /* Send the client registration */
1679 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
1681 /* TODO - What if we have peers and networks configured, do we have to
1686 static int bgp_zebra_process_local_vni(int command
, struct zclient
*zclient
,
1687 zebra_size_t length
, vrf_id_t vrf_id
)
1692 struct in_addr vtep_ip
;
1695 vni
= stream_getl(s
);
1696 if (command
== ZEBRA_VNI_ADD
)
1697 vtep_ip
.s_addr
= stream_get_ipv4(s
);
1698 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1702 if (BGP_DEBUG(zebra
, ZEBRA
))
1703 zlog_debug("Rx VNI %s VRF %u VNI %u",
1704 (command
== ZEBRA_VNI_ADD
) ? "add" : "del", vrf_id
,
1707 if (command
== ZEBRA_VNI_ADD
)
1708 return bgp_evpn_local_vni_add(
1709 bgp
, vni
, vtep_ip
.s_addr
? vtep_ip
: bgp
->router_id
);
1711 return bgp_evpn_local_vni_del(bgp
, vni
);
1714 static int bgp_zebra_process_local_macip(int command
, struct zclient
*zclient
,
1715 zebra_size_t length
, vrf_id_t vrf_id
)
1723 char buf
[ETHER_ADDR_STRLEN
];
1724 char buf1
[INET6_ADDRSTRLEN
];
1727 memset(&ip
, 0, sizeof(ip
));
1729 vni
= stream_getl(s
);
1730 stream_get(&mac
.octet
, s
, ETH_ALEN
);
1731 ipa_len
= stream_getl(s
);
1732 if (ipa_len
!= 0 && ipa_len
!= IPV4_MAX_BYTELEN
1733 && ipa_len
!= IPV6_MAX_BYTELEN
) {
1734 zlog_err("%u:Recv MACIP %s with invalid IP addr length %d",
1735 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
1742 (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
: IPADDR_V6
;
1743 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
1745 flags
= stream_getc(s
);
1747 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
1751 if (BGP_DEBUG(zebra
, ZEBRA
))
1752 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u",
1753 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
1754 flags
, prefix_mac2str(&mac
, buf
, sizeof(buf
)),
1755 ipaddr2str(&ip
, buf1
, sizeof(buf1
)), vni
);
1757 if (command
== ZEBRA_MACIP_ADD
)
1758 return bgp_evpn_local_macip_add(bgp
, vni
, &mac
, &ip
, flags
);
1760 return bgp_evpn_local_macip_del(bgp
, vni
, &mac
, &ip
);
1763 void bgp_zebra_init(struct thread_master
*master
)
1765 zclient_num_connects
= 0;
1767 /* Set default values. */
1768 zclient
= zclient_new(master
);
1769 zclient_init(zclient
, ZEBRA_ROUTE_BGP
, 0);
1770 zclient
->zebra_connected
= bgp_zebra_connected
;
1771 zclient
->router_id_update
= bgp_router_id_update
;
1772 zclient
->interface_add
= bgp_interface_add
;
1773 zclient
->interface_delete
= bgp_interface_delete
;
1774 zclient
->interface_address_add
= bgp_interface_address_add
;
1775 zclient
->interface_address_delete
= bgp_interface_address_delete
;
1776 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
1777 zclient
->interface_nbr_address_delete
=
1778 bgp_interface_nbr_address_delete
;
1779 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
1780 zclient
->redistribute_route_add
= zebra_read_route
;
1781 zclient
->redistribute_route_del
= zebra_read_route
;
1782 zclient
->interface_up
= bgp_interface_up
;
1783 zclient
->interface_down
= bgp_interface_down
;
1784 zclient
->nexthop_update
= bgp_read_nexthop_update
;
1785 zclient
->import_check_update
= bgp_read_import_check_update
;
1786 zclient
->fec_update
= bgp_read_fec_update
;
1787 zclient
->local_vni_add
= bgp_zebra_process_local_vni
;
1788 zclient
->local_vni_del
= bgp_zebra_process_local_vni
;
1789 zclient
->local_macip_add
= bgp_zebra_process_local_macip
;
1790 zclient
->local_macip_del
= bgp_zebra_process_local_macip
;
1793 void bgp_zebra_destroy(void)
1795 if (zclient
== NULL
)
1797 zclient_stop(zclient
);
1798 zclient_free(zclient
);
1802 int bgp_zebra_num_connects(void)
1804 return zclient_num_connects
;