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"
36 #include "bgpd/bgpd.h"
37 #include "bgpd/bgp_route.h"
38 #include "bgpd/bgp_attr.h"
39 #include "bgpd/bgp_nexthop.h"
40 #include "bgpd/bgp_zebra.h"
41 #include "bgpd/bgp_fsm.h"
42 #include "bgpd/bgp_debug.h"
43 #include "bgpd/bgp_mpath.h"
44 #include "bgpd/bgp_nexthop.h"
45 #include "bgpd/bgp_nht.h"
46 #include "bgpd/bgp_bfd.h"
48 /* All information about zebra. */
49 struct zclient
*zclient
= NULL
;
51 /* Growable buffer for nexthops sent to zebra */
52 struct stream
*bgp_nexthop_buf
= NULL
;
53 struct stream
*bgp_ifindices_buf
= NULL
;
55 /* These array buffers are used in making a copy of the attributes for
56 route-map apply. Arrays are being used here to minimize mallocs and
57 frees for the temporary copy of the attributes.
58 Given the zapi api expects the nexthop buffer to contain pointer to
59 pointers for nexthops, we couldnt have used a single nexthop variable
60 on the stack, hence we had two options:
61 1. maintain a linked-list and free it after zapi_*_route call
62 2. use an array to avoid number of mallocs.
63 Number of supported next-hops are finite, use of arrays should be ok. */
64 struct attr attr_cp
[MULTIPATH_NUM
];
65 struct attr_extra attr_extra_cp
[MULTIPATH_NUM
];
68 /* Once per address-family initialization of the attribute array */
69 #define BGP_INFO_ATTR_BUF_INIT()\
71 memset(attr_cp, 0, MULTIPATH_NUM * sizeof(struct attr));\
72 memset(attr_extra_cp, 0, MULTIPATH_NUM * sizeof(struct attr_extra));\
76 #define BGP_INFO_ATTR_BUF_COPY(info_src, info_dst)\
78 *info_dst = *info_src; \
79 assert(attr_index != MULTIPATH_NUM);\
80 attr_cp[attr_index].extra = &attr_extra_cp[attr_index]; \
81 bgp_attr_dup (&attr_cp[attr_index], info_src->attr); \
82 bgp_attr_deep_dup (&attr_cp[attr_index], info_src->attr); \
83 info_dst->attr = &attr_cp[attr_index]; \
87 #define BGP_INFO_ATTR_BUF_FREE(info) \
89 bgp_attr_deep_free(info->attr); \
93 /* Can we install into zebra? */
95 bgp_install_info_to_zebra (struct bgp
*bgp
)
97 if (zclient
->sock
<= 0)
100 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
106 /* Router-id update message from zebra. */
108 bgp_router_id_update (int command
, struct zclient
*zclient
, zebra_size_t length
,
111 struct prefix router_id
;
112 struct listnode
*node
, *nnode
;
115 zebra_router_id_update_read(zclient
->ibuf
,&router_id
);
117 if (BGP_DEBUG (zebra
, ZEBRA
))
119 char buf
[PREFIX2STR_BUFFER
];
120 prefix2str(&router_id
, buf
, sizeof(buf
));
121 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id
, buf
);
124 if (vrf_id
== VRF_DEFAULT
)
126 /* Router-id change for default VRF has to also update all views. */
127 for (ALL_LIST_ELEMENTS (bm
->bgp
, node
, nnode
, bgp
))
129 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
132 bgp
->router_id_zebra
= router_id
.u
.prefix4
;
134 if (!bgp
->router_id_static
.s_addr
)
135 bgp_router_id_set (bgp
, &router_id
.u
.prefix4
);
140 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
143 bgp
->router_id_zebra
= router_id
.u
.prefix4
;
145 if (!bgp
->router_id_static
.s_addr
)
146 bgp_router_id_set (bgp
, &router_id
.u
.prefix4
);
153 /* Nexthop update message from zebra. */
155 bgp_read_nexthop_update (int command
, struct zclient
*zclient
,
156 zebra_size_t length
, vrf_id_t vrf_id
)
158 bgp_parse_nexthop_update(command
, vrf_id
);
163 bgp_read_import_check_update(int command
, struct zclient
*zclient
,
164 zebra_size_t length
, vrf_id_t vrf_id
)
166 bgp_parse_nexthop_update(command
, vrf_id
);
170 /* Set or clear interface on which unnumbered neighbor is configured. This
171 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
175 bgp_update_interface_nbrs (struct bgp
*bgp
, struct interface
*ifp
,
176 struct interface
*upd_ifp
)
178 struct listnode
*node
, *nnode
;
181 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
184 (strcmp (peer
->conf_if
, ifp
->name
) == 0))
189 bgp_zebra_initiate_radv (bgp
, peer
);
193 bgp_zebra_terminate_radv (bgp
, peer
);
201 bgp_start_interface_nbrs (struct bgp
*bgp
, struct interface
*ifp
)
203 struct listnode
*node
, *nnode
;
206 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
209 (strcmp (peer
->conf_if
, ifp
->name
) == 0) &&
210 peer
->status
!= Established
)
212 if (peer_active(peer
))
213 BGP_EVENT_ADD (peer
, BGP_Stop
);
214 BGP_EVENT_ADD (peer
, BGP_Start
);
220 bgp_nbr_connected_add (struct bgp
*bgp
, struct nbr_connected
*ifc
)
222 struct listnode
*node
;
223 struct connected
*connected
;
224 struct interface
*ifp
;
227 /* Kick-off the FSM for any relevant peers only if there is a
228 * valid local address on the interface.
231 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, connected
))
233 p
= connected
->address
;
234 if (p
->family
== AF_INET6
&&
235 IN6_IS_ADDR_LINKLOCAL (&p
->u
.prefix6
))
241 bgp_start_interface_nbrs (bgp
, ifp
);
245 bgp_nbr_connected_delete (struct bgp
*bgp
, struct nbr_connected
*ifc
, int del
)
247 struct listnode
*node
, *nnode
;
249 struct interface
*ifp
;
251 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
253 if (peer
->conf_if
&& (strcmp (peer
->conf_if
, ifc
->ifp
->name
) == 0))
255 peer
->last_reset
= PEER_DOWN_NBR_ADDR_DEL
;
256 BGP_EVENT_ADD (peer
, BGP_Stop
);
259 /* Free neighbor also, if we're asked to. */
263 listnode_delete (ifp
->nbr_connected
, ifc
);
264 nbr_connected_free (ifc
);
268 /* Inteface addition message from zebra. */
270 bgp_interface_add (int command
, struct zclient
*zclient
, zebra_size_t length
,
273 struct interface
*ifp
;
276 ifp
= zebra_interface_add_read (zclient
->ibuf
, vrf_id
);
277 if (!ifp
) // unexpected
280 if (BGP_DEBUG (zebra
, ZEBRA
) && ifp
)
281 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id
, ifp
->name
);
283 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
287 bgp_update_interface_nbrs (bgp
, ifp
, ifp
);
292 bgp_interface_delete (int command
, struct zclient
*zclient
,
293 zebra_size_t length
, vrf_id_t vrf_id
)
296 struct interface
*ifp
;
300 ifp
= zebra_interface_state_read (s
, vrf_id
);
301 if (!ifp
) /* This may happen if we've just unregistered for a VRF. */
304 ifp
->ifindex
= IFINDEX_DELETED
;
306 if (BGP_DEBUG (zebra
, ZEBRA
))
307 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id
, ifp
->name
);
309 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
313 bgp_update_interface_nbrs (bgp
, ifp
, NULL
);
318 bgp_interface_up (int command
, struct zclient
*zclient
, zebra_size_t length
,
322 struct interface
*ifp
;
324 struct nbr_connected
*nc
;
325 struct listnode
*node
, *nnode
;
329 ifp
= zebra_interface_state_read (s
, vrf_id
);
334 if (BGP_DEBUG (zebra
, ZEBRA
))
335 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id
, ifp
->name
);
337 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
341 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
342 bgp_connected_add (bgp
, c
);
344 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
345 bgp_nbr_connected_add (bgp
, nc
);
351 bgp_interface_down (int command
, struct zclient
*zclient
, zebra_size_t length
,
355 struct interface
*ifp
;
357 struct nbr_connected
*nc
;
358 struct listnode
*node
, *nnode
;
362 ifp
= zebra_interface_state_read (s
, vrf_id
);
366 if (BGP_DEBUG (zebra
, ZEBRA
))
367 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id
, ifp
->name
);
369 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
373 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
374 bgp_connected_delete (bgp
, c
);
376 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
377 bgp_nbr_connected_delete (bgp
, nc
, 1);
379 /* Fast external-failover */
383 if (CHECK_FLAG (bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
386 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
388 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
391 if (ifp
== peer
->nexthop
.ifp
)
393 BGP_EVENT_ADD (peer
, BGP_Stop
);
394 peer
->last_reset
= PEER_DOWN_IF_DOWN
;
403 bgp_interface_address_add (int command
, struct zclient
*zclient
,
404 zebra_size_t length
, vrf_id_t vrf_id
)
406 struct connected
*ifc
;
408 ifc
= zebra_interface_address_read (command
, zclient
->ibuf
, vrf_id
);
413 if (bgp_debug_zebra(ifc
->address
))
415 char buf
[PREFIX2STR_BUFFER
];
416 prefix2str(ifc
->address
, buf
, sizeof(buf
));
417 zlog_debug("Rx Intf address add VRF %u IF %s addr %s",
418 vrf_id
, ifc
->ifp
->name
, buf
);
421 if (if_is_operative (ifc
->ifp
))
425 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
429 bgp_connected_add (bgp
, ifc
);
430 /* If we have learnt of any neighbors on this interface,
431 * check to kick off any BGP interface-based neighbors,
432 * but only if this is a link-local address.
434 if (IN6_IS_ADDR_LINKLOCAL(&ifc
->address
->u
.prefix6
) &&
435 !list_isempty(ifc
->ifp
->nbr_connected
))
436 bgp_start_interface_nbrs (bgp
, ifc
->ifp
);
443 bgp_interface_address_delete (int command
, struct zclient
*zclient
,
444 zebra_size_t length
, vrf_id_t vrf_id
)
446 struct connected
*ifc
;
449 ifc
= zebra_interface_address_read (command
, zclient
->ibuf
, vrf_id
);
454 if (bgp_debug_zebra(ifc
->address
))
456 char buf
[PREFIX2STR_BUFFER
];
457 prefix2str(ifc
->address
, buf
, sizeof(buf
));
458 zlog_debug("Rx Intf address del VRF %u IF %s addr %s",
459 vrf_id
, ifc
->ifp
->name
, buf
);
462 if (if_is_operative (ifc
->ifp
))
464 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
466 bgp_connected_delete (bgp
, ifc
);
469 connected_free (ifc
);
475 bgp_interface_nbr_address_add (int command
, struct zclient
*zclient
,
476 zebra_size_t length
, vrf_id_t vrf_id
)
478 struct nbr_connected
*ifc
= NULL
;
481 ifc
= zebra_interface_nbr_address_read (command
, zclient
->ibuf
, vrf_id
);
486 if (bgp_debug_zebra(ifc
->address
))
488 char buf
[PREFIX2STR_BUFFER
];
489 prefix2str(ifc
->address
, buf
, sizeof(buf
));
490 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s",
491 vrf_id
, ifc
->ifp
->name
, buf
);
494 if (if_is_operative (ifc
->ifp
))
496 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
498 bgp_nbr_connected_add (bgp
, ifc
);
505 bgp_interface_nbr_address_delete (int command
, struct zclient
*zclient
,
506 zebra_size_t length
, vrf_id_t vrf_id
)
508 struct nbr_connected
*ifc
= NULL
;
511 ifc
= zebra_interface_nbr_address_read (command
, zclient
->ibuf
, vrf_id
);
516 if (bgp_debug_zebra(ifc
->address
))
518 char buf
[PREFIX2STR_BUFFER
];
519 prefix2str(ifc
->address
, buf
, sizeof(buf
));
520 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s",
521 vrf_id
, ifc
->ifp
->name
, buf
);
524 if (if_is_operative (ifc
->ifp
))
526 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
528 bgp_nbr_connected_delete (bgp
, ifc
, 0);
531 nbr_connected_free (ifc
);
536 /* VRF update for an interface. */
538 bgp_interface_vrf_update (int command
, struct zclient
*zclient
, zebra_size_t length
,
541 struct interface
*ifp
;
544 struct nbr_connected
*nc
;
545 struct listnode
*node
, *nnode
;
548 ifp
= zebra_interface_vrf_update_read (zclient
->ibuf
, vrf_id
, &new_vrf_id
);
552 if (BGP_DEBUG (zebra
, ZEBRA
) && ifp
)
553 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u",
554 vrf_id
, ifp
->name
, new_vrf_id
);
556 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
560 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
561 bgp_connected_delete (bgp
, c
);
563 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
564 bgp_nbr_connected_delete (bgp
, nc
, 1);
566 /* Fast external-failover */
570 if (CHECK_FLAG (bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
573 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
575 if ((peer
->ttl
!= 1) && (peer
->gtsm_hops
!= 1))
578 if (ifp
== peer
->nexthop
.ifp
)
579 BGP_EVENT_ADD (peer
, BGP_Stop
);
583 if_update_vrf (ifp
, ifp
->name
, strlen (ifp
->name
), new_vrf_id
);
585 bgp
= bgp_lookup_by_vrf_id (new_vrf_id
);
589 for (ALL_LIST_ELEMENTS (ifp
->connected
, node
, nnode
, c
))
590 bgp_connected_add (bgp
, c
);
592 for (ALL_LIST_ELEMENTS (ifp
->nbr_connected
, node
, nnode
, nc
))
593 bgp_nbr_connected_add (bgp
, nc
);
597 /* Zebra route add and delete treatment. */
599 zebra_read_ipv4 (int command
, struct zclient
*zclient
, zebra_size_t length
,
603 struct zapi_ipv4 api
;
604 struct in_addr nexthop
;
605 struct prefix_ipv4 p
;
606 unsigned int ifindex
;
610 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
617 /* Type, flags, message. */
618 api
.type
= stream_getc (s
);
619 api
.instance
= stream_getw (s
);
620 api
.flags
= stream_getc (s
);
621 api
.message
= stream_getc (s
);
624 memset (&p
, 0, sizeof (struct prefix_ipv4
));
626 p
.prefixlen
= stream_getc (s
);
627 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
629 /* Nexthop, ifindex, distance, metric. */
630 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
632 api
.nexthop_num
= stream_getc (s
);
633 nexthop
.s_addr
= stream_get_ipv4 (s
);
636 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
638 api
.ifindex_num
= stream_getc (s
);
639 ifindex
= stream_getl (s
); /* ifindex, unused */
646 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
647 api
.distance
= stream_getc (s
);
649 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
650 api
.metric
= stream_getl (s
);
654 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
655 api
.tag
= stream_getw (s
);
659 if (command
== ZEBRA_REDISTRIBUTE_IPV4_ADD
)
661 if (bgp_debug_zebra((struct prefix
*)&p
))
663 char buf
[2][INET_ADDRSTRLEN
];
664 zlog_debug("Rx IPv4 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %d",
666 zebra_route_string(api
.type
), api
.instance
,
667 inet_ntop(AF_INET
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
669 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
675 * The ADD message is actually an UPDATE and there is no explicit DEL
676 * for a prior redistributed route, if any. So, perform an implicit
677 * DEL processing for the same redistributed route from any other
680 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
683 bgp_redistribute_delete(bgp
, (struct prefix
*)&p
, i
, api
.instance
);
686 /* Now perform the add/update. */
687 bgp_redistribute_add(bgp
, (struct prefix
*)&p
, &nexthop
, NULL
, ifindex
,
688 api
.metric
, api
.type
, api
.instance
, api
.tag
);
690 else if (command
== ZEBRA_REDISTRIBUTE_IPV4_DEL
)
692 if (bgp_debug_zebra((struct prefix
*)&p
))
694 char buf
[2][INET_ADDRSTRLEN
];
695 zlog_debug("Rx IPv4 route delete VRF %u %s[%d] %s/%d "
696 "nexthop %s metric %u tag %d",
698 zebra_route_string(api
.type
), api
.instance
,
699 inet_ntop(AF_INET
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
701 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
705 bgp_redistribute_delete(bgp
, (struct prefix
*)&p
, api
.type
, api
.instance
);
712 /* Zebra route add and delete treatment. */
714 zebra_read_ipv6 (int command
, struct zclient
*zclient
, zebra_size_t length
,
718 struct zapi_ipv6 api
;
719 struct in6_addr nexthop
;
720 struct prefix_ipv6 p
;
721 unsigned int ifindex
;
725 bgp
= bgp_lookup_by_vrf_id (vrf_id
);
730 memset (&nexthop
, 0, sizeof (struct in6_addr
));
732 /* Type, flags, message. */
733 api
.type
= stream_getc (s
);
734 api
.instance
= stream_getw (s
);
735 api
.flags
= stream_getc (s
);
736 api
.message
= stream_getc (s
);
739 memset (&p
, 0, sizeof (struct prefix_ipv6
));
741 p
.prefixlen
= stream_getc (s
);
742 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
744 /* Nexthop, ifindex, distance, metric. */
745 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
747 api
.nexthop_num
= stream_getc (s
);
748 stream_get (&nexthop
, s
, 16);
751 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
753 api
.ifindex_num
= stream_getc (s
);
754 ifindex
= stream_getl (s
); /* ifindex, unused */
761 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
762 api
.distance
= stream_getc (s
);
766 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
767 api
.metric
= stream_getl (s
);
771 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_TAG
))
772 api
.tag
= stream_getw (s
);
776 /* Simply ignore link-local address. */
777 if (IN6_IS_ADDR_LINKLOCAL (&p
.prefix
))
780 if (command
== ZEBRA_REDISTRIBUTE_IPV6_ADD
)
782 if (bgp_debug_zebra((struct prefix
*)&p
))
784 char buf
[2][INET6_ADDRSTRLEN
];
785 zlog_debug("Rx IPv6 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %d",
787 zebra_route_string(api
.type
), api
.instance
,
788 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
790 inet_ntop(AF_INET
, &nexthop
, buf
[1], sizeof(buf
[1])),
796 * The ADD message is actually an UPDATE and there is no explicit DEL
797 * for a prior redistributed route, if any. So, perform an implicit
798 * DEL processing for the same redistributed route from any other
801 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
804 bgp_redistribute_delete(bgp
, (struct prefix
*)&p
, i
, api
.instance
);
807 bgp_redistribute_add (bgp
, (struct prefix
*)&p
, NULL
, &nexthop
, ifindex
,
808 api
.metric
, api
.type
, api
.instance
, api
.tag
);
810 else if (command
== ZEBRA_REDISTRIBUTE_IPV6_DEL
)
812 if (bgp_debug_zebra((struct prefix
*)&p
))
814 char buf
[2][INET6_ADDRSTRLEN
];
815 zlog_debug("Rx IPv6 route delete VRF %u %s[%d] %s/%d "
816 "nexthop %s metric %u tag %d",
818 zebra_route_string(api
.type
), api
.instance
,
819 inet_ntop(AF_INET6
, &p
.prefix
, buf
[0], sizeof(buf
[0])),
821 inet_ntop(AF_INET6
, &nexthop
, buf
[1], sizeof(buf
[1])),
825 bgp_redistribute_delete (bgp
, (struct prefix
*) &p
, api
.type
, api
.instance
);
830 #endif /* HAVE_IPV6 */
833 if_lookup_by_ipv4 (struct in_addr
*addr
, vrf_id_t vrf_id
)
835 struct listnode
*ifnode
;
836 struct listnode
*cnode
;
837 struct interface
*ifp
;
838 struct connected
*connected
;
839 struct prefix_ipv4 p
;
844 p
.prefixlen
= IPV4_MAX_BITLEN
;
846 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
848 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
850 cp
= connected
->address
;
852 if (cp
->family
== AF_INET
)
853 if (prefix_match (cp
, (struct prefix
*)&p
))
861 if_lookup_by_ipv4_exact (struct in_addr
*addr
, vrf_id_t vrf_id
)
863 struct listnode
*ifnode
;
864 struct listnode
*cnode
;
865 struct interface
*ifp
;
866 struct connected
*connected
;
869 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
871 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
873 cp
= connected
->address
;
875 if (cp
->family
== AF_INET
)
876 if (IPV4_ADDR_SAME (&cp
->u
.prefix4
, addr
))
885 if_lookup_by_ipv6 (struct in6_addr
*addr
, unsigned int ifindex
, vrf_id_t vrf_id
)
887 struct listnode
*ifnode
;
888 struct listnode
*cnode
;
889 struct interface
*ifp
;
890 struct connected
*connected
;
891 struct prefix_ipv6 p
;
896 p
.prefixlen
= IPV6_MAX_BITLEN
;
898 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
900 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
902 cp
= connected
->address
;
904 if (cp
->family
== AF_INET6
)
905 if (prefix_match (cp
, (struct prefix
*)&p
))
907 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
.s6_addr32
[0]))
909 if (ifindex
== ifp
->ifindex
)
921 if_lookup_by_ipv6_exact (struct in6_addr
*addr
, unsigned int ifindex
, vrf_id_t vrf_id
)
923 struct listnode
*ifnode
;
924 struct listnode
*cnode
;
925 struct interface
*ifp
;
926 struct connected
*connected
;
929 for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id
), ifnode
, ifp
))
931 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
933 cp
= connected
->address
;
935 if (cp
->family
== AF_INET6
)
936 if (IPV6_ADDR_SAME (&cp
->u
.prefix6
, addr
))
938 if (IN6_IS_ADDR_LINKLOCAL(&cp
->u
.prefix6
))
940 if (ifindex
== ifp
->ifindex
)
952 if_get_ipv6_global (struct interface
*ifp
, struct in6_addr
*addr
)
954 struct listnode
*cnode
;
955 struct connected
*connected
;
958 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
960 cp
= connected
->address
;
962 if (cp
->family
== AF_INET6
)
963 if (! IN6_IS_ADDR_LINKLOCAL (&cp
->u
.prefix6
))
965 memcpy (addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
973 if_get_ipv6_local (struct interface
*ifp
, struct in6_addr
*addr
)
975 struct listnode
*cnode
;
976 struct connected
*connected
;
979 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
981 cp
= connected
->address
;
983 if (cp
->family
== AF_INET6
)
984 if (IN6_IS_ADDR_LINKLOCAL (&cp
->u
.prefix6
))
986 memcpy (addr
, &cp
->u
.prefix6
, IPV6_MAX_BYTELEN
);
992 #endif /* HAVE_IPV6 */
995 if_get_ipv4_address (struct interface
*ifp
, struct in_addr
*addr
)
997 struct listnode
*cnode
;
998 struct connected
*connected
;
1001 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, cnode
, connected
))
1003 cp
= connected
->address
;
1004 if ((cp
->family
== AF_INET
) && !ipv4_martian(&(cp
->u
.prefix4
)))
1006 *addr
= cp
->u
.prefix4
;
1014 bgp_nexthop_set (union sockunion
*local
, union sockunion
*remote
,
1015 struct bgp_nexthop
*nexthop
, struct peer
*peer
)
1018 struct interface
*ifp
= NULL
;
1020 memset (nexthop
, 0, sizeof (struct bgp_nexthop
));
1027 if (local
->sa
.sa_family
== AF_INET
)
1029 nexthop
->v4
= local
->sin
.sin_addr
;
1030 if (peer
->update_if
)
1031 ifp
= if_lookup_by_name_vrf (peer
->update_if
, peer
->bgp
->vrf_id
);
1033 ifp
= if_lookup_by_ipv4_exact (&local
->sin
.sin_addr
, peer
->bgp
->vrf_id
);
1036 if (local
->sa
.sa_family
== AF_INET6
)
1038 if (IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
))
1040 if (peer
->conf_if
|| peer
->ifname
)
1041 ifp
= if_lookup_by_index_vrf (if_nametoindex (peer
->conf_if
? peer
->conf_if
: peer
->ifname
), peer
->bgp
->vrf_id
);
1043 else if (peer
->update_if
)
1044 ifp
= if_lookup_by_name_vrf (peer
->update_if
, peer
->bgp
->vrf_id
);
1046 ifp
= if_lookup_by_ipv6_exact (&local
->sin6
.sin6_addr
,
1047 local
->sin6
.sin6_scope_id
,
1050 #endif /* HAVE_IPV6 */
1057 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
1058 if (local
->sa
.sa_family
== AF_INET
)
1062 ret
= if_get_ipv6_global (ifp
, &nexthop
->v6_global
);
1066 /* There is no global nexthop. Use link-local address as both the
1067 * global and link-local nexthop. In this scenario, the expectation
1068 * for interop is that the network admin would use a route-map to
1069 * specify the global IPv6 nexthop.
1071 if_get_ipv6_local (ifp
, &nexthop
->v6_global
);
1072 memcpy (&nexthop
->v6_local
, &nexthop
->v6_global
,
1076 if_get_ipv6_local (ifp
, &nexthop
->v6_local
);
1078 if (if_lookup_by_ipv4 (&remote
->sin
.sin_addr
, peer
->bgp
->vrf_id
))
1079 peer
->shared_network
= 1;
1081 peer
->shared_network
= 0;
1082 #endif /* HAVE_IPV6 */
1086 /* IPv6 connection, fetch and store IPv4 local address if any. */
1087 if (local
->sa
.sa_family
== AF_INET6
)
1089 struct interface
*direct
= NULL
;
1092 ret
= if_get_ipv4_address(ifp
, &nexthop
->v4
);
1093 if (!ret
&& peer
->local_id
.s_addr
)
1094 nexthop
->v4
= peer
->local_id
;
1097 if (! IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
))
1099 memcpy (&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
1102 /* If directory connected set link-local address. */
1103 direct
= if_lookup_by_ipv6 (&remote
->sin6
.sin6_addr
,
1104 remote
->sin6
.sin6_scope_id
, peer
->bgp
->vrf_id
);
1106 if_get_ipv6_local (ifp
, &nexthop
->v6_local
);
1109 /* Link-local address. */
1111 ret
= if_get_ipv6_global (ifp
, &nexthop
->v6_global
);
1113 /* If there is no global address. Set link-local address as
1114 global. I know this break RFC specification... */
1115 /* In this scenario, the expectation for interop is that the
1116 * network admin would use a route-map to specify the global
1120 memcpy (&nexthop
->v6_global
, &local
->sin6
.sin6_addr
,
1122 /* Always set the link-local address */
1123 memcpy (&nexthop
->v6_local
, &local
->sin6
.sin6_addr
,
1127 if (IN6_IS_ADDR_LINKLOCAL (&local
->sin6
.sin6_addr
) ||
1128 if_lookup_by_ipv6 (&remote
->sin6
.sin6_addr
, remote
->sin6
.sin6_scope_id
,
1130 peer
->shared_network
= 1;
1132 peer
->shared_network
= 0;
1135 /* KAME stack specific treatment. */
1137 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->v6_global
)
1138 && IN6_LINKLOCAL_IFINDEX (nexthop
->v6_global
))
1140 SET_IN6_LINKLOCAL_IFINDEX (nexthop
->v6_global
, 0);
1142 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->v6_local
)
1143 && IN6_LINKLOCAL_IFINDEX (nexthop
->v6_local
))
1145 SET_IN6_LINKLOCAL_IFINDEX (nexthop
->v6_local
, 0);
1148 #endif /* HAVE_IPV6 */
1150 /* If we have identified the local interface, there is no error for now. */
1154 static struct in6_addr
*
1155 bgp_info_to_ipv6_nexthop (struct bgp_info
*info
)
1157 struct in6_addr
*nexthop
= NULL
;
1159 /* Only global address nexthop exists. */
1160 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL
)
1161 nexthop
= &info
->attr
->extra
->mp_nexthop_global
;
1163 /* If both global and link-local address present. */
1164 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1166 /* Workaround for Cisco's nexthop bug. */
1167 if (IN6_IS_ADDR_UNSPECIFIED (&info
->attr
->extra
->mp_nexthop_global
)
1168 && info
->peer
->su_remote
->sa
.sa_family
== AF_INET6
)
1169 nexthop
= &info
->peer
->su_remote
->sin6
.sin6_addr
;
1171 nexthop
= &info
->attr
->extra
->mp_nexthop_local
;
1178 bgp_table_map_apply (struct route_map
*map
, struct prefix
*p
,
1179 struct bgp_info
*info
)
1181 if (route_map_apply(map
, p
, RMAP_BGP
, info
) != RMAP_DENYMATCH
)
1184 if (bgp_debug_zebra(p
))
1186 if (p
->family
== AF_INET
)
1188 char buf
[2][INET_ADDRSTRLEN
];
1189 zlog_debug("Zebra rmap deny: IPv4 route %s/%d nexthop %s",
1190 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1192 inet_ntop(AF_INET
, &info
->attr
->nexthop
, buf
[1],
1195 if (p
->family
== AF_INET6
)
1197 char buf
[2][INET6_ADDRSTRLEN
];
1198 zlog_debug("Zebra rmap deny: IPv6 route %s/%d nexthop %s",
1199 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1201 inet_ntop(AF_INET6
, bgp_info_to_ipv6_nexthop(info
), buf
[1],
1209 bgp_zebra_announce (struct prefix
*p
, struct bgp_info
*info
, struct bgp
*bgp
,
1210 afi_t afi
, safi_t safi
)
1215 struct bgp_info
*mpinfo
;
1216 size_t oldsize
, newsize
;
1217 u_int32_t nhcount
, metric
;
1218 struct bgp_info local_info
;
1219 struct bgp_info
*info_cp
= &local_info
;
1222 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1223 * know of this instance.
1225 if (!bgp_install_info_to_zebra (bgp
))
1228 if ((p
->family
== AF_INET
&&
1229 !vrf_bitmap_check (zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_BGP
], bgp
->vrf_id
))
1230 || (p
->family
== AF_INET6
&&
1231 !vrf_bitmap_check (zclient
->redist
[AFI_IP6
][ZEBRA_ROUTE_BGP
], bgp
->vrf_id
)))
1234 if (bgp
->main_zebra_update_hold
)
1240 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1241 tag
= info
->attr
->extra
->tag
;
1245 /* When we create an aggregate route we must also install a Null0 route in
1247 if (info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1248 SET_FLAG (flags
, ZEBRA_FLAG_BLACKHOLE
);
1250 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
||
1251 info
->sub_type
== BGP_ROUTE_AGGREGATE
)
1253 SET_FLAG (flags
, ZEBRA_FLAG_IBGP
);
1254 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1257 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1258 || CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1259 || bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1261 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1263 nhcount
= 1 + bgp_info_mpath_count (info
);
1265 if (p
->family
== AF_INET
&& !BGP_ATTR_NEXTHOP_AFI_IP6(info
->attr
))
1267 struct zapi_ipv4 api
;
1268 struct in_addr
*nexthop
;
1269 char buf
[2][INET_ADDRSTRLEN
];
1270 int valid_nh_count
= 0;
1272 /* resize nexthop buffer size if necessary */
1273 if ((oldsize
= stream_get_size (bgp_nexthop_buf
)) <
1274 (sizeof (struct in_addr
*) * nhcount
))
1276 newsize
= (sizeof (struct in_addr
*) * nhcount
);
1277 newsize
= stream_resize (bgp_nexthop_buf
, newsize
);
1278 if (newsize
== oldsize
)
1280 zlog_err ("can't resize nexthop buffer");
1284 stream_reset (bgp_nexthop_buf
);
1287 /* Metric is currently based on the best-path only. */
1288 metric
= info
->attr
->med
;
1290 if (bgp
->table_map
[afi
][safi
].name
)
1292 BGP_INFO_ATTR_BUF_INIT();
1294 /* Copy info and attributes, so the route-map apply doesn't modify the
1296 BGP_INFO_ATTR_BUF_COPY(info
, info_cp
);
1297 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1299 metric
= info_cp
->attr
->med
;
1300 nexthop
= &info_cp
->attr
->nexthop
;
1302 if (info_cp
->attr
->extra
)
1303 tag
= info_cp
->attr
->extra
->tag
;
1305 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1309 nexthop
= &info
->attr
->nexthop
;
1314 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in_addr
*));
1318 for (mpinfo
= bgp_info_mpath_first (info
); mpinfo
;
1319 mpinfo
= bgp_info_mpath_next (mpinfo
))
1323 if (bgp
->table_map
[afi
][safi
].name
)
1325 /* Copy info and attributes, so the route-map apply doesn't modify the
1327 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
1328 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1329 nexthop
= &info_cp
->attr
->nexthop
;
1330 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1334 nexthop
= &mpinfo
->attr
->nexthop
;
1337 if (nexthop
== NULL
)
1340 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in_addr
*));
1344 api
.vrf_id
= bgp
->vrf_id
;
1346 api
.type
= ZEBRA_ROUTE_BGP
;
1350 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1352 /* Note that this currently only applies to Null0 routes for aggregates.
1353 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv4_route to encode a special
1354 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
1355 * do not want to also encode the 0.0.0.0 nexthop for the aggregate route.
1357 if (CHECK_FLAG(flags
, ZEBRA_FLAG_BLACKHOLE
))
1358 api
.nexthop_num
= 0;
1360 api
.nexthop_num
= valid_nh_count
;
1362 api
.nexthop
= (struct in_addr
**)STREAM_DATA (bgp_nexthop_buf
);
1363 api
.ifindex_num
= 0;
1364 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1365 api
.metric
= metric
;
1369 SET_FLAG (api
.message
, ZAPI_MESSAGE_TAG
);
1373 distance
= bgp_distance_apply (p
, info
, bgp
);
1377 SET_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
);
1378 api
.distance
= distance
;
1381 if (bgp_debug_zebra(p
))
1384 zlog_debug("Tx IPv4 route %s VRF %u %s/%d metric %u tag %d"
1385 " count %d", (valid_nh_count
? "add":"delete"),
1387 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1388 p
->prefixlen
, api
.metric
, api
.tag
, api
.nexthop_num
);
1389 for (i
= 0; i
< api
.nexthop_num
; i
++)
1390 zlog_debug(" IPv4 [nexthop %d] %s", i
+1,
1391 inet_ntop(AF_INET
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1394 zapi_ipv4_route (valid_nh_count
? ZEBRA_IPV4_ROUTE_ADD
: ZEBRA_IPV4_ROUTE_DELETE
,
1395 zclient
, (struct prefix_ipv4
*) p
, &api
);
1399 /* We have to think about a IPv6 link-local address curse. */
1400 if (p
->family
== AF_INET6
||
1401 (p
->family
== AF_INET
&& BGP_ATTR_NEXTHOP_AFI_IP6(info
->attr
)))
1403 unsigned int ifindex
;
1404 struct in6_addr
*nexthop
;
1405 struct zapi_ipv6 api
;
1406 int valid_nh_count
= 0;
1407 char buf
[2][INET6_ADDRSTRLEN
];
1409 /* resize nexthop buffer size if necessary */
1410 if ((oldsize
= stream_get_size (bgp_nexthop_buf
)) <
1411 (sizeof (struct in6_addr
*) * nhcount
))
1413 newsize
= (sizeof (struct in6_addr
*) * nhcount
);
1414 newsize
= stream_resize (bgp_nexthop_buf
, newsize
);
1415 if (newsize
== oldsize
)
1417 zlog_err ("can't resize nexthop buffer");
1421 stream_reset (bgp_nexthop_buf
);
1423 /* resize ifindices buffer size if necessary */
1424 if ((oldsize
= stream_get_size (bgp_ifindices_buf
)) <
1425 (sizeof (unsigned int) * nhcount
))
1427 newsize
= (sizeof (unsigned int) * nhcount
);
1428 newsize
= stream_resize (bgp_ifindices_buf
, newsize
);
1429 if (newsize
== oldsize
)
1431 zlog_err ("can't resize nexthop buffer");
1435 stream_reset (bgp_ifindices_buf
);
1440 assert (info
->attr
->extra
);
1442 /* Metric is currently based on the best-path only. */
1443 metric
= info
->attr
->med
;
1445 if (bgp
->table_map
[afi
][safi
].name
)
1447 BGP_INFO_ATTR_BUF_INIT();
1449 /* Copy info and attributes, so the route-map apply doesn't modify the
1451 BGP_INFO_ATTR_BUF_COPY(info
, info_cp
);
1452 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1454 metric
= info_cp
->attr
->med
;
1455 nexthop
= bgp_info_to_ipv6_nexthop(info_cp
);
1457 if (info_cp
->attr
->extra
)
1458 tag
= info_cp
->attr
->extra
->tag
;
1460 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1464 nexthop
= bgp_info_to_ipv6_nexthop(info
);
1469 if (info
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1470 if (info
->peer
->nexthop
.ifp
)
1471 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
1475 if (info
->peer
->conf_if
|| info
->peer
->ifname
)
1476 ifindex
= if_nametoindex (info
->peer
->conf_if
? info
->peer
->conf_if
: info
->peer
->ifname
);
1477 else if (info
->peer
->nexthop
.ifp
)
1478 ifindex
= info
->peer
->nexthop
.ifp
->ifindex
;
1480 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in6_addr
*));
1481 stream_put (bgp_ifindices_buf
, &ifindex
, sizeof (unsigned int));
1485 for (mpinfo
= bgp_info_mpath_first (info
); mpinfo
;
1486 mpinfo
= bgp_info_mpath_next (mpinfo
))
1491 if (bgp
->table_map
[afi
][safi
].name
)
1493 /* Copy info and attributes, so the route-map apply doesn't modify the
1495 BGP_INFO_ATTR_BUF_COPY(mpinfo
, info_cp
);
1496 if (bgp_table_map_apply(bgp
->table_map
[afi
][safi
].map
, p
, info_cp
))
1497 nexthop
= bgp_info_to_ipv6_nexthop(info_cp
);
1498 BGP_INFO_ATTR_BUF_FREE(info_cp
);
1502 nexthop
= bgp_info_to_ipv6_nexthop(mpinfo
);
1505 if (nexthop
== NULL
)
1508 if (mpinfo
->attr
->extra
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
)
1509 if (mpinfo
->peer
->nexthop
.ifp
)
1510 ifindex
= mpinfo
->peer
->nexthop
.ifp
->ifindex
;
1514 if (mpinfo
->peer
->conf_if
|| mpinfo
->peer
->ifname
)
1515 ifindex
= if_nametoindex (mpinfo
->peer
->conf_if
? mpinfo
->peer
->conf_if
: mpinfo
->peer
->ifname
);
1516 else if (mpinfo
->peer
->nexthop
.ifp
)
1517 ifindex
= mpinfo
->peer
->nexthop
.ifp
->ifindex
;
1522 stream_put (bgp_nexthop_buf
, &nexthop
, sizeof (struct in6_addr
*));
1523 stream_put (bgp_ifindices_buf
, &ifindex
, sizeof (unsigned int));
1527 /* Make Zebra API structure. */
1528 api
.vrf_id
= bgp
->vrf_id
;
1530 api
.type
= ZEBRA_ROUTE_BGP
;
1534 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1536 /* Note that this currently only applies to Null0 routes for aggregates.
1537 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv6_route to encode a special
1538 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
1539 * do not want to also encode the :: nexthop for the aggregate route.
1541 if (CHECK_FLAG(flags
, ZEBRA_FLAG_BLACKHOLE
))
1542 api
.nexthop_num
= 0;
1544 api
.nexthop_num
= valid_nh_count
;
1546 api
.nexthop
= (struct in6_addr
**)STREAM_DATA (bgp_nexthop_buf
);
1547 SET_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
);
1548 api
.ifindex_num
= valid_nh_count
;
1549 api
.ifindex
= (unsigned int *)STREAM_DATA (bgp_ifindices_buf
);
1550 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1551 api
.metric
= metric
;
1555 SET_FLAG (api
.message
, ZAPI_MESSAGE_TAG
);
1559 if (p
->family
== AF_INET
)
1561 if (bgp_debug_zebra(p
))
1564 zlog_debug("Tx IPv4 route %s VRF %u %s/%d metric %u tag %d",
1565 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1566 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1567 p
->prefixlen
, api
.metric
, api
.tag
);
1568 for (i
= 0; i
< api
.nexthop_num
; i
++)
1569 zlog_debug(" IPv6 [nexthop %d] %s", i
+1,
1570 inet_ntop(AF_INET6
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1574 zapi_ipv4_route_ipv6_nexthop (ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
,
1575 zclient
, (struct prefix_ipv4
*) p
,
1576 (struct zapi_ipv6
*)&api
);
1578 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
,
1579 zclient
, (struct prefix_ipv4
*) p
, (struct zapi_ipv4
*)&api
);
1583 if (bgp_debug_zebra(p
))
1586 zlog_debug("Tx IPv6 route %s VRF %u %s/%d metric %u tag %d",
1587 valid_nh_count
? "add" : "delete", bgp
->vrf_id
,
1588 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1589 p
->prefixlen
, api
.metric
, api
.tag
);
1590 for (i
= 0; i
< api
.nexthop_num
; i
++)
1591 zlog_debug(" IPv6 [nexthop %d] %s", i
+1,
1592 inet_ntop(AF_INET6
, api
.nexthop
[i
], buf
[1], sizeof(buf
[1])));
1595 zapi_ipv6_route (valid_nh_count
?
1596 ZEBRA_IPV6_ROUTE_ADD
: ZEBRA_IPV6_ROUTE_DELETE
,
1597 zclient
, (struct prefix_ipv6
*) p
, &api
);
1600 #endif /* HAVE_IPV6 */
1603 /* Announce all routes of a table to zebra */
1605 bgp_zebra_announce_table (struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1607 struct bgp_node
*rn
;
1608 struct bgp_table
*table
;
1609 struct bgp_info
*ri
;
1611 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1612 * know of this instance.
1614 if (!bgp_install_info_to_zebra (bgp
))
1617 table
= bgp
->rib
[afi
][safi
];
1620 for (rn
= bgp_table_top (table
); rn
; rn
= bgp_route_next (rn
))
1621 for (ri
= rn
->info
; ri
; ri
= ri
->next
)
1622 if (CHECK_FLAG (ri
->flags
, BGP_INFO_SELECTED
)
1623 && ri
->type
== ZEBRA_ROUTE_BGP
1624 && ri
->sub_type
== BGP_ROUTE_NORMAL
)
1625 bgp_zebra_announce (&rn
->p
, ri
, bgp
, afi
, safi
);
1629 bgp_zebra_withdraw (struct prefix
*p
, struct bgp_info
*info
, safi_t safi
)
1637 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1638 * know of this instance.
1640 if (!bgp_install_info_to_zebra (peer
->bgp
))
1643 if ((p
->family
== AF_INET
&&
1644 !vrf_bitmap_check (zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_BGP
], peer
->bgp
->vrf_id
))
1645 || (p
->family
== AF_INET6
&&
1646 !vrf_bitmap_check (zclient
->redist
[AFI_IP6
][ZEBRA_ROUTE_BGP
], peer
->bgp
->vrf_id
)))
1651 if (peer
->sort
== BGP_PEER_IBGP
)
1653 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1654 SET_FLAG (flags
, ZEBRA_FLAG_IBGP
);
1657 if ((peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
!= 1)
1658 || CHECK_FLAG (peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1659 || bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1660 SET_FLAG (flags
, ZEBRA_FLAG_INTERNAL
);
1662 if (p
->family
== AF_INET
)
1664 struct zapi_ipv4 api
;
1666 api
.vrf_id
= peer
->bgp
->vrf_id
;
1669 api
.type
= ZEBRA_ROUTE_BGP
;
1673 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1674 api
.nexthop_num
= 0;
1676 api
.ifindex_num
= 0;
1677 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1678 api
.metric
= info
->attr
->med
;
1680 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1682 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1683 api
.tag
= info
->attr
->extra
->tag
;
1686 if (bgp_debug_zebra(p
))
1688 char buf
[2][INET_ADDRSTRLEN
];
1689 zlog_debug("Tx IPv4 route delete VRF %u %s/%d metric %u tag %d",
1691 inet_ntop(AF_INET
, &p
->u
.prefix4
, buf
[0], sizeof(buf
[0])),
1692 p
->prefixlen
, api
.metric
, api
.tag
);
1695 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
, zclient
,
1696 (struct prefix_ipv4
*) p
, &api
);
1699 /* We have to think about a IPv6 link-local address curse. */
1700 if (p
->family
== AF_INET6
)
1702 struct zapi_ipv6 api
;
1704 assert (info
->attr
->extra
);
1706 api
.vrf_id
= peer
->bgp
->vrf_id
;
1708 api
.type
= ZEBRA_ROUTE_BGP
;
1712 SET_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
);
1713 api
.nexthop_num
= 0;
1715 api
.ifindex_num
= 0;
1716 SET_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
);
1717 api
.metric
= info
->attr
->med
;
1719 if ((info
->attr
->extra
) && (info
->attr
->extra
->tag
!= 0))
1721 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
1722 api
.tag
= info
->attr
->extra
->tag
;
1725 if (bgp_debug_zebra(p
))
1727 char buf
[2][INET6_ADDRSTRLEN
];
1728 zlog_debug("Tx IPv6 route delete VRF %u %s/%d metric %u tag %d",
1730 inet_ntop(AF_INET6
, &p
->u
.prefix6
, buf
[0], sizeof(buf
[0])),
1731 p
->prefixlen
, api
.metric
, api
.tag
);
1734 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE
, zclient
,
1735 (struct prefix_ipv6
*) p
, &api
);
1737 #endif /* HAVE_IPV6 */
1740 bgp_redist_lookup (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1742 struct list
*red_list
;
1743 struct listnode
*node
;
1744 struct bgp_redist
*red
;
1746 red_list
= bgp
->redist
[afi
][type
];
1750 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
1751 if (red
->instance
== instance
)
1758 bgp_redist_add (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1760 struct list
*red_list
;
1761 struct bgp_redist
*red
;
1763 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1767 if (!bgp
->redist
[afi
][type
])
1768 bgp
->redist
[afi
][type
] = list_new();
1770 red_list
= bgp
->redist
[afi
][type
];
1771 red
= (struct bgp_redist
*)XCALLOC(MTYPE_BGP_REDIST
, sizeof(struct bgp_redist
));
1772 red
->instance
= instance
;
1774 listnode_add(red_list
, red
);
1780 bgp_redist_del (struct bgp
*bgp
, afi_t afi
, u_char type
, u_short instance
)
1782 struct bgp_redist
*red
;
1784 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1788 listnode_delete(bgp
->redist
[afi
][type
], red
);
1789 if (!bgp
->redist
[afi
][type
]->count
)
1791 list_free(bgp
->redist
[afi
][type
]);
1792 bgp
->redist
[afi
][type
] = NULL
;
1797 /* Other routes redistribution into BGP. */
1799 bgp_redistribute_set (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1802 /* Return if already redistribute flag is set. */
1805 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1808 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1812 if (vrf_bitmap_check (zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1815 vrf_bitmap_set (zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1818 /* Don't try to register if we're not connected to Zebra or Zebra doesn't
1819 * know of this instance.
1821 if (!bgp_install_info_to_zebra (bgp
))
1824 if (BGP_DEBUG (zebra
, ZEBRA
))
1825 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1827 zebra_route_string(type
), instance
);
1829 /* Send distribute add message to zebra. */
1830 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1831 instance
, bgp
->vrf_id
);
1837 bgp_redistribute_resend (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1839 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1840 * know of this instance.
1842 if (!bgp_install_info_to_zebra (bgp
))
1845 if (BGP_DEBUG (zebra
, ZEBRA
))
1846 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1848 zebra_route_string(type
), instance
);
1850 /* Send distribute add message to zebra. */
1851 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
,
1852 instance
, bgp
->vrf_id
);
1853 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, type
,
1854 instance
, bgp
->vrf_id
);
1859 /* Redistribute with route-map specification. */
1861 bgp_redistribute_rmap_set (struct bgp_redist
*red
, const char *name
)
1864 && (strcmp (red
->rmap
.name
, name
) == 0))
1868 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1869 red
->rmap
.name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, name
);
1870 red
->rmap
.map
= route_map_lookup_by_name (name
);
1875 /* Redistribute with metric specification. */
1877 bgp_redistribute_metric_set (struct bgp
*bgp
, struct bgp_redist
*red
, afi_t afi
,
1878 int type
, u_int32_t metric
)
1880 struct bgp_node
*rn
;
1881 struct bgp_info
*ri
;
1883 if (red
->redist_metric_flag
1884 && red
->redist_metric
== metric
)
1887 red
->redist_metric_flag
= 1;
1888 red
->redist_metric
= metric
;
1890 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
; rn
= bgp_route_next(rn
)) {
1891 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
1892 if (ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
&& ri
->type
== type
&&
1893 ri
->instance
== red
->instance
) {
1894 ri
->attr
->med
= red
->redist_metric
;
1895 bgp_info_set_flag(rn
, ri
, BGP_INFO_ATTR_CHANGED
);
1896 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
1904 /* Unset redistribution. */
1906 bgp_redistribute_unreg (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1908 struct bgp_redist
*red
;
1910 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1914 /* Return if zebra connection is disabled. */
1917 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1919 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1923 if (! vrf_bitmap_check (zclient
->redist
[afi
][type
], bgp
->vrf_id
))
1925 vrf_bitmap_unset (zclient
->redist
[afi
][type
], bgp
->vrf_id
);
1928 if (bgp_install_info_to_zebra (bgp
))
1930 /* Send distribute delete message to zebra. */
1931 if (BGP_DEBUG (zebra
, ZEBRA
))
1932 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1933 bgp
->vrf_id
, afi
, zebra_route_string(type
), instance
);
1934 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, type
, instance
,
1938 /* Withdraw redistributed routes from current BGP's routing table. */
1939 bgp_redistribute_withdraw (bgp
, afi
, type
, instance
);
1944 /* Unset redistribution. */
1946 bgp_redistribute_unset (struct bgp
*bgp
, afi_t afi
, int type
, u_short instance
)
1948 struct bgp_redist
*red
;
1950 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
1954 bgp_redistribute_unreg(bgp
, afi
, type
, instance
);
1956 /* Unset route-map. */
1958 XFREE(MTYPE_ROUTE_MAP_NAME
, red
->rmap
.name
);
1959 red
->rmap
.name
= NULL
;
1960 red
->rmap
.map
= NULL
;
1963 red
->redist_metric_flag
= 0;
1964 red
->redist_metric
= 0;
1966 bgp_redist_del(bgp
, afi
, type
, instance
);
1972 bgp_zclient_reset (void)
1974 zclient_reset (zclient
);
1977 /* Register this instance with Zebra. Invoked upon connect (for
1978 * default instance) and when other VRFs are learnt (or created and
1982 bgp_zebra_instance_register (struct bgp
*bgp
)
1984 /* Don't try to register if we're not connected to Zebra */
1985 if (!zclient
|| zclient
->sock
< 0)
1988 if (BGP_DEBUG (zebra
, ZEBRA
))
1989 zlog_debug("Registering VRF %u", bgp
->vrf_id
);
1991 /* Register for router-id, interfaces, redistributed routes. */
1992 zclient_send_reg_requests (zclient
, bgp
->vrf_id
);
1995 /* Deregister this instance with Zebra. Invoked upon the instance
1996 * being deleted (default or VRF) and it is already registered.
1999 bgp_zebra_instance_deregister (struct bgp
*bgp
)
2001 /* Don't try to deregister if we're not connected to Zebra */
2002 if (zclient
->sock
< 0)
2005 if (BGP_DEBUG (zebra
, ZEBRA
))
2006 zlog_debug("Deregistering VRF %u", bgp
->vrf_id
);
2008 /* Deregister for router-id, interfaces, redistributed routes. */
2009 zclient_send_dereg_requests (zclient
, bgp
->vrf_id
);
2013 bgp_zebra_initiate_radv (struct bgp
*bgp
, struct peer
*peer
)
2015 /* Don't try to initiate if we're not connected to Zebra */
2016 if (zclient
->sock
< 0)
2019 if (BGP_DEBUG (zebra
, ZEBRA
))
2020 zlog_debug("%u: Initiating RA for peer %s", bgp
->vrf_id
, peer
->host
);
2022 zclient_send_interface_radv_req (zclient
, bgp
->vrf_id
, peer
->ifp
, 1);
2026 bgp_zebra_terminate_radv (struct bgp
*bgp
, struct peer
*peer
)
2028 /* Don't try to terminate if we're not connected to Zebra */
2029 if (zclient
->sock
< 0)
2032 if (BGP_DEBUG (zebra
, ZEBRA
))
2033 zlog_debug("%u: Terminating RA for peer %s", bgp
->vrf_id
, peer
->host
);
2035 zclient_send_interface_radv_req (zclient
, bgp
->vrf_id
, peer
->ifp
, 0);
2038 /* BGP has established connection with Zebra. */
2040 bgp_zebra_connected (struct zclient
*zclient
)
2044 /* At this point, we may or may not have BGP instances configured, but
2045 * we're only interested in the default VRF (others wouldn't have learnt
2046 * the VRF from Zebra yet.)
2048 bgp
= bgp_get_default();
2052 bgp_zebra_instance_register (bgp
);
2054 /* TODO - What if we have peers and networks configured, do we have to
2061 bgp_zebra_init (struct thread_master
*master
)
2063 /* Set default values. */
2064 zclient
= zclient_new (master
);
2065 zclient_init (zclient
, ZEBRA_ROUTE_BGP
, 0);
2066 zclient
->zebra_connected
= bgp_zebra_connected
;
2067 zclient
->router_id_update
= bgp_router_id_update
;
2068 zclient
->interface_add
= bgp_interface_add
;
2069 zclient
->interface_delete
= bgp_interface_delete
;
2070 zclient
->interface_address_add
= bgp_interface_address_add
;
2071 zclient
->interface_address_delete
= bgp_interface_address_delete
;
2072 zclient
->interface_nbr_address_add
= bgp_interface_nbr_address_add
;
2073 zclient
->interface_nbr_address_delete
= bgp_interface_nbr_address_delete
;
2074 zclient
->interface_vrf_update
= bgp_interface_vrf_update
;
2075 zclient
->ipv4_route_add
= zebra_read_ipv4
;
2076 zclient
->ipv4_route_delete
= zebra_read_ipv4
;
2077 zclient
->redistribute_route_ipv4_add
= zebra_read_ipv4
;
2078 zclient
->redistribute_route_ipv4_del
= zebra_read_ipv4
;
2079 zclient
->interface_up
= bgp_interface_up
;
2080 zclient
->interface_down
= bgp_interface_down
;
2081 zclient
->ipv6_route_add
= zebra_read_ipv6
;
2082 zclient
->ipv6_route_delete
= zebra_read_ipv6
;
2083 zclient
->redistribute_route_ipv6_add
= zebra_read_ipv6
;
2084 zclient
->redistribute_route_ipv6_del
= zebra_read_ipv6
;
2085 zclient
->nexthop_update
= bgp_read_nexthop_update
;
2086 zclient
->import_check_update
= bgp_read_import_check_update
;
2088 bgp_nexthop_buf
= stream_new(BGP_NEXTHOP_BUF_SIZE
);
2089 bgp_ifindices_buf
= stream_new(BGP_IFINDICES_BUF_SIZE
);