2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "sockunion.h"
40 #include "bgpd/bgpd.h"
41 #include "bgpd/bgp_route.h"
42 #include "bgpd/bgp_attr.h"
43 #include "bgpd/bgp_nexthop.h"
44 #include "bgpd/bgp_zebra.h"
45 #include "bgpd/bgp_fsm.h"
46 #include "bgpd/bgp_debug.h"
47 #include "bgpd/bgp_mpath.h"
48 #include "bgpd/bgp_nexthop.h"
49 #include "bgpd/bgp_nht.h"
50 #include "bgpd/bgp_bfd.h"
51 #include "bgpd/bgp_label.h"
53 #include "bgpd/rfapi/rfapi_backend.h"
54 #include "bgpd/rfapi/vnc_export_bgp.h"
56 #include "bgpd/bgp_evpn.h"
58 /* All information about zebra. */
59 struct zclient
*zclient
= NULL
;
61 /* Growable buffer for nexthops sent to zebra */
62 struct stream
*bgp_nexthop_buf
= NULL
;
63 struct stream
*bgp_ifindices_buf
= NULL
;
64 struct stream
*bgp_label_buf
= NULL
;
66 /* These array buffers are used in making a copy of the attributes for
67 route-map apply. Arrays are being used here to minimize mallocs and
68 frees for the temporary copy of the attributes.
69 Given the zapi api expects the nexthop buffer to contain pointer to
70 pointers for nexthops, we couldnt have used a single nexthop variable
71 on the stack, hence we had two options:
72 1. maintain a linked-list and free it after zapi_*_route call
73 2. use an array to avoid number of mallocs.
74 Number of supported next-hops are finite, use of arrays should be ok. */
75 struct attr attr_cp
[MULTIPATH_NUM
];
76 unsigned int attr_index
= 0;
78 /* Once per address-family initialization of the attribute array */
79 #define BGP_INFO_ATTR_BUF_INIT() \
81 memset(attr_cp, 0, MULTIPATH_NUM * sizeof(struct attr)); \
85 #define BGP_INFO_ATTR_BUF_COPY(info_src, info_dst) \
87 *info_dst = *info_src; \
88 assert(attr_index != multipath_num); \
89 bgp_attr_dup(&attr_cp[attr_index], info_src->attr); \
90 bgp_attr_deep_dup(&attr_cp[attr_index], info_src->attr); \
91 info_dst->attr = &attr_cp[attr_index]; \
95 #define BGP_INFO_ATTR_BUF_FREE(info) \
97 bgp_attr_deep_free(info->attr); \
101 /* Can we install into zebra? */
102 static inline int bgp_install_info_to_zebra(struct bgp
*bgp
)
104 if (zclient
->sock
<= 0)
107 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
113 int zclient_num_connects
;
115 /* Router-id update message from zebra. */
116 static int bgp_router_id_update(int command
, struct zclient
*zclient
,
117 zebra_size_t length
, vrf_id_t vrf_id
)
119 struct prefix router_id
;
121 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
123 if (BGP_DEBUG(zebra
, ZEBRA
)) {
124 char buf
[PREFIX2STR_BUFFER
];
125 prefix2str(&router_id
, buf
, sizeof(buf
));
126 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id
, buf
);
129 bgp_router_id_zebra_bump(vrf_id
, &router_id
);
133 /* Nexthop update message from zebra. */
134 static int bgp_read_nexthop_update(int command
, struct zclient
*zclient
,
135 zebra_size_t length
, vrf_id_t vrf_id
)
137 bgp_parse_nexthop_update(command
, vrf_id
);
141 static int bgp_read_import_check_update(int command
, struct zclient
*zclient
,
142 zebra_size_t length
, vrf_id_t vrf_id
)
144 bgp_parse_nexthop_update(command
, vrf_id
);
148 /* Set or clear interface on which unnumbered neighbor is configured. This
149 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
152 static void bgp_update_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
,
153 struct interface
*upd_ifp
)
155 struct listnode
*node
, *nnode
;
158 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
159 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)) {
162 bgp_zebra_initiate_radv(bgp
, peer
);
164 bgp_zebra_terminate_radv(bgp
, peer
);
171 static int bgp_read_fec_update(int command
, struct zclient
*zclient
,
174 bgp_parse_fec_update();
178 static void bgp_start_interface_nbrs(struct bgp
*bgp
, struct interface
*ifp
)
180 struct listnode
*node
, *nnode
;
183 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
184 if (peer
->conf_if
&& (strcmp(peer
->conf_if
, ifp
->name
) == 0)
185 && peer
->status
!= Established
) {
186 if (peer_active(peer
))
187 BGP_EVENT_ADD(peer
, BGP_Stop
);
188 BGP_EVENT_ADD(peer
, BGP_Start
);
193 static void bgp_nbr_connected_add(struct bgp
*bgp
, struct nbr_connected
*ifc
)
195 struct listnode
*node
;
196 struct connected
*connected
;
197 struct interface
*ifp
;
200 /* Kick-off the FSM for any relevant peers only if there is a
201 * valid local address on the interface.
204 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
205 p
= connected
->address
;
206 if (p
->family
== AF_INET6
207 && IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
213 bgp_start_interface_nbrs(bgp
, ifp
);
216 static void bgp_nbr_connected_delete(struct bgp
*bgp
, struct nbr_connected
*ifc
,
219 struct listnode
*node
, *nnode
;
221 struct interface
*ifp
;
223 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
225 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)) {
226 peer
->last_reset
= PEER_DOWN_NBR_ADDR_DEL
;
227 BGP_EVENT_ADD(peer
, BGP_Stop
);
230 /* Free neighbor also, if we're asked to. */
233 listnode_delete(ifp
->nbr_connected
, ifc
);
234 nbr_connected_free(ifc
);
238 /* Inteface addition message from zebra. */
239 static int bgp_interface_add(int command
, struct zclient
*zclient
,
240 zebra_size_t length
, vrf_id_t vrf_id
)
242 struct interface
*ifp
;
245 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
246 if (!ifp
) // unexpected
249 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
250 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id
, ifp
->name
);
252 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
256 bgp_update_interface_nbrs(bgp
, ifp
, ifp
);
260 static int bgp_interface_delete(int command
, struct zclient
*zclient
,
261 zebra_size_t length
, vrf_id_t vrf_id
)
264 struct interface
*ifp
;
268 ifp
= zebra_interface_state_read(s
, vrf_id
);
269 if (!ifp
) /* This may happen if we've just unregistered for a VRF. */
272 ifp
->ifindex
= IFINDEX_DELETED
;
274 if (BGP_DEBUG(zebra
, ZEBRA
))
275 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id
, ifp
->name
);
277 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
281 bgp_update_interface_nbrs(bgp
, ifp
, NULL
);
285 static int bgp_interface_up(int command
, struct zclient
*zclient
,
286 zebra_size_t length
, vrf_id_t vrf_id
)
289 struct interface
*ifp
;
291 struct nbr_connected
*nc
;
292 struct listnode
*node
, *nnode
;
296 ifp
= zebra_interface_state_read(s
, vrf_id
);
301 if (BGP_DEBUG(zebra
, ZEBRA
))
302 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id
, ifp
->name
);
304 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
308 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
309 bgp_connected_add(bgp
, c
);
311 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
312 bgp_nbr_connected_add(bgp
, nc
);
317 static int bgp_interface_down(int command
, struct zclient
*zclient
,
318 zebra_size_t length
, vrf_id_t vrf_id
)
321 struct interface
*ifp
;
323 struct nbr_connected
*nc
;
324 struct listnode
*node
, *nnode
;
328 ifp
= zebra_interface_state_read(s
, vrf_id
);
332 if (BGP_DEBUG(zebra
, ZEBRA
))
333 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id
, ifp
->name
);
335 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
339 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
340 bgp_connected_delete(bgp
, c
);
342 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
343 bgp_nbr_connected_delete(bgp
, nc
, 1);
345 /* Fast external-failover */
349 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
352 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
353 #if defined(HAVE_CUMULUS)
354 /* Take down directly connected EBGP peers as well as
356 * tracked (directly connected) IBGP peers.
358 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1)
360 || bgp_bfd_is_peer_multihop(peer
)))
362 /* Take down directly connected EBGP peers */
363 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
367 if (ifp
== peer
->nexthop
.ifp
) {
368 BGP_EVENT_ADD(peer
, BGP_Stop
);
369 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
377 static int bgp_interface_address_add(int command
, struct zclient
*zclient
,
378 zebra_size_t length
, vrf_id_t vrf_id
)
380 struct connected
*ifc
;
382 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
387 if (bgp_debug_zebra(ifc
->address
)) {
388 char buf
[PREFIX2STR_BUFFER
];
389 prefix2str(ifc
->address
, buf
, sizeof(buf
));
390 zlog_debug("Rx Intf address add VRF %u IF %s addr %s", vrf_id
,
391 ifc
->ifp
->name
, buf
);
394 if (if_is_operative(ifc
->ifp
)) {
397 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
401 bgp_connected_add(bgp
, ifc
);
402 /* If we have learnt of any neighbors on this interface,
403 * check to kick off any BGP interface-based neighbors,
404 * but only if this is a link-local address.
406 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
)
407 && !list_isempty(ifc
->ifp
->nbr_connected
))
408 bgp_start_interface_nbrs(bgp
, ifc
->ifp
);
414 static int bgp_interface_address_delete(int command
, struct zclient
*zclient
,
415 zebra_size_t length
, vrf_id_t vrf_id
)
417 struct connected
*ifc
;
420 ifc
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
425 if (bgp_debug_zebra(ifc
->address
)) {
426 char buf
[PREFIX2STR_BUFFER
];
427 prefix2str(ifc
->address
, buf
, sizeof(buf
));
428 zlog_debug("Rx Intf address del VRF %u IF %s addr %s", vrf_id
,
429 ifc
->ifp
->name
, buf
);
432 if (if_is_operative(ifc
->ifp
)) {
433 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
435 bgp_connected_delete(bgp
, ifc
);
443 static int bgp_interface_nbr_address_add(int command
, struct zclient
*zclient
,
444 zebra_size_t length
, vrf_id_t vrf_id
)
446 struct nbr_connected
*ifc
= NULL
;
449 ifc
= zebra_interface_nbr_address_read(command
, zclient
->ibuf
, vrf_id
);
454 if (bgp_debug_zebra(ifc
->address
)) {
455 char buf
[PREFIX2STR_BUFFER
];
456 prefix2str(ifc
->address
, buf
, sizeof(buf
));
457 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s", vrf_id
,
458 ifc
->ifp
->name
, buf
);
461 if (if_is_operative(ifc
->ifp
)) {
462 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
464 bgp_nbr_connected_add(bgp
, ifc
);
470 static int bgp_interface_nbr_address_delete(int command
,
471 struct zclient
*zclient
,
475 struct nbr_connected
*ifc
= NULL
;
478 ifc
= zebra_interface_nbr_address_read(command
, zclient
->ibuf
, vrf_id
);
483 if (bgp_debug_zebra(ifc
->address
)) {
484 char buf
[PREFIX2STR_BUFFER
];
485 prefix2str(ifc
->address
, buf
, sizeof(buf
));
486 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s", vrf_id
,
487 ifc
->ifp
->name
, buf
);
490 if (if_is_operative(ifc
->ifp
)) {
491 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
493 bgp_nbr_connected_delete(bgp
, ifc
, 0);
496 nbr_connected_free(ifc
);
501 /* VRF update for an interface. */
502 static int bgp_interface_vrf_update(int command
, struct zclient
*zclient
,
503 zebra_size_t length
, vrf_id_t vrf_id
)
505 struct interface
*ifp
;
508 struct nbr_connected
*nc
;
509 struct listnode
*node
, *nnode
;
512 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
,
517 if (BGP_DEBUG(zebra
, ZEBRA
) && ifp
)
518 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id
,
519 ifp
->name
, new_vrf_id
);
521 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
525 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
526 bgp_connected_delete(bgp
, c
);
528 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
529 bgp_nbr_connected_delete(bgp
, nc
, 1);
531 /* Fast external-failover */
535 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
538 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
539 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
542 if (ifp
== peer
->nexthop
.ifp
)
543 BGP_EVENT_ADD(peer
, BGP_Stop
);
547 if_update_to_new_vrf(ifp
, new_vrf_id
);
549 bgp
= bgp_lookup_by_vrf_id(new_vrf_id
);
553 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, c
))
554 bgp_connected_add(bgp
, c
);
556 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, node
, nnode
, nc
))
557 bgp_nbr_connected_add(bgp
, nc
);
561 /* Zebra route add and delete treatment. */
562 static int zebra_read_ipv4(int command
, struct zclient
*zclient
,
563 zebra_size_t length
, vrf_id_t vrf_id
)
566 struct zapi_ipv4 api
;
567 struct in_addr nexthop
;
568 struct prefix_ipv4 p
;
569 unsigned int ifindex
;
573 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
580 /* Type, flags, message. */
581 api
.type
= stream_getc(s
);
582 api
.instance
= stream_getw(s
);
583 api
.flags
= stream_getl(s
);
584 api
.message
= stream_getc(s
);
587 memset(&p
, 0, sizeof(struct prefix_ipv4
));
589 p
.prefixlen
= MIN(IPV4_MAX_PREFIXLEN
, stream_getc(s
));
590 stream_get(&p
.prefix
, s
, PSIZE(p
.prefixlen
));
592 /* Nexthop, ifindex, distance, metric. */
593 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
594 api
.nexthop_num
= stream_getc(s
);
595 nexthop
.s_addr
= stream_get_ipv4(s
);
598 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_IFINDEX
)) {
599 api
.ifindex_num
= stream_getc(s
);
600 ifindex
= stream_getl(s
); /* ifindex, unused */
605 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
606 api
.distance
= stream_getc(s
);
608 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
609 api
.metric
= stream_getl(s
);
613 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
614 api
.tag
= stream_getl(s
);
618 if (command
== ZEBRA_REDISTRIBUTE_IPV4_ADD
) {
619 if (bgp_debug_zebra((struct prefix
*)&p
)) {
620 char buf
[2][INET_ADDRSTRLEN
];
622 "Rx IPv4 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %" ROUTE_TAG_PRI
,
623 vrf_id
, zebra_route_string(api
.type
),
625 inet_ntop(AF_INET
, &p
.prefix
, buf
[0],
628 inet_ntop(AF_INET
, &nexthop
, buf
[1],
630 api
.metric
, api
.tag
);
634 * The ADD message is actually an UPDATE and there is no
636 * for a prior redistributed route, if any. So, perform an
638 * DEL processing for the same redistributed route from any
642 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
644 bgp_redistribute_delete(bgp
,
645 (struct prefix
*)&p
, i
,
649 /* Now perform the add/update. */
650 bgp_redistribute_add(bgp
, (struct prefix
*)&p
, &nexthop
, NULL
,
651 ifindex
, api
.metric
, api
.type
,
652 api
.instance
, api
.tag
);
653 } else if (command
== ZEBRA_REDISTRIBUTE_IPV4_DEL
) {
654 if (bgp_debug_zebra((struct prefix
*)&p
)) {
655 char buf
[2][INET_ADDRSTRLEN
];
657 "Rx IPv4 route delete VRF %u %s[%d] %s/%d "
658 "nexthop %s metric %u tag %" ROUTE_TAG_PRI
,
659 vrf_id
, zebra_route_string(api
.type
),
661 inet_ntop(AF_INET
, &p
.prefix
, buf
[0],
664 inet_ntop(AF_INET
, &nexthop
, buf
[1],
666 api
.metric
, api
.tag
);
668 bgp_redistribute_delete(bgp
, (struct prefix
*)&p
, api
.type
,
675 /* Zebra route add and delete treatment. */
676 static int zebra_read_ipv6(int command
, struct zclient
*zclient
,
677 zebra_size_t length
, vrf_id_t vrf_id
)
680 struct zapi_ipv6 api
;
681 struct in6_addr nexthop
;
682 struct prefix_ipv6 p
, src_p
;
683 unsigned int ifindex
;
687 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
692 memset(&nexthop
, 0, sizeof(struct in6_addr
));
694 /* Type, flags, message. */
695 api
.type
= stream_getc(s
);
696 api
.instance
= stream_getw(s
);
697 api
.flags
= stream_getl(s
);
698 api
.message
= stream_getc(s
);
701 memset(&p
, 0, sizeof(struct prefix_ipv6
));
703 p
.prefixlen
= MIN(IPV6_MAX_PREFIXLEN
, stream_getc(s
));
704 stream_get(&p
.prefix
, s
, PSIZE(p
.prefixlen
));
706 memset(&src_p
, 0, sizeof(struct prefix_ipv6
));
707 src_p
.family
= AF_INET6
;
708 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
709 src_p
.prefixlen
= stream_getc(s
);
710 stream_get(&src_p
.prefix
, s
, PSIZE(src_p
.prefixlen
));
714 /* we completely ignore srcdest routes for now. */
717 /* Nexthop, ifindex, distance, metric. */
718 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
719 api
.nexthop_num
= stream_getc(s
);
720 stream_get(&nexthop
, s
, 16);
723 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_IFINDEX
)) {
724 api
.ifindex_num
= stream_getc(s
);
725 ifindex
= stream_getl(s
); /* ifindex, unused */
730 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
731 api
.distance
= stream_getc(s
);
735 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
736 api
.metric
= stream_getl(s
);
740 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
741 api
.tag
= stream_getl(s
);
745 /* Simply ignore link-local address. */
746 if (IN6_IS_ADDR_LINKLOCAL(&p
.prefix
))
749 if (command
== ZEBRA_REDISTRIBUTE_IPV6_ADD
) {
750 if (bgp_debug_zebra((struct prefix
*)&p
)) {
751 char buf
[2][INET6_ADDRSTRLEN
];
753 "Rx IPv6 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %" ROUTE_TAG_PRI
,
754 vrf_id
, zebra_route_string(api
.type
),
756 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0],
759 inet_ntop(AF_INET
, &nexthop
, buf
[1],
761 api
.metric
, api
.tag
);
765 * The ADD message is actually an UPDATE and there is no
767 * for a prior redistributed route, if any. So, perform an
769 * DEL processing for the same redistributed route from any
773 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
775 bgp_redistribute_delete(bgp
,
776 (struct prefix
*)&p
, i
,
780 bgp_redistribute_add(bgp
, (struct prefix
*)&p
, NULL
, &nexthop
,
781 ifindex
, api
.metric
, api
.type
,
782 api
.instance
, api
.tag
);
783 } else if (command
== ZEBRA_REDISTRIBUTE_IPV6_DEL
) {
784 if (bgp_debug_zebra((struct prefix
*)&p
)) {
785 char buf
[2][INET6_ADDRSTRLEN
];
787 "Rx IPv6 route delete VRF %u %s[%d] %s/%d "
788 "nexthop %s metric %u tag %" ROUTE_TAG_PRI
,
789 vrf_id
, zebra_route_string(api
.type
),
791 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0],
794 inet_ntop(AF_INET6
, &nexthop
, buf
[1],
796 api
.metric
, api
.tag
);
798 bgp_redistribute_delete(bgp
, (struct prefix
*)&p
, api
.type
,
805 struct interface
*if_lookup_by_ipv4(struct in_addr
*addr
, vrf_id_t vrf_id
)
807 struct listnode
*ifnode
;
808 struct listnode
*cnode
;
809 struct interface
*ifp
;
810 struct connected
*connected
;
811 struct prefix_ipv4 p
;
816 p
.prefixlen
= IPV4_MAX_BITLEN
;
818 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), ifnode
, ifp
)) {
819 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
820 cp
= connected
->address
;
822 if (cp
->family
== AF_INET
)
823 if (prefix_match(cp
, (struct prefix
*)&p
))
830 struct interface
*if_lookup_by_ipv4_exact(struct in_addr
*addr
, vrf_id_t vrf_id
)
832 struct listnode
*ifnode
;
833 struct listnode
*cnode
;
834 struct interface
*ifp
;
835 struct connected
*connected
;
838 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), ifnode
, ifp
)) {
839 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
840 cp
= connected
->address
;
842 if (cp
->family
== AF_INET
)
843 if (IPV4_ADDR_SAME(&cp
->u
.prefix4
, addr
))
850 struct interface
*if_lookup_by_ipv6(struct in6_addr
*addr
, ifindex_t ifindex
,
853 struct listnode
*ifnode
;
854 struct listnode
*cnode
;
855 struct interface
*ifp
;
856 struct connected
*connected
;
857 struct prefix_ipv6 p
;
862 p
.prefixlen
= IPV6_MAX_BITLEN
;
864 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), ifnode
, ifp
)) {
865 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
866 cp
= connected
->address
;
868 if (cp
->family
== AF_INET6
)
869 if (prefix_match(cp
, (struct prefix
*)&p
)) {
870 if (IN6_IS_ADDR_LINKLOCAL(
872 if (ifindex
== ifp
->ifindex
)
882 struct interface
*if_lookup_by_ipv6_exact(struct in6_addr
*addr
,
883 ifindex_t ifindex
, vrf_id_t vrf_id
)
885 struct listnode
*ifnode
;
886 struct listnode
*cnode
;
887 struct interface
*ifp
;
888 struct connected
*connected
;
891 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id
), ifnode
, ifp
)) {
892 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
893 cp
= connected
->address
;
895 if (cp
->family
== AF_INET6
)
896 if (IPV6_ADDR_SAME(&cp
->u
.prefix6
, addr
)) {
897 if (IN6_IS_ADDR_LINKLOCAL(
899 if (ifindex
== ifp
->ifindex
)
909 static int if_get_ipv6_global(struct interface
*ifp
, struct in6_addr
*addr
)
911 struct listnode
*cnode
;
912 struct connected
*connected
;
915 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
916 cp
= connected
->address
;
918 if (cp
->family
== AF_INET6
)
919 if (!IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
920 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
927 static int if_get_ipv6_local(struct interface
*ifp
, struct in6_addr
*addr
)
929 struct listnode
*cnode
;
930 struct connected
*connected
;
933 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
934 cp
= connected
->address
;
936 if (cp
->family
== AF_INET6
)
937 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
)) {
938 memcpy(addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
945 static int if_get_ipv4_address(struct interface
*ifp
, struct in_addr
*addr
)
947 struct listnode
*cnode
;
948 struct connected
*connected
;
951 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, connected
)) {
952 cp
= connected
->address
;
953 if ((cp
->family
== AF_INET
)
954 && !ipv4_martian(&(cp
->u
.prefix4
))) {
955 *addr
= cp
->u
.prefix4
;
962 int bgp_nexthop_set(union sockunion
*local
, union sockunion
*remote
,
963 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
966 struct interface
*ifp
= NULL
;
968 memset(nexthop
, 0, sizeof(struct bgp_nexthop
));
975 if (local
->sa
.sa_family
== AF_INET
) {
976 nexthop
->v4
= local
->sin
.sin_addr
;
978 ifp
= if_lookup_by_name(peer
->update_if
,
981 ifp
= if_lookup_by_ipv4_exact(&local
->sin
.sin_addr
,
984 if (local
->sa
.sa_family
== AF_INET6
) {
985 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
986 if (peer
->conf_if
|| peer
->ifname
)
987 ifp
= if_lookup_by_name(peer
->conf_if
991 } else if (peer
->update_if
)
992 ifp
= if_lookup_by_name(peer
->update_if
,
995 ifp
= if_lookup_by_ipv6_exact(&local
->sin6
.sin6_addr
,
996 local
->sin6
.sin6_scope_id
,
1005 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
1006 if (local
->sa
.sa_family
== AF_INET
) {
1008 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
1011 /* There is no global nexthop. Use link-local address as
1013 * global and link-local nexthop. In this scenario, the
1015 * for interop is that the network admin would use a
1017 * specify the global IPv6 nexthop.
1019 if_get_ipv6_local(ifp
, &nexthop
->v6_global
);
1020 memcpy(&nexthop
->v6_local
, &nexthop
->v6_global
,
1023 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
1025 if (if_lookup_by_ipv4(&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
1026 peer
->shared_network
= 1;
1028 peer
->shared_network
= 0;
1031 /* IPv6 connection, fetch and store IPv4 local address if any. */
1032 if (local
->sa
.sa_family
== AF_INET6
) {
1033 struct interface
*direct
= NULL
;
1036 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
1037 if (!ret
&& peer
->local_id
.s_addr
)
1038 nexthop
->v4
= peer
->local_id
;
1041 if (!IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)) {
1042 memcpy(&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
1045 /* If directory connected set link-local address. */
1046 direct
= if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
1047 remote
->sin6
.sin6_scope_id
,
1050 if_get_ipv6_local(ifp
, &nexthop
->v6_local
);
1052 /* Link-local address. */
1054 ret
= if_get_ipv6_global(ifp
, &nexthop
->v6_global
);
1056 /* If there is no global address. Set link-local
1058 global. I know this break RFC specification... */
1059 /* In this scenario, the expectation for interop is that
1061 * network admin would use a route-map to specify the
1066 memcpy(&nexthop
->v6_global
,
1067 &local
->sin6
.sin6_addr
,
1069 /* Always set the link-local address */
1070 memcpy(&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
1074 if (IN6_IS_ADDR_LINKLOCAL(&local
->sin6
.sin6_addr
)
1075 || if_lookup_by_ipv6(&remote
->sin6
.sin6_addr
,
1076 remote
->sin6
.sin6_scope_id
,
1078 peer
->shared_network
= 1;
1080 peer
->shared_network
= 0;
1083 /* KAME stack specific treatment. */
1085 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_global
)
1086 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
)) {
1087 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_global
, 0);
1089 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->v6_local
)
1090 && IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
)) {
1091 SET_IN6_LINKLOCAL_IFINDEX(nexthop
->v6_local
, 0);
1095 /* If we have identified the local interface, there is no error for now.
1100 static struct in6_addr
*bgp_info_to_ipv6_nexthop(struct bgp_info
*info
)
1102 struct in6_addr
*nexthop
= NULL
;
1104 /* Only global address nexthop exists. */
1105 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
)
1106 nexthop
= &info
->attr
->mp_nexthop_global
;
1108 /* If both global and link-local address present. */
1109 if (info
->attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
1110 /* Check if route-map is set to prefer global over link-local */
1111 if (info
->attr
->mp_nexthop_prefer_global
)
1112 nexthop
= &info
->attr
->mp_nexthop_global
;
1114 /* Workaround for Cisco's nexthop bug. */
1115 if (IN6_IS_ADDR_UNSPECIFIED(
1116 &info
->attr
->mp_nexthop_global
)
1117 && info
->peer
->su_remote
->sa
.sa_family
== AF_INET6
)
1119 &info
->peer
->su_remote
->sin6
.sin6_addr
;
1121 nexthop
= &info
->attr
->mp_nexthop_local
;
1128 static int bgp_table_map_apply(struct route_map
*map
, struct prefix
*p
,
1129 struct bgp_info
*info
)
1131 if (route_map_apply(map
, p
, RMAP_BGP
, info
) != RMAP_DENYMATCH
)
1134 if (bgp_debug_zebra(p
)) {
1135 if (p
->family
== AF_INET
) {
1136 char buf
[2][INET_ADDRSTRLEN
];
1138 "Zebra rmap deny: IPv4 route %s/%d nexthop %s",
1139 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0],
1142 inet_ntop(AF_INET
, &info
->attr
->nexthop
, buf
[1],
1145 if (p
->family
== AF_INET6
) {
1146 char buf
[2][INET6_ADDRSTRLEN
];
1148 "Zebra rmap deny: IPv6 route %s/%d nexthop %s",
1149 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0],
1153 bgp_info_to_ipv6_nexthop(info
),
1154 buf
[1], sizeof(buf
[1])));
1160 void bgp_zebra_announce(struct bgp_node
*rn
, struct prefix
*p
,
1161 struct bgp_info
*info
, struct bgp
*bgp
, afi_t afi
,
1167 struct bgp_info
*mpinfo
;
1169 struct bgp_info local_info
;
1170 struct bgp_info
*info_cp
= &local_info
;
1174 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1175 * know of this instance.
1177 if (!bgp_install_info_to_zebra(bgp
))
1180 if ((p
->family
== AF_INET
1181 && !vrf_bitmap_check(zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_BGP
],
1183 || (p
->family
== AF_INET6
1184 && !vrf_bitmap_check(zclient
->redist
[AFI_IP6
][ZEBRA_ROUTE_BGP
],
1188 if (bgp
->main_zebra_update_hold
)
1194 tag
= info
->attr
->tag
;
1196 /* When we create an aggregate route we must also install a Null0 route
1199 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1200 SET_FLAG(flags
, ZEBRA_FLAG_BLACKHOLE
);
1202 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
1203 || info
->sub_type
== BGP_ROUTE_AGGREGATE
) {
1204 SET_FLAG(flags
, ZEBRA_FLAG_IBGP
);
1205 SET_FLAG(flags
, ZEBRA_FLAG_INTERNAL
);
1208 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1209 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1210 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1212 SET_FLAG(flags
, ZEBRA_FLAG_INTERNAL
);
1214 if (p
->family
== AF_INET
&& !BGP_ATTR_NEXTHOP_AFI_IP6(info
->attr
)) {
1215 struct zapi_ipv4 api
;
1216 struct in_addr
*nexthop
;
1217 char buf
[2][INET_ADDRSTRLEN
];
1218 int valid_nh_count
= 0;
1219 int has_valid_label
= 0;
1221 /* resize nexthop buffer size if necessary */
1222 stream_reset(bgp_nexthop_buf
);
1225 stream_reset(bgp_label_buf
);
1227 if (bgp
->table_map
[afi
][safi
].name
)
1228 BGP_INFO_ATTR_BUF_INIT();
1230 /* Metric is currently based on the best-path only */
1231 metric
= info
->attr
->med
;
1232 for (mpinfo
= info
; mpinfo
;
1233 mpinfo
= bgp_info_mpath_next(mpinfo
)) {
1236 if (bgp
->table_map
[afi
][safi
].name
) {
1237 /* Copy info and attributes, so the route-map
1238 apply doesn't modify the
1240 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
1241 if (bgp_table_map_apply(
1242 bgp
->table_map
[afi
][safi
].map
, p
,
1244 if (mpinfo
== info
) {
1245 /* Metric is currently based on
1246 * the best-path only */
1247 metric
= info_cp
->attr
->med
;
1248 tag
= info_cp
->attr
->tag
;
1250 nexthop
= &info_cp
->attr
->nexthop
;
1252 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1254 nexthop
= &mpinfo
->attr
->nexthop
;
1256 if (nexthop
== NULL
)
1259 stream_put(bgp_nexthop_buf
, &nexthop
,
1260 sizeof(struct in_addr
*));
1262 && bgp_is_valid_label(&mpinfo
->extra
->label
)) {
1263 has_valid_label
= 1;
1264 label
= label_pton(&mpinfo
->extra
->label
);
1265 stream_put(bgp_label_buf
, &label
,
1266 sizeof(mpls_label_t
));
1271 api
.vrf_id
= bgp
->vrf_id
;
1273 api
.type
= ZEBRA_ROUTE_BGP
;
1277 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1279 if (has_valid_label
)
1280 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
1282 /* Note that this currently only applies to Null0 routes for
1284 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv4_route to encode a
1286 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero
1288 * do not want to also encode the 0.0.0.0 nexthop for the
1291 if (CHECK_FLAG(flags
, ZEBRA_FLAG_BLACKHOLE
))
1292 api
.nexthop_num
= 0;
1294 api
.nexthop_num
= valid_nh_count
;
1296 api
.nexthop
= (struct in_addr
**)STREAM_DATA(bgp_nexthop_buf
);
1297 if (has_valid_label
) {
1298 api
.label_num
= valid_nh_count
;
1299 api
.label
= (unsigned int *)STREAM_DATA(bgp_label_buf
);
1304 api
.ifindex_num
= 0;
1305 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1306 api
.metric
= metric
;
1310 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1314 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1316 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1317 api
.distance
= distance
;
1320 if (bgp_debug_zebra(p
)) {
1324 "Tx IPv4 route %s VRF %u %s/%d metric %u tag %" ROUTE_TAG_PRI
1326 (valid_nh_count
? "add" : "delete"),
1328 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0],
1330 p
->prefixlen
, api
.metric
, api
.tag
,
1332 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1333 label_buf
[0] = '\0';
1334 if (has_valid_label
)
1335 sprintf(label_buf
, "label %u",
1337 zlog_debug(" nhop [%d]: %s %s", i
+ 1,
1338 inet_ntop(AF_INET
, api
.nexthop
[i
],
1339 buf
[1], sizeof(buf
[1])),
1344 zapi_ipv4_route(valid_nh_count
? ZEBRA_IPV4_ROUTE_ADD
1345 : ZEBRA_IPV4_ROUTE_DELETE
,
1346 zclient
, (struct prefix_ipv4
*)p
, &api
);
1349 /* We have to think about a IPv6 link-local address curse. */
1350 if (p
->family
== AF_INET6
1351 || (p
->family
== AF_INET
&& BGP_ATTR_NEXTHOP_AFI_IP6(info
->attr
))) {
1353 struct in6_addr
*nexthop
;
1354 struct zapi_ipv6 api
;
1355 int valid_nh_count
= 0;
1356 char buf
[2][INET6_ADDRSTRLEN
];
1357 int has_valid_label
= 0;
1359 stream_reset(bgp_nexthop_buf
);
1360 stream_reset(bgp_ifindices_buf
);
1361 stream_reset(bgp_label_buf
);
1366 if (bgp
->table_map
[afi
][safi
].name
)
1367 BGP_INFO_ATTR_BUF_INIT();
1369 metric
= info
->attr
->med
;
1370 for (mpinfo
= info
; mpinfo
;
1371 mpinfo
= bgp_info_mpath_next(mpinfo
)) {
1375 if (bgp
->table_map
[afi
][safi
].name
) {
1376 /* Copy info and attributes, so the route-map
1377 apply doesn't modify the
1379 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
1380 if (bgp_table_map_apply(
1381 bgp
->table_map
[afi
][safi
].map
, p
,
1383 if (mpinfo
== info
) {
1384 metric
= info_cp
->attr
->med
;
1385 tag
= info_cp
->attr
->tag
;
1387 nexthop
= bgp_info_to_ipv6_nexthop(
1390 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1392 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo
);
1394 if (nexthop
== NULL
)
1397 if ((mpinfo
== info
)
1398 && mpinfo
->attr
->mp_nexthop_len
1399 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1400 if (mpinfo
->peer
->nexthop
.ifp
)
1401 ifindex
= mpinfo
->peer
->nexthop
.ifp
1405 if (mpinfo
->peer
->conf_if
1406 || mpinfo
->peer
->ifname
)
1407 ifindex
= ifname2ifindex(
1408 mpinfo
->peer
->conf_if
1409 ? mpinfo
->peer
->conf_if
1410 : mpinfo
->peer
->ifname
,
1412 else if (mpinfo
->peer
->nexthop
.ifp
)
1413 ifindex
= mpinfo
->peer
->nexthop
.ifp
1419 stream_put(bgp_nexthop_buf
, &nexthop
,
1420 sizeof(struct in6_addr
*));
1421 stream_put(bgp_ifindices_buf
, &ifindex
,
1422 sizeof(unsigned int));
1425 && bgp_is_valid_label(&mpinfo
->extra
->label
)) {
1426 has_valid_label
= 1;
1427 label
= label_pton(&mpinfo
->extra
->label
);
1428 stream_put(bgp_label_buf
, &label
,
1429 sizeof(mpls_label_t
));
1434 /* Make Zebra API structure. */
1435 api
.vrf_id
= bgp
->vrf_id
;
1437 api
.type
= ZEBRA_ROUTE_BGP
;
1441 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1443 if (has_valid_label
)
1444 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
1446 /* Note that this currently only applies to Null0 routes for
1448 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv6_route to encode a
1450 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero
1452 * do not want to also encode the :: nexthop for the aggregate
1455 if (CHECK_FLAG(flags
, ZEBRA_FLAG_BLACKHOLE
))
1456 api
.nexthop_num
= 0;
1458 api
.nexthop_num
= valid_nh_count
;
1460 api
.nexthop
= (struct in6_addr
**)STREAM_DATA(bgp_nexthop_buf
);
1461 SET_FLAG(api
.message
, ZAPI_MESSAGE_IFINDEX
);
1462 api
.ifindex_num
= valid_nh_count
;
1463 api
.ifindex
= (ifindex_t
*)STREAM_DATA(bgp_ifindices_buf
);
1464 if (has_valid_label
) {
1465 api
.label_num
= valid_nh_count
;
1466 api
.label
= (unsigned int *)STREAM_DATA(bgp_label_buf
);
1471 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1472 api
.metric
= metric
;
1476 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1480 distance
= bgp_distance_apply(p
, info
, afi
, safi
, bgp
);
1482 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
1483 api
.distance
= distance
;
1486 if (p
->family
== AF_INET
) {
1487 if (bgp_debug_zebra(p
)) {
1491 "Tx IPv4 route %s VRF %u %s/%d metric %u tag %" ROUTE_TAG_PRI
,
1492 valid_nh_count
? "add" : "delete",
1494 inet_ntop(AF_INET
, &p
->u
.prefix4
,
1495 buf
[0], sizeof(buf
[0])),
1496 p
->prefixlen
, api
.metric
, api
.tag
);
1497 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1498 label_buf
[0] = '\0';
1499 if (has_valid_label
)
1500 sprintf(label_buf
, "label %u",
1503 " nhop [%d]: %s if %s %s",
1509 ifindex2ifname(api
.ifindex
[i
],
1516 zapi_ipv4_route_ipv6_nexthop(
1517 ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
,
1518 zclient
, (struct prefix_ipv4
*)p
,
1519 (struct zapi_ipv6
*)&api
);
1521 zapi_ipv4_route(ZEBRA_IPV4_ROUTE_DELETE
,
1523 (struct prefix_ipv4
*)p
,
1524 (struct zapi_ipv4
*)&api
);
1526 if (bgp_debug_zebra(p
)) {
1530 "Tx IPv6 route %s VRF %u %s/%d metric %u tag %" ROUTE_TAG_PRI
,
1531 valid_nh_count
? "add" : "delete",
1533 inet_ntop(AF_INET6
, &p
->u
.prefix6
,
1534 buf
[0], sizeof(buf
[0])),
1535 p
->prefixlen
, api
.metric
, api
.tag
);
1536 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1537 label_buf
[0] = '\0';
1538 if (has_valid_label
)
1539 sprintf(label_buf
, "label %u",
1542 " nhop [%d]: %s if %s %s",
1548 ifindex2ifname(api
.ifindex
[i
],
1555 valid_nh_count
? ZEBRA_IPV6_ROUTE_ADD
1556 : ZEBRA_IPV6_ROUTE_DELETE
,
1557 zclient
, (struct prefix_ipv6
*)p
, NULL
, &api
);
1562 /* Announce all routes of a table to zebra */
1563 void bgp_zebra_announce_table(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1565 struct bgp_node
*rn
;
1566 struct bgp_table
*table
;
1567 struct bgp_info
*ri
;
1569 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1570 * know of this instance.
1572 if (!bgp_install_info_to_zebra(bgp
))
1575 table
= bgp
->rib
[afi
][safi
];
1579 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
1580 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1581 if (CHECK_FLAG(ri
->flags
, BGP_INFO_SELECTED
)
1582 && ri
->type
== ZEBRA_ROUTE_BGP
1583 && ri
->sub_type
== BGP_ROUTE_NORMAL
)
1584 bgp_zebra_announce(rn
, &rn
->p
, ri
, bgp
, afi
,
1588 void bgp_zebra_withdraw(struct prefix
*p
, struct bgp_info
*info
, safi_t safi
)
1596 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1597 * know of this instance.
1599 if (!bgp_install_info_to_zebra(peer
->bgp
))
1602 if ((p
->family
== AF_INET
1603 && !vrf_bitmap_check(zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_BGP
],
1605 || (p
->family
== AF_INET6
1606 && !vrf_bitmap_check(zclient
->redist
[AFI_IP6
][ZEBRA_ROUTE_BGP
],
1607 peer
->bgp
->vrf_id
)))
1612 if (peer
->sort
== BGP_PEER_IBGP
) {
1613 SET_FLAG(flags
, ZEBRA_FLAG_INTERNAL
);
1614 SET_FLAG(flags
, ZEBRA_FLAG_IBGP
);
1617 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1618 || CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1619 || bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1620 SET_FLAG(flags
, ZEBRA_FLAG_INTERNAL
);
1622 if (p
->family
== AF_INET
) {
1623 struct zapi_ipv4 api
;
1625 api
.vrf_id
= peer
->bgp
->vrf_id
;
1628 api
.type
= ZEBRA_ROUTE_BGP
;
1632 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1633 api
.nexthop_num
= 0;
1637 api
.ifindex_num
= 0;
1638 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1639 api
.metric
= info
->attr
->med
;
1642 if (info
->attr
->tag
!= 0) {
1643 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1644 api
.tag
= info
->attr
->tag
;
1647 if (bgp_debug_zebra(p
)) {
1648 char buf
[2][INET_ADDRSTRLEN
];
1650 "Tx IPv4 route delete VRF %u %s/%d metric %u tag %" ROUTE_TAG_PRI
,
1652 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0],
1654 p
->prefixlen
, api
.metric
, api
.tag
);
1657 zapi_ipv4_route(ZEBRA_IPV4_ROUTE_DELETE
, zclient
,
1658 (struct prefix_ipv4
*)p
, &api
);
1660 /* We have to think about a IPv6 link-local address curse. */
1661 if (p
->family
== AF_INET6
) {
1662 struct zapi_ipv6 api
;
1664 api
.vrf_id
= peer
->bgp
->vrf_id
;
1666 api
.type
= ZEBRA_ROUTE_BGP
;
1670 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1671 api
.nexthop_num
= 0;
1673 api
.ifindex_num
= 0;
1675 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
1676 api
.metric
= info
->attr
->med
;
1679 if (info
->attr
->tag
!= 0) {
1680 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1681 api
.tag
= info
->attr
->tag
;
1684 if (bgp_debug_zebra(p
)) {
1685 char buf
[2][INET6_ADDRSTRLEN
];
1687 "Tx IPv6 route delete VRF %u %s/%d metric %u tag %" ROUTE_TAG_PRI
,
1689 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0],
1691 p
->prefixlen
, api
.metric
, api
.tag
);
1694 zapi_ipv6_route(ZEBRA_IPV6_ROUTE_DELETE
, zclient
,
1695 (struct prefix_ipv6
*)p
, NULL
, &api
);
1699 struct bgp_redist
*bgp_redist_lookup(struct bgp
*bgp
, afi_t afi
, u_char type
,
1702 struct list
*red_list
;
1703 struct listnode
*node
;
1704 struct bgp_redist
*red
;
1706 red_list
= bgp
->redist
[afi
][type
];
1710 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1711 if (red
->instance
== instance
)
1717 struct bgp_redist
*bgp_redist_add(struct bgp
*bgp
, afi_t afi
, u_char type
,
1720 struct list
*red_list
;
1721 struct bgp_redist
*red
;
1723 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1727 if (!bgp
->redist
[afi
][type
])
1728 bgp
->redist
[afi
][type
] = list_new();
1730 red_list
= bgp
->redist
[afi
][type
];
1731 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
,
1732 sizeof(struct bgp_redist
));
1733 red
->instance
= instance
;
1735 listnode_add(red_list
, red
);
1740 static void bgp_redist_del(struct bgp
*bgp
, afi_t afi
, u_char type
,
1743 struct bgp_redist
*red
;
1745 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1748 listnode_delete(bgp
->redist
[afi
][type
], red
);
1749 XFREE(MTYPE_BGP_REDIST
, red
);
1750 if (!bgp
->redist
[afi
][type
]->count
) {
1751 list_free(bgp
->redist
[afi
][type
]);
1752 bgp
->redist
[afi
][type
] = NULL
;
1757 /* Other routes redistribution into BGP. */
1758 int bgp_redistribute_set(struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1761 /* Return if already redistribute flag is set. */
1763 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1767 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1769 if (vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1773 if (bgp
->vrf_id
== VRF_DEFAULT
1774 && type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1775 vnc_export_bgp_enable(
1776 bgp
, afi
); /* only enables if mode bits cfg'd */
1780 vrf_bitmap_set(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1783 /* Don't try to register if we're not connected to Zebra or Zebra
1785 * know of this instance.
1787 if (!bgp_install_info_to_zebra(bgp
))
1788 return CMD_WARNING_CONFIG_FAILED
;
1790 if (BGP_DEBUG(zebra
, ZEBRA
))
1791 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1792 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1795 /* Send distribute add message to zebra. */
1796 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1797 instance
, bgp
->vrf_id
);
1802 int bgp_redistribute_resend(struct bgp
*bgp
, afi_t afi
, int type
,
1805 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1806 * know of this instance.
1808 if (!bgp_install_info_to_zebra(bgp
))
1811 if (BGP_DEBUG(zebra
, ZEBRA
))
1812 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1813 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1816 /* Send distribute add message to zebra. */
1817 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1818 instance
, bgp
->vrf_id
);
1819 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1820 instance
, bgp
->vrf_id
);
1825 /* Redistribute with route-map specification. */
1826 int bgp_redistribute_rmap_set(struct bgp_redist
*red
, const char *name
)
1828 if (red
->rmap
.name
&& (strcmp(red
->rmap
.name
, name
) == 0))
1832 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1833 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1834 red
->rmap
.map
= route_map_lookup_by_name(name
);
1839 /* Redistribute with metric specification. */
1840 int bgp_redistribute_metric_set(struct bgp
*bgp
, struct bgp_redist
*red
,
1841 afi_t afi
, int type
, u_int32_t metric
)
1843 struct bgp_node
*rn
;
1844 struct bgp_info
*ri
;
1846 if (red
->redist_metric_flag
&& red
->redist_metric
== metric
)
1849 red
->redist_metric_flag
= 1;
1850 red
->redist_metric
= metric
;
1852 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
1853 rn
= bgp_route_next(rn
)) {
1854 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
1855 if (ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
1857 && ri
->instance
== red
->instance
) {
1858 struct attr
*old_attr
;
1859 struct attr new_attr
;
1861 bgp_attr_dup(&new_attr
, ri
->attr
);
1862 new_attr
.med
= red
->redist_metric
;
1863 old_attr
= ri
->attr
;
1864 ri
->attr
= bgp_attr_intern(&new_attr
);
1865 bgp_attr_unintern(&old_attr
);
1867 bgp_info_set_flag(rn
, ri
,
1868 BGP_INFO_ATTR_CHANGED
);
1869 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1877 /* Unset redistribution. */
1878 int bgp_redistribute_unreg(struct bgp
*bgp
, afi_t afi
, int type
,
1881 struct bgp_redist
*red
;
1883 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1887 /* Return if zebra connection is disabled. */
1889 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
],
1892 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1894 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1896 vrf_bitmap_unset(zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1900 if (bgp
->vrf_id
== VRF_DEFAULT
&& type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1901 vnc_export_bgp_disable(bgp
, afi
);
1905 if (bgp_install_info_to_zebra(bgp
)) {
1906 /* Send distribute delete message to zebra. */
1907 if (BGP_DEBUG(zebra
, ZEBRA
))
1908 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1909 bgp
->vrf_id
, afi
, zebra_route_string(type
),
1911 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
1912 type
, instance
, bgp
->vrf_id
);
1915 /* Withdraw redistributed routes from current BGP's routing table. */
1916 bgp_redistribute_withdraw(bgp
, afi
, type
, instance
);
1921 /* Unset redistribution. */
1922 int bgp_redistribute_unset(struct bgp
*bgp
, afi_t afi
, int type
,
1925 struct bgp_redist
*red
;
1927 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1931 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1933 /* Unset route-map. */
1935 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1936 red
->rmap
.name
= NULL
;
1937 red
->rmap
.map
= NULL
;
1940 red
->redist_metric_flag
= 0;
1941 red
->redist_metric
= 0;
1943 bgp_redist_del(bgp
, afi
, type
, instance
);
1948 /* Update redistribute vrf bitmap during triggers like
1949 restart networking or delete/add VRFs */
1950 void bgp_update_redist_vrf_bitmaps(struct bgp
*bgp
, vrf_id_t old_vrf_id
)
1955 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1956 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1957 if (vrf_bitmap_check(zclient
->redist
[afi
][i
],
1959 vrf_bitmap_unset(zclient
->redist
[afi
][i
],
1961 vrf_bitmap_set(zclient
->redist
[afi
][i
],
1967 void bgp_zclient_reset(void)
1969 zclient_reset(zclient
);
1972 /* Register this instance with Zebra. Invoked upon connect (for
1973 * default instance) and when other VRFs are learnt (or created and
1976 void bgp_zebra_instance_register(struct bgp
*bgp
)
1978 /* Don't try to register if we're not connected to Zebra */
1979 if (!zclient
|| zclient
->sock
< 0)
1982 if (BGP_DEBUG(zebra
, ZEBRA
))
1983 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
1985 /* Register for router-id, interfaces, redistributed routes. */
1986 zclient_send_reg_requests(zclient
, bgp
->vrf_id
);
1988 /* For default instance, register to learn about VNIs, if appropriate.
1990 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
1991 && bgp
->advertise_all_vni
)
1992 bgp_zebra_advertise_all_vni(bgp
, 1);
1995 /* Deregister this instance with Zebra. Invoked upon the instance
1996 * being deleted (default or VRF) and it is already registered.
1998 void bgp_zebra_instance_deregister(struct bgp
*bgp
)
2000 /* Don't try to deregister if we're not connected to Zebra */
2001 if (zclient
->sock
< 0)
2004 if (BGP_DEBUG(zebra
, ZEBRA
))
2005 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
2007 /* For default instance, unregister learning about VNIs, if appropriate.
2009 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
2010 && bgp
->advertise_all_vni
)
2011 bgp_zebra_advertise_all_vni(bgp
, 0);
2013 /* Deregister for router-id, interfaces, redistributed routes. */
2014 zclient_send_dereg_requests(zclient
, bgp
->vrf_id
);
2017 void bgp_zebra_initiate_radv(struct bgp
*bgp
, struct peer
*peer
)
2019 int ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
2021 /* Don't try to initiate if we're not connected to Zebra */
2022 if (zclient
->sock
< 0)
2025 if (BGP_DEBUG(zebra
, ZEBRA
))
2026 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
,
2029 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 1,
2033 void bgp_zebra_terminate_radv(struct bgp
*bgp
, struct peer
*peer
)
2035 /* Don't try to terminate if we're not connected to Zebra */
2036 if (zclient
->sock
< 0)
2039 if (BGP_DEBUG(zebra
, ZEBRA
))
2040 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
,
2043 zclient_send_interface_radv_req(zclient
, bgp
->vrf_id
, peer
->ifp
, 0, 0);
2046 int bgp_zebra_advertise_all_vni(struct bgp
*bgp
, int advertise
)
2051 if (!zclient
|| zclient
->sock
< 0)
2054 /* Don't try to register if Zebra doesn't know of this instance. */
2055 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
2061 zclient_create_header(s
, ZEBRA_ADVERTISE_ALL_VNI
, bgp
->vrf_id
);
2062 stream_putc(s
, advertise
);
2063 stream_putw_at(s
, 0, stream_get_endp(s
));
2065 return zclient_send_message(zclient
);
2068 /* BGP has established connection with Zebra. */
2069 static void bgp_zebra_connected(struct zclient
*zclient
)
2073 zclient_num_connects
++; /* increment even if not responding */
2075 /* At this point, we may or may not have BGP instances configured, but
2076 * we're only interested in the default VRF (others wouldn't have learnt
2077 * the VRF from Zebra yet.)
2079 bgp
= bgp_get_default();
2083 bgp_zebra_instance_register(bgp
);
2085 /* Send the client registration */
2086 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
2088 /* TODO - What if we have peers and networks configured, do we have to
2093 static int bgp_zebra_process_local_vni(int command
, struct zclient
*zclient
,
2094 zebra_size_t length
, vrf_id_t vrf_id
)
2099 struct in_addr vtep_ip
;
2102 vni
= stream_getl(s
);
2103 if (command
== ZEBRA_VNI_ADD
)
2104 vtep_ip
.s_addr
= stream_get_ipv4(s
);
2105 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2109 if (BGP_DEBUG(zebra
, ZEBRA
))
2110 zlog_debug("Rx VNI %s VRF %u VNI %u",
2111 (command
== ZEBRA_VNI_ADD
) ? "add" : "del", vrf_id
,
2114 if (command
== ZEBRA_VNI_ADD
)
2115 return bgp_evpn_local_vni_add(
2116 bgp
, vni
, vtep_ip
.s_addr
? vtep_ip
: bgp
->router_id
);
2118 return bgp_evpn_local_vni_del(bgp
, vni
);
2121 static int bgp_zebra_process_local_macip(int command
, struct zclient
*zclient
,
2122 zebra_size_t length
, vrf_id_t vrf_id
)
2130 char buf
[ETHER_ADDR_STRLEN
];
2131 char buf1
[INET6_ADDRSTRLEN
];
2134 memset(&ip
, 0, sizeof(ip
));
2136 vni
= stream_getl(s
);
2137 stream_get(&mac
.octet
, s
, ETHER_ADDR_LEN
);
2138 ipa_len
= stream_getl(s
);
2139 if (ipa_len
!= 0 && ipa_len
!= IPV4_MAX_BYTELEN
2140 && ipa_len
!= IPV6_MAX_BYTELEN
) {
2141 zlog_err("%u:Recv MACIP %s with invalid IP addr length %d",
2142 vrf_id
, (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
2149 (ipa_len
== IPV4_MAX_BYTELEN
) ? IPADDR_V4
: IPADDR_V6
;
2150 stream_get(&ip
.ip
.addr
, s
, ipa_len
);
2152 sticky
= stream_getc(s
);
2154 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
2158 if (BGP_DEBUG(zebra
, ZEBRA
))
2159 zlog_debug("%u:Recv MACIP %s %sMAC %s IP %s VNI %u", vrf_id
,
2160 (command
== ZEBRA_MACIP_ADD
) ? "Add" : "Del",
2161 sticky
? "sticky " : "",
2162 prefix_mac2str(&mac
, buf
, sizeof(buf
)),
2163 ipaddr2str(&ip
, buf1
, sizeof(buf1
)), vni
);
2165 if (command
== ZEBRA_MACIP_ADD
)
2166 return bgp_evpn_local_macip_add(bgp
, vni
, &mac
, &ip
, sticky
);
2168 return bgp_evpn_local_macip_del(bgp
, vni
, &mac
, &ip
);
2171 void bgp_zebra_init(struct thread_master
*master
)
2173 zclient_num_connects
= 0;
2175 /* Set default values. */
2176 zclient
= zclient_new(master
);
2177 zclient_init(zclient
, ZEBRA_ROUTE_BGP
, 0);
2178 zclient
->zebra_connected
= bgp_zebra_connected
;
2179 zclient
->router_id_update
= bgp_router_id_update
;
2180 zclient
->interface_add
= bgp_interface_add
;
2181 zclient
->interface_delete
= bgp_interface_delete
;
2182 zclient
->interface_address_add
= bgp_interface_address_add
;
2183 zclient
->interface_address_delete
= bgp_interface_address_delete
;
2184 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
2185 zclient
->interface_nbr_address_delete
=
2186 bgp_interface_nbr_address_delete
;
2187 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
2188 zclient
->redistribute_route_ipv4_add
= zebra_read_ipv4
;
2189 zclient
->redistribute_route_ipv4_del
= zebra_read_ipv4
;
2190 zclient
->interface_up
= bgp_interface_up
;
2191 zclient
->interface_down
= bgp_interface_down
;
2192 zclient
->redistribute_route_ipv6_add
= zebra_read_ipv6
;
2193 zclient
->redistribute_route_ipv6_del
= zebra_read_ipv6
;
2194 zclient
->nexthop_update
= bgp_read_nexthop_update
;
2195 zclient
->import_check_update
= bgp_read_import_check_update
;
2196 zclient
->fec_update
= bgp_read_fec_update
;
2197 zclient
->local_vni_add
= bgp_zebra_process_local_vni
;
2198 zclient
->local_vni_del
= bgp_zebra_process_local_vni
;
2199 zclient
->local_macip_add
= bgp_zebra_process_local_macip
;
2200 zclient
->local_macip_del
= bgp_zebra_process_local_macip
;
2202 bgp_nexthop_buf
= stream_new(multipath_num
* sizeof(struct in6_addr
));
2203 bgp_ifindices_buf
= stream_new(multipath_num
* sizeof(unsigned int));
2204 bgp_label_buf
= stream_new(multipath_num
* sizeof(unsigned int));
2207 void bgp_zebra_destroy(void)
2210 if (bgp_nexthop_buf
)
2211 stream_free(bgp_nexthop_buf
);
2212 if (bgp_ifindices_buf
)
2213 stream_free(bgp_ifindices_buf
);
2215 stream_free(bgp_label_buf
);
2217 if (zclient
== NULL
)
2219 zclient_stop(zclient
);
2220 zclient_free(zclient
);
2224 int bgp_zebra_num_connects(void)
2226 return zclient_num_connects
;