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
)) {
72 zlog_debug("%s: No zebra instance to talk to, not installing information",
80 int zclient_num_connects
;
82 /* Router-id update message from zebra. */
83 static int bgp_router_id_update(int command
, struct zclient
*zclient
,
84 zebra_size_t length
, vrf_id_t vrf_id
)
86 struct prefix router_id
;
88 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
90 if (BGP_DEBUG(zebra
, ZEBRA
)) {
91 char buf
[PREFIX2STR_BUFFER
];
92 prefix2str(&router_id
, buf
, sizeof(buf
));
93 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id
, buf
);
96 bgp_router_id_zebra_bump(vrf_id
, &router_id
);
100 /* Nexthop update message from zebra. */
101 static int bgp_read_nexthop_update(int command
, struct zclient
*zclient
,
102 zebra_size_t length
, vrf_id_t vrf_id
)
104 bgp_parse_nexthop_update(command
, vrf_id
);
108 static int bgp_read_import_check_update(int command
, struct zclient
*zclient
,
109 zebra_size_t length
, vrf_id_t vrf_id
)
111 bgp_parse_nexthop_update(command
, vrf_id
);
115 /* Set or clear interface on which unnumbered neighbor is configured. This
116 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
119 static void bgp_update_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
,
120 struct interface
*upd_ifp
)
122 struct listnode
*node
, *nnode
;
125 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
126 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)) {
129 bgp_zebra_initiate_radv(bgp
, peer
);
131 bgp_zebra_terminate_radv(bgp
, peer
);
138 static int bgp_read_fec_update(int command
, struct zclient
*zclient
,
141 bgp_parse_fec_update();
145 static void bgp_start_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
)
147 struct listnode
*node
, *nnode
;
150 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
151 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)
152 && peer
->status
!= Established
) {
153 if (peer_active(peer
))
154 BGP_EVENT_ADD(peer
, BGP_Stop
);
155 BGP_EVENT_ADD(peer
, BGP_Start
);
160 static void bgp_nbr_connected_add(struct bgp
*bgp
, struct nbr_connected
*ifc
)
162 struct listnode
*node
;
163 struct connected
*connected
;
164 struct interface
*ifp
;
167 /* Kick-off the FSM for any relevant peers only if there is a
168 * valid local address on the interface.
171 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
172 p
= connected
->address
;
173 if (p
->family
== AF_INET6
174 && IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
180 bgp_start_interface_nbrs(bgp
, ifp
);
183 static void bgp_nbr_connected_delete(struct bgp
*bgp
, struct nbr_connected
*ifc
,
186 struct listnode
*node
, *nnode
;
188 struct interface
*ifp
;
190 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
192 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)) {
193 peer
->last_reset
= PEER_DOWN_NBR_ADDR_DEL
;
194 BGP_EVENT_ADD(peer
, BGP_Stop
);
197 /* Free neighbor also, if we're asked to. */
200 listnode_delete(ifp
->nbr_connected
, ifc
);
201 nbr_connected_free(ifc
);
205 /* Inteface addition message from zebra. */
206 static int bgp_interface_add(int command
, struct zclient
*zclient
,
207 zebra_size_t length
, vrf_id_t vrf_id
)
209 struct interface
*ifp
;
212 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
213 if (!ifp
) // unexpected
216 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
217 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id
, ifp
->name
);
219 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
223 bgp_update_interface_nbrs(bgp
, ifp
, ifp
);
227 static int bgp_interface_delete(int command
, struct zclient
*zclient
,
228 zebra_size_t length
, vrf_id_t vrf_id
)
231 struct interface
*ifp
;
234 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
237 ifp
= zebra_interface_state_read(s
, vrf_id
);
238 if (!ifp
) /* This may happen if we've just unregistered for a VRF. */
241 if (BGP_DEBUG(zebra
, ZEBRA
))
242 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id
, ifp
->name
);
245 bgp_update_interface_nbrs(bgp
, ifp
, NULL
);
247 if_set_index(ifp
, IFINDEX_INTERNAL
);
251 static int bgp_interface_up(int command
, struct zclient
*zclient
,
252 zebra_size_t length
, vrf_id_t vrf_id
)
255 struct interface
*ifp
;
257 struct nbr_connected
*nc
;
258 struct listnode
*node
, *nnode
;
261 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
);
275 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
276 bgp_connected_add(bgp
, c
);
278 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
279 bgp_nbr_connected_add(bgp
, nc
);
284 static int bgp_interface_down(int command
, struct zclient
*zclient
,
285 zebra_size_t length
, vrf_id_t vrf_id
)
288 struct interface
*ifp
;
290 struct nbr_connected
*nc
;
291 struct listnode
*node
, *nnode
;
295 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
298 ifp
= zebra_interface_state_read(s
, vrf_id
);
302 if (BGP_DEBUG(zebra
, ZEBRA
))
303 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id
, ifp
->name
);
308 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
309 bgp_connected_delete(bgp
, c
);
311 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
312 bgp_nbr_connected_delete(bgp
, nc
, 1);
314 /* Fast external-failover */
315 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
)) {
317 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
318 #if defined(HAVE_CUMULUS)
319 /* Take down directly connected EBGP peers as well as
321 * tracked (directly connected) IBGP peers.
323 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1)
325 || bgp_bfd_is_peer_multihop(peer
)))
327 /* Take down directly connected EBGP peers */
328 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
332 if (ifp
== peer
->nexthop
.ifp
) {
333 BGP_EVENT_ADD(peer
, BGP_Stop
);
334 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
342 static int bgp_interface_address_add(int command
, struct zclient
*zclient
,
343 zebra_size_t length
, vrf_id_t vrf_id
)
345 struct connected
*ifc
;
348 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
);
365 if (if_is_operative(ifc
->ifp
)) {
366 bgp_connected_add(bgp
, ifc
);
368 /* If we have learnt of any neighbors on this interface,
369 * check to kick off any BGP interface-based neighbors,
370 * but only if this is a link-local address.
372 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
)
373 && !list_isempty(ifc
->ifp
->nbr_connected
))
374 bgp_start_interface_nbrs(bgp
, ifc
->ifp
);
380 static int bgp_interface_address_delete(int command
, struct zclient
*zclient
,
381 zebra_size_t length
, vrf_id_t vrf_id
)
383 struct connected
*ifc
;
386 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 (bgp
&& 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
;
479 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
484 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
485 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id
,
486 ifp
->name
, new_vrf_id
);
488 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 */
498 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
)) {
499 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
500 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
503 if (ifp
== peer
->nexthop
.ifp
)
504 BGP_EVENT_ADD(peer
, BGP_Stop
);
509 if_update_to_new_vrf(ifp
, new_vrf_id
);
511 bgp
= bgp_lookup_by_vrf_id(new_vrf_id
);
515 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
516 bgp_connected_add(bgp
, c
);
518 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
519 bgp_nbr_connected_add(bgp
, nc
);
523 /* Zebra route add and delete treatment. */
524 static int zebra_read_route(int command
, struct zclient
*zclient
,
525 zebra_size_t length
, vrf_id_t vrf_id
)
527 enum nexthop_types_t nhtype
;
528 struct zapi_route api
;
529 union g_addr nexthop
;
534 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
538 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
541 /* we completely ignore srcdest routes for now. */
542 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
545 /* ignore link-local address. */
546 if (api
.prefix
.family
== AF_INET6
547 && IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
550 nexthop
= api
.nexthops
[0].gate
;
551 ifindex
= api
.nexthops
[0].ifindex
;
552 nhtype
= api
.nexthops
[0].type
;
554 add
= (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
557 * The ADD message is actually an UPDATE and there is no
559 * for a prior redistributed route, if any. So, perform an
561 * DEL processing for the same redistributed route from any
565 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
567 bgp_redistribute_delete(bgp
, &api
.prefix
, i
,
571 /* Now perform the add/update. */
572 bgp_redistribute_add(bgp
, &api
.prefix
, &nexthop
, ifindex
,
573 nhtype
, api
.metric
, api
.type
, api
.instance
,
576 bgp_redistribute_delete(bgp
, &api
.prefix
, api
.type
,
580 if (bgp_debug_zebra(&api
.prefix
)) {
581 char buf
[2][PREFIX_STRLEN
];
583 prefix2str(&api
.prefix
, buf
[0], sizeof(buf
[0]));
585 inet_ntop(api
.prefix
.family
, &nexthop
, buf
[1],
588 "Rx route ADD VRF %u %s[%d] %s nexthop %s (type %d if %u) metric %u tag %" ROUTE_TAG_PRI
,
589 vrf_id
, zebra_route_string(api
.type
),
590 api
.instance
, buf
[0], buf
[1], nhtype
,
591 ifindex
, api
.metric
, api
.tag
);
594 "Rx route DEL VRF %u %s[%d] %s",
595 vrf_id
, zebra_route_string(api
.type
),
596 api
.instance
, buf
[0]);
603 struct interface
*if_lookup_by_ipv4(struct in_addr
*addr
, vrf_id_t vrf_id
)
606 struct listnode
*cnode
;
607 struct interface
*ifp
;
608 struct connected
*connected
;
609 struct prefix_ipv4 p
;
612 vrf
= vrf_lookup_by_id(vrf_id
);
618 p
.prefixlen
= IPV4_MAX_BITLEN
;
620 FOR_ALL_INTERFACES (vrf
, ifp
) {
621 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
622 cp
= connected
->address
;
624 if (cp
->family
== AF_INET
)
625 if (prefix_match(cp
, (struct prefix
*)&p
))
632 struct interface
*if_lookup_by_ipv4_exact(struct in_addr
*addr
, vrf_id_t vrf_id
)
635 struct listnode
*cnode
;
636 struct interface
*ifp
;
637 struct connected
*connected
;
640 vrf
= vrf_lookup_by_id(vrf_id
);
644 FOR_ALL_INTERFACES (vrf
, ifp
) {
645 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
646 cp
= connected
->address
;
648 if (cp
->family
== AF_INET
)
649 if (IPV4_ADDR_SAME(&cp
->u
.prefix4
, addr
))
656 struct interface
*if_lookup_by_ipv6(struct in6_addr
*addr
, ifindex_t ifindex
,
660 struct listnode
*cnode
;
661 struct interface
*ifp
;
662 struct connected
*connected
;
663 struct prefix_ipv6 p
;
666 vrf
= vrf_lookup_by_id(vrf_id
);
672 p
.prefixlen
= IPV6_MAX_BITLEN
;
674 FOR_ALL_INTERFACES (vrf
, ifp
) {
675 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
676 cp
= connected
->address
;
678 if (cp
->family
== AF_INET6
)
679 if (prefix_match(cp
, (struct prefix
*)&p
)) {
680 if (IN6_IS_ADDR_LINKLOCAL(
682 if (ifindex
== ifp
->ifindex
)
692 struct interface
*if_lookup_by_ipv6_exact(struct in6_addr
*addr
,
693 ifindex_t ifindex
, vrf_id_t vrf_id
)
696 struct listnode
*cnode
;
697 struct interface
*ifp
;
698 struct connected
*connected
;
701 vrf
= vrf_lookup_by_id(vrf_id
);
705 FOR_ALL_INTERFACES (vrf
, ifp
) {
706 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
707 cp
= connected
->address
;
709 if (cp
->family
== AF_INET6
)
710 if (IPV6_ADDR_SAME(&cp
->u
.prefix6
, addr
)) {
711 if (IN6_IS_ADDR_LINKLOCAL(
713 if (ifindex
== ifp
->ifindex
)
723 static int if_get_ipv6_global(struct interface
*ifp
, struct in6_addr
*addr
)
725 struct listnode
*cnode
;
726 struct connected
*connected
;
729 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
730 cp
= connected
->address
;
732 if (cp
->family
== AF_INET6
)
733 if (!IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
734 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
741 static int if_get_ipv6_local(struct interface
*ifp
, struct in6_addr
*addr
)
743 struct listnode
*cnode
;
744 struct connected
*connected
;
747 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
748 cp
= connected
->address
;
750 if (cp
->family
== AF_INET6
)
751 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
752 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
759 static int if_get_ipv4_address(struct interface
*ifp
, struct in_addr
*addr
)
761 struct listnode
*cnode
;
762 struct connected
*connected
;
765 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
766 cp
= connected
->address
;
767 if ((cp
->family
== AF_INET
)
768 && !ipv4_martian(&(cp
->u
.prefix4
))) {
769 *addr
= cp
->u
.prefix4
;
777 bool bgp_zebra_nexthop_set(union sockunion
*local
, union sockunion
*remote
,
778 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
781 struct interface
*ifp
= NULL
;
783 memset(nexthop
, 0, sizeof(struct bgp_nexthop
));
790 if (local
->sa
.sa_family
== AF_INET
) {
791 nexthop
->v4
= local
->sin
.sin_addr
;
793 ifp
= if_lookup_by_name(peer
->update_if
,
796 ifp
= if_lookup_by_ipv4_exact(&local
->sin
.sin_addr
,
799 if (local
->sa
.sa_family
== AF_INET6
) {
800 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
801 if (peer
->conf_if
|| peer
->ifname
)
802 ifp
= if_lookup_by_name(peer
->conf_if
806 } else if (peer
->update_if
)
807 ifp
= if_lookup_by_name(peer
->update_if
,
810 ifp
= if_lookup_by_ipv6_exact(&local
->sin6
.sin6_addr
,
811 local
->sin6
.sin6_scope_id
,
817 * BGP views do not currently get proper data
818 * from zebra( when attached ) to be able to
819 * properly resolve nexthops, so give this
820 * instance type a pass.
822 if (peer
->bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
825 * If we have no interface data but we have established
826 * some connection w/ zebra than something has gone
827 * terribly terribly wrong here, so say this failed
828 * If we do not any zebra connection then not
829 * having a ifp pointer is ok.
831 return zclient_num_connects
? false : true;
836 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
837 if (local
->sa
.sa_family
== AF_INET
) {
839 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
842 /* There is no global nexthop. Use link-local address as
844 * global and link-local nexthop. In this scenario, the
846 * for interop is that the network admin would use a
848 * specify the global IPv6 nexthop.
850 if_get_ipv6_local(ifp
, &nexthop
->v6_global
);
851 memcpy(&nexthop
->v6_local
, &nexthop
->v6_global
,
854 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
856 if (if_lookup_by_ipv4(&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
857 peer
->shared_network
= 1;
859 peer
->shared_network
= 0;
862 /* IPv6 connection, fetch and store IPv4 local address if any. */
863 if (local
->sa
.sa_family
== AF_INET6
) {
864 struct interface
*direct
= NULL
;
867 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
868 if (!ret
&& peer
->local_id
.s_addr
)
869 nexthop
->v4
= peer
->local_id
;
872 if (!IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
873 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
876 /* If directory connected set link-local address. */
877 direct
= if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
878 remote
->sin6
.sin6_scope_id
,
881 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
883 /* Link-local address. */
885 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
887 /* If there is no global address. Set link-local
889 global. I know this break RFC specification... */
890 /* In this scenario, the expectation for interop is that
892 * network admin would use a route-map to specify the
897 memcpy(&nexthop
->v6_global
,
898 &local
->sin6
.sin6_addr
,
900 /* Always set the link-local address */
901 memcpy(&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
905 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)
906 || if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
907 remote
->sin6
.sin6_scope_id
,
909 peer
->shared_network
= 1;
911 peer
->shared_network
= 0;
914 /* KAME stack specific treatment. */
916 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_global
)
917 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
)) {
918 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
, 0);
920 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_local
)
921 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
)) {
922 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
, 0);
926 /* If we have identified the local interface, there is no error for now.
931 static struct in6_addr
*
932 bgp_path_info_to_ipv6_nexthop(struct bgp_path_info
*path
, ifindex_t
*ifindex
)
934 struct in6_addr
*nexthop
= NULL
;
936 /* Only global address nexthop exists. */
937 if (path
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
) {
938 nexthop
= &path
->attr
->mp_nexthop_global
;
939 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
940 *ifindex
= path
->attr
->nh_ifindex
;
943 /* If both global and link-local address present. */
944 if (path
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
945 /* Check if route-map is set to prefer global over link-local */
946 if (path
->attr
->mp_nexthop_prefer_global
) {
947 nexthop
= &path
->attr
->mp_nexthop_global
;
948 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
949 *ifindex
= path
->attr
->nh_ifindex
;
951 /* Workaround for Cisco's nexthop bug. */
952 if (IN6_IS_ADDR_UNSPECIFIED(
953 &path
->attr
->mp_nexthop_global
)
954 && path
->peer
->su_remote
->sa
.sa_family
957 &path
->peer
->su_remote
->sin6
.sin6_addr
;
958 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
959 *ifindex
= path
->peer
->nexthop
.ifp
962 nexthop
= &path
->attr
->mp_nexthop_local
;
963 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
964 *ifindex
= path
->attr
->nh_lla_ifindex
;
972 static int bgp_table_map_apply(struct route_map
*map
, struct prefix
*p
,
973 struct bgp_path_info
*path
)
975 route_map_result_t ret
;
977 ret
= route_map_apply(map
, p
, RMAP_BGP
, path
);
978 bgp_attr_flush(path
->attr
);
980 if (ret
!= RMAP_DENYMATCH
)
983 if (bgp_debug_zebra(p
)) {
984 if (p
->family
== AF_INET
) {
985 char buf
[2][INET_ADDRSTRLEN
];
987 "Zebra rmap deny: IPv4 route %s/%d nexthop %s",
988 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0],
991 inet_ntop(AF_INET
, &path
->attr
->nexthop
, buf
[1],
994 if (p
->family
== AF_INET6
) {
995 char buf
[2][INET6_ADDRSTRLEN
];
997 struct in6_addr
*nexthop
;
999 nexthop
= bgp_path_info_to_ipv6_nexthop(path
, &ifindex
);
1001 "Zebra rmap deny: IPv6 route %s/%d nexthop %s",
1002 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0],
1005 inet_ntop(AF_INET6
, nexthop
,
1006 buf
[1], sizeof(buf
[1])));
1012 static struct thread
*bgp_tm_thread_connect
;
1013 static bool bgp_tm_status_connected
;
1014 static bool bgp_tm_chunk_obtained
;
1015 #define BGP_FLOWSPEC_TABLE_CHUNK 100000
1016 static uint32_t bgp_tm_min
, bgp_tm_max
, bgp_tm_chunk_size
;
1017 struct bgp
*bgp_tm_bgp
;
1019 static int bgp_zebra_tm_connect(struct thread
*t
)
1021 struct zclient
*zclient
;
1022 int delay
= 10, ret
= 0;
1024 zclient
= THREAD_ARG(t
);
1025 if (bgp_tm_status_connected
&& zclient
->sock
> 0)
1028 bgp_tm_status_connected
= false;
1029 ret
= tm_table_manager_connect(zclient
);
1032 zlog_info("Error connecting to table manager!");
1033 bgp_tm_status_connected
= false;
1035 if (!bgp_tm_status_connected
)
1036 zlog_debug("Connecting to table manager. Success");
1037 bgp_tm_status_connected
= true;
1038 if (!bgp_tm_chunk_obtained
) {
1039 if (bgp_zebra_get_table_range(bgp_tm_chunk_size
,
1041 &bgp_tm_max
) >= 0) {
1042 bgp_tm_chunk_obtained
= true;
1043 /* parse non installed entries */
1044 bgp_zebra_announce_table(bgp_tm_bgp
, AFI_IP
, SAFI_FLOWSPEC
);
1048 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1049 &bgp_tm_thread_connect
);
1053 bool bgp_zebra_tm_chunk_obtained(void)
1055 return bgp_tm_chunk_obtained
;
1058 uint32_t bgp_zebra_tm_get_id(void)
1060 static int table_id
;
1062 if (!bgp_tm_chunk_obtained
)
1064 return bgp_tm_min
++;
1067 void bgp_zebra_init_tm_connect(struct bgp
*bgp
)
1071 /* if already set, do nothing
1073 if (bgp_tm_thread_connect
!= NULL
)
1075 bgp_tm_status_connected
= false;
1076 bgp_tm_chunk_obtained
= false;
1077 bgp_tm_min
= bgp_tm_max
= 0;
1078 bgp_tm_chunk_size
= BGP_FLOWSPEC_TABLE_CHUNK
;
1080 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1081 &bgp_tm_thread_connect
);
1084 int bgp_zebra_get_table_range(uint32_t chunk_size
,
1085 uint32_t *start
, uint32_t *end
)
1089 if (!bgp_tm_status_connected
)
1091 ret
= tm_get_table_chunk(zclient
, chunk_size
, start
, end
);
1093 flog_err(EC_BGP_TABLE_CHUNK
,
1094 "BGP: Error getting table chunk %u", chunk_size
);
1097 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1098 chunk_size
, *start
, *end
);
1102 static int update_ipv4nh_for_route_install(int nh_othervrf
,
1103 struct in_addr
*nexthop
,
1106 struct zapi_nexthop
*api_nh
)
1108 api_nh
->gate
.ipv4
= *nexthop
;
1110 /* Need to set fields appropriately for EVPN routes imported into
1111 * a VRF (which are programmed as onlink on l3-vni SVI) as well as
1112 * connected routes leaked into a VRF.
1115 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1116 else if (nh_othervrf
&&
1117 api_nh
->gate
.ipv4
.s_addr
== INADDR_ANY
) {
1118 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1119 api_nh
->ifindex
= attr
->nh_ifindex
;
1121 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1127 update_ipv6nh_for_route_install(int nh_othervrf
, struct in6_addr
*nexthop
,
1128 ifindex_t ifindex
, struct bgp_path_info
*pi
,
1129 struct bgp_path_info
*best_pi
, bool is_evpn
,
1130 struct zapi_nexthop
*api_nh
)
1137 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1138 else if (nh_othervrf
) {
1139 if (IN6_IS_ADDR_UNSPECIFIED(nexthop
)) {
1140 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1141 api_nh
->ifindex
= attr
->nh_ifindex
;
1142 } else if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1145 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1146 api_nh
->ifindex
= ifindex
;
1148 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1149 api_nh
->ifindex
= 0;
1152 if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1154 && attr
->mp_nexthop_len
1155 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1156 if (pi
->peer
->nexthop
.ifp
)
1158 pi
->peer
->nexthop
.ifp
->ifindex
;
1160 if (pi
->peer
->conf_if
)
1161 ifindex
= pi
->peer
->ifp
->ifindex
;
1162 else if (pi
->peer
->ifname
)
1163 ifindex
= ifname2ifindex(
1165 pi
->peer
->bgp
->vrf_id
);
1166 else if (pi
->peer
->nexthop
.ifp
)
1168 pi
->peer
->nexthop
.ifp
->ifindex
;
1173 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1174 api_nh
->ifindex
= ifindex
;
1176 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1177 api_nh
->ifindex
= 0;
1180 api_nh
->gate
.ipv6
= *nexthop
;
1185 void bgp_zebra_announce(struct bgp_node
*rn
, struct prefix
*p
,
1186 struct bgp_path_info
*info
, struct bgp
*bgp
, afi_t afi
,
1189 struct zapi_route api
;
1190 struct zapi_nexthop
*api_nh
;
1192 unsigned int valid_nh_count
= 0;
1193 int has_valid_label
= 0;
1196 struct bgp_path_info
*mpinfo
;
1198 struct attr local_attr
;
1199 struct bgp_path_info local_info
;
1200 struct bgp_path_info
*mpinfo_cp
= &local_info
;
1203 int nh_othervrf
= 0;
1204 char buf_prefix
[PREFIX_STRLEN
]; /* filled in if we are debugging */
1208 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1209 * know of this instance.
1211 if (!bgp_install_info_to_zebra(bgp
))
1214 if (bgp
->main_zebra_update_hold
)
1217 if (bgp_debug_zebra(p
))
1218 prefix2str(p
, buf_prefix
, sizeof(buf_prefix
));
1220 if (safi
== SAFI_FLOWSPEC
)
1221 return bgp_pbr_update_entry(bgp
, &rn
->p
,
1222 info
, afi
, safi
, true);
1225 * vrf leaking support (will have only one nexthop)
1227 if (info
->extra
&& info
->extra
->bgp_orig
)
1230 /* Make Zebra API structure. */
1231 memset(&api
, 0, sizeof(api
));
1232 api
.vrf_id
= bgp
->vrf_id
;
1233 api
.type
= ZEBRA_ROUTE_BGP
;
1236 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1240 if (info
->type
== ZEBRA_ROUTE_BGP
1241 && info
->sub_type
== BGP_ROUTE_IMPORTED
) {
1243 /* Obtain peer from parent */
1244 if (info
->extra
&& info
->extra
->parent
)
1245 peer
= ((struct bgp_path_info
*)(info
->extra
->parent
))
1249 tag
= info
->attr
->tag
;
1251 /* If the route's source is EVPN, flag as such. */
1252 is_evpn
= is_route_parent_evpn(info
);
1254 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1256 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
1257 || info
->sub_type
== BGP_ROUTE_AGGREGATE
) {
1258 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1259 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1262 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1263 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1264 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1266 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1268 /* Metric is currently based on the best-path only */
1269 metric
= info
->attr
->med
;
1270 for (mpinfo
= info
; mpinfo
; mpinfo
= bgp_path_info_mpath_next(mpinfo
)) {
1271 if (valid_nh_count
>= multipath_num
)
1274 *mpinfo_cp
= *mpinfo
;
1276 /* Get nexthop address-family */
1277 if (p
->family
== AF_INET
1278 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
))
1279 nh_family
= AF_INET
;
1280 else if (p
->family
== AF_INET6
1281 || (p
->family
== AF_INET
1282 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
)))
1283 nh_family
= AF_INET6
;
1287 api_nh
= &api
.nexthops
[valid_nh_count
];
1288 api_nh
->vrf_id
= nh_othervrf
? info
->extra
->bgp_orig
->vrf_id
1290 if (nh_family
== AF_INET
) {
1291 if (bgp_debug_zebra(&api
.prefix
)) {
1292 if (mpinfo
->extra
) {
1294 "%s: p=%s, bgp_is_valid_label: %d",
1295 __func__
, buf_prefix
,
1301 "%s: p=%s, extra is NULL, no label",
1302 __func__
, buf_prefix
);
1306 if (bgp
->table_map
[afi
][safi
].name
) {
1307 /* Copy info and attributes, so the route-map
1308 apply doesn't modify the BGP route info. */
1309 local_attr
= *mpinfo
->attr
;
1310 mpinfo_cp
->attr
= &local_attr
;
1313 if (bgp
->table_map
[afi
][safi
].name
) {
1314 if (!bgp_table_map_apply(
1315 bgp
->table_map
[afi
][safi
].map
, p
,
1319 /* metric/tag is only allowed to be
1320 * overridden on 1st nexthop */
1321 if (mpinfo
== info
) {
1322 metric
= mpinfo_cp
->attr
->med
;
1323 tag
= mpinfo_cp
->attr
->tag
;
1327 nh_updated
= update_ipv4nh_for_route_install(
1329 &mpinfo_cp
->attr
->nexthop
,
1330 mpinfo_cp
->attr
, is_evpn
, api_nh
);
1332 ifindex_t ifindex
= IFINDEX_INTERNAL
;
1333 struct in6_addr
*nexthop
;
1335 if (bgp
->table_map
[afi
][safi
].name
) {
1336 /* Copy info and attributes, so the route-map
1337 apply doesn't modify the BGP route info. */
1338 local_attr
= *mpinfo
->attr
;
1339 mpinfo_cp
->attr
= &local_attr
;
1342 if (bgp
->table_map
[afi
][safi
].name
) {
1343 /* Copy info and attributes, so the route-map
1344 apply doesn't modify the BGP route info. */
1345 local_attr
= *mpinfo
->attr
;
1346 mpinfo_cp
->attr
= &local_attr
;
1348 if (!bgp_table_map_apply(
1349 bgp
->table_map
[afi
][safi
].map
, p
,
1353 /* metric/tag is only allowed to be
1354 * overridden on 1st nexthop */
1355 if (mpinfo
== info
) {
1356 metric
= mpinfo_cp
->attr
->med
;
1357 tag
= mpinfo_cp
->attr
->tag
;
1360 nexthop
= bgp_path_info_to_ipv6_nexthop(mpinfo_cp
,
1362 nh_updated
= update_ipv6nh_for_route_install(
1363 nh_othervrf
, nexthop
, ifindex
,
1364 mpinfo
, info
, is_evpn
, api_nh
);
1367 /* Did we get proper nexthop info to update zebra? */
1372 && bgp_is_valid_label(&mpinfo
->extra
->label
[0])
1373 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1374 has_valid_label
= 1;
1375 label
= label_pton(&mpinfo
->extra
->label
[0]);
1377 api_nh
->label_num
= 1;
1378 api_nh
->labels
[0] = label
;
1380 memcpy(&api_nh
->rmac
, &(mpinfo
->attr
->rmac
),
1381 sizeof(struct ethaddr
));
1386 /* if this is a evpn route we don't have to include the label */
1387 if (has_valid_label
&& !(CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)))
1388 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
1391 * When we create an aggregate route we must also
1392 * install a Null0 route in the RIB, so overwrite
1393 * what was written into api with a blackhole route
1395 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1396 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
1398 api
.nexthop_num
= valid_nh_count
;
1400 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1401 api
.metric
= metric
;
1404 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1408 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1410 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1411 api
.distance
= distance
;
1414 if (bgp_debug_zebra(p
)) {
1415 char prefix_buf
[PREFIX_STRLEN
];
1416 char nh_buf
[INET6_ADDRSTRLEN
];
1420 prefix2str(&api
.prefix
, prefix_buf
, sizeof(prefix_buf
));
1421 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1423 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1424 prefix_buf
, api
.metric
, api
.tag
, api
.nexthop_num
);
1425 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1426 api_nh
= &api
.nexthops
[i
];
1428 if (api_nh
->type
== NEXTHOP_TYPE_IFINDEX
)
1431 if (api_nh
->type
== NEXTHOP_TYPE_IPV4
)
1432 nh_family
= AF_INET
;
1434 nh_family
= AF_INET6
;
1435 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1439 label_buf
[0] = '\0';
1441 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1442 sprintf(label_buf
, "label %u",
1444 zlog_debug(" nhop [%d]: %s if %u VRF %u %s",
1445 i
+ 1, nh_buf
, api_nh
->ifindex
,
1446 api_nh
->vrf_id
, label_buf
);
1450 if (bgp_debug_zebra(p
)) {
1451 int recursion_flag
= 0;
1453 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
))
1456 zlog_debug("%s: %s: announcing to zebra (recursion %sset)",
1457 __func__
, buf_prefix
,
1458 (recursion_flag
? "" : "NOT "));
1460 zclient_route_send(valid_nh_count
? ZEBRA_ROUTE_ADD
1461 : ZEBRA_ROUTE_DELETE
,
1465 /* Announce all routes of a table to zebra */
1466 void bgp_zebra_announce_table(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1468 struct bgp_node
*rn
;
1469 struct bgp_table
*table
;
1470 struct bgp_path_info
*pi
;
1472 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1473 * know of this instance.
1475 if (!bgp_install_info_to_zebra(bgp
))
1478 table
= bgp
->rib
[afi
][safi
];
1482 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
1483 for (pi
= rn
->info
; pi
; pi
= pi
->next
)
1484 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
) &&
1486 (pi
->type
== ZEBRA_ROUTE_BGP
1487 && (pi
->sub_type
== BGP_ROUTE_NORMAL
1488 || pi
->sub_type
== BGP_ROUTE_IMPORTED
)))
1490 bgp_zebra_announce(rn
, &rn
->p
, pi
, bgp
, afi
,
1494 void bgp_zebra_withdraw(struct prefix
*p
, struct bgp_path_info
*info
,
1495 struct bgp
*bgp
, safi_t safi
)
1497 struct zapi_route api
;
1500 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1501 * know of this instance.
1503 if (!bgp_install_info_to_zebra(bgp
))
1506 if (safi
== SAFI_FLOWSPEC
) {
1508 return bgp_pbr_update_entry(peer
->bgp
, p
,
1509 info
, AFI_IP
, safi
, false);
1512 memset(&api
, 0, sizeof(api
));
1513 api
.vrf_id
= bgp
->vrf_id
;
1514 api
.type
= ZEBRA_ROUTE_BGP
;
1518 /* If the route's source is EVPN, flag as such. */
1519 if (is_route_parent_evpn(info
))
1520 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1522 if (bgp_debug_zebra(p
)) {
1523 char buf
[PREFIX_STRLEN
];
1525 prefix2str(&api
.prefix
, buf
, sizeof(buf
));
1526 zlog_debug("Tx route delete VRF %u %s", bgp
->vrf_id
, buf
);
1529 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
1532 struct bgp_redist
*bgp_redist_lookup(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1533 unsigned short instance
)
1535 struct list
*red_list
;
1536 struct listnode
*node
;
1537 struct bgp_redist
*red
;
1539 red_list
= bgp
->redist
[afi
][type
];
1543 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1544 if (red
->instance
== instance
)
1550 struct bgp_redist
*bgp_redist_add(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1551 unsigned short instance
)
1553 struct list
*red_list
;
1554 struct bgp_redist
*red
;
1556 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1560 if (!bgp
->redist
[afi
][type
])
1561 bgp
->redist
[afi
][type
] = list_new();
1563 red_list
= bgp
->redist
[afi
][type
];
1564 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
,
1565 sizeof(struct bgp_redist
));
1566 red
->instance
= instance
;
1568 listnode_add(red_list
, red
);
1573 static void bgp_redist_del(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1574 unsigned short instance
)
1576 struct bgp_redist
*red
;
1578 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1581 listnode_delete(bgp
->redist
[afi
][type
], red
);
1582 XFREE(MTYPE_BGP_REDIST
, red
);
1583 if (!bgp
->redist
[afi
][type
]->count
)
1584 list_delete(&bgp
->redist
[afi
][type
]);
1588 /* Other routes redistribution into BGP. */
1589 int bgp_redistribute_set(struct bgp
*bgp
, afi_t afi
, int type
,
1590 unsigned short instance
, bool changed
)
1592 /* If redistribute options are changed call
1593 * bgp_redistribute_unreg() to reset the option and withdraw
1597 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1599 /* Return if already redistribute flag is set. */
1601 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1605 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1607 if (vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1611 if (bgp
->vrf_id
== VRF_DEFAULT
1612 && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1613 vnc_export_bgp_enable(
1614 bgp
, afi
); /* only enables if mode bits cfg'd */
1618 vrf_bitmap_set(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1622 * Don't try to register if we're not connected to Zebra or Zebra
1623 * doesn't know of this instance.
1625 * When we come up later well resend if needed.
1627 if (!bgp_install_info_to_zebra(bgp
))
1630 if (BGP_DEBUG(zebra
, ZEBRA
))
1631 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1632 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1635 /* Send distribute add message to zebra. */
1636 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1637 instance
, bgp
->vrf_id
);
1642 int bgp_redistribute_resend(struct bgp
*bgp
, afi_t afi
, int type
,
1643 unsigned short instance
)
1645 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1646 * know of this instance.
1648 if (!bgp_install_info_to_zebra(bgp
))
1651 if (BGP_DEBUG(zebra
, ZEBRA
))
1652 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1653 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1656 /* Send distribute add message to zebra. */
1657 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1658 instance
, bgp
->vrf_id
);
1659 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1660 instance
, bgp
->vrf_id
);
1665 /* Redistribute with route-map specification. */
1666 int bgp_redistribute_rmap_set(struct bgp_redist
*red
, const char *name
,
1667 struct route_map
*route_map
)
1669 if (red
->rmap
.name
&& (strcmp(red
->rmap
.name
, name
) == 0))
1673 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1674 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1675 red
->rmap
.map
= route_map
;
1680 /* Redistribute with metric specification. */
1681 int bgp_redistribute_metric_set(struct bgp
*bgp
, struct bgp_redist
*red
,
1682 afi_t afi
, int type
, uint32_t metric
)
1684 struct bgp_node
*rn
;
1685 struct bgp_path_info
*pi
;
1687 if (red
->redist_metric_flag
&& red
->redist_metric
== metric
)
1690 red
->redist_metric_flag
= 1;
1691 red
->redist_metric
= metric
;
1693 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
1694 rn
= bgp_route_next(rn
)) {
1695 for (pi
= rn
->info
; pi
; pi
= pi
->next
) {
1696 if (pi
->sub_type
== BGP_ROUTE_REDISTRIBUTE
1698 && pi
->instance
== red
->instance
) {
1699 struct attr
*old_attr
;
1700 struct attr new_attr
;
1702 bgp_attr_dup(&new_attr
, pi
->attr
);
1703 new_attr
.med
= red
->redist_metric
;
1704 old_attr
= pi
->attr
;
1705 pi
->attr
= bgp_attr_intern(&new_attr
);
1706 bgp_attr_unintern(&old_attr
);
1708 bgp_path_info_set_flag(rn
, pi
,
1709 BGP_PATH_ATTR_CHANGED
);
1710 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1718 /* Unset redistribution. */
1719 int bgp_redistribute_unreg(struct bgp
*bgp
, afi_t afi
, int type
,
1720 unsigned short instance
)
1722 struct bgp_redist
*red
;
1724 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1728 /* Return if zebra connection is disabled. */
1730 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1733 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1735 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1737 vrf_bitmap_unset(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1741 if (bgp_install_info_to_zebra(bgp
)) {
1742 /* Send distribute delete message to zebra. */
1743 if (BGP_DEBUG(zebra
, ZEBRA
))
1744 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1745 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1747 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
1748 type
, instance
, bgp
->vrf_id
);
1751 /* Withdraw redistributed routes from current BGP's routing table. */
1752 bgp_redistribute_withdraw(bgp
, afi
, type
, instance
);
1757 /* Unset redistribution. */
1758 int bgp_redistribute_unset(struct bgp
*bgp
, afi_t afi
, int type
,
1759 unsigned short instance
)
1761 struct bgp_redist
*red
;
1764 * vnc and vpn->vrf checks must be before red check because
1765 * they operate within bgpd irrespective of zebra connection
1766 * status. red lookup fails if there is no zebra connection.
1769 if (bgp
->vrf_id
== VRF_DEFAULT
&& type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1770 vnc_export_bgp_disable(bgp
, afi
);
1774 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1778 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1780 /* Unset route-map. */
1782 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1783 red
->rmap
.name
= NULL
;
1784 red
->rmap
.map
= NULL
;
1787 red
->redist_metric_flag
= 0;
1788 red
->redist_metric
= 0;
1790 bgp_redist_del(bgp
, afi
, type
, instance
);
1795 /* Update redistribute vrf bitmap during triggers like
1796 restart networking or delete/add VRFs */
1797 void bgp_update_redist_vrf_bitmaps(struct bgp
*bgp
, vrf_id_t old_vrf_id
)
1802 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1803 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1804 if ((old_vrf_id
== VRF_UNKNOWN
)
1805 || vrf_bitmap_check(zclient
->redist
[afi
][i
],
1807 vrf_bitmap_unset(zclient
->redist
[afi
][i
],
1809 vrf_bitmap_set(zclient
->redist
[afi
][i
],
1815 void bgp_zclient_reset(void)
1817 zclient_reset(zclient
);
1820 /* Register this instance with Zebra. Invoked upon connect (for
1821 * default instance) and when other VRFs are learnt (or created and
1824 void bgp_zebra_instance_register(struct bgp
*bgp
)
1826 /* Don't try to register if we're not connected to Zebra */
1827 if (!zclient
|| zclient
->sock
< 0)
1830 if (BGP_DEBUG(zebra
, ZEBRA
))
1831 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
1833 /* Register for router-id, interfaces, redistributed routes. */
1834 zclient_send_reg_requests(zclient
, bgp
->vrf_id
);
1836 /* For default instance, register to learn about VNIs, if appropriate.
1838 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& is_evpn_enabled())
1839 bgp_zebra_advertise_all_vni(bgp
, 1);
1841 bgp_nht_register_nexthops(bgp
);
1844 /* Deregister this instance with Zebra. Invoked upon the instance
1845 * being deleted (default or VRF) and it is already registered.
1847 void bgp_zebra_instance_deregister(struct bgp
*bgp
)
1849 /* Don't try to deregister if we're not connected to Zebra */
1850 if (zclient
->sock
< 0)
1853 if (BGP_DEBUG(zebra
, ZEBRA
))
1854 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
1856 /* For default instance, unregister learning about VNIs, if appropriate.
1858 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& is_evpn_enabled())
1859 bgp_zebra_advertise_all_vni(bgp
, 0);
1861 /* Deregister for router-id, interfaces, redistributed routes. */
1862 zclient_send_dereg_requests(zclient
, bgp
->vrf_id
);
1865 void bgp_zebra_initiate_radv(struct bgp
*bgp
, struct peer
*peer
)
1867 int ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
1869 /* Don't try to initiate if we're not connected to Zebra */
1870 if (zclient
->sock
< 0)
1873 if (BGP_DEBUG(zebra
, ZEBRA
))
1874 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
,
1877 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 1,
1881 void bgp_zebra_terminate_radv(struct bgp
*bgp
, struct peer
*peer
)
1883 /* Don't try to terminate if we're not connected to Zebra */
1884 if (zclient
->sock
< 0)
1887 if (BGP_DEBUG(zebra
, ZEBRA
))
1888 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
,
1891 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 0, 0);
1894 int bgp_zebra_advertise_subnet(struct bgp
*bgp
, int advertise
, vni_t vni
)
1896 struct stream
*s
= NULL
;
1899 if (!zclient
|| zclient
->sock
< 0)
1902 /* Don't try to register if Zebra doesn't know of this instance. */
1903 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
1904 if (BGP_DEBUG(zebra
, ZEBRA
))
1905 zlog_debug("%s: No zebra instance to talk to, cannot advertise subnet",
1906 __PRETTY_FUNCTION__
);
1913 zclient_create_header(s
, ZEBRA_ADVERTISE_SUBNET
, bgp
->vrf_id
);
1914 stream_putc(s
, advertise
);
1915 stream_put3(s
, vni
);
1916 stream_putw_at(s
, 0, stream_get_endp(s
));
1918 return zclient_send_message(zclient
);
1921 int bgp_zebra_advertise_gw_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
1923 struct stream
*s
= NULL
;
1926 if (!zclient
|| zclient
->sock
< 0)
1929 /* Don't try to register if Zebra doesn't know of this instance. */
1930 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
1931 if (BGP_DEBUG(zebra
, ZEBRA
))
1932 zlog_debug("%s: No zebra instance to talk to, not installing gw_macip",
1933 __PRETTY_FUNCTION__
);
1940 zclient_create_header(s
, ZEBRA_ADVERTISE_DEFAULT_GW
, bgp
->vrf_id
);
1941 stream_putc(s
, advertise
);
1942 stream_putl(s
, vni
);
1943 stream_putw_at(s
, 0, stream_get_endp(s
));
1945 return zclient_send_message(zclient
);
1948 int bgp_zebra_vxlan_flood_control(struct bgp
*bgp
,
1949 enum vxlan_flood_control flood_ctrl
)
1954 if (!zclient
|| zclient
->sock
< 0)
1957 /* Don't try to register if Zebra doesn't know of this instance. */
1958 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
1959 if (BGP_DEBUG(zebra
, ZEBRA
))
1960 zlog_debug("%s: No zebra instance to talk to, not installing all vni",
1961 __PRETTY_FUNCTION__
);
1968 zclient_create_header(s
, ZEBRA_VXLAN_FLOOD_CONTROL
, bgp
->vrf_id
);
1969 stream_putc(s
, flood_ctrl
);
1970 stream_putw_at(s
, 0, stream_get_endp(s
));
1972 return zclient_send_message(zclient
);
1975 int bgp_zebra_advertise_all_vni(struct bgp
*bgp
, int advertise
)
1980 if (!zclient
|| zclient
->sock
< 0)
1983 /* Don't try to register if Zebra doesn't know of this instance. */
1984 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1990 zclient_create_header(s
, ZEBRA_ADVERTISE_ALL_VNI
, bgp
->vrf_id
);
1991 stream_putc(s
, advertise
);
1992 /* Also inform current BUM handling setting. This is really
1993 * relevant only when 'advertise' is set.
1995 stream_putc(s
, bgp
->vxlan_flood_ctrl
);
1996 stream_putw_at(s
, 0, stream_get_endp(s
));
1998 return zclient_send_message(zclient
);
2001 static int rule_notify_owner(int command
, struct zclient
*zclient
,
2002 zebra_size_t length
, vrf_id_t vrf_id
)
2004 uint32_t seqno
, priority
, unique
;
2005 enum zapi_rule_notify_owner note
;
2006 struct bgp_pbr_action
*bgp_pbra
;
2009 if (!zapi_rule_notify_decode(zclient
->ibuf
, &seqno
, &priority
, &unique
,
2013 bgp_pbra
= bgp_pbr_action_rule_lookup(vrf_id
, unique
);
2015 if (BGP_DEBUG(zebra
, ZEBRA
))
2016 zlog_debug("%s: Fail to look BGP rule (%u)",
2017 __PRETTY_FUNCTION__
, unique
);
2022 case ZAPI_RULE_FAIL_INSTALL
:
2023 if (BGP_DEBUG(zebra
, ZEBRA
))
2024 zlog_debug("%s: Received RULE_FAIL_INSTALL",
2025 __PRETTY_FUNCTION__
);
2026 bgp_pbra
->installed
= false;
2027 bgp_pbra
->install_in_progress
= false;
2029 case ZAPI_RULE_INSTALLED
:
2030 bgp_pbra
->installed
= true;
2031 bgp_pbra
->install_in_progress
= false;
2032 if (BGP_DEBUG(zebra
, ZEBRA
))
2033 zlog_debug("%s: Received RULE_INSTALLED",
2034 __PRETTY_FUNCTION__
);
2036 case ZAPI_RULE_FAIL_REMOVE
:
2037 case ZAPI_RULE_REMOVED
:
2038 if (BGP_DEBUG(zebra
, ZEBRA
))
2039 zlog_debug("%s: Received RULE REMOVED",
2040 __PRETTY_FUNCTION__
);
2047 static int ipset_notify_owner(int command
, struct zclient
*zclient
,
2048 zebra_size_t length
, vrf_id_t vrf_id
)
2051 enum zapi_ipset_notify_owner note
;
2052 struct bgp_pbr_match
*bgp_pbim
;
2054 if (!zapi_ipset_notify_decode(zclient
->ibuf
,
2059 bgp_pbim
= bgp_pbr_match_ipset_lookup(vrf_id
, unique
);
2061 if (BGP_DEBUG(zebra
, ZEBRA
))
2062 zlog_debug("%s: Fail to look BGP match ( %u, ID %u)",
2063 __PRETTY_FUNCTION__
, note
, unique
);
2068 case ZAPI_IPSET_FAIL_INSTALL
:
2069 if (BGP_DEBUG(zebra
, ZEBRA
))
2070 zlog_debug("%s: Received IPSET_FAIL_INSTALL",
2071 __PRETTY_FUNCTION__
);
2072 bgp_pbim
->installed
= false;
2073 bgp_pbim
->install_in_progress
= false;
2075 case ZAPI_IPSET_INSTALLED
:
2076 bgp_pbim
->installed
= true;
2077 bgp_pbim
->install_in_progress
= false;
2078 if (BGP_DEBUG(zebra
, ZEBRA
))
2079 zlog_debug("%s: Received IPSET_INSTALLED",
2080 __PRETTY_FUNCTION__
);
2082 case ZAPI_IPSET_FAIL_REMOVE
:
2083 case ZAPI_IPSET_REMOVED
:
2084 if (BGP_DEBUG(zebra
, ZEBRA
))
2085 zlog_debug("%s: Received IPSET REMOVED",
2086 __PRETTY_FUNCTION__
);
2093 static int ipset_entry_notify_owner(int command
, struct zclient
*zclient
,
2094 zebra_size_t length
, vrf_id_t vrf_id
)
2097 char ipset_name
[ZEBRA_IPSET_NAME_SIZE
];
2098 enum zapi_ipset_entry_notify_owner note
;
2099 struct bgp_pbr_match_entry
*bgp_pbime
;
2101 if (!zapi_ipset_entry_notify_decode(
2107 bgp_pbime
= bgp_pbr_match_ipset_entry_lookup(vrf_id
,
2111 if (BGP_DEBUG(zebra
, ZEBRA
))
2112 zlog_debug("%s: Fail to look BGP match entry (%u, ID %u)",
2113 __PRETTY_FUNCTION__
, note
, unique
);
2118 case ZAPI_IPSET_ENTRY_FAIL_INSTALL
:
2119 if (BGP_DEBUG(zebra
, ZEBRA
))
2120 zlog_debug("%s: Received IPSET_ENTRY_FAIL_INSTALL",
2121 __PRETTY_FUNCTION__
);
2122 bgp_pbime
->installed
= false;
2123 bgp_pbime
->install_in_progress
= false;
2125 case ZAPI_IPSET_ENTRY_INSTALLED
:
2127 struct bgp_path_info
*path
;
2128 struct bgp_path_info_extra
*extra
;
2130 bgp_pbime
->installed
= true;
2131 bgp_pbime
->install_in_progress
= false;
2132 if (BGP_DEBUG(zebra
, ZEBRA
))
2133 zlog_debug("%s: Received IPSET_ENTRY_INSTALLED",
2134 __PRETTY_FUNCTION__
);
2135 /* link bgp_path_info to bpme */
2136 path
= (struct bgp_path_info
*)bgp_pbime
->path
;
2137 extra
= bgp_path_info_extra_get(path
);
2138 if (extra
->bgp_fs_pbr
== NULL
)
2139 extra
->bgp_fs_pbr
= list_new();
2140 listnode_add(extra
->bgp_fs_pbr
, bgp_pbime
);
2143 case ZAPI_IPSET_ENTRY_FAIL_REMOVE
:
2144 case ZAPI_IPSET_ENTRY_REMOVED
:
2145 if (BGP_DEBUG(zebra
, ZEBRA
))
2146 zlog_debug("%s: Received IPSET_ENTRY_REMOVED",
2147 __PRETTY_FUNCTION__
);
2153 static int iptable_notify_owner(int command
, struct zclient
*zclient
,
2154 zebra_size_t length
, vrf_id_t vrf_id
)
2157 enum zapi_iptable_notify_owner note
;
2158 struct bgp_pbr_match
*bgpm
;
2160 if (!zapi_iptable_notify_decode(
2165 bgpm
= bgp_pbr_match_iptable_lookup(vrf_id
, unique
);
2167 if (BGP_DEBUG(zebra
, ZEBRA
))
2168 zlog_debug("%s: Fail to look BGP iptable (%u %u)",
2169 __PRETTY_FUNCTION__
, note
, unique
);
2173 case ZAPI_IPTABLE_FAIL_INSTALL
:
2174 if (BGP_DEBUG(zebra
, ZEBRA
))
2175 zlog_debug("%s: Received IPTABLE_FAIL_INSTALL",
2176 __PRETTY_FUNCTION__
);
2177 bgpm
->installed_in_iptable
= false;
2178 bgpm
->install_iptable_in_progress
= false;
2180 case ZAPI_IPTABLE_INSTALLED
:
2181 bgpm
->installed_in_iptable
= true;
2182 bgpm
->install_iptable_in_progress
= false;
2183 if (BGP_DEBUG(zebra
, ZEBRA
))
2184 zlog_debug("%s: Received IPTABLE_INSTALLED",
2185 __PRETTY_FUNCTION__
);
2186 bgpm
->action
->refcnt
++;
2188 case ZAPI_IPTABLE_FAIL_REMOVE
:
2189 case ZAPI_IPTABLE_REMOVED
:
2190 if (BGP_DEBUG(zebra
, ZEBRA
))
2191 zlog_debug("%s: Received IPTABLE REMOVED",
2192 __PRETTY_FUNCTION__
);
2198 static void bgp_encode_pbr_rule_action(struct stream
*s
,
2199 struct bgp_pbr_action
*pbra
)
2203 stream_putl(s
, 0); /* seqno unused */
2204 stream_putl(s
, 0); /* ruleno unused */
2206 stream_putl(s
, pbra
->unique
);
2208 memset(&any
, 0, sizeof(any
));
2209 any
.family
= AF_INET
;
2210 stream_putc(s
, any
.family
);
2211 stream_putc(s
, any
.prefixlen
);
2212 stream_put(s
, &any
.u
.prefix
, prefix_blen(&any
));
2214 stream_putw(s
, 0); /* src port */
2216 stream_putc(s
, any
.family
);
2217 stream_putc(s
, any
.prefixlen
);
2218 stream_put(s
, &any
.u
.prefix
, prefix_blen(&any
));
2220 stream_putw(s
, 0); /* dst port */
2222 stream_putl(s
, pbra
->fwmark
); /* fwmark */
2224 stream_putl(s
, pbra
->table_id
);
2226 stream_putl(s
, 0); /* ifindex unused */
2229 static void bgp_encode_pbr_ipset_match(struct stream
*s
,
2230 struct bgp_pbr_match
*pbim
)
2232 stream_putl(s
, pbim
->unique
);
2233 stream_putl(s
, pbim
->type
);
2235 stream_put(s
, pbim
->ipset_name
,
2236 ZEBRA_IPSET_NAME_SIZE
);
2239 static void bgp_encode_pbr_ipset_entry_match(struct stream
*s
,
2240 struct bgp_pbr_match_entry
*pbime
)
2242 stream_putl(s
, pbime
->unique
);
2243 /* check that back pointer is not null */
2244 stream_put(s
, pbime
->backpointer
->ipset_name
,
2245 ZEBRA_IPSET_NAME_SIZE
);
2247 stream_putc(s
, pbime
->src
.family
);
2248 stream_putc(s
, pbime
->src
.prefixlen
);
2249 stream_put(s
, &pbime
->src
.u
.prefix
, prefix_blen(&pbime
->src
));
2251 stream_putc(s
, pbime
->dst
.family
);
2252 stream_putc(s
, pbime
->dst
.prefixlen
);
2253 stream_put(s
, &pbime
->dst
.u
.prefix
, prefix_blen(&pbime
->dst
));
2255 stream_putw(s
, pbime
->src_port_min
);
2256 stream_putw(s
, pbime
->src_port_max
);
2257 stream_putw(s
, pbime
->dst_port_min
);
2258 stream_putw(s
, pbime
->dst_port_max
);
2259 stream_putc(s
, pbime
->proto
);
2262 static void bgp_encode_pbr_iptable_match(struct stream
*s
,
2263 struct bgp_pbr_action
*bpa
,
2264 struct bgp_pbr_match
*pbm
)
2266 stream_putl(s
, pbm
->unique2
);
2268 stream_putl(s
, pbm
->type
);
2270 stream_putl(s
, pbm
->flags
);
2272 /* TODO: correlate with what is contained
2273 * into bgp_pbr_action.
2274 * currently only forward supported
2276 if (bpa
->nh
.type
== NEXTHOP_TYPE_BLACKHOLE
)
2277 stream_putl(s
, ZEBRA_IPTABLES_DROP
);
2279 stream_putl(s
, ZEBRA_IPTABLES_FORWARD
);
2280 stream_putl(s
, bpa
->fwmark
);
2281 stream_put(s
, pbm
->ipset_name
,
2282 ZEBRA_IPSET_NAME_SIZE
);
2283 stream_putw(s
, pbm
->pkt_len_min
);
2284 stream_putw(s
, pbm
->pkt_len_max
);
2285 stream_putw(s
, pbm
->tcp_flags
);
2286 stream_putw(s
, pbm
->tcp_mask_flags
);
2287 stream_putc(s
, pbm
->dscp_value
);
2288 stream_putc(s
, pbm
->fragment
);
2291 /* BGP has established connection with Zebra. */
2292 static void bgp_zebra_connected(struct zclient
*zclient
)
2296 zclient_num_connects
++; /* increment even if not responding */
2298 /* At this point, we may or may not have BGP instances configured, but
2299 * we're only interested in the default VRF (others wouldn't have learnt
2300 * the VRF from Zebra yet.)
2302 bgp
= bgp_get_default();
2306 bgp_zebra_instance_register(bgp
);
2308 /* Send the client registration */
2309 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
2311 /* tell label pool that zebra is connected */
2312 bgp_lp_event_zebra_up();
2314 /* TODO - What if we have peers and networks configured, do we have to
2319 static int bgp_zebra_process_local_es(int cmd
, struct zclient
*zclient
,
2320 zebra_size_t length
, vrf_id_t vrf_id
)
2323 struct bgp
*bgp
= NULL
;
2324 struct stream
*s
= NULL
;
2325 char buf
[ESI_STR_LEN
];
2326 char buf1
[INET6_ADDRSTRLEN
];
2327 struct ipaddr originator_ip
;
2329 memset(&esi
, 0, sizeof(esi_t
));
2330 memset(&originator_ip
, 0, sizeof(struct ipaddr
));
2332 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2337 stream_get(&esi
, s
, sizeof(esi_t
));
2338 stream_get(&originator_ip
, s
, sizeof(struct ipaddr
));
2340 if (BGP_DEBUG(zebra
, ZEBRA
))
2341 zlog_debug("Rx %s ESI %s originator-ip %s",
2342 (cmd
== ZEBRA_LOCAL_ES_ADD
) ? "add" : "del",
2343 esi_to_str(&esi
, buf
, sizeof(buf
)),
2344 ipaddr2str(&originator_ip
, buf1
, sizeof(buf1
)));
2346 if (cmd
== ZEBRA_LOCAL_ES_ADD
)
2347 bgp_evpn_local_es_add(bgp
, &esi
, &originator_ip
);
2349 bgp_evpn_local_es_del(bgp
, &esi
, &originator_ip
);
2353 static int bgp_zebra_process_local_l3vni(int cmd
, struct zclient
*zclient
,
2354 zebra_size_t length
, vrf_id_t vrf_id
)
2357 char buf
[ETHER_ADDR_STRLEN
];
2359 struct ethaddr rmac
;
2360 struct in_addr originator_ip
;
2363 memset(&rmac
, 0, sizeof(struct ethaddr
));
2364 memset(&originator_ip
, 0, sizeof(struct in_addr
));
2366 l3vni
= stream_getl(s
);
2367 if (cmd
== ZEBRA_L3VNI_ADD
) {
2368 stream_get(&rmac
, s
, sizeof(struct ethaddr
));
2369 originator_ip
.s_addr
= stream_get_ipv4(s
);
2370 stream_get(&filter
, s
, sizeof(int));
2373 if (BGP_DEBUG(zebra
, ZEBRA
))
2374 zlog_debug("Rx L3-VNI %s VRF %s VNI %u RMAC %s filter %s",
2375 (cmd
== ZEBRA_L3VNI_ADD
) ? "add" : "del",
2376 vrf_id_to_name(vrf_id
), l3vni
,
2377 prefix_mac2str(&rmac
, buf
, sizeof(buf
)),
2378 filter
? "prefix-routes-only" : "none");
2380 if (cmd
== ZEBRA_L3VNI_ADD
)
2381 bgp_evpn_local_l3vni_add(l3vni
, vrf_id
, &rmac
, originator_ip
,
2384 bgp_evpn_local_l3vni_del(l3vni
, vrf_id
);
2389 static int bgp_zebra_process_local_vni(int command
, struct zclient
*zclient
,
2390 zebra_size_t length
, vrf_id_t vrf_id
)
2395 struct in_addr vtep_ip
= {INADDR_ANY
};
2396 vrf_id_t tenant_vrf_id
= VRF_DEFAULT
;
2399 vni
= stream_getl(s
);
2400 if (command
== ZEBRA_VNI_ADD
) {
2401 vtep_ip
.s_addr
= stream_get_ipv4(s
);
2402 stream_get(&tenant_vrf_id
, s
, sizeof(vrf_id_t
));
2405 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2409 if (BGP_DEBUG(zebra
, ZEBRA
))
2410 zlog_debug("Rx VNI %s VRF %s VNI %u tenant-vrf %s",
2411 (command
== ZEBRA_VNI_ADD
) ? "add" : "del",
2412 vrf_id_to_name(vrf_id
), vni
,
2413 vrf_id_to_name(tenant_vrf_id
));
2415 if (command
== ZEBRA_VNI_ADD
)
2416 return bgp_evpn_local_vni_add(
2417 bgp
, vni
, vtep_ip
.s_addr
? vtep_ip
: bgp
->router_id
,
2420 return bgp_evpn_local_vni_del(bgp
, vni
);
2423 static int bgp_zebra_process_local_macip(int command
, struct zclient
*zclient
,
2424 zebra_size_t length
, vrf_id_t vrf_id
)
2432 char buf
[ETHER_ADDR_STRLEN
];
2433 char buf1
[INET6_ADDRSTRLEN
];
2435 uint32_t seqnum
= 0;
2437 memset(&ip
, 0, sizeof(ip
));
2439 vni
= stream_getl(s
);
2440 stream_get(&mac
.octet
, s
, ETH_ALEN
);
2441 ipa_len
= stream_getl(s
);
2442 if (ipa_len
!= 0 && ipa_len
!= IPV4_MAX_BYTELEN
2443 && ipa_len
!= IPV6_MAX_BYTELEN
) {
2444 flog_err(EC_BGP_MACIP_LEN
,
2445 "%u:Recv MACIP %s with invalid IP addr length %d",
2446 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
2453 (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
: IPADDR_V6
;
2454 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
2456 if (command
== ZEBRA_MACIP_ADD
) {
2457 flags
= stream_getc(s
);
2458 seqnum
= stream_getl(s
);
2461 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2465 if (BGP_DEBUG(zebra
, ZEBRA
))
2466 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u seq %u",
2467 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
2468 flags
, prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2469 ipaddr2str(&ip
, buf1
, sizeof(buf1
)), vni
, seqnum
);
2471 if (command
== ZEBRA_MACIP_ADD
)
2472 return bgp_evpn_local_macip_add(bgp
, vni
, &mac
, &ip
,
2475 return bgp_evpn_local_macip_del(bgp
, vni
, &mac
, &ip
);
2478 static void bgp_zebra_process_local_ip_prefix(int cmd
, struct zclient
*zclient
,
2479 zebra_size_t length
,
2482 struct stream
*s
= NULL
;
2483 struct bgp
*bgp_vrf
= NULL
;
2485 char buf
[PREFIX_STRLEN
];
2487 memset(&p
, 0, sizeof(struct prefix
));
2489 stream_get(&p
, s
, sizeof(struct prefix
));
2491 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
2495 if (BGP_DEBUG(zebra
, ZEBRA
))
2496 zlog_debug("Recv prefix %s %s on vrf %s",
2497 prefix2str(&p
, buf
, sizeof(buf
)),
2498 (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) ? "ADD" : "DEL",
2499 vrf_id_to_name(vrf_id
));
2501 if (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) {
2503 if (p
.family
== AF_INET
)
2504 return bgp_evpn_advertise_type5_route(
2505 bgp_vrf
, &p
, NULL
, AFI_IP
, SAFI_UNICAST
);
2507 return bgp_evpn_advertise_type5_route(
2508 bgp_vrf
, &p
, NULL
, AFI_IP6
, SAFI_UNICAST
);
2511 if (p
.family
== AF_INET
)
2512 return bgp_evpn_withdraw_type5_route(
2513 bgp_vrf
, &p
, AFI_IP
, SAFI_UNICAST
);
2515 return bgp_evpn_withdraw_type5_route(
2516 bgp_vrf
, &p
, AFI_IP6
, SAFI_UNICAST
);
2520 static void bgp_zebra_process_label_chunk(
2522 struct zclient
*zclient
,
2523 zebra_size_t length
,
2526 struct stream
*s
= NULL
;
2527 uint8_t response_keep
;
2531 unsigned short instance
;
2534 STREAM_GETC(s
, proto
);
2535 STREAM_GETW(s
, instance
);
2536 STREAM_GETC(s
, response_keep
);
2537 STREAM_GETL(s
, first
);
2538 STREAM_GETL(s
, last
);
2540 if (zclient
->redist_default
!= proto
) {
2541 flog_err(EC_BGP_LM_ERROR
, "Got LM msg with wrong proto %u",
2545 if (zclient
->instance
!= instance
) {
2546 flog_err(EC_BGP_LM_ERROR
, "Got LM msg with wrong instance %u",
2552 first
< MPLS_LABEL_UNRESERVED_MIN
||
2553 last
> MPLS_LABEL_UNRESERVED_MAX
) {
2555 flog_err(EC_BGP_LM_ERROR
, "%s: Invalid Label chunk: %u - %u",
2556 __func__
, first
, last
);
2559 if (BGP_DEBUG(zebra
, ZEBRA
)) {
2560 zlog_debug("Label Chunk assign: %u - %u (%u) ",
2561 first
, last
, response_keep
);
2564 bgp_lp_event_chunk(response_keep
, first
, last
);
2566 stream_failure
: /* for STREAM_GETX */
2570 extern struct zebra_privs_t bgpd_privs
;
2572 void bgp_zebra_init(struct thread_master
*master
, unsigned short instance
)
2574 zclient_num_connects
= 0;
2576 /* Set default values. */
2577 zclient
= zclient_new(master
, &zclient_options_default
);
2578 zclient_init(zclient
, ZEBRA_ROUTE_BGP
, 0, &bgpd_privs
);
2579 zclient
->zebra_connected
= bgp_zebra_connected
;
2580 zclient
->router_id_update
= bgp_router_id_update
;
2581 zclient
->interface_add
= bgp_interface_add
;
2582 zclient
->interface_delete
= bgp_interface_delete
;
2583 zclient
->interface_address_add
= bgp_interface_address_add
;
2584 zclient
->interface_address_delete
= bgp_interface_address_delete
;
2585 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
2586 zclient
->interface_nbr_address_delete
=
2587 bgp_interface_nbr_address_delete
;
2588 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
2589 zclient
->redistribute_route_add
= zebra_read_route
;
2590 zclient
->redistribute_route_del
= zebra_read_route
;
2591 zclient
->interface_up
= bgp_interface_up
;
2592 zclient
->interface_down
= bgp_interface_down
;
2593 zclient
->nexthop_update
= bgp_read_nexthop_update
;
2594 zclient
->import_check_update
= bgp_read_import_check_update
;
2595 zclient
->fec_update
= bgp_read_fec_update
;
2596 zclient
->local_es_add
= bgp_zebra_process_local_es
;
2597 zclient
->local_es_del
= bgp_zebra_process_local_es
;
2598 zclient
->local_vni_add
= bgp_zebra_process_local_vni
;
2599 zclient
->local_vni_del
= bgp_zebra_process_local_vni
;
2600 zclient
->local_macip_add
= bgp_zebra_process_local_macip
;
2601 zclient
->local_macip_del
= bgp_zebra_process_local_macip
;
2602 zclient
->local_l3vni_add
= bgp_zebra_process_local_l3vni
;
2603 zclient
->local_l3vni_del
= bgp_zebra_process_local_l3vni
;
2604 zclient
->local_ip_prefix_add
= bgp_zebra_process_local_ip_prefix
;
2605 zclient
->local_ip_prefix_del
= bgp_zebra_process_local_ip_prefix
;
2606 zclient
->label_chunk
= bgp_zebra_process_label_chunk
;
2607 zclient
->rule_notify_owner
= rule_notify_owner
;
2608 zclient
->ipset_notify_owner
= ipset_notify_owner
;
2609 zclient
->ipset_entry_notify_owner
= ipset_entry_notify_owner
;
2610 zclient
->iptable_notify_owner
= iptable_notify_owner
;
2611 zclient
->instance
= instance
;
2614 void bgp_zebra_destroy(void)
2616 if (zclient
== NULL
)
2618 zclient_stop(zclient
);
2619 zclient_free(zclient
);
2623 int bgp_zebra_num_connects(void)
2625 return zclient_num_connects
;
2628 void bgp_send_pbr_rule_action(struct bgp_pbr_action
*pbra
, bool install
)
2632 if (pbra
->install_in_progress
)
2634 if (BGP_DEBUG(zebra
, ZEBRA
))
2635 zlog_debug("%s: table %d fwmark %d %d",
2636 __PRETTY_FUNCTION__
,
2637 pbra
->table_id
, pbra
->fwmark
, install
);
2641 zclient_create_header(s
,
2642 install
? ZEBRA_RULE_ADD
: ZEBRA_RULE_DELETE
,
2644 stream_putl(s
, 1); /* send one pbr action */
2646 bgp_encode_pbr_rule_action(s
, pbra
);
2648 stream_putw_at(s
, 0, stream_get_endp(s
));
2649 if (!zclient_send_message(zclient
) && install
)
2650 pbra
->install_in_progress
= true;
2653 void bgp_send_pbr_ipset_match(struct bgp_pbr_match
*pbrim
, bool install
)
2657 if (pbrim
->install_in_progress
)
2659 if (BGP_DEBUG(zebra
, ZEBRA
))
2660 zlog_debug("%s: name %s type %d %d, ID %u",
2661 __PRETTY_FUNCTION__
,
2662 pbrim
->ipset_name
, pbrim
->type
,
2663 install
, pbrim
->unique
);
2667 zclient_create_header(s
,
2668 install
? ZEBRA_IPSET_CREATE
:
2669 ZEBRA_IPSET_DESTROY
,
2672 stream_putl(s
, 1); /* send one pbr action */
2674 bgp_encode_pbr_ipset_match(s
, pbrim
);
2676 stream_putw_at(s
, 0, stream_get_endp(s
));
2677 if (!zclient_send_message(zclient
) && install
)
2678 pbrim
->install_in_progress
= true;
2681 void bgp_send_pbr_ipset_entry_match(struct bgp_pbr_match_entry
*pbrime
,
2686 if (pbrime
->install_in_progress
)
2688 if (BGP_DEBUG(zebra
, ZEBRA
))
2689 zlog_debug("%s: name %s %d %d, ID %u", __PRETTY_FUNCTION__
,
2690 pbrime
->backpointer
->ipset_name
,
2691 pbrime
->unique
, install
, pbrime
->unique
);
2695 zclient_create_header(s
,
2696 install
? ZEBRA_IPSET_ENTRY_ADD
:
2697 ZEBRA_IPSET_ENTRY_DELETE
,
2700 stream_putl(s
, 1); /* send one pbr action */
2702 bgp_encode_pbr_ipset_entry_match(s
, pbrime
);
2704 stream_putw_at(s
, 0, stream_get_endp(s
));
2705 if (!zclient_send_message(zclient
) && install
)
2706 pbrime
->install_in_progress
= true;
2709 static void bgp_encode_pbr_interface_list(struct bgp
*bgp
, struct stream
*s
)
2711 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
2712 struct bgp_pbr_interface_head
*head
;
2713 struct bgp_pbr_interface
*pbr_if
;
2714 struct interface
*ifp
;
2718 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
2720 RB_FOREACH (pbr_if
, bgp_pbr_interface_head
, head
) {
2721 ifp
= if_lookup_by_name(pbr_if
->name
, bgp
->vrf_id
);
2723 stream_putl(s
, ifp
->ifindex
);
2727 static int bgp_pbr_get_ifnumber(struct bgp
*bgp
)
2729 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
2730 struct bgp_pbr_interface_head
*head
;
2731 struct bgp_pbr_interface
*pbr_if
;
2736 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
2738 RB_FOREACH (pbr_if
, bgp_pbr_interface_head
, head
) {
2739 if (if_lookup_by_name(pbr_if
->name
, bgp
->vrf_id
))
2745 void bgp_send_pbr_iptable(struct bgp_pbr_action
*pba
,
2746 struct bgp_pbr_match
*pbm
,
2753 if (pbm
->install_iptable_in_progress
)
2755 if (BGP_DEBUG(zebra
, ZEBRA
))
2756 zlog_debug("%s: name %s type %d mark %d %d, ID %u",
2757 __PRETTY_FUNCTION__
, pbm
->ipset_name
,
2758 pbm
->type
, pba
->fwmark
, install
,
2763 zclient_create_header(s
,
2764 install
? ZEBRA_IPTABLE_ADD
:
2765 ZEBRA_IPTABLE_DELETE
,
2768 bgp_encode_pbr_iptable_match(s
, pba
, pbm
);
2769 nb_interface
= bgp_pbr_get_ifnumber(pba
->bgp
);
2770 stream_putl(s
, nb_interface
);
2772 bgp_encode_pbr_interface_list(pba
->bgp
, s
);
2773 stream_putw_at(s
, 0, stream_get_endp(s
));
2774 ret
= zclient_send_message(zclient
);
2779 pbm
->install_iptable_in_progress
= true;
2783 /* inject in table <table_id> a default route to:
2784 * - if nexthop IP is present : to this nexthop
2785 * - if vrf is different from local : to the matching VRF
2787 void bgp_zebra_announce_default(struct bgp
*bgp
, struct nexthop
*nh
,
2788 afi_t afi
, uint32_t table_id
, bool announce
)
2790 struct zapi_nexthop
*api_nh
;
2791 struct zapi_route api
;
2794 if (!nh
|| nh
->type
!= NEXTHOP_TYPE_IPV4
2795 || nh
->vrf_id
== VRF_UNKNOWN
)
2797 memset(&p
, 0, sizeof(struct prefix
));
2802 memset(&api
, 0, sizeof(api
));
2803 api
.vrf_id
= bgp
->vrf_id
;
2804 api
.type
= ZEBRA_ROUTE_BGP
;
2805 api
.safi
= SAFI_UNICAST
;
2807 api
.tableid
= table_id
;
2808 api
.nexthop_num
= 1;
2809 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
2810 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
2811 api_nh
= &api
.nexthops
[0];
2814 if (nh
->gate
.ipv4
.s_addr
) {
2815 char buff
[PREFIX_STRLEN
];
2817 api_nh
->vrf_id
= nh
->vrf_id
;
2818 api_nh
->gate
.ipv4
= nh
->gate
.ipv4
;
2819 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
2821 inet_ntop(AF_INET
, &(nh
->gate
.ipv4
), buff
, INET_ADDRSTRLEN
);
2822 if (BGP_DEBUG(zebra
, ZEBRA
))
2823 zlog_info("BGP: %s default route to %s table %d (redirect IP)",
2824 announce
? "adding" : "withdrawing",
2826 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
2827 : ZEBRA_ROUTE_DELETE
,
2829 } else if (nh
->vrf_id
!= bgp
->vrf_id
) {
2831 struct interface
*ifp
;
2833 vrf
= vrf_lookup_by_id(nh
->vrf_id
);
2836 /* create default route with interface <VRF>
2837 * with nexthop-vrf <VRF>
2839 ifp
= if_lookup_by_name_all_vrf(vrf
->name
);
2842 api_nh
->vrf_id
= nh
->vrf_id
;
2843 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
2844 api_nh
->ifindex
= ifp
->ifindex
;
2845 if (BGP_DEBUG(zebra
, ZEBRA
))
2846 zlog_info("BGP: %s default route to %s table %d (redirect VRF)",
2847 announce
? "adding" : "withdrawing",
2848 vrf
->name
, table_id
);
2849 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
2850 : ZEBRA_ROUTE_DELETE
,