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_errors.h"
48 #include "bgpd/bgp_mpath.h"
49 #include "bgpd/bgp_nexthop.h"
50 #include "bgpd/bgp_nht.h"
51 #include "bgpd/bgp_bfd.h"
52 #include "bgpd/bgp_label.h"
54 #include "bgpd/rfapi/rfapi_backend.h"
55 #include "bgpd/rfapi/vnc_export_bgp.h"
57 #include "bgpd/bgp_evpn.h"
58 #include "bgpd/bgp_mplsvpn.h"
59 #include "bgpd/bgp_labelpool.h"
60 #include "bgpd/bgp_pbr.h"
62 /* All information about zebra. */
63 struct zclient
*zclient
= NULL
;
65 /* Can we install into zebra? */
66 static inline int bgp_install_info_to_zebra(struct bgp
*bgp
)
68 if (zclient
->sock
<= 0)
71 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
77 int zclient_num_connects
;
79 /* Router-id update message from zebra. */
80 static int bgp_router_id_update(int command
, struct zclient
*zclient
,
81 zebra_size_t length
, vrf_id_t vrf_id
)
83 struct prefix router_id
;
85 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
87 if (BGP_DEBUG(zebra
, ZEBRA
)) {
88 char buf
[PREFIX2STR_BUFFER
];
89 prefix2str(&router_id
, buf
, sizeof(buf
));
90 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id
, buf
);
93 bgp_router_id_zebra_bump(vrf_id
, &router_id
);
97 /* Nexthop update message from zebra. */
98 static int bgp_read_nexthop_update(int command
, struct zclient
*zclient
,
99 zebra_size_t length
, vrf_id_t vrf_id
)
101 bgp_parse_nexthop_update(command
, vrf_id
);
105 static int bgp_read_import_check_update(int command
, struct zclient
*zclient
,
106 zebra_size_t length
, vrf_id_t vrf_id
)
108 bgp_parse_nexthop_update(command
, vrf_id
);
112 /* Set or clear interface on which unnumbered neighbor is configured. This
113 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
116 static void bgp_update_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
,
117 struct interface
*upd_ifp
)
119 struct listnode
*node
, *nnode
;
122 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
123 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)) {
126 bgp_zebra_initiate_radv(bgp
, peer
);
128 bgp_zebra_terminate_radv(bgp
, peer
);
135 static int bgp_read_fec_update(int command
, struct zclient
*zclient
,
138 bgp_parse_fec_update();
142 static void bgp_start_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
)
144 struct listnode
*node
, *nnode
;
147 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
148 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)
149 && peer
->status
!= Established
) {
150 if (peer_active(peer
))
151 BGP_EVENT_ADD(peer
, BGP_Stop
);
152 BGP_EVENT_ADD(peer
, BGP_Start
);
157 static void bgp_nbr_connected_add(struct bgp
*bgp
, struct nbr_connected
*ifc
)
159 struct listnode
*node
;
160 struct connected
*connected
;
161 struct interface
*ifp
;
164 /* Kick-off the FSM for any relevant peers only if there is a
165 * valid local address on the interface.
168 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
169 p
= connected
->address
;
170 if (p
->family
== AF_INET6
171 && IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
177 bgp_start_interface_nbrs(bgp
, ifp
);
180 static void bgp_nbr_connected_delete(struct bgp
*bgp
, struct nbr_connected
*ifc
,
183 struct listnode
*node
, *nnode
;
185 struct interface
*ifp
;
187 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
189 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)) {
190 peer
->last_reset
= PEER_DOWN_NBR_ADDR_DEL
;
191 BGP_EVENT_ADD(peer
, BGP_Stop
);
194 /* Free neighbor also, if we're asked to. */
197 listnode_delete(ifp
->nbr_connected
, ifc
);
198 nbr_connected_free(ifc
);
202 /* Inteface addition message from zebra. */
203 static int bgp_interface_add(int command
, struct zclient
*zclient
,
204 zebra_size_t length
, vrf_id_t vrf_id
)
206 struct interface
*ifp
;
209 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
210 if (!ifp
) // unexpected
213 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
214 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id
, ifp
->name
);
216 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
220 bgp_update_interface_nbrs(bgp
, ifp
, ifp
);
224 static int bgp_interface_delete(int command
, struct zclient
*zclient
,
225 zebra_size_t length
, vrf_id_t vrf_id
)
228 struct interface
*ifp
;
231 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
236 ifp
= zebra_interface_state_read(s
, vrf_id
);
237 if (!ifp
) /* This may happen if we've just unregistered for a VRF. */
240 if (BGP_DEBUG(zebra
, ZEBRA
))
241 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id
, ifp
->name
);
243 bgp_update_interface_nbrs(bgp
, ifp
, NULL
);
245 if_set_index(ifp
, IFINDEX_INTERNAL
);
249 static int bgp_interface_up(int command
, struct zclient
*zclient
,
250 zebra_size_t length
, vrf_id_t vrf_id
)
253 struct interface
*ifp
;
255 struct nbr_connected
*nc
;
256 struct listnode
*node
, *nnode
;
259 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
264 ifp
= zebra_interface_state_read(s
, vrf_id
);
269 if (BGP_DEBUG(zebra
, ZEBRA
))
270 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id
, ifp
->name
);
272 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
273 bgp_connected_add(bgp
, c
);
275 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
276 bgp_nbr_connected_add(bgp
, nc
);
281 static int bgp_interface_down(int command
, struct zclient
*zclient
,
282 zebra_size_t length
, vrf_id_t vrf_id
)
285 struct interface
*ifp
;
287 struct nbr_connected
*nc
;
288 struct listnode
*node
, *nnode
;
291 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
296 ifp
= zebra_interface_state_read(s
, vrf_id
);
300 if (BGP_DEBUG(zebra
, ZEBRA
))
301 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id
, ifp
->name
);
303 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
304 bgp_connected_delete(bgp
, c
);
306 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
307 bgp_nbr_connected_delete(bgp
, nc
, 1);
309 /* Fast external-failover */
313 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
316 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
317 #if defined(HAVE_CUMULUS)
318 /* Take down directly connected EBGP peers as well as
320 * tracked (directly connected) IBGP peers.
322 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1)
324 || bgp_bfd_is_peer_multihop(peer
)))
326 /* Take down directly connected EBGP peers */
327 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
331 if (ifp
== peer
->nexthop
.ifp
) {
332 BGP_EVENT_ADD(peer
, BGP_Stop
);
333 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
341 static int bgp_interface_address_add(int command
, struct zclient
*zclient
,
342 zebra_size_t length
, vrf_id_t vrf_id
)
344 struct connected
*ifc
;
347 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
351 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
356 if (bgp_debug_zebra(ifc
->address
)) {
357 char buf
[PREFIX2STR_BUFFER
];
358 prefix2str(ifc
->address
, buf
, sizeof(buf
));
359 zlog_debug("Rx Intf address add VRF %u IF %s addr %s", vrf_id
,
360 ifc
->ifp
->name
, buf
);
363 if (if_is_operative(ifc
->ifp
)) {
364 bgp_connected_add(bgp
, ifc
);
366 /* If we have learnt of any neighbors on this interface,
367 * check to kick off any BGP interface-based neighbors,
368 * but only if this is a link-local address.
370 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
)
371 && !list_isempty(ifc
->ifp
->nbr_connected
))
372 bgp_start_interface_nbrs(bgp
, ifc
->ifp
);
378 static int bgp_interface_address_delete(int command
, struct zclient
*zclient
,
379 zebra_size_t length
, vrf_id_t vrf_id
)
381 struct connected
*ifc
;
384 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
388 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
393 if (bgp_debug_zebra(ifc
->address
)) {
394 char buf
[PREFIX2STR_BUFFER
];
395 prefix2str(ifc
->address
, buf
, sizeof(buf
));
396 zlog_debug("Rx Intf address del VRF %u IF %s addr %s", vrf_id
,
397 ifc
->ifp
->name
, buf
);
400 if (if_is_operative(ifc
->ifp
)) {
401 bgp_connected_delete(bgp
, ifc
);
409 static int bgp_interface_nbr_address_add(int command
, struct zclient
*zclient
,
410 zebra_size_t length
, vrf_id_t vrf_id
)
412 struct nbr_connected
*ifc
= NULL
;
415 ifc
= zebra_interface_nbr_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 neighbor add 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_nbr_connected_add(bgp
, ifc
);
436 static int bgp_interface_nbr_address_delete(int command
,
437 struct zclient
*zclient
,
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 del 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_delete(bgp
, ifc
, 0);
462 nbr_connected_free(ifc
);
467 /* VRF update for an interface. */
468 static int bgp_interface_vrf_update(int command
, struct zclient
*zclient
,
469 zebra_size_t length
, vrf_id_t vrf_id
)
471 struct interface
*ifp
;
474 struct nbr_connected
*nc
;
475 struct listnode
*node
, *nnode
;
478 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
483 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
484 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id
,
485 ifp
->name
, new_vrf_id
);
487 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
491 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
492 bgp_connected_delete(bgp
, c
);
494 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
495 bgp_nbr_connected_delete(bgp
, nc
, 1);
497 /* Fast external-failover */
501 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
504 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
505 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
508 if (ifp
== peer
->nexthop
.ifp
)
509 BGP_EVENT_ADD(peer
, BGP_Stop
);
513 if_update_to_new_vrf(ifp
, new_vrf_id
);
515 bgp
= bgp_lookup_by_vrf_id(new_vrf_id
);
519 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
520 bgp_connected_add(bgp
, c
);
522 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
523 bgp_nbr_connected_add(bgp
, nc
);
527 /* Zebra route add and delete treatment. */
528 static int zebra_read_route(int command
, struct zclient
*zclient
,
529 zebra_size_t length
, vrf_id_t vrf_id
)
531 enum nexthop_types_t nhtype
;
532 struct zapi_route api
;
533 union g_addr nexthop
;
538 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
542 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
545 /* we completely ignore srcdest routes for now. */
546 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
549 /* ignore link-local address. */
550 if (api
.prefix
.family
== AF_INET6
551 && IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
554 nexthop
= api
.nexthops
[0].gate
;
555 ifindex
= api
.nexthops
[0].ifindex
;
556 nhtype
= api
.nexthops
[0].type
;
558 add
= (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
561 * The ADD message is actually an UPDATE and there is no
563 * for a prior redistributed route, if any. So, perform an
565 * DEL processing for the same redistributed route from any
569 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
571 bgp_redistribute_delete(bgp
, &api
.prefix
, i
,
575 /* Now perform the add/update. */
576 bgp_redistribute_add(bgp
, &api
.prefix
, &nexthop
, ifindex
,
577 nhtype
, api
.metric
, api
.type
, api
.instance
,
580 bgp_redistribute_delete(bgp
, &api
.prefix
, api
.type
,
584 if (bgp_debug_zebra(&api
.prefix
)) {
585 char buf
[2][PREFIX_STRLEN
];
587 prefix2str(&api
.prefix
, buf
[0], sizeof(buf
[0]));
589 inet_ntop(api
.prefix
.family
, &nexthop
, buf
[1],
592 "Rx route ADD VRF %u %s[%d] %s nexthop %s (type %d if %u) metric %u tag %" ROUTE_TAG_PRI
,
593 vrf_id
, zebra_route_string(api
.type
),
594 api
.instance
, buf
[0], buf
[1], nhtype
,
595 ifindex
, api
.metric
, api
.tag
);
598 "Rx route DEL VRF %u %s[%d] %s",
599 vrf_id
, zebra_route_string(api
.type
),
600 api
.instance
, buf
[0]);
607 struct interface
*if_lookup_by_ipv4(struct in_addr
*addr
, vrf_id_t vrf_id
)
610 struct listnode
*cnode
;
611 struct interface
*ifp
;
612 struct connected
*connected
;
613 struct prefix_ipv4 p
;
616 vrf
= vrf_lookup_by_id(vrf_id
);
622 p
.prefixlen
= IPV4_MAX_BITLEN
;
624 FOR_ALL_INTERFACES (vrf
, ifp
) {
625 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
626 cp
= connected
->address
;
628 if (cp
->family
== AF_INET
)
629 if (prefix_match(cp
, (struct prefix
*)&p
))
636 struct interface
*if_lookup_by_ipv4_exact(struct in_addr
*addr
, vrf_id_t vrf_id
)
639 struct listnode
*cnode
;
640 struct interface
*ifp
;
641 struct connected
*connected
;
644 vrf
= vrf_lookup_by_id(vrf_id
);
648 FOR_ALL_INTERFACES (vrf
, ifp
) {
649 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
650 cp
= connected
->address
;
652 if (cp
->family
== AF_INET
)
653 if (IPV4_ADDR_SAME(&cp
->u
.prefix4
, addr
))
660 struct interface
*if_lookup_by_ipv6(struct in6_addr
*addr
, ifindex_t ifindex
,
664 struct listnode
*cnode
;
665 struct interface
*ifp
;
666 struct connected
*connected
;
667 struct prefix_ipv6 p
;
670 vrf
= vrf_lookup_by_id(vrf_id
);
676 p
.prefixlen
= IPV6_MAX_BITLEN
;
678 FOR_ALL_INTERFACES (vrf
, ifp
) {
679 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
680 cp
= connected
->address
;
682 if (cp
->family
== AF_INET6
)
683 if (prefix_match(cp
, (struct prefix
*)&p
)) {
684 if (IN6_IS_ADDR_LINKLOCAL(
686 if (ifindex
== ifp
->ifindex
)
696 struct interface
*if_lookup_by_ipv6_exact(struct in6_addr
*addr
,
697 ifindex_t ifindex
, vrf_id_t vrf_id
)
700 struct listnode
*cnode
;
701 struct interface
*ifp
;
702 struct connected
*connected
;
705 vrf
= vrf_lookup_by_id(vrf_id
);
709 FOR_ALL_INTERFACES (vrf
, ifp
) {
710 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
711 cp
= connected
->address
;
713 if (cp
->family
== AF_INET6
)
714 if (IPV6_ADDR_SAME(&cp
->u
.prefix6
, addr
)) {
715 if (IN6_IS_ADDR_LINKLOCAL(
717 if (ifindex
== ifp
->ifindex
)
727 static int if_get_ipv6_global(struct interface
*ifp
, struct in6_addr
*addr
)
729 struct listnode
*cnode
;
730 struct connected
*connected
;
733 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
734 cp
= connected
->address
;
736 if (cp
->family
== AF_INET6
)
737 if (!IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
738 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
745 static int if_get_ipv6_local(struct interface
*ifp
, struct in6_addr
*addr
)
747 struct listnode
*cnode
;
748 struct connected
*connected
;
751 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
752 cp
= connected
->address
;
754 if (cp
->family
== AF_INET6
)
755 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
756 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
763 static int if_get_ipv4_address(struct interface
*ifp
, struct in_addr
*addr
)
765 struct listnode
*cnode
;
766 struct connected
*connected
;
769 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
770 cp
= connected
->address
;
771 if ((cp
->family
== AF_INET
)
772 && !ipv4_martian(&(cp
->u
.prefix4
))) {
773 *addr
= cp
->u
.prefix4
;
780 int bgp_nexthop_set(union sockunion
*local
, union sockunion
*remote
,
781 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
784 struct interface
*ifp
= NULL
;
786 memset(nexthop
, 0, sizeof(struct bgp_nexthop
));
793 if (local
->sa
.sa_family
== AF_INET
) {
794 nexthop
->v4
= local
->sin
.sin_addr
;
796 ifp
= if_lookup_by_name(peer
->update_if
,
799 ifp
= if_lookup_by_ipv4_exact(&local
->sin
.sin_addr
,
802 if (local
->sa
.sa_family
== AF_INET6
) {
803 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
804 if (peer
->conf_if
|| peer
->ifname
)
805 ifp
= if_lookup_by_name(peer
->conf_if
809 } else if (peer
->update_if
)
810 ifp
= if_lookup_by_name(peer
->update_if
,
813 ifp
= if_lookup_by_ipv6_exact(&local
->sin6
.sin6_addr
,
814 local
->sin6
.sin6_scope_id
,
823 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
824 if (local
->sa
.sa_family
== AF_INET
) {
826 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
829 /* There is no global nexthop. Use link-local address as
831 * global and link-local nexthop. In this scenario, the
833 * for interop is that the network admin would use a
835 * specify the global IPv6 nexthop.
837 if_get_ipv6_local(ifp
, &nexthop
->v6_global
);
838 memcpy(&nexthop
->v6_local
, &nexthop
->v6_global
,
841 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
843 if (if_lookup_by_ipv4(&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
844 peer
->shared_network
= 1;
846 peer
->shared_network
= 0;
849 /* IPv6 connection, fetch and store IPv4 local address if any. */
850 if (local
->sa
.sa_family
== AF_INET6
) {
851 struct interface
*direct
= NULL
;
854 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
855 if (!ret
&& peer
->local_id
.s_addr
)
856 nexthop
->v4
= peer
->local_id
;
859 if (!IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
860 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
863 /* If directory connected set link-local address. */
864 direct
= if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
865 remote
->sin6
.sin6_scope_id
,
868 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
870 /* Link-local address. */
872 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
874 /* If there is no global address. Set link-local
876 global. I know this break RFC specification... */
877 /* In this scenario, the expectation for interop is that
879 * network admin would use a route-map to specify the
884 memcpy(&nexthop
->v6_global
,
885 &local
->sin6
.sin6_addr
,
887 /* Always set the link-local address */
888 memcpy(&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
892 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)
893 || if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
894 remote
->sin6
.sin6_scope_id
,
896 peer
->shared_network
= 1;
898 peer
->shared_network
= 0;
901 /* KAME stack specific treatment. */
903 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_global
)
904 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
)) {
905 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
, 0);
907 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_local
)
908 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
)) {
909 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
, 0);
913 /* If we have identified the local interface, there is no error for now.
918 static struct in6_addr
*bgp_info_to_ipv6_nexthop(struct bgp_info
*info
,
921 struct in6_addr
*nexthop
= NULL
;
923 /* Only global address nexthop exists. */
924 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
) {
925 nexthop
= &info
->attr
->mp_nexthop_global
;
926 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
927 *ifindex
= info
->attr
->nh_ifindex
;
931 /* If both global and link-local address present. */
932 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
933 /* Check if route-map is set to prefer global over link-local */
934 if (info
->attr
->mp_nexthop_prefer_global
) {
935 nexthop
= &info
->attr
->mp_nexthop_global
;
936 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
937 *ifindex
= info
->attr
->nh_ifindex
;
939 /* Workaround for Cisco's nexthop bug. */
940 if (IN6_IS_ADDR_UNSPECIFIED(
941 &info
->attr
->mp_nexthop_global
)
942 && info
->peer
->su_remote
->sa
.sa_family
945 &info
->peer
->su_remote
->sin6
.sin6_addr
;
946 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
947 *ifindex
= info
->peer
->nexthop
.ifp
950 nexthop
= &info
->attr
->mp_nexthop_local
;
951 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
952 *ifindex
= info
->attr
->nh_lla_ifindex
;
960 static int bgp_table_map_apply(struct route_map
*map
, struct prefix
*p
,
961 struct bgp_info
*info
)
963 route_map_result_t ret
;
965 ret
= route_map_apply(map
, p
, RMAP_BGP
, info
);
966 bgp_attr_flush(info
->attr
);
968 if (ret
!= RMAP_DENYMATCH
)
971 if (bgp_debug_zebra(p
)) {
972 if (p
->family
== AF_INET
) {
973 char buf
[2][INET_ADDRSTRLEN
];
975 "Zebra rmap deny: IPv4 route %s/%d nexthop %s",
976 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0],
979 inet_ntop(AF_INET
, &info
->attr
->nexthop
, buf
[1],
982 if (p
->family
== AF_INET6
) {
983 char buf
[2][INET6_ADDRSTRLEN
];
985 struct in6_addr
*nexthop
;
987 nexthop
= bgp_info_to_ipv6_nexthop(info
, &ifindex
);
989 "Zebra rmap deny: IPv6 route %s/%d nexthop %s",
990 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0],
993 inet_ntop(AF_INET6
, nexthop
,
994 buf
[1], sizeof(buf
[1])));
1000 static struct thread
*bgp_tm_thread_connect
;
1001 static bool bgp_tm_status_connected
;
1002 static bool bgp_tm_chunk_obtained
;
1003 #define BGP_FLOWSPEC_TABLE_CHUNK 100000
1004 static uint32_t bgp_tm_min
, bgp_tm_max
, bgp_tm_chunk_size
;
1005 struct bgp
*bgp_tm_bgp
;
1007 static int bgp_zebra_tm_connect(struct thread
*t
)
1009 struct zclient
*zclient
;
1010 int delay
= 10, ret
= 0;
1012 zclient
= THREAD_ARG(t
);
1013 if (bgp_tm_status_connected
&& zclient
->sock
> 0)
1016 bgp_tm_status_connected
= false;
1017 ret
= tm_table_manager_connect(zclient
);
1020 zlog_info("Error connecting to table manager!");
1021 bgp_tm_status_connected
= false;
1023 if (!bgp_tm_status_connected
)
1024 zlog_debug("Connecting to table manager. Success");
1025 bgp_tm_status_connected
= true;
1026 if (!bgp_tm_chunk_obtained
) {
1027 if (bgp_zebra_get_table_range(bgp_tm_chunk_size
,
1029 &bgp_tm_max
) >= 0) {
1030 bgp_tm_chunk_obtained
= true;
1031 /* parse non installed entries */
1032 bgp_zebra_announce_table(bgp_tm_bgp
, AFI_IP
, SAFI_FLOWSPEC
);
1036 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1037 &bgp_tm_thread_connect
);
1041 bool bgp_zebra_tm_chunk_obtained(void)
1043 return bgp_tm_chunk_obtained
;
1046 uint32_t bgp_zebra_tm_get_id(void)
1048 static int table_id
;
1050 if (!bgp_tm_chunk_obtained
)
1052 return bgp_tm_min
++;
1055 void bgp_zebra_init_tm_connect(struct bgp
*bgp
)
1059 /* if already set, do nothing
1061 if (bgp_tm_thread_connect
!= NULL
)
1063 bgp_tm_status_connected
= false;
1064 bgp_tm_chunk_obtained
= false;
1065 bgp_tm_min
= bgp_tm_max
= 0;
1066 bgp_tm_chunk_size
= BGP_FLOWSPEC_TABLE_CHUNK
;
1068 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1069 &bgp_tm_thread_connect
);
1072 int bgp_zebra_get_table_range(uint32_t chunk_size
,
1073 uint32_t *start
, uint32_t *end
)
1077 if (!bgp_tm_status_connected
)
1079 ret
= tm_get_table_chunk(zclient
, chunk_size
, start
, end
);
1081 flog_err(BGP_ERR_TABLE_CHUNK
,
1082 "BGP: Error getting table chunk %u", chunk_size
);
1085 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1086 chunk_size
, *start
, *end
);
1090 static int update_ipv4nh_for_route_install(int nh_othervrf
,
1091 struct in_addr
*nexthop
,
1094 struct zapi_nexthop
*api_nh
)
1096 api_nh
->gate
.ipv4
= *nexthop
;
1098 /* Need to set fields appropriately for EVPN routes imported into
1099 * a VRF (which are programmed as onlink on l3-vni SVI) as well as
1100 * connected routes leaked into a VRF.
1103 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1104 else if (nh_othervrf
&&
1105 api_nh
->gate
.ipv4
.s_addr
== INADDR_ANY
) {
1106 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1107 api_nh
->ifindex
= attr
->nh_ifindex
;
1109 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1114 static int update_ipv6nh_for_route_install(int nh_othervrf
,
1115 struct in6_addr
*nexthop
,
1117 struct bgp_info
*ri
,
1118 struct bgp_info
*best_ri
,
1120 struct zapi_nexthop
*api_nh
)
1127 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1128 else if (nh_othervrf
) {
1129 if (IN6_IS_ADDR_UNSPECIFIED(nexthop
)) {
1130 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1131 api_nh
->ifindex
= attr
->nh_ifindex
;
1132 } else if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1135 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1136 api_nh
->ifindex
= ifindex
;
1138 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1139 api_nh
->ifindex
= 0;
1142 if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1143 if (ri
== best_ri
&&
1144 attr
->mp_nexthop_len
1145 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1146 if (ri
->peer
->nexthop
.ifp
)
1147 ifindex
= ri
->peer
->nexthop
.ifp
1150 if (ri
->peer
->conf_if
)
1151 ifindex
= ri
->peer
->ifp
->ifindex
;
1152 else if (ri
->peer
->ifname
)
1153 ifindex
= ifname2ifindex(
1155 ri
->peer
->bgp
->vrf_id
);
1156 else if (ri
->peer
->nexthop
.ifp
)
1157 ifindex
= ri
->peer
->nexthop
.ifp
1163 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1164 api_nh
->ifindex
= ifindex
;
1166 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1167 api_nh
->ifindex
= 0;
1170 api_nh
->gate
.ipv6
= *nexthop
;
1175 void bgp_zebra_announce(struct bgp_node
*rn
, struct prefix
*p
,
1176 struct bgp_info
*info
, struct bgp
*bgp
, afi_t afi
,
1179 struct zapi_route api
;
1180 struct zapi_nexthop
*api_nh
;
1182 unsigned int valid_nh_count
= 0;
1183 int has_valid_label
= 0;
1186 struct bgp_info
*mpinfo
;
1188 struct attr local_attr
;
1189 struct bgp_info local_info
;
1190 struct bgp_info
*mpinfo_cp
= &local_info
;
1193 int nh_othervrf
= 0;
1194 char buf_prefix
[PREFIX_STRLEN
]; /* filled in if we are debugging */
1198 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1199 * know of this instance.
1201 if (!bgp_install_info_to_zebra(bgp
))
1204 if (bgp
->main_zebra_update_hold
)
1207 if (bgp_debug_zebra(p
))
1208 prefix2str(p
, buf_prefix
, sizeof(buf_prefix
));
1210 if (safi
== SAFI_FLOWSPEC
)
1211 return bgp_pbr_update_entry(bgp
, &rn
->p
,
1212 info
, afi
, safi
, true);
1215 * vrf leaking support (will have only one nexthop)
1217 if (info
->extra
&& info
->extra
->bgp_orig
)
1220 /* Make Zebra API structure. */
1221 memset(&api
, 0, sizeof(api
));
1222 api
.vrf_id
= bgp
->vrf_id
;
1223 api
.type
= ZEBRA_ROUTE_BGP
;
1226 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1230 if (info
->type
== ZEBRA_ROUTE_BGP
1231 && info
->sub_type
== BGP_ROUTE_IMPORTED
) {
1233 /* Obtain peer from parent */
1234 if (info
->extra
&& info
->extra
->parent
)
1235 peer
= ((struct bgp_info
*)(info
->extra
->parent
))->peer
;
1238 tag
= info
->attr
->tag
;
1240 /* If the route's source is EVPN, flag as such. */
1241 is_evpn
= is_route_parent_evpn(info
);
1243 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1245 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
1246 || info
->sub_type
== BGP_ROUTE_AGGREGATE
) {
1247 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1248 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1251 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1252 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1253 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1255 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1257 /* Metric is currently based on the best-path only */
1258 metric
= info
->attr
->med
;
1259 for (mpinfo
= info
; mpinfo
; mpinfo
= bgp_info_mpath_next(mpinfo
)) {
1260 if (valid_nh_count
>= multipath_num
)
1263 *mpinfo_cp
= *mpinfo
;
1265 /* Get nexthop address-family */
1266 if (p
->family
== AF_INET
1267 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
))
1268 nh_family
= AF_INET
;
1269 else if (p
->family
== AF_INET6
1270 || (p
->family
== AF_INET
1271 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
)))
1272 nh_family
= AF_INET6
;
1276 api_nh
= &api
.nexthops
[valid_nh_count
];
1277 api_nh
->vrf_id
= nh_othervrf
? info
->extra
->bgp_orig
->vrf_id
1279 if (nh_family
== AF_INET
) {
1280 if (bgp_debug_zebra(&api
.prefix
)) {
1281 if (mpinfo
->extra
) {
1283 "%s: p=%s, bgp_is_valid_label: %d",
1284 __func__
, buf_prefix
,
1290 "%s: p=%s, extra is NULL, no label",
1291 __func__
, buf_prefix
);
1295 if (bgp
->table_map
[afi
][safi
].name
) {
1296 /* Copy info and attributes, so the route-map
1297 apply doesn't modify the BGP route info. */
1298 local_attr
= *mpinfo
->attr
;
1299 mpinfo_cp
->attr
= &local_attr
;
1302 if (bgp
->table_map
[afi
][safi
].name
) {
1303 if (!bgp_table_map_apply(
1304 bgp
->table_map
[afi
][safi
].map
, p
,
1308 /* metric/tag is only allowed to be
1309 * overridden on 1st nexthop */
1310 if (mpinfo
== info
) {
1311 metric
= mpinfo_cp
->attr
->med
;
1312 tag
= mpinfo_cp
->attr
->tag
;
1316 nh_updated
= update_ipv4nh_for_route_install(
1318 &mpinfo_cp
->attr
->nexthop
,
1319 mpinfo_cp
->attr
, is_evpn
, api_nh
);
1321 ifindex_t ifindex
= IFINDEX_INTERNAL
;
1322 struct in6_addr
*nexthop
;
1324 if (bgp
->table_map
[afi
][safi
].name
) {
1325 /* Copy info and attributes, so the route-map
1326 apply doesn't modify the BGP route info. */
1327 local_attr
= *mpinfo
->attr
;
1328 mpinfo_cp
->attr
= &local_attr
;
1331 if (bgp
->table_map
[afi
][safi
].name
) {
1332 /* Copy info and attributes, so the route-map
1333 apply doesn't modify the BGP route info. */
1334 local_attr
= *mpinfo
->attr
;
1335 mpinfo_cp
->attr
= &local_attr
;
1337 if (!bgp_table_map_apply(
1338 bgp
->table_map
[afi
][safi
].map
, p
,
1342 /* metric/tag is only allowed to be
1343 * overridden on 1st nexthop */
1344 if (mpinfo
== info
) {
1345 metric
= mpinfo_cp
->attr
->med
;
1346 tag
= mpinfo_cp
->attr
->tag
;
1349 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo_cp
,
1351 nh_updated
= update_ipv6nh_for_route_install(
1352 nh_othervrf
, nexthop
, ifindex
,
1353 mpinfo
, info
, is_evpn
, api_nh
);
1356 /* Did we get proper nexthop info to update zebra? */
1361 && bgp_is_valid_label(&mpinfo
->extra
->label
[0])
1362 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1363 has_valid_label
= 1;
1364 label
= label_pton(&mpinfo
->extra
->label
[0]);
1366 api_nh
->label_num
= 1;
1367 api_nh
->labels
[0] = label
;
1369 memcpy(&api_nh
->rmac
, &(mpinfo
->attr
->rmac
),
1370 sizeof(struct ethaddr
));
1375 /* if this is a evpn route we don't have to include the label */
1376 if (has_valid_label
&& !(CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)))
1377 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
1380 * When we create an aggregate route we must also
1381 * install a Null0 route in the RIB, so overwrite
1382 * what was written into api with a blackhole route
1384 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1385 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
1387 api
.nexthop_num
= valid_nh_count
;
1389 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1390 api
.metric
= metric
;
1393 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1397 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1399 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1400 api
.distance
= distance
;
1403 if (bgp_debug_zebra(p
)) {
1404 char prefix_buf
[PREFIX_STRLEN
];
1405 char nh_buf
[INET6_ADDRSTRLEN
];
1409 prefix2str(&api
.prefix
, prefix_buf
, sizeof(prefix_buf
));
1410 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1412 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1413 prefix_buf
, api
.metric
, api
.tag
, api
.nexthop_num
);
1414 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1415 api_nh
= &api
.nexthops
[i
];
1417 if (api_nh
->type
== NEXTHOP_TYPE_IFINDEX
)
1420 if (api_nh
->type
== NEXTHOP_TYPE_IPV4
)
1421 nh_family
= AF_INET
;
1423 nh_family
= AF_INET6
;
1424 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1428 label_buf
[0] = '\0';
1430 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1431 sprintf(label_buf
, "label %u",
1433 zlog_debug(" nhop [%d]: %s if %u VRF %u %s",
1434 i
+ 1, nh_buf
, api_nh
->ifindex
,
1435 api_nh
->vrf_id
, label_buf
);
1439 if (bgp_debug_zebra(p
)) {
1440 int recursion_flag
= 0;
1442 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
))
1445 zlog_debug("%s: %s: announcing to zebra (recursion %sset)",
1446 __func__
, buf_prefix
,
1447 (recursion_flag
? "" : "NOT "));
1449 zclient_route_send(valid_nh_count
? ZEBRA_ROUTE_ADD
1450 : ZEBRA_ROUTE_DELETE
,
1454 /* Announce all routes of a table to zebra */
1455 void bgp_zebra_announce_table(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1457 struct bgp_node
*rn
;
1458 struct bgp_table
*table
;
1459 struct bgp_info
*ri
;
1461 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1462 * know of this instance.
1464 if (!bgp_install_info_to_zebra(bgp
))
1467 table
= bgp
->rib
[afi
][safi
];
1471 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
1472 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1473 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
) &&
1475 (ri
->type
== ZEBRA_ROUTE_BGP
1476 && (ri
->sub_type
== BGP_ROUTE_NORMAL
1477 || ri
->sub_type
== BGP_ROUTE_IMPORTED
)))
1479 bgp_zebra_announce(rn
, &rn
->p
, ri
, bgp
, afi
,
1483 void bgp_zebra_withdraw(struct prefix
*p
, struct bgp_info
*info
,
1484 struct bgp
*bgp
, safi_t safi
)
1486 struct zapi_route api
;
1489 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1490 * know of this instance.
1492 if (!bgp_install_info_to_zebra(bgp
))
1495 if (safi
== SAFI_FLOWSPEC
) {
1497 return bgp_pbr_update_entry(peer
->bgp
, p
,
1498 info
, AFI_IP
, safi
, false);
1501 memset(&api
, 0, sizeof(api
));
1502 api
.vrf_id
= bgp
->vrf_id
;
1503 api
.type
= ZEBRA_ROUTE_BGP
;
1507 /* If the route's source is EVPN, flag as such. */
1508 if (is_route_parent_evpn(info
))
1509 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1511 if (bgp_debug_zebra(p
)) {
1512 char buf
[PREFIX_STRLEN
];
1514 prefix2str(&api
.prefix
, buf
, sizeof(buf
));
1515 zlog_debug("Tx route delete VRF %u %s", bgp
->vrf_id
, buf
);
1518 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
1521 struct bgp_redist
*bgp_redist_lookup(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1522 unsigned short instance
)
1524 struct list
*red_list
;
1525 struct listnode
*node
;
1526 struct bgp_redist
*red
;
1528 red_list
= bgp
->redist
[afi
][type
];
1532 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1533 if (red
->instance
== instance
)
1539 struct bgp_redist
*bgp_redist_add(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1540 unsigned short instance
)
1542 struct list
*red_list
;
1543 struct bgp_redist
*red
;
1545 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1549 if (!bgp
->redist
[afi
][type
])
1550 bgp
->redist
[afi
][type
] = list_new();
1552 red_list
= bgp
->redist
[afi
][type
];
1553 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
,
1554 sizeof(struct bgp_redist
));
1555 red
->instance
= instance
;
1557 listnode_add(red_list
, red
);
1562 static void bgp_redist_del(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1563 unsigned short instance
)
1565 struct bgp_redist
*red
;
1567 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1570 listnode_delete(bgp
->redist
[afi
][type
], red
);
1571 XFREE(MTYPE_BGP_REDIST
, red
);
1572 if (!bgp
->redist
[afi
][type
]->count
)
1573 list_delete_and_null(&bgp
->redist
[afi
][type
]);
1577 /* Other routes redistribution into BGP. */
1578 int bgp_redistribute_set(struct bgp
*bgp
, afi_t afi
, int type
,
1579 unsigned short instance
, bool changed
)
1581 /* If redistribute options are changed call
1582 * bgp_redistribute_unreg() to reset the option and withdraw
1586 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1588 /* Return if already redistribute flag is set. */
1590 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1594 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1596 if (vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1600 if (bgp
->vrf_id
== VRF_DEFAULT
1601 && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1602 vnc_export_bgp_enable(
1603 bgp
, afi
); /* only enables if mode bits cfg'd */
1607 vrf_bitmap_set(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1611 * Don't try to register if we're not connected to Zebra or Zebra
1612 * doesn't know of this instance.
1614 * When we come up later well resend if needed.
1616 if (!bgp_install_info_to_zebra(bgp
))
1619 if (BGP_DEBUG(zebra
, ZEBRA
))
1620 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1621 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1624 /* Send distribute add message to zebra. */
1625 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1626 instance
, bgp
->vrf_id
);
1631 int bgp_redistribute_resend(struct bgp
*bgp
, afi_t afi
, int type
,
1632 unsigned short instance
)
1634 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1635 * know of this instance.
1637 if (!bgp_install_info_to_zebra(bgp
))
1640 if (BGP_DEBUG(zebra
, ZEBRA
))
1641 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1642 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1645 /* Send distribute add message to zebra. */
1646 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1647 instance
, bgp
->vrf_id
);
1648 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1649 instance
, bgp
->vrf_id
);
1654 /* Redistribute with route-map specification. */
1655 int bgp_redistribute_rmap_set(struct bgp_redist
*red
, const char *name
)
1657 if (red
->rmap
.name
&& (strcmp(red
->rmap
.name
, name
) == 0))
1661 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1662 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1663 red
->rmap
.map
= route_map_lookup_by_name(name
);
1668 /* Redistribute with metric specification. */
1669 int bgp_redistribute_metric_set(struct bgp
*bgp
, struct bgp_redist
*red
,
1670 afi_t afi
, int type
, uint32_t metric
)
1672 struct bgp_node
*rn
;
1673 struct bgp_info
*ri
;
1675 if (red
->redist_metric_flag
&& red
->redist_metric
== metric
)
1678 red
->redist_metric_flag
= 1;
1679 red
->redist_metric
= metric
;
1681 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
1682 rn
= bgp_route_next(rn
)) {
1683 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
1684 if (ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
1686 && ri
->instance
== red
->instance
) {
1687 struct attr
*old_attr
;
1688 struct attr new_attr
;
1690 bgp_attr_dup(&new_attr
, ri
->attr
);
1691 new_attr
.med
= red
->redist_metric
;
1692 old_attr
= ri
->attr
;
1693 ri
->attr
= bgp_attr_intern(&new_attr
);
1694 bgp_attr_unintern(&old_attr
);
1696 bgp_info_set_flag(rn
, ri
,
1697 BGP_INFO_ATTR_CHANGED
);
1698 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1706 /* Unset redistribution. */
1707 int bgp_redistribute_unreg(struct bgp
*bgp
, afi_t afi
, int type
,
1708 unsigned short instance
)
1710 struct bgp_redist
*red
;
1712 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1716 /* Return if zebra connection is disabled. */
1718 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1721 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1723 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1725 vrf_bitmap_unset(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1729 if (bgp_install_info_to_zebra(bgp
)) {
1730 /* Send distribute delete message to zebra. */
1731 if (BGP_DEBUG(zebra
, ZEBRA
))
1732 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1733 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1735 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
1736 type
, instance
, bgp
->vrf_id
);
1739 /* Withdraw redistributed routes from current BGP's routing table. */
1740 bgp_redistribute_withdraw(bgp
, afi
, type
, instance
);
1745 /* Unset redistribution. */
1746 int bgp_redistribute_unset(struct bgp
*bgp
, afi_t afi
, int type
,
1747 unsigned short instance
)
1749 struct bgp_redist
*red
;
1752 * vnc and vpn->vrf checks must be before red check because
1753 * they operate within bgpd irrespective of zebra connection
1754 * status. red lookup fails if there is no zebra connection.
1757 if (bgp
->vrf_id
== VRF_DEFAULT
&& type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1758 vnc_export_bgp_disable(bgp
, afi
);
1762 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1766 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1768 /* Unset route-map. */
1770 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1771 red
->rmap
.name
= NULL
;
1772 red
->rmap
.map
= NULL
;
1775 red
->redist_metric_flag
= 0;
1776 red
->redist_metric
= 0;
1778 bgp_redist_del(bgp
, afi
, type
, instance
);
1783 /* Update redistribute vrf bitmap during triggers like
1784 restart networking or delete/add VRFs */
1785 void bgp_update_redist_vrf_bitmaps(struct bgp
*bgp
, vrf_id_t old_vrf_id
)
1790 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1791 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1792 if ((old_vrf_id
== VRF_UNKNOWN
)
1793 || vrf_bitmap_check(zclient
->redist
[afi
][i
],
1795 vrf_bitmap_unset(zclient
->redist
[afi
][i
],
1797 vrf_bitmap_set(zclient
->redist
[afi
][i
],
1803 void bgp_zclient_reset(void)
1805 zclient_reset(zclient
);
1808 /* Register this instance with Zebra. Invoked upon connect (for
1809 * default instance) and when other VRFs are learnt (or created and
1812 void bgp_zebra_instance_register(struct bgp
*bgp
)
1814 /* Don't try to register if we're not connected to Zebra */
1815 if (!zclient
|| zclient
->sock
< 0)
1818 if (BGP_DEBUG(zebra
, ZEBRA
))
1819 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
1821 /* Register for router-id, interfaces, redistributed routes. */
1822 zclient_send_reg_requests(zclient
, bgp
->vrf_id
);
1824 /* For default instance, register to learn about VNIs, if appropriate.
1826 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& is_evpn_enabled())
1827 bgp_zebra_advertise_all_vni(bgp
, 1);
1830 /* Deregister this instance with Zebra. Invoked upon the instance
1831 * being deleted (default or VRF) and it is already registered.
1833 void bgp_zebra_instance_deregister(struct bgp
*bgp
)
1835 /* Don't try to deregister if we're not connected to Zebra */
1836 if (zclient
->sock
< 0)
1839 if (BGP_DEBUG(zebra
, ZEBRA
))
1840 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
1842 /* For default instance, unregister learning about VNIs, if appropriate.
1844 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& is_evpn_enabled())
1845 bgp_zebra_advertise_all_vni(bgp
, 0);
1847 /* Deregister for router-id, interfaces, redistributed routes. */
1848 zclient_send_dereg_requests(zclient
, bgp
->vrf_id
);
1851 void bgp_zebra_initiate_radv(struct bgp
*bgp
, struct peer
*peer
)
1853 int ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
1855 /* Don't try to initiate if we're not connected to Zebra */
1856 if (zclient
->sock
< 0)
1859 if (BGP_DEBUG(zebra
, ZEBRA
))
1860 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
,
1863 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 1,
1867 void bgp_zebra_terminate_radv(struct bgp
*bgp
, struct peer
*peer
)
1869 /* Don't try to terminate if we're not connected to Zebra */
1870 if (zclient
->sock
< 0)
1873 if (BGP_DEBUG(zebra
, ZEBRA
))
1874 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
,
1877 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 0, 0);
1880 int bgp_zebra_advertise_subnet(struct bgp
*bgp
, int advertise
, vni_t vni
)
1882 struct stream
*s
= NULL
;
1885 if (!zclient
|| zclient
->sock
< 0)
1888 /* Don't try to register if Zebra doesn't know of this instance. */
1889 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1895 zclient_create_header(s
, ZEBRA_ADVERTISE_SUBNET
, bgp
->vrf_id
);
1896 stream_putc(s
, advertise
);
1897 stream_put3(s
, vni
);
1898 stream_putw_at(s
, 0, stream_get_endp(s
));
1900 return zclient_send_message(zclient
);
1903 int bgp_zebra_advertise_gw_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
1905 struct stream
*s
= NULL
;
1908 if (!zclient
|| zclient
->sock
< 0)
1911 /* Don't try to register if Zebra doesn't know of this instance. */
1912 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1918 zclient_create_header(s
, ZEBRA_ADVERTISE_DEFAULT_GW
, bgp
->vrf_id
);
1919 stream_putc(s
, advertise
);
1920 stream_putl(s
, vni
);
1921 stream_putw_at(s
, 0, stream_get_endp(s
));
1923 return zclient_send_message(zclient
);
1926 int bgp_zebra_advertise_all_vni(struct bgp
*bgp
, int advertise
)
1931 if (!zclient
|| zclient
->sock
< 0)
1934 /* Don't try to register if Zebra doesn't know of this instance. */
1935 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1941 zclient_create_header(s
, ZEBRA_ADVERTISE_ALL_VNI
, bgp
->vrf_id
);
1942 stream_putc(s
, advertise
);
1943 stream_putw_at(s
, 0, stream_get_endp(s
));
1945 return zclient_send_message(zclient
);
1948 static int rule_notify_owner(int command
, struct zclient
*zclient
,
1949 zebra_size_t length
, vrf_id_t vrf_id
)
1951 uint32_t seqno
, priority
, unique
;
1952 enum zapi_rule_notify_owner note
;
1953 struct bgp_pbr_action
*bgp_pbra
;
1956 if (!zapi_rule_notify_decode(zclient
->ibuf
, &seqno
, &priority
, &unique
,
1960 bgp_pbra
= bgp_pbr_action_rule_lookup(vrf_id
, unique
);
1962 if (BGP_DEBUG(zebra
, ZEBRA
))
1963 zlog_debug("%s: Fail to look BGP rule (%u)",
1964 __PRETTY_FUNCTION__
, unique
);
1969 case ZAPI_RULE_FAIL_INSTALL
:
1970 if (BGP_DEBUG(zebra
, ZEBRA
))
1971 zlog_debug("%s: Received RULE_FAIL_INSTALL",
1972 __PRETTY_FUNCTION__
);
1973 bgp_pbra
->installed
= false;
1974 bgp_pbra
->install_in_progress
= false;
1976 case ZAPI_RULE_INSTALLED
:
1977 bgp_pbra
->installed
= true;
1978 bgp_pbra
->install_in_progress
= false;
1979 if (BGP_DEBUG(zebra
, ZEBRA
))
1980 zlog_debug("%s: Received RULE_INSTALLED",
1981 __PRETTY_FUNCTION__
);
1983 case ZAPI_RULE_FAIL_REMOVE
:
1984 case ZAPI_RULE_REMOVED
:
1985 if (BGP_DEBUG(zebra
, ZEBRA
))
1986 zlog_debug("%s: Received RULE REMOVED",
1987 __PRETTY_FUNCTION__
);
1994 static int ipset_notify_owner(int command
, struct zclient
*zclient
,
1995 zebra_size_t length
, vrf_id_t vrf_id
)
1998 enum zapi_ipset_notify_owner note
;
1999 struct bgp_pbr_match
*bgp_pbim
;
2001 if (!zapi_ipset_notify_decode(zclient
->ibuf
,
2006 bgp_pbim
= bgp_pbr_match_ipset_lookup(vrf_id
, unique
);
2008 if (BGP_DEBUG(zebra
, ZEBRA
))
2009 zlog_debug("%s: Fail to look BGP match ( %u, ID %u)",
2010 __PRETTY_FUNCTION__
, note
, unique
);
2015 case ZAPI_IPSET_FAIL_INSTALL
:
2016 if (BGP_DEBUG(zebra
, ZEBRA
))
2017 zlog_debug("%s: Received IPSET_FAIL_INSTALL",
2018 __PRETTY_FUNCTION__
);
2019 bgp_pbim
->installed
= false;
2020 bgp_pbim
->install_in_progress
= false;
2022 case ZAPI_IPSET_INSTALLED
:
2023 bgp_pbim
->installed
= true;
2024 bgp_pbim
->install_in_progress
= false;
2025 if (BGP_DEBUG(zebra
, ZEBRA
))
2026 zlog_debug("%s: Received IPSET_INSTALLED",
2027 __PRETTY_FUNCTION__
);
2029 case ZAPI_IPSET_FAIL_REMOVE
:
2030 case ZAPI_IPSET_REMOVED
:
2031 if (BGP_DEBUG(zebra
, ZEBRA
))
2032 zlog_debug("%s: Received IPSET REMOVED",
2033 __PRETTY_FUNCTION__
);
2040 static int ipset_entry_notify_owner(int command
, struct zclient
*zclient
,
2041 zebra_size_t length
, vrf_id_t vrf_id
)
2044 char ipset_name
[ZEBRA_IPSET_NAME_SIZE
];
2045 enum zapi_ipset_entry_notify_owner note
;
2046 struct bgp_pbr_match_entry
*bgp_pbime
;
2048 if (!zapi_ipset_entry_notify_decode(
2054 bgp_pbime
= bgp_pbr_match_ipset_entry_lookup(vrf_id
,
2058 if (BGP_DEBUG(zebra
, ZEBRA
))
2059 zlog_debug("%s: Fail to look BGP match entry (%u, ID %u)",
2060 __PRETTY_FUNCTION__
, note
, unique
);
2065 case ZAPI_IPSET_ENTRY_FAIL_INSTALL
:
2066 if (BGP_DEBUG(zebra
, ZEBRA
))
2067 zlog_debug("%s: Received IPSET_ENTRY_FAIL_INSTALL",
2068 __PRETTY_FUNCTION__
);
2069 bgp_pbime
->installed
= false;
2070 bgp_pbime
->install_in_progress
= false;
2072 case ZAPI_IPSET_ENTRY_INSTALLED
:
2074 struct bgp_info
*bgp_info
;
2075 struct bgp_info_extra
*extra
;
2077 bgp_pbime
->installed
= true;
2078 bgp_pbime
->install_in_progress
= false;
2079 if (BGP_DEBUG(zebra
, ZEBRA
))
2080 zlog_debug("%s: Received IPSET_ENTRY_INSTALLED",
2081 __PRETTY_FUNCTION__
);
2082 /* link bgp_info to bpme */
2083 bgp_info
= (struct bgp_info
*)bgp_pbime
->bgp_info
;
2084 extra
= bgp_info_extra_get(bgp_info
);
2085 if (extra
->bgp_fs_pbr
== NULL
)
2086 extra
->bgp_fs_pbr
= list_new();
2087 listnode_add(extra
->bgp_fs_pbr
, bgp_pbime
);
2090 case ZAPI_IPSET_ENTRY_FAIL_REMOVE
:
2091 case ZAPI_IPSET_ENTRY_REMOVED
:
2092 if (BGP_DEBUG(zebra
, ZEBRA
))
2093 zlog_debug("%s: Received IPSET_ENTRY_REMOVED",
2094 __PRETTY_FUNCTION__
);
2100 static int iptable_notify_owner(int command
, struct zclient
*zclient
,
2101 zebra_size_t length
, vrf_id_t vrf_id
)
2104 enum zapi_iptable_notify_owner note
;
2105 struct bgp_pbr_match
*bgpm
;
2107 if (!zapi_iptable_notify_decode(
2112 bgpm
= bgp_pbr_match_iptable_lookup(vrf_id
, unique
);
2114 if (BGP_DEBUG(zebra
, ZEBRA
))
2115 zlog_debug("%s: Fail to look BGP iptable (%u %u)",
2116 __PRETTY_FUNCTION__
, note
, unique
);
2120 case ZAPI_IPTABLE_FAIL_INSTALL
:
2121 if (BGP_DEBUG(zebra
, ZEBRA
))
2122 zlog_debug("%s: Received IPTABLE_FAIL_INSTALL",
2123 __PRETTY_FUNCTION__
);
2124 bgpm
->installed_in_iptable
= false;
2125 bgpm
->install_iptable_in_progress
= false;
2127 case ZAPI_IPTABLE_INSTALLED
:
2128 bgpm
->installed_in_iptable
= true;
2129 bgpm
->install_iptable_in_progress
= false;
2130 if (BGP_DEBUG(zebra
, ZEBRA
))
2131 zlog_debug("%s: Received IPTABLE_INSTALLED",
2132 __PRETTY_FUNCTION__
);
2133 bgpm
->action
->refcnt
++;
2135 case ZAPI_IPTABLE_FAIL_REMOVE
:
2136 case ZAPI_IPTABLE_REMOVED
:
2137 if (BGP_DEBUG(zebra
, ZEBRA
))
2138 zlog_debug("%s: Received IPTABLE REMOVED",
2139 __PRETTY_FUNCTION__
);
2145 static void bgp_encode_pbr_rule_action(struct stream
*s
,
2146 struct bgp_pbr_action
*pbra
)
2150 stream_putl(s
, 0); /* seqno unused */
2151 stream_putl(s
, 0); /* ruleno unused */
2153 stream_putl(s
, pbra
->unique
);
2155 memset(&any
, 0, sizeof(any
));
2156 any
.family
= AF_INET
;
2157 stream_putc(s
, any
.family
);
2158 stream_putc(s
, any
.prefixlen
);
2159 stream_put(s
, &any
.u
.prefix
, prefix_blen(&any
));
2161 stream_putw(s
, 0); /* src port */
2163 stream_putc(s
, any
.family
);
2164 stream_putc(s
, any
.prefixlen
);
2165 stream_put(s
, &any
.u
.prefix
, prefix_blen(&any
));
2167 stream_putw(s
, 0); /* dst port */
2169 stream_putl(s
, pbra
->fwmark
); /* fwmark */
2171 stream_putl(s
, pbra
->table_id
);
2173 stream_putl(s
, 0); /* ifindex unused */
2176 static void bgp_encode_pbr_ipset_match(struct stream
*s
,
2177 struct bgp_pbr_match
*pbim
)
2179 stream_putl(s
, pbim
->unique
);
2180 stream_putl(s
, pbim
->type
);
2182 stream_put(s
, pbim
->ipset_name
,
2183 ZEBRA_IPSET_NAME_SIZE
);
2188 static void bgp_encode_pbr_ipset_entry_match(struct stream
*s
,
2189 struct bgp_pbr_match_entry
*pbime
)
2191 stream_putl(s
, pbime
->unique
);
2192 /* check that back pointer is not null */
2193 stream_put(s
, pbime
->backpointer
->ipset_name
,
2194 ZEBRA_IPSET_NAME_SIZE
);
2196 stream_putc(s
, pbime
->src
.family
);
2197 stream_putc(s
, pbime
->src
.prefixlen
);
2198 stream_put(s
, &pbime
->src
.u
.prefix
, prefix_blen(&pbime
->src
));
2200 stream_putc(s
, pbime
->dst
.family
);
2201 stream_putc(s
, pbime
->dst
.prefixlen
);
2202 stream_put(s
, &pbime
->dst
.u
.prefix
, prefix_blen(&pbime
->dst
));
2204 stream_putw(s
, pbime
->src_port_min
);
2205 stream_putw(s
, pbime
->src_port_max
);
2206 stream_putw(s
, pbime
->dst_port_min
);
2207 stream_putw(s
, pbime
->dst_port_max
);
2208 stream_putc(s
, pbime
->proto
);
2211 static void bgp_encode_pbr_iptable_match(struct stream
*s
,
2212 struct bgp_pbr_action
*bpa
,
2213 struct bgp_pbr_match
*pbm
)
2215 stream_putl(s
, pbm
->unique2
);
2217 stream_putl(s
, pbm
->type
);
2219 stream_putl(s
, pbm
->flags
);
2221 /* TODO: correlate with what is contained
2222 * into bgp_pbr_action.
2223 * currently only forward supported
2225 if (bpa
->nh
.type
== NEXTHOP_TYPE_BLACKHOLE
)
2226 stream_putl(s
, ZEBRA_IPTABLES_DROP
);
2228 stream_putl(s
, ZEBRA_IPTABLES_FORWARD
);
2229 stream_putl(s
, bpa
->fwmark
);
2230 stream_put(s
, pbm
->ipset_name
,
2231 ZEBRA_IPSET_NAME_SIZE
);
2232 stream_putw(s
, pbm
->pkt_len_min
);
2233 stream_putw(s
, pbm
->pkt_len_max
);
2234 stream_putw(s
, pbm
->tcp_flags
);
2235 stream_putw(s
, pbm
->tcp_mask_flags
);
2236 stream_putc(s
, pbm
->dscp_value
);
2237 stream_putc(s
, pbm
->fragment
);
2240 /* BGP has established connection with Zebra. */
2241 static void bgp_zebra_connected(struct zclient
*zclient
)
2245 zclient_num_connects
++; /* increment even if not responding */
2247 /* At this point, we may or may not have BGP instances configured, but
2248 * we're only interested in the default VRF (others wouldn't have learnt
2249 * the VRF from Zebra yet.)
2251 bgp
= bgp_get_default();
2255 bgp_zebra_instance_register(bgp
);
2257 /* Send the client registration */
2258 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
2260 /* tell label pool that zebra is connected */
2261 bgp_lp_event_zebra_up();
2263 /* TODO - What if we have peers and networks configured, do we have to
2268 static int bgp_zebra_process_local_es(int cmd
, struct zclient
*zclient
,
2269 zebra_size_t length
, vrf_id_t vrf_id
)
2272 struct bgp
*bgp
= NULL
;
2273 struct stream
*s
= NULL
;
2274 char buf
[ESI_STR_LEN
];
2275 char buf1
[INET6_ADDRSTRLEN
];
2276 struct ipaddr originator_ip
;
2278 memset(&esi
, 0, sizeof(esi_t
));
2279 memset(&originator_ip
, 0, sizeof(struct ipaddr
));
2281 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2286 stream_get(&esi
, s
, sizeof(esi_t
));
2287 stream_get(&originator_ip
, s
, sizeof(struct ipaddr
));
2289 if (BGP_DEBUG(zebra
, ZEBRA
))
2290 zlog_debug("Rx %s ESI %s originator-ip %s",
2291 (cmd
== ZEBRA_LOCAL_ES_ADD
) ? "add" : "del",
2292 esi_to_str(&esi
, buf
, sizeof(buf
)),
2293 ipaddr2str(&originator_ip
, buf1
, sizeof(buf1
)));
2295 if (cmd
== ZEBRA_LOCAL_ES_ADD
)
2296 bgp_evpn_local_es_add(bgp
, &esi
, &originator_ip
);
2298 bgp_evpn_local_es_del(bgp
, &esi
, &originator_ip
);
2302 static int bgp_zebra_process_local_l3vni(int cmd
, struct zclient
*zclient
,
2303 zebra_size_t length
, vrf_id_t vrf_id
)
2306 char buf
[ETHER_ADDR_STRLEN
];
2308 struct ethaddr rmac
;
2309 struct in_addr originator_ip
;
2312 memset(&rmac
, 0, sizeof(struct ethaddr
));
2313 memset(&originator_ip
, 0, sizeof(struct in_addr
));
2315 l3vni
= stream_getl(s
);
2316 if (cmd
== ZEBRA_L3VNI_ADD
) {
2317 stream_get(&rmac
, s
, sizeof(struct ethaddr
));
2318 originator_ip
.s_addr
= stream_get_ipv4(s
);
2319 stream_get(&filter
, s
, sizeof(int));
2322 if (BGP_DEBUG(zebra
, ZEBRA
))
2323 zlog_debug("Rx L3-VNI %s VRF %s VNI %u RMAC %s filter %s",
2324 (cmd
== ZEBRA_L3VNI_ADD
) ? "add" : "del",
2325 vrf_id_to_name(vrf_id
), l3vni
,
2326 prefix_mac2str(&rmac
, buf
, sizeof(buf
)),
2327 filter
? "prefix-routes-only" : "none");
2329 if (cmd
== ZEBRA_L3VNI_ADD
)
2330 bgp_evpn_local_l3vni_add(l3vni
, vrf_id
, &rmac
, originator_ip
,
2333 bgp_evpn_local_l3vni_del(l3vni
, vrf_id
);
2338 static int bgp_zebra_process_local_vni(int command
, struct zclient
*zclient
,
2339 zebra_size_t length
, vrf_id_t vrf_id
)
2344 struct in_addr vtep_ip
= {INADDR_ANY
};
2345 vrf_id_t tenant_vrf_id
= VRF_DEFAULT
;
2348 vni
= stream_getl(s
);
2349 if (command
== ZEBRA_VNI_ADD
) {
2350 vtep_ip
.s_addr
= stream_get_ipv4(s
);
2351 stream_get(&tenant_vrf_id
, s
, sizeof(vrf_id_t
));
2354 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2358 if (BGP_DEBUG(zebra
, ZEBRA
))
2359 zlog_debug("Rx VNI %s VRF %s VNI %u tenant-vrf %s",
2360 (command
== ZEBRA_VNI_ADD
) ? "add" : "del",
2361 vrf_id_to_name(vrf_id
), vni
,
2362 vrf_id_to_name(tenant_vrf_id
));
2364 if (command
== ZEBRA_VNI_ADD
)
2365 return bgp_evpn_local_vni_add(
2366 bgp
, vni
, vtep_ip
.s_addr
? vtep_ip
: bgp
->router_id
,
2369 return bgp_evpn_local_vni_del(bgp
, vni
);
2372 static int bgp_zebra_process_local_macip(int command
, struct zclient
*zclient
,
2373 zebra_size_t length
, vrf_id_t vrf_id
)
2381 char buf
[ETHER_ADDR_STRLEN
];
2382 char buf1
[INET6_ADDRSTRLEN
];
2384 uint32_t seqnum
= 0;
2386 memset(&ip
, 0, sizeof(ip
));
2388 vni
= stream_getl(s
);
2389 stream_get(&mac
.octet
, s
, ETH_ALEN
);
2390 ipa_len
= stream_getl(s
);
2391 if (ipa_len
!= 0 && ipa_len
!= IPV4_MAX_BYTELEN
2392 && ipa_len
!= IPV6_MAX_BYTELEN
) {
2393 flog_err(BGP_ERR_MACIP_LEN
,
2394 "%u:Recv MACIP %s with invalid IP addr length %d",
2395 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
2402 (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
: IPADDR_V6
;
2403 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
2405 if (command
== ZEBRA_MACIP_ADD
) {
2406 flags
= stream_getc(s
);
2407 seqnum
= stream_getl(s
);
2410 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2414 if (BGP_DEBUG(zebra
, ZEBRA
))
2415 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u seq %u",
2416 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
2417 flags
, prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2418 ipaddr2str(&ip
, buf1
, sizeof(buf1
)), vni
, seqnum
);
2420 if (command
== ZEBRA_MACIP_ADD
)
2421 return bgp_evpn_local_macip_add(bgp
, vni
, &mac
, &ip
,
2424 return bgp_evpn_local_macip_del(bgp
, vni
, &mac
, &ip
);
2427 static void bgp_zebra_process_local_ip_prefix(int cmd
, struct zclient
*zclient
,
2428 zebra_size_t length
,
2431 struct stream
*s
= NULL
;
2432 struct bgp
*bgp_vrf
= NULL
;
2434 char buf
[PREFIX_STRLEN
];
2436 memset(&p
, 0, sizeof(struct prefix
));
2438 stream_get(&p
, s
, sizeof(struct prefix
));
2440 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
2444 if (BGP_DEBUG(zebra
, ZEBRA
))
2445 zlog_debug("Recv prefix %s %s on vrf %s",
2446 prefix2str(&p
, buf
, sizeof(buf
)),
2447 (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) ? "ADD" : "DEL",
2448 vrf_id_to_name(vrf_id
));
2450 if (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) {
2452 if (p
.family
== AF_INET
)
2453 return bgp_evpn_advertise_type5_route(
2454 bgp_vrf
, &p
, NULL
, AFI_IP
, SAFI_UNICAST
);
2456 return bgp_evpn_advertise_type5_route(
2457 bgp_vrf
, &p
, NULL
, AFI_IP6
, SAFI_UNICAST
);
2460 if (p
.family
== AF_INET
)
2461 return bgp_evpn_withdraw_type5_route(
2462 bgp_vrf
, &p
, AFI_IP
, SAFI_UNICAST
);
2464 return bgp_evpn_withdraw_type5_route(
2465 bgp_vrf
, &p
, AFI_IP6
, SAFI_UNICAST
);
2469 static void bgp_zebra_process_label_chunk(
2471 struct zclient
*zclient
,
2472 zebra_size_t length
,
2475 struct stream
*s
= NULL
;
2476 uint8_t response_keep
;
2480 unsigned short instance
;
2483 STREAM_GETC(s
, proto
);
2484 STREAM_GETW(s
, instance
);
2485 STREAM_GETC(s
, response_keep
);
2486 STREAM_GETL(s
, first
);
2487 STREAM_GETL(s
, last
);
2489 if (zclient
->redist_default
!= proto
) {
2490 flog_err(BGP_ERR_LM_ERROR
, "Got LM msg with wrong proto %u",
2494 if (zclient
->instance
!= instance
) {
2495 flog_err(BGP_ERR_LM_ERROR
, "Got LM msg with wrong instance %u",
2501 first
< MPLS_LABEL_UNRESERVED_MIN
||
2502 last
> MPLS_LABEL_UNRESERVED_MAX
) {
2504 flog_err(BGP_ERR_LM_ERROR
, "%s: Invalid Label chunk: %u - %u",
2505 __func__
, first
, last
);
2508 if (BGP_DEBUG(zebra
, ZEBRA
)) {
2509 zlog_debug("Label Chunk assign: %u - %u (%u) ",
2510 first
, last
, response_keep
);
2513 bgp_lp_event_chunk(response_keep
, first
, last
);
2515 stream_failure
: /* for STREAM_GETX */
2519 extern struct zebra_privs_t bgpd_privs
;
2521 void bgp_zebra_init(struct thread_master
*master
)
2523 zclient_num_connects
= 0;
2525 /* Set default values. */
2526 zclient
= zclient_new_notify(master
, &zclient_options_default
);
2527 zclient_init(zclient
, ZEBRA_ROUTE_BGP
, 0, &bgpd_privs
);
2528 zclient
->zebra_connected
= bgp_zebra_connected
;
2529 zclient
->router_id_update
= bgp_router_id_update
;
2530 zclient
->interface_add
= bgp_interface_add
;
2531 zclient
->interface_delete
= bgp_interface_delete
;
2532 zclient
->interface_address_add
= bgp_interface_address_add
;
2533 zclient
->interface_address_delete
= bgp_interface_address_delete
;
2534 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
2535 zclient
->interface_nbr_address_delete
=
2536 bgp_interface_nbr_address_delete
;
2537 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
2538 zclient
->redistribute_route_add
= zebra_read_route
;
2539 zclient
->redistribute_route_del
= zebra_read_route
;
2540 zclient
->interface_up
= bgp_interface_up
;
2541 zclient
->interface_down
= bgp_interface_down
;
2542 zclient
->nexthop_update
= bgp_read_nexthop_update
;
2543 zclient
->import_check_update
= bgp_read_import_check_update
;
2544 zclient
->fec_update
= bgp_read_fec_update
;
2545 zclient
->local_es_add
= bgp_zebra_process_local_es
;
2546 zclient
->local_es_del
= bgp_zebra_process_local_es
;
2547 zclient
->local_vni_add
= bgp_zebra_process_local_vni
;
2548 zclient
->local_vni_del
= bgp_zebra_process_local_vni
;
2549 zclient
->local_macip_add
= bgp_zebra_process_local_macip
;
2550 zclient
->local_macip_del
= bgp_zebra_process_local_macip
;
2551 zclient
->local_l3vni_add
= bgp_zebra_process_local_l3vni
;
2552 zclient
->local_l3vni_del
= bgp_zebra_process_local_l3vni
;
2553 zclient
->local_ip_prefix_add
= bgp_zebra_process_local_ip_prefix
;
2554 zclient
->local_ip_prefix_del
= bgp_zebra_process_local_ip_prefix
;
2555 zclient
->label_chunk
= bgp_zebra_process_label_chunk
;
2556 zclient
->rule_notify_owner
= rule_notify_owner
;
2557 zclient
->ipset_notify_owner
= ipset_notify_owner
;
2558 zclient
->ipset_entry_notify_owner
= ipset_entry_notify_owner
;
2559 zclient
->iptable_notify_owner
= iptable_notify_owner
;
2562 void bgp_zebra_destroy(void)
2564 if (zclient
== NULL
)
2566 zclient_stop(zclient
);
2567 zclient_free(zclient
);
2571 int bgp_zebra_num_connects(void)
2573 return zclient_num_connects
;
2576 void bgp_send_pbr_rule_action(struct bgp_pbr_action
*pbra
, bool install
)
2580 if (pbra
->install_in_progress
)
2582 if (BGP_DEBUG(zebra
, ZEBRA
))
2583 zlog_debug("%s: table %d fwmark %d %d",
2584 __PRETTY_FUNCTION__
,
2585 pbra
->table_id
, pbra
->fwmark
, install
);
2589 zclient_create_header(s
,
2590 install
? ZEBRA_RULE_ADD
: ZEBRA_RULE_DELETE
,
2592 stream_putl(s
, 1); /* send one pbr action */
2594 bgp_encode_pbr_rule_action(s
, pbra
);
2596 stream_putw_at(s
, 0, stream_get_endp(s
));
2597 if (!zclient_send_message(zclient
) && install
)
2598 pbra
->install_in_progress
= true;
2601 void bgp_send_pbr_ipset_match(struct bgp_pbr_match
*pbrim
, bool install
)
2605 if (pbrim
->install_in_progress
)
2607 if (BGP_DEBUG(zebra
, ZEBRA
))
2608 zlog_debug("%s: name %s type %d %d, ID %u",
2609 __PRETTY_FUNCTION__
,
2610 pbrim
->ipset_name
, pbrim
->type
,
2611 install
, pbrim
->unique
);
2615 zclient_create_header(s
,
2616 install
? ZEBRA_IPSET_CREATE
:
2617 ZEBRA_IPSET_DESTROY
,
2620 stream_putl(s
, 1); /* send one pbr action */
2622 bgp_encode_pbr_ipset_match(s
, pbrim
);
2624 stream_putw_at(s
, 0, stream_get_endp(s
));
2625 if (!zclient_send_message(zclient
) && install
)
2626 pbrim
->install_in_progress
= true;
2629 void bgp_send_pbr_ipset_entry_match(struct bgp_pbr_match_entry
*pbrime
,
2634 if (pbrime
->install_in_progress
)
2636 if (BGP_DEBUG(zebra
, ZEBRA
))
2637 zlog_debug("%s: name %s %d %d, ID %u", __PRETTY_FUNCTION__
,
2638 pbrime
->backpointer
->ipset_name
,
2639 pbrime
->unique
, install
, pbrime
->unique
);
2643 zclient_create_header(s
,
2644 install
? ZEBRA_IPSET_ENTRY_ADD
:
2645 ZEBRA_IPSET_ENTRY_DELETE
,
2648 stream_putl(s
, 1); /* send one pbr action */
2650 bgp_encode_pbr_ipset_entry_match(s
, pbrime
);
2652 stream_putw_at(s
, 0, stream_get_endp(s
));
2653 if (!zclient_send_message(zclient
) && install
)
2654 pbrime
->install_in_progress
= true;
2657 static void bgp_encode_pbr_interface_list(struct bgp
*bgp
, struct stream
*s
)
2659 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
2660 struct bgp_pbr_interface_head
*head
;
2661 struct bgp_pbr_interface
*pbr_if
;
2662 struct interface
*ifp
;
2666 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
2668 RB_FOREACH (pbr_if
, bgp_pbr_interface_head
, head
) {
2669 ifp
= if_lookup_by_name(pbr_if
->name
, bgp
->vrf_id
);
2671 stream_putl(s
, ifp
->ifindex
);
2675 static int bgp_pbr_get_ifnumber(struct bgp
*bgp
)
2677 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
2678 struct bgp_pbr_interface_head
*head
;
2679 struct bgp_pbr_interface
*pbr_if
;
2684 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
2686 RB_FOREACH (pbr_if
, bgp_pbr_interface_head
, head
) {
2687 if (if_lookup_by_name(pbr_if
->name
, bgp
->vrf_id
))
2693 void bgp_send_pbr_iptable(struct bgp_pbr_action
*pba
,
2694 struct bgp_pbr_match
*pbm
,
2701 if (pbm
->install_iptable_in_progress
)
2703 if (BGP_DEBUG(zebra
, ZEBRA
))
2704 zlog_debug("%s: name %s type %d mark %d %d, ID %u",
2705 __PRETTY_FUNCTION__
, pbm
->ipset_name
,
2706 pbm
->type
, pba
->fwmark
, install
,
2711 zclient_create_header(s
,
2712 install
? ZEBRA_IPTABLE_ADD
:
2713 ZEBRA_IPTABLE_DELETE
,
2716 bgp_encode_pbr_iptable_match(s
, pba
, pbm
);
2717 nb_interface
= bgp_pbr_get_ifnumber(pba
->bgp
);
2718 stream_putl(s
, nb_interface
);
2720 bgp_encode_pbr_interface_list(pba
->bgp
, s
);
2721 stream_putw_at(s
, 0, stream_get_endp(s
));
2722 ret
= zclient_send_message(zclient
);
2727 pbm
->install_iptable_in_progress
= true;
2731 /* inject in table <table_id> a default route to:
2732 * - if nexthop IP is present : to this nexthop
2733 * - if vrf is different from local : to the matching VRF
2735 void bgp_zebra_announce_default(struct bgp
*bgp
, struct nexthop
*nh
,
2736 afi_t afi
, uint32_t table_id
, bool announce
)
2738 struct zapi_nexthop
*api_nh
;
2739 struct zapi_route api
;
2742 if (!nh
|| nh
->type
!= NEXTHOP_TYPE_IPV4
2743 || nh
->vrf_id
== VRF_UNKNOWN
)
2745 memset(&p
, 0, sizeof(struct prefix
));
2750 memset(&api
, 0, sizeof(api
));
2751 api
.vrf_id
= bgp
->vrf_id
;
2752 api
.type
= ZEBRA_ROUTE_BGP
;
2753 api
.safi
= SAFI_UNICAST
;
2755 api
.tableid
= table_id
;
2756 api
.nexthop_num
= 1;
2757 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
2758 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
2759 api_nh
= &api
.nexthops
[0];
2762 if (nh
->gate
.ipv4
.s_addr
) {
2763 char buff
[PREFIX_STRLEN
];
2765 api_nh
->vrf_id
= nh
->vrf_id
;
2766 api_nh
->gate
.ipv4
= nh
->gate
.ipv4
;
2767 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
2769 inet_ntop(AF_INET
, &(nh
->gate
.ipv4
), buff
, INET_ADDRSTRLEN
);
2770 if (BGP_DEBUG(zebra
, ZEBRA
))
2771 zlog_info("BGP: %s default route to %s table %d (redirect IP)",
2772 announce
? "adding" : "withdrawing",
2774 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
2775 : ZEBRA_ROUTE_DELETE
,
2777 } else if (nh
->vrf_id
!= bgp
->vrf_id
) {
2779 struct interface
*ifp
;
2781 vrf
= vrf_lookup_by_id(nh
->vrf_id
);
2784 /* create default route with interface <VRF>
2785 * with nexthop-vrf <VRF>
2787 ifp
= if_lookup_by_name_all_vrf(vrf
->name
);
2790 api_nh
->vrf_id
= nh
->vrf_id
;
2791 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
2792 api_nh
->ifindex
= ifp
->ifindex
;
2793 if (BGP_DEBUG(zebra
, ZEBRA
))
2794 zlog_info("BGP: %s default route to %s table %d (redirect VRF)",
2795 announce
? "adding" : "withdrawing",
2796 vrf
->name
, table_id
);
2797 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
2798 : ZEBRA_ROUTE_DELETE
,