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"
49 /* All information about zebra. */
50 struct zclient
*zclient
= NULL
;
52 /* Growable buffer for nexthops sent to zebra */
53 struct stream
*bgp_nexthop_buf
= NULL
;
54 struct stream
*bgp_ifindices_buf
= NULL
;
56 /* These array buffers are used in making a copy of the attributes for
57 route-map apply. Arrays are being used here to minimize mallocs and
58 frees for the temporary copy of the attributes.
59 Given the zapi api expects the nexthop buffer to contain pointer to
60 pointers for nexthops, we couldnt have used a single nexthop variable
61 on the stack, hence we had two options:
62 1. maintain a linked-list and free it after zapi_*_route call
63 2. use an array to avoid number of mallocs.
64 Number of supported next-hops are finite, use of arrays should be ok. */
65 struct attr attr_cp
[MULTIPATH_NUM
];
66 struct attr_extra attr_extra_cp
[MULTIPATH_NUM
];
69 /* Once per address-family initialization of the attribute array */
70 #define BGP_INFO_ATTR_BUF_INIT()\
72 memset(attr_cp, 0, MULTIPATH_NUM * sizeof(struct attr));\
73 memset(attr_extra_cp, 0, MULTIPATH_NUM * sizeof(struct attr_extra));\
77 #define BGP_INFO_ATTR_BUF_COPY(info_src, info_dst)\
79 *info_dst = *info_src; \
80 assert(attr_index != MULTIPATH_NUM);\
81 attr_cp[attr_index].extra = &attr_extra_cp[attr_index]; \
82 bgp_attr_dup (&attr_cp[attr_index], info_src->attr); \
83 bgp_attr_deep_dup (&attr_cp[attr_index], info_src->attr); \
84 info_dst->attr = &attr_cp[attr_index]; \
88 #define BGP_INFO_ATTR_BUF_FREE(info) \
90 bgp_attr_deep_free(info->attr); \
94 /* Can we install into zebra? */
96 bgp_install_info_to_zebra (struct bgp
*bgp
)
98 if (zclient
->sock
<= 0)
101 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
107 /* Router-id update message from zebra. */
109 bgp_router_id_update (int command
, struct zclient
*zclient
, zebra_size_t length
,
112 struct prefix router_id
;
114 zebra_router_id_update_read(zclient
->ibuf
,&router_id
);
116 if (BGP_DEBUG (zebra
, ZEBRA
))
118 char buf
[PREFIX2STR_BUFFER
];
119 prefix2str(&router_id
, buf
, sizeof(buf
));
120 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id
, buf
);
123 bgp_router_id_zebra_bump (vrf_id
, &router_id
);
127 /* Nexthop update message from zebra. */
129 bgp_read_nexthop_update (int command
, struct zclient
*zclient
,
130 zebra_size_t length
, vrf_id_t vrf_id
)
132 bgp_parse_nexthop_update(command
, vrf_id
);
137 bgp_read_import_check_update(int command
, struct zclient
*zclient
,
138 zebra_size_t length
, vrf_id_t vrf_id
)
140 bgp_parse_nexthop_update(command
, vrf_id
);
144 /* Set or clear interface on which unnumbered neighbor is configured. This
145 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
149 bgp_update_interface_nbrs (struct bgp
*bgp
, struct interface
*ifp
,
150 struct interface
*upd_ifp
)
152 struct listnode
*node
, *nnode
;
155 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
158 (strcmp (peer
->conf_if
, ifp
->name
) == 0))
163 bgp_zebra_initiate_radv (bgp
, peer
);
167 bgp_zebra_terminate_radv (bgp
, peer
);
175 bgp_start_interface_nbrs (struct bgp
*bgp
, struct interface
*ifp
)
177 struct listnode
*node
, *nnode
;
180 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
183 (strcmp (peer
->conf_if
, ifp
->name
) == 0) &&
184 peer
->status
!= Established
)
186 if (peer_active(peer
))
187 BGP_EVENT_ADD (peer
, BGP_Stop
);
188 BGP_EVENT_ADD (peer
, BGP_Start
);
194 bgp_nbr_connected_add (struct bgp
*bgp
, struct nbr_connected
*ifc
)
196 struct listnode
*node
;
197 struct connected
*connected
;
198 struct interface
*ifp
;
201 /* Kick-off the FSM for any relevant peers only if there is a
202 * valid local address on the interface.
205 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, connected
))
207 p
= connected
->address
;
208 if (p
->family
== AF_INET6
&&
209 IN6_IS_ADDR_LINKLOCAL (&p
->u
.prefix6
))
215 bgp_start_interface_nbrs (bgp
, ifp
);
219 bgp_nbr_connected_delete (struct bgp
*bgp
, struct nbr_connected
*ifc
, int del
)
221 struct listnode
*node
, *nnode
;
223 struct interface
*ifp
;
225 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
227 if (peer
->conf_if
&& (strcmp (peer
->conf_if
, ifc
->ifp
->name
) == 0))
229 peer
->last_reset
= PEER_DOWN_NBR_ADDR_DEL
;
230 BGP_EVENT_ADD (peer
, BGP_Stop
);
233 /* Free neighbor also, if we're asked to. */
237 listnode_delete (ifp
->nbr_connected
, ifc
);
238 nbr_connected_free (ifc
);
242 /* Inteface addition message from zebra. */
244 bgp_interface_add (int command
, struct zclient
*zclient
, zebra_size_t length
,
247 struct interface
*ifp
;
250 ifp
= zebra_interface_add_read (zclient
->ibuf
, vrf_id
);
251 if (!ifp
) // unexpected
254 if (BGP_DEBUG (zebra
, ZEBRA
) && ifp
)
255 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id
, ifp
->name
);
257 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
261 bgp_update_interface_nbrs (bgp
, ifp
, ifp
);
266 bgp_interface_delete (int command
, struct zclient
*zclient
,
267 zebra_size_t length
, vrf_id_t vrf_id
)
270 struct interface
*ifp
;
274 ifp
= zebra_interface_state_read (s
, vrf_id
);
275 if (!ifp
) /* This may happen if we've just unregistered for a VRF. */
278 ifp
->ifindex
= IFINDEX_DELETED
;
280 if (BGP_DEBUG (zebra
, ZEBRA
))
281 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id
, ifp
->name
);
283 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
287 bgp_update_interface_nbrs (bgp
, ifp
, NULL
);
292 bgp_interface_up (int command
, struct zclient
*zclient
, zebra_size_t length
,
296 struct interface
*ifp
;
298 struct nbr_connected
*nc
;
299 struct listnode
*node
, *nnode
;
303 ifp
= zebra_interface_state_read (s
, vrf_id
);
308 if (BGP_DEBUG (zebra
, ZEBRA
))
309 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id
, ifp
->name
);
311 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
315 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
316 bgp_connected_add (bgp
, c
);
318 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
319 bgp_nbr_connected_add (bgp
, nc
);
325 bgp_interface_down (int command
, struct zclient
*zclient
, zebra_size_t length
,
329 struct interface
*ifp
;
331 struct nbr_connected
*nc
;
332 struct listnode
*node
, *nnode
;
336 ifp
= zebra_interface_state_read (s
, vrf_id
);
340 if (BGP_DEBUG (zebra
, ZEBRA
))
341 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id
, ifp
->name
);
343 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
347 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
348 bgp_connected_delete (bgp
, c
);
350 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
351 bgp_nbr_connected_delete (bgp
, nc
, 1);
353 /* Fast external-failover */
357 if (CHECK_FLAG (bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
360 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
362 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
365 if (ifp
== peer
->nexthop
.ifp
)
367 BGP_EVENT_ADD (peer
, BGP_Stop
);
368 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
377 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
))
389 char buf
[PREFIX2STR_BUFFER
];
390 prefix2str(ifc
->address
, buf
, sizeof(buf
));
391 zlog_debug("Rx Intf address add VRF %u IF %s addr %s",
392 vrf_id
, ifc
->ifp
->name
, buf
);
395 if (if_is_operative (ifc
->ifp
))
399 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
403 bgp_connected_add (bgp
, ifc
);
404 /* If we have learnt of any neighbors on this interface,
405 * check to kick off any BGP interface-based neighbors,
406 * but only if this is a link-local address.
408 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
) &&
409 !list_isempty(ifc
->ifp
->nbr_connected
))
410 bgp_start_interface_nbrs (bgp
, ifc
->ifp
);
417 bgp_interface_address_delete (int command
, struct zclient
*zclient
,
418 zebra_size_t length
, vrf_id_t vrf_id
)
420 struct connected
*ifc
;
423 ifc
= zebra_interface_address_read (command
, zclient
->ibuf
, vrf_id
);
428 if (bgp_debug_zebra(ifc
->address
))
430 char buf
[PREFIX2STR_BUFFER
];
431 prefix2str(ifc
->address
, buf
, sizeof(buf
));
432 zlog_debug("Rx Intf address del VRF %u IF %s addr %s",
433 vrf_id
, ifc
->ifp
->name
, buf
);
436 if (if_is_operative (ifc
->ifp
))
438 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
440 bgp_connected_delete (bgp
, ifc
);
443 connected_free (ifc
);
449 bgp_interface_nbr_address_add (int command
, struct zclient
*zclient
,
450 zebra_size_t length
, vrf_id_t vrf_id
)
452 struct nbr_connected
*ifc
= NULL
;
455 ifc
= zebra_interface_nbr_address_read (command
, zclient
->ibuf
, vrf_id
);
460 if (bgp_debug_zebra(ifc
->address
))
462 char buf
[PREFIX2STR_BUFFER
];
463 prefix2str(ifc
->address
, buf
, sizeof(buf
));
464 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s",
465 vrf_id
, ifc
->ifp
->name
, buf
);
468 if (if_is_operative (ifc
->ifp
))
470 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
472 bgp_nbr_connected_add (bgp
, ifc
);
479 bgp_interface_nbr_address_delete (int command
, struct zclient
*zclient
,
480 zebra_size_t length
, vrf_id_t vrf_id
)
482 struct nbr_connected
*ifc
= NULL
;
485 ifc
= zebra_interface_nbr_address_read (command
, zclient
->ibuf
, vrf_id
);
490 if (bgp_debug_zebra(ifc
->address
))
492 char buf
[PREFIX2STR_BUFFER
];
493 prefix2str(ifc
->address
, buf
, sizeof(buf
));
494 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s",
495 vrf_id
, ifc
->ifp
->name
, buf
);
498 if (if_is_operative (ifc
->ifp
))
500 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
502 bgp_nbr_connected_delete (bgp
, ifc
, 0);
505 nbr_connected_free (ifc
);
510 /* VRF update for an interface. */
512 bgp_interface_vrf_update (int command
, struct zclient
*zclient
, zebra_size_t length
,
515 struct interface
*ifp
;
518 struct nbr_connected
*nc
;
519 struct listnode
*node
, *nnode
;
522 ifp
= zebra_interface_vrf_update_read (zclient
->ibuf
, vrf_id
, &new_vrf_id
);
526 if (BGP_DEBUG (zebra
, ZEBRA
) && ifp
)
527 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u",
528 vrf_id
, ifp
->name
, new_vrf_id
);
530 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
534 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
535 bgp_connected_delete (bgp
, c
);
537 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
538 bgp_nbr_connected_delete (bgp
, nc
, 1);
540 /* Fast external-failover */
544 if (CHECK_FLAG (bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
547 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
549 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
552 if (ifp
== peer
->nexthop
.ifp
)
553 BGP_EVENT_ADD (peer
, BGP_Stop
);
557 if_update_vrf (ifp
, ifp
->name
, strlen (ifp
->name
), new_vrf_id
);
559 bgp
= bgp_lookup_by_vrf_id (new_vrf_id
);
563 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
564 bgp_connected_add (bgp
, c
);
566 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
567 bgp_nbr_connected_add (bgp
, nc
);
571 /* Zebra route add and delete treatment. */
573 zebra_read_ipv4 (int command
, struct zclient
*zclient
, zebra_size_t length
,
577 struct zapi_ipv4 api
;
578 struct in_addr nexthop
;
579 struct prefix_ipv4 p
;
580 unsigned int ifindex
;
584 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
591 /* Type, flags, message. */
592 api
.type
= stream_getc (s
);
593 api
.instance
= stream_getw (s
);
594 api
.flags
= stream_getc (s
);
595 api
.message
= stream_getc (s
);
598 memset (&p
, 0, sizeof (struct prefix_ipv4
));
600 p
.prefixlen
= stream_getc (s
);
601 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
603 /* Nexthop, ifindex, distance, metric. */
604 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
606 api
.nexthop_num
= stream_getc (s
);
607 nexthop
.s_addr
= stream_get_ipv4 (s
);
610 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
612 api
.ifindex_num
= stream_getc (s
);
613 ifindex
= stream_getl (s
); /* ifindex, unused */
620 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
621 api
.distance
= stream_getc (s
);
623 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
624 api
.metric
= stream_getl (s
);
628 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
629 api
.tag
= stream_getw (s
);
633 if (command
== ZEBRA_REDISTRIBUTE_IPV4_ADD
)
635 if (bgp_debug_zebra((struct prefix
*)&p
))
637 char buf
[2][INET_ADDRSTRLEN
];
638 zlog_debug("Rx IPv4 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %d",
640 zebra_route_string(api
.type
), api
.instance
,
641 inet_ntop(AF_INET
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
643 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
649 * The ADD message is actually an UPDATE and there is no explicit DEL
650 * for a prior redistributed route, if any. So, perform an implicit
651 * DEL processing for the same redistributed route from any other
654 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
657 bgp_redistribute_delete(bgp
, (struct prefix
*)&p
, i
, api
.instance
);
660 /* Now perform the add/update. */
661 bgp_redistribute_add(bgp
, (struct prefix
*)&p
, &nexthop
, NULL
, ifindex
,
662 api
.metric
, api
.type
, api
.instance
, api
.tag
);
664 else if (command
== ZEBRA_REDISTRIBUTE_IPV4_DEL
)
666 if (bgp_debug_zebra((struct prefix
*)&p
))
668 char buf
[2][INET_ADDRSTRLEN
];
669 zlog_debug("Rx IPv4 route delete VRF %u %s[%d] %s/%d "
670 "nexthop %s metric %u tag %d",
672 zebra_route_string(api
.type
), api
.instance
,
673 inet_ntop(AF_INET
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
675 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
679 bgp_redistribute_delete(bgp
, (struct prefix
*)&p
, api
.type
, api
.instance
);
686 /* Zebra route add and delete treatment. */
688 zebra_read_ipv6 (int command
, struct zclient
*zclient
, zebra_size_t length
,
692 struct zapi_ipv6 api
;
693 struct in6_addr nexthop
;
694 struct prefix_ipv6 p
;
695 unsigned int ifindex
;
699 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
704 memset (&nexthop
, 0, sizeof (struct in6_addr
));
706 /* Type, flags, message. */
707 api
.type
= stream_getc (s
);
708 api
.instance
= stream_getw (s
);
709 api
.flags
= stream_getc (s
);
710 api
.message
= stream_getc (s
);
713 memset (&p
, 0, sizeof (struct prefix_ipv6
));
715 p
.prefixlen
= stream_getc (s
);
716 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
718 /* Nexthop, ifindex, distance, metric. */
719 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
721 api
.nexthop_num
= stream_getc (s
);
722 stream_get (&nexthop
, s
, 16);
725 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
727 api
.ifindex_num
= stream_getc (s
);
728 ifindex
= stream_getl (s
); /* ifindex, unused */
735 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
736 api
.distance
= stream_getc (s
);
740 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
741 api
.metric
= stream_getl (s
);
745 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
746 api
.tag
= stream_getw (s
);
750 /* Simply ignore link-local address. */
751 if (IN6_IS_ADDR_LINKLOCAL (&p
.prefix
))
754 if (command
== ZEBRA_REDISTRIBUTE_IPV6_ADD
)
756 if (bgp_debug_zebra((struct prefix
*)&p
))
758 char buf
[2][INET6_ADDRSTRLEN
];
759 zlog_debug("Rx IPv6 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %d",
761 zebra_route_string(api
.type
), api
.instance
,
762 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
764 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
770 * The ADD message is actually an UPDATE and there is no explicit DEL
771 * for a prior redistributed route, if any. So, perform an implicit
772 * DEL processing for the same redistributed route from any other
775 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
778 bgp_redistribute_delete(bgp
, (struct prefix
*)&p
, i
, api
.instance
);
781 bgp_redistribute_add (bgp
, (struct prefix
*)&p
, NULL
, &nexthop
, ifindex
,
782 api
.metric
, api
.type
, api
.instance
, api
.tag
);
784 else if (command
== ZEBRA_REDISTRIBUTE_IPV6_DEL
)
786 if (bgp_debug_zebra((struct prefix
*)&p
))
788 char buf
[2][INET6_ADDRSTRLEN
];
789 zlog_debug("Rx IPv6 route delete VRF %u %s[%d] %s/%d "
790 "nexthop %s metric %u tag %d",
792 zebra_route_string(api
.type
), api
.instance
,
793 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
795 inet_ntop(AF_INET6
, &nexthop
, buf
[1], sizeof(buf
[1])),
799 bgp_redistribute_delete (bgp
, (struct prefix
*) &p
, api
.type
, api
.instance
);
804 #endif /* HAVE_IPV6 */
807 if_lookup_by_ipv4 (struct in_addr
*addr
, vrf_id_t vrf_id
)
809 struct listnode
*ifnode
;
810 struct listnode
*cnode
;
811 struct interface
*ifp
;
812 struct connected
*connected
;
813 struct prefix_ipv4 p
;
818 p
.prefixlen
= IPV4_MAX_BITLEN
;
820 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
822 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
824 cp
= connected
->address
;
826 if (cp
->family
== AF_INET
)
827 if (prefix_match (cp
, (struct prefix
*)&p
))
835 if_lookup_by_ipv4_exact (struct in_addr
*addr
, vrf_id_t vrf_id
)
837 struct listnode
*ifnode
;
838 struct listnode
*cnode
;
839 struct interface
*ifp
;
840 struct connected
*connected
;
843 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
845 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
847 cp
= connected
->address
;
849 if (cp
->family
== AF_INET
)
850 if (IPV4_ADDR_SAME (&cp
->u
.prefix4
, addr
))
859 if_lookup_by_ipv6 (struct in6_addr
*addr
, unsigned int ifindex
, vrf_id_t vrf_id
)
861 struct listnode
*ifnode
;
862 struct listnode
*cnode
;
863 struct interface
*ifp
;
864 struct connected
*connected
;
865 struct prefix_ipv6 p
;
870 p
.prefixlen
= IPV6_MAX_BITLEN
;
872 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
874 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
876 cp
= connected
->address
;
878 if (cp
->family
== AF_INET6
)
879 if (prefix_match (cp
, (struct prefix
*)&p
))
881 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
))
883 if (ifindex
== ifp
->ifindex
)
895 if_lookup_by_ipv6_exact (struct in6_addr
*addr
, unsigned int ifindex
, vrf_id_t vrf_id
)
897 struct listnode
*ifnode
;
898 struct listnode
*cnode
;
899 struct interface
*ifp
;
900 struct connected
*connected
;
903 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
905 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
907 cp
= connected
->address
;
909 if (cp
->family
== AF_INET6
)
910 if (IPV6_ADDR_SAME (&cp
->u
.prefix6
, addr
))
912 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
))
914 if (ifindex
== ifp
->ifindex
)
926 if_get_ipv6_global (struct interface
*ifp
, struct in6_addr
*addr
)
928 struct listnode
*cnode
;
929 struct connected
*connected
;
932 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
))
939 memcpy (addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
947 if_get_ipv6_local (struct interface
*ifp
, struct in6_addr
*addr
)
949 struct listnode
*cnode
;
950 struct connected
*connected
;
953 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
955 cp
= connected
->address
;
957 if (cp
->family
== AF_INET6
)
958 if (IN6_IS_ADDR_LINKLOCAL (&cp
->u
.prefix6
))
960 memcpy (addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
966 #endif /* HAVE_IPV6 */
969 if_get_ipv4_address (struct interface
*ifp
, struct in_addr
*addr
)
971 struct listnode
*cnode
;
972 struct connected
*connected
;
975 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
977 cp
= connected
->address
;
978 if ((cp
->family
== AF_INET
) && !ipv4_martian(&(cp
->u
.prefix4
)))
980 *addr
= cp
->u
.prefix4
;
988 bgp_nexthop_set (union sockunion
*local
, union sockunion
*remote
,
989 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
992 struct interface
*ifp
= NULL
;
994 memset (nexthop
, 0, sizeof (struct bgp_nexthop
));
1001 if (local
->sa
.sa_family
== AF_INET
)
1003 nexthop
->v4
= local
->sin
.sin_addr
;
1004 if (peer
->update_if
)
1005 ifp
= if_lookup_by_name_vrf (peer
->update_if
, peer
->bgp
->vrf_id
);
1007 ifp
= if_lookup_by_ipv4_exact (&local
->sin
.sin_addr
, peer
->bgp
->vrf_id
);
1010 if (local
->sa
.sa_family
== AF_INET6
)
1012 if (IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
))
1014 if (peer
->conf_if
|| peer
->ifname
)
1015 ifp
= if_lookup_by_name_vrf (peer
->conf_if
? peer
->conf_if
: peer
->ifname
, peer
->bgp
->vrf_id
);
1017 else if (peer
->update_if
)
1018 ifp
= if_lookup_by_name_vrf (peer
->update_if
, peer
->bgp
->vrf_id
);
1020 ifp
= if_lookup_by_ipv6_exact (&local
->sin6
.sin6_addr
,
1021 local
->sin6
.sin6_scope_id
,
1024 #endif /* HAVE_IPV6 */
1031 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
1032 if (local
->sa
.sa_family
== AF_INET
)
1036 ret
= if_get_ipv6_global (ifp
, &nexthop
->v6_global
);
1040 /* There is no global nexthop. Use link-local address as both the
1041 * global and link-local nexthop. In this scenario, the expectation
1042 * for interop is that the network admin would use a route-map to
1043 * specify the global IPv6 nexthop.
1045 if_get_ipv6_local (ifp
, &nexthop
->v6_global
);
1046 memcpy (&nexthop
->v6_local
, &nexthop
->v6_global
,
1050 if_get_ipv6_local (ifp
, &nexthop
->v6_local
);
1052 if (if_lookup_by_ipv4 (&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
1053 peer
->shared_network
= 1;
1055 peer
->shared_network
= 0;
1056 #endif /* HAVE_IPV6 */
1060 /* IPv6 connection, fetch and store IPv4 local address if any. */
1061 if (local
->sa
.sa_family
== AF_INET6
)
1063 struct interface
*direct
= NULL
;
1066 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
1067 if (!ret
&& peer
->local_id
.s_addr
)
1068 nexthop
->v4
= peer
->local_id
;
1071 if (! IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
))
1073 memcpy (&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
1076 /* If directory connected set link-local address. */
1077 direct
= if_lookup_by_ipv6 (&remote
->sin6
.sin6_addr
,
1078 remote
->sin6
.sin6_scope_id
, peer
->bgp
->vrf_id
);
1080 if_get_ipv6_local (ifp
, &nexthop
->v6_local
);
1083 /* Link-local address. */
1085 ret
= if_get_ipv6_global (ifp
, &nexthop
->v6_global
);
1087 /* If there is no global address. Set link-local address as
1088 global. I know this break RFC specification... */
1089 /* In this scenario, the expectation for interop is that the
1090 * network admin would use a route-map to specify the global
1094 memcpy (&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
1096 /* Always set the link-local address */
1097 memcpy (&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
1101 if (IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
) ||
1102 if_lookup_by_ipv6 (&remote
->sin6
.sin6_addr
, remote
->sin6
.sin6_scope_id
,
1104 peer
->shared_network
= 1;
1106 peer
->shared_network
= 0;
1109 /* KAME stack specific treatment. */
1111 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->v6_global
)
1112 && IN6_LINKLOCAL_IFINDEX (nexthop
->v6_global
))
1114 SET_IN6_LINKLOCAL_IFINDEX (nexthop
->v6_global
, 0);
1116 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->v6_local
)
1117 && IN6_LINKLOCAL_IFINDEX (nexthop
->v6_local
))
1119 SET_IN6_LINKLOCAL_IFINDEX (nexthop
->v6_local
, 0);
1122 #endif /* HAVE_IPV6 */
1124 /* If we have identified the local interface, there is no error for now. */
1128 static struct in6_addr
*
1129 bgp_info_to_ipv6_nexthop (struct bgp_info
*info
)
1131 struct in6_addr
*nexthop
= NULL
;
1133 /* Only global address nexthop exists. */
1134 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
)
1135 nexthop
= &info
->attr
->extra
->mp_nexthop_global
;
1137 /* If both global and link-local address present. */
1138 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1140 /* Check if route-map is set to prefer global over link-local */
1141 if (info
->attr
->extra
->mp_nexthop_prefer_global
)
1142 nexthop
= &info
->attr
->extra
->mp_nexthop_global
;
1145 /* Workaround for Cisco's nexthop bug. */
1146 if (IN6_IS_ADDR_UNSPECIFIED (&info
->attr
->extra
->mp_nexthop_global
)
1147 && info
->peer
->su_remote
->sa
.sa_family
== AF_INET6
)
1148 nexthop
= &info
->peer
->su_remote
->sin6
.sin6_addr
;
1150 nexthop
= &info
->attr
->extra
->mp_nexthop_local
;
1158 bgp_table_map_apply (struct route_map
*map
, struct prefix
*p
,
1159 struct bgp_info
*info
)
1161 if (route_map_apply(map
, p
, RMAP_BGP
, info
) != RMAP_DENYMATCH
)
1164 if (bgp_debug_zebra(p
))
1166 if (p
->family
== AF_INET
)
1168 char buf
[2][INET_ADDRSTRLEN
];
1169 zlog_debug("Zebra rmap deny: IPv4 route %s/%d nexthop %s",
1170 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1172 inet_ntop(AF_INET
, &info
->attr
->nexthop
, buf
[1],
1175 if (p
->family
== AF_INET6
)
1177 char buf
[2][INET6_ADDRSTRLEN
];
1178 zlog_debug("Zebra rmap deny: IPv6 route %s/%d nexthop %s",
1179 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1181 inet_ntop(AF_INET6
, bgp_info_to_ipv6_nexthop(info
), buf
[1],
1189 bgp_zebra_announce (struct prefix
*p
, struct bgp_info
*info
, struct bgp
*bgp
,
1190 afi_t afi
, safi_t safi
)
1195 struct bgp_info
*mpinfo
;
1196 size_t oldsize
, newsize
;
1197 u_int32_t nhcount
, metric
;
1198 struct bgp_info local_info
;
1199 struct bgp_info
*info_cp
= &local_info
;
1202 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1203 * know of this instance.
1205 if (!bgp_install_info_to_zebra (bgp
))
1208 if ((p
->family
== AF_INET
&&
1209 !vrf_bitmap_check (zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_BGP
], bgp
->vrf_id
))
1210 || (p
->family
== AF_INET6
&&
1211 !vrf_bitmap_check (zclient
->redist
[AFI_IP6
][ZEBRA_ROUTE_BGP
], bgp
->vrf_id
)))
1214 if (bgp
->main_zebra_update_hold
)
1220 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1221 tag
= info
->attr
->extra
->tag
;
1225 /* When we create an aggregate route we must also install a Null0 route in
1227 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1228 SET_FLAG (flags
, ZEBRA_FLAG_BLACKHOLE
);
1230 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
||
1231 info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1233 SET_FLAG (flags
, ZEBRA_FLAG_IBGP
);
1234 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1237 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1238 || CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1239 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1241 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1243 nhcount
= 1 + bgp_info_mpath_count (info
);
1245 if (p
->family
== AF_INET
&& !BGP_ATTR_NEXTHOP_AFI_IP6(info
->attr
))
1247 struct zapi_ipv4 api
;
1248 struct in_addr
*nexthop
;
1249 char buf
[2][INET_ADDRSTRLEN
];
1250 int valid_nh_count
= 0;
1252 /* resize nexthop buffer size if necessary */
1253 if ((oldsize
= stream_get_size (bgp_nexthop_buf
)) <
1254 (sizeof (struct in_addr
*) * nhcount
))
1256 newsize
= (sizeof (struct in_addr
*) * nhcount
);
1257 newsize
= stream_resize (bgp_nexthop_buf
, newsize
);
1258 if (newsize
== oldsize
)
1260 zlog_err ("can't resize nexthop buffer");
1264 stream_reset (bgp_nexthop_buf
);
1267 /* Metric is currently based on the best-path only. */
1268 metric
= info
->attr
->med
;
1270 if (bgp
->table_map
[afi
][safi
].name
)
1272 BGP_INFO_ATTR_BUF_INIT();
1274 /* Copy info and attributes, so the route-map apply doesn't modify the
1276 BGP_INFO_ATTR_BUF_COPY(info
, info_cp
);
1277 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1279 metric
= info_cp
->attr
->med
;
1280 nexthop
= &info_cp
->attr
->nexthop
;
1282 if (info_cp
->attr
->extra
)
1283 tag
= info_cp
->attr
->extra
->tag
;
1285 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1289 nexthop
= &info
->attr
->nexthop
;
1294 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in_addr
*));
1298 for (mpinfo
= bgp_info_mpath_first (info
); mpinfo
;
1299 mpinfo
= bgp_info_mpath_next (mpinfo
))
1303 if (bgp
->table_map
[afi
][safi
].name
)
1305 /* Copy info and attributes, so the route-map apply doesn't modify the
1307 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
1308 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1309 nexthop
= &info_cp
->attr
->nexthop
;
1310 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1314 nexthop
= &mpinfo
->attr
->nexthop
;
1317 if (nexthop
== NULL
)
1320 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in_addr
*));
1324 api
.vrf_id
= bgp
->vrf_id
;
1326 api
.type
= ZEBRA_ROUTE_BGP
;
1330 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1332 /* Note that this currently only applies to Null0 routes for aggregates.
1333 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv4_route to encode a special
1334 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
1335 * do not want to also encode the 0.0.0.0 nexthop for the aggregate route.
1337 if (CHECK_FLAG(flags
, ZEBRA_FLAG_BLACKHOLE
))
1338 api
.nexthop_num
= 0;
1340 api
.nexthop_num
= valid_nh_count
;
1342 api
.nexthop
= (struct in_addr
**)STREAM_DATA (bgp_nexthop_buf
);
1343 api
.ifindex_num
= 0;
1344 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1345 api
.metric
= metric
;
1350 SET_FLAG (api
.message
, ZAPI_MESSAGE_TAG
);
1354 distance
= bgp_distance_apply (p
, info
, bgp
);
1358 SET_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
);
1359 api
.distance
= distance
;
1362 if (bgp_debug_zebra(p
))
1365 zlog_debug("Tx IPv4 route %s VRF %u %s/%d metric %u tag %d"
1366 " count %d", (valid_nh_count
? "add":"delete"),
1368 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1369 p
->prefixlen
, api
.metric
, api
.tag
, api
.nexthop_num
);
1370 for (i
= 0; i
< api
.nexthop_num
; i
++)
1371 zlog_debug(" IPv4 [nexthop %d] %s", i
+1,
1372 inet_ntop(AF_INET
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1375 zapi_ipv4_route (valid_nh_count
? ZEBRA_IPV4_ROUTE_ADD
: ZEBRA_IPV4_ROUTE_DELETE
,
1376 zclient
, (struct prefix_ipv4
*) p
, &api
);
1380 /* We have to think about a IPv6 link-local address curse. */
1381 if (p
->family
== AF_INET6
||
1382 (p
->family
== AF_INET
&& BGP_ATTR_NEXTHOP_AFI_IP6(info
->attr
)))
1384 unsigned int ifindex
;
1385 struct in6_addr
*nexthop
;
1386 struct zapi_ipv6 api
;
1387 int valid_nh_count
= 0;
1388 char buf
[2][INET6_ADDRSTRLEN
];
1390 /* resize nexthop buffer size if necessary */
1391 if ((oldsize
= stream_get_size (bgp_nexthop_buf
)) <
1392 (sizeof (struct in6_addr
*) * nhcount
))
1394 newsize
= (sizeof (struct in6_addr
*) * nhcount
);
1395 newsize
= stream_resize (bgp_nexthop_buf
, newsize
);
1396 if (newsize
== oldsize
)
1398 zlog_err ("can't resize nexthop buffer");
1402 stream_reset (bgp_nexthop_buf
);
1404 /* resize ifindices buffer size if necessary */
1405 if ((oldsize
= stream_get_size (bgp_ifindices_buf
)) <
1406 (sizeof (unsigned int) * nhcount
))
1408 newsize
= (sizeof (unsigned int) * nhcount
);
1409 newsize
= stream_resize (bgp_ifindices_buf
, newsize
);
1410 if (newsize
== oldsize
)
1412 zlog_err ("can't resize nexthop buffer");
1416 stream_reset (bgp_ifindices_buf
);
1421 assert (info
->attr
->extra
);
1423 /* Metric is currently based on the best-path only. */
1424 metric
= info
->attr
->med
;
1426 if (bgp
->table_map
[afi
][safi
].name
)
1428 BGP_INFO_ATTR_BUF_INIT();
1430 /* Copy info and attributes, so the route-map apply doesn't modify the
1432 BGP_INFO_ATTR_BUF_COPY(info
, info_cp
);
1433 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1435 metric
= info_cp
->attr
->med
;
1436 nexthop
= bgp_info_to_ipv6_nexthop(info_cp
);
1438 if (info_cp
->attr
->extra
)
1439 tag
= info_cp
->attr
->extra
->tag
;
1441 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1445 nexthop
= bgp_info_to_ipv6_nexthop(info
);
1450 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1451 if (info
->peer
->nexthop
.ifp
)
1452 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
1456 if (info
->peer
->conf_if
|| info
->peer
->ifname
)
1457 ifindex
= if_nametoindex (info
->peer
->conf_if
? info
->peer
->conf_if
: info
->peer
->ifname
);
1458 else if (info
->peer
->nexthop
.ifp
)
1459 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
1461 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in6_addr
*));
1462 stream_put (bgp_ifindices_buf
, &ifindex
, sizeof (unsigned int));
1466 for (mpinfo
= bgp_info_mpath_first (info
); mpinfo
;
1467 mpinfo
= bgp_info_mpath_next (mpinfo
))
1472 if (bgp
->table_map
[afi
][safi
].name
)
1474 /* Copy info and attributes, so the route-map apply doesn't modify the
1476 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
1477 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1478 nexthop
= bgp_info_to_ipv6_nexthop(info_cp
);
1479 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1483 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo
);
1486 if (nexthop
== NULL
)
1489 if (mpinfo
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1490 if (mpinfo
->peer
->nexthop
.ifp
)
1491 ifindex
= mpinfo
->peer
->nexthop
.ifp
->ifindex
;
1495 if (mpinfo
->peer
->conf_if
|| mpinfo
->peer
->ifname
)
1496 ifindex
= ifname2ifindex (mpinfo
->peer
->conf_if
? mpinfo
->peer
->conf_if
: mpinfo
->peer
->ifname
);
1497 else if (mpinfo
->peer
->nexthop
.ifp
)
1498 ifindex
= mpinfo
->peer
->nexthop
.ifp
->ifindex
;
1503 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in6_addr
*));
1504 stream_put (bgp_ifindices_buf
, &ifindex
, sizeof (unsigned int));
1508 /* Make Zebra API structure. */
1509 api
.vrf_id
= bgp
->vrf_id
;
1511 api
.type
= ZEBRA_ROUTE_BGP
;
1515 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1517 /* Note that this currently only applies to Null0 routes for aggregates.
1518 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv6_route to encode a special
1519 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
1520 * do not want to also encode the :: nexthop for the aggregate route.
1522 if (CHECK_FLAG(flags
, ZEBRA_FLAG_BLACKHOLE
))
1523 api
.nexthop_num
= 0;
1525 api
.nexthop_num
= valid_nh_count
;
1527 api
.nexthop
= (struct in6_addr
**)STREAM_DATA (bgp_nexthop_buf
);
1528 SET_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
);
1529 api
.ifindex_num
= valid_nh_count
;
1530 api
.ifindex
= (unsigned int *)STREAM_DATA (bgp_ifindices_buf
);
1531 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1532 api
.metric
= metric
;
1537 SET_FLAG (api
.message
, ZAPI_MESSAGE_TAG
);
1541 if (p
->family
== AF_INET
)
1543 if (bgp_debug_zebra(p
))
1546 zlog_debug("Tx IPv4 route %s VRF %u %s/%d metric %u tag %d",
1547 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1548 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1549 p
->prefixlen
, api
.metric
, api
.tag
);
1550 for (i
= 0; i
< api
.nexthop_num
; i
++)
1551 zlog_debug(" IPv6 [nexthop %d] %s", i
+1,
1552 inet_ntop(AF_INET6
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1556 zapi_ipv4_route_ipv6_nexthop (ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
,
1557 zclient
, (struct prefix_ipv4
*) p
,
1558 (struct zapi_ipv6
*)&api
);
1560 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
,
1561 zclient
, (struct prefix_ipv4
*) p
, (struct zapi_ipv4
*)&api
);
1565 if (bgp_debug_zebra(p
))
1568 zlog_debug("Tx IPv6 route %s VRF %u %s/%d metric %u tag %d",
1569 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1570 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1571 p
->prefixlen
, api
.metric
, api
.tag
);
1572 for (i
= 0; i
< api
.nexthop_num
; i
++)
1573 zlog_debug(" IPv6 [nexthop %d] %s", i
+1,
1574 inet_ntop(AF_INET6
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1577 zapi_ipv6_route (valid_nh_count
?
1578 ZEBRA_IPV6_ROUTE_ADD
: ZEBRA_IPV6_ROUTE_DELETE
,
1579 zclient
, (struct prefix_ipv6
*) p
, &api
);
1582 #endif /* HAVE_IPV6 */
1585 /* Announce all routes of a table to zebra */
1587 bgp_zebra_announce_table (struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1589 struct bgp_node
*rn
;
1590 struct bgp_table
*table
;
1591 struct bgp_info
*ri
;
1593 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1594 * know of this instance.
1596 if (!bgp_install_info_to_zebra (bgp
))
1599 table
= bgp
->rib
[afi
][safi
];
1602 for (rn
= bgp_table_top (table
); rn
; rn
= bgp_route_next (rn
))
1603 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1604 if (CHECK_FLAG (ri
->flags
, BGP_INFO_SELECTED
)
1605 && ri
->type
== ZEBRA_ROUTE_BGP
1606 && ri
->sub_type
== BGP_ROUTE_NORMAL
)
1607 bgp_zebra_announce (&rn
->p
, ri
, bgp
, afi
, safi
);
1611 bgp_zebra_withdraw (struct prefix
*p
, struct bgp_info
*info
, safi_t safi
)
1619 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1620 * know of this instance.
1622 if (!bgp_install_info_to_zebra (peer
->bgp
))
1625 if ((p
->family
== AF_INET
&&
1626 !vrf_bitmap_check (zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_BGP
], peer
->bgp
->vrf_id
))
1627 || (p
->family
== AF_INET6
&&
1628 !vrf_bitmap_check (zclient
->redist
[AFI_IP6
][ZEBRA_ROUTE_BGP
], peer
->bgp
->vrf_id
)))
1633 if (peer
->sort
== BGP_PEER_IBGP
)
1635 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1636 SET_FLAG (flags
, ZEBRA_FLAG_IBGP
);
1639 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1640 || CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1641 || bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1642 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1644 if (p
->family
== AF_INET
)
1646 struct zapi_ipv4 api
;
1648 api
.vrf_id
= peer
->bgp
->vrf_id
;
1651 api
.type
= ZEBRA_ROUTE_BGP
;
1655 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1656 api
.nexthop_num
= 0;
1658 api
.ifindex_num
= 0;
1659 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1660 api
.metric
= info
->attr
->med
;
1663 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1665 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1666 api
.tag
= info
->attr
->extra
->tag
;
1669 if (bgp_debug_zebra(p
))
1671 char buf
[2][INET_ADDRSTRLEN
];
1672 zlog_debug("Tx IPv4 route delete VRF %u %s/%d metric %u tag %d",
1674 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1675 p
->prefixlen
, api
.metric
, api
.tag
);
1678 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
, zclient
,
1679 (struct prefix_ipv4
*) p
, &api
);
1682 /* We have to think about a IPv6 link-local address curse. */
1683 if (p
->family
== AF_INET6
)
1685 struct zapi_ipv6 api
;
1687 assert (info
->attr
->extra
);
1689 api
.vrf_id
= peer
->bgp
->vrf_id
;
1691 api
.type
= ZEBRA_ROUTE_BGP
;
1695 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1696 api
.nexthop_num
= 0;
1698 api
.ifindex_num
= 0;
1699 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1700 api
.metric
= info
->attr
->med
;
1703 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1705 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1706 api
.tag
= info
->attr
->extra
->tag
;
1709 if (bgp_debug_zebra(p
))
1711 char buf
[2][INET6_ADDRSTRLEN
];
1712 zlog_debug("Tx IPv6 route delete VRF %u %s/%d metric %u tag %d",
1714 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1715 p
->prefixlen
, api
.metric
, api
.tag
);
1718 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE
, zclient
,
1719 (struct prefix_ipv6
*) p
, &api
);
1721 #endif /* HAVE_IPV6 */
1724 bgp_redist_lookup (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1726 struct list
*red_list
;
1727 struct listnode
*node
;
1728 struct bgp_redist
*red
;
1730 red_list
= bgp
->redist
[afi
][type
];
1734 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1735 if (red
->instance
== instance
)
1742 bgp_redist_add (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1744 struct list
*red_list
;
1745 struct bgp_redist
*red
;
1747 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1751 if (!bgp
->redist
[afi
][type
])
1752 bgp
->redist
[afi
][type
] = list_new();
1754 red_list
= bgp
->redist
[afi
][type
];
1755 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
, sizeof(struct bgp_redist
));
1756 red
->instance
= instance
;
1758 listnode_add(red_list
, red
);
1764 bgp_redist_del (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1766 struct bgp_redist
*red
;
1768 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1772 listnode_delete(bgp
->redist
[afi
][type
], red
);
1773 if (!bgp
->redist
[afi
][type
]->count
)
1775 list_free(bgp
->redist
[afi
][type
]);
1776 bgp
->redist
[afi
][type
] = NULL
;
1781 /* Other routes redistribution into BGP. */
1783 bgp_redistribute_set (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1786 /* Return if already redistribute flag is set. */
1789 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1792 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1796 if (vrf_bitmap_check (zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1799 vrf_bitmap_set (zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1802 /* Don't try to register if we're not connected to Zebra or Zebra doesn't
1803 * know of this instance.
1805 if (!bgp_install_info_to_zebra (bgp
))
1808 if (BGP_DEBUG (zebra
, ZEBRA
))
1809 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1811 zebra_route_string(type
), instance
);
1813 /* Send distribute add message to zebra. */
1814 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1815 instance
, bgp
->vrf_id
);
1821 bgp_redistribute_resend (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1823 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1824 * know of this instance.
1826 if (!bgp_install_info_to_zebra (bgp
))
1829 if (BGP_DEBUG (zebra
, ZEBRA
))
1830 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1832 zebra_route_string(type
), instance
);
1834 /* Send distribute add message to zebra. */
1835 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1836 instance
, bgp
->vrf_id
);
1837 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1838 instance
, bgp
->vrf_id
);
1843 /* Redistribute with route-map specification. */
1845 bgp_redistribute_rmap_set (struct bgp_redist
*red
, const char *name
)
1848 && (strcmp (red
->rmap
.name
, name
) == 0))
1852 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1853 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1854 red
->rmap
.map
= route_map_lookup_by_name (name
);
1859 /* Redistribute with metric specification. */
1861 bgp_redistribute_metric_set (struct bgp
*bgp
, struct bgp_redist
*red
, afi_t afi
,
1862 int type
, u_int32_t metric
)
1864 struct bgp_node
*rn
;
1865 struct bgp_info
*ri
;
1867 if (red
->redist_metric_flag
1868 && red
->redist_metric
== metric
)
1871 red
->redist_metric_flag
= 1;
1872 red
->redist_metric
= metric
;
1874 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
; rn
= bgp_route_next(rn
))
1876 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1878 if (ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
&&
1880 ri
->instance
== red
->instance
)
1882 struct attr
*old_attr
;
1883 struct attr new_attr
;
1884 struct attr_extra new_extra
;
1886 new_attr
.extra
= &new_extra
;
1887 bgp_attr_dup (&new_attr
, ri
->attr
);
1888 new_attr
.med
= red
->redist_metric
;
1889 old_attr
= ri
->attr
;
1890 ri
->attr
= bgp_attr_intern (&new_attr
);
1891 bgp_attr_unintern (&old_attr
);
1893 bgp_info_set_flag(rn
, ri
, BGP_INFO_ATTR_CHANGED
);
1894 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1902 /* Unset redistribution. */
1904 bgp_redistribute_unreg (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1906 struct bgp_redist
*red
;
1908 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1912 /* Return if zebra connection is disabled. */
1915 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1917 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1921 if (! vrf_bitmap_check (zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1923 vrf_bitmap_unset (zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1926 if (bgp_install_info_to_zebra (bgp
))
1928 /* Send distribute delete message to zebra. */
1929 if (BGP_DEBUG (zebra
, ZEBRA
))
1930 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1931 bgp
->vrf_id
, afi
, zebra_route_string(type
), instance
);
1932 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
, instance
,
1936 /* Withdraw redistributed routes from current BGP's routing table. */
1937 bgp_redistribute_withdraw (bgp
, afi
, type
, instance
);
1942 /* Unset redistribution. */
1944 bgp_redistribute_unset (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1946 struct bgp_redist
*red
;
1948 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1952 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1954 /* Unset route-map. */
1956 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1957 red
->rmap
.name
= NULL
;
1958 red
->rmap
.map
= NULL
;
1961 red
->redist_metric_flag
= 0;
1962 red
->redist_metric
= 0;
1964 bgp_redist_del(bgp
, afi
, type
, instance
);
1969 /* Update redistribute vrf bitmap during triggers like
1970 restart networking or delete/add VRFs */
1972 bgp_update_redist_vrf_bitmaps (struct bgp
*bgp
, vrf_id_t old_vrf_id
)
1977 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1978 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1979 if (vrf_bitmap_check (zclient
->redist
[afi
][i
], old_vrf_id
))
1981 vrf_bitmap_unset (zclient
->redist
[afi
][i
], old_vrf_id
);
1982 vrf_bitmap_set (zclient
->redist
[afi
][i
], bgp
->vrf_id
);
1988 bgp_zclient_reset (void)
1990 zclient_reset (zclient
);
1993 /* Register this instance with Zebra. Invoked upon connect (for
1994 * default instance) and when other VRFs are learnt (or created and
1998 bgp_zebra_instance_register (struct bgp
*bgp
)
2000 /* Don't try to register if we're not connected to Zebra */
2001 if (!zclient
|| zclient
->sock
< 0)
2004 if (BGP_DEBUG (zebra
, ZEBRA
))
2005 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
2007 /* Register for router-id, interfaces, redistributed routes. */
2008 zclient_send_reg_requests (zclient
, bgp
->vrf_id
);
2011 /* Deregister this instance with Zebra. Invoked upon the instance
2012 * being deleted (default or VRF) and it is already registered.
2015 bgp_zebra_instance_deregister (struct bgp
*bgp
)
2017 /* Don't try to deregister if we're not connected to Zebra */
2018 if (zclient
->sock
< 0)
2021 if (BGP_DEBUG (zebra
, ZEBRA
))
2022 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
2024 /* Deregister for router-id, interfaces, redistributed routes. */
2025 zclient_send_dereg_requests (zclient
, bgp
->vrf_id
);
2029 bgp_zebra_initiate_radv (struct bgp
*bgp
, struct peer
*peer
)
2031 int ra_interval
= BGP_UNNUM_DEFAULT_RA_INTERVAL
;
2033 /* Don't try to initiate if we're not connected to Zebra */
2034 if (zclient
->sock
< 0)
2037 if (BGP_DEBUG (zebra
, ZEBRA
))
2038 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
, peer
->host
);
2040 zclient_send_interface_radv_req (zclient
, bgp
->vrf_id
, peer
->ifp
, 1, ra_interval
);
2044 bgp_zebra_terminate_radv (struct bgp
*bgp
, struct peer
*peer
)
2046 /* Don't try to terminate if we're not connected to Zebra */
2047 if (zclient
->sock
< 0)
2050 if (BGP_DEBUG (zebra
, ZEBRA
))
2051 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
, peer
->host
);
2053 zclient_send_interface_radv_req (zclient
, bgp
->vrf_id
, peer
->ifp
, 0, 0);
2056 /* BGP has established connection with Zebra. */
2058 bgp_zebra_connected (struct zclient
*zclient
)
2062 /* At this point, we may or may not have BGP instances configured, but
2063 * we're only interested in the default VRF (others wouldn't have learnt
2064 * the VRF from Zebra yet.)
2066 bgp
= bgp_get_default();
2070 bgp_zebra_instance_register (bgp
);
2072 /* Send the client registration */
2073 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
2075 /* TODO - What if we have peers and networks configured, do we have to
2082 bgp_zebra_init (struct thread_master
*master
)
2084 /* Set default values. */
2085 zclient
= zclient_new (master
);
2086 zclient_init (zclient
, ZEBRA_ROUTE_BGP
, 0);
2087 zclient
->zebra_connected
= bgp_zebra_connected
;
2088 zclient
->router_id_update
= bgp_router_id_update
;
2089 zclient
->interface_add
= bgp_interface_add
;
2090 zclient
->interface_delete
= bgp_interface_delete
;
2091 zclient
->interface_address_add
= bgp_interface_address_add
;
2092 zclient
->interface_address_delete
= bgp_interface_address_delete
;
2093 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
2094 zclient
->interface_nbr_address_delete
= bgp_interface_nbr_address_delete
;
2095 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
2096 zclient
->ipv4_route_add
= zebra_read_ipv4
;
2097 zclient
->ipv4_route_delete
= zebra_read_ipv4
;
2098 zclient
->redistribute_route_ipv4_add
= zebra_read_ipv4
;
2099 zclient
->redistribute_route_ipv4_del
= zebra_read_ipv4
;
2100 zclient
->interface_up
= bgp_interface_up
;
2101 zclient
->interface_down
= bgp_interface_down
;
2102 zclient
->ipv6_route_add
= zebra_read_ipv6
;
2103 zclient
->ipv6_route_delete
= zebra_read_ipv6
;
2104 zclient
->redistribute_route_ipv6_add
= zebra_read_ipv6
;
2105 zclient
->redistribute_route_ipv6_del
= zebra_read_ipv6
;
2106 zclient
->nexthop_update
= bgp_read_nexthop_update
;
2107 zclient
->import_check_update
= bgp_read_import_check_update
;
2109 bgp_nexthop_buf
= stream_new(BGP_NEXTHOP_BUF_SIZE
);
2110 bgp_ifindices_buf
= stream_new(BGP_IFINDICES_BUF_SIZE
);
2114 bgp_zebra_destroy(void)
2116 if (zclient
== NULL
)
2118 zclient_stop(zclient
);
2119 zclient_free(zclient
);