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
);
234 ifp
= zebra_interface_state_read(s
, vrf_id
);
235 if (!ifp
) /* This may happen if we've just unregistered for a VRF. */
238 if (BGP_DEBUG(zebra
, ZEBRA
))
239 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id
, ifp
->name
);
242 bgp_update_interface_nbrs(bgp
, ifp
, NULL
);
244 if_set_index(ifp
, IFINDEX_INTERNAL
);
248 static int bgp_interface_up(int command
, struct zclient
*zclient
,
249 zebra_size_t length
, vrf_id_t vrf_id
)
252 struct interface
*ifp
;
254 struct nbr_connected
*nc
;
255 struct listnode
*node
, *nnode
;
258 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
261 ifp
= zebra_interface_state_read(s
, vrf_id
);
266 if (BGP_DEBUG(zebra
, ZEBRA
))
267 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
;
292 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
295 ifp
= zebra_interface_state_read(s
, vrf_id
);
299 if (BGP_DEBUG(zebra
, ZEBRA
))
300 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id
, ifp
->name
);
305 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
306 bgp_connected_delete(bgp
, c
);
308 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
309 bgp_nbr_connected_delete(bgp
, nc
, 1);
311 /* Fast external-failover */
312 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
)) {
314 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
315 #if defined(HAVE_CUMULUS)
316 /* Take down directly connected EBGP peers as well as
318 * tracked (directly connected) IBGP peers.
320 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1)
322 || bgp_bfd_is_peer_multihop(peer
)))
324 /* Take down directly connected EBGP peers */
325 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
329 if (ifp
== peer
->nexthop
.ifp
) {
330 BGP_EVENT_ADD(peer
, BGP_Stop
);
331 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
339 static int bgp_interface_address_add(int command
, struct zclient
*zclient
,
340 zebra_size_t length
, vrf_id_t vrf_id
)
342 struct connected
*ifc
;
345 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
347 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
352 if (bgp_debug_zebra(ifc
->address
)) {
353 char buf
[PREFIX2STR_BUFFER
];
354 prefix2str(ifc
->address
, buf
, sizeof(buf
));
355 zlog_debug("Rx Intf address add VRF %u IF %s addr %s", vrf_id
,
356 ifc
->ifp
->name
, buf
);
362 if (if_is_operative(ifc
->ifp
)) {
363 bgp_connected_add(bgp
, ifc
);
365 /* If we have learnt of any neighbors on this interface,
366 * check to kick off any BGP interface-based neighbors,
367 * but only if this is a link-local address.
369 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
)
370 && !list_isempty(ifc
->ifp
->nbr_connected
))
371 bgp_start_interface_nbrs(bgp
, ifc
->ifp
);
377 static int bgp_interface_address_delete(int command
, struct zclient
*zclient
,
378 zebra_size_t length
, vrf_id_t vrf_id
)
380 struct connected
*ifc
;
383 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
385 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
390 if (bgp_debug_zebra(ifc
->address
)) {
391 char buf
[PREFIX2STR_BUFFER
];
392 prefix2str(ifc
->address
, buf
, sizeof(buf
));
393 zlog_debug("Rx Intf address del VRF %u IF %s addr %s", vrf_id
,
394 ifc
->ifp
->name
, buf
);
397 if (bgp
&& if_is_operative(ifc
->ifp
)) {
398 bgp_connected_delete(bgp
, ifc
);
406 static int bgp_interface_nbr_address_add(int command
, struct zclient
*zclient
,
407 zebra_size_t length
, vrf_id_t vrf_id
)
409 struct nbr_connected
*ifc
= NULL
;
412 ifc
= zebra_interface_nbr_address_read(command
, zclient
->ibuf
, vrf_id
);
417 if (bgp_debug_zebra(ifc
->address
)) {
418 char buf
[PREFIX2STR_BUFFER
];
419 prefix2str(ifc
->address
, buf
, sizeof(buf
));
420 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s", vrf_id
,
421 ifc
->ifp
->name
, buf
);
424 if (if_is_operative(ifc
->ifp
)) {
425 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
427 bgp_nbr_connected_add(bgp
, ifc
);
433 static int bgp_interface_nbr_address_delete(int command
,
434 struct zclient
*zclient
,
438 struct nbr_connected
*ifc
= NULL
;
441 ifc
= zebra_interface_nbr_address_read(command
, zclient
->ibuf
, vrf_id
);
446 if (bgp_debug_zebra(ifc
->address
)) {
447 char buf
[PREFIX2STR_BUFFER
];
448 prefix2str(ifc
->address
, buf
, sizeof(buf
));
449 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s", vrf_id
,
450 ifc
->ifp
->name
, buf
);
453 if (if_is_operative(ifc
->ifp
)) {
454 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
456 bgp_nbr_connected_delete(bgp
, ifc
, 0);
459 nbr_connected_free(ifc
);
464 /* VRF update for an interface. */
465 static int bgp_interface_vrf_update(int command
, struct zclient
*zclient
,
466 zebra_size_t length
, vrf_id_t vrf_id
)
468 struct interface
*ifp
;
471 struct nbr_connected
*nc
;
472 struct listnode
*node
, *nnode
;
476 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
481 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
482 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id
,
483 ifp
->name
, new_vrf_id
);
485 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
488 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
489 bgp_connected_delete(bgp
, c
);
491 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
492 bgp_nbr_connected_delete(bgp
, nc
, 1);
494 /* Fast external-failover */
495 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
)) {
496 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
497 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
500 if (ifp
== peer
->nexthop
.ifp
)
501 BGP_EVENT_ADD(peer
, BGP_Stop
);
506 if_update_to_new_vrf(ifp
, new_vrf_id
);
508 bgp
= bgp_lookup_by_vrf_id(new_vrf_id
);
512 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
513 bgp_connected_add(bgp
, c
);
515 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
516 bgp_nbr_connected_add(bgp
, nc
);
520 /* Zebra route add and delete treatment. */
521 static int zebra_read_route(int command
, struct zclient
*zclient
,
522 zebra_size_t length
, vrf_id_t vrf_id
)
524 enum nexthop_types_t nhtype
;
525 struct zapi_route api
;
526 union g_addr nexthop
;
531 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
535 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
538 /* we completely ignore srcdest routes for now. */
539 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
542 /* ignore link-local address. */
543 if (api
.prefix
.family
== AF_INET6
544 && IN6_IS_ADDR_LINKLOCAL(&api
.prefix
.u
.prefix6
))
547 nexthop
= api
.nexthops
[0].gate
;
548 ifindex
= api
.nexthops
[0].ifindex
;
549 nhtype
= api
.nexthops
[0].type
;
551 add
= (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
554 * The ADD message is actually an UPDATE and there is no
556 * for a prior redistributed route, if any. So, perform an
558 * DEL processing for the same redistributed route from any
562 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
564 bgp_redistribute_delete(bgp
, &api
.prefix
, i
,
568 /* Now perform the add/update. */
569 bgp_redistribute_add(bgp
, &api
.prefix
, &nexthop
, ifindex
,
570 nhtype
, api
.metric
, api
.type
, api
.instance
,
573 bgp_redistribute_delete(bgp
, &api
.prefix
, api
.type
,
577 if (bgp_debug_zebra(&api
.prefix
)) {
578 char buf
[2][PREFIX_STRLEN
];
580 prefix2str(&api
.prefix
, buf
[0], sizeof(buf
[0]));
582 inet_ntop(api
.prefix
.family
, &nexthop
, buf
[1],
585 "Rx route ADD VRF %u %s[%d] %s nexthop %s (type %d if %u) metric %u tag %" ROUTE_TAG_PRI
,
586 vrf_id
, zebra_route_string(api
.type
),
587 api
.instance
, buf
[0], buf
[1], nhtype
,
588 ifindex
, api
.metric
, api
.tag
);
591 "Rx route DEL VRF %u %s[%d] %s",
592 vrf_id
, zebra_route_string(api
.type
),
593 api
.instance
, buf
[0]);
600 struct interface
*if_lookup_by_ipv4(struct in_addr
*addr
, vrf_id_t vrf_id
)
603 struct listnode
*cnode
;
604 struct interface
*ifp
;
605 struct connected
*connected
;
606 struct prefix_ipv4 p
;
609 vrf
= vrf_lookup_by_id(vrf_id
);
615 p
.prefixlen
= IPV4_MAX_BITLEN
;
617 FOR_ALL_INTERFACES (vrf
, ifp
) {
618 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
619 cp
= connected
->address
;
621 if (cp
->family
== AF_INET
)
622 if (prefix_match(cp
, (struct prefix
*)&p
))
629 struct interface
*if_lookup_by_ipv4_exact(struct in_addr
*addr
, vrf_id_t vrf_id
)
632 struct listnode
*cnode
;
633 struct interface
*ifp
;
634 struct connected
*connected
;
637 vrf
= vrf_lookup_by_id(vrf_id
);
641 FOR_ALL_INTERFACES (vrf
, ifp
) {
642 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
643 cp
= connected
->address
;
645 if (cp
->family
== AF_INET
)
646 if (IPV4_ADDR_SAME(&cp
->u
.prefix4
, addr
))
653 struct interface
*if_lookup_by_ipv6(struct in6_addr
*addr
, ifindex_t ifindex
,
657 struct listnode
*cnode
;
658 struct interface
*ifp
;
659 struct connected
*connected
;
660 struct prefix_ipv6 p
;
663 vrf
= vrf_lookup_by_id(vrf_id
);
669 p
.prefixlen
= IPV6_MAX_BITLEN
;
671 FOR_ALL_INTERFACES (vrf
, ifp
) {
672 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
673 cp
= connected
->address
;
675 if (cp
->family
== AF_INET6
)
676 if (prefix_match(cp
, (struct prefix
*)&p
)) {
677 if (IN6_IS_ADDR_LINKLOCAL(
679 if (ifindex
== ifp
->ifindex
)
689 struct interface
*if_lookup_by_ipv6_exact(struct in6_addr
*addr
,
690 ifindex_t ifindex
, vrf_id_t vrf_id
)
693 struct listnode
*cnode
;
694 struct interface
*ifp
;
695 struct connected
*connected
;
698 vrf
= vrf_lookup_by_id(vrf_id
);
702 FOR_ALL_INTERFACES (vrf
, ifp
) {
703 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
704 cp
= connected
->address
;
706 if (cp
->family
== AF_INET6
)
707 if (IPV6_ADDR_SAME(&cp
->u
.prefix6
, addr
)) {
708 if (IN6_IS_ADDR_LINKLOCAL(
710 if (ifindex
== ifp
->ifindex
)
720 static int if_get_ipv6_global(struct interface
*ifp
, struct in6_addr
*addr
)
722 struct listnode
*cnode
;
723 struct connected
*connected
;
726 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
727 cp
= connected
->address
;
729 if (cp
->family
== AF_INET6
)
730 if (!IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
731 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
738 static int if_get_ipv6_local(struct interface
*ifp
, struct in6_addr
*addr
)
740 struct listnode
*cnode
;
741 struct connected
*connected
;
744 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
745 cp
= connected
->address
;
747 if (cp
->family
== AF_INET6
)
748 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
749 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
756 static int if_get_ipv4_address(struct interface
*ifp
, struct in_addr
*addr
)
758 struct listnode
*cnode
;
759 struct connected
*connected
;
762 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
763 cp
= connected
->address
;
764 if ((cp
->family
== AF_INET
)
765 && !ipv4_martian(&(cp
->u
.prefix4
))) {
766 *addr
= cp
->u
.prefix4
;
774 bool bgp_zebra_nexthop_set(union sockunion
*local
, union sockunion
*remote
,
775 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
778 struct interface
*ifp
= NULL
;
780 memset(nexthop
, 0, sizeof(struct bgp_nexthop
));
787 if (local
->sa
.sa_family
== AF_INET
) {
788 nexthop
->v4
= local
->sin
.sin_addr
;
790 ifp
= if_lookup_by_name(peer
->update_if
,
793 ifp
= if_lookup_by_ipv4_exact(&local
->sin
.sin_addr
,
796 if (local
->sa
.sa_family
== AF_INET6
) {
797 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
798 if (peer
->conf_if
|| peer
->ifname
)
799 ifp
= if_lookup_by_name(peer
->conf_if
803 } else if (peer
->update_if
)
804 ifp
= if_lookup_by_name(peer
->update_if
,
807 ifp
= if_lookup_by_ipv6_exact(&local
->sin6
.sin6_addr
,
808 local
->sin6
.sin6_scope_id
,
814 * BGP views do not currently get proper data
815 * from zebra( when attached ) to be able to
816 * properly resolve nexthops, so give this
817 * instance type a pass.
819 if (peer
->bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
822 * If we have no interface data but we have established
823 * some connection w/ zebra than something has gone
824 * terribly terribly wrong here, so say this failed
825 * If we do not any zebra connection then not
826 * having a ifp pointer is ok.
828 return zclient_num_connects
? false : true;
833 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
834 if (local
->sa
.sa_family
== AF_INET
) {
836 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
839 /* There is no global nexthop. Use link-local address as
841 * global and link-local nexthop. In this scenario, the
843 * for interop is that the network admin would use a
845 * specify the global IPv6 nexthop.
847 if_get_ipv6_local(ifp
, &nexthop
->v6_global
);
848 memcpy(&nexthop
->v6_local
, &nexthop
->v6_global
,
851 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
853 if (if_lookup_by_ipv4(&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
854 peer
->shared_network
= 1;
856 peer
->shared_network
= 0;
859 /* IPv6 connection, fetch and store IPv4 local address if any. */
860 if (local
->sa
.sa_family
== AF_INET6
) {
861 struct interface
*direct
= NULL
;
864 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
865 if (!ret
&& peer
->local_id
.s_addr
)
866 nexthop
->v4
= peer
->local_id
;
869 if (!IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
870 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
873 /* If directory connected set link-local address. */
874 direct
= if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
875 remote
->sin6
.sin6_scope_id
,
878 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
880 /* Link-local address. */
882 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
884 /* If there is no global address. Set link-local
886 global. I know this break RFC specification... */
887 /* In this scenario, the expectation for interop is that
889 * network admin would use a route-map to specify the
894 memcpy(&nexthop
->v6_global
,
895 &local
->sin6
.sin6_addr
,
897 /* Always set the link-local address */
898 memcpy(&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
902 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)
903 || if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
904 remote
->sin6
.sin6_scope_id
,
906 peer
->shared_network
= 1;
908 peer
->shared_network
= 0;
911 /* KAME stack specific treatment. */
913 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_global
)
914 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
)) {
915 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
, 0);
917 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_local
)
918 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
)) {
919 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
, 0);
923 /* If we have identified the local interface, there is no error for now.
928 static struct in6_addr
*bgp_info_to_ipv6_nexthop(struct bgp_path_info
*info
,
931 struct in6_addr
*nexthop
= NULL
;
933 /* Only global address nexthop exists. */
934 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
) {
935 nexthop
= &info
->attr
->mp_nexthop_global
;
936 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
937 *ifindex
= info
->attr
->nh_ifindex
;
941 /* If both global and link-local address present. */
942 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
943 /* Check if route-map is set to prefer global over link-local */
944 if (info
->attr
->mp_nexthop_prefer_global
) {
945 nexthop
= &info
->attr
->mp_nexthop_global
;
946 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
947 *ifindex
= info
->attr
->nh_ifindex
;
949 /* Workaround for Cisco's nexthop bug. */
950 if (IN6_IS_ADDR_UNSPECIFIED(
951 &info
->attr
->mp_nexthop_global
)
952 && info
->peer
->su_remote
->sa
.sa_family
955 &info
->peer
->su_remote
->sin6
.sin6_addr
;
956 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
957 *ifindex
= info
->peer
->nexthop
.ifp
960 nexthop
= &info
->attr
->mp_nexthop_local
;
961 if (IN6_IS_ADDR_LINKLOCAL(nexthop
))
962 *ifindex
= info
->attr
->nh_lla_ifindex
;
970 static int bgp_table_map_apply(struct route_map
*map
, struct prefix
*p
,
971 struct bgp_path_info
*info
)
973 route_map_result_t ret
;
975 ret
= route_map_apply(map
, p
, RMAP_BGP
, info
);
976 bgp_attr_flush(info
->attr
);
978 if (ret
!= RMAP_DENYMATCH
)
981 if (bgp_debug_zebra(p
)) {
982 if (p
->family
== AF_INET
) {
983 char buf
[2][INET_ADDRSTRLEN
];
985 "Zebra rmap deny: IPv4 route %s/%d nexthop %s",
986 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0],
989 inet_ntop(AF_INET
, &info
->attr
->nexthop
, buf
[1],
992 if (p
->family
== AF_INET6
) {
993 char buf
[2][INET6_ADDRSTRLEN
];
995 struct in6_addr
*nexthop
;
997 nexthop
= bgp_info_to_ipv6_nexthop(info
, &ifindex
);
999 "Zebra rmap deny: IPv6 route %s/%d nexthop %s",
1000 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0],
1003 inet_ntop(AF_INET6
, nexthop
,
1004 buf
[1], sizeof(buf
[1])));
1010 static struct thread
*bgp_tm_thread_connect
;
1011 static bool bgp_tm_status_connected
;
1012 static bool bgp_tm_chunk_obtained
;
1013 #define BGP_FLOWSPEC_TABLE_CHUNK 100000
1014 static uint32_t bgp_tm_min
, bgp_tm_max
, bgp_tm_chunk_size
;
1015 struct bgp
*bgp_tm_bgp
;
1017 static int bgp_zebra_tm_connect(struct thread
*t
)
1019 struct zclient
*zclient
;
1020 int delay
= 10, ret
= 0;
1022 zclient
= THREAD_ARG(t
);
1023 if (bgp_tm_status_connected
&& zclient
->sock
> 0)
1026 bgp_tm_status_connected
= false;
1027 ret
= tm_table_manager_connect(zclient
);
1030 zlog_info("Error connecting to table manager!");
1031 bgp_tm_status_connected
= false;
1033 if (!bgp_tm_status_connected
)
1034 zlog_debug("Connecting to table manager. Success");
1035 bgp_tm_status_connected
= true;
1036 if (!bgp_tm_chunk_obtained
) {
1037 if (bgp_zebra_get_table_range(bgp_tm_chunk_size
,
1039 &bgp_tm_max
) >= 0) {
1040 bgp_tm_chunk_obtained
= true;
1041 /* parse non installed entries */
1042 bgp_zebra_announce_table(bgp_tm_bgp
, AFI_IP
, SAFI_FLOWSPEC
);
1046 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1047 &bgp_tm_thread_connect
);
1051 bool bgp_zebra_tm_chunk_obtained(void)
1053 return bgp_tm_chunk_obtained
;
1056 uint32_t bgp_zebra_tm_get_id(void)
1058 static int table_id
;
1060 if (!bgp_tm_chunk_obtained
)
1062 return bgp_tm_min
++;
1065 void bgp_zebra_init_tm_connect(struct bgp
*bgp
)
1069 /* if already set, do nothing
1071 if (bgp_tm_thread_connect
!= NULL
)
1073 bgp_tm_status_connected
= false;
1074 bgp_tm_chunk_obtained
= false;
1075 bgp_tm_min
= bgp_tm_max
= 0;
1076 bgp_tm_chunk_size
= BGP_FLOWSPEC_TABLE_CHUNK
;
1078 thread_add_timer(bm
->master
, bgp_zebra_tm_connect
, zclient
, delay
,
1079 &bgp_tm_thread_connect
);
1082 int bgp_zebra_get_table_range(uint32_t chunk_size
,
1083 uint32_t *start
, uint32_t *end
)
1087 if (!bgp_tm_status_connected
)
1089 ret
= tm_get_table_chunk(zclient
, chunk_size
, start
, end
);
1091 flog_err(EC_BGP_TABLE_CHUNK
,
1092 "BGP: Error getting table chunk %u", chunk_size
);
1095 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1096 chunk_size
, *start
, *end
);
1100 static int update_ipv4nh_for_route_install(int nh_othervrf
,
1101 struct in_addr
*nexthop
,
1104 struct zapi_nexthop
*api_nh
)
1106 api_nh
->gate
.ipv4
= *nexthop
;
1108 /* Need to set fields appropriately for EVPN routes imported into
1109 * a VRF (which are programmed as onlink on l3-vni SVI) as well as
1110 * connected routes leaked into a VRF.
1113 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1114 else if (nh_othervrf
&&
1115 api_nh
->gate
.ipv4
.s_addr
== INADDR_ANY
) {
1116 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1117 api_nh
->ifindex
= attr
->nh_ifindex
;
1119 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
1125 update_ipv6nh_for_route_install(int nh_othervrf
, struct in6_addr
*nexthop
,
1126 ifindex_t ifindex
, struct bgp_path_info
*ri
,
1127 struct bgp_path_info
*best_ri
, bool is_evpn
,
1128 struct zapi_nexthop
*api_nh
)
1135 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1136 else if (nh_othervrf
) {
1137 if (IN6_IS_ADDR_UNSPECIFIED(nexthop
)) {
1138 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
1139 api_nh
->ifindex
= attr
->nh_ifindex
;
1140 } else if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1143 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1144 api_nh
->ifindex
= ifindex
;
1146 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1147 api_nh
->ifindex
= 0;
1150 if (IN6_IS_ADDR_LINKLOCAL(nexthop
)) {
1151 if (ri
== best_ri
&&
1152 attr
->mp_nexthop_len
1153 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1154 if (ri
->peer
->nexthop
.ifp
)
1155 ifindex
= ri
->peer
->nexthop
.ifp
1158 if (ri
->peer
->conf_if
)
1159 ifindex
= ri
->peer
->ifp
->ifindex
;
1160 else if (ri
->peer
->ifname
)
1161 ifindex
= ifname2ifindex(
1163 ri
->peer
->bgp
->vrf_id
);
1164 else if (ri
->peer
->nexthop
.ifp
)
1165 ifindex
= ri
->peer
->nexthop
.ifp
1171 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1172 api_nh
->ifindex
= ifindex
;
1174 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
1175 api_nh
->ifindex
= 0;
1178 api_nh
->gate
.ipv6
= *nexthop
;
1183 void bgp_zebra_announce(struct bgp_node
*rn
, struct prefix
*p
,
1184 struct bgp_path_info
*info
, struct bgp
*bgp
, afi_t afi
,
1187 struct zapi_route api
;
1188 struct zapi_nexthop
*api_nh
;
1190 unsigned int valid_nh_count
= 0;
1191 int has_valid_label
= 0;
1194 struct bgp_path_info
*mpinfo
;
1196 struct attr local_attr
;
1197 struct bgp_path_info local_info
;
1198 struct bgp_path_info
*mpinfo_cp
= &local_info
;
1201 int nh_othervrf
= 0;
1202 char buf_prefix
[PREFIX_STRLEN
]; /* filled in if we are debugging */
1206 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1207 * know of this instance.
1209 if (!bgp_install_info_to_zebra(bgp
))
1212 if (bgp
->main_zebra_update_hold
)
1215 if (bgp_debug_zebra(p
))
1216 prefix2str(p
, buf_prefix
, sizeof(buf_prefix
));
1218 if (safi
== SAFI_FLOWSPEC
)
1219 return bgp_pbr_update_entry(bgp
, &rn
->p
,
1220 info
, afi
, safi
, true);
1223 * vrf leaking support (will have only one nexthop)
1225 if (info
->extra
&& info
->extra
->bgp_orig
)
1228 /* Make Zebra API structure. */
1229 memset(&api
, 0, sizeof(api
));
1230 api
.vrf_id
= bgp
->vrf_id
;
1231 api
.type
= ZEBRA_ROUTE_BGP
;
1234 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1238 if (info
->type
== ZEBRA_ROUTE_BGP
1239 && info
->sub_type
== BGP_ROUTE_IMPORTED
) {
1241 /* Obtain peer from parent */
1242 if (info
->extra
&& info
->extra
->parent
)
1243 peer
= ((struct bgp_path_info
*)(info
->extra
->parent
))
1247 tag
= info
->attr
->tag
;
1249 /* If the route's source is EVPN, flag as such. */
1250 is_evpn
= is_route_parent_evpn(info
);
1252 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1254 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
1255 || info
->sub_type
== BGP_ROUTE_AGGREGATE
) {
1256 SET_FLAG(api
.flags
, ZEBRA_FLAG_IBGP
);
1257 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1260 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1261 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1262 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1264 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
1266 /* Metric is currently based on the best-path only */
1267 metric
= info
->attr
->med
;
1268 for (mpinfo
= info
; mpinfo
; mpinfo
= bgp_info_mpath_next(mpinfo
)) {
1269 if (valid_nh_count
>= multipath_num
)
1272 *mpinfo_cp
= *mpinfo
;
1274 /* Get nexthop address-family */
1275 if (p
->family
== AF_INET
1276 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
))
1277 nh_family
= AF_INET
;
1278 else if (p
->family
== AF_INET6
1279 || (p
->family
== AF_INET
1280 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp
->attr
)))
1281 nh_family
= AF_INET6
;
1285 api_nh
= &api
.nexthops
[valid_nh_count
];
1286 api_nh
->vrf_id
= nh_othervrf
? info
->extra
->bgp_orig
->vrf_id
1288 if (nh_family
== AF_INET
) {
1289 if (bgp_debug_zebra(&api
.prefix
)) {
1290 if (mpinfo
->extra
) {
1292 "%s: p=%s, bgp_is_valid_label: %d",
1293 __func__
, buf_prefix
,
1299 "%s: p=%s, extra is NULL, no label",
1300 __func__
, buf_prefix
);
1304 if (bgp
->table_map
[afi
][safi
].name
) {
1305 /* Copy info and attributes, so the route-map
1306 apply doesn't modify the BGP route info. */
1307 local_attr
= *mpinfo
->attr
;
1308 mpinfo_cp
->attr
= &local_attr
;
1311 if (bgp
->table_map
[afi
][safi
].name
) {
1312 if (!bgp_table_map_apply(
1313 bgp
->table_map
[afi
][safi
].map
, p
,
1317 /* metric/tag is only allowed to be
1318 * overridden on 1st nexthop */
1319 if (mpinfo
== info
) {
1320 metric
= mpinfo_cp
->attr
->med
;
1321 tag
= mpinfo_cp
->attr
->tag
;
1325 nh_updated
= update_ipv4nh_for_route_install(
1327 &mpinfo_cp
->attr
->nexthop
,
1328 mpinfo_cp
->attr
, is_evpn
, api_nh
);
1330 ifindex_t ifindex
= IFINDEX_INTERNAL
;
1331 struct in6_addr
*nexthop
;
1333 if (bgp
->table_map
[afi
][safi
].name
) {
1334 /* Copy info and attributes, so the route-map
1335 apply doesn't modify the BGP route info. */
1336 local_attr
= *mpinfo
->attr
;
1337 mpinfo_cp
->attr
= &local_attr
;
1340 if (bgp
->table_map
[afi
][safi
].name
) {
1341 /* Copy info and attributes, so the route-map
1342 apply doesn't modify the BGP route info. */
1343 local_attr
= *mpinfo
->attr
;
1344 mpinfo_cp
->attr
= &local_attr
;
1346 if (!bgp_table_map_apply(
1347 bgp
->table_map
[afi
][safi
].map
, p
,
1351 /* metric/tag is only allowed to be
1352 * overridden on 1st nexthop */
1353 if (mpinfo
== info
) {
1354 metric
= mpinfo_cp
->attr
->med
;
1355 tag
= mpinfo_cp
->attr
->tag
;
1358 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo_cp
,
1360 nh_updated
= update_ipv6nh_for_route_install(
1361 nh_othervrf
, nexthop
, ifindex
,
1362 mpinfo
, info
, is_evpn
, api_nh
);
1365 /* Did we get proper nexthop info to update zebra? */
1370 && bgp_is_valid_label(&mpinfo
->extra
->label
[0])
1371 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1372 has_valid_label
= 1;
1373 label
= label_pton(&mpinfo
->extra
->label
[0]);
1375 api_nh
->label_num
= 1;
1376 api_nh
->labels
[0] = label
;
1378 memcpy(&api_nh
->rmac
, &(mpinfo
->attr
->rmac
),
1379 sizeof(struct ethaddr
));
1384 /* if this is a evpn route we don't have to include the label */
1385 if (has_valid_label
&& !(CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)))
1386 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
1389 * When we create an aggregate route we must also
1390 * install a Null0 route in the RIB, so overwrite
1391 * what was written into api with a blackhole route
1393 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1394 zapi_route_set_blackhole(&api
, BLACKHOLE_NULL
);
1396 api
.nexthop_num
= valid_nh_count
;
1398 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1399 api
.metric
= metric
;
1402 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1406 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1408 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1409 api
.distance
= distance
;
1412 if (bgp_debug_zebra(p
)) {
1413 char prefix_buf
[PREFIX_STRLEN
];
1414 char nh_buf
[INET6_ADDRSTRLEN
];
1418 prefix2str(&api
.prefix
, prefix_buf
, sizeof(prefix_buf
));
1419 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1421 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1422 prefix_buf
, api
.metric
, api
.tag
, api
.nexthop_num
);
1423 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1424 api_nh
= &api
.nexthops
[i
];
1426 if (api_nh
->type
== NEXTHOP_TYPE_IFINDEX
)
1429 if (api_nh
->type
== NEXTHOP_TYPE_IPV4
)
1430 nh_family
= AF_INET
;
1432 nh_family
= AF_INET6
;
1433 inet_ntop(nh_family
, &api_nh
->gate
, nh_buf
,
1437 label_buf
[0] = '\0';
1439 && !CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1440 sprintf(label_buf
, "label %u",
1442 zlog_debug(" nhop [%d]: %s if %u VRF %u %s",
1443 i
+ 1, nh_buf
, api_nh
->ifindex
,
1444 api_nh
->vrf_id
, label_buf
);
1448 if (bgp_debug_zebra(p
)) {
1449 int recursion_flag
= 0;
1451 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
))
1454 zlog_debug("%s: %s: announcing to zebra (recursion %sset)",
1455 __func__
, buf_prefix
,
1456 (recursion_flag
? "" : "NOT "));
1458 zclient_route_send(valid_nh_count
? ZEBRA_ROUTE_ADD
1459 : ZEBRA_ROUTE_DELETE
,
1463 /* Announce all routes of a table to zebra */
1464 void bgp_zebra_announce_table(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1466 struct bgp_node
*rn
;
1467 struct bgp_table
*table
;
1468 struct bgp_path_info
*ri
;
1470 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1471 * know of this instance.
1473 if (!bgp_install_info_to_zebra(bgp
))
1476 table
= bgp
->rib
[afi
][safi
];
1480 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
1481 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1482 if (CHECK_FLAG(ri
->flags
, BGP_PATH_SELECTED
) &&
1484 (ri
->type
== ZEBRA_ROUTE_BGP
1485 && (ri
->sub_type
== BGP_ROUTE_NORMAL
1486 || ri
->sub_type
== BGP_ROUTE_IMPORTED
)))
1488 bgp_zebra_announce(rn
, &rn
->p
, ri
, bgp
, afi
,
1492 void bgp_zebra_withdraw(struct prefix
*p
, struct bgp_path_info
*info
,
1493 struct bgp
*bgp
, safi_t safi
)
1495 struct zapi_route api
;
1498 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1499 * know of this instance.
1501 if (!bgp_install_info_to_zebra(bgp
))
1504 if (safi
== SAFI_FLOWSPEC
) {
1506 return bgp_pbr_update_entry(peer
->bgp
, p
,
1507 info
, AFI_IP
, safi
, false);
1510 memset(&api
, 0, sizeof(api
));
1511 api
.vrf_id
= bgp
->vrf_id
;
1512 api
.type
= ZEBRA_ROUTE_BGP
;
1516 /* If the route's source is EVPN, flag as such. */
1517 if (is_route_parent_evpn(info
))
1518 SET_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
);
1520 if (bgp_debug_zebra(p
)) {
1521 char buf
[PREFIX_STRLEN
];
1523 prefix2str(&api
.prefix
, buf
, sizeof(buf
));
1524 zlog_debug("Tx route delete VRF %u %s", bgp
->vrf_id
, buf
);
1527 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
1530 struct bgp_redist
*bgp_redist_lookup(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1531 unsigned short instance
)
1533 struct list
*red_list
;
1534 struct listnode
*node
;
1535 struct bgp_redist
*red
;
1537 red_list
= bgp
->redist
[afi
][type
];
1541 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1542 if (red
->instance
== instance
)
1548 struct bgp_redist
*bgp_redist_add(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1549 unsigned short instance
)
1551 struct list
*red_list
;
1552 struct bgp_redist
*red
;
1554 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1558 if (!bgp
->redist
[afi
][type
])
1559 bgp
->redist
[afi
][type
] = list_new();
1561 red_list
= bgp
->redist
[afi
][type
];
1562 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
,
1563 sizeof(struct bgp_redist
));
1564 red
->instance
= instance
;
1566 listnode_add(red_list
, red
);
1571 static void bgp_redist_del(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
1572 unsigned short instance
)
1574 struct bgp_redist
*red
;
1576 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1579 listnode_delete(bgp
->redist
[afi
][type
], red
);
1580 XFREE(MTYPE_BGP_REDIST
, red
);
1581 if (!bgp
->redist
[afi
][type
]->count
)
1582 list_delete(&bgp
->redist
[afi
][type
]);
1586 /* Other routes redistribution into BGP. */
1587 int bgp_redistribute_set(struct bgp
*bgp
, afi_t afi
, int type
,
1588 unsigned short instance
, bool changed
)
1590 /* If redistribute options are changed call
1591 * bgp_redistribute_unreg() to reset the option and withdraw
1595 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1597 /* Return if already redistribute flag is set. */
1599 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1603 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1605 if (vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1609 if (bgp
->vrf_id
== VRF_DEFAULT
1610 && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1611 vnc_export_bgp_enable(
1612 bgp
, afi
); /* only enables if mode bits cfg'd */
1616 vrf_bitmap_set(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1620 * Don't try to register if we're not connected to Zebra or Zebra
1621 * doesn't know of this instance.
1623 * When we come up later well resend if needed.
1625 if (!bgp_install_info_to_zebra(bgp
))
1628 if (BGP_DEBUG(zebra
, ZEBRA
))
1629 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1630 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1633 /* Send distribute add message to zebra. */
1634 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1635 instance
, bgp
->vrf_id
);
1640 int bgp_redistribute_resend(struct bgp
*bgp
, afi_t afi
, int type
,
1641 unsigned short instance
)
1643 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1644 * know of this instance.
1646 if (!bgp_install_info_to_zebra(bgp
))
1649 if (BGP_DEBUG(zebra
, ZEBRA
))
1650 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1651 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1654 /* Send distribute add message to zebra. */
1655 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1656 instance
, bgp
->vrf_id
);
1657 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1658 instance
, bgp
->vrf_id
);
1663 /* Redistribute with route-map specification. */
1664 int bgp_redistribute_rmap_set(struct bgp_redist
*red
, const char *name
)
1666 if (red
->rmap
.name
&& (strcmp(red
->rmap
.name
, name
) == 0))
1670 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1671 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1672 red
->rmap
.map
= route_map_lookup_by_name(name
);
1677 /* Redistribute with metric specification. */
1678 int bgp_redistribute_metric_set(struct bgp
*bgp
, struct bgp_redist
*red
,
1679 afi_t afi
, int type
, uint32_t metric
)
1681 struct bgp_node
*rn
;
1682 struct bgp_path_info
*ri
;
1684 if (red
->redist_metric_flag
&& red
->redist_metric
== metric
)
1687 red
->redist_metric_flag
= 1;
1688 red
->redist_metric
= metric
;
1690 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
1691 rn
= bgp_route_next(rn
)) {
1692 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
1693 if (ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
1695 && ri
->instance
== red
->instance
) {
1696 struct attr
*old_attr
;
1697 struct attr new_attr
;
1699 bgp_attr_dup(&new_attr
, ri
->attr
);
1700 new_attr
.med
= red
->redist_metric
;
1701 old_attr
= ri
->attr
;
1702 ri
->attr
= bgp_attr_intern(&new_attr
);
1703 bgp_attr_unintern(&old_attr
);
1705 bgp_info_set_flag(rn
, ri
,
1706 BGP_PATH_ATTR_CHANGED
);
1707 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1715 /* Unset redistribution. */
1716 int bgp_redistribute_unreg(struct bgp
*bgp
, afi_t afi
, int type
,
1717 unsigned short instance
)
1719 struct bgp_redist
*red
;
1721 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1725 /* Return if zebra connection is disabled. */
1727 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1730 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1732 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1734 vrf_bitmap_unset(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1738 if (bgp_install_info_to_zebra(bgp
)) {
1739 /* Send distribute delete message to zebra. */
1740 if (BGP_DEBUG(zebra
, ZEBRA
))
1741 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1742 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1744 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
1745 type
, instance
, bgp
->vrf_id
);
1748 /* Withdraw redistributed routes from current BGP's routing table. */
1749 bgp_redistribute_withdraw(bgp
, afi
, type
, instance
);
1754 /* Unset redistribution. */
1755 int bgp_redistribute_unset(struct bgp
*bgp
, afi_t afi
, int type
,
1756 unsigned short instance
)
1758 struct bgp_redist
*red
;
1761 * vnc and vpn->vrf checks must be before red check because
1762 * they operate within bgpd irrespective of zebra connection
1763 * status. red lookup fails if there is no zebra connection.
1766 if (bgp
->vrf_id
== VRF_DEFAULT
&& type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1767 vnc_export_bgp_disable(bgp
, afi
);
1771 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1775 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1777 /* Unset route-map. */
1779 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1780 red
->rmap
.name
= NULL
;
1781 red
->rmap
.map
= NULL
;
1784 red
->redist_metric_flag
= 0;
1785 red
->redist_metric
= 0;
1787 bgp_redist_del(bgp
, afi
, type
, instance
);
1792 /* Update redistribute vrf bitmap during triggers like
1793 restart networking or delete/add VRFs */
1794 void bgp_update_redist_vrf_bitmaps(struct bgp
*bgp
, vrf_id_t old_vrf_id
)
1799 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1800 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1801 if ((old_vrf_id
== VRF_UNKNOWN
)
1802 || vrf_bitmap_check(zclient
->redist
[afi
][i
],
1804 vrf_bitmap_unset(zclient
->redist
[afi
][i
],
1806 vrf_bitmap_set(zclient
->redist
[afi
][i
],
1812 void bgp_zclient_reset(void)
1814 zclient_reset(zclient
);
1817 /* Register this instance with Zebra. Invoked upon connect (for
1818 * default instance) and when other VRFs are learnt (or created and
1821 void bgp_zebra_instance_register(struct bgp
*bgp
)
1823 /* Don't try to register if we're not connected to Zebra */
1824 if (!zclient
|| zclient
->sock
< 0)
1827 if (BGP_DEBUG(zebra
, ZEBRA
))
1828 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
1830 /* Register for router-id, interfaces, redistributed routes. */
1831 zclient_send_reg_requests(zclient
, bgp
->vrf_id
);
1833 /* For default instance, register to learn about VNIs, if appropriate.
1835 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& is_evpn_enabled())
1836 bgp_zebra_advertise_all_vni(bgp
, 1);
1839 /* Deregister this instance with Zebra. Invoked upon the instance
1840 * being deleted (default or VRF) and it is already registered.
1842 void bgp_zebra_instance_deregister(struct bgp
*bgp
)
1844 /* Don't try to deregister if we're not connected to Zebra */
1845 if (zclient
->sock
< 0)
1848 if (BGP_DEBUG(zebra
, ZEBRA
))
1849 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
1851 /* For default instance, unregister learning about VNIs, if appropriate.
1853 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& is_evpn_enabled())
1854 bgp_zebra_advertise_all_vni(bgp
, 0);
1856 /* Deregister for router-id, interfaces, redistributed routes. */
1857 zclient_send_dereg_requests(zclient
, bgp
->vrf_id
);
1860 void bgp_zebra_initiate_radv(struct bgp
*bgp
, struct peer
*peer
)
1862 int ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
1864 /* Don't try to initiate if we're not connected to Zebra */
1865 if (zclient
->sock
< 0)
1868 if (BGP_DEBUG(zebra
, ZEBRA
))
1869 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
,
1872 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 1,
1876 void bgp_zebra_terminate_radv(struct bgp
*bgp
, struct peer
*peer
)
1878 /* Don't try to terminate if we're not connected to Zebra */
1879 if (zclient
->sock
< 0)
1882 if (BGP_DEBUG(zebra
, ZEBRA
))
1883 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
,
1886 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 0, 0);
1889 int bgp_zebra_advertise_subnet(struct bgp
*bgp
, int advertise
, vni_t vni
)
1891 struct stream
*s
= NULL
;
1894 if (!zclient
|| zclient
->sock
< 0)
1897 /* Don't try to register if Zebra doesn't know of this instance. */
1898 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1904 zclient_create_header(s
, ZEBRA_ADVERTISE_SUBNET
, bgp
->vrf_id
);
1905 stream_putc(s
, advertise
);
1906 stream_put3(s
, vni
);
1907 stream_putw_at(s
, 0, stream_get_endp(s
));
1909 return zclient_send_message(zclient
);
1912 int bgp_zebra_advertise_gw_macip(struct bgp
*bgp
, int advertise
, vni_t vni
)
1914 struct stream
*s
= NULL
;
1917 if (!zclient
|| zclient
->sock
< 0)
1920 /* Don't try to register if Zebra doesn't know of this instance. */
1921 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1927 zclient_create_header(s
, ZEBRA_ADVERTISE_DEFAULT_GW
, bgp
->vrf_id
);
1928 stream_putc(s
, advertise
);
1929 stream_putl(s
, vni
);
1930 stream_putw_at(s
, 0, stream_get_endp(s
));
1932 return zclient_send_message(zclient
);
1935 int bgp_zebra_advertise_all_vni(struct bgp
*bgp
, int advertise
)
1940 if (!zclient
|| zclient
->sock
< 0)
1943 /* Don't try to register if Zebra doesn't know of this instance. */
1944 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
1950 zclient_create_header(s
, ZEBRA_ADVERTISE_ALL_VNI
, bgp
->vrf_id
);
1951 stream_putc(s
, advertise
);
1952 stream_putw_at(s
, 0, stream_get_endp(s
));
1954 return zclient_send_message(zclient
);
1957 static int rule_notify_owner(int command
, struct zclient
*zclient
,
1958 zebra_size_t length
, vrf_id_t vrf_id
)
1960 uint32_t seqno
, priority
, unique
;
1961 enum zapi_rule_notify_owner note
;
1962 struct bgp_pbr_action
*bgp_pbra
;
1965 if (!zapi_rule_notify_decode(zclient
->ibuf
, &seqno
, &priority
, &unique
,
1969 bgp_pbra
= bgp_pbr_action_rule_lookup(vrf_id
, unique
);
1971 if (BGP_DEBUG(zebra
, ZEBRA
))
1972 zlog_debug("%s: Fail to look BGP rule (%u)",
1973 __PRETTY_FUNCTION__
, unique
);
1978 case ZAPI_RULE_FAIL_INSTALL
:
1979 if (BGP_DEBUG(zebra
, ZEBRA
))
1980 zlog_debug("%s: Received RULE_FAIL_INSTALL",
1981 __PRETTY_FUNCTION__
);
1982 bgp_pbra
->installed
= false;
1983 bgp_pbra
->install_in_progress
= false;
1985 case ZAPI_RULE_INSTALLED
:
1986 bgp_pbra
->installed
= true;
1987 bgp_pbra
->install_in_progress
= false;
1988 if (BGP_DEBUG(zebra
, ZEBRA
))
1989 zlog_debug("%s: Received RULE_INSTALLED",
1990 __PRETTY_FUNCTION__
);
1992 case ZAPI_RULE_FAIL_REMOVE
:
1993 case ZAPI_RULE_REMOVED
:
1994 if (BGP_DEBUG(zebra
, ZEBRA
))
1995 zlog_debug("%s: Received RULE REMOVED",
1996 __PRETTY_FUNCTION__
);
2003 static int ipset_notify_owner(int command
, struct zclient
*zclient
,
2004 zebra_size_t length
, vrf_id_t vrf_id
)
2007 enum zapi_ipset_notify_owner note
;
2008 struct bgp_pbr_match
*bgp_pbim
;
2010 if (!zapi_ipset_notify_decode(zclient
->ibuf
,
2015 bgp_pbim
= bgp_pbr_match_ipset_lookup(vrf_id
, unique
);
2017 if (BGP_DEBUG(zebra
, ZEBRA
))
2018 zlog_debug("%s: Fail to look BGP match ( %u, ID %u)",
2019 __PRETTY_FUNCTION__
, note
, unique
);
2024 case ZAPI_IPSET_FAIL_INSTALL
:
2025 if (BGP_DEBUG(zebra
, ZEBRA
))
2026 zlog_debug("%s: Received IPSET_FAIL_INSTALL",
2027 __PRETTY_FUNCTION__
);
2028 bgp_pbim
->installed
= false;
2029 bgp_pbim
->install_in_progress
= false;
2031 case ZAPI_IPSET_INSTALLED
:
2032 bgp_pbim
->installed
= true;
2033 bgp_pbim
->install_in_progress
= false;
2034 if (BGP_DEBUG(zebra
, ZEBRA
))
2035 zlog_debug("%s: Received IPSET_INSTALLED",
2036 __PRETTY_FUNCTION__
);
2038 case ZAPI_IPSET_FAIL_REMOVE
:
2039 case ZAPI_IPSET_REMOVED
:
2040 if (BGP_DEBUG(zebra
, ZEBRA
))
2041 zlog_debug("%s: Received IPSET REMOVED",
2042 __PRETTY_FUNCTION__
);
2049 static int ipset_entry_notify_owner(int command
, struct zclient
*zclient
,
2050 zebra_size_t length
, vrf_id_t vrf_id
)
2053 char ipset_name
[ZEBRA_IPSET_NAME_SIZE
];
2054 enum zapi_ipset_entry_notify_owner note
;
2055 struct bgp_pbr_match_entry
*bgp_pbime
;
2057 if (!zapi_ipset_entry_notify_decode(
2063 bgp_pbime
= bgp_pbr_match_ipset_entry_lookup(vrf_id
,
2067 if (BGP_DEBUG(zebra
, ZEBRA
))
2068 zlog_debug("%s: Fail to look BGP match entry (%u, ID %u)",
2069 __PRETTY_FUNCTION__
, note
, unique
);
2074 case ZAPI_IPSET_ENTRY_FAIL_INSTALL
:
2075 if (BGP_DEBUG(zebra
, ZEBRA
))
2076 zlog_debug("%s: Received IPSET_ENTRY_FAIL_INSTALL",
2077 __PRETTY_FUNCTION__
);
2078 bgp_pbime
->installed
= false;
2079 bgp_pbime
->install_in_progress
= false;
2081 case ZAPI_IPSET_ENTRY_INSTALLED
:
2083 struct bgp_path_info
*bgp_info
;
2084 struct bgp_path_info_extra
*extra
;
2086 bgp_pbime
->installed
= true;
2087 bgp_pbime
->install_in_progress
= false;
2088 if (BGP_DEBUG(zebra
, ZEBRA
))
2089 zlog_debug("%s: Received IPSET_ENTRY_INSTALLED",
2090 __PRETTY_FUNCTION__
);
2091 /* link bgp_info to bpme */
2092 bgp_info
= (struct bgp_path_info
*)bgp_pbime
->bgp_info
;
2093 extra
= bgp_info_extra_get(bgp_info
);
2094 if (extra
->bgp_fs_pbr
== NULL
)
2095 extra
->bgp_fs_pbr
= list_new();
2096 listnode_add(extra
->bgp_fs_pbr
, bgp_pbime
);
2099 case ZAPI_IPSET_ENTRY_FAIL_REMOVE
:
2100 case ZAPI_IPSET_ENTRY_REMOVED
:
2101 if (BGP_DEBUG(zebra
, ZEBRA
))
2102 zlog_debug("%s: Received IPSET_ENTRY_REMOVED",
2103 __PRETTY_FUNCTION__
);
2109 static int iptable_notify_owner(int command
, struct zclient
*zclient
,
2110 zebra_size_t length
, vrf_id_t vrf_id
)
2113 enum zapi_iptable_notify_owner note
;
2114 struct bgp_pbr_match
*bgpm
;
2116 if (!zapi_iptable_notify_decode(
2121 bgpm
= bgp_pbr_match_iptable_lookup(vrf_id
, unique
);
2123 if (BGP_DEBUG(zebra
, ZEBRA
))
2124 zlog_debug("%s: Fail to look BGP iptable (%u %u)",
2125 __PRETTY_FUNCTION__
, note
, unique
);
2129 case ZAPI_IPTABLE_FAIL_INSTALL
:
2130 if (BGP_DEBUG(zebra
, ZEBRA
))
2131 zlog_debug("%s: Received IPTABLE_FAIL_INSTALL",
2132 __PRETTY_FUNCTION__
);
2133 bgpm
->installed_in_iptable
= false;
2134 bgpm
->install_iptable_in_progress
= false;
2136 case ZAPI_IPTABLE_INSTALLED
:
2137 bgpm
->installed_in_iptable
= true;
2138 bgpm
->install_iptable_in_progress
= false;
2139 if (BGP_DEBUG(zebra
, ZEBRA
))
2140 zlog_debug("%s: Received IPTABLE_INSTALLED",
2141 __PRETTY_FUNCTION__
);
2142 bgpm
->action
->refcnt
++;
2144 case ZAPI_IPTABLE_FAIL_REMOVE
:
2145 case ZAPI_IPTABLE_REMOVED
:
2146 if (BGP_DEBUG(zebra
, ZEBRA
))
2147 zlog_debug("%s: Received IPTABLE REMOVED",
2148 __PRETTY_FUNCTION__
);
2154 static void bgp_encode_pbr_rule_action(struct stream
*s
,
2155 struct bgp_pbr_action
*pbra
)
2159 stream_putl(s
, 0); /* seqno unused */
2160 stream_putl(s
, 0); /* ruleno unused */
2162 stream_putl(s
, pbra
->unique
);
2164 memset(&any
, 0, sizeof(any
));
2165 any
.family
= AF_INET
;
2166 stream_putc(s
, any
.family
);
2167 stream_putc(s
, any
.prefixlen
);
2168 stream_put(s
, &any
.u
.prefix
, prefix_blen(&any
));
2170 stream_putw(s
, 0); /* src port */
2172 stream_putc(s
, any
.family
);
2173 stream_putc(s
, any
.prefixlen
);
2174 stream_put(s
, &any
.u
.prefix
, prefix_blen(&any
));
2176 stream_putw(s
, 0); /* dst port */
2178 stream_putl(s
, pbra
->fwmark
); /* fwmark */
2180 stream_putl(s
, pbra
->table_id
);
2182 stream_putl(s
, 0); /* ifindex unused */
2185 static void bgp_encode_pbr_ipset_match(struct stream
*s
,
2186 struct bgp_pbr_match
*pbim
)
2188 stream_putl(s
, pbim
->unique
);
2189 stream_putl(s
, pbim
->type
);
2191 stream_put(s
, pbim
->ipset_name
,
2192 ZEBRA_IPSET_NAME_SIZE
);
2195 static void bgp_encode_pbr_ipset_entry_match(struct stream
*s
,
2196 struct bgp_pbr_match_entry
*pbime
)
2198 stream_putl(s
, pbime
->unique
);
2199 /* check that back pointer is not null */
2200 stream_put(s
, pbime
->backpointer
->ipset_name
,
2201 ZEBRA_IPSET_NAME_SIZE
);
2203 stream_putc(s
, pbime
->src
.family
);
2204 stream_putc(s
, pbime
->src
.prefixlen
);
2205 stream_put(s
, &pbime
->src
.u
.prefix
, prefix_blen(&pbime
->src
));
2207 stream_putc(s
, pbime
->dst
.family
);
2208 stream_putc(s
, pbime
->dst
.prefixlen
);
2209 stream_put(s
, &pbime
->dst
.u
.prefix
, prefix_blen(&pbime
->dst
));
2211 stream_putw(s
, pbime
->src_port_min
);
2212 stream_putw(s
, pbime
->src_port_max
);
2213 stream_putw(s
, pbime
->dst_port_min
);
2214 stream_putw(s
, pbime
->dst_port_max
);
2215 stream_putc(s
, pbime
->proto
);
2218 static void bgp_encode_pbr_iptable_match(struct stream
*s
,
2219 struct bgp_pbr_action
*bpa
,
2220 struct bgp_pbr_match
*pbm
)
2222 stream_putl(s
, pbm
->unique2
);
2224 stream_putl(s
, pbm
->type
);
2226 stream_putl(s
, pbm
->flags
);
2228 /* TODO: correlate with what is contained
2229 * into bgp_pbr_action.
2230 * currently only forward supported
2232 if (bpa
->nh
.type
== NEXTHOP_TYPE_BLACKHOLE
)
2233 stream_putl(s
, ZEBRA_IPTABLES_DROP
);
2235 stream_putl(s
, ZEBRA_IPTABLES_FORWARD
);
2236 stream_putl(s
, bpa
->fwmark
);
2237 stream_put(s
, pbm
->ipset_name
,
2238 ZEBRA_IPSET_NAME_SIZE
);
2239 stream_putw(s
, pbm
->pkt_len_min
);
2240 stream_putw(s
, pbm
->pkt_len_max
);
2241 stream_putw(s
, pbm
->tcp_flags
);
2242 stream_putw(s
, pbm
->tcp_mask_flags
);
2243 stream_putc(s
, pbm
->dscp_value
);
2244 stream_putc(s
, pbm
->fragment
);
2247 /* BGP has established connection with Zebra. */
2248 static void bgp_zebra_connected(struct zclient
*zclient
)
2252 zclient_num_connects
++; /* increment even if not responding */
2254 /* At this point, we may or may not have BGP instances configured, but
2255 * we're only interested in the default VRF (others wouldn't have learnt
2256 * the VRF from Zebra yet.)
2258 bgp
= bgp_get_default();
2262 bgp_zebra_instance_register(bgp
);
2264 /* Send the client registration */
2265 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
2267 /* tell label pool that zebra is connected */
2268 bgp_lp_event_zebra_up();
2270 /* TODO - What if we have peers and networks configured, do we have to
2275 static int bgp_zebra_process_local_es(int cmd
, struct zclient
*zclient
,
2276 zebra_size_t length
, vrf_id_t vrf_id
)
2279 struct bgp
*bgp
= NULL
;
2280 struct stream
*s
= NULL
;
2281 char buf
[ESI_STR_LEN
];
2282 char buf1
[INET6_ADDRSTRLEN
];
2283 struct ipaddr originator_ip
;
2285 memset(&esi
, 0, sizeof(esi_t
));
2286 memset(&originator_ip
, 0, sizeof(struct ipaddr
));
2288 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2293 stream_get(&esi
, s
, sizeof(esi_t
));
2294 stream_get(&originator_ip
, s
, sizeof(struct ipaddr
));
2296 if (BGP_DEBUG(zebra
, ZEBRA
))
2297 zlog_debug("Rx %s ESI %s originator-ip %s",
2298 (cmd
== ZEBRA_LOCAL_ES_ADD
) ? "add" : "del",
2299 esi_to_str(&esi
, buf
, sizeof(buf
)),
2300 ipaddr2str(&originator_ip
, buf1
, sizeof(buf1
)));
2302 if (cmd
== ZEBRA_LOCAL_ES_ADD
)
2303 bgp_evpn_local_es_add(bgp
, &esi
, &originator_ip
);
2305 bgp_evpn_local_es_del(bgp
, &esi
, &originator_ip
);
2309 static int bgp_zebra_process_local_l3vni(int cmd
, struct zclient
*zclient
,
2310 zebra_size_t length
, vrf_id_t vrf_id
)
2313 char buf
[ETHER_ADDR_STRLEN
];
2315 struct ethaddr rmac
;
2316 struct in_addr originator_ip
;
2319 memset(&rmac
, 0, sizeof(struct ethaddr
));
2320 memset(&originator_ip
, 0, sizeof(struct in_addr
));
2322 l3vni
= stream_getl(s
);
2323 if (cmd
== ZEBRA_L3VNI_ADD
) {
2324 stream_get(&rmac
, s
, sizeof(struct ethaddr
));
2325 originator_ip
.s_addr
= stream_get_ipv4(s
);
2326 stream_get(&filter
, s
, sizeof(int));
2329 if (BGP_DEBUG(zebra
, ZEBRA
))
2330 zlog_debug("Rx L3-VNI %s VRF %s VNI %u RMAC %s filter %s",
2331 (cmd
== ZEBRA_L3VNI_ADD
) ? "add" : "del",
2332 vrf_id_to_name(vrf_id
), l3vni
,
2333 prefix_mac2str(&rmac
, buf
, sizeof(buf
)),
2334 filter
? "prefix-routes-only" : "none");
2336 if (cmd
== ZEBRA_L3VNI_ADD
)
2337 bgp_evpn_local_l3vni_add(l3vni
, vrf_id
, &rmac
, originator_ip
,
2340 bgp_evpn_local_l3vni_del(l3vni
, vrf_id
);
2345 static int bgp_zebra_process_local_vni(int command
, struct zclient
*zclient
,
2346 zebra_size_t length
, vrf_id_t vrf_id
)
2351 struct in_addr vtep_ip
= {INADDR_ANY
};
2352 vrf_id_t tenant_vrf_id
= VRF_DEFAULT
;
2355 vni
= stream_getl(s
);
2356 if (command
== ZEBRA_VNI_ADD
) {
2357 vtep_ip
.s_addr
= stream_get_ipv4(s
);
2358 stream_get(&tenant_vrf_id
, s
, sizeof(vrf_id_t
));
2361 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2365 if (BGP_DEBUG(zebra
, ZEBRA
))
2366 zlog_debug("Rx VNI %s VRF %s VNI %u tenant-vrf %s",
2367 (command
== ZEBRA_VNI_ADD
) ? "add" : "del",
2368 vrf_id_to_name(vrf_id
), vni
,
2369 vrf_id_to_name(tenant_vrf_id
));
2371 if (command
== ZEBRA_VNI_ADD
)
2372 return bgp_evpn_local_vni_add(
2373 bgp
, vni
, vtep_ip
.s_addr
? vtep_ip
: bgp
->router_id
,
2376 return bgp_evpn_local_vni_del(bgp
, vni
);
2379 static int bgp_zebra_process_local_macip(int command
, struct zclient
*zclient
,
2380 zebra_size_t length
, vrf_id_t vrf_id
)
2388 char buf
[ETHER_ADDR_STRLEN
];
2389 char buf1
[INET6_ADDRSTRLEN
];
2391 uint32_t seqnum
= 0;
2393 memset(&ip
, 0, sizeof(ip
));
2395 vni
= stream_getl(s
);
2396 stream_get(&mac
.octet
, s
, ETH_ALEN
);
2397 ipa_len
= stream_getl(s
);
2398 if (ipa_len
!= 0 && ipa_len
!= IPV4_MAX_BYTELEN
2399 && ipa_len
!= IPV6_MAX_BYTELEN
) {
2400 flog_err(EC_BGP_MACIP_LEN
,
2401 "%u:Recv MACIP %s with invalid IP addr length %d",
2402 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
2409 (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
: IPADDR_V6
;
2410 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
2412 if (command
== ZEBRA_MACIP_ADD
) {
2413 flags
= stream_getc(s
);
2414 seqnum
= stream_getl(s
);
2417 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2421 if (BGP_DEBUG(zebra
, ZEBRA
))
2422 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u seq %u",
2423 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
2424 flags
, prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2425 ipaddr2str(&ip
, buf1
, sizeof(buf1
)), vni
, seqnum
);
2427 if (command
== ZEBRA_MACIP_ADD
)
2428 return bgp_evpn_local_macip_add(bgp
, vni
, &mac
, &ip
,
2431 return bgp_evpn_local_macip_del(bgp
, vni
, &mac
, &ip
);
2434 static void bgp_zebra_process_local_ip_prefix(int cmd
, struct zclient
*zclient
,
2435 zebra_size_t length
,
2438 struct stream
*s
= NULL
;
2439 struct bgp
*bgp_vrf
= NULL
;
2441 char buf
[PREFIX_STRLEN
];
2443 memset(&p
, 0, sizeof(struct prefix
));
2445 stream_get(&p
, s
, sizeof(struct prefix
));
2447 bgp_vrf
= bgp_lookup_by_vrf_id(vrf_id
);
2451 if (BGP_DEBUG(zebra
, ZEBRA
))
2452 zlog_debug("Recv prefix %s %s on vrf %s",
2453 prefix2str(&p
, buf
, sizeof(buf
)),
2454 (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) ? "ADD" : "DEL",
2455 vrf_id_to_name(vrf_id
));
2457 if (cmd
== ZEBRA_IP_PREFIX_ROUTE_ADD
) {
2459 if (p
.family
== AF_INET
)
2460 return bgp_evpn_advertise_type5_route(
2461 bgp_vrf
, &p
, NULL
, AFI_IP
, SAFI_UNICAST
);
2463 return bgp_evpn_advertise_type5_route(
2464 bgp_vrf
, &p
, NULL
, AFI_IP6
, SAFI_UNICAST
);
2467 if (p
.family
== AF_INET
)
2468 return bgp_evpn_withdraw_type5_route(
2469 bgp_vrf
, &p
, AFI_IP
, SAFI_UNICAST
);
2471 return bgp_evpn_withdraw_type5_route(
2472 bgp_vrf
, &p
, AFI_IP6
, SAFI_UNICAST
);
2476 static void bgp_zebra_process_label_chunk(
2478 struct zclient
*zclient
,
2479 zebra_size_t length
,
2482 struct stream
*s
= NULL
;
2483 uint8_t response_keep
;
2487 unsigned short instance
;
2490 STREAM_GETC(s
, proto
);
2491 STREAM_GETW(s
, instance
);
2492 STREAM_GETC(s
, response_keep
);
2493 STREAM_GETL(s
, first
);
2494 STREAM_GETL(s
, last
);
2496 if (zclient
->redist_default
!= proto
) {
2497 flog_err(EC_BGP_LM_ERROR
, "Got LM msg with wrong proto %u",
2501 if (zclient
->instance
!= instance
) {
2502 flog_err(EC_BGP_LM_ERROR
, "Got LM msg with wrong instance %u",
2508 first
< MPLS_LABEL_UNRESERVED_MIN
||
2509 last
> MPLS_LABEL_UNRESERVED_MAX
) {
2511 flog_err(EC_BGP_LM_ERROR
, "%s: Invalid Label chunk: %u - %u",
2512 __func__
, first
, last
);
2515 if (BGP_DEBUG(zebra
, ZEBRA
)) {
2516 zlog_debug("Label Chunk assign: %u - %u (%u) ",
2517 first
, last
, response_keep
);
2520 bgp_lp_event_chunk(response_keep
, first
, last
);
2522 stream_failure
: /* for STREAM_GETX */
2526 extern struct zebra_privs_t bgpd_privs
;
2528 void bgp_zebra_init(struct thread_master
*master
, unsigned short instance
)
2530 zclient_num_connects
= 0;
2532 /* Set default values. */
2533 zclient
= zclient_new_notify(master
, &zclient_options_default
);
2534 zclient_init(zclient
, ZEBRA_ROUTE_BGP
, 0, &bgpd_privs
);
2535 zclient
->zebra_connected
= bgp_zebra_connected
;
2536 zclient
->router_id_update
= bgp_router_id_update
;
2537 zclient
->interface_add
= bgp_interface_add
;
2538 zclient
->interface_delete
= bgp_interface_delete
;
2539 zclient
->interface_address_add
= bgp_interface_address_add
;
2540 zclient
->interface_address_delete
= bgp_interface_address_delete
;
2541 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
2542 zclient
->interface_nbr_address_delete
=
2543 bgp_interface_nbr_address_delete
;
2544 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
2545 zclient
->redistribute_route_add
= zebra_read_route
;
2546 zclient
->redistribute_route_del
= zebra_read_route
;
2547 zclient
->interface_up
= bgp_interface_up
;
2548 zclient
->interface_down
= bgp_interface_down
;
2549 zclient
->nexthop_update
= bgp_read_nexthop_update
;
2550 zclient
->import_check_update
= bgp_read_import_check_update
;
2551 zclient
->fec_update
= bgp_read_fec_update
;
2552 zclient
->local_es_add
= bgp_zebra_process_local_es
;
2553 zclient
->local_es_del
= bgp_zebra_process_local_es
;
2554 zclient
->local_vni_add
= bgp_zebra_process_local_vni
;
2555 zclient
->local_vni_del
= bgp_zebra_process_local_vni
;
2556 zclient
->local_macip_add
= bgp_zebra_process_local_macip
;
2557 zclient
->local_macip_del
= bgp_zebra_process_local_macip
;
2558 zclient
->local_l3vni_add
= bgp_zebra_process_local_l3vni
;
2559 zclient
->local_l3vni_del
= bgp_zebra_process_local_l3vni
;
2560 zclient
->local_ip_prefix_add
= bgp_zebra_process_local_ip_prefix
;
2561 zclient
->local_ip_prefix_del
= bgp_zebra_process_local_ip_prefix
;
2562 zclient
->label_chunk
= bgp_zebra_process_label_chunk
;
2563 zclient
->rule_notify_owner
= rule_notify_owner
;
2564 zclient
->ipset_notify_owner
= ipset_notify_owner
;
2565 zclient
->ipset_entry_notify_owner
= ipset_entry_notify_owner
;
2566 zclient
->iptable_notify_owner
= iptable_notify_owner
;
2567 zclient
->instance
= instance
;
2570 void bgp_zebra_destroy(void)
2572 if (zclient
== NULL
)
2574 zclient_stop(zclient
);
2575 zclient_free(zclient
);
2579 int bgp_zebra_num_connects(void)
2581 return zclient_num_connects
;
2584 void bgp_send_pbr_rule_action(struct bgp_pbr_action
*pbra
, bool install
)
2588 if (pbra
->install_in_progress
)
2590 if (BGP_DEBUG(zebra
, ZEBRA
))
2591 zlog_debug("%s: table %d fwmark %d %d",
2592 __PRETTY_FUNCTION__
,
2593 pbra
->table_id
, pbra
->fwmark
, install
);
2597 zclient_create_header(s
,
2598 install
? ZEBRA_RULE_ADD
: ZEBRA_RULE_DELETE
,
2600 stream_putl(s
, 1); /* send one pbr action */
2602 bgp_encode_pbr_rule_action(s
, pbra
);
2604 stream_putw_at(s
, 0, stream_get_endp(s
));
2605 if (!zclient_send_message(zclient
) && install
)
2606 pbra
->install_in_progress
= true;
2609 void bgp_send_pbr_ipset_match(struct bgp_pbr_match
*pbrim
, bool install
)
2613 if (pbrim
->install_in_progress
)
2615 if (BGP_DEBUG(zebra
, ZEBRA
))
2616 zlog_debug("%s: name %s type %d %d, ID %u",
2617 __PRETTY_FUNCTION__
,
2618 pbrim
->ipset_name
, pbrim
->type
,
2619 install
, pbrim
->unique
);
2623 zclient_create_header(s
,
2624 install
? ZEBRA_IPSET_CREATE
:
2625 ZEBRA_IPSET_DESTROY
,
2628 stream_putl(s
, 1); /* send one pbr action */
2630 bgp_encode_pbr_ipset_match(s
, pbrim
);
2632 stream_putw_at(s
, 0, stream_get_endp(s
));
2633 if (!zclient_send_message(zclient
) && install
)
2634 pbrim
->install_in_progress
= true;
2637 void bgp_send_pbr_ipset_entry_match(struct bgp_pbr_match_entry
*pbrime
,
2642 if (pbrime
->install_in_progress
)
2644 if (BGP_DEBUG(zebra
, ZEBRA
))
2645 zlog_debug("%s: name %s %d %d, ID %u", __PRETTY_FUNCTION__
,
2646 pbrime
->backpointer
->ipset_name
,
2647 pbrime
->unique
, install
, pbrime
->unique
);
2651 zclient_create_header(s
,
2652 install
? ZEBRA_IPSET_ENTRY_ADD
:
2653 ZEBRA_IPSET_ENTRY_DELETE
,
2656 stream_putl(s
, 1); /* send one pbr action */
2658 bgp_encode_pbr_ipset_entry_match(s
, pbrime
);
2660 stream_putw_at(s
, 0, stream_get_endp(s
));
2661 if (!zclient_send_message(zclient
) && install
)
2662 pbrime
->install_in_progress
= true;
2665 static void bgp_encode_pbr_interface_list(struct bgp
*bgp
, struct stream
*s
)
2667 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
2668 struct bgp_pbr_interface_head
*head
;
2669 struct bgp_pbr_interface
*pbr_if
;
2670 struct interface
*ifp
;
2674 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
2676 RB_FOREACH (pbr_if
, bgp_pbr_interface_head
, head
) {
2677 ifp
= if_lookup_by_name(pbr_if
->name
, bgp
->vrf_id
);
2679 stream_putl(s
, ifp
->ifindex
);
2683 static int bgp_pbr_get_ifnumber(struct bgp
*bgp
)
2685 struct bgp_pbr_config
*bgp_pbr_cfg
= bgp
->bgp_pbr_cfg
;
2686 struct bgp_pbr_interface_head
*head
;
2687 struct bgp_pbr_interface
*pbr_if
;
2692 head
= &(bgp_pbr_cfg
->ifaces_by_name_ipv4
);
2694 RB_FOREACH (pbr_if
, bgp_pbr_interface_head
, head
) {
2695 if (if_lookup_by_name(pbr_if
->name
, bgp
->vrf_id
))
2701 void bgp_send_pbr_iptable(struct bgp_pbr_action
*pba
,
2702 struct bgp_pbr_match
*pbm
,
2709 if (pbm
->install_iptable_in_progress
)
2711 if (BGP_DEBUG(zebra
, ZEBRA
))
2712 zlog_debug("%s: name %s type %d mark %d %d, ID %u",
2713 __PRETTY_FUNCTION__
, pbm
->ipset_name
,
2714 pbm
->type
, pba
->fwmark
, install
,
2719 zclient_create_header(s
,
2720 install
? ZEBRA_IPTABLE_ADD
:
2721 ZEBRA_IPTABLE_DELETE
,
2724 bgp_encode_pbr_iptable_match(s
, pba
, pbm
);
2725 nb_interface
= bgp_pbr_get_ifnumber(pba
->bgp
);
2726 stream_putl(s
, nb_interface
);
2728 bgp_encode_pbr_interface_list(pba
->bgp
, s
);
2729 stream_putw_at(s
, 0, stream_get_endp(s
));
2730 ret
= zclient_send_message(zclient
);
2735 pbm
->install_iptable_in_progress
= true;
2739 /* inject in table <table_id> a default route to:
2740 * - if nexthop IP is present : to this nexthop
2741 * - if vrf is different from local : to the matching VRF
2743 void bgp_zebra_announce_default(struct bgp
*bgp
, struct nexthop
*nh
,
2744 afi_t afi
, uint32_t table_id
, bool announce
)
2746 struct zapi_nexthop
*api_nh
;
2747 struct zapi_route api
;
2750 if (!nh
|| nh
->type
!= NEXTHOP_TYPE_IPV4
2751 || nh
->vrf_id
== VRF_UNKNOWN
)
2753 memset(&p
, 0, sizeof(struct prefix
));
2758 memset(&api
, 0, sizeof(api
));
2759 api
.vrf_id
= bgp
->vrf_id
;
2760 api
.type
= ZEBRA_ROUTE_BGP
;
2761 api
.safi
= SAFI_UNICAST
;
2763 api
.tableid
= table_id
;
2764 api
.nexthop_num
= 1;
2765 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
2766 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
2767 api_nh
= &api
.nexthops
[0];
2770 if (nh
->gate
.ipv4
.s_addr
) {
2771 char buff
[PREFIX_STRLEN
];
2773 api_nh
->vrf_id
= nh
->vrf_id
;
2774 api_nh
->gate
.ipv4
= nh
->gate
.ipv4
;
2775 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
2777 inet_ntop(AF_INET
, &(nh
->gate
.ipv4
), buff
, INET_ADDRSTRLEN
);
2778 if (BGP_DEBUG(zebra
, ZEBRA
))
2779 zlog_info("BGP: %s default route to %s table %d (redirect IP)",
2780 announce
? "adding" : "withdrawing",
2782 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
2783 : ZEBRA_ROUTE_DELETE
,
2785 } else if (nh
->vrf_id
!= bgp
->vrf_id
) {
2787 struct interface
*ifp
;
2789 vrf
= vrf_lookup_by_id(nh
->vrf_id
);
2792 /* create default route with interface <VRF>
2793 * with nexthop-vrf <VRF>
2795 ifp
= if_lookup_by_name_all_vrf(vrf
->name
);
2798 api_nh
->vrf_id
= nh
->vrf_id
;
2799 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
2800 api_nh
->ifindex
= ifp
->ifindex
;
2801 if (BGP_DEBUG(zebra
, ZEBRA
))
2802 zlog_info("BGP: %s default route to %s table %d (redirect VRF)",
2803 announce
? "adding" : "withdrawing",
2804 vrf
->name
, table_id
);
2805 zclient_route_send(announce
? ZEBRA_ROUTE_ADD
2806 : ZEBRA_ROUTE_DELETE
,