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
17 along with GNU Zebra; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
28 #include "sockunion.h"
38 #include "bgpd/bgpd.h"
39 #include "bgpd/bgp_route.h"
40 #include "bgpd/bgp_attr.h"
41 #include "bgpd/bgp_nexthop.h"
42 #include "bgpd/bgp_zebra.h"
43 #include "bgpd/bgp_fsm.h"
44 #include "bgpd/bgp_debug.h"
45 #include "bgpd/bgp_mpath.h"
46 #include "bgpd/bgp_nexthop.h"
47 #include "bgpd/bgp_nht.h"
48 #include "bgpd/bgp_bfd.h"
50 # include "bgpd/rfapi/rfapi_backend.h"
51 # include "bgpd/rfapi/vnc_export_bgp.h"
54 /* All information about zebra. */
55 struct zclient
*zclient
= NULL
;
57 /* Growable buffer for nexthops sent to zebra */
58 struct stream
*bgp_nexthop_buf
= NULL
;
59 struct stream
*bgp_ifindices_buf
= NULL
;
61 /* These array buffers are used in making a copy of the attributes for
62 route-map apply. Arrays are being used here to minimize mallocs and
63 frees for the temporary copy of the attributes.
64 Given the zapi api expects the nexthop buffer to contain pointer to
65 pointers for nexthops, we couldnt have used a single nexthop variable
66 on the stack, hence we had two options:
67 1. maintain a linked-list and free it after zapi_*_route call
68 2. use an array to avoid number of mallocs.
69 Number of supported next-hops are finite, use of arrays should be ok. */
70 struct attr attr_cp
[MULTIPATH_NUM
];
71 struct attr_extra attr_extra_cp
[MULTIPATH_NUM
];
74 /* Once per address-family initialization of the attribute array */
75 #define BGP_INFO_ATTR_BUF_INIT()\
77 memset(attr_cp, 0, MULTIPATH_NUM * sizeof(struct attr));\
78 memset(attr_extra_cp, 0, MULTIPATH_NUM * sizeof(struct attr_extra));\
82 #define BGP_INFO_ATTR_BUF_COPY(info_src, info_dst)\
84 *info_dst = *info_src; \
85 assert(attr_index != MULTIPATH_NUM);\
86 attr_cp[attr_index].extra = &attr_extra_cp[attr_index]; \
87 bgp_attr_dup (&attr_cp[attr_index], info_src->attr); \
88 bgp_attr_deep_dup (&attr_cp[attr_index], info_src->attr); \
89 info_dst->attr = &attr_cp[attr_index]; \
93 #define BGP_INFO_ATTR_BUF_FREE(info) \
95 bgp_attr_deep_free(info->attr); \
99 /* Can we install into zebra? */
101 bgp_install_info_to_zebra (struct bgp
*bgp
)
103 if (zclient
->sock
<= 0)
106 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
112 int zclient_num_connects
;
114 /* Router-id update message from zebra. */
116 bgp_router_id_update (int command
, struct zclient
*zclient
, zebra_size_t length
,
119 struct prefix router_id
;
121 zebra_router_id_update_read(zclient
->ibuf
,&router_id
);
123 if (BGP_DEBUG (zebra
, ZEBRA
))
125 char buf
[PREFIX2STR_BUFFER
];
126 prefix2str(&router_id
, buf
, sizeof(buf
));
127 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id
, buf
);
130 bgp_router_id_zebra_bump (vrf_id
, &router_id
);
134 /* Nexthop update message from zebra. */
136 bgp_read_nexthop_update (int command
, struct zclient
*zclient
,
137 zebra_size_t length
, vrf_id_t vrf_id
)
139 bgp_parse_nexthop_update(command
, vrf_id
);
144 bgp_read_import_check_update(int command
, struct zclient
*zclient
,
145 zebra_size_t length
, vrf_id_t vrf_id
)
147 bgp_parse_nexthop_update(command
, vrf_id
);
151 /* Set or clear interface on which unnumbered neighbor is configured. This
152 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
156 bgp_update_interface_nbrs (struct bgp
*bgp
, struct interface
*ifp
,
157 struct interface
*upd_ifp
)
159 struct listnode
*node
, *nnode
;
162 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
165 (strcmp (peer
->conf_if
, ifp
->name
) == 0))
170 bgp_zebra_initiate_radv (bgp
, peer
);
174 bgp_zebra_terminate_radv (bgp
, peer
);
182 bgp_start_interface_nbrs (struct bgp
*bgp
, struct interface
*ifp
)
184 struct listnode
*node
, *nnode
;
187 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
190 (strcmp (peer
->conf_if
, ifp
->name
) == 0) &&
191 peer
->status
!= Established
)
193 if (peer_active(peer
))
194 BGP_EVENT_ADD (peer
, BGP_Stop
);
195 BGP_EVENT_ADD (peer
, BGP_Start
);
201 bgp_nbr_connected_add (struct bgp
*bgp
, struct nbr_connected
*ifc
)
203 struct listnode
*node
;
204 struct connected
*connected
;
205 struct interface
*ifp
;
208 /* Kick-off the FSM for any relevant peers only if there is a
209 * valid local address on the interface.
212 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, connected
))
214 p
= connected
->address
;
215 if (p
->family
== AF_INET6
&&
216 IN6_IS_ADDR_LINKLOCAL (&p
->u
.prefix6
))
222 bgp_start_interface_nbrs (bgp
, ifp
);
226 bgp_nbr_connected_delete (struct bgp
*bgp
, struct nbr_connected
*ifc
, int del
)
228 struct listnode
*node
, *nnode
;
230 struct interface
*ifp
;
232 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
234 if (peer
->conf_if
&& (strcmp (peer
->conf_if
, ifc
->ifp
->name
) == 0))
236 peer
->last_reset
= PEER_DOWN_NBR_ADDR_DEL
;
237 BGP_EVENT_ADD (peer
, BGP_Stop
);
240 /* Free neighbor also, if we're asked to. */
244 listnode_delete (ifp
->nbr_connected
, ifc
);
245 nbr_connected_free (ifc
);
249 /* Inteface addition message from zebra. */
251 bgp_interface_add (int command
, struct zclient
*zclient
, zebra_size_t length
,
254 struct interface
*ifp
;
257 ifp
= zebra_interface_add_read (zclient
->ibuf
, vrf_id
);
258 if (!ifp
) // unexpected
261 if (BGP_DEBUG (zebra
, ZEBRA
) && ifp
)
262 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id
, ifp
->name
);
264 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
268 bgp_update_interface_nbrs (bgp
, ifp
, ifp
);
273 bgp_interface_delete (int command
, struct zclient
*zclient
,
274 zebra_size_t length
, vrf_id_t vrf_id
)
277 struct interface
*ifp
;
281 ifp
= zebra_interface_state_read (s
, vrf_id
);
282 if (!ifp
) /* This may happen if we've just unregistered for a VRF. */
285 ifp
->ifindex
= IFINDEX_DELETED
;
287 if (BGP_DEBUG (zebra
, ZEBRA
))
288 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id
, ifp
->name
);
290 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
294 bgp_update_interface_nbrs (bgp
, ifp
, NULL
);
299 bgp_interface_up (int command
, struct zclient
*zclient
, zebra_size_t length
,
303 struct interface
*ifp
;
305 struct nbr_connected
*nc
;
306 struct listnode
*node
, *nnode
;
310 ifp
= zebra_interface_state_read (s
, vrf_id
);
315 if (BGP_DEBUG (zebra
, ZEBRA
))
316 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id
, ifp
->name
);
318 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
322 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
323 bgp_connected_add (bgp
, c
);
325 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
326 bgp_nbr_connected_add (bgp
, nc
);
332 bgp_interface_down (int command
, struct zclient
*zclient
, zebra_size_t length
,
336 struct interface
*ifp
;
338 struct nbr_connected
*nc
;
339 struct listnode
*node
, *nnode
;
343 ifp
= zebra_interface_state_read (s
, vrf_id
);
347 if (BGP_DEBUG (zebra
, ZEBRA
))
348 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id
, ifp
->name
);
350 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
354 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
355 bgp_connected_delete (bgp
, c
);
357 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
358 bgp_nbr_connected_delete (bgp
, nc
, 1);
360 /* Fast external-failover */
364 if (CHECK_FLAG (bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
367 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
369 #if defined(HAVE_CUMULUS)
370 /* Take down directly connected EBGP peers as well as 1-hop BFD
371 * tracked (directly connected) IBGP peers.
373 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1) &&
374 (!peer
->bfd_info
|| bgp_bfd_is_peer_multihop(peer
)))
376 /* Take down directly connected EBGP peers */
377 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
381 if (ifp
== peer
->nexthop
.ifp
)
383 BGP_EVENT_ADD (peer
, BGP_Stop
);
384 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
393 bgp_interface_address_add (int command
, struct zclient
*zclient
,
394 zebra_size_t length
, vrf_id_t vrf_id
)
396 struct connected
*ifc
;
398 ifc
= zebra_interface_address_read (command
, zclient
->ibuf
, vrf_id
);
403 if (bgp_debug_zebra(ifc
->address
))
405 char buf
[PREFIX2STR_BUFFER
];
406 prefix2str(ifc
->address
, buf
, sizeof(buf
));
407 zlog_debug("Rx Intf address add VRF %u IF %s addr %s",
408 vrf_id
, ifc
->ifp
->name
, buf
);
411 if (if_is_operative (ifc
->ifp
))
415 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
419 bgp_connected_add (bgp
, ifc
);
420 /* If we have learnt of any neighbors on this interface,
421 * check to kick off any BGP interface-based neighbors,
422 * but only if this is a link-local address.
424 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
) &&
425 !list_isempty(ifc
->ifp
->nbr_connected
))
426 bgp_start_interface_nbrs (bgp
, ifc
->ifp
);
433 bgp_interface_address_delete (int command
, struct zclient
*zclient
,
434 zebra_size_t length
, vrf_id_t vrf_id
)
436 struct connected
*ifc
;
439 ifc
= zebra_interface_address_read (command
, zclient
->ibuf
, vrf_id
);
444 if (bgp_debug_zebra(ifc
->address
))
446 char buf
[PREFIX2STR_BUFFER
];
447 prefix2str(ifc
->address
, buf
, sizeof(buf
));
448 zlog_debug("Rx Intf address del VRF %u IF %s addr %s",
449 vrf_id
, ifc
->ifp
->name
, buf
);
452 if (if_is_operative (ifc
->ifp
))
454 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
456 bgp_connected_delete (bgp
, ifc
);
459 connected_free (ifc
);
465 bgp_interface_nbr_address_add (int command
, struct zclient
*zclient
,
466 zebra_size_t length
, vrf_id_t vrf_id
)
468 struct nbr_connected
*ifc
= NULL
;
471 ifc
= zebra_interface_nbr_address_read (command
, zclient
->ibuf
, vrf_id
);
476 if (bgp_debug_zebra(ifc
->address
))
478 char buf
[PREFIX2STR_BUFFER
];
479 prefix2str(ifc
->address
, buf
, sizeof(buf
));
480 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s",
481 vrf_id
, ifc
->ifp
->name
, buf
);
484 if (if_is_operative (ifc
->ifp
))
486 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
488 bgp_nbr_connected_add (bgp
, ifc
);
495 bgp_interface_nbr_address_delete (int command
, struct zclient
*zclient
,
496 zebra_size_t length
, vrf_id_t vrf_id
)
498 struct nbr_connected
*ifc
= NULL
;
501 ifc
= zebra_interface_nbr_address_read (command
, zclient
->ibuf
, vrf_id
);
506 if (bgp_debug_zebra(ifc
->address
))
508 char buf
[PREFIX2STR_BUFFER
];
509 prefix2str(ifc
->address
, buf
, sizeof(buf
));
510 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s",
511 vrf_id
, ifc
->ifp
->name
, buf
);
514 if (if_is_operative (ifc
->ifp
))
516 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
518 bgp_nbr_connected_delete (bgp
, ifc
, 0);
521 nbr_connected_free (ifc
);
526 /* VRF update for an interface. */
528 bgp_interface_vrf_update (int command
, struct zclient
*zclient
, zebra_size_t length
,
531 struct interface
*ifp
;
534 struct nbr_connected
*nc
;
535 struct listnode
*node
, *nnode
;
538 ifp
= zebra_interface_vrf_update_read (zclient
->ibuf
, vrf_id
, &new_vrf_id
);
542 if (BGP_DEBUG (zebra
, ZEBRA
) && ifp
)
543 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u",
544 vrf_id
, ifp
->name
, new_vrf_id
);
546 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
550 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
551 bgp_connected_delete (bgp
, c
);
553 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
554 bgp_nbr_connected_delete (bgp
, nc
, 1);
556 /* Fast external-failover */
560 if (CHECK_FLAG (bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
563 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
565 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
568 if (ifp
== peer
->nexthop
.ifp
)
569 BGP_EVENT_ADD (peer
, BGP_Stop
);
573 if_update_vrf (ifp
, ifp
->name
, strlen (ifp
->name
), new_vrf_id
);
575 bgp
= bgp_lookup_by_vrf_id (new_vrf_id
);
579 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
580 bgp_connected_add (bgp
, c
);
582 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
583 bgp_nbr_connected_add (bgp
, nc
);
587 /* Zebra route add and delete treatment. */
589 zebra_read_ipv4 (int command
, struct zclient
*zclient
, zebra_size_t length
,
593 struct zapi_ipv4 api
;
594 struct in_addr nexthop
;
595 struct prefix_ipv4 p
;
596 unsigned int ifindex
;
600 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
607 /* Type, flags, message. */
608 api
.type
= stream_getc (s
);
609 api
.instance
= stream_getw (s
);
610 api
.flags
= stream_getl (s
);
611 api
.message
= stream_getc (s
);
614 memset (&p
, 0, sizeof (struct prefix_ipv4
));
616 p
.prefixlen
= MIN(IPV4_MAX_PREFIXLEN
, stream_getc (s
));
617 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
619 /* Nexthop, ifindex, distance, metric. */
620 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
622 api
.nexthop_num
= stream_getc (s
);
623 nexthop
.s_addr
= stream_get_ipv4 (s
);
626 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
628 api
.ifindex_num
= stream_getc (s
);
629 ifindex
= stream_getl (s
); /* ifindex, unused */
636 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
637 api
.distance
= stream_getc (s
);
639 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
640 api
.metric
= stream_getl (s
);
644 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
645 api
.tag
= stream_getl (s
);
649 if (command
== ZEBRA_REDISTRIBUTE_IPV4_ADD
)
651 if (bgp_debug_zebra((struct prefix
*)&p
))
653 char buf
[2][INET_ADDRSTRLEN
];
654 zlog_debug("Rx IPv4 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %"ROUTE_TAG_PRI
,
656 zebra_route_string(api
.type
), api
.instance
,
657 inet_ntop(AF_INET
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
659 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
665 * The ADD message is actually an UPDATE and there is no explicit DEL
666 * for a prior redistributed route, if any. So, perform an implicit
667 * DEL processing for the same redistributed route from any other
670 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
673 bgp_redistribute_delete(bgp
, (struct prefix
*)&p
, i
, api
.instance
);
676 /* Now perform the add/update. */
677 bgp_redistribute_add(bgp
, (struct prefix
*)&p
, &nexthop
, NULL
, ifindex
,
678 api
.metric
, api
.type
, api
.instance
, api
.tag
);
680 else if (command
== ZEBRA_REDISTRIBUTE_IPV4_DEL
)
682 if (bgp_debug_zebra((struct prefix
*)&p
))
684 char buf
[2][INET_ADDRSTRLEN
];
685 zlog_debug("Rx IPv4 route delete VRF %u %s[%d] %s/%d "
686 "nexthop %s metric %u tag %"ROUTE_TAG_PRI
,
688 zebra_route_string(api
.type
), api
.instance
,
689 inet_ntop(AF_INET
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
691 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
695 bgp_redistribute_delete(bgp
, (struct prefix
*)&p
, api
.type
, api
.instance
);
702 /* Zebra route add and delete treatment. */
704 zebra_read_ipv6 (int command
, struct zclient
*zclient
, zebra_size_t length
,
708 struct zapi_ipv6 api
;
709 struct in6_addr nexthop
;
710 struct prefix_ipv6 p
;
711 unsigned int ifindex
;
715 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
720 memset (&nexthop
, 0, sizeof (struct in6_addr
));
722 /* Type, flags, message. */
723 api
.type
= stream_getc (s
);
724 api
.instance
= stream_getw (s
);
725 api
.flags
= stream_getl (s
);
726 api
.message
= stream_getc (s
);
729 memset (&p
, 0, sizeof (struct prefix_ipv6
));
731 p
.prefixlen
= MIN(IPV6_MAX_PREFIXLEN
, stream_getc (s
));
732 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
734 /* Nexthop, ifindex, distance, metric. */
735 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
737 api
.nexthop_num
= stream_getc (s
);
738 stream_get (&nexthop
, s
, 16);
741 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
743 api
.ifindex_num
= stream_getc (s
);
744 ifindex
= stream_getl (s
); /* ifindex, unused */
751 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
752 api
.distance
= stream_getc (s
);
756 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
757 api
.metric
= stream_getl (s
);
761 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
762 api
.tag
= stream_getl (s
);
766 /* Simply ignore link-local address. */
767 if (IN6_IS_ADDR_LINKLOCAL (&p
.prefix
))
770 if (command
== ZEBRA_REDISTRIBUTE_IPV6_ADD
)
772 if (bgp_debug_zebra((struct prefix
*)&p
))
774 char buf
[2][INET6_ADDRSTRLEN
];
775 zlog_debug("Rx IPv6 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %"ROUTE_TAG_PRI
,
777 zebra_route_string(api
.type
), api
.instance
,
778 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
780 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
786 * The ADD message is actually an UPDATE and there is no explicit DEL
787 * for a prior redistributed route, if any. So, perform an implicit
788 * DEL processing for the same redistributed route from any other
791 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
794 bgp_redistribute_delete(bgp
, (struct prefix
*)&p
, i
, api
.instance
);
797 bgp_redistribute_add (bgp
, (struct prefix
*)&p
, NULL
, &nexthop
, ifindex
,
798 api
.metric
, api
.type
, api
.instance
, api
.tag
);
800 else if (command
== ZEBRA_REDISTRIBUTE_IPV6_DEL
)
802 if (bgp_debug_zebra((struct prefix
*)&p
))
804 char buf
[2][INET6_ADDRSTRLEN
];
805 zlog_debug("Rx IPv6 route delete VRF %u %s[%d] %s/%d "
806 "nexthop %s metric %u tag %"ROUTE_TAG_PRI
,
808 zebra_route_string(api
.type
), api
.instance
,
809 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
811 inet_ntop(AF_INET6
, &nexthop
, buf
[1], sizeof(buf
[1])),
815 bgp_redistribute_delete (bgp
, (struct prefix
*) &p
, api
.type
, api
.instance
);
820 #endif /* HAVE_IPV6 */
823 if_lookup_by_ipv4 (struct in_addr
*addr
, vrf_id_t vrf_id
)
825 struct listnode
*ifnode
;
826 struct listnode
*cnode
;
827 struct interface
*ifp
;
828 struct connected
*connected
;
829 struct prefix_ipv4 p
;
834 p
.prefixlen
= IPV4_MAX_BITLEN
;
836 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
838 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
840 cp
= connected
->address
;
842 if (cp
->family
== AF_INET
)
843 if (prefix_match (cp
, (struct prefix
*)&p
))
851 if_lookup_by_ipv4_exact (struct in_addr
*addr
, vrf_id_t vrf_id
)
853 struct listnode
*ifnode
;
854 struct listnode
*cnode
;
855 struct interface
*ifp
;
856 struct connected
*connected
;
859 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
861 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
863 cp
= connected
->address
;
865 if (cp
->family
== AF_INET
)
866 if (IPV4_ADDR_SAME (&cp
->u
.prefix4
, addr
))
875 if_lookup_by_ipv6 (struct in6_addr
*addr
, ifindex_t ifindex
, vrf_id_t vrf_id
)
877 struct listnode
*ifnode
;
878 struct listnode
*cnode
;
879 struct interface
*ifp
;
880 struct connected
*connected
;
881 struct prefix_ipv6 p
;
886 p
.prefixlen
= IPV6_MAX_BITLEN
;
888 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
890 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
892 cp
= connected
->address
;
894 if (cp
->family
== AF_INET6
)
895 if (prefix_match (cp
, (struct prefix
*)&p
))
897 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
))
899 if (ifindex
== ifp
->ifindex
)
911 if_lookup_by_ipv6_exact (struct in6_addr
*addr
, ifindex_t ifindex
, vrf_id_t vrf_id
)
913 struct listnode
*ifnode
;
914 struct listnode
*cnode
;
915 struct interface
*ifp
;
916 struct connected
*connected
;
919 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
921 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
923 cp
= connected
->address
;
925 if (cp
->family
== AF_INET6
)
926 if (IPV6_ADDR_SAME (&cp
->u
.prefix6
, addr
))
928 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
))
930 if (ifindex
== ifp
->ifindex
)
942 if_get_ipv6_global (struct interface
*ifp
, struct in6_addr
*addr
)
944 struct listnode
*cnode
;
945 struct connected
*connected
;
948 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
950 cp
= connected
->address
;
952 if (cp
->family
== AF_INET6
)
953 if (! IN6_IS_ADDR_LINKLOCAL (&cp
->u
.prefix6
))
955 memcpy (addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
963 if_get_ipv6_local (struct interface
*ifp
, struct in6_addr
*addr
)
965 struct listnode
*cnode
;
966 struct connected
*connected
;
969 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
971 cp
= connected
->address
;
973 if (cp
->family
== AF_INET6
)
974 if (IN6_IS_ADDR_LINKLOCAL (&cp
->u
.prefix6
))
976 memcpy (addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
982 #endif /* HAVE_IPV6 */
985 if_get_ipv4_address (struct interface
*ifp
, struct in_addr
*addr
)
987 struct listnode
*cnode
;
988 struct connected
*connected
;
991 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
993 cp
= connected
->address
;
994 if ((cp
->family
== AF_INET
) && !ipv4_martian(&(cp
->u
.prefix4
)))
996 *addr
= cp
->u
.prefix4
;
1004 bgp_nexthop_set (union sockunion
*local
, union sockunion
*remote
,
1005 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
1008 struct interface
*ifp
= NULL
;
1010 memset (nexthop
, 0, sizeof (struct bgp_nexthop
));
1017 if (local
->sa
.sa_family
== AF_INET
)
1019 nexthop
->v4
= local
->sin
.sin_addr
;
1020 if (peer
->update_if
)
1021 ifp
= if_lookup_by_name_vrf (peer
->update_if
, peer
->bgp
->vrf_id
);
1023 ifp
= if_lookup_by_ipv4_exact (&local
->sin
.sin_addr
, peer
->bgp
->vrf_id
);
1026 if (local
->sa
.sa_family
== AF_INET6
)
1028 if (IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
))
1030 if (peer
->conf_if
|| peer
->ifname
)
1031 ifp
= if_lookup_by_name_vrf (peer
->conf_if
? peer
->conf_if
: peer
->ifname
, peer
->bgp
->vrf_id
);
1033 else if (peer
->update_if
)
1034 ifp
= if_lookup_by_name_vrf (peer
->update_if
, peer
->bgp
->vrf_id
);
1036 ifp
= if_lookup_by_ipv6_exact (&local
->sin6
.sin6_addr
,
1037 local
->sin6
.sin6_scope_id
,
1040 #endif /* HAVE_IPV6 */
1047 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
1048 if (local
->sa
.sa_family
== AF_INET
)
1052 ret
= if_get_ipv6_global (ifp
, &nexthop
->v6_global
);
1056 /* There is no global nexthop. Use link-local address as both the
1057 * global and link-local nexthop. In this scenario, the expectation
1058 * for interop is that the network admin would use a route-map to
1059 * specify the global IPv6 nexthop.
1061 if_get_ipv6_local (ifp
, &nexthop
->v6_global
);
1062 memcpy (&nexthop
->v6_local
, &nexthop
->v6_global
,
1066 if_get_ipv6_local (ifp
, &nexthop
->v6_local
);
1068 if (if_lookup_by_ipv4 (&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
1069 peer
->shared_network
= 1;
1071 peer
->shared_network
= 0;
1072 #endif /* HAVE_IPV6 */
1076 /* IPv6 connection, fetch and store IPv4 local address if any. */
1077 if (local
->sa
.sa_family
== AF_INET6
)
1079 struct interface
*direct
= NULL
;
1082 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
1083 if (!ret
&& peer
->local_id
.s_addr
)
1084 nexthop
->v4
= peer
->local_id
;
1087 if (! IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
))
1089 memcpy (&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
1092 /* If directory connected set link-local address. */
1093 direct
= if_lookup_by_ipv6 (&remote
->sin6
.sin6_addr
,
1094 remote
->sin6
.sin6_scope_id
, peer
->bgp
->vrf_id
);
1096 if_get_ipv6_local (ifp
, &nexthop
->v6_local
);
1099 /* Link-local address. */
1101 ret
= if_get_ipv6_global (ifp
, &nexthop
->v6_global
);
1103 /* If there is no global address. Set link-local address as
1104 global. I know this break RFC specification... */
1105 /* In this scenario, the expectation for interop is that the
1106 * network admin would use a route-map to specify the global
1110 memcpy (&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
1112 /* Always set the link-local address */
1113 memcpy (&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
1117 if (IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
) ||
1118 if_lookup_by_ipv6 (&remote
->sin6
.sin6_addr
, remote
->sin6
.sin6_scope_id
,
1120 peer
->shared_network
= 1;
1122 peer
->shared_network
= 0;
1125 /* KAME stack specific treatment. */
1127 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->v6_global
)
1128 && IN6_LINKLOCAL_IFINDEX (nexthop
->v6_global
))
1130 SET_IN6_LINKLOCAL_IFINDEX (nexthop
->v6_global
, 0);
1132 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->v6_local
)
1133 && IN6_LINKLOCAL_IFINDEX (nexthop
->v6_local
))
1135 SET_IN6_LINKLOCAL_IFINDEX (nexthop
->v6_local
, 0);
1138 #endif /* HAVE_IPV6 */
1140 /* If we have identified the local interface, there is no error for now. */
1144 static struct in6_addr
*
1145 bgp_info_to_ipv6_nexthop (struct bgp_info
*info
)
1147 struct in6_addr
*nexthop
= NULL
;
1149 /* Only global address nexthop exists. */
1150 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
)
1151 nexthop
= &info
->attr
->extra
->mp_nexthop_global
;
1153 /* If both global and link-local address present. */
1154 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1156 /* Check if route-map is set to prefer global over link-local */
1157 if (info
->attr
->extra
->mp_nexthop_prefer_global
)
1158 nexthop
= &info
->attr
->extra
->mp_nexthop_global
;
1161 /* Workaround for Cisco's nexthop bug. */
1162 if (IN6_IS_ADDR_UNSPECIFIED (&info
->attr
->extra
->mp_nexthop_global
)
1163 && info
->peer
->su_remote
->sa
.sa_family
== AF_INET6
)
1164 nexthop
= &info
->peer
->su_remote
->sin6
.sin6_addr
;
1166 nexthop
= &info
->attr
->extra
->mp_nexthop_local
;
1174 bgp_table_map_apply (struct route_map
*map
, struct prefix
*p
,
1175 struct bgp_info
*info
)
1177 if (route_map_apply(map
, p
, RMAP_BGP
, info
) != RMAP_DENYMATCH
)
1180 if (bgp_debug_zebra(p
))
1182 if (p
->family
== AF_INET
)
1184 char buf
[2][INET_ADDRSTRLEN
];
1185 zlog_debug("Zebra rmap deny: IPv4 route %s/%d nexthop %s",
1186 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1188 inet_ntop(AF_INET
, &info
->attr
->nexthop
, buf
[1],
1191 if (p
->family
== AF_INET6
)
1193 char buf
[2][INET6_ADDRSTRLEN
];
1194 zlog_debug("Zebra rmap deny: IPv6 route %s/%d nexthop %s",
1195 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1197 inet_ntop(AF_INET6
, bgp_info_to_ipv6_nexthop(info
), buf
[1],
1205 bgp_zebra_announce (struct prefix
*p
, struct bgp_info
*info
, struct bgp
*bgp
,
1206 afi_t afi
, safi_t safi
)
1211 struct bgp_info
*mpinfo
;
1212 size_t oldsize
, newsize
;
1213 u_int32_t nhcount
, metric
;
1214 struct bgp_info local_info
;
1215 struct bgp_info
*info_cp
= &local_info
;
1218 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1219 * know of this instance.
1221 if (!bgp_install_info_to_zebra (bgp
))
1224 if ((p
->family
== AF_INET
&&
1225 !vrf_bitmap_check (zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_BGP
], bgp
->vrf_id
))
1226 || (p
->family
== AF_INET6
&&
1227 !vrf_bitmap_check (zclient
->redist
[AFI_IP6
][ZEBRA_ROUTE_BGP
], bgp
->vrf_id
)))
1230 if (bgp
->main_zebra_update_hold
)
1236 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1237 tag
= info
->attr
->extra
->tag
;
1241 /* When we create an aggregate route we must also install a Null0 route in
1243 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1244 SET_FLAG (flags
, ZEBRA_FLAG_BLACKHOLE
);
1246 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
||
1247 info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1249 SET_FLAG (flags
, ZEBRA_FLAG_IBGP
);
1250 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1253 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1254 || CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1255 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1257 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1259 nhcount
= 1 + bgp_info_mpath_count (info
);
1261 if (p
->family
== AF_INET
&& !BGP_ATTR_NEXTHOP_AFI_IP6(info
->attr
))
1263 struct zapi_ipv4 api
;
1264 struct in_addr
*nexthop
;
1265 char buf
[2][INET_ADDRSTRLEN
];
1266 int valid_nh_count
= 0;
1268 /* resize nexthop buffer size if necessary */
1269 if ((oldsize
= stream_get_size (bgp_nexthop_buf
)) <
1270 (sizeof (struct in_addr
*) * nhcount
))
1272 newsize
= (sizeof (struct in_addr
*) * nhcount
);
1273 newsize
= stream_resize (bgp_nexthop_buf
, newsize
);
1274 if (newsize
== oldsize
)
1276 zlog_err ("can't resize nexthop buffer");
1280 stream_reset (bgp_nexthop_buf
);
1283 /* Metric is currently based on the best-path only. */
1284 metric
= info
->attr
->med
;
1286 if (bgp
->table_map
[afi
][safi
].name
)
1288 BGP_INFO_ATTR_BUF_INIT();
1290 /* Copy info and attributes, so the route-map apply doesn't modify the
1292 BGP_INFO_ATTR_BUF_COPY(info
, info_cp
);
1293 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1295 metric
= info_cp
->attr
->med
;
1296 nexthop
= &info_cp
->attr
->nexthop
;
1298 if (info_cp
->attr
->extra
)
1299 tag
= info_cp
->attr
->extra
->tag
;
1301 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1305 nexthop
= &info
->attr
->nexthop
;
1310 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in_addr
*));
1314 for (mpinfo
= bgp_info_mpath_first (info
); mpinfo
;
1315 mpinfo
= bgp_info_mpath_next (mpinfo
))
1319 if (bgp
->table_map
[afi
][safi
].name
)
1321 /* Copy info and attributes, so the route-map apply doesn't modify the
1323 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
1324 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1325 nexthop
= &info_cp
->attr
->nexthop
;
1326 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1330 nexthop
= &mpinfo
->attr
->nexthop
;
1333 if (nexthop
== NULL
)
1336 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in_addr
*));
1340 api
.vrf_id
= bgp
->vrf_id
;
1342 api
.type
= ZEBRA_ROUTE_BGP
;
1346 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1348 /* Note that this currently only applies to Null0 routes for aggregates.
1349 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv4_route to encode a special
1350 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
1351 * do not want to also encode the 0.0.0.0 nexthop for the aggregate route.
1353 if (CHECK_FLAG(flags
, ZEBRA_FLAG_BLACKHOLE
))
1354 api
.nexthop_num
= 0;
1356 api
.nexthop_num
= valid_nh_count
;
1358 api
.nexthop
= (struct in_addr
**)STREAM_DATA (bgp_nexthop_buf
);
1359 api
.ifindex_num
= 0;
1360 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1361 api
.metric
= metric
;
1366 SET_FLAG (api
.message
, ZAPI_MESSAGE_TAG
);
1370 distance
= bgp_distance_apply (p
, info
, afi
, safi
, bgp
);
1373 SET_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
);
1374 api
.distance
= distance
;
1377 if (bgp_debug_zebra(p
))
1380 zlog_debug("Tx IPv4 route %s VRF %u %s/%d metric %u tag %"ROUTE_TAG_PRI
1381 " count %d", (valid_nh_count
? "add":"delete"),
1383 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1384 p
->prefixlen
, api
.metric
, api
.tag
, api
.nexthop_num
);
1385 for (i
= 0; i
< api
.nexthop_num
; i
++)
1386 zlog_debug(" IPv4 [nexthop %d] %s", i
+1,
1387 inet_ntop(AF_INET
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1390 zapi_ipv4_route (valid_nh_count
? ZEBRA_IPV4_ROUTE_ADD
: ZEBRA_IPV4_ROUTE_DELETE
,
1391 zclient
, (struct prefix_ipv4
*) p
, &api
);
1395 /* We have to think about a IPv6 link-local address curse. */
1396 if (p
->family
== AF_INET6
||
1397 (p
->family
== AF_INET
&& BGP_ATTR_NEXTHOP_AFI_IP6(info
->attr
)))
1400 struct in6_addr
*nexthop
;
1401 struct zapi_ipv6 api
;
1402 int valid_nh_count
= 0;
1403 char buf
[2][INET6_ADDRSTRLEN
];
1405 /* resize nexthop buffer size if necessary */
1406 if ((oldsize
= stream_get_size (bgp_nexthop_buf
)) <
1407 (sizeof (struct in6_addr
*) * nhcount
))
1409 newsize
= (sizeof (struct in6_addr
*) * nhcount
);
1410 newsize
= stream_resize (bgp_nexthop_buf
, newsize
);
1411 if (newsize
== oldsize
)
1413 zlog_err ("can't resize nexthop buffer");
1417 stream_reset (bgp_nexthop_buf
);
1419 /* resize ifindices buffer size if necessary */
1420 if ((oldsize
= stream_get_size (bgp_ifindices_buf
)) <
1421 (sizeof (unsigned int) * nhcount
))
1423 newsize
= (sizeof (unsigned int) * nhcount
);
1424 newsize
= stream_resize (bgp_ifindices_buf
, newsize
);
1425 if (newsize
== oldsize
)
1427 zlog_err ("can't resize nexthop buffer");
1431 stream_reset (bgp_ifindices_buf
);
1436 assert (info
->attr
->extra
);
1438 /* Metric is currently based on the best-path only. */
1439 metric
= info
->attr
->med
;
1441 if (bgp
->table_map
[afi
][safi
].name
)
1443 BGP_INFO_ATTR_BUF_INIT();
1445 /* Copy info and attributes, so the route-map apply doesn't modify the
1447 BGP_INFO_ATTR_BUF_COPY(info
, info_cp
);
1448 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1450 metric
= info_cp
->attr
->med
;
1451 nexthop
= bgp_info_to_ipv6_nexthop(info_cp
);
1453 if (info_cp
->attr
->extra
)
1454 tag
= info_cp
->attr
->extra
->tag
;
1456 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1460 nexthop
= bgp_info_to_ipv6_nexthop(info
);
1465 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1466 if (info
->peer
->nexthop
.ifp
)
1467 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
1471 if (info
->peer
->conf_if
|| info
->peer
->ifname
)
1472 ifindex
= ifname2ifindex_vrf (info
->peer
->conf_if
? info
->peer
->conf_if
:
1473 info
->peer
->ifname
, bgp
->vrf_id
);
1474 else if (info
->peer
->nexthop
.ifp
)
1475 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
1477 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in6_addr
*));
1478 stream_put (bgp_ifindices_buf
, &ifindex
, sizeof (unsigned int));
1482 for (mpinfo
= bgp_info_mpath_first (info
); mpinfo
;
1483 mpinfo
= bgp_info_mpath_next (mpinfo
))
1488 if (bgp
->table_map
[afi
][safi
].name
)
1490 /* Copy info and attributes, so the route-map apply doesn't modify the
1492 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
1493 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1494 nexthop
= bgp_info_to_ipv6_nexthop(info_cp
);
1495 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1499 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo
);
1502 if (nexthop
== NULL
)
1505 if (mpinfo
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1506 if (mpinfo
->peer
->nexthop
.ifp
)
1507 ifindex
= mpinfo
->peer
->nexthop
.ifp
->ifindex
;
1511 if (mpinfo
->peer
->conf_if
|| mpinfo
->peer
->ifname
)
1512 ifindex
= ifname2ifindex_vrf (mpinfo
->peer
->conf_if
? mpinfo
->peer
->conf_if
:
1513 mpinfo
->peer
->ifname
, bgp
->vrf_id
);
1514 else if (mpinfo
->peer
->nexthop
.ifp
)
1515 ifindex
= mpinfo
->peer
->nexthop
.ifp
->ifindex
;
1520 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in6_addr
*));
1521 stream_put (bgp_ifindices_buf
, &ifindex
, sizeof (unsigned int));
1525 /* Make Zebra API structure. */
1526 api
.vrf_id
= bgp
->vrf_id
;
1528 api
.type
= ZEBRA_ROUTE_BGP
;
1532 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1534 /* Note that this currently only applies to Null0 routes for aggregates.
1535 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv6_route to encode a special
1536 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
1537 * do not want to also encode the :: nexthop for the aggregate route.
1539 if (CHECK_FLAG(flags
, ZEBRA_FLAG_BLACKHOLE
))
1540 api
.nexthop_num
= 0;
1542 api
.nexthop_num
= valid_nh_count
;
1544 api
.nexthop
= (struct in6_addr
**)STREAM_DATA (bgp_nexthop_buf
);
1545 SET_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
);
1546 api
.ifindex_num
= valid_nh_count
;
1547 api
.ifindex
= (ifindex_t
*)STREAM_DATA (bgp_ifindices_buf
);
1548 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1549 api
.metric
= metric
;
1554 SET_FLAG (api
.message
, ZAPI_MESSAGE_TAG
);
1558 distance
= bgp_distance_apply (p
, info
, afi
, safi
, bgp
);
1561 SET_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
);
1562 api
.distance
= distance
;
1565 if (p
->family
== AF_INET
)
1567 if (bgp_debug_zebra(p
))
1570 zlog_debug("Tx IPv4 route %s VRF %u %s/%d metric %u tag %"ROUTE_TAG_PRI
,
1571 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1572 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1573 p
->prefixlen
, api
.metric
, api
.tag
);
1574 for (i
= 0; i
< api
.nexthop_num
; i
++)
1575 zlog_debug(" IPv6 [nexthop %d] %s", i
+1,
1576 inet_ntop(AF_INET6
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1580 zapi_ipv4_route_ipv6_nexthop (ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
,
1581 zclient
, (struct prefix_ipv4
*) p
,
1582 (struct zapi_ipv6
*)&api
);
1584 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
,
1585 zclient
, (struct prefix_ipv4
*) p
, (struct zapi_ipv4
*)&api
);
1589 if (bgp_debug_zebra(p
))
1592 zlog_debug("Tx IPv6 route %s VRF %u %s/%d metric %u tag %"ROUTE_TAG_PRI
,
1593 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1594 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1595 p
->prefixlen
, api
.metric
, api
.tag
);
1596 for (i
= 0; i
< api
.nexthop_num
; i
++)
1597 zlog_debug(" IPv6 [nexthop %d] %s", i
+1,
1598 inet_ntop(AF_INET6
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1601 zapi_ipv6_route (valid_nh_count
?
1602 ZEBRA_IPV6_ROUTE_ADD
: ZEBRA_IPV6_ROUTE_DELETE
,
1603 zclient
, (struct prefix_ipv6
*) p
, &api
);
1606 #endif /* HAVE_IPV6 */
1609 /* Announce all routes of a table to zebra */
1611 bgp_zebra_announce_table (struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1613 struct bgp_node
*rn
;
1614 struct bgp_table
*table
;
1615 struct bgp_info
*ri
;
1617 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1618 * know of this instance.
1620 if (!bgp_install_info_to_zebra (bgp
))
1623 table
= bgp
->rib
[afi
][safi
];
1626 for (rn
= bgp_table_top (table
); rn
; rn
= bgp_route_next (rn
))
1627 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1628 if (CHECK_FLAG (ri
->flags
, BGP_INFO_SELECTED
)
1629 && ri
->type
== ZEBRA_ROUTE_BGP
1630 && ri
->sub_type
== BGP_ROUTE_NORMAL
)
1631 bgp_zebra_announce (&rn
->p
, ri
, bgp
, afi
, safi
);
1635 bgp_zebra_withdraw (struct prefix
*p
, struct bgp_info
*info
, safi_t safi
)
1643 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1644 * know of this instance.
1646 if (!bgp_install_info_to_zebra (peer
->bgp
))
1649 if ((p
->family
== AF_INET
&&
1650 !vrf_bitmap_check (zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_BGP
], peer
->bgp
->vrf_id
))
1651 || (p
->family
== AF_INET6
&&
1652 !vrf_bitmap_check (zclient
->redist
[AFI_IP6
][ZEBRA_ROUTE_BGP
], peer
->bgp
->vrf_id
)))
1657 if (peer
->sort
== BGP_PEER_IBGP
)
1659 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1660 SET_FLAG (flags
, ZEBRA_FLAG_IBGP
);
1663 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1664 || CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1665 || bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1666 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1668 if (p
->family
== AF_INET
)
1670 struct zapi_ipv4 api
;
1672 api
.vrf_id
= peer
->bgp
->vrf_id
;
1675 api
.type
= ZEBRA_ROUTE_BGP
;
1679 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1680 api
.nexthop_num
= 0;
1682 api
.ifindex_num
= 0;
1683 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1684 api
.metric
= info
->attr
->med
;
1687 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1689 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1690 api
.tag
= info
->attr
->extra
->tag
;
1693 if (bgp_debug_zebra(p
))
1695 char buf
[2][INET_ADDRSTRLEN
];
1696 zlog_debug("Tx IPv4 route delete VRF %u %s/%d metric %u tag %"ROUTE_TAG_PRI
,
1698 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1699 p
->prefixlen
, api
.metric
, api
.tag
);
1702 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
, zclient
,
1703 (struct prefix_ipv4
*) p
, &api
);
1706 /* We have to think about a IPv6 link-local address curse. */
1707 if (p
->family
== AF_INET6
)
1709 struct zapi_ipv6 api
;
1711 assert (info
->attr
->extra
);
1713 api
.vrf_id
= peer
->bgp
->vrf_id
;
1715 api
.type
= ZEBRA_ROUTE_BGP
;
1719 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1720 api
.nexthop_num
= 0;
1722 api
.ifindex_num
= 0;
1723 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1724 api
.metric
= info
->attr
->med
;
1727 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1729 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1730 api
.tag
= info
->attr
->extra
->tag
;
1733 if (bgp_debug_zebra(p
))
1735 char buf
[2][INET6_ADDRSTRLEN
];
1736 zlog_debug("Tx IPv6 route delete VRF %u %s/%d metric %u tag %"ROUTE_TAG_PRI
,
1738 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1739 p
->prefixlen
, api
.metric
, api
.tag
);
1742 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE
, zclient
,
1743 (struct prefix_ipv6
*) p
, &api
);
1745 #endif /* HAVE_IPV6 */
1748 bgp_redist_lookup (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1750 struct list
*red_list
;
1751 struct listnode
*node
;
1752 struct bgp_redist
*red
;
1754 red_list
= bgp
->redist
[afi
][type
];
1758 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1759 if (red
->instance
== instance
)
1766 bgp_redist_add (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1768 struct list
*red_list
;
1769 struct bgp_redist
*red
;
1771 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1775 if (!bgp
->redist
[afi
][type
])
1776 bgp
->redist
[afi
][type
] = list_new();
1778 red_list
= bgp
->redist
[afi
][type
];
1779 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
, sizeof(struct bgp_redist
));
1780 red
->instance
= instance
;
1782 listnode_add(red_list
, red
);
1788 bgp_redist_del (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1790 struct bgp_redist
*red
;
1792 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1796 listnode_delete(bgp
->redist
[afi
][type
], red
);
1797 XFREE (MTYPE_BGP_REDIST
, red
);
1798 if (!bgp
->redist
[afi
][type
]->count
)
1800 list_free(bgp
->redist
[afi
][type
]);
1801 bgp
->redist
[afi
][type
] = NULL
;
1806 /* Other routes redistribution into BGP. */
1808 bgp_redistribute_set (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1811 /* Return if already redistribute flag is set. */
1814 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1817 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1821 if (vrf_bitmap_check (zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1825 if (bgp
->vrf_id
== VRF_DEFAULT
&&
1826 type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1827 vnc_export_bgp_enable(bgp
, afi
); /* only enables if mode bits cfg'd */
1831 vrf_bitmap_set (zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1834 /* Don't try to register if we're not connected to Zebra or Zebra doesn't
1835 * know of this instance.
1837 if (!bgp_install_info_to_zebra (bgp
))
1840 if (BGP_DEBUG (zebra
, ZEBRA
))
1841 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1843 zebra_route_string(type
), instance
);
1845 /* Send distribute add message to zebra. */
1846 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1847 instance
, bgp
->vrf_id
);
1853 bgp_redistribute_resend (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1855 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1856 * know of this instance.
1858 if (!bgp_install_info_to_zebra (bgp
))
1861 if (BGP_DEBUG (zebra
, ZEBRA
))
1862 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1864 zebra_route_string(type
), instance
);
1866 /* Send distribute add message to zebra. */
1867 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1868 instance
, bgp
->vrf_id
);
1869 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1870 instance
, bgp
->vrf_id
);
1875 /* Redistribute with route-map specification. */
1877 bgp_redistribute_rmap_set (struct bgp_redist
*red
, const char *name
)
1880 && (strcmp (red
->rmap
.name
, name
) == 0))
1884 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1885 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1886 red
->rmap
.map
= route_map_lookup_by_name (name
);
1891 /* Redistribute with metric specification. */
1893 bgp_redistribute_metric_set (struct bgp
*bgp
, struct bgp_redist
*red
, afi_t afi
,
1894 int type
, u_int32_t metric
)
1896 struct bgp_node
*rn
;
1897 struct bgp_info
*ri
;
1899 if (red
->redist_metric_flag
1900 && red
->redist_metric
== metric
)
1903 red
->redist_metric_flag
= 1;
1904 red
->redist_metric
= metric
;
1906 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
; rn
= bgp_route_next(rn
))
1908 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1910 if (ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
&&
1912 ri
->instance
== red
->instance
)
1914 struct attr
*old_attr
;
1915 struct attr new_attr
;
1916 struct attr_extra new_extra
;
1918 new_attr
.extra
= &new_extra
;
1919 bgp_attr_dup (&new_attr
, ri
->attr
);
1920 new_attr
.med
= red
->redist_metric
;
1921 old_attr
= ri
->attr
;
1922 ri
->attr
= bgp_attr_intern (&new_attr
);
1923 bgp_attr_unintern (&old_attr
);
1925 bgp_info_set_flag(rn
, ri
, BGP_INFO_ATTR_CHANGED
);
1926 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1934 /* Unset redistribution. */
1936 bgp_redistribute_unreg (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1938 struct bgp_redist
*red
;
1940 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1944 /* Return if zebra connection is disabled. */
1947 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1949 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1953 if (! vrf_bitmap_check (zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1955 vrf_bitmap_unset (zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1959 if (bgp
->vrf_id
== VRF_DEFAULT
&&
1960 type
== ZEBRA_ROUTE_VNC_DIRECT
) {
1961 vnc_export_bgp_disable(bgp
, afi
);
1965 if (bgp_install_info_to_zebra (bgp
))
1967 /* Send distribute delete message to zebra. */
1968 if (BGP_DEBUG (zebra
, ZEBRA
))
1969 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1970 bgp
->vrf_id
, afi
, zebra_route_string(type
), instance
);
1971 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
, instance
,
1975 /* Withdraw redistributed routes from current BGP's routing table. */
1976 bgp_redistribute_withdraw (bgp
, afi
, type
, instance
);
1981 /* Unset redistribution. */
1983 bgp_redistribute_unset (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1985 struct bgp_redist
*red
;
1987 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1991 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1993 /* Unset route-map. */
1995 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1996 red
->rmap
.name
= NULL
;
1997 red
->rmap
.map
= NULL
;
2000 red
->redist_metric_flag
= 0;
2001 red
->redist_metric
= 0;
2003 bgp_redist_del(bgp
, afi
, type
, instance
);
2008 /* Update redistribute vrf bitmap during triggers like
2009 restart networking or delete/add VRFs */
2011 bgp_update_redist_vrf_bitmaps (struct bgp
*bgp
, vrf_id_t old_vrf_id
)
2016 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2017 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2018 if (vrf_bitmap_check (zclient
->redist
[afi
][i
], old_vrf_id
))
2020 vrf_bitmap_unset (zclient
->redist
[afi
][i
], old_vrf_id
);
2021 vrf_bitmap_set (zclient
->redist
[afi
][i
], bgp
->vrf_id
);
2027 bgp_zclient_reset (void)
2029 zclient_reset (zclient
);
2032 /* Register this instance with Zebra. Invoked upon connect (for
2033 * default instance) and when other VRFs are learnt (or created and
2037 bgp_zebra_instance_register (struct bgp
*bgp
)
2039 /* Don't try to register if we're not connected to Zebra */
2040 if (!zclient
|| zclient
->sock
< 0)
2043 if (BGP_DEBUG (zebra
, ZEBRA
))
2044 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
2046 /* Register for router-id, interfaces, redistributed routes. */
2047 zclient_send_reg_requests (zclient
, bgp
->vrf_id
);
2050 /* Deregister this instance with Zebra. Invoked upon the instance
2051 * being deleted (default or VRF) and it is already registered.
2054 bgp_zebra_instance_deregister (struct bgp
*bgp
)
2056 /* Don't try to deregister if we're not connected to Zebra */
2057 if (zclient
->sock
< 0)
2060 if (BGP_DEBUG (zebra
, ZEBRA
))
2061 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
2063 /* Deregister for router-id, interfaces, redistributed routes. */
2064 zclient_send_dereg_requests (zclient
, bgp
->vrf_id
);
2068 bgp_zebra_initiate_radv (struct bgp
*bgp
, struct peer
*peer
)
2070 int ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
2072 /* Don't try to initiate if we're not connected to Zebra */
2073 if (zclient
->sock
< 0)
2076 if (BGP_DEBUG (zebra
, ZEBRA
))
2077 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
, peer
->host
);
2079 zclient_send_interface_radv_req (zclient
, bgp
->vrf_id
, peer
->ifp
, 1, ra_interval
);
2083 bgp_zebra_terminate_radv (struct bgp
*bgp
, struct peer
*peer
)
2085 /* Don't try to terminate if we're not connected to Zebra */
2086 if (zclient
->sock
< 0)
2089 if (BGP_DEBUG (zebra
, ZEBRA
))
2090 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
, peer
->host
);
2092 zclient_send_interface_radv_req (zclient
, bgp
->vrf_id
, peer
->ifp
, 0, 0);
2095 /* BGP has established connection with Zebra. */
2097 bgp_zebra_connected (struct zclient
*zclient
)
2101 zclient_num_connects
++; /* increment even if not responding */
2103 /* At this point, we may or may not have BGP instances configured, but
2104 * we're only interested in the default VRF (others wouldn't have learnt
2105 * the VRF from Zebra yet.)
2107 bgp
= bgp_get_default();
2111 bgp_zebra_instance_register (bgp
);
2113 /* Send the client registration */
2114 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
2116 /* TODO - What if we have peers and networks configured, do we have to
2123 bgp_zebra_init (struct thread_master
*master
)
2125 zclient_num_connects
= 0;
2127 /* Set default values. */
2128 zclient
= zclient_new (master
);
2129 zclient_init (zclient
, ZEBRA_ROUTE_BGP
, 0);
2130 zclient
->zebra_connected
= bgp_zebra_connected
;
2131 zclient
->router_id_update
= bgp_router_id_update
;
2132 zclient
->interface_add
= bgp_interface_add
;
2133 zclient
->interface_delete
= bgp_interface_delete
;
2134 zclient
->interface_address_add
= bgp_interface_address_add
;
2135 zclient
->interface_address_delete
= bgp_interface_address_delete
;
2136 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
2137 zclient
->interface_nbr_address_delete
= bgp_interface_nbr_address_delete
;
2138 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
2139 zclient
->redistribute_route_ipv4_add
= zebra_read_ipv4
;
2140 zclient
->redistribute_route_ipv4_del
= zebra_read_ipv4
;
2141 zclient
->interface_up
= bgp_interface_up
;
2142 zclient
->interface_down
= bgp_interface_down
;
2143 zclient
->redistribute_route_ipv6_add
= zebra_read_ipv6
;
2144 zclient
->redistribute_route_ipv6_del
= zebra_read_ipv6
;
2145 zclient
->nexthop_update
= bgp_read_nexthop_update
;
2146 zclient
->import_check_update
= bgp_read_import_check_update
;
2148 bgp_nexthop_buf
= stream_new(BGP_NEXTHOP_BUF_SIZE
);
2149 bgp_ifindices_buf
= stream_new(BGP_IFINDICES_BUF_SIZE
);
2153 bgp_zebra_destroy(void)
2155 if (zclient
== NULL
)
2157 zclient_stop(zclient
);
2158 zclient_free(zclient
);
2163 bgp_zebra_num_connects(void)
2165 return zclient_num_connects
;