2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "sockunion.h"
40 #include "bgpd/bgpd.h"
41 #include "bgpd/bgp_route.h"
42 #include "bgpd/bgp_attr.h"
43 #include "bgpd/bgp_nexthop.h"
44 #include "bgpd/bgp_zebra.h"
45 #include "bgpd/bgp_fsm.h"
46 #include "bgpd/bgp_debug.h"
47 #include "bgpd/bgp_mpath.h"
48 #include "bgpd/bgp_nexthop.h"
49 #include "bgpd/bgp_nht.h"
50 #include "bgpd/bgp_bfd.h"
51 #include "bgpd/bgp_label.h"
53 #include "bgpd/rfapi/rfapi_backend.h"
54 #include "bgpd/rfapi/vnc_export_bgp.h"
56 #include "bgpd/bgp_evpn.h"
57 #include "bgpd/bgp_mplsvpn.h"
58 #include "bgpd/bgp_labelpool.h"
59 #include "bgpd/bgp_pbr.h"
61 /* All information about zebra. */
62 struct zclient
*zclient
= NULL
;
64 /* Can we install into zebra? */
65 static inline int bgp_install_info_to_zebra(struct bgp
*bgp
)
67 if (zclient
->sock
<= 0)
70 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
76 int zclient_num_connects
;
78 /* Router-id update message from zebra. */
79 static int bgp_router_id_update(int command
, struct zclient
*zclient
,
80 zebra_size_t length
, vrf_id_t vrf_id
)
82 struct prefix router_id
;
84 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
86 if (BGP_DEBUG(zebra
, ZEBRA
)) {
87 char buf
[PREFIX2STR_BUFFER
];
88 prefix2str(&router_id
, buf
, sizeof(buf
));
89 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id
, buf
);
92 bgp_router_id_zebra_bump(vrf_id
, &router_id
);
96 /* Nexthop update message from zebra. */
97 static int bgp_read_nexthop_update(int command
, struct zclient
*zclient
,
98 zebra_size_t length
, vrf_id_t vrf_id
)
100 bgp_parse_nexthop_update(command
, vrf_id
);
104 static int bgp_read_import_check_update(int command
, struct zclient
*zclient
,
105 zebra_size_t length
, vrf_id_t vrf_id
)
107 bgp_parse_nexthop_update(command
, vrf_id
);
111 /* Set or clear interface on which unnumbered neighbor is configured. This
112 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
115 static void bgp_update_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
,
116 struct interface
*upd_ifp
)
118 struct listnode
*node
, *nnode
;
121 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
122 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)) {
125 bgp_zebra_initiate_radv(bgp
, peer
);
127 bgp_zebra_terminate_radv(bgp
, peer
);
134 static int bgp_read_fec_update(int command
, struct zclient
*zclient
,
137 bgp_parse_fec_update();
141 static void bgp_start_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
)
143 struct listnode
*node
, *nnode
;
146 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
147 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)
148 && peer
->status
!= Established
) {
149 if (peer_active(peer
))
150 BGP_EVENT_ADD(peer
, BGP_Stop
);
151 BGP_EVENT_ADD(peer
, BGP_Start
);
156 static void bgp_nbr_connected_add(struct bgp
*bgp
, struct nbr_connected
*ifc
)
158 struct listnode
*node
;
159 struct connected
*connected
;
160 struct interface
*ifp
;
163 /* Kick-off the FSM for any relevant peers only if there is a
164 * valid local address on the interface.
167 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
168 p
= connected
->address
;
169 if (p
->family
== AF_INET6
170 && IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
176 bgp_start_interface_nbrs(bgp
, ifp
);
179 static void bgp_nbr_connected_delete(struct bgp
*bgp
, struct nbr_connected
*ifc
,
182 struct listnode
*node
, *nnode
;
184 struct interface
*ifp
;
186 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
188 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)) {
189 peer
->last_reset
= PEER_DOWN_NBR_ADDR_DEL
;
190 BGP_EVENT_ADD(peer
, BGP_Stop
);
193 /* Free neighbor also, if we're asked to. */
196 listnode_delete(ifp
->nbr_connected
, ifc
);
197 nbr_connected_free(ifc
);
201 /* Inteface addition message from zebra. */
202 static int bgp_interface_add(int command
, struct zclient
*zclient
,
203 zebra_size_t length
, vrf_id_t vrf_id
)
205 struct interface
*ifp
;
208 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
209 if (!ifp
) // unexpected
212 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
213 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id
, ifp
->name
);
215 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
219 bgp_update_interface_nbrs(bgp
, ifp
, ifp
);
223 static int bgp_interface_delete(int command
, struct zclient
*zclient
,
224 zebra_size_t length
, vrf_id_t vrf_id
)
227 struct interface
*ifp
;
230 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
235 ifp
= zebra_interface_state_read(s
, vrf_id
);
236 if (!ifp
) /* This may happen if we've just unregistered for a VRF. */
239 if (BGP_DEBUG(zebra
, ZEBRA
))
240 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id
, ifp
->name
);
242 bgp_update_interface_nbrs(bgp
, ifp
, NULL
);
244 if_set_index(ifp
, IFINDEX_INTERNAL
);
248 static int bgp_interface_up(int command
, struct zclient
*zclient
,
249 zebra_size_t length
, vrf_id_t vrf_id
)
252 struct interface
*ifp
;
254 struct nbr_connected
*nc
;
255 struct listnode
*node
, *nnode
;
258 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
263 ifp
= zebra_interface_state_read(s
, vrf_id
);
268 if (BGP_DEBUG(zebra
, ZEBRA
))
269 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id
, ifp
->name
);
271 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
272 bgp_connected_add(bgp
, c
);
274 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
275 bgp_nbr_connected_add(bgp
, nc
);
280 static int bgp_interface_down(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
;
290 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
295 ifp
= zebra_interface_state_read(s
, vrf_id
);
299 if (BGP_DEBUG(zebra
, ZEBRA
))
300 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id
, ifp
->name
);
302 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
303 bgp_connected_delete(bgp
, c
);
305 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
306 bgp_nbr_connected_delete(bgp
, nc
, 1);
308 /* Fast external-failover */
312 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
315 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
316 #if defined(HAVE_CUMULUS)
317 /* Take down directly connected EBGP peers as well as
319 * tracked (directly connected) IBGP peers.
321 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1)
323 || bgp_bfd_is_peer_multihop(peer
)))
325 /* Take down directly connected EBGP peers */
326 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
330 if (ifp
== peer
->nexthop
.ifp
) {
331 BGP_EVENT_ADD(peer
, BGP_Stop
);
332 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
340 static int bgp_interface_address_add(int command
, struct zclient
*zclient
,
341 zebra_size_t length
, vrf_id_t vrf_id
)
343 struct connected
*ifc
;
346 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
350 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
355 if (bgp_debug_zebra(ifc
->address
)) {
356 char buf
[PREFIX2STR_BUFFER
];
357 prefix2str(ifc
->address
, buf
, sizeof(buf
));
358 zlog_debug("Rx Intf address add VRF %u IF %s addr %s", vrf_id
,
359 ifc
->ifp
->name
, buf
);
362 if (if_is_operative(ifc
->ifp
)) {
363 bgp_connected_add(bgp
, ifc
);
365 /* If we have learnt of any neighbors on this interface,
366 * check to kick off any BGP interface-based neighbors,
367 * but only if this is a link-local address.
369 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
)
370 && !list_isempty(ifc
->ifp
->nbr_connected
))
371 bgp_start_interface_nbrs(bgp
, ifc
->ifp
);
377 static int bgp_interface_address_delete(int command
, struct zclient
*zclient
,
378 zebra_size_t length
, vrf_id_t vrf_id
)
380 struct connected
*ifc
;
383 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
387 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
392 if (bgp_debug_zebra(ifc
->address
)) {
393 char buf
[PREFIX2STR_BUFFER
];
394 prefix2str(ifc
->address
, buf
, sizeof(buf
));
395 zlog_debug("Rx Intf address del VRF %u IF %s addr %s", vrf_id
,
396 ifc
->ifp
->name
, buf
);
399 if (if_is_operative(ifc
->ifp
)) {
400 bgp_connected_delete(bgp
, ifc
);
408 static int bgp_interface_nbr_address_add(int command
, struct zclient
*zclient
,
409 zebra_size_t length
, vrf_id_t vrf_id
)
411 struct nbr_connected
*ifc
= NULL
;
414 ifc
= zebra_interface_nbr_address_read(command
, zclient
->ibuf
, vrf_id
);
419 if (bgp_debug_zebra(ifc
->address
)) {
420 char buf
[PREFIX2STR_BUFFER
];
421 prefix2str(ifc
->address
, buf
, sizeof(buf
));
422 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s", vrf_id
,
423 ifc
->ifp
->name
, buf
);
426 if (if_is_operative(ifc
->ifp
)) {
427 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
429 bgp_nbr_connected_add(bgp
, ifc
);
435 static int bgp_interface_nbr_address_delete(int command
,
436 struct zclient
*zclient
,
440 struct nbr_connected
*ifc
= NULL
;
443 ifc
= zebra_interface_nbr_address_read(command
, zclient
->ibuf
, vrf_id
);
448 if (bgp_debug_zebra(ifc
->address
)) {
449 char buf
[PREFIX2STR_BUFFER
];
450 prefix2str(ifc
->address
, buf
, sizeof(buf
));
451 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s", vrf_id
,
452 ifc
->ifp
->name
, buf
);
455 if (if_is_operative(ifc
->ifp
)) {
456 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
458 bgp_nbr_connected_delete(bgp
, ifc
, 0);
461 nbr_connected_free(ifc
);
466 /* VRF update for an interface. */
467 static int bgp_interface_vrf_update(int command
, struct zclient
*zclient
,
468 zebra_size_t length
, vrf_id_t vrf_id
)
470 struct interface
*ifp
;
473 struct nbr_connected
*nc
;
474 struct listnode
*node
, *nnode
;
477 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
482 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
483 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id
,
484 ifp
->name
, new_vrf_id
);
486 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
490 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
491 bgp_connected_delete(bgp
, c
);
493 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
494 bgp_nbr_connected_delete(bgp
, nc
, 1);
496 /* Fast external-failover */
500 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
503 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
504 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
507 if (ifp
== peer
->nexthop
.ifp
)
508 BGP_EVENT_ADD(peer
, BGP_Stop
);
512 if_update_to_new_vrf(ifp
, new_vrf_id
);
514 bgp
= bgp_lookup_by_vrf_id(new_vrf_id
);
518 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
519 bgp_connected_add(bgp
, c
);
521 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
522 bgp_nbr_connected_add(bgp
, nc
);
526 /* Zebra route add and delete treatment. */
527 static int zebra_read_route(int command
, struct zclient
*zclient
,
528 zebra_size_t length
, vrf_id_t vrf_id
)
530 enum nexthop_types_t nhtype
;
531 struct zapi_route api
;
532 union g_addr nexthop
;
537 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
541 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
544 /* we completely ignore srcdest routes for now. */
545 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
548 /* ignore link-local address. */
549 if (api
.prefix
.family
== AF_INET6
550 && IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
553 nexthop
= api
.nexthops
[0].gate
;
554 ifindex
= api
.nexthops
[0].ifindex
;
555 nhtype
= api
.nexthops
[0].type
;
557 add
= (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
560 * The ADD message is actually an UPDATE and there is no
562 * for a prior redistributed route, if any. So, perform an
564 * DEL processing for the same redistributed route from any
568 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
570 bgp_redistribute_delete(bgp
, &api
.prefix
, i
,
574 /* Now perform the add/update. */
575 bgp_redistribute_add(bgp
, &api
.prefix
, &nexthop
, ifindex
,
576 nhtype
, api
.metric
, api
.type
, api
.instance
,
579 bgp_redistribute_delete(bgp
, &api
.prefix
, api
.type
,
583 if (bgp_debug_zebra(&api
.prefix
)) {
584 char buf
[2][PREFIX_STRLEN
];
586 prefix2str(&api
.prefix
, buf
[0], sizeof(buf
[0]));
588 inet_ntop(api
.prefix
.family
, &nexthop
, buf
[1],
591 "Rx route ADD VRF %u %s[%d] %s nexthop %s (type %d if %u) metric %u tag %" ROUTE_TAG_PRI
,
592 vrf_id
, zebra_route_string(api
.type
),
593 api
.instance
, buf
[0], buf
[1], nhtype
,
594 ifindex
, api
.metric
, api
.tag
);
597 "Rx route DEL VRF %u %s[%d] %s",
598 vrf_id
, zebra_route_string(api
.type
),
599 api
.instance
, buf
[0]);
606 struct interface
*if_lookup_by_ipv4(struct in_addr
*addr
, vrf_id_t vrf_id
)
609 struct listnode
*cnode
;
610 struct interface
*ifp
;
611 struct connected
*connected
;
612 struct prefix_ipv4 p
;
615 vrf
= vrf_lookup_by_id(vrf_id
);
621 p
.prefixlen
= IPV4_MAX_BITLEN
;
623 FOR_ALL_INTERFACES (vrf
, ifp
) {
624 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
625 cp
= connected
->address
;
627 if (cp
->family
== AF_INET
)
628 if (prefix_match(cp
, (struct prefix
*)&p
))
635 struct interface
*if_lookup_by_ipv4_exact(struct in_addr
*addr
, vrf_id_t vrf_id
)
638 struct listnode
*cnode
;
639 struct interface
*ifp
;
640 struct connected
*connected
;
643 vrf
= vrf_lookup_by_id(vrf_id
);
647 FOR_ALL_INTERFACES (vrf
, ifp
) {
648 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
649 cp
= connected
->address
;
651 if (cp
->family
== AF_INET
)
652 if (IPV4_ADDR_SAME(&cp
->u
.prefix4
, addr
))
659 struct interface
*if_lookup_by_ipv6(struct in6_addr
*addr
, ifindex_t ifindex
,
663 struct listnode
*cnode
;
664 struct interface
*ifp
;
665 struct connected
*connected
;
666 struct prefix_ipv6 p
;
669 vrf
= vrf_lookup_by_id(vrf_id
);
675 p
.prefixlen
= IPV6_MAX_BITLEN
;
677 FOR_ALL_INTERFACES (vrf
, ifp
) {
678 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
679 cp
= connected
->address
;
681 if (cp
->family
== AF_INET6
)
682 if (prefix_match(cp
, (struct prefix
*)&p
)) {
683 if (IN6_IS_ADDR_LINKLOCAL(
685 if (ifindex
== ifp
->ifindex
)
695 struct interface
*if_lookup_by_ipv6_exact(struct in6_addr
*addr
,
696 ifindex_t ifindex
, vrf_id_t vrf_id
)
699 struct listnode
*cnode
;
700 struct interface
*ifp
;
701 struct connected
*connected
;
704 vrf
= vrf_lookup_by_id(vrf_id
);
708 FOR_ALL_INTERFACES (vrf
, ifp
) {
709 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
710 cp
= connected
->address
;
712 if (cp
->family
== AF_INET6
)
713 if (IPV6_ADDR_SAME(&cp
->u
.prefix6
, addr
)) {
714 if (IN6_IS_ADDR_LINKLOCAL(
716 if (ifindex
== ifp
->ifindex
)
726 static int if_get_ipv6_global(struct interface
*ifp
, struct in6_addr
*addr
)
728 struct listnode
*cnode
;
729 struct connected
*connected
;
732 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
733 cp
= connected
->address
;
735 if (cp
->family
== AF_INET6
)
736 if (!IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
737 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
744 static int if_get_ipv6_local(struct interface
*ifp
, struct in6_addr
*addr
)
746 struct listnode
*cnode
;
747 struct connected
*connected
;
750 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
751 cp
= connected
->address
;
753 if (cp
->family
== AF_INET6
)
754 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
755 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
762 static int if_get_ipv4_address(struct interface
*ifp
, struct in_addr
*addr
)
764 struct listnode
*cnode
;
765 struct connected
*connected
;
768 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
769 cp
= connected
->address
;
770 if ((cp
->family
== AF_INET
)
771 && !ipv4_martian(&(cp
->u
.prefix4
))) {
772 *addr
= cp
->u
.prefix4
;
779 int bgp_nexthop_set(union sockunion
*local
, union sockunion
*remote
,
780 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
783 struct interface
*ifp
= NULL
;
785 memset(nexthop
, 0, sizeof(struct bgp_nexthop
));
792 if (local
->sa
.sa_family
== AF_INET
) {
793 nexthop
->v4
= local
->sin
.sin_addr
;
795 ifp
= if_lookup_by_name(peer
->update_if
,
798 ifp
= if_lookup_by_ipv4_exact(&local
->sin
.sin_addr
,
801 if (local
->sa
.sa_family
== AF_INET6
) {
802 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
803 if (peer
->conf_if
|| peer
->ifname
)
804 ifp
= if_lookup_by_name(peer
->conf_if
808 } else if (peer
->update_if
)
809 ifp
= if_lookup_by_name(peer
->update_if
,
812 ifp
= if_lookup_by_ipv6_exact(&local
->sin6
.sin6_addr
,
813 local
->sin6
.sin6_scope_id
,
822 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
823 if (local
->sa
.sa_family
== AF_INET
) {
825 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
828 /* There is no global nexthop. Use link-local address as
830 * global and link-local nexthop. In this scenario, the
832 * for interop is that the network admin would use a
834 * specify the global IPv6 nexthop.
836 if_get_ipv6_local(ifp
, &nexthop
->v6_global
);
837 memcpy(&nexthop
->v6_local
, &nexthop
->v6_global
,
840 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
842 if (if_lookup_by_ipv4(&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
843 peer
->shared_network
= 1;
845 peer
->shared_network
= 0;
848 /* IPv6 connection, fetch and store IPv4 local address if any. */
849 if (local
->sa
.sa_family
== AF_INET6
) {
850 struct interface
*direct
= NULL
;
853 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
854 if (!ret
&& peer
->local_id
.s_addr
)
855 nexthop
->v4
= peer
->local_id
;
858 if (!IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
859 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
862 /* If directory connected set link-local address. */
863 direct
= if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
864 remote
->sin6
.sin6_scope_id
,
867 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
869 /* Link-local address. */
871 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
873 /* If there is no global address. Set link-local
875 global. I know this break RFC specification... */
876 /* In this scenario, the expectation for interop is that
878 * network admin would use a route-map to specify the
883 memcpy(&nexthop
->v6_global
,
884 &local
->sin6
.sin6_addr
,
886 /* Always set the link-local address */
887 memcpy(&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
891 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)
892 || if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
893 remote
->sin6
.sin6_scope_id
,
895 peer
->shared_network
= 1;
897 peer
->shared_network
= 0;
900 /* KAME stack specific treatment. */
902 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_global
)
903 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
)) {
904 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
, 0);
906 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_local
)
907 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
)) {
908 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
, 0);
912 /* If we have identified the local interface, there is no error for now.
917 static struct in6_addr
*bgp_info_to_ipv6_nexthop(struct bgp_info
*info
,
920 struct in6_addr
*nexthop
= NULL
;
922 /* Only global address nexthop exists. */
923 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
) {
924 nexthop
= &info
->attr
->mp_nexthop_global
;
925 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
926 *ifindex
= info
->attr
->nh_ifindex
;
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
;
935 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
936 *ifindex
= info
->attr
->nh_ifindex
;
938 /* Workaround for Cisco's nexthop bug. */
939 if (IN6_IS_ADDR_UNSPECIFIED(
940 &info
->attr
->mp_nexthop_global
)
941 && info
->peer
->su_remote
->sa
.sa_family
944 &info
->peer
->su_remote
->sin6
.sin6_addr
;
945 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
946 *ifindex
= info
->peer
->nexthop
.ifp
949 nexthop
= &info
->attr
->mp_nexthop_local
;
950 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
951 *ifindex
= info
->attr
->nh_lla_ifindex
;
959 static int bgp_table_map_apply(struct route_map
*map
, struct prefix
*p
,
960 struct bgp_info
*info
)
962 route_map_result_t ret
;
964 ret
= route_map_apply(map
, p
, RMAP_BGP
, info
);
965 bgp_attr_flush(info
->attr
);
967 if (ret
!= RMAP_DENYMATCH
)
970 if (bgp_debug_zebra(p
)) {
971 if (p
->family
== AF_INET
) {
972 char buf
[2][INET_ADDRSTRLEN
];
974 "Zebra rmap deny: IPv4 route %s/%d nexthop %s",
975 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0],
978 inet_ntop(AF_INET
, &info
->attr
->nexthop
, buf
[1],
981 if (p
->family
== AF_INET6
) {
982 char buf
[2][INET6_ADDRSTRLEN
];
984 struct in6_addr
*nexthop
;
986 nexthop
= bgp_info_to_ipv6_nexthop(info
, &ifindex
);
988 "Zebra rmap deny: IPv6 route %s/%d nexthop %s",
989 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0],
992 inet_ntop(AF_INET6
, nexthop
,
993 buf
[1], sizeof(buf
[1])));
999 static struct thread
*bgp_tm_thread_connect
;
1000 static bool bgp_tm_status_connected
;
1001 static bool bgp_tm_chunk_obtained
;
1002 #define BGP_FLOWSPEC_TABLE_CHUNK 100000
1003 static uint32_t bgp_tm_min
, bgp_tm_max
, bgp_tm_chunk_size
;
1005 static int bgp_zebra_tm_connect(struct thread
*t
)
1007 struct zclient
*zclient
;
1008 int delay
= 10, ret
= 0;
1010 zclient
= THREAD_ARG(t
);
1011 if (bgp_tm_status_connected
&& zclient
->sock
> 0)
1014 bgp_tm_status_connected
= false;
1015 ret
= tm_table_manager_connect(zclient
);
1018 zlog_warn("Error connecting to table manager!");
1019 bgp_tm_status_connected
= false;
1021 if (!bgp_tm_status_connected
)
1022 zlog_debug("Connecting to table manager. Success");
1023 bgp_tm_status_connected
= true;
1024 if (!bgp_tm_chunk_obtained
) {
1025 if (bgp_zebra_get_table_range(bgp_tm_chunk_size
,
1028 bgp_tm_chunk_obtained
= true;
1031 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1032 &bgp_tm_thread_connect
);
1036 uint32_t bgp_zebra_tm_get_id(void)
1038 static int table_id
;
1040 if (!bgp_tm_chunk_obtained
)
1042 return bgp_tm_min
++;
1045 void bgp_zebra_init_tm_connect(void)
1049 /* if already set, do nothing
1051 if (bgp_tm_thread_connect
!= NULL
)
1053 bgp_tm_status_connected
= false;
1054 bgp_tm_chunk_obtained
= false;
1055 bgp_tm_min
= bgp_tm_max
= 0;
1056 bgp_tm_chunk_size
= BGP_FLOWSPEC_TABLE_CHUNK
;
1057 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1058 &bgp_tm_thread_connect
);
1061 int bgp_zebra_get_table_range(uint32_t chunk_size
,
1062 uint32_t *start
, uint32_t *end
)
1066 if (!bgp_tm_status_connected
)
1068 ret
= tm_get_table_chunk(zclient
, chunk_size
, start
, end
);
1070 zlog_err("BGP: Error getting table chunk %u", chunk_size
);
1073 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1074 chunk_size
, *start
, *end
);
1078 static int update_ipv4nh_for_route_install(int nh_othervrf
,
1079 struct in_addr
*nexthop
,
1082 struct zapi_nexthop
*api_nh
)
1084 api_nh
->gate
.ipv4
= *nexthop
;
1086 /* Need to set fields appropriately for EVPN routes imported into
1087 * a VRF (which are programmed as onlink on l3-vni SVI) as well as
1088 * connected routes leaked into a VRF.
1091 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1092 else if (nh_othervrf
&&
1093 api_nh
->gate
.ipv4
.s_addr
== INADDR_ANY
) {
1094 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1095 api_nh
->ifindex
= attr
->nh_ifindex
;
1097 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1102 static int update_ipv6nh_for_route_install(int nh_othervrf
,
1103 struct in6_addr
*nexthop
,
1105 struct bgp_info
*ri
,
1106 struct bgp_info
*best_ri
,
1108 struct zapi_nexthop
*api_nh
)
1115 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1116 else if (nh_othervrf
) {
1117 if (IN6_IS_ADDR_UNSPECIFIED(nexthop
)) {
1118 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1119 api_nh
->ifindex
= attr
->nh_ifindex
;
1120 } else if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1123 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1124 api_nh
->ifindex
= ifindex
;
1126 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1127 api_nh
->ifindex
= 0;
1130 if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1131 if (ri
== best_ri
&&
1132 attr
->mp_nexthop_len
1133 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1134 if (ri
->peer
->nexthop
.ifp
)
1135 ifindex
= ri
->peer
->nexthop
.ifp
1138 if (ri
->peer
->conf_if
)
1139 ifindex
= ri
->peer
->ifp
->ifindex
;
1140 else if (ri
->peer
->ifname
)
1141 ifindex
= ifname2ifindex(
1143 ri
->peer
->bgp
->vrf_id
);
1144 else if (ri
->peer
->nexthop
.ifp
)
1145 ifindex
= ri
->peer
->nexthop
.ifp
1151 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1152 api_nh
->ifindex
= ifindex
;
1154 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1155 api_nh
->ifindex
= 0;
1158 api_nh
->gate
.ipv6
= *nexthop
;
1163 void bgp_zebra_announce(struct bgp_node
*rn
, struct prefix
*p
,
1164 struct bgp_info
*info
, struct bgp
*bgp
, afi_t afi
,
1167 struct zapi_route api
;
1168 struct zapi_nexthop
*api_nh
;
1170 unsigned int valid_nh_count
= 0;
1171 int has_valid_label
= 0;
1174 struct bgp_info
*mpinfo
;
1176 struct attr local_attr
;
1177 struct bgp_info local_info
;
1178 struct bgp_info
*mpinfo_cp
= &local_info
;
1181 int nh_othervrf
= 0;
1182 char buf_prefix
[PREFIX_STRLEN
]; /* filled in if we are debugging */
1183 bool is_evpn
= false;
1186 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1187 * know of this instance.
1189 if (!bgp_install_info_to_zebra(bgp
))
1192 if (bgp
->main_zebra_update_hold
)
1195 if (bgp_debug_zebra(p
))
1196 prefix2str(p
, buf_prefix
, sizeof(buf_prefix
));
1198 if (safi
== SAFI_FLOWSPEC
)
1199 return bgp_pbr_update_entry(bgp
, &rn
->p
,
1200 info
, afi
, safi
, true);
1203 * vrf leaking support (will have only one nexthop)
1205 if (info
->extra
&& info
->extra
->bgp_orig
)
1208 /* Make Zebra API structure. */
1209 memset(&api
, 0, sizeof(api
));
1210 api
.vrf_id
= bgp
->vrf_id
;
1211 api
.type
= ZEBRA_ROUTE_BGP
;
1214 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1218 if (info
->type
== ZEBRA_ROUTE_BGP
1219 && info
->sub_type
== BGP_ROUTE_IMPORTED
) {
1221 /* Obtain peer from parent */
1222 if (info
->extra
&& info
->extra
->parent
)
1223 peer
= ((struct bgp_info
*)(info
->extra
->parent
))->peer
;
1226 tag
= info
->attr
->tag
;
1229 * When we create an aggregate route we must also install a
1230 * Null0 route in the RIB
1232 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1233 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
1235 /* If the route's source is EVPN, flag as such. */
1236 is_evpn
= is_route_parent_evpn(info
);
1238 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1240 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
1241 || info
->sub_type
== BGP_ROUTE_AGGREGATE
) {
1242 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1243 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1246 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1247 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1248 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1250 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1252 /* Metric is currently based on the best-path only */
1253 metric
= info
->attr
->med
;
1254 for (mpinfo
= info
; mpinfo
; mpinfo
= bgp_info_mpath_next(mpinfo
)) {
1255 if (valid_nh_count
>= multipath_num
)
1258 *mpinfo_cp
= *mpinfo
;
1260 /* Get nexthop address-family */
1261 if (p
->family
== AF_INET
1262 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
))
1263 nh_family
= AF_INET
;
1264 else if (p
->family
== AF_INET6
1265 || (p
->family
== AF_INET
1266 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
)))
1267 nh_family
= AF_INET6
;
1271 api_nh
= &api
.nexthops
[valid_nh_count
];
1272 api_nh
->vrf_id
= nh_othervrf
? info
->extra
->bgp_orig
->vrf_id
1274 if (nh_family
== AF_INET
) {
1275 if (bgp_debug_zebra(&api
.prefix
)) {
1276 if (mpinfo
->extra
) {
1278 "%s: p=%s, bgp_is_valid_label: %d",
1279 __func__
, buf_prefix
,
1285 "%s: p=%s, extra is NULL, no label",
1286 __func__
, buf_prefix
);
1290 if (bgp
->table_map
[afi
][safi
].name
) {
1291 /* Copy info and attributes, so the route-map
1292 apply doesn't modify the BGP route info. */
1293 local_attr
= *mpinfo
->attr
;
1294 mpinfo_cp
->attr
= &local_attr
;
1297 if (bgp
->table_map
[afi
][safi
].name
) {
1298 if (!bgp_table_map_apply(
1299 bgp
->table_map
[afi
][safi
].map
, p
,
1303 /* metric/tag is only allowed to be
1304 * overridden on 1st nexthop */
1305 if (mpinfo
== info
) {
1306 metric
= mpinfo_cp
->attr
->med
;
1307 tag
= mpinfo_cp
->attr
->tag
;
1311 nh_updated
= update_ipv4nh_for_route_install(
1313 &mpinfo_cp
->attr
->nexthop
,
1314 mpinfo_cp
->attr
, is_evpn
, api_nh
);
1317 struct in6_addr
*nexthop
;
1319 if (bgp
->table_map
[afi
][safi
].name
) {
1320 /* Copy info and attributes, so the route-map
1321 apply doesn't modify the BGP route info. */
1322 local_attr
= *mpinfo
->attr
;
1323 mpinfo_cp
->attr
= &local_attr
;
1326 if (bgp
->table_map
[afi
][safi
].name
) {
1327 /* Copy info and attributes, so the route-map
1328 apply doesn't modify the BGP route info. */
1329 local_attr
= *mpinfo
->attr
;
1330 mpinfo_cp
->attr
= &local_attr
;
1332 if (!bgp_table_map_apply(
1333 bgp
->table_map
[afi
][safi
].map
, p
,
1337 /* metric/tag is only allowed to be
1338 * overridden on 1st nexthop */
1339 if (mpinfo
== info
) {
1340 metric
= mpinfo_cp
->attr
->med
;
1341 tag
= mpinfo_cp
->attr
->tag
;
1344 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo_cp
,
1346 nh_updated
= update_ipv6nh_for_route_install(
1347 nh_othervrf
, nexthop
, ifindex
,
1348 mpinfo
, info
, is_evpn
, api_nh
);
1351 /* Did we get proper nexthop info to update zebra? */
1356 && bgp_is_valid_label(&mpinfo
->extra
->label
[0])
1357 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1358 has_valid_label
= 1;
1359 label
= label_pton(&mpinfo
->extra
->label
[0]);
1361 api_nh
->label_num
= 1;
1362 api_nh
->labels
[0] = label
;
1364 memcpy(&api_nh
->rmac
, &(mpinfo
->attr
->rmac
),
1365 sizeof(struct ethaddr
));
1370 /* if this is a evpn route we don't have to include the label */
1371 if (has_valid_label
&& !(CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)))
1372 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
1374 if (info
->sub_type
!= BGP_ROUTE_AGGREGATE
)
1375 api
.nexthop_num
= valid_nh_count
;
1377 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1378 api
.metric
= metric
;
1381 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1385 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1387 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1388 api
.distance
= distance
;
1391 if (bgp_debug_zebra(p
)) {
1392 char prefix_buf
[PREFIX_STRLEN
];
1393 char nh_buf
[INET6_ADDRSTRLEN
];
1397 prefix2str(&api
.prefix
, prefix_buf
, sizeof(prefix_buf
));
1398 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1400 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1401 prefix_buf
, api
.metric
, api
.tag
, api
.nexthop_num
);
1402 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1403 api_nh
= &api
.nexthops
[i
];
1405 if (api_nh
->type
== NEXTHOP_TYPE_IFINDEX
)
1408 if (api_nh
->type
== NEXTHOP_TYPE_IPV4
)
1409 nh_family
= AF_INET
;
1411 nh_family
= AF_INET6
;
1412 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1416 label_buf
[0] = '\0';
1418 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1419 sprintf(label_buf
, "label %u",
1421 zlog_debug(" nhop [%d]: %s if %u VRF %u %s",
1422 i
+ 1, nh_buf
, api_nh
->ifindex
,
1423 api_nh
->vrf_id
, label_buf
);
1427 if (bgp_debug_zebra(p
)) {
1428 int recursion_flag
= 0;
1430 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
))
1433 zlog_debug("%s: %s: announcing to zebra (recursion %sset)",
1434 __func__
, buf_prefix
,
1435 (recursion_flag
? "" : "NOT "));
1437 zclient_route_send(valid_nh_count
? ZEBRA_ROUTE_ADD
1438 : ZEBRA_ROUTE_DELETE
,
1442 /* Announce all routes of a table to zebra */
1443 void bgp_zebra_announce_table(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1445 struct bgp_node
*rn
;
1446 struct bgp_table
*table
;
1447 struct bgp_info
*ri
;
1449 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1450 * know of this instance.
1452 if (!bgp_install_info_to_zebra(bgp
))
1455 table
= bgp
->rib
[afi
][safi
];
1459 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
1460 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1461 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
) &&
1463 (ri
->type
== ZEBRA_ROUTE_BGP
1464 && (ri
->sub_type
== BGP_ROUTE_NORMAL
1465 || ri
->sub_type
== BGP_ROUTE_IMPORTED
)))
1467 bgp_zebra_announce(rn
, &rn
->p
, ri
, bgp
, afi
,
1471 void bgp_zebra_withdraw(struct prefix
*p
, struct bgp_info
*info
,
1472 struct bgp
*bgp
, safi_t safi
)
1474 struct zapi_route api
;
1477 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1478 * know of this instance.
1480 if (!bgp_install_info_to_zebra(bgp
))
1483 if (safi
== SAFI_FLOWSPEC
) {
1485 return bgp_pbr_update_entry(peer
->bgp
, p
,
1486 info
, AFI_IP
, safi
, false);
1489 memset(&api
, 0, sizeof(api
));
1490 api
.vrf_id
= bgp
->vrf_id
;
1491 api
.type
= ZEBRA_ROUTE_BGP
;
1495 /* If the route's source is EVPN, flag as such. */
1496 if (is_route_parent_evpn(info
))
1497 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1499 if (bgp_debug_zebra(p
)) {
1500 char buf
[PREFIX_STRLEN
];
1502 prefix2str(&api
.prefix
, buf
, sizeof(buf
));
1503 zlog_debug("Tx route delete VRF %u %s", bgp
->vrf_id
, buf
);
1506 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
1509 struct bgp_redist
*bgp_redist_lookup(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1510 unsigned short instance
)
1512 struct list
*red_list
;
1513 struct listnode
*node
;
1514 struct bgp_redist
*red
;
1516 red_list
= bgp
->redist
[afi
][type
];
1520 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1521 if (red
->instance
== instance
)
1527 struct bgp_redist
*bgp_redist_add(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1528 unsigned short instance
)
1530 struct list
*red_list
;
1531 struct bgp_redist
*red
;
1533 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1537 if (!bgp
->redist
[afi
][type
])
1538 bgp
->redist
[afi
][type
] = list_new();
1540 red_list
= bgp
->redist
[afi
][type
];
1541 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
,
1542 sizeof(struct bgp_redist
));
1543 red
->instance
= instance
;
1545 listnode_add(red_list
, red
);
1550 static void bgp_redist_del(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1551 unsigned short instance
)
1553 struct bgp_redist
*red
;
1555 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1558 listnode_delete(bgp
->redist
[afi
][type
], red
);
1559 XFREE(MTYPE_BGP_REDIST
, red
);
1560 if (!bgp
->redist
[afi
][type
]->count
)
1561 list_delete_and_null(&bgp
->redist
[afi
][type
]);
1565 /* Other routes redistribution into BGP. */
1566 int bgp_redistribute_set(struct bgp
*bgp
, afi_t afi
, int type
,
1567 unsigned short instance
)
1570 /* Return if already redistribute flag is set. */
1572 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1576 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1578 if (vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1582 if (bgp
->vrf_id
== VRF_DEFAULT
1583 && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1584 vnc_export_bgp_enable(
1585 bgp
, afi
); /* only enables if mode bits cfg'd */
1589 vrf_bitmap_set(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1593 * Don't try to register if we're not connected to Zebra or Zebra
1594 * doesn't know of this instance.
1596 * When we come up later well resend if needed.
1598 if (!bgp_install_info_to_zebra(bgp
))
1601 if (BGP_DEBUG(zebra
, ZEBRA
))
1602 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1603 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1606 /* Send distribute add message to zebra. */
1607 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1608 instance
, bgp
->vrf_id
);
1613 int bgp_redistribute_resend(struct bgp
*bgp
, afi_t afi
, int type
,
1614 unsigned short instance
)
1616 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1617 * know of this instance.
1619 if (!bgp_install_info_to_zebra(bgp
))
1622 if (BGP_DEBUG(zebra
, ZEBRA
))
1623 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1624 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1627 /* Send distribute add message to zebra. */
1628 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1629 instance
, bgp
->vrf_id
);
1630 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1631 instance
, bgp
->vrf_id
);
1636 /* Redistribute with route-map specification. */
1637 int bgp_redistribute_rmap_set(struct bgp_redist
*red
, const char *name
)
1639 if (red
->rmap
.name
&& (strcmp(red
->rmap
.name
, name
) == 0))
1643 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1644 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1645 red
->rmap
.map
= route_map_lookup_by_name(name
);
1650 /* Redistribute with metric specification. */
1651 int bgp_redistribute_metric_set(struct bgp
*bgp
, struct bgp_redist
*red
,
1652 afi_t afi
, int type
, uint32_t metric
)
1654 struct bgp_node
*rn
;
1655 struct bgp_info
*ri
;
1657 if (red
->redist_metric_flag
&& red
->redist_metric
== metric
)
1660 red
->redist_metric_flag
= 1;
1661 red
->redist_metric
= metric
;
1663 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
1664 rn
= bgp_route_next(rn
)) {
1665 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
1666 if (ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
1668 && ri
->instance
== red
->instance
) {
1669 struct attr
*old_attr
;
1670 struct attr new_attr
;
1672 bgp_attr_dup(&new_attr
, ri
->attr
);
1673 new_attr
.med
= red
->redist_metric
;
1674 old_attr
= ri
->attr
;
1675 ri
->attr
= bgp_attr_intern(&new_attr
);
1676 bgp_attr_unintern(&old_attr
);
1678 bgp_info_set_flag(rn
, ri
,
1679 BGP_INFO_ATTR_CHANGED
);
1680 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1688 /* Unset redistribution. */
1689 int bgp_redistribute_unreg(struct bgp
*bgp
, afi_t afi
, int type
,
1690 unsigned short instance
)
1692 struct bgp_redist
*red
;
1694 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1698 /* Return if zebra connection is disabled. */
1700 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1703 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1705 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1707 vrf_bitmap_unset(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1711 if (bgp_install_info_to_zebra(bgp
)) {
1712 /* Send distribute delete message to zebra. */
1713 if (BGP_DEBUG(zebra
, ZEBRA
))
1714 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1715 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1717 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
1718 type
, instance
, bgp
->vrf_id
);
1721 /* Withdraw redistributed routes from current BGP's routing table. */
1722 bgp_redistribute_withdraw(bgp
, afi
, type
, instance
);
1727 /* Unset redistribution. */
1728 int bgp_redistribute_unset(struct bgp
*bgp
, afi_t afi
, int type
,
1729 unsigned short instance
)
1731 struct bgp_redist
*red
;
1734 * vnc and vpn->vrf checks must be before red check because
1735 * they operate within bgpd irrespective of zebra connection
1736 * status. red lookup fails if there is no zebra connection.
1739 if (bgp
->vrf_id
== VRF_DEFAULT
&& type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1740 vnc_export_bgp_disable(bgp
, afi
);
1744 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1748 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1750 /* Unset route-map. */
1752 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1753 red
->rmap
.name
= NULL
;
1754 red
->rmap
.map
= NULL
;
1757 red
->redist_metric_flag
= 0;
1758 red
->redist_metric
= 0;
1760 bgp_redist_del(bgp
, afi
, type
, instance
);
1765 /* Update redistribute vrf bitmap during triggers like
1766 restart networking or delete/add VRFs */
1767 void bgp_update_redist_vrf_bitmaps(struct bgp
*bgp
, vrf_id_t old_vrf_id
)
1772 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1773 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1774 if ((old_vrf_id
== VRF_UNKNOWN
)
1775 || vrf_bitmap_check(zclient
->redist
[afi
][i
],
1777 vrf_bitmap_unset(zclient
->redist
[afi
][i
],
1779 vrf_bitmap_set(zclient
->redist
[afi
][i
],
1785 void bgp_zclient_reset(void)
1787 zclient_reset(zclient
);
1790 /* Register this instance with Zebra. Invoked upon connect (for
1791 * default instance) and when other VRFs are learnt (or created and
1794 void bgp_zebra_instance_register(struct bgp
*bgp
)
1796 /* Don't try to register if we're not connected to Zebra */
1797 if (!zclient
|| zclient
->sock
< 0)
1800 if (BGP_DEBUG(zebra
, ZEBRA
))
1801 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
1803 /* Register for router-id, interfaces, redistributed routes. */
1804 zclient_send_reg_requests(zclient
, bgp
->vrf_id
);
1806 /* For default instance, register to learn about VNIs, if appropriate.
1808 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& is_evpn_enabled())
1809 bgp_zebra_advertise_all_vni(bgp
, 1);
1812 /* Deregister this instance with Zebra. Invoked upon the instance
1813 * being deleted (default or VRF) and it is already registered.
1815 void bgp_zebra_instance_deregister(struct bgp
*bgp
)
1817 /* Don't try to deregister if we're not connected to Zebra */
1818 if (zclient
->sock
< 0)
1821 if (BGP_DEBUG(zebra
, ZEBRA
))
1822 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
1824 /* For default instance, unregister learning about VNIs, if appropriate.
1826 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& is_evpn_enabled())
1827 bgp_zebra_advertise_all_vni(bgp
, 0);
1829 /* Deregister for router-id, interfaces, redistributed routes. */
1830 zclient_send_dereg_requests(zclient
, bgp
->vrf_id
);
1833 void bgp_zebra_initiate_radv(struct bgp
*bgp
, struct peer
*peer
)
1835 int ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
1837 /* Don't try to initiate if we're not connected to Zebra */
1838 if (zclient
->sock
< 0)
1841 if (BGP_DEBUG(zebra
, ZEBRA
))
1842 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
,
1845 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 1,
1849 void bgp_zebra_terminate_radv(struct bgp
*bgp
, struct peer
*peer
)
1851 /* Don't try to terminate if we're not connected to Zebra */
1852 if (zclient
->sock
< 0)
1855 if (BGP_DEBUG(zebra
, ZEBRA
))
1856 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
,
1859 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 0, 0);
1862 int bgp_zebra_advertise_subnet(struct bgp
*bgp
, int advertise
, vni_t vni
)
1864 struct stream
*s
= NULL
;
1867 if (!zclient
|| zclient
->sock
< 0)
1870 /* Don't try to register if Zebra doesn't know of this instance. */
1871 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1877 zclient_create_header(s
, ZEBRA_ADVERTISE_SUBNET
, bgp
->vrf_id
);
1878 stream_putc(s
, advertise
);
1879 stream_put3(s
, vni
);
1880 stream_putw_at(s
, 0, stream_get_endp(s
));
1882 return zclient_send_message(zclient
);
1885 int bgp_zebra_advertise_gw_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
1887 struct stream
*s
= NULL
;
1890 if (!zclient
|| zclient
->sock
< 0)
1893 /* Don't try to register if Zebra doesn't know of this instance. */
1894 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1900 zclient_create_header(s
, ZEBRA_ADVERTISE_DEFAULT_GW
, bgp
->vrf_id
);
1901 stream_putc(s
, advertise
);
1902 stream_putl(s
, vni
);
1903 stream_putw_at(s
, 0, stream_get_endp(s
));
1905 return zclient_send_message(zclient
);
1908 int bgp_zebra_advertise_all_vni(struct bgp
*bgp
, int advertise
)
1913 if (!zclient
|| zclient
->sock
< 0)
1916 /* Don't try to register if Zebra doesn't know of this instance. */
1917 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1923 zclient_create_header(s
, ZEBRA_ADVERTISE_ALL_VNI
, bgp
->vrf_id
);
1924 stream_putc(s
, advertise
);
1925 stream_putw_at(s
, 0, stream_get_endp(s
));
1927 return zclient_send_message(zclient
);
1930 static int rule_notify_owner(int command
, struct zclient
*zclient
,
1931 zebra_size_t length
, vrf_id_t vrf_id
)
1933 uint32_t seqno
, priority
, unique
;
1934 enum zapi_rule_notify_owner note
;
1935 struct bgp_pbr_action
*bgp_pbra
;
1938 if (!zapi_rule_notify_decode(zclient
->ibuf
, &seqno
, &priority
, &unique
,
1942 bgp_pbra
= bgp_pbr_action_rule_lookup(vrf_id
, unique
);
1944 if (BGP_DEBUG(zebra
, ZEBRA
))
1945 zlog_debug("%s: Fail to look BGP rule (%u)",
1946 __PRETTY_FUNCTION__
, unique
);
1951 case ZAPI_RULE_FAIL_INSTALL
:
1952 if (BGP_DEBUG(zebra
, ZEBRA
))
1953 zlog_debug("%s: Received RULE_FAIL_INSTALL",
1954 __PRETTY_FUNCTION__
);
1955 bgp_pbra
->installed
= false;
1956 bgp_pbra
->install_in_progress
= false;
1958 case ZAPI_RULE_INSTALLED
:
1959 bgp_pbra
->installed
= true;
1960 bgp_pbra
->install_in_progress
= false;
1961 if (BGP_DEBUG(zebra
, ZEBRA
))
1962 zlog_debug("%s: Received RULE_INSTALLED",
1963 __PRETTY_FUNCTION__
);
1965 case ZAPI_RULE_REMOVED
:
1966 if (BGP_DEBUG(zebra
, ZEBRA
))
1967 zlog_debug("%s: Received RULE REMOVED",
1968 __PRETTY_FUNCTION__
);
1975 static int ipset_notify_owner(int command
, struct zclient
*zclient
,
1976 zebra_size_t length
, vrf_id_t vrf_id
)
1979 enum zapi_ipset_notify_owner note
;
1980 struct bgp_pbr_match
*bgp_pbim
;
1982 if (!zapi_ipset_notify_decode(zclient
->ibuf
,
1987 bgp_pbim
= bgp_pbr_match_ipset_lookup(vrf_id
, unique
);
1989 if (BGP_DEBUG(zebra
, ZEBRA
))
1990 zlog_debug("%s: Fail to look BGP match (%u)",
1991 __PRETTY_FUNCTION__
, unique
);
1996 case ZAPI_IPSET_FAIL_INSTALL
:
1997 if (BGP_DEBUG(zebra
, ZEBRA
))
1998 zlog_debug("%s: Received IPSET_FAIL_INSTALL",
1999 __PRETTY_FUNCTION__
);
2000 bgp_pbim
->installed
= false;
2001 bgp_pbim
->install_in_progress
= false;
2003 case ZAPI_IPSET_INSTALLED
:
2004 bgp_pbim
->installed
= true;
2005 bgp_pbim
->install_in_progress
= false;
2006 if (BGP_DEBUG(zebra
, ZEBRA
))
2007 zlog_debug("%s: Received IPSET_INSTALLED",
2008 __PRETTY_FUNCTION__
);
2010 case ZAPI_IPSET_REMOVED
:
2011 if (BGP_DEBUG(zebra
, ZEBRA
))
2012 zlog_debug("%s: Received IPSET REMOVED",
2013 __PRETTY_FUNCTION__
);
2020 static int ipset_entry_notify_owner(int command
, struct zclient
*zclient
,
2021 zebra_size_t length
, vrf_id_t vrf_id
)
2024 char ipset_name
[ZEBRA_IPSET_NAME_SIZE
];
2025 enum zapi_ipset_entry_notify_owner note
;
2026 struct bgp_pbr_match_entry
*bgp_pbime
;
2028 if (!zapi_ipset_entry_notify_decode(
2034 bgp_pbime
= bgp_pbr_match_ipset_entry_lookup(vrf_id
,
2038 if (BGP_DEBUG(zebra
, ZEBRA
))
2039 zlog_debug("%s: Fail to look BGP match entry (%u)",
2040 __PRETTY_FUNCTION__
, unique
);
2045 case ZAPI_IPSET_ENTRY_FAIL_INSTALL
:
2046 if (BGP_DEBUG(zebra
, ZEBRA
))
2047 zlog_debug("%s: Received IPSET_ENTRY_FAIL_INSTALL",
2048 __PRETTY_FUNCTION__
);
2049 bgp_pbime
->installed
= false;
2050 bgp_pbime
->install_in_progress
= false;
2052 case ZAPI_IPSET_ENTRY_INSTALLED
:
2053 bgp_pbime
->installed
= true;
2054 bgp_pbime
->install_in_progress
= false;
2055 if (BGP_DEBUG(zebra
, ZEBRA
))
2056 zlog_debug("%s: Received IPSET_ENTRY_INSTALLED",
2057 __PRETTY_FUNCTION__
);
2059 case ZAPI_IPSET_ENTRY_REMOVED
:
2060 if (BGP_DEBUG(zebra
, ZEBRA
))
2061 zlog_debug("%s: Received IPSET_ENTRY_REMOVED",
2062 __PRETTY_FUNCTION__
);
2068 static int iptable_notify_owner(int command
, struct zclient
*zclient
,
2069 zebra_size_t length
, vrf_id_t vrf_id
)
2072 enum zapi_iptable_notify_owner note
;
2073 struct bgp_pbr_match
*bgpm
;
2075 if (!zapi_iptable_notify_decode(
2080 bgpm
= bgp_pbr_match_iptable_lookup(vrf_id
, unique
);
2082 if (BGP_DEBUG(zebra
, ZEBRA
))
2083 zlog_debug("%s: Fail to look BGP iptable (%u)",
2084 __PRETTY_FUNCTION__
, unique
);
2088 case ZAPI_IPTABLE_FAIL_INSTALL
:
2089 if (BGP_DEBUG(zebra
, ZEBRA
))
2090 zlog_debug("%s: Received IPTABLE_FAIL_INSTALL",
2091 __PRETTY_FUNCTION__
);
2092 bgpm
->installed_in_iptable
= false;
2093 bgpm
->install_iptable_in_progress
= false;
2095 case ZAPI_IPTABLE_INSTALLED
:
2096 bgpm
->installed_in_iptable
= true;
2097 bgpm
->install_iptable_in_progress
= false;
2098 if (BGP_DEBUG(zebra
, ZEBRA
))
2099 zlog_debug("%s: Received IPTABLE_INSTALLED",
2100 __PRETTY_FUNCTION__
);
2101 bgpm
->action
->refcnt
++;
2103 case ZAPI_IPTABLE_REMOVED
:
2104 if (BGP_DEBUG(zebra
, ZEBRA
))
2105 zlog_debug("%s: Received IPTABLE REMOVED",
2106 __PRETTY_FUNCTION__
);
2112 static void bgp_encode_pbr_rule_action(struct stream
*s
,
2113 struct bgp_pbr_action
*pbra
)
2117 stream_putl(s
, 0); /* seqno unused */
2118 stream_putl(s
, 0); /* ruleno unused */
2120 stream_putl(s
, pbra
->unique
);
2122 memset(&any
, 0, sizeof(any
));
2123 any
.family
= AF_INET
;
2124 stream_putc(s
, any
.family
);
2125 stream_putc(s
, any
.prefixlen
);
2126 stream_put(s
, &any
.u
.prefix
, prefix_blen(&any
));
2128 stream_putw(s
, 0); /* src port */
2130 stream_putc(s
, any
.family
);
2131 stream_putc(s
, any
.prefixlen
);
2132 stream_put(s
, &any
.u
.prefix
, prefix_blen(&any
));
2134 stream_putw(s
, 0); /* dst port */
2136 stream_putl(s
, pbra
->fwmark
); /* fwmark */
2138 stream_putl(s
, pbra
->table_id
);
2140 stream_putl(s
, 0); /* ifindex unused */
2143 static void bgp_encode_pbr_ipset_match(struct stream
*s
,
2144 struct bgp_pbr_match
*pbim
)
2146 stream_putl(s
, pbim
->unique
);
2147 stream_putl(s
, pbim
->type
);
2149 stream_put(s
, pbim
->ipset_name
,
2150 ZEBRA_IPSET_NAME_SIZE
);
2155 static void bgp_encode_pbr_ipset_entry_match(struct stream
*s
,
2156 struct bgp_pbr_match_entry
*pbime
)
2158 stream_putl(s
, pbime
->unique
);
2159 /* check that back pointer is not null */
2160 stream_put(s
, pbime
->backpointer
->ipset_name
,
2161 ZEBRA_IPSET_NAME_SIZE
);
2163 stream_putc(s
, pbime
->src
.family
);
2164 stream_putc(s
, pbime
->src
.prefixlen
);
2165 stream_put(s
, &pbime
->src
.u
.prefix
, prefix_blen(&pbime
->src
));
2167 stream_putc(s
, pbime
->dst
.family
);
2168 stream_putc(s
, pbime
->dst
.prefixlen
);
2169 stream_put(s
, &pbime
->dst
.u
.prefix
, prefix_blen(&pbime
->dst
));
2172 static void bgp_encode_pbr_iptable_match(struct stream
*s
,
2173 struct bgp_pbr_action
*bpa
,
2174 struct bgp_pbr_match
*pbm
)
2176 stream_putl(s
, pbm
->unique2
);
2178 stream_putl(s
, pbm
->type
);
2180 stream_putl(s
, pbm
->flags
);
2182 /* TODO: correlate with what is contained
2183 * into bgp_pbr_action.
2184 * currently only forward supported
2186 if (bpa
->nh
.type
== NEXTHOP_TYPE_BLACKHOLE
)
2187 stream_putl(s
, ZEBRA_IPTABLES_DROP
);
2189 stream_putl(s
, ZEBRA_IPTABLES_FORWARD
);
2190 stream_putl(s
, bpa
->fwmark
);
2191 stream_put(s
, pbm
->ipset_name
,
2192 ZEBRA_IPSET_NAME_SIZE
);
2195 /* BGP has established connection with Zebra. */
2196 static void bgp_zebra_connected(struct zclient
*zclient
)
2200 zclient_num_connects
++; /* increment even if not responding */
2202 /* At this point, we may or may not have BGP instances configured, but
2203 * we're only interested in the default VRF (others wouldn't have learnt
2204 * the VRF from Zebra yet.)
2206 bgp
= bgp_get_default();
2210 bgp_zebra_instance_register(bgp
);
2212 /* Send the client registration */
2213 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
2215 /* tell label pool that zebra is connected */
2216 bgp_lp_event_zebra_up();
2218 /* TODO - What if we have peers and networks configured, do we have to
2223 static int bgp_zebra_process_local_l3vni(int cmd
, struct zclient
*zclient
,
2224 zebra_size_t length
, vrf_id_t vrf_id
)
2227 char buf
[ETHER_ADDR_STRLEN
];
2229 struct ethaddr rmac
;
2230 struct in_addr originator_ip
;
2233 memset(&rmac
, 0, sizeof(struct ethaddr
));
2234 memset(&originator_ip
, 0, sizeof(struct in_addr
));
2236 l3vni
= stream_getl(s
);
2237 if (cmd
== ZEBRA_L3VNI_ADD
) {
2238 stream_get(&rmac
, s
, sizeof(struct ethaddr
));
2239 originator_ip
.s_addr
= stream_get_ipv4(s
);
2240 stream_get(&filter
, s
, sizeof(int));
2243 if (BGP_DEBUG(zebra
, ZEBRA
))
2244 zlog_debug("Rx L3-VNI %s VRF %s VNI %u RMAC %s filter %s",
2245 (cmd
== ZEBRA_L3VNI_ADD
) ? "add" : "del",
2246 vrf_id_to_name(vrf_id
), l3vni
,
2247 prefix_mac2str(&rmac
, buf
, sizeof(buf
)),
2248 filter
? "prefix-routes-only" : "none");
2250 if (cmd
== ZEBRA_L3VNI_ADD
)
2251 bgp_evpn_local_l3vni_add(l3vni
, vrf_id
, &rmac
, originator_ip
,
2254 bgp_evpn_local_l3vni_del(l3vni
, vrf_id
);
2259 static int bgp_zebra_process_local_vni(int command
, struct zclient
*zclient
,
2260 zebra_size_t length
, vrf_id_t vrf_id
)
2265 struct in_addr vtep_ip
= {INADDR_ANY
};
2266 vrf_id_t tenant_vrf_id
= VRF_DEFAULT
;
2269 vni
= stream_getl(s
);
2270 if (command
== ZEBRA_VNI_ADD
) {
2271 vtep_ip
.s_addr
= stream_get_ipv4(s
);
2272 stream_get(&tenant_vrf_id
, s
, sizeof(vrf_id_t
));
2275 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2279 if (BGP_DEBUG(zebra
, ZEBRA
))
2280 zlog_debug("Rx VNI %s VRF %s VNI %u tenant-vrf %s",
2281 (command
== ZEBRA_VNI_ADD
) ? "add" : "del",
2282 vrf_id_to_name(vrf_id
), vni
,
2283 vrf_id_to_name(tenant_vrf_id
));
2285 if (command
== ZEBRA_VNI_ADD
)
2286 return bgp_evpn_local_vni_add(
2287 bgp
, vni
, vtep_ip
.s_addr
? vtep_ip
: bgp
->router_id
,
2290 return bgp_evpn_local_vni_del(bgp
, vni
);
2293 static int bgp_zebra_process_local_macip(int command
, struct zclient
*zclient
,
2294 zebra_size_t length
, vrf_id_t vrf_id
)
2302 char buf
[ETHER_ADDR_STRLEN
];
2303 char buf1
[INET6_ADDRSTRLEN
];
2306 memset(&ip
, 0, sizeof(ip
));
2308 vni
= stream_getl(s
);
2309 stream_get(&mac
.octet
, s
, ETH_ALEN
);
2310 ipa_len
= stream_getl(s
);
2311 if (ipa_len
!= 0 && ipa_len
!= IPV4_MAX_BYTELEN
2312 && ipa_len
!= IPV6_MAX_BYTELEN
) {
2313 zlog_err("%u:Recv MACIP %s with invalid IP addr length %d",
2314 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
2321 (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
: IPADDR_V6
;
2322 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
2324 flags
= stream_getc(s
);
2326 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2330 if (BGP_DEBUG(zebra
, ZEBRA
))
2331 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u",
2332 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
2333 flags
, prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2334 ipaddr2str(&ip
, buf1
, sizeof(buf1
)), vni
);
2336 if (command
== ZEBRA_MACIP_ADD
)
2337 return bgp_evpn_local_macip_add(bgp
, vni
, &mac
, &ip
, flags
);
2339 return bgp_evpn_local_macip_del(bgp
, vni
, &mac
, &ip
);
2342 static void bgp_zebra_process_local_ip_prefix(int cmd
, struct zclient
*zclient
,
2343 zebra_size_t length
,
2346 struct stream
*s
= NULL
;
2347 struct bgp
*bgp_vrf
= NULL
;
2349 char buf
[PREFIX_STRLEN
];
2351 memset(&p
, 0, sizeof(struct prefix
));
2353 stream_get(&p
, s
, sizeof(struct prefix
));
2355 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
2359 if (BGP_DEBUG(zebra
, ZEBRA
))
2360 zlog_debug("Recv prefix %s %s on vrf %s",
2361 prefix2str(&p
, buf
, sizeof(buf
)),
2362 (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) ? "ADD" : "DEL",
2363 vrf_id_to_name(vrf_id
));
2365 if (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) {
2367 if (p
.family
== AF_INET
)
2368 return bgp_evpn_advertise_type5_route(
2369 bgp_vrf
, &p
, NULL
, AFI_IP
, SAFI_UNICAST
);
2371 return bgp_evpn_advertise_type5_route(
2372 bgp_vrf
, &p
, NULL
, AFI_IP6
, SAFI_UNICAST
);
2375 if (p
.family
== AF_INET
)
2376 return bgp_evpn_withdraw_type5_route(
2377 bgp_vrf
, &p
, AFI_IP
, SAFI_UNICAST
);
2379 return bgp_evpn_withdraw_type5_route(
2380 bgp_vrf
, &p
, AFI_IP6
, SAFI_UNICAST
);
2384 static void bgp_zebra_process_label_chunk(
2386 struct zclient
*zclient
,
2387 zebra_size_t length
,
2390 struct stream
*s
= NULL
;
2391 uint8_t response_keep
;
2395 unsigned short instance
;
2398 STREAM_GETC(s
, proto
);
2399 STREAM_GETW(s
, instance
);
2400 STREAM_GETC(s
, response_keep
);
2401 STREAM_GETL(s
, first
);
2402 STREAM_GETL(s
, last
);
2404 if (zclient
->redist_default
!= proto
) {
2405 zlog_err("Got LM msg with wrong proto %u", proto
);
2408 if (zclient
->instance
!= instance
) {
2409 zlog_err("Got LM msg with wrong instance %u", proto
);
2414 first
< MPLS_LABEL_UNRESERVED_MIN
||
2415 last
> MPLS_LABEL_UNRESERVED_MAX
) {
2417 zlog_err("%s: Invalid Label chunk: %u - %u",
2418 __func__
, first
, last
);
2421 if (BGP_DEBUG(zebra
, ZEBRA
)) {
2422 zlog_debug("Label Chunk assign: %u - %u (%u) ",
2423 first
, last
, response_keep
);
2426 bgp_lp_event_chunk(response_keep
, first
, last
);
2428 stream_failure
: /* for STREAM_GETX */
2432 extern struct zebra_privs_t bgpd_privs
;
2434 void bgp_zebra_init(struct thread_master
*master
)
2436 zclient_num_connects
= 0;
2438 /* Set default values. */
2439 zclient
= zclient_new_notify(master
, &zclient_options_default
);
2440 zclient_init(zclient
, ZEBRA_ROUTE_BGP
, 0, &bgpd_privs
);
2441 zclient
->zebra_connected
= bgp_zebra_connected
;
2442 zclient
->router_id_update
= bgp_router_id_update
;
2443 zclient
->interface_add
= bgp_interface_add
;
2444 zclient
->interface_delete
= bgp_interface_delete
;
2445 zclient
->interface_address_add
= bgp_interface_address_add
;
2446 zclient
->interface_address_delete
= bgp_interface_address_delete
;
2447 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
2448 zclient
->interface_nbr_address_delete
=
2449 bgp_interface_nbr_address_delete
;
2450 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
2451 zclient
->redistribute_route_add
= zebra_read_route
;
2452 zclient
->redistribute_route_del
= zebra_read_route
;
2453 zclient
->interface_up
= bgp_interface_up
;
2454 zclient
->interface_down
= bgp_interface_down
;
2455 zclient
->nexthop_update
= bgp_read_nexthop_update
;
2456 zclient
->import_check_update
= bgp_read_import_check_update
;
2457 zclient
->fec_update
= bgp_read_fec_update
;
2458 zclient
->local_vni_add
= bgp_zebra_process_local_vni
;
2459 zclient
->local_vni_del
= bgp_zebra_process_local_vni
;
2460 zclient
->local_macip_add
= bgp_zebra_process_local_macip
;
2461 zclient
->local_macip_del
= bgp_zebra_process_local_macip
;
2462 zclient
->local_l3vni_add
= bgp_zebra_process_local_l3vni
;
2463 zclient
->local_l3vni_del
= bgp_zebra_process_local_l3vni
;
2464 zclient
->local_ip_prefix_add
= bgp_zebra_process_local_ip_prefix
;
2465 zclient
->local_ip_prefix_del
= bgp_zebra_process_local_ip_prefix
;
2466 zclient
->label_chunk
= bgp_zebra_process_label_chunk
;
2467 zclient
->rule_notify_owner
= rule_notify_owner
;
2468 zclient
->ipset_notify_owner
= ipset_notify_owner
;
2469 zclient
->ipset_entry_notify_owner
= ipset_entry_notify_owner
;
2470 zclient
->iptable_notify_owner
= iptable_notify_owner
;
2473 void bgp_zebra_destroy(void)
2475 if (zclient
== NULL
)
2477 zclient_stop(zclient
);
2478 zclient_free(zclient
);
2482 int bgp_zebra_num_connects(void)
2484 return zclient_num_connects
;
2487 void bgp_send_pbr_rule_action(struct bgp_pbr_action
*pbra
, bool install
)
2491 if (pbra
->install_in_progress
)
2493 zlog_debug("%s: table %d fwmark %d %d", __PRETTY_FUNCTION__
,
2494 pbra
->table_id
, pbra
->fwmark
, install
);
2498 zclient_create_header(s
,
2499 install
? ZEBRA_RULE_ADD
: ZEBRA_RULE_DELETE
,
2501 stream_putl(s
, 1); /* send one pbr action */
2503 bgp_encode_pbr_rule_action(s
, pbra
);
2505 stream_putw_at(s
, 0, stream_get_endp(s
));
2506 if (!zclient_send_message(zclient
) && install
)
2507 pbra
->install_in_progress
= true;
2510 void bgp_send_pbr_ipset_match(struct bgp_pbr_match
*pbrim
, bool install
)
2514 if (pbrim
->install_in_progress
)
2516 zlog_debug("%s: name %s type %d %d", __PRETTY_FUNCTION__
,
2517 pbrim
->ipset_name
, pbrim
->type
, install
);
2521 zclient_create_header(s
,
2522 install
? ZEBRA_IPSET_CREATE
:
2523 ZEBRA_IPSET_DESTROY
,
2526 stream_putl(s
, 1); /* send one pbr action */
2528 bgp_encode_pbr_ipset_match(s
, pbrim
);
2530 stream_putw_at(s
, 0, stream_get_endp(s
));
2531 if (!zclient_send_message(zclient
) && install
)
2532 pbrim
->install_in_progress
= true;
2535 void bgp_send_pbr_ipset_entry_match(struct bgp_pbr_match_entry
*pbrime
,
2540 if (pbrime
->install_in_progress
)
2542 zlog_debug("%s: name %s %d %d", __PRETTY_FUNCTION__
,
2543 pbrime
->backpointer
->ipset_name
,
2544 pbrime
->unique
, install
);
2548 zclient_create_header(s
,
2549 install
? ZEBRA_IPSET_ENTRY_ADD
:
2550 ZEBRA_IPSET_ENTRY_DELETE
,
2553 stream_putl(s
, 1); /* send one pbr action */
2555 bgp_encode_pbr_ipset_entry_match(s
, pbrime
);
2557 stream_putw_at(s
, 0, stream_get_endp(s
));
2558 if (!zclient_send_message(zclient
) && install
)
2559 pbrime
->install_in_progress
= true;
2562 void bgp_send_pbr_iptable(struct bgp_pbr_action
*pba
,
2563 struct bgp_pbr_match
*pbm
,
2568 if (pbm
->install_iptable_in_progress
)
2570 zlog_debug("%s: name %s type %d mark %d %d", __PRETTY_FUNCTION__
,
2571 pbm
->ipset_name
, pbm
->type
, pba
->fwmark
, install
);
2575 zclient_create_header(s
,
2576 install
? ZEBRA_IPTABLE_ADD
:
2577 ZEBRA_IPTABLE_DELETE
,
2580 bgp_encode_pbr_iptable_match(s
, pba
, pbm
);
2582 stream_putw_at(s
, 0, stream_get_endp(s
));
2583 if (!zclient_send_message(zclient
) && install
) {
2584 pbm
->install_iptable_in_progress
= true;
2589 /* inject in table <table_id> a default route to:
2590 * - if nexthop IP is present : to this nexthop
2591 * - if vrf is different from local : to the matching VRF
2593 void bgp_zebra_announce_default(struct bgp
*bgp
, struct nexthop
*nh
,
2594 afi_t afi
, uint32_t table_id
, bool announce
)
2596 struct zapi_nexthop
*api_nh
;
2597 struct zapi_route api
;
2600 if (!nh
|| nh
->type
!= NEXTHOP_TYPE_IPV4
2601 || nh
->vrf_id
== VRF_UNKNOWN
)
2603 memset(&p
, 0, sizeof(struct prefix
));
2608 memset(&api
, 0, sizeof(api
));
2609 api
.vrf_id
= bgp
->vrf_id
;
2610 api
.type
= ZEBRA_ROUTE_BGP
;
2611 api
.safi
= SAFI_UNICAST
;
2613 api
.tableid
= table_id
;
2614 api
.nexthop_num
= 1;
2615 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
2616 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
2617 api_nh
= &api
.nexthops
[0];
2620 if (nh
->gate
.ipv4
.s_addr
) {
2621 char buff
[PREFIX_STRLEN
];
2623 api_nh
->vrf_id
= nh
->vrf_id
;
2624 api_nh
->gate
.ipv4
= nh
->gate
.ipv4
;
2625 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
2627 inet_ntop(AF_INET
, &(nh
->gate
.ipv4
), buff
, INET_ADDRSTRLEN
);
2628 if (BGP_DEBUG(zebra
, ZEBRA
))
2629 zlog_info("BGP: sending default route to %s table %d (redirect IP)",
2631 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
2632 : ZEBRA_ROUTE_DELETE
,
2634 } else if (nh
->vrf_id
!= bgp
->vrf_id
) {
2636 struct interface
*ifp
;
2638 vrf
= vrf_lookup_by_id(nh
->vrf_id
);
2641 /* create default route with interface <VRF>
2642 * with nexthop-vrf <VRF>
2644 ifp
= if_lookup_by_name_all_vrf(vrf
->name
);
2647 api_nh
->vrf_id
= nh
->vrf_id
;
2648 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
2649 api_nh
->ifindex
= ifp
->ifindex
;
2650 if (BGP_DEBUG(zebra
, ZEBRA
))
2651 zlog_info("BGP: sending default route to %s table %d (redirect VRF)",
2652 vrf
->name
, table_id
);
2653 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
2654 : ZEBRA_ROUTE_DELETE
,